* dwarf2read.c (dwarf2_cu): Add ranges_base.
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2012 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "bfd.h"
33 #include "symtab.h"
34 #include "gdbtypes.h"
35 #include "objfiles.h"
36 #include "dwarf2.h"
37 #include "buildsym.h"
38 #include "demangle.h"
39 #include "gdb-demangle.h"
40 #include "expression.h"
41 #include "filenames.h" /* for DOSish file names */
42 #include "macrotab.h"
43 #include "language.h"
44 #include "complaints.h"
45 #include "bcache.h"
46 #include "dwarf2expr.h"
47 #include "dwarf2loc.h"
48 #include "cp-support.h"
49 #include "hashtab.h"
50 #include "command.h"
51 #include "gdbcmd.h"
52 #include "block.h"
53 #include "addrmap.h"
54 #include "typeprint.h"
55 #include "jv-lang.h"
56 #include "psympriv.h"
57 #include "exceptions.h"
58 #include "gdb_stat.h"
59 #include "completer.h"
60 #include "vec.h"
61 #include "c-lang.h"
62 #include "go-lang.h"
63 #include "valprint.h"
64 #include "gdbcore.h" /* for gnutarget */
65 #include "gdb/gdb-index.h"
66 #include <ctype.h>
67
68 #include <fcntl.h>
69 #include "gdb_string.h"
70 #include "gdb_assert.h"
71 #include <sys/types.h>
72 #ifdef HAVE_ZLIB_H
73 #include <zlib.h>
74 #endif
75 #ifdef HAVE_MMAP
76 #include <sys/mman.h>
77 #ifndef MAP_FAILED
78 #define MAP_FAILED ((void *) -1)
79 #endif
80 #endif
81
82 typedef struct symbol *symbolp;
83 DEF_VEC_P (symbolp);
84
85 /* When non-zero, print basic high level tracing messages.
86 This is in contrast to the low level DIE reading of dwarf2_die_debug. */
87 static int dwarf2_read_debug = 0;
88
89 /* When non-zero, dump DIEs after they are read in. */
90 static int dwarf2_die_debug = 0;
91
92 /* When non-zero, cross-check physname against demangler. */
93 static int check_physname = 0;
94
95 /* When non-zero, do not reject deprecated .gdb_index sections. */
96 int use_deprecated_index_sections = 0;
97
98 static int pagesize;
99
100 /* When set, the file that we're processing is known to have debugging
101 info for C++ namespaces. GCC 3.3.x did not produce this information,
102 but later versions do. */
103
104 static int processing_has_namespace_info;
105
106 static const struct objfile_data *dwarf2_objfile_data_key;
107
108 struct dwarf2_section_info
109 {
110 asection *asection;
111 gdb_byte *buffer;
112 bfd_size_type size;
113 /* Not NULL if the section was actually mmapped. */
114 void *map_addr;
115 /* Page aligned size of mmapped area. */
116 bfd_size_type map_len;
117 /* True if we have tried to read this section. */
118 int readin;
119 };
120
121 typedef struct dwarf2_section_info dwarf2_section_info_def;
122 DEF_VEC_O (dwarf2_section_info_def);
123
124 /* All offsets in the index are of this type. It must be
125 architecture-independent. */
126 typedef uint32_t offset_type;
127
128 DEF_VEC_I (offset_type);
129
130 /* Ensure only legit values are used. */
131 #define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
132 do { \
133 gdb_assert ((unsigned int) (value) <= 1); \
134 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
135 } while (0)
136
137 /* Ensure only legit values are used. */
138 #define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
139 do { \
140 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
141 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
142 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
143 } while (0)
144
145 /* Ensure we don't use more than the alloted nuber of bits for the CU. */
146 #define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
147 do { \
148 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
149 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
150 } while (0)
151
152 /* A description of the mapped index. The file format is described in
153 a comment by the code that writes the index. */
154 struct mapped_index
155 {
156 /* Index data format version. */
157 int version;
158
159 /* The total length of the buffer. */
160 off_t total_size;
161
162 /* A pointer to the address table data. */
163 const gdb_byte *address_table;
164
165 /* Size of the address table data in bytes. */
166 offset_type address_table_size;
167
168 /* The symbol table, implemented as a hash table. */
169 const offset_type *symbol_table;
170
171 /* Size in slots, each slot is 2 offset_types. */
172 offset_type symbol_table_slots;
173
174 /* A pointer to the constant pool. */
175 const char *constant_pool;
176 };
177
178 typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
179 DEF_VEC_P (dwarf2_per_cu_ptr);
180
181 /* Collection of data recorded per objfile.
182 This hangs off of dwarf2_objfile_data_key. */
183
184 struct dwarf2_per_objfile
185 {
186 struct dwarf2_section_info info;
187 struct dwarf2_section_info abbrev;
188 struct dwarf2_section_info line;
189 struct dwarf2_section_info loc;
190 struct dwarf2_section_info macinfo;
191 struct dwarf2_section_info macro;
192 struct dwarf2_section_info str;
193 struct dwarf2_section_info ranges;
194 struct dwarf2_section_info addr;
195 struct dwarf2_section_info frame;
196 struct dwarf2_section_info eh_frame;
197 struct dwarf2_section_info gdb_index;
198
199 VEC (dwarf2_section_info_def) *types;
200
201 /* Back link. */
202 struct objfile *objfile;
203
204 /* Table of all the compilation units. This is used to locate
205 the target compilation unit of a particular reference. */
206 struct dwarf2_per_cu_data **all_comp_units;
207
208 /* The number of compilation units in ALL_COMP_UNITS. */
209 int n_comp_units;
210
211 /* The number of .debug_types-related CUs. */
212 int n_type_units;
213
214 /* The .debug_types-related CUs (TUs). */
215 struct dwarf2_per_cu_data **all_type_units;
216
217 /* A chain of compilation units that are currently read in, so that
218 they can be freed later. */
219 struct dwarf2_per_cu_data *read_in_chain;
220
221 /* A table mapping .debug_types signatures to its signatured_type entry.
222 This is NULL if the .debug_types section hasn't been read in yet. */
223 htab_t signatured_types;
224
225 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
226 This is NULL if the table hasn't been allocated yet. */
227 htab_t dwo_files;
228
229 /* A flag indicating wether this objfile has a section loaded at a
230 VMA of 0. */
231 int has_section_at_zero;
232
233 /* True if we are using the mapped index,
234 or we are faking it for OBJF_READNOW's sake. */
235 unsigned char using_index;
236
237 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
238 struct mapped_index *index_table;
239
240 /* When using index_table, this keeps track of all quick_file_names entries.
241 TUs can share line table entries with CUs or other TUs, and there can be
242 a lot more TUs than unique line tables, so we maintain a separate table
243 of all line table entries to support the sharing. */
244 htab_t quick_file_names_table;
245
246 /* Set during partial symbol reading, to prevent queueing of full
247 symbols. */
248 int reading_partial_symbols;
249
250 /* Table mapping type DIEs to their struct type *.
251 This is NULL if not allocated yet.
252 The mapping is done via (CU/TU signature + DIE offset) -> type. */
253 htab_t die_type_hash;
254
255 /* The CUs we recently read. */
256 VEC (dwarf2_per_cu_ptr) *just_read_cus;
257 };
258
259 static struct dwarf2_per_objfile *dwarf2_per_objfile;
260
261 /* Default names of the debugging sections. */
262
263 /* Note that if the debugging section has been compressed, it might
264 have a name like .zdebug_info. */
265
266 static const struct dwarf2_debug_sections dwarf2_elf_names =
267 {
268 { ".debug_info", ".zdebug_info" },
269 { ".debug_abbrev", ".zdebug_abbrev" },
270 { ".debug_line", ".zdebug_line" },
271 { ".debug_loc", ".zdebug_loc" },
272 { ".debug_macinfo", ".zdebug_macinfo" },
273 { ".debug_macro", ".zdebug_macro" },
274 { ".debug_str", ".zdebug_str" },
275 { ".debug_ranges", ".zdebug_ranges" },
276 { ".debug_types", ".zdebug_types" },
277 { ".debug_addr", ".zdebug_addr" },
278 { ".debug_frame", ".zdebug_frame" },
279 { ".eh_frame", NULL },
280 { ".gdb_index", ".zgdb_index" },
281 23
282 };
283
284 /* List of DWO sections. */
285
286 static const struct dwo_section_names
287 {
288 struct dwarf2_section_names abbrev_dwo;
289 struct dwarf2_section_names info_dwo;
290 struct dwarf2_section_names line_dwo;
291 struct dwarf2_section_names loc_dwo;
292 struct dwarf2_section_names macinfo_dwo;
293 struct dwarf2_section_names macro_dwo;
294 struct dwarf2_section_names str_dwo;
295 struct dwarf2_section_names str_offsets_dwo;
296 struct dwarf2_section_names types_dwo;
297 }
298 dwo_section_names =
299 {
300 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
301 { ".debug_info.dwo", ".zdebug_info.dwo" },
302 { ".debug_line.dwo", ".zdebug_line.dwo" },
303 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
304 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
305 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
306 { ".debug_str.dwo", ".zdebug_str.dwo" },
307 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
308 { ".debug_types.dwo", ".zdebug_types.dwo" },
309 };
310
311 /* local data types */
312
313 /* We hold several abbreviation tables in memory at the same time. */
314 #ifndef ABBREV_HASH_SIZE
315 #define ABBREV_HASH_SIZE 121
316 #endif
317
318 /* The data in a compilation unit header, after target2host
319 translation, looks like this. */
320 struct comp_unit_head
321 {
322 unsigned int length;
323 short version;
324 unsigned char addr_size;
325 unsigned char signed_addr_p;
326 sect_offset abbrev_offset;
327
328 /* Size of file offsets; either 4 or 8. */
329 unsigned int offset_size;
330
331 /* Size of the length field; either 4 or 12. */
332 unsigned int initial_length_size;
333
334 /* Offset to the first byte of this compilation unit header in the
335 .debug_info section, for resolving relative reference dies. */
336 sect_offset offset;
337
338 /* Offset to first die in this cu from the start of the cu.
339 This will be the first byte following the compilation unit header. */
340 cu_offset first_die_offset;
341 };
342
343 /* Type used for delaying computation of method physnames.
344 See comments for compute_delayed_physnames. */
345 struct delayed_method_info
346 {
347 /* The type to which the method is attached, i.e., its parent class. */
348 struct type *type;
349
350 /* The index of the method in the type's function fieldlists. */
351 int fnfield_index;
352
353 /* The index of the method in the fieldlist. */
354 int index;
355
356 /* The name of the DIE. */
357 const char *name;
358
359 /* The DIE associated with this method. */
360 struct die_info *die;
361 };
362
363 typedef struct delayed_method_info delayed_method_info;
364 DEF_VEC_O (delayed_method_info);
365
366 /* Internal state when decoding a particular compilation unit. */
367 struct dwarf2_cu
368 {
369 /* The objfile containing this compilation unit. */
370 struct objfile *objfile;
371
372 /* The header of the compilation unit. */
373 struct comp_unit_head header;
374
375 /* Base address of this compilation unit. */
376 CORE_ADDR base_address;
377
378 /* Non-zero if base_address has been set. */
379 int base_known;
380
381 /* The language we are debugging. */
382 enum language language;
383 const struct language_defn *language_defn;
384
385 const char *producer;
386
387 /* The generic symbol table building routines have separate lists for
388 file scope symbols and all all other scopes (local scopes). So
389 we need to select the right one to pass to add_symbol_to_list().
390 We do it by keeping a pointer to the correct list in list_in_scope.
391
392 FIXME: The original dwarf code just treated the file scope as the
393 first local scope, and all other local scopes as nested local
394 scopes, and worked fine. Check to see if we really need to
395 distinguish these in buildsym.c. */
396 struct pending **list_in_scope;
397
398 /* DWARF abbreviation table associated with this compilation unit. */
399 struct abbrev_info **dwarf2_abbrevs;
400
401 /* Storage for the abbrev table. */
402 struct obstack abbrev_obstack;
403
404 /* Hash table holding all the loaded partial DIEs
405 with partial_die->offset.SECT_OFF as hash. */
406 htab_t partial_dies;
407
408 /* Storage for things with the same lifetime as this read-in compilation
409 unit, including partial DIEs. */
410 struct obstack comp_unit_obstack;
411
412 /* When multiple dwarf2_cu structures are living in memory, this field
413 chains them all together, so that they can be released efficiently.
414 We will probably also want a generation counter so that most-recently-used
415 compilation units are cached... */
416 struct dwarf2_per_cu_data *read_in_chain;
417
418 /* Backchain to our per_cu entry if the tree has been built. */
419 struct dwarf2_per_cu_data *per_cu;
420
421 /* How many compilation units ago was this CU last referenced? */
422 int last_used;
423
424 /* A hash table of DIE cu_offset for following references with
425 die_info->offset.sect_off as hash. */
426 htab_t die_hash;
427
428 /* Full DIEs if read in. */
429 struct die_info *dies;
430
431 /* A set of pointers to dwarf2_per_cu_data objects for compilation
432 units referenced by this one. Only set during full symbol processing;
433 partial symbol tables do not have dependencies. */
434 htab_t dependencies;
435
436 /* Header data from the line table, during full symbol processing. */
437 struct line_header *line_header;
438
439 /* A list of methods which need to have physnames computed
440 after all type information has been read. */
441 VEC (delayed_method_info) *method_list;
442
443 /* To be copied to symtab->call_site_htab. */
444 htab_t call_site_htab;
445
446 /* Non-NULL if this CU came from a DWO file.
447 There is an invariant here that is important to remember:
448 Except for attributes copied from the top level DIE in the "main"
449 (or "stub") file in preparation for reading the DWO file
450 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
451 Either there isn't a DWO file (in which case this is NULL and the point
452 is moot), or there is and either we're not going to read it (in which
453 case this is NULL) or there is and we are reading it (in which case this
454 is non-NULL). */
455 struct dwo_unit *dwo_unit;
456
457 /* The DW_AT_addr_base attribute if present, zero otherwise
458 (zero is a valid value though).
459 Note this value comes from the stub CU/TU's DIE. */
460 ULONGEST addr_base;
461
462 /* The DW_AT_ranges_base attribute if present, zero otherwise
463 (zero is a valid value though).
464 Note this value comes from the stub CU/TU's DIE.
465 Also note that the value is zero in the non-DWO case so this value can
466 be used without needing to know whether DWO files are in use or not. */
467 ULONGEST ranges_base;
468
469 /* Mark used when releasing cached dies. */
470 unsigned int mark : 1;
471
472 /* This CU references .debug_loc. See the symtab->locations_valid field.
473 This test is imperfect as there may exist optimized debug code not using
474 any location list and still facing inlining issues if handled as
475 unoptimized code. For a future better test see GCC PR other/32998. */
476 unsigned int has_loclist : 1;
477
478 /* These cache the results for producer_is_gxx_lt_4_6 and producer_is_icc.
479 CHECKED_PRODUCER is set if both PRODUCER_IS_GXX_LT_4_6 and PRODUCER_IS_ICC
480 are valid. This information is cached because profiling CU expansion
481 showed excessive time spent in producer_is_gxx_lt_4_6. */
482 unsigned int checked_producer : 1;
483 unsigned int producer_is_gxx_lt_4_6 : 1;
484 unsigned int producer_is_icc : 1;
485 };
486
487 /* Persistent data held for a compilation unit, even when not
488 processing it. We put a pointer to this structure in the
489 read_symtab_private field of the psymtab. */
490
491 struct dwarf2_per_cu_data
492 {
493 /* The start offset and length of this compilation unit. 2**29-1
494 bytes should suffice to store the length of any compilation unit
495 - if it doesn't, GDB will fall over anyway.
496 NOTE: Unlike comp_unit_head.length, this length includes
497 initial_length_size.
498 If the DIE refers to a DWO file, this is always of the original die,
499 not the DWO file. */
500 sect_offset offset;
501 unsigned int length : 29;
502
503 /* Flag indicating this compilation unit will be read in before
504 any of the current compilation units are processed. */
505 unsigned int queued : 1;
506
507 /* This flag will be set when reading partial DIEs if we need to load
508 absolutely all DIEs for this compilation unit, instead of just the ones
509 we think are interesting. It gets set if we look for a DIE in the
510 hash table and don't find it. */
511 unsigned int load_all_dies : 1;
512
513 /* Non-zero if this CU is from .debug_types. */
514 unsigned int is_debug_types : 1;
515
516 /* The section this CU/TU lives in.
517 If the DIE refers to a DWO file, this is always the original die,
518 not the DWO file. */
519 struct dwarf2_section_info *info_or_types_section;
520
521 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
522 of the CU cache it gets reset to NULL again. */
523 struct dwarf2_cu *cu;
524
525 /* The corresponding objfile.
526 Normally we can get the objfile from dwarf2_per_objfile.
527 However we can enter this file with just a "per_cu" handle. */
528 struct objfile *objfile;
529
530 /* When using partial symbol tables, the 'psymtab' field is active.
531 Otherwise the 'quick' field is active. */
532 union
533 {
534 /* The partial symbol table associated with this compilation unit,
535 or NULL for unread partial units. */
536 struct partial_symtab *psymtab;
537
538 /* Data needed by the "quick" functions. */
539 struct dwarf2_per_cu_quick_data *quick;
540 } v;
541
542 /* The CUs we import using DW_TAG_imported_unit. This is filled in
543 while reading psymtabs, used to compute the psymtab dependencies,
544 and then cleared. Then it is filled in again while reading full
545 symbols, and only deleted when the objfile is destroyed. */
546 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
547 };
548
549 /* Entry in the signatured_types hash table. */
550
551 struct signatured_type
552 {
553 /* The type's signature. */
554 ULONGEST signature;
555
556 /* Offset in the TU of the type's DIE, as read from the TU header.
557 If the definition lives in a DWO file, this value is unusable. */
558 cu_offset type_offset_in_tu;
559
560 /* Offset in the section of the type's DIE.
561 If the definition lives in a DWO file, this is the offset in the
562 .debug_types.dwo section.
563 The value is zero until the actual value is known.
564 Zero is otherwise not a valid section offset. */
565 sect_offset type_offset_in_section;
566
567 /* The CU(/TU) of this type. */
568 struct dwarf2_per_cu_data per_cu;
569 };
570
571 /* These sections are what may appear in a "dwo" file. */
572
573 struct dwo_sections
574 {
575 struct dwarf2_section_info abbrev;
576 struct dwarf2_section_info info;
577 struct dwarf2_section_info line;
578 struct dwarf2_section_info loc;
579 struct dwarf2_section_info macinfo;
580 struct dwarf2_section_info macro;
581 struct dwarf2_section_info str;
582 struct dwarf2_section_info str_offsets;
583 VEC (dwarf2_section_info_def) *types;
584 };
585
586 /* Common bits of DWO CUs/TUs. */
587
588 struct dwo_unit
589 {
590 /* Backlink to the containing struct dwo_file. */
591 struct dwo_file *dwo_file;
592
593 /* The "id" that distinguishes this CU/TU.
594 .debug_info calls this "dwo_id", .debug_types calls this "signature".
595 Since signatures came first, we stick with it for consistency. */
596 ULONGEST signature;
597
598 /* The section this CU/TU lives in, in the DWO file. */
599 struct dwarf2_section_info *info_or_types_section;
600
601 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
602 sect_offset offset;
603 unsigned int length;
604
605 /* For types, offset in the type's DIE of the type defined by this TU. */
606 cu_offset type_offset_in_tu;
607 };
608
609 /* Data for one DWO file. */
610
611 struct dwo_file
612 {
613 /* The DW_AT_GNU_dwo_name attribute.
614 We don't manage space for this, it's an attribute. */
615 const char *dwo_name;
616
617 /* The bfd, when the file is open. Otherwise this is NULL. */
618 bfd *dwo_bfd;
619
620 /* Section info for this file. */
621 struct dwo_sections sections;
622
623 /* Table of CUs in the file.
624 Each element is a struct dwo_unit. */
625 htab_t cus;
626
627 /* Table of TUs in the file.
628 Each element is a struct dwo_unit. */
629 htab_t tus;
630 };
631
632 /* Struct used to pass misc. parameters to read_die_and_children, et
633 al. which are used for both .debug_info and .debug_types dies.
634 All parameters here are unchanging for the life of the call. This
635 struct exists to abstract away the constant parameters of die reading. */
636
637 struct die_reader_specs
638 {
639 /* die_section->asection->owner. */
640 bfd* abfd;
641
642 /* The CU of the DIE we are parsing. */
643 struct dwarf2_cu *cu;
644
645 /* Non-NULL if reading a DWO file. */
646 struct dwo_file *dwo_file;
647
648 /* The section the die comes from.
649 This is either .debug_info or .debug_types, or the .dwo variants. */
650 struct dwarf2_section_info *die_section;
651
652 /* die_section->buffer. */
653 gdb_byte *buffer;
654
655 /* The end of the buffer. */
656 const gdb_byte *buffer_end;
657 };
658
659 /* Type of function passed to init_cutu_and_read_dies, et.al. */
660 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
661 gdb_byte *info_ptr,
662 struct die_info *comp_unit_die,
663 int has_children,
664 void *data);
665
666 /* The line number information for a compilation unit (found in the
667 .debug_line section) begins with a "statement program header",
668 which contains the following information. */
669 struct line_header
670 {
671 unsigned int total_length;
672 unsigned short version;
673 unsigned int header_length;
674 unsigned char minimum_instruction_length;
675 unsigned char maximum_ops_per_instruction;
676 unsigned char default_is_stmt;
677 int line_base;
678 unsigned char line_range;
679 unsigned char opcode_base;
680
681 /* standard_opcode_lengths[i] is the number of operands for the
682 standard opcode whose value is i. This means that
683 standard_opcode_lengths[0] is unused, and the last meaningful
684 element is standard_opcode_lengths[opcode_base - 1]. */
685 unsigned char *standard_opcode_lengths;
686
687 /* The include_directories table. NOTE! These strings are not
688 allocated with xmalloc; instead, they are pointers into
689 debug_line_buffer. If you try to free them, `free' will get
690 indigestion. */
691 unsigned int num_include_dirs, include_dirs_size;
692 char **include_dirs;
693
694 /* The file_names table. NOTE! These strings are not allocated
695 with xmalloc; instead, they are pointers into debug_line_buffer.
696 Don't try to free them directly. */
697 unsigned int num_file_names, file_names_size;
698 struct file_entry
699 {
700 char *name;
701 unsigned int dir_index;
702 unsigned int mod_time;
703 unsigned int length;
704 int included_p; /* Non-zero if referenced by the Line Number Program. */
705 struct symtab *symtab; /* The associated symbol table, if any. */
706 } *file_names;
707
708 /* The start and end of the statement program following this
709 header. These point into dwarf2_per_objfile->line_buffer. */
710 gdb_byte *statement_program_start, *statement_program_end;
711 };
712
713 /* When we construct a partial symbol table entry we only
714 need this much information. */
715 struct partial_die_info
716 {
717 /* Offset of this DIE. */
718 sect_offset offset;
719
720 /* DWARF-2 tag for this DIE. */
721 ENUM_BITFIELD(dwarf_tag) tag : 16;
722
723 /* Assorted flags describing the data found in this DIE. */
724 unsigned int has_children : 1;
725 unsigned int is_external : 1;
726 unsigned int is_declaration : 1;
727 unsigned int has_type : 1;
728 unsigned int has_specification : 1;
729 unsigned int has_pc_info : 1;
730 unsigned int may_be_inlined : 1;
731
732 /* Flag set if the SCOPE field of this structure has been
733 computed. */
734 unsigned int scope_set : 1;
735
736 /* Flag set if the DIE has a byte_size attribute. */
737 unsigned int has_byte_size : 1;
738
739 /* Flag set if any of the DIE's children are template arguments. */
740 unsigned int has_template_arguments : 1;
741
742 /* Flag set if fixup_partial_die has been called on this die. */
743 unsigned int fixup_called : 1;
744
745 /* The name of this DIE. Normally the value of DW_AT_name, but
746 sometimes a default name for unnamed DIEs. */
747 char *name;
748
749 /* The linkage name, if present. */
750 const char *linkage_name;
751
752 /* The scope to prepend to our children. This is generally
753 allocated on the comp_unit_obstack, so will disappear
754 when this compilation unit leaves the cache. */
755 char *scope;
756
757 /* Some data associated with the partial DIE. The tag determines
758 which field is live. */
759 union
760 {
761 /* The location description associated with this DIE, if any. */
762 struct dwarf_block *locdesc;
763 /* The offset of an import, for DW_TAG_imported_unit. */
764 sect_offset offset;
765 } d;
766
767 /* If HAS_PC_INFO, the PC range associated with this DIE. */
768 CORE_ADDR lowpc;
769 CORE_ADDR highpc;
770
771 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
772 DW_AT_sibling, if any. */
773 /* NOTE: This member isn't strictly necessary, read_partial_die could
774 return DW_AT_sibling values to its caller load_partial_dies. */
775 gdb_byte *sibling;
776
777 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
778 DW_AT_specification (or DW_AT_abstract_origin or
779 DW_AT_extension). */
780 sect_offset spec_offset;
781
782 /* Pointers to this DIE's parent, first child, and next sibling,
783 if any. */
784 struct partial_die_info *die_parent, *die_child, *die_sibling;
785 };
786
787 /* This data structure holds the information of an abbrev. */
788 struct abbrev_info
789 {
790 unsigned int number; /* number identifying abbrev */
791 enum dwarf_tag tag; /* dwarf tag */
792 unsigned short has_children; /* boolean */
793 unsigned short num_attrs; /* number of attributes */
794 struct attr_abbrev *attrs; /* an array of attribute descriptions */
795 struct abbrev_info *next; /* next in chain */
796 };
797
798 struct attr_abbrev
799 {
800 ENUM_BITFIELD(dwarf_attribute) name : 16;
801 ENUM_BITFIELD(dwarf_form) form : 16;
802 };
803
804 /* Attributes have a name and a value. */
805 struct attribute
806 {
807 ENUM_BITFIELD(dwarf_attribute) name : 16;
808 ENUM_BITFIELD(dwarf_form) form : 15;
809
810 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
811 field should be in u.str (existing only for DW_STRING) but it is kept
812 here for better struct attribute alignment. */
813 unsigned int string_is_canonical : 1;
814
815 union
816 {
817 char *str;
818 struct dwarf_block *blk;
819 ULONGEST unsnd;
820 LONGEST snd;
821 CORE_ADDR addr;
822 struct signatured_type *signatured_type;
823 }
824 u;
825 };
826
827 /* This data structure holds a complete die structure. */
828 struct die_info
829 {
830 /* DWARF-2 tag for this DIE. */
831 ENUM_BITFIELD(dwarf_tag) tag : 16;
832
833 /* Number of attributes */
834 unsigned char num_attrs;
835
836 /* True if we're presently building the full type name for the
837 type derived from this DIE. */
838 unsigned char building_fullname : 1;
839
840 /* Abbrev number */
841 unsigned int abbrev;
842
843 /* Offset in .debug_info or .debug_types section. */
844 sect_offset offset;
845
846 /* The dies in a compilation unit form an n-ary tree. PARENT
847 points to this die's parent; CHILD points to the first child of
848 this node; and all the children of a given node are chained
849 together via their SIBLING fields. */
850 struct die_info *child; /* Its first child, if any. */
851 struct die_info *sibling; /* Its next sibling, if any. */
852 struct die_info *parent; /* Its parent, if any. */
853
854 /* An array of attributes, with NUM_ATTRS elements. There may be
855 zero, but it's not common and zero-sized arrays are not
856 sufficiently portable C. */
857 struct attribute attrs[1];
858 };
859
860 /* Get at parts of an attribute structure. */
861
862 #define DW_STRING(attr) ((attr)->u.str)
863 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
864 #define DW_UNSND(attr) ((attr)->u.unsnd)
865 #define DW_BLOCK(attr) ((attr)->u.blk)
866 #define DW_SND(attr) ((attr)->u.snd)
867 #define DW_ADDR(attr) ((attr)->u.addr)
868 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
869
870 /* Blocks are a bunch of untyped bytes. */
871 struct dwarf_block
872 {
873 unsigned int size;
874
875 /* Valid only if SIZE is not zero. */
876 gdb_byte *data;
877 };
878
879 #ifndef ATTR_ALLOC_CHUNK
880 #define ATTR_ALLOC_CHUNK 4
881 #endif
882
883 /* Allocate fields for structs, unions and enums in this size. */
884 #ifndef DW_FIELD_ALLOC_CHUNK
885 #define DW_FIELD_ALLOC_CHUNK 4
886 #endif
887
888 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
889 but this would require a corresponding change in unpack_field_as_long
890 and friends. */
891 static int bits_per_byte = 8;
892
893 /* The routines that read and process dies for a C struct or C++ class
894 pass lists of data member fields and lists of member function fields
895 in an instance of a field_info structure, as defined below. */
896 struct field_info
897 {
898 /* List of data member and baseclasses fields. */
899 struct nextfield
900 {
901 struct nextfield *next;
902 int accessibility;
903 int virtuality;
904 struct field field;
905 }
906 *fields, *baseclasses;
907
908 /* Number of fields (including baseclasses). */
909 int nfields;
910
911 /* Number of baseclasses. */
912 int nbaseclasses;
913
914 /* Set if the accesibility of one of the fields is not public. */
915 int non_public_fields;
916
917 /* Member function fields array, entries are allocated in the order they
918 are encountered in the object file. */
919 struct nextfnfield
920 {
921 struct nextfnfield *next;
922 struct fn_field fnfield;
923 }
924 *fnfields;
925
926 /* Member function fieldlist array, contains name of possibly overloaded
927 member function, number of overloaded member functions and a pointer
928 to the head of the member function field chain. */
929 struct fnfieldlist
930 {
931 char *name;
932 int length;
933 struct nextfnfield *head;
934 }
935 *fnfieldlists;
936
937 /* Number of entries in the fnfieldlists array. */
938 int nfnfields;
939
940 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
941 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
942 struct typedef_field_list
943 {
944 struct typedef_field field;
945 struct typedef_field_list *next;
946 }
947 *typedef_field_list;
948 unsigned typedef_field_list_count;
949 };
950
951 /* One item on the queue of compilation units to read in full symbols
952 for. */
953 struct dwarf2_queue_item
954 {
955 struct dwarf2_per_cu_data *per_cu;
956 enum language pretend_language;
957 struct dwarf2_queue_item *next;
958 };
959
960 /* The current queue. */
961 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
962
963 /* Loaded secondary compilation units are kept in memory until they
964 have not been referenced for the processing of this many
965 compilation units. Set this to zero to disable caching. Cache
966 sizes of up to at least twenty will improve startup time for
967 typical inter-CU-reference binaries, at an obvious memory cost. */
968 static int dwarf2_max_cache_age = 5;
969 static void
970 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
971 struct cmd_list_element *c, const char *value)
972 {
973 fprintf_filtered (file, _("The upper bound on the age of cached "
974 "dwarf2 compilation units is %s.\n"),
975 value);
976 }
977
978
979 /* Various complaints about symbol reading that don't abort the process. */
980
981 static void
982 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
983 {
984 complaint (&symfile_complaints,
985 _("statement list doesn't fit in .debug_line section"));
986 }
987
988 static void
989 dwarf2_debug_line_missing_file_complaint (void)
990 {
991 complaint (&symfile_complaints,
992 _(".debug_line section has line data without a file"));
993 }
994
995 static void
996 dwarf2_debug_line_missing_end_sequence_complaint (void)
997 {
998 complaint (&symfile_complaints,
999 _(".debug_line section has line "
1000 "program sequence without an end"));
1001 }
1002
1003 static void
1004 dwarf2_complex_location_expr_complaint (void)
1005 {
1006 complaint (&symfile_complaints, _("location expression too complex"));
1007 }
1008
1009 static void
1010 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1011 int arg3)
1012 {
1013 complaint (&symfile_complaints,
1014 _("const value length mismatch for '%s', got %d, expected %d"),
1015 arg1, arg2, arg3);
1016 }
1017
1018 static void
1019 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1020 {
1021 complaint (&symfile_complaints,
1022 _("debug info runs off end of %s section"
1023 " [in module %s]"),
1024 section->asection->name,
1025 bfd_get_filename (section->asection->owner));
1026 }
1027
1028 static void
1029 dwarf2_macro_malformed_definition_complaint (const char *arg1)
1030 {
1031 complaint (&symfile_complaints,
1032 _("macro debug info contains a "
1033 "malformed macro definition:\n`%s'"),
1034 arg1);
1035 }
1036
1037 static void
1038 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1039 {
1040 complaint (&symfile_complaints,
1041 _("invalid attribute class or form for '%s' in '%s'"),
1042 arg1, arg2);
1043 }
1044
1045 /* local function prototypes */
1046
1047 static void dwarf2_locate_sections (bfd *, asection *, void *);
1048
1049 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
1050 struct objfile *);
1051
1052 static void dwarf2_find_base_address (struct die_info *die,
1053 struct dwarf2_cu *cu);
1054
1055 static void dwarf2_build_psymtabs_hard (struct objfile *);
1056
1057 static void scan_partial_symbols (struct partial_die_info *,
1058 CORE_ADDR *, CORE_ADDR *,
1059 int, struct dwarf2_cu *);
1060
1061 static void add_partial_symbol (struct partial_die_info *,
1062 struct dwarf2_cu *);
1063
1064 static void add_partial_namespace (struct partial_die_info *pdi,
1065 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1066 int need_pc, struct dwarf2_cu *cu);
1067
1068 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1069 CORE_ADDR *highpc, int need_pc,
1070 struct dwarf2_cu *cu);
1071
1072 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1073 struct dwarf2_cu *cu);
1074
1075 static void add_partial_subprogram (struct partial_die_info *pdi,
1076 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1077 int need_pc, struct dwarf2_cu *cu);
1078
1079 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
1080
1081 static void psymtab_to_symtab_1 (struct partial_symtab *);
1082
1083 static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1084 struct dwarf2_section_info *);
1085
1086 static void dwarf2_free_abbrev_table (void *);
1087
1088 static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1089
1090 static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
1091 struct dwarf2_cu *);
1092
1093 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
1094 struct dwarf2_cu *);
1095
1096 static struct partial_die_info *load_partial_dies
1097 (const struct die_reader_specs *, gdb_byte *, int);
1098
1099 static gdb_byte *read_partial_die (const struct die_reader_specs *,
1100 struct partial_die_info *,
1101 struct abbrev_info *,
1102 unsigned int,
1103 gdb_byte *);
1104
1105 static struct partial_die_info *find_partial_die (sect_offset,
1106 struct dwarf2_cu *);
1107
1108 static void fixup_partial_die (struct partial_die_info *,
1109 struct dwarf2_cu *);
1110
1111 static gdb_byte *read_attribute (const struct die_reader_specs *,
1112 struct attribute *, struct attr_abbrev *,
1113 gdb_byte *);
1114
1115 static unsigned int read_1_byte (bfd *, gdb_byte *);
1116
1117 static int read_1_signed_byte (bfd *, gdb_byte *);
1118
1119 static unsigned int read_2_bytes (bfd *, gdb_byte *);
1120
1121 static unsigned int read_4_bytes (bfd *, gdb_byte *);
1122
1123 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
1124
1125 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
1126 unsigned int *);
1127
1128 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1129
1130 static LONGEST read_checked_initial_length_and_offset
1131 (bfd *, gdb_byte *, const struct comp_unit_head *,
1132 unsigned int *, unsigned int *);
1133
1134 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
1135 unsigned int *);
1136
1137 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
1138
1139 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
1140
1141 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
1142
1143 static char *read_indirect_string (bfd *, gdb_byte *,
1144 const struct comp_unit_head *,
1145 unsigned int *);
1146
1147 static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
1148
1149 static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
1150
1151 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1152 unsigned int *);
1153
1154 static char *read_str_index (const struct die_reader_specs *reader,
1155 struct dwarf2_cu *cu, ULONGEST str_index);
1156
1157 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1158
1159 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1160 struct dwarf2_cu *);
1161
1162 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1163 unsigned int,
1164 struct dwarf2_cu *);
1165
1166 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1167 struct dwarf2_cu *cu);
1168
1169 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1170
1171 static struct die_info *die_specification (struct die_info *die,
1172 struct dwarf2_cu **);
1173
1174 static void free_line_header (struct line_header *lh);
1175
1176 static void add_file_name (struct line_header *, char *, unsigned int,
1177 unsigned int, unsigned int);
1178
1179 static struct line_header *dwarf_decode_line_header (unsigned int offset,
1180 struct dwarf2_cu *cu);
1181
1182 static void dwarf_decode_lines (struct line_header *, const char *,
1183 struct dwarf2_cu *, struct partial_symtab *,
1184 int);
1185
1186 static void dwarf2_start_subfile (char *, const char *, const char *);
1187
1188 static struct symbol *new_symbol (struct die_info *, struct type *,
1189 struct dwarf2_cu *);
1190
1191 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1192 struct dwarf2_cu *, struct symbol *);
1193
1194 static void dwarf2_const_value (struct attribute *, struct symbol *,
1195 struct dwarf2_cu *);
1196
1197 static void dwarf2_const_value_attr (struct attribute *attr,
1198 struct type *type,
1199 const char *name,
1200 struct obstack *obstack,
1201 struct dwarf2_cu *cu, LONGEST *value,
1202 gdb_byte **bytes,
1203 struct dwarf2_locexpr_baton **baton);
1204
1205 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1206
1207 static int need_gnat_info (struct dwarf2_cu *);
1208
1209 static struct type *die_descriptive_type (struct die_info *,
1210 struct dwarf2_cu *);
1211
1212 static void set_descriptive_type (struct type *, struct die_info *,
1213 struct dwarf2_cu *);
1214
1215 static struct type *die_containing_type (struct die_info *,
1216 struct dwarf2_cu *);
1217
1218 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1219 struct dwarf2_cu *);
1220
1221 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1222
1223 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1224
1225 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1226
1227 static char *typename_concat (struct obstack *obs, const char *prefix,
1228 const char *suffix, int physname,
1229 struct dwarf2_cu *cu);
1230
1231 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1232
1233 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1234
1235 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1236
1237 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1238
1239 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1240
1241 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1242 struct dwarf2_cu *, struct partial_symtab *);
1243
1244 static int dwarf2_get_pc_bounds (struct die_info *,
1245 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1246 struct partial_symtab *);
1247
1248 static void get_scope_pc_bounds (struct die_info *,
1249 CORE_ADDR *, CORE_ADDR *,
1250 struct dwarf2_cu *);
1251
1252 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1253 CORE_ADDR, struct dwarf2_cu *);
1254
1255 static void dwarf2_add_field (struct field_info *, struct die_info *,
1256 struct dwarf2_cu *);
1257
1258 static void dwarf2_attach_fields_to_type (struct field_info *,
1259 struct type *, struct dwarf2_cu *);
1260
1261 static void dwarf2_add_member_fn (struct field_info *,
1262 struct die_info *, struct type *,
1263 struct dwarf2_cu *);
1264
1265 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1266 struct type *,
1267 struct dwarf2_cu *);
1268
1269 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1270
1271 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1272
1273 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1274
1275 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1276
1277 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1278
1279 static struct type *read_module_type (struct die_info *die,
1280 struct dwarf2_cu *cu);
1281
1282 static const char *namespace_name (struct die_info *die,
1283 int *is_anonymous, struct dwarf2_cu *);
1284
1285 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1286
1287 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1288
1289 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1290 struct dwarf2_cu *);
1291
1292 static struct die_info *read_die_and_children (const struct die_reader_specs *,
1293 gdb_byte *info_ptr,
1294 gdb_byte **new_info_ptr,
1295 struct die_info *parent);
1296
1297 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1298 gdb_byte *info_ptr,
1299 gdb_byte **new_info_ptr,
1300 struct die_info *parent);
1301
1302 static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1303 struct die_info **, gdb_byte *, int *, int);
1304
1305 static gdb_byte *read_full_die (const struct die_reader_specs *,
1306 struct die_info **, gdb_byte *, int *);
1307
1308 static void process_die (struct die_info *, struct dwarf2_cu *);
1309
1310 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1311 struct obstack *);
1312
1313 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1314
1315 static const char *dwarf2_full_name (char *name,
1316 struct die_info *die,
1317 struct dwarf2_cu *cu);
1318
1319 static struct die_info *dwarf2_extension (struct die_info *die,
1320 struct dwarf2_cu **);
1321
1322 static const char *dwarf_tag_name (unsigned int);
1323
1324 static const char *dwarf_attr_name (unsigned int);
1325
1326 static const char *dwarf_form_name (unsigned int);
1327
1328 static char *dwarf_bool_name (unsigned int);
1329
1330 static const char *dwarf_type_encoding_name (unsigned int);
1331
1332 static struct die_info *sibling_die (struct die_info *);
1333
1334 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1335
1336 static void dump_die_for_error (struct die_info *);
1337
1338 static void dump_die_1 (struct ui_file *, int level, int max_level,
1339 struct die_info *);
1340
1341 /*static*/ void dump_die (struct die_info *, int max_level);
1342
1343 static void store_in_ref_table (struct die_info *,
1344 struct dwarf2_cu *);
1345
1346 static int is_ref_attr (struct attribute *);
1347
1348 static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
1349
1350 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1351
1352 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1353 struct attribute *,
1354 struct dwarf2_cu **);
1355
1356 static struct die_info *follow_die_ref (struct die_info *,
1357 struct attribute *,
1358 struct dwarf2_cu **);
1359
1360 static struct die_info *follow_die_sig (struct die_info *,
1361 struct attribute *,
1362 struct dwarf2_cu **);
1363
1364 static struct signatured_type *lookup_signatured_type_at_offset
1365 (struct objfile *objfile,
1366 struct dwarf2_section_info *section, sect_offset offset);
1367
1368 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1369
1370 static void read_signatured_type (struct signatured_type *);
1371
1372 /* memory allocation interface */
1373
1374 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1375
1376 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1377
1378 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1379
1380 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
1381 char *, int);
1382
1383 static int attr_form_is_block (struct attribute *);
1384
1385 static int attr_form_is_section_offset (struct attribute *);
1386
1387 static int attr_form_is_constant (struct attribute *);
1388
1389 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1390 struct dwarf2_loclist_baton *baton,
1391 struct attribute *attr);
1392
1393 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1394 struct symbol *sym,
1395 struct dwarf2_cu *cu);
1396
1397 static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1398 gdb_byte *info_ptr,
1399 struct abbrev_info *abbrev);
1400
1401 static void free_stack_comp_unit (void *);
1402
1403 static hashval_t partial_die_hash (const void *item);
1404
1405 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1406
1407 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1408 (sect_offset offset, struct objfile *objfile);
1409
1410 static void init_one_comp_unit (struct dwarf2_cu *cu,
1411 struct dwarf2_per_cu_data *per_cu);
1412
1413 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1414 struct die_info *comp_unit_die,
1415 enum language pretend_language);
1416
1417 static void free_heap_comp_unit (void *);
1418
1419 static void free_cached_comp_units (void *);
1420
1421 static void age_cached_comp_units (void);
1422
1423 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1424
1425 static struct type *set_die_type (struct die_info *, struct type *,
1426 struct dwarf2_cu *);
1427
1428 static void create_all_comp_units (struct objfile *);
1429
1430 static int create_all_type_units (struct objfile *);
1431
1432 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1433 enum language);
1434
1435 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1436 enum language);
1437
1438 static void dwarf2_add_dependence (struct dwarf2_cu *,
1439 struct dwarf2_per_cu_data *);
1440
1441 static void dwarf2_mark (struct dwarf2_cu *);
1442
1443 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1444
1445 static struct type *get_die_type_at_offset (sect_offset,
1446 struct dwarf2_per_cu_data *per_cu);
1447
1448 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1449
1450 static void dwarf2_release_queue (void *dummy);
1451
1452 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1453 enum language pretend_language);
1454
1455 static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1456 struct dwarf2_per_cu_data *per_cu,
1457 enum language pretend_language);
1458
1459 static void process_queue (void);
1460
1461 static void find_file_and_directory (struct die_info *die,
1462 struct dwarf2_cu *cu,
1463 char **name, char **comp_dir);
1464
1465 static char *file_full_name (int file, struct line_header *lh,
1466 const char *comp_dir);
1467
1468 static void init_cutu_and_read_dies
1469 (struct dwarf2_per_cu_data *this_cu, int use_existing_cu, int keep,
1470 die_reader_func_ftype *die_reader_func, void *data);
1471
1472 static void init_cutu_and_read_dies_simple
1473 (struct dwarf2_per_cu_data *this_cu,
1474 die_reader_func_ftype *die_reader_func, void *data);
1475
1476 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1477
1478 static void process_psymtab_comp_unit (struct dwarf2_per_cu_data *, int);
1479
1480 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1481
1482 static struct dwo_unit *lookup_dwo_comp_unit
1483 (struct dwarf2_per_cu_data *, char *, const char *, ULONGEST);
1484
1485 static struct dwo_unit *lookup_dwo_type_unit
1486 (struct signatured_type *, char *, const char *);
1487
1488 static void free_dwo_file_cleanup (void *);
1489
1490 static void munmap_section_buffer (struct dwarf2_section_info *);
1491
1492 static void process_cu_includes (void);
1493
1494 #if WORDS_BIGENDIAN
1495
1496 /* Convert VALUE between big- and little-endian. */
1497 static offset_type
1498 byte_swap (offset_type value)
1499 {
1500 offset_type result;
1501
1502 result = (value & 0xff) << 24;
1503 result |= (value & 0xff00) << 8;
1504 result |= (value & 0xff0000) >> 8;
1505 result |= (value & 0xff000000) >> 24;
1506 return result;
1507 }
1508
1509 #define MAYBE_SWAP(V) byte_swap (V)
1510
1511 #else
1512 #define MAYBE_SWAP(V) (V)
1513 #endif /* WORDS_BIGENDIAN */
1514
1515 /* The suffix for an index file. */
1516 #define INDEX_SUFFIX ".gdb-index"
1517
1518 static const char *dwarf2_physname (char *name, struct die_info *die,
1519 struct dwarf2_cu *cu);
1520
1521 /* Try to locate the sections we need for DWARF 2 debugging
1522 information and return true if we have enough to do something.
1523 NAMES points to the dwarf2 section names, or is NULL if the standard
1524 ELF names are used. */
1525
1526 int
1527 dwarf2_has_info (struct objfile *objfile,
1528 const struct dwarf2_debug_sections *names)
1529 {
1530 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1531 if (!dwarf2_per_objfile)
1532 {
1533 /* Initialize per-objfile state. */
1534 struct dwarf2_per_objfile *data
1535 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1536
1537 memset (data, 0, sizeof (*data));
1538 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1539 dwarf2_per_objfile = data;
1540
1541 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1542 (void *) names);
1543 dwarf2_per_objfile->objfile = objfile;
1544 }
1545 return (dwarf2_per_objfile->info.asection != NULL
1546 && dwarf2_per_objfile->abbrev.asection != NULL);
1547 }
1548
1549 /* When loading sections, we look either for uncompressed section or for
1550 compressed section names. */
1551
1552 static int
1553 section_is_p (const char *section_name,
1554 const struct dwarf2_section_names *names)
1555 {
1556 if (names->normal != NULL
1557 && strcmp (section_name, names->normal) == 0)
1558 return 1;
1559 if (names->compressed != NULL
1560 && strcmp (section_name, names->compressed) == 0)
1561 return 1;
1562 return 0;
1563 }
1564
1565 /* This function is mapped across the sections and remembers the
1566 offset and size of each of the debugging sections we are interested
1567 in. */
1568
1569 static void
1570 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1571 {
1572 const struct dwarf2_debug_sections *names;
1573
1574 if (vnames == NULL)
1575 names = &dwarf2_elf_names;
1576 else
1577 names = (const struct dwarf2_debug_sections *) vnames;
1578
1579 if (section_is_p (sectp->name, &names->info))
1580 {
1581 dwarf2_per_objfile->info.asection = sectp;
1582 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1583 }
1584 else if (section_is_p (sectp->name, &names->abbrev))
1585 {
1586 dwarf2_per_objfile->abbrev.asection = sectp;
1587 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1588 }
1589 else if (section_is_p (sectp->name, &names->line))
1590 {
1591 dwarf2_per_objfile->line.asection = sectp;
1592 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1593 }
1594 else if (section_is_p (sectp->name, &names->loc))
1595 {
1596 dwarf2_per_objfile->loc.asection = sectp;
1597 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1598 }
1599 else if (section_is_p (sectp->name, &names->macinfo))
1600 {
1601 dwarf2_per_objfile->macinfo.asection = sectp;
1602 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1603 }
1604 else if (section_is_p (sectp->name, &names->macro))
1605 {
1606 dwarf2_per_objfile->macro.asection = sectp;
1607 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1608 }
1609 else if (section_is_p (sectp->name, &names->str))
1610 {
1611 dwarf2_per_objfile->str.asection = sectp;
1612 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1613 }
1614 else if (section_is_p (sectp->name, &names->addr))
1615 {
1616 dwarf2_per_objfile->addr.asection = sectp;
1617 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1618 }
1619 else if (section_is_p (sectp->name, &names->frame))
1620 {
1621 dwarf2_per_objfile->frame.asection = sectp;
1622 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1623 }
1624 else if (section_is_p (sectp->name, &names->eh_frame))
1625 {
1626 flagword aflag = bfd_get_section_flags (abfd, sectp);
1627
1628 if (aflag & SEC_HAS_CONTENTS)
1629 {
1630 dwarf2_per_objfile->eh_frame.asection = sectp;
1631 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1632 }
1633 }
1634 else if (section_is_p (sectp->name, &names->ranges))
1635 {
1636 dwarf2_per_objfile->ranges.asection = sectp;
1637 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1638 }
1639 else if (section_is_p (sectp->name, &names->types))
1640 {
1641 struct dwarf2_section_info type_section;
1642
1643 memset (&type_section, 0, sizeof (type_section));
1644 type_section.asection = sectp;
1645 type_section.size = bfd_get_section_size (sectp);
1646
1647 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1648 &type_section);
1649 }
1650 else if (section_is_p (sectp->name, &names->gdb_index))
1651 {
1652 dwarf2_per_objfile->gdb_index.asection = sectp;
1653 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1654 }
1655
1656 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1657 && bfd_section_vma (abfd, sectp) == 0)
1658 dwarf2_per_objfile->has_section_at_zero = 1;
1659 }
1660
1661 /* Decompress a section that was compressed using zlib. Store the
1662 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
1663
1664 static void
1665 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1666 gdb_byte **outbuf, bfd_size_type *outsize)
1667 {
1668 bfd *abfd = sectp->owner;
1669 #ifndef HAVE_ZLIB_H
1670 error (_("Support for zlib-compressed DWARF data (from '%s') "
1671 "is disabled in this copy of GDB"),
1672 bfd_get_filename (abfd));
1673 #else
1674 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1675 gdb_byte *compressed_buffer = xmalloc (compressed_size);
1676 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1677 bfd_size_type uncompressed_size;
1678 gdb_byte *uncompressed_buffer;
1679 z_stream strm;
1680 int rc;
1681 int header_size = 12;
1682
1683 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1684 || bfd_bread (compressed_buffer,
1685 compressed_size, abfd) != compressed_size)
1686 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1687 bfd_get_filename (abfd));
1688
1689 /* Read the zlib header. In this case, it should be "ZLIB" followed
1690 by the uncompressed section size, 8 bytes in big-endian order. */
1691 if (compressed_size < header_size
1692 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1693 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1694 bfd_get_filename (abfd));
1695 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1696 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1697 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1698 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1699 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1700 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1701 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1702 uncompressed_size += compressed_buffer[11];
1703
1704 /* It is possible the section consists of several compressed
1705 buffers concatenated together, so we uncompress in a loop. */
1706 strm.zalloc = NULL;
1707 strm.zfree = NULL;
1708 strm.opaque = NULL;
1709 strm.avail_in = compressed_size - header_size;
1710 strm.next_in = (Bytef*) compressed_buffer + header_size;
1711 strm.avail_out = uncompressed_size;
1712 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1713 uncompressed_size);
1714 rc = inflateInit (&strm);
1715 while (strm.avail_in > 0)
1716 {
1717 if (rc != Z_OK)
1718 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1719 bfd_get_filename (abfd), rc);
1720 strm.next_out = ((Bytef*) uncompressed_buffer
1721 + (uncompressed_size - strm.avail_out));
1722 rc = inflate (&strm, Z_FINISH);
1723 if (rc != Z_STREAM_END)
1724 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1725 bfd_get_filename (abfd), rc);
1726 rc = inflateReset (&strm);
1727 }
1728 rc = inflateEnd (&strm);
1729 if (rc != Z_OK
1730 || strm.avail_out != 0)
1731 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1732 bfd_get_filename (abfd), rc);
1733
1734 do_cleanups (cleanup);
1735 *outbuf = uncompressed_buffer;
1736 *outsize = uncompressed_size;
1737 #endif
1738 }
1739
1740 /* A helper function that decides whether a section is empty,
1741 or not present. */
1742
1743 static int
1744 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1745 {
1746 return info->asection == NULL || info->size == 0;
1747 }
1748
1749 /* Read the contents of the section INFO.
1750 OBJFILE is the main object file, but not necessarily the file where
1751 the section comes from. E.g., for DWO files INFO->asection->owner
1752 is the bfd of the DWO file.
1753 If the section is compressed, uncompress it before returning. */
1754
1755 static void
1756 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1757 {
1758 asection *sectp = info->asection;
1759 bfd *abfd;
1760 gdb_byte *buf, *retbuf;
1761 unsigned char header[4];
1762
1763 if (info->readin)
1764 return;
1765 info->buffer = NULL;
1766 info->map_addr = NULL;
1767 info->readin = 1;
1768
1769 if (dwarf2_section_empty_p (info))
1770 return;
1771
1772 /* Note that ABFD may not be from OBJFILE, e.g. a DWO section. */
1773 abfd = sectp->owner;
1774
1775 /* Check if the file has a 4-byte header indicating compression. */
1776 if (info->size > sizeof (header)
1777 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1778 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1779 {
1780 /* Upon decompression, update the buffer and its size. */
1781 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1782 {
1783 zlib_decompress_section (objfile, sectp, &info->buffer,
1784 &info->size);
1785 return;
1786 }
1787 }
1788
1789 #ifdef HAVE_MMAP
1790 if (pagesize == 0)
1791 pagesize = getpagesize ();
1792
1793 /* Only try to mmap sections which are large enough: we don't want to
1794 waste space due to fragmentation. Also, only try mmap for sections
1795 without relocations. */
1796
1797 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1798 {
1799 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1800 MAP_PRIVATE, sectp->filepos,
1801 &info->map_addr, &info->map_len);
1802
1803 if ((caddr_t)info->buffer != MAP_FAILED)
1804 {
1805 #if HAVE_POSIX_MADVISE
1806 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
1807 #endif
1808 return;
1809 }
1810 }
1811 #endif
1812
1813 /* If we get here, we are a normal, not-compressed section. */
1814 info->buffer = buf
1815 = obstack_alloc (&objfile->objfile_obstack, info->size);
1816
1817 /* When debugging .o files, we may need to apply relocations; see
1818 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1819 We never compress sections in .o files, so we only need to
1820 try this when the section is not compressed. */
1821 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1822 if (retbuf != NULL)
1823 {
1824 info->buffer = retbuf;
1825 return;
1826 }
1827
1828 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1829 || bfd_bread (buf, info->size, abfd) != info->size)
1830 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1831 bfd_get_filename (abfd));
1832 }
1833
1834 /* A helper function that returns the size of a section in a safe way.
1835 If you are positive that the section has been read before using the
1836 size, then it is safe to refer to the dwarf2_section_info object's
1837 "size" field directly. In other cases, you must call this
1838 function, because for compressed sections the size field is not set
1839 correctly until the section has been read. */
1840
1841 static bfd_size_type
1842 dwarf2_section_size (struct objfile *objfile,
1843 struct dwarf2_section_info *info)
1844 {
1845 if (!info->readin)
1846 dwarf2_read_section (objfile, info);
1847 return info->size;
1848 }
1849
1850 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1851 SECTION_NAME. */
1852
1853 void
1854 dwarf2_get_section_info (struct objfile *objfile,
1855 enum dwarf2_section_enum sect,
1856 asection **sectp, gdb_byte **bufp,
1857 bfd_size_type *sizep)
1858 {
1859 struct dwarf2_per_objfile *data
1860 = objfile_data (objfile, dwarf2_objfile_data_key);
1861 struct dwarf2_section_info *info;
1862
1863 /* We may see an objfile without any DWARF, in which case we just
1864 return nothing. */
1865 if (data == NULL)
1866 {
1867 *sectp = NULL;
1868 *bufp = NULL;
1869 *sizep = 0;
1870 return;
1871 }
1872 switch (sect)
1873 {
1874 case DWARF2_DEBUG_FRAME:
1875 info = &data->frame;
1876 break;
1877 case DWARF2_EH_FRAME:
1878 info = &data->eh_frame;
1879 break;
1880 default:
1881 gdb_assert_not_reached ("unexpected section");
1882 }
1883
1884 dwarf2_read_section (objfile, info);
1885
1886 *sectp = info->asection;
1887 *bufp = info->buffer;
1888 *sizep = info->size;
1889 }
1890
1891 \f
1892 /* DWARF quick_symbols_functions support. */
1893
1894 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1895 unique line tables, so we maintain a separate table of all .debug_line
1896 derived entries to support the sharing.
1897 All the quick functions need is the list of file names. We discard the
1898 line_header when we're done and don't need to record it here. */
1899 struct quick_file_names
1900 {
1901 /* The offset in .debug_line of the line table. We hash on this. */
1902 unsigned int offset;
1903
1904 /* The number of entries in file_names, real_names. */
1905 unsigned int num_file_names;
1906
1907 /* The file names from the line table, after being run through
1908 file_full_name. */
1909 const char **file_names;
1910
1911 /* The file names from the line table after being run through
1912 gdb_realpath. These are computed lazily. */
1913 const char **real_names;
1914 };
1915
1916 /* When using the index (and thus not using psymtabs), each CU has an
1917 object of this type. This is used to hold information needed by
1918 the various "quick" methods. */
1919 struct dwarf2_per_cu_quick_data
1920 {
1921 /* The file table. This can be NULL if there was no file table
1922 or it's currently not read in.
1923 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1924 struct quick_file_names *file_names;
1925
1926 /* The corresponding symbol table. This is NULL if symbols for this
1927 CU have not yet been read. */
1928 struct symtab *symtab;
1929
1930 /* A temporary mark bit used when iterating over all CUs in
1931 expand_symtabs_matching. */
1932 unsigned int mark : 1;
1933
1934 /* True if we've tried to read the file table and found there isn't one.
1935 There will be no point in trying to read it again next time. */
1936 unsigned int no_file_data : 1;
1937 };
1938
1939 /* Hash function for a quick_file_names. */
1940
1941 static hashval_t
1942 hash_file_name_entry (const void *e)
1943 {
1944 const struct quick_file_names *file_data = e;
1945
1946 return file_data->offset;
1947 }
1948
1949 /* Equality function for a quick_file_names. */
1950
1951 static int
1952 eq_file_name_entry (const void *a, const void *b)
1953 {
1954 const struct quick_file_names *ea = a;
1955 const struct quick_file_names *eb = b;
1956
1957 return ea->offset == eb->offset;
1958 }
1959
1960 /* Delete function for a quick_file_names. */
1961
1962 static void
1963 delete_file_name_entry (void *e)
1964 {
1965 struct quick_file_names *file_data = e;
1966 int i;
1967
1968 for (i = 0; i < file_data->num_file_names; ++i)
1969 {
1970 xfree ((void*) file_data->file_names[i]);
1971 if (file_data->real_names)
1972 xfree ((void*) file_data->real_names[i]);
1973 }
1974
1975 /* The space for the struct itself lives on objfile_obstack,
1976 so we don't free it here. */
1977 }
1978
1979 /* Create a quick_file_names hash table. */
1980
1981 static htab_t
1982 create_quick_file_names_table (unsigned int nr_initial_entries)
1983 {
1984 return htab_create_alloc (nr_initial_entries,
1985 hash_file_name_entry, eq_file_name_entry,
1986 delete_file_name_entry, xcalloc, xfree);
1987 }
1988
1989 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
1990 have to be created afterwards. You should call age_cached_comp_units after
1991 processing PER_CU->CU. dw2_setup must have been already called. */
1992
1993 static void
1994 load_cu (struct dwarf2_per_cu_data *per_cu)
1995 {
1996 if (per_cu->is_debug_types)
1997 load_full_type_unit (per_cu);
1998 else
1999 load_full_comp_unit (per_cu, language_minimal);
2000
2001 gdb_assert (per_cu->cu != NULL);
2002
2003 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2004 }
2005
2006 /* Read in the symbols for PER_CU. */
2007
2008 static void
2009 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2010 {
2011 struct cleanup *back_to;
2012
2013 back_to = make_cleanup (dwarf2_release_queue, NULL);
2014
2015 if (dwarf2_per_objfile->using_index
2016 ? per_cu->v.quick->symtab == NULL
2017 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2018 {
2019 queue_comp_unit (per_cu, language_minimal);
2020 load_cu (per_cu);
2021 }
2022
2023 process_queue ();
2024
2025 /* Age the cache, releasing compilation units that have not
2026 been used recently. */
2027 age_cached_comp_units ();
2028
2029 do_cleanups (back_to);
2030 }
2031
2032 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2033 the objfile from which this CU came. Returns the resulting symbol
2034 table. */
2035
2036 static struct symtab *
2037 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2038 {
2039 gdb_assert (dwarf2_per_objfile->using_index);
2040 if (!per_cu->v.quick->symtab)
2041 {
2042 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2043 increment_reading_symtab ();
2044 dw2_do_instantiate_symtab (per_cu);
2045 process_cu_includes ();
2046 do_cleanups (back_to);
2047 }
2048 return per_cu->v.quick->symtab;
2049 }
2050
2051 /* Return the CU given its index. */
2052
2053 static struct dwarf2_per_cu_data *
2054 dw2_get_cu (int index)
2055 {
2056 if (index >= dwarf2_per_objfile->n_comp_units)
2057 {
2058 index -= dwarf2_per_objfile->n_comp_units;
2059 return dwarf2_per_objfile->all_type_units[index];
2060 }
2061 return dwarf2_per_objfile->all_comp_units[index];
2062 }
2063
2064 /* A helper function that knows how to read a 64-bit value in a way
2065 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
2066 otherwise. */
2067
2068 static int
2069 extract_cu_value (const char *bytes, ULONGEST *result)
2070 {
2071 if (sizeof (ULONGEST) < 8)
2072 {
2073 int i;
2074
2075 /* Ignore the upper 4 bytes if they are all zero. */
2076 for (i = 0; i < 4; ++i)
2077 if (bytes[i + 4] != 0)
2078 return 0;
2079
2080 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
2081 }
2082 else
2083 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2084 return 1;
2085 }
2086
2087 /* Read the CU list from the mapped index, and use it to create all
2088 the CU objects for this objfile. Return 0 if something went wrong,
2089 1 if everything went ok. */
2090
2091 static int
2092 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
2093 offset_type cu_list_elements)
2094 {
2095 offset_type i;
2096
2097 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
2098 dwarf2_per_objfile->all_comp_units
2099 = obstack_alloc (&objfile->objfile_obstack,
2100 dwarf2_per_objfile->n_comp_units
2101 * sizeof (struct dwarf2_per_cu_data *));
2102
2103 for (i = 0; i < cu_list_elements; i += 2)
2104 {
2105 struct dwarf2_per_cu_data *the_cu;
2106 ULONGEST offset, length;
2107
2108 if (!extract_cu_value (cu_list, &offset)
2109 || !extract_cu_value (cu_list + 8, &length))
2110 return 0;
2111 cu_list += 2 * 8;
2112
2113 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2114 struct dwarf2_per_cu_data);
2115 the_cu->offset.sect_off = offset;
2116 the_cu->length = length;
2117 the_cu->objfile = objfile;
2118 the_cu->info_or_types_section = &dwarf2_per_objfile->info;
2119 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2120 struct dwarf2_per_cu_quick_data);
2121 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
2122 }
2123
2124 return 1;
2125 }
2126
2127 /* Create the signatured type hash table from the index. */
2128
2129 static int
2130 create_signatured_type_table_from_index (struct objfile *objfile,
2131 struct dwarf2_section_info *section,
2132 const gdb_byte *bytes,
2133 offset_type elements)
2134 {
2135 offset_type i;
2136 htab_t sig_types_hash;
2137
2138 dwarf2_per_objfile->n_type_units = elements / 3;
2139 dwarf2_per_objfile->all_type_units
2140 = obstack_alloc (&objfile->objfile_obstack,
2141 dwarf2_per_objfile->n_type_units
2142 * sizeof (struct dwarf2_per_cu_data *));
2143
2144 sig_types_hash = allocate_signatured_type_table (objfile);
2145
2146 for (i = 0; i < elements; i += 3)
2147 {
2148 struct signatured_type *sig_type;
2149 ULONGEST offset, type_offset_in_tu, signature;
2150 void **slot;
2151
2152 if (!extract_cu_value (bytes, &offset)
2153 || !extract_cu_value (bytes + 8, &type_offset_in_tu))
2154 return 0;
2155 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2156 bytes += 3 * 8;
2157
2158 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2159 struct signatured_type);
2160 sig_type->signature = signature;
2161 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2162 sig_type->per_cu.is_debug_types = 1;
2163 sig_type->per_cu.info_or_types_section = section;
2164 sig_type->per_cu.offset.sect_off = offset;
2165 sig_type->per_cu.objfile = objfile;
2166 sig_type->per_cu.v.quick
2167 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2168 struct dwarf2_per_cu_quick_data);
2169
2170 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2171 *slot = sig_type;
2172
2173 dwarf2_per_objfile->all_type_units[i / 3] = &sig_type->per_cu;
2174 }
2175
2176 dwarf2_per_objfile->signatured_types = sig_types_hash;
2177
2178 return 1;
2179 }
2180
2181 /* Read the address map data from the mapped index, and use it to
2182 populate the objfile's psymtabs_addrmap. */
2183
2184 static void
2185 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2186 {
2187 const gdb_byte *iter, *end;
2188 struct obstack temp_obstack;
2189 struct addrmap *mutable_map;
2190 struct cleanup *cleanup;
2191 CORE_ADDR baseaddr;
2192
2193 obstack_init (&temp_obstack);
2194 cleanup = make_cleanup_obstack_free (&temp_obstack);
2195 mutable_map = addrmap_create_mutable (&temp_obstack);
2196
2197 iter = index->address_table;
2198 end = iter + index->address_table_size;
2199
2200 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2201
2202 while (iter < end)
2203 {
2204 ULONGEST hi, lo, cu_index;
2205 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2206 iter += 8;
2207 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2208 iter += 8;
2209 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2210 iter += 4;
2211
2212 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2213 dw2_get_cu (cu_index));
2214 }
2215
2216 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2217 &objfile->objfile_obstack);
2218 do_cleanups (cleanup);
2219 }
2220
2221 /* The hash function for strings in the mapped index. This is the same as
2222 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2223 implementation. This is necessary because the hash function is tied to the
2224 format of the mapped index file. The hash values do not have to match with
2225 SYMBOL_HASH_NEXT.
2226
2227 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2228
2229 static hashval_t
2230 mapped_index_string_hash (int index_version, const void *p)
2231 {
2232 const unsigned char *str = (const unsigned char *) p;
2233 hashval_t r = 0;
2234 unsigned char c;
2235
2236 while ((c = *str++) != 0)
2237 {
2238 if (index_version >= 5)
2239 c = tolower (c);
2240 r = r * 67 + c - 113;
2241 }
2242
2243 return r;
2244 }
2245
2246 /* Find a slot in the mapped index INDEX for the object named NAME.
2247 If NAME is found, set *VEC_OUT to point to the CU vector in the
2248 constant pool and return 1. If NAME cannot be found, return 0. */
2249
2250 static int
2251 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2252 offset_type **vec_out)
2253 {
2254 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2255 offset_type hash;
2256 offset_type slot, step;
2257 int (*cmp) (const char *, const char *);
2258
2259 if (current_language->la_language == language_cplus
2260 || current_language->la_language == language_java
2261 || current_language->la_language == language_fortran)
2262 {
2263 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2264 not contain any. */
2265 const char *paren = strchr (name, '(');
2266
2267 if (paren)
2268 {
2269 char *dup;
2270
2271 dup = xmalloc (paren - name + 1);
2272 memcpy (dup, name, paren - name);
2273 dup[paren - name] = 0;
2274
2275 make_cleanup (xfree, dup);
2276 name = dup;
2277 }
2278 }
2279
2280 /* Index version 4 did not support case insensitive searches. But the
2281 indices for case insensitive languages are built in lowercase, therefore
2282 simulate our NAME being searched is also lowercased. */
2283 hash = mapped_index_string_hash ((index->version == 4
2284 && case_sensitivity == case_sensitive_off
2285 ? 5 : index->version),
2286 name);
2287
2288 slot = hash & (index->symbol_table_slots - 1);
2289 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2290 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2291
2292 for (;;)
2293 {
2294 /* Convert a slot number to an offset into the table. */
2295 offset_type i = 2 * slot;
2296 const char *str;
2297 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2298 {
2299 do_cleanups (back_to);
2300 return 0;
2301 }
2302
2303 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2304 if (!cmp (name, str))
2305 {
2306 *vec_out = (offset_type *) (index->constant_pool
2307 + MAYBE_SWAP (index->symbol_table[i + 1]));
2308 do_cleanups (back_to);
2309 return 1;
2310 }
2311
2312 slot = (slot + step) & (index->symbol_table_slots - 1);
2313 }
2314 }
2315
2316 /* Read the index file. If everything went ok, initialize the "quick"
2317 elements of all the CUs and return 1. Otherwise, return 0. */
2318
2319 static int
2320 dwarf2_read_index (struct objfile *objfile)
2321 {
2322 char *addr;
2323 struct mapped_index *map;
2324 offset_type *metadata;
2325 const gdb_byte *cu_list;
2326 const gdb_byte *types_list = NULL;
2327 offset_type version, cu_list_elements;
2328 offset_type types_list_elements = 0;
2329 int i;
2330
2331 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
2332 return 0;
2333
2334 /* Older elfutils strip versions could keep the section in the main
2335 executable while splitting it for the separate debug info file. */
2336 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2337 & SEC_HAS_CONTENTS) == 0)
2338 return 0;
2339
2340 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2341
2342 addr = dwarf2_per_objfile->gdb_index.buffer;
2343 /* Version check. */
2344 version = MAYBE_SWAP (*(offset_type *) addr);
2345 /* Versions earlier than 3 emitted every copy of a psymbol. This
2346 causes the index to behave very poorly for certain requests. Version 3
2347 contained incomplete addrmap. So, it seems better to just ignore such
2348 indices. */
2349 if (version < 4)
2350 {
2351 static int warning_printed = 0;
2352 if (!warning_printed)
2353 {
2354 warning (_("Skipping obsolete .gdb_index section in %s."),
2355 objfile->name);
2356 warning_printed = 1;
2357 }
2358 return 0;
2359 }
2360 /* Index version 4 uses a different hash function than index version
2361 5 and later.
2362
2363 Versions earlier than 6 did not emit psymbols for inlined
2364 functions. Using these files will cause GDB not to be able to
2365 set breakpoints on inlined functions by name, so we ignore these
2366 indices unless the --use-deprecated-index-sections command line
2367 option was supplied. */
2368 if (version < 6 && !use_deprecated_index_sections)
2369 {
2370 static int warning_printed = 0;
2371 if (!warning_printed)
2372 {
2373 warning (_("Skipping deprecated .gdb_index section in %s, pass "
2374 "--use-deprecated-index-sections to use them anyway"),
2375 objfile->name);
2376 warning_printed = 1;
2377 }
2378 return 0;
2379 }
2380 /* Indexes with higher version than the one supported by GDB may be no
2381 longer backward compatible. */
2382 if (version > 7)
2383 return 0;
2384
2385 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
2386 map->version = version;
2387 map->total_size = dwarf2_per_objfile->gdb_index.size;
2388
2389 metadata = (offset_type *) (addr + sizeof (offset_type));
2390
2391 i = 0;
2392 cu_list = addr + MAYBE_SWAP (metadata[i]);
2393 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2394 / 8);
2395 ++i;
2396
2397 types_list = addr + MAYBE_SWAP (metadata[i]);
2398 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2399 - MAYBE_SWAP (metadata[i]))
2400 / 8);
2401 ++i;
2402
2403 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2404 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2405 - MAYBE_SWAP (metadata[i]));
2406 ++i;
2407
2408 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2409 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2410 - MAYBE_SWAP (metadata[i]))
2411 / (2 * sizeof (offset_type)));
2412 ++i;
2413
2414 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2415
2416 /* Don't use the index if it's empty. */
2417 if (map->symbol_table_slots == 0)
2418 return 0;
2419
2420 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2421 return 0;
2422
2423 if (types_list_elements)
2424 {
2425 struct dwarf2_section_info *section;
2426
2427 /* We can only handle a single .debug_types when we have an
2428 index. */
2429 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2430 return 0;
2431
2432 section = VEC_index (dwarf2_section_info_def,
2433 dwarf2_per_objfile->types, 0);
2434
2435 if (!create_signatured_type_table_from_index (objfile, section,
2436 types_list,
2437 types_list_elements))
2438 return 0;
2439 }
2440
2441 create_addrmap_from_index (objfile, map);
2442
2443 dwarf2_per_objfile->index_table = map;
2444 dwarf2_per_objfile->using_index = 1;
2445 dwarf2_per_objfile->quick_file_names_table =
2446 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2447
2448 return 1;
2449 }
2450
2451 /* A helper for the "quick" functions which sets the global
2452 dwarf2_per_objfile according to OBJFILE. */
2453
2454 static void
2455 dw2_setup (struct objfile *objfile)
2456 {
2457 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2458 gdb_assert (dwarf2_per_objfile);
2459 }
2460
2461 /* die_reader_func for dw2_get_file_names. */
2462
2463 static void
2464 dw2_get_file_names_reader (const struct die_reader_specs *reader,
2465 gdb_byte *info_ptr,
2466 struct die_info *comp_unit_die,
2467 int has_children,
2468 void *data)
2469 {
2470 struct dwarf2_cu *cu = reader->cu;
2471 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2472 struct objfile *objfile = dwarf2_per_objfile->objfile;
2473 struct line_header *lh;
2474 struct attribute *attr;
2475 int i;
2476 char *name, *comp_dir;
2477 void **slot;
2478 struct quick_file_names *qfn;
2479 unsigned int line_offset;
2480
2481 /* Our callers never want to match partial units -- instead they
2482 will match the enclosing full CU. */
2483 if (comp_unit_die->tag == DW_TAG_partial_unit)
2484 {
2485 this_cu->v.quick->no_file_data = 1;
2486 return;
2487 }
2488
2489 lh = NULL;
2490 slot = NULL;
2491 line_offset = 0;
2492
2493 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2494 if (attr)
2495 {
2496 struct quick_file_names find_entry;
2497
2498 line_offset = DW_UNSND (attr);
2499
2500 /* We may have already read in this line header (TU line header sharing).
2501 If we have we're done. */
2502 find_entry.offset = line_offset;
2503 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2504 &find_entry, INSERT);
2505 if (*slot != NULL)
2506 {
2507 this_cu->v.quick->file_names = *slot;
2508 return;
2509 }
2510
2511 lh = dwarf_decode_line_header (line_offset, cu);
2512 }
2513 if (lh == NULL)
2514 {
2515 this_cu->v.quick->no_file_data = 1;
2516 return;
2517 }
2518
2519 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2520 qfn->offset = line_offset;
2521 gdb_assert (slot != NULL);
2522 *slot = qfn;
2523
2524 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
2525
2526 qfn->num_file_names = lh->num_file_names;
2527 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2528 lh->num_file_names * sizeof (char *));
2529 for (i = 0; i < lh->num_file_names; ++i)
2530 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2531 qfn->real_names = NULL;
2532
2533 free_line_header (lh);
2534
2535 this_cu->v.quick->file_names = qfn;
2536 }
2537
2538 /* A helper for the "quick" functions which attempts to read the line
2539 table for THIS_CU. */
2540
2541 static struct quick_file_names *
2542 dw2_get_file_names (struct objfile *objfile,
2543 struct dwarf2_per_cu_data *this_cu)
2544 {
2545 if (this_cu->v.quick->file_names != NULL)
2546 return this_cu->v.quick->file_names;
2547 /* If we know there is no line data, no point in looking again. */
2548 if (this_cu->v.quick->no_file_data)
2549 return NULL;
2550
2551 /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2552 in the stub for CUs, there's is no need to lookup the DWO file.
2553 However, that's not the case for TUs where DW_AT_stmt_list lives in the
2554 DWO file. */
2555 if (this_cu->is_debug_types)
2556 init_cutu_and_read_dies (this_cu, 0, 0, dw2_get_file_names_reader, NULL);
2557 else
2558 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
2559
2560 if (this_cu->v.quick->no_file_data)
2561 return NULL;
2562 return this_cu->v.quick->file_names;
2563 }
2564
2565 /* A helper for the "quick" functions which computes and caches the
2566 real path for a given file name from the line table. */
2567
2568 static const char *
2569 dw2_get_real_path (struct objfile *objfile,
2570 struct quick_file_names *qfn, int index)
2571 {
2572 if (qfn->real_names == NULL)
2573 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2574 qfn->num_file_names, sizeof (char *));
2575
2576 if (qfn->real_names[index] == NULL)
2577 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2578
2579 return qfn->real_names[index];
2580 }
2581
2582 static struct symtab *
2583 dw2_find_last_source_symtab (struct objfile *objfile)
2584 {
2585 int index;
2586
2587 dw2_setup (objfile);
2588 index = dwarf2_per_objfile->n_comp_units - 1;
2589 return dw2_instantiate_symtab (dw2_get_cu (index));
2590 }
2591
2592 /* Traversal function for dw2_forget_cached_source_info. */
2593
2594 static int
2595 dw2_free_cached_file_names (void **slot, void *info)
2596 {
2597 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2598
2599 if (file_data->real_names)
2600 {
2601 int i;
2602
2603 for (i = 0; i < file_data->num_file_names; ++i)
2604 {
2605 xfree ((void*) file_data->real_names[i]);
2606 file_data->real_names[i] = NULL;
2607 }
2608 }
2609
2610 return 1;
2611 }
2612
2613 static void
2614 dw2_forget_cached_source_info (struct objfile *objfile)
2615 {
2616 dw2_setup (objfile);
2617
2618 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2619 dw2_free_cached_file_names, NULL);
2620 }
2621
2622 /* Helper function for dw2_map_symtabs_matching_filename that expands
2623 the symtabs and calls the iterator. */
2624
2625 static int
2626 dw2_map_expand_apply (struct objfile *objfile,
2627 struct dwarf2_per_cu_data *per_cu,
2628 const char *name,
2629 const char *full_path, const char *real_path,
2630 int (*callback) (struct symtab *, void *),
2631 void *data)
2632 {
2633 struct symtab *last_made = objfile->symtabs;
2634
2635 /* Don't visit already-expanded CUs. */
2636 if (per_cu->v.quick->symtab)
2637 return 0;
2638
2639 /* This may expand more than one symtab, and we want to iterate over
2640 all of them. */
2641 dw2_instantiate_symtab (per_cu);
2642
2643 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2644 objfile->symtabs, last_made);
2645 }
2646
2647 /* Implementation of the map_symtabs_matching_filename method. */
2648
2649 static int
2650 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
2651 const char *full_path, const char *real_path,
2652 int (*callback) (struct symtab *, void *),
2653 void *data)
2654 {
2655 int i;
2656 const char *name_basename = lbasename (name);
2657 int name_len = strlen (name);
2658 int is_abs = IS_ABSOLUTE_PATH (name);
2659
2660 dw2_setup (objfile);
2661
2662 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2663 + dwarf2_per_objfile->n_type_units); ++i)
2664 {
2665 int j;
2666 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2667 struct quick_file_names *file_data;
2668
2669 /* We only need to look at symtabs not already expanded. */
2670 if (per_cu->v.quick->symtab)
2671 continue;
2672
2673 file_data = dw2_get_file_names (objfile, per_cu);
2674 if (file_data == NULL)
2675 continue;
2676
2677 for (j = 0; j < file_data->num_file_names; ++j)
2678 {
2679 const char *this_name = file_data->file_names[j];
2680
2681 if (FILENAME_CMP (name, this_name) == 0
2682 || (!is_abs && compare_filenames_for_search (this_name,
2683 name, name_len)))
2684 {
2685 if (dw2_map_expand_apply (objfile, per_cu,
2686 name, full_path, real_path,
2687 callback, data))
2688 return 1;
2689 }
2690
2691 /* Before we invoke realpath, which can get expensive when many
2692 files are involved, do a quick comparison of the basenames. */
2693 if (! basenames_may_differ
2694 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
2695 continue;
2696
2697 if (full_path != NULL)
2698 {
2699 const char *this_real_name = dw2_get_real_path (objfile,
2700 file_data, j);
2701
2702 if (this_real_name != NULL
2703 && (FILENAME_CMP (full_path, this_real_name) == 0
2704 || (!is_abs
2705 && compare_filenames_for_search (this_real_name,
2706 name, name_len))))
2707 {
2708 if (dw2_map_expand_apply (objfile, per_cu,
2709 name, full_path, real_path,
2710 callback, data))
2711 return 1;
2712 }
2713 }
2714
2715 if (real_path != NULL)
2716 {
2717 const char *this_real_name = dw2_get_real_path (objfile,
2718 file_data, j);
2719
2720 if (this_real_name != NULL
2721 && (FILENAME_CMP (real_path, this_real_name) == 0
2722 || (!is_abs
2723 && compare_filenames_for_search (this_real_name,
2724 name, name_len))))
2725 {
2726 if (dw2_map_expand_apply (objfile, per_cu,
2727 name, full_path, real_path,
2728 callback, data))
2729 return 1;
2730 }
2731 }
2732 }
2733 }
2734
2735 return 0;
2736 }
2737
2738 static struct symtab *
2739 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2740 const char *name, domain_enum domain)
2741 {
2742 /* We do all the work in the pre_expand_symtabs_matching hook
2743 instead. */
2744 return NULL;
2745 }
2746
2747 /* A helper function that expands all symtabs that hold an object
2748 named NAME. If WANT_SPECIFIC_BLOCK is non-zero, only look for
2749 symbols in block BLOCK_KIND. */
2750
2751 static void
2752 dw2_do_expand_symtabs_matching (struct objfile *objfile,
2753 int want_specific_block,
2754 enum block_enum block_kind,
2755 const char *name, domain_enum domain)
2756 {
2757 struct mapped_index *index;
2758
2759 dw2_setup (objfile);
2760
2761 index = dwarf2_per_objfile->index_table;
2762
2763 /* index_table is NULL if OBJF_READNOW. */
2764 if (index)
2765 {
2766 offset_type *vec;
2767
2768 if (find_slot_in_mapped_hash (index, name, &vec))
2769 {
2770 offset_type i, len = MAYBE_SWAP (*vec);
2771 for (i = 0; i < len; ++i)
2772 {
2773 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[i + 1]);
2774 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
2775 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2776 int want_static = block_kind != GLOBAL_BLOCK;
2777 /* This value is only valid for index versions >= 7. */
2778 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
2779 gdb_index_symbol_kind symbol_kind =
2780 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
2781
2782 if (want_specific_block
2783 && index->version >= 7
2784 && want_static != is_static)
2785 continue;
2786
2787 /* Only check the symbol's kind if it has one.
2788 Indices prior to version 7 don't record it. */
2789 if (index->version >= 7)
2790 {
2791 switch (domain)
2792 {
2793 case VAR_DOMAIN:
2794 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
2795 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
2796 /* Some types are also in VAR_DOMAIN. */
2797 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
2798 continue;
2799 break;
2800 case STRUCT_DOMAIN:
2801 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
2802 continue;
2803 break;
2804 case LABEL_DOMAIN:
2805 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
2806 continue;
2807 break;
2808 default:
2809 break;
2810 }
2811 }
2812
2813 dw2_instantiate_symtab (per_cu);
2814 }
2815 }
2816 }
2817 }
2818
2819 static void
2820 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2821 enum block_enum block_kind, const char *name,
2822 domain_enum domain)
2823 {
2824 dw2_do_expand_symtabs_matching (objfile, 1, block_kind, name, domain);
2825 }
2826
2827 static void
2828 dw2_print_stats (struct objfile *objfile)
2829 {
2830 int i, count;
2831
2832 dw2_setup (objfile);
2833 count = 0;
2834 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2835 + dwarf2_per_objfile->n_type_units); ++i)
2836 {
2837 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2838
2839 if (!per_cu->v.quick->symtab)
2840 ++count;
2841 }
2842 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2843 }
2844
2845 static void
2846 dw2_dump (struct objfile *objfile)
2847 {
2848 /* Nothing worth printing. */
2849 }
2850
2851 static void
2852 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2853 struct section_offsets *delta)
2854 {
2855 /* There's nothing to relocate here. */
2856 }
2857
2858 static void
2859 dw2_expand_symtabs_for_function (struct objfile *objfile,
2860 const char *func_name)
2861 {
2862 /* Note: It doesn't matter what we pass for block_kind here. */
2863 dw2_do_expand_symtabs_matching (objfile, 0, GLOBAL_BLOCK, func_name,
2864 VAR_DOMAIN);
2865 }
2866
2867 static void
2868 dw2_expand_all_symtabs (struct objfile *objfile)
2869 {
2870 int i;
2871
2872 dw2_setup (objfile);
2873
2874 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2875 + dwarf2_per_objfile->n_type_units); ++i)
2876 {
2877 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2878
2879 dw2_instantiate_symtab (per_cu);
2880 }
2881 }
2882
2883 static void
2884 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2885 const char *filename)
2886 {
2887 int i;
2888
2889 dw2_setup (objfile);
2890
2891 /* We don't need to consider type units here.
2892 This is only called for examining code, e.g. expand_line_sal.
2893 There can be an order of magnitude (or more) more type units
2894 than comp units, and we avoid them if we can. */
2895
2896 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2897 {
2898 int j;
2899 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2900 struct quick_file_names *file_data;
2901
2902 /* We only need to look at symtabs not already expanded. */
2903 if (per_cu->v.quick->symtab)
2904 continue;
2905
2906 file_data = dw2_get_file_names (objfile, per_cu);
2907 if (file_data == NULL)
2908 continue;
2909
2910 for (j = 0; j < file_data->num_file_names; ++j)
2911 {
2912 const char *this_name = file_data->file_names[j];
2913 if (FILENAME_CMP (this_name, filename) == 0)
2914 {
2915 dw2_instantiate_symtab (per_cu);
2916 break;
2917 }
2918 }
2919 }
2920 }
2921
2922 /* A helper function for dw2_find_symbol_file that finds the primary
2923 file name for a given CU. This is a die_reader_func. */
2924
2925 static void
2926 dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
2927 gdb_byte *info_ptr,
2928 struct die_info *comp_unit_die,
2929 int has_children,
2930 void *data)
2931 {
2932 const char **result_ptr = data;
2933 struct dwarf2_cu *cu = reader->cu;
2934 struct attribute *attr;
2935
2936 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
2937 if (attr == NULL)
2938 *result_ptr = NULL;
2939 else
2940 *result_ptr = DW_STRING (attr);
2941 }
2942
2943 static const char *
2944 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2945 {
2946 struct dwarf2_per_cu_data *per_cu;
2947 offset_type *vec;
2948 struct quick_file_names *file_data;
2949 const char *filename;
2950
2951 dw2_setup (objfile);
2952
2953 /* index_table is NULL if OBJF_READNOW. */
2954 if (!dwarf2_per_objfile->index_table)
2955 {
2956 struct symtab *s;
2957
2958 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
2959 {
2960 struct blockvector *bv = BLOCKVECTOR (s);
2961 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2962 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2963
2964 if (sym)
2965 return sym->symtab->filename;
2966 }
2967 return NULL;
2968 }
2969
2970 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2971 name, &vec))
2972 return NULL;
2973
2974 /* Note that this just looks at the very first one named NAME -- but
2975 actually we are looking for a function. find_main_filename
2976 should be rewritten so that it doesn't require a custom hook. It
2977 could just use the ordinary symbol tables. */
2978 /* vec[0] is the length, which must always be >0. */
2979 per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
2980
2981 if (per_cu->v.quick->symtab != NULL)
2982 return per_cu->v.quick->symtab->filename;
2983
2984 init_cutu_and_read_dies (per_cu, 0, 0, dw2_get_primary_filename_reader,
2985 &filename);
2986
2987 return filename;
2988 }
2989
2990 static void
2991 dw2_map_matching_symbols (const char * name, domain_enum namespace,
2992 struct objfile *objfile, int global,
2993 int (*callback) (struct block *,
2994 struct symbol *, void *),
2995 void *data, symbol_compare_ftype *match,
2996 symbol_compare_ftype *ordered_compare)
2997 {
2998 /* Currently unimplemented; used for Ada. The function can be called if the
2999 current language is Ada for a non-Ada objfile using GNU index. As Ada
3000 does not look for non-Ada symbols this function should just return. */
3001 }
3002
3003 static void
3004 dw2_expand_symtabs_matching
3005 (struct objfile *objfile,
3006 int (*file_matcher) (const char *, void *),
3007 int (*name_matcher) (const char *, void *),
3008 enum search_domain kind,
3009 void *data)
3010 {
3011 int i;
3012 offset_type iter;
3013 struct mapped_index *index;
3014
3015 dw2_setup (objfile);
3016
3017 /* index_table is NULL if OBJF_READNOW. */
3018 if (!dwarf2_per_objfile->index_table)
3019 return;
3020 index = dwarf2_per_objfile->index_table;
3021
3022 if (file_matcher != NULL)
3023 {
3024 struct cleanup *cleanup;
3025 htab_t visited_found, visited_not_found;
3026
3027 visited_found = htab_create_alloc (10,
3028 htab_hash_pointer, htab_eq_pointer,
3029 NULL, xcalloc, xfree);
3030 cleanup = make_cleanup_htab_delete (visited_found);
3031 visited_not_found = htab_create_alloc (10,
3032 htab_hash_pointer, htab_eq_pointer,
3033 NULL, xcalloc, xfree);
3034 make_cleanup_htab_delete (visited_not_found);
3035
3036 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3037 + dwarf2_per_objfile->n_type_units); ++i)
3038 {
3039 int j;
3040 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3041 struct quick_file_names *file_data;
3042 void **slot;
3043
3044 per_cu->v.quick->mark = 0;
3045
3046 /* We only need to look at symtabs not already expanded. */
3047 if (per_cu->v.quick->symtab)
3048 continue;
3049
3050 file_data = dw2_get_file_names (objfile, per_cu);
3051 if (file_data == NULL)
3052 continue;
3053
3054 if (htab_find (visited_not_found, file_data) != NULL)
3055 continue;
3056 else if (htab_find (visited_found, file_data) != NULL)
3057 {
3058 per_cu->v.quick->mark = 1;
3059 continue;
3060 }
3061
3062 for (j = 0; j < file_data->num_file_names; ++j)
3063 {
3064 if (file_matcher (file_data->file_names[j], data))
3065 {
3066 per_cu->v.quick->mark = 1;
3067 break;
3068 }
3069 }
3070
3071 slot = htab_find_slot (per_cu->v.quick->mark
3072 ? visited_found
3073 : visited_not_found,
3074 file_data, INSERT);
3075 *slot = file_data;
3076 }
3077
3078 do_cleanups (cleanup);
3079 }
3080
3081 for (iter = 0; iter < index->symbol_table_slots; ++iter)
3082 {
3083 offset_type idx = 2 * iter;
3084 const char *name;
3085 offset_type *vec, vec_len, vec_idx;
3086
3087 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
3088 continue;
3089
3090 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
3091
3092 if (! (*name_matcher) (name, data))
3093 continue;
3094
3095 /* The name was matched, now expand corresponding CUs that were
3096 marked. */
3097 vec = (offset_type *) (index->constant_pool
3098 + MAYBE_SWAP (index->symbol_table[idx + 1]));
3099 vec_len = MAYBE_SWAP (vec[0]);
3100 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3101 {
3102 struct dwarf2_per_cu_data *per_cu;
3103 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3104 gdb_index_symbol_kind symbol_kind =
3105 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3106 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3107
3108 /* Don't crash on bad data. */
3109 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3110 + dwarf2_per_objfile->n_comp_units))
3111 continue;
3112
3113 /* Only check the symbol's kind if it has one.
3114 Indices prior to version 7 don't record it. */
3115 if (index->version >= 7)
3116 {
3117 switch (kind)
3118 {
3119 case VARIABLES_DOMAIN:
3120 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3121 continue;
3122 break;
3123 case FUNCTIONS_DOMAIN:
3124 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3125 continue;
3126 break;
3127 case TYPES_DOMAIN:
3128 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3129 continue;
3130 break;
3131 default:
3132 break;
3133 }
3134 }
3135
3136 per_cu = dw2_get_cu (cu_index);
3137 if (file_matcher == NULL || per_cu->v.quick->mark)
3138 dw2_instantiate_symtab (per_cu);
3139 }
3140 }
3141 }
3142
3143 /* A helper for dw2_find_pc_sect_symtab which finds the most specific
3144 symtab. */
3145
3146 static struct symtab *
3147 recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3148 {
3149 int i;
3150
3151 if (BLOCKVECTOR (symtab) != NULL
3152 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3153 return symtab;
3154
3155 if (symtab->includes == NULL)
3156 return NULL;
3157
3158 for (i = 0; symtab->includes[i]; ++i)
3159 {
3160 struct symtab *s = symtab->includes[i];
3161
3162 s = recursively_find_pc_sect_symtab (s, pc);
3163 if (s != NULL)
3164 return s;
3165 }
3166
3167 return NULL;
3168 }
3169
3170 static struct symtab *
3171 dw2_find_pc_sect_symtab (struct objfile *objfile,
3172 struct minimal_symbol *msymbol,
3173 CORE_ADDR pc,
3174 struct obj_section *section,
3175 int warn_if_readin)
3176 {
3177 struct dwarf2_per_cu_data *data;
3178 struct symtab *result;
3179
3180 dw2_setup (objfile);
3181
3182 if (!objfile->psymtabs_addrmap)
3183 return NULL;
3184
3185 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3186 if (!data)
3187 return NULL;
3188
3189 if (warn_if_readin && data->v.quick->symtab)
3190 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
3191 paddress (get_objfile_arch (objfile), pc));
3192
3193 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3194 gdb_assert (result != NULL);
3195 return result;
3196 }
3197
3198 static void
3199 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
3200 void *data, int need_fullname)
3201 {
3202 int i;
3203 struct cleanup *cleanup;
3204 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3205 NULL, xcalloc, xfree);
3206
3207 cleanup = make_cleanup_htab_delete (visited);
3208 dw2_setup (objfile);
3209
3210 /* We can ignore file names coming from already-expanded CUs. */
3211 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3212 + dwarf2_per_objfile->n_type_units); ++i)
3213 {
3214 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3215
3216 if (per_cu->v.quick->symtab)
3217 {
3218 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3219 INSERT);
3220
3221 *slot = per_cu->v.quick->file_names;
3222 }
3223 }
3224
3225 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3226 + dwarf2_per_objfile->n_type_units); ++i)
3227 {
3228 int j;
3229 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3230 struct quick_file_names *file_data;
3231 void **slot;
3232
3233 /* We only need to look at symtabs not already expanded. */
3234 if (per_cu->v.quick->symtab)
3235 continue;
3236
3237 file_data = dw2_get_file_names (objfile, per_cu);
3238 if (file_data == NULL)
3239 continue;
3240
3241 slot = htab_find_slot (visited, file_data, INSERT);
3242 if (*slot)
3243 {
3244 /* Already visited. */
3245 continue;
3246 }
3247 *slot = file_data;
3248
3249 for (j = 0; j < file_data->num_file_names; ++j)
3250 {
3251 const char *this_real_name;
3252
3253 if (need_fullname)
3254 this_real_name = dw2_get_real_path (objfile, file_data, j);
3255 else
3256 this_real_name = NULL;
3257 (*fun) (file_data->file_names[j], this_real_name, data);
3258 }
3259 }
3260
3261 do_cleanups (cleanup);
3262 }
3263
3264 static int
3265 dw2_has_symbols (struct objfile *objfile)
3266 {
3267 return 1;
3268 }
3269
3270 const struct quick_symbol_functions dwarf2_gdb_index_functions =
3271 {
3272 dw2_has_symbols,
3273 dw2_find_last_source_symtab,
3274 dw2_forget_cached_source_info,
3275 dw2_map_symtabs_matching_filename,
3276 dw2_lookup_symbol,
3277 dw2_pre_expand_symtabs_matching,
3278 dw2_print_stats,
3279 dw2_dump,
3280 dw2_relocate,
3281 dw2_expand_symtabs_for_function,
3282 dw2_expand_all_symtabs,
3283 dw2_expand_symtabs_with_filename,
3284 dw2_find_symbol_file,
3285 dw2_map_matching_symbols,
3286 dw2_expand_symtabs_matching,
3287 dw2_find_pc_sect_symtab,
3288 dw2_map_symbol_filenames
3289 };
3290
3291 /* Initialize for reading DWARF for this objfile. Return 0 if this
3292 file will use psymtabs, or 1 if using the GNU index. */
3293
3294 int
3295 dwarf2_initialize_objfile (struct objfile *objfile)
3296 {
3297 /* If we're about to read full symbols, don't bother with the
3298 indices. In this case we also don't care if some other debug
3299 format is making psymtabs, because they are all about to be
3300 expanded anyway. */
3301 if ((objfile->flags & OBJF_READNOW))
3302 {
3303 int i;
3304
3305 dwarf2_per_objfile->using_index = 1;
3306 create_all_comp_units (objfile);
3307 create_all_type_units (objfile);
3308 dwarf2_per_objfile->quick_file_names_table =
3309 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3310
3311 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3312 + dwarf2_per_objfile->n_type_units); ++i)
3313 {
3314 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3315
3316 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3317 struct dwarf2_per_cu_quick_data);
3318 }
3319
3320 /* Return 1 so that gdb sees the "quick" functions. However,
3321 these functions will be no-ops because we will have expanded
3322 all symtabs. */
3323 return 1;
3324 }
3325
3326 if (dwarf2_read_index (objfile))
3327 return 1;
3328
3329 return 0;
3330 }
3331
3332 \f
3333
3334 /* Build a partial symbol table. */
3335
3336 void
3337 dwarf2_build_psymtabs (struct objfile *objfile)
3338 {
3339 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
3340 {
3341 init_psymbol_list (objfile, 1024);
3342 }
3343
3344 dwarf2_build_psymtabs_hard (objfile);
3345 }
3346
3347 /* Return TRUE if OFFSET is within CU_HEADER. */
3348
3349 static inline int
3350 offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
3351 {
3352 sect_offset bottom = { cu_header->offset.sect_off };
3353 sect_offset top = { (cu_header->offset.sect_off + cu_header->length
3354 + cu_header->initial_length_size) };
3355
3356 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
3357 }
3358
3359 /* Read in the comp unit header information from the debug_info at info_ptr.
3360 NOTE: This leaves members offset, first_die_offset to be filled in
3361 by the caller. */
3362
3363 static gdb_byte *
3364 read_comp_unit_head (struct comp_unit_head *cu_header,
3365 gdb_byte *info_ptr, bfd *abfd)
3366 {
3367 int signed_addr;
3368 unsigned int bytes_read;
3369
3370 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3371 cu_header->initial_length_size = bytes_read;
3372 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
3373 info_ptr += bytes_read;
3374 cu_header->version = read_2_bytes (abfd, info_ptr);
3375 info_ptr += 2;
3376 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3377 &bytes_read);
3378 info_ptr += bytes_read;
3379 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3380 info_ptr += 1;
3381 signed_addr = bfd_get_sign_extend_vma (abfd);
3382 if (signed_addr < 0)
3383 internal_error (__FILE__, __LINE__,
3384 _("read_comp_unit_head: dwarf from non elf file"));
3385 cu_header->signed_addr_p = signed_addr;
3386
3387 return info_ptr;
3388 }
3389
3390 /* Subroutine of read_and_check_comp_unit_head and
3391 read_and_check_type_unit_head to simplify them.
3392 Perform various error checking on the header. */
3393
3394 static void
3395 error_check_comp_unit_head (struct comp_unit_head *header,
3396 struct dwarf2_section_info *section,
3397 struct dwarf2_section_info *abbrev_section)
3398 {
3399 bfd *abfd = section->asection->owner;
3400 const char *filename = bfd_get_filename (abfd);
3401
3402 if (header->version != 2 && header->version != 3 && header->version != 4)
3403 error (_("Dwarf Error: wrong version in compilation unit header "
3404 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3405 filename);
3406
3407 if (header->abbrev_offset.sect_off
3408 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
3409 &dwarf2_per_objfile->abbrev))
3410 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3411 "(offset 0x%lx + 6) [in module %s]"),
3412 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
3413 filename);
3414
3415 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3416 avoid potential 32-bit overflow. */
3417 if (((unsigned long) header->offset.sect_off
3418 + header->length + header->initial_length_size)
3419 > section->size)
3420 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3421 "(offset 0x%lx + 0) [in module %s]"),
3422 (long) header->length, (long) header->offset.sect_off,
3423 filename);
3424 }
3425
3426 /* Read in a CU/TU header and perform some basic error checking.
3427 The contents of the header are stored in HEADER.
3428 The result is a pointer to the start of the first DIE. */
3429
3430 static gdb_byte *
3431 read_and_check_comp_unit_head (struct comp_unit_head *header,
3432 struct dwarf2_section_info *section,
3433 struct dwarf2_section_info *abbrev_section,
3434 gdb_byte *info_ptr,
3435 int is_debug_types_section)
3436 {
3437 gdb_byte *beg_of_comp_unit = info_ptr;
3438 bfd *abfd = section->asection->owner;
3439
3440 header->offset.sect_off = beg_of_comp_unit - section->buffer;
3441
3442 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3443
3444 /* If we're reading a type unit, skip over the signature and
3445 type_offset fields. */
3446 if (is_debug_types_section)
3447 info_ptr += 8 /*signature*/ + header->offset_size;
3448
3449 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
3450
3451 error_check_comp_unit_head (header, section, abbrev_section);
3452
3453 return info_ptr;
3454 }
3455
3456 /* Read in the types comp unit header information from .debug_types entry at
3457 types_ptr. The result is a pointer to one past the end of the header. */
3458
3459 static gdb_byte *
3460 read_and_check_type_unit_head (struct comp_unit_head *header,
3461 struct dwarf2_section_info *section,
3462 struct dwarf2_section_info *abbrev_section,
3463 gdb_byte *info_ptr,
3464 ULONGEST *signature,
3465 cu_offset *type_offset_in_tu)
3466 {
3467 gdb_byte *beg_of_comp_unit = info_ptr;
3468 bfd *abfd = section->asection->owner;
3469
3470 header->offset.sect_off = beg_of_comp_unit - section->buffer;
3471
3472 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3473
3474 /* If we're reading a type unit, skip over the signature and
3475 type_offset fields. */
3476 if (signature != NULL)
3477 *signature = read_8_bytes (abfd, info_ptr);
3478 info_ptr += 8;
3479 if (type_offset_in_tu != NULL)
3480 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
3481 header->offset_size);
3482 info_ptr += header->offset_size;
3483
3484 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
3485
3486 error_check_comp_unit_head (header, section, abbrev_section);
3487
3488 return info_ptr;
3489 }
3490
3491 /* Allocate a new partial symtab for file named NAME and mark this new
3492 partial symtab as being an include of PST. */
3493
3494 static void
3495 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3496 struct objfile *objfile)
3497 {
3498 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3499
3500 subpst->section_offsets = pst->section_offsets;
3501 subpst->textlow = 0;
3502 subpst->texthigh = 0;
3503
3504 subpst->dependencies = (struct partial_symtab **)
3505 obstack_alloc (&objfile->objfile_obstack,
3506 sizeof (struct partial_symtab *));
3507 subpst->dependencies[0] = pst;
3508 subpst->number_of_dependencies = 1;
3509
3510 subpst->globals_offset = 0;
3511 subpst->n_global_syms = 0;
3512 subpst->statics_offset = 0;
3513 subpst->n_static_syms = 0;
3514 subpst->symtab = NULL;
3515 subpst->read_symtab = pst->read_symtab;
3516 subpst->readin = 0;
3517
3518 /* No private part is necessary for include psymtabs. This property
3519 can be used to differentiate between such include psymtabs and
3520 the regular ones. */
3521 subpst->read_symtab_private = NULL;
3522 }
3523
3524 /* Read the Line Number Program data and extract the list of files
3525 included by the source file represented by PST. Build an include
3526 partial symtab for each of these included files. */
3527
3528 static void
3529 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
3530 struct die_info *die,
3531 struct partial_symtab *pst)
3532 {
3533 struct line_header *lh = NULL;
3534 struct attribute *attr;
3535
3536 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3537 if (attr)
3538 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
3539 if (lh == NULL)
3540 return; /* No linetable, so no includes. */
3541
3542 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
3543 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
3544
3545 free_line_header (lh);
3546 }
3547
3548 static hashval_t
3549 hash_signatured_type (const void *item)
3550 {
3551 const struct signatured_type *sig_type = item;
3552
3553 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3554 return sig_type->signature;
3555 }
3556
3557 static int
3558 eq_signatured_type (const void *item_lhs, const void *item_rhs)
3559 {
3560 const struct signatured_type *lhs = item_lhs;
3561 const struct signatured_type *rhs = item_rhs;
3562
3563 return lhs->signature == rhs->signature;
3564 }
3565
3566 /* Allocate a hash table for signatured types. */
3567
3568 static htab_t
3569 allocate_signatured_type_table (struct objfile *objfile)
3570 {
3571 return htab_create_alloc_ex (41,
3572 hash_signatured_type,
3573 eq_signatured_type,
3574 NULL,
3575 &objfile->objfile_obstack,
3576 hashtab_obstack_allocate,
3577 dummy_obstack_deallocate);
3578 }
3579
3580 /* A helper function to add a signatured type CU to a table. */
3581
3582 static int
3583 add_signatured_type_cu_to_table (void **slot, void *datum)
3584 {
3585 struct signatured_type *sigt = *slot;
3586 struct dwarf2_per_cu_data ***datap = datum;
3587
3588 **datap = &sigt->per_cu;
3589 ++*datap;
3590
3591 return 1;
3592 }
3593
3594 /* Create the hash table of all entries in the .debug_types section.
3595 DWO_FILE is a pointer to the DWO file for .debug_types.dwo, NULL otherwise.
3596 The result is a pointer to the hash table or NULL if there are
3597 no types. */
3598
3599 static htab_t
3600 create_debug_types_hash_table (struct dwo_file *dwo_file,
3601 VEC (dwarf2_section_info_def) *types)
3602 {
3603 struct objfile *objfile = dwarf2_per_objfile->objfile;
3604 htab_t types_htab = NULL;
3605 int ix;
3606 struct dwarf2_section_info *section;
3607 struct dwarf2_section_info *abbrev_section;
3608
3609 if (VEC_empty (dwarf2_section_info_def, types))
3610 return NULL;
3611
3612 abbrev_section = (dwo_file != NULL
3613 ? &dwo_file->sections.abbrev
3614 : &dwarf2_per_objfile->abbrev);
3615
3616 for (ix = 0;
3617 VEC_iterate (dwarf2_section_info_def, types, ix, section);
3618 ++ix)
3619 {
3620 bfd *abfd;
3621 gdb_byte *info_ptr, *end_ptr;
3622
3623 dwarf2_read_section (objfile, section);
3624 info_ptr = section->buffer;
3625
3626 if (info_ptr == NULL)
3627 continue;
3628
3629 /* We can't set abfd until now because the section may be empty or
3630 not present, in which case section->asection will be NULL. */
3631 abfd = section->asection->owner;
3632
3633 if (types_htab == NULL)
3634 {
3635 if (dwo_file)
3636 types_htab = allocate_dwo_unit_table (objfile);
3637 else
3638 types_htab = allocate_signatured_type_table (objfile);
3639 }
3640
3641 if (dwarf2_die_debug)
3642 fprintf_unfiltered (gdb_stdlog, "Reading signatured types for %s:\n",
3643 bfd_get_filename (abfd));
3644
3645 /* We don't use init_cutu_and_read_dies_simple, or some such, here
3646 because we don't need to read any dies: the signature is in the
3647 header. */
3648
3649 end_ptr = info_ptr + section->size;
3650 while (info_ptr < end_ptr)
3651 {
3652 sect_offset offset;
3653 cu_offset type_offset_in_tu;
3654 ULONGEST signature;
3655 struct signatured_type *sig_type;
3656 struct dwo_unit *dwo_tu;
3657 void **slot;
3658 gdb_byte *ptr = info_ptr;
3659 struct comp_unit_head header;
3660 unsigned int length;
3661
3662 offset.sect_off = ptr - section->buffer;
3663
3664 /* We need to read the type's signature in order to build the hash
3665 table, but we don't need anything else just yet. */
3666
3667 ptr = read_and_check_type_unit_head (&header, section,
3668 abbrev_section, ptr,
3669 &signature, &type_offset_in_tu);
3670
3671 length = header.initial_length_size + header.length;
3672
3673 /* Skip dummy type units. */
3674 if (ptr >= info_ptr + length
3675 || peek_abbrev_code (abfd, ptr) == 0)
3676 {
3677 info_ptr += header.initial_length_size + header.length;
3678 continue;
3679 }
3680
3681 if (dwo_file)
3682 {
3683 sig_type = NULL;
3684 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3685 struct dwo_unit);
3686 dwo_tu->dwo_file = dwo_file;
3687 dwo_tu->signature = signature;
3688 dwo_tu->type_offset_in_tu = type_offset_in_tu;
3689 dwo_tu->info_or_types_section = section;
3690 dwo_tu->offset = offset;
3691 dwo_tu->length = length;
3692 }
3693 else
3694 {
3695 /* N.B.: type_offset is not usable if this type uses a DWO file.
3696 The real type_offset is in the DWO file. */
3697 dwo_tu = NULL;
3698 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3699 struct signatured_type);
3700 sig_type->signature = signature;
3701 sig_type->type_offset_in_tu = type_offset_in_tu;
3702 sig_type->per_cu.objfile = objfile;
3703 sig_type->per_cu.is_debug_types = 1;
3704 sig_type->per_cu.info_or_types_section = section;
3705 sig_type->per_cu.offset = offset;
3706 sig_type->per_cu.length = length;
3707 }
3708
3709 slot = htab_find_slot (types_htab,
3710 dwo_file ? (void*) dwo_tu : (void *) sig_type,
3711 INSERT);
3712 gdb_assert (slot != NULL);
3713 if (*slot != NULL)
3714 {
3715 sect_offset dup_offset;
3716
3717 if (dwo_file)
3718 {
3719 const struct dwo_unit *dup_tu = *slot;
3720
3721 dup_offset = dup_tu->offset;
3722 }
3723 else
3724 {
3725 const struct signatured_type *dup_tu = *slot;
3726
3727 dup_offset = dup_tu->per_cu.offset;
3728 }
3729
3730 complaint (&symfile_complaints,
3731 _("debug type entry at offset 0x%x is duplicate to the "
3732 "entry at offset 0x%x, signature 0x%s"),
3733 offset.sect_off, dup_offset.sect_off,
3734 phex (signature, sizeof (signature)));
3735 }
3736 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3737
3738 if (dwarf2_die_debug)
3739 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3740 offset.sect_off,
3741 phex (signature, sizeof (signature)));
3742
3743 info_ptr += length;
3744 }
3745 }
3746
3747 return types_htab;
3748 }
3749
3750 /* Create the hash table of all entries in the .debug_types section,
3751 and initialize all_type_units.
3752 The result is zero if there is an error (e.g. missing .debug_types section),
3753 otherwise non-zero. */
3754
3755 static int
3756 create_all_type_units (struct objfile *objfile)
3757 {
3758 htab_t types_htab;
3759 struct dwarf2_per_cu_data **iter;
3760
3761 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
3762 if (types_htab == NULL)
3763 {
3764 dwarf2_per_objfile->signatured_types = NULL;
3765 return 0;
3766 }
3767
3768 dwarf2_per_objfile->signatured_types = types_htab;
3769
3770 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
3771 dwarf2_per_objfile->all_type_units
3772 = obstack_alloc (&objfile->objfile_obstack,
3773 dwarf2_per_objfile->n_type_units
3774 * sizeof (struct dwarf2_per_cu_data *));
3775 iter = &dwarf2_per_objfile->all_type_units[0];
3776 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
3777 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
3778 == dwarf2_per_objfile->n_type_units);
3779
3780 return 1;
3781 }
3782
3783 /* Lookup a signature based type for DW_FORM_ref_sig8.
3784 Returns NULL if signature SIG is not present in the table. */
3785
3786 static struct signatured_type *
3787 lookup_signatured_type (ULONGEST sig)
3788 {
3789 struct signatured_type find_entry, *entry;
3790
3791 if (dwarf2_per_objfile->signatured_types == NULL)
3792 {
3793 complaint (&symfile_complaints,
3794 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
3795 return NULL;
3796 }
3797
3798 find_entry.signature = sig;
3799 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3800 return entry;
3801 }
3802
3803 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3804
3805 static void
3806 init_cu_die_reader (struct die_reader_specs *reader,
3807 struct dwarf2_cu *cu,
3808 struct dwarf2_section_info *section,
3809 struct dwo_file *dwo_file)
3810 {
3811 gdb_assert (section->readin && section->buffer != NULL);
3812 reader->abfd = section->asection->owner;
3813 reader->cu = cu;
3814 reader->dwo_file = dwo_file;
3815 reader->die_section = section;
3816 reader->buffer = section->buffer;
3817 reader->buffer_end = section->buffer + section->size;
3818 }
3819
3820 /* Find the base address of the compilation unit for range lists and
3821 location lists. It will normally be specified by DW_AT_low_pc.
3822 In DWARF-3 draft 4, the base address could be overridden by
3823 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3824 compilation units with discontinuous ranges. */
3825
3826 static void
3827 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3828 {
3829 struct attribute *attr;
3830
3831 cu->base_known = 0;
3832 cu->base_address = 0;
3833
3834 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3835 if (attr)
3836 {
3837 cu->base_address = DW_ADDR (attr);
3838 cu->base_known = 1;
3839 }
3840 else
3841 {
3842 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3843 if (attr)
3844 {
3845 cu->base_address = DW_ADDR (attr);
3846 cu->base_known = 1;
3847 }
3848 }
3849 }
3850
3851 /* Initialize a CU (or TU) and read its DIEs.
3852 If the CU defers to a DWO file, read the DWO file as well.
3853
3854 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
3855 Otherwise, a new CU is allocated with xmalloc.
3856
3857 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
3858 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
3859
3860 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
3861 linker) then DIE_READER_FUNC will not get called. */
3862
3863 static void
3864 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
3865 int use_existing_cu, int keep,
3866 die_reader_func_ftype *die_reader_func,
3867 void *data)
3868 {
3869 struct objfile *objfile = dwarf2_per_objfile->objfile;
3870 struct dwarf2_section_info *section = this_cu->info_or_types_section;
3871 bfd *abfd = section->asection->owner;
3872 struct dwarf2_cu *cu;
3873 gdb_byte *begin_info_ptr, *info_ptr;
3874 struct die_reader_specs reader;
3875 struct die_info *comp_unit_die;
3876 int has_children;
3877 struct attribute *attr;
3878 struct cleanup *cleanups, *free_cu_cleanup = NULL;
3879 struct signatured_type *sig_type = NULL;
3880 struct dwarf2_section_info *abbrev_section;
3881
3882 if (use_existing_cu)
3883 gdb_assert (keep);
3884
3885 cleanups = make_cleanup (null_cleanup, NULL);
3886
3887 /* This is cheap if the section is already read in. */
3888 dwarf2_read_section (objfile, section);
3889
3890 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
3891 abbrev_section = &dwarf2_per_objfile->abbrev;
3892
3893 if (use_existing_cu && this_cu->cu != NULL)
3894 {
3895 cu = this_cu->cu;
3896 info_ptr += cu->header.first_die_offset.cu_off;
3897 }
3898 else
3899 {
3900 /* If !use_existing_cu, this_cu->cu must be NULL. */
3901 gdb_assert (this_cu->cu == NULL);
3902
3903 cu = xmalloc (sizeof (*cu));
3904 init_one_comp_unit (cu, this_cu);
3905
3906 /* If an error occurs while loading, release our storage. */
3907 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
3908
3909 if (this_cu->is_debug_types)
3910 {
3911 ULONGEST signature;
3912
3913 info_ptr = read_and_check_type_unit_head (&cu->header, section,
3914 abbrev_section, info_ptr,
3915 &signature, NULL);
3916
3917 /* There's no way to get from PER_CU to its containing
3918 struct signatured_type.
3919 But we have the signature so we can use that. */
3920 sig_type = lookup_signatured_type (signature);
3921 /* We've already scanned all the signatured types,
3922 this must succeed. */
3923 gdb_assert (sig_type != NULL);
3924 gdb_assert (&sig_type->per_cu == this_cu);
3925 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
3926
3927 /* LENGTH has not been set yet for type units. */
3928 this_cu->length = cu->header.length + cu->header.initial_length_size;
3929
3930 /* Establish the type offset that can be used to lookup the type. */
3931 sig_type->type_offset_in_section.sect_off =
3932 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
3933 }
3934 else
3935 {
3936 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
3937 abbrev_section,
3938 info_ptr, 0);
3939
3940 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
3941 gdb_assert (this_cu->length
3942 == cu->header.length + cu->header.initial_length_size);
3943 }
3944 }
3945
3946 /* Skip dummy compilation units. */
3947 if (info_ptr >= begin_info_ptr + this_cu->length
3948 || peek_abbrev_code (abfd, info_ptr) == 0)
3949 {
3950 do_cleanups (cleanups);
3951 return;
3952 }
3953
3954 /* Read the abbrevs for this compilation unit into a table. */
3955 if (cu->dwarf2_abbrevs == NULL)
3956 {
3957 dwarf2_read_abbrevs (cu, abbrev_section);
3958 make_cleanup (dwarf2_free_abbrev_table, cu);
3959 }
3960
3961 /* Read the top level CU/TU die. */
3962 init_cu_die_reader (&reader, cu, section, NULL);
3963 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
3964
3965 /* If we have a DWO stub, process it and then read in the DWO file.
3966 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
3967 a DWO CU, that this test will fail. */
3968 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
3969 if (attr)
3970 {
3971 char *dwo_name = DW_STRING (attr);
3972 const char *comp_dir;
3973 struct dwo_unit *dwo_unit;
3974 ULONGEST signature; /* Or dwo_id. */
3975 struct attribute *stmt_list, *low_pc, *high_pc, *ranges;
3976 int i,num_extra_attrs;
3977 struct dwarf2_section_info *dwo_abbrev_section;
3978
3979 if (has_children)
3980 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
3981 " has children (offset 0x%x) [in module %s]"),
3982 this_cu->offset.sect_off, bfd_get_filename (abfd));
3983
3984 /* These attributes aren't processed until later:
3985 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
3986 However, the attribute is found in the stub which we won't have later.
3987 In order to not impose this complication on the rest of the code,
3988 we read them here and copy them to the DWO CU/TU die. */
3989 stmt_list = low_pc = high_pc = ranges = NULL;
3990
3991 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
3992 DWO file. */
3993 if (! this_cu->is_debug_types)
3994 stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3995 low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
3996 high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
3997 ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
3998
3999 /* There should be a DW_AT_addr_base attribute here (if needed).
4000 We need the value before we can process DW_FORM_GNU_addr_index. */
4001 cu->addr_base = 0;
4002 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
4003 if (attr)
4004 cu->addr_base = DW_UNSND (attr);
4005
4006 /* There should be a DW_AT_ranges_base attribute here (if needed).
4007 We need the value before we can process DW_AT_ranges. */
4008 cu->ranges_base = 0;
4009 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_ranges_base, cu);
4010 if (attr)
4011 cu->ranges_base = DW_UNSND (attr);
4012
4013 if (this_cu->is_debug_types)
4014 {
4015 gdb_assert (sig_type != NULL);
4016 signature = sig_type->signature;
4017 }
4018 else
4019 {
4020 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4021 if (! attr)
4022 error (_("Dwarf Error: missing dwo_id [in module %s]"),
4023 dwo_name);
4024 signature = DW_UNSND (attr);
4025 }
4026
4027 /* We may need the comp_dir in order to find the DWO file. */
4028 comp_dir = NULL;
4029 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4030 if (attr)
4031 comp_dir = DW_STRING (attr);
4032
4033 if (this_cu->is_debug_types)
4034 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
4035 else
4036 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
4037 signature);
4038
4039 if (dwo_unit == NULL)
4040 {
4041 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
4042 " with ID %s [in module %s]"),
4043 this_cu->offset.sect_off,
4044 phex (signature, sizeof (signature)),
4045 objfile->name);
4046 }
4047
4048 /* Set up for reading the DWO CU/TU. */
4049 cu->dwo_unit = dwo_unit;
4050 section = dwo_unit->info_or_types_section;
4051 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4052 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
4053 init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
4054
4055 if (this_cu->is_debug_types)
4056 {
4057 ULONGEST signature;
4058
4059 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4060 dwo_abbrev_section,
4061 info_ptr,
4062 &signature, NULL);
4063 gdb_assert (sig_type->signature == signature);
4064 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4065 gdb_assert (dwo_unit->length
4066 == cu->header.length + cu->header.initial_length_size);
4067
4068 /* Establish the type offset that can be used to lookup the type.
4069 For DWO files, we don't know it until now. */
4070 sig_type->type_offset_in_section.sect_off =
4071 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4072 }
4073 else
4074 {
4075 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4076 dwo_abbrev_section,
4077 info_ptr, 0);
4078 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4079 gdb_assert (dwo_unit->length
4080 == cu->header.length + cu->header.initial_length_size);
4081 }
4082
4083 /* Discard the original CU's abbrev table, and read the DWO's. */
4084 dwarf2_free_abbrev_table (cu);
4085 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4086
4087 /* Read in the die, but leave space to copy over the attributes
4088 from the stub. This has the benefit of simplifying the rest of
4089 the code - all the real work is done here. */
4090 num_extra_attrs = ((stmt_list != NULL)
4091 + (low_pc != NULL)
4092 + (high_pc != NULL)
4093 + (ranges != NULL));
4094 info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
4095 &has_children, num_extra_attrs);
4096
4097 /* Copy over the attributes from the stub to the DWO die. */
4098 i = comp_unit_die->num_attrs;
4099 if (stmt_list != NULL)
4100 comp_unit_die->attrs[i++] = *stmt_list;
4101 if (low_pc != NULL)
4102 comp_unit_die->attrs[i++] = *low_pc;
4103 if (high_pc != NULL)
4104 comp_unit_die->attrs[i++] = *high_pc;
4105 if (ranges != NULL)
4106 comp_unit_die->attrs[i++] = *ranges;
4107 comp_unit_die->num_attrs += num_extra_attrs;
4108
4109 /* Skip dummy compilation units. */
4110 if (info_ptr >= begin_info_ptr + dwo_unit->length
4111 || peek_abbrev_code (abfd, info_ptr) == 0)
4112 {
4113 do_cleanups (cleanups);
4114 return;
4115 }
4116 }
4117
4118 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4119
4120 if (free_cu_cleanup != NULL)
4121 {
4122 if (keep)
4123 {
4124 /* We've successfully allocated this compilation unit. Let our
4125 caller clean it up when finished with it. */
4126 discard_cleanups (free_cu_cleanup);
4127
4128 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4129 So we have to manually free the abbrev table. */
4130 dwarf2_free_abbrev_table (cu);
4131
4132 /* Link this CU into read_in_chain. */
4133 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4134 dwarf2_per_objfile->read_in_chain = this_cu;
4135 }
4136 else
4137 do_cleanups (free_cu_cleanup);
4138 }
4139
4140 do_cleanups (cleanups);
4141 }
4142
4143 /* Read CU/TU THIS_CU in section SECTION,
4144 but do not follow DW_AT_GNU_dwo_name if present.
4145 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed to
4146 have already done the lookup to find the DWO file).
4147
4148 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
4149 THIS_CU->is_debug_types, but nothing else.
4150
4151 We fill in THIS_CU->length.
4152
4153 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4154 linker) then DIE_READER_FUNC will not get called.
4155
4156 THIS_CU->cu is always freed when done.
4157 This is done in order to not leave THIS_CU->cu in a state where we have
4158 to care whether it refers to the "main" CU or the DWO CU. */
4159
4160 static void
4161 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4162 struct dwarf2_section_info *abbrev_section,
4163 struct dwo_file *dwo_file,
4164 die_reader_func_ftype *die_reader_func,
4165 void *data)
4166 {
4167 struct objfile *objfile = dwarf2_per_objfile->objfile;
4168 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4169 bfd *abfd = section->asection->owner;
4170 struct dwarf2_cu cu;
4171 gdb_byte *begin_info_ptr, *info_ptr;
4172 struct die_reader_specs reader;
4173 struct cleanup *cleanups;
4174 struct die_info *comp_unit_die;
4175 int has_children;
4176
4177 gdb_assert (this_cu->cu == NULL);
4178
4179 /* This is cheap if the section is already read in. */
4180 dwarf2_read_section (objfile, section);
4181
4182 init_one_comp_unit (&cu, this_cu);
4183
4184 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4185
4186 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4187 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
4188 abbrev_section, info_ptr,
4189 this_cu->is_debug_types);
4190
4191 this_cu->length = cu.header.length + cu.header.initial_length_size;
4192
4193 /* Skip dummy compilation units. */
4194 if (info_ptr >= begin_info_ptr + this_cu->length
4195 || peek_abbrev_code (abfd, info_ptr) == 0)
4196 {
4197 do_cleanups (cleanups);
4198 return;
4199 }
4200
4201 dwarf2_read_abbrevs (&cu, abbrev_section);
4202 make_cleanup (dwarf2_free_abbrev_table, &cu);
4203
4204 init_cu_die_reader (&reader, &cu, section, dwo_file);
4205 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4206
4207 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4208
4209 do_cleanups (cleanups);
4210 }
4211
4212 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4213 does not lookup the specified DWO file.
4214 This cannot be used to read DWO files.
4215
4216 THIS_CU->cu is always freed when done.
4217 This is done in order to not leave THIS_CU->cu in a state where we have
4218 to care whether it refers to the "main" CU or the DWO CU.
4219 We can revisit this if the data shows there's a performance issue. */
4220
4221 static void
4222 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4223 die_reader_func_ftype *die_reader_func,
4224 void *data)
4225 {
4226 init_cutu_and_read_dies_no_follow (this_cu,
4227 &dwarf2_per_objfile->abbrev,
4228 NULL,
4229 die_reader_func, data);
4230 }
4231
4232 /* die_reader_func for process_psymtab_comp_unit. */
4233
4234 static void
4235 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
4236 gdb_byte *info_ptr,
4237 struct die_info *comp_unit_die,
4238 int has_children,
4239 void *data)
4240 {
4241 struct dwarf2_cu *cu = reader->cu;
4242 struct objfile *objfile = cu->objfile;
4243 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
4244 struct attribute *attr;
4245 CORE_ADDR baseaddr;
4246 CORE_ADDR best_lowpc = 0, best_highpc = 0;
4247 struct partial_symtab *pst;
4248 int has_pc_info;
4249 const char *filename;
4250 int *want_partial_unit_ptr = data;
4251
4252 if (comp_unit_die->tag == DW_TAG_partial_unit
4253 && (want_partial_unit_ptr == NULL
4254 || !*want_partial_unit_ptr))
4255 return;
4256
4257 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
4258
4259 cu->list_in_scope = &file_symbols;
4260
4261 /* Allocate a new partial symbol table structure. */
4262 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
4263 if (attr == NULL || !DW_STRING (attr))
4264 filename = "";
4265 else
4266 filename = DW_STRING (attr);
4267 pst = start_psymtab_common (objfile, objfile->section_offsets,
4268 filename,
4269 /* TEXTLOW and TEXTHIGH are set below. */
4270 0,
4271 objfile->global_psymbols.next,
4272 objfile->static_psymbols.next);
4273 pst->psymtabs_addrmap_supported = 1;
4274
4275 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4276 if (attr != NULL)
4277 pst->dirname = DW_STRING (attr);
4278
4279 pst->read_symtab_private = per_cu;
4280
4281 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4282
4283 /* Store the function that reads in the rest of the symbol table. */
4284 pst->read_symtab = dwarf2_psymtab_to_symtab;
4285
4286 per_cu->v.psymtab = pst;
4287
4288 dwarf2_find_base_address (comp_unit_die, cu);
4289
4290 /* Possibly set the default values of LOWPC and HIGHPC from
4291 `DW_AT_ranges'. */
4292 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
4293 &best_highpc, cu, pst);
4294 if (has_pc_info == 1 && best_lowpc < best_highpc)
4295 /* Store the contiguous range if it is not empty; it can be empty for
4296 CUs with no code. */
4297 addrmap_set_empty (objfile->psymtabs_addrmap,
4298 best_lowpc + baseaddr,
4299 best_highpc + baseaddr - 1, pst);
4300
4301 /* Check if comp unit has_children.
4302 If so, read the rest of the partial symbols from this comp unit.
4303 If not, there's no more debug_info for this comp unit. */
4304 if (has_children)
4305 {
4306 struct partial_die_info *first_die;
4307 CORE_ADDR lowpc, highpc;
4308
4309 lowpc = ((CORE_ADDR) -1);
4310 highpc = ((CORE_ADDR) 0);
4311
4312 first_die = load_partial_dies (reader, info_ptr, 1);
4313
4314 scan_partial_symbols (first_die, &lowpc, &highpc,
4315 ! has_pc_info, cu);
4316
4317 /* If we didn't find a lowpc, set it to highpc to avoid
4318 complaints from `maint check'. */
4319 if (lowpc == ((CORE_ADDR) -1))
4320 lowpc = highpc;
4321
4322 /* If the compilation unit didn't have an explicit address range,
4323 then use the information extracted from its child dies. */
4324 if (! has_pc_info)
4325 {
4326 best_lowpc = lowpc;
4327 best_highpc = highpc;
4328 }
4329 }
4330 pst->textlow = best_lowpc + baseaddr;
4331 pst->texthigh = best_highpc + baseaddr;
4332
4333 pst->n_global_syms = objfile->global_psymbols.next -
4334 (objfile->global_psymbols.list + pst->globals_offset);
4335 pst->n_static_syms = objfile->static_psymbols.next -
4336 (objfile->static_psymbols.list + pst->statics_offset);
4337 sort_pst_symbols (pst);
4338
4339 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
4340 {
4341 int i;
4342 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
4343 struct dwarf2_per_cu_data *iter;
4344
4345 /* Fill in 'dependencies' here; we fill in 'users' in a
4346 post-pass. */
4347 pst->number_of_dependencies = len;
4348 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
4349 len * sizeof (struct symtab *));
4350 for (i = 0;
4351 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
4352 i, iter);
4353 ++i)
4354 pst->dependencies[i] = iter->v.psymtab;
4355
4356 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
4357 }
4358
4359 if (per_cu->is_debug_types)
4360 {
4361 /* It's not clear we want to do anything with stmt lists here.
4362 Waiting to see what gcc ultimately does. */
4363 }
4364 else
4365 {
4366 /* Get the list of files included in the current compilation unit,
4367 and build a psymtab for each of them. */
4368 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
4369 }
4370 }
4371
4372 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4373 Process compilation unit THIS_CU for a psymtab. */
4374
4375 static void
4376 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
4377 int want_partial_unit)
4378 {
4379 /* If this compilation unit was already read in, free the
4380 cached copy in order to read it in again. This is
4381 necessary because we skipped some symbols when we first
4382 read in the compilation unit (see load_partial_dies).
4383 This problem could be avoided, but the benefit is unclear. */
4384 if (this_cu->cu != NULL)
4385 free_one_cached_comp_unit (this_cu);
4386
4387 gdb_assert (! this_cu->is_debug_types);
4388 init_cutu_and_read_dies (this_cu, 0, 0, process_psymtab_comp_unit_reader,
4389 &want_partial_unit);
4390
4391 /* Age out any secondary CUs. */
4392 age_cached_comp_units ();
4393 }
4394
4395 /* Traversal function for htab_traverse_noresize.
4396 Process one .debug_types comp-unit. */
4397
4398 static int
4399 process_psymtab_type_unit (void **slot, void *info)
4400 {
4401 struct signatured_type *sig_type = (struct signatured_type *) *slot;
4402 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
4403
4404 gdb_assert (per_cu->is_debug_types);
4405 gdb_assert (info == NULL);
4406
4407 /* If this compilation unit was already read in, free the
4408 cached copy in order to read it in again. This is
4409 necessary because we skipped some symbols when we first
4410 read in the compilation unit (see load_partial_dies).
4411 This problem could be avoided, but the benefit is unclear. */
4412 if (per_cu->cu != NULL)
4413 free_one_cached_comp_unit (per_cu);
4414
4415 init_cutu_and_read_dies (per_cu, 0, 0, process_psymtab_comp_unit_reader,
4416 NULL);
4417
4418 /* Age out any secondary CUs. */
4419 age_cached_comp_units ();
4420
4421 return 1;
4422 }
4423
4424 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4425 Build partial symbol tables for the .debug_types comp-units. */
4426
4427 static void
4428 build_type_psymtabs (struct objfile *objfile)
4429 {
4430 if (! create_all_type_units (objfile))
4431 return;
4432
4433 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
4434 process_psymtab_type_unit, NULL);
4435 }
4436
4437 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
4438
4439 static void
4440 psymtabs_addrmap_cleanup (void *o)
4441 {
4442 struct objfile *objfile = o;
4443
4444 objfile->psymtabs_addrmap = NULL;
4445 }
4446
4447 /* Compute the 'user' field for each psymtab in OBJFILE. */
4448
4449 static void
4450 set_partial_user (struct objfile *objfile)
4451 {
4452 int i;
4453
4454 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4455 {
4456 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4457 struct partial_symtab *pst = per_cu->v.psymtab;
4458 int j;
4459
4460 for (j = 0; j < pst->number_of_dependencies; ++j)
4461 {
4462 /* Set the 'user' field only if it is not already set. */
4463 if (pst->dependencies[j]->user == NULL)
4464 pst->dependencies[j]->user = pst;
4465 }
4466 }
4467 }
4468
4469 /* Build the partial symbol table by doing a quick pass through the
4470 .debug_info and .debug_abbrev sections. */
4471
4472 static void
4473 dwarf2_build_psymtabs_hard (struct objfile *objfile)
4474 {
4475 struct cleanup *back_to, *addrmap_cleanup;
4476 struct obstack temp_obstack;
4477 int i;
4478
4479 if (dwarf2_read_debug)
4480 {
4481 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4482 objfile->name);
4483 }
4484
4485 dwarf2_per_objfile->reading_partial_symbols = 1;
4486
4487 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4488
4489 /* Any cached compilation units will be linked by the per-objfile
4490 read_in_chain. Make sure to free them when we're done. */
4491 back_to = make_cleanup (free_cached_comp_units, NULL);
4492
4493 build_type_psymtabs (objfile);
4494
4495 create_all_comp_units (objfile);
4496
4497 /* Create a temporary address map on a temporary obstack. We later
4498 copy this to the final obstack. */
4499 obstack_init (&temp_obstack);
4500 make_cleanup_obstack_free (&temp_obstack);
4501 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
4502 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
4503
4504 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4505 {
4506 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4507
4508 process_psymtab_comp_unit (per_cu, 0);
4509 }
4510
4511 set_partial_user (objfile);
4512
4513 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
4514 &objfile->objfile_obstack);
4515 discard_cleanups (addrmap_cleanup);
4516
4517 do_cleanups (back_to);
4518
4519 if (dwarf2_read_debug)
4520 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4521 objfile->name);
4522 }
4523
4524 /* die_reader_func for load_partial_comp_unit. */
4525
4526 static void
4527 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
4528 gdb_byte *info_ptr,
4529 struct die_info *comp_unit_die,
4530 int has_children,
4531 void *data)
4532 {
4533 struct dwarf2_cu *cu = reader->cu;
4534
4535 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
4536
4537 /* Check if comp unit has_children.
4538 If so, read the rest of the partial symbols from this comp unit.
4539 If not, there's no more debug_info for this comp unit. */
4540 if (has_children)
4541 load_partial_dies (reader, info_ptr, 0);
4542 }
4543
4544 /* Load the partial DIEs for a secondary CU into memory.
4545 This is also used when rereading a primary CU with load_all_dies. */
4546
4547 static void
4548 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
4549 {
4550 init_cutu_and_read_dies (this_cu, 1, 1, load_partial_comp_unit_reader, NULL);
4551 }
4552
4553 /* Create a list of all compilation units in OBJFILE.
4554 This is only done for -readnow and building partial symtabs. */
4555
4556 static void
4557 create_all_comp_units (struct objfile *objfile)
4558 {
4559 int n_allocated;
4560 int n_comp_units;
4561 struct dwarf2_per_cu_data **all_comp_units;
4562 gdb_byte *info_ptr;
4563
4564 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4565 info_ptr = dwarf2_per_objfile->info.buffer;
4566
4567 n_comp_units = 0;
4568 n_allocated = 10;
4569 all_comp_units = xmalloc (n_allocated
4570 * sizeof (struct dwarf2_per_cu_data *));
4571
4572 while (info_ptr < dwarf2_per_objfile->info.buffer
4573 + dwarf2_per_objfile->info.size)
4574 {
4575 unsigned int length, initial_length_size;
4576 struct dwarf2_per_cu_data *this_cu;
4577 sect_offset offset;
4578
4579 offset.sect_off = info_ptr - dwarf2_per_objfile->info.buffer;
4580
4581 /* Read just enough information to find out where the next
4582 compilation unit is. */
4583 length = read_initial_length (objfile->obfd, info_ptr,
4584 &initial_length_size);
4585
4586 /* Save the compilation unit for later lookup. */
4587 this_cu = obstack_alloc (&objfile->objfile_obstack,
4588 sizeof (struct dwarf2_per_cu_data));
4589 memset (this_cu, 0, sizeof (*this_cu));
4590 this_cu->offset = offset;
4591 this_cu->length = length + initial_length_size;
4592 this_cu->objfile = objfile;
4593 this_cu->info_or_types_section = &dwarf2_per_objfile->info;
4594
4595 if (n_comp_units == n_allocated)
4596 {
4597 n_allocated *= 2;
4598 all_comp_units = xrealloc (all_comp_units,
4599 n_allocated
4600 * sizeof (struct dwarf2_per_cu_data *));
4601 }
4602 all_comp_units[n_comp_units++] = this_cu;
4603
4604 info_ptr = info_ptr + this_cu->length;
4605 }
4606
4607 dwarf2_per_objfile->all_comp_units
4608 = obstack_alloc (&objfile->objfile_obstack,
4609 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
4610 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
4611 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
4612 xfree (all_comp_units);
4613 dwarf2_per_objfile->n_comp_units = n_comp_units;
4614 }
4615
4616 /* Process all loaded DIEs for compilation unit CU, starting at
4617 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
4618 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
4619 DW_AT_ranges). If NEED_PC is set, then this function will set
4620 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
4621 and record the covered ranges in the addrmap. */
4622
4623 static void
4624 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
4625 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4626 {
4627 struct partial_die_info *pdi;
4628
4629 /* Now, march along the PDI's, descending into ones which have
4630 interesting children but skipping the children of the other ones,
4631 until we reach the end of the compilation unit. */
4632
4633 pdi = first_die;
4634
4635 while (pdi != NULL)
4636 {
4637 fixup_partial_die (pdi, cu);
4638
4639 /* Anonymous namespaces or modules have no name but have interesting
4640 children, so we need to look at them. Ditto for anonymous
4641 enums. */
4642
4643 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
4644 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
4645 || pdi->tag == DW_TAG_imported_unit)
4646 {
4647 switch (pdi->tag)
4648 {
4649 case DW_TAG_subprogram:
4650 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4651 break;
4652 case DW_TAG_constant:
4653 case DW_TAG_variable:
4654 case DW_TAG_typedef:
4655 case DW_TAG_union_type:
4656 if (!pdi->is_declaration)
4657 {
4658 add_partial_symbol (pdi, cu);
4659 }
4660 break;
4661 case DW_TAG_class_type:
4662 case DW_TAG_interface_type:
4663 case DW_TAG_structure_type:
4664 if (!pdi->is_declaration)
4665 {
4666 add_partial_symbol (pdi, cu);
4667 }
4668 break;
4669 case DW_TAG_enumeration_type:
4670 if (!pdi->is_declaration)
4671 add_partial_enumeration (pdi, cu);
4672 break;
4673 case DW_TAG_base_type:
4674 case DW_TAG_subrange_type:
4675 /* File scope base type definitions are added to the partial
4676 symbol table. */
4677 add_partial_symbol (pdi, cu);
4678 break;
4679 case DW_TAG_namespace:
4680 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
4681 break;
4682 case DW_TAG_module:
4683 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
4684 break;
4685 case DW_TAG_imported_unit:
4686 {
4687 struct dwarf2_per_cu_data *per_cu;
4688
4689 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
4690 cu->objfile);
4691
4692 /* Go read the partial unit, if needed. */
4693 if (per_cu->v.psymtab == NULL)
4694 process_psymtab_comp_unit (per_cu, 1);
4695
4696 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
4697 per_cu);
4698 }
4699 break;
4700 default:
4701 break;
4702 }
4703 }
4704
4705 /* If the die has a sibling, skip to the sibling. */
4706
4707 pdi = pdi->die_sibling;
4708 }
4709 }
4710
4711 /* Functions used to compute the fully scoped name of a partial DIE.
4712
4713 Normally, this is simple. For C++, the parent DIE's fully scoped
4714 name is concatenated with "::" and the partial DIE's name. For
4715 Java, the same thing occurs except that "." is used instead of "::".
4716 Enumerators are an exception; they use the scope of their parent
4717 enumeration type, i.e. the name of the enumeration type is not
4718 prepended to the enumerator.
4719
4720 There are two complexities. One is DW_AT_specification; in this
4721 case "parent" means the parent of the target of the specification,
4722 instead of the direct parent of the DIE. The other is compilers
4723 which do not emit DW_TAG_namespace; in this case we try to guess
4724 the fully qualified name of structure types from their members'
4725 linkage names. This must be done using the DIE's children rather
4726 than the children of any DW_AT_specification target. We only need
4727 to do this for structures at the top level, i.e. if the target of
4728 any DW_AT_specification (if any; otherwise the DIE itself) does not
4729 have a parent. */
4730
4731 /* Compute the scope prefix associated with PDI's parent, in
4732 compilation unit CU. The result will be allocated on CU's
4733 comp_unit_obstack, or a copy of the already allocated PDI->NAME
4734 field. NULL is returned if no prefix is necessary. */
4735 static char *
4736 partial_die_parent_scope (struct partial_die_info *pdi,
4737 struct dwarf2_cu *cu)
4738 {
4739 char *grandparent_scope;
4740 struct partial_die_info *parent, *real_pdi;
4741
4742 /* We need to look at our parent DIE; if we have a DW_AT_specification,
4743 then this means the parent of the specification DIE. */
4744
4745 real_pdi = pdi;
4746 while (real_pdi->has_specification)
4747 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
4748
4749 parent = real_pdi->die_parent;
4750 if (parent == NULL)
4751 return NULL;
4752
4753 if (parent->scope_set)
4754 return parent->scope;
4755
4756 fixup_partial_die (parent, cu);
4757
4758 grandparent_scope = partial_die_parent_scope (parent, cu);
4759
4760 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
4761 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
4762 Work around this problem here. */
4763 if (cu->language == language_cplus
4764 && parent->tag == DW_TAG_namespace
4765 && strcmp (parent->name, "::") == 0
4766 && grandparent_scope == NULL)
4767 {
4768 parent->scope = NULL;
4769 parent->scope_set = 1;
4770 return NULL;
4771 }
4772
4773 if (pdi->tag == DW_TAG_enumerator)
4774 /* Enumerators should not get the name of the enumeration as a prefix. */
4775 parent->scope = grandparent_scope;
4776 else if (parent->tag == DW_TAG_namespace
4777 || parent->tag == DW_TAG_module
4778 || parent->tag == DW_TAG_structure_type
4779 || parent->tag == DW_TAG_class_type
4780 || parent->tag == DW_TAG_interface_type
4781 || parent->tag == DW_TAG_union_type
4782 || parent->tag == DW_TAG_enumeration_type)
4783 {
4784 if (grandparent_scope == NULL)
4785 parent->scope = parent->name;
4786 else
4787 parent->scope = typename_concat (&cu->comp_unit_obstack,
4788 grandparent_scope,
4789 parent->name, 0, cu);
4790 }
4791 else
4792 {
4793 /* FIXME drow/2004-04-01: What should we be doing with
4794 function-local names? For partial symbols, we should probably be
4795 ignoring them. */
4796 complaint (&symfile_complaints,
4797 _("unhandled containing DIE tag %d for DIE at %d"),
4798 parent->tag, pdi->offset.sect_off);
4799 parent->scope = grandparent_scope;
4800 }
4801
4802 parent->scope_set = 1;
4803 return parent->scope;
4804 }
4805
4806 /* Return the fully scoped name associated with PDI, from compilation unit
4807 CU. The result will be allocated with malloc. */
4808
4809 static char *
4810 partial_die_full_name (struct partial_die_info *pdi,
4811 struct dwarf2_cu *cu)
4812 {
4813 char *parent_scope;
4814
4815 /* If this is a template instantiation, we can not work out the
4816 template arguments from partial DIEs. So, unfortunately, we have
4817 to go through the full DIEs. At least any work we do building
4818 types here will be reused if full symbols are loaded later. */
4819 if (pdi->has_template_arguments)
4820 {
4821 fixup_partial_die (pdi, cu);
4822
4823 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
4824 {
4825 struct die_info *die;
4826 struct attribute attr;
4827 struct dwarf2_cu *ref_cu = cu;
4828
4829 /* DW_FORM_ref_addr is using section offset. */
4830 attr.name = 0;
4831 attr.form = DW_FORM_ref_addr;
4832 attr.u.unsnd = pdi->offset.sect_off;
4833 die = follow_die_ref (NULL, &attr, &ref_cu);
4834
4835 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
4836 }
4837 }
4838
4839 parent_scope = partial_die_parent_scope (pdi, cu);
4840 if (parent_scope == NULL)
4841 return NULL;
4842 else
4843 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
4844 }
4845
4846 static void
4847 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
4848 {
4849 struct objfile *objfile = cu->objfile;
4850 CORE_ADDR addr = 0;
4851 char *actual_name = NULL;
4852 CORE_ADDR baseaddr;
4853 int built_actual_name = 0;
4854
4855 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4856
4857 actual_name = partial_die_full_name (pdi, cu);
4858 if (actual_name)
4859 built_actual_name = 1;
4860
4861 if (actual_name == NULL)
4862 actual_name = pdi->name;
4863
4864 switch (pdi->tag)
4865 {
4866 case DW_TAG_subprogram:
4867 if (pdi->is_external || cu->language == language_ada)
4868 {
4869 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
4870 of the global scope. But in Ada, we want to be able to access
4871 nested procedures globally. So all Ada subprograms are stored
4872 in the global scope. */
4873 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
4874 mst_text, objfile); */
4875 add_psymbol_to_list (actual_name, strlen (actual_name),
4876 built_actual_name,
4877 VAR_DOMAIN, LOC_BLOCK,
4878 &objfile->global_psymbols,
4879 0, pdi->lowpc + baseaddr,
4880 cu->language, objfile);
4881 }
4882 else
4883 {
4884 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
4885 mst_file_text, objfile); */
4886 add_psymbol_to_list (actual_name, strlen (actual_name),
4887 built_actual_name,
4888 VAR_DOMAIN, LOC_BLOCK,
4889 &objfile->static_psymbols,
4890 0, pdi->lowpc + baseaddr,
4891 cu->language, objfile);
4892 }
4893 break;
4894 case DW_TAG_constant:
4895 {
4896 struct psymbol_allocation_list *list;
4897
4898 if (pdi->is_external)
4899 list = &objfile->global_psymbols;
4900 else
4901 list = &objfile->static_psymbols;
4902 add_psymbol_to_list (actual_name, strlen (actual_name),
4903 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4904 list, 0, 0, cu->language, objfile);
4905 }
4906 break;
4907 case DW_TAG_variable:
4908 if (pdi->d.locdesc)
4909 addr = decode_locdesc (pdi->d.locdesc, cu);
4910
4911 if (pdi->d.locdesc
4912 && addr == 0
4913 && !dwarf2_per_objfile->has_section_at_zero)
4914 {
4915 /* A global or static variable may also have been stripped
4916 out by the linker if unused, in which case its address
4917 will be nullified; do not add such variables into partial
4918 symbol table then. */
4919 }
4920 else if (pdi->is_external)
4921 {
4922 /* Global Variable.
4923 Don't enter into the minimal symbol tables as there is
4924 a minimal symbol table entry from the ELF symbols already.
4925 Enter into partial symbol table if it has a location
4926 descriptor or a type.
4927 If the location descriptor is missing, new_symbol will create
4928 a LOC_UNRESOLVED symbol, the address of the variable will then
4929 be determined from the minimal symbol table whenever the variable
4930 is referenced.
4931 The address for the partial symbol table entry is not
4932 used by GDB, but it comes in handy for debugging partial symbol
4933 table building. */
4934
4935 if (pdi->d.locdesc || pdi->has_type)
4936 add_psymbol_to_list (actual_name, strlen (actual_name),
4937 built_actual_name,
4938 VAR_DOMAIN, LOC_STATIC,
4939 &objfile->global_psymbols,
4940 0, addr + baseaddr,
4941 cu->language, objfile);
4942 }
4943 else
4944 {
4945 /* Static Variable. Skip symbols without location descriptors. */
4946 if (pdi->d.locdesc == NULL)
4947 {
4948 if (built_actual_name)
4949 xfree (actual_name);
4950 return;
4951 }
4952 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
4953 mst_file_data, objfile); */
4954 add_psymbol_to_list (actual_name, strlen (actual_name),
4955 built_actual_name,
4956 VAR_DOMAIN, LOC_STATIC,
4957 &objfile->static_psymbols,
4958 0, addr + baseaddr,
4959 cu->language, objfile);
4960 }
4961 break;
4962 case DW_TAG_typedef:
4963 case DW_TAG_base_type:
4964 case DW_TAG_subrange_type:
4965 add_psymbol_to_list (actual_name, strlen (actual_name),
4966 built_actual_name,
4967 VAR_DOMAIN, LOC_TYPEDEF,
4968 &objfile->static_psymbols,
4969 0, (CORE_ADDR) 0, cu->language, objfile);
4970 break;
4971 case DW_TAG_namespace:
4972 add_psymbol_to_list (actual_name, strlen (actual_name),
4973 built_actual_name,
4974 VAR_DOMAIN, LOC_TYPEDEF,
4975 &objfile->global_psymbols,
4976 0, (CORE_ADDR) 0, cu->language, objfile);
4977 break;
4978 case DW_TAG_class_type:
4979 case DW_TAG_interface_type:
4980 case DW_TAG_structure_type:
4981 case DW_TAG_union_type:
4982 case DW_TAG_enumeration_type:
4983 /* Skip external references. The DWARF standard says in the section
4984 about "Structure, Union, and Class Type Entries": "An incomplete
4985 structure, union or class type is represented by a structure,
4986 union or class entry that does not have a byte size attribute
4987 and that has a DW_AT_declaration attribute." */
4988 if (!pdi->has_byte_size && pdi->is_declaration)
4989 {
4990 if (built_actual_name)
4991 xfree (actual_name);
4992 return;
4993 }
4994
4995 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4996 static vs. global. */
4997 add_psymbol_to_list (actual_name, strlen (actual_name),
4998 built_actual_name,
4999 STRUCT_DOMAIN, LOC_TYPEDEF,
5000 (cu->language == language_cplus
5001 || cu->language == language_java)
5002 ? &objfile->global_psymbols
5003 : &objfile->static_psymbols,
5004 0, (CORE_ADDR) 0, cu->language, objfile);
5005
5006 break;
5007 case DW_TAG_enumerator:
5008 add_psymbol_to_list (actual_name, strlen (actual_name),
5009 built_actual_name,
5010 VAR_DOMAIN, LOC_CONST,
5011 (cu->language == language_cplus
5012 || cu->language == language_java)
5013 ? &objfile->global_psymbols
5014 : &objfile->static_psymbols,
5015 0, (CORE_ADDR) 0, cu->language, objfile);
5016 break;
5017 default:
5018 break;
5019 }
5020
5021 if (built_actual_name)
5022 xfree (actual_name);
5023 }
5024
5025 /* Read a partial die corresponding to a namespace; also, add a symbol
5026 corresponding to that namespace to the symbol table. NAMESPACE is
5027 the name of the enclosing namespace. */
5028
5029 static void
5030 add_partial_namespace (struct partial_die_info *pdi,
5031 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5032 int need_pc, struct dwarf2_cu *cu)
5033 {
5034 /* Add a symbol for the namespace. */
5035
5036 add_partial_symbol (pdi, cu);
5037
5038 /* Now scan partial symbols in that namespace. */
5039
5040 if (pdi->has_children)
5041 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
5042 }
5043
5044 /* Read a partial die corresponding to a Fortran module. */
5045
5046 static void
5047 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
5048 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
5049 {
5050 /* Now scan partial symbols in that module. */
5051
5052 if (pdi->has_children)
5053 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
5054 }
5055
5056 /* Read a partial die corresponding to a subprogram and create a partial
5057 symbol for that subprogram. When the CU language allows it, this
5058 routine also defines a partial symbol for each nested subprogram
5059 that this subprogram contains.
5060
5061 DIE my also be a lexical block, in which case we simply search
5062 recursively for suprograms defined inside that lexical block.
5063 Again, this is only performed when the CU language allows this
5064 type of definitions. */
5065
5066 static void
5067 add_partial_subprogram (struct partial_die_info *pdi,
5068 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5069 int need_pc, struct dwarf2_cu *cu)
5070 {
5071 if (pdi->tag == DW_TAG_subprogram)
5072 {
5073 if (pdi->has_pc_info)
5074 {
5075 if (pdi->lowpc < *lowpc)
5076 *lowpc = pdi->lowpc;
5077 if (pdi->highpc > *highpc)
5078 *highpc = pdi->highpc;
5079 if (need_pc)
5080 {
5081 CORE_ADDR baseaddr;
5082 struct objfile *objfile = cu->objfile;
5083
5084 baseaddr = ANOFFSET (objfile->section_offsets,
5085 SECT_OFF_TEXT (objfile));
5086 addrmap_set_empty (objfile->psymtabs_addrmap,
5087 pdi->lowpc + baseaddr,
5088 pdi->highpc - 1 + baseaddr,
5089 cu->per_cu->v.psymtab);
5090 }
5091 }
5092
5093 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
5094 {
5095 if (!pdi->is_declaration)
5096 /* Ignore subprogram DIEs that do not have a name, they are
5097 illegal. Do not emit a complaint at this point, we will
5098 do so when we convert this psymtab into a symtab. */
5099 if (pdi->name)
5100 add_partial_symbol (pdi, cu);
5101 }
5102 }
5103
5104 if (! pdi->has_children)
5105 return;
5106
5107 if (cu->language == language_ada)
5108 {
5109 pdi = pdi->die_child;
5110 while (pdi != NULL)
5111 {
5112 fixup_partial_die (pdi, cu);
5113 if (pdi->tag == DW_TAG_subprogram
5114 || pdi->tag == DW_TAG_lexical_block)
5115 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
5116 pdi = pdi->die_sibling;
5117 }
5118 }
5119 }
5120
5121 /* Read a partial die corresponding to an enumeration type. */
5122
5123 static void
5124 add_partial_enumeration (struct partial_die_info *enum_pdi,
5125 struct dwarf2_cu *cu)
5126 {
5127 struct partial_die_info *pdi;
5128
5129 if (enum_pdi->name != NULL)
5130 add_partial_symbol (enum_pdi, cu);
5131
5132 pdi = enum_pdi->die_child;
5133 while (pdi)
5134 {
5135 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
5136 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
5137 else
5138 add_partial_symbol (pdi, cu);
5139 pdi = pdi->die_sibling;
5140 }
5141 }
5142
5143 /* Return the initial uleb128 in the die at INFO_PTR. */
5144
5145 static unsigned int
5146 peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
5147 {
5148 unsigned int bytes_read;
5149
5150 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5151 }
5152
5153 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
5154 Return the corresponding abbrev, or NULL if the number is zero (indicating
5155 an empty DIE). In either case *BYTES_READ will be set to the length of
5156 the initial number. */
5157
5158 static struct abbrev_info *
5159 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
5160 struct dwarf2_cu *cu)
5161 {
5162 bfd *abfd = cu->objfile->obfd;
5163 unsigned int abbrev_number;
5164 struct abbrev_info *abbrev;
5165
5166 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
5167
5168 if (abbrev_number == 0)
5169 return NULL;
5170
5171 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
5172 if (!abbrev)
5173 {
5174 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
5175 abbrev_number, bfd_get_filename (abfd));
5176 }
5177
5178 return abbrev;
5179 }
5180
5181 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
5182 Returns a pointer to the end of a series of DIEs, terminated by an empty
5183 DIE. Any children of the skipped DIEs will also be skipped. */
5184
5185 static gdb_byte *
5186 skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
5187 {
5188 struct dwarf2_cu *cu = reader->cu;
5189 struct abbrev_info *abbrev;
5190 unsigned int bytes_read;
5191
5192 while (1)
5193 {
5194 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
5195 if (abbrev == NULL)
5196 return info_ptr + bytes_read;
5197 else
5198 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
5199 }
5200 }
5201
5202 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
5203 INFO_PTR should point just after the initial uleb128 of a DIE, and the
5204 abbrev corresponding to that skipped uleb128 should be passed in
5205 ABBREV. Returns a pointer to this DIE's sibling, skipping any
5206 children. */
5207
5208 static gdb_byte *
5209 skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
5210 struct abbrev_info *abbrev)
5211 {
5212 unsigned int bytes_read;
5213 struct attribute attr;
5214 bfd *abfd = reader->abfd;
5215 struct dwarf2_cu *cu = reader->cu;
5216 gdb_byte *buffer = reader->buffer;
5217 const gdb_byte *buffer_end = reader->buffer_end;
5218 gdb_byte *start_info_ptr = info_ptr;
5219 unsigned int form, i;
5220
5221 for (i = 0; i < abbrev->num_attrs; i++)
5222 {
5223 /* The only abbrev we care about is DW_AT_sibling. */
5224 if (abbrev->attrs[i].name == DW_AT_sibling)
5225 {
5226 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
5227 if (attr.form == DW_FORM_ref_addr)
5228 complaint (&symfile_complaints,
5229 _("ignoring absolute DW_AT_sibling"));
5230 else
5231 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
5232 }
5233
5234 /* If it isn't DW_AT_sibling, skip this attribute. */
5235 form = abbrev->attrs[i].form;
5236 skip_attribute:
5237 switch (form)
5238 {
5239 case DW_FORM_ref_addr:
5240 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
5241 and later it is offset sized. */
5242 if (cu->header.version == 2)
5243 info_ptr += cu->header.addr_size;
5244 else
5245 info_ptr += cu->header.offset_size;
5246 break;
5247 case DW_FORM_addr:
5248 info_ptr += cu->header.addr_size;
5249 break;
5250 case DW_FORM_data1:
5251 case DW_FORM_ref1:
5252 case DW_FORM_flag:
5253 info_ptr += 1;
5254 break;
5255 case DW_FORM_flag_present:
5256 break;
5257 case DW_FORM_data2:
5258 case DW_FORM_ref2:
5259 info_ptr += 2;
5260 break;
5261 case DW_FORM_data4:
5262 case DW_FORM_ref4:
5263 info_ptr += 4;
5264 break;
5265 case DW_FORM_data8:
5266 case DW_FORM_ref8:
5267 case DW_FORM_ref_sig8:
5268 info_ptr += 8;
5269 break;
5270 case DW_FORM_string:
5271 read_direct_string (abfd, info_ptr, &bytes_read);
5272 info_ptr += bytes_read;
5273 break;
5274 case DW_FORM_sec_offset:
5275 case DW_FORM_strp:
5276 info_ptr += cu->header.offset_size;
5277 break;
5278 case DW_FORM_exprloc:
5279 case DW_FORM_block:
5280 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5281 info_ptr += bytes_read;
5282 break;
5283 case DW_FORM_block1:
5284 info_ptr += 1 + read_1_byte (abfd, info_ptr);
5285 break;
5286 case DW_FORM_block2:
5287 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
5288 break;
5289 case DW_FORM_block4:
5290 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
5291 break;
5292 case DW_FORM_sdata:
5293 case DW_FORM_udata:
5294 case DW_FORM_ref_udata:
5295 case DW_FORM_GNU_addr_index:
5296 case DW_FORM_GNU_str_index:
5297 info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
5298 break;
5299 case DW_FORM_indirect:
5300 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5301 info_ptr += bytes_read;
5302 /* We need to continue parsing from here, so just go back to
5303 the top. */
5304 goto skip_attribute;
5305
5306 default:
5307 error (_("Dwarf Error: Cannot handle %s "
5308 "in DWARF reader [in module %s]"),
5309 dwarf_form_name (form),
5310 bfd_get_filename (abfd));
5311 }
5312 }
5313
5314 if (abbrev->has_children)
5315 return skip_children (reader, info_ptr);
5316 else
5317 return info_ptr;
5318 }
5319
5320 /* Locate ORIG_PDI's sibling.
5321 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
5322
5323 static gdb_byte *
5324 locate_pdi_sibling (const struct die_reader_specs *reader,
5325 struct partial_die_info *orig_pdi,
5326 gdb_byte *info_ptr)
5327 {
5328 /* Do we know the sibling already? */
5329
5330 if (orig_pdi->sibling)
5331 return orig_pdi->sibling;
5332
5333 /* Are there any children to deal with? */
5334
5335 if (!orig_pdi->has_children)
5336 return info_ptr;
5337
5338 /* Skip the children the long way. */
5339
5340 return skip_children (reader, info_ptr);
5341 }
5342
5343 /* Expand this partial symbol table into a full symbol table. */
5344
5345 static void
5346 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
5347 {
5348 if (pst != NULL)
5349 {
5350 if (pst->readin)
5351 {
5352 warning (_("bug: psymtab for %s is already read in."),
5353 pst->filename);
5354 }
5355 else
5356 {
5357 if (info_verbose)
5358 {
5359 printf_filtered (_("Reading in symbols for %s..."),
5360 pst->filename);
5361 gdb_flush (gdb_stdout);
5362 }
5363
5364 /* Restore our global data. */
5365 dwarf2_per_objfile = objfile_data (pst->objfile,
5366 dwarf2_objfile_data_key);
5367
5368 /* If this psymtab is constructed from a debug-only objfile, the
5369 has_section_at_zero flag will not necessarily be correct. We
5370 can get the correct value for this flag by looking at the data
5371 associated with the (presumably stripped) associated objfile. */
5372 if (pst->objfile->separate_debug_objfile_backlink)
5373 {
5374 struct dwarf2_per_objfile *dpo_backlink
5375 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
5376 dwarf2_objfile_data_key);
5377
5378 dwarf2_per_objfile->has_section_at_zero
5379 = dpo_backlink->has_section_at_zero;
5380 }
5381
5382 dwarf2_per_objfile->reading_partial_symbols = 0;
5383
5384 psymtab_to_symtab_1 (pst);
5385
5386 /* Finish up the debug error message. */
5387 if (info_verbose)
5388 printf_filtered (_("done.\n"));
5389 }
5390 }
5391
5392 process_cu_includes ();
5393 }
5394 \f
5395 /* Reading in full CUs. */
5396
5397 /* Add PER_CU to the queue. */
5398
5399 static void
5400 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
5401 enum language pretend_language)
5402 {
5403 struct dwarf2_queue_item *item;
5404
5405 per_cu->queued = 1;
5406 item = xmalloc (sizeof (*item));
5407 item->per_cu = per_cu;
5408 item->pretend_language = pretend_language;
5409 item->next = NULL;
5410
5411 if (dwarf2_queue == NULL)
5412 dwarf2_queue = item;
5413 else
5414 dwarf2_queue_tail->next = item;
5415
5416 dwarf2_queue_tail = item;
5417 }
5418
5419 /* Process the queue. */
5420
5421 static void
5422 process_queue (void)
5423 {
5424 struct dwarf2_queue_item *item, *next_item;
5425
5426 if (dwarf2_read_debug)
5427 {
5428 fprintf_unfiltered (gdb_stdlog,
5429 "Expanding one or more symtabs of objfile %s ...\n",
5430 dwarf2_per_objfile->objfile->name);
5431 }
5432
5433 /* The queue starts out with one item, but following a DIE reference
5434 may load a new CU, adding it to the end of the queue. */
5435 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
5436 {
5437 if (dwarf2_per_objfile->using_index
5438 ? !item->per_cu->v.quick->symtab
5439 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
5440 process_full_comp_unit (item->per_cu, item->pretend_language);
5441
5442 item->per_cu->queued = 0;
5443 next_item = item->next;
5444 xfree (item);
5445 }
5446
5447 dwarf2_queue_tail = NULL;
5448
5449 if (dwarf2_read_debug)
5450 {
5451 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
5452 dwarf2_per_objfile->objfile->name);
5453 }
5454 }
5455
5456 /* Free all allocated queue entries. This function only releases anything if
5457 an error was thrown; if the queue was processed then it would have been
5458 freed as we went along. */
5459
5460 static void
5461 dwarf2_release_queue (void *dummy)
5462 {
5463 struct dwarf2_queue_item *item, *last;
5464
5465 item = dwarf2_queue;
5466 while (item)
5467 {
5468 /* Anything still marked queued is likely to be in an
5469 inconsistent state, so discard it. */
5470 if (item->per_cu->queued)
5471 {
5472 if (item->per_cu->cu != NULL)
5473 free_one_cached_comp_unit (item->per_cu);
5474 item->per_cu->queued = 0;
5475 }
5476
5477 last = item;
5478 item = item->next;
5479 xfree (last);
5480 }
5481
5482 dwarf2_queue = dwarf2_queue_tail = NULL;
5483 }
5484
5485 /* Read in full symbols for PST, and anything it depends on. */
5486
5487 static void
5488 psymtab_to_symtab_1 (struct partial_symtab *pst)
5489 {
5490 struct dwarf2_per_cu_data *per_cu;
5491 int i;
5492
5493 if (pst->readin)
5494 return;
5495
5496 for (i = 0; i < pst->number_of_dependencies; i++)
5497 if (!pst->dependencies[i]->readin
5498 && pst->dependencies[i]->user == NULL)
5499 {
5500 /* Inform about additional files that need to be read in. */
5501 if (info_verbose)
5502 {
5503 /* FIXME: i18n: Need to make this a single string. */
5504 fputs_filtered (" ", gdb_stdout);
5505 wrap_here ("");
5506 fputs_filtered ("and ", gdb_stdout);
5507 wrap_here ("");
5508 printf_filtered ("%s...", pst->dependencies[i]->filename);
5509 wrap_here (""); /* Flush output. */
5510 gdb_flush (gdb_stdout);
5511 }
5512 psymtab_to_symtab_1 (pst->dependencies[i]);
5513 }
5514
5515 per_cu = pst->read_symtab_private;
5516
5517 if (per_cu == NULL)
5518 {
5519 /* It's an include file, no symbols to read for it.
5520 Everything is in the parent symtab. */
5521 pst->readin = 1;
5522 return;
5523 }
5524
5525 dw2_do_instantiate_symtab (per_cu);
5526 }
5527
5528 /* Trivial hash function for die_info: the hash value of a DIE
5529 is its offset in .debug_info for this objfile. */
5530
5531 static hashval_t
5532 die_hash (const void *item)
5533 {
5534 const struct die_info *die = item;
5535
5536 return die->offset.sect_off;
5537 }
5538
5539 /* Trivial comparison function for die_info structures: two DIEs
5540 are equal if they have the same offset. */
5541
5542 static int
5543 die_eq (const void *item_lhs, const void *item_rhs)
5544 {
5545 const struct die_info *die_lhs = item_lhs;
5546 const struct die_info *die_rhs = item_rhs;
5547
5548 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
5549 }
5550
5551 /* die_reader_func for load_full_comp_unit.
5552 This is identical to read_signatured_type_reader,
5553 but is kept separate for now. */
5554
5555 static void
5556 load_full_comp_unit_reader (const struct die_reader_specs *reader,
5557 gdb_byte *info_ptr,
5558 struct die_info *comp_unit_die,
5559 int has_children,
5560 void *data)
5561 {
5562 struct dwarf2_cu *cu = reader->cu;
5563 enum language *language_ptr = data;
5564
5565 gdb_assert (cu->die_hash == NULL);
5566 cu->die_hash =
5567 htab_create_alloc_ex (cu->header.length / 12,
5568 die_hash,
5569 die_eq,
5570 NULL,
5571 &cu->comp_unit_obstack,
5572 hashtab_obstack_allocate,
5573 dummy_obstack_deallocate);
5574
5575 if (has_children)
5576 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
5577 &info_ptr, comp_unit_die);
5578 cu->dies = comp_unit_die;
5579 /* comp_unit_die is not stored in die_hash, no need. */
5580
5581 /* We try not to read any attributes in this function, because not
5582 all CUs needed for references have been loaded yet, and symbol
5583 table processing isn't initialized. But we have to set the CU language,
5584 or we won't be able to build types correctly.
5585 Similarly, if we do not read the producer, we can not apply
5586 producer-specific interpretation. */
5587 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
5588 }
5589
5590 /* Load the DIEs associated with PER_CU into memory. */
5591
5592 static void
5593 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
5594 enum language pretend_language)
5595 {
5596 gdb_assert (! this_cu->is_debug_types);
5597
5598 init_cutu_and_read_dies (this_cu, 1, 1, load_full_comp_unit_reader,
5599 &pretend_language);
5600 }
5601
5602 /* Add a DIE to the delayed physname list. */
5603
5604 static void
5605 add_to_method_list (struct type *type, int fnfield_index, int index,
5606 const char *name, struct die_info *die,
5607 struct dwarf2_cu *cu)
5608 {
5609 struct delayed_method_info mi;
5610 mi.type = type;
5611 mi.fnfield_index = fnfield_index;
5612 mi.index = index;
5613 mi.name = name;
5614 mi.die = die;
5615 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
5616 }
5617
5618 /* A cleanup for freeing the delayed method list. */
5619
5620 static void
5621 free_delayed_list (void *ptr)
5622 {
5623 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
5624 if (cu->method_list != NULL)
5625 {
5626 VEC_free (delayed_method_info, cu->method_list);
5627 cu->method_list = NULL;
5628 }
5629 }
5630
5631 /* Compute the physnames of any methods on the CU's method list.
5632
5633 The computation of method physnames is delayed in order to avoid the
5634 (bad) condition that one of the method's formal parameters is of an as yet
5635 incomplete type. */
5636
5637 static void
5638 compute_delayed_physnames (struct dwarf2_cu *cu)
5639 {
5640 int i;
5641 struct delayed_method_info *mi;
5642 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
5643 {
5644 const char *physname;
5645 struct fn_fieldlist *fn_flp
5646 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
5647 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
5648 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
5649 }
5650 }
5651
5652 /* Go objects should be embedded in a DW_TAG_module DIE,
5653 and it's not clear if/how imported objects will appear.
5654 To keep Go support simple until that's worked out,
5655 go back through what we've read and create something usable.
5656 We could do this while processing each DIE, and feels kinda cleaner,
5657 but that way is more invasive.
5658 This is to, for example, allow the user to type "p var" or "b main"
5659 without having to specify the package name, and allow lookups
5660 of module.object to work in contexts that use the expression
5661 parser. */
5662
5663 static void
5664 fixup_go_packaging (struct dwarf2_cu *cu)
5665 {
5666 char *package_name = NULL;
5667 struct pending *list;
5668 int i;
5669
5670 for (list = global_symbols; list != NULL; list = list->next)
5671 {
5672 for (i = 0; i < list->nsyms; ++i)
5673 {
5674 struct symbol *sym = list->symbol[i];
5675
5676 if (SYMBOL_LANGUAGE (sym) == language_go
5677 && SYMBOL_CLASS (sym) == LOC_BLOCK)
5678 {
5679 char *this_package_name = go_symbol_package_name (sym);
5680
5681 if (this_package_name == NULL)
5682 continue;
5683 if (package_name == NULL)
5684 package_name = this_package_name;
5685 else
5686 {
5687 if (strcmp (package_name, this_package_name) != 0)
5688 complaint (&symfile_complaints,
5689 _("Symtab %s has objects from two different Go packages: %s and %s"),
5690 (sym->symtab && sym->symtab->filename
5691 ? sym->symtab->filename
5692 : cu->objfile->name),
5693 this_package_name, package_name);
5694 xfree (this_package_name);
5695 }
5696 }
5697 }
5698 }
5699
5700 if (package_name != NULL)
5701 {
5702 struct objfile *objfile = cu->objfile;
5703 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
5704 package_name, objfile);
5705 struct symbol *sym;
5706
5707 TYPE_TAG_NAME (type) = TYPE_NAME (type);
5708
5709 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
5710 SYMBOL_SET_LANGUAGE (sym, language_go);
5711 SYMBOL_SET_NAMES (sym, package_name, strlen (package_name), 1, objfile);
5712 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
5713 e.g., "main" finds the "main" module and not C's main(). */
5714 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
5715 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
5716 SYMBOL_TYPE (sym) = type;
5717
5718 add_symbol_to_list (sym, &global_symbols);
5719
5720 xfree (package_name);
5721 }
5722 }
5723
5724 static void compute_symtab_includes (struct dwarf2_per_cu_data *per_cu);
5725
5726 /* Return the symtab for PER_CU. This works properly regardless of
5727 whether we're using the index or psymtabs. */
5728
5729 static struct symtab *
5730 get_symtab (struct dwarf2_per_cu_data *per_cu)
5731 {
5732 return (dwarf2_per_objfile->using_index
5733 ? per_cu->v.quick->symtab
5734 : per_cu->v.psymtab->symtab);
5735 }
5736
5737 /* A helper function for computing the list of all symbol tables
5738 included by PER_CU. */
5739
5740 static void
5741 recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
5742 htab_t all_children,
5743 struct dwarf2_per_cu_data *per_cu)
5744 {
5745 void **slot;
5746 int ix;
5747 struct dwarf2_per_cu_data *iter;
5748
5749 slot = htab_find_slot (all_children, per_cu, INSERT);
5750 if (*slot != NULL)
5751 {
5752 /* This inclusion and its children have been processed. */
5753 return;
5754 }
5755
5756 *slot = per_cu;
5757 /* Only add a CU if it has a symbol table. */
5758 if (get_symtab (per_cu) != NULL)
5759 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
5760
5761 for (ix = 0;
5762 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
5763 ++ix)
5764 recursively_compute_inclusions (result, all_children, iter);
5765 }
5766
5767 /* Compute the symtab 'includes' fields for the symtab related to
5768 PER_CU. */
5769
5770 static void
5771 compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
5772 {
5773 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
5774 {
5775 int ix, len;
5776 struct dwarf2_per_cu_data *iter;
5777 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
5778 htab_t all_children;
5779 struct symtab *symtab = get_symtab (per_cu);
5780
5781 /* If we don't have a symtab, we can just skip this case. */
5782 if (symtab == NULL)
5783 return;
5784
5785 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
5786 NULL, xcalloc, xfree);
5787
5788 for (ix = 0;
5789 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
5790 ix, iter);
5791 ++ix)
5792 recursively_compute_inclusions (&result_children, all_children, iter);
5793
5794 /* Now we have a transitive closure of all the included CUs, so
5795 we can convert it to a list of symtabs. */
5796 len = VEC_length (dwarf2_per_cu_ptr, result_children);
5797 symtab->includes
5798 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
5799 (len + 1) * sizeof (struct symtab *));
5800 for (ix = 0;
5801 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
5802 ++ix)
5803 symtab->includes[ix] = get_symtab (iter);
5804 symtab->includes[len] = NULL;
5805
5806 VEC_free (dwarf2_per_cu_ptr, result_children);
5807 htab_delete (all_children);
5808 }
5809 }
5810
5811 /* Compute the 'includes' field for the symtabs of all the CUs we just
5812 read. */
5813
5814 static void
5815 process_cu_includes (void)
5816 {
5817 int ix;
5818 struct dwarf2_per_cu_data *iter;
5819
5820 for (ix = 0;
5821 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
5822 ix, iter);
5823 ++ix)
5824 compute_symtab_includes (iter);
5825
5826 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
5827 }
5828
5829 /* Generate full symbol information for PER_CU, whose DIEs have
5830 already been loaded into memory. */
5831
5832 static void
5833 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
5834 enum language pretend_language)
5835 {
5836 struct dwarf2_cu *cu = per_cu->cu;
5837 struct objfile *objfile = per_cu->objfile;
5838 CORE_ADDR lowpc, highpc;
5839 struct symtab *symtab;
5840 struct cleanup *back_to, *delayed_list_cleanup;
5841 CORE_ADDR baseaddr;
5842
5843 if (dwarf2_read_debug)
5844 {
5845 fprintf_unfiltered (gdb_stdlog,
5846 "Expanding symtab of %s at offset 0x%x\n",
5847 per_cu->is_debug_types ? "TU" : "CU",
5848 per_cu->offset.sect_off);
5849 }
5850
5851 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5852
5853 buildsym_init ();
5854 back_to = make_cleanup (really_free_pendings, NULL);
5855 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
5856
5857 cu->list_in_scope = &file_symbols;
5858
5859 cu->language = pretend_language;
5860 cu->language_defn = language_def (cu->language);
5861
5862 /* Do line number decoding in read_file_scope () */
5863 process_die (cu->dies, cu);
5864
5865 /* For now fudge the Go package. */
5866 if (cu->language == language_go)
5867 fixup_go_packaging (cu);
5868
5869 /* Now that we have processed all the DIEs in the CU, all the types
5870 should be complete, and it should now be safe to compute all of the
5871 physnames. */
5872 compute_delayed_physnames (cu);
5873 do_cleanups (delayed_list_cleanup);
5874
5875 /* Some compilers don't define a DW_AT_high_pc attribute for the
5876 compilation unit. If the DW_AT_high_pc is missing, synthesize
5877 it, by scanning the DIE's below the compilation unit. */
5878 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
5879
5880 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
5881
5882 if (symtab != NULL)
5883 {
5884 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
5885
5886 /* Set symtab language to language from DW_AT_language. If the
5887 compilation is from a C file generated by language preprocessors, do
5888 not set the language if it was already deduced by start_subfile. */
5889 if (!(cu->language == language_c && symtab->language != language_c))
5890 symtab->language = cu->language;
5891
5892 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
5893 produce DW_AT_location with location lists but it can be possibly
5894 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
5895 there were bugs in prologue debug info, fixed later in GCC-4.5
5896 by "unwind info for epilogues" patch (which is not directly related).
5897
5898 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
5899 needed, it would be wrong due to missing DW_AT_producer there.
5900
5901 Still one can confuse GDB by using non-standard GCC compilation
5902 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5903 */
5904 if (cu->has_loclist && gcc_4_minor >= 5)
5905 symtab->locations_valid = 1;
5906
5907 if (gcc_4_minor >= 5)
5908 symtab->epilogue_unwind_valid = 1;
5909
5910 symtab->call_site_htab = cu->call_site_htab;
5911 }
5912
5913 if (dwarf2_per_objfile->using_index)
5914 per_cu->v.quick->symtab = symtab;
5915 else
5916 {
5917 struct partial_symtab *pst = per_cu->v.psymtab;
5918 pst->symtab = symtab;
5919 pst->readin = 1;
5920 }
5921
5922 /* Push it for inclusion processing later. */
5923 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
5924
5925 do_cleanups (back_to);
5926
5927 if (dwarf2_read_debug)
5928 {
5929 fprintf_unfiltered (gdb_stdlog,
5930 "Done expanding symtab of %s at offset 0x%x\n",
5931 per_cu->is_debug_types ? "TU" : "CU",
5932 per_cu->offset.sect_off);
5933 }
5934 }
5935
5936 /* Process an imported unit DIE. */
5937
5938 static void
5939 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
5940 {
5941 struct attribute *attr;
5942
5943 attr = dwarf2_attr (die, DW_AT_import, cu);
5944 if (attr != NULL)
5945 {
5946 struct dwarf2_per_cu_data *per_cu;
5947 struct symtab *imported_symtab;
5948 sect_offset offset;
5949
5950 offset = dwarf2_get_ref_die_offset (attr);
5951 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
5952
5953 /* Queue the unit, if needed. */
5954 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
5955 load_full_comp_unit (per_cu, cu->language);
5956
5957 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
5958 per_cu);
5959 }
5960 }
5961
5962 /* Process a die and its children. */
5963
5964 static void
5965 process_die (struct die_info *die, struct dwarf2_cu *cu)
5966 {
5967 switch (die->tag)
5968 {
5969 case DW_TAG_padding:
5970 break;
5971 case DW_TAG_compile_unit:
5972 case DW_TAG_partial_unit:
5973 read_file_scope (die, cu);
5974 break;
5975 case DW_TAG_type_unit:
5976 read_type_unit_scope (die, cu);
5977 break;
5978 case DW_TAG_subprogram:
5979 case DW_TAG_inlined_subroutine:
5980 read_func_scope (die, cu);
5981 break;
5982 case DW_TAG_lexical_block:
5983 case DW_TAG_try_block:
5984 case DW_TAG_catch_block:
5985 read_lexical_block_scope (die, cu);
5986 break;
5987 case DW_TAG_GNU_call_site:
5988 read_call_site_scope (die, cu);
5989 break;
5990 case DW_TAG_class_type:
5991 case DW_TAG_interface_type:
5992 case DW_TAG_structure_type:
5993 case DW_TAG_union_type:
5994 process_structure_scope (die, cu);
5995 break;
5996 case DW_TAG_enumeration_type:
5997 process_enumeration_scope (die, cu);
5998 break;
5999
6000 /* These dies have a type, but processing them does not create
6001 a symbol or recurse to process the children. Therefore we can
6002 read them on-demand through read_type_die. */
6003 case DW_TAG_subroutine_type:
6004 case DW_TAG_set_type:
6005 case DW_TAG_array_type:
6006 case DW_TAG_pointer_type:
6007 case DW_TAG_ptr_to_member_type:
6008 case DW_TAG_reference_type:
6009 case DW_TAG_string_type:
6010 break;
6011
6012 case DW_TAG_base_type:
6013 case DW_TAG_subrange_type:
6014 case DW_TAG_typedef:
6015 /* Add a typedef symbol for the type definition, if it has a
6016 DW_AT_name. */
6017 new_symbol (die, read_type_die (die, cu), cu);
6018 break;
6019 case DW_TAG_common_block:
6020 read_common_block (die, cu);
6021 break;
6022 case DW_TAG_common_inclusion:
6023 break;
6024 case DW_TAG_namespace:
6025 processing_has_namespace_info = 1;
6026 read_namespace (die, cu);
6027 break;
6028 case DW_TAG_module:
6029 processing_has_namespace_info = 1;
6030 read_module (die, cu);
6031 break;
6032 case DW_TAG_imported_declaration:
6033 case DW_TAG_imported_module:
6034 processing_has_namespace_info = 1;
6035 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
6036 || cu->language != language_fortran))
6037 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
6038 dwarf_tag_name (die->tag));
6039 read_import_statement (die, cu);
6040 break;
6041
6042 case DW_TAG_imported_unit:
6043 process_imported_unit_die (die, cu);
6044 break;
6045
6046 default:
6047 new_symbol (die, NULL, cu);
6048 break;
6049 }
6050 }
6051
6052 /* A helper function for dwarf2_compute_name which determines whether DIE
6053 needs to have the name of the scope prepended to the name listed in the
6054 die. */
6055
6056 static int
6057 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
6058 {
6059 struct attribute *attr;
6060
6061 switch (die->tag)
6062 {
6063 case DW_TAG_namespace:
6064 case DW_TAG_typedef:
6065 case DW_TAG_class_type:
6066 case DW_TAG_interface_type:
6067 case DW_TAG_structure_type:
6068 case DW_TAG_union_type:
6069 case DW_TAG_enumeration_type:
6070 case DW_TAG_enumerator:
6071 case DW_TAG_subprogram:
6072 case DW_TAG_member:
6073 return 1;
6074
6075 case DW_TAG_variable:
6076 case DW_TAG_constant:
6077 /* We only need to prefix "globally" visible variables. These include
6078 any variable marked with DW_AT_external or any variable that
6079 lives in a namespace. [Variables in anonymous namespaces
6080 require prefixing, but they are not DW_AT_external.] */
6081
6082 if (dwarf2_attr (die, DW_AT_specification, cu))
6083 {
6084 struct dwarf2_cu *spec_cu = cu;
6085
6086 return die_needs_namespace (die_specification (die, &spec_cu),
6087 spec_cu);
6088 }
6089
6090 attr = dwarf2_attr (die, DW_AT_external, cu);
6091 if (attr == NULL && die->parent->tag != DW_TAG_namespace
6092 && die->parent->tag != DW_TAG_module)
6093 return 0;
6094 /* A variable in a lexical block of some kind does not need a
6095 namespace, even though in C++ such variables may be external
6096 and have a mangled name. */
6097 if (die->parent->tag == DW_TAG_lexical_block
6098 || die->parent->tag == DW_TAG_try_block
6099 || die->parent->tag == DW_TAG_catch_block
6100 || die->parent->tag == DW_TAG_subprogram)
6101 return 0;
6102 return 1;
6103
6104 default:
6105 return 0;
6106 }
6107 }
6108
6109 /* Retrieve the last character from a mem_file. */
6110
6111 static void
6112 do_ui_file_peek_last (void *object, const char *buffer, long length)
6113 {
6114 char *last_char_p = (char *) object;
6115
6116 if (length > 0)
6117 *last_char_p = buffer[length - 1];
6118 }
6119
6120 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
6121 compute the physname for the object, which include a method's:
6122 - formal parameters (C++/Java),
6123 - receiver type (Go),
6124 - return type (Java).
6125
6126 The term "physname" is a bit confusing.
6127 For C++, for example, it is the demangled name.
6128 For Go, for example, it's the mangled name.
6129
6130 For Ada, return the DIE's linkage name rather than the fully qualified
6131 name. PHYSNAME is ignored..
6132
6133 The result is allocated on the objfile_obstack and canonicalized. */
6134
6135 static const char *
6136 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
6137 int physname)
6138 {
6139 struct objfile *objfile = cu->objfile;
6140
6141 if (name == NULL)
6142 name = dwarf2_name (die, cu);
6143
6144 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
6145 compute it by typename_concat inside GDB. */
6146 if (cu->language == language_ada
6147 || (cu->language == language_fortran && physname))
6148 {
6149 /* For Ada unit, we prefer the linkage name over the name, as
6150 the former contains the exported name, which the user expects
6151 to be able to reference. Ideally, we want the user to be able
6152 to reference this entity using either natural or linkage name,
6153 but we haven't started looking at this enhancement yet. */
6154 struct attribute *attr;
6155
6156 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
6157 if (attr == NULL)
6158 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
6159 if (attr && DW_STRING (attr))
6160 return DW_STRING (attr);
6161 }
6162
6163 /* These are the only languages we know how to qualify names in. */
6164 if (name != NULL
6165 && (cu->language == language_cplus || cu->language == language_java
6166 || cu->language == language_fortran))
6167 {
6168 if (die_needs_namespace (die, cu))
6169 {
6170 long length;
6171 const char *prefix;
6172 struct ui_file *buf;
6173
6174 prefix = determine_prefix (die, cu);
6175 buf = mem_fileopen ();
6176 if (*prefix != '\0')
6177 {
6178 char *prefixed_name = typename_concat (NULL, prefix, name,
6179 physname, cu);
6180
6181 fputs_unfiltered (prefixed_name, buf);
6182 xfree (prefixed_name);
6183 }
6184 else
6185 fputs_unfiltered (name, buf);
6186
6187 /* Template parameters may be specified in the DIE's DW_AT_name, or
6188 as children with DW_TAG_template_type_param or
6189 DW_TAG_value_type_param. If the latter, add them to the name
6190 here. If the name already has template parameters, then
6191 skip this step; some versions of GCC emit both, and
6192 it is more efficient to use the pre-computed name.
6193
6194 Something to keep in mind about this process: it is very
6195 unlikely, or in some cases downright impossible, to produce
6196 something that will match the mangled name of a function.
6197 If the definition of the function has the same debug info,
6198 we should be able to match up with it anyway. But fallbacks
6199 using the minimal symbol, for instance to find a method
6200 implemented in a stripped copy of libstdc++, will not work.
6201 If we do not have debug info for the definition, we will have to
6202 match them up some other way.
6203
6204 When we do name matching there is a related problem with function
6205 templates; two instantiated function templates are allowed to
6206 differ only by their return types, which we do not add here. */
6207
6208 if (cu->language == language_cplus && strchr (name, '<') == NULL)
6209 {
6210 struct attribute *attr;
6211 struct die_info *child;
6212 int first = 1;
6213
6214 die->building_fullname = 1;
6215
6216 for (child = die->child; child != NULL; child = child->sibling)
6217 {
6218 struct type *type;
6219 LONGEST value;
6220 gdb_byte *bytes;
6221 struct dwarf2_locexpr_baton *baton;
6222 struct value *v;
6223
6224 if (child->tag != DW_TAG_template_type_param
6225 && child->tag != DW_TAG_template_value_param)
6226 continue;
6227
6228 if (first)
6229 {
6230 fputs_unfiltered ("<", buf);
6231 first = 0;
6232 }
6233 else
6234 fputs_unfiltered (", ", buf);
6235
6236 attr = dwarf2_attr (child, DW_AT_type, cu);
6237 if (attr == NULL)
6238 {
6239 complaint (&symfile_complaints,
6240 _("template parameter missing DW_AT_type"));
6241 fputs_unfiltered ("UNKNOWN_TYPE", buf);
6242 continue;
6243 }
6244 type = die_type (child, cu);
6245
6246 if (child->tag == DW_TAG_template_type_param)
6247 {
6248 c_print_type (type, "", buf, -1, 0);
6249 continue;
6250 }
6251
6252 attr = dwarf2_attr (child, DW_AT_const_value, cu);
6253 if (attr == NULL)
6254 {
6255 complaint (&symfile_complaints,
6256 _("template parameter missing "
6257 "DW_AT_const_value"));
6258 fputs_unfiltered ("UNKNOWN_VALUE", buf);
6259 continue;
6260 }
6261
6262 dwarf2_const_value_attr (attr, type, name,
6263 &cu->comp_unit_obstack, cu,
6264 &value, &bytes, &baton);
6265
6266 if (TYPE_NOSIGN (type))
6267 /* GDB prints characters as NUMBER 'CHAR'. If that's
6268 changed, this can use value_print instead. */
6269 c_printchar (value, type, buf);
6270 else
6271 {
6272 struct value_print_options opts;
6273
6274 if (baton != NULL)
6275 v = dwarf2_evaluate_loc_desc (type, NULL,
6276 baton->data,
6277 baton->size,
6278 baton->per_cu);
6279 else if (bytes != NULL)
6280 {
6281 v = allocate_value (type);
6282 memcpy (value_contents_writeable (v), bytes,
6283 TYPE_LENGTH (type));
6284 }
6285 else
6286 v = value_from_longest (type, value);
6287
6288 /* Specify decimal so that we do not depend on
6289 the radix. */
6290 get_formatted_print_options (&opts, 'd');
6291 opts.raw = 1;
6292 value_print (v, buf, &opts);
6293 release_value (v);
6294 value_free (v);
6295 }
6296 }
6297
6298 die->building_fullname = 0;
6299
6300 if (!first)
6301 {
6302 /* Close the argument list, with a space if necessary
6303 (nested templates). */
6304 char last_char = '\0';
6305 ui_file_put (buf, do_ui_file_peek_last, &last_char);
6306 if (last_char == '>')
6307 fputs_unfiltered (" >", buf);
6308 else
6309 fputs_unfiltered (">", buf);
6310 }
6311 }
6312
6313 /* For Java and C++ methods, append formal parameter type
6314 information, if PHYSNAME. */
6315
6316 if (physname && die->tag == DW_TAG_subprogram
6317 && (cu->language == language_cplus
6318 || cu->language == language_java))
6319 {
6320 struct type *type = read_type_die (die, cu);
6321
6322 c_type_print_args (type, buf, 1, cu->language);
6323
6324 if (cu->language == language_java)
6325 {
6326 /* For java, we must append the return type to method
6327 names. */
6328 if (die->tag == DW_TAG_subprogram)
6329 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
6330 0, 0);
6331 }
6332 else if (cu->language == language_cplus)
6333 {
6334 /* Assume that an artificial first parameter is
6335 "this", but do not crash if it is not. RealView
6336 marks unnamed (and thus unused) parameters as
6337 artificial; there is no way to differentiate
6338 the two cases. */
6339 if (TYPE_NFIELDS (type) > 0
6340 && TYPE_FIELD_ARTIFICIAL (type, 0)
6341 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
6342 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
6343 0))))
6344 fputs_unfiltered (" const", buf);
6345 }
6346 }
6347
6348 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
6349 &length);
6350 ui_file_delete (buf);
6351
6352 if (cu->language == language_cplus)
6353 {
6354 char *cname
6355 = dwarf2_canonicalize_name (name, cu,
6356 &objfile->objfile_obstack);
6357
6358 if (cname != NULL)
6359 name = cname;
6360 }
6361 }
6362 }
6363
6364 return name;
6365 }
6366
6367 /* Return the fully qualified name of DIE, based on its DW_AT_name.
6368 If scope qualifiers are appropriate they will be added. The result
6369 will be allocated on the objfile_obstack, or NULL if the DIE does
6370 not have a name. NAME may either be from a previous call to
6371 dwarf2_name or NULL.
6372
6373 The output string will be canonicalized (if C++/Java). */
6374
6375 static const char *
6376 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
6377 {
6378 return dwarf2_compute_name (name, die, cu, 0);
6379 }
6380
6381 /* Construct a physname for the given DIE in CU. NAME may either be
6382 from a previous call to dwarf2_name or NULL. The result will be
6383 allocated on the objfile_objstack or NULL if the DIE does not have a
6384 name.
6385
6386 The output string will be canonicalized (if C++/Java). */
6387
6388 static const char *
6389 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
6390 {
6391 struct objfile *objfile = cu->objfile;
6392 struct attribute *attr;
6393 const char *retval, *mangled = NULL, *canon = NULL;
6394 struct cleanup *back_to;
6395 int need_copy = 1;
6396
6397 /* In this case dwarf2_compute_name is just a shortcut not building anything
6398 on its own. */
6399 if (!die_needs_namespace (die, cu))
6400 return dwarf2_compute_name (name, die, cu, 1);
6401
6402 back_to = make_cleanup (null_cleanup, NULL);
6403
6404 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
6405 if (!attr)
6406 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
6407
6408 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
6409 has computed. */
6410 if (attr && DW_STRING (attr))
6411 {
6412 char *demangled;
6413
6414 mangled = DW_STRING (attr);
6415
6416 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
6417 type. It is easier for GDB users to search for such functions as
6418 `name(params)' than `long name(params)'. In such case the minimal
6419 symbol names do not match the full symbol names but for template
6420 functions there is never a need to look up their definition from their
6421 declaration so the only disadvantage remains the minimal symbol
6422 variant `long name(params)' does not have the proper inferior type.
6423 */
6424
6425 if (cu->language == language_go)
6426 {
6427 /* This is a lie, but we already lie to the caller new_symbol_full.
6428 new_symbol_full assumes we return the mangled name.
6429 This just undoes that lie until things are cleaned up. */
6430 demangled = NULL;
6431 }
6432 else
6433 {
6434 demangled = cplus_demangle (mangled,
6435 (DMGL_PARAMS | DMGL_ANSI
6436 | (cu->language == language_java
6437 ? DMGL_JAVA | DMGL_RET_POSTFIX
6438 : DMGL_RET_DROP)));
6439 }
6440 if (demangled)
6441 {
6442 make_cleanup (xfree, demangled);
6443 canon = demangled;
6444 }
6445 else
6446 {
6447 canon = mangled;
6448 need_copy = 0;
6449 }
6450 }
6451
6452 if (canon == NULL || check_physname)
6453 {
6454 const char *physname = dwarf2_compute_name (name, die, cu, 1);
6455
6456 if (canon != NULL && strcmp (physname, canon) != 0)
6457 {
6458 /* It may not mean a bug in GDB. The compiler could also
6459 compute DW_AT_linkage_name incorrectly. But in such case
6460 GDB would need to be bug-to-bug compatible. */
6461
6462 complaint (&symfile_complaints,
6463 _("Computed physname <%s> does not match demangled <%s> "
6464 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
6465 physname, canon, mangled, die->offset.sect_off, objfile->name);
6466
6467 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
6468 is available here - over computed PHYSNAME. It is safer
6469 against both buggy GDB and buggy compilers. */
6470
6471 retval = canon;
6472 }
6473 else
6474 {
6475 retval = physname;
6476 need_copy = 0;
6477 }
6478 }
6479 else
6480 retval = canon;
6481
6482 if (need_copy)
6483 retval = obsavestring (retval, strlen (retval),
6484 &objfile->objfile_obstack);
6485
6486 do_cleanups (back_to);
6487 return retval;
6488 }
6489
6490 /* Read the import statement specified by the given die and record it. */
6491
6492 static void
6493 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
6494 {
6495 struct objfile *objfile = cu->objfile;
6496 struct attribute *import_attr;
6497 struct die_info *imported_die, *child_die;
6498 struct dwarf2_cu *imported_cu;
6499 const char *imported_name;
6500 const char *imported_name_prefix;
6501 const char *canonical_name;
6502 const char *import_alias;
6503 const char *imported_declaration = NULL;
6504 const char *import_prefix;
6505 VEC (const_char_ptr) *excludes = NULL;
6506 struct cleanup *cleanups;
6507
6508 char *temp;
6509
6510 import_attr = dwarf2_attr (die, DW_AT_import, cu);
6511 if (import_attr == NULL)
6512 {
6513 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
6514 dwarf_tag_name (die->tag));
6515 return;
6516 }
6517
6518 imported_cu = cu;
6519 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
6520 imported_name = dwarf2_name (imported_die, imported_cu);
6521 if (imported_name == NULL)
6522 {
6523 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
6524
6525 The import in the following code:
6526 namespace A
6527 {
6528 typedef int B;
6529 }
6530
6531 int main ()
6532 {
6533 using A::B;
6534 B b;
6535 return b;
6536 }
6537
6538 ...
6539 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
6540 <52> DW_AT_decl_file : 1
6541 <53> DW_AT_decl_line : 6
6542 <54> DW_AT_import : <0x75>
6543 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
6544 <59> DW_AT_name : B
6545 <5b> DW_AT_decl_file : 1
6546 <5c> DW_AT_decl_line : 2
6547 <5d> DW_AT_type : <0x6e>
6548 ...
6549 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
6550 <76> DW_AT_byte_size : 4
6551 <77> DW_AT_encoding : 5 (signed)
6552
6553 imports the wrong die ( 0x75 instead of 0x58 ).
6554 This case will be ignored until the gcc bug is fixed. */
6555 return;
6556 }
6557
6558 /* Figure out the local name after import. */
6559 import_alias = dwarf2_name (die, cu);
6560
6561 /* Figure out where the statement is being imported to. */
6562 import_prefix = determine_prefix (die, cu);
6563
6564 /* Figure out what the scope of the imported die is and prepend it
6565 to the name of the imported die. */
6566 imported_name_prefix = determine_prefix (imported_die, imported_cu);
6567
6568 if (imported_die->tag != DW_TAG_namespace
6569 && imported_die->tag != DW_TAG_module)
6570 {
6571 imported_declaration = imported_name;
6572 canonical_name = imported_name_prefix;
6573 }
6574 else if (strlen (imported_name_prefix) > 0)
6575 {
6576 temp = alloca (strlen (imported_name_prefix)
6577 + 2 + strlen (imported_name) + 1);
6578 strcpy (temp, imported_name_prefix);
6579 strcat (temp, "::");
6580 strcat (temp, imported_name);
6581 canonical_name = temp;
6582 }
6583 else
6584 canonical_name = imported_name;
6585
6586 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
6587
6588 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
6589 for (child_die = die->child; child_die && child_die->tag;
6590 child_die = sibling_die (child_die))
6591 {
6592 /* DWARF-4: A Fortran use statement with a “rename list” may be
6593 represented by an imported module entry with an import attribute
6594 referring to the module and owned entries corresponding to those
6595 entities that are renamed as part of being imported. */
6596
6597 if (child_die->tag != DW_TAG_imported_declaration)
6598 {
6599 complaint (&symfile_complaints,
6600 _("child DW_TAG_imported_declaration expected "
6601 "- DIE at 0x%x [in module %s]"),
6602 child_die->offset.sect_off, objfile->name);
6603 continue;
6604 }
6605
6606 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
6607 if (import_attr == NULL)
6608 {
6609 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
6610 dwarf_tag_name (child_die->tag));
6611 continue;
6612 }
6613
6614 imported_cu = cu;
6615 imported_die = follow_die_ref_or_sig (child_die, import_attr,
6616 &imported_cu);
6617 imported_name = dwarf2_name (imported_die, imported_cu);
6618 if (imported_name == NULL)
6619 {
6620 complaint (&symfile_complaints,
6621 _("child DW_TAG_imported_declaration has unknown "
6622 "imported name - DIE at 0x%x [in module %s]"),
6623 child_die->offset.sect_off, objfile->name);
6624 continue;
6625 }
6626
6627 VEC_safe_push (const_char_ptr, excludes, imported_name);
6628
6629 process_die (child_die, cu);
6630 }
6631
6632 cp_add_using_directive (import_prefix,
6633 canonical_name,
6634 import_alias,
6635 imported_declaration,
6636 excludes,
6637 &objfile->objfile_obstack);
6638
6639 do_cleanups (cleanups);
6640 }
6641
6642 /* Cleanup function for read_file_scope. */
6643
6644 static void
6645 free_cu_line_header (void *arg)
6646 {
6647 struct dwarf2_cu *cu = arg;
6648
6649 free_line_header (cu->line_header);
6650 cu->line_header = NULL;
6651 }
6652
6653 static void
6654 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
6655 char **name, char **comp_dir)
6656 {
6657 struct attribute *attr;
6658
6659 *name = NULL;
6660 *comp_dir = NULL;
6661
6662 /* Find the filename. Do not use dwarf2_name here, since the filename
6663 is not a source language identifier. */
6664 attr = dwarf2_attr (die, DW_AT_name, cu);
6665 if (attr)
6666 {
6667 *name = DW_STRING (attr);
6668 }
6669
6670 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
6671 if (attr)
6672 *comp_dir = DW_STRING (attr);
6673 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
6674 {
6675 *comp_dir = ldirname (*name);
6676 if (*comp_dir != NULL)
6677 make_cleanup (xfree, *comp_dir);
6678 }
6679 if (*comp_dir != NULL)
6680 {
6681 /* Irix 6.2 native cc prepends <machine>.: to the compilation
6682 directory, get rid of it. */
6683 char *cp = strchr (*comp_dir, ':');
6684
6685 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
6686 *comp_dir = cp + 1;
6687 }
6688
6689 if (*name == NULL)
6690 *name = "<unknown>";
6691 }
6692
6693 /* Handle DW_AT_stmt_list for a compilation unit or type unit.
6694 DIE is the DW_TAG_compile_unit or DW_TAG_type_unit die for CU.
6695 COMP_DIR is the compilation directory.
6696 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
6697
6698 static void
6699 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
6700 const char *comp_dir, int want_line_info)
6701 {
6702 struct attribute *attr;
6703
6704 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6705 if (attr)
6706 {
6707 unsigned int line_offset = DW_UNSND (attr);
6708 struct line_header *line_header
6709 = dwarf_decode_line_header (line_offset, cu);
6710
6711 if (line_header)
6712 {
6713 cu->line_header = line_header;
6714 make_cleanup (free_cu_line_header, cu);
6715 dwarf_decode_lines (line_header, comp_dir, cu, NULL, want_line_info);
6716 }
6717 }
6718 }
6719
6720 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
6721
6722 static void
6723 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
6724 {
6725 struct objfile *objfile = dwarf2_per_objfile->objfile;
6726 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
6727 CORE_ADDR lowpc = ((CORE_ADDR) -1);
6728 CORE_ADDR highpc = ((CORE_ADDR) 0);
6729 struct attribute *attr;
6730 char *name = NULL;
6731 char *comp_dir = NULL;
6732 struct die_info *child_die;
6733 bfd *abfd = objfile->obfd;
6734 CORE_ADDR baseaddr;
6735
6736 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6737
6738 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
6739
6740 /* If we didn't find a lowpc, set it to highpc to avoid complaints
6741 from finish_block. */
6742 if (lowpc == ((CORE_ADDR) -1))
6743 lowpc = highpc;
6744 lowpc += baseaddr;
6745 highpc += baseaddr;
6746
6747 find_file_and_directory (die, cu, &name, &comp_dir);
6748
6749 prepare_one_comp_unit (cu, die, cu->language);
6750
6751 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
6752 standardised yet. As a workaround for the language detection we fall
6753 back to the DW_AT_producer string. */
6754 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
6755 cu->language = language_opencl;
6756
6757 /* Similar hack for Go. */
6758 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
6759 set_cu_language (DW_LANG_Go, cu);
6760
6761 /* We assume that we're processing GCC output. */
6762 processing_gcc_compilation = 2;
6763
6764 processing_has_namespace_info = 0;
6765
6766 start_symtab (name, comp_dir, lowpc);
6767 record_debugformat ("DWARF 2");
6768 record_producer (cu->producer);
6769
6770 /* Decode line number information if present. We do this before
6771 processing child DIEs, so that the line header table is available
6772 for DW_AT_decl_file. */
6773 handle_DW_AT_stmt_list (die, cu, comp_dir, 1);
6774
6775 /* Process all dies in compilation unit. */
6776 if (die->child != NULL)
6777 {
6778 child_die = die->child;
6779 while (child_die && child_die->tag)
6780 {
6781 process_die (child_die, cu);
6782 child_die = sibling_die (child_die);
6783 }
6784 }
6785
6786 /* Decode macro information, if present. Dwarf 2 macro information
6787 refers to information in the line number info statement program
6788 header, so we can only read it if we've read the header
6789 successfully. */
6790 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
6791 if (attr && cu->line_header)
6792 {
6793 if (dwarf2_attr (die, DW_AT_macro_info, cu))
6794 complaint (&symfile_complaints,
6795 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
6796
6797 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
6798 }
6799 else
6800 {
6801 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
6802 if (attr && cu->line_header)
6803 {
6804 unsigned int macro_offset = DW_UNSND (attr);
6805
6806 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
6807 }
6808 }
6809
6810 do_cleanups (back_to);
6811 }
6812
6813 /* Process DW_TAG_type_unit.
6814 For TUs we want to skip the first top level sibling if it's not the
6815 actual type being defined by this TU. In this case the first top
6816 level sibling is there to provide context only. */
6817
6818 static void
6819 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
6820 {
6821 struct objfile *objfile = cu->objfile;
6822 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
6823 CORE_ADDR lowpc;
6824 struct attribute *attr;
6825 char *name = NULL;
6826 char *comp_dir = NULL;
6827 struct die_info *child_die;
6828 bfd *abfd = objfile->obfd;
6829
6830 /* start_symtab needs a low pc, but we don't really have one.
6831 Do what read_file_scope would do in the absence of such info. */
6832 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6833
6834 /* Find the filename. Do not use dwarf2_name here, since the filename
6835 is not a source language identifier. */
6836 attr = dwarf2_attr (die, DW_AT_name, cu);
6837 if (attr)
6838 name = DW_STRING (attr);
6839
6840 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
6841 if (attr)
6842 comp_dir = DW_STRING (attr);
6843 else if (name != NULL && IS_ABSOLUTE_PATH (name))
6844 {
6845 comp_dir = ldirname (name);
6846 if (comp_dir != NULL)
6847 make_cleanup (xfree, comp_dir);
6848 }
6849
6850 if (name == NULL)
6851 name = "<unknown>";
6852
6853 prepare_one_comp_unit (cu, die, language_minimal);
6854
6855 /* We assume that we're processing GCC output. */
6856 processing_gcc_compilation = 2;
6857
6858 processing_has_namespace_info = 0;
6859
6860 start_symtab (name, comp_dir, lowpc);
6861 record_debugformat ("DWARF 2");
6862 record_producer (cu->producer);
6863
6864 /* Decode line number information if present. We do this before
6865 processing child DIEs, so that the line header table is available
6866 for DW_AT_decl_file.
6867 We don't need the pc/line-number mapping for type units. */
6868 handle_DW_AT_stmt_list (die, cu, comp_dir, 0);
6869
6870 /* Process the dies in the type unit. */
6871 if (die->child == NULL)
6872 {
6873 dump_die_for_error (die);
6874 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
6875 bfd_get_filename (abfd));
6876 }
6877
6878 child_die = die->child;
6879
6880 while (child_die && child_die->tag)
6881 {
6882 process_die (child_die, cu);
6883
6884 child_die = sibling_die (child_die);
6885 }
6886
6887 do_cleanups (back_to);
6888 }
6889 \f
6890 /* DWO files. */
6891
6892 static hashval_t
6893 hash_dwo_file (const void *item)
6894 {
6895 const struct dwo_file *dwo_file = item;
6896
6897 return htab_hash_string (dwo_file->dwo_name);
6898 }
6899
6900 static int
6901 eq_dwo_file (const void *item_lhs, const void *item_rhs)
6902 {
6903 const struct dwo_file *lhs = item_lhs;
6904 const struct dwo_file *rhs = item_rhs;
6905
6906 return strcmp (lhs->dwo_name, rhs->dwo_name) == 0;
6907 }
6908
6909 /* Allocate a hash table for DWO files. */
6910
6911 static htab_t
6912 allocate_dwo_file_hash_table (void)
6913 {
6914 struct objfile *objfile = dwarf2_per_objfile->objfile;
6915
6916 return htab_create_alloc_ex (41,
6917 hash_dwo_file,
6918 eq_dwo_file,
6919 NULL,
6920 &objfile->objfile_obstack,
6921 hashtab_obstack_allocate,
6922 dummy_obstack_deallocate);
6923 }
6924
6925 static hashval_t
6926 hash_dwo_unit (const void *item)
6927 {
6928 const struct dwo_unit *dwo_unit = item;
6929
6930 /* This drops the top 32 bits of the id, but is ok for a hash. */
6931 return dwo_unit->signature;
6932 }
6933
6934 static int
6935 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
6936 {
6937 const struct dwo_unit *lhs = item_lhs;
6938 const struct dwo_unit *rhs = item_rhs;
6939
6940 /* The signature is assumed to be unique within the DWO file.
6941 So while object file CU dwo_id's always have the value zero,
6942 that's OK, assuming each object file DWO file has only one CU,
6943 and that's the rule for now. */
6944 return lhs->signature == rhs->signature;
6945 }
6946
6947 /* Allocate a hash table for DWO CUs,TUs.
6948 There is one of these tables for each of CUs,TUs for each DWO file. */
6949
6950 static htab_t
6951 allocate_dwo_unit_table (struct objfile *objfile)
6952 {
6953 /* Start out with a pretty small number.
6954 Generally DWO files contain only one CU and maybe some TUs. */
6955 return htab_create_alloc_ex (3,
6956 hash_dwo_unit,
6957 eq_dwo_unit,
6958 NULL,
6959 &objfile->objfile_obstack,
6960 hashtab_obstack_allocate,
6961 dummy_obstack_deallocate);
6962 }
6963
6964 /* This function is mapped across the sections and remembers the offset and
6965 size of each of the DWO debugging sections we are interested in. */
6966
6967 static void
6968 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_file_ptr)
6969 {
6970 struct dwo_file *dwo_file = dwo_file_ptr;
6971 const struct dwo_section_names *names = &dwo_section_names;
6972
6973 if (section_is_p (sectp->name, &names->abbrev_dwo))
6974 {
6975 dwo_file->sections.abbrev.asection = sectp;
6976 dwo_file->sections.abbrev.size = bfd_get_section_size (sectp);
6977 }
6978 else if (section_is_p (sectp->name, &names->info_dwo))
6979 {
6980 dwo_file->sections.info.asection = sectp;
6981 dwo_file->sections.info.size = bfd_get_section_size (sectp);
6982 }
6983 else if (section_is_p (sectp->name, &names->line_dwo))
6984 {
6985 dwo_file->sections.line.asection = sectp;
6986 dwo_file->sections.line.size = bfd_get_section_size (sectp);
6987 }
6988 else if (section_is_p (sectp->name, &names->loc_dwo))
6989 {
6990 dwo_file->sections.loc.asection = sectp;
6991 dwo_file->sections.loc.size = bfd_get_section_size (sectp);
6992 }
6993 else if (section_is_p (sectp->name, &names->macinfo_dwo))
6994 {
6995 dwo_file->sections.macinfo.asection = sectp;
6996 dwo_file->sections.macinfo.size = bfd_get_section_size (sectp);
6997 }
6998 else if (section_is_p (sectp->name, &names->macro_dwo))
6999 {
7000 dwo_file->sections.macro.asection = sectp;
7001 dwo_file->sections.macro.size = bfd_get_section_size (sectp);
7002 }
7003 else if (section_is_p (sectp->name, &names->str_dwo))
7004 {
7005 dwo_file->sections.str.asection = sectp;
7006 dwo_file->sections.str.size = bfd_get_section_size (sectp);
7007 }
7008 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
7009 {
7010 dwo_file->sections.str_offsets.asection = sectp;
7011 dwo_file->sections.str_offsets.size = bfd_get_section_size (sectp);
7012 }
7013 else if (section_is_p (sectp->name, &names->types_dwo))
7014 {
7015 struct dwarf2_section_info type_section;
7016
7017 memset (&type_section, 0, sizeof (type_section));
7018 type_section.asection = sectp;
7019 type_section.size = bfd_get_section_size (sectp);
7020 VEC_safe_push (dwarf2_section_info_def, dwo_file->sections.types,
7021 &type_section);
7022 }
7023 }
7024
7025 /* Structure used to pass data to create_debug_info_hash_table_reader. */
7026
7027 struct create_dwo_info_table_data
7028 {
7029 struct dwo_file *dwo_file;
7030 htab_t cu_htab;
7031 };
7032
7033 /* die_reader_func for create_debug_info_hash_table. */
7034
7035 static void
7036 create_debug_info_hash_table_reader (const struct die_reader_specs *reader,
7037 gdb_byte *info_ptr,
7038 struct die_info *comp_unit_die,
7039 int has_children,
7040 void *datap)
7041 {
7042 struct dwarf2_cu *cu = reader->cu;
7043 struct objfile *objfile = dwarf2_per_objfile->objfile;
7044 sect_offset offset = cu->per_cu->offset;
7045 struct dwarf2_section_info *section = cu->per_cu->info_or_types_section;
7046 struct create_dwo_info_table_data *data = datap;
7047 struct dwo_file *dwo_file = data->dwo_file;
7048 htab_t cu_htab = data->cu_htab;
7049 void **slot;
7050 struct attribute *attr;
7051 struct dwo_unit *dwo_unit;
7052
7053 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7054 if (attr == NULL)
7055 {
7056 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
7057 " its dwo_id [in module %s]"),
7058 offset.sect_off, dwo_file->dwo_name);
7059 return;
7060 }
7061
7062 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
7063 dwo_unit->dwo_file = dwo_file;
7064 dwo_unit->signature = DW_UNSND (attr);
7065 dwo_unit->info_or_types_section = section;
7066 dwo_unit->offset = offset;
7067 dwo_unit->length = cu->per_cu->length;
7068
7069 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
7070 gdb_assert (slot != NULL);
7071 if (*slot != NULL)
7072 {
7073 const struct dwo_unit *dup_dwo_unit = *slot;
7074
7075 complaint (&symfile_complaints,
7076 _("debug entry at offset 0x%x is duplicate to the entry at"
7077 " offset 0x%x, dwo_id 0x%s [in module %s]"),
7078 offset.sect_off, dup_dwo_unit->offset.sect_off,
7079 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
7080 dwo_file->dwo_name);
7081 }
7082 else
7083 *slot = dwo_unit;
7084
7085 if (dwarf2_die_debug)
7086 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
7087 offset.sect_off,
7088 phex (dwo_unit->signature,
7089 sizeof (dwo_unit->signature)));
7090 }
7091
7092 /* Create a hash table to map DWO IDs to their CU entry in .debug_info.dwo. */
7093
7094 static htab_t
7095 create_debug_info_hash_table (struct dwo_file *dwo_file)
7096 {
7097 struct objfile *objfile = dwarf2_per_objfile->objfile;
7098 struct dwarf2_section_info *section = &dwo_file->sections.info;
7099 bfd *abfd;
7100 htab_t cu_htab;
7101 gdb_byte *info_ptr, *end_ptr;
7102 struct create_dwo_info_table_data create_dwo_info_table_data;
7103
7104 dwarf2_read_section (objfile, section);
7105 info_ptr = section->buffer;
7106
7107 if (info_ptr == NULL)
7108 return NULL;
7109
7110 /* We can't set abfd until now because the section may be empty or
7111 not present, in which case section->asection will be NULL. */
7112 abfd = section->asection->owner;
7113
7114 if (dwarf2_die_debug)
7115 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
7116 bfd_get_filename (abfd));
7117
7118 cu_htab = allocate_dwo_unit_table (objfile);
7119
7120 create_dwo_info_table_data.dwo_file = dwo_file;
7121 create_dwo_info_table_data.cu_htab = cu_htab;
7122
7123 end_ptr = info_ptr + section->size;
7124 while (info_ptr < end_ptr)
7125 {
7126 struct dwarf2_per_cu_data per_cu;
7127
7128 memset (&per_cu, 0, sizeof (per_cu));
7129 per_cu.objfile = objfile;
7130 per_cu.is_debug_types = 0;
7131 per_cu.offset.sect_off = info_ptr - section->buffer;
7132 per_cu.info_or_types_section = section;
7133
7134 init_cutu_and_read_dies_no_follow (&per_cu,
7135 &dwo_file->sections.abbrev,
7136 dwo_file,
7137 create_debug_info_hash_table_reader,
7138 &create_dwo_info_table_data);
7139
7140 info_ptr += per_cu.length;
7141 }
7142
7143 return cu_htab;
7144 }
7145
7146 /* Subroutine of open_dwo_file to simplify it.
7147 Open the file specified by FILE_NAME and hand it off to BFD for
7148 preliminary analysis. Return a newly initialized bfd *, which
7149 includes a canonicalized copy of FILE_NAME.
7150 In case of trouble, return NULL.
7151 NOTE: This function is derived from symfile_bfd_open. */
7152
7153 static bfd *
7154 try_open_dwo_file (const char *file_name)
7155 {
7156 bfd *sym_bfd;
7157 int desc;
7158 char *absolute_name;
7159
7160 desc = openp (debug_file_directory, OPF_TRY_CWD_FIRST, file_name,
7161 O_RDONLY | O_BINARY, &absolute_name);
7162 if (desc < 0)
7163 return NULL;
7164
7165 sym_bfd = bfd_fopen (absolute_name, gnutarget, FOPEN_RB, desc);
7166 if (!sym_bfd)
7167 {
7168 xfree (absolute_name);
7169 return NULL;
7170 }
7171 bfd_set_cacheable (sym_bfd, 1);
7172
7173 if (!bfd_check_format (sym_bfd, bfd_object))
7174 {
7175 bfd_close (sym_bfd); /* This also closes desc. */
7176 xfree (absolute_name);
7177 return NULL;
7178 }
7179
7180 /* bfd_usrdata exists for applications and libbfd must not touch it. */
7181 gdb_assert (bfd_usrdata (sym_bfd) == NULL);
7182
7183 return sym_bfd;
7184 }
7185
7186 /* Try to open DWO file DWO_NAME.
7187 COMP_DIR is the DW_AT_comp_dir attribute.
7188 The result is the bfd handle of the file.
7189 If there is a problem finding or opening the file, return NULL.
7190 Upon success, the canonicalized path of the file is stored in the bfd,
7191 same as symfile_bfd_open. */
7192
7193 static bfd *
7194 open_dwo_file (const char *dwo_name, const char *comp_dir)
7195 {
7196 bfd *abfd;
7197
7198 if (IS_ABSOLUTE_PATH (dwo_name))
7199 return try_open_dwo_file (dwo_name);
7200
7201 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
7202
7203 if (comp_dir != NULL)
7204 {
7205 char *path_to_try = concat (comp_dir, SLASH_STRING, dwo_name, NULL);
7206
7207 /* NOTE: If comp_dir is a relative path, this will also try the
7208 search path, which seems useful. */
7209 abfd = try_open_dwo_file (path_to_try);
7210 xfree (path_to_try);
7211 if (abfd != NULL)
7212 return abfd;
7213 }
7214
7215 /* That didn't work, try debug-file-directory, which, despite its name,
7216 is a list of paths. */
7217
7218 if (*debug_file_directory == '\0')
7219 return NULL;
7220
7221 return try_open_dwo_file (dwo_name);
7222 }
7223
7224 /* Initialize the use of the DWO file specified by DWO_NAME. */
7225
7226 static struct dwo_file *
7227 init_dwo_file (const char *dwo_name, const char *comp_dir)
7228 {
7229 struct objfile *objfile = dwarf2_per_objfile->objfile;
7230 struct dwo_file *dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7231 struct dwo_file);
7232 bfd *abfd;
7233 struct cleanup *cleanups;
7234
7235 if (dwarf2_die_debug)
7236 fprintf_unfiltered (gdb_stdlog, "Reading DWO file %s:\n", dwo_name);
7237
7238 abfd = open_dwo_file (dwo_name, comp_dir);
7239 if (abfd == NULL)
7240 return NULL;
7241 dwo_file->dwo_name = dwo_name;
7242 dwo_file->dwo_bfd = abfd;
7243
7244 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
7245
7246 bfd_map_over_sections (abfd, dwarf2_locate_dwo_sections, dwo_file);
7247
7248 dwo_file->cus = create_debug_info_hash_table (dwo_file);
7249
7250 dwo_file->tus = create_debug_types_hash_table (dwo_file,
7251 dwo_file->sections.types);
7252
7253 discard_cleanups (cleanups);
7254
7255 return dwo_file;
7256 }
7257
7258 /* Lookup DWO file DWO_NAME. */
7259
7260 static struct dwo_file *
7261 lookup_dwo_file (char *dwo_name, const char *comp_dir)
7262 {
7263 struct dwo_file *dwo_file;
7264 struct dwo_file find_entry;
7265 void **slot;
7266
7267 if (dwarf2_per_objfile->dwo_files == NULL)
7268 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
7269
7270 /* Have we already seen this DWO file? */
7271 find_entry.dwo_name = dwo_name;
7272 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
7273
7274 /* If not, read it in and build a table of the DWOs it contains. */
7275 if (*slot == NULL)
7276 *slot = init_dwo_file (dwo_name, comp_dir);
7277
7278 /* NOTE: This will be NULL if unable to open the file. */
7279 dwo_file = *slot;
7280
7281 return dwo_file;
7282 }
7283
7284 /* Lookup the DWO CU referenced from THIS_CU in DWO file DWO_NAME.
7285 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
7286 SIGNATURE is the "dwo_id" of the CU (for consistency we use the same
7287 nomenclature as TUs).
7288 The result is a pointer to the dwo_unit object or NULL if we didn't find it
7289 (dwo_id mismatch or couldn't find the DWO file). */
7290
7291 static struct dwo_unit *
7292 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
7293 char *dwo_name, const char *comp_dir,
7294 ULONGEST signature)
7295 {
7296 struct objfile *objfile = dwarf2_per_objfile->objfile;
7297 struct dwo_file *dwo_file;
7298
7299 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
7300 if (dwo_file == NULL)
7301 return NULL;
7302
7303 /* Look up the DWO using its signature(dwo_id). */
7304
7305 if (dwo_file->cus != NULL)
7306 {
7307 struct dwo_unit find_dwo_cu, *dwo_cu;
7308
7309 find_dwo_cu.signature = signature;
7310 dwo_cu = htab_find (dwo_file->cus, &find_dwo_cu);
7311
7312 if (dwo_cu != NULL)
7313 return dwo_cu;
7314 }
7315
7316 /* We didn't find it. This must mean a dwo_id mismatch. */
7317
7318 complaint (&symfile_complaints,
7319 _("Could not find DWO CU referenced by CU at offset 0x%x"
7320 " [in module %s]"),
7321 this_cu->offset.sect_off, objfile->name);
7322 return NULL;
7323 }
7324
7325 /* Lookup the DWO TU referenced from THIS_TU in DWO file DWO_NAME.
7326 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
7327 The result is a pointer to the dwo_unit object or NULL if we didn't find it
7328 (dwo_id mismatch or couldn't find the DWO file). */
7329
7330 static struct dwo_unit *
7331 lookup_dwo_type_unit (struct signatured_type *this_tu,
7332 char *dwo_name, const char *comp_dir)
7333 {
7334 struct objfile *objfile = dwarf2_per_objfile->objfile;
7335 struct dwo_file *dwo_file;
7336
7337 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
7338 if (dwo_file == NULL)
7339 return NULL;
7340
7341 /* Look up the DWO using its signature(dwo_id). */
7342
7343 if (dwo_file->tus != NULL)
7344 {
7345 struct dwo_unit find_dwo_tu, *dwo_tu;
7346
7347 find_dwo_tu.signature = this_tu->signature;
7348 dwo_tu = htab_find (dwo_file->tus, &find_dwo_tu);
7349
7350 if (dwo_tu != NULL)
7351 return dwo_tu;
7352 }
7353
7354 /* We didn't find it. This must mean a dwo_id mismatch. */
7355
7356 complaint (&symfile_complaints,
7357 _("Could not find DWO TU referenced by TU at offset 0x%x"
7358 " [in module %s]"),
7359 this_tu->per_cu.offset.sect_off, objfile->name);
7360 return NULL;
7361 }
7362
7363 /* Free all resources associated with DWO_FILE.
7364 Close the DWO file and munmap the sections.
7365 All memory should be on the objfile obstack. */
7366
7367 static void
7368 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
7369 {
7370 int ix;
7371 struct dwarf2_section_info *section;
7372
7373 gdb_assert (dwo_file->dwo_bfd != objfile->obfd);
7374 bfd_close (dwo_file->dwo_bfd);
7375
7376 munmap_section_buffer (&dwo_file->sections.abbrev);
7377 munmap_section_buffer (&dwo_file->sections.info);
7378 munmap_section_buffer (&dwo_file->sections.line);
7379 munmap_section_buffer (&dwo_file->sections.loc);
7380 munmap_section_buffer (&dwo_file->sections.str);
7381 munmap_section_buffer (&dwo_file->sections.str_offsets);
7382
7383 for (ix = 0;
7384 VEC_iterate (dwarf2_section_info_def, dwo_file->sections.types,
7385 ix, section);
7386 ++ix)
7387 munmap_section_buffer (section);
7388
7389 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
7390 }
7391
7392 /* Wrapper for free_dwo_file for use in cleanups. */
7393
7394 static void
7395 free_dwo_file_cleanup (void *arg)
7396 {
7397 struct dwo_file *dwo_file = (struct dwo_file *) arg;
7398 struct objfile *objfile = dwarf2_per_objfile->objfile;
7399
7400 free_dwo_file (dwo_file, objfile);
7401 }
7402
7403 /* Traversal function for free_dwo_files. */
7404
7405 static int
7406 free_dwo_file_from_slot (void **slot, void *info)
7407 {
7408 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7409 struct objfile *objfile = (struct objfile *) info;
7410
7411 free_dwo_file (dwo_file, objfile);
7412
7413 return 1;
7414 }
7415
7416 /* Free all resources associated with DWO_FILES. */
7417
7418 static void
7419 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
7420 {
7421 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
7422 }
7423 \f
7424 /* Read in various DIEs. */
7425
7426 /* qsort helper for inherit_abstract_dies. */
7427
7428 static int
7429 unsigned_int_compar (const void *ap, const void *bp)
7430 {
7431 unsigned int a = *(unsigned int *) ap;
7432 unsigned int b = *(unsigned int *) bp;
7433
7434 return (a > b) - (b > a);
7435 }
7436
7437 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
7438 Inherit only the children of the DW_AT_abstract_origin DIE not being
7439 already referenced by DW_AT_abstract_origin from the children of the
7440 current DIE. */
7441
7442 static void
7443 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
7444 {
7445 struct die_info *child_die;
7446 unsigned die_children_count;
7447 /* CU offsets which were referenced by children of the current DIE. */
7448 sect_offset *offsets;
7449 sect_offset *offsets_end, *offsetp;
7450 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
7451 struct die_info *origin_die;
7452 /* Iterator of the ORIGIN_DIE children. */
7453 struct die_info *origin_child_die;
7454 struct cleanup *cleanups;
7455 struct attribute *attr;
7456 struct dwarf2_cu *origin_cu;
7457 struct pending **origin_previous_list_in_scope;
7458
7459 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
7460 if (!attr)
7461 return;
7462
7463 /* Note that following die references may follow to a die in a
7464 different cu. */
7465
7466 origin_cu = cu;
7467 origin_die = follow_die_ref (die, attr, &origin_cu);
7468
7469 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
7470 symbols in. */
7471 origin_previous_list_in_scope = origin_cu->list_in_scope;
7472 origin_cu->list_in_scope = cu->list_in_scope;
7473
7474 if (die->tag != origin_die->tag
7475 && !(die->tag == DW_TAG_inlined_subroutine
7476 && origin_die->tag == DW_TAG_subprogram))
7477 complaint (&symfile_complaints,
7478 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
7479 die->offset.sect_off, origin_die->offset.sect_off);
7480
7481 child_die = die->child;
7482 die_children_count = 0;
7483 while (child_die && child_die->tag)
7484 {
7485 child_die = sibling_die (child_die);
7486 die_children_count++;
7487 }
7488 offsets = xmalloc (sizeof (*offsets) * die_children_count);
7489 cleanups = make_cleanup (xfree, offsets);
7490
7491 offsets_end = offsets;
7492 child_die = die->child;
7493 while (child_die && child_die->tag)
7494 {
7495 /* For each CHILD_DIE, find the corresponding child of
7496 ORIGIN_DIE. If there is more than one layer of
7497 DW_AT_abstract_origin, follow them all; there shouldn't be,
7498 but GCC versions at least through 4.4 generate this (GCC PR
7499 40573). */
7500 struct die_info *child_origin_die = child_die;
7501 struct dwarf2_cu *child_origin_cu = cu;
7502
7503 while (1)
7504 {
7505 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
7506 child_origin_cu);
7507 if (attr == NULL)
7508 break;
7509 child_origin_die = follow_die_ref (child_origin_die, attr,
7510 &child_origin_cu);
7511 }
7512
7513 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
7514 counterpart may exist. */
7515 if (child_origin_die != child_die)
7516 {
7517 if (child_die->tag != child_origin_die->tag
7518 && !(child_die->tag == DW_TAG_inlined_subroutine
7519 && child_origin_die->tag == DW_TAG_subprogram))
7520 complaint (&symfile_complaints,
7521 _("Child DIE 0x%x and its abstract origin 0x%x have "
7522 "different tags"), child_die->offset.sect_off,
7523 child_origin_die->offset.sect_off);
7524 if (child_origin_die->parent != origin_die)
7525 complaint (&symfile_complaints,
7526 _("Child DIE 0x%x and its abstract origin 0x%x have "
7527 "different parents"), child_die->offset.sect_off,
7528 child_origin_die->offset.sect_off);
7529 else
7530 *offsets_end++ = child_origin_die->offset;
7531 }
7532 child_die = sibling_die (child_die);
7533 }
7534 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
7535 unsigned_int_compar);
7536 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
7537 if (offsetp[-1].sect_off == offsetp->sect_off)
7538 complaint (&symfile_complaints,
7539 _("Multiple children of DIE 0x%x refer "
7540 "to DIE 0x%x as their abstract origin"),
7541 die->offset.sect_off, offsetp->sect_off);
7542
7543 offsetp = offsets;
7544 origin_child_die = origin_die->child;
7545 while (origin_child_die && origin_child_die->tag)
7546 {
7547 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
7548 while (offsetp < offsets_end
7549 && offsetp->sect_off < origin_child_die->offset.sect_off)
7550 offsetp++;
7551 if (offsetp >= offsets_end
7552 || offsetp->sect_off > origin_child_die->offset.sect_off)
7553 {
7554 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
7555 process_die (origin_child_die, origin_cu);
7556 }
7557 origin_child_die = sibling_die (origin_child_die);
7558 }
7559 origin_cu->list_in_scope = origin_previous_list_in_scope;
7560
7561 do_cleanups (cleanups);
7562 }
7563
7564 static void
7565 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
7566 {
7567 struct objfile *objfile = cu->objfile;
7568 struct context_stack *new;
7569 CORE_ADDR lowpc;
7570 CORE_ADDR highpc;
7571 struct die_info *child_die;
7572 struct attribute *attr, *call_line, *call_file;
7573 char *name;
7574 CORE_ADDR baseaddr;
7575 struct block *block;
7576 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
7577 VEC (symbolp) *template_args = NULL;
7578 struct template_symbol *templ_func = NULL;
7579
7580 if (inlined_func)
7581 {
7582 /* If we do not have call site information, we can't show the
7583 caller of this inlined function. That's too confusing, so
7584 only use the scope for local variables. */
7585 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
7586 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
7587 if (call_line == NULL || call_file == NULL)
7588 {
7589 read_lexical_block_scope (die, cu);
7590 return;
7591 }
7592 }
7593
7594 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7595
7596 name = dwarf2_name (die, cu);
7597
7598 /* Ignore functions with missing or empty names. These are actually
7599 illegal according to the DWARF standard. */
7600 if (name == NULL)
7601 {
7602 complaint (&symfile_complaints,
7603 _("missing name for subprogram DIE at %d"),
7604 die->offset.sect_off);
7605 return;
7606 }
7607
7608 /* Ignore functions with missing or invalid low and high pc attributes. */
7609 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
7610 {
7611 attr = dwarf2_attr (die, DW_AT_external, cu);
7612 if (!attr || !DW_UNSND (attr))
7613 complaint (&symfile_complaints,
7614 _("cannot get low and high bounds "
7615 "for subprogram DIE at %d"),
7616 die->offset.sect_off);
7617 return;
7618 }
7619
7620 lowpc += baseaddr;
7621 highpc += baseaddr;
7622
7623 /* If we have any template arguments, then we must allocate a
7624 different sort of symbol. */
7625 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
7626 {
7627 if (child_die->tag == DW_TAG_template_type_param
7628 || child_die->tag == DW_TAG_template_value_param)
7629 {
7630 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7631 struct template_symbol);
7632 templ_func->base.is_cplus_template_function = 1;
7633 break;
7634 }
7635 }
7636
7637 new = push_context (0, lowpc);
7638 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
7639 (struct symbol *) templ_func);
7640
7641 /* If there is a location expression for DW_AT_frame_base, record
7642 it. */
7643 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
7644 if (attr)
7645 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
7646 expression is being recorded directly in the function's symbol
7647 and not in a separate frame-base object. I guess this hack is
7648 to avoid adding some sort of frame-base adjunct/annex to the
7649 function's symbol :-(. The problem with doing this is that it
7650 results in a function symbol with a location expression that
7651 has nothing to do with the location of the function, ouch! The
7652 relationship should be: a function's symbol has-a frame base; a
7653 frame-base has-a location expression. */
7654 dwarf2_symbol_mark_computed (attr, new->name, cu);
7655
7656 cu->list_in_scope = &local_symbols;
7657
7658 if (die->child != NULL)
7659 {
7660 child_die = die->child;
7661 while (child_die && child_die->tag)
7662 {
7663 if (child_die->tag == DW_TAG_template_type_param
7664 || child_die->tag == DW_TAG_template_value_param)
7665 {
7666 struct symbol *arg = new_symbol (child_die, NULL, cu);
7667
7668 if (arg != NULL)
7669 VEC_safe_push (symbolp, template_args, arg);
7670 }
7671 else
7672 process_die (child_die, cu);
7673 child_die = sibling_die (child_die);
7674 }
7675 }
7676
7677 inherit_abstract_dies (die, cu);
7678
7679 /* If we have a DW_AT_specification, we might need to import using
7680 directives from the context of the specification DIE. See the
7681 comment in determine_prefix. */
7682 if (cu->language == language_cplus
7683 && dwarf2_attr (die, DW_AT_specification, cu))
7684 {
7685 struct dwarf2_cu *spec_cu = cu;
7686 struct die_info *spec_die = die_specification (die, &spec_cu);
7687
7688 while (spec_die)
7689 {
7690 child_die = spec_die->child;
7691 while (child_die && child_die->tag)
7692 {
7693 if (child_die->tag == DW_TAG_imported_module)
7694 process_die (child_die, spec_cu);
7695 child_die = sibling_die (child_die);
7696 }
7697
7698 /* In some cases, GCC generates specification DIEs that
7699 themselves contain DW_AT_specification attributes. */
7700 spec_die = die_specification (spec_die, &spec_cu);
7701 }
7702 }
7703
7704 new = pop_context ();
7705 /* Make a block for the local symbols within. */
7706 block = finish_block (new->name, &local_symbols, new->old_blocks,
7707 lowpc, highpc, objfile);
7708
7709 /* For C++, set the block's scope. */
7710 if (cu->language == language_cplus || cu->language == language_fortran)
7711 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
7712 determine_prefix (die, cu),
7713 processing_has_namespace_info);
7714
7715 /* If we have address ranges, record them. */
7716 dwarf2_record_block_ranges (die, block, baseaddr, cu);
7717
7718 /* Attach template arguments to function. */
7719 if (! VEC_empty (symbolp, template_args))
7720 {
7721 gdb_assert (templ_func != NULL);
7722
7723 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
7724 templ_func->template_arguments
7725 = obstack_alloc (&objfile->objfile_obstack,
7726 (templ_func->n_template_arguments
7727 * sizeof (struct symbol *)));
7728 memcpy (templ_func->template_arguments,
7729 VEC_address (symbolp, template_args),
7730 (templ_func->n_template_arguments * sizeof (struct symbol *)));
7731 VEC_free (symbolp, template_args);
7732 }
7733
7734 /* In C++, we can have functions nested inside functions (e.g., when
7735 a function declares a class that has methods). This means that
7736 when we finish processing a function scope, we may need to go
7737 back to building a containing block's symbol lists. */
7738 local_symbols = new->locals;
7739 param_symbols = new->params;
7740 using_directives = new->using_directives;
7741
7742 /* If we've finished processing a top-level function, subsequent
7743 symbols go in the file symbol list. */
7744 if (outermost_context_p ())
7745 cu->list_in_scope = &file_symbols;
7746 }
7747
7748 /* Process all the DIES contained within a lexical block scope. Start
7749 a new scope, process the dies, and then close the scope. */
7750
7751 static void
7752 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
7753 {
7754 struct objfile *objfile = cu->objfile;
7755 struct context_stack *new;
7756 CORE_ADDR lowpc, highpc;
7757 struct die_info *child_die;
7758 CORE_ADDR baseaddr;
7759
7760 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7761
7762 /* Ignore blocks with missing or invalid low and high pc attributes. */
7763 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
7764 as multiple lexical blocks? Handling children in a sane way would
7765 be nasty. Might be easier to properly extend generic blocks to
7766 describe ranges. */
7767 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
7768 return;
7769 lowpc += baseaddr;
7770 highpc += baseaddr;
7771
7772 push_context (0, lowpc);
7773 if (die->child != NULL)
7774 {
7775 child_die = die->child;
7776 while (child_die && child_die->tag)
7777 {
7778 process_die (child_die, cu);
7779 child_die = sibling_die (child_die);
7780 }
7781 }
7782 new = pop_context ();
7783
7784 if (local_symbols != NULL || using_directives != NULL)
7785 {
7786 struct block *block
7787 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
7788 highpc, objfile);
7789
7790 /* Note that recording ranges after traversing children, as we
7791 do here, means that recording a parent's ranges entails
7792 walking across all its children's ranges as they appear in
7793 the address map, which is quadratic behavior.
7794
7795 It would be nicer to record the parent's ranges before
7796 traversing its children, simply overriding whatever you find
7797 there. But since we don't even decide whether to create a
7798 block until after we've traversed its children, that's hard
7799 to do. */
7800 dwarf2_record_block_ranges (die, block, baseaddr, cu);
7801 }
7802 local_symbols = new->locals;
7803 using_directives = new->using_directives;
7804 }
7805
7806 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
7807
7808 static void
7809 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
7810 {
7811 struct objfile *objfile = cu->objfile;
7812 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7813 CORE_ADDR pc, baseaddr;
7814 struct attribute *attr;
7815 struct call_site *call_site, call_site_local;
7816 void **slot;
7817 int nparams;
7818 struct die_info *child_die;
7819
7820 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7821
7822 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
7823 if (!attr)
7824 {
7825 complaint (&symfile_complaints,
7826 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
7827 "DIE 0x%x [in module %s]"),
7828 die->offset.sect_off, objfile->name);
7829 return;
7830 }
7831 pc = DW_ADDR (attr) + baseaddr;
7832
7833 if (cu->call_site_htab == NULL)
7834 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
7835 NULL, &objfile->objfile_obstack,
7836 hashtab_obstack_allocate, NULL);
7837 call_site_local.pc = pc;
7838 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
7839 if (*slot != NULL)
7840 {
7841 complaint (&symfile_complaints,
7842 _("Duplicate PC %s for DW_TAG_GNU_call_site "
7843 "DIE 0x%x [in module %s]"),
7844 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
7845 return;
7846 }
7847
7848 /* Count parameters at the caller. */
7849
7850 nparams = 0;
7851 for (child_die = die->child; child_die && child_die->tag;
7852 child_die = sibling_die (child_die))
7853 {
7854 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
7855 {
7856 complaint (&symfile_complaints,
7857 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
7858 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
7859 child_die->tag, child_die->offset.sect_off, objfile->name);
7860 continue;
7861 }
7862
7863 nparams++;
7864 }
7865
7866 call_site = obstack_alloc (&objfile->objfile_obstack,
7867 (sizeof (*call_site)
7868 + (sizeof (*call_site->parameter)
7869 * (nparams - 1))));
7870 *slot = call_site;
7871 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
7872 call_site->pc = pc;
7873
7874 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
7875 {
7876 struct die_info *func_die;
7877
7878 /* Skip also over DW_TAG_inlined_subroutine. */
7879 for (func_die = die->parent;
7880 func_die && func_die->tag != DW_TAG_subprogram
7881 && func_die->tag != DW_TAG_subroutine_type;
7882 func_die = func_die->parent);
7883
7884 /* DW_AT_GNU_all_call_sites is a superset
7885 of DW_AT_GNU_all_tail_call_sites. */
7886 if (func_die
7887 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
7888 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
7889 {
7890 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
7891 not complete. But keep CALL_SITE for look ups via call_site_htab,
7892 both the initial caller containing the real return address PC and
7893 the final callee containing the current PC of a chain of tail
7894 calls do not need to have the tail call list complete. But any
7895 function candidate for a virtual tail call frame searched via
7896 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
7897 determined unambiguously. */
7898 }
7899 else
7900 {
7901 struct type *func_type = NULL;
7902
7903 if (func_die)
7904 func_type = get_die_type (func_die, cu);
7905 if (func_type != NULL)
7906 {
7907 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
7908
7909 /* Enlist this call site to the function. */
7910 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
7911 TYPE_TAIL_CALL_LIST (func_type) = call_site;
7912 }
7913 else
7914 complaint (&symfile_complaints,
7915 _("Cannot find function owning DW_TAG_GNU_call_site "
7916 "DIE 0x%x [in module %s]"),
7917 die->offset.sect_off, objfile->name);
7918 }
7919 }
7920
7921 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
7922 if (attr == NULL)
7923 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
7924 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
7925 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
7926 /* Keep NULL DWARF_BLOCK. */;
7927 else if (attr_form_is_block (attr))
7928 {
7929 struct dwarf2_locexpr_baton *dlbaton;
7930
7931 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
7932 dlbaton->data = DW_BLOCK (attr)->data;
7933 dlbaton->size = DW_BLOCK (attr)->size;
7934 dlbaton->per_cu = cu->per_cu;
7935
7936 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
7937 }
7938 else if (is_ref_attr (attr))
7939 {
7940 struct dwarf2_cu *target_cu = cu;
7941 struct die_info *target_die;
7942
7943 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
7944 gdb_assert (target_cu->objfile == objfile);
7945 if (die_is_declaration (target_die, target_cu))
7946 {
7947 const char *target_physname;
7948
7949 target_physname = dwarf2_physname (NULL, target_die, target_cu);
7950 if (target_physname == NULL)
7951 complaint (&symfile_complaints,
7952 _("DW_AT_GNU_call_site_target target DIE has invalid "
7953 "physname, for referencing DIE 0x%x [in module %s]"),
7954 die->offset.sect_off, objfile->name);
7955 else
7956 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
7957 }
7958 else
7959 {
7960 CORE_ADDR lowpc;
7961
7962 /* DW_AT_entry_pc should be preferred. */
7963 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
7964 complaint (&symfile_complaints,
7965 _("DW_AT_GNU_call_site_target target DIE has invalid "
7966 "low pc, for referencing DIE 0x%x [in module %s]"),
7967 die->offset.sect_off, objfile->name);
7968 else
7969 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
7970 }
7971 }
7972 else
7973 complaint (&symfile_complaints,
7974 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
7975 "block nor reference, for DIE 0x%x [in module %s]"),
7976 die->offset.sect_off, objfile->name);
7977
7978 call_site->per_cu = cu->per_cu;
7979
7980 for (child_die = die->child;
7981 child_die && child_die->tag;
7982 child_die = sibling_die (child_die))
7983 {
7984 struct call_site_parameter *parameter;
7985 struct attribute *loc, *origin;
7986
7987 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
7988 {
7989 /* Already printed the complaint above. */
7990 continue;
7991 }
7992
7993 gdb_assert (call_site->parameter_count < nparams);
7994 parameter = &call_site->parameter[call_site->parameter_count];
7995
7996 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
7997 specifies DW_TAG_formal_parameter. Value of the data assumed for the
7998 register is contained in DW_AT_GNU_call_site_value. */
7999
8000 loc = dwarf2_attr (child_die, DW_AT_location, cu);
8001 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
8002 if (loc == NULL && origin != NULL && is_ref_attr (origin))
8003 {
8004 sect_offset offset;
8005
8006 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
8007 offset = dwarf2_get_ref_die_offset (origin);
8008 gdb_assert (offset.sect_off >= cu->header.offset.sect_off);
8009 parameter->u.param_offset.cu_off = (offset.sect_off
8010 - cu->header.offset.sect_off);
8011 }
8012 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
8013 {
8014 complaint (&symfile_complaints,
8015 _("No DW_FORM_block* DW_AT_location for "
8016 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
8017 child_die->offset.sect_off, objfile->name);
8018 continue;
8019 }
8020 else
8021 {
8022 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
8023 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
8024 if (parameter->u.dwarf_reg != -1)
8025 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
8026 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
8027 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
8028 &parameter->u.fb_offset))
8029 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
8030 else
8031 {
8032 complaint (&symfile_complaints,
8033 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
8034 "for DW_FORM_block* DW_AT_location is supported for "
8035 "DW_TAG_GNU_call_site child DIE 0x%x "
8036 "[in module %s]"),
8037 child_die->offset.sect_off, objfile->name);
8038 continue;
8039 }
8040 }
8041
8042 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
8043 if (!attr_form_is_block (attr))
8044 {
8045 complaint (&symfile_complaints,
8046 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
8047 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
8048 child_die->offset.sect_off, objfile->name);
8049 continue;
8050 }
8051 parameter->value = DW_BLOCK (attr)->data;
8052 parameter->value_size = DW_BLOCK (attr)->size;
8053
8054 /* Parameters are not pre-cleared by memset above. */
8055 parameter->data_value = NULL;
8056 parameter->data_value_size = 0;
8057 call_site->parameter_count++;
8058
8059 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
8060 if (attr)
8061 {
8062 if (!attr_form_is_block (attr))
8063 complaint (&symfile_complaints,
8064 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
8065 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
8066 child_die->offset.sect_off, objfile->name);
8067 else
8068 {
8069 parameter->data_value = DW_BLOCK (attr)->data;
8070 parameter->data_value_size = DW_BLOCK (attr)->size;
8071 }
8072 }
8073 }
8074 }
8075
8076 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
8077 Return 1 if the attributes are present and valid, otherwise, return 0.
8078 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
8079
8080 static int
8081 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
8082 CORE_ADDR *high_return, struct dwarf2_cu *cu,
8083 struct partial_symtab *ranges_pst)
8084 {
8085 struct objfile *objfile = cu->objfile;
8086 struct comp_unit_head *cu_header = &cu->header;
8087 bfd *obfd = objfile->obfd;
8088 unsigned int addr_size = cu_header->addr_size;
8089 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
8090 /* Base address selection entry. */
8091 CORE_ADDR base;
8092 int found_base;
8093 unsigned int dummy;
8094 gdb_byte *buffer;
8095 CORE_ADDR marker;
8096 int low_set;
8097 CORE_ADDR low = 0;
8098 CORE_ADDR high = 0;
8099 CORE_ADDR baseaddr;
8100
8101 found_base = cu->base_known;
8102 base = cu->base_address;
8103
8104 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
8105 if (offset >= dwarf2_per_objfile->ranges.size)
8106 {
8107 complaint (&symfile_complaints,
8108 _("Offset %d out of bounds for DW_AT_ranges attribute"),
8109 offset);
8110 return 0;
8111 }
8112 buffer = dwarf2_per_objfile->ranges.buffer + offset;
8113
8114 /* Read in the largest possible address. */
8115 marker = read_address (obfd, buffer, cu, &dummy);
8116 if ((marker & mask) == mask)
8117 {
8118 /* If we found the largest possible address, then
8119 read the base address. */
8120 base = read_address (obfd, buffer + addr_size, cu, &dummy);
8121 buffer += 2 * addr_size;
8122 offset += 2 * addr_size;
8123 found_base = 1;
8124 }
8125
8126 low_set = 0;
8127
8128 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8129
8130 while (1)
8131 {
8132 CORE_ADDR range_beginning, range_end;
8133
8134 range_beginning = read_address (obfd, buffer, cu, &dummy);
8135 buffer += addr_size;
8136 range_end = read_address (obfd, buffer, cu, &dummy);
8137 buffer += addr_size;
8138 offset += 2 * addr_size;
8139
8140 /* An end of list marker is a pair of zero addresses. */
8141 if (range_beginning == 0 && range_end == 0)
8142 /* Found the end of list entry. */
8143 break;
8144
8145 /* Each base address selection entry is a pair of 2 values.
8146 The first is the largest possible address, the second is
8147 the base address. Check for a base address here. */
8148 if ((range_beginning & mask) == mask)
8149 {
8150 /* If we found the largest possible address, then
8151 read the base address. */
8152 base = read_address (obfd, buffer + addr_size, cu, &dummy);
8153 found_base = 1;
8154 continue;
8155 }
8156
8157 if (!found_base)
8158 {
8159 /* We have no valid base address for the ranges
8160 data. */
8161 complaint (&symfile_complaints,
8162 _("Invalid .debug_ranges data (no base address)"));
8163 return 0;
8164 }
8165
8166 if (range_beginning > range_end)
8167 {
8168 /* Inverted range entries are invalid. */
8169 complaint (&symfile_complaints,
8170 _("Invalid .debug_ranges data (inverted range)"));
8171 return 0;
8172 }
8173
8174 /* Empty range entries have no effect. */
8175 if (range_beginning == range_end)
8176 continue;
8177
8178 range_beginning += base;
8179 range_end += base;
8180
8181 if (ranges_pst != NULL)
8182 addrmap_set_empty (objfile->psymtabs_addrmap,
8183 range_beginning + baseaddr,
8184 range_end - 1 + baseaddr,
8185 ranges_pst);
8186
8187 /* FIXME: This is recording everything as a low-high
8188 segment of consecutive addresses. We should have a
8189 data structure for discontiguous block ranges
8190 instead. */
8191 if (! low_set)
8192 {
8193 low = range_beginning;
8194 high = range_end;
8195 low_set = 1;
8196 }
8197 else
8198 {
8199 if (range_beginning < low)
8200 low = range_beginning;
8201 if (range_end > high)
8202 high = range_end;
8203 }
8204 }
8205
8206 if (! low_set)
8207 /* If the first entry is an end-of-list marker, the range
8208 describes an empty scope, i.e. no instructions. */
8209 return 0;
8210
8211 if (low_return)
8212 *low_return = low;
8213 if (high_return)
8214 *high_return = high;
8215 return 1;
8216 }
8217
8218 /* Get low and high pc attributes from a die. Return 1 if the attributes
8219 are present and valid, otherwise, return 0. Return -1 if the range is
8220 discontinuous, i.e. derived from DW_AT_ranges information. */
8221
8222 static int
8223 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
8224 CORE_ADDR *highpc, struct dwarf2_cu *cu,
8225 struct partial_symtab *pst)
8226 {
8227 struct attribute *attr;
8228 struct attribute *attr_high;
8229 CORE_ADDR low = 0;
8230 CORE_ADDR high = 0;
8231 int ret = 0;
8232
8233 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
8234 if (attr_high)
8235 {
8236 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
8237 if (attr)
8238 {
8239 low = DW_ADDR (attr);
8240 if (attr_high->form == DW_FORM_addr
8241 || attr_high->form == DW_FORM_GNU_addr_index)
8242 high = DW_ADDR (attr_high);
8243 else
8244 high = low + DW_UNSND (attr_high);
8245 }
8246 else
8247 /* Found high w/o low attribute. */
8248 return 0;
8249
8250 /* Found consecutive range of addresses. */
8251 ret = 1;
8252 }
8253 else
8254 {
8255 attr = dwarf2_attr (die, DW_AT_ranges, cu);
8256 if (attr != NULL)
8257 {
8258 unsigned int ranges_offset = DW_UNSND (attr) + cu->ranges_base;
8259
8260 /* Value of the DW_AT_ranges attribute is the offset in the
8261 .debug_ranges section. */
8262 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
8263 return 0;
8264 /* Found discontinuous range of addresses. */
8265 ret = -1;
8266 }
8267 }
8268
8269 /* read_partial_die has also the strict LOW < HIGH requirement. */
8270 if (high <= low)
8271 return 0;
8272
8273 /* When using the GNU linker, .gnu.linkonce. sections are used to
8274 eliminate duplicate copies of functions and vtables and such.
8275 The linker will arbitrarily choose one and discard the others.
8276 The AT_*_pc values for such functions refer to local labels in
8277 these sections. If the section from that file was discarded, the
8278 labels are not in the output, so the relocs get a value of 0.
8279 If this is a discarded function, mark the pc bounds as invalid,
8280 so that GDB will ignore it. */
8281 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
8282 return 0;
8283
8284 *lowpc = low;
8285 if (highpc)
8286 *highpc = high;
8287 return ret;
8288 }
8289
8290 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
8291 its low and high PC addresses. Do nothing if these addresses could not
8292 be determined. Otherwise, set LOWPC to the low address if it is smaller,
8293 and HIGHPC to the high address if greater than HIGHPC. */
8294
8295 static void
8296 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
8297 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8298 struct dwarf2_cu *cu)
8299 {
8300 CORE_ADDR low, high;
8301 struct die_info *child = die->child;
8302
8303 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
8304 {
8305 *lowpc = min (*lowpc, low);
8306 *highpc = max (*highpc, high);
8307 }
8308
8309 /* If the language does not allow nested subprograms (either inside
8310 subprograms or lexical blocks), we're done. */
8311 if (cu->language != language_ada)
8312 return;
8313
8314 /* Check all the children of the given DIE. If it contains nested
8315 subprograms, then check their pc bounds. Likewise, we need to
8316 check lexical blocks as well, as they may also contain subprogram
8317 definitions. */
8318 while (child && child->tag)
8319 {
8320 if (child->tag == DW_TAG_subprogram
8321 || child->tag == DW_TAG_lexical_block)
8322 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
8323 child = sibling_die (child);
8324 }
8325 }
8326
8327 /* Get the low and high pc's represented by the scope DIE, and store
8328 them in *LOWPC and *HIGHPC. If the correct values can't be
8329 determined, set *LOWPC to -1 and *HIGHPC to 0. */
8330
8331 static void
8332 get_scope_pc_bounds (struct die_info *die,
8333 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8334 struct dwarf2_cu *cu)
8335 {
8336 CORE_ADDR best_low = (CORE_ADDR) -1;
8337 CORE_ADDR best_high = (CORE_ADDR) 0;
8338 CORE_ADDR current_low, current_high;
8339
8340 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
8341 {
8342 best_low = current_low;
8343 best_high = current_high;
8344 }
8345 else
8346 {
8347 struct die_info *child = die->child;
8348
8349 while (child && child->tag)
8350 {
8351 switch (child->tag) {
8352 case DW_TAG_subprogram:
8353 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
8354 break;
8355 case DW_TAG_namespace:
8356 case DW_TAG_module:
8357 /* FIXME: carlton/2004-01-16: Should we do this for
8358 DW_TAG_class_type/DW_TAG_structure_type, too? I think
8359 that current GCC's always emit the DIEs corresponding
8360 to definitions of methods of classes as children of a
8361 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
8362 the DIEs giving the declarations, which could be
8363 anywhere). But I don't see any reason why the
8364 standards says that they have to be there. */
8365 get_scope_pc_bounds (child, &current_low, &current_high, cu);
8366
8367 if (current_low != ((CORE_ADDR) -1))
8368 {
8369 best_low = min (best_low, current_low);
8370 best_high = max (best_high, current_high);
8371 }
8372 break;
8373 default:
8374 /* Ignore. */
8375 break;
8376 }
8377
8378 child = sibling_die (child);
8379 }
8380 }
8381
8382 *lowpc = best_low;
8383 *highpc = best_high;
8384 }
8385
8386 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
8387 in DIE. */
8388
8389 static void
8390 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
8391 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
8392 {
8393 struct objfile *objfile = cu->objfile;
8394 struct attribute *attr;
8395 struct attribute *attr_high;
8396
8397 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
8398 if (attr_high)
8399 {
8400 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
8401 if (attr)
8402 {
8403 CORE_ADDR low = DW_ADDR (attr);
8404 CORE_ADDR high;
8405 if (attr_high->form == DW_FORM_addr
8406 || attr_high->form == DW_FORM_GNU_addr_index)
8407 high = DW_ADDR (attr_high);
8408 else
8409 high = low + DW_UNSND (attr_high);
8410
8411 record_block_range (block, baseaddr + low, baseaddr + high - 1);
8412 }
8413 }
8414
8415 attr = dwarf2_attr (die, DW_AT_ranges, cu);
8416 if (attr)
8417 {
8418 bfd *obfd = objfile->obfd;
8419
8420 /* The value of the DW_AT_ranges attribute is the offset of the
8421 address range list in the .debug_ranges section. */
8422 unsigned long offset = DW_UNSND (attr) + cu->ranges_base;
8423 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
8424
8425 /* For some target architectures, but not others, the
8426 read_address function sign-extends the addresses it returns.
8427 To recognize base address selection entries, we need a
8428 mask. */
8429 unsigned int addr_size = cu->header.addr_size;
8430 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
8431
8432 /* The base address, to which the next pair is relative. Note
8433 that this 'base' is a DWARF concept: most entries in a range
8434 list are relative, to reduce the number of relocs against the
8435 debugging information. This is separate from this function's
8436 'baseaddr' argument, which GDB uses to relocate debugging
8437 information from a shared library based on the address at
8438 which the library was loaded. */
8439 CORE_ADDR base = cu->base_address;
8440 int base_known = cu->base_known;
8441
8442 gdb_assert (dwarf2_per_objfile->ranges.readin);
8443 if (offset >= dwarf2_per_objfile->ranges.size)
8444 {
8445 complaint (&symfile_complaints,
8446 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
8447 offset);
8448 return;
8449 }
8450
8451 for (;;)
8452 {
8453 unsigned int bytes_read;
8454 CORE_ADDR start, end;
8455
8456 start = read_address (obfd, buffer, cu, &bytes_read);
8457 buffer += bytes_read;
8458 end = read_address (obfd, buffer, cu, &bytes_read);
8459 buffer += bytes_read;
8460
8461 /* Did we find the end of the range list? */
8462 if (start == 0 && end == 0)
8463 break;
8464
8465 /* Did we find a base address selection entry? */
8466 else if ((start & base_select_mask) == base_select_mask)
8467 {
8468 base = end;
8469 base_known = 1;
8470 }
8471
8472 /* We found an ordinary address range. */
8473 else
8474 {
8475 if (!base_known)
8476 {
8477 complaint (&symfile_complaints,
8478 _("Invalid .debug_ranges data "
8479 "(no base address)"));
8480 return;
8481 }
8482
8483 if (start > end)
8484 {
8485 /* Inverted range entries are invalid. */
8486 complaint (&symfile_complaints,
8487 _("Invalid .debug_ranges data "
8488 "(inverted range)"));
8489 return;
8490 }
8491
8492 /* Empty range entries have no effect. */
8493 if (start == end)
8494 continue;
8495
8496 record_block_range (block,
8497 baseaddr + base + start,
8498 baseaddr + base + end - 1);
8499 }
8500 }
8501 }
8502 }
8503
8504 /* Check whether the producer field indicates either of GCC < 4.6, or the
8505 Intel C/C++ compiler, and cache the result in CU. */
8506
8507 static void
8508 check_producer (struct dwarf2_cu *cu)
8509 {
8510 const char *cs;
8511 int major, minor, release;
8512
8513 if (cu->producer == NULL)
8514 {
8515 /* For unknown compilers expect their behavior is DWARF version
8516 compliant.
8517
8518 GCC started to support .debug_types sections by -gdwarf-4 since
8519 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
8520 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
8521 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
8522 interpreted incorrectly by GDB now - GCC PR debug/48229. */
8523 }
8524 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
8525 {
8526 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
8527
8528 cs = &cu->producer[strlen ("GNU ")];
8529 while (*cs && !isdigit (*cs))
8530 cs++;
8531 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
8532 {
8533 /* Not recognized as GCC. */
8534 }
8535 else
8536 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
8537 }
8538 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
8539 cu->producer_is_icc = 1;
8540 else
8541 {
8542 /* For other non-GCC compilers, expect their behavior is DWARF version
8543 compliant. */
8544 }
8545
8546 cu->checked_producer = 1;
8547 }
8548
8549 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
8550 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
8551 during 4.6.0 experimental. */
8552
8553 static int
8554 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
8555 {
8556 if (!cu->checked_producer)
8557 check_producer (cu);
8558
8559 return cu->producer_is_gxx_lt_4_6;
8560 }
8561
8562 /* Return the default accessibility type if it is not overriden by
8563 DW_AT_accessibility. */
8564
8565 static enum dwarf_access_attribute
8566 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
8567 {
8568 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
8569 {
8570 /* The default DWARF 2 accessibility for members is public, the default
8571 accessibility for inheritance is private. */
8572
8573 if (die->tag != DW_TAG_inheritance)
8574 return DW_ACCESS_public;
8575 else
8576 return DW_ACCESS_private;
8577 }
8578 else
8579 {
8580 /* DWARF 3+ defines the default accessibility a different way. The same
8581 rules apply now for DW_TAG_inheritance as for the members and it only
8582 depends on the container kind. */
8583
8584 if (die->parent->tag == DW_TAG_class_type)
8585 return DW_ACCESS_private;
8586 else
8587 return DW_ACCESS_public;
8588 }
8589 }
8590
8591 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
8592 offset. If the attribute was not found return 0, otherwise return
8593 1. If it was found but could not properly be handled, set *OFFSET
8594 to 0. */
8595
8596 static int
8597 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
8598 LONGEST *offset)
8599 {
8600 struct attribute *attr;
8601
8602 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
8603 if (attr != NULL)
8604 {
8605 *offset = 0;
8606
8607 /* Note that we do not check for a section offset first here.
8608 This is because DW_AT_data_member_location is new in DWARF 4,
8609 so if we see it, we can assume that a constant form is really
8610 a constant and not a section offset. */
8611 if (attr_form_is_constant (attr))
8612 *offset = dwarf2_get_attr_constant_value (attr, 0);
8613 else if (attr_form_is_section_offset (attr))
8614 dwarf2_complex_location_expr_complaint ();
8615 else if (attr_form_is_block (attr))
8616 *offset = decode_locdesc (DW_BLOCK (attr), cu);
8617 else
8618 dwarf2_complex_location_expr_complaint ();
8619
8620 return 1;
8621 }
8622
8623 return 0;
8624 }
8625
8626 /* Add an aggregate field to the field list. */
8627
8628 static void
8629 dwarf2_add_field (struct field_info *fip, struct die_info *die,
8630 struct dwarf2_cu *cu)
8631 {
8632 struct objfile *objfile = cu->objfile;
8633 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8634 struct nextfield *new_field;
8635 struct attribute *attr;
8636 struct field *fp;
8637 char *fieldname = "";
8638
8639 /* Allocate a new field list entry and link it in. */
8640 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
8641 make_cleanup (xfree, new_field);
8642 memset (new_field, 0, sizeof (struct nextfield));
8643
8644 if (die->tag == DW_TAG_inheritance)
8645 {
8646 new_field->next = fip->baseclasses;
8647 fip->baseclasses = new_field;
8648 }
8649 else
8650 {
8651 new_field->next = fip->fields;
8652 fip->fields = new_field;
8653 }
8654 fip->nfields++;
8655
8656 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
8657 if (attr)
8658 new_field->accessibility = DW_UNSND (attr);
8659 else
8660 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
8661 if (new_field->accessibility != DW_ACCESS_public)
8662 fip->non_public_fields = 1;
8663
8664 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
8665 if (attr)
8666 new_field->virtuality = DW_UNSND (attr);
8667 else
8668 new_field->virtuality = DW_VIRTUALITY_none;
8669
8670 fp = &new_field->field;
8671
8672 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
8673 {
8674 LONGEST offset;
8675
8676 /* Data member other than a C++ static data member. */
8677
8678 /* Get type of field. */
8679 fp->type = die_type (die, cu);
8680
8681 SET_FIELD_BITPOS (*fp, 0);
8682
8683 /* Get bit size of field (zero if none). */
8684 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
8685 if (attr)
8686 {
8687 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
8688 }
8689 else
8690 {
8691 FIELD_BITSIZE (*fp) = 0;
8692 }
8693
8694 /* Get bit offset of field. */
8695 if (handle_data_member_location (die, cu, &offset))
8696 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
8697 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
8698 if (attr)
8699 {
8700 if (gdbarch_bits_big_endian (gdbarch))
8701 {
8702 /* For big endian bits, the DW_AT_bit_offset gives the
8703 additional bit offset from the MSB of the containing
8704 anonymous object to the MSB of the field. We don't
8705 have to do anything special since we don't need to
8706 know the size of the anonymous object. */
8707 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
8708 }
8709 else
8710 {
8711 /* For little endian bits, compute the bit offset to the
8712 MSB of the anonymous object, subtract off the number of
8713 bits from the MSB of the field to the MSB of the
8714 object, and then subtract off the number of bits of
8715 the field itself. The result is the bit offset of
8716 the LSB of the field. */
8717 int anonymous_size;
8718 int bit_offset = DW_UNSND (attr);
8719
8720 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8721 if (attr)
8722 {
8723 /* The size of the anonymous object containing
8724 the bit field is explicit, so use the
8725 indicated size (in bytes). */
8726 anonymous_size = DW_UNSND (attr);
8727 }
8728 else
8729 {
8730 /* The size of the anonymous object containing
8731 the bit field must be inferred from the type
8732 attribute of the data member containing the
8733 bit field. */
8734 anonymous_size = TYPE_LENGTH (fp->type);
8735 }
8736 SET_FIELD_BITPOS (*fp,
8737 (FIELD_BITPOS (*fp)
8738 + anonymous_size * bits_per_byte
8739 - bit_offset - FIELD_BITSIZE (*fp)));
8740 }
8741 }
8742
8743 /* Get name of field. */
8744 fieldname = dwarf2_name (die, cu);
8745 if (fieldname == NULL)
8746 fieldname = "";
8747
8748 /* The name is already allocated along with this objfile, so we don't
8749 need to duplicate it for the type. */
8750 fp->name = fieldname;
8751
8752 /* Change accessibility for artificial fields (e.g. virtual table
8753 pointer or virtual base class pointer) to private. */
8754 if (dwarf2_attr (die, DW_AT_artificial, cu))
8755 {
8756 FIELD_ARTIFICIAL (*fp) = 1;
8757 new_field->accessibility = DW_ACCESS_private;
8758 fip->non_public_fields = 1;
8759 }
8760 }
8761 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
8762 {
8763 /* C++ static member. */
8764
8765 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
8766 is a declaration, but all versions of G++ as of this writing
8767 (so through at least 3.2.1) incorrectly generate
8768 DW_TAG_variable tags. */
8769
8770 const char *physname;
8771
8772 /* Get name of field. */
8773 fieldname = dwarf2_name (die, cu);
8774 if (fieldname == NULL)
8775 return;
8776
8777 attr = dwarf2_attr (die, DW_AT_const_value, cu);
8778 if (attr
8779 /* Only create a symbol if this is an external value.
8780 new_symbol checks this and puts the value in the global symbol
8781 table, which we want. If it is not external, new_symbol
8782 will try to put the value in cu->list_in_scope which is wrong. */
8783 && dwarf2_flag_true_p (die, DW_AT_external, cu))
8784 {
8785 /* A static const member, not much different than an enum as far as
8786 we're concerned, except that we can support more types. */
8787 new_symbol (die, NULL, cu);
8788 }
8789
8790 /* Get physical name. */
8791 physname = dwarf2_physname (fieldname, die, cu);
8792
8793 /* The name is already allocated along with this objfile, so we don't
8794 need to duplicate it for the type. */
8795 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
8796 FIELD_TYPE (*fp) = die_type (die, cu);
8797 FIELD_NAME (*fp) = fieldname;
8798 }
8799 else if (die->tag == DW_TAG_inheritance)
8800 {
8801 LONGEST offset;
8802
8803 /* C++ base class field. */
8804 if (handle_data_member_location (die, cu, &offset))
8805 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
8806 FIELD_BITSIZE (*fp) = 0;
8807 FIELD_TYPE (*fp) = die_type (die, cu);
8808 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
8809 fip->nbaseclasses++;
8810 }
8811 }
8812
8813 /* Add a typedef defined in the scope of the FIP's class. */
8814
8815 static void
8816 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
8817 struct dwarf2_cu *cu)
8818 {
8819 struct objfile *objfile = cu->objfile;
8820 struct typedef_field_list *new_field;
8821 struct attribute *attr;
8822 struct typedef_field *fp;
8823 char *fieldname = "";
8824
8825 /* Allocate a new field list entry and link it in. */
8826 new_field = xzalloc (sizeof (*new_field));
8827 make_cleanup (xfree, new_field);
8828
8829 gdb_assert (die->tag == DW_TAG_typedef);
8830
8831 fp = &new_field->field;
8832
8833 /* Get name of field. */
8834 fp->name = dwarf2_name (die, cu);
8835 if (fp->name == NULL)
8836 return;
8837
8838 fp->type = read_type_die (die, cu);
8839
8840 new_field->next = fip->typedef_field_list;
8841 fip->typedef_field_list = new_field;
8842 fip->typedef_field_list_count++;
8843 }
8844
8845 /* Create the vector of fields, and attach it to the type. */
8846
8847 static void
8848 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
8849 struct dwarf2_cu *cu)
8850 {
8851 int nfields = fip->nfields;
8852
8853 /* Record the field count, allocate space for the array of fields,
8854 and create blank accessibility bitfields if necessary. */
8855 TYPE_NFIELDS (type) = nfields;
8856 TYPE_FIELDS (type) = (struct field *)
8857 TYPE_ALLOC (type, sizeof (struct field) * nfields);
8858 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
8859
8860 if (fip->non_public_fields && cu->language != language_ada)
8861 {
8862 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8863
8864 TYPE_FIELD_PRIVATE_BITS (type) =
8865 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8866 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
8867
8868 TYPE_FIELD_PROTECTED_BITS (type) =
8869 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8870 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
8871
8872 TYPE_FIELD_IGNORE_BITS (type) =
8873 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8874 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
8875 }
8876
8877 /* If the type has baseclasses, allocate and clear a bit vector for
8878 TYPE_FIELD_VIRTUAL_BITS. */
8879 if (fip->nbaseclasses && cu->language != language_ada)
8880 {
8881 int num_bytes = B_BYTES (fip->nbaseclasses);
8882 unsigned char *pointer;
8883
8884 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8885 pointer = TYPE_ALLOC (type, num_bytes);
8886 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
8887 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
8888 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
8889 }
8890
8891 /* Copy the saved-up fields into the field vector. Start from the head of
8892 the list, adding to the tail of the field array, so that they end up in
8893 the same order in the array in which they were added to the list. */
8894 while (nfields-- > 0)
8895 {
8896 struct nextfield *fieldp;
8897
8898 if (fip->fields)
8899 {
8900 fieldp = fip->fields;
8901 fip->fields = fieldp->next;
8902 }
8903 else
8904 {
8905 fieldp = fip->baseclasses;
8906 fip->baseclasses = fieldp->next;
8907 }
8908
8909 TYPE_FIELD (type, nfields) = fieldp->field;
8910 switch (fieldp->accessibility)
8911 {
8912 case DW_ACCESS_private:
8913 if (cu->language != language_ada)
8914 SET_TYPE_FIELD_PRIVATE (type, nfields);
8915 break;
8916
8917 case DW_ACCESS_protected:
8918 if (cu->language != language_ada)
8919 SET_TYPE_FIELD_PROTECTED (type, nfields);
8920 break;
8921
8922 case DW_ACCESS_public:
8923 break;
8924
8925 default:
8926 /* Unknown accessibility. Complain and treat it as public. */
8927 {
8928 complaint (&symfile_complaints, _("unsupported accessibility %d"),
8929 fieldp->accessibility);
8930 }
8931 break;
8932 }
8933 if (nfields < fip->nbaseclasses)
8934 {
8935 switch (fieldp->virtuality)
8936 {
8937 case DW_VIRTUALITY_virtual:
8938 case DW_VIRTUALITY_pure_virtual:
8939 if (cu->language == language_ada)
8940 error (_("unexpected virtuality in component of Ada type"));
8941 SET_TYPE_FIELD_VIRTUAL (type, nfields);
8942 break;
8943 }
8944 }
8945 }
8946 }
8947
8948 /* Add a member function to the proper fieldlist. */
8949
8950 static void
8951 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
8952 struct type *type, struct dwarf2_cu *cu)
8953 {
8954 struct objfile *objfile = cu->objfile;
8955 struct attribute *attr;
8956 struct fnfieldlist *flp;
8957 int i;
8958 struct fn_field *fnp;
8959 char *fieldname;
8960 struct nextfnfield *new_fnfield;
8961 struct type *this_type;
8962 enum dwarf_access_attribute accessibility;
8963
8964 if (cu->language == language_ada)
8965 error (_("unexpected member function in Ada type"));
8966
8967 /* Get name of member function. */
8968 fieldname = dwarf2_name (die, cu);
8969 if (fieldname == NULL)
8970 return;
8971
8972 /* Look up member function name in fieldlist. */
8973 for (i = 0; i < fip->nfnfields; i++)
8974 {
8975 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
8976 break;
8977 }
8978
8979 /* Create new list element if necessary. */
8980 if (i < fip->nfnfields)
8981 flp = &fip->fnfieldlists[i];
8982 else
8983 {
8984 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
8985 {
8986 fip->fnfieldlists = (struct fnfieldlist *)
8987 xrealloc (fip->fnfieldlists,
8988 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
8989 * sizeof (struct fnfieldlist));
8990 if (fip->nfnfields == 0)
8991 make_cleanup (free_current_contents, &fip->fnfieldlists);
8992 }
8993 flp = &fip->fnfieldlists[fip->nfnfields];
8994 flp->name = fieldname;
8995 flp->length = 0;
8996 flp->head = NULL;
8997 i = fip->nfnfields++;
8998 }
8999
9000 /* Create a new member function field and chain it to the field list
9001 entry. */
9002 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
9003 make_cleanup (xfree, new_fnfield);
9004 memset (new_fnfield, 0, sizeof (struct nextfnfield));
9005 new_fnfield->next = flp->head;
9006 flp->head = new_fnfield;
9007 flp->length++;
9008
9009 /* Fill in the member function field info. */
9010 fnp = &new_fnfield->fnfield;
9011
9012 /* Delay processing of the physname until later. */
9013 if (cu->language == language_cplus || cu->language == language_java)
9014 {
9015 add_to_method_list (type, i, flp->length - 1, fieldname,
9016 die, cu);
9017 }
9018 else
9019 {
9020 const char *physname = dwarf2_physname (fieldname, die, cu);
9021 fnp->physname = physname ? physname : "";
9022 }
9023
9024 fnp->type = alloc_type (objfile);
9025 this_type = read_type_die (die, cu);
9026 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
9027 {
9028 int nparams = TYPE_NFIELDS (this_type);
9029
9030 /* TYPE is the domain of this method, and THIS_TYPE is the type
9031 of the method itself (TYPE_CODE_METHOD). */
9032 smash_to_method_type (fnp->type, type,
9033 TYPE_TARGET_TYPE (this_type),
9034 TYPE_FIELDS (this_type),
9035 TYPE_NFIELDS (this_type),
9036 TYPE_VARARGS (this_type));
9037
9038 /* Handle static member functions.
9039 Dwarf2 has no clean way to discern C++ static and non-static
9040 member functions. G++ helps GDB by marking the first
9041 parameter for non-static member functions (which is the this
9042 pointer) as artificial. We obtain this information from
9043 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
9044 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
9045 fnp->voffset = VOFFSET_STATIC;
9046 }
9047 else
9048 complaint (&symfile_complaints, _("member function type missing for '%s'"),
9049 dwarf2_full_name (fieldname, die, cu));
9050
9051 /* Get fcontext from DW_AT_containing_type if present. */
9052 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
9053 fnp->fcontext = die_containing_type (die, cu);
9054
9055 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
9056 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
9057
9058 /* Get accessibility. */
9059 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
9060 if (attr)
9061 accessibility = DW_UNSND (attr);
9062 else
9063 accessibility = dwarf2_default_access_attribute (die, cu);
9064 switch (accessibility)
9065 {
9066 case DW_ACCESS_private:
9067 fnp->is_private = 1;
9068 break;
9069 case DW_ACCESS_protected:
9070 fnp->is_protected = 1;
9071 break;
9072 }
9073
9074 /* Check for artificial methods. */
9075 attr = dwarf2_attr (die, DW_AT_artificial, cu);
9076 if (attr && DW_UNSND (attr) != 0)
9077 fnp->is_artificial = 1;
9078
9079 /* Get index in virtual function table if it is a virtual member
9080 function. For older versions of GCC, this is an offset in the
9081 appropriate virtual table, as specified by DW_AT_containing_type.
9082 For everyone else, it is an expression to be evaluated relative
9083 to the object address. */
9084
9085 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
9086 if (attr)
9087 {
9088 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
9089 {
9090 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
9091 {
9092 /* Old-style GCC. */
9093 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
9094 }
9095 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
9096 || (DW_BLOCK (attr)->size > 1
9097 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
9098 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
9099 {
9100 struct dwarf_block blk;
9101 int offset;
9102
9103 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
9104 ? 1 : 2);
9105 blk.size = DW_BLOCK (attr)->size - offset;
9106 blk.data = DW_BLOCK (attr)->data + offset;
9107 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
9108 if ((fnp->voffset % cu->header.addr_size) != 0)
9109 dwarf2_complex_location_expr_complaint ();
9110 else
9111 fnp->voffset /= cu->header.addr_size;
9112 fnp->voffset += 2;
9113 }
9114 else
9115 dwarf2_complex_location_expr_complaint ();
9116
9117 if (!fnp->fcontext)
9118 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
9119 }
9120 else if (attr_form_is_section_offset (attr))
9121 {
9122 dwarf2_complex_location_expr_complaint ();
9123 }
9124 else
9125 {
9126 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
9127 fieldname);
9128 }
9129 }
9130 else
9131 {
9132 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
9133 if (attr && DW_UNSND (attr))
9134 {
9135 /* GCC does this, as of 2008-08-25; PR debug/37237. */
9136 complaint (&symfile_complaints,
9137 _("Member function \"%s\" (offset %d) is virtual "
9138 "but the vtable offset is not specified"),
9139 fieldname, die->offset.sect_off);
9140 ALLOCATE_CPLUS_STRUCT_TYPE (type);
9141 TYPE_CPLUS_DYNAMIC (type) = 1;
9142 }
9143 }
9144 }
9145
9146 /* Create the vector of member function fields, and attach it to the type. */
9147
9148 static void
9149 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
9150 struct dwarf2_cu *cu)
9151 {
9152 struct fnfieldlist *flp;
9153 int i;
9154
9155 if (cu->language == language_ada)
9156 error (_("unexpected member functions in Ada type"));
9157
9158 ALLOCATE_CPLUS_STRUCT_TYPE (type);
9159 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
9160 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
9161
9162 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
9163 {
9164 struct nextfnfield *nfp = flp->head;
9165 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
9166 int k;
9167
9168 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
9169 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
9170 fn_flp->fn_fields = (struct fn_field *)
9171 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
9172 for (k = flp->length; (k--, nfp); nfp = nfp->next)
9173 fn_flp->fn_fields[k] = nfp->fnfield;
9174 }
9175
9176 TYPE_NFN_FIELDS (type) = fip->nfnfields;
9177 }
9178
9179 /* Returns non-zero if NAME is the name of a vtable member in CU's
9180 language, zero otherwise. */
9181 static int
9182 is_vtable_name (const char *name, struct dwarf2_cu *cu)
9183 {
9184 static const char vptr[] = "_vptr";
9185 static const char vtable[] = "vtable";
9186
9187 /* Look for the C++ and Java forms of the vtable. */
9188 if ((cu->language == language_java
9189 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
9190 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
9191 && is_cplus_marker (name[sizeof (vptr) - 1])))
9192 return 1;
9193
9194 return 0;
9195 }
9196
9197 /* GCC outputs unnamed structures that are really pointers to member
9198 functions, with the ABI-specified layout. If TYPE describes
9199 such a structure, smash it into a member function type.
9200
9201 GCC shouldn't do this; it should just output pointer to member DIEs.
9202 This is GCC PR debug/28767. */
9203
9204 static void
9205 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
9206 {
9207 struct type *pfn_type, *domain_type, *new_type;
9208
9209 /* Check for a structure with no name and two children. */
9210 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
9211 return;
9212
9213 /* Check for __pfn and __delta members. */
9214 if (TYPE_FIELD_NAME (type, 0) == NULL
9215 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
9216 || TYPE_FIELD_NAME (type, 1) == NULL
9217 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
9218 return;
9219
9220 /* Find the type of the method. */
9221 pfn_type = TYPE_FIELD_TYPE (type, 0);
9222 if (pfn_type == NULL
9223 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
9224 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
9225 return;
9226
9227 /* Look for the "this" argument. */
9228 pfn_type = TYPE_TARGET_TYPE (pfn_type);
9229 if (TYPE_NFIELDS (pfn_type) == 0
9230 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
9231 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
9232 return;
9233
9234 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
9235 new_type = alloc_type (objfile);
9236 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
9237 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
9238 TYPE_VARARGS (pfn_type));
9239 smash_to_methodptr_type (type, new_type);
9240 }
9241
9242 /* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
9243 (icc). */
9244
9245 static int
9246 producer_is_icc (struct dwarf2_cu *cu)
9247 {
9248 if (!cu->checked_producer)
9249 check_producer (cu);
9250
9251 return cu->producer_is_icc;
9252 }
9253
9254 /* Called when we find the DIE that starts a structure or union scope
9255 (definition) to create a type for the structure or union. Fill in
9256 the type's name and general properties; the members will not be
9257 processed until process_structure_type.
9258
9259 NOTE: we need to call these functions regardless of whether or not the
9260 DIE has a DW_AT_name attribute, since it might be an anonymous
9261 structure or union. This gets the type entered into our set of
9262 user defined types.
9263
9264 However, if the structure is incomplete (an opaque struct/union)
9265 then suppress creating a symbol table entry for it since gdb only
9266 wants to find the one with the complete definition. Note that if
9267 it is complete, we just call new_symbol, which does it's own
9268 checking about whether the struct/union is anonymous or not (and
9269 suppresses creating a symbol table entry itself). */
9270
9271 static struct type *
9272 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
9273 {
9274 struct objfile *objfile = cu->objfile;
9275 struct type *type;
9276 struct attribute *attr;
9277 char *name;
9278
9279 /* If the definition of this type lives in .debug_types, read that type.
9280 Don't follow DW_AT_specification though, that will take us back up
9281 the chain and we want to go down. */
9282 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
9283 if (attr)
9284 {
9285 struct dwarf2_cu *type_cu = cu;
9286 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9287
9288 /* We could just recurse on read_structure_type, but we need to call
9289 get_die_type to ensure only one type for this DIE is created.
9290 This is important, for example, because for c++ classes we need
9291 TYPE_NAME set which is only done by new_symbol. Blech. */
9292 type = read_type_die (type_die, type_cu);
9293
9294 /* TYPE_CU may not be the same as CU.
9295 Ensure TYPE is recorded in CU's type_hash table. */
9296 return set_die_type (die, type, cu);
9297 }
9298
9299 type = alloc_type (objfile);
9300 INIT_CPLUS_SPECIFIC (type);
9301
9302 name = dwarf2_name (die, cu);
9303 if (name != NULL)
9304 {
9305 if (cu->language == language_cplus
9306 || cu->language == language_java)
9307 {
9308 char *full_name = (char *) dwarf2_full_name (name, die, cu);
9309
9310 /* dwarf2_full_name might have already finished building the DIE's
9311 type. If so, there is no need to continue. */
9312 if (get_die_type (die, cu) != NULL)
9313 return get_die_type (die, cu);
9314
9315 TYPE_TAG_NAME (type) = full_name;
9316 if (die->tag == DW_TAG_structure_type
9317 || die->tag == DW_TAG_class_type)
9318 TYPE_NAME (type) = TYPE_TAG_NAME (type);
9319 }
9320 else
9321 {
9322 /* The name is already allocated along with this objfile, so
9323 we don't need to duplicate it for the type. */
9324 TYPE_TAG_NAME (type) = (char *) name;
9325 if (die->tag == DW_TAG_class_type)
9326 TYPE_NAME (type) = TYPE_TAG_NAME (type);
9327 }
9328 }
9329
9330 if (die->tag == DW_TAG_structure_type)
9331 {
9332 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9333 }
9334 else if (die->tag == DW_TAG_union_type)
9335 {
9336 TYPE_CODE (type) = TYPE_CODE_UNION;
9337 }
9338 else
9339 {
9340 TYPE_CODE (type) = TYPE_CODE_CLASS;
9341 }
9342
9343 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
9344 TYPE_DECLARED_CLASS (type) = 1;
9345
9346 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9347 if (attr)
9348 {
9349 TYPE_LENGTH (type) = DW_UNSND (attr);
9350 }
9351 else
9352 {
9353 TYPE_LENGTH (type) = 0;
9354 }
9355
9356 if (producer_is_icc (cu))
9357 {
9358 /* ICC does not output the required DW_AT_declaration
9359 on incomplete types, but gives them a size of zero. */
9360 }
9361 else
9362 TYPE_STUB_SUPPORTED (type) = 1;
9363
9364 if (die_is_declaration (die, cu))
9365 TYPE_STUB (type) = 1;
9366 else if (attr == NULL && die->child == NULL
9367 && producer_is_realview (cu->producer))
9368 /* RealView does not output the required DW_AT_declaration
9369 on incomplete types. */
9370 TYPE_STUB (type) = 1;
9371
9372 /* We need to add the type field to the die immediately so we don't
9373 infinitely recurse when dealing with pointers to the structure
9374 type within the structure itself. */
9375 set_die_type (die, type, cu);
9376
9377 /* set_die_type should be already done. */
9378 set_descriptive_type (type, die, cu);
9379
9380 return type;
9381 }
9382
9383 /* Finish creating a structure or union type, including filling in
9384 its members and creating a symbol for it. */
9385
9386 static void
9387 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
9388 {
9389 struct objfile *objfile = cu->objfile;
9390 struct die_info *child_die = die->child;
9391 struct type *type;
9392
9393 type = get_die_type (die, cu);
9394 if (type == NULL)
9395 type = read_structure_type (die, cu);
9396
9397 if (die->child != NULL && ! die_is_declaration (die, cu))
9398 {
9399 struct field_info fi;
9400 struct die_info *child_die;
9401 VEC (symbolp) *template_args = NULL;
9402 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
9403
9404 memset (&fi, 0, sizeof (struct field_info));
9405
9406 child_die = die->child;
9407
9408 while (child_die && child_die->tag)
9409 {
9410 if (child_die->tag == DW_TAG_member
9411 || child_die->tag == DW_TAG_variable)
9412 {
9413 /* NOTE: carlton/2002-11-05: A C++ static data member
9414 should be a DW_TAG_member that is a declaration, but
9415 all versions of G++ as of this writing (so through at
9416 least 3.2.1) incorrectly generate DW_TAG_variable
9417 tags for them instead. */
9418 dwarf2_add_field (&fi, child_die, cu);
9419 }
9420 else if (child_die->tag == DW_TAG_subprogram)
9421 {
9422 /* C++ member function. */
9423 dwarf2_add_member_fn (&fi, child_die, type, cu);
9424 }
9425 else if (child_die->tag == DW_TAG_inheritance)
9426 {
9427 /* C++ base class field. */
9428 dwarf2_add_field (&fi, child_die, cu);
9429 }
9430 else if (child_die->tag == DW_TAG_typedef)
9431 dwarf2_add_typedef (&fi, child_die, cu);
9432 else if (child_die->tag == DW_TAG_template_type_param
9433 || child_die->tag == DW_TAG_template_value_param)
9434 {
9435 struct symbol *arg = new_symbol (child_die, NULL, cu);
9436
9437 if (arg != NULL)
9438 VEC_safe_push (symbolp, template_args, arg);
9439 }
9440
9441 child_die = sibling_die (child_die);
9442 }
9443
9444 /* Attach template arguments to type. */
9445 if (! VEC_empty (symbolp, template_args))
9446 {
9447 ALLOCATE_CPLUS_STRUCT_TYPE (type);
9448 TYPE_N_TEMPLATE_ARGUMENTS (type)
9449 = VEC_length (symbolp, template_args);
9450 TYPE_TEMPLATE_ARGUMENTS (type)
9451 = obstack_alloc (&objfile->objfile_obstack,
9452 (TYPE_N_TEMPLATE_ARGUMENTS (type)
9453 * sizeof (struct symbol *)));
9454 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
9455 VEC_address (symbolp, template_args),
9456 (TYPE_N_TEMPLATE_ARGUMENTS (type)
9457 * sizeof (struct symbol *)));
9458 VEC_free (symbolp, template_args);
9459 }
9460
9461 /* Attach fields and member functions to the type. */
9462 if (fi.nfields)
9463 dwarf2_attach_fields_to_type (&fi, type, cu);
9464 if (fi.nfnfields)
9465 {
9466 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
9467
9468 /* Get the type which refers to the base class (possibly this
9469 class itself) which contains the vtable pointer for the current
9470 class from the DW_AT_containing_type attribute. This use of
9471 DW_AT_containing_type is a GNU extension. */
9472
9473 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
9474 {
9475 struct type *t = die_containing_type (die, cu);
9476
9477 TYPE_VPTR_BASETYPE (type) = t;
9478 if (type == t)
9479 {
9480 int i;
9481
9482 /* Our own class provides vtbl ptr. */
9483 for (i = TYPE_NFIELDS (t) - 1;
9484 i >= TYPE_N_BASECLASSES (t);
9485 --i)
9486 {
9487 const char *fieldname = TYPE_FIELD_NAME (t, i);
9488
9489 if (is_vtable_name (fieldname, cu))
9490 {
9491 TYPE_VPTR_FIELDNO (type) = i;
9492 break;
9493 }
9494 }
9495
9496 /* Complain if virtual function table field not found. */
9497 if (i < TYPE_N_BASECLASSES (t))
9498 complaint (&symfile_complaints,
9499 _("virtual function table pointer "
9500 "not found when defining class '%s'"),
9501 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
9502 "");
9503 }
9504 else
9505 {
9506 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
9507 }
9508 }
9509 else if (cu->producer
9510 && strncmp (cu->producer,
9511 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
9512 {
9513 /* The IBM XLC compiler does not provide direct indication
9514 of the containing type, but the vtable pointer is
9515 always named __vfp. */
9516
9517 int i;
9518
9519 for (i = TYPE_NFIELDS (type) - 1;
9520 i >= TYPE_N_BASECLASSES (type);
9521 --i)
9522 {
9523 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
9524 {
9525 TYPE_VPTR_FIELDNO (type) = i;
9526 TYPE_VPTR_BASETYPE (type) = type;
9527 break;
9528 }
9529 }
9530 }
9531 }
9532
9533 /* Copy fi.typedef_field_list linked list elements content into the
9534 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
9535 if (fi.typedef_field_list)
9536 {
9537 int i = fi.typedef_field_list_count;
9538
9539 ALLOCATE_CPLUS_STRUCT_TYPE (type);
9540 TYPE_TYPEDEF_FIELD_ARRAY (type)
9541 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
9542 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
9543
9544 /* Reverse the list order to keep the debug info elements order. */
9545 while (--i >= 0)
9546 {
9547 struct typedef_field *dest, *src;
9548
9549 dest = &TYPE_TYPEDEF_FIELD (type, i);
9550 src = &fi.typedef_field_list->field;
9551 fi.typedef_field_list = fi.typedef_field_list->next;
9552 *dest = *src;
9553 }
9554 }
9555
9556 do_cleanups (back_to);
9557
9558 if (HAVE_CPLUS_STRUCT (type))
9559 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
9560 }
9561
9562 quirk_gcc_member_function_pointer (type, objfile);
9563
9564 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
9565 snapshots) has been known to create a die giving a declaration
9566 for a class that has, as a child, a die giving a definition for a
9567 nested class. So we have to process our children even if the
9568 current die is a declaration. Normally, of course, a declaration
9569 won't have any children at all. */
9570
9571 while (child_die != NULL && child_die->tag)
9572 {
9573 if (child_die->tag == DW_TAG_member
9574 || child_die->tag == DW_TAG_variable
9575 || child_die->tag == DW_TAG_inheritance
9576 || child_die->tag == DW_TAG_template_value_param
9577 || child_die->tag == DW_TAG_template_type_param)
9578 {
9579 /* Do nothing. */
9580 }
9581 else
9582 process_die (child_die, cu);
9583
9584 child_die = sibling_die (child_die);
9585 }
9586
9587 /* Do not consider external references. According to the DWARF standard,
9588 these DIEs are identified by the fact that they have no byte_size
9589 attribute, and a declaration attribute. */
9590 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
9591 || !die_is_declaration (die, cu))
9592 new_symbol (die, type, cu);
9593 }
9594
9595 /* Given a DW_AT_enumeration_type die, set its type. We do not
9596 complete the type's fields yet, or create any symbols. */
9597
9598 static struct type *
9599 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
9600 {
9601 struct objfile *objfile = cu->objfile;
9602 struct type *type;
9603 struct attribute *attr;
9604 const char *name;
9605
9606 /* If the definition of this type lives in .debug_types, read that type.
9607 Don't follow DW_AT_specification though, that will take us back up
9608 the chain and we want to go down. */
9609 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
9610 if (attr)
9611 {
9612 struct dwarf2_cu *type_cu = cu;
9613 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9614
9615 type = read_type_die (type_die, type_cu);
9616
9617 /* TYPE_CU may not be the same as CU.
9618 Ensure TYPE is recorded in CU's type_hash table. */
9619 return set_die_type (die, type, cu);
9620 }
9621
9622 type = alloc_type (objfile);
9623
9624 TYPE_CODE (type) = TYPE_CODE_ENUM;
9625 name = dwarf2_full_name (NULL, die, cu);
9626 if (name != NULL)
9627 TYPE_TAG_NAME (type) = (char *) name;
9628
9629 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9630 if (attr)
9631 {
9632 TYPE_LENGTH (type) = DW_UNSND (attr);
9633 }
9634 else
9635 {
9636 TYPE_LENGTH (type) = 0;
9637 }
9638
9639 /* The enumeration DIE can be incomplete. In Ada, any type can be
9640 declared as private in the package spec, and then defined only
9641 inside the package body. Such types are known as Taft Amendment
9642 Types. When another package uses such a type, an incomplete DIE
9643 may be generated by the compiler. */
9644 if (die_is_declaration (die, cu))
9645 TYPE_STUB (type) = 1;
9646
9647 return set_die_type (die, type, cu);
9648 }
9649
9650 /* Given a pointer to a die which begins an enumeration, process all
9651 the dies that define the members of the enumeration, and create the
9652 symbol for the enumeration type.
9653
9654 NOTE: We reverse the order of the element list. */
9655
9656 static void
9657 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
9658 {
9659 struct type *this_type;
9660
9661 this_type = get_die_type (die, cu);
9662 if (this_type == NULL)
9663 this_type = read_enumeration_type (die, cu);
9664
9665 if (die->child != NULL)
9666 {
9667 struct die_info *child_die;
9668 struct symbol *sym;
9669 struct field *fields = NULL;
9670 int num_fields = 0;
9671 int unsigned_enum = 1;
9672 char *name;
9673 int flag_enum = 1;
9674 ULONGEST mask = 0;
9675
9676 child_die = die->child;
9677 while (child_die && child_die->tag)
9678 {
9679 if (child_die->tag != DW_TAG_enumerator)
9680 {
9681 process_die (child_die, cu);
9682 }
9683 else
9684 {
9685 name = dwarf2_name (child_die, cu);
9686 if (name)
9687 {
9688 sym = new_symbol (child_die, this_type, cu);
9689 if (SYMBOL_VALUE (sym) < 0)
9690 {
9691 unsigned_enum = 0;
9692 flag_enum = 0;
9693 }
9694 else if ((mask & SYMBOL_VALUE (sym)) != 0)
9695 flag_enum = 0;
9696 else
9697 mask |= SYMBOL_VALUE (sym);
9698
9699 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
9700 {
9701 fields = (struct field *)
9702 xrealloc (fields,
9703 (num_fields + DW_FIELD_ALLOC_CHUNK)
9704 * sizeof (struct field));
9705 }
9706
9707 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
9708 FIELD_TYPE (fields[num_fields]) = NULL;
9709 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
9710 FIELD_BITSIZE (fields[num_fields]) = 0;
9711
9712 num_fields++;
9713 }
9714 }
9715
9716 child_die = sibling_die (child_die);
9717 }
9718
9719 if (num_fields)
9720 {
9721 TYPE_NFIELDS (this_type) = num_fields;
9722 TYPE_FIELDS (this_type) = (struct field *)
9723 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
9724 memcpy (TYPE_FIELDS (this_type), fields,
9725 sizeof (struct field) * num_fields);
9726 xfree (fields);
9727 }
9728 if (unsigned_enum)
9729 TYPE_UNSIGNED (this_type) = 1;
9730 if (flag_enum)
9731 TYPE_FLAG_ENUM (this_type) = 1;
9732 }
9733
9734 /* If we are reading an enum from a .debug_types unit, and the enum
9735 is a declaration, and the enum is not the signatured type in the
9736 unit, then we do not want to add a symbol for it. Adding a
9737 symbol would in some cases obscure the true definition of the
9738 enum, giving users an incomplete type when the definition is
9739 actually available. Note that we do not want to do this for all
9740 enums which are just declarations, because C++0x allows forward
9741 enum declarations. */
9742 if (cu->per_cu->is_debug_types
9743 && die_is_declaration (die, cu))
9744 {
9745 struct signatured_type *sig_type;
9746
9747 sig_type
9748 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
9749 cu->per_cu->info_or_types_section,
9750 cu->per_cu->offset);
9751 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
9752 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
9753 return;
9754 }
9755
9756 new_symbol (die, this_type, cu);
9757 }
9758
9759 /* Extract all information from a DW_TAG_array_type DIE and put it in
9760 the DIE's type field. For now, this only handles one dimensional
9761 arrays. */
9762
9763 static struct type *
9764 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
9765 {
9766 struct objfile *objfile = cu->objfile;
9767 struct die_info *child_die;
9768 struct type *type;
9769 struct type *element_type, *range_type, *index_type;
9770 struct type **range_types = NULL;
9771 struct attribute *attr;
9772 int ndim = 0;
9773 struct cleanup *back_to;
9774 char *name;
9775
9776 element_type = die_type (die, cu);
9777
9778 /* The die_type call above may have already set the type for this DIE. */
9779 type = get_die_type (die, cu);
9780 if (type)
9781 return type;
9782
9783 /* Irix 6.2 native cc creates array types without children for
9784 arrays with unspecified length. */
9785 if (die->child == NULL)
9786 {
9787 index_type = objfile_type (objfile)->builtin_int;
9788 range_type = create_range_type (NULL, index_type, 0, -1);
9789 type = create_array_type (NULL, element_type, range_type);
9790 return set_die_type (die, type, cu);
9791 }
9792
9793 back_to = make_cleanup (null_cleanup, NULL);
9794 child_die = die->child;
9795 while (child_die && child_die->tag)
9796 {
9797 if (child_die->tag == DW_TAG_subrange_type)
9798 {
9799 struct type *child_type = read_type_die (child_die, cu);
9800
9801 if (child_type != NULL)
9802 {
9803 /* The range type was succesfully read. Save it for the
9804 array type creation. */
9805 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
9806 {
9807 range_types = (struct type **)
9808 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
9809 * sizeof (struct type *));
9810 if (ndim == 0)
9811 make_cleanup (free_current_contents, &range_types);
9812 }
9813 range_types[ndim++] = child_type;
9814 }
9815 }
9816 child_die = sibling_die (child_die);
9817 }
9818
9819 /* Dwarf2 dimensions are output from left to right, create the
9820 necessary array types in backwards order. */
9821
9822 type = element_type;
9823
9824 if (read_array_order (die, cu) == DW_ORD_col_major)
9825 {
9826 int i = 0;
9827
9828 while (i < ndim)
9829 type = create_array_type (NULL, type, range_types[i++]);
9830 }
9831 else
9832 {
9833 while (ndim-- > 0)
9834 type = create_array_type (NULL, type, range_types[ndim]);
9835 }
9836
9837 /* Understand Dwarf2 support for vector types (like they occur on
9838 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
9839 array type. This is not part of the Dwarf2/3 standard yet, but a
9840 custom vendor extension. The main difference between a regular
9841 array and the vector variant is that vectors are passed by value
9842 to functions. */
9843 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
9844 if (attr)
9845 make_vector_type (type);
9846
9847 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
9848 implementation may choose to implement triple vectors using this
9849 attribute. */
9850 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9851 if (attr)
9852 {
9853 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
9854 TYPE_LENGTH (type) = DW_UNSND (attr);
9855 else
9856 complaint (&symfile_complaints,
9857 _("DW_AT_byte_size for array type smaller "
9858 "than the total size of elements"));
9859 }
9860
9861 name = dwarf2_name (die, cu);
9862 if (name)
9863 TYPE_NAME (type) = name;
9864
9865 /* Install the type in the die. */
9866 set_die_type (die, type, cu);
9867
9868 /* set_die_type should be already done. */
9869 set_descriptive_type (type, die, cu);
9870
9871 do_cleanups (back_to);
9872
9873 return type;
9874 }
9875
9876 static enum dwarf_array_dim_ordering
9877 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
9878 {
9879 struct attribute *attr;
9880
9881 attr = dwarf2_attr (die, DW_AT_ordering, cu);
9882
9883 if (attr) return DW_SND (attr);
9884
9885 /* GNU F77 is a special case, as at 08/2004 array type info is the
9886 opposite order to the dwarf2 specification, but data is still
9887 laid out as per normal fortran.
9888
9889 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
9890 version checking. */
9891
9892 if (cu->language == language_fortran
9893 && cu->producer && strstr (cu->producer, "GNU F77"))
9894 {
9895 return DW_ORD_row_major;
9896 }
9897
9898 switch (cu->language_defn->la_array_ordering)
9899 {
9900 case array_column_major:
9901 return DW_ORD_col_major;
9902 case array_row_major:
9903 default:
9904 return DW_ORD_row_major;
9905 };
9906 }
9907
9908 /* Extract all information from a DW_TAG_set_type DIE and put it in
9909 the DIE's type field. */
9910
9911 static struct type *
9912 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
9913 {
9914 struct type *domain_type, *set_type;
9915 struct attribute *attr;
9916
9917 domain_type = die_type (die, cu);
9918
9919 /* The die_type call above may have already set the type for this DIE. */
9920 set_type = get_die_type (die, cu);
9921 if (set_type)
9922 return set_type;
9923
9924 set_type = create_set_type (NULL, domain_type);
9925
9926 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9927 if (attr)
9928 TYPE_LENGTH (set_type) = DW_UNSND (attr);
9929
9930 return set_die_type (die, set_type, cu);
9931 }
9932
9933 /* First cut: install each common block member as a global variable. */
9934
9935 static void
9936 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
9937 {
9938 struct die_info *child_die;
9939 struct attribute *attr;
9940 struct symbol *sym;
9941 CORE_ADDR base = (CORE_ADDR) 0;
9942
9943 attr = dwarf2_attr (die, DW_AT_location, cu);
9944 if (attr)
9945 {
9946 /* Support the .debug_loc offsets. */
9947 if (attr_form_is_block (attr))
9948 {
9949 base = decode_locdesc (DW_BLOCK (attr), cu);
9950 }
9951 else if (attr_form_is_section_offset (attr))
9952 {
9953 dwarf2_complex_location_expr_complaint ();
9954 }
9955 else
9956 {
9957 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9958 "common block member");
9959 }
9960 }
9961 if (die->child != NULL)
9962 {
9963 child_die = die->child;
9964 while (child_die && child_die->tag)
9965 {
9966 LONGEST offset;
9967
9968 sym = new_symbol (child_die, NULL, cu);
9969 if (sym != NULL
9970 && handle_data_member_location (child_die, cu, &offset))
9971 {
9972 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
9973 add_symbol_to_list (sym, &global_symbols);
9974 }
9975 child_die = sibling_die (child_die);
9976 }
9977 }
9978 }
9979
9980 /* Create a type for a C++ namespace. */
9981
9982 static struct type *
9983 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
9984 {
9985 struct objfile *objfile = cu->objfile;
9986 const char *previous_prefix, *name;
9987 int is_anonymous;
9988 struct type *type;
9989
9990 /* For extensions, reuse the type of the original namespace. */
9991 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
9992 {
9993 struct die_info *ext_die;
9994 struct dwarf2_cu *ext_cu = cu;
9995
9996 ext_die = dwarf2_extension (die, &ext_cu);
9997 type = read_type_die (ext_die, ext_cu);
9998
9999 /* EXT_CU may not be the same as CU.
10000 Ensure TYPE is recorded in CU's type_hash table. */
10001 return set_die_type (die, type, cu);
10002 }
10003
10004 name = namespace_name (die, &is_anonymous, cu);
10005
10006 /* Now build the name of the current namespace. */
10007
10008 previous_prefix = determine_prefix (die, cu);
10009 if (previous_prefix[0] != '\0')
10010 name = typename_concat (&objfile->objfile_obstack,
10011 previous_prefix, name, 0, cu);
10012
10013 /* Create the type. */
10014 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
10015 objfile);
10016 TYPE_NAME (type) = (char *) name;
10017 TYPE_TAG_NAME (type) = TYPE_NAME (type);
10018
10019 return set_die_type (die, type, cu);
10020 }
10021
10022 /* Read a C++ namespace. */
10023
10024 static void
10025 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
10026 {
10027 struct objfile *objfile = cu->objfile;
10028 int is_anonymous;
10029
10030 /* Add a symbol associated to this if we haven't seen the namespace
10031 before. Also, add a using directive if it's an anonymous
10032 namespace. */
10033
10034 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
10035 {
10036 struct type *type;
10037
10038 type = read_type_die (die, cu);
10039 new_symbol (die, type, cu);
10040
10041 namespace_name (die, &is_anonymous, cu);
10042 if (is_anonymous)
10043 {
10044 const char *previous_prefix = determine_prefix (die, cu);
10045
10046 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
10047 NULL, NULL, &objfile->objfile_obstack);
10048 }
10049 }
10050
10051 if (die->child != NULL)
10052 {
10053 struct die_info *child_die = die->child;
10054
10055 while (child_die && child_die->tag)
10056 {
10057 process_die (child_die, cu);
10058 child_die = sibling_die (child_die);
10059 }
10060 }
10061 }
10062
10063 /* Read a Fortran module as type. This DIE can be only a declaration used for
10064 imported module. Still we need that type as local Fortran "use ... only"
10065 declaration imports depend on the created type in determine_prefix. */
10066
10067 static struct type *
10068 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
10069 {
10070 struct objfile *objfile = cu->objfile;
10071 char *module_name;
10072 struct type *type;
10073
10074 module_name = dwarf2_name (die, cu);
10075 if (!module_name)
10076 complaint (&symfile_complaints,
10077 _("DW_TAG_module has no name, offset 0x%x"),
10078 die->offset.sect_off);
10079 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
10080
10081 /* determine_prefix uses TYPE_TAG_NAME. */
10082 TYPE_TAG_NAME (type) = TYPE_NAME (type);
10083
10084 return set_die_type (die, type, cu);
10085 }
10086
10087 /* Read a Fortran module. */
10088
10089 static void
10090 read_module (struct die_info *die, struct dwarf2_cu *cu)
10091 {
10092 struct die_info *child_die = die->child;
10093
10094 while (child_die && child_die->tag)
10095 {
10096 process_die (child_die, cu);
10097 child_die = sibling_die (child_die);
10098 }
10099 }
10100
10101 /* Return the name of the namespace represented by DIE. Set
10102 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
10103 namespace. */
10104
10105 static const char *
10106 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
10107 {
10108 struct die_info *current_die;
10109 const char *name = NULL;
10110
10111 /* Loop through the extensions until we find a name. */
10112
10113 for (current_die = die;
10114 current_die != NULL;
10115 current_die = dwarf2_extension (die, &cu))
10116 {
10117 name = dwarf2_name (current_die, cu);
10118 if (name != NULL)
10119 break;
10120 }
10121
10122 /* Is it an anonymous namespace? */
10123
10124 *is_anonymous = (name == NULL);
10125 if (*is_anonymous)
10126 name = CP_ANONYMOUS_NAMESPACE_STR;
10127
10128 return name;
10129 }
10130
10131 /* Extract all information from a DW_TAG_pointer_type DIE and add to
10132 the user defined type vector. */
10133
10134 static struct type *
10135 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
10136 {
10137 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
10138 struct comp_unit_head *cu_header = &cu->header;
10139 struct type *type;
10140 struct attribute *attr_byte_size;
10141 struct attribute *attr_address_class;
10142 int byte_size, addr_class;
10143 struct type *target_type;
10144
10145 target_type = die_type (die, cu);
10146
10147 /* The die_type call above may have already set the type for this DIE. */
10148 type = get_die_type (die, cu);
10149 if (type)
10150 return type;
10151
10152 type = lookup_pointer_type (target_type);
10153
10154 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
10155 if (attr_byte_size)
10156 byte_size = DW_UNSND (attr_byte_size);
10157 else
10158 byte_size = cu_header->addr_size;
10159
10160 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
10161 if (attr_address_class)
10162 addr_class = DW_UNSND (attr_address_class);
10163 else
10164 addr_class = DW_ADDR_none;
10165
10166 /* If the pointer size or address class is different than the
10167 default, create a type variant marked as such and set the
10168 length accordingly. */
10169 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
10170 {
10171 if (gdbarch_address_class_type_flags_p (gdbarch))
10172 {
10173 int type_flags;
10174
10175 type_flags = gdbarch_address_class_type_flags
10176 (gdbarch, byte_size, addr_class);
10177 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
10178 == 0);
10179 type = make_type_with_address_space (type, type_flags);
10180 }
10181 else if (TYPE_LENGTH (type) != byte_size)
10182 {
10183 complaint (&symfile_complaints,
10184 _("invalid pointer size %d"), byte_size);
10185 }
10186 else
10187 {
10188 /* Should we also complain about unhandled address classes? */
10189 }
10190 }
10191
10192 TYPE_LENGTH (type) = byte_size;
10193 return set_die_type (die, type, cu);
10194 }
10195
10196 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
10197 the user defined type vector. */
10198
10199 static struct type *
10200 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
10201 {
10202 struct type *type;
10203 struct type *to_type;
10204 struct type *domain;
10205
10206 to_type = die_type (die, cu);
10207 domain = die_containing_type (die, cu);
10208
10209 /* The calls above may have already set the type for this DIE. */
10210 type = get_die_type (die, cu);
10211 if (type)
10212 return type;
10213
10214 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
10215 type = lookup_methodptr_type (to_type);
10216 else
10217 type = lookup_memberptr_type (to_type, domain);
10218
10219 return set_die_type (die, type, cu);
10220 }
10221
10222 /* Extract all information from a DW_TAG_reference_type DIE and add to
10223 the user defined type vector. */
10224
10225 static struct type *
10226 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
10227 {
10228 struct comp_unit_head *cu_header = &cu->header;
10229 struct type *type, *target_type;
10230 struct attribute *attr;
10231
10232 target_type = die_type (die, cu);
10233
10234 /* The die_type call above may have already set the type for this DIE. */
10235 type = get_die_type (die, cu);
10236 if (type)
10237 return type;
10238
10239 type = lookup_reference_type (target_type);
10240 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10241 if (attr)
10242 {
10243 TYPE_LENGTH (type) = DW_UNSND (attr);
10244 }
10245 else
10246 {
10247 TYPE_LENGTH (type) = cu_header->addr_size;
10248 }
10249 return set_die_type (die, type, cu);
10250 }
10251
10252 static struct type *
10253 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
10254 {
10255 struct type *base_type, *cv_type;
10256
10257 base_type = die_type (die, cu);
10258
10259 /* The die_type call above may have already set the type for this DIE. */
10260 cv_type = get_die_type (die, cu);
10261 if (cv_type)
10262 return cv_type;
10263
10264 /* In case the const qualifier is applied to an array type, the element type
10265 is so qualified, not the array type (section 6.7.3 of C99). */
10266 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
10267 {
10268 struct type *el_type, *inner_array;
10269
10270 base_type = copy_type (base_type);
10271 inner_array = base_type;
10272
10273 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
10274 {
10275 TYPE_TARGET_TYPE (inner_array) =
10276 copy_type (TYPE_TARGET_TYPE (inner_array));
10277 inner_array = TYPE_TARGET_TYPE (inner_array);
10278 }
10279
10280 el_type = TYPE_TARGET_TYPE (inner_array);
10281 TYPE_TARGET_TYPE (inner_array) =
10282 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
10283
10284 return set_die_type (die, base_type, cu);
10285 }
10286
10287 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
10288 return set_die_type (die, cv_type, cu);
10289 }
10290
10291 static struct type *
10292 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
10293 {
10294 struct type *base_type, *cv_type;
10295
10296 base_type = die_type (die, cu);
10297
10298 /* The die_type call above may have already set the type for this DIE. */
10299 cv_type = get_die_type (die, cu);
10300 if (cv_type)
10301 return cv_type;
10302
10303 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
10304 return set_die_type (die, cv_type, cu);
10305 }
10306
10307 /* Extract all information from a DW_TAG_string_type DIE and add to
10308 the user defined type vector. It isn't really a user defined type,
10309 but it behaves like one, with other DIE's using an AT_user_def_type
10310 attribute to reference it. */
10311
10312 static struct type *
10313 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
10314 {
10315 struct objfile *objfile = cu->objfile;
10316 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10317 struct type *type, *range_type, *index_type, *char_type;
10318 struct attribute *attr;
10319 unsigned int length;
10320
10321 attr = dwarf2_attr (die, DW_AT_string_length, cu);
10322 if (attr)
10323 {
10324 length = DW_UNSND (attr);
10325 }
10326 else
10327 {
10328 /* Check for the DW_AT_byte_size attribute. */
10329 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10330 if (attr)
10331 {
10332 length = DW_UNSND (attr);
10333 }
10334 else
10335 {
10336 length = 1;
10337 }
10338 }
10339
10340 index_type = objfile_type (objfile)->builtin_int;
10341 range_type = create_range_type (NULL, index_type, 1, length);
10342 char_type = language_string_char_type (cu->language_defn, gdbarch);
10343 type = create_string_type (NULL, char_type, range_type);
10344
10345 return set_die_type (die, type, cu);
10346 }
10347
10348 /* Handle DIES due to C code like:
10349
10350 struct foo
10351 {
10352 int (*funcp)(int a, long l);
10353 int b;
10354 };
10355
10356 ('funcp' generates a DW_TAG_subroutine_type DIE). */
10357
10358 static struct type *
10359 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
10360 {
10361 struct objfile *objfile = cu->objfile;
10362 struct type *type; /* Type that this function returns. */
10363 struct type *ftype; /* Function that returns above type. */
10364 struct attribute *attr;
10365
10366 type = die_type (die, cu);
10367
10368 /* The die_type call above may have already set the type for this DIE. */
10369 ftype = get_die_type (die, cu);
10370 if (ftype)
10371 return ftype;
10372
10373 ftype = lookup_function_type (type);
10374
10375 /* All functions in C++, Pascal and Java have prototypes. */
10376 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
10377 if ((attr && (DW_UNSND (attr) != 0))
10378 || cu->language == language_cplus
10379 || cu->language == language_java
10380 || cu->language == language_pascal)
10381 TYPE_PROTOTYPED (ftype) = 1;
10382 else if (producer_is_realview (cu->producer))
10383 /* RealView does not emit DW_AT_prototyped. We can not
10384 distinguish prototyped and unprototyped functions; default to
10385 prototyped, since that is more common in modern code (and
10386 RealView warns about unprototyped functions). */
10387 TYPE_PROTOTYPED (ftype) = 1;
10388
10389 /* Store the calling convention in the type if it's available in
10390 the subroutine die. Otherwise set the calling convention to
10391 the default value DW_CC_normal. */
10392 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
10393 if (attr)
10394 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
10395 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
10396 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
10397 else
10398 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
10399
10400 /* We need to add the subroutine type to the die immediately so
10401 we don't infinitely recurse when dealing with parameters
10402 declared as the same subroutine type. */
10403 set_die_type (die, ftype, cu);
10404
10405 if (die->child != NULL)
10406 {
10407 struct type *void_type = objfile_type (objfile)->builtin_void;
10408 struct die_info *child_die;
10409 int nparams, iparams;
10410
10411 /* Count the number of parameters.
10412 FIXME: GDB currently ignores vararg functions, but knows about
10413 vararg member functions. */
10414 nparams = 0;
10415 child_die = die->child;
10416 while (child_die && child_die->tag)
10417 {
10418 if (child_die->tag == DW_TAG_formal_parameter)
10419 nparams++;
10420 else if (child_die->tag == DW_TAG_unspecified_parameters)
10421 TYPE_VARARGS (ftype) = 1;
10422 child_die = sibling_die (child_die);
10423 }
10424
10425 /* Allocate storage for parameters and fill them in. */
10426 TYPE_NFIELDS (ftype) = nparams;
10427 TYPE_FIELDS (ftype) = (struct field *)
10428 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
10429
10430 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
10431 even if we error out during the parameters reading below. */
10432 for (iparams = 0; iparams < nparams; iparams++)
10433 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
10434
10435 iparams = 0;
10436 child_die = die->child;
10437 while (child_die && child_die->tag)
10438 {
10439 if (child_die->tag == DW_TAG_formal_parameter)
10440 {
10441 struct type *arg_type;
10442
10443 /* DWARF version 2 has no clean way to discern C++
10444 static and non-static member functions. G++ helps
10445 GDB by marking the first parameter for non-static
10446 member functions (which is the this pointer) as
10447 artificial. We pass this information to
10448 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
10449
10450 DWARF version 3 added DW_AT_object_pointer, which GCC
10451 4.5 does not yet generate. */
10452 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
10453 if (attr)
10454 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
10455 else
10456 {
10457 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
10458
10459 /* GCC/43521: In java, the formal parameter
10460 "this" is sometimes not marked with DW_AT_artificial. */
10461 if (cu->language == language_java)
10462 {
10463 const char *name = dwarf2_name (child_die, cu);
10464
10465 if (name && !strcmp (name, "this"))
10466 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
10467 }
10468 }
10469 arg_type = die_type (child_die, cu);
10470
10471 /* RealView does not mark THIS as const, which the testsuite
10472 expects. GCC marks THIS as const in method definitions,
10473 but not in the class specifications (GCC PR 43053). */
10474 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
10475 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
10476 {
10477 int is_this = 0;
10478 struct dwarf2_cu *arg_cu = cu;
10479 const char *name = dwarf2_name (child_die, cu);
10480
10481 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
10482 if (attr)
10483 {
10484 /* If the compiler emits this, use it. */
10485 if (follow_die_ref (die, attr, &arg_cu) == child_die)
10486 is_this = 1;
10487 }
10488 else if (name && strcmp (name, "this") == 0)
10489 /* Function definitions will have the argument names. */
10490 is_this = 1;
10491 else if (name == NULL && iparams == 0)
10492 /* Declarations may not have the names, so like
10493 elsewhere in GDB, assume an artificial first
10494 argument is "this". */
10495 is_this = 1;
10496
10497 if (is_this)
10498 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
10499 arg_type, 0);
10500 }
10501
10502 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
10503 iparams++;
10504 }
10505 child_die = sibling_die (child_die);
10506 }
10507 }
10508
10509 return ftype;
10510 }
10511
10512 static struct type *
10513 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
10514 {
10515 struct objfile *objfile = cu->objfile;
10516 const char *name = NULL;
10517 struct type *this_type, *target_type;
10518
10519 name = dwarf2_full_name (NULL, die, cu);
10520 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
10521 TYPE_FLAG_TARGET_STUB, NULL, objfile);
10522 TYPE_NAME (this_type) = (char *) name;
10523 set_die_type (die, this_type, cu);
10524 target_type = die_type (die, cu);
10525 if (target_type != this_type)
10526 TYPE_TARGET_TYPE (this_type) = target_type;
10527 else
10528 {
10529 /* Self-referential typedefs are, it seems, not allowed by the DWARF
10530 spec and cause infinite loops in GDB. */
10531 complaint (&symfile_complaints,
10532 _("Self-referential DW_TAG_typedef "
10533 "- DIE at 0x%x [in module %s]"),
10534 die->offset.sect_off, objfile->name);
10535 TYPE_TARGET_TYPE (this_type) = NULL;
10536 }
10537 return this_type;
10538 }
10539
10540 /* Find a representation of a given base type and install
10541 it in the TYPE field of the die. */
10542
10543 static struct type *
10544 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
10545 {
10546 struct objfile *objfile = cu->objfile;
10547 struct type *type;
10548 struct attribute *attr;
10549 int encoding = 0, size = 0;
10550 char *name;
10551 enum type_code code = TYPE_CODE_INT;
10552 int type_flags = 0;
10553 struct type *target_type = NULL;
10554
10555 attr = dwarf2_attr (die, DW_AT_encoding, cu);
10556 if (attr)
10557 {
10558 encoding = DW_UNSND (attr);
10559 }
10560 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10561 if (attr)
10562 {
10563 size = DW_UNSND (attr);
10564 }
10565 name = dwarf2_name (die, cu);
10566 if (!name)
10567 {
10568 complaint (&symfile_complaints,
10569 _("DW_AT_name missing from DW_TAG_base_type"));
10570 }
10571
10572 switch (encoding)
10573 {
10574 case DW_ATE_address:
10575 /* Turn DW_ATE_address into a void * pointer. */
10576 code = TYPE_CODE_PTR;
10577 type_flags |= TYPE_FLAG_UNSIGNED;
10578 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
10579 break;
10580 case DW_ATE_boolean:
10581 code = TYPE_CODE_BOOL;
10582 type_flags |= TYPE_FLAG_UNSIGNED;
10583 break;
10584 case DW_ATE_complex_float:
10585 code = TYPE_CODE_COMPLEX;
10586 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
10587 break;
10588 case DW_ATE_decimal_float:
10589 code = TYPE_CODE_DECFLOAT;
10590 break;
10591 case DW_ATE_float:
10592 code = TYPE_CODE_FLT;
10593 break;
10594 case DW_ATE_signed:
10595 break;
10596 case DW_ATE_unsigned:
10597 type_flags |= TYPE_FLAG_UNSIGNED;
10598 if (cu->language == language_fortran
10599 && name
10600 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
10601 code = TYPE_CODE_CHAR;
10602 break;
10603 case DW_ATE_signed_char:
10604 if (cu->language == language_ada || cu->language == language_m2
10605 || cu->language == language_pascal
10606 || cu->language == language_fortran)
10607 code = TYPE_CODE_CHAR;
10608 break;
10609 case DW_ATE_unsigned_char:
10610 if (cu->language == language_ada || cu->language == language_m2
10611 || cu->language == language_pascal
10612 || cu->language == language_fortran)
10613 code = TYPE_CODE_CHAR;
10614 type_flags |= TYPE_FLAG_UNSIGNED;
10615 break;
10616 case DW_ATE_UTF:
10617 /* We just treat this as an integer and then recognize the
10618 type by name elsewhere. */
10619 break;
10620
10621 default:
10622 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
10623 dwarf_type_encoding_name (encoding));
10624 break;
10625 }
10626
10627 type = init_type (code, size, type_flags, NULL, objfile);
10628 TYPE_NAME (type) = name;
10629 TYPE_TARGET_TYPE (type) = target_type;
10630
10631 if (name && strcmp (name, "char") == 0)
10632 TYPE_NOSIGN (type) = 1;
10633
10634 return set_die_type (die, type, cu);
10635 }
10636
10637 /* Read the given DW_AT_subrange DIE. */
10638
10639 static struct type *
10640 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
10641 {
10642 struct type *base_type;
10643 struct type *range_type;
10644 struct attribute *attr;
10645 LONGEST low, high;
10646 int low_default_is_valid;
10647 char *name;
10648 LONGEST negative_mask;
10649
10650 base_type = die_type (die, cu);
10651 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
10652 check_typedef (base_type);
10653
10654 /* The die_type call above may have already set the type for this DIE. */
10655 range_type = get_die_type (die, cu);
10656 if (range_type)
10657 return range_type;
10658
10659 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
10660 omitting DW_AT_lower_bound. */
10661 switch (cu->language)
10662 {
10663 case language_c:
10664 case language_cplus:
10665 low = 0;
10666 low_default_is_valid = 1;
10667 break;
10668 case language_fortran:
10669 low = 1;
10670 low_default_is_valid = 1;
10671 break;
10672 case language_d:
10673 case language_java:
10674 case language_objc:
10675 low = 0;
10676 low_default_is_valid = (cu->header.version >= 4);
10677 break;
10678 case language_ada:
10679 case language_m2:
10680 case language_pascal:
10681 low = 1;
10682 low_default_is_valid = (cu->header.version >= 4);
10683 break;
10684 default:
10685 low = 0;
10686 low_default_is_valid = 0;
10687 break;
10688 }
10689
10690 /* FIXME: For variable sized arrays either of these could be
10691 a variable rather than a constant value. We'll allow it,
10692 but we don't know how to handle it. */
10693 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
10694 if (attr)
10695 low = dwarf2_get_attr_constant_value (attr, low);
10696 else if (!low_default_is_valid)
10697 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
10698 "- DIE at 0x%x [in module %s]"),
10699 die->offset.sect_off, cu->objfile->name);
10700
10701 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
10702 if (attr)
10703 {
10704 if (attr_form_is_block (attr) || is_ref_attr (attr))
10705 {
10706 /* GCC encodes arrays with unspecified or dynamic length
10707 with a DW_FORM_block1 attribute or a reference attribute.
10708 FIXME: GDB does not yet know how to handle dynamic
10709 arrays properly, treat them as arrays with unspecified
10710 length for now.
10711
10712 FIXME: jimb/2003-09-22: GDB does not really know
10713 how to handle arrays of unspecified length
10714 either; we just represent them as zero-length
10715 arrays. Choose an appropriate upper bound given
10716 the lower bound we've computed above. */
10717 high = low - 1;
10718 }
10719 else
10720 high = dwarf2_get_attr_constant_value (attr, 1);
10721 }
10722 else
10723 {
10724 attr = dwarf2_attr (die, DW_AT_count, cu);
10725 if (attr)
10726 {
10727 int count = dwarf2_get_attr_constant_value (attr, 1);
10728 high = low + count - 1;
10729 }
10730 else
10731 {
10732 /* Unspecified array length. */
10733 high = low - 1;
10734 }
10735 }
10736
10737 /* Dwarf-2 specifications explicitly allows to create subrange types
10738 without specifying a base type.
10739 In that case, the base type must be set to the type of
10740 the lower bound, upper bound or count, in that order, if any of these
10741 three attributes references an object that has a type.
10742 If no base type is found, the Dwarf-2 specifications say that
10743 a signed integer type of size equal to the size of an address should
10744 be used.
10745 For the following C code: `extern char gdb_int [];'
10746 GCC produces an empty range DIE.
10747 FIXME: muller/2010-05-28: Possible references to object for low bound,
10748 high bound or count are not yet handled by this code. */
10749 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
10750 {
10751 struct objfile *objfile = cu->objfile;
10752 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10753 int addr_size = gdbarch_addr_bit (gdbarch) /8;
10754 struct type *int_type = objfile_type (objfile)->builtin_int;
10755
10756 /* Test "int", "long int", and "long long int" objfile types,
10757 and select the first one having a size above or equal to the
10758 architecture address size. */
10759 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10760 base_type = int_type;
10761 else
10762 {
10763 int_type = objfile_type (objfile)->builtin_long;
10764 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10765 base_type = int_type;
10766 else
10767 {
10768 int_type = objfile_type (objfile)->builtin_long_long;
10769 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10770 base_type = int_type;
10771 }
10772 }
10773 }
10774
10775 negative_mask =
10776 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
10777 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
10778 low |= negative_mask;
10779 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
10780 high |= negative_mask;
10781
10782 range_type = create_range_type (NULL, base_type, low, high);
10783
10784 /* Mark arrays with dynamic length at least as an array of unspecified
10785 length. GDB could check the boundary but before it gets implemented at
10786 least allow accessing the array elements. */
10787 if (attr && attr_form_is_block (attr))
10788 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
10789
10790 /* Ada expects an empty array on no boundary attributes. */
10791 if (attr == NULL && cu->language != language_ada)
10792 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
10793
10794 name = dwarf2_name (die, cu);
10795 if (name)
10796 TYPE_NAME (range_type) = name;
10797
10798 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10799 if (attr)
10800 TYPE_LENGTH (range_type) = DW_UNSND (attr);
10801
10802 set_die_type (die, range_type, cu);
10803
10804 /* set_die_type should be already done. */
10805 set_descriptive_type (range_type, die, cu);
10806
10807 return range_type;
10808 }
10809
10810 static struct type *
10811 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
10812 {
10813 struct type *type;
10814
10815 /* For now, we only support the C meaning of an unspecified type: void. */
10816
10817 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
10818 TYPE_NAME (type) = dwarf2_name (die, cu);
10819
10820 return set_die_type (die, type, cu);
10821 }
10822
10823 /* Read a single die and all its descendents. Set the die's sibling
10824 field to NULL; set other fields in the die correctly, and set all
10825 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
10826 location of the info_ptr after reading all of those dies. PARENT
10827 is the parent of the die in question. */
10828
10829 static struct die_info *
10830 read_die_and_children (const struct die_reader_specs *reader,
10831 gdb_byte *info_ptr,
10832 gdb_byte **new_info_ptr,
10833 struct die_info *parent)
10834 {
10835 struct die_info *die;
10836 gdb_byte *cur_ptr;
10837 int has_children;
10838
10839 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
10840 if (die == NULL)
10841 {
10842 *new_info_ptr = cur_ptr;
10843 return NULL;
10844 }
10845 store_in_ref_table (die, reader->cu);
10846
10847 if (has_children)
10848 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
10849 else
10850 {
10851 die->child = NULL;
10852 *new_info_ptr = cur_ptr;
10853 }
10854
10855 die->sibling = NULL;
10856 die->parent = parent;
10857 return die;
10858 }
10859
10860 /* Read a die, all of its descendents, and all of its siblings; set
10861 all of the fields of all of the dies correctly. Arguments are as
10862 in read_die_and_children. */
10863
10864 static struct die_info *
10865 read_die_and_siblings (const struct die_reader_specs *reader,
10866 gdb_byte *info_ptr,
10867 gdb_byte **new_info_ptr,
10868 struct die_info *parent)
10869 {
10870 struct die_info *first_die, *last_sibling;
10871 gdb_byte *cur_ptr;
10872
10873 cur_ptr = info_ptr;
10874 first_die = last_sibling = NULL;
10875
10876 while (1)
10877 {
10878 struct die_info *die
10879 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
10880
10881 if (die == NULL)
10882 {
10883 *new_info_ptr = cur_ptr;
10884 return first_die;
10885 }
10886
10887 if (!first_die)
10888 first_die = die;
10889 else
10890 last_sibling->sibling = die;
10891
10892 last_sibling = die;
10893 }
10894 }
10895
10896 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
10897 attributes.
10898 The caller is responsible for filling in the extra attributes
10899 and updating (*DIEP)->num_attrs.
10900 Set DIEP to point to a newly allocated die with its information,
10901 except for its child, sibling, and parent fields.
10902 Set HAS_CHILDREN to tell whether the die has children or not. */
10903
10904 static gdb_byte *
10905 read_full_die_1 (const struct die_reader_specs *reader,
10906 struct die_info **diep, gdb_byte *info_ptr,
10907 int *has_children, int num_extra_attrs)
10908 {
10909 unsigned int abbrev_number, bytes_read, i;
10910 sect_offset offset;
10911 struct abbrev_info *abbrev;
10912 struct die_info *die;
10913 struct dwarf2_cu *cu = reader->cu;
10914 bfd *abfd = reader->abfd;
10915
10916 offset.sect_off = info_ptr - reader->buffer;
10917 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10918 info_ptr += bytes_read;
10919 if (!abbrev_number)
10920 {
10921 *diep = NULL;
10922 *has_children = 0;
10923 return info_ptr;
10924 }
10925
10926 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
10927 if (!abbrev)
10928 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
10929 abbrev_number,
10930 bfd_get_filename (abfd));
10931
10932 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
10933 die->offset = offset;
10934 die->tag = abbrev->tag;
10935 die->abbrev = abbrev_number;
10936
10937 /* Make the result usable.
10938 The caller needs to update num_attrs after adding the extra
10939 attributes. */
10940 die->num_attrs = abbrev->num_attrs;
10941
10942 for (i = 0; i < abbrev->num_attrs; ++i)
10943 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
10944 info_ptr);
10945
10946 *diep = die;
10947 *has_children = abbrev->has_children;
10948 return info_ptr;
10949 }
10950
10951 /* Read a die and all its attributes.
10952 Set DIEP to point to a newly allocated die with its information,
10953 except for its child, sibling, and parent fields.
10954 Set HAS_CHILDREN to tell whether the die has children or not. */
10955
10956 static gdb_byte *
10957 read_full_die (const struct die_reader_specs *reader,
10958 struct die_info **diep, gdb_byte *info_ptr,
10959 int *has_children)
10960 {
10961 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
10962 }
10963
10964 /* In DWARF version 2, the description of the debugging information is
10965 stored in a separate .debug_abbrev section. Before we read any
10966 dies from a section we read in all abbreviations and install them
10967 in a hash table. This function also sets flags in CU describing
10968 the data found in the abbrev table. */
10969
10970 static void
10971 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
10972 struct dwarf2_section_info *abbrev_section)
10973
10974 {
10975 bfd *abfd = abbrev_section->asection->owner;
10976 struct comp_unit_head *cu_header = &cu->header;
10977 gdb_byte *abbrev_ptr;
10978 struct abbrev_info *cur_abbrev;
10979 unsigned int abbrev_number, bytes_read, abbrev_name;
10980 unsigned int abbrev_form, hash_number;
10981 struct attr_abbrev *cur_attrs;
10982 unsigned int allocated_attrs;
10983
10984 /* Initialize dwarf2 abbrevs. */
10985 obstack_init (&cu->abbrev_obstack);
10986 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
10987 (ABBREV_HASH_SIZE
10988 * sizeof (struct abbrev_info *)));
10989 memset (cu->dwarf2_abbrevs, 0,
10990 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
10991
10992 dwarf2_read_section (cu->objfile, abbrev_section);
10993 abbrev_ptr = abbrev_section->buffer + cu_header->abbrev_offset.sect_off;
10994 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10995 abbrev_ptr += bytes_read;
10996
10997 allocated_attrs = ATTR_ALLOC_CHUNK;
10998 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
10999
11000 /* Loop until we reach an abbrev number of 0. */
11001 while (abbrev_number)
11002 {
11003 cur_abbrev = dwarf_alloc_abbrev (cu);
11004
11005 /* read in abbrev header */
11006 cur_abbrev->number = abbrev_number;
11007 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
11008 abbrev_ptr += bytes_read;
11009 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
11010 abbrev_ptr += 1;
11011
11012 /* now read in declarations */
11013 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
11014 abbrev_ptr += bytes_read;
11015 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
11016 abbrev_ptr += bytes_read;
11017 while (abbrev_name)
11018 {
11019 if (cur_abbrev->num_attrs == allocated_attrs)
11020 {
11021 allocated_attrs += ATTR_ALLOC_CHUNK;
11022 cur_attrs
11023 = xrealloc (cur_attrs, (allocated_attrs
11024 * sizeof (struct attr_abbrev)));
11025 }
11026
11027 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
11028 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
11029 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
11030 abbrev_ptr += bytes_read;
11031 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
11032 abbrev_ptr += bytes_read;
11033 }
11034
11035 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
11036 (cur_abbrev->num_attrs
11037 * sizeof (struct attr_abbrev)));
11038 memcpy (cur_abbrev->attrs, cur_attrs,
11039 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
11040
11041 hash_number = abbrev_number % ABBREV_HASH_SIZE;
11042 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
11043 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
11044
11045 /* Get next abbreviation.
11046 Under Irix6 the abbreviations for a compilation unit are not
11047 always properly terminated with an abbrev number of 0.
11048 Exit loop if we encounter an abbreviation which we have
11049 already read (which means we are about to read the abbreviations
11050 for the next compile unit) or if the end of the abbreviation
11051 table is reached. */
11052 if ((unsigned int) (abbrev_ptr - abbrev_section->buffer)
11053 >= abbrev_section->size)
11054 break;
11055 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
11056 abbrev_ptr += bytes_read;
11057 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
11058 break;
11059 }
11060
11061 xfree (cur_attrs);
11062 }
11063
11064 /* Release the memory used by the abbrev table for a compilation unit. */
11065
11066 static void
11067 dwarf2_free_abbrev_table (void *ptr_to_cu)
11068 {
11069 struct dwarf2_cu *cu = ptr_to_cu;
11070
11071 obstack_free (&cu->abbrev_obstack, NULL);
11072 cu->dwarf2_abbrevs = NULL;
11073 }
11074
11075 /* Lookup an abbrev_info structure in the abbrev hash table. */
11076
11077 static struct abbrev_info *
11078 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
11079 {
11080 unsigned int hash_number;
11081 struct abbrev_info *abbrev;
11082
11083 hash_number = number % ABBREV_HASH_SIZE;
11084 abbrev = cu->dwarf2_abbrevs[hash_number];
11085
11086 while (abbrev)
11087 {
11088 if (abbrev->number == number)
11089 return abbrev;
11090 else
11091 abbrev = abbrev->next;
11092 }
11093 return NULL;
11094 }
11095
11096 /* Returns nonzero if TAG represents a type that we might generate a partial
11097 symbol for. */
11098
11099 static int
11100 is_type_tag_for_partial (int tag)
11101 {
11102 switch (tag)
11103 {
11104 #if 0
11105 /* Some types that would be reasonable to generate partial symbols for,
11106 that we don't at present. */
11107 case DW_TAG_array_type:
11108 case DW_TAG_file_type:
11109 case DW_TAG_ptr_to_member_type:
11110 case DW_TAG_set_type:
11111 case DW_TAG_string_type:
11112 case DW_TAG_subroutine_type:
11113 #endif
11114 case DW_TAG_base_type:
11115 case DW_TAG_class_type:
11116 case DW_TAG_interface_type:
11117 case DW_TAG_enumeration_type:
11118 case DW_TAG_structure_type:
11119 case DW_TAG_subrange_type:
11120 case DW_TAG_typedef:
11121 case DW_TAG_union_type:
11122 return 1;
11123 default:
11124 return 0;
11125 }
11126 }
11127
11128 /* Load all DIEs that are interesting for partial symbols into memory. */
11129
11130 static struct partial_die_info *
11131 load_partial_dies (const struct die_reader_specs *reader,
11132 gdb_byte *info_ptr, int building_psymtab)
11133 {
11134 struct dwarf2_cu *cu = reader->cu;
11135 struct objfile *objfile = cu->objfile;
11136 struct partial_die_info *part_die;
11137 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
11138 struct abbrev_info *abbrev;
11139 unsigned int bytes_read;
11140 unsigned int load_all = 0;
11141 int nesting_level = 1;
11142
11143 parent_die = NULL;
11144 last_die = NULL;
11145
11146 gdb_assert (cu->per_cu != NULL);
11147 if (cu->per_cu->load_all_dies)
11148 load_all = 1;
11149
11150 cu->partial_dies
11151 = htab_create_alloc_ex (cu->header.length / 12,
11152 partial_die_hash,
11153 partial_die_eq,
11154 NULL,
11155 &cu->comp_unit_obstack,
11156 hashtab_obstack_allocate,
11157 dummy_obstack_deallocate);
11158
11159 part_die = obstack_alloc (&cu->comp_unit_obstack,
11160 sizeof (struct partial_die_info));
11161
11162 while (1)
11163 {
11164 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
11165
11166 /* A NULL abbrev means the end of a series of children. */
11167 if (abbrev == NULL)
11168 {
11169 if (--nesting_level == 0)
11170 {
11171 /* PART_DIE was probably the last thing allocated on the
11172 comp_unit_obstack, so we could call obstack_free
11173 here. We don't do that because the waste is small,
11174 and will be cleaned up when we're done with this
11175 compilation unit. This way, we're also more robust
11176 against other users of the comp_unit_obstack. */
11177 return first_die;
11178 }
11179 info_ptr += bytes_read;
11180 last_die = parent_die;
11181 parent_die = parent_die->die_parent;
11182 continue;
11183 }
11184
11185 /* Check for template arguments. We never save these; if
11186 they're seen, we just mark the parent, and go on our way. */
11187 if (parent_die != NULL
11188 && cu->language == language_cplus
11189 && (abbrev->tag == DW_TAG_template_type_param
11190 || abbrev->tag == DW_TAG_template_value_param))
11191 {
11192 parent_die->has_template_arguments = 1;
11193
11194 if (!load_all)
11195 {
11196 /* We don't need a partial DIE for the template argument. */
11197 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
11198 continue;
11199 }
11200 }
11201
11202 /* We only recurse into c++ subprograms looking for template arguments.
11203 Skip their other children. */
11204 if (!load_all
11205 && cu->language == language_cplus
11206 && parent_die != NULL
11207 && parent_die->tag == DW_TAG_subprogram)
11208 {
11209 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
11210 continue;
11211 }
11212
11213 /* Check whether this DIE is interesting enough to save. Normally
11214 we would not be interested in members here, but there may be
11215 later variables referencing them via DW_AT_specification (for
11216 static members). */
11217 if (!load_all
11218 && !is_type_tag_for_partial (abbrev->tag)
11219 && abbrev->tag != DW_TAG_constant
11220 && abbrev->tag != DW_TAG_enumerator
11221 && abbrev->tag != DW_TAG_subprogram
11222 && abbrev->tag != DW_TAG_lexical_block
11223 && abbrev->tag != DW_TAG_variable
11224 && abbrev->tag != DW_TAG_namespace
11225 && abbrev->tag != DW_TAG_module
11226 && abbrev->tag != DW_TAG_member
11227 && abbrev->tag != DW_TAG_imported_unit)
11228 {
11229 /* Otherwise we skip to the next sibling, if any. */
11230 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
11231 continue;
11232 }
11233
11234 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
11235 info_ptr);
11236
11237 /* This two-pass algorithm for processing partial symbols has a
11238 high cost in cache pressure. Thus, handle some simple cases
11239 here which cover the majority of C partial symbols. DIEs
11240 which neither have specification tags in them, nor could have
11241 specification tags elsewhere pointing at them, can simply be
11242 processed and discarded.
11243
11244 This segment is also optional; scan_partial_symbols and
11245 add_partial_symbol will handle these DIEs if we chain
11246 them in normally. When compilers which do not emit large
11247 quantities of duplicate debug information are more common,
11248 this code can probably be removed. */
11249
11250 /* Any complete simple types at the top level (pretty much all
11251 of them, for a language without namespaces), can be processed
11252 directly. */
11253 if (parent_die == NULL
11254 && part_die->has_specification == 0
11255 && part_die->is_declaration == 0
11256 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
11257 || part_die->tag == DW_TAG_base_type
11258 || part_die->tag == DW_TAG_subrange_type))
11259 {
11260 if (building_psymtab && part_die->name != NULL)
11261 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
11262 VAR_DOMAIN, LOC_TYPEDEF,
11263 &objfile->static_psymbols,
11264 0, (CORE_ADDR) 0, cu->language, objfile);
11265 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
11266 continue;
11267 }
11268
11269 /* The exception for DW_TAG_typedef with has_children above is
11270 a workaround of GCC PR debug/47510. In the case of this complaint
11271 type_name_no_tag_or_error will error on such types later.
11272
11273 GDB skipped children of DW_TAG_typedef by the shortcut above and then
11274 it could not find the child DIEs referenced later, this is checked
11275 above. In correct DWARF DW_TAG_typedef should have no children. */
11276
11277 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
11278 complaint (&symfile_complaints,
11279 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
11280 "- DIE at 0x%x [in module %s]"),
11281 part_die->offset.sect_off, objfile->name);
11282
11283 /* If we're at the second level, and we're an enumerator, and
11284 our parent has no specification (meaning possibly lives in a
11285 namespace elsewhere), then we can add the partial symbol now
11286 instead of queueing it. */
11287 if (part_die->tag == DW_TAG_enumerator
11288 && parent_die != NULL
11289 && parent_die->die_parent == NULL
11290 && parent_die->tag == DW_TAG_enumeration_type
11291 && parent_die->has_specification == 0)
11292 {
11293 if (part_die->name == NULL)
11294 complaint (&symfile_complaints,
11295 _("malformed enumerator DIE ignored"));
11296 else if (building_psymtab)
11297 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
11298 VAR_DOMAIN, LOC_CONST,
11299 (cu->language == language_cplus
11300 || cu->language == language_java)
11301 ? &objfile->global_psymbols
11302 : &objfile->static_psymbols,
11303 0, (CORE_ADDR) 0, cu->language, objfile);
11304
11305 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
11306 continue;
11307 }
11308
11309 /* We'll save this DIE so link it in. */
11310 part_die->die_parent = parent_die;
11311 part_die->die_sibling = NULL;
11312 part_die->die_child = NULL;
11313
11314 if (last_die && last_die == parent_die)
11315 last_die->die_child = part_die;
11316 else if (last_die)
11317 last_die->die_sibling = part_die;
11318
11319 last_die = part_die;
11320
11321 if (first_die == NULL)
11322 first_die = part_die;
11323
11324 /* Maybe add the DIE to the hash table. Not all DIEs that we
11325 find interesting need to be in the hash table, because we
11326 also have the parent/sibling/child chains; only those that we
11327 might refer to by offset later during partial symbol reading.
11328
11329 For now this means things that might have be the target of a
11330 DW_AT_specification, DW_AT_abstract_origin, or
11331 DW_AT_extension. DW_AT_extension will refer only to
11332 namespaces; DW_AT_abstract_origin refers to functions (and
11333 many things under the function DIE, but we do not recurse
11334 into function DIEs during partial symbol reading) and
11335 possibly variables as well; DW_AT_specification refers to
11336 declarations. Declarations ought to have the DW_AT_declaration
11337 flag. It happens that GCC forgets to put it in sometimes, but
11338 only for functions, not for types.
11339
11340 Adding more things than necessary to the hash table is harmless
11341 except for the performance cost. Adding too few will result in
11342 wasted time in find_partial_die, when we reread the compilation
11343 unit with load_all_dies set. */
11344
11345 if (load_all
11346 || abbrev->tag == DW_TAG_constant
11347 || abbrev->tag == DW_TAG_subprogram
11348 || abbrev->tag == DW_TAG_variable
11349 || abbrev->tag == DW_TAG_namespace
11350 || part_die->is_declaration)
11351 {
11352 void **slot;
11353
11354 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
11355 part_die->offset.sect_off, INSERT);
11356 *slot = part_die;
11357 }
11358
11359 part_die = obstack_alloc (&cu->comp_unit_obstack,
11360 sizeof (struct partial_die_info));
11361
11362 /* For some DIEs we want to follow their children (if any). For C
11363 we have no reason to follow the children of structures; for other
11364 languages we have to, so that we can get at method physnames
11365 to infer fully qualified class names, for DW_AT_specification,
11366 and for C++ template arguments. For C++, we also look one level
11367 inside functions to find template arguments (if the name of the
11368 function does not already contain the template arguments).
11369
11370 For Ada, we need to scan the children of subprograms and lexical
11371 blocks as well because Ada allows the definition of nested
11372 entities that could be interesting for the debugger, such as
11373 nested subprograms for instance. */
11374 if (last_die->has_children
11375 && (load_all
11376 || last_die->tag == DW_TAG_namespace
11377 || last_die->tag == DW_TAG_module
11378 || last_die->tag == DW_TAG_enumeration_type
11379 || (cu->language == language_cplus
11380 && last_die->tag == DW_TAG_subprogram
11381 && (last_die->name == NULL
11382 || strchr (last_die->name, '<') == NULL))
11383 || (cu->language != language_c
11384 && (last_die->tag == DW_TAG_class_type
11385 || last_die->tag == DW_TAG_interface_type
11386 || last_die->tag == DW_TAG_structure_type
11387 || last_die->tag == DW_TAG_union_type))
11388 || (cu->language == language_ada
11389 && (last_die->tag == DW_TAG_subprogram
11390 || last_die->tag == DW_TAG_lexical_block))))
11391 {
11392 nesting_level++;
11393 parent_die = last_die;
11394 continue;
11395 }
11396
11397 /* Otherwise we skip to the next sibling, if any. */
11398 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
11399
11400 /* Back to the top, do it again. */
11401 }
11402 }
11403
11404 /* Read a minimal amount of information into the minimal die structure. */
11405
11406 static gdb_byte *
11407 read_partial_die (const struct die_reader_specs *reader,
11408 struct partial_die_info *part_die,
11409 struct abbrev_info *abbrev, unsigned int abbrev_len,
11410 gdb_byte *info_ptr)
11411 {
11412 struct dwarf2_cu *cu = reader->cu;
11413 struct objfile *objfile = cu->objfile;
11414 gdb_byte *buffer = reader->buffer;
11415 unsigned int i;
11416 struct attribute attr;
11417 int has_low_pc_attr = 0;
11418 int has_high_pc_attr = 0;
11419 int high_pc_relative = 0;
11420
11421 memset (part_die, 0, sizeof (struct partial_die_info));
11422
11423 part_die->offset.sect_off = info_ptr - buffer;
11424
11425 info_ptr += abbrev_len;
11426
11427 if (abbrev == NULL)
11428 return info_ptr;
11429
11430 part_die->tag = abbrev->tag;
11431 part_die->has_children = abbrev->has_children;
11432
11433 for (i = 0; i < abbrev->num_attrs; ++i)
11434 {
11435 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
11436
11437 /* Store the data if it is of an attribute we want to keep in a
11438 partial symbol table. */
11439 switch (attr.name)
11440 {
11441 case DW_AT_name:
11442 switch (part_die->tag)
11443 {
11444 case DW_TAG_compile_unit:
11445 case DW_TAG_partial_unit:
11446 case DW_TAG_type_unit:
11447 /* Compilation units have a DW_AT_name that is a filename, not
11448 a source language identifier. */
11449 case DW_TAG_enumeration_type:
11450 case DW_TAG_enumerator:
11451 /* These tags always have simple identifiers already; no need
11452 to canonicalize them. */
11453 part_die->name = DW_STRING (&attr);
11454 break;
11455 default:
11456 part_die->name
11457 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
11458 &objfile->objfile_obstack);
11459 break;
11460 }
11461 break;
11462 case DW_AT_linkage_name:
11463 case DW_AT_MIPS_linkage_name:
11464 /* Note that both forms of linkage name might appear. We
11465 assume they will be the same, and we only store the last
11466 one we see. */
11467 if (cu->language == language_ada)
11468 part_die->name = DW_STRING (&attr);
11469 part_die->linkage_name = DW_STRING (&attr);
11470 break;
11471 case DW_AT_low_pc:
11472 has_low_pc_attr = 1;
11473 part_die->lowpc = DW_ADDR (&attr);
11474 break;
11475 case DW_AT_high_pc:
11476 has_high_pc_attr = 1;
11477 if (attr.form == DW_FORM_addr
11478 || attr.form == DW_FORM_GNU_addr_index)
11479 part_die->highpc = DW_ADDR (&attr);
11480 else
11481 {
11482 high_pc_relative = 1;
11483 part_die->highpc = DW_UNSND (&attr);
11484 }
11485 break;
11486 case DW_AT_location:
11487 /* Support the .debug_loc offsets. */
11488 if (attr_form_is_block (&attr))
11489 {
11490 part_die->d.locdesc = DW_BLOCK (&attr);
11491 }
11492 else if (attr_form_is_section_offset (&attr))
11493 {
11494 dwarf2_complex_location_expr_complaint ();
11495 }
11496 else
11497 {
11498 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
11499 "partial symbol information");
11500 }
11501 break;
11502 case DW_AT_external:
11503 part_die->is_external = DW_UNSND (&attr);
11504 break;
11505 case DW_AT_declaration:
11506 part_die->is_declaration = DW_UNSND (&attr);
11507 break;
11508 case DW_AT_type:
11509 part_die->has_type = 1;
11510 break;
11511 case DW_AT_abstract_origin:
11512 case DW_AT_specification:
11513 case DW_AT_extension:
11514 part_die->has_specification = 1;
11515 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
11516 break;
11517 case DW_AT_sibling:
11518 /* Ignore absolute siblings, they might point outside of
11519 the current compile unit. */
11520 if (attr.form == DW_FORM_ref_addr)
11521 complaint (&symfile_complaints,
11522 _("ignoring absolute DW_AT_sibling"));
11523 else
11524 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
11525 break;
11526 case DW_AT_byte_size:
11527 part_die->has_byte_size = 1;
11528 break;
11529 case DW_AT_calling_convention:
11530 /* DWARF doesn't provide a way to identify a program's source-level
11531 entry point. DW_AT_calling_convention attributes are only meant
11532 to describe functions' calling conventions.
11533
11534 However, because it's a necessary piece of information in
11535 Fortran, and because DW_CC_program is the only piece of debugging
11536 information whose definition refers to a 'main program' at all,
11537 several compilers have begun marking Fortran main programs with
11538 DW_CC_program --- even when those functions use the standard
11539 calling conventions.
11540
11541 So until DWARF specifies a way to provide this information and
11542 compilers pick up the new representation, we'll support this
11543 practice. */
11544 if (DW_UNSND (&attr) == DW_CC_program
11545 && cu->language == language_fortran)
11546 {
11547 set_main_name (part_die->name);
11548
11549 /* As this DIE has a static linkage the name would be difficult
11550 to look up later. */
11551 language_of_main = language_fortran;
11552 }
11553 break;
11554 case DW_AT_inline:
11555 if (DW_UNSND (&attr) == DW_INL_inlined
11556 || DW_UNSND (&attr) == DW_INL_declared_inlined)
11557 part_die->may_be_inlined = 1;
11558 break;
11559
11560 case DW_AT_import:
11561 if (part_die->tag == DW_TAG_imported_unit)
11562 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
11563 break;
11564
11565 default:
11566 break;
11567 }
11568 }
11569
11570 if (high_pc_relative)
11571 part_die->highpc += part_die->lowpc;
11572
11573 if (has_low_pc_attr && has_high_pc_attr)
11574 {
11575 /* When using the GNU linker, .gnu.linkonce. sections are used to
11576 eliminate duplicate copies of functions and vtables and such.
11577 The linker will arbitrarily choose one and discard the others.
11578 The AT_*_pc values for such functions refer to local labels in
11579 these sections. If the section from that file was discarded, the
11580 labels are not in the output, so the relocs get a value of 0.
11581 If this is a discarded function, mark the pc bounds as invalid,
11582 so that GDB will ignore it. */
11583 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
11584 {
11585 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11586
11587 complaint (&symfile_complaints,
11588 _("DW_AT_low_pc %s is zero "
11589 "for DIE at 0x%x [in module %s]"),
11590 paddress (gdbarch, part_die->lowpc),
11591 part_die->offset.sect_off, objfile->name);
11592 }
11593 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
11594 else if (part_die->lowpc >= part_die->highpc)
11595 {
11596 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11597
11598 complaint (&symfile_complaints,
11599 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
11600 "for DIE at 0x%x [in module %s]"),
11601 paddress (gdbarch, part_die->lowpc),
11602 paddress (gdbarch, part_die->highpc),
11603 part_die->offset.sect_off, objfile->name);
11604 }
11605 else
11606 part_die->has_pc_info = 1;
11607 }
11608
11609 return info_ptr;
11610 }
11611
11612 /* Find a cached partial DIE at OFFSET in CU. */
11613
11614 static struct partial_die_info *
11615 find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
11616 {
11617 struct partial_die_info *lookup_die = NULL;
11618 struct partial_die_info part_die;
11619
11620 part_die.offset = offset;
11621 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
11622 offset.sect_off);
11623
11624 return lookup_die;
11625 }
11626
11627 /* Find a partial DIE at OFFSET, which may or may not be in CU,
11628 except in the case of .debug_types DIEs which do not reference
11629 outside their CU (they do however referencing other types via
11630 DW_FORM_ref_sig8). */
11631
11632 static struct partial_die_info *
11633 find_partial_die (sect_offset offset, struct dwarf2_cu *cu)
11634 {
11635 struct objfile *objfile = cu->objfile;
11636 struct dwarf2_per_cu_data *per_cu = NULL;
11637 struct partial_die_info *pd = NULL;
11638
11639 if (offset_in_cu_p (&cu->header, offset))
11640 {
11641 pd = find_partial_die_in_comp_unit (offset, cu);
11642 if (pd != NULL)
11643 return pd;
11644 /* We missed recording what we needed.
11645 Load all dies and try again. */
11646 per_cu = cu->per_cu;
11647 }
11648 else
11649 {
11650 /* TUs don't reference other CUs/TUs (except via type signatures). */
11651 if (cu->per_cu->is_debug_types)
11652 {
11653 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
11654 " external reference to offset 0x%lx [in module %s].\n"),
11655 (long) cu->header.offset.sect_off, (long) offset.sect_off,
11656 bfd_get_filename (objfile->obfd));
11657 }
11658 per_cu = dwarf2_find_containing_comp_unit (offset, objfile);
11659
11660 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
11661 load_partial_comp_unit (per_cu);
11662
11663 per_cu->cu->last_used = 0;
11664 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
11665 }
11666
11667 /* If we didn't find it, and not all dies have been loaded,
11668 load them all and try again. */
11669
11670 if (pd == NULL && per_cu->load_all_dies == 0)
11671 {
11672 per_cu->load_all_dies = 1;
11673
11674 /* This is nasty. When we reread the DIEs, somewhere up the call chain
11675 THIS_CU->cu may already be in use. So we can't just free it and
11676 replace its DIEs with the ones we read in. Instead, we leave those
11677 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
11678 and clobber THIS_CU->cu->partial_dies with the hash table for the new
11679 set. */
11680 load_partial_comp_unit (per_cu);
11681
11682 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
11683 }
11684
11685 if (pd == NULL)
11686 internal_error (__FILE__, __LINE__,
11687 _("could not find partial DIE 0x%x "
11688 "in cache [from module %s]\n"),
11689 offset.sect_off, bfd_get_filename (objfile->obfd));
11690 return pd;
11691 }
11692
11693 /* See if we can figure out if the class lives in a namespace. We do
11694 this by looking for a member function; its demangled name will
11695 contain namespace info, if there is any. */
11696
11697 static void
11698 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
11699 struct dwarf2_cu *cu)
11700 {
11701 /* NOTE: carlton/2003-10-07: Getting the info this way changes
11702 what template types look like, because the demangler
11703 frequently doesn't give the same name as the debug info. We
11704 could fix this by only using the demangled name to get the
11705 prefix (but see comment in read_structure_type). */
11706
11707 struct partial_die_info *real_pdi;
11708 struct partial_die_info *child_pdi;
11709
11710 /* If this DIE (this DIE's specification, if any) has a parent, then
11711 we should not do this. We'll prepend the parent's fully qualified
11712 name when we create the partial symbol. */
11713
11714 real_pdi = struct_pdi;
11715 while (real_pdi->has_specification)
11716 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
11717
11718 if (real_pdi->die_parent != NULL)
11719 return;
11720
11721 for (child_pdi = struct_pdi->die_child;
11722 child_pdi != NULL;
11723 child_pdi = child_pdi->die_sibling)
11724 {
11725 if (child_pdi->tag == DW_TAG_subprogram
11726 && child_pdi->linkage_name != NULL)
11727 {
11728 char *actual_class_name
11729 = language_class_name_from_physname (cu->language_defn,
11730 child_pdi->linkage_name);
11731 if (actual_class_name != NULL)
11732 {
11733 struct_pdi->name
11734 = obsavestring (actual_class_name,
11735 strlen (actual_class_name),
11736 &cu->objfile->objfile_obstack);
11737 xfree (actual_class_name);
11738 }
11739 break;
11740 }
11741 }
11742 }
11743
11744 /* Adjust PART_DIE before generating a symbol for it. This function
11745 may set the is_external flag or change the DIE's name. */
11746
11747 static void
11748 fixup_partial_die (struct partial_die_info *part_die,
11749 struct dwarf2_cu *cu)
11750 {
11751 /* Once we've fixed up a die, there's no point in doing so again.
11752 This also avoids a memory leak if we were to call
11753 guess_partial_die_structure_name multiple times. */
11754 if (part_die->fixup_called)
11755 return;
11756
11757 /* If we found a reference attribute and the DIE has no name, try
11758 to find a name in the referred to DIE. */
11759
11760 if (part_die->name == NULL && part_die->has_specification)
11761 {
11762 struct partial_die_info *spec_die;
11763
11764 spec_die = find_partial_die (part_die->spec_offset, cu);
11765
11766 fixup_partial_die (spec_die, cu);
11767
11768 if (spec_die->name)
11769 {
11770 part_die->name = spec_die->name;
11771
11772 /* Copy DW_AT_external attribute if it is set. */
11773 if (spec_die->is_external)
11774 part_die->is_external = spec_die->is_external;
11775 }
11776 }
11777
11778 /* Set default names for some unnamed DIEs. */
11779
11780 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
11781 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
11782
11783 /* If there is no parent die to provide a namespace, and there are
11784 children, see if we can determine the namespace from their linkage
11785 name. */
11786 if (cu->language == language_cplus
11787 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
11788 && part_die->die_parent == NULL
11789 && part_die->has_children
11790 && (part_die->tag == DW_TAG_class_type
11791 || part_die->tag == DW_TAG_structure_type
11792 || part_die->tag == DW_TAG_union_type))
11793 guess_partial_die_structure_name (part_die, cu);
11794
11795 /* GCC might emit a nameless struct or union that has a linkage
11796 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
11797 if (part_die->name == NULL
11798 && (part_die->tag == DW_TAG_class_type
11799 || part_die->tag == DW_TAG_interface_type
11800 || part_die->tag == DW_TAG_structure_type
11801 || part_die->tag == DW_TAG_union_type)
11802 && part_die->linkage_name != NULL)
11803 {
11804 char *demangled;
11805
11806 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
11807 if (demangled)
11808 {
11809 const char *base;
11810
11811 /* Strip any leading namespaces/classes, keep only the base name.
11812 DW_AT_name for named DIEs does not contain the prefixes. */
11813 base = strrchr (demangled, ':');
11814 if (base && base > demangled && base[-1] == ':')
11815 base++;
11816 else
11817 base = demangled;
11818
11819 part_die->name = obsavestring (base, strlen (base),
11820 &cu->objfile->objfile_obstack);
11821 xfree (demangled);
11822 }
11823 }
11824
11825 part_die->fixup_called = 1;
11826 }
11827
11828 /* Read an attribute value described by an attribute form. */
11829
11830 static gdb_byte *
11831 read_attribute_value (const struct die_reader_specs *reader,
11832 struct attribute *attr, unsigned form,
11833 gdb_byte *info_ptr)
11834 {
11835 struct dwarf2_cu *cu = reader->cu;
11836 bfd *abfd = reader->abfd;
11837 struct comp_unit_head *cu_header = &cu->header;
11838 unsigned int bytes_read;
11839 struct dwarf_block *blk;
11840
11841 attr->form = form;
11842 switch (form)
11843 {
11844 case DW_FORM_ref_addr:
11845 if (cu->header.version == 2)
11846 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
11847 else
11848 DW_UNSND (attr) = read_offset (abfd, info_ptr,
11849 &cu->header, &bytes_read);
11850 info_ptr += bytes_read;
11851 break;
11852 case DW_FORM_addr:
11853 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
11854 info_ptr += bytes_read;
11855 break;
11856 case DW_FORM_block2:
11857 blk = dwarf_alloc_block (cu);
11858 blk->size = read_2_bytes (abfd, info_ptr);
11859 info_ptr += 2;
11860 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11861 info_ptr += blk->size;
11862 DW_BLOCK (attr) = blk;
11863 break;
11864 case DW_FORM_block4:
11865 blk = dwarf_alloc_block (cu);
11866 blk->size = read_4_bytes (abfd, info_ptr);
11867 info_ptr += 4;
11868 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11869 info_ptr += blk->size;
11870 DW_BLOCK (attr) = blk;
11871 break;
11872 case DW_FORM_data2:
11873 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
11874 info_ptr += 2;
11875 break;
11876 case DW_FORM_data4:
11877 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
11878 info_ptr += 4;
11879 break;
11880 case DW_FORM_data8:
11881 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
11882 info_ptr += 8;
11883 break;
11884 case DW_FORM_sec_offset:
11885 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
11886 info_ptr += bytes_read;
11887 break;
11888 case DW_FORM_string:
11889 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
11890 DW_STRING_IS_CANONICAL (attr) = 0;
11891 info_ptr += bytes_read;
11892 break;
11893 case DW_FORM_strp:
11894 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
11895 &bytes_read);
11896 DW_STRING_IS_CANONICAL (attr) = 0;
11897 info_ptr += bytes_read;
11898 break;
11899 case DW_FORM_exprloc:
11900 case DW_FORM_block:
11901 blk = dwarf_alloc_block (cu);
11902 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11903 info_ptr += bytes_read;
11904 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11905 info_ptr += blk->size;
11906 DW_BLOCK (attr) = blk;
11907 break;
11908 case DW_FORM_block1:
11909 blk = dwarf_alloc_block (cu);
11910 blk->size = read_1_byte (abfd, info_ptr);
11911 info_ptr += 1;
11912 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11913 info_ptr += blk->size;
11914 DW_BLOCK (attr) = blk;
11915 break;
11916 case DW_FORM_data1:
11917 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
11918 info_ptr += 1;
11919 break;
11920 case DW_FORM_flag:
11921 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
11922 info_ptr += 1;
11923 break;
11924 case DW_FORM_flag_present:
11925 DW_UNSND (attr) = 1;
11926 break;
11927 case DW_FORM_sdata:
11928 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
11929 info_ptr += bytes_read;
11930 break;
11931 case DW_FORM_udata:
11932 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11933 info_ptr += bytes_read;
11934 break;
11935 case DW_FORM_ref1:
11936 DW_UNSND (attr) = (cu->header.offset.sect_off
11937 + read_1_byte (abfd, info_ptr));
11938 info_ptr += 1;
11939 break;
11940 case DW_FORM_ref2:
11941 DW_UNSND (attr) = (cu->header.offset.sect_off
11942 + read_2_bytes (abfd, info_ptr));
11943 info_ptr += 2;
11944 break;
11945 case DW_FORM_ref4:
11946 DW_UNSND (attr) = (cu->header.offset.sect_off
11947 + read_4_bytes (abfd, info_ptr));
11948 info_ptr += 4;
11949 break;
11950 case DW_FORM_ref8:
11951 DW_UNSND (attr) = (cu->header.offset.sect_off
11952 + read_8_bytes (abfd, info_ptr));
11953 info_ptr += 8;
11954 break;
11955 case DW_FORM_ref_sig8:
11956 /* Convert the signature to something we can record in DW_UNSND
11957 for later lookup.
11958 NOTE: This is NULL if the type wasn't found. */
11959 DW_SIGNATURED_TYPE (attr) =
11960 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
11961 info_ptr += 8;
11962 break;
11963 case DW_FORM_ref_udata:
11964 DW_UNSND (attr) = (cu->header.offset.sect_off
11965 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
11966 info_ptr += bytes_read;
11967 break;
11968 case DW_FORM_indirect:
11969 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11970 info_ptr += bytes_read;
11971 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
11972 break;
11973 case DW_FORM_GNU_addr_index:
11974 if (reader->dwo_file == NULL)
11975 {
11976 /* For now flag a hard error.
11977 Later we can turn this into a complaint. */
11978 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
11979 dwarf_form_name (form),
11980 bfd_get_filename (abfd));
11981 }
11982 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
11983 info_ptr += bytes_read;
11984 break;
11985 case DW_FORM_GNU_str_index:
11986 if (reader->dwo_file == NULL)
11987 {
11988 /* For now flag a hard error.
11989 Later we can turn this into a complaint if warranted. */
11990 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
11991 dwarf_form_name (form),
11992 bfd_get_filename (abfd));
11993 }
11994 {
11995 ULONGEST str_index =
11996 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11997
11998 DW_STRING (attr) = read_str_index (reader, cu, str_index);
11999 DW_STRING_IS_CANONICAL (attr) = 0;
12000 info_ptr += bytes_read;
12001 }
12002 break;
12003 default:
12004 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
12005 dwarf_form_name (form),
12006 bfd_get_filename (abfd));
12007 }
12008
12009 /* We have seen instances where the compiler tried to emit a byte
12010 size attribute of -1 which ended up being encoded as an unsigned
12011 0xffffffff. Although 0xffffffff is technically a valid size value,
12012 an object of this size seems pretty unlikely so we can relatively
12013 safely treat these cases as if the size attribute was invalid and
12014 treat them as zero by default. */
12015 if (attr->name == DW_AT_byte_size
12016 && form == DW_FORM_data4
12017 && DW_UNSND (attr) >= 0xffffffff)
12018 {
12019 complaint
12020 (&symfile_complaints,
12021 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
12022 hex_string (DW_UNSND (attr)));
12023 DW_UNSND (attr) = 0;
12024 }
12025
12026 return info_ptr;
12027 }
12028
12029 /* Read an attribute described by an abbreviated attribute. */
12030
12031 static gdb_byte *
12032 read_attribute (const struct die_reader_specs *reader,
12033 struct attribute *attr, struct attr_abbrev *abbrev,
12034 gdb_byte *info_ptr)
12035 {
12036 attr->name = abbrev->name;
12037 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
12038 }
12039
12040 /* Read dwarf information from a buffer. */
12041
12042 static unsigned int
12043 read_1_byte (bfd *abfd, gdb_byte *buf)
12044 {
12045 return bfd_get_8 (abfd, buf);
12046 }
12047
12048 static int
12049 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
12050 {
12051 return bfd_get_signed_8 (abfd, buf);
12052 }
12053
12054 static unsigned int
12055 read_2_bytes (bfd *abfd, gdb_byte *buf)
12056 {
12057 return bfd_get_16 (abfd, buf);
12058 }
12059
12060 static int
12061 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
12062 {
12063 return bfd_get_signed_16 (abfd, buf);
12064 }
12065
12066 static unsigned int
12067 read_4_bytes (bfd *abfd, gdb_byte *buf)
12068 {
12069 return bfd_get_32 (abfd, buf);
12070 }
12071
12072 static int
12073 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
12074 {
12075 return bfd_get_signed_32 (abfd, buf);
12076 }
12077
12078 static ULONGEST
12079 read_8_bytes (bfd *abfd, gdb_byte *buf)
12080 {
12081 return bfd_get_64 (abfd, buf);
12082 }
12083
12084 static CORE_ADDR
12085 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
12086 unsigned int *bytes_read)
12087 {
12088 struct comp_unit_head *cu_header = &cu->header;
12089 CORE_ADDR retval = 0;
12090
12091 if (cu_header->signed_addr_p)
12092 {
12093 switch (cu_header->addr_size)
12094 {
12095 case 2:
12096 retval = bfd_get_signed_16 (abfd, buf);
12097 break;
12098 case 4:
12099 retval = bfd_get_signed_32 (abfd, buf);
12100 break;
12101 case 8:
12102 retval = bfd_get_signed_64 (abfd, buf);
12103 break;
12104 default:
12105 internal_error (__FILE__, __LINE__,
12106 _("read_address: bad switch, signed [in module %s]"),
12107 bfd_get_filename (abfd));
12108 }
12109 }
12110 else
12111 {
12112 switch (cu_header->addr_size)
12113 {
12114 case 2:
12115 retval = bfd_get_16 (abfd, buf);
12116 break;
12117 case 4:
12118 retval = bfd_get_32 (abfd, buf);
12119 break;
12120 case 8:
12121 retval = bfd_get_64 (abfd, buf);
12122 break;
12123 default:
12124 internal_error (__FILE__, __LINE__,
12125 _("read_address: bad switch, "
12126 "unsigned [in module %s]"),
12127 bfd_get_filename (abfd));
12128 }
12129 }
12130
12131 *bytes_read = cu_header->addr_size;
12132 return retval;
12133 }
12134
12135 /* Read the initial length from a section. The (draft) DWARF 3
12136 specification allows the initial length to take up either 4 bytes
12137 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
12138 bytes describe the length and all offsets will be 8 bytes in length
12139 instead of 4.
12140
12141 An older, non-standard 64-bit format is also handled by this
12142 function. The older format in question stores the initial length
12143 as an 8-byte quantity without an escape value. Lengths greater
12144 than 2^32 aren't very common which means that the initial 4 bytes
12145 is almost always zero. Since a length value of zero doesn't make
12146 sense for the 32-bit format, this initial zero can be considered to
12147 be an escape value which indicates the presence of the older 64-bit
12148 format. As written, the code can't detect (old format) lengths
12149 greater than 4GB. If it becomes necessary to handle lengths
12150 somewhat larger than 4GB, we could allow other small values (such
12151 as the non-sensical values of 1, 2, and 3) to also be used as
12152 escape values indicating the presence of the old format.
12153
12154 The value returned via bytes_read should be used to increment the
12155 relevant pointer after calling read_initial_length().
12156
12157 [ Note: read_initial_length() and read_offset() are based on the
12158 document entitled "DWARF Debugging Information Format", revision
12159 3, draft 8, dated November 19, 2001. This document was obtained
12160 from:
12161
12162 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
12163
12164 This document is only a draft and is subject to change. (So beware.)
12165
12166 Details regarding the older, non-standard 64-bit format were
12167 determined empirically by examining 64-bit ELF files produced by
12168 the SGI toolchain on an IRIX 6.5 machine.
12169
12170 - Kevin, July 16, 2002
12171 ] */
12172
12173 static LONGEST
12174 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
12175 {
12176 LONGEST length = bfd_get_32 (abfd, buf);
12177
12178 if (length == 0xffffffff)
12179 {
12180 length = bfd_get_64 (abfd, buf + 4);
12181 *bytes_read = 12;
12182 }
12183 else if (length == 0)
12184 {
12185 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
12186 length = bfd_get_64 (abfd, buf);
12187 *bytes_read = 8;
12188 }
12189 else
12190 {
12191 *bytes_read = 4;
12192 }
12193
12194 return length;
12195 }
12196
12197 /* Cover function for read_initial_length.
12198 Returns the length of the object at BUF, and stores the size of the
12199 initial length in *BYTES_READ and stores the size that offsets will be in
12200 *OFFSET_SIZE.
12201 If the initial length size is not equivalent to that specified in
12202 CU_HEADER then issue a complaint.
12203 This is useful when reading non-comp-unit headers. */
12204
12205 static LONGEST
12206 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
12207 const struct comp_unit_head *cu_header,
12208 unsigned int *bytes_read,
12209 unsigned int *offset_size)
12210 {
12211 LONGEST length = read_initial_length (abfd, buf, bytes_read);
12212
12213 gdb_assert (cu_header->initial_length_size == 4
12214 || cu_header->initial_length_size == 8
12215 || cu_header->initial_length_size == 12);
12216
12217 if (cu_header->initial_length_size != *bytes_read)
12218 complaint (&symfile_complaints,
12219 _("intermixed 32-bit and 64-bit DWARF sections"));
12220
12221 *offset_size = (*bytes_read == 4) ? 4 : 8;
12222 return length;
12223 }
12224
12225 /* Read an offset from the data stream. The size of the offset is
12226 given by cu_header->offset_size. */
12227
12228 static LONGEST
12229 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
12230 unsigned int *bytes_read)
12231 {
12232 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
12233
12234 *bytes_read = cu_header->offset_size;
12235 return offset;
12236 }
12237
12238 /* Read an offset from the data stream. */
12239
12240 static LONGEST
12241 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
12242 {
12243 LONGEST retval = 0;
12244
12245 switch (offset_size)
12246 {
12247 case 4:
12248 retval = bfd_get_32 (abfd, buf);
12249 break;
12250 case 8:
12251 retval = bfd_get_64 (abfd, buf);
12252 break;
12253 default:
12254 internal_error (__FILE__, __LINE__,
12255 _("read_offset_1: bad switch [in module %s]"),
12256 bfd_get_filename (abfd));
12257 }
12258
12259 return retval;
12260 }
12261
12262 static gdb_byte *
12263 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
12264 {
12265 /* If the size of a host char is 8 bits, we can return a pointer
12266 to the buffer, otherwise we have to copy the data to a buffer
12267 allocated on the temporary obstack. */
12268 gdb_assert (HOST_CHAR_BIT == 8);
12269 return buf;
12270 }
12271
12272 static char *
12273 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
12274 {
12275 /* If the size of a host char is 8 bits, we can return a pointer
12276 to the string, otherwise we have to copy the string to a buffer
12277 allocated on the temporary obstack. */
12278 gdb_assert (HOST_CHAR_BIT == 8);
12279 if (*buf == '\0')
12280 {
12281 *bytes_read_ptr = 1;
12282 return NULL;
12283 }
12284 *bytes_read_ptr = strlen ((char *) buf) + 1;
12285 return (char *) buf;
12286 }
12287
12288 static char *
12289 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
12290 {
12291 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
12292 if (dwarf2_per_objfile->str.buffer == NULL)
12293 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
12294 bfd_get_filename (abfd));
12295 if (str_offset >= dwarf2_per_objfile->str.size)
12296 error (_("DW_FORM_strp pointing outside of "
12297 ".debug_str section [in module %s]"),
12298 bfd_get_filename (abfd));
12299 gdb_assert (HOST_CHAR_BIT == 8);
12300 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
12301 return NULL;
12302 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
12303 }
12304
12305 static char *
12306 read_indirect_string (bfd *abfd, gdb_byte *buf,
12307 const struct comp_unit_head *cu_header,
12308 unsigned int *bytes_read_ptr)
12309 {
12310 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
12311
12312 return read_indirect_string_at_offset (abfd, str_offset);
12313 }
12314
12315 static ULONGEST
12316 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
12317 {
12318 ULONGEST result;
12319 unsigned int num_read;
12320 int i, shift;
12321 unsigned char byte;
12322
12323 result = 0;
12324 shift = 0;
12325 num_read = 0;
12326 i = 0;
12327 while (1)
12328 {
12329 byte = bfd_get_8 (abfd, buf);
12330 buf++;
12331 num_read++;
12332 result |= ((ULONGEST) (byte & 127) << shift);
12333 if ((byte & 128) == 0)
12334 {
12335 break;
12336 }
12337 shift += 7;
12338 }
12339 *bytes_read_ptr = num_read;
12340 return result;
12341 }
12342
12343 static LONGEST
12344 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
12345 {
12346 LONGEST result;
12347 int i, shift, num_read;
12348 unsigned char byte;
12349
12350 result = 0;
12351 shift = 0;
12352 num_read = 0;
12353 i = 0;
12354 while (1)
12355 {
12356 byte = bfd_get_8 (abfd, buf);
12357 buf++;
12358 num_read++;
12359 result |= ((LONGEST) (byte & 127) << shift);
12360 shift += 7;
12361 if ((byte & 128) == 0)
12362 {
12363 break;
12364 }
12365 }
12366 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12367 result |= -(((LONGEST) 1) << shift);
12368 *bytes_read_ptr = num_read;
12369 return result;
12370 }
12371
12372 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
12373 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
12374 ADDR_SIZE is the size of addresses from the CU header. */
12375
12376 static CORE_ADDR
12377 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
12378 {
12379 struct objfile *objfile = dwarf2_per_objfile->objfile;
12380 bfd *abfd = objfile->obfd;
12381 const gdb_byte *info_ptr;
12382
12383 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
12384 if (dwarf2_per_objfile->addr.buffer == NULL)
12385 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
12386 objfile->name);
12387 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
12388 error (_("DW_FORM_addr_index pointing outside of "
12389 ".debug_addr section [in module %s]"),
12390 objfile->name);
12391 info_ptr = (dwarf2_per_objfile->addr.buffer
12392 + addr_base + addr_index * addr_size);
12393 if (addr_size == 4)
12394 return bfd_get_32 (abfd, info_ptr);
12395 else
12396 return bfd_get_64 (abfd, info_ptr);
12397 }
12398
12399 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
12400
12401 static CORE_ADDR
12402 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
12403 {
12404 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
12405 }
12406
12407 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
12408
12409 static CORE_ADDR
12410 read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
12411 unsigned int *bytes_read)
12412 {
12413 bfd *abfd = cu->objfile->obfd;
12414 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
12415
12416 return read_addr_index (cu, addr_index);
12417 }
12418
12419 /* Data structure to pass results from dwarf2_read_addr_index_reader
12420 back to dwarf2_read_addr_index. */
12421
12422 struct dwarf2_read_addr_index_data
12423 {
12424 ULONGEST addr_base;
12425 int addr_size;
12426 };
12427
12428 /* die_reader_func for dwarf2_read_addr_index. */
12429
12430 static void
12431 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
12432 gdb_byte *info_ptr,
12433 struct die_info *comp_unit_die,
12434 int has_children,
12435 void *data)
12436 {
12437 struct dwarf2_cu *cu = reader->cu;
12438 struct dwarf2_read_addr_index_data *aidata =
12439 (struct dwarf2_read_addr_index_data *) data;
12440
12441 aidata->addr_base = cu->addr_base;
12442 aidata->addr_size = cu->header.addr_size;
12443 }
12444
12445 /* Given an index in .debug_addr, fetch the value.
12446 NOTE: This can be called during dwarf expression evaluation,
12447 long after the debug information has been read, and thus per_cu->cu
12448 may no longer exist. */
12449
12450 CORE_ADDR
12451 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
12452 unsigned int addr_index)
12453 {
12454 struct objfile *objfile = per_cu->objfile;
12455 struct dwarf2_cu *cu = per_cu->cu;
12456 ULONGEST addr_base;
12457 int addr_size;
12458
12459 /* This is intended to be called from outside this file. */
12460 dw2_setup (objfile);
12461
12462 /* We need addr_base and addr_size.
12463 If we don't have PER_CU->cu, we have to get it.
12464 Nasty, but the alternative is storing the needed info in PER_CU,
12465 which at this point doesn't seem justified: it's not clear how frequently
12466 it would get used and it would increase the size of every PER_CU.
12467 Entry points like dwarf2_per_cu_addr_size do a similar thing
12468 so we're not in uncharted territory here.
12469 Alas we need to be a bit more complicated as addr_base is contained
12470 in the DIE.
12471
12472 We don't need to read the entire CU(/TU).
12473 We just need the header and top level die.
12474 IWBN to use the aging mechanism to let us lazily later discard the CU.
12475 See however init_cutu_and_read_dies_simple. */
12476
12477 if (cu != NULL)
12478 {
12479 addr_base = cu->addr_base;
12480 addr_size = cu->header.addr_size;
12481 }
12482 else
12483 {
12484 struct dwarf2_read_addr_index_data aidata;
12485
12486 init_cutu_and_read_dies_simple (per_cu, dwarf2_read_addr_index_reader,
12487 &aidata);
12488 addr_base = aidata.addr_base;
12489 addr_size = aidata.addr_size;
12490 }
12491
12492 return read_addr_index_1 (addr_index, addr_base, addr_size);
12493 }
12494
12495 /* Given a DW_AT_str_index, fetch the string. */
12496
12497 static char *
12498 read_str_index (const struct die_reader_specs *reader,
12499 struct dwarf2_cu *cu, ULONGEST str_index)
12500 {
12501 struct objfile *objfile = dwarf2_per_objfile->objfile;
12502 const char *dwo_name = objfile->name;
12503 bfd *abfd = objfile->obfd;
12504 struct dwo_sections *sections = &reader->dwo_file->sections;
12505 gdb_byte *info_ptr;
12506 ULONGEST str_offset;
12507
12508 dwarf2_read_section (objfile, &sections->str);
12509 dwarf2_read_section (objfile, &sections->str_offsets);
12510 if (sections->str.buffer == NULL)
12511 error (_("DW_FORM_str_index used without .debug_str.dwo section"
12512 " in CU at offset 0x%lx [in module %s]"),
12513 (long) cu->header.offset.sect_off, dwo_name);
12514 if (sections->str_offsets.buffer == NULL)
12515 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
12516 " in CU at offset 0x%lx [in module %s]"),
12517 (long) cu->header.offset.sect_off, dwo_name);
12518 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
12519 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
12520 " section in CU at offset 0x%lx [in module %s]"),
12521 (long) cu->header.offset.sect_off, dwo_name);
12522 info_ptr = (sections->str_offsets.buffer
12523 + str_index * cu->header.offset_size);
12524 if (cu->header.offset_size == 4)
12525 str_offset = bfd_get_32 (abfd, info_ptr);
12526 else
12527 str_offset = bfd_get_64 (abfd, info_ptr);
12528 if (str_offset >= sections->str.size)
12529 error (_("Offset from DW_FORM_str_index pointing outside of"
12530 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
12531 (long) cu->header.offset.sect_off, dwo_name);
12532 return (char *) (sections->str.buffer + str_offset);
12533 }
12534
12535 /* Return the length of an LEB128 number in BUF. */
12536
12537 static int
12538 leb128_size (const gdb_byte *buf)
12539 {
12540 const gdb_byte *begin = buf;
12541 gdb_byte byte;
12542
12543 while (1)
12544 {
12545 byte = *buf++;
12546 if ((byte & 128) == 0)
12547 return buf - begin;
12548 }
12549 }
12550
12551 static void
12552 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
12553 {
12554 switch (lang)
12555 {
12556 case DW_LANG_C89:
12557 case DW_LANG_C99:
12558 case DW_LANG_C:
12559 cu->language = language_c;
12560 break;
12561 case DW_LANG_C_plus_plus:
12562 cu->language = language_cplus;
12563 break;
12564 case DW_LANG_D:
12565 cu->language = language_d;
12566 break;
12567 case DW_LANG_Fortran77:
12568 case DW_LANG_Fortran90:
12569 case DW_LANG_Fortran95:
12570 cu->language = language_fortran;
12571 break;
12572 case DW_LANG_Go:
12573 cu->language = language_go;
12574 break;
12575 case DW_LANG_Mips_Assembler:
12576 cu->language = language_asm;
12577 break;
12578 case DW_LANG_Java:
12579 cu->language = language_java;
12580 break;
12581 case DW_LANG_Ada83:
12582 case DW_LANG_Ada95:
12583 cu->language = language_ada;
12584 break;
12585 case DW_LANG_Modula2:
12586 cu->language = language_m2;
12587 break;
12588 case DW_LANG_Pascal83:
12589 cu->language = language_pascal;
12590 break;
12591 case DW_LANG_ObjC:
12592 cu->language = language_objc;
12593 break;
12594 case DW_LANG_Cobol74:
12595 case DW_LANG_Cobol85:
12596 default:
12597 cu->language = language_minimal;
12598 break;
12599 }
12600 cu->language_defn = language_def (cu->language);
12601 }
12602
12603 /* Return the named attribute or NULL if not there. */
12604
12605 static struct attribute *
12606 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
12607 {
12608 for (;;)
12609 {
12610 unsigned int i;
12611 struct attribute *spec = NULL;
12612
12613 for (i = 0; i < die->num_attrs; ++i)
12614 {
12615 if (die->attrs[i].name == name)
12616 return &die->attrs[i];
12617 if (die->attrs[i].name == DW_AT_specification
12618 || die->attrs[i].name == DW_AT_abstract_origin)
12619 spec = &die->attrs[i];
12620 }
12621
12622 if (!spec)
12623 break;
12624
12625 die = follow_die_ref (die, spec, &cu);
12626 }
12627
12628 return NULL;
12629 }
12630
12631 /* Return the named attribute or NULL if not there,
12632 but do not follow DW_AT_specification, etc.
12633 This is for use in contexts where we're reading .debug_types dies.
12634 Following DW_AT_specification, DW_AT_abstract_origin will take us
12635 back up the chain, and we want to go down. */
12636
12637 static struct attribute *
12638 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
12639 struct dwarf2_cu *cu)
12640 {
12641 unsigned int i;
12642
12643 for (i = 0; i < die->num_attrs; ++i)
12644 if (die->attrs[i].name == name)
12645 return &die->attrs[i];
12646
12647 return NULL;
12648 }
12649
12650 /* Return non-zero iff the attribute NAME is defined for the given DIE,
12651 and holds a non-zero value. This function should only be used for
12652 DW_FORM_flag or DW_FORM_flag_present attributes. */
12653
12654 static int
12655 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
12656 {
12657 struct attribute *attr = dwarf2_attr (die, name, cu);
12658
12659 return (attr && DW_UNSND (attr));
12660 }
12661
12662 static int
12663 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
12664 {
12665 /* A DIE is a declaration if it has a DW_AT_declaration attribute
12666 which value is non-zero. However, we have to be careful with
12667 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
12668 (via dwarf2_flag_true_p) follows this attribute. So we may
12669 end up accidently finding a declaration attribute that belongs
12670 to a different DIE referenced by the specification attribute,
12671 even though the given DIE does not have a declaration attribute. */
12672 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
12673 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
12674 }
12675
12676 /* Return the die giving the specification for DIE, if there is
12677 one. *SPEC_CU is the CU containing DIE on input, and the CU
12678 containing the return value on output. If there is no
12679 specification, but there is an abstract origin, that is
12680 returned. */
12681
12682 static struct die_info *
12683 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
12684 {
12685 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
12686 *spec_cu);
12687
12688 if (spec_attr == NULL)
12689 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
12690
12691 if (spec_attr == NULL)
12692 return NULL;
12693 else
12694 return follow_die_ref (die, spec_attr, spec_cu);
12695 }
12696
12697 /* Free the line_header structure *LH, and any arrays and strings it
12698 refers to.
12699 NOTE: This is also used as a "cleanup" function. */
12700
12701 static void
12702 free_line_header (struct line_header *lh)
12703 {
12704 if (lh->standard_opcode_lengths)
12705 xfree (lh->standard_opcode_lengths);
12706
12707 /* Remember that all the lh->file_names[i].name pointers are
12708 pointers into debug_line_buffer, and don't need to be freed. */
12709 if (lh->file_names)
12710 xfree (lh->file_names);
12711
12712 /* Similarly for the include directory names. */
12713 if (lh->include_dirs)
12714 xfree (lh->include_dirs);
12715
12716 xfree (lh);
12717 }
12718
12719 /* Add an entry to LH's include directory table. */
12720
12721 static void
12722 add_include_dir (struct line_header *lh, char *include_dir)
12723 {
12724 /* Grow the array if necessary. */
12725 if (lh->include_dirs_size == 0)
12726 {
12727 lh->include_dirs_size = 1; /* for testing */
12728 lh->include_dirs = xmalloc (lh->include_dirs_size
12729 * sizeof (*lh->include_dirs));
12730 }
12731 else if (lh->num_include_dirs >= lh->include_dirs_size)
12732 {
12733 lh->include_dirs_size *= 2;
12734 lh->include_dirs = xrealloc (lh->include_dirs,
12735 (lh->include_dirs_size
12736 * sizeof (*lh->include_dirs)));
12737 }
12738
12739 lh->include_dirs[lh->num_include_dirs++] = include_dir;
12740 }
12741
12742 /* Add an entry to LH's file name table. */
12743
12744 static void
12745 add_file_name (struct line_header *lh,
12746 char *name,
12747 unsigned int dir_index,
12748 unsigned int mod_time,
12749 unsigned int length)
12750 {
12751 struct file_entry *fe;
12752
12753 /* Grow the array if necessary. */
12754 if (lh->file_names_size == 0)
12755 {
12756 lh->file_names_size = 1; /* for testing */
12757 lh->file_names = xmalloc (lh->file_names_size
12758 * sizeof (*lh->file_names));
12759 }
12760 else if (lh->num_file_names >= lh->file_names_size)
12761 {
12762 lh->file_names_size *= 2;
12763 lh->file_names = xrealloc (lh->file_names,
12764 (lh->file_names_size
12765 * sizeof (*lh->file_names)));
12766 }
12767
12768 fe = &lh->file_names[lh->num_file_names++];
12769 fe->name = name;
12770 fe->dir_index = dir_index;
12771 fe->mod_time = mod_time;
12772 fe->length = length;
12773 fe->included_p = 0;
12774 fe->symtab = NULL;
12775 }
12776
12777 /* Read the statement program header starting at OFFSET in
12778 .debug_line, or .debug_line.dwo. Return a pointer
12779 to a struct line_header, allocated using xmalloc.
12780
12781 NOTE: the strings in the include directory and file name tables of
12782 the returned object point into the dwarf line section buffer,
12783 and must not be freed. */
12784
12785 static struct line_header *
12786 dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
12787 {
12788 struct cleanup *back_to;
12789 struct line_header *lh;
12790 gdb_byte *line_ptr;
12791 unsigned int bytes_read, offset_size;
12792 int i;
12793 char *cur_dir, *cur_file;
12794 struct dwarf2_section_info *section;
12795 bfd *abfd;
12796
12797 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
12798 DWO file. */
12799 if (cu->dwo_unit && cu->per_cu->is_debug_types)
12800 section = &cu->dwo_unit->dwo_file->sections.line;
12801 else
12802 section = &dwarf2_per_objfile->line;
12803
12804 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
12805 if (section->buffer == NULL)
12806 {
12807 if (cu->dwo_unit && cu->per_cu->is_debug_types)
12808 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
12809 else
12810 complaint (&symfile_complaints, _("missing .debug_line section"));
12811 return 0;
12812 }
12813
12814 /* We can't do this until we know the section is non-empty.
12815 Only then do we know we have such a section. */
12816 abfd = section->asection->owner;
12817
12818 /* Make sure that at least there's room for the total_length field.
12819 That could be 12 bytes long, but we're just going to fudge that. */
12820 if (offset + 4 >= section->size)
12821 {
12822 dwarf2_statement_list_fits_in_line_number_section_complaint ();
12823 return 0;
12824 }
12825
12826 lh = xmalloc (sizeof (*lh));
12827 memset (lh, 0, sizeof (*lh));
12828 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
12829 (void *) lh);
12830
12831 line_ptr = section->buffer + offset;
12832
12833 /* Read in the header. */
12834 lh->total_length =
12835 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
12836 &bytes_read, &offset_size);
12837 line_ptr += bytes_read;
12838 if (line_ptr + lh->total_length > (section->buffer + section->size))
12839 {
12840 dwarf2_statement_list_fits_in_line_number_section_complaint ();
12841 return 0;
12842 }
12843 lh->statement_program_end = line_ptr + lh->total_length;
12844 lh->version = read_2_bytes (abfd, line_ptr);
12845 line_ptr += 2;
12846 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
12847 line_ptr += offset_size;
12848 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
12849 line_ptr += 1;
12850 if (lh->version >= 4)
12851 {
12852 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
12853 line_ptr += 1;
12854 }
12855 else
12856 lh->maximum_ops_per_instruction = 1;
12857
12858 if (lh->maximum_ops_per_instruction == 0)
12859 {
12860 lh->maximum_ops_per_instruction = 1;
12861 complaint (&symfile_complaints,
12862 _("invalid maximum_ops_per_instruction "
12863 "in `.debug_line' section"));
12864 }
12865
12866 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
12867 line_ptr += 1;
12868 lh->line_base = read_1_signed_byte (abfd, line_ptr);
12869 line_ptr += 1;
12870 lh->line_range = read_1_byte (abfd, line_ptr);
12871 line_ptr += 1;
12872 lh->opcode_base = read_1_byte (abfd, line_ptr);
12873 line_ptr += 1;
12874 lh->standard_opcode_lengths
12875 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
12876
12877 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
12878 for (i = 1; i < lh->opcode_base; ++i)
12879 {
12880 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
12881 line_ptr += 1;
12882 }
12883
12884 /* Read directory table. */
12885 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
12886 {
12887 line_ptr += bytes_read;
12888 add_include_dir (lh, cur_dir);
12889 }
12890 line_ptr += bytes_read;
12891
12892 /* Read file name table. */
12893 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
12894 {
12895 unsigned int dir_index, mod_time, length;
12896
12897 line_ptr += bytes_read;
12898 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12899 line_ptr += bytes_read;
12900 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12901 line_ptr += bytes_read;
12902 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12903 line_ptr += bytes_read;
12904
12905 add_file_name (lh, cur_file, dir_index, mod_time, length);
12906 }
12907 line_ptr += bytes_read;
12908 lh->statement_program_start = line_ptr;
12909
12910 if (line_ptr > (section->buffer + section->size))
12911 complaint (&symfile_complaints,
12912 _("line number info header doesn't "
12913 "fit in `.debug_line' section"));
12914
12915 discard_cleanups (back_to);
12916 return lh;
12917 }
12918
12919 /* Subroutine of dwarf_decode_lines to simplify it.
12920 Return the file name of the psymtab for included file FILE_INDEX
12921 in line header LH of PST.
12922 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
12923 If space for the result is malloc'd, it will be freed by a cleanup.
12924 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
12925
12926 static char *
12927 psymtab_include_file_name (const struct line_header *lh, int file_index,
12928 const struct partial_symtab *pst,
12929 const char *comp_dir)
12930 {
12931 const struct file_entry fe = lh->file_names [file_index];
12932 char *include_name = fe.name;
12933 char *include_name_to_compare = include_name;
12934 char *dir_name = NULL;
12935 const char *pst_filename;
12936 char *copied_name = NULL;
12937 int file_is_pst;
12938
12939 if (fe.dir_index)
12940 dir_name = lh->include_dirs[fe.dir_index - 1];
12941
12942 if (!IS_ABSOLUTE_PATH (include_name)
12943 && (dir_name != NULL || comp_dir != NULL))
12944 {
12945 /* Avoid creating a duplicate psymtab for PST.
12946 We do this by comparing INCLUDE_NAME and PST_FILENAME.
12947 Before we do the comparison, however, we need to account
12948 for DIR_NAME and COMP_DIR.
12949 First prepend dir_name (if non-NULL). If we still don't
12950 have an absolute path prepend comp_dir (if non-NULL).
12951 However, the directory we record in the include-file's
12952 psymtab does not contain COMP_DIR (to match the
12953 corresponding symtab(s)).
12954
12955 Example:
12956
12957 bash$ cd /tmp
12958 bash$ gcc -g ./hello.c
12959 include_name = "hello.c"
12960 dir_name = "."
12961 DW_AT_comp_dir = comp_dir = "/tmp"
12962 DW_AT_name = "./hello.c" */
12963
12964 if (dir_name != NULL)
12965 {
12966 include_name = concat (dir_name, SLASH_STRING,
12967 include_name, (char *)NULL);
12968 include_name_to_compare = include_name;
12969 make_cleanup (xfree, include_name);
12970 }
12971 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
12972 {
12973 include_name_to_compare = concat (comp_dir, SLASH_STRING,
12974 include_name, (char *)NULL);
12975 }
12976 }
12977
12978 pst_filename = pst->filename;
12979 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
12980 {
12981 copied_name = concat (pst->dirname, SLASH_STRING,
12982 pst_filename, (char *)NULL);
12983 pst_filename = copied_name;
12984 }
12985
12986 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
12987
12988 if (include_name_to_compare != include_name)
12989 xfree (include_name_to_compare);
12990 if (copied_name != NULL)
12991 xfree (copied_name);
12992
12993 if (file_is_pst)
12994 return NULL;
12995 return include_name;
12996 }
12997
12998 /* Ignore this record_line request. */
12999
13000 static void
13001 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
13002 {
13003 return;
13004 }
13005
13006 /* Subroutine of dwarf_decode_lines to simplify it.
13007 Process the line number information in LH. */
13008
13009 static void
13010 dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
13011 struct dwarf2_cu *cu, struct partial_symtab *pst)
13012 {
13013 gdb_byte *line_ptr, *extended_end;
13014 gdb_byte *line_end;
13015 unsigned int bytes_read, extended_len;
13016 unsigned char op_code, extended_op, adj_opcode;
13017 CORE_ADDR baseaddr;
13018 struct objfile *objfile = cu->objfile;
13019 bfd *abfd = objfile->obfd;
13020 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13021 const int decode_for_pst_p = (pst != NULL);
13022 struct subfile *last_subfile = NULL;
13023 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
13024 = record_line;
13025
13026 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13027
13028 line_ptr = lh->statement_program_start;
13029 line_end = lh->statement_program_end;
13030
13031 /* Read the statement sequences until there's nothing left. */
13032 while (line_ptr < line_end)
13033 {
13034 /* state machine registers */
13035 CORE_ADDR address = 0;
13036 unsigned int file = 1;
13037 unsigned int line = 1;
13038 unsigned int column = 0;
13039 int is_stmt = lh->default_is_stmt;
13040 int basic_block = 0;
13041 int end_sequence = 0;
13042 CORE_ADDR addr;
13043 unsigned char op_index = 0;
13044
13045 if (!decode_for_pst_p && lh->num_file_names >= file)
13046 {
13047 /* Start a subfile for the current file of the state machine. */
13048 /* lh->include_dirs and lh->file_names are 0-based, but the
13049 directory and file name numbers in the statement program
13050 are 1-based. */
13051 struct file_entry *fe = &lh->file_names[file - 1];
13052 char *dir = NULL;
13053
13054 if (fe->dir_index)
13055 dir = lh->include_dirs[fe->dir_index - 1];
13056
13057 dwarf2_start_subfile (fe->name, dir, comp_dir);
13058 }
13059
13060 /* Decode the table. */
13061 while (!end_sequence)
13062 {
13063 op_code = read_1_byte (abfd, line_ptr);
13064 line_ptr += 1;
13065 if (line_ptr > line_end)
13066 {
13067 dwarf2_debug_line_missing_end_sequence_complaint ();
13068 break;
13069 }
13070
13071 if (op_code >= lh->opcode_base)
13072 {
13073 /* Special operand. */
13074 adj_opcode = op_code - lh->opcode_base;
13075 address += (((op_index + (adj_opcode / lh->line_range))
13076 / lh->maximum_ops_per_instruction)
13077 * lh->minimum_instruction_length);
13078 op_index = ((op_index + (adj_opcode / lh->line_range))
13079 % lh->maximum_ops_per_instruction);
13080 line += lh->line_base + (adj_opcode % lh->line_range);
13081 if (lh->num_file_names < file || file == 0)
13082 dwarf2_debug_line_missing_file_complaint ();
13083 /* For now we ignore lines not starting on an
13084 instruction boundary. */
13085 else if (op_index == 0)
13086 {
13087 lh->file_names[file - 1].included_p = 1;
13088 if (!decode_for_pst_p && is_stmt)
13089 {
13090 if (last_subfile != current_subfile)
13091 {
13092 addr = gdbarch_addr_bits_remove (gdbarch, address);
13093 if (last_subfile)
13094 (*p_record_line) (last_subfile, 0, addr);
13095 last_subfile = current_subfile;
13096 }
13097 /* Append row to matrix using current values. */
13098 addr = gdbarch_addr_bits_remove (gdbarch, address);
13099 (*p_record_line) (current_subfile, line, addr);
13100 }
13101 }
13102 basic_block = 0;
13103 }
13104 else switch (op_code)
13105 {
13106 case DW_LNS_extended_op:
13107 extended_len = read_unsigned_leb128 (abfd, line_ptr,
13108 &bytes_read);
13109 line_ptr += bytes_read;
13110 extended_end = line_ptr + extended_len;
13111 extended_op = read_1_byte (abfd, line_ptr);
13112 line_ptr += 1;
13113 switch (extended_op)
13114 {
13115 case DW_LNE_end_sequence:
13116 p_record_line = record_line;
13117 end_sequence = 1;
13118 break;
13119 case DW_LNE_set_address:
13120 address = read_address (abfd, line_ptr, cu, &bytes_read);
13121
13122 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
13123 {
13124 /* This line table is for a function which has been
13125 GCd by the linker. Ignore it. PR gdb/12528 */
13126
13127 long line_offset
13128 = line_ptr - dwarf2_per_objfile->line.buffer;
13129
13130 complaint (&symfile_complaints,
13131 _(".debug_line address at offset 0x%lx is 0 "
13132 "[in module %s]"),
13133 line_offset, objfile->name);
13134 p_record_line = noop_record_line;
13135 }
13136
13137 op_index = 0;
13138 line_ptr += bytes_read;
13139 address += baseaddr;
13140 break;
13141 case DW_LNE_define_file:
13142 {
13143 char *cur_file;
13144 unsigned int dir_index, mod_time, length;
13145
13146 cur_file = read_direct_string (abfd, line_ptr,
13147 &bytes_read);
13148 line_ptr += bytes_read;
13149 dir_index =
13150 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13151 line_ptr += bytes_read;
13152 mod_time =
13153 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13154 line_ptr += bytes_read;
13155 length =
13156 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13157 line_ptr += bytes_read;
13158 add_file_name (lh, cur_file, dir_index, mod_time, length);
13159 }
13160 break;
13161 case DW_LNE_set_discriminator:
13162 /* The discriminator is not interesting to the debugger;
13163 just ignore it. */
13164 line_ptr = extended_end;
13165 break;
13166 default:
13167 complaint (&symfile_complaints,
13168 _("mangled .debug_line section"));
13169 return;
13170 }
13171 /* Make sure that we parsed the extended op correctly. If e.g.
13172 we expected a different address size than the producer used,
13173 we may have read the wrong number of bytes. */
13174 if (line_ptr != extended_end)
13175 {
13176 complaint (&symfile_complaints,
13177 _("mangled .debug_line section"));
13178 return;
13179 }
13180 break;
13181 case DW_LNS_copy:
13182 if (lh->num_file_names < file || file == 0)
13183 dwarf2_debug_line_missing_file_complaint ();
13184 else
13185 {
13186 lh->file_names[file - 1].included_p = 1;
13187 if (!decode_for_pst_p && is_stmt)
13188 {
13189 if (last_subfile != current_subfile)
13190 {
13191 addr = gdbarch_addr_bits_remove (gdbarch, address);
13192 if (last_subfile)
13193 (*p_record_line) (last_subfile, 0, addr);
13194 last_subfile = current_subfile;
13195 }
13196 addr = gdbarch_addr_bits_remove (gdbarch, address);
13197 (*p_record_line) (current_subfile, line, addr);
13198 }
13199 }
13200 basic_block = 0;
13201 break;
13202 case DW_LNS_advance_pc:
13203 {
13204 CORE_ADDR adjust
13205 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13206
13207 address += (((op_index + adjust)
13208 / lh->maximum_ops_per_instruction)
13209 * lh->minimum_instruction_length);
13210 op_index = ((op_index + adjust)
13211 % lh->maximum_ops_per_instruction);
13212 line_ptr += bytes_read;
13213 }
13214 break;
13215 case DW_LNS_advance_line:
13216 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
13217 line_ptr += bytes_read;
13218 break;
13219 case DW_LNS_set_file:
13220 {
13221 /* The arrays lh->include_dirs and lh->file_names are
13222 0-based, but the directory and file name numbers in
13223 the statement program are 1-based. */
13224 struct file_entry *fe;
13225 char *dir = NULL;
13226
13227 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13228 line_ptr += bytes_read;
13229 if (lh->num_file_names < file || file == 0)
13230 dwarf2_debug_line_missing_file_complaint ();
13231 else
13232 {
13233 fe = &lh->file_names[file - 1];
13234 if (fe->dir_index)
13235 dir = lh->include_dirs[fe->dir_index - 1];
13236 if (!decode_for_pst_p)
13237 {
13238 last_subfile = current_subfile;
13239 dwarf2_start_subfile (fe->name, dir, comp_dir);
13240 }
13241 }
13242 }
13243 break;
13244 case DW_LNS_set_column:
13245 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13246 line_ptr += bytes_read;
13247 break;
13248 case DW_LNS_negate_stmt:
13249 is_stmt = (!is_stmt);
13250 break;
13251 case DW_LNS_set_basic_block:
13252 basic_block = 1;
13253 break;
13254 /* Add to the address register of the state machine the
13255 address increment value corresponding to special opcode
13256 255. I.e., this value is scaled by the minimum
13257 instruction length since special opcode 255 would have
13258 scaled the increment. */
13259 case DW_LNS_const_add_pc:
13260 {
13261 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
13262
13263 address += (((op_index + adjust)
13264 / lh->maximum_ops_per_instruction)
13265 * lh->minimum_instruction_length);
13266 op_index = ((op_index + adjust)
13267 % lh->maximum_ops_per_instruction);
13268 }
13269 break;
13270 case DW_LNS_fixed_advance_pc:
13271 address += read_2_bytes (abfd, line_ptr);
13272 op_index = 0;
13273 line_ptr += 2;
13274 break;
13275 default:
13276 {
13277 /* Unknown standard opcode, ignore it. */
13278 int i;
13279
13280 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
13281 {
13282 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13283 line_ptr += bytes_read;
13284 }
13285 }
13286 }
13287 }
13288 if (lh->num_file_names < file || file == 0)
13289 dwarf2_debug_line_missing_file_complaint ();
13290 else
13291 {
13292 lh->file_names[file - 1].included_p = 1;
13293 if (!decode_for_pst_p)
13294 {
13295 addr = gdbarch_addr_bits_remove (gdbarch, address);
13296 (*p_record_line) (current_subfile, 0, addr);
13297 }
13298 }
13299 }
13300 }
13301
13302 /* Decode the Line Number Program (LNP) for the given line_header
13303 structure and CU. The actual information extracted and the type
13304 of structures created from the LNP depends on the value of PST.
13305
13306 1. If PST is NULL, then this procedure uses the data from the program
13307 to create all necessary symbol tables, and their linetables.
13308
13309 2. If PST is not NULL, this procedure reads the program to determine
13310 the list of files included by the unit represented by PST, and
13311 builds all the associated partial symbol tables.
13312
13313 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
13314 It is used for relative paths in the line table.
13315 NOTE: When processing partial symtabs (pst != NULL),
13316 comp_dir == pst->dirname.
13317
13318 NOTE: It is important that psymtabs have the same file name (via strcmp)
13319 as the corresponding symtab. Since COMP_DIR is not used in the name of the
13320 symtab we don't use it in the name of the psymtabs we create.
13321 E.g. expand_line_sal requires this when finding psymtabs to expand.
13322 A good testcase for this is mb-inline.exp. */
13323
13324 static void
13325 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
13326 struct dwarf2_cu *cu, struct partial_symtab *pst,
13327 int want_line_info)
13328 {
13329 struct objfile *objfile = cu->objfile;
13330 const int decode_for_pst_p = (pst != NULL);
13331 struct subfile *first_subfile = current_subfile;
13332
13333 if (want_line_info)
13334 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
13335
13336 if (decode_for_pst_p)
13337 {
13338 int file_index;
13339
13340 /* Now that we're done scanning the Line Header Program, we can
13341 create the psymtab of each included file. */
13342 for (file_index = 0; file_index < lh->num_file_names; file_index++)
13343 if (lh->file_names[file_index].included_p == 1)
13344 {
13345 char *include_name =
13346 psymtab_include_file_name (lh, file_index, pst, comp_dir);
13347 if (include_name != NULL)
13348 dwarf2_create_include_psymtab (include_name, pst, objfile);
13349 }
13350 }
13351 else
13352 {
13353 /* Make sure a symtab is created for every file, even files
13354 which contain only variables (i.e. no code with associated
13355 line numbers). */
13356 int i;
13357
13358 for (i = 0; i < lh->num_file_names; i++)
13359 {
13360 char *dir = NULL;
13361 struct file_entry *fe;
13362
13363 fe = &lh->file_names[i];
13364 if (fe->dir_index)
13365 dir = lh->include_dirs[fe->dir_index - 1];
13366 dwarf2_start_subfile (fe->name, dir, comp_dir);
13367
13368 /* Skip the main file; we don't need it, and it must be
13369 allocated last, so that it will show up before the
13370 non-primary symtabs in the objfile's symtab list. */
13371 if (current_subfile == first_subfile)
13372 continue;
13373
13374 if (current_subfile->symtab == NULL)
13375 current_subfile->symtab = allocate_symtab (current_subfile->name,
13376 objfile);
13377 fe->symtab = current_subfile->symtab;
13378 }
13379 }
13380 }
13381
13382 /* Start a subfile for DWARF. FILENAME is the name of the file and
13383 DIRNAME the name of the source directory which contains FILENAME
13384 or NULL if not known. COMP_DIR is the compilation directory for the
13385 linetable's compilation unit or NULL if not known.
13386 This routine tries to keep line numbers from identical absolute and
13387 relative file names in a common subfile.
13388
13389 Using the `list' example from the GDB testsuite, which resides in
13390 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
13391 of /srcdir/list0.c yields the following debugging information for list0.c:
13392
13393 DW_AT_name: /srcdir/list0.c
13394 DW_AT_comp_dir: /compdir
13395 files.files[0].name: list0.h
13396 files.files[0].dir: /srcdir
13397 files.files[1].name: list0.c
13398 files.files[1].dir: /srcdir
13399
13400 The line number information for list0.c has to end up in a single
13401 subfile, so that `break /srcdir/list0.c:1' works as expected.
13402 start_subfile will ensure that this happens provided that we pass the
13403 concatenation of files.files[1].dir and files.files[1].name as the
13404 subfile's name. */
13405
13406 static void
13407 dwarf2_start_subfile (char *filename, const char *dirname,
13408 const char *comp_dir)
13409 {
13410 char *fullname;
13411
13412 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
13413 `start_symtab' will always pass the contents of DW_AT_comp_dir as
13414 second argument to start_subfile. To be consistent, we do the
13415 same here. In order not to lose the line information directory,
13416 we concatenate it to the filename when it makes sense.
13417 Note that the Dwarf3 standard says (speaking of filenames in line
13418 information): ``The directory index is ignored for file names
13419 that represent full path names''. Thus ignoring dirname in the
13420 `else' branch below isn't an issue. */
13421
13422 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
13423 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
13424 else
13425 fullname = filename;
13426
13427 start_subfile (fullname, comp_dir);
13428
13429 if (fullname != filename)
13430 xfree (fullname);
13431 }
13432
13433 static void
13434 var_decode_location (struct attribute *attr, struct symbol *sym,
13435 struct dwarf2_cu *cu)
13436 {
13437 struct objfile *objfile = cu->objfile;
13438 struct comp_unit_head *cu_header = &cu->header;
13439
13440 /* NOTE drow/2003-01-30: There used to be a comment and some special
13441 code here to turn a symbol with DW_AT_external and a
13442 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
13443 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
13444 with some versions of binutils) where shared libraries could have
13445 relocations against symbols in their debug information - the
13446 minimal symbol would have the right address, but the debug info
13447 would not. It's no longer necessary, because we will explicitly
13448 apply relocations when we read in the debug information now. */
13449
13450 /* A DW_AT_location attribute with no contents indicates that a
13451 variable has been optimized away. */
13452 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
13453 {
13454 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
13455 return;
13456 }
13457
13458 /* Handle one degenerate form of location expression specially, to
13459 preserve GDB's previous behavior when section offsets are
13460 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
13461 then mark this symbol as LOC_STATIC. */
13462
13463 if (attr_form_is_block (attr)
13464 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
13465 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
13466 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
13467 && (DW_BLOCK (attr)->size
13468 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
13469 {
13470 unsigned int dummy;
13471
13472 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
13473 SYMBOL_VALUE_ADDRESS (sym) =
13474 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
13475 else
13476 SYMBOL_VALUE_ADDRESS (sym) =
13477 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
13478 SYMBOL_CLASS (sym) = LOC_STATIC;
13479 fixup_symbol_section (sym, objfile);
13480 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
13481 SYMBOL_SECTION (sym));
13482 return;
13483 }
13484
13485 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
13486 expression evaluator, and use LOC_COMPUTED only when necessary
13487 (i.e. when the value of a register or memory location is
13488 referenced, or a thread-local block, etc.). Then again, it might
13489 not be worthwhile. I'm assuming that it isn't unless performance
13490 or memory numbers show me otherwise. */
13491
13492 dwarf2_symbol_mark_computed (attr, sym, cu);
13493 SYMBOL_CLASS (sym) = LOC_COMPUTED;
13494
13495 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
13496 cu->has_loclist = 1;
13497 }
13498
13499 /* Given a pointer to a DWARF information entry, figure out if we need
13500 to make a symbol table entry for it, and if so, create a new entry
13501 and return a pointer to it.
13502 If TYPE is NULL, determine symbol type from the die, otherwise
13503 used the passed type.
13504 If SPACE is not NULL, use it to hold the new symbol. If it is
13505 NULL, allocate a new symbol on the objfile's obstack. */
13506
13507 static struct symbol *
13508 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
13509 struct symbol *space)
13510 {
13511 struct objfile *objfile = cu->objfile;
13512 struct symbol *sym = NULL;
13513 char *name;
13514 struct attribute *attr = NULL;
13515 struct attribute *attr2 = NULL;
13516 CORE_ADDR baseaddr;
13517 struct pending **list_to_add = NULL;
13518
13519 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13520
13521 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13522
13523 name = dwarf2_name (die, cu);
13524 if (name)
13525 {
13526 const char *linkagename;
13527 int suppress_add = 0;
13528
13529 if (space)
13530 sym = space;
13531 else
13532 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
13533 OBJSTAT (objfile, n_syms++);
13534
13535 /* Cache this symbol's name and the name's demangled form (if any). */
13536 SYMBOL_SET_LANGUAGE (sym, cu->language);
13537 linkagename = dwarf2_physname (name, die, cu);
13538 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
13539
13540 /* Fortran does not have mangling standard and the mangling does differ
13541 between gfortran, iFort etc. */
13542 if (cu->language == language_fortran
13543 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
13544 symbol_set_demangled_name (&(sym->ginfo),
13545 (char *) dwarf2_full_name (name, die, cu),
13546 NULL);
13547
13548 /* Default assumptions.
13549 Use the passed type or decode it from the die. */
13550 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
13551 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
13552 if (type != NULL)
13553 SYMBOL_TYPE (sym) = type;
13554 else
13555 SYMBOL_TYPE (sym) = die_type (die, cu);
13556 attr = dwarf2_attr (die,
13557 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
13558 cu);
13559 if (attr)
13560 {
13561 SYMBOL_LINE (sym) = DW_UNSND (attr);
13562 }
13563
13564 attr = dwarf2_attr (die,
13565 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
13566 cu);
13567 if (attr)
13568 {
13569 int file_index = DW_UNSND (attr);
13570
13571 if (cu->line_header == NULL
13572 || file_index > cu->line_header->num_file_names)
13573 complaint (&symfile_complaints,
13574 _("file index out of range"));
13575 else if (file_index > 0)
13576 {
13577 struct file_entry *fe;
13578
13579 fe = &cu->line_header->file_names[file_index - 1];
13580 SYMBOL_SYMTAB (sym) = fe->symtab;
13581 }
13582 }
13583
13584 switch (die->tag)
13585 {
13586 case DW_TAG_label:
13587 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13588 if (attr)
13589 {
13590 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
13591 }
13592 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
13593 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
13594 SYMBOL_CLASS (sym) = LOC_LABEL;
13595 add_symbol_to_list (sym, cu->list_in_scope);
13596 break;
13597 case DW_TAG_subprogram:
13598 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
13599 finish_block. */
13600 SYMBOL_CLASS (sym) = LOC_BLOCK;
13601 attr2 = dwarf2_attr (die, DW_AT_external, cu);
13602 if ((attr2 && (DW_UNSND (attr2) != 0))
13603 || cu->language == language_ada)
13604 {
13605 /* Subprograms marked external are stored as a global symbol.
13606 Ada subprograms, whether marked external or not, are always
13607 stored as a global symbol, because we want to be able to
13608 access them globally. For instance, we want to be able
13609 to break on a nested subprogram without having to
13610 specify the context. */
13611 list_to_add = &global_symbols;
13612 }
13613 else
13614 {
13615 list_to_add = cu->list_in_scope;
13616 }
13617 break;
13618 case DW_TAG_inlined_subroutine:
13619 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
13620 finish_block. */
13621 SYMBOL_CLASS (sym) = LOC_BLOCK;
13622 SYMBOL_INLINED (sym) = 1;
13623 list_to_add = cu->list_in_scope;
13624 break;
13625 case DW_TAG_template_value_param:
13626 suppress_add = 1;
13627 /* Fall through. */
13628 case DW_TAG_constant:
13629 case DW_TAG_variable:
13630 case DW_TAG_member:
13631 /* Compilation with minimal debug info may result in
13632 variables with missing type entries. Change the
13633 misleading `void' type to something sensible. */
13634 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
13635 SYMBOL_TYPE (sym)
13636 = objfile_type (objfile)->nodebug_data_symbol;
13637
13638 attr = dwarf2_attr (die, DW_AT_const_value, cu);
13639 /* In the case of DW_TAG_member, we should only be called for
13640 static const members. */
13641 if (die->tag == DW_TAG_member)
13642 {
13643 /* dwarf2_add_field uses die_is_declaration,
13644 so we do the same. */
13645 gdb_assert (die_is_declaration (die, cu));
13646 gdb_assert (attr);
13647 }
13648 if (attr)
13649 {
13650 dwarf2_const_value (attr, sym, cu);
13651 attr2 = dwarf2_attr (die, DW_AT_external, cu);
13652 if (!suppress_add)
13653 {
13654 if (attr2 && (DW_UNSND (attr2) != 0))
13655 list_to_add = &global_symbols;
13656 else
13657 list_to_add = cu->list_in_scope;
13658 }
13659 break;
13660 }
13661 attr = dwarf2_attr (die, DW_AT_location, cu);
13662 if (attr)
13663 {
13664 var_decode_location (attr, sym, cu);
13665 attr2 = dwarf2_attr (die, DW_AT_external, cu);
13666 if (SYMBOL_CLASS (sym) == LOC_STATIC
13667 && SYMBOL_VALUE_ADDRESS (sym) == 0
13668 && !dwarf2_per_objfile->has_section_at_zero)
13669 {
13670 /* When a static variable is eliminated by the linker,
13671 the corresponding debug information is not stripped
13672 out, but the variable address is set to null;
13673 do not add such variables into symbol table. */
13674 }
13675 else if (attr2 && (DW_UNSND (attr2) != 0))
13676 {
13677 /* Workaround gfortran PR debug/40040 - it uses
13678 DW_AT_location for variables in -fPIC libraries which may
13679 get overriden by other libraries/executable and get
13680 a different address. Resolve it by the minimal symbol
13681 which may come from inferior's executable using copy
13682 relocation. Make this workaround only for gfortran as for
13683 other compilers GDB cannot guess the minimal symbol
13684 Fortran mangling kind. */
13685 if (cu->language == language_fortran && die->parent
13686 && die->parent->tag == DW_TAG_module
13687 && cu->producer
13688 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
13689 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
13690
13691 /* A variable with DW_AT_external is never static,
13692 but it may be block-scoped. */
13693 list_to_add = (cu->list_in_scope == &file_symbols
13694 ? &global_symbols : cu->list_in_scope);
13695 }
13696 else
13697 list_to_add = cu->list_in_scope;
13698 }
13699 else
13700 {
13701 /* We do not know the address of this symbol.
13702 If it is an external symbol and we have type information
13703 for it, enter the symbol as a LOC_UNRESOLVED symbol.
13704 The address of the variable will then be determined from
13705 the minimal symbol table whenever the variable is
13706 referenced. */
13707 attr2 = dwarf2_attr (die, DW_AT_external, cu);
13708 if (attr2 && (DW_UNSND (attr2) != 0)
13709 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
13710 {
13711 /* A variable with DW_AT_external is never static, but it
13712 may be block-scoped. */
13713 list_to_add = (cu->list_in_scope == &file_symbols
13714 ? &global_symbols : cu->list_in_scope);
13715
13716 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
13717 }
13718 else if (!die_is_declaration (die, cu))
13719 {
13720 /* Use the default LOC_OPTIMIZED_OUT class. */
13721 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
13722 if (!suppress_add)
13723 list_to_add = cu->list_in_scope;
13724 }
13725 }
13726 break;
13727 case DW_TAG_formal_parameter:
13728 /* If we are inside a function, mark this as an argument. If
13729 not, we might be looking at an argument to an inlined function
13730 when we do not have enough information to show inlined frames;
13731 pretend it's a local variable in that case so that the user can
13732 still see it. */
13733 if (context_stack_depth > 0
13734 && context_stack[context_stack_depth - 1].name != NULL)
13735 SYMBOL_IS_ARGUMENT (sym) = 1;
13736 attr = dwarf2_attr (die, DW_AT_location, cu);
13737 if (attr)
13738 {
13739 var_decode_location (attr, sym, cu);
13740 }
13741 attr = dwarf2_attr (die, DW_AT_const_value, cu);
13742 if (attr)
13743 {
13744 dwarf2_const_value (attr, sym, cu);
13745 }
13746
13747 list_to_add = cu->list_in_scope;
13748 break;
13749 case DW_TAG_unspecified_parameters:
13750 /* From varargs functions; gdb doesn't seem to have any
13751 interest in this information, so just ignore it for now.
13752 (FIXME?) */
13753 break;
13754 case DW_TAG_template_type_param:
13755 suppress_add = 1;
13756 /* Fall through. */
13757 case DW_TAG_class_type:
13758 case DW_TAG_interface_type:
13759 case DW_TAG_structure_type:
13760 case DW_TAG_union_type:
13761 case DW_TAG_set_type:
13762 case DW_TAG_enumeration_type:
13763 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13764 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
13765
13766 {
13767 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
13768 really ever be static objects: otherwise, if you try
13769 to, say, break of a class's method and you're in a file
13770 which doesn't mention that class, it won't work unless
13771 the check for all static symbols in lookup_symbol_aux
13772 saves you. See the OtherFileClass tests in
13773 gdb.c++/namespace.exp. */
13774
13775 if (!suppress_add)
13776 {
13777 list_to_add = (cu->list_in_scope == &file_symbols
13778 && (cu->language == language_cplus
13779 || cu->language == language_java)
13780 ? &global_symbols : cu->list_in_scope);
13781
13782 /* The semantics of C++ state that "struct foo {
13783 ... }" also defines a typedef for "foo". A Java
13784 class declaration also defines a typedef for the
13785 class. */
13786 if (cu->language == language_cplus
13787 || cu->language == language_java
13788 || cu->language == language_ada)
13789 {
13790 /* The symbol's name is already allocated along
13791 with this objfile, so we don't need to
13792 duplicate it for the type. */
13793 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
13794 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
13795 }
13796 }
13797 }
13798 break;
13799 case DW_TAG_typedef:
13800 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13801 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
13802 list_to_add = cu->list_in_scope;
13803 break;
13804 case DW_TAG_base_type:
13805 case DW_TAG_subrange_type:
13806 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13807 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
13808 list_to_add = cu->list_in_scope;
13809 break;
13810 case DW_TAG_enumerator:
13811 attr = dwarf2_attr (die, DW_AT_const_value, cu);
13812 if (attr)
13813 {
13814 dwarf2_const_value (attr, sym, cu);
13815 }
13816 {
13817 /* NOTE: carlton/2003-11-10: See comment above in the
13818 DW_TAG_class_type, etc. block. */
13819
13820 list_to_add = (cu->list_in_scope == &file_symbols
13821 && (cu->language == language_cplus
13822 || cu->language == language_java)
13823 ? &global_symbols : cu->list_in_scope);
13824 }
13825 break;
13826 case DW_TAG_namespace:
13827 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13828 list_to_add = &global_symbols;
13829 break;
13830 default:
13831 /* Not a tag we recognize. Hopefully we aren't processing
13832 trash data, but since we must specifically ignore things
13833 we don't recognize, there is nothing else we should do at
13834 this point. */
13835 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
13836 dwarf_tag_name (die->tag));
13837 break;
13838 }
13839
13840 if (suppress_add)
13841 {
13842 sym->hash_next = objfile->template_symbols;
13843 objfile->template_symbols = sym;
13844 list_to_add = NULL;
13845 }
13846
13847 if (list_to_add != NULL)
13848 add_symbol_to_list (sym, list_to_add);
13849
13850 /* For the benefit of old versions of GCC, check for anonymous
13851 namespaces based on the demangled name. */
13852 if (!processing_has_namespace_info
13853 && cu->language == language_cplus)
13854 cp_scan_for_anonymous_namespaces (sym, objfile);
13855 }
13856 return (sym);
13857 }
13858
13859 /* A wrapper for new_symbol_full that always allocates a new symbol. */
13860
13861 static struct symbol *
13862 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
13863 {
13864 return new_symbol_full (die, type, cu, NULL);
13865 }
13866
13867 /* Given an attr with a DW_FORM_dataN value in host byte order,
13868 zero-extend it as appropriate for the symbol's type. The DWARF
13869 standard (v4) is not entirely clear about the meaning of using
13870 DW_FORM_dataN for a constant with a signed type, where the type is
13871 wider than the data. The conclusion of a discussion on the DWARF
13872 list was that this is unspecified. We choose to always zero-extend
13873 because that is the interpretation long in use by GCC. */
13874
13875 static gdb_byte *
13876 dwarf2_const_value_data (struct attribute *attr, struct type *type,
13877 const char *name, struct obstack *obstack,
13878 struct dwarf2_cu *cu, LONGEST *value, int bits)
13879 {
13880 struct objfile *objfile = cu->objfile;
13881 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
13882 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
13883 LONGEST l = DW_UNSND (attr);
13884
13885 if (bits < sizeof (*value) * 8)
13886 {
13887 l &= ((LONGEST) 1 << bits) - 1;
13888 *value = l;
13889 }
13890 else if (bits == sizeof (*value) * 8)
13891 *value = l;
13892 else
13893 {
13894 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
13895 store_unsigned_integer (bytes, bits / 8, byte_order, l);
13896 return bytes;
13897 }
13898
13899 return NULL;
13900 }
13901
13902 /* Read a constant value from an attribute. Either set *VALUE, or if
13903 the value does not fit in *VALUE, set *BYTES - either already
13904 allocated on the objfile obstack, or newly allocated on OBSTACK,
13905 or, set *BATON, if we translated the constant to a location
13906 expression. */
13907
13908 static void
13909 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
13910 const char *name, struct obstack *obstack,
13911 struct dwarf2_cu *cu,
13912 LONGEST *value, gdb_byte **bytes,
13913 struct dwarf2_locexpr_baton **baton)
13914 {
13915 struct objfile *objfile = cu->objfile;
13916 struct comp_unit_head *cu_header = &cu->header;
13917 struct dwarf_block *blk;
13918 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
13919 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
13920
13921 *value = 0;
13922 *bytes = NULL;
13923 *baton = NULL;
13924
13925 switch (attr->form)
13926 {
13927 case DW_FORM_addr:
13928 case DW_FORM_GNU_addr_index:
13929 {
13930 gdb_byte *data;
13931
13932 if (TYPE_LENGTH (type) != cu_header->addr_size)
13933 dwarf2_const_value_length_mismatch_complaint (name,
13934 cu_header->addr_size,
13935 TYPE_LENGTH (type));
13936 /* Symbols of this form are reasonably rare, so we just
13937 piggyback on the existing location code rather than writing
13938 a new implementation of symbol_computed_ops. */
13939 *baton = obstack_alloc (&objfile->objfile_obstack,
13940 sizeof (struct dwarf2_locexpr_baton));
13941 (*baton)->per_cu = cu->per_cu;
13942 gdb_assert ((*baton)->per_cu);
13943
13944 (*baton)->size = 2 + cu_header->addr_size;
13945 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
13946 (*baton)->data = data;
13947
13948 data[0] = DW_OP_addr;
13949 store_unsigned_integer (&data[1], cu_header->addr_size,
13950 byte_order, DW_ADDR (attr));
13951 data[cu_header->addr_size + 1] = DW_OP_stack_value;
13952 }
13953 break;
13954 case DW_FORM_string:
13955 case DW_FORM_strp:
13956 case DW_FORM_GNU_str_index:
13957 /* DW_STRING is already allocated on the objfile obstack, point
13958 directly to it. */
13959 *bytes = (gdb_byte *) DW_STRING (attr);
13960 break;
13961 case DW_FORM_block1:
13962 case DW_FORM_block2:
13963 case DW_FORM_block4:
13964 case DW_FORM_block:
13965 case DW_FORM_exprloc:
13966 blk = DW_BLOCK (attr);
13967 if (TYPE_LENGTH (type) != blk->size)
13968 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
13969 TYPE_LENGTH (type));
13970 *bytes = blk->data;
13971 break;
13972
13973 /* The DW_AT_const_value attributes are supposed to carry the
13974 symbol's value "represented as it would be on the target
13975 architecture." By the time we get here, it's already been
13976 converted to host endianness, so we just need to sign- or
13977 zero-extend it as appropriate. */
13978 case DW_FORM_data1:
13979 *bytes = dwarf2_const_value_data (attr, type, name,
13980 obstack, cu, value, 8);
13981 break;
13982 case DW_FORM_data2:
13983 *bytes = dwarf2_const_value_data (attr, type, name,
13984 obstack, cu, value, 16);
13985 break;
13986 case DW_FORM_data4:
13987 *bytes = dwarf2_const_value_data (attr, type, name,
13988 obstack, cu, value, 32);
13989 break;
13990 case DW_FORM_data8:
13991 *bytes = dwarf2_const_value_data (attr, type, name,
13992 obstack, cu, value, 64);
13993 break;
13994
13995 case DW_FORM_sdata:
13996 *value = DW_SND (attr);
13997 break;
13998
13999 case DW_FORM_udata:
14000 *value = DW_UNSND (attr);
14001 break;
14002
14003 default:
14004 complaint (&symfile_complaints,
14005 _("unsupported const value attribute form: '%s'"),
14006 dwarf_form_name (attr->form));
14007 *value = 0;
14008 break;
14009 }
14010 }
14011
14012
14013 /* Copy constant value from an attribute to a symbol. */
14014
14015 static void
14016 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
14017 struct dwarf2_cu *cu)
14018 {
14019 struct objfile *objfile = cu->objfile;
14020 struct comp_unit_head *cu_header = &cu->header;
14021 LONGEST value;
14022 gdb_byte *bytes;
14023 struct dwarf2_locexpr_baton *baton;
14024
14025 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
14026 SYMBOL_PRINT_NAME (sym),
14027 &objfile->objfile_obstack, cu,
14028 &value, &bytes, &baton);
14029
14030 if (baton != NULL)
14031 {
14032 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
14033 SYMBOL_LOCATION_BATON (sym) = baton;
14034 SYMBOL_CLASS (sym) = LOC_COMPUTED;
14035 }
14036 else if (bytes != NULL)
14037 {
14038 SYMBOL_VALUE_BYTES (sym) = bytes;
14039 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
14040 }
14041 else
14042 {
14043 SYMBOL_VALUE (sym) = value;
14044 SYMBOL_CLASS (sym) = LOC_CONST;
14045 }
14046 }
14047
14048 /* Return the type of the die in question using its DW_AT_type attribute. */
14049
14050 static struct type *
14051 die_type (struct die_info *die, struct dwarf2_cu *cu)
14052 {
14053 struct attribute *type_attr;
14054
14055 type_attr = dwarf2_attr (die, DW_AT_type, cu);
14056 if (!type_attr)
14057 {
14058 /* A missing DW_AT_type represents a void type. */
14059 return objfile_type (cu->objfile)->builtin_void;
14060 }
14061
14062 return lookup_die_type (die, type_attr, cu);
14063 }
14064
14065 /* True iff CU's producer generates GNAT Ada auxiliary information
14066 that allows to find parallel types through that information instead
14067 of having to do expensive parallel lookups by type name. */
14068
14069 static int
14070 need_gnat_info (struct dwarf2_cu *cu)
14071 {
14072 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
14073 of GNAT produces this auxiliary information, without any indication
14074 that it is produced. Part of enhancing the FSF version of GNAT
14075 to produce that information will be to put in place an indicator
14076 that we can use in order to determine whether the descriptive type
14077 info is available or not. One suggestion that has been made is
14078 to use a new attribute, attached to the CU die. For now, assume
14079 that the descriptive type info is not available. */
14080 return 0;
14081 }
14082
14083 /* Return the auxiliary type of the die in question using its
14084 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
14085 attribute is not present. */
14086
14087 static struct type *
14088 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
14089 {
14090 struct attribute *type_attr;
14091
14092 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
14093 if (!type_attr)
14094 return NULL;
14095
14096 return lookup_die_type (die, type_attr, cu);
14097 }
14098
14099 /* If DIE has a descriptive_type attribute, then set the TYPE's
14100 descriptive type accordingly. */
14101
14102 static void
14103 set_descriptive_type (struct type *type, struct die_info *die,
14104 struct dwarf2_cu *cu)
14105 {
14106 struct type *descriptive_type = die_descriptive_type (die, cu);
14107
14108 if (descriptive_type)
14109 {
14110 ALLOCATE_GNAT_AUX_TYPE (type);
14111 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
14112 }
14113 }
14114
14115 /* Return the containing type of the die in question using its
14116 DW_AT_containing_type attribute. */
14117
14118 static struct type *
14119 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14120 {
14121 struct attribute *type_attr;
14122
14123 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
14124 if (!type_attr)
14125 error (_("Dwarf Error: Problem turning containing type into gdb type "
14126 "[in module %s]"), cu->objfile->name);
14127
14128 return lookup_die_type (die, type_attr, cu);
14129 }
14130
14131 /* Look up the type of DIE in CU using its type attribute ATTR.
14132 If there is no type substitute an error marker. */
14133
14134 static struct type *
14135 lookup_die_type (struct die_info *die, struct attribute *attr,
14136 struct dwarf2_cu *cu)
14137 {
14138 struct objfile *objfile = cu->objfile;
14139 struct type *this_type;
14140
14141 /* First see if we have it cached. */
14142
14143 if (is_ref_attr (attr))
14144 {
14145 sect_offset offset = dwarf2_get_ref_die_offset (attr);
14146
14147 this_type = get_die_type_at_offset (offset, cu->per_cu);
14148 }
14149 else if (attr->form == DW_FORM_ref_sig8)
14150 {
14151 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
14152
14153 /* sig_type will be NULL if the signatured type is missing from
14154 the debug info. */
14155 if (sig_type == NULL)
14156 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
14157 "at 0x%x [in module %s]"),
14158 die->offset.sect_off, objfile->name);
14159
14160 gdb_assert (sig_type->per_cu.is_debug_types);
14161 /* If we haven't filled in type_offset_in_section yet, then we
14162 haven't read the type in yet. */
14163 this_type = NULL;
14164 if (sig_type->type_offset_in_section.sect_off != 0)
14165 {
14166 this_type =
14167 get_die_type_at_offset (sig_type->type_offset_in_section,
14168 &sig_type->per_cu);
14169 }
14170 }
14171 else
14172 {
14173 dump_die_for_error (die);
14174 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
14175 dwarf_attr_name (attr->name), objfile->name);
14176 }
14177
14178 /* If not cached we need to read it in. */
14179
14180 if (this_type == NULL)
14181 {
14182 struct die_info *type_die;
14183 struct dwarf2_cu *type_cu = cu;
14184
14185 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
14186 /* If we found the type now, it's probably because the type came
14187 from an inter-CU reference and the type's CU got expanded before
14188 ours. */
14189 this_type = get_die_type (type_die, type_cu);
14190 if (this_type == NULL)
14191 this_type = read_type_die_1 (type_die, type_cu);
14192 }
14193
14194 /* If we still don't have a type use an error marker. */
14195
14196 if (this_type == NULL)
14197 {
14198 char *message, *saved;
14199
14200 /* read_type_die already issued a complaint. */
14201 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
14202 objfile->name,
14203 cu->header.offset.sect_off,
14204 die->offset.sect_off);
14205 saved = obstack_copy0 (&objfile->objfile_obstack,
14206 message, strlen (message));
14207 xfree (message);
14208
14209 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
14210 }
14211
14212 return this_type;
14213 }
14214
14215 /* Return the type in DIE, CU.
14216 Returns NULL for invalid types.
14217
14218 This first does a lookup in the appropriate type_hash table,
14219 and only reads the die in if necessary.
14220
14221 NOTE: This can be called when reading in partial or full symbols. */
14222
14223 static struct type *
14224 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
14225 {
14226 struct type *this_type;
14227
14228 this_type = get_die_type (die, cu);
14229 if (this_type)
14230 return this_type;
14231
14232 return read_type_die_1 (die, cu);
14233 }
14234
14235 /* Read the type in DIE, CU.
14236 Returns NULL for invalid types. */
14237
14238 static struct type *
14239 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
14240 {
14241 struct type *this_type = NULL;
14242
14243 switch (die->tag)
14244 {
14245 case DW_TAG_class_type:
14246 case DW_TAG_interface_type:
14247 case DW_TAG_structure_type:
14248 case DW_TAG_union_type:
14249 this_type = read_structure_type (die, cu);
14250 break;
14251 case DW_TAG_enumeration_type:
14252 this_type = read_enumeration_type (die, cu);
14253 break;
14254 case DW_TAG_subprogram:
14255 case DW_TAG_subroutine_type:
14256 case DW_TAG_inlined_subroutine:
14257 this_type = read_subroutine_type (die, cu);
14258 break;
14259 case DW_TAG_array_type:
14260 this_type = read_array_type (die, cu);
14261 break;
14262 case DW_TAG_set_type:
14263 this_type = read_set_type (die, cu);
14264 break;
14265 case DW_TAG_pointer_type:
14266 this_type = read_tag_pointer_type (die, cu);
14267 break;
14268 case DW_TAG_ptr_to_member_type:
14269 this_type = read_tag_ptr_to_member_type (die, cu);
14270 break;
14271 case DW_TAG_reference_type:
14272 this_type = read_tag_reference_type (die, cu);
14273 break;
14274 case DW_TAG_const_type:
14275 this_type = read_tag_const_type (die, cu);
14276 break;
14277 case DW_TAG_volatile_type:
14278 this_type = read_tag_volatile_type (die, cu);
14279 break;
14280 case DW_TAG_string_type:
14281 this_type = read_tag_string_type (die, cu);
14282 break;
14283 case DW_TAG_typedef:
14284 this_type = read_typedef (die, cu);
14285 break;
14286 case DW_TAG_subrange_type:
14287 this_type = read_subrange_type (die, cu);
14288 break;
14289 case DW_TAG_base_type:
14290 this_type = read_base_type (die, cu);
14291 break;
14292 case DW_TAG_unspecified_type:
14293 this_type = read_unspecified_type (die, cu);
14294 break;
14295 case DW_TAG_namespace:
14296 this_type = read_namespace_type (die, cu);
14297 break;
14298 case DW_TAG_module:
14299 this_type = read_module_type (die, cu);
14300 break;
14301 default:
14302 complaint (&symfile_complaints,
14303 _("unexpected tag in read_type_die: '%s'"),
14304 dwarf_tag_name (die->tag));
14305 break;
14306 }
14307
14308 return this_type;
14309 }
14310
14311 /* See if we can figure out if the class lives in a namespace. We do
14312 this by looking for a member function; its demangled name will
14313 contain namespace info, if there is any.
14314 Return the computed name or NULL.
14315 Space for the result is allocated on the objfile's obstack.
14316 This is the full-die version of guess_partial_die_structure_name.
14317 In this case we know DIE has no useful parent. */
14318
14319 static char *
14320 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
14321 {
14322 struct die_info *spec_die;
14323 struct dwarf2_cu *spec_cu;
14324 struct die_info *child;
14325
14326 spec_cu = cu;
14327 spec_die = die_specification (die, &spec_cu);
14328 if (spec_die != NULL)
14329 {
14330 die = spec_die;
14331 cu = spec_cu;
14332 }
14333
14334 for (child = die->child;
14335 child != NULL;
14336 child = child->sibling)
14337 {
14338 if (child->tag == DW_TAG_subprogram)
14339 {
14340 struct attribute *attr;
14341
14342 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
14343 if (attr == NULL)
14344 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
14345 if (attr != NULL)
14346 {
14347 char *actual_name
14348 = language_class_name_from_physname (cu->language_defn,
14349 DW_STRING (attr));
14350 char *name = NULL;
14351
14352 if (actual_name != NULL)
14353 {
14354 char *die_name = dwarf2_name (die, cu);
14355
14356 if (die_name != NULL
14357 && strcmp (die_name, actual_name) != 0)
14358 {
14359 /* Strip off the class name from the full name.
14360 We want the prefix. */
14361 int die_name_len = strlen (die_name);
14362 int actual_name_len = strlen (actual_name);
14363
14364 /* Test for '::' as a sanity check. */
14365 if (actual_name_len > die_name_len + 2
14366 && actual_name[actual_name_len
14367 - die_name_len - 1] == ':')
14368 name =
14369 obsavestring (actual_name,
14370 actual_name_len - die_name_len - 2,
14371 &cu->objfile->objfile_obstack);
14372 }
14373 }
14374 xfree (actual_name);
14375 return name;
14376 }
14377 }
14378 }
14379
14380 return NULL;
14381 }
14382
14383 /* GCC might emit a nameless typedef that has a linkage name. Determine the
14384 prefix part in such case. See
14385 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14386
14387 static char *
14388 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
14389 {
14390 struct attribute *attr;
14391 char *base;
14392
14393 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
14394 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
14395 return NULL;
14396
14397 attr = dwarf2_attr (die, DW_AT_name, cu);
14398 if (attr != NULL && DW_STRING (attr) != NULL)
14399 return NULL;
14400
14401 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
14402 if (attr == NULL)
14403 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
14404 if (attr == NULL || DW_STRING (attr) == NULL)
14405 return NULL;
14406
14407 /* dwarf2_name had to be already called. */
14408 gdb_assert (DW_STRING_IS_CANONICAL (attr));
14409
14410 /* Strip the base name, keep any leading namespaces/classes. */
14411 base = strrchr (DW_STRING (attr), ':');
14412 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
14413 return "";
14414
14415 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
14416 &cu->objfile->objfile_obstack);
14417 }
14418
14419 /* Return the name of the namespace/class that DIE is defined within,
14420 or "" if we can't tell. The caller should not xfree the result.
14421
14422 For example, if we're within the method foo() in the following
14423 code:
14424
14425 namespace N {
14426 class C {
14427 void foo () {
14428 }
14429 };
14430 }
14431
14432 then determine_prefix on foo's die will return "N::C". */
14433
14434 static const char *
14435 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
14436 {
14437 struct die_info *parent, *spec_die;
14438 struct dwarf2_cu *spec_cu;
14439 struct type *parent_type;
14440 char *retval;
14441
14442 if (cu->language != language_cplus && cu->language != language_java
14443 && cu->language != language_fortran)
14444 return "";
14445
14446 retval = anonymous_struct_prefix (die, cu);
14447 if (retval)
14448 return retval;
14449
14450 /* We have to be careful in the presence of DW_AT_specification.
14451 For example, with GCC 3.4, given the code
14452
14453 namespace N {
14454 void foo() {
14455 // Definition of N::foo.
14456 }
14457 }
14458
14459 then we'll have a tree of DIEs like this:
14460
14461 1: DW_TAG_compile_unit
14462 2: DW_TAG_namespace // N
14463 3: DW_TAG_subprogram // declaration of N::foo
14464 4: DW_TAG_subprogram // definition of N::foo
14465 DW_AT_specification // refers to die #3
14466
14467 Thus, when processing die #4, we have to pretend that we're in
14468 the context of its DW_AT_specification, namely the contex of die
14469 #3. */
14470 spec_cu = cu;
14471 spec_die = die_specification (die, &spec_cu);
14472 if (spec_die == NULL)
14473 parent = die->parent;
14474 else
14475 {
14476 parent = spec_die->parent;
14477 cu = spec_cu;
14478 }
14479
14480 if (parent == NULL)
14481 return "";
14482 else if (parent->building_fullname)
14483 {
14484 const char *name;
14485 const char *parent_name;
14486
14487 /* It has been seen on RealView 2.2 built binaries,
14488 DW_TAG_template_type_param types actually _defined_ as
14489 children of the parent class:
14490
14491 enum E {};
14492 template class <class Enum> Class{};
14493 Class<enum E> class_e;
14494
14495 1: DW_TAG_class_type (Class)
14496 2: DW_TAG_enumeration_type (E)
14497 3: DW_TAG_enumerator (enum1:0)
14498 3: DW_TAG_enumerator (enum2:1)
14499 ...
14500 2: DW_TAG_template_type_param
14501 DW_AT_type DW_FORM_ref_udata (E)
14502
14503 Besides being broken debug info, it can put GDB into an
14504 infinite loop. Consider:
14505
14506 When we're building the full name for Class<E>, we'll start
14507 at Class, and go look over its template type parameters,
14508 finding E. We'll then try to build the full name of E, and
14509 reach here. We're now trying to build the full name of E,
14510 and look over the parent DIE for containing scope. In the
14511 broken case, if we followed the parent DIE of E, we'd again
14512 find Class, and once again go look at its template type
14513 arguments, etc., etc. Simply don't consider such parent die
14514 as source-level parent of this die (it can't be, the language
14515 doesn't allow it), and break the loop here. */
14516 name = dwarf2_name (die, cu);
14517 parent_name = dwarf2_name (parent, cu);
14518 complaint (&symfile_complaints,
14519 _("template param type '%s' defined within parent '%s'"),
14520 name ? name : "<unknown>",
14521 parent_name ? parent_name : "<unknown>");
14522 return "";
14523 }
14524 else
14525 switch (parent->tag)
14526 {
14527 case DW_TAG_namespace:
14528 parent_type = read_type_die (parent, cu);
14529 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
14530 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
14531 Work around this problem here. */
14532 if (cu->language == language_cplus
14533 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
14534 return "";
14535 /* We give a name to even anonymous namespaces. */
14536 return TYPE_TAG_NAME (parent_type);
14537 case DW_TAG_class_type:
14538 case DW_TAG_interface_type:
14539 case DW_TAG_structure_type:
14540 case DW_TAG_union_type:
14541 case DW_TAG_module:
14542 parent_type = read_type_die (parent, cu);
14543 if (TYPE_TAG_NAME (parent_type) != NULL)
14544 return TYPE_TAG_NAME (parent_type);
14545 else
14546 /* An anonymous structure is only allowed non-static data
14547 members; no typedefs, no member functions, et cetera.
14548 So it does not need a prefix. */
14549 return "";
14550 case DW_TAG_compile_unit:
14551 case DW_TAG_partial_unit:
14552 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
14553 if (cu->language == language_cplus
14554 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
14555 && die->child != NULL
14556 && (die->tag == DW_TAG_class_type
14557 || die->tag == DW_TAG_structure_type
14558 || die->tag == DW_TAG_union_type))
14559 {
14560 char *name = guess_full_die_structure_name (die, cu);
14561 if (name != NULL)
14562 return name;
14563 }
14564 return "";
14565 default:
14566 return determine_prefix (parent, cu);
14567 }
14568 }
14569
14570 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
14571 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
14572 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
14573 an obconcat, otherwise allocate storage for the result. The CU argument is
14574 used to determine the language and hence, the appropriate separator. */
14575
14576 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
14577
14578 static char *
14579 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
14580 int physname, struct dwarf2_cu *cu)
14581 {
14582 const char *lead = "";
14583 const char *sep;
14584
14585 if (suffix == NULL || suffix[0] == '\0'
14586 || prefix == NULL || prefix[0] == '\0')
14587 sep = "";
14588 else if (cu->language == language_java)
14589 sep = ".";
14590 else if (cu->language == language_fortran && physname)
14591 {
14592 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
14593 DW_AT_MIPS_linkage_name is preferred and used instead. */
14594
14595 lead = "__";
14596 sep = "_MOD_";
14597 }
14598 else
14599 sep = "::";
14600
14601 if (prefix == NULL)
14602 prefix = "";
14603 if (suffix == NULL)
14604 suffix = "";
14605
14606 if (obs == NULL)
14607 {
14608 char *retval
14609 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
14610
14611 strcpy (retval, lead);
14612 strcat (retval, prefix);
14613 strcat (retval, sep);
14614 strcat (retval, suffix);
14615 return retval;
14616 }
14617 else
14618 {
14619 /* We have an obstack. */
14620 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
14621 }
14622 }
14623
14624 /* Return sibling of die, NULL if no sibling. */
14625
14626 static struct die_info *
14627 sibling_die (struct die_info *die)
14628 {
14629 return die->sibling;
14630 }
14631
14632 /* Get name of a die, return NULL if not found. */
14633
14634 static char *
14635 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
14636 struct obstack *obstack)
14637 {
14638 if (name && cu->language == language_cplus)
14639 {
14640 char *canon_name = cp_canonicalize_string (name);
14641
14642 if (canon_name != NULL)
14643 {
14644 if (strcmp (canon_name, name) != 0)
14645 name = obsavestring (canon_name, strlen (canon_name),
14646 obstack);
14647 xfree (canon_name);
14648 }
14649 }
14650
14651 return name;
14652 }
14653
14654 /* Get name of a die, return NULL if not found. */
14655
14656 static char *
14657 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
14658 {
14659 struct attribute *attr;
14660
14661 attr = dwarf2_attr (die, DW_AT_name, cu);
14662 if ((!attr || !DW_STRING (attr))
14663 && die->tag != DW_TAG_class_type
14664 && die->tag != DW_TAG_interface_type
14665 && die->tag != DW_TAG_structure_type
14666 && die->tag != DW_TAG_union_type)
14667 return NULL;
14668
14669 switch (die->tag)
14670 {
14671 case DW_TAG_compile_unit:
14672 case DW_TAG_partial_unit:
14673 /* Compilation units have a DW_AT_name that is a filename, not
14674 a source language identifier. */
14675 case DW_TAG_enumeration_type:
14676 case DW_TAG_enumerator:
14677 /* These tags always have simple identifiers already; no need
14678 to canonicalize them. */
14679 return DW_STRING (attr);
14680
14681 case DW_TAG_subprogram:
14682 /* Java constructors will all be named "<init>", so return
14683 the class name when we see this special case. */
14684 if (cu->language == language_java
14685 && DW_STRING (attr) != NULL
14686 && strcmp (DW_STRING (attr), "<init>") == 0)
14687 {
14688 struct dwarf2_cu *spec_cu = cu;
14689 struct die_info *spec_die;
14690
14691 /* GCJ will output '<init>' for Java constructor names.
14692 For this special case, return the name of the parent class. */
14693
14694 /* GCJ may output suprogram DIEs with AT_specification set.
14695 If so, use the name of the specified DIE. */
14696 spec_die = die_specification (die, &spec_cu);
14697 if (spec_die != NULL)
14698 return dwarf2_name (spec_die, spec_cu);
14699
14700 do
14701 {
14702 die = die->parent;
14703 if (die->tag == DW_TAG_class_type)
14704 return dwarf2_name (die, cu);
14705 }
14706 while (die->tag != DW_TAG_compile_unit
14707 && die->tag != DW_TAG_partial_unit);
14708 }
14709 break;
14710
14711 case DW_TAG_class_type:
14712 case DW_TAG_interface_type:
14713 case DW_TAG_structure_type:
14714 case DW_TAG_union_type:
14715 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
14716 structures or unions. These were of the form "._%d" in GCC 4.1,
14717 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
14718 and GCC 4.4. We work around this problem by ignoring these. */
14719 if (attr && DW_STRING (attr)
14720 && (strncmp (DW_STRING (attr), "._", 2) == 0
14721 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
14722 return NULL;
14723
14724 /* GCC might emit a nameless typedef that has a linkage name. See
14725 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14726 if (!attr || DW_STRING (attr) == NULL)
14727 {
14728 char *demangled = NULL;
14729
14730 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
14731 if (attr == NULL)
14732 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
14733
14734 if (attr == NULL || DW_STRING (attr) == NULL)
14735 return NULL;
14736
14737 /* Avoid demangling DW_STRING (attr) the second time on a second
14738 call for the same DIE. */
14739 if (!DW_STRING_IS_CANONICAL (attr))
14740 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
14741
14742 if (demangled)
14743 {
14744 char *base;
14745
14746 /* FIXME: we already did this for the partial symbol... */
14747 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
14748 &cu->objfile->objfile_obstack);
14749 DW_STRING_IS_CANONICAL (attr) = 1;
14750 xfree (demangled);
14751
14752 /* Strip any leading namespaces/classes, keep only the base name.
14753 DW_AT_name for named DIEs does not contain the prefixes. */
14754 base = strrchr (DW_STRING (attr), ':');
14755 if (base && base > DW_STRING (attr) && base[-1] == ':')
14756 return &base[1];
14757 else
14758 return DW_STRING (attr);
14759 }
14760 }
14761 break;
14762
14763 default:
14764 break;
14765 }
14766
14767 if (!DW_STRING_IS_CANONICAL (attr))
14768 {
14769 DW_STRING (attr)
14770 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
14771 &cu->objfile->objfile_obstack);
14772 DW_STRING_IS_CANONICAL (attr) = 1;
14773 }
14774 return DW_STRING (attr);
14775 }
14776
14777 /* Return the die that this die in an extension of, or NULL if there
14778 is none. *EXT_CU is the CU containing DIE on input, and the CU
14779 containing the return value on output. */
14780
14781 static struct die_info *
14782 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
14783 {
14784 struct attribute *attr;
14785
14786 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
14787 if (attr == NULL)
14788 return NULL;
14789
14790 return follow_die_ref (die, attr, ext_cu);
14791 }
14792
14793 /* Convert a DIE tag into its string name. */
14794
14795 static const char *
14796 dwarf_tag_name (unsigned tag)
14797 {
14798 const char *name = get_DW_TAG_name (tag);
14799
14800 if (name == NULL)
14801 return "DW_TAG_<unknown>";
14802
14803 return name;
14804 }
14805
14806 /* Convert a DWARF attribute code into its string name. */
14807
14808 static const char *
14809 dwarf_attr_name (unsigned attr)
14810 {
14811 const char *name;
14812
14813 #ifdef MIPS /* collides with DW_AT_HP_block_index */
14814 if (attr == DW_AT_MIPS_fde)
14815 return "DW_AT_MIPS_fde";
14816 #else
14817 if (attr == DW_AT_HP_block_index)
14818 return "DW_AT_HP_block_index";
14819 #endif
14820
14821 name = get_DW_AT_name (attr);
14822
14823 if (name == NULL)
14824 return "DW_AT_<unknown>";
14825
14826 return name;
14827 }
14828
14829 /* Convert a DWARF value form code into its string name. */
14830
14831 static const char *
14832 dwarf_form_name (unsigned form)
14833 {
14834 const char *name = get_DW_FORM_name (form);
14835
14836 if (name == NULL)
14837 return "DW_FORM_<unknown>";
14838
14839 return name;
14840 }
14841
14842 static char *
14843 dwarf_bool_name (unsigned mybool)
14844 {
14845 if (mybool)
14846 return "TRUE";
14847 else
14848 return "FALSE";
14849 }
14850
14851 /* Convert a DWARF type code into its string name. */
14852
14853 static const char *
14854 dwarf_type_encoding_name (unsigned enc)
14855 {
14856 const char *name = get_DW_ATE_name (enc);
14857
14858 if (name == NULL)
14859 return "DW_ATE_<unknown>";
14860
14861 return name;
14862 }
14863
14864 static void
14865 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
14866 {
14867 unsigned int i;
14868
14869 print_spaces (indent, f);
14870 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
14871 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
14872
14873 if (die->parent != NULL)
14874 {
14875 print_spaces (indent, f);
14876 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
14877 die->parent->offset.sect_off);
14878 }
14879
14880 print_spaces (indent, f);
14881 fprintf_unfiltered (f, " has children: %s\n",
14882 dwarf_bool_name (die->child != NULL));
14883
14884 print_spaces (indent, f);
14885 fprintf_unfiltered (f, " attributes:\n");
14886
14887 for (i = 0; i < die->num_attrs; ++i)
14888 {
14889 print_spaces (indent, f);
14890 fprintf_unfiltered (f, " %s (%s) ",
14891 dwarf_attr_name (die->attrs[i].name),
14892 dwarf_form_name (die->attrs[i].form));
14893
14894 switch (die->attrs[i].form)
14895 {
14896 case DW_FORM_addr:
14897 case DW_FORM_GNU_addr_index:
14898 fprintf_unfiltered (f, "address: ");
14899 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
14900 break;
14901 case DW_FORM_block2:
14902 case DW_FORM_block4:
14903 case DW_FORM_block:
14904 case DW_FORM_block1:
14905 fprintf_unfiltered (f, "block: size %d",
14906 DW_BLOCK (&die->attrs[i])->size);
14907 break;
14908 case DW_FORM_exprloc:
14909 fprintf_unfiltered (f, "expression: size %u",
14910 DW_BLOCK (&die->attrs[i])->size);
14911 break;
14912 case DW_FORM_ref_addr:
14913 fprintf_unfiltered (f, "ref address: ");
14914 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
14915 break;
14916 case DW_FORM_ref1:
14917 case DW_FORM_ref2:
14918 case DW_FORM_ref4:
14919 case DW_FORM_ref8:
14920 case DW_FORM_ref_udata:
14921 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
14922 (long) (DW_UNSND (&die->attrs[i])));
14923 break;
14924 case DW_FORM_data1:
14925 case DW_FORM_data2:
14926 case DW_FORM_data4:
14927 case DW_FORM_data8:
14928 case DW_FORM_udata:
14929 case DW_FORM_sdata:
14930 fprintf_unfiltered (f, "constant: %s",
14931 pulongest (DW_UNSND (&die->attrs[i])));
14932 break;
14933 case DW_FORM_sec_offset:
14934 fprintf_unfiltered (f, "section offset: %s",
14935 pulongest (DW_UNSND (&die->attrs[i])));
14936 break;
14937 case DW_FORM_ref_sig8:
14938 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
14939 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
14940 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
14941 else
14942 fprintf_unfiltered (f, "signatured type, offset: unknown");
14943 break;
14944 case DW_FORM_string:
14945 case DW_FORM_strp:
14946 case DW_FORM_GNU_str_index:
14947 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
14948 DW_STRING (&die->attrs[i])
14949 ? DW_STRING (&die->attrs[i]) : "",
14950 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
14951 break;
14952 case DW_FORM_flag:
14953 if (DW_UNSND (&die->attrs[i]))
14954 fprintf_unfiltered (f, "flag: TRUE");
14955 else
14956 fprintf_unfiltered (f, "flag: FALSE");
14957 break;
14958 case DW_FORM_flag_present:
14959 fprintf_unfiltered (f, "flag: TRUE");
14960 break;
14961 case DW_FORM_indirect:
14962 /* The reader will have reduced the indirect form to
14963 the "base form" so this form should not occur. */
14964 fprintf_unfiltered (f,
14965 "unexpected attribute form: DW_FORM_indirect");
14966 break;
14967 default:
14968 fprintf_unfiltered (f, "unsupported attribute form: %d.",
14969 die->attrs[i].form);
14970 break;
14971 }
14972 fprintf_unfiltered (f, "\n");
14973 }
14974 }
14975
14976 static void
14977 dump_die_for_error (struct die_info *die)
14978 {
14979 dump_die_shallow (gdb_stderr, 0, die);
14980 }
14981
14982 static void
14983 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
14984 {
14985 int indent = level * 4;
14986
14987 gdb_assert (die != NULL);
14988
14989 if (level >= max_level)
14990 return;
14991
14992 dump_die_shallow (f, indent, die);
14993
14994 if (die->child != NULL)
14995 {
14996 print_spaces (indent, f);
14997 fprintf_unfiltered (f, " Children:");
14998 if (level + 1 < max_level)
14999 {
15000 fprintf_unfiltered (f, "\n");
15001 dump_die_1 (f, level + 1, max_level, die->child);
15002 }
15003 else
15004 {
15005 fprintf_unfiltered (f,
15006 " [not printed, max nesting level reached]\n");
15007 }
15008 }
15009
15010 if (die->sibling != NULL && level > 0)
15011 {
15012 dump_die_1 (f, level, max_level, die->sibling);
15013 }
15014 }
15015
15016 /* This is called from the pdie macro in gdbinit.in.
15017 It's not static so gcc will keep a copy callable from gdb. */
15018
15019 void
15020 dump_die (struct die_info *die, int max_level)
15021 {
15022 dump_die_1 (gdb_stdlog, 0, max_level, die);
15023 }
15024
15025 static void
15026 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
15027 {
15028 void **slot;
15029
15030 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
15031 INSERT);
15032
15033 *slot = die;
15034 }
15035
15036 /* DW_ADDR is always stored already as sect_offset; despite for the forms
15037 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
15038
15039 static int
15040 is_ref_attr (struct attribute *attr)
15041 {
15042 switch (attr->form)
15043 {
15044 case DW_FORM_ref_addr:
15045 case DW_FORM_ref1:
15046 case DW_FORM_ref2:
15047 case DW_FORM_ref4:
15048 case DW_FORM_ref8:
15049 case DW_FORM_ref_udata:
15050 return 1;
15051 default:
15052 return 0;
15053 }
15054 }
15055
15056 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
15057 required kind. */
15058
15059 static sect_offset
15060 dwarf2_get_ref_die_offset (struct attribute *attr)
15061 {
15062 sect_offset retval = { DW_UNSND (attr) };
15063
15064 if (is_ref_attr (attr))
15065 return retval;
15066
15067 retval.sect_off = 0;
15068 complaint (&symfile_complaints,
15069 _("unsupported die ref attribute form: '%s'"),
15070 dwarf_form_name (attr->form));
15071 return retval;
15072 }
15073
15074 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
15075 * the value held by the attribute is not constant. */
15076
15077 static LONGEST
15078 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
15079 {
15080 if (attr->form == DW_FORM_sdata)
15081 return DW_SND (attr);
15082 else if (attr->form == DW_FORM_udata
15083 || attr->form == DW_FORM_data1
15084 || attr->form == DW_FORM_data2
15085 || attr->form == DW_FORM_data4
15086 || attr->form == DW_FORM_data8)
15087 return DW_UNSND (attr);
15088 else
15089 {
15090 complaint (&symfile_complaints,
15091 _("Attribute value is not a constant (%s)"),
15092 dwarf_form_name (attr->form));
15093 return default_value;
15094 }
15095 }
15096
15097 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
15098 unit and add it to our queue.
15099 The result is non-zero if PER_CU was queued, otherwise the result is zero
15100 meaning either PER_CU is already queued or it is already loaded. */
15101
15102 static int
15103 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
15104 struct dwarf2_per_cu_data *per_cu,
15105 enum language pretend_language)
15106 {
15107 /* We may arrive here during partial symbol reading, if we need full
15108 DIEs to process an unusual case (e.g. template arguments). Do
15109 not queue PER_CU, just tell our caller to load its DIEs. */
15110 if (dwarf2_per_objfile->reading_partial_symbols)
15111 {
15112 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
15113 return 1;
15114 return 0;
15115 }
15116
15117 /* Mark the dependence relation so that we don't flush PER_CU
15118 too early. */
15119 dwarf2_add_dependence (this_cu, per_cu);
15120
15121 /* If it's already on the queue, we have nothing to do. */
15122 if (per_cu->queued)
15123 return 0;
15124
15125 /* If the compilation unit is already loaded, just mark it as
15126 used. */
15127 if (per_cu->cu != NULL)
15128 {
15129 per_cu->cu->last_used = 0;
15130 return 0;
15131 }
15132
15133 /* Add it to the queue. */
15134 queue_comp_unit (per_cu, pretend_language);
15135
15136 return 1;
15137 }
15138
15139 /* Follow reference or signature attribute ATTR of SRC_DIE.
15140 On entry *REF_CU is the CU of SRC_DIE.
15141 On exit *REF_CU is the CU of the result. */
15142
15143 static struct die_info *
15144 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
15145 struct dwarf2_cu **ref_cu)
15146 {
15147 struct die_info *die;
15148
15149 if (is_ref_attr (attr))
15150 die = follow_die_ref (src_die, attr, ref_cu);
15151 else if (attr->form == DW_FORM_ref_sig8)
15152 die = follow_die_sig (src_die, attr, ref_cu);
15153 else
15154 {
15155 dump_die_for_error (src_die);
15156 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
15157 (*ref_cu)->objfile->name);
15158 }
15159
15160 return die;
15161 }
15162
15163 /* Follow reference OFFSET.
15164 On entry *REF_CU is the CU of the source die referencing OFFSET.
15165 On exit *REF_CU is the CU of the result.
15166 Returns NULL if OFFSET is invalid. */
15167
15168 static struct die_info *
15169 follow_die_offset (sect_offset offset, struct dwarf2_cu **ref_cu)
15170 {
15171 struct die_info temp_die;
15172 struct dwarf2_cu *target_cu, *cu = *ref_cu;
15173
15174 gdb_assert (cu->per_cu != NULL);
15175
15176 target_cu = cu;
15177
15178 if (cu->per_cu->is_debug_types)
15179 {
15180 /* .debug_types CUs cannot reference anything outside their CU.
15181 If they need to, they have to reference a signatured type via
15182 DW_FORM_ref_sig8. */
15183 if (! offset_in_cu_p (&cu->header, offset))
15184 return NULL;
15185 }
15186 else if (! offset_in_cu_p (&cu->header, offset))
15187 {
15188 struct dwarf2_per_cu_data *per_cu;
15189
15190 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
15191
15192 /* If necessary, add it to the queue and load its DIEs. */
15193 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
15194 load_full_comp_unit (per_cu, cu->language);
15195
15196 target_cu = per_cu->cu;
15197 }
15198 else if (cu->dies == NULL)
15199 {
15200 /* We're loading full DIEs during partial symbol reading. */
15201 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
15202 load_full_comp_unit (cu->per_cu, language_minimal);
15203 }
15204
15205 *ref_cu = target_cu;
15206 temp_die.offset = offset;
15207 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
15208 }
15209
15210 /* Follow reference attribute ATTR of SRC_DIE.
15211 On entry *REF_CU is the CU of SRC_DIE.
15212 On exit *REF_CU is the CU of the result. */
15213
15214 static struct die_info *
15215 follow_die_ref (struct die_info *src_die, struct attribute *attr,
15216 struct dwarf2_cu **ref_cu)
15217 {
15218 sect_offset offset = dwarf2_get_ref_die_offset (attr);
15219 struct dwarf2_cu *cu = *ref_cu;
15220 struct die_info *die;
15221
15222 die = follow_die_offset (offset, ref_cu);
15223 if (!die)
15224 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
15225 "at 0x%x [in module %s]"),
15226 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
15227
15228 return die;
15229 }
15230
15231 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
15232 Returned value is intended for DW_OP_call*. Returned
15233 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
15234
15235 struct dwarf2_locexpr_baton
15236 dwarf2_fetch_die_location_block (cu_offset offset_in_cu,
15237 struct dwarf2_per_cu_data *per_cu,
15238 CORE_ADDR (*get_frame_pc) (void *baton),
15239 void *baton)
15240 {
15241 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
15242 struct dwarf2_cu *cu;
15243 struct die_info *die;
15244 struct attribute *attr;
15245 struct dwarf2_locexpr_baton retval;
15246
15247 dw2_setup (per_cu->objfile);
15248
15249 if (per_cu->cu == NULL)
15250 load_cu (per_cu);
15251 cu = per_cu->cu;
15252
15253 die = follow_die_offset (offset, &cu);
15254 if (!die)
15255 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
15256 offset.sect_off, per_cu->objfile->name);
15257
15258 attr = dwarf2_attr (die, DW_AT_location, cu);
15259 if (!attr)
15260 {
15261 /* DWARF: "If there is no such attribute, then there is no effect.".
15262 DATA is ignored if SIZE is 0. */
15263
15264 retval.data = NULL;
15265 retval.size = 0;
15266 }
15267 else if (attr_form_is_section_offset (attr))
15268 {
15269 struct dwarf2_loclist_baton loclist_baton;
15270 CORE_ADDR pc = (*get_frame_pc) (baton);
15271 size_t size;
15272
15273 fill_in_loclist_baton (cu, &loclist_baton, attr);
15274
15275 retval.data = dwarf2_find_location_expression (&loclist_baton,
15276 &size, pc);
15277 retval.size = size;
15278 }
15279 else
15280 {
15281 if (!attr_form_is_block (attr))
15282 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
15283 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
15284 offset.sect_off, per_cu->objfile->name);
15285
15286 retval.data = DW_BLOCK (attr)->data;
15287 retval.size = DW_BLOCK (attr)->size;
15288 }
15289 retval.per_cu = cu->per_cu;
15290
15291 age_cached_comp_units ();
15292
15293 return retval;
15294 }
15295
15296 /* Return the type of the DIE at DIE_OFFSET in the CU named by
15297 PER_CU. */
15298
15299 struct type *
15300 dwarf2_get_die_type (cu_offset die_offset,
15301 struct dwarf2_per_cu_data *per_cu)
15302 {
15303 sect_offset die_offset_sect;
15304
15305 dw2_setup (per_cu->objfile);
15306
15307 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
15308 return get_die_type_at_offset (die_offset_sect, per_cu);
15309 }
15310
15311 /* Follow the signature attribute ATTR in SRC_DIE.
15312 On entry *REF_CU is the CU of SRC_DIE.
15313 On exit *REF_CU is the CU of the result. */
15314
15315 static struct die_info *
15316 follow_die_sig (struct die_info *src_die, struct attribute *attr,
15317 struct dwarf2_cu **ref_cu)
15318 {
15319 struct objfile *objfile = (*ref_cu)->objfile;
15320 struct die_info temp_die;
15321 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
15322 struct dwarf2_cu *sig_cu;
15323 struct die_info *die;
15324
15325 /* sig_type will be NULL if the signatured type is missing from
15326 the debug info. */
15327 if (sig_type == NULL)
15328 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
15329 "at 0x%x [in module %s]"),
15330 src_die->offset.sect_off, objfile->name);
15331
15332 /* If necessary, add it to the queue and load its DIEs. */
15333
15334 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
15335 read_signatured_type (sig_type);
15336
15337 gdb_assert (sig_type->per_cu.cu != NULL);
15338
15339 sig_cu = sig_type->per_cu.cu;
15340 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
15341 temp_die.offset = sig_type->type_offset_in_section;
15342 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
15343 temp_die.offset.sect_off);
15344 if (die)
15345 {
15346 *ref_cu = sig_cu;
15347 return die;
15348 }
15349
15350 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
15351 "from DIE at 0x%x [in module %s]"),
15352 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
15353 }
15354
15355 /* Given an offset of a signatured type, return its signatured_type. */
15356
15357 static struct signatured_type *
15358 lookup_signatured_type_at_offset (struct objfile *objfile,
15359 struct dwarf2_section_info *section,
15360 sect_offset offset)
15361 {
15362 gdb_byte *info_ptr = section->buffer + offset.sect_off;
15363 unsigned int length, initial_length_size;
15364 unsigned int sig_offset;
15365 struct signatured_type find_entry, *sig_type;
15366
15367 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
15368 sig_offset = (initial_length_size
15369 + 2 /*version*/
15370 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
15371 + 1 /*address_size*/);
15372 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
15373 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
15374
15375 /* This is only used to lookup previously recorded types.
15376 If we didn't find it, it's our bug. */
15377 gdb_assert (sig_type != NULL);
15378 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
15379
15380 return sig_type;
15381 }
15382
15383 /* Load the DIEs associated with type unit PER_CU into memory. */
15384
15385 static void
15386 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
15387 {
15388 struct objfile *objfile = per_cu->objfile;
15389 struct dwarf2_section_info *sect = per_cu->info_or_types_section;
15390 sect_offset offset = per_cu->offset;
15391 struct signatured_type *sig_type;
15392
15393 dwarf2_read_section (objfile, sect);
15394
15395 /* We have the section offset, but we need the signature to do the
15396 hash table lookup. */
15397 /* FIXME: This is sorta unnecessary, read_signatured_type only uses
15398 the signature to assert we found the right one.
15399 Ok, but it's a lot of work. We should simplify things so any needed
15400 assert doesn't require all this clumsiness. */
15401 sig_type = lookup_signatured_type_at_offset (objfile, sect, offset);
15402
15403 gdb_assert (&sig_type->per_cu == per_cu);
15404 gdb_assert (sig_type->per_cu.cu == NULL);
15405
15406 read_signatured_type (sig_type);
15407
15408 gdb_assert (sig_type->per_cu.cu != NULL);
15409 }
15410
15411 /* die_reader_func for read_signatured_type.
15412 This is identical to load_full_comp_unit_reader,
15413 but is kept separate for now. */
15414
15415 static void
15416 read_signatured_type_reader (const struct die_reader_specs *reader,
15417 gdb_byte *info_ptr,
15418 struct die_info *comp_unit_die,
15419 int has_children,
15420 void *data)
15421 {
15422 struct dwarf2_cu *cu = reader->cu;
15423
15424 gdb_assert (cu->die_hash == NULL);
15425 cu->die_hash =
15426 htab_create_alloc_ex (cu->header.length / 12,
15427 die_hash,
15428 die_eq,
15429 NULL,
15430 &cu->comp_unit_obstack,
15431 hashtab_obstack_allocate,
15432 dummy_obstack_deallocate);
15433
15434 if (has_children)
15435 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
15436 &info_ptr, comp_unit_die);
15437 cu->dies = comp_unit_die;
15438 /* comp_unit_die is not stored in die_hash, no need. */
15439
15440 /* We try not to read any attributes in this function, because not
15441 all CUs needed for references have been loaded yet, and symbol
15442 table processing isn't initialized. But we have to set the CU language,
15443 or we won't be able to build types correctly.
15444 Similarly, if we do not read the producer, we can not apply
15445 producer-specific interpretation. */
15446 prepare_one_comp_unit (cu, cu->dies, language_minimal);
15447 }
15448
15449 /* Read in a signatured type and build its CU and DIEs.
15450 If the type is a stub for the real type in a DWO file,
15451 read in the real type from the DWO file as well. */
15452
15453 static void
15454 read_signatured_type (struct signatured_type *sig_type)
15455 {
15456 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
15457
15458 gdb_assert (per_cu->is_debug_types);
15459 gdb_assert (per_cu->cu == NULL);
15460
15461 init_cutu_and_read_dies (per_cu, 0, 1, read_signatured_type_reader, NULL);
15462 }
15463
15464 /* Decode simple location descriptions.
15465 Given a pointer to a dwarf block that defines a location, compute
15466 the location and return the value.
15467
15468 NOTE drow/2003-11-18: This function is called in two situations
15469 now: for the address of static or global variables (partial symbols
15470 only) and for offsets into structures which are expected to be
15471 (more or less) constant. The partial symbol case should go away,
15472 and only the constant case should remain. That will let this
15473 function complain more accurately. A few special modes are allowed
15474 without complaint for global variables (for instance, global
15475 register values and thread-local values).
15476
15477 A location description containing no operations indicates that the
15478 object is optimized out. The return value is 0 for that case.
15479 FIXME drow/2003-11-16: No callers check for this case any more; soon all
15480 callers will only want a very basic result and this can become a
15481 complaint.
15482
15483 Note that stack[0] is unused except as a default error return. */
15484
15485 static CORE_ADDR
15486 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
15487 {
15488 struct objfile *objfile = cu->objfile;
15489 int i;
15490 int size = blk->size;
15491 gdb_byte *data = blk->data;
15492 CORE_ADDR stack[64];
15493 int stacki;
15494 unsigned int bytes_read, unsnd;
15495 gdb_byte op;
15496
15497 i = 0;
15498 stacki = 0;
15499 stack[stacki] = 0;
15500 stack[++stacki] = 0;
15501
15502 while (i < size)
15503 {
15504 op = data[i++];
15505 switch (op)
15506 {
15507 case DW_OP_lit0:
15508 case DW_OP_lit1:
15509 case DW_OP_lit2:
15510 case DW_OP_lit3:
15511 case DW_OP_lit4:
15512 case DW_OP_lit5:
15513 case DW_OP_lit6:
15514 case DW_OP_lit7:
15515 case DW_OP_lit8:
15516 case DW_OP_lit9:
15517 case DW_OP_lit10:
15518 case DW_OP_lit11:
15519 case DW_OP_lit12:
15520 case DW_OP_lit13:
15521 case DW_OP_lit14:
15522 case DW_OP_lit15:
15523 case DW_OP_lit16:
15524 case DW_OP_lit17:
15525 case DW_OP_lit18:
15526 case DW_OP_lit19:
15527 case DW_OP_lit20:
15528 case DW_OP_lit21:
15529 case DW_OP_lit22:
15530 case DW_OP_lit23:
15531 case DW_OP_lit24:
15532 case DW_OP_lit25:
15533 case DW_OP_lit26:
15534 case DW_OP_lit27:
15535 case DW_OP_lit28:
15536 case DW_OP_lit29:
15537 case DW_OP_lit30:
15538 case DW_OP_lit31:
15539 stack[++stacki] = op - DW_OP_lit0;
15540 break;
15541
15542 case DW_OP_reg0:
15543 case DW_OP_reg1:
15544 case DW_OP_reg2:
15545 case DW_OP_reg3:
15546 case DW_OP_reg4:
15547 case DW_OP_reg5:
15548 case DW_OP_reg6:
15549 case DW_OP_reg7:
15550 case DW_OP_reg8:
15551 case DW_OP_reg9:
15552 case DW_OP_reg10:
15553 case DW_OP_reg11:
15554 case DW_OP_reg12:
15555 case DW_OP_reg13:
15556 case DW_OP_reg14:
15557 case DW_OP_reg15:
15558 case DW_OP_reg16:
15559 case DW_OP_reg17:
15560 case DW_OP_reg18:
15561 case DW_OP_reg19:
15562 case DW_OP_reg20:
15563 case DW_OP_reg21:
15564 case DW_OP_reg22:
15565 case DW_OP_reg23:
15566 case DW_OP_reg24:
15567 case DW_OP_reg25:
15568 case DW_OP_reg26:
15569 case DW_OP_reg27:
15570 case DW_OP_reg28:
15571 case DW_OP_reg29:
15572 case DW_OP_reg30:
15573 case DW_OP_reg31:
15574 stack[++stacki] = op - DW_OP_reg0;
15575 if (i < size)
15576 dwarf2_complex_location_expr_complaint ();
15577 break;
15578
15579 case DW_OP_regx:
15580 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
15581 i += bytes_read;
15582 stack[++stacki] = unsnd;
15583 if (i < size)
15584 dwarf2_complex_location_expr_complaint ();
15585 break;
15586
15587 case DW_OP_addr:
15588 stack[++stacki] = read_address (objfile->obfd, &data[i],
15589 cu, &bytes_read);
15590 i += bytes_read;
15591 break;
15592
15593 case DW_OP_const1u:
15594 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
15595 i += 1;
15596 break;
15597
15598 case DW_OP_const1s:
15599 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
15600 i += 1;
15601 break;
15602
15603 case DW_OP_const2u:
15604 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
15605 i += 2;
15606 break;
15607
15608 case DW_OP_const2s:
15609 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
15610 i += 2;
15611 break;
15612
15613 case DW_OP_const4u:
15614 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
15615 i += 4;
15616 break;
15617
15618 case DW_OP_const4s:
15619 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
15620 i += 4;
15621 break;
15622
15623 case DW_OP_const8u:
15624 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
15625 i += 8;
15626 break;
15627
15628 case DW_OP_constu:
15629 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
15630 &bytes_read);
15631 i += bytes_read;
15632 break;
15633
15634 case DW_OP_consts:
15635 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
15636 i += bytes_read;
15637 break;
15638
15639 case DW_OP_dup:
15640 stack[stacki + 1] = stack[stacki];
15641 stacki++;
15642 break;
15643
15644 case DW_OP_plus:
15645 stack[stacki - 1] += stack[stacki];
15646 stacki--;
15647 break;
15648
15649 case DW_OP_plus_uconst:
15650 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
15651 &bytes_read);
15652 i += bytes_read;
15653 break;
15654
15655 case DW_OP_minus:
15656 stack[stacki - 1] -= stack[stacki];
15657 stacki--;
15658 break;
15659
15660 case DW_OP_deref:
15661 /* If we're not the last op, then we definitely can't encode
15662 this using GDB's address_class enum. This is valid for partial
15663 global symbols, although the variable's address will be bogus
15664 in the psymtab. */
15665 if (i < size)
15666 dwarf2_complex_location_expr_complaint ();
15667 break;
15668
15669 case DW_OP_GNU_push_tls_address:
15670 /* The top of the stack has the offset from the beginning
15671 of the thread control block at which the variable is located. */
15672 /* Nothing should follow this operator, so the top of stack would
15673 be returned. */
15674 /* This is valid for partial global symbols, but the variable's
15675 address will be bogus in the psymtab. Make it always at least
15676 non-zero to not look as a variable garbage collected by linker
15677 which have DW_OP_addr 0. */
15678 if (i < size)
15679 dwarf2_complex_location_expr_complaint ();
15680 stack[stacki]++;
15681 break;
15682
15683 case DW_OP_GNU_uninit:
15684 break;
15685
15686 case DW_OP_GNU_addr_index:
15687 case DW_OP_GNU_const_index:
15688 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
15689 &bytes_read);
15690 i += bytes_read;
15691 break;
15692
15693 default:
15694 {
15695 const char *name = get_DW_OP_name (op);
15696
15697 if (name)
15698 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
15699 name);
15700 else
15701 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
15702 op);
15703 }
15704
15705 return (stack[stacki]);
15706 }
15707
15708 /* Enforce maximum stack depth of SIZE-1 to avoid writing
15709 outside of the allocated space. Also enforce minimum>0. */
15710 if (stacki >= ARRAY_SIZE (stack) - 1)
15711 {
15712 complaint (&symfile_complaints,
15713 _("location description stack overflow"));
15714 return 0;
15715 }
15716
15717 if (stacki <= 0)
15718 {
15719 complaint (&symfile_complaints,
15720 _("location description stack underflow"));
15721 return 0;
15722 }
15723 }
15724 return (stack[stacki]);
15725 }
15726
15727 /* memory allocation interface */
15728
15729 static struct dwarf_block *
15730 dwarf_alloc_block (struct dwarf2_cu *cu)
15731 {
15732 struct dwarf_block *blk;
15733
15734 blk = (struct dwarf_block *)
15735 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
15736 return (blk);
15737 }
15738
15739 static struct abbrev_info *
15740 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
15741 {
15742 struct abbrev_info *abbrev;
15743
15744 abbrev = (struct abbrev_info *)
15745 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
15746 memset (abbrev, 0, sizeof (struct abbrev_info));
15747 return (abbrev);
15748 }
15749
15750 static struct die_info *
15751 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
15752 {
15753 struct die_info *die;
15754 size_t size = sizeof (struct die_info);
15755
15756 if (num_attrs > 1)
15757 size += (num_attrs - 1) * sizeof (struct attribute);
15758
15759 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
15760 memset (die, 0, sizeof (struct die_info));
15761 return (die);
15762 }
15763
15764 \f
15765 /* Macro support. */
15766
15767 /* Return the full name of file number I in *LH's file name table.
15768 Use COMP_DIR as the name of the current directory of the
15769 compilation. The result is allocated using xmalloc; the caller is
15770 responsible for freeing it. */
15771 static char *
15772 file_full_name (int file, struct line_header *lh, const char *comp_dir)
15773 {
15774 /* Is the file number a valid index into the line header's file name
15775 table? Remember that file numbers start with one, not zero. */
15776 if (1 <= file && file <= lh->num_file_names)
15777 {
15778 struct file_entry *fe = &lh->file_names[file - 1];
15779
15780 if (IS_ABSOLUTE_PATH (fe->name))
15781 return xstrdup (fe->name);
15782 else
15783 {
15784 const char *dir;
15785 int dir_len;
15786 char *full_name;
15787
15788 if (fe->dir_index)
15789 dir = lh->include_dirs[fe->dir_index - 1];
15790 else
15791 dir = comp_dir;
15792
15793 if (dir)
15794 {
15795 dir_len = strlen (dir);
15796 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
15797 strcpy (full_name, dir);
15798 full_name[dir_len] = '/';
15799 strcpy (full_name + dir_len + 1, fe->name);
15800 return full_name;
15801 }
15802 else
15803 return xstrdup (fe->name);
15804 }
15805 }
15806 else
15807 {
15808 /* The compiler produced a bogus file number. We can at least
15809 record the macro definitions made in the file, even if we
15810 won't be able to find the file by name. */
15811 char fake_name[80];
15812
15813 sprintf (fake_name, "<bad macro file number %d>", file);
15814
15815 complaint (&symfile_complaints,
15816 _("bad file number in macro information (%d)"),
15817 file);
15818
15819 return xstrdup (fake_name);
15820 }
15821 }
15822
15823
15824 static struct macro_source_file *
15825 macro_start_file (int file, int line,
15826 struct macro_source_file *current_file,
15827 const char *comp_dir,
15828 struct line_header *lh, struct objfile *objfile)
15829 {
15830 /* The full name of this source file. */
15831 char *full_name = file_full_name (file, lh, comp_dir);
15832
15833 /* We don't create a macro table for this compilation unit
15834 at all until we actually get a filename. */
15835 if (! pending_macros)
15836 pending_macros = new_macro_table (&objfile->objfile_obstack,
15837 objfile->macro_cache);
15838
15839 if (! current_file)
15840 {
15841 /* If we have no current file, then this must be the start_file
15842 directive for the compilation unit's main source file. */
15843 current_file = macro_set_main (pending_macros, full_name);
15844 macro_define_special (pending_macros);
15845 }
15846 else
15847 current_file = macro_include (current_file, line, full_name);
15848
15849 xfree (full_name);
15850
15851 return current_file;
15852 }
15853
15854
15855 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
15856 followed by a null byte. */
15857 static char *
15858 copy_string (const char *buf, int len)
15859 {
15860 char *s = xmalloc (len + 1);
15861
15862 memcpy (s, buf, len);
15863 s[len] = '\0';
15864 return s;
15865 }
15866
15867
15868 static const char *
15869 consume_improper_spaces (const char *p, const char *body)
15870 {
15871 if (*p == ' ')
15872 {
15873 complaint (&symfile_complaints,
15874 _("macro definition contains spaces "
15875 "in formal argument list:\n`%s'"),
15876 body);
15877
15878 while (*p == ' ')
15879 p++;
15880 }
15881
15882 return p;
15883 }
15884
15885
15886 static void
15887 parse_macro_definition (struct macro_source_file *file, int line,
15888 const char *body)
15889 {
15890 const char *p;
15891
15892 /* The body string takes one of two forms. For object-like macro
15893 definitions, it should be:
15894
15895 <macro name> " " <definition>
15896
15897 For function-like macro definitions, it should be:
15898
15899 <macro name> "() " <definition>
15900 or
15901 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
15902
15903 Spaces may appear only where explicitly indicated, and in the
15904 <definition>.
15905
15906 The Dwarf 2 spec says that an object-like macro's name is always
15907 followed by a space, but versions of GCC around March 2002 omit
15908 the space when the macro's definition is the empty string.
15909
15910 The Dwarf 2 spec says that there should be no spaces between the
15911 formal arguments in a function-like macro's formal argument list,
15912 but versions of GCC around March 2002 include spaces after the
15913 commas. */
15914
15915
15916 /* Find the extent of the macro name. The macro name is terminated
15917 by either a space or null character (for an object-like macro) or
15918 an opening paren (for a function-like macro). */
15919 for (p = body; *p; p++)
15920 if (*p == ' ' || *p == '(')
15921 break;
15922
15923 if (*p == ' ' || *p == '\0')
15924 {
15925 /* It's an object-like macro. */
15926 int name_len = p - body;
15927 char *name = copy_string (body, name_len);
15928 const char *replacement;
15929
15930 if (*p == ' ')
15931 replacement = body + name_len + 1;
15932 else
15933 {
15934 dwarf2_macro_malformed_definition_complaint (body);
15935 replacement = body + name_len;
15936 }
15937
15938 macro_define_object (file, line, name, replacement);
15939
15940 xfree (name);
15941 }
15942 else if (*p == '(')
15943 {
15944 /* It's a function-like macro. */
15945 char *name = copy_string (body, p - body);
15946 int argc = 0;
15947 int argv_size = 1;
15948 char **argv = xmalloc (argv_size * sizeof (*argv));
15949
15950 p++;
15951
15952 p = consume_improper_spaces (p, body);
15953
15954 /* Parse the formal argument list. */
15955 while (*p && *p != ')')
15956 {
15957 /* Find the extent of the current argument name. */
15958 const char *arg_start = p;
15959
15960 while (*p && *p != ',' && *p != ')' && *p != ' ')
15961 p++;
15962
15963 if (! *p || p == arg_start)
15964 dwarf2_macro_malformed_definition_complaint (body);
15965 else
15966 {
15967 /* Make sure argv has room for the new argument. */
15968 if (argc >= argv_size)
15969 {
15970 argv_size *= 2;
15971 argv = xrealloc (argv, argv_size * sizeof (*argv));
15972 }
15973
15974 argv[argc++] = copy_string (arg_start, p - arg_start);
15975 }
15976
15977 p = consume_improper_spaces (p, body);
15978
15979 /* Consume the comma, if present. */
15980 if (*p == ',')
15981 {
15982 p++;
15983
15984 p = consume_improper_spaces (p, body);
15985 }
15986 }
15987
15988 if (*p == ')')
15989 {
15990 p++;
15991
15992 if (*p == ' ')
15993 /* Perfectly formed definition, no complaints. */
15994 macro_define_function (file, line, name,
15995 argc, (const char **) argv,
15996 p + 1);
15997 else if (*p == '\0')
15998 {
15999 /* Complain, but do define it. */
16000 dwarf2_macro_malformed_definition_complaint (body);
16001 macro_define_function (file, line, name,
16002 argc, (const char **) argv,
16003 p);
16004 }
16005 else
16006 /* Just complain. */
16007 dwarf2_macro_malformed_definition_complaint (body);
16008 }
16009 else
16010 /* Just complain. */
16011 dwarf2_macro_malformed_definition_complaint (body);
16012
16013 xfree (name);
16014 {
16015 int i;
16016
16017 for (i = 0; i < argc; i++)
16018 xfree (argv[i]);
16019 }
16020 xfree (argv);
16021 }
16022 else
16023 dwarf2_macro_malformed_definition_complaint (body);
16024 }
16025
16026 /* Skip some bytes from BYTES according to the form given in FORM.
16027 Returns the new pointer. */
16028
16029 static gdb_byte *
16030 skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
16031 enum dwarf_form form,
16032 unsigned int offset_size,
16033 struct dwarf2_section_info *section)
16034 {
16035 unsigned int bytes_read;
16036
16037 switch (form)
16038 {
16039 case DW_FORM_data1:
16040 case DW_FORM_flag:
16041 ++bytes;
16042 break;
16043
16044 case DW_FORM_data2:
16045 bytes += 2;
16046 break;
16047
16048 case DW_FORM_data4:
16049 bytes += 4;
16050 break;
16051
16052 case DW_FORM_data8:
16053 bytes += 8;
16054 break;
16055
16056 case DW_FORM_string:
16057 read_direct_string (abfd, bytes, &bytes_read);
16058 bytes += bytes_read;
16059 break;
16060
16061 case DW_FORM_sec_offset:
16062 case DW_FORM_strp:
16063 bytes += offset_size;
16064 break;
16065
16066 case DW_FORM_block:
16067 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
16068 bytes += bytes_read;
16069 break;
16070
16071 case DW_FORM_block1:
16072 bytes += 1 + read_1_byte (abfd, bytes);
16073 break;
16074 case DW_FORM_block2:
16075 bytes += 2 + read_2_bytes (abfd, bytes);
16076 break;
16077 case DW_FORM_block4:
16078 bytes += 4 + read_4_bytes (abfd, bytes);
16079 break;
16080
16081 case DW_FORM_sdata:
16082 case DW_FORM_udata:
16083 case DW_FORM_GNU_addr_index:
16084 case DW_FORM_GNU_str_index:
16085 bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
16086 if (bytes == NULL)
16087 {
16088 dwarf2_section_buffer_overflow_complaint (section);
16089 return NULL;
16090 }
16091 break;
16092
16093 default:
16094 {
16095 complain:
16096 complaint (&symfile_complaints,
16097 _("invalid form 0x%x in `%s'"),
16098 form,
16099 section->asection->name);
16100 return NULL;
16101 }
16102 }
16103
16104 return bytes;
16105 }
16106
16107 /* A helper for dwarf_decode_macros that handles skipping an unknown
16108 opcode. Returns an updated pointer to the macro data buffer; or,
16109 on error, issues a complaint and returns NULL. */
16110
16111 static gdb_byte *
16112 skip_unknown_opcode (unsigned int opcode,
16113 gdb_byte **opcode_definitions,
16114 gdb_byte *mac_ptr, gdb_byte *mac_end,
16115 bfd *abfd,
16116 unsigned int offset_size,
16117 struct dwarf2_section_info *section)
16118 {
16119 unsigned int bytes_read, i;
16120 unsigned long arg;
16121 gdb_byte *defn;
16122
16123 if (opcode_definitions[opcode] == NULL)
16124 {
16125 complaint (&symfile_complaints,
16126 _("unrecognized DW_MACFINO opcode 0x%x"),
16127 opcode);
16128 return NULL;
16129 }
16130
16131 defn = opcode_definitions[opcode];
16132 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
16133 defn += bytes_read;
16134
16135 for (i = 0; i < arg; ++i)
16136 {
16137 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
16138 section);
16139 if (mac_ptr == NULL)
16140 {
16141 /* skip_form_bytes already issued the complaint. */
16142 return NULL;
16143 }
16144 }
16145
16146 return mac_ptr;
16147 }
16148
16149 /* A helper function which parses the header of a macro section.
16150 If the macro section is the extended (for now called "GNU") type,
16151 then this updates *OFFSET_SIZE. Returns a pointer to just after
16152 the header, or issues a complaint and returns NULL on error. */
16153
16154 static gdb_byte *
16155 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
16156 bfd *abfd,
16157 gdb_byte *mac_ptr,
16158 unsigned int *offset_size,
16159 int section_is_gnu)
16160 {
16161 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
16162
16163 if (section_is_gnu)
16164 {
16165 unsigned int version, flags;
16166
16167 version = read_2_bytes (abfd, mac_ptr);
16168 if (version != 4)
16169 {
16170 complaint (&symfile_complaints,
16171 _("unrecognized version `%d' in .debug_macro section"),
16172 version);
16173 return NULL;
16174 }
16175 mac_ptr += 2;
16176
16177 flags = read_1_byte (abfd, mac_ptr);
16178 ++mac_ptr;
16179 *offset_size = (flags & 1) ? 8 : 4;
16180
16181 if ((flags & 2) != 0)
16182 /* We don't need the line table offset. */
16183 mac_ptr += *offset_size;
16184
16185 /* Vendor opcode descriptions. */
16186 if ((flags & 4) != 0)
16187 {
16188 unsigned int i, count;
16189
16190 count = read_1_byte (abfd, mac_ptr);
16191 ++mac_ptr;
16192 for (i = 0; i < count; ++i)
16193 {
16194 unsigned int opcode, bytes_read;
16195 unsigned long arg;
16196
16197 opcode = read_1_byte (abfd, mac_ptr);
16198 ++mac_ptr;
16199 opcode_definitions[opcode] = mac_ptr;
16200 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16201 mac_ptr += bytes_read;
16202 mac_ptr += arg;
16203 }
16204 }
16205 }
16206
16207 return mac_ptr;
16208 }
16209
16210 /* A helper for dwarf_decode_macros that handles the GNU extensions,
16211 including DW_MACRO_GNU_transparent_include. */
16212
16213 static void
16214 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
16215 struct macro_source_file *current_file,
16216 struct line_header *lh, char *comp_dir,
16217 struct dwarf2_section_info *section,
16218 int section_is_gnu,
16219 unsigned int offset_size,
16220 struct objfile *objfile,
16221 htab_t include_hash)
16222 {
16223 enum dwarf_macro_record_type macinfo_type;
16224 int at_commandline;
16225 gdb_byte *opcode_definitions[256];
16226
16227 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
16228 &offset_size, section_is_gnu);
16229 if (mac_ptr == NULL)
16230 {
16231 /* We already issued a complaint. */
16232 return;
16233 }
16234
16235 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
16236 GDB is still reading the definitions from command line. First
16237 DW_MACINFO_start_file will need to be ignored as it was already executed
16238 to create CURRENT_FILE for the main source holding also the command line
16239 definitions. On first met DW_MACINFO_start_file this flag is reset to
16240 normally execute all the remaining DW_MACINFO_start_file macinfos. */
16241
16242 at_commandline = 1;
16243
16244 do
16245 {
16246 /* Do we at least have room for a macinfo type byte? */
16247 if (mac_ptr >= mac_end)
16248 {
16249 dwarf2_section_buffer_overflow_complaint (section);
16250 break;
16251 }
16252
16253 macinfo_type = read_1_byte (abfd, mac_ptr);
16254 mac_ptr++;
16255
16256 /* Note that we rely on the fact that the corresponding GNU and
16257 DWARF constants are the same. */
16258 switch (macinfo_type)
16259 {
16260 /* A zero macinfo type indicates the end of the macro
16261 information. */
16262 case 0:
16263 break;
16264
16265 case DW_MACRO_GNU_define:
16266 case DW_MACRO_GNU_undef:
16267 case DW_MACRO_GNU_define_indirect:
16268 case DW_MACRO_GNU_undef_indirect:
16269 {
16270 unsigned int bytes_read;
16271 int line;
16272 char *body;
16273 int is_define;
16274
16275 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16276 mac_ptr += bytes_read;
16277
16278 if (macinfo_type == DW_MACRO_GNU_define
16279 || macinfo_type == DW_MACRO_GNU_undef)
16280 {
16281 body = read_direct_string (abfd, mac_ptr, &bytes_read);
16282 mac_ptr += bytes_read;
16283 }
16284 else
16285 {
16286 LONGEST str_offset;
16287
16288 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
16289 mac_ptr += offset_size;
16290
16291 body = read_indirect_string_at_offset (abfd, str_offset);
16292 }
16293
16294 is_define = (macinfo_type == DW_MACRO_GNU_define
16295 || macinfo_type == DW_MACRO_GNU_define_indirect);
16296 if (! current_file)
16297 {
16298 /* DWARF violation as no main source is present. */
16299 complaint (&symfile_complaints,
16300 _("debug info with no main source gives macro %s "
16301 "on line %d: %s"),
16302 is_define ? _("definition") : _("undefinition"),
16303 line, body);
16304 break;
16305 }
16306 if ((line == 0 && !at_commandline)
16307 || (line != 0 && at_commandline))
16308 complaint (&symfile_complaints,
16309 _("debug info gives %s macro %s with %s line %d: %s"),
16310 at_commandline ? _("command-line") : _("in-file"),
16311 is_define ? _("definition") : _("undefinition"),
16312 line == 0 ? _("zero") : _("non-zero"), line, body);
16313
16314 if (is_define)
16315 parse_macro_definition (current_file, line, body);
16316 else
16317 {
16318 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
16319 || macinfo_type == DW_MACRO_GNU_undef_indirect);
16320 macro_undef (current_file, line, body);
16321 }
16322 }
16323 break;
16324
16325 case DW_MACRO_GNU_start_file:
16326 {
16327 unsigned int bytes_read;
16328 int line, file;
16329
16330 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16331 mac_ptr += bytes_read;
16332 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16333 mac_ptr += bytes_read;
16334
16335 if ((line == 0 && !at_commandline)
16336 || (line != 0 && at_commandline))
16337 complaint (&symfile_complaints,
16338 _("debug info gives source %d included "
16339 "from %s at %s line %d"),
16340 file, at_commandline ? _("command-line") : _("file"),
16341 line == 0 ? _("zero") : _("non-zero"), line);
16342
16343 if (at_commandline)
16344 {
16345 /* This DW_MACRO_GNU_start_file was executed in the
16346 pass one. */
16347 at_commandline = 0;
16348 }
16349 else
16350 current_file = macro_start_file (file, line,
16351 current_file, comp_dir,
16352 lh, objfile);
16353 }
16354 break;
16355
16356 case DW_MACRO_GNU_end_file:
16357 if (! current_file)
16358 complaint (&symfile_complaints,
16359 _("macro debug info has an unmatched "
16360 "`close_file' directive"));
16361 else
16362 {
16363 current_file = current_file->included_by;
16364 if (! current_file)
16365 {
16366 enum dwarf_macro_record_type next_type;
16367
16368 /* GCC circa March 2002 doesn't produce the zero
16369 type byte marking the end of the compilation
16370 unit. Complain if it's not there, but exit no
16371 matter what. */
16372
16373 /* Do we at least have room for a macinfo type byte? */
16374 if (mac_ptr >= mac_end)
16375 {
16376 dwarf2_section_buffer_overflow_complaint (section);
16377 return;
16378 }
16379
16380 /* We don't increment mac_ptr here, so this is just
16381 a look-ahead. */
16382 next_type = read_1_byte (abfd, mac_ptr);
16383 if (next_type != 0)
16384 complaint (&symfile_complaints,
16385 _("no terminating 0-type entry for "
16386 "macros in `.debug_macinfo' section"));
16387
16388 return;
16389 }
16390 }
16391 break;
16392
16393 case DW_MACRO_GNU_transparent_include:
16394 {
16395 LONGEST offset;
16396 void **slot;
16397
16398 offset = read_offset_1 (abfd, mac_ptr, offset_size);
16399 mac_ptr += offset_size;
16400
16401 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
16402 if (*slot != NULL)
16403 {
16404 /* This has actually happened; see
16405 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
16406 complaint (&symfile_complaints,
16407 _("recursive DW_MACRO_GNU_transparent_include in "
16408 ".debug_macro section"));
16409 }
16410 else
16411 {
16412 *slot = mac_ptr;
16413
16414 dwarf_decode_macro_bytes (abfd,
16415 section->buffer + offset,
16416 mac_end, current_file,
16417 lh, comp_dir,
16418 section, section_is_gnu,
16419 offset_size, objfile, include_hash);
16420
16421 htab_remove_elt (include_hash, mac_ptr);
16422 }
16423 }
16424 break;
16425
16426 case DW_MACINFO_vendor_ext:
16427 if (!section_is_gnu)
16428 {
16429 unsigned int bytes_read;
16430 int constant;
16431
16432 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16433 mac_ptr += bytes_read;
16434 read_direct_string (abfd, mac_ptr, &bytes_read);
16435 mac_ptr += bytes_read;
16436
16437 /* We don't recognize any vendor extensions. */
16438 break;
16439 }
16440 /* FALLTHROUGH */
16441
16442 default:
16443 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
16444 mac_ptr, mac_end, abfd, offset_size,
16445 section);
16446 if (mac_ptr == NULL)
16447 return;
16448 break;
16449 }
16450 } while (macinfo_type != 0);
16451 }
16452
16453 static void
16454 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
16455 char *comp_dir, int section_is_gnu)
16456 {
16457 struct objfile *objfile = dwarf2_per_objfile->objfile;
16458 struct line_header *lh = cu->line_header;
16459 bfd *abfd;
16460 gdb_byte *mac_ptr, *mac_end;
16461 struct macro_source_file *current_file = 0;
16462 enum dwarf_macro_record_type macinfo_type;
16463 unsigned int offset_size = cu->header.offset_size;
16464 gdb_byte *opcode_definitions[256];
16465 struct cleanup *cleanup;
16466 htab_t include_hash;
16467 void **slot;
16468 struct dwarf2_section_info *section;
16469 const char *section_name;
16470
16471 if (cu->dwo_unit != NULL)
16472 {
16473 if (section_is_gnu)
16474 {
16475 section = &cu->dwo_unit->dwo_file->sections.macro;
16476 section_name = ".debug_macro.dwo";
16477 }
16478 else
16479 {
16480 section = &cu->dwo_unit->dwo_file->sections.macinfo;
16481 section_name = ".debug_macinfo.dwo";
16482 }
16483 }
16484 else
16485 {
16486 if (section_is_gnu)
16487 {
16488 section = &dwarf2_per_objfile->macro;
16489 section_name = ".debug_macro";
16490 }
16491 else
16492 {
16493 section = &dwarf2_per_objfile->macinfo;
16494 section_name = ".debug_macinfo";
16495 }
16496 }
16497
16498 dwarf2_read_section (objfile, section);
16499 if (section->buffer == NULL)
16500 {
16501 complaint (&symfile_complaints, _("missing %s section"), section_name);
16502 return;
16503 }
16504 abfd = section->asection->owner;
16505
16506 /* First pass: Find the name of the base filename.
16507 This filename is needed in order to process all macros whose definition
16508 (or undefinition) comes from the command line. These macros are defined
16509 before the first DW_MACINFO_start_file entry, and yet still need to be
16510 associated to the base file.
16511
16512 To determine the base file name, we scan the macro definitions until we
16513 reach the first DW_MACINFO_start_file entry. We then initialize
16514 CURRENT_FILE accordingly so that any macro definition found before the
16515 first DW_MACINFO_start_file can still be associated to the base file. */
16516
16517 mac_ptr = section->buffer + offset;
16518 mac_end = section->buffer + section->size;
16519
16520 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
16521 &offset_size, section_is_gnu);
16522 if (mac_ptr == NULL)
16523 {
16524 /* We already issued a complaint. */
16525 return;
16526 }
16527
16528 do
16529 {
16530 /* Do we at least have room for a macinfo type byte? */
16531 if (mac_ptr >= mac_end)
16532 {
16533 /* Complaint is printed during the second pass as GDB will probably
16534 stop the first pass earlier upon finding
16535 DW_MACINFO_start_file. */
16536 break;
16537 }
16538
16539 macinfo_type = read_1_byte (abfd, mac_ptr);
16540 mac_ptr++;
16541
16542 /* Note that we rely on the fact that the corresponding GNU and
16543 DWARF constants are the same. */
16544 switch (macinfo_type)
16545 {
16546 /* A zero macinfo type indicates the end of the macro
16547 information. */
16548 case 0:
16549 break;
16550
16551 case DW_MACRO_GNU_define:
16552 case DW_MACRO_GNU_undef:
16553 /* Only skip the data by MAC_PTR. */
16554 {
16555 unsigned int bytes_read;
16556
16557 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16558 mac_ptr += bytes_read;
16559 read_direct_string (abfd, mac_ptr, &bytes_read);
16560 mac_ptr += bytes_read;
16561 }
16562 break;
16563
16564 case DW_MACRO_GNU_start_file:
16565 {
16566 unsigned int bytes_read;
16567 int line, file;
16568
16569 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16570 mac_ptr += bytes_read;
16571 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16572 mac_ptr += bytes_read;
16573
16574 current_file = macro_start_file (file, line, current_file,
16575 comp_dir, lh, objfile);
16576 }
16577 break;
16578
16579 case DW_MACRO_GNU_end_file:
16580 /* No data to skip by MAC_PTR. */
16581 break;
16582
16583 case DW_MACRO_GNU_define_indirect:
16584 case DW_MACRO_GNU_undef_indirect:
16585 {
16586 unsigned int bytes_read;
16587
16588 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16589 mac_ptr += bytes_read;
16590 mac_ptr += offset_size;
16591 }
16592 break;
16593
16594 case DW_MACRO_GNU_transparent_include:
16595 /* Note that, according to the spec, a transparent include
16596 chain cannot call DW_MACRO_GNU_start_file. So, we can just
16597 skip this opcode. */
16598 mac_ptr += offset_size;
16599 break;
16600
16601 case DW_MACINFO_vendor_ext:
16602 /* Only skip the data by MAC_PTR. */
16603 if (!section_is_gnu)
16604 {
16605 unsigned int bytes_read;
16606
16607 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16608 mac_ptr += bytes_read;
16609 read_direct_string (abfd, mac_ptr, &bytes_read);
16610 mac_ptr += bytes_read;
16611 }
16612 /* FALLTHROUGH */
16613
16614 default:
16615 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
16616 mac_ptr, mac_end, abfd, offset_size,
16617 section);
16618 if (mac_ptr == NULL)
16619 return;
16620 break;
16621 }
16622 } while (macinfo_type != 0 && current_file == NULL);
16623
16624 /* Second pass: Process all entries.
16625
16626 Use the AT_COMMAND_LINE flag to determine whether we are still processing
16627 command-line macro definitions/undefinitions. This flag is unset when we
16628 reach the first DW_MACINFO_start_file entry. */
16629
16630 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
16631 NULL, xcalloc, xfree);
16632 cleanup = make_cleanup_htab_delete (include_hash);
16633 mac_ptr = section->buffer + offset;
16634 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
16635 *slot = mac_ptr;
16636 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
16637 current_file, lh, comp_dir, section, section_is_gnu,
16638 offset_size, objfile, include_hash);
16639 do_cleanups (cleanup);
16640 }
16641
16642 /* Check if the attribute's form is a DW_FORM_block*
16643 if so return true else false. */
16644
16645 static int
16646 attr_form_is_block (struct attribute *attr)
16647 {
16648 return (attr == NULL ? 0 :
16649 attr->form == DW_FORM_block1
16650 || attr->form == DW_FORM_block2
16651 || attr->form == DW_FORM_block4
16652 || attr->form == DW_FORM_block
16653 || attr->form == DW_FORM_exprloc);
16654 }
16655
16656 /* Return non-zero if ATTR's value is a section offset --- classes
16657 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
16658 You may use DW_UNSND (attr) to retrieve such offsets.
16659
16660 Section 7.5.4, "Attribute Encodings", explains that no attribute
16661 may have a value that belongs to more than one of these classes; it
16662 would be ambiguous if we did, because we use the same forms for all
16663 of them. */
16664
16665 static int
16666 attr_form_is_section_offset (struct attribute *attr)
16667 {
16668 return (attr->form == DW_FORM_data4
16669 || attr->form == DW_FORM_data8
16670 || attr->form == DW_FORM_sec_offset);
16671 }
16672
16673 /* Return non-zero if ATTR's value falls in the 'constant' class, or
16674 zero otherwise. When this function returns true, you can apply
16675 dwarf2_get_attr_constant_value to it.
16676
16677 However, note that for some attributes you must check
16678 attr_form_is_section_offset before using this test. DW_FORM_data4
16679 and DW_FORM_data8 are members of both the constant class, and of
16680 the classes that contain offsets into other debug sections
16681 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
16682 that, if an attribute's can be either a constant or one of the
16683 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
16684 taken as section offsets, not constants. */
16685
16686 static int
16687 attr_form_is_constant (struct attribute *attr)
16688 {
16689 switch (attr->form)
16690 {
16691 case DW_FORM_sdata:
16692 case DW_FORM_udata:
16693 case DW_FORM_data1:
16694 case DW_FORM_data2:
16695 case DW_FORM_data4:
16696 case DW_FORM_data8:
16697 return 1;
16698 default:
16699 return 0;
16700 }
16701 }
16702
16703 /* Return the .debug_loc section to use for CU.
16704 For DWO files use .debug_loc.dwo. */
16705
16706 static struct dwarf2_section_info *
16707 cu_debug_loc_section (struct dwarf2_cu *cu)
16708 {
16709 if (cu->dwo_unit)
16710 return &cu->dwo_unit->dwo_file->sections.loc;
16711 return &dwarf2_per_objfile->loc;
16712 }
16713
16714 /* A helper function that fills in a dwarf2_loclist_baton. */
16715
16716 static void
16717 fill_in_loclist_baton (struct dwarf2_cu *cu,
16718 struct dwarf2_loclist_baton *baton,
16719 struct attribute *attr)
16720 {
16721 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
16722
16723 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
16724
16725 baton->per_cu = cu->per_cu;
16726 gdb_assert (baton->per_cu);
16727 /* We don't know how long the location list is, but make sure we
16728 don't run off the edge of the section. */
16729 baton->size = section->size - DW_UNSND (attr);
16730 baton->data = section->buffer + DW_UNSND (attr);
16731 baton->base_address = cu->base_address;
16732 baton->from_dwo = cu->dwo_unit != NULL;
16733 }
16734
16735 static void
16736 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
16737 struct dwarf2_cu *cu)
16738 {
16739 struct objfile *objfile = dwarf2_per_objfile->objfile;
16740 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
16741
16742 if (attr_form_is_section_offset (attr)
16743 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
16744 the section. If so, fall through to the complaint in the
16745 other branch. */
16746 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
16747 {
16748 struct dwarf2_loclist_baton *baton;
16749
16750 baton = obstack_alloc (&objfile->objfile_obstack,
16751 sizeof (struct dwarf2_loclist_baton));
16752
16753 fill_in_loclist_baton (cu, baton, attr);
16754
16755 if (cu->base_known == 0)
16756 complaint (&symfile_complaints,
16757 _("Location list used without "
16758 "specifying the CU base address."));
16759
16760 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
16761 SYMBOL_LOCATION_BATON (sym) = baton;
16762 }
16763 else
16764 {
16765 struct dwarf2_locexpr_baton *baton;
16766
16767 baton = obstack_alloc (&objfile->objfile_obstack,
16768 sizeof (struct dwarf2_locexpr_baton));
16769 baton->per_cu = cu->per_cu;
16770 gdb_assert (baton->per_cu);
16771
16772 if (attr_form_is_block (attr))
16773 {
16774 /* Note that we're just copying the block's data pointer
16775 here, not the actual data. We're still pointing into the
16776 info_buffer for SYM's objfile; right now we never release
16777 that buffer, but when we do clean up properly this may
16778 need to change. */
16779 baton->size = DW_BLOCK (attr)->size;
16780 baton->data = DW_BLOCK (attr)->data;
16781 }
16782 else
16783 {
16784 dwarf2_invalid_attrib_class_complaint ("location description",
16785 SYMBOL_NATURAL_NAME (sym));
16786 baton->size = 0;
16787 }
16788
16789 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
16790 SYMBOL_LOCATION_BATON (sym) = baton;
16791 }
16792 }
16793
16794 /* Return the OBJFILE associated with the compilation unit CU. If CU
16795 came from a separate debuginfo file, then the master objfile is
16796 returned. */
16797
16798 struct objfile *
16799 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
16800 {
16801 struct objfile *objfile = per_cu->objfile;
16802
16803 /* Return the master objfile, so that we can report and look up the
16804 correct file containing this variable. */
16805 if (objfile->separate_debug_objfile_backlink)
16806 objfile = objfile->separate_debug_objfile_backlink;
16807
16808 return objfile;
16809 }
16810
16811 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
16812 (CU_HEADERP is unused in such case) or prepare a temporary copy at
16813 CU_HEADERP first. */
16814
16815 static const struct comp_unit_head *
16816 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
16817 struct dwarf2_per_cu_data *per_cu)
16818 {
16819 gdb_byte *info_ptr;
16820
16821 if (per_cu->cu)
16822 return &per_cu->cu->header;
16823
16824 info_ptr = per_cu->info_or_types_section->buffer + per_cu->offset.sect_off;
16825
16826 memset (cu_headerp, 0, sizeof (*cu_headerp));
16827 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
16828
16829 return cu_headerp;
16830 }
16831
16832 /* Return the address size given in the compilation unit header for CU. */
16833
16834 int
16835 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
16836 {
16837 struct comp_unit_head cu_header_local;
16838 const struct comp_unit_head *cu_headerp;
16839
16840 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16841
16842 return cu_headerp->addr_size;
16843 }
16844
16845 /* Return the offset size given in the compilation unit header for CU. */
16846
16847 int
16848 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
16849 {
16850 struct comp_unit_head cu_header_local;
16851 const struct comp_unit_head *cu_headerp;
16852
16853 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16854
16855 return cu_headerp->offset_size;
16856 }
16857
16858 /* See its dwarf2loc.h declaration. */
16859
16860 int
16861 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
16862 {
16863 struct comp_unit_head cu_header_local;
16864 const struct comp_unit_head *cu_headerp;
16865
16866 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16867
16868 if (cu_headerp->version == 2)
16869 return cu_headerp->addr_size;
16870 else
16871 return cu_headerp->offset_size;
16872 }
16873
16874 /* Return the text offset of the CU. The returned offset comes from
16875 this CU's objfile. If this objfile came from a separate debuginfo
16876 file, then the offset may be different from the corresponding
16877 offset in the parent objfile. */
16878
16879 CORE_ADDR
16880 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
16881 {
16882 struct objfile *objfile = per_cu->objfile;
16883
16884 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16885 }
16886
16887 /* Locate the .debug_info compilation unit from CU's objfile which contains
16888 the DIE at OFFSET. Raises an error on failure. */
16889
16890 static struct dwarf2_per_cu_data *
16891 dwarf2_find_containing_comp_unit (sect_offset offset,
16892 struct objfile *objfile)
16893 {
16894 struct dwarf2_per_cu_data *this_cu;
16895 int low, high;
16896
16897 low = 0;
16898 high = dwarf2_per_objfile->n_comp_units - 1;
16899 while (high > low)
16900 {
16901 int mid = low + (high - low) / 2;
16902
16903 if (dwarf2_per_objfile->all_comp_units[mid]->offset.sect_off
16904 >= offset.sect_off)
16905 high = mid;
16906 else
16907 low = mid + 1;
16908 }
16909 gdb_assert (low == high);
16910 if (dwarf2_per_objfile->all_comp_units[low]->offset.sect_off
16911 > offset.sect_off)
16912 {
16913 if (low == 0)
16914 error (_("Dwarf Error: could not find partial DIE containing "
16915 "offset 0x%lx [in module %s]"),
16916 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
16917
16918 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
16919 <= offset.sect_off);
16920 return dwarf2_per_objfile->all_comp_units[low-1];
16921 }
16922 else
16923 {
16924 this_cu = dwarf2_per_objfile->all_comp_units[low];
16925 if (low == dwarf2_per_objfile->n_comp_units - 1
16926 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
16927 error (_("invalid dwarf2 offset %u"), offset.sect_off);
16928 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
16929 return this_cu;
16930 }
16931 }
16932
16933 /* Initialize dwarf2_cu CU, owned by PER_CU. */
16934
16935 static void
16936 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
16937 {
16938 memset (cu, 0, sizeof (*cu));
16939 per_cu->cu = cu;
16940 cu->per_cu = per_cu;
16941 cu->objfile = per_cu->objfile;
16942 obstack_init (&cu->comp_unit_obstack);
16943 }
16944
16945 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
16946
16947 static void
16948 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
16949 enum language pretend_language)
16950 {
16951 struct attribute *attr;
16952
16953 /* Set the language we're debugging. */
16954 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
16955 if (attr)
16956 set_cu_language (DW_UNSND (attr), cu);
16957 else
16958 {
16959 cu->language = pretend_language;
16960 cu->language_defn = language_def (cu->language);
16961 }
16962
16963 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
16964 if (attr)
16965 cu->producer = DW_STRING (attr);
16966 }
16967
16968 /* Release one cached compilation unit, CU. We unlink it from the tree
16969 of compilation units, but we don't remove it from the read_in_chain;
16970 the caller is responsible for that.
16971 NOTE: DATA is a void * because this function is also used as a
16972 cleanup routine. */
16973
16974 static void
16975 free_heap_comp_unit (void *data)
16976 {
16977 struct dwarf2_cu *cu = data;
16978
16979 gdb_assert (cu->per_cu != NULL);
16980 cu->per_cu->cu = NULL;
16981 cu->per_cu = NULL;
16982
16983 obstack_free (&cu->comp_unit_obstack, NULL);
16984
16985 xfree (cu);
16986 }
16987
16988 /* This cleanup function is passed the address of a dwarf2_cu on the stack
16989 when we're finished with it. We can't free the pointer itself, but be
16990 sure to unlink it from the cache. Also release any associated storage. */
16991
16992 static void
16993 free_stack_comp_unit (void *data)
16994 {
16995 struct dwarf2_cu *cu = data;
16996
16997 gdb_assert (cu->per_cu != NULL);
16998 cu->per_cu->cu = NULL;
16999 cu->per_cu = NULL;
17000
17001 obstack_free (&cu->comp_unit_obstack, NULL);
17002 cu->partial_dies = NULL;
17003 }
17004
17005 /* Free all cached compilation units. */
17006
17007 static void
17008 free_cached_comp_units (void *data)
17009 {
17010 struct dwarf2_per_cu_data *per_cu, **last_chain;
17011
17012 per_cu = dwarf2_per_objfile->read_in_chain;
17013 last_chain = &dwarf2_per_objfile->read_in_chain;
17014 while (per_cu != NULL)
17015 {
17016 struct dwarf2_per_cu_data *next_cu;
17017
17018 next_cu = per_cu->cu->read_in_chain;
17019
17020 free_heap_comp_unit (per_cu->cu);
17021 *last_chain = next_cu;
17022
17023 per_cu = next_cu;
17024 }
17025 }
17026
17027 /* Increase the age counter on each cached compilation unit, and free
17028 any that are too old. */
17029
17030 static void
17031 age_cached_comp_units (void)
17032 {
17033 struct dwarf2_per_cu_data *per_cu, **last_chain;
17034
17035 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
17036 per_cu = dwarf2_per_objfile->read_in_chain;
17037 while (per_cu != NULL)
17038 {
17039 per_cu->cu->last_used ++;
17040 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
17041 dwarf2_mark (per_cu->cu);
17042 per_cu = per_cu->cu->read_in_chain;
17043 }
17044
17045 per_cu = dwarf2_per_objfile->read_in_chain;
17046 last_chain = &dwarf2_per_objfile->read_in_chain;
17047 while (per_cu != NULL)
17048 {
17049 struct dwarf2_per_cu_data *next_cu;
17050
17051 next_cu = per_cu->cu->read_in_chain;
17052
17053 if (!per_cu->cu->mark)
17054 {
17055 free_heap_comp_unit (per_cu->cu);
17056 *last_chain = next_cu;
17057 }
17058 else
17059 last_chain = &per_cu->cu->read_in_chain;
17060
17061 per_cu = next_cu;
17062 }
17063 }
17064
17065 /* Remove a single compilation unit from the cache. */
17066
17067 static void
17068 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
17069 {
17070 struct dwarf2_per_cu_data *per_cu, **last_chain;
17071
17072 per_cu = dwarf2_per_objfile->read_in_chain;
17073 last_chain = &dwarf2_per_objfile->read_in_chain;
17074 while (per_cu != NULL)
17075 {
17076 struct dwarf2_per_cu_data *next_cu;
17077
17078 next_cu = per_cu->cu->read_in_chain;
17079
17080 if (per_cu == target_per_cu)
17081 {
17082 free_heap_comp_unit (per_cu->cu);
17083 per_cu->cu = NULL;
17084 *last_chain = next_cu;
17085 break;
17086 }
17087 else
17088 last_chain = &per_cu->cu->read_in_chain;
17089
17090 per_cu = next_cu;
17091 }
17092 }
17093
17094 /* Release all extra memory associated with OBJFILE. */
17095
17096 void
17097 dwarf2_free_objfile (struct objfile *objfile)
17098 {
17099 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17100
17101 if (dwarf2_per_objfile == NULL)
17102 return;
17103
17104 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
17105 free_cached_comp_units (NULL);
17106
17107 if (dwarf2_per_objfile->quick_file_names_table)
17108 htab_delete (dwarf2_per_objfile->quick_file_names_table);
17109
17110 /* Everything else should be on the objfile obstack. */
17111 }
17112
17113 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
17114 We store these in a hash table separate from the DIEs, and preserve them
17115 when the DIEs are flushed out of cache.
17116
17117 The CU "per_cu" pointer is needed because offset alone is not enough to
17118 uniquely identify the type. A file may have multiple .debug_types sections,
17119 or the type may come from a DWO file. We have to use something in
17120 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
17121 routine, get_die_type_at_offset, from outside this file, and thus won't
17122 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
17123 of the objfile. */
17124
17125 struct dwarf2_per_cu_offset_and_type
17126 {
17127 const struct dwarf2_per_cu_data *per_cu;
17128 sect_offset offset;
17129 struct type *type;
17130 };
17131
17132 /* Hash function for a dwarf2_per_cu_offset_and_type. */
17133
17134 static hashval_t
17135 per_cu_offset_and_type_hash (const void *item)
17136 {
17137 const struct dwarf2_per_cu_offset_and_type *ofs = item;
17138
17139 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
17140 }
17141
17142 /* Equality function for a dwarf2_per_cu_offset_and_type. */
17143
17144 static int
17145 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
17146 {
17147 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
17148 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
17149
17150 return (ofs_lhs->per_cu == ofs_rhs->per_cu
17151 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
17152 }
17153
17154 /* Set the type associated with DIE to TYPE. Save it in CU's hash
17155 table if necessary. For convenience, return TYPE.
17156
17157 The DIEs reading must have careful ordering to:
17158 * Not cause infite loops trying to read in DIEs as a prerequisite for
17159 reading current DIE.
17160 * Not trying to dereference contents of still incompletely read in types
17161 while reading in other DIEs.
17162 * Enable referencing still incompletely read in types just by a pointer to
17163 the type without accessing its fields.
17164
17165 Therefore caller should follow these rules:
17166 * Try to fetch any prerequisite types we may need to build this DIE type
17167 before building the type and calling set_die_type.
17168 * After building type call set_die_type for current DIE as soon as
17169 possible before fetching more types to complete the current type.
17170 * Make the type as complete as possible before fetching more types. */
17171
17172 static struct type *
17173 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
17174 {
17175 struct dwarf2_per_cu_offset_and_type **slot, ofs;
17176 struct objfile *objfile = cu->objfile;
17177
17178 /* For Ada types, make sure that the gnat-specific data is always
17179 initialized (if not already set). There are a few types where
17180 we should not be doing so, because the type-specific area is
17181 already used to hold some other piece of info (eg: TYPE_CODE_FLT
17182 where the type-specific area is used to store the floatformat).
17183 But this is not a problem, because the gnat-specific information
17184 is actually not needed for these types. */
17185 if (need_gnat_info (cu)
17186 && TYPE_CODE (type) != TYPE_CODE_FUNC
17187 && TYPE_CODE (type) != TYPE_CODE_FLT
17188 && !HAVE_GNAT_AUX_INFO (type))
17189 INIT_GNAT_SPECIFIC (type);
17190
17191 if (dwarf2_per_objfile->die_type_hash == NULL)
17192 {
17193 dwarf2_per_objfile->die_type_hash =
17194 htab_create_alloc_ex (127,
17195 per_cu_offset_and_type_hash,
17196 per_cu_offset_and_type_eq,
17197 NULL,
17198 &objfile->objfile_obstack,
17199 hashtab_obstack_allocate,
17200 dummy_obstack_deallocate);
17201 }
17202
17203 ofs.per_cu = cu->per_cu;
17204 ofs.offset = die->offset;
17205 ofs.type = type;
17206 slot = (struct dwarf2_per_cu_offset_and_type **)
17207 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
17208 if (*slot)
17209 complaint (&symfile_complaints,
17210 _("A problem internal to GDB: DIE 0x%x has type already set"),
17211 die->offset.sect_off);
17212 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
17213 **slot = ofs;
17214 return type;
17215 }
17216
17217 /* Look up the type for the die at OFFSET in the appropriate type_hash
17218 table, or return NULL if the die does not have a saved type. */
17219
17220 static struct type *
17221 get_die_type_at_offset (sect_offset offset,
17222 struct dwarf2_per_cu_data *per_cu)
17223 {
17224 struct dwarf2_per_cu_offset_and_type *slot, ofs;
17225
17226 if (dwarf2_per_objfile->die_type_hash == NULL)
17227 return NULL;
17228
17229 ofs.per_cu = per_cu;
17230 ofs.offset = offset;
17231 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
17232 if (slot)
17233 return slot->type;
17234 else
17235 return NULL;
17236 }
17237
17238 /* Look up the type for DIE in the appropriate type_hash table,
17239 or return NULL if DIE does not have a saved type. */
17240
17241 static struct type *
17242 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
17243 {
17244 return get_die_type_at_offset (die->offset, cu->per_cu);
17245 }
17246
17247 /* Add a dependence relationship from CU to REF_PER_CU. */
17248
17249 static void
17250 dwarf2_add_dependence (struct dwarf2_cu *cu,
17251 struct dwarf2_per_cu_data *ref_per_cu)
17252 {
17253 void **slot;
17254
17255 if (cu->dependencies == NULL)
17256 cu->dependencies
17257 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
17258 NULL, &cu->comp_unit_obstack,
17259 hashtab_obstack_allocate,
17260 dummy_obstack_deallocate);
17261
17262 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
17263 if (*slot == NULL)
17264 *slot = ref_per_cu;
17265 }
17266
17267 /* Subroutine of dwarf2_mark to pass to htab_traverse.
17268 Set the mark field in every compilation unit in the
17269 cache that we must keep because we are keeping CU. */
17270
17271 static int
17272 dwarf2_mark_helper (void **slot, void *data)
17273 {
17274 struct dwarf2_per_cu_data *per_cu;
17275
17276 per_cu = (struct dwarf2_per_cu_data *) *slot;
17277
17278 /* cu->dependencies references may not yet have been ever read if QUIT aborts
17279 reading of the chain. As such dependencies remain valid it is not much
17280 useful to track and undo them during QUIT cleanups. */
17281 if (per_cu->cu == NULL)
17282 return 1;
17283
17284 if (per_cu->cu->mark)
17285 return 1;
17286 per_cu->cu->mark = 1;
17287
17288 if (per_cu->cu->dependencies != NULL)
17289 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
17290
17291 return 1;
17292 }
17293
17294 /* Set the mark field in CU and in every other compilation unit in the
17295 cache that we must keep because we are keeping CU. */
17296
17297 static void
17298 dwarf2_mark (struct dwarf2_cu *cu)
17299 {
17300 if (cu->mark)
17301 return;
17302 cu->mark = 1;
17303 if (cu->dependencies != NULL)
17304 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
17305 }
17306
17307 static void
17308 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
17309 {
17310 while (per_cu)
17311 {
17312 per_cu->cu->mark = 0;
17313 per_cu = per_cu->cu->read_in_chain;
17314 }
17315 }
17316
17317 /* Trivial hash function for partial_die_info: the hash value of a DIE
17318 is its offset in .debug_info for this objfile. */
17319
17320 static hashval_t
17321 partial_die_hash (const void *item)
17322 {
17323 const struct partial_die_info *part_die = item;
17324
17325 return part_die->offset.sect_off;
17326 }
17327
17328 /* Trivial comparison function for partial_die_info structures: two DIEs
17329 are equal if they have the same offset. */
17330
17331 static int
17332 partial_die_eq (const void *item_lhs, const void *item_rhs)
17333 {
17334 const struct partial_die_info *part_die_lhs = item_lhs;
17335 const struct partial_die_info *part_die_rhs = item_rhs;
17336
17337 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
17338 }
17339
17340 static struct cmd_list_element *set_dwarf2_cmdlist;
17341 static struct cmd_list_element *show_dwarf2_cmdlist;
17342
17343 static void
17344 set_dwarf2_cmd (char *args, int from_tty)
17345 {
17346 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
17347 }
17348
17349 static void
17350 show_dwarf2_cmd (char *args, int from_tty)
17351 {
17352 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
17353 }
17354
17355 /* If section described by INFO was mmapped, munmap it now. */
17356
17357 static void
17358 munmap_section_buffer (struct dwarf2_section_info *info)
17359 {
17360 if (info->map_addr != NULL)
17361 {
17362 #ifdef HAVE_MMAP
17363 int res;
17364
17365 res = munmap (info->map_addr, info->map_len);
17366 gdb_assert (res == 0);
17367 #else
17368 /* Without HAVE_MMAP, we should never be here to begin with. */
17369 gdb_assert_not_reached ("no mmap support");
17370 #endif
17371 }
17372 }
17373
17374 /* munmap debug sections for OBJFILE, if necessary. */
17375
17376 static void
17377 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
17378 {
17379 struct dwarf2_per_objfile *data = d;
17380 int ix;
17381 struct dwarf2_section_info *section;
17382
17383 /* This is sorted according to the order they're defined in to make it easier
17384 to keep in sync. */
17385 munmap_section_buffer (&data->info);
17386 munmap_section_buffer (&data->abbrev);
17387 munmap_section_buffer (&data->line);
17388 munmap_section_buffer (&data->loc);
17389 munmap_section_buffer (&data->macinfo);
17390 munmap_section_buffer (&data->macro);
17391 munmap_section_buffer (&data->str);
17392 munmap_section_buffer (&data->ranges);
17393 munmap_section_buffer (&data->addr);
17394 munmap_section_buffer (&data->frame);
17395 munmap_section_buffer (&data->eh_frame);
17396 munmap_section_buffer (&data->gdb_index);
17397
17398 for (ix = 0;
17399 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
17400 ++ix)
17401 munmap_section_buffer (section);
17402
17403 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
17404 VEC_free (dwarf2_per_cu_ptr,
17405 dwarf2_per_objfile->all_comp_units[ix]->imported_symtabs);
17406
17407 VEC_free (dwarf2_section_info_def, data->types);
17408
17409 if (data->dwo_files)
17410 free_dwo_files (data->dwo_files, objfile);
17411 }
17412
17413 \f
17414 /* The "save gdb-index" command. */
17415
17416 /* The contents of the hash table we create when building the string
17417 table. */
17418 struct strtab_entry
17419 {
17420 offset_type offset;
17421 const char *str;
17422 };
17423
17424 /* Hash function for a strtab_entry.
17425
17426 Function is used only during write_hash_table so no index format backward
17427 compatibility is needed. */
17428
17429 static hashval_t
17430 hash_strtab_entry (const void *e)
17431 {
17432 const struct strtab_entry *entry = e;
17433 return mapped_index_string_hash (INT_MAX, entry->str);
17434 }
17435
17436 /* Equality function for a strtab_entry. */
17437
17438 static int
17439 eq_strtab_entry (const void *a, const void *b)
17440 {
17441 const struct strtab_entry *ea = a;
17442 const struct strtab_entry *eb = b;
17443 return !strcmp (ea->str, eb->str);
17444 }
17445
17446 /* Create a strtab_entry hash table. */
17447
17448 static htab_t
17449 create_strtab (void)
17450 {
17451 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
17452 xfree, xcalloc, xfree);
17453 }
17454
17455 /* Add a string to the constant pool. Return the string's offset in
17456 host order. */
17457
17458 static offset_type
17459 add_string (htab_t table, struct obstack *cpool, const char *str)
17460 {
17461 void **slot;
17462 struct strtab_entry entry;
17463 struct strtab_entry *result;
17464
17465 entry.str = str;
17466 slot = htab_find_slot (table, &entry, INSERT);
17467 if (*slot)
17468 result = *slot;
17469 else
17470 {
17471 result = XNEW (struct strtab_entry);
17472 result->offset = obstack_object_size (cpool);
17473 result->str = str;
17474 obstack_grow_str0 (cpool, str);
17475 *slot = result;
17476 }
17477 return result->offset;
17478 }
17479
17480 /* An entry in the symbol table. */
17481 struct symtab_index_entry
17482 {
17483 /* The name of the symbol. */
17484 const char *name;
17485 /* The offset of the name in the constant pool. */
17486 offset_type index_offset;
17487 /* A sorted vector of the indices of all the CUs that hold an object
17488 of this name. */
17489 VEC (offset_type) *cu_indices;
17490 };
17491
17492 /* The symbol table. This is a power-of-2-sized hash table. */
17493 struct mapped_symtab
17494 {
17495 offset_type n_elements;
17496 offset_type size;
17497 struct symtab_index_entry **data;
17498 };
17499
17500 /* Hash function for a symtab_index_entry. */
17501
17502 static hashval_t
17503 hash_symtab_entry (const void *e)
17504 {
17505 const struct symtab_index_entry *entry = e;
17506 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
17507 sizeof (offset_type) * VEC_length (offset_type,
17508 entry->cu_indices),
17509 0);
17510 }
17511
17512 /* Equality function for a symtab_index_entry. */
17513
17514 static int
17515 eq_symtab_entry (const void *a, const void *b)
17516 {
17517 const struct symtab_index_entry *ea = a;
17518 const struct symtab_index_entry *eb = b;
17519 int len = VEC_length (offset_type, ea->cu_indices);
17520 if (len != VEC_length (offset_type, eb->cu_indices))
17521 return 0;
17522 return !memcmp (VEC_address (offset_type, ea->cu_indices),
17523 VEC_address (offset_type, eb->cu_indices),
17524 sizeof (offset_type) * len);
17525 }
17526
17527 /* Destroy a symtab_index_entry. */
17528
17529 static void
17530 delete_symtab_entry (void *p)
17531 {
17532 struct symtab_index_entry *entry = p;
17533 VEC_free (offset_type, entry->cu_indices);
17534 xfree (entry);
17535 }
17536
17537 /* Create a hash table holding symtab_index_entry objects. */
17538
17539 static htab_t
17540 create_symbol_hash_table (void)
17541 {
17542 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
17543 delete_symtab_entry, xcalloc, xfree);
17544 }
17545
17546 /* Create a new mapped symtab object. */
17547
17548 static struct mapped_symtab *
17549 create_mapped_symtab (void)
17550 {
17551 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
17552 symtab->n_elements = 0;
17553 symtab->size = 1024;
17554 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
17555 return symtab;
17556 }
17557
17558 /* Destroy a mapped_symtab. */
17559
17560 static void
17561 cleanup_mapped_symtab (void *p)
17562 {
17563 struct mapped_symtab *symtab = p;
17564 /* The contents of the array are freed when the other hash table is
17565 destroyed. */
17566 xfree (symtab->data);
17567 xfree (symtab);
17568 }
17569
17570 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
17571 the slot.
17572
17573 Function is used only during write_hash_table so no index format backward
17574 compatibility is needed. */
17575
17576 static struct symtab_index_entry **
17577 find_slot (struct mapped_symtab *symtab, const char *name)
17578 {
17579 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
17580
17581 index = hash & (symtab->size - 1);
17582 step = ((hash * 17) & (symtab->size - 1)) | 1;
17583
17584 for (;;)
17585 {
17586 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
17587 return &symtab->data[index];
17588 index = (index + step) & (symtab->size - 1);
17589 }
17590 }
17591
17592 /* Expand SYMTAB's hash table. */
17593
17594 static void
17595 hash_expand (struct mapped_symtab *symtab)
17596 {
17597 offset_type old_size = symtab->size;
17598 offset_type i;
17599 struct symtab_index_entry **old_entries = symtab->data;
17600
17601 symtab->size *= 2;
17602 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
17603
17604 for (i = 0; i < old_size; ++i)
17605 {
17606 if (old_entries[i])
17607 {
17608 struct symtab_index_entry **slot = find_slot (symtab,
17609 old_entries[i]->name);
17610 *slot = old_entries[i];
17611 }
17612 }
17613
17614 xfree (old_entries);
17615 }
17616
17617 /* Add an entry to SYMTAB. NAME is the name of the symbol.
17618 CU_INDEX is the index of the CU in which the symbol appears.
17619 IS_STATIC is one if the symbol is static, otherwise zero (global). */
17620
17621 static void
17622 add_index_entry (struct mapped_symtab *symtab, const char *name,
17623 int is_static, gdb_index_symbol_kind kind,
17624 offset_type cu_index)
17625 {
17626 struct symtab_index_entry **slot;
17627 offset_type cu_index_and_attrs;
17628
17629 ++symtab->n_elements;
17630 if (4 * symtab->n_elements / 3 >= symtab->size)
17631 hash_expand (symtab);
17632
17633 slot = find_slot (symtab, name);
17634 if (!*slot)
17635 {
17636 *slot = XNEW (struct symtab_index_entry);
17637 (*slot)->name = name;
17638 /* index_offset is set later. */
17639 (*slot)->cu_indices = NULL;
17640 }
17641
17642 cu_index_and_attrs = 0;
17643 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
17644 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
17645 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
17646
17647 /* We don't want to record an index value twice as we want to avoid the
17648 duplication.
17649 We process all global symbols and then all static symbols
17650 (which would allow us to avoid the duplication by only having to check
17651 the last entry pushed), but a symbol could have multiple kinds in one CU.
17652 To keep things simple we don't worry about the duplication here and
17653 sort and uniqufy the list after we've processed all symbols. */
17654 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
17655 }
17656
17657 /* qsort helper routine for uniquify_cu_indices. */
17658
17659 static int
17660 offset_type_compare (const void *ap, const void *bp)
17661 {
17662 offset_type a = *(offset_type *) ap;
17663 offset_type b = *(offset_type *) bp;
17664
17665 return (a > b) - (b > a);
17666 }
17667
17668 /* Sort and remove duplicates of all symbols' cu_indices lists. */
17669
17670 static void
17671 uniquify_cu_indices (struct mapped_symtab *symtab)
17672 {
17673 int i;
17674
17675 for (i = 0; i < symtab->size; ++i)
17676 {
17677 struct symtab_index_entry *entry = symtab->data[i];
17678
17679 if (entry
17680 && entry->cu_indices != NULL)
17681 {
17682 unsigned int next_to_insert, next_to_check;
17683 offset_type last_value;
17684
17685 qsort (VEC_address (offset_type, entry->cu_indices),
17686 VEC_length (offset_type, entry->cu_indices),
17687 sizeof (offset_type), offset_type_compare);
17688
17689 last_value = VEC_index (offset_type, entry->cu_indices, 0);
17690 next_to_insert = 1;
17691 for (next_to_check = 1;
17692 next_to_check < VEC_length (offset_type, entry->cu_indices);
17693 ++next_to_check)
17694 {
17695 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
17696 != last_value)
17697 {
17698 last_value = VEC_index (offset_type, entry->cu_indices,
17699 next_to_check);
17700 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
17701 last_value);
17702 ++next_to_insert;
17703 }
17704 }
17705 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
17706 }
17707 }
17708 }
17709
17710 /* Add a vector of indices to the constant pool. */
17711
17712 static offset_type
17713 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
17714 struct symtab_index_entry *entry)
17715 {
17716 void **slot;
17717
17718 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
17719 if (!*slot)
17720 {
17721 offset_type len = VEC_length (offset_type, entry->cu_indices);
17722 offset_type val = MAYBE_SWAP (len);
17723 offset_type iter;
17724 int i;
17725
17726 *slot = entry;
17727 entry->index_offset = obstack_object_size (cpool);
17728
17729 obstack_grow (cpool, &val, sizeof (val));
17730 for (i = 0;
17731 VEC_iterate (offset_type, entry->cu_indices, i, iter);
17732 ++i)
17733 {
17734 val = MAYBE_SWAP (iter);
17735 obstack_grow (cpool, &val, sizeof (val));
17736 }
17737 }
17738 else
17739 {
17740 struct symtab_index_entry *old_entry = *slot;
17741 entry->index_offset = old_entry->index_offset;
17742 entry = old_entry;
17743 }
17744 return entry->index_offset;
17745 }
17746
17747 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
17748 constant pool entries going into the obstack CPOOL. */
17749
17750 static void
17751 write_hash_table (struct mapped_symtab *symtab,
17752 struct obstack *output, struct obstack *cpool)
17753 {
17754 offset_type i;
17755 htab_t symbol_hash_table;
17756 htab_t str_table;
17757
17758 symbol_hash_table = create_symbol_hash_table ();
17759 str_table = create_strtab ();
17760
17761 /* We add all the index vectors to the constant pool first, to
17762 ensure alignment is ok. */
17763 for (i = 0; i < symtab->size; ++i)
17764 {
17765 if (symtab->data[i])
17766 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
17767 }
17768
17769 /* Now write out the hash table. */
17770 for (i = 0; i < symtab->size; ++i)
17771 {
17772 offset_type str_off, vec_off;
17773
17774 if (symtab->data[i])
17775 {
17776 str_off = add_string (str_table, cpool, symtab->data[i]->name);
17777 vec_off = symtab->data[i]->index_offset;
17778 }
17779 else
17780 {
17781 /* While 0 is a valid constant pool index, it is not valid
17782 to have 0 for both offsets. */
17783 str_off = 0;
17784 vec_off = 0;
17785 }
17786
17787 str_off = MAYBE_SWAP (str_off);
17788 vec_off = MAYBE_SWAP (vec_off);
17789
17790 obstack_grow (output, &str_off, sizeof (str_off));
17791 obstack_grow (output, &vec_off, sizeof (vec_off));
17792 }
17793
17794 htab_delete (str_table);
17795 htab_delete (symbol_hash_table);
17796 }
17797
17798 /* Struct to map psymtab to CU index in the index file. */
17799 struct psymtab_cu_index_map
17800 {
17801 struct partial_symtab *psymtab;
17802 unsigned int cu_index;
17803 };
17804
17805 static hashval_t
17806 hash_psymtab_cu_index (const void *item)
17807 {
17808 const struct psymtab_cu_index_map *map = item;
17809
17810 return htab_hash_pointer (map->psymtab);
17811 }
17812
17813 static int
17814 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
17815 {
17816 const struct psymtab_cu_index_map *lhs = item_lhs;
17817 const struct psymtab_cu_index_map *rhs = item_rhs;
17818
17819 return lhs->psymtab == rhs->psymtab;
17820 }
17821
17822 /* Helper struct for building the address table. */
17823 struct addrmap_index_data
17824 {
17825 struct objfile *objfile;
17826 struct obstack *addr_obstack;
17827 htab_t cu_index_htab;
17828
17829 /* Non-zero if the previous_* fields are valid.
17830 We can't write an entry until we see the next entry (since it is only then
17831 that we know the end of the entry). */
17832 int previous_valid;
17833 /* Index of the CU in the table of all CUs in the index file. */
17834 unsigned int previous_cu_index;
17835 /* Start address of the CU. */
17836 CORE_ADDR previous_cu_start;
17837 };
17838
17839 /* Write an address entry to OBSTACK. */
17840
17841 static void
17842 add_address_entry (struct objfile *objfile, struct obstack *obstack,
17843 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
17844 {
17845 offset_type cu_index_to_write;
17846 char addr[8];
17847 CORE_ADDR baseaddr;
17848
17849 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
17850
17851 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
17852 obstack_grow (obstack, addr, 8);
17853 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
17854 obstack_grow (obstack, addr, 8);
17855 cu_index_to_write = MAYBE_SWAP (cu_index);
17856 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
17857 }
17858
17859 /* Worker function for traversing an addrmap to build the address table. */
17860
17861 static int
17862 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
17863 {
17864 struct addrmap_index_data *data = datap;
17865 struct partial_symtab *pst = obj;
17866
17867 if (data->previous_valid)
17868 add_address_entry (data->objfile, data->addr_obstack,
17869 data->previous_cu_start, start_addr,
17870 data->previous_cu_index);
17871
17872 data->previous_cu_start = start_addr;
17873 if (pst != NULL)
17874 {
17875 struct psymtab_cu_index_map find_map, *map;
17876 find_map.psymtab = pst;
17877 map = htab_find (data->cu_index_htab, &find_map);
17878 gdb_assert (map != NULL);
17879 data->previous_cu_index = map->cu_index;
17880 data->previous_valid = 1;
17881 }
17882 else
17883 data->previous_valid = 0;
17884
17885 return 0;
17886 }
17887
17888 /* Write OBJFILE's address map to OBSTACK.
17889 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
17890 in the index file. */
17891
17892 static void
17893 write_address_map (struct objfile *objfile, struct obstack *obstack,
17894 htab_t cu_index_htab)
17895 {
17896 struct addrmap_index_data addrmap_index_data;
17897
17898 /* When writing the address table, we have to cope with the fact that
17899 the addrmap iterator only provides the start of a region; we have to
17900 wait until the next invocation to get the start of the next region. */
17901
17902 addrmap_index_data.objfile = objfile;
17903 addrmap_index_data.addr_obstack = obstack;
17904 addrmap_index_data.cu_index_htab = cu_index_htab;
17905 addrmap_index_data.previous_valid = 0;
17906
17907 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
17908 &addrmap_index_data);
17909
17910 /* It's highly unlikely the last entry (end address = 0xff...ff)
17911 is valid, but we should still handle it.
17912 The end address is recorded as the start of the next region, but that
17913 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
17914 anyway. */
17915 if (addrmap_index_data.previous_valid)
17916 add_address_entry (objfile, obstack,
17917 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
17918 addrmap_index_data.previous_cu_index);
17919 }
17920
17921 /* Return the symbol kind of PSYM. */
17922
17923 static gdb_index_symbol_kind
17924 symbol_kind (struct partial_symbol *psym)
17925 {
17926 domain_enum domain = PSYMBOL_DOMAIN (psym);
17927 enum address_class aclass = PSYMBOL_CLASS (psym);
17928
17929 switch (domain)
17930 {
17931 case VAR_DOMAIN:
17932 switch (aclass)
17933 {
17934 case LOC_BLOCK:
17935 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
17936 case LOC_TYPEDEF:
17937 return GDB_INDEX_SYMBOL_KIND_TYPE;
17938 case LOC_COMPUTED:
17939 case LOC_CONST_BYTES:
17940 case LOC_OPTIMIZED_OUT:
17941 case LOC_STATIC:
17942 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
17943 case LOC_CONST:
17944 /* Note: It's currently impossible to recognize psyms as enum values
17945 short of reading the type info. For now punt. */
17946 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
17947 default:
17948 /* There are other LOC_FOO values that one might want to classify
17949 as variables, but dwarf2read.c doesn't currently use them. */
17950 return GDB_INDEX_SYMBOL_KIND_OTHER;
17951 }
17952 case STRUCT_DOMAIN:
17953 return GDB_INDEX_SYMBOL_KIND_TYPE;
17954 default:
17955 return GDB_INDEX_SYMBOL_KIND_OTHER;
17956 }
17957 }
17958
17959 /* Add a list of partial symbols to SYMTAB. */
17960
17961 static void
17962 write_psymbols (struct mapped_symtab *symtab,
17963 htab_t psyms_seen,
17964 struct partial_symbol **psymp,
17965 int count,
17966 offset_type cu_index,
17967 int is_static)
17968 {
17969 for (; count-- > 0; ++psymp)
17970 {
17971 struct partial_symbol *psym = *psymp;
17972 void **slot;
17973
17974 if (SYMBOL_LANGUAGE (psym) == language_ada)
17975 error (_("Ada is not currently supported by the index"));
17976
17977 /* Only add a given psymbol once. */
17978 slot = htab_find_slot (psyms_seen, psym, INSERT);
17979 if (!*slot)
17980 {
17981 gdb_index_symbol_kind kind = symbol_kind (psym);
17982
17983 *slot = psym;
17984 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
17985 is_static, kind, cu_index);
17986 }
17987 }
17988 }
17989
17990 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
17991 exception if there is an error. */
17992
17993 static void
17994 write_obstack (FILE *file, struct obstack *obstack)
17995 {
17996 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
17997 file)
17998 != obstack_object_size (obstack))
17999 error (_("couldn't data write to file"));
18000 }
18001
18002 /* Unlink a file if the argument is not NULL. */
18003
18004 static void
18005 unlink_if_set (void *p)
18006 {
18007 char **filename = p;
18008 if (*filename)
18009 unlink (*filename);
18010 }
18011
18012 /* A helper struct used when iterating over debug_types. */
18013 struct signatured_type_index_data
18014 {
18015 struct objfile *objfile;
18016 struct mapped_symtab *symtab;
18017 struct obstack *types_list;
18018 htab_t psyms_seen;
18019 int cu_index;
18020 };
18021
18022 /* A helper function that writes a single signatured_type to an
18023 obstack. */
18024
18025 static int
18026 write_one_signatured_type (void **slot, void *d)
18027 {
18028 struct signatured_type_index_data *info = d;
18029 struct signatured_type *entry = (struct signatured_type *) *slot;
18030 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
18031 struct partial_symtab *psymtab = per_cu->v.psymtab;
18032 gdb_byte val[8];
18033
18034 write_psymbols (info->symtab,
18035 info->psyms_seen,
18036 info->objfile->global_psymbols.list
18037 + psymtab->globals_offset,
18038 psymtab->n_global_syms, info->cu_index,
18039 0);
18040 write_psymbols (info->symtab,
18041 info->psyms_seen,
18042 info->objfile->static_psymbols.list
18043 + psymtab->statics_offset,
18044 psymtab->n_static_syms, info->cu_index,
18045 1);
18046
18047 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
18048 entry->per_cu.offset.sect_off);
18049 obstack_grow (info->types_list, val, 8);
18050 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
18051 entry->type_offset_in_tu.cu_off);
18052 obstack_grow (info->types_list, val, 8);
18053 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
18054 obstack_grow (info->types_list, val, 8);
18055
18056 ++info->cu_index;
18057
18058 return 1;
18059 }
18060
18061 /* Recurse into all "included" dependencies and write their symbols as
18062 if they appeared in this psymtab. */
18063
18064 static void
18065 recursively_write_psymbols (struct objfile *objfile,
18066 struct partial_symtab *psymtab,
18067 struct mapped_symtab *symtab,
18068 htab_t psyms_seen,
18069 offset_type cu_index)
18070 {
18071 int i;
18072
18073 for (i = 0; i < psymtab->number_of_dependencies; ++i)
18074 if (psymtab->dependencies[i]->user != NULL)
18075 recursively_write_psymbols (objfile, psymtab->dependencies[i],
18076 symtab, psyms_seen, cu_index);
18077
18078 write_psymbols (symtab,
18079 psyms_seen,
18080 objfile->global_psymbols.list + psymtab->globals_offset,
18081 psymtab->n_global_syms, cu_index,
18082 0);
18083 write_psymbols (symtab,
18084 psyms_seen,
18085 objfile->static_psymbols.list + psymtab->statics_offset,
18086 psymtab->n_static_syms, cu_index,
18087 1);
18088 }
18089
18090 /* Create an index file for OBJFILE in the directory DIR. */
18091
18092 static void
18093 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
18094 {
18095 struct cleanup *cleanup;
18096 char *filename, *cleanup_filename;
18097 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
18098 struct obstack cu_list, types_cu_list;
18099 int i;
18100 FILE *out_file;
18101 struct mapped_symtab *symtab;
18102 offset_type val, size_of_contents, total_len;
18103 struct stat st;
18104 htab_t psyms_seen;
18105 htab_t cu_index_htab;
18106 struct psymtab_cu_index_map *psymtab_cu_index_map;
18107
18108 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
18109 return;
18110
18111 if (dwarf2_per_objfile->using_index)
18112 error (_("Cannot use an index to create the index"));
18113
18114 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
18115 error (_("Cannot make an index when the file has multiple .debug_types sections"));
18116
18117 if (stat (objfile->name, &st) < 0)
18118 perror_with_name (objfile->name);
18119
18120 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
18121 INDEX_SUFFIX, (char *) NULL);
18122 cleanup = make_cleanup (xfree, filename);
18123
18124 out_file = fopen (filename, "wb");
18125 if (!out_file)
18126 error (_("Can't open `%s' for writing"), filename);
18127
18128 cleanup_filename = filename;
18129 make_cleanup (unlink_if_set, &cleanup_filename);
18130
18131 symtab = create_mapped_symtab ();
18132 make_cleanup (cleanup_mapped_symtab, symtab);
18133
18134 obstack_init (&addr_obstack);
18135 make_cleanup_obstack_free (&addr_obstack);
18136
18137 obstack_init (&cu_list);
18138 make_cleanup_obstack_free (&cu_list);
18139
18140 obstack_init (&types_cu_list);
18141 make_cleanup_obstack_free (&types_cu_list);
18142
18143 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
18144 NULL, xcalloc, xfree);
18145 make_cleanup_htab_delete (psyms_seen);
18146
18147 /* While we're scanning CU's create a table that maps a psymtab pointer
18148 (which is what addrmap records) to its index (which is what is recorded
18149 in the index file). This will later be needed to write the address
18150 table. */
18151 cu_index_htab = htab_create_alloc (100,
18152 hash_psymtab_cu_index,
18153 eq_psymtab_cu_index,
18154 NULL, xcalloc, xfree);
18155 make_cleanup_htab_delete (cu_index_htab);
18156 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
18157 xmalloc (sizeof (struct psymtab_cu_index_map)
18158 * dwarf2_per_objfile->n_comp_units);
18159 make_cleanup (xfree, psymtab_cu_index_map);
18160
18161 /* The CU list is already sorted, so we don't need to do additional
18162 work here. Also, the debug_types entries do not appear in
18163 all_comp_units, but only in their own hash table. */
18164 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
18165 {
18166 struct dwarf2_per_cu_data *per_cu
18167 = dwarf2_per_objfile->all_comp_units[i];
18168 struct partial_symtab *psymtab = per_cu->v.psymtab;
18169 gdb_byte val[8];
18170 struct psymtab_cu_index_map *map;
18171 void **slot;
18172
18173 if (psymtab->user == NULL)
18174 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
18175
18176 map = &psymtab_cu_index_map[i];
18177 map->psymtab = psymtab;
18178 map->cu_index = i;
18179 slot = htab_find_slot (cu_index_htab, map, INSERT);
18180 gdb_assert (slot != NULL);
18181 gdb_assert (*slot == NULL);
18182 *slot = map;
18183
18184 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
18185 per_cu->offset.sect_off);
18186 obstack_grow (&cu_list, val, 8);
18187 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
18188 obstack_grow (&cu_list, val, 8);
18189 }
18190
18191 /* Dump the address map. */
18192 write_address_map (objfile, &addr_obstack, cu_index_htab);
18193
18194 /* Write out the .debug_type entries, if any. */
18195 if (dwarf2_per_objfile->signatured_types)
18196 {
18197 struct signatured_type_index_data sig_data;
18198
18199 sig_data.objfile = objfile;
18200 sig_data.symtab = symtab;
18201 sig_data.types_list = &types_cu_list;
18202 sig_data.psyms_seen = psyms_seen;
18203 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
18204 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
18205 write_one_signatured_type, &sig_data);
18206 }
18207
18208 /* Now that we've processed all symbols we can shrink their cu_indices
18209 lists. */
18210 uniquify_cu_indices (symtab);
18211
18212 obstack_init (&constant_pool);
18213 make_cleanup_obstack_free (&constant_pool);
18214 obstack_init (&symtab_obstack);
18215 make_cleanup_obstack_free (&symtab_obstack);
18216 write_hash_table (symtab, &symtab_obstack, &constant_pool);
18217
18218 obstack_init (&contents);
18219 make_cleanup_obstack_free (&contents);
18220 size_of_contents = 6 * sizeof (offset_type);
18221 total_len = size_of_contents;
18222
18223 /* The version number. */
18224 val = MAYBE_SWAP (7);
18225 obstack_grow (&contents, &val, sizeof (val));
18226
18227 /* The offset of the CU list from the start of the file. */
18228 val = MAYBE_SWAP (total_len);
18229 obstack_grow (&contents, &val, sizeof (val));
18230 total_len += obstack_object_size (&cu_list);
18231
18232 /* The offset of the types CU list from the start of the file. */
18233 val = MAYBE_SWAP (total_len);
18234 obstack_grow (&contents, &val, sizeof (val));
18235 total_len += obstack_object_size (&types_cu_list);
18236
18237 /* The offset of the address table from the start of the file. */
18238 val = MAYBE_SWAP (total_len);
18239 obstack_grow (&contents, &val, sizeof (val));
18240 total_len += obstack_object_size (&addr_obstack);
18241
18242 /* The offset of the symbol table from the start of the file. */
18243 val = MAYBE_SWAP (total_len);
18244 obstack_grow (&contents, &val, sizeof (val));
18245 total_len += obstack_object_size (&symtab_obstack);
18246
18247 /* The offset of the constant pool from the start of the file. */
18248 val = MAYBE_SWAP (total_len);
18249 obstack_grow (&contents, &val, sizeof (val));
18250 total_len += obstack_object_size (&constant_pool);
18251
18252 gdb_assert (obstack_object_size (&contents) == size_of_contents);
18253
18254 write_obstack (out_file, &contents);
18255 write_obstack (out_file, &cu_list);
18256 write_obstack (out_file, &types_cu_list);
18257 write_obstack (out_file, &addr_obstack);
18258 write_obstack (out_file, &symtab_obstack);
18259 write_obstack (out_file, &constant_pool);
18260
18261 fclose (out_file);
18262
18263 /* We want to keep the file, so we set cleanup_filename to NULL
18264 here. See unlink_if_set. */
18265 cleanup_filename = NULL;
18266
18267 do_cleanups (cleanup);
18268 }
18269
18270 /* Implementation of the `save gdb-index' command.
18271
18272 Note that the file format used by this command is documented in the
18273 GDB manual. Any changes here must be documented there. */
18274
18275 static void
18276 save_gdb_index_command (char *arg, int from_tty)
18277 {
18278 struct objfile *objfile;
18279
18280 if (!arg || !*arg)
18281 error (_("usage: save gdb-index DIRECTORY"));
18282
18283 ALL_OBJFILES (objfile)
18284 {
18285 struct stat st;
18286
18287 /* If the objfile does not correspond to an actual file, skip it. */
18288 if (stat (objfile->name, &st) < 0)
18289 continue;
18290
18291 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
18292 if (dwarf2_per_objfile)
18293 {
18294 volatile struct gdb_exception except;
18295
18296 TRY_CATCH (except, RETURN_MASK_ERROR)
18297 {
18298 write_psymtabs_to_index (objfile, arg);
18299 }
18300 if (except.reason < 0)
18301 exception_fprintf (gdb_stderr, except,
18302 _("Error while writing index for `%s': "),
18303 objfile->name);
18304 }
18305 }
18306 }
18307
18308 \f
18309
18310 int dwarf2_always_disassemble;
18311
18312 static void
18313 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
18314 struct cmd_list_element *c, const char *value)
18315 {
18316 fprintf_filtered (file,
18317 _("Whether to always disassemble "
18318 "DWARF expressions is %s.\n"),
18319 value);
18320 }
18321
18322 static void
18323 show_check_physname (struct ui_file *file, int from_tty,
18324 struct cmd_list_element *c, const char *value)
18325 {
18326 fprintf_filtered (file,
18327 _("Whether to check \"physname\" is %s.\n"),
18328 value);
18329 }
18330
18331 void _initialize_dwarf2_read (void);
18332
18333 void
18334 _initialize_dwarf2_read (void)
18335 {
18336 struct cmd_list_element *c;
18337
18338 dwarf2_objfile_data_key
18339 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
18340
18341 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
18342 Set DWARF 2 specific variables.\n\
18343 Configure DWARF 2 variables such as the cache size"),
18344 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
18345 0/*allow-unknown*/, &maintenance_set_cmdlist);
18346
18347 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
18348 Show DWARF 2 specific variables\n\
18349 Show DWARF 2 variables such as the cache size"),
18350 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
18351 0/*allow-unknown*/, &maintenance_show_cmdlist);
18352
18353 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
18354 &dwarf2_max_cache_age, _("\
18355 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
18356 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
18357 A higher limit means that cached compilation units will be stored\n\
18358 in memory longer, and more total memory will be used. Zero disables\n\
18359 caching, which can slow down startup."),
18360 NULL,
18361 show_dwarf2_max_cache_age,
18362 &set_dwarf2_cmdlist,
18363 &show_dwarf2_cmdlist);
18364
18365 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
18366 &dwarf2_always_disassemble, _("\
18367 Set whether `info address' always disassembles DWARF expressions."), _("\
18368 Show whether `info address' always disassembles DWARF expressions."), _("\
18369 When enabled, DWARF expressions are always printed in an assembly-like\n\
18370 syntax. When disabled, expressions will be printed in a more\n\
18371 conversational style, when possible."),
18372 NULL,
18373 show_dwarf2_always_disassemble,
18374 &set_dwarf2_cmdlist,
18375 &show_dwarf2_cmdlist);
18376
18377 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
18378 Set debugging of the dwarf2 reader."), _("\
18379 Show debugging of the dwarf2 reader."), _("\
18380 When enabled, debugging messages are printed during dwarf2 reading\n\
18381 and symtab expansion."),
18382 NULL,
18383 NULL,
18384 &setdebuglist, &showdebuglist);
18385
18386 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
18387 Set debugging of the dwarf2 DIE reader."), _("\
18388 Show debugging of the dwarf2 DIE reader."), _("\
18389 When enabled (non-zero), DIEs are dumped after they are read in.\n\
18390 The value is the maximum depth to print."),
18391 NULL,
18392 NULL,
18393 &setdebuglist, &showdebuglist);
18394
18395 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
18396 Set cross-checking of \"physname\" code against demangler."), _("\
18397 Show cross-checking of \"physname\" code against demangler."), _("\
18398 When enabled, GDB's internal \"physname\" code is checked against\n\
18399 the demangler."),
18400 NULL, show_check_physname,
18401 &setdebuglist, &showdebuglist);
18402
18403 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
18404 _("\
18405 Save a gdb-index file.\n\
18406 Usage: save gdb-index DIRECTORY"),
18407 &save_cmdlist);
18408 set_cmd_completer (c, filename_completer);
18409 }
This page took 0.380735 seconds and 5 git commands to generate.