* dwarf2read.c (ABBREV_HASH_SIZE): Remove enclosing #ifndef/#endif.
[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 /* The data in a compilation unit header, after target2host
314 translation, looks like this. */
315 struct comp_unit_head
316 {
317 unsigned int length;
318 short version;
319 unsigned char addr_size;
320 unsigned char signed_addr_p;
321 sect_offset abbrev_offset;
322
323 /* Size of file offsets; either 4 or 8. */
324 unsigned int offset_size;
325
326 /* Size of the length field; either 4 or 12. */
327 unsigned int initial_length_size;
328
329 /* Offset to the first byte of this compilation unit header in the
330 .debug_info section, for resolving relative reference dies. */
331 sect_offset offset;
332
333 /* Offset to first die in this cu from the start of the cu.
334 This will be the first byte following the compilation unit header. */
335 cu_offset first_die_offset;
336 };
337
338 /* Type used for delaying computation of method physnames.
339 See comments for compute_delayed_physnames. */
340 struct delayed_method_info
341 {
342 /* The type to which the method is attached, i.e., its parent class. */
343 struct type *type;
344
345 /* The index of the method in the type's function fieldlists. */
346 int fnfield_index;
347
348 /* The index of the method in the fieldlist. */
349 int index;
350
351 /* The name of the DIE. */
352 const char *name;
353
354 /* The DIE associated with this method. */
355 struct die_info *die;
356 };
357
358 typedef struct delayed_method_info delayed_method_info;
359 DEF_VEC_O (delayed_method_info);
360
361 /* Internal state when decoding a particular compilation unit. */
362 struct dwarf2_cu
363 {
364 /* The objfile containing this compilation unit. */
365 struct objfile *objfile;
366
367 /* The header of the compilation unit. */
368 struct comp_unit_head header;
369
370 /* Base address of this compilation unit. */
371 CORE_ADDR base_address;
372
373 /* Non-zero if base_address has been set. */
374 int base_known;
375
376 /* The language we are debugging. */
377 enum language language;
378 const struct language_defn *language_defn;
379
380 const char *producer;
381
382 /* The generic symbol table building routines have separate lists for
383 file scope symbols and all all other scopes (local scopes). So
384 we need to select the right one to pass to add_symbol_to_list().
385 We do it by keeping a pointer to the correct list in list_in_scope.
386
387 FIXME: The original dwarf code just treated the file scope as the
388 first local scope, and all other local scopes as nested local
389 scopes, and worked fine. Check to see if we really need to
390 distinguish these in buildsym.c. */
391 struct pending **list_in_scope;
392
393 /* The abbrev table for this CU.
394 Normally this points to the abbrev table in the objfile.
395 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
396 struct abbrev_table *abbrev_table;
397
398 /* Hash table holding all the loaded partial DIEs
399 with partial_die->offset.SECT_OFF as hash. */
400 htab_t partial_dies;
401
402 /* Storage for things with the same lifetime as this read-in compilation
403 unit, including partial DIEs. */
404 struct obstack comp_unit_obstack;
405
406 /* When multiple dwarf2_cu structures are living in memory, this field
407 chains them all together, so that they can be released efficiently.
408 We will probably also want a generation counter so that most-recently-used
409 compilation units are cached... */
410 struct dwarf2_per_cu_data *read_in_chain;
411
412 /* Backchain to our per_cu entry if the tree has been built. */
413 struct dwarf2_per_cu_data *per_cu;
414
415 /* How many compilation units ago was this CU last referenced? */
416 int last_used;
417
418 /* A hash table of DIE cu_offset for following references with
419 die_info->offset.sect_off as hash. */
420 htab_t die_hash;
421
422 /* Full DIEs if read in. */
423 struct die_info *dies;
424
425 /* A set of pointers to dwarf2_per_cu_data objects for compilation
426 units referenced by this one. Only set during full symbol processing;
427 partial symbol tables do not have dependencies. */
428 htab_t dependencies;
429
430 /* Header data from the line table, during full symbol processing. */
431 struct line_header *line_header;
432
433 /* A list of methods which need to have physnames computed
434 after all type information has been read. */
435 VEC (delayed_method_info) *method_list;
436
437 /* To be copied to symtab->call_site_htab. */
438 htab_t call_site_htab;
439
440 /* Non-NULL if this CU came from a DWO file.
441 There is an invariant here that is important to remember:
442 Except for attributes copied from the top level DIE in the "main"
443 (or "stub") file in preparation for reading the DWO file
444 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
445 Either there isn't a DWO file (in which case this is NULL and the point
446 is moot), or there is and either we're not going to read it (in which
447 case this is NULL) or there is and we are reading it (in which case this
448 is non-NULL). */
449 struct dwo_unit *dwo_unit;
450
451 /* The DW_AT_addr_base attribute if present, zero otherwise
452 (zero is a valid value though).
453 Note this value comes from the stub CU/TU's DIE. */
454 ULONGEST addr_base;
455
456 /* The DW_AT_ranges_base attribute if present, zero otherwise
457 (zero is a valid value though).
458 Note this value comes from the stub CU/TU's DIE.
459 Also note that the value is zero in the non-DWO case so this value can
460 be used without needing to know whether DWO files are in use or not. */
461 ULONGEST ranges_base;
462
463 /* Mark used when releasing cached dies. */
464 unsigned int mark : 1;
465
466 /* This CU references .debug_loc. See the symtab->locations_valid field.
467 This test is imperfect as there may exist optimized debug code not using
468 any location list and still facing inlining issues if handled as
469 unoptimized code. For a future better test see GCC PR other/32998. */
470 unsigned int has_loclist : 1;
471
472 /* These cache the results for producer_is_gxx_lt_4_6 and producer_is_icc.
473 CHECKED_PRODUCER is set if both PRODUCER_IS_GXX_LT_4_6 and PRODUCER_IS_ICC
474 are valid. This information is cached because profiling CU expansion
475 showed excessive time spent in producer_is_gxx_lt_4_6. */
476 unsigned int checked_producer : 1;
477 unsigned int producer_is_gxx_lt_4_6 : 1;
478 unsigned int producer_is_icc : 1;
479 };
480
481 /* Persistent data held for a compilation unit, even when not
482 processing it. We put a pointer to this structure in the
483 read_symtab_private field of the psymtab. */
484
485 struct dwarf2_per_cu_data
486 {
487 /* The start offset and length of this compilation unit. 2**29-1
488 bytes should suffice to store the length of any compilation unit
489 - if it doesn't, GDB will fall over anyway.
490 NOTE: Unlike comp_unit_head.length, this length includes
491 initial_length_size.
492 If the DIE refers to a DWO file, this is always of the original die,
493 not the DWO file. */
494 sect_offset offset;
495 unsigned int length : 29;
496
497 /* Flag indicating this compilation unit will be read in before
498 any of the current compilation units are processed. */
499 unsigned int queued : 1;
500
501 /* This flag will be set when reading partial DIEs if we need to load
502 absolutely all DIEs for this compilation unit, instead of just the ones
503 we think are interesting. It gets set if we look for a DIE in the
504 hash table and don't find it. */
505 unsigned int load_all_dies : 1;
506
507 /* Non-zero if this CU is from .debug_types. */
508 unsigned int is_debug_types : 1;
509
510 /* The section this CU/TU lives in.
511 If the DIE refers to a DWO file, this is always the original die,
512 not the DWO file. */
513 struct dwarf2_section_info *info_or_types_section;
514
515 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
516 of the CU cache it gets reset to NULL again. */
517 struct dwarf2_cu *cu;
518
519 /* The corresponding objfile.
520 Normally we can get the objfile from dwarf2_per_objfile.
521 However we can enter this file with just a "per_cu" handle. */
522 struct objfile *objfile;
523
524 /* When using partial symbol tables, the 'psymtab' field is active.
525 Otherwise the 'quick' field is active. */
526 union
527 {
528 /* The partial symbol table associated with this compilation unit,
529 or NULL for unread partial units. */
530 struct partial_symtab *psymtab;
531
532 /* Data needed by the "quick" functions. */
533 struct dwarf2_per_cu_quick_data *quick;
534 } v;
535
536 /* The CUs we import using DW_TAG_imported_unit. This is filled in
537 while reading psymtabs, used to compute the psymtab dependencies,
538 and then cleared. Then it is filled in again while reading full
539 symbols, and only deleted when the objfile is destroyed. */
540 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
541 };
542
543 /* Entry in the signatured_types hash table. */
544
545 struct signatured_type
546 {
547 /* The type's signature. */
548 ULONGEST signature;
549
550 /* Offset in the TU of the type's DIE, as read from the TU header.
551 If the definition lives in a DWO file, this value is unusable. */
552 cu_offset type_offset_in_tu;
553
554 /* Offset in the section of the type's DIE.
555 If the definition lives in a DWO file, this is the offset in the
556 .debug_types.dwo section.
557 The value is zero until the actual value is known.
558 Zero is otherwise not a valid section offset. */
559 sect_offset type_offset_in_section;
560
561 /* The CU(/TU) of this type. */
562 struct dwarf2_per_cu_data per_cu;
563 };
564
565 /* These sections are what may appear in a "dwo" file. */
566
567 struct dwo_sections
568 {
569 struct dwarf2_section_info abbrev;
570 struct dwarf2_section_info info;
571 struct dwarf2_section_info line;
572 struct dwarf2_section_info loc;
573 struct dwarf2_section_info macinfo;
574 struct dwarf2_section_info macro;
575 struct dwarf2_section_info str;
576 struct dwarf2_section_info str_offsets;
577 VEC (dwarf2_section_info_def) *types;
578 };
579
580 /* Common bits of DWO CUs/TUs. */
581
582 struct dwo_unit
583 {
584 /* Backlink to the containing struct dwo_file. */
585 struct dwo_file *dwo_file;
586
587 /* The "id" that distinguishes this CU/TU.
588 .debug_info calls this "dwo_id", .debug_types calls this "signature".
589 Since signatures came first, we stick with it for consistency. */
590 ULONGEST signature;
591
592 /* The section this CU/TU lives in, in the DWO file. */
593 struct dwarf2_section_info *info_or_types_section;
594
595 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
596 sect_offset offset;
597 unsigned int length;
598
599 /* For types, offset in the type's DIE of the type defined by this TU. */
600 cu_offset type_offset_in_tu;
601 };
602
603 /* Data for one DWO file. */
604
605 struct dwo_file
606 {
607 /* The DW_AT_GNU_dwo_name attribute.
608 We don't manage space for this, it's an attribute. */
609 const char *dwo_name;
610
611 /* The bfd, when the file is open. Otherwise this is NULL. */
612 bfd *dwo_bfd;
613
614 /* Section info for this file. */
615 struct dwo_sections sections;
616
617 /* Table of CUs in the file.
618 Each element is a struct dwo_unit. */
619 htab_t cus;
620
621 /* Table of TUs in the file.
622 Each element is a struct dwo_unit. */
623 htab_t tus;
624 };
625
626 /* Struct used to pass misc. parameters to read_die_and_children, et
627 al. which are used for both .debug_info and .debug_types dies.
628 All parameters here are unchanging for the life of the call. This
629 struct exists to abstract away the constant parameters of die reading. */
630
631 struct die_reader_specs
632 {
633 /* die_section->asection->owner. */
634 bfd* abfd;
635
636 /* The CU of the DIE we are parsing. */
637 struct dwarf2_cu *cu;
638
639 /* Non-NULL if reading a DWO file. */
640 struct dwo_file *dwo_file;
641
642 /* The section the die comes from.
643 This is either .debug_info or .debug_types, or the .dwo variants. */
644 struct dwarf2_section_info *die_section;
645
646 /* die_section->buffer. */
647 gdb_byte *buffer;
648
649 /* The end of the buffer. */
650 const gdb_byte *buffer_end;
651 };
652
653 /* Type of function passed to init_cutu_and_read_dies, et.al. */
654 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
655 gdb_byte *info_ptr,
656 struct die_info *comp_unit_die,
657 int has_children,
658 void *data);
659
660 /* The line number information for a compilation unit (found in the
661 .debug_line section) begins with a "statement program header",
662 which contains the following information. */
663 struct line_header
664 {
665 unsigned int total_length;
666 unsigned short version;
667 unsigned int header_length;
668 unsigned char minimum_instruction_length;
669 unsigned char maximum_ops_per_instruction;
670 unsigned char default_is_stmt;
671 int line_base;
672 unsigned char line_range;
673 unsigned char opcode_base;
674
675 /* standard_opcode_lengths[i] is the number of operands for the
676 standard opcode whose value is i. This means that
677 standard_opcode_lengths[0] is unused, and the last meaningful
678 element is standard_opcode_lengths[opcode_base - 1]. */
679 unsigned char *standard_opcode_lengths;
680
681 /* The include_directories table. NOTE! These strings are not
682 allocated with xmalloc; instead, they are pointers into
683 debug_line_buffer. If you try to free them, `free' will get
684 indigestion. */
685 unsigned int num_include_dirs, include_dirs_size;
686 char **include_dirs;
687
688 /* The file_names table. NOTE! These strings are not allocated
689 with xmalloc; instead, they are pointers into debug_line_buffer.
690 Don't try to free them directly. */
691 unsigned int num_file_names, file_names_size;
692 struct file_entry
693 {
694 char *name;
695 unsigned int dir_index;
696 unsigned int mod_time;
697 unsigned int length;
698 int included_p; /* Non-zero if referenced by the Line Number Program. */
699 struct symtab *symtab; /* The associated symbol table, if any. */
700 } *file_names;
701
702 /* The start and end of the statement program following this
703 header. These point into dwarf2_per_objfile->line_buffer. */
704 gdb_byte *statement_program_start, *statement_program_end;
705 };
706
707 /* When we construct a partial symbol table entry we only
708 need this much information. */
709 struct partial_die_info
710 {
711 /* Offset of this DIE. */
712 sect_offset offset;
713
714 /* DWARF-2 tag for this DIE. */
715 ENUM_BITFIELD(dwarf_tag) tag : 16;
716
717 /* Assorted flags describing the data found in this DIE. */
718 unsigned int has_children : 1;
719 unsigned int is_external : 1;
720 unsigned int is_declaration : 1;
721 unsigned int has_type : 1;
722 unsigned int has_specification : 1;
723 unsigned int has_pc_info : 1;
724 unsigned int may_be_inlined : 1;
725
726 /* Flag set if the SCOPE field of this structure has been
727 computed. */
728 unsigned int scope_set : 1;
729
730 /* Flag set if the DIE has a byte_size attribute. */
731 unsigned int has_byte_size : 1;
732
733 /* Flag set if any of the DIE's children are template arguments. */
734 unsigned int has_template_arguments : 1;
735
736 /* Flag set if fixup_partial_die has been called on this die. */
737 unsigned int fixup_called : 1;
738
739 /* The name of this DIE. Normally the value of DW_AT_name, but
740 sometimes a default name for unnamed DIEs. */
741 char *name;
742
743 /* The linkage name, if present. */
744 const char *linkage_name;
745
746 /* The scope to prepend to our children. This is generally
747 allocated on the comp_unit_obstack, so will disappear
748 when this compilation unit leaves the cache. */
749 char *scope;
750
751 /* Some data associated with the partial DIE. The tag determines
752 which field is live. */
753 union
754 {
755 /* The location description associated with this DIE, if any. */
756 struct dwarf_block *locdesc;
757 /* The offset of an import, for DW_TAG_imported_unit. */
758 sect_offset offset;
759 } d;
760
761 /* If HAS_PC_INFO, the PC range associated with this DIE. */
762 CORE_ADDR lowpc;
763 CORE_ADDR highpc;
764
765 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
766 DW_AT_sibling, if any. */
767 /* NOTE: This member isn't strictly necessary, read_partial_die could
768 return DW_AT_sibling values to its caller load_partial_dies. */
769 gdb_byte *sibling;
770
771 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
772 DW_AT_specification (or DW_AT_abstract_origin or
773 DW_AT_extension). */
774 sect_offset spec_offset;
775
776 /* Pointers to this DIE's parent, first child, and next sibling,
777 if any. */
778 struct partial_die_info *die_parent, *die_child, *die_sibling;
779 };
780
781 /* This data structure holds the information of an abbrev. */
782 struct abbrev_info
783 {
784 unsigned int number; /* number identifying abbrev */
785 enum dwarf_tag tag; /* dwarf tag */
786 unsigned short has_children; /* boolean */
787 unsigned short num_attrs; /* number of attributes */
788 struct attr_abbrev *attrs; /* an array of attribute descriptions */
789 struct abbrev_info *next; /* next in chain */
790 };
791
792 struct attr_abbrev
793 {
794 ENUM_BITFIELD(dwarf_attribute) name : 16;
795 ENUM_BITFIELD(dwarf_form) form : 16;
796 };
797
798 /* Size of abbrev_table.abbrev_hash_table. */
799 #define ABBREV_HASH_SIZE 121
800
801 /* Top level data structure to contain an abbreviation table. */
802
803 struct abbrev_table
804 {
805 /* Where the abbrev table came from. */
806 struct dwarf2_section_info *section;
807 sect_offset offset;
808
809 /* Storage for the abbrev table. */
810 struct obstack abbrev_obstack;
811
812 /* Hash table of abbrevs.
813 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
814 It could be statically allocated, but the previous code didn't so we
815 don't either. */
816 struct abbrev_info **abbrevs;
817 };
818
819 /* Attributes have a name and a value. */
820 struct attribute
821 {
822 ENUM_BITFIELD(dwarf_attribute) name : 16;
823 ENUM_BITFIELD(dwarf_form) form : 15;
824
825 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
826 field should be in u.str (existing only for DW_STRING) but it is kept
827 here for better struct attribute alignment. */
828 unsigned int string_is_canonical : 1;
829
830 union
831 {
832 char *str;
833 struct dwarf_block *blk;
834 ULONGEST unsnd;
835 LONGEST snd;
836 CORE_ADDR addr;
837 struct signatured_type *signatured_type;
838 }
839 u;
840 };
841
842 /* This data structure holds a complete die structure. */
843 struct die_info
844 {
845 /* DWARF-2 tag for this DIE. */
846 ENUM_BITFIELD(dwarf_tag) tag : 16;
847
848 /* Number of attributes */
849 unsigned char num_attrs;
850
851 /* True if we're presently building the full type name for the
852 type derived from this DIE. */
853 unsigned char building_fullname : 1;
854
855 /* Abbrev number */
856 unsigned int abbrev;
857
858 /* Offset in .debug_info or .debug_types section. */
859 sect_offset offset;
860
861 /* The dies in a compilation unit form an n-ary tree. PARENT
862 points to this die's parent; CHILD points to the first child of
863 this node; and all the children of a given node are chained
864 together via their SIBLING fields. */
865 struct die_info *child; /* Its first child, if any. */
866 struct die_info *sibling; /* Its next sibling, if any. */
867 struct die_info *parent; /* Its parent, if any. */
868
869 /* An array of attributes, with NUM_ATTRS elements. There may be
870 zero, but it's not common and zero-sized arrays are not
871 sufficiently portable C. */
872 struct attribute attrs[1];
873 };
874
875 /* Get at parts of an attribute structure. */
876
877 #define DW_STRING(attr) ((attr)->u.str)
878 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
879 #define DW_UNSND(attr) ((attr)->u.unsnd)
880 #define DW_BLOCK(attr) ((attr)->u.blk)
881 #define DW_SND(attr) ((attr)->u.snd)
882 #define DW_ADDR(attr) ((attr)->u.addr)
883 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
884
885 /* Blocks are a bunch of untyped bytes. */
886 struct dwarf_block
887 {
888 unsigned int size;
889
890 /* Valid only if SIZE is not zero. */
891 gdb_byte *data;
892 };
893
894 #ifndef ATTR_ALLOC_CHUNK
895 #define ATTR_ALLOC_CHUNK 4
896 #endif
897
898 /* Allocate fields for structs, unions and enums in this size. */
899 #ifndef DW_FIELD_ALLOC_CHUNK
900 #define DW_FIELD_ALLOC_CHUNK 4
901 #endif
902
903 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
904 but this would require a corresponding change in unpack_field_as_long
905 and friends. */
906 static int bits_per_byte = 8;
907
908 /* The routines that read and process dies for a C struct or C++ class
909 pass lists of data member fields and lists of member function fields
910 in an instance of a field_info structure, as defined below. */
911 struct field_info
912 {
913 /* List of data member and baseclasses fields. */
914 struct nextfield
915 {
916 struct nextfield *next;
917 int accessibility;
918 int virtuality;
919 struct field field;
920 }
921 *fields, *baseclasses;
922
923 /* Number of fields (including baseclasses). */
924 int nfields;
925
926 /* Number of baseclasses. */
927 int nbaseclasses;
928
929 /* Set if the accesibility of one of the fields is not public. */
930 int non_public_fields;
931
932 /* Member function fields array, entries are allocated in the order they
933 are encountered in the object file. */
934 struct nextfnfield
935 {
936 struct nextfnfield *next;
937 struct fn_field fnfield;
938 }
939 *fnfields;
940
941 /* Member function fieldlist array, contains name of possibly overloaded
942 member function, number of overloaded member functions and a pointer
943 to the head of the member function field chain. */
944 struct fnfieldlist
945 {
946 char *name;
947 int length;
948 struct nextfnfield *head;
949 }
950 *fnfieldlists;
951
952 /* Number of entries in the fnfieldlists array. */
953 int nfnfields;
954
955 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
956 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
957 struct typedef_field_list
958 {
959 struct typedef_field field;
960 struct typedef_field_list *next;
961 }
962 *typedef_field_list;
963 unsigned typedef_field_list_count;
964 };
965
966 /* One item on the queue of compilation units to read in full symbols
967 for. */
968 struct dwarf2_queue_item
969 {
970 struct dwarf2_per_cu_data *per_cu;
971 enum language pretend_language;
972 struct dwarf2_queue_item *next;
973 };
974
975 /* The current queue. */
976 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
977
978 /* Loaded secondary compilation units are kept in memory until they
979 have not been referenced for the processing of this many
980 compilation units. Set this to zero to disable caching. Cache
981 sizes of up to at least twenty will improve startup time for
982 typical inter-CU-reference binaries, at an obvious memory cost. */
983 static int dwarf2_max_cache_age = 5;
984 static void
985 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
986 struct cmd_list_element *c, const char *value)
987 {
988 fprintf_filtered (file, _("The upper bound on the age of cached "
989 "dwarf2 compilation units is %s.\n"),
990 value);
991 }
992
993
994 /* Various complaints about symbol reading that don't abort the process. */
995
996 static void
997 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
998 {
999 complaint (&symfile_complaints,
1000 _("statement list doesn't fit in .debug_line section"));
1001 }
1002
1003 static void
1004 dwarf2_debug_line_missing_file_complaint (void)
1005 {
1006 complaint (&symfile_complaints,
1007 _(".debug_line section has line data without a file"));
1008 }
1009
1010 static void
1011 dwarf2_debug_line_missing_end_sequence_complaint (void)
1012 {
1013 complaint (&symfile_complaints,
1014 _(".debug_line section has line "
1015 "program sequence without an end"));
1016 }
1017
1018 static void
1019 dwarf2_complex_location_expr_complaint (void)
1020 {
1021 complaint (&symfile_complaints, _("location expression too complex"));
1022 }
1023
1024 static void
1025 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1026 int arg3)
1027 {
1028 complaint (&symfile_complaints,
1029 _("const value length mismatch for '%s', got %d, expected %d"),
1030 arg1, arg2, arg3);
1031 }
1032
1033 static void
1034 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1035 {
1036 complaint (&symfile_complaints,
1037 _("debug info runs off end of %s section"
1038 " [in module %s]"),
1039 section->asection->name,
1040 bfd_get_filename (section->asection->owner));
1041 }
1042
1043 static void
1044 dwarf2_macro_malformed_definition_complaint (const char *arg1)
1045 {
1046 complaint (&symfile_complaints,
1047 _("macro debug info contains a "
1048 "malformed macro definition:\n`%s'"),
1049 arg1);
1050 }
1051
1052 static void
1053 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1054 {
1055 complaint (&symfile_complaints,
1056 _("invalid attribute class or form for '%s' in '%s'"),
1057 arg1, arg2);
1058 }
1059
1060 /* local function prototypes */
1061
1062 static void dwarf2_locate_sections (bfd *, asection *, void *);
1063
1064 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
1065 struct objfile *);
1066
1067 static void dwarf2_find_base_address (struct die_info *die,
1068 struct dwarf2_cu *cu);
1069
1070 static void dwarf2_build_psymtabs_hard (struct objfile *);
1071
1072 static void scan_partial_symbols (struct partial_die_info *,
1073 CORE_ADDR *, CORE_ADDR *,
1074 int, struct dwarf2_cu *);
1075
1076 static void add_partial_symbol (struct partial_die_info *,
1077 struct dwarf2_cu *);
1078
1079 static void add_partial_namespace (struct partial_die_info *pdi,
1080 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1081 int need_pc, struct dwarf2_cu *cu);
1082
1083 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1084 CORE_ADDR *highpc, int need_pc,
1085 struct dwarf2_cu *cu);
1086
1087 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1088 struct dwarf2_cu *cu);
1089
1090 static void add_partial_subprogram (struct partial_die_info *pdi,
1091 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1092 int need_pc, struct dwarf2_cu *cu);
1093
1094 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
1095
1096 static void psymtab_to_symtab_1 (struct partial_symtab *);
1097
1098 static struct abbrev_info *abbrev_table_lookup_abbrev
1099 (const struct abbrev_table *, unsigned int);
1100
1101 static struct abbrev_table *abbrev_table_read_table
1102 (struct dwarf2_section_info *, sect_offset);
1103
1104 static void abbrev_table_free (struct abbrev_table *);
1105
1106 static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1107 struct dwarf2_section_info *);
1108
1109 static void dwarf2_free_abbrev_table (void *);
1110
1111 static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1112
1113 static struct partial_die_info *load_partial_dies
1114 (const struct die_reader_specs *, gdb_byte *, int);
1115
1116 static gdb_byte *read_partial_die (const struct die_reader_specs *,
1117 struct partial_die_info *,
1118 struct abbrev_info *,
1119 unsigned int,
1120 gdb_byte *);
1121
1122 static struct partial_die_info *find_partial_die (sect_offset,
1123 struct dwarf2_cu *);
1124
1125 static void fixup_partial_die (struct partial_die_info *,
1126 struct dwarf2_cu *);
1127
1128 static gdb_byte *read_attribute (const struct die_reader_specs *,
1129 struct attribute *, struct attr_abbrev *,
1130 gdb_byte *);
1131
1132 static unsigned int read_1_byte (bfd *, gdb_byte *);
1133
1134 static int read_1_signed_byte (bfd *, gdb_byte *);
1135
1136 static unsigned int read_2_bytes (bfd *, gdb_byte *);
1137
1138 static unsigned int read_4_bytes (bfd *, gdb_byte *);
1139
1140 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
1141
1142 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
1143 unsigned int *);
1144
1145 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1146
1147 static LONGEST read_checked_initial_length_and_offset
1148 (bfd *, gdb_byte *, const struct comp_unit_head *,
1149 unsigned int *, unsigned int *);
1150
1151 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
1152 unsigned int *);
1153
1154 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
1155
1156 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
1157
1158 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
1159
1160 static char *read_indirect_string (bfd *, gdb_byte *,
1161 const struct comp_unit_head *,
1162 unsigned int *);
1163
1164 static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
1165
1166 static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
1167
1168 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1169 unsigned int *);
1170
1171 static char *read_str_index (const struct die_reader_specs *reader,
1172 struct dwarf2_cu *cu, ULONGEST str_index);
1173
1174 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1175
1176 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1177 struct dwarf2_cu *);
1178
1179 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1180 unsigned int,
1181 struct dwarf2_cu *);
1182
1183 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1184 struct dwarf2_cu *cu);
1185
1186 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1187
1188 static struct die_info *die_specification (struct die_info *die,
1189 struct dwarf2_cu **);
1190
1191 static void free_line_header (struct line_header *lh);
1192
1193 static void add_file_name (struct line_header *, char *, unsigned int,
1194 unsigned int, unsigned int);
1195
1196 static struct line_header *dwarf_decode_line_header (unsigned int offset,
1197 struct dwarf2_cu *cu);
1198
1199 static void dwarf_decode_lines (struct line_header *, const char *,
1200 struct dwarf2_cu *, struct partial_symtab *,
1201 int);
1202
1203 static void dwarf2_start_subfile (char *, const char *, const char *);
1204
1205 static struct symbol *new_symbol (struct die_info *, struct type *,
1206 struct dwarf2_cu *);
1207
1208 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1209 struct dwarf2_cu *, struct symbol *);
1210
1211 static void dwarf2_const_value (struct attribute *, struct symbol *,
1212 struct dwarf2_cu *);
1213
1214 static void dwarf2_const_value_attr (struct attribute *attr,
1215 struct type *type,
1216 const char *name,
1217 struct obstack *obstack,
1218 struct dwarf2_cu *cu, LONGEST *value,
1219 gdb_byte **bytes,
1220 struct dwarf2_locexpr_baton **baton);
1221
1222 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1223
1224 static int need_gnat_info (struct dwarf2_cu *);
1225
1226 static struct type *die_descriptive_type (struct die_info *,
1227 struct dwarf2_cu *);
1228
1229 static void set_descriptive_type (struct type *, struct die_info *,
1230 struct dwarf2_cu *);
1231
1232 static struct type *die_containing_type (struct die_info *,
1233 struct dwarf2_cu *);
1234
1235 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1236 struct dwarf2_cu *);
1237
1238 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1239
1240 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1241
1242 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1243
1244 static char *typename_concat (struct obstack *obs, const char *prefix,
1245 const char *suffix, int physname,
1246 struct dwarf2_cu *cu);
1247
1248 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1249
1250 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1251
1252 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1253
1254 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1255
1256 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1257
1258 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1259 struct dwarf2_cu *, struct partial_symtab *);
1260
1261 static int dwarf2_get_pc_bounds (struct die_info *,
1262 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1263 struct partial_symtab *);
1264
1265 static void get_scope_pc_bounds (struct die_info *,
1266 CORE_ADDR *, CORE_ADDR *,
1267 struct dwarf2_cu *);
1268
1269 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1270 CORE_ADDR, struct dwarf2_cu *);
1271
1272 static void dwarf2_add_field (struct field_info *, struct die_info *,
1273 struct dwarf2_cu *);
1274
1275 static void dwarf2_attach_fields_to_type (struct field_info *,
1276 struct type *, struct dwarf2_cu *);
1277
1278 static void dwarf2_add_member_fn (struct field_info *,
1279 struct die_info *, struct type *,
1280 struct dwarf2_cu *);
1281
1282 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1283 struct type *,
1284 struct dwarf2_cu *);
1285
1286 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1287
1288 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1289
1290 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1291
1292 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1293
1294 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1295
1296 static struct type *read_module_type (struct die_info *die,
1297 struct dwarf2_cu *cu);
1298
1299 static const char *namespace_name (struct die_info *die,
1300 int *is_anonymous, struct dwarf2_cu *);
1301
1302 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1303
1304 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1305
1306 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1307 struct dwarf2_cu *);
1308
1309 static struct die_info *read_die_and_children (const struct die_reader_specs *,
1310 gdb_byte *info_ptr,
1311 gdb_byte **new_info_ptr,
1312 struct die_info *parent);
1313
1314 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1315 gdb_byte *info_ptr,
1316 gdb_byte **new_info_ptr,
1317 struct die_info *parent);
1318
1319 static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1320 struct die_info **, gdb_byte *, int *, int);
1321
1322 static gdb_byte *read_full_die (const struct die_reader_specs *,
1323 struct die_info **, gdb_byte *, int *);
1324
1325 static void process_die (struct die_info *, struct dwarf2_cu *);
1326
1327 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1328 struct obstack *);
1329
1330 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1331
1332 static const char *dwarf2_full_name (char *name,
1333 struct die_info *die,
1334 struct dwarf2_cu *cu);
1335
1336 static struct die_info *dwarf2_extension (struct die_info *die,
1337 struct dwarf2_cu **);
1338
1339 static const char *dwarf_tag_name (unsigned int);
1340
1341 static const char *dwarf_attr_name (unsigned int);
1342
1343 static const char *dwarf_form_name (unsigned int);
1344
1345 static char *dwarf_bool_name (unsigned int);
1346
1347 static const char *dwarf_type_encoding_name (unsigned int);
1348
1349 static struct die_info *sibling_die (struct die_info *);
1350
1351 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1352
1353 static void dump_die_for_error (struct die_info *);
1354
1355 static void dump_die_1 (struct ui_file *, int level, int max_level,
1356 struct die_info *);
1357
1358 /*static*/ void dump_die (struct die_info *, int max_level);
1359
1360 static void store_in_ref_table (struct die_info *,
1361 struct dwarf2_cu *);
1362
1363 static int is_ref_attr (struct attribute *);
1364
1365 static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
1366
1367 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1368
1369 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1370 struct attribute *,
1371 struct dwarf2_cu **);
1372
1373 static struct die_info *follow_die_ref (struct die_info *,
1374 struct attribute *,
1375 struct dwarf2_cu **);
1376
1377 static struct die_info *follow_die_sig (struct die_info *,
1378 struct attribute *,
1379 struct dwarf2_cu **);
1380
1381 static struct signatured_type *lookup_signatured_type_at_offset
1382 (struct objfile *objfile,
1383 struct dwarf2_section_info *section, sect_offset offset);
1384
1385 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1386
1387 static void read_signatured_type (struct signatured_type *);
1388
1389 /* memory allocation interface */
1390
1391 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1392
1393 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1394
1395 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
1396 char *, int);
1397
1398 static int attr_form_is_block (struct attribute *);
1399
1400 static int attr_form_is_section_offset (struct attribute *);
1401
1402 static int attr_form_is_constant (struct attribute *);
1403
1404 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1405 struct dwarf2_loclist_baton *baton,
1406 struct attribute *attr);
1407
1408 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1409 struct symbol *sym,
1410 struct dwarf2_cu *cu);
1411
1412 static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1413 gdb_byte *info_ptr,
1414 struct abbrev_info *abbrev);
1415
1416 static void free_stack_comp_unit (void *);
1417
1418 static hashval_t partial_die_hash (const void *item);
1419
1420 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1421
1422 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1423 (sect_offset offset, struct objfile *objfile);
1424
1425 static void init_one_comp_unit (struct dwarf2_cu *cu,
1426 struct dwarf2_per_cu_data *per_cu);
1427
1428 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1429 struct die_info *comp_unit_die,
1430 enum language pretend_language);
1431
1432 static void free_heap_comp_unit (void *);
1433
1434 static void free_cached_comp_units (void *);
1435
1436 static void age_cached_comp_units (void);
1437
1438 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1439
1440 static struct type *set_die_type (struct die_info *, struct type *,
1441 struct dwarf2_cu *);
1442
1443 static void create_all_comp_units (struct objfile *);
1444
1445 static int create_all_type_units (struct objfile *);
1446
1447 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1448 enum language);
1449
1450 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1451 enum language);
1452
1453 static void dwarf2_add_dependence (struct dwarf2_cu *,
1454 struct dwarf2_per_cu_data *);
1455
1456 static void dwarf2_mark (struct dwarf2_cu *);
1457
1458 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1459
1460 static struct type *get_die_type_at_offset (sect_offset,
1461 struct dwarf2_per_cu_data *per_cu);
1462
1463 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1464
1465 static void dwarf2_release_queue (void *dummy);
1466
1467 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1468 enum language pretend_language);
1469
1470 static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1471 struct dwarf2_per_cu_data *per_cu,
1472 enum language pretend_language);
1473
1474 static void process_queue (void);
1475
1476 static void find_file_and_directory (struct die_info *die,
1477 struct dwarf2_cu *cu,
1478 char **name, char **comp_dir);
1479
1480 static char *file_full_name (int file, struct line_header *lh,
1481 const char *comp_dir);
1482
1483 static void init_cutu_and_read_dies
1484 (struct dwarf2_per_cu_data *this_cu, int use_existing_cu, int keep,
1485 die_reader_func_ftype *die_reader_func, void *data);
1486
1487 static void init_cutu_and_read_dies_simple
1488 (struct dwarf2_per_cu_data *this_cu,
1489 die_reader_func_ftype *die_reader_func, void *data);
1490
1491 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1492
1493 static void process_psymtab_comp_unit (struct dwarf2_per_cu_data *, int);
1494
1495 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1496
1497 static struct dwo_unit *lookup_dwo_comp_unit
1498 (struct dwarf2_per_cu_data *, char *, const char *, ULONGEST);
1499
1500 static struct dwo_unit *lookup_dwo_type_unit
1501 (struct signatured_type *, char *, const char *);
1502
1503 static void free_dwo_file_cleanup (void *);
1504
1505 static void munmap_section_buffer (struct dwarf2_section_info *);
1506
1507 static void process_cu_includes (void);
1508
1509 #if WORDS_BIGENDIAN
1510
1511 /* Convert VALUE between big- and little-endian. */
1512 static offset_type
1513 byte_swap (offset_type value)
1514 {
1515 offset_type result;
1516
1517 result = (value & 0xff) << 24;
1518 result |= (value & 0xff00) << 8;
1519 result |= (value & 0xff0000) >> 8;
1520 result |= (value & 0xff000000) >> 24;
1521 return result;
1522 }
1523
1524 #define MAYBE_SWAP(V) byte_swap (V)
1525
1526 #else
1527 #define MAYBE_SWAP(V) (V)
1528 #endif /* WORDS_BIGENDIAN */
1529
1530 /* The suffix for an index file. */
1531 #define INDEX_SUFFIX ".gdb-index"
1532
1533 static const char *dwarf2_physname (char *name, struct die_info *die,
1534 struct dwarf2_cu *cu);
1535
1536 /* Try to locate the sections we need for DWARF 2 debugging
1537 information and return true if we have enough to do something.
1538 NAMES points to the dwarf2 section names, or is NULL if the standard
1539 ELF names are used. */
1540
1541 int
1542 dwarf2_has_info (struct objfile *objfile,
1543 const struct dwarf2_debug_sections *names)
1544 {
1545 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1546 if (!dwarf2_per_objfile)
1547 {
1548 /* Initialize per-objfile state. */
1549 struct dwarf2_per_objfile *data
1550 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1551
1552 memset (data, 0, sizeof (*data));
1553 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1554 dwarf2_per_objfile = data;
1555
1556 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1557 (void *) names);
1558 dwarf2_per_objfile->objfile = objfile;
1559 }
1560 return (dwarf2_per_objfile->info.asection != NULL
1561 && dwarf2_per_objfile->abbrev.asection != NULL);
1562 }
1563
1564 /* When loading sections, we look either for uncompressed section or for
1565 compressed section names. */
1566
1567 static int
1568 section_is_p (const char *section_name,
1569 const struct dwarf2_section_names *names)
1570 {
1571 if (names->normal != NULL
1572 && strcmp (section_name, names->normal) == 0)
1573 return 1;
1574 if (names->compressed != NULL
1575 && strcmp (section_name, names->compressed) == 0)
1576 return 1;
1577 return 0;
1578 }
1579
1580 /* This function is mapped across the sections and remembers the
1581 offset and size of each of the debugging sections we are interested
1582 in. */
1583
1584 static void
1585 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1586 {
1587 const struct dwarf2_debug_sections *names;
1588
1589 if (vnames == NULL)
1590 names = &dwarf2_elf_names;
1591 else
1592 names = (const struct dwarf2_debug_sections *) vnames;
1593
1594 if (section_is_p (sectp->name, &names->info))
1595 {
1596 dwarf2_per_objfile->info.asection = sectp;
1597 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1598 }
1599 else if (section_is_p (sectp->name, &names->abbrev))
1600 {
1601 dwarf2_per_objfile->abbrev.asection = sectp;
1602 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1603 }
1604 else if (section_is_p (sectp->name, &names->line))
1605 {
1606 dwarf2_per_objfile->line.asection = sectp;
1607 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1608 }
1609 else if (section_is_p (sectp->name, &names->loc))
1610 {
1611 dwarf2_per_objfile->loc.asection = sectp;
1612 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1613 }
1614 else if (section_is_p (sectp->name, &names->macinfo))
1615 {
1616 dwarf2_per_objfile->macinfo.asection = sectp;
1617 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1618 }
1619 else if (section_is_p (sectp->name, &names->macro))
1620 {
1621 dwarf2_per_objfile->macro.asection = sectp;
1622 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1623 }
1624 else if (section_is_p (sectp->name, &names->str))
1625 {
1626 dwarf2_per_objfile->str.asection = sectp;
1627 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1628 }
1629 else if (section_is_p (sectp->name, &names->addr))
1630 {
1631 dwarf2_per_objfile->addr.asection = sectp;
1632 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1633 }
1634 else if (section_is_p (sectp->name, &names->frame))
1635 {
1636 dwarf2_per_objfile->frame.asection = sectp;
1637 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1638 }
1639 else if (section_is_p (sectp->name, &names->eh_frame))
1640 {
1641 flagword aflag = bfd_get_section_flags (abfd, sectp);
1642
1643 if (aflag & SEC_HAS_CONTENTS)
1644 {
1645 dwarf2_per_objfile->eh_frame.asection = sectp;
1646 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1647 }
1648 }
1649 else if (section_is_p (sectp->name, &names->ranges))
1650 {
1651 dwarf2_per_objfile->ranges.asection = sectp;
1652 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1653 }
1654 else if (section_is_p (sectp->name, &names->types))
1655 {
1656 struct dwarf2_section_info type_section;
1657
1658 memset (&type_section, 0, sizeof (type_section));
1659 type_section.asection = sectp;
1660 type_section.size = bfd_get_section_size (sectp);
1661
1662 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1663 &type_section);
1664 }
1665 else if (section_is_p (sectp->name, &names->gdb_index))
1666 {
1667 dwarf2_per_objfile->gdb_index.asection = sectp;
1668 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1669 }
1670
1671 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1672 && bfd_section_vma (abfd, sectp) == 0)
1673 dwarf2_per_objfile->has_section_at_zero = 1;
1674 }
1675
1676 /* Decompress a section that was compressed using zlib. Store the
1677 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
1678
1679 static void
1680 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1681 gdb_byte **outbuf, bfd_size_type *outsize)
1682 {
1683 bfd *abfd = sectp->owner;
1684 #ifndef HAVE_ZLIB_H
1685 error (_("Support for zlib-compressed DWARF data (from '%s') "
1686 "is disabled in this copy of GDB"),
1687 bfd_get_filename (abfd));
1688 #else
1689 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1690 gdb_byte *compressed_buffer = xmalloc (compressed_size);
1691 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1692 bfd_size_type uncompressed_size;
1693 gdb_byte *uncompressed_buffer;
1694 z_stream strm;
1695 int rc;
1696 int header_size = 12;
1697
1698 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1699 || bfd_bread (compressed_buffer,
1700 compressed_size, abfd) != compressed_size)
1701 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1702 bfd_get_filename (abfd));
1703
1704 /* Read the zlib header. In this case, it should be "ZLIB" followed
1705 by the uncompressed section size, 8 bytes in big-endian order. */
1706 if (compressed_size < header_size
1707 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1708 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1709 bfd_get_filename (abfd));
1710 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1711 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1712 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1713 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1714 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1715 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1716 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1717 uncompressed_size += compressed_buffer[11];
1718
1719 /* It is possible the section consists of several compressed
1720 buffers concatenated together, so we uncompress in a loop. */
1721 strm.zalloc = NULL;
1722 strm.zfree = NULL;
1723 strm.opaque = NULL;
1724 strm.avail_in = compressed_size - header_size;
1725 strm.next_in = (Bytef*) compressed_buffer + header_size;
1726 strm.avail_out = uncompressed_size;
1727 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1728 uncompressed_size);
1729 rc = inflateInit (&strm);
1730 while (strm.avail_in > 0)
1731 {
1732 if (rc != Z_OK)
1733 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1734 bfd_get_filename (abfd), rc);
1735 strm.next_out = ((Bytef*) uncompressed_buffer
1736 + (uncompressed_size - strm.avail_out));
1737 rc = inflate (&strm, Z_FINISH);
1738 if (rc != Z_STREAM_END)
1739 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1740 bfd_get_filename (abfd), rc);
1741 rc = inflateReset (&strm);
1742 }
1743 rc = inflateEnd (&strm);
1744 if (rc != Z_OK
1745 || strm.avail_out != 0)
1746 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1747 bfd_get_filename (abfd), rc);
1748
1749 do_cleanups (cleanup);
1750 *outbuf = uncompressed_buffer;
1751 *outsize = uncompressed_size;
1752 #endif
1753 }
1754
1755 /* A helper function that decides whether a section is empty,
1756 or not present. */
1757
1758 static int
1759 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1760 {
1761 return info->asection == NULL || info->size == 0;
1762 }
1763
1764 /* Read the contents of the section INFO.
1765 OBJFILE is the main object file, but not necessarily the file where
1766 the section comes from. E.g., for DWO files INFO->asection->owner
1767 is the bfd of the DWO file.
1768 If the section is compressed, uncompress it before returning. */
1769
1770 static void
1771 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1772 {
1773 asection *sectp = info->asection;
1774 bfd *abfd;
1775 gdb_byte *buf, *retbuf;
1776 unsigned char header[4];
1777
1778 if (info->readin)
1779 return;
1780 info->buffer = NULL;
1781 info->map_addr = NULL;
1782 info->readin = 1;
1783
1784 if (dwarf2_section_empty_p (info))
1785 return;
1786
1787 /* Note that ABFD may not be from OBJFILE, e.g. a DWO section. */
1788 abfd = sectp->owner;
1789
1790 /* Check if the file has a 4-byte header indicating compression. */
1791 if (info->size > sizeof (header)
1792 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1793 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1794 {
1795 /* Upon decompression, update the buffer and its size. */
1796 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1797 {
1798 zlib_decompress_section (objfile, sectp, &info->buffer,
1799 &info->size);
1800 return;
1801 }
1802 }
1803
1804 #ifdef HAVE_MMAP
1805 if (pagesize == 0)
1806 pagesize = getpagesize ();
1807
1808 /* Only try to mmap sections which are large enough: we don't want to
1809 waste space due to fragmentation. Also, only try mmap for sections
1810 without relocations. */
1811
1812 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1813 {
1814 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1815 MAP_PRIVATE, sectp->filepos,
1816 &info->map_addr, &info->map_len);
1817
1818 if ((caddr_t)info->buffer != MAP_FAILED)
1819 {
1820 #if HAVE_POSIX_MADVISE
1821 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
1822 #endif
1823 return;
1824 }
1825 }
1826 #endif
1827
1828 /* If we get here, we are a normal, not-compressed section. */
1829 info->buffer = buf
1830 = obstack_alloc (&objfile->objfile_obstack, info->size);
1831
1832 /* When debugging .o files, we may need to apply relocations; see
1833 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1834 We never compress sections in .o files, so we only need to
1835 try this when the section is not compressed. */
1836 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1837 if (retbuf != NULL)
1838 {
1839 info->buffer = retbuf;
1840 return;
1841 }
1842
1843 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1844 || bfd_bread (buf, info->size, abfd) != info->size)
1845 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1846 bfd_get_filename (abfd));
1847 }
1848
1849 /* A helper function that returns the size of a section in a safe way.
1850 If you are positive that the section has been read before using the
1851 size, then it is safe to refer to the dwarf2_section_info object's
1852 "size" field directly. In other cases, you must call this
1853 function, because for compressed sections the size field is not set
1854 correctly until the section has been read. */
1855
1856 static bfd_size_type
1857 dwarf2_section_size (struct objfile *objfile,
1858 struct dwarf2_section_info *info)
1859 {
1860 if (!info->readin)
1861 dwarf2_read_section (objfile, info);
1862 return info->size;
1863 }
1864
1865 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1866 SECTION_NAME. */
1867
1868 void
1869 dwarf2_get_section_info (struct objfile *objfile,
1870 enum dwarf2_section_enum sect,
1871 asection **sectp, gdb_byte **bufp,
1872 bfd_size_type *sizep)
1873 {
1874 struct dwarf2_per_objfile *data
1875 = objfile_data (objfile, dwarf2_objfile_data_key);
1876 struct dwarf2_section_info *info;
1877
1878 /* We may see an objfile without any DWARF, in which case we just
1879 return nothing. */
1880 if (data == NULL)
1881 {
1882 *sectp = NULL;
1883 *bufp = NULL;
1884 *sizep = 0;
1885 return;
1886 }
1887 switch (sect)
1888 {
1889 case DWARF2_DEBUG_FRAME:
1890 info = &data->frame;
1891 break;
1892 case DWARF2_EH_FRAME:
1893 info = &data->eh_frame;
1894 break;
1895 default:
1896 gdb_assert_not_reached ("unexpected section");
1897 }
1898
1899 dwarf2_read_section (objfile, info);
1900
1901 *sectp = info->asection;
1902 *bufp = info->buffer;
1903 *sizep = info->size;
1904 }
1905
1906 \f
1907 /* DWARF quick_symbols_functions support. */
1908
1909 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1910 unique line tables, so we maintain a separate table of all .debug_line
1911 derived entries to support the sharing.
1912 All the quick functions need is the list of file names. We discard the
1913 line_header when we're done and don't need to record it here. */
1914 struct quick_file_names
1915 {
1916 /* The offset in .debug_line of the line table. We hash on this. */
1917 unsigned int offset;
1918
1919 /* The number of entries in file_names, real_names. */
1920 unsigned int num_file_names;
1921
1922 /* The file names from the line table, after being run through
1923 file_full_name. */
1924 const char **file_names;
1925
1926 /* The file names from the line table after being run through
1927 gdb_realpath. These are computed lazily. */
1928 const char **real_names;
1929 };
1930
1931 /* When using the index (and thus not using psymtabs), each CU has an
1932 object of this type. This is used to hold information needed by
1933 the various "quick" methods. */
1934 struct dwarf2_per_cu_quick_data
1935 {
1936 /* The file table. This can be NULL if there was no file table
1937 or it's currently not read in.
1938 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1939 struct quick_file_names *file_names;
1940
1941 /* The corresponding symbol table. This is NULL if symbols for this
1942 CU have not yet been read. */
1943 struct symtab *symtab;
1944
1945 /* A temporary mark bit used when iterating over all CUs in
1946 expand_symtabs_matching. */
1947 unsigned int mark : 1;
1948
1949 /* True if we've tried to read the file table and found there isn't one.
1950 There will be no point in trying to read it again next time. */
1951 unsigned int no_file_data : 1;
1952 };
1953
1954 /* Hash function for a quick_file_names. */
1955
1956 static hashval_t
1957 hash_file_name_entry (const void *e)
1958 {
1959 const struct quick_file_names *file_data = e;
1960
1961 return file_data->offset;
1962 }
1963
1964 /* Equality function for a quick_file_names. */
1965
1966 static int
1967 eq_file_name_entry (const void *a, const void *b)
1968 {
1969 const struct quick_file_names *ea = a;
1970 const struct quick_file_names *eb = b;
1971
1972 return ea->offset == eb->offset;
1973 }
1974
1975 /* Delete function for a quick_file_names. */
1976
1977 static void
1978 delete_file_name_entry (void *e)
1979 {
1980 struct quick_file_names *file_data = e;
1981 int i;
1982
1983 for (i = 0; i < file_data->num_file_names; ++i)
1984 {
1985 xfree ((void*) file_data->file_names[i]);
1986 if (file_data->real_names)
1987 xfree ((void*) file_data->real_names[i]);
1988 }
1989
1990 /* The space for the struct itself lives on objfile_obstack,
1991 so we don't free it here. */
1992 }
1993
1994 /* Create a quick_file_names hash table. */
1995
1996 static htab_t
1997 create_quick_file_names_table (unsigned int nr_initial_entries)
1998 {
1999 return htab_create_alloc (nr_initial_entries,
2000 hash_file_name_entry, eq_file_name_entry,
2001 delete_file_name_entry, xcalloc, xfree);
2002 }
2003
2004 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2005 have to be created afterwards. You should call age_cached_comp_units after
2006 processing PER_CU->CU. dw2_setup must have been already called. */
2007
2008 static void
2009 load_cu (struct dwarf2_per_cu_data *per_cu)
2010 {
2011 if (per_cu->is_debug_types)
2012 load_full_type_unit (per_cu);
2013 else
2014 load_full_comp_unit (per_cu, language_minimal);
2015
2016 gdb_assert (per_cu->cu != NULL);
2017
2018 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2019 }
2020
2021 /* Read in the symbols for PER_CU. */
2022
2023 static void
2024 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2025 {
2026 struct cleanup *back_to;
2027
2028 back_to = make_cleanup (dwarf2_release_queue, NULL);
2029
2030 if (dwarf2_per_objfile->using_index
2031 ? per_cu->v.quick->symtab == NULL
2032 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2033 {
2034 queue_comp_unit (per_cu, language_minimal);
2035 load_cu (per_cu);
2036 }
2037
2038 process_queue ();
2039
2040 /* Age the cache, releasing compilation units that have not
2041 been used recently. */
2042 age_cached_comp_units ();
2043
2044 do_cleanups (back_to);
2045 }
2046
2047 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2048 the objfile from which this CU came. Returns the resulting symbol
2049 table. */
2050
2051 static struct symtab *
2052 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2053 {
2054 gdb_assert (dwarf2_per_objfile->using_index);
2055 if (!per_cu->v.quick->symtab)
2056 {
2057 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2058 increment_reading_symtab ();
2059 dw2_do_instantiate_symtab (per_cu);
2060 process_cu_includes ();
2061 do_cleanups (back_to);
2062 }
2063 return per_cu->v.quick->symtab;
2064 }
2065
2066 /* Return the CU given its index. */
2067
2068 static struct dwarf2_per_cu_data *
2069 dw2_get_cu (int index)
2070 {
2071 if (index >= dwarf2_per_objfile->n_comp_units)
2072 {
2073 index -= dwarf2_per_objfile->n_comp_units;
2074 return dwarf2_per_objfile->all_type_units[index];
2075 }
2076 return dwarf2_per_objfile->all_comp_units[index];
2077 }
2078
2079 /* A helper function that knows how to read a 64-bit value in a way
2080 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
2081 otherwise. */
2082
2083 static int
2084 extract_cu_value (const char *bytes, ULONGEST *result)
2085 {
2086 if (sizeof (ULONGEST) < 8)
2087 {
2088 int i;
2089
2090 /* Ignore the upper 4 bytes if they are all zero. */
2091 for (i = 0; i < 4; ++i)
2092 if (bytes[i + 4] != 0)
2093 return 0;
2094
2095 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
2096 }
2097 else
2098 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2099 return 1;
2100 }
2101
2102 /* Read the CU list from the mapped index, and use it to create all
2103 the CU objects for this objfile. Return 0 if something went wrong,
2104 1 if everything went ok. */
2105
2106 static int
2107 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
2108 offset_type cu_list_elements)
2109 {
2110 offset_type i;
2111
2112 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
2113 dwarf2_per_objfile->all_comp_units
2114 = obstack_alloc (&objfile->objfile_obstack,
2115 dwarf2_per_objfile->n_comp_units
2116 * sizeof (struct dwarf2_per_cu_data *));
2117
2118 for (i = 0; i < cu_list_elements; i += 2)
2119 {
2120 struct dwarf2_per_cu_data *the_cu;
2121 ULONGEST offset, length;
2122
2123 if (!extract_cu_value (cu_list, &offset)
2124 || !extract_cu_value (cu_list + 8, &length))
2125 return 0;
2126 cu_list += 2 * 8;
2127
2128 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2129 struct dwarf2_per_cu_data);
2130 the_cu->offset.sect_off = offset;
2131 the_cu->length = length;
2132 the_cu->objfile = objfile;
2133 the_cu->info_or_types_section = &dwarf2_per_objfile->info;
2134 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2135 struct dwarf2_per_cu_quick_data);
2136 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
2137 }
2138
2139 return 1;
2140 }
2141
2142 /* Create the signatured type hash table from the index. */
2143
2144 static int
2145 create_signatured_type_table_from_index (struct objfile *objfile,
2146 struct dwarf2_section_info *section,
2147 const gdb_byte *bytes,
2148 offset_type elements)
2149 {
2150 offset_type i;
2151 htab_t sig_types_hash;
2152
2153 dwarf2_per_objfile->n_type_units = elements / 3;
2154 dwarf2_per_objfile->all_type_units
2155 = obstack_alloc (&objfile->objfile_obstack,
2156 dwarf2_per_objfile->n_type_units
2157 * sizeof (struct dwarf2_per_cu_data *));
2158
2159 sig_types_hash = allocate_signatured_type_table (objfile);
2160
2161 for (i = 0; i < elements; i += 3)
2162 {
2163 struct signatured_type *sig_type;
2164 ULONGEST offset, type_offset_in_tu, signature;
2165 void **slot;
2166
2167 if (!extract_cu_value (bytes, &offset)
2168 || !extract_cu_value (bytes + 8, &type_offset_in_tu))
2169 return 0;
2170 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2171 bytes += 3 * 8;
2172
2173 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2174 struct signatured_type);
2175 sig_type->signature = signature;
2176 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2177 sig_type->per_cu.is_debug_types = 1;
2178 sig_type->per_cu.info_or_types_section = section;
2179 sig_type->per_cu.offset.sect_off = offset;
2180 sig_type->per_cu.objfile = objfile;
2181 sig_type->per_cu.v.quick
2182 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2183 struct dwarf2_per_cu_quick_data);
2184
2185 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2186 *slot = sig_type;
2187
2188 dwarf2_per_objfile->all_type_units[i / 3] = &sig_type->per_cu;
2189 }
2190
2191 dwarf2_per_objfile->signatured_types = sig_types_hash;
2192
2193 return 1;
2194 }
2195
2196 /* Read the address map data from the mapped index, and use it to
2197 populate the objfile's psymtabs_addrmap. */
2198
2199 static void
2200 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2201 {
2202 const gdb_byte *iter, *end;
2203 struct obstack temp_obstack;
2204 struct addrmap *mutable_map;
2205 struct cleanup *cleanup;
2206 CORE_ADDR baseaddr;
2207
2208 obstack_init (&temp_obstack);
2209 cleanup = make_cleanup_obstack_free (&temp_obstack);
2210 mutable_map = addrmap_create_mutable (&temp_obstack);
2211
2212 iter = index->address_table;
2213 end = iter + index->address_table_size;
2214
2215 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2216
2217 while (iter < end)
2218 {
2219 ULONGEST hi, lo, cu_index;
2220 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2221 iter += 8;
2222 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2223 iter += 8;
2224 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2225 iter += 4;
2226
2227 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2228 dw2_get_cu (cu_index));
2229 }
2230
2231 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2232 &objfile->objfile_obstack);
2233 do_cleanups (cleanup);
2234 }
2235
2236 /* The hash function for strings in the mapped index. This is the same as
2237 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2238 implementation. This is necessary because the hash function is tied to the
2239 format of the mapped index file. The hash values do not have to match with
2240 SYMBOL_HASH_NEXT.
2241
2242 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2243
2244 static hashval_t
2245 mapped_index_string_hash (int index_version, const void *p)
2246 {
2247 const unsigned char *str = (const unsigned char *) p;
2248 hashval_t r = 0;
2249 unsigned char c;
2250
2251 while ((c = *str++) != 0)
2252 {
2253 if (index_version >= 5)
2254 c = tolower (c);
2255 r = r * 67 + c - 113;
2256 }
2257
2258 return r;
2259 }
2260
2261 /* Find a slot in the mapped index INDEX for the object named NAME.
2262 If NAME is found, set *VEC_OUT to point to the CU vector in the
2263 constant pool and return 1. If NAME cannot be found, return 0. */
2264
2265 static int
2266 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2267 offset_type **vec_out)
2268 {
2269 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2270 offset_type hash;
2271 offset_type slot, step;
2272 int (*cmp) (const char *, const char *);
2273
2274 if (current_language->la_language == language_cplus
2275 || current_language->la_language == language_java
2276 || current_language->la_language == language_fortran)
2277 {
2278 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2279 not contain any. */
2280 const char *paren = strchr (name, '(');
2281
2282 if (paren)
2283 {
2284 char *dup;
2285
2286 dup = xmalloc (paren - name + 1);
2287 memcpy (dup, name, paren - name);
2288 dup[paren - name] = 0;
2289
2290 make_cleanup (xfree, dup);
2291 name = dup;
2292 }
2293 }
2294
2295 /* Index version 4 did not support case insensitive searches. But the
2296 indices for case insensitive languages are built in lowercase, therefore
2297 simulate our NAME being searched is also lowercased. */
2298 hash = mapped_index_string_hash ((index->version == 4
2299 && case_sensitivity == case_sensitive_off
2300 ? 5 : index->version),
2301 name);
2302
2303 slot = hash & (index->symbol_table_slots - 1);
2304 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2305 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2306
2307 for (;;)
2308 {
2309 /* Convert a slot number to an offset into the table. */
2310 offset_type i = 2 * slot;
2311 const char *str;
2312 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2313 {
2314 do_cleanups (back_to);
2315 return 0;
2316 }
2317
2318 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2319 if (!cmp (name, str))
2320 {
2321 *vec_out = (offset_type *) (index->constant_pool
2322 + MAYBE_SWAP (index->symbol_table[i + 1]));
2323 do_cleanups (back_to);
2324 return 1;
2325 }
2326
2327 slot = (slot + step) & (index->symbol_table_slots - 1);
2328 }
2329 }
2330
2331 /* Read the index file. If everything went ok, initialize the "quick"
2332 elements of all the CUs and return 1. Otherwise, return 0. */
2333
2334 static int
2335 dwarf2_read_index (struct objfile *objfile)
2336 {
2337 char *addr;
2338 struct mapped_index *map;
2339 offset_type *metadata;
2340 const gdb_byte *cu_list;
2341 const gdb_byte *types_list = NULL;
2342 offset_type version, cu_list_elements;
2343 offset_type types_list_elements = 0;
2344 int i;
2345
2346 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
2347 return 0;
2348
2349 /* Older elfutils strip versions could keep the section in the main
2350 executable while splitting it for the separate debug info file. */
2351 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2352 & SEC_HAS_CONTENTS) == 0)
2353 return 0;
2354
2355 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2356
2357 addr = dwarf2_per_objfile->gdb_index.buffer;
2358 /* Version check. */
2359 version = MAYBE_SWAP (*(offset_type *) addr);
2360 /* Versions earlier than 3 emitted every copy of a psymbol. This
2361 causes the index to behave very poorly for certain requests. Version 3
2362 contained incomplete addrmap. So, it seems better to just ignore such
2363 indices. */
2364 if (version < 4)
2365 {
2366 static int warning_printed = 0;
2367 if (!warning_printed)
2368 {
2369 warning (_("Skipping obsolete .gdb_index section in %s."),
2370 objfile->name);
2371 warning_printed = 1;
2372 }
2373 return 0;
2374 }
2375 /* Index version 4 uses a different hash function than index version
2376 5 and later.
2377
2378 Versions earlier than 6 did not emit psymbols for inlined
2379 functions. Using these files will cause GDB not to be able to
2380 set breakpoints on inlined functions by name, so we ignore these
2381 indices unless the --use-deprecated-index-sections command line
2382 option was supplied. */
2383 if (version < 6 && !use_deprecated_index_sections)
2384 {
2385 static int warning_printed = 0;
2386 if (!warning_printed)
2387 {
2388 warning (_("Skipping deprecated .gdb_index section in %s, pass "
2389 "--use-deprecated-index-sections to use them anyway"),
2390 objfile->name);
2391 warning_printed = 1;
2392 }
2393 return 0;
2394 }
2395 /* Indexes with higher version than the one supported by GDB may be no
2396 longer backward compatible. */
2397 if (version > 7)
2398 return 0;
2399
2400 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
2401 map->version = version;
2402 map->total_size = dwarf2_per_objfile->gdb_index.size;
2403
2404 metadata = (offset_type *) (addr + sizeof (offset_type));
2405
2406 i = 0;
2407 cu_list = addr + MAYBE_SWAP (metadata[i]);
2408 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2409 / 8);
2410 ++i;
2411
2412 types_list = addr + MAYBE_SWAP (metadata[i]);
2413 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2414 - MAYBE_SWAP (metadata[i]))
2415 / 8);
2416 ++i;
2417
2418 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2419 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2420 - MAYBE_SWAP (metadata[i]));
2421 ++i;
2422
2423 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2424 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2425 - MAYBE_SWAP (metadata[i]))
2426 / (2 * sizeof (offset_type)));
2427 ++i;
2428
2429 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2430
2431 /* Don't use the index if it's empty. */
2432 if (map->symbol_table_slots == 0)
2433 return 0;
2434
2435 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2436 return 0;
2437
2438 if (types_list_elements)
2439 {
2440 struct dwarf2_section_info *section;
2441
2442 /* We can only handle a single .debug_types when we have an
2443 index. */
2444 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2445 return 0;
2446
2447 section = VEC_index (dwarf2_section_info_def,
2448 dwarf2_per_objfile->types, 0);
2449
2450 if (!create_signatured_type_table_from_index (objfile, section,
2451 types_list,
2452 types_list_elements))
2453 return 0;
2454 }
2455
2456 create_addrmap_from_index (objfile, map);
2457
2458 dwarf2_per_objfile->index_table = map;
2459 dwarf2_per_objfile->using_index = 1;
2460 dwarf2_per_objfile->quick_file_names_table =
2461 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2462
2463 return 1;
2464 }
2465
2466 /* A helper for the "quick" functions which sets the global
2467 dwarf2_per_objfile according to OBJFILE. */
2468
2469 static void
2470 dw2_setup (struct objfile *objfile)
2471 {
2472 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2473 gdb_assert (dwarf2_per_objfile);
2474 }
2475
2476 /* die_reader_func for dw2_get_file_names. */
2477
2478 static void
2479 dw2_get_file_names_reader (const struct die_reader_specs *reader,
2480 gdb_byte *info_ptr,
2481 struct die_info *comp_unit_die,
2482 int has_children,
2483 void *data)
2484 {
2485 struct dwarf2_cu *cu = reader->cu;
2486 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2487 struct objfile *objfile = dwarf2_per_objfile->objfile;
2488 struct line_header *lh;
2489 struct attribute *attr;
2490 int i;
2491 char *name, *comp_dir;
2492 void **slot;
2493 struct quick_file_names *qfn;
2494 unsigned int line_offset;
2495
2496 /* Our callers never want to match partial units -- instead they
2497 will match the enclosing full CU. */
2498 if (comp_unit_die->tag == DW_TAG_partial_unit)
2499 {
2500 this_cu->v.quick->no_file_data = 1;
2501 return;
2502 }
2503
2504 lh = NULL;
2505 slot = NULL;
2506 line_offset = 0;
2507
2508 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2509 if (attr)
2510 {
2511 struct quick_file_names find_entry;
2512
2513 line_offset = DW_UNSND (attr);
2514
2515 /* We may have already read in this line header (TU line header sharing).
2516 If we have we're done. */
2517 find_entry.offset = line_offset;
2518 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2519 &find_entry, INSERT);
2520 if (*slot != NULL)
2521 {
2522 this_cu->v.quick->file_names = *slot;
2523 return;
2524 }
2525
2526 lh = dwarf_decode_line_header (line_offset, cu);
2527 }
2528 if (lh == NULL)
2529 {
2530 this_cu->v.quick->no_file_data = 1;
2531 return;
2532 }
2533
2534 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2535 qfn->offset = line_offset;
2536 gdb_assert (slot != NULL);
2537 *slot = qfn;
2538
2539 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
2540
2541 qfn->num_file_names = lh->num_file_names;
2542 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2543 lh->num_file_names * sizeof (char *));
2544 for (i = 0; i < lh->num_file_names; ++i)
2545 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2546 qfn->real_names = NULL;
2547
2548 free_line_header (lh);
2549
2550 this_cu->v.quick->file_names = qfn;
2551 }
2552
2553 /* A helper for the "quick" functions which attempts to read the line
2554 table for THIS_CU. */
2555
2556 static struct quick_file_names *
2557 dw2_get_file_names (struct objfile *objfile,
2558 struct dwarf2_per_cu_data *this_cu)
2559 {
2560 if (this_cu->v.quick->file_names != NULL)
2561 return this_cu->v.quick->file_names;
2562 /* If we know there is no line data, no point in looking again. */
2563 if (this_cu->v.quick->no_file_data)
2564 return NULL;
2565
2566 /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2567 in the stub for CUs, there's is no need to lookup the DWO file.
2568 However, that's not the case for TUs where DW_AT_stmt_list lives in the
2569 DWO file. */
2570 if (this_cu->is_debug_types)
2571 init_cutu_and_read_dies (this_cu, 0, 0, dw2_get_file_names_reader, NULL);
2572 else
2573 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
2574
2575 if (this_cu->v.quick->no_file_data)
2576 return NULL;
2577 return this_cu->v.quick->file_names;
2578 }
2579
2580 /* A helper for the "quick" functions which computes and caches the
2581 real path for a given file name from the line table. */
2582
2583 static const char *
2584 dw2_get_real_path (struct objfile *objfile,
2585 struct quick_file_names *qfn, int index)
2586 {
2587 if (qfn->real_names == NULL)
2588 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2589 qfn->num_file_names, sizeof (char *));
2590
2591 if (qfn->real_names[index] == NULL)
2592 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2593
2594 return qfn->real_names[index];
2595 }
2596
2597 static struct symtab *
2598 dw2_find_last_source_symtab (struct objfile *objfile)
2599 {
2600 int index;
2601
2602 dw2_setup (objfile);
2603 index = dwarf2_per_objfile->n_comp_units - 1;
2604 return dw2_instantiate_symtab (dw2_get_cu (index));
2605 }
2606
2607 /* Traversal function for dw2_forget_cached_source_info. */
2608
2609 static int
2610 dw2_free_cached_file_names (void **slot, void *info)
2611 {
2612 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2613
2614 if (file_data->real_names)
2615 {
2616 int i;
2617
2618 for (i = 0; i < file_data->num_file_names; ++i)
2619 {
2620 xfree ((void*) file_data->real_names[i]);
2621 file_data->real_names[i] = NULL;
2622 }
2623 }
2624
2625 return 1;
2626 }
2627
2628 static void
2629 dw2_forget_cached_source_info (struct objfile *objfile)
2630 {
2631 dw2_setup (objfile);
2632
2633 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2634 dw2_free_cached_file_names, NULL);
2635 }
2636
2637 /* Helper function for dw2_map_symtabs_matching_filename that expands
2638 the symtabs and calls the iterator. */
2639
2640 static int
2641 dw2_map_expand_apply (struct objfile *objfile,
2642 struct dwarf2_per_cu_data *per_cu,
2643 const char *name,
2644 const char *full_path, const char *real_path,
2645 int (*callback) (struct symtab *, void *),
2646 void *data)
2647 {
2648 struct symtab *last_made = objfile->symtabs;
2649
2650 /* Don't visit already-expanded CUs. */
2651 if (per_cu->v.quick->symtab)
2652 return 0;
2653
2654 /* This may expand more than one symtab, and we want to iterate over
2655 all of them. */
2656 dw2_instantiate_symtab (per_cu);
2657
2658 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2659 objfile->symtabs, last_made);
2660 }
2661
2662 /* Implementation of the map_symtabs_matching_filename method. */
2663
2664 static int
2665 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
2666 const char *full_path, const char *real_path,
2667 int (*callback) (struct symtab *, void *),
2668 void *data)
2669 {
2670 int i;
2671 const char *name_basename = lbasename (name);
2672 int name_len = strlen (name);
2673 int is_abs = IS_ABSOLUTE_PATH (name);
2674
2675 dw2_setup (objfile);
2676
2677 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2678 + dwarf2_per_objfile->n_type_units); ++i)
2679 {
2680 int j;
2681 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2682 struct quick_file_names *file_data;
2683
2684 /* We only need to look at symtabs not already expanded. */
2685 if (per_cu->v.quick->symtab)
2686 continue;
2687
2688 file_data = dw2_get_file_names (objfile, per_cu);
2689 if (file_data == NULL)
2690 continue;
2691
2692 for (j = 0; j < file_data->num_file_names; ++j)
2693 {
2694 const char *this_name = file_data->file_names[j];
2695
2696 if (FILENAME_CMP (name, this_name) == 0
2697 || (!is_abs && compare_filenames_for_search (this_name,
2698 name, name_len)))
2699 {
2700 if (dw2_map_expand_apply (objfile, per_cu,
2701 name, full_path, real_path,
2702 callback, data))
2703 return 1;
2704 }
2705
2706 /* Before we invoke realpath, which can get expensive when many
2707 files are involved, do a quick comparison of the basenames. */
2708 if (! basenames_may_differ
2709 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
2710 continue;
2711
2712 if (full_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 (full_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 if (real_path != NULL)
2731 {
2732 const char *this_real_name = dw2_get_real_path (objfile,
2733 file_data, j);
2734
2735 if (this_real_name != NULL
2736 && (FILENAME_CMP (real_path, this_real_name) == 0
2737 || (!is_abs
2738 && compare_filenames_for_search (this_real_name,
2739 name, name_len))))
2740 {
2741 if (dw2_map_expand_apply (objfile, per_cu,
2742 name, full_path, real_path,
2743 callback, data))
2744 return 1;
2745 }
2746 }
2747 }
2748 }
2749
2750 return 0;
2751 }
2752
2753 static struct symtab *
2754 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2755 const char *name, domain_enum domain)
2756 {
2757 /* We do all the work in the pre_expand_symtabs_matching hook
2758 instead. */
2759 return NULL;
2760 }
2761
2762 /* A helper function that expands all symtabs that hold an object
2763 named NAME. If WANT_SPECIFIC_BLOCK is non-zero, only look for
2764 symbols in block BLOCK_KIND. */
2765
2766 static void
2767 dw2_do_expand_symtabs_matching (struct objfile *objfile,
2768 int want_specific_block,
2769 enum block_enum block_kind,
2770 const char *name, domain_enum domain)
2771 {
2772 struct mapped_index *index;
2773
2774 dw2_setup (objfile);
2775
2776 index = dwarf2_per_objfile->index_table;
2777
2778 /* index_table is NULL if OBJF_READNOW. */
2779 if (index)
2780 {
2781 offset_type *vec;
2782
2783 if (find_slot_in_mapped_hash (index, name, &vec))
2784 {
2785 offset_type i, len = MAYBE_SWAP (*vec);
2786 for (i = 0; i < len; ++i)
2787 {
2788 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[i + 1]);
2789 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
2790 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2791 int want_static = block_kind != GLOBAL_BLOCK;
2792 /* This value is only valid for index versions >= 7. */
2793 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
2794 gdb_index_symbol_kind symbol_kind =
2795 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
2796
2797 if (want_specific_block
2798 && index->version >= 7
2799 && want_static != is_static)
2800 continue;
2801
2802 /* Only check the symbol's kind if it has one.
2803 Indices prior to version 7 don't record it. */
2804 if (index->version >= 7)
2805 {
2806 switch (domain)
2807 {
2808 case VAR_DOMAIN:
2809 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
2810 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
2811 /* Some types are also in VAR_DOMAIN. */
2812 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
2813 continue;
2814 break;
2815 case STRUCT_DOMAIN:
2816 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
2817 continue;
2818 break;
2819 case LABEL_DOMAIN:
2820 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
2821 continue;
2822 break;
2823 default:
2824 break;
2825 }
2826 }
2827
2828 dw2_instantiate_symtab (per_cu);
2829 }
2830 }
2831 }
2832 }
2833
2834 static void
2835 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2836 enum block_enum block_kind, const char *name,
2837 domain_enum domain)
2838 {
2839 dw2_do_expand_symtabs_matching (objfile, 1, block_kind, name, domain);
2840 }
2841
2842 static void
2843 dw2_print_stats (struct objfile *objfile)
2844 {
2845 int i, count;
2846
2847 dw2_setup (objfile);
2848 count = 0;
2849 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2850 + dwarf2_per_objfile->n_type_units); ++i)
2851 {
2852 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2853
2854 if (!per_cu->v.quick->symtab)
2855 ++count;
2856 }
2857 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2858 }
2859
2860 static void
2861 dw2_dump (struct objfile *objfile)
2862 {
2863 /* Nothing worth printing. */
2864 }
2865
2866 static void
2867 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2868 struct section_offsets *delta)
2869 {
2870 /* There's nothing to relocate here. */
2871 }
2872
2873 static void
2874 dw2_expand_symtabs_for_function (struct objfile *objfile,
2875 const char *func_name)
2876 {
2877 /* Note: It doesn't matter what we pass for block_kind here. */
2878 dw2_do_expand_symtabs_matching (objfile, 0, GLOBAL_BLOCK, func_name,
2879 VAR_DOMAIN);
2880 }
2881
2882 static void
2883 dw2_expand_all_symtabs (struct objfile *objfile)
2884 {
2885 int i;
2886
2887 dw2_setup (objfile);
2888
2889 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2890 + dwarf2_per_objfile->n_type_units); ++i)
2891 {
2892 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2893
2894 dw2_instantiate_symtab (per_cu);
2895 }
2896 }
2897
2898 static void
2899 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2900 const char *filename)
2901 {
2902 int i;
2903
2904 dw2_setup (objfile);
2905
2906 /* We don't need to consider type units here.
2907 This is only called for examining code, e.g. expand_line_sal.
2908 There can be an order of magnitude (or more) more type units
2909 than comp units, and we avoid them if we can. */
2910
2911 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2912 {
2913 int j;
2914 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2915 struct quick_file_names *file_data;
2916
2917 /* We only need to look at symtabs not already expanded. */
2918 if (per_cu->v.quick->symtab)
2919 continue;
2920
2921 file_data = dw2_get_file_names (objfile, per_cu);
2922 if (file_data == NULL)
2923 continue;
2924
2925 for (j = 0; j < file_data->num_file_names; ++j)
2926 {
2927 const char *this_name = file_data->file_names[j];
2928 if (FILENAME_CMP (this_name, filename) == 0)
2929 {
2930 dw2_instantiate_symtab (per_cu);
2931 break;
2932 }
2933 }
2934 }
2935 }
2936
2937 /* A helper function for dw2_find_symbol_file that finds the primary
2938 file name for a given CU. This is a die_reader_func. */
2939
2940 static void
2941 dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
2942 gdb_byte *info_ptr,
2943 struct die_info *comp_unit_die,
2944 int has_children,
2945 void *data)
2946 {
2947 const char **result_ptr = data;
2948 struct dwarf2_cu *cu = reader->cu;
2949 struct attribute *attr;
2950
2951 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
2952 if (attr == NULL)
2953 *result_ptr = NULL;
2954 else
2955 *result_ptr = DW_STRING (attr);
2956 }
2957
2958 static const char *
2959 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2960 {
2961 struct dwarf2_per_cu_data *per_cu;
2962 offset_type *vec;
2963 struct quick_file_names *file_data;
2964 const char *filename;
2965
2966 dw2_setup (objfile);
2967
2968 /* index_table is NULL if OBJF_READNOW. */
2969 if (!dwarf2_per_objfile->index_table)
2970 {
2971 struct symtab *s;
2972
2973 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
2974 {
2975 struct blockvector *bv = BLOCKVECTOR (s);
2976 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2977 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2978
2979 if (sym)
2980 return sym->symtab->filename;
2981 }
2982 return NULL;
2983 }
2984
2985 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2986 name, &vec))
2987 return NULL;
2988
2989 /* Note that this just looks at the very first one named NAME -- but
2990 actually we are looking for a function. find_main_filename
2991 should be rewritten so that it doesn't require a custom hook. It
2992 could just use the ordinary symbol tables. */
2993 /* vec[0] is the length, which must always be >0. */
2994 per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
2995
2996 if (per_cu->v.quick->symtab != NULL)
2997 return per_cu->v.quick->symtab->filename;
2998
2999 init_cutu_and_read_dies (per_cu, 0, 0, dw2_get_primary_filename_reader,
3000 &filename);
3001
3002 return filename;
3003 }
3004
3005 static void
3006 dw2_map_matching_symbols (const char * name, domain_enum namespace,
3007 struct objfile *objfile, int global,
3008 int (*callback) (struct block *,
3009 struct symbol *, void *),
3010 void *data, symbol_compare_ftype *match,
3011 symbol_compare_ftype *ordered_compare)
3012 {
3013 /* Currently unimplemented; used for Ada. The function can be called if the
3014 current language is Ada for a non-Ada objfile using GNU index. As Ada
3015 does not look for non-Ada symbols this function should just return. */
3016 }
3017
3018 static void
3019 dw2_expand_symtabs_matching
3020 (struct objfile *objfile,
3021 int (*file_matcher) (const char *, void *),
3022 int (*name_matcher) (const char *, void *),
3023 enum search_domain kind,
3024 void *data)
3025 {
3026 int i;
3027 offset_type iter;
3028 struct mapped_index *index;
3029
3030 dw2_setup (objfile);
3031
3032 /* index_table is NULL if OBJF_READNOW. */
3033 if (!dwarf2_per_objfile->index_table)
3034 return;
3035 index = dwarf2_per_objfile->index_table;
3036
3037 if (file_matcher != NULL)
3038 {
3039 struct cleanup *cleanup;
3040 htab_t visited_found, visited_not_found;
3041
3042 visited_found = htab_create_alloc (10,
3043 htab_hash_pointer, htab_eq_pointer,
3044 NULL, xcalloc, xfree);
3045 cleanup = make_cleanup_htab_delete (visited_found);
3046 visited_not_found = htab_create_alloc (10,
3047 htab_hash_pointer, htab_eq_pointer,
3048 NULL, xcalloc, xfree);
3049 make_cleanup_htab_delete (visited_not_found);
3050
3051 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3052 + dwarf2_per_objfile->n_type_units); ++i)
3053 {
3054 int j;
3055 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3056 struct quick_file_names *file_data;
3057 void **slot;
3058
3059 per_cu->v.quick->mark = 0;
3060
3061 /* We only need to look at symtabs not already expanded. */
3062 if (per_cu->v.quick->symtab)
3063 continue;
3064
3065 file_data = dw2_get_file_names (objfile, per_cu);
3066 if (file_data == NULL)
3067 continue;
3068
3069 if (htab_find (visited_not_found, file_data) != NULL)
3070 continue;
3071 else if (htab_find (visited_found, file_data) != NULL)
3072 {
3073 per_cu->v.quick->mark = 1;
3074 continue;
3075 }
3076
3077 for (j = 0; j < file_data->num_file_names; ++j)
3078 {
3079 if (file_matcher (file_data->file_names[j], data))
3080 {
3081 per_cu->v.quick->mark = 1;
3082 break;
3083 }
3084 }
3085
3086 slot = htab_find_slot (per_cu->v.quick->mark
3087 ? visited_found
3088 : visited_not_found,
3089 file_data, INSERT);
3090 *slot = file_data;
3091 }
3092
3093 do_cleanups (cleanup);
3094 }
3095
3096 for (iter = 0; iter < index->symbol_table_slots; ++iter)
3097 {
3098 offset_type idx = 2 * iter;
3099 const char *name;
3100 offset_type *vec, vec_len, vec_idx;
3101
3102 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
3103 continue;
3104
3105 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
3106
3107 if (! (*name_matcher) (name, data))
3108 continue;
3109
3110 /* The name was matched, now expand corresponding CUs that were
3111 marked. */
3112 vec = (offset_type *) (index->constant_pool
3113 + MAYBE_SWAP (index->symbol_table[idx + 1]));
3114 vec_len = MAYBE_SWAP (vec[0]);
3115 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3116 {
3117 struct dwarf2_per_cu_data *per_cu;
3118 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3119 gdb_index_symbol_kind symbol_kind =
3120 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3121 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3122
3123 /* Don't crash on bad data. */
3124 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3125 + dwarf2_per_objfile->n_comp_units))
3126 continue;
3127
3128 /* Only check the symbol's kind if it has one.
3129 Indices prior to version 7 don't record it. */
3130 if (index->version >= 7)
3131 {
3132 switch (kind)
3133 {
3134 case VARIABLES_DOMAIN:
3135 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3136 continue;
3137 break;
3138 case FUNCTIONS_DOMAIN:
3139 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3140 continue;
3141 break;
3142 case TYPES_DOMAIN:
3143 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3144 continue;
3145 break;
3146 default:
3147 break;
3148 }
3149 }
3150
3151 per_cu = dw2_get_cu (cu_index);
3152 if (file_matcher == NULL || per_cu->v.quick->mark)
3153 dw2_instantiate_symtab (per_cu);
3154 }
3155 }
3156 }
3157
3158 /* A helper for dw2_find_pc_sect_symtab which finds the most specific
3159 symtab. */
3160
3161 static struct symtab *
3162 recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3163 {
3164 int i;
3165
3166 if (BLOCKVECTOR (symtab) != NULL
3167 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3168 return symtab;
3169
3170 if (symtab->includes == NULL)
3171 return NULL;
3172
3173 for (i = 0; symtab->includes[i]; ++i)
3174 {
3175 struct symtab *s = symtab->includes[i];
3176
3177 s = recursively_find_pc_sect_symtab (s, pc);
3178 if (s != NULL)
3179 return s;
3180 }
3181
3182 return NULL;
3183 }
3184
3185 static struct symtab *
3186 dw2_find_pc_sect_symtab (struct objfile *objfile,
3187 struct minimal_symbol *msymbol,
3188 CORE_ADDR pc,
3189 struct obj_section *section,
3190 int warn_if_readin)
3191 {
3192 struct dwarf2_per_cu_data *data;
3193 struct symtab *result;
3194
3195 dw2_setup (objfile);
3196
3197 if (!objfile->psymtabs_addrmap)
3198 return NULL;
3199
3200 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3201 if (!data)
3202 return NULL;
3203
3204 if (warn_if_readin && data->v.quick->symtab)
3205 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
3206 paddress (get_objfile_arch (objfile), pc));
3207
3208 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3209 gdb_assert (result != NULL);
3210 return result;
3211 }
3212
3213 static void
3214 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
3215 void *data, int need_fullname)
3216 {
3217 int i;
3218 struct cleanup *cleanup;
3219 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3220 NULL, xcalloc, xfree);
3221
3222 cleanup = make_cleanup_htab_delete (visited);
3223 dw2_setup (objfile);
3224
3225 /* We can ignore file names coming from already-expanded CUs. */
3226 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3227 + dwarf2_per_objfile->n_type_units); ++i)
3228 {
3229 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3230
3231 if (per_cu->v.quick->symtab)
3232 {
3233 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3234 INSERT);
3235
3236 *slot = per_cu->v.quick->file_names;
3237 }
3238 }
3239
3240 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3241 + dwarf2_per_objfile->n_type_units); ++i)
3242 {
3243 int j;
3244 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3245 struct quick_file_names *file_data;
3246 void **slot;
3247
3248 /* We only need to look at symtabs not already expanded. */
3249 if (per_cu->v.quick->symtab)
3250 continue;
3251
3252 file_data = dw2_get_file_names (objfile, per_cu);
3253 if (file_data == NULL)
3254 continue;
3255
3256 slot = htab_find_slot (visited, file_data, INSERT);
3257 if (*slot)
3258 {
3259 /* Already visited. */
3260 continue;
3261 }
3262 *slot = file_data;
3263
3264 for (j = 0; j < file_data->num_file_names; ++j)
3265 {
3266 const char *this_real_name;
3267
3268 if (need_fullname)
3269 this_real_name = dw2_get_real_path (objfile, file_data, j);
3270 else
3271 this_real_name = NULL;
3272 (*fun) (file_data->file_names[j], this_real_name, data);
3273 }
3274 }
3275
3276 do_cleanups (cleanup);
3277 }
3278
3279 static int
3280 dw2_has_symbols (struct objfile *objfile)
3281 {
3282 return 1;
3283 }
3284
3285 const struct quick_symbol_functions dwarf2_gdb_index_functions =
3286 {
3287 dw2_has_symbols,
3288 dw2_find_last_source_symtab,
3289 dw2_forget_cached_source_info,
3290 dw2_map_symtabs_matching_filename,
3291 dw2_lookup_symbol,
3292 dw2_pre_expand_symtabs_matching,
3293 dw2_print_stats,
3294 dw2_dump,
3295 dw2_relocate,
3296 dw2_expand_symtabs_for_function,
3297 dw2_expand_all_symtabs,
3298 dw2_expand_symtabs_with_filename,
3299 dw2_find_symbol_file,
3300 dw2_map_matching_symbols,
3301 dw2_expand_symtabs_matching,
3302 dw2_find_pc_sect_symtab,
3303 dw2_map_symbol_filenames
3304 };
3305
3306 /* Initialize for reading DWARF for this objfile. Return 0 if this
3307 file will use psymtabs, or 1 if using the GNU index. */
3308
3309 int
3310 dwarf2_initialize_objfile (struct objfile *objfile)
3311 {
3312 /* If we're about to read full symbols, don't bother with the
3313 indices. In this case we also don't care if some other debug
3314 format is making psymtabs, because they are all about to be
3315 expanded anyway. */
3316 if ((objfile->flags & OBJF_READNOW))
3317 {
3318 int i;
3319
3320 dwarf2_per_objfile->using_index = 1;
3321 create_all_comp_units (objfile);
3322 create_all_type_units (objfile);
3323 dwarf2_per_objfile->quick_file_names_table =
3324 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3325
3326 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3327 + dwarf2_per_objfile->n_type_units); ++i)
3328 {
3329 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3330
3331 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3332 struct dwarf2_per_cu_quick_data);
3333 }
3334
3335 /* Return 1 so that gdb sees the "quick" functions. However,
3336 these functions will be no-ops because we will have expanded
3337 all symtabs. */
3338 return 1;
3339 }
3340
3341 if (dwarf2_read_index (objfile))
3342 return 1;
3343
3344 return 0;
3345 }
3346
3347 \f
3348
3349 /* Build a partial symbol table. */
3350
3351 void
3352 dwarf2_build_psymtabs (struct objfile *objfile)
3353 {
3354 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
3355 {
3356 init_psymbol_list (objfile, 1024);
3357 }
3358
3359 dwarf2_build_psymtabs_hard (objfile);
3360 }
3361
3362 /* Return TRUE if OFFSET is within CU_HEADER. */
3363
3364 static inline int
3365 offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
3366 {
3367 sect_offset bottom = { cu_header->offset.sect_off };
3368 sect_offset top = { (cu_header->offset.sect_off + cu_header->length
3369 + cu_header->initial_length_size) };
3370
3371 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
3372 }
3373
3374 /* Read in the comp unit header information from the debug_info at info_ptr.
3375 NOTE: This leaves members offset, first_die_offset to be filled in
3376 by the caller. */
3377
3378 static gdb_byte *
3379 read_comp_unit_head (struct comp_unit_head *cu_header,
3380 gdb_byte *info_ptr, bfd *abfd)
3381 {
3382 int signed_addr;
3383 unsigned int bytes_read;
3384
3385 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3386 cu_header->initial_length_size = bytes_read;
3387 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
3388 info_ptr += bytes_read;
3389 cu_header->version = read_2_bytes (abfd, info_ptr);
3390 info_ptr += 2;
3391 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3392 &bytes_read);
3393 info_ptr += bytes_read;
3394 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3395 info_ptr += 1;
3396 signed_addr = bfd_get_sign_extend_vma (abfd);
3397 if (signed_addr < 0)
3398 internal_error (__FILE__, __LINE__,
3399 _("read_comp_unit_head: dwarf from non elf file"));
3400 cu_header->signed_addr_p = signed_addr;
3401
3402 return info_ptr;
3403 }
3404
3405 /* Subroutine of read_and_check_comp_unit_head and
3406 read_and_check_type_unit_head to simplify them.
3407 Perform various error checking on the header. */
3408
3409 static void
3410 error_check_comp_unit_head (struct comp_unit_head *header,
3411 struct dwarf2_section_info *section,
3412 struct dwarf2_section_info *abbrev_section)
3413 {
3414 bfd *abfd = section->asection->owner;
3415 const char *filename = bfd_get_filename (abfd);
3416
3417 if (header->version != 2 && header->version != 3 && header->version != 4)
3418 error (_("Dwarf Error: wrong version in compilation unit header "
3419 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3420 filename);
3421
3422 if (header->abbrev_offset.sect_off
3423 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
3424 &dwarf2_per_objfile->abbrev))
3425 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3426 "(offset 0x%lx + 6) [in module %s]"),
3427 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
3428 filename);
3429
3430 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3431 avoid potential 32-bit overflow. */
3432 if (((unsigned long) header->offset.sect_off
3433 + header->length + header->initial_length_size)
3434 > section->size)
3435 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3436 "(offset 0x%lx + 0) [in module %s]"),
3437 (long) header->length, (long) header->offset.sect_off,
3438 filename);
3439 }
3440
3441 /* Read in a CU/TU header and perform some basic error checking.
3442 The contents of the header are stored in HEADER.
3443 The result is a pointer to the start of the first DIE. */
3444
3445 static gdb_byte *
3446 read_and_check_comp_unit_head (struct comp_unit_head *header,
3447 struct dwarf2_section_info *section,
3448 struct dwarf2_section_info *abbrev_section,
3449 gdb_byte *info_ptr,
3450 int is_debug_types_section)
3451 {
3452 gdb_byte *beg_of_comp_unit = info_ptr;
3453 bfd *abfd = section->asection->owner;
3454
3455 header->offset.sect_off = beg_of_comp_unit - section->buffer;
3456
3457 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3458
3459 /* If we're reading a type unit, skip over the signature and
3460 type_offset fields. */
3461 if (is_debug_types_section)
3462 info_ptr += 8 /*signature*/ + header->offset_size;
3463
3464 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
3465
3466 error_check_comp_unit_head (header, section, abbrev_section);
3467
3468 return info_ptr;
3469 }
3470
3471 /* Read in the types comp unit header information from .debug_types entry at
3472 types_ptr. The result is a pointer to one past the end of the header. */
3473
3474 static gdb_byte *
3475 read_and_check_type_unit_head (struct comp_unit_head *header,
3476 struct dwarf2_section_info *section,
3477 struct dwarf2_section_info *abbrev_section,
3478 gdb_byte *info_ptr,
3479 ULONGEST *signature,
3480 cu_offset *type_offset_in_tu)
3481 {
3482 gdb_byte *beg_of_comp_unit = info_ptr;
3483 bfd *abfd = section->asection->owner;
3484
3485 header->offset.sect_off = beg_of_comp_unit - section->buffer;
3486
3487 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3488
3489 /* If we're reading a type unit, skip over the signature and
3490 type_offset fields. */
3491 if (signature != NULL)
3492 *signature = read_8_bytes (abfd, info_ptr);
3493 info_ptr += 8;
3494 if (type_offset_in_tu != NULL)
3495 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
3496 header->offset_size);
3497 info_ptr += header->offset_size;
3498
3499 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
3500
3501 error_check_comp_unit_head (header, section, abbrev_section);
3502
3503 return info_ptr;
3504 }
3505
3506 /* Allocate a new partial symtab for file named NAME and mark this new
3507 partial symtab as being an include of PST. */
3508
3509 static void
3510 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3511 struct objfile *objfile)
3512 {
3513 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3514
3515 subpst->section_offsets = pst->section_offsets;
3516 subpst->textlow = 0;
3517 subpst->texthigh = 0;
3518
3519 subpst->dependencies = (struct partial_symtab **)
3520 obstack_alloc (&objfile->objfile_obstack,
3521 sizeof (struct partial_symtab *));
3522 subpst->dependencies[0] = pst;
3523 subpst->number_of_dependencies = 1;
3524
3525 subpst->globals_offset = 0;
3526 subpst->n_global_syms = 0;
3527 subpst->statics_offset = 0;
3528 subpst->n_static_syms = 0;
3529 subpst->symtab = NULL;
3530 subpst->read_symtab = pst->read_symtab;
3531 subpst->readin = 0;
3532
3533 /* No private part is necessary for include psymtabs. This property
3534 can be used to differentiate between such include psymtabs and
3535 the regular ones. */
3536 subpst->read_symtab_private = NULL;
3537 }
3538
3539 /* Read the Line Number Program data and extract the list of files
3540 included by the source file represented by PST. Build an include
3541 partial symtab for each of these included files. */
3542
3543 static void
3544 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
3545 struct die_info *die,
3546 struct partial_symtab *pst)
3547 {
3548 struct line_header *lh = NULL;
3549 struct attribute *attr;
3550
3551 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3552 if (attr)
3553 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
3554 if (lh == NULL)
3555 return; /* No linetable, so no includes. */
3556
3557 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
3558 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
3559
3560 free_line_header (lh);
3561 }
3562
3563 static hashval_t
3564 hash_signatured_type (const void *item)
3565 {
3566 const struct signatured_type *sig_type = item;
3567
3568 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3569 return sig_type->signature;
3570 }
3571
3572 static int
3573 eq_signatured_type (const void *item_lhs, const void *item_rhs)
3574 {
3575 const struct signatured_type *lhs = item_lhs;
3576 const struct signatured_type *rhs = item_rhs;
3577
3578 return lhs->signature == rhs->signature;
3579 }
3580
3581 /* Allocate a hash table for signatured types. */
3582
3583 static htab_t
3584 allocate_signatured_type_table (struct objfile *objfile)
3585 {
3586 return htab_create_alloc_ex (41,
3587 hash_signatured_type,
3588 eq_signatured_type,
3589 NULL,
3590 &objfile->objfile_obstack,
3591 hashtab_obstack_allocate,
3592 dummy_obstack_deallocate);
3593 }
3594
3595 /* A helper function to add a signatured type CU to a table. */
3596
3597 static int
3598 add_signatured_type_cu_to_table (void **slot, void *datum)
3599 {
3600 struct signatured_type *sigt = *slot;
3601 struct dwarf2_per_cu_data ***datap = datum;
3602
3603 **datap = &sigt->per_cu;
3604 ++*datap;
3605
3606 return 1;
3607 }
3608
3609 /* Create the hash table of all entries in the .debug_types section.
3610 DWO_FILE is a pointer to the DWO file for .debug_types.dwo, NULL otherwise.
3611 The result is a pointer to the hash table or NULL if there are
3612 no types. */
3613
3614 static htab_t
3615 create_debug_types_hash_table (struct dwo_file *dwo_file,
3616 VEC (dwarf2_section_info_def) *types)
3617 {
3618 struct objfile *objfile = dwarf2_per_objfile->objfile;
3619 htab_t types_htab = NULL;
3620 int ix;
3621 struct dwarf2_section_info *section;
3622 struct dwarf2_section_info *abbrev_section;
3623
3624 if (VEC_empty (dwarf2_section_info_def, types))
3625 return NULL;
3626
3627 abbrev_section = (dwo_file != NULL
3628 ? &dwo_file->sections.abbrev
3629 : &dwarf2_per_objfile->abbrev);
3630
3631 for (ix = 0;
3632 VEC_iterate (dwarf2_section_info_def, types, ix, section);
3633 ++ix)
3634 {
3635 bfd *abfd;
3636 gdb_byte *info_ptr, *end_ptr;
3637
3638 dwarf2_read_section (objfile, section);
3639 info_ptr = section->buffer;
3640
3641 if (info_ptr == NULL)
3642 continue;
3643
3644 /* We can't set abfd until now because the section may be empty or
3645 not present, in which case section->asection will be NULL. */
3646 abfd = section->asection->owner;
3647
3648 if (types_htab == NULL)
3649 {
3650 if (dwo_file)
3651 types_htab = allocate_dwo_unit_table (objfile);
3652 else
3653 types_htab = allocate_signatured_type_table (objfile);
3654 }
3655
3656 if (dwarf2_die_debug)
3657 fprintf_unfiltered (gdb_stdlog, "Reading signatured types for %s:\n",
3658 bfd_get_filename (abfd));
3659
3660 /* We don't use init_cutu_and_read_dies_simple, or some such, here
3661 because we don't need to read any dies: the signature is in the
3662 header. */
3663
3664 end_ptr = info_ptr + section->size;
3665 while (info_ptr < end_ptr)
3666 {
3667 sect_offset offset;
3668 cu_offset type_offset_in_tu;
3669 ULONGEST signature;
3670 struct signatured_type *sig_type;
3671 struct dwo_unit *dwo_tu;
3672 void **slot;
3673 gdb_byte *ptr = info_ptr;
3674 struct comp_unit_head header;
3675 unsigned int length;
3676
3677 offset.sect_off = ptr - section->buffer;
3678
3679 /* We need to read the type's signature in order to build the hash
3680 table, but we don't need anything else just yet. */
3681
3682 ptr = read_and_check_type_unit_head (&header, section,
3683 abbrev_section, ptr,
3684 &signature, &type_offset_in_tu);
3685
3686 length = header.initial_length_size + header.length;
3687
3688 /* Skip dummy type units. */
3689 if (ptr >= info_ptr + length
3690 || peek_abbrev_code (abfd, ptr) == 0)
3691 {
3692 info_ptr += header.initial_length_size + header.length;
3693 continue;
3694 }
3695
3696 if (dwo_file)
3697 {
3698 sig_type = NULL;
3699 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3700 struct dwo_unit);
3701 dwo_tu->dwo_file = dwo_file;
3702 dwo_tu->signature = signature;
3703 dwo_tu->type_offset_in_tu = type_offset_in_tu;
3704 dwo_tu->info_or_types_section = section;
3705 dwo_tu->offset = offset;
3706 dwo_tu->length = length;
3707 }
3708 else
3709 {
3710 /* N.B.: type_offset is not usable if this type uses a DWO file.
3711 The real type_offset is in the DWO file. */
3712 dwo_tu = NULL;
3713 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3714 struct signatured_type);
3715 sig_type->signature = signature;
3716 sig_type->type_offset_in_tu = type_offset_in_tu;
3717 sig_type->per_cu.objfile = objfile;
3718 sig_type->per_cu.is_debug_types = 1;
3719 sig_type->per_cu.info_or_types_section = section;
3720 sig_type->per_cu.offset = offset;
3721 sig_type->per_cu.length = length;
3722 }
3723
3724 slot = htab_find_slot (types_htab,
3725 dwo_file ? (void*) dwo_tu : (void *) sig_type,
3726 INSERT);
3727 gdb_assert (slot != NULL);
3728 if (*slot != NULL)
3729 {
3730 sect_offset dup_offset;
3731
3732 if (dwo_file)
3733 {
3734 const struct dwo_unit *dup_tu = *slot;
3735
3736 dup_offset = dup_tu->offset;
3737 }
3738 else
3739 {
3740 const struct signatured_type *dup_tu = *slot;
3741
3742 dup_offset = dup_tu->per_cu.offset;
3743 }
3744
3745 complaint (&symfile_complaints,
3746 _("debug type entry at offset 0x%x is duplicate to the "
3747 "entry at offset 0x%x, signature 0x%s"),
3748 offset.sect_off, dup_offset.sect_off,
3749 phex (signature, sizeof (signature)));
3750 }
3751 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3752
3753 if (dwarf2_die_debug)
3754 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3755 offset.sect_off,
3756 phex (signature, sizeof (signature)));
3757
3758 info_ptr += length;
3759 }
3760 }
3761
3762 return types_htab;
3763 }
3764
3765 /* Create the hash table of all entries in the .debug_types section,
3766 and initialize all_type_units.
3767 The result is zero if there is an error (e.g. missing .debug_types section),
3768 otherwise non-zero. */
3769
3770 static int
3771 create_all_type_units (struct objfile *objfile)
3772 {
3773 htab_t types_htab;
3774 struct dwarf2_per_cu_data **iter;
3775
3776 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
3777 if (types_htab == NULL)
3778 {
3779 dwarf2_per_objfile->signatured_types = NULL;
3780 return 0;
3781 }
3782
3783 dwarf2_per_objfile->signatured_types = types_htab;
3784
3785 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
3786 dwarf2_per_objfile->all_type_units
3787 = obstack_alloc (&objfile->objfile_obstack,
3788 dwarf2_per_objfile->n_type_units
3789 * sizeof (struct dwarf2_per_cu_data *));
3790 iter = &dwarf2_per_objfile->all_type_units[0];
3791 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
3792 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
3793 == dwarf2_per_objfile->n_type_units);
3794
3795 return 1;
3796 }
3797
3798 /* Lookup a signature based type for DW_FORM_ref_sig8.
3799 Returns NULL if signature SIG is not present in the table. */
3800
3801 static struct signatured_type *
3802 lookup_signatured_type (ULONGEST sig)
3803 {
3804 struct signatured_type find_entry, *entry;
3805
3806 if (dwarf2_per_objfile->signatured_types == NULL)
3807 {
3808 complaint (&symfile_complaints,
3809 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
3810 return NULL;
3811 }
3812
3813 find_entry.signature = sig;
3814 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3815 return entry;
3816 }
3817
3818 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3819
3820 static void
3821 init_cu_die_reader (struct die_reader_specs *reader,
3822 struct dwarf2_cu *cu,
3823 struct dwarf2_section_info *section,
3824 struct dwo_file *dwo_file)
3825 {
3826 gdb_assert (section->readin && section->buffer != NULL);
3827 reader->abfd = section->asection->owner;
3828 reader->cu = cu;
3829 reader->dwo_file = dwo_file;
3830 reader->die_section = section;
3831 reader->buffer = section->buffer;
3832 reader->buffer_end = section->buffer + section->size;
3833 }
3834
3835 /* Find the base address of the compilation unit for range lists and
3836 location lists. It will normally be specified by DW_AT_low_pc.
3837 In DWARF-3 draft 4, the base address could be overridden by
3838 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3839 compilation units with discontinuous ranges. */
3840
3841 static void
3842 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3843 {
3844 struct attribute *attr;
3845
3846 cu->base_known = 0;
3847 cu->base_address = 0;
3848
3849 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3850 if (attr)
3851 {
3852 cu->base_address = DW_ADDR (attr);
3853 cu->base_known = 1;
3854 }
3855 else
3856 {
3857 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3858 if (attr)
3859 {
3860 cu->base_address = DW_ADDR (attr);
3861 cu->base_known = 1;
3862 }
3863 }
3864 }
3865
3866 /* Initialize a CU (or TU) and read its DIEs.
3867 If the CU defers to a DWO file, read the DWO file as well.
3868
3869 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
3870 Otherwise, a new CU is allocated with xmalloc.
3871
3872 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
3873 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
3874
3875 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
3876 linker) then DIE_READER_FUNC will not get called. */
3877
3878 static void
3879 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
3880 int use_existing_cu, int keep,
3881 die_reader_func_ftype *die_reader_func,
3882 void *data)
3883 {
3884 struct objfile *objfile = dwarf2_per_objfile->objfile;
3885 struct dwarf2_section_info *section = this_cu->info_or_types_section;
3886 bfd *abfd = section->asection->owner;
3887 struct dwarf2_cu *cu;
3888 gdb_byte *begin_info_ptr, *info_ptr;
3889 struct die_reader_specs reader;
3890 struct die_info *comp_unit_die;
3891 int has_children;
3892 struct attribute *attr;
3893 struct cleanup *cleanups, *free_cu_cleanup = NULL;
3894 struct signatured_type *sig_type = NULL;
3895 struct dwarf2_section_info *abbrev_section;
3896
3897 if (use_existing_cu)
3898 gdb_assert (keep);
3899
3900 cleanups = make_cleanup (null_cleanup, NULL);
3901
3902 /* This is cheap if the section is already read in. */
3903 dwarf2_read_section (objfile, section);
3904
3905 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
3906 abbrev_section = &dwarf2_per_objfile->abbrev;
3907
3908 if (use_existing_cu && this_cu->cu != NULL)
3909 {
3910 cu = this_cu->cu;
3911 info_ptr += cu->header.first_die_offset.cu_off;
3912 }
3913 else
3914 {
3915 /* If !use_existing_cu, this_cu->cu must be NULL. */
3916 gdb_assert (this_cu->cu == NULL);
3917
3918 cu = xmalloc (sizeof (*cu));
3919 init_one_comp_unit (cu, this_cu);
3920
3921 /* If an error occurs while loading, release our storage. */
3922 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
3923
3924 if (this_cu->is_debug_types)
3925 {
3926 ULONGEST signature;
3927
3928 info_ptr = read_and_check_type_unit_head (&cu->header, section,
3929 abbrev_section, info_ptr,
3930 &signature, NULL);
3931
3932 /* There's no way to get from PER_CU to its containing
3933 struct signatured_type.
3934 But we have the signature so we can use that. */
3935 sig_type = lookup_signatured_type (signature);
3936 /* We've already scanned all the signatured types,
3937 this must succeed. */
3938 gdb_assert (sig_type != NULL);
3939 gdb_assert (&sig_type->per_cu == this_cu);
3940 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
3941
3942 /* LENGTH has not been set yet for type units. */
3943 this_cu->length = cu->header.length + cu->header.initial_length_size;
3944
3945 /* Establish the type offset that can be used to lookup the type. */
3946 sig_type->type_offset_in_section.sect_off =
3947 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
3948 }
3949 else
3950 {
3951 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
3952 abbrev_section,
3953 info_ptr, 0);
3954
3955 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
3956 gdb_assert (this_cu->length
3957 == cu->header.length + cu->header.initial_length_size);
3958 }
3959 }
3960
3961 /* Skip dummy compilation units. */
3962 if (info_ptr >= begin_info_ptr + this_cu->length
3963 || peek_abbrev_code (abfd, info_ptr) == 0)
3964 {
3965 do_cleanups (cleanups);
3966 return;
3967 }
3968
3969 /* If we don't have them yet, read the abbrevs for this compilation unit.
3970 And if we need to read them now, make sure they're freed when we're
3971 done. */
3972 if (cu->abbrev_table == NULL)
3973 {
3974 dwarf2_read_abbrevs (cu, abbrev_section);
3975 make_cleanup (dwarf2_free_abbrev_table, cu);
3976 }
3977
3978 /* Read the top level CU/TU die. */
3979 init_cu_die_reader (&reader, cu, section, NULL);
3980 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
3981
3982 /* If we have a DWO stub, process it and then read in the DWO file.
3983 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
3984 a DWO CU, that this test will fail. */
3985 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
3986 if (attr)
3987 {
3988 char *dwo_name = DW_STRING (attr);
3989 const char *comp_dir;
3990 struct dwo_unit *dwo_unit;
3991 ULONGEST signature; /* Or dwo_id. */
3992 struct attribute *stmt_list, *low_pc, *high_pc, *ranges;
3993 int i,num_extra_attrs;
3994 struct dwarf2_section_info *dwo_abbrev_section;
3995
3996 if (has_children)
3997 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
3998 " has children (offset 0x%x) [in module %s]"),
3999 this_cu->offset.sect_off, bfd_get_filename (abfd));
4000
4001 /* These attributes aren't processed until later:
4002 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4003 However, the attribute is found in the stub which we won't have later.
4004 In order to not impose this complication on the rest of the code,
4005 we read them here and copy them to the DWO CU/TU die. */
4006 stmt_list = low_pc = high_pc = ranges = NULL;
4007
4008 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4009 DWO file. */
4010 if (! this_cu->is_debug_types)
4011 stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
4012 low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
4013 high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
4014 ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
4015
4016 /* There should be a DW_AT_addr_base attribute here (if needed).
4017 We need the value before we can process DW_FORM_GNU_addr_index. */
4018 cu->addr_base = 0;
4019 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
4020 if (attr)
4021 cu->addr_base = DW_UNSND (attr);
4022
4023 /* There should be a DW_AT_ranges_base attribute here (if needed).
4024 We need the value before we can process DW_AT_ranges. */
4025 cu->ranges_base = 0;
4026 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_ranges_base, cu);
4027 if (attr)
4028 cu->ranges_base = DW_UNSND (attr);
4029
4030 if (this_cu->is_debug_types)
4031 {
4032 gdb_assert (sig_type != NULL);
4033 signature = sig_type->signature;
4034 }
4035 else
4036 {
4037 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4038 if (! attr)
4039 error (_("Dwarf Error: missing dwo_id [in module %s]"),
4040 dwo_name);
4041 signature = DW_UNSND (attr);
4042 }
4043
4044 /* We may need the comp_dir in order to find the DWO file. */
4045 comp_dir = NULL;
4046 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4047 if (attr)
4048 comp_dir = DW_STRING (attr);
4049
4050 if (this_cu->is_debug_types)
4051 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
4052 else
4053 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
4054 signature);
4055
4056 if (dwo_unit == NULL)
4057 {
4058 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
4059 " with ID %s [in module %s]"),
4060 this_cu->offset.sect_off,
4061 phex (signature, sizeof (signature)),
4062 objfile->name);
4063 }
4064
4065 /* Set up for reading the DWO CU/TU. */
4066 cu->dwo_unit = dwo_unit;
4067 section = dwo_unit->info_or_types_section;
4068 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4069 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
4070 init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
4071
4072 if (this_cu->is_debug_types)
4073 {
4074 ULONGEST signature;
4075
4076 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4077 dwo_abbrev_section,
4078 info_ptr,
4079 &signature, NULL);
4080 gdb_assert (sig_type->signature == signature);
4081 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4082 gdb_assert (dwo_unit->length
4083 == cu->header.length + cu->header.initial_length_size);
4084
4085 /* Establish the type offset that can be used to lookup the type.
4086 For DWO files, we don't know it until now. */
4087 sig_type->type_offset_in_section.sect_off =
4088 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4089 }
4090 else
4091 {
4092 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4093 dwo_abbrev_section,
4094 info_ptr, 0);
4095 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4096 gdb_assert (dwo_unit->length
4097 == cu->header.length + cu->header.initial_length_size);
4098 }
4099
4100 /* Discard the original CU's abbrev table, and read the DWO's. */
4101 dwarf2_free_abbrev_table (cu);
4102 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4103
4104 /* Read in the die, but leave space to copy over the attributes
4105 from the stub. This has the benefit of simplifying the rest of
4106 the code - all the real work is done here. */
4107 num_extra_attrs = ((stmt_list != NULL)
4108 + (low_pc != NULL)
4109 + (high_pc != NULL)
4110 + (ranges != NULL));
4111 info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
4112 &has_children, num_extra_attrs);
4113
4114 /* Copy over the attributes from the stub to the DWO die. */
4115 i = comp_unit_die->num_attrs;
4116 if (stmt_list != NULL)
4117 comp_unit_die->attrs[i++] = *stmt_list;
4118 if (low_pc != NULL)
4119 comp_unit_die->attrs[i++] = *low_pc;
4120 if (high_pc != NULL)
4121 comp_unit_die->attrs[i++] = *high_pc;
4122 if (ranges != NULL)
4123 comp_unit_die->attrs[i++] = *ranges;
4124 comp_unit_die->num_attrs += num_extra_attrs;
4125
4126 /* Skip dummy compilation units. */
4127 if (info_ptr >= begin_info_ptr + dwo_unit->length
4128 || peek_abbrev_code (abfd, info_ptr) == 0)
4129 {
4130 do_cleanups (cleanups);
4131 return;
4132 }
4133 }
4134
4135 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4136
4137 if (free_cu_cleanup != NULL)
4138 {
4139 if (keep)
4140 {
4141 /* We've successfully allocated this compilation unit. Let our
4142 caller clean it up when finished with it. */
4143 discard_cleanups (free_cu_cleanup);
4144
4145 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4146 So we have to manually free the abbrev table. */
4147 dwarf2_free_abbrev_table (cu);
4148
4149 /* Link this CU into read_in_chain. */
4150 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4151 dwarf2_per_objfile->read_in_chain = this_cu;
4152 }
4153 else
4154 do_cleanups (free_cu_cleanup);
4155 }
4156
4157 do_cleanups (cleanups);
4158 }
4159
4160 /* Read CU/TU THIS_CU in section SECTION,
4161 but do not follow DW_AT_GNU_dwo_name if present.
4162 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed to
4163 have already done the lookup to find the DWO file).
4164
4165 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
4166 THIS_CU->is_debug_types, but nothing else.
4167
4168 We fill in THIS_CU->length.
4169
4170 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4171 linker) then DIE_READER_FUNC will not get called.
4172
4173 THIS_CU->cu is always freed when done.
4174 This is done in order to not leave THIS_CU->cu in a state where we have
4175 to care whether it refers to the "main" CU or the DWO CU. */
4176
4177 static void
4178 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4179 struct dwarf2_section_info *abbrev_section,
4180 struct dwo_file *dwo_file,
4181 die_reader_func_ftype *die_reader_func,
4182 void *data)
4183 {
4184 struct objfile *objfile = dwarf2_per_objfile->objfile;
4185 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4186 bfd *abfd = section->asection->owner;
4187 struct dwarf2_cu cu;
4188 gdb_byte *begin_info_ptr, *info_ptr;
4189 struct die_reader_specs reader;
4190 struct cleanup *cleanups;
4191 struct die_info *comp_unit_die;
4192 int has_children;
4193
4194 gdb_assert (this_cu->cu == NULL);
4195
4196 /* This is cheap if the section is already read in. */
4197 dwarf2_read_section (objfile, section);
4198
4199 init_one_comp_unit (&cu, this_cu);
4200
4201 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4202
4203 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4204 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
4205 abbrev_section, info_ptr,
4206 this_cu->is_debug_types);
4207
4208 this_cu->length = cu.header.length + cu.header.initial_length_size;
4209
4210 /* Skip dummy compilation units. */
4211 if (info_ptr >= begin_info_ptr + this_cu->length
4212 || peek_abbrev_code (abfd, info_ptr) == 0)
4213 {
4214 do_cleanups (cleanups);
4215 return;
4216 }
4217
4218 dwarf2_read_abbrevs (&cu, abbrev_section);
4219 make_cleanup (dwarf2_free_abbrev_table, &cu);
4220
4221 init_cu_die_reader (&reader, &cu, section, dwo_file);
4222 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4223
4224 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4225
4226 do_cleanups (cleanups);
4227 }
4228
4229 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4230 does not lookup the specified DWO file.
4231 This cannot be used to read DWO files.
4232
4233 THIS_CU->cu is always freed when done.
4234 This is done in order to not leave THIS_CU->cu in a state where we have
4235 to care whether it refers to the "main" CU or the DWO CU.
4236 We can revisit this if the data shows there's a performance issue. */
4237
4238 static void
4239 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4240 die_reader_func_ftype *die_reader_func,
4241 void *data)
4242 {
4243 init_cutu_and_read_dies_no_follow (this_cu,
4244 &dwarf2_per_objfile->abbrev,
4245 NULL,
4246 die_reader_func, data);
4247 }
4248
4249 /* die_reader_func for process_psymtab_comp_unit. */
4250
4251 static void
4252 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
4253 gdb_byte *info_ptr,
4254 struct die_info *comp_unit_die,
4255 int has_children,
4256 void *data)
4257 {
4258 struct dwarf2_cu *cu = reader->cu;
4259 struct objfile *objfile = cu->objfile;
4260 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
4261 struct attribute *attr;
4262 CORE_ADDR baseaddr;
4263 CORE_ADDR best_lowpc = 0, best_highpc = 0;
4264 struct partial_symtab *pst;
4265 int has_pc_info;
4266 const char *filename;
4267 int *want_partial_unit_ptr = data;
4268
4269 if (comp_unit_die->tag == DW_TAG_partial_unit
4270 && (want_partial_unit_ptr == NULL
4271 || !*want_partial_unit_ptr))
4272 return;
4273
4274 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
4275
4276 cu->list_in_scope = &file_symbols;
4277
4278 /* Allocate a new partial symbol table structure. */
4279 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
4280 if (attr == NULL || !DW_STRING (attr))
4281 filename = "";
4282 else
4283 filename = DW_STRING (attr);
4284 pst = start_psymtab_common (objfile, objfile->section_offsets,
4285 filename,
4286 /* TEXTLOW and TEXTHIGH are set below. */
4287 0,
4288 objfile->global_psymbols.next,
4289 objfile->static_psymbols.next);
4290 pst->psymtabs_addrmap_supported = 1;
4291
4292 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4293 if (attr != NULL)
4294 pst->dirname = DW_STRING (attr);
4295
4296 pst->read_symtab_private = per_cu;
4297
4298 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4299
4300 /* Store the function that reads in the rest of the symbol table. */
4301 pst->read_symtab = dwarf2_psymtab_to_symtab;
4302
4303 per_cu->v.psymtab = pst;
4304
4305 dwarf2_find_base_address (comp_unit_die, cu);
4306
4307 /* Possibly set the default values of LOWPC and HIGHPC from
4308 `DW_AT_ranges'. */
4309 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
4310 &best_highpc, cu, pst);
4311 if (has_pc_info == 1 && best_lowpc < best_highpc)
4312 /* Store the contiguous range if it is not empty; it can be empty for
4313 CUs with no code. */
4314 addrmap_set_empty (objfile->psymtabs_addrmap,
4315 best_lowpc + baseaddr,
4316 best_highpc + baseaddr - 1, pst);
4317
4318 /* Check if comp unit has_children.
4319 If so, read the rest of the partial symbols from this comp unit.
4320 If not, there's no more debug_info for this comp unit. */
4321 if (has_children)
4322 {
4323 struct partial_die_info *first_die;
4324 CORE_ADDR lowpc, highpc;
4325
4326 lowpc = ((CORE_ADDR) -1);
4327 highpc = ((CORE_ADDR) 0);
4328
4329 first_die = load_partial_dies (reader, info_ptr, 1);
4330
4331 scan_partial_symbols (first_die, &lowpc, &highpc,
4332 ! has_pc_info, cu);
4333
4334 /* If we didn't find a lowpc, set it to highpc to avoid
4335 complaints from `maint check'. */
4336 if (lowpc == ((CORE_ADDR) -1))
4337 lowpc = highpc;
4338
4339 /* If the compilation unit didn't have an explicit address range,
4340 then use the information extracted from its child dies. */
4341 if (! has_pc_info)
4342 {
4343 best_lowpc = lowpc;
4344 best_highpc = highpc;
4345 }
4346 }
4347 pst->textlow = best_lowpc + baseaddr;
4348 pst->texthigh = best_highpc + baseaddr;
4349
4350 pst->n_global_syms = objfile->global_psymbols.next -
4351 (objfile->global_psymbols.list + pst->globals_offset);
4352 pst->n_static_syms = objfile->static_psymbols.next -
4353 (objfile->static_psymbols.list + pst->statics_offset);
4354 sort_pst_symbols (pst);
4355
4356 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
4357 {
4358 int i;
4359 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
4360 struct dwarf2_per_cu_data *iter;
4361
4362 /* Fill in 'dependencies' here; we fill in 'users' in a
4363 post-pass. */
4364 pst->number_of_dependencies = len;
4365 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
4366 len * sizeof (struct symtab *));
4367 for (i = 0;
4368 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
4369 i, iter);
4370 ++i)
4371 pst->dependencies[i] = iter->v.psymtab;
4372
4373 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
4374 }
4375
4376 if (per_cu->is_debug_types)
4377 {
4378 /* It's not clear we want to do anything with stmt lists here.
4379 Waiting to see what gcc ultimately does. */
4380 }
4381 else
4382 {
4383 /* Get the list of files included in the current compilation unit,
4384 and build a psymtab for each of them. */
4385 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
4386 }
4387 }
4388
4389 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4390 Process compilation unit THIS_CU for a psymtab. */
4391
4392 static void
4393 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
4394 int want_partial_unit)
4395 {
4396 /* If this compilation unit was already read in, free the
4397 cached copy in order to read it in again. This is
4398 necessary because we skipped some symbols when we first
4399 read in the compilation unit (see load_partial_dies).
4400 This problem could be avoided, but the benefit is unclear. */
4401 if (this_cu->cu != NULL)
4402 free_one_cached_comp_unit (this_cu);
4403
4404 gdb_assert (! this_cu->is_debug_types);
4405 init_cutu_and_read_dies (this_cu, 0, 0, process_psymtab_comp_unit_reader,
4406 &want_partial_unit);
4407
4408 /* Age out any secondary CUs. */
4409 age_cached_comp_units ();
4410 }
4411
4412 /* Traversal function for htab_traverse_noresize.
4413 Process one .debug_types comp-unit. */
4414
4415 static int
4416 process_psymtab_type_unit (void **slot, void *info)
4417 {
4418 struct signatured_type *sig_type = (struct signatured_type *) *slot;
4419 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
4420
4421 gdb_assert (per_cu->is_debug_types);
4422 gdb_assert (info == NULL);
4423
4424 /* If this compilation unit was already read in, free the
4425 cached copy in order to read it in again. This is
4426 necessary because we skipped some symbols when we first
4427 read in the compilation unit (see load_partial_dies).
4428 This problem could be avoided, but the benefit is unclear. */
4429 if (per_cu->cu != NULL)
4430 free_one_cached_comp_unit (per_cu);
4431
4432 init_cutu_and_read_dies (per_cu, 0, 0, process_psymtab_comp_unit_reader,
4433 NULL);
4434
4435 /* Age out any secondary CUs. */
4436 age_cached_comp_units ();
4437
4438 return 1;
4439 }
4440
4441 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4442 Build partial symbol tables for the .debug_types comp-units. */
4443
4444 static void
4445 build_type_psymtabs (struct objfile *objfile)
4446 {
4447 if (! create_all_type_units (objfile))
4448 return;
4449
4450 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
4451 process_psymtab_type_unit, NULL);
4452 }
4453
4454 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
4455
4456 static void
4457 psymtabs_addrmap_cleanup (void *o)
4458 {
4459 struct objfile *objfile = o;
4460
4461 objfile->psymtabs_addrmap = NULL;
4462 }
4463
4464 /* Compute the 'user' field for each psymtab in OBJFILE. */
4465
4466 static void
4467 set_partial_user (struct objfile *objfile)
4468 {
4469 int i;
4470
4471 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4472 {
4473 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4474 struct partial_symtab *pst = per_cu->v.psymtab;
4475 int j;
4476
4477 for (j = 0; j < pst->number_of_dependencies; ++j)
4478 {
4479 /* Set the 'user' field only if it is not already set. */
4480 if (pst->dependencies[j]->user == NULL)
4481 pst->dependencies[j]->user = pst;
4482 }
4483 }
4484 }
4485
4486 /* Build the partial symbol table by doing a quick pass through the
4487 .debug_info and .debug_abbrev sections. */
4488
4489 static void
4490 dwarf2_build_psymtabs_hard (struct objfile *objfile)
4491 {
4492 struct cleanup *back_to, *addrmap_cleanup;
4493 struct obstack temp_obstack;
4494 int i;
4495
4496 if (dwarf2_read_debug)
4497 {
4498 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4499 objfile->name);
4500 }
4501
4502 dwarf2_per_objfile->reading_partial_symbols = 1;
4503
4504 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4505
4506 /* Any cached compilation units will be linked by the per-objfile
4507 read_in_chain. Make sure to free them when we're done. */
4508 back_to = make_cleanup (free_cached_comp_units, NULL);
4509
4510 build_type_psymtabs (objfile);
4511
4512 create_all_comp_units (objfile);
4513
4514 /* Create a temporary address map on a temporary obstack. We later
4515 copy this to the final obstack. */
4516 obstack_init (&temp_obstack);
4517 make_cleanup_obstack_free (&temp_obstack);
4518 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
4519 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
4520
4521 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4522 {
4523 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4524
4525 process_psymtab_comp_unit (per_cu, 0);
4526 }
4527
4528 set_partial_user (objfile);
4529
4530 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
4531 &objfile->objfile_obstack);
4532 discard_cleanups (addrmap_cleanup);
4533
4534 do_cleanups (back_to);
4535
4536 if (dwarf2_read_debug)
4537 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4538 objfile->name);
4539 }
4540
4541 /* die_reader_func for load_partial_comp_unit. */
4542
4543 static void
4544 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
4545 gdb_byte *info_ptr,
4546 struct die_info *comp_unit_die,
4547 int has_children,
4548 void *data)
4549 {
4550 struct dwarf2_cu *cu = reader->cu;
4551
4552 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
4553
4554 /* Check if comp unit has_children.
4555 If so, read the rest of the partial symbols from this comp unit.
4556 If not, there's no more debug_info for this comp unit. */
4557 if (has_children)
4558 load_partial_dies (reader, info_ptr, 0);
4559 }
4560
4561 /* Load the partial DIEs for a secondary CU into memory.
4562 This is also used when rereading a primary CU with load_all_dies. */
4563
4564 static void
4565 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
4566 {
4567 init_cutu_and_read_dies (this_cu, 1, 1, load_partial_comp_unit_reader, NULL);
4568 }
4569
4570 /* Create a list of all compilation units in OBJFILE.
4571 This is only done for -readnow and building partial symtabs. */
4572
4573 static void
4574 create_all_comp_units (struct objfile *objfile)
4575 {
4576 int n_allocated;
4577 int n_comp_units;
4578 struct dwarf2_per_cu_data **all_comp_units;
4579 gdb_byte *info_ptr;
4580
4581 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4582 info_ptr = dwarf2_per_objfile->info.buffer;
4583
4584 n_comp_units = 0;
4585 n_allocated = 10;
4586 all_comp_units = xmalloc (n_allocated
4587 * sizeof (struct dwarf2_per_cu_data *));
4588
4589 while (info_ptr < dwarf2_per_objfile->info.buffer
4590 + dwarf2_per_objfile->info.size)
4591 {
4592 unsigned int length, initial_length_size;
4593 struct dwarf2_per_cu_data *this_cu;
4594 sect_offset offset;
4595
4596 offset.sect_off = info_ptr - dwarf2_per_objfile->info.buffer;
4597
4598 /* Read just enough information to find out where the next
4599 compilation unit is. */
4600 length = read_initial_length (objfile->obfd, info_ptr,
4601 &initial_length_size);
4602
4603 /* Save the compilation unit for later lookup. */
4604 this_cu = obstack_alloc (&objfile->objfile_obstack,
4605 sizeof (struct dwarf2_per_cu_data));
4606 memset (this_cu, 0, sizeof (*this_cu));
4607 this_cu->offset = offset;
4608 this_cu->length = length + initial_length_size;
4609 this_cu->objfile = objfile;
4610 this_cu->info_or_types_section = &dwarf2_per_objfile->info;
4611
4612 if (n_comp_units == n_allocated)
4613 {
4614 n_allocated *= 2;
4615 all_comp_units = xrealloc (all_comp_units,
4616 n_allocated
4617 * sizeof (struct dwarf2_per_cu_data *));
4618 }
4619 all_comp_units[n_comp_units++] = this_cu;
4620
4621 info_ptr = info_ptr + this_cu->length;
4622 }
4623
4624 dwarf2_per_objfile->all_comp_units
4625 = obstack_alloc (&objfile->objfile_obstack,
4626 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
4627 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
4628 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
4629 xfree (all_comp_units);
4630 dwarf2_per_objfile->n_comp_units = n_comp_units;
4631 }
4632
4633 /* Process all loaded DIEs for compilation unit CU, starting at
4634 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
4635 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
4636 DW_AT_ranges). If NEED_PC is set, then this function will set
4637 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
4638 and record the covered ranges in the addrmap. */
4639
4640 static void
4641 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
4642 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4643 {
4644 struct partial_die_info *pdi;
4645
4646 /* Now, march along the PDI's, descending into ones which have
4647 interesting children but skipping the children of the other ones,
4648 until we reach the end of the compilation unit. */
4649
4650 pdi = first_die;
4651
4652 while (pdi != NULL)
4653 {
4654 fixup_partial_die (pdi, cu);
4655
4656 /* Anonymous namespaces or modules have no name but have interesting
4657 children, so we need to look at them. Ditto for anonymous
4658 enums. */
4659
4660 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
4661 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
4662 || pdi->tag == DW_TAG_imported_unit)
4663 {
4664 switch (pdi->tag)
4665 {
4666 case DW_TAG_subprogram:
4667 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4668 break;
4669 case DW_TAG_constant:
4670 case DW_TAG_variable:
4671 case DW_TAG_typedef:
4672 case DW_TAG_union_type:
4673 if (!pdi->is_declaration)
4674 {
4675 add_partial_symbol (pdi, cu);
4676 }
4677 break;
4678 case DW_TAG_class_type:
4679 case DW_TAG_interface_type:
4680 case DW_TAG_structure_type:
4681 if (!pdi->is_declaration)
4682 {
4683 add_partial_symbol (pdi, cu);
4684 }
4685 break;
4686 case DW_TAG_enumeration_type:
4687 if (!pdi->is_declaration)
4688 add_partial_enumeration (pdi, cu);
4689 break;
4690 case DW_TAG_base_type:
4691 case DW_TAG_subrange_type:
4692 /* File scope base type definitions are added to the partial
4693 symbol table. */
4694 add_partial_symbol (pdi, cu);
4695 break;
4696 case DW_TAG_namespace:
4697 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
4698 break;
4699 case DW_TAG_module:
4700 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
4701 break;
4702 case DW_TAG_imported_unit:
4703 {
4704 struct dwarf2_per_cu_data *per_cu;
4705
4706 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
4707 cu->objfile);
4708
4709 /* Go read the partial unit, if needed. */
4710 if (per_cu->v.psymtab == NULL)
4711 process_psymtab_comp_unit (per_cu, 1);
4712
4713 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
4714 per_cu);
4715 }
4716 break;
4717 default:
4718 break;
4719 }
4720 }
4721
4722 /* If the die has a sibling, skip to the sibling. */
4723
4724 pdi = pdi->die_sibling;
4725 }
4726 }
4727
4728 /* Functions used to compute the fully scoped name of a partial DIE.
4729
4730 Normally, this is simple. For C++, the parent DIE's fully scoped
4731 name is concatenated with "::" and the partial DIE's name. For
4732 Java, the same thing occurs except that "." is used instead of "::".
4733 Enumerators are an exception; they use the scope of their parent
4734 enumeration type, i.e. the name of the enumeration type is not
4735 prepended to the enumerator.
4736
4737 There are two complexities. One is DW_AT_specification; in this
4738 case "parent" means the parent of the target of the specification,
4739 instead of the direct parent of the DIE. The other is compilers
4740 which do not emit DW_TAG_namespace; in this case we try to guess
4741 the fully qualified name of structure types from their members'
4742 linkage names. This must be done using the DIE's children rather
4743 than the children of any DW_AT_specification target. We only need
4744 to do this for structures at the top level, i.e. if the target of
4745 any DW_AT_specification (if any; otherwise the DIE itself) does not
4746 have a parent. */
4747
4748 /* Compute the scope prefix associated with PDI's parent, in
4749 compilation unit CU. The result will be allocated on CU's
4750 comp_unit_obstack, or a copy of the already allocated PDI->NAME
4751 field. NULL is returned if no prefix is necessary. */
4752 static char *
4753 partial_die_parent_scope (struct partial_die_info *pdi,
4754 struct dwarf2_cu *cu)
4755 {
4756 char *grandparent_scope;
4757 struct partial_die_info *parent, *real_pdi;
4758
4759 /* We need to look at our parent DIE; if we have a DW_AT_specification,
4760 then this means the parent of the specification DIE. */
4761
4762 real_pdi = pdi;
4763 while (real_pdi->has_specification)
4764 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
4765
4766 parent = real_pdi->die_parent;
4767 if (parent == NULL)
4768 return NULL;
4769
4770 if (parent->scope_set)
4771 return parent->scope;
4772
4773 fixup_partial_die (parent, cu);
4774
4775 grandparent_scope = partial_die_parent_scope (parent, cu);
4776
4777 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
4778 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
4779 Work around this problem here. */
4780 if (cu->language == language_cplus
4781 && parent->tag == DW_TAG_namespace
4782 && strcmp (parent->name, "::") == 0
4783 && grandparent_scope == NULL)
4784 {
4785 parent->scope = NULL;
4786 parent->scope_set = 1;
4787 return NULL;
4788 }
4789
4790 if (pdi->tag == DW_TAG_enumerator)
4791 /* Enumerators should not get the name of the enumeration as a prefix. */
4792 parent->scope = grandparent_scope;
4793 else if (parent->tag == DW_TAG_namespace
4794 || parent->tag == DW_TAG_module
4795 || parent->tag == DW_TAG_structure_type
4796 || parent->tag == DW_TAG_class_type
4797 || parent->tag == DW_TAG_interface_type
4798 || parent->tag == DW_TAG_union_type
4799 || parent->tag == DW_TAG_enumeration_type)
4800 {
4801 if (grandparent_scope == NULL)
4802 parent->scope = parent->name;
4803 else
4804 parent->scope = typename_concat (&cu->comp_unit_obstack,
4805 grandparent_scope,
4806 parent->name, 0, cu);
4807 }
4808 else
4809 {
4810 /* FIXME drow/2004-04-01: What should we be doing with
4811 function-local names? For partial symbols, we should probably be
4812 ignoring them. */
4813 complaint (&symfile_complaints,
4814 _("unhandled containing DIE tag %d for DIE at %d"),
4815 parent->tag, pdi->offset.sect_off);
4816 parent->scope = grandparent_scope;
4817 }
4818
4819 parent->scope_set = 1;
4820 return parent->scope;
4821 }
4822
4823 /* Return the fully scoped name associated with PDI, from compilation unit
4824 CU. The result will be allocated with malloc. */
4825
4826 static char *
4827 partial_die_full_name (struct partial_die_info *pdi,
4828 struct dwarf2_cu *cu)
4829 {
4830 char *parent_scope;
4831
4832 /* If this is a template instantiation, we can not work out the
4833 template arguments from partial DIEs. So, unfortunately, we have
4834 to go through the full DIEs. At least any work we do building
4835 types here will be reused if full symbols are loaded later. */
4836 if (pdi->has_template_arguments)
4837 {
4838 fixup_partial_die (pdi, cu);
4839
4840 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
4841 {
4842 struct die_info *die;
4843 struct attribute attr;
4844 struct dwarf2_cu *ref_cu = cu;
4845
4846 /* DW_FORM_ref_addr is using section offset. */
4847 attr.name = 0;
4848 attr.form = DW_FORM_ref_addr;
4849 attr.u.unsnd = pdi->offset.sect_off;
4850 die = follow_die_ref (NULL, &attr, &ref_cu);
4851
4852 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
4853 }
4854 }
4855
4856 parent_scope = partial_die_parent_scope (pdi, cu);
4857 if (parent_scope == NULL)
4858 return NULL;
4859 else
4860 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
4861 }
4862
4863 static void
4864 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
4865 {
4866 struct objfile *objfile = cu->objfile;
4867 CORE_ADDR addr = 0;
4868 char *actual_name = NULL;
4869 CORE_ADDR baseaddr;
4870 int built_actual_name = 0;
4871
4872 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4873
4874 actual_name = partial_die_full_name (pdi, cu);
4875 if (actual_name)
4876 built_actual_name = 1;
4877
4878 if (actual_name == NULL)
4879 actual_name = pdi->name;
4880
4881 switch (pdi->tag)
4882 {
4883 case DW_TAG_subprogram:
4884 if (pdi->is_external || cu->language == language_ada)
4885 {
4886 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
4887 of the global scope. But in Ada, we want to be able to access
4888 nested procedures globally. So all Ada subprograms are stored
4889 in the global scope. */
4890 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
4891 mst_text, objfile); */
4892 add_psymbol_to_list (actual_name, strlen (actual_name),
4893 built_actual_name,
4894 VAR_DOMAIN, LOC_BLOCK,
4895 &objfile->global_psymbols,
4896 0, pdi->lowpc + baseaddr,
4897 cu->language, objfile);
4898 }
4899 else
4900 {
4901 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
4902 mst_file_text, objfile); */
4903 add_psymbol_to_list (actual_name, strlen (actual_name),
4904 built_actual_name,
4905 VAR_DOMAIN, LOC_BLOCK,
4906 &objfile->static_psymbols,
4907 0, pdi->lowpc + baseaddr,
4908 cu->language, objfile);
4909 }
4910 break;
4911 case DW_TAG_constant:
4912 {
4913 struct psymbol_allocation_list *list;
4914
4915 if (pdi->is_external)
4916 list = &objfile->global_psymbols;
4917 else
4918 list = &objfile->static_psymbols;
4919 add_psymbol_to_list (actual_name, strlen (actual_name),
4920 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4921 list, 0, 0, cu->language, objfile);
4922 }
4923 break;
4924 case DW_TAG_variable:
4925 if (pdi->d.locdesc)
4926 addr = decode_locdesc (pdi->d.locdesc, cu);
4927
4928 if (pdi->d.locdesc
4929 && addr == 0
4930 && !dwarf2_per_objfile->has_section_at_zero)
4931 {
4932 /* A global or static variable may also have been stripped
4933 out by the linker if unused, in which case its address
4934 will be nullified; do not add such variables into partial
4935 symbol table then. */
4936 }
4937 else if (pdi->is_external)
4938 {
4939 /* Global Variable.
4940 Don't enter into the minimal symbol tables as there is
4941 a minimal symbol table entry from the ELF symbols already.
4942 Enter into partial symbol table if it has a location
4943 descriptor or a type.
4944 If the location descriptor is missing, new_symbol will create
4945 a LOC_UNRESOLVED symbol, the address of the variable will then
4946 be determined from the minimal symbol table whenever the variable
4947 is referenced.
4948 The address for the partial symbol table entry is not
4949 used by GDB, but it comes in handy for debugging partial symbol
4950 table building. */
4951
4952 if (pdi->d.locdesc || pdi->has_type)
4953 add_psymbol_to_list (actual_name, strlen (actual_name),
4954 built_actual_name,
4955 VAR_DOMAIN, LOC_STATIC,
4956 &objfile->global_psymbols,
4957 0, addr + baseaddr,
4958 cu->language, objfile);
4959 }
4960 else
4961 {
4962 /* Static Variable. Skip symbols without location descriptors. */
4963 if (pdi->d.locdesc == NULL)
4964 {
4965 if (built_actual_name)
4966 xfree (actual_name);
4967 return;
4968 }
4969 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
4970 mst_file_data, objfile); */
4971 add_psymbol_to_list (actual_name, strlen (actual_name),
4972 built_actual_name,
4973 VAR_DOMAIN, LOC_STATIC,
4974 &objfile->static_psymbols,
4975 0, addr + baseaddr,
4976 cu->language, objfile);
4977 }
4978 break;
4979 case DW_TAG_typedef:
4980 case DW_TAG_base_type:
4981 case DW_TAG_subrange_type:
4982 add_psymbol_to_list (actual_name, strlen (actual_name),
4983 built_actual_name,
4984 VAR_DOMAIN, LOC_TYPEDEF,
4985 &objfile->static_psymbols,
4986 0, (CORE_ADDR) 0, cu->language, objfile);
4987 break;
4988 case DW_TAG_namespace:
4989 add_psymbol_to_list (actual_name, strlen (actual_name),
4990 built_actual_name,
4991 VAR_DOMAIN, LOC_TYPEDEF,
4992 &objfile->global_psymbols,
4993 0, (CORE_ADDR) 0, cu->language, objfile);
4994 break;
4995 case DW_TAG_class_type:
4996 case DW_TAG_interface_type:
4997 case DW_TAG_structure_type:
4998 case DW_TAG_union_type:
4999 case DW_TAG_enumeration_type:
5000 /* Skip external references. The DWARF standard says in the section
5001 about "Structure, Union, and Class Type Entries": "An incomplete
5002 structure, union or class type is represented by a structure,
5003 union or class entry that does not have a byte size attribute
5004 and that has a DW_AT_declaration attribute." */
5005 if (!pdi->has_byte_size && pdi->is_declaration)
5006 {
5007 if (built_actual_name)
5008 xfree (actual_name);
5009 return;
5010 }
5011
5012 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
5013 static vs. global. */
5014 add_psymbol_to_list (actual_name, strlen (actual_name),
5015 built_actual_name,
5016 STRUCT_DOMAIN, LOC_TYPEDEF,
5017 (cu->language == language_cplus
5018 || cu->language == language_java)
5019 ? &objfile->global_psymbols
5020 : &objfile->static_psymbols,
5021 0, (CORE_ADDR) 0, cu->language, objfile);
5022
5023 break;
5024 case DW_TAG_enumerator:
5025 add_psymbol_to_list (actual_name, strlen (actual_name),
5026 built_actual_name,
5027 VAR_DOMAIN, LOC_CONST,
5028 (cu->language == language_cplus
5029 || cu->language == language_java)
5030 ? &objfile->global_psymbols
5031 : &objfile->static_psymbols,
5032 0, (CORE_ADDR) 0, cu->language, objfile);
5033 break;
5034 default:
5035 break;
5036 }
5037
5038 if (built_actual_name)
5039 xfree (actual_name);
5040 }
5041
5042 /* Read a partial die corresponding to a namespace; also, add a symbol
5043 corresponding to that namespace to the symbol table. NAMESPACE is
5044 the name of the enclosing namespace. */
5045
5046 static void
5047 add_partial_namespace (struct partial_die_info *pdi,
5048 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5049 int need_pc, struct dwarf2_cu *cu)
5050 {
5051 /* Add a symbol for the namespace. */
5052
5053 add_partial_symbol (pdi, cu);
5054
5055 /* Now scan partial symbols in that namespace. */
5056
5057 if (pdi->has_children)
5058 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
5059 }
5060
5061 /* Read a partial die corresponding to a Fortran module. */
5062
5063 static void
5064 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
5065 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
5066 {
5067 /* Now scan partial symbols in that module. */
5068
5069 if (pdi->has_children)
5070 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
5071 }
5072
5073 /* Read a partial die corresponding to a subprogram and create a partial
5074 symbol for that subprogram. When the CU language allows it, this
5075 routine also defines a partial symbol for each nested subprogram
5076 that this subprogram contains.
5077
5078 DIE my also be a lexical block, in which case we simply search
5079 recursively for suprograms defined inside that lexical block.
5080 Again, this is only performed when the CU language allows this
5081 type of definitions. */
5082
5083 static void
5084 add_partial_subprogram (struct partial_die_info *pdi,
5085 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5086 int need_pc, struct dwarf2_cu *cu)
5087 {
5088 if (pdi->tag == DW_TAG_subprogram)
5089 {
5090 if (pdi->has_pc_info)
5091 {
5092 if (pdi->lowpc < *lowpc)
5093 *lowpc = pdi->lowpc;
5094 if (pdi->highpc > *highpc)
5095 *highpc = pdi->highpc;
5096 if (need_pc)
5097 {
5098 CORE_ADDR baseaddr;
5099 struct objfile *objfile = cu->objfile;
5100
5101 baseaddr = ANOFFSET (objfile->section_offsets,
5102 SECT_OFF_TEXT (objfile));
5103 addrmap_set_empty (objfile->psymtabs_addrmap,
5104 pdi->lowpc + baseaddr,
5105 pdi->highpc - 1 + baseaddr,
5106 cu->per_cu->v.psymtab);
5107 }
5108 }
5109
5110 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
5111 {
5112 if (!pdi->is_declaration)
5113 /* Ignore subprogram DIEs that do not have a name, they are
5114 illegal. Do not emit a complaint at this point, we will
5115 do so when we convert this psymtab into a symtab. */
5116 if (pdi->name)
5117 add_partial_symbol (pdi, cu);
5118 }
5119 }
5120
5121 if (! pdi->has_children)
5122 return;
5123
5124 if (cu->language == language_ada)
5125 {
5126 pdi = pdi->die_child;
5127 while (pdi != NULL)
5128 {
5129 fixup_partial_die (pdi, cu);
5130 if (pdi->tag == DW_TAG_subprogram
5131 || pdi->tag == DW_TAG_lexical_block)
5132 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
5133 pdi = pdi->die_sibling;
5134 }
5135 }
5136 }
5137
5138 /* Read a partial die corresponding to an enumeration type. */
5139
5140 static void
5141 add_partial_enumeration (struct partial_die_info *enum_pdi,
5142 struct dwarf2_cu *cu)
5143 {
5144 struct partial_die_info *pdi;
5145
5146 if (enum_pdi->name != NULL)
5147 add_partial_symbol (enum_pdi, cu);
5148
5149 pdi = enum_pdi->die_child;
5150 while (pdi)
5151 {
5152 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
5153 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
5154 else
5155 add_partial_symbol (pdi, cu);
5156 pdi = pdi->die_sibling;
5157 }
5158 }
5159
5160 /* Return the initial uleb128 in the die at INFO_PTR. */
5161
5162 static unsigned int
5163 peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
5164 {
5165 unsigned int bytes_read;
5166
5167 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5168 }
5169
5170 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
5171 Return the corresponding abbrev, or NULL if the number is zero (indicating
5172 an empty DIE). In either case *BYTES_READ will be set to the length of
5173 the initial number. */
5174
5175 static struct abbrev_info *
5176 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
5177 struct dwarf2_cu *cu)
5178 {
5179 bfd *abfd = cu->objfile->obfd;
5180 unsigned int abbrev_number;
5181 struct abbrev_info *abbrev;
5182
5183 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
5184
5185 if (abbrev_number == 0)
5186 return NULL;
5187
5188 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
5189 if (!abbrev)
5190 {
5191 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
5192 abbrev_number, bfd_get_filename (abfd));
5193 }
5194
5195 return abbrev;
5196 }
5197
5198 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
5199 Returns a pointer to the end of a series of DIEs, terminated by an empty
5200 DIE. Any children of the skipped DIEs will also be skipped. */
5201
5202 static gdb_byte *
5203 skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
5204 {
5205 struct dwarf2_cu *cu = reader->cu;
5206 struct abbrev_info *abbrev;
5207 unsigned int bytes_read;
5208
5209 while (1)
5210 {
5211 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
5212 if (abbrev == NULL)
5213 return info_ptr + bytes_read;
5214 else
5215 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
5216 }
5217 }
5218
5219 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
5220 INFO_PTR should point just after the initial uleb128 of a DIE, and the
5221 abbrev corresponding to that skipped uleb128 should be passed in
5222 ABBREV. Returns a pointer to this DIE's sibling, skipping any
5223 children. */
5224
5225 static gdb_byte *
5226 skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
5227 struct abbrev_info *abbrev)
5228 {
5229 unsigned int bytes_read;
5230 struct attribute attr;
5231 bfd *abfd = reader->abfd;
5232 struct dwarf2_cu *cu = reader->cu;
5233 gdb_byte *buffer = reader->buffer;
5234 const gdb_byte *buffer_end = reader->buffer_end;
5235 gdb_byte *start_info_ptr = info_ptr;
5236 unsigned int form, i;
5237
5238 for (i = 0; i < abbrev->num_attrs; i++)
5239 {
5240 /* The only abbrev we care about is DW_AT_sibling. */
5241 if (abbrev->attrs[i].name == DW_AT_sibling)
5242 {
5243 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
5244 if (attr.form == DW_FORM_ref_addr)
5245 complaint (&symfile_complaints,
5246 _("ignoring absolute DW_AT_sibling"));
5247 else
5248 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
5249 }
5250
5251 /* If it isn't DW_AT_sibling, skip this attribute. */
5252 form = abbrev->attrs[i].form;
5253 skip_attribute:
5254 switch (form)
5255 {
5256 case DW_FORM_ref_addr:
5257 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
5258 and later it is offset sized. */
5259 if (cu->header.version == 2)
5260 info_ptr += cu->header.addr_size;
5261 else
5262 info_ptr += cu->header.offset_size;
5263 break;
5264 case DW_FORM_addr:
5265 info_ptr += cu->header.addr_size;
5266 break;
5267 case DW_FORM_data1:
5268 case DW_FORM_ref1:
5269 case DW_FORM_flag:
5270 info_ptr += 1;
5271 break;
5272 case DW_FORM_flag_present:
5273 break;
5274 case DW_FORM_data2:
5275 case DW_FORM_ref2:
5276 info_ptr += 2;
5277 break;
5278 case DW_FORM_data4:
5279 case DW_FORM_ref4:
5280 info_ptr += 4;
5281 break;
5282 case DW_FORM_data8:
5283 case DW_FORM_ref8:
5284 case DW_FORM_ref_sig8:
5285 info_ptr += 8;
5286 break;
5287 case DW_FORM_string:
5288 read_direct_string (abfd, info_ptr, &bytes_read);
5289 info_ptr += bytes_read;
5290 break;
5291 case DW_FORM_sec_offset:
5292 case DW_FORM_strp:
5293 info_ptr += cu->header.offset_size;
5294 break;
5295 case DW_FORM_exprloc:
5296 case DW_FORM_block:
5297 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5298 info_ptr += bytes_read;
5299 break;
5300 case DW_FORM_block1:
5301 info_ptr += 1 + read_1_byte (abfd, info_ptr);
5302 break;
5303 case DW_FORM_block2:
5304 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
5305 break;
5306 case DW_FORM_block4:
5307 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
5308 break;
5309 case DW_FORM_sdata:
5310 case DW_FORM_udata:
5311 case DW_FORM_ref_udata:
5312 case DW_FORM_GNU_addr_index:
5313 case DW_FORM_GNU_str_index:
5314 info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
5315 break;
5316 case DW_FORM_indirect:
5317 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5318 info_ptr += bytes_read;
5319 /* We need to continue parsing from here, so just go back to
5320 the top. */
5321 goto skip_attribute;
5322
5323 default:
5324 error (_("Dwarf Error: Cannot handle %s "
5325 "in DWARF reader [in module %s]"),
5326 dwarf_form_name (form),
5327 bfd_get_filename (abfd));
5328 }
5329 }
5330
5331 if (abbrev->has_children)
5332 return skip_children (reader, info_ptr);
5333 else
5334 return info_ptr;
5335 }
5336
5337 /* Locate ORIG_PDI's sibling.
5338 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
5339
5340 static gdb_byte *
5341 locate_pdi_sibling (const struct die_reader_specs *reader,
5342 struct partial_die_info *orig_pdi,
5343 gdb_byte *info_ptr)
5344 {
5345 /* Do we know the sibling already? */
5346
5347 if (orig_pdi->sibling)
5348 return orig_pdi->sibling;
5349
5350 /* Are there any children to deal with? */
5351
5352 if (!orig_pdi->has_children)
5353 return info_ptr;
5354
5355 /* Skip the children the long way. */
5356
5357 return skip_children (reader, info_ptr);
5358 }
5359
5360 /* Expand this partial symbol table into a full symbol table. */
5361
5362 static void
5363 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
5364 {
5365 if (pst != NULL)
5366 {
5367 if (pst->readin)
5368 {
5369 warning (_("bug: psymtab for %s is already read in."),
5370 pst->filename);
5371 }
5372 else
5373 {
5374 if (info_verbose)
5375 {
5376 printf_filtered (_("Reading in symbols for %s..."),
5377 pst->filename);
5378 gdb_flush (gdb_stdout);
5379 }
5380
5381 /* Restore our global data. */
5382 dwarf2_per_objfile = objfile_data (pst->objfile,
5383 dwarf2_objfile_data_key);
5384
5385 /* If this psymtab is constructed from a debug-only objfile, the
5386 has_section_at_zero flag will not necessarily be correct. We
5387 can get the correct value for this flag by looking at the data
5388 associated with the (presumably stripped) associated objfile. */
5389 if (pst->objfile->separate_debug_objfile_backlink)
5390 {
5391 struct dwarf2_per_objfile *dpo_backlink
5392 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
5393 dwarf2_objfile_data_key);
5394
5395 dwarf2_per_objfile->has_section_at_zero
5396 = dpo_backlink->has_section_at_zero;
5397 }
5398
5399 dwarf2_per_objfile->reading_partial_symbols = 0;
5400
5401 psymtab_to_symtab_1 (pst);
5402
5403 /* Finish up the debug error message. */
5404 if (info_verbose)
5405 printf_filtered (_("done.\n"));
5406 }
5407 }
5408
5409 process_cu_includes ();
5410 }
5411 \f
5412 /* Reading in full CUs. */
5413
5414 /* Add PER_CU to the queue. */
5415
5416 static void
5417 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
5418 enum language pretend_language)
5419 {
5420 struct dwarf2_queue_item *item;
5421
5422 per_cu->queued = 1;
5423 item = xmalloc (sizeof (*item));
5424 item->per_cu = per_cu;
5425 item->pretend_language = pretend_language;
5426 item->next = NULL;
5427
5428 if (dwarf2_queue == NULL)
5429 dwarf2_queue = item;
5430 else
5431 dwarf2_queue_tail->next = item;
5432
5433 dwarf2_queue_tail = item;
5434 }
5435
5436 /* Process the queue. */
5437
5438 static void
5439 process_queue (void)
5440 {
5441 struct dwarf2_queue_item *item, *next_item;
5442
5443 if (dwarf2_read_debug)
5444 {
5445 fprintf_unfiltered (gdb_stdlog,
5446 "Expanding one or more symtabs of objfile %s ...\n",
5447 dwarf2_per_objfile->objfile->name);
5448 }
5449
5450 /* The queue starts out with one item, but following a DIE reference
5451 may load a new CU, adding it to the end of the queue. */
5452 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
5453 {
5454 if (dwarf2_per_objfile->using_index
5455 ? !item->per_cu->v.quick->symtab
5456 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
5457 process_full_comp_unit (item->per_cu, item->pretend_language);
5458
5459 item->per_cu->queued = 0;
5460 next_item = item->next;
5461 xfree (item);
5462 }
5463
5464 dwarf2_queue_tail = NULL;
5465
5466 if (dwarf2_read_debug)
5467 {
5468 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
5469 dwarf2_per_objfile->objfile->name);
5470 }
5471 }
5472
5473 /* Free all allocated queue entries. This function only releases anything if
5474 an error was thrown; if the queue was processed then it would have been
5475 freed as we went along. */
5476
5477 static void
5478 dwarf2_release_queue (void *dummy)
5479 {
5480 struct dwarf2_queue_item *item, *last;
5481
5482 item = dwarf2_queue;
5483 while (item)
5484 {
5485 /* Anything still marked queued is likely to be in an
5486 inconsistent state, so discard it. */
5487 if (item->per_cu->queued)
5488 {
5489 if (item->per_cu->cu != NULL)
5490 free_one_cached_comp_unit (item->per_cu);
5491 item->per_cu->queued = 0;
5492 }
5493
5494 last = item;
5495 item = item->next;
5496 xfree (last);
5497 }
5498
5499 dwarf2_queue = dwarf2_queue_tail = NULL;
5500 }
5501
5502 /* Read in full symbols for PST, and anything it depends on. */
5503
5504 static void
5505 psymtab_to_symtab_1 (struct partial_symtab *pst)
5506 {
5507 struct dwarf2_per_cu_data *per_cu;
5508 int i;
5509
5510 if (pst->readin)
5511 return;
5512
5513 for (i = 0; i < pst->number_of_dependencies; i++)
5514 if (!pst->dependencies[i]->readin
5515 && pst->dependencies[i]->user == NULL)
5516 {
5517 /* Inform about additional files that need to be read in. */
5518 if (info_verbose)
5519 {
5520 /* FIXME: i18n: Need to make this a single string. */
5521 fputs_filtered (" ", gdb_stdout);
5522 wrap_here ("");
5523 fputs_filtered ("and ", gdb_stdout);
5524 wrap_here ("");
5525 printf_filtered ("%s...", pst->dependencies[i]->filename);
5526 wrap_here (""); /* Flush output. */
5527 gdb_flush (gdb_stdout);
5528 }
5529 psymtab_to_symtab_1 (pst->dependencies[i]);
5530 }
5531
5532 per_cu = pst->read_symtab_private;
5533
5534 if (per_cu == NULL)
5535 {
5536 /* It's an include file, no symbols to read for it.
5537 Everything is in the parent symtab. */
5538 pst->readin = 1;
5539 return;
5540 }
5541
5542 dw2_do_instantiate_symtab (per_cu);
5543 }
5544
5545 /* Trivial hash function for die_info: the hash value of a DIE
5546 is its offset in .debug_info for this objfile. */
5547
5548 static hashval_t
5549 die_hash (const void *item)
5550 {
5551 const struct die_info *die = item;
5552
5553 return die->offset.sect_off;
5554 }
5555
5556 /* Trivial comparison function for die_info structures: two DIEs
5557 are equal if they have the same offset. */
5558
5559 static int
5560 die_eq (const void *item_lhs, const void *item_rhs)
5561 {
5562 const struct die_info *die_lhs = item_lhs;
5563 const struct die_info *die_rhs = item_rhs;
5564
5565 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
5566 }
5567
5568 /* die_reader_func for load_full_comp_unit.
5569 This is identical to read_signatured_type_reader,
5570 but is kept separate for now. */
5571
5572 static void
5573 load_full_comp_unit_reader (const struct die_reader_specs *reader,
5574 gdb_byte *info_ptr,
5575 struct die_info *comp_unit_die,
5576 int has_children,
5577 void *data)
5578 {
5579 struct dwarf2_cu *cu = reader->cu;
5580 enum language *language_ptr = data;
5581
5582 gdb_assert (cu->die_hash == NULL);
5583 cu->die_hash =
5584 htab_create_alloc_ex (cu->header.length / 12,
5585 die_hash,
5586 die_eq,
5587 NULL,
5588 &cu->comp_unit_obstack,
5589 hashtab_obstack_allocate,
5590 dummy_obstack_deallocate);
5591
5592 if (has_children)
5593 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
5594 &info_ptr, comp_unit_die);
5595 cu->dies = comp_unit_die;
5596 /* comp_unit_die is not stored in die_hash, no need. */
5597
5598 /* We try not to read any attributes in this function, because not
5599 all CUs needed for references have been loaded yet, and symbol
5600 table processing isn't initialized. But we have to set the CU language,
5601 or we won't be able to build types correctly.
5602 Similarly, if we do not read the producer, we can not apply
5603 producer-specific interpretation. */
5604 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
5605 }
5606
5607 /* Load the DIEs associated with PER_CU into memory. */
5608
5609 static void
5610 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
5611 enum language pretend_language)
5612 {
5613 gdb_assert (! this_cu->is_debug_types);
5614
5615 init_cutu_and_read_dies (this_cu, 1, 1, load_full_comp_unit_reader,
5616 &pretend_language);
5617 }
5618
5619 /* Add a DIE to the delayed physname list. */
5620
5621 static void
5622 add_to_method_list (struct type *type, int fnfield_index, int index,
5623 const char *name, struct die_info *die,
5624 struct dwarf2_cu *cu)
5625 {
5626 struct delayed_method_info mi;
5627 mi.type = type;
5628 mi.fnfield_index = fnfield_index;
5629 mi.index = index;
5630 mi.name = name;
5631 mi.die = die;
5632 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
5633 }
5634
5635 /* A cleanup for freeing the delayed method list. */
5636
5637 static void
5638 free_delayed_list (void *ptr)
5639 {
5640 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
5641 if (cu->method_list != NULL)
5642 {
5643 VEC_free (delayed_method_info, cu->method_list);
5644 cu->method_list = NULL;
5645 }
5646 }
5647
5648 /* Compute the physnames of any methods on the CU's method list.
5649
5650 The computation of method physnames is delayed in order to avoid the
5651 (bad) condition that one of the method's formal parameters is of an as yet
5652 incomplete type. */
5653
5654 static void
5655 compute_delayed_physnames (struct dwarf2_cu *cu)
5656 {
5657 int i;
5658 struct delayed_method_info *mi;
5659 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
5660 {
5661 const char *physname;
5662 struct fn_fieldlist *fn_flp
5663 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
5664 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
5665 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
5666 }
5667 }
5668
5669 /* Go objects should be embedded in a DW_TAG_module DIE,
5670 and it's not clear if/how imported objects will appear.
5671 To keep Go support simple until that's worked out,
5672 go back through what we've read and create something usable.
5673 We could do this while processing each DIE, and feels kinda cleaner,
5674 but that way is more invasive.
5675 This is to, for example, allow the user to type "p var" or "b main"
5676 without having to specify the package name, and allow lookups
5677 of module.object to work in contexts that use the expression
5678 parser. */
5679
5680 static void
5681 fixup_go_packaging (struct dwarf2_cu *cu)
5682 {
5683 char *package_name = NULL;
5684 struct pending *list;
5685 int i;
5686
5687 for (list = global_symbols; list != NULL; list = list->next)
5688 {
5689 for (i = 0; i < list->nsyms; ++i)
5690 {
5691 struct symbol *sym = list->symbol[i];
5692
5693 if (SYMBOL_LANGUAGE (sym) == language_go
5694 && SYMBOL_CLASS (sym) == LOC_BLOCK)
5695 {
5696 char *this_package_name = go_symbol_package_name (sym);
5697
5698 if (this_package_name == NULL)
5699 continue;
5700 if (package_name == NULL)
5701 package_name = this_package_name;
5702 else
5703 {
5704 if (strcmp (package_name, this_package_name) != 0)
5705 complaint (&symfile_complaints,
5706 _("Symtab %s has objects from two different Go packages: %s and %s"),
5707 (sym->symtab && sym->symtab->filename
5708 ? sym->symtab->filename
5709 : cu->objfile->name),
5710 this_package_name, package_name);
5711 xfree (this_package_name);
5712 }
5713 }
5714 }
5715 }
5716
5717 if (package_name != NULL)
5718 {
5719 struct objfile *objfile = cu->objfile;
5720 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
5721 package_name, objfile);
5722 struct symbol *sym;
5723
5724 TYPE_TAG_NAME (type) = TYPE_NAME (type);
5725
5726 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
5727 SYMBOL_SET_LANGUAGE (sym, language_go);
5728 SYMBOL_SET_NAMES (sym, package_name, strlen (package_name), 1, objfile);
5729 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
5730 e.g., "main" finds the "main" module and not C's main(). */
5731 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
5732 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
5733 SYMBOL_TYPE (sym) = type;
5734
5735 add_symbol_to_list (sym, &global_symbols);
5736
5737 xfree (package_name);
5738 }
5739 }
5740
5741 static void compute_symtab_includes (struct dwarf2_per_cu_data *per_cu);
5742
5743 /* Return the symtab for PER_CU. This works properly regardless of
5744 whether we're using the index or psymtabs. */
5745
5746 static struct symtab *
5747 get_symtab (struct dwarf2_per_cu_data *per_cu)
5748 {
5749 return (dwarf2_per_objfile->using_index
5750 ? per_cu->v.quick->symtab
5751 : per_cu->v.psymtab->symtab);
5752 }
5753
5754 /* A helper function for computing the list of all symbol tables
5755 included by PER_CU. */
5756
5757 static void
5758 recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
5759 htab_t all_children,
5760 struct dwarf2_per_cu_data *per_cu)
5761 {
5762 void **slot;
5763 int ix;
5764 struct dwarf2_per_cu_data *iter;
5765
5766 slot = htab_find_slot (all_children, per_cu, INSERT);
5767 if (*slot != NULL)
5768 {
5769 /* This inclusion and its children have been processed. */
5770 return;
5771 }
5772
5773 *slot = per_cu;
5774 /* Only add a CU if it has a symbol table. */
5775 if (get_symtab (per_cu) != NULL)
5776 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
5777
5778 for (ix = 0;
5779 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
5780 ++ix)
5781 recursively_compute_inclusions (result, all_children, iter);
5782 }
5783
5784 /* Compute the symtab 'includes' fields for the symtab related to
5785 PER_CU. */
5786
5787 static void
5788 compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
5789 {
5790 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
5791 {
5792 int ix, len;
5793 struct dwarf2_per_cu_data *iter;
5794 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
5795 htab_t all_children;
5796 struct symtab *symtab = get_symtab (per_cu);
5797
5798 /* If we don't have a symtab, we can just skip this case. */
5799 if (symtab == NULL)
5800 return;
5801
5802 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
5803 NULL, xcalloc, xfree);
5804
5805 for (ix = 0;
5806 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
5807 ix, iter);
5808 ++ix)
5809 recursively_compute_inclusions (&result_children, all_children, iter);
5810
5811 /* Now we have a transitive closure of all the included CUs, so
5812 we can convert it to a list of symtabs. */
5813 len = VEC_length (dwarf2_per_cu_ptr, result_children);
5814 symtab->includes
5815 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
5816 (len + 1) * sizeof (struct symtab *));
5817 for (ix = 0;
5818 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
5819 ++ix)
5820 symtab->includes[ix] = get_symtab (iter);
5821 symtab->includes[len] = NULL;
5822
5823 VEC_free (dwarf2_per_cu_ptr, result_children);
5824 htab_delete (all_children);
5825 }
5826 }
5827
5828 /* Compute the 'includes' field for the symtabs of all the CUs we just
5829 read. */
5830
5831 static void
5832 process_cu_includes (void)
5833 {
5834 int ix;
5835 struct dwarf2_per_cu_data *iter;
5836
5837 for (ix = 0;
5838 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
5839 ix, iter);
5840 ++ix)
5841 compute_symtab_includes (iter);
5842
5843 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
5844 }
5845
5846 /* Generate full symbol information for PER_CU, whose DIEs have
5847 already been loaded into memory. */
5848
5849 static void
5850 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
5851 enum language pretend_language)
5852 {
5853 struct dwarf2_cu *cu = per_cu->cu;
5854 struct objfile *objfile = per_cu->objfile;
5855 CORE_ADDR lowpc, highpc;
5856 struct symtab *symtab;
5857 struct cleanup *back_to, *delayed_list_cleanup;
5858 CORE_ADDR baseaddr;
5859
5860 if (dwarf2_read_debug)
5861 {
5862 fprintf_unfiltered (gdb_stdlog,
5863 "Expanding symtab of %s at offset 0x%x\n",
5864 per_cu->is_debug_types ? "TU" : "CU",
5865 per_cu->offset.sect_off);
5866 }
5867
5868 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5869
5870 buildsym_init ();
5871 back_to = make_cleanup (really_free_pendings, NULL);
5872 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
5873
5874 cu->list_in_scope = &file_symbols;
5875
5876 cu->language = pretend_language;
5877 cu->language_defn = language_def (cu->language);
5878
5879 /* Do line number decoding in read_file_scope () */
5880 process_die (cu->dies, cu);
5881
5882 /* For now fudge the Go package. */
5883 if (cu->language == language_go)
5884 fixup_go_packaging (cu);
5885
5886 /* Now that we have processed all the DIEs in the CU, all the types
5887 should be complete, and it should now be safe to compute all of the
5888 physnames. */
5889 compute_delayed_physnames (cu);
5890 do_cleanups (delayed_list_cleanup);
5891
5892 /* Some compilers don't define a DW_AT_high_pc attribute for the
5893 compilation unit. If the DW_AT_high_pc is missing, synthesize
5894 it, by scanning the DIE's below the compilation unit. */
5895 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
5896
5897 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
5898
5899 if (symtab != NULL)
5900 {
5901 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
5902
5903 /* Set symtab language to language from DW_AT_language. If the
5904 compilation is from a C file generated by language preprocessors, do
5905 not set the language if it was already deduced by start_subfile. */
5906 if (!(cu->language == language_c && symtab->language != language_c))
5907 symtab->language = cu->language;
5908
5909 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
5910 produce DW_AT_location with location lists but it can be possibly
5911 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
5912 there were bugs in prologue debug info, fixed later in GCC-4.5
5913 by "unwind info for epilogues" patch (which is not directly related).
5914
5915 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
5916 needed, it would be wrong due to missing DW_AT_producer there.
5917
5918 Still one can confuse GDB by using non-standard GCC compilation
5919 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5920 */
5921 if (cu->has_loclist && gcc_4_minor >= 5)
5922 symtab->locations_valid = 1;
5923
5924 if (gcc_4_minor >= 5)
5925 symtab->epilogue_unwind_valid = 1;
5926
5927 symtab->call_site_htab = cu->call_site_htab;
5928 }
5929
5930 if (dwarf2_per_objfile->using_index)
5931 per_cu->v.quick->symtab = symtab;
5932 else
5933 {
5934 struct partial_symtab *pst = per_cu->v.psymtab;
5935 pst->symtab = symtab;
5936 pst->readin = 1;
5937 }
5938
5939 /* Push it for inclusion processing later. */
5940 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
5941
5942 do_cleanups (back_to);
5943
5944 if (dwarf2_read_debug)
5945 {
5946 fprintf_unfiltered (gdb_stdlog,
5947 "Done expanding symtab of %s at offset 0x%x\n",
5948 per_cu->is_debug_types ? "TU" : "CU",
5949 per_cu->offset.sect_off);
5950 }
5951 }
5952
5953 /* Process an imported unit DIE. */
5954
5955 static void
5956 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
5957 {
5958 struct attribute *attr;
5959
5960 attr = dwarf2_attr (die, DW_AT_import, cu);
5961 if (attr != NULL)
5962 {
5963 struct dwarf2_per_cu_data *per_cu;
5964 struct symtab *imported_symtab;
5965 sect_offset offset;
5966
5967 offset = dwarf2_get_ref_die_offset (attr);
5968 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
5969
5970 /* Queue the unit, if needed. */
5971 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
5972 load_full_comp_unit (per_cu, cu->language);
5973
5974 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
5975 per_cu);
5976 }
5977 }
5978
5979 /* Process a die and its children. */
5980
5981 static void
5982 process_die (struct die_info *die, struct dwarf2_cu *cu)
5983 {
5984 switch (die->tag)
5985 {
5986 case DW_TAG_padding:
5987 break;
5988 case DW_TAG_compile_unit:
5989 case DW_TAG_partial_unit:
5990 read_file_scope (die, cu);
5991 break;
5992 case DW_TAG_type_unit:
5993 read_type_unit_scope (die, cu);
5994 break;
5995 case DW_TAG_subprogram:
5996 case DW_TAG_inlined_subroutine:
5997 read_func_scope (die, cu);
5998 break;
5999 case DW_TAG_lexical_block:
6000 case DW_TAG_try_block:
6001 case DW_TAG_catch_block:
6002 read_lexical_block_scope (die, cu);
6003 break;
6004 case DW_TAG_GNU_call_site:
6005 read_call_site_scope (die, cu);
6006 break;
6007 case DW_TAG_class_type:
6008 case DW_TAG_interface_type:
6009 case DW_TAG_structure_type:
6010 case DW_TAG_union_type:
6011 process_structure_scope (die, cu);
6012 break;
6013 case DW_TAG_enumeration_type:
6014 process_enumeration_scope (die, cu);
6015 break;
6016
6017 /* These dies have a type, but processing them does not create
6018 a symbol or recurse to process the children. Therefore we can
6019 read them on-demand through read_type_die. */
6020 case DW_TAG_subroutine_type:
6021 case DW_TAG_set_type:
6022 case DW_TAG_array_type:
6023 case DW_TAG_pointer_type:
6024 case DW_TAG_ptr_to_member_type:
6025 case DW_TAG_reference_type:
6026 case DW_TAG_string_type:
6027 break;
6028
6029 case DW_TAG_base_type:
6030 case DW_TAG_subrange_type:
6031 case DW_TAG_typedef:
6032 /* Add a typedef symbol for the type definition, if it has a
6033 DW_AT_name. */
6034 new_symbol (die, read_type_die (die, cu), cu);
6035 break;
6036 case DW_TAG_common_block:
6037 read_common_block (die, cu);
6038 break;
6039 case DW_TAG_common_inclusion:
6040 break;
6041 case DW_TAG_namespace:
6042 processing_has_namespace_info = 1;
6043 read_namespace (die, cu);
6044 break;
6045 case DW_TAG_module:
6046 processing_has_namespace_info = 1;
6047 read_module (die, cu);
6048 break;
6049 case DW_TAG_imported_declaration:
6050 case DW_TAG_imported_module:
6051 processing_has_namespace_info = 1;
6052 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
6053 || cu->language != language_fortran))
6054 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
6055 dwarf_tag_name (die->tag));
6056 read_import_statement (die, cu);
6057 break;
6058
6059 case DW_TAG_imported_unit:
6060 process_imported_unit_die (die, cu);
6061 break;
6062
6063 default:
6064 new_symbol (die, NULL, cu);
6065 break;
6066 }
6067 }
6068
6069 /* A helper function for dwarf2_compute_name which determines whether DIE
6070 needs to have the name of the scope prepended to the name listed in the
6071 die. */
6072
6073 static int
6074 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
6075 {
6076 struct attribute *attr;
6077
6078 switch (die->tag)
6079 {
6080 case DW_TAG_namespace:
6081 case DW_TAG_typedef:
6082 case DW_TAG_class_type:
6083 case DW_TAG_interface_type:
6084 case DW_TAG_structure_type:
6085 case DW_TAG_union_type:
6086 case DW_TAG_enumeration_type:
6087 case DW_TAG_enumerator:
6088 case DW_TAG_subprogram:
6089 case DW_TAG_member:
6090 return 1;
6091
6092 case DW_TAG_variable:
6093 case DW_TAG_constant:
6094 /* We only need to prefix "globally" visible variables. These include
6095 any variable marked with DW_AT_external or any variable that
6096 lives in a namespace. [Variables in anonymous namespaces
6097 require prefixing, but they are not DW_AT_external.] */
6098
6099 if (dwarf2_attr (die, DW_AT_specification, cu))
6100 {
6101 struct dwarf2_cu *spec_cu = cu;
6102
6103 return die_needs_namespace (die_specification (die, &spec_cu),
6104 spec_cu);
6105 }
6106
6107 attr = dwarf2_attr (die, DW_AT_external, cu);
6108 if (attr == NULL && die->parent->tag != DW_TAG_namespace
6109 && die->parent->tag != DW_TAG_module)
6110 return 0;
6111 /* A variable in a lexical block of some kind does not need a
6112 namespace, even though in C++ such variables may be external
6113 and have a mangled name. */
6114 if (die->parent->tag == DW_TAG_lexical_block
6115 || die->parent->tag == DW_TAG_try_block
6116 || die->parent->tag == DW_TAG_catch_block
6117 || die->parent->tag == DW_TAG_subprogram)
6118 return 0;
6119 return 1;
6120
6121 default:
6122 return 0;
6123 }
6124 }
6125
6126 /* Retrieve the last character from a mem_file. */
6127
6128 static void
6129 do_ui_file_peek_last (void *object, const char *buffer, long length)
6130 {
6131 char *last_char_p = (char *) object;
6132
6133 if (length > 0)
6134 *last_char_p = buffer[length - 1];
6135 }
6136
6137 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
6138 compute the physname for the object, which include a method's:
6139 - formal parameters (C++/Java),
6140 - receiver type (Go),
6141 - return type (Java).
6142
6143 The term "physname" is a bit confusing.
6144 For C++, for example, it is the demangled name.
6145 For Go, for example, it's the mangled name.
6146
6147 For Ada, return the DIE's linkage name rather than the fully qualified
6148 name. PHYSNAME is ignored..
6149
6150 The result is allocated on the objfile_obstack and canonicalized. */
6151
6152 static const char *
6153 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
6154 int physname)
6155 {
6156 struct objfile *objfile = cu->objfile;
6157
6158 if (name == NULL)
6159 name = dwarf2_name (die, cu);
6160
6161 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
6162 compute it by typename_concat inside GDB. */
6163 if (cu->language == language_ada
6164 || (cu->language == language_fortran && physname))
6165 {
6166 /* For Ada unit, we prefer the linkage name over the name, as
6167 the former contains the exported name, which the user expects
6168 to be able to reference. Ideally, we want the user to be able
6169 to reference this entity using either natural or linkage name,
6170 but we haven't started looking at this enhancement yet. */
6171 struct attribute *attr;
6172
6173 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
6174 if (attr == NULL)
6175 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
6176 if (attr && DW_STRING (attr))
6177 return DW_STRING (attr);
6178 }
6179
6180 /* These are the only languages we know how to qualify names in. */
6181 if (name != NULL
6182 && (cu->language == language_cplus || cu->language == language_java
6183 || cu->language == language_fortran))
6184 {
6185 if (die_needs_namespace (die, cu))
6186 {
6187 long length;
6188 const char *prefix;
6189 struct ui_file *buf;
6190
6191 prefix = determine_prefix (die, cu);
6192 buf = mem_fileopen ();
6193 if (*prefix != '\0')
6194 {
6195 char *prefixed_name = typename_concat (NULL, prefix, name,
6196 physname, cu);
6197
6198 fputs_unfiltered (prefixed_name, buf);
6199 xfree (prefixed_name);
6200 }
6201 else
6202 fputs_unfiltered (name, buf);
6203
6204 /* Template parameters may be specified in the DIE's DW_AT_name, or
6205 as children with DW_TAG_template_type_param or
6206 DW_TAG_value_type_param. If the latter, add them to the name
6207 here. If the name already has template parameters, then
6208 skip this step; some versions of GCC emit both, and
6209 it is more efficient to use the pre-computed name.
6210
6211 Something to keep in mind about this process: it is very
6212 unlikely, or in some cases downright impossible, to produce
6213 something that will match the mangled name of a function.
6214 If the definition of the function has the same debug info,
6215 we should be able to match up with it anyway. But fallbacks
6216 using the minimal symbol, for instance to find a method
6217 implemented in a stripped copy of libstdc++, will not work.
6218 If we do not have debug info for the definition, we will have to
6219 match them up some other way.
6220
6221 When we do name matching there is a related problem with function
6222 templates; two instantiated function templates are allowed to
6223 differ only by their return types, which we do not add here. */
6224
6225 if (cu->language == language_cplus && strchr (name, '<') == NULL)
6226 {
6227 struct attribute *attr;
6228 struct die_info *child;
6229 int first = 1;
6230
6231 die->building_fullname = 1;
6232
6233 for (child = die->child; child != NULL; child = child->sibling)
6234 {
6235 struct type *type;
6236 LONGEST value;
6237 gdb_byte *bytes;
6238 struct dwarf2_locexpr_baton *baton;
6239 struct value *v;
6240
6241 if (child->tag != DW_TAG_template_type_param
6242 && child->tag != DW_TAG_template_value_param)
6243 continue;
6244
6245 if (first)
6246 {
6247 fputs_unfiltered ("<", buf);
6248 first = 0;
6249 }
6250 else
6251 fputs_unfiltered (", ", buf);
6252
6253 attr = dwarf2_attr (child, DW_AT_type, cu);
6254 if (attr == NULL)
6255 {
6256 complaint (&symfile_complaints,
6257 _("template parameter missing DW_AT_type"));
6258 fputs_unfiltered ("UNKNOWN_TYPE", buf);
6259 continue;
6260 }
6261 type = die_type (child, cu);
6262
6263 if (child->tag == DW_TAG_template_type_param)
6264 {
6265 c_print_type (type, "", buf, -1, 0);
6266 continue;
6267 }
6268
6269 attr = dwarf2_attr (child, DW_AT_const_value, cu);
6270 if (attr == NULL)
6271 {
6272 complaint (&symfile_complaints,
6273 _("template parameter missing "
6274 "DW_AT_const_value"));
6275 fputs_unfiltered ("UNKNOWN_VALUE", buf);
6276 continue;
6277 }
6278
6279 dwarf2_const_value_attr (attr, type, name,
6280 &cu->comp_unit_obstack, cu,
6281 &value, &bytes, &baton);
6282
6283 if (TYPE_NOSIGN (type))
6284 /* GDB prints characters as NUMBER 'CHAR'. If that's
6285 changed, this can use value_print instead. */
6286 c_printchar (value, type, buf);
6287 else
6288 {
6289 struct value_print_options opts;
6290
6291 if (baton != NULL)
6292 v = dwarf2_evaluate_loc_desc (type, NULL,
6293 baton->data,
6294 baton->size,
6295 baton->per_cu);
6296 else if (bytes != NULL)
6297 {
6298 v = allocate_value (type);
6299 memcpy (value_contents_writeable (v), bytes,
6300 TYPE_LENGTH (type));
6301 }
6302 else
6303 v = value_from_longest (type, value);
6304
6305 /* Specify decimal so that we do not depend on
6306 the radix. */
6307 get_formatted_print_options (&opts, 'd');
6308 opts.raw = 1;
6309 value_print (v, buf, &opts);
6310 release_value (v);
6311 value_free (v);
6312 }
6313 }
6314
6315 die->building_fullname = 0;
6316
6317 if (!first)
6318 {
6319 /* Close the argument list, with a space if necessary
6320 (nested templates). */
6321 char last_char = '\0';
6322 ui_file_put (buf, do_ui_file_peek_last, &last_char);
6323 if (last_char == '>')
6324 fputs_unfiltered (" >", buf);
6325 else
6326 fputs_unfiltered (">", buf);
6327 }
6328 }
6329
6330 /* For Java and C++ methods, append formal parameter type
6331 information, if PHYSNAME. */
6332
6333 if (physname && die->tag == DW_TAG_subprogram
6334 && (cu->language == language_cplus
6335 || cu->language == language_java))
6336 {
6337 struct type *type = read_type_die (die, cu);
6338
6339 c_type_print_args (type, buf, 1, cu->language);
6340
6341 if (cu->language == language_java)
6342 {
6343 /* For java, we must append the return type to method
6344 names. */
6345 if (die->tag == DW_TAG_subprogram)
6346 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
6347 0, 0);
6348 }
6349 else if (cu->language == language_cplus)
6350 {
6351 /* Assume that an artificial first parameter is
6352 "this", but do not crash if it is not. RealView
6353 marks unnamed (and thus unused) parameters as
6354 artificial; there is no way to differentiate
6355 the two cases. */
6356 if (TYPE_NFIELDS (type) > 0
6357 && TYPE_FIELD_ARTIFICIAL (type, 0)
6358 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
6359 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
6360 0))))
6361 fputs_unfiltered (" const", buf);
6362 }
6363 }
6364
6365 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
6366 &length);
6367 ui_file_delete (buf);
6368
6369 if (cu->language == language_cplus)
6370 {
6371 char *cname
6372 = dwarf2_canonicalize_name (name, cu,
6373 &objfile->objfile_obstack);
6374
6375 if (cname != NULL)
6376 name = cname;
6377 }
6378 }
6379 }
6380
6381 return name;
6382 }
6383
6384 /* Return the fully qualified name of DIE, based on its DW_AT_name.
6385 If scope qualifiers are appropriate they will be added. The result
6386 will be allocated on the objfile_obstack, or NULL if the DIE does
6387 not have a name. NAME may either be from a previous call to
6388 dwarf2_name or NULL.
6389
6390 The output string will be canonicalized (if C++/Java). */
6391
6392 static const char *
6393 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
6394 {
6395 return dwarf2_compute_name (name, die, cu, 0);
6396 }
6397
6398 /* Construct a physname for the given DIE in CU. NAME may either be
6399 from a previous call to dwarf2_name or NULL. The result will be
6400 allocated on the objfile_objstack or NULL if the DIE does not have a
6401 name.
6402
6403 The output string will be canonicalized (if C++/Java). */
6404
6405 static const char *
6406 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
6407 {
6408 struct objfile *objfile = cu->objfile;
6409 struct attribute *attr;
6410 const char *retval, *mangled = NULL, *canon = NULL;
6411 struct cleanup *back_to;
6412 int need_copy = 1;
6413
6414 /* In this case dwarf2_compute_name is just a shortcut not building anything
6415 on its own. */
6416 if (!die_needs_namespace (die, cu))
6417 return dwarf2_compute_name (name, die, cu, 1);
6418
6419 back_to = make_cleanup (null_cleanup, NULL);
6420
6421 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
6422 if (!attr)
6423 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
6424
6425 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
6426 has computed. */
6427 if (attr && DW_STRING (attr))
6428 {
6429 char *demangled;
6430
6431 mangled = DW_STRING (attr);
6432
6433 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
6434 type. It is easier for GDB users to search for such functions as
6435 `name(params)' than `long name(params)'. In such case the minimal
6436 symbol names do not match the full symbol names but for template
6437 functions there is never a need to look up their definition from their
6438 declaration so the only disadvantage remains the minimal symbol
6439 variant `long name(params)' does not have the proper inferior type.
6440 */
6441
6442 if (cu->language == language_go)
6443 {
6444 /* This is a lie, but we already lie to the caller new_symbol_full.
6445 new_symbol_full assumes we return the mangled name.
6446 This just undoes that lie until things are cleaned up. */
6447 demangled = NULL;
6448 }
6449 else
6450 {
6451 demangled = cplus_demangle (mangled,
6452 (DMGL_PARAMS | DMGL_ANSI
6453 | (cu->language == language_java
6454 ? DMGL_JAVA | DMGL_RET_POSTFIX
6455 : DMGL_RET_DROP)));
6456 }
6457 if (demangled)
6458 {
6459 make_cleanup (xfree, demangled);
6460 canon = demangled;
6461 }
6462 else
6463 {
6464 canon = mangled;
6465 need_copy = 0;
6466 }
6467 }
6468
6469 if (canon == NULL || check_physname)
6470 {
6471 const char *physname = dwarf2_compute_name (name, die, cu, 1);
6472
6473 if (canon != NULL && strcmp (physname, canon) != 0)
6474 {
6475 /* It may not mean a bug in GDB. The compiler could also
6476 compute DW_AT_linkage_name incorrectly. But in such case
6477 GDB would need to be bug-to-bug compatible. */
6478
6479 complaint (&symfile_complaints,
6480 _("Computed physname <%s> does not match demangled <%s> "
6481 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
6482 physname, canon, mangled, die->offset.sect_off, objfile->name);
6483
6484 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
6485 is available here - over computed PHYSNAME. It is safer
6486 against both buggy GDB and buggy compilers. */
6487
6488 retval = canon;
6489 }
6490 else
6491 {
6492 retval = physname;
6493 need_copy = 0;
6494 }
6495 }
6496 else
6497 retval = canon;
6498
6499 if (need_copy)
6500 retval = obsavestring (retval, strlen (retval),
6501 &objfile->objfile_obstack);
6502
6503 do_cleanups (back_to);
6504 return retval;
6505 }
6506
6507 /* Read the import statement specified by the given die and record it. */
6508
6509 static void
6510 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
6511 {
6512 struct objfile *objfile = cu->objfile;
6513 struct attribute *import_attr;
6514 struct die_info *imported_die, *child_die;
6515 struct dwarf2_cu *imported_cu;
6516 const char *imported_name;
6517 const char *imported_name_prefix;
6518 const char *canonical_name;
6519 const char *import_alias;
6520 const char *imported_declaration = NULL;
6521 const char *import_prefix;
6522 VEC (const_char_ptr) *excludes = NULL;
6523 struct cleanup *cleanups;
6524
6525 char *temp;
6526
6527 import_attr = dwarf2_attr (die, DW_AT_import, cu);
6528 if (import_attr == NULL)
6529 {
6530 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
6531 dwarf_tag_name (die->tag));
6532 return;
6533 }
6534
6535 imported_cu = cu;
6536 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
6537 imported_name = dwarf2_name (imported_die, imported_cu);
6538 if (imported_name == NULL)
6539 {
6540 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
6541
6542 The import in the following code:
6543 namespace A
6544 {
6545 typedef int B;
6546 }
6547
6548 int main ()
6549 {
6550 using A::B;
6551 B b;
6552 return b;
6553 }
6554
6555 ...
6556 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
6557 <52> DW_AT_decl_file : 1
6558 <53> DW_AT_decl_line : 6
6559 <54> DW_AT_import : <0x75>
6560 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
6561 <59> DW_AT_name : B
6562 <5b> DW_AT_decl_file : 1
6563 <5c> DW_AT_decl_line : 2
6564 <5d> DW_AT_type : <0x6e>
6565 ...
6566 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
6567 <76> DW_AT_byte_size : 4
6568 <77> DW_AT_encoding : 5 (signed)
6569
6570 imports the wrong die ( 0x75 instead of 0x58 ).
6571 This case will be ignored until the gcc bug is fixed. */
6572 return;
6573 }
6574
6575 /* Figure out the local name after import. */
6576 import_alias = dwarf2_name (die, cu);
6577
6578 /* Figure out where the statement is being imported to. */
6579 import_prefix = determine_prefix (die, cu);
6580
6581 /* Figure out what the scope of the imported die is and prepend it
6582 to the name of the imported die. */
6583 imported_name_prefix = determine_prefix (imported_die, imported_cu);
6584
6585 if (imported_die->tag != DW_TAG_namespace
6586 && imported_die->tag != DW_TAG_module)
6587 {
6588 imported_declaration = imported_name;
6589 canonical_name = imported_name_prefix;
6590 }
6591 else if (strlen (imported_name_prefix) > 0)
6592 {
6593 temp = alloca (strlen (imported_name_prefix)
6594 + 2 + strlen (imported_name) + 1);
6595 strcpy (temp, imported_name_prefix);
6596 strcat (temp, "::");
6597 strcat (temp, imported_name);
6598 canonical_name = temp;
6599 }
6600 else
6601 canonical_name = imported_name;
6602
6603 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
6604
6605 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
6606 for (child_die = die->child; child_die && child_die->tag;
6607 child_die = sibling_die (child_die))
6608 {
6609 /* DWARF-4: A Fortran use statement with a “rename list” may be
6610 represented by an imported module entry with an import attribute
6611 referring to the module and owned entries corresponding to those
6612 entities that are renamed as part of being imported. */
6613
6614 if (child_die->tag != DW_TAG_imported_declaration)
6615 {
6616 complaint (&symfile_complaints,
6617 _("child DW_TAG_imported_declaration expected "
6618 "- DIE at 0x%x [in module %s]"),
6619 child_die->offset.sect_off, objfile->name);
6620 continue;
6621 }
6622
6623 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
6624 if (import_attr == NULL)
6625 {
6626 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
6627 dwarf_tag_name (child_die->tag));
6628 continue;
6629 }
6630
6631 imported_cu = cu;
6632 imported_die = follow_die_ref_or_sig (child_die, import_attr,
6633 &imported_cu);
6634 imported_name = dwarf2_name (imported_die, imported_cu);
6635 if (imported_name == NULL)
6636 {
6637 complaint (&symfile_complaints,
6638 _("child DW_TAG_imported_declaration has unknown "
6639 "imported name - DIE at 0x%x [in module %s]"),
6640 child_die->offset.sect_off, objfile->name);
6641 continue;
6642 }
6643
6644 VEC_safe_push (const_char_ptr, excludes, imported_name);
6645
6646 process_die (child_die, cu);
6647 }
6648
6649 cp_add_using_directive (import_prefix,
6650 canonical_name,
6651 import_alias,
6652 imported_declaration,
6653 excludes,
6654 &objfile->objfile_obstack);
6655
6656 do_cleanups (cleanups);
6657 }
6658
6659 /* Cleanup function for read_file_scope. */
6660
6661 static void
6662 free_cu_line_header (void *arg)
6663 {
6664 struct dwarf2_cu *cu = arg;
6665
6666 free_line_header (cu->line_header);
6667 cu->line_header = NULL;
6668 }
6669
6670 static void
6671 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
6672 char **name, char **comp_dir)
6673 {
6674 struct attribute *attr;
6675
6676 *name = NULL;
6677 *comp_dir = NULL;
6678
6679 /* Find the filename. Do not use dwarf2_name here, since the filename
6680 is not a source language identifier. */
6681 attr = dwarf2_attr (die, DW_AT_name, cu);
6682 if (attr)
6683 {
6684 *name = DW_STRING (attr);
6685 }
6686
6687 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
6688 if (attr)
6689 *comp_dir = DW_STRING (attr);
6690 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
6691 {
6692 *comp_dir = ldirname (*name);
6693 if (*comp_dir != NULL)
6694 make_cleanup (xfree, *comp_dir);
6695 }
6696 if (*comp_dir != NULL)
6697 {
6698 /* Irix 6.2 native cc prepends <machine>.: to the compilation
6699 directory, get rid of it. */
6700 char *cp = strchr (*comp_dir, ':');
6701
6702 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
6703 *comp_dir = cp + 1;
6704 }
6705
6706 if (*name == NULL)
6707 *name = "<unknown>";
6708 }
6709
6710 /* Handle DW_AT_stmt_list for a compilation unit or type unit.
6711 DIE is the DW_TAG_compile_unit or DW_TAG_type_unit die for CU.
6712 COMP_DIR is the compilation directory.
6713 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
6714
6715 static void
6716 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
6717 const char *comp_dir, int want_line_info)
6718 {
6719 struct attribute *attr;
6720
6721 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6722 if (attr)
6723 {
6724 unsigned int line_offset = DW_UNSND (attr);
6725 struct line_header *line_header
6726 = dwarf_decode_line_header (line_offset, cu);
6727
6728 if (line_header)
6729 {
6730 cu->line_header = line_header;
6731 make_cleanup (free_cu_line_header, cu);
6732 dwarf_decode_lines (line_header, comp_dir, cu, NULL, want_line_info);
6733 }
6734 }
6735 }
6736
6737 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
6738
6739 static void
6740 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
6741 {
6742 struct objfile *objfile = dwarf2_per_objfile->objfile;
6743 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
6744 CORE_ADDR lowpc = ((CORE_ADDR) -1);
6745 CORE_ADDR highpc = ((CORE_ADDR) 0);
6746 struct attribute *attr;
6747 char *name = NULL;
6748 char *comp_dir = NULL;
6749 struct die_info *child_die;
6750 bfd *abfd = objfile->obfd;
6751 CORE_ADDR baseaddr;
6752
6753 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6754
6755 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
6756
6757 /* If we didn't find a lowpc, set it to highpc to avoid complaints
6758 from finish_block. */
6759 if (lowpc == ((CORE_ADDR) -1))
6760 lowpc = highpc;
6761 lowpc += baseaddr;
6762 highpc += baseaddr;
6763
6764 find_file_and_directory (die, cu, &name, &comp_dir);
6765
6766 prepare_one_comp_unit (cu, die, cu->language);
6767
6768 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
6769 standardised yet. As a workaround for the language detection we fall
6770 back to the DW_AT_producer string. */
6771 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
6772 cu->language = language_opencl;
6773
6774 /* Similar hack for Go. */
6775 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
6776 set_cu_language (DW_LANG_Go, cu);
6777
6778 /* We assume that we're processing GCC output. */
6779 processing_gcc_compilation = 2;
6780
6781 processing_has_namespace_info = 0;
6782
6783 start_symtab (name, comp_dir, lowpc);
6784 record_debugformat ("DWARF 2");
6785 record_producer (cu->producer);
6786
6787 /* Decode line number information if present. We do this before
6788 processing child DIEs, so that the line header table is available
6789 for DW_AT_decl_file. */
6790 handle_DW_AT_stmt_list (die, cu, comp_dir, 1);
6791
6792 /* Process all dies in compilation unit. */
6793 if (die->child != NULL)
6794 {
6795 child_die = die->child;
6796 while (child_die && child_die->tag)
6797 {
6798 process_die (child_die, cu);
6799 child_die = sibling_die (child_die);
6800 }
6801 }
6802
6803 /* Decode macro information, if present. Dwarf 2 macro information
6804 refers to information in the line number info statement program
6805 header, so we can only read it if we've read the header
6806 successfully. */
6807 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
6808 if (attr && cu->line_header)
6809 {
6810 if (dwarf2_attr (die, DW_AT_macro_info, cu))
6811 complaint (&symfile_complaints,
6812 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
6813
6814 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
6815 }
6816 else
6817 {
6818 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
6819 if (attr && cu->line_header)
6820 {
6821 unsigned int macro_offset = DW_UNSND (attr);
6822
6823 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
6824 }
6825 }
6826
6827 do_cleanups (back_to);
6828 }
6829
6830 /* Process DW_TAG_type_unit.
6831 For TUs we want to skip the first top level sibling if it's not the
6832 actual type being defined by this TU. In this case the first top
6833 level sibling is there to provide context only. */
6834
6835 static void
6836 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
6837 {
6838 struct objfile *objfile = cu->objfile;
6839 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
6840 CORE_ADDR lowpc;
6841 struct attribute *attr;
6842 char *name = NULL;
6843 char *comp_dir = NULL;
6844 struct die_info *child_die;
6845 bfd *abfd = objfile->obfd;
6846
6847 /* start_symtab needs a low pc, but we don't really have one.
6848 Do what read_file_scope would do in the absence of such info. */
6849 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6850
6851 /* Find the filename. Do not use dwarf2_name here, since the filename
6852 is not a source language identifier. */
6853 attr = dwarf2_attr (die, DW_AT_name, cu);
6854 if (attr)
6855 name = DW_STRING (attr);
6856
6857 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
6858 if (attr)
6859 comp_dir = DW_STRING (attr);
6860 else if (name != NULL && IS_ABSOLUTE_PATH (name))
6861 {
6862 comp_dir = ldirname (name);
6863 if (comp_dir != NULL)
6864 make_cleanup (xfree, comp_dir);
6865 }
6866
6867 if (name == NULL)
6868 name = "<unknown>";
6869
6870 prepare_one_comp_unit (cu, die, language_minimal);
6871
6872 /* We assume that we're processing GCC output. */
6873 processing_gcc_compilation = 2;
6874
6875 processing_has_namespace_info = 0;
6876
6877 start_symtab (name, comp_dir, lowpc);
6878 record_debugformat ("DWARF 2");
6879 record_producer (cu->producer);
6880
6881 /* Decode line number information if present. We do this before
6882 processing child DIEs, so that the line header table is available
6883 for DW_AT_decl_file.
6884 We don't need the pc/line-number mapping for type units. */
6885 handle_DW_AT_stmt_list (die, cu, comp_dir, 0);
6886
6887 /* Process the dies in the type unit. */
6888 if (die->child == NULL)
6889 {
6890 dump_die_for_error (die);
6891 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
6892 bfd_get_filename (abfd));
6893 }
6894
6895 child_die = die->child;
6896
6897 while (child_die && child_die->tag)
6898 {
6899 process_die (child_die, cu);
6900
6901 child_die = sibling_die (child_die);
6902 }
6903
6904 do_cleanups (back_to);
6905 }
6906 \f
6907 /* DWO files. */
6908
6909 static hashval_t
6910 hash_dwo_file (const void *item)
6911 {
6912 const struct dwo_file *dwo_file = item;
6913
6914 return htab_hash_string (dwo_file->dwo_name);
6915 }
6916
6917 static int
6918 eq_dwo_file (const void *item_lhs, const void *item_rhs)
6919 {
6920 const struct dwo_file *lhs = item_lhs;
6921 const struct dwo_file *rhs = item_rhs;
6922
6923 return strcmp (lhs->dwo_name, rhs->dwo_name) == 0;
6924 }
6925
6926 /* Allocate a hash table for DWO files. */
6927
6928 static htab_t
6929 allocate_dwo_file_hash_table (void)
6930 {
6931 struct objfile *objfile = dwarf2_per_objfile->objfile;
6932
6933 return htab_create_alloc_ex (41,
6934 hash_dwo_file,
6935 eq_dwo_file,
6936 NULL,
6937 &objfile->objfile_obstack,
6938 hashtab_obstack_allocate,
6939 dummy_obstack_deallocate);
6940 }
6941
6942 static hashval_t
6943 hash_dwo_unit (const void *item)
6944 {
6945 const struct dwo_unit *dwo_unit = item;
6946
6947 /* This drops the top 32 bits of the id, but is ok for a hash. */
6948 return dwo_unit->signature;
6949 }
6950
6951 static int
6952 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
6953 {
6954 const struct dwo_unit *lhs = item_lhs;
6955 const struct dwo_unit *rhs = item_rhs;
6956
6957 /* The signature is assumed to be unique within the DWO file.
6958 So while object file CU dwo_id's always have the value zero,
6959 that's OK, assuming each object file DWO file has only one CU,
6960 and that's the rule for now. */
6961 return lhs->signature == rhs->signature;
6962 }
6963
6964 /* Allocate a hash table for DWO CUs,TUs.
6965 There is one of these tables for each of CUs,TUs for each DWO file. */
6966
6967 static htab_t
6968 allocate_dwo_unit_table (struct objfile *objfile)
6969 {
6970 /* Start out with a pretty small number.
6971 Generally DWO files contain only one CU and maybe some TUs. */
6972 return htab_create_alloc_ex (3,
6973 hash_dwo_unit,
6974 eq_dwo_unit,
6975 NULL,
6976 &objfile->objfile_obstack,
6977 hashtab_obstack_allocate,
6978 dummy_obstack_deallocate);
6979 }
6980
6981 /* This function is mapped across the sections and remembers the offset and
6982 size of each of the DWO debugging sections we are interested in. */
6983
6984 static void
6985 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_file_ptr)
6986 {
6987 struct dwo_file *dwo_file = dwo_file_ptr;
6988 const struct dwo_section_names *names = &dwo_section_names;
6989
6990 if (section_is_p (sectp->name, &names->abbrev_dwo))
6991 {
6992 dwo_file->sections.abbrev.asection = sectp;
6993 dwo_file->sections.abbrev.size = bfd_get_section_size (sectp);
6994 }
6995 else if (section_is_p (sectp->name, &names->info_dwo))
6996 {
6997 dwo_file->sections.info.asection = sectp;
6998 dwo_file->sections.info.size = bfd_get_section_size (sectp);
6999 }
7000 else if (section_is_p (sectp->name, &names->line_dwo))
7001 {
7002 dwo_file->sections.line.asection = sectp;
7003 dwo_file->sections.line.size = bfd_get_section_size (sectp);
7004 }
7005 else if (section_is_p (sectp->name, &names->loc_dwo))
7006 {
7007 dwo_file->sections.loc.asection = sectp;
7008 dwo_file->sections.loc.size = bfd_get_section_size (sectp);
7009 }
7010 else if (section_is_p (sectp->name, &names->macinfo_dwo))
7011 {
7012 dwo_file->sections.macinfo.asection = sectp;
7013 dwo_file->sections.macinfo.size = bfd_get_section_size (sectp);
7014 }
7015 else if (section_is_p (sectp->name, &names->macro_dwo))
7016 {
7017 dwo_file->sections.macro.asection = sectp;
7018 dwo_file->sections.macro.size = bfd_get_section_size (sectp);
7019 }
7020 else if (section_is_p (sectp->name, &names->str_dwo))
7021 {
7022 dwo_file->sections.str.asection = sectp;
7023 dwo_file->sections.str.size = bfd_get_section_size (sectp);
7024 }
7025 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
7026 {
7027 dwo_file->sections.str_offsets.asection = sectp;
7028 dwo_file->sections.str_offsets.size = bfd_get_section_size (sectp);
7029 }
7030 else if (section_is_p (sectp->name, &names->types_dwo))
7031 {
7032 struct dwarf2_section_info type_section;
7033
7034 memset (&type_section, 0, sizeof (type_section));
7035 type_section.asection = sectp;
7036 type_section.size = bfd_get_section_size (sectp);
7037 VEC_safe_push (dwarf2_section_info_def, dwo_file->sections.types,
7038 &type_section);
7039 }
7040 }
7041
7042 /* Structure used to pass data to create_debug_info_hash_table_reader. */
7043
7044 struct create_dwo_info_table_data
7045 {
7046 struct dwo_file *dwo_file;
7047 htab_t cu_htab;
7048 };
7049
7050 /* die_reader_func for create_debug_info_hash_table. */
7051
7052 static void
7053 create_debug_info_hash_table_reader (const struct die_reader_specs *reader,
7054 gdb_byte *info_ptr,
7055 struct die_info *comp_unit_die,
7056 int has_children,
7057 void *datap)
7058 {
7059 struct dwarf2_cu *cu = reader->cu;
7060 struct objfile *objfile = dwarf2_per_objfile->objfile;
7061 sect_offset offset = cu->per_cu->offset;
7062 struct dwarf2_section_info *section = cu->per_cu->info_or_types_section;
7063 struct create_dwo_info_table_data *data = datap;
7064 struct dwo_file *dwo_file = data->dwo_file;
7065 htab_t cu_htab = data->cu_htab;
7066 void **slot;
7067 struct attribute *attr;
7068 struct dwo_unit *dwo_unit;
7069
7070 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7071 if (attr == NULL)
7072 {
7073 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
7074 " its dwo_id [in module %s]"),
7075 offset.sect_off, dwo_file->dwo_name);
7076 return;
7077 }
7078
7079 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
7080 dwo_unit->dwo_file = dwo_file;
7081 dwo_unit->signature = DW_UNSND (attr);
7082 dwo_unit->info_or_types_section = section;
7083 dwo_unit->offset = offset;
7084 dwo_unit->length = cu->per_cu->length;
7085
7086 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
7087 gdb_assert (slot != NULL);
7088 if (*slot != NULL)
7089 {
7090 const struct dwo_unit *dup_dwo_unit = *slot;
7091
7092 complaint (&symfile_complaints,
7093 _("debug entry at offset 0x%x is duplicate to the entry at"
7094 " offset 0x%x, dwo_id 0x%s [in module %s]"),
7095 offset.sect_off, dup_dwo_unit->offset.sect_off,
7096 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
7097 dwo_file->dwo_name);
7098 }
7099 else
7100 *slot = dwo_unit;
7101
7102 if (dwarf2_die_debug)
7103 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
7104 offset.sect_off,
7105 phex (dwo_unit->signature,
7106 sizeof (dwo_unit->signature)));
7107 }
7108
7109 /* Create a hash table to map DWO IDs to their CU entry in .debug_info.dwo. */
7110
7111 static htab_t
7112 create_debug_info_hash_table (struct dwo_file *dwo_file)
7113 {
7114 struct objfile *objfile = dwarf2_per_objfile->objfile;
7115 struct dwarf2_section_info *section = &dwo_file->sections.info;
7116 bfd *abfd;
7117 htab_t cu_htab;
7118 gdb_byte *info_ptr, *end_ptr;
7119 struct create_dwo_info_table_data create_dwo_info_table_data;
7120
7121 dwarf2_read_section (objfile, section);
7122 info_ptr = section->buffer;
7123
7124 if (info_ptr == NULL)
7125 return NULL;
7126
7127 /* We can't set abfd until now because the section may be empty or
7128 not present, in which case section->asection will be NULL. */
7129 abfd = section->asection->owner;
7130
7131 if (dwarf2_die_debug)
7132 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
7133 bfd_get_filename (abfd));
7134
7135 cu_htab = allocate_dwo_unit_table (objfile);
7136
7137 create_dwo_info_table_data.dwo_file = dwo_file;
7138 create_dwo_info_table_data.cu_htab = cu_htab;
7139
7140 end_ptr = info_ptr + section->size;
7141 while (info_ptr < end_ptr)
7142 {
7143 struct dwarf2_per_cu_data per_cu;
7144
7145 memset (&per_cu, 0, sizeof (per_cu));
7146 per_cu.objfile = objfile;
7147 per_cu.is_debug_types = 0;
7148 per_cu.offset.sect_off = info_ptr - section->buffer;
7149 per_cu.info_or_types_section = section;
7150
7151 init_cutu_and_read_dies_no_follow (&per_cu,
7152 &dwo_file->sections.abbrev,
7153 dwo_file,
7154 create_debug_info_hash_table_reader,
7155 &create_dwo_info_table_data);
7156
7157 info_ptr += per_cu.length;
7158 }
7159
7160 return cu_htab;
7161 }
7162
7163 /* Subroutine of open_dwo_file to simplify it.
7164 Open the file specified by FILE_NAME and hand it off to BFD for
7165 preliminary analysis. Return a newly initialized bfd *, which
7166 includes a canonicalized copy of FILE_NAME.
7167 In case of trouble, return NULL.
7168 NOTE: This function is derived from symfile_bfd_open. */
7169
7170 static bfd *
7171 try_open_dwo_file (const char *file_name)
7172 {
7173 bfd *sym_bfd;
7174 int desc;
7175 char *absolute_name;
7176
7177 desc = openp (debug_file_directory, OPF_TRY_CWD_FIRST, file_name,
7178 O_RDONLY | O_BINARY, &absolute_name);
7179 if (desc < 0)
7180 return NULL;
7181
7182 sym_bfd = bfd_fopen (absolute_name, gnutarget, FOPEN_RB, desc);
7183 if (!sym_bfd)
7184 {
7185 xfree (absolute_name);
7186 return NULL;
7187 }
7188 bfd_set_cacheable (sym_bfd, 1);
7189
7190 if (!bfd_check_format (sym_bfd, bfd_object))
7191 {
7192 bfd_close (sym_bfd); /* This also closes desc. */
7193 xfree (absolute_name);
7194 return NULL;
7195 }
7196
7197 /* bfd_usrdata exists for applications and libbfd must not touch it. */
7198 gdb_assert (bfd_usrdata (sym_bfd) == NULL);
7199
7200 return sym_bfd;
7201 }
7202
7203 /* Try to open DWO file DWO_NAME.
7204 COMP_DIR is the DW_AT_comp_dir attribute.
7205 The result is the bfd handle of the file.
7206 If there is a problem finding or opening the file, return NULL.
7207 Upon success, the canonicalized path of the file is stored in the bfd,
7208 same as symfile_bfd_open. */
7209
7210 static bfd *
7211 open_dwo_file (const char *dwo_name, const char *comp_dir)
7212 {
7213 bfd *abfd;
7214
7215 if (IS_ABSOLUTE_PATH (dwo_name))
7216 return try_open_dwo_file (dwo_name);
7217
7218 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
7219
7220 if (comp_dir != NULL)
7221 {
7222 char *path_to_try = concat (comp_dir, SLASH_STRING, dwo_name, NULL);
7223
7224 /* NOTE: If comp_dir is a relative path, this will also try the
7225 search path, which seems useful. */
7226 abfd = try_open_dwo_file (path_to_try);
7227 xfree (path_to_try);
7228 if (abfd != NULL)
7229 return abfd;
7230 }
7231
7232 /* That didn't work, try debug-file-directory, which, despite its name,
7233 is a list of paths. */
7234
7235 if (*debug_file_directory == '\0')
7236 return NULL;
7237
7238 return try_open_dwo_file (dwo_name);
7239 }
7240
7241 /* Initialize the use of the DWO file specified by DWO_NAME. */
7242
7243 static struct dwo_file *
7244 init_dwo_file (const char *dwo_name, const char *comp_dir)
7245 {
7246 struct objfile *objfile = dwarf2_per_objfile->objfile;
7247 struct dwo_file *dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7248 struct dwo_file);
7249 bfd *abfd;
7250 struct cleanup *cleanups;
7251
7252 if (dwarf2_die_debug)
7253 fprintf_unfiltered (gdb_stdlog, "Reading DWO file %s:\n", dwo_name);
7254
7255 abfd = open_dwo_file (dwo_name, comp_dir);
7256 if (abfd == NULL)
7257 return NULL;
7258 dwo_file->dwo_name = dwo_name;
7259 dwo_file->dwo_bfd = abfd;
7260
7261 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
7262
7263 bfd_map_over_sections (abfd, dwarf2_locate_dwo_sections, dwo_file);
7264
7265 dwo_file->cus = create_debug_info_hash_table (dwo_file);
7266
7267 dwo_file->tus = create_debug_types_hash_table (dwo_file,
7268 dwo_file->sections.types);
7269
7270 discard_cleanups (cleanups);
7271
7272 return dwo_file;
7273 }
7274
7275 /* Lookup DWO file DWO_NAME. */
7276
7277 static struct dwo_file *
7278 lookup_dwo_file (char *dwo_name, const char *comp_dir)
7279 {
7280 struct dwo_file *dwo_file;
7281 struct dwo_file find_entry;
7282 void **slot;
7283
7284 if (dwarf2_per_objfile->dwo_files == NULL)
7285 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
7286
7287 /* Have we already seen this DWO file? */
7288 find_entry.dwo_name = dwo_name;
7289 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
7290
7291 /* If not, read it in and build a table of the DWOs it contains. */
7292 if (*slot == NULL)
7293 *slot = init_dwo_file (dwo_name, comp_dir);
7294
7295 /* NOTE: This will be NULL if unable to open the file. */
7296 dwo_file = *slot;
7297
7298 return dwo_file;
7299 }
7300
7301 /* Lookup the DWO CU referenced from THIS_CU in DWO file DWO_NAME.
7302 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
7303 SIGNATURE is the "dwo_id" of the CU (for consistency we use the same
7304 nomenclature as TUs).
7305 The result is a pointer to the dwo_unit object or NULL if we didn't find it
7306 (dwo_id mismatch or couldn't find the DWO file). */
7307
7308 static struct dwo_unit *
7309 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
7310 char *dwo_name, const char *comp_dir,
7311 ULONGEST signature)
7312 {
7313 struct objfile *objfile = dwarf2_per_objfile->objfile;
7314 struct dwo_file *dwo_file;
7315
7316 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
7317 if (dwo_file == NULL)
7318 return NULL;
7319
7320 /* Look up the DWO using its signature(dwo_id). */
7321
7322 if (dwo_file->cus != NULL)
7323 {
7324 struct dwo_unit find_dwo_cu, *dwo_cu;
7325
7326 find_dwo_cu.signature = signature;
7327 dwo_cu = htab_find (dwo_file->cus, &find_dwo_cu);
7328
7329 if (dwo_cu != NULL)
7330 return dwo_cu;
7331 }
7332
7333 /* We didn't find it. This must mean a dwo_id mismatch. */
7334
7335 complaint (&symfile_complaints,
7336 _("Could not find DWO CU referenced by CU at offset 0x%x"
7337 " [in module %s]"),
7338 this_cu->offset.sect_off, objfile->name);
7339 return NULL;
7340 }
7341
7342 /* Lookup the DWO TU referenced from THIS_TU in DWO file DWO_NAME.
7343 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
7344 The result is a pointer to the dwo_unit object or NULL if we didn't find it
7345 (dwo_id mismatch or couldn't find the DWO file). */
7346
7347 static struct dwo_unit *
7348 lookup_dwo_type_unit (struct signatured_type *this_tu,
7349 char *dwo_name, const char *comp_dir)
7350 {
7351 struct objfile *objfile = dwarf2_per_objfile->objfile;
7352 struct dwo_file *dwo_file;
7353
7354 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
7355 if (dwo_file == NULL)
7356 return NULL;
7357
7358 /* Look up the DWO using its signature(dwo_id). */
7359
7360 if (dwo_file->tus != NULL)
7361 {
7362 struct dwo_unit find_dwo_tu, *dwo_tu;
7363
7364 find_dwo_tu.signature = this_tu->signature;
7365 dwo_tu = htab_find (dwo_file->tus, &find_dwo_tu);
7366
7367 if (dwo_tu != NULL)
7368 return dwo_tu;
7369 }
7370
7371 /* We didn't find it. This must mean a dwo_id mismatch. */
7372
7373 complaint (&symfile_complaints,
7374 _("Could not find DWO TU referenced by TU at offset 0x%x"
7375 " [in module %s]"),
7376 this_tu->per_cu.offset.sect_off, objfile->name);
7377 return NULL;
7378 }
7379
7380 /* Free all resources associated with DWO_FILE.
7381 Close the DWO file and munmap the sections.
7382 All memory should be on the objfile obstack. */
7383
7384 static void
7385 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
7386 {
7387 int ix;
7388 struct dwarf2_section_info *section;
7389
7390 gdb_assert (dwo_file->dwo_bfd != objfile->obfd);
7391 bfd_close (dwo_file->dwo_bfd);
7392
7393 munmap_section_buffer (&dwo_file->sections.abbrev);
7394 munmap_section_buffer (&dwo_file->sections.info);
7395 munmap_section_buffer (&dwo_file->sections.line);
7396 munmap_section_buffer (&dwo_file->sections.loc);
7397 munmap_section_buffer (&dwo_file->sections.str);
7398 munmap_section_buffer (&dwo_file->sections.str_offsets);
7399
7400 for (ix = 0;
7401 VEC_iterate (dwarf2_section_info_def, dwo_file->sections.types,
7402 ix, section);
7403 ++ix)
7404 munmap_section_buffer (section);
7405
7406 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
7407 }
7408
7409 /* Wrapper for free_dwo_file for use in cleanups. */
7410
7411 static void
7412 free_dwo_file_cleanup (void *arg)
7413 {
7414 struct dwo_file *dwo_file = (struct dwo_file *) arg;
7415 struct objfile *objfile = dwarf2_per_objfile->objfile;
7416
7417 free_dwo_file (dwo_file, objfile);
7418 }
7419
7420 /* Traversal function for free_dwo_files. */
7421
7422 static int
7423 free_dwo_file_from_slot (void **slot, void *info)
7424 {
7425 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7426 struct objfile *objfile = (struct objfile *) info;
7427
7428 free_dwo_file (dwo_file, objfile);
7429
7430 return 1;
7431 }
7432
7433 /* Free all resources associated with DWO_FILES. */
7434
7435 static void
7436 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
7437 {
7438 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
7439 }
7440 \f
7441 /* Read in various DIEs. */
7442
7443 /* qsort helper for inherit_abstract_dies. */
7444
7445 static int
7446 unsigned_int_compar (const void *ap, const void *bp)
7447 {
7448 unsigned int a = *(unsigned int *) ap;
7449 unsigned int b = *(unsigned int *) bp;
7450
7451 return (a > b) - (b > a);
7452 }
7453
7454 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
7455 Inherit only the children of the DW_AT_abstract_origin DIE not being
7456 already referenced by DW_AT_abstract_origin from the children of the
7457 current DIE. */
7458
7459 static void
7460 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
7461 {
7462 struct die_info *child_die;
7463 unsigned die_children_count;
7464 /* CU offsets which were referenced by children of the current DIE. */
7465 sect_offset *offsets;
7466 sect_offset *offsets_end, *offsetp;
7467 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
7468 struct die_info *origin_die;
7469 /* Iterator of the ORIGIN_DIE children. */
7470 struct die_info *origin_child_die;
7471 struct cleanup *cleanups;
7472 struct attribute *attr;
7473 struct dwarf2_cu *origin_cu;
7474 struct pending **origin_previous_list_in_scope;
7475
7476 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
7477 if (!attr)
7478 return;
7479
7480 /* Note that following die references may follow to a die in a
7481 different cu. */
7482
7483 origin_cu = cu;
7484 origin_die = follow_die_ref (die, attr, &origin_cu);
7485
7486 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
7487 symbols in. */
7488 origin_previous_list_in_scope = origin_cu->list_in_scope;
7489 origin_cu->list_in_scope = cu->list_in_scope;
7490
7491 if (die->tag != origin_die->tag
7492 && !(die->tag == DW_TAG_inlined_subroutine
7493 && origin_die->tag == DW_TAG_subprogram))
7494 complaint (&symfile_complaints,
7495 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
7496 die->offset.sect_off, origin_die->offset.sect_off);
7497
7498 child_die = die->child;
7499 die_children_count = 0;
7500 while (child_die && child_die->tag)
7501 {
7502 child_die = sibling_die (child_die);
7503 die_children_count++;
7504 }
7505 offsets = xmalloc (sizeof (*offsets) * die_children_count);
7506 cleanups = make_cleanup (xfree, offsets);
7507
7508 offsets_end = offsets;
7509 child_die = die->child;
7510 while (child_die && child_die->tag)
7511 {
7512 /* For each CHILD_DIE, find the corresponding child of
7513 ORIGIN_DIE. If there is more than one layer of
7514 DW_AT_abstract_origin, follow them all; there shouldn't be,
7515 but GCC versions at least through 4.4 generate this (GCC PR
7516 40573). */
7517 struct die_info *child_origin_die = child_die;
7518 struct dwarf2_cu *child_origin_cu = cu;
7519
7520 while (1)
7521 {
7522 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
7523 child_origin_cu);
7524 if (attr == NULL)
7525 break;
7526 child_origin_die = follow_die_ref (child_origin_die, attr,
7527 &child_origin_cu);
7528 }
7529
7530 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
7531 counterpart may exist. */
7532 if (child_origin_die != child_die)
7533 {
7534 if (child_die->tag != child_origin_die->tag
7535 && !(child_die->tag == DW_TAG_inlined_subroutine
7536 && child_origin_die->tag == DW_TAG_subprogram))
7537 complaint (&symfile_complaints,
7538 _("Child DIE 0x%x and its abstract origin 0x%x have "
7539 "different tags"), child_die->offset.sect_off,
7540 child_origin_die->offset.sect_off);
7541 if (child_origin_die->parent != origin_die)
7542 complaint (&symfile_complaints,
7543 _("Child DIE 0x%x and its abstract origin 0x%x have "
7544 "different parents"), child_die->offset.sect_off,
7545 child_origin_die->offset.sect_off);
7546 else
7547 *offsets_end++ = child_origin_die->offset;
7548 }
7549 child_die = sibling_die (child_die);
7550 }
7551 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
7552 unsigned_int_compar);
7553 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
7554 if (offsetp[-1].sect_off == offsetp->sect_off)
7555 complaint (&symfile_complaints,
7556 _("Multiple children of DIE 0x%x refer "
7557 "to DIE 0x%x as their abstract origin"),
7558 die->offset.sect_off, offsetp->sect_off);
7559
7560 offsetp = offsets;
7561 origin_child_die = origin_die->child;
7562 while (origin_child_die && origin_child_die->tag)
7563 {
7564 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
7565 while (offsetp < offsets_end
7566 && offsetp->sect_off < origin_child_die->offset.sect_off)
7567 offsetp++;
7568 if (offsetp >= offsets_end
7569 || offsetp->sect_off > origin_child_die->offset.sect_off)
7570 {
7571 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
7572 process_die (origin_child_die, origin_cu);
7573 }
7574 origin_child_die = sibling_die (origin_child_die);
7575 }
7576 origin_cu->list_in_scope = origin_previous_list_in_scope;
7577
7578 do_cleanups (cleanups);
7579 }
7580
7581 static void
7582 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
7583 {
7584 struct objfile *objfile = cu->objfile;
7585 struct context_stack *new;
7586 CORE_ADDR lowpc;
7587 CORE_ADDR highpc;
7588 struct die_info *child_die;
7589 struct attribute *attr, *call_line, *call_file;
7590 char *name;
7591 CORE_ADDR baseaddr;
7592 struct block *block;
7593 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
7594 VEC (symbolp) *template_args = NULL;
7595 struct template_symbol *templ_func = NULL;
7596
7597 if (inlined_func)
7598 {
7599 /* If we do not have call site information, we can't show the
7600 caller of this inlined function. That's too confusing, so
7601 only use the scope for local variables. */
7602 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
7603 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
7604 if (call_line == NULL || call_file == NULL)
7605 {
7606 read_lexical_block_scope (die, cu);
7607 return;
7608 }
7609 }
7610
7611 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7612
7613 name = dwarf2_name (die, cu);
7614
7615 /* Ignore functions with missing or empty names. These are actually
7616 illegal according to the DWARF standard. */
7617 if (name == NULL)
7618 {
7619 complaint (&symfile_complaints,
7620 _("missing name for subprogram DIE at %d"),
7621 die->offset.sect_off);
7622 return;
7623 }
7624
7625 /* Ignore functions with missing or invalid low and high pc attributes. */
7626 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
7627 {
7628 attr = dwarf2_attr (die, DW_AT_external, cu);
7629 if (!attr || !DW_UNSND (attr))
7630 complaint (&symfile_complaints,
7631 _("cannot get low and high bounds "
7632 "for subprogram DIE at %d"),
7633 die->offset.sect_off);
7634 return;
7635 }
7636
7637 lowpc += baseaddr;
7638 highpc += baseaddr;
7639
7640 /* If we have any template arguments, then we must allocate a
7641 different sort of symbol. */
7642 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
7643 {
7644 if (child_die->tag == DW_TAG_template_type_param
7645 || child_die->tag == DW_TAG_template_value_param)
7646 {
7647 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7648 struct template_symbol);
7649 templ_func->base.is_cplus_template_function = 1;
7650 break;
7651 }
7652 }
7653
7654 new = push_context (0, lowpc);
7655 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
7656 (struct symbol *) templ_func);
7657
7658 /* If there is a location expression for DW_AT_frame_base, record
7659 it. */
7660 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
7661 if (attr)
7662 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
7663 expression is being recorded directly in the function's symbol
7664 and not in a separate frame-base object. I guess this hack is
7665 to avoid adding some sort of frame-base adjunct/annex to the
7666 function's symbol :-(. The problem with doing this is that it
7667 results in a function symbol with a location expression that
7668 has nothing to do with the location of the function, ouch! The
7669 relationship should be: a function's symbol has-a frame base; a
7670 frame-base has-a location expression. */
7671 dwarf2_symbol_mark_computed (attr, new->name, cu);
7672
7673 cu->list_in_scope = &local_symbols;
7674
7675 if (die->child != NULL)
7676 {
7677 child_die = die->child;
7678 while (child_die && child_die->tag)
7679 {
7680 if (child_die->tag == DW_TAG_template_type_param
7681 || child_die->tag == DW_TAG_template_value_param)
7682 {
7683 struct symbol *arg = new_symbol (child_die, NULL, cu);
7684
7685 if (arg != NULL)
7686 VEC_safe_push (symbolp, template_args, arg);
7687 }
7688 else
7689 process_die (child_die, cu);
7690 child_die = sibling_die (child_die);
7691 }
7692 }
7693
7694 inherit_abstract_dies (die, cu);
7695
7696 /* If we have a DW_AT_specification, we might need to import using
7697 directives from the context of the specification DIE. See the
7698 comment in determine_prefix. */
7699 if (cu->language == language_cplus
7700 && dwarf2_attr (die, DW_AT_specification, cu))
7701 {
7702 struct dwarf2_cu *spec_cu = cu;
7703 struct die_info *spec_die = die_specification (die, &spec_cu);
7704
7705 while (spec_die)
7706 {
7707 child_die = spec_die->child;
7708 while (child_die && child_die->tag)
7709 {
7710 if (child_die->tag == DW_TAG_imported_module)
7711 process_die (child_die, spec_cu);
7712 child_die = sibling_die (child_die);
7713 }
7714
7715 /* In some cases, GCC generates specification DIEs that
7716 themselves contain DW_AT_specification attributes. */
7717 spec_die = die_specification (spec_die, &spec_cu);
7718 }
7719 }
7720
7721 new = pop_context ();
7722 /* Make a block for the local symbols within. */
7723 block = finish_block (new->name, &local_symbols, new->old_blocks,
7724 lowpc, highpc, objfile);
7725
7726 /* For C++, set the block's scope. */
7727 if (cu->language == language_cplus || cu->language == language_fortran)
7728 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
7729 determine_prefix (die, cu),
7730 processing_has_namespace_info);
7731
7732 /* If we have address ranges, record them. */
7733 dwarf2_record_block_ranges (die, block, baseaddr, cu);
7734
7735 /* Attach template arguments to function. */
7736 if (! VEC_empty (symbolp, template_args))
7737 {
7738 gdb_assert (templ_func != NULL);
7739
7740 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
7741 templ_func->template_arguments
7742 = obstack_alloc (&objfile->objfile_obstack,
7743 (templ_func->n_template_arguments
7744 * sizeof (struct symbol *)));
7745 memcpy (templ_func->template_arguments,
7746 VEC_address (symbolp, template_args),
7747 (templ_func->n_template_arguments * sizeof (struct symbol *)));
7748 VEC_free (symbolp, template_args);
7749 }
7750
7751 /* In C++, we can have functions nested inside functions (e.g., when
7752 a function declares a class that has methods). This means that
7753 when we finish processing a function scope, we may need to go
7754 back to building a containing block's symbol lists. */
7755 local_symbols = new->locals;
7756 param_symbols = new->params;
7757 using_directives = new->using_directives;
7758
7759 /* If we've finished processing a top-level function, subsequent
7760 symbols go in the file symbol list. */
7761 if (outermost_context_p ())
7762 cu->list_in_scope = &file_symbols;
7763 }
7764
7765 /* Process all the DIES contained within a lexical block scope. Start
7766 a new scope, process the dies, and then close the scope. */
7767
7768 static void
7769 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
7770 {
7771 struct objfile *objfile = cu->objfile;
7772 struct context_stack *new;
7773 CORE_ADDR lowpc, highpc;
7774 struct die_info *child_die;
7775 CORE_ADDR baseaddr;
7776
7777 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7778
7779 /* Ignore blocks with missing or invalid low and high pc attributes. */
7780 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
7781 as multiple lexical blocks? Handling children in a sane way would
7782 be nasty. Might be easier to properly extend generic blocks to
7783 describe ranges. */
7784 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
7785 return;
7786 lowpc += baseaddr;
7787 highpc += baseaddr;
7788
7789 push_context (0, lowpc);
7790 if (die->child != NULL)
7791 {
7792 child_die = die->child;
7793 while (child_die && child_die->tag)
7794 {
7795 process_die (child_die, cu);
7796 child_die = sibling_die (child_die);
7797 }
7798 }
7799 new = pop_context ();
7800
7801 if (local_symbols != NULL || using_directives != NULL)
7802 {
7803 struct block *block
7804 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
7805 highpc, objfile);
7806
7807 /* Note that recording ranges after traversing children, as we
7808 do here, means that recording a parent's ranges entails
7809 walking across all its children's ranges as they appear in
7810 the address map, which is quadratic behavior.
7811
7812 It would be nicer to record the parent's ranges before
7813 traversing its children, simply overriding whatever you find
7814 there. But since we don't even decide whether to create a
7815 block until after we've traversed its children, that's hard
7816 to do. */
7817 dwarf2_record_block_ranges (die, block, baseaddr, cu);
7818 }
7819 local_symbols = new->locals;
7820 using_directives = new->using_directives;
7821 }
7822
7823 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
7824
7825 static void
7826 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
7827 {
7828 struct objfile *objfile = cu->objfile;
7829 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7830 CORE_ADDR pc, baseaddr;
7831 struct attribute *attr;
7832 struct call_site *call_site, call_site_local;
7833 void **slot;
7834 int nparams;
7835 struct die_info *child_die;
7836
7837 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7838
7839 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
7840 if (!attr)
7841 {
7842 complaint (&symfile_complaints,
7843 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
7844 "DIE 0x%x [in module %s]"),
7845 die->offset.sect_off, objfile->name);
7846 return;
7847 }
7848 pc = DW_ADDR (attr) + baseaddr;
7849
7850 if (cu->call_site_htab == NULL)
7851 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
7852 NULL, &objfile->objfile_obstack,
7853 hashtab_obstack_allocate, NULL);
7854 call_site_local.pc = pc;
7855 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
7856 if (*slot != NULL)
7857 {
7858 complaint (&symfile_complaints,
7859 _("Duplicate PC %s for DW_TAG_GNU_call_site "
7860 "DIE 0x%x [in module %s]"),
7861 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
7862 return;
7863 }
7864
7865 /* Count parameters at the caller. */
7866
7867 nparams = 0;
7868 for (child_die = die->child; child_die && child_die->tag;
7869 child_die = sibling_die (child_die))
7870 {
7871 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
7872 {
7873 complaint (&symfile_complaints,
7874 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
7875 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
7876 child_die->tag, child_die->offset.sect_off, objfile->name);
7877 continue;
7878 }
7879
7880 nparams++;
7881 }
7882
7883 call_site = obstack_alloc (&objfile->objfile_obstack,
7884 (sizeof (*call_site)
7885 + (sizeof (*call_site->parameter)
7886 * (nparams - 1))));
7887 *slot = call_site;
7888 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
7889 call_site->pc = pc;
7890
7891 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
7892 {
7893 struct die_info *func_die;
7894
7895 /* Skip also over DW_TAG_inlined_subroutine. */
7896 for (func_die = die->parent;
7897 func_die && func_die->tag != DW_TAG_subprogram
7898 && func_die->tag != DW_TAG_subroutine_type;
7899 func_die = func_die->parent);
7900
7901 /* DW_AT_GNU_all_call_sites is a superset
7902 of DW_AT_GNU_all_tail_call_sites. */
7903 if (func_die
7904 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
7905 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
7906 {
7907 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
7908 not complete. But keep CALL_SITE for look ups via call_site_htab,
7909 both the initial caller containing the real return address PC and
7910 the final callee containing the current PC of a chain of tail
7911 calls do not need to have the tail call list complete. But any
7912 function candidate for a virtual tail call frame searched via
7913 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
7914 determined unambiguously. */
7915 }
7916 else
7917 {
7918 struct type *func_type = NULL;
7919
7920 if (func_die)
7921 func_type = get_die_type (func_die, cu);
7922 if (func_type != NULL)
7923 {
7924 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
7925
7926 /* Enlist this call site to the function. */
7927 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
7928 TYPE_TAIL_CALL_LIST (func_type) = call_site;
7929 }
7930 else
7931 complaint (&symfile_complaints,
7932 _("Cannot find function owning DW_TAG_GNU_call_site "
7933 "DIE 0x%x [in module %s]"),
7934 die->offset.sect_off, objfile->name);
7935 }
7936 }
7937
7938 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
7939 if (attr == NULL)
7940 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
7941 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
7942 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
7943 /* Keep NULL DWARF_BLOCK. */;
7944 else if (attr_form_is_block (attr))
7945 {
7946 struct dwarf2_locexpr_baton *dlbaton;
7947
7948 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
7949 dlbaton->data = DW_BLOCK (attr)->data;
7950 dlbaton->size = DW_BLOCK (attr)->size;
7951 dlbaton->per_cu = cu->per_cu;
7952
7953 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
7954 }
7955 else if (is_ref_attr (attr))
7956 {
7957 struct dwarf2_cu *target_cu = cu;
7958 struct die_info *target_die;
7959
7960 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
7961 gdb_assert (target_cu->objfile == objfile);
7962 if (die_is_declaration (target_die, target_cu))
7963 {
7964 const char *target_physname;
7965
7966 target_physname = dwarf2_physname (NULL, target_die, target_cu);
7967 if (target_physname == NULL)
7968 complaint (&symfile_complaints,
7969 _("DW_AT_GNU_call_site_target target DIE has invalid "
7970 "physname, for referencing DIE 0x%x [in module %s]"),
7971 die->offset.sect_off, objfile->name);
7972 else
7973 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
7974 }
7975 else
7976 {
7977 CORE_ADDR lowpc;
7978
7979 /* DW_AT_entry_pc should be preferred. */
7980 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
7981 complaint (&symfile_complaints,
7982 _("DW_AT_GNU_call_site_target target DIE has invalid "
7983 "low pc, for referencing DIE 0x%x [in module %s]"),
7984 die->offset.sect_off, objfile->name);
7985 else
7986 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
7987 }
7988 }
7989 else
7990 complaint (&symfile_complaints,
7991 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
7992 "block nor reference, for DIE 0x%x [in module %s]"),
7993 die->offset.sect_off, objfile->name);
7994
7995 call_site->per_cu = cu->per_cu;
7996
7997 for (child_die = die->child;
7998 child_die && child_die->tag;
7999 child_die = sibling_die (child_die))
8000 {
8001 struct call_site_parameter *parameter;
8002 struct attribute *loc, *origin;
8003
8004 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
8005 {
8006 /* Already printed the complaint above. */
8007 continue;
8008 }
8009
8010 gdb_assert (call_site->parameter_count < nparams);
8011 parameter = &call_site->parameter[call_site->parameter_count];
8012
8013 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
8014 specifies DW_TAG_formal_parameter. Value of the data assumed for the
8015 register is contained in DW_AT_GNU_call_site_value. */
8016
8017 loc = dwarf2_attr (child_die, DW_AT_location, cu);
8018 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
8019 if (loc == NULL && origin != NULL && is_ref_attr (origin))
8020 {
8021 sect_offset offset;
8022
8023 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
8024 offset = dwarf2_get_ref_die_offset (origin);
8025 gdb_assert (offset.sect_off >= cu->header.offset.sect_off);
8026 parameter->u.param_offset.cu_off = (offset.sect_off
8027 - cu->header.offset.sect_off);
8028 }
8029 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
8030 {
8031 complaint (&symfile_complaints,
8032 _("No DW_FORM_block* DW_AT_location for "
8033 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
8034 child_die->offset.sect_off, objfile->name);
8035 continue;
8036 }
8037 else
8038 {
8039 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
8040 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
8041 if (parameter->u.dwarf_reg != -1)
8042 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
8043 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
8044 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
8045 &parameter->u.fb_offset))
8046 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
8047 else
8048 {
8049 complaint (&symfile_complaints,
8050 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
8051 "for DW_FORM_block* DW_AT_location is supported for "
8052 "DW_TAG_GNU_call_site child DIE 0x%x "
8053 "[in module %s]"),
8054 child_die->offset.sect_off, objfile->name);
8055 continue;
8056 }
8057 }
8058
8059 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
8060 if (!attr_form_is_block (attr))
8061 {
8062 complaint (&symfile_complaints,
8063 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
8064 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
8065 child_die->offset.sect_off, objfile->name);
8066 continue;
8067 }
8068 parameter->value = DW_BLOCK (attr)->data;
8069 parameter->value_size = DW_BLOCK (attr)->size;
8070
8071 /* Parameters are not pre-cleared by memset above. */
8072 parameter->data_value = NULL;
8073 parameter->data_value_size = 0;
8074 call_site->parameter_count++;
8075
8076 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
8077 if (attr)
8078 {
8079 if (!attr_form_is_block (attr))
8080 complaint (&symfile_complaints,
8081 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
8082 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
8083 child_die->offset.sect_off, objfile->name);
8084 else
8085 {
8086 parameter->data_value = DW_BLOCK (attr)->data;
8087 parameter->data_value_size = DW_BLOCK (attr)->size;
8088 }
8089 }
8090 }
8091 }
8092
8093 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
8094 Return 1 if the attributes are present and valid, otherwise, return 0.
8095 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
8096
8097 static int
8098 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
8099 CORE_ADDR *high_return, struct dwarf2_cu *cu,
8100 struct partial_symtab *ranges_pst)
8101 {
8102 struct objfile *objfile = cu->objfile;
8103 struct comp_unit_head *cu_header = &cu->header;
8104 bfd *obfd = objfile->obfd;
8105 unsigned int addr_size = cu_header->addr_size;
8106 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
8107 /* Base address selection entry. */
8108 CORE_ADDR base;
8109 int found_base;
8110 unsigned int dummy;
8111 gdb_byte *buffer;
8112 CORE_ADDR marker;
8113 int low_set;
8114 CORE_ADDR low = 0;
8115 CORE_ADDR high = 0;
8116 CORE_ADDR baseaddr;
8117
8118 found_base = cu->base_known;
8119 base = cu->base_address;
8120
8121 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
8122 if (offset >= dwarf2_per_objfile->ranges.size)
8123 {
8124 complaint (&symfile_complaints,
8125 _("Offset %d out of bounds for DW_AT_ranges attribute"),
8126 offset);
8127 return 0;
8128 }
8129 buffer = dwarf2_per_objfile->ranges.buffer + offset;
8130
8131 /* Read in the largest possible address. */
8132 marker = read_address (obfd, buffer, cu, &dummy);
8133 if ((marker & mask) == mask)
8134 {
8135 /* If we found the largest possible address, then
8136 read the base address. */
8137 base = read_address (obfd, buffer + addr_size, cu, &dummy);
8138 buffer += 2 * addr_size;
8139 offset += 2 * addr_size;
8140 found_base = 1;
8141 }
8142
8143 low_set = 0;
8144
8145 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8146
8147 while (1)
8148 {
8149 CORE_ADDR range_beginning, range_end;
8150
8151 range_beginning = read_address (obfd, buffer, cu, &dummy);
8152 buffer += addr_size;
8153 range_end = read_address (obfd, buffer, cu, &dummy);
8154 buffer += addr_size;
8155 offset += 2 * addr_size;
8156
8157 /* An end of list marker is a pair of zero addresses. */
8158 if (range_beginning == 0 && range_end == 0)
8159 /* Found the end of list entry. */
8160 break;
8161
8162 /* Each base address selection entry is a pair of 2 values.
8163 The first is the largest possible address, the second is
8164 the base address. Check for a base address here. */
8165 if ((range_beginning & mask) == mask)
8166 {
8167 /* If we found the largest possible address, then
8168 read the base address. */
8169 base = read_address (obfd, buffer + addr_size, cu, &dummy);
8170 found_base = 1;
8171 continue;
8172 }
8173
8174 if (!found_base)
8175 {
8176 /* We have no valid base address for the ranges
8177 data. */
8178 complaint (&symfile_complaints,
8179 _("Invalid .debug_ranges data (no base address)"));
8180 return 0;
8181 }
8182
8183 if (range_beginning > range_end)
8184 {
8185 /* Inverted range entries are invalid. */
8186 complaint (&symfile_complaints,
8187 _("Invalid .debug_ranges data (inverted range)"));
8188 return 0;
8189 }
8190
8191 /* Empty range entries have no effect. */
8192 if (range_beginning == range_end)
8193 continue;
8194
8195 range_beginning += base;
8196 range_end += base;
8197
8198 if (ranges_pst != NULL)
8199 addrmap_set_empty (objfile->psymtabs_addrmap,
8200 range_beginning + baseaddr,
8201 range_end - 1 + baseaddr,
8202 ranges_pst);
8203
8204 /* FIXME: This is recording everything as a low-high
8205 segment of consecutive addresses. We should have a
8206 data structure for discontiguous block ranges
8207 instead. */
8208 if (! low_set)
8209 {
8210 low = range_beginning;
8211 high = range_end;
8212 low_set = 1;
8213 }
8214 else
8215 {
8216 if (range_beginning < low)
8217 low = range_beginning;
8218 if (range_end > high)
8219 high = range_end;
8220 }
8221 }
8222
8223 if (! low_set)
8224 /* If the first entry is an end-of-list marker, the range
8225 describes an empty scope, i.e. no instructions. */
8226 return 0;
8227
8228 if (low_return)
8229 *low_return = low;
8230 if (high_return)
8231 *high_return = high;
8232 return 1;
8233 }
8234
8235 /* Get low and high pc attributes from a die. Return 1 if the attributes
8236 are present and valid, otherwise, return 0. Return -1 if the range is
8237 discontinuous, i.e. derived from DW_AT_ranges information. */
8238
8239 static int
8240 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
8241 CORE_ADDR *highpc, struct dwarf2_cu *cu,
8242 struct partial_symtab *pst)
8243 {
8244 struct attribute *attr;
8245 struct attribute *attr_high;
8246 CORE_ADDR low = 0;
8247 CORE_ADDR high = 0;
8248 int ret = 0;
8249
8250 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
8251 if (attr_high)
8252 {
8253 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
8254 if (attr)
8255 {
8256 low = DW_ADDR (attr);
8257 if (attr_high->form == DW_FORM_addr
8258 || attr_high->form == DW_FORM_GNU_addr_index)
8259 high = DW_ADDR (attr_high);
8260 else
8261 high = low + DW_UNSND (attr_high);
8262 }
8263 else
8264 /* Found high w/o low attribute. */
8265 return 0;
8266
8267 /* Found consecutive range of addresses. */
8268 ret = 1;
8269 }
8270 else
8271 {
8272 attr = dwarf2_attr (die, DW_AT_ranges, cu);
8273 if (attr != NULL)
8274 {
8275 unsigned int ranges_offset = DW_UNSND (attr) + cu->ranges_base;
8276
8277 /* Value of the DW_AT_ranges attribute is the offset in the
8278 .debug_ranges section. */
8279 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
8280 return 0;
8281 /* Found discontinuous range of addresses. */
8282 ret = -1;
8283 }
8284 }
8285
8286 /* read_partial_die has also the strict LOW < HIGH requirement. */
8287 if (high <= low)
8288 return 0;
8289
8290 /* When using the GNU linker, .gnu.linkonce. sections are used to
8291 eliminate duplicate copies of functions and vtables and such.
8292 The linker will arbitrarily choose one and discard the others.
8293 The AT_*_pc values for such functions refer to local labels in
8294 these sections. If the section from that file was discarded, the
8295 labels are not in the output, so the relocs get a value of 0.
8296 If this is a discarded function, mark the pc bounds as invalid,
8297 so that GDB will ignore it. */
8298 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
8299 return 0;
8300
8301 *lowpc = low;
8302 if (highpc)
8303 *highpc = high;
8304 return ret;
8305 }
8306
8307 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
8308 its low and high PC addresses. Do nothing if these addresses could not
8309 be determined. Otherwise, set LOWPC to the low address if it is smaller,
8310 and HIGHPC to the high address if greater than HIGHPC. */
8311
8312 static void
8313 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
8314 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8315 struct dwarf2_cu *cu)
8316 {
8317 CORE_ADDR low, high;
8318 struct die_info *child = die->child;
8319
8320 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
8321 {
8322 *lowpc = min (*lowpc, low);
8323 *highpc = max (*highpc, high);
8324 }
8325
8326 /* If the language does not allow nested subprograms (either inside
8327 subprograms or lexical blocks), we're done. */
8328 if (cu->language != language_ada)
8329 return;
8330
8331 /* Check all the children of the given DIE. If it contains nested
8332 subprograms, then check their pc bounds. Likewise, we need to
8333 check lexical blocks as well, as they may also contain subprogram
8334 definitions. */
8335 while (child && child->tag)
8336 {
8337 if (child->tag == DW_TAG_subprogram
8338 || child->tag == DW_TAG_lexical_block)
8339 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
8340 child = sibling_die (child);
8341 }
8342 }
8343
8344 /* Get the low and high pc's represented by the scope DIE, and store
8345 them in *LOWPC and *HIGHPC. If the correct values can't be
8346 determined, set *LOWPC to -1 and *HIGHPC to 0. */
8347
8348 static void
8349 get_scope_pc_bounds (struct die_info *die,
8350 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8351 struct dwarf2_cu *cu)
8352 {
8353 CORE_ADDR best_low = (CORE_ADDR) -1;
8354 CORE_ADDR best_high = (CORE_ADDR) 0;
8355 CORE_ADDR current_low, current_high;
8356
8357 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
8358 {
8359 best_low = current_low;
8360 best_high = current_high;
8361 }
8362 else
8363 {
8364 struct die_info *child = die->child;
8365
8366 while (child && child->tag)
8367 {
8368 switch (child->tag) {
8369 case DW_TAG_subprogram:
8370 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
8371 break;
8372 case DW_TAG_namespace:
8373 case DW_TAG_module:
8374 /* FIXME: carlton/2004-01-16: Should we do this for
8375 DW_TAG_class_type/DW_TAG_structure_type, too? I think
8376 that current GCC's always emit the DIEs corresponding
8377 to definitions of methods of classes as children of a
8378 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
8379 the DIEs giving the declarations, which could be
8380 anywhere). But I don't see any reason why the
8381 standards says that they have to be there. */
8382 get_scope_pc_bounds (child, &current_low, &current_high, cu);
8383
8384 if (current_low != ((CORE_ADDR) -1))
8385 {
8386 best_low = min (best_low, current_low);
8387 best_high = max (best_high, current_high);
8388 }
8389 break;
8390 default:
8391 /* Ignore. */
8392 break;
8393 }
8394
8395 child = sibling_die (child);
8396 }
8397 }
8398
8399 *lowpc = best_low;
8400 *highpc = best_high;
8401 }
8402
8403 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
8404 in DIE. */
8405
8406 static void
8407 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
8408 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
8409 {
8410 struct objfile *objfile = cu->objfile;
8411 struct attribute *attr;
8412 struct attribute *attr_high;
8413
8414 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
8415 if (attr_high)
8416 {
8417 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
8418 if (attr)
8419 {
8420 CORE_ADDR low = DW_ADDR (attr);
8421 CORE_ADDR high;
8422 if (attr_high->form == DW_FORM_addr
8423 || attr_high->form == DW_FORM_GNU_addr_index)
8424 high = DW_ADDR (attr_high);
8425 else
8426 high = low + DW_UNSND (attr_high);
8427
8428 record_block_range (block, baseaddr + low, baseaddr + high - 1);
8429 }
8430 }
8431
8432 attr = dwarf2_attr (die, DW_AT_ranges, cu);
8433 if (attr)
8434 {
8435 bfd *obfd = objfile->obfd;
8436
8437 /* The value of the DW_AT_ranges attribute is the offset of the
8438 address range list in the .debug_ranges section. */
8439 unsigned long offset = DW_UNSND (attr) + cu->ranges_base;
8440 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
8441
8442 /* For some target architectures, but not others, the
8443 read_address function sign-extends the addresses it returns.
8444 To recognize base address selection entries, we need a
8445 mask. */
8446 unsigned int addr_size = cu->header.addr_size;
8447 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
8448
8449 /* The base address, to which the next pair is relative. Note
8450 that this 'base' is a DWARF concept: most entries in a range
8451 list are relative, to reduce the number of relocs against the
8452 debugging information. This is separate from this function's
8453 'baseaddr' argument, which GDB uses to relocate debugging
8454 information from a shared library based on the address at
8455 which the library was loaded. */
8456 CORE_ADDR base = cu->base_address;
8457 int base_known = cu->base_known;
8458
8459 gdb_assert (dwarf2_per_objfile->ranges.readin);
8460 if (offset >= dwarf2_per_objfile->ranges.size)
8461 {
8462 complaint (&symfile_complaints,
8463 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
8464 offset);
8465 return;
8466 }
8467
8468 for (;;)
8469 {
8470 unsigned int bytes_read;
8471 CORE_ADDR start, end;
8472
8473 start = read_address (obfd, buffer, cu, &bytes_read);
8474 buffer += bytes_read;
8475 end = read_address (obfd, buffer, cu, &bytes_read);
8476 buffer += bytes_read;
8477
8478 /* Did we find the end of the range list? */
8479 if (start == 0 && end == 0)
8480 break;
8481
8482 /* Did we find a base address selection entry? */
8483 else if ((start & base_select_mask) == base_select_mask)
8484 {
8485 base = end;
8486 base_known = 1;
8487 }
8488
8489 /* We found an ordinary address range. */
8490 else
8491 {
8492 if (!base_known)
8493 {
8494 complaint (&symfile_complaints,
8495 _("Invalid .debug_ranges data "
8496 "(no base address)"));
8497 return;
8498 }
8499
8500 if (start > end)
8501 {
8502 /* Inverted range entries are invalid. */
8503 complaint (&symfile_complaints,
8504 _("Invalid .debug_ranges data "
8505 "(inverted range)"));
8506 return;
8507 }
8508
8509 /* Empty range entries have no effect. */
8510 if (start == end)
8511 continue;
8512
8513 record_block_range (block,
8514 baseaddr + base + start,
8515 baseaddr + base + end - 1);
8516 }
8517 }
8518 }
8519 }
8520
8521 /* Check whether the producer field indicates either of GCC < 4.6, or the
8522 Intel C/C++ compiler, and cache the result in CU. */
8523
8524 static void
8525 check_producer (struct dwarf2_cu *cu)
8526 {
8527 const char *cs;
8528 int major, minor, release;
8529
8530 if (cu->producer == NULL)
8531 {
8532 /* For unknown compilers expect their behavior is DWARF version
8533 compliant.
8534
8535 GCC started to support .debug_types sections by -gdwarf-4 since
8536 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
8537 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
8538 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
8539 interpreted incorrectly by GDB now - GCC PR debug/48229. */
8540 }
8541 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
8542 {
8543 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
8544
8545 cs = &cu->producer[strlen ("GNU ")];
8546 while (*cs && !isdigit (*cs))
8547 cs++;
8548 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
8549 {
8550 /* Not recognized as GCC. */
8551 }
8552 else
8553 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
8554 }
8555 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
8556 cu->producer_is_icc = 1;
8557 else
8558 {
8559 /* For other non-GCC compilers, expect their behavior is DWARF version
8560 compliant. */
8561 }
8562
8563 cu->checked_producer = 1;
8564 }
8565
8566 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
8567 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
8568 during 4.6.0 experimental. */
8569
8570 static int
8571 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
8572 {
8573 if (!cu->checked_producer)
8574 check_producer (cu);
8575
8576 return cu->producer_is_gxx_lt_4_6;
8577 }
8578
8579 /* Return the default accessibility type if it is not overriden by
8580 DW_AT_accessibility. */
8581
8582 static enum dwarf_access_attribute
8583 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
8584 {
8585 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
8586 {
8587 /* The default DWARF 2 accessibility for members is public, the default
8588 accessibility for inheritance is private. */
8589
8590 if (die->tag != DW_TAG_inheritance)
8591 return DW_ACCESS_public;
8592 else
8593 return DW_ACCESS_private;
8594 }
8595 else
8596 {
8597 /* DWARF 3+ defines the default accessibility a different way. The same
8598 rules apply now for DW_TAG_inheritance as for the members and it only
8599 depends on the container kind. */
8600
8601 if (die->parent->tag == DW_TAG_class_type)
8602 return DW_ACCESS_private;
8603 else
8604 return DW_ACCESS_public;
8605 }
8606 }
8607
8608 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
8609 offset. If the attribute was not found return 0, otherwise return
8610 1. If it was found but could not properly be handled, set *OFFSET
8611 to 0. */
8612
8613 static int
8614 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
8615 LONGEST *offset)
8616 {
8617 struct attribute *attr;
8618
8619 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
8620 if (attr != NULL)
8621 {
8622 *offset = 0;
8623
8624 /* Note that we do not check for a section offset first here.
8625 This is because DW_AT_data_member_location is new in DWARF 4,
8626 so if we see it, we can assume that a constant form is really
8627 a constant and not a section offset. */
8628 if (attr_form_is_constant (attr))
8629 *offset = dwarf2_get_attr_constant_value (attr, 0);
8630 else if (attr_form_is_section_offset (attr))
8631 dwarf2_complex_location_expr_complaint ();
8632 else if (attr_form_is_block (attr))
8633 *offset = decode_locdesc (DW_BLOCK (attr), cu);
8634 else
8635 dwarf2_complex_location_expr_complaint ();
8636
8637 return 1;
8638 }
8639
8640 return 0;
8641 }
8642
8643 /* Add an aggregate field to the field list. */
8644
8645 static void
8646 dwarf2_add_field (struct field_info *fip, struct die_info *die,
8647 struct dwarf2_cu *cu)
8648 {
8649 struct objfile *objfile = cu->objfile;
8650 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8651 struct nextfield *new_field;
8652 struct attribute *attr;
8653 struct field *fp;
8654 char *fieldname = "";
8655
8656 /* Allocate a new field list entry and link it in. */
8657 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
8658 make_cleanup (xfree, new_field);
8659 memset (new_field, 0, sizeof (struct nextfield));
8660
8661 if (die->tag == DW_TAG_inheritance)
8662 {
8663 new_field->next = fip->baseclasses;
8664 fip->baseclasses = new_field;
8665 }
8666 else
8667 {
8668 new_field->next = fip->fields;
8669 fip->fields = new_field;
8670 }
8671 fip->nfields++;
8672
8673 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
8674 if (attr)
8675 new_field->accessibility = DW_UNSND (attr);
8676 else
8677 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
8678 if (new_field->accessibility != DW_ACCESS_public)
8679 fip->non_public_fields = 1;
8680
8681 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
8682 if (attr)
8683 new_field->virtuality = DW_UNSND (attr);
8684 else
8685 new_field->virtuality = DW_VIRTUALITY_none;
8686
8687 fp = &new_field->field;
8688
8689 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
8690 {
8691 LONGEST offset;
8692
8693 /* Data member other than a C++ static data member. */
8694
8695 /* Get type of field. */
8696 fp->type = die_type (die, cu);
8697
8698 SET_FIELD_BITPOS (*fp, 0);
8699
8700 /* Get bit size of field (zero if none). */
8701 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
8702 if (attr)
8703 {
8704 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
8705 }
8706 else
8707 {
8708 FIELD_BITSIZE (*fp) = 0;
8709 }
8710
8711 /* Get bit offset of field. */
8712 if (handle_data_member_location (die, cu, &offset))
8713 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
8714 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
8715 if (attr)
8716 {
8717 if (gdbarch_bits_big_endian (gdbarch))
8718 {
8719 /* For big endian bits, the DW_AT_bit_offset gives the
8720 additional bit offset from the MSB of the containing
8721 anonymous object to the MSB of the field. We don't
8722 have to do anything special since we don't need to
8723 know the size of the anonymous object. */
8724 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
8725 }
8726 else
8727 {
8728 /* For little endian bits, compute the bit offset to the
8729 MSB of the anonymous object, subtract off the number of
8730 bits from the MSB of the field to the MSB of the
8731 object, and then subtract off the number of bits of
8732 the field itself. The result is the bit offset of
8733 the LSB of the field. */
8734 int anonymous_size;
8735 int bit_offset = DW_UNSND (attr);
8736
8737 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8738 if (attr)
8739 {
8740 /* The size of the anonymous object containing
8741 the bit field is explicit, so use the
8742 indicated size (in bytes). */
8743 anonymous_size = DW_UNSND (attr);
8744 }
8745 else
8746 {
8747 /* The size of the anonymous object containing
8748 the bit field must be inferred from the type
8749 attribute of the data member containing the
8750 bit field. */
8751 anonymous_size = TYPE_LENGTH (fp->type);
8752 }
8753 SET_FIELD_BITPOS (*fp,
8754 (FIELD_BITPOS (*fp)
8755 + anonymous_size * bits_per_byte
8756 - bit_offset - FIELD_BITSIZE (*fp)));
8757 }
8758 }
8759
8760 /* Get name of field. */
8761 fieldname = dwarf2_name (die, cu);
8762 if (fieldname == NULL)
8763 fieldname = "";
8764
8765 /* The name is already allocated along with this objfile, so we don't
8766 need to duplicate it for the type. */
8767 fp->name = fieldname;
8768
8769 /* Change accessibility for artificial fields (e.g. virtual table
8770 pointer or virtual base class pointer) to private. */
8771 if (dwarf2_attr (die, DW_AT_artificial, cu))
8772 {
8773 FIELD_ARTIFICIAL (*fp) = 1;
8774 new_field->accessibility = DW_ACCESS_private;
8775 fip->non_public_fields = 1;
8776 }
8777 }
8778 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
8779 {
8780 /* C++ static member. */
8781
8782 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
8783 is a declaration, but all versions of G++ as of this writing
8784 (so through at least 3.2.1) incorrectly generate
8785 DW_TAG_variable tags. */
8786
8787 const char *physname;
8788
8789 /* Get name of field. */
8790 fieldname = dwarf2_name (die, cu);
8791 if (fieldname == NULL)
8792 return;
8793
8794 attr = dwarf2_attr (die, DW_AT_const_value, cu);
8795 if (attr
8796 /* Only create a symbol if this is an external value.
8797 new_symbol checks this and puts the value in the global symbol
8798 table, which we want. If it is not external, new_symbol
8799 will try to put the value in cu->list_in_scope which is wrong. */
8800 && dwarf2_flag_true_p (die, DW_AT_external, cu))
8801 {
8802 /* A static const member, not much different than an enum as far as
8803 we're concerned, except that we can support more types. */
8804 new_symbol (die, NULL, cu);
8805 }
8806
8807 /* Get physical name. */
8808 physname = dwarf2_physname (fieldname, die, cu);
8809
8810 /* The name is already allocated along with this objfile, so we don't
8811 need to duplicate it for the type. */
8812 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
8813 FIELD_TYPE (*fp) = die_type (die, cu);
8814 FIELD_NAME (*fp) = fieldname;
8815 }
8816 else if (die->tag == DW_TAG_inheritance)
8817 {
8818 LONGEST offset;
8819
8820 /* C++ base class field. */
8821 if (handle_data_member_location (die, cu, &offset))
8822 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
8823 FIELD_BITSIZE (*fp) = 0;
8824 FIELD_TYPE (*fp) = die_type (die, cu);
8825 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
8826 fip->nbaseclasses++;
8827 }
8828 }
8829
8830 /* Add a typedef defined in the scope of the FIP's class. */
8831
8832 static void
8833 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
8834 struct dwarf2_cu *cu)
8835 {
8836 struct objfile *objfile = cu->objfile;
8837 struct typedef_field_list *new_field;
8838 struct attribute *attr;
8839 struct typedef_field *fp;
8840 char *fieldname = "";
8841
8842 /* Allocate a new field list entry and link it in. */
8843 new_field = xzalloc (sizeof (*new_field));
8844 make_cleanup (xfree, new_field);
8845
8846 gdb_assert (die->tag == DW_TAG_typedef);
8847
8848 fp = &new_field->field;
8849
8850 /* Get name of field. */
8851 fp->name = dwarf2_name (die, cu);
8852 if (fp->name == NULL)
8853 return;
8854
8855 fp->type = read_type_die (die, cu);
8856
8857 new_field->next = fip->typedef_field_list;
8858 fip->typedef_field_list = new_field;
8859 fip->typedef_field_list_count++;
8860 }
8861
8862 /* Create the vector of fields, and attach it to the type. */
8863
8864 static void
8865 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
8866 struct dwarf2_cu *cu)
8867 {
8868 int nfields = fip->nfields;
8869
8870 /* Record the field count, allocate space for the array of fields,
8871 and create blank accessibility bitfields if necessary. */
8872 TYPE_NFIELDS (type) = nfields;
8873 TYPE_FIELDS (type) = (struct field *)
8874 TYPE_ALLOC (type, sizeof (struct field) * nfields);
8875 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
8876
8877 if (fip->non_public_fields && cu->language != language_ada)
8878 {
8879 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8880
8881 TYPE_FIELD_PRIVATE_BITS (type) =
8882 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8883 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
8884
8885 TYPE_FIELD_PROTECTED_BITS (type) =
8886 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8887 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
8888
8889 TYPE_FIELD_IGNORE_BITS (type) =
8890 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8891 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
8892 }
8893
8894 /* If the type has baseclasses, allocate and clear a bit vector for
8895 TYPE_FIELD_VIRTUAL_BITS. */
8896 if (fip->nbaseclasses && cu->language != language_ada)
8897 {
8898 int num_bytes = B_BYTES (fip->nbaseclasses);
8899 unsigned char *pointer;
8900
8901 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8902 pointer = TYPE_ALLOC (type, num_bytes);
8903 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
8904 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
8905 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
8906 }
8907
8908 /* Copy the saved-up fields into the field vector. Start from the head of
8909 the list, adding to the tail of the field array, so that they end up in
8910 the same order in the array in which they were added to the list. */
8911 while (nfields-- > 0)
8912 {
8913 struct nextfield *fieldp;
8914
8915 if (fip->fields)
8916 {
8917 fieldp = fip->fields;
8918 fip->fields = fieldp->next;
8919 }
8920 else
8921 {
8922 fieldp = fip->baseclasses;
8923 fip->baseclasses = fieldp->next;
8924 }
8925
8926 TYPE_FIELD (type, nfields) = fieldp->field;
8927 switch (fieldp->accessibility)
8928 {
8929 case DW_ACCESS_private:
8930 if (cu->language != language_ada)
8931 SET_TYPE_FIELD_PRIVATE (type, nfields);
8932 break;
8933
8934 case DW_ACCESS_protected:
8935 if (cu->language != language_ada)
8936 SET_TYPE_FIELD_PROTECTED (type, nfields);
8937 break;
8938
8939 case DW_ACCESS_public:
8940 break;
8941
8942 default:
8943 /* Unknown accessibility. Complain and treat it as public. */
8944 {
8945 complaint (&symfile_complaints, _("unsupported accessibility %d"),
8946 fieldp->accessibility);
8947 }
8948 break;
8949 }
8950 if (nfields < fip->nbaseclasses)
8951 {
8952 switch (fieldp->virtuality)
8953 {
8954 case DW_VIRTUALITY_virtual:
8955 case DW_VIRTUALITY_pure_virtual:
8956 if (cu->language == language_ada)
8957 error (_("unexpected virtuality in component of Ada type"));
8958 SET_TYPE_FIELD_VIRTUAL (type, nfields);
8959 break;
8960 }
8961 }
8962 }
8963 }
8964
8965 /* Add a member function to the proper fieldlist. */
8966
8967 static void
8968 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
8969 struct type *type, struct dwarf2_cu *cu)
8970 {
8971 struct objfile *objfile = cu->objfile;
8972 struct attribute *attr;
8973 struct fnfieldlist *flp;
8974 int i;
8975 struct fn_field *fnp;
8976 char *fieldname;
8977 struct nextfnfield *new_fnfield;
8978 struct type *this_type;
8979 enum dwarf_access_attribute accessibility;
8980
8981 if (cu->language == language_ada)
8982 error (_("unexpected member function in Ada type"));
8983
8984 /* Get name of member function. */
8985 fieldname = dwarf2_name (die, cu);
8986 if (fieldname == NULL)
8987 return;
8988
8989 /* Look up member function name in fieldlist. */
8990 for (i = 0; i < fip->nfnfields; i++)
8991 {
8992 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
8993 break;
8994 }
8995
8996 /* Create new list element if necessary. */
8997 if (i < fip->nfnfields)
8998 flp = &fip->fnfieldlists[i];
8999 else
9000 {
9001 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
9002 {
9003 fip->fnfieldlists = (struct fnfieldlist *)
9004 xrealloc (fip->fnfieldlists,
9005 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
9006 * sizeof (struct fnfieldlist));
9007 if (fip->nfnfields == 0)
9008 make_cleanup (free_current_contents, &fip->fnfieldlists);
9009 }
9010 flp = &fip->fnfieldlists[fip->nfnfields];
9011 flp->name = fieldname;
9012 flp->length = 0;
9013 flp->head = NULL;
9014 i = fip->nfnfields++;
9015 }
9016
9017 /* Create a new member function field and chain it to the field list
9018 entry. */
9019 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
9020 make_cleanup (xfree, new_fnfield);
9021 memset (new_fnfield, 0, sizeof (struct nextfnfield));
9022 new_fnfield->next = flp->head;
9023 flp->head = new_fnfield;
9024 flp->length++;
9025
9026 /* Fill in the member function field info. */
9027 fnp = &new_fnfield->fnfield;
9028
9029 /* Delay processing of the physname until later. */
9030 if (cu->language == language_cplus || cu->language == language_java)
9031 {
9032 add_to_method_list (type, i, flp->length - 1, fieldname,
9033 die, cu);
9034 }
9035 else
9036 {
9037 const char *physname = dwarf2_physname (fieldname, die, cu);
9038 fnp->physname = physname ? physname : "";
9039 }
9040
9041 fnp->type = alloc_type (objfile);
9042 this_type = read_type_die (die, cu);
9043 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
9044 {
9045 int nparams = TYPE_NFIELDS (this_type);
9046
9047 /* TYPE is the domain of this method, and THIS_TYPE is the type
9048 of the method itself (TYPE_CODE_METHOD). */
9049 smash_to_method_type (fnp->type, type,
9050 TYPE_TARGET_TYPE (this_type),
9051 TYPE_FIELDS (this_type),
9052 TYPE_NFIELDS (this_type),
9053 TYPE_VARARGS (this_type));
9054
9055 /* Handle static member functions.
9056 Dwarf2 has no clean way to discern C++ static and non-static
9057 member functions. G++ helps GDB by marking the first
9058 parameter for non-static member functions (which is the this
9059 pointer) as artificial. We obtain this information from
9060 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
9061 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
9062 fnp->voffset = VOFFSET_STATIC;
9063 }
9064 else
9065 complaint (&symfile_complaints, _("member function type missing for '%s'"),
9066 dwarf2_full_name (fieldname, die, cu));
9067
9068 /* Get fcontext from DW_AT_containing_type if present. */
9069 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
9070 fnp->fcontext = die_containing_type (die, cu);
9071
9072 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
9073 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
9074
9075 /* Get accessibility. */
9076 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
9077 if (attr)
9078 accessibility = DW_UNSND (attr);
9079 else
9080 accessibility = dwarf2_default_access_attribute (die, cu);
9081 switch (accessibility)
9082 {
9083 case DW_ACCESS_private:
9084 fnp->is_private = 1;
9085 break;
9086 case DW_ACCESS_protected:
9087 fnp->is_protected = 1;
9088 break;
9089 }
9090
9091 /* Check for artificial methods. */
9092 attr = dwarf2_attr (die, DW_AT_artificial, cu);
9093 if (attr && DW_UNSND (attr) != 0)
9094 fnp->is_artificial = 1;
9095
9096 /* Get index in virtual function table if it is a virtual member
9097 function. For older versions of GCC, this is an offset in the
9098 appropriate virtual table, as specified by DW_AT_containing_type.
9099 For everyone else, it is an expression to be evaluated relative
9100 to the object address. */
9101
9102 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
9103 if (attr)
9104 {
9105 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
9106 {
9107 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
9108 {
9109 /* Old-style GCC. */
9110 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
9111 }
9112 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
9113 || (DW_BLOCK (attr)->size > 1
9114 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
9115 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
9116 {
9117 struct dwarf_block blk;
9118 int offset;
9119
9120 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
9121 ? 1 : 2);
9122 blk.size = DW_BLOCK (attr)->size - offset;
9123 blk.data = DW_BLOCK (attr)->data + offset;
9124 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
9125 if ((fnp->voffset % cu->header.addr_size) != 0)
9126 dwarf2_complex_location_expr_complaint ();
9127 else
9128 fnp->voffset /= cu->header.addr_size;
9129 fnp->voffset += 2;
9130 }
9131 else
9132 dwarf2_complex_location_expr_complaint ();
9133
9134 if (!fnp->fcontext)
9135 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
9136 }
9137 else if (attr_form_is_section_offset (attr))
9138 {
9139 dwarf2_complex_location_expr_complaint ();
9140 }
9141 else
9142 {
9143 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
9144 fieldname);
9145 }
9146 }
9147 else
9148 {
9149 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
9150 if (attr && DW_UNSND (attr))
9151 {
9152 /* GCC does this, as of 2008-08-25; PR debug/37237. */
9153 complaint (&symfile_complaints,
9154 _("Member function \"%s\" (offset %d) is virtual "
9155 "but the vtable offset is not specified"),
9156 fieldname, die->offset.sect_off);
9157 ALLOCATE_CPLUS_STRUCT_TYPE (type);
9158 TYPE_CPLUS_DYNAMIC (type) = 1;
9159 }
9160 }
9161 }
9162
9163 /* Create the vector of member function fields, and attach it to the type. */
9164
9165 static void
9166 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
9167 struct dwarf2_cu *cu)
9168 {
9169 struct fnfieldlist *flp;
9170 int i;
9171
9172 if (cu->language == language_ada)
9173 error (_("unexpected member functions in Ada type"));
9174
9175 ALLOCATE_CPLUS_STRUCT_TYPE (type);
9176 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
9177 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
9178
9179 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
9180 {
9181 struct nextfnfield *nfp = flp->head;
9182 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
9183 int k;
9184
9185 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
9186 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
9187 fn_flp->fn_fields = (struct fn_field *)
9188 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
9189 for (k = flp->length; (k--, nfp); nfp = nfp->next)
9190 fn_flp->fn_fields[k] = nfp->fnfield;
9191 }
9192
9193 TYPE_NFN_FIELDS (type) = fip->nfnfields;
9194 }
9195
9196 /* Returns non-zero if NAME is the name of a vtable member in CU's
9197 language, zero otherwise. */
9198 static int
9199 is_vtable_name (const char *name, struct dwarf2_cu *cu)
9200 {
9201 static const char vptr[] = "_vptr";
9202 static const char vtable[] = "vtable";
9203
9204 /* Look for the C++ and Java forms of the vtable. */
9205 if ((cu->language == language_java
9206 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
9207 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
9208 && is_cplus_marker (name[sizeof (vptr) - 1])))
9209 return 1;
9210
9211 return 0;
9212 }
9213
9214 /* GCC outputs unnamed structures that are really pointers to member
9215 functions, with the ABI-specified layout. If TYPE describes
9216 such a structure, smash it into a member function type.
9217
9218 GCC shouldn't do this; it should just output pointer to member DIEs.
9219 This is GCC PR debug/28767. */
9220
9221 static void
9222 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
9223 {
9224 struct type *pfn_type, *domain_type, *new_type;
9225
9226 /* Check for a structure with no name and two children. */
9227 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
9228 return;
9229
9230 /* Check for __pfn and __delta members. */
9231 if (TYPE_FIELD_NAME (type, 0) == NULL
9232 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
9233 || TYPE_FIELD_NAME (type, 1) == NULL
9234 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
9235 return;
9236
9237 /* Find the type of the method. */
9238 pfn_type = TYPE_FIELD_TYPE (type, 0);
9239 if (pfn_type == NULL
9240 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
9241 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
9242 return;
9243
9244 /* Look for the "this" argument. */
9245 pfn_type = TYPE_TARGET_TYPE (pfn_type);
9246 if (TYPE_NFIELDS (pfn_type) == 0
9247 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
9248 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
9249 return;
9250
9251 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
9252 new_type = alloc_type (objfile);
9253 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
9254 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
9255 TYPE_VARARGS (pfn_type));
9256 smash_to_methodptr_type (type, new_type);
9257 }
9258
9259 /* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
9260 (icc). */
9261
9262 static int
9263 producer_is_icc (struct dwarf2_cu *cu)
9264 {
9265 if (!cu->checked_producer)
9266 check_producer (cu);
9267
9268 return cu->producer_is_icc;
9269 }
9270
9271 /* Called when we find the DIE that starts a structure or union scope
9272 (definition) to create a type for the structure or union. Fill in
9273 the type's name and general properties; the members will not be
9274 processed until process_structure_type.
9275
9276 NOTE: we need to call these functions regardless of whether or not the
9277 DIE has a DW_AT_name attribute, since it might be an anonymous
9278 structure or union. This gets the type entered into our set of
9279 user defined types.
9280
9281 However, if the structure is incomplete (an opaque struct/union)
9282 then suppress creating a symbol table entry for it since gdb only
9283 wants to find the one with the complete definition. Note that if
9284 it is complete, we just call new_symbol, which does it's own
9285 checking about whether the struct/union is anonymous or not (and
9286 suppresses creating a symbol table entry itself). */
9287
9288 static struct type *
9289 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
9290 {
9291 struct objfile *objfile = cu->objfile;
9292 struct type *type;
9293 struct attribute *attr;
9294 char *name;
9295
9296 /* If the definition of this type lives in .debug_types, read that type.
9297 Don't follow DW_AT_specification though, that will take us back up
9298 the chain and we want to go down. */
9299 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
9300 if (attr)
9301 {
9302 struct dwarf2_cu *type_cu = cu;
9303 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9304
9305 /* We could just recurse on read_structure_type, but we need to call
9306 get_die_type to ensure only one type for this DIE is created.
9307 This is important, for example, because for c++ classes we need
9308 TYPE_NAME set which is only done by new_symbol. Blech. */
9309 type = read_type_die (type_die, type_cu);
9310
9311 /* TYPE_CU may not be the same as CU.
9312 Ensure TYPE is recorded in CU's type_hash table. */
9313 return set_die_type (die, type, cu);
9314 }
9315
9316 type = alloc_type (objfile);
9317 INIT_CPLUS_SPECIFIC (type);
9318
9319 name = dwarf2_name (die, cu);
9320 if (name != NULL)
9321 {
9322 if (cu->language == language_cplus
9323 || cu->language == language_java)
9324 {
9325 char *full_name = (char *) dwarf2_full_name (name, die, cu);
9326
9327 /* dwarf2_full_name might have already finished building the DIE's
9328 type. If so, there is no need to continue. */
9329 if (get_die_type (die, cu) != NULL)
9330 return get_die_type (die, cu);
9331
9332 TYPE_TAG_NAME (type) = full_name;
9333 if (die->tag == DW_TAG_structure_type
9334 || die->tag == DW_TAG_class_type)
9335 TYPE_NAME (type) = TYPE_TAG_NAME (type);
9336 }
9337 else
9338 {
9339 /* The name is already allocated along with this objfile, so
9340 we don't need to duplicate it for the type. */
9341 TYPE_TAG_NAME (type) = (char *) name;
9342 if (die->tag == DW_TAG_class_type)
9343 TYPE_NAME (type) = TYPE_TAG_NAME (type);
9344 }
9345 }
9346
9347 if (die->tag == DW_TAG_structure_type)
9348 {
9349 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9350 }
9351 else if (die->tag == DW_TAG_union_type)
9352 {
9353 TYPE_CODE (type) = TYPE_CODE_UNION;
9354 }
9355 else
9356 {
9357 TYPE_CODE (type) = TYPE_CODE_CLASS;
9358 }
9359
9360 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
9361 TYPE_DECLARED_CLASS (type) = 1;
9362
9363 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9364 if (attr)
9365 {
9366 TYPE_LENGTH (type) = DW_UNSND (attr);
9367 }
9368 else
9369 {
9370 TYPE_LENGTH (type) = 0;
9371 }
9372
9373 if (producer_is_icc (cu))
9374 {
9375 /* ICC does not output the required DW_AT_declaration
9376 on incomplete types, but gives them a size of zero. */
9377 }
9378 else
9379 TYPE_STUB_SUPPORTED (type) = 1;
9380
9381 if (die_is_declaration (die, cu))
9382 TYPE_STUB (type) = 1;
9383 else if (attr == NULL && die->child == NULL
9384 && producer_is_realview (cu->producer))
9385 /* RealView does not output the required DW_AT_declaration
9386 on incomplete types. */
9387 TYPE_STUB (type) = 1;
9388
9389 /* We need to add the type field to the die immediately so we don't
9390 infinitely recurse when dealing with pointers to the structure
9391 type within the structure itself. */
9392 set_die_type (die, type, cu);
9393
9394 /* set_die_type should be already done. */
9395 set_descriptive_type (type, die, cu);
9396
9397 return type;
9398 }
9399
9400 /* Finish creating a structure or union type, including filling in
9401 its members and creating a symbol for it. */
9402
9403 static void
9404 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
9405 {
9406 struct objfile *objfile = cu->objfile;
9407 struct die_info *child_die = die->child;
9408 struct type *type;
9409
9410 type = get_die_type (die, cu);
9411 if (type == NULL)
9412 type = read_structure_type (die, cu);
9413
9414 if (die->child != NULL && ! die_is_declaration (die, cu))
9415 {
9416 struct field_info fi;
9417 struct die_info *child_die;
9418 VEC (symbolp) *template_args = NULL;
9419 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
9420
9421 memset (&fi, 0, sizeof (struct field_info));
9422
9423 child_die = die->child;
9424
9425 while (child_die && child_die->tag)
9426 {
9427 if (child_die->tag == DW_TAG_member
9428 || child_die->tag == DW_TAG_variable)
9429 {
9430 /* NOTE: carlton/2002-11-05: A C++ static data member
9431 should be a DW_TAG_member that is a declaration, but
9432 all versions of G++ as of this writing (so through at
9433 least 3.2.1) incorrectly generate DW_TAG_variable
9434 tags for them instead. */
9435 dwarf2_add_field (&fi, child_die, cu);
9436 }
9437 else if (child_die->tag == DW_TAG_subprogram)
9438 {
9439 /* C++ member function. */
9440 dwarf2_add_member_fn (&fi, child_die, type, cu);
9441 }
9442 else if (child_die->tag == DW_TAG_inheritance)
9443 {
9444 /* C++ base class field. */
9445 dwarf2_add_field (&fi, child_die, cu);
9446 }
9447 else if (child_die->tag == DW_TAG_typedef)
9448 dwarf2_add_typedef (&fi, child_die, cu);
9449 else if (child_die->tag == DW_TAG_template_type_param
9450 || child_die->tag == DW_TAG_template_value_param)
9451 {
9452 struct symbol *arg = new_symbol (child_die, NULL, cu);
9453
9454 if (arg != NULL)
9455 VEC_safe_push (symbolp, template_args, arg);
9456 }
9457
9458 child_die = sibling_die (child_die);
9459 }
9460
9461 /* Attach template arguments to type. */
9462 if (! VEC_empty (symbolp, template_args))
9463 {
9464 ALLOCATE_CPLUS_STRUCT_TYPE (type);
9465 TYPE_N_TEMPLATE_ARGUMENTS (type)
9466 = VEC_length (symbolp, template_args);
9467 TYPE_TEMPLATE_ARGUMENTS (type)
9468 = obstack_alloc (&objfile->objfile_obstack,
9469 (TYPE_N_TEMPLATE_ARGUMENTS (type)
9470 * sizeof (struct symbol *)));
9471 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
9472 VEC_address (symbolp, template_args),
9473 (TYPE_N_TEMPLATE_ARGUMENTS (type)
9474 * sizeof (struct symbol *)));
9475 VEC_free (symbolp, template_args);
9476 }
9477
9478 /* Attach fields and member functions to the type. */
9479 if (fi.nfields)
9480 dwarf2_attach_fields_to_type (&fi, type, cu);
9481 if (fi.nfnfields)
9482 {
9483 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
9484
9485 /* Get the type which refers to the base class (possibly this
9486 class itself) which contains the vtable pointer for the current
9487 class from the DW_AT_containing_type attribute. This use of
9488 DW_AT_containing_type is a GNU extension. */
9489
9490 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
9491 {
9492 struct type *t = die_containing_type (die, cu);
9493
9494 TYPE_VPTR_BASETYPE (type) = t;
9495 if (type == t)
9496 {
9497 int i;
9498
9499 /* Our own class provides vtbl ptr. */
9500 for (i = TYPE_NFIELDS (t) - 1;
9501 i >= TYPE_N_BASECLASSES (t);
9502 --i)
9503 {
9504 const char *fieldname = TYPE_FIELD_NAME (t, i);
9505
9506 if (is_vtable_name (fieldname, cu))
9507 {
9508 TYPE_VPTR_FIELDNO (type) = i;
9509 break;
9510 }
9511 }
9512
9513 /* Complain if virtual function table field not found. */
9514 if (i < TYPE_N_BASECLASSES (t))
9515 complaint (&symfile_complaints,
9516 _("virtual function table pointer "
9517 "not found when defining class '%s'"),
9518 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
9519 "");
9520 }
9521 else
9522 {
9523 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
9524 }
9525 }
9526 else if (cu->producer
9527 && strncmp (cu->producer,
9528 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
9529 {
9530 /* The IBM XLC compiler does not provide direct indication
9531 of the containing type, but the vtable pointer is
9532 always named __vfp. */
9533
9534 int i;
9535
9536 for (i = TYPE_NFIELDS (type) - 1;
9537 i >= TYPE_N_BASECLASSES (type);
9538 --i)
9539 {
9540 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
9541 {
9542 TYPE_VPTR_FIELDNO (type) = i;
9543 TYPE_VPTR_BASETYPE (type) = type;
9544 break;
9545 }
9546 }
9547 }
9548 }
9549
9550 /* Copy fi.typedef_field_list linked list elements content into the
9551 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
9552 if (fi.typedef_field_list)
9553 {
9554 int i = fi.typedef_field_list_count;
9555
9556 ALLOCATE_CPLUS_STRUCT_TYPE (type);
9557 TYPE_TYPEDEF_FIELD_ARRAY (type)
9558 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
9559 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
9560
9561 /* Reverse the list order to keep the debug info elements order. */
9562 while (--i >= 0)
9563 {
9564 struct typedef_field *dest, *src;
9565
9566 dest = &TYPE_TYPEDEF_FIELD (type, i);
9567 src = &fi.typedef_field_list->field;
9568 fi.typedef_field_list = fi.typedef_field_list->next;
9569 *dest = *src;
9570 }
9571 }
9572
9573 do_cleanups (back_to);
9574
9575 if (HAVE_CPLUS_STRUCT (type))
9576 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
9577 }
9578
9579 quirk_gcc_member_function_pointer (type, objfile);
9580
9581 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
9582 snapshots) has been known to create a die giving a declaration
9583 for a class that has, as a child, a die giving a definition for a
9584 nested class. So we have to process our children even if the
9585 current die is a declaration. Normally, of course, a declaration
9586 won't have any children at all. */
9587
9588 while (child_die != NULL && child_die->tag)
9589 {
9590 if (child_die->tag == DW_TAG_member
9591 || child_die->tag == DW_TAG_variable
9592 || child_die->tag == DW_TAG_inheritance
9593 || child_die->tag == DW_TAG_template_value_param
9594 || child_die->tag == DW_TAG_template_type_param)
9595 {
9596 /* Do nothing. */
9597 }
9598 else
9599 process_die (child_die, cu);
9600
9601 child_die = sibling_die (child_die);
9602 }
9603
9604 /* Do not consider external references. According to the DWARF standard,
9605 these DIEs are identified by the fact that they have no byte_size
9606 attribute, and a declaration attribute. */
9607 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
9608 || !die_is_declaration (die, cu))
9609 new_symbol (die, type, cu);
9610 }
9611
9612 /* Given a DW_AT_enumeration_type die, set its type. We do not
9613 complete the type's fields yet, or create any symbols. */
9614
9615 static struct type *
9616 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
9617 {
9618 struct objfile *objfile = cu->objfile;
9619 struct type *type;
9620 struct attribute *attr;
9621 const char *name;
9622
9623 /* If the definition of this type lives in .debug_types, read that type.
9624 Don't follow DW_AT_specification though, that will take us back up
9625 the chain and we want to go down. */
9626 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
9627 if (attr)
9628 {
9629 struct dwarf2_cu *type_cu = cu;
9630 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9631
9632 type = read_type_die (type_die, type_cu);
9633
9634 /* TYPE_CU may not be the same as CU.
9635 Ensure TYPE is recorded in CU's type_hash table. */
9636 return set_die_type (die, type, cu);
9637 }
9638
9639 type = alloc_type (objfile);
9640
9641 TYPE_CODE (type) = TYPE_CODE_ENUM;
9642 name = dwarf2_full_name (NULL, die, cu);
9643 if (name != NULL)
9644 TYPE_TAG_NAME (type) = (char *) name;
9645
9646 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9647 if (attr)
9648 {
9649 TYPE_LENGTH (type) = DW_UNSND (attr);
9650 }
9651 else
9652 {
9653 TYPE_LENGTH (type) = 0;
9654 }
9655
9656 /* The enumeration DIE can be incomplete. In Ada, any type can be
9657 declared as private in the package spec, and then defined only
9658 inside the package body. Such types are known as Taft Amendment
9659 Types. When another package uses such a type, an incomplete DIE
9660 may be generated by the compiler. */
9661 if (die_is_declaration (die, cu))
9662 TYPE_STUB (type) = 1;
9663
9664 return set_die_type (die, type, cu);
9665 }
9666
9667 /* Given a pointer to a die which begins an enumeration, process all
9668 the dies that define the members of the enumeration, and create the
9669 symbol for the enumeration type.
9670
9671 NOTE: We reverse the order of the element list. */
9672
9673 static void
9674 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
9675 {
9676 struct type *this_type;
9677
9678 this_type = get_die_type (die, cu);
9679 if (this_type == NULL)
9680 this_type = read_enumeration_type (die, cu);
9681
9682 if (die->child != NULL)
9683 {
9684 struct die_info *child_die;
9685 struct symbol *sym;
9686 struct field *fields = NULL;
9687 int num_fields = 0;
9688 int unsigned_enum = 1;
9689 char *name;
9690 int flag_enum = 1;
9691 ULONGEST mask = 0;
9692
9693 child_die = die->child;
9694 while (child_die && child_die->tag)
9695 {
9696 if (child_die->tag != DW_TAG_enumerator)
9697 {
9698 process_die (child_die, cu);
9699 }
9700 else
9701 {
9702 name = dwarf2_name (child_die, cu);
9703 if (name)
9704 {
9705 sym = new_symbol (child_die, this_type, cu);
9706 if (SYMBOL_VALUE (sym) < 0)
9707 {
9708 unsigned_enum = 0;
9709 flag_enum = 0;
9710 }
9711 else if ((mask & SYMBOL_VALUE (sym)) != 0)
9712 flag_enum = 0;
9713 else
9714 mask |= SYMBOL_VALUE (sym);
9715
9716 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
9717 {
9718 fields = (struct field *)
9719 xrealloc (fields,
9720 (num_fields + DW_FIELD_ALLOC_CHUNK)
9721 * sizeof (struct field));
9722 }
9723
9724 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
9725 FIELD_TYPE (fields[num_fields]) = NULL;
9726 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
9727 FIELD_BITSIZE (fields[num_fields]) = 0;
9728
9729 num_fields++;
9730 }
9731 }
9732
9733 child_die = sibling_die (child_die);
9734 }
9735
9736 if (num_fields)
9737 {
9738 TYPE_NFIELDS (this_type) = num_fields;
9739 TYPE_FIELDS (this_type) = (struct field *)
9740 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
9741 memcpy (TYPE_FIELDS (this_type), fields,
9742 sizeof (struct field) * num_fields);
9743 xfree (fields);
9744 }
9745 if (unsigned_enum)
9746 TYPE_UNSIGNED (this_type) = 1;
9747 if (flag_enum)
9748 TYPE_FLAG_ENUM (this_type) = 1;
9749 }
9750
9751 /* If we are reading an enum from a .debug_types unit, and the enum
9752 is a declaration, and the enum is not the signatured type in the
9753 unit, then we do not want to add a symbol for it. Adding a
9754 symbol would in some cases obscure the true definition of the
9755 enum, giving users an incomplete type when the definition is
9756 actually available. Note that we do not want to do this for all
9757 enums which are just declarations, because C++0x allows forward
9758 enum declarations. */
9759 if (cu->per_cu->is_debug_types
9760 && die_is_declaration (die, cu))
9761 {
9762 struct signatured_type *sig_type;
9763
9764 sig_type
9765 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
9766 cu->per_cu->info_or_types_section,
9767 cu->per_cu->offset);
9768 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
9769 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
9770 return;
9771 }
9772
9773 new_symbol (die, this_type, cu);
9774 }
9775
9776 /* Extract all information from a DW_TAG_array_type DIE and put it in
9777 the DIE's type field. For now, this only handles one dimensional
9778 arrays. */
9779
9780 static struct type *
9781 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
9782 {
9783 struct objfile *objfile = cu->objfile;
9784 struct die_info *child_die;
9785 struct type *type;
9786 struct type *element_type, *range_type, *index_type;
9787 struct type **range_types = NULL;
9788 struct attribute *attr;
9789 int ndim = 0;
9790 struct cleanup *back_to;
9791 char *name;
9792
9793 element_type = die_type (die, cu);
9794
9795 /* The die_type call above may have already set the type for this DIE. */
9796 type = get_die_type (die, cu);
9797 if (type)
9798 return type;
9799
9800 /* Irix 6.2 native cc creates array types without children for
9801 arrays with unspecified length. */
9802 if (die->child == NULL)
9803 {
9804 index_type = objfile_type (objfile)->builtin_int;
9805 range_type = create_range_type (NULL, index_type, 0, -1);
9806 type = create_array_type (NULL, element_type, range_type);
9807 return set_die_type (die, type, cu);
9808 }
9809
9810 back_to = make_cleanup (null_cleanup, NULL);
9811 child_die = die->child;
9812 while (child_die && child_die->tag)
9813 {
9814 if (child_die->tag == DW_TAG_subrange_type)
9815 {
9816 struct type *child_type = read_type_die (child_die, cu);
9817
9818 if (child_type != NULL)
9819 {
9820 /* The range type was succesfully read. Save it for the
9821 array type creation. */
9822 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
9823 {
9824 range_types = (struct type **)
9825 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
9826 * sizeof (struct type *));
9827 if (ndim == 0)
9828 make_cleanup (free_current_contents, &range_types);
9829 }
9830 range_types[ndim++] = child_type;
9831 }
9832 }
9833 child_die = sibling_die (child_die);
9834 }
9835
9836 /* Dwarf2 dimensions are output from left to right, create the
9837 necessary array types in backwards order. */
9838
9839 type = element_type;
9840
9841 if (read_array_order (die, cu) == DW_ORD_col_major)
9842 {
9843 int i = 0;
9844
9845 while (i < ndim)
9846 type = create_array_type (NULL, type, range_types[i++]);
9847 }
9848 else
9849 {
9850 while (ndim-- > 0)
9851 type = create_array_type (NULL, type, range_types[ndim]);
9852 }
9853
9854 /* Understand Dwarf2 support for vector types (like they occur on
9855 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
9856 array type. This is not part of the Dwarf2/3 standard yet, but a
9857 custom vendor extension. The main difference between a regular
9858 array and the vector variant is that vectors are passed by value
9859 to functions. */
9860 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
9861 if (attr)
9862 make_vector_type (type);
9863
9864 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
9865 implementation may choose to implement triple vectors using this
9866 attribute. */
9867 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9868 if (attr)
9869 {
9870 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
9871 TYPE_LENGTH (type) = DW_UNSND (attr);
9872 else
9873 complaint (&symfile_complaints,
9874 _("DW_AT_byte_size for array type smaller "
9875 "than the total size of elements"));
9876 }
9877
9878 name = dwarf2_name (die, cu);
9879 if (name)
9880 TYPE_NAME (type) = name;
9881
9882 /* Install the type in the die. */
9883 set_die_type (die, type, cu);
9884
9885 /* set_die_type should be already done. */
9886 set_descriptive_type (type, die, cu);
9887
9888 do_cleanups (back_to);
9889
9890 return type;
9891 }
9892
9893 static enum dwarf_array_dim_ordering
9894 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
9895 {
9896 struct attribute *attr;
9897
9898 attr = dwarf2_attr (die, DW_AT_ordering, cu);
9899
9900 if (attr) return DW_SND (attr);
9901
9902 /* GNU F77 is a special case, as at 08/2004 array type info is the
9903 opposite order to the dwarf2 specification, but data is still
9904 laid out as per normal fortran.
9905
9906 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
9907 version checking. */
9908
9909 if (cu->language == language_fortran
9910 && cu->producer && strstr (cu->producer, "GNU F77"))
9911 {
9912 return DW_ORD_row_major;
9913 }
9914
9915 switch (cu->language_defn->la_array_ordering)
9916 {
9917 case array_column_major:
9918 return DW_ORD_col_major;
9919 case array_row_major:
9920 default:
9921 return DW_ORD_row_major;
9922 };
9923 }
9924
9925 /* Extract all information from a DW_TAG_set_type DIE and put it in
9926 the DIE's type field. */
9927
9928 static struct type *
9929 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
9930 {
9931 struct type *domain_type, *set_type;
9932 struct attribute *attr;
9933
9934 domain_type = die_type (die, cu);
9935
9936 /* The die_type call above may have already set the type for this DIE. */
9937 set_type = get_die_type (die, cu);
9938 if (set_type)
9939 return set_type;
9940
9941 set_type = create_set_type (NULL, domain_type);
9942
9943 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9944 if (attr)
9945 TYPE_LENGTH (set_type) = DW_UNSND (attr);
9946
9947 return set_die_type (die, set_type, cu);
9948 }
9949
9950 /* First cut: install each common block member as a global variable. */
9951
9952 static void
9953 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
9954 {
9955 struct die_info *child_die;
9956 struct attribute *attr;
9957 struct symbol *sym;
9958 CORE_ADDR base = (CORE_ADDR) 0;
9959
9960 attr = dwarf2_attr (die, DW_AT_location, cu);
9961 if (attr)
9962 {
9963 /* Support the .debug_loc offsets. */
9964 if (attr_form_is_block (attr))
9965 {
9966 base = decode_locdesc (DW_BLOCK (attr), cu);
9967 }
9968 else if (attr_form_is_section_offset (attr))
9969 {
9970 dwarf2_complex_location_expr_complaint ();
9971 }
9972 else
9973 {
9974 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9975 "common block member");
9976 }
9977 }
9978 if (die->child != NULL)
9979 {
9980 child_die = die->child;
9981 while (child_die && child_die->tag)
9982 {
9983 LONGEST offset;
9984
9985 sym = new_symbol (child_die, NULL, cu);
9986 if (sym != NULL
9987 && handle_data_member_location (child_die, cu, &offset))
9988 {
9989 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
9990 add_symbol_to_list (sym, &global_symbols);
9991 }
9992 child_die = sibling_die (child_die);
9993 }
9994 }
9995 }
9996
9997 /* Create a type for a C++ namespace. */
9998
9999 static struct type *
10000 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
10001 {
10002 struct objfile *objfile = cu->objfile;
10003 const char *previous_prefix, *name;
10004 int is_anonymous;
10005 struct type *type;
10006
10007 /* For extensions, reuse the type of the original namespace. */
10008 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
10009 {
10010 struct die_info *ext_die;
10011 struct dwarf2_cu *ext_cu = cu;
10012
10013 ext_die = dwarf2_extension (die, &ext_cu);
10014 type = read_type_die (ext_die, ext_cu);
10015
10016 /* EXT_CU may not be the same as CU.
10017 Ensure TYPE is recorded in CU's type_hash table. */
10018 return set_die_type (die, type, cu);
10019 }
10020
10021 name = namespace_name (die, &is_anonymous, cu);
10022
10023 /* Now build the name of the current namespace. */
10024
10025 previous_prefix = determine_prefix (die, cu);
10026 if (previous_prefix[0] != '\0')
10027 name = typename_concat (&objfile->objfile_obstack,
10028 previous_prefix, name, 0, cu);
10029
10030 /* Create the type. */
10031 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
10032 objfile);
10033 TYPE_NAME (type) = (char *) name;
10034 TYPE_TAG_NAME (type) = TYPE_NAME (type);
10035
10036 return set_die_type (die, type, cu);
10037 }
10038
10039 /* Read a C++ namespace. */
10040
10041 static void
10042 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
10043 {
10044 struct objfile *objfile = cu->objfile;
10045 int is_anonymous;
10046
10047 /* Add a symbol associated to this if we haven't seen the namespace
10048 before. Also, add a using directive if it's an anonymous
10049 namespace. */
10050
10051 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
10052 {
10053 struct type *type;
10054
10055 type = read_type_die (die, cu);
10056 new_symbol (die, type, cu);
10057
10058 namespace_name (die, &is_anonymous, cu);
10059 if (is_anonymous)
10060 {
10061 const char *previous_prefix = determine_prefix (die, cu);
10062
10063 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
10064 NULL, NULL, &objfile->objfile_obstack);
10065 }
10066 }
10067
10068 if (die->child != NULL)
10069 {
10070 struct die_info *child_die = die->child;
10071
10072 while (child_die && child_die->tag)
10073 {
10074 process_die (child_die, cu);
10075 child_die = sibling_die (child_die);
10076 }
10077 }
10078 }
10079
10080 /* Read a Fortran module as type. This DIE can be only a declaration used for
10081 imported module. Still we need that type as local Fortran "use ... only"
10082 declaration imports depend on the created type in determine_prefix. */
10083
10084 static struct type *
10085 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
10086 {
10087 struct objfile *objfile = cu->objfile;
10088 char *module_name;
10089 struct type *type;
10090
10091 module_name = dwarf2_name (die, cu);
10092 if (!module_name)
10093 complaint (&symfile_complaints,
10094 _("DW_TAG_module has no name, offset 0x%x"),
10095 die->offset.sect_off);
10096 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
10097
10098 /* determine_prefix uses TYPE_TAG_NAME. */
10099 TYPE_TAG_NAME (type) = TYPE_NAME (type);
10100
10101 return set_die_type (die, type, cu);
10102 }
10103
10104 /* Read a Fortran module. */
10105
10106 static void
10107 read_module (struct die_info *die, struct dwarf2_cu *cu)
10108 {
10109 struct die_info *child_die = die->child;
10110
10111 while (child_die && child_die->tag)
10112 {
10113 process_die (child_die, cu);
10114 child_die = sibling_die (child_die);
10115 }
10116 }
10117
10118 /* Return the name of the namespace represented by DIE. Set
10119 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
10120 namespace. */
10121
10122 static const char *
10123 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
10124 {
10125 struct die_info *current_die;
10126 const char *name = NULL;
10127
10128 /* Loop through the extensions until we find a name. */
10129
10130 for (current_die = die;
10131 current_die != NULL;
10132 current_die = dwarf2_extension (die, &cu))
10133 {
10134 name = dwarf2_name (current_die, cu);
10135 if (name != NULL)
10136 break;
10137 }
10138
10139 /* Is it an anonymous namespace? */
10140
10141 *is_anonymous = (name == NULL);
10142 if (*is_anonymous)
10143 name = CP_ANONYMOUS_NAMESPACE_STR;
10144
10145 return name;
10146 }
10147
10148 /* Extract all information from a DW_TAG_pointer_type DIE and add to
10149 the user defined type vector. */
10150
10151 static struct type *
10152 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
10153 {
10154 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
10155 struct comp_unit_head *cu_header = &cu->header;
10156 struct type *type;
10157 struct attribute *attr_byte_size;
10158 struct attribute *attr_address_class;
10159 int byte_size, addr_class;
10160 struct type *target_type;
10161
10162 target_type = die_type (die, cu);
10163
10164 /* The die_type call above may have already set the type for this DIE. */
10165 type = get_die_type (die, cu);
10166 if (type)
10167 return type;
10168
10169 type = lookup_pointer_type (target_type);
10170
10171 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
10172 if (attr_byte_size)
10173 byte_size = DW_UNSND (attr_byte_size);
10174 else
10175 byte_size = cu_header->addr_size;
10176
10177 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
10178 if (attr_address_class)
10179 addr_class = DW_UNSND (attr_address_class);
10180 else
10181 addr_class = DW_ADDR_none;
10182
10183 /* If the pointer size or address class is different than the
10184 default, create a type variant marked as such and set the
10185 length accordingly. */
10186 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
10187 {
10188 if (gdbarch_address_class_type_flags_p (gdbarch))
10189 {
10190 int type_flags;
10191
10192 type_flags = gdbarch_address_class_type_flags
10193 (gdbarch, byte_size, addr_class);
10194 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
10195 == 0);
10196 type = make_type_with_address_space (type, type_flags);
10197 }
10198 else if (TYPE_LENGTH (type) != byte_size)
10199 {
10200 complaint (&symfile_complaints,
10201 _("invalid pointer size %d"), byte_size);
10202 }
10203 else
10204 {
10205 /* Should we also complain about unhandled address classes? */
10206 }
10207 }
10208
10209 TYPE_LENGTH (type) = byte_size;
10210 return set_die_type (die, type, cu);
10211 }
10212
10213 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
10214 the user defined type vector. */
10215
10216 static struct type *
10217 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
10218 {
10219 struct type *type;
10220 struct type *to_type;
10221 struct type *domain;
10222
10223 to_type = die_type (die, cu);
10224 domain = die_containing_type (die, cu);
10225
10226 /* The calls 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 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
10232 type = lookup_methodptr_type (to_type);
10233 else
10234 type = lookup_memberptr_type (to_type, domain);
10235
10236 return set_die_type (die, type, cu);
10237 }
10238
10239 /* Extract all information from a DW_TAG_reference_type DIE and add to
10240 the user defined type vector. */
10241
10242 static struct type *
10243 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
10244 {
10245 struct comp_unit_head *cu_header = &cu->header;
10246 struct type *type, *target_type;
10247 struct attribute *attr;
10248
10249 target_type = die_type (die, cu);
10250
10251 /* The die_type call above may have already set the type for this DIE. */
10252 type = get_die_type (die, cu);
10253 if (type)
10254 return type;
10255
10256 type = lookup_reference_type (target_type);
10257 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10258 if (attr)
10259 {
10260 TYPE_LENGTH (type) = DW_UNSND (attr);
10261 }
10262 else
10263 {
10264 TYPE_LENGTH (type) = cu_header->addr_size;
10265 }
10266 return set_die_type (die, type, cu);
10267 }
10268
10269 static struct type *
10270 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
10271 {
10272 struct type *base_type, *cv_type;
10273
10274 base_type = die_type (die, cu);
10275
10276 /* The die_type call above may have already set the type for this DIE. */
10277 cv_type = get_die_type (die, cu);
10278 if (cv_type)
10279 return cv_type;
10280
10281 /* In case the const qualifier is applied to an array type, the element type
10282 is so qualified, not the array type (section 6.7.3 of C99). */
10283 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
10284 {
10285 struct type *el_type, *inner_array;
10286
10287 base_type = copy_type (base_type);
10288 inner_array = base_type;
10289
10290 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
10291 {
10292 TYPE_TARGET_TYPE (inner_array) =
10293 copy_type (TYPE_TARGET_TYPE (inner_array));
10294 inner_array = TYPE_TARGET_TYPE (inner_array);
10295 }
10296
10297 el_type = TYPE_TARGET_TYPE (inner_array);
10298 TYPE_TARGET_TYPE (inner_array) =
10299 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
10300
10301 return set_die_type (die, base_type, cu);
10302 }
10303
10304 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
10305 return set_die_type (die, cv_type, cu);
10306 }
10307
10308 static struct type *
10309 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
10310 {
10311 struct type *base_type, *cv_type;
10312
10313 base_type = die_type (die, cu);
10314
10315 /* The die_type call above may have already set the type for this DIE. */
10316 cv_type = get_die_type (die, cu);
10317 if (cv_type)
10318 return cv_type;
10319
10320 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
10321 return set_die_type (die, cv_type, cu);
10322 }
10323
10324 /* Extract all information from a DW_TAG_string_type DIE and add to
10325 the user defined type vector. It isn't really a user defined type,
10326 but it behaves like one, with other DIE's using an AT_user_def_type
10327 attribute to reference it. */
10328
10329 static struct type *
10330 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
10331 {
10332 struct objfile *objfile = cu->objfile;
10333 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10334 struct type *type, *range_type, *index_type, *char_type;
10335 struct attribute *attr;
10336 unsigned int length;
10337
10338 attr = dwarf2_attr (die, DW_AT_string_length, cu);
10339 if (attr)
10340 {
10341 length = DW_UNSND (attr);
10342 }
10343 else
10344 {
10345 /* Check for the DW_AT_byte_size attribute. */
10346 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10347 if (attr)
10348 {
10349 length = DW_UNSND (attr);
10350 }
10351 else
10352 {
10353 length = 1;
10354 }
10355 }
10356
10357 index_type = objfile_type (objfile)->builtin_int;
10358 range_type = create_range_type (NULL, index_type, 1, length);
10359 char_type = language_string_char_type (cu->language_defn, gdbarch);
10360 type = create_string_type (NULL, char_type, range_type);
10361
10362 return set_die_type (die, type, cu);
10363 }
10364
10365 /* Handle DIES due to C code like:
10366
10367 struct foo
10368 {
10369 int (*funcp)(int a, long l);
10370 int b;
10371 };
10372
10373 ('funcp' generates a DW_TAG_subroutine_type DIE). */
10374
10375 static struct type *
10376 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
10377 {
10378 struct objfile *objfile = cu->objfile;
10379 struct type *type; /* Type that this function returns. */
10380 struct type *ftype; /* Function that returns above type. */
10381 struct attribute *attr;
10382
10383 type = die_type (die, cu);
10384
10385 /* The die_type call above may have already set the type for this DIE. */
10386 ftype = get_die_type (die, cu);
10387 if (ftype)
10388 return ftype;
10389
10390 ftype = lookup_function_type (type);
10391
10392 /* All functions in C++, Pascal and Java have prototypes. */
10393 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
10394 if ((attr && (DW_UNSND (attr) != 0))
10395 || cu->language == language_cplus
10396 || cu->language == language_java
10397 || cu->language == language_pascal)
10398 TYPE_PROTOTYPED (ftype) = 1;
10399 else if (producer_is_realview (cu->producer))
10400 /* RealView does not emit DW_AT_prototyped. We can not
10401 distinguish prototyped and unprototyped functions; default to
10402 prototyped, since that is more common in modern code (and
10403 RealView warns about unprototyped functions). */
10404 TYPE_PROTOTYPED (ftype) = 1;
10405
10406 /* Store the calling convention in the type if it's available in
10407 the subroutine die. Otherwise set the calling convention to
10408 the default value DW_CC_normal. */
10409 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
10410 if (attr)
10411 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
10412 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
10413 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
10414 else
10415 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
10416
10417 /* We need to add the subroutine type to the die immediately so
10418 we don't infinitely recurse when dealing with parameters
10419 declared as the same subroutine type. */
10420 set_die_type (die, ftype, cu);
10421
10422 if (die->child != NULL)
10423 {
10424 struct type *void_type = objfile_type (objfile)->builtin_void;
10425 struct die_info *child_die;
10426 int nparams, iparams;
10427
10428 /* Count the number of parameters.
10429 FIXME: GDB currently ignores vararg functions, but knows about
10430 vararg member functions. */
10431 nparams = 0;
10432 child_die = die->child;
10433 while (child_die && child_die->tag)
10434 {
10435 if (child_die->tag == DW_TAG_formal_parameter)
10436 nparams++;
10437 else if (child_die->tag == DW_TAG_unspecified_parameters)
10438 TYPE_VARARGS (ftype) = 1;
10439 child_die = sibling_die (child_die);
10440 }
10441
10442 /* Allocate storage for parameters and fill them in. */
10443 TYPE_NFIELDS (ftype) = nparams;
10444 TYPE_FIELDS (ftype) = (struct field *)
10445 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
10446
10447 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
10448 even if we error out during the parameters reading below. */
10449 for (iparams = 0; iparams < nparams; iparams++)
10450 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
10451
10452 iparams = 0;
10453 child_die = die->child;
10454 while (child_die && child_die->tag)
10455 {
10456 if (child_die->tag == DW_TAG_formal_parameter)
10457 {
10458 struct type *arg_type;
10459
10460 /* DWARF version 2 has no clean way to discern C++
10461 static and non-static member functions. G++ helps
10462 GDB by marking the first parameter for non-static
10463 member functions (which is the this pointer) as
10464 artificial. We pass this information to
10465 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
10466
10467 DWARF version 3 added DW_AT_object_pointer, which GCC
10468 4.5 does not yet generate. */
10469 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
10470 if (attr)
10471 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
10472 else
10473 {
10474 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
10475
10476 /* GCC/43521: In java, the formal parameter
10477 "this" is sometimes not marked with DW_AT_artificial. */
10478 if (cu->language == language_java)
10479 {
10480 const char *name = dwarf2_name (child_die, cu);
10481
10482 if (name && !strcmp (name, "this"))
10483 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
10484 }
10485 }
10486 arg_type = die_type (child_die, cu);
10487
10488 /* RealView does not mark THIS as const, which the testsuite
10489 expects. GCC marks THIS as const in method definitions,
10490 but not in the class specifications (GCC PR 43053). */
10491 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
10492 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
10493 {
10494 int is_this = 0;
10495 struct dwarf2_cu *arg_cu = cu;
10496 const char *name = dwarf2_name (child_die, cu);
10497
10498 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
10499 if (attr)
10500 {
10501 /* If the compiler emits this, use it. */
10502 if (follow_die_ref (die, attr, &arg_cu) == child_die)
10503 is_this = 1;
10504 }
10505 else if (name && strcmp (name, "this") == 0)
10506 /* Function definitions will have the argument names. */
10507 is_this = 1;
10508 else if (name == NULL && iparams == 0)
10509 /* Declarations may not have the names, so like
10510 elsewhere in GDB, assume an artificial first
10511 argument is "this". */
10512 is_this = 1;
10513
10514 if (is_this)
10515 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
10516 arg_type, 0);
10517 }
10518
10519 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
10520 iparams++;
10521 }
10522 child_die = sibling_die (child_die);
10523 }
10524 }
10525
10526 return ftype;
10527 }
10528
10529 static struct type *
10530 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
10531 {
10532 struct objfile *objfile = cu->objfile;
10533 const char *name = NULL;
10534 struct type *this_type, *target_type;
10535
10536 name = dwarf2_full_name (NULL, die, cu);
10537 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
10538 TYPE_FLAG_TARGET_STUB, NULL, objfile);
10539 TYPE_NAME (this_type) = (char *) name;
10540 set_die_type (die, this_type, cu);
10541 target_type = die_type (die, cu);
10542 if (target_type != this_type)
10543 TYPE_TARGET_TYPE (this_type) = target_type;
10544 else
10545 {
10546 /* Self-referential typedefs are, it seems, not allowed by the DWARF
10547 spec and cause infinite loops in GDB. */
10548 complaint (&symfile_complaints,
10549 _("Self-referential DW_TAG_typedef "
10550 "- DIE at 0x%x [in module %s]"),
10551 die->offset.sect_off, objfile->name);
10552 TYPE_TARGET_TYPE (this_type) = NULL;
10553 }
10554 return this_type;
10555 }
10556
10557 /* Find a representation of a given base type and install
10558 it in the TYPE field of the die. */
10559
10560 static struct type *
10561 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
10562 {
10563 struct objfile *objfile = cu->objfile;
10564 struct type *type;
10565 struct attribute *attr;
10566 int encoding = 0, size = 0;
10567 char *name;
10568 enum type_code code = TYPE_CODE_INT;
10569 int type_flags = 0;
10570 struct type *target_type = NULL;
10571
10572 attr = dwarf2_attr (die, DW_AT_encoding, cu);
10573 if (attr)
10574 {
10575 encoding = DW_UNSND (attr);
10576 }
10577 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10578 if (attr)
10579 {
10580 size = DW_UNSND (attr);
10581 }
10582 name = dwarf2_name (die, cu);
10583 if (!name)
10584 {
10585 complaint (&symfile_complaints,
10586 _("DW_AT_name missing from DW_TAG_base_type"));
10587 }
10588
10589 switch (encoding)
10590 {
10591 case DW_ATE_address:
10592 /* Turn DW_ATE_address into a void * pointer. */
10593 code = TYPE_CODE_PTR;
10594 type_flags |= TYPE_FLAG_UNSIGNED;
10595 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
10596 break;
10597 case DW_ATE_boolean:
10598 code = TYPE_CODE_BOOL;
10599 type_flags |= TYPE_FLAG_UNSIGNED;
10600 break;
10601 case DW_ATE_complex_float:
10602 code = TYPE_CODE_COMPLEX;
10603 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
10604 break;
10605 case DW_ATE_decimal_float:
10606 code = TYPE_CODE_DECFLOAT;
10607 break;
10608 case DW_ATE_float:
10609 code = TYPE_CODE_FLT;
10610 break;
10611 case DW_ATE_signed:
10612 break;
10613 case DW_ATE_unsigned:
10614 type_flags |= TYPE_FLAG_UNSIGNED;
10615 if (cu->language == language_fortran
10616 && name
10617 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
10618 code = TYPE_CODE_CHAR;
10619 break;
10620 case DW_ATE_signed_char:
10621 if (cu->language == language_ada || cu->language == language_m2
10622 || cu->language == language_pascal
10623 || cu->language == language_fortran)
10624 code = TYPE_CODE_CHAR;
10625 break;
10626 case DW_ATE_unsigned_char:
10627 if (cu->language == language_ada || cu->language == language_m2
10628 || cu->language == language_pascal
10629 || cu->language == language_fortran)
10630 code = TYPE_CODE_CHAR;
10631 type_flags |= TYPE_FLAG_UNSIGNED;
10632 break;
10633 case DW_ATE_UTF:
10634 /* We just treat this as an integer and then recognize the
10635 type by name elsewhere. */
10636 break;
10637
10638 default:
10639 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
10640 dwarf_type_encoding_name (encoding));
10641 break;
10642 }
10643
10644 type = init_type (code, size, type_flags, NULL, objfile);
10645 TYPE_NAME (type) = name;
10646 TYPE_TARGET_TYPE (type) = target_type;
10647
10648 if (name && strcmp (name, "char") == 0)
10649 TYPE_NOSIGN (type) = 1;
10650
10651 return set_die_type (die, type, cu);
10652 }
10653
10654 /* Read the given DW_AT_subrange DIE. */
10655
10656 static struct type *
10657 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
10658 {
10659 struct type *base_type;
10660 struct type *range_type;
10661 struct attribute *attr;
10662 LONGEST low, high;
10663 int low_default_is_valid;
10664 char *name;
10665 LONGEST negative_mask;
10666
10667 base_type = die_type (die, cu);
10668 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
10669 check_typedef (base_type);
10670
10671 /* The die_type call above may have already set the type for this DIE. */
10672 range_type = get_die_type (die, cu);
10673 if (range_type)
10674 return range_type;
10675
10676 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
10677 omitting DW_AT_lower_bound. */
10678 switch (cu->language)
10679 {
10680 case language_c:
10681 case language_cplus:
10682 low = 0;
10683 low_default_is_valid = 1;
10684 break;
10685 case language_fortran:
10686 low = 1;
10687 low_default_is_valid = 1;
10688 break;
10689 case language_d:
10690 case language_java:
10691 case language_objc:
10692 low = 0;
10693 low_default_is_valid = (cu->header.version >= 4);
10694 break;
10695 case language_ada:
10696 case language_m2:
10697 case language_pascal:
10698 low = 1;
10699 low_default_is_valid = (cu->header.version >= 4);
10700 break;
10701 default:
10702 low = 0;
10703 low_default_is_valid = 0;
10704 break;
10705 }
10706
10707 /* FIXME: For variable sized arrays either of these could be
10708 a variable rather than a constant value. We'll allow it,
10709 but we don't know how to handle it. */
10710 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
10711 if (attr)
10712 low = dwarf2_get_attr_constant_value (attr, low);
10713 else if (!low_default_is_valid)
10714 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
10715 "- DIE at 0x%x [in module %s]"),
10716 die->offset.sect_off, cu->objfile->name);
10717
10718 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
10719 if (attr)
10720 {
10721 if (attr_form_is_block (attr) || is_ref_attr (attr))
10722 {
10723 /* GCC encodes arrays with unspecified or dynamic length
10724 with a DW_FORM_block1 attribute or a reference attribute.
10725 FIXME: GDB does not yet know how to handle dynamic
10726 arrays properly, treat them as arrays with unspecified
10727 length for now.
10728
10729 FIXME: jimb/2003-09-22: GDB does not really know
10730 how to handle arrays of unspecified length
10731 either; we just represent them as zero-length
10732 arrays. Choose an appropriate upper bound given
10733 the lower bound we've computed above. */
10734 high = low - 1;
10735 }
10736 else
10737 high = dwarf2_get_attr_constant_value (attr, 1);
10738 }
10739 else
10740 {
10741 attr = dwarf2_attr (die, DW_AT_count, cu);
10742 if (attr)
10743 {
10744 int count = dwarf2_get_attr_constant_value (attr, 1);
10745 high = low + count - 1;
10746 }
10747 else
10748 {
10749 /* Unspecified array length. */
10750 high = low - 1;
10751 }
10752 }
10753
10754 /* Dwarf-2 specifications explicitly allows to create subrange types
10755 without specifying a base type.
10756 In that case, the base type must be set to the type of
10757 the lower bound, upper bound or count, in that order, if any of these
10758 three attributes references an object that has a type.
10759 If no base type is found, the Dwarf-2 specifications say that
10760 a signed integer type of size equal to the size of an address should
10761 be used.
10762 For the following C code: `extern char gdb_int [];'
10763 GCC produces an empty range DIE.
10764 FIXME: muller/2010-05-28: Possible references to object for low bound,
10765 high bound or count are not yet handled by this code. */
10766 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
10767 {
10768 struct objfile *objfile = cu->objfile;
10769 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10770 int addr_size = gdbarch_addr_bit (gdbarch) /8;
10771 struct type *int_type = objfile_type (objfile)->builtin_int;
10772
10773 /* Test "int", "long int", and "long long int" objfile types,
10774 and select the first one having a size above or equal to the
10775 architecture address size. */
10776 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10777 base_type = int_type;
10778 else
10779 {
10780 int_type = objfile_type (objfile)->builtin_long;
10781 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10782 base_type = int_type;
10783 else
10784 {
10785 int_type = objfile_type (objfile)->builtin_long_long;
10786 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10787 base_type = int_type;
10788 }
10789 }
10790 }
10791
10792 negative_mask =
10793 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
10794 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
10795 low |= negative_mask;
10796 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
10797 high |= negative_mask;
10798
10799 range_type = create_range_type (NULL, base_type, low, high);
10800
10801 /* Mark arrays with dynamic length at least as an array of unspecified
10802 length. GDB could check the boundary but before it gets implemented at
10803 least allow accessing the array elements. */
10804 if (attr && attr_form_is_block (attr))
10805 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
10806
10807 /* Ada expects an empty array on no boundary attributes. */
10808 if (attr == NULL && cu->language != language_ada)
10809 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
10810
10811 name = dwarf2_name (die, cu);
10812 if (name)
10813 TYPE_NAME (range_type) = name;
10814
10815 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10816 if (attr)
10817 TYPE_LENGTH (range_type) = DW_UNSND (attr);
10818
10819 set_die_type (die, range_type, cu);
10820
10821 /* set_die_type should be already done. */
10822 set_descriptive_type (range_type, die, cu);
10823
10824 return range_type;
10825 }
10826
10827 static struct type *
10828 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
10829 {
10830 struct type *type;
10831
10832 /* For now, we only support the C meaning of an unspecified type: void. */
10833
10834 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
10835 TYPE_NAME (type) = dwarf2_name (die, cu);
10836
10837 return set_die_type (die, type, cu);
10838 }
10839
10840 /* Read a single die and all its descendents. Set the die's sibling
10841 field to NULL; set other fields in the die correctly, and set all
10842 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
10843 location of the info_ptr after reading all of those dies. PARENT
10844 is the parent of the die in question. */
10845
10846 static struct die_info *
10847 read_die_and_children (const struct die_reader_specs *reader,
10848 gdb_byte *info_ptr,
10849 gdb_byte **new_info_ptr,
10850 struct die_info *parent)
10851 {
10852 struct die_info *die;
10853 gdb_byte *cur_ptr;
10854 int has_children;
10855
10856 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
10857 if (die == NULL)
10858 {
10859 *new_info_ptr = cur_ptr;
10860 return NULL;
10861 }
10862 store_in_ref_table (die, reader->cu);
10863
10864 if (has_children)
10865 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
10866 else
10867 {
10868 die->child = NULL;
10869 *new_info_ptr = cur_ptr;
10870 }
10871
10872 die->sibling = NULL;
10873 die->parent = parent;
10874 return die;
10875 }
10876
10877 /* Read a die, all of its descendents, and all of its siblings; set
10878 all of the fields of all of the dies correctly. Arguments are as
10879 in read_die_and_children. */
10880
10881 static struct die_info *
10882 read_die_and_siblings (const struct die_reader_specs *reader,
10883 gdb_byte *info_ptr,
10884 gdb_byte **new_info_ptr,
10885 struct die_info *parent)
10886 {
10887 struct die_info *first_die, *last_sibling;
10888 gdb_byte *cur_ptr;
10889
10890 cur_ptr = info_ptr;
10891 first_die = last_sibling = NULL;
10892
10893 while (1)
10894 {
10895 struct die_info *die
10896 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
10897
10898 if (die == NULL)
10899 {
10900 *new_info_ptr = cur_ptr;
10901 return first_die;
10902 }
10903
10904 if (!first_die)
10905 first_die = die;
10906 else
10907 last_sibling->sibling = die;
10908
10909 last_sibling = die;
10910 }
10911 }
10912
10913 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
10914 attributes.
10915 The caller is responsible for filling in the extra attributes
10916 and updating (*DIEP)->num_attrs.
10917 Set DIEP to point to a newly allocated die with its information,
10918 except for its child, sibling, and parent fields.
10919 Set HAS_CHILDREN to tell whether the die has children or not. */
10920
10921 static gdb_byte *
10922 read_full_die_1 (const struct die_reader_specs *reader,
10923 struct die_info **diep, gdb_byte *info_ptr,
10924 int *has_children, int num_extra_attrs)
10925 {
10926 unsigned int abbrev_number, bytes_read, i;
10927 sect_offset offset;
10928 struct abbrev_info *abbrev;
10929 struct die_info *die;
10930 struct dwarf2_cu *cu = reader->cu;
10931 bfd *abfd = reader->abfd;
10932
10933 offset.sect_off = info_ptr - reader->buffer;
10934 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10935 info_ptr += bytes_read;
10936 if (!abbrev_number)
10937 {
10938 *diep = NULL;
10939 *has_children = 0;
10940 return info_ptr;
10941 }
10942
10943 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
10944 if (!abbrev)
10945 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
10946 abbrev_number,
10947 bfd_get_filename (abfd));
10948
10949 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
10950 die->offset = offset;
10951 die->tag = abbrev->tag;
10952 die->abbrev = abbrev_number;
10953
10954 /* Make the result usable.
10955 The caller needs to update num_attrs after adding the extra
10956 attributes. */
10957 die->num_attrs = abbrev->num_attrs;
10958
10959 for (i = 0; i < abbrev->num_attrs; ++i)
10960 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
10961 info_ptr);
10962
10963 *diep = die;
10964 *has_children = abbrev->has_children;
10965 return info_ptr;
10966 }
10967
10968 /* Read a die and all its attributes.
10969 Set DIEP to point to a newly allocated die with its information,
10970 except for its child, sibling, and parent fields.
10971 Set HAS_CHILDREN to tell whether the die has children or not. */
10972
10973 static gdb_byte *
10974 read_full_die (const struct die_reader_specs *reader,
10975 struct die_info **diep, gdb_byte *info_ptr,
10976 int *has_children)
10977 {
10978 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
10979 }
10980 \f
10981 /* Abbreviation tables.
10982
10983 In DWARF version 2, the description of the debugging information is
10984 stored in a separate .debug_abbrev section. Before we read any
10985 dies from a section we read in all abbreviations and install them
10986 in a hash table. */
10987
10988 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
10989
10990 static struct abbrev_info *
10991 abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
10992 {
10993 struct abbrev_info *abbrev;
10994
10995 abbrev = (struct abbrev_info *)
10996 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
10997 memset (abbrev, 0, sizeof (struct abbrev_info));
10998 return abbrev;
10999 }
11000
11001 /* Add an abbreviation to the table. */
11002
11003 static void
11004 abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
11005 unsigned int abbrev_number,
11006 struct abbrev_info *abbrev)
11007 {
11008 unsigned int hash_number;
11009
11010 hash_number = abbrev_number % ABBREV_HASH_SIZE;
11011 abbrev->next = abbrev_table->abbrevs[hash_number];
11012 abbrev_table->abbrevs[hash_number] = abbrev;
11013 }
11014
11015 /* Look up an abbrev in the table.
11016 Returns NULL if the abbrev is not found. */
11017
11018 static struct abbrev_info *
11019 abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
11020 unsigned int abbrev_number)
11021 {
11022 unsigned int hash_number;
11023 struct abbrev_info *abbrev;
11024
11025 hash_number = abbrev_number % ABBREV_HASH_SIZE;
11026 abbrev = abbrev_table->abbrevs[hash_number];
11027
11028 while (abbrev)
11029 {
11030 if (abbrev->number == abbrev_number)
11031 return abbrev;
11032 abbrev = abbrev->next;
11033 }
11034 return NULL;
11035 }
11036
11037 /* Read in an abbrev table. */
11038
11039 static struct abbrev_table *
11040 abbrev_table_read_table (struct dwarf2_section_info *section,
11041 sect_offset offset)
11042 {
11043 struct objfile *objfile = dwarf2_per_objfile->objfile;
11044 bfd *abfd = section->asection->owner;
11045 struct abbrev_table *abbrev_table;
11046 gdb_byte *abbrev_ptr;
11047 struct abbrev_info *cur_abbrev;
11048 unsigned int abbrev_number, bytes_read, abbrev_name;
11049 unsigned int abbrev_form;
11050 struct attr_abbrev *cur_attrs;
11051 unsigned int allocated_attrs;
11052
11053 abbrev_table = XMALLOC (struct abbrev_table);
11054 obstack_init (&abbrev_table->abbrev_obstack);
11055 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
11056 (ABBREV_HASH_SIZE
11057 * sizeof (struct abbrev_info *)));
11058 memset (abbrev_table->abbrevs, 0,
11059 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
11060
11061 dwarf2_read_section (objfile, section);
11062 abbrev_ptr = section->buffer + offset.sect_off;
11063 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
11064 abbrev_ptr += bytes_read;
11065
11066 allocated_attrs = ATTR_ALLOC_CHUNK;
11067 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
11068
11069 /* Loop until we reach an abbrev number of 0. */
11070 while (abbrev_number)
11071 {
11072 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
11073
11074 /* read in abbrev header */
11075 cur_abbrev->number = abbrev_number;
11076 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
11077 abbrev_ptr += bytes_read;
11078 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
11079 abbrev_ptr += 1;
11080
11081 /* now read in declarations */
11082 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
11083 abbrev_ptr += bytes_read;
11084 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
11085 abbrev_ptr += bytes_read;
11086 while (abbrev_name)
11087 {
11088 if (cur_abbrev->num_attrs == allocated_attrs)
11089 {
11090 allocated_attrs += ATTR_ALLOC_CHUNK;
11091 cur_attrs
11092 = xrealloc (cur_attrs, (allocated_attrs
11093 * sizeof (struct attr_abbrev)));
11094 }
11095
11096 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
11097 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
11098 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
11099 abbrev_ptr += bytes_read;
11100 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
11101 abbrev_ptr += bytes_read;
11102 }
11103
11104 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
11105 (cur_abbrev->num_attrs
11106 * sizeof (struct attr_abbrev)));
11107 memcpy (cur_abbrev->attrs, cur_attrs,
11108 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
11109
11110 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
11111
11112 /* Get next abbreviation.
11113 Under Irix6 the abbreviations for a compilation unit are not
11114 always properly terminated with an abbrev number of 0.
11115 Exit loop if we encounter an abbreviation which we have
11116 already read (which means we are about to read the abbreviations
11117 for the next compile unit) or if the end of the abbreviation
11118 table is reached. */
11119 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
11120 break;
11121 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
11122 abbrev_ptr += bytes_read;
11123 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
11124 break;
11125 }
11126
11127 xfree (cur_attrs);
11128 return abbrev_table;
11129 }
11130
11131 /* Free the resources held by ABBREV_TABLE. */
11132
11133 static void
11134 abbrev_table_free (struct abbrev_table *abbrev_table)
11135 {
11136 obstack_free (&abbrev_table->abbrev_obstack, NULL);
11137 xfree (abbrev_table);
11138 }
11139
11140 /* Read the abbrev table for CU from ABBREV_SECTION. */
11141
11142 static void
11143 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
11144 struct dwarf2_section_info *abbrev_section)
11145
11146 {
11147 cu->abbrev_table =
11148 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
11149 }
11150
11151 /* Release the memory used by the abbrev table for a compilation unit. */
11152
11153 static void
11154 dwarf2_free_abbrev_table (void *ptr_to_cu)
11155 {
11156 struct dwarf2_cu *cu = ptr_to_cu;
11157
11158 abbrev_table_free (cu->abbrev_table);
11159 /* Set this to NULL so that we SEGV if we try to read it later,
11160 and also because free_comp_unit verifies this is NULL. */
11161 cu->abbrev_table = NULL;
11162 }
11163 \f
11164 /* Returns nonzero if TAG represents a type that we might generate a partial
11165 symbol for. */
11166
11167 static int
11168 is_type_tag_for_partial (int tag)
11169 {
11170 switch (tag)
11171 {
11172 #if 0
11173 /* Some types that would be reasonable to generate partial symbols for,
11174 that we don't at present. */
11175 case DW_TAG_array_type:
11176 case DW_TAG_file_type:
11177 case DW_TAG_ptr_to_member_type:
11178 case DW_TAG_set_type:
11179 case DW_TAG_string_type:
11180 case DW_TAG_subroutine_type:
11181 #endif
11182 case DW_TAG_base_type:
11183 case DW_TAG_class_type:
11184 case DW_TAG_interface_type:
11185 case DW_TAG_enumeration_type:
11186 case DW_TAG_structure_type:
11187 case DW_TAG_subrange_type:
11188 case DW_TAG_typedef:
11189 case DW_TAG_union_type:
11190 return 1;
11191 default:
11192 return 0;
11193 }
11194 }
11195
11196 /* Load all DIEs that are interesting for partial symbols into memory. */
11197
11198 static struct partial_die_info *
11199 load_partial_dies (const struct die_reader_specs *reader,
11200 gdb_byte *info_ptr, int building_psymtab)
11201 {
11202 struct dwarf2_cu *cu = reader->cu;
11203 struct objfile *objfile = cu->objfile;
11204 struct partial_die_info *part_die;
11205 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
11206 struct abbrev_info *abbrev;
11207 unsigned int bytes_read;
11208 unsigned int load_all = 0;
11209 int nesting_level = 1;
11210
11211 parent_die = NULL;
11212 last_die = NULL;
11213
11214 gdb_assert (cu->per_cu != NULL);
11215 if (cu->per_cu->load_all_dies)
11216 load_all = 1;
11217
11218 cu->partial_dies
11219 = htab_create_alloc_ex (cu->header.length / 12,
11220 partial_die_hash,
11221 partial_die_eq,
11222 NULL,
11223 &cu->comp_unit_obstack,
11224 hashtab_obstack_allocate,
11225 dummy_obstack_deallocate);
11226
11227 part_die = obstack_alloc (&cu->comp_unit_obstack,
11228 sizeof (struct partial_die_info));
11229
11230 while (1)
11231 {
11232 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
11233
11234 /* A NULL abbrev means the end of a series of children. */
11235 if (abbrev == NULL)
11236 {
11237 if (--nesting_level == 0)
11238 {
11239 /* PART_DIE was probably the last thing allocated on the
11240 comp_unit_obstack, so we could call obstack_free
11241 here. We don't do that because the waste is small,
11242 and will be cleaned up when we're done with this
11243 compilation unit. This way, we're also more robust
11244 against other users of the comp_unit_obstack. */
11245 return first_die;
11246 }
11247 info_ptr += bytes_read;
11248 last_die = parent_die;
11249 parent_die = parent_die->die_parent;
11250 continue;
11251 }
11252
11253 /* Check for template arguments. We never save these; if
11254 they're seen, we just mark the parent, and go on our way. */
11255 if (parent_die != NULL
11256 && cu->language == language_cplus
11257 && (abbrev->tag == DW_TAG_template_type_param
11258 || abbrev->tag == DW_TAG_template_value_param))
11259 {
11260 parent_die->has_template_arguments = 1;
11261
11262 if (!load_all)
11263 {
11264 /* We don't need a partial DIE for the template argument. */
11265 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
11266 continue;
11267 }
11268 }
11269
11270 /* We only recurse into c++ subprograms looking for template arguments.
11271 Skip their other children. */
11272 if (!load_all
11273 && cu->language == language_cplus
11274 && parent_die != NULL
11275 && parent_die->tag == DW_TAG_subprogram)
11276 {
11277 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
11278 continue;
11279 }
11280
11281 /* Check whether this DIE is interesting enough to save. Normally
11282 we would not be interested in members here, but there may be
11283 later variables referencing them via DW_AT_specification (for
11284 static members). */
11285 if (!load_all
11286 && !is_type_tag_for_partial (abbrev->tag)
11287 && abbrev->tag != DW_TAG_constant
11288 && abbrev->tag != DW_TAG_enumerator
11289 && abbrev->tag != DW_TAG_subprogram
11290 && abbrev->tag != DW_TAG_lexical_block
11291 && abbrev->tag != DW_TAG_variable
11292 && abbrev->tag != DW_TAG_namespace
11293 && abbrev->tag != DW_TAG_module
11294 && abbrev->tag != DW_TAG_member
11295 && abbrev->tag != DW_TAG_imported_unit)
11296 {
11297 /* Otherwise we skip to the next sibling, if any. */
11298 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
11299 continue;
11300 }
11301
11302 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
11303 info_ptr);
11304
11305 /* This two-pass algorithm for processing partial symbols has a
11306 high cost in cache pressure. Thus, handle some simple cases
11307 here which cover the majority of C partial symbols. DIEs
11308 which neither have specification tags in them, nor could have
11309 specification tags elsewhere pointing at them, can simply be
11310 processed and discarded.
11311
11312 This segment is also optional; scan_partial_symbols and
11313 add_partial_symbol will handle these DIEs if we chain
11314 them in normally. When compilers which do not emit large
11315 quantities of duplicate debug information are more common,
11316 this code can probably be removed. */
11317
11318 /* Any complete simple types at the top level (pretty much all
11319 of them, for a language without namespaces), can be processed
11320 directly. */
11321 if (parent_die == NULL
11322 && part_die->has_specification == 0
11323 && part_die->is_declaration == 0
11324 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
11325 || part_die->tag == DW_TAG_base_type
11326 || part_die->tag == DW_TAG_subrange_type))
11327 {
11328 if (building_psymtab && part_die->name != NULL)
11329 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
11330 VAR_DOMAIN, LOC_TYPEDEF,
11331 &objfile->static_psymbols,
11332 0, (CORE_ADDR) 0, cu->language, objfile);
11333 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
11334 continue;
11335 }
11336
11337 /* The exception for DW_TAG_typedef with has_children above is
11338 a workaround of GCC PR debug/47510. In the case of this complaint
11339 type_name_no_tag_or_error will error on such types later.
11340
11341 GDB skipped children of DW_TAG_typedef by the shortcut above and then
11342 it could not find the child DIEs referenced later, this is checked
11343 above. In correct DWARF DW_TAG_typedef should have no children. */
11344
11345 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
11346 complaint (&symfile_complaints,
11347 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
11348 "- DIE at 0x%x [in module %s]"),
11349 part_die->offset.sect_off, objfile->name);
11350
11351 /* If we're at the second level, and we're an enumerator, and
11352 our parent has no specification (meaning possibly lives in a
11353 namespace elsewhere), then we can add the partial symbol now
11354 instead of queueing it. */
11355 if (part_die->tag == DW_TAG_enumerator
11356 && parent_die != NULL
11357 && parent_die->die_parent == NULL
11358 && parent_die->tag == DW_TAG_enumeration_type
11359 && parent_die->has_specification == 0)
11360 {
11361 if (part_die->name == NULL)
11362 complaint (&symfile_complaints,
11363 _("malformed enumerator DIE ignored"));
11364 else if (building_psymtab)
11365 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
11366 VAR_DOMAIN, LOC_CONST,
11367 (cu->language == language_cplus
11368 || cu->language == language_java)
11369 ? &objfile->global_psymbols
11370 : &objfile->static_psymbols,
11371 0, (CORE_ADDR) 0, cu->language, objfile);
11372
11373 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
11374 continue;
11375 }
11376
11377 /* We'll save this DIE so link it in. */
11378 part_die->die_parent = parent_die;
11379 part_die->die_sibling = NULL;
11380 part_die->die_child = NULL;
11381
11382 if (last_die && last_die == parent_die)
11383 last_die->die_child = part_die;
11384 else if (last_die)
11385 last_die->die_sibling = part_die;
11386
11387 last_die = part_die;
11388
11389 if (first_die == NULL)
11390 first_die = part_die;
11391
11392 /* Maybe add the DIE to the hash table. Not all DIEs that we
11393 find interesting need to be in the hash table, because we
11394 also have the parent/sibling/child chains; only those that we
11395 might refer to by offset later during partial symbol reading.
11396
11397 For now this means things that might have be the target of a
11398 DW_AT_specification, DW_AT_abstract_origin, or
11399 DW_AT_extension. DW_AT_extension will refer only to
11400 namespaces; DW_AT_abstract_origin refers to functions (and
11401 many things under the function DIE, but we do not recurse
11402 into function DIEs during partial symbol reading) and
11403 possibly variables as well; DW_AT_specification refers to
11404 declarations. Declarations ought to have the DW_AT_declaration
11405 flag. It happens that GCC forgets to put it in sometimes, but
11406 only for functions, not for types.
11407
11408 Adding more things than necessary to the hash table is harmless
11409 except for the performance cost. Adding too few will result in
11410 wasted time in find_partial_die, when we reread the compilation
11411 unit with load_all_dies set. */
11412
11413 if (load_all
11414 || abbrev->tag == DW_TAG_constant
11415 || abbrev->tag == DW_TAG_subprogram
11416 || abbrev->tag == DW_TAG_variable
11417 || abbrev->tag == DW_TAG_namespace
11418 || part_die->is_declaration)
11419 {
11420 void **slot;
11421
11422 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
11423 part_die->offset.sect_off, INSERT);
11424 *slot = part_die;
11425 }
11426
11427 part_die = obstack_alloc (&cu->comp_unit_obstack,
11428 sizeof (struct partial_die_info));
11429
11430 /* For some DIEs we want to follow their children (if any). For C
11431 we have no reason to follow the children of structures; for other
11432 languages we have to, so that we can get at method physnames
11433 to infer fully qualified class names, for DW_AT_specification,
11434 and for C++ template arguments. For C++, we also look one level
11435 inside functions to find template arguments (if the name of the
11436 function does not already contain the template arguments).
11437
11438 For Ada, we need to scan the children of subprograms and lexical
11439 blocks as well because Ada allows the definition of nested
11440 entities that could be interesting for the debugger, such as
11441 nested subprograms for instance. */
11442 if (last_die->has_children
11443 && (load_all
11444 || last_die->tag == DW_TAG_namespace
11445 || last_die->tag == DW_TAG_module
11446 || last_die->tag == DW_TAG_enumeration_type
11447 || (cu->language == language_cplus
11448 && last_die->tag == DW_TAG_subprogram
11449 && (last_die->name == NULL
11450 || strchr (last_die->name, '<') == NULL))
11451 || (cu->language != language_c
11452 && (last_die->tag == DW_TAG_class_type
11453 || last_die->tag == DW_TAG_interface_type
11454 || last_die->tag == DW_TAG_structure_type
11455 || last_die->tag == DW_TAG_union_type))
11456 || (cu->language == language_ada
11457 && (last_die->tag == DW_TAG_subprogram
11458 || last_die->tag == DW_TAG_lexical_block))))
11459 {
11460 nesting_level++;
11461 parent_die = last_die;
11462 continue;
11463 }
11464
11465 /* Otherwise we skip to the next sibling, if any. */
11466 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
11467
11468 /* Back to the top, do it again. */
11469 }
11470 }
11471
11472 /* Read a minimal amount of information into the minimal die structure. */
11473
11474 static gdb_byte *
11475 read_partial_die (const struct die_reader_specs *reader,
11476 struct partial_die_info *part_die,
11477 struct abbrev_info *abbrev, unsigned int abbrev_len,
11478 gdb_byte *info_ptr)
11479 {
11480 struct dwarf2_cu *cu = reader->cu;
11481 struct objfile *objfile = cu->objfile;
11482 gdb_byte *buffer = reader->buffer;
11483 unsigned int i;
11484 struct attribute attr;
11485 int has_low_pc_attr = 0;
11486 int has_high_pc_attr = 0;
11487 int high_pc_relative = 0;
11488
11489 memset (part_die, 0, sizeof (struct partial_die_info));
11490
11491 part_die->offset.sect_off = info_ptr - buffer;
11492
11493 info_ptr += abbrev_len;
11494
11495 if (abbrev == NULL)
11496 return info_ptr;
11497
11498 part_die->tag = abbrev->tag;
11499 part_die->has_children = abbrev->has_children;
11500
11501 for (i = 0; i < abbrev->num_attrs; ++i)
11502 {
11503 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
11504
11505 /* Store the data if it is of an attribute we want to keep in a
11506 partial symbol table. */
11507 switch (attr.name)
11508 {
11509 case DW_AT_name:
11510 switch (part_die->tag)
11511 {
11512 case DW_TAG_compile_unit:
11513 case DW_TAG_partial_unit:
11514 case DW_TAG_type_unit:
11515 /* Compilation units have a DW_AT_name that is a filename, not
11516 a source language identifier. */
11517 case DW_TAG_enumeration_type:
11518 case DW_TAG_enumerator:
11519 /* These tags always have simple identifiers already; no need
11520 to canonicalize them. */
11521 part_die->name = DW_STRING (&attr);
11522 break;
11523 default:
11524 part_die->name
11525 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
11526 &objfile->objfile_obstack);
11527 break;
11528 }
11529 break;
11530 case DW_AT_linkage_name:
11531 case DW_AT_MIPS_linkage_name:
11532 /* Note that both forms of linkage name might appear. We
11533 assume they will be the same, and we only store the last
11534 one we see. */
11535 if (cu->language == language_ada)
11536 part_die->name = DW_STRING (&attr);
11537 part_die->linkage_name = DW_STRING (&attr);
11538 break;
11539 case DW_AT_low_pc:
11540 has_low_pc_attr = 1;
11541 part_die->lowpc = DW_ADDR (&attr);
11542 break;
11543 case DW_AT_high_pc:
11544 has_high_pc_attr = 1;
11545 if (attr.form == DW_FORM_addr
11546 || attr.form == DW_FORM_GNU_addr_index)
11547 part_die->highpc = DW_ADDR (&attr);
11548 else
11549 {
11550 high_pc_relative = 1;
11551 part_die->highpc = DW_UNSND (&attr);
11552 }
11553 break;
11554 case DW_AT_location:
11555 /* Support the .debug_loc offsets. */
11556 if (attr_form_is_block (&attr))
11557 {
11558 part_die->d.locdesc = DW_BLOCK (&attr);
11559 }
11560 else if (attr_form_is_section_offset (&attr))
11561 {
11562 dwarf2_complex_location_expr_complaint ();
11563 }
11564 else
11565 {
11566 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
11567 "partial symbol information");
11568 }
11569 break;
11570 case DW_AT_external:
11571 part_die->is_external = DW_UNSND (&attr);
11572 break;
11573 case DW_AT_declaration:
11574 part_die->is_declaration = DW_UNSND (&attr);
11575 break;
11576 case DW_AT_type:
11577 part_die->has_type = 1;
11578 break;
11579 case DW_AT_abstract_origin:
11580 case DW_AT_specification:
11581 case DW_AT_extension:
11582 part_die->has_specification = 1;
11583 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
11584 break;
11585 case DW_AT_sibling:
11586 /* Ignore absolute siblings, they might point outside of
11587 the current compile unit. */
11588 if (attr.form == DW_FORM_ref_addr)
11589 complaint (&symfile_complaints,
11590 _("ignoring absolute DW_AT_sibling"));
11591 else
11592 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
11593 break;
11594 case DW_AT_byte_size:
11595 part_die->has_byte_size = 1;
11596 break;
11597 case DW_AT_calling_convention:
11598 /* DWARF doesn't provide a way to identify a program's source-level
11599 entry point. DW_AT_calling_convention attributes are only meant
11600 to describe functions' calling conventions.
11601
11602 However, because it's a necessary piece of information in
11603 Fortran, and because DW_CC_program is the only piece of debugging
11604 information whose definition refers to a 'main program' at all,
11605 several compilers have begun marking Fortran main programs with
11606 DW_CC_program --- even when those functions use the standard
11607 calling conventions.
11608
11609 So until DWARF specifies a way to provide this information and
11610 compilers pick up the new representation, we'll support this
11611 practice. */
11612 if (DW_UNSND (&attr) == DW_CC_program
11613 && cu->language == language_fortran)
11614 {
11615 set_main_name (part_die->name);
11616
11617 /* As this DIE has a static linkage the name would be difficult
11618 to look up later. */
11619 language_of_main = language_fortran;
11620 }
11621 break;
11622 case DW_AT_inline:
11623 if (DW_UNSND (&attr) == DW_INL_inlined
11624 || DW_UNSND (&attr) == DW_INL_declared_inlined)
11625 part_die->may_be_inlined = 1;
11626 break;
11627
11628 case DW_AT_import:
11629 if (part_die->tag == DW_TAG_imported_unit)
11630 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
11631 break;
11632
11633 default:
11634 break;
11635 }
11636 }
11637
11638 if (high_pc_relative)
11639 part_die->highpc += part_die->lowpc;
11640
11641 if (has_low_pc_attr && has_high_pc_attr)
11642 {
11643 /* When using the GNU linker, .gnu.linkonce. sections are used to
11644 eliminate duplicate copies of functions and vtables and such.
11645 The linker will arbitrarily choose one and discard the others.
11646 The AT_*_pc values for such functions refer to local labels in
11647 these sections. If the section from that file was discarded, the
11648 labels are not in the output, so the relocs get a value of 0.
11649 If this is a discarded function, mark the pc bounds as invalid,
11650 so that GDB will ignore it. */
11651 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
11652 {
11653 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11654
11655 complaint (&symfile_complaints,
11656 _("DW_AT_low_pc %s is zero "
11657 "for DIE at 0x%x [in module %s]"),
11658 paddress (gdbarch, part_die->lowpc),
11659 part_die->offset.sect_off, objfile->name);
11660 }
11661 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
11662 else if (part_die->lowpc >= part_die->highpc)
11663 {
11664 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11665
11666 complaint (&symfile_complaints,
11667 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
11668 "for DIE at 0x%x [in module %s]"),
11669 paddress (gdbarch, part_die->lowpc),
11670 paddress (gdbarch, part_die->highpc),
11671 part_die->offset.sect_off, objfile->name);
11672 }
11673 else
11674 part_die->has_pc_info = 1;
11675 }
11676
11677 return info_ptr;
11678 }
11679
11680 /* Find a cached partial DIE at OFFSET in CU. */
11681
11682 static struct partial_die_info *
11683 find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
11684 {
11685 struct partial_die_info *lookup_die = NULL;
11686 struct partial_die_info part_die;
11687
11688 part_die.offset = offset;
11689 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
11690 offset.sect_off);
11691
11692 return lookup_die;
11693 }
11694
11695 /* Find a partial DIE at OFFSET, which may or may not be in CU,
11696 except in the case of .debug_types DIEs which do not reference
11697 outside their CU (they do however referencing other types via
11698 DW_FORM_ref_sig8). */
11699
11700 static struct partial_die_info *
11701 find_partial_die (sect_offset offset, struct dwarf2_cu *cu)
11702 {
11703 struct objfile *objfile = cu->objfile;
11704 struct dwarf2_per_cu_data *per_cu = NULL;
11705 struct partial_die_info *pd = NULL;
11706
11707 if (offset_in_cu_p (&cu->header, offset))
11708 {
11709 pd = find_partial_die_in_comp_unit (offset, cu);
11710 if (pd != NULL)
11711 return pd;
11712 /* We missed recording what we needed.
11713 Load all dies and try again. */
11714 per_cu = cu->per_cu;
11715 }
11716 else
11717 {
11718 /* TUs don't reference other CUs/TUs (except via type signatures). */
11719 if (cu->per_cu->is_debug_types)
11720 {
11721 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
11722 " external reference to offset 0x%lx [in module %s].\n"),
11723 (long) cu->header.offset.sect_off, (long) offset.sect_off,
11724 bfd_get_filename (objfile->obfd));
11725 }
11726 per_cu = dwarf2_find_containing_comp_unit (offset, objfile);
11727
11728 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
11729 load_partial_comp_unit (per_cu);
11730
11731 per_cu->cu->last_used = 0;
11732 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
11733 }
11734
11735 /* If we didn't find it, and not all dies have been loaded,
11736 load them all and try again. */
11737
11738 if (pd == NULL && per_cu->load_all_dies == 0)
11739 {
11740 per_cu->load_all_dies = 1;
11741
11742 /* This is nasty. When we reread the DIEs, somewhere up the call chain
11743 THIS_CU->cu may already be in use. So we can't just free it and
11744 replace its DIEs with the ones we read in. Instead, we leave those
11745 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
11746 and clobber THIS_CU->cu->partial_dies with the hash table for the new
11747 set. */
11748 load_partial_comp_unit (per_cu);
11749
11750 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
11751 }
11752
11753 if (pd == NULL)
11754 internal_error (__FILE__, __LINE__,
11755 _("could not find partial DIE 0x%x "
11756 "in cache [from module %s]\n"),
11757 offset.sect_off, bfd_get_filename (objfile->obfd));
11758 return pd;
11759 }
11760
11761 /* See if we can figure out if the class lives in a namespace. We do
11762 this by looking for a member function; its demangled name will
11763 contain namespace info, if there is any. */
11764
11765 static void
11766 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
11767 struct dwarf2_cu *cu)
11768 {
11769 /* NOTE: carlton/2003-10-07: Getting the info this way changes
11770 what template types look like, because the demangler
11771 frequently doesn't give the same name as the debug info. We
11772 could fix this by only using the demangled name to get the
11773 prefix (but see comment in read_structure_type). */
11774
11775 struct partial_die_info *real_pdi;
11776 struct partial_die_info *child_pdi;
11777
11778 /* If this DIE (this DIE's specification, if any) has a parent, then
11779 we should not do this. We'll prepend the parent's fully qualified
11780 name when we create the partial symbol. */
11781
11782 real_pdi = struct_pdi;
11783 while (real_pdi->has_specification)
11784 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
11785
11786 if (real_pdi->die_parent != NULL)
11787 return;
11788
11789 for (child_pdi = struct_pdi->die_child;
11790 child_pdi != NULL;
11791 child_pdi = child_pdi->die_sibling)
11792 {
11793 if (child_pdi->tag == DW_TAG_subprogram
11794 && child_pdi->linkage_name != NULL)
11795 {
11796 char *actual_class_name
11797 = language_class_name_from_physname (cu->language_defn,
11798 child_pdi->linkage_name);
11799 if (actual_class_name != NULL)
11800 {
11801 struct_pdi->name
11802 = obsavestring (actual_class_name,
11803 strlen (actual_class_name),
11804 &cu->objfile->objfile_obstack);
11805 xfree (actual_class_name);
11806 }
11807 break;
11808 }
11809 }
11810 }
11811
11812 /* Adjust PART_DIE before generating a symbol for it. This function
11813 may set the is_external flag or change the DIE's name. */
11814
11815 static void
11816 fixup_partial_die (struct partial_die_info *part_die,
11817 struct dwarf2_cu *cu)
11818 {
11819 /* Once we've fixed up a die, there's no point in doing so again.
11820 This also avoids a memory leak if we were to call
11821 guess_partial_die_structure_name multiple times. */
11822 if (part_die->fixup_called)
11823 return;
11824
11825 /* If we found a reference attribute and the DIE has no name, try
11826 to find a name in the referred to DIE. */
11827
11828 if (part_die->name == NULL && part_die->has_specification)
11829 {
11830 struct partial_die_info *spec_die;
11831
11832 spec_die = find_partial_die (part_die->spec_offset, cu);
11833
11834 fixup_partial_die (spec_die, cu);
11835
11836 if (spec_die->name)
11837 {
11838 part_die->name = spec_die->name;
11839
11840 /* Copy DW_AT_external attribute if it is set. */
11841 if (spec_die->is_external)
11842 part_die->is_external = spec_die->is_external;
11843 }
11844 }
11845
11846 /* Set default names for some unnamed DIEs. */
11847
11848 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
11849 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
11850
11851 /* If there is no parent die to provide a namespace, and there are
11852 children, see if we can determine the namespace from their linkage
11853 name. */
11854 if (cu->language == language_cplus
11855 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
11856 && part_die->die_parent == NULL
11857 && part_die->has_children
11858 && (part_die->tag == DW_TAG_class_type
11859 || part_die->tag == DW_TAG_structure_type
11860 || part_die->tag == DW_TAG_union_type))
11861 guess_partial_die_structure_name (part_die, cu);
11862
11863 /* GCC might emit a nameless struct or union that has a linkage
11864 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
11865 if (part_die->name == NULL
11866 && (part_die->tag == DW_TAG_class_type
11867 || part_die->tag == DW_TAG_interface_type
11868 || part_die->tag == DW_TAG_structure_type
11869 || part_die->tag == DW_TAG_union_type)
11870 && part_die->linkage_name != NULL)
11871 {
11872 char *demangled;
11873
11874 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
11875 if (demangled)
11876 {
11877 const char *base;
11878
11879 /* Strip any leading namespaces/classes, keep only the base name.
11880 DW_AT_name for named DIEs does not contain the prefixes. */
11881 base = strrchr (demangled, ':');
11882 if (base && base > demangled && base[-1] == ':')
11883 base++;
11884 else
11885 base = demangled;
11886
11887 part_die->name = obsavestring (base, strlen (base),
11888 &cu->objfile->objfile_obstack);
11889 xfree (demangled);
11890 }
11891 }
11892
11893 part_die->fixup_called = 1;
11894 }
11895
11896 /* Read an attribute value described by an attribute form. */
11897
11898 static gdb_byte *
11899 read_attribute_value (const struct die_reader_specs *reader,
11900 struct attribute *attr, unsigned form,
11901 gdb_byte *info_ptr)
11902 {
11903 struct dwarf2_cu *cu = reader->cu;
11904 bfd *abfd = reader->abfd;
11905 struct comp_unit_head *cu_header = &cu->header;
11906 unsigned int bytes_read;
11907 struct dwarf_block *blk;
11908
11909 attr->form = form;
11910 switch (form)
11911 {
11912 case DW_FORM_ref_addr:
11913 if (cu->header.version == 2)
11914 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
11915 else
11916 DW_UNSND (attr) = read_offset (abfd, info_ptr,
11917 &cu->header, &bytes_read);
11918 info_ptr += bytes_read;
11919 break;
11920 case DW_FORM_addr:
11921 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
11922 info_ptr += bytes_read;
11923 break;
11924 case DW_FORM_block2:
11925 blk = dwarf_alloc_block (cu);
11926 blk->size = read_2_bytes (abfd, info_ptr);
11927 info_ptr += 2;
11928 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11929 info_ptr += blk->size;
11930 DW_BLOCK (attr) = blk;
11931 break;
11932 case DW_FORM_block4:
11933 blk = dwarf_alloc_block (cu);
11934 blk->size = read_4_bytes (abfd, info_ptr);
11935 info_ptr += 4;
11936 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11937 info_ptr += blk->size;
11938 DW_BLOCK (attr) = blk;
11939 break;
11940 case DW_FORM_data2:
11941 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
11942 info_ptr += 2;
11943 break;
11944 case DW_FORM_data4:
11945 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
11946 info_ptr += 4;
11947 break;
11948 case DW_FORM_data8:
11949 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
11950 info_ptr += 8;
11951 break;
11952 case DW_FORM_sec_offset:
11953 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
11954 info_ptr += bytes_read;
11955 break;
11956 case DW_FORM_string:
11957 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
11958 DW_STRING_IS_CANONICAL (attr) = 0;
11959 info_ptr += bytes_read;
11960 break;
11961 case DW_FORM_strp:
11962 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
11963 &bytes_read);
11964 DW_STRING_IS_CANONICAL (attr) = 0;
11965 info_ptr += bytes_read;
11966 break;
11967 case DW_FORM_exprloc:
11968 case DW_FORM_block:
11969 blk = dwarf_alloc_block (cu);
11970 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11971 info_ptr += bytes_read;
11972 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11973 info_ptr += blk->size;
11974 DW_BLOCK (attr) = blk;
11975 break;
11976 case DW_FORM_block1:
11977 blk = dwarf_alloc_block (cu);
11978 blk->size = read_1_byte (abfd, info_ptr);
11979 info_ptr += 1;
11980 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11981 info_ptr += blk->size;
11982 DW_BLOCK (attr) = blk;
11983 break;
11984 case DW_FORM_data1:
11985 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
11986 info_ptr += 1;
11987 break;
11988 case DW_FORM_flag:
11989 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
11990 info_ptr += 1;
11991 break;
11992 case DW_FORM_flag_present:
11993 DW_UNSND (attr) = 1;
11994 break;
11995 case DW_FORM_sdata:
11996 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
11997 info_ptr += bytes_read;
11998 break;
11999 case DW_FORM_udata:
12000 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
12001 info_ptr += bytes_read;
12002 break;
12003 case DW_FORM_ref1:
12004 DW_UNSND (attr) = (cu->header.offset.sect_off
12005 + read_1_byte (abfd, info_ptr));
12006 info_ptr += 1;
12007 break;
12008 case DW_FORM_ref2:
12009 DW_UNSND (attr) = (cu->header.offset.sect_off
12010 + read_2_bytes (abfd, info_ptr));
12011 info_ptr += 2;
12012 break;
12013 case DW_FORM_ref4:
12014 DW_UNSND (attr) = (cu->header.offset.sect_off
12015 + read_4_bytes (abfd, info_ptr));
12016 info_ptr += 4;
12017 break;
12018 case DW_FORM_ref8:
12019 DW_UNSND (attr) = (cu->header.offset.sect_off
12020 + read_8_bytes (abfd, info_ptr));
12021 info_ptr += 8;
12022 break;
12023 case DW_FORM_ref_sig8:
12024 /* Convert the signature to something we can record in DW_UNSND
12025 for later lookup.
12026 NOTE: This is NULL if the type wasn't found. */
12027 DW_SIGNATURED_TYPE (attr) =
12028 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
12029 info_ptr += 8;
12030 break;
12031 case DW_FORM_ref_udata:
12032 DW_UNSND (attr) = (cu->header.offset.sect_off
12033 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
12034 info_ptr += bytes_read;
12035 break;
12036 case DW_FORM_indirect:
12037 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
12038 info_ptr += bytes_read;
12039 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
12040 break;
12041 case DW_FORM_GNU_addr_index:
12042 if (reader->dwo_file == NULL)
12043 {
12044 /* For now flag a hard error.
12045 Later we can turn this into a complaint. */
12046 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
12047 dwarf_form_name (form),
12048 bfd_get_filename (abfd));
12049 }
12050 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
12051 info_ptr += bytes_read;
12052 break;
12053 case DW_FORM_GNU_str_index:
12054 if (reader->dwo_file == NULL)
12055 {
12056 /* For now flag a hard error.
12057 Later we can turn this into a complaint if warranted. */
12058 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
12059 dwarf_form_name (form),
12060 bfd_get_filename (abfd));
12061 }
12062 {
12063 ULONGEST str_index =
12064 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
12065
12066 DW_STRING (attr) = read_str_index (reader, cu, str_index);
12067 DW_STRING_IS_CANONICAL (attr) = 0;
12068 info_ptr += bytes_read;
12069 }
12070 break;
12071 default:
12072 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
12073 dwarf_form_name (form),
12074 bfd_get_filename (abfd));
12075 }
12076
12077 /* We have seen instances where the compiler tried to emit a byte
12078 size attribute of -1 which ended up being encoded as an unsigned
12079 0xffffffff. Although 0xffffffff is technically a valid size value,
12080 an object of this size seems pretty unlikely so we can relatively
12081 safely treat these cases as if the size attribute was invalid and
12082 treat them as zero by default. */
12083 if (attr->name == DW_AT_byte_size
12084 && form == DW_FORM_data4
12085 && DW_UNSND (attr) >= 0xffffffff)
12086 {
12087 complaint
12088 (&symfile_complaints,
12089 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
12090 hex_string (DW_UNSND (attr)));
12091 DW_UNSND (attr) = 0;
12092 }
12093
12094 return info_ptr;
12095 }
12096
12097 /* Read an attribute described by an abbreviated attribute. */
12098
12099 static gdb_byte *
12100 read_attribute (const struct die_reader_specs *reader,
12101 struct attribute *attr, struct attr_abbrev *abbrev,
12102 gdb_byte *info_ptr)
12103 {
12104 attr->name = abbrev->name;
12105 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
12106 }
12107
12108 /* Read dwarf information from a buffer. */
12109
12110 static unsigned int
12111 read_1_byte (bfd *abfd, gdb_byte *buf)
12112 {
12113 return bfd_get_8 (abfd, buf);
12114 }
12115
12116 static int
12117 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
12118 {
12119 return bfd_get_signed_8 (abfd, buf);
12120 }
12121
12122 static unsigned int
12123 read_2_bytes (bfd *abfd, gdb_byte *buf)
12124 {
12125 return bfd_get_16 (abfd, buf);
12126 }
12127
12128 static int
12129 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
12130 {
12131 return bfd_get_signed_16 (abfd, buf);
12132 }
12133
12134 static unsigned int
12135 read_4_bytes (bfd *abfd, gdb_byte *buf)
12136 {
12137 return bfd_get_32 (abfd, buf);
12138 }
12139
12140 static int
12141 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
12142 {
12143 return bfd_get_signed_32 (abfd, buf);
12144 }
12145
12146 static ULONGEST
12147 read_8_bytes (bfd *abfd, gdb_byte *buf)
12148 {
12149 return bfd_get_64 (abfd, buf);
12150 }
12151
12152 static CORE_ADDR
12153 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
12154 unsigned int *bytes_read)
12155 {
12156 struct comp_unit_head *cu_header = &cu->header;
12157 CORE_ADDR retval = 0;
12158
12159 if (cu_header->signed_addr_p)
12160 {
12161 switch (cu_header->addr_size)
12162 {
12163 case 2:
12164 retval = bfd_get_signed_16 (abfd, buf);
12165 break;
12166 case 4:
12167 retval = bfd_get_signed_32 (abfd, buf);
12168 break;
12169 case 8:
12170 retval = bfd_get_signed_64 (abfd, buf);
12171 break;
12172 default:
12173 internal_error (__FILE__, __LINE__,
12174 _("read_address: bad switch, signed [in module %s]"),
12175 bfd_get_filename (abfd));
12176 }
12177 }
12178 else
12179 {
12180 switch (cu_header->addr_size)
12181 {
12182 case 2:
12183 retval = bfd_get_16 (abfd, buf);
12184 break;
12185 case 4:
12186 retval = bfd_get_32 (abfd, buf);
12187 break;
12188 case 8:
12189 retval = bfd_get_64 (abfd, buf);
12190 break;
12191 default:
12192 internal_error (__FILE__, __LINE__,
12193 _("read_address: bad switch, "
12194 "unsigned [in module %s]"),
12195 bfd_get_filename (abfd));
12196 }
12197 }
12198
12199 *bytes_read = cu_header->addr_size;
12200 return retval;
12201 }
12202
12203 /* Read the initial length from a section. The (draft) DWARF 3
12204 specification allows the initial length to take up either 4 bytes
12205 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
12206 bytes describe the length and all offsets will be 8 bytes in length
12207 instead of 4.
12208
12209 An older, non-standard 64-bit format is also handled by this
12210 function. The older format in question stores the initial length
12211 as an 8-byte quantity without an escape value. Lengths greater
12212 than 2^32 aren't very common which means that the initial 4 bytes
12213 is almost always zero. Since a length value of zero doesn't make
12214 sense for the 32-bit format, this initial zero can be considered to
12215 be an escape value which indicates the presence of the older 64-bit
12216 format. As written, the code can't detect (old format) lengths
12217 greater than 4GB. If it becomes necessary to handle lengths
12218 somewhat larger than 4GB, we could allow other small values (such
12219 as the non-sensical values of 1, 2, and 3) to also be used as
12220 escape values indicating the presence of the old format.
12221
12222 The value returned via bytes_read should be used to increment the
12223 relevant pointer after calling read_initial_length().
12224
12225 [ Note: read_initial_length() and read_offset() are based on the
12226 document entitled "DWARF Debugging Information Format", revision
12227 3, draft 8, dated November 19, 2001. This document was obtained
12228 from:
12229
12230 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
12231
12232 This document is only a draft and is subject to change. (So beware.)
12233
12234 Details regarding the older, non-standard 64-bit format were
12235 determined empirically by examining 64-bit ELF files produced by
12236 the SGI toolchain on an IRIX 6.5 machine.
12237
12238 - Kevin, July 16, 2002
12239 ] */
12240
12241 static LONGEST
12242 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
12243 {
12244 LONGEST length = bfd_get_32 (abfd, buf);
12245
12246 if (length == 0xffffffff)
12247 {
12248 length = bfd_get_64 (abfd, buf + 4);
12249 *bytes_read = 12;
12250 }
12251 else if (length == 0)
12252 {
12253 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
12254 length = bfd_get_64 (abfd, buf);
12255 *bytes_read = 8;
12256 }
12257 else
12258 {
12259 *bytes_read = 4;
12260 }
12261
12262 return length;
12263 }
12264
12265 /* Cover function for read_initial_length.
12266 Returns the length of the object at BUF, and stores the size of the
12267 initial length in *BYTES_READ and stores the size that offsets will be in
12268 *OFFSET_SIZE.
12269 If the initial length size is not equivalent to that specified in
12270 CU_HEADER then issue a complaint.
12271 This is useful when reading non-comp-unit headers. */
12272
12273 static LONGEST
12274 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
12275 const struct comp_unit_head *cu_header,
12276 unsigned int *bytes_read,
12277 unsigned int *offset_size)
12278 {
12279 LONGEST length = read_initial_length (abfd, buf, bytes_read);
12280
12281 gdb_assert (cu_header->initial_length_size == 4
12282 || cu_header->initial_length_size == 8
12283 || cu_header->initial_length_size == 12);
12284
12285 if (cu_header->initial_length_size != *bytes_read)
12286 complaint (&symfile_complaints,
12287 _("intermixed 32-bit and 64-bit DWARF sections"));
12288
12289 *offset_size = (*bytes_read == 4) ? 4 : 8;
12290 return length;
12291 }
12292
12293 /* Read an offset from the data stream. The size of the offset is
12294 given by cu_header->offset_size. */
12295
12296 static LONGEST
12297 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
12298 unsigned int *bytes_read)
12299 {
12300 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
12301
12302 *bytes_read = cu_header->offset_size;
12303 return offset;
12304 }
12305
12306 /* Read an offset from the data stream. */
12307
12308 static LONGEST
12309 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
12310 {
12311 LONGEST retval = 0;
12312
12313 switch (offset_size)
12314 {
12315 case 4:
12316 retval = bfd_get_32 (abfd, buf);
12317 break;
12318 case 8:
12319 retval = bfd_get_64 (abfd, buf);
12320 break;
12321 default:
12322 internal_error (__FILE__, __LINE__,
12323 _("read_offset_1: bad switch [in module %s]"),
12324 bfd_get_filename (abfd));
12325 }
12326
12327 return retval;
12328 }
12329
12330 static gdb_byte *
12331 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
12332 {
12333 /* If the size of a host char is 8 bits, we can return a pointer
12334 to the buffer, otherwise we have to copy the data to a buffer
12335 allocated on the temporary obstack. */
12336 gdb_assert (HOST_CHAR_BIT == 8);
12337 return buf;
12338 }
12339
12340 static char *
12341 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
12342 {
12343 /* If the size of a host char is 8 bits, we can return a pointer
12344 to the string, otherwise we have to copy the string to a buffer
12345 allocated on the temporary obstack. */
12346 gdb_assert (HOST_CHAR_BIT == 8);
12347 if (*buf == '\0')
12348 {
12349 *bytes_read_ptr = 1;
12350 return NULL;
12351 }
12352 *bytes_read_ptr = strlen ((char *) buf) + 1;
12353 return (char *) buf;
12354 }
12355
12356 static char *
12357 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
12358 {
12359 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
12360 if (dwarf2_per_objfile->str.buffer == NULL)
12361 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
12362 bfd_get_filename (abfd));
12363 if (str_offset >= dwarf2_per_objfile->str.size)
12364 error (_("DW_FORM_strp pointing outside of "
12365 ".debug_str section [in module %s]"),
12366 bfd_get_filename (abfd));
12367 gdb_assert (HOST_CHAR_BIT == 8);
12368 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
12369 return NULL;
12370 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
12371 }
12372
12373 static char *
12374 read_indirect_string (bfd *abfd, gdb_byte *buf,
12375 const struct comp_unit_head *cu_header,
12376 unsigned int *bytes_read_ptr)
12377 {
12378 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
12379
12380 return read_indirect_string_at_offset (abfd, str_offset);
12381 }
12382
12383 static ULONGEST
12384 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
12385 {
12386 ULONGEST result;
12387 unsigned int num_read;
12388 int i, shift;
12389 unsigned char byte;
12390
12391 result = 0;
12392 shift = 0;
12393 num_read = 0;
12394 i = 0;
12395 while (1)
12396 {
12397 byte = bfd_get_8 (abfd, buf);
12398 buf++;
12399 num_read++;
12400 result |= ((ULONGEST) (byte & 127) << shift);
12401 if ((byte & 128) == 0)
12402 {
12403 break;
12404 }
12405 shift += 7;
12406 }
12407 *bytes_read_ptr = num_read;
12408 return result;
12409 }
12410
12411 static LONGEST
12412 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
12413 {
12414 LONGEST result;
12415 int i, shift, num_read;
12416 unsigned char byte;
12417
12418 result = 0;
12419 shift = 0;
12420 num_read = 0;
12421 i = 0;
12422 while (1)
12423 {
12424 byte = bfd_get_8 (abfd, buf);
12425 buf++;
12426 num_read++;
12427 result |= ((LONGEST) (byte & 127) << shift);
12428 shift += 7;
12429 if ((byte & 128) == 0)
12430 {
12431 break;
12432 }
12433 }
12434 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12435 result |= -(((LONGEST) 1) << shift);
12436 *bytes_read_ptr = num_read;
12437 return result;
12438 }
12439
12440 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
12441 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
12442 ADDR_SIZE is the size of addresses from the CU header. */
12443
12444 static CORE_ADDR
12445 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
12446 {
12447 struct objfile *objfile = dwarf2_per_objfile->objfile;
12448 bfd *abfd = objfile->obfd;
12449 const gdb_byte *info_ptr;
12450
12451 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
12452 if (dwarf2_per_objfile->addr.buffer == NULL)
12453 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
12454 objfile->name);
12455 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
12456 error (_("DW_FORM_addr_index pointing outside of "
12457 ".debug_addr section [in module %s]"),
12458 objfile->name);
12459 info_ptr = (dwarf2_per_objfile->addr.buffer
12460 + addr_base + addr_index * addr_size);
12461 if (addr_size == 4)
12462 return bfd_get_32 (abfd, info_ptr);
12463 else
12464 return bfd_get_64 (abfd, info_ptr);
12465 }
12466
12467 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
12468
12469 static CORE_ADDR
12470 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
12471 {
12472 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
12473 }
12474
12475 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
12476
12477 static CORE_ADDR
12478 read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
12479 unsigned int *bytes_read)
12480 {
12481 bfd *abfd = cu->objfile->obfd;
12482 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
12483
12484 return read_addr_index (cu, addr_index);
12485 }
12486
12487 /* Data structure to pass results from dwarf2_read_addr_index_reader
12488 back to dwarf2_read_addr_index. */
12489
12490 struct dwarf2_read_addr_index_data
12491 {
12492 ULONGEST addr_base;
12493 int addr_size;
12494 };
12495
12496 /* die_reader_func for dwarf2_read_addr_index. */
12497
12498 static void
12499 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
12500 gdb_byte *info_ptr,
12501 struct die_info *comp_unit_die,
12502 int has_children,
12503 void *data)
12504 {
12505 struct dwarf2_cu *cu = reader->cu;
12506 struct dwarf2_read_addr_index_data *aidata =
12507 (struct dwarf2_read_addr_index_data *) data;
12508
12509 aidata->addr_base = cu->addr_base;
12510 aidata->addr_size = cu->header.addr_size;
12511 }
12512
12513 /* Given an index in .debug_addr, fetch the value.
12514 NOTE: This can be called during dwarf expression evaluation,
12515 long after the debug information has been read, and thus per_cu->cu
12516 may no longer exist. */
12517
12518 CORE_ADDR
12519 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
12520 unsigned int addr_index)
12521 {
12522 struct objfile *objfile = per_cu->objfile;
12523 struct dwarf2_cu *cu = per_cu->cu;
12524 ULONGEST addr_base;
12525 int addr_size;
12526
12527 /* This is intended to be called from outside this file. */
12528 dw2_setup (objfile);
12529
12530 /* We need addr_base and addr_size.
12531 If we don't have PER_CU->cu, we have to get it.
12532 Nasty, but the alternative is storing the needed info in PER_CU,
12533 which at this point doesn't seem justified: it's not clear how frequently
12534 it would get used and it would increase the size of every PER_CU.
12535 Entry points like dwarf2_per_cu_addr_size do a similar thing
12536 so we're not in uncharted territory here.
12537 Alas we need to be a bit more complicated as addr_base is contained
12538 in the DIE.
12539
12540 We don't need to read the entire CU(/TU).
12541 We just need the header and top level die.
12542 IWBN to use the aging mechanism to let us lazily later discard the CU.
12543 See however init_cutu_and_read_dies_simple. */
12544
12545 if (cu != NULL)
12546 {
12547 addr_base = cu->addr_base;
12548 addr_size = cu->header.addr_size;
12549 }
12550 else
12551 {
12552 struct dwarf2_read_addr_index_data aidata;
12553
12554 init_cutu_and_read_dies_simple (per_cu, dwarf2_read_addr_index_reader,
12555 &aidata);
12556 addr_base = aidata.addr_base;
12557 addr_size = aidata.addr_size;
12558 }
12559
12560 return read_addr_index_1 (addr_index, addr_base, addr_size);
12561 }
12562
12563 /* Given a DW_AT_str_index, fetch the string. */
12564
12565 static char *
12566 read_str_index (const struct die_reader_specs *reader,
12567 struct dwarf2_cu *cu, ULONGEST str_index)
12568 {
12569 struct objfile *objfile = dwarf2_per_objfile->objfile;
12570 const char *dwo_name = objfile->name;
12571 bfd *abfd = objfile->obfd;
12572 struct dwo_sections *sections = &reader->dwo_file->sections;
12573 gdb_byte *info_ptr;
12574 ULONGEST str_offset;
12575
12576 dwarf2_read_section (objfile, &sections->str);
12577 dwarf2_read_section (objfile, &sections->str_offsets);
12578 if (sections->str.buffer == NULL)
12579 error (_("DW_FORM_str_index used without .debug_str.dwo section"
12580 " in CU at offset 0x%lx [in module %s]"),
12581 (long) cu->header.offset.sect_off, dwo_name);
12582 if (sections->str_offsets.buffer == NULL)
12583 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
12584 " in CU at offset 0x%lx [in module %s]"),
12585 (long) cu->header.offset.sect_off, dwo_name);
12586 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
12587 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
12588 " section in CU at offset 0x%lx [in module %s]"),
12589 (long) cu->header.offset.sect_off, dwo_name);
12590 info_ptr = (sections->str_offsets.buffer
12591 + str_index * cu->header.offset_size);
12592 if (cu->header.offset_size == 4)
12593 str_offset = bfd_get_32 (abfd, info_ptr);
12594 else
12595 str_offset = bfd_get_64 (abfd, info_ptr);
12596 if (str_offset >= sections->str.size)
12597 error (_("Offset from DW_FORM_str_index pointing outside of"
12598 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
12599 (long) cu->header.offset.sect_off, dwo_name);
12600 return (char *) (sections->str.buffer + str_offset);
12601 }
12602
12603 /* Return the length of an LEB128 number in BUF. */
12604
12605 static int
12606 leb128_size (const gdb_byte *buf)
12607 {
12608 const gdb_byte *begin = buf;
12609 gdb_byte byte;
12610
12611 while (1)
12612 {
12613 byte = *buf++;
12614 if ((byte & 128) == 0)
12615 return buf - begin;
12616 }
12617 }
12618
12619 static void
12620 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
12621 {
12622 switch (lang)
12623 {
12624 case DW_LANG_C89:
12625 case DW_LANG_C99:
12626 case DW_LANG_C:
12627 cu->language = language_c;
12628 break;
12629 case DW_LANG_C_plus_plus:
12630 cu->language = language_cplus;
12631 break;
12632 case DW_LANG_D:
12633 cu->language = language_d;
12634 break;
12635 case DW_LANG_Fortran77:
12636 case DW_LANG_Fortran90:
12637 case DW_LANG_Fortran95:
12638 cu->language = language_fortran;
12639 break;
12640 case DW_LANG_Go:
12641 cu->language = language_go;
12642 break;
12643 case DW_LANG_Mips_Assembler:
12644 cu->language = language_asm;
12645 break;
12646 case DW_LANG_Java:
12647 cu->language = language_java;
12648 break;
12649 case DW_LANG_Ada83:
12650 case DW_LANG_Ada95:
12651 cu->language = language_ada;
12652 break;
12653 case DW_LANG_Modula2:
12654 cu->language = language_m2;
12655 break;
12656 case DW_LANG_Pascal83:
12657 cu->language = language_pascal;
12658 break;
12659 case DW_LANG_ObjC:
12660 cu->language = language_objc;
12661 break;
12662 case DW_LANG_Cobol74:
12663 case DW_LANG_Cobol85:
12664 default:
12665 cu->language = language_minimal;
12666 break;
12667 }
12668 cu->language_defn = language_def (cu->language);
12669 }
12670
12671 /* Return the named attribute or NULL if not there. */
12672
12673 static struct attribute *
12674 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
12675 {
12676 for (;;)
12677 {
12678 unsigned int i;
12679 struct attribute *spec = NULL;
12680
12681 for (i = 0; i < die->num_attrs; ++i)
12682 {
12683 if (die->attrs[i].name == name)
12684 return &die->attrs[i];
12685 if (die->attrs[i].name == DW_AT_specification
12686 || die->attrs[i].name == DW_AT_abstract_origin)
12687 spec = &die->attrs[i];
12688 }
12689
12690 if (!spec)
12691 break;
12692
12693 die = follow_die_ref (die, spec, &cu);
12694 }
12695
12696 return NULL;
12697 }
12698
12699 /* Return the named attribute or NULL if not there,
12700 but do not follow DW_AT_specification, etc.
12701 This is for use in contexts where we're reading .debug_types dies.
12702 Following DW_AT_specification, DW_AT_abstract_origin will take us
12703 back up the chain, and we want to go down. */
12704
12705 static struct attribute *
12706 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
12707 struct dwarf2_cu *cu)
12708 {
12709 unsigned int i;
12710
12711 for (i = 0; i < die->num_attrs; ++i)
12712 if (die->attrs[i].name == name)
12713 return &die->attrs[i];
12714
12715 return NULL;
12716 }
12717
12718 /* Return non-zero iff the attribute NAME is defined for the given DIE,
12719 and holds a non-zero value. This function should only be used for
12720 DW_FORM_flag or DW_FORM_flag_present attributes. */
12721
12722 static int
12723 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
12724 {
12725 struct attribute *attr = dwarf2_attr (die, name, cu);
12726
12727 return (attr && DW_UNSND (attr));
12728 }
12729
12730 static int
12731 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
12732 {
12733 /* A DIE is a declaration if it has a DW_AT_declaration attribute
12734 which value is non-zero. However, we have to be careful with
12735 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
12736 (via dwarf2_flag_true_p) follows this attribute. So we may
12737 end up accidently finding a declaration attribute that belongs
12738 to a different DIE referenced by the specification attribute,
12739 even though the given DIE does not have a declaration attribute. */
12740 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
12741 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
12742 }
12743
12744 /* Return the die giving the specification for DIE, if there is
12745 one. *SPEC_CU is the CU containing DIE on input, and the CU
12746 containing the return value on output. If there is no
12747 specification, but there is an abstract origin, that is
12748 returned. */
12749
12750 static struct die_info *
12751 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
12752 {
12753 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
12754 *spec_cu);
12755
12756 if (spec_attr == NULL)
12757 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
12758
12759 if (spec_attr == NULL)
12760 return NULL;
12761 else
12762 return follow_die_ref (die, spec_attr, spec_cu);
12763 }
12764
12765 /* Free the line_header structure *LH, and any arrays and strings it
12766 refers to.
12767 NOTE: This is also used as a "cleanup" function. */
12768
12769 static void
12770 free_line_header (struct line_header *lh)
12771 {
12772 if (lh->standard_opcode_lengths)
12773 xfree (lh->standard_opcode_lengths);
12774
12775 /* Remember that all the lh->file_names[i].name pointers are
12776 pointers into debug_line_buffer, and don't need to be freed. */
12777 if (lh->file_names)
12778 xfree (lh->file_names);
12779
12780 /* Similarly for the include directory names. */
12781 if (lh->include_dirs)
12782 xfree (lh->include_dirs);
12783
12784 xfree (lh);
12785 }
12786
12787 /* Add an entry to LH's include directory table. */
12788
12789 static void
12790 add_include_dir (struct line_header *lh, char *include_dir)
12791 {
12792 /* Grow the array if necessary. */
12793 if (lh->include_dirs_size == 0)
12794 {
12795 lh->include_dirs_size = 1; /* for testing */
12796 lh->include_dirs = xmalloc (lh->include_dirs_size
12797 * sizeof (*lh->include_dirs));
12798 }
12799 else if (lh->num_include_dirs >= lh->include_dirs_size)
12800 {
12801 lh->include_dirs_size *= 2;
12802 lh->include_dirs = xrealloc (lh->include_dirs,
12803 (lh->include_dirs_size
12804 * sizeof (*lh->include_dirs)));
12805 }
12806
12807 lh->include_dirs[lh->num_include_dirs++] = include_dir;
12808 }
12809
12810 /* Add an entry to LH's file name table. */
12811
12812 static void
12813 add_file_name (struct line_header *lh,
12814 char *name,
12815 unsigned int dir_index,
12816 unsigned int mod_time,
12817 unsigned int length)
12818 {
12819 struct file_entry *fe;
12820
12821 /* Grow the array if necessary. */
12822 if (lh->file_names_size == 0)
12823 {
12824 lh->file_names_size = 1; /* for testing */
12825 lh->file_names = xmalloc (lh->file_names_size
12826 * sizeof (*lh->file_names));
12827 }
12828 else if (lh->num_file_names >= lh->file_names_size)
12829 {
12830 lh->file_names_size *= 2;
12831 lh->file_names = xrealloc (lh->file_names,
12832 (lh->file_names_size
12833 * sizeof (*lh->file_names)));
12834 }
12835
12836 fe = &lh->file_names[lh->num_file_names++];
12837 fe->name = name;
12838 fe->dir_index = dir_index;
12839 fe->mod_time = mod_time;
12840 fe->length = length;
12841 fe->included_p = 0;
12842 fe->symtab = NULL;
12843 }
12844
12845 /* Read the statement program header starting at OFFSET in
12846 .debug_line, or .debug_line.dwo. Return a pointer
12847 to a struct line_header, allocated using xmalloc.
12848
12849 NOTE: the strings in the include directory and file name tables of
12850 the returned object point into the dwarf line section buffer,
12851 and must not be freed. */
12852
12853 static struct line_header *
12854 dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
12855 {
12856 struct cleanup *back_to;
12857 struct line_header *lh;
12858 gdb_byte *line_ptr;
12859 unsigned int bytes_read, offset_size;
12860 int i;
12861 char *cur_dir, *cur_file;
12862 struct dwarf2_section_info *section;
12863 bfd *abfd;
12864
12865 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
12866 DWO file. */
12867 if (cu->dwo_unit && cu->per_cu->is_debug_types)
12868 section = &cu->dwo_unit->dwo_file->sections.line;
12869 else
12870 section = &dwarf2_per_objfile->line;
12871
12872 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
12873 if (section->buffer == NULL)
12874 {
12875 if (cu->dwo_unit && cu->per_cu->is_debug_types)
12876 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
12877 else
12878 complaint (&symfile_complaints, _("missing .debug_line section"));
12879 return 0;
12880 }
12881
12882 /* We can't do this until we know the section is non-empty.
12883 Only then do we know we have such a section. */
12884 abfd = section->asection->owner;
12885
12886 /* Make sure that at least there's room for the total_length field.
12887 That could be 12 bytes long, but we're just going to fudge that. */
12888 if (offset + 4 >= section->size)
12889 {
12890 dwarf2_statement_list_fits_in_line_number_section_complaint ();
12891 return 0;
12892 }
12893
12894 lh = xmalloc (sizeof (*lh));
12895 memset (lh, 0, sizeof (*lh));
12896 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
12897 (void *) lh);
12898
12899 line_ptr = section->buffer + offset;
12900
12901 /* Read in the header. */
12902 lh->total_length =
12903 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
12904 &bytes_read, &offset_size);
12905 line_ptr += bytes_read;
12906 if (line_ptr + lh->total_length > (section->buffer + section->size))
12907 {
12908 dwarf2_statement_list_fits_in_line_number_section_complaint ();
12909 return 0;
12910 }
12911 lh->statement_program_end = line_ptr + lh->total_length;
12912 lh->version = read_2_bytes (abfd, line_ptr);
12913 line_ptr += 2;
12914 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
12915 line_ptr += offset_size;
12916 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
12917 line_ptr += 1;
12918 if (lh->version >= 4)
12919 {
12920 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
12921 line_ptr += 1;
12922 }
12923 else
12924 lh->maximum_ops_per_instruction = 1;
12925
12926 if (lh->maximum_ops_per_instruction == 0)
12927 {
12928 lh->maximum_ops_per_instruction = 1;
12929 complaint (&symfile_complaints,
12930 _("invalid maximum_ops_per_instruction "
12931 "in `.debug_line' section"));
12932 }
12933
12934 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
12935 line_ptr += 1;
12936 lh->line_base = read_1_signed_byte (abfd, line_ptr);
12937 line_ptr += 1;
12938 lh->line_range = read_1_byte (abfd, line_ptr);
12939 line_ptr += 1;
12940 lh->opcode_base = read_1_byte (abfd, line_ptr);
12941 line_ptr += 1;
12942 lh->standard_opcode_lengths
12943 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
12944
12945 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
12946 for (i = 1; i < lh->opcode_base; ++i)
12947 {
12948 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
12949 line_ptr += 1;
12950 }
12951
12952 /* Read directory table. */
12953 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
12954 {
12955 line_ptr += bytes_read;
12956 add_include_dir (lh, cur_dir);
12957 }
12958 line_ptr += bytes_read;
12959
12960 /* Read file name table. */
12961 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
12962 {
12963 unsigned int dir_index, mod_time, length;
12964
12965 line_ptr += bytes_read;
12966 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12967 line_ptr += bytes_read;
12968 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12969 line_ptr += bytes_read;
12970 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12971 line_ptr += bytes_read;
12972
12973 add_file_name (lh, cur_file, dir_index, mod_time, length);
12974 }
12975 line_ptr += bytes_read;
12976 lh->statement_program_start = line_ptr;
12977
12978 if (line_ptr > (section->buffer + section->size))
12979 complaint (&symfile_complaints,
12980 _("line number info header doesn't "
12981 "fit in `.debug_line' section"));
12982
12983 discard_cleanups (back_to);
12984 return lh;
12985 }
12986
12987 /* Subroutine of dwarf_decode_lines to simplify it.
12988 Return the file name of the psymtab for included file FILE_INDEX
12989 in line header LH of PST.
12990 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
12991 If space for the result is malloc'd, it will be freed by a cleanup.
12992 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
12993
12994 static char *
12995 psymtab_include_file_name (const struct line_header *lh, int file_index,
12996 const struct partial_symtab *pst,
12997 const char *comp_dir)
12998 {
12999 const struct file_entry fe = lh->file_names [file_index];
13000 char *include_name = fe.name;
13001 char *include_name_to_compare = include_name;
13002 char *dir_name = NULL;
13003 const char *pst_filename;
13004 char *copied_name = NULL;
13005 int file_is_pst;
13006
13007 if (fe.dir_index)
13008 dir_name = lh->include_dirs[fe.dir_index - 1];
13009
13010 if (!IS_ABSOLUTE_PATH (include_name)
13011 && (dir_name != NULL || comp_dir != NULL))
13012 {
13013 /* Avoid creating a duplicate psymtab for PST.
13014 We do this by comparing INCLUDE_NAME and PST_FILENAME.
13015 Before we do the comparison, however, we need to account
13016 for DIR_NAME and COMP_DIR.
13017 First prepend dir_name (if non-NULL). If we still don't
13018 have an absolute path prepend comp_dir (if non-NULL).
13019 However, the directory we record in the include-file's
13020 psymtab does not contain COMP_DIR (to match the
13021 corresponding symtab(s)).
13022
13023 Example:
13024
13025 bash$ cd /tmp
13026 bash$ gcc -g ./hello.c
13027 include_name = "hello.c"
13028 dir_name = "."
13029 DW_AT_comp_dir = comp_dir = "/tmp"
13030 DW_AT_name = "./hello.c" */
13031
13032 if (dir_name != NULL)
13033 {
13034 include_name = concat (dir_name, SLASH_STRING,
13035 include_name, (char *)NULL);
13036 include_name_to_compare = include_name;
13037 make_cleanup (xfree, include_name);
13038 }
13039 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
13040 {
13041 include_name_to_compare = concat (comp_dir, SLASH_STRING,
13042 include_name, (char *)NULL);
13043 }
13044 }
13045
13046 pst_filename = pst->filename;
13047 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
13048 {
13049 copied_name = concat (pst->dirname, SLASH_STRING,
13050 pst_filename, (char *)NULL);
13051 pst_filename = copied_name;
13052 }
13053
13054 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
13055
13056 if (include_name_to_compare != include_name)
13057 xfree (include_name_to_compare);
13058 if (copied_name != NULL)
13059 xfree (copied_name);
13060
13061 if (file_is_pst)
13062 return NULL;
13063 return include_name;
13064 }
13065
13066 /* Ignore this record_line request. */
13067
13068 static void
13069 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
13070 {
13071 return;
13072 }
13073
13074 /* Subroutine of dwarf_decode_lines to simplify it.
13075 Process the line number information in LH. */
13076
13077 static void
13078 dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
13079 struct dwarf2_cu *cu, struct partial_symtab *pst)
13080 {
13081 gdb_byte *line_ptr, *extended_end;
13082 gdb_byte *line_end;
13083 unsigned int bytes_read, extended_len;
13084 unsigned char op_code, extended_op, adj_opcode;
13085 CORE_ADDR baseaddr;
13086 struct objfile *objfile = cu->objfile;
13087 bfd *abfd = objfile->obfd;
13088 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13089 const int decode_for_pst_p = (pst != NULL);
13090 struct subfile *last_subfile = NULL;
13091 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
13092 = record_line;
13093
13094 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13095
13096 line_ptr = lh->statement_program_start;
13097 line_end = lh->statement_program_end;
13098
13099 /* Read the statement sequences until there's nothing left. */
13100 while (line_ptr < line_end)
13101 {
13102 /* state machine registers */
13103 CORE_ADDR address = 0;
13104 unsigned int file = 1;
13105 unsigned int line = 1;
13106 unsigned int column = 0;
13107 int is_stmt = lh->default_is_stmt;
13108 int basic_block = 0;
13109 int end_sequence = 0;
13110 CORE_ADDR addr;
13111 unsigned char op_index = 0;
13112
13113 if (!decode_for_pst_p && lh->num_file_names >= file)
13114 {
13115 /* Start a subfile for the current file of the state machine. */
13116 /* lh->include_dirs and lh->file_names are 0-based, but the
13117 directory and file name numbers in the statement program
13118 are 1-based. */
13119 struct file_entry *fe = &lh->file_names[file - 1];
13120 char *dir = NULL;
13121
13122 if (fe->dir_index)
13123 dir = lh->include_dirs[fe->dir_index - 1];
13124
13125 dwarf2_start_subfile (fe->name, dir, comp_dir);
13126 }
13127
13128 /* Decode the table. */
13129 while (!end_sequence)
13130 {
13131 op_code = read_1_byte (abfd, line_ptr);
13132 line_ptr += 1;
13133 if (line_ptr > line_end)
13134 {
13135 dwarf2_debug_line_missing_end_sequence_complaint ();
13136 break;
13137 }
13138
13139 if (op_code >= lh->opcode_base)
13140 {
13141 /* Special operand. */
13142 adj_opcode = op_code - lh->opcode_base;
13143 address += (((op_index + (adj_opcode / lh->line_range))
13144 / lh->maximum_ops_per_instruction)
13145 * lh->minimum_instruction_length);
13146 op_index = ((op_index + (adj_opcode / lh->line_range))
13147 % lh->maximum_ops_per_instruction);
13148 line += lh->line_base + (adj_opcode % lh->line_range);
13149 if (lh->num_file_names < file || file == 0)
13150 dwarf2_debug_line_missing_file_complaint ();
13151 /* For now we ignore lines not starting on an
13152 instruction boundary. */
13153 else if (op_index == 0)
13154 {
13155 lh->file_names[file - 1].included_p = 1;
13156 if (!decode_for_pst_p && is_stmt)
13157 {
13158 if (last_subfile != current_subfile)
13159 {
13160 addr = gdbarch_addr_bits_remove (gdbarch, address);
13161 if (last_subfile)
13162 (*p_record_line) (last_subfile, 0, addr);
13163 last_subfile = current_subfile;
13164 }
13165 /* Append row to matrix using current values. */
13166 addr = gdbarch_addr_bits_remove (gdbarch, address);
13167 (*p_record_line) (current_subfile, line, addr);
13168 }
13169 }
13170 basic_block = 0;
13171 }
13172 else switch (op_code)
13173 {
13174 case DW_LNS_extended_op:
13175 extended_len = read_unsigned_leb128 (abfd, line_ptr,
13176 &bytes_read);
13177 line_ptr += bytes_read;
13178 extended_end = line_ptr + extended_len;
13179 extended_op = read_1_byte (abfd, line_ptr);
13180 line_ptr += 1;
13181 switch (extended_op)
13182 {
13183 case DW_LNE_end_sequence:
13184 p_record_line = record_line;
13185 end_sequence = 1;
13186 break;
13187 case DW_LNE_set_address:
13188 address = read_address (abfd, line_ptr, cu, &bytes_read);
13189
13190 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
13191 {
13192 /* This line table is for a function which has been
13193 GCd by the linker. Ignore it. PR gdb/12528 */
13194
13195 long line_offset
13196 = line_ptr - dwarf2_per_objfile->line.buffer;
13197
13198 complaint (&symfile_complaints,
13199 _(".debug_line address at offset 0x%lx is 0 "
13200 "[in module %s]"),
13201 line_offset, objfile->name);
13202 p_record_line = noop_record_line;
13203 }
13204
13205 op_index = 0;
13206 line_ptr += bytes_read;
13207 address += baseaddr;
13208 break;
13209 case DW_LNE_define_file:
13210 {
13211 char *cur_file;
13212 unsigned int dir_index, mod_time, length;
13213
13214 cur_file = read_direct_string (abfd, line_ptr,
13215 &bytes_read);
13216 line_ptr += bytes_read;
13217 dir_index =
13218 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13219 line_ptr += bytes_read;
13220 mod_time =
13221 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13222 line_ptr += bytes_read;
13223 length =
13224 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13225 line_ptr += bytes_read;
13226 add_file_name (lh, cur_file, dir_index, mod_time, length);
13227 }
13228 break;
13229 case DW_LNE_set_discriminator:
13230 /* The discriminator is not interesting to the debugger;
13231 just ignore it. */
13232 line_ptr = extended_end;
13233 break;
13234 default:
13235 complaint (&symfile_complaints,
13236 _("mangled .debug_line section"));
13237 return;
13238 }
13239 /* Make sure that we parsed the extended op correctly. If e.g.
13240 we expected a different address size than the producer used,
13241 we may have read the wrong number of bytes. */
13242 if (line_ptr != extended_end)
13243 {
13244 complaint (&symfile_complaints,
13245 _("mangled .debug_line section"));
13246 return;
13247 }
13248 break;
13249 case DW_LNS_copy:
13250 if (lh->num_file_names < file || file == 0)
13251 dwarf2_debug_line_missing_file_complaint ();
13252 else
13253 {
13254 lh->file_names[file - 1].included_p = 1;
13255 if (!decode_for_pst_p && is_stmt)
13256 {
13257 if (last_subfile != current_subfile)
13258 {
13259 addr = gdbarch_addr_bits_remove (gdbarch, address);
13260 if (last_subfile)
13261 (*p_record_line) (last_subfile, 0, addr);
13262 last_subfile = current_subfile;
13263 }
13264 addr = gdbarch_addr_bits_remove (gdbarch, address);
13265 (*p_record_line) (current_subfile, line, addr);
13266 }
13267 }
13268 basic_block = 0;
13269 break;
13270 case DW_LNS_advance_pc:
13271 {
13272 CORE_ADDR adjust
13273 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13274
13275 address += (((op_index + adjust)
13276 / lh->maximum_ops_per_instruction)
13277 * lh->minimum_instruction_length);
13278 op_index = ((op_index + adjust)
13279 % lh->maximum_ops_per_instruction);
13280 line_ptr += bytes_read;
13281 }
13282 break;
13283 case DW_LNS_advance_line:
13284 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
13285 line_ptr += bytes_read;
13286 break;
13287 case DW_LNS_set_file:
13288 {
13289 /* The arrays lh->include_dirs and lh->file_names are
13290 0-based, but the directory and file name numbers in
13291 the statement program are 1-based. */
13292 struct file_entry *fe;
13293 char *dir = NULL;
13294
13295 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13296 line_ptr += bytes_read;
13297 if (lh->num_file_names < file || file == 0)
13298 dwarf2_debug_line_missing_file_complaint ();
13299 else
13300 {
13301 fe = &lh->file_names[file - 1];
13302 if (fe->dir_index)
13303 dir = lh->include_dirs[fe->dir_index - 1];
13304 if (!decode_for_pst_p)
13305 {
13306 last_subfile = current_subfile;
13307 dwarf2_start_subfile (fe->name, dir, comp_dir);
13308 }
13309 }
13310 }
13311 break;
13312 case DW_LNS_set_column:
13313 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13314 line_ptr += bytes_read;
13315 break;
13316 case DW_LNS_negate_stmt:
13317 is_stmt = (!is_stmt);
13318 break;
13319 case DW_LNS_set_basic_block:
13320 basic_block = 1;
13321 break;
13322 /* Add to the address register of the state machine the
13323 address increment value corresponding to special opcode
13324 255. I.e., this value is scaled by the minimum
13325 instruction length since special opcode 255 would have
13326 scaled the increment. */
13327 case DW_LNS_const_add_pc:
13328 {
13329 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
13330
13331 address += (((op_index + adjust)
13332 / lh->maximum_ops_per_instruction)
13333 * lh->minimum_instruction_length);
13334 op_index = ((op_index + adjust)
13335 % lh->maximum_ops_per_instruction);
13336 }
13337 break;
13338 case DW_LNS_fixed_advance_pc:
13339 address += read_2_bytes (abfd, line_ptr);
13340 op_index = 0;
13341 line_ptr += 2;
13342 break;
13343 default:
13344 {
13345 /* Unknown standard opcode, ignore it. */
13346 int i;
13347
13348 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
13349 {
13350 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13351 line_ptr += bytes_read;
13352 }
13353 }
13354 }
13355 }
13356 if (lh->num_file_names < file || file == 0)
13357 dwarf2_debug_line_missing_file_complaint ();
13358 else
13359 {
13360 lh->file_names[file - 1].included_p = 1;
13361 if (!decode_for_pst_p)
13362 {
13363 addr = gdbarch_addr_bits_remove (gdbarch, address);
13364 (*p_record_line) (current_subfile, 0, addr);
13365 }
13366 }
13367 }
13368 }
13369
13370 /* Decode the Line Number Program (LNP) for the given line_header
13371 structure and CU. The actual information extracted and the type
13372 of structures created from the LNP depends on the value of PST.
13373
13374 1. If PST is NULL, then this procedure uses the data from the program
13375 to create all necessary symbol tables, and their linetables.
13376
13377 2. If PST is not NULL, this procedure reads the program to determine
13378 the list of files included by the unit represented by PST, and
13379 builds all the associated partial symbol tables.
13380
13381 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
13382 It is used for relative paths in the line table.
13383 NOTE: When processing partial symtabs (pst != NULL),
13384 comp_dir == pst->dirname.
13385
13386 NOTE: It is important that psymtabs have the same file name (via strcmp)
13387 as the corresponding symtab. Since COMP_DIR is not used in the name of the
13388 symtab we don't use it in the name of the psymtabs we create.
13389 E.g. expand_line_sal requires this when finding psymtabs to expand.
13390 A good testcase for this is mb-inline.exp. */
13391
13392 static void
13393 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
13394 struct dwarf2_cu *cu, struct partial_symtab *pst,
13395 int want_line_info)
13396 {
13397 struct objfile *objfile = cu->objfile;
13398 const int decode_for_pst_p = (pst != NULL);
13399 struct subfile *first_subfile = current_subfile;
13400
13401 if (want_line_info)
13402 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
13403
13404 if (decode_for_pst_p)
13405 {
13406 int file_index;
13407
13408 /* Now that we're done scanning the Line Header Program, we can
13409 create the psymtab of each included file. */
13410 for (file_index = 0; file_index < lh->num_file_names; file_index++)
13411 if (lh->file_names[file_index].included_p == 1)
13412 {
13413 char *include_name =
13414 psymtab_include_file_name (lh, file_index, pst, comp_dir);
13415 if (include_name != NULL)
13416 dwarf2_create_include_psymtab (include_name, pst, objfile);
13417 }
13418 }
13419 else
13420 {
13421 /* Make sure a symtab is created for every file, even files
13422 which contain only variables (i.e. no code with associated
13423 line numbers). */
13424 int i;
13425
13426 for (i = 0; i < lh->num_file_names; i++)
13427 {
13428 char *dir = NULL;
13429 struct file_entry *fe;
13430
13431 fe = &lh->file_names[i];
13432 if (fe->dir_index)
13433 dir = lh->include_dirs[fe->dir_index - 1];
13434 dwarf2_start_subfile (fe->name, dir, comp_dir);
13435
13436 /* Skip the main file; we don't need it, and it must be
13437 allocated last, so that it will show up before the
13438 non-primary symtabs in the objfile's symtab list. */
13439 if (current_subfile == first_subfile)
13440 continue;
13441
13442 if (current_subfile->symtab == NULL)
13443 current_subfile->symtab = allocate_symtab (current_subfile->name,
13444 objfile);
13445 fe->symtab = current_subfile->symtab;
13446 }
13447 }
13448 }
13449
13450 /* Start a subfile for DWARF. FILENAME is the name of the file and
13451 DIRNAME the name of the source directory which contains FILENAME
13452 or NULL if not known. COMP_DIR is the compilation directory for the
13453 linetable's compilation unit or NULL if not known.
13454 This routine tries to keep line numbers from identical absolute and
13455 relative file names in a common subfile.
13456
13457 Using the `list' example from the GDB testsuite, which resides in
13458 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
13459 of /srcdir/list0.c yields the following debugging information for list0.c:
13460
13461 DW_AT_name: /srcdir/list0.c
13462 DW_AT_comp_dir: /compdir
13463 files.files[0].name: list0.h
13464 files.files[0].dir: /srcdir
13465 files.files[1].name: list0.c
13466 files.files[1].dir: /srcdir
13467
13468 The line number information for list0.c has to end up in a single
13469 subfile, so that `break /srcdir/list0.c:1' works as expected.
13470 start_subfile will ensure that this happens provided that we pass the
13471 concatenation of files.files[1].dir and files.files[1].name as the
13472 subfile's name. */
13473
13474 static void
13475 dwarf2_start_subfile (char *filename, const char *dirname,
13476 const char *comp_dir)
13477 {
13478 char *fullname;
13479
13480 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
13481 `start_symtab' will always pass the contents of DW_AT_comp_dir as
13482 second argument to start_subfile. To be consistent, we do the
13483 same here. In order not to lose the line information directory,
13484 we concatenate it to the filename when it makes sense.
13485 Note that the Dwarf3 standard says (speaking of filenames in line
13486 information): ``The directory index is ignored for file names
13487 that represent full path names''. Thus ignoring dirname in the
13488 `else' branch below isn't an issue. */
13489
13490 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
13491 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
13492 else
13493 fullname = filename;
13494
13495 start_subfile (fullname, comp_dir);
13496
13497 if (fullname != filename)
13498 xfree (fullname);
13499 }
13500
13501 static void
13502 var_decode_location (struct attribute *attr, struct symbol *sym,
13503 struct dwarf2_cu *cu)
13504 {
13505 struct objfile *objfile = cu->objfile;
13506 struct comp_unit_head *cu_header = &cu->header;
13507
13508 /* NOTE drow/2003-01-30: There used to be a comment and some special
13509 code here to turn a symbol with DW_AT_external and a
13510 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
13511 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
13512 with some versions of binutils) where shared libraries could have
13513 relocations against symbols in their debug information - the
13514 minimal symbol would have the right address, but the debug info
13515 would not. It's no longer necessary, because we will explicitly
13516 apply relocations when we read in the debug information now. */
13517
13518 /* A DW_AT_location attribute with no contents indicates that a
13519 variable has been optimized away. */
13520 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
13521 {
13522 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
13523 return;
13524 }
13525
13526 /* Handle one degenerate form of location expression specially, to
13527 preserve GDB's previous behavior when section offsets are
13528 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
13529 then mark this symbol as LOC_STATIC. */
13530
13531 if (attr_form_is_block (attr)
13532 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
13533 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
13534 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
13535 && (DW_BLOCK (attr)->size
13536 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
13537 {
13538 unsigned int dummy;
13539
13540 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
13541 SYMBOL_VALUE_ADDRESS (sym) =
13542 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
13543 else
13544 SYMBOL_VALUE_ADDRESS (sym) =
13545 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
13546 SYMBOL_CLASS (sym) = LOC_STATIC;
13547 fixup_symbol_section (sym, objfile);
13548 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
13549 SYMBOL_SECTION (sym));
13550 return;
13551 }
13552
13553 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
13554 expression evaluator, and use LOC_COMPUTED only when necessary
13555 (i.e. when the value of a register or memory location is
13556 referenced, or a thread-local block, etc.). Then again, it might
13557 not be worthwhile. I'm assuming that it isn't unless performance
13558 or memory numbers show me otherwise. */
13559
13560 dwarf2_symbol_mark_computed (attr, sym, cu);
13561 SYMBOL_CLASS (sym) = LOC_COMPUTED;
13562
13563 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
13564 cu->has_loclist = 1;
13565 }
13566
13567 /* Given a pointer to a DWARF information entry, figure out if we need
13568 to make a symbol table entry for it, and if so, create a new entry
13569 and return a pointer to it.
13570 If TYPE is NULL, determine symbol type from the die, otherwise
13571 used the passed type.
13572 If SPACE is not NULL, use it to hold the new symbol. If it is
13573 NULL, allocate a new symbol on the objfile's obstack. */
13574
13575 static struct symbol *
13576 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
13577 struct symbol *space)
13578 {
13579 struct objfile *objfile = cu->objfile;
13580 struct symbol *sym = NULL;
13581 char *name;
13582 struct attribute *attr = NULL;
13583 struct attribute *attr2 = NULL;
13584 CORE_ADDR baseaddr;
13585 struct pending **list_to_add = NULL;
13586
13587 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13588
13589 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13590
13591 name = dwarf2_name (die, cu);
13592 if (name)
13593 {
13594 const char *linkagename;
13595 int suppress_add = 0;
13596
13597 if (space)
13598 sym = space;
13599 else
13600 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
13601 OBJSTAT (objfile, n_syms++);
13602
13603 /* Cache this symbol's name and the name's demangled form (if any). */
13604 SYMBOL_SET_LANGUAGE (sym, cu->language);
13605 linkagename = dwarf2_physname (name, die, cu);
13606 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
13607
13608 /* Fortran does not have mangling standard and the mangling does differ
13609 between gfortran, iFort etc. */
13610 if (cu->language == language_fortran
13611 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
13612 symbol_set_demangled_name (&(sym->ginfo),
13613 (char *) dwarf2_full_name (name, die, cu),
13614 NULL);
13615
13616 /* Default assumptions.
13617 Use the passed type or decode it from the die. */
13618 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
13619 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
13620 if (type != NULL)
13621 SYMBOL_TYPE (sym) = type;
13622 else
13623 SYMBOL_TYPE (sym) = die_type (die, cu);
13624 attr = dwarf2_attr (die,
13625 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
13626 cu);
13627 if (attr)
13628 {
13629 SYMBOL_LINE (sym) = DW_UNSND (attr);
13630 }
13631
13632 attr = dwarf2_attr (die,
13633 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
13634 cu);
13635 if (attr)
13636 {
13637 int file_index = DW_UNSND (attr);
13638
13639 if (cu->line_header == NULL
13640 || file_index > cu->line_header->num_file_names)
13641 complaint (&symfile_complaints,
13642 _("file index out of range"));
13643 else if (file_index > 0)
13644 {
13645 struct file_entry *fe;
13646
13647 fe = &cu->line_header->file_names[file_index - 1];
13648 SYMBOL_SYMTAB (sym) = fe->symtab;
13649 }
13650 }
13651
13652 switch (die->tag)
13653 {
13654 case DW_TAG_label:
13655 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13656 if (attr)
13657 {
13658 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
13659 }
13660 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
13661 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
13662 SYMBOL_CLASS (sym) = LOC_LABEL;
13663 add_symbol_to_list (sym, cu->list_in_scope);
13664 break;
13665 case DW_TAG_subprogram:
13666 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
13667 finish_block. */
13668 SYMBOL_CLASS (sym) = LOC_BLOCK;
13669 attr2 = dwarf2_attr (die, DW_AT_external, cu);
13670 if ((attr2 && (DW_UNSND (attr2) != 0))
13671 || cu->language == language_ada)
13672 {
13673 /* Subprograms marked external are stored as a global symbol.
13674 Ada subprograms, whether marked external or not, are always
13675 stored as a global symbol, because we want to be able to
13676 access them globally. For instance, we want to be able
13677 to break on a nested subprogram without having to
13678 specify the context. */
13679 list_to_add = &global_symbols;
13680 }
13681 else
13682 {
13683 list_to_add = cu->list_in_scope;
13684 }
13685 break;
13686 case DW_TAG_inlined_subroutine:
13687 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
13688 finish_block. */
13689 SYMBOL_CLASS (sym) = LOC_BLOCK;
13690 SYMBOL_INLINED (sym) = 1;
13691 list_to_add = cu->list_in_scope;
13692 break;
13693 case DW_TAG_template_value_param:
13694 suppress_add = 1;
13695 /* Fall through. */
13696 case DW_TAG_constant:
13697 case DW_TAG_variable:
13698 case DW_TAG_member:
13699 /* Compilation with minimal debug info may result in
13700 variables with missing type entries. Change the
13701 misleading `void' type to something sensible. */
13702 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
13703 SYMBOL_TYPE (sym)
13704 = objfile_type (objfile)->nodebug_data_symbol;
13705
13706 attr = dwarf2_attr (die, DW_AT_const_value, cu);
13707 /* In the case of DW_TAG_member, we should only be called for
13708 static const members. */
13709 if (die->tag == DW_TAG_member)
13710 {
13711 /* dwarf2_add_field uses die_is_declaration,
13712 so we do the same. */
13713 gdb_assert (die_is_declaration (die, cu));
13714 gdb_assert (attr);
13715 }
13716 if (attr)
13717 {
13718 dwarf2_const_value (attr, sym, cu);
13719 attr2 = dwarf2_attr (die, DW_AT_external, cu);
13720 if (!suppress_add)
13721 {
13722 if (attr2 && (DW_UNSND (attr2) != 0))
13723 list_to_add = &global_symbols;
13724 else
13725 list_to_add = cu->list_in_scope;
13726 }
13727 break;
13728 }
13729 attr = dwarf2_attr (die, DW_AT_location, cu);
13730 if (attr)
13731 {
13732 var_decode_location (attr, sym, cu);
13733 attr2 = dwarf2_attr (die, DW_AT_external, cu);
13734 if (SYMBOL_CLASS (sym) == LOC_STATIC
13735 && SYMBOL_VALUE_ADDRESS (sym) == 0
13736 && !dwarf2_per_objfile->has_section_at_zero)
13737 {
13738 /* When a static variable is eliminated by the linker,
13739 the corresponding debug information is not stripped
13740 out, but the variable address is set to null;
13741 do not add such variables into symbol table. */
13742 }
13743 else if (attr2 && (DW_UNSND (attr2) != 0))
13744 {
13745 /* Workaround gfortran PR debug/40040 - it uses
13746 DW_AT_location for variables in -fPIC libraries which may
13747 get overriden by other libraries/executable and get
13748 a different address. Resolve it by the minimal symbol
13749 which may come from inferior's executable using copy
13750 relocation. Make this workaround only for gfortran as for
13751 other compilers GDB cannot guess the minimal symbol
13752 Fortran mangling kind. */
13753 if (cu->language == language_fortran && die->parent
13754 && die->parent->tag == DW_TAG_module
13755 && cu->producer
13756 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
13757 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
13758
13759 /* A variable with DW_AT_external is never static,
13760 but it may be block-scoped. */
13761 list_to_add = (cu->list_in_scope == &file_symbols
13762 ? &global_symbols : cu->list_in_scope);
13763 }
13764 else
13765 list_to_add = cu->list_in_scope;
13766 }
13767 else
13768 {
13769 /* We do not know the address of this symbol.
13770 If it is an external symbol and we have type information
13771 for it, enter the symbol as a LOC_UNRESOLVED symbol.
13772 The address of the variable will then be determined from
13773 the minimal symbol table whenever the variable is
13774 referenced. */
13775 attr2 = dwarf2_attr (die, DW_AT_external, cu);
13776 if (attr2 && (DW_UNSND (attr2) != 0)
13777 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
13778 {
13779 /* A variable with DW_AT_external is never static, but it
13780 may be block-scoped. */
13781 list_to_add = (cu->list_in_scope == &file_symbols
13782 ? &global_symbols : cu->list_in_scope);
13783
13784 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
13785 }
13786 else if (!die_is_declaration (die, cu))
13787 {
13788 /* Use the default LOC_OPTIMIZED_OUT class. */
13789 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
13790 if (!suppress_add)
13791 list_to_add = cu->list_in_scope;
13792 }
13793 }
13794 break;
13795 case DW_TAG_formal_parameter:
13796 /* If we are inside a function, mark this as an argument. If
13797 not, we might be looking at an argument to an inlined function
13798 when we do not have enough information to show inlined frames;
13799 pretend it's a local variable in that case so that the user can
13800 still see it. */
13801 if (context_stack_depth > 0
13802 && context_stack[context_stack_depth - 1].name != NULL)
13803 SYMBOL_IS_ARGUMENT (sym) = 1;
13804 attr = dwarf2_attr (die, DW_AT_location, cu);
13805 if (attr)
13806 {
13807 var_decode_location (attr, sym, cu);
13808 }
13809 attr = dwarf2_attr (die, DW_AT_const_value, cu);
13810 if (attr)
13811 {
13812 dwarf2_const_value (attr, sym, cu);
13813 }
13814
13815 list_to_add = cu->list_in_scope;
13816 break;
13817 case DW_TAG_unspecified_parameters:
13818 /* From varargs functions; gdb doesn't seem to have any
13819 interest in this information, so just ignore it for now.
13820 (FIXME?) */
13821 break;
13822 case DW_TAG_template_type_param:
13823 suppress_add = 1;
13824 /* Fall through. */
13825 case DW_TAG_class_type:
13826 case DW_TAG_interface_type:
13827 case DW_TAG_structure_type:
13828 case DW_TAG_union_type:
13829 case DW_TAG_set_type:
13830 case DW_TAG_enumeration_type:
13831 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13832 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
13833
13834 {
13835 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
13836 really ever be static objects: otherwise, if you try
13837 to, say, break of a class's method and you're in a file
13838 which doesn't mention that class, it won't work unless
13839 the check for all static symbols in lookup_symbol_aux
13840 saves you. See the OtherFileClass tests in
13841 gdb.c++/namespace.exp. */
13842
13843 if (!suppress_add)
13844 {
13845 list_to_add = (cu->list_in_scope == &file_symbols
13846 && (cu->language == language_cplus
13847 || cu->language == language_java)
13848 ? &global_symbols : cu->list_in_scope);
13849
13850 /* The semantics of C++ state that "struct foo {
13851 ... }" also defines a typedef for "foo". A Java
13852 class declaration also defines a typedef for the
13853 class. */
13854 if (cu->language == language_cplus
13855 || cu->language == language_java
13856 || cu->language == language_ada)
13857 {
13858 /* The symbol's name is already allocated along
13859 with this objfile, so we don't need to
13860 duplicate it for the type. */
13861 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
13862 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
13863 }
13864 }
13865 }
13866 break;
13867 case DW_TAG_typedef:
13868 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13869 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
13870 list_to_add = cu->list_in_scope;
13871 break;
13872 case DW_TAG_base_type:
13873 case DW_TAG_subrange_type:
13874 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13875 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
13876 list_to_add = cu->list_in_scope;
13877 break;
13878 case DW_TAG_enumerator:
13879 attr = dwarf2_attr (die, DW_AT_const_value, cu);
13880 if (attr)
13881 {
13882 dwarf2_const_value (attr, sym, cu);
13883 }
13884 {
13885 /* NOTE: carlton/2003-11-10: See comment above in the
13886 DW_TAG_class_type, etc. block. */
13887
13888 list_to_add = (cu->list_in_scope == &file_symbols
13889 && (cu->language == language_cplus
13890 || cu->language == language_java)
13891 ? &global_symbols : cu->list_in_scope);
13892 }
13893 break;
13894 case DW_TAG_namespace:
13895 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13896 list_to_add = &global_symbols;
13897 break;
13898 default:
13899 /* Not a tag we recognize. Hopefully we aren't processing
13900 trash data, but since we must specifically ignore things
13901 we don't recognize, there is nothing else we should do at
13902 this point. */
13903 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
13904 dwarf_tag_name (die->tag));
13905 break;
13906 }
13907
13908 if (suppress_add)
13909 {
13910 sym->hash_next = objfile->template_symbols;
13911 objfile->template_symbols = sym;
13912 list_to_add = NULL;
13913 }
13914
13915 if (list_to_add != NULL)
13916 add_symbol_to_list (sym, list_to_add);
13917
13918 /* For the benefit of old versions of GCC, check for anonymous
13919 namespaces based on the demangled name. */
13920 if (!processing_has_namespace_info
13921 && cu->language == language_cplus)
13922 cp_scan_for_anonymous_namespaces (sym, objfile);
13923 }
13924 return (sym);
13925 }
13926
13927 /* A wrapper for new_symbol_full that always allocates a new symbol. */
13928
13929 static struct symbol *
13930 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
13931 {
13932 return new_symbol_full (die, type, cu, NULL);
13933 }
13934
13935 /* Given an attr with a DW_FORM_dataN value in host byte order,
13936 zero-extend it as appropriate for the symbol's type. The DWARF
13937 standard (v4) is not entirely clear about the meaning of using
13938 DW_FORM_dataN for a constant with a signed type, where the type is
13939 wider than the data. The conclusion of a discussion on the DWARF
13940 list was that this is unspecified. We choose to always zero-extend
13941 because that is the interpretation long in use by GCC. */
13942
13943 static gdb_byte *
13944 dwarf2_const_value_data (struct attribute *attr, struct type *type,
13945 const char *name, struct obstack *obstack,
13946 struct dwarf2_cu *cu, LONGEST *value, int bits)
13947 {
13948 struct objfile *objfile = cu->objfile;
13949 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
13950 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
13951 LONGEST l = DW_UNSND (attr);
13952
13953 if (bits < sizeof (*value) * 8)
13954 {
13955 l &= ((LONGEST) 1 << bits) - 1;
13956 *value = l;
13957 }
13958 else if (bits == sizeof (*value) * 8)
13959 *value = l;
13960 else
13961 {
13962 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
13963 store_unsigned_integer (bytes, bits / 8, byte_order, l);
13964 return bytes;
13965 }
13966
13967 return NULL;
13968 }
13969
13970 /* Read a constant value from an attribute. Either set *VALUE, or if
13971 the value does not fit in *VALUE, set *BYTES - either already
13972 allocated on the objfile obstack, or newly allocated on OBSTACK,
13973 or, set *BATON, if we translated the constant to a location
13974 expression. */
13975
13976 static void
13977 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
13978 const char *name, struct obstack *obstack,
13979 struct dwarf2_cu *cu,
13980 LONGEST *value, gdb_byte **bytes,
13981 struct dwarf2_locexpr_baton **baton)
13982 {
13983 struct objfile *objfile = cu->objfile;
13984 struct comp_unit_head *cu_header = &cu->header;
13985 struct dwarf_block *blk;
13986 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
13987 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
13988
13989 *value = 0;
13990 *bytes = NULL;
13991 *baton = NULL;
13992
13993 switch (attr->form)
13994 {
13995 case DW_FORM_addr:
13996 case DW_FORM_GNU_addr_index:
13997 {
13998 gdb_byte *data;
13999
14000 if (TYPE_LENGTH (type) != cu_header->addr_size)
14001 dwarf2_const_value_length_mismatch_complaint (name,
14002 cu_header->addr_size,
14003 TYPE_LENGTH (type));
14004 /* Symbols of this form are reasonably rare, so we just
14005 piggyback on the existing location code rather than writing
14006 a new implementation of symbol_computed_ops. */
14007 *baton = obstack_alloc (&objfile->objfile_obstack,
14008 sizeof (struct dwarf2_locexpr_baton));
14009 (*baton)->per_cu = cu->per_cu;
14010 gdb_assert ((*baton)->per_cu);
14011
14012 (*baton)->size = 2 + cu_header->addr_size;
14013 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
14014 (*baton)->data = data;
14015
14016 data[0] = DW_OP_addr;
14017 store_unsigned_integer (&data[1], cu_header->addr_size,
14018 byte_order, DW_ADDR (attr));
14019 data[cu_header->addr_size + 1] = DW_OP_stack_value;
14020 }
14021 break;
14022 case DW_FORM_string:
14023 case DW_FORM_strp:
14024 case DW_FORM_GNU_str_index:
14025 /* DW_STRING is already allocated on the objfile obstack, point
14026 directly to it. */
14027 *bytes = (gdb_byte *) DW_STRING (attr);
14028 break;
14029 case DW_FORM_block1:
14030 case DW_FORM_block2:
14031 case DW_FORM_block4:
14032 case DW_FORM_block:
14033 case DW_FORM_exprloc:
14034 blk = DW_BLOCK (attr);
14035 if (TYPE_LENGTH (type) != blk->size)
14036 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
14037 TYPE_LENGTH (type));
14038 *bytes = blk->data;
14039 break;
14040
14041 /* The DW_AT_const_value attributes are supposed to carry the
14042 symbol's value "represented as it would be on the target
14043 architecture." By the time we get here, it's already been
14044 converted to host endianness, so we just need to sign- or
14045 zero-extend it as appropriate. */
14046 case DW_FORM_data1:
14047 *bytes = dwarf2_const_value_data (attr, type, name,
14048 obstack, cu, value, 8);
14049 break;
14050 case DW_FORM_data2:
14051 *bytes = dwarf2_const_value_data (attr, type, name,
14052 obstack, cu, value, 16);
14053 break;
14054 case DW_FORM_data4:
14055 *bytes = dwarf2_const_value_data (attr, type, name,
14056 obstack, cu, value, 32);
14057 break;
14058 case DW_FORM_data8:
14059 *bytes = dwarf2_const_value_data (attr, type, name,
14060 obstack, cu, value, 64);
14061 break;
14062
14063 case DW_FORM_sdata:
14064 *value = DW_SND (attr);
14065 break;
14066
14067 case DW_FORM_udata:
14068 *value = DW_UNSND (attr);
14069 break;
14070
14071 default:
14072 complaint (&symfile_complaints,
14073 _("unsupported const value attribute form: '%s'"),
14074 dwarf_form_name (attr->form));
14075 *value = 0;
14076 break;
14077 }
14078 }
14079
14080
14081 /* Copy constant value from an attribute to a symbol. */
14082
14083 static void
14084 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
14085 struct dwarf2_cu *cu)
14086 {
14087 struct objfile *objfile = cu->objfile;
14088 struct comp_unit_head *cu_header = &cu->header;
14089 LONGEST value;
14090 gdb_byte *bytes;
14091 struct dwarf2_locexpr_baton *baton;
14092
14093 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
14094 SYMBOL_PRINT_NAME (sym),
14095 &objfile->objfile_obstack, cu,
14096 &value, &bytes, &baton);
14097
14098 if (baton != NULL)
14099 {
14100 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
14101 SYMBOL_LOCATION_BATON (sym) = baton;
14102 SYMBOL_CLASS (sym) = LOC_COMPUTED;
14103 }
14104 else if (bytes != NULL)
14105 {
14106 SYMBOL_VALUE_BYTES (sym) = bytes;
14107 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
14108 }
14109 else
14110 {
14111 SYMBOL_VALUE (sym) = value;
14112 SYMBOL_CLASS (sym) = LOC_CONST;
14113 }
14114 }
14115
14116 /* Return the type of the die in question using its DW_AT_type attribute. */
14117
14118 static struct type *
14119 die_type (struct die_info *die, struct dwarf2_cu *cu)
14120 {
14121 struct attribute *type_attr;
14122
14123 type_attr = dwarf2_attr (die, DW_AT_type, cu);
14124 if (!type_attr)
14125 {
14126 /* A missing DW_AT_type represents a void type. */
14127 return objfile_type (cu->objfile)->builtin_void;
14128 }
14129
14130 return lookup_die_type (die, type_attr, cu);
14131 }
14132
14133 /* True iff CU's producer generates GNAT Ada auxiliary information
14134 that allows to find parallel types through that information instead
14135 of having to do expensive parallel lookups by type name. */
14136
14137 static int
14138 need_gnat_info (struct dwarf2_cu *cu)
14139 {
14140 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
14141 of GNAT produces this auxiliary information, without any indication
14142 that it is produced. Part of enhancing the FSF version of GNAT
14143 to produce that information will be to put in place an indicator
14144 that we can use in order to determine whether the descriptive type
14145 info is available or not. One suggestion that has been made is
14146 to use a new attribute, attached to the CU die. For now, assume
14147 that the descriptive type info is not available. */
14148 return 0;
14149 }
14150
14151 /* Return the auxiliary type of the die in question using its
14152 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
14153 attribute is not present. */
14154
14155 static struct type *
14156 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
14157 {
14158 struct attribute *type_attr;
14159
14160 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
14161 if (!type_attr)
14162 return NULL;
14163
14164 return lookup_die_type (die, type_attr, cu);
14165 }
14166
14167 /* If DIE has a descriptive_type attribute, then set the TYPE's
14168 descriptive type accordingly. */
14169
14170 static void
14171 set_descriptive_type (struct type *type, struct die_info *die,
14172 struct dwarf2_cu *cu)
14173 {
14174 struct type *descriptive_type = die_descriptive_type (die, cu);
14175
14176 if (descriptive_type)
14177 {
14178 ALLOCATE_GNAT_AUX_TYPE (type);
14179 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
14180 }
14181 }
14182
14183 /* Return the containing type of the die in question using its
14184 DW_AT_containing_type attribute. */
14185
14186 static struct type *
14187 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14188 {
14189 struct attribute *type_attr;
14190
14191 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
14192 if (!type_attr)
14193 error (_("Dwarf Error: Problem turning containing type into gdb type "
14194 "[in module %s]"), cu->objfile->name);
14195
14196 return lookup_die_type (die, type_attr, cu);
14197 }
14198
14199 /* Look up the type of DIE in CU using its type attribute ATTR.
14200 If there is no type substitute an error marker. */
14201
14202 static struct type *
14203 lookup_die_type (struct die_info *die, struct attribute *attr,
14204 struct dwarf2_cu *cu)
14205 {
14206 struct objfile *objfile = cu->objfile;
14207 struct type *this_type;
14208
14209 /* First see if we have it cached. */
14210
14211 if (is_ref_attr (attr))
14212 {
14213 sect_offset offset = dwarf2_get_ref_die_offset (attr);
14214
14215 this_type = get_die_type_at_offset (offset, cu->per_cu);
14216 }
14217 else if (attr->form == DW_FORM_ref_sig8)
14218 {
14219 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
14220
14221 /* sig_type will be NULL if the signatured type is missing from
14222 the debug info. */
14223 if (sig_type == NULL)
14224 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
14225 "at 0x%x [in module %s]"),
14226 die->offset.sect_off, objfile->name);
14227
14228 gdb_assert (sig_type->per_cu.is_debug_types);
14229 /* If we haven't filled in type_offset_in_section yet, then we
14230 haven't read the type in yet. */
14231 this_type = NULL;
14232 if (sig_type->type_offset_in_section.sect_off != 0)
14233 {
14234 this_type =
14235 get_die_type_at_offset (sig_type->type_offset_in_section,
14236 &sig_type->per_cu);
14237 }
14238 }
14239 else
14240 {
14241 dump_die_for_error (die);
14242 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
14243 dwarf_attr_name (attr->name), objfile->name);
14244 }
14245
14246 /* If not cached we need to read it in. */
14247
14248 if (this_type == NULL)
14249 {
14250 struct die_info *type_die;
14251 struct dwarf2_cu *type_cu = cu;
14252
14253 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
14254 /* If we found the type now, it's probably because the type came
14255 from an inter-CU reference and the type's CU got expanded before
14256 ours. */
14257 this_type = get_die_type (type_die, type_cu);
14258 if (this_type == NULL)
14259 this_type = read_type_die_1 (type_die, type_cu);
14260 }
14261
14262 /* If we still don't have a type use an error marker. */
14263
14264 if (this_type == NULL)
14265 {
14266 char *message, *saved;
14267
14268 /* read_type_die already issued a complaint. */
14269 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
14270 objfile->name,
14271 cu->header.offset.sect_off,
14272 die->offset.sect_off);
14273 saved = obstack_copy0 (&objfile->objfile_obstack,
14274 message, strlen (message));
14275 xfree (message);
14276
14277 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
14278 }
14279
14280 return this_type;
14281 }
14282
14283 /* Return the type in DIE, CU.
14284 Returns NULL for invalid types.
14285
14286 This first does a lookup in the appropriate type_hash table,
14287 and only reads the die in if necessary.
14288
14289 NOTE: This can be called when reading in partial or full symbols. */
14290
14291 static struct type *
14292 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
14293 {
14294 struct type *this_type;
14295
14296 this_type = get_die_type (die, cu);
14297 if (this_type)
14298 return this_type;
14299
14300 return read_type_die_1 (die, cu);
14301 }
14302
14303 /* Read the type in DIE, CU.
14304 Returns NULL for invalid types. */
14305
14306 static struct type *
14307 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
14308 {
14309 struct type *this_type = NULL;
14310
14311 switch (die->tag)
14312 {
14313 case DW_TAG_class_type:
14314 case DW_TAG_interface_type:
14315 case DW_TAG_structure_type:
14316 case DW_TAG_union_type:
14317 this_type = read_structure_type (die, cu);
14318 break;
14319 case DW_TAG_enumeration_type:
14320 this_type = read_enumeration_type (die, cu);
14321 break;
14322 case DW_TAG_subprogram:
14323 case DW_TAG_subroutine_type:
14324 case DW_TAG_inlined_subroutine:
14325 this_type = read_subroutine_type (die, cu);
14326 break;
14327 case DW_TAG_array_type:
14328 this_type = read_array_type (die, cu);
14329 break;
14330 case DW_TAG_set_type:
14331 this_type = read_set_type (die, cu);
14332 break;
14333 case DW_TAG_pointer_type:
14334 this_type = read_tag_pointer_type (die, cu);
14335 break;
14336 case DW_TAG_ptr_to_member_type:
14337 this_type = read_tag_ptr_to_member_type (die, cu);
14338 break;
14339 case DW_TAG_reference_type:
14340 this_type = read_tag_reference_type (die, cu);
14341 break;
14342 case DW_TAG_const_type:
14343 this_type = read_tag_const_type (die, cu);
14344 break;
14345 case DW_TAG_volatile_type:
14346 this_type = read_tag_volatile_type (die, cu);
14347 break;
14348 case DW_TAG_string_type:
14349 this_type = read_tag_string_type (die, cu);
14350 break;
14351 case DW_TAG_typedef:
14352 this_type = read_typedef (die, cu);
14353 break;
14354 case DW_TAG_subrange_type:
14355 this_type = read_subrange_type (die, cu);
14356 break;
14357 case DW_TAG_base_type:
14358 this_type = read_base_type (die, cu);
14359 break;
14360 case DW_TAG_unspecified_type:
14361 this_type = read_unspecified_type (die, cu);
14362 break;
14363 case DW_TAG_namespace:
14364 this_type = read_namespace_type (die, cu);
14365 break;
14366 case DW_TAG_module:
14367 this_type = read_module_type (die, cu);
14368 break;
14369 default:
14370 complaint (&symfile_complaints,
14371 _("unexpected tag in read_type_die: '%s'"),
14372 dwarf_tag_name (die->tag));
14373 break;
14374 }
14375
14376 return this_type;
14377 }
14378
14379 /* See if we can figure out if the class lives in a namespace. We do
14380 this by looking for a member function; its demangled name will
14381 contain namespace info, if there is any.
14382 Return the computed name or NULL.
14383 Space for the result is allocated on the objfile's obstack.
14384 This is the full-die version of guess_partial_die_structure_name.
14385 In this case we know DIE has no useful parent. */
14386
14387 static char *
14388 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
14389 {
14390 struct die_info *spec_die;
14391 struct dwarf2_cu *spec_cu;
14392 struct die_info *child;
14393
14394 spec_cu = cu;
14395 spec_die = die_specification (die, &spec_cu);
14396 if (spec_die != NULL)
14397 {
14398 die = spec_die;
14399 cu = spec_cu;
14400 }
14401
14402 for (child = die->child;
14403 child != NULL;
14404 child = child->sibling)
14405 {
14406 if (child->tag == DW_TAG_subprogram)
14407 {
14408 struct attribute *attr;
14409
14410 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
14411 if (attr == NULL)
14412 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
14413 if (attr != NULL)
14414 {
14415 char *actual_name
14416 = language_class_name_from_physname (cu->language_defn,
14417 DW_STRING (attr));
14418 char *name = NULL;
14419
14420 if (actual_name != NULL)
14421 {
14422 char *die_name = dwarf2_name (die, cu);
14423
14424 if (die_name != NULL
14425 && strcmp (die_name, actual_name) != 0)
14426 {
14427 /* Strip off the class name from the full name.
14428 We want the prefix. */
14429 int die_name_len = strlen (die_name);
14430 int actual_name_len = strlen (actual_name);
14431
14432 /* Test for '::' as a sanity check. */
14433 if (actual_name_len > die_name_len + 2
14434 && actual_name[actual_name_len
14435 - die_name_len - 1] == ':')
14436 name =
14437 obsavestring (actual_name,
14438 actual_name_len - die_name_len - 2,
14439 &cu->objfile->objfile_obstack);
14440 }
14441 }
14442 xfree (actual_name);
14443 return name;
14444 }
14445 }
14446 }
14447
14448 return NULL;
14449 }
14450
14451 /* GCC might emit a nameless typedef that has a linkage name. Determine the
14452 prefix part in such case. See
14453 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14454
14455 static char *
14456 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
14457 {
14458 struct attribute *attr;
14459 char *base;
14460
14461 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
14462 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
14463 return NULL;
14464
14465 attr = dwarf2_attr (die, DW_AT_name, cu);
14466 if (attr != NULL && DW_STRING (attr) != NULL)
14467 return NULL;
14468
14469 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
14470 if (attr == NULL)
14471 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
14472 if (attr == NULL || DW_STRING (attr) == NULL)
14473 return NULL;
14474
14475 /* dwarf2_name had to be already called. */
14476 gdb_assert (DW_STRING_IS_CANONICAL (attr));
14477
14478 /* Strip the base name, keep any leading namespaces/classes. */
14479 base = strrchr (DW_STRING (attr), ':');
14480 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
14481 return "";
14482
14483 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
14484 &cu->objfile->objfile_obstack);
14485 }
14486
14487 /* Return the name of the namespace/class that DIE is defined within,
14488 or "" if we can't tell. The caller should not xfree the result.
14489
14490 For example, if we're within the method foo() in the following
14491 code:
14492
14493 namespace N {
14494 class C {
14495 void foo () {
14496 }
14497 };
14498 }
14499
14500 then determine_prefix on foo's die will return "N::C". */
14501
14502 static const char *
14503 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
14504 {
14505 struct die_info *parent, *spec_die;
14506 struct dwarf2_cu *spec_cu;
14507 struct type *parent_type;
14508 char *retval;
14509
14510 if (cu->language != language_cplus && cu->language != language_java
14511 && cu->language != language_fortran)
14512 return "";
14513
14514 retval = anonymous_struct_prefix (die, cu);
14515 if (retval)
14516 return retval;
14517
14518 /* We have to be careful in the presence of DW_AT_specification.
14519 For example, with GCC 3.4, given the code
14520
14521 namespace N {
14522 void foo() {
14523 // Definition of N::foo.
14524 }
14525 }
14526
14527 then we'll have a tree of DIEs like this:
14528
14529 1: DW_TAG_compile_unit
14530 2: DW_TAG_namespace // N
14531 3: DW_TAG_subprogram // declaration of N::foo
14532 4: DW_TAG_subprogram // definition of N::foo
14533 DW_AT_specification // refers to die #3
14534
14535 Thus, when processing die #4, we have to pretend that we're in
14536 the context of its DW_AT_specification, namely the contex of die
14537 #3. */
14538 spec_cu = cu;
14539 spec_die = die_specification (die, &spec_cu);
14540 if (spec_die == NULL)
14541 parent = die->parent;
14542 else
14543 {
14544 parent = spec_die->parent;
14545 cu = spec_cu;
14546 }
14547
14548 if (parent == NULL)
14549 return "";
14550 else if (parent->building_fullname)
14551 {
14552 const char *name;
14553 const char *parent_name;
14554
14555 /* It has been seen on RealView 2.2 built binaries,
14556 DW_TAG_template_type_param types actually _defined_ as
14557 children of the parent class:
14558
14559 enum E {};
14560 template class <class Enum> Class{};
14561 Class<enum E> class_e;
14562
14563 1: DW_TAG_class_type (Class)
14564 2: DW_TAG_enumeration_type (E)
14565 3: DW_TAG_enumerator (enum1:0)
14566 3: DW_TAG_enumerator (enum2:1)
14567 ...
14568 2: DW_TAG_template_type_param
14569 DW_AT_type DW_FORM_ref_udata (E)
14570
14571 Besides being broken debug info, it can put GDB into an
14572 infinite loop. Consider:
14573
14574 When we're building the full name for Class<E>, we'll start
14575 at Class, and go look over its template type parameters,
14576 finding E. We'll then try to build the full name of E, and
14577 reach here. We're now trying to build the full name of E,
14578 and look over the parent DIE for containing scope. In the
14579 broken case, if we followed the parent DIE of E, we'd again
14580 find Class, and once again go look at its template type
14581 arguments, etc., etc. Simply don't consider such parent die
14582 as source-level parent of this die (it can't be, the language
14583 doesn't allow it), and break the loop here. */
14584 name = dwarf2_name (die, cu);
14585 parent_name = dwarf2_name (parent, cu);
14586 complaint (&symfile_complaints,
14587 _("template param type '%s' defined within parent '%s'"),
14588 name ? name : "<unknown>",
14589 parent_name ? parent_name : "<unknown>");
14590 return "";
14591 }
14592 else
14593 switch (parent->tag)
14594 {
14595 case DW_TAG_namespace:
14596 parent_type = read_type_die (parent, cu);
14597 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
14598 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
14599 Work around this problem here. */
14600 if (cu->language == language_cplus
14601 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
14602 return "";
14603 /* We give a name to even anonymous namespaces. */
14604 return TYPE_TAG_NAME (parent_type);
14605 case DW_TAG_class_type:
14606 case DW_TAG_interface_type:
14607 case DW_TAG_structure_type:
14608 case DW_TAG_union_type:
14609 case DW_TAG_module:
14610 parent_type = read_type_die (parent, cu);
14611 if (TYPE_TAG_NAME (parent_type) != NULL)
14612 return TYPE_TAG_NAME (parent_type);
14613 else
14614 /* An anonymous structure is only allowed non-static data
14615 members; no typedefs, no member functions, et cetera.
14616 So it does not need a prefix. */
14617 return "";
14618 case DW_TAG_compile_unit:
14619 case DW_TAG_partial_unit:
14620 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
14621 if (cu->language == language_cplus
14622 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
14623 && die->child != NULL
14624 && (die->tag == DW_TAG_class_type
14625 || die->tag == DW_TAG_structure_type
14626 || die->tag == DW_TAG_union_type))
14627 {
14628 char *name = guess_full_die_structure_name (die, cu);
14629 if (name != NULL)
14630 return name;
14631 }
14632 return "";
14633 default:
14634 return determine_prefix (parent, cu);
14635 }
14636 }
14637
14638 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
14639 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
14640 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
14641 an obconcat, otherwise allocate storage for the result. The CU argument is
14642 used to determine the language and hence, the appropriate separator. */
14643
14644 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
14645
14646 static char *
14647 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
14648 int physname, struct dwarf2_cu *cu)
14649 {
14650 const char *lead = "";
14651 const char *sep;
14652
14653 if (suffix == NULL || suffix[0] == '\0'
14654 || prefix == NULL || prefix[0] == '\0')
14655 sep = "";
14656 else if (cu->language == language_java)
14657 sep = ".";
14658 else if (cu->language == language_fortran && physname)
14659 {
14660 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
14661 DW_AT_MIPS_linkage_name is preferred and used instead. */
14662
14663 lead = "__";
14664 sep = "_MOD_";
14665 }
14666 else
14667 sep = "::";
14668
14669 if (prefix == NULL)
14670 prefix = "";
14671 if (suffix == NULL)
14672 suffix = "";
14673
14674 if (obs == NULL)
14675 {
14676 char *retval
14677 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
14678
14679 strcpy (retval, lead);
14680 strcat (retval, prefix);
14681 strcat (retval, sep);
14682 strcat (retval, suffix);
14683 return retval;
14684 }
14685 else
14686 {
14687 /* We have an obstack. */
14688 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
14689 }
14690 }
14691
14692 /* Return sibling of die, NULL if no sibling. */
14693
14694 static struct die_info *
14695 sibling_die (struct die_info *die)
14696 {
14697 return die->sibling;
14698 }
14699
14700 /* Get name of a die, return NULL if not found. */
14701
14702 static char *
14703 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
14704 struct obstack *obstack)
14705 {
14706 if (name && cu->language == language_cplus)
14707 {
14708 char *canon_name = cp_canonicalize_string (name);
14709
14710 if (canon_name != NULL)
14711 {
14712 if (strcmp (canon_name, name) != 0)
14713 name = obsavestring (canon_name, strlen (canon_name),
14714 obstack);
14715 xfree (canon_name);
14716 }
14717 }
14718
14719 return name;
14720 }
14721
14722 /* Get name of a die, return NULL if not found. */
14723
14724 static char *
14725 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
14726 {
14727 struct attribute *attr;
14728
14729 attr = dwarf2_attr (die, DW_AT_name, cu);
14730 if ((!attr || !DW_STRING (attr))
14731 && die->tag != DW_TAG_class_type
14732 && die->tag != DW_TAG_interface_type
14733 && die->tag != DW_TAG_structure_type
14734 && die->tag != DW_TAG_union_type)
14735 return NULL;
14736
14737 switch (die->tag)
14738 {
14739 case DW_TAG_compile_unit:
14740 case DW_TAG_partial_unit:
14741 /* Compilation units have a DW_AT_name that is a filename, not
14742 a source language identifier. */
14743 case DW_TAG_enumeration_type:
14744 case DW_TAG_enumerator:
14745 /* These tags always have simple identifiers already; no need
14746 to canonicalize them. */
14747 return DW_STRING (attr);
14748
14749 case DW_TAG_subprogram:
14750 /* Java constructors will all be named "<init>", so return
14751 the class name when we see this special case. */
14752 if (cu->language == language_java
14753 && DW_STRING (attr) != NULL
14754 && strcmp (DW_STRING (attr), "<init>") == 0)
14755 {
14756 struct dwarf2_cu *spec_cu = cu;
14757 struct die_info *spec_die;
14758
14759 /* GCJ will output '<init>' for Java constructor names.
14760 For this special case, return the name of the parent class. */
14761
14762 /* GCJ may output suprogram DIEs with AT_specification set.
14763 If so, use the name of the specified DIE. */
14764 spec_die = die_specification (die, &spec_cu);
14765 if (spec_die != NULL)
14766 return dwarf2_name (spec_die, spec_cu);
14767
14768 do
14769 {
14770 die = die->parent;
14771 if (die->tag == DW_TAG_class_type)
14772 return dwarf2_name (die, cu);
14773 }
14774 while (die->tag != DW_TAG_compile_unit
14775 && die->tag != DW_TAG_partial_unit);
14776 }
14777 break;
14778
14779 case DW_TAG_class_type:
14780 case DW_TAG_interface_type:
14781 case DW_TAG_structure_type:
14782 case DW_TAG_union_type:
14783 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
14784 structures or unions. These were of the form "._%d" in GCC 4.1,
14785 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
14786 and GCC 4.4. We work around this problem by ignoring these. */
14787 if (attr && DW_STRING (attr)
14788 && (strncmp (DW_STRING (attr), "._", 2) == 0
14789 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
14790 return NULL;
14791
14792 /* GCC might emit a nameless typedef that has a linkage name. See
14793 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14794 if (!attr || DW_STRING (attr) == NULL)
14795 {
14796 char *demangled = NULL;
14797
14798 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
14799 if (attr == NULL)
14800 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
14801
14802 if (attr == NULL || DW_STRING (attr) == NULL)
14803 return NULL;
14804
14805 /* Avoid demangling DW_STRING (attr) the second time on a second
14806 call for the same DIE. */
14807 if (!DW_STRING_IS_CANONICAL (attr))
14808 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
14809
14810 if (demangled)
14811 {
14812 char *base;
14813
14814 /* FIXME: we already did this for the partial symbol... */
14815 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
14816 &cu->objfile->objfile_obstack);
14817 DW_STRING_IS_CANONICAL (attr) = 1;
14818 xfree (demangled);
14819
14820 /* Strip any leading namespaces/classes, keep only the base name.
14821 DW_AT_name for named DIEs does not contain the prefixes. */
14822 base = strrchr (DW_STRING (attr), ':');
14823 if (base && base > DW_STRING (attr) && base[-1] == ':')
14824 return &base[1];
14825 else
14826 return DW_STRING (attr);
14827 }
14828 }
14829 break;
14830
14831 default:
14832 break;
14833 }
14834
14835 if (!DW_STRING_IS_CANONICAL (attr))
14836 {
14837 DW_STRING (attr)
14838 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
14839 &cu->objfile->objfile_obstack);
14840 DW_STRING_IS_CANONICAL (attr) = 1;
14841 }
14842 return DW_STRING (attr);
14843 }
14844
14845 /* Return the die that this die in an extension of, or NULL if there
14846 is none. *EXT_CU is the CU containing DIE on input, and the CU
14847 containing the return value on output. */
14848
14849 static struct die_info *
14850 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
14851 {
14852 struct attribute *attr;
14853
14854 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
14855 if (attr == NULL)
14856 return NULL;
14857
14858 return follow_die_ref (die, attr, ext_cu);
14859 }
14860
14861 /* Convert a DIE tag into its string name. */
14862
14863 static const char *
14864 dwarf_tag_name (unsigned tag)
14865 {
14866 const char *name = get_DW_TAG_name (tag);
14867
14868 if (name == NULL)
14869 return "DW_TAG_<unknown>";
14870
14871 return name;
14872 }
14873
14874 /* Convert a DWARF attribute code into its string name. */
14875
14876 static const char *
14877 dwarf_attr_name (unsigned attr)
14878 {
14879 const char *name;
14880
14881 #ifdef MIPS /* collides with DW_AT_HP_block_index */
14882 if (attr == DW_AT_MIPS_fde)
14883 return "DW_AT_MIPS_fde";
14884 #else
14885 if (attr == DW_AT_HP_block_index)
14886 return "DW_AT_HP_block_index";
14887 #endif
14888
14889 name = get_DW_AT_name (attr);
14890
14891 if (name == NULL)
14892 return "DW_AT_<unknown>";
14893
14894 return name;
14895 }
14896
14897 /* Convert a DWARF value form code into its string name. */
14898
14899 static const char *
14900 dwarf_form_name (unsigned form)
14901 {
14902 const char *name = get_DW_FORM_name (form);
14903
14904 if (name == NULL)
14905 return "DW_FORM_<unknown>";
14906
14907 return name;
14908 }
14909
14910 static char *
14911 dwarf_bool_name (unsigned mybool)
14912 {
14913 if (mybool)
14914 return "TRUE";
14915 else
14916 return "FALSE";
14917 }
14918
14919 /* Convert a DWARF type code into its string name. */
14920
14921 static const char *
14922 dwarf_type_encoding_name (unsigned enc)
14923 {
14924 const char *name = get_DW_ATE_name (enc);
14925
14926 if (name == NULL)
14927 return "DW_ATE_<unknown>";
14928
14929 return name;
14930 }
14931
14932 static void
14933 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
14934 {
14935 unsigned int i;
14936
14937 print_spaces (indent, f);
14938 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
14939 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
14940
14941 if (die->parent != NULL)
14942 {
14943 print_spaces (indent, f);
14944 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
14945 die->parent->offset.sect_off);
14946 }
14947
14948 print_spaces (indent, f);
14949 fprintf_unfiltered (f, " has children: %s\n",
14950 dwarf_bool_name (die->child != NULL));
14951
14952 print_spaces (indent, f);
14953 fprintf_unfiltered (f, " attributes:\n");
14954
14955 for (i = 0; i < die->num_attrs; ++i)
14956 {
14957 print_spaces (indent, f);
14958 fprintf_unfiltered (f, " %s (%s) ",
14959 dwarf_attr_name (die->attrs[i].name),
14960 dwarf_form_name (die->attrs[i].form));
14961
14962 switch (die->attrs[i].form)
14963 {
14964 case DW_FORM_addr:
14965 case DW_FORM_GNU_addr_index:
14966 fprintf_unfiltered (f, "address: ");
14967 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
14968 break;
14969 case DW_FORM_block2:
14970 case DW_FORM_block4:
14971 case DW_FORM_block:
14972 case DW_FORM_block1:
14973 fprintf_unfiltered (f, "block: size %d",
14974 DW_BLOCK (&die->attrs[i])->size);
14975 break;
14976 case DW_FORM_exprloc:
14977 fprintf_unfiltered (f, "expression: size %u",
14978 DW_BLOCK (&die->attrs[i])->size);
14979 break;
14980 case DW_FORM_ref_addr:
14981 fprintf_unfiltered (f, "ref address: ");
14982 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
14983 break;
14984 case DW_FORM_ref1:
14985 case DW_FORM_ref2:
14986 case DW_FORM_ref4:
14987 case DW_FORM_ref8:
14988 case DW_FORM_ref_udata:
14989 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
14990 (long) (DW_UNSND (&die->attrs[i])));
14991 break;
14992 case DW_FORM_data1:
14993 case DW_FORM_data2:
14994 case DW_FORM_data4:
14995 case DW_FORM_data8:
14996 case DW_FORM_udata:
14997 case DW_FORM_sdata:
14998 fprintf_unfiltered (f, "constant: %s",
14999 pulongest (DW_UNSND (&die->attrs[i])));
15000 break;
15001 case DW_FORM_sec_offset:
15002 fprintf_unfiltered (f, "section offset: %s",
15003 pulongest (DW_UNSND (&die->attrs[i])));
15004 break;
15005 case DW_FORM_ref_sig8:
15006 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
15007 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
15008 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
15009 else
15010 fprintf_unfiltered (f, "signatured type, offset: unknown");
15011 break;
15012 case DW_FORM_string:
15013 case DW_FORM_strp:
15014 case DW_FORM_GNU_str_index:
15015 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
15016 DW_STRING (&die->attrs[i])
15017 ? DW_STRING (&die->attrs[i]) : "",
15018 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
15019 break;
15020 case DW_FORM_flag:
15021 if (DW_UNSND (&die->attrs[i]))
15022 fprintf_unfiltered (f, "flag: TRUE");
15023 else
15024 fprintf_unfiltered (f, "flag: FALSE");
15025 break;
15026 case DW_FORM_flag_present:
15027 fprintf_unfiltered (f, "flag: TRUE");
15028 break;
15029 case DW_FORM_indirect:
15030 /* The reader will have reduced the indirect form to
15031 the "base form" so this form should not occur. */
15032 fprintf_unfiltered (f,
15033 "unexpected attribute form: DW_FORM_indirect");
15034 break;
15035 default:
15036 fprintf_unfiltered (f, "unsupported attribute form: %d.",
15037 die->attrs[i].form);
15038 break;
15039 }
15040 fprintf_unfiltered (f, "\n");
15041 }
15042 }
15043
15044 static void
15045 dump_die_for_error (struct die_info *die)
15046 {
15047 dump_die_shallow (gdb_stderr, 0, die);
15048 }
15049
15050 static void
15051 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
15052 {
15053 int indent = level * 4;
15054
15055 gdb_assert (die != NULL);
15056
15057 if (level >= max_level)
15058 return;
15059
15060 dump_die_shallow (f, indent, die);
15061
15062 if (die->child != NULL)
15063 {
15064 print_spaces (indent, f);
15065 fprintf_unfiltered (f, " Children:");
15066 if (level + 1 < max_level)
15067 {
15068 fprintf_unfiltered (f, "\n");
15069 dump_die_1 (f, level + 1, max_level, die->child);
15070 }
15071 else
15072 {
15073 fprintf_unfiltered (f,
15074 " [not printed, max nesting level reached]\n");
15075 }
15076 }
15077
15078 if (die->sibling != NULL && level > 0)
15079 {
15080 dump_die_1 (f, level, max_level, die->sibling);
15081 }
15082 }
15083
15084 /* This is called from the pdie macro in gdbinit.in.
15085 It's not static so gcc will keep a copy callable from gdb. */
15086
15087 void
15088 dump_die (struct die_info *die, int max_level)
15089 {
15090 dump_die_1 (gdb_stdlog, 0, max_level, die);
15091 }
15092
15093 static void
15094 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
15095 {
15096 void **slot;
15097
15098 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
15099 INSERT);
15100
15101 *slot = die;
15102 }
15103
15104 /* DW_ADDR is always stored already as sect_offset; despite for the forms
15105 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
15106
15107 static int
15108 is_ref_attr (struct attribute *attr)
15109 {
15110 switch (attr->form)
15111 {
15112 case DW_FORM_ref_addr:
15113 case DW_FORM_ref1:
15114 case DW_FORM_ref2:
15115 case DW_FORM_ref4:
15116 case DW_FORM_ref8:
15117 case DW_FORM_ref_udata:
15118 return 1;
15119 default:
15120 return 0;
15121 }
15122 }
15123
15124 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
15125 required kind. */
15126
15127 static sect_offset
15128 dwarf2_get_ref_die_offset (struct attribute *attr)
15129 {
15130 sect_offset retval = { DW_UNSND (attr) };
15131
15132 if (is_ref_attr (attr))
15133 return retval;
15134
15135 retval.sect_off = 0;
15136 complaint (&symfile_complaints,
15137 _("unsupported die ref attribute form: '%s'"),
15138 dwarf_form_name (attr->form));
15139 return retval;
15140 }
15141
15142 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
15143 * the value held by the attribute is not constant. */
15144
15145 static LONGEST
15146 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
15147 {
15148 if (attr->form == DW_FORM_sdata)
15149 return DW_SND (attr);
15150 else if (attr->form == DW_FORM_udata
15151 || attr->form == DW_FORM_data1
15152 || attr->form == DW_FORM_data2
15153 || attr->form == DW_FORM_data4
15154 || attr->form == DW_FORM_data8)
15155 return DW_UNSND (attr);
15156 else
15157 {
15158 complaint (&symfile_complaints,
15159 _("Attribute value is not a constant (%s)"),
15160 dwarf_form_name (attr->form));
15161 return default_value;
15162 }
15163 }
15164
15165 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
15166 unit and add it to our queue.
15167 The result is non-zero if PER_CU was queued, otherwise the result is zero
15168 meaning either PER_CU is already queued or it is already loaded. */
15169
15170 static int
15171 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
15172 struct dwarf2_per_cu_data *per_cu,
15173 enum language pretend_language)
15174 {
15175 /* We may arrive here during partial symbol reading, if we need full
15176 DIEs to process an unusual case (e.g. template arguments). Do
15177 not queue PER_CU, just tell our caller to load its DIEs. */
15178 if (dwarf2_per_objfile->reading_partial_symbols)
15179 {
15180 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
15181 return 1;
15182 return 0;
15183 }
15184
15185 /* Mark the dependence relation so that we don't flush PER_CU
15186 too early. */
15187 dwarf2_add_dependence (this_cu, per_cu);
15188
15189 /* If it's already on the queue, we have nothing to do. */
15190 if (per_cu->queued)
15191 return 0;
15192
15193 /* If the compilation unit is already loaded, just mark it as
15194 used. */
15195 if (per_cu->cu != NULL)
15196 {
15197 per_cu->cu->last_used = 0;
15198 return 0;
15199 }
15200
15201 /* Add it to the queue. */
15202 queue_comp_unit (per_cu, pretend_language);
15203
15204 return 1;
15205 }
15206
15207 /* Follow reference or signature attribute ATTR of SRC_DIE.
15208 On entry *REF_CU is the CU of SRC_DIE.
15209 On exit *REF_CU is the CU of the result. */
15210
15211 static struct die_info *
15212 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
15213 struct dwarf2_cu **ref_cu)
15214 {
15215 struct die_info *die;
15216
15217 if (is_ref_attr (attr))
15218 die = follow_die_ref (src_die, attr, ref_cu);
15219 else if (attr->form == DW_FORM_ref_sig8)
15220 die = follow_die_sig (src_die, attr, ref_cu);
15221 else
15222 {
15223 dump_die_for_error (src_die);
15224 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
15225 (*ref_cu)->objfile->name);
15226 }
15227
15228 return die;
15229 }
15230
15231 /* Follow reference OFFSET.
15232 On entry *REF_CU is the CU of the source die referencing OFFSET.
15233 On exit *REF_CU is the CU of the result.
15234 Returns NULL if OFFSET is invalid. */
15235
15236 static struct die_info *
15237 follow_die_offset (sect_offset offset, struct dwarf2_cu **ref_cu)
15238 {
15239 struct die_info temp_die;
15240 struct dwarf2_cu *target_cu, *cu = *ref_cu;
15241
15242 gdb_assert (cu->per_cu != NULL);
15243
15244 target_cu = cu;
15245
15246 if (cu->per_cu->is_debug_types)
15247 {
15248 /* .debug_types CUs cannot reference anything outside their CU.
15249 If they need to, they have to reference a signatured type via
15250 DW_FORM_ref_sig8. */
15251 if (! offset_in_cu_p (&cu->header, offset))
15252 return NULL;
15253 }
15254 else if (! offset_in_cu_p (&cu->header, offset))
15255 {
15256 struct dwarf2_per_cu_data *per_cu;
15257
15258 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
15259
15260 /* If necessary, add it to the queue and load its DIEs. */
15261 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
15262 load_full_comp_unit (per_cu, cu->language);
15263
15264 target_cu = per_cu->cu;
15265 }
15266 else if (cu->dies == NULL)
15267 {
15268 /* We're loading full DIEs during partial symbol reading. */
15269 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
15270 load_full_comp_unit (cu->per_cu, language_minimal);
15271 }
15272
15273 *ref_cu = target_cu;
15274 temp_die.offset = offset;
15275 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
15276 }
15277
15278 /* Follow reference attribute ATTR of SRC_DIE.
15279 On entry *REF_CU is the CU of SRC_DIE.
15280 On exit *REF_CU is the CU of the result. */
15281
15282 static struct die_info *
15283 follow_die_ref (struct die_info *src_die, struct attribute *attr,
15284 struct dwarf2_cu **ref_cu)
15285 {
15286 sect_offset offset = dwarf2_get_ref_die_offset (attr);
15287 struct dwarf2_cu *cu = *ref_cu;
15288 struct die_info *die;
15289
15290 die = follow_die_offset (offset, ref_cu);
15291 if (!die)
15292 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
15293 "at 0x%x [in module %s]"),
15294 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
15295
15296 return die;
15297 }
15298
15299 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
15300 Returned value is intended for DW_OP_call*. Returned
15301 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
15302
15303 struct dwarf2_locexpr_baton
15304 dwarf2_fetch_die_location_block (cu_offset offset_in_cu,
15305 struct dwarf2_per_cu_data *per_cu,
15306 CORE_ADDR (*get_frame_pc) (void *baton),
15307 void *baton)
15308 {
15309 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
15310 struct dwarf2_cu *cu;
15311 struct die_info *die;
15312 struct attribute *attr;
15313 struct dwarf2_locexpr_baton retval;
15314
15315 dw2_setup (per_cu->objfile);
15316
15317 if (per_cu->cu == NULL)
15318 load_cu (per_cu);
15319 cu = per_cu->cu;
15320
15321 die = follow_die_offset (offset, &cu);
15322 if (!die)
15323 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
15324 offset.sect_off, per_cu->objfile->name);
15325
15326 attr = dwarf2_attr (die, DW_AT_location, cu);
15327 if (!attr)
15328 {
15329 /* DWARF: "If there is no such attribute, then there is no effect.".
15330 DATA is ignored if SIZE is 0. */
15331
15332 retval.data = NULL;
15333 retval.size = 0;
15334 }
15335 else if (attr_form_is_section_offset (attr))
15336 {
15337 struct dwarf2_loclist_baton loclist_baton;
15338 CORE_ADDR pc = (*get_frame_pc) (baton);
15339 size_t size;
15340
15341 fill_in_loclist_baton (cu, &loclist_baton, attr);
15342
15343 retval.data = dwarf2_find_location_expression (&loclist_baton,
15344 &size, pc);
15345 retval.size = size;
15346 }
15347 else
15348 {
15349 if (!attr_form_is_block (attr))
15350 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
15351 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
15352 offset.sect_off, per_cu->objfile->name);
15353
15354 retval.data = DW_BLOCK (attr)->data;
15355 retval.size = DW_BLOCK (attr)->size;
15356 }
15357 retval.per_cu = cu->per_cu;
15358
15359 age_cached_comp_units ();
15360
15361 return retval;
15362 }
15363
15364 /* Return the type of the DIE at DIE_OFFSET in the CU named by
15365 PER_CU. */
15366
15367 struct type *
15368 dwarf2_get_die_type (cu_offset die_offset,
15369 struct dwarf2_per_cu_data *per_cu)
15370 {
15371 sect_offset die_offset_sect;
15372
15373 dw2_setup (per_cu->objfile);
15374
15375 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
15376 return get_die_type_at_offset (die_offset_sect, per_cu);
15377 }
15378
15379 /* Follow the signature attribute ATTR in SRC_DIE.
15380 On entry *REF_CU is the CU of SRC_DIE.
15381 On exit *REF_CU is the CU of the result. */
15382
15383 static struct die_info *
15384 follow_die_sig (struct die_info *src_die, struct attribute *attr,
15385 struct dwarf2_cu **ref_cu)
15386 {
15387 struct objfile *objfile = (*ref_cu)->objfile;
15388 struct die_info temp_die;
15389 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
15390 struct dwarf2_cu *sig_cu;
15391 struct die_info *die;
15392
15393 /* sig_type will be NULL if the signatured type is missing from
15394 the debug info. */
15395 if (sig_type == NULL)
15396 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
15397 "at 0x%x [in module %s]"),
15398 src_die->offset.sect_off, objfile->name);
15399
15400 /* If necessary, add it to the queue and load its DIEs. */
15401
15402 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
15403 read_signatured_type (sig_type);
15404
15405 gdb_assert (sig_type->per_cu.cu != NULL);
15406
15407 sig_cu = sig_type->per_cu.cu;
15408 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
15409 temp_die.offset = sig_type->type_offset_in_section;
15410 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
15411 temp_die.offset.sect_off);
15412 if (die)
15413 {
15414 *ref_cu = sig_cu;
15415 return die;
15416 }
15417
15418 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
15419 "from DIE at 0x%x [in module %s]"),
15420 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
15421 }
15422
15423 /* Given an offset of a signatured type, return its signatured_type. */
15424
15425 static struct signatured_type *
15426 lookup_signatured_type_at_offset (struct objfile *objfile,
15427 struct dwarf2_section_info *section,
15428 sect_offset offset)
15429 {
15430 gdb_byte *info_ptr = section->buffer + offset.sect_off;
15431 unsigned int length, initial_length_size;
15432 unsigned int sig_offset;
15433 struct signatured_type find_entry, *sig_type;
15434
15435 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
15436 sig_offset = (initial_length_size
15437 + 2 /*version*/
15438 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
15439 + 1 /*address_size*/);
15440 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
15441 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
15442
15443 /* This is only used to lookup previously recorded types.
15444 If we didn't find it, it's our bug. */
15445 gdb_assert (sig_type != NULL);
15446 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
15447
15448 return sig_type;
15449 }
15450
15451 /* Load the DIEs associated with type unit PER_CU into memory. */
15452
15453 static void
15454 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
15455 {
15456 struct objfile *objfile = per_cu->objfile;
15457 struct dwarf2_section_info *sect = per_cu->info_or_types_section;
15458 sect_offset offset = per_cu->offset;
15459 struct signatured_type *sig_type;
15460
15461 dwarf2_read_section (objfile, sect);
15462
15463 /* We have the section offset, but we need the signature to do the
15464 hash table lookup. */
15465 /* FIXME: This is sorta unnecessary, read_signatured_type only uses
15466 the signature to assert we found the right one.
15467 Ok, but it's a lot of work. We should simplify things so any needed
15468 assert doesn't require all this clumsiness. */
15469 sig_type = lookup_signatured_type_at_offset (objfile, sect, offset);
15470
15471 gdb_assert (&sig_type->per_cu == per_cu);
15472 gdb_assert (sig_type->per_cu.cu == NULL);
15473
15474 read_signatured_type (sig_type);
15475
15476 gdb_assert (sig_type->per_cu.cu != NULL);
15477 }
15478
15479 /* die_reader_func for read_signatured_type.
15480 This is identical to load_full_comp_unit_reader,
15481 but is kept separate for now. */
15482
15483 static void
15484 read_signatured_type_reader (const struct die_reader_specs *reader,
15485 gdb_byte *info_ptr,
15486 struct die_info *comp_unit_die,
15487 int has_children,
15488 void *data)
15489 {
15490 struct dwarf2_cu *cu = reader->cu;
15491
15492 gdb_assert (cu->die_hash == NULL);
15493 cu->die_hash =
15494 htab_create_alloc_ex (cu->header.length / 12,
15495 die_hash,
15496 die_eq,
15497 NULL,
15498 &cu->comp_unit_obstack,
15499 hashtab_obstack_allocate,
15500 dummy_obstack_deallocate);
15501
15502 if (has_children)
15503 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
15504 &info_ptr, comp_unit_die);
15505 cu->dies = comp_unit_die;
15506 /* comp_unit_die is not stored in die_hash, no need. */
15507
15508 /* We try not to read any attributes in this function, because not
15509 all CUs needed for references have been loaded yet, and symbol
15510 table processing isn't initialized. But we have to set the CU language,
15511 or we won't be able to build types correctly.
15512 Similarly, if we do not read the producer, we can not apply
15513 producer-specific interpretation. */
15514 prepare_one_comp_unit (cu, cu->dies, language_minimal);
15515 }
15516
15517 /* Read in a signatured type and build its CU and DIEs.
15518 If the type is a stub for the real type in a DWO file,
15519 read in the real type from the DWO file as well. */
15520
15521 static void
15522 read_signatured_type (struct signatured_type *sig_type)
15523 {
15524 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
15525
15526 gdb_assert (per_cu->is_debug_types);
15527 gdb_assert (per_cu->cu == NULL);
15528
15529 init_cutu_and_read_dies (per_cu, 0, 1, read_signatured_type_reader, NULL);
15530 }
15531
15532 /* Decode simple location descriptions.
15533 Given a pointer to a dwarf block that defines a location, compute
15534 the location and return the value.
15535
15536 NOTE drow/2003-11-18: This function is called in two situations
15537 now: for the address of static or global variables (partial symbols
15538 only) and for offsets into structures which are expected to be
15539 (more or less) constant. The partial symbol case should go away,
15540 and only the constant case should remain. That will let this
15541 function complain more accurately. A few special modes are allowed
15542 without complaint for global variables (for instance, global
15543 register values and thread-local values).
15544
15545 A location description containing no operations indicates that the
15546 object is optimized out. The return value is 0 for that case.
15547 FIXME drow/2003-11-16: No callers check for this case any more; soon all
15548 callers will only want a very basic result and this can become a
15549 complaint.
15550
15551 Note that stack[0] is unused except as a default error return. */
15552
15553 static CORE_ADDR
15554 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
15555 {
15556 struct objfile *objfile = cu->objfile;
15557 int i;
15558 int size = blk->size;
15559 gdb_byte *data = blk->data;
15560 CORE_ADDR stack[64];
15561 int stacki;
15562 unsigned int bytes_read, unsnd;
15563 gdb_byte op;
15564
15565 i = 0;
15566 stacki = 0;
15567 stack[stacki] = 0;
15568 stack[++stacki] = 0;
15569
15570 while (i < size)
15571 {
15572 op = data[i++];
15573 switch (op)
15574 {
15575 case DW_OP_lit0:
15576 case DW_OP_lit1:
15577 case DW_OP_lit2:
15578 case DW_OP_lit3:
15579 case DW_OP_lit4:
15580 case DW_OP_lit5:
15581 case DW_OP_lit6:
15582 case DW_OP_lit7:
15583 case DW_OP_lit8:
15584 case DW_OP_lit9:
15585 case DW_OP_lit10:
15586 case DW_OP_lit11:
15587 case DW_OP_lit12:
15588 case DW_OP_lit13:
15589 case DW_OP_lit14:
15590 case DW_OP_lit15:
15591 case DW_OP_lit16:
15592 case DW_OP_lit17:
15593 case DW_OP_lit18:
15594 case DW_OP_lit19:
15595 case DW_OP_lit20:
15596 case DW_OP_lit21:
15597 case DW_OP_lit22:
15598 case DW_OP_lit23:
15599 case DW_OP_lit24:
15600 case DW_OP_lit25:
15601 case DW_OP_lit26:
15602 case DW_OP_lit27:
15603 case DW_OP_lit28:
15604 case DW_OP_lit29:
15605 case DW_OP_lit30:
15606 case DW_OP_lit31:
15607 stack[++stacki] = op - DW_OP_lit0;
15608 break;
15609
15610 case DW_OP_reg0:
15611 case DW_OP_reg1:
15612 case DW_OP_reg2:
15613 case DW_OP_reg3:
15614 case DW_OP_reg4:
15615 case DW_OP_reg5:
15616 case DW_OP_reg6:
15617 case DW_OP_reg7:
15618 case DW_OP_reg8:
15619 case DW_OP_reg9:
15620 case DW_OP_reg10:
15621 case DW_OP_reg11:
15622 case DW_OP_reg12:
15623 case DW_OP_reg13:
15624 case DW_OP_reg14:
15625 case DW_OP_reg15:
15626 case DW_OP_reg16:
15627 case DW_OP_reg17:
15628 case DW_OP_reg18:
15629 case DW_OP_reg19:
15630 case DW_OP_reg20:
15631 case DW_OP_reg21:
15632 case DW_OP_reg22:
15633 case DW_OP_reg23:
15634 case DW_OP_reg24:
15635 case DW_OP_reg25:
15636 case DW_OP_reg26:
15637 case DW_OP_reg27:
15638 case DW_OP_reg28:
15639 case DW_OP_reg29:
15640 case DW_OP_reg30:
15641 case DW_OP_reg31:
15642 stack[++stacki] = op - DW_OP_reg0;
15643 if (i < size)
15644 dwarf2_complex_location_expr_complaint ();
15645 break;
15646
15647 case DW_OP_regx:
15648 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
15649 i += bytes_read;
15650 stack[++stacki] = unsnd;
15651 if (i < size)
15652 dwarf2_complex_location_expr_complaint ();
15653 break;
15654
15655 case DW_OP_addr:
15656 stack[++stacki] = read_address (objfile->obfd, &data[i],
15657 cu, &bytes_read);
15658 i += bytes_read;
15659 break;
15660
15661 case DW_OP_const1u:
15662 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
15663 i += 1;
15664 break;
15665
15666 case DW_OP_const1s:
15667 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
15668 i += 1;
15669 break;
15670
15671 case DW_OP_const2u:
15672 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
15673 i += 2;
15674 break;
15675
15676 case DW_OP_const2s:
15677 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
15678 i += 2;
15679 break;
15680
15681 case DW_OP_const4u:
15682 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
15683 i += 4;
15684 break;
15685
15686 case DW_OP_const4s:
15687 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
15688 i += 4;
15689 break;
15690
15691 case DW_OP_const8u:
15692 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
15693 i += 8;
15694 break;
15695
15696 case DW_OP_constu:
15697 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
15698 &bytes_read);
15699 i += bytes_read;
15700 break;
15701
15702 case DW_OP_consts:
15703 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
15704 i += bytes_read;
15705 break;
15706
15707 case DW_OP_dup:
15708 stack[stacki + 1] = stack[stacki];
15709 stacki++;
15710 break;
15711
15712 case DW_OP_plus:
15713 stack[stacki - 1] += stack[stacki];
15714 stacki--;
15715 break;
15716
15717 case DW_OP_plus_uconst:
15718 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
15719 &bytes_read);
15720 i += bytes_read;
15721 break;
15722
15723 case DW_OP_minus:
15724 stack[stacki - 1] -= stack[stacki];
15725 stacki--;
15726 break;
15727
15728 case DW_OP_deref:
15729 /* If we're not the last op, then we definitely can't encode
15730 this using GDB's address_class enum. This is valid for partial
15731 global symbols, although the variable's address will be bogus
15732 in the psymtab. */
15733 if (i < size)
15734 dwarf2_complex_location_expr_complaint ();
15735 break;
15736
15737 case DW_OP_GNU_push_tls_address:
15738 /* The top of the stack has the offset from the beginning
15739 of the thread control block at which the variable is located. */
15740 /* Nothing should follow this operator, so the top of stack would
15741 be returned. */
15742 /* This is valid for partial global symbols, but the variable's
15743 address will be bogus in the psymtab. Make it always at least
15744 non-zero to not look as a variable garbage collected by linker
15745 which have DW_OP_addr 0. */
15746 if (i < size)
15747 dwarf2_complex_location_expr_complaint ();
15748 stack[stacki]++;
15749 break;
15750
15751 case DW_OP_GNU_uninit:
15752 break;
15753
15754 case DW_OP_GNU_addr_index:
15755 case DW_OP_GNU_const_index:
15756 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
15757 &bytes_read);
15758 i += bytes_read;
15759 break;
15760
15761 default:
15762 {
15763 const char *name = get_DW_OP_name (op);
15764
15765 if (name)
15766 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
15767 name);
15768 else
15769 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
15770 op);
15771 }
15772
15773 return (stack[stacki]);
15774 }
15775
15776 /* Enforce maximum stack depth of SIZE-1 to avoid writing
15777 outside of the allocated space. Also enforce minimum>0. */
15778 if (stacki >= ARRAY_SIZE (stack) - 1)
15779 {
15780 complaint (&symfile_complaints,
15781 _("location description stack overflow"));
15782 return 0;
15783 }
15784
15785 if (stacki <= 0)
15786 {
15787 complaint (&symfile_complaints,
15788 _("location description stack underflow"));
15789 return 0;
15790 }
15791 }
15792 return (stack[stacki]);
15793 }
15794
15795 /* memory allocation interface */
15796
15797 static struct dwarf_block *
15798 dwarf_alloc_block (struct dwarf2_cu *cu)
15799 {
15800 struct dwarf_block *blk;
15801
15802 blk = (struct dwarf_block *)
15803 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
15804 return (blk);
15805 }
15806
15807 static struct die_info *
15808 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
15809 {
15810 struct die_info *die;
15811 size_t size = sizeof (struct die_info);
15812
15813 if (num_attrs > 1)
15814 size += (num_attrs - 1) * sizeof (struct attribute);
15815
15816 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
15817 memset (die, 0, sizeof (struct die_info));
15818 return (die);
15819 }
15820
15821 \f
15822 /* Macro support. */
15823
15824 /* Return the full name of file number I in *LH's file name table.
15825 Use COMP_DIR as the name of the current directory of the
15826 compilation. The result is allocated using xmalloc; the caller is
15827 responsible for freeing it. */
15828 static char *
15829 file_full_name (int file, struct line_header *lh, const char *comp_dir)
15830 {
15831 /* Is the file number a valid index into the line header's file name
15832 table? Remember that file numbers start with one, not zero. */
15833 if (1 <= file && file <= lh->num_file_names)
15834 {
15835 struct file_entry *fe = &lh->file_names[file - 1];
15836
15837 if (IS_ABSOLUTE_PATH (fe->name))
15838 return xstrdup (fe->name);
15839 else
15840 {
15841 const char *dir;
15842 int dir_len;
15843 char *full_name;
15844
15845 if (fe->dir_index)
15846 dir = lh->include_dirs[fe->dir_index - 1];
15847 else
15848 dir = comp_dir;
15849
15850 if (dir)
15851 {
15852 dir_len = strlen (dir);
15853 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
15854 strcpy (full_name, dir);
15855 full_name[dir_len] = '/';
15856 strcpy (full_name + dir_len + 1, fe->name);
15857 return full_name;
15858 }
15859 else
15860 return xstrdup (fe->name);
15861 }
15862 }
15863 else
15864 {
15865 /* The compiler produced a bogus file number. We can at least
15866 record the macro definitions made in the file, even if we
15867 won't be able to find the file by name. */
15868 char fake_name[80];
15869
15870 sprintf (fake_name, "<bad macro file number %d>", file);
15871
15872 complaint (&symfile_complaints,
15873 _("bad file number in macro information (%d)"),
15874 file);
15875
15876 return xstrdup (fake_name);
15877 }
15878 }
15879
15880
15881 static struct macro_source_file *
15882 macro_start_file (int file, int line,
15883 struct macro_source_file *current_file,
15884 const char *comp_dir,
15885 struct line_header *lh, struct objfile *objfile)
15886 {
15887 /* The full name of this source file. */
15888 char *full_name = file_full_name (file, lh, comp_dir);
15889
15890 /* We don't create a macro table for this compilation unit
15891 at all until we actually get a filename. */
15892 if (! pending_macros)
15893 pending_macros = new_macro_table (&objfile->objfile_obstack,
15894 objfile->macro_cache);
15895
15896 if (! current_file)
15897 {
15898 /* If we have no current file, then this must be the start_file
15899 directive for the compilation unit's main source file. */
15900 current_file = macro_set_main (pending_macros, full_name);
15901 macro_define_special (pending_macros);
15902 }
15903 else
15904 current_file = macro_include (current_file, line, full_name);
15905
15906 xfree (full_name);
15907
15908 return current_file;
15909 }
15910
15911
15912 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
15913 followed by a null byte. */
15914 static char *
15915 copy_string (const char *buf, int len)
15916 {
15917 char *s = xmalloc (len + 1);
15918
15919 memcpy (s, buf, len);
15920 s[len] = '\0';
15921 return s;
15922 }
15923
15924
15925 static const char *
15926 consume_improper_spaces (const char *p, const char *body)
15927 {
15928 if (*p == ' ')
15929 {
15930 complaint (&symfile_complaints,
15931 _("macro definition contains spaces "
15932 "in formal argument list:\n`%s'"),
15933 body);
15934
15935 while (*p == ' ')
15936 p++;
15937 }
15938
15939 return p;
15940 }
15941
15942
15943 static void
15944 parse_macro_definition (struct macro_source_file *file, int line,
15945 const char *body)
15946 {
15947 const char *p;
15948
15949 /* The body string takes one of two forms. For object-like macro
15950 definitions, it should be:
15951
15952 <macro name> " " <definition>
15953
15954 For function-like macro definitions, it should be:
15955
15956 <macro name> "() " <definition>
15957 or
15958 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
15959
15960 Spaces may appear only where explicitly indicated, and in the
15961 <definition>.
15962
15963 The Dwarf 2 spec says that an object-like macro's name is always
15964 followed by a space, but versions of GCC around March 2002 omit
15965 the space when the macro's definition is the empty string.
15966
15967 The Dwarf 2 spec says that there should be no spaces between the
15968 formal arguments in a function-like macro's formal argument list,
15969 but versions of GCC around March 2002 include spaces after the
15970 commas. */
15971
15972
15973 /* Find the extent of the macro name. The macro name is terminated
15974 by either a space or null character (for an object-like macro) or
15975 an opening paren (for a function-like macro). */
15976 for (p = body; *p; p++)
15977 if (*p == ' ' || *p == '(')
15978 break;
15979
15980 if (*p == ' ' || *p == '\0')
15981 {
15982 /* It's an object-like macro. */
15983 int name_len = p - body;
15984 char *name = copy_string (body, name_len);
15985 const char *replacement;
15986
15987 if (*p == ' ')
15988 replacement = body + name_len + 1;
15989 else
15990 {
15991 dwarf2_macro_malformed_definition_complaint (body);
15992 replacement = body + name_len;
15993 }
15994
15995 macro_define_object (file, line, name, replacement);
15996
15997 xfree (name);
15998 }
15999 else if (*p == '(')
16000 {
16001 /* It's a function-like macro. */
16002 char *name = copy_string (body, p - body);
16003 int argc = 0;
16004 int argv_size = 1;
16005 char **argv = xmalloc (argv_size * sizeof (*argv));
16006
16007 p++;
16008
16009 p = consume_improper_spaces (p, body);
16010
16011 /* Parse the formal argument list. */
16012 while (*p && *p != ')')
16013 {
16014 /* Find the extent of the current argument name. */
16015 const char *arg_start = p;
16016
16017 while (*p && *p != ',' && *p != ')' && *p != ' ')
16018 p++;
16019
16020 if (! *p || p == arg_start)
16021 dwarf2_macro_malformed_definition_complaint (body);
16022 else
16023 {
16024 /* Make sure argv has room for the new argument. */
16025 if (argc >= argv_size)
16026 {
16027 argv_size *= 2;
16028 argv = xrealloc (argv, argv_size * sizeof (*argv));
16029 }
16030
16031 argv[argc++] = copy_string (arg_start, p - arg_start);
16032 }
16033
16034 p = consume_improper_spaces (p, body);
16035
16036 /* Consume the comma, if present. */
16037 if (*p == ',')
16038 {
16039 p++;
16040
16041 p = consume_improper_spaces (p, body);
16042 }
16043 }
16044
16045 if (*p == ')')
16046 {
16047 p++;
16048
16049 if (*p == ' ')
16050 /* Perfectly formed definition, no complaints. */
16051 macro_define_function (file, line, name,
16052 argc, (const char **) argv,
16053 p + 1);
16054 else if (*p == '\0')
16055 {
16056 /* Complain, but do define it. */
16057 dwarf2_macro_malformed_definition_complaint (body);
16058 macro_define_function (file, line, name,
16059 argc, (const char **) argv,
16060 p);
16061 }
16062 else
16063 /* Just complain. */
16064 dwarf2_macro_malformed_definition_complaint (body);
16065 }
16066 else
16067 /* Just complain. */
16068 dwarf2_macro_malformed_definition_complaint (body);
16069
16070 xfree (name);
16071 {
16072 int i;
16073
16074 for (i = 0; i < argc; i++)
16075 xfree (argv[i]);
16076 }
16077 xfree (argv);
16078 }
16079 else
16080 dwarf2_macro_malformed_definition_complaint (body);
16081 }
16082
16083 /* Skip some bytes from BYTES according to the form given in FORM.
16084 Returns the new pointer. */
16085
16086 static gdb_byte *
16087 skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
16088 enum dwarf_form form,
16089 unsigned int offset_size,
16090 struct dwarf2_section_info *section)
16091 {
16092 unsigned int bytes_read;
16093
16094 switch (form)
16095 {
16096 case DW_FORM_data1:
16097 case DW_FORM_flag:
16098 ++bytes;
16099 break;
16100
16101 case DW_FORM_data2:
16102 bytes += 2;
16103 break;
16104
16105 case DW_FORM_data4:
16106 bytes += 4;
16107 break;
16108
16109 case DW_FORM_data8:
16110 bytes += 8;
16111 break;
16112
16113 case DW_FORM_string:
16114 read_direct_string (abfd, bytes, &bytes_read);
16115 bytes += bytes_read;
16116 break;
16117
16118 case DW_FORM_sec_offset:
16119 case DW_FORM_strp:
16120 bytes += offset_size;
16121 break;
16122
16123 case DW_FORM_block:
16124 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
16125 bytes += bytes_read;
16126 break;
16127
16128 case DW_FORM_block1:
16129 bytes += 1 + read_1_byte (abfd, bytes);
16130 break;
16131 case DW_FORM_block2:
16132 bytes += 2 + read_2_bytes (abfd, bytes);
16133 break;
16134 case DW_FORM_block4:
16135 bytes += 4 + read_4_bytes (abfd, bytes);
16136 break;
16137
16138 case DW_FORM_sdata:
16139 case DW_FORM_udata:
16140 case DW_FORM_GNU_addr_index:
16141 case DW_FORM_GNU_str_index:
16142 bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
16143 if (bytes == NULL)
16144 {
16145 dwarf2_section_buffer_overflow_complaint (section);
16146 return NULL;
16147 }
16148 break;
16149
16150 default:
16151 {
16152 complain:
16153 complaint (&symfile_complaints,
16154 _("invalid form 0x%x in `%s'"),
16155 form,
16156 section->asection->name);
16157 return NULL;
16158 }
16159 }
16160
16161 return bytes;
16162 }
16163
16164 /* A helper for dwarf_decode_macros that handles skipping an unknown
16165 opcode. Returns an updated pointer to the macro data buffer; or,
16166 on error, issues a complaint and returns NULL. */
16167
16168 static gdb_byte *
16169 skip_unknown_opcode (unsigned int opcode,
16170 gdb_byte **opcode_definitions,
16171 gdb_byte *mac_ptr, gdb_byte *mac_end,
16172 bfd *abfd,
16173 unsigned int offset_size,
16174 struct dwarf2_section_info *section)
16175 {
16176 unsigned int bytes_read, i;
16177 unsigned long arg;
16178 gdb_byte *defn;
16179
16180 if (opcode_definitions[opcode] == NULL)
16181 {
16182 complaint (&symfile_complaints,
16183 _("unrecognized DW_MACFINO opcode 0x%x"),
16184 opcode);
16185 return NULL;
16186 }
16187
16188 defn = opcode_definitions[opcode];
16189 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
16190 defn += bytes_read;
16191
16192 for (i = 0; i < arg; ++i)
16193 {
16194 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
16195 section);
16196 if (mac_ptr == NULL)
16197 {
16198 /* skip_form_bytes already issued the complaint. */
16199 return NULL;
16200 }
16201 }
16202
16203 return mac_ptr;
16204 }
16205
16206 /* A helper function which parses the header of a macro section.
16207 If the macro section is the extended (for now called "GNU") type,
16208 then this updates *OFFSET_SIZE. Returns a pointer to just after
16209 the header, or issues a complaint and returns NULL on error. */
16210
16211 static gdb_byte *
16212 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
16213 bfd *abfd,
16214 gdb_byte *mac_ptr,
16215 unsigned int *offset_size,
16216 int section_is_gnu)
16217 {
16218 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
16219
16220 if (section_is_gnu)
16221 {
16222 unsigned int version, flags;
16223
16224 version = read_2_bytes (abfd, mac_ptr);
16225 if (version != 4)
16226 {
16227 complaint (&symfile_complaints,
16228 _("unrecognized version `%d' in .debug_macro section"),
16229 version);
16230 return NULL;
16231 }
16232 mac_ptr += 2;
16233
16234 flags = read_1_byte (abfd, mac_ptr);
16235 ++mac_ptr;
16236 *offset_size = (flags & 1) ? 8 : 4;
16237
16238 if ((flags & 2) != 0)
16239 /* We don't need the line table offset. */
16240 mac_ptr += *offset_size;
16241
16242 /* Vendor opcode descriptions. */
16243 if ((flags & 4) != 0)
16244 {
16245 unsigned int i, count;
16246
16247 count = read_1_byte (abfd, mac_ptr);
16248 ++mac_ptr;
16249 for (i = 0; i < count; ++i)
16250 {
16251 unsigned int opcode, bytes_read;
16252 unsigned long arg;
16253
16254 opcode = read_1_byte (abfd, mac_ptr);
16255 ++mac_ptr;
16256 opcode_definitions[opcode] = mac_ptr;
16257 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16258 mac_ptr += bytes_read;
16259 mac_ptr += arg;
16260 }
16261 }
16262 }
16263
16264 return mac_ptr;
16265 }
16266
16267 /* A helper for dwarf_decode_macros that handles the GNU extensions,
16268 including DW_MACRO_GNU_transparent_include. */
16269
16270 static void
16271 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
16272 struct macro_source_file *current_file,
16273 struct line_header *lh, char *comp_dir,
16274 struct dwarf2_section_info *section,
16275 int section_is_gnu,
16276 unsigned int offset_size,
16277 struct objfile *objfile,
16278 htab_t include_hash)
16279 {
16280 enum dwarf_macro_record_type macinfo_type;
16281 int at_commandline;
16282 gdb_byte *opcode_definitions[256];
16283
16284 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
16285 &offset_size, section_is_gnu);
16286 if (mac_ptr == NULL)
16287 {
16288 /* We already issued a complaint. */
16289 return;
16290 }
16291
16292 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
16293 GDB is still reading the definitions from command line. First
16294 DW_MACINFO_start_file will need to be ignored as it was already executed
16295 to create CURRENT_FILE for the main source holding also the command line
16296 definitions. On first met DW_MACINFO_start_file this flag is reset to
16297 normally execute all the remaining DW_MACINFO_start_file macinfos. */
16298
16299 at_commandline = 1;
16300
16301 do
16302 {
16303 /* Do we at least have room for a macinfo type byte? */
16304 if (mac_ptr >= mac_end)
16305 {
16306 dwarf2_section_buffer_overflow_complaint (section);
16307 break;
16308 }
16309
16310 macinfo_type = read_1_byte (abfd, mac_ptr);
16311 mac_ptr++;
16312
16313 /* Note that we rely on the fact that the corresponding GNU and
16314 DWARF constants are the same. */
16315 switch (macinfo_type)
16316 {
16317 /* A zero macinfo type indicates the end of the macro
16318 information. */
16319 case 0:
16320 break;
16321
16322 case DW_MACRO_GNU_define:
16323 case DW_MACRO_GNU_undef:
16324 case DW_MACRO_GNU_define_indirect:
16325 case DW_MACRO_GNU_undef_indirect:
16326 {
16327 unsigned int bytes_read;
16328 int line;
16329 char *body;
16330 int is_define;
16331
16332 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16333 mac_ptr += bytes_read;
16334
16335 if (macinfo_type == DW_MACRO_GNU_define
16336 || macinfo_type == DW_MACRO_GNU_undef)
16337 {
16338 body = read_direct_string (abfd, mac_ptr, &bytes_read);
16339 mac_ptr += bytes_read;
16340 }
16341 else
16342 {
16343 LONGEST str_offset;
16344
16345 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
16346 mac_ptr += offset_size;
16347
16348 body = read_indirect_string_at_offset (abfd, str_offset);
16349 }
16350
16351 is_define = (macinfo_type == DW_MACRO_GNU_define
16352 || macinfo_type == DW_MACRO_GNU_define_indirect);
16353 if (! current_file)
16354 {
16355 /* DWARF violation as no main source is present. */
16356 complaint (&symfile_complaints,
16357 _("debug info with no main source gives macro %s "
16358 "on line %d: %s"),
16359 is_define ? _("definition") : _("undefinition"),
16360 line, body);
16361 break;
16362 }
16363 if ((line == 0 && !at_commandline)
16364 || (line != 0 && at_commandline))
16365 complaint (&symfile_complaints,
16366 _("debug info gives %s macro %s with %s line %d: %s"),
16367 at_commandline ? _("command-line") : _("in-file"),
16368 is_define ? _("definition") : _("undefinition"),
16369 line == 0 ? _("zero") : _("non-zero"), line, body);
16370
16371 if (is_define)
16372 parse_macro_definition (current_file, line, body);
16373 else
16374 {
16375 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
16376 || macinfo_type == DW_MACRO_GNU_undef_indirect);
16377 macro_undef (current_file, line, body);
16378 }
16379 }
16380 break;
16381
16382 case DW_MACRO_GNU_start_file:
16383 {
16384 unsigned int bytes_read;
16385 int line, file;
16386
16387 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16388 mac_ptr += bytes_read;
16389 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16390 mac_ptr += bytes_read;
16391
16392 if ((line == 0 && !at_commandline)
16393 || (line != 0 && at_commandline))
16394 complaint (&symfile_complaints,
16395 _("debug info gives source %d included "
16396 "from %s at %s line %d"),
16397 file, at_commandline ? _("command-line") : _("file"),
16398 line == 0 ? _("zero") : _("non-zero"), line);
16399
16400 if (at_commandline)
16401 {
16402 /* This DW_MACRO_GNU_start_file was executed in the
16403 pass one. */
16404 at_commandline = 0;
16405 }
16406 else
16407 current_file = macro_start_file (file, line,
16408 current_file, comp_dir,
16409 lh, objfile);
16410 }
16411 break;
16412
16413 case DW_MACRO_GNU_end_file:
16414 if (! current_file)
16415 complaint (&symfile_complaints,
16416 _("macro debug info has an unmatched "
16417 "`close_file' directive"));
16418 else
16419 {
16420 current_file = current_file->included_by;
16421 if (! current_file)
16422 {
16423 enum dwarf_macro_record_type next_type;
16424
16425 /* GCC circa March 2002 doesn't produce the zero
16426 type byte marking the end of the compilation
16427 unit. Complain if it's not there, but exit no
16428 matter what. */
16429
16430 /* Do we at least have room for a macinfo type byte? */
16431 if (mac_ptr >= mac_end)
16432 {
16433 dwarf2_section_buffer_overflow_complaint (section);
16434 return;
16435 }
16436
16437 /* We don't increment mac_ptr here, so this is just
16438 a look-ahead. */
16439 next_type = read_1_byte (abfd, mac_ptr);
16440 if (next_type != 0)
16441 complaint (&symfile_complaints,
16442 _("no terminating 0-type entry for "
16443 "macros in `.debug_macinfo' section"));
16444
16445 return;
16446 }
16447 }
16448 break;
16449
16450 case DW_MACRO_GNU_transparent_include:
16451 {
16452 LONGEST offset;
16453 void **slot;
16454
16455 offset = read_offset_1 (abfd, mac_ptr, offset_size);
16456 mac_ptr += offset_size;
16457
16458 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
16459 if (*slot != NULL)
16460 {
16461 /* This has actually happened; see
16462 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
16463 complaint (&symfile_complaints,
16464 _("recursive DW_MACRO_GNU_transparent_include in "
16465 ".debug_macro section"));
16466 }
16467 else
16468 {
16469 *slot = mac_ptr;
16470
16471 dwarf_decode_macro_bytes (abfd,
16472 section->buffer + offset,
16473 mac_end, current_file,
16474 lh, comp_dir,
16475 section, section_is_gnu,
16476 offset_size, objfile, include_hash);
16477
16478 htab_remove_elt (include_hash, mac_ptr);
16479 }
16480 }
16481 break;
16482
16483 case DW_MACINFO_vendor_ext:
16484 if (!section_is_gnu)
16485 {
16486 unsigned int bytes_read;
16487 int constant;
16488
16489 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16490 mac_ptr += bytes_read;
16491 read_direct_string (abfd, mac_ptr, &bytes_read);
16492 mac_ptr += bytes_read;
16493
16494 /* We don't recognize any vendor extensions. */
16495 break;
16496 }
16497 /* FALLTHROUGH */
16498
16499 default:
16500 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
16501 mac_ptr, mac_end, abfd, offset_size,
16502 section);
16503 if (mac_ptr == NULL)
16504 return;
16505 break;
16506 }
16507 } while (macinfo_type != 0);
16508 }
16509
16510 static void
16511 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
16512 char *comp_dir, int section_is_gnu)
16513 {
16514 struct objfile *objfile = dwarf2_per_objfile->objfile;
16515 struct line_header *lh = cu->line_header;
16516 bfd *abfd;
16517 gdb_byte *mac_ptr, *mac_end;
16518 struct macro_source_file *current_file = 0;
16519 enum dwarf_macro_record_type macinfo_type;
16520 unsigned int offset_size = cu->header.offset_size;
16521 gdb_byte *opcode_definitions[256];
16522 struct cleanup *cleanup;
16523 htab_t include_hash;
16524 void **slot;
16525 struct dwarf2_section_info *section;
16526 const char *section_name;
16527
16528 if (cu->dwo_unit != NULL)
16529 {
16530 if (section_is_gnu)
16531 {
16532 section = &cu->dwo_unit->dwo_file->sections.macro;
16533 section_name = ".debug_macro.dwo";
16534 }
16535 else
16536 {
16537 section = &cu->dwo_unit->dwo_file->sections.macinfo;
16538 section_name = ".debug_macinfo.dwo";
16539 }
16540 }
16541 else
16542 {
16543 if (section_is_gnu)
16544 {
16545 section = &dwarf2_per_objfile->macro;
16546 section_name = ".debug_macro";
16547 }
16548 else
16549 {
16550 section = &dwarf2_per_objfile->macinfo;
16551 section_name = ".debug_macinfo";
16552 }
16553 }
16554
16555 dwarf2_read_section (objfile, section);
16556 if (section->buffer == NULL)
16557 {
16558 complaint (&symfile_complaints, _("missing %s section"), section_name);
16559 return;
16560 }
16561 abfd = section->asection->owner;
16562
16563 /* First pass: Find the name of the base filename.
16564 This filename is needed in order to process all macros whose definition
16565 (or undefinition) comes from the command line. These macros are defined
16566 before the first DW_MACINFO_start_file entry, and yet still need to be
16567 associated to the base file.
16568
16569 To determine the base file name, we scan the macro definitions until we
16570 reach the first DW_MACINFO_start_file entry. We then initialize
16571 CURRENT_FILE accordingly so that any macro definition found before the
16572 first DW_MACINFO_start_file can still be associated to the base file. */
16573
16574 mac_ptr = section->buffer + offset;
16575 mac_end = section->buffer + section->size;
16576
16577 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
16578 &offset_size, section_is_gnu);
16579 if (mac_ptr == NULL)
16580 {
16581 /* We already issued a complaint. */
16582 return;
16583 }
16584
16585 do
16586 {
16587 /* Do we at least have room for a macinfo type byte? */
16588 if (mac_ptr >= mac_end)
16589 {
16590 /* Complaint is printed during the second pass as GDB will probably
16591 stop the first pass earlier upon finding
16592 DW_MACINFO_start_file. */
16593 break;
16594 }
16595
16596 macinfo_type = read_1_byte (abfd, mac_ptr);
16597 mac_ptr++;
16598
16599 /* Note that we rely on the fact that the corresponding GNU and
16600 DWARF constants are the same. */
16601 switch (macinfo_type)
16602 {
16603 /* A zero macinfo type indicates the end of the macro
16604 information. */
16605 case 0:
16606 break;
16607
16608 case DW_MACRO_GNU_define:
16609 case DW_MACRO_GNU_undef:
16610 /* Only skip the data by MAC_PTR. */
16611 {
16612 unsigned int bytes_read;
16613
16614 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16615 mac_ptr += bytes_read;
16616 read_direct_string (abfd, mac_ptr, &bytes_read);
16617 mac_ptr += bytes_read;
16618 }
16619 break;
16620
16621 case DW_MACRO_GNU_start_file:
16622 {
16623 unsigned int bytes_read;
16624 int line, file;
16625
16626 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16627 mac_ptr += bytes_read;
16628 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16629 mac_ptr += bytes_read;
16630
16631 current_file = macro_start_file (file, line, current_file,
16632 comp_dir, lh, objfile);
16633 }
16634 break;
16635
16636 case DW_MACRO_GNU_end_file:
16637 /* No data to skip by MAC_PTR. */
16638 break;
16639
16640 case DW_MACRO_GNU_define_indirect:
16641 case DW_MACRO_GNU_undef_indirect:
16642 {
16643 unsigned int bytes_read;
16644
16645 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16646 mac_ptr += bytes_read;
16647 mac_ptr += offset_size;
16648 }
16649 break;
16650
16651 case DW_MACRO_GNU_transparent_include:
16652 /* Note that, according to the spec, a transparent include
16653 chain cannot call DW_MACRO_GNU_start_file. So, we can just
16654 skip this opcode. */
16655 mac_ptr += offset_size;
16656 break;
16657
16658 case DW_MACINFO_vendor_ext:
16659 /* Only skip the data by MAC_PTR. */
16660 if (!section_is_gnu)
16661 {
16662 unsigned int bytes_read;
16663
16664 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16665 mac_ptr += bytes_read;
16666 read_direct_string (abfd, mac_ptr, &bytes_read);
16667 mac_ptr += bytes_read;
16668 }
16669 /* FALLTHROUGH */
16670
16671 default:
16672 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
16673 mac_ptr, mac_end, abfd, offset_size,
16674 section);
16675 if (mac_ptr == NULL)
16676 return;
16677 break;
16678 }
16679 } while (macinfo_type != 0 && current_file == NULL);
16680
16681 /* Second pass: Process all entries.
16682
16683 Use the AT_COMMAND_LINE flag to determine whether we are still processing
16684 command-line macro definitions/undefinitions. This flag is unset when we
16685 reach the first DW_MACINFO_start_file entry. */
16686
16687 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
16688 NULL, xcalloc, xfree);
16689 cleanup = make_cleanup_htab_delete (include_hash);
16690 mac_ptr = section->buffer + offset;
16691 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
16692 *slot = mac_ptr;
16693 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
16694 current_file, lh, comp_dir, section, section_is_gnu,
16695 offset_size, objfile, include_hash);
16696 do_cleanups (cleanup);
16697 }
16698
16699 /* Check if the attribute's form is a DW_FORM_block*
16700 if so return true else false. */
16701
16702 static int
16703 attr_form_is_block (struct attribute *attr)
16704 {
16705 return (attr == NULL ? 0 :
16706 attr->form == DW_FORM_block1
16707 || attr->form == DW_FORM_block2
16708 || attr->form == DW_FORM_block4
16709 || attr->form == DW_FORM_block
16710 || attr->form == DW_FORM_exprloc);
16711 }
16712
16713 /* Return non-zero if ATTR's value is a section offset --- classes
16714 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
16715 You may use DW_UNSND (attr) to retrieve such offsets.
16716
16717 Section 7.5.4, "Attribute Encodings", explains that no attribute
16718 may have a value that belongs to more than one of these classes; it
16719 would be ambiguous if we did, because we use the same forms for all
16720 of them. */
16721
16722 static int
16723 attr_form_is_section_offset (struct attribute *attr)
16724 {
16725 return (attr->form == DW_FORM_data4
16726 || attr->form == DW_FORM_data8
16727 || attr->form == DW_FORM_sec_offset);
16728 }
16729
16730 /* Return non-zero if ATTR's value falls in the 'constant' class, or
16731 zero otherwise. When this function returns true, you can apply
16732 dwarf2_get_attr_constant_value to it.
16733
16734 However, note that for some attributes you must check
16735 attr_form_is_section_offset before using this test. DW_FORM_data4
16736 and DW_FORM_data8 are members of both the constant class, and of
16737 the classes that contain offsets into other debug sections
16738 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
16739 that, if an attribute's can be either a constant or one of the
16740 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
16741 taken as section offsets, not constants. */
16742
16743 static int
16744 attr_form_is_constant (struct attribute *attr)
16745 {
16746 switch (attr->form)
16747 {
16748 case DW_FORM_sdata:
16749 case DW_FORM_udata:
16750 case DW_FORM_data1:
16751 case DW_FORM_data2:
16752 case DW_FORM_data4:
16753 case DW_FORM_data8:
16754 return 1;
16755 default:
16756 return 0;
16757 }
16758 }
16759
16760 /* Return the .debug_loc section to use for CU.
16761 For DWO files use .debug_loc.dwo. */
16762
16763 static struct dwarf2_section_info *
16764 cu_debug_loc_section (struct dwarf2_cu *cu)
16765 {
16766 if (cu->dwo_unit)
16767 return &cu->dwo_unit->dwo_file->sections.loc;
16768 return &dwarf2_per_objfile->loc;
16769 }
16770
16771 /* A helper function that fills in a dwarf2_loclist_baton. */
16772
16773 static void
16774 fill_in_loclist_baton (struct dwarf2_cu *cu,
16775 struct dwarf2_loclist_baton *baton,
16776 struct attribute *attr)
16777 {
16778 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
16779
16780 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
16781
16782 baton->per_cu = cu->per_cu;
16783 gdb_assert (baton->per_cu);
16784 /* We don't know how long the location list is, but make sure we
16785 don't run off the edge of the section. */
16786 baton->size = section->size - DW_UNSND (attr);
16787 baton->data = section->buffer + DW_UNSND (attr);
16788 baton->base_address = cu->base_address;
16789 baton->from_dwo = cu->dwo_unit != NULL;
16790 }
16791
16792 static void
16793 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
16794 struct dwarf2_cu *cu)
16795 {
16796 struct objfile *objfile = dwarf2_per_objfile->objfile;
16797 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
16798
16799 if (attr_form_is_section_offset (attr)
16800 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
16801 the section. If so, fall through to the complaint in the
16802 other branch. */
16803 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
16804 {
16805 struct dwarf2_loclist_baton *baton;
16806
16807 baton = obstack_alloc (&objfile->objfile_obstack,
16808 sizeof (struct dwarf2_loclist_baton));
16809
16810 fill_in_loclist_baton (cu, baton, attr);
16811
16812 if (cu->base_known == 0)
16813 complaint (&symfile_complaints,
16814 _("Location list used without "
16815 "specifying the CU base address."));
16816
16817 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
16818 SYMBOL_LOCATION_BATON (sym) = baton;
16819 }
16820 else
16821 {
16822 struct dwarf2_locexpr_baton *baton;
16823
16824 baton = obstack_alloc (&objfile->objfile_obstack,
16825 sizeof (struct dwarf2_locexpr_baton));
16826 baton->per_cu = cu->per_cu;
16827 gdb_assert (baton->per_cu);
16828
16829 if (attr_form_is_block (attr))
16830 {
16831 /* Note that we're just copying the block's data pointer
16832 here, not the actual data. We're still pointing into the
16833 info_buffer for SYM's objfile; right now we never release
16834 that buffer, but when we do clean up properly this may
16835 need to change. */
16836 baton->size = DW_BLOCK (attr)->size;
16837 baton->data = DW_BLOCK (attr)->data;
16838 }
16839 else
16840 {
16841 dwarf2_invalid_attrib_class_complaint ("location description",
16842 SYMBOL_NATURAL_NAME (sym));
16843 baton->size = 0;
16844 }
16845
16846 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
16847 SYMBOL_LOCATION_BATON (sym) = baton;
16848 }
16849 }
16850
16851 /* Return the OBJFILE associated with the compilation unit CU. If CU
16852 came from a separate debuginfo file, then the master objfile is
16853 returned. */
16854
16855 struct objfile *
16856 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
16857 {
16858 struct objfile *objfile = per_cu->objfile;
16859
16860 /* Return the master objfile, so that we can report and look up the
16861 correct file containing this variable. */
16862 if (objfile->separate_debug_objfile_backlink)
16863 objfile = objfile->separate_debug_objfile_backlink;
16864
16865 return objfile;
16866 }
16867
16868 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
16869 (CU_HEADERP is unused in such case) or prepare a temporary copy at
16870 CU_HEADERP first. */
16871
16872 static const struct comp_unit_head *
16873 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
16874 struct dwarf2_per_cu_data *per_cu)
16875 {
16876 gdb_byte *info_ptr;
16877
16878 if (per_cu->cu)
16879 return &per_cu->cu->header;
16880
16881 info_ptr = per_cu->info_or_types_section->buffer + per_cu->offset.sect_off;
16882
16883 memset (cu_headerp, 0, sizeof (*cu_headerp));
16884 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
16885
16886 return cu_headerp;
16887 }
16888
16889 /* Return the address size given in the compilation unit header for CU. */
16890
16891 int
16892 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
16893 {
16894 struct comp_unit_head cu_header_local;
16895 const struct comp_unit_head *cu_headerp;
16896
16897 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16898
16899 return cu_headerp->addr_size;
16900 }
16901
16902 /* Return the offset size given in the compilation unit header for CU. */
16903
16904 int
16905 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
16906 {
16907 struct comp_unit_head cu_header_local;
16908 const struct comp_unit_head *cu_headerp;
16909
16910 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16911
16912 return cu_headerp->offset_size;
16913 }
16914
16915 /* See its dwarf2loc.h declaration. */
16916
16917 int
16918 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
16919 {
16920 struct comp_unit_head cu_header_local;
16921 const struct comp_unit_head *cu_headerp;
16922
16923 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16924
16925 if (cu_headerp->version == 2)
16926 return cu_headerp->addr_size;
16927 else
16928 return cu_headerp->offset_size;
16929 }
16930
16931 /* Return the text offset of the CU. The returned offset comes from
16932 this CU's objfile. If this objfile came from a separate debuginfo
16933 file, then the offset may be different from the corresponding
16934 offset in the parent objfile. */
16935
16936 CORE_ADDR
16937 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
16938 {
16939 struct objfile *objfile = per_cu->objfile;
16940
16941 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16942 }
16943
16944 /* Locate the .debug_info compilation unit from CU's objfile which contains
16945 the DIE at OFFSET. Raises an error on failure. */
16946
16947 static struct dwarf2_per_cu_data *
16948 dwarf2_find_containing_comp_unit (sect_offset offset,
16949 struct objfile *objfile)
16950 {
16951 struct dwarf2_per_cu_data *this_cu;
16952 int low, high;
16953
16954 low = 0;
16955 high = dwarf2_per_objfile->n_comp_units - 1;
16956 while (high > low)
16957 {
16958 int mid = low + (high - low) / 2;
16959
16960 if (dwarf2_per_objfile->all_comp_units[mid]->offset.sect_off
16961 >= offset.sect_off)
16962 high = mid;
16963 else
16964 low = mid + 1;
16965 }
16966 gdb_assert (low == high);
16967 if (dwarf2_per_objfile->all_comp_units[low]->offset.sect_off
16968 > offset.sect_off)
16969 {
16970 if (low == 0)
16971 error (_("Dwarf Error: could not find partial DIE containing "
16972 "offset 0x%lx [in module %s]"),
16973 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
16974
16975 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
16976 <= offset.sect_off);
16977 return dwarf2_per_objfile->all_comp_units[low-1];
16978 }
16979 else
16980 {
16981 this_cu = dwarf2_per_objfile->all_comp_units[low];
16982 if (low == dwarf2_per_objfile->n_comp_units - 1
16983 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
16984 error (_("invalid dwarf2 offset %u"), offset.sect_off);
16985 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
16986 return this_cu;
16987 }
16988 }
16989
16990 /* Initialize dwarf2_cu CU, owned by PER_CU. */
16991
16992 static void
16993 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
16994 {
16995 memset (cu, 0, sizeof (*cu));
16996 per_cu->cu = cu;
16997 cu->per_cu = per_cu;
16998 cu->objfile = per_cu->objfile;
16999 obstack_init (&cu->comp_unit_obstack);
17000 }
17001
17002 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
17003
17004 static void
17005 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
17006 enum language pretend_language)
17007 {
17008 struct attribute *attr;
17009
17010 /* Set the language we're debugging. */
17011 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
17012 if (attr)
17013 set_cu_language (DW_UNSND (attr), cu);
17014 else
17015 {
17016 cu->language = pretend_language;
17017 cu->language_defn = language_def (cu->language);
17018 }
17019
17020 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
17021 if (attr)
17022 cu->producer = DW_STRING (attr);
17023 }
17024
17025 /* Release one cached compilation unit, CU. We unlink it from the tree
17026 of compilation units, but we don't remove it from the read_in_chain;
17027 the caller is responsible for that.
17028 NOTE: DATA is a void * because this function is also used as a
17029 cleanup routine. */
17030
17031 static void
17032 free_heap_comp_unit (void *data)
17033 {
17034 struct dwarf2_cu *cu = data;
17035
17036 gdb_assert (cu->per_cu != NULL);
17037 cu->per_cu->cu = NULL;
17038 cu->per_cu = NULL;
17039
17040 obstack_free (&cu->comp_unit_obstack, NULL);
17041
17042 xfree (cu);
17043 }
17044
17045 /* This cleanup function is passed the address of a dwarf2_cu on the stack
17046 when we're finished with it. We can't free the pointer itself, but be
17047 sure to unlink it from the cache. Also release any associated storage. */
17048
17049 static void
17050 free_stack_comp_unit (void *data)
17051 {
17052 struct dwarf2_cu *cu = data;
17053
17054 gdb_assert (cu->per_cu != NULL);
17055 cu->per_cu->cu = NULL;
17056 cu->per_cu = NULL;
17057
17058 obstack_free (&cu->comp_unit_obstack, NULL);
17059 cu->partial_dies = NULL;
17060 }
17061
17062 /* Free all cached compilation units. */
17063
17064 static void
17065 free_cached_comp_units (void *data)
17066 {
17067 struct dwarf2_per_cu_data *per_cu, **last_chain;
17068
17069 per_cu = dwarf2_per_objfile->read_in_chain;
17070 last_chain = &dwarf2_per_objfile->read_in_chain;
17071 while (per_cu != NULL)
17072 {
17073 struct dwarf2_per_cu_data *next_cu;
17074
17075 next_cu = per_cu->cu->read_in_chain;
17076
17077 free_heap_comp_unit (per_cu->cu);
17078 *last_chain = next_cu;
17079
17080 per_cu = next_cu;
17081 }
17082 }
17083
17084 /* Increase the age counter on each cached compilation unit, and free
17085 any that are too old. */
17086
17087 static void
17088 age_cached_comp_units (void)
17089 {
17090 struct dwarf2_per_cu_data *per_cu, **last_chain;
17091
17092 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
17093 per_cu = dwarf2_per_objfile->read_in_chain;
17094 while (per_cu != NULL)
17095 {
17096 per_cu->cu->last_used ++;
17097 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
17098 dwarf2_mark (per_cu->cu);
17099 per_cu = per_cu->cu->read_in_chain;
17100 }
17101
17102 per_cu = dwarf2_per_objfile->read_in_chain;
17103 last_chain = &dwarf2_per_objfile->read_in_chain;
17104 while (per_cu != NULL)
17105 {
17106 struct dwarf2_per_cu_data *next_cu;
17107
17108 next_cu = per_cu->cu->read_in_chain;
17109
17110 if (!per_cu->cu->mark)
17111 {
17112 free_heap_comp_unit (per_cu->cu);
17113 *last_chain = next_cu;
17114 }
17115 else
17116 last_chain = &per_cu->cu->read_in_chain;
17117
17118 per_cu = next_cu;
17119 }
17120 }
17121
17122 /* Remove a single compilation unit from the cache. */
17123
17124 static void
17125 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
17126 {
17127 struct dwarf2_per_cu_data *per_cu, **last_chain;
17128
17129 per_cu = dwarf2_per_objfile->read_in_chain;
17130 last_chain = &dwarf2_per_objfile->read_in_chain;
17131 while (per_cu != NULL)
17132 {
17133 struct dwarf2_per_cu_data *next_cu;
17134
17135 next_cu = per_cu->cu->read_in_chain;
17136
17137 if (per_cu == target_per_cu)
17138 {
17139 free_heap_comp_unit (per_cu->cu);
17140 per_cu->cu = NULL;
17141 *last_chain = next_cu;
17142 break;
17143 }
17144 else
17145 last_chain = &per_cu->cu->read_in_chain;
17146
17147 per_cu = next_cu;
17148 }
17149 }
17150
17151 /* Release all extra memory associated with OBJFILE. */
17152
17153 void
17154 dwarf2_free_objfile (struct objfile *objfile)
17155 {
17156 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17157
17158 if (dwarf2_per_objfile == NULL)
17159 return;
17160
17161 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
17162 free_cached_comp_units (NULL);
17163
17164 if (dwarf2_per_objfile->quick_file_names_table)
17165 htab_delete (dwarf2_per_objfile->quick_file_names_table);
17166
17167 /* Everything else should be on the objfile obstack. */
17168 }
17169
17170 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
17171 We store these in a hash table separate from the DIEs, and preserve them
17172 when the DIEs are flushed out of cache.
17173
17174 The CU "per_cu" pointer is needed because offset alone is not enough to
17175 uniquely identify the type. A file may have multiple .debug_types sections,
17176 or the type may come from a DWO file. We have to use something in
17177 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
17178 routine, get_die_type_at_offset, from outside this file, and thus won't
17179 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
17180 of the objfile. */
17181
17182 struct dwarf2_per_cu_offset_and_type
17183 {
17184 const struct dwarf2_per_cu_data *per_cu;
17185 sect_offset offset;
17186 struct type *type;
17187 };
17188
17189 /* Hash function for a dwarf2_per_cu_offset_and_type. */
17190
17191 static hashval_t
17192 per_cu_offset_and_type_hash (const void *item)
17193 {
17194 const struct dwarf2_per_cu_offset_and_type *ofs = item;
17195
17196 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
17197 }
17198
17199 /* Equality function for a dwarf2_per_cu_offset_and_type. */
17200
17201 static int
17202 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
17203 {
17204 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
17205 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
17206
17207 return (ofs_lhs->per_cu == ofs_rhs->per_cu
17208 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
17209 }
17210
17211 /* Set the type associated with DIE to TYPE. Save it in CU's hash
17212 table if necessary. For convenience, return TYPE.
17213
17214 The DIEs reading must have careful ordering to:
17215 * Not cause infite loops trying to read in DIEs as a prerequisite for
17216 reading current DIE.
17217 * Not trying to dereference contents of still incompletely read in types
17218 while reading in other DIEs.
17219 * Enable referencing still incompletely read in types just by a pointer to
17220 the type without accessing its fields.
17221
17222 Therefore caller should follow these rules:
17223 * Try to fetch any prerequisite types we may need to build this DIE type
17224 before building the type and calling set_die_type.
17225 * After building type call set_die_type for current DIE as soon as
17226 possible before fetching more types to complete the current type.
17227 * Make the type as complete as possible before fetching more types. */
17228
17229 static struct type *
17230 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
17231 {
17232 struct dwarf2_per_cu_offset_and_type **slot, ofs;
17233 struct objfile *objfile = cu->objfile;
17234
17235 /* For Ada types, make sure that the gnat-specific data is always
17236 initialized (if not already set). There are a few types where
17237 we should not be doing so, because the type-specific area is
17238 already used to hold some other piece of info (eg: TYPE_CODE_FLT
17239 where the type-specific area is used to store the floatformat).
17240 But this is not a problem, because the gnat-specific information
17241 is actually not needed for these types. */
17242 if (need_gnat_info (cu)
17243 && TYPE_CODE (type) != TYPE_CODE_FUNC
17244 && TYPE_CODE (type) != TYPE_CODE_FLT
17245 && !HAVE_GNAT_AUX_INFO (type))
17246 INIT_GNAT_SPECIFIC (type);
17247
17248 if (dwarf2_per_objfile->die_type_hash == NULL)
17249 {
17250 dwarf2_per_objfile->die_type_hash =
17251 htab_create_alloc_ex (127,
17252 per_cu_offset_and_type_hash,
17253 per_cu_offset_and_type_eq,
17254 NULL,
17255 &objfile->objfile_obstack,
17256 hashtab_obstack_allocate,
17257 dummy_obstack_deallocate);
17258 }
17259
17260 ofs.per_cu = cu->per_cu;
17261 ofs.offset = die->offset;
17262 ofs.type = type;
17263 slot = (struct dwarf2_per_cu_offset_and_type **)
17264 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
17265 if (*slot)
17266 complaint (&symfile_complaints,
17267 _("A problem internal to GDB: DIE 0x%x has type already set"),
17268 die->offset.sect_off);
17269 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
17270 **slot = ofs;
17271 return type;
17272 }
17273
17274 /* Look up the type for the die at OFFSET in the appropriate type_hash
17275 table, or return NULL if the die does not have a saved type. */
17276
17277 static struct type *
17278 get_die_type_at_offset (sect_offset offset,
17279 struct dwarf2_per_cu_data *per_cu)
17280 {
17281 struct dwarf2_per_cu_offset_and_type *slot, ofs;
17282
17283 if (dwarf2_per_objfile->die_type_hash == NULL)
17284 return NULL;
17285
17286 ofs.per_cu = per_cu;
17287 ofs.offset = offset;
17288 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
17289 if (slot)
17290 return slot->type;
17291 else
17292 return NULL;
17293 }
17294
17295 /* Look up the type for DIE in the appropriate type_hash table,
17296 or return NULL if DIE does not have a saved type. */
17297
17298 static struct type *
17299 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
17300 {
17301 return get_die_type_at_offset (die->offset, cu->per_cu);
17302 }
17303
17304 /* Add a dependence relationship from CU to REF_PER_CU. */
17305
17306 static void
17307 dwarf2_add_dependence (struct dwarf2_cu *cu,
17308 struct dwarf2_per_cu_data *ref_per_cu)
17309 {
17310 void **slot;
17311
17312 if (cu->dependencies == NULL)
17313 cu->dependencies
17314 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
17315 NULL, &cu->comp_unit_obstack,
17316 hashtab_obstack_allocate,
17317 dummy_obstack_deallocate);
17318
17319 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
17320 if (*slot == NULL)
17321 *slot = ref_per_cu;
17322 }
17323
17324 /* Subroutine of dwarf2_mark to pass to htab_traverse.
17325 Set the mark field in every compilation unit in the
17326 cache that we must keep because we are keeping CU. */
17327
17328 static int
17329 dwarf2_mark_helper (void **slot, void *data)
17330 {
17331 struct dwarf2_per_cu_data *per_cu;
17332
17333 per_cu = (struct dwarf2_per_cu_data *) *slot;
17334
17335 /* cu->dependencies references may not yet have been ever read if QUIT aborts
17336 reading of the chain. As such dependencies remain valid it is not much
17337 useful to track and undo them during QUIT cleanups. */
17338 if (per_cu->cu == NULL)
17339 return 1;
17340
17341 if (per_cu->cu->mark)
17342 return 1;
17343 per_cu->cu->mark = 1;
17344
17345 if (per_cu->cu->dependencies != NULL)
17346 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
17347
17348 return 1;
17349 }
17350
17351 /* Set the mark field in CU and in every other compilation unit in the
17352 cache that we must keep because we are keeping CU. */
17353
17354 static void
17355 dwarf2_mark (struct dwarf2_cu *cu)
17356 {
17357 if (cu->mark)
17358 return;
17359 cu->mark = 1;
17360 if (cu->dependencies != NULL)
17361 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
17362 }
17363
17364 static void
17365 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
17366 {
17367 while (per_cu)
17368 {
17369 per_cu->cu->mark = 0;
17370 per_cu = per_cu->cu->read_in_chain;
17371 }
17372 }
17373
17374 /* Trivial hash function for partial_die_info: the hash value of a DIE
17375 is its offset in .debug_info for this objfile. */
17376
17377 static hashval_t
17378 partial_die_hash (const void *item)
17379 {
17380 const struct partial_die_info *part_die = item;
17381
17382 return part_die->offset.sect_off;
17383 }
17384
17385 /* Trivial comparison function for partial_die_info structures: two DIEs
17386 are equal if they have the same offset. */
17387
17388 static int
17389 partial_die_eq (const void *item_lhs, const void *item_rhs)
17390 {
17391 const struct partial_die_info *part_die_lhs = item_lhs;
17392 const struct partial_die_info *part_die_rhs = item_rhs;
17393
17394 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
17395 }
17396
17397 static struct cmd_list_element *set_dwarf2_cmdlist;
17398 static struct cmd_list_element *show_dwarf2_cmdlist;
17399
17400 static void
17401 set_dwarf2_cmd (char *args, int from_tty)
17402 {
17403 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
17404 }
17405
17406 static void
17407 show_dwarf2_cmd (char *args, int from_tty)
17408 {
17409 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
17410 }
17411
17412 /* If section described by INFO was mmapped, munmap it now. */
17413
17414 static void
17415 munmap_section_buffer (struct dwarf2_section_info *info)
17416 {
17417 if (info->map_addr != NULL)
17418 {
17419 #ifdef HAVE_MMAP
17420 int res;
17421
17422 res = munmap (info->map_addr, info->map_len);
17423 gdb_assert (res == 0);
17424 #else
17425 /* Without HAVE_MMAP, we should never be here to begin with. */
17426 gdb_assert_not_reached ("no mmap support");
17427 #endif
17428 }
17429 }
17430
17431 /* munmap debug sections for OBJFILE, if necessary. */
17432
17433 static void
17434 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
17435 {
17436 struct dwarf2_per_objfile *data = d;
17437 int ix;
17438 struct dwarf2_section_info *section;
17439
17440 /* This is sorted according to the order they're defined in to make it easier
17441 to keep in sync. */
17442 munmap_section_buffer (&data->info);
17443 munmap_section_buffer (&data->abbrev);
17444 munmap_section_buffer (&data->line);
17445 munmap_section_buffer (&data->loc);
17446 munmap_section_buffer (&data->macinfo);
17447 munmap_section_buffer (&data->macro);
17448 munmap_section_buffer (&data->str);
17449 munmap_section_buffer (&data->ranges);
17450 munmap_section_buffer (&data->addr);
17451 munmap_section_buffer (&data->frame);
17452 munmap_section_buffer (&data->eh_frame);
17453 munmap_section_buffer (&data->gdb_index);
17454
17455 for (ix = 0;
17456 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
17457 ++ix)
17458 munmap_section_buffer (section);
17459
17460 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
17461 VEC_free (dwarf2_per_cu_ptr,
17462 dwarf2_per_objfile->all_comp_units[ix]->imported_symtabs);
17463
17464 VEC_free (dwarf2_section_info_def, data->types);
17465
17466 if (data->dwo_files)
17467 free_dwo_files (data->dwo_files, objfile);
17468 }
17469
17470 \f
17471 /* The "save gdb-index" command. */
17472
17473 /* The contents of the hash table we create when building the string
17474 table. */
17475 struct strtab_entry
17476 {
17477 offset_type offset;
17478 const char *str;
17479 };
17480
17481 /* Hash function for a strtab_entry.
17482
17483 Function is used only during write_hash_table so no index format backward
17484 compatibility is needed. */
17485
17486 static hashval_t
17487 hash_strtab_entry (const void *e)
17488 {
17489 const struct strtab_entry *entry = e;
17490 return mapped_index_string_hash (INT_MAX, entry->str);
17491 }
17492
17493 /* Equality function for a strtab_entry. */
17494
17495 static int
17496 eq_strtab_entry (const void *a, const void *b)
17497 {
17498 const struct strtab_entry *ea = a;
17499 const struct strtab_entry *eb = b;
17500 return !strcmp (ea->str, eb->str);
17501 }
17502
17503 /* Create a strtab_entry hash table. */
17504
17505 static htab_t
17506 create_strtab (void)
17507 {
17508 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
17509 xfree, xcalloc, xfree);
17510 }
17511
17512 /* Add a string to the constant pool. Return the string's offset in
17513 host order. */
17514
17515 static offset_type
17516 add_string (htab_t table, struct obstack *cpool, const char *str)
17517 {
17518 void **slot;
17519 struct strtab_entry entry;
17520 struct strtab_entry *result;
17521
17522 entry.str = str;
17523 slot = htab_find_slot (table, &entry, INSERT);
17524 if (*slot)
17525 result = *slot;
17526 else
17527 {
17528 result = XNEW (struct strtab_entry);
17529 result->offset = obstack_object_size (cpool);
17530 result->str = str;
17531 obstack_grow_str0 (cpool, str);
17532 *slot = result;
17533 }
17534 return result->offset;
17535 }
17536
17537 /* An entry in the symbol table. */
17538 struct symtab_index_entry
17539 {
17540 /* The name of the symbol. */
17541 const char *name;
17542 /* The offset of the name in the constant pool. */
17543 offset_type index_offset;
17544 /* A sorted vector of the indices of all the CUs that hold an object
17545 of this name. */
17546 VEC (offset_type) *cu_indices;
17547 };
17548
17549 /* The symbol table. This is a power-of-2-sized hash table. */
17550 struct mapped_symtab
17551 {
17552 offset_type n_elements;
17553 offset_type size;
17554 struct symtab_index_entry **data;
17555 };
17556
17557 /* Hash function for a symtab_index_entry. */
17558
17559 static hashval_t
17560 hash_symtab_entry (const void *e)
17561 {
17562 const struct symtab_index_entry *entry = e;
17563 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
17564 sizeof (offset_type) * VEC_length (offset_type,
17565 entry->cu_indices),
17566 0);
17567 }
17568
17569 /* Equality function for a symtab_index_entry. */
17570
17571 static int
17572 eq_symtab_entry (const void *a, const void *b)
17573 {
17574 const struct symtab_index_entry *ea = a;
17575 const struct symtab_index_entry *eb = b;
17576 int len = VEC_length (offset_type, ea->cu_indices);
17577 if (len != VEC_length (offset_type, eb->cu_indices))
17578 return 0;
17579 return !memcmp (VEC_address (offset_type, ea->cu_indices),
17580 VEC_address (offset_type, eb->cu_indices),
17581 sizeof (offset_type) * len);
17582 }
17583
17584 /* Destroy a symtab_index_entry. */
17585
17586 static void
17587 delete_symtab_entry (void *p)
17588 {
17589 struct symtab_index_entry *entry = p;
17590 VEC_free (offset_type, entry->cu_indices);
17591 xfree (entry);
17592 }
17593
17594 /* Create a hash table holding symtab_index_entry objects. */
17595
17596 static htab_t
17597 create_symbol_hash_table (void)
17598 {
17599 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
17600 delete_symtab_entry, xcalloc, xfree);
17601 }
17602
17603 /* Create a new mapped symtab object. */
17604
17605 static struct mapped_symtab *
17606 create_mapped_symtab (void)
17607 {
17608 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
17609 symtab->n_elements = 0;
17610 symtab->size = 1024;
17611 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
17612 return symtab;
17613 }
17614
17615 /* Destroy a mapped_symtab. */
17616
17617 static void
17618 cleanup_mapped_symtab (void *p)
17619 {
17620 struct mapped_symtab *symtab = p;
17621 /* The contents of the array are freed when the other hash table is
17622 destroyed. */
17623 xfree (symtab->data);
17624 xfree (symtab);
17625 }
17626
17627 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
17628 the slot.
17629
17630 Function is used only during write_hash_table so no index format backward
17631 compatibility is needed. */
17632
17633 static struct symtab_index_entry **
17634 find_slot (struct mapped_symtab *symtab, const char *name)
17635 {
17636 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
17637
17638 index = hash & (symtab->size - 1);
17639 step = ((hash * 17) & (symtab->size - 1)) | 1;
17640
17641 for (;;)
17642 {
17643 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
17644 return &symtab->data[index];
17645 index = (index + step) & (symtab->size - 1);
17646 }
17647 }
17648
17649 /* Expand SYMTAB's hash table. */
17650
17651 static void
17652 hash_expand (struct mapped_symtab *symtab)
17653 {
17654 offset_type old_size = symtab->size;
17655 offset_type i;
17656 struct symtab_index_entry **old_entries = symtab->data;
17657
17658 symtab->size *= 2;
17659 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
17660
17661 for (i = 0; i < old_size; ++i)
17662 {
17663 if (old_entries[i])
17664 {
17665 struct symtab_index_entry **slot = find_slot (symtab,
17666 old_entries[i]->name);
17667 *slot = old_entries[i];
17668 }
17669 }
17670
17671 xfree (old_entries);
17672 }
17673
17674 /* Add an entry to SYMTAB. NAME is the name of the symbol.
17675 CU_INDEX is the index of the CU in which the symbol appears.
17676 IS_STATIC is one if the symbol is static, otherwise zero (global). */
17677
17678 static void
17679 add_index_entry (struct mapped_symtab *symtab, const char *name,
17680 int is_static, gdb_index_symbol_kind kind,
17681 offset_type cu_index)
17682 {
17683 struct symtab_index_entry **slot;
17684 offset_type cu_index_and_attrs;
17685
17686 ++symtab->n_elements;
17687 if (4 * symtab->n_elements / 3 >= symtab->size)
17688 hash_expand (symtab);
17689
17690 slot = find_slot (symtab, name);
17691 if (!*slot)
17692 {
17693 *slot = XNEW (struct symtab_index_entry);
17694 (*slot)->name = name;
17695 /* index_offset is set later. */
17696 (*slot)->cu_indices = NULL;
17697 }
17698
17699 cu_index_and_attrs = 0;
17700 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
17701 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
17702 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
17703
17704 /* We don't want to record an index value twice as we want to avoid the
17705 duplication.
17706 We process all global symbols and then all static symbols
17707 (which would allow us to avoid the duplication by only having to check
17708 the last entry pushed), but a symbol could have multiple kinds in one CU.
17709 To keep things simple we don't worry about the duplication here and
17710 sort and uniqufy the list after we've processed all symbols. */
17711 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
17712 }
17713
17714 /* qsort helper routine for uniquify_cu_indices. */
17715
17716 static int
17717 offset_type_compare (const void *ap, const void *bp)
17718 {
17719 offset_type a = *(offset_type *) ap;
17720 offset_type b = *(offset_type *) bp;
17721
17722 return (a > b) - (b > a);
17723 }
17724
17725 /* Sort and remove duplicates of all symbols' cu_indices lists. */
17726
17727 static void
17728 uniquify_cu_indices (struct mapped_symtab *symtab)
17729 {
17730 int i;
17731
17732 for (i = 0; i < symtab->size; ++i)
17733 {
17734 struct symtab_index_entry *entry = symtab->data[i];
17735
17736 if (entry
17737 && entry->cu_indices != NULL)
17738 {
17739 unsigned int next_to_insert, next_to_check;
17740 offset_type last_value;
17741
17742 qsort (VEC_address (offset_type, entry->cu_indices),
17743 VEC_length (offset_type, entry->cu_indices),
17744 sizeof (offset_type), offset_type_compare);
17745
17746 last_value = VEC_index (offset_type, entry->cu_indices, 0);
17747 next_to_insert = 1;
17748 for (next_to_check = 1;
17749 next_to_check < VEC_length (offset_type, entry->cu_indices);
17750 ++next_to_check)
17751 {
17752 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
17753 != last_value)
17754 {
17755 last_value = VEC_index (offset_type, entry->cu_indices,
17756 next_to_check);
17757 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
17758 last_value);
17759 ++next_to_insert;
17760 }
17761 }
17762 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
17763 }
17764 }
17765 }
17766
17767 /* Add a vector of indices to the constant pool. */
17768
17769 static offset_type
17770 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
17771 struct symtab_index_entry *entry)
17772 {
17773 void **slot;
17774
17775 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
17776 if (!*slot)
17777 {
17778 offset_type len = VEC_length (offset_type, entry->cu_indices);
17779 offset_type val = MAYBE_SWAP (len);
17780 offset_type iter;
17781 int i;
17782
17783 *slot = entry;
17784 entry->index_offset = obstack_object_size (cpool);
17785
17786 obstack_grow (cpool, &val, sizeof (val));
17787 for (i = 0;
17788 VEC_iterate (offset_type, entry->cu_indices, i, iter);
17789 ++i)
17790 {
17791 val = MAYBE_SWAP (iter);
17792 obstack_grow (cpool, &val, sizeof (val));
17793 }
17794 }
17795 else
17796 {
17797 struct symtab_index_entry *old_entry = *slot;
17798 entry->index_offset = old_entry->index_offset;
17799 entry = old_entry;
17800 }
17801 return entry->index_offset;
17802 }
17803
17804 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
17805 constant pool entries going into the obstack CPOOL. */
17806
17807 static void
17808 write_hash_table (struct mapped_symtab *symtab,
17809 struct obstack *output, struct obstack *cpool)
17810 {
17811 offset_type i;
17812 htab_t symbol_hash_table;
17813 htab_t str_table;
17814
17815 symbol_hash_table = create_symbol_hash_table ();
17816 str_table = create_strtab ();
17817
17818 /* We add all the index vectors to the constant pool first, to
17819 ensure alignment is ok. */
17820 for (i = 0; i < symtab->size; ++i)
17821 {
17822 if (symtab->data[i])
17823 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
17824 }
17825
17826 /* Now write out the hash table. */
17827 for (i = 0; i < symtab->size; ++i)
17828 {
17829 offset_type str_off, vec_off;
17830
17831 if (symtab->data[i])
17832 {
17833 str_off = add_string (str_table, cpool, symtab->data[i]->name);
17834 vec_off = symtab->data[i]->index_offset;
17835 }
17836 else
17837 {
17838 /* While 0 is a valid constant pool index, it is not valid
17839 to have 0 for both offsets. */
17840 str_off = 0;
17841 vec_off = 0;
17842 }
17843
17844 str_off = MAYBE_SWAP (str_off);
17845 vec_off = MAYBE_SWAP (vec_off);
17846
17847 obstack_grow (output, &str_off, sizeof (str_off));
17848 obstack_grow (output, &vec_off, sizeof (vec_off));
17849 }
17850
17851 htab_delete (str_table);
17852 htab_delete (symbol_hash_table);
17853 }
17854
17855 /* Struct to map psymtab to CU index in the index file. */
17856 struct psymtab_cu_index_map
17857 {
17858 struct partial_symtab *psymtab;
17859 unsigned int cu_index;
17860 };
17861
17862 static hashval_t
17863 hash_psymtab_cu_index (const void *item)
17864 {
17865 const struct psymtab_cu_index_map *map = item;
17866
17867 return htab_hash_pointer (map->psymtab);
17868 }
17869
17870 static int
17871 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
17872 {
17873 const struct psymtab_cu_index_map *lhs = item_lhs;
17874 const struct psymtab_cu_index_map *rhs = item_rhs;
17875
17876 return lhs->psymtab == rhs->psymtab;
17877 }
17878
17879 /* Helper struct for building the address table. */
17880 struct addrmap_index_data
17881 {
17882 struct objfile *objfile;
17883 struct obstack *addr_obstack;
17884 htab_t cu_index_htab;
17885
17886 /* Non-zero if the previous_* fields are valid.
17887 We can't write an entry until we see the next entry (since it is only then
17888 that we know the end of the entry). */
17889 int previous_valid;
17890 /* Index of the CU in the table of all CUs in the index file. */
17891 unsigned int previous_cu_index;
17892 /* Start address of the CU. */
17893 CORE_ADDR previous_cu_start;
17894 };
17895
17896 /* Write an address entry to OBSTACK. */
17897
17898 static void
17899 add_address_entry (struct objfile *objfile, struct obstack *obstack,
17900 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
17901 {
17902 offset_type cu_index_to_write;
17903 char addr[8];
17904 CORE_ADDR baseaddr;
17905
17906 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
17907
17908 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
17909 obstack_grow (obstack, addr, 8);
17910 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
17911 obstack_grow (obstack, addr, 8);
17912 cu_index_to_write = MAYBE_SWAP (cu_index);
17913 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
17914 }
17915
17916 /* Worker function for traversing an addrmap to build the address table. */
17917
17918 static int
17919 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
17920 {
17921 struct addrmap_index_data *data = datap;
17922 struct partial_symtab *pst = obj;
17923
17924 if (data->previous_valid)
17925 add_address_entry (data->objfile, data->addr_obstack,
17926 data->previous_cu_start, start_addr,
17927 data->previous_cu_index);
17928
17929 data->previous_cu_start = start_addr;
17930 if (pst != NULL)
17931 {
17932 struct psymtab_cu_index_map find_map, *map;
17933 find_map.psymtab = pst;
17934 map = htab_find (data->cu_index_htab, &find_map);
17935 gdb_assert (map != NULL);
17936 data->previous_cu_index = map->cu_index;
17937 data->previous_valid = 1;
17938 }
17939 else
17940 data->previous_valid = 0;
17941
17942 return 0;
17943 }
17944
17945 /* Write OBJFILE's address map to OBSTACK.
17946 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
17947 in the index file. */
17948
17949 static void
17950 write_address_map (struct objfile *objfile, struct obstack *obstack,
17951 htab_t cu_index_htab)
17952 {
17953 struct addrmap_index_data addrmap_index_data;
17954
17955 /* When writing the address table, we have to cope with the fact that
17956 the addrmap iterator only provides the start of a region; we have to
17957 wait until the next invocation to get the start of the next region. */
17958
17959 addrmap_index_data.objfile = objfile;
17960 addrmap_index_data.addr_obstack = obstack;
17961 addrmap_index_data.cu_index_htab = cu_index_htab;
17962 addrmap_index_data.previous_valid = 0;
17963
17964 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
17965 &addrmap_index_data);
17966
17967 /* It's highly unlikely the last entry (end address = 0xff...ff)
17968 is valid, but we should still handle it.
17969 The end address is recorded as the start of the next region, but that
17970 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
17971 anyway. */
17972 if (addrmap_index_data.previous_valid)
17973 add_address_entry (objfile, obstack,
17974 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
17975 addrmap_index_data.previous_cu_index);
17976 }
17977
17978 /* Return the symbol kind of PSYM. */
17979
17980 static gdb_index_symbol_kind
17981 symbol_kind (struct partial_symbol *psym)
17982 {
17983 domain_enum domain = PSYMBOL_DOMAIN (psym);
17984 enum address_class aclass = PSYMBOL_CLASS (psym);
17985
17986 switch (domain)
17987 {
17988 case VAR_DOMAIN:
17989 switch (aclass)
17990 {
17991 case LOC_BLOCK:
17992 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
17993 case LOC_TYPEDEF:
17994 return GDB_INDEX_SYMBOL_KIND_TYPE;
17995 case LOC_COMPUTED:
17996 case LOC_CONST_BYTES:
17997 case LOC_OPTIMIZED_OUT:
17998 case LOC_STATIC:
17999 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
18000 case LOC_CONST:
18001 /* Note: It's currently impossible to recognize psyms as enum values
18002 short of reading the type info. For now punt. */
18003 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
18004 default:
18005 /* There are other LOC_FOO values that one might want to classify
18006 as variables, but dwarf2read.c doesn't currently use them. */
18007 return GDB_INDEX_SYMBOL_KIND_OTHER;
18008 }
18009 case STRUCT_DOMAIN:
18010 return GDB_INDEX_SYMBOL_KIND_TYPE;
18011 default:
18012 return GDB_INDEX_SYMBOL_KIND_OTHER;
18013 }
18014 }
18015
18016 /* Add a list of partial symbols to SYMTAB. */
18017
18018 static void
18019 write_psymbols (struct mapped_symtab *symtab,
18020 htab_t psyms_seen,
18021 struct partial_symbol **psymp,
18022 int count,
18023 offset_type cu_index,
18024 int is_static)
18025 {
18026 for (; count-- > 0; ++psymp)
18027 {
18028 struct partial_symbol *psym = *psymp;
18029 void **slot;
18030
18031 if (SYMBOL_LANGUAGE (psym) == language_ada)
18032 error (_("Ada is not currently supported by the index"));
18033
18034 /* Only add a given psymbol once. */
18035 slot = htab_find_slot (psyms_seen, psym, INSERT);
18036 if (!*slot)
18037 {
18038 gdb_index_symbol_kind kind = symbol_kind (psym);
18039
18040 *slot = psym;
18041 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
18042 is_static, kind, cu_index);
18043 }
18044 }
18045 }
18046
18047 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
18048 exception if there is an error. */
18049
18050 static void
18051 write_obstack (FILE *file, struct obstack *obstack)
18052 {
18053 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
18054 file)
18055 != obstack_object_size (obstack))
18056 error (_("couldn't data write to file"));
18057 }
18058
18059 /* Unlink a file if the argument is not NULL. */
18060
18061 static void
18062 unlink_if_set (void *p)
18063 {
18064 char **filename = p;
18065 if (*filename)
18066 unlink (*filename);
18067 }
18068
18069 /* A helper struct used when iterating over debug_types. */
18070 struct signatured_type_index_data
18071 {
18072 struct objfile *objfile;
18073 struct mapped_symtab *symtab;
18074 struct obstack *types_list;
18075 htab_t psyms_seen;
18076 int cu_index;
18077 };
18078
18079 /* A helper function that writes a single signatured_type to an
18080 obstack. */
18081
18082 static int
18083 write_one_signatured_type (void **slot, void *d)
18084 {
18085 struct signatured_type_index_data *info = d;
18086 struct signatured_type *entry = (struct signatured_type *) *slot;
18087 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
18088 struct partial_symtab *psymtab = per_cu->v.psymtab;
18089 gdb_byte val[8];
18090
18091 write_psymbols (info->symtab,
18092 info->psyms_seen,
18093 info->objfile->global_psymbols.list
18094 + psymtab->globals_offset,
18095 psymtab->n_global_syms, info->cu_index,
18096 0);
18097 write_psymbols (info->symtab,
18098 info->psyms_seen,
18099 info->objfile->static_psymbols.list
18100 + psymtab->statics_offset,
18101 psymtab->n_static_syms, info->cu_index,
18102 1);
18103
18104 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
18105 entry->per_cu.offset.sect_off);
18106 obstack_grow (info->types_list, val, 8);
18107 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
18108 entry->type_offset_in_tu.cu_off);
18109 obstack_grow (info->types_list, val, 8);
18110 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
18111 obstack_grow (info->types_list, val, 8);
18112
18113 ++info->cu_index;
18114
18115 return 1;
18116 }
18117
18118 /* Recurse into all "included" dependencies and write their symbols as
18119 if they appeared in this psymtab. */
18120
18121 static void
18122 recursively_write_psymbols (struct objfile *objfile,
18123 struct partial_symtab *psymtab,
18124 struct mapped_symtab *symtab,
18125 htab_t psyms_seen,
18126 offset_type cu_index)
18127 {
18128 int i;
18129
18130 for (i = 0; i < psymtab->number_of_dependencies; ++i)
18131 if (psymtab->dependencies[i]->user != NULL)
18132 recursively_write_psymbols (objfile, psymtab->dependencies[i],
18133 symtab, psyms_seen, cu_index);
18134
18135 write_psymbols (symtab,
18136 psyms_seen,
18137 objfile->global_psymbols.list + psymtab->globals_offset,
18138 psymtab->n_global_syms, cu_index,
18139 0);
18140 write_psymbols (symtab,
18141 psyms_seen,
18142 objfile->static_psymbols.list + psymtab->statics_offset,
18143 psymtab->n_static_syms, cu_index,
18144 1);
18145 }
18146
18147 /* Create an index file for OBJFILE in the directory DIR. */
18148
18149 static void
18150 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
18151 {
18152 struct cleanup *cleanup;
18153 char *filename, *cleanup_filename;
18154 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
18155 struct obstack cu_list, types_cu_list;
18156 int i;
18157 FILE *out_file;
18158 struct mapped_symtab *symtab;
18159 offset_type val, size_of_contents, total_len;
18160 struct stat st;
18161 htab_t psyms_seen;
18162 htab_t cu_index_htab;
18163 struct psymtab_cu_index_map *psymtab_cu_index_map;
18164
18165 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
18166 return;
18167
18168 if (dwarf2_per_objfile->using_index)
18169 error (_("Cannot use an index to create the index"));
18170
18171 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
18172 error (_("Cannot make an index when the file has multiple .debug_types sections"));
18173
18174 if (stat (objfile->name, &st) < 0)
18175 perror_with_name (objfile->name);
18176
18177 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
18178 INDEX_SUFFIX, (char *) NULL);
18179 cleanup = make_cleanup (xfree, filename);
18180
18181 out_file = fopen (filename, "wb");
18182 if (!out_file)
18183 error (_("Can't open `%s' for writing"), filename);
18184
18185 cleanup_filename = filename;
18186 make_cleanup (unlink_if_set, &cleanup_filename);
18187
18188 symtab = create_mapped_symtab ();
18189 make_cleanup (cleanup_mapped_symtab, symtab);
18190
18191 obstack_init (&addr_obstack);
18192 make_cleanup_obstack_free (&addr_obstack);
18193
18194 obstack_init (&cu_list);
18195 make_cleanup_obstack_free (&cu_list);
18196
18197 obstack_init (&types_cu_list);
18198 make_cleanup_obstack_free (&types_cu_list);
18199
18200 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
18201 NULL, xcalloc, xfree);
18202 make_cleanup_htab_delete (psyms_seen);
18203
18204 /* While we're scanning CU's create a table that maps a psymtab pointer
18205 (which is what addrmap records) to its index (which is what is recorded
18206 in the index file). This will later be needed to write the address
18207 table. */
18208 cu_index_htab = htab_create_alloc (100,
18209 hash_psymtab_cu_index,
18210 eq_psymtab_cu_index,
18211 NULL, xcalloc, xfree);
18212 make_cleanup_htab_delete (cu_index_htab);
18213 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
18214 xmalloc (sizeof (struct psymtab_cu_index_map)
18215 * dwarf2_per_objfile->n_comp_units);
18216 make_cleanup (xfree, psymtab_cu_index_map);
18217
18218 /* The CU list is already sorted, so we don't need to do additional
18219 work here. Also, the debug_types entries do not appear in
18220 all_comp_units, but only in their own hash table. */
18221 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
18222 {
18223 struct dwarf2_per_cu_data *per_cu
18224 = dwarf2_per_objfile->all_comp_units[i];
18225 struct partial_symtab *psymtab = per_cu->v.psymtab;
18226 gdb_byte val[8];
18227 struct psymtab_cu_index_map *map;
18228 void **slot;
18229
18230 if (psymtab->user == NULL)
18231 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
18232
18233 map = &psymtab_cu_index_map[i];
18234 map->psymtab = psymtab;
18235 map->cu_index = i;
18236 slot = htab_find_slot (cu_index_htab, map, INSERT);
18237 gdb_assert (slot != NULL);
18238 gdb_assert (*slot == NULL);
18239 *slot = map;
18240
18241 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
18242 per_cu->offset.sect_off);
18243 obstack_grow (&cu_list, val, 8);
18244 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
18245 obstack_grow (&cu_list, val, 8);
18246 }
18247
18248 /* Dump the address map. */
18249 write_address_map (objfile, &addr_obstack, cu_index_htab);
18250
18251 /* Write out the .debug_type entries, if any. */
18252 if (dwarf2_per_objfile->signatured_types)
18253 {
18254 struct signatured_type_index_data sig_data;
18255
18256 sig_data.objfile = objfile;
18257 sig_data.symtab = symtab;
18258 sig_data.types_list = &types_cu_list;
18259 sig_data.psyms_seen = psyms_seen;
18260 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
18261 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
18262 write_one_signatured_type, &sig_data);
18263 }
18264
18265 /* Now that we've processed all symbols we can shrink their cu_indices
18266 lists. */
18267 uniquify_cu_indices (symtab);
18268
18269 obstack_init (&constant_pool);
18270 make_cleanup_obstack_free (&constant_pool);
18271 obstack_init (&symtab_obstack);
18272 make_cleanup_obstack_free (&symtab_obstack);
18273 write_hash_table (symtab, &symtab_obstack, &constant_pool);
18274
18275 obstack_init (&contents);
18276 make_cleanup_obstack_free (&contents);
18277 size_of_contents = 6 * sizeof (offset_type);
18278 total_len = size_of_contents;
18279
18280 /* The version number. */
18281 val = MAYBE_SWAP (7);
18282 obstack_grow (&contents, &val, sizeof (val));
18283
18284 /* The offset of the CU list from the start of the file. */
18285 val = MAYBE_SWAP (total_len);
18286 obstack_grow (&contents, &val, sizeof (val));
18287 total_len += obstack_object_size (&cu_list);
18288
18289 /* The offset of the types CU list from the start of the file. */
18290 val = MAYBE_SWAP (total_len);
18291 obstack_grow (&contents, &val, sizeof (val));
18292 total_len += obstack_object_size (&types_cu_list);
18293
18294 /* The offset of the address table from the start of the file. */
18295 val = MAYBE_SWAP (total_len);
18296 obstack_grow (&contents, &val, sizeof (val));
18297 total_len += obstack_object_size (&addr_obstack);
18298
18299 /* The offset of the symbol table from the start of the file. */
18300 val = MAYBE_SWAP (total_len);
18301 obstack_grow (&contents, &val, sizeof (val));
18302 total_len += obstack_object_size (&symtab_obstack);
18303
18304 /* The offset of the constant pool from the start of the file. */
18305 val = MAYBE_SWAP (total_len);
18306 obstack_grow (&contents, &val, sizeof (val));
18307 total_len += obstack_object_size (&constant_pool);
18308
18309 gdb_assert (obstack_object_size (&contents) == size_of_contents);
18310
18311 write_obstack (out_file, &contents);
18312 write_obstack (out_file, &cu_list);
18313 write_obstack (out_file, &types_cu_list);
18314 write_obstack (out_file, &addr_obstack);
18315 write_obstack (out_file, &symtab_obstack);
18316 write_obstack (out_file, &constant_pool);
18317
18318 fclose (out_file);
18319
18320 /* We want to keep the file, so we set cleanup_filename to NULL
18321 here. See unlink_if_set. */
18322 cleanup_filename = NULL;
18323
18324 do_cleanups (cleanup);
18325 }
18326
18327 /* Implementation of the `save gdb-index' command.
18328
18329 Note that the file format used by this command is documented in the
18330 GDB manual. Any changes here must be documented there. */
18331
18332 static void
18333 save_gdb_index_command (char *arg, int from_tty)
18334 {
18335 struct objfile *objfile;
18336
18337 if (!arg || !*arg)
18338 error (_("usage: save gdb-index DIRECTORY"));
18339
18340 ALL_OBJFILES (objfile)
18341 {
18342 struct stat st;
18343
18344 /* If the objfile does not correspond to an actual file, skip it. */
18345 if (stat (objfile->name, &st) < 0)
18346 continue;
18347
18348 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
18349 if (dwarf2_per_objfile)
18350 {
18351 volatile struct gdb_exception except;
18352
18353 TRY_CATCH (except, RETURN_MASK_ERROR)
18354 {
18355 write_psymtabs_to_index (objfile, arg);
18356 }
18357 if (except.reason < 0)
18358 exception_fprintf (gdb_stderr, except,
18359 _("Error while writing index for `%s': "),
18360 objfile->name);
18361 }
18362 }
18363 }
18364
18365 \f
18366
18367 int dwarf2_always_disassemble;
18368
18369 static void
18370 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
18371 struct cmd_list_element *c, const char *value)
18372 {
18373 fprintf_filtered (file,
18374 _("Whether to always disassemble "
18375 "DWARF expressions is %s.\n"),
18376 value);
18377 }
18378
18379 static void
18380 show_check_physname (struct ui_file *file, int from_tty,
18381 struct cmd_list_element *c, const char *value)
18382 {
18383 fprintf_filtered (file,
18384 _("Whether to check \"physname\" is %s.\n"),
18385 value);
18386 }
18387
18388 void _initialize_dwarf2_read (void);
18389
18390 void
18391 _initialize_dwarf2_read (void)
18392 {
18393 struct cmd_list_element *c;
18394
18395 dwarf2_objfile_data_key
18396 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
18397
18398 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
18399 Set DWARF 2 specific variables.\n\
18400 Configure DWARF 2 variables such as the cache size"),
18401 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
18402 0/*allow-unknown*/, &maintenance_set_cmdlist);
18403
18404 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
18405 Show DWARF 2 specific variables\n\
18406 Show DWARF 2 variables such as the cache size"),
18407 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
18408 0/*allow-unknown*/, &maintenance_show_cmdlist);
18409
18410 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
18411 &dwarf2_max_cache_age, _("\
18412 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
18413 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
18414 A higher limit means that cached compilation units will be stored\n\
18415 in memory longer, and more total memory will be used. Zero disables\n\
18416 caching, which can slow down startup."),
18417 NULL,
18418 show_dwarf2_max_cache_age,
18419 &set_dwarf2_cmdlist,
18420 &show_dwarf2_cmdlist);
18421
18422 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
18423 &dwarf2_always_disassemble, _("\
18424 Set whether `info address' always disassembles DWARF expressions."), _("\
18425 Show whether `info address' always disassembles DWARF expressions."), _("\
18426 When enabled, DWARF expressions are always printed in an assembly-like\n\
18427 syntax. When disabled, expressions will be printed in a more\n\
18428 conversational style, when possible."),
18429 NULL,
18430 show_dwarf2_always_disassemble,
18431 &set_dwarf2_cmdlist,
18432 &show_dwarf2_cmdlist);
18433
18434 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
18435 Set debugging of the dwarf2 reader."), _("\
18436 Show debugging of the dwarf2 reader."), _("\
18437 When enabled, debugging messages are printed during dwarf2 reading\n\
18438 and symtab expansion."),
18439 NULL,
18440 NULL,
18441 &setdebuglist, &showdebuglist);
18442
18443 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
18444 Set debugging of the dwarf2 DIE reader."), _("\
18445 Show debugging of the dwarf2 DIE reader."), _("\
18446 When enabled (non-zero), DIEs are dumped after they are read in.\n\
18447 The value is the maximum depth to print."),
18448 NULL,
18449 NULL,
18450 &setdebuglist, &showdebuglist);
18451
18452 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
18453 Set cross-checking of \"physname\" code against demangler."), _("\
18454 Show cross-checking of \"physname\" code against demangler."), _("\
18455 When enabled, GDB's internal \"physname\" code is checked against\n\
18456 the demangler."),
18457 NULL, show_check_physname,
18458 &setdebuglist, &showdebuglist);
18459
18460 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
18461 _("\
18462 Save a gdb-index file.\n\
18463 Usage: save gdb-index DIRECTORY"),
18464 &save_cmdlist);
18465 set_cmd_completer (c, filename_completer);
18466 }