* dwarf2read.c (dwarf2_find_base_address): Move definition.
[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 /* Find the base address of the compilation unit for range lists and
3375 location lists. It will normally be specified by DW_AT_low_pc.
3376 In DWARF-3 draft 4, the base address could be overridden by
3377 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3378 compilation units with discontinuous ranges. */
3379
3380 static void
3381 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3382 {
3383 struct attribute *attr;
3384
3385 cu->base_known = 0;
3386 cu->base_address = 0;
3387
3388 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3389 if (attr)
3390 {
3391 cu->base_address = DW_ADDR (attr);
3392 cu->base_known = 1;
3393 }
3394 else
3395 {
3396 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3397 if (attr)
3398 {
3399 cu->base_address = DW_ADDR (attr);
3400 cu->base_known = 1;
3401 }
3402 }
3403 }
3404
3405 /* Read in the comp unit header information from the debug_info at info_ptr.
3406 NOTE: This leaves members offset, first_die_offset to be filled in
3407 by the caller. */
3408
3409 static gdb_byte *
3410 read_comp_unit_head (struct comp_unit_head *cu_header,
3411 gdb_byte *info_ptr, bfd *abfd)
3412 {
3413 int signed_addr;
3414 unsigned int bytes_read;
3415
3416 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3417 cu_header->initial_length_size = bytes_read;
3418 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
3419 info_ptr += bytes_read;
3420 cu_header->version = read_2_bytes (abfd, info_ptr);
3421 info_ptr += 2;
3422 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3423 &bytes_read);
3424 info_ptr += bytes_read;
3425 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3426 info_ptr += 1;
3427 signed_addr = bfd_get_sign_extend_vma (abfd);
3428 if (signed_addr < 0)
3429 internal_error (__FILE__, __LINE__,
3430 _("read_comp_unit_head: dwarf from non elf file"));
3431 cu_header->signed_addr_p = signed_addr;
3432
3433 return info_ptr;
3434 }
3435
3436 /* Subroutine of read_and_check_comp_unit_head and
3437 read_and_check_type_unit_head to simplify them.
3438 Perform various error checking on the header. */
3439
3440 static void
3441 error_check_comp_unit_head (struct comp_unit_head *header,
3442 struct dwarf2_section_info *section,
3443 struct dwarf2_section_info *abbrev_section)
3444 {
3445 bfd *abfd = section->asection->owner;
3446 const char *filename = bfd_get_filename (abfd);
3447
3448 if (header->version != 2 && header->version != 3 && header->version != 4)
3449 error (_("Dwarf Error: wrong version in compilation unit header "
3450 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3451 filename);
3452
3453 if (header->abbrev_offset.sect_off
3454 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
3455 &dwarf2_per_objfile->abbrev))
3456 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3457 "(offset 0x%lx + 6) [in module %s]"),
3458 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
3459 filename);
3460
3461 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3462 avoid potential 32-bit overflow. */
3463 if (((unsigned long) header->offset.sect_off
3464 + header->length + header->initial_length_size)
3465 > section->size)
3466 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3467 "(offset 0x%lx + 0) [in module %s]"),
3468 (long) header->length, (long) header->offset.sect_off,
3469 filename);
3470 }
3471
3472 /* Read in a CU/TU header and perform some basic error checking.
3473 The contents of the header are stored in HEADER.
3474 The result is a pointer to the start of the first DIE. */
3475
3476 static gdb_byte *
3477 read_and_check_comp_unit_head (struct comp_unit_head *header,
3478 struct dwarf2_section_info *section,
3479 struct dwarf2_section_info *abbrev_section,
3480 gdb_byte *info_ptr,
3481 int is_debug_types_section)
3482 {
3483 gdb_byte *beg_of_comp_unit = info_ptr;
3484 bfd *abfd = section->asection->owner;
3485
3486 header->offset.sect_off = beg_of_comp_unit - section->buffer;
3487
3488 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3489
3490 /* If we're reading a type unit, skip over the signature and
3491 type_offset fields. */
3492 if (is_debug_types_section)
3493 info_ptr += 8 /*signature*/ + header->offset_size;
3494
3495 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
3496
3497 error_check_comp_unit_head (header, section, abbrev_section);
3498
3499 return info_ptr;
3500 }
3501
3502 /* Read in the types comp unit header information from .debug_types entry at
3503 types_ptr. The result is a pointer to one past the end of the header. */
3504
3505 static gdb_byte *
3506 read_and_check_type_unit_head (struct comp_unit_head *header,
3507 struct dwarf2_section_info *section,
3508 struct dwarf2_section_info *abbrev_section,
3509 gdb_byte *info_ptr,
3510 ULONGEST *signature,
3511 cu_offset *type_offset_in_tu)
3512 {
3513 gdb_byte *beg_of_comp_unit = info_ptr;
3514 bfd *abfd = section->asection->owner;
3515
3516 header->offset.sect_off = beg_of_comp_unit - section->buffer;
3517
3518 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3519
3520 /* If we're reading a type unit, skip over the signature and
3521 type_offset fields. */
3522 if (signature != NULL)
3523 *signature = read_8_bytes (abfd, info_ptr);
3524 info_ptr += 8;
3525 if (type_offset_in_tu != NULL)
3526 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
3527 header->offset_size);
3528 info_ptr += header->offset_size;
3529
3530 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
3531
3532 error_check_comp_unit_head (header, section, abbrev_section);
3533
3534 return info_ptr;
3535 }
3536
3537 /* Allocate a new partial symtab for file named NAME and mark this new
3538 partial symtab as being an include of PST. */
3539
3540 static void
3541 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3542 struct objfile *objfile)
3543 {
3544 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3545
3546 subpst->section_offsets = pst->section_offsets;
3547 subpst->textlow = 0;
3548 subpst->texthigh = 0;
3549
3550 subpst->dependencies = (struct partial_symtab **)
3551 obstack_alloc (&objfile->objfile_obstack,
3552 sizeof (struct partial_symtab *));
3553 subpst->dependencies[0] = pst;
3554 subpst->number_of_dependencies = 1;
3555
3556 subpst->globals_offset = 0;
3557 subpst->n_global_syms = 0;
3558 subpst->statics_offset = 0;
3559 subpst->n_static_syms = 0;
3560 subpst->symtab = NULL;
3561 subpst->read_symtab = pst->read_symtab;
3562 subpst->readin = 0;
3563
3564 /* No private part is necessary for include psymtabs. This property
3565 can be used to differentiate between such include psymtabs and
3566 the regular ones. */
3567 subpst->read_symtab_private = NULL;
3568 }
3569
3570 /* Read the Line Number Program data and extract the list of files
3571 included by the source file represented by PST. Build an include
3572 partial symtab for each of these included files. */
3573
3574 static void
3575 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
3576 struct die_info *die,
3577 struct partial_symtab *pst)
3578 {
3579 struct line_header *lh = NULL;
3580 struct attribute *attr;
3581
3582 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3583 if (attr)
3584 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
3585 if (lh == NULL)
3586 return; /* No linetable, so no includes. */
3587
3588 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
3589 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
3590
3591 free_line_header (lh);
3592 }
3593
3594 static hashval_t
3595 hash_signatured_type (const void *item)
3596 {
3597 const struct signatured_type *sig_type = item;
3598
3599 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3600 return sig_type->signature;
3601 }
3602
3603 static int
3604 eq_signatured_type (const void *item_lhs, const void *item_rhs)
3605 {
3606 const struct signatured_type *lhs = item_lhs;
3607 const struct signatured_type *rhs = item_rhs;
3608
3609 return lhs->signature == rhs->signature;
3610 }
3611
3612 /* Allocate a hash table for signatured types. */
3613
3614 static htab_t
3615 allocate_signatured_type_table (struct objfile *objfile)
3616 {
3617 return htab_create_alloc_ex (41,
3618 hash_signatured_type,
3619 eq_signatured_type,
3620 NULL,
3621 &objfile->objfile_obstack,
3622 hashtab_obstack_allocate,
3623 dummy_obstack_deallocate);
3624 }
3625
3626 /* A helper function to add a signatured type CU to a table. */
3627
3628 static int
3629 add_signatured_type_cu_to_table (void **slot, void *datum)
3630 {
3631 struct signatured_type *sigt = *slot;
3632 struct dwarf2_per_cu_data ***datap = datum;
3633
3634 **datap = &sigt->per_cu;
3635 ++*datap;
3636
3637 return 1;
3638 }
3639
3640 /* Create the hash table of all entries in the .debug_types section.
3641 DWO_FILE is a pointer to the DWO file for .debug_types.dwo, NULL otherwise.
3642 The result is a pointer to the hash table or NULL if there are
3643 no types. */
3644
3645 static htab_t
3646 create_debug_types_hash_table (struct dwo_file *dwo_file,
3647 VEC (dwarf2_section_info_def) *types)
3648 {
3649 struct objfile *objfile = dwarf2_per_objfile->objfile;
3650 htab_t types_htab = NULL;
3651 int ix;
3652 struct dwarf2_section_info *section;
3653 struct dwarf2_section_info *abbrev_section;
3654
3655 if (VEC_empty (dwarf2_section_info_def, types))
3656 return NULL;
3657
3658 abbrev_section = (dwo_file != NULL
3659 ? &dwo_file->sections.abbrev
3660 : &dwarf2_per_objfile->abbrev);
3661
3662 for (ix = 0;
3663 VEC_iterate (dwarf2_section_info_def, types, ix, section);
3664 ++ix)
3665 {
3666 bfd *abfd;
3667 gdb_byte *info_ptr, *end_ptr;
3668
3669 dwarf2_read_section (objfile, section);
3670 info_ptr = section->buffer;
3671
3672 if (info_ptr == NULL)
3673 continue;
3674
3675 /* We can't set abfd until now because the section may be empty or
3676 not present, in which case section->asection will be NULL. */
3677 abfd = section->asection->owner;
3678
3679 if (types_htab == NULL)
3680 {
3681 if (dwo_file)
3682 types_htab = allocate_dwo_unit_table (objfile);
3683 else
3684 types_htab = allocate_signatured_type_table (objfile);
3685 }
3686
3687 if (dwarf2_die_debug)
3688 fprintf_unfiltered (gdb_stdlog, "Reading signatured types for %s:\n",
3689 bfd_get_filename (abfd));
3690
3691 /* We don't use init_cutu_and_read_dies_simple, or some such, here
3692 because we don't need to read any dies: the signature is in the
3693 header. */
3694
3695 end_ptr = info_ptr + section->size;
3696 while (info_ptr < end_ptr)
3697 {
3698 sect_offset offset;
3699 cu_offset type_offset_in_tu;
3700 ULONGEST signature;
3701 struct signatured_type *sig_type;
3702 struct dwo_unit *dwo_tu;
3703 void **slot;
3704 gdb_byte *ptr = info_ptr;
3705 struct comp_unit_head header;
3706 unsigned int length;
3707
3708 offset.sect_off = ptr - section->buffer;
3709
3710 /* We need to read the type's signature in order to build the hash
3711 table, but we don't need anything else just yet. */
3712
3713 ptr = read_and_check_type_unit_head (&header, section,
3714 abbrev_section, ptr,
3715 &signature, &type_offset_in_tu);
3716
3717 length = header.initial_length_size + header.length;
3718
3719 /* Skip dummy type units. */
3720 if (ptr >= info_ptr + length
3721 || peek_abbrev_code (abfd, ptr) == 0)
3722 {
3723 info_ptr += header.initial_length_size + header.length;
3724 continue;
3725 }
3726
3727 if (dwo_file)
3728 {
3729 sig_type = NULL;
3730 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3731 struct dwo_unit);
3732 dwo_tu->dwo_file = dwo_file;
3733 dwo_tu->signature = signature;
3734 dwo_tu->type_offset_in_tu = type_offset_in_tu;
3735 dwo_tu->info_or_types_section = section;
3736 dwo_tu->offset = offset;
3737 dwo_tu->length = length;
3738 }
3739 else
3740 {
3741 /* N.B.: type_offset is not usable if this type uses a DWO file.
3742 The real type_offset is in the DWO file. */
3743 dwo_tu = NULL;
3744 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3745 struct signatured_type);
3746 sig_type->signature = signature;
3747 sig_type->type_offset_in_tu = type_offset_in_tu;
3748 sig_type->per_cu.objfile = objfile;
3749 sig_type->per_cu.is_debug_types = 1;
3750 sig_type->per_cu.info_or_types_section = section;
3751 sig_type->per_cu.offset = offset;
3752 sig_type->per_cu.length = length;
3753 }
3754
3755 slot = htab_find_slot (types_htab,
3756 dwo_file ? (void*) dwo_tu : (void *) sig_type,
3757 INSERT);
3758 gdb_assert (slot != NULL);
3759 if (*slot != NULL)
3760 {
3761 sect_offset dup_offset;
3762
3763 if (dwo_file)
3764 {
3765 const struct dwo_unit *dup_tu = *slot;
3766
3767 dup_offset = dup_tu->offset;
3768 }
3769 else
3770 {
3771 const struct signatured_type *dup_tu = *slot;
3772
3773 dup_offset = dup_tu->per_cu.offset;
3774 }
3775
3776 complaint (&symfile_complaints,
3777 _("debug type entry at offset 0x%x is duplicate to the "
3778 "entry at offset 0x%x, signature 0x%s"),
3779 offset.sect_off, dup_offset.sect_off,
3780 phex (signature, sizeof (signature)));
3781 }
3782 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3783
3784 if (dwarf2_die_debug)
3785 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3786 offset.sect_off,
3787 phex (signature, sizeof (signature)));
3788
3789 info_ptr += length;
3790 }
3791 }
3792
3793 return types_htab;
3794 }
3795
3796 /* Create the hash table of all entries in the .debug_types section,
3797 and initialize all_type_units.
3798 The result is zero if there is an error (e.g. missing .debug_types section),
3799 otherwise non-zero. */
3800
3801 static int
3802 create_all_type_units (struct objfile *objfile)
3803 {
3804 htab_t types_htab;
3805 struct dwarf2_per_cu_data **iter;
3806
3807 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
3808 if (types_htab == NULL)
3809 {
3810 dwarf2_per_objfile->signatured_types = NULL;
3811 return 0;
3812 }
3813
3814 dwarf2_per_objfile->signatured_types = types_htab;
3815
3816 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
3817 dwarf2_per_objfile->all_type_units
3818 = obstack_alloc (&objfile->objfile_obstack,
3819 dwarf2_per_objfile->n_type_units
3820 * sizeof (struct dwarf2_per_cu_data *));
3821 iter = &dwarf2_per_objfile->all_type_units[0];
3822 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
3823 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
3824 == dwarf2_per_objfile->n_type_units);
3825
3826 return 1;
3827 }
3828
3829 /* Lookup a signature based type for DW_FORM_ref_sig8.
3830 Returns NULL if signature SIG is not present in the table. */
3831
3832 static struct signatured_type *
3833 lookup_signatured_type (ULONGEST sig)
3834 {
3835 struct signatured_type find_entry, *entry;
3836
3837 if (dwarf2_per_objfile->signatured_types == NULL)
3838 {
3839 complaint (&symfile_complaints,
3840 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
3841 return NULL;
3842 }
3843
3844 find_entry.signature = sig;
3845 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3846 return entry;
3847 }
3848
3849 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3850
3851 static void
3852 init_cu_die_reader (struct die_reader_specs *reader,
3853 struct dwarf2_cu *cu,
3854 struct dwarf2_section_info *section,
3855 struct dwo_file *dwo_file)
3856 {
3857 gdb_assert (section->readin && section->buffer != NULL);
3858 reader->abfd = section->asection->owner;
3859 reader->cu = cu;
3860 reader->dwo_file = dwo_file;
3861 reader->die_section = section;
3862 reader->buffer = section->buffer;
3863 reader->buffer_end = section->buffer + section->size;
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 cu->abbrev_table =
11147 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
11148 }
11149
11150 /* Release the memory used by the abbrev table for a compilation unit. */
11151
11152 static void
11153 dwarf2_free_abbrev_table (void *ptr_to_cu)
11154 {
11155 struct dwarf2_cu *cu = ptr_to_cu;
11156
11157 abbrev_table_free (cu->abbrev_table);
11158 /* Set this to NULL so that we SEGV if we try to read it later,
11159 and also because free_comp_unit verifies this is NULL. */
11160 cu->abbrev_table = NULL;
11161 }
11162 \f
11163 /* Returns nonzero if TAG represents a type that we might generate a partial
11164 symbol for. */
11165
11166 static int
11167 is_type_tag_for_partial (int tag)
11168 {
11169 switch (tag)
11170 {
11171 #if 0
11172 /* Some types that would be reasonable to generate partial symbols for,
11173 that we don't at present. */
11174 case DW_TAG_array_type:
11175 case DW_TAG_file_type:
11176 case DW_TAG_ptr_to_member_type:
11177 case DW_TAG_set_type:
11178 case DW_TAG_string_type:
11179 case DW_TAG_subroutine_type:
11180 #endif
11181 case DW_TAG_base_type:
11182 case DW_TAG_class_type:
11183 case DW_TAG_interface_type:
11184 case DW_TAG_enumeration_type:
11185 case DW_TAG_structure_type:
11186 case DW_TAG_subrange_type:
11187 case DW_TAG_typedef:
11188 case DW_TAG_union_type:
11189 return 1;
11190 default:
11191 return 0;
11192 }
11193 }
11194
11195 /* Load all DIEs that are interesting for partial symbols into memory. */
11196
11197 static struct partial_die_info *
11198 load_partial_dies (const struct die_reader_specs *reader,
11199 gdb_byte *info_ptr, int building_psymtab)
11200 {
11201 struct dwarf2_cu *cu = reader->cu;
11202 struct objfile *objfile = cu->objfile;
11203 struct partial_die_info *part_die;
11204 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
11205 struct abbrev_info *abbrev;
11206 unsigned int bytes_read;
11207 unsigned int load_all = 0;
11208 int nesting_level = 1;
11209
11210 parent_die = NULL;
11211 last_die = NULL;
11212
11213 gdb_assert (cu->per_cu != NULL);
11214 if (cu->per_cu->load_all_dies)
11215 load_all = 1;
11216
11217 cu->partial_dies
11218 = htab_create_alloc_ex (cu->header.length / 12,
11219 partial_die_hash,
11220 partial_die_eq,
11221 NULL,
11222 &cu->comp_unit_obstack,
11223 hashtab_obstack_allocate,
11224 dummy_obstack_deallocate);
11225
11226 part_die = obstack_alloc (&cu->comp_unit_obstack,
11227 sizeof (struct partial_die_info));
11228
11229 while (1)
11230 {
11231 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
11232
11233 /* A NULL abbrev means the end of a series of children. */
11234 if (abbrev == NULL)
11235 {
11236 if (--nesting_level == 0)
11237 {
11238 /* PART_DIE was probably the last thing allocated on the
11239 comp_unit_obstack, so we could call obstack_free
11240 here. We don't do that because the waste is small,
11241 and will be cleaned up when we're done with this
11242 compilation unit. This way, we're also more robust
11243 against other users of the comp_unit_obstack. */
11244 return first_die;
11245 }
11246 info_ptr += bytes_read;
11247 last_die = parent_die;
11248 parent_die = parent_die->die_parent;
11249 continue;
11250 }
11251
11252 /* Check for template arguments. We never save these; if
11253 they're seen, we just mark the parent, and go on our way. */
11254 if (parent_die != NULL
11255 && cu->language == language_cplus
11256 && (abbrev->tag == DW_TAG_template_type_param
11257 || abbrev->tag == DW_TAG_template_value_param))
11258 {
11259 parent_die->has_template_arguments = 1;
11260
11261 if (!load_all)
11262 {
11263 /* We don't need a partial DIE for the template argument. */
11264 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
11265 continue;
11266 }
11267 }
11268
11269 /* We only recurse into c++ subprograms looking for template arguments.
11270 Skip their other children. */
11271 if (!load_all
11272 && cu->language == language_cplus
11273 && parent_die != NULL
11274 && parent_die->tag == DW_TAG_subprogram)
11275 {
11276 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
11277 continue;
11278 }
11279
11280 /* Check whether this DIE is interesting enough to save. Normally
11281 we would not be interested in members here, but there may be
11282 later variables referencing them via DW_AT_specification (for
11283 static members). */
11284 if (!load_all
11285 && !is_type_tag_for_partial (abbrev->tag)
11286 && abbrev->tag != DW_TAG_constant
11287 && abbrev->tag != DW_TAG_enumerator
11288 && abbrev->tag != DW_TAG_subprogram
11289 && abbrev->tag != DW_TAG_lexical_block
11290 && abbrev->tag != DW_TAG_variable
11291 && abbrev->tag != DW_TAG_namespace
11292 && abbrev->tag != DW_TAG_module
11293 && abbrev->tag != DW_TAG_member
11294 && abbrev->tag != DW_TAG_imported_unit)
11295 {
11296 /* Otherwise we skip to the next sibling, if any. */
11297 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
11298 continue;
11299 }
11300
11301 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
11302 info_ptr);
11303
11304 /* This two-pass algorithm for processing partial symbols has a
11305 high cost in cache pressure. Thus, handle some simple cases
11306 here which cover the majority of C partial symbols. DIEs
11307 which neither have specification tags in them, nor could have
11308 specification tags elsewhere pointing at them, can simply be
11309 processed and discarded.
11310
11311 This segment is also optional; scan_partial_symbols and
11312 add_partial_symbol will handle these DIEs if we chain
11313 them in normally. When compilers which do not emit large
11314 quantities of duplicate debug information are more common,
11315 this code can probably be removed. */
11316
11317 /* Any complete simple types at the top level (pretty much all
11318 of them, for a language without namespaces), can be processed
11319 directly. */
11320 if (parent_die == NULL
11321 && part_die->has_specification == 0
11322 && part_die->is_declaration == 0
11323 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
11324 || part_die->tag == DW_TAG_base_type
11325 || part_die->tag == DW_TAG_subrange_type))
11326 {
11327 if (building_psymtab && part_die->name != NULL)
11328 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
11329 VAR_DOMAIN, LOC_TYPEDEF,
11330 &objfile->static_psymbols,
11331 0, (CORE_ADDR) 0, cu->language, objfile);
11332 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
11333 continue;
11334 }
11335
11336 /* The exception for DW_TAG_typedef with has_children above is
11337 a workaround of GCC PR debug/47510. In the case of this complaint
11338 type_name_no_tag_or_error will error on such types later.
11339
11340 GDB skipped children of DW_TAG_typedef by the shortcut above and then
11341 it could not find the child DIEs referenced later, this is checked
11342 above. In correct DWARF DW_TAG_typedef should have no children. */
11343
11344 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
11345 complaint (&symfile_complaints,
11346 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
11347 "- DIE at 0x%x [in module %s]"),
11348 part_die->offset.sect_off, objfile->name);
11349
11350 /* If we're at the second level, and we're an enumerator, and
11351 our parent has no specification (meaning possibly lives in a
11352 namespace elsewhere), then we can add the partial symbol now
11353 instead of queueing it. */
11354 if (part_die->tag == DW_TAG_enumerator
11355 && parent_die != NULL
11356 && parent_die->die_parent == NULL
11357 && parent_die->tag == DW_TAG_enumeration_type
11358 && parent_die->has_specification == 0)
11359 {
11360 if (part_die->name == NULL)
11361 complaint (&symfile_complaints,
11362 _("malformed enumerator DIE ignored"));
11363 else if (building_psymtab)
11364 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
11365 VAR_DOMAIN, LOC_CONST,
11366 (cu->language == language_cplus
11367 || cu->language == language_java)
11368 ? &objfile->global_psymbols
11369 : &objfile->static_psymbols,
11370 0, (CORE_ADDR) 0, cu->language, objfile);
11371
11372 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
11373 continue;
11374 }
11375
11376 /* We'll save this DIE so link it in. */
11377 part_die->die_parent = parent_die;
11378 part_die->die_sibling = NULL;
11379 part_die->die_child = NULL;
11380
11381 if (last_die && last_die == parent_die)
11382 last_die->die_child = part_die;
11383 else if (last_die)
11384 last_die->die_sibling = part_die;
11385
11386 last_die = part_die;
11387
11388 if (first_die == NULL)
11389 first_die = part_die;
11390
11391 /* Maybe add the DIE to the hash table. Not all DIEs that we
11392 find interesting need to be in the hash table, because we
11393 also have the parent/sibling/child chains; only those that we
11394 might refer to by offset later during partial symbol reading.
11395
11396 For now this means things that might have be the target of a
11397 DW_AT_specification, DW_AT_abstract_origin, or
11398 DW_AT_extension. DW_AT_extension will refer only to
11399 namespaces; DW_AT_abstract_origin refers to functions (and
11400 many things under the function DIE, but we do not recurse
11401 into function DIEs during partial symbol reading) and
11402 possibly variables as well; DW_AT_specification refers to
11403 declarations. Declarations ought to have the DW_AT_declaration
11404 flag. It happens that GCC forgets to put it in sometimes, but
11405 only for functions, not for types.
11406
11407 Adding more things than necessary to the hash table is harmless
11408 except for the performance cost. Adding too few will result in
11409 wasted time in find_partial_die, when we reread the compilation
11410 unit with load_all_dies set. */
11411
11412 if (load_all
11413 || abbrev->tag == DW_TAG_constant
11414 || abbrev->tag == DW_TAG_subprogram
11415 || abbrev->tag == DW_TAG_variable
11416 || abbrev->tag == DW_TAG_namespace
11417 || part_die->is_declaration)
11418 {
11419 void **slot;
11420
11421 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
11422 part_die->offset.sect_off, INSERT);
11423 *slot = part_die;
11424 }
11425
11426 part_die = obstack_alloc (&cu->comp_unit_obstack,
11427 sizeof (struct partial_die_info));
11428
11429 /* For some DIEs we want to follow their children (if any). For C
11430 we have no reason to follow the children of structures; for other
11431 languages we have to, so that we can get at method physnames
11432 to infer fully qualified class names, for DW_AT_specification,
11433 and for C++ template arguments. For C++, we also look one level
11434 inside functions to find template arguments (if the name of the
11435 function does not already contain the template arguments).
11436
11437 For Ada, we need to scan the children of subprograms and lexical
11438 blocks as well because Ada allows the definition of nested
11439 entities that could be interesting for the debugger, such as
11440 nested subprograms for instance. */
11441 if (last_die->has_children
11442 && (load_all
11443 || last_die->tag == DW_TAG_namespace
11444 || last_die->tag == DW_TAG_module
11445 || last_die->tag == DW_TAG_enumeration_type
11446 || (cu->language == language_cplus
11447 && last_die->tag == DW_TAG_subprogram
11448 && (last_die->name == NULL
11449 || strchr (last_die->name, '<') == NULL))
11450 || (cu->language != language_c
11451 && (last_die->tag == DW_TAG_class_type
11452 || last_die->tag == DW_TAG_interface_type
11453 || last_die->tag == DW_TAG_structure_type
11454 || last_die->tag == DW_TAG_union_type))
11455 || (cu->language == language_ada
11456 && (last_die->tag == DW_TAG_subprogram
11457 || last_die->tag == DW_TAG_lexical_block))))
11458 {
11459 nesting_level++;
11460 parent_die = last_die;
11461 continue;
11462 }
11463
11464 /* Otherwise we skip to the next sibling, if any. */
11465 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
11466
11467 /* Back to the top, do it again. */
11468 }
11469 }
11470
11471 /* Read a minimal amount of information into the minimal die structure. */
11472
11473 static gdb_byte *
11474 read_partial_die (const struct die_reader_specs *reader,
11475 struct partial_die_info *part_die,
11476 struct abbrev_info *abbrev, unsigned int abbrev_len,
11477 gdb_byte *info_ptr)
11478 {
11479 struct dwarf2_cu *cu = reader->cu;
11480 struct objfile *objfile = cu->objfile;
11481 gdb_byte *buffer = reader->buffer;
11482 unsigned int i;
11483 struct attribute attr;
11484 int has_low_pc_attr = 0;
11485 int has_high_pc_attr = 0;
11486 int high_pc_relative = 0;
11487
11488 memset (part_die, 0, sizeof (struct partial_die_info));
11489
11490 part_die->offset.sect_off = info_ptr - buffer;
11491
11492 info_ptr += abbrev_len;
11493
11494 if (abbrev == NULL)
11495 return info_ptr;
11496
11497 part_die->tag = abbrev->tag;
11498 part_die->has_children = abbrev->has_children;
11499
11500 for (i = 0; i < abbrev->num_attrs; ++i)
11501 {
11502 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
11503
11504 /* Store the data if it is of an attribute we want to keep in a
11505 partial symbol table. */
11506 switch (attr.name)
11507 {
11508 case DW_AT_name:
11509 switch (part_die->tag)
11510 {
11511 case DW_TAG_compile_unit:
11512 case DW_TAG_partial_unit:
11513 case DW_TAG_type_unit:
11514 /* Compilation units have a DW_AT_name that is a filename, not
11515 a source language identifier. */
11516 case DW_TAG_enumeration_type:
11517 case DW_TAG_enumerator:
11518 /* These tags always have simple identifiers already; no need
11519 to canonicalize them. */
11520 part_die->name = DW_STRING (&attr);
11521 break;
11522 default:
11523 part_die->name
11524 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
11525 &objfile->objfile_obstack);
11526 break;
11527 }
11528 break;
11529 case DW_AT_linkage_name:
11530 case DW_AT_MIPS_linkage_name:
11531 /* Note that both forms of linkage name might appear. We
11532 assume they will be the same, and we only store the last
11533 one we see. */
11534 if (cu->language == language_ada)
11535 part_die->name = DW_STRING (&attr);
11536 part_die->linkage_name = DW_STRING (&attr);
11537 break;
11538 case DW_AT_low_pc:
11539 has_low_pc_attr = 1;
11540 part_die->lowpc = DW_ADDR (&attr);
11541 break;
11542 case DW_AT_high_pc:
11543 has_high_pc_attr = 1;
11544 if (attr.form == DW_FORM_addr
11545 || attr.form == DW_FORM_GNU_addr_index)
11546 part_die->highpc = DW_ADDR (&attr);
11547 else
11548 {
11549 high_pc_relative = 1;
11550 part_die->highpc = DW_UNSND (&attr);
11551 }
11552 break;
11553 case DW_AT_location:
11554 /* Support the .debug_loc offsets. */
11555 if (attr_form_is_block (&attr))
11556 {
11557 part_die->d.locdesc = DW_BLOCK (&attr);
11558 }
11559 else if (attr_form_is_section_offset (&attr))
11560 {
11561 dwarf2_complex_location_expr_complaint ();
11562 }
11563 else
11564 {
11565 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
11566 "partial symbol information");
11567 }
11568 break;
11569 case DW_AT_external:
11570 part_die->is_external = DW_UNSND (&attr);
11571 break;
11572 case DW_AT_declaration:
11573 part_die->is_declaration = DW_UNSND (&attr);
11574 break;
11575 case DW_AT_type:
11576 part_die->has_type = 1;
11577 break;
11578 case DW_AT_abstract_origin:
11579 case DW_AT_specification:
11580 case DW_AT_extension:
11581 part_die->has_specification = 1;
11582 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
11583 break;
11584 case DW_AT_sibling:
11585 /* Ignore absolute siblings, they might point outside of
11586 the current compile unit. */
11587 if (attr.form == DW_FORM_ref_addr)
11588 complaint (&symfile_complaints,
11589 _("ignoring absolute DW_AT_sibling"));
11590 else
11591 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
11592 break;
11593 case DW_AT_byte_size:
11594 part_die->has_byte_size = 1;
11595 break;
11596 case DW_AT_calling_convention:
11597 /* DWARF doesn't provide a way to identify a program's source-level
11598 entry point. DW_AT_calling_convention attributes are only meant
11599 to describe functions' calling conventions.
11600
11601 However, because it's a necessary piece of information in
11602 Fortran, and because DW_CC_program is the only piece of debugging
11603 information whose definition refers to a 'main program' at all,
11604 several compilers have begun marking Fortran main programs with
11605 DW_CC_program --- even when those functions use the standard
11606 calling conventions.
11607
11608 So until DWARF specifies a way to provide this information and
11609 compilers pick up the new representation, we'll support this
11610 practice. */
11611 if (DW_UNSND (&attr) == DW_CC_program
11612 && cu->language == language_fortran)
11613 {
11614 set_main_name (part_die->name);
11615
11616 /* As this DIE has a static linkage the name would be difficult
11617 to look up later. */
11618 language_of_main = language_fortran;
11619 }
11620 break;
11621 case DW_AT_inline:
11622 if (DW_UNSND (&attr) == DW_INL_inlined
11623 || DW_UNSND (&attr) == DW_INL_declared_inlined)
11624 part_die->may_be_inlined = 1;
11625 break;
11626
11627 case DW_AT_import:
11628 if (part_die->tag == DW_TAG_imported_unit)
11629 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
11630 break;
11631
11632 default:
11633 break;
11634 }
11635 }
11636
11637 if (high_pc_relative)
11638 part_die->highpc += part_die->lowpc;
11639
11640 if (has_low_pc_attr && has_high_pc_attr)
11641 {
11642 /* When using the GNU linker, .gnu.linkonce. sections are used to
11643 eliminate duplicate copies of functions and vtables and such.
11644 The linker will arbitrarily choose one and discard the others.
11645 The AT_*_pc values for such functions refer to local labels in
11646 these sections. If the section from that file was discarded, the
11647 labels are not in the output, so the relocs get a value of 0.
11648 If this is a discarded function, mark the pc bounds as invalid,
11649 so that GDB will ignore it. */
11650 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
11651 {
11652 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11653
11654 complaint (&symfile_complaints,
11655 _("DW_AT_low_pc %s is zero "
11656 "for DIE at 0x%x [in module %s]"),
11657 paddress (gdbarch, part_die->lowpc),
11658 part_die->offset.sect_off, objfile->name);
11659 }
11660 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
11661 else if (part_die->lowpc >= part_die->highpc)
11662 {
11663 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11664
11665 complaint (&symfile_complaints,
11666 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
11667 "for DIE at 0x%x [in module %s]"),
11668 paddress (gdbarch, part_die->lowpc),
11669 paddress (gdbarch, part_die->highpc),
11670 part_die->offset.sect_off, objfile->name);
11671 }
11672 else
11673 part_die->has_pc_info = 1;
11674 }
11675
11676 return info_ptr;
11677 }
11678
11679 /* Find a cached partial DIE at OFFSET in CU. */
11680
11681 static struct partial_die_info *
11682 find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
11683 {
11684 struct partial_die_info *lookup_die = NULL;
11685 struct partial_die_info part_die;
11686
11687 part_die.offset = offset;
11688 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
11689 offset.sect_off);
11690
11691 return lookup_die;
11692 }
11693
11694 /* Find a partial DIE at OFFSET, which may or may not be in CU,
11695 except in the case of .debug_types DIEs which do not reference
11696 outside their CU (they do however referencing other types via
11697 DW_FORM_ref_sig8). */
11698
11699 static struct partial_die_info *
11700 find_partial_die (sect_offset offset, struct dwarf2_cu *cu)
11701 {
11702 struct objfile *objfile = cu->objfile;
11703 struct dwarf2_per_cu_data *per_cu = NULL;
11704 struct partial_die_info *pd = NULL;
11705
11706 if (offset_in_cu_p (&cu->header, offset))
11707 {
11708 pd = find_partial_die_in_comp_unit (offset, cu);
11709 if (pd != NULL)
11710 return pd;
11711 /* We missed recording what we needed.
11712 Load all dies and try again. */
11713 per_cu = cu->per_cu;
11714 }
11715 else
11716 {
11717 /* TUs don't reference other CUs/TUs (except via type signatures). */
11718 if (cu->per_cu->is_debug_types)
11719 {
11720 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
11721 " external reference to offset 0x%lx [in module %s].\n"),
11722 (long) cu->header.offset.sect_off, (long) offset.sect_off,
11723 bfd_get_filename (objfile->obfd));
11724 }
11725 per_cu = dwarf2_find_containing_comp_unit (offset, objfile);
11726
11727 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
11728 load_partial_comp_unit (per_cu);
11729
11730 per_cu->cu->last_used = 0;
11731 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
11732 }
11733
11734 /* If we didn't find it, and not all dies have been loaded,
11735 load them all and try again. */
11736
11737 if (pd == NULL && per_cu->load_all_dies == 0)
11738 {
11739 per_cu->load_all_dies = 1;
11740
11741 /* This is nasty. When we reread the DIEs, somewhere up the call chain
11742 THIS_CU->cu may already be in use. So we can't just free it and
11743 replace its DIEs with the ones we read in. Instead, we leave those
11744 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
11745 and clobber THIS_CU->cu->partial_dies with the hash table for the new
11746 set. */
11747 load_partial_comp_unit (per_cu);
11748
11749 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
11750 }
11751
11752 if (pd == NULL)
11753 internal_error (__FILE__, __LINE__,
11754 _("could not find partial DIE 0x%x "
11755 "in cache [from module %s]\n"),
11756 offset.sect_off, bfd_get_filename (objfile->obfd));
11757 return pd;
11758 }
11759
11760 /* See if we can figure out if the class lives in a namespace. We do
11761 this by looking for a member function; its demangled name will
11762 contain namespace info, if there is any. */
11763
11764 static void
11765 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
11766 struct dwarf2_cu *cu)
11767 {
11768 /* NOTE: carlton/2003-10-07: Getting the info this way changes
11769 what template types look like, because the demangler
11770 frequently doesn't give the same name as the debug info. We
11771 could fix this by only using the demangled name to get the
11772 prefix (but see comment in read_structure_type). */
11773
11774 struct partial_die_info *real_pdi;
11775 struct partial_die_info *child_pdi;
11776
11777 /* If this DIE (this DIE's specification, if any) has a parent, then
11778 we should not do this. We'll prepend the parent's fully qualified
11779 name when we create the partial symbol. */
11780
11781 real_pdi = struct_pdi;
11782 while (real_pdi->has_specification)
11783 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
11784
11785 if (real_pdi->die_parent != NULL)
11786 return;
11787
11788 for (child_pdi = struct_pdi->die_child;
11789 child_pdi != NULL;
11790 child_pdi = child_pdi->die_sibling)
11791 {
11792 if (child_pdi->tag == DW_TAG_subprogram
11793 && child_pdi->linkage_name != NULL)
11794 {
11795 char *actual_class_name
11796 = language_class_name_from_physname (cu->language_defn,
11797 child_pdi->linkage_name);
11798 if (actual_class_name != NULL)
11799 {
11800 struct_pdi->name
11801 = obsavestring (actual_class_name,
11802 strlen (actual_class_name),
11803 &cu->objfile->objfile_obstack);
11804 xfree (actual_class_name);
11805 }
11806 break;
11807 }
11808 }
11809 }
11810
11811 /* Adjust PART_DIE before generating a symbol for it. This function
11812 may set the is_external flag or change the DIE's name. */
11813
11814 static void
11815 fixup_partial_die (struct partial_die_info *part_die,
11816 struct dwarf2_cu *cu)
11817 {
11818 /* Once we've fixed up a die, there's no point in doing so again.
11819 This also avoids a memory leak if we were to call
11820 guess_partial_die_structure_name multiple times. */
11821 if (part_die->fixup_called)
11822 return;
11823
11824 /* If we found a reference attribute and the DIE has no name, try
11825 to find a name in the referred to DIE. */
11826
11827 if (part_die->name == NULL && part_die->has_specification)
11828 {
11829 struct partial_die_info *spec_die;
11830
11831 spec_die = find_partial_die (part_die->spec_offset, cu);
11832
11833 fixup_partial_die (spec_die, cu);
11834
11835 if (spec_die->name)
11836 {
11837 part_die->name = spec_die->name;
11838
11839 /* Copy DW_AT_external attribute if it is set. */
11840 if (spec_die->is_external)
11841 part_die->is_external = spec_die->is_external;
11842 }
11843 }
11844
11845 /* Set default names for some unnamed DIEs. */
11846
11847 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
11848 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
11849
11850 /* If there is no parent die to provide a namespace, and there are
11851 children, see if we can determine the namespace from their linkage
11852 name. */
11853 if (cu->language == language_cplus
11854 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
11855 && part_die->die_parent == NULL
11856 && part_die->has_children
11857 && (part_die->tag == DW_TAG_class_type
11858 || part_die->tag == DW_TAG_structure_type
11859 || part_die->tag == DW_TAG_union_type))
11860 guess_partial_die_structure_name (part_die, cu);
11861
11862 /* GCC might emit a nameless struct or union that has a linkage
11863 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
11864 if (part_die->name == NULL
11865 && (part_die->tag == DW_TAG_class_type
11866 || part_die->tag == DW_TAG_interface_type
11867 || part_die->tag == DW_TAG_structure_type
11868 || part_die->tag == DW_TAG_union_type)
11869 && part_die->linkage_name != NULL)
11870 {
11871 char *demangled;
11872
11873 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
11874 if (demangled)
11875 {
11876 const char *base;
11877
11878 /* Strip any leading namespaces/classes, keep only the base name.
11879 DW_AT_name for named DIEs does not contain the prefixes. */
11880 base = strrchr (demangled, ':');
11881 if (base && base > demangled && base[-1] == ':')
11882 base++;
11883 else
11884 base = demangled;
11885
11886 part_die->name = obsavestring (base, strlen (base),
11887 &cu->objfile->objfile_obstack);
11888 xfree (demangled);
11889 }
11890 }
11891
11892 part_die->fixup_called = 1;
11893 }
11894
11895 /* Read an attribute value described by an attribute form. */
11896
11897 static gdb_byte *
11898 read_attribute_value (const struct die_reader_specs *reader,
11899 struct attribute *attr, unsigned form,
11900 gdb_byte *info_ptr)
11901 {
11902 struct dwarf2_cu *cu = reader->cu;
11903 bfd *abfd = reader->abfd;
11904 struct comp_unit_head *cu_header = &cu->header;
11905 unsigned int bytes_read;
11906 struct dwarf_block *blk;
11907
11908 attr->form = form;
11909 switch (form)
11910 {
11911 case DW_FORM_ref_addr:
11912 if (cu->header.version == 2)
11913 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
11914 else
11915 DW_UNSND (attr) = read_offset (abfd, info_ptr,
11916 &cu->header, &bytes_read);
11917 info_ptr += bytes_read;
11918 break;
11919 case DW_FORM_addr:
11920 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
11921 info_ptr += bytes_read;
11922 break;
11923 case DW_FORM_block2:
11924 blk = dwarf_alloc_block (cu);
11925 blk->size = read_2_bytes (abfd, info_ptr);
11926 info_ptr += 2;
11927 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11928 info_ptr += blk->size;
11929 DW_BLOCK (attr) = blk;
11930 break;
11931 case DW_FORM_block4:
11932 blk = dwarf_alloc_block (cu);
11933 blk->size = read_4_bytes (abfd, info_ptr);
11934 info_ptr += 4;
11935 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11936 info_ptr += blk->size;
11937 DW_BLOCK (attr) = blk;
11938 break;
11939 case DW_FORM_data2:
11940 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
11941 info_ptr += 2;
11942 break;
11943 case DW_FORM_data4:
11944 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
11945 info_ptr += 4;
11946 break;
11947 case DW_FORM_data8:
11948 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
11949 info_ptr += 8;
11950 break;
11951 case DW_FORM_sec_offset:
11952 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
11953 info_ptr += bytes_read;
11954 break;
11955 case DW_FORM_string:
11956 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
11957 DW_STRING_IS_CANONICAL (attr) = 0;
11958 info_ptr += bytes_read;
11959 break;
11960 case DW_FORM_strp:
11961 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
11962 &bytes_read);
11963 DW_STRING_IS_CANONICAL (attr) = 0;
11964 info_ptr += bytes_read;
11965 break;
11966 case DW_FORM_exprloc:
11967 case DW_FORM_block:
11968 blk = dwarf_alloc_block (cu);
11969 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11970 info_ptr += bytes_read;
11971 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11972 info_ptr += blk->size;
11973 DW_BLOCK (attr) = blk;
11974 break;
11975 case DW_FORM_block1:
11976 blk = dwarf_alloc_block (cu);
11977 blk->size = read_1_byte (abfd, info_ptr);
11978 info_ptr += 1;
11979 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11980 info_ptr += blk->size;
11981 DW_BLOCK (attr) = blk;
11982 break;
11983 case DW_FORM_data1:
11984 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
11985 info_ptr += 1;
11986 break;
11987 case DW_FORM_flag:
11988 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
11989 info_ptr += 1;
11990 break;
11991 case DW_FORM_flag_present:
11992 DW_UNSND (attr) = 1;
11993 break;
11994 case DW_FORM_sdata:
11995 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
11996 info_ptr += bytes_read;
11997 break;
11998 case DW_FORM_udata:
11999 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
12000 info_ptr += bytes_read;
12001 break;
12002 case DW_FORM_ref1:
12003 DW_UNSND (attr) = (cu->header.offset.sect_off
12004 + read_1_byte (abfd, info_ptr));
12005 info_ptr += 1;
12006 break;
12007 case DW_FORM_ref2:
12008 DW_UNSND (attr) = (cu->header.offset.sect_off
12009 + read_2_bytes (abfd, info_ptr));
12010 info_ptr += 2;
12011 break;
12012 case DW_FORM_ref4:
12013 DW_UNSND (attr) = (cu->header.offset.sect_off
12014 + read_4_bytes (abfd, info_ptr));
12015 info_ptr += 4;
12016 break;
12017 case DW_FORM_ref8:
12018 DW_UNSND (attr) = (cu->header.offset.sect_off
12019 + read_8_bytes (abfd, info_ptr));
12020 info_ptr += 8;
12021 break;
12022 case DW_FORM_ref_sig8:
12023 /* Convert the signature to something we can record in DW_UNSND
12024 for later lookup.
12025 NOTE: This is NULL if the type wasn't found. */
12026 DW_SIGNATURED_TYPE (attr) =
12027 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
12028 info_ptr += 8;
12029 break;
12030 case DW_FORM_ref_udata:
12031 DW_UNSND (attr) = (cu->header.offset.sect_off
12032 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
12033 info_ptr += bytes_read;
12034 break;
12035 case DW_FORM_indirect:
12036 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
12037 info_ptr += bytes_read;
12038 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
12039 break;
12040 case DW_FORM_GNU_addr_index:
12041 if (reader->dwo_file == NULL)
12042 {
12043 /* For now flag a hard error.
12044 Later we can turn this into a complaint. */
12045 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
12046 dwarf_form_name (form),
12047 bfd_get_filename (abfd));
12048 }
12049 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
12050 info_ptr += bytes_read;
12051 break;
12052 case DW_FORM_GNU_str_index:
12053 if (reader->dwo_file == NULL)
12054 {
12055 /* For now flag a hard error.
12056 Later we can turn this into a complaint if warranted. */
12057 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
12058 dwarf_form_name (form),
12059 bfd_get_filename (abfd));
12060 }
12061 {
12062 ULONGEST str_index =
12063 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
12064
12065 DW_STRING (attr) = read_str_index (reader, cu, str_index);
12066 DW_STRING_IS_CANONICAL (attr) = 0;
12067 info_ptr += bytes_read;
12068 }
12069 break;
12070 default:
12071 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
12072 dwarf_form_name (form),
12073 bfd_get_filename (abfd));
12074 }
12075
12076 /* We have seen instances where the compiler tried to emit a byte
12077 size attribute of -1 which ended up being encoded as an unsigned
12078 0xffffffff. Although 0xffffffff is technically a valid size value,
12079 an object of this size seems pretty unlikely so we can relatively
12080 safely treat these cases as if the size attribute was invalid and
12081 treat them as zero by default. */
12082 if (attr->name == DW_AT_byte_size
12083 && form == DW_FORM_data4
12084 && DW_UNSND (attr) >= 0xffffffff)
12085 {
12086 complaint
12087 (&symfile_complaints,
12088 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
12089 hex_string (DW_UNSND (attr)));
12090 DW_UNSND (attr) = 0;
12091 }
12092
12093 return info_ptr;
12094 }
12095
12096 /* Read an attribute described by an abbreviated attribute. */
12097
12098 static gdb_byte *
12099 read_attribute (const struct die_reader_specs *reader,
12100 struct attribute *attr, struct attr_abbrev *abbrev,
12101 gdb_byte *info_ptr)
12102 {
12103 attr->name = abbrev->name;
12104 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
12105 }
12106
12107 /* Read dwarf information from a buffer. */
12108
12109 static unsigned int
12110 read_1_byte (bfd *abfd, gdb_byte *buf)
12111 {
12112 return bfd_get_8 (abfd, buf);
12113 }
12114
12115 static int
12116 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
12117 {
12118 return bfd_get_signed_8 (abfd, buf);
12119 }
12120
12121 static unsigned int
12122 read_2_bytes (bfd *abfd, gdb_byte *buf)
12123 {
12124 return bfd_get_16 (abfd, buf);
12125 }
12126
12127 static int
12128 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
12129 {
12130 return bfd_get_signed_16 (abfd, buf);
12131 }
12132
12133 static unsigned int
12134 read_4_bytes (bfd *abfd, gdb_byte *buf)
12135 {
12136 return bfd_get_32 (abfd, buf);
12137 }
12138
12139 static int
12140 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
12141 {
12142 return bfd_get_signed_32 (abfd, buf);
12143 }
12144
12145 static ULONGEST
12146 read_8_bytes (bfd *abfd, gdb_byte *buf)
12147 {
12148 return bfd_get_64 (abfd, buf);
12149 }
12150
12151 static CORE_ADDR
12152 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
12153 unsigned int *bytes_read)
12154 {
12155 struct comp_unit_head *cu_header = &cu->header;
12156 CORE_ADDR retval = 0;
12157
12158 if (cu_header->signed_addr_p)
12159 {
12160 switch (cu_header->addr_size)
12161 {
12162 case 2:
12163 retval = bfd_get_signed_16 (abfd, buf);
12164 break;
12165 case 4:
12166 retval = bfd_get_signed_32 (abfd, buf);
12167 break;
12168 case 8:
12169 retval = bfd_get_signed_64 (abfd, buf);
12170 break;
12171 default:
12172 internal_error (__FILE__, __LINE__,
12173 _("read_address: bad switch, signed [in module %s]"),
12174 bfd_get_filename (abfd));
12175 }
12176 }
12177 else
12178 {
12179 switch (cu_header->addr_size)
12180 {
12181 case 2:
12182 retval = bfd_get_16 (abfd, buf);
12183 break;
12184 case 4:
12185 retval = bfd_get_32 (abfd, buf);
12186 break;
12187 case 8:
12188 retval = bfd_get_64 (abfd, buf);
12189 break;
12190 default:
12191 internal_error (__FILE__, __LINE__,
12192 _("read_address: bad switch, "
12193 "unsigned [in module %s]"),
12194 bfd_get_filename (abfd));
12195 }
12196 }
12197
12198 *bytes_read = cu_header->addr_size;
12199 return retval;
12200 }
12201
12202 /* Read the initial length from a section. The (draft) DWARF 3
12203 specification allows the initial length to take up either 4 bytes
12204 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
12205 bytes describe the length and all offsets will be 8 bytes in length
12206 instead of 4.
12207
12208 An older, non-standard 64-bit format is also handled by this
12209 function. The older format in question stores the initial length
12210 as an 8-byte quantity without an escape value. Lengths greater
12211 than 2^32 aren't very common which means that the initial 4 bytes
12212 is almost always zero. Since a length value of zero doesn't make
12213 sense for the 32-bit format, this initial zero can be considered to
12214 be an escape value which indicates the presence of the older 64-bit
12215 format. As written, the code can't detect (old format) lengths
12216 greater than 4GB. If it becomes necessary to handle lengths
12217 somewhat larger than 4GB, we could allow other small values (such
12218 as the non-sensical values of 1, 2, and 3) to also be used as
12219 escape values indicating the presence of the old format.
12220
12221 The value returned via bytes_read should be used to increment the
12222 relevant pointer after calling read_initial_length().
12223
12224 [ Note: read_initial_length() and read_offset() are based on the
12225 document entitled "DWARF Debugging Information Format", revision
12226 3, draft 8, dated November 19, 2001. This document was obtained
12227 from:
12228
12229 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
12230
12231 This document is only a draft and is subject to change. (So beware.)
12232
12233 Details regarding the older, non-standard 64-bit format were
12234 determined empirically by examining 64-bit ELF files produced by
12235 the SGI toolchain on an IRIX 6.5 machine.
12236
12237 - Kevin, July 16, 2002
12238 ] */
12239
12240 static LONGEST
12241 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
12242 {
12243 LONGEST length = bfd_get_32 (abfd, buf);
12244
12245 if (length == 0xffffffff)
12246 {
12247 length = bfd_get_64 (abfd, buf + 4);
12248 *bytes_read = 12;
12249 }
12250 else if (length == 0)
12251 {
12252 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
12253 length = bfd_get_64 (abfd, buf);
12254 *bytes_read = 8;
12255 }
12256 else
12257 {
12258 *bytes_read = 4;
12259 }
12260
12261 return length;
12262 }
12263
12264 /* Cover function for read_initial_length.
12265 Returns the length of the object at BUF, and stores the size of the
12266 initial length in *BYTES_READ and stores the size that offsets will be in
12267 *OFFSET_SIZE.
12268 If the initial length size is not equivalent to that specified in
12269 CU_HEADER then issue a complaint.
12270 This is useful when reading non-comp-unit headers. */
12271
12272 static LONGEST
12273 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
12274 const struct comp_unit_head *cu_header,
12275 unsigned int *bytes_read,
12276 unsigned int *offset_size)
12277 {
12278 LONGEST length = read_initial_length (abfd, buf, bytes_read);
12279
12280 gdb_assert (cu_header->initial_length_size == 4
12281 || cu_header->initial_length_size == 8
12282 || cu_header->initial_length_size == 12);
12283
12284 if (cu_header->initial_length_size != *bytes_read)
12285 complaint (&symfile_complaints,
12286 _("intermixed 32-bit and 64-bit DWARF sections"));
12287
12288 *offset_size = (*bytes_read == 4) ? 4 : 8;
12289 return length;
12290 }
12291
12292 /* Read an offset from the data stream. The size of the offset is
12293 given by cu_header->offset_size. */
12294
12295 static LONGEST
12296 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
12297 unsigned int *bytes_read)
12298 {
12299 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
12300
12301 *bytes_read = cu_header->offset_size;
12302 return offset;
12303 }
12304
12305 /* Read an offset from the data stream. */
12306
12307 static LONGEST
12308 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
12309 {
12310 LONGEST retval = 0;
12311
12312 switch (offset_size)
12313 {
12314 case 4:
12315 retval = bfd_get_32 (abfd, buf);
12316 break;
12317 case 8:
12318 retval = bfd_get_64 (abfd, buf);
12319 break;
12320 default:
12321 internal_error (__FILE__, __LINE__,
12322 _("read_offset_1: bad switch [in module %s]"),
12323 bfd_get_filename (abfd));
12324 }
12325
12326 return retval;
12327 }
12328
12329 static gdb_byte *
12330 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
12331 {
12332 /* If the size of a host char is 8 bits, we can return a pointer
12333 to the buffer, otherwise we have to copy the data to a buffer
12334 allocated on the temporary obstack. */
12335 gdb_assert (HOST_CHAR_BIT == 8);
12336 return buf;
12337 }
12338
12339 static char *
12340 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
12341 {
12342 /* If the size of a host char is 8 bits, we can return a pointer
12343 to the string, otherwise we have to copy the string to a buffer
12344 allocated on the temporary obstack. */
12345 gdb_assert (HOST_CHAR_BIT == 8);
12346 if (*buf == '\0')
12347 {
12348 *bytes_read_ptr = 1;
12349 return NULL;
12350 }
12351 *bytes_read_ptr = strlen ((char *) buf) + 1;
12352 return (char *) buf;
12353 }
12354
12355 static char *
12356 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
12357 {
12358 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
12359 if (dwarf2_per_objfile->str.buffer == NULL)
12360 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
12361 bfd_get_filename (abfd));
12362 if (str_offset >= dwarf2_per_objfile->str.size)
12363 error (_("DW_FORM_strp pointing outside of "
12364 ".debug_str section [in module %s]"),
12365 bfd_get_filename (abfd));
12366 gdb_assert (HOST_CHAR_BIT == 8);
12367 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
12368 return NULL;
12369 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
12370 }
12371
12372 static char *
12373 read_indirect_string (bfd *abfd, gdb_byte *buf,
12374 const struct comp_unit_head *cu_header,
12375 unsigned int *bytes_read_ptr)
12376 {
12377 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
12378
12379 return read_indirect_string_at_offset (abfd, str_offset);
12380 }
12381
12382 static ULONGEST
12383 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
12384 {
12385 ULONGEST result;
12386 unsigned int num_read;
12387 int i, shift;
12388 unsigned char byte;
12389
12390 result = 0;
12391 shift = 0;
12392 num_read = 0;
12393 i = 0;
12394 while (1)
12395 {
12396 byte = bfd_get_8 (abfd, buf);
12397 buf++;
12398 num_read++;
12399 result |= ((ULONGEST) (byte & 127) << shift);
12400 if ((byte & 128) == 0)
12401 {
12402 break;
12403 }
12404 shift += 7;
12405 }
12406 *bytes_read_ptr = num_read;
12407 return result;
12408 }
12409
12410 static LONGEST
12411 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
12412 {
12413 LONGEST result;
12414 int i, shift, num_read;
12415 unsigned char byte;
12416
12417 result = 0;
12418 shift = 0;
12419 num_read = 0;
12420 i = 0;
12421 while (1)
12422 {
12423 byte = bfd_get_8 (abfd, buf);
12424 buf++;
12425 num_read++;
12426 result |= ((LONGEST) (byte & 127) << shift);
12427 shift += 7;
12428 if ((byte & 128) == 0)
12429 {
12430 break;
12431 }
12432 }
12433 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12434 result |= -(((LONGEST) 1) << shift);
12435 *bytes_read_ptr = num_read;
12436 return result;
12437 }
12438
12439 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
12440 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
12441 ADDR_SIZE is the size of addresses from the CU header. */
12442
12443 static CORE_ADDR
12444 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
12445 {
12446 struct objfile *objfile = dwarf2_per_objfile->objfile;
12447 bfd *abfd = objfile->obfd;
12448 const gdb_byte *info_ptr;
12449
12450 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
12451 if (dwarf2_per_objfile->addr.buffer == NULL)
12452 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
12453 objfile->name);
12454 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
12455 error (_("DW_FORM_addr_index pointing outside of "
12456 ".debug_addr section [in module %s]"),
12457 objfile->name);
12458 info_ptr = (dwarf2_per_objfile->addr.buffer
12459 + addr_base + addr_index * addr_size);
12460 if (addr_size == 4)
12461 return bfd_get_32 (abfd, info_ptr);
12462 else
12463 return bfd_get_64 (abfd, info_ptr);
12464 }
12465
12466 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
12467
12468 static CORE_ADDR
12469 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
12470 {
12471 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
12472 }
12473
12474 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
12475
12476 static CORE_ADDR
12477 read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
12478 unsigned int *bytes_read)
12479 {
12480 bfd *abfd = cu->objfile->obfd;
12481 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
12482
12483 return read_addr_index (cu, addr_index);
12484 }
12485
12486 /* Data structure to pass results from dwarf2_read_addr_index_reader
12487 back to dwarf2_read_addr_index. */
12488
12489 struct dwarf2_read_addr_index_data
12490 {
12491 ULONGEST addr_base;
12492 int addr_size;
12493 };
12494
12495 /* die_reader_func for dwarf2_read_addr_index. */
12496
12497 static void
12498 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
12499 gdb_byte *info_ptr,
12500 struct die_info *comp_unit_die,
12501 int has_children,
12502 void *data)
12503 {
12504 struct dwarf2_cu *cu = reader->cu;
12505 struct dwarf2_read_addr_index_data *aidata =
12506 (struct dwarf2_read_addr_index_data *) data;
12507
12508 aidata->addr_base = cu->addr_base;
12509 aidata->addr_size = cu->header.addr_size;
12510 }
12511
12512 /* Given an index in .debug_addr, fetch the value.
12513 NOTE: This can be called during dwarf expression evaluation,
12514 long after the debug information has been read, and thus per_cu->cu
12515 may no longer exist. */
12516
12517 CORE_ADDR
12518 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
12519 unsigned int addr_index)
12520 {
12521 struct objfile *objfile = per_cu->objfile;
12522 struct dwarf2_cu *cu = per_cu->cu;
12523 ULONGEST addr_base;
12524 int addr_size;
12525
12526 /* This is intended to be called from outside this file. */
12527 dw2_setup (objfile);
12528
12529 /* We need addr_base and addr_size.
12530 If we don't have PER_CU->cu, we have to get it.
12531 Nasty, but the alternative is storing the needed info in PER_CU,
12532 which at this point doesn't seem justified: it's not clear how frequently
12533 it would get used and it would increase the size of every PER_CU.
12534 Entry points like dwarf2_per_cu_addr_size do a similar thing
12535 so we're not in uncharted territory here.
12536 Alas we need to be a bit more complicated as addr_base is contained
12537 in the DIE.
12538
12539 We don't need to read the entire CU(/TU).
12540 We just need the header and top level die.
12541 IWBN to use the aging mechanism to let us lazily later discard the CU.
12542 See however init_cutu_and_read_dies_simple. */
12543
12544 if (cu != NULL)
12545 {
12546 addr_base = cu->addr_base;
12547 addr_size = cu->header.addr_size;
12548 }
12549 else
12550 {
12551 struct dwarf2_read_addr_index_data aidata;
12552
12553 init_cutu_and_read_dies_simple (per_cu, dwarf2_read_addr_index_reader,
12554 &aidata);
12555 addr_base = aidata.addr_base;
12556 addr_size = aidata.addr_size;
12557 }
12558
12559 return read_addr_index_1 (addr_index, addr_base, addr_size);
12560 }
12561
12562 /* Given a DW_AT_str_index, fetch the string. */
12563
12564 static char *
12565 read_str_index (const struct die_reader_specs *reader,
12566 struct dwarf2_cu *cu, ULONGEST str_index)
12567 {
12568 struct objfile *objfile = dwarf2_per_objfile->objfile;
12569 const char *dwo_name = objfile->name;
12570 bfd *abfd = objfile->obfd;
12571 struct dwo_sections *sections = &reader->dwo_file->sections;
12572 gdb_byte *info_ptr;
12573 ULONGEST str_offset;
12574
12575 dwarf2_read_section (objfile, &sections->str);
12576 dwarf2_read_section (objfile, &sections->str_offsets);
12577 if (sections->str.buffer == NULL)
12578 error (_("DW_FORM_str_index used without .debug_str.dwo section"
12579 " in CU at offset 0x%lx [in module %s]"),
12580 (long) cu->header.offset.sect_off, dwo_name);
12581 if (sections->str_offsets.buffer == NULL)
12582 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
12583 " in CU at offset 0x%lx [in module %s]"),
12584 (long) cu->header.offset.sect_off, dwo_name);
12585 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
12586 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
12587 " section in CU at offset 0x%lx [in module %s]"),
12588 (long) cu->header.offset.sect_off, dwo_name);
12589 info_ptr = (sections->str_offsets.buffer
12590 + str_index * cu->header.offset_size);
12591 if (cu->header.offset_size == 4)
12592 str_offset = bfd_get_32 (abfd, info_ptr);
12593 else
12594 str_offset = bfd_get_64 (abfd, info_ptr);
12595 if (str_offset >= sections->str.size)
12596 error (_("Offset from DW_FORM_str_index pointing outside of"
12597 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
12598 (long) cu->header.offset.sect_off, dwo_name);
12599 return (char *) (sections->str.buffer + str_offset);
12600 }
12601
12602 /* Return the length of an LEB128 number in BUF. */
12603
12604 static int
12605 leb128_size (const gdb_byte *buf)
12606 {
12607 const gdb_byte *begin = buf;
12608 gdb_byte byte;
12609
12610 while (1)
12611 {
12612 byte = *buf++;
12613 if ((byte & 128) == 0)
12614 return buf - begin;
12615 }
12616 }
12617
12618 static void
12619 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
12620 {
12621 switch (lang)
12622 {
12623 case DW_LANG_C89:
12624 case DW_LANG_C99:
12625 case DW_LANG_C:
12626 cu->language = language_c;
12627 break;
12628 case DW_LANG_C_plus_plus:
12629 cu->language = language_cplus;
12630 break;
12631 case DW_LANG_D:
12632 cu->language = language_d;
12633 break;
12634 case DW_LANG_Fortran77:
12635 case DW_LANG_Fortran90:
12636 case DW_LANG_Fortran95:
12637 cu->language = language_fortran;
12638 break;
12639 case DW_LANG_Go:
12640 cu->language = language_go;
12641 break;
12642 case DW_LANG_Mips_Assembler:
12643 cu->language = language_asm;
12644 break;
12645 case DW_LANG_Java:
12646 cu->language = language_java;
12647 break;
12648 case DW_LANG_Ada83:
12649 case DW_LANG_Ada95:
12650 cu->language = language_ada;
12651 break;
12652 case DW_LANG_Modula2:
12653 cu->language = language_m2;
12654 break;
12655 case DW_LANG_Pascal83:
12656 cu->language = language_pascal;
12657 break;
12658 case DW_LANG_ObjC:
12659 cu->language = language_objc;
12660 break;
12661 case DW_LANG_Cobol74:
12662 case DW_LANG_Cobol85:
12663 default:
12664 cu->language = language_minimal;
12665 break;
12666 }
12667 cu->language_defn = language_def (cu->language);
12668 }
12669
12670 /* Return the named attribute or NULL if not there. */
12671
12672 static struct attribute *
12673 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
12674 {
12675 for (;;)
12676 {
12677 unsigned int i;
12678 struct attribute *spec = NULL;
12679
12680 for (i = 0; i < die->num_attrs; ++i)
12681 {
12682 if (die->attrs[i].name == name)
12683 return &die->attrs[i];
12684 if (die->attrs[i].name == DW_AT_specification
12685 || die->attrs[i].name == DW_AT_abstract_origin)
12686 spec = &die->attrs[i];
12687 }
12688
12689 if (!spec)
12690 break;
12691
12692 die = follow_die_ref (die, spec, &cu);
12693 }
12694
12695 return NULL;
12696 }
12697
12698 /* Return the named attribute or NULL if not there,
12699 but do not follow DW_AT_specification, etc.
12700 This is for use in contexts where we're reading .debug_types dies.
12701 Following DW_AT_specification, DW_AT_abstract_origin will take us
12702 back up the chain, and we want to go down. */
12703
12704 static struct attribute *
12705 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
12706 struct dwarf2_cu *cu)
12707 {
12708 unsigned int i;
12709
12710 for (i = 0; i < die->num_attrs; ++i)
12711 if (die->attrs[i].name == name)
12712 return &die->attrs[i];
12713
12714 return NULL;
12715 }
12716
12717 /* Return non-zero iff the attribute NAME is defined for the given DIE,
12718 and holds a non-zero value. This function should only be used for
12719 DW_FORM_flag or DW_FORM_flag_present attributes. */
12720
12721 static int
12722 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
12723 {
12724 struct attribute *attr = dwarf2_attr (die, name, cu);
12725
12726 return (attr && DW_UNSND (attr));
12727 }
12728
12729 static int
12730 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
12731 {
12732 /* A DIE is a declaration if it has a DW_AT_declaration attribute
12733 which value is non-zero. However, we have to be careful with
12734 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
12735 (via dwarf2_flag_true_p) follows this attribute. So we may
12736 end up accidently finding a declaration attribute that belongs
12737 to a different DIE referenced by the specification attribute,
12738 even though the given DIE does not have a declaration attribute. */
12739 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
12740 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
12741 }
12742
12743 /* Return the die giving the specification for DIE, if there is
12744 one. *SPEC_CU is the CU containing DIE on input, and the CU
12745 containing the return value on output. If there is no
12746 specification, but there is an abstract origin, that is
12747 returned. */
12748
12749 static struct die_info *
12750 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
12751 {
12752 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
12753 *spec_cu);
12754
12755 if (spec_attr == NULL)
12756 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
12757
12758 if (spec_attr == NULL)
12759 return NULL;
12760 else
12761 return follow_die_ref (die, spec_attr, spec_cu);
12762 }
12763
12764 /* Free the line_header structure *LH, and any arrays and strings it
12765 refers to.
12766 NOTE: This is also used as a "cleanup" function. */
12767
12768 static void
12769 free_line_header (struct line_header *lh)
12770 {
12771 if (lh->standard_opcode_lengths)
12772 xfree (lh->standard_opcode_lengths);
12773
12774 /* Remember that all the lh->file_names[i].name pointers are
12775 pointers into debug_line_buffer, and don't need to be freed. */
12776 if (lh->file_names)
12777 xfree (lh->file_names);
12778
12779 /* Similarly for the include directory names. */
12780 if (lh->include_dirs)
12781 xfree (lh->include_dirs);
12782
12783 xfree (lh);
12784 }
12785
12786 /* Add an entry to LH's include directory table. */
12787
12788 static void
12789 add_include_dir (struct line_header *lh, char *include_dir)
12790 {
12791 /* Grow the array if necessary. */
12792 if (lh->include_dirs_size == 0)
12793 {
12794 lh->include_dirs_size = 1; /* for testing */
12795 lh->include_dirs = xmalloc (lh->include_dirs_size
12796 * sizeof (*lh->include_dirs));
12797 }
12798 else if (lh->num_include_dirs >= lh->include_dirs_size)
12799 {
12800 lh->include_dirs_size *= 2;
12801 lh->include_dirs = xrealloc (lh->include_dirs,
12802 (lh->include_dirs_size
12803 * sizeof (*lh->include_dirs)));
12804 }
12805
12806 lh->include_dirs[lh->num_include_dirs++] = include_dir;
12807 }
12808
12809 /* Add an entry to LH's file name table. */
12810
12811 static void
12812 add_file_name (struct line_header *lh,
12813 char *name,
12814 unsigned int dir_index,
12815 unsigned int mod_time,
12816 unsigned int length)
12817 {
12818 struct file_entry *fe;
12819
12820 /* Grow the array if necessary. */
12821 if (lh->file_names_size == 0)
12822 {
12823 lh->file_names_size = 1; /* for testing */
12824 lh->file_names = xmalloc (lh->file_names_size
12825 * sizeof (*lh->file_names));
12826 }
12827 else if (lh->num_file_names >= lh->file_names_size)
12828 {
12829 lh->file_names_size *= 2;
12830 lh->file_names = xrealloc (lh->file_names,
12831 (lh->file_names_size
12832 * sizeof (*lh->file_names)));
12833 }
12834
12835 fe = &lh->file_names[lh->num_file_names++];
12836 fe->name = name;
12837 fe->dir_index = dir_index;
12838 fe->mod_time = mod_time;
12839 fe->length = length;
12840 fe->included_p = 0;
12841 fe->symtab = NULL;
12842 }
12843
12844 /* Read the statement program header starting at OFFSET in
12845 .debug_line, or .debug_line.dwo. Return a pointer
12846 to a struct line_header, allocated using xmalloc.
12847
12848 NOTE: the strings in the include directory and file name tables of
12849 the returned object point into the dwarf line section buffer,
12850 and must not be freed. */
12851
12852 static struct line_header *
12853 dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
12854 {
12855 struct cleanup *back_to;
12856 struct line_header *lh;
12857 gdb_byte *line_ptr;
12858 unsigned int bytes_read, offset_size;
12859 int i;
12860 char *cur_dir, *cur_file;
12861 struct dwarf2_section_info *section;
12862 bfd *abfd;
12863
12864 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
12865 DWO file. */
12866 if (cu->dwo_unit && cu->per_cu->is_debug_types)
12867 section = &cu->dwo_unit->dwo_file->sections.line;
12868 else
12869 section = &dwarf2_per_objfile->line;
12870
12871 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
12872 if (section->buffer == NULL)
12873 {
12874 if (cu->dwo_unit && cu->per_cu->is_debug_types)
12875 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
12876 else
12877 complaint (&symfile_complaints, _("missing .debug_line section"));
12878 return 0;
12879 }
12880
12881 /* We can't do this until we know the section is non-empty.
12882 Only then do we know we have such a section. */
12883 abfd = section->asection->owner;
12884
12885 /* Make sure that at least there's room for the total_length field.
12886 That could be 12 bytes long, but we're just going to fudge that. */
12887 if (offset + 4 >= section->size)
12888 {
12889 dwarf2_statement_list_fits_in_line_number_section_complaint ();
12890 return 0;
12891 }
12892
12893 lh = xmalloc (sizeof (*lh));
12894 memset (lh, 0, sizeof (*lh));
12895 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
12896 (void *) lh);
12897
12898 line_ptr = section->buffer + offset;
12899
12900 /* Read in the header. */
12901 lh->total_length =
12902 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
12903 &bytes_read, &offset_size);
12904 line_ptr += bytes_read;
12905 if (line_ptr + lh->total_length > (section->buffer + section->size))
12906 {
12907 dwarf2_statement_list_fits_in_line_number_section_complaint ();
12908 return 0;
12909 }
12910 lh->statement_program_end = line_ptr + lh->total_length;
12911 lh->version = read_2_bytes (abfd, line_ptr);
12912 line_ptr += 2;
12913 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
12914 line_ptr += offset_size;
12915 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
12916 line_ptr += 1;
12917 if (lh->version >= 4)
12918 {
12919 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
12920 line_ptr += 1;
12921 }
12922 else
12923 lh->maximum_ops_per_instruction = 1;
12924
12925 if (lh->maximum_ops_per_instruction == 0)
12926 {
12927 lh->maximum_ops_per_instruction = 1;
12928 complaint (&symfile_complaints,
12929 _("invalid maximum_ops_per_instruction "
12930 "in `.debug_line' section"));
12931 }
12932
12933 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
12934 line_ptr += 1;
12935 lh->line_base = read_1_signed_byte (abfd, line_ptr);
12936 line_ptr += 1;
12937 lh->line_range = read_1_byte (abfd, line_ptr);
12938 line_ptr += 1;
12939 lh->opcode_base = read_1_byte (abfd, line_ptr);
12940 line_ptr += 1;
12941 lh->standard_opcode_lengths
12942 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
12943
12944 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
12945 for (i = 1; i < lh->opcode_base; ++i)
12946 {
12947 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
12948 line_ptr += 1;
12949 }
12950
12951 /* Read directory table. */
12952 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
12953 {
12954 line_ptr += bytes_read;
12955 add_include_dir (lh, cur_dir);
12956 }
12957 line_ptr += bytes_read;
12958
12959 /* Read file name table. */
12960 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
12961 {
12962 unsigned int dir_index, mod_time, length;
12963
12964 line_ptr += bytes_read;
12965 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12966 line_ptr += bytes_read;
12967 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12968 line_ptr += bytes_read;
12969 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12970 line_ptr += bytes_read;
12971
12972 add_file_name (lh, cur_file, dir_index, mod_time, length);
12973 }
12974 line_ptr += bytes_read;
12975 lh->statement_program_start = line_ptr;
12976
12977 if (line_ptr > (section->buffer + section->size))
12978 complaint (&symfile_complaints,
12979 _("line number info header doesn't "
12980 "fit in `.debug_line' section"));
12981
12982 discard_cleanups (back_to);
12983 return lh;
12984 }
12985
12986 /* Subroutine of dwarf_decode_lines to simplify it.
12987 Return the file name of the psymtab for included file FILE_INDEX
12988 in line header LH of PST.
12989 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
12990 If space for the result is malloc'd, it will be freed by a cleanup.
12991 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
12992
12993 static char *
12994 psymtab_include_file_name (const struct line_header *lh, int file_index,
12995 const struct partial_symtab *pst,
12996 const char *comp_dir)
12997 {
12998 const struct file_entry fe = lh->file_names [file_index];
12999 char *include_name = fe.name;
13000 char *include_name_to_compare = include_name;
13001 char *dir_name = NULL;
13002 const char *pst_filename;
13003 char *copied_name = NULL;
13004 int file_is_pst;
13005
13006 if (fe.dir_index)
13007 dir_name = lh->include_dirs[fe.dir_index - 1];
13008
13009 if (!IS_ABSOLUTE_PATH (include_name)
13010 && (dir_name != NULL || comp_dir != NULL))
13011 {
13012 /* Avoid creating a duplicate psymtab for PST.
13013 We do this by comparing INCLUDE_NAME and PST_FILENAME.
13014 Before we do the comparison, however, we need to account
13015 for DIR_NAME and COMP_DIR.
13016 First prepend dir_name (if non-NULL). If we still don't
13017 have an absolute path prepend comp_dir (if non-NULL).
13018 However, the directory we record in the include-file's
13019 psymtab does not contain COMP_DIR (to match the
13020 corresponding symtab(s)).
13021
13022 Example:
13023
13024 bash$ cd /tmp
13025 bash$ gcc -g ./hello.c
13026 include_name = "hello.c"
13027 dir_name = "."
13028 DW_AT_comp_dir = comp_dir = "/tmp"
13029 DW_AT_name = "./hello.c" */
13030
13031 if (dir_name != NULL)
13032 {
13033 include_name = concat (dir_name, SLASH_STRING,
13034 include_name, (char *)NULL);
13035 include_name_to_compare = include_name;
13036 make_cleanup (xfree, include_name);
13037 }
13038 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
13039 {
13040 include_name_to_compare = concat (comp_dir, SLASH_STRING,
13041 include_name, (char *)NULL);
13042 }
13043 }
13044
13045 pst_filename = pst->filename;
13046 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
13047 {
13048 copied_name = concat (pst->dirname, SLASH_STRING,
13049 pst_filename, (char *)NULL);
13050 pst_filename = copied_name;
13051 }
13052
13053 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
13054
13055 if (include_name_to_compare != include_name)
13056 xfree (include_name_to_compare);
13057 if (copied_name != NULL)
13058 xfree (copied_name);
13059
13060 if (file_is_pst)
13061 return NULL;
13062 return include_name;
13063 }
13064
13065 /* Ignore this record_line request. */
13066
13067 static void
13068 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
13069 {
13070 return;
13071 }
13072
13073 /* Subroutine of dwarf_decode_lines to simplify it.
13074 Process the line number information in LH. */
13075
13076 static void
13077 dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
13078 struct dwarf2_cu *cu, struct partial_symtab *pst)
13079 {
13080 gdb_byte *line_ptr, *extended_end;
13081 gdb_byte *line_end;
13082 unsigned int bytes_read, extended_len;
13083 unsigned char op_code, extended_op, adj_opcode;
13084 CORE_ADDR baseaddr;
13085 struct objfile *objfile = cu->objfile;
13086 bfd *abfd = objfile->obfd;
13087 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13088 const int decode_for_pst_p = (pst != NULL);
13089 struct subfile *last_subfile = NULL;
13090 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
13091 = record_line;
13092
13093 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13094
13095 line_ptr = lh->statement_program_start;
13096 line_end = lh->statement_program_end;
13097
13098 /* Read the statement sequences until there's nothing left. */
13099 while (line_ptr < line_end)
13100 {
13101 /* state machine registers */
13102 CORE_ADDR address = 0;
13103 unsigned int file = 1;
13104 unsigned int line = 1;
13105 unsigned int column = 0;
13106 int is_stmt = lh->default_is_stmt;
13107 int basic_block = 0;
13108 int end_sequence = 0;
13109 CORE_ADDR addr;
13110 unsigned char op_index = 0;
13111
13112 if (!decode_for_pst_p && lh->num_file_names >= file)
13113 {
13114 /* Start a subfile for the current file of the state machine. */
13115 /* lh->include_dirs and lh->file_names are 0-based, but the
13116 directory and file name numbers in the statement program
13117 are 1-based. */
13118 struct file_entry *fe = &lh->file_names[file - 1];
13119 char *dir = NULL;
13120
13121 if (fe->dir_index)
13122 dir = lh->include_dirs[fe->dir_index - 1];
13123
13124 dwarf2_start_subfile (fe->name, dir, comp_dir);
13125 }
13126
13127 /* Decode the table. */
13128 while (!end_sequence)
13129 {
13130 op_code = read_1_byte (abfd, line_ptr);
13131 line_ptr += 1;
13132 if (line_ptr > line_end)
13133 {
13134 dwarf2_debug_line_missing_end_sequence_complaint ();
13135 break;
13136 }
13137
13138 if (op_code >= lh->opcode_base)
13139 {
13140 /* Special operand. */
13141 adj_opcode = op_code - lh->opcode_base;
13142 address += (((op_index + (adj_opcode / lh->line_range))
13143 / lh->maximum_ops_per_instruction)
13144 * lh->minimum_instruction_length);
13145 op_index = ((op_index + (adj_opcode / lh->line_range))
13146 % lh->maximum_ops_per_instruction);
13147 line += lh->line_base + (adj_opcode % lh->line_range);
13148 if (lh->num_file_names < file || file == 0)
13149 dwarf2_debug_line_missing_file_complaint ();
13150 /* For now we ignore lines not starting on an
13151 instruction boundary. */
13152 else if (op_index == 0)
13153 {
13154 lh->file_names[file - 1].included_p = 1;
13155 if (!decode_for_pst_p && is_stmt)
13156 {
13157 if (last_subfile != current_subfile)
13158 {
13159 addr = gdbarch_addr_bits_remove (gdbarch, address);
13160 if (last_subfile)
13161 (*p_record_line) (last_subfile, 0, addr);
13162 last_subfile = current_subfile;
13163 }
13164 /* Append row to matrix using current values. */
13165 addr = gdbarch_addr_bits_remove (gdbarch, address);
13166 (*p_record_line) (current_subfile, line, addr);
13167 }
13168 }
13169 basic_block = 0;
13170 }
13171 else switch (op_code)
13172 {
13173 case DW_LNS_extended_op:
13174 extended_len = read_unsigned_leb128 (abfd, line_ptr,
13175 &bytes_read);
13176 line_ptr += bytes_read;
13177 extended_end = line_ptr + extended_len;
13178 extended_op = read_1_byte (abfd, line_ptr);
13179 line_ptr += 1;
13180 switch (extended_op)
13181 {
13182 case DW_LNE_end_sequence:
13183 p_record_line = record_line;
13184 end_sequence = 1;
13185 break;
13186 case DW_LNE_set_address:
13187 address = read_address (abfd, line_ptr, cu, &bytes_read);
13188
13189 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
13190 {
13191 /* This line table is for a function which has been
13192 GCd by the linker. Ignore it. PR gdb/12528 */
13193
13194 long line_offset
13195 = line_ptr - dwarf2_per_objfile->line.buffer;
13196
13197 complaint (&symfile_complaints,
13198 _(".debug_line address at offset 0x%lx is 0 "
13199 "[in module %s]"),
13200 line_offset, objfile->name);
13201 p_record_line = noop_record_line;
13202 }
13203
13204 op_index = 0;
13205 line_ptr += bytes_read;
13206 address += baseaddr;
13207 break;
13208 case DW_LNE_define_file:
13209 {
13210 char *cur_file;
13211 unsigned int dir_index, mod_time, length;
13212
13213 cur_file = read_direct_string (abfd, line_ptr,
13214 &bytes_read);
13215 line_ptr += bytes_read;
13216 dir_index =
13217 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13218 line_ptr += bytes_read;
13219 mod_time =
13220 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13221 line_ptr += bytes_read;
13222 length =
13223 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13224 line_ptr += bytes_read;
13225 add_file_name (lh, cur_file, dir_index, mod_time, length);
13226 }
13227 break;
13228 case DW_LNE_set_discriminator:
13229 /* The discriminator is not interesting to the debugger;
13230 just ignore it. */
13231 line_ptr = extended_end;
13232 break;
13233 default:
13234 complaint (&symfile_complaints,
13235 _("mangled .debug_line section"));
13236 return;
13237 }
13238 /* Make sure that we parsed the extended op correctly. If e.g.
13239 we expected a different address size than the producer used,
13240 we may have read the wrong number of bytes. */
13241 if (line_ptr != extended_end)
13242 {
13243 complaint (&symfile_complaints,
13244 _("mangled .debug_line section"));
13245 return;
13246 }
13247 break;
13248 case DW_LNS_copy:
13249 if (lh->num_file_names < file || file == 0)
13250 dwarf2_debug_line_missing_file_complaint ();
13251 else
13252 {
13253 lh->file_names[file - 1].included_p = 1;
13254 if (!decode_for_pst_p && is_stmt)
13255 {
13256 if (last_subfile != current_subfile)
13257 {
13258 addr = gdbarch_addr_bits_remove (gdbarch, address);
13259 if (last_subfile)
13260 (*p_record_line) (last_subfile, 0, addr);
13261 last_subfile = current_subfile;
13262 }
13263 addr = gdbarch_addr_bits_remove (gdbarch, address);
13264 (*p_record_line) (current_subfile, line, addr);
13265 }
13266 }
13267 basic_block = 0;
13268 break;
13269 case DW_LNS_advance_pc:
13270 {
13271 CORE_ADDR adjust
13272 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13273
13274 address += (((op_index + adjust)
13275 / lh->maximum_ops_per_instruction)
13276 * lh->minimum_instruction_length);
13277 op_index = ((op_index + adjust)
13278 % lh->maximum_ops_per_instruction);
13279 line_ptr += bytes_read;
13280 }
13281 break;
13282 case DW_LNS_advance_line:
13283 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
13284 line_ptr += bytes_read;
13285 break;
13286 case DW_LNS_set_file:
13287 {
13288 /* The arrays lh->include_dirs and lh->file_names are
13289 0-based, but the directory and file name numbers in
13290 the statement program are 1-based. */
13291 struct file_entry *fe;
13292 char *dir = NULL;
13293
13294 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13295 line_ptr += bytes_read;
13296 if (lh->num_file_names < file || file == 0)
13297 dwarf2_debug_line_missing_file_complaint ();
13298 else
13299 {
13300 fe = &lh->file_names[file - 1];
13301 if (fe->dir_index)
13302 dir = lh->include_dirs[fe->dir_index - 1];
13303 if (!decode_for_pst_p)
13304 {
13305 last_subfile = current_subfile;
13306 dwarf2_start_subfile (fe->name, dir, comp_dir);
13307 }
13308 }
13309 }
13310 break;
13311 case DW_LNS_set_column:
13312 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13313 line_ptr += bytes_read;
13314 break;
13315 case DW_LNS_negate_stmt:
13316 is_stmt = (!is_stmt);
13317 break;
13318 case DW_LNS_set_basic_block:
13319 basic_block = 1;
13320 break;
13321 /* Add to the address register of the state machine the
13322 address increment value corresponding to special opcode
13323 255. I.e., this value is scaled by the minimum
13324 instruction length since special opcode 255 would have
13325 scaled the increment. */
13326 case DW_LNS_const_add_pc:
13327 {
13328 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
13329
13330 address += (((op_index + adjust)
13331 / lh->maximum_ops_per_instruction)
13332 * lh->minimum_instruction_length);
13333 op_index = ((op_index + adjust)
13334 % lh->maximum_ops_per_instruction);
13335 }
13336 break;
13337 case DW_LNS_fixed_advance_pc:
13338 address += read_2_bytes (abfd, line_ptr);
13339 op_index = 0;
13340 line_ptr += 2;
13341 break;
13342 default:
13343 {
13344 /* Unknown standard opcode, ignore it. */
13345 int i;
13346
13347 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
13348 {
13349 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13350 line_ptr += bytes_read;
13351 }
13352 }
13353 }
13354 }
13355 if (lh->num_file_names < file || file == 0)
13356 dwarf2_debug_line_missing_file_complaint ();
13357 else
13358 {
13359 lh->file_names[file - 1].included_p = 1;
13360 if (!decode_for_pst_p)
13361 {
13362 addr = gdbarch_addr_bits_remove (gdbarch, address);
13363 (*p_record_line) (current_subfile, 0, addr);
13364 }
13365 }
13366 }
13367 }
13368
13369 /* Decode the Line Number Program (LNP) for the given line_header
13370 structure and CU. The actual information extracted and the type
13371 of structures created from the LNP depends on the value of PST.
13372
13373 1. If PST is NULL, then this procedure uses the data from the program
13374 to create all necessary symbol tables, and their linetables.
13375
13376 2. If PST is not NULL, this procedure reads the program to determine
13377 the list of files included by the unit represented by PST, and
13378 builds all the associated partial symbol tables.
13379
13380 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
13381 It is used for relative paths in the line table.
13382 NOTE: When processing partial symtabs (pst != NULL),
13383 comp_dir == pst->dirname.
13384
13385 NOTE: It is important that psymtabs have the same file name (via strcmp)
13386 as the corresponding symtab. Since COMP_DIR is not used in the name of the
13387 symtab we don't use it in the name of the psymtabs we create.
13388 E.g. expand_line_sal requires this when finding psymtabs to expand.
13389 A good testcase for this is mb-inline.exp. */
13390
13391 static void
13392 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
13393 struct dwarf2_cu *cu, struct partial_symtab *pst,
13394 int want_line_info)
13395 {
13396 struct objfile *objfile = cu->objfile;
13397 const int decode_for_pst_p = (pst != NULL);
13398 struct subfile *first_subfile = current_subfile;
13399
13400 if (want_line_info)
13401 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
13402
13403 if (decode_for_pst_p)
13404 {
13405 int file_index;
13406
13407 /* Now that we're done scanning the Line Header Program, we can
13408 create the psymtab of each included file. */
13409 for (file_index = 0; file_index < lh->num_file_names; file_index++)
13410 if (lh->file_names[file_index].included_p == 1)
13411 {
13412 char *include_name =
13413 psymtab_include_file_name (lh, file_index, pst, comp_dir);
13414 if (include_name != NULL)
13415 dwarf2_create_include_psymtab (include_name, pst, objfile);
13416 }
13417 }
13418 else
13419 {
13420 /* Make sure a symtab is created for every file, even files
13421 which contain only variables (i.e. no code with associated
13422 line numbers). */
13423 int i;
13424
13425 for (i = 0; i < lh->num_file_names; i++)
13426 {
13427 char *dir = NULL;
13428 struct file_entry *fe;
13429
13430 fe = &lh->file_names[i];
13431 if (fe->dir_index)
13432 dir = lh->include_dirs[fe->dir_index - 1];
13433 dwarf2_start_subfile (fe->name, dir, comp_dir);
13434
13435 /* Skip the main file; we don't need it, and it must be
13436 allocated last, so that it will show up before the
13437 non-primary symtabs in the objfile's symtab list. */
13438 if (current_subfile == first_subfile)
13439 continue;
13440
13441 if (current_subfile->symtab == NULL)
13442 current_subfile->symtab = allocate_symtab (current_subfile->name,
13443 objfile);
13444 fe->symtab = current_subfile->symtab;
13445 }
13446 }
13447 }
13448
13449 /* Start a subfile for DWARF. FILENAME is the name of the file and
13450 DIRNAME the name of the source directory which contains FILENAME
13451 or NULL if not known. COMP_DIR is the compilation directory for the
13452 linetable's compilation unit or NULL if not known.
13453 This routine tries to keep line numbers from identical absolute and
13454 relative file names in a common subfile.
13455
13456 Using the `list' example from the GDB testsuite, which resides in
13457 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
13458 of /srcdir/list0.c yields the following debugging information for list0.c:
13459
13460 DW_AT_name: /srcdir/list0.c
13461 DW_AT_comp_dir: /compdir
13462 files.files[0].name: list0.h
13463 files.files[0].dir: /srcdir
13464 files.files[1].name: list0.c
13465 files.files[1].dir: /srcdir
13466
13467 The line number information for list0.c has to end up in a single
13468 subfile, so that `break /srcdir/list0.c:1' works as expected.
13469 start_subfile will ensure that this happens provided that we pass the
13470 concatenation of files.files[1].dir and files.files[1].name as the
13471 subfile's name. */
13472
13473 static void
13474 dwarf2_start_subfile (char *filename, const char *dirname,
13475 const char *comp_dir)
13476 {
13477 char *fullname;
13478
13479 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
13480 `start_symtab' will always pass the contents of DW_AT_comp_dir as
13481 second argument to start_subfile. To be consistent, we do the
13482 same here. In order not to lose the line information directory,
13483 we concatenate it to the filename when it makes sense.
13484 Note that the Dwarf3 standard says (speaking of filenames in line
13485 information): ``The directory index is ignored for file names
13486 that represent full path names''. Thus ignoring dirname in the
13487 `else' branch below isn't an issue. */
13488
13489 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
13490 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
13491 else
13492 fullname = filename;
13493
13494 start_subfile (fullname, comp_dir);
13495
13496 if (fullname != filename)
13497 xfree (fullname);
13498 }
13499
13500 static void
13501 var_decode_location (struct attribute *attr, struct symbol *sym,
13502 struct dwarf2_cu *cu)
13503 {
13504 struct objfile *objfile = cu->objfile;
13505 struct comp_unit_head *cu_header = &cu->header;
13506
13507 /* NOTE drow/2003-01-30: There used to be a comment and some special
13508 code here to turn a symbol with DW_AT_external and a
13509 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
13510 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
13511 with some versions of binutils) where shared libraries could have
13512 relocations against symbols in their debug information - the
13513 minimal symbol would have the right address, but the debug info
13514 would not. It's no longer necessary, because we will explicitly
13515 apply relocations when we read in the debug information now. */
13516
13517 /* A DW_AT_location attribute with no contents indicates that a
13518 variable has been optimized away. */
13519 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
13520 {
13521 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
13522 return;
13523 }
13524
13525 /* Handle one degenerate form of location expression specially, to
13526 preserve GDB's previous behavior when section offsets are
13527 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
13528 then mark this symbol as LOC_STATIC. */
13529
13530 if (attr_form_is_block (attr)
13531 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
13532 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
13533 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
13534 && (DW_BLOCK (attr)->size
13535 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
13536 {
13537 unsigned int dummy;
13538
13539 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
13540 SYMBOL_VALUE_ADDRESS (sym) =
13541 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
13542 else
13543 SYMBOL_VALUE_ADDRESS (sym) =
13544 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
13545 SYMBOL_CLASS (sym) = LOC_STATIC;
13546 fixup_symbol_section (sym, objfile);
13547 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
13548 SYMBOL_SECTION (sym));
13549 return;
13550 }
13551
13552 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
13553 expression evaluator, and use LOC_COMPUTED only when necessary
13554 (i.e. when the value of a register or memory location is
13555 referenced, or a thread-local block, etc.). Then again, it might
13556 not be worthwhile. I'm assuming that it isn't unless performance
13557 or memory numbers show me otherwise. */
13558
13559 dwarf2_symbol_mark_computed (attr, sym, cu);
13560 SYMBOL_CLASS (sym) = LOC_COMPUTED;
13561
13562 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
13563 cu->has_loclist = 1;
13564 }
13565
13566 /* Given a pointer to a DWARF information entry, figure out if we need
13567 to make a symbol table entry for it, and if so, create a new entry
13568 and return a pointer to it.
13569 If TYPE is NULL, determine symbol type from the die, otherwise
13570 used the passed type.
13571 If SPACE is not NULL, use it to hold the new symbol. If it is
13572 NULL, allocate a new symbol on the objfile's obstack. */
13573
13574 static struct symbol *
13575 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
13576 struct symbol *space)
13577 {
13578 struct objfile *objfile = cu->objfile;
13579 struct symbol *sym = NULL;
13580 char *name;
13581 struct attribute *attr = NULL;
13582 struct attribute *attr2 = NULL;
13583 CORE_ADDR baseaddr;
13584 struct pending **list_to_add = NULL;
13585
13586 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13587
13588 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13589
13590 name = dwarf2_name (die, cu);
13591 if (name)
13592 {
13593 const char *linkagename;
13594 int suppress_add = 0;
13595
13596 if (space)
13597 sym = space;
13598 else
13599 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
13600 OBJSTAT (objfile, n_syms++);
13601
13602 /* Cache this symbol's name and the name's demangled form (if any). */
13603 SYMBOL_SET_LANGUAGE (sym, cu->language);
13604 linkagename = dwarf2_physname (name, die, cu);
13605 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
13606
13607 /* Fortran does not have mangling standard and the mangling does differ
13608 between gfortran, iFort etc. */
13609 if (cu->language == language_fortran
13610 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
13611 symbol_set_demangled_name (&(sym->ginfo),
13612 (char *) dwarf2_full_name (name, die, cu),
13613 NULL);
13614
13615 /* Default assumptions.
13616 Use the passed type or decode it from the die. */
13617 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
13618 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
13619 if (type != NULL)
13620 SYMBOL_TYPE (sym) = type;
13621 else
13622 SYMBOL_TYPE (sym) = die_type (die, cu);
13623 attr = dwarf2_attr (die,
13624 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
13625 cu);
13626 if (attr)
13627 {
13628 SYMBOL_LINE (sym) = DW_UNSND (attr);
13629 }
13630
13631 attr = dwarf2_attr (die,
13632 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
13633 cu);
13634 if (attr)
13635 {
13636 int file_index = DW_UNSND (attr);
13637
13638 if (cu->line_header == NULL
13639 || file_index > cu->line_header->num_file_names)
13640 complaint (&symfile_complaints,
13641 _("file index out of range"));
13642 else if (file_index > 0)
13643 {
13644 struct file_entry *fe;
13645
13646 fe = &cu->line_header->file_names[file_index - 1];
13647 SYMBOL_SYMTAB (sym) = fe->symtab;
13648 }
13649 }
13650
13651 switch (die->tag)
13652 {
13653 case DW_TAG_label:
13654 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13655 if (attr)
13656 {
13657 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
13658 }
13659 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
13660 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
13661 SYMBOL_CLASS (sym) = LOC_LABEL;
13662 add_symbol_to_list (sym, cu->list_in_scope);
13663 break;
13664 case DW_TAG_subprogram:
13665 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
13666 finish_block. */
13667 SYMBOL_CLASS (sym) = LOC_BLOCK;
13668 attr2 = dwarf2_attr (die, DW_AT_external, cu);
13669 if ((attr2 && (DW_UNSND (attr2) != 0))
13670 || cu->language == language_ada)
13671 {
13672 /* Subprograms marked external are stored as a global symbol.
13673 Ada subprograms, whether marked external or not, are always
13674 stored as a global symbol, because we want to be able to
13675 access them globally. For instance, we want to be able
13676 to break on a nested subprogram without having to
13677 specify the context. */
13678 list_to_add = &global_symbols;
13679 }
13680 else
13681 {
13682 list_to_add = cu->list_in_scope;
13683 }
13684 break;
13685 case DW_TAG_inlined_subroutine:
13686 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
13687 finish_block. */
13688 SYMBOL_CLASS (sym) = LOC_BLOCK;
13689 SYMBOL_INLINED (sym) = 1;
13690 list_to_add = cu->list_in_scope;
13691 break;
13692 case DW_TAG_template_value_param:
13693 suppress_add = 1;
13694 /* Fall through. */
13695 case DW_TAG_constant:
13696 case DW_TAG_variable:
13697 case DW_TAG_member:
13698 /* Compilation with minimal debug info may result in
13699 variables with missing type entries. Change the
13700 misleading `void' type to something sensible. */
13701 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
13702 SYMBOL_TYPE (sym)
13703 = objfile_type (objfile)->nodebug_data_symbol;
13704
13705 attr = dwarf2_attr (die, DW_AT_const_value, cu);
13706 /* In the case of DW_TAG_member, we should only be called for
13707 static const members. */
13708 if (die->tag == DW_TAG_member)
13709 {
13710 /* dwarf2_add_field uses die_is_declaration,
13711 so we do the same. */
13712 gdb_assert (die_is_declaration (die, cu));
13713 gdb_assert (attr);
13714 }
13715 if (attr)
13716 {
13717 dwarf2_const_value (attr, sym, cu);
13718 attr2 = dwarf2_attr (die, DW_AT_external, cu);
13719 if (!suppress_add)
13720 {
13721 if (attr2 && (DW_UNSND (attr2) != 0))
13722 list_to_add = &global_symbols;
13723 else
13724 list_to_add = cu->list_in_scope;
13725 }
13726 break;
13727 }
13728 attr = dwarf2_attr (die, DW_AT_location, cu);
13729 if (attr)
13730 {
13731 var_decode_location (attr, sym, cu);
13732 attr2 = dwarf2_attr (die, DW_AT_external, cu);
13733 if (SYMBOL_CLASS (sym) == LOC_STATIC
13734 && SYMBOL_VALUE_ADDRESS (sym) == 0
13735 && !dwarf2_per_objfile->has_section_at_zero)
13736 {
13737 /* When a static variable is eliminated by the linker,
13738 the corresponding debug information is not stripped
13739 out, but the variable address is set to null;
13740 do not add such variables into symbol table. */
13741 }
13742 else if (attr2 && (DW_UNSND (attr2) != 0))
13743 {
13744 /* Workaround gfortran PR debug/40040 - it uses
13745 DW_AT_location for variables in -fPIC libraries which may
13746 get overriden by other libraries/executable and get
13747 a different address. Resolve it by the minimal symbol
13748 which may come from inferior's executable using copy
13749 relocation. Make this workaround only for gfortran as for
13750 other compilers GDB cannot guess the minimal symbol
13751 Fortran mangling kind. */
13752 if (cu->language == language_fortran && die->parent
13753 && die->parent->tag == DW_TAG_module
13754 && cu->producer
13755 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
13756 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
13757
13758 /* A variable with DW_AT_external is never static,
13759 but it may be block-scoped. */
13760 list_to_add = (cu->list_in_scope == &file_symbols
13761 ? &global_symbols : cu->list_in_scope);
13762 }
13763 else
13764 list_to_add = cu->list_in_scope;
13765 }
13766 else
13767 {
13768 /* We do not know the address of this symbol.
13769 If it is an external symbol and we have type information
13770 for it, enter the symbol as a LOC_UNRESOLVED symbol.
13771 The address of the variable will then be determined from
13772 the minimal symbol table whenever the variable is
13773 referenced. */
13774 attr2 = dwarf2_attr (die, DW_AT_external, cu);
13775 if (attr2 && (DW_UNSND (attr2) != 0)
13776 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
13777 {
13778 /* A variable with DW_AT_external is never static, but it
13779 may be block-scoped. */
13780 list_to_add = (cu->list_in_scope == &file_symbols
13781 ? &global_symbols : cu->list_in_scope);
13782
13783 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
13784 }
13785 else if (!die_is_declaration (die, cu))
13786 {
13787 /* Use the default LOC_OPTIMIZED_OUT class. */
13788 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
13789 if (!suppress_add)
13790 list_to_add = cu->list_in_scope;
13791 }
13792 }
13793 break;
13794 case DW_TAG_formal_parameter:
13795 /* If we are inside a function, mark this as an argument. If
13796 not, we might be looking at an argument to an inlined function
13797 when we do not have enough information to show inlined frames;
13798 pretend it's a local variable in that case so that the user can
13799 still see it. */
13800 if (context_stack_depth > 0
13801 && context_stack[context_stack_depth - 1].name != NULL)
13802 SYMBOL_IS_ARGUMENT (sym) = 1;
13803 attr = dwarf2_attr (die, DW_AT_location, cu);
13804 if (attr)
13805 {
13806 var_decode_location (attr, sym, cu);
13807 }
13808 attr = dwarf2_attr (die, DW_AT_const_value, cu);
13809 if (attr)
13810 {
13811 dwarf2_const_value (attr, sym, cu);
13812 }
13813
13814 list_to_add = cu->list_in_scope;
13815 break;
13816 case DW_TAG_unspecified_parameters:
13817 /* From varargs functions; gdb doesn't seem to have any
13818 interest in this information, so just ignore it for now.
13819 (FIXME?) */
13820 break;
13821 case DW_TAG_template_type_param:
13822 suppress_add = 1;
13823 /* Fall through. */
13824 case DW_TAG_class_type:
13825 case DW_TAG_interface_type:
13826 case DW_TAG_structure_type:
13827 case DW_TAG_union_type:
13828 case DW_TAG_set_type:
13829 case DW_TAG_enumeration_type:
13830 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13831 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
13832
13833 {
13834 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
13835 really ever be static objects: otherwise, if you try
13836 to, say, break of a class's method and you're in a file
13837 which doesn't mention that class, it won't work unless
13838 the check for all static symbols in lookup_symbol_aux
13839 saves you. See the OtherFileClass tests in
13840 gdb.c++/namespace.exp. */
13841
13842 if (!suppress_add)
13843 {
13844 list_to_add = (cu->list_in_scope == &file_symbols
13845 && (cu->language == language_cplus
13846 || cu->language == language_java)
13847 ? &global_symbols : cu->list_in_scope);
13848
13849 /* The semantics of C++ state that "struct foo {
13850 ... }" also defines a typedef for "foo". A Java
13851 class declaration also defines a typedef for the
13852 class. */
13853 if (cu->language == language_cplus
13854 || cu->language == language_java
13855 || cu->language == language_ada)
13856 {
13857 /* The symbol's name is already allocated along
13858 with this objfile, so we don't need to
13859 duplicate it for the type. */
13860 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
13861 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
13862 }
13863 }
13864 }
13865 break;
13866 case DW_TAG_typedef:
13867 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13868 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
13869 list_to_add = cu->list_in_scope;
13870 break;
13871 case DW_TAG_base_type:
13872 case DW_TAG_subrange_type:
13873 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13874 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
13875 list_to_add = cu->list_in_scope;
13876 break;
13877 case DW_TAG_enumerator:
13878 attr = dwarf2_attr (die, DW_AT_const_value, cu);
13879 if (attr)
13880 {
13881 dwarf2_const_value (attr, sym, cu);
13882 }
13883 {
13884 /* NOTE: carlton/2003-11-10: See comment above in the
13885 DW_TAG_class_type, etc. block. */
13886
13887 list_to_add = (cu->list_in_scope == &file_symbols
13888 && (cu->language == language_cplus
13889 || cu->language == language_java)
13890 ? &global_symbols : cu->list_in_scope);
13891 }
13892 break;
13893 case DW_TAG_namespace:
13894 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13895 list_to_add = &global_symbols;
13896 break;
13897 default:
13898 /* Not a tag we recognize. Hopefully we aren't processing
13899 trash data, but since we must specifically ignore things
13900 we don't recognize, there is nothing else we should do at
13901 this point. */
13902 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
13903 dwarf_tag_name (die->tag));
13904 break;
13905 }
13906
13907 if (suppress_add)
13908 {
13909 sym->hash_next = objfile->template_symbols;
13910 objfile->template_symbols = sym;
13911 list_to_add = NULL;
13912 }
13913
13914 if (list_to_add != NULL)
13915 add_symbol_to_list (sym, list_to_add);
13916
13917 /* For the benefit of old versions of GCC, check for anonymous
13918 namespaces based on the demangled name. */
13919 if (!processing_has_namespace_info
13920 && cu->language == language_cplus)
13921 cp_scan_for_anonymous_namespaces (sym, objfile);
13922 }
13923 return (sym);
13924 }
13925
13926 /* A wrapper for new_symbol_full that always allocates a new symbol. */
13927
13928 static struct symbol *
13929 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
13930 {
13931 return new_symbol_full (die, type, cu, NULL);
13932 }
13933
13934 /* Given an attr with a DW_FORM_dataN value in host byte order,
13935 zero-extend it as appropriate for the symbol's type. The DWARF
13936 standard (v4) is not entirely clear about the meaning of using
13937 DW_FORM_dataN for a constant with a signed type, where the type is
13938 wider than the data. The conclusion of a discussion on the DWARF
13939 list was that this is unspecified. We choose to always zero-extend
13940 because that is the interpretation long in use by GCC. */
13941
13942 static gdb_byte *
13943 dwarf2_const_value_data (struct attribute *attr, struct type *type,
13944 const char *name, struct obstack *obstack,
13945 struct dwarf2_cu *cu, LONGEST *value, int bits)
13946 {
13947 struct objfile *objfile = cu->objfile;
13948 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
13949 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
13950 LONGEST l = DW_UNSND (attr);
13951
13952 if (bits < sizeof (*value) * 8)
13953 {
13954 l &= ((LONGEST) 1 << bits) - 1;
13955 *value = l;
13956 }
13957 else if (bits == sizeof (*value) * 8)
13958 *value = l;
13959 else
13960 {
13961 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
13962 store_unsigned_integer (bytes, bits / 8, byte_order, l);
13963 return bytes;
13964 }
13965
13966 return NULL;
13967 }
13968
13969 /* Read a constant value from an attribute. Either set *VALUE, or if
13970 the value does not fit in *VALUE, set *BYTES - either already
13971 allocated on the objfile obstack, or newly allocated on OBSTACK,
13972 or, set *BATON, if we translated the constant to a location
13973 expression. */
13974
13975 static void
13976 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
13977 const char *name, struct obstack *obstack,
13978 struct dwarf2_cu *cu,
13979 LONGEST *value, gdb_byte **bytes,
13980 struct dwarf2_locexpr_baton **baton)
13981 {
13982 struct objfile *objfile = cu->objfile;
13983 struct comp_unit_head *cu_header = &cu->header;
13984 struct dwarf_block *blk;
13985 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
13986 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
13987
13988 *value = 0;
13989 *bytes = NULL;
13990 *baton = NULL;
13991
13992 switch (attr->form)
13993 {
13994 case DW_FORM_addr:
13995 case DW_FORM_GNU_addr_index:
13996 {
13997 gdb_byte *data;
13998
13999 if (TYPE_LENGTH (type) != cu_header->addr_size)
14000 dwarf2_const_value_length_mismatch_complaint (name,
14001 cu_header->addr_size,
14002 TYPE_LENGTH (type));
14003 /* Symbols of this form are reasonably rare, so we just
14004 piggyback on the existing location code rather than writing
14005 a new implementation of symbol_computed_ops. */
14006 *baton = obstack_alloc (&objfile->objfile_obstack,
14007 sizeof (struct dwarf2_locexpr_baton));
14008 (*baton)->per_cu = cu->per_cu;
14009 gdb_assert ((*baton)->per_cu);
14010
14011 (*baton)->size = 2 + cu_header->addr_size;
14012 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
14013 (*baton)->data = data;
14014
14015 data[0] = DW_OP_addr;
14016 store_unsigned_integer (&data[1], cu_header->addr_size,
14017 byte_order, DW_ADDR (attr));
14018 data[cu_header->addr_size + 1] = DW_OP_stack_value;
14019 }
14020 break;
14021 case DW_FORM_string:
14022 case DW_FORM_strp:
14023 case DW_FORM_GNU_str_index:
14024 /* DW_STRING is already allocated on the objfile obstack, point
14025 directly to it. */
14026 *bytes = (gdb_byte *) DW_STRING (attr);
14027 break;
14028 case DW_FORM_block1:
14029 case DW_FORM_block2:
14030 case DW_FORM_block4:
14031 case DW_FORM_block:
14032 case DW_FORM_exprloc:
14033 blk = DW_BLOCK (attr);
14034 if (TYPE_LENGTH (type) != blk->size)
14035 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
14036 TYPE_LENGTH (type));
14037 *bytes = blk->data;
14038 break;
14039
14040 /* The DW_AT_const_value attributes are supposed to carry the
14041 symbol's value "represented as it would be on the target
14042 architecture." By the time we get here, it's already been
14043 converted to host endianness, so we just need to sign- or
14044 zero-extend it as appropriate. */
14045 case DW_FORM_data1:
14046 *bytes = dwarf2_const_value_data (attr, type, name,
14047 obstack, cu, value, 8);
14048 break;
14049 case DW_FORM_data2:
14050 *bytes = dwarf2_const_value_data (attr, type, name,
14051 obstack, cu, value, 16);
14052 break;
14053 case DW_FORM_data4:
14054 *bytes = dwarf2_const_value_data (attr, type, name,
14055 obstack, cu, value, 32);
14056 break;
14057 case DW_FORM_data8:
14058 *bytes = dwarf2_const_value_data (attr, type, name,
14059 obstack, cu, value, 64);
14060 break;
14061
14062 case DW_FORM_sdata:
14063 *value = DW_SND (attr);
14064 break;
14065
14066 case DW_FORM_udata:
14067 *value = DW_UNSND (attr);
14068 break;
14069
14070 default:
14071 complaint (&symfile_complaints,
14072 _("unsupported const value attribute form: '%s'"),
14073 dwarf_form_name (attr->form));
14074 *value = 0;
14075 break;
14076 }
14077 }
14078
14079
14080 /* Copy constant value from an attribute to a symbol. */
14081
14082 static void
14083 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
14084 struct dwarf2_cu *cu)
14085 {
14086 struct objfile *objfile = cu->objfile;
14087 struct comp_unit_head *cu_header = &cu->header;
14088 LONGEST value;
14089 gdb_byte *bytes;
14090 struct dwarf2_locexpr_baton *baton;
14091
14092 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
14093 SYMBOL_PRINT_NAME (sym),
14094 &objfile->objfile_obstack, cu,
14095 &value, &bytes, &baton);
14096
14097 if (baton != NULL)
14098 {
14099 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
14100 SYMBOL_LOCATION_BATON (sym) = baton;
14101 SYMBOL_CLASS (sym) = LOC_COMPUTED;
14102 }
14103 else if (bytes != NULL)
14104 {
14105 SYMBOL_VALUE_BYTES (sym) = bytes;
14106 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
14107 }
14108 else
14109 {
14110 SYMBOL_VALUE (sym) = value;
14111 SYMBOL_CLASS (sym) = LOC_CONST;
14112 }
14113 }
14114
14115 /* Return the type of the die in question using its DW_AT_type attribute. */
14116
14117 static struct type *
14118 die_type (struct die_info *die, struct dwarf2_cu *cu)
14119 {
14120 struct attribute *type_attr;
14121
14122 type_attr = dwarf2_attr (die, DW_AT_type, cu);
14123 if (!type_attr)
14124 {
14125 /* A missing DW_AT_type represents a void type. */
14126 return objfile_type (cu->objfile)->builtin_void;
14127 }
14128
14129 return lookup_die_type (die, type_attr, cu);
14130 }
14131
14132 /* True iff CU's producer generates GNAT Ada auxiliary information
14133 that allows to find parallel types through that information instead
14134 of having to do expensive parallel lookups by type name. */
14135
14136 static int
14137 need_gnat_info (struct dwarf2_cu *cu)
14138 {
14139 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
14140 of GNAT produces this auxiliary information, without any indication
14141 that it is produced. Part of enhancing the FSF version of GNAT
14142 to produce that information will be to put in place an indicator
14143 that we can use in order to determine whether the descriptive type
14144 info is available or not. One suggestion that has been made is
14145 to use a new attribute, attached to the CU die. For now, assume
14146 that the descriptive type info is not available. */
14147 return 0;
14148 }
14149
14150 /* Return the auxiliary type of the die in question using its
14151 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
14152 attribute is not present. */
14153
14154 static struct type *
14155 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
14156 {
14157 struct attribute *type_attr;
14158
14159 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
14160 if (!type_attr)
14161 return NULL;
14162
14163 return lookup_die_type (die, type_attr, cu);
14164 }
14165
14166 /* If DIE has a descriptive_type attribute, then set the TYPE's
14167 descriptive type accordingly. */
14168
14169 static void
14170 set_descriptive_type (struct type *type, struct die_info *die,
14171 struct dwarf2_cu *cu)
14172 {
14173 struct type *descriptive_type = die_descriptive_type (die, cu);
14174
14175 if (descriptive_type)
14176 {
14177 ALLOCATE_GNAT_AUX_TYPE (type);
14178 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
14179 }
14180 }
14181
14182 /* Return the containing type of the die in question using its
14183 DW_AT_containing_type attribute. */
14184
14185 static struct type *
14186 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14187 {
14188 struct attribute *type_attr;
14189
14190 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
14191 if (!type_attr)
14192 error (_("Dwarf Error: Problem turning containing type into gdb type "
14193 "[in module %s]"), cu->objfile->name);
14194
14195 return lookup_die_type (die, type_attr, cu);
14196 }
14197
14198 /* Look up the type of DIE in CU using its type attribute ATTR.
14199 If there is no type substitute an error marker. */
14200
14201 static struct type *
14202 lookup_die_type (struct die_info *die, struct attribute *attr,
14203 struct dwarf2_cu *cu)
14204 {
14205 struct objfile *objfile = cu->objfile;
14206 struct type *this_type;
14207
14208 /* First see if we have it cached. */
14209
14210 if (is_ref_attr (attr))
14211 {
14212 sect_offset offset = dwarf2_get_ref_die_offset (attr);
14213
14214 this_type = get_die_type_at_offset (offset, cu->per_cu);
14215 }
14216 else if (attr->form == DW_FORM_ref_sig8)
14217 {
14218 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
14219
14220 /* sig_type will be NULL if the signatured type is missing from
14221 the debug info. */
14222 if (sig_type == NULL)
14223 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
14224 "at 0x%x [in module %s]"),
14225 die->offset.sect_off, objfile->name);
14226
14227 gdb_assert (sig_type->per_cu.is_debug_types);
14228 /* If we haven't filled in type_offset_in_section yet, then we
14229 haven't read the type in yet. */
14230 this_type = NULL;
14231 if (sig_type->type_offset_in_section.sect_off != 0)
14232 {
14233 this_type =
14234 get_die_type_at_offset (sig_type->type_offset_in_section,
14235 &sig_type->per_cu);
14236 }
14237 }
14238 else
14239 {
14240 dump_die_for_error (die);
14241 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
14242 dwarf_attr_name (attr->name), objfile->name);
14243 }
14244
14245 /* If not cached we need to read it in. */
14246
14247 if (this_type == NULL)
14248 {
14249 struct die_info *type_die;
14250 struct dwarf2_cu *type_cu = cu;
14251
14252 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
14253 /* If we found the type now, it's probably because the type came
14254 from an inter-CU reference and the type's CU got expanded before
14255 ours. */
14256 this_type = get_die_type (type_die, type_cu);
14257 if (this_type == NULL)
14258 this_type = read_type_die_1 (type_die, type_cu);
14259 }
14260
14261 /* If we still don't have a type use an error marker. */
14262
14263 if (this_type == NULL)
14264 {
14265 char *message, *saved;
14266
14267 /* read_type_die already issued a complaint. */
14268 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
14269 objfile->name,
14270 cu->header.offset.sect_off,
14271 die->offset.sect_off);
14272 saved = obstack_copy0 (&objfile->objfile_obstack,
14273 message, strlen (message));
14274 xfree (message);
14275
14276 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
14277 }
14278
14279 return this_type;
14280 }
14281
14282 /* Return the type in DIE, CU.
14283 Returns NULL for invalid types.
14284
14285 This first does a lookup in the appropriate type_hash table,
14286 and only reads the die in if necessary.
14287
14288 NOTE: This can be called when reading in partial or full symbols. */
14289
14290 static struct type *
14291 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
14292 {
14293 struct type *this_type;
14294
14295 this_type = get_die_type (die, cu);
14296 if (this_type)
14297 return this_type;
14298
14299 return read_type_die_1 (die, cu);
14300 }
14301
14302 /* Read the type in DIE, CU.
14303 Returns NULL for invalid types. */
14304
14305 static struct type *
14306 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
14307 {
14308 struct type *this_type = NULL;
14309
14310 switch (die->tag)
14311 {
14312 case DW_TAG_class_type:
14313 case DW_TAG_interface_type:
14314 case DW_TAG_structure_type:
14315 case DW_TAG_union_type:
14316 this_type = read_structure_type (die, cu);
14317 break;
14318 case DW_TAG_enumeration_type:
14319 this_type = read_enumeration_type (die, cu);
14320 break;
14321 case DW_TAG_subprogram:
14322 case DW_TAG_subroutine_type:
14323 case DW_TAG_inlined_subroutine:
14324 this_type = read_subroutine_type (die, cu);
14325 break;
14326 case DW_TAG_array_type:
14327 this_type = read_array_type (die, cu);
14328 break;
14329 case DW_TAG_set_type:
14330 this_type = read_set_type (die, cu);
14331 break;
14332 case DW_TAG_pointer_type:
14333 this_type = read_tag_pointer_type (die, cu);
14334 break;
14335 case DW_TAG_ptr_to_member_type:
14336 this_type = read_tag_ptr_to_member_type (die, cu);
14337 break;
14338 case DW_TAG_reference_type:
14339 this_type = read_tag_reference_type (die, cu);
14340 break;
14341 case DW_TAG_const_type:
14342 this_type = read_tag_const_type (die, cu);
14343 break;
14344 case DW_TAG_volatile_type:
14345 this_type = read_tag_volatile_type (die, cu);
14346 break;
14347 case DW_TAG_string_type:
14348 this_type = read_tag_string_type (die, cu);
14349 break;
14350 case DW_TAG_typedef:
14351 this_type = read_typedef (die, cu);
14352 break;
14353 case DW_TAG_subrange_type:
14354 this_type = read_subrange_type (die, cu);
14355 break;
14356 case DW_TAG_base_type:
14357 this_type = read_base_type (die, cu);
14358 break;
14359 case DW_TAG_unspecified_type:
14360 this_type = read_unspecified_type (die, cu);
14361 break;
14362 case DW_TAG_namespace:
14363 this_type = read_namespace_type (die, cu);
14364 break;
14365 case DW_TAG_module:
14366 this_type = read_module_type (die, cu);
14367 break;
14368 default:
14369 complaint (&symfile_complaints,
14370 _("unexpected tag in read_type_die: '%s'"),
14371 dwarf_tag_name (die->tag));
14372 break;
14373 }
14374
14375 return this_type;
14376 }
14377
14378 /* See if we can figure out if the class lives in a namespace. We do
14379 this by looking for a member function; its demangled name will
14380 contain namespace info, if there is any.
14381 Return the computed name or NULL.
14382 Space for the result is allocated on the objfile's obstack.
14383 This is the full-die version of guess_partial_die_structure_name.
14384 In this case we know DIE has no useful parent. */
14385
14386 static char *
14387 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
14388 {
14389 struct die_info *spec_die;
14390 struct dwarf2_cu *spec_cu;
14391 struct die_info *child;
14392
14393 spec_cu = cu;
14394 spec_die = die_specification (die, &spec_cu);
14395 if (spec_die != NULL)
14396 {
14397 die = spec_die;
14398 cu = spec_cu;
14399 }
14400
14401 for (child = die->child;
14402 child != NULL;
14403 child = child->sibling)
14404 {
14405 if (child->tag == DW_TAG_subprogram)
14406 {
14407 struct attribute *attr;
14408
14409 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
14410 if (attr == NULL)
14411 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
14412 if (attr != NULL)
14413 {
14414 char *actual_name
14415 = language_class_name_from_physname (cu->language_defn,
14416 DW_STRING (attr));
14417 char *name = NULL;
14418
14419 if (actual_name != NULL)
14420 {
14421 char *die_name = dwarf2_name (die, cu);
14422
14423 if (die_name != NULL
14424 && strcmp (die_name, actual_name) != 0)
14425 {
14426 /* Strip off the class name from the full name.
14427 We want the prefix. */
14428 int die_name_len = strlen (die_name);
14429 int actual_name_len = strlen (actual_name);
14430
14431 /* Test for '::' as a sanity check. */
14432 if (actual_name_len > die_name_len + 2
14433 && actual_name[actual_name_len
14434 - die_name_len - 1] == ':')
14435 name =
14436 obsavestring (actual_name,
14437 actual_name_len - die_name_len - 2,
14438 &cu->objfile->objfile_obstack);
14439 }
14440 }
14441 xfree (actual_name);
14442 return name;
14443 }
14444 }
14445 }
14446
14447 return NULL;
14448 }
14449
14450 /* GCC might emit a nameless typedef that has a linkage name. Determine the
14451 prefix part in such case. See
14452 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14453
14454 static char *
14455 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
14456 {
14457 struct attribute *attr;
14458 char *base;
14459
14460 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
14461 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
14462 return NULL;
14463
14464 attr = dwarf2_attr (die, DW_AT_name, cu);
14465 if (attr != NULL && DW_STRING (attr) != NULL)
14466 return NULL;
14467
14468 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
14469 if (attr == NULL)
14470 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
14471 if (attr == NULL || DW_STRING (attr) == NULL)
14472 return NULL;
14473
14474 /* dwarf2_name had to be already called. */
14475 gdb_assert (DW_STRING_IS_CANONICAL (attr));
14476
14477 /* Strip the base name, keep any leading namespaces/classes. */
14478 base = strrchr (DW_STRING (attr), ':');
14479 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
14480 return "";
14481
14482 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
14483 &cu->objfile->objfile_obstack);
14484 }
14485
14486 /* Return the name of the namespace/class that DIE is defined within,
14487 or "" if we can't tell. The caller should not xfree the result.
14488
14489 For example, if we're within the method foo() in the following
14490 code:
14491
14492 namespace N {
14493 class C {
14494 void foo () {
14495 }
14496 };
14497 }
14498
14499 then determine_prefix on foo's die will return "N::C". */
14500
14501 static const char *
14502 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
14503 {
14504 struct die_info *parent, *spec_die;
14505 struct dwarf2_cu *spec_cu;
14506 struct type *parent_type;
14507 char *retval;
14508
14509 if (cu->language != language_cplus && cu->language != language_java
14510 && cu->language != language_fortran)
14511 return "";
14512
14513 retval = anonymous_struct_prefix (die, cu);
14514 if (retval)
14515 return retval;
14516
14517 /* We have to be careful in the presence of DW_AT_specification.
14518 For example, with GCC 3.4, given the code
14519
14520 namespace N {
14521 void foo() {
14522 // Definition of N::foo.
14523 }
14524 }
14525
14526 then we'll have a tree of DIEs like this:
14527
14528 1: DW_TAG_compile_unit
14529 2: DW_TAG_namespace // N
14530 3: DW_TAG_subprogram // declaration of N::foo
14531 4: DW_TAG_subprogram // definition of N::foo
14532 DW_AT_specification // refers to die #3
14533
14534 Thus, when processing die #4, we have to pretend that we're in
14535 the context of its DW_AT_specification, namely the contex of die
14536 #3. */
14537 spec_cu = cu;
14538 spec_die = die_specification (die, &spec_cu);
14539 if (spec_die == NULL)
14540 parent = die->parent;
14541 else
14542 {
14543 parent = spec_die->parent;
14544 cu = spec_cu;
14545 }
14546
14547 if (parent == NULL)
14548 return "";
14549 else if (parent->building_fullname)
14550 {
14551 const char *name;
14552 const char *parent_name;
14553
14554 /* It has been seen on RealView 2.2 built binaries,
14555 DW_TAG_template_type_param types actually _defined_ as
14556 children of the parent class:
14557
14558 enum E {};
14559 template class <class Enum> Class{};
14560 Class<enum E> class_e;
14561
14562 1: DW_TAG_class_type (Class)
14563 2: DW_TAG_enumeration_type (E)
14564 3: DW_TAG_enumerator (enum1:0)
14565 3: DW_TAG_enumerator (enum2:1)
14566 ...
14567 2: DW_TAG_template_type_param
14568 DW_AT_type DW_FORM_ref_udata (E)
14569
14570 Besides being broken debug info, it can put GDB into an
14571 infinite loop. Consider:
14572
14573 When we're building the full name for Class<E>, we'll start
14574 at Class, and go look over its template type parameters,
14575 finding E. We'll then try to build the full name of E, and
14576 reach here. We're now trying to build the full name of E,
14577 and look over the parent DIE for containing scope. In the
14578 broken case, if we followed the parent DIE of E, we'd again
14579 find Class, and once again go look at its template type
14580 arguments, etc., etc. Simply don't consider such parent die
14581 as source-level parent of this die (it can't be, the language
14582 doesn't allow it), and break the loop here. */
14583 name = dwarf2_name (die, cu);
14584 parent_name = dwarf2_name (parent, cu);
14585 complaint (&symfile_complaints,
14586 _("template param type '%s' defined within parent '%s'"),
14587 name ? name : "<unknown>",
14588 parent_name ? parent_name : "<unknown>");
14589 return "";
14590 }
14591 else
14592 switch (parent->tag)
14593 {
14594 case DW_TAG_namespace:
14595 parent_type = read_type_die (parent, cu);
14596 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
14597 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
14598 Work around this problem here. */
14599 if (cu->language == language_cplus
14600 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
14601 return "";
14602 /* We give a name to even anonymous namespaces. */
14603 return TYPE_TAG_NAME (parent_type);
14604 case DW_TAG_class_type:
14605 case DW_TAG_interface_type:
14606 case DW_TAG_structure_type:
14607 case DW_TAG_union_type:
14608 case DW_TAG_module:
14609 parent_type = read_type_die (parent, cu);
14610 if (TYPE_TAG_NAME (parent_type) != NULL)
14611 return TYPE_TAG_NAME (parent_type);
14612 else
14613 /* An anonymous structure is only allowed non-static data
14614 members; no typedefs, no member functions, et cetera.
14615 So it does not need a prefix. */
14616 return "";
14617 case DW_TAG_compile_unit:
14618 case DW_TAG_partial_unit:
14619 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
14620 if (cu->language == language_cplus
14621 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
14622 && die->child != NULL
14623 && (die->tag == DW_TAG_class_type
14624 || die->tag == DW_TAG_structure_type
14625 || die->tag == DW_TAG_union_type))
14626 {
14627 char *name = guess_full_die_structure_name (die, cu);
14628 if (name != NULL)
14629 return name;
14630 }
14631 return "";
14632 default:
14633 return determine_prefix (parent, cu);
14634 }
14635 }
14636
14637 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
14638 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
14639 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
14640 an obconcat, otherwise allocate storage for the result. The CU argument is
14641 used to determine the language and hence, the appropriate separator. */
14642
14643 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
14644
14645 static char *
14646 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
14647 int physname, struct dwarf2_cu *cu)
14648 {
14649 const char *lead = "";
14650 const char *sep;
14651
14652 if (suffix == NULL || suffix[0] == '\0'
14653 || prefix == NULL || prefix[0] == '\0')
14654 sep = "";
14655 else if (cu->language == language_java)
14656 sep = ".";
14657 else if (cu->language == language_fortran && physname)
14658 {
14659 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
14660 DW_AT_MIPS_linkage_name is preferred and used instead. */
14661
14662 lead = "__";
14663 sep = "_MOD_";
14664 }
14665 else
14666 sep = "::";
14667
14668 if (prefix == NULL)
14669 prefix = "";
14670 if (suffix == NULL)
14671 suffix = "";
14672
14673 if (obs == NULL)
14674 {
14675 char *retval
14676 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
14677
14678 strcpy (retval, lead);
14679 strcat (retval, prefix);
14680 strcat (retval, sep);
14681 strcat (retval, suffix);
14682 return retval;
14683 }
14684 else
14685 {
14686 /* We have an obstack. */
14687 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
14688 }
14689 }
14690
14691 /* Return sibling of die, NULL if no sibling. */
14692
14693 static struct die_info *
14694 sibling_die (struct die_info *die)
14695 {
14696 return die->sibling;
14697 }
14698
14699 /* Get name of a die, return NULL if not found. */
14700
14701 static char *
14702 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
14703 struct obstack *obstack)
14704 {
14705 if (name && cu->language == language_cplus)
14706 {
14707 char *canon_name = cp_canonicalize_string (name);
14708
14709 if (canon_name != NULL)
14710 {
14711 if (strcmp (canon_name, name) != 0)
14712 name = obsavestring (canon_name, strlen (canon_name),
14713 obstack);
14714 xfree (canon_name);
14715 }
14716 }
14717
14718 return name;
14719 }
14720
14721 /* Get name of a die, return NULL if not found. */
14722
14723 static char *
14724 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
14725 {
14726 struct attribute *attr;
14727
14728 attr = dwarf2_attr (die, DW_AT_name, cu);
14729 if ((!attr || !DW_STRING (attr))
14730 && die->tag != DW_TAG_class_type
14731 && die->tag != DW_TAG_interface_type
14732 && die->tag != DW_TAG_structure_type
14733 && die->tag != DW_TAG_union_type)
14734 return NULL;
14735
14736 switch (die->tag)
14737 {
14738 case DW_TAG_compile_unit:
14739 case DW_TAG_partial_unit:
14740 /* Compilation units have a DW_AT_name that is a filename, not
14741 a source language identifier. */
14742 case DW_TAG_enumeration_type:
14743 case DW_TAG_enumerator:
14744 /* These tags always have simple identifiers already; no need
14745 to canonicalize them. */
14746 return DW_STRING (attr);
14747
14748 case DW_TAG_subprogram:
14749 /* Java constructors will all be named "<init>", so return
14750 the class name when we see this special case. */
14751 if (cu->language == language_java
14752 && DW_STRING (attr) != NULL
14753 && strcmp (DW_STRING (attr), "<init>") == 0)
14754 {
14755 struct dwarf2_cu *spec_cu = cu;
14756 struct die_info *spec_die;
14757
14758 /* GCJ will output '<init>' for Java constructor names.
14759 For this special case, return the name of the parent class. */
14760
14761 /* GCJ may output suprogram DIEs with AT_specification set.
14762 If so, use the name of the specified DIE. */
14763 spec_die = die_specification (die, &spec_cu);
14764 if (spec_die != NULL)
14765 return dwarf2_name (spec_die, spec_cu);
14766
14767 do
14768 {
14769 die = die->parent;
14770 if (die->tag == DW_TAG_class_type)
14771 return dwarf2_name (die, cu);
14772 }
14773 while (die->tag != DW_TAG_compile_unit
14774 && die->tag != DW_TAG_partial_unit);
14775 }
14776 break;
14777
14778 case DW_TAG_class_type:
14779 case DW_TAG_interface_type:
14780 case DW_TAG_structure_type:
14781 case DW_TAG_union_type:
14782 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
14783 structures or unions. These were of the form "._%d" in GCC 4.1,
14784 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
14785 and GCC 4.4. We work around this problem by ignoring these. */
14786 if (attr && DW_STRING (attr)
14787 && (strncmp (DW_STRING (attr), "._", 2) == 0
14788 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
14789 return NULL;
14790
14791 /* GCC might emit a nameless typedef that has a linkage name. See
14792 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14793 if (!attr || DW_STRING (attr) == NULL)
14794 {
14795 char *demangled = NULL;
14796
14797 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
14798 if (attr == NULL)
14799 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
14800
14801 if (attr == NULL || DW_STRING (attr) == NULL)
14802 return NULL;
14803
14804 /* Avoid demangling DW_STRING (attr) the second time on a second
14805 call for the same DIE. */
14806 if (!DW_STRING_IS_CANONICAL (attr))
14807 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
14808
14809 if (demangled)
14810 {
14811 char *base;
14812
14813 /* FIXME: we already did this for the partial symbol... */
14814 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
14815 &cu->objfile->objfile_obstack);
14816 DW_STRING_IS_CANONICAL (attr) = 1;
14817 xfree (demangled);
14818
14819 /* Strip any leading namespaces/classes, keep only the base name.
14820 DW_AT_name for named DIEs does not contain the prefixes. */
14821 base = strrchr (DW_STRING (attr), ':');
14822 if (base && base > DW_STRING (attr) && base[-1] == ':')
14823 return &base[1];
14824 else
14825 return DW_STRING (attr);
14826 }
14827 }
14828 break;
14829
14830 default:
14831 break;
14832 }
14833
14834 if (!DW_STRING_IS_CANONICAL (attr))
14835 {
14836 DW_STRING (attr)
14837 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
14838 &cu->objfile->objfile_obstack);
14839 DW_STRING_IS_CANONICAL (attr) = 1;
14840 }
14841 return DW_STRING (attr);
14842 }
14843
14844 /* Return the die that this die in an extension of, or NULL if there
14845 is none. *EXT_CU is the CU containing DIE on input, and the CU
14846 containing the return value on output. */
14847
14848 static struct die_info *
14849 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
14850 {
14851 struct attribute *attr;
14852
14853 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
14854 if (attr == NULL)
14855 return NULL;
14856
14857 return follow_die_ref (die, attr, ext_cu);
14858 }
14859
14860 /* Convert a DIE tag into its string name. */
14861
14862 static const char *
14863 dwarf_tag_name (unsigned tag)
14864 {
14865 const char *name = get_DW_TAG_name (tag);
14866
14867 if (name == NULL)
14868 return "DW_TAG_<unknown>";
14869
14870 return name;
14871 }
14872
14873 /* Convert a DWARF attribute code into its string name. */
14874
14875 static const char *
14876 dwarf_attr_name (unsigned attr)
14877 {
14878 const char *name;
14879
14880 #ifdef MIPS /* collides with DW_AT_HP_block_index */
14881 if (attr == DW_AT_MIPS_fde)
14882 return "DW_AT_MIPS_fde";
14883 #else
14884 if (attr == DW_AT_HP_block_index)
14885 return "DW_AT_HP_block_index";
14886 #endif
14887
14888 name = get_DW_AT_name (attr);
14889
14890 if (name == NULL)
14891 return "DW_AT_<unknown>";
14892
14893 return name;
14894 }
14895
14896 /* Convert a DWARF value form code into its string name. */
14897
14898 static const char *
14899 dwarf_form_name (unsigned form)
14900 {
14901 const char *name = get_DW_FORM_name (form);
14902
14903 if (name == NULL)
14904 return "DW_FORM_<unknown>";
14905
14906 return name;
14907 }
14908
14909 static char *
14910 dwarf_bool_name (unsigned mybool)
14911 {
14912 if (mybool)
14913 return "TRUE";
14914 else
14915 return "FALSE";
14916 }
14917
14918 /* Convert a DWARF type code into its string name. */
14919
14920 static const char *
14921 dwarf_type_encoding_name (unsigned enc)
14922 {
14923 const char *name = get_DW_ATE_name (enc);
14924
14925 if (name == NULL)
14926 return "DW_ATE_<unknown>";
14927
14928 return name;
14929 }
14930
14931 static void
14932 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
14933 {
14934 unsigned int i;
14935
14936 print_spaces (indent, f);
14937 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
14938 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
14939
14940 if (die->parent != NULL)
14941 {
14942 print_spaces (indent, f);
14943 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
14944 die->parent->offset.sect_off);
14945 }
14946
14947 print_spaces (indent, f);
14948 fprintf_unfiltered (f, " has children: %s\n",
14949 dwarf_bool_name (die->child != NULL));
14950
14951 print_spaces (indent, f);
14952 fprintf_unfiltered (f, " attributes:\n");
14953
14954 for (i = 0; i < die->num_attrs; ++i)
14955 {
14956 print_spaces (indent, f);
14957 fprintf_unfiltered (f, " %s (%s) ",
14958 dwarf_attr_name (die->attrs[i].name),
14959 dwarf_form_name (die->attrs[i].form));
14960
14961 switch (die->attrs[i].form)
14962 {
14963 case DW_FORM_addr:
14964 case DW_FORM_GNU_addr_index:
14965 fprintf_unfiltered (f, "address: ");
14966 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
14967 break;
14968 case DW_FORM_block2:
14969 case DW_FORM_block4:
14970 case DW_FORM_block:
14971 case DW_FORM_block1:
14972 fprintf_unfiltered (f, "block: size %d",
14973 DW_BLOCK (&die->attrs[i])->size);
14974 break;
14975 case DW_FORM_exprloc:
14976 fprintf_unfiltered (f, "expression: size %u",
14977 DW_BLOCK (&die->attrs[i])->size);
14978 break;
14979 case DW_FORM_ref_addr:
14980 fprintf_unfiltered (f, "ref address: ");
14981 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
14982 break;
14983 case DW_FORM_ref1:
14984 case DW_FORM_ref2:
14985 case DW_FORM_ref4:
14986 case DW_FORM_ref8:
14987 case DW_FORM_ref_udata:
14988 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
14989 (long) (DW_UNSND (&die->attrs[i])));
14990 break;
14991 case DW_FORM_data1:
14992 case DW_FORM_data2:
14993 case DW_FORM_data4:
14994 case DW_FORM_data8:
14995 case DW_FORM_udata:
14996 case DW_FORM_sdata:
14997 fprintf_unfiltered (f, "constant: %s",
14998 pulongest (DW_UNSND (&die->attrs[i])));
14999 break;
15000 case DW_FORM_sec_offset:
15001 fprintf_unfiltered (f, "section offset: %s",
15002 pulongest (DW_UNSND (&die->attrs[i])));
15003 break;
15004 case DW_FORM_ref_sig8:
15005 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
15006 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
15007 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
15008 else
15009 fprintf_unfiltered (f, "signatured type, offset: unknown");
15010 break;
15011 case DW_FORM_string:
15012 case DW_FORM_strp:
15013 case DW_FORM_GNU_str_index:
15014 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
15015 DW_STRING (&die->attrs[i])
15016 ? DW_STRING (&die->attrs[i]) : "",
15017 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
15018 break;
15019 case DW_FORM_flag:
15020 if (DW_UNSND (&die->attrs[i]))
15021 fprintf_unfiltered (f, "flag: TRUE");
15022 else
15023 fprintf_unfiltered (f, "flag: FALSE");
15024 break;
15025 case DW_FORM_flag_present:
15026 fprintf_unfiltered (f, "flag: TRUE");
15027 break;
15028 case DW_FORM_indirect:
15029 /* The reader will have reduced the indirect form to
15030 the "base form" so this form should not occur. */
15031 fprintf_unfiltered (f,
15032 "unexpected attribute form: DW_FORM_indirect");
15033 break;
15034 default:
15035 fprintf_unfiltered (f, "unsupported attribute form: %d.",
15036 die->attrs[i].form);
15037 break;
15038 }
15039 fprintf_unfiltered (f, "\n");
15040 }
15041 }
15042
15043 static void
15044 dump_die_for_error (struct die_info *die)
15045 {
15046 dump_die_shallow (gdb_stderr, 0, die);
15047 }
15048
15049 static void
15050 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
15051 {
15052 int indent = level * 4;
15053
15054 gdb_assert (die != NULL);
15055
15056 if (level >= max_level)
15057 return;
15058
15059 dump_die_shallow (f, indent, die);
15060
15061 if (die->child != NULL)
15062 {
15063 print_spaces (indent, f);
15064 fprintf_unfiltered (f, " Children:");
15065 if (level + 1 < max_level)
15066 {
15067 fprintf_unfiltered (f, "\n");
15068 dump_die_1 (f, level + 1, max_level, die->child);
15069 }
15070 else
15071 {
15072 fprintf_unfiltered (f,
15073 " [not printed, max nesting level reached]\n");
15074 }
15075 }
15076
15077 if (die->sibling != NULL && level > 0)
15078 {
15079 dump_die_1 (f, level, max_level, die->sibling);
15080 }
15081 }
15082
15083 /* This is called from the pdie macro in gdbinit.in.
15084 It's not static so gcc will keep a copy callable from gdb. */
15085
15086 void
15087 dump_die (struct die_info *die, int max_level)
15088 {
15089 dump_die_1 (gdb_stdlog, 0, max_level, die);
15090 }
15091
15092 static void
15093 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
15094 {
15095 void **slot;
15096
15097 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
15098 INSERT);
15099
15100 *slot = die;
15101 }
15102
15103 /* DW_ADDR is always stored already as sect_offset; despite for the forms
15104 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
15105
15106 static int
15107 is_ref_attr (struct attribute *attr)
15108 {
15109 switch (attr->form)
15110 {
15111 case DW_FORM_ref_addr:
15112 case DW_FORM_ref1:
15113 case DW_FORM_ref2:
15114 case DW_FORM_ref4:
15115 case DW_FORM_ref8:
15116 case DW_FORM_ref_udata:
15117 return 1;
15118 default:
15119 return 0;
15120 }
15121 }
15122
15123 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
15124 required kind. */
15125
15126 static sect_offset
15127 dwarf2_get_ref_die_offset (struct attribute *attr)
15128 {
15129 sect_offset retval = { DW_UNSND (attr) };
15130
15131 if (is_ref_attr (attr))
15132 return retval;
15133
15134 retval.sect_off = 0;
15135 complaint (&symfile_complaints,
15136 _("unsupported die ref attribute form: '%s'"),
15137 dwarf_form_name (attr->form));
15138 return retval;
15139 }
15140
15141 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
15142 * the value held by the attribute is not constant. */
15143
15144 static LONGEST
15145 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
15146 {
15147 if (attr->form == DW_FORM_sdata)
15148 return DW_SND (attr);
15149 else if (attr->form == DW_FORM_udata
15150 || attr->form == DW_FORM_data1
15151 || attr->form == DW_FORM_data2
15152 || attr->form == DW_FORM_data4
15153 || attr->form == DW_FORM_data8)
15154 return DW_UNSND (attr);
15155 else
15156 {
15157 complaint (&symfile_complaints,
15158 _("Attribute value is not a constant (%s)"),
15159 dwarf_form_name (attr->form));
15160 return default_value;
15161 }
15162 }
15163
15164 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
15165 unit and add it to our queue.
15166 The result is non-zero if PER_CU was queued, otherwise the result is zero
15167 meaning either PER_CU is already queued or it is already loaded. */
15168
15169 static int
15170 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
15171 struct dwarf2_per_cu_data *per_cu,
15172 enum language pretend_language)
15173 {
15174 /* We may arrive here during partial symbol reading, if we need full
15175 DIEs to process an unusual case (e.g. template arguments). Do
15176 not queue PER_CU, just tell our caller to load its DIEs. */
15177 if (dwarf2_per_objfile->reading_partial_symbols)
15178 {
15179 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
15180 return 1;
15181 return 0;
15182 }
15183
15184 /* Mark the dependence relation so that we don't flush PER_CU
15185 too early. */
15186 dwarf2_add_dependence (this_cu, per_cu);
15187
15188 /* If it's already on the queue, we have nothing to do. */
15189 if (per_cu->queued)
15190 return 0;
15191
15192 /* If the compilation unit is already loaded, just mark it as
15193 used. */
15194 if (per_cu->cu != NULL)
15195 {
15196 per_cu->cu->last_used = 0;
15197 return 0;
15198 }
15199
15200 /* Add it to the queue. */
15201 queue_comp_unit (per_cu, pretend_language);
15202
15203 return 1;
15204 }
15205
15206 /* Follow reference or signature attribute ATTR of SRC_DIE.
15207 On entry *REF_CU is the CU of SRC_DIE.
15208 On exit *REF_CU is the CU of the result. */
15209
15210 static struct die_info *
15211 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
15212 struct dwarf2_cu **ref_cu)
15213 {
15214 struct die_info *die;
15215
15216 if (is_ref_attr (attr))
15217 die = follow_die_ref (src_die, attr, ref_cu);
15218 else if (attr->form == DW_FORM_ref_sig8)
15219 die = follow_die_sig (src_die, attr, ref_cu);
15220 else
15221 {
15222 dump_die_for_error (src_die);
15223 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
15224 (*ref_cu)->objfile->name);
15225 }
15226
15227 return die;
15228 }
15229
15230 /* Follow reference OFFSET.
15231 On entry *REF_CU is the CU of the source die referencing OFFSET.
15232 On exit *REF_CU is the CU of the result.
15233 Returns NULL if OFFSET is invalid. */
15234
15235 static struct die_info *
15236 follow_die_offset (sect_offset offset, struct dwarf2_cu **ref_cu)
15237 {
15238 struct die_info temp_die;
15239 struct dwarf2_cu *target_cu, *cu = *ref_cu;
15240
15241 gdb_assert (cu->per_cu != NULL);
15242
15243 target_cu = cu;
15244
15245 if (cu->per_cu->is_debug_types)
15246 {
15247 /* .debug_types CUs cannot reference anything outside their CU.
15248 If they need to, they have to reference a signatured type via
15249 DW_FORM_ref_sig8. */
15250 if (! offset_in_cu_p (&cu->header, offset))
15251 return NULL;
15252 }
15253 else if (! offset_in_cu_p (&cu->header, offset))
15254 {
15255 struct dwarf2_per_cu_data *per_cu;
15256
15257 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
15258
15259 /* If necessary, add it to the queue and load its DIEs. */
15260 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
15261 load_full_comp_unit (per_cu, cu->language);
15262
15263 target_cu = per_cu->cu;
15264 }
15265 else if (cu->dies == NULL)
15266 {
15267 /* We're loading full DIEs during partial symbol reading. */
15268 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
15269 load_full_comp_unit (cu->per_cu, language_minimal);
15270 }
15271
15272 *ref_cu = target_cu;
15273 temp_die.offset = offset;
15274 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
15275 }
15276
15277 /* Follow reference attribute ATTR of SRC_DIE.
15278 On entry *REF_CU is the CU of SRC_DIE.
15279 On exit *REF_CU is the CU of the result. */
15280
15281 static struct die_info *
15282 follow_die_ref (struct die_info *src_die, struct attribute *attr,
15283 struct dwarf2_cu **ref_cu)
15284 {
15285 sect_offset offset = dwarf2_get_ref_die_offset (attr);
15286 struct dwarf2_cu *cu = *ref_cu;
15287 struct die_info *die;
15288
15289 die = follow_die_offset (offset, ref_cu);
15290 if (!die)
15291 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
15292 "at 0x%x [in module %s]"),
15293 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
15294
15295 return die;
15296 }
15297
15298 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
15299 Returned value is intended for DW_OP_call*. Returned
15300 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
15301
15302 struct dwarf2_locexpr_baton
15303 dwarf2_fetch_die_location_block (cu_offset offset_in_cu,
15304 struct dwarf2_per_cu_data *per_cu,
15305 CORE_ADDR (*get_frame_pc) (void *baton),
15306 void *baton)
15307 {
15308 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
15309 struct dwarf2_cu *cu;
15310 struct die_info *die;
15311 struct attribute *attr;
15312 struct dwarf2_locexpr_baton retval;
15313
15314 dw2_setup (per_cu->objfile);
15315
15316 if (per_cu->cu == NULL)
15317 load_cu (per_cu);
15318 cu = per_cu->cu;
15319
15320 die = follow_die_offset (offset, &cu);
15321 if (!die)
15322 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
15323 offset.sect_off, per_cu->objfile->name);
15324
15325 attr = dwarf2_attr (die, DW_AT_location, cu);
15326 if (!attr)
15327 {
15328 /* DWARF: "If there is no such attribute, then there is no effect.".
15329 DATA is ignored if SIZE is 0. */
15330
15331 retval.data = NULL;
15332 retval.size = 0;
15333 }
15334 else if (attr_form_is_section_offset (attr))
15335 {
15336 struct dwarf2_loclist_baton loclist_baton;
15337 CORE_ADDR pc = (*get_frame_pc) (baton);
15338 size_t size;
15339
15340 fill_in_loclist_baton (cu, &loclist_baton, attr);
15341
15342 retval.data = dwarf2_find_location_expression (&loclist_baton,
15343 &size, pc);
15344 retval.size = size;
15345 }
15346 else
15347 {
15348 if (!attr_form_is_block (attr))
15349 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
15350 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
15351 offset.sect_off, per_cu->objfile->name);
15352
15353 retval.data = DW_BLOCK (attr)->data;
15354 retval.size = DW_BLOCK (attr)->size;
15355 }
15356 retval.per_cu = cu->per_cu;
15357
15358 age_cached_comp_units ();
15359
15360 return retval;
15361 }
15362
15363 /* Return the type of the DIE at DIE_OFFSET in the CU named by
15364 PER_CU. */
15365
15366 struct type *
15367 dwarf2_get_die_type (cu_offset die_offset,
15368 struct dwarf2_per_cu_data *per_cu)
15369 {
15370 sect_offset die_offset_sect;
15371
15372 dw2_setup (per_cu->objfile);
15373
15374 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
15375 return get_die_type_at_offset (die_offset_sect, per_cu);
15376 }
15377
15378 /* Follow the signature attribute ATTR in SRC_DIE.
15379 On entry *REF_CU is the CU of SRC_DIE.
15380 On exit *REF_CU is the CU of the result. */
15381
15382 static struct die_info *
15383 follow_die_sig (struct die_info *src_die, struct attribute *attr,
15384 struct dwarf2_cu **ref_cu)
15385 {
15386 struct objfile *objfile = (*ref_cu)->objfile;
15387 struct die_info temp_die;
15388 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
15389 struct dwarf2_cu *sig_cu;
15390 struct die_info *die;
15391
15392 /* sig_type will be NULL if the signatured type is missing from
15393 the debug info. */
15394 if (sig_type == NULL)
15395 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
15396 "at 0x%x [in module %s]"),
15397 src_die->offset.sect_off, objfile->name);
15398
15399 /* If necessary, add it to the queue and load its DIEs. */
15400
15401 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
15402 read_signatured_type (sig_type);
15403
15404 gdb_assert (sig_type->per_cu.cu != NULL);
15405
15406 sig_cu = sig_type->per_cu.cu;
15407 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
15408 temp_die.offset = sig_type->type_offset_in_section;
15409 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
15410 temp_die.offset.sect_off);
15411 if (die)
15412 {
15413 *ref_cu = sig_cu;
15414 return die;
15415 }
15416
15417 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
15418 "from DIE at 0x%x [in module %s]"),
15419 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
15420 }
15421
15422 /* Given an offset of a signatured type, return its signatured_type. */
15423
15424 static struct signatured_type *
15425 lookup_signatured_type_at_offset (struct objfile *objfile,
15426 struct dwarf2_section_info *section,
15427 sect_offset offset)
15428 {
15429 gdb_byte *info_ptr = section->buffer + offset.sect_off;
15430 unsigned int length, initial_length_size;
15431 unsigned int sig_offset;
15432 struct signatured_type find_entry, *sig_type;
15433
15434 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
15435 sig_offset = (initial_length_size
15436 + 2 /*version*/
15437 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
15438 + 1 /*address_size*/);
15439 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
15440 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
15441
15442 /* This is only used to lookup previously recorded types.
15443 If we didn't find it, it's our bug. */
15444 gdb_assert (sig_type != NULL);
15445 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
15446
15447 return sig_type;
15448 }
15449
15450 /* Load the DIEs associated with type unit PER_CU into memory. */
15451
15452 static void
15453 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
15454 {
15455 struct objfile *objfile = per_cu->objfile;
15456 struct dwarf2_section_info *sect = per_cu->info_or_types_section;
15457 sect_offset offset = per_cu->offset;
15458 struct signatured_type *sig_type;
15459
15460 dwarf2_read_section (objfile, sect);
15461
15462 /* We have the section offset, but we need the signature to do the
15463 hash table lookup. */
15464 /* FIXME: This is sorta unnecessary, read_signatured_type only uses
15465 the signature to assert we found the right one.
15466 Ok, but it's a lot of work. We should simplify things so any needed
15467 assert doesn't require all this clumsiness. */
15468 sig_type = lookup_signatured_type_at_offset (objfile, sect, offset);
15469
15470 gdb_assert (&sig_type->per_cu == per_cu);
15471 gdb_assert (sig_type->per_cu.cu == NULL);
15472
15473 read_signatured_type (sig_type);
15474
15475 gdb_assert (sig_type->per_cu.cu != NULL);
15476 }
15477
15478 /* die_reader_func for read_signatured_type.
15479 This is identical to load_full_comp_unit_reader,
15480 but is kept separate for now. */
15481
15482 static void
15483 read_signatured_type_reader (const struct die_reader_specs *reader,
15484 gdb_byte *info_ptr,
15485 struct die_info *comp_unit_die,
15486 int has_children,
15487 void *data)
15488 {
15489 struct dwarf2_cu *cu = reader->cu;
15490
15491 gdb_assert (cu->die_hash == NULL);
15492 cu->die_hash =
15493 htab_create_alloc_ex (cu->header.length / 12,
15494 die_hash,
15495 die_eq,
15496 NULL,
15497 &cu->comp_unit_obstack,
15498 hashtab_obstack_allocate,
15499 dummy_obstack_deallocate);
15500
15501 if (has_children)
15502 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
15503 &info_ptr, comp_unit_die);
15504 cu->dies = comp_unit_die;
15505 /* comp_unit_die is not stored in die_hash, no need. */
15506
15507 /* We try not to read any attributes in this function, because not
15508 all CUs needed for references have been loaded yet, and symbol
15509 table processing isn't initialized. But we have to set the CU language,
15510 or we won't be able to build types correctly.
15511 Similarly, if we do not read the producer, we can not apply
15512 producer-specific interpretation. */
15513 prepare_one_comp_unit (cu, cu->dies, language_minimal);
15514 }
15515
15516 /* Read in a signatured type and build its CU and DIEs.
15517 If the type is a stub for the real type in a DWO file,
15518 read in the real type from the DWO file as well. */
15519
15520 static void
15521 read_signatured_type (struct signatured_type *sig_type)
15522 {
15523 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
15524
15525 gdb_assert (per_cu->is_debug_types);
15526 gdb_assert (per_cu->cu == NULL);
15527
15528 init_cutu_and_read_dies (per_cu, 0, 1, read_signatured_type_reader, NULL);
15529 }
15530
15531 /* Decode simple location descriptions.
15532 Given a pointer to a dwarf block that defines a location, compute
15533 the location and return the value.
15534
15535 NOTE drow/2003-11-18: This function is called in two situations
15536 now: for the address of static or global variables (partial symbols
15537 only) and for offsets into structures which are expected to be
15538 (more or less) constant. The partial symbol case should go away,
15539 and only the constant case should remain. That will let this
15540 function complain more accurately. A few special modes are allowed
15541 without complaint for global variables (for instance, global
15542 register values and thread-local values).
15543
15544 A location description containing no operations indicates that the
15545 object is optimized out. The return value is 0 for that case.
15546 FIXME drow/2003-11-16: No callers check for this case any more; soon all
15547 callers will only want a very basic result and this can become a
15548 complaint.
15549
15550 Note that stack[0] is unused except as a default error return. */
15551
15552 static CORE_ADDR
15553 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
15554 {
15555 struct objfile *objfile = cu->objfile;
15556 int i;
15557 int size = blk->size;
15558 gdb_byte *data = blk->data;
15559 CORE_ADDR stack[64];
15560 int stacki;
15561 unsigned int bytes_read, unsnd;
15562 gdb_byte op;
15563
15564 i = 0;
15565 stacki = 0;
15566 stack[stacki] = 0;
15567 stack[++stacki] = 0;
15568
15569 while (i < size)
15570 {
15571 op = data[i++];
15572 switch (op)
15573 {
15574 case DW_OP_lit0:
15575 case DW_OP_lit1:
15576 case DW_OP_lit2:
15577 case DW_OP_lit3:
15578 case DW_OP_lit4:
15579 case DW_OP_lit5:
15580 case DW_OP_lit6:
15581 case DW_OP_lit7:
15582 case DW_OP_lit8:
15583 case DW_OP_lit9:
15584 case DW_OP_lit10:
15585 case DW_OP_lit11:
15586 case DW_OP_lit12:
15587 case DW_OP_lit13:
15588 case DW_OP_lit14:
15589 case DW_OP_lit15:
15590 case DW_OP_lit16:
15591 case DW_OP_lit17:
15592 case DW_OP_lit18:
15593 case DW_OP_lit19:
15594 case DW_OP_lit20:
15595 case DW_OP_lit21:
15596 case DW_OP_lit22:
15597 case DW_OP_lit23:
15598 case DW_OP_lit24:
15599 case DW_OP_lit25:
15600 case DW_OP_lit26:
15601 case DW_OP_lit27:
15602 case DW_OP_lit28:
15603 case DW_OP_lit29:
15604 case DW_OP_lit30:
15605 case DW_OP_lit31:
15606 stack[++stacki] = op - DW_OP_lit0;
15607 break;
15608
15609 case DW_OP_reg0:
15610 case DW_OP_reg1:
15611 case DW_OP_reg2:
15612 case DW_OP_reg3:
15613 case DW_OP_reg4:
15614 case DW_OP_reg5:
15615 case DW_OP_reg6:
15616 case DW_OP_reg7:
15617 case DW_OP_reg8:
15618 case DW_OP_reg9:
15619 case DW_OP_reg10:
15620 case DW_OP_reg11:
15621 case DW_OP_reg12:
15622 case DW_OP_reg13:
15623 case DW_OP_reg14:
15624 case DW_OP_reg15:
15625 case DW_OP_reg16:
15626 case DW_OP_reg17:
15627 case DW_OP_reg18:
15628 case DW_OP_reg19:
15629 case DW_OP_reg20:
15630 case DW_OP_reg21:
15631 case DW_OP_reg22:
15632 case DW_OP_reg23:
15633 case DW_OP_reg24:
15634 case DW_OP_reg25:
15635 case DW_OP_reg26:
15636 case DW_OP_reg27:
15637 case DW_OP_reg28:
15638 case DW_OP_reg29:
15639 case DW_OP_reg30:
15640 case DW_OP_reg31:
15641 stack[++stacki] = op - DW_OP_reg0;
15642 if (i < size)
15643 dwarf2_complex_location_expr_complaint ();
15644 break;
15645
15646 case DW_OP_regx:
15647 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
15648 i += bytes_read;
15649 stack[++stacki] = unsnd;
15650 if (i < size)
15651 dwarf2_complex_location_expr_complaint ();
15652 break;
15653
15654 case DW_OP_addr:
15655 stack[++stacki] = read_address (objfile->obfd, &data[i],
15656 cu, &bytes_read);
15657 i += bytes_read;
15658 break;
15659
15660 case DW_OP_const1u:
15661 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
15662 i += 1;
15663 break;
15664
15665 case DW_OP_const1s:
15666 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
15667 i += 1;
15668 break;
15669
15670 case DW_OP_const2u:
15671 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
15672 i += 2;
15673 break;
15674
15675 case DW_OP_const2s:
15676 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
15677 i += 2;
15678 break;
15679
15680 case DW_OP_const4u:
15681 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
15682 i += 4;
15683 break;
15684
15685 case DW_OP_const4s:
15686 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
15687 i += 4;
15688 break;
15689
15690 case DW_OP_const8u:
15691 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
15692 i += 8;
15693 break;
15694
15695 case DW_OP_constu:
15696 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
15697 &bytes_read);
15698 i += bytes_read;
15699 break;
15700
15701 case DW_OP_consts:
15702 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
15703 i += bytes_read;
15704 break;
15705
15706 case DW_OP_dup:
15707 stack[stacki + 1] = stack[stacki];
15708 stacki++;
15709 break;
15710
15711 case DW_OP_plus:
15712 stack[stacki - 1] += stack[stacki];
15713 stacki--;
15714 break;
15715
15716 case DW_OP_plus_uconst:
15717 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
15718 &bytes_read);
15719 i += bytes_read;
15720 break;
15721
15722 case DW_OP_minus:
15723 stack[stacki - 1] -= stack[stacki];
15724 stacki--;
15725 break;
15726
15727 case DW_OP_deref:
15728 /* If we're not the last op, then we definitely can't encode
15729 this using GDB's address_class enum. This is valid for partial
15730 global symbols, although the variable's address will be bogus
15731 in the psymtab. */
15732 if (i < size)
15733 dwarf2_complex_location_expr_complaint ();
15734 break;
15735
15736 case DW_OP_GNU_push_tls_address:
15737 /* The top of the stack has the offset from the beginning
15738 of the thread control block at which the variable is located. */
15739 /* Nothing should follow this operator, so the top of stack would
15740 be returned. */
15741 /* This is valid for partial global symbols, but the variable's
15742 address will be bogus in the psymtab. Make it always at least
15743 non-zero to not look as a variable garbage collected by linker
15744 which have DW_OP_addr 0. */
15745 if (i < size)
15746 dwarf2_complex_location_expr_complaint ();
15747 stack[stacki]++;
15748 break;
15749
15750 case DW_OP_GNU_uninit:
15751 break;
15752
15753 case DW_OP_GNU_addr_index:
15754 case DW_OP_GNU_const_index:
15755 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
15756 &bytes_read);
15757 i += bytes_read;
15758 break;
15759
15760 default:
15761 {
15762 const char *name = get_DW_OP_name (op);
15763
15764 if (name)
15765 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
15766 name);
15767 else
15768 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
15769 op);
15770 }
15771
15772 return (stack[stacki]);
15773 }
15774
15775 /* Enforce maximum stack depth of SIZE-1 to avoid writing
15776 outside of the allocated space. Also enforce minimum>0. */
15777 if (stacki >= ARRAY_SIZE (stack) - 1)
15778 {
15779 complaint (&symfile_complaints,
15780 _("location description stack overflow"));
15781 return 0;
15782 }
15783
15784 if (stacki <= 0)
15785 {
15786 complaint (&symfile_complaints,
15787 _("location description stack underflow"));
15788 return 0;
15789 }
15790 }
15791 return (stack[stacki]);
15792 }
15793
15794 /* memory allocation interface */
15795
15796 static struct dwarf_block *
15797 dwarf_alloc_block (struct dwarf2_cu *cu)
15798 {
15799 struct dwarf_block *blk;
15800
15801 blk = (struct dwarf_block *)
15802 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
15803 return (blk);
15804 }
15805
15806 static struct die_info *
15807 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
15808 {
15809 struct die_info *die;
15810 size_t size = sizeof (struct die_info);
15811
15812 if (num_attrs > 1)
15813 size += (num_attrs - 1) * sizeof (struct attribute);
15814
15815 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
15816 memset (die, 0, sizeof (struct die_info));
15817 return (die);
15818 }
15819
15820 \f
15821 /* Macro support. */
15822
15823 /* Return the full name of file number I in *LH's file name table.
15824 Use COMP_DIR as the name of the current directory of the
15825 compilation. The result is allocated using xmalloc; the caller is
15826 responsible for freeing it. */
15827 static char *
15828 file_full_name (int file, struct line_header *lh, const char *comp_dir)
15829 {
15830 /* Is the file number a valid index into the line header's file name
15831 table? Remember that file numbers start with one, not zero. */
15832 if (1 <= file && file <= lh->num_file_names)
15833 {
15834 struct file_entry *fe = &lh->file_names[file - 1];
15835
15836 if (IS_ABSOLUTE_PATH (fe->name))
15837 return xstrdup (fe->name);
15838 else
15839 {
15840 const char *dir;
15841 int dir_len;
15842 char *full_name;
15843
15844 if (fe->dir_index)
15845 dir = lh->include_dirs[fe->dir_index - 1];
15846 else
15847 dir = comp_dir;
15848
15849 if (dir)
15850 {
15851 dir_len = strlen (dir);
15852 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
15853 strcpy (full_name, dir);
15854 full_name[dir_len] = '/';
15855 strcpy (full_name + dir_len + 1, fe->name);
15856 return full_name;
15857 }
15858 else
15859 return xstrdup (fe->name);
15860 }
15861 }
15862 else
15863 {
15864 /* The compiler produced a bogus file number. We can at least
15865 record the macro definitions made in the file, even if we
15866 won't be able to find the file by name. */
15867 char fake_name[80];
15868
15869 sprintf (fake_name, "<bad macro file number %d>", file);
15870
15871 complaint (&symfile_complaints,
15872 _("bad file number in macro information (%d)"),
15873 file);
15874
15875 return xstrdup (fake_name);
15876 }
15877 }
15878
15879
15880 static struct macro_source_file *
15881 macro_start_file (int file, int line,
15882 struct macro_source_file *current_file,
15883 const char *comp_dir,
15884 struct line_header *lh, struct objfile *objfile)
15885 {
15886 /* The full name of this source file. */
15887 char *full_name = file_full_name (file, lh, comp_dir);
15888
15889 /* We don't create a macro table for this compilation unit
15890 at all until we actually get a filename. */
15891 if (! pending_macros)
15892 pending_macros = new_macro_table (&objfile->objfile_obstack,
15893 objfile->macro_cache);
15894
15895 if (! current_file)
15896 {
15897 /* If we have no current file, then this must be the start_file
15898 directive for the compilation unit's main source file. */
15899 current_file = macro_set_main (pending_macros, full_name);
15900 macro_define_special (pending_macros);
15901 }
15902 else
15903 current_file = macro_include (current_file, line, full_name);
15904
15905 xfree (full_name);
15906
15907 return current_file;
15908 }
15909
15910
15911 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
15912 followed by a null byte. */
15913 static char *
15914 copy_string (const char *buf, int len)
15915 {
15916 char *s = xmalloc (len + 1);
15917
15918 memcpy (s, buf, len);
15919 s[len] = '\0';
15920 return s;
15921 }
15922
15923
15924 static const char *
15925 consume_improper_spaces (const char *p, const char *body)
15926 {
15927 if (*p == ' ')
15928 {
15929 complaint (&symfile_complaints,
15930 _("macro definition contains spaces "
15931 "in formal argument list:\n`%s'"),
15932 body);
15933
15934 while (*p == ' ')
15935 p++;
15936 }
15937
15938 return p;
15939 }
15940
15941
15942 static void
15943 parse_macro_definition (struct macro_source_file *file, int line,
15944 const char *body)
15945 {
15946 const char *p;
15947
15948 /* The body string takes one of two forms. For object-like macro
15949 definitions, it should be:
15950
15951 <macro name> " " <definition>
15952
15953 For function-like macro definitions, it should be:
15954
15955 <macro name> "() " <definition>
15956 or
15957 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
15958
15959 Spaces may appear only where explicitly indicated, and in the
15960 <definition>.
15961
15962 The Dwarf 2 spec says that an object-like macro's name is always
15963 followed by a space, but versions of GCC around March 2002 omit
15964 the space when the macro's definition is the empty string.
15965
15966 The Dwarf 2 spec says that there should be no spaces between the
15967 formal arguments in a function-like macro's formal argument list,
15968 but versions of GCC around March 2002 include spaces after the
15969 commas. */
15970
15971
15972 /* Find the extent of the macro name. The macro name is terminated
15973 by either a space or null character (for an object-like macro) or
15974 an opening paren (for a function-like macro). */
15975 for (p = body; *p; p++)
15976 if (*p == ' ' || *p == '(')
15977 break;
15978
15979 if (*p == ' ' || *p == '\0')
15980 {
15981 /* It's an object-like macro. */
15982 int name_len = p - body;
15983 char *name = copy_string (body, name_len);
15984 const char *replacement;
15985
15986 if (*p == ' ')
15987 replacement = body + name_len + 1;
15988 else
15989 {
15990 dwarf2_macro_malformed_definition_complaint (body);
15991 replacement = body + name_len;
15992 }
15993
15994 macro_define_object (file, line, name, replacement);
15995
15996 xfree (name);
15997 }
15998 else if (*p == '(')
15999 {
16000 /* It's a function-like macro. */
16001 char *name = copy_string (body, p - body);
16002 int argc = 0;
16003 int argv_size = 1;
16004 char **argv = xmalloc (argv_size * sizeof (*argv));
16005
16006 p++;
16007
16008 p = consume_improper_spaces (p, body);
16009
16010 /* Parse the formal argument list. */
16011 while (*p && *p != ')')
16012 {
16013 /* Find the extent of the current argument name. */
16014 const char *arg_start = p;
16015
16016 while (*p && *p != ',' && *p != ')' && *p != ' ')
16017 p++;
16018
16019 if (! *p || p == arg_start)
16020 dwarf2_macro_malformed_definition_complaint (body);
16021 else
16022 {
16023 /* Make sure argv has room for the new argument. */
16024 if (argc >= argv_size)
16025 {
16026 argv_size *= 2;
16027 argv = xrealloc (argv, argv_size * sizeof (*argv));
16028 }
16029
16030 argv[argc++] = copy_string (arg_start, p - arg_start);
16031 }
16032
16033 p = consume_improper_spaces (p, body);
16034
16035 /* Consume the comma, if present. */
16036 if (*p == ',')
16037 {
16038 p++;
16039
16040 p = consume_improper_spaces (p, body);
16041 }
16042 }
16043
16044 if (*p == ')')
16045 {
16046 p++;
16047
16048 if (*p == ' ')
16049 /* Perfectly formed definition, no complaints. */
16050 macro_define_function (file, line, name,
16051 argc, (const char **) argv,
16052 p + 1);
16053 else if (*p == '\0')
16054 {
16055 /* Complain, but do define it. */
16056 dwarf2_macro_malformed_definition_complaint (body);
16057 macro_define_function (file, line, name,
16058 argc, (const char **) argv,
16059 p);
16060 }
16061 else
16062 /* Just complain. */
16063 dwarf2_macro_malformed_definition_complaint (body);
16064 }
16065 else
16066 /* Just complain. */
16067 dwarf2_macro_malformed_definition_complaint (body);
16068
16069 xfree (name);
16070 {
16071 int i;
16072
16073 for (i = 0; i < argc; i++)
16074 xfree (argv[i]);
16075 }
16076 xfree (argv);
16077 }
16078 else
16079 dwarf2_macro_malformed_definition_complaint (body);
16080 }
16081
16082 /* Skip some bytes from BYTES according to the form given in FORM.
16083 Returns the new pointer. */
16084
16085 static gdb_byte *
16086 skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
16087 enum dwarf_form form,
16088 unsigned int offset_size,
16089 struct dwarf2_section_info *section)
16090 {
16091 unsigned int bytes_read;
16092
16093 switch (form)
16094 {
16095 case DW_FORM_data1:
16096 case DW_FORM_flag:
16097 ++bytes;
16098 break;
16099
16100 case DW_FORM_data2:
16101 bytes += 2;
16102 break;
16103
16104 case DW_FORM_data4:
16105 bytes += 4;
16106 break;
16107
16108 case DW_FORM_data8:
16109 bytes += 8;
16110 break;
16111
16112 case DW_FORM_string:
16113 read_direct_string (abfd, bytes, &bytes_read);
16114 bytes += bytes_read;
16115 break;
16116
16117 case DW_FORM_sec_offset:
16118 case DW_FORM_strp:
16119 bytes += offset_size;
16120 break;
16121
16122 case DW_FORM_block:
16123 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
16124 bytes += bytes_read;
16125 break;
16126
16127 case DW_FORM_block1:
16128 bytes += 1 + read_1_byte (abfd, bytes);
16129 break;
16130 case DW_FORM_block2:
16131 bytes += 2 + read_2_bytes (abfd, bytes);
16132 break;
16133 case DW_FORM_block4:
16134 bytes += 4 + read_4_bytes (abfd, bytes);
16135 break;
16136
16137 case DW_FORM_sdata:
16138 case DW_FORM_udata:
16139 case DW_FORM_GNU_addr_index:
16140 case DW_FORM_GNU_str_index:
16141 bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
16142 if (bytes == NULL)
16143 {
16144 dwarf2_section_buffer_overflow_complaint (section);
16145 return NULL;
16146 }
16147 break;
16148
16149 default:
16150 {
16151 complain:
16152 complaint (&symfile_complaints,
16153 _("invalid form 0x%x in `%s'"),
16154 form,
16155 section->asection->name);
16156 return NULL;
16157 }
16158 }
16159
16160 return bytes;
16161 }
16162
16163 /* A helper for dwarf_decode_macros that handles skipping an unknown
16164 opcode. Returns an updated pointer to the macro data buffer; or,
16165 on error, issues a complaint and returns NULL. */
16166
16167 static gdb_byte *
16168 skip_unknown_opcode (unsigned int opcode,
16169 gdb_byte **opcode_definitions,
16170 gdb_byte *mac_ptr, gdb_byte *mac_end,
16171 bfd *abfd,
16172 unsigned int offset_size,
16173 struct dwarf2_section_info *section)
16174 {
16175 unsigned int bytes_read, i;
16176 unsigned long arg;
16177 gdb_byte *defn;
16178
16179 if (opcode_definitions[opcode] == NULL)
16180 {
16181 complaint (&symfile_complaints,
16182 _("unrecognized DW_MACFINO opcode 0x%x"),
16183 opcode);
16184 return NULL;
16185 }
16186
16187 defn = opcode_definitions[opcode];
16188 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
16189 defn += bytes_read;
16190
16191 for (i = 0; i < arg; ++i)
16192 {
16193 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
16194 section);
16195 if (mac_ptr == NULL)
16196 {
16197 /* skip_form_bytes already issued the complaint. */
16198 return NULL;
16199 }
16200 }
16201
16202 return mac_ptr;
16203 }
16204
16205 /* A helper function which parses the header of a macro section.
16206 If the macro section is the extended (for now called "GNU") type,
16207 then this updates *OFFSET_SIZE. Returns a pointer to just after
16208 the header, or issues a complaint and returns NULL on error. */
16209
16210 static gdb_byte *
16211 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
16212 bfd *abfd,
16213 gdb_byte *mac_ptr,
16214 unsigned int *offset_size,
16215 int section_is_gnu)
16216 {
16217 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
16218
16219 if (section_is_gnu)
16220 {
16221 unsigned int version, flags;
16222
16223 version = read_2_bytes (abfd, mac_ptr);
16224 if (version != 4)
16225 {
16226 complaint (&symfile_complaints,
16227 _("unrecognized version `%d' in .debug_macro section"),
16228 version);
16229 return NULL;
16230 }
16231 mac_ptr += 2;
16232
16233 flags = read_1_byte (abfd, mac_ptr);
16234 ++mac_ptr;
16235 *offset_size = (flags & 1) ? 8 : 4;
16236
16237 if ((flags & 2) != 0)
16238 /* We don't need the line table offset. */
16239 mac_ptr += *offset_size;
16240
16241 /* Vendor opcode descriptions. */
16242 if ((flags & 4) != 0)
16243 {
16244 unsigned int i, count;
16245
16246 count = read_1_byte (abfd, mac_ptr);
16247 ++mac_ptr;
16248 for (i = 0; i < count; ++i)
16249 {
16250 unsigned int opcode, bytes_read;
16251 unsigned long arg;
16252
16253 opcode = read_1_byte (abfd, mac_ptr);
16254 ++mac_ptr;
16255 opcode_definitions[opcode] = mac_ptr;
16256 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16257 mac_ptr += bytes_read;
16258 mac_ptr += arg;
16259 }
16260 }
16261 }
16262
16263 return mac_ptr;
16264 }
16265
16266 /* A helper for dwarf_decode_macros that handles the GNU extensions,
16267 including DW_MACRO_GNU_transparent_include. */
16268
16269 static void
16270 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
16271 struct macro_source_file *current_file,
16272 struct line_header *lh, char *comp_dir,
16273 struct dwarf2_section_info *section,
16274 int section_is_gnu,
16275 unsigned int offset_size,
16276 struct objfile *objfile,
16277 htab_t include_hash)
16278 {
16279 enum dwarf_macro_record_type macinfo_type;
16280 int at_commandline;
16281 gdb_byte *opcode_definitions[256];
16282
16283 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
16284 &offset_size, section_is_gnu);
16285 if (mac_ptr == NULL)
16286 {
16287 /* We already issued a complaint. */
16288 return;
16289 }
16290
16291 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
16292 GDB is still reading the definitions from command line. First
16293 DW_MACINFO_start_file will need to be ignored as it was already executed
16294 to create CURRENT_FILE for the main source holding also the command line
16295 definitions. On first met DW_MACINFO_start_file this flag is reset to
16296 normally execute all the remaining DW_MACINFO_start_file macinfos. */
16297
16298 at_commandline = 1;
16299
16300 do
16301 {
16302 /* Do we at least have room for a macinfo type byte? */
16303 if (mac_ptr >= mac_end)
16304 {
16305 dwarf2_section_buffer_overflow_complaint (section);
16306 break;
16307 }
16308
16309 macinfo_type = read_1_byte (abfd, mac_ptr);
16310 mac_ptr++;
16311
16312 /* Note that we rely on the fact that the corresponding GNU and
16313 DWARF constants are the same. */
16314 switch (macinfo_type)
16315 {
16316 /* A zero macinfo type indicates the end of the macro
16317 information. */
16318 case 0:
16319 break;
16320
16321 case DW_MACRO_GNU_define:
16322 case DW_MACRO_GNU_undef:
16323 case DW_MACRO_GNU_define_indirect:
16324 case DW_MACRO_GNU_undef_indirect:
16325 {
16326 unsigned int bytes_read;
16327 int line;
16328 char *body;
16329 int is_define;
16330
16331 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16332 mac_ptr += bytes_read;
16333
16334 if (macinfo_type == DW_MACRO_GNU_define
16335 || macinfo_type == DW_MACRO_GNU_undef)
16336 {
16337 body = read_direct_string (abfd, mac_ptr, &bytes_read);
16338 mac_ptr += bytes_read;
16339 }
16340 else
16341 {
16342 LONGEST str_offset;
16343
16344 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
16345 mac_ptr += offset_size;
16346
16347 body = read_indirect_string_at_offset (abfd, str_offset);
16348 }
16349
16350 is_define = (macinfo_type == DW_MACRO_GNU_define
16351 || macinfo_type == DW_MACRO_GNU_define_indirect);
16352 if (! current_file)
16353 {
16354 /* DWARF violation as no main source is present. */
16355 complaint (&symfile_complaints,
16356 _("debug info with no main source gives macro %s "
16357 "on line %d: %s"),
16358 is_define ? _("definition") : _("undefinition"),
16359 line, body);
16360 break;
16361 }
16362 if ((line == 0 && !at_commandline)
16363 || (line != 0 && at_commandline))
16364 complaint (&symfile_complaints,
16365 _("debug info gives %s macro %s with %s line %d: %s"),
16366 at_commandline ? _("command-line") : _("in-file"),
16367 is_define ? _("definition") : _("undefinition"),
16368 line == 0 ? _("zero") : _("non-zero"), line, body);
16369
16370 if (is_define)
16371 parse_macro_definition (current_file, line, body);
16372 else
16373 {
16374 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
16375 || macinfo_type == DW_MACRO_GNU_undef_indirect);
16376 macro_undef (current_file, line, body);
16377 }
16378 }
16379 break;
16380
16381 case DW_MACRO_GNU_start_file:
16382 {
16383 unsigned int bytes_read;
16384 int line, file;
16385
16386 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16387 mac_ptr += bytes_read;
16388 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16389 mac_ptr += bytes_read;
16390
16391 if ((line == 0 && !at_commandline)
16392 || (line != 0 && at_commandline))
16393 complaint (&symfile_complaints,
16394 _("debug info gives source %d included "
16395 "from %s at %s line %d"),
16396 file, at_commandline ? _("command-line") : _("file"),
16397 line == 0 ? _("zero") : _("non-zero"), line);
16398
16399 if (at_commandline)
16400 {
16401 /* This DW_MACRO_GNU_start_file was executed in the
16402 pass one. */
16403 at_commandline = 0;
16404 }
16405 else
16406 current_file = macro_start_file (file, line,
16407 current_file, comp_dir,
16408 lh, objfile);
16409 }
16410 break;
16411
16412 case DW_MACRO_GNU_end_file:
16413 if (! current_file)
16414 complaint (&symfile_complaints,
16415 _("macro debug info has an unmatched "
16416 "`close_file' directive"));
16417 else
16418 {
16419 current_file = current_file->included_by;
16420 if (! current_file)
16421 {
16422 enum dwarf_macro_record_type next_type;
16423
16424 /* GCC circa March 2002 doesn't produce the zero
16425 type byte marking the end of the compilation
16426 unit. Complain if it's not there, but exit no
16427 matter what. */
16428
16429 /* Do we at least have room for a macinfo type byte? */
16430 if (mac_ptr >= mac_end)
16431 {
16432 dwarf2_section_buffer_overflow_complaint (section);
16433 return;
16434 }
16435
16436 /* We don't increment mac_ptr here, so this is just
16437 a look-ahead. */
16438 next_type = read_1_byte (abfd, mac_ptr);
16439 if (next_type != 0)
16440 complaint (&symfile_complaints,
16441 _("no terminating 0-type entry for "
16442 "macros in `.debug_macinfo' section"));
16443
16444 return;
16445 }
16446 }
16447 break;
16448
16449 case DW_MACRO_GNU_transparent_include:
16450 {
16451 LONGEST offset;
16452 void **slot;
16453
16454 offset = read_offset_1 (abfd, mac_ptr, offset_size);
16455 mac_ptr += offset_size;
16456
16457 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
16458 if (*slot != NULL)
16459 {
16460 /* This has actually happened; see
16461 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
16462 complaint (&symfile_complaints,
16463 _("recursive DW_MACRO_GNU_transparent_include in "
16464 ".debug_macro section"));
16465 }
16466 else
16467 {
16468 *slot = mac_ptr;
16469
16470 dwarf_decode_macro_bytes (abfd,
16471 section->buffer + offset,
16472 mac_end, current_file,
16473 lh, comp_dir,
16474 section, section_is_gnu,
16475 offset_size, objfile, include_hash);
16476
16477 htab_remove_elt (include_hash, mac_ptr);
16478 }
16479 }
16480 break;
16481
16482 case DW_MACINFO_vendor_ext:
16483 if (!section_is_gnu)
16484 {
16485 unsigned int bytes_read;
16486 int constant;
16487
16488 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16489 mac_ptr += bytes_read;
16490 read_direct_string (abfd, mac_ptr, &bytes_read);
16491 mac_ptr += bytes_read;
16492
16493 /* We don't recognize any vendor extensions. */
16494 break;
16495 }
16496 /* FALLTHROUGH */
16497
16498 default:
16499 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
16500 mac_ptr, mac_end, abfd, offset_size,
16501 section);
16502 if (mac_ptr == NULL)
16503 return;
16504 break;
16505 }
16506 } while (macinfo_type != 0);
16507 }
16508
16509 static void
16510 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
16511 char *comp_dir, int section_is_gnu)
16512 {
16513 struct objfile *objfile = dwarf2_per_objfile->objfile;
16514 struct line_header *lh = cu->line_header;
16515 bfd *abfd;
16516 gdb_byte *mac_ptr, *mac_end;
16517 struct macro_source_file *current_file = 0;
16518 enum dwarf_macro_record_type macinfo_type;
16519 unsigned int offset_size = cu->header.offset_size;
16520 gdb_byte *opcode_definitions[256];
16521 struct cleanup *cleanup;
16522 htab_t include_hash;
16523 void **slot;
16524 struct dwarf2_section_info *section;
16525 const char *section_name;
16526
16527 if (cu->dwo_unit != NULL)
16528 {
16529 if (section_is_gnu)
16530 {
16531 section = &cu->dwo_unit->dwo_file->sections.macro;
16532 section_name = ".debug_macro.dwo";
16533 }
16534 else
16535 {
16536 section = &cu->dwo_unit->dwo_file->sections.macinfo;
16537 section_name = ".debug_macinfo.dwo";
16538 }
16539 }
16540 else
16541 {
16542 if (section_is_gnu)
16543 {
16544 section = &dwarf2_per_objfile->macro;
16545 section_name = ".debug_macro";
16546 }
16547 else
16548 {
16549 section = &dwarf2_per_objfile->macinfo;
16550 section_name = ".debug_macinfo";
16551 }
16552 }
16553
16554 dwarf2_read_section (objfile, section);
16555 if (section->buffer == NULL)
16556 {
16557 complaint (&symfile_complaints, _("missing %s section"), section_name);
16558 return;
16559 }
16560 abfd = section->asection->owner;
16561
16562 /* First pass: Find the name of the base filename.
16563 This filename is needed in order to process all macros whose definition
16564 (or undefinition) comes from the command line. These macros are defined
16565 before the first DW_MACINFO_start_file entry, and yet still need to be
16566 associated to the base file.
16567
16568 To determine the base file name, we scan the macro definitions until we
16569 reach the first DW_MACINFO_start_file entry. We then initialize
16570 CURRENT_FILE accordingly so that any macro definition found before the
16571 first DW_MACINFO_start_file can still be associated to the base file. */
16572
16573 mac_ptr = section->buffer + offset;
16574 mac_end = section->buffer + section->size;
16575
16576 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
16577 &offset_size, section_is_gnu);
16578 if (mac_ptr == NULL)
16579 {
16580 /* We already issued a complaint. */
16581 return;
16582 }
16583
16584 do
16585 {
16586 /* Do we at least have room for a macinfo type byte? */
16587 if (mac_ptr >= mac_end)
16588 {
16589 /* Complaint is printed during the second pass as GDB will probably
16590 stop the first pass earlier upon finding
16591 DW_MACINFO_start_file. */
16592 break;
16593 }
16594
16595 macinfo_type = read_1_byte (abfd, mac_ptr);
16596 mac_ptr++;
16597
16598 /* Note that we rely on the fact that the corresponding GNU and
16599 DWARF constants are the same. */
16600 switch (macinfo_type)
16601 {
16602 /* A zero macinfo type indicates the end of the macro
16603 information. */
16604 case 0:
16605 break;
16606
16607 case DW_MACRO_GNU_define:
16608 case DW_MACRO_GNU_undef:
16609 /* Only skip the data by MAC_PTR. */
16610 {
16611 unsigned int bytes_read;
16612
16613 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16614 mac_ptr += bytes_read;
16615 read_direct_string (abfd, mac_ptr, &bytes_read);
16616 mac_ptr += bytes_read;
16617 }
16618 break;
16619
16620 case DW_MACRO_GNU_start_file:
16621 {
16622 unsigned int bytes_read;
16623 int line, file;
16624
16625 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16626 mac_ptr += bytes_read;
16627 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16628 mac_ptr += bytes_read;
16629
16630 current_file = macro_start_file (file, line, current_file,
16631 comp_dir, lh, objfile);
16632 }
16633 break;
16634
16635 case DW_MACRO_GNU_end_file:
16636 /* No data to skip by MAC_PTR. */
16637 break;
16638
16639 case DW_MACRO_GNU_define_indirect:
16640 case DW_MACRO_GNU_undef_indirect:
16641 {
16642 unsigned int bytes_read;
16643
16644 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16645 mac_ptr += bytes_read;
16646 mac_ptr += offset_size;
16647 }
16648 break;
16649
16650 case DW_MACRO_GNU_transparent_include:
16651 /* Note that, according to the spec, a transparent include
16652 chain cannot call DW_MACRO_GNU_start_file. So, we can just
16653 skip this opcode. */
16654 mac_ptr += offset_size;
16655 break;
16656
16657 case DW_MACINFO_vendor_ext:
16658 /* Only skip the data by MAC_PTR. */
16659 if (!section_is_gnu)
16660 {
16661 unsigned int bytes_read;
16662
16663 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16664 mac_ptr += bytes_read;
16665 read_direct_string (abfd, mac_ptr, &bytes_read);
16666 mac_ptr += bytes_read;
16667 }
16668 /* FALLTHROUGH */
16669
16670 default:
16671 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
16672 mac_ptr, mac_end, abfd, offset_size,
16673 section);
16674 if (mac_ptr == NULL)
16675 return;
16676 break;
16677 }
16678 } while (macinfo_type != 0 && current_file == NULL);
16679
16680 /* Second pass: Process all entries.
16681
16682 Use the AT_COMMAND_LINE flag to determine whether we are still processing
16683 command-line macro definitions/undefinitions. This flag is unset when we
16684 reach the first DW_MACINFO_start_file entry. */
16685
16686 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
16687 NULL, xcalloc, xfree);
16688 cleanup = make_cleanup_htab_delete (include_hash);
16689 mac_ptr = section->buffer + offset;
16690 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
16691 *slot = mac_ptr;
16692 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
16693 current_file, lh, comp_dir, section, section_is_gnu,
16694 offset_size, objfile, include_hash);
16695 do_cleanups (cleanup);
16696 }
16697
16698 /* Check if the attribute's form is a DW_FORM_block*
16699 if so return true else false. */
16700
16701 static int
16702 attr_form_is_block (struct attribute *attr)
16703 {
16704 return (attr == NULL ? 0 :
16705 attr->form == DW_FORM_block1
16706 || attr->form == DW_FORM_block2
16707 || attr->form == DW_FORM_block4
16708 || attr->form == DW_FORM_block
16709 || attr->form == DW_FORM_exprloc);
16710 }
16711
16712 /* Return non-zero if ATTR's value is a section offset --- classes
16713 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
16714 You may use DW_UNSND (attr) to retrieve such offsets.
16715
16716 Section 7.5.4, "Attribute Encodings", explains that no attribute
16717 may have a value that belongs to more than one of these classes; it
16718 would be ambiguous if we did, because we use the same forms for all
16719 of them. */
16720
16721 static int
16722 attr_form_is_section_offset (struct attribute *attr)
16723 {
16724 return (attr->form == DW_FORM_data4
16725 || attr->form == DW_FORM_data8
16726 || attr->form == DW_FORM_sec_offset);
16727 }
16728
16729 /* Return non-zero if ATTR's value falls in the 'constant' class, or
16730 zero otherwise. When this function returns true, you can apply
16731 dwarf2_get_attr_constant_value to it.
16732
16733 However, note that for some attributes you must check
16734 attr_form_is_section_offset before using this test. DW_FORM_data4
16735 and DW_FORM_data8 are members of both the constant class, and of
16736 the classes that contain offsets into other debug sections
16737 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
16738 that, if an attribute's can be either a constant or one of the
16739 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
16740 taken as section offsets, not constants. */
16741
16742 static int
16743 attr_form_is_constant (struct attribute *attr)
16744 {
16745 switch (attr->form)
16746 {
16747 case DW_FORM_sdata:
16748 case DW_FORM_udata:
16749 case DW_FORM_data1:
16750 case DW_FORM_data2:
16751 case DW_FORM_data4:
16752 case DW_FORM_data8:
16753 return 1;
16754 default:
16755 return 0;
16756 }
16757 }
16758
16759 /* Return the .debug_loc section to use for CU.
16760 For DWO files use .debug_loc.dwo. */
16761
16762 static struct dwarf2_section_info *
16763 cu_debug_loc_section (struct dwarf2_cu *cu)
16764 {
16765 if (cu->dwo_unit)
16766 return &cu->dwo_unit->dwo_file->sections.loc;
16767 return &dwarf2_per_objfile->loc;
16768 }
16769
16770 /* A helper function that fills in a dwarf2_loclist_baton. */
16771
16772 static void
16773 fill_in_loclist_baton (struct dwarf2_cu *cu,
16774 struct dwarf2_loclist_baton *baton,
16775 struct attribute *attr)
16776 {
16777 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
16778
16779 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
16780
16781 baton->per_cu = cu->per_cu;
16782 gdb_assert (baton->per_cu);
16783 /* We don't know how long the location list is, but make sure we
16784 don't run off the edge of the section. */
16785 baton->size = section->size - DW_UNSND (attr);
16786 baton->data = section->buffer + DW_UNSND (attr);
16787 baton->base_address = cu->base_address;
16788 baton->from_dwo = cu->dwo_unit != NULL;
16789 }
16790
16791 static void
16792 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
16793 struct dwarf2_cu *cu)
16794 {
16795 struct objfile *objfile = dwarf2_per_objfile->objfile;
16796 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
16797
16798 if (attr_form_is_section_offset (attr)
16799 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
16800 the section. If so, fall through to the complaint in the
16801 other branch. */
16802 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
16803 {
16804 struct dwarf2_loclist_baton *baton;
16805
16806 baton = obstack_alloc (&objfile->objfile_obstack,
16807 sizeof (struct dwarf2_loclist_baton));
16808
16809 fill_in_loclist_baton (cu, baton, attr);
16810
16811 if (cu->base_known == 0)
16812 complaint (&symfile_complaints,
16813 _("Location list used without "
16814 "specifying the CU base address."));
16815
16816 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
16817 SYMBOL_LOCATION_BATON (sym) = baton;
16818 }
16819 else
16820 {
16821 struct dwarf2_locexpr_baton *baton;
16822
16823 baton = obstack_alloc (&objfile->objfile_obstack,
16824 sizeof (struct dwarf2_locexpr_baton));
16825 baton->per_cu = cu->per_cu;
16826 gdb_assert (baton->per_cu);
16827
16828 if (attr_form_is_block (attr))
16829 {
16830 /* Note that we're just copying the block's data pointer
16831 here, not the actual data. We're still pointing into the
16832 info_buffer for SYM's objfile; right now we never release
16833 that buffer, but when we do clean up properly this may
16834 need to change. */
16835 baton->size = DW_BLOCK (attr)->size;
16836 baton->data = DW_BLOCK (attr)->data;
16837 }
16838 else
16839 {
16840 dwarf2_invalid_attrib_class_complaint ("location description",
16841 SYMBOL_NATURAL_NAME (sym));
16842 baton->size = 0;
16843 }
16844
16845 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
16846 SYMBOL_LOCATION_BATON (sym) = baton;
16847 }
16848 }
16849
16850 /* Return the OBJFILE associated with the compilation unit CU. If CU
16851 came from a separate debuginfo file, then the master objfile is
16852 returned. */
16853
16854 struct objfile *
16855 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
16856 {
16857 struct objfile *objfile = per_cu->objfile;
16858
16859 /* Return the master objfile, so that we can report and look up the
16860 correct file containing this variable. */
16861 if (objfile->separate_debug_objfile_backlink)
16862 objfile = objfile->separate_debug_objfile_backlink;
16863
16864 return objfile;
16865 }
16866
16867 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
16868 (CU_HEADERP is unused in such case) or prepare a temporary copy at
16869 CU_HEADERP first. */
16870
16871 static const struct comp_unit_head *
16872 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
16873 struct dwarf2_per_cu_data *per_cu)
16874 {
16875 gdb_byte *info_ptr;
16876
16877 if (per_cu->cu)
16878 return &per_cu->cu->header;
16879
16880 info_ptr = per_cu->info_or_types_section->buffer + per_cu->offset.sect_off;
16881
16882 memset (cu_headerp, 0, sizeof (*cu_headerp));
16883 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
16884
16885 return cu_headerp;
16886 }
16887
16888 /* Return the address size given in the compilation unit header for CU. */
16889
16890 int
16891 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
16892 {
16893 struct comp_unit_head cu_header_local;
16894 const struct comp_unit_head *cu_headerp;
16895
16896 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16897
16898 return cu_headerp->addr_size;
16899 }
16900
16901 /* Return the offset size given in the compilation unit header for CU. */
16902
16903 int
16904 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
16905 {
16906 struct comp_unit_head cu_header_local;
16907 const struct comp_unit_head *cu_headerp;
16908
16909 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16910
16911 return cu_headerp->offset_size;
16912 }
16913
16914 /* See its dwarf2loc.h declaration. */
16915
16916 int
16917 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
16918 {
16919 struct comp_unit_head cu_header_local;
16920 const struct comp_unit_head *cu_headerp;
16921
16922 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16923
16924 if (cu_headerp->version == 2)
16925 return cu_headerp->addr_size;
16926 else
16927 return cu_headerp->offset_size;
16928 }
16929
16930 /* Return the text offset of the CU. The returned offset comes from
16931 this CU's objfile. If this objfile came from a separate debuginfo
16932 file, then the offset may be different from the corresponding
16933 offset in the parent objfile. */
16934
16935 CORE_ADDR
16936 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
16937 {
16938 struct objfile *objfile = per_cu->objfile;
16939
16940 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16941 }
16942
16943 /* Locate the .debug_info compilation unit from CU's objfile which contains
16944 the DIE at OFFSET. Raises an error on failure. */
16945
16946 static struct dwarf2_per_cu_data *
16947 dwarf2_find_containing_comp_unit (sect_offset offset,
16948 struct objfile *objfile)
16949 {
16950 struct dwarf2_per_cu_data *this_cu;
16951 int low, high;
16952
16953 low = 0;
16954 high = dwarf2_per_objfile->n_comp_units - 1;
16955 while (high > low)
16956 {
16957 int mid = low + (high - low) / 2;
16958
16959 if (dwarf2_per_objfile->all_comp_units[mid]->offset.sect_off
16960 >= offset.sect_off)
16961 high = mid;
16962 else
16963 low = mid + 1;
16964 }
16965 gdb_assert (low == high);
16966 if (dwarf2_per_objfile->all_comp_units[low]->offset.sect_off
16967 > offset.sect_off)
16968 {
16969 if (low == 0)
16970 error (_("Dwarf Error: could not find partial DIE containing "
16971 "offset 0x%lx [in module %s]"),
16972 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
16973
16974 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
16975 <= offset.sect_off);
16976 return dwarf2_per_objfile->all_comp_units[low-1];
16977 }
16978 else
16979 {
16980 this_cu = dwarf2_per_objfile->all_comp_units[low];
16981 if (low == dwarf2_per_objfile->n_comp_units - 1
16982 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
16983 error (_("invalid dwarf2 offset %u"), offset.sect_off);
16984 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
16985 return this_cu;
16986 }
16987 }
16988
16989 /* Initialize dwarf2_cu CU, owned by PER_CU. */
16990
16991 static void
16992 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
16993 {
16994 memset (cu, 0, sizeof (*cu));
16995 per_cu->cu = cu;
16996 cu->per_cu = per_cu;
16997 cu->objfile = per_cu->objfile;
16998 obstack_init (&cu->comp_unit_obstack);
16999 }
17000
17001 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
17002
17003 static void
17004 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
17005 enum language pretend_language)
17006 {
17007 struct attribute *attr;
17008
17009 /* Set the language we're debugging. */
17010 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
17011 if (attr)
17012 set_cu_language (DW_UNSND (attr), cu);
17013 else
17014 {
17015 cu->language = pretend_language;
17016 cu->language_defn = language_def (cu->language);
17017 }
17018
17019 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
17020 if (attr)
17021 cu->producer = DW_STRING (attr);
17022 }
17023
17024 /* Release one cached compilation unit, CU. We unlink it from the tree
17025 of compilation units, but we don't remove it from the read_in_chain;
17026 the caller is responsible for that.
17027 NOTE: DATA is a void * because this function is also used as a
17028 cleanup routine. */
17029
17030 static void
17031 free_heap_comp_unit (void *data)
17032 {
17033 struct dwarf2_cu *cu = data;
17034
17035 gdb_assert (cu->per_cu != NULL);
17036 cu->per_cu->cu = NULL;
17037 cu->per_cu = NULL;
17038
17039 obstack_free (&cu->comp_unit_obstack, NULL);
17040
17041 xfree (cu);
17042 }
17043
17044 /* This cleanup function is passed the address of a dwarf2_cu on the stack
17045 when we're finished with it. We can't free the pointer itself, but be
17046 sure to unlink it from the cache. Also release any associated storage. */
17047
17048 static void
17049 free_stack_comp_unit (void *data)
17050 {
17051 struct dwarf2_cu *cu = data;
17052
17053 gdb_assert (cu->per_cu != NULL);
17054 cu->per_cu->cu = NULL;
17055 cu->per_cu = NULL;
17056
17057 obstack_free (&cu->comp_unit_obstack, NULL);
17058 cu->partial_dies = NULL;
17059 }
17060
17061 /* Free all cached compilation units. */
17062
17063 static void
17064 free_cached_comp_units (void *data)
17065 {
17066 struct dwarf2_per_cu_data *per_cu, **last_chain;
17067
17068 per_cu = dwarf2_per_objfile->read_in_chain;
17069 last_chain = &dwarf2_per_objfile->read_in_chain;
17070 while (per_cu != NULL)
17071 {
17072 struct dwarf2_per_cu_data *next_cu;
17073
17074 next_cu = per_cu->cu->read_in_chain;
17075
17076 free_heap_comp_unit (per_cu->cu);
17077 *last_chain = next_cu;
17078
17079 per_cu = next_cu;
17080 }
17081 }
17082
17083 /* Increase the age counter on each cached compilation unit, and free
17084 any that are too old. */
17085
17086 static void
17087 age_cached_comp_units (void)
17088 {
17089 struct dwarf2_per_cu_data *per_cu, **last_chain;
17090
17091 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
17092 per_cu = dwarf2_per_objfile->read_in_chain;
17093 while (per_cu != NULL)
17094 {
17095 per_cu->cu->last_used ++;
17096 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
17097 dwarf2_mark (per_cu->cu);
17098 per_cu = per_cu->cu->read_in_chain;
17099 }
17100
17101 per_cu = dwarf2_per_objfile->read_in_chain;
17102 last_chain = &dwarf2_per_objfile->read_in_chain;
17103 while (per_cu != NULL)
17104 {
17105 struct dwarf2_per_cu_data *next_cu;
17106
17107 next_cu = per_cu->cu->read_in_chain;
17108
17109 if (!per_cu->cu->mark)
17110 {
17111 free_heap_comp_unit (per_cu->cu);
17112 *last_chain = next_cu;
17113 }
17114 else
17115 last_chain = &per_cu->cu->read_in_chain;
17116
17117 per_cu = next_cu;
17118 }
17119 }
17120
17121 /* Remove a single compilation unit from the cache. */
17122
17123 static void
17124 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
17125 {
17126 struct dwarf2_per_cu_data *per_cu, **last_chain;
17127
17128 per_cu = dwarf2_per_objfile->read_in_chain;
17129 last_chain = &dwarf2_per_objfile->read_in_chain;
17130 while (per_cu != NULL)
17131 {
17132 struct dwarf2_per_cu_data *next_cu;
17133
17134 next_cu = per_cu->cu->read_in_chain;
17135
17136 if (per_cu == target_per_cu)
17137 {
17138 free_heap_comp_unit (per_cu->cu);
17139 per_cu->cu = NULL;
17140 *last_chain = next_cu;
17141 break;
17142 }
17143 else
17144 last_chain = &per_cu->cu->read_in_chain;
17145
17146 per_cu = next_cu;
17147 }
17148 }
17149
17150 /* Release all extra memory associated with OBJFILE. */
17151
17152 void
17153 dwarf2_free_objfile (struct objfile *objfile)
17154 {
17155 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17156
17157 if (dwarf2_per_objfile == NULL)
17158 return;
17159
17160 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
17161 free_cached_comp_units (NULL);
17162
17163 if (dwarf2_per_objfile->quick_file_names_table)
17164 htab_delete (dwarf2_per_objfile->quick_file_names_table);
17165
17166 /* Everything else should be on the objfile obstack. */
17167 }
17168
17169 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
17170 We store these in a hash table separate from the DIEs, and preserve them
17171 when the DIEs are flushed out of cache.
17172
17173 The CU "per_cu" pointer is needed because offset alone is not enough to
17174 uniquely identify the type. A file may have multiple .debug_types sections,
17175 or the type may come from a DWO file. We have to use something in
17176 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
17177 routine, get_die_type_at_offset, from outside this file, and thus won't
17178 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
17179 of the objfile. */
17180
17181 struct dwarf2_per_cu_offset_and_type
17182 {
17183 const struct dwarf2_per_cu_data *per_cu;
17184 sect_offset offset;
17185 struct type *type;
17186 };
17187
17188 /* Hash function for a dwarf2_per_cu_offset_and_type. */
17189
17190 static hashval_t
17191 per_cu_offset_and_type_hash (const void *item)
17192 {
17193 const struct dwarf2_per_cu_offset_and_type *ofs = item;
17194
17195 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
17196 }
17197
17198 /* Equality function for a dwarf2_per_cu_offset_and_type. */
17199
17200 static int
17201 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
17202 {
17203 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
17204 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
17205
17206 return (ofs_lhs->per_cu == ofs_rhs->per_cu
17207 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
17208 }
17209
17210 /* Set the type associated with DIE to TYPE. Save it in CU's hash
17211 table if necessary. For convenience, return TYPE.
17212
17213 The DIEs reading must have careful ordering to:
17214 * Not cause infite loops trying to read in DIEs as a prerequisite for
17215 reading current DIE.
17216 * Not trying to dereference contents of still incompletely read in types
17217 while reading in other DIEs.
17218 * Enable referencing still incompletely read in types just by a pointer to
17219 the type without accessing its fields.
17220
17221 Therefore caller should follow these rules:
17222 * Try to fetch any prerequisite types we may need to build this DIE type
17223 before building the type and calling set_die_type.
17224 * After building type call set_die_type for current DIE as soon as
17225 possible before fetching more types to complete the current type.
17226 * Make the type as complete as possible before fetching more types. */
17227
17228 static struct type *
17229 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
17230 {
17231 struct dwarf2_per_cu_offset_and_type **slot, ofs;
17232 struct objfile *objfile = cu->objfile;
17233
17234 /* For Ada types, make sure that the gnat-specific data is always
17235 initialized (if not already set). There are a few types where
17236 we should not be doing so, because the type-specific area is
17237 already used to hold some other piece of info (eg: TYPE_CODE_FLT
17238 where the type-specific area is used to store the floatformat).
17239 But this is not a problem, because the gnat-specific information
17240 is actually not needed for these types. */
17241 if (need_gnat_info (cu)
17242 && TYPE_CODE (type) != TYPE_CODE_FUNC
17243 && TYPE_CODE (type) != TYPE_CODE_FLT
17244 && !HAVE_GNAT_AUX_INFO (type))
17245 INIT_GNAT_SPECIFIC (type);
17246
17247 if (dwarf2_per_objfile->die_type_hash == NULL)
17248 {
17249 dwarf2_per_objfile->die_type_hash =
17250 htab_create_alloc_ex (127,
17251 per_cu_offset_and_type_hash,
17252 per_cu_offset_and_type_eq,
17253 NULL,
17254 &objfile->objfile_obstack,
17255 hashtab_obstack_allocate,
17256 dummy_obstack_deallocate);
17257 }
17258
17259 ofs.per_cu = cu->per_cu;
17260 ofs.offset = die->offset;
17261 ofs.type = type;
17262 slot = (struct dwarf2_per_cu_offset_and_type **)
17263 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
17264 if (*slot)
17265 complaint (&symfile_complaints,
17266 _("A problem internal to GDB: DIE 0x%x has type already set"),
17267 die->offset.sect_off);
17268 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
17269 **slot = ofs;
17270 return type;
17271 }
17272
17273 /* Look up the type for the die at OFFSET in the appropriate type_hash
17274 table, or return NULL if the die does not have a saved type. */
17275
17276 static struct type *
17277 get_die_type_at_offset (sect_offset offset,
17278 struct dwarf2_per_cu_data *per_cu)
17279 {
17280 struct dwarf2_per_cu_offset_and_type *slot, ofs;
17281
17282 if (dwarf2_per_objfile->die_type_hash == NULL)
17283 return NULL;
17284
17285 ofs.per_cu = per_cu;
17286 ofs.offset = offset;
17287 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
17288 if (slot)
17289 return slot->type;
17290 else
17291 return NULL;
17292 }
17293
17294 /* Look up the type for DIE in the appropriate type_hash table,
17295 or return NULL if DIE does not have a saved type. */
17296
17297 static struct type *
17298 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
17299 {
17300 return get_die_type_at_offset (die->offset, cu->per_cu);
17301 }
17302
17303 /* Add a dependence relationship from CU to REF_PER_CU. */
17304
17305 static void
17306 dwarf2_add_dependence (struct dwarf2_cu *cu,
17307 struct dwarf2_per_cu_data *ref_per_cu)
17308 {
17309 void **slot;
17310
17311 if (cu->dependencies == NULL)
17312 cu->dependencies
17313 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
17314 NULL, &cu->comp_unit_obstack,
17315 hashtab_obstack_allocate,
17316 dummy_obstack_deallocate);
17317
17318 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
17319 if (*slot == NULL)
17320 *slot = ref_per_cu;
17321 }
17322
17323 /* Subroutine of dwarf2_mark to pass to htab_traverse.
17324 Set the mark field in every compilation unit in the
17325 cache that we must keep because we are keeping CU. */
17326
17327 static int
17328 dwarf2_mark_helper (void **slot, void *data)
17329 {
17330 struct dwarf2_per_cu_data *per_cu;
17331
17332 per_cu = (struct dwarf2_per_cu_data *) *slot;
17333
17334 /* cu->dependencies references may not yet have been ever read if QUIT aborts
17335 reading of the chain. As such dependencies remain valid it is not much
17336 useful to track and undo them during QUIT cleanups. */
17337 if (per_cu->cu == NULL)
17338 return 1;
17339
17340 if (per_cu->cu->mark)
17341 return 1;
17342 per_cu->cu->mark = 1;
17343
17344 if (per_cu->cu->dependencies != NULL)
17345 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
17346
17347 return 1;
17348 }
17349
17350 /* Set the mark field in CU and in every other compilation unit in the
17351 cache that we must keep because we are keeping CU. */
17352
17353 static void
17354 dwarf2_mark (struct dwarf2_cu *cu)
17355 {
17356 if (cu->mark)
17357 return;
17358 cu->mark = 1;
17359 if (cu->dependencies != NULL)
17360 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
17361 }
17362
17363 static void
17364 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
17365 {
17366 while (per_cu)
17367 {
17368 per_cu->cu->mark = 0;
17369 per_cu = per_cu->cu->read_in_chain;
17370 }
17371 }
17372
17373 /* Trivial hash function for partial_die_info: the hash value of a DIE
17374 is its offset in .debug_info for this objfile. */
17375
17376 static hashval_t
17377 partial_die_hash (const void *item)
17378 {
17379 const struct partial_die_info *part_die = item;
17380
17381 return part_die->offset.sect_off;
17382 }
17383
17384 /* Trivial comparison function for partial_die_info structures: two DIEs
17385 are equal if they have the same offset. */
17386
17387 static int
17388 partial_die_eq (const void *item_lhs, const void *item_rhs)
17389 {
17390 const struct partial_die_info *part_die_lhs = item_lhs;
17391 const struct partial_die_info *part_die_rhs = item_rhs;
17392
17393 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
17394 }
17395
17396 static struct cmd_list_element *set_dwarf2_cmdlist;
17397 static struct cmd_list_element *show_dwarf2_cmdlist;
17398
17399 static void
17400 set_dwarf2_cmd (char *args, int from_tty)
17401 {
17402 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
17403 }
17404
17405 static void
17406 show_dwarf2_cmd (char *args, int from_tty)
17407 {
17408 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
17409 }
17410
17411 /* If section described by INFO was mmapped, munmap it now. */
17412
17413 static void
17414 munmap_section_buffer (struct dwarf2_section_info *info)
17415 {
17416 if (info->map_addr != NULL)
17417 {
17418 #ifdef HAVE_MMAP
17419 int res;
17420
17421 res = munmap (info->map_addr, info->map_len);
17422 gdb_assert (res == 0);
17423 #else
17424 /* Without HAVE_MMAP, we should never be here to begin with. */
17425 gdb_assert_not_reached ("no mmap support");
17426 #endif
17427 }
17428 }
17429
17430 /* munmap debug sections for OBJFILE, if necessary. */
17431
17432 static void
17433 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
17434 {
17435 struct dwarf2_per_objfile *data = d;
17436 int ix;
17437 struct dwarf2_section_info *section;
17438
17439 /* This is sorted according to the order they're defined in to make it easier
17440 to keep in sync. */
17441 munmap_section_buffer (&data->info);
17442 munmap_section_buffer (&data->abbrev);
17443 munmap_section_buffer (&data->line);
17444 munmap_section_buffer (&data->loc);
17445 munmap_section_buffer (&data->macinfo);
17446 munmap_section_buffer (&data->macro);
17447 munmap_section_buffer (&data->str);
17448 munmap_section_buffer (&data->ranges);
17449 munmap_section_buffer (&data->addr);
17450 munmap_section_buffer (&data->frame);
17451 munmap_section_buffer (&data->eh_frame);
17452 munmap_section_buffer (&data->gdb_index);
17453
17454 for (ix = 0;
17455 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
17456 ++ix)
17457 munmap_section_buffer (section);
17458
17459 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
17460 VEC_free (dwarf2_per_cu_ptr,
17461 dwarf2_per_objfile->all_comp_units[ix]->imported_symtabs);
17462
17463 VEC_free (dwarf2_section_info_def, data->types);
17464
17465 if (data->dwo_files)
17466 free_dwo_files (data->dwo_files, objfile);
17467 }
17468
17469 \f
17470 /* The "save gdb-index" command. */
17471
17472 /* The contents of the hash table we create when building the string
17473 table. */
17474 struct strtab_entry
17475 {
17476 offset_type offset;
17477 const char *str;
17478 };
17479
17480 /* Hash function for a strtab_entry.
17481
17482 Function is used only during write_hash_table so no index format backward
17483 compatibility is needed. */
17484
17485 static hashval_t
17486 hash_strtab_entry (const void *e)
17487 {
17488 const struct strtab_entry *entry = e;
17489 return mapped_index_string_hash (INT_MAX, entry->str);
17490 }
17491
17492 /* Equality function for a strtab_entry. */
17493
17494 static int
17495 eq_strtab_entry (const void *a, const void *b)
17496 {
17497 const struct strtab_entry *ea = a;
17498 const struct strtab_entry *eb = b;
17499 return !strcmp (ea->str, eb->str);
17500 }
17501
17502 /* Create a strtab_entry hash table. */
17503
17504 static htab_t
17505 create_strtab (void)
17506 {
17507 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
17508 xfree, xcalloc, xfree);
17509 }
17510
17511 /* Add a string to the constant pool. Return the string's offset in
17512 host order. */
17513
17514 static offset_type
17515 add_string (htab_t table, struct obstack *cpool, const char *str)
17516 {
17517 void **slot;
17518 struct strtab_entry entry;
17519 struct strtab_entry *result;
17520
17521 entry.str = str;
17522 slot = htab_find_slot (table, &entry, INSERT);
17523 if (*slot)
17524 result = *slot;
17525 else
17526 {
17527 result = XNEW (struct strtab_entry);
17528 result->offset = obstack_object_size (cpool);
17529 result->str = str;
17530 obstack_grow_str0 (cpool, str);
17531 *slot = result;
17532 }
17533 return result->offset;
17534 }
17535
17536 /* An entry in the symbol table. */
17537 struct symtab_index_entry
17538 {
17539 /* The name of the symbol. */
17540 const char *name;
17541 /* The offset of the name in the constant pool. */
17542 offset_type index_offset;
17543 /* A sorted vector of the indices of all the CUs that hold an object
17544 of this name. */
17545 VEC (offset_type) *cu_indices;
17546 };
17547
17548 /* The symbol table. This is a power-of-2-sized hash table. */
17549 struct mapped_symtab
17550 {
17551 offset_type n_elements;
17552 offset_type size;
17553 struct symtab_index_entry **data;
17554 };
17555
17556 /* Hash function for a symtab_index_entry. */
17557
17558 static hashval_t
17559 hash_symtab_entry (const void *e)
17560 {
17561 const struct symtab_index_entry *entry = e;
17562 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
17563 sizeof (offset_type) * VEC_length (offset_type,
17564 entry->cu_indices),
17565 0);
17566 }
17567
17568 /* Equality function for a symtab_index_entry. */
17569
17570 static int
17571 eq_symtab_entry (const void *a, const void *b)
17572 {
17573 const struct symtab_index_entry *ea = a;
17574 const struct symtab_index_entry *eb = b;
17575 int len = VEC_length (offset_type, ea->cu_indices);
17576 if (len != VEC_length (offset_type, eb->cu_indices))
17577 return 0;
17578 return !memcmp (VEC_address (offset_type, ea->cu_indices),
17579 VEC_address (offset_type, eb->cu_indices),
17580 sizeof (offset_type) * len);
17581 }
17582
17583 /* Destroy a symtab_index_entry. */
17584
17585 static void
17586 delete_symtab_entry (void *p)
17587 {
17588 struct symtab_index_entry *entry = p;
17589 VEC_free (offset_type, entry->cu_indices);
17590 xfree (entry);
17591 }
17592
17593 /* Create a hash table holding symtab_index_entry objects. */
17594
17595 static htab_t
17596 create_symbol_hash_table (void)
17597 {
17598 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
17599 delete_symtab_entry, xcalloc, xfree);
17600 }
17601
17602 /* Create a new mapped symtab object. */
17603
17604 static struct mapped_symtab *
17605 create_mapped_symtab (void)
17606 {
17607 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
17608 symtab->n_elements = 0;
17609 symtab->size = 1024;
17610 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
17611 return symtab;
17612 }
17613
17614 /* Destroy a mapped_symtab. */
17615
17616 static void
17617 cleanup_mapped_symtab (void *p)
17618 {
17619 struct mapped_symtab *symtab = p;
17620 /* The contents of the array are freed when the other hash table is
17621 destroyed. */
17622 xfree (symtab->data);
17623 xfree (symtab);
17624 }
17625
17626 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
17627 the slot.
17628
17629 Function is used only during write_hash_table so no index format backward
17630 compatibility is needed. */
17631
17632 static struct symtab_index_entry **
17633 find_slot (struct mapped_symtab *symtab, const char *name)
17634 {
17635 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
17636
17637 index = hash & (symtab->size - 1);
17638 step = ((hash * 17) & (symtab->size - 1)) | 1;
17639
17640 for (;;)
17641 {
17642 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
17643 return &symtab->data[index];
17644 index = (index + step) & (symtab->size - 1);
17645 }
17646 }
17647
17648 /* Expand SYMTAB's hash table. */
17649
17650 static void
17651 hash_expand (struct mapped_symtab *symtab)
17652 {
17653 offset_type old_size = symtab->size;
17654 offset_type i;
17655 struct symtab_index_entry **old_entries = symtab->data;
17656
17657 symtab->size *= 2;
17658 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
17659
17660 for (i = 0; i < old_size; ++i)
17661 {
17662 if (old_entries[i])
17663 {
17664 struct symtab_index_entry **slot = find_slot (symtab,
17665 old_entries[i]->name);
17666 *slot = old_entries[i];
17667 }
17668 }
17669
17670 xfree (old_entries);
17671 }
17672
17673 /* Add an entry to SYMTAB. NAME is the name of the symbol.
17674 CU_INDEX is the index of the CU in which the symbol appears.
17675 IS_STATIC is one if the symbol is static, otherwise zero (global). */
17676
17677 static void
17678 add_index_entry (struct mapped_symtab *symtab, const char *name,
17679 int is_static, gdb_index_symbol_kind kind,
17680 offset_type cu_index)
17681 {
17682 struct symtab_index_entry **slot;
17683 offset_type cu_index_and_attrs;
17684
17685 ++symtab->n_elements;
17686 if (4 * symtab->n_elements / 3 >= symtab->size)
17687 hash_expand (symtab);
17688
17689 slot = find_slot (symtab, name);
17690 if (!*slot)
17691 {
17692 *slot = XNEW (struct symtab_index_entry);
17693 (*slot)->name = name;
17694 /* index_offset is set later. */
17695 (*slot)->cu_indices = NULL;
17696 }
17697
17698 cu_index_and_attrs = 0;
17699 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
17700 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
17701 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
17702
17703 /* We don't want to record an index value twice as we want to avoid the
17704 duplication.
17705 We process all global symbols and then all static symbols
17706 (which would allow us to avoid the duplication by only having to check
17707 the last entry pushed), but a symbol could have multiple kinds in one CU.
17708 To keep things simple we don't worry about the duplication here and
17709 sort and uniqufy the list after we've processed all symbols. */
17710 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
17711 }
17712
17713 /* qsort helper routine for uniquify_cu_indices. */
17714
17715 static int
17716 offset_type_compare (const void *ap, const void *bp)
17717 {
17718 offset_type a = *(offset_type *) ap;
17719 offset_type b = *(offset_type *) bp;
17720
17721 return (a > b) - (b > a);
17722 }
17723
17724 /* Sort and remove duplicates of all symbols' cu_indices lists. */
17725
17726 static void
17727 uniquify_cu_indices (struct mapped_symtab *symtab)
17728 {
17729 int i;
17730
17731 for (i = 0; i < symtab->size; ++i)
17732 {
17733 struct symtab_index_entry *entry = symtab->data[i];
17734
17735 if (entry
17736 && entry->cu_indices != NULL)
17737 {
17738 unsigned int next_to_insert, next_to_check;
17739 offset_type last_value;
17740
17741 qsort (VEC_address (offset_type, entry->cu_indices),
17742 VEC_length (offset_type, entry->cu_indices),
17743 sizeof (offset_type), offset_type_compare);
17744
17745 last_value = VEC_index (offset_type, entry->cu_indices, 0);
17746 next_to_insert = 1;
17747 for (next_to_check = 1;
17748 next_to_check < VEC_length (offset_type, entry->cu_indices);
17749 ++next_to_check)
17750 {
17751 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
17752 != last_value)
17753 {
17754 last_value = VEC_index (offset_type, entry->cu_indices,
17755 next_to_check);
17756 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
17757 last_value);
17758 ++next_to_insert;
17759 }
17760 }
17761 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
17762 }
17763 }
17764 }
17765
17766 /* Add a vector of indices to the constant pool. */
17767
17768 static offset_type
17769 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
17770 struct symtab_index_entry *entry)
17771 {
17772 void **slot;
17773
17774 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
17775 if (!*slot)
17776 {
17777 offset_type len = VEC_length (offset_type, entry->cu_indices);
17778 offset_type val = MAYBE_SWAP (len);
17779 offset_type iter;
17780 int i;
17781
17782 *slot = entry;
17783 entry->index_offset = obstack_object_size (cpool);
17784
17785 obstack_grow (cpool, &val, sizeof (val));
17786 for (i = 0;
17787 VEC_iterate (offset_type, entry->cu_indices, i, iter);
17788 ++i)
17789 {
17790 val = MAYBE_SWAP (iter);
17791 obstack_grow (cpool, &val, sizeof (val));
17792 }
17793 }
17794 else
17795 {
17796 struct symtab_index_entry *old_entry = *slot;
17797 entry->index_offset = old_entry->index_offset;
17798 entry = old_entry;
17799 }
17800 return entry->index_offset;
17801 }
17802
17803 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
17804 constant pool entries going into the obstack CPOOL. */
17805
17806 static void
17807 write_hash_table (struct mapped_symtab *symtab,
17808 struct obstack *output, struct obstack *cpool)
17809 {
17810 offset_type i;
17811 htab_t symbol_hash_table;
17812 htab_t str_table;
17813
17814 symbol_hash_table = create_symbol_hash_table ();
17815 str_table = create_strtab ();
17816
17817 /* We add all the index vectors to the constant pool first, to
17818 ensure alignment is ok. */
17819 for (i = 0; i < symtab->size; ++i)
17820 {
17821 if (symtab->data[i])
17822 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
17823 }
17824
17825 /* Now write out the hash table. */
17826 for (i = 0; i < symtab->size; ++i)
17827 {
17828 offset_type str_off, vec_off;
17829
17830 if (symtab->data[i])
17831 {
17832 str_off = add_string (str_table, cpool, symtab->data[i]->name);
17833 vec_off = symtab->data[i]->index_offset;
17834 }
17835 else
17836 {
17837 /* While 0 is a valid constant pool index, it is not valid
17838 to have 0 for both offsets. */
17839 str_off = 0;
17840 vec_off = 0;
17841 }
17842
17843 str_off = MAYBE_SWAP (str_off);
17844 vec_off = MAYBE_SWAP (vec_off);
17845
17846 obstack_grow (output, &str_off, sizeof (str_off));
17847 obstack_grow (output, &vec_off, sizeof (vec_off));
17848 }
17849
17850 htab_delete (str_table);
17851 htab_delete (symbol_hash_table);
17852 }
17853
17854 /* Struct to map psymtab to CU index in the index file. */
17855 struct psymtab_cu_index_map
17856 {
17857 struct partial_symtab *psymtab;
17858 unsigned int cu_index;
17859 };
17860
17861 static hashval_t
17862 hash_psymtab_cu_index (const void *item)
17863 {
17864 const struct psymtab_cu_index_map *map = item;
17865
17866 return htab_hash_pointer (map->psymtab);
17867 }
17868
17869 static int
17870 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
17871 {
17872 const struct psymtab_cu_index_map *lhs = item_lhs;
17873 const struct psymtab_cu_index_map *rhs = item_rhs;
17874
17875 return lhs->psymtab == rhs->psymtab;
17876 }
17877
17878 /* Helper struct for building the address table. */
17879 struct addrmap_index_data
17880 {
17881 struct objfile *objfile;
17882 struct obstack *addr_obstack;
17883 htab_t cu_index_htab;
17884
17885 /* Non-zero if the previous_* fields are valid.
17886 We can't write an entry until we see the next entry (since it is only then
17887 that we know the end of the entry). */
17888 int previous_valid;
17889 /* Index of the CU in the table of all CUs in the index file. */
17890 unsigned int previous_cu_index;
17891 /* Start address of the CU. */
17892 CORE_ADDR previous_cu_start;
17893 };
17894
17895 /* Write an address entry to OBSTACK. */
17896
17897 static void
17898 add_address_entry (struct objfile *objfile, struct obstack *obstack,
17899 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
17900 {
17901 offset_type cu_index_to_write;
17902 char addr[8];
17903 CORE_ADDR baseaddr;
17904
17905 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
17906
17907 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
17908 obstack_grow (obstack, addr, 8);
17909 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
17910 obstack_grow (obstack, addr, 8);
17911 cu_index_to_write = MAYBE_SWAP (cu_index);
17912 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
17913 }
17914
17915 /* Worker function for traversing an addrmap to build the address table. */
17916
17917 static int
17918 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
17919 {
17920 struct addrmap_index_data *data = datap;
17921 struct partial_symtab *pst = obj;
17922
17923 if (data->previous_valid)
17924 add_address_entry (data->objfile, data->addr_obstack,
17925 data->previous_cu_start, start_addr,
17926 data->previous_cu_index);
17927
17928 data->previous_cu_start = start_addr;
17929 if (pst != NULL)
17930 {
17931 struct psymtab_cu_index_map find_map, *map;
17932 find_map.psymtab = pst;
17933 map = htab_find (data->cu_index_htab, &find_map);
17934 gdb_assert (map != NULL);
17935 data->previous_cu_index = map->cu_index;
17936 data->previous_valid = 1;
17937 }
17938 else
17939 data->previous_valid = 0;
17940
17941 return 0;
17942 }
17943
17944 /* Write OBJFILE's address map to OBSTACK.
17945 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
17946 in the index file. */
17947
17948 static void
17949 write_address_map (struct objfile *objfile, struct obstack *obstack,
17950 htab_t cu_index_htab)
17951 {
17952 struct addrmap_index_data addrmap_index_data;
17953
17954 /* When writing the address table, we have to cope with the fact that
17955 the addrmap iterator only provides the start of a region; we have to
17956 wait until the next invocation to get the start of the next region. */
17957
17958 addrmap_index_data.objfile = objfile;
17959 addrmap_index_data.addr_obstack = obstack;
17960 addrmap_index_data.cu_index_htab = cu_index_htab;
17961 addrmap_index_data.previous_valid = 0;
17962
17963 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
17964 &addrmap_index_data);
17965
17966 /* It's highly unlikely the last entry (end address = 0xff...ff)
17967 is valid, but we should still handle it.
17968 The end address is recorded as the start of the next region, but that
17969 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
17970 anyway. */
17971 if (addrmap_index_data.previous_valid)
17972 add_address_entry (objfile, obstack,
17973 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
17974 addrmap_index_data.previous_cu_index);
17975 }
17976
17977 /* Return the symbol kind of PSYM. */
17978
17979 static gdb_index_symbol_kind
17980 symbol_kind (struct partial_symbol *psym)
17981 {
17982 domain_enum domain = PSYMBOL_DOMAIN (psym);
17983 enum address_class aclass = PSYMBOL_CLASS (psym);
17984
17985 switch (domain)
17986 {
17987 case VAR_DOMAIN:
17988 switch (aclass)
17989 {
17990 case LOC_BLOCK:
17991 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
17992 case LOC_TYPEDEF:
17993 return GDB_INDEX_SYMBOL_KIND_TYPE;
17994 case LOC_COMPUTED:
17995 case LOC_CONST_BYTES:
17996 case LOC_OPTIMIZED_OUT:
17997 case LOC_STATIC:
17998 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
17999 case LOC_CONST:
18000 /* Note: It's currently impossible to recognize psyms as enum values
18001 short of reading the type info. For now punt. */
18002 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
18003 default:
18004 /* There are other LOC_FOO values that one might want to classify
18005 as variables, but dwarf2read.c doesn't currently use them. */
18006 return GDB_INDEX_SYMBOL_KIND_OTHER;
18007 }
18008 case STRUCT_DOMAIN:
18009 return GDB_INDEX_SYMBOL_KIND_TYPE;
18010 default:
18011 return GDB_INDEX_SYMBOL_KIND_OTHER;
18012 }
18013 }
18014
18015 /* Add a list of partial symbols to SYMTAB. */
18016
18017 static void
18018 write_psymbols (struct mapped_symtab *symtab,
18019 htab_t psyms_seen,
18020 struct partial_symbol **psymp,
18021 int count,
18022 offset_type cu_index,
18023 int is_static)
18024 {
18025 for (; count-- > 0; ++psymp)
18026 {
18027 struct partial_symbol *psym = *psymp;
18028 void **slot;
18029
18030 if (SYMBOL_LANGUAGE (psym) == language_ada)
18031 error (_("Ada is not currently supported by the index"));
18032
18033 /* Only add a given psymbol once. */
18034 slot = htab_find_slot (psyms_seen, psym, INSERT);
18035 if (!*slot)
18036 {
18037 gdb_index_symbol_kind kind = symbol_kind (psym);
18038
18039 *slot = psym;
18040 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
18041 is_static, kind, cu_index);
18042 }
18043 }
18044 }
18045
18046 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
18047 exception if there is an error. */
18048
18049 static void
18050 write_obstack (FILE *file, struct obstack *obstack)
18051 {
18052 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
18053 file)
18054 != obstack_object_size (obstack))
18055 error (_("couldn't data write to file"));
18056 }
18057
18058 /* Unlink a file if the argument is not NULL. */
18059
18060 static void
18061 unlink_if_set (void *p)
18062 {
18063 char **filename = p;
18064 if (*filename)
18065 unlink (*filename);
18066 }
18067
18068 /* A helper struct used when iterating over debug_types. */
18069 struct signatured_type_index_data
18070 {
18071 struct objfile *objfile;
18072 struct mapped_symtab *symtab;
18073 struct obstack *types_list;
18074 htab_t psyms_seen;
18075 int cu_index;
18076 };
18077
18078 /* A helper function that writes a single signatured_type to an
18079 obstack. */
18080
18081 static int
18082 write_one_signatured_type (void **slot, void *d)
18083 {
18084 struct signatured_type_index_data *info = d;
18085 struct signatured_type *entry = (struct signatured_type *) *slot;
18086 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
18087 struct partial_symtab *psymtab = per_cu->v.psymtab;
18088 gdb_byte val[8];
18089
18090 write_psymbols (info->symtab,
18091 info->psyms_seen,
18092 info->objfile->global_psymbols.list
18093 + psymtab->globals_offset,
18094 psymtab->n_global_syms, info->cu_index,
18095 0);
18096 write_psymbols (info->symtab,
18097 info->psyms_seen,
18098 info->objfile->static_psymbols.list
18099 + psymtab->statics_offset,
18100 psymtab->n_static_syms, info->cu_index,
18101 1);
18102
18103 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
18104 entry->per_cu.offset.sect_off);
18105 obstack_grow (info->types_list, val, 8);
18106 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
18107 entry->type_offset_in_tu.cu_off);
18108 obstack_grow (info->types_list, val, 8);
18109 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
18110 obstack_grow (info->types_list, val, 8);
18111
18112 ++info->cu_index;
18113
18114 return 1;
18115 }
18116
18117 /* Recurse into all "included" dependencies and write their symbols as
18118 if they appeared in this psymtab. */
18119
18120 static void
18121 recursively_write_psymbols (struct objfile *objfile,
18122 struct partial_symtab *psymtab,
18123 struct mapped_symtab *symtab,
18124 htab_t psyms_seen,
18125 offset_type cu_index)
18126 {
18127 int i;
18128
18129 for (i = 0; i < psymtab->number_of_dependencies; ++i)
18130 if (psymtab->dependencies[i]->user != NULL)
18131 recursively_write_psymbols (objfile, psymtab->dependencies[i],
18132 symtab, psyms_seen, cu_index);
18133
18134 write_psymbols (symtab,
18135 psyms_seen,
18136 objfile->global_psymbols.list + psymtab->globals_offset,
18137 psymtab->n_global_syms, cu_index,
18138 0);
18139 write_psymbols (symtab,
18140 psyms_seen,
18141 objfile->static_psymbols.list + psymtab->statics_offset,
18142 psymtab->n_static_syms, cu_index,
18143 1);
18144 }
18145
18146 /* Create an index file for OBJFILE in the directory DIR. */
18147
18148 static void
18149 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
18150 {
18151 struct cleanup *cleanup;
18152 char *filename, *cleanup_filename;
18153 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
18154 struct obstack cu_list, types_cu_list;
18155 int i;
18156 FILE *out_file;
18157 struct mapped_symtab *symtab;
18158 offset_type val, size_of_contents, total_len;
18159 struct stat st;
18160 htab_t psyms_seen;
18161 htab_t cu_index_htab;
18162 struct psymtab_cu_index_map *psymtab_cu_index_map;
18163
18164 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
18165 return;
18166
18167 if (dwarf2_per_objfile->using_index)
18168 error (_("Cannot use an index to create the index"));
18169
18170 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
18171 error (_("Cannot make an index when the file has multiple .debug_types sections"));
18172
18173 if (stat (objfile->name, &st) < 0)
18174 perror_with_name (objfile->name);
18175
18176 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
18177 INDEX_SUFFIX, (char *) NULL);
18178 cleanup = make_cleanup (xfree, filename);
18179
18180 out_file = fopen (filename, "wb");
18181 if (!out_file)
18182 error (_("Can't open `%s' for writing"), filename);
18183
18184 cleanup_filename = filename;
18185 make_cleanup (unlink_if_set, &cleanup_filename);
18186
18187 symtab = create_mapped_symtab ();
18188 make_cleanup (cleanup_mapped_symtab, symtab);
18189
18190 obstack_init (&addr_obstack);
18191 make_cleanup_obstack_free (&addr_obstack);
18192
18193 obstack_init (&cu_list);
18194 make_cleanup_obstack_free (&cu_list);
18195
18196 obstack_init (&types_cu_list);
18197 make_cleanup_obstack_free (&types_cu_list);
18198
18199 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
18200 NULL, xcalloc, xfree);
18201 make_cleanup_htab_delete (psyms_seen);
18202
18203 /* While we're scanning CU's create a table that maps a psymtab pointer
18204 (which is what addrmap records) to its index (which is what is recorded
18205 in the index file). This will later be needed to write the address
18206 table. */
18207 cu_index_htab = htab_create_alloc (100,
18208 hash_psymtab_cu_index,
18209 eq_psymtab_cu_index,
18210 NULL, xcalloc, xfree);
18211 make_cleanup_htab_delete (cu_index_htab);
18212 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
18213 xmalloc (sizeof (struct psymtab_cu_index_map)
18214 * dwarf2_per_objfile->n_comp_units);
18215 make_cleanup (xfree, psymtab_cu_index_map);
18216
18217 /* The CU list is already sorted, so we don't need to do additional
18218 work here. Also, the debug_types entries do not appear in
18219 all_comp_units, but only in their own hash table. */
18220 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
18221 {
18222 struct dwarf2_per_cu_data *per_cu
18223 = dwarf2_per_objfile->all_comp_units[i];
18224 struct partial_symtab *psymtab = per_cu->v.psymtab;
18225 gdb_byte val[8];
18226 struct psymtab_cu_index_map *map;
18227 void **slot;
18228
18229 if (psymtab->user == NULL)
18230 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
18231
18232 map = &psymtab_cu_index_map[i];
18233 map->psymtab = psymtab;
18234 map->cu_index = i;
18235 slot = htab_find_slot (cu_index_htab, map, INSERT);
18236 gdb_assert (slot != NULL);
18237 gdb_assert (*slot == NULL);
18238 *slot = map;
18239
18240 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
18241 per_cu->offset.sect_off);
18242 obstack_grow (&cu_list, val, 8);
18243 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
18244 obstack_grow (&cu_list, val, 8);
18245 }
18246
18247 /* Dump the address map. */
18248 write_address_map (objfile, &addr_obstack, cu_index_htab);
18249
18250 /* Write out the .debug_type entries, if any. */
18251 if (dwarf2_per_objfile->signatured_types)
18252 {
18253 struct signatured_type_index_data sig_data;
18254
18255 sig_data.objfile = objfile;
18256 sig_data.symtab = symtab;
18257 sig_data.types_list = &types_cu_list;
18258 sig_data.psyms_seen = psyms_seen;
18259 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
18260 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
18261 write_one_signatured_type, &sig_data);
18262 }
18263
18264 /* Now that we've processed all symbols we can shrink their cu_indices
18265 lists. */
18266 uniquify_cu_indices (symtab);
18267
18268 obstack_init (&constant_pool);
18269 make_cleanup_obstack_free (&constant_pool);
18270 obstack_init (&symtab_obstack);
18271 make_cleanup_obstack_free (&symtab_obstack);
18272 write_hash_table (symtab, &symtab_obstack, &constant_pool);
18273
18274 obstack_init (&contents);
18275 make_cleanup_obstack_free (&contents);
18276 size_of_contents = 6 * sizeof (offset_type);
18277 total_len = size_of_contents;
18278
18279 /* The version number. */
18280 val = MAYBE_SWAP (7);
18281 obstack_grow (&contents, &val, sizeof (val));
18282
18283 /* The offset of the CU list from the start of the file. */
18284 val = MAYBE_SWAP (total_len);
18285 obstack_grow (&contents, &val, sizeof (val));
18286 total_len += obstack_object_size (&cu_list);
18287
18288 /* The offset of the types CU list from the start of the file. */
18289 val = MAYBE_SWAP (total_len);
18290 obstack_grow (&contents, &val, sizeof (val));
18291 total_len += obstack_object_size (&types_cu_list);
18292
18293 /* The offset of the address table from the start of the file. */
18294 val = MAYBE_SWAP (total_len);
18295 obstack_grow (&contents, &val, sizeof (val));
18296 total_len += obstack_object_size (&addr_obstack);
18297
18298 /* The offset of the symbol table from the start of the file. */
18299 val = MAYBE_SWAP (total_len);
18300 obstack_grow (&contents, &val, sizeof (val));
18301 total_len += obstack_object_size (&symtab_obstack);
18302
18303 /* The offset of the constant pool from the start of the file. */
18304 val = MAYBE_SWAP (total_len);
18305 obstack_grow (&contents, &val, sizeof (val));
18306 total_len += obstack_object_size (&constant_pool);
18307
18308 gdb_assert (obstack_object_size (&contents) == size_of_contents);
18309
18310 write_obstack (out_file, &contents);
18311 write_obstack (out_file, &cu_list);
18312 write_obstack (out_file, &types_cu_list);
18313 write_obstack (out_file, &addr_obstack);
18314 write_obstack (out_file, &symtab_obstack);
18315 write_obstack (out_file, &constant_pool);
18316
18317 fclose (out_file);
18318
18319 /* We want to keep the file, so we set cleanup_filename to NULL
18320 here. See unlink_if_set. */
18321 cleanup_filename = NULL;
18322
18323 do_cleanups (cleanup);
18324 }
18325
18326 /* Implementation of the `save gdb-index' command.
18327
18328 Note that the file format used by this command is documented in the
18329 GDB manual. Any changes here must be documented there. */
18330
18331 static void
18332 save_gdb_index_command (char *arg, int from_tty)
18333 {
18334 struct objfile *objfile;
18335
18336 if (!arg || !*arg)
18337 error (_("usage: save gdb-index DIRECTORY"));
18338
18339 ALL_OBJFILES (objfile)
18340 {
18341 struct stat st;
18342
18343 /* If the objfile does not correspond to an actual file, skip it. */
18344 if (stat (objfile->name, &st) < 0)
18345 continue;
18346
18347 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
18348 if (dwarf2_per_objfile)
18349 {
18350 volatile struct gdb_exception except;
18351
18352 TRY_CATCH (except, RETURN_MASK_ERROR)
18353 {
18354 write_psymtabs_to_index (objfile, arg);
18355 }
18356 if (except.reason < 0)
18357 exception_fprintf (gdb_stderr, except,
18358 _("Error while writing index for `%s': "),
18359 objfile->name);
18360 }
18361 }
18362 }
18363
18364 \f
18365
18366 int dwarf2_always_disassemble;
18367
18368 static void
18369 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
18370 struct cmd_list_element *c, const char *value)
18371 {
18372 fprintf_filtered (file,
18373 _("Whether to always disassemble "
18374 "DWARF expressions is %s.\n"),
18375 value);
18376 }
18377
18378 static void
18379 show_check_physname (struct ui_file *file, int from_tty,
18380 struct cmd_list_element *c, const char *value)
18381 {
18382 fprintf_filtered (file,
18383 _("Whether to check \"physname\" is %s.\n"),
18384 value);
18385 }
18386
18387 void _initialize_dwarf2_read (void);
18388
18389 void
18390 _initialize_dwarf2_read (void)
18391 {
18392 struct cmd_list_element *c;
18393
18394 dwarf2_objfile_data_key
18395 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
18396
18397 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
18398 Set DWARF 2 specific variables.\n\
18399 Configure DWARF 2 variables such as the cache size"),
18400 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
18401 0/*allow-unknown*/, &maintenance_set_cmdlist);
18402
18403 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
18404 Show DWARF 2 specific variables\n\
18405 Show DWARF 2 variables such as the cache size"),
18406 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
18407 0/*allow-unknown*/, &maintenance_show_cmdlist);
18408
18409 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
18410 &dwarf2_max_cache_age, _("\
18411 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
18412 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
18413 A higher limit means that cached compilation units will be stored\n\
18414 in memory longer, and more total memory will be used. Zero disables\n\
18415 caching, which can slow down startup."),
18416 NULL,
18417 show_dwarf2_max_cache_age,
18418 &set_dwarf2_cmdlist,
18419 &show_dwarf2_cmdlist);
18420
18421 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
18422 &dwarf2_always_disassemble, _("\
18423 Set whether `info address' always disassembles DWARF expressions."), _("\
18424 Show whether `info address' always disassembles DWARF expressions."), _("\
18425 When enabled, DWARF expressions are always printed in an assembly-like\n\
18426 syntax. When disabled, expressions will be printed in a more\n\
18427 conversational style, when possible."),
18428 NULL,
18429 show_dwarf2_always_disassemble,
18430 &set_dwarf2_cmdlist,
18431 &show_dwarf2_cmdlist);
18432
18433 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
18434 Set debugging of the dwarf2 reader."), _("\
18435 Show debugging of the dwarf2 reader."), _("\
18436 When enabled, debugging messages are printed during dwarf2 reading\n\
18437 and symtab expansion."),
18438 NULL,
18439 NULL,
18440 &setdebuglist, &showdebuglist);
18441
18442 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
18443 Set debugging of the dwarf2 DIE reader."), _("\
18444 Show debugging of the dwarf2 DIE reader."), _("\
18445 When enabled (non-zero), DIEs are dumped after they are read in.\n\
18446 The value is the maximum depth to print."),
18447 NULL,
18448 NULL,
18449 &setdebuglist, &showdebuglist);
18450
18451 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
18452 Set cross-checking of \"physname\" code against demangler."), _("\
18453 Show cross-checking of \"physname\" code against demangler."), _("\
18454 When enabled, GDB's internal \"physname\" code is checked against\n\
18455 the demangler."),
18456 NULL, show_check_physname,
18457 &setdebuglist, &showdebuglist);
18458
18459 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
18460 _("\
18461 Save a gdb-index file.\n\
18462 Usage: save gdb-index DIRECTORY"),
18463 &save_cmdlist);
18464 set_cmd_completer (c, filename_completer);
18465 }