* dwarf2read.c (read_die_and_siblings_1): Renamed from
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2013 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 "elf-bfd.h"
34 #include "symtab.h"
35 #include "gdbtypes.h"
36 #include "objfiles.h"
37 #include "dwarf2.h"
38 #include "buildsym.h"
39 #include "demangle.h"
40 #include "gdb-demangle.h"
41 #include "expression.h"
42 #include "filenames.h" /* for DOSish file names */
43 #include "macrotab.h"
44 #include "language.h"
45 #include "complaints.h"
46 #include "bcache.h"
47 #include "dwarf2expr.h"
48 #include "dwarf2loc.h"
49 #include "cp-support.h"
50 #include "hashtab.h"
51 #include "command.h"
52 #include "gdbcmd.h"
53 #include "block.h"
54 #include "addrmap.h"
55 #include "typeprint.h"
56 #include "jv-lang.h"
57 #include "psympriv.h"
58 #include "exceptions.h"
59 #include "gdb_stat.h"
60 #include "completer.h"
61 #include "vec.h"
62 #include "c-lang.h"
63 #include "go-lang.h"
64 #include "valprint.h"
65 #include "gdbcore.h" /* for gnutarget */
66 #include "gdb/gdb-index.h"
67 #include <ctype.h>
68 #include "gdb_bfd.h"
69 #include "f-lang.h"
70 #include "source.h"
71
72 #include <fcntl.h>
73 #include "gdb_string.h"
74 #include "gdb_assert.h"
75 #include <sys/types.h>
76
77 typedef struct symbol *symbolp;
78 DEF_VEC_P (symbolp);
79
80 /* When non-zero, print basic high level tracing messages.
81 This is in contrast to the low level DIE reading of dwarf2_die_debug. */
82 static int dwarf2_read_debug = 0;
83
84 /* When non-zero, dump DIEs after they are read in. */
85 static unsigned int dwarf2_die_debug = 0;
86
87 /* When non-zero, cross-check physname against demangler. */
88 static int check_physname = 0;
89
90 /* When non-zero, do not reject deprecated .gdb_index sections. */
91 static int use_deprecated_index_sections = 0;
92
93 static const struct objfile_data *dwarf2_objfile_data_key;
94
95 /* The "aclass" indices for various kinds of computed DWARF symbols. */
96
97 static int dwarf2_locexpr_index;
98 static int dwarf2_loclist_index;
99 static int dwarf2_locexpr_block_index;
100 static int dwarf2_loclist_block_index;
101
102 struct dwarf2_section_info
103 {
104 asection *asection;
105 gdb_byte *buffer;
106 bfd_size_type size;
107 /* True if we have tried to read this section. */
108 int readin;
109 };
110
111 typedef struct dwarf2_section_info dwarf2_section_info_def;
112 DEF_VEC_O (dwarf2_section_info_def);
113
114 /* All offsets in the index are of this type. It must be
115 architecture-independent. */
116 typedef uint32_t offset_type;
117
118 DEF_VEC_I (offset_type);
119
120 /* Ensure only legit values are used. */
121 #define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
122 do { \
123 gdb_assert ((unsigned int) (value) <= 1); \
124 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
125 } while (0)
126
127 /* Ensure only legit values are used. */
128 #define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
129 do { \
130 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
131 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
132 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
133 } while (0)
134
135 /* Ensure we don't use more than the alloted nuber of bits for the CU. */
136 #define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
137 do { \
138 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
139 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
140 } while (0)
141
142 /* A description of the mapped index. The file format is described in
143 a comment by the code that writes the index. */
144 struct mapped_index
145 {
146 /* Index data format version. */
147 int version;
148
149 /* The total length of the buffer. */
150 off_t total_size;
151
152 /* A pointer to the address table data. */
153 const gdb_byte *address_table;
154
155 /* Size of the address table data in bytes. */
156 offset_type address_table_size;
157
158 /* The symbol table, implemented as a hash table. */
159 const offset_type *symbol_table;
160
161 /* Size in slots, each slot is 2 offset_types. */
162 offset_type symbol_table_slots;
163
164 /* A pointer to the constant pool. */
165 const char *constant_pool;
166 };
167
168 typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
169 DEF_VEC_P (dwarf2_per_cu_ptr);
170
171 /* Collection of data recorded per objfile.
172 This hangs off of dwarf2_objfile_data_key. */
173
174 struct dwarf2_per_objfile
175 {
176 struct dwarf2_section_info info;
177 struct dwarf2_section_info abbrev;
178 struct dwarf2_section_info line;
179 struct dwarf2_section_info loc;
180 struct dwarf2_section_info macinfo;
181 struct dwarf2_section_info macro;
182 struct dwarf2_section_info str;
183 struct dwarf2_section_info ranges;
184 struct dwarf2_section_info addr;
185 struct dwarf2_section_info frame;
186 struct dwarf2_section_info eh_frame;
187 struct dwarf2_section_info gdb_index;
188
189 VEC (dwarf2_section_info_def) *types;
190
191 /* Back link. */
192 struct objfile *objfile;
193
194 /* Table of all the compilation units. This is used to locate
195 the target compilation unit of a particular reference. */
196 struct dwarf2_per_cu_data **all_comp_units;
197
198 /* The number of compilation units in ALL_COMP_UNITS. */
199 int n_comp_units;
200
201 /* The number of .debug_types-related CUs. */
202 int n_type_units;
203
204 /* The .debug_types-related CUs (TUs). */
205 struct signatured_type **all_type_units;
206
207 /* The number of entries in all_type_unit_groups. */
208 int n_type_unit_groups;
209
210 /* Table of type unit groups.
211 This exists to make it easy to iterate over all CUs and TU groups. */
212 struct type_unit_group **all_type_unit_groups;
213
214 /* Table of struct type_unit_group objects.
215 The hash key is the DW_AT_stmt_list value. */
216 htab_t type_unit_groups;
217
218 /* A table mapping .debug_types signatures to its signatured_type entry.
219 This is NULL if the .debug_types section hasn't been read in yet. */
220 htab_t signatured_types;
221
222 /* Type unit statistics, to see how well the scaling improvements
223 are doing. */
224 struct tu_stats
225 {
226 int nr_uniq_abbrev_tables;
227 int nr_symtabs;
228 int nr_symtab_sharers;
229 int nr_stmt_less_type_units;
230 } tu_stats;
231
232 /* A chain of compilation units that are currently read in, so that
233 they can be freed later. */
234 struct dwarf2_per_cu_data *read_in_chain;
235
236 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
237 This is NULL if the table hasn't been allocated yet. */
238 htab_t dwo_files;
239
240 /* Non-zero if we've check for whether there is a DWP file. */
241 int dwp_checked;
242
243 /* The DWP file if there is one, or NULL. */
244 struct dwp_file *dwp_file;
245
246 /* The shared '.dwz' file, if one exists. This is used when the
247 original data was compressed using 'dwz -m'. */
248 struct dwz_file *dwz_file;
249
250 /* A flag indicating wether this objfile has a section loaded at a
251 VMA of 0. */
252 int has_section_at_zero;
253
254 /* True if we are using the mapped index,
255 or we are faking it for OBJF_READNOW's sake. */
256 unsigned char using_index;
257
258 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
259 struct mapped_index *index_table;
260
261 /* When using index_table, this keeps track of all quick_file_names entries.
262 TUs typically share line table entries with a CU, so we maintain a
263 separate table of all line table entries to support the sharing.
264 Note that while there can be way more TUs than CUs, we've already
265 sorted all the TUs into "type unit groups", grouped by their
266 DW_AT_stmt_list value. Therefore the only sharing done here is with a
267 CU and its associated TU group if there is one. */
268 htab_t quick_file_names_table;
269
270 /* Set during partial symbol reading, to prevent queueing of full
271 symbols. */
272 int reading_partial_symbols;
273
274 /* Table mapping type DIEs to their struct type *.
275 This is NULL if not allocated yet.
276 The mapping is done via (CU/TU signature + DIE offset) -> type. */
277 htab_t die_type_hash;
278
279 /* The CUs we recently read. */
280 VEC (dwarf2_per_cu_ptr) *just_read_cus;
281 };
282
283 static struct dwarf2_per_objfile *dwarf2_per_objfile;
284
285 /* Default names of the debugging sections. */
286
287 /* Note that if the debugging section has been compressed, it might
288 have a name like .zdebug_info. */
289
290 static const struct dwarf2_debug_sections dwarf2_elf_names =
291 {
292 { ".debug_info", ".zdebug_info" },
293 { ".debug_abbrev", ".zdebug_abbrev" },
294 { ".debug_line", ".zdebug_line" },
295 { ".debug_loc", ".zdebug_loc" },
296 { ".debug_macinfo", ".zdebug_macinfo" },
297 { ".debug_macro", ".zdebug_macro" },
298 { ".debug_str", ".zdebug_str" },
299 { ".debug_ranges", ".zdebug_ranges" },
300 { ".debug_types", ".zdebug_types" },
301 { ".debug_addr", ".zdebug_addr" },
302 { ".debug_frame", ".zdebug_frame" },
303 { ".eh_frame", NULL },
304 { ".gdb_index", ".zgdb_index" },
305 23
306 };
307
308 /* List of DWO/DWP sections. */
309
310 static const struct dwop_section_names
311 {
312 struct dwarf2_section_names abbrev_dwo;
313 struct dwarf2_section_names info_dwo;
314 struct dwarf2_section_names line_dwo;
315 struct dwarf2_section_names loc_dwo;
316 struct dwarf2_section_names macinfo_dwo;
317 struct dwarf2_section_names macro_dwo;
318 struct dwarf2_section_names str_dwo;
319 struct dwarf2_section_names str_offsets_dwo;
320 struct dwarf2_section_names types_dwo;
321 struct dwarf2_section_names cu_index;
322 struct dwarf2_section_names tu_index;
323 }
324 dwop_section_names =
325 {
326 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
327 { ".debug_info.dwo", ".zdebug_info.dwo" },
328 { ".debug_line.dwo", ".zdebug_line.dwo" },
329 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
330 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
331 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
332 { ".debug_str.dwo", ".zdebug_str.dwo" },
333 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
334 { ".debug_types.dwo", ".zdebug_types.dwo" },
335 { ".debug_cu_index", ".zdebug_cu_index" },
336 { ".debug_tu_index", ".zdebug_tu_index" },
337 };
338
339 /* local data types */
340
341 /* The data in a compilation unit header, after target2host
342 translation, looks like this. */
343 struct comp_unit_head
344 {
345 unsigned int length;
346 short version;
347 unsigned char addr_size;
348 unsigned char signed_addr_p;
349 sect_offset abbrev_offset;
350
351 /* Size of file offsets; either 4 or 8. */
352 unsigned int offset_size;
353
354 /* Size of the length field; either 4 or 12. */
355 unsigned int initial_length_size;
356
357 /* Offset to the first byte of this compilation unit header in the
358 .debug_info section, for resolving relative reference dies. */
359 sect_offset offset;
360
361 /* Offset to first die in this cu from the start of the cu.
362 This will be the first byte following the compilation unit header. */
363 cu_offset first_die_offset;
364 };
365
366 /* Type used for delaying computation of method physnames.
367 See comments for compute_delayed_physnames. */
368 struct delayed_method_info
369 {
370 /* The type to which the method is attached, i.e., its parent class. */
371 struct type *type;
372
373 /* The index of the method in the type's function fieldlists. */
374 int fnfield_index;
375
376 /* The index of the method in the fieldlist. */
377 int index;
378
379 /* The name of the DIE. */
380 const char *name;
381
382 /* The DIE associated with this method. */
383 struct die_info *die;
384 };
385
386 typedef struct delayed_method_info delayed_method_info;
387 DEF_VEC_O (delayed_method_info);
388
389 /* Internal state when decoding a particular compilation unit. */
390 struct dwarf2_cu
391 {
392 /* The objfile containing this compilation unit. */
393 struct objfile *objfile;
394
395 /* The header of the compilation unit. */
396 struct comp_unit_head header;
397
398 /* Base address of this compilation unit. */
399 CORE_ADDR base_address;
400
401 /* Non-zero if base_address has been set. */
402 int base_known;
403
404 /* The language we are debugging. */
405 enum language language;
406 const struct language_defn *language_defn;
407
408 const char *producer;
409
410 /* The generic symbol table building routines have separate lists for
411 file scope symbols and all all other scopes (local scopes). So
412 we need to select the right one to pass to add_symbol_to_list().
413 We do it by keeping a pointer to the correct list in list_in_scope.
414
415 FIXME: The original dwarf code just treated the file scope as the
416 first local scope, and all other local scopes as nested local
417 scopes, and worked fine. Check to see if we really need to
418 distinguish these in buildsym.c. */
419 struct pending **list_in_scope;
420
421 /* The abbrev table for this CU.
422 Normally this points to the abbrev table in the objfile.
423 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
424 struct abbrev_table *abbrev_table;
425
426 /* Hash table holding all the loaded partial DIEs
427 with partial_die->offset.SECT_OFF as hash. */
428 htab_t partial_dies;
429
430 /* Storage for things with the same lifetime as this read-in compilation
431 unit, including partial DIEs. */
432 struct obstack comp_unit_obstack;
433
434 /* When multiple dwarf2_cu structures are living in memory, this field
435 chains them all together, so that they can be released efficiently.
436 We will probably also want a generation counter so that most-recently-used
437 compilation units are cached... */
438 struct dwarf2_per_cu_data *read_in_chain;
439
440 /* Backchain to our per_cu entry if the tree has been built. */
441 struct dwarf2_per_cu_data *per_cu;
442
443 /* How many compilation units ago was this CU last referenced? */
444 int last_used;
445
446 /* A hash table of DIE cu_offset for following references with
447 die_info->offset.sect_off as hash. */
448 htab_t die_hash;
449
450 /* Full DIEs if read in. */
451 struct die_info *dies;
452
453 /* A set of pointers to dwarf2_per_cu_data objects for compilation
454 units referenced by this one. Only set during full symbol processing;
455 partial symbol tables do not have dependencies. */
456 htab_t dependencies;
457
458 /* Header data from the line table, during full symbol processing. */
459 struct line_header *line_header;
460
461 /* A list of methods which need to have physnames computed
462 after all type information has been read. */
463 VEC (delayed_method_info) *method_list;
464
465 /* To be copied to symtab->call_site_htab. */
466 htab_t call_site_htab;
467
468 /* Non-NULL if this CU came from a DWO file.
469 There is an invariant here that is important to remember:
470 Except for attributes copied from the top level DIE in the "main"
471 (or "stub") file in preparation for reading the DWO file
472 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
473 Either there isn't a DWO file (in which case this is NULL and the point
474 is moot), or there is and either we're not going to read it (in which
475 case this is NULL) or there is and we are reading it (in which case this
476 is non-NULL). */
477 struct dwo_unit *dwo_unit;
478
479 /* The DW_AT_addr_base attribute if present, zero otherwise
480 (zero is a valid value though).
481 Note this value comes from the stub CU/TU's DIE. */
482 ULONGEST addr_base;
483
484 /* The DW_AT_ranges_base attribute if present, zero otherwise
485 (zero is a valid value though).
486 Note this value comes from the stub CU/TU's DIE.
487 Also note that the value is zero in the non-DWO case so this value can
488 be used without needing to know whether DWO files are in use or not.
489 N.B. This does not apply to DW_AT_ranges appearing in
490 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
491 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
492 DW_AT_ranges_base *would* have to be applied, and we'd have to care
493 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
494 ULONGEST ranges_base;
495
496 /* Mark used when releasing cached dies. */
497 unsigned int mark : 1;
498
499 /* This CU references .debug_loc. See the symtab->locations_valid field.
500 This test is imperfect as there may exist optimized debug code not using
501 any location list and still facing inlining issues if handled as
502 unoptimized code. For a future better test see GCC PR other/32998. */
503 unsigned int has_loclist : 1;
504
505 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
506 if all the producer_is_* fields are valid. This information is cached
507 because profiling CU expansion showed excessive time spent in
508 producer_is_gxx_lt_4_6. */
509 unsigned int checked_producer : 1;
510 unsigned int producer_is_gxx_lt_4_6 : 1;
511 unsigned int producer_is_gcc_lt_4_3 : 1;
512 unsigned int producer_is_icc : 1;
513
514 /* When set, the file that we're processing is known to have
515 debugging info for C++ namespaces. GCC 3.3.x did not produce
516 this information, but later versions do. */
517
518 unsigned int processing_has_namespace_info : 1;
519 };
520
521 /* Persistent data held for a compilation unit, even when not
522 processing it. We put a pointer to this structure in the
523 read_symtab_private field of the psymtab. */
524
525 struct dwarf2_per_cu_data
526 {
527 /* The start offset and length of this compilation unit.
528 NOTE: Unlike comp_unit_head.length, this length includes
529 initial_length_size.
530 If the DIE refers to a DWO file, this is always of the original die,
531 not the DWO file. */
532 sect_offset offset;
533 unsigned int length;
534
535 /* Flag indicating this compilation unit will be read in before
536 any of the current compilation units are processed. */
537 unsigned int queued : 1;
538
539 /* This flag will be set when reading partial DIEs if we need to load
540 absolutely all DIEs for this compilation unit, instead of just the ones
541 we think are interesting. It gets set if we look for a DIE in the
542 hash table and don't find it. */
543 unsigned int load_all_dies : 1;
544
545 /* Non-zero if this CU is from .debug_types.
546 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
547 this is non-zero. */
548 unsigned int is_debug_types : 1;
549
550 /* Non-zero if this CU is from the .dwz file. */
551 unsigned int is_dwz : 1;
552
553 /* The section this CU/TU lives in.
554 If the DIE refers to a DWO file, this is always the original die,
555 not the DWO file. */
556 struct dwarf2_section_info *section;
557
558 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
559 of the CU cache it gets reset to NULL again. */
560 struct dwarf2_cu *cu;
561
562 /* The corresponding objfile.
563 Normally we can get the objfile from dwarf2_per_objfile.
564 However we can enter this file with just a "per_cu" handle. */
565 struct objfile *objfile;
566
567 /* When using partial symbol tables, the 'psymtab' field is active.
568 Otherwise the 'quick' field is active. */
569 union
570 {
571 /* The partial symbol table associated with this compilation unit,
572 or NULL for unread partial units. */
573 struct partial_symtab *psymtab;
574
575 /* Data needed by the "quick" functions. */
576 struct dwarf2_per_cu_quick_data *quick;
577 } v;
578
579 /* The CUs we import using DW_TAG_imported_unit. This is filled in
580 while reading psymtabs, used to compute the psymtab dependencies,
581 and then cleared. Then it is filled in again while reading full
582 symbols, and only deleted when the objfile is destroyed.
583
584 This is also used to work around a difference between the way gold
585 generates .gdb_index version <=7 and the way gdb does. Arguably this
586 is a gold bug. For symbols coming from TUs, gold records in the index
587 the CU that includes the TU instead of the TU itself. This breaks
588 dw2_lookup_symbol: It assumes that if the index says symbol X lives
589 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
590 will find X. Alas TUs live in their own symtab, so after expanding CU Y
591 we need to look in TU Z to find X. Fortunately, this is akin to
592 DW_TAG_imported_unit, so we just use the same mechanism: For
593 .gdb_index version <=7 this also records the TUs that the CU referred
594 to. Concurrently with this change gdb was modified to emit version 8
595 indices so we only pay a price for gold generated indices. */
596 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
597 };
598
599 /* Entry in the signatured_types hash table. */
600
601 struct signatured_type
602 {
603 /* The "per_cu" object of this type.
604 N.B.: This is the first member so that it's easy to convert pointers
605 between them. */
606 struct dwarf2_per_cu_data per_cu;
607
608 /* The type's signature. */
609 ULONGEST signature;
610
611 /* Offset in the TU of the type's DIE, as read from the TU header.
612 If this TU is a DWO stub and the definition lives in a DWO file
613 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
614 cu_offset type_offset_in_tu;
615
616 /* Offset in the section of the type's DIE.
617 If the definition lives in a DWO file, this is the offset in the
618 .debug_types.dwo section.
619 The value is zero until the actual value is known.
620 Zero is otherwise not a valid section offset. */
621 sect_offset type_offset_in_section;
622
623 /* Type units are grouped by their DW_AT_stmt_list entry so that they
624 can share them. This points to the containing symtab. */
625 struct type_unit_group *type_unit_group;
626 };
627
628 typedef struct signatured_type *sig_type_ptr;
629 DEF_VEC_P (sig_type_ptr);
630
631 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
632 This includes type_unit_group and quick_file_names. */
633
634 struct stmt_list_hash
635 {
636 /* The DWO unit this table is from or NULL if there is none. */
637 struct dwo_unit *dwo_unit;
638
639 /* Offset in .debug_line or .debug_line.dwo. */
640 sect_offset line_offset;
641 };
642
643 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
644 an object of this type. */
645
646 struct type_unit_group
647 {
648 /* dwarf2read.c's main "handle" on a TU symtab.
649 To simplify things we create an artificial CU that "includes" all the
650 type units using this stmt_list so that the rest of the code still has
651 a "per_cu" handle on the symtab.
652 This PER_CU is recognized by having no section. */
653 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
654 struct dwarf2_per_cu_data per_cu;
655
656 /* The TUs that share this DW_AT_stmt_list entry.
657 This is added to while parsing type units to build partial symtabs,
658 and is deleted afterwards and not used again. */
659 VEC (sig_type_ptr) *tus;
660
661 /* The primary symtab.
662 Type units in a group needn't all be defined in the same source file,
663 so we create an essentially anonymous symtab as the primary symtab. */
664 struct symtab *primary_symtab;
665
666 /* The data used to construct the hash key. */
667 struct stmt_list_hash hash;
668
669 /* The number of symtabs from the line header.
670 The value here must match line_header.num_file_names. */
671 unsigned int num_symtabs;
672
673 /* The symbol tables for this TU (obtained from the files listed in
674 DW_AT_stmt_list).
675 WARNING: The order of entries here must match the order of entries
676 in the line header. After the first TU using this type_unit_group, the
677 line header for the subsequent TUs is recreated from this. This is done
678 because we need to use the same symtabs for each TU using the same
679 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
680 there's no guarantee the line header doesn't have duplicate entries. */
681 struct symtab **symtabs;
682 };
683
684 /* These sections are what may appear in a DWO file. */
685
686 struct dwo_sections
687 {
688 struct dwarf2_section_info abbrev;
689 struct dwarf2_section_info line;
690 struct dwarf2_section_info loc;
691 struct dwarf2_section_info macinfo;
692 struct dwarf2_section_info macro;
693 struct dwarf2_section_info str;
694 struct dwarf2_section_info str_offsets;
695 /* In the case of a virtual DWO file, these two are unused. */
696 struct dwarf2_section_info info;
697 VEC (dwarf2_section_info_def) *types;
698 };
699
700 /* CUs/TUs in DWP/DWO files. */
701
702 struct dwo_unit
703 {
704 /* Backlink to the containing struct dwo_file. */
705 struct dwo_file *dwo_file;
706
707 /* The "id" that distinguishes this CU/TU.
708 .debug_info calls this "dwo_id", .debug_types calls this "signature".
709 Since signatures came first, we stick with it for consistency. */
710 ULONGEST signature;
711
712 /* The section this CU/TU lives in, in the DWO file. */
713 struct dwarf2_section_info *section;
714
715 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
716 sect_offset offset;
717 unsigned int length;
718
719 /* For types, offset in the type's DIE of the type defined by this TU. */
720 cu_offset type_offset_in_tu;
721 };
722
723 /* Data for one DWO file.
724 This includes virtual DWO files that have been packaged into a
725 DWP file. */
726
727 struct dwo_file
728 {
729 /* The DW_AT_GNU_dwo_name attribute.
730 For virtual DWO files the name is constructed from the section offsets
731 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
732 from related CU+TUs. */
733 const char *dwo_name;
734
735 /* The DW_AT_comp_dir attribute. */
736 const char *comp_dir;
737
738 /* The bfd, when the file is open. Otherwise this is NULL.
739 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
740 bfd *dbfd;
741
742 /* Section info for this file. */
743 struct dwo_sections sections;
744
745 /* Table of CUs in the file.
746 Each element is a struct dwo_unit. */
747 htab_t cus;
748
749 /* Table of TUs in the file.
750 Each element is a struct dwo_unit. */
751 htab_t tus;
752 };
753
754 /* These sections are what may appear in a DWP file. */
755
756 struct dwp_sections
757 {
758 struct dwarf2_section_info str;
759 struct dwarf2_section_info cu_index;
760 struct dwarf2_section_info tu_index;
761 /* The .debug_info.dwo, .debug_types.dwo, and other sections are referenced
762 by section number. We don't need to record them here. */
763 };
764
765 /* These sections are what may appear in a virtual DWO file. */
766
767 struct virtual_dwo_sections
768 {
769 struct dwarf2_section_info abbrev;
770 struct dwarf2_section_info line;
771 struct dwarf2_section_info loc;
772 struct dwarf2_section_info macinfo;
773 struct dwarf2_section_info macro;
774 struct dwarf2_section_info str_offsets;
775 /* Each DWP hash table entry records one CU or one TU.
776 That is recorded here, and copied to dwo_unit.section. */
777 struct dwarf2_section_info info_or_types;
778 };
779
780 /* Contents of DWP hash tables. */
781
782 struct dwp_hash_table
783 {
784 uint32_t nr_units, nr_slots;
785 const gdb_byte *hash_table, *unit_table, *section_pool;
786 };
787
788 /* Data for one DWP file. */
789
790 struct dwp_file
791 {
792 /* Name of the file. */
793 const char *name;
794
795 /* The bfd, when the file is open. Otherwise this is NULL. */
796 bfd *dbfd;
797
798 /* Section info for this file. */
799 struct dwp_sections sections;
800
801 /* Table of CUs in the file. */
802 const struct dwp_hash_table *cus;
803
804 /* Table of TUs in the file. */
805 const struct dwp_hash_table *tus;
806
807 /* Table of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
808 htab_t loaded_cutus;
809
810 /* Table to map ELF section numbers to their sections. */
811 unsigned int num_sections;
812 asection **elf_sections;
813 };
814
815 /* This represents a '.dwz' file. */
816
817 struct dwz_file
818 {
819 /* A dwz file can only contain a few sections. */
820 struct dwarf2_section_info abbrev;
821 struct dwarf2_section_info info;
822 struct dwarf2_section_info str;
823 struct dwarf2_section_info line;
824 struct dwarf2_section_info macro;
825 struct dwarf2_section_info gdb_index;
826
827 /* The dwz's BFD. */
828 bfd *dwz_bfd;
829 };
830
831 /* Struct used to pass misc. parameters to read_die_and_children, et
832 al. which are used for both .debug_info and .debug_types dies.
833 All parameters here are unchanging for the life of the call. This
834 struct exists to abstract away the constant parameters of die reading. */
835
836 struct die_reader_specs
837 {
838 /* die_section->asection->owner. */
839 bfd* abfd;
840
841 /* The CU of the DIE we are parsing. */
842 struct dwarf2_cu *cu;
843
844 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
845 struct dwo_file *dwo_file;
846
847 /* The section the die comes from.
848 This is either .debug_info or .debug_types, or the .dwo variants. */
849 struct dwarf2_section_info *die_section;
850
851 /* die_section->buffer. */
852 gdb_byte *buffer;
853
854 /* The end of the buffer. */
855 const gdb_byte *buffer_end;
856 };
857
858 /* Type of function passed to init_cutu_and_read_dies, et.al. */
859 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
860 gdb_byte *info_ptr,
861 struct die_info *comp_unit_die,
862 int has_children,
863 void *data);
864
865 /* The line number information for a compilation unit (found in the
866 .debug_line section) begins with a "statement program header",
867 which contains the following information. */
868 struct line_header
869 {
870 unsigned int total_length;
871 unsigned short version;
872 unsigned int header_length;
873 unsigned char minimum_instruction_length;
874 unsigned char maximum_ops_per_instruction;
875 unsigned char default_is_stmt;
876 int line_base;
877 unsigned char line_range;
878 unsigned char opcode_base;
879
880 /* standard_opcode_lengths[i] is the number of operands for the
881 standard opcode whose value is i. This means that
882 standard_opcode_lengths[0] is unused, and the last meaningful
883 element is standard_opcode_lengths[opcode_base - 1]. */
884 unsigned char *standard_opcode_lengths;
885
886 /* The include_directories table. NOTE! These strings are not
887 allocated with xmalloc; instead, they are pointers into
888 debug_line_buffer. If you try to free them, `free' will get
889 indigestion. */
890 unsigned int num_include_dirs, include_dirs_size;
891 char **include_dirs;
892
893 /* The file_names table. NOTE! These strings are not allocated
894 with xmalloc; instead, they are pointers into debug_line_buffer.
895 Don't try to free them directly. */
896 unsigned int num_file_names, file_names_size;
897 struct file_entry
898 {
899 char *name;
900 unsigned int dir_index;
901 unsigned int mod_time;
902 unsigned int length;
903 int included_p; /* Non-zero if referenced by the Line Number Program. */
904 struct symtab *symtab; /* The associated symbol table, if any. */
905 } *file_names;
906
907 /* The start and end of the statement program following this
908 header. These point into dwarf2_per_objfile->line_buffer. */
909 gdb_byte *statement_program_start, *statement_program_end;
910 };
911
912 /* When we construct a partial symbol table entry we only
913 need this much information. */
914 struct partial_die_info
915 {
916 /* Offset of this DIE. */
917 sect_offset offset;
918
919 /* DWARF-2 tag for this DIE. */
920 ENUM_BITFIELD(dwarf_tag) tag : 16;
921
922 /* Assorted flags describing the data found in this DIE. */
923 unsigned int has_children : 1;
924 unsigned int is_external : 1;
925 unsigned int is_declaration : 1;
926 unsigned int has_type : 1;
927 unsigned int has_specification : 1;
928 unsigned int has_pc_info : 1;
929 unsigned int may_be_inlined : 1;
930
931 /* Flag set if the SCOPE field of this structure has been
932 computed. */
933 unsigned int scope_set : 1;
934
935 /* Flag set if the DIE has a byte_size attribute. */
936 unsigned int has_byte_size : 1;
937
938 /* Flag set if any of the DIE's children are template arguments. */
939 unsigned int has_template_arguments : 1;
940
941 /* Flag set if fixup_partial_die has been called on this die. */
942 unsigned int fixup_called : 1;
943
944 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
945 unsigned int is_dwz : 1;
946
947 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
948 unsigned int spec_is_dwz : 1;
949
950 /* The name of this DIE. Normally the value of DW_AT_name, but
951 sometimes a default name for unnamed DIEs. */
952 const char *name;
953
954 /* The linkage name, if present. */
955 const char *linkage_name;
956
957 /* The scope to prepend to our children. This is generally
958 allocated on the comp_unit_obstack, so will disappear
959 when this compilation unit leaves the cache. */
960 const char *scope;
961
962 /* Some data associated with the partial DIE. The tag determines
963 which field is live. */
964 union
965 {
966 /* The location description associated with this DIE, if any. */
967 struct dwarf_block *locdesc;
968 /* The offset of an import, for DW_TAG_imported_unit. */
969 sect_offset offset;
970 } d;
971
972 /* If HAS_PC_INFO, the PC range associated with this DIE. */
973 CORE_ADDR lowpc;
974 CORE_ADDR highpc;
975
976 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
977 DW_AT_sibling, if any. */
978 /* NOTE: This member isn't strictly necessary, read_partial_die could
979 return DW_AT_sibling values to its caller load_partial_dies. */
980 gdb_byte *sibling;
981
982 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
983 DW_AT_specification (or DW_AT_abstract_origin or
984 DW_AT_extension). */
985 sect_offset spec_offset;
986
987 /* Pointers to this DIE's parent, first child, and next sibling,
988 if any. */
989 struct partial_die_info *die_parent, *die_child, *die_sibling;
990 };
991
992 /* This data structure holds the information of an abbrev. */
993 struct abbrev_info
994 {
995 unsigned int number; /* number identifying abbrev */
996 enum dwarf_tag tag; /* dwarf tag */
997 unsigned short has_children; /* boolean */
998 unsigned short num_attrs; /* number of attributes */
999 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1000 struct abbrev_info *next; /* next in chain */
1001 };
1002
1003 struct attr_abbrev
1004 {
1005 ENUM_BITFIELD(dwarf_attribute) name : 16;
1006 ENUM_BITFIELD(dwarf_form) form : 16;
1007 };
1008
1009 /* Size of abbrev_table.abbrev_hash_table. */
1010 #define ABBREV_HASH_SIZE 121
1011
1012 /* Top level data structure to contain an abbreviation table. */
1013
1014 struct abbrev_table
1015 {
1016 /* Where the abbrev table came from.
1017 This is used as a sanity check when the table is used. */
1018 sect_offset offset;
1019
1020 /* Storage for the abbrev table. */
1021 struct obstack abbrev_obstack;
1022
1023 /* Hash table of abbrevs.
1024 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1025 It could be statically allocated, but the previous code didn't so we
1026 don't either. */
1027 struct abbrev_info **abbrevs;
1028 };
1029
1030 /* Attributes have a name and a value. */
1031 struct attribute
1032 {
1033 ENUM_BITFIELD(dwarf_attribute) name : 16;
1034 ENUM_BITFIELD(dwarf_form) form : 15;
1035
1036 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1037 field should be in u.str (existing only for DW_STRING) but it is kept
1038 here for better struct attribute alignment. */
1039 unsigned int string_is_canonical : 1;
1040
1041 union
1042 {
1043 const char *str;
1044 struct dwarf_block *blk;
1045 ULONGEST unsnd;
1046 LONGEST snd;
1047 CORE_ADDR addr;
1048 struct signatured_type *signatured_type;
1049 }
1050 u;
1051 };
1052
1053 /* This data structure holds a complete die structure. */
1054 struct die_info
1055 {
1056 /* DWARF-2 tag for this DIE. */
1057 ENUM_BITFIELD(dwarf_tag) tag : 16;
1058
1059 /* Number of attributes */
1060 unsigned char num_attrs;
1061
1062 /* True if we're presently building the full type name for the
1063 type derived from this DIE. */
1064 unsigned char building_fullname : 1;
1065
1066 /* Abbrev number */
1067 unsigned int abbrev;
1068
1069 /* Offset in .debug_info or .debug_types section. */
1070 sect_offset offset;
1071
1072 /* The dies in a compilation unit form an n-ary tree. PARENT
1073 points to this die's parent; CHILD points to the first child of
1074 this node; and all the children of a given node are chained
1075 together via their SIBLING fields. */
1076 struct die_info *child; /* Its first child, if any. */
1077 struct die_info *sibling; /* Its next sibling, if any. */
1078 struct die_info *parent; /* Its parent, if any. */
1079
1080 /* An array of attributes, with NUM_ATTRS elements. There may be
1081 zero, but it's not common and zero-sized arrays are not
1082 sufficiently portable C. */
1083 struct attribute attrs[1];
1084 };
1085
1086 /* Get at parts of an attribute structure. */
1087
1088 #define DW_STRING(attr) ((attr)->u.str)
1089 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1090 #define DW_UNSND(attr) ((attr)->u.unsnd)
1091 #define DW_BLOCK(attr) ((attr)->u.blk)
1092 #define DW_SND(attr) ((attr)->u.snd)
1093 #define DW_ADDR(attr) ((attr)->u.addr)
1094 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
1095
1096 /* Blocks are a bunch of untyped bytes. */
1097 struct dwarf_block
1098 {
1099 size_t size;
1100
1101 /* Valid only if SIZE is not zero. */
1102 gdb_byte *data;
1103 };
1104
1105 #ifndef ATTR_ALLOC_CHUNK
1106 #define ATTR_ALLOC_CHUNK 4
1107 #endif
1108
1109 /* Allocate fields for structs, unions and enums in this size. */
1110 #ifndef DW_FIELD_ALLOC_CHUNK
1111 #define DW_FIELD_ALLOC_CHUNK 4
1112 #endif
1113
1114 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1115 but this would require a corresponding change in unpack_field_as_long
1116 and friends. */
1117 static int bits_per_byte = 8;
1118
1119 /* The routines that read and process dies for a C struct or C++ class
1120 pass lists of data member fields and lists of member function fields
1121 in an instance of a field_info structure, as defined below. */
1122 struct field_info
1123 {
1124 /* List of data member and baseclasses fields. */
1125 struct nextfield
1126 {
1127 struct nextfield *next;
1128 int accessibility;
1129 int virtuality;
1130 struct field field;
1131 }
1132 *fields, *baseclasses;
1133
1134 /* Number of fields (including baseclasses). */
1135 int nfields;
1136
1137 /* Number of baseclasses. */
1138 int nbaseclasses;
1139
1140 /* Set if the accesibility of one of the fields is not public. */
1141 int non_public_fields;
1142
1143 /* Member function fields array, entries are allocated in the order they
1144 are encountered in the object file. */
1145 struct nextfnfield
1146 {
1147 struct nextfnfield *next;
1148 struct fn_field fnfield;
1149 }
1150 *fnfields;
1151
1152 /* Member function fieldlist array, contains name of possibly overloaded
1153 member function, number of overloaded member functions and a pointer
1154 to the head of the member function field chain. */
1155 struct fnfieldlist
1156 {
1157 const char *name;
1158 int length;
1159 struct nextfnfield *head;
1160 }
1161 *fnfieldlists;
1162
1163 /* Number of entries in the fnfieldlists array. */
1164 int nfnfields;
1165
1166 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1167 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1168 struct typedef_field_list
1169 {
1170 struct typedef_field field;
1171 struct typedef_field_list *next;
1172 }
1173 *typedef_field_list;
1174 unsigned typedef_field_list_count;
1175 };
1176
1177 /* One item on the queue of compilation units to read in full symbols
1178 for. */
1179 struct dwarf2_queue_item
1180 {
1181 struct dwarf2_per_cu_data *per_cu;
1182 enum language pretend_language;
1183 struct dwarf2_queue_item *next;
1184 };
1185
1186 /* The current queue. */
1187 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1188
1189 /* Loaded secondary compilation units are kept in memory until they
1190 have not been referenced for the processing of this many
1191 compilation units. Set this to zero to disable caching. Cache
1192 sizes of up to at least twenty will improve startup time for
1193 typical inter-CU-reference binaries, at an obvious memory cost. */
1194 static int dwarf2_max_cache_age = 5;
1195 static void
1196 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
1197 struct cmd_list_element *c, const char *value)
1198 {
1199 fprintf_filtered (file, _("The upper bound on the age of cached "
1200 "dwarf2 compilation units is %s.\n"),
1201 value);
1202 }
1203
1204
1205 /* Various complaints about symbol reading that don't abort the process. */
1206
1207 static void
1208 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1209 {
1210 complaint (&symfile_complaints,
1211 _("statement list doesn't fit in .debug_line section"));
1212 }
1213
1214 static void
1215 dwarf2_debug_line_missing_file_complaint (void)
1216 {
1217 complaint (&symfile_complaints,
1218 _(".debug_line section has line data without a file"));
1219 }
1220
1221 static void
1222 dwarf2_debug_line_missing_end_sequence_complaint (void)
1223 {
1224 complaint (&symfile_complaints,
1225 _(".debug_line section has line "
1226 "program sequence without an end"));
1227 }
1228
1229 static void
1230 dwarf2_complex_location_expr_complaint (void)
1231 {
1232 complaint (&symfile_complaints, _("location expression too complex"));
1233 }
1234
1235 static void
1236 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1237 int arg3)
1238 {
1239 complaint (&symfile_complaints,
1240 _("const value length mismatch for '%s', got %d, expected %d"),
1241 arg1, arg2, arg3);
1242 }
1243
1244 static void
1245 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1246 {
1247 complaint (&symfile_complaints,
1248 _("debug info runs off end of %s section"
1249 " [in module %s]"),
1250 section->asection->name,
1251 bfd_get_filename (section->asection->owner));
1252 }
1253
1254 static void
1255 dwarf2_macro_malformed_definition_complaint (const char *arg1)
1256 {
1257 complaint (&symfile_complaints,
1258 _("macro debug info contains a "
1259 "malformed macro definition:\n`%s'"),
1260 arg1);
1261 }
1262
1263 static void
1264 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1265 {
1266 complaint (&symfile_complaints,
1267 _("invalid attribute class or form for '%s' in '%s'"),
1268 arg1, arg2);
1269 }
1270
1271 /* local function prototypes */
1272
1273 static void dwarf2_locate_sections (bfd *, asection *, void *);
1274
1275 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
1276 struct objfile *);
1277
1278 static void dwarf2_find_base_address (struct die_info *die,
1279 struct dwarf2_cu *cu);
1280
1281 static struct partial_symtab *create_partial_symtab
1282 (struct dwarf2_per_cu_data *per_cu, const char *name);
1283
1284 static void dwarf2_build_psymtabs_hard (struct objfile *);
1285
1286 static void scan_partial_symbols (struct partial_die_info *,
1287 CORE_ADDR *, CORE_ADDR *,
1288 int, struct dwarf2_cu *);
1289
1290 static void add_partial_symbol (struct partial_die_info *,
1291 struct dwarf2_cu *);
1292
1293 static void add_partial_namespace (struct partial_die_info *pdi,
1294 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1295 int need_pc, struct dwarf2_cu *cu);
1296
1297 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1298 CORE_ADDR *highpc, int need_pc,
1299 struct dwarf2_cu *cu);
1300
1301 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1302 struct dwarf2_cu *cu);
1303
1304 static void add_partial_subprogram (struct partial_die_info *pdi,
1305 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1306 int need_pc, struct dwarf2_cu *cu);
1307
1308 static void dwarf2_read_symtab (struct partial_symtab *,
1309 struct objfile *);
1310
1311 static void psymtab_to_symtab_1 (struct partial_symtab *);
1312
1313 static struct abbrev_info *abbrev_table_lookup_abbrev
1314 (const struct abbrev_table *, unsigned int);
1315
1316 static struct abbrev_table *abbrev_table_read_table
1317 (struct dwarf2_section_info *, sect_offset);
1318
1319 static void abbrev_table_free (struct abbrev_table *);
1320
1321 static void abbrev_table_free_cleanup (void *);
1322
1323 static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1324 struct dwarf2_section_info *);
1325
1326 static void dwarf2_free_abbrev_table (void *);
1327
1328 static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1329
1330 static struct partial_die_info *load_partial_dies
1331 (const struct die_reader_specs *, gdb_byte *, int);
1332
1333 static gdb_byte *read_partial_die (const struct die_reader_specs *,
1334 struct partial_die_info *,
1335 struct abbrev_info *,
1336 unsigned int,
1337 gdb_byte *);
1338
1339 static struct partial_die_info *find_partial_die (sect_offset, int,
1340 struct dwarf2_cu *);
1341
1342 static void fixup_partial_die (struct partial_die_info *,
1343 struct dwarf2_cu *);
1344
1345 static gdb_byte *read_attribute (const struct die_reader_specs *,
1346 struct attribute *, struct attr_abbrev *,
1347 gdb_byte *);
1348
1349 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1350
1351 static int read_1_signed_byte (bfd *, const gdb_byte *);
1352
1353 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1354
1355 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1356
1357 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1358
1359 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
1360 unsigned int *);
1361
1362 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1363
1364 static LONGEST read_checked_initial_length_and_offset
1365 (bfd *, gdb_byte *, const struct comp_unit_head *,
1366 unsigned int *, unsigned int *);
1367
1368 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
1369 unsigned int *);
1370
1371 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
1372
1373 static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1374 sect_offset);
1375
1376 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
1377
1378 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
1379
1380 static char *read_indirect_string (bfd *, gdb_byte *,
1381 const struct comp_unit_head *,
1382 unsigned int *);
1383
1384 static char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1385
1386 static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
1387
1388 static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
1389
1390 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1391 unsigned int *);
1392
1393 static char *read_str_index (const struct die_reader_specs *reader,
1394 struct dwarf2_cu *cu, ULONGEST str_index);
1395
1396 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1397
1398 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1399 struct dwarf2_cu *);
1400
1401 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1402 unsigned int);
1403
1404 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1405 struct dwarf2_cu *cu);
1406
1407 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1408
1409 static struct die_info *die_specification (struct die_info *die,
1410 struct dwarf2_cu **);
1411
1412 static void free_line_header (struct line_header *lh);
1413
1414 static void add_file_name (struct line_header *, char *, unsigned int,
1415 unsigned int, unsigned int);
1416
1417 static struct line_header *dwarf_decode_line_header (unsigned int offset,
1418 struct dwarf2_cu *cu);
1419
1420 static void dwarf_decode_lines (struct line_header *, const char *,
1421 struct dwarf2_cu *, struct partial_symtab *,
1422 int);
1423
1424 static void dwarf2_start_subfile (char *, const char *, const char *);
1425
1426 static void dwarf2_start_symtab (struct dwarf2_cu *,
1427 const char *, const char *, CORE_ADDR);
1428
1429 static struct symbol *new_symbol (struct die_info *, struct type *,
1430 struct dwarf2_cu *);
1431
1432 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1433 struct dwarf2_cu *, struct symbol *);
1434
1435 static void dwarf2_const_value (struct attribute *, struct symbol *,
1436 struct dwarf2_cu *);
1437
1438 static void dwarf2_const_value_attr (struct attribute *attr,
1439 struct type *type,
1440 const char *name,
1441 struct obstack *obstack,
1442 struct dwarf2_cu *cu, LONGEST *value,
1443 gdb_byte **bytes,
1444 struct dwarf2_locexpr_baton **baton);
1445
1446 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1447
1448 static int need_gnat_info (struct dwarf2_cu *);
1449
1450 static struct type *die_descriptive_type (struct die_info *,
1451 struct dwarf2_cu *);
1452
1453 static void set_descriptive_type (struct type *, struct die_info *,
1454 struct dwarf2_cu *);
1455
1456 static struct type *die_containing_type (struct die_info *,
1457 struct dwarf2_cu *);
1458
1459 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1460 struct dwarf2_cu *);
1461
1462 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1463
1464 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1465
1466 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1467
1468 static char *typename_concat (struct obstack *obs, const char *prefix,
1469 const char *suffix, int physname,
1470 struct dwarf2_cu *cu);
1471
1472 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1473
1474 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1475
1476 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1477
1478 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1479
1480 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1481
1482 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1483 struct dwarf2_cu *, struct partial_symtab *);
1484
1485 static int dwarf2_get_pc_bounds (struct die_info *,
1486 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1487 struct partial_symtab *);
1488
1489 static void get_scope_pc_bounds (struct die_info *,
1490 CORE_ADDR *, CORE_ADDR *,
1491 struct dwarf2_cu *);
1492
1493 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1494 CORE_ADDR, struct dwarf2_cu *);
1495
1496 static void dwarf2_add_field (struct field_info *, struct die_info *,
1497 struct dwarf2_cu *);
1498
1499 static void dwarf2_attach_fields_to_type (struct field_info *,
1500 struct type *, struct dwarf2_cu *);
1501
1502 static void dwarf2_add_member_fn (struct field_info *,
1503 struct die_info *, struct type *,
1504 struct dwarf2_cu *);
1505
1506 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1507 struct type *,
1508 struct dwarf2_cu *);
1509
1510 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1511
1512 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1513
1514 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1515
1516 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1517
1518 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1519
1520 static struct type *read_module_type (struct die_info *die,
1521 struct dwarf2_cu *cu);
1522
1523 static const char *namespace_name (struct die_info *die,
1524 int *is_anonymous, struct dwarf2_cu *);
1525
1526 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1527
1528 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1529
1530 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1531 struct dwarf2_cu *);
1532
1533 static struct die_info *read_die_and_siblings_1
1534 (const struct die_reader_specs *, gdb_byte *, gdb_byte **,
1535 struct die_info *);
1536
1537 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1538 gdb_byte *info_ptr,
1539 gdb_byte **new_info_ptr,
1540 struct die_info *parent);
1541
1542 static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1543 struct die_info **, gdb_byte *, int *, int);
1544
1545 static gdb_byte *read_full_die (const struct die_reader_specs *,
1546 struct die_info **, gdb_byte *, int *);
1547
1548 static void process_die (struct die_info *, struct dwarf2_cu *);
1549
1550 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1551 struct obstack *);
1552
1553 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1554
1555 static const char *dwarf2_full_name (const char *name,
1556 struct die_info *die,
1557 struct dwarf2_cu *cu);
1558
1559 static const char *dwarf2_physname (const char *name, struct die_info *die,
1560 struct dwarf2_cu *cu);
1561
1562 static struct die_info *dwarf2_extension (struct die_info *die,
1563 struct dwarf2_cu **);
1564
1565 static const char *dwarf_tag_name (unsigned int);
1566
1567 static const char *dwarf_attr_name (unsigned int);
1568
1569 static const char *dwarf_form_name (unsigned int);
1570
1571 static char *dwarf_bool_name (unsigned int);
1572
1573 static const char *dwarf_type_encoding_name (unsigned int);
1574
1575 static struct die_info *sibling_die (struct die_info *);
1576
1577 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1578
1579 static void dump_die_for_error (struct die_info *);
1580
1581 static void dump_die_1 (struct ui_file *, int level, int max_level,
1582 struct die_info *);
1583
1584 /*static*/ void dump_die (struct die_info *, int max_level);
1585
1586 static void store_in_ref_table (struct die_info *,
1587 struct dwarf2_cu *);
1588
1589 static int is_ref_attr (struct attribute *);
1590
1591 static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
1592
1593 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1594
1595 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1596 struct attribute *,
1597 struct dwarf2_cu **);
1598
1599 static struct die_info *follow_die_ref (struct die_info *,
1600 struct attribute *,
1601 struct dwarf2_cu **);
1602
1603 static struct die_info *follow_die_sig (struct die_info *,
1604 struct attribute *,
1605 struct dwarf2_cu **);
1606
1607 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1608
1609 static void read_signatured_type (struct signatured_type *);
1610
1611 static struct type_unit_group *get_type_unit_group
1612 (struct dwarf2_cu *, struct attribute *);
1613
1614 static void build_type_unit_groups (die_reader_func_ftype *, void *);
1615
1616 /* memory allocation interface */
1617
1618 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1619
1620 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1621
1622 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
1623 const char *, int);
1624
1625 static int attr_form_is_block (struct attribute *);
1626
1627 static int attr_form_is_section_offset (struct attribute *);
1628
1629 static int attr_form_is_constant (struct attribute *);
1630
1631 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1632 struct dwarf2_loclist_baton *baton,
1633 struct attribute *attr);
1634
1635 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1636 struct symbol *sym,
1637 struct dwarf2_cu *cu,
1638 int is_block);
1639
1640 static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1641 gdb_byte *info_ptr,
1642 struct abbrev_info *abbrev);
1643
1644 static void free_stack_comp_unit (void *);
1645
1646 static hashval_t partial_die_hash (const void *item);
1647
1648 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1649
1650 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1651 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
1652
1653 static void init_one_comp_unit (struct dwarf2_cu *cu,
1654 struct dwarf2_per_cu_data *per_cu);
1655
1656 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1657 struct die_info *comp_unit_die,
1658 enum language pretend_language);
1659
1660 static void free_heap_comp_unit (void *);
1661
1662 static void free_cached_comp_units (void *);
1663
1664 static void age_cached_comp_units (void);
1665
1666 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1667
1668 static struct type *set_die_type (struct die_info *, struct type *,
1669 struct dwarf2_cu *);
1670
1671 static void create_all_comp_units (struct objfile *);
1672
1673 static int create_all_type_units (struct objfile *);
1674
1675 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1676 enum language);
1677
1678 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1679 enum language);
1680
1681 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1682 enum language);
1683
1684 static void dwarf2_add_dependence (struct dwarf2_cu *,
1685 struct dwarf2_per_cu_data *);
1686
1687 static void dwarf2_mark (struct dwarf2_cu *);
1688
1689 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1690
1691 static struct type *get_die_type_at_offset (sect_offset,
1692 struct dwarf2_per_cu_data *per_cu);
1693
1694 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1695
1696 static void dwarf2_release_queue (void *dummy);
1697
1698 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1699 enum language pretend_language);
1700
1701 static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1702 struct dwarf2_per_cu_data *per_cu,
1703 enum language pretend_language);
1704
1705 static void process_queue (void);
1706
1707 static void find_file_and_directory (struct die_info *die,
1708 struct dwarf2_cu *cu,
1709 const char **name, const char **comp_dir);
1710
1711 static char *file_full_name (int file, struct line_header *lh,
1712 const char *comp_dir);
1713
1714 static gdb_byte *read_and_check_comp_unit_head
1715 (struct comp_unit_head *header,
1716 struct dwarf2_section_info *section,
1717 struct dwarf2_section_info *abbrev_section, gdb_byte *info_ptr,
1718 int is_debug_types_section);
1719
1720 static void init_cutu_and_read_dies
1721 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1722 int use_existing_cu, int keep,
1723 die_reader_func_ftype *die_reader_func, void *data);
1724
1725 static void init_cutu_and_read_dies_simple
1726 (struct dwarf2_per_cu_data *this_cu,
1727 die_reader_func_ftype *die_reader_func, void *data);
1728
1729 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1730
1731 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1732
1733 static struct dwo_unit *lookup_dwo_comp_unit
1734 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1735
1736 static struct dwo_unit *lookup_dwo_type_unit
1737 (struct signatured_type *, const char *, const char *);
1738
1739 static void free_dwo_file_cleanup (void *);
1740
1741 static void process_cu_includes (void);
1742
1743 static void check_producer (struct dwarf2_cu *cu);
1744
1745 #if WORDS_BIGENDIAN
1746
1747 /* Convert VALUE between big- and little-endian. */
1748 static offset_type
1749 byte_swap (offset_type value)
1750 {
1751 offset_type result;
1752
1753 result = (value & 0xff) << 24;
1754 result |= (value & 0xff00) << 8;
1755 result |= (value & 0xff0000) >> 8;
1756 result |= (value & 0xff000000) >> 24;
1757 return result;
1758 }
1759
1760 #define MAYBE_SWAP(V) byte_swap (V)
1761
1762 #else
1763 #define MAYBE_SWAP(V) (V)
1764 #endif /* WORDS_BIGENDIAN */
1765
1766 /* The suffix for an index file. */
1767 #define INDEX_SUFFIX ".gdb-index"
1768
1769 /* Try to locate the sections we need for DWARF 2 debugging
1770 information and return true if we have enough to do something.
1771 NAMES points to the dwarf2 section names, or is NULL if the standard
1772 ELF names are used. */
1773
1774 int
1775 dwarf2_has_info (struct objfile *objfile,
1776 const struct dwarf2_debug_sections *names)
1777 {
1778 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1779 if (!dwarf2_per_objfile)
1780 {
1781 /* Initialize per-objfile state. */
1782 struct dwarf2_per_objfile *data
1783 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1784
1785 memset (data, 0, sizeof (*data));
1786 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1787 dwarf2_per_objfile = data;
1788
1789 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1790 (void *) names);
1791 dwarf2_per_objfile->objfile = objfile;
1792 }
1793 return (dwarf2_per_objfile->info.asection != NULL
1794 && dwarf2_per_objfile->abbrev.asection != NULL);
1795 }
1796
1797 /* When loading sections, we look either for uncompressed section or for
1798 compressed section names. */
1799
1800 static int
1801 section_is_p (const char *section_name,
1802 const struct dwarf2_section_names *names)
1803 {
1804 if (names->normal != NULL
1805 && strcmp (section_name, names->normal) == 0)
1806 return 1;
1807 if (names->compressed != NULL
1808 && strcmp (section_name, names->compressed) == 0)
1809 return 1;
1810 return 0;
1811 }
1812
1813 /* This function is mapped across the sections and remembers the
1814 offset and size of each of the debugging sections we are interested
1815 in. */
1816
1817 static void
1818 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1819 {
1820 const struct dwarf2_debug_sections *names;
1821 flagword aflag = bfd_get_section_flags (abfd, sectp);
1822
1823 if (vnames == NULL)
1824 names = &dwarf2_elf_names;
1825 else
1826 names = (const struct dwarf2_debug_sections *) vnames;
1827
1828 if ((aflag & SEC_HAS_CONTENTS) == 0)
1829 {
1830 }
1831 else if (section_is_p (sectp->name, &names->info))
1832 {
1833 dwarf2_per_objfile->info.asection = sectp;
1834 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1835 }
1836 else if (section_is_p (sectp->name, &names->abbrev))
1837 {
1838 dwarf2_per_objfile->abbrev.asection = sectp;
1839 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1840 }
1841 else if (section_is_p (sectp->name, &names->line))
1842 {
1843 dwarf2_per_objfile->line.asection = sectp;
1844 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1845 }
1846 else if (section_is_p (sectp->name, &names->loc))
1847 {
1848 dwarf2_per_objfile->loc.asection = sectp;
1849 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1850 }
1851 else if (section_is_p (sectp->name, &names->macinfo))
1852 {
1853 dwarf2_per_objfile->macinfo.asection = sectp;
1854 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1855 }
1856 else if (section_is_p (sectp->name, &names->macro))
1857 {
1858 dwarf2_per_objfile->macro.asection = sectp;
1859 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1860 }
1861 else if (section_is_p (sectp->name, &names->str))
1862 {
1863 dwarf2_per_objfile->str.asection = sectp;
1864 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1865 }
1866 else if (section_is_p (sectp->name, &names->addr))
1867 {
1868 dwarf2_per_objfile->addr.asection = sectp;
1869 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1870 }
1871 else if (section_is_p (sectp->name, &names->frame))
1872 {
1873 dwarf2_per_objfile->frame.asection = sectp;
1874 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1875 }
1876 else if (section_is_p (sectp->name, &names->eh_frame))
1877 {
1878 dwarf2_per_objfile->eh_frame.asection = sectp;
1879 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1880 }
1881 else if (section_is_p (sectp->name, &names->ranges))
1882 {
1883 dwarf2_per_objfile->ranges.asection = sectp;
1884 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1885 }
1886 else if (section_is_p (sectp->name, &names->types))
1887 {
1888 struct dwarf2_section_info type_section;
1889
1890 memset (&type_section, 0, sizeof (type_section));
1891 type_section.asection = sectp;
1892 type_section.size = bfd_get_section_size (sectp);
1893
1894 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1895 &type_section);
1896 }
1897 else if (section_is_p (sectp->name, &names->gdb_index))
1898 {
1899 dwarf2_per_objfile->gdb_index.asection = sectp;
1900 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1901 }
1902
1903 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1904 && bfd_section_vma (abfd, sectp) == 0)
1905 dwarf2_per_objfile->has_section_at_zero = 1;
1906 }
1907
1908 /* A helper function that decides whether a section is empty,
1909 or not present. */
1910
1911 static int
1912 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1913 {
1914 return info->asection == NULL || info->size == 0;
1915 }
1916
1917 /* Read the contents of the section INFO.
1918 OBJFILE is the main object file, but not necessarily the file where
1919 the section comes from. E.g., for DWO files INFO->asection->owner
1920 is the bfd of the DWO file.
1921 If the section is compressed, uncompress it before returning. */
1922
1923 static void
1924 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1925 {
1926 asection *sectp = info->asection;
1927 bfd *abfd;
1928 gdb_byte *buf, *retbuf;
1929 unsigned char header[4];
1930
1931 if (info->readin)
1932 return;
1933 info->buffer = NULL;
1934 info->readin = 1;
1935
1936 if (dwarf2_section_empty_p (info))
1937 return;
1938
1939 abfd = sectp->owner;
1940
1941 /* If the section has relocations, we must read it ourselves.
1942 Otherwise we attach it to the BFD. */
1943 if ((sectp->flags & SEC_RELOC) == 0)
1944 {
1945 const gdb_byte *bytes = gdb_bfd_map_section (sectp, &info->size);
1946
1947 /* We have to cast away const here for historical reasons.
1948 Fixing dwarf2read to be const-correct would be quite nice. */
1949 info->buffer = (gdb_byte *) bytes;
1950 return;
1951 }
1952
1953 buf = obstack_alloc (&objfile->objfile_obstack, info->size);
1954 info->buffer = buf;
1955
1956 /* When debugging .o files, we may need to apply relocations; see
1957 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1958 We never compress sections in .o files, so we only need to
1959 try this when the section is not compressed. */
1960 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1961 if (retbuf != NULL)
1962 {
1963 info->buffer = retbuf;
1964 return;
1965 }
1966
1967 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1968 || bfd_bread (buf, info->size, abfd) != info->size)
1969 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1970 bfd_get_filename (abfd));
1971 }
1972
1973 /* A helper function that returns the size of a section in a safe way.
1974 If you are positive that the section has been read before using the
1975 size, then it is safe to refer to the dwarf2_section_info object's
1976 "size" field directly. In other cases, you must call this
1977 function, because for compressed sections the size field is not set
1978 correctly until the section has been read. */
1979
1980 static bfd_size_type
1981 dwarf2_section_size (struct objfile *objfile,
1982 struct dwarf2_section_info *info)
1983 {
1984 if (!info->readin)
1985 dwarf2_read_section (objfile, info);
1986 return info->size;
1987 }
1988
1989 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1990 SECTION_NAME. */
1991
1992 void
1993 dwarf2_get_section_info (struct objfile *objfile,
1994 enum dwarf2_section_enum sect,
1995 asection **sectp, gdb_byte **bufp,
1996 bfd_size_type *sizep)
1997 {
1998 struct dwarf2_per_objfile *data
1999 = objfile_data (objfile, dwarf2_objfile_data_key);
2000 struct dwarf2_section_info *info;
2001
2002 /* We may see an objfile without any DWARF, in which case we just
2003 return nothing. */
2004 if (data == NULL)
2005 {
2006 *sectp = NULL;
2007 *bufp = NULL;
2008 *sizep = 0;
2009 return;
2010 }
2011 switch (sect)
2012 {
2013 case DWARF2_DEBUG_FRAME:
2014 info = &data->frame;
2015 break;
2016 case DWARF2_EH_FRAME:
2017 info = &data->eh_frame;
2018 break;
2019 default:
2020 gdb_assert_not_reached ("unexpected section");
2021 }
2022
2023 dwarf2_read_section (objfile, info);
2024
2025 *sectp = info->asection;
2026 *bufp = info->buffer;
2027 *sizep = info->size;
2028 }
2029
2030 /* A helper function to find the sections for a .dwz file. */
2031
2032 static void
2033 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2034 {
2035 struct dwz_file *dwz_file = arg;
2036
2037 /* Note that we only support the standard ELF names, because .dwz
2038 is ELF-only (at the time of writing). */
2039 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2040 {
2041 dwz_file->abbrev.asection = sectp;
2042 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2043 }
2044 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2045 {
2046 dwz_file->info.asection = sectp;
2047 dwz_file->info.size = bfd_get_section_size (sectp);
2048 }
2049 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2050 {
2051 dwz_file->str.asection = sectp;
2052 dwz_file->str.size = bfd_get_section_size (sectp);
2053 }
2054 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2055 {
2056 dwz_file->line.asection = sectp;
2057 dwz_file->line.size = bfd_get_section_size (sectp);
2058 }
2059 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2060 {
2061 dwz_file->macro.asection = sectp;
2062 dwz_file->macro.size = bfd_get_section_size (sectp);
2063 }
2064 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2065 {
2066 dwz_file->gdb_index.asection = sectp;
2067 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2068 }
2069 }
2070
2071 /* Open the separate '.dwz' debug file, if needed. Error if the file
2072 cannot be found. */
2073
2074 static struct dwz_file *
2075 dwarf2_get_dwz_file (void)
2076 {
2077 bfd *abfd, *dwz_bfd;
2078 asection *section;
2079 gdb_byte *data;
2080 struct cleanup *cleanup;
2081 const char *filename;
2082 struct dwz_file *result;
2083
2084 if (dwarf2_per_objfile->dwz_file != NULL)
2085 return dwarf2_per_objfile->dwz_file;
2086
2087 abfd = dwarf2_per_objfile->objfile->obfd;
2088 section = bfd_get_section_by_name (abfd, ".gnu_debugaltlink");
2089 if (section == NULL)
2090 error (_("could not find '.gnu_debugaltlink' section"));
2091 if (!bfd_malloc_and_get_section (abfd, section, &data))
2092 error (_("could not read '.gnu_debugaltlink' section: %s"),
2093 bfd_errmsg (bfd_get_error ()));
2094 cleanup = make_cleanup (xfree, data);
2095
2096 filename = data;
2097 if (!IS_ABSOLUTE_PATH (filename))
2098 {
2099 char *abs = gdb_realpath (dwarf2_per_objfile->objfile->name);
2100 char *rel;
2101
2102 make_cleanup (xfree, abs);
2103 abs = ldirname (abs);
2104 make_cleanup (xfree, abs);
2105
2106 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2107 make_cleanup (xfree, rel);
2108 filename = rel;
2109 }
2110
2111 /* The format is just a NUL-terminated file name, followed by the
2112 build-id. For now, though, we ignore the build-id. */
2113 dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2114 if (dwz_bfd == NULL)
2115 error (_("could not read '%s': %s"), filename,
2116 bfd_errmsg (bfd_get_error ()));
2117
2118 if (!bfd_check_format (dwz_bfd, bfd_object))
2119 {
2120 gdb_bfd_unref (dwz_bfd);
2121 error (_("file '%s' was not usable: %s"), filename,
2122 bfd_errmsg (bfd_get_error ()));
2123 }
2124
2125 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2126 struct dwz_file);
2127 result->dwz_bfd = dwz_bfd;
2128
2129 bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2130
2131 do_cleanups (cleanup);
2132
2133 dwarf2_per_objfile->dwz_file = result;
2134 return result;
2135 }
2136 \f
2137 /* DWARF quick_symbols_functions support. */
2138
2139 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2140 unique line tables, so we maintain a separate table of all .debug_line
2141 derived entries to support the sharing.
2142 All the quick functions need is the list of file names. We discard the
2143 line_header when we're done and don't need to record it here. */
2144 struct quick_file_names
2145 {
2146 /* The data used to construct the hash key. */
2147 struct stmt_list_hash hash;
2148
2149 /* The number of entries in file_names, real_names. */
2150 unsigned int num_file_names;
2151
2152 /* The file names from the line table, after being run through
2153 file_full_name. */
2154 const char **file_names;
2155
2156 /* The file names from the line table after being run through
2157 gdb_realpath. These are computed lazily. */
2158 const char **real_names;
2159 };
2160
2161 /* When using the index (and thus not using psymtabs), each CU has an
2162 object of this type. This is used to hold information needed by
2163 the various "quick" methods. */
2164 struct dwarf2_per_cu_quick_data
2165 {
2166 /* The file table. This can be NULL if there was no file table
2167 or it's currently not read in.
2168 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2169 struct quick_file_names *file_names;
2170
2171 /* The corresponding symbol table. This is NULL if symbols for this
2172 CU have not yet been read. */
2173 struct symtab *symtab;
2174
2175 /* A temporary mark bit used when iterating over all CUs in
2176 expand_symtabs_matching. */
2177 unsigned int mark : 1;
2178
2179 /* True if we've tried to read the file table and found there isn't one.
2180 There will be no point in trying to read it again next time. */
2181 unsigned int no_file_data : 1;
2182 };
2183
2184 /* Utility hash function for a stmt_list_hash. */
2185
2186 static hashval_t
2187 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2188 {
2189 hashval_t v = 0;
2190
2191 if (stmt_list_hash->dwo_unit != NULL)
2192 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2193 v += stmt_list_hash->line_offset.sect_off;
2194 return v;
2195 }
2196
2197 /* Utility equality function for a stmt_list_hash. */
2198
2199 static int
2200 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2201 const struct stmt_list_hash *rhs)
2202 {
2203 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2204 return 0;
2205 if (lhs->dwo_unit != NULL
2206 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2207 return 0;
2208
2209 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2210 }
2211
2212 /* Hash function for a quick_file_names. */
2213
2214 static hashval_t
2215 hash_file_name_entry (const void *e)
2216 {
2217 const struct quick_file_names *file_data = e;
2218
2219 return hash_stmt_list_entry (&file_data->hash);
2220 }
2221
2222 /* Equality function for a quick_file_names. */
2223
2224 static int
2225 eq_file_name_entry (const void *a, const void *b)
2226 {
2227 const struct quick_file_names *ea = a;
2228 const struct quick_file_names *eb = b;
2229
2230 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2231 }
2232
2233 /* Delete function for a quick_file_names. */
2234
2235 static void
2236 delete_file_name_entry (void *e)
2237 {
2238 struct quick_file_names *file_data = e;
2239 int i;
2240
2241 for (i = 0; i < file_data->num_file_names; ++i)
2242 {
2243 xfree ((void*) file_data->file_names[i]);
2244 if (file_data->real_names)
2245 xfree ((void*) file_data->real_names[i]);
2246 }
2247
2248 /* The space for the struct itself lives on objfile_obstack,
2249 so we don't free it here. */
2250 }
2251
2252 /* Create a quick_file_names hash table. */
2253
2254 static htab_t
2255 create_quick_file_names_table (unsigned int nr_initial_entries)
2256 {
2257 return htab_create_alloc (nr_initial_entries,
2258 hash_file_name_entry, eq_file_name_entry,
2259 delete_file_name_entry, xcalloc, xfree);
2260 }
2261
2262 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2263 have to be created afterwards. You should call age_cached_comp_units after
2264 processing PER_CU->CU. dw2_setup must have been already called. */
2265
2266 static void
2267 load_cu (struct dwarf2_per_cu_data *per_cu)
2268 {
2269 if (per_cu->is_debug_types)
2270 load_full_type_unit (per_cu);
2271 else
2272 load_full_comp_unit (per_cu, language_minimal);
2273
2274 gdb_assert (per_cu->cu != NULL);
2275
2276 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2277 }
2278
2279 /* Read in the symbols for PER_CU. */
2280
2281 static void
2282 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2283 {
2284 struct cleanup *back_to;
2285
2286 /* Skip type_unit_groups, reading the type units they contain
2287 is handled elsewhere. */
2288 if (IS_TYPE_UNIT_GROUP (per_cu))
2289 return;
2290
2291 back_to = make_cleanup (dwarf2_release_queue, NULL);
2292
2293 if (dwarf2_per_objfile->using_index
2294 ? per_cu->v.quick->symtab == NULL
2295 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2296 {
2297 queue_comp_unit (per_cu, language_minimal);
2298 load_cu (per_cu);
2299 }
2300
2301 process_queue ();
2302
2303 /* Age the cache, releasing compilation units that have not
2304 been used recently. */
2305 age_cached_comp_units ();
2306
2307 do_cleanups (back_to);
2308 }
2309
2310 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2311 the objfile from which this CU came. Returns the resulting symbol
2312 table. */
2313
2314 static struct symtab *
2315 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2316 {
2317 gdb_assert (dwarf2_per_objfile->using_index);
2318 if (!per_cu->v.quick->symtab)
2319 {
2320 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2321 increment_reading_symtab ();
2322 dw2_do_instantiate_symtab (per_cu);
2323 process_cu_includes ();
2324 do_cleanups (back_to);
2325 }
2326 return per_cu->v.quick->symtab;
2327 }
2328
2329 /* Return the CU given its index.
2330
2331 This is intended for loops like:
2332
2333 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2334 + dwarf2_per_objfile->n_type_units); ++i)
2335 {
2336 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2337
2338 ...;
2339 }
2340 */
2341
2342 static struct dwarf2_per_cu_data *
2343 dw2_get_cu (int index)
2344 {
2345 if (index >= dwarf2_per_objfile->n_comp_units)
2346 {
2347 index -= dwarf2_per_objfile->n_comp_units;
2348 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2349 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
2350 }
2351
2352 return dwarf2_per_objfile->all_comp_units[index];
2353 }
2354
2355 /* Return the primary CU given its index.
2356 The difference between this function and dw2_get_cu is in the handling
2357 of type units (TUs). Here we return the type_unit_group object.
2358
2359 This is intended for loops like:
2360
2361 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2362 + dwarf2_per_objfile->n_type_unit_groups); ++i)
2363 {
2364 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2365
2366 ...;
2367 }
2368 */
2369
2370 static struct dwarf2_per_cu_data *
2371 dw2_get_primary_cu (int index)
2372 {
2373 if (index >= dwarf2_per_objfile->n_comp_units)
2374 {
2375 index -= dwarf2_per_objfile->n_comp_units;
2376 gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2377 return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
2378 }
2379
2380 return dwarf2_per_objfile->all_comp_units[index];
2381 }
2382
2383 /* A helper for create_cus_from_index that handles a given list of
2384 CUs. */
2385
2386 static void
2387 create_cus_from_index_list (struct objfile *objfile,
2388 const gdb_byte *cu_list, offset_type n_elements,
2389 struct dwarf2_section_info *section,
2390 int is_dwz,
2391 int base_offset)
2392 {
2393 offset_type i;
2394
2395 for (i = 0; i < n_elements; i += 2)
2396 {
2397 struct dwarf2_per_cu_data *the_cu;
2398 ULONGEST offset, length;
2399
2400 gdb_static_assert (sizeof (ULONGEST) >= 8);
2401 offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2402 length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2403 cu_list += 2 * 8;
2404
2405 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2406 struct dwarf2_per_cu_data);
2407 the_cu->offset.sect_off = offset;
2408 the_cu->length = length;
2409 the_cu->objfile = objfile;
2410 the_cu->section = section;
2411 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2412 struct dwarf2_per_cu_quick_data);
2413 the_cu->is_dwz = is_dwz;
2414 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
2415 }
2416 }
2417
2418 /* Read the CU list from the mapped index, and use it to create all
2419 the CU objects for this objfile. */
2420
2421 static void
2422 create_cus_from_index (struct objfile *objfile,
2423 const gdb_byte *cu_list, offset_type cu_list_elements,
2424 const gdb_byte *dwz_list, offset_type dwz_elements)
2425 {
2426 struct dwz_file *dwz;
2427
2428 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2429 dwarf2_per_objfile->all_comp_units
2430 = obstack_alloc (&objfile->objfile_obstack,
2431 dwarf2_per_objfile->n_comp_units
2432 * sizeof (struct dwarf2_per_cu_data *));
2433
2434 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2435 &dwarf2_per_objfile->info, 0, 0);
2436
2437 if (dwz_elements == 0)
2438 return;
2439
2440 dwz = dwarf2_get_dwz_file ();
2441 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2442 cu_list_elements / 2);
2443 }
2444
2445 /* Create the signatured type hash table from the index. */
2446
2447 static void
2448 create_signatured_type_table_from_index (struct objfile *objfile,
2449 struct dwarf2_section_info *section,
2450 const gdb_byte *bytes,
2451 offset_type elements)
2452 {
2453 offset_type i;
2454 htab_t sig_types_hash;
2455
2456 dwarf2_per_objfile->n_type_units = elements / 3;
2457 dwarf2_per_objfile->all_type_units
2458 = obstack_alloc (&objfile->objfile_obstack,
2459 dwarf2_per_objfile->n_type_units
2460 * sizeof (struct signatured_type *));
2461
2462 sig_types_hash = allocate_signatured_type_table (objfile);
2463
2464 for (i = 0; i < elements; i += 3)
2465 {
2466 struct signatured_type *sig_type;
2467 ULONGEST offset, type_offset_in_tu, signature;
2468 void **slot;
2469
2470 gdb_static_assert (sizeof (ULONGEST) >= 8);
2471 offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2472 type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
2473 BFD_ENDIAN_LITTLE);
2474 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2475 bytes += 3 * 8;
2476
2477 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2478 struct signatured_type);
2479 sig_type->signature = signature;
2480 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2481 sig_type->per_cu.is_debug_types = 1;
2482 sig_type->per_cu.section = section;
2483 sig_type->per_cu.offset.sect_off = offset;
2484 sig_type->per_cu.objfile = objfile;
2485 sig_type->per_cu.v.quick
2486 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2487 struct dwarf2_per_cu_quick_data);
2488
2489 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2490 *slot = sig_type;
2491
2492 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
2493 }
2494
2495 dwarf2_per_objfile->signatured_types = sig_types_hash;
2496 }
2497
2498 /* Read the address map data from the mapped index, and use it to
2499 populate the objfile's psymtabs_addrmap. */
2500
2501 static void
2502 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2503 {
2504 const gdb_byte *iter, *end;
2505 struct obstack temp_obstack;
2506 struct addrmap *mutable_map;
2507 struct cleanup *cleanup;
2508 CORE_ADDR baseaddr;
2509
2510 obstack_init (&temp_obstack);
2511 cleanup = make_cleanup_obstack_free (&temp_obstack);
2512 mutable_map = addrmap_create_mutable (&temp_obstack);
2513
2514 iter = index->address_table;
2515 end = iter + index->address_table_size;
2516
2517 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2518
2519 while (iter < end)
2520 {
2521 ULONGEST hi, lo, cu_index;
2522 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2523 iter += 8;
2524 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2525 iter += 8;
2526 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2527 iter += 4;
2528
2529 if (cu_index < dwarf2_per_objfile->n_comp_units)
2530 {
2531 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2532 dw2_get_cu (cu_index));
2533 }
2534 else
2535 {
2536 complaint (&symfile_complaints,
2537 _(".gdb_index address table has invalid CU number %u"),
2538 (unsigned) cu_index);
2539 }
2540 }
2541
2542 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2543 &objfile->objfile_obstack);
2544 do_cleanups (cleanup);
2545 }
2546
2547 /* The hash function for strings in the mapped index. This is the same as
2548 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2549 implementation. This is necessary because the hash function is tied to the
2550 format of the mapped index file. The hash values do not have to match with
2551 SYMBOL_HASH_NEXT.
2552
2553 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2554
2555 static hashval_t
2556 mapped_index_string_hash (int index_version, const void *p)
2557 {
2558 const unsigned char *str = (const unsigned char *) p;
2559 hashval_t r = 0;
2560 unsigned char c;
2561
2562 while ((c = *str++) != 0)
2563 {
2564 if (index_version >= 5)
2565 c = tolower (c);
2566 r = r * 67 + c - 113;
2567 }
2568
2569 return r;
2570 }
2571
2572 /* Find a slot in the mapped index INDEX for the object named NAME.
2573 If NAME is found, set *VEC_OUT to point to the CU vector in the
2574 constant pool and return 1. If NAME cannot be found, return 0. */
2575
2576 static int
2577 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2578 offset_type **vec_out)
2579 {
2580 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2581 offset_type hash;
2582 offset_type slot, step;
2583 int (*cmp) (const char *, const char *);
2584
2585 if (current_language->la_language == language_cplus
2586 || current_language->la_language == language_java
2587 || current_language->la_language == language_fortran)
2588 {
2589 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2590 not contain any. */
2591 const char *paren = strchr (name, '(');
2592
2593 if (paren)
2594 {
2595 char *dup;
2596
2597 dup = xmalloc (paren - name + 1);
2598 memcpy (dup, name, paren - name);
2599 dup[paren - name] = 0;
2600
2601 make_cleanup (xfree, dup);
2602 name = dup;
2603 }
2604 }
2605
2606 /* Index version 4 did not support case insensitive searches. But the
2607 indices for case insensitive languages are built in lowercase, therefore
2608 simulate our NAME being searched is also lowercased. */
2609 hash = mapped_index_string_hash ((index->version == 4
2610 && case_sensitivity == case_sensitive_off
2611 ? 5 : index->version),
2612 name);
2613
2614 slot = hash & (index->symbol_table_slots - 1);
2615 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2616 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2617
2618 for (;;)
2619 {
2620 /* Convert a slot number to an offset into the table. */
2621 offset_type i = 2 * slot;
2622 const char *str;
2623 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2624 {
2625 do_cleanups (back_to);
2626 return 0;
2627 }
2628
2629 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2630 if (!cmp (name, str))
2631 {
2632 *vec_out = (offset_type *) (index->constant_pool
2633 + MAYBE_SWAP (index->symbol_table[i + 1]));
2634 do_cleanups (back_to);
2635 return 1;
2636 }
2637
2638 slot = (slot + step) & (index->symbol_table_slots - 1);
2639 }
2640 }
2641
2642 /* A helper function that reads the .gdb_index from SECTION and fills
2643 in MAP. FILENAME is the name of the file containing the section;
2644 it is used for error reporting. DEPRECATED_OK is nonzero if it is
2645 ok to use deprecated sections.
2646
2647 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2648 out parameters that are filled in with information about the CU and
2649 TU lists in the section.
2650
2651 Returns 1 if all went well, 0 otherwise. */
2652
2653 static int
2654 read_index_from_section (struct objfile *objfile,
2655 const char *filename,
2656 int deprecated_ok,
2657 struct dwarf2_section_info *section,
2658 struct mapped_index *map,
2659 const gdb_byte **cu_list,
2660 offset_type *cu_list_elements,
2661 const gdb_byte **types_list,
2662 offset_type *types_list_elements)
2663 {
2664 char *addr;
2665 offset_type version;
2666 offset_type *metadata;
2667 int i;
2668
2669 if (dwarf2_section_empty_p (section))
2670 return 0;
2671
2672 /* Older elfutils strip versions could keep the section in the main
2673 executable while splitting it for the separate debug info file. */
2674 if ((bfd_get_file_flags (section->asection) & SEC_HAS_CONTENTS) == 0)
2675 return 0;
2676
2677 dwarf2_read_section (objfile, section);
2678
2679 addr = section->buffer;
2680 /* Version check. */
2681 version = MAYBE_SWAP (*(offset_type *) addr);
2682 /* Versions earlier than 3 emitted every copy of a psymbol. This
2683 causes the index to behave very poorly for certain requests. Version 3
2684 contained incomplete addrmap. So, it seems better to just ignore such
2685 indices. */
2686 if (version < 4)
2687 {
2688 static int warning_printed = 0;
2689 if (!warning_printed)
2690 {
2691 warning (_("Skipping obsolete .gdb_index section in %s."),
2692 filename);
2693 warning_printed = 1;
2694 }
2695 return 0;
2696 }
2697 /* Index version 4 uses a different hash function than index version
2698 5 and later.
2699
2700 Versions earlier than 6 did not emit psymbols for inlined
2701 functions. Using these files will cause GDB not to be able to
2702 set breakpoints on inlined functions by name, so we ignore these
2703 indices unless the user has done
2704 "set use-deprecated-index-sections on". */
2705 if (version < 6 && !deprecated_ok)
2706 {
2707 static int warning_printed = 0;
2708 if (!warning_printed)
2709 {
2710 warning (_("\
2711 Skipping deprecated .gdb_index section in %s.\n\
2712 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2713 to use the section anyway."),
2714 filename);
2715 warning_printed = 1;
2716 }
2717 return 0;
2718 }
2719 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2720 of the TU (for symbols coming from TUs). It's just a performance bug, and
2721 we can't distinguish gdb-generated indices from gold-generated ones, so
2722 nothing to do here. */
2723
2724 /* Indexes with higher version than the one supported by GDB may be no
2725 longer backward compatible. */
2726 if (version > 8)
2727 return 0;
2728
2729 map->version = version;
2730 map->total_size = section->size;
2731
2732 metadata = (offset_type *) (addr + sizeof (offset_type));
2733
2734 i = 0;
2735 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2736 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2737 / 8);
2738 ++i;
2739
2740 *types_list = addr + MAYBE_SWAP (metadata[i]);
2741 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2742 - MAYBE_SWAP (metadata[i]))
2743 / 8);
2744 ++i;
2745
2746 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2747 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2748 - MAYBE_SWAP (metadata[i]));
2749 ++i;
2750
2751 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2752 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2753 - MAYBE_SWAP (metadata[i]))
2754 / (2 * sizeof (offset_type)));
2755 ++i;
2756
2757 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2758
2759 return 1;
2760 }
2761
2762
2763 /* Read the index file. If everything went ok, initialize the "quick"
2764 elements of all the CUs and return 1. Otherwise, return 0. */
2765
2766 static int
2767 dwarf2_read_index (struct objfile *objfile)
2768 {
2769 struct mapped_index local_map, *map;
2770 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2771 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2772
2773 if (!read_index_from_section (objfile, objfile->name,
2774 use_deprecated_index_sections,
2775 &dwarf2_per_objfile->gdb_index, &local_map,
2776 &cu_list, &cu_list_elements,
2777 &types_list, &types_list_elements))
2778 return 0;
2779
2780 /* Don't use the index if it's empty. */
2781 if (local_map.symbol_table_slots == 0)
2782 return 0;
2783
2784 /* If there is a .dwz file, read it so we can get its CU list as
2785 well. */
2786 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
2787 {
2788 struct dwz_file *dwz = dwarf2_get_dwz_file ();
2789 struct mapped_index dwz_map;
2790 const gdb_byte *dwz_types_ignore;
2791 offset_type dwz_types_elements_ignore;
2792
2793 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2794 1,
2795 &dwz->gdb_index, &dwz_map,
2796 &dwz_list, &dwz_list_elements,
2797 &dwz_types_ignore,
2798 &dwz_types_elements_ignore))
2799 {
2800 warning (_("could not read '.gdb_index' section from %s; skipping"),
2801 bfd_get_filename (dwz->dwz_bfd));
2802 return 0;
2803 }
2804 }
2805
2806 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
2807 dwz_list_elements);
2808
2809 if (types_list_elements)
2810 {
2811 struct dwarf2_section_info *section;
2812
2813 /* We can only handle a single .debug_types when we have an
2814 index. */
2815 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2816 return 0;
2817
2818 section = VEC_index (dwarf2_section_info_def,
2819 dwarf2_per_objfile->types, 0);
2820
2821 create_signatured_type_table_from_index (objfile, section, types_list,
2822 types_list_elements);
2823 }
2824
2825 create_addrmap_from_index (objfile, &local_map);
2826
2827 map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2828 *map = local_map;
2829
2830 dwarf2_per_objfile->index_table = map;
2831 dwarf2_per_objfile->using_index = 1;
2832 dwarf2_per_objfile->quick_file_names_table =
2833 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2834
2835 return 1;
2836 }
2837
2838 /* A helper for the "quick" functions which sets the global
2839 dwarf2_per_objfile according to OBJFILE. */
2840
2841 static void
2842 dw2_setup (struct objfile *objfile)
2843 {
2844 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2845 gdb_assert (dwarf2_per_objfile);
2846 }
2847
2848 /* die_reader_func for dw2_get_file_names. */
2849
2850 static void
2851 dw2_get_file_names_reader (const struct die_reader_specs *reader,
2852 gdb_byte *info_ptr,
2853 struct die_info *comp_unit_die,
2854 int has_children,
2855 void *data)
2856 {
2857 struct dwarf2_cu *cu = reader->cu;
2858 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2859 struct objfile *objfile = dwarf2_per_objfile->objfile;
2860 struct dwarf2_per_cu_data *lh_cu;
2861 struct line_header *lh;
2862 struct attribute *attr;
2863 int i;
2864 const char *name, *comp_dir;
2865 void **slot;
2866 struct quick_file_names *qfn;
2867 unsigned int line_offset;
2868
2869 gdb_assert (! this_cu->is_debug_types);
2870
2871 /* Our callers never want to match partial units -- instead they
2872 will match the enclosing full CU. */
2873 if (comp_unit_die->tag == DW_TAG_partial_unit)
2874 {
2875 this_cu->v.quick->no_file_data = 1;
2876 return;
2877 }
2878
2879 lh_cu = this_cu;
2880 lh = NULL;
2881 slot = NULL;
2882 line_offset = 0;
2883
2884 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2885 if (attr)
2886 {
2887 struct quick_file_names find_entry;
2888
2889 line_offset = DW_UNSND (attr);
2890
2891 /* We may have already read in this line header (TU line header sharing).
2892 If we have we're done. */
2893 find_entry.hash.dwo_unit = cu->dwo_unit;
2894 find_entry.hash.line_offset.sect_off = line_offset;
2895 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2896 &find_entry, INSERT);
2897 if (*slot != NULL)
2898 {
2899 lh_cu->v.quick->file_names = *slot;
2900 return;
2901 }
2902
2903 lh = dwarf_decode_line_header (line_offset, cu);
2904 }
2905 if (lh == NULL)
2906 {
2907 lh_cu->v.quick->no_file_data = 1;
2908 return;
2909 }
2910
2911 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2912 qfn->hash.dwo_unit = cu->dwo_unit;
2913 qfn->hash.line_offset.sect_off = line_offset;
2914 gdb_assert (slot != NULL);
2915 *slot = qfn;
2916
2917 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
2918
2919 qfn->num_file_names = lh->num_file_names;
2920 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2921 lh->num_file_names * sizeof (char *));
2922 for (i = 0; i < lh->num_file_names; ++i)
2923 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2924 qfn->real_names = NULL;
2925
2926 free_line_header (lh);
2927
2928 lh_cu->v.quick->file_names = qfn;
2929 }
2930
2931 /* A helper for the "quick" functions which attempts to read the line
2932 table for THIS_CU. */
2933
2934 static struct quick_file_names *
2935 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
2936 {
2937 /* This should never be called for TUs. */
2938 gdb_assert (! this_cu->is_debug_types);
2939 /* Nor type unit groups. */
2940 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
2941
2942 if (this_cu->v.quick->file_names != NULL)
2943 return this_cu->v.quick->file_names;
2944 /* If we know there is no line data, no point in looking again. */
2945 if (this_cu->v.quick->no_file_data)
2946 return NULL;
2947
2948 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
2949
2950 if (this_cu->v.quick->no_file_data)
2951 return NULL;
2952 return this_cu->v.quick->file_names;
2953 }
2954
2955 /* A helper for the "quick" functions which computes and caches the
2956 real path for a given file name from the line table. */
2957
2958 static const char *
2959 dw2_get_real_path (struct objfile *objfile,
2960 struct quick_file_names *qfn, int index)
2961 {
2962 if (qfn->real_names == NULL)
2963 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2964 qfn->num_file_names, sizeof (char *));
2965
2966 if (qfn->real_names[index] == NULL)
2967 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2968
2969 return qfn->real_names[index];
2970 }
2971
2972 static struct symtab *
2973 dw2_find_last_source_symtab (struct objfile *objfile)
2974 {
2975 int index;
2976
2977 dw2_setup (objfile);
2978 index = dwarf2_per_objfile->n_comp_units - 1;
2979 return dw2_instantiate_symtab (dw2_get_cu (index));
2980 }
2981
2982 /* Traversal function for dw2_forget_cached_source_info. */
2983
2984 static int
2985 dw2_free_cached_file_names (void **slot, void *info)
2986 {
2987 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2988
2989 if (file_data->real_names)
2990 {
2991 int i;
2992
2993 for (i = 0; i < file_data->num_file_names; ++i)
2994 {
2995 xfree ((void*) file_data->real_names[i]);
2996 file_data->real_names[i] = NULL;
2997 }
2998 }
2999
3000 return 1;
3001 }
3002
3003 static void
3004 dw2_forget_cached_source_info (struct objfile *objfile)
3005 {
3006 dw2_setup (objfile);
3007
3008 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3009 dw2_free_cached_file_names, NULL);
3010 }
3011
3012 /* Helper function for dw2_map_symtabs_matching_filename that expands
3013 the symtabs and calls the iterator. */
3014
3015 static int
3016 dw2_map_expand_apply (struct objfile *objfile,
3017 struct dwarf2_per_cu_data *per_cu,
3018 const char *name, const char *real_path,
3019 int (*callback) (struct symtab *, void *),
3020 void *data)
3021 {
3022 struct symtab *last_made = objfile->symtabs;
3023
3024 /* Don't visit already-expanded CUs. */
3025 if (per_cu->v.quick->symtab)
3026 return 0;
3027
3028 /* This may expand more than one symtab, and we want to iterate over
3029 all of them. */
3030 dw2_instantiate_symtab (per_cu);
3031
3032 return iterate_over_some_symtabs (name, real_path, callback, data,
3033 objfile->symtabs, last_made);
3034 }
3035
3036 /* Implementation of the map_symtabs_matching_filename method. */
3037
3038 static int
3039 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
3040 const char *real_path,
3041 int (*callback) (struct symtab *, void *),
3042 void *data)
3043 {
3044 int i;
3045 const char *name_basename = lbasename (name);
3046
3047 dw2_setup (objfile);
3048
3049 /* The rule is CUs specify all the files, including those used by
3050 any TU, so there's no need to scan TUs here. */
3051
3052 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3053 {
3054 int j;
3055 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3056 struct quick_file_names *file_data;
3057
3058 /* We only need to look at symtabs not already expanded. */
3059 if (per_cu->v.quick->symtab)
3060 continue;
3061
3062 file_data = dw2_get_file_names (per_cu);
3063 if (file_data == NULL)
3064 continue;
3065
3066 for (j = 0; j < file_data->num_file_names; ++j)
3067 {
3068 const char *this_name = file_data->file_names[j];
3069 const char *this_real_name;
3070
3071 if (compare_filenames_for_search (this_name, name))
3072 {
3073 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3074 callback, data))
3075 return 1;
3076 continue;
3077 }
3078
3079 /* Before we invoke realpath, which can get expensive when many
3080 files are involved, do a quick comparison of the basenames. */
3081 if (! basenames_may_differ
3082 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3083 continue;
3084
3085 this_real_name = dw2_get_real_path (objfile, file_data, j);
3086 if (compare_filenames_for_search (this_real_name, name))
3087 {
3088 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3089 callback, data))
3090 return 1;
3091 continue;
3092 }
3093
3094 if (real_path != NULL)
3095 {
3096 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3097 gdb_assert (IS_ABSOLUTE_PATH (name));
3098 if (this_real_name != NULL
3099 && FILENAME_CMP (real_path, this_real_name) == 0)
3100 {
3101 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3102 callback, data))
3103 return 1;
3104 continue;
3105 }
3106 }
3107 }
3108 }
3109
3110 return 0;
3111 }
3112
3113 /* Struct used to manage iterating over all CUs looking for a symbol. */
3114
3115 struct dw2_symtab_iterator
3116 {
3117 /* The internalized form of .gdb_index. */
3118 struct mapped_index *index;
3119 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3120 int want_specific_block;
3121 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3122 Unused if !WANT_SPECIFIC_BLOCK. */
3123 int block_index;
3124 /* The kind of symbol we're looking for. */
3125 domain_enum domain;
3126 /* The list of CUs from the index entry of the symbol,
3127 or NULL if not found. */
3128 offset_type *vec;
3129 /* The next element in VEC to look at. */
3130 int next;
3131 /* The number of elements in VEC, or zero if there is no match. */
3132 int length;
3133 };
3134
3135 /* Initialize the index symtab iterator ITER.
3136 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3137 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
3138
3139 static void
3140 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3141 struct mapped_index *index,
3142 int want_specific_block,
3143 int block_index,
3144 domain_enum domain,
3145 const char *name)
3146 {
3147 iter->index = index;
3148 iter->want_specific_block = want_specific_block;
3149 iter->block_index = block_index;
3150 iter->domain = domain;
3151 iter->next = 0;
3152
3153 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3154 iter->length = MAYBE_SWAP (*iter->vec);
3155 else
3156 {
3157 iter->vec = NULL;
3158 iter->length = 0;
3159 }
3160 }
3161
3162 /* Return the next matching CU or NULL if there are no more. */
3163
3164 static struct dwarf2_per_cu_data *
3165 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3166 {
3167 for ( ; iter->next < iter->length; ++iter->next)
3168 {
3169 offset_type cu_index_and_attrs =
3170 MAYBE_SWAP (iter->vec[iter->next + 1]);
3171 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3172 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
3173 int want_static = iter->block_index != GLOBAL_BLOCK;
3174 /* This value is only valid for index versions >= 7. */
3175 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3176 gdb_index_symbol_kind symbol_kind =
3177 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3178 /* Only check the symbol attributes if they're present.
3179 Indices prior to version 7 don't record them,
3180 and indices >= 7 may elide them for certain symbols
3181 (gold does this). */
3182 int attrs_valid =
3183 (iter->index->version >= 7
3184 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3185
3186 /* Skip if already read in. */
3187 if (per_cu->v.quick->symtab)
3188 continue;
3189
3190 if (attrs_valid
3191 && iter->want_specific_block
3192 && want_static != is_static)
3193 continue;
3194
3195 /* Only check the symbol's kind if it has one. */
3196 if (attrs_valid)
3197 {
3198 switch (iter->domain)
3199 {
3200 case VAR_DOMAIN:
3201 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3202 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3203 /* Some types are also in VAR_DOMAIN. */
3204 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3205 continue;
3206 break;
3207 case STRUCT_DOMAIN:
3208 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3209 continue;
3210 break;
3211 case LABEL_DOMAIN:
3212 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3213 continue;
3214 break;
3215 default:
3216 break;
3217 }
3218 }
3219
3220 ++iter->next;
3221 return per_cu;
3222 }
3223
3224 return NULL;
3225 }
3226
3227 static struct symtab *
3228 dw2_lookup_symbol (struct objfile *objfile, int block_index,
3229 const char *name, domain_enum domain)
3230 {
3231 struct symtab *stab_best = NULL;
3232 struct mapped_index *index;
3233
3234 dw2_setup (objfile);
3235
3236 index = dwarf2_per_objfile->index_table;
3237
3238 /* index is NULL if OBJF_READNOW. */
3239 if (index)
3240 {
3241 struct dw2_symtab_iterator iter;
3242 struct dwarf2_per_cu_data *per_cu;
3243
3244 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
3245
3246 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3247 {
3248 struct symbol *sym = NULL;
3249 struct symtab *stab = dw2_instantiate_symtab (per_cu);
3250
3251 /* Some caution must be observed with overloaded functions
3252 and methods, since the index will not contain any overload
3253 information (but NAME might contain it). */
3254 if (stab->primary)
3255 {
3256 struct blockvector *bv = BLOCKVECTOR (stab);
3257 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3258
3259 sym = lookup_block_symbol (block, name, domain);
3260 }
3261
3262 if (sym && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3263 {
3264 if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
3265 return stab;
3266
3267 stab_best = stab;
3268 }
3269
3270 /* Keep looking through other CUs. */
3271 }
3272 }
3273
3274 return stab_best;
3275 }
3276
3277 static void
3278 dw2_print_stats (struct objfile *objfile)
3279 {
3280 int i, total, count;
3281
3282 dw2_setup (objfile);
3283 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
3284 count = 0;
3285 for (i = 0; i < total; ++i)
3286 {
3287 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3288
3289 if (!per_cu->v.quick->symtab)
3290 ++count;
3291 }
3292 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3293 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3294 }
3295
3296 static void
3297 dw2_dump (struct objfile *objfile)
3298 {
3299 /* Nothing worth printing. */
3300 }
3301
3302 static void
3303 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
3304 struct section_offsets *delta)
3305 {
3306 /* There's nothing to relocate here. */
3307 }
3308
3309 static void
3310 dw2_expand_symtabs_for_function (struct objfile *objfile,
3311 const char *func_name)
3312 {
3313 struct mapped_index *index;
3314
3315 dw2_setup (objfile);
3316
3317 index = dwarf2_per_objfile->index_table;
3318
3319 /* index is NULL if OBJF_READNOW. */
3320 if (index)
3321 {
3322 struct dw2_symtab_iterator iter;
3323 struct dwarf2_per_cu_data *per_cu;
3324
3325 /* Note: It doesn't matter what we pass for block_index here. */
3326 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3327 func_name);
3328
3329 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3330 dw2_instantiate_symtab (per_cu);
3331 }
3332 }
3333
3334 static void
3335 dw2_expand_all_symtabs (struct objfile *objfile)
3336 {
3337 int i;
3338
3339 dw2_setup (objfile);
3340
3341 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3342 + dwarf2_per_objfile->n_type_units); ++i)
3343 {
3344 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3345
3346 dw2_instantiate_symtab (per_cu);
3347 }
3348 }
3349
3350 static void
3351 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3352 const char *fullname)
3353 {
3354 int i;
3355
3356 dw2_setup (objfile);
3357
3358 /* We don't need to consider type units here.
3359 This is only called for examining code, e.g. expand_line_sal.
3360 There can be an order of magnitude (or more) more type units
3361 than comp units, and we avoid them if we can. */
3362
3363 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3364 {
3365 int j;
3366 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3367 struct quick_file_names *file_data;
3368
3369 /* We only need to look at symtabs not already expanded. */
3370 if (per_cu->v.quick->symtab)
3371 continue;
3372
3373 file_data = dw2_get_file_names (per_cu);
3374 if (file_data == NULL)
3375 continue;
3376
3377 for (j = 0; j < file_data->num_file_names; ++j)
3378 {
3379 const char *this_fullname = file_data->file_names[j];
3380
3381 if (filename_cmp (this_fullname, fullname) == 0)
3382 {
3383 dw2_instantiate_symtab (per_cu);
3384 break;
3385 }
3386 }
3387 }
3388 }
3389
3390 /* A helper function for dw2_find_symbol_file that finds the primary
3391 file name for a given CU. This is a die_reader_func. */
3392
3393 static void
3394 dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
3395 gdb_byte *info_ptr,
3396 struct die_info *comp_unit_die,
3397 int has_children,
3398 void *data)
3399 {
3400 const char **result_ptr = data;
3401 struct dwarf2_cu *cu = reader->cu;
3402 struct attribute *attr;
3403
3404 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3405 if (attr == NULL)
3406 *result_ptr = NULL;
3407 else
3408 *result_ptr = DW_STRING (attr);
3409 }
3410
3411 static const char *
3412 dw2_find_symbol_file (struct objfile *objfile, const char *name)
3413 {
3414 struct dwarf2_per_cu_data *per_cu;
3415 offset_type *vec;
3416 const char *filename;
3417
3418 dw2_setup (objfile);
3419
3420 /* index_table is NULL if OBJF_READNOW. */
3421 if (!dwarf2_per_objfile->index_table)
3422 {
3423 struct symtab *s;
3424
3425 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
3426 {
3427 struct blockvector *bv = BLOCKVECTOR (s);
3428 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
3429 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
3430
3431 if (sym)
3432 {
3433 /* Only file extension of returned filename is recognized. */
3434 return SYMBOL_SYMTAB (sym)->filename;
3435 }
3436 }
3437 return NULL;
3438 }
3439
3440 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
3441 name, &vec))
3442 return NULL;
3443
3444 /* Note that this just looks at the very first one named NAME -- but
3445 actually we are looking for a function. find_main_filename
3446 should be rewritten so that it doesn't require a custom hook. It
3447 could just use the ordinary symbol tables. */
3448 /* vec[0] is the length, which must always be >0. */
3449 per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
3450
3451 if (per_cu->v.quick->symtab != NULL)
3452 {
3453 /* Only file extension of returned filename is recognized. */
3454 return per_cu->v.quick->symtab->filename;
3455 }
3456
3457 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
3458 dw2_get_primary_filename_reader, &filename);
3459
3460 /* Only file extension of returned filename is recognized. */
3461 return filename;
3462 }
3463
3464 static void
3465 dw2_map_matching_symbols (const char * name, domain_enum namespace,
3466 struct objfile *objfile, int global,
3467 int (*callback) (struct block *,
3468 struct symbol *, void *),
3469 void *data, symbol_compare_ftype *match,
3470 symbol_compare_ftype *ordered_compare)
3471 {
3472 /* Currently unimplemented; used for Ada. The function can be called if the
3473 current language is Ada for a non-Ada objfile using GNU index. As Ada
3474 does not look for non-Ada symbols this function should just return. */
3475 }
3476
3477 static void
3478 dw2_expand_symtabs_matching
3479 (struct objfile *objfile,
3480 int (*file_matcher) (const char *, void *, int basenames),
3481 int (*name_matcher) (const char *, void *),
3482 enum search_domain kind,
3483 void *data)
3484 {
3485 int i;
3486 offset_type iter;
3487 struct mapped_index *index;
3488
3489 dw2_setup (objfile);
3490
3491 /* index_table is NULL if OBJF_READNOW. */
3492 if (!dwarf2_per_objfile->index_table)
3493 return;
3494 index = dwarf2_per_objfile->index_table;
3495
3496 if (file_matcher != NULL)
3497 {
3498 struct cleanup *cleanup;
3499 htab_t visited_found, visited_not_found;
3500
3501 visited_found = htab_create_alloc (10,
3502 htab_hash_pointer, htab_eq_pointer,
3503 NULL, xcalloc, xfree);
3504 cleanup = make_cleanup_htab_delete (visited_found);
3505 visited_not_found = htab_create_alloc (10,
3506 htab_hash_pointer, htab_eq_pointer,
3507 NULL, xcalloc, xfree);
3508 make_cleanup_htab_delete (visited_not_found);
3509
3510 /* The rule is CUs specify all the files, including those used by
3511 any TU, so there's no need to scan TUs here. */
3512
3513 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3514 {
3515 int j;
3516 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3517 struct quick_file_names *file_data;
3518 void **slot;
3519
3520 per_cu->v.quick->mark = 0;
3521
3522 /* We only need to look at symtabs not already expanded. */
3523 if (per_cu->v.quick->symtab)
3524 continue;
3525
3526 file_data = dw2_get_file_names (per_cu);
3527 if (file_data == NULL)
3528 continue;
3529
3530 if (htab_find (visited_not_found, file_data) != NULL)
3531 continue;
3532 else if (htab_find (visited_found, file_data) != NULL)
3533 {
3534 per_cu->v.quick->mark = 1;
3535 continue;
3536 }
3537
3538 for (j = 0; j < file_data->num_file_names; ++j)
3539 {
3540 const char *this_real_name;
3541
3542 if (file_matcher (file_data->file_names[j], data, 0))
3543 {
3544 per_cu->v.quick->mark = 1;
3545 break;
3546 }
3547
3548 /* Before we invoke realpath, which can get expensive when many
3549 files are involved, do a quick comparison of the basenames. */
3550 if (!basenames_may_differ
3551 && !file_matcher (lbasename (file_data->file_names[j]),
3552 data, 1))
3553 continue;
3554
3555 this_real_name = dw2_get_real_path (objfile, file_data, j);
3556 if (file_matcher (this_real_name, data, 0))
3557 {
3558 per_cu->v.quick->mark = 1;
3559 break;
3560 }
3561 }
3562
3563 slot = htab_find_slot (per_cu->v.quick->mark
3564 ? visited_found
3565 : visited_not_found,
3566 file_data, INSERT);
3567 *slot = file_data;
3568 }
3569
3570 do_cleanups (cleanup);
3571 }
3572
3573 for (iter = 0; iter < index->symbol_table_slots; ++iter)
3574 {
3575 offset_type idx = 2 * iter;
3576 const char *name;
3577 offset_type *vec, vec_len, vec_idx;
3578
3579 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
3580 continue;
3581
3582 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
3583
3584 if (! (*name_matcher) (name, data))
3585 continue;
3586
3587 /* The name was matched, now expand corresponding CUs that were
3588 marked. */
3589 vec = (offset_type *) (index->constant_pool
3590 + MAYBE_SWAP (index->symbol_table[idx + 1]));
3591 vec_len = MAYBE_SWAP (vec[0]);
3592 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3593 {
3594 struct dwarf2_per_cu_data *per_cu;
3595 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3596 gdb_index_symbol_kind symbol_kind =
3597 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3598 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3599
3600 /* Don't crash on bad data. */
3601 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3602 + dwarf2_per_objfile->n_type_units))
3603 continue;
3604
3605 /* Only check the symbol's kind if it has one.
3606 Indices prior to version 7 don't record it. */
3607 if (index->version >= 7)
3608 {
3609 switch (kind)
3610 {
3611 case VARIABLES_DOMAIN:
3612 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3613 continue;
3614 break;
3615 case FUNCTIONS_DOMAIN:
3616 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3617 continue;
3618 break;
3619 case TYPES_DOMAIN:
3620 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3621 continue;
3622 break;
3623 default:
3624 break;
3625 }
3626 }
3627
3628 per_cu = dw2_get_cu (cu_index);
3629 if (file_matcher == NULL || per_cu->v.quick->mark)
3630 dw2_instantiate_symtab (per_cu);
3631 }
3632 }
3633 }
3634
3635 /* A helper for dw2_find_pc_sect_symtab which finds the most specific
3636 symtab. */
3637
3638 static struct symtab *
3639 recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3640 {
3641 int i;
3642
3643 if (BLOCKVECTOR (symtab) != NULL
3644 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3645 return symtab;
3646
3647 if (symtab->includes == NULL)
3648 return NULL;
3649
3650 for (i = 0; symtab->includes[i]; ++i)
3651 {
3652 struct symtab *s = symtab->includes[i];
3653
3654 s = recursively_find_pc_sect_symtab (s, pc);
3655 if (s != NULL)
3656 return s;
3657 }
3658
3659 return NULL;
3660 }
3661
3662 static struct symtab *
3663 dw2_find_pc_sect_symtab (struct objfile *objfile,
3664 struct minimal_symbol *msymbol,
3665 CORE_ADDR pc,
3666 struct obj_section *section,
3667 int warn_if_readin)
3668 {
3669 struct dwarf2_per_cu_data *data;
3670 struct symtab *result;
3671
3672 dw2_setup (objfile);
3673
3674 if (!objfile->psymtabs_addrmap)
3675 return NULL;
3676
3677 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3678 if (!data)
3679 return NULL;
3680
3681 if (warn_if_readin && data->v.quick->symtab)
3682 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
3683 paddress (get_objfile_arch (objfile), pc));
3684
3685 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3686 gdb_assert (result != NULL);
3687 return result;
3688 }
3689
3690 static void
3691 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
3692 void *data, int need_fullname)
3693 {
3694 int i;
3695 struct cleanup *cleanup;
3696 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3697 NULL, xcalloc, xfree);
3698
3699 cleanup = make_cleanup_htab_delete (visited);
3700 dw2_setup (objfile);
3701
3702 /* The rule is CUs specify all the files, including those used by
3703 any TU, so there's no need to scan TUs here.
3704 We can ignore file names coming from already-expanded CUs. */
3705
3706 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3707 {
3708 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3709
3710 if (per_cu->v.quick->symtab)
3711 {
3712 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3713 INSERT);
3714
3715 *slot = per_cu->v.quick->file_names;
3716 }
3717 }
3718
3719 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3720 {
3721 int j;
3722 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3723 struct quick_file_names *file_data;
3724 void **slot;
3725
3726 /* We only need to look at symtabs not already expanded. */
3727 if (per_cu->v.quick->symtab)
3728 continue;
3729
3730 file_data = dw2_get_file_names (per_cu);
3731 if (file_data == NULL)
3732 continue;
3733
3734 slot = htab_find_slot (visited, file_data, INSERT);
3735 if (*slot)
3736 {
3737 /* Already visited. */
3738 continue;
3739 }
3740 *slot = file_data;
3741
3742 for (j = 0; j < file_data->num_file_names; ++j)
3743 {
3744 const char *this_real_name;
3745
3746 if (need_fullname)
3747 this_real_name = dw2_get_real_path (objfile, file_data, j);
3748 else
3749 this_real_name = NULL;
3750 (*fun) (file_data->file_names[j], this_real_name, data);
3751 }
3752 }
3753
3754 do_cleanups (cleanup);
3755 }
3756
3757 static int
3758 dw2_has_symbols (struct objfile *objfile)
3759 {
3760 return 1;
3761 }
3762
3763 const struct quick_symbol_functions dwarf2_gdb_index_functions =
3764 {
3765 dw2_has_symbols,
3766 dw2_find_last_source_symtab,
3767 dw2_forget_cached_source_info,
3768 dw2_map_symtabs_matching_filename,
3769 dw2_lookup_symbol,
3770 dw2_print_stats,
3771 dw2_dump,
3772 dw2_relocate,
3773 dw2_expand_symtabs_for_function,
3774 dw2_expand_all_symtabs,
3775 dw2_expand_symtabs_with_fullname,
3776 dw2_find_symbol_file,
3777 dw2_map_matching_symbols,
3778 dw2_expand_symtabs_matching,
3779 dw2_find_pc_sect_symtab,
3780 dw2_map_symbol_filenames
3781 };
3782
3783 /* Initialize for reading DWARF for this objfile. Return 0 if this
3784 file will use psymtabs, or 1 if using the GNU index. */
3785
3786 int
3787 dwarf2_initialize_objfile (struct objfile *objfile)
3788 {
3789 /* If we're about to read full symbols, don't bother with the
3790 indices. In this case we also don't care if some other debug
3791 format is making psymtabs, because they are all about to be
3792 expanded anyway. */
3793 if ((objfile->flags & OBJF_READNOW))
3794 {
3795 int i;
3796
3797 dwarf2_per_objfile->using_index = 1;
3798 create_all_comp_units (objfile);
3799 create_all_type_units (objfile);
3800 dwarf2_per_objfile->quick_file_names_table =
3801 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3802
3803 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3804 + dwarf2_per_objfile->n_type_units); ++i)
3805 {
3806 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3807
3808 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3809 struct dwarf2_per_cu_quick_data);
3810 }
3811
3812 /* Return 1 so that gdb sees the "quick" functions. However,
3813 these functions will be no-ops because we will have expanded
3814 all symtabs. */
3815 return 1;
3816 }
3817
3818 if (dwarf2_read_index (objfile))
3819 return 1;
3820
3821 return 0;
3822 }
3823
3824 \f
3825
3826 /* Build a partial symbol table. */
3827
3828 void
3829 dwarf2_build_psymtabs (struct objfile *objfile)
3830 {
3831 volatile struct gdb_exception except;
3832
3833 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
3834 {
3835 init_psymbol_list (objfile, 1024);
3836 }
3837
3838 TRY_CATCH (except, RETURN_MASK_ERROR)
3839 {
3840 /* This isn't really ideal: all the data we allocate on the
3841 objfile's obstack is still uselessly kept around. However,
3842 freeing it seems unsafe. */
3843 struct cleanup *cleanups = make_cleanup_discard_psymtabs (objfile);
3844
3845 dwarf2_build_psymtabs_hard (objfile);
3846 discard_cleanups (cleanups);
3847 }
3848 if (except.reason < 0)
3849 exception_print (gdb_stderr, except);
3850 }
3851
3852 /* Return the total length of the CU described by HEADER. */
3853
3854 static unsigned int
3855 get_cu_length (const struct comp_unit_head *header)
3856 {
3857 return header->initial_length_size + header->length;
3858 }
3859
3860 /* Return TRUE if OFFSET is within CU_HEADER. */
3861
3862 static inline int
3863 offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
3864 {
3865 sect_offset bottom = { cu_header->offset.sect_off };
3866 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
3867
3868 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
3869 }
3870
3871 /* Find the base address of the compilation unit for range lists and
3872 location lists. It will normally be specified by DW_AT_low_pc.
3873 In DWARF-3 draft 4, the base address could be overridden by
3874 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3875 compilation units with discontinuous ranges. */
3876
3877 static void
3878 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3879 {
3880 struct attribute *attr;
3881
3882 cu->base_known = 0;
3883 cu->base_address = 0;
3884
3885 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3886 if (attr)
3887 {
3888 cu->base_address = DW_ADDR (attr);
3889 cu->base_known = 1;
3890 }
3891 else
3892 {
3893 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3894 if (attr)
3895 {
3896 cu->base_address = DW_ADDR (attr);
3897 cu->base_known = 1;
3898 }
3899 }
3900 }
3901
3902 /* Read in the comp unit header information from the debug_info at info_ptr.
3903 NOTE: This leaves members offset, first_die_offset to be filled in
3904 by the caller. */
3905
3906 static gdb_byte *
3907 read_comp_unit_head (struct comp_unit_head *cu_header,
3908 gdb_byte *info_ptr, bfd *abfd)
3909 {
3910 int signed_addr;
3911 unsigned int bytes_read;
3912
3913 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3914 cu_header->initial_length_size = bytes_read;
3915 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
3916 info_ptr += bytes_read;
3917 cu_header->version = read_2_bytes (abfd, info_ptr);
3918 info_ptr += 2;
3919 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3920 &bytes_read);
3921 info_ptr += bytes_read;
3922 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3923 info_ptr += 1;
3924 signed_addr = bfd_get_sign_extend_vma (abfd);
3925 if (signed_addr < 0)
3926 internal_error (__FILE__, __LINE__,
3927 _("read_comp_unit_head: dwarf from non elf file"));
3928 cu_header->signed_addr_p = signed_addr;
3929
3930 return info_ptr;
3931 }
3932
3933 /* Helper function that returns the proper abbrev section for
3934 THIS_CU. */
3935
3936 static struct dwarf2_section_info *
3937 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3938 {
3939 struct dwarf2_section_info *abbrev;
3940
3941 if (this_cu->is_dwz)
3942 abbrev = &dwarf2_get_dwz_file ()->abbrev;
3943 else
3944 abbrev = &dwarf2_per_objfile->abbrev;
3945
3946 return abbrev;
3947 }
3948
3949 /* Subroutine of read_and_check_comp_unit_head and
3950 read_and_check_type_unit_head to simplify them.
3951 Perform various error checking on the header. */
3952
3953 static void
3954 error_check_comp_unit_head (struct comp_unit_head *header,
3955 struct dwarf2_section_info *section,
3956 struct dwarf2_section_info *abbrev_section)
3957 {
3958 bfd *abfd = section->asection->owner;
3959 const char *filename = bfd_get_filename (abfd);
3960
3961 if (header->version != 2 && header->version != 3 && header->version != 4)
3962 error (_("Dwarf Error: wrong version in compilation unit header "
3963 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3964 filename);
3965
3966 if (header->abbrev_offset.sect_off
3967 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
3968 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3969 "(offset 0x%lx + 6) [in module %s]"),
3970 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
3971 filename);
3972
3973 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3974 avoid potential 32-bit overflow. */
3975 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
3976 > section->size)
3977 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3978 "(offset 0x%lx + 0) [in module %s]"),
3979 (long) header->length, (long) header->offset.sect_off,
3980 filename);
3981 }
3982
3983 /* Read in a CU/TU header and perform some basic error checking.
3984 The contents of the header are stored in HEADER.
3985 The result is a pointer to the start of the first DIE. */
3986
3987 static gdb_byte *
3988 read_and_check_comp_unit_head (struct comp_unit_head *header,
3989 struct dwarf2_section_info *section,
3990 struct dwarf2_section_info *abbrev_section,
3991 gdb_byte *info_ptr,
3992 int is_debug_types_section)
3993 {
3994 gdb_byte *beg_of_comp_unit = info_ptr;
3995 bfd *abfd = section->asection->owner;
3996
3997 header->offset.sect_off = beg_of_comp_unit - section->buffer;
3998
3999 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4000
4001 /* If we're reading a type unit, skip over the signature and
4002 type_offset fields. */
4003 if (is_debug_types_section)
4004 info_ptr += 8 /*signature*/ + header->offset_size;
4005
4006 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
4007
4008 error_check_comp_unit_head (header, section, abbrev_section);
4009
4010 return info_ptr;
4011 }
4012
4013 /* Read in the types comp unit header information from .debug_types entry at
4014 types_ptr. The result is a pointer to one past the end of the header. */
4015
4016 static gdb_byte *
4017 read_and_check_type_unit_head (struct comp_unit_head *header,
4018 struct dwarf2_section_info *section,
4019 struct dwarf2_section_info *abbrev_section,
4020 gdb_byte *info_ptr,
4021 ULONGEST *signature,
4022 cu_offset *type_offset_in_tu)
4023 {
4024 gdb_byte *beg_of_comp_unit = info_ptr;
4025 bfd *abfd = section->asection->owner;
4026
4027 header->offset.sect_off = beg_of_comp_unit - section->buffer;
4028
4029 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4030
4031 /* If we're reading a type unit, skip over the signature and
4032 type_offset fields. */
4033 if (signature != NULL)
4034 *signature = read_8_bytes (abfd, info_ptr);
4035 info_ptr += 8;
4036 if (type_offset_in_tu != NULL)
4037 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
4038 header->offset_size);
4039 info_ptr += header->offset_size;
4040
4041 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
4042
4043 error_check_comp_unit_head (header, section, abbrev_section);
4044
4045 return info_ptr;
4046 }
4047
4048 /* Fetch the abbreviation table offset from a comp or type unit header. */
4049
4050 static sect_offset
4051 read_abbrev_offset (struct dwarf2_section_info *section,
4052 sect_offset offset)
4053 {
4054 bfd *abfd = section->asection->owner;
4055 gdb_byte *info_ptr;
4056 unsigned int length, initial_length_size, offset_size;
4057 sect_offset abbrev_offset;
4058
4059 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4060 info_ptr = section->buffer + offset.sect_off;
4061 length = read_initial_length (abfd, info_ptr, &initial_length_size);
4062 offset_size = initial_length_size == 4 ? 4 : 8;
4063 info_ptr += initial_length_size + 2 /*version*/;
4064 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4065 return abbrev_offset;
4066 }
4067
4068 /* Allocate a new partial symtab for file named NAME and mark this new
4069 partial symtab as being an include of PST. */
4070
4071 static void
4072 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
4073 struct objfile *objfile)
4074 {
4075 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4076
4077 if (!IS_ABSOLUTE_PATH (subpst->filename))
4078 {
4079 /* It shares objfile->objfile_obstack. */
4080 subpst->dirname = pst->dirname;
4081 }
4082
4083 subpst->section_offsets = pst->section_offsets;
4084 subpst->textlow = 0;
4085 subpst->texthigh = 0;
4086
4087 subpst->dependencies = (struct partial_symtab **)
4088 obstack_alloc (&objfile->objfile_obstack,
4089 sizeof (struct partial_symtab *));
4090 subpst->dependencies[0] = pst;
4091 subpst->number_of_dependencies = 1;
4092
4093 subpst->globals_offset = 0;
4094 subpst->n_global_syms = 0;
4095 subpst->statics_offset = 0;
4096 subpst->n_static_syms = 0;
4097 subpst->symtab = NULL;
4098 subpst->read_symtab = pst->read_symtab;
4099 subpst->readin = 0;
4100
4101 /* No private part is necessary for include psymtabs. This property
4102 can be used to differentiate between such include psymtabs and
4103 the regular ones. */
4104 subpst->read_symtab_private = NULL;
4105 }
4106
4107 /* Read the Line Number Program data and extract the list of files
4108 included by the source file represented by PST. Build an include
4109 partial symtab for each of these included files. */
4110
4111 static void
4112 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
4113 struct die_info *die,
4114 struct partial_symtab *pst)
4115 {
4116 struct line_header *lh = NULL;
4117 struct attribute *attr;
4118
4119 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4120 if (attr)
4121 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
4122 if (lh == NULL)
4123 return; /* No linetable, so no includes. */
4124
4125 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
4126 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
4127
4128 free_line_header (lh);
4129 }
4130
4131 static hashval_t
4132 hash_signatured_type (const void *item)
4133 {
4134 const struct signatured_type *sig_type = item;
4135
4136 /* This drops the top 32 bits of the signature, but is ok for a hash. */
4137 return sig_type->signature;
4138 }
4139
4140 static int
4141 eq_signatured_type (const void *item_lhs, const void *item_rhs)
4142 {
4143 const struct signatured_type *lhs = item_lhs;
4144 const struct signatured_type *rhs = item_rhs;
4145
4146 return lhs->signature == rhs->signature;
4147 }
4148
4149 /* Allocate a hash table for signatured types. */
4150
4151 static htab_t
4152 allocate_signatured_type_table (struct objfile *objfile)
4153 {
4154 return htab_create_alloc_ex (41,
4155 hash_signatured_type,
4156 eq_signatured_type,
4157 NULL,
4158 &objfile->objfile_obstack,
4159 hashtab_obstack_allocate,
4160 dummy_obstack_deallocate);
4161 }
4162
4163 /* A helper function to add a signatured type CU to a table. */
4164
4165 static int
4166 add_signatured_type_cu_to_table (void **slot, void *datum)
4167 {
4168 struct signatured_type *sigt = *slot;
4169 struct signatured_type ***datap = datum;
4170
4171 **datap = sigt;
4172 ++*datap;
4173
4174 return 1;
4175 }
4176
4177 /* Create the hash table of all entries in the .debug_types
4178 (or .debug_types.dwo) section(s).
4179 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
4180 otherwise it is NULL.
4181
4182 The result is a pointer to the hash table or NULL if there are no types.
4183
4184 Note: This function processes DWO files only, not DWP files. */
4185
4186 static htab_t
4187 create_debug_types_hash_table (struct dwo_file *dwo_file,
4188 VEC (dwarf2_section_info_def) *types)
4189 {
4190 struct objfile *objfile = dwarf2_per_objfile->objfile;
4191 htab_t types_htab = NULL;
4192 int ix;
4193 struct dwarf2_section_info *section;
4194 struct dwarf2_section_info *abbrev_section;
4195
4196 if (VEC_empty (dwarf2_section_info_def, types))
4197 return NULL;
4198
4199 abbrev_section = (dwo_file != NULL
4200 ? &dwo_file->sections.abbrev
4201 : &dwarf2_per_objfile->abbrev);
4202
4203 if (dwarf2_read_debug)
4204 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4205 dwo_file ? ".dwo" : "",
4206 bfd_get_filename (abbrev_section->asection->owner));
4207
4208 for (ix = 0;
4209 VEC_iterate (dwarf2_section_info_def, types, ix, section);
4210 ++ix)
4211 {
4212 bfd *abfd;
4213 gdb_byte *info_ptr, *end_ptr;
4214 struct dwarf2_section_info *abbrev_section;
4215
4216 dwarf2_read_section (objfile, section);
4217 info_ptr = section->buffer;
4218
4219 if (info_ptr == NULL)
4220 continue;
4221
4222 /* We can't set abfd until now because the section may be empty or
4223 not present, in which case section->asection will be NULL. */
4224 abfd = section->asection->owner;
4225
4226 if (dwo_file)
4227 abbrev_section = &dwo_file->sections.abbrev;
4228 else
4229 abbrev_section = &dwarf2_per_objfile->abbrev;
4230
4231 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4232 because we don't need to read any dies: the signature is in the
4233 header. */
4234
4235 end_ptr = info_ptr + section->size;
4236 while (info_ptr < end_ptr)
4237 {
4238 sect_offset offset;
4239 cu_offset type_offset_in_tu;
4240 ULONGEST signature;
4241 struct signatured_type *sig_type;
4242 struct dwo_unit *dwo_tu;
4243 void **slot;
4244 gdb_byte *ptr = info_ptr;
4245 struct comp_unit_head header;
4246 unsigned int length;
4247
4248 offset.sect_off = ptr - section->buffer;
4249
4250 /* We need to read the type's signature in order to build the hash
4251 table, but we don't need anything else just yet. */
4252
4253 ptr = read_and_check_type_unit_head (&header, section,
4254 abbrev_section, ptr,
4255 &signature, &type_offset_in_tu);
4256
4257 length = get_cu_length (&header);
4258
4259 /* Skip dummy type units. */
4260 if (ptr >= info_ptr + length
4261 || peek_abbrev_code (abfd, ptr) == 0)
4262 {
4263 info_ptr += length;
4264 continue;
4265 }
4266
4267 if (types_htab == NULL)
4268 {
4269 if (dwo_file)
4270 types_htab = allocate_dwo_unit_table (objfile);
4271 else
4272 types_htab = allocate_signatured_type_table (objfile);
4273 }
4274
4275 if (dwo_file)
4276 {
4277 sig_type = NULL;
4278 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4279 struct dwo_unit);
4280 dwo_tu->dwo_file = dwo_file;
4281 dwo_tu->signature = signature;
4282 dwo_tu->type_offset_in_tu = type_offset_in_tu;
4283 dwo_tu->section = section;
4284 dwo_tu->offset = offset;
4285 dwo_tu->length = length;
4286 }
4287 else
4288 {
4289 /* N.B.: type_offset is not usable if this type uses a DWO file.
4290 The real type_offset is in the DWO file. */
4291 dwo_tu = NULL;
4292 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4293 struct signatured_type);
4294 sig_type->signature = signature;
4295 sig_type->type_offset_in_tu = type_offset_in_tu;
4296 sig_type->per_cu.objfile = objfile;
4297 sig_type->per_cu.is_debug_types = 1;
4298 sig_type->per_cu.section = section;
4299 sig_type->per_cu.offset = offset;
4300 sig_type->per_cu.length = length;
4301 }
4302
4303 slot = htab_find_slot (types_htab,
4304 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4305 INSERT);
4306 gdb_assert (slot != NULL);
4307 if (*slot != NULL)
4308 {
4309 sect_offset dup_offset;
4310
4311 if (dwo_file)
4312 {
4313 const struct dwo_unit *dup_tu = *slot;
4314
4315 dup_offset = dup_tu->offset;
4316 }
4317 else
4318 {
4319 const struct signatured_type *dup_tu = *slot;
4320
4321 dup_offset = dup_tu->per_cu.offset;
4322 }
4323
4324 complaint (&symfile_complaints,
4325 _("debug type entry at offset 0x%x is duplicate to"
4326 " the entry at offset 0x%x, signature 0x%s"),
4327 offset.sect_off, dup_offset.sect_off,
4328 phex (signature, sizeof (signature)));
4329 }
4330 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
4331
4332 if (dwarf2_read_debug)
4333 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
4334 offset.sect_off,
4335 phex (signature, sizeof (signature)));
4336
4337 info_ptr += length;
4338 }
4339 }
4340
4341 return types_htab;
4342 }
4343
4344 /* Create the hash table of all entries in the .debug_types section,
4345 and initialize all_type_units.
4346 The result is zero if there is an error (e.g. missing .debug_types section),
4347 otherwise non-zero. */
4348
4349 static int
4350 create_all_type_units (struct objfile *objfile)
4351 {
4352 htab_t types_htab;
4353 struct signatured_type **iter;
4354
4355 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4356 if (types_htab == NULL)
4357 {
4358 dwarf2_per_objfile->signatured_types = NULL;
4359 return 0;
4360 }
4361
4362 dwarf2_per_objfile->signatured_types = types_htab;
4363
4364 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4365 dwarf2_per_objfile->all_type_units
4366 = obstack_alloc (&objfile->objfile_obstack,
4367 dwarf2_per_objfile->n_type_units
4368 * sizeof (struct signatured_type *));
4369 iter = &dwarf2_per_objfile->all_type_units[0];
4370 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4371 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4372 == dwarf2_per_objfile->n_type_units);
4373
4374 return 1;
4375 }
4376
4377 /* Lookup a signature based type for DW_FORM_ref_sig8.
4378 Returns NULL if signature SIG is not present in the table.
4379 It is up to the caller to complain about this. */
4380
4381 static struct signatured_type *
4382 lookup_signatured_type (ULONGEST sig)
4383 {
4384 struct signatured_type find_entry, *entry;
4385
4386 if (dwarf2_per_objfile->signatured_types == NULL)
4387 return NULL;
4388 find_entry.signature = sig;
4389 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4390 return entry;
4391 }
4392 \f
4393 /* Low level DIE reading support. */
4394
4395 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4396
4397 static void
4398 init_cu_die_reader (struct die_reader_specs *reader,
4399 struct dwarf2_cu *cu,
4400 struct dwarf2_section_info *section,
4401 struct dwo_file *dwo_file)
4402 {
4403 gdb_assert (section->readin && section->buffer != NULL);
4404 reader->abfd = section->asection->owner;
4405 reader->cu = cu;
4406 reader->dwo_file = dwo_file;
4407 reader->die_section = section;
4408 reader->buffer = section->buffer;
4409 reader->buffer_end = section->buffer + section->size;
4410 }
4411
4412 /* Subroutine of init_cutu_and_read_dies to simplify it.
4413 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
4414 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
4415 already.
4416
4417 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
4418 from it to the DIE in the DWO. If NULL we are skipping the stub.
4419 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
4420 are filled in with the info of the DIE from the DWO file.
4421 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
4422 provided an abbrev table to use.
4423 The result is non-zero if a valid (non-dummy) DIE was found. */
4424
4425 static int
4426 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
4427 struct dwo_unit *dwo_unit,
4428 int abbrev_table_provided,
4429 struct die_info *stub_comp_unit_die,
4430 struct die_reader_specs *result_reader,
4431 gdb_byte **result_info_ptr,
4432 struct die_info **result_comp_unit_die,
4433 int *result_has_children)
4434 {
4435 struct objfile *objfile = dwarf2_per_objfile->objfile;
4436 struct dwarf2_cu *cu = this_cu->cu;
4437 struct dwarf2_section_info *section;
4438 bfd *abfd;
4439 gdb_byte *begin_info_ptr, *info_ptr;
4440 const char *comp_dir_string;
4441 ULONGEST signature; /* Or dwo_id. */
4442 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
4443 int i,num_extra_attrs;
4444 struct dwarf2_section_info *dwo_abbrev_section;
4445 struct attribute *attr;
4446 struct die_info *comp_unit_die;
4447
4448 /* These attributes aren't processed until later:
4449 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4450 However, the attribute is found in the stub which we won't have later.
4451 In order to not impose this complication on the rest of the code,
4452 we read them here and copy them to the DWO CU/TU die. */
4453
4454 stmt_list = NULL;
4455 low_pc = NULL;
4456 high_pc = NULL;
4457 ranges = NULL;
4458 comp_dir = NULL;
4459
4460 if (stub_comp_unit_die != NULL)
4461 {
4462 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4463 DWO file. */
4464 if (! this_cu->is_debug_types)
4465 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
4466 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
4467 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
4468 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
4469 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
4470
4471 /* There should be a DW_AT_addr_base attribute here (if needed).
4472 We need the value before we can process DW_FORM_GNU_addr_index. */
4473 cu->addr_base = 0;
4474 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
4475 if (attr)
4476 cu->addr_base = DW_UNSND (attr);
4477
4478 /* There should be a DW_AT_ranges_base attribute here (if needed).
4479 We need the value before we can process DW_AT_ranges. */
4480 cu->ranges_base = 0;
4481 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
4482 if (attr)
4483 cu->ranges_base = DW_UNSND (attr);
4484 }
4485
4486 /* Set up for reading the DWO CU/TU. */
4487 cu->dwo_unit = dwo_unit;
4488 section = dwo_unit->section;
4489 dwarf2_read_section (objfile, section);
4490 abfd = section->asection->owner;
4491 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4492 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
4493 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
4494
4495 if (this_cu->is_debug_types)
4496 {
4497 ULONGEST header_signature;
4498 cu_offset type_offset_in_tu;
4499 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
4500
4501 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4502 dwo_abbrev_section,
4503 info_ptr,
4504 &header_signature,
4505 &type_offset_in_tu);
4506 gdb_assert (sig_type->signature == header_signature);
4507 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4508 /* For DWOs coming from DWP files, we don't know the CU length
4509 nor the type's offset in the TU until now. */
4510 dwo_unit->length = get_cu_length (&cu->header);
4511 dwo_unit->type_offset_in_tu = type_offset_in_tu;
4512
4513 /* Establish the type offset that can be used to lookup the type.
4514 For DWO files, we don't know it until now. */
4515 sig_type->type_offset_in_section.sect_off =
4516 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4517 }
4518 else
4519 {
4520 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4521 dwo_abbrev_section,
4522 info_ptr, 0);
4523 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4524 /* For DWOs coming from DWP files, we don't know the CU length
4525 until now. */
4526 dwo_unit->length = get_cu_length (&cu->header);
4527 }
4528
4529 /* Replace the CU's original abbrev table with the DWO's. */
4530 if (abbrev_table_provided)
4531 {
4532 /* Don't free the provided abbrev table, the caller of
4533 init_cutu_and_read_dies owns it. */
4534 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4535 make_cleanup (dwarf2_free_abbrev_table, cu);
4536 }
4537 else
4538 {
4539 dwarf2_free_abbrev_table (cu);
4540 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4541 }
4542
4543 /* Read in the die, but leave space to copy over the attributes
4544 from the stub. This has the benefit of simplifying the rest of
4545 the code - all the work to maintain the illusion of a single
4546 DW_TAG_{compile,type}_unit DIE is done here. */
4547 num_extra_attrs = ((stmt_list != NULL)
4548 + (low_pc != NULL)
4549 + (high_pc != NULL)
4550 + (ranges != NULL)
4551 + (comp_dir != NULL));
4552 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
4553 result_has_children, num_extra_attrs);
4554
4555 /* Copy over the attributes from the stub to the DIE we just read in. */
4556 comp_unit_die = *result_comp_unit_die;
4557 i = comp_unit_die->num_attrs;
4558 if (stmt_list != NULL)
4559 comp_unit_die->attrs[i++] = *stmt_list;
4560 if (low_pc != NULL)
4561 comp_unit_die->attrs[i++] = *low_pc;
4562 if (high_pc != NULL)
4563 comp_unit_die->attrs[i++] = *high_pc;
4564 if (ranges != NULL)
4565 comp_unit_die->attrs[i++] = *ranges;
4566 if (comp_dir != NULL)
4567 comp_unit_die->attrs[i++] = *comp_dir;
4568 comp_unit_die->num_attrs += num_extra_attrs;
4569
4570 if (dwarf2_die_debug)
4571 {
4572 fprintf_unfiltered (gdb_stdlog,
4573 "Read die from %s@0x%x of %s:\n",
4574 bfd_section_name (abfd, section->asection),
4575 (unsigned) (begin_info_ptr - section->buffer),
4576 bfd_get_filename (abfd));
4577 dump_die (comp_unit_die, dwarf2_die_debug);
4578 }
4579
4580 /* Skip dummy compilation units. */
4581 if (info_ptr >= begin_info_ptr + dwo_unit->length
4582 || peek_abbrev_code (abfd, info_ptr) == 0)
4583 return 0;
4584
4585 *result_info_ptr = info_ptr;
4586 return 1;
4587 }
4588
4589 /* Subroutine of init_cutu_and_read_dies to simplify it.
4590 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
4591 If the specified DWO unit cannot be found an error is thrown. */
4592
4593 static struct dwo_unit *
4594 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
4595 struct die_info *comp_unit_die)
4596 {
4597 struct dwarf2_cu *cu = this_cu->cu;
4598 struct attribute *attr;
4599 ULONGEST signature;
4600 struct dwo_unit *dwo_unit;
4601 const char *comp_dir, *dwo_name;
4602
4603 /* Yeah, we look dwo_name up again, but it simplifies the code. */
4604 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4605 gdb_assert (attr != NULL);
4606 dwo_name = DW_STRING (attr);
4607 comp_dir = NULL;
4608 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4609 if (attr)
4610 comp_dir = DW_STRING (attr);
4611
4612 if (this_cu->is_debug_types)
4613 {
4614 struct signatured_type *sig_type;
4615
4616 /* Since this_cu is the first member of struct signatured_type,
4617 we can go from a pointer to one to a pointer to the other. */
4618 sig_type = (struct signatured_type *) this_cu;
4619 signature = sig_type->signature;
4620 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
4621 }
4622 else
4623 {
4624 struct attribute *attr;
4625
4626 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4627 if (! attr)
4628 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
4629 " [in module %s]"),
4630 dwo_name, this_cu->objfile->name);
4631 signature = DW_UNSND (attr);
4632 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
4633 signature);
4634 }
4635
4636 if (dwo_unit == NULL)
4637 {
4638 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
4639 " with ID %s [in module %s]"),
4640 this_cu->offset.sect_off,
4641 phex (signature, sizeof (signature)),
4642 this_cu->objfile->name);
4643 }
4644
4645 return dwo_unit;
4646 }
4647
4648 /* Initialize a CU (or TU) and read its DIEs.
4649 If the CU defers to a DWO file, read the DWO file as well.
4650
4651 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4652 Otherwise the table specified in the comp unit header is read in and used.
4653 This is an optimization for when we already have the abbrev table.
4654
4655 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4656 Otherwise, a new CU is allocated with xmalloc.
4657
4658 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4659 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
4660
4661 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4662 linker) then DIE_READER_FUNC will not get called. */
4663
4664 static void
4665 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
4666 struct abbrev_table *abbrev_table,
4667 int use_existing_cu, int keep,
4668 die_reader_func_ftype *die_reader_func,
4669 void *data)
4670 {
4671 struct objfile *objfile = dwarf2_per_objfile->objfile;
4672 struct dwarf2_section_info *section = this_cu->section;
4673 bfd *abfd = section->asection->owner;
4674 struct dwarf2_cu *cu;
4675 gdb_byte *begin_info_ptr, *info_ptr;
4676 struct die_reader_specs reader;
4677 struct die_info *comp_unit_die;
4678 int has_children;
4679 struct attribute *attr;
4680 struct cleanup *cleanups, *free_cu_cleanup = NULL;
4681 struct signatured_type *sig_type = NULL;
4682 struct dwarf2_section_info *abbrev_section;
4683 /* Non-zero if CU currently points to a DWO file and we need to
4684 reread it. When this happens we need to reread the skeleton die
4685 before we can reread the DWO file. */
4686 int rereading_dwo_cu = 0;
4687
4688 if (dwarf2_die_debug)
4689 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4690 this_cu->is_debug_types ? "type" : "comp",
4691 this_cu->offset.sect_off);
4692
4693 if (use_existing_cu)
4694 gdb_assert (keep);
4695
4696 cleanups = make_cleanup (null_cleanup, NULL);
4697
4698 /* This is cheap if the section is already read in. */
4699 dwarf2_read_section (objfile, section);
4700
4701 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4702
4703 abbrev_section = get_abbrev_section_for_cu (this_cu);
4704
4705 if (use_existing_cu && this_cu->cu != NULL)
4706 {
4707 cu = this_cu->cu;
4708
4709 /* If this CU is from a DWO file we need to start over, we need to
4710 refetch the attributes from the skeleton CU.
4711 This could be optimized by retrieving those attributes from when we
4712 were here the first time: the previous comp_unit_die was stored in
4713 comp_unit_obstack. But there's no data yet that we need this
4714 optimization. */
4715 if (cu->dwo_unit != NULL)
4716 rereading_dwo_cu = 1;
4717 }
4718 else
4719 {
4720 /* If !use_existing_cu, this_cu->cu must be NULL. */
4721 gdb_assert (this_cu->cu == NULL);
4722
4723 cu = xmalloc (sizeof (*cu));
4724 init_one_comp_unit (cu, this_cu);
4725
4726 /* If an error occurs while loading, release our storage. */
4727 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
4728 }
4729
4730 /* Get the header. */
4731 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
4732 {
4733 /* We already have the header, there's no need to read it in again. */
4734 info_ptr += cu->header.first_die_offset.cu_off;
4735 }
4736 else
4737 {
4738 if (this_cu->is_debug_types)
4739 {
4740 ULONGEST signature;
4741 cu_offset type_offset_in_tu;
4742
4743 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4744 abbrev_section, info_ptr,
4745 &signature,
4746 &type_offset_in_tu);
4747
4748 /* Since per_cu is the first member of struct signatured_type,
4749 we can go from a pointer to one to a pointer to the other. */
4750 sig_type = (struct signatured_type *) this_cu;
4751 gdb_assert (sig_type->signature == signature);
4752 gdb_assert (sig_type->type_offset_in_tu.cu_off
4753 == type_offset_in_tu.cu_off);
4754 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4755
4756 /* LENGTH has not been set yet for type units if we're
4757 using .gdb_index. */
4758 this_cu->length = get_cu_length (&cu->header);
4759
4760 /* Establish the type offset that can be used to lookup the type. */
4761 sig_type->type_offset_in_section.sect_off =
4762 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
4763 }
4764 else
4765 {
4766 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4767 abbrev_section,
4768 info_ptr, 0);
4769
4770 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4771 gdb_assert (this_cu->length == get_cu_length (&cu->header));
4772 }
4773 }
4774
4775 /* Skip dummy compilation units. */
4776 if (info_ptr >= begin_info_ptr + this_cu->length
4777 || peek_abbrev_code (abfd, info_ptr) == 0)
4778 {
4779 do_cleanups (cleanups);
4780 return;
4781 }
4782
4783 /* If we don't have them yet, read the abbrevs for this compilation unit.
4784 And if we need to read them now, make sure they're freed when we're
4785 done. Note that it's important that if the CU had an abbrev table
4786 on entry we don't free it when we're done: Somewhere up the call stack
4787 it may be in use. */
4788 if (abbrev_table != NULL)
4789 {
4790 gdb_assert (cu->abbrev_table == NULL);
4791 gdb_assert (cu->header.abbrev_offset.sect_off
4792 == abbrev_table->offset.sect_off);
4793 cu->abbrev_table = abbrev_table;
4794 }
4795 else if (cu->abbrev_table == NULL)
4796 {
4797 dwarf2_read_abbrevs (cu, abbrev_section);
4798 make_cleanup (dwarf2_free_abbrev_table, cu);
4799 }
4800 else if (rereading_dwo_cu)
4801 {
4802 dwarf2_free_abbrev_table (cu);
4803 dwarf2_read_abbrevs (cu, abbrev_section);
4804 }
4805
4806 /* Read the top level CU/TU die. */
4807 init_cu_die_reader (&reader, cu, section, NULL);
4808 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4809
4810 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
4811 from the DWO file.
4812 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
4813 DWO CU, that this test will fail (the attribute will not be present). */
4814 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4815 if (attr)
4816 {
4817 struct dwo_unit *dwo_unit;
4818 struct die_info *dwo_comp_unit_die;
4819
4820 if (has_children)
4821 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
4822 " has children (offset 0x%x) [in module %s]"),
4823 this_cu->offset.sect_off, bfd_get_filename (abfd));
4824 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
4825 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
4826 abbrev_table != NULL,
4827 comp_unit_die,
4828 &reader, &info_ptr,
4829 &dwo_comp_unit_die, &has_children) == 0)
4830 {
4831 /* Dummy die. */
4832 do_cleanups (cleanups);
4833 return;
4834 }
4835 comp_unit_die = dwo_comp_unit_die;
4836 }
4837
4838 /* All of the above is setup for this call. Yikes. */
4839 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4840
4841 /* Done, clean up. */
4842 if (free_cu_cleanup != NULL)
4843 {
4844 if (keep)
4845 {
4846 /* We've successfully allocated this compilation unit. Let our
4847 caller clean it up when finished with it. */
4848 discard_cleanups (free_cu_cleanup);
4849
4850 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4851 So we have to manually free the abbrev table. */
4852 dwarf2_free_abbrev_table (cu);
4853
4854 /* Link this CU into read_in_chain. */
4855 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4856 dwarf2_per_objfile->read_in_chain = this_cu;
4857 }
4858 else
4859 do_cleanups (free_cu_cleanup);
4860 }
4861
4862 do_cleanups (cleanups);
4863 }
4864
4865 /* Read CU/TU THIS_CU in section SECTION,
4866 but do not follow DW_AT_GNU_dwo_name if present.
4867 DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
4868 to have already done the lookup to find the DWO/DWP file).
4869
4870 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
4871 THIS_CU->is_debug_types, but nothing else.
4872
4873 We fill in THIS_CU->length.
4874
4875 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4876 linker) then DIE_READER_FUNC will not get called.
4877
4878 THIS_CU->cu is always freed when done.
4879 This is done in order to not leave THIS_CU->cu in a state where we have
4880 to care whether it refers to the "main" CU or the DWO CU. */
4881
4882 static void
4883 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4884 struct dwarf2_section_info *abbrev_section,
4885 struct dwo_file *dwo_file,
4886 die_reader_func_ftype *die_reader_func,
4887 void *data)
4888 {
4889 struct objfile *objfile = dwarf2_per_objfile->objfile;
4890 struct dwarf2_section_info *section = this_cu->section;
4891 bfd *abfd = section->asection->owner;
4892 struct dwarf2_cu cu;
4893 gdb_byte *begin_info_ptr, *info_ptr;
4894 struct die_reader_specs reader;
4895 struct cleanup *cleanups;
4896 struct die_info *comp_unit_die;
4897 int has_children;
4898
4899 if (dwarf2_die_debug)
4900 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4901 this_cu->is_debug_types ? "type" : "comp",
4902 this_cu->offset.sect_off);
4903
4904 gdb_assert (this_cu->cu == NULL);
4905
4906 /* This is cheap if the section is already read in. */
4907 dwarf2_read_section (objfile, section);
4908
4909 init_one_comp_unit (&cu, this_cu);
4910
4911 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4912
4913 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4914 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
4915 abbrev_section, info_ptr,
4916 this_cu->is_debug_types);
4917
4918 this_cu->length = get_cu_length (&cu.header);
4919
4920 /* Skip dummy compilation units. */
4921 if (info_ptr >= begin_info_ptr + this_cu->length
4922 || peek_abbrev_code (abfd, info_ptr) == 0)
4923 {
4924 do_cleanups (cleanups);
4925 return;
4926 }
4927
4928 dwarf2_read_abbrevs (&cu, abbrev_section);
4929 make_cleanup (dwarf2_free_abbrev_table, &cu);
4930
4931 init_cu_die_reader (&reader, &cu, section, dwo_file);
4932 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4933
4934 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4935
4936 do_cleanups (cleanups);
4937 }
4938
4939 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4940 does not lookup the specified DWO file.
4941 This cannot be used to read DWO files.
4942
4943 THIS_CU->cu is always freed when done.
4944 This is done in order to not leave THIS_CU->cu in a state where we have
4945 to care whether it refers to the "main" CU or the DWO CU.
4946 We can revisit this if the data shows there's a performance issue. */
4947
4948 static void
4949 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4950 die_reader_func_ftype *die_reader_func,
4951 void *data)
4952 {
4953 init_cutu_and_read_dies_no_follow (this_cu,
4954 get_abbrev_section_for_cu (this_cu),
4955 NULL,
4956 die_reader_func, data);
4957 }
4958 \f
4959 /* Type Unit Groups.
4960
4961 Type Unit Groups are a way to collapse the set of all TUs (type units) into
4962 a more manageable set. The grouping is done by DW_AT_stmt_list entry
4963 so that all types coming from the same compilation (.o file) are grouped
4964 together. A future step could be to put the types in the same symtab as
4965 the CU the types ultimately came from. */
4966
4967 static hashval_t
4968 hash_type_unit_group (const void *item)
4969 {
4970 const struct type_unit_group *tu_group = item;
4971
4972 return hash_stmt_list_entry (&tu_group->hash);
4973 }
4974
4975 static int
4976 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
4977 {
4978 const struct type_unit_group *lhs = item_lhs;
4979 const struct type_unit_group *rhs = item_rhs;
4980
4981 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
4982 }
4983
4984 /* Allocate a hash table for type unit groups. */
4985
4986 static htab_t
4987 allocate_type_unit_groups_table (void)
4988 {
4989 return htab_create_alloc_ex (3,
4990 hash_type_unit_group,
4991 eq_type_unit_group,
4992 NULL,
4993 &dwarf2_per_objfile->objfile->objfile_obstack,
4994 hashtab_obstack_allocate,
4995 dummy_obstack_deallocate);
4996 }
4997
4998 /* Type units that don't have DW_AT_stmt_list are grouped into their own
4999 partial symtabs. We combine several TUs per psymtab to not let the size
5000 of any one psymtab grow too big. */
5001 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5002 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
5003
5004 /* Helper routine for get_type_unit_group.
5005 Create the type_unit_group object used to hold one or more TUs. */
5006
5007 static struct type_unit_group *
5008 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
5009 {
5010 struct objfile *objfile = dwarf2_per_objfile->objfile;
5011 struct dwarf2_per_cu_data *per_cu;
5012 struct type_unit_group *tu_group;
5013
5014 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5015 struct type_unit_group);
5016 per_cu = &tu_group->per_cu;
5017 per_cu->objfile = objfile;
5018
5019 if (dwarf2_per_objfile->using_index)
5020 {
5021 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5022 struct dwarf2_per_cu_quick_data);
5023 }
5024 else
5025 {
5026 unsigned int line_offset = line_offset_struct.sect_off;
5027 struct partial_symtab *pst;
5028 char *name;
5029
5030 /* Give the symtab a useful name for debug purposes. */
5031 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5032 name = xstrprintf ("<type_units_%d>",
5033 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5034 else
5035 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5036
5037 pst = create_partial_symtab (per_cu, name);
5038 pst->anonymous = 1;
5039
5040 xfree (name);
5041 }
5042
5043 tu_group->hash.dwo_unit = cu->dwo_unit;
5044 tu_group->hash.line_offset = line_offset_struct;
5045
5046 return tu_group;
5047 }
5048
5049 /* Look up the type_unit_group for type unit CU, and create it if necessary.
5050 STMT_LIST is a DW_AT_stmt_list attribute. */
5051
5052 static struct type_unit_group *
5053 get_type_unit_group (struct dwarf2_cu *cu, struct attribute *stmt_list)
5054 {
5055 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5056 struct type_unit_group *tu_group;
5057 void **slot;
5058 unsigned int line_offset;
5059 struct type_unit_group type_unit_group_for_lookup;
5060
5061 if (dwarf2_per_objfile->type_unit_groups == NULL)
5062 {
5063 dwarf2_per_objfile->type_unit_groups =
5064 allocate_type_unit_groups_table ();
5065 }
5066
5067 /* Do we need to create a new group, or can we use an existing one? */
5068
5069 if (stmt_list)
5070 {
5071 line_offset = DW_UNSND (stmt_list);
5072 ++tu_stats->nr_symtab_sharers;
5073 }
5074 else
5075 {
5076 /* Ugh, no stmt_list. Rare, but we have to handle it.
5077 We can do various things here like create one group per TU or
5078 spread them over multiple groups to split up the expansion work.
5079 To avoid worst case scenarios (too many groups or too large groups)
5080 we, umm, group them in bunches. */
5081 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5082 | (tu_stats->nr_stmt_less_type_units
5083 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5084 ++tu_stats->nr_stmt_less_type_units;
5085 }
5086
5087 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5088 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
5089 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5090 &type_unit_group_for_lookup, INSERT);
5091 if (*slot != NULL)
5092 {
5093 tu_group = *slot;
5094 gdb_assert (tu_group != NULL);
5095 }
5096 else
5097 {
5098 sect_offset line_offset_struct;
5099
5100 line_offset_struct.sect_off = line_offset;
5101 tu_group = create_type_unit_group (cu, line_offset_struct);
5102 *slot = tu_group;
5103 ++tu_stats->nr_symtabs;
5104 }
5105
5106 return tu_group;
5107 }
5108
5109 /* Struct used to sort TUs by their abbreviation table offset. */
5110
5111 struct tu_abbrev_offset
5112 {
5113 struct signatured_type *sig_type;
5114 sect_offset abbrev_offset;
5115 };
5116
5117 /* Helper routine for build_type_unit_groups, passed to qsort. */
5118
5119 static int
5120 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5121 {
5122 const struct tu_abbrev_offset * const *a = ap;
5123 const struct tu_abbrev_offset * const *b = bp;
5124 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5125 unsigned int boff = (*b)->abbrev_offset.sect_off;
5126
5127 return (aoff > boff) - (aoff < boff);
5128 }
5129
5130 /* A helper function to add a type_unit_group to a table. */
5131
5132 static int
5133 add_type_unit_group_to_table (void **slot, void *datum)
5134 {
5135 struct type_unit_group *tu_group = *slot;
5136 struct type_unit_group ***datap = datum;
5137
5138 **datap = tu_group;
5139 ++*datap;
5140
5141 return 1;
5142 }
5143
5144 /* Efficiently read all the type units, calling init_cutu_and_read_dies on
5145 each one passing FUNC,DATA.
5146
5147 The efficiency is because we sort TUs by the abbrev table they use and
5148 only read each abbrev table once. In one program there are 200K TUs
5149 sharing 8K abbrev tables.
5150
5151 The main purpose of this function is to support building the
5152 dwarf2_per_objfile->type_unit_groups table.
5153 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5154 can collapse the search space by grouping them by stmt_list.
5155 The savings can be significant, in the same program from above the 200K TUs
5156 share 8K stmt_list tables.
5157
5158 FUNC is expected to call get_type_unit_group, which will create the
5159 struct type_unit_group if necessary and add it to
5160 dwarf2_per_objfile->type_unit_groups. */
5161
5162 static void
5163 build_type_unit_groups (die_reader_func_ftype *func, void *data)
5164 {
5165 struct objfile *objfile = dwarf2_per_objfile->objfile;
5166 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5167 struct cleanup *cleanups;
5168 struct abbrev_table *abbrev_table;
5169 sect_offset abbrev_offset;
5170 struct tu_abbrev_offset *sorted_by_abbrev;
5171 struct type_unit_group **iter;
5172 int i;
5173
5174 /* It's up to the caller to not call us multiple times. */
5175 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5176
5177 if (dwarf2_per_objfile->n_type_units == 0)
5178 return;
5179
5180 /* TUs typically share abbrev tables, and there can be way more TUs than
5181 abbrev tables. Sort by abbrev table to reduce the number of times we
5182 read each abbrev table in.
5183 Alternatives are to punt or to maintain a cache of abbrev tables.
5184 This is simpler and efficient enough for now.
5185
5186 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5187 symtab to use). Typically TUs with the same abbrev offset have the same
5188 stmt_list value too so in practice this should work well.
5189
5190 The basic algorithm here is:
5191
5192 sort TUs by abbrev table
5193 for each TU with same abbrev table:
5194 read abbrev table if first user
5195 read TU top level DIE
5196 [IWBN if DWO skeletons had DW_AT_stmt_list]
5197 call FUNC */
5198
5199 if (dwarf2_read_debug)
5200 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5201
5202 /* Sort in a separate table to maintain the order of all_type_units
5203 for .gdb_index: TU indices directly index all_type_units. */
5204 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5205 dwarf2_per_objfile->n_type_units);
5206 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5207 {
5208 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5209
5210 sorted_by_abbrev[i].sig_type = sig_type;
5211 sorted_by_abbrev[i].abbrev_offset =
5212 read_abbrev_offset (sig_type->per_cu.section,
5213 sig_type->per_cu.offset);
5214 }
5215 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5216 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5217 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5218
5219 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5220 called any number of times, so we don't reset tu_stats here. */
5221
5222 abbrev_offset.sect_off = ~(unsigned) 0;
5223 abbrev_table = NULL;
5224 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5225
5226 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5227 {
5228 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5229
5230 /* Switch to the next abbrev table if necessary. */
5231 if (abbrev_table == NULL
5232 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5233 {
5234 if (abbrev_table != NULL)
5235 {
5236 abbrev_table_free (abbrev_table);
5237 /* Reset to NULL in case abbrev_table_read_table throws
5238 an error: abbrev_table_free_cleanup will get called. */
5239 abbrev_table = NULL;
5240 }
5241 abbrev_offset = tu->abbrev_offset;
5242 abbrev_table =
5243 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5244 abbrev_offset);
5245 ++tu_stats->nr_uniq_abbrev_tables;
5246 }
5247
5248 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5249 func, data);
5250 }
5251
5252 /* Create a vector of pointers to primary type units to make it easy to
5253 iterate over them and CUs. See dw2_get_primary_cu. */
5254 dwarf2_per_objfile->n_type_unit_groups =
5255 htab_elements (dwarf2_per_objfile->type_unit_groups);
5256 dwarf2_per_objfile->all_type_unit_groups =
5257 obstack_alloc (&objfile->objfile_obstack,
5258 dwarf2_per_objfile->n_type_unit_groups
5259 * sizeof (struct type_unit_group *));
5260 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5261 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5262 add_type_unit_group_to_table, &iter);
5263 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5264 == dwarf2_per_objfile->n_type_unit_groups);
5265
5266 do_cleanups (cleanups);
5267
5268 if (dwarf2_read_debug)
5269 {
5270 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5271 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5272 dwarf2_per_objfile->n_type_units);
5273 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5274 tu_stats->nr_uniq_abbrev_tables);
5275 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5276 tu_stats->nr_symtabs);
5277 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5278 tu_stats->nr_symtab_sharers);
5279 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5280 tu_stats->nr_stmt_less_type_units);
5281 }
5282 }
5283 \f
5284 /* Partial symbol tables. */
5285
5286 /* Create a psymtab named NAME and assign it to PER_CU.
5287
5288 The caller must fill in the following details:
5289 dirname, textlow, texthigh. */
5290
5291 static struct partial_symtab *
5292 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
5293 {
5294 struct objfile *objfile = per_cu->objfile;
5295 struct partial_symtab *pst;
5296
5297 pst = start_psymtab_common (objfile, objfile->section_offsets,
5298 name, 0,
5299 objfile->global_psymbols.next,
5300 objfile->static_psymbols.next);
5301
5302 pst->psymtabs_addrmap_supported = 1;
5303
5304 /* This is the glue that links PST into GDB's symbol API. */
5305 pst->read_symtab_private = per_cu;
5306 pst->read_symtab = dwarf2_read_symtab;
5307 per_cu->v.psymtab = pst;
5308
5309 return pst;
5310 }
5311
5312 /* die_reader_func for process_psymtab_comp_unit. */
5313
5314 static void
5315 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
5316 gdb_byte *info_ptr,
5317 struct die_info *comp_unit_die,
5318 int has_children,
5319 void *data)
5320 {
5321 struct dwarf2_cu *cu = reader->cu;
5322 struct objfile *objfile = cu->objfile;
5323 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5324 struct attribute *attr;
5325 CORE_ADDR baseaddr;
5326 CORE_ADDR best_lowpc = 0, best_highpc = 0;
5327 struct partial_symtab *pst;
5328 int has_pc_info;
5329 const char *filename;
5330 int *want_partial_unit_ptr = data;
5331
5332 if (comp_unit_die->tag == DW_TAG_partial_unit
5333 && (want_partial_unit_ptr == NULL
5334 || !*want_partial_unit_ptr))
5335 return;
5336
5337 gdb_assert (! per_cu->is_debug_types);
5338
5339 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
5340
5341 cu->list_in_scope = &file_symbols;
5342
5343 /* Allocate a new partial symbol table structure. */
5344 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
5345 if (attr == NULL || !DW_STRING (attr))
5346 filename = "";
5347 else
5348 filename = DW_STRING (attr);
5349
5350 pst = create_partial_symtab (per_cu, filename);
5351
5352 /* This must be done before calling dwarf2_build_include_psymtabs. */
5353 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
5354 if (attr != NULL)
5355 pst->dirname = DW_STRING (attr);
5356
5357 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5358
5359 dwarf2_find_base_address (comp_unit_die, cu);
5360
5361 /* Possibly set the default values of LOWPC and HIGHPC from
5362 `DW_AT_ranges'. */
5363 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
5364 &best_highpc, cu, pst);
5365 if (has_pc_info == 1 && best_lowpc < best_highpc)
5366 /* Store the contiguous range if it is not empty; it can be empty for
5367 CUs with no code. */
5368 addrmap_set_empty (objfile->psymtabs_addrmap,
5369 best_lowpc + baseaddr,
5370 best_highpc + baseaddr - 1, pst);
5371
5372 /* Check if comp unit has_children.
5373 If so, read the rest of the partial symbols from this comp unit.
5374 If not, there's no more debug_info for this comp unit. */
5375 if (has_children)
5376 {
5377 struct partial_die_info *first_die;
5378 CORE_ADDR lowpc, highpc;
5379
5380 lowpc = ((CORE_ADDR) -1);
5381 highpc = ((CORE_ADDR) 0);
5382
5383 first_die = load_partial_dies (reader, info_ptr, 1);
5384
5385 scan_partial_symbols (first_die, &lowpc, &highpc,
5386 ! has_pc_info, cu);
5387
5388 /* If we didn't find a lowpc, set it to highpc to avoid
5389 complaints from `maint check'. */
5390 if (lowpc == ((CORE_ADDR) -1))
5391 lowpc = highpc;
5392
5393 /* If the compilation unit didn't have an explicit address range,
5394 then use the information extracted from its child dies. */
5395 if (! has_pc_info)
5396 {
5397 best_lowpc = lowpc;
5398 best_highpc = highpc;
5399 }
5400 }
5401 pst->textlow = best_lowpc + baseaddr;
5402 pst->texthigh = best_highpc + baseaddr;
5403
5404 pst->n_global_syms = objfile->global_psymbols.next -
5405 (objfile->global_psymbols.list + pst->globals_offset);
5406 pst->n_static_syms = objfile->static_psymbols.next -
5407 (objfile->static_psymbols.list + pst->statics_offset);
5408 sort_pst_symbols (objfile, pst);
5409
5410 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
5411 {
5412 int i;
5413 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5414 struct dwarf2_per_cu_data *iter;
5415
5416 /* Fill in 'dependencies' here; we fill in 'users' in a
5417 post-pass. */
5418 pst->number_of_dependencies = len;
5419 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5420 len * sizeof (struct symtab *));
5421 for (i = 0;
5422 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
5423 i, iter);
5424 ++i)
5425 pst->dependencies[i] = iter->v.psymtab;
5426
5427 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5428 }
5429
5430 /* Get the list of files included in the current compilation unit,
5431 and build a psymtab for each of them. */
5432 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
5433
5434 if (dwarf2_read_debug)
5435 {
5436 struct gdbarch *gdbarch = get_objfile_arch (objfile);
5437
5438 fprintf_unfiltered (gdb_stdlog,
5439 "Psymtab for %s unit @0x%x: %s - %s"
5440 ", %d global, %d static syms\n",
5441 per_cu->is_debug_types ? "type" : "comp",
5442 per_cu->offset.sect_off,
5443 paddress (gdbarch, pst->textlow),
5444 paddress (gdbarch, pst->texthigh),
5445 pst->n_global_syms, pst->n_static_syms);
5446 }
5447 }
5448
5449 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5450 Process compilation unit THIS_CU for a psymtab. */
5451
5452 static void
5453 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
5454 int want_partial_unit)
5455 {
5456 /* If this compilation unit was already read in, free the
5457 cached copy in order to read it in again. This is
5458 necessary because we skipped some symbols when we first
5459 read in the compilation unit (see load_partial_dies).
5460 This problem could be avoided, but the benefit is unclear. */
5461 if (this_cu->cu != NULL)
5462 free_one_cached_comp_unit (this_cu);
5463
5464 gdb_assert (! this_cu->is_debug_types);
5465 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
5466 process_psymtab_comp_unit_reader,
5467 &want_partial_unit);
5468
5469 /* Age out any secondary CUs. */
5470 age_cached_comp_units ();
5471 }
5472
5473 /* Reader function for build_type_psymtabs. */
5474
5475 static void
5476 build_type_psymtabs_reader (const struct die_reader_specs *reader,
5477 gdb_byte *info_ptr,
5478 struct die_info *type_unit_die,
5479 int has_children,
5480 void *data)
5481 {
5482 struct objfile *objfile = dwarf2_per_objfile->objfile;
5483 struct dwarf2_cu *cu = reader->cu;
5484 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5485 struct signatured_type *sig_type;
5486 struct type_unit_group *tu_group;
5487 struct attribute *attr;
5488 struct partial_die_info *first_die;
5489 CORE_ADDR lowpc, highpc;
5490 struct partial_symtab *pst;
5491
5492 gdb_assert (data == NULL);
5493 gdb_assert (per_cu->is_debug_types);
5494 sig_type = (struct signatured_type *) per_cu;
5495
5496 if (! has_children)
5497 return;
5498
5499 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
5500 tu_group = get_type_unit_group (cu, attr);
5501
5502 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
5503
5504 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5505 cu->list_in_scope = &file_symbols;
5506 pst = create_partial_symtab (per_cu, "");
5507 pst->anonymous = 1;
5508
5509 first_die = load_partial_dies (reader, info_ptr, 1);
5510
5511 lowpc = (CORE_ADDR) -1;
5512 highpc = (CORE_ADDR) 0;
5513 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5514
5515 pst->n_global_syms = objfile->global_psymbols.next -
5516 (objfile->global_psymbols.list + pst->globals_offset);
5517 pst->n_static_syms = objfile->static_psymbols.next -
5518 (objfile->static_psymbols.list + pst->statics_offset);
5519 sort_pst_symbols (objfile, pst);
5520 }
5521
5522 /* Traversal function for build_type_psymtabs. */
5523
5524 static int
5525 build_type_psymtab_dependencies (void **slot, void *info)
5526 {
5527 struct objfile *objfile = dwarf2_per_objfile->objfile;
5528 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
5529 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
5530 struct partial_symtab *pst = per_cu->v.psymtab;
5531 int len = VEC_length (sig_type_ptr, tu_group->tus);
5532 struct signatured_type *iter;
5533 int i;
5534
5535 gdb_assert (len > 0);
5536 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
5537
5538 pst->number_of_dependencies = len;
5539 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5540 len * sizeof (struct psymtab *));
5541 for (i = 0;
5542 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
5543 ++i)
5544 {
5545 gdb_assert (iter->per_cu.is_debug_types);
5546 pst->dependencies[i] = iter->per_cu.v.psymtab;
5547 iter->type_unit_group = tu_group;
5548 }
5549
5550 VEC_free (sig_type_ptr, tu_group->tus);
5551
5552 return 1;
5553 }
5554
5555 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5556 Build partial symbol tables for the .debug_types comp-units. */
5557
5558 static void
5559 build_type_psymtabs (struct objfile *objfile)
5560 {
5561 if (! create_all_type_units (objfile))
5562 return;
5563
5564 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5565
5566 /* Now that all TUs have been processed we can fill in the dependencies. */
5567 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5568 build_type_psymtab_dependencies, NULL);
5569 }
5570
5571 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
5572
5573 static void
5574 psymtabs_addrmap_cleanup (void *o)
5575 {
5576 struct objfile *objfile = o;
5577
5578 objfile->psymtabs_addrmap = NULL;
5579 }
5580
5581 /* Compute the 'user' field for each psymtab in OBJFILE. */
5582
5583 static void
5584 set_partial_user (struct objfile *objfile)
5585 {
5586 int i;
5587
5588 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5589 {
5590 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5591 struct partial_symtab *pst = per_cu->v.psymtab;
5592 int j;
5593
5594 if (pst == NULL)
5595 continue;
5596
5597 for (j = 0; j < pst->number_of_dependencies; ++j)
5598 {
5599 /* Set the 'user' field only if it is not already set. */
5600 if (pst->dependencies[j]->user == NULL)
5601 pst->dependencies[j]->user = pst;
5602 }
5603 }
5604 }
5605
5606 /* Build the partial symbol table by doing a quick pass through the
5607 .debug_info and .debug_abbrev sections. */
5608
5609 static void
5610 dwarf2_build_psymtabs_hard (struct objfile *objfile)
5611 {
5612 struct cleanup *back_to, *addrmap_cleanup;
5613 struct obstack temp_obstack;
5614 int i;
5615
5616 if (dwarf2_read_debug)
5617 {
5618 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5619 objfile->name);
5620 }
5621
5622 dwarf2_per_objfile->reading_partial_symbols = 1;
5623
5624 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
5625
5626 /* Any cached compilation units will be linked by the per-objfile
5627 read_in_chain. Make sure to free them when we're done. */
5628 back_to = make_cleanup (free_cached_comp_units, NULL);
5629
5630 build_type_psymtabs (objfile);
5631
5632 create_all_comp_units (objfile);
5633
5634 /* Create a temporary address map on a temporary obstack. We later
5635 copy this to the final obstack. */
5636 obstack_init (&temp_obstack);
5637 make_cleanup_obstack_free (&temp_obstack);
5638 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
5639 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
5640
5641 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5642 {
5643 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5644
5645 process_psymtab_comp_unit (per_cu, 0);
5646 }
5647
5648 set_partial_user (objfile);
5649
5650 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
5651 &objfile->objfile_obstack);
5652 discard_cleanups (addrmap_cleanup);
5653
5654 do_cleanups (back_to);
5655
5656 if (dwarf2_read_debug)
5657 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
5658 objfile->name);
5659 }
5660
5661 /* die_reader_func for load_partial_comp_unit. */
5662
5663 static void
5664 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
5665 gdb_byte *info_ptr,
5666 struct die_info *comp_unit_die,
5667 int has_children,
5668 void *data)
5669 {
5670 struct dwarf2_cu *cu = reader->cu;
5671
5672 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
5673
5674 /* Check if comp unit has_children.
5675 If so, read the rest of the partial symbols from this comp unit.
5676 If not, there's no more debug_info for this comp unit. */
5677 if (has_children)
5678 load_partial_dies (reader, info_ptr, 0);
5679 }
5680
5681 /* Load the partial DIEs for a secondary CU into memory.
5682 This is also used when rereading a primary CU with load_all_dies. */
5683
5684 static void
5685 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
5686 {
5687 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
5688 load_partial_comp_unit_reader, NULL);
5689 }
5690
5691 static void
5692 read_comp_units_from_section (struct objfile *objfile,
5693 struct dwarf2_section_info *section,
5694 unsigned int is_dwz,
5695 int *n_allocated,
5696 int *n_comp_units,
5697 struct dwarf2_per_cu_data ***all_comp_units)
5698 {
5699 gdb_byte *info_ptr;
5700 bfd *abfd = section->asection->owner;
5701
5702 if (dwarf2_read_debug)
5703 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
5704 section->asection->name, bfd_get_filename (abfd));
5705
5706 dwarf2_read_section (objfile, section);
5707
5708 info_ptr = section->buffer;
5709
5710 while (info_ptr < section->buffer + section->size)
5711 {
5712 unsigned int length, initial_length_size;
5713 struct dwarf2_per_cu_data *this_cu;
5714 sect_offset offset;
5715
5716 offset.sect_off = info_ptr - section->buffer;
5717
5718 /* Read just enough information to find out where the next
5719 compilation unit is. */
5720 length = read_initial_length (abfd, info_ptr, &initial_length_size);
5721
5722 /* Save the compilation unit for later lookup. */
5723 this_cu = obstack_alloc (&objfile->objfile_obstack,
5724 sizeof (struct dwarf2_per_cu_data));
5725 memset (this_cu, 0, sizeof (*this_cu));
5726 this_cu->offset = offset;
5727 this_cu->length = length + initial_length_size;
5728 this_cu->is_dwz = is_dwz;
5729 this_cu->objfile = objfile;
5730 this_cu->section = section;
5731
5732 if (*n_comp_units == *n_allocated)
5733 {
5734 *n_allocated *= 2;
5735 *all_comp_units = xrealloc (*all_comp_units,
5736 *n_allocated
5737 * sizeof (struct dwarf2_per_cu_data *));
5738 }
5739 (*all_comp_units)[*n_comp_units] = this_cu;
5740 ++*n_comp_units;
5741
5742 info_ptr = info_ptr + this_cu->length;
5743 }
5744 }
5745
5746 /* Create a list of all compilation units in OBJFILE.
5747 This is only done for -readnow and building partial symtabs. */
5748
5749 static void
5750 create_all_comp_units (struct objfile *objfile)
5751 {
5752 int n_allocated;
5753 int n_comp_units;
5754 struct dwarf2_per_cu_data **all_comp_units;
5755
5756 n_comp_units = 0;
5757 n_allocated = 10;
5758 all_comp_units = xmalloc (n_allocated
5759 * sizeof (struct dwarf2_per_cu_data *));
5760
5761 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
5762 &n_allocated, &n_comp_units, &all_comp_units);
5763
5764 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
5765 {
5766 struct dwz_file *dwz = dwarf2_get_dwz_file ();
5767
5768 read_comp_units_from_section (objfile, &dwz->info, 1,
5769 &n_allocated, &n_comp_units,
5770 &all_comp_units);
5771 }
5772
5773 dwarf2_per_objfile->all_comp_units
5774 = obstack_alloc (&objfile->objfile_obstack,
5775 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5776 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
5777 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5778 xfree (all_comp_units);
5779 dwarf2_per_objfile->n_comp_units = n_comp_units;
5780 }
5781
5782 /* Process all loaded DIEs for compilation unit CU, starting at
5783 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
5784 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
5785 DW_AT_ranges). If NEED_PC is set, then this function will set
5786 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
5787 and record the covered ranges in the addrmap. */
5788
5789 static void
5790 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5791 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
5792 {
5793 struct partial_die_info *pdi;
5794
5795 /* Now, march along the PDI's, descending into ones which have
5796 interesting children but skipping the children of the other ones,
5797 until we reach the end of the compilation unit. */
5798
5799 pdi = first_die;
5800
5801 while (pdi != NULL)
5802 {
5803 fixup_partial_die (pdi, cu);
5804
5805 /* Anonymous namespaces or modules have no name but have interesting
5806 children, so we need to look at them. Ditto for anonymous
5807 enums. */
5808
5809 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
5810 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
5811 || pdi->tag == DW_TAG_imported_unit)
5812 {
5813 switch (pdi->tag)
5814 {
5815 case DW_TAG_subprogram:
5816 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
5817 break;
5818 case DW_TAG_constant:
5819 case DW_TAG_variable:
5820 case DW_TAG_typedef:
5821 case DW_TAG_union_type:
5822 if (!pdi->is_declaration)
5823 {
5824 add_partial_symbol (pdi, cu);
5825 }
5826 break;
5827 case DW_TAG_class_type:
5828 case DW_TAG_interface_type:
5829 case DW_TAG_structure_type:
5830 if (!pdi->is_declaration)
5831 {
5832 add_partial_symbol (pdi, cu);
5833 }
5834 break;
5835 case DW_TAG_enumeration_type:
5836 if (!pdi->is_declaration)
5837 add_partial_enumeration (pdi, cu);
5838 break;
5839 case DW_TAG_base_type:
5840 case DW_TAG_subrange_type:
5841 /* File scope base type definitions are added to the partial
5842 symbol table. */
5843 add_partial_symbol (pdi, cu);
5844 break;
5845 case DW_TAG_namespace:
5846 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
5847 break;
5848 case DW_TAG_module:
5849 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
5850 break;
5851 case DW_TAG_imported_unit:
5852 {
5853 struct dwarf2_per_cu_data *per_cu;
5854
5855 /* For now we don't handle imported units in type units. */
5856 if (cu->per_cu->is_debug_types)
5857 {
5858 error (_("Dwarf Error: DW_TAG_imported_unit is not"
5859 " supported in type units [in module %s]"),
5860 cu->objfile->name);
5861 }
5862
5863 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
5864 pdi->is_dwz,
5865 cu->objfile);
5866
5867 /* Go read the partial unit, if needed. */
5868 if (per_cu->v.psymtab == NULL)
5869 process_psymtab_comp_unit (per_cu, 1);
5870
5871 VEC_safe_push (dwarf2_per_cu_ptr,
5872 cu->per_cu->imported_symtabs, per_cu);
5873 }
5874 break;
5875 default:
5876 break;
5877 }
5878 }
5879
5880 /* If the die has a sibling, skip to the sibling. */
5881
5882 pdi = pdi->die_sibling;
5883 }
5884 }
5885
5886 /* Functions used to compute the fully scoped name of a partial DIE.
5887
5888 Normally, this is simple. For C++, the parent DIE's fully scoped
5889 name is concatenated with "::" and the partial DIE's name. For
5890 Java, the same thing occurs except that "." is used instead of "::".
5891 Enumerators are an exception; they use the scope of their parent
5892 enumeration type, i.e. the name of the enumeration type is not
5893 prepended to the enumerator.
5894
5895 There are two complexities. One is DW_AT_specification; in this
5896 case "parent" means the parent of the target of the specification,
5897 instead of the direct parent of the DIE. The other is compilers
5898 which do not emit DW_TAG_namespace; in this case we try to guess
5899 the fully qualified name of structure types from their members'
5900 linkage names. This must be done using the DIE's children rather
5901 than the children of any DW_AT_specification target. We only need
5902 to do this for structures at the top level, i.e. if the target of
5903 any DW_AT_specification (if any; otherwise the DIE itself) does not
5904 have a parent. */
5905
5906 /* Compute the scope prefix associated with PDI's parent, in
5907 compilation unit CU. The result will be allocated on CU's
5908 comp_unit_obstack, or a copy of the already allocated PDI->NAME
5909 field. NULL is returned if no prefix is necessary. */
5910 static const char *
5911 partial_die_parent_scope (struct partial_die_info *pdi,
5912 struct dwarf2_cu *cu)
5913 {
5914 const char *grandparent_scope;
5915 struct partial_die_info *parent, *real_pdi;
5916
5917 /* We need to look at our parent DIE; if we have a DW_AT_specification,
5918 then this means the parent of the specification DIE. */
5919
5920 real_pdi = pdi;
5921 while (real_pdi->has_specification)
5922 real_pdi = find_partial_die (real_pdi->spec_offset,
5923 real_pdi->spec_is_dwz, cu);
5924
5925 parent = real_pdi->die_parent;
5926 if (parent == NULL)
5927 return NULL;
5928
5929 if (parent->scope_set)
5930 return parent->scope;
5931
5932 fixup_partial_die (parent, cu);
5933
5934 grandparent_scope = partial_die_parent_scope (parent, cu);
5935
5936 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
5937 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
5938 Work around this problem here. */
5939 if (cu->language == language_cplus
5940 && parent->tag == DW_TAG_namespace
5941 && strcmp (parent->name, "::") == 0
5942 && grandparent_scope == NULL)
5943 {
5944 parent->scope = NULL;
5945 parent->scope_set = 1;
5946 return NULL;
5947 }
5948
5949 if (pdi->tag == DW_TAG_enumerator)
5950 /* Enumerators should not get the name of the enumeration as a prefix. */
5951 parent->scope = grandparent_scope;
5952 else if (parent->tag == DW_TAG_namespace
5953 || parent->tag == DW_TAG_module
5954 || parent->tag == DW_TAG_structure_type
5955 || parent->tag == DW_TAG_class_type
5956 || parent->tag == DW_TAG_interface_type
5957 || parent->tag == DW_TAG_union_type
5958 || parent->tag == DW_TAG_enumeration_type)
5959 {
5960 if (grandparent_scope == NULL)
5961 parent->scope = parent->name;
5962 else
5963 parent->scope = typename_concat (&cu->comp_unit_obstack,
5964 grandparent_scope,
5965 parent->name, 0, cu);
5966 }
5967 else
5968 {
5969 /* FIXME drow/2004-04-01: What should we be doing with
5970 function-local names? For partial symbols, we should probably be
5971 ignoring them. */
5972 complaint (&symfile_complaints,
5973 _("unhandled containing DIE tag %d for DIE at %d"),
5974 parent->tag, pdi->offset.sect_off);
5975 parent->scope = grandparent_scope;
5976 }
5977
5978 parent->scope_set = 1;
5979 return parent->scope;
5980 }
5981
5982 /* Return the fully scoped name associated with PDI, from compilation unit
5983 CU. The result will be allocated with malloc. */
5984
5985 static char *
5986 partial_die_full_name (struct partial_die_info *pdi,
5987 struct dwarf2_cu *cu)
5988 {
5989 const char *parent_scope;
5990
5991 /* If this is a template instantiation, we can not work out the
5992 template arguments from partial DIEs. So, unfortunately, we have
5993 to go through the full DIEs. At least any work we do building
5994 types here will be reused if full symbols are loaded later. */
5995 if (pdi->has_template_arguments)
5996 {
5997 fixup_partial_die (pdi, cu);
5998
5999 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
6000 {
6001 struct die_info *die;
6002 struct attribute attr;
6003 struct dwarf2_cu *ref_cu = cu;
6004
6005 /* DW_FORM_ref_addr is using section offset. */
6006 attr.name = 0;
6007 attr.form = DW_FORM_ref_addr;
6008 attr.u.unsnd = pdi->offset.sect_off;
6009 die = follow_die_ref (NULL, &attr, &ref_cu);
6010
6011 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
6012 }
6013 }
6014
6015 parent_scope = partial_die_parent_scope (pdi, cu);
6016 if (parent_scope == NULL)
6017 return NULL;
6018 else
6019 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
6020 }
6021
6022 static void
6023 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
6024 {
6025 struct objfile *objfile = cu->objfile;
6026 CORE_ADDR addr = 0;
6027 const char *actual_name = NULL;
6028 CORE_ADDR baseaddr;
6029 char *built_actual_name;
6030
6031 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6032
6033 built_actual_name = partial_die_full_name (pdi, cu);
6034 if (built_actual_name != NULL)
6035 actual_name = built_actual_name;
6036
6037 if (actual_name == NULL)
6038 actual_name = pdi->name;
6039
6040 switch (pdi->tag)
6041 {
6042 case DW_TAG_subprogram:
6043 if (pdi->is_external || cu->language == language_ada)
6044 {
6045 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
6046 of the global scope. But in Ada, we want to be able to access
6047 nested procedures globally. So all Ada subprograms are stored
6048 in the global scope. */
6049 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
6050 mst_text, objfile); */
6051 add_psymbol_to_list (actual_name, strlen (actual_name),
6052 built_actual_name != NULL,
6053 VAR_DOMAIN, LOC_BLOCK,
6054 &objfile->global_psymbols,
6055 0, pdi->lowpc + baseaddr,
6056 cu->language, objfile);
6057 }
6058 else
6059 {
6060 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
6061 mst_file_text, objfile); */
6062 add_psymbol_to_list (actual_name, strlen (actual_name),
6063 built_actual_name != NULL,
6064 VAR_DOMAIN, LOC_BLOCK,
6065 &objfile->static_psymbols,
6066 0, pdi->lowpc + baseaddr,
6067 cu->language, objfile);
6068 }
6069 break;
6070 case DW_TAG_constant:
6071 {
6072 struct psymbol_allocation_list *list;
6073
6074 if (pdi->is_external)
6075 list = &objfile->global_psymbols;
6076 else
6077 list = &objfile->static_psymbols;
6078 add_psymbol_to_list (actual_name, strlen (actual_name),
6079 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
6080 list, 0, 0, cu->language, objfile);
6081 }
6082 break;
6083 case DW_TAG_variable:
6084 if (pdi->d.locdesc)
6085 addr = decode_locdesc (pdi->d.locdesc, cu);
6086
6087 if (pdi->d.locdesc
6088 && addr == 0
6089 && !dwarf2_per_objfile->has_section_at_zero)
6090 {
6091 /* A global or static variable may also have been stripped
6092 out by the linker if unused, in which case its address
6093 will be nullified; do not add such variables into partial
6094 symbol table then. */
6095 }
6096 else if (pdi->is_external)
6097 {
6098 /* Global Variable.
6099 Don't enter into the minimal symbol tables as there is
6100 a minimal symbol table entry from the ELF symbols already.
6101 Enter into partial symbol table if it has a location
6102 descriptor or a type.
6103 If the location descriptor is missing, new_symbol will create
6104 a LOC_UNRESOLVED symbol, the address of the variable will then
6105 be determined from the minimal symbol table whenever the variable
6106 is referenced.
6107 The address for the partial symbol table entry is not
6108 used by GDB, but it comes in handy for debugging partial symbol
6109 table building. */
6110
6111 if (pdi->d.locdesc || pdi->has_type)
6112 add_psymbol_to_list (actual_name, strlen (actual_name),
6113 built_actual_name != NULL,
6114 VAR_DOMAIN, LOC_STATIC,
6115 &objfile->global_psymbols,
6116 0, addr + baseaddr,
6117 cu->language, objfile);
6118 }
6119 else
6120 {
6121 /* Static Variable. Skip symbols without location descriptors. */
6122 if (pdi->d.locdesc == NULL)
6123 {
6124 xfree (built_actual_name);
6125 return;
6126 }
6127 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
6128 mst_file_data, objfile); */
6129 add_psymbol_to_list (actual_name, strlen (actual_name),
6130 built_actual_name != NULL,
6131 VAR_DOMAIN, LOC_STATIC,
6132 &objfile->static_psymbols,
6133 0, addr + baseaddr,
6134 cu->language, objfile);
6135 }
6136 break;
6137 case DW_TAG_typedef:
6138 case DW_TAG_base_type:
6139 case DW_TAG_subrange_type:
6140 add_psymbol_to_list (actual_name, strlen (actual_name),
6141 built_actual_name != NULL,
6142 VAR_DOMAIN, LOC_TYPEDEF,
6143 &objfile->static_psymbols,
6144 0, (CORE_ADDR) 0, cu->language, objfile);
6145 break;
6146 case DW_TAG_namespace:
6147 add_psymbol_to_list (actual_name, strlen (actual_name),
6148 built_actual_name != NULL,
6149 VAR_DOMAIN, LOC_TYPEDEF,
6150 &objfile->global_psymbols,
6151 0, (CORE_ADDR) 0, cu->language, objfile);
6152 break;
6153 case DW_TAG_class_type:
6154 case DW_TAG_interface_type:
6155 case DW_TAG_structure_type:
6156 case DW_TAG_union_type:
6157 case DW_TAG_enumeration_type:
6158 /* Skip external references. The DWARF standard says in the section
6159 about "Structure, Union, and Class Type Entries": "An incomplete
6160 structure, union or class type is represented by a structure,
6161 union or class entry that does not have a byte size attribute
6162 and that has a DW_AT_declaration attribute." */
6163 if (!pdi->has_byte_size && pdi->is_declaration)
6164 {
6165 xfree (built_actual_name);
6166 return;
6167 }
6168
6169 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
6170 static vs. global. */
6171 add_psymbol_to_list (actual_name, strlen (actual_name),
6172 built_actual_name != NULL,
6173 STRUCT_DOMAIN, LOC_TYPEDEF,
6174 (cu->language == language_cplus
6175 || cu->language == language_java)
6176 ? &objfile->global_psymbols
6177 : &objfile->static_psymbols,
6178 0, (CORE_ADDR) 0, cu->language, objfile);
6179
6180 break;
6181 case DW_TAG_enumerator:
6182 add_psymbol_to_list (actual_name, strlen (actual_name),
6183 built_actual_name != NULL,
6184 VAR_DOMAIN, LOC_CONST,
6185 (cu->language == language_cplus
6186 || cu->language == language_java)
6187 ? &objfile->global_psymbols
6188 : &objfile->static_psymbols,
6189 0, (CORE_ADDR) 0, cu->language, objfile);
6190 break;
6191 default:
6192 break;
6193 }
6194
6195 xfree (built_actual_name);
6196 }
6197
6198 /* Read a partial die corresponding to a namespace; also, add a symbol
6199 corresponding to that namespace to the symbol table. NAMESPACE is
6200 the name of the enclosing namespace. */
6201
6202 static void
6203 add_partial_namespace (struct partial_die_info *pdi,
6204 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6205 int need_pc, struct dwarf2_cu *cu)
6206 {
6207 /* Add a symbol for the namespace. */
6208
6209 add_partial_symbol (pdi, cu);
6210
6211 /* Now scan partial symbols in that namespace. */
6212
6213 if (pdi->has_children)
6214 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6215 }
6216
6217 /* Read a partial die corresponding to a Fortran module. */
6218
6219 static void
6220 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
6221 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6222 {
6223 /* Now scan partial symbols in that module. */
6224
6225 if (pdi->has_children)
6226 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6227 }
6228
6229 /* Read a partial die corresponding to a subprogram and create a partial
6230 symbol for that subprogram. When the CU language allows it, this
6231 routine also defines a partial symbol for each nested subprogram
6232 that this subprogram contains.
6233
6234 DIE my also be a lexical block, in which case we simply search
6235 recursively for suprograms defined inside that lexical block.
6236 Again, this is only performed when the CU language allows this
6237 type of definitions. */
6238
6239 static void
6240 add_partial_subprogram (struct partial_die_info *pdi,
6241 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6242 int need_pc, struct dwarf2_cu *cu)
6243 {
6244 if (pdi->tag == DW_TAG_subprogram)
6245 {
6246 if (pdi->has_pc_info)
6247 {
6248 if (pdi->lowpc < *lowpc)
6249 *lowpc = pdi->lowpc;
6250 if (pdi->highpc > *highpc)
6251 *highpc = pdi->highpc;
6252 if (need_pc)
6253 {
6254 CORE_ADDR baseaddr;
6255 struct objfile *objfile = cu->objfile;
6256
6257 baseaddr = ANOFFSET (objfile->section_offsets,
6258 SECT_OFF_TEXT (objfile));
6259 addrmap_set_empty (objfile->psymtabs_addrmap,
6260 pdi->lowpc + baseaddr,
6261 pdi->highpc - 1 + baseaddr,
6262 cu->per_cu->v.psymtab);
6263 }
6264 }
6265
6266 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6267 {
6268 if (!pdi->is_declaration)
6269 /* Ignore subprogram DIEs that do not have a name, they are
6270 illegal. Do not emit a complaint at this point, we will
6271 do so when we convert this psymtab into a symtab. */
6272 if (pdi->name)
6273 add_partial_symbol (pdi, cu);
6274 }
6275 }
6276
6277 if (! pdi->has_children)
6278 return;
6279
6280 if (cu->language == language_ada)
6281 {
6282 pdi = pdi->die_child;
6283 while (pdi != NULL)
6284 {
6285 fixup_partial_die (pdi, cu);
6286 if (pdi->tag == DW_TAG_subprogram
6287 || pdi->tag == DW_TAG_lexical_block)
6288 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
6289 pdi = pdi->die_sibling;
6290 }
6291 }
6292 }
6293
6294 /* Read a partial die corresponding to an enumeration type. */
6295
6296 static void
6297 add_partial_enumeration (struct partial_die_info *enum_pdi,
6298 struct dwarf2_cu *cu)
6299 {
6300 struct partial_die_info *pdi;
6301
6302 if (enum_pdi->name != NULL)
6303 add_partial_symbol (enum_pdi, cu);
6304
6305 pdi = enum_pdi->die_child;
6306 while (pdi)
6307 {
6308 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
6309 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
6310 else
6311 add_partial_symbol (pdi, cu);
6312 pdi = pdi->die_sibling;
6313 }
6314 }
6315
6316 /* Return the initial uleb128 in the die at INFO_PTR. */
6317
6318 static unsigned int
6319 peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
6320 {
6321 unsigned int bytes_read;
6322
6323 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6324 }
6325
6326 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6327 Return the corresponding abbrev, or NULL if the number is zero (indicating
6328 an empty DIE). In either case *BYTES_READ will be set to the length of
6329 the initial number. */
6330
6331 static struct abbrev_info *
6332 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
6333 struct dwarf2_cu *cu)
6334 {
6335 bfd *abfd = cu->objfile->obfd;
6336 unsigned int abbrev_number;
6337 struct abbrev_info *abbrev;
6338
6339 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6340
6341 if (abbrev_number == 0)
6342 return NULL;
6343
6344 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
6345 if (!abbrev)
6346 {
6347 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6348 abbrev_number, bfd_get_filename (abfd));
6349 }
6350
6351 return abbrev;
6352 }
6353
6354 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6355 Returns a pointer to the end of a series of DIEs, terminated by an empty
6356 DIE. Any children of the skipped DIEs will also be skipped. */
6357
6358 static gdb_byte *
6359 skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
6360 {
6361 struct dwarf2_cu *cu = reader->cu;
6362 struct abbrev_info *abbrev;
6363 unsigned int bytes_read;
6364
6365 while (1)
6366 {
6367 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6368 if (abbrev == NULL)
6369 return info_ptr + bytes_read;
6370 else
6371 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
6372 }
6373 }
6374
6375 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6376 INFO_PTR should point just after the initial uleb128 of a DIE, and the
6377 abbrev corresponding to that skipped uleb128 should be passed in
6378 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6379 children. */
6380
6381 static gdb_byte *
6382 skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
6383 struct abbrev_info *abbrev)
6384 {
6385 unsigned int bytes_read;
6386 struct attribute attr;
6387 bfd *abfd = reader->abfd;
6388 struct dwarf2_cu *cu = reader->cu;
6389 gdb_byte *buffer = reader->buffer;
6390 const gdb_byte *buffer_end = reader->buffer_end;
6391 gdb_byte *start_info_ptr = info_ptr;
6392 unsigned int form, i;
6393
6394 for (i = 0; i < abbrev->num_attrs; i++)
6395 {
6396 /* The only abbrev we care about is DW_AT_sibling. */
6397 if (abbrev->attrs[i].name == DW_AT_sibling)
6398 {
6399 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
6400 if (attr.form == DW_FORM_ref_addr)
6401 complaint (&symfile_complaints,
6402 _("ignoring absolute DW_AT_sibling"));
6403 else
6404 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
6405 }
6406
6407 /* If it isn't DW_AT_sibling, skip this attribute. */
6408 form = abbrev->attrs[i].form;
6409 skip_attribute:
6410 switch (form)
6411 {
6412 case DW_FORM_ref_addr:
6413 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6414 and later it is offset sized. */
6415 if (cu->header.version == 2)
6416 info_ptr += cu->header.addr_size;
6417 else
6418 info_ptr += cu->header.offset_size;
6419 break;
6420 case DW_FORM_GNU_ref_alt:
6421 info_ptr += cu->header.offset_size;
6422 break;
6423 case DW_FORM_addr:
6424 info_ptr += cu->header.addr_size;
6425 break;
6426 case DW_FORM_data1:
6427 case DW_FORM_ref1:
6428 case DW_FORM_flag:
6429 info_ptr += 1;
6430 break;
6431 case DW_FORM_flag_present:
6432 break;
6433 case DW_FORM_data2:
6434 case DW_FORM_ref2:
6435 info_ptr += 2;
6436 break;
6437 case DW_FORM_data4:
6438 case DW_FORM_ref4:
6439 info_ptr += 4;
6440 break;
6441 case DW_FORM_data8:
6442 case DW_FORM_ref8:
6443 case DW_FORM_ref_sig8:
6444 info_ptr += 8;
6445 break;
6446 case DW_FORM_string:
6447 read_direct_string (abfd, info_ptr, &bytes_read);
6448 info_ptr += bytes_read;
6449 break;
6450 case DW_FORM_sec_offset:
6451 case DW_FORM_strp:
6452 case DW_FORM_GNU_strp_alt:
6453 info_ptr += cu->header.offset_size;
6454 break;
6455 case DW_FORM_exprloc:
6456 case DW_FORM_block:
6457 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6458 info_ptr += bytes_read;
6459 break;
6460 case DW_FORM_block1:
6461 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6462 break;
6463 case DW_FORM_block2:
6464 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6465 break;
6466 case DW_FORM_block4:
6467 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6468 break;
6469 case DW_FORM_sdata:
6470 case DW_FORM_udata:
6471 case DW_FORM_ref_udata:
6472 case DW_FORM_GNU_addr_index:
6473 case DW_FORM_GNU_str_index:
6474 info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
6475 break;
6476 case DW_FORM_indirect:
6477 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6478 info_ptr += bytes_read;
6479 /* We need to continue parsing from here, so just go back to
6480 the top. */
6481 goto skip_attribute;
6482
6483 default:
6484 error (_("Dwarf Error: Cannot handle %s "
6485 "in DWARF reader [in module %s]"),
6486 dwarf_form_name (form),
6487 bfd_get_filename (abfd));
6488 }
6489 }
6490
6491 if (abbrev->has_children)
6492 return skip_children (reader, info_ptr);
6493 else
6494 return info_ptr;
6495 }
6496
6497 /* Locate ORIG_PDI's sibling.
6498 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
6499
6500 static gdb_byte *
6501 locate_pdi_sibling (const struct die_reader_specs *reader,
6502 struct partial_die_info *orig_pdi,
6503 gdb_byte *info_ptr)
6504 {
6505 /* Do we know the sibling already? */
6506
6507 if (orig_pdi->sibling)
6508 return orig_pdi->sibling;
6509
6510 /* Are there any children to deal with? */
6511
6512 if (!orig_pdi->has_children)
6513 return info_ptr;
6514
6515 /* Skip the children the long way. */
6516
6517 return skip_children (reader, info_ptr);
6518 }
6519
6520 /* Expand this partial symbol table into a full symbol table. SELF is
6521 not NULL. */
6522
6523 static void
6524 dwarf2_read_symtab (struct partial_symtab *self,
6525 struct objfile *objfile)
6526 {
6527 if (self->readin)
6528 {
6529 warning (_("bug: psymtab for %s is already read in."),
6530 self->filename);
6531 }
6532 else
6533 {
6534 if (info_verbose)
6535 {
6536 printf_filtered (_("Reading in symbols for %s..."),
6537 self->filename);
6538 gdb_flush (gdb_stdout);
6539 }
6540
6541 /* Restore our global data. */
6542 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
6543
6544 /* If this psymtab is constructed from a debug-only objfile, the
6545 has_section_at_zero flag will not necessarily be correct. We
6546 can get the correct value for this flag by looking at the data
6547 associated with the (presumably stripped) associated objfile. */
6548 if (objfile->separate_debug_objfile_backlink)
6549 {
6550 struct dwarf2_per_objfile *dpo_backlink
6551 = objfile_data (objfile->separate_debug_objfile_backlink,
6552 dwarf2_objfile_data_key);
6553
6554 dwarf2_per_objfile->has_section_at_zero
6555 = dpo_backlink->has_section_at_zero;
6556 }
6557
6558 dwarf2_per_objfile->reading_partial_symbols = 0;
6559
6560 psymtab_to_symtab_1 (self);
6561
6562 /* Finish up the debug error message. */
6563 if (info_verbose)
6564 printf_filtered (_("done.\n"));
6565 }
6566
6567 process_cu_includes ();
6568 }
6569 \f
6570 /* Reading in full CUs. */
6571
6572 /* Add PER_CU to the queue. */
6573
6574 static void
6575 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6576 enum language pretend_language)
6577 {
6578 struct dwarf2_queue_item *item;
6579
6580 per_cu->queued = 1;
6581 item = xmalloc (sizeof (*item));
6582 item->per_cu = per_cu;
6583 item->pretend_language = pretend_language;
6584 item->next = NULL;
6585
6586 if (dwarf2_queue == NULL)
6587 dwarf2_queue = item;
6588 else
6589 dwarf2_queue_tail->next = item;
6590
6591 dwarf2_queue_tail = item;
6592 }
6593
6594 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
6595 unit and add it to our queue.
6596 The result is non-zero if PER_CU was queued, otherwise the result is zero
6597 meaning either PER_CU is already queued or it is already loaded. */
6598
6599 static int
6600 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6601 struct dwarf2_per_cu_data *per_cu,
6602 enum language pretend_language)
6603 {
6604 /* We may arrive here during partial symbol reading, if we need full
6605 DIEs to process an unusual case (e.g. template arguments). Do
6606 not queue PER_CU, just tell our caller to load its DIEs. */
6607 if (dwarf2_per_objfile->reading_partial_symbols)
6608 {
6609 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6610 return 1;
6611 return 0;
6612 }
6613
6614 /* Mark the dependence relation so that we don't flush PER_CU
6615 too early. */
6616 dwarf2_add_dependence (this_cu, per_cu);
6617
6618 /* If it's already on the queue, we have nothing to do. */
6619 if (per_cu->queued)
6620 return 0;
6621
6622 /* If the compilation unit is already loaded, just mark it as
6623 used. */
6624 if (per_cu->cu != NULL)
6625 {
6626 per_cu->cu->last_used = 0;
6627 return 0;
6628 }
6629
6630 /* Add it to the queue. */
6631 queue_comp_unit (per_cu, pretend_language);
6632
6633 return 1;
6634 }
6635
6636 /* Process the queue. */
6637
6638 static void
6639 process_queue (void)
6640 {
6641 struct dwarf2_queue_item *item, *next_item;
6642
6643 if (dwarf2_read_debug)
6644 {
6645 fprintf_unfiltered (gdb_stdlog,
6646 "Expanding one or more symtabs of objfile %s ...\n",
6647 dwarf2_per_objfile->objfile->name);
6648 }
6649
6650 /* The queue starts out with one item, but following a DIE reference
6651 may load a new CU, adding it to the end of the queue. */
6652 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
6653 {
6654 if (dwarf2_per_objfile->using_index
6655 ? !item->per_cu->v.quick->symtab
6656 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
6657 {
6658 struct dwarf2_per_cu_data *per_cu = item->per_cu;
6659
6660 if (dwarf2_read_debug)
6661 {
6662 fprintf_unfiltered (gdb_stdlog,
6663 "Expanding symtab of %s at offset 0x%x\n",
6664 per_cu->is_debug_types ? "TU" : "CU",
6665 per_cu->offset.sect_off);
6666 }
6667
6668 if (per_cu->is_debug_types)
6669 process_full_type_unit (per_cu, item->pretend_language);
6670 else
6671 process_full_comp_unit (per_cu, item->pretend_language);
6672
6673 if (dwarf2_read_debug)
6674 {
6675 fprintf_unfiltered (gdb_stdlog,
6676 "Done expanding %s at offset 0x%x\n",
6677 per_cu->is_debug_types ? "TU" : "CU",
6678 per_cu->offset.sect_off);
6679 }
6680 }
6681
6682 item->per_cu->queued = 0;
6683 next_item = item->next;
6684 xfree (item);
6685 }
6686
6687 dwarf2_queue_tail = NULL;
6688
6689 if (dwarf2_read_debug)
6690 {
6691 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
6692 dwarf2_per_objfile->objfile->name);
6693 }
6694 }
6695
6696 /* Free all allocated queue entries. This function only releases anything if
6697 an error was thrown; if the queue was processed then it would have been
6698 freed as we went along. */
6699
6700 static void
6701 dwarf2_release_queue (void *dummy)
6702 {
6703 struct dwarf2_queue_item *item, *last;
6704
6705 item = dwarf2_queue;
6706 while (item)
6707 {
6708 /* Anything still marked queued is likely to be in an
6709 inconsistent state, so discard it. */
6710 if (item->per_cu->queued)
6711 {
6712 if (item->per_cu->cu != NULL)
6713 free_one_cached_comp_unit (item->per_cu);
6714 item->per_cu->queued = 0;
6715 }
6716
6717 last = item;
6718 item = item->next;
6719 xfree (last);
6720 }
6721
6722 dwarf2_queue = dwarf2_queue_tail = NULL;
6723 }
6724
6725 /* Read in full symbols for PST, and anything it depends on. */
6726
6727 static void
6728 psymtab_to_symtab_1 (struct partial_symtab *pst)
6729 {
6730 struct dwarf2_per_cu_data *per_cu;
6731 int i;
6732
6733 if (pst->readin)
6734 return;
6735
6736 for (i = 0; i < pst->number_of_dependencies; i++)
6737 if (!pst->dependencies[i]->readin
6738 && pst->dependencies[i]->user == NULL)
6739 {
6740 /* Inform about additional files that need to be read in. */
6741 if (info_verbose)
6742 {
6743 /* FIXME: i18n: Need to make this a single string. */
6744 fputs_filtered (" ", gdb_stdout);
6745 wrap_here ("");
6746 fputs_filtered ("and ", gdb_stdout);
6747 wrap_here ("");
6748 printf_filtered ("%s...", pst->dependencies[i]->filename);
6749 wrap_here (""); /* Flush output. */
6750 gdb_flush (gdb_stdout);
6751 }
6752 psymtab_to_symtab_1 (pst->dependencies[i]);
6753 }
6754
6755 per_cu = pst->read_symtab_private;
6756
6757 if (per_cu == NULL)
6758 {
6759 /* It's an include file, no symbols to read for it.
6760 Everything is in the parent symtab. */
6761 pst->readin = 1;
6762 return;
6763 }
6764
6765 dw2_do_instantiate_symtab (per_cu);
6766 }
6767
6768 /* Trivial hash function for die_info: the hash value of a DIE
6769 is its offset in .debug_info for this objfile. */
6770
6771 static hashval_t
6772 die_hash (const void *item)
6773 {
6774 const struct die_info *die = item;
6775
6776 return die->offset.sect_off;
6777 }
6778
6779 /* Trivial comparison function for die_info structures: two DIEs
6780 are equal if they have the same offset. */
6781
6782 static int
6783 die_eq (const void *item_lhs, const void *item_rhs)
6784 {
6785 const struct die_info *die_lhs = item_lhs;
6786 const struct die_info *die_rhs = item_rhs;
6787
6788 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
6789 }
6790
6791 /* die_reader_func for load_full_comp_unit.
6792 This is identical to read_signatured_type_reader,
6793 but is kept separate for now. */
6794
6795 static void
6796 load_full_comp_unit_reader (const struct die_reader_specs *reader,
6797 gdb_byte *info_ptr,
6798 struct die_info *comp_unit_die,
6799 int has_children,
6800 void *data)
6801 {
6802 struct dwarf2_cu *cu = reader->cu;
6803 enum language *language_ptr = data;
6804
6805 gdb_assert (cu->die_hash == NULL);
6806 cu->die_hash =
6807 htab_create_alloc_ex (cu->header.length / 12,
6808 die_hash,
6809 die_eq,
6810 NULL,
6811 &cu->comp_unit_obstack,
6812 hashtab_obstack_allocate,
6813 dummy_obstack_deallocate);
6814
6815 if (has_children)
6816 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
6817 &info_ptr, comp_unit_die);
6818 cu->dies = comp_unit_die;
6819 /* comp_unit_die is not stored in die_hash, no need. */
6820
6821 /* We try not to read any attributes in this function, because not
6822 all CUs needed for references have been loaded yet, and symbol
6823 table processing isn't initialized. But we have to set the CU language,
6824 or we won't be able to build types correctly.
6825 Similarly, if we do not read the producer, we can not apply
6826 producer-specific interpretation. */
6827 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
6828 }
6829
6830 /* Load the DIEs associated with PER_CU into memory. */
6831
6832 static void
6833 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
6834 enum language pretend_language)
6835 {
6836 gdb_assert (! this_cu->is_debug_types);
6837
6838 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6839 load_full_comp_unit_reader, &pretend_language);
6840 }
6841
6842 /* Add a DIE to the delayed physname list. */
6843
6844 static void
6845 add_to_method_list (struct type *type, int fnfield_index, int index,
6846 const char *name, struct die_info *die,
6847 struct dwarf2_cu *cu)
6848 {
6849 struct delayed_method_info mi;
6850 mi.type = type;
6851 mi.fnfield_index = fnfield_index;
6852 mi.index = index;
6853 mi.name = name;
6854 mi.die = die;
6855 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
6856 }
6857
6858 /* A cleanup for freeing the delayed method list. */
6859
6860 static void
6861 free_delayed_list (void *ptr)
6862 {
6863 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
6864 if (cu->method_list != NULL)
6865 {
6866 VEC_free (delayed_method_info, cu->method_list);
6867 cu->method_list = NULL;
6868 }
6869 }
6870
6871 /* Compute the physnames of any methods on the CU's method list.
6872
6873 The computation of method physnames is delayed in order to avoid the
6874 (bad) condition that one of the method's formal parameters is of an as yet
6875 incomplete type. */
6876
6877 static void
6878 compute_delayed_physnames (struct dwarf2_cu *cu)
6879 {
6880 int i;
6881 struct delayed_method_info *mi;
6882 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
6883 {
6884 const char *physname;
6885 struct fn_fieldlist *fn_flp
6886 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
6887 physname = dwarf2_physname (mi->name, mi->die, cu);
6888 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
6889 }
6890 }
6891
6892 /* Go objects should be embedded in a DW_TAG_module DIE,
6893 and it's not clear if/how imported objects will appear.
6894 To keep Go support simple until that's worked out,
6895 go back through what we've read and create something usable.
6896 We could do this while processing each DIE, and feels kinda cleaner,
6897 but that way is more invasive.
6898 This is to, for example, allow the user to type "p var" or "b main"
6899 without having to specify the package name, and allow lookups
6900 of module.object to work in contexts that use the expression
6901 parser. */
6902
6903 static void
6904 fixup_go_packaging (struct dwarf2_cu *cu)
6905 {
6906 char *package_name = NULL;
6907 struct pending *list;
6908 int i;
6909
6910 for (list = global_symbols; list != NULL; list = list->next)
6911 {
6912 for (i = 0; i < list->nsyms; ++i)
6913 {
6914 struct symbol *sym = list->symbol[i];
6915
6916 if (SYMBOL_LANGUAGE (sym) == language_go
6917 && SYMBOL_CLASS (sym) == LOC_BLOCK)
6918 {
6919 char *this_package_name = go_symbol_package_name (sym);
6920
6921 if (this_package_name == NULL)
6922 continue;
6923 if (package_name == NULL)
6924 package_name = this_package_name;
6925 else
6926 {
6927 if (strcmp (package_name, this_package_name) != 0)
6928 complaint (&symfile_complaints,
6929 _("Symtab %s has objects from two different Go packages: %s and %s"),
6930 (SYMBOL_SYMTAB (sym)
6931 ? symtab_to_filename_for_display (SYMBOL_SYMTAB (sym))
6932 : cu->objfile->name),
6933 this_package_name, package_name);
6934 xfree (this_package_name);
6935 }
6936 }
6937 }
6938 }
6939
6940 if (package_name != NULL)
6941 {
6942 struct objfile *objfile = cu->objfile;
6943 const char *saved_package_name = obstack_copy0 (&objfile->objfile_obstack,
6944 package_name,
6945 strlen (package_name));
6946 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
6947 saved_package_name, objfile);
6948 struct symbol *sym;
6949
6950 TYPE_TAG_NAME (type) = TYPE_NAME (type);
6951
6952 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
6953 SYMBOL_SET_LANGUAGE (sym, language_go);
6954 SYMBOL_SET_NAMES (sym, saved_package_name,
6955 strlen (saved_package_name), 0, objfile);
6956 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
6957 e.g., "main" finds the "main" module and not C's main(). */
6958 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
6959 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
6960 SYMBOL_TYPE (sym) = type;
6961
6962 add_symbol_to_list (sym, &global_symbols);
6963
6964 xfree (package_name);
6965 }
6966 }
6967
6968 /* Return the symtab for PER_CU. This works properly regardless of
6969 whether we're using the index or psymtabs. */
6970
6971 static struct symtab *
6972 get_symtab (struct dwarf2_per_cu_data *per_cu)
6973 {
6974 return (dwarf2_per_objfile->using_index
6975 ? per_cu->v.quick->symtab
6976 : per_cu->v.psymtab->symtab);
6977 }
6978
6979 /* A helper function for computing the list of all symbol tables
6980 included by PER_CU. */
6981
6982 static void
6983 recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
6984 htab_t all_children,
6985 struct dwarf2_per_cu_data *per_cu)
6986 {
6987 void **slot;
6988 int ix;
6989 struct dwarf2_per_cu_data *iter;
6990
6991 slot = htab_find_slot (all_children, per_cu, INSERT);
6992 if (*slot != NULL)
6993 {
6994 /* This inclusion and its children have been processed. */
6995 return;
6996 }
6997
6998 *slot = per_cu;
6999 /* Only add a CU if it has a symbol table. */
7000 if (get_symtab (per_cu) != NULL)
7001 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
7002
7003 for (ix = 0;
7004 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
7005 ++ix)
7006 recursively_compute_inclusions (result, all_children, iter);
7007 }
7008
7009 /* Compute the symtab 'includes' fields for the symtab related to
7010 PER_CU. */
7011
7012 static void
7013 compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
7014 {
7015 gdb_assert (! per_cu->is_debug_types);
7016
7017 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
7018 {
7019 int ix, len;
7020 struct dwarf2_per_cu_data *iter;
7021 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
7022 htab_t all_children;
7023 struct symtab *symtab = get_symtab (per_cu);
7024
7025 /* If we don't have a symtab, we can just skip this case. */
7026 if (symtab == NULL)
7027 return;
7028
7029 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
7030 NULL, xcalloc, xfree);
7031
7032 for (ix = 0;
7033 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
7034 ix, iter);
7035 ++ix)
7036 recursively_compute_inclusions (&result_children, all_children, iter);
7037
7038 /* Now we have a transitive closure of all the included CUs, and
7039 for .gdb_index version 7 the included TUs, so we can convert it
7040 to a list of symtabs. */
7041 len = VEC_length (dwarf2_per_cu_ptr, result_children);
7042 symtab->includes
7043 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
7044 (len + 1) * sizeof (struct symtab *));
7045 for (ix = 0;
7046 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
7047 ++ix)
7048 symtab->includes[ix] = get_symtab (iter);
7049 symtab->includes[len] = NULL;
7050
7051 VEC_free (dwarf2_per_cu_ptr, result_children);
7052 htab_delete (all_children);
7053 }
7054 }
7055
7056 /* Compute the 'includes' field for the symtabs of all the CUs we just
7057 read. */
7058
7059 static void
7060 process_cu_includes (void)
7061 {
7062 int ix;
7063 struct dwarf2_per_cu_data *iter;
7064
7065 for (ix = 0;
7066 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
7067 ix, iter);
7068 ++ix)
7069 {
7070 if (! iter->is_debug_types)
7071 compute_symtab_includes (iter);
7072 }
7073
7074 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
7075 }
7076
7077 /* Generate full symbol information for PER_CU, whose DIEs have
7078 already been loaded into memory. */
7079
7080 static void
7081 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
7082 enum language pretend_language)
7083 {
7084 struct dwarf2_cu *cu = per_cu->cu;
7085 struct objfile *objfile = per_cu->objfile;
7086 CORE_ADDR lowpc, highpc;
7087 struct symtab *symtab;
7088 struct cleanup *back_to, *delayed_list_cleanup;
7089 CORE_ADDR baseaddr;
7090 struct block *static_block;
7091
7092 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7093
7094 buildsym_init ();
7095 back_to = make_cleanup (really_free_pendings, NULL);
7096 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7097
7098 cu->list_in_scope = &file_symbols;
7099
7100 cu->language = pretend_language;
7101 cu->language_defn = language_def (cu->language);
7102
7103 /* Do line number decoding in read_file_scope () */
7104 process_die (cu->dies, cu);
7105
7106 /* For now fudge the Go package. */
7107 if (cu->language == language_go)
7108 fixup_go_packaging (cu);
7109
7110 /* Now that we have processed all the DIEs in the CU, all the types
7111 should be complete, and it should now be safe to compute all of the
7112 physnames. */
7113 compute_delayed_physnames (cu);
7114 do_cleanups (delayed_list_cleanup);
7115
7116 /* Some compilers don't define a DW_AT_high_pc attribute for the
7117 compilation unit. If the DW_AT_high_pc is missing, synthesize
7118 it, by scanning the DIE's below the compilation unit. */
7119 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
7120
7121 static_block
7122 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0,
7123 per_cu->imported_symtabs != NULL);
7124
7125 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
7126 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
7127 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
7128 addrmap to help ensure it has an accurate map of pc values belonging to
7129 this comp unit. */
7130 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
7131
7132 symtab = end_symtab_from_static_block (static_block, objfile,
7133 SECT_OFF_TEXT (objfile), 0);
7134
7135 if (symtab != NULL)
7136 {
7137 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
7138
7139 /* Set symtab language to language from DW_AT_language. If the
7140 compilation is from a C file generated by language preprocessors, do
7141 not set the language if it was already deduced by start_subfile. */
7142 if (!(cu->language == language_c && symtab->language != language_c))
7143 symtab->language = cu->language;
7144
7145 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
7146 produce DW_AT_location with location lists but it can be possibly
7147 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
7148 there were bugs in prologue debug info, fixed later in GCC-4.5
7149 by "unwind info for epilogues" patch (which is not directly related).
7150
7151 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
7152 needed, it would be wrong due to missing DW_AT_producer there.
7153
7154 Still one can confuse GDB by using non-standard GCC compilation
7155 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
7156 */
7157 if (cu->has_loclist && gcc_4_minor >= 5)
7158 symtab->locations_valid = 1;
7159
7160 if (gcc_4_minor >= 5)
7161 symtab->epilogue_unwind_valid = 1;
7162
7163 symtab->call_site_htab = cu->call_site_htab;
7164 }
7165
7166 if (dwarf2_per_objfile->using_index)
7167 per_cu->v.quick->symtab = symtab;
7168 else
7169 {
7170 struct partial_symtab *pst = per_cu->v.psymtab;
7171 pst->symtab = symtab;
7172 pst->readin = 1;
7173 }
7174
7175 /* Push it for inclusion processing later. */
7176 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
7177
7178 do_cleanups (back_to);
7179 }
7180
7181 /* Generate full symbol information for type unit PER_CU, whose DIEs have
7182 already been loaded into memory. */
7183
7184 static void
7185 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
7186 enum language pretend_language)
7187 {
7188 struct dwarf2_cu *cu = per_cu->cu;
7189 struct objfile *objfile = per_cu->objfile;
7190 struct symtab *symtab;
7191 struct cleanup *back_to, *delayed_list_cleanup;
7192 struct signatured_type *sig_type;
7193
7194 gdb_assert (per_cu->is_debug_types);
7195 sig_type = (struct signatured_type *) per_cu;
7196
7197 buildsym_init ();
7198 back_to = make_cleanup (really_free_pendings, NULL);
7199 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7200
7201 cu->list_in_scope = &file_symbols;
7202
7203 cu->language = pretend_language;
7204 cu->language_defn = language_def (cu->language);
7205
7206 /* The symbol tables are set up in read_type_unit_scope. */
7207 process_die (cu->dies, cu);
7208
7209 /* For now fudge the Go package. */
7210 if (cu->language == language_go)
7211 fixup_go_packaging (cu);
7212
7213 /* Now that we have processed all the DIEs in the CU, all the types
7214 should be complete, and it should now be safe to compute all of the
7215 physnames. */
7216 compute_delayed_physnames (cu);
7217 do_cleanups (delayed_list_cleanup);
7218
7219 /* TUs share symbol tables.
7220 If this is the first TU to use this symtab, complete the construction
7221 of it with end_expandable_symtab. Otherwise, complete the addition of
7222 this TU's symbols to the existing symtab. */
7223 if (sig_type->type_unit_group->primary_symtab == NULL)
7224 {
7225 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
7226 sig_type->type_unit_group->primary_symtab = symtab;
7227
7228 if (symtab != NULL)
7229 {
7230 /* Set symtab language to language from DW_AT_language. If the
7231 compilation is from a C file generated by language preprocessors,
7232 do not set the language if it was already deduced by
7233 start_subfile. */
7234 if (!(cu->language == language_c && symtab->language != language_c))
7235 symtab->language = cu->language;
7236 }
7237 }
7238 else
7239 {
7240 augment_type_symtab (objfile,
7241 sig_type->type_unit_group->primary_symtab);
7242 symtab = sig_type->type_unit_group->primary_symtab;
7243 }
7244
7245 if (dwarf2_per_objfile->using_index)
7246 per_cu->v.quick->symtab = symtab;
7247 else
7248 {
7249 struct partial_symtab *pst = per_cu->v.psymtab;
7250 pst->symtab = symtab;
7251 pst->readin = 1;
7252 }
7253
7254 do_cleanups (back_to);
7255 }
7256
7257 /* Process an imported unit DIE. */
7258
7259 static void
7260 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7261 {
7262 struct attribute *attr;
7263
7264 /* For now we don't handle imported units in type units. */
7265 if (cu->per_cu->is_debug_types)
7266 {
7267 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7268 " supported in type units [in module %s]"),
7269 cu->objfile->name);
7270 }
7271
7272 attr = dwarf2_attr (die, DW_AT_import, cu);
7273 if (attr != NULL)
7274 {
7275 struct dwarf2_per_cu_data *per_cu;
7276 struct symtab *imported_symtab;
7277 sect_offset offset;
7278 int is_dwz;
7279
7280 offset = dwarf2_get_ref_die_offset (attr);
7281 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7282 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
7283
7284 /* Queue the unit, if needed. */
7285 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7286 load_full_comp_unit (per_cu, cu->language);
7287
7288 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
7289 per_cu);
7290 }
7291 }
7292
7293 /* Process a die and its children. */
7294
7295 static void
7296 process_die (struct die_info *die, struct dwarf2_cu *cu)
7297 {
7298 switch (die->tag)
7299 {
7300 case DW_TAG_padding:
7301 break;
7302 case DW_TAG_compile_unit:
7303 case DW_TAG_partial_unit:
7304 read_file_scope (die, cu);
7305 break;
7306 case DW_TAG_type_unit:
7307 read_type_unit_scope (die, cu);
7308 break;
7309 case DW_TAG_subprogram:
7310 case DW_TAG_inlined_subroutine:
7311 read_func_scope (die, cu);
7312 break;
7313 case DW_TAG_lexical_block:
7314 case DW_TAG_try_block:
7315 case DW_TAG_catch_block:
7316 read_lexical_block_scope (die, cu);
7317 break;
7318 case DW_TAG_GNU_call_site:
7319 read_call_site_scope (die, cu);
7320 break;
7321 case DW_TAG_class_type:
7322 case DW_TAG_interface_type:
7323 case DW_TAG_structure_type:
7324 case DW_TAG_union_type:
7325 process_structure_scope (die, cu);
7326 break;
7327 case DW_TAG_enumeration_type:
7328 process_enumeration_scope (die, cu);
7329 break;
7330
7331 /* These dies have a type, but processing them does not create
7332 a symbol or recurse to process the children. Therefore we can
7333 read them on-demand through read_type_die. */
7334 case DW_TAG_subroutine_type:
7335 case DW_TAG_set_type:
7336 case DW_TAG_array_type:
7337 case DW_TAG_pointer_type:
7338 case DW_TAG_ptr_to_member_type:
7339 case DW_TAG_reference_type:
7340 case DW_TAG_string_type:
7341 break;
7342
7343 case DW_TAG_base_type:
7344 case DW_TAG_subrange_type:
7345 case DW_TAG_typedef:
7346 /* Add a typedef symbol for the type definition, if it has a
7347 DW_AT_name. */
7348 new_symbol (die, read_type_die (die, cu), cu);
7349 break;
7350 case DW_TAG_common_block:
7351 read_common_block (die, cu);
7352 break;
7353 case DW_TAG_common_inclusion:
7354 break;
7355 case DW_TAG_namespace:
7356 cu->processing_has_namespace_info = 1;
7357 read_namespace (die, cu);
7358 break;
7359 case DW_TAG_module:
7360 cu->processing_has_namespace_info = 1;
7361 read_module (die, cu);
7362 break;
7363 case DW_TAG_imported_declaration:
7364 case DW_TAG_imported_module:
7365 cu->processing_has_namespace_info = 1;
7366 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7367 || cu->language != language_fortran))
7368 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7369 dwarf_tag_name (die->tag));
7370 read_import_statement (die, cu);
7371 break;
7372
7373 case DW_TAG_imported_unit:
7374 process_imported_unit_die (die, cu);
7375 break;
7376
7377 default:
7378 new_symbol (die, NULL, cu);
7379 break;
7380 }
7381 }
7382 \f
7383 /* DWARF name computation. */
7384
7385 /* A helper function for dwarf2_compute_name which determines whether DIE
7386 needs to have the name of the scope prepended to the name listed in the
7387 die. */
7388
7389 static int
7390 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7391 {
7392 struct attribute *attr;
7393
7394 switch (die->tag)
7395 {
7396 case DW_TAG_namespace:
7397 case DW_TAG_typedef:
7398 case DW_TAG_class_type:
7399 case DW_TAG_interface_type:
7400 case DW_TAG_structure_type:
7401 case DW_TAG_union_type:
7402 case DW_TAG_enumeration_type:
7403 case DW_TAG_enumerator:
7404 case DW_TAG_subprogram:
7405 case DW_TAG_member:
7406 return 1;
7407
7408 case DW_TAG_variable:
7409 case DW_TAG_constant:
7410 /* We only need to prefix "globally" visible variables. These include
7411 any variable marked with DW_AT_external or any variable that
7412 lives in a namespace. [Variables in anonymous namespaces
7413 require prefixing, but they are not DW_AT_external.] */
7414
7415 if (dwarf2_attr (die, DW_AT_specification, cu))
7416 {
7417 struct dwarf2_cu *spec_cu = cu;
7418
7419 return die_needs_namespace (die_specification (die, &spec_cu),
7420 spec_cu);
7421 }
7422
7423 attr = dwarf2_attr (die, DW_AT_external, cu);
7424 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7425 && die->parent->tag != DW_TAG_module)
7426 return 0;
7427 /* A variable in a lexical block of some kind does not need a
7428 namespace, even though in C++ such variables may be external
7429 and have a mangled name. */
7430 if (die->parent->tag == DW_TAG_lexical_block
7431 || die->parent->tag == DW_TAG_try_block
7432 || die->parent->tag == DW_TAG_catch_block
7433 || die->parent->tag == DW_TAG_subprogram)
7434 return 0;
7435 return 1;
7436
7437 default:
7438 return 0;
7439 }
7440 }
7441
7442 /* Retrieve the last character from a mem_file. */
7443
7444 static void
7445 do_ui_file_peek_last (void *object, const char *buffer, long length)
7446 {
7447 char *last_char_p = (char *) object;
7448
7449 if (length > 0)
7450 *last_char_p = buffer[length - 1];
7451 }
7452
7453 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
7454 compute the physname for the object, which include a method's:
7455 - formal parameters (C++/Java),
7456 - receiver type (Go),
7457 - return type (Java).
7458
7459 The term "physname" is a bit confusing.
7460 For C++, for example, it is the demangled name.
7461 For Go, for example, it's the mangled name.
7462
7463 For Ada, return the DIE's linkage name rather than the fully qualified
7464 name. PHYSNAME is ignored..
7465
7466 The result is allocated on the objfile_obstack and canonicalized. */
7467
7468 static const char *
7469 dwarf2_compute_name (const char *name,
7470 struct die_info *die, struct dwarf2_cu *cu,
7471 int physname)
7472 {
7473 struct objfile *objfile = cu->objfile;
7474
7475 if (name == NULL)
7476 name = dwarf2_name (die, cu);
7477
7478 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7479 compute it by typename_concat inside GDB. */
7480 if (cu->language == language_ada
7481 || (cu->language == language_fortran && physname))
7482 {
7483 /* For Ada unit, we prefer the linkage name over the name, as
7484 the former contains the exported name, which the user expects
7485 to be able to reference. Ideally, we want the user to be able
7486 to reference this entity using either natural or linkage name,
7487 but we haven't started looking at this enhancement yet. */
7488 struct attribute *attr;
7489
7490 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7491 if (attr == NULL)
7492 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7493 if (attr && DW_STRING (attr))
7494 return DW_STRING (attr);
7495 }
7496
7497 /* These are the only languages we know how to qualify names in. */
7498 if (name != NULL
7499 && (cu->language == language_cplus || cu->language == language_java
7500 || cu->language == language_fortran))
7501 {
7502 if (die_needs_namespace (die, cu))
7503 {
7504 long length;
7505 const char *prefix;
7506 struct ui_file *buf;
7507
7508 prefix = determine_prefix (die, cu);
7509 buf = mem_fileopen ();
7510 if (*prefix != '\0')
7511 {
7512 char *prefixed_name = typename_concat (NULL, prefix, name,
7513 physname, cu);
7514
7515 fputs_unfiltered (prefixed_name, buf);
7516 xfree (prefixed_name);
7517 }
7518 else
7519 fputs_unfiltered (name, buf);
7520
7521 /* Template parameters may be specified in the DIE's DW_AT_name, or
7522 as children with DW_TAG_template_type_param or
7523 DW_TAG_value_type_param. If the latter, add them to the name
7524 here. If the name already has template parameters, then
7525 skip this step; some versions of GCC emit both, and
7526 it is more efficient to use the pre-computed name.
7527
7528 Something to keep in mind about this process: it is very
7529 unlikely, or in some cases downright impossible, to produce
7530 something that will match the mangled name of a function.
7531 If the definition of the function has the same debug info,
7532 we should be able to match up with it anyway. But fallbacks
7533 using the minimal symbol, for instance to find a method
7534 implemented in a stripped copy of libstdc++, will not work.
7535 If we do not have debug info for the definition, we will have to
7536 match them up some other way.
7537
7538 When we do name matching there is a related problem with function
7539 templates; two instantiated function templates are allowed to
7540 differ only by their return types, which we do not add here. */
7541
7542 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7543 {
7544 struct attribute *attr;
7545 struct die_info *child;
7546 int first = 1;
7547
7548 die->building_fullname = 1;
7549
7550 for (child = die->child; child != NULL; child = child->sibling)
7551 {
7552 struct type *type;
7553 LONGEST value;
7554 gdb_byte *bytes;
7555 struct dwarf2_locexpr_baton *baton;
7556 struct value *v;
7557
7558 if (child->tag != DW_TAG_template_type_param
7559 && child->tag != DW_TAG_template_value_param)
7560 continue;
7561
7562 if (first)
7563 {
7564 fputs_unfiltered ("<", buf);
7565 first = 0;
7566 }
7567 else
7568 fputs_unfiltered (", ", buf);
7569
7570 attr = dwarf2_attr (child, DW_AT_type, cu);
7571 if (attr == NULL)
7572 {
7573 complaint (&symfile_complaints,
7574 _("template parameter missing DW_AT_type"));
7575 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7576 continue;
7577 }
7578 type = die_type (child, cu);
7579
7580 if (child->tag == DW_TAG_template_type_param)
7581 {
7582 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
7583 continue;
7584 }
7585
7586 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7587 if (attr == NULL)
7588 {
7589 complaint (&symfile_complaints,
7590 _("template parameter missing "
7591 "DW_AT_const_value"));
7592 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7593 continue;
7594 }
7595
7596 dwarf2_const_value_attr (attr, type, name,
7597 &cu->comp_unit_obstack, cu,
7598 &value, &bytes, &baton);
7599
7600 if (TYPE_NOSIGN (type))
7601 /* GDB prints characters as NUMBER 'CHAR'. If that's
7602 changed, this can use value_print instead. */
7603 c_printchar (value, type, buf);
7604 else
7605 {
7606 struct value_print_options opts;
7607
7608 if (baton != NULL)
7609 v = dwarf2_evaluate_loc_desc (type, NULL,
7610 baton->data,
7611 baton->size,
7612 baton->per_cu);
7613 else if (bytes != NULL)
7614 {
7615 v = allocate_value (type);
7616 memcpy (value_contents_writeable (v), bytes,
7617 TYPE_LENGTH (type));
7618 }
7619 else
7620 v = value_from_longest (type, value);
7621
7622 /* Specify decimal so that we do not depend on
7623 the radix. */
7624 get_formatted_print_options (&opts, 'd');
7625 opts.raw = 1;
7626 value_print (v, buf, &opts);
7627 release_value (v);
7628 value_free (v);
7629 }
7630 }
7631
7632 die->building_fullname = 0;
7633
7634 if (!first)
7635 {
7636 /* Close the argument list, with a space if necessary
7637 (nested templates). */
7638 char last_char = '\0';
7639 ui_file_put (buf, do_ui_file_peek_last, &last_char);
7640 if (last_char == '>')
7641 fputs_unfiltered (" >", buf);
7642 else
7643 fputs_unfiltered (">", buf);
7644 }
7645 }
7646
7647 /* For Java and C++ methods, append formal parameter type
7648 information, if PHYSNAME. */
7649
7650 if (physname && die->tag == DW_TAG_subprogram
7651 && (cu->language == language_cplus
7652 || cu->language == language_java))
7653 {
7654 struct type *type = read_type_die (die, cu);
7655
7656 c_type_print_args (type, buf, 1, cu->language,
7657 &type_print_raw_options);
7658
7659 if (cu->language == language_java)
7660 {
7661 /* For java, we must append the return type to method
7662 names. */
7663 if (die->tag == DW_TAG_subprogram)
7664 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
7665 0, 0, &type_print_raw_options);
7666 }
7667 else if (cu->language == language_cplus)
7668 {
7669 /* Assume that an artificial first parameter is
7670 "this", but do not crash if it is not. RealView
7671 marks unnamed (and thus unused) parameters as
7672 artificial; there is no way to differentiate
7673 the two cases. */
7674 if (TYPE_NFIELDS (type) > 0
7675 && TYPE_FIELD_ARTIFICIAL (type, 0)
7676 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
7677 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
7678 0))))
7679 fputs_unfiltered (" const", buf);
7680 }
7681 }
7682
7683 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
7684 &length);
7685 ui_file_delete (buf);
7686
7687 if (cu->language == language_cplus)
7688 {
7689 const char *cname
7690 = dwarf2_canonicalize_name (name, cu,
7691 &objfile->objfile_obstack);
7692
7693 if (cname != NULL)
7694 name = cname;
7695 }
7696 }
7697 }
7698
7699 return name;
7700 }
7701
7702 /* Return the fully qualified name of DIE, based on its DW_AT_name.
7703 If scope qualifiers are appropriate they will be added. The result
7704 will be allocated on the objfile_obstack, or NULL if the DIE does
7705 not have a name. NAME may either be from a previous call to
7706 dwarf2_name or NULL.
7707
7708 The output string will be canonicalized (if C++/Java). */
7709
7710 static const char *
7711 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
7712 {
7713 return dwarf2_compute_name (name, die, cu, 0);
7714 }
7715
7716 /* Construct a physname for the given DIE in CU. NAME may either be
7717 from a previous call to dwarf2_name or NULL. The result will be
7718 allocated on the objfile_objstack or NULL if the DIE does not have a
7719 name.
7720
7721 The output string will be canonicalized (if C++/Java). */
7722
7723 static const char *
7724 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
7725 {
7726 struct objfile *objfile = cu->objfile;
7727 struct attribute *attr;
7728 const char *retval, *mangled = NULL, *canon = NULL;
7729 struct cleanup *back_to;
7730 int need_copy = 1;
7731
7732 /* In this case dwarf2_compute_name is just a shortcut not building anything
7733 on its own. */
7734 if (!die_needs_namespace (die, cu))
7735 return dwarf2_compute_name (name, die, cu, 1);
7736
7737 back_to = make_cleanup (null_cleanup, NULL);
7738
7739 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7740 if (!attr)
7741 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7742
7743 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
7744 has computed. */
7745 if (attr && DW_STRING (attr))
7746 {
7747 char *demangled;
7748
7749 mangled = DW_STRING (attr);
7750
7751 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
7752 type. It is easier for GDB users to search for such functions as
7753 `name(params)' than `long name(params)'. In such case the minimal
7754 symbol names do not match the full symbol names but for template
7755 functions there is never a need to look up their definition from their
7756 declaration so the only disadvantage remains the minimal symbol
7757 variant `long name(params)' does not have the proper inferior type.
7758 */
7759
7760 if (cu->language == language_go)
7761 {
7762 /* This is a lie, but we already lie to the caller new_symbol_full.
7763 new_symbol_full assumes we return the mangled name.
7764 This just undoes that lie until things are cleaned up. */
7765 demangled = NULL;
7766 }
7767 else
7768 {
7769 demangled = cplus_demangle (mangled,
7770 (DMGL_PARAMS | DMGL_ANSI
7771 | (cu->language == language_java
7772 ? DMGL_JAVA | DMGL_RET_POSTFIX
7773 : DMGL_RET_DROP)));
7774 }
7775 if (demangled)
7776 {
7777 make_cleanup (xfree, demangled);
7778 canon = demangled;
7779 }
7780 else
7781 {
7782 canon = mangled;
7783 need_copy = 0;
7784 }
7785 }
7786
7787 if (canon == NULL || check_physname)
7788 {
7789 const char *physname = dwarf2_compute_name (name, die, cu, 1);
7790
7791 if (canon != NULL && strcmp (physname, canon) != 0)
7792 {
7793 /* It may not mean a bug in GDB. The compiler could also
7794 compute DW_AT_linkage_name incorrectly. But in such case
7795 GDB would need to be bug-to-bug compatible. */
7796
7797 complaint (&symfile_complaints,
7798 _("Computed physname <%s> does not match demangled <%s> "
7799 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
7800 physname, canon, mangled, die->offset.sect_off, objfile->name);
7801
7802 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
7803 is available here - over computed PHYSNAME. It is safer
7804 against both buggy GDB and buggy compilers. */
7805
7806 retval = canon;
7807 }
7808 else
7809 {
7810 retval = physname;
7811 need_copy = 0;
7812 }
7813 }
7814 else
7815 retval = canon;
7816
7817 if (need_copy)
7818 retval = obstack_copy0 (&objfile->objfile_obstack, retval, strlen (retval));
7819
7820 do_cleanups (back_to);
7821 return retval;
7822 }
7823
7824 /* Read the import statement specified by the given die and record it. */
7825
7826 static void
7827 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
7828 {
7829 struct objfile *objfile = cu->objfile;
7830 struct attribute *import_attr;
7831 struct die_info *imported_die, *child_die;
7832 struct dwarf2_cu *imported_cu;
7833 const char *imported_name;
7834 const char *imported_name_prefix;
7835 const char *canonical_name;
7836 const char *import_alias;
7837 const char *imported_declaration = NULL;
7838 const char *import_prefix;
7839 VEC (const_char_ptr) *excludes = NULL;
7840 struct cleanup *cleanups;
7841
7842 import_attr = dwarf2_attr (die, DW_AT_import, cu);
7843 if (import_attr == NULL)
7844 {
7845 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7846 dwarf_tag_name (die->tag));
7847 return;
7848 }
7849
7850 imported_cu = cu;
7851 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
7852 imported_name = dwarf2_name (imported_die, imported_cu);
7853 if (imported_name == NULL)
7854 {
7855 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
7856
7857 The import in the following code:
7858 namespace A
7859 {
7860 typedef int B;
7861 }
7862
7863 int main ()
7864 {
7865 using A::B;
7866 B b;
7867 return b;
7868 }
7869
7870 ...
7871 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
7872 <52> DW_AT_decl_file : 1
7873 <53> DW_AT_decl_line : 6
7874 <54> DW_AT_import : <0x75>
7875 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
7876 <59> DW_AT_name : B
7877 <5b> DW_AT_decl_file : 1
7878 <5c> DW_AT_decl_line : 2
7879 <5d> DW_AT_type : <0x6e>
7880 ...
7881 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
7882 <76> DW_AT_byte_size : 4
7883 <77> DW_AT_encoding : 5 (signed)
7884
7885 imports the wrong die ( 0x75 instead of 0x58 ).
7886 This case will be ignored until the gcc bug is fixed. */
7887 return;
7888 }
7889
7890 /* Figure out the local name after import. */
7891 import_alias = dwarf2_name (die, cu);
7892
7893 /* Figure out where the statement is being imported to. */
7894 import_prefix = determine_prefix (die, cu);
7895
7896 /* Figure out what the scope of the imported die is and prepend it
7897 to the name of the imported die. */
7898 imported_name_prefix = determine_prefix (imported_die, imported_cu);
7899
7900 if (imported_die->tag != DW_TAG_namespace
7901 && imported_die->tag != DW_TAG_module)
7902 {
7903 imported_declaration = imported_name;
7904 canonical_name = imported_name_prefix;
7905 }
7906 else if (strlen (imported_name_prefix) > 0)
7907 canonical_name = obconcat (&objfile->objfile_obstack,
7908 imported_name_prefix, "::", imported_name,
7909 (char *) NULL);
7910 else
7911 canonical_name = imported_name;
7912
7913 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
7914
7915 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
7916 for (child_die = die->child; child_die && child_die->tag;
7917 child_die = sibling_die (child_die))
7918 {
7919 /* DWARF-4: A Fortran use statement with a “rename list” may be
7920 represented by an imported module entry with an import attribute
7921 referring to the module and owned entries corresponding to those
7922 entities that are renamed as part of being imported. */
7923
7924 if (child_die->tag != DW_TAG_imported_declaration)
7925 {
7926 complaint (&symfile_complaints,
7927 _("child DW_TAG_imported_declaration expected "
7928 "- DIE at 0x%x [in module %s]"),
7929 child_die->offset.sect_off, objfile->name);
7930 continue;
7931 }
7932
7933 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
7934 if (import_attr == NULL)
7935 {
7936 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7937 dwarf_tag_name (child_die->tag));
7938 continue;
7939 }
7940
7941 imported_cu = cu;
7942 imported_die = follow_die_ref_or_sig (child_die, import_attr,
7943 &imported_cu);
7944 imported_name = dwarf2_name (imported_die, imported_cu);
7945 if (imported_name == NULL)
7946 {
7947 complaint (&symfile_complaints,
7948 _("child DW_TAG_imported_declaration has unknown "
7949 "imported name - DIE at 0x%x [in module %s]"),
7950 child_die->offset.sect_off, objfile->name);
7951 continue;
7952 }
7953
7954 VEC_safe_push (const_char_ptr, excludes, imported_name);
7955
7956 process_die (child_die, cu);
7957 }
7958
7959 cp_add_using_directive (import_prefix,
7960 canonical_name,
7961 import_alias,
7962 imported_declaration,
7963 excludes,
7964 0,
7965 &objfile->objfile_obstack);
7966
7967 do_cleanups (cleanups);
7968 }
7969
7970 /* Cleanup function for handle_DW_AT_stmt_list. */
7971
7972 static void
7973 free_cu_line_header (void *arg)
7974 {
7975 struct dwarf2_cu *cu = arg;
7976
7977 free_line_header (cu->line_header);
7978 cu->line_header = NULL;
7979 }
7980
7981 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
7982 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
7983 this, it was first present in GCC release 4.3.0. */
7984
7985 static int
7986 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
7987 {
7988 if (!cu->checked_producer)
7989 check_producer (cu);
7990
7991 return cu->producer_is_gcc_lt_4_3;
7992 }
7993
7994 static void
7995 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
7996 const char **name, const char **comp_dir)
7997 {
7998 struct attribute *attr;
7999
8000 *name = NULL;
8001 *comp_dir = NULL;
8002
8003 /* Find the filename. Do not use dwarf2_name here, since the filename
8004 is not a source language identifier. */
8005 attr = dwarf2_attr (die, DW_AT_name, cu);
8006 if (attr)
8007 {
8008 *name = DW_STRING (attr);
8009 }
8010
8011 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
8012 if (attr)
8013 *comp_dir = DW_STRING (attr);
8014 else if (producer_is_gcc_lt_4_3 (cu) && *name != NULL
8015 && IS_ABSOLUTE_PATH (*name))
8016 {
8017 char *d = ldirname (*name);
8018
8019 *comp_dir = d;
8020 if (d != NULL)
8021 make_cleanup (xfree, d);
8022 }
8023 if (*comp_dir != NULL)
8024 {
8025 /* Irix 6.2 native cc prepends <machine>.: to the compilation
8026 directory, get rid of it. */
8027 char *cp = strchr (*comp_dir, ':');
8028
8029 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
8030 *comp_dir = cp + 1;
8031 }
8032
8033 if (*name == NULL)
8034 *name = "<unknown>";
8035 }
8036
8037 /* Handle DW_AT_stmt_list for a compilation unit.
8038 DIE is the DW_TAG_compile_unit die for CU.
8039 COMP_DIR is the compilation directory.
8040 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
8041
8042 static void
8043 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
8044 const char *comp_dir)
8045 {
8046 struct attribute *attr;
8047
8048 gdb_assert (! cu->per_cu->is_debug_types);
8049
8050 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
8051 if (attr)
8052 {
8053 unsigned int line_offset = DW_UNSND (attr);
8054 struct line_header *line_header
8055 = dwarf_decode_line_header (line_offset, cu);
8056
8057 if (line_header)
8058 {
8059 cu->line_header = line_header;
8060 make_cleanup (free_cu_line_header, cu);
8061 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
8062 }
8063 }
8064 }
8065
8066 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
8067
8068 static void
8069 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
8070 {
8071 struct objfile *objfile = dwarf2_per_objfile->objfile;
8072 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
8073 CORE_ADDR lowpc = ((CORE_ADDR) -1);
8074 CORE_ADDR highpc = ((CORE_ADDR) 0);
8075 struct attribute *attr;
8076 const char *name = NULL;
8077 const char *comp_dir = NULL;
8078 struct die_info *child_die;
8079 bfd *abfd = objfile->obfd;
8080 CORE_ADDR baseaddr;
8081
8082 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8083
8084 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
8085
8086 /* If we didn't find a lowpc, set it to highpc to avoid complaints
8087 from finish_block. */
8088 if (lowpc == ((CORE_ADDR) -1))
8089 lowpc = highpc;
8090 lowpc += baseaddr;
8091 highpc += baseaddr;
8092
8093 find_file_and_directory (die, cu, &name, &comp_dir);
8094
8095 prepare_one_comp_unit (cu, die, cu->language);
8096
8097 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
8098 standardised yet. As a workaround for the language detection we fall
8099 back to the DW_AT_producer string. */
8100 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
8101 cu->language = language_opencl;
8102
8103 /* Similar hack for Go. */
8104 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
8105 set_cu_language (DW_LANG_Go, cu);
8106
8107 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
8108
8109 /* Decode line number information if present. We do this before
8110 processing child DIEs, so that the line header table is available
8111 for DW_AT_decl_file. */
8112 handle_DW_AT_stmt_list (die, cu, comp_dir);
8113
8114 /* Process all dies in compilation unit. */
8115 if (die->child != NULL)
8116 {
8117 child_die = die->child;
8118 while (child_die && child_die->tag)
8119 {
8120 process_die (child_die, cu);
8121 child_die = sibling_die (child_die);
8122 }
8123 }
8124
8125 /* Decode macro information, if present. Dwarf 2 macro information
8126 refers to information in the line number info statement program
8127 header, so we can only read it if we've read the header
8128 successfully. */
8129 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
8130 if (attr && cu->line_header)
8131 {
8132 if (dwarf2_attr (die, DW_AT_macro_info, cu))
8133 complaint (&symfile_complaints,
8134 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
8135
8136 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
8137 }
8138 else
8139 {
8140 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
8141 if (attr && cu->line_header)
8142 {
8143 unsigned int macro_offset = DW_UNSND (attr);
8144
8145 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
8146 }
8147 }
8148
8149 do_cleanups (back_to);
8150 }
8151
8152 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
8153 Create the set of symtabs used by this TU, or if this TU is sharing
8154 symtabs with another TU and the symtabs have already been created
8155 then restore those symtabs in the line header.
8156 We don't need the pc/line-number mapping for type units. */
8157
8158 static void
8159 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
8160 {
8161 struct objfile *objfile = dwarf2_per_objfile->objfile;
8162 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8163 struct type_unit_group *tu_group;
8164 int first_time;
8165 struct line_header *lh;
8166 struct attribute *attr;
8167 unsigned int i, line_offset;
8168 struct signatured_type *sig_type;
8169
8170 gdb_assert (per_cu->is_debug_types);
8171 sig_type = (struct signatured_type *) per_cu;
8172
8173 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
8174
8175 /* If we're using .gdb_index (includes -readnow) then
8176 per_cu->s.type_unit_group may not have been set up yet. */
8177 if (sig_type->type_unit_group == NULL)
8178 sig_type->type_unit_group = get_type_unit_group (cu, attr);
8179 tu_group = sig_type->type_unit_group;
8180
8181 /* If we've already processed this stmt_list there's no real need to
8182 do it again, we could fake it and just recreate the part we need
8183 (file name,index -> symtab mapping). If data shows this optimization
8184 is useful we can do it then. */
8185 first_time = tu_group->primary_symtab == NULL;
8186
8187 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
8188 debug info. */
8189 lh = NULL;
8190 if (attr != NULL)
8191 {
8192 line_offset = DW_UNSND (attr);
8193 lh = dwarf_decode_line_header (line_offset, cu);
8194 }
8195 if (lh == NULL)
8196 {
8197 if (first_time)
8198 dwarf2_start_symtab (cu, "", NULL, 0);
8199 else
8200 {
8201 gdb_assert (tu_group->symtabs == NULL);
8202 restart_symtab (0);
8203 }
8204 /* Note: The primary symtab will get allocated at the end. */
8205 return;
8206 }
8207
8208 cu->line_header = lh;
8209 make_cleanup (free_cu_line_header, cu);
8210
8211 if (first_time)
8212 {
8213 dwarf2_start_symtab (cu, "", NULL, 0);
8214
8215 tu_group->num_symtabs = lh->num_file_names;
8216 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
8217
8218 for (i = 0; i < lh->num_file_names; ++i)
8219 {
8220 char *dir = NULL;
8221 struct file_entry *fe = &lh->file_names[i];
8222
8223 if (fe->dir_index)
8224 dir = lh->include_dirs[fe->dir_index - 1];
8225 dwarf2_start_subfile (fe->name, dir, NULL);
8226
8227 /* Note: We don't have to watch for the main subfile here, type units
8228 don't have DW_AT_name. */
8229
8230 if (current_subfile->symtab == NULL)
8231 {
8232 /* NOTE: start_subfile will recognize when it's been passed
8233 a file it has already seen. So we can't assume there's a
8234 simple mapping from lh->file_names to subfiles,
8235 lh->file_names may contain dups. */
8236 current_subfile->symtab = allocate_symtab (current_subfile->name,
8237 objfile);
8238 }
8239
8240 fe->symtab = current_subfile->symtab;
8241 tu_group->symtabs[i] = fe->symtab;
8242 }
8243 }
8244 else
8245 {
8246 restart_symtab (0);
8247
8248 for (i = 0; i < lh->num_file_names; ++i)
8249 {
8250 struct file_entry *fe = &lh->file_names[i];
8251
8252 fe->symtab = tu_group->symtabs[i];
8253 }
8254 }
8255
8256 /* The main symtab is allocated last. Type units don't have DW_AT_name
8257 so they don't have a "real" (so to speak) symtab anyway.
8258 There is later code that will assign the main symtab to all symbols
8259 that don't have one. We need to handle the case of a symbol with a
8260 missing symtab (DW_AT_decl_file) anyway. */
8261 }
8262
8263 /* Process DW_TAG_type_unit.
8264 For TUs we want to skip the first top level sibling if it's not the
8265 actual type being defined by this TU. In this case the first top
8266 level sibling is there to provide context only. */
8267
8268 static void
8269 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8270 {
8271 struct die_info *child_die;
8272
8273 prepare_one_comp_unit (cu, die, language_minimal);
8274
8275 /* Initialize (or reinitialize) the machinery for building symtabs.
8276 We do this before processing child DIEs, so that the line header table
8277 is available for DW_AT_decl_file. */
8278 setup_type_unit_groups (die, cu);
8279
8280 if (die->child != NULL)
8281 {
8282 child_die = die->child;
8283 while (child_die && child_die->tag)
8284 {
8285 process_die (child_die, cu);
8286 child_die = sibling_die (child_die);
8287 }
8288 }
8289 }
8290 \f
8291 /* DWO/DWP files.
8292
8293 http://gcc.gnu.org/wiki/DebugFission
8294 http://gcc.gnu.org/wiki/DebugFissionDWP
8295
8296 To simplify handling of both DWO files ("object" files with the DWARF info)
8297 and DWP files (a file with the DWOs packaged up into one file), we treat
8298 DWP files as having a collection of virtual DWO files. */
8299
8300 static hashval_t
8301 hash_dwo_file (const void *item)
8302 {
8303 const struct dwo_file *dwo_file = item;
8304
8305 return (htab_hash_string (dwo_file->dwo_name)
8306 + htab_hash_string (dwo_file->comp_dir));
8307 }
8308
8309 static int
8310 eq_dwo_file (const void *item_lhs, const void *item_rhs)
8311 {
8312 const struct dwo_file *lhs = item_lhs;
8313 const struct dwo_file *rhs = item_rhs;
8314
8315 return (strcmp (lhs->dwo_name, rhs->dwo_name) == 0
8316 && strcmp (lhs->comp_dir, rhs->comp_dir) == 0);
8317 }
8318
8319 /* Allocate a hash table for DWO files. */
8320
8321 static htab_t
8322 allocate_dwo_file_hash_table (void)
8323 {
8324 struct objfile *objfile = dwarf2_per_objfile->objfile;
8325
8326 return htab_create_alloc_ex (41,
8327 hash_dwo_file,
8328 eq_dwo_file,
8329 NULL,
8330 &objfile->objfile_obstack,
8331 hashtab_obstack_allocate,
8332 dummy_obstack_deallocate);
8333 }
8334
8335 /* Lookup DWO file DWO_NAME. */
8336
8337 static void **
8338 lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
8339 {
8340 struct dwo_file find_entry;
8341 void **slot;
8342
8343 if (dwarf2_per_objfile->dwo_files == NULL)
8344 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8345
8346 memset (&find_entry, 0, sizeof (find_entry));
8347 find_entry.dwo_name = dwo_name;
8348 find_entry.comp_dir = comp_dir;
8349 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8350
8351 return slot;
8352 }
8353
8354 static hashval_t
8355 hash_dwo_unit (const void *item)
8356 {
8357 const struct dwo_unit *dwo_unit = item;
8358
8359 /* This drops the top 32 bits of the id, but is ok for a hash. */
8360 return dwo_unit->signature;
8361 }
8362
8363 static int
8364 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8365 {
8366 const struct dwo_unit *lhs = item_lhs;
8367 const struct dwo_unit *rhs = item_rhs;
8368
8369 /* The signature is assumed to be unique within the DWO file.
8370 So while object file CU dwo_id's always have the value zero,
8371 that's OK, assuming each object file DWO file has only one CU,
8372 and that's the rule for now. */
8373 return lhs->signature == rhs->signature;
8374 }
8375
8376 /* Allocate a hash table for DWO CUs,TUs.
8377 There is one of these tables for each of CUs,TUs for each DWO file. */
8378
8379 static htab_t
8380 allocate_dwo_unit_table (struct objfile *objfile)
8381 {
8382 /* Start out with a pretty small number.
8383 Generally DWO files contain only one CU and maybe some TUs. */
8384 return htab_create_alloc_ex (3,
8385 hash_dwo_unit,
8386 eq_dwo_unit,
8387 NULL,
8388 &objfile->objfile_obstack,
8389 hashtab_obstack_allocate,
8390 dummy_obstack_deallocate);
8391 }
8392
8393 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
8394
8395 struct create_dwo_info_table_data
8396 {
8397 struct dwo_file *dwo_file;
8398 htab_t cu_htab;
8399 };
8400
8401 /* die_reader_func for create_dwo_debug_info_hash_table. */
8402
8403 static void
8404 create_dwo_debug_info_hash_table_reader (const struct die_reader_specs *reader,
8405 gdb_byte *info_ptr,
8406 struct die_info *comp_unit_die,
8407 int has_children,
8408 void *datap)
8409 {
8410 struct dwarf2_cu *cu = reader->cu;
8411 struct objfile *objfile = dwarf2_per_objfile->objfile;
8412 sect_offset offset = cu->per_cu->offset;
8413 struct dwarf2_section_info *section = cu->per_cu->section;
8414 struct create_dwo_info_table_data *data = datap;
8415 struct dwo_file *dwo_file = data->dwo_file;
8416 htab_t cu_htab = data->cu_htab;
8417 void **slot;
8418 struct attribute *attr;
8419 struct dwo_unit *dwo_unit;
8420
8421 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8422 if (attr == NULL)
8423 {
8424 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
8425 " its dwo_id [in module %s]"),
8426 offset.sect_off, dwo_file->dwo_name);
8427 return;
8428 }
8429
8430 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8431 dwo_unit->dwo_file = dwo_file;
8432 dwo_unit->signature = DW_UNSND (attr);
8433 dwo_unit->section = section;
8434 dwo_unit->offset = offset;
8435 dwo_unit->length = cu->per_cu->length;
8436
8437 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
8438 gdb_assert (slot != NULL);
8439 if (*slot != NULL)
8440 {
8441 const struct dwo_unit *dup_dwo_unit = *slot;
8442
8443 complaint (&symfile_complaints,
8444 _("debug entry at offset 0x%x is duplicate to the entry at"
8445 " offset 0x%x, dwo_id 0x%s [in module %s]"),
8446 offset.sect_off, dup_dwo_unit->offset.sect_off,
8447 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
8448 dwo_file->dwo_name);
8449 }
8450 else
8451 *slot = dwo_unit;
8452
8453 if (dwarf2_read_debug)
8454 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
8455 offset.sect_off,
8456 phex (dwo_unit->signature,
8457 sizeof (dwo_unit->signature)));
8458 }
8459
8460 /* Create a hash table to map DWO IDs to their CU entry in
8461 .debug_info.dwo in DWO_FILE.
8462 Note: This function processes DWO files only, not DWP files.
8463 Note: A DWO file generally contains one CU, but we don't assume this. */
8464
8465 static htab_t
8466 create_dwo_debug_info_hash_table (struct dwo_file *dwo_file)
8467 {
8468 struct objfile *objfile = dwarf2_per_objfile->objfile;
8469 struct dwarf2_section_info *section = &dwo_file->sections.info;
8470 bfd *abfd;
8471 htab_t cu_htab;
8472 gdb_byte *info_ptr, *end_ptr;
8473 struct create_dwo_info_table_data create_dwo_info_table_data;
8474
8475 dwarf2_read_section (objfile, section);
8476 info_ptr = section->buffer;
8477
8478 if (info_ptr == NULL)
8479 return NULL;
8480
8481 /* We can't set abfd until now because the section may be empty or
8482 not present, in which case section->asection will be NULL. */
8483 abfd = section->asection->owner;
8484
8485 if (dwarf2_read_debug)
8486 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
8487 bfd_get_filename (abfd));
8488
8489 cu_htab = allocate_dwo_unit_table (objfile);
8490
8491 create_dwo_info_table_data.dwo_file = dwo_file;
8492 create_dwo_info_table_data.cu_htab = cu_htab;
8493
8494 end_ptr = info_ptr + section->size;
8495 while (info_ptr < end_ptr)
8496 {
8497 struct dwarf2_per_cu_data per_cu;
8498
8499 memset (&per_cu, 0, sizeof (per_cu));
8500 per_cu.objfile = objfile;
8501 per_cu.is_debug_types = 0;
8502 per_cu.offset.sect_off = info_ptr - section->buffer;
8503 per_cu.section = section;
8504
8505 init_cutu_and_read_dies_no_follow (&per_cu,
8506 &dwo_file->sections.abbrev,
8507 dwo_file,
8508 create_dwo_debug_info_hash_table_reader,
8509 &create_dwo_info_table_data);
8510
8511 info_ptr += per_cu.length;
8512 }
8513
8514 return cu_htab;
8515 }
8516
8517 /* DWP file .debug_{cu,tu}_index section format:
8518 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8519
8520 Both index sections have the same format, and serve to map a 64-bit
8521 signature to a set of section numbers. Each section begins with a header,
8522 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8523 indexes, and a pool of 32-bit section numbers. The index sections will be
8524 aligned at 8-byte boundaries in the file.
8525
8526 The index section header contains two unsigned 32-bit values (using the
8527 byte order of the application binary):
8528
8529 N, the number of compilation units or type units in the index
8530 M, the number of slots in the hash table
8531
8532 (We assume that N and M will not exceed 2^32 - 1.)
8533
8534 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
8535
8536 The hash table begins at offset 8 in the section, and consists of an array
8537 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
8538 order of the application binary). Unused slots in the hash table are 0.
8539 (We rely on the extreme unlikeliness of a signature being exactly 0.)
8540
8541 The parallel table begins immediately after the hash table
8542 (at offset 8 + 8 * M from the beginning of the section), and consists of an
8543 array of 32-bit indexes (using the byte order of the application binary),
8544 corresponding 1-1 with slots in the hash table. Each entry in the parallel
8545 table contains a 32-bit index into the pool of section numbers. For unused
8546 hash table slots, the corresponding entry in the parallel table will be 0.
8547
8548 Given a 64-bit compilation unit signature or a type signature S, an entry
8549 in the hash table is located as follows:
8550
8551 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8552 the low-order k bits all set to 1.
8553
8554 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
8555
8556 3) If the hash table entry at index H matches the signature, use that
8557 entry. If the hash table entry at index H is unused (all zeroes),
8558 terminate the search: the signature is not present in the table.
8559
8560 4) Let H = (H + H') modulo M. Repeat at Step 3.
8561
8562 Because M > N and H' and M are relatively prime, the search is guaranteed
8563 to stop at an unused slot or find the match.
8564
8565 The pool of section numbers begins immediately following the hash table
8566 (at offset 8 + 12 * M from the beginning of the section). The pool of
8567 section numbers consists of an array of 32-bit words (using the byte order
8568 of the application binary). Each item in the array is indexed starting
8569 from 0. The hash table entry provides the index of the first section
8570 number in the set. Additional section numbers in the set follow, and the
8571 set is terminated by a 0 entry (section number 0 is not used in ELF).
8572
8573 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8574 section must be the first entry in the set, and the .debug_abbrev.dwo must
8575 be the second entry. Other members of the set may follow in any order. */
8576
8577 /* Create a hash table to map DWO IDs to their CU/TU entry in
8578 .debug_{info,types}.dwo in DWP_FILE.
8579 Returns NULL if there isn't one.
8580 Note: This function processes DWP files only, not DWO files. */
8581
8582 static struct dwp_hash_table *
8583 create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
8584 {
8585 struct objfile *objfile = dwarf2_per_objfile->objfile;
8586 bfd *dbfd = dwp_file->dbfd;
8587 char *index_ptr, *index_end;
8588 struct dwarf2_section_info *index;
8589 uint32_t version, nr_units, nr_slots;
8590 struct dwp_hash_table *htab;
8591
8592 if (is_debug_types)
8593 index = &dwp_file->sections.tu_index;
8594 else
8595 index = &dwp_file->sections.cu_index;
8596
8597 if (dwarf2_section_empty_p (index))
8598 return NULL;
8599 dwarf2_read_section (objfile, index);
8600
8601 index_ptr = index->buffer;
8602 index_end = index_ptr + index->size;
8603
8604 version = read_4_bytes (dbfd, index_ptr);
8605 index_ptr += 8; /* Skip the unused word. */
8606 nr_units = read_4_bytes (dbfd, index_ptr);
8607 index_ptr += 4;
8608 nr_slots = read_4_bytes (dbfd, index_ptr);
8609 index_ptr += 4;
8610
8611 if (version != 1)
8612 {
8613 error (_("Dwarf Error: unsupported DWP file version (%u)"
8614 " [in module %s]"),
8615 version, dwp_file->name);
8616 }
8617 if (nr_slots != (nr_slots & -nr_slots))
8618 {
8619 error (_("Dwarf Error: number of slots in DWP hash table (%u)"
8620 " is not power of 2 [in module %s]"),
8621 nr_slots, dwp_file->name);
8622 }
8623
8624 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
8625 htab->nr_units = nr_units;
8626 htab->nr_slots = nr_slots;
8627 htab->hash_table = index_ptr;
8628 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
8629 htab->section_pool = htab->unit_table + sizeof (uint32_t) * nr_slots;
8630
8631 return htab;
8632 }
8633
8634 /* Update SECTIONS with the data from SECTP.
8635
8636 This function is like the other "locate" section routines that are
8637 passed to bfd_map_over_sections, but in this context the sections to
8638 read comes from the DWP hash table, not the full ELF section table.
8639
8640 The result is non-zero for success, or zero if an error was found. */
8641
8642 static int
8643 locate_virtual_dwo_sections (asection *sectp,
8644 struct virtual_dwo_sections *sections)
8645 {
8646 const struct dwop_section_names *names = &dwop_section_names;
8647
8648 if (section_is_p (sectp->name, &names->abbrev_dwo))
8649 {
8650 /* There can be only one. */
8651 if (sections->abbrev.asection != NULL)
8652 return 0;
8653 sections->abbrev.asection = sectp;
8654 sections->abbrev.size = bfd_get_section_size (sectp);
8655 }
8656 else if (section_is_p (sectp->name, &names->info_dwo)
8657 || section_is_p (sectp->name, &names->types_dwo))
8658 {
8659 /* There can be only one. */
8660 if (sections->info_or_types.asection != NULL)
8661 return 0;
8662 sections->info_or_types.asection = sectp;
8663 sections->info_or_types.size = bfd_get_section_size (sectp);
8664 }
8665 else if (section_is_p (sectp->name, &names->line_dwo))
8666 {
8667 /* There can be only one. */
8668 if (sections->line.asection != NULL)
8669 return 0;
8670 sections->line.asection = sectp;
8671 sections->line.size = bfd_get_section_size (sectp);
8672 }
8673 else if (section_is_p (sectp->name, &names->loc_dwo))
8674 {
8675 /* There can be only one. */
8676 if (sections->loc.asection != NULL)
8677 return 0;
8678 sections->loc.asection = sectp;
8679 sections->loc.size = bfd_get_section_size (sectp);
8680 }
8681 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8682 {
8683 /* There can be only one. */
8684 if (sections->macinfo.asection != NULL)
8685 return 0;
8686 sections->macinfo.asection = sectp;
8687 sections->macinfo.size = bfd_get_section_size (sectp);
8688 }
8689 else if (section_is_p (sectp->name, &names->macro_dwo))
8690 {
8691 /* There can be only one. */
8692 if (sections->macro.asection != NULL)
8693 return 0;
8694 sections->macro.asection = sectp;
8695 sections->macro.size = bfd_get_section_size (sectp);
8696 }
8697 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8698 {
8699 /* There can be only one. */
8700 if (sections->str_offsets.asection != NULL)
8701 return 0;
8702 sections->str_offsets.asection = sectp;
8703 sections->str_offsets.size = bfd_get_section_size (sectp);
8704 }
8705 else
8706 {
8707 /* No other kind of section is valid. */
8708 return 0;
8709 }
8710
8711 return 1;
8712 }
8713
8714 /* Create a dwo_unit object for the DWO with signature SIGNATURE.
8715 HTAB is the hash table from the DWP file.
8716 SECTION_INDEX is the index of the DWO in HTAB.
8717 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU. */
8718
8719 static struct dwo_unit *
8720 create_dwo_in_dwp (struct dwp_file *dwp_file,
8721 const struct dwp_hash_table *htab,
8722 uint32_t section_index,
8723 const char *comp_dir,
8724 ULONGEST signature, int is_debug_types)
8725 {
8726 struct objfile *objfile = dwarf2_per_objfile->objfile;
8727 bfd *dbfd = dwp_file->dbfd;
8728 const char *kind = is_debug_types ? "TU" : "CU";
8729 struct dwo_file *dwo_file;
8730 struct dwo_unit *dwo_unit;
8731 struct virtual_dwo_sections sections;
8732 void **dwo_file_slot;
8733 char *virtual_dwo_name;
8734 struct dwarf2_section_info *cutu;
8735 struct cleanup *cleanups;
8736 int i;
8737
8738 if (dwarf2_read_debug)
8739 {
8740 fprintf_unfiltered (gdb_stdlog, "Reading %s %u/0x%s in DWP file: %s\n",
8741 kind,
8742 section_index, phex (signature, sizeof (signature)),
8743 dwp_file->name);
8744 }
8745
8746 /* Fetch the sections of this DWO.
8747 Put a limit on the number of sections we look for so that bad data
8748 doesn't cause us to loop forever. */
8749
8750 #define MAX_NR_DWO_SECTIONS \
8751 (1 /* .debug_info or .debug_types */ \
8752 + 1 /* .debug_abbrev */ \
8753 + 1 /* .debug_line */ \
8754 + 1 /* .debug_loc */ \
8755 + 1 /* .debug_str_offsets */ \
8756 + 1 /* .debug_macro */ \
8757 + 1 /* .debug_macinfo */ \
8758 + 1 /* trailing zero */)
8759
8760 memset (&sections, 0, sizeof (sections));
8761 cleanups = make_cleanup (null_cleanup, 0);
8762
8763 for (i = 0; i < MAX_NR_DWO_SECTIONS; ++i)
8764 {
8765 asection *sectp;
8766 uint32_t section_nr =
8767 read_4_bytes (dbfd,
8768 htab->section_pool
8769 + (section_index + i) * sizeof (uint32_t));
8770
8771 if (section_nr == 0)
8772 break;
8773 if (section_nr >= dwp_file->num_sections)
8774 {
8775 error (_("Dwarf Error: bad DWP hash table, section number too large"
8776 " [in module %s]"),
8777 dwp_file->name);
8778 }
8779
8780 sectp = dwp_file->elf_sections[section_nr];
8781 if (! locate_virtual_dwo_sections (sectp, &sections))
8782 {
8783 error (_("Dwarf Error: bad DWP hash table, invalid section found"
8784 " [in module %s]"),
8785 dwp_file->name);
8786 }
8787 }
8788
8789 if (i < 2
8790 || sections.info_or_types.asection == NULL
8791 || sections.abbrev.asection == NULL)
8792 {
8793 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
8794 " [in module %s]"),
8795 dwp_file->name);
8796 }
8797 if (i == MAX_NR_DWO_SECTIONS)
8798 {
8799 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
8800 " [in module %s]"),
8801 dwp_file->name);
8802 }
8803
8804 /* It's easier for the rest of the code if we fake a struct dwo_file and
8805 have dwo_unit "live" in that. At least for now.
8806
8807 The DWP file can be made up of a random collection of CUs and TUs.
8808 However, for each CU + set of TUs that came from the same original DWO
8809 file, we want to combine them back into a virtual DWO file to save space
8810 (fewer struct dwo_file objects to allocated). Remember that for really
8811 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8812
8813 virtual_dwo_name =
8814 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
8815 sections.abbrev.asection ? sections.abbrev.asection->id : 0,
8816 sections.line.asection ? sections.line.asection->id : 0,
8817 sections.loc.asection ? sections.loc.asection->id : 0,
8818 (sections.str_offsets.asection
8819 ? sections.str_offsets.asection->id
8820 : 0));
8821 make_cleanup (xfree, virtual_dwo_name);
8822 /* Can we use an existing virtual DWO file? */
8823 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
8824 /* Create one if necessary. */
8825 if (*dwo_file_slot == NULL)
8826 {
8827 if (dwarf2_read_debug)
8828 {
8829 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
8830 virtual_dwo_name);
8831 }
8832 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8833 dwo_file->dwo_name = obstack_copy0 (&objfile->objfile_obstack,
8834 virtual_dwo_name,
8835 strlen (virtual_dwo_name));
8836 dwo_file->comp_dir = comp_dir;
8837 dwo_file->sections.abbrev = sections.abbrev;
8838 dwo_file->sections.line = sections.line;
8839 dwo_file->sections.loc = sections.loc;
8840 dwo_file->sections.macinfo = sections.macinfo;
8841 dwo_file->sections.macro = sections.macro;
8842 dwo_file->sections.str_offsets = sections.str_offsets;
8843 /* The "str" section is global to the entire DWP file. */
8844 dwo_file->sections.str = dwp_file->sections.str;
8845 /* The info or types section is assigned later to dwo_unit,
8846 there's no need to record it in dwo_file.
8847 Also, we can't simply record type sections in dwo_file because
8848 we record a pointer into the vector in dwo_unit. As we collect more
8849 types we'll grow the vector and eventually have to reallocate space
8850 for it, invalidating all the pointers into the current copy. */
8851 *dwo_file_slot = dwo_file;
8852 }
8853 else
8854 {
8855 if (dwarf2_read_debug)
8856 {
8857 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
8858 virtual_dwo_name);
8859 }
8860 dwo_file = *dwo_file_slot;
8861 }
8862 do_cleanups (cleanups);
8863
8864 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8865 dwo_unit->dwo_file = dwo_file;
8866 dwo_unit->signature = signature;
8867 dwo_unit->section = obstack_alloc (&objfile->objfile_obstack,
8868 sizeof (struct dwarf2_section_info));
8869 *dwo_unit->section = sections.info_or_types;
8870 /* offset, length, type_offset_in_tu are set later. */
8871
8872 return dwo_unit;
8873 }
8874
8875 /* Lookup the DWO with SIGNATURE in DWP_FILE. */
8876
8877 static struct dwo_unit *
8878 lookup_dwo_in_dwp (struct dwp_file *dwp_file,
8879 const struct dwp_hash_table *htab,
8880 const char *comp_dir,
8881 ULONGEST signature, int is_debug_types)
8882 {
8883 bfd *dbfd = dwp_file->dbfd;
8884 uint32_t mask = htab->nr_slots - 1;
8885 uint32_t hash = signature & mask;
8886 uint32_t hash2 = ((signature >> 32) & mask) | 1;
8887 unsigned int i;
8888 void **slot;
8889 struct dwo_unit find_dwo_cu, *dwo_cu;
8890
8891 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
8892 find_dwo_cu.signature = signature;
8893 slot = htab_find_slot (dwp_file->loaded_cutus, &find_dwo_cu, INSERT);
8894
8895 if (*slot != NULL)
8896 return *slot;
8897
8898 /* Use a for loop so that we don't loop forever on bad debug info. */
8899 for (i = 0; i < htab->nr_slots; ++i)
8900 {
8901 ULONGEST signature_in_table;
8902
8903 signature_in_table =
8904 read_8_bytes (dbfd, htab->hash_table + hash * sizeof (uint64_t));
8905 if (signature_in_table == signature)
8906 {
8907 uint32_t section_index =
8908 read_4_bytes (dbfd, htab->unit_table + hash * sizeof (uint32_t));
8909
8910 *slot = create_dwo_in_dwp (dwp_file, htab, section_index,
8911 comp_dir, signature, is_debug_types);
8912 return *slot;
8913 }
8914 if (signature_in_table == 0)
8915 return NULL;
8916 hash = (hash + hash2) & mask;
8917 }
8918
8919 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
8920 " [in module %s]"),
8921 dwp_file->name);
8922 }
8923
8924 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
8925 Open the file specified by FILE_NAME and hand it off to BFD for
8926 preliminary analysis. Return a newly initialized bfd *, which
8927 includes a canonicalized copy of FILE_NAME.
8928 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
8929 In case of trouble, return NULL.
8930 NOTE: This function is derived from symfile_bfd_open. */
8931
8932 static bfd *
8933 try_open_dwop_file (const char *file_name, int is_dwp)
8934 {
8935 bfd *sym_bfd;
8936 int desc, flags;
8937 char *absolute_name;
8938
8939 flags = OPF_TRY_CWD_FIRST;
8940 if (is_dwp)
8941 flags |= OPF_SEARCH_IN_PATH;
8942 desc = openp (debug_file_directory, flags, file_name,
8943 O_RDONLY | O_BINARY, &absolute_name);
8944 if (desc < 0)
8945 return NULL;
8946
8947 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
8948 if (!sym_bfd)
8949 {
8950 xfree (absolute_name);
8951 return NULL;
8952 }
8953 xfree (absolute_name);
8954 bfd_set_cacheable (sym_bfd, 1);
8955
8956 if (!bfd_check_format (sym_bfd, bfd_object))
8957 {
8958 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
8959 return NULL;
8960 }
8961
8962 return sym_bfd;
8963 }
8964
8965 /* Try to open DWO file FILE_NAME.
8966 COMP_DIR is the DW_AT_comp_dir attribute.
8967 The result is the bfd handle of the file.
8968 If there is a problem finding or opening the file, return NULL.
8969 Upon success, the canonicalized path of the file is stored in the bfd,
8970 same as symfile_bfd_open. */
8971
8972 static bfd *
8973 open_dwo_file (const char *file_name, const char *comp_dir)
8974 {
8975 bfd *abfd;
8976
8977 if (IS_ABSOLUTE_PATH (file_name))
8978 return try_open_dwop_file (file_name, 0 /*is_dwp*/);
8979
8980 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
8981
8982 if (comp_dir != NULL)
8983 {
8984 char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
8985
8986 /* NOTE: If comp_dir is a relative path, this will also try the
8987 search path, which seems useful. */
8988 abfd = try_open_dwop_file (path_to_try, 0 /*is_dwp*/);
8989 xfree (path_to_try);
8990 if (abfd != NULL)
8991 return abfd;
8992 }
8993
8994 /* That didn't work, try debug-file-directory, which, despite its name,
8995 is a list of paths. */
8996
8997 if (*debug_file_directory == '\0')
8998 return NULL;
8999
9000 return try_open_dwop_file (file_name, 0 /*is_dwp*/);
9001 }
9002
9003 /* This function is mapped across the sections and remembers the offset and
9004 size of each of the DWO debugging sections we are interested in. */
9005
9006 static void
9007 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
9008 {
9009 struct dwo_sections *dwo_sections = dwo_sections_ptr;
9010 const struct dwop_section_names *names = &dwop_section_names;
9011
9012 if (section_is_p (sectp->name, &names->abbrev_dwo))
9013 {
9014 dwo_sections->abbrev.asection = sectp;
9015 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
9016 }
9017 else if (section_is_p (sectp->name, &names->info_dwo))
9018 {
9019 dwo_sections->info.asection = sectp;
9020 dwo_sections->info.size = bfd_get_section_size (sectp);
9021 }
9022 else if (section_is_p (sectp->name, &names->line_dwo))
9023 {
9024 dwo_sections->line.asection = sectp;
9025 dwo_sections->line.size = bfd_get_section_size (sectp);
9026 }
9027 else if (section_is_p (sectp->name, &names->loc_dwo))
9028 {
9029 dwo_sections->loc.asection = sectp;
9030 dwo_sections->loc.size = bfd_get_section_size (sectp);
9031 }
9032 else if (section_is_p (sectp->name, &names->macinfo_dwo))
9033 {
9034 dwo_sections->macinfo.asection = sectp;
9035 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
9036 }
9037 else if (section_is_p (sectp->name, &names->macro_dwo))
9038 {
9039 dwo_sections->macro.asection = sectp;
9040 dwo_sections->macro.size = bfd_get_section_size (sectp);
9041 }
9042 else if (section_is_p (sectp->name, &names->str_dwo))
9043 {
9044 dwo_sections->str.asection = sectp;
9045 dwo_sections->str.size = bfd_get_section_size (sectp);
9046 }
9047 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
9048 {
9049 dwo_sections->str_offsets.asection = sectp;
9050 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
9051 }
9052 else if (section_is_p (sectp->name, &names->types_dwo))
9053 {
9054 struct dwarf2_section_info type_section;
9055
9056 memset (&type_section, 0, sizeof (type_section));
9057 type_section.asection = sectp;
9058 type_section.size = bfd_get_section_size (sectp);
9059 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
9060 &type_section);
9061 }
9062 }
9063
9064 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
9065 by PER_CU.
9066 The result is NULL if DWO_NAME can't be found. */
9067
9068 static struct dwo_file *
9069 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
9070 const char *dwo_name, const char *comp_dir)
9071 {
9072 struct objfile *objfile = dwarf2_per_objfile->objfile;
9073 struct dwo_file *dwo_file;
9074 bfd *dbfd;
9075 struct cleanup *cleanups;
9076
9077 dbfd = open_dwo_file (dwo_name, comp_dir);
9078 if (dbfd == NULL)
9079 {
9080 if (dwarf2_read_debug)
9081 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
9082 return NULL;
9083 }
9084 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
9085 dwo_file->dwo_name = dwo_name;
9086 dwo_file->comp_dir = comp_dir;
9087 dwo_file->dbfd = dbfd;
9088
9089 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
9090
9091 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
9092
9093 dwo_file->cus = create_dwo_debug_info_hash_table (dwo_file);
9094
9095 dwo_file->tus = create_debug_types_hash_table (dwo_file,
9096 dwo_file->sections.types);
9097
9098 discard_cleanups (cleanups);
9099
9100 if (dwarf2_read_debug)
9101 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
9102
9103 return dwo_file;
9104 }
9105
9106 /* This function is mapped across the sections and remembers the offset and
9107 size of each of the DWP debugging sections we are interested in. */
9108
9109 static void
9110 dwarf2_locate_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
9111 {
9112 struct dwp_file *dwp_file = dwp_file_ptr;
9113 const struct dwop_section_names *names = &dwop_section_names;
9114 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
9115
9116 /* Record the ELF section number for later lookup: this is what the
9117 .debug_cu_index,.debug_tu_index tables use. */
9118 gdb_assert (elf_section_nr < dwp_file->num_sections);
9119 dwp_file->elf_sections[elf_section_nr] = sectp;
9120
9121 /* Look for specific sections that we need. */
9122 if (section_is_p (sectp->name, &names->str_dwo))
9123 {
9124 dwp_file->sections.str.asection = sectp;
9125 dwp_file->sections.str.size = bfd_get_section_size (sectp);
9126 }
9127 else if (section_is_p (sectp->name, &names->cu_index))
9128 {
9129 dwp_file->sections.cu_index.asection = sectp;
9130 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
9131 }
9132 else if (section_is_p (sectp->name, &names->tu_index))
9133 {
9134 dwp_file->sections.tu_index.asection = sectp;
9135 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
9136 }
9137 }
9138
9139 /* Hash function for dwp_file loaded CUs/TUs. */
9140
9141 static hashval_t
9142 hash_dwp_loaded_cutus (const void *item)
9143 {
9144 const struct dwo_unit *dwo_unit = item;
9145
9146 /* This drops the top 32 bits of the signature, but is ok for a hash. */
9147 return dwo_unit->signature;
9148 }
9149
9150 /* Equality function for dwp_file loaded CUs/TUs. */
9151
9152 static int
9153 eq_dwp_loaded_cutus (const void *a, const void *b)
9154 {
9155 const struct dwo_unit *dua = a;
9156 const struct dwo_unit *dub = b;
9157
9158 return dua->signature == dub->signature;
9159 }
9160
9161 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
9162
9163 static htab_t
9164 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
9165 {
9166 return htab_create_alloc_ex (3,
9167 hash_dwp_loaded_cutus,
9168 eq_dwp_loaded_cutus,
9169 NULL,
9170 &objfile->objfile_obstack,
9171 hashtab_obstack_allocate,
9172 dummy_obstack_deallocate);
9173 }
9174
9175 /* Try to open DWP file FILE_NAME.
9176 The result is the bfd handle of the file.
9177 If there is a problem finding or opening the file, return NULL.
9178 Upon success, the canonicalized path of the file is stored in the bfd,
9179 same as symfile_bfd_open. */
9180
9181 static bfd *
9182 open_dwp_file (const char *file_name)
9183 {
9184 return try_open_dwop_file (file_name, 1 /*is_dwp*/);
9185 }
9186
9187 /* Initialize the use of the DWP file for the current objfile.
9188 By convention the name of the DWP file is ${objfile}.dwp.
9189 The result is NULL if it can't be found. */
9190
9191 static struct dwp_file *
9192 open_and_init_dwp_file (void)
9193 {
9194 struct objfile *objfile = dwarf2_per_objfile->objfile;
9195 struct dwp_file *dwp_file;
9196 char *dwp_name;
9197 bfd *dbfd;
9198 struct cleanup *cleanups;
9199
9200 dwp_name = xstrprintf ("%s.dwp", dwarf2_per_objfile->objfile->name);
9201 cleanups = make_cleanup (xfree, dwp_name);
9202
9203 dbfd = open_dwp_file (dwp_name);
9204 if (dbfd == NULL)
9205 {
9206 if (dwarf2_read_debug)
9207 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
9208 do_cleanups (cleanups);
9209 return NULL;
9210 }
9211 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
9212 dwp_file->name = obstack_copy0 (&objfile->objfile_obstack,
9213 dwp_name, strlen (dwp_name));
9214 dwp_file->dbfd = dbfd;
9215 do_cleanups (cleanups);
9216
9217 /* +1: section 0 is unused */
9218 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
9219 dwp_file->elf_sections =
9220 OBSTACK_CALLOC (&objfile->objfile_obstack,
9221 dwp_file->num_sections, asection *);
9222
9223 bfd_map_over_sections (dbfd, dwarf2_locate_dwp_sections, dwp_file);
9224
9225 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
9226
9227 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
9228
9229 dwp_file->loaded_cutus = allocate_dwp_loaded_cutus_table (objfile);
9230
9231 if (dwarf2_read_debug)
9232 {
9233 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
9234 fprintf_unfiltered (gdb_stdlog,
9235 " %u CUs, %u TUs\n",
9236 dwp_file->cus ? dwp_file->cus->nr_units : 0,
9237 dwp_file->tus ? dwp_file->tus->nr_units : 0);
9238 }
9239
9240 return dwp_file;
9241 }
9242
9243 /* Wrapper around open_and_init_dwp_file, only open it once. */
9244
9245 static struct dwp_file *
9246 get_dwp_file (void)
9247 {
9248 if (! dwarf2_per_objfile->dwp_checked)
9249 {
9250 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
9251 dwarf2_per_objfile->dwp_checked = 1;
9252 }
9253 return dwarf2_per_objfile->dwp_file;
9254 }
9255
9256 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9257 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9258 or in the DWP file for the objfile, referenced by THIS_UNIT.
9259 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
9260 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9261
9262 This is called, for example, when wanting to read a variable with a
9263 complex location. Therefore we don't want to do file i/o for every call.
9264 Therefore we don't want to look for a DWO file on every call.
9265 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9266 then we check if we've already seen DWO_NAME, and only THEN do we check
9267 for a DWO file.
9268
9269 The result is a pointer to the dwo_unit object or NULL if we didn't find it
9270 (dwo_id mismatch or couldn't find the DWO/DWP file). */
9271
9272 static struct dwo_unit *
9273 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
9274 const char *dwo_name, const char *comp_dir,
9275 ULONGEST signature, int is_debug_types)
9276 {
9277 struct objfile *objfile = dwarf2_per_objfile->objfile;
9278 const char *kind = is_debug_types ? "TU" : "CU";
9279 void **dwo_file_slot;
9280 struct dwo_file *dwo_file;
9281 struct dwp_file *dwp_file;
9282
9283 /* Have we already read SIGNATURE from a DWP file? */
9284
9285 dwp_file = get_dwp_file ();
9286 if (dwp_file != NULL)
9287 {
9288 const struct dwp_hash_table *dwp_htab =
9289 is_debug_types ? dwp_file->tus : dwp_file->cus;
9290
9291 if (dwp_htab != NULL)
9292 {
9293 struct dwo_unit *dwo_cutu =
9294 lookup_dwo_in_dwp (dwp_file, dwp_htab, comp_dir,
9295 signature, is_debug_types);
9296
9297 if (dwo_cutu != NULL)
9298 {
9299 if (dwarf2_read_debug)
9300 {
9301 fprintf_unfiltered (gdb_stdlog,
9302 "Virtual DWO %s %s found: @%s\n",
9303 kind, hex_string (signature),
9304 host_address_to_string (dwo_cutu));
9305 }
9306 return dwo_cutu;
9307 }
9308 }
9309 }
9310
9311 /* Have we already seen DWO_NAME? */
9312
9313 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
9314 if (*dwo_file_slot == NULL)
9315 {
9316 /* Read in the file and build a table of the DWOs it contains. */
9317 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
9318 }
9319 /* NOTE: This will be NULL if unable to open the file. */
9320 dwo_file = *dwo_file_slot;
9321
9322 if (dwo_file != NULL)
9323 {
9324 htab_t htab = is_debug_types ? dwo_file->tus : dwo_file->cus;
9325
9326 if (htab != NULL)
9327 {
9328 struct dwo_unit find_dwo_cutu, *dwo_cutu;
9329
9330 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9331 find_dwo_cutu.signature = signature;
9332 dwo_cutu = htab_find (htab, &find_dwo_cutu);
9333
9334 if (dwo_cutu != NULL)
9335 {
9336 if (dwarf2_read_debug)
9337 {
9338 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
9339 kind, dwo_name, hex_string (signature),
9340 host_address_to_string (dwo_cutu));
9341 }
9342 return dwo_cutu;
9343 }
9344 }
9345 }
9346
9347 /* We didn't find it. This could mean a dwo_id mismatch, or
9348 someone deleted the DWO/DWP file, or the search path isn't set up
9349 correctly to find the file. */
9350
9351 if (dwarf2_read_debug)
9352 {
9353 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
9354 kind, dwo_name, hex_string (signature));
9355 }
9356
9357 complaint (&symfile_complaints,
9358 _("Could not find DWO %s referenced by CU at offset 0x%x"
9359 " [in module %s]"),
9360 kind, this_unit->offset.sect_off, objfile->name);
9361 return NULL;
9362 }
9363
9364 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9365 See lookup_dwo_cutu_unit for details. */
9366
9367 static struct dwo_unit *
9368 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
9369 const char *dwo_name, const char *comp_dir,
9370 ULONGEST signature)
9371 {
9372 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
9373 }
9374
9375 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9376 See lookup_dwo_cutu_unit for details. */
9377
9378 static struct dwo_unit *
9379 lookup_dwo_type_unit (struct signatured_type *this_tu,
9380 const char *dwo_name, const char *comp_dir)
9381 {
9382 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
9383 }
9384
9385 /* Free all resources associated with DWO_FILE.
9386 Close the DWO file and munmap the sections.
9387 All memory should be on the objfile obstack. */
9388
9389 static void
9390 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
9391 {
9392 int ix;
9393 struct dwarf2_section_info *section;
9394
9395 /* Note: dbfd is NULL for virtual DWO files. */
9396 gdb_bfd_unref (dwo_file->dbfd);
9397
9398 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
9399 }
9400
9401 /* Wrapper for free_dwo_file for use in cleanups. */
9402
9403 static void
9404 free_dwo_file_cleanup (void *arg)
9405 {
9406 struct dwo_file *dwo_file = (struct dwo_file *) arg;
9407 struct objfile *objfile = dwarf2_per_objfile->objfile;
9408
9409 free_dwo_file (dwo_file, objfile);
9410 }
9411
9412 /* Traversal function for free_dwo_files. */
9413
9414 static int
9415 free_dwo_file_from_slot (void **slot, void *info)
9416 {
9417 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
9418 struct objfile *objfile = (struct objfile *) info;
9419
9420 free_dwo_file (dwo_file, objfile);
9421
9422 return 1;
9423 }
9424
9425 /* Free all resources associated with DWO_FILES. */
9426
9427 static void
9428 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
9429 {
9430 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
9431 }
9432 \f
9433 /* Read in various DIEs. */
9434
9435 /* qsort helper for inherit_abstract_dies. */
9436
9437 static int
9438 unsigned_int_compar (const void *ap, const void *bp)
9439 {
9440 unsigned int a = *(unsigned int *) ap;
9441 unsigned int b = *(unsigned int *) bp;
9442
9443 return (a > b) - (b > a);
9444 }
9445
9446 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
9447 Inherit only the children of the DW_AT_abstract_origin DIE not being
9448 already referenced by DW_AT_abstract_origin from the children of the
9449 current DIE. */
9450
9451 static void
9452 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9453 {
9454 struct die_info *child_die;
9455 unsigned die_children_count;
9456 /* CU offsets which were referenced by children of the current DIE. */
9457 sect_offset *offsets;
9458 sect_offset *offsets_end, *offsetp;
9459 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9460 struct die_info *origin_die;
9461 /* Iterator of the ORIGIN_DIE children. */
9462 struct die_info *origin_child_die;
9463 struct cleanup *cleanups;
9464 struct attribute *attr;
9465 struct dwarf2_cu *origin_cu;
9466 struct pending **origin_previous_list_in_scope;
9467
9468 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9469 if (!attr)
9470 return;
9471
9472 /* Note that following die references may follow to a die in a
9473 different cu. */
9474
9475 origin_cu = cu;
9476 origin_die = follow_die_ref (die, attr, &origin_cu);
9477
9478 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9479 symbols in. */
9480 origin_previous_list_in_scope = origin_cu->list_in_scope;
9481 origin_cu->list_in_scope = cu->list_in_scope;
9482
9483 if (die->tag != origin_die->tag
9484 && !(die->tag == DW_TAG_inlined_subroutine
9485 && origin_die->tag == DW_TAG_subprogram))
9486 complaint (&symfile_complaints,
9487 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
9488 die->offset.sect_off, origin_die->offset.sect_off);
9489
9490 child_die = die->child;
9491 die_children_count = 0;
9492 while (child_die && child_die->tag)
9493 {
9494 child_die = sibling_die (child_die);
9495 die_children_count++;
9496 }
9497 offsets = xmalloc (sizeof (*offsets) * die_children_count);
9498 cleanups = make_cleanup (xfree, offsets);
9499
9500 offsets_end = offsets;
9501 child_die = die->child;
9502 while (child_die && child_die->tag)
9503 {
9504 /* For each CHILD_DIE, find the corresponding child of
9505 ORIGIN_DIE. If there is more than one layer of
9506 DW_AT_abstract_origin, follow them all; there shouldn't be,
9507 but GCC versions at least through 4.4 generate this (GCC PR
9508 40573). */
9509 struct die_info *child_origin_die = child_die;
9510 struct dwarf2_cu *child_origin_cu = cu;
9511
9512 while (1)
9513 {
9514 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
9515 child_origin_cu);
9516 if (attr == NULL)
9517 break;
9518 child_origin_die = follow_die_ref (child_origin_die, attr,
9519 &child_origin_cu);
9520 }
9521
9522 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
9523 counterpart may exist. */
9524 if (child_origin_die != child_die)
9525 {
9526 if (child_die->tag != child_origin_die->tag
9527 && !(child_die->tag == DW_TAG_inlined_subroutine
9528 && child_origin_die->tag == DW_TAG_subprogram))
9529 complaint (&symfile_complaints,
9530 _("Child DIE 0x%x and its abstract origin 0x%x have "
9531 "different tags"), child_die->offset.sect_off,
9532 child_origin_die->offset.sect_off);
9533 if (child_origin_die->parent != origin_die)
9534 complaint (&symfile_complaints,
9535 _("Child DIE 0x%x and its abstract origin 0x%x have "
9536 "different parents"), child_die->offset.sect_off,
9537 child_origin_die->offset.sect_off);
9538 else
9539 *offsets_end++ = child_origin_die->offset;
9540 }
9541 child_die = sibling_die (child_die);
9542 }
9543 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
9544 unsigned_int_compar);
9545 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
9546 if (offsetp[-1].sect_off == offsetp->sect_off)
9547 complaint (&symfile_complaints,
9548 _("Multiple children of DIE 0x%x refer "
9549 "to DIE 0x%x as their abstract origin"),
9550 die->offset.sect_off, offsetp->sect_off);
9551
9552 offsetp = offsets;
9553 origin_child_die = origin_die->child;
9554 while (origin_child_die && origin_child_die->tag)
9555 {
9556 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
9557 while (offsetp < offsets_end
9558 && offsetp->sect_off < origin_child_die->offset.sect_off)
9559 offsetp++;
9560 if (offsetp >= offsets_end
9561 || offsetp->sect_off > origin_child_die->offset.sect_off)
9562 {
9563 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
9564 process_die (origin_child_die, origin_cu);
9565 }
9566 origin_child_die = sibling_die (origin_child_die);
9567 }
9568 origin_cu->list_in_scope = origin_previous_list_in_scope;
9569
9570 do_cleanups (cleanups);
9571 }
9572
9573 static void
9574 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
9575 {
9576 struct objfile *objfile = cu->objfile;
9577 struct context_stack *new;
9578 CORE_ADDR lowpc;
9579 CORE_ADDR highpc;
9580 struct die_info *child_die;
9581 struct attribute *attr, *call_line, *call_file;
9582 const char *name;
9583 CORE_ADDR baseaddr;
9584 struct block *block;
9585 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
9586 VEC (symbolp) *template_args = NULL;
9587 struct template_symbol *templ_func = NULL;
9588
9589 if (inlined_func)
9590 {
9591 /* If we do not have call site information, we can't show the
9592 caller of this inlined function. That's too confusing, so
9593 only use the scope for local variables. */
9594 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
9595 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
9596 if (call_line == NULL || call_file == NULL)
9597 {
9598 read_lexical_block_scope (die, cu);
9599 return;
9600 }
9601 }
9602
9603 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9604
9605 name = dwarf2_name (die, cu);
9606
9607 /* Ignore functions with missing or empty names. These are actually
9608 illegal according to the DWARF standard. */
9609 if (name == NULL)
9610 {
9611 complaint (&symfile_complaints,
9612 _("missing name for subprogram DIE at %d"),
9613 die->offset.sect_off);
9614 return;
9615 }
9616
9617 /* Ignore functions with missing or invalid low and high pc attributes. */
9618 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9619 {
9620 attr = dwarf2_attr (die, DW_AT_external, cu);
9621 if (!attr || !DW_UNSND (attr))
9622 complaint (&symfile_complaints,
9623 _("cannot get low and high bounds "
9624 "for subprogram DIE at %d"),
9625 die->offset.sect_off);
9626 return;
9627 }
9628
9629 lowpc += baseaddr;
9630 highpc += baseaddr;
9631
9632 /* If we have any template arguments, then we must allocate a
9633 different sort of symbol. */
9634 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
9635 {
9636 if (child_die->tag == DW_TAG_template_type_param
9637 || child_die->tag == DW_TAG_template_value_param)
9638 {
9639 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
9640 struct template_symbol);
9641 templ_func->base.is_cplus_template_function = 1;
9642 break;
9643 }
9644 }
9645
9646 new = push_context (0, lowpc);
9647 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
9648 (struct symbol *) templ_func);
9649
9650 /* If there is a location expression for DW_AT_frame_base, record
9651 it. */
9652 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
9653 if (attr)
9654 dwarf2_symbol_mark_computed (attr, new->name, cu, 1);
9655
9656 cu->list_in_scope = &local_symbols;
9657
9658 if (die->child != NULL)
9659 {
9660 child_die = die->child;
9661 while (child_die && child_die->tag)
9662 {
9663 if (child_die->tag == DW_TAG_template_type_param
9664 || child_die->tag == DW_TAG_template_value_param)
9665 {
9666 struct symbol *arg = new_symbol (child_die, NULL, cu);
9667
9668 if (arg != NULL)
9669 VEC_safe_push (symbolp, template_args, arg);
9670 }
9671 else
9672 process_die (child_die, cu);
9673 child_die = sibling_die (child_die);
9674 }
9675 }
9676
9677 inherit_abstract_dies (die, cu);
9678
9679 /* If we have a DW_AT_specification, we might need to import using
9680 directives from the context of the specification DIE. See the
9681 comment in determine_prefix. */
9682 if (cu->language == language_cplus
9683 && dwarf2_attr (die, DW_AT_specification, cu))
9684 {
9685 struct dwarf2_cu *spec_cu = cu;
9686 struct die_info *spec_die = die_specification (die, &spec_cu);
9687
9688 while (spec_die)
9689 {
9690 child_die = spec_die->child;
9691 while (child_die && child_die->tag)
9692 {
9693 if (child_die->tag == DW_TAG_imported_module)
9694 process_die (child_die, spec_cu);
9695 child_die = sibling_die (child_die);
9696 }
9697
9698 /* In some cases, GCC generates specification DIEs that
9699 themselves contain DW_AT_specification attributes. */
9700 spec_die = die_specification (spec_die, &spec_cu);
9701 }
9702 }
9703
9704 new = pop_context ();
9705 /* Make a block for the local symbols within. */
9706 block = finish_block (new->name, &local_symbols, new->old_blocks,
9707 lowpc, highpc, objfile);
9708
9709 /* For C++, set the block's scope. */
9710 if ((cu->language == language_cplus || cu->language == language_fortran)
9711 && cu->processing_has_namespace_info)
9712 block_set_scope (block, determine_prefix (die, cu),
9713 &objfile->objfile_obstack);
9714
9715 /* If we have address ranges, record them. */
9716 dwarf2_record_block_ranges (die, block, baseaddr, cu);
9717
9718 /* Attach template arguments to function. */
9719 if (! VEC_empty (symbolp, template_args))
9720 {
9721 gdb_assert (templ_func != NULL);
9722
9723 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
9724 templ_func->template_arguments
9725 = obstack_alloc (&objfile->objfile_obstack,
9726 (templ_func->n_template_arguments
9727 * sizeof (struct symbol *)));
9728 memcpy (templ_func->template_arguments,
9729 VEC_address (symbolp, template_args),
9730 (templ_func->n_template_arguments * sizeof (struct symbol *)));
9731 VEC_free (symbolp, template_args);
9732 }
9733
9734 /* In C++, we can have functions nested inside functions (e.g., when
9735 a function declares a class that has methods). This means that
9736 when we finish processing a function scope, we may need to go
9737 back to building a containing block's symbol lists. */
9738 local_symbols = new->locals;
9739 using_directives = new->using_directives;
9740
9741 /* If we've finished processing a top-level function, subsequent
9742 symbols go in the file symbol list. */
9743 if (outermost_context_p ())
9744 cu->list_in_scope = &file_symbols;
9745 }
9746
9747 /* Process all the DIES contained within a lexical block scope. Start
9748 a new scope, process the dies, and then close the scope. */
9749
9750 static void
9751 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
9752 {
9753 struct objfile *objfile = cu->objfile;
9754 struct context_stack *new;
9755 CORE_ADDR lowpc, highpc;
9756 struct die_info *child_die;
9757 CORE_ADDR baseaddr;
9758
9759 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9760
9761 /* Ignore blocks with missing or invalid low and high pc attributes. */
9762 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
9763 as multiple lexical blocks? Handling children in a sane way would
9764 be nasty. Might be easier to properly extend generic blocks to
9765 describe ranges. */
9766 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9767 return;
9768 lowpc += baseaddr;
9769 highpc += baseaddr;
9770
9771 push_context (0, lowpc);
9772 if (die->child != NULL)
9773 {
9774 child_die = die->child;
9775 while (child_die && child_die->tag)
9776 {
9777 process_die (child_die, cu);
9778 child_die = sibling_die (child_die);
9779 }
9780 }
9781 new = pop_context ();
9782
9783 if (local_symbols != NULL || using_directives != NULL)
9784 {
9785 struct block *block
9786 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
9787 highpc, objfile);
9788
9789 /* Note that recording ranges after traversing children, as we
9790 do here, means that recording a parent's ranges entails
9791 walking across all its children's ranges as they appear in
9792 the address map, which is quadratic behavior.
9793
9794 It would be nicer to record the parent's ranges before
9795 traversing its children, simply overriding whatever you find
9796 there. But since we don't even decide whether to create a
9797 block until after we've traversed its children, that's hard
9798 to do. */
9799 dwarf2_record_block_ranges (die, block, baseaddr, cu);
9800 }
9801 local_symbols = new->locals;
9802 using_directives = new->using_directives;
9803 }
9804
9805 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
9806
9807 static void
9808 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
9809 {
9810 struct objfile *objfile = cu->objfile;
9811 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9812 CORE_ADDR pc, baseaddr;
9813 struct attribute *attr;
9814 struct call_site *call_site, call_site_local;
9815 void **slot;
9816 int nparams;
9817 struct die_info *child_die;
9818
9819 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9820
9821 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9822 if (!attr)
9823 {
9824 complaint (&symfile_complaints,
9825 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
9826 "DIE 0x%x [in module %s]"),
9827 die->offset.sect_off, objfile->name);
9828 return;
9829 }
9830 pc = DW_ADDR (attr) + baseaddr;
9831
9832 if (cu->call_site_htab == NULL)
9833 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
9834 NULL, &objfile->objfile_obstack,
9835 hashtab_obstack_allocate, NULL);
9836 call_site_local.pc = pc;
9837 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
9838 if (*slot != NULL)
9839 {
9840 complaint (&symfile_complaints,
9841 _("Duplicate PC %s for DW_TAG_GNU_call_site "
9842 "DIE 0x%x [in module %s]"),
9843 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
9844 return;
9845 }
9846
9847 /* Count parameters at the caller. */
9848
9849 nparams = 0;
9850 for (child_die = die->child; child_die && child_die->tag;
9851 child_die = sibling_die (child_die))
9852 {
9853 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9854 {
9855 complaint (&symfile_complaints,
9856 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
9857 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9858 child_die->tag, child_die->offset.sect_off, objfile->name);
9859 continue;
9860 }
9861
9862 nparams++;
9863 }
9864
9865 call_site = obstack_alloc (&objfile->objfile_obstack,
9866 (sizeof (*call_site)
9867 + (sizeof (*call_site->parameter)
9868 * (nparams - 1))));
9869 *slot = call_site;
9870 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
9871 call_site->pc = pc;
9872
9873 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
9874 {
9875 struct die_info *func_die;
9876
9877 /* Skip also over DW_TAG_inlined_subroutine. */
9878 for (func_die = die->parent;
9879 func_die && func_die->tag != DW_TAG_subprogram
9880 && func_die->tag != DW_TAG_subroutine_type;
9881 func_die = func_die->parent);
9882
9883 /* DW_AT_GNU_all_call_sites is a superset
9884 of DW_AT_GNU_all_tail_call_sites. */
9885 if (func_die
9886 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
9887 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
9888 {
9889 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
9890 not complete. But keep CALL_SITE for look ups via call_site_htab,
9891 both the initial caller containing the real return address PC and
9892 the final callee containing the current PC of a chain of tail
9893 calls do not need to have the tail call list complete. But any
9894 function candidate for a virtual tail call frame searched via
9895 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
9896 determined unambiguously. */
9897 }
9898 else
9899 {
9900 struct type *func_type = NULL;
9901
9902 if (func_die)
9903 func_type = get_die_type (func_die, cu);
9904 if (func_type != NULL)
9905 {
9906 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
9907
9908 /* Enlist this call site to the function. */
9909 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
9910 TYPE_TAIL_CALL_LIST (func_type) = call_site;
9911 }
9912 else
9913 complaint (&symfile_complaints,
9914 _("Cannot find function owning DW_TAG_GNU_call_site "
9915 "DIE 0x%x [in module %s]"),
9916 die->offset.sect_off, objfile->name);
9917 }
9918 }
9919
9920 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
9921 if (attr == NULL)
9922 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9923 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9924 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
9925 /* Keep NULL DWARF_BLOCK. */;
9926 else if (attr_form_is_block (attr))
9927 {
9928 struct dwarf2_locexpr_baton *dlbaton;
9929
9930 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
9931 dlbaton->data = DW_BLOCK (attr)->data;
9932 dlbaton->size = DW_BLOCK (attr)->size;
9933 dlbaton->per_cu = cu->per_cu;
9934
9935 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
9936 }
9937 else if (is_ref_attr (attr))
9938 {
9939 struct dwarf2_cu *target_cu = cu;
9940 struct die_info *target_die;
9941
9942 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
9943 gdb_assert (target_cu->objfile == objfile);
9944 if (die_is_declaration (target_die, target_cu))
9945 {
9946 const char *target_physname = NULL;
9947 struct attribute *target_attr;
9948
9949 /* Prefer the mangled name; otherwise compute the demangled one. */
9950 target_attr = dwarf2_attr (target_die, DW_AT_linkage_name, target_cu);
9951 if (target_attr == NULL)
9952 target_attr = dwarf2_attr (target_die, DW_AT_MIPS_linkage_name,
9953 target_cu);
9954 if (target_attr != NULL && DW_STRING (target_attr) != NULL)
9955 target_physname = DW_STRING (target_attr);
9956 else
9957 target_physname = dwarf2_physname (NULL, target_die, target_cu);
9958 if (target_physname == NULL)
9959 complaint (&symfile_complaints,
9960 _("DW_AT_GNU_call_site_target target DIE has invalid "
9961 "physname, for referencing DIE 0x%x [in module %s]"),
9962 die->offset.sect_off, objfile->name);
9963 else
9964 SET_FIELD_PHYSNAME (call_site->target, target_physname);
9965 }
9966 else
9967 {
9968 CORE_ADDR lowpc;
9969
9970 /* DW_AT_entry_pc should be preferred. */
9971 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
9972 complaint (&symfile_complaints,
9973 _("DW_AT_GNU_call_site_target target DIE has invalid "
9974 "low pc, for referencing DIE 0x%x [in module %s]"),
9975 die->offset.sect_off, objfile->name);
9976 else
9977 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
9978 }
9979 }
9980 else
9981 complaint (&symfile_complaints,
9982 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
9983 "block nor reference, for DIE 0x%x [in module %s]"),
9984 die->offset.sect_off, objfile->name);
9985
9986 call_site->per_cu = cu->per_cu;
9987
9988 for (child_die = die->child;
9989 child_die && child_die->tag;
9990 child_die = sibling_die (child_die))
9991 {
9992 struct call_site_parameter *parameter;
9993 struct attribute *loc, *origin;
9994
9995 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9996 {
9997 /* Already printed the complaint above. */
9998 continue;
9999 }
10000
10001 gdb_assert (call_site->parameter_count < nparams);
10002 parameter = &call_site->parameter[call_site->parameter_count];
10003
10004 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
10005 specifies DW_TAG_formal_parameter. Value of the data assumed for the
10006 register is contained in DW_AT_GNU_call_site_value. */
10007
10008 loc = dwarf2_attr (child_die, DW_AT_location, cu);
10009 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
10010 if (loc == NULL && origin != NULL && is_ref_attr (origin))
10011 {
10012 sect_offset offset;
10013
10014 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
10015 offset = dwarf2_get_ref_die_offset (origin);
10016 if (!offset_in_cu_p (&cu->header, offset))
10017 {
10018 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
10019 binding can be done only inside one CU. Such referenced DIE
10020 therefore cannot be even moved to DW_TAG_partial_unit. */
10021 complaint (&symfile_complaints,
10022 _("DW_AT_abstract_origin offset is not in CU for "
10023 "DW_TAG_GNU_call_site child DIE 0x%x "
10024 "[in module %s]"),
10025 child_die->offset.sect_off, objfile->name);
10026 continue;
10027 }
10028 parameter->u.param_offset.cu_off = (offset.sect_off
10029 - cu->header.offset.sect_off);
10030 }
10031 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
10032 {
10033 complaint (&symfile_complaints,
10034 _("No DW_FORM_block* DW_AT_location for "
10035 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
10036 child_die->offset.sect_off, objfile->name);
10037 continue;
10038 }
10039 else
10040 {
10041 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
10042 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
10043 if (parameter->u.dwarf_reg != -1)
10044 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
10045 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
10046 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
10047 &parameter->u.fb_offset))
10048 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
10049 else
10050 {
10051 complaint (&symfile_complaints,
10052 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
10053 "for DW_FORM_block* DW_AT_location is supported for "
10054 "DW_TAG_GNU_call_site child DIE 0x%x "
10055 "[in module %s]"),
10056 child_die->offset.sect_off, objfile->name);
10057 continue;
10058 }
10059 }
10060
10061 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
10062 if (!attr_form_is_block (attr))
10063 {
10064 complaint (&symfile_complaints,
10065 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
10066 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
10067 child_die->offset.sect_off, objfile->name);
10068 continue;
10069 }
10070 parameter->value = DW_BLOCK (attr)->data;
10071 parameter->value_size = DW_BLOCK (attr)->size;
10072
10073 /* Parameters are not pre-cleared by memset above. */
10074 parameter->data_value = NULL;
10075 parameter->data_value_size = 0;
10076 call_site->parameter_count++;
10077
10078 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
10079 if (attr)
10080 {
10081 if (!attr_form_is_block (attr))
10082 complaint (&symfile_complaints,
10083 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
10084 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
10085 child_die->offset.sect_off, objfile->name);
10086 else
10087 {
10088 parameter->data_value = DW_BLOCK (attr)->data;
10089 parameter->data_value_size = DW_BLOCK (attr)->size;
10090 }
10091 }
10092 }
10093 }
10094
10095 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
10096 Return 1 if the attributes are present and valid, otherwise, return 0.
10097 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
10098
10099 static int
10100 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
10101 CORE_ADDR *high_return, struct dwarf2_cu *cu,
10102 struct partial_symtab *ranges_pst)
10103 {
10104 struct objfile *objfile = cu->objfile;
10105 struct comp_unit_head *cu_header = &cu->header;
10106 bfd *obfd = objfile->obfd;
10107 unsigned int addr_size = cu_header->addr_size;
10108 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10109 /* Base address selection entry. */
10110 CORE_ADDR base;
10111 int found_base;
10112 unsigned int dummy;
10113 gdb_byte *buffer;
10114 CORE_ADDR marker;
10115 int low_set;
10116 CORE_ADDR low = 0;
10117 CORE_ADDR high = 0;
10118 CORE_ADDR baseaddr;
10119
10120 found_base = cu->base_known;
10121 base = cu->base_address;
10122
10123 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
10124 if (offset >= dwarf2_per_objfile->ranges.size)
10125 {
10126 complaint (&symfile_complaints,
10127 _("Offset %d out of bounds for DW_AT_ranges attribute"),
10128 offset);
10129 return 0;
10130 }
10131 buffer = dwarf2_per_objfile->ranges.buffer + offset;
10132
10133 /* Read in the largest possible address. */
10134 marker = read_address (obfd, buffer, cu, &dummy);
10135 if ((marker & mask) == mask)
10136 {
10137 /* If we found the largest possible address, then
10138 read the base address. */
10139 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10140 buffer += 2 * addr_size;
10141 offset += 2 * addr_size;
10142 found_base = 1;
10143 }
10144
10145 low_set = 0;
10146
10147 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10148
10149 while (1)
10150 {
10151 CORE_ADDR range_beginning, range_end;
10152
10153 range_beginning = read_address (obfd, buffer, cu, &dummy);
10154 buffer += addr_size;
10155 range_end = read_address (obfd, buffer, cu, &dummy);
10156 buffer += addr_size;
10157 offset += 2 * addr_size;
10158
10159 /* An end of list marker is a pair of zero addresses. */
10160 if (range_beginning == 0 && range_end == 0)
10161 /* Found the end of list entry. */
10162 break;
10163
10164 /* Each base address selection entry is a pair of 2 values.
10165 The first is the largest possible address, the second is
10166 the base address. Check for a base address here. */
10167 if ((range_beginning & mask) == mask)
10168 {
10169 /* If we found the largest possible address, then
10170 read the base address. */
10171 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10172 found_base = 1;
10173 continue;
10174 }
10175
10176 if (!found_base)
10177 {
10178 /* We have no valid base address for the ranges
10179 data. */
10180 complaint (&symfile_complaints,
10181 _("Invalid .debug_ranges data (no base address)"));
10182 return 0;
10183 }
10184
10185 if (range_beginning > range_end)
10186 {
10187 /* Inverted range entries are invalid. */
10188 complaint (&symfile_complaints,
10189 _("Invalid .debug_ranges data (inverted range)"));
10190 return 0;
10191 }
10192
10193 /* Empty range entries have no effect. */
10194 if (range_beginning == range_end)
10195 continue;
10196
10197 range_beginning += base;
10198 range_end += base;
10199
10200 /* A not-uncommon case of bad debug info.
10201 Don't pollute the addrmap with bad data. */
10202 if (range_beginning + baseaddr == 0
10203 && !dwarf2_per_objfile->has_section_at_zero)
10204 {
10205 complaint (&symfile_complaints,
10206 _(".debug_ranges entry has start address of zero"
10207 " [in module %s]"), objfile->name);
10208 continue;
10209 }
10210
10211 if (ranges_pst != NULL)
10212 addrmap_set_empty (objfile->psymtabs_addrmap,
10213 range_beginning + baseaddr,
10214 range_end - 1 + baseaddr,
10215 ranges_pst);
10216
10217 /* FIXME: This is recording everything as a low-high
10218 segment of consecutive addresses. We should have a
10219 data structure for discontiguous block ranges
10220 instead. */
10221 if (! low_set)
10222 {
10223 low = range_beginning;
10224 high = range_end;
10225 low_set = 1;
10226 }
10227 else
10228 {
10229 if (range_beginning < low)
10230 low = range_beginning;
10231 if (range_end > high)
10232 high = range_end;
10233 }
10234 }
10235
10236 if (! low_set)
10237 /* If the first entry is an end-of-list marker, the range
10238 describes an empty scope, i.e. no instructions. */
10239 return 0;
10240
10241 if (low_return)
10242 *low_return = low;
10243 if (high_return)
10244 *high_return = high;
10245 return 1;
10246 }
10247
10248 /* Get low and high pc attributes from a die. Return 1 if the attributes
10249 are present and valid, otherwise, return 0. Return -1 if the range is
10250 discontinuous, i.e. derived from DW_AT_ranges information. */
10251
10252 static int
10253 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
10254 CORE_ADDR *highpc, struct dwarf2_cu *cu,
10255 struct partial_symtab *pst)
10256 {
10257 struct attribute *attr;
10258 struct attribute *attr_high;
10259 CORE_ADDR low = 0;
10260 CORE_ADDR high = 0;
10261 int ret = 0;
10262
10263 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10264 if (attr_high)
10265 {
10266 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10267 if (attr)
10268 {
10269 low = DW_ADDR (attr);
10270 if (attr_high->form == DW_FORM_addr
10271 || attr_high->form == DW_FORM_GNU_addr_index)
10272 high = DW_ADDR (attr_high);
10273 else
10274 high = low + DW_UNSND (attr_high);
10275 }
10276 else
10277 /* Found high w/o low attribute. */
10278 return 0;
10279
10280 /* Found consecutive range of addresses. */
10281 ret = 1;
10282 }
10283 else
10284 {
10285 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10286 if (attr != NULL)
10287 {
10288 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10289 We take advantage of the fact that DW_AT_ranges does not appear
10290 in DW_TAG_compile_unit of DWO files. */
10291 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10292 unsigned int ranges_offset = (DW_UNSND (attr)
10293 + (need_ranges_base
10294 ? cu->ranges_base
10295 : 0));
10296
10297 /* Value of the DW_AT_ranges attribute is the offset in the
10298 .debug_ranges section. */
10299 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
10300 return 0;
10301 /* Found discontinuous range of addresses. */
10302 ret = -1;
10303 }
10304 }
10305
10306 /* read_partial_die has also the strict LOW < HIGH requirement. */
10307 if (high <= low)
10308 return 0;
10309
10310 /* When using the GNU linker, .gnu.linkonce. sections are used to
10311 eliminate duplicate copies of functions and vtables and such.
10312 The linker will arbitrarily choose one and discard the others.
10313 The AT_*_pc values for such functions refer to local labels in
10314 these sections. If the section from that file was discarded, the
10315 labels are not in the output, so the relocs get a value of 0.
10316 If this is a discarded function, mark the pc bounds as invalid,
10317 so that GDB will ignore it. */
10318 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
10319 return 0;
10320
10321 *lowpc = low;
10322 if (highpc)
10323 *highpc = high;
10324 return ret;
10325 }
10326
10327 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
10328 its low and high PC addresses. Do nothing if these addresses could not
10329 be determined. Otherwise, set LOWPC to the low address if it is smaller,
10330 and HIGHPC to the high address if greater than HIGHPC. */
10331
10332 static void
10333 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
10334 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10335 struct dwarf2_cu *cu)
10336 {
10337 CORE_ADDR low, high;
10338 struct die_info *child = die->child;
10339
10340 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
10341 {
10342 *lowpc = min (*lowpc, low);
10343 *highpc = max (*highpc, high);
10344 }
10345
10346 /* If the language does not allow nested subprograms (either inside
10347 subprograms or lexical blocks), we're done. */
10348 if (cu->language != language_ada)
10349 return;
10350
10351 /* Check all the children of the given DIE. If it contains nested
10352 subprograms, then check their pc bounds. Likewise, we need to
10353 check lexical blocks as well, as they may also contain subprogram
10354 definitions. */
10355 while (child && child->tag)
10356 {
10357 if (child->tag == DW_TAG_subprogram
10358 || child->tag == DW_TAG_lexical_block)
10359 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
10360 child = sibling_die (child);
10361 }
10362 }
10363
10364 /* Get the low and high pc's represented by the scope DIE, and store
10365 them in *LOWPC and *HIGHPC. If the correct values can't be
10366 determined, set *LOWPC to -1 and *HIGHPC to 0. */
10367
10368 static void
10369 get_scope_pc_bounds (struct die_info *die,
10370 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10371 struct dwarf2_cu *cu)
10372 {
10373 CORE_ADDR best_low = (CORE_ADDR) -1;
10374 CORE_ADDR best_high = (CORE_ADDR) 0;
10375 CORE_ADDR current_low, current_high;
10376
10377 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
10378 {
10379 best_low = current_low;
10380 best_high = current_high;
10381 }
10382 else
10383 {
10384 struct die_info *child = die->child;
10385
10386 while (child && child->tag)
10387 {
10388 switch (child->tag) {
10389 case DW_TAG_subprogram:
10390 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
10391 break;
10392 case DW_TAG_namespace:
10393 case DW_TAG_module:
10394 /* FIXME: carlton/2004-01-16: Should we do this for
10395 DW_TAG_class_type/DW_TAG_structure_type, too? I think
10396 that current GCC's always emit the DIEs corresponding
10397 to definitions of methods of classes as children of a
10398 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
10399 the DIEs giving the declarations, which could be
10400 anywhere). But I don't see any reason why the
10401 standards says that they have to be there. */
10402 get_scope_pc_bounds (child, &current_low, &current_high, cu);
10403
10404 if (current_low != ((CORE_ADDR) -1))
10405 {
10406 best_low = min (best_low, current_low);
10407 best_high = max (best_high, current_high);
10408 }
10409 break;
10410 default:
10411 /* Ignore. */
10412 break;
10413 }
10414
10415 child = sibling_die (child);
10416 }
10417 }
10418
10419 *lowpc = best_low;
10420 *highpc = best_high;
10421 }
10422
10423 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
10424 in DIE. */
10425
10426 static void
10427 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
10428 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
10429 {
10430 struct objfile *objfile = cu->objfile;
10431 struct attribute *attr;
10432 struct attribute *attr_high;
10433
10434 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10435 if (attr_high)
10436 {
10437 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10438 if (attr)
10439 {
10440 CORE_ADDR low = DW_ADDR (attr);
10441 CORE_ADDR high;
10442 if (attr_high->form == DW_FORM_addr
10443 || attr_high->form == DW_FORM_GNU_addr_index)
10444 high = DW_ADDR (attr_high);
10445 else
10446 high = low + DW_UNSND (attr_high);
10447
10448 record_block_range (block, baseaddr + low, baseaddr + high - 1);
10449 }
10450 }
10451
10452 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10453 if (attr)
10454 {
10455 bfd *obfd = objfile->obfd;
10456 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10457 We take advantage of the fact that DW_AT_ranges does not appear
10458 in DW_TAG_compile_unit of DWO files. */
10459 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10460
10461 /* The value of the DW_AT_ranges attribute is the offset of the
10462 address range list in the .debug_ranges section. */
10463 unsigned long offset = (DW_UNSND (attr)
10464 + (need_ranges_base ? cu->ranges_base : 0));
10465 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
10466
10467 /* For some target architectures, but not others, the
10468 read_address function sign-extends the addresses it returns.
10469 To recognize base address selection entries, we need a
10470 mask. */
10471 unsigned int addr_size = cu->header.addr_size;
10472 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10473
10474 /* The base address, to which the next pair is relative. Note
10475 that this 'base' is a DWARF concept: most entries in a range
10476 list are relative, to reduce the number of relocs against the
10477 debugging information. This is separate from this function's
10478 'baseaddr' argument, which GDB uses to relocate debugging
10479 information from a shared library based on the address at
10480 which the library was loaded. */
10481 CORE_ADDR base = cu->base_address;
10482 int base_known = cu->base_known;
10483
10484 gdb_assert (dwarf2_per_objfile->ranges.readin);
10485 if (offset >= dwarf2_per_objfile->ranges.size)
10486 {
10487 complaint (&symfile_complaints,
10488 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
10489 offset);
10490 return;
10491 }
10492
10493 for (;;)
10494 {
10495 unsigned int bytes_read;
10496 CORE_ADDR start, end;
10497
10498 start = read_address (obfd, buffer, cu, &bytes_read);
10499 buffer += bytes_read;
10500 end = read_address (obfd, buffer, cu, &bytes_read);
10501 buffer += bytes_read;
10502
10503 /* Did we find the end of the range list? */
10504 if (start == 0 && end == 0)
10505 break;
10506
10507 /* Did we find a base address selection entry? */
10508 else if ((start & base_select_mask) == base_select_mask)
10509 {
10510 base = end;
10511 base_known = 1;
10512 }
10513
10514 /* We found an ordinary address range. */
10515 else
10516 {
10517 if (!base_known)
10518 {
10519 complaint (&symfile_complaints,
10520 _("Invalid .debug_ranges data "
10521 "(no base address)"));
10522 return;
10523 }
10524
10525 if (start > end)
10526 {
10527 /* Inverted range entries are invalid. */
10528 complaint (&symfile_complaints,
10529 _("Invalid .debug_ranges data "
10530 "(inverted range)"));
10531 return;
10532 }
10533
10534 /* Empty range entries have no effect. */
10535 if (start == end)
10536 continue;
10537
10538 start += base + baseaddr;
10539 end += base + baseaddr;
10540
10541 /* A not-uncommon case of bad debug info.
10542 Don't pollute the addrmap with bad data. */
10543 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
10544 {
10545 complaint (&symfile_complaints,
10546 _(".debug_ranges entry has start address of zero"
10547 " [in module %s]"), objfile->name);
10548 continue;
10549 }
10550
10551 record_block_range (block, start, end - 1);
10552 }
10553 }
10554 }
10555 }
10556
10557 /* Check whether the producer field indicates either of GCC < 4.6, or the
10558 Intel C/C++ compiler, and cache the result in CU. */
10559
10560 static void
10561 check_producer (struct dwarf2_cu *cu)
10562 {
10563 const char *cs;
10564 int major, minor, release;
10565
10566 if (cu->producer == NULL)
10567 {
10568 /* For unknown compilers expect their behavior is DWARF version
10569 compliant.
10570
10571 GCC started to support .debug_types sections by -gdwarf-4 since
10572 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
10573 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
10574 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
10575 interpreted incorrectly by GDB now - GCC PR debug/48229. */
10576 }
10577 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
10578 {
10579 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
10580
10581 cs = &cu->producer[strlen ("GNU ")];
10582 while (*cs && !isdigit (*cs))
10583 cs++;
10584 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
10585 {
10586 /* Not recognized as GCC. */
10587 }
10588 else
10589 {
10590 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
10591 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
10592 }
10593 }
10594 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
10595 cu->producer_is_icc = 1;
10596 else
10597 {
10598 /* For other non-GCC compilers, expect their behavior is DWARF version
10599 compliant. */
10600 }
10601
10602 cu->checked_producer = 1;
10603 }
10604
10605 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
10606 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
10607 during 4.6.0 experimental. */
10608
10609 static int
10610 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
10611 {
10612 if (!cu->checked_producer)
10613 check_producer (cu);
10614
10615 return cu->producer_is_gxx_lt_4_6;
10616 }
10617
10618 /* Return the default accessibility type if it is not overriden by
10619 DW_AT_accessibility. */
10620
10621 static enum dwarf_access_attribute
10622 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
10623 {
10624 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
10625 {
10626 /* The default DWARF 2 accessibility for members is public, the default
10627 accessibility for inheritance is private. */
10628
10629 if (die->tag != DW_TAG_inheritance)
10630 return DW_ACCESS_public;
10631 else
10632 return DW_ACCESS_private;
10633 }
10634 else
10635 {
10636 /* DWARF 3+ defines the default accessibility a different way. The same
10637 rules apply now for DW_TAG_inheritance as for the members and it only
10638 depends on the container kind. */
10639
10640 if (die->parent->tag == DW_TAG_class_type)
10641 return DW_ACCESS_private;
10642 else
10643 return DW_ACCESS_public;
10644 }
10645 }
10646
10647 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
10648 offset. If the attribute was not found return 0, otherwise return
10649 1. If it was found but could not properly be handled, set *OFFSET
10650 to 0. */
10651
10652 static int
10653 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
10654 LONGEST *offset)
10655 {
10656 struct attribute *attr;
10657
10658 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
10659 if (attr != NULL)
10660 {
10661 *offset = 0;
10662
10663 /* Note that we do not check for a section offset first here.
10664 This is because DW_AT_data_member_location is new in DWARF 4,
10665 so if we see it, we can assume that a constant form is really
10666 a constant and not a section offset. */
10667 if (attr_form_is_constant (attr))
10668 *offset = dwarf2_get_attr_constant_value (attr, 0);
10669 else if (attr_form_is_section_offset (attr))
10670 dwarf2_complex_location_expr_complaint ();
10671 else if (attr_form_is_block (attr))
10672 *offset = decode_locdesc (DW_BLOCK (attr), cu);
10673 else
10674 dwarf2_complex_location_expr_complaint ();
10675
10676 return 1;
10677 }
10678
10679 return 0;
10680 }
10681
10682 /* Add an aggregate field to the field list. */
10683
10684 static void
10685 dwarf2_add_field (struct field_info *fip, struct die_info *die,
10686 struct dwarf2_cu *cu)
10687 {
10688 struct objfile *objfile = cu->objfile;
10689 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10690 struct nextfield *new_field;
10691 struct attribute *attr;
10692 struct field *fp;
10693 const char *fieldname = "";
10694
10695 /* Allocate a new field list entry and link it in. */
10696 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
10697 make_cleanup (xfree, new_field);
10698 memset (new_field, 0, sizeof (struct nextfield));
10699
10700 if (die->tag == DW_TAG_inheritance)
10701 {
10702 new_field->next = fip->baseclasses;
10703 fip->baseclasses = new_field;
10704 }
10705 else
10706 {
10707 new_field->next = fip->fields;
10708 fip->fields = new_field;
10709 }
10710 fip->nfields++;
10711
10712 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
10713 if (attr)
10714 new_field->accessibility = DW_UNSND (attr);
10715 else
10716 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
10717 if (new_field->accessibility != DW_ACCESS_public)
10718 fip->non_public_fields = 1;
10719
10720 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
10721 if (attr)
10722 new_field->virtuality = DW_UNSND (attr);
10723 else
10724 new_field->virtuality = DW_VIRTUALITY_none;
10725
10726 fp = &new_field->field;
10727
10728 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
10729 {
10730 LONGEST offset;
10731
10732 /* Data member other than a C++ static data member. */
10733
10734 /* Get type of field. */
10735 fp->type = die_type (die, cu);
10736
10737 SET_FIELD_BITPOS (*fp, 0);
10738
10739 /* Get bit size of field (zero if none). */
10740 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
10741 if (attr)
10742 {
10743 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
10744 }
10745 else
10746 {
10747 FIELD_BITSIZE (*fp) = 0;
10748 }
10749
10750 /* Get bit offset of field. */
10751 if (handle_data_member_location (die, cu, &offset))
10752 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
10753 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
10754 if (attr)
10755 {
10756 if (gdbarch_bits_big_endian (gdbarch))
10757 {
10758 /* For big endian bits, the DW_AT_bit_offset gives the
10759 additional bit offset from the MSB of the containing
10760 anonymous object to the MSB of the field. We don't
10761 have to do anything special since we don't need to
10762 know the size of the anonymous object. */
10763 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
10764 }
10765 else
10766 {
10767 /* For little endian bits, compute the bit offset to the
10768 MSB of the anonymous object, subtract off the number of
10769 bits from the MSB of the field to the MSB of the
10770 object, and then subtract off the number of bits of
10771 the field itself. The result is the bit offset of
10772 the LSB of the field. */
10773 int anonymous_size;
10774 int bit_offset = DW_UNSND (attr);
10775
10776 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10777 if (attr)
10778 {
10779 /* The size of the anonymous object containing
10780 the bit field is explicit, so use the
10781 indicated size (in bytes). */
10782 anonymous_size = DW_UNSND (attr);
10783 }
10784 else
10785 {
10786 /* The size of the anonymous object containing
10787 the bit field must be inferred from the type
10788 attribute of the data member containing the
10789 bit field. */
10790 anonymous_size = TYPE_LENGTH (fp->type);
10791 }
10792 SET_FIELD_BITPOS (*fp,
10793 (FIELD_BITPOS (*fp)
10794 + anonymous_size * bits_per_byte
10795 - bit_offset - FIELD_BITSIZE (*fp)));
10796 }
10797 }
10798
10799 /* Get name of field. */
10800 fieldname = dwarf2_name (die, cu);
10801 if (fieldname == NULL)
10802 fieldname = "";
10803
10804 /* The name is already allocated along with this objfile, so we don't
10805 need to duplicate it for the type. */
10806 fp->name = fieldname;
10807
10808 /* Change accessibility for artificial fields (e.g. virtual table
10809 pointer or virtual base class pointer) to private. */
10810 if (dwarf2_attr (die, DW_AT_artificial, cu))
10811 {
10812 FIELD_ARTIFICIAL (*fp) = 1;
10813 new_field->accessibility = DW_ACCESS_private;
10814 fip->non_public_fields = 1;
10815 }
10816 }
10817 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
10818 {
10819 /* C++ static member. */
10820
10821 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
10822 is a declaration, but all versions of G++ as of this writing
10823 (so through at least 3.2.1) incorrectly generate
10824 DW_TAG_variable tags. */
10825
10826 const char *physname;
10827
10828 /* Get name of field. */
10829 fieldname = dwarf2_name (die, cu);
10830 if (fieldname == NULL)
10831 return;
10832
10833 attr = dwarf2_attr (die, DW_AT_const_value, cu);
10834 if (attr
10835 /* Only create a symbol if this is an external value.
10836 new_symbol checks this and puts the value in the global symbol
10837 table, which we want. If it is not external, new_symbol
10838 will try to put the value in cu->list_in_scope which is wrong. */
10839 && dwarf2_flag_true_p (die, DW_AT_external, cu))
10840 {
10841 /* A static const member, not much different than an enum as far as
10842 we're concerned, except that we can support more types. */
10843 new_symbol (die, NULL, cu);
10844 }
10845
10846 /* Get physical name. */
10847 physname = dwarf2_physname (fieldname, die, cu);
10848
10849 /* The name is already allocated along with this objfile, so we don't
10850 need to duplicate it for the type. */
10851 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
10852 FIELD_TYPE (*fp) = die_type (die, cu);
10853 FIELD_NAME (*fp) = fieldname;
10854 }
10855 else if (die->tag == DW_TAG_inheritance)
10856 {
10857 LONGEST offset;
10858
10859 /* C++ base class field. */
10860 if (handle_data_member_location (die, cu, &offset))
10861 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
10862 FIELD_BITSIZE (*fp) = 0;
10863 FIELD_TYPE (*fp) = die_type (die, cu);
10864 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
10865 fip->nbaseclasses++;
10866 }
10867 }
10868
10869 /* Add a typedef defined in the scope of the FIP's class. */
10870
10871 static void
10872 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
10873 struct dwarf2_cu *cu)
10874 {
10875 struct objfile *objfile = cu->objfile;
10876 struct typedef_field_list *new_field;
10877 struct attribute *attr;
10878 struct typedef_field *fp;
10879 char *fieldname = "";
10880
10881 /* Allocate a new field list entry and link it in. */
10882 new_field = xzalloc (sizeof (*new_field));
10883 make_cleanup (xfree, new_field);
10884
10885 gdb_assert (die->tag == DW_TAG_typedef);
10886
10887 fp = &new_field->field;
10888
10889 /* Get name of field. */
10890 fp->name = dwarf2_name (die, cu);
10891 if (fp->name == NULL)
10892 return;
10893
10894 fp->type = read_type_die (die, cu);
10895
10896 new_field->next = fip->typedef_field_list;
10897 fip->typedef_field_list = new_field;
10898 fip->typedef_field_list_count++;
10899 }
10900
10901 /* Create the vector of fields, and attach it to the type. */
10902
10903 static void
10904 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
10905 struct dwarf2_cu *cu)
10906 {
10907 int nfields = fip->nfields;
10908
10909 /* Record the field count, allocate space for the array of fields,
10910 and create blank accessibility bitfields if necessary. */
10911 TYPE_NFIELDS (type) = nfields;
10912 TYPE_FIELDS (type) = (struct field *)
10913 TYPE_ALLOC (type, sizeof (struct field) * nfields);
10914 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
10915
10916 if (fip->non_public_fields && cu->language != language_ada)
10917 {
10918 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10919
10920 TYPE_FIELD_PRIVATE_BITS (type) =
10921 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10922 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
10923
10924 TYPE_FIELD_PROTECTED_BITS (type) =
10925 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10926 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
10927
10928 TYPE_FIELD_IGNORE_BITS (type) =
10929 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10930 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
10931 }
10932
10933 /* If the type has baseclasses, allocate and clear a bit vector for
10934 TYPE_FIELD_VIRTUAL_BITS. */
10935 if (fip->nbaseclasses && cu->language != language_ada)
10936 {
10937 int num_bytes = B_BYTES (fip->nbaseclasses);
10938 unsigned char *pointer;
10939
10940 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10941 pointer = TYPE_ALLOC (type, num_bytes);
10942 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
10943 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
10944 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
10945 }
10946
10947 /* Copy the saved-up fields into the field vector. Start from the head of
10948 the list, adding to the tail of the field array, so that they end up in
10949 the same order in the array in which they were added to the list. */
10950 while (nfields-- > 0)
10951 {
10952 struct nextfield *fieldp;
10953
10954 if (fip->fields)
10955 {
10956 fieldp = fip->fields;
10957 fip->fields = fieldp->next;
10958 }
10959 else
10960 {
10961 fieldp = fip->baseclasses;
10962 fip->baseclasses = fieldp->next;
10963 }
10964
10965 TYPE_FIELD (type, nfields) = fieldp->field;
10966 switch (fieldp->accessibility)
10967 {
10968 case DW_ACCESS_private:
10969 if (cu->language != language_ada)
10970 SET_TYPE_FIELD_PRIVATE (type, nfields);
10971 break;
10972
10973 case DW_ACCESS_protected:
10974 if (cu->language != language_ada)
10975 SET_TYPE_FIELD_PROTECTED (type, nfields);
10976 break;
10977
10978 case DW_ACCESS_public:
10979 break;
10980
10981 default:
10982 /* Unknown accessibility. Complain and treat it as public. */
10983 {
10984 complaint (&symfile_complaints, _("unsupported accessibility %d"),
10985 fieldp->accessibility);
10986 }
10987 break;
10988 }
10989 if (nfields < fip->nbaseclasses)
10990 {
10991 switch (fieldp->virtuality)
10992 {
10993 case DW_VIRTUALITY_virtual:
10994 case DW_VIRTUALITY_pure_virtual:
10995 if (cu->language == language_ada)
10996 error (_("unexpected virtuality in component of Ada type"));
10997 SET_TYPE_FIELD_VIRTUAL (type, nfields);
10998 break;
10999 }
11000 }
11001 }
11002 }
11003
11004 /* Return true if this member function is a constructor, false
11005 otherwise. */
11006
11007 static int
11008 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
11009 {
11010 const char *fieldname;
11011 const char *typename;
11012 int len;
11013
11014 if (die->parent == NULL)
11015 return 0;
11016
11017 if (die->parent->tag != DW_TAG_structure_type
11018 && die->parent->tag != DW_TAG_union_type
11019 && die->parent->tag != DW_TAG_class_type)
11020 return 0;
11021
11022 fieldname = dwarf2_name (die, cu);
11023 typename = dwarf2_name (die->parent, cu);
11024 if (fieldname == NULL || typename == NULL)
11025 return 0;
11026
11027 len = strlen (fieldname);
11028 return (strncmp (fieldname, typename, len) == 0
11029 && (typename[len] == '\0' || typename[len] == '<'));
11030 }
11031
11032 /* Add a member function to the proper fieldlist. */
11033
11034 static void
11035 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
11036 struct type *type, struct dwarf2_cu *cu)
11037 {
11038 struct objfile *objfile = cu->objfile;
11039 struct attribute *attr;
11040 struct fnfieldlist *flp;
11041 int i;
11042 struct fn_field *fnp;
11043 const char *fieldname;
11044 struct nextfnfield *new_fnfield;
11045 struct type *this_type;
11046 enum dwarf_access_attribute accessibility;
11047
11048 if (cu->language == language_ada)
11049 error (_("unexpected member function in Ada type"));
11050
11051 /* Get name of member function. */
11052 fieldname = dwarf2_name (die, cu);
11053 if (fieldname == NULL)
11054 return;
11055
11056 /* Look up member function name in fieldlist. */
11057 for (i = 0; i < fip->nfnfields; i++)
11058 {
11059 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
11060 break;
11061 }
11062
11063 /* Create new list element if necessary. */
11064 if (i < fip->nfnfields)
11065 flp = &fip->fnfieldlists[i];
11066 else
11067 {
11068 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
11069 {
11070 fip->fnfieldlists = (struct fnfieldlist *)
11071 xrealloc (fip->fnfieldlists,
11072 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
11073 * sizeof (struct fnfieldlist));
11074 if (fip->nfnfields == 0)
11075 make_cleanup (free_current_contents, &fip->fnfieldlists);
11076 }
11077 flp = &fip->fnfieldlists[fip->nfnfields];
11078 flp->name = fieldname;
11079 flp->length = 0;
11080 flp->head = NULL;
11081 i = fip->nfnfields++;
11082 }
11083
11084 /* Create a new member function field and chain it to the field list
11085 entry. */
11086 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
11087 make_cleanup (xfree, new_fnfield);
11088 memset (new_fnfield, 0, sizeof (struct nextfnfield));
11089 new_fnfield->next = flp->head;
11090 flp->head = new_fnfield;
11091 flp->length++;
11092
11093 /* Fill in the member function field info. */
11094 fnp = &new_fnfield->fnfield;
11095
11096 /* Delay processing of the physname until later. */
11097 if (cu->language == language_cplus || cu->language == language_java)
11098 {
11099 add_to_method_list (type, i, flp->length - 1, fieldname,
11100 die, cu);
11101 }
11102 else
11103 {
11104 const char *physname = dwarf2_physname (fieldname, die, cu);
11105 fnp->physname = physname ? physname : "";
11106 }
11107
11108 fnp->type = alloc_type (objfile);
11109 this_type = read_type_die (die, cu);
11110 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
11111 {
11112 int nparams = TYPE_NFIELDS (this_type);
11113
11114 /* TYPE is the domain of this method, and THIS_TYPE is the type
11115 of the method itself (TYPE_CODE_METHOD). */
11116 smash_to_method_type (fnp->type, type,
11117 TYPE_TARGET_TYPE (this_type),
11118 TYPE_FIELDS (this_type),
11119 TYPE_NFIELDS (this_type),
11120 TYPE_VARARGS (this_type));
11121
11122 /* Handle static member functions.
11123 Dwarf2 has no clean way to discern C++ static and non-static
11124 member functions. G++ helps GDB by marking the first
11125 parameter for non-static member functions (which is the this
11126 pointer) as artificial. We obtain this information from
11127 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
11128 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
11129 fnp->voffset = VOFFSET_STATIC;
11130 }
11131 else
11132 complaint (&symfile_complaints, _("member function type missing for '%s'"),
11133 dwarf2_full_name (fieldname, die, cu));
11134
11135 /* Get fcontext from DW_AT_containing_type if present. */
11136 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
11137 fnp->fcontext = die_containing_type (die, cu);
11138
11139 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
11140 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
11141
11142 /* Get accessibility. */
11143 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
11144 if (attr)
11145 accessibility = DW_UNSND (attr);
11146 else
11147 accessibility = dwarf2_default_access_attribute (die, cu);
11148 switch (accessibility)
11149 {
11150 case DW_ACCESS_private:
11151 fnp->is_private = 1;
11152 break;
11153 case DW_ACCESS_protected:
11154 fnp->is_protected = 1;
11155 break;
11156 }
11157
11158 /* Check for artificial methods. */
11159 attr = dwarf2_attr (die, DW_AT_artificial, cu);
11160 if (attr && DW_UNSND (attr) != 0)
11161 fnp->is_artificial = 1;
11162
11163 fnp->is_constructor = dwarf2_is_constructor (die, cu);
11164
11165 /* Get index in virtual function table if it is a virtual member
11166 function. For older versions of GCC, this is an offset in the
11167 appropriate virtual table, as specified by DW_AT_containing_type.
11168 For everyone else, it is an expression to be evaluated relative
11169 to the object address. */
11170
11171 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
11172 if (attr)
11173 {
11174 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
11175 {
11176 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
11177 {
11178 /* Old-style GCC. */
11179 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
11180 }
11181 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
11182 || (DW_BLOCK (attr)->size > 1
11183 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
11184 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
11185 {
11186 struct dwarf_block blk;
11187 int offset;
11188
11189 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
11190 ? 1 : 2);
11191 blk.size = DW_BLOCK (attr)->size - offset;
11192 blk.data = DW_BLOCK (attr)->data + offset;
11193 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
11194 if ((fnp->voffset % cu->header.addr_size) != 0)
11195 dwarf2_complex_location_expr_complaint ();
11196 else
11197 fnp->voffset /= cu->header.addr_size;
11198 fnp->voffset += 2;
11199 }
11200 else
11201 dwarf2_complex_location_expr_complaint ();
11202
11203 if (!fnp->fcontext)
11204 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
11205 }
11206 else if (attr_form_is_section_offset (attr))
11207 {
11208 dwarf2_complex_location_expr_complaint ();
11209 }
11210 else
11211 {
11212 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
11213 fieldname);
11214 }
11215 }
11216 else
11217 {
11218 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11219 if (attr && DW_UNSND (attr))
11220 {
11221 /* GCC does this, as of 2008-08-25; PR debug/37237. */
11222 complaint (&symfile_complaints,
11223 _("Member function \"%s\" (offset %d) is virtual "
11224 "but the vtable offset is not specified"),
11225 fieldname, die->offset.sect_off);
11226 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11227 TYPE_CPLUS_DYNAMIC (type) = 1;
11228 }
11229 }
11230 }
11231
11232 /* Create the vector of member function fields, and attach it to the type. */
11233
11234 static void
11235 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
11236 struct dwarf2_cu *cu)
11237 {
11238 struct fnfieldlist *flp;
11239 int i;
11240
11241 if (cu->language == language_ada)
11242 error (_("unexpected member functions in Ada type"));
11243
11244 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11245 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
11246 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
11247
11248 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
11249 {
11250 struct nextfnfield *nfp = flp->head;
11251 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
11252 int k;
11253
11254 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
11255 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
11256 fn_flp->fn_fields = (struct fn_field *)
11257 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
11258 for (k = flp->length; (k--, nfp); nfp = nfp->next)
11259 fn_flp->fn_fields[k] = nfp->fnfield;
11260 }
11261
11262 TYPE_NFN_FIELDS (type) = fip->nfnfields;
11263 }
11264
11265 /* Returns non-zero if NAME is the name of a vtable member in CU's
11266 language, zero otherwise. */
11267 static int
11268 is_vtable_name (const char *name, struct dwarf2_cu *cu)
11269 {
11270 static const char vptr[] = "_vptr";
11271 static const char vtable[] = "vtable";
11272
11273 /* Look for the C++ and Java forms of the vtable. */
11274 if ((cu->language == language_java
11275 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
11276 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
11277 && is_cplus_marker (name[sizeof (vptr) - 1])))
11278 return 1;
11279
11280 return 0;
11281 }
11282
11283 /* GCC outputs unnamed structures that are really pointers to member
11284 functions, with the ABI-specified layout. If TYPE describes
11285 such a structure, smash it into a member function type.
11286
11287 GCC shouldn't do this; it should just output pointer to member DIEs.
11288 This is GCC PR debug/28767. */
11289
11290 static void
11291 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
11292 {
11293 struct type *pfn_type, *domain_type, *new_type;
11294
11295 /* Check for a structure with no name and two children. */
11296 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
11297 return;
11298
11299 /* Check for __pfn and __delta members. */
11300 if (TYPE_FIELD_NAME (type, 0) == NULL
11301 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
11302 || TYPE_FIELD_NAME (type, 1) == NULL
11303 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
11304 return;
11305
11306 /* Find the type of the method. */
11307 pfn_type = TYPE_FIELD_TYPE (type, 0);
11308 if (pfn_type == NULL
11309 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
11310 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
11311 return;
11312
11313 /* Look for the "this" argument. */
11314 pfn_type = TYPE_TARGET_TYPE (pfn_type);
11315 if (TYPE_NFIELDS (pfn_type) == 0
11316 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
11317 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
11318 return;
11319
11320 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
11321 new_type = alloc_type (objfile);
11322 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
11323 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
11324 TYPE_VARARGS (pfn_type));
11325 smash_to_methodptr_type (type, new_type);
11326 }
11327
11328 /* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
11329 (icc). */
11330
11331 static int
11332 producer_is_icc (struct dwarf2_cu *cu)
11333 {
11334 if (!cu->checked_producer)
11335 check_producer (cu);
11336
11337 return cu->producer_is_icc;
11338 }
11339
11340 /* Called when we find the DIE that starts a structure or union scope
11341 (definition) to create a type for the structure or union. Fill in
11342 the type's name and general properties; the members will not be
11343 processed until process_structure_scope.
11344
11345 NOTE: we need to call these functions regardless of whether or not the
11346 DIE has a DW_AT_name attribute, since it might be an anonymous
11347 structure or union. This gets the type entered into our set of
11348 user defined types.
11349
11350 However, if the structure is incomplete (an opaque struct/union)
11351 then suppress creating a symbol table entry for it since gdb only
11352 wants to find the one with the complete definition. Note that if
11353 it is complete, we just call new_symbol, which does it's own
11354 checking about whether the struct/union is anonymous or not (and
11355 suppresses creating a symbol table entry itself). */
11356
11357 static struct type *
11358 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
11359 {
11360 struct objfile *objfile = cu->objfile;
11361 struct type *type;
11362 struct attribute *attr;
11363 const char *name;
11364
11365 /* If the definition of this type lives in .debug_types, read that type.
11366 Don't follow DW_AT_specification though, that will take us back up
11367 the chain and we want to go down. */
11368 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
11369 if (attr)
11370 {
11371 struct dwarf2_cu *type_cu = cu;
11372 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11373
11374 /* We could just recurse on read_structure_type, but we need to call
11375 get_die_type to ensure only one type for this DIE is created.
11376 This is important, for example, because for c++ classes we need
11377 TYPE_NAME set which is only done by new_symbol. Blech. */
11378 type = read_type_die (type_die, type_cu);
11379
11380 /* TYPE_CU may not be the same as CU.
11381 Ensure TYPE is recorded in CU's type_hash table. */
11382 return set_die_type (die, type, cu);
11383 }
11384
11385 type = alloc_type (objfile);
11386 INIT_CPLUS_SPECIFIC (type);
11387
11388 name = dwarf2_name (die, cu);
11389 if (name != NULL)
11390 {
11391 if (cu->language == language_cplus
11392 || cu->language == language_java)
11393 {
11394 const char *full_name = dwarf2_full_name (name, die, cu);
11395
11396 /* dwarf2_full_name might have already finished building the DIE's
11397 type. If so, there is no need to continue. */
11398 if (get_die_type (die, cu) != NULL)
11399 return get_die_type (die, cu);
11400
11401 TYPE_TAG_NAME (type) = full_name;
11402 if (die->tag == DW_TAG_structure_type
11403 || die->tag == DW_TAG_class_type)
11404 TYPE_NAME (type) = TYPE_TAG_NAME (type);
11405 }
11406 else
11407 {
11408 /* The name is already allocated along with this objfile, so
11409 we don't need to duplicate it for the type. */
11410 TYPE_TAG_NAME (type) = name;
11411 if (die->tag == DW_TAG_class_type)
11412 TYPE_NAME (type) = TYPE_TAG_NAME (type);
11413 }
11414 }
11415
11416 if (die->tag == DW_TAG_structure_type)
11417 {
11418 TYPE_CODE (type) = TYPE_CODE_STRUCT;
11419 }
11420 else if (die->tag == DW_TAG_union_type)
11421 {
11422 TYPE_CODE (type) = TYPE_CODE_UNION;
11423 }
11424 else
11425 {
11426 TYPE_CODE (type) = TYPE_CODE_CLASS;
11427 }
11428
11429 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
11430 TYPE_DECLARED_CLASS (type) = 1;
11431
11432 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11433 if (attr)
11434 {
11435 TYPE_LENGTH (type) = DW_UNSND (attr);
11436 }
11437 else
11438 {
11439 TYPE_LENGTH (type) = 0;
11440 }
11441
11442 if (producer_is_icc (cu))
11443 {
11444 /* ICC does not output the required DW_AT_declaration
11445 on incomplete types, but gives them a size of zero. */
11446 }
11447 else
11448 TYPE_STUB_SUPPORTED (type) = 1;
11449
11450 if (die_is_declaration (die, cu))
11451 TYPE_STUB (type) = 1;
11452 else if (attr == NULL && die->child == NULL
11453 && producer_is_realview (cu->producer))
11454 /* RealView does not output the required DW_AT_declaration
11455 on incomplete types. */
11456 TYPE_STUB (type) = 1;
11457
11458 /* We need to add the type field to the die immediately so we don't
11459 infinitely recurse when dealing with pointers to the structure
11460 type within the structure itself. */
11461 set_die_type (die, type, cu);
11462
11463 /* set_die_type should be already done. */
11464 set_descriptive_type (type, die, cu);
11465
11466 return type;
11467 }
11468
11469 /* Finish creating a structure or union type, including filling in
11470 its members and creating a symbol for it. */
11471
11472 static void
11473 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
11474 {
11475 struct objfile *objfile = cu->objfile;
11476 struct die_info *child_die = die->child;
11477 struct type *type;
11478
11479 type = get_die_type (die, cu);
11480 if (type == NULL)
11481 type = read_structure_type (die, cu);
11482
11483 if (die->child != NULL && ! die_is_declaration (die, cu))
11484 {
11485 struct field_info fi;
11486 struct die_info *child_die;
11487 VEC (symbolp) *template_args = NULL;
11488 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
11489
11490 memset (&fi, 0, sizeof (struct field_info));
11491
11492 child_die = die->child;
11493
11494 while (child_die && child_die->tag)
11495 {
11496 if (child_die->tag == DW_TAG_member
11497 || child_die->tag == DW_TAG_variable)
11498 {
11499 /* NOTE: carlton/2002-11-05: A C++ static data member
11500 should be a DW_TAG_member that is a declaration, but
11501 all versions of G++ as of this writing (so through at
11502 least 3.2.1) incorrectly generate DW_TAG_variable
11503 tags for them instead. */
11504 dwarf2_add_field (&fi, child_die, cu);
11505 }
11506 else if (child_die->tag == DW_TAG_subprogram)
11507 {
11508 /* C++ member function. */
11509 dwarf2_add_member_fn (&fi, child_die, type, cu);
11510 }
11511 else if (child_die->tag == DW_TAG_inheritance)
11512 {
11513 /* C++ base class field. */
11514 dwarf2_add_field (&fi, child_die, cu);
11515 }
11516 else if (child_die->tag == DW_TAG_typedef)
11517 dwarf2_add_typedef (&fi, child_die, cu);
11518 else if (child_die->tag == DW_TAG_template_type_param
11519 || child_die->tag == DW_TAG_template_value_param)
11520 {
11521 struct symbol *arg = new_symbol (child_die, NULL, cu);
11522
11523 if (arg != NULL)
11524 VEC_safe_push (symbolp, template_args, arg);
11525 }
11526
11527 child_die = sibling_die (child_die);
11528 }
11529
11530 /* Attach template arguments to type. */
11531 if (! VEC_empty (symbolp, template_args))
11532 {
11533 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11534 TYPE_N_TEMPLATE_ARGUMENTS (type)
11535 = VEC_length (symbolp, template_args);
11536 TYPE_TEMPLATE_ARGUMENTS (type)
11537 = obstack_alloc (&objfile->objfile_obstack,
11538 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11539 * sizeof (struct symbol *)));
11540 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
11541 VEC_address (symbolp, template_args),
11542 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11543 * sizeof (struct symbol *)));
11544 VEC_free (symbolp, template_args);
11545 }
11546
11547 /* Attach fields and member functions to the type. */
11548 if (fi.nfields)
11549 dwarf2_attach_fields_to_type (&fi, type, cu);
11550 if (fi.nfnfields)
11551 {
11552 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
11553
11554 /* Get the type which refers to the base class (possibly this
11555 class itself) which contains the vtable pointer for the current
11556 class from the DW_AT_containing_type attribute. This use of
11557 DW_AT_containing_type is a GNU extension. */
11558
11559 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
11560 {
11561 struct type *t = die_containing_type (die, cu);
11562
11563 TYPE_VPTR_BASETYPE (type) = t;
11564 if (type == t)
11565 {
11566 int i;
11567
11568 /* Our own class provides vtbl ptr. */
11569 for (i = TYPE_NFIELDS (t) - 1;
11570 i >= TYPE_N_BASECLASSES (t);
11571 --i)
11572 {
11573 const char *fieldname = TYPE_FIELD_NAME (t, i);
11574
11575 if (is_vtable_name (fieldname, cu))
11576 {
11577 TYPE_VPTR_FIELDNO (type) = i;
11578 break;
11579 }
11580 }
11581
11582 /* Complain if virtual function table field not found. */
11583 if (i < TYPE_N_BASECLASSES (t))
11584 complaint (&symfile_complaints,
11585 _("virtual function table pointer "
11586 "not found when defining class '%s'"),
11587 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
11588 "");
11589 }
11590 else
11591 {
11592 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
11593 }
11594 }
11595 else if (cu->producer
11596 && strncmp (cu->producer,
11597 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
11598 {
11599 /* The IBM XLC compiler does not provide direct indication
11600 of the containing type, but the vtable pointer is
11601 always named __vfp. */
11602
11603 int i;
11604
11605 for (i = TYPE_NFIELDS (type) - 1;
11606 i >= TYPE_N_BASECLASSES (type);
11607 --i)
11608 {
11609 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
11610 {
11611 TYPE_VPTR_FIELDNO (type) = i;
11612 TYPE_VPTR_BASETYPE (type) = type;
11613 break;
11614 }
11615 }
11616 }
11617 }
11618
11619 /* Copy fi.typedef_field_list linked list elements content into the
11620 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
11621 if (fi.typedef_field_list)
11622 {
11623 int i = fi.typedef_field_list_count;
11624
11625 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11626 TYPE_TYPEDEF_FIELD_ARRAY (type)
11627 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
11628 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
11629
11630 /* Reverse the list order to keep the debug info elements order. */
11631 while (--i >= 0)
11632 {
11633 struct typedef_field *dest, *src;
11634
11635 dest = &TYPE_TYPEDEF_FIELD (type, i);
11636 src = &fi.typedef_field_list->field;
11637 fi.typedef_field_list = fi.typedef_field_list->next;
11638 *dest = *src;
11639 }
11640 }
11641
11642 do_cleanups (back_to);
11643
11644 if (HAVE_CPLUS_STRUCT (type))
11645 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
11646 }
11647
11648 quirk_gcc_member_function_pointer (type, objfile);
11649
11650 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
11651 snapshots) has been known to create a die giving a declaration
11652 for a class that has, as a child, a die giving a definition for a
11653 nested class. So we have to process our children even if the
11654 current die is a declaration. Normally, of course, a declaration
11655 won't have any children at all. */
11656
11657 while (child_die != NULL && child_die->tag)
11658 {
11659 if (child_die->tag == DW_TAG_member
11660 || child_die->tag == DW_TAG_variable
11661 || child_die->tag == DW_TAG_inheritance
11662 || child_die->tag == DW_TAG_template_value_param
11663 || child_die->tag == DW_TAG_template_type_param)
11664 {
11665 /* Do nothing. */
11666 }
11667 else
11668 process_die (child_die, cu);
11669
11670 child_die = sibling_die (child_die);
11671 }
11672
11673 /* Do not consider external references. According to the DWARF standard,
11674 these DIEs are identified by the fact that they have no byte_size
11675 attribute, and a declaration attribute. */
11676 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
11677 || !die_is_declaration (die, cu))
11678 new_symbol (die, type, cu);
11679 }
11680
11681 /* Given a DW_AT_enumeration_type die, set its type. We do not
11682 complete the type's fields yet, or create any symbols. */
11683
11684 static struct type *
11685 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
11686 {
11687 struct objfile *objfile = cu->objfile;
11688 struct type *type;
11689 struct attribute *attr;
11690 const char *name;
11691
11692 /* If the definition of this type lives in .debug_types, read that type.
11693 Don't follow DW_AT_specification though, that will take us back up
11694 the chain and we want to go down. */
11695 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
11696 if (attr)
11697 {
11698 struct dwarf2_cu *type_cu = cu;
11699 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11700
11701 type = read_type_die (type_die, type_cu);
11702
11703 /* TYPE_CU may not be the same as CU.
11704 Ensure TYPE is recorded in CU's type_hash table. */
11705 return set_die_type (die, type, cu);
11706 }
11707
11708 type = alloc_type (objfile);
11709
11710 TYPE_CODE (type) = TYPE_CODE_ENUM;
11711 name = dwarf2_full_name (NULL, die, cu);
11712 if (name != NULL)
11713 TYPE_TAG_NAME (type) = name;
11714
11715 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11716 if (attr)
11717 {
11718 TYPE_LENGTH (type) = DW_UNSND (attr);
11719 }
11720 else
11721 {
11722 TYPE_LENGTH (type) = 0;
11723 }
11724
11725 /* The enumeration DIE can be incomplete. In Ada, any type can be
11726 declared as private in the package spec, and then defined only
11727 inside the package body. Such types are known as Taft Amendment
11728 Types. When another package uses such a type, an incomplete DIE
11729 may be generated by the compiler. */
11730 if (die_is_declaration (die, cu))
11731 TYPE_STUB (type) = 1;
11732
11733 return set_die_type (die, type, cu);
11734 }
11735
11736 /* Given a pointer to a die which begins an enumeration, process all
11737 the dies that define the members of the enumeration, and create the
11738 symbol for the enumeration type.
11739
11740 NOTE: We reverse the order of the element list. */
11741
11742 static void
11743 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
11744 {
11745 struct type *this_type;
11746
11747 this_type = get_die_type (die, cu);
11748 if (this_type == NULL)
11749 this_type = read_enumeration_type (die, cu);
11750
11751 if (die->child != NULL)
11752 {
11753 struct die_info *child_die;
11754 struct symbol *sym;
11755 struct field *fields = NULL;
11756 int num_fields = 0;
11757 int unsigned_enum = 1;
11758 const char *name;
11759 int flag_enum = 1;
11760 ULONGEST mask = 0;
11761
11762 child_die = die->child;
11763 while (child_die && child_die->tag)
11764 {
11765 if (child_die->tag != DW_TAG_enumerator)
11766 {
11767 process_die (child_die, cu);
11768 }
11769 else
11770 {
11771 name = dwarf2_name (child_die, cu);
11772 if (name)
11773 {
11774 sym = new_symbol (child_die, this_type, cu);
11775 if (SYMBOL_VALUE (sym) < 0)
11776 {
11777 unsigned_enum = 0;
11778 flag_enum = 0;
11779 }
11780 else if ((mask & SYMBOL_VALUE (sym)) != 0)
11781 flag_enum = 0;
11782 else
11783 mask |= SYMBOL_VALUE (sym);
11784
11785 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
11786 {
11787 fields = (struct field *)
11788 xrealloc (fields,
11789 (num_fields + DW_FIELD_ALLOC_CHUNK)
11790 * sizeof (struct field));
11791 }
11792
11793 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
11794 FIELD_TYPE (fields[num_fields]) = NULL;
11795 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
11796 FIELD_BITSIZE (fields[num_fields]) = 0;
11797
11798 num_fields++;
11799 }
11800 }
11801
11802 child_die = sibling_die (child_die);
11803 }
11804
11805 if (num_fields)
11806 {
11807 TYPE_NFIELDS (this_type) = num_fields;
11808 TYPE_FIELDS (this_type) = (struct field *)
11809 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
11810 memcpy (TYPE_FIELDS (this_type), fields,
11811 sizeof (struct field) * num_fields);
11812 xfree (fields);
11813 }
11814 if (unsigned_enum)
11815 TYPE_UNSIGNED (this_type) = 1;
11816 if (flag_enum)
11817 TYPE_FLAG_ENUM (this_type) = 1;
11818 }
11819
11820 /* If we are reading an enum from a .debug_types unit, and the enum
11821 is a declaration, and the enum is not the signatured type in the
11822 unit, then we do not want to add a symbol for it. Adding a
11823 symbol would in some cases obscure the true definition of the
11824 enum, giving users an incomplete type when the definition is
11825 actually available. Note that we do not want to do this for all
11826 enums which are just declarations, because C++0x allows forward
11827 enum declarations. */
11828 if (cu->per_cu->is_debug_types
11829 && die_is_declaration (die, cu))
11830 {
11831 struct signatured_type *sig_type;
11832
11833 sig_type = (struct signatured_type *) cu->per_cu;
11834 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
11835 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
11836 return;
11837 }
11838
11839 new_symbol (die, this_type, cu);
11840 }
11841
11842 /* Extract all information from a DW_TAG_array_type DIE and put it in
11843 the DIE's type field. For now, this only handles one dimensional
11844 arrays. */
11845
11846 static struct type *
11847 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
11848 {
11849 struct objfile *objfile = cu->objfile;
11850 struct die_info *child_die;
11851 struct type *type;
11852 struct type *element_type, *range_type, *index_type;
11853 struct type **range_types = NULL;
11854 struct attribute *attr;
11855 int ndim = 0;
11856 struct cleanup *back_to;
11857 const char *name;
11858
11859 element_type = die_type (die, cu);
11860
11861 /* The die_type call above may have already set the type for this DIE. */
11862 type = get_die_type (die, cu);
11863 if (type)
11864 return type;
11865
11866 /* Irix 6.2 native cc creates array types without children for
11867 arrays with unspecified length. */
11868 if (die->child == NULL)
11869 {
11870 index_type = objfile_type (objfile)->builtin_int;
11871 range_type = create_range_type (NULL, index_type, 0, -1);
11872 type = create_array_type (NULL, element_type, range_type);
11873 return set_die_type (die, type, cu);
11874 }
11875
11876 back_to = make_cleanup (null_cleanup, NULL);
11877 child_die = die->child;
11878 while (child_die && child_die->tag)
11879 {
11880 if (child_die->tag == DW_TAG_subrange_type)
11881 {
11882 struct type *child_type = read_type_die (child_die, cu);
11883
11884 if (child_type != NULL)
11885 {
11886 /* The range type was succesfully read. Save it for the
11887 array type creation. */
11888 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
11889 {
11890 range_types = (struct type **)
11891 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
11892 * sizeof (struct type *));
11893 if (ndim == 0)
11894 make_cleanup (free_current_contents, &range_types);
11895 }
11896 range_types[ndim++] = child_type;
11897 }
11898 }
11899 child_die = sibling_die (child_die);
11900 }
11901
11902 /* Dwarf2 dimensions are output from left to right, create the
11903 necessary array types in backwards order. */
11904
11905 type = element_type;
11906
11907 if (read_array_order (die, cu) == DW_ORD_col_major)
11908 {
11909 int i = 0;
11910
11911 while (i < ndim)
11912 type = create_array_type (NULL, type, range_types[i++]);
11913 }
11914 else
11915 {
11916 while (ndim-- > 0)
11917 type = create_array_type (NULL, type, range_types[ndim]);
11918 }
11919
11920 /* Understand Dwarf2 support for vector types (like they occur on
11921 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
11922 array type. This is not part of the Dwarf2/3 standard yet, but a
11923 custom vendor extension. The main difference between a regular
11924 array and the vector variant is that vectors are passed by value
11925 to functions. */
11926 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
11927 if (attr)
11928 make_vector_type (type);
11929
11930 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
11931 implementation may choose to implement triple vectors using this
11932 attribute. */
11933 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11934 if (attr)
11935 {
11936 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
11937 TYPE_LENGTH (type) = DW_UNSND (attr);
11938 else
11939 complaint (&symfile_complaints,
11940 _("DW_AT_byte_size for array type smaller "
11941 "than the total size of elements"));
11942 }
11943
11944 name = dwarf2_name (die, cu);
11945 if (name)
11946 TYPE_NAME (type) = name;
11947
11948 /* Install the type in the die. */
11949 set_die_type (die, type, cu);
11950
11951 /* set_die_type should be already done. */
11952 set_descriptive_type (type, die, cu);
11953
11954 do_cleanups (back_to);
11955
11956 return type;
11957 }
11958
11959 static enum dwarf_array_dim_ordering
11960 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
11961 {
11962 struct attribute *attr;
11963
11964 attr = dwarf2_attr (die, DW_AT_ordering, cu);
11965
11966 if (attr) return DW_SND (attr);
11967
11968 /* GNU F77 is a special case, as at 08/2004 array type info is the
11969 opposite order to the dwarf2 specification, but data is still
11970 laid out as per normal fortran.
11971
11972 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
11973 version checking. */
11974
11975 if (cu->language == language_fortran
11976 && cu->producer && strstr (cu->producer, "GNU F77"))
11977 {
11978 return DW_ORD_row_major;
11979 }
11980
11981 switch (cu->language_defn->la_array_ordering)
11982 {
11983 case array_column_major:
11984 return DW_ORD_col_major;
11985 case array_row_major:
11986 default:
11987 return DW_ORD_row_major;
11988 };
11989 }
11990
11991 /* Extract all information from a DW_TAG_set_type DIE and put it in
11992 the DIE's type field. */
11993
11994 static struct type *
11995 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
11996 {
11997 struct type *domain_type, *set_type;
11998 struct attribute *attr;
11999
12000 domain_type = die_type (die, cu);
12001
12002 /* The die_type call above may have already set the type for this DIE. */
12003 set_type = get_die_type (die, cu);
12004 if (set_type)
12005 return set_type;
12006
12007 set_type = create_set_type (NULL, domain_type);
12008
12009 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12010 if (attr)
12011 TYPE_LENGTH (set_type) = DW_UNSND (attr);
12012
12013 return set_die_type (die, set_type, cu);
12014 }
12015
12016 /* A helper for read_common_block that creates a locexpr baton.
12017 SYM is the symbol which we are marking as computed.
12018 COMMON_DIE is the DIE for the common block.
12019 COMMON_LOC is the location expression attribute for the common
12020 block itself.
12021 MEMBER_LOC is the location expression attribute for the particular
12022 member of the common block that we are processing.
12023 CU is the CU from which the above come. */
12024
12025 static void
12026 mark_common_block_symbol_computed (struct symbol *sym,
12027 struct die_info *common_die,
12028 struct attribute *common_loc,
12029 struct attribute *member_loc,
12030 struct dwarf2_cu *cu)
12031 {
12032 struct objfile *objfile = dwarf2_per_objfile->objfile;
12033 struct dwarf2_locexpr_baton *baton;
12034 gdb_byte *ptr;
12035 unsigned int cu_off;
12036 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
12037 LONGEST offset = 0;
12038
12039 gdb_assert (common_loc && member_loc);
12040 gdb_assert (attr_form_is_block (common_loc));
12041 gdb_assert (attr_form_is_block (member_loc)
12042 || attr_form_is_constant (member_loc));
12043
12044 baton = obstack_alloc (&objfile->objfile_obstack,
12045 sizeof (struct dwarf2_locexpr_baton));
12046 baton->per_cu = cu->per_cu;
12047 gdb_assert (baton->per_cu);
12048
12049 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
12050
12051 if (attr_form_is_constant (member_loc))
12052 {
12053 offset = dwarf2_get_attr_constant_value (member_loc, 0);
12054 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
12055 }
12056 else
12057 baton->size += DW_BLOCK (member_loc)->size;
12058
12059 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
12060 baton->data = ptr;
12061
12062 *ptr++ = DW_OP_call4;
12063 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
12064 store_unsigned_integer (ptr, 4, byte_order, cu_off);
12065 ptr += 4;
12066
12067 if (attr_form_is_constant (member_loc))
12068 {
12069 *ptr++ = DW_OP_addr;
12070 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
12071 ptr += cu->header.addr_size;
12072 }
12073 else
12074 {
12075 /* We have to copy the data here, because DW_OP_call4 will only
12076 use a DW_AT_location attribute. */
12077 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
12078 ptr += DW_BLOCK (member_loc)->size;
12079 }
12080
12081 *ptr++ = DW_OP_plus;
12082 gdb_assert (ptr - baton->data == baton->size);
12083
12084 SYMBOL_LOCATION_BATON (sym) = baton;
12085 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
12086 }
12087
12088 /* Create appropriate locally-scoped variables for all the
12089 DW_TAG_common_block entries. Also create a struct common_block
12090 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
12091 is used to sepate the common blocks name namespace from regular
12092 variable names. */
12093
12094 static void
12095 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
12096 {
12097 struct attribute *attr;
12098
12099 attr = dwarf2_attr (die, DW_AT_location, cu);
12100 if (attr)
12101 {
12102 /* Support the .debug_loc offsets. */
12103 if (attr_form_is_block (attr))
12104 {
12105 /* Ok. */
12106 }
12107 else if (attr_form_is_section_offset (attr))
12108 {
12109 dwarf2_complex_location_expr_complaint ();
12110 attr = NULL;
12111 }
12112 else
12113 {
12114 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
12115 "common block member");
12116 attr = NULL;
12117 }
12118 }
12119
12120 if (die->child != NULL)
12121 {
12122 struct objfile *objfile = cu->objfile;
12123 struct die_info *child_die;
12124 size_t n_entries = 0, size;
12125 struct common_block *common_block;
12126 struct symbol *sym;
12127
12128 for (child_die = die->child;
12129 child_die && child_die->tag;
12130 child_die = sibling_die (child_die))
12131 ++n_entries;
12132
12133 size = (sizeof (struct common_block)
12134 + (n_entries - 1) * sizeof (struct symbol *));
12135 common_block = obstack_alloc (&objfile->objfile_obstack, size);
12136 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
12137 common_block->n_entries = 0;
12138
12139 for (child_die = die->child;
12140 child_die && child_die->tag;
12141 child_die = sibling_die (child_die))
12142 {
12143 /* Create the symbol in the DW_TAG_common_block block in the current
12144 symbol scope. */
12145 sym = new_symbol (child_die, NULL, cu);
12146 if (sym != NULL)
12147 {
12148 struct attribute *member_loc;
12149
12150 common_block->contents[common_block->n_entries++] = sym;
12151
12152 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
12153 cu);
12154 if (member_loc)
12155 {
12156 /* GDB has handled this for a long time, but it is
12157 not specified by DWARF. It seems to have been
12158 emitted by gfortran at least as recently as:
12159 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
12160 complaint (&symfile_complaints,
12161 _("Variable in common block has "
12162 "DW_AT_data_member_location "
12163 "- DIE at 0x%x [in module %s]"),
12164 child_die->offset.sect_off, cu->objfile->name);
12165
12166 if (attr_form_is_section_offset (member_loc))
12167 dwarf2_complex_location_expr_complaint ();
12168 else if (attr_form_is_constant (member_loc)
12169 || attr_form_is_block (member_loc))
12170 {
12171 if (attr)
12172 mark_common_block_symbol_computed (sym, die, attr,
12173 member_loc, cu);
12174 }
12175 else
12176 dwarf2_complex_location_expr_complaint ();
12177 }
12178 }
12179 }
12180
12181 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
12182 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
12183 }
12184 }
12185
12186 /* Create a type for a C++ namespace. */
12187
12188 static struct type *
12189 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
12190 {
12191 struct objfile *objfile = cu->objfile;
12192 const char *previous_prefix, *name;
12193 int is_anonymous;
12194 struct type *type;
12195
12196 /* For extensions, reuse the type of the original namespace. */
12197 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
12198 {
12199 struct die_info *ext_die;
12200 struct dwarf2_cu *ext_cu = cu;
12201
12202 ext_die = dwarf2_extension (die, &ext_cu);
12203 type = read_type_die (ext_die, ext_cu);
12204
12205 /* EXT_CU may not be the same as CU.
12206 Ensure TYPE is recorded in CU's type_hash table. */
12207 return set_die_type (die, type, cu);
12208 }
12209
12210 name = namespace_name (die, &is_anonymous, cu);
12211
12212 /* Now build the name of the current namespace. */
12213
12214 previous_prefix = determine_prefix (die, cu);
12215 if (previous_prefix[0] != '\0')
12216 name = typename_concat (&objfile->objfile_obstack,
12217 previous_prefix, name, 0, cu);
12218
12219 /* Create the type. */
12220 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
12221 objfile);
12222 TYPE_NAME (type) = name;
12223 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12224
12225 return set_die_type (die, type, cu);
12226 }
12227
12228 /* Read a C++ namespace. */
12229
12230 static void
12231 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
12232 {
12233 struct objfile *objfile = cu->objfile;
12234 int is_anonymous;
12235
12236 /* Add a symbol associated to this if we haven't seen the namespace
12237 before. Also, add a using directive if it's an anonymous
12238 namespace. */
12239
12240 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
12241 {
12242 struct type *type;
12243
12244 type = read_type_die (die, cu);
12245 new_symbol (die, type, cu);
12246
12247 namespace_name (die, &is_anonymous, cu);
12248 if (is_anonymous)
12249 {
12250 const char *previous_prefix = determine_prefix (die, cu);
12251
12252 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
12253 NULL, NULL, 0, &objfile->objfile_obstack);
12254 }
12255 }
12256
12257 if (die->child != NULL)
12258 {
12259 struct die_info *child_die = die->child;
12260
12261 while (child_die && child_die->tag)
12262 {
12263 process_die (child_die, cu);
12264 child_die = sibling_die (child_die);
12265 }
12266 }
12267 }
12268
12269 /* Read a Fortran module as type. This DIE can be only a declaration used for
12270 imported module. Still we need that type as local Fortran "use ... only"
12271 declaration imports depend on the created type in determine_prefix. */
12272
12273 static struct type *
12274 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
12275 {
12276 struct objfile *objfile = cu->objfile;
12277 const char *module_name;
12278 struct type *type;
12279
12280 module_name = dwarf2_name (die, cu);
12281 if (!module_name)
12282 complaint (&symfile_complaints,
12283 _("DW_TAG_module has no name, offset 0x%x"),
12284 die->offset.sect_off);
12285 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
12286
12287 /* determine_prefix uses TYPE_TAG_NAME. */
12288 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12289
12290 return set_die_type (die, type, cu);
12291 }
12292
12293 /* Read a Fortran module. */
12294
12295 static void
12296 read_module (struct die_info *die, struct dwarf2_cu *cu)
12297 {
12298 struct die_info *child_die = die->child;
12299
12300 while (child_die && child_die->tag)
12301 {
12302 process_die (child_die, cu);
12303 child_die = sibling_die (child_die);
12304 }
12305 }
12306
12307 /* Return the name of the namespace represented by DIE. Set
12308 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
12309 namespace. */
12310
12311 static const char *
12312 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
12313 {
12314 struct die_info *current_die;
12315 const char *name = NULL;
12316
12317 /* Loop through the extensions until we find a name. */
12318
12319 for (current_die = die;
12320 current_die != NULL;
12321 current_die = dwarf2_extension (die, &cu))
12322 {
12323 name = dwarf2_name (current_die, cu);
12324 if (name != NULL)
12325 break;
12326 }
12327
12328 /* Is it an anonymous namespace? */
12329
12330 *is_anonymous = (name == NULL);
12331 if (*is_anonymous)
12332 name = CP_ANONYMOUS_NAMESPACE_STR;
12333
12334 return name;
12335 }
12336
12337 /* Extract all information from a DW_TAG_pointer_type DIE and add to
12338 the user defined type vector. */
12339
12340 static struct type *
12341 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
12342 {
12343 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
12344 struct comp_unit_head *cu_header = &cu->header;
12345 struct type *type;
12346 struct attribute *attr_byte_size;
12347 struct attribute *attr_address_class;
12348 int byte_size, addr_class;
12349 struct type *target_type;
12350
12351 target_type = die_type (die, cu);
12352
12353 /* The die_type call above may have already set the type for this DIE. */
12354 type = get_die_type (die, cu);
12355 if (type)
12356 return type;
12357
12358 type = lookup_pointer_type (target_type);
12359
12360 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
12361 if (attr_byte_size)
12362 byte_size = DW_UNSND (attr_byte_size);
12363 else
12364 byte_size = cu_header->addr_size;
12365
12366 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
12367 if (attr_address_class)
12368 addr_class = DW_UNSND (attr_address_class);
12369 else
12370 addr_class = DW_ADDR_none;
12371
12372 /* If the pointer size or address class is different than the
12373 default, create a type variant marked as such and set the
12374 length accordingly. */
12375 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
12376 {
12377 if (gdbarch_address_class_type_flags_p (gdbarch))
12378 {
12379 int type_flags;
12380
12381 type_flags = gdbarch_address_class_type_flags
12382 (gdbarch, byte_size, addr_class);
12383 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
12384 == 0);
12385 type = make_type_with_address_space (type, type_flags);
12386 }
12387 else if (TYPE_LENGTH (type) != byte_size)
12388 {
12389 complaint (&symfile_complaints,
12390 _("invalid pointer size %d"), byte_size);
12391 }
12392 else
12393 {
12394 /* Should we also complain about unhandled address classes? */
12395 }
12396 }
12397
12398 TYPE_LENGTH (type) = byte_size;
12399 return set_die_type (die, type, cu);
12400 }
12401
12402 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
12403 the user defined type vector. */
12404
12405 static struct type *
12406 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
12407 {
12408 struct type *type;
12409 struct type *to_type;
12410 struct type *domain;
12411
12412 to_type = die_type (die, cu);
12413 domain = die_containing_type (die, cu);
12414
12415 /* The calls above may have already set the type for this DIE. */
12416 type = get_die_type (die, cu);
12417 if (type)
12418 return type;
12419
12420 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
12421 type = lookup_methodptr_type (to_type);
12422 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
12423 {
12424 struct type *new_type = alloc_type (cu->objfile);
12425
12426 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
12427 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
12428 TYPE_VARARGS (to_type));
12429 type = lookup_methodptr_type (new_type);
12430 }
12431 else
12432 type = lookup_memberptr_type (to_type, domain);
12433
12434 return set_die_type (die, type, cu);
12435 }
12436
12437 /* Extract all information from a DW_TAG_reference_type DIE and add to
12438 the user defined type vector. */
12439
12440 static struct type *
12441 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
12442 {
12443 struct comp_unit_head *cu_header = &cu->header;
12444 struct type *type, *target_type;
12445 struct attribute *attr;
12446
12447 target_type = die_type (die, cu);
12448
12449 /* The die_type call above may have already set the type for this DIE. */
12450 type = get_die_type (die, cu);
12451 if (type)
12452 return type;
12453
12454 type = lookup_reference_type (target_type);
12455 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12456 if (attr)
12457 {
12458 TYPE_LENGTH (type) = DW_UNSND (attr);
12459 }
12460 else
12461 {
12462 TYPE_LENGTH (type) = cu_header->addr_size;
12463 }
12464 return set_die_type (die, type, cu);
12465 }
12466
12467 static struct type *
12468 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
12469 {
12470 struct type *base_type, *cv_type;
12471
12472 base_type = die_type (die, cu);
12473
12474 /* The die_type call above may have already set the type for this DIE. */
12475 cv_type = get_die_type (die, cu);
12476 if (cv_type)
12477 return cv_type;
12478
12479 /* In case the const qualifier is applied to an array type, the element type
12480 is so qualified, not the array type (section 6.7.3 of C99). */
12481 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
12482 {
12483 struct type *el_type, *inner_array;
12484
12485 base_type = copy_type (base_type);
12486 inner_array = base_type;
12487
12488 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
12489 {
12490 TYPE_TARGET_TYPE (inner_array) =
12491 copy_type (TYPE_TARGET_TYPE (inner_array));
12492 inner_array = TYPE_TARGET_TYPE (inner_array);
12493 }
12494
12495 el_type = TYPE_TARGET_TYPE (inner_array);
12496 TYPE_TARGET_TYPE (inner_array) =
12497 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
12498
12499 return set_die_type (die, base_type, cu);
12500 }
12501
12502 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
12503 return set_die_type (die, cv_type, cu);
12504 }
12505
12506 static struct type *
12507 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
12508 {
12509 struct type *base_type, *cv_type;
12510
12511 base_type = die_type (die, cu);
12512
12513 /* The die_type call above may have already set the type for this DIE. */
12514 cv_type = get_die_type (die, cu);
12515 if (cv_type)
12516 return cv_type;
12517
12518 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
12519 return set_die_type (die, cv_type, cu);
12520 }
12521
12522 /* Handle DW_TAG_restrict_type. */
12523
12524 static struct type *
12525 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
12526 {
12527 struct type *base_type, *cv_type;
12528
12529 base_type = die_type (die, cu);
12530
12531 /* The die_type call above may have already set the type for this DIE. */
12532 cv_type = get_die_type (die, cu);
12533 if (cv_type)
12534 return cv_type;
12535
12536 cv_type = make_restrict_type (base_type);
12537 return set_die_type (die, cv_type, cu);
12538 }
12539
12540 /* Extract all information from a DW_TAG_string_type DIE and add to
12541 the user defined type vector. It isn't really a user defined type,
12542 but it behaves like one, with other DIE's using an AT_user_def_type
12543 attribute to reference it. */
12544
12545 static struct type *
12546 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
12547 {
12548 struct objfile *objfile = cu->objfile;
12549 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12550 struct type *type, *range_type, *index_type, *char_type;
12551 struct attribute *attr;
12552 unsigned int length;
12553
12554 attr = dwarf2_attr (die, DW_AT_string_length, cu);
12555 if (attr)
12556 {
12557 length = DW_UNSND (attr);
12558 }
12559 else
12560 {
12561 /* Check for the DW_AT_byte_size attribute. */
12562 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12563 if (attr)
12564 {
12565 length = DW_UNSND (attr);
12566 }
12567 else
12568 {
12569 length = 1;
12570 }
12571 }
12572
12573 index_type = objfile_type (objfile)->builtin_int;
12574 range_type = create_range_type (NULL, index_type, 1, length);
12575 char_type = language_string_char_type (cu->language_defn, gdbarch);
12576 type = create_string_type (NULL, char_type, range_type);
12577
12578 return set_die_type (die, type, cu);
12579 }
12580
12581 /* Handle DIES due to C code like:
12582
12583 struct foo
12584 {
12585 int (*funcp)(int a, long l);
12586 int b;
12587 };
12588
12589 ('funcp' generates a DW_TAG_subroutine_type DIE). */
12590
12591 static struct type *
12592 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
12593 {
12594 struct objfile *objfile = cu->objfile;
12595 struct type *type; /* Type that this function returns. */
12596 struct type *ftype; /* Function that returns above type. */
12597 struct attribute *attr;
12598
12599 type = die_type (die, cu);
12600
12601 /* The die_type call above may have already set the type for this DIE. */
12602 ftype = get_die_type (die, cu);
12603 if (ftype)
12604 return ftype;
12605
12606 ftype = lookup_function_type (type);
12607
12608 /* All functions in C++, Pascal and Java have prototypes. */
12609 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
12610 if ((attr && (DW_UNSND (attr) != 0))
12611 || cu->language == language_cplus
12612 || cu->language == language_java
12613 || cu->language == language_pascal)
12614 TYPE_PROTOTYPED (ftype) = 1;
12615 else if (producer_is_realview (cu->producer))
12616 /* RealView does not emit DW_AT_prototyped. We can not
12617 distinguish prototyped and unprototyped functions; default to
12618 prototyped, since that is more common in modern code (and
12619 RealView warns about unprototyped functions). */
12620 TYPE_PROTOTYPED (ftype) = 1;
12621
12622 /* Store the calling convention in the type if it's available in
12623 the subroutine die. Otherwise set the calling convention to
12624 the default value DW_CC_normal. */
12625 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
12626 if (attr)
12627 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
12628 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
12629 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
12630 else
12631 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
12632
12633 /* We need to add the subroutine type to the die immediately so
12634 we don't infinitely recurse when dealing with parameters
12635 declared as the same subroutine type. */
12636 set_die_type (die, ftype, cu);
12637
12638 if (die->child != NULL)
12639 {
12640 struct type *void_type = objfile_type (objfile)->builtin_void;
12641 struct die_info *child_die;
12642 int nparams, iparams;
12643
12644 /* Count the number of parameters.
12645 FIXME: GDB currently ignores vararg functions, but knows about
12646 vararg member functions. */
12647 nparams = 0;
12648 child_die = die->child;
12649 while (child_die && child_die->tag)
12650 {
12651 if (child_die->tag == DW_TAG_formal_parameter)
12652 nparams++;
12653 else if (child_die->tag == DW_TAG_unspecified_parameters)
12654 TYPE_VARARGS (ftype) = 1;
12655 child_die = sibling_die (child_die);
12656 }
12657
12658 /* Allocate storage for parameters and fill them in. */
12659 TYPE_NFIELDS (ftype) = nparams;
12660 TYPE_FIELDS (ftype) = (struct field *)
12661 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
12662
12663 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
12664 even if we error out during the parameters reading below. */
12665 for (iparams = 0; iparams < nparams; iparams++)
12666 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
12667
12668 iparams = 0;
12669 child_die = die->child;
12670 while (child_die && child_die->tag)
12671 {
12672 if (child_die->tag == DW_TAG_formal_parameter)
12673 {
12674 struct type *arg_type;
12675
12676 /* DWARF version 2 has no clean way to discern C++
12677 static and non-static member functions. G++ helps
12678 GDB by marking the first parameter for non-static
12679 member functions (which is the this pointer) as
12680 artificial. We pass this information to
12681 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
12682
12683 DWARF version 3 added DW_AT_object_pointer, which GCC
12684 4.5 does not yet generate. */
12685 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
12686 if (attr)
12687 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
12688 else
12689 {
12690 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
12691
12692 /* GCC/43521: In java, the formal parameter
12693 "this" is sometimes not marked with DW_AT_artificial. */
12694 if (cu->language == language_java)
12695 {
12696 const char *name = dwarf2_name (child_die, cu);
12697
12698 if (name && !strcmp (name, "this"))
12699 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
12700 }
12701 }
12702 arg_type = die_type (child_die, cu);
12703
12704 /* RealView does not mark THIS as const, which the testsuite
12705 expects. GCC marks THIS as const in method definitions,
12706 but not in the class specifications (GCC PR 43053). */
12707 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
12708 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
12709 {
12710 int is_this = 0;
12711 struct dwarf2_cu *arg_cu = cu;
12712 const char *name = dwarf2_name (child_die, cu);
12713
12714 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
12715 if (attr)
12716 {
12717 /* If the compiler emits this, use it. */
12718 if (follow_die_ref (die, attr, &arg_cu) == child_die)
12719 is_this = 1;
12720 }
12721 else if (name && strcmp (name, "this") == 0)
12722 /* Function definitions will have the argument names. */
12723 is_this = 1;
12724 else if (name == NULL && iparams == 0)
12725 /* Declarations may not have the names, so like
12726 elsewhere in GDB, assume an artificial first
12727 argument is "this". */
12728 is_this = 1;
12729
12730 if (is_this)
12731 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
12732 arg_type, 0);
12733 }
12734
12735 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
12736 iparams++;
12737 }
12738 child_die = sibling_die (child_die);
12739 }
12740 }
12741
12742 return ftype;
12743 }
12744
12745 static struct type *
12746 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
12747 {
12748 struct objfile *objfile = cu->objfile;
12749 const char *name = NULL;
12750 struct type *this_type, *target_type;
12751
12752 name = dwarf2_full_name (NULL, die, cu);
12753 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
12754 TYPE_FLAG_TARGET_STUB, NULL, objfile);
12755 TYPE_NAME (this_type) = name;
12756 set_die_type (die, this_type, cu);
12757 target_type = die_type (die, cu);
12758 if (target_type != this_type)
12759 TYPE_TARGET_TYPE (this_type) = target_type;
12760 else
12761 {
12762 /* Self-referential typedefs are, it seems, not allowed by the DWARF
12763 spec and cause infinite loops in GDB. */
12764 complaint (&symfile_complaints,
12765 _("Self-referential DW_TAG_typedef "
12766 "- DIE at 0x%x [in module %s]"),
12767 die->offset.sect_off, objfile->name);
12768 TYPE_TARGET_TYPE (this_type) = NULL;
12769 }
12770 return this_type;
12771 }
12772
12773 /* Find a representation of a given base type and install
12774 it in the TYPE field of the die. */
12775
12776 static struct type *
12777 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
12778 {
12779 struct objfile *objfile = cu->objfile;
12780 struct type *type;
12781 struct attribute *attr;
12782 int encoding = 0, size = 0;
12783 const char *name;
12784 enum type_code code = TYPE_CODE_INT;
12785 int type_flags = 0;
12786 struct type *target_type = NULL;
12787
12788 attr = dwarf2_attr (die, DW_AT_encoding, cu);
12789 if (attr)
12790 {
12791 encoding = DW_UNSND (attr);
12792 }
12793 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12794 if (attr)
12795 {
12796 size = DW_UNSND (attr);
12797 }
12798 name = dwarf2_name (die, cu);
12799 if (!name)
12800 {
12801 complaint (&symfile_complaints,
12802 _("DW_AT_name missing from DW_TAG_base_type"));
12803 }
12804
12805 switch (encoding)
12806 {
12807 case DW_ATE_address:
12808 /* Turn DW_ATE_address into a void * pointer. */
12809 code = TYPE_CODE_PTR;
12810 type_flags |= TYPE_FLAG_UNSIGNED;
12811 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
12812 break;
12813 case DW_ATE_boolean:
12814 code = TYPE_CODE_BOOL;
12815 type_flags |= TYPE_FLAG_UNSIGNED;
12816 break;
12817 case DW_ATE_complex_float:
12818 code = TYPE_CODE_COMPLEX;
12819 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
12820 break;
12821 case DW_ATE_decimal_float:
12822 code = TYPE_CODE_DECFLOAT;
12823 break;
12824 case DW_ATE_float:
12825 code = TYPE_CODE_FLT;
12826 break;
12827 case DW_ATE_signed:
12828 break;
12829 case DW_ATE_unsigned:
12830 type_flags |= TYPE_FLAG_UNSIGNED;
12831 if (cu->language == language_fortran
12832 && name
12833 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
12834 code = TYPE_CODE_CHAR;
12835 break;
12836 case DW_ATE_signed_char:
12837 if (cu->language == language_ada || cu->language == language_m2
12838 || cu->language == language_pascal
12839 || cu->language == language_fortran)
12840 code = TYPE_CODE_CHAR;
12841 break;
12842 case DW_ATE_unsigned_char:
12843 if (cu->language == language_ada || cu->language == language_m2
12844 || cu->language == language_pascal
12845 || cu->language == language_fortran)
12846 code = TYPE_CODE_CHAR;
12847 type_flags |= TYPE_FLAG_UNSIGNED;
12848 break;
12849 case DW_ATE_UTF:
12850 /* We just treat this as an integer and then recognize the
12851 type by name elsewhere. */
12852 break;
12853
12854 default:
12855 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
12856 dwarf_type_encoding_name (encoding));
12857 break;
12858 }
12859
12860 type = init_type (code, size, type_flags, NULL, objfile);
12861 TYPE_NAME (type) = name;
12862 TYPE_TARGET_TYPE (type) = target_type;
12863
12864 if (name && strcmp (name, "char") == 0)
12865 TYPE_NOSIGN (type) = 1;
12866
12867 return set_die_type (die, type, cu);
12868 }
12869
12870 /* Read the given DW_AT_subrange DIE. */
12871
12872 static struct type *
12873 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
12874 {
12875 struct type *base_type, *orig_base_type;
12876 struct type *range_type;
12877 struct attribute *attr;
12878 LONGEST low, high;
12879 int low_default_is_valid;
12880 const char *name;
12881 LONGEST negative_mask;
12882
12883 orig_base_type = die_type (die, cu);
12884 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
12885 whereas the real type might be. So, we use ORIG_BASE_TYPE when
12886 creating the range type, but we use the result of check_typedef
12887 when examining properties of the type. */
12888 base_type = check_typedef (orig_base_type);
12889
12890 /* The die_type call above may have already set the type for this DIE. */
12891 range_type = get_die_type (die, cu);
12892 if (range_type)
12893 return range_type;
12894
12895 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
12896 omitting DW_AT_lower_bound. */
12897 switch (cu->language)
12898 {
12899 case language_c:
12900 case language_cplus:
12901 low = 0;
12902 low_default_is_valid = 1;
12903 break;
12904 case language_fortran:
12905 low = 1;
12906 low_default_is_valid = 1;
12907 break;
12908 case language_d:
12909 case language_java:
12910 case language_objc:
12911 low = 0;
12912 low_default_is_valid = (cu->header.version >= 4);
12913 break;
12914 case language_ada:
12915 case language_m2:
12916 case language_pascal:
12917 low = 1;
12918 low_default_is_valid = (cu->header.version >= 4);
12919 break;
12920 default:
12921 low = 0;
12922 low_default_is_valid = 0;
12923 break;
12924 }
12925
12926 /* FIXME: For variable sized arrays either of these could be
12927 a variable rather than a constant value. We'll allow it,
12928 but we don't know how to handle it. */
12929 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
12930 if (attr)
12931 low = dwarf2_get_attr_constant_value (attr, low);
12932 else if (!low_default_is_valid)
12933 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
12934 "- DIE at 0x%x [in module %s]"),
12935 die->offset.sect_off, cu->objfile->name);
12936
12937 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
12938 if (attr)
12939 {
12940 if (attr_form_is_block (attr) || is_ref_attr (attr))
12941 {
12942 /* GCC encodes arrays with unspecified or dynamic length
12943 with a DW_FORM_block1 attribute or a reference attribute.
12944 FIXME: GDB does not yet know how to handle dynamic
12945 arrays properly, treat them as arrays with unspecified
12946 length for now.
12947
12948 FIXME: jimb/2003-09-22: GDB does not really know
12949 how to handle arrays of unspecified length
12950 either; we just represent them as zero-length
12951 arrays. Choose an appropriate upper bound given
12952 the lower bound we've computed above. */
12953 high = low - 1;
12954 }
12955 else
12956 high = dwarf2_get_attr_constant_value (attr, 1);
12957 }
12958 else
12959 {
12960 attr = dwarf2_attr (die, DW_AT_count, cu);
12961 if (attr)
12962 {
12963 int count = dwarf2_get_attr_constant_value (attr, 1);
12964 high = low + count - 1;
12965 }
12966 else
12967 {
12968 /* Unspecified array length. */
12969 high = low - 1;
12970 }
12971 }
12972
12973 /* Dwarf-2 specifications explicitly allows to create subrange types
12974 without specifying a base type.
12975 In that case, the base type must be set to the type of
12976 the lower bound, upper bound or count, in that order, if any of these
12977 three attributes references an object that has a type.
12978 If no base type is found, the Dwarf-2 specifications say that
12979 a signed integer type of size equal to the size of an address should
12980 be used.
12981 For the following C code: `extern char gdb_int [];'
12982 GCC produces an empty range DIE.
12983 FIXME: muller/2010-05-28: Possible references to object for low bound,
12984 high bound or count are not yet handled by this code. */
12985 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
12986 {
12987 struct objfile *objfile = cu->objfile;
12988 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12989 int addr_size = gdbarch_addr_bit (gdbarch) /8;
12990 struct type *int_type = objfile_type (objfile)->builtin_int;
12991
12992 /* Test "int", "long int", and "long long int" objfile types,
12993 and select the first one having a size above or equal to the
12994 architecture address size. */
12995 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12996 base_type = int_type;
12997 else
12998 {
12999 int_type = objfile_type (objfile)->builtin_long;
13000 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13001 base_type = int_type;
13002 else
13003 {
13004 int_type = objfile_type (objfile)->builtin_long_long;
13005 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13006 base_type = int_type;
13007 }
13008 }
13009 }
13010
13011 negative_mask =
13012 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
13013 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
13014 low |= negative_mask;
13015 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
13016 high |= negative_mask;
13017
13018 range_type = create_range_type (NULL, orig_base_type, low, high);
13019
13020 /* Mark arrays with dynamic length at least as an array of unspecified
13021 length. GDB could check the boundary but before it gets implemented at
13022 least allow accessing the array elements. */
13023 if (attr && attr_form_is_block (attr))
13024 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
13025
13026 /* Ada expects an empty array on no boundary attributes. */
13027 if (attr == NULL && cu->language != language_ada)
13028 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
13029
13030 name = dwarf2_name (die, cu);
13031 if (name)
13032 TYPE_NAME (range_type) = name;
13033
13034 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13035 if (attr)
13036 TYPE_LENGTH (range_type) = DW_UNSND (attr);
13037
13038 set_die_type (die, range_type, cu);
13039
13040 /* set_die_type should be already done. */
13041 set_descriptive_type (range_type, die, cu);
13042
13043 return range_type;
13044 }
13045
13046 static struct type *
13047 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
13048 {
13049 struct type *type;
13050
13051 /* For now, we only support the C meaning of an unspecified type: void. */
13052
13053 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
13054 TYPE_NAME (type) = dwarf2_name (die, cu);
13055
13056 return set_die_type (die, type, cu);
13057 }
13058
13059 /* Read a single die and all its descendents. Set the die's sibling
13060 field to NULL; set other fields in the die correctly, and set all
13061 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
13062 location of the info_ptr after reading all of those dies. PARENT
13063 is the parent of the die in question. */
13064
13065 static struct die_info *
13066 read_die_and_children (const struct die_reader_specs *reader,
13067 gdb_byte *info_ptr,
13068 gdb_byte **new_info_ptr,
13069 struct die_info *parent)
13070 {
13071 struct die_info *die;
13072 gdb_byte *cur_ptr;
13073 int has_children;
13074
13075 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
13076 if (die == NULL)
13077 {
13078 *new_info_ptr = cur_ptr;
13079 return NULL;
13080 }
13081 store_in_ref_table (die, reader->cu);
13082
13083 if (has_children)
13084 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
13085 else
13086 {
13087 die->child = NULL;
13088 *new_info_ptr = cur_ptr;
13089 }
13090
13091 die->sibling = NULL;
13092 die->parent = parent;
13093 return die;
13094 }
13095
13096 /* Read a die, all of its descendents, and all of its siblings; set
13097 all of the fields of all of the dies correctly. Arguments are as
13098 in read_die_and_children. */
13099
13100 static struct die_info *
13101 read_die_and_siblings_1 (const struct die_reader_specs *reader,
13102 gdb_byte *info_ptr,
13103 gdb_byte **new_info_ptr,
13104 struct die_info *parent)
13105 {
13106 struct die_info *first_die, *last_sibling;
13107 gdb_byte *cur_ptr;
13108
13109 cur_ptr = info_ptr;
13110 first_die = last_sibling = NULL;
13111
13112 while (1)
13113 {
13114 struct die_info *die
13115 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
13116
13117 if (die == NULL)
13118 {
13119 *new_info_ptr = cur_ptr;
13120 return first_die;
13121 }
13122
13123 if (!first_die)
13124 first_die = die;
13125 else
13126 last_sibling->sibling = die;
13127
13128 last_sibling = die;
13129 }
13130 }
13131
13132 /* Read a die, all of its descendents, and all of its siblings; set
13133 all of the fields of all of the dies correctly. Arguments are as
13134 in read_die_and_children.
13135 This the main entry point for reading a DIE and all its children. */
13136
13137 static struct die_info *
13138 read_die_and_siblings (const struct die_reader_specs *reader,
13139 gdb_byte *info_ptr,
13140 gdb_byte **new_info_ptr,
13141 struct die_info *parent)
13142 {
13143 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
13144 new_info_ptr, parent);
13145
13146 if (dwarf2_die_debug)
13147 {
13148 fprintf_unfiltered (gdb_stdlog,
13149 "Read die from %s@0x%x of %s:\n",
13150 bfd_section_name (reader->abfd,
13151 reader->die_section->asection),
13152 (unsigned) (info_ptr - reader->die_section->buffer),
13153 bfd_get_filename (reader->abfd));
13154 dump_die (die, dwarf2_die_debug);
13155 }
13156
13157 return die;
13158 }
13159
13160 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
13161 attributes.
13162 The caller is responsible for filling in the extra attributes
13163 and updating (*DIEP)->num_attrs.
13164 Set DIEP to point to a newly allocated die with its information,
13165 except for its child, sibling, and parent fields.
13166 Set HAS_CHILDREN to tell whether the die has children or not. */
13167
13168 static gdb_byte *
13169 read_full_die_1 (const struct die_reader_specs *reader,
13170 struct die_info **diep, gdb_byte *info_ptr,
13171 int *has_children, int num_extra_attrs)
13172 {
13173 unsigned int abbrev_number, bytes_read, i;
13174 sect_offset offset;
13175 struct abbrev_info *abbrev;
13176 struct die_info *die;
13177 struct dwarf2_cu *cu = reader->cu;
13178 bfd *abfd = reader->abfd;
13179
13180 offset.sect_off = info_ptr - reader->buffer;
13181 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13182 info_ptr += bytes_read;
13183 if (!abbrev_number)
13184 {
13185 *diep = NULL;
13186 *has_children = 0;
13187 return info_ptr;
13188 }
13189
13190 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
13191 if (!abbrev)
13192 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
13193 abbrev_number,
13194 bfd_get_filename (abfd));
13195
13196 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
13197 die->offset = offset;
13198 die->tag = abbrev->tag;
13199 die->abbrev = abbrev_number;
13200
13201 /* Make the result usable.
13202 The caller needs to update num_attrs after adding the extra
13203 attributes. */
13204 die->num_attrs = abbrev->num_attrs;
13205
13206 for (i = 0; i < abbrev->num_attrs; ++i)
13207 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
13208 info_ptr);
13209
13210 *diep = die;
13211 *has_children = abbrev->has_children;
13212 return info_ptr;
13213 }
13214
13215 /* Read a die and all its attributes.
13216 Set DIEP to point to a newly allocated die with its information,
13217 except for its child, sibling, and parent fields.
13218 Set HAS_CHILDREN to tell whether the die has children or not. */
13219
13220 static gdb_byte *
13221 read_full_die (const struct die_reader_specs *reader,
13222 struct die_info **diep, gdb_byte *info_ptr,
13223 int *has_children)
13224 {
13225 gdb_byte *result;
13226
13227 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
13228
13229 if (dwarf2_die_debug)
13230 {
13231 fprintf_unfiltered (gdb_stdlog,
13232 "Read die from %s@0x%x of %s:\n",
13233 bfd_section_name (reader->abfd,
13234 reader->die_section->asection),
13235 (unsigned) (info_ptr - reader->die_section->buffer),
13236 bfd_get_filename (reader->abfd));
13237 dump_die (*diep, dwarf2_die_debug);
13238 }
13239
13240 return result;
13241 }
13242 \f
13243 /* Abbreviation tables.
13244
13245 In DWARF version 2, the description of the debugging information is
13246 stored in a separate .debug_abbrev section. Before we read any
13247 dies from a section we read in all abbreviations and install them
13248 in a hash table. */
13249
13250 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
13251
13252 static struct abbrev_info *
13253 abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
13254 {
13255 struct abbrev_info *abbrev;
13256
13257 abbrev = (struct abbrev_info *)
13258 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
13259 memset (abbrev, 0, sizeof (struct abbrev_info));
13260 return abbrev;
13261 }
13262
13263 /* Add an abbreviation to the table. */
13264
13265 static void
13266 abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
13267 unsigned int abbrev_number,
13268 struct abbrev_info *abbrev)
13269 {
13270 unsigned int hash_number;
13271
13272 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13273 abbrev->next = abbrev_table->abbrevs[hash_number];
13274 abbrev_table->abbrevs[hash_number] = abbrev;
13275 }
13276
13277 /* Look up an abbrev in the table.
13278 Returns NULL if the abbrev is not found. */
13279
13280 static struct abbrev_info *
13281 abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
13282 unsigned int abbrev_number)
13283 {
13284 unsigned int hash_number;
13285 struct abbrev_info *abbrev;
13286
13287 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13288 abbrev = abbrev_table->abbrevs[hash_number];
13289
13290 while (abbrev)
13291 {
13292 if (abbrev->number == abbrev_number)
13293 return abbrev;
13294 abbrev = abbrev->next;
13295 }
13296 return NULL;
13297 }
13298
13299 /* Read in an abbrev table. */
13300
13301 static struct abbrev_table *
13302 abbrev_table_read_table (struct dwarf2_section_info *section,
13303 sect_offset offset)
13304 {
13305 struct objfile *objfile = dwarf2_per_objfile->objfile;
13306 bfd *abfd = section->asection->owner;
13307 struct abbrev_table *abbrev_table;
13308 gdb_byte *abbrev_ptr;
13309 struct abbrev_info *cur_abbrev;
13310 unsigned int abbrev_number, bytes_read, abbrev_name;
13311 unsigned int abbrev_form;
13312 struct attr_abbrev *cur_attrs;
13313 unsigned int allocated_attrs;
13314
13315 abbrev_table = XMALLOC (struct abbrev_table);
13316 abbrev_table->offset = offset;
13317 obstack_init (&abbrev_table->abbrev_obstack);
13318 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
13319 (ABBREV_HASH_SIZE
13320 * sizeof (struct abbrev_info *)));
13321 memset (abbrev_table->abbrevs, 0,
13322 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
13323
13324 dwarf2_read_section (objfile, section);
13325 abbrev_ptr = section->buffer + offset.sect_off;
13326 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13327 abbrev_ptr += bytes_read;
13328
13329 allocated_attrs = ATTR_ALLOC_CHUNK;
13330 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
13331
13332 /* Loop until we reach an abbrev number of 0. */
13333 while (abbrev_number)
13334 {
13335 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
13336
13337 /* read in abbrev header */
13338 cur_abbrev->number = abbrev_number;
13339 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13340 abbrev_ptr += bytes_read;
13341 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
13342 abbrev_ptr += 1;
13343
13344 /* now read in declarations */
13345 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13346 abbrev_ptr += bytes_read;
13347 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13348 abbrev_ptr += bytes_read;
13349 while (abbrev_name)
13350 {
13351 if (cur_abbrev->num_attrs == allocated_attrs)
13352 {
13353 allocated_attrs += ATTR_ALLOC_CHUNK;
13354 cur_attrs
13355 = xrealloc (cur_attrs, (allocated_attrs
13356 * sizeof (struct attr_abbrev)));
13357 }
13358
13359 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
13360 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
13361 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13362 abbrev_ptr += bytes_read;
13363 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13364 abbrev_ptr += bytes_read;
13365 }
13366
13367 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
13368 (cur_abbrev->num_attrs
13369 * sizeof (struct attr_abbrev)));
13370 memcpy (cur_abbrev->attrs, cur_attrs,
13371 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
13372
13373 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
13374
13375 /* Get next abbreviation.
13376 Under Irix6 the abbreviations for a compilation unit are not
13377 always properly terminated with an abbrev number of 0.
13378 Exit loop if we encounter an abbreviation which we have
13379 already read (which means we are about to read the abbreviations
13380 for the next compile unit) or if the end of the abbreviation
13381 table is reached. */
13382 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
13383 break;
13384 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13385 abbrev_ptr += bytes_read;
13386 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
13387 break;
13388 }
13389
13390 xfree (cur_attrs);
13391 return abbrev_table;
13392 }
13393
13394 /* Free the resources held by ABBREV_TABLE. */
13395
13396 static void
13397 abbrev_table_free (struct abbrev_table *abbrev_table)
13398 {
13399 obstack_free (&abbrev_table->abbrev_obstack, NULL);
13400 xfree (abbrev_table);
13401 }
13402
13403 /* Same as abbrev_table_free but as a cleanup.
13404 We pass in a pointer to the pointer to the table so that we can
13405 set the pointer to NULL when we're done. It also simplifies
13406 build_type_unit_groups. */
13407
13408 static void
13409 abbrev_table_free_cleanup (void *table_ptr)
13410 {
13411 struct abbrev_table **abbrev_table_ptr = table_ptr;
13412
13413 if (*abbrev_table_ptr != NULL)
13414 abbrev_table_free (*abbrev_table_ptr);
13415 *abbrev_table_ptr = NULL;
13416 }
13417
13418 /* Read the abbrev table for CU from ABBREV_SECTION. */
13419
13420 static void
13421 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
13422 struct dwarf2_section_info *abbrev_section)
13423 {
13424 cu->abbrev_table =
13425 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
13426 }
13427
13428 /* Release the memory used by the abbrev table for a compilation unit. */
13429
13430 static void
13431 dwarf2_free_abbrev_table (void *ptr_to_cu)
13432 {
13433 struct dwarf2_cu *cu = ptr_to_cu;
13434
13435 abbrev_table_free (cu->abbrev_table);
13436 /* Set this to NULL so that we SEGV if we try to read it later,
13437 and also because free_comp_unit verifies this is NULL. */
13438 cu->abbrev_table = NULL;
13439 }
13440 \f
13441 /* Returns nonzero if TAG represents a type that we might generate a partial
13442 symbol for. */
13443
13444 static int
13445 is_type_tag_for_partial (int tag)
13446 {
13447 switch (tag)
13448 {
13449 #if 0
13450 /* Some types that would be reasonable to generate partial symbols for,
13451 that we don't at present. */
13452 case DW_TAG_array_type:
13453 case DW_TAG_file_type:
13454 case DW_TAG_ptr_to_member_type:
13455 case DW_TAG_set_type:
13456 case DW_TAG_string_type:
13457 case DW_TAG_subroutine_type:
13458 #endif
13459 case DW_TAG_base_type:
13460 case DW_TAG_class_type:
13461 case DW_TAG_interface_type:
13462 case DW_TAG_enumeration_type:
13463 case DW_TAG_structure_type:
13464 case DW_TAG_subrange_type:
13465 case DW_TAG_typedef:
13466 case DW_TAG_union_type:
13467 return 1;
13468 default:
13469 return 0;
13470 }
13471 }
13472
13473 /* Load all DIEs that are interesting for partial symbols into memory. */
13474
13475 static struct partial_die_info *
13476 load_partial_dies (const struct die_reader_specs *reader,
13477 gdb_byte *info_ptr, int building_psymtab)
13478 {
13479 struct dwarf2_cu *cu = reader->cu;
13480 struct objfile *objfile = cu->objfile;
13481 struct partial_die_info *part_die;
13482 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
13483 struct abbrev_info *abbrev;
13484 unsigned int bytes_read;
13485 unsigned int load_all = 0;
13486 int nesting_level = 1;
13487
13488 parent_die = NULL;
13489 last_die = NULL;
13490
13491 gdb_assert (cu->per_cu != NULL);
13492 if (cu->per_cu->load_all_dies)
13493 load_all = 1;
13494
13495 cu->partial_dies
13496 = htab_create_alloc_ex (cu->header.length / 12,
13497 partial_die_hash,
13498 partial_die_eq,
13499 NULL,
13500 &cu->comp_unit_obstack,
13501 hashtab_obstack_allocate,
13502 dummy_obstack_deallocate);
13503
13504 part_die = obstack_alloc (&cu->comp_unit_obstack,
13505 sizeof (struct partial_die_info));
13506
13507 while (1)
13508 {
13509 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
13510
13511 /* A NULL abbrev means the end of a series of children. */
13512 if (abbrev == NULL)
13513 {
13514 if (--nesting_level == 0)
13515 {
13516 /* PART_DIE was probably the last thing allocated on the
13517 comp_unit_obstack, so we could call obstack_free
13518 here. We don't do that because the waste is small,
13519 and will be cleaned up when we're done with this
13520 compilation unit. This way, we're also more robust
13521 against other users of the comp_unit_obstack. */
13522 return first_die;
13523 }
13524 info_ptr += bytes_read;
13525 last_die = parent_die;
13526 parent_die = parent_die->die_parent;
13527 continue;
13528 }
13529
13530 /* Check for template arguments. We never save these; if
13531 they're seen, we just mark the parent, and go on our way. */
13532 if (parent_die != NULL
13533 && cu->language == language_cplus
13534 && (abbrev->tag == DW_TAG_template_type_param
13535 || abbrev->tag == DW_TAG_template_value_param))
13536 {
13537 parent_die->has_template_arguments = 1;
13538
13539 if (!load_all)
13540 {
13541 /* We don't need a partial DIE for the template argument. */
13542 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
13543 continue;
13544 }
13545 }
13546
13547 /* We only recurse into c++ subprograms looking for template arguments.
13548 Skip their other children. */
13549 if (!load_all
13550 && cu->language == language_cplus
13551 && parent_die != NULL
13552 && parent_die->tag == DW_TAG_subprogram)
13553 {
13554 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
13555 continue;
13556 }
13557
13558 /* Check whether this DIE is interesting enough to save. Normally
13559 we would not be interested in members here, but there may be
13560 later variables referencing them via DW_AT_specification (for
13561 static members). */
13562 if (!load_all
13563 && !is_type_tag_for_partial (abbrev->tag)
13564 && abbrev->tag != DW_TAG_constant
13565 && abbrev->tag != DW_TAG_enumerator
13566 && abbrev->tag != DW_TAG_subprogram
13567 && abbrev->tag != DW_TAG_lexical_block
13568 && abbrev->tag != DW_TAG_variable
13569 && abbrev->tag != DW_TAG_namespace
13570 && abbrev->tag != DW_TAG_module
13571 && abbrev->tag != DW_TAG_member
13572 && abbrev->tag != DW_TAG_imported_unit)
13573 {
13574 /* Otherwise we skip to the next sibling, if any. */
13575 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
13576 continue;
13577 }
13578
13579 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
13580 info_ptr);
13581
13582 /* This two-pass algorithm for processing partial symbols has a
13583 high cost in cache pressure. Thus, handle some simple cases
13584 here which cover the majority of C partial symbols. DIEs
13585 which neither have specification tags in them, nor could have
13586 specification tags elsewhere pointing at them, can simply be
13587 processed and discarded.
13588
13589 This segment is also optional; scan_partial_symbols and
13590 add_partial_symbol will handle these DIEs if we chain
13591 them in normally. When compilers which do not emit large
13592 quantities of duplicate debug information are more common,
13593 this code can probably be removed. */
13594
13595 /* Any complete simple types at the top level (pretty much all
13596 of them, for a language without namespaces), can be processed
13597 directly. */
13598 if (parent_die == NULL
13599 && part_die->has_specification == 0
13600 && part_die->is_declaration == 0
13601 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
13602 || part_die->tag == DW_TAG_base_type
13603 || part_die->tag == DW_TAG_subrange_type))
13604 {
13605 if (building_psymtab && part_die->name != NULL)
13606 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
13607 VAR_DOMAIN, LOC_TYPEDEF,
13608 &objfile->static_psymbols,
13609 0, (CORE_ADDR) 0, cu->language, objfile);
13610 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
13611 continue;
13612 }
13613
13614 /* The exception for DW_TAG_typedef with has_children above is
13615 a workaround of GCC PR debug/47510. In the case of this complaint
13616 type_name_no_tag_or_error will error on such types later.
13617
13618 GDB skipped children of DW_TAG_typedef by the shortcut above and then
13619 it could not find the child DIEs referenced later, this is checked
13620 above. In correct DWARF DW_TAG_typedef should have no children. */
13621
13622 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
13623 complaint (&symfile_complaints,
13624 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
13625 "- DIE at 0x%x [in module %s]"),
13626 part_die->offset.sect_off, objfile->name);
13627
13628 /* If we're at the second level, and we're an enumerator, and
13629 our parent has no specification (meaning possibly lives in a
13630 namespace elsewhere), then we can add the partial symbol now
13631 instead of queueing it. */
13632 if (part_die->tag == DW_TAG_enumerator
13633 && parent_die != NULL
13634 && parent_die->die_parent == NULL
13635 && parent_die->tag == DW_TAG_enumeration_type
13636 && parent_die->has_specification == 0)
13637 {
13638 if (part_die->name == NULL)
13639 complaint (&symfile_complaints,
13640 _("malformed enumerator DIE ignored"));
13641 else if (building_psymtab)
13642 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
13643 VAR_DOMAIN, LOC_CONST,
13644 (cu->language == language_cplus
13645 || cu->language == language_java)
13646 ? &objfile->global_psymbols
13647 : &objfile->static_psymbols,
13648 0, (CORE_ADDR) 0, cu->language, objfile);
13649
13650 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
13651 continue;
13652 }
13653
13654 /* We'll save this DIE so link it in. */
13655 part_die->die_parent = parent_die;
13656 part_die->die_sibling = NULL;
13657 part_die->die_child = NULL;
13658
13659 if (last_die && last_die == parent_die)
13660 last_die->die_child = part_die;
13661 else if (last_die)
13662 last_die->die_sibling = part_die;
13663
13664 last_die = part_die;
13665
13666 if (first_die == NULL)
13667 first_die = part_die;
13668
13669 /* Maybe add the DIE to the hash table. Not all DIEs that we
13670 find interesting need to be in the hash table, because we
13671 also have the parent/sibling/child chains; only those that we
13672 might refer to by offset later during partial symbol reading.
13673
13674 For now this means things that might have be the target of a
13675 DW_AT_specification, DW_AT_abstract_origin, or
13676 DW_AT_extension. DW_AT_extension will refer only to
13677 namespaces; DW_AT_abstract_origin refers to functions (and
13678 many things under the function DIE, but we do not recurse
13679 into function DIEs during partial symbol reading) and
13680 possibly variables as well; DW_AT_specification refers to
13681 declarations. Declarations ought to have the DW_AT_declaration
13682 flag. It happens that GCC forgets to put it in sometimes, but
13683 only for functions, not for types.
13684
13685 Adding more things than necessary to the hash table is harmless
13686 except for the performance cost. Adding too few will result in
13687 wasted time in find_partial_die, when we reread the compilation
13688 unit with load_all_dies set. */
13689
13690 if (load_all
13691 || abbrev->tag == DW_TAG_constant
13692 || abbrev->tag == DW_TAG_subprogram
13693 || abbrev->tag == DW_TAG_variable
13694 || abbrev->tag == DW_TAG_namespace
13695 || part_die->is_declaration)
13696 {
13697 void **slot;
13698
13699 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
13700 part_die->offset.sect_off, INSERT);
13701 *slot = part_die;
13702 }
13703
13704 part_die = obstack_alloc (&cu->comp_unit_obstack,
13705 sizeof (struct partial_die_info));
13706
13707 /* For some DIEs we want to follow their children (if any). For C
13708 we have no reason to follow the children of structures; for other
13709 languages we have to, so that we can get at method physnames
13710 to infer fully qualified class names, for DW_AT_specification,
13711 and for C++ template arguments. For C++, we also look one level
13712 inside functions to find template arguments (if the name of the
13713 function does not already contain the template arguments).
13714
13715 For Ada, we need to scan the children of subprograms and lexical
13716 blocks as well because Ada allows the definition of nested
13717 entities that could be interesting for the debugger, such as
13718 nested subprograms for instance. */
13719 if (last_die->has_children
13720 && (load_all
13721 || last_die->tag == DW_TAG_namespace
13722 || last_die->tag == DW_TAG_module
13723 || last_die->tag == DW_TAG_enumeration_type
13724 || (cu->language == language_cplus
13725 && last_die->tag == DW_TAG_subprogram
13726 && (last_die->name == NULL
13727 || strchr (last_die->name, '<') == NULL))
13728 || (cu->language != language_c
13729 && (last_die->tag == DW_TAG_class_type
13730 || last_die->tag == DW_TAG_interface_type
13731 || last_die->tag == DW_TAG_structure_type
13732 || last_die->tag == DW_TAG_union_type))
13733 || (cu->language == language_ada
13734 && (last_die->tag == DW_TAG_subprogram
13735 || last_die->tag == DW_TAG_lexical_block))))
13736 {
13737 nesting_level++;
13738 parent_die = last_die;
13739 continue;
13740 }
13741
13742 /* Otherwise we skip to the next sibling, if any. */
13743 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
13744
13745 /* Back to the top, do it again. */
13746 }
13747 }
13748
13749 /* Read a minimal amount of information into the minimal die structure. */
13750
13751 static gdb_byte *
13752 read_partial_die (const struct die_reader_specs *reader,
13753 struct partial_die_info *part_die,
13754 struct abbrev_info *abbrev, unsigned int abbrev_len,
13755 gdb_byte *info_ptr)
13756 {
13757 struct dwarf2_cu *cu = reader->cu;
13758 struct objfile *objfile = cu->objfile;
13759 gdb_byte *buffer = reader->buffer;
13760 unsigned int i;
13761 struct attribute attr;
13762 int has_low_pc_attr = 0;
13763 int has_high_pc_attr = 0;
13764 int high_pc_relative = 0;
13765
13766 memset (part_die, 0, sizeof (struct partial_die_info));
13767
13768 part_die->offset.sect_off = info_ptr - buffer;
13769
13770 info_ptr += abbrev_len;
13771
13772 if (abbrev == NULL)
13773 return info_ptr;
13774
13775 part_die->tag = abbrev->tag;
13776 part_die->has_children = abbrev->has_children;
13777
13778 for (i = 0; i < abbrev->num_attrs; ++i)
13779 {
13780 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
13781
13782 /* Store the data if it is of an attribute we want to keep in a
13783 partial symbol table. */
13784 switch (attr.name)
13785 {
13786 case DW_AT_name:
13787 switch (part_die->tag)
13788 {
13789 case DW_TAG_compile_unit:
13790 case DW_TAG_partial_unit:
13791 case DW_TAG_type_unit:
13792 /* Compilation units have a DW_AT_name that is a filename, not
13793 a source language identifier. */
13794 case DW_TAG_enumeration_type:
13795 case DW_TAG_enumerator:
13796 /* These tags always have simple identifiers already; no need
13797 to canonicalize them. */
13798 part_die->name = DW_STRING (&attr);
13799 break;
13800 default:
13801 part_die->name
13802 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
13803 &objfile->objfile_obstack);
13804 break;
13805 }
13806 break;
13807 case DW_AT_linkage_name:
13808 case DW_AT_MIPS_linkage_name:
13809 /* Note that both forms of linkage name might appear. We
13810 assume they will be the same, and we only store the last
13811 one we see. */
13812 if (cu->language == language_ada)
13813 part_die->name = DW_STRING (&attr);
13814 part_die->linkage_name = DW_STRING (&attr);
13815 break;
13816 case DW_AT_low_pc:
13817 has_low_pc_attr = 1;
13818 part_die->lowpc = DW_ADDR (&attr);
13819 break;
13820 case DW_AT_high_pc:
13821 has_high_pc_attr = 1;
13822 if (attr.form == DW_FORM_addr
13823 || attr.form == DW_FORM_GNU_addr_index)
13824 part_die->highpc = DW_ADDR (&attr);
13825 else
13826 {
13827 high_pc_relative = 1;
13828 part_die->highpc = DW_UNSND (&attr);
13829 }
13830 break;
13831 case DW_AT_location:
13832 /* Support the .debug_loc offsets. */
13833 if (attr_form_is_block (&attr))
13834 {
13835 part_die->d.locdesc = DW_BLOCK (&attr);
13836 }
13837 else if (attr_form_is_section_offset (&attr))
13838 {
13839 dwarf2_complex_location_expr_complaint ();
13840 }
13841 else
13842 {
13843 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
13844 "partial symbol information");
13845 }
13846 break;
13847 case DW_AT_external:
13848 part_die->is_external = DW_UNSND (&attr);
13849 break;
13850 case DW_AT_declaration:
13851 part_die->is_declaration = DW_UNSND (&attr);
13852 break;
13853 case DW_AT_type:
13854 part_die->has_type = 1;
13855 break;
13856 case DW_AT_abstract_origin:
13857 case DW_AT_specification:
13858 case DW_AT_extension:
13859 part_die->has_specification = 1;
13860 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
13861 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13862 || cu->per_cu->is_dwz);
13863 break;
13864 case DW_AT_sibling:
13865 /* Ignore absolute siblings, they might point outside of
13866 the current compile unit. */
13867 if (attr.form == DW_FORM_ref_addr)
13868 complaint (&symfile_complaints,
13869 _("ignoring absolute DW_AT_sibling"));
13870 else
13871 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
13872 break;
13873 case DW_AT_byte_size:
13874 part_die->has_byte_size = 1;
13875 break;
13876 case DW_AT_calling_convention:
13877 /* DWARF doesn't provide a way to identify a program's source-level
13878 entry point. DW_AT_calling_convention attributes are only meant
13879 to describe functions' calling conventions.
13880
13881 However, because it's a necessary piece of information in
13882 Fortran, and because DW_CC_program is the only piece of debugging
13883 information whose definition refers to a 'main program' at all,
13884 several compilers have begun marking Fortran main programs with
13885 DW_CC_program --- even when those functions use the standard
13886 calling conventions.
13887
13888 So until DWARF specifies a way to provide this information and
13889 compilers pick up the new representation, we'll support this
13890 practice. */
13891 if (DW_UNSND (&attr) == DW_CC_program
13892 && cu->language == language_fortran)
13893 {
13894 set_main_name (part_die->name);
13895
13896 /* As this DIE has a static linkage the name would be difficult
13897 to look up later. */
13898 language_of_main = language_fortran;
13899 }
13900 break;
13901 case DW_AT_inline:
13902 if (DW_UNSND (&attr) == DW_INL_inlined
13903 || DW_UNSND (&attr) == DW_INL_declared_inlined)
13904 part_die->may_be_inlined = 1;
13905 break;
13906
13907 case DW_AT_import:
13908 if (part_die->tag == DW_TAG_imported_unit)
13909 {
13910 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
13911 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13912 || cu->per_cu->is_dwz);
13913 }
13914 break;
13915
13916 default:
13917 break;
13918 }
13919 }
13920
13921 if (high_pc_relative)
13922 part_die->highpc += part_die->lowpc;
13923
13924 if (has_low_pc_attr && has_high_pc_attr)
13925 {
13926 /* When using the GNU linker, .gnu.linkonce. sections are used to
13927 eliminate duplicate copies of functions and vtables and such.
13928 The linker will arbitrarily choose one and discard the others.
13929 The AT_*_pc values for such functions refer to local labels in
13930 these sections. If the section from that file was discarded, the
13931 labels are not in the output, so the relocs get a value of 0.
13932 If this is a discarded function, mark the pc bounds as invalid,
13933 so that GDB will ignore it. */
13934 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
13935 {
13936 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13937
13938 complaint (&symfile_complaints,
13939 _("DW_AT_low_pc %s is zero "
13940 "for DIE at 0x%x [in module %s]"),
13941 paddress (gdbarch, part_die->lowpc),
13942 part_die->offset.sect_off, objfile->name);
13943 }
13944 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
13945 else if (part_die->lowpc >= part_die->highpc)
13946 {
13947 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13948
13949 complaint (&symfile_complaints,
13950 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
13951 "for DIE at 0x%x [in module %s]"),
13952 paddress (gdbarch, part_die->lowpc),
13953 paddress (gdbarch, part_die->highpc),
13954 part_die->offset.sect_off, objfile->name);
13955 }
13956 else
13957 part_die->has_pc_info = 1;
13958 }
13959
13960 return info_ptr;
13961 }
13962
13963 /* Find a cached partial DIE at OFFSET in CU. */
13964
13965 static struct partial_die_info *
13966 find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
13967 {
13968 struct partial_die_info *lookup_die = NULL;
13969 struct partial_die_info part_die;
13970
13971 part_die.offset = offset;
13972 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
13973 offset.sect_off);
13974
13975 return lookup_die;
13976 }
13977
13978 /* Find a partial DIE at OFFSET, which may or may not be in CU,
13979 except in the case of .debug_types DIEs which do not reference
13980 outside their CU (they do however referencing other types via
13981 DW_FORM_ref_sig8). */
13982
13983 static struct partial_die_info *
13984 find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
13985 {
13986 struct objfile *objfile = cu->objfile;
13987 struct dwarf2_per_cu_data *per_cu = NULL;
13988 struct partial_die_info *pd = NULL;
13989
13990 if (offset_in_dwz == cu->per_cu->is_dwz
13991 && offset_in_cu_p (&cu->header, offset))
13992 {
13993 pd = find_partial_die_in_comp_unit (offset, cu);
13994 if (pd != NULL)
13995 return pd;
13996 /* We missed recording what we needed.
13997 Load all dies and try again. */
13998 per_cu = cu->per_cu;
13999 }
14000 else
14001 {
14002 /* TUs don't reference other CUs/TUs (except via type signatures). */
14003 if (cu->per_cu->is_debug_types)
14004 {
14005 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
14006 " external reference to offset 0x%lx [in module %s].\n"),
14007 (long) cu->header.offset.sect_off, (long) offset.sect_off,
14008 bfd_get_filename (objfile->obfd));
14009 }
14010 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
14011 objfile);
14012
14013 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
14014 load_partial_comp_unit (per_cu);
14015
14016 per_cu->cu->last_used = 0;
14017 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
14018 }
14019
14020 /* If we didn't find it, and not all dies have been loaded,
14021 load them all and try again. */
14022
14023 if (pd == NULL && per_cu->load_all_dies == 0)
14024 {
14025 per_cu->load_all_dies = 1;
14026
14027 /* This is nasty. When we reread the DIEs, somewhere up the call chain
14028 THIS_CU->cu may already be in use. So we can't just free it and
14029 replace its DIEs with the ones we read in. Instead, we leave those
14030 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
14031 and clobber THIS_CU->cu->partial_dies with the hash table for the new
14032 set. */
14033 load_partial_comp_unit (per_cu);
14034
14035 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
14036 }
14037
14038 if (pd == NULL)
14039 internal_error (__FILE__, __LINE__,
14040 _("could not find partial DIE 0x%x "
14041 "in cache [from module %s]\n"),
14042 offset.sect_off, bfd_get_filename (objfile->obfd));
14043 return pd;
14044 }
14045
14046 /* See if we can figure out if the class lives in a namespace. We do
14047 this by looking for a member function; its demangled name will
14048 contain namespace info, if there is any. */
14049
14050 static void
14051 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
14052 struct dwarf2_cu *cu)
14053 {
14054 /* NOTE: carlton/2003-10-07: Getting the info this way changes
14055 what template types look like, because the demangler
14056 frequently doesn't give the same name as the debug info. We
14057 could fix this by only using the demangled name to get the
14058 prefix (but see comment in read_structure_type). */
14059
14060 struct partial_die_info *real_pdi;
14061 struct partial_die_info *child_pdi;
14062
14063 /* If this DIE (this DIE's specification, if any) has a parent, then
14064 we should not do this. We'll prepend the parent's fully qualified
14065 name when we create the partial symbol. */
14066
14067 real_pdi = struct_pdi;
14068 while (real_pdi->has_specification)
14069 real_pdi = find_partial_die (real_pdi->spec_offset,
14070 real_pdi->spec_is_dwz, cu);
14071
14072 if (real_pdi->die_parent != NULL)
14073 return;
14074
14075 for (child_pdi = struct_pdi->die_child;
14076 child_pdi != NULL;
14077 child_pdi = child_pdi->die_sibling)
14078 {
14079 if (child_pdi->tag == DW_TAG_subprogram
14080 && child_pdi->linkage_name != NULL)
14081 {
14082 char *actual_class_name
14083 = language_class_name_from_physname (cu->language_defn,
14084 child_pdi->linkage_name);
14085 if (actual_class_name != NULL)
14086 {
14087 struct_pdi->name
14088 = obstack_copy0 (&cu->objfile->objfile_obstack,
14089 actual_class_name,
14090 strlen (actual_class_name));
14091 xfree (actual_class_name);
14092 }
14093 break;
14094 }
14095 }
14096 }
14097
14098 /* Adjust PART_DIE before generating a symbol for it. This function
14099 may set the is_external flag or change the DIE's name. */
14100
14101 static void
14102 fixup_partial_die (struct partial_die_info *part_die,
14103 struct dwarf2_cu *cu)
14104 {
14105 /* Once we've fixed up a die, there's no point in doing so again.
14106 This also avoids a memory leak if we were to call
14107 guess_partial_die_structure_name multiple times. */
14108 if (part_die->fixup_called)
14109 return;
14110
14111 /* If we found a reference attribute and the DIE has no name, try
14112 to find a name in the referred to DIE. */
14113
14114 if (part_die->name == NULL && part_die->has_specification)
14115 {
14116 struct partial_die_info *spec_die;
14117
14118 spec_die = find_partial_die (part_die->spec_offset,
14119 part_die->spec_is_dwz, cu);
14120
14121 fixup_partial_die (spec_die, cu);
14122
14123 if (spec_die->name)
14124 {
14125 part_die->name = spec_die->name;
14126
14127 /* Copy DW_AT_external attribute if it is set. */
14128 if (spec_die->is_external)
14129 part_die->is_external = spec_die->is_external;
14130 }
14131 }
14132
14133 /* Set default names for some unnamed DIEs. */
14134
14135 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
14136 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
14137
14138 /* If there is no parent die to provide a namespace, and there are
14139 children, see if we can determine the namespace from their linkage
14140 name. */
14141 if (cu->language == language_cplus
14142 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
14143 && part_die->die_parent == NULL
14144 && part_die->has_children
14145 && (part_die->tag == DW_TAG_class_type
14146 || part_die->tag == DW_TAG_structure_type
14147 || part_die->tag == DW_TAG_union_type))
14148 guess_partial_die_structure_name (part_die, cu);
14149
14150 /* GCC might emit a nameless struct or union that has a linkage
14151 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14152 if (part_die->name == NULL
14153 && (part_die->tag == DW_TAG_class_type
14154 || part_die->tag == DW_TAG_interface_type
14155 || part_die->tag == DW_TAG_structure_type
14156 || part_die->tag == DW_TAG_union_type)
14157 && part_die->linkage_name != NULL)
14158 {
14159 char *demangled;
14160
14161 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
14162 if (demangled)
14163 {
14164 const char *base;
14165
14166 /* Strip any leading namespaces/classes, keep only the base name.
14167 DW_AT_name for named DIEs does not contain the prefixes. */
14168 base = strrchr (demangled, ':');
14169 if (base && base > demangled && base[-1] == ':')
14170 base++;
14171 else
14172 base = demangled;
14173
14174 part_die->name = obstack_copy0 (&cu->objfile->objfile_obstack,
14175 base, strlen (base));
14176 xfree (demangled);
14177 }
14178 }
14179
14180 part_die->fixup_called = 1;
14181 }
14182
14183 /* Read an attribute value described by an attribute form. */
14184
14185 static gdb_byte *
14186 read_attribute_value (const struct die_reader_specs *reader,
14187 struct attribute *attr, unsigned form,
14188 gdb_byte *info_ptr)
14189 {
14190 struct dwarf2_cu *cu = reader->cu;
14191 bfd *abfd = reader->abfd;
14192 struct comp_unit_head *cu_header = &cu->header;
14193 unsigned int bytes_read;
14194 struct dwarf_block *blk;
14195
14196 attr->form = form;
14197 switch (form)
14198 {
14199 case DW_FORM_ref_addr:
14200 if (cu->header.version == 2)
14201 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
14202 else
14203 DW_UNSND (attr) = read_offset (abfd, info_ptr,
14204 &cu->header, &bytes_read);
14205 info_ptr += bytes_read;
14206 break;
14207 case DW_FORM_GNU_ref_alt:
14208 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14209 info_ptr += bytes_read;
14210 break;
14211 case DW_FORM_addr:
14212 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
14213 info_ptr += bytes_read;
14214 break;
14215 case DW_FORM_block2:
14216 blk = dwarf_alloc_block (cu);
14217 blk->size = read_2_bytes (abfd, info_ptr);
14218 info_ptr += 2;
14219 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14220 info_ptr += blk->size;
14221 DW_BLOCK (attr) = blk;
14222 break;
14223 case DW_FORM_block4:
14224 blk = dwarf_alloc_block (cu);
14225 blk->size = read_4_bytes (abfd, info_ptr);
14226 info_ptr += 4;
14227 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14228 info_ptr += blk->size;
14229 DW_BLOCK (attr) = blk;
14230 break;
14231 case DW_FORM_data2:
14232 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
14233 info_ptr += 2;
14234 break;
14235 case DW_FORM_data4:
14236 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
14237 info_ptr += 4;
14238 break;
14239 case DW_FORM_data8:
14240 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
14241 info_ptr += 8;
14242 break;
14243 case DW_FORM_sec_offset:
14244 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14245 info_ptr += bytes_read;
14246 break;
14247 case DW_FORM_string:
14248 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
14249 DW_STRING_IS_CANONICAL (attr) = 0;
14250 info_ptr += bytes_read;
14251 break;
14252 case DW_FORM_strp:
14253 if (!cu->per_cu->is_dwz)
14254 {
14255 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
14256 &bytes_read);
14257 DW_STRING_IS_CANONICAL (attr) = 0;
14258 info_ptr += bytes_read;
14259 break;
14260 }
14261 /* FALLTHROUGH */
14262 case DW_FORM_GNU_strp_alt:
14263 {
14264 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14265 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
14266 &bytes_read);
14267
14268 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
14269 DW_STRING_IS_CANONICAL (attr) = 0;
14270 info_ptr += bytes_read;
14271 }
14272 break;
14273 case DW_FORM_exprloc:
14274 case DW_FORM_block:
14275 blk = dwarf_alloc_block (cu);
14276 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14277 info_ptr += bytes_read;
14278 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14279 info_ptr += blk->size;
14280 DW_BLOCK (attr) = blk;
14281 break;
14282 case DW_FORM_block1:
14283 blk = dwarf_alloc_block (cu);
14284 blk->size = read_1_byte (abfd, info_ptr);
14285 info_ptr += 1;
14286 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14287 info_ptr += blk->size;
14288 DW_BLOCK (attr) = blk;
14289 break;
14290 case DW_FORM_data1:
14291 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14292 info_ptr += 1;
14293 break;
14294 case DW_FORM_flag:
14295 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14296 info_ptr += 1;
14297 break;
14298 case DW_FORM_flag_present:
14299 DW_UNSND (attr) = 1;
14300 break;
14301 case DW_FORM_sdata:
14302 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
14303 info_ptr += bytes_read;
14304 break;
14305 case DW_FORM_udata:
14306 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14307 info_ptr += bytes_read;
14308 break;
14309 case DW_FORM_ref1:
14310 DW_UNSND (attr) = (cu->header.offset.sect_off
14311 + read_1_byte (abfd, info_ptr));
14312 info_ptr += 1;
14313 break;
14314 case DW_FORM_ref2:
14315 DW_UNSND (attr) = (cu->header.offset.sect_off
14316 + read_2_bytes (abfd, info_ptr));
14317 info_ptr += 2;
14318 break;
14319 case DW_FORM_ref4:
14320 DW_UNSND (attr) = (cu->header.offset.sect_off
14321 + read_4_bytes (abfd, info_ptr));
14322 info_ptr += 4;
14323 break;
14324 case DW_FORM_ref8:
14325 DW_UNSND (attr) = (cu->header.offset.sect_off
14326 + read_8_bytes (abfd, info_ptr));
14327 info_ptr += 8;
14328 break;
14329 case DW_FORM_ref_sig8:
14330 /* Convert the signature to something we can record in DW_UNSND
14331 for later lookup.
14332 NOTE: This is NULL if the type wasn't found. */
14333 DW_SIGNATURED_TYPE (attr) =
14334 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
14335 info_ptr += 8;
14336 break;
14337 case DW_FORM_ref_udata:
14338 DW_UNSND (attr) = (cu->header.offset.sect_off
14339 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
14340 info_ptr += bytes_read;
14341 break;
14342 case DW_FORM_indirect:
14343 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14344 info_ptr += bytes_read;
14345 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
14346 break;
14347 case DW_FORM_GNU_addr_index:
14348 if (reader->dwo_file == NULL)
14349 {
14350 /* For now flag a hard error.
14351 Later we can turn this into a complaint. */
14352 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14353 dwarf_form_name (form),
14354 bfd_get_filename (abfd));
14355 }
14356 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
14357 info_ptr += bytes_read;
14358 break;
14359 case DW_FORM_GNU_str_index:
14360 if (reader->dwo_file == NULL)
14361 {
14362 /* For now flag a hard error.
14363 Later we can turn this into a complaint if warranted. */
14364 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14365 dwarf_form_name (form),
14366 bfd_get_filename (abfd));
14367 }
14368 {
14369 ULONGEST str_index =
14370 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14371
14372 DW_STRING (attr) = read_str_index (reader, cu, str_index);
14373 DW_STRING_IS_CANONICAL (attr) = 0;
14374 info_ptr += bytes_read;
14375 }
14376 break;
14377 default:
14378 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
14379 dwarf_form_name (form),
14380 bfd_get_filename (abfd));
14381 }
14382
14383 /* Super hack. */
14384 if (cu->per_cu->is_dwz && is_ref_attr (attr))
14385 attr->form = DW_FORM_GNU_ref_alt;
14386
14387 /* We have seen instances where the compiler tried to emit a byte
14388 size attribute of -1 which ended up being encoded as an unsigned
14389 0xffffffff. Although 0xffffffff is technically a valid size value,
14390 an object of this size seems pretty unlikely so we can relatively
14391 safely treat these cases as if the size attribute was invalid and
14392 treat them as zero by default. */
14393 if (attr->name == DW_AT_byte_size
14394 && form == DW_FORM_data4
14395 && DW_UNSND (attr) >= 0xffffffff)
14396 {
14397 complaint
14398 (&symfile_complaints,
14399 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
14400 hex_string (DW_UNSND (attr)));
14401 DW_UNSND (attr) = 0;
14402 }
14403
14404 return info_ptr;
14405 }
14406
14407 /* Read an attribute described by an abbreviated attribute. */
14408
14409 static gdb_byte *
14410 read_attribute (const struct die_reader_specs *reader,
14411 struct attribute *attr, struct attr_abbrev *abbrev,
14412 gdb_byte *info_ptr)
14413 {
14414 attr->name = abbrev->name;
14415 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
14416 }
14417
14418 /* Read dwarf information from a buffer. */
14419
14420 static unsigned int
14421 read_1_byte (bfd *abfd, const gdb_byte *buf)
14422 {
14423 return bfd_get_8 (abfd, buf);
14424 }
14425
14426 static int
14427 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
14428 {
14429 return bfd_get_signed_8 (abfd, buf);
14430 }
14431
14432 static unsigned int
14433 read_2_bytes (bfd *abfd, const gdb_byte *buf)
14434 {
14435 return bfd_get_16 (abfd, buf);
14436 }
14437
14438 static int
14439 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
14440 {
14441 return bfd_get_signed_16 (abfd, buf);
14442 }
14443
14444 static unsigned int
14445 read_4_bytes (bfd *abfd, const gdb_byte *buf)
14446 {
14447 return bfd_get_32 (abfd, buf);
14448 }
14449
14450 static int
14451 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
14452 {
14453 return bfd_get_signed_32 (abfd, buf);
14454 }
14455
14456 static ULONGEST
14457 read_8_bytes (bfd *abfd, const gdb_byte *buf)
14458 {
14459 return bfd_get_64 (abfd, buf);
14460 }
14461
14462 static CORE_ADDR
14463 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
14464 unsigned int *bytes_read)
14465 {
14466 struct comp_unit_head *cu_header = &cu->header;
14467 CORE_ADDR retval = 0;
14468
14469 if (cu_header->signed_addr_p)
14470 {
14471 switch (cu_header->addr_size)
14472 {
14473 case 2:
14474 retval = bfd_get_signed_16 (abfd, buf);
14475 break;
14476 case 4:
14477 retval = bfd_get_signed_32 (abfd, buf);
14478 break;
14479 case 8:
14480 retval = bfd_get_signed_64 (abfd, buf);
14481 break;
14482 default:
14483 internal_error (__FILE__, __LINE__,
14484 _("read_address: bad switch, signed [in module %s]"),
14485 bfd_get_filename (abfd));
14486 }
14487 }
14488 else
14489 {
14490 switch (cu_header->addr_size)
14491 {
14492 case 2:
14493 retval = bfd_get_16 (abfd, buf);
14494 break;
14495 case 4:
14496 retval = bfd_get_32 (abfd, buf);
14497 break;
14498 case 8:
14499 retval = bfd_get_64 (abfd, buf);
14500 break;
14501 default:
14502 internal_error (__FILE__, __LINE__,
14503 _("read_address: bad switch, "
14504 "unsigned [in module %s]"),
14505 bfd_get_filename (abfd));
14506 }
14507 }
14508
14509 *bytes_read = cu_header->addr_size;
14510 return retval;
14511 }
14512
14513 /* Read the initial length from a section. The (draft) DWARF 3
14514 specification allows the initial length to take up either 4 bytes
14515 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
14516 bytes describe the length and all offsets will be 8 bytes in length
14517 instead of 4.
14518
14519 An older, non-standard 64-bit format is also handled by this
14520 function. The older format in question stores the initial length
14521 as an 8-byte quantity without an escape value. Lengths greater
14522 than 2^32 aren't very common which means that the initial 4 bytes
14523 is almost always zero. Since a length value of zero doesn't make
14524 sense for the 32-bit format, this initial zero can be considered to
14525 be an escape value which indicates the presence of the older 64-bit
14526 format. As written, the code can't detect (old format) lengths
14527 greater than 4GB. If it becomes necessary to handle lengths
14528 somewhat larger than 4GB, we could allow other small values (such
14529 as the non-sensical values of 1, 2, and 3) to also be used as
14530 escape values indicating the presence of the old format.
14531
14532 The value returned via bytes_read should be used to increment the
14533 relevant pointer after calling read_initial_length().
14534
14535 [ Note: read_initial_length() and read_offset() are based on the
14536 document entitled "DWARF Debugging Information Format", revision
14537 3, draft 8, dated November 19, 2001. This document was obtained
14538 from:
14539
14540 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
14541
14542 This document is only a draft and is subject to change. (So beware.)
14543
14544 Details regarding the older, non-standard 64-bit format were
14545 determined empirically by examining 64-bit ELF files produced by
14546 the SGI toolchain on an IRIX 6.5 machine.
14547
14548 - Kevin, July 16, 2002
14549 ] */
14550
14551 static LONGEST
14552 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
14553 {
14554 LONGEST length = bfd_get_32 (abfd, buf);
14555
14556 if (length == 0xffffffff)
14557 {
14558 length = bfd_get_64 (abfd, buf + 4);
14559 *bytes_read = 12;
14560 }
14561 else if (length == 0)
14562 {
14563 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
14564 length = bfd_get_64 (abfd, buf);
14565 *bytes_read = 8;
14566 }
14567 else
14568 {
14569 *bytes_read = 4;
14570 }
14571
14572 return length;
14573 }
14574
14575 /* Cover function for read_initial_length.
14576 Returns the length of the object at BUF, and stores the size of the
14577 initial length in *BYTES_READ and stores the size that offsets will be in
14578 *OFFSET_SIZE.
14579 If the initial length size is not equivalent to that specified in
14580 CU_HEADER then issue a complaint.
14581 This is useful when reading non-comp-unit headers. */
14582
14583 static LONGEST
14584 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
14585 const struct comp_unit_head *cu_header,
14586 unsigned int *bytes_read,
14587 unsigned int *offset_size)
14588 {
14589 LONGEST length = read_initial_length (abfd, buf, bytes_read);
14590
14591 gdb_assert (cu_header->initial_length_size == 4
14592 || cu_header->initial_length_size == 8
14593 || cu_header->initial_length_size == 12);
14594
14595 if (cu_header->initial_length_size != *bytes_read)
14596 complaint (&symfile_complaints,
14597 _("intermixed 32-bit and 64-bit DWARF sections"));
14598
14599 *offset_size = (*bytes_read == 4) ? 4 : 8;
14600 return length;
14601 }
14602
14603 /* Read an offset from the data stream. The size of the offset is
14604 given by cu_header->offset_size. */
14605
14606 static LONGEST
14607 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
14608 unsigned int *bytes_read)
14609 {
14610 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
14611
14612 *bytes_read = cu_header->offset_size;
14613 return offset;
14614 }
14615
14616 /* Read an offset from the data stream. */
14617
14618 static LONGEST
14619 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
14620 {
14621 LONGEST retval = 0;
14622
14623 switch (offset_size)
14624 {
14625 case 4:
14626 retval = bfd_get_32 (abfd, buf);
14627 break;
14628 case 8:
14629 retval = bfd_get_64 (abfd, buf);
14630 break;
14631 default:
14632 internal_error (__FILE__, __LINE__,
14633 _("read_offset_1: bad switch [in module %s]"),
14634 bfd_get_filename (abfd));
14635 }
14636
14637 return retval;
14638 }
14639
14640 static gdb_byte *
14641 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
14642 {
14643 /* If the size of a host char is 8 bits, we can return a pointer
14644 to the buffer, otherwise we have to copy the data to a buffer
14645 allocated on the temporary obstack. */
14646 gdb_assert (HOST_CHAR_BIT == 8);
14647 return buf;
14648 }
14649
14650 static char *
14651 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
14652 {
14653 /* If the size of a host char is 8 bits, we can return a pointer
14654 to the string, otherwise we have to copy the string to a buffer
14655 allocated on the temporary obstack. */
14656 gdb_assert (HOST_CHAR_BIT == 8);
14657 if (*buf == '\0')
14658 {
14659 *bytes_read_ptr = 1;
14660 return NULL;
14661 }
14662 *bytes_read_ptr = strlen ((char *) buf) + 1;
14663 return (char *) buf;
14664 }
14665
14666 static char *
14667 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
14668 {
14669 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
14670 if (dwarf2_per_objfile->str.buffer == NULL)
14671 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
14672 bfd_get_filename (abfd));
14673 if (str_offset >= dwarf2_per_objfile->str.size)
14674 error (_("DW_FORM_strp pointing outside of "
14675 ".debug_str section [in module %s]"),
14676 bfd_get_filename (abfd));
14677 gdb_assert (HOST_CHAR_BIT == 8);
14678 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
14679 return NULL;
14680 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
14681 }
14682
14683 /* Read a string at offset STR_OFFSET in the .debug_str section from
14684 the .dwz file DWZ. Throw an error if the offset is too large. If
14685 the string consists of a single NUL byte, return NULL; otherwise
14686 return a pointer to the string. */
14687
14688 static char *
14689 read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
14690 {
14691 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
14692
14693 if (dwz->str.buffer == NULL)
14694 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
14695 "section [in module %s]"),
14696 bfd_get_filename (dwz->dwz_bfd));
14697 if (str_offset >= dwz->str.size)
14698 error (_("DW_FORM_GNU_strp_alt pointing outside of "
14699 ".debug_str section [in module %s]"),
14700 bfd_get_filename (dwz->dwz_bfd));
14701 gdb_assert (HOST_CHAR_BIT == 8);
14702 if (dwz->str.buffer[str_offset] == '\0')
14703 return NULL;
14704 return (char *) (dwz->str.buffer + str_offset);
14705 }
14706
14707 static char *
14708 read_indirect_string (bfd *abfd, gdb_byte *buf,
14709 const struct comp_unit_head *cu_header,
14710 unsigned int *bytes_read_ptr)
14711 {
14712 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
14713
14714 return read_indirect_string_at_offset (abfd, str_offset);
14715 }
14716
14717 static ULONGEST
14718 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
14719 {
14720 ULONGEST result;
14721 unsigned int num_read;
14722 int i, shift;
14723 unsigned char byte;
14724
14725 result = 0;
14726 shift = 0;
14727 num_read = 0;
14728 i = 0;
14729 while (1)
14730 {
14731 byte = bfd_get_8 (abfd, buf);
14732 buf++;
14733 num_read++;
14734 result |= ((ULONGEST) (byte & 127) << shift);
14735 if ((byte & 128) == 0)
14736 {
14737 break;
14738 }
14739 shift += 7;
14740 }
14741 *bytes_read_ptr = num_read;
14742 return result;
14743 }
14744
14745 static LONGEST
14746 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
14747 {
14748 LONGEST result;
14749 int i, shift, num_read;
14750 unsigned char byte;
14751
14752 result = 0;
14753 shift = 0;
14754 num_read = 0;
14755 i = 0;
14756 while (1)
14757 {
14758 byte = bfd_get_8 (abfd, buf);
14759 buf++;
14760 num_read++;
14761 result |= ((LONGEST) (byte & 127) << shift);
14762 shift += 7;
14763 if ((byte & 128) == 0)
14764 {
14765 break;
14766 }
14767 }
14768 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
14769 result |= -(((LONGEST) 1) << shift);
14770 *bytes_read_ptr = num_read;
14771 return result;
14772 }
14773
14774 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
14775 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
14776 ADDR_SIZE is the size of addresses from the CU header. */
14777
14778 static CORE_ADDR
14779 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
14780 {
14781 struct objfile *objfile = dwarf2_per_objfile->objfile;
14782 bfd *abfd = objfile->obfd;
14783 const gdb_byte *info_ptr;
14784
14785 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
14786 if (dwarf2_per_objfile->addr.buffer == NULL)
14787 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
14788 objfile->name);
14789 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
14790 error (_("DW_FORM_addr_index pointing outside of "
14791 ".debug_addr section [in module %s]"),
14792 objfile->name);
14793 info_ptr = (dwarf2_per_objfile->addr.buffer
14794 + addr_base + addr_index * addr_size);
14795 if (addr_size == 4)
14796 return bfd_get_32 (abfd, info_ptr);
14797 else
14798 return bfd_get_64 (abfd, info_ptr);
14799 }
14800
14801 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
14802
14803 static CORE_ADDR
14804 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
14805 {
14806 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
14807 }
14808
14809 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
14810
14811 static CORE_ADDR
14812 read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
14813 unsigned int *bytes_read)
14814 {
14815 bfd *abfd = cu->objfile->obfd;
14816 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
14817
14818 return read_addr_index (cu, addr_index);
14819 }
14820
14821 /* Data structure to pass results from dwarf2_read_addr_index_reader
14822 back to dwarf2_read_addr_index. */
14823
14824 struct dwarf2_read_addr_index_data
14825 {
14826 ULONGEST addr_base;
14827 int addr_size;
14828 };
14829
14830 /* die_reader_func for dwarf2_read_addr_index. */
14831
14832 static void
14833 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
14834 gdb_byte *info_ptr,
14835 struct die_info *comp_unit_die,
14836 int has_children,
14837 void *data)
14838 {
14839 struct dwarf2_cu *cu = reader->cu;
14840 struct dwarf2_read_addr_index_data *aidata =
14841 (struct dwarf2_read_addr_index_data *) data;
14842
14843 aidata->addr_base = cu->addr_base;
14844 aidata->addr_size = cu->header.addr_size;
14845 }
14846
14847 /* Given an index in .debug_addr, fetch the value.
14848 NOTE: This can be called during dwarf expression evaluation,
14849 long after the debug information has been read, and thus per_cu->cu
14850 may no longer exist. */
14851
14852 CORE_ADDR
14853 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
14854 unsigned int addr_index)
14855 {
14856 struct objfile *objfile = per_cu->objfile;
14857 struct dwarf2_cu *cu = per_cu->cu;
14858 ULONGEST addr_base;
14859 int addr_size;
14860
14861 /* This is intended to be called from outside this file. */
14862 dw2_setup (objfile);
14863
14864 /* We need addr_base and addr_size.
14865 If we don't have PER_CU->cu, we have to get it.
14866 Nasty, but the alternative is storing the needed info in PER_CU,
14867 which at this point doesn't seem justified: it's not clear how frequently
14868 it would get used and it would increase the size of every PER_CU.
14869 Entry points like dwarf2_per_cu_addr_size do a similar thing
14870 so we're not in uncharted territory here.
14871 Alas we need to be a bit more complicated as addr_base is contained
14872 in the DIE.
14873
14874 We don't need to read the entire CU(/TU).
14875 We just need the header and top level die.
14876
14877 IWBN to use the aging mechanism to let us lazily later discard the CU.
14878 For now we skip this optimization. */
14879
14880 if (cu != NULL)
14881 {
14882 addr_base = cu->addr_base;
14883 addr_size = cu->header.addr_size;
14884 }
14885 else
14886 {
14887 struct dwarf2_read_addr_index_data aidata;
14888
14889 /* Note: We can't use init_cutu_and_read_dies_simple here,
14890 we need addr_base. */
14891 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
14892 dwarf2_read_addr_index_reader, &aidata);
14893 addr_base = aidata.addr_base;
14894 addr_size = aidata.addr_size;
14895 }
14896
14897 return read_addr_index_1 (addr_index, addr_base, addr_size);
14898 }
14899
14900 /* Given a DW_AT_str_index, fetch the string. */
14901
14902 static char *
14903 read_str_index (const struct die_reader_specs *reader,
14904 struct dwarf2_cu *cu, ULONGEST str_index)
14905 {
14906 struct objfile *objfile = dwarf2_per_objfile->objfile;
14907 const char *dwo_name = objfile->name;
14908 bfd *abfd = objfile->obfd;
14909 struct dwo_sections *sections = &reader->dwo_file->sections;
14910 gdb_byte *info_ptr;
14911 ULONGEST str_offset;
14912
14913 dwarf2_read_section (objfile, &sections->str);
14914 dwarf2_read_section (objfile, &sections->str_offsets);
14915 if (sections->str.buffer == NULL)
14916 error (_("DW_FORM_str_index used without .debug_str.dwo section"
14917 " in CU at offset 0x%lx [in module %s]"),
14918 (long) cu->header.offset.sect_off, dwo_name);
14919 if (sections->str_offsets.buffer == NULL)
14920 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
14921 " in CU at offset 0x%lx [in module %s]"),
14922 (long) cu->header.offset.sect_off, dwo_name);
14923 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
14924 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
14925 " section in CU at offset 0x%lx [in module %s]"),
14926 (long) cu->header.offset.sect_off, dwo_name);
14927 info_ptr = (sections->str_offsets.buffer
14928 + str_index * cu->header.offset_size);
14929 if (cu->header.offset_size == 4)
14930 str_offset = bfd_get_32 (abfd, info_ptr);
14931 else
14932 str_offset = bfd_get_64 (abfd, info_ptr);
14933 if (str_offset >= sections->str.size)
14934 error (_("Offset from DW_FORM_str_index pointing outside of"
14935 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
14936 (long) cu->header.offset.sect_off, dwo_name);
14937 return (char *) (sections->str.buffer + str_offset);
14938 }
14939
14940 /* Return the length of an LEB128 number in BUF. */
14941
14942 static int
14943 leb128_size (const gdb_byte *buf)
14944 {
14945 const gdb_byte *begin = buf;
14946 gdb_byte byte;
14947
14948 while (1)
14949 {
14950 byte = *buf++;
14951 if ((byte & 128) == 0)
14952 return buf - begin;
14953 }
14954 }
14955
14956 static void
14957 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
14958 {
14959 switch (lang)
14960 {
14961 case DW_LANG_C89:
14962 case DW_LANG_C99:
14963 case DW_LANG_C:
14964 cu->language = language_c;
14965 break;
14966 case DW_LANG_C_plus_plus:
14967 cu->language = language_cplus;
14968 break;
14969 case DW_LANG_D:
14970 cu->language = language_d;
14971 break;
14972 case DW_LANG_Fortran77:
14973 case DW_LANG_Fortran90:
14974 case DW_LANG_Fortran95:
14975 cu->language = language_fortran;
14976 break;
14977 case DW_LANG_Go:
14978 cu->language = language_go;
14979 break;
14980 case DW_LANG_Mips_Assembler:
14981 cu->language = language_asm;
14982 break;
14983 case DW_LANG_Java:
14984 cu->language = language_java;
14985 break;
14986 case DW_LANG_Ada83:
14987 case DW_LANG_Ada95:
14988 cu->language = language_ada;
14989 break;
14990 case DW_LANG_Modula2:
14991 cu->language = language_m2;
14992 break;
14993 case DW_LANG_Pascal83:
14994 cu->language = language_pascal;
14995 break;
14996 case DW_LANG_ObjC:
14997 cu->language = language_objc;
14998 break;
14999 case DW_LANG_Cobol74:
15000 case DW_LANG_Cobol85:
15001 default:
15002 cu->language = language_minimal;
15003 break;
15004 }
15005 cu->language_defn = language_def (cu->language);
15006 }
15007
15008 /* Return the named attribute or NULL if not there. */
15009
15010 static struct attribute *
15011 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
15012 {
15013 for (;;)
15014 {
15015 unsigned int i;
15016 struct attribute *spec = NULL;
15017
15018 for (i = 0; i < die->num_attrs; ++i)
15019 {
15020 if (die->attrs[i].name == name)
15021 return &die->attrs[i];
15022 if (die->attrs[i].name == DW_AT_specification
15023 || die->attrs[i].name == DW_AT_abstract_origin)
15024 spec = &die->attrs[i];
15025 }
15026
15027 if (!spec)
15028 break;
15029
15030 die = follow_die_ref (die, spec, &cu);
15031 }
15032
15033 return NULL;
15034 }
15035
15036 /* Return the named attribute or NULL if not there,
15037 but do not follow DW_AT_specification, etc.
15038 This is for use in contexts where we're reading .debug_types dies.
15039 Following DW_AT_specification, DW_AT_abstract_origin will take us
15040 back up the chain, and we want to go down. */
15041
15042 static struct attribute *
15043 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
15044 {
15045 unsigned int i;
15046
15047 for (i = 0; i < die->num_attrs; ++i)
15048 if (die->attrs[i].name == name)
15049 return &die->attrs[i];
15050
15051 return NULL;
15052 }
15053
15054 /* Return non-zero iff the attribute NAME is defined for the given DIE,
15055 and holds a non-zero value. This function should only be used for
15056 DW_FORM_flag or DW_FORM_flag_present attributes. */
15057
15058 static int
15059 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
15060 {
15061 struct attribute *attr = dwarf2_attr (die, name, cu);
15062
15063 return (attr && DW_UNSND (attr));
15064 }
15065
15066 static int
15067 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
15068 {
15069 /* A DIE is a declaration if it has a DW_AT_declaration attribute
15070 which value is non-zero. However, we have to be careful with
15071 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
15072 (via dwarf2_flag_true_p) follows this attribute. So we may
15073 end up accidently finding a declaration attribute that belongs
15074 to a different DIE referenced by the specification attribute,
15075 even though the given DIE does not have a declaration attribute. */
15076 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
15077 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
15078 }
15079
15080 /* Return the die giving the specification for DIE, if there is
15081 one. *SPEC_CU is the CU containing DIE on input, and the CU
15082 containing the return value on output. If there is no
15083 specification, but there is an abstract origin, that is
15084 returned. */
15085
15086 static struct die_info *
15087 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
15088 {
15089 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
15090 *spec_cu);
15091
15092 if (spec_attr == NULL)
15093 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
15094
15095 if (spec_attr == NULL)
15096 return NULL;
15097 else
15098 return follow_die_ref (die, spec_attr, spec_cu);
15099 }
15100
15101 /* Free the line_header structure *LH, and any arrays and strings it
15102 refers to.
15103 NOTE: This is also used as a "cleanup" function. */
15104
15105 static void
15106 free_line_header (struct line_header *lh)
15107 {
15108 if (lh->standard_opcode_lengths)
15109 xfree (lh->standard_opcode_lengths);
15110
15111 /* Remember that all the lh->file_names[i].name pointers are
15112 pointers into debug_line_buffer, and don't need to be freed. */
15113 if (lh->file_names)
15114 xfree (lh->file_names);
15115
15116 /* Similarly for the include directory names. */
15117 if (lh->include_dirs)
15118 xfree (lh->include_dirs);
15119
15120 xfree (lh);
15121 }
15122
15123 /* Add an entry to LH's include directory table. */
15124
15125 static void
15126 add_include_dir (struct line_header *lh, char *include_dir)
15127 {
15128 /* Grow the array if necessary. */
15129 if (lh->include_dirs_size == 0)
15130 {
15131 lh->include_dirs_size = 1; /* for testing */
15132 lh->include_dirs = xmalloc (lh->include_dirs_size
15133 * sizeof (*lh->include_dirs));
15134 }
15135 else if (lh->num_include_dirs >= lh->include_dirs_size)
15136 {
15137 lh->include_dirs_size *= 2;
15138 lh->include_dirs = xrealloc (lh->include_dirs,
15139 (lh->include_dirs_size
15140 * sizeof (*lh->include_dirs)));
15141 }
15142
15143 lh->include_dirs[lh->num_include_dirs++] = include_dir;
15144 }
15145
15146 /* Add an entry to LH's file name table. */
15147
15148 static void
15149 add_file_name (struct line_header *lh,
15150 char *name,
15151 unsigned int dir_index,
15152 unsigned int mod_time,
15153 unsigned int length)
15154 {
15155 struct file_entry *fe;
15156
15157 /* Grow the array if necessary. */
15158 if (lh->file_names_size == 0)
15159 {
15160 lh->file_names_size = 1; /* for testing */
15161 lh->file_names = xmalloc (lh->file_names_size
15162 * sizeof (*lh->file_names));
15163 }
15164 else if (lh->num_file_names >= lh->file_names_size)
15165 {
15166 lh->file_names_size *= 2;
15167 lh->file_names = xrealloc (lh->file_names,
15168 (lh->file_names_size
15169 * sizeof (*lh->file_names)));
15170 }
15171
15172 fe = &lh->file_names[lh->num_file_names++];
15173 fe->name = name;
15174 fe->dir_index = dir_index;
15175 fe->mod_time = mod_time;
15176 fe->length = length;
15177 fe->included_p = 0;
15178 fe->symtab = NULL;
15179 }
15180
15181 /* A convenience function to find the proper .debug_line section for a
15182 CU. */
15183
15184 static struct dwarf2_section_info *
15185 get_debug_line_section (struct dwarf2_cu *cu)
15186 {
15187 struct dwarf2_section_info *section;
15188
15189 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
15190 DWO file. */
15191 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15192 section = &cu->dwo_unit->dwo_file->sections.line;
15193 else if (cu->per_cu->is_dwz)
15194 {
15195 struct dwz_file *dwz = dwarf2_get_dwz_file ();
15196
15197 section = &dwz->line;
15198 }
15199 else
15200 section = &dwarf2_per_objfile->line;
15201
15202 return section;
15203 }
15204
15205 /* Read the statement program header starting at OFFSET in
15206 .debug_line, or .debug_line.dwo. Return a pointer
15207 to a struct line_header, allocated using xmalloc.
15208
15209 NOTE: the strings in the include directory and file name tables of
15210 the returned object point into the dwarf line section buffer,
15211 and must not be freed. */
15212
15213 static struct line_header *
15214 dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
15215 {
15216 struct cleanup *back_to;
15217 struct line_header *lh;
15218 gdb_byte *line_ptr;
15219 unsigned int bytes_read, offset_size;
15220 int i;
15221 char *cur_dir, *cur_file;
15222 struct dwarf2_section_info *section;
15223 bfd *abfd;
15224
15225 section = get_debug_line_section (cu);
15226 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15227 if (section->buffer == NULL)
15228 {
15229 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15230 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
15231 else
15232 complaint (&symfile_complaints, _("missing .debug_line section"));
15233 return 0;
15234 }
15235
15236 /* We can't do this until we know the section is non-empty.
15237 Only then do we know we have such a section. */
15238 abfd = section->asection->owner;
15239
15240 /* Make sure that at least there's room for the total_length field.
15241 That could be 12 bytes long, but we're just going to fudge that. */
15242 if (offset + 4 >= section->size)
15243 {
15244 dwarf2_statement_list_fits_in_line_number_section_complaint ();
15245 return 0;
15246 }
15247
15248 lh = xmalloc (sizeof (*lh));
15249 memset (lh, 0, sizeof (*lh));
15250 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
15251 (void *) lh);
15252
15253 line_ptr = section->buffer + offset;
15254
15255 /* Read in the header. */
15256 lh->total_length =
15257 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
15258 &bytes_read, &offset_size);
15259 line_ptr += bytes_read;
15260 if (line_ptr + lh->total_length > (section->buffer + section->size))
15261 {
15262 dwarf2_statement_list_fits_in_line_number_section_complaint ();
15263 return 0;
15264 }
15265 lh->statement_program_end = line_ptr + lh->total_length;
15266 lh->version = read_2_bytes (abfd, line_ptr);
15267 line_ptr += 2;
15268 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
15269 line_ptr += offset_size;
15270 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
15271 line_ptr += 1;
15272 if (lh->version >= 4)
15273 {
15274 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
15275 line_ptr += 1;
15276 }
15277 else
15278 lh->maximum_ops_per_instruction = 1;
15279
15280 if (lh->maximum_ops_per_instruction == 0)
15281 {
15282 lh->maximum_ops_per_instruction = 1;
15283 complaint (&symfile_complaints,
15284 _("invalid maximum_ops_per_instruction "
15285 "in `.debug_line' section"));
15286 }
15287
15288 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
15289 line_ptr += 1;
15290 lh->line_base = read_1_signed_byte (abfd, line_ptr);
15291 line_ptr += 1;
15292 lh->line_range = read_1_byte (abfd, line_ptr);
15293 line_ptr += 1;
15294 lh->opcode_base = read_1_byte (abfd, line_ptr);
15295 line_ptr += 1;
15296 lh->standard_opcode_lengths
15297 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
15298
15299 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
15300 for (i = 1; i < lh->opcode_base; ++i)
15301 {
15302 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
15303 line_ptr += 1;
15304 }
15305
15306 /* Read directory table. */
15307 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
15308 {
15309 line_ptr += bytes_read;
15310 add_include_dir (lh, cur_dir);
15311 }
15312 line_ptr += bytes_read;
15313
15314 /* Read file name table. */
15315 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
15316 {
15317 unsigned int dir_index, mod_time, length;
15318
15319 line_ptr += bytes_read;
15320 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15321 line_ptr += bytes_read;
15322 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15323 line_ptr += bytes_read;
15324 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15325 line_ptr += bytes_read;
15326
15327 add_file_name (lh, cur_file, dir_index, mod_time, length);
15328 }
15329 line_ptr += bytes_read;
15330 lh->statement_program_start = line_ptr;
15331
15332 if (line_ptr > (section->buffer + section->size))
15333 complaint (&symfile_complaints,
15334 _("line number info header doesn't "
15335 "fit in `.debug_line' section"));
15336
15337 discard_cleanups (back_to);
15338 return lh;
15339 }
15340
15341 /* Subroutine of dwarf_decode_lines to simplify it.
15342 Return the file name of the psymtab for included file FILE_INDEX
15343 in line header LH of PST.
15344 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15345 If space for the result is malloc'd, it will be freed by a cleanup.
15346 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
15347
15348 The function creates dangling cleanup registration. */
15349
15350 static char *
15351 psymtab_include_file_name (const struct line_header *lh, int file_index,
15352 const struct partial_symtab *pst,
15353 const char *comp_dir)
15354 {
15355 const struct file_entry fe = lh->file_names [file_index];
15356 char *include_name = fe.name;
15357 char *include_name_to_compare = include_name;
15358 char *dir_name = NULL;
15359 const char *pst_filename;
15360 char *copied_name = NULL;
15361 int file_is_pst;
15362
15363 if (fe.dir_index)
15364 dir_name = lh->include_dirs[fe.dir_index - 1];
15365
15366 if (!IS_ABSOLUTE_PATH (include_name)
15367 && (dir_name != NULL || comp_dir != NULL))
15368 {
15369 /* Avoid creating a duplicate psymtab for PST.
15370 We do this by comparing INCLUDE_NAME and PST_FILENAME.
15371 Before we do the comparison, however, we need to account
15372 for DIR_NAME and COMP_DIR.
15373 First prepend dir_name (if non-NULL). If we still don't
15374 have an absolute path prepend comp_dir (if non-NULL).
15375 However, the directory we record in the include-file's
15376 psymtab does not contain COMP_DIR (to match the
15377 corresponding symtab(s)).
15378
15379 Example:
15380
15381 bash$ cd /tmp
15382 bash$ gcc -g ./hello.c
15383 include_name = "hello.c"
15384 dir_name = "."
15385 DW_AT_comp_dir = comp_dir = "/tmp"
15386 DW_AT_name = "./hello.c" */
15387
15388 if (dir_name != NULL)
15389 {
15390 include_name = concat (dir_name, SLASH_STRING,
15391 include_name, (char *)NULL);
15392 include_name_to_compare = include_name;
15393 make_cleanup (xfree, include_name);
15394 }
15395 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
15396 {
15397 include_name_to_compare = concat (comp_dir, SLASH_STRING,
15398 include_name, (char *)NULL);
15399 }
15400 }
15401
15402 pst_filename = pst->filename;
15403 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
15404 {
15405 copied_name = concat (pst->dirname, SLASH_STRING,
15406 pst_filename, (char *)NULL);
15407 pst_filename = copied_name;
15408 }
15409
15410 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
15411
15412 if (include_name_to_compare != include_name)
15413 xfree (include_name_to_compare);
15414 if (copied_name != NULL)
15415 xfree (copied_name);
15416
15417 if (file_is_pst)
15418 return NULL;
15419 return include_name;
15420 }
15421
15422 /* Ignore this record_line request. */
15423
15424 static void
15425 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
15426 {
15427 return;
15428 }
15429
15430 /* Subroutine of dwarf_decode_lines to simplify it.
15431 Process the line number information in LH. */
15432
15433 static void
15434 dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
15435 struct dwarf2_cu *cu, struct partial_symtab *pst)
15436 {
15437 gdb_byte *line_ptr, *extended_end;
15438 gdb_byte *line_end;
15439 unsigned int bytes_read, extended_len;
15440 unsigned char op_code, extended_op, adj_opcode;
15441 CORE_ADDR baseaddr;
15442 struct objfile *objfile = cu->objfile;
15443 bfd *abfd = objfile->obfd;
15444 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15445 const int decode_for_pst_p = (pst != NULL);
15446 struct subfile *last_subfile = NULL;
15447 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
15448 = record_line;
15449
15450 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15451
15452 line_ptr = lh->statement_program_start;
15453 line_end = lh->statement_program_end;
15454
15455 /* Read the statement sequences until there's nothing left. */
15456 while (line_ptr < line_end)
15457 {
15458 /* state machine registers */
15459 CORE_ADDR address = 0;
15460 unsigned int file = 1;
15461 unsigned int line = 1;
15462 unsigned int column = 0;
15463 int is_stmt = lh->default_is_stmt;
15464 int basic_block = 0;
15465 int end_sequence = 0;
15466 CORE_ADDR addr;
15467 unsigned char op_index = 0;
15468
15469 if (!decode_for_pst_p && lh->num_file_names >= file)
15470 {
15471 /* Start a subfile for the current file of the state machine. */
15472 /* lh->include_dirs and lh->file_names are 0-based, but the
15473 directory and file name numbers in the statement program
15474 are 1-based. */
15475 struct file_entry *fe = &lh->file_names[file - 1];
15476 char *dir = NULL;
15477
15478 if (fe->dir_index)
15479 dir = lh->include_dirs[fe->dir_index - 1];
15480
15481 dwarf2_start_subfile (fe->name, dir, comp_dir);
15482 }
15483
15484 /* Decode the table. */
15485 while (!end_sequence)
15486 {
15487 op_code = read_1_byte (abfd, line_ptr);
15488 line_ptr += 1;
15489 if (line_ptr > line_end)
15490 {
15491 dwarf2_debug_line_missing_end_sequence_complaint ();
15492 break;
15493 }
15494
15495 if (op_code >= lh->opcode_base)
15496 {
15497 /* Special operand. */
15498 adj_opcode = op_code - lh->opcode_base;
15499 address += (((op_index + (adj_opcode / lh->line_range))
15500 / lh->maximum_ops_per_instruction)
15501 * lh->minimum_instruction_length);
15502 op_index = ((op_index + (adj_opcode / lh->line_range))
15503 % lh->maximum_ops_per_instruction);
15504 line += lh->line_base + (adj_opcode % lh->line_range);
15505 if (lh->num_file_names < file || file == 0)
15506 dwarf2_debug_line_missing_file_complaint ();
15507 /* For now we ignore lines not starting on an
15508 instruction boundary. */
15509 else if (op_index == 0)
15510 {
15511 lh->file_names[file - 1].included_p = 1;
15512 if (!decode_for_pst_p && is_stmt)
15513 {
15514 if (last_subfile != current_subfile)
15515 {
15516 addr = gdbarch_addr_bits_remove (gdbarch, address);
15517 if (last_subfile)
15518 (*p_record_line) (last_subfile, 0, addr);
15519 last_subfile = current_subfile;
15520 }
15521 /* Append row to matrix using current values. */
15522 addr = gdbarch_addr_bits_remove (gdbarch, address);
15523 (*p_record_line) (current_subfile, line, addr);
15524 }
15525 }
15526 basic_block = 0;
15527 }
15528 else switch (op_code)
15529 {
15530 case DW_LNS_extended_op:
15531 extended_len = read_unsigned_leb128 (abfd, line_ptr,
15532 &bytes_read);
15533 line_ptr += bytes_read;
15534 extended_end = line_ptr + extended_len;
15535 extended_op = read_1_byte (abfd, line_ptr);
15536 line_ptr += 1;
15537 switch (extended_op)
15538 {
15539 case DW_LNE_end_sequence:
15540 p_record_line = record_line;
15541 end_sequence = 1;
15542 break;
15543 case DW_LNE_set_address:
15544 address = read_address (abfd, line_ptr, cu, &bytes_read);
15545
15546 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
15547 {
15548 /* This line table is for a function which has been
15549 GCd by the linker. Ignore it. PR gdb/12528 */
15550
15551 long line_offset
15552 = line_ptr - get_debug_line_section (cu)->buffer;
15553
15554 complaint (&symfile_complaints,
15555 _(".debug_line address at offset 0x%lx is 0 "
15556 "[in module %s]"),
15557 line_offset, objfile->name);
15558 p_record_line = noop_record_line;
15559 }
15560
15561 op_index = 0;
15562 line_ptr += bytes_read;
15563 address += baseaddr;
15564 break;
15565 case DW_LNE_define_file:
15566 {
15567 char *cur_file;
15568 unsigned int dir_index, mod_time, length;
15569
15570 cur_file = read_direct_string (abfd, line_ptr,
15571 &bytes_read);
15572 line_ptr += bytes_read;
15573 dir_index =
15574 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15575 line_ptr += bytes_read;
15576 mod_time =
15577 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15578 line_ptr += bytes_read;
15579 length =
15580 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15581 line_ptr += bytes_read;
15582 add_file_name (lh, cur_file, dir_index, mod_time, length);
15583 }
15584 break;
15585 case DW_LNE_set_discriminator:
15586 /* The discriminator is not interesting to the debugger;
15587 just ignore it. */
15588 line_ptr = extended_end;
15589 break;
15590 default:
15591 complaint (&symfile_complaints,
15592 _("mangled .debug_line section"));
15593 return;
15594 }
15595 /* Make sure that we parsed the extended op correctly. If e.g.
15596 we expected a different address size than the producer used,
15597 we may have read the wrong number of bytes. */
15598 if (line_ptr != extended_end)
15599 {
15600 complaint (&symfile_complaints,
15601 _("mangled .debug_line section"));
15602 return;
15603 }
15604 break;
15605 case DW_LNS_copy:
15606 if (lh->num_file_names < file || file == 0)
15607 dwarf2_debug_line_missing_file_complaint ();
15608 else
15609 {
15610 lh->file_names[file - 1].included_p = 1;
15611 if (!decode_for_pst_p && is_stmt)
15612 {
15613 if (last_subfile != current_subfile)
15614 {
15615 addr = gdbarch_addr_bits_remove (gdbarch, address);
15616 if (last_subfile)
15617 (*p_record_line) (last_subfile, 0, addr);
15618 last_subfile = current_subfile;
15619 }
15620 addr = gdbarch_addr_bits_remove (gdbarch, address);
15621 (*p_record_line) (current_subfile, line, addr);
15622 }
15623 }
15624 basic_block = 0;
15625 break;
15626 case DW_LNS_advance_pc:
15627 {
15628 CORE_ADDR adjust
15629 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15630
15631 address += (((op_index + adjust)
15632 / lh->maximum_ops_per_instruction)
15633 * lh->minimum_instruction_length);
15634 op_index = ((op_index + adjust)
15635 % lh->maximum_ops_per_instruction);
15636 line_ptr += bytes_read;
15637 }
15638 break;
15639 case DW_LNS_advance_line:
15640 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
15641 line_ptr += bytes_read;
15642 break;
15643 case DW_LNS_set_file:
15644 {
15645 /* The arrays lh->include_dirs and lh->file_names are
15646 0-based, but the directory and file name numbers in
15647 the statement program are 1-based. */
15648 struct file_entry *fe;
15649 char *dir = NULL;
15650
15651 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15652 line_ptr += bytes_read;
15653 if (lh->num_file_names < file || file == 0)
15654 dwarf2_debug_line_missing_file_complaint ();
15655 else
15656 {
15657 fe = &lh->file_names[file - 1];
15658 if (fe->dir_index)
15659 dir = lh->include_dirs[fe->dir_index - 1];
15660 if (!decode_for_pst_p)
15661 {
15662 last_subfile = current_subfile;
15663 dwarf2_start_subfile (fe->name, dir, comp_dir);
15664 }
15665 }
15666 }
15667 break;
15668 case DW_LNS_set_column:
15669 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15670 line_ptr += bytes_read;
15671 break;
15672 case DW_LNS_negate_stmt:
15673 is_stmt = (!is_stmt);
15674 break;
15675 case DW_LNS_set_basic_block:
15676 basic_block = 1;
15677 break;
15678 /* Add to the address register of the state machine the
15679 address increment value corresponding to special opcode
15680 255. I.e., this value is scaled by the minimum
15681 instruction length since special opcode 255 would have
15682 scaled the increment. */
15683 case DW_LNS_const_add_pc:
15684 {
15685 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
15686
15687 address += (((op_index + adjust)
15688 / lh->maximum_ops_per_instruction)
15689 * lh->minimum_instruction_length);
15690 op_index = ((op_index + adjust)
15691 % lh->maximum_ops_per_instruction);
15692 }
15693 break;
15694 case DW_LNS_fixed_advance_pc:
15695 address += read_2_bytes (abfd, line_ptr);
15696 op_index = 0;
15697 line_ptr += 2;
15698 break;
15699 default:
15700 {
15701 /* Unknown standard opcode, ignore it. */
15702 int i;
15703
15704 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
15705 {
15706 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15707 line_ptr += bytes_read;
15708 }
15709 }
15710 }
15711 }
15712 if (lh->num_file_names < file || file == 0)
15713 dwarf2_debug_line_missing_file_complaint ();
15714 else
15715 {
15716 lh->file_names[file - 1].included_p = 1;
15717 if (!decode_for_pst_p)
15718 {
15719 addr = gdbarch_addr_bits_remove (gdbarch, address);
15720 (*p_record_line) (current_subfile, 0, addr);
15721 }
15722 }
15723 }
15724 }
15725
15726 /* Decode the Line Number Program (LNP) for the given line_header
15727 structure and CU. The actual information extracted and the type
15728 of structures created from the LNP depends on the value of PST.
15729
15730 1. If PST is NULL, then this procedure uses the data from the program
15731 to create all necessary symbol tables, and their linetables.
15732
15733 2. If PST is not NULL, this procedure reads the program to determine
15734 the list of files included by the unit represented by PST, and
15735 builds all the associated partial symbol tables.
15736
15737 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15738 It is used for relative paths in the line table.
15739 NOTE: When processing partial symtabs (pst != NULL),
15740 comp_dir == pst->dirname.
15741
15742 NOTE: It is important that psymtabs have the same file name (via strcmp)
15743 as the corresponding symtab. Since COMP_DIR is not used in the name of the
15744 symtab we don't use it in the name of the psymtabs we create.
15745 E.g. expand_line_sal requires this when finding psymtabs to expand.
15746 A good testcase for this is mb-inline.exp. */
15747
15748 static void
15749 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
15750 struct dwarf2_cu *cu, struct partial_symtab *pst,
15751 int want_line_info)
15752 {
15753 struct objfile *objfile = cu->objfile;
15754 const int decode_for_pst_p = (pst != NULL);
15755 struct subfile *first_subfile = current_subfile;
15756
15757 if (want_line_info)
15758 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
15759
15760 if (decode_for_pst_p)
15761 {
15762 int file_index;
15763
15764 /* Now that we're done scanning the Line Header Program, we can
15765 create the psymtab of each included file. */
15766 for (file_index = 0; file_index < lh->num_file_names; file_index++)
15767 if (lh->file_names[file_index].included_p == 1)
15768 {
15769 char *include_name =
15770 psymtab_include_file_name (lh, file_index, pst, comp_dir);
15771 if (include_name != NULL)
15772 dwarf2_create_include_psymtab (include_name, pst, objfile);
15773 }
15774 }
15775 else
15776 {
15777 /* Make sure a symtab is created for every file, even files
15778 which contain only variables (i.e. no code with associated
15779 line numbers). */
15780 int i;
15781
15782 for (i = 0; i < lh->num_file_names; i++)
15783 {
15784 char *dir = NULL;
15785 struct file_entry *fe;
15786
15787 fe = &lh->file_names[i];
15788 if (fe->dir_index)
15789 dir = lh->include_dirs[fe->dir_index - 1];
15790 dwarf2_start_subfile (fe->name, dir, comp_dir);
15791
15792 /* Skip the main file; we don't need it, and it must be
15793 allocated last, so that it will show up before the
15794 non-primary symtabs in the objfile's symtab list. */
15795 if (current_subfile == first_subfile)
15796 continue;
15797
15798 if (current_subfile->symtab == NULL)
15799 current_subfile->symtab = allocate_symtab (current_subfile->name,
15800 objfile);
15801 fe->symtab = current_subfile->symtab;
15802 }
15803 }
15804 }
15805
15806 /* Start a subfile for DWARF. FILENAME is the name of the file and
15807 DIRNAME the name of the source directory which contains FILENAME
15808 or NULL if not known. COMP_DIR is the compilation directory for the
15809 linetable's compilation unit or NULL if not known.
15810 This routine tries to keep line numbers from identical absolute and
15811 relative file names in a common subfile.
15812
15813 Using the `list' example from the GDB testsuite, which resides in
15814 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
15815 of /srcdir/list0.c yields the following debugging information for list0.c:
15816
15817 DW_AT_name: /srcdir/list0.c
15818 DW_AT_comp_dir: /compdir
15819 files.files[0].name: list0.h
15820 files.files[0].dir: /srcdir
15821 files.files[1].name: list0.c
15822 files.files[1].dir: /srcdir
15823
15824 The line number information for list0.c has to end up in a single
15825 subfile, so that `break /srcdir/list0.c:1' works as expected.
15826 start_subfile will ensure that this happens provided that we pass the
15827 concatenation of files.files[1].dir and files.files[1].name as the
15828 subfile's name. */
15829
15830 static void
15831 dwarf2_start_subfile (char *filename, const char *dirname,
15832 const char *comp_dir)
15833 {
15834 char *fullname;
15835
15836 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
15837 `start_symtab' will always pass the contents of DW_AT_comp_dir as
15838 second argument to start_subfile. To be consistent, we do the
15839 same here. In order not to lose the line information directory,
15840 we concatenate it to the filename when it makes sense.
15841 Note that the Dwarf3 standard says (speaking of filenames in line
15842 information): ``The directory index is ignored for file names
15843 that represent full path names''. Thus ignoring dirname in the
15844 `else' branch below isn't an issue. */
15845
15846 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
15847 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
15848 else
15849 fullname = filename;
15850
15851 start_subfile (fullname, comp_dir);
15852
15853 if (fullname != filename)
15854 xfree (fullname);
15855 }
15856
15857 /* Start a symtab for DWARF.
15858 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
15859
15860 static void
15861 dwarf2_start_symtab (struct dwarf2_cu *cu,
15862 const char *name, const char *comp_dir, CORE_ADDR low_pc)
15863 {
15864 start_symtab (name, comp_dir, low_pc);
15865 record_debugformat ("DWARF 2");
15866 record_producer (cu->producer);
15867
15868 /* We assume that we're processing GCC output. */
15869 processing_gcc_compilation = 2;
15870
15871 cu->processing_has_namespace_info = 0;
15872 }
15873
15874 static void
15875 var_decode_location (struct attribute *attr, struct symbol *sym,
15876 struct dwarf2_cu *cu)
15877 {
15878 struct objfile *objfile = cu->objfile;
15879 struct comp_unit_head *cu_header = &cu->header;
15880
15881 /* NOTE drow/2003-01-30: There used to be a comment and some special
15882 code here to turn a symbol with DW_AT_external and a
15883 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
15884 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
15885 with some versions of binutils) where shared libraries could have
15886 relocations against symbols in their debug information - the
15887 minimal symbol would have the right address, but the debug info
15888 would not. It's no longer necessary, because we will explicitly
15889 apply relocations when we read in the debug information now. */
15890
15891 /* A DW_AT_location attribute with no contents indicates that a
15892 variable has been optimized away. */
15893 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
15894 {
15895 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
15896 return;
15897 }
15898
15899 /* Handle one degenerate form of location expression specially, to
15900 preserve GDB's previous behavior when section offsets are
15901 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
15902 then mark this symbol as LOC_STATIC. */
15903
15904 if (attr_form_is_block (attr)
15905 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
15906 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
15907 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
15908 && (DW_BLOCK (attr)->size
15909 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
15910 {
15911 unsigned int dummy;
15912
15913 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
15914 SYMBOL_VALUE_ADDRESS (sym) =
15915 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
15916 else
15917 SYMBOL_VALUE_ADDRESS (sym) =
15918 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
15919 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
15920 fixup_symbol_section (sym, objfile);
15921 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
15922 SYMBOL_SECTION (sym));
15923 return;
15924 }
15925
15926 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
15927 expression evaluator, and use LOC_COMPUTED only when necessary
15928 (i.e. when the value of a register or memory location is
15929 referenced, or a thread-local block, etc.). Then again, it might
15930 not be worthwhile. I'm assuming that it isn't unless performance
15931 or memory numbers show me otherwise. */
15932
15933 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
15934
15935 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
15936 cu->has_loclist = 1;
15937 }
15938
15939 /* Given a pointer to a DWARF information entry, figure out if we need
15940 to make a symbol table entry for it, and if so, create a new entry
15941 and return a pointer to it.
15942 If TYPE is NULL, determine symbol type from the die, otherwise
15943 used the passed type.
15944 If SPACE is not NULL, use it to hold the new symbol. If it is
15945 NULL, allocate a new symbol on the objfile's obstack. */
15946
15947 static struct symbol *
15948 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
15949 struct symbol *space)
15950 {
15951 struct objfile *objfile = cu->objfile;
15952 struct symbol *sym = NULL;
15953 const char *name;
15954 struct attribute *attr = NULL;
15955 struct attribute *attr2 = NULL;
15956 CORE_ADDR baseaddr;
15957 struct pending **list_to_add = NULL;
15958
15959 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
15960
15961 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15962
15963 name = dwarf2_name (die, cu);
15964 if (name)
15965 {
15966 const char *linkagename;
15967 int suppress_add = 0;
15968
15969 if (space)
15970 sym = space;
15971 else
15972 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
15973 OBJSTAT (objfile, n_syms++);
15974
15975 /* Cache this symbol's name and the name's demangled form (if any). */
15976 SYMBOL_SET_LANGUAGE (sym, cu->language);
15977 linkagename = dwarf2_physname (name, die, cu);
15978 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
15979
15980 /* Fortran does not have mangling standard and the mangling does differ
15981 between gfortran, iFort etc. */
15982 if (cu->language == language_fortran
15983 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
15984 symbol_set_demangled_name (&(sym->ginfo),
15985 dwarf2_full_name (name, die, cu),
15986 NULL);
15987
15988 /* Default assumptions.
15989 Use the passed type or decode it from the die. */
15990 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
15991 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
15992 if (type != NULL)
15993 SYMBOL_TYPE (sym) = type;
15994 else
15995 SYMBOL_TYPE (sym) = die_type (die, cu);
15996 attr = dwarf2_attr (die,
15997 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
15998 cu);
15999 if (attr)
16000 {
16001 SYMBOL_LINE (sym) = DW_UNSND (attr);
16002 }
16003
16004 attr = dwarf2_attr (die,
16005 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
16006 cu);
16007 if (attr)
16008 {
16009 int file_index = DW_UNSND (attr);
16010
16011 if (cu->line_header == NULL
16012 || file_index > cu->line_header->num_file_names)
16013 complaint (&symfile_complaints,
16014 _("file index out of range"));
16015 else if (file_index > 0)
16016 {
16017 struct file_entry *fe;
16018
16019 fe = &cu->line_header->file_names[file_index - 1];
16020 SYMBOL_SYMTAB (sym) = fe->symtab;
16021 }
16022 }
16023
16024 switch (die->tag)
16025 {
16026 case DW_TAG_label:
16027 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
16028 if (attr)
16029 {
16030 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
16031 }
16032 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
16033 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
16034 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
16035 add_symbol_to_list (sym, cu->list_in_scope);
16036 break;
16037 case DW_TAG_subprogram:
16038 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
16039 finish_block. */
16040 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
16041 attr2 = dwarf2_attr (die, DW_AT_external, cu);
16042 if ((attr2 && (DW_UNSND (attr2) != 0))
16043 || cu->language == language_ada)
16044 {
16045 /* Subprograms marked external are stored as a global symbol.
16046 Ada subprograms, whether marked external or not, are always
16047 stored as a global symbol, because we want to be able to
16048 access them globally. For instance, we want to be able
16049 to break on a nested subprogram without having to
16050 specify the context. */
16051 list_to_add = &global_symbols;
16052 }
16053 else
16054 {
16055 list_to_add = cu->list_in_scope;
16056 }
16057 break;
16058 case DW_TAG_inlined_subroutine:
16059 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
16060 finish_block. */
16061 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
16062 SYMBOL_INLINED (sym) = 1;
16063 list_to_add = cu->list_in_scope;
16064 break;
16065 case DW_TAG_template_value_param:
16066 suppress_add = 1;
16067 /* Fall through. */
16068 case DW_TAG_constant:
16069 case DW_TAG_variable:
16070 case DW_TAG_member:
16071 /* Compilation with minimal debug info may result in
16072 variables with missing type entries. Change the
16073 misleading `void' type to something sensible. */
16074 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
16075 SYMBOL_TYPE (sym)
16076 = objfile_type (objfile)->nodebug_data_symbol;
16077
16078 attr = dwarf2_attr (die, DW_AT_const_value, cu);
16079 /* In the case of DW_TAG_member, we should only be called for
16080 static const members. */
16081 if (die->tag == DW_TAG_member)
16082 {
16083 /* dwarf2_add_field uses die_is_declaration,
16084 so we do the same. */
16085 gdb_assert (die_is_declaration (die, cu));
16086 gdb_assert (attr);
16087 }
16088 if (attr)
16089 {
16090 dwarf2_const_value (attr, sym, cu);
16091 attr2 = dwarf2_attr (die, DW_AT_external, cu);
16092 if (!suppress_add)
16093 {
16094 if (attr2 && (DW_UNSND (attr2) != 0))
16095 list_to_add = &global_symbols;
16096 else
16097 list_to_add = cu->list_in_scope;
16098 }
16099 break;
16100 }
16101 attr = dwarf2_attr (die, DW_AT_location, cu);
16102 if (attr)
16103 {
16104 var_decode_location (attr, sym, cu);
16105 attr2 = dwarf2_attr (die, DW_AT_external, cu);
16106
16107 /* Fortran explicitly imports any global symbols to the local
16108 scope by DW_TAG_common_block. */
16109 if (cu->language == language_fortran && die->parent
16110 && die->parent->tag == DW_TAG_common_block)
16111 attr2 = NULL;
16112
16113 if (SYMBOL_CLASS (sym) == LOC_STATIC
16114 && SYMBOL_VALUE_ADDRESS (sym) == 0
16115 && !dwarf2_per_objfile->has_section_at_zero)
16116 {
16117 /* When a static variable is eliminated by the linker,
16118 the corresponding debug information is not stripped
16119 out, but the variable address is set to null;
16120 do not add such variables into symbol table. */
16121 }
16122 else if (attr2 && (DW_UNSND (attr2) != 0))
16123 {
16124 /* Workaround gfortran PR debug/40040 - it uses
16125 DW_AT_location for variables in -fPIC libraries which may
16126 get overriden by other libraries/executable and get
16127 a different address. Resolve it by the minimal symbol
16128 which may come from inferior's executable using copy
16129 relocation. Make this workaround only for gfortran as for
16130 other compilers GDB cannot guess the minimal symbol
16131 Fortran mangling kind. */
16132 if (cu->language == language_fortran && die->parent
16133 && die->parent->tag == DW_TAG_module
16134 && cu->producer
16135 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
16136 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
16137
16138 /* A variable with DW_AT_external is never static,
16139 but it may be block-scoped. */
16140 list_to_add = (cu->list_in_scope == &file_symbols
16141 ? &global_symbols : cu->list_in_scope);
16142 }
16143 else
16144 list_to_add = cu->list_in_scope;
16145 }
16146 else
16147 {
16148 /* We do not know the address of this symbol.
16149 If it is an external symbol and we have type information
16150 for it, enter the symbol as a LOC_UNRESOLVED symbol.
16151 The address of the variable will then be determined from
16152 the minimal symbol table whenever the variable is
16153 referenced. */
16154 attr2 = dwarf2_attr (die, DW_AT_external, cu);
16155
16156 /* Fortran explicitly imports any global symbols to the local
16157 scope by DW_TAG_common_block. */
16158 if (cu->language == language_fortran && die->parent
16159 && die->parent->tag == DW_TAG_common_block)
16160 {
16161 /* SYMBOL_CLASS doesn't matter here because
16162 read_common_block is going to reset it. */
16163 if (!suppress_add)
16164 list_to_add = cu->list_in_scope;
16165 }
16166 else if (attr2 && (DW_UNSND (attr2) != 0)
16167 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
16168 {
16169 /* A variable with DW_AT_external is never static, but it
16170 may be block-scoped. */
16171 list_to_add = (cu->list_in_scope == &file_symbols
16172 ? &global_symbols : cu->list_in_scope);
16173
16174 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
16175 }
16176 else if (!die_is_declaration (die, cu))
16177 {
16178 /* Use the default LOC_OPTIMIZED_OUT class. */
16179 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
16180 if (!suppress_add)
16181 list_to_add = cu->list_in_scope;
16182 }
16183 }
16184 break;
16185 case DW_TAG_formal_parameter:
16186 /* If we are inside a function, mark this as an argument. If
16187 not, we might be looking at an argument to an inlined function
16188 when we do not have enough information to show inlined frames;
16189 pretend it's a local variable in that case so that the user can
16190 still see it. */
16191 if (context_stack_depth > 0
16192 && context_stack[context_stack_depth - 1].name != NULL)
16193 SYMBOL_IS_ARGUMENT (sym) = 1;
16194 attr = dwarf2_attr (die, DW_AT_location, cu);
16195 if (attr)
16196 {
16197 var_decode_location (attr, sym, cu);
16198 }
16199 attr = dwarf2_attr (die, DW_AT_const_value, cu);
16200 if (attr)
16201 {
16202 dwarf2_const_value (attr, sym, cu);
16203 }
16204
16205 list_to_add = cu->list_in_scope;
16206 break;
16207 case DW_TAG_unspecified_parameters:
16208 /* From varargs functions; gdb doesn't seem to have any
16209 interest in this information, so just ignore it for now.
16210 (FIXME?) */
16211 break;
16212 case DW_TAG_template_type_param:
16213 suppress_add = 1;
16214 /* Fall through. */
16215 case DW_TAG_class_type:
16216 case DW_TAG_interface_type:
16217 case DW_TAG_structure_type:
16218 case DW_TAG_union_type:
16219 case DW_TAG_set_type:
16220 case DW_TAG_enumeration_type:
16221 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16222 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
16223
16224 {
16225 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
16226 really ever be static objects: otherwise, if you try
16227 to, say, break of a class's method and you're in a file
16228 which doesn't mention that class, it won't work unless
16229 the check for all static symbols in lookup_symbol_aux
16230 saves you. See the OtherFileClass tests in
16231 gdb.c++/namespace.exp. */
16232
16233 if (!suppress_add)
16234 {
16235 list_to_add = (cu->list_in_scope == &file_symbols
16236 && (cu->language == language_cplus
16237 || cu->language == language_java)
16238 ? &global_symbols : cu->list_in_scope);
16239
16240 /* The semantics of C++ state that "struct foo {
16241 ... }" also defines a typedef for "foo". A Java
16242 class declaration also defines a typedef for the
16243 class. */
16244 if (cu->language == language_cplus
16245 || cu->language == language_java
16246 || cu->language == language_ada)
16247 {
16248 /* The symbol's name is already allocated along
16249 with this objfile, so we don't need to
16250 duplicate it for the type. */
16251 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
16252 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
16253 }
16254 }
16255 }
16256 break;
16257 case DW_TAG_typedef:
16258 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16259 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
16260 list_to_add = cu->list_in_scope;
16261 break;
16262 case DW_TAG_base_type:
16263 case DW_TAG_subrange_type:
16264 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16265 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
16266 list_to_add = cu->list_in_scope;
16267 break;
16268 case DW_TAG_enumerator:
16269 attr = dwarf2_attr (die, DW_AT_const_value, cu);
16270 if (attr)
16271 {
16272 dwarf2_const_value (attr, sym, cu);
16273 }
16274 {
16275 /* NOTE: carlton/2003-11-10: See comment above in the
16276 DW_TAG_class_type, etc. block. */
16277
16278 list_to_add = (cu->list_in_scope == &file_symbols
16279 && (cu->language == language_cplus
16280 || cu->language == language_java)
16281 ? &global_symbols : cu->list_in_scope);
16282 }
16283 break;
16284 case DW_TAG_namespace:
16285 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16286 list_to_add = &global_symbols;
16287 break;
16288 case DW_TAG_common_block:
16289 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
16290 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
16291 add_symbol_to_list (sym, cu->list_in_scope);
16292 break;
16293 default:
16294 /* Not a tag we recognize. Hopefully we aren't processing
16295 trash data, but since we must specifically ignore things
16296 we don't recognize, there is nothing else we should do at
16297 this point. */
16298 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
16299 dwarf_tag_name (die->tag));
16300 break;
16301 }
16302
16303 if (suppress_add)
16304 {
16305 sym->hash_next = objfile->template_symbols;
16306 objfile->template_symbols = sym;
16307 list_to_add = NULL;
16308 }
16309
16310 if (list_to_add != NULL)
16311 add_symbol_to_list (sym, list_to_add);
16312
16313 /* For the benefit of old versions of GCC, check for anonymous
16314 namespaces based on the demangled name. */
16315 if (!cu->processing_has_namespace_info
16316 && cu->language == language_cplus)
16317 cp_scan_for_anonymous_namespaces (sym, objfile);
16318 }
16319 return (sym);
16320 }
16321
16322 /* A wrapper for new_symbol_full that always allocates a new symbol. */
16323
16324 static struct symbol *
16325 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16326 {
16327 return new_symbol_full (die, type, cu, NULL);
16328 }
16329
16330 /* Given an attr with a DW_FORM_dataN value in host byte order,
16331 zero-extend it as appropriate for the symbol's type. The DWARF
16332 standard (v4) is not entirely clear about the meaning of using
16333 DW_FORM_dataN for a constant with a signed type, where the type is
16334 wider than the data. The conclusion of a discussion on the DWARF
16335 list was that this is unspecified. We choose to always zero-extend
16336 because that is the interpretation long in use by GCC. */
16337
16338 static gdb_byte *
16339 dwarf2_const_value_data (struct attribute *attr, struct type *type,
16340 const char *name, struct obstack *obstack,
16341 struct dwarf2_cu *cu, LONGEST *value, int bits)
16342 {
16343 struct objfile *objfile = cu->objfile;
16344 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
16345 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
16346 LONGEST l = DW_UNSND (attr);
16347
16348 if (bits < sizeof (*value) * 8)
16349 {
16350 l &= ((LONGEST) 1 << bits) - 1;
16351 *value = l;
16352 }
16353 else if (bits == sizeof (*value) * 8)
16354 *value = l;
16355 else
16356 {
16357 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
16358 store_unsigned_integer (bytes, bits / 8, byte_order, l);
16359 return bytes;
16360 }
16361
16362 return NULL;
16363 }
16364
16365 /* Read a constant value from an attribute. Either set *VALUE, or if
16366 the value does not fit in *VALUE, set *BYTES - either already
16367 allocated on the objfile obstack, or newly allocated on OBSTACK,
16368 or, set *BATON, if we translated the constant to a location
16369 expression. */
16370
16371 static void
16372 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
16373 const char *name, struct obstack *obstack,
16374 struct dwarf2_cu *cu,
16375 LONGEST *value, gdb_byte **bytes,
16376 struct dwarf2_locexpr_baton **baton)
16377 {
16378 struct objfile *objfile = cu->objfile;
16379 struct comp_unit_head *cu_header = &cu->header;
16380 struct dwarf_block *blk;
16381 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
16382 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
16383
16384 *value = 0;
16385 *bytes = NULL;
16386 *baton = NULL;
16387
16388 switch (attr->form)
16389 {
16390 case DW_FORM_addr:
16391 case DW_FORM_GNU_addr_index:
16392 {
16393 gdb_byte *data;
16394
16395 if (TYPE_LENGTH (type) != cu_header->addr_size)
16396 dwarf2_const_value_length_mismatch_complaint (name,
16397 cu_header->addr_size,
16398 TYPE_LENGTH (type));
16399 /* Symbols of this form are reasonably rare, so we just
16400 piggyback on the existing location code rather than writing
16401 a new implementation of symbol_computed_ops. */
16402 *baton = obstack_alloc (&objfile->objfile_obstack,
16403 sizeof (struct dwarf2_locexpr_baton));
16404 (*baton)->per_cu = cu->per_cu;
16405 gdb_assert ((*baton)->per_cu);
16406
16407 (*baton)->size = 2 + cu_header->addr_size;
16408 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
16409 (*baton)->data = data;
16410
16411 data[0] = DW_OP_addr;
16412 store_unsigned_integer (&data[1], cu_header->addr_size,
16413 byte_order, DW_ADDR (attr));
16414 data[cu_header->addr_size + 1] = DW_OP_stack_value;
16415 }
16416 break;
16417 case DW_FORM_string:
16418 case DW_FORM_strp:
16419 case DW_FORM_GNU_str_index:
16420 case DW_FORM_GNU_strp_alt:
16421 /* DW_STRING is already allocated on the objfile obstack, point
16422 directly to it. */
16423 *bytes = (gdb_byte *) DW_STRING (attr);
16424 break;
16425 case DW_FORM_block1:
16426 case DW_FORM_block2:
16427 case DW_FORM_block4:
16428 case DW_FORM_block:
16429 case DW_FORM_exprloc:
16430 blk = DW_BLOCK (attr);
16431 if (TYPE_LENGTH (type) != blk->size)
16432 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
16433 TYPE_LENGTH (type));
16434 *bytes = blk->data;
16435 break;
16436
16437 /* The DW_AT_const_value attributes are supposed to carry the
16438 symbol's value "represented as it would be on the target
16439 architecture." By the time we get here, it's already been
16440 converted to host endianness, so we just need to sign- or
16441 zero-extend it as appropriate. */
16442 case DW_FORM_data1:
16443 *bytes = dwarf2_const_value_data (attr, type, name,
16444 obstack, cu, value, 8);
16445 break;
16446 case DW_FORM_data2:
16447 *bytes = dwarf2_const_value_data (attr, type, name,
16448 obstack, cu, value, 16);
16449 break;
16450 case DW_FORM_data4:
16451 *bytes = dwarf2_const_value_data (attr, type, name,
16452 obstack, cu, value, 32);
16453 break;
16454 case DW_FORM_data8:
16455 *bytes = dwarf2_const_value_data (attr, type, name,
16456 obstack, cu, value, 64);
16457 break;
16458
16459 case DW_FORM_sdata:
16460 *value = DW_SND (attr);
16461 break;
16462
16463 case DW_FORM_udata:
16464 *value = DW_UNSND (attr);
16465 break;
16466
16467 default:
16468 complaint (&symfile_complaints,
16469 _("unsupported const value attribute form: '%s'"),
16470 dwarf_form_name (attr->form));
16471 *value = 0;
16472 break;
16473 }
16474 }
16475
16476
16477 /* Copy constant value from an attribute to a symbol. */
16478
16479 static void
16480 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
16481 struct dwarf2_cu *cu)
16482 {
16483 struct objfile *objfile = cu->objfile;
16484 struct comp_unit_head *cu_header = &cu->header;
16485 LONGEST value;
16486 gdb_byte *bytes;
16487 struct dwarf2_locexpr_baton *baton;
16488
16489 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
16490 SYMBOL_PRINT_NAME (sym),
16491 &objfile->objfile_obstack, cu,
16492 &value, &bytes, &baton);
16493
16494 if (baton != NULL)
16495 {
16496 SYMBOL_LOCATION_BATON (sym) = baton;
16497 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16498 }
16499 else if (bytes != NULL)
16500 {
16501 SYMBOL_VALUE_BYTES (sym) = bytes;
16502 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
16503 }
16504 else
16505 {
16506 SYMBOL_VALUE (sym) = value;
16507 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
16508 }
16509 }
16510
16511 /* Return the type of the die in question using its DW_AT_type attribute. */
16512
16513 static struct type *
16514 die_type (struct die_info *die, struct dwarf2_cu *cu)
16515 {
16516 struct attribute *type_attr;
16517
16518 type_attr = dwarf2_attr (die, DW_AT_type, cu);
16519 if (!type_attr)
16520 {
16521 /* A missing DW_AT_type represents a void type. */
16522 return objfile_type (cu->objfile)->builtin_void;
16523 }
16524
16525 return lookup_die_type (die, type_attr, cu);
16526 }
16527
16528 /* True iff CU's producer generates GNAT Ada auxiliary information
16529 that allows to find parallel types through that information instead
16530 of having to do expensive parallel lookups by type name. */
16531
16532 static int
16533 need_gnat_info (struct dwarf2_cu *cu)
16534 {
16535 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
16536 of GNAT produces this auxiliary information, without any indication
16537 that it is produced. Part of enhancing the FSF version of GNAT
16538 to produce that information will be to put in place an indicator
16539 that we can use in order to determine whether the descriptive type
16540 info is available or not. One suggestion that has been made is
16541 to use a new attribute, attached to the CU die. For now, assume
16542 that the descriptive type info is not available. */
16543 return 0;
16544 }
16545
16546 /* Return the auxiliary type of the die in question using its
16547 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
16548 attribute is not present. */
16549
16550 static struct type *
16551 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
16552 {
16553 struct attribute *type_attr;
16554
16555 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
16556 if (!type_attr)
16557 return NULL;
16558
16559 return lookup_die_type (die, type_attr, cu);
16560 }
16561
16562 /* If DIE has a descriptive_type attribute, then set the TYPE's
16563 descriptive type accordingly. */
16564
16565 static void
16566 set_descriptive_type (struct type *type, struct die_info *die,
16567 struct dwarf2_cu *cu)
16568 {
16569 struct type *descriptive_type = die_descriptive_type (die, cu);
16570
16571 if (descriptive_type)
16572 {
16573 ALLOCATE_GNAT_AUX_TYPE (type);
16574 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
16575 }
16576 }
16577
16578 /* Return the containing type of the die in question using its
16579 DW_AT_containing_type attribute. */
16580
16581 static struct type *
16582 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
16583 {
16584 struct attribute *type_attr;
16585
16586 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
16587 if (!type_attr)
16588 error (_("Dwarf Error: Problem turning containing type into gdb type "
16589 "[in module %s]"), cu->objfile->name);
16590
16591 return lookup_die_type (die, type_attr, cu);
16592 }
16593
16594 /* Look up the type of DIE in CU using its type attribute ATTR.
16595 If there is no type substitute an error marker. */
16596
16597 static struct type *
16598 lookup_die_type (struct die_info *die, struct attribute *attr,
16599 struct dwarf2_cu *cu)
16600 {
16601 struct objfile *objfile = cu->objfile;
16602 struct type *this_type;
16603
16604 /* First see if we have it cached. */
16605
16606 if (attr->form == DW_FORM_GNU_ref_alt)
16607 {
16608 struct dwarf2_per_cu_data *per_cu;
16609 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16610
16611 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
16612 this_type = get_die_type_at_offset (offset, per_cu);
16613 }
16614 else if (is_ref_attr (attr))
16615 {
16616 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16617
16618 this_type = get_die_type_at_offset (offset, cu->per_cu);
16619 }
16620 else if (attr->form == DW_FORM_ref_sig8)
16621 {
16622 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
16623
16624 /* sig_type will be NULL if the signatured type is missing from
16625 the debug info. */
16626 if (sig_type == NULL)
16627 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
16628 "at 0x%x [in module %s]"),
16629 die->offset.sect_off, objfile->name);
16630
16631 gdb_assert (sig_type->per_cu.is_debug_types);
16632 /* If we haven't filled in type_offset_in_section yet, then we
16633 haven't read the type in yet. */
16634 this_type = NULL;
16635 if (sig_type->type_offset_in_section.sect_off != 0)
16636 {
16637 this_type =
16638 get_die_type_at_offset (sig_type->type_offset_in_section,
16639 &sig_type->per_cu);
16640 }
16641 }
16642 else
16643 {
16644 dump_die_for_error (die);
16645 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
16646 dwarf_attr_name (attr->name), objfile->name);
16647 }
16648
16649 /* If not cached we need to read it in. */
16650
16651 if (this_type == NULL)
16652 {
16653 struct die_info *type_die;
16654 struct dwarf2_cu *type_cu = cu;
16655
16656 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
16657 /* If we found the type now, it's probably because the type came
16658 from an inter-CU reference and the type's CU got expanded before
16659 ours. */
16660 this_type = get_die_type (type_die, type_cu);
16661 if (this_type == NULL)
16662 this_type = read_type_die_1 (type_die, type_cu);
16663 }
16664
16665 /* If we still don't have a type use an error marker. */
16666
16667 if (this_type == NULL)
16668 {
16669 char *message, *saved;
16670
16671 /* read_type_die already issued a complaint. */
16672 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
16673 objfile->name,
16674 cu->header.offset.sect_off,
16675 die->offset.sect_off);
16676 saved = obstack_copy0 (&objfile->objfile_obstack,
16677 message, strlen (message));
16678 xfree (message);
16679
16680 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
16681 }
16682
16683 return this_type;
16684 }
16685
16686 /* Return the type in DIE, CU.
16687 Returns NULL for invalid types.
16688
16689 This first does a lookup in the appropriate type_hash table,
16690 and only reads the die in if necessary.
16691
16692 NOTE: This can be called when reading in partial or full symbols. */
16693
16694 static struct type *
16695 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
16696 {
16697 struct type *this_type;
16698
16699 this_type = get_die_type (die, cu);
16700 if (this_type)
16701 return this_type;
16702
16703 return read_type_die_1 (die, cu);
16704 }
16705
16706 /* Read the type in DIE, CU.
16707 Returns NULL for invalid types. */
16708
16709 static struct type *
16710 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
16711 {
16712 struct type *this_type = NULL;
16713
16714 switch (die->tag)
16715 {
16716 case DW_TAG_class_type:
16717 case DW_TAG_interface_type:
16718 case DW_TAG_structure_type:
16719 case DW_TAG_union_type:
16720 this_type = read_structure_type (die, cu);
16721 break;
16722 case DW_TAG_enumeration_type:
16723 this_type = read_enumeration_type (die, cu);
16724 break;
16725 case DW_TAG_subprogram:
16726 case DW_TAG_subroutine_type:
16727 case DW_TAG_inlined_subroutine:
16728 this_type = read_subroutine_type (die, cu);
16729 break;
16730 case DW_TAG_array_type:
16731 this_type = read_array_type (die, cu);
16732 break;
16733 case DW_TAG_set_type:
16734 this_type = read_set_type (die, cu);
16735 break;
16736 case DW_TAG_pointer_type:
16737 this_type = read_tag_pointer_type (die, cu);
16738 break;
16739 case DW_TAG_ptr_to_member_type:
16740 this_type = read_tag_ptr_to_member_type (die, cu);
16741 break;
16742 case DW_TAG_reference_type:
16743 this_type = read_tag_reference_type (die, cu);
16744 break;
16745 case DW_TAG_const_type:
16746 this_type = read_tag_const_type (die, cu);
16747 break;
16748 case DW_TAG_volatile_type:
16749 this_type = read_tag_volatile_type (die, cu);
16750 break;
16751 case DW_TAG_restrict_type:
16752 this_type = read_tag_restrict_type (die, cu);
16753 break;
16754 case DW_TAG_string_type:
16755 this_type = read_tag_string_type (die, cu);
16756 break;
16757 case DW_TAG_typedef:
16758 this_type = read_typedef (die, cu);
16759 break;
16760 case DW_TAG_subrange_type:
16761 this_type = read_subrange_type (die, cu);
16762 break;
16763 case DW_TAG_base_type:
16764 this_type = read_base_type (die, cu);
16765 break;
16766 case DW_TAG_unspecified_type:
16767 this_type = read_unspecified_type (die, cu);
16768 break;
16769 case DW_TAG_namespace:
16770 this_type = read_namespace_type (die, cu);
16771 break;
16772 case DW_TAG_module:
16773 this_type = read_module_type (die, cu);
16774 break;
16775 default:
16776 complaint (&symfile_complaints,
16777 _("unexpected tag in read_type_die: '%s'"),
16778 dwarf_tag_name (die->tag));
16779 break;
16780 }
16781
16782 return this_type;
16783 }
16784
16785 /* See if we can figure out if the class lives in a namespace. We do
16786 this by looking for a member function; its demangled name will
16787 contain namespace info, if there is any.
16788 Return the computed name or NULL.
16789 Space for the result is allocated on the objfile's obstack.
16790 This is the full-die version of guess_partial_die_structure_name.
16791 In this case we know DIE has no useful parent. */
16792
16793 static char *
16794 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
16795 {
16796 struct die_info *spec_die;
16797 struct dwarf2_cu *spec_cu;
16798 struct die_info *child;
16799
16800 spec_cu = cu;
16801 spec_die = die_specification (die, &spec_cu);
16802 if (spec_die != NULL)
16803 {
16804 die = spec_die;
16805 cu = spec_cu;
16806 }
16807
16808 for (child = die->child;
16809 child != NULL;
16810 child = child->sibling)
16811 {
16812 if (child->tag == DW_TAG_subprogram)
16813 {
16814 struct attribute *attr;
16815
16816 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
16817 if (attr == NULL)
16818 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
16819 if (attr != NULL)
16820 {
16821 char *actual_name
16822 = language_class_name_from_physname (cu->language_defn,
16823 DW_STRING (attr));
16824 char *name = NULL;
16825
16826 if (actual_name != NULL)
16827 {
16828 const char *die_name = dwarf2_name (die, cu);
16829
16830 if (die_name != NULL
16831 && strcmp (die_name, actual_name) != 0)
16832 {
16833 /* Strip off the class name from the full name.
16834 We want the prefix. */
16835 int die_name_len = strlen (die_name);
16836 int actual_name_len = strlen (actual_name);
16837
16838 /* Test for '::' as a sanity check. */
16839 if (actual_name_len > die_name_len + 2
16840 && actual_name[actual_name_len
16841 - die_name_len - 1] == ':')
16842 name =
16843 obstack_copy0 (&cu->objfile->objfile_obstack,
16844 actual_name,
16845 actual_name_len - die_name_len - 2);
16846 }
16847 }
16848 xfree (actual_name);
16849 return name;
16850 }
16851 }
16852 }
16853
16854 return NULL;
16855 }
16856
16857 /* GCC might emit a nameless typedef that has a linkage name. Determine the
16858 prefix part in such case. See
16859 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16860
16861 static char *
16862 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
16863 {
16864 struct attribute *attr;
16865 char *base;
16866
16867 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
16868 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
16869 return NULL;
16870
16871 attr = dwarf2_attr (die, DW_AT_name, cu);
16872 if (attr != NULL && DW_STRING (attr) != NULL)
16873 return NULL;
16874
16875 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16876 if (attr == NULL)
16877 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16878 if (attr == NULL || DW_STRING (attr) == NULL)
16879 return NULL;
16880
16881 /* dwarf2_name had to be already called. */
16882 gdb_assert (DW_STRING_IS_CANONICAL (attr));
16883
16884 /* Strip the base name, keep any leading namespaces/classes. */
16885 base = strrchr (DW_STRING (attr), ':');
16886 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
16887 return "";
16888
16889 return obstack_copy0 (&cu->objfile->objfile_obstack,
16890 DW_STRING (attr), &base[-1] - DW_STRING (attr));
16891 }
16892
16893 /* Return the name of the namespace/class that DIE is defined within,
16894 or "" if we can't tell. The caller should not xfree the result.
16895
16896 For example, if we're within the method foo() in the following
16897 code:
16898
16899 namespace N {
16900 class C {
16901 void foo () {
16902 }
16903 };
16904 }
16905
16906 then determine_prefix on foo's die will return "N::C". */
16907
16908 static const char *
16909 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
16910 {
16911 struct die_info *parent, *spec_die;
16912 struct dwarf2_cu *spec_cu;
16913 struct type *parent_type;
16914 char *retval;
16915
16916 if (cu->language != language_cplus && cu->language != language_java
16917 && cu->language != language_fortran)
16918 return "";
16919
16920 retval = anonymous_struct_prefix (die, cu);
16921 if (retval)
16922 return retval;
16923
16924 /* We have to be careful in the presence of DW_AT_specification.
16925 For example, with GCC 3.4, given the code
16926
16927 namespace N {
16928 void foo() {
16929 // Definition of N::foo.
16930 }
16931 }
16932
16933 then we'll have a tree of DIEs like this:
16934
16935 1: DW_TAG_compile_unit
16936 2: DW_TAG_namespace // N
16937 3: DW_TAG_subprogram // declaration of N::foo
16938 4: DW_TAG_subprogram // definition of N::foo
16939 DW_AT_specification // refers to die #3
16940
16941 Thus, when processing die #4, we have to pretend that we're in
16942 the context of its DW_AT_specification, namely the contex of die
16943 #3. */
16944 spec_cu = cu;
16945 spec_die = die_specification (die, &spec_cu);
16946 if (spec_die == NULL)
16947 parent = die->parent;
16948 else
16949 {
16950 parent = spec_die->parent;
16951 cu = spec_cu;
16952 }
16953
16954 if (parent == NULL)
16955 return "";
16956 else if (parent->building_fullname)
16957 {
16958 const char *name;
16959 const char *parent_name;
16960
16961 /* It has been seen on RealView 2.2 built binaries,
16962 DW_TAG_template_type_param types actually _defined_ as
16963 children of the parent class:
16964
16965 enum E {};
16966 template class <class Enum> Class{};
16967 Class<enum E> class_e;
16968
16969 1: DW_TAG_class_type (Class)
16970 2: DW_TAG_enumeration_type (E)
16971 3: DW_TAG_enumerator (enum1:0)
16972 3: DW_TAG_enumerator (enum2:1)
16973 ...
16974 2: DW_TAG_template_type_param
16975 DW_AT_type DW_FORM_ref_udata (E)
16976
16977 Besides being broken debug info, it can put GDB into an
16978 infinite loop. Consider:
16979
16980 When we're building the full name for Class<E>, we'll start
16981 at Class, and go look over its template type parameters,
16982 finding E. We'll then try to build the full name of E, and
16983 reach here. We're now trying to build the full name of E,
16984 and look over the parent DIE for containing scope. In the
16985 broken case, if we followed the parent DIE of E, we'd again
16986 find Class, and once again go look at its template type
16987 arguments, etc., etc. Simply don't consider such parent die
16988 as source-level parent of this die (it can't be, the language
16989 doesn't allow it), and break the loop here. */
16990 name = dwarf2_name (die, cu);
16991 parent_name = dwarf2_name (parent, cu);
16992 complaint (&symfile_complaints,
16993 _("template param type '%s' defined within parent '%s'"),
16994 name ? name : "<unknown>",
16995 parent_name ? parent_name : "<unknown>");
16996 return "";
16997 }
16998 else
16999 switch (parent->tag)
17000 {
17001 case DW_TAG_namespace:
17002 parent_type = read_type_die (parent, cu);
17003 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
17004 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
17005 Work around this problem here. */
17006 if (cu->language == language_cplus
17007 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
17008 return "";
17009 /* We give a name to even anonymous namespaces. */
17010 return TYPE_TAG_NAME (parent_type);
17011 case DW_TAG_class_type:
17012 case DW_TAG_interface_type:
17013 case DW_TAG_structure_type:
17014 case DW_TAG_union_type:
17015 case DW_TAG_module:
17016 parent_type = read_type_die (parent, cu);
17017 if (TYPE_TAG_NAME (parent_type) != NULL)
17018 return TYPE_TAG_NAME (parent_type);
17019 else
17020 /* An anonymous structure is only allowed non-static data
17021 members; no typedefs, no member functions, et cetera.
17022 So it does not need a prefix. */
17023 return "";
17024 case DW_TAG_compile_unit:
17025 case DW_TAG_partial_unit:
17026 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
17027 if (cu->language == language_cplus
17028 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
17029 && die->child != NULL
17030 && (die->tag == DW_TAG_class_type
17031 || die->tag == DW_TAG_structure_type
17032 || die->tag == DW_TAG_union_type))
17033 {
17034 char *name = guess_full_die_structure_name (die, cu);
17035 if (name != NULL)
17036 return name;
17037 }
17038 return "";
17039 default:
17040 return determine_prefix (parent, cu);
17041 }
17042 }
17043
17044 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
17045 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
17046 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
17047 an obconcat, otherwise allocate storage for the result. The CU argument is
17048 used to determine the language and hence, the appropriate separator. */
17049
17050 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
17051
17052 static char *
17053 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
17054 int physname, struct dwarf2_cu *cu)
17055 {
17056 const char *lead = "";
17057 const char *sep;
17058
17059 if (suffix == NULL || suffix[0] == '\0'
17060 || prefix == NULL || prefix[0] == '\0')
17061 sep = "";
17062 else if (cu->language == language_java)
17063 sep = ".";
17064 else if (cu->language == language_fortran && physname)
17065 {
17066 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
17067 DW_AT_MIPS_linkage_name is preferred and used instead. */
17068
17069 lead = "__";
17070 sep = "_MOD_";
17071 }
17072 else
17073 sep = "::";
17074
17075 if (prefix == NULL)
17076 prefix = "";
17077 if (suffix == NULL)
17078 suffix = "";
17079
17080 if (obs == NULL)
17081 {
17082 char *retval
17083 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
17084
17085 strcpy (retval, lead);
17086 strcat (retval, prefix);
17087 strcat (retval, sep);
17088 strcat (retval, suffix);
17089 return retval;
17090 }
17091 else
17092 {
17093 /* We have an obstack. */
17094 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
17095 }
17096 }
17097
17098 /* Return sibling of die, NULL if no sibling. */
17099
17100 static struct die_info *
17101 sibling_die (struct die_info *die)
17102 {
17103 return die->sibling;
17104 }
17105
17106 /* Get name of a die, return NULL if not found. */
17107
17108 static const char *
17109 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
17110 struct obstack *obstack)
17111 {
17112 if (name && cu->language == language_cplus)
17113 {
17114 char *canon_name = cp_canonicalize_string (name);
17115
17116 if (canon_name != NULL)
17117 {
17118 if (strcmp (canon_name, name) != 0)
17119 name = obstack_copy0 (obstack, canon_name, strlen (canon_name));
17120 xfree (canon_name);
17121 }
17122 }
17123
17124 return name;
17125 }
17126
17127 /* Get name of a die, return NULL if not found. */
17128
17129 static const char *
17130 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
17131 {
17132 struct attribute *attr;
17133
17134 attr = dwarf2_attr (die, DW_AT_name, cu);
17135 if ((!attr || !DW_STRING (attr))
17136 && die->tag != DW_TAG_class_type
17137 && die->tag != DW_TAG_interface_type
17138 && die->tag != DW_TAG_structure_type
17139 && die->tag != DW_TAG_union_type)
17140 return NULL;
17141
17142 switch (die->tag)
17143 {
17144 case DW_TAG_compile_unit:
17145 case DW_TAG_partial_unit:
17146 /* Compilation units have a DW_AT_name that is a filename, not
17147 a source language identifier. */
17148 case DW_TAG_enumeration_type:
17149 case DW_TAG_enumerator:
17150 /* These tags always have simple identifiers already; no need
17151 to canonicalize them. */
17152 return DW_STRING (attr);
17153
17154 case DW_TAG_subprogram:
17155 /* Java constructors will all be named "<init>", so return
17156 the class name when we see this special case. */
17157 if (cu->language == language_java
17158 && DW_STRING (attr) != NULL
17159 && strcmp (DW_STRING (attr), "<init>") == 0)
17160 {
17161 struct dwarf2_cu *spec_cu = cu;
17162 struct die_info *spec_die;
17163
17164 /* GCJ will output '<init>' for Java constructor names.
17165 For this special case, return the name of the parent class. */
17166
17167 /* GCJ may output suprogram DIEs with AT_specification set.
17168 If so, use the name of the specified DIE. */
17169 spec_die = die_specification (die, &spec_cu);
17170 if (spec_die != NULL)
17171 return dwarf2_name (spec_die, spec_cu);
17172
17173 do
17174 {
17175 die = die->parent;
17176 if (die->tag == DW_TAG_class_type)
17177 return dwarf2_name (die, cu);
17178 }
17179 while (die->tag != DW_TAG_compile_unit
17180 && die->tag != DW_TAG_partial_unit);
17181 }
17182 break;
17183
17184 case DW_TAG_class_type:
17185 case DW_TAG_interface_type:
17186 case DW_TAG_structure_type:
17187 case DW_TAG_union_type:
17188 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
17189 structures or unions. These were of the form "._%d" in GCC 4.1,
17190 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
17191 and GCC 4.4. We work around this problem by ignoring these. */
17192 if (attr && DW_STRING (attr)
17193 && (strncmp (DW_STRING (attr), "._", 2) == 0
17194 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
17195 return NULL;
17196
17197 /* GCC might emit a nameless typedef that has a linkage name. See
17198 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17199 if (!attr || DW_STRING (attr) == NULL)
17200 {
17201 char *demangled = NULL;
17202
17203 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
17204 if (attr == NULL)
17205 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
17206
17207 if (attr == NULL || DW_STRING (attr) == NULL)
17208 return NULL;
17209
17210 /* Avoid demangling DW_STRING (attr) the second time on a second
17211 call for the same DIE. */
17212 if (!DW_STRING_IS_CANONICAL (attr))
17213 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
17214
17215 if (demangled)
17216 {
17217 char *base;
17218
17219 /* FIXME: we already did this for the partial symbol... */
17220 DW_STRING (attr) = obstack_copy0 (&cu->objfile->objfile_obstack,
17221 demangled, strlen (demangled));
17222 DW_STRING_IS_CANONICAL (attr) = 1;
17223 xfree (demangled);
17224
17225 /* Strip any leading namespaces/classes, keep only the base name.
17226 DW_AT_name for named DIEs does not contain the prefixes. */
17227 base = strrchr (DW_STRING (attr), ':');
17228 if (base && base > DW_STRING (attr) && base[-1] == ':')
17229 return &base[1];
17230 else
17231 return DW_STRING (attr);
17232 }
17233 }
17234 break;
17235
17236 default:
17237 break;
17238 }
17239
17240 if (!DW_STRING_IS_CANONICAL (attr))
17241 {
17242 DW_STRING (attr)
17243 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
17244 &cu->objfile->objfile_obstack);
17245 DW_STRING_IS_CANONICAL (attr) = 1;
17246 }
17247 return DW_STRING (attr);
17248 }
17249
17250 /* Return the die that this die in an extension of, or NULL if there
17251 is none. *EXT_CU is the CU containing DIE on input, and the CU
17252 containing the return value on output. */
17253
17254 static struct die_info *
17255 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
17256 {
17257 struct attribute *attr;
17258
17259 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
17260 if (attr == NULL)
17261 return NULL;
17262
17263 return follow_die_ref (die, attr, ext_cu);
17264 }
17265
17266 /* Convert a DIE tag into its string name. */
17267
17268 static const char *
17269 dwarf_tag_name (unsigned tag)
17270 {
17271 const char *name = get_DW_TAG_name (tag);
17272
17273 if (name == NULL)
17274 return "DW_TAG_<unknown>";
17275
17276 return name;
17277 }
17278
17279 /* Convert a DWARF attribute code into its string name. */
17280
17281 static const char *
17282 dwarf_attr_name (unsigned attr)
17283 {
17284 const char *name;
17285
17286 #ifdef MIPS /* collides with DW_AT_HP_block_index */
17287 if (attr == DW_AT_MIPS_fde)
17288 return "DW_AT_MIPS_fde";
17289 #else
17290 if (attr == DW_AT_HP_block_index)
17291 return "DW_AT_HP_block_index";
17292 #endif
17293
17294 name = get_DW_AT_name (attr);
17295
17296 if (name == NULL)
17297 return "DW_AT_<unknown>";
17298
17299 return name;
17300 }
17301
17302 /* Convert a DWARF value form code into its string name. */
17303
17304 static const char *
17305 dwarf_form_name (unsigned form)
17306 {
17307 const char *name = get_DW_FORM_name (form);
17308
17309 if (name == NULL)
17310 return "DW_FORM_<unknown>";
17311
17312 return name;
17313 }
17314
17315 static char *
17316 dwarf_bool_name (unsigned mybool)
17317 {
17318 if (mybool)
17319 return "TRUE";
17320 else
17321 return "FALSE";
17322 }
17323
17324 /* Convert a DWARF type code into its string name. */
17325
17326 static const char *
17327 dwarf_type_encoding_name (unsigned enc)
17328 {
17329 const char *name = get_DW_ATE_name (enc);
17330
17331 if (name == NULL)
17332 return "DW_ATE_<unknown>";
17333
17334 return name;
17335 }
17336
17337 static void
17338 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
17339 {
17340 unsigned int i;
17341
17342 print_spaces (indent, f);
17343 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
17344 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
17345
17346 if (die->parent != NULL)
17347 {
17348 print_spaces (indent, f);
17349 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
17350 die->parent->offset.sect_off);
17351 }
17352
17353 print_spaces (indent, f);
17354 fprintf_unfiltered (f, " has children: %s\n",
17355 dwarf_bool_name (die->child != NULL));
17356
17357 print_spaces (indent, f);
17358 fprintf_unfiltered (f, " attributes:\n");
17359
17360 for (i = 0; i < die->num_attrs; ++i)
17361 {
17362 print_spaces (indent, f);
17363 fprintf_unfiltered (f, " %s (%s) ",
17364 dwarf_attr_name (die->attrs[i].name),
17365 dwarf_form_name (die->attrs[i].form));
17366
17367 switch (die->attrs[i].form)
17368 {
17369 case DW_FORM_addr:
17370 case DW_FORM_GNU_addr_index:
17371 fprintf_unfiltered (f, "address: ");
17372 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
17373 break;
17374 case DW_FORM_block2:
17375 case DW_FORM_block4:
17376 case DW_FORM_block:
17377 case DW_FORM_block1:
17378 fprintf_unfiltered (f, "block: size %s",
17379 pulongest (DW_BLOCK (&die->attrs[i])->size));
17380 break;
17381 case DW_FORM_exprloc:
17382 fprintf_unfiltered (f, "expression: size %s",
17383 pulongest (DW_BLOCK (&die->attrs[i])->size));
17384 break;
17385 case DW_FORM_ref_addr:
17386 fprintf_unfiltered (f, "ref address: ");
17387 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17388 break;
17389 case DW_FORM_GNU_ref_alt:
17390 fprintf_unfiltered (f, "alt ref address: ");
17391 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17392 break;
17393 case DW_FORM_ref1:
17394 case DW_FORM_ref2:
17395 case DW_FORM_ref4:
17396 case DW_FORM_ref8:
17397 case DW_FORM_ref_udata:
17398 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
17399 (long) (DW_UNSND (&die->attrs[i])));
17400 break;
17401 case DW_FORM_data1:
17402 case DW_FORM_data2:
17403 case DW_FORM_data4:
17404 case DW_FORM_data8:
17405 case DW_FORM_udata:
17406 case DW_FORM_sdata:
17407 fprintf_unfiltered (f, "constant: %s",
17408 pulongest (DW_UNSND (&die->attrs[i])));
17409 break;
17410 case DW_FORM_sec_offset:
17411 fprintf_unfiltered (f, "section offset: %s",
17412 pulongest (DW_UNSND (&die->attrs[i])));
17413 break;
17414 case DW_FORM_ref_sig8:
17415 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
17416 {
17417 struct signatured_type *sig_type =
17418 DW_SIGNATURED_TYPE (&die->attrs[i]);
17419
17420 fprintf_unfiltered (f, "signatured type: 0x%s, offset 0x%x",
17421 hex_string (sig_type->signature),
17422 sig_type->per_cu.offset.sect_off);
17423 }
17424 else
17425 fprintf_unfiltered (f, "signatured type, unknown");
17426 break;
17427 case DW_FORM_string:
17428 case DW_FORM_strp:
17429 case DW_FORM_GNU_str_index:
17430 case DW_FORM_GNU_strp_alt:
17431 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
17432 DW_STRING (&die->attrs[i])
17433 ? DW_STRING (&die->attrs[i]) : "",
17434 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
17435 break;
17436 case DW_FORM_flag:
17437 if (DW_UNSND (&die->attrs[i]))
17438 fprintf_unfiltered (f, "flag: TRUE");
17439 else
17440 fprintf_unfiltered (f, "flag: FALSE");
17441 break;
17442 case DW_FORM_flag_present:
17443 fprintf_unfiltered (f, "flag: TRUE");
17444 break;
17445 case DW_FORM_indirect:
17446 /* The reader will have reduced the indirect form to
17447 the "base form" so this form should not occur. */
17448 fprintf_unfiltered (f,
17449 "unexpected attribute form: DW_FORM_indirect");
17450 break;
17451 default:
17452 fprintf_unfiltered (f, "unsupported attribute form: %d.",
17453 die->attrs[i].form);
17454 break;
17455 }
17456 fprintf_unfiltered (f, "\n");
17457 }
17458 }
17459
17460 static void
17461 dump_die_for_error (struct die_info *die)
17462 {
17463 dump_die_shallow (gdb_stderr, 0, die);
17464 }
17465
17466 static void
17467 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
17468 {
17469 int indent = level * 4;
17470
17471 gdb_assert (die != NULL);
17472
17473 if (level >= max_level)
17474 return;
17475
17476 dump_die_shallow (f, indent, die);
17477
17478 if (die->child != NULL)
17479 {
17480 print_spaces (indent, f);
17481 fprintf_unfiltered (f, " Children:");
17482 if (level + 1 < max_level)
17483 {
17484 fprintf_unfiltered (f, "\n");
17485 dump_die_1 (f, level + 1, max_level, die->child);
17486 }
17487 else
17488 {
17489 fprintf_unfiltered (f,
17490 " [not printed, max nesting level reached]\n");
17491 }
17492 }
17493
17494 if (die->sibling != NULL && level > 0)
17495 {
17496 dump_die_1 (f, level, max_level, die->sibling);
17497 }
17498 }
17499
17500 /* This is called from the pdie macro in gdbinit.in.
17501 It's not static so gcc will keep a copy callable from gdb. */
17502
17503 void
17504 dump_die (struct die_info *die, int max_level)
17505 {
17506 dump_die_1 (gdb_stdlog, 0, max_level, die);
17507 }
17508
17509 static void
17510 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
17511 {
17512 void **slot;
17513
17514 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
17515 INSERT);
17516
17517 *slot = die;
17518 }
17519
17520 /* DW_ADDR is always stored already as sect_offset; despite for the forms
17521 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
17522
17523 static int
17524 is_ref_attr (struct attribute *attr)
17525 {
17526 switch (attr->form)
17527 {
17528 case DW_FORM_ref_addr:
17529 case DW_FORM_ref1:
17530 case DW_FORM_ref2:
17531 case DW_FORM_ref4:
17532 case DW_FORM_ref8:
17533 case DW_FORM_ref_udata:
17534 case DW_FORM_GNU_ref_alt:
17535 return 1;
17536 default:
17537 return 0;
17538 }
17539 }
17540
17541 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
17542 required kind. */
17543
17544 static sect_offset
17545 dwarf2_get_ref_die_offset (struct attribute *attr)
17546 {
17547 sect_offset retval = { DW_UNSND (attr) };
17548
17549 if (is_ref_attr (attr))
17550 return retval;
17551
17552 retval.sect_off = 0;
17553 complaint (&symfile_complaints,
17554 _("unsupported die ref attribute form: '%s'"),
17555 dwarf_form_name (attr->form));
17556 return retval;
17557 }
17558
17559 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
17560 * the value held by the attribute is not constant. */
17561
17562 static LONGEST
17563 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
17564 {
17565 if (attr->form == DW_FORM_sdata)
17566 return DW_SND (attr);
17567 else if (attr->form == DW_FORM_udata
17568 || attr->form == DW_FORM_data1
17569 || attr->form == DW_FORM_data2
17570 || attr->form == DW_FORM_data4
17571 || attr->form == DW_FORM_data8)
17572 return DW_UNSND (attr);
17573 else
17574 {
17575 complaint (&symfile_complaints,
17576 _("Attribute value is not a constant (%s)"),
17577 dwarf_form_name (attr->form));
17578 return default_value;
17579 }
17580 }
17581
17582 /* Follow reference or signature attribute ATTR of SRC_DIE.
17583 On entry *REF_CU is the CU of SRC_DIE.
17584 On exit *REF_CU is the CU of the result. */
17585
17586 static struct die_info *
17587 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
17588 struct dwarf2_cu **ref_cu)
17589 {
17590 struct die_info *die;
17591
17592 if (is_ref_attr (attr))
17593 die = follow_die_ref (src_die, attr, ref_cu);
17594 else if (attr->form == DW_FORM_ref_sig8)
17595 die = follow_die_sig (src_die, attr, ref_cu);
17596 else
17597 {
17598 dump_die_for_error (src_die);
17599 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
17600 (*ref_cu)->objfile->name);
17601 }
17602
17603 return die;
17604 }
17605
17606 /* Follow reference OFFSET.
17607 On entry *REF_CU is the CU of the source die referencing OFFSET.
17608 On exit *REF_CU is the CU of the result.
17609 Returns NULL if OFFSET is invalid. */
17610
17611 static struct die_info *
17612 follow_die_offset (sect_offset offset, int offset_in_dwz,
17613 struct dwarf2_cu **ref_cu)
17614 {
17615 struct die_info temp_die;
17616 struct dwarf2_cu *target_cu, *cu = *ref_cu;
17617
17618 gdb_assert (cu->per_cu != NULL);
17619
17620 target_cu = cu;
17621
17622 if (cu->per_cu->is_debug_types)
17623 {
17624 /* .debug_types CUs cannot reference anything outside their CU.
17625 If they need to, they have to reference a signatured type via
17626 DW_FORM_ref_sig8. */
17627 if (! offset_in_cu_p (&cu->header, offset))
17628 return NULL;
17629 }
17630 else if (offset_in_dwz != cu->per_cu->is_dwz
17631 || ! offset_in_cu_p (&cu->header, offset))
17632 {
17633 struct dwarf2_per_cu_data *per_cu;
17634
17635 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
17636 cu->objfile);
17637
17638 /* If necessary, add it to the queue and load its DIEs. */
17639 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
17640 load_full_comp_unit (per_cu, cu->language);
17641
17642 target_cu = per_cu->cu;
17643 }
17644 else if (cu->dies == NULL)
17645 {
17646 /* We're loading full DIEs during partial symbol reading. */
17647 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
17648 load_full_comp_unit (cu->per_cu, language_minimal);
17649 }
17650
17651 *ref_cu = target_cu;
17652 temp_die.offset = offset;
17653 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
17654 }
17655
17656 /* Follow reference attribute ATTR of SRC_DIE.
17657 On entry *REF_CU is the CU of SRC_DIE.
17658 On exit *REF_CU is the CU of the result. */
17659
17660 static struct die_info *
17661 follow_die_ref (struct die_info *src_die, struct attribute *attr,
17662 struct dwarf2_cu **ref_cu)
17663 {
17664 sect_offset offset = dwarf2_get_ref_die_offset (attr);
17665 struct dwarf2_cu *cu = *ref_cu;
17666 struct die_info *die;
17667
17668 die = follow_die_offset (offset,
17669 (attr->form == DW_FORM_GNU_ref_alt
17670 || cu->per_cu->is_dwz),
17671 ref_cu);
17672 if (!die)
17673 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
17674 "at 0x%x [in module %s]"),
17675 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
17676
17677 return die;
17678 }
17679
17680 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
17681 Returned value is intended for DW_OP_call*. Returned
17682 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
17683
17684 struct dwarf2_locexpr_baton
17685 dwarf2_fetch_die_loc_sect_off (sect_offset offset,
17686 struct dwarf2_per_cu_data *per_cu,
17687 CORE_ADDR (*get_frame_pc) (void *baton),
17688 void *baton)
17689 {
17690 struct dwarf2_cu *cu;
17691 struct die_info *die;
17692 struct attribute *attr;
17693 struct dwarf2_locexpr_baton retval;
17694
17695 dw2_setup (per_cu->objfile);
17696
17697 if (per_cu->cu == NULL)
17698 load_cu (per_cu);
17699 cu = per_cu->cu;
17700
17701 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
17702 if (!die)
17703 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
17704 offset.sect_off, per_cu->objfile->name);
17705
17706 attr = dwarf2_attr (die, DW_AT_location, cu);
17707 if (!attr)
17708 {
17709 /* DWARF: "If there is no such attribute, then there is no effect.".
17710 DATA is ignored if SIZE is 0. */
17711
17712 retval.data = NULL;
17713 retval.size = 0;
17714 }
17715 else if (attr_form_is_section_offset (attr))
17716 {
17717 struct dwarf2_loclist_baton loclist_baton;
17718 CORE_ADDR pc = (*get_frame_pc) (baton);
17719 size_t size;
17720
17721 fill_in_loclist_baton (cu, &loclist_baton, attr);
17722
17723 retval.data = dwarf2_find_location_expression (&loclist_baton,
17724 &size, pc);
17725 retval.size = size;
17726 }
17727 else
17728 {
17729 if (!attr_form_is_block (attr))
17730 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
17731 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
17732 offset.sect_off, per_cu->objfile->name);
17733
17734 retval.data = DW_BLOCK (attr)->data;
17735 retval.size = DW_BLOCK (attr)->size;
17736 }
17737 retval.per_cu = cu->per_cu;
17738
17739 age_cached_comp_units ();
17740
17741 return retval;
17742 }
17743
17744 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
17745 offset. */
17746
17747 struct dwarf2_locexpr_baton
17748 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
17749 struct dwarf2_per_cu_data *per_cu,
17750 CORE_ADDR (*get_frame_pc) (void *baton),
17751 void *baton)
17752 {
17753 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
17754
17755 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
17756 }
17757
17758 /* Return the type of the DIE at DIE_OFFSET in the CU named by
17759 PER_CU. */
17760
17761 struct type *
17762 dwarf2_get_die_type (cu_offset die_offset,
17763 struct dwarf2_per_cu_data *per_cu)
17764 {
17765 sect_offset die_offset_sect;
17766
17767 dw2_setup (per_cu->objfile);
17768
17769 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
17770 return get_die_type_at_offset (die_offset_sect, per_cu);
17771 }
17772
17773 /* Follow the signature attribute ATTR in SRC_DIE.
17774 On entry *REF_CU is the CU of SRC_DIE.
17775 On exit *REF_CU is the CU of the result. */
17776
17777 static struct die_info *
17778 follow_die_sig (struct die_info *src_die, struct attribute *attr,
17779 struct dwarf2_cu **ref_cu)
17780 {
17781 struct objfile *objfile = (*ref_cu)->objfile;
17782 struct die_info temp_die;
17783 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
17784 struct dwarf2_cu *sig_cu;
17785 struct die_info *die;
17786
17787 /* sig_type will be NULL if the signatured type is missing from
17788 the debug info. */
17789 if (sig_type == NULL)
17790 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
17791 "at 0x%x [in module %s]"),
17792 src_die->offset.sect_off, objfile->name);
17793
17794 /* If necessary, add it to the queue and load its DIEs. */
17795
17796 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
17797 read_signatured_type (sig_type);
17798
17799 gdb_assert (sig_type->per_cu.cu != NULL);
17800
17801 sig_cu = sig_type->per_cu.cu;
17802 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
17803 temp_die.offset = sig_type->type_offset_in_section;
17804 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
17805 temp_die.offset.sect_off);
17806 if (die)
17807 {
17808 /* For .gdb_index version 7 keep track of included TUs.
17809 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
17810 if (dwarf2_per_objfile->index_table != NULL
17811 && dwarf2_per_objfile->index_table->version <= 7)
17812 {
17813 VEC_safe_push (dwarf2_per_cu_ptr,
17814 (*ref_cu)->per_cu->imported_symtabs,
17815 sig_cu->per_cu);
17816 }
17817
17818 *ref_cu = sig_cu;
17819 return die;
17820 }
17821
17822 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
17823 "from DIE at 0x%x [in module %s]"),
17824 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
17825 }
17826
17827 /* Load the DIEs associated with type unit PER_CU into memory. */
17828
17829 static void
17830 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
17831 {
17832 struct signatured_type *sig_type;
17833
17834 /* Caller is responsible for ensuring type_unit_groups don't get here. */
17835 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
17836
17837 /* We have the per_cu, but we need the signatured_type.
17838 Fortunately this is an easy translation. */
17839 gdb_assert (per_cu->is_debug_types);
17840 sig_type = (struct signatured_type *) per_cu;
17841
17842 gdb_assert (per_cu->cu == NULL);
17843
17844 read_signatured_type (sig_type);
17845
17846 gdb_assert (per_cu->cu != NULL);
17847 }
17848
17849 /* die_reader_func for read_signatured_type.
17850 This is identical to load_full_comp_unit_reader,
17851 but is kept separate for now. */
17852
17853 static void
17854 read_signatured_type_reader (const struct die_reader_specs *reader,
17855 gdb_byte *info_ptr,
17856 struct die_info *comp_unit_die,
17857 int has_children,
17858 void *data)
17859 {
17860 struct dwarf2_cu *cu = reader->cu;
17861
17862 gdb_assert (cu->die_hash == NULL);
17863 cu->die_hash =
17864 htab_create_alloc_ex (cu->header.length / 12,
17865 die_hash,
17866 die_eq,
17867 NULL,
17868 &cu->comp_unit_obstack,
17869 hashtab_obstack_allocate,
17870 dummy_obstack_deallocate);
17871
17872 if (has_children)
17873 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
17874 &info_ptr, comp_unit_die);
17875 cu->dies = comp_unit_die;
17876 /* comp_unit_die is not stored in die_hash, no need. */
17877
17878 /* We try not to read any attributes in this function, because not
17879 all CUs needed for references have been loaded yet, and symbol
17880 table processing isn't initialized. But we have to set the CU language,
17881 or we won't be able to build types correctly.
17882 Similarly, if we do not read the producer, we can not apply
17883 producer-specific interpretation. */
17884 prepare_one_comp_unit (cu, cu->dies, language_minimal);
17885 }
17886
17887 /* Read in a signatured type and build its CU and DIEs.
17888 If the type is a stub for the real type in a DWO file,
17889 read in the real type from the DWO file as well. */
17890
17891 static void
17892 read_signatured_type (struct signatured_type *sig_type)
17893 {
17894 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
17895
17896 gdb_assert (per_cu->is_debug_types);
17897 gdb_assert (per_cu->cu == NULL);
17898
17899 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
17900 read_signatured_type_reader, NULL);
17901 }
17902
17903 /* Decode simple location descriptions.
17904 Given a pointer to a dwarf block that defines a location, compute
17905 the location and return the value.
17906
17907 NOTE drow/2003-11-18: This function is called in two situations
17908 now: for the address of static or global variables (partial symbols
17909 only) and for offsets into structures which are expected to be
17910 (more or less) constant. The partial symbol case should go away,
17911 and only the constant case should remain. That will let this
17912 function complain more accurately. A few special modes are allowed
17913 without complaint for global variables (for instance, global
17914 register values and thread-local values).
17915
17916 A location description containing no operations indicates that the
17917 object is optimized out. The return value is 0 for that case.
17918 FIXME drow/2003-11-16: No callers check for this case any more; soon all
17919 callers will only want a very basic result and this can become a
17920 complaint.
17921
17922 Note that stack[0] is unused except as a default error return. */
17923
17924 static CORE_ADDR
17925 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
17926 {
17927 struct objfile *objfile = cu->objfile;
17928 size_t i;
17929 size_t size = blk->size;
17930 gdb_byte *data = blk->data;
17931 CORE_ADDR stack[64];
17932 int stacki;
17933 unsigned int bytes_read, unsnd;
17934 gdb_byte op;
17935
17936 i = 0;
17937 stacki = 0;
17938 stack[stacki] = 0;
17939 stack[++stacki] = 0;
17940
17941 while (i < size)
17942 {
17943 op = data[i++];
17944 switch (op)
17945 {
17946 case DW_OP_lit0:
17947 case DW_OP_lit1:
17948 case DW_OP_lit2:
17949 case DW_OP_lit3:
17950 case DW_OP_lit4:
17951 case DW_OP_lit5:
17952 case DW_OP_lit6:
17953 case DW_OP_lit7:
17954 case DW_OP_lit8:
17955 case DW_OP_lit9:
17956 case DW_OP_lit10:
17957 case DW_OP_lit11:
17958 case DW_OP_lit12:
17959 case DW_OP_lit13:
17960 case DW_OP_lit14:
17961 case DW_OP_lit15:
17962 case DW_OP_lit16:
17963 case DW_OP_lit17:
17964 case DW_OP_lit18:
17965 case DW_OP_lit19:
17966 case DW_OP_lit20:
17967 case DW_OP_lit21:
17968 case DW_OP_lit22:
17969 case DW_OP_lit23:
17970 case DW_OP_lit24:
17971 case DW_OP_lit25:
17972 case DW_OP_lit26:
17973 case DW_OP_lit27:
17974 case DW_OP_lit28:
17975 case DW_OP_lit29:
17976 case DW_OP_lit30:
17977 case DW_OP_lit31:
17978 stack[++stacki] = op - DW_OP_lit0;
17979 break;
17980
17981 case DW_OP_reg0:
17982 case DW_OP_reg1:
17983 case DW_OP_reg2:
17984 case DW_OP_reg3:
17985 case DW_OP_reg4:
17986 case DW_OP_reg5:
17987 case DW_OP_reg6:
17988 case DW_OP_reg7:
17989 case DW_OP_reg8:
17990 case DW_OP_reg9:
17991 case DW_OP_reg10:
17992 case DW_OP_reg11:
17993 case DW_OP_reg12:
17994 case DW_OP_reg13:
17995 case DW_OP_reg14:
17996 case DW_OP_reg15:
17997 case DW_OP_reg16:
17998 case DW_OP_reg17:
17999 case DW_OP_reg18:
18000 case DW_OP_reg19:
18001 case DW_OP_reg20:
18002 case DW_OP_reg21:
18003 case DW_OP_reg22:
18004 case DW_OP_reg23:
18005 case DW_OP_reg24:
18006 case DW_OP_reg25:
18007 case DW_OP_reg26:
18008 case DW_OP_reg27:
18009 case DW_OP_reg28:
18010 case DW_OP_reg29:
18011 case DW_OP_reg30:
18012 case DW_OP_reg31:
18013 stack[++stacki] = op - DW_OP_reg0;
18014 if (i < size)
18015 dwarf2_complex_location_expr_complaint ();
18016 break;
18017
18018 case DW_OP_regx:
18019 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
18020 i += bytes_read;
18021 stack[++stacki] = unsnd;
18022 if (i < size)
18023 dwarf2_complex_location_expr_complaint ();
18024 break;
18025
18026 case DW_OP_addr:
18027 stack[++stacki] = read_address (objfile->obfd, &data[i],
18028 cu, &bytes_read);
18029 i += bytes_read;
18030 break;
18031
18032 case DW_OP_const1u:
18033 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
18034 i += 1;
18035 break;
18036
18037 case DW_OP_const1s:
18038 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
18039 i += 1;
18040 break;
18041
18042 case DW_OP_const2u:
18043 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
18044 i += 2;
18045 break;
18046
18047 case DW_OP_const2s:
18048 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
18049 i += 2;
18050 break;
18051
18052 case DW_OP_const4u:
18053 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
18054 i += 4;
18055 break;
18056
18057 case DW_OP_const4s:
18058 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
18059 i += 4;
18060 break;
18061
18062 case DW_OP_const8u:
18063 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
18064 i += 8;
18065 break;
18066
18067 case DW_OP_constu:
18068 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
18069 &bytes_read);
18070 i += bytes_read;
18071 break;
18072
18073 case DW_OP_consts:
18074 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
18075 i += bytes_read;
18076 break;
18077
18078 case DW_OP_dup:
18079 stack[stacki + 1] = stack[stacki];
18080 stacki++;
18081 break;
18082
18083 case DW_OP_plus:
18084 stack[stacki - 1] += stack[stacki];
18085 stacki--;
18086 break;
18087
18088 case DW_OP_plus_uconst:
18089 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
18090 &bytes_read);
18091 i += bytes_read;
18092 break;
18093
18094 case DW_OP_minus:
18095 stack[stacki - 1] -= stack[stacki];
18096 stacki--;
18097 break;
18098
18099 case DW_OP_deref:
18100 /* If we're not the last op, then we definitely can't encode
18101 this using GDB's address_class enum. This is valid for partial
18102 global symbols, although the variable's address will be bogus
18103 in the psymtab. */
18104 if (i < size)
18105 dwarf2_complex_location_expr_complaint ();
18106 break;
18107
18108 case DW_OP_GNU_push_tls_address:
18109 /* The top of the stack has the offset from the beginning
18110 of the thread control block at which the variable is located. */
18111 /* Nothing should follow this operator, so the top of stack would
18112 be returned. */
18113 /* This is valid for partial global symbols, but the variable's
18114 address will be bogus in the psymtab. Make it always at least
18115 non-zero to not look as a variable garbage collected by linker
18116 which have DW_OP_addr 0. */
18117 if (i < size)
18118 dwarf2_complex_location_expr_complaint ();
18119 stack[stacki]++;
18120 break;
18121
18122 case DW_OP_GNU_uninit:
18123 break;
18124
18125 case DW_OP_GNU_addr_index:
18126 case DW_OP_GNU_const_index:
18127 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
18128 &bytes_read);
18129 i += bytes_read;
18130 break;
18131
18132 default:
18133 {
18134 const char *name = get_DW_OP_name (op);
18135
18136 if (name)
18137 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
18138 name);
18139 else
18140 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
18141 op);
18142 }
18143
18144 return (stack[stacki]);
18145 }
18146
18147 /* Enforce maximum stack depth of SIZE-1 to avoid writing
18148 outside of the allocated space. Also enforce minimum>0. */
18149 if (stacki >= ARRAY_SIZE (stack) - 1)
18150 {
18151 complaint (&symfile_complaints,
18152 _("location description stack overflow"));
18153 return 0;
18154 }
18155
18156 if (stacki <= 0)
18157 {
18158 complaint (&symfile_complaints,
18159 _("location description stack underflow"));
18160 return 0;
18161 }
18162 }
18163 return (stack[stacki]);
18164 }
18165
18166 /* memory allocation interface */
18167
18168 static struct dwarf_block *
18169 dwarf_alloc_block (struct dwarf2_cu *cu)
18170 {
18171 struct dwarf_block *blk;
18172
18173 blk = (struct dwarf_block *)
18174 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
18175 return (blk);
18176 }
18177
18178 static struct die_info *
18179 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
18180 {
18181 struct die_info *die;
18182 size_t size = sizeof (struct die_info);
18183
18184 if (num_attrs > 1)
18185 size += (num_attrs - 1) * sizeof (struct attribute);
18186
18187 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
18188 memset (die, 0, sizeof (struct die_info));
18189 return (die);
18190 }
18191
18192 \f
18193 /* Macro support. */
18194
18195 /* Return file name relative to the compilation directory of file number I in
18196 *LH's file name table. The result is allocated using xmalloc; the caller is
18197 responsible for freeing it. */
18198
18199 static char *
18200 file_file_name (int file, struct line_header *lh)
18201 {
18202 /* Is the file number a valid index into the line header's file name
18203 table? Remember that file numbers start with one, not zero. */
18204 if (1 <= file && file <= lh->num_file_names)
18205 {
18206 struct file_entry *fe = &lh->file_names[file - 1];
18207
18208 if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0)
18209 return xstrdup (fe->name);
18210 return concat (lh->include_dirs[fe->dir_index - 1], SLASH_STRING,
18211 fe->name, NULL);
18212 }
18213 else
18214 {
18215 /* The compiler produced a bogus file number. We can at least
18216 record the macro definitions made in the file, even if we
18217 won't be able to find the file by name. */
18218 char fake_name[80];
18219
18220 xsnprintf (fake_name, sizeof (fake_name),
18221 "<bad macro file number %d>", file);
18222
18223 complaint (&symfile_complaints,
18224 _("bad file number in macro information (%d)"),
18225 file);
18226
18227 return xstrdup (fake_name);
18228 }
18229 }
18230
18231 /* Return the full name of file number I in *LH's file name table.
18232 Use COMP_DIR as the name of the current directory of the
18233 compilation. The result is allocated using xmalloc; the caller is
18234 responsible for freeing it. */
18235 static char *
18236 file_full_name (int file, struct line_header *lh, const char *comp_dir)
18237 {
18238 /* Is the file number a valid index into the line header's file name
18239 table? Remember that file numbers start with one, not zero. */
18240 if (1 <= file && file <= lh->num_file_names)
18241 {
18242 char *relative = file_file_name (file, lh);
18243
18244 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
18245 return relative;
18246 return reconcat (relative, comp_dir, SLASH_STRING, relative, NULL);
18247 }
18248 else
18249 return file_file_name (file, lh);
18250 }
18251
18252
18253 static struct macro_source_file *
18254 macro_start_file (int file, int line,
18255 struct macro_source_file *current_file,
18256 const char *comp_dir,
18257 struct line_header *lh, struct objfile *objfile)
18258 {
18259 /* File name relative to the compilation directory of this source file. */
18260 char *file_name = file_file_name (file, lh);
18261
18262 /* We don't create a macro table for this compilation unit
18263 at all until we actually get a filename. */
18264 if (! pending_macros)
18265 pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
18266 objfile->per_bfd->macro_cache,
18267 comp_dir);
18268
18269 if (! current_file)
18270 {
18271 /* If we have no current file, then this must be the start_file
18272 directive for the compilation unit's main source file. */
18273 current_file = macro_set_main (pending_macros, file_name);
18274 macro_define_special (pending_macros);
18275 }
18276 else
18277 current_file = macro_include (current_file, line, file_name);
18278
18279 xfree (file_name);
18280
18281 return current_file;
18282 }
18283
18284
18285 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
18286 followed by a null byte. */
18287 static char *
18288 copy_string (const char *buf, int len)
18289 {
18290 char *s = xmalloc (len + 1);
18291
18292 memcpy (s, buf, len);
18293 s[len] = '\0';
18294 return s;
18295 }
18296
18297
18298 static const char *
18299 consume_improper_spaces (const char *p, const char *body)
18300 {
18301 if (*p == ' ')
18302 {
18303 complaint (&symfile_complaints,
18304 _("macro definition contains spaces "
18305 "in formal argument list:\n`%s'"),
18306 body);
18307
18308 while (*p == ' ')
18309 p++;
18310 }
18311
18312 return p;
18313 }
18314
18315
18316 static void
18317 parse_macro_definition (struct macro_source_file *file, int line,
18318 const char *body)
18319 {
18320 const char *p;
18321
18322 /* The body string takes one of two forms. For object-like macro
18323 definitions, it should be:
18324
18325 <macro name> " " <definition>
18326
18327 For function-like macro definitions, it should be:
18328
18329 <macro name> "() " <definition>
18330 or
18331 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
18332
18333 Spaces may appear only where explicitly indicated, and in the
18334 <definition>.
18335
18336 The Dwarf 2 spec says that an object-like macro's name is always
18337 followed by a space, but versions of GCC around March 2002 omit
18338 the space when the macro's definition is the empty string.
18339
18340 The Dwarf 2 spec says that there should be no spaces between the
18341 formal arguments in a function-like macro's formal argument list,
18342 but versions of GCC around March 2002 include spaces after the
18343 commas. */
18344
18345
18346 /* Find the extent of the macro name. The macro name is terminated
18347 by either a space or null character (for an object-like macro) or
18348 an opening paren (for a function-like macro). */
18349 for (p = body; *p; p++)
18350 if (*p == ' ' || *p == '(')
18351 break;
18352
18353 if (*p == ' ' || *p == '\0')
18354 {
18355 /* It's an object-like macro. */
18356 int name_len = p - body;
18357 char *name = copy_string (body, name_len);
18358 const char *replacement;
18359
18360 if (*p == ' ')
18361 replacement = body + name_len + 1;
18362 else
18363 {
18364 dwarf2_macro_malformed_definition_complaint (body);
18365 replacement = body + name_len;
18366 }
18367
18368 macro_define_object (file, line, name, replacement);
18369
18370 xfree (name);
18371 }
18372 else if (*p == '(')
18373 {
18374 /* It's a function-like macro. */
18375 char *name = copy_string (body, p - body);
18376 int argc = 0;
18377 int argv_size = 1;
18378 char **argv = xmalloc (argv_size * sizeof (*argv));
18379
18380 p++;
18381
18382 p = consume_improper_spaces (p, body);
18383
18384 /* Parse the formal argument list. */
18385 while (*p && *p != ')')
18386 {
18387 /* Find the extent of the current argument name. */
18388 const char *arg_start = p;
18389
18390 while (*p && *p != ',' && *p != ')' && *p != ' ')
18391 p++;
18392
18393 if (! *p || p == arg_start)
18394 dwarf2_macro_malformed_definition_complaint (body);
18395 else
18396 {
18397 /* Make sure argv has room for the new argument. */
18398 if (argc >= argv_size)
18399 {
18400 argv_size *= 2;
18401 argv = xrealloc (argv, argv_size * sizeof (*argv));
18402 }
18403
18404 argv[argc++] = copy_string (arg_start, p - arg_start);
18405 }
18406
18407 p = consume_improper_spaces (p, body);
18408
18409 /* Consume the comma, if present. */
18410 if (*p == ',')
18411 {
18412 p++;
18413
18414 p = consume_improper_spaces (p, body);
18415 }
18416 }
18417
18418 if (*p == ')')
18419 {
18420 p++;
18421
18422 if (*p == ' ')
18423 /* Perfectly formed definition, no complaints. */
18424 macro_define_function (file, line, name,
18425 argc, (const char **) argv,
18426 p + 1);
18427 else if (*p == '\0')
18428 {
18429 /* Complain, but do define it. */
18430 dwarf2_macro_malformed_definition_complaint (body);
18431 macro_define_function (file, line, name,
18432 argc, (const char **) argv,
18433 p);
18434 }
18435 else
18436 /* Just complain. */
18437 dwarf2_macro_malformed_definition_complaint (body);
18438 }
18439 else
18440 /* Just complain. */
18441 dwarf2_macro_malformed_definition_complaint (body);
18442
18443 xfree (name);
18444 {
18445 int i;
18446
18447 for (i = 0; i < argc; i++)
18448 xfree (argv[i]);
18449 }
18450 xfree (argv);
18451 }
18452 else
18453 dwarf2_macro_malformed_definition_complaint (body);
18454 }
18455
18456 /* Skip some bytes from BYTES according to the form given in FORM.
18457 Returns the new pointer. */
18458
18459 static gdb_byte *
18460 skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
18461 enum dwarf_form form,
18462 unsigned int offset_size,
18463 struct dwarf2_section_info *section)
18464 {
18465 unsigned int bytes_read;
18466
18467 switch (form)
18468 {
18469 case DW_FORM_data1:
18470 case DW_FORM_flag:
18471 ++bytes;
18472 break;
18473
18474 case DW_FORM_data2:
18475 bytes += 2;
18476 break;
18477
18478 case DW_FORM_data4:
18479 bytes += 4;
18480 break;
18481
18482 case DW_FORM_data8:
18483 bytes += 8;
18484 break;
18485
18486 case DW_FORM_string:
18487 read_direct_string (abfd, bytes, &bytes_read);
18488 bytes += bytes_read;
18489 break;
18490
18491 case DW_FORM_sec_offset:
18492 case DW_FORM_strp:
18493 case DW_FORM_GNU_strp_alt:
18494 bytes += offset_size;
18495 break;
18496
18497 case DW_FORM_block:
18498 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
18499 bytes += bytes_read;
18500 break;
18501
18502 case DW_FORM_block1:
18503 bytes += 1 + read_1_byte (abfd, bytes);
18504 break;
18505 case DW_FORM_block2:
18506 bytes += 2 + read_2_bytes (abfd, bytes);
18507 break;
18508 case DW_FORM_block4:
18509 bytes += 4 + read_4_bytes (abfd, bytes);
18510 break;
18511
18512 case DW_FORM_sdata:
18513 case DW_FORM_udata:
18514 case DW_FORM_GNU_addr_index:
18515 case DW_FORM_GNU_str_index:
18516 bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
18517 if (bytes == NULL)
18518 {
18519 dwarf2_section_buffer_overflow_complaint (section);
18520 return NULL;
18521 }
18522 break;
18523
18524 default:
18525 {
18526 complain:
18527 complaint (&symfile_complaints,
18528 _("invalid form 0x%x in `%s'"),
18529 form,
18530 section->asection->name);
18531 return NULL;
18532 }
18533 }
18534
18535 return bytes;
18536 }
18537
18538 /* A helper for dwarf_decode_macros that handles skipping an unknown
18539 opcode. Returns an updated pointer to the macro data buffer; or,
18540 on error, issues a complaint and returns NULL. */
18541
18542 static gdb_byte *
18543 skip_unknown_opcode (unsigned int opcode,
18544 gdb_byte **opcode_definitions,
18545 gdb_byte *mac_ptr, gdb_byte *mac_end,
18546 bfd *abfd,
18547 unsigned int offset_size,
18548 struct dwarf2_section_info *section)
18549 {
18550 unsigned int bytes_read, i;
18551 unsigned long arg;
18552 gdb_byte *defn;
18553
18554 if (opcode_definitions[opcode] == NULL)
18555 {
18556 complaint (&symfile_complaints,
18557 _("unrecognized DW_MACFINO opcode 0x%x"),
18558 opcode);
18559 return NULL;
18560 }
18561
18562 defn = opcode_definitions[opcode];
18563 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
18564 defn += bytes_read;
18565
18566 for (i = 0; i < arg; ++i)
18567 {
18568 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
18569 section);
18570 if (mac_ptr == NULL)
18571 {
18572 /* skip_form_bytes already issued the complaint. */
18573 return NULL;
18574 }
18575 }
18576
18577 return mac_ptr;
18578 }
18579
18580 /* A helper function which parses the header of a macro section.
18581 If the macro section is the extended (for now called "GNU") type,
18582 then this updates *OFFSET_SIZE. Returns a pointer to just after
18583 the header, or issues a complaint and returns NULL on error. */
18584
18585 static gdb_byte *
18586 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
18587 bfd *abfd,
18588 gdb_byte *mac_ptr,
18589 unsigned int *offset_size,
18590 int section_is_gnu)
18591 {
18592 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
18593
18594 if (section_is_gnu)
18595 {
18596 unsigned int version, flags;
18597
18598 version = read_2_bytes (abfd, mac_ptr);
18599 if (version != 4)
18600 {
18601 complaint (&symfile_complaints,
18602 _("unrecognized version `%d' in .debug_macro section"),
18603 version);
18604 return NULL;
18605 }
18606 mac_ptr += 2;
18607
18608 flags = read_1_byte (abfd, mac_ptr);
18609 ++mac_ptr;
18610 *offset_size = (flags & 1) ? 8 : 4;
18611
18612 if ((flags & 2) != 0)
18613 /* We don't need the line table offset. */
18614 mac_ptr += *offset_size;
18615
18616 /* Vendor opcode descriptions. */
18617 if ((flags & 4) != 0)
18618 {
18619 unsigned int i, count;
18620
18621 count = read_1_byte (abfd, mac_ptr);
18622 ++mac_ptr;
18623 for (i = 0; i < count; ++i)
18624 {
18625 unsigned int opcode, bytes_read;
18626 unsigned long arg;
18627
18628 opcode = read_1_byte (abfd, mac_ptr);
18629 ++mac_ptr;
18630 opcode_definitions[opcode] = mac_ptr;
18631 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18632 mac_ptr += bytes_read;
18633 mac_ptr += arg;
18634 }
18635 }
18636 }
18637
18638 return mac_ptr;
18639 }
18640
18641 /* A helper for dwarf_decode_macros that handles the GNU extensions,
18642 including DW_MACRO_GNU_transparent_include. */
18643
18644 static void
18645 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
18646 struct macro_source_file *current_file,
18647 struct line_header *lh, const char *comp_dir,
18648 struct dwarf2_section_info *section,
18649 int section_is_gnu, int section_is_dwz,
18650 unsigned int offset_size,
18651 struct objfile *objfile,
18652 htab_t include_hash)
18653 {
18654 enum dwarf_macro_record_type macinfo_type;
18655 int at_commandline;
18656 gdb_byte *opcode_definitions[256];
18657
18658 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18659 &offset_size, section_is_gnu);
18660 if (mac_ptr == NULL)
18661 {
18662 /* We already issued a complaint. */
18663 return;
18664 }
18665
18666 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
18667 GDB is still reading the definitions from command line. First
18668 DW_MACINFO_start_file will need to be ignored as it was already executed
18669 to create CURRENT_FILE for the main source holding also the command line
18670 definitions. On first met DW_MACINFO_start_file this flag is reset to
18671 normally execute all the remaining DW_MACINFO_start_file macinfos. */
18672
18673 at_commandline = 1;
18674
18675 do
18676 {
18677 /* Do we at least have room for a macinfo type byte? */
18678 if (mac_ptr >= mac_end)
18679 {
18680 dwarf2_section_buffer_overflow_complaint (section);
18681 break;
18682 }
18683
18684 macinfo_type = read_1_byte (abfd, mac_ptr);
18685 mac_ptr++;
18686
18687 /* Note that we rely on the fact that the corresponding GNU and
18688 DWARF constants are the same. */
18689 switch (macinfo_type)
18690 {
18691 /* A zero macinfo type indicates the end of the macro
18692 information. */
18693 case 0:
18694 break;
18695
18696 case DW_MACRO_GNU_define:
18697 case DW_MACRO_GNU_undef:
18698 case DW_MACRO_GNU_define_indirect:
18699 case DW_MACRO_GNU_undef_indirect:
18700 case DW_MACRO_GNU_define_indirect_alt:
18701 case DW_MACRO_GNU_undef_indirect_alt:
18702 {
18703 unsigned int bytes_read;
18704 int line;
18705 char *body;
18706 int is_define;
18707
18708 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18709 mac_ptr += bytes_read;
18710
18711 if (macinfo_type == DW_MACRO_GNU_define
18712 || macinfo_type == DW_MACRO_GNU_undef)
18713 {
18714 body = read_direct_string (abfd, mac_ptr, &bytes_read);
18715 mac_ptr += bytes_read;
18716 }
18717 else
18718 {
18719 LONGEST str_offset;
18720
18721 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
18722 mac_ptr += offset_size;
18723
18724 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
18725 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
18726 || section_is_dwz)
18727 {
18728 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18729
18730 body = read_indirect_string_from_dwz (dwz, str_offset);
18731 }
18732 else
18733 body = read_indirect_string_at_offset (abfd, str_offset);
18734 }
18735
18736 is_define = (macinfo_type == DW_MACRO_GNU_define
18737 || macinfo_type == DW_MACRO_GNU_define_indirect
18738 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
18739 if (! current_file)
18740 {
18741 /* DWARF violation as no main source is present. */
18742 complaint (&symfile_complaints,
18743 _("debug info with no main source gives macro %s "
18744 "on line %d: %s"),
18745 is_define ? _("definition") : _("undefinition"),
18746 line, body);
18747 break;
18748 }
18749 if ((line == 0 && !at_commandline)
18750 || (line != 0 && at_commandline))
18751 complaint (&symfile_complaints,
18752 _("debug info gives %s macro %s with %s line %d: %s"),
18753 at_commandline ? _("command-line") : _("in-file"),
18754 is_define ? _("definition") : _("undefinition"),
18755 line == 0 ? _("zero") : _("non-zero"), line, body);
18756
18757 if (is_define)
18758 parse_macro_definition (current_file, line, body);
18759 else
18760 {
18761 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
18762 || macinfo_type == DW_MACRO_GNU_undef_indirect
18763 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
18764 macro_undef (current_file, line, body);
18765 }
18766 }
18767 break;
18768
18769 case DW_MACRO_GNU_start_file:
18770 {
18771 unsigned int bytes_read;
18772 int line, file;
18773
18774 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18775 mac_ptr += bytes_read;
18776 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18777 mac_ptr += bytes_read;
18778
18779 if ((line == 0 && !at_commandline)
18780 || (line != 0 && at_commandline))
18781 complaint (&symfile_complaints,
18782 _("debug info gives source %d included "
18783 "from %s at %s line %d"),
18784 file, at_commandline ? _("command-line") : _("file"),
18785 line == 0 ? _("zero") : _("non-zero"), line);
18786
18787 if (at_commandline)
18788 {
18789 /* This DW_MACRO_GNU_start_file was executed in the
18790 pass one. */
18791 at_commandline = 0;
18792 }
18793 else
18794 current_file = macro_start_file (file, line,
18795 current_file, comp_dir,
18796 lh, objfile);
18797 }
18798 break;
18799
18800 case DW_MACRO_GNU_end_file:
18801 if (! current_file)
18802 complaint (&symfile_complaints,
18803 _("macro debug info has an unmatched "
18804 "`close_file' directive"));
18805 else
18806 {
18807 current_file = current_file->included_by;
18808 if (! current_file)
18809 {
18810 enum dwarf_macro_record_type next_type;
18811
18812 /* GCC circa March 2002 doesn't produce the zero
18813 type byte marking the end of the compilation
18814 unit. Complain if it's not there, but exit no
18815 matter what. */
18816
18817 /* Do we at least have room for a macinfo type byte? */
18818 if (mac_ptr >= mac_end)
18819 {
18820 dwarf2_section_buffer_overflow_complaint (section);
18821 return;
18822 }
18823
18824 /* We don't increment mac_ptr here, so this is just
18825 a look-ahead. */
18826 next_type = read_1_byte (abfd, mac_ptr);
18827 if (next_type != 0)
18828 complaint (&symfile_complaints,
18829 _("no terminating 0-type entry for "
18830 "macros in `.debug_macinfo' section"));
18831
18832 return;
18833 }
18834 }
18835 break;
18836
18837 case DW_MACRO_GNU_transparent_include:
18838 case DW_MACRO_GNU_transparent_include_alt:
18839 {
18840 LONGEST offset;
18841 void **slot;
18842 bfd *include_bfd = abfd;
18843 struct dwarf2_section_info *include_section = section;
18844 struct dwarf2_section_info alt_section;
18845 gdb_byte *include_mac_end = mac_end;
18846 int is_dwz = section_is_dwz;
18847 gdb_byte *new_mac_ptr;
18848
18849 offset = read_offset_1 (abfd, mac_ptr, offset_size);
18850 mac_ptr += offset_size;
18851
18852 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
18853 {
18854 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18855
18856 dwarf2_read_section (dwarf2_per_objfile->objfile,
18857 &dwz->macro);
18858
18859 include_bfd = dwz->macro.asection->owner;
18860 include_section = &dwz->macro;
18861 include_mac_end = dwz->macro.buffer + dwz->macro.size;
18862 is_dwz = 1;
18863 }
18864
18865 new_mac_ptr = include_section->buffer + offset;
18866 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
18867
18868 if (*slot != NULL)
18869 {
18870 /* This has actually happened; see
18871 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
18872 complaint (&symfile_complaints,
18873 _("recursive DW_MACRO_GNU_transparent_include in "
18874 ".debug_macro section"));
18875 }
18876 else
18877 {
18878 *slot = new_mac_ptr;
18879
18880 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
18881 include_mac_end, current_file,
18882 lh, comp_dir,
18883 section, section_is_gnu, is_dwz,
18884 offset_size, objfile, include_hash);
18885
18886 htab_remove_elt (include_hash, new_mac_ptr);
18887 }
18888 }
18889 break;
18890
18891 case DW_MACINFO_vendor_ext:
18892 if (!section_is_gnu)
18893 {
18894 unsigned int bytes_read;
18895 int constant;
18896
18897 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18898 mac_ptr += bytes_read;
18899 read_direct_string (abfd, mac_ptr, &bytes_read);
18900 mac_ptr += bytes_read;
18901
18902 /* We don't recognize any vendor extensions. */
18903 break;
18904 }
18905 /* FALLTHROUGH */
18906
18907 default:
18908 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
18909 mac_ptr, mac_end, abfd, offset_size,
18910 section);
18911 if (mac_ptr == NULL)
18912 return;
18913 break;
18914 }
18915 } while (macinfo_type != 0);
18916 }
18917
18918 static void
18919 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
18920 const char *comp_dir, int section_is_gnu)
18921 {
18922 struct objfile *objfile = dwarf2_per_objfile->objfile;
18923 struct line_header *lh = cu->line_header;
18924 bfd *abfd;
18925 gdb_byte *mac_ptr, *mac_end;
18926 struct macro_source_file *current_file = 0;
18927 enum dwarf_macro_record_type macinfo_type;
18928 unsigned int offset_size = cu->header.offset_size;
18929 gdb_byte *opcode_definitions[256];
18930 struct cleanup *cleanup;
18931 htab_t include_hash;
18932 void **slot;
18933 struct dwarf2_section_info *section;
18934 const char *section_name;
18935
18936 if (cu->dwo_unit != NULL)
18937 {
18938 if (section_is_gnu)
18939 {
18940 section = &cu->dwo_unit->dwo_file->sections.macro;
18941 section_name = ".debug_macro.dwo";
18942 }
18943 else
18944 {
18945 section = &cu->dwo_unit->dwo_file->sections.macinfo;
18946 section_name = ".debug_macinfo.dwo";
18947 }
18948 }
18949 else
18950 {
18951 if (section_is_gnu)
18952 {
18953 section = &dwarf2_per_objfile->macro;
18954 section_name = ".debug_macro";
18955 }
18956 else
18957 {
18958 section = &dwarf2_per_objfile->macinfo;
18959 section_name = ".debug_macinfo";
18960 }
18961 }
18962
18963 dwarf2_read_section (objfile, section);
18964 if (section->buffer == NULL)
18965 {
18966 complaint (&symfile_complaints, _("missing %s section"), section_name);
18967 return;
18968 }
18969 abfd = section->asection->owner;
18970
18971 /* First pass: Find the name of the base filename.
18972 This filename is needed in order to process all macros whose definition
18973 (or undefinition) comes from the command line. These macros are defined
18974 before the first DW_MACINFO_start_file entry, and yet still need to be
18975 associated to the base file.
18976
18977 To determine the base file name, we scan the macro definitions until we
18978 reach the first DW_MACINFO_start_file entry. We then initialize
18979 CURRENT_FILE accordingly so that any macro definition found before the
18980 first DW_MACINFO_start_file can still be associated to the base file. */
18981
18982 mac_ptr = section->buffer + offset;
18983 mac_end = section->buffer + section->size;
18984
18985 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18986 &offset_size, section_is_gnu);
18987 if (mac_ptr == NULL)
18988 {
18989 /* We already issued a complaint. */
18990 return;
18991 }
18992
18993 do
18994 {
18995 /* Do we at least have room for a macinfo type byte? */
18996 if (mac_ptr >= mac_end)
18997 {
18998 /* Complaint is printed during the second pass as GDB will probably
18999 stop the first pass earlier upon finding
19000 DW_MACINFO_start_file. */
19001 break;
19002 }
19003
19004 macinfo_type = read_1_byte (abfd, mac_ptr);
19005 mac_ptr++;
19006
19007 /* Note that we rely on the fact that the corresponding GNU and
19008 DWARF constants are the same. */
19009 switch (macinfo_type)
19010 {
19011 /* A zero macinfo type indicates the end of the macro
19012 information. */
19013 case 0:
19014 break;
19015
19016 case DW_MACRO_GNU_define:
19017 case DW_MACRO_GNU_undef:
19018 /* Only skip the data by MAC_PTR. */
19019 {
19020 unsigned int bytes_read;
19021
19022 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19023 mac_ptr += bytes_read;
19024 read_direct_string (abfd, mac_ptr, &bytes_read);
19025 mac_ptr += bytes_read;
19026 }
19027 break;
19028
19029 case DW_MACRO_GNU_start_file:
19030 {
19031 unsigned int bytes_read;
19032 int line, file;
19033
19034 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19035 mac_ptr += bytes_read;
19036 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19037 mac_ptr += bytes_read;
19038
19039 current_file = macro_start_file (file, line, current_file,
19040 comp_dir, lh, objfile);
19041 }
19042 break;
19043
19044 case DW_MACRO_GNU_end_file:
19045 /* No data to skip by MAC_PTR. */
19046 break;
19047
19048 case DW_MACRO_GNU_define_indirect:
19049 case DW_MACRO_GNU_undef_indirect:
19050 case DW_MACRO_GNU_define_indirect_alt:
19051 case DW_MACRO_GNU_undef_indirect_alt:
19052 {
19053 unsigned int bytes_read;
19054
19055 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19056 mac_ptr += bytes_read;
19057 mac_ptr += offset_size;
19058 }
19059 break;
19060
19061 case DW_MACRO_GNU_transparent_include:
19062 case DW_MACRO_GNU_transparent_include_alt:
19063 /* Note that, according to the spec, a transparent include
19064 chain cannot call DW_MACRO_GNU_start_file. So, we can just
19065 skip this opcode. */
19066 mac_ptr += offset_size;
19067 break;
19068
19069 case DW_MACINFO_vendor_ext:
19070 /* Only skip the data by MAC_PTR. */
19071 if (!section_is_gnu)
19072 {
19073 unsigned int bytes_read;
19074
19075 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19076 mac_ptr += bytes_read;
19077 read_direct_string (abfd, mac_ptr, &bytes_read);
19078 mac_ptr += bytes_read;
19079 }
19080 /* FALLTHROUGH */
19081
19082 default:
19083 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
19084 mac_ptr, mac_end, abfd, offset_size,
19085 section);
19086 if (mac_ptr == NULL)
19087 return;
19088 break;
19089 }
19090 } while (macinfo_type != 0 && current_file == NULL);
19091
19092 /* Second pass: Process all entries.
19093
19094 Use the AT_COMMAND_LINE flag to determine whether we are still processing
19095 command-line macro definitions/undefinitions. This flag is unset when we
19096 reach the first DW_MACINFO_start_file entry. */
19097
19098 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
19099 NULL, xcalloc, xfree);
19100 cleanup = make_cleanup_htab_delete (include_hash);
19101 mac_ptr = section->buffer + offset;
19102 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
19103 *slot = mac_ptr;
19104 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
19105 current_file, lh, comp_dir, section,
19106 section_is_gnu, 0,
19107 offset_size, objfile, include_hash);
19108 do_cleanups (cleanup);
19109 }
19110
19111 /* Check if the attribute's form is a DW_FORM_block*
19112 if so return true else false. */
19113
19114 static int
19115 attr_form_is_block (struct attribute *attr)
19116 {
19117 return (attr == NULL ? 0 :
19118 attr->form == DW_FORM_block1
19119 || attr->form == DW_FORM_block2
19120 || attr->form == DW_FORM_block4
19121 || attr->form == DW_FORM_block
19122 || attr->form == DW_FORM_exprloc);
19123 }
19124
19125 /* Return non-zero if ATTR's value is a section offset --- classes
19126 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
19127 You may use DW_UNSND (attr) to retrieve such offsets.
19128
19129 Section 7.5.4, "Attribute Encodings", explains that no attribute
19130 may have a value that belongs to more than one of these classes; it
19131 would be ambiguous if we did, because we use the same forms for all
19132 of them. */
19133
19134 static int
19135 attr_form_is_section_offset (struct attribute *attr)
19136 {
19137 return (attr->form == DW_FORM_data4
19138 || attr->form == DW_FORM_data8
19139 || attr->form == DW_FORM_sec_offset);
19140 }
19141
19142 /* Return non-zero if ATTR's value falls in the 'constant' class, or
19143 zero otherwise. When this function returns true, you can apply
19144 dwarf2_get_attr_constant_value to it.
19145
19146 However, note that for some attributes you must check
19147 attr_form_is_section_offset before using this test. DW_FORM_data4
19148 and DW_FORM_data8 are members of both the constant class, and of
19149 the classes that contain offsets into other debug sections
19150 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
19151 that, if an attribute's can be either a constant or one of the
19152 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
19153 taken as section offsets, not constants. */
19154
19155 static int
19156 attr_form_is_constant (struct attribute *attr)
19157 {
19158 switch (attr->form)
19159 {
19160 case DW_FORM_sdata:
19161 case DW_FORM_udata:
19162 case DW_FORM_data1:
19163 case DW_FORM_data2:
19164 case DW_FORM_data4:
19165 case DW_FORM_data8:
19166 return 1;
19167 default:
19168 return 0;
19169 }
19170 }
19171
19172 /* Return the .debug_loc section to use for CU.
19173 For DWO files use .debug_loc.dwo. */
19174
19175 static struct dwarf2_section_info *
19176 cu_debug_loc_section (struct dwarf2_cu *cu)
19177 {
19178 if (cu->dwo_unit)
19179 return &cu->dwo_unit->dwo_file->sections.loc;
19180 return &dwarf2_per_objfile->loc;
19181 }
19182
19183 /* A helper function that fills in a dwarf2_loclist_baton. */
19184
19185 static void
19186 fill_in_loclist_baton (struct dwarf2_cu *cu,
19187 struct dwarf2_loclist_baton *baton,
19188 struct attribute *attr)
19189 {
19190 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19191
19192 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
19193
19194 baton->per_cu = cu->per_cu;
19195 gdb_assert (baton->per_cu);
19196 /* We don't know how long the location list is, but make sure we
19197 don't run off the edge of the section. */
19198 baton->size = section->size - DW_UNSND (attr);
19199 baton->data = section->buffer + DW_UNSND (attr);
19200 baton->base_address = cu->base_address;
19201 baton->from_dwo = cu->dwo_unit != NULL;
19202 }
19203
19204 static void
19205 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
19206 struct dwarf2_cu *cu, int is_block)
19207 {
19208 struct objfile *objfile = dwarf2_per_objfile->objfile;
19209 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19210
19211 if (attr_form_is_section_offset (attr)
19212 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
19213 the section. If so, fall through to the complaint in the
19214 other branch. */
19215 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
19216 {
19217 struct dwarf2_loclist_baton *baton;
19218
19219 baton = obstack_alloc (&objfile->objfile_obstack,
19220 sizeof (struct dwarf2_loclist_baton));
19221
19222 fill_in_loclist_baton (cu, baton, attr);
19223
19224 if (cu->base_known == 0)
19225 complaint (&symfile_complaints,
19226 _("Location list used without "
19227 "specifying the CU base address."));
19228
19229 SYMBOL_ACLASS_INDEX (sym) = (is_block
19230 ? dwarf2_loclist_block_index
19231 : dwarf2_loclist_index);
19232 SYMBOL_LOCATION_BATON (sym) = baton;
19233 }
19234 else
19235 {
19236 struct dwarf2_locexpr_baton *baton;
19237
19238 baton = obstack_alloc (&objfile->objfile_obstack,
19239 sizeof (struct dwarf2_locexpr_baton));
19240 baton->per_cu = cu->per_cu;
19241 gdb_assert (baton->per_cu);
19242
19243 if (attr_form_is_block (attr))
19244 {
19245 /* Note that we're just copying the block's data pointer
19246 here, not the actual data. We're still pointing into the
19247 info_buffer for SYM's objfile; right now we never release
19248 that buffer, but when we do clean up properly this may
19249 need to change. */
19250 baton->size = DW_BLOCK (attr)->size;
19251 baton->data = DW_BLOCK (attr)->data;
19252 }
19253 else
19254 {
19255 dwarf2_invalid_attrib_class_complaint ("location description",
19256 SYMBOL_NATURAL_NAME (sym));
19257 baton->size = 0;
19258 }
19259
19260 SYMBOL_ACLASS_INDEX (sym) = (is_block
19261 ? dwarf2_locexpr_block_index
19262 : dwarf2_locexpr_index);
19263 SYMBOL_LOCATION_BATON (sym) = baton;
19264 }
19265 }
19266
19267 /* Return the OBJFILE associated with the compilation unit CU. If CU
19268 came from a separate debuginfo file, then the master objfile is
19269 returned. */
19270
19271 struct objfile *
19272 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
19273 {
19274 struct objfile *objfile = per_cu->objfile;
19275
19276 /* Return the master objfile, so that we can report and look up the
19277 correct file containing this variable. */
19278 if (objfile->separate_debug_objfile_backlink)
19279 objfile = objfile->separate_debug_objfile_backlink;
19280
19281 return objfile;
19282 }
19283
19284 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
19285 (CU_HEADERP is unused in such case) or prepare a temporary copy at
19286 CU_HEADERP first. */
19287
19288 static const struct comp_unit_head *
19289 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
19290 struct dwarf2_per_cu_data *per_cu)
19291 {
19292 gdb_byte *info_ptr;
19293
19294 if (per_cu->cu)
19295 return &per_cu->cu->header;
19296
19297 info_ptr = per_cu->section->buffer + per_cu->offset.sect_off;
19298
19299 memset (cu_headerp, 0, sizeof (*cu_headerp));
19300 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
19301
19302 return cu_headerp;
19303 }
19304
19305 /* Return the address size given in the compilation unit header for CU. */
19306
19307 int
19308 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
19309 {
19310 struct comp_unit_head cu_header_local;
19311 const struct comp_unit_head *cu_headerp;
19312
19313 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19314
19315 return cu_headerp->addr_size;
19316 }
19317
19318 /* Return the offset size given in the compilation unit header for CU. */
19319
19320 int
19321 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
19322 {
19323 struct comp_unit_head cu_header_local;
19324 const struct comp_unit_head *cu_headerp;
19325
19326 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19327
19328 return cu_headerp->offset_size;
19329 }
19330
19331 /* See its dwarf2loc.h declaration. */
19332
19333 int
19334 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
19335 {
19336 struct comp_unit_head cu_header_local;
19337 const struct comp_unit_head *cu_headerp;
19338
19339 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19340
19341 if (cu_headerp->version == 2)
19342 return cu_headerp->addr_size;
19343 else
19344 return cu_headerp->offset_size;
19345 }
19346
19347 /* Return the text offset of the CU. The returned offset comes from
19348 this CU's objfile. If this objfile came from a separate debuginfo
19349 file, then the offset may be different from the corresponding
19350 offset in the parent objfile. */
19351
19352 CORE_ADDR
19353 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
19354 {
19355 struct objfile *objfile = per_cu->objfile;
19356
19357 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19358 }
19359
19360 /* Locate the .debug_info compilation unit from CU's objfile which contains
19361 the DIE at OFFSET. Raises an error on failure. */
19362
19363 static struct dwarf2_per_cu_data *
19364 dwarf2_find_containing_comp_unit (sect_offset offset,
19365 unsigned int offset_in_dwz,
19366 struct objfile *objfile)
19367 {
19368 struct dwarf2_per_cu_data *this_cu;
19369 int low, high;
19370 const sect_offset *cu_off;
19371
19372 low = 0;
19373 high = dwarf2_per_objfile->n_comp_units - 1;
19374 while (high > low)
19375 {
19376 struct dwarf2_per_cu_data *mid_cu;
19377 int mid = low + (high - low) / 2;
19378
19379 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
19380 cu_off = &mid_cu->offset;
19381 if (mid_cu->is_dwz > offset_in_dwz
19382 || (mid_cu->is_dwz == offset_in_dwz
19383 && cu_off->sect_off >= offset.sect_off))
19384 high = mid;
19385 else
19386 low = mid + 1;
19387 }
19388 gdb_assert (low == high);
19389 this_cu = dwarf2_per_objfile->all_comp_units[low];
19390 cu_off = &this_cu->offset;
19391 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
19392 {
19393 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
19394 error (_("Dwarf Error: could not find partial DIE containing "
19395 "offset 0x%lx [in module %s]"),
19396 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
19397
19398 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
19399 <= offset.sect_off);
19400 return dwarf2_per_objfile->all_comp_units[low-1];
19401 }
19402 else
19403 {
19404 this_cu = dwarf2_per_objfile->all_comp_units[low];
19405 if (low == dwarf2_per_objfile->n_comp_units - 1
19406 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
19407 error (_("invalid dwarf2 offset %u"), offset.sect_off);
19408 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
19409 return this_cu;
19410 }
19411 }
19412
19413 /* Initialize dwarf2_cu CU, owned by PER_CU. */
19414
19415 static void
19416 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
19417 {
19418 memset (cu, 0, sizeof (*cu));
19419 per_cu->cu = cu;
19420 cu->per_cu = per_cu;
19421 cu->objfile = per_cu->objfile;
19422 obstack_init (&cu->comp_unit_obstack);
19423 }
19424
19425 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
19426
19427 static void
19428 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
19429 enum language pretend_language)
19430 {
19431 struct attribute *attr;
19432
19433 /* Set the language we're debugging. */
19434 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
19435 if (attr)
19436 set_cu_language (DW_UNSND (attr), cu);
19437 else
19438 {
19439 cu->language = pretend_language;
19440 cu->language_defn = language_def (cu->language);
19441 }
19442
19443 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
19444 if (attr)
19445 cu->producer = DW_STRING (attr);
19446 }
19447
19448 /* Release one cached compilation unit, CU. We unlink it from the tree
19449 of compilation units, but we don't remove it from the read_in_chain;
19450 the caller is responsible for that.
19451 NOTE: DATA is a void * because this function is also used as a
19452 cleanup routine. */
19453
19454 static void
19455 free_heap_comp_unit (void *data)
19456 {
19457 struct dwarf2_cu *cu = data;
19458
19459 gdb_assert (cu->per_cu != NULL);
19460 cu->per_cu->cu = NULL;
19461 cu->per_cu = NULL;
19462
19463 obstack_free (&cu->comp_unit_obstack, NULL);
19464
19465 xfree (cu);
19466 }
19467
19468 /* This cleanup function is passed the address of a dwarf2_cu on the stack
19469 when we're finished with it. We can't free the pointer itself, but be
19470 sure to unlink it from the cache. Also release any associated storage. */
19471
19472 static void
19473 free_stack_comp_unit (void *data)
19474 {
19475 struct dwarf2_cu *cu = data;
19476
19477 gdb_assert (cu->per_cu != NULL);
19478 cu->per_cu->cu = NULL;
19479 cu->per_cu = NULL;
19480
19481 obstack_free (&cu->comp_unit_obstack, NULL);
19482 cu->partial_dies = NULL;
19483 }
19484
19485 /* Free all cached compilation units. */
19486
19487 static void
19488 free_cached_comp_units (void *data)
19489 {
19490 struct dwarf2_per_cu_data *per_cu, **last_chain;
19491
19492 per_cu = dwarf2_per_objfile->read_in_chain;
19493 last_chain = &dwarf2_per_objfile->read_in_chain;
19494 while (per_cu != NULL)
19495 {
19496 struct dwarf2_per_cu_data *next_cu;
19497
19498 next_cu = per_cu->cu->read_in_chain;
19499
19500 free_heap_comp_unit (per_cu->cu);
19501 *last_chain = next_cu;
19502
19503 per_cu = next_cu;
19504 }
19505 }
19506
19507 /* Increase the age counter on each cached compilation unit, and free
19508 any that are too old. */
19509
19510 static void
19511 age_cached_comp_units (void)
19512 {
19513 struct dwarf2_per_cu_data *per_cu, **last_chain;
19514
19515 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
19516 per_cu = dwarf2_per_objfile->read_in_chain;
19517 while (per_cu != NULL)
19518 {
19519 per_cu->cu->last_used ++;
19520 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
19521 dwarf2_mark (per_cu->cu);
19522 per_cu = per_cu->cu->read_in_chain;
19523 }
19524
19525 per_cu = dwarf2_per_objfile->read_in_chain;
19526 last_chain = &dwarf2_per_objfile->read_in_chain;
19527 while (per_cu != NULL)
19528 {
19529 struct dwarf2_per_cu_data *next_cu;
19530
19531 next_cu = per_cu->cu->read_in_chain;
19532
19533 if (!per_cu->cu->mark)
19534 {
19535 free_heap_comp_unit (per_cu->cu);
19536 *last_chain = next_cu;
19537 }
19538 else
19539 last_chain = &per_cu->cu->read_in_chain;
19540
19541 per_cu = next_cu;
19542 }
19543 }
19544
19545 /* Remove a single compilation unit from the cache. */
19546
19547 static void
19548 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
19549 {
19550 struct dwarf2_per_cu_data *per_cu, **last_chain;
19551
19552 per_cu = dwarf2_per_objfile->read_in_chain;
19553 last_chain = &dwarf2_per_objfile->read_in_chain;
19554 while (per_cu != NULL)
19555 {
19556 struct dwarf2_per_cu_data *next_cu;
19557
19558 next_cu = per_cu->cu->read_in_chain;
19559
19560 if (per_cu == target_per_cu)
19561 {
19562 free_heap_comp_unit (per_cu->cu);
19563 per_cu->cu = NULL;
19564 *last_chain = next_cu;
19565 break;
19566 }
19567 else
19568 last_chain = &per_cu->cu->read_in_chain;
19569
19570 per_cu = next_cu;
19571 }
19572 }
19573
19574 /* Release all extra memory associated with OBJFILE. */
19575
19576 void
19577 dwarf2_free_objfile (struct objfile *objfile)
19578 {
19579 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
19580
19581 if (dwarf2_per_objfile == NULL)
19582 return;
19583
19584 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
19585 free_cached_comp_units (NULL);
19586
19587 if (dwarf2_per_objfile->quick_file_names_table)
19588 htab_delete (dwarf2_per_objfile->quick_file_names_table);
19589
19590 /* Everything else should be on the objfile obstack. */
19591 }
19592
19593 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
19594 We store these in a hash table separate from the DIEs, and preserve them
19595 when the DIEs are flushed out of cache.
19596
19597 The CU "per_cu" pointer is needed because offset alone is not enough to
19598 uniquely identify the type. A file may have multiple .debug_types sections,
19599 or the type may come from a DWO file. Furthermore, while it's more logical
19600 to use per_cu->section+offset, with Fission the section with the data is in
19601 the DWO file but we don't know that section at the point we need it.
19602 We have to use something in dwarf2_per_cu_data (or the pointer to it)
19603 because we can enter the lookup routine, get_die_type_at_offset, from
19604 outside this file, and thus won't necessarily have PER_CU->cu.
19605 Fortunately, PER_CU is stable for the life of the objfile. */
19606
19607 struct dwarf2_per_cu_offset_and_type
19608 {
19609 const struct dwarf2_per_cu_data *per_cu;
19610 sect_offset offset;
19611 struct type *type;
19612 };
19613
19614 /* Hash function for a dwarf2_per_cu_offset_and_type. */
19615
19616 static hashval_t
19617 per_cu_offset_and_type_hash (const void *item)
19618 {
19619 const struct dwarf2_per_cu_offset_and_type *ofs = item;
19620
19621 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
19622 }
19623
19624 /* Equality function for a dwarf2_per_cu_offset_and_type. */
19625
19626 static int
19627 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
19628 {
19629 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
19630 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
19631
19632 return (ofs_lhs->per_cu == ofs_rhs->per_cu
19633 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
19634 }
19635
19636 /* Set the type associated with DIE to TYPE. Save it in CU's hash
19637 table if necessary. For convenience, return TYPE.
19638
19639 The DIEs reading must have careful ordering to:
19640 * Not cause infite loops trying to read in DIEs as a prerequisite for
19641 reading current DIE.
19642 * Not trying to dereference contents of still incompletely read in types
19643 while reading in other DIEs.
19644 * Enable referencing still incompletely read in types just by a pointer to
19645 the type without accessing its fields.
19646
19647 Therefore caller should follow these rules:
19648 * Try to fetch any prerequisite types we may need to build this DIE type
19649 before building the type and calling set_die_type.
19650 * After building type call set_die_type for current DIE as soon as
19651 possible before fetching more types to complete the current type.
19652 * Make the type as complete as possible before fetching more types. */
19653
19654 static struct type *
19655 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19656 {
19657 struct dwarf2_per_cu_offset_and_type **slot, ofs;
19658 struct objfile *objfile = cu->objfile;
19659
19660 /* For Ada types, make sure that the gnat-specific data is always
19661 initialized (if not already set). There are a few types where
19662 we should not be doing so, because the type-specific area is
19663 already used to hold some other piece of info (eg: TYPE_CODE_FLT
19664 where the type-specific area is used to store the floatformat).
19665 But this is not a problem, because the gnat-specific information
19666 is actually not needed for these types. */
19667 if (need_gnat_info (cu)
19668 && TYPE_CODE (type) != TYPE_CODE_FUNC
19669 && TYPE_CODE (type) != TYPE_CODE_FLT
19670 && !HAVE_GNAT_AUX_INFO (type))
19671 INIT_GNAT_SPECIFIC (type);
19672
19673 if (dwarf2_per_objfile->die_type_hash == NULL)
19674 {
19675 dwarf2_per_objfile->die_type_hash =
19676 htab_create_alloc_ex (127,
19677 per_cu_offset_and_type_hash,
19678 per_cu_offset_and_type_eq,
19679 NULL,
19680 &objfile->objfile_obstack,
19681 hashtab_obstack_allocate,
19682 dummy_obstack_deallocate);
19683 }
19684
19685 ofs.per_cu = cu->per_cu;
19686 ofs.offset = die->offset;
19687 ofs.type = type;
19688 slot = (struct dwarf2_per_cu_offset_and_type **)
19689 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
19690 if (*slot)
19691 complaint (&symfile_complaints,
19692 _("A problem internal to GDB: DIE 0x%x has type already set"),
19693 die->offset.sect_off);
19694 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
19695 **slot = ofs;
19696 return type;
19697 }
19698
19699 /* Look up the type for the die at OFFSET in the appropriate type_hash
19700 table, or return NULL if the die does not have a saved type. */
19701
19702 static struct type *
19703 get_die_type_at_offset (sect_offset offset,
19704 struct dwarf2_per_cu_data *per_cu)
19705 {
19706 struct dwarf2_per_cu_offset_and_type *slot, ofs;
19707
19708 if (dwarf2_per_objfile->die_type_hash == NULL)
19709 return NULL;
19710
19711 ofs.per_cu = per_cu;
19712 ofs.offset = offset;
19713 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
19714 if (slot)
19715 return slot->type;
19716 else
19717 return NULL;
19718 }
19719
19720 /* Look up the type for DIE in the appropriate type_hash table,
19721 or return NULL if DIE does not have a saved type. */
19722
19723 static struct type *
19724 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
19725 {
19726 return get_die_type_at_offset (die->offset, cu->per_cu);
19727 }
19728
19729 /* Add a dependence relationship from CU to REF_PER_CU. */
19730
19731 static void
19732 dwarf2_add_dependence (struct dwarf2_cu *cu,
19733 struct dwarf2_per_cu_data *ref_per_cu)
19734 {
19735 void **slot;
19736
19737 if (cu->dependencies == NULL)
19738 cu->dependencies
19739 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
19740 NULL, &cu->comp_unit_obstack,
19741 hashtab_obstack_allocate,
19742 dummy_obstack_deallocate);
19743
19744 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
19745 if (*slot == NULL)
19746 *slot = ref_per_cu;
19747 }
19748
19749 /* Subroutine of dwarf2_mark to pass to htab_traverse.
19750 Set the mark field in every compilation unit in the
19751 cache that we must keep because we are keeping CU. */
19752
19753 static int
19754 dwarf2_mark_helper (void **slot, void *data)
19755 {
19756 struct dwarf2_per_cu_data *per_cu;
19757
19758 per_cu = (struct dwarf2_per_cu_data *) *slot;
19759
19760 /* cu->dependencies references may not yet have been ever read if QUIT aborts
19761 reading of the chain. As such dependencies remain valid it is not much
19762 useful to track and undo them during QUIT cleanups. */
19763 if (per_cu->cu == NULL)
19764 return 1;
19765
19766 if (per_cu->cu->mark)
19767 return 1;
19768 per_cu->cu->mark = 1;
19769
19770 if (per_cu->cu->dependencies != NULL)
19771 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
19772
19773 return 1;
19774 }
19775
19776 /* Set the mark field in CU and in every other compilation unit in the
19777 cache that we must keep because we are keeping CU. */
19778
19779 static void
19780 dwarf2_mark (struct dwarf2_cu *cu)
19781 {
19782 if (cu->mark)
19783 return;
19784 cu->mark = 1;
19785 if (cu->dependencies != NULL)
19786 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
19787 }
19788
19789 static void
19790 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
19791 {
19792 while (per_cu)
19793 {
19794 per_cu->cu->mark = 0;
19795 per_cu = per_cu->cu->read_in_chain;
19796 }
19797 }
19798
19799 /* Trivial hash function for partial_die_info: the hash value of a DIE
19800 is its offset in .debug_info for this objfile. */
19801
19802 static hashval_t
19803 partial_die_hash (const void *item)
19804 {
19805 const struct partial_die_info *part_die = item;
19806
19807 return part_die->offset.sect_off;
19808 }
19809
19810 /* Trivial comparison function for partial_die_info structures: two DIEs
19811 are equal if they have the same offset. */
19812
19813 static int
19814 partial_die_eq (const void *item_lhs, const void *item_rhs)
19815 {
19816 const struct partial_die_info *part_die_lhs = item_lhs;
19817 const struct partial_die_info *part_die_rhs = item_rhs;
19818
19819 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
19820 }
19821
19822 static struct cmd_list_element *set_dwarf2_cmdlist;
19823 static struct cmd_list_element *show_dwarf2_cmdlist;
19824
19825 static void
19826 set_dwarf2_cmd (char *args, int from_tty)
19827 {
19828 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
19829 }
19830
19831 static void
19832 show_dwarf2_cmd (char *args, int from_tty)
19833 {
19834 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
19835 }
19836
19837 /* Free data associated with OBJFILE, if necessary. */
19838
19839 static void
19840 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
19841 {
19842 struct dwarf2_per_objfile *data = d;
19843 int ix;
19844
19845 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
19846 VEC_free (dwarf2_per_cu_ptr,
19847 dwarf2_per_objfile->all_comp_units[ix]->imported_symtabs);
19848
19849 for (ix = 0; ix < dwarf2_per_objfile->n_type_units; ++ix)
19850 VEC_free (dwarf2_per_cu_ptr,
19851 dwarf2_per_objfile->all_type_units[ix]->per_cu.imported_symtabs);
19852
19853 VEC_free (dwarf2_section_info_def, data->types);
19854
19855 if (data->dwo_files)
19856 free_dwo_files (data->dwo_files, objfile);
19857 if (data->dwp_file)
19858 gdb_bfd_unref (data->dwp_file->dbfd);
19859
19860 if (data->dwz_file && data->dwz_file->dwz_bfd)
19861 gdb_bfd_unref (data->dwz_file->dwz_bfd);
19862 }
19863
19864 \f
19865 /* The "save gdb-index" command. */
19866
19867 /* The contents of the hash table we create when building the string
19868 table. */
19869 struct strtab_entry
19870 {
19871 offset_type offset;
19872 const char *str;
19873 };
19874
19875 /* Hash function for a strtab_entry.
19876
19877 Function is used only during write_hash_table so no index format backward
19878 compatibility is needed. */
19879
19880 static hashval_t
19881 hash_strtab_entry (const void *e)
19882 {
19883 const struct strtab_entry *entry = e;
19884 return mapped_index_string_hash (INT_MAX, entry->str);
19885 }
19886
19887 /* Equality function for a strtab_entry. */
19888
19889 static int
19890 eq_strtab_entry (const void *a, const void *b)
19891 {
19892 const struct strtab_entry *ea = a;
19893 const struct strtab_entry *eb = b;
19894 return !strcmp (ea->str, eb->str);
19895 }
19896
19897 /* Create a strtab_entry hash table. */
19898
19899 static htab_t
19900 create_strtab (void)
19901 {
19902 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
19903 xfree, xcalloc, xfree);
19904 }
19905
19906 /* Add a string to the constant pool. Return the string's offset in
19907 host order. */
19908
19909 static offset_type
19910 add_string (htab_t table, struct obstack *cpool, const char *str)
19911 {
19912 void **slot;
19913 struct strtab_entry entry;
19914 struct strtab_entry *result;
19915
19916 entry.str = str;
19917 slot = htab_find_slot (table, &entry, INSERT);
19918 if (*slot)
19919 result = *slot;
19920 else
19921 {
19922 result = XNEW (struct strtab_entry);
19923 result->offset = obstack_object_size (cpool);
19924 result->str = str;
19925 obstack_grow_str0 (cpool, str);
19926 *slot = result;
19927 }
19928 return result->offset;
19929 }
19930
19931 /* An entry in the symbol table. */
19932 struct symtab_index_entry
19933 {
19934 /* The name of the symbol. */
19935 const char *name;
19936 /* The offset of the name in the constant pool. */
19937 offset_type index_offset;
19938 /* A sorted vector of the indices of all the CUs that hold an object
19939 of this name. */
19940 VEC (offset_type) *cu_indices;
19941 };
19942
19943 /* The symbol table. This is a power-of-2-sized hash table. */
19944 struct mapped_symtab
19945 {
19946 offset_type n_elements;
19947 offset_type size;
19948 struct symtab_index_entry **data;
19949 };
19950
19951 /* Hash function for a symtab_index_entry. */
19952
19953 static hashval_t
19954 hash_symtab_entry (const void *e)
19955 {
19956 const struct symtab_index_entry *entry = e;
19957 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
19958 sizeof (offset_type) * VEC_length (offset_type,
19959 entry->cu_indices),
19960 0);
19961 }
19962
19963 /* Equality function for a symtab_index_entry. */
19964
19965 static int
19966 eq_symtab_entry (const void *a, const void *b)
19967 {
19968 const struct symtab_index_entry *ea = a;
19969 const struct symtab_index_entry *eb = b;
19970 int len = VEC_length (offset_type, ea->cu_indices);
19971 if (len != VEC_length (offset_type, eb->cu_indices))
19972 return 0;
19973 return !memcmp (VEC_address (offset_type, ea->cu_indices),
19974 VEC_address (offset_type, eb->cu_indices),
19975 sizeof (offset_type) * len);
19976 }
19977
19978 /* Destroy a symtab_index_entry. */
19979
19980 static void
19981 delete_symtab_entry (void *p)
19982 {
19983 struct symtab_index_entry *entry = p;
19984 VEC_free (offset_type, entry->cu_indices);
19985 xfree (entry);
19986 }
19987
19988 /* Create a hash table holding symtab_index_entry objects. */
19989
19990 static htab_t
19991 create_symbol_hash_table (void)
19992 {
19993 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
19994 delete_symtab_entry, xcalloc, xfree);
19995 }
19996
19997 /* Create a new mapped symtab object. */
19998
19999 static struct mapped_symtab *
20000 create_mapped_symtab (void)
20001 {
20002 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
20003 symtab->n_elements = 0;
20004 symtab->size = 1024;
20005 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
20006 return symtab;
20007 }
20008
20009 /* Destroy a mapped_symtab. */
20010
20011 static void
20012 cleanup_mapped_symtab (void *p)
20013 {
20014 struct mapped_symtab *symtab = p;
20015 /* The contents of the array are freed when the other hash table is
20016 destroyed. */
20017 xfree (symtab->data);
20018 xfree (symtab);
20019 }
20020
20021 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
20022 the slot.
20023
20024 Function is used only during write_hash_table so no index format backward
20025 compatibility is needed. */
20026
20027 static struct symtab_index_entry **
20028 find_slot (struct mapped_symtab *symtab, const char *name)
20029 {
20030 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
20031
20032 index = hash & (symtab->size - 1);
20033 step = ((hash * 17) & (symtab->size - 1)) | 1;
20034
20035 for (;;)
20036 {
20037 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
20038 return &symtab->data[index];
20039 index = (index + step) & (symtab->size - 1);
20040 }
20041 }
20042
20043 /* Expand SYMTAB's hash table. */
20044
20045 static void
20046 hash_expand (struct mapped_symtab *symtab)
20047 {
20048 offset_type old_size = symtab->size;
20049 offset_type i;
20050 struct symtab_index_entry **old_entries = symtab->data;
20051
20052 symtab->size *= 2;
20053 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
20054
20055 for (i = 0; i < old_size; ++i)
20056 {
20057 if (old_entries[i])
20058 {
20059 struct symtab_index_entry **slot = find_slot (symtab,
20060 old_entries[i]->name);
20061 *slot = old_entries[i];
20062 }
20063 }
20064
20065 xfree (old_entries);
20066 }
20067
20068 /* Add an entry to SYMTAB. NAME is the name of the symbol.
20069 CU_INDEX is the index of the CU in which the symbol appears.
20070 IS_STATIC is one if the symbol is static, otherwise zero (global). */
20071
20072 static void
20073 add_index_entry (struct mapped_symtab *symtab, const char *name,
20074 int is_static, gdb_index_symbol_kind kind,
20075 offset_type cu_index)
20076 {
20077 struct symtab_index_entry **slot;
20078 offset_type cu_index_and_attrs;
20079
20080 ++symtab->n_elements;
20081 if (4 * symtab->n_elements / 3 >= symtab->size)
20082 hash_expand (symtab);
20083
20084 slot = find_slot (symtab, name);
20085 if (!*slot)
20086 {
20087 *slot = XNEW (struct symtab_index_entry);
20088 (*slot)->name = name;
20089 /* index_offset is set later. */
20090 (*slot)->cu_indices = NULL;
20091 }
20092
20093 cu_index_and_attrs = 0;
20094 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
20095 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
20096 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
20097
20098 /* We don't want to record an index value twice as we want to avoid the
20099 duplication.
20100 We process all global symbols and then all static symbols
20101 (which would allow us to avoid the duplication by only having to check
20102 the last entry pushed), but a symbol could have multiple kinds in one CU.
20103 To keep things simple we don't worry about the duplication here and
20104 sort and uniqufy the list after we've processed all symbols. */
20105 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
20106 }
20107
20108 /* qsort helper routine for uniquify_cu_indices. */
20109
20110 static int
20111 offset_type_compare (const void *ap, const void *bp)
20112 {
20113 offset_type a = *(offset_type *) ap;
20114 offset_type b = *(offset_type *) bp;
20115
20116 return (a > b) - (b > a);
20117 }
20118
20119 /* Sort and remove duplicates of all symbols' cu_indices lists. */
20120
20121 static void
20122 uniquify_cu_indices (struct mapped_symtab *symtab)
20123 {
20124 int i;
20125
20126 for (i = 0; i < symtab->size; ++i)
20127 {
20128 struct symtab_index_entry *entry = symtab->data[i];
20129
20130 if (entry
20131 && entry->cu_indices != NULL)
20132 {
20133 unsigned int next_to_insert, next_to_check;
20134 offset_type last_value;
20135
20136 qsort (VEC_address (offset_type, entry->cu_indices),
20137 VEC_length (offset_type, entry->cu_indices),
20138 sizeof (offset_type), offset_type_compare);
20139
20140 last_value = VEC_index (offset_type, entry->cu_indices, 0);
20141 next_to_insert = 1;
20142 for (next_to_check = 1;
20143 next_to_check < VEC_length (offset_type, entry->cu_indices);
20144 ++next_to_check)
20145 {
20146 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
20147 != last_value)
20148 {
20149 last_value = VEC_index (offset_type, entry->cu_indices,
20150 next_to_check);
20151 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
20152 last_value);
20153 ++next_to_insert;
20154 }
20155 }
20156 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
20157 }
20158 }
20159 }
20160
20161 /* Add a vector of indices to the constant pool. */
20162
20163 static offset_type
20164 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
20165 struct symtab_index_entry *entry)
20166 {
20167 void **slot;
20168
20169 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
20170 if (!*slot)
20171 {
20172 offset_type len = VEC_length (offset_type, entry->cu_indices);
20173 offset_type val = MAYBE_SWAP (len);
20174 offset_type iter;
20175 int i;
20176
20177 *slot = entry;
20178 entry->index_offset = obstack_object_size (cpool);
20179
20180 obstack_grow (cpool, &val, sizeof (val));
20181 for (i = 0;
20182 VEC_iterate (offset_type, entry->cu_indices, i, iter);
20183 ++i)
20184 {
20185 val = MAYBE_SWAP (iter);
20186 obstack_grow (cpool, &val, sizeof (val));
20187 }
20188 }
20189 else
20190 {
20191 struct symtab_index_entry *old_entry = *slot;
20192 entry->index_offset = old_entry->index_offset;
20193 entry = old_entry;
20194 }
20195 return entry->index_offset;
20196 }
20197
20198 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
20199 constant pool entries going into the obstack CPOOL. */
20200
20201 static void
20202 write_hash_table (struct mapped_symtab *symtab,
20203 struct obstack *output, struct obstack *cpool)
20204 {
20205 offset_type i;
20206 htab_t symbol_hash_table;
20207 htab_t str_table;
20208
20209 symbol_hash_table = create_symbol_hash_table ();
20210 str_table = create_strtab ();
20211
20212 /* We add all the index vectors to the constant pool first, to
20213 ensure alignment is ok. */
20214 for (i = 0; i < symtab->size; ++i)
20215 {
20216 if (symtab->data[i])
20217 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
20218 }
20219
20220 /* Now write out the hash table. */
20221 for (i = 0; i < symtab->size; ++i)
20222 {
20223 offset_type str_off, vec_off;
20224
20225 if (symtab->data[i])
20226 {
20227 str_off = add_string (str_table, cpool, symtab->data[i]->name);
20228 vec_off = symtab->data[i]->index_offset;
20229 }
20230 else
20231 {
20232 /* While 0 is a valid constant pool index, it is not valid
20233 to have 0 for both offsets. */
20234 str_off = 0;
20235 vec_off = 0;
20236 }
20237
20238 str_off = MAYBE_SWAP (str_off);
20239 vec_off = MAYBE_SWAP (vec_off);
20240
20241 obstack_grow (output, &str_off, sizeof (str_off));
20242 obstack_grow (output, &vec_off, sizeof (vec_off));
20243 }
20244
20245 htab_delete (str_table);
20246 htab_delete (symbol_hash_table);
20247 }
20248
20249 /* Struct to map psymtab to CU index in the index file. */
20250 struct psymtab_cu_index_map
20251 {
20252 struct partial_symtab *psymtab;
20253 unsigned int cu_index;
20254 };
20255
20256 static hashval_t
20257 hash_psymtab_cu_index (const void *item)
20258 {
20259 const struct psymtab_cu_index_map *map = item;
20260
20261 return htab_hash_pointer (map->psymtab);
20262 }
20263
20264 static int
20265 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
20266 {
20267 const struct psymtab_cu_index_map *lhs = item_lhs;
20268 const struct psymtab_cu_index_map *rhs = item_rhs;
20269
20270 return lhs->psymtab == rhs->psymtab;
20271 }
20272
20273 /* Helper struct for building the address table. */
20274 struct addrmap_index_data
20275 {
20276 struct objfile *objfile;
20277 struct obstack *addr_obstack;
20278 htab_t cu_index_htab;
20279
20280 /* Non-zero if the previous_* fields are valid.
20281 We can't write an entry until we see the next entry (since it is only then
20282 that we know the end of the entry). */
20283 int previous_valid;
20284 /* Index of the CU in the table of all CUs in the index file. */
20285 unsigned int previous_cu_index;
20286 /* Start address of the CU. */
20287 CORE_ADDR previous_cu_start;
20288 };
20289
20290 /* Write an address entry to OBSTACK. */
20291
20292 static void
20293 add_address_entry (struct objfile *objfile, struct obstack *obstack,
20294 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
20295 {
20296 offset_type cu_index_to_write;
20297 char addr[8];
20298 CORE_ADDR baseaddr;
20299
20300 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20301
20302 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
20303 obstack_grow (obstack, addr, 8);
20304 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
20305 obstack_grow (obstack, addr, 8);
20306 cu_index_to_write = MAYBE_SWAP (cu_index);
20307 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
20308 }
20309
20310 /* Worker function for traversing an addrmap to build the address table. */
20311
20312 static int
20313 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
20314 {
20315 struct addrmap_index_data *data = datap;
20316 struct partial_symtab *pst = obj;
20317
20318 if (data->previous_valid)
20319 add_address_entry (data->objfile, data->addr_obstack,
20320 data->previous_cu_start, start_addr,
20321 data->previous_cu_index);
20322
20323 data->previous_cu_start = start_addr;
20324 if (pst != NULL)
20325 {
20326 struct psymtab_cu_index_map find_map, *map;
20327 find_map.psymtab = pst;
20328 map = htab_find (data->cu_index_htab, &find_map);
20329 gdb_assert (map != NULL);
20330 data->previous_cu_index = map->cu_index;
20331 data->previous_valid = 1;
20332 }
20333 else
20334 data->previous_valid = 0;
20335
20336 return 0;
20337 }
20338
20339 /* Write OBJFILE's address map to OBSTACK.
20340 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
20341 in the index file. */
20342
20343 static void
20344 write_address_map (struct objfile *objfile, struct obstack *obstack,
20345 htab_t cu_index_htab)
20346 {
20347 struct addrmap_index_data addrmap_index_data;
20348
20349 /* When writing the address table, we have to cope with the fact that
20350 the addrmap iterator only provides the start of a region; we have to
20351 wait until the next invocation to get the start of the next region. */
20352
20353 addrmap_index_data.objfile = objfile;
20354 addrmap_index_data.addr_obstack = obstack;
20355 addrmap_index_data.cu_index_htab = cu_index_htab;
20356 addrmap_index_data.previous_valid = 0;
20357
20358 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
20359 &addrmap_index_data);
20360
20361 /* It's highly unlikely the last entry (end address = 0xff...ff)
20362 is valid, but we should still handle it.
20363 The end address is recorded as the start of the next region, but that
20364 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
20365 anyway. */
20366 if (addrmap_index_data.previous_valid)
20367 add_address_entry (objfile, obstack,
20368 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
20369 addrmap_index_data.previous_cu_index);
20370 }
20371
20372 /* Return the symbol kind of PSYM. */
20373
20374 static gdb_index_symbol_kind
20375 symbol_kind (struct partial_symbol *psym)
20376 {
20377 domain_enum domain = PSYMBOL_DOMAIN (psym);
20378 enum address_class aclass = PSYMBOL_CLASS (psym);
20379
20380 switch (domain)
20381 {
20382 case VAR_DOMAIN:
20383 switch (aclass)
20384 {
20385 case LOC_BLOCK:
20386 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
20387 case LOC_TYPEDEF:
20388 return GDB_INDEX_SYMBOL_KIND_TYPE;
20389 case LOC_COMPUTED:
20390 case LOC_CONST_BYTES:
20391 case LOC_OPTIMIZED_OUT:
20392 case LOC_STATIC:
20393 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20394 case LOC_CONST:
20395 /* Note: It's currently impossible to recognize psyms as enum values
20396 short of reading the type info. For now punt. */
20397 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20398 default:
20399 /* There are other LOC_FOO values that one might want to classify
20400 as variables, but dwarf2read.c doesn't currently use them. */
20401 return GDB_INDEX_SYMBOL_KIND_OTHER;
20402 }
20403 case STRUCT_DOMAIN:
20404 return GDB_INDEX_SYMBOL_KIND_TYPE;
20405 default:
20406 return GDB_INDEX_SYMBOL_KIND_OTHER;
20407 }
20408 }
20409
20410 /* Add a list of partial symbols to SYMTAB. */
20411
20412 static void
20413 write_psymbols (struct mapped_symtab *symtab,
20414 htab_t psyms_seen,
20415 struct partial_symbol **psymp,
20416 int count,
20417 offset_type cu_index,
20418 int is_static)
20419 {
20420 for (; count-- > 0; ++psymp)
20421 {
20422 struct partial_symbol *psym = *psymp;
20423 void **slot;
20424
20425 if (SYMBOL_LANGUAGE (psym) == language_ada)
20426 error (_("Ada is not currently supported by the index"));
20427
20428 /* Only add a given psymbol once. */
20429 slot = htab_find_slot (psyms_seen, psym, INSERT);
20430 if (!*slot)
20431 {
20432 gdb_index_symbol_kind kind = symbol_kind (psym);
20433
20434 *slot = psym;
20435 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
20436 is_static, kind, cu_index);
20437 }
20438 }
20439 }
20440
20441 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
20442 exception if there is an error. */
20443
20444 static void
20445 write_obstack (FILE *file, struct obstack *obstack)
20446 {
20447 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
20448 file)
20449 != obstack_object_size (obstack))
20450 error (_("couldn't data write to file"));
20451 }
20452
20453 /* Unlink a file if the argument is not NULL. */
20454
20455 static void
20456 unlink_if_set (void *p)
20457 {
20458 char **filename = p;
20459 if (*filename)
20460 unlink (*filename);
20461 }
20462
20463 /* A helper struct used when iterating over debug_types. */
20464 struct signatured_type_index_data
20465 {
20466 struct objfile *objfile;
20467 struct mapped_symtab *symtab;
20468 struct obstack *types_list;
20469 htab_t psyms_seen;
20470 int cu_index;
20471 };
20472
20473 /* A helper function that writes a single signatured_type to an
20474 obstack. */
20475
20476 static int
20477 write_one_signatured_type (void **slot, void *d)
20478 {
20479 struct signatured_type_index_data *info = d;
20480 struct signatured_type *entry = (struct signatured_type *) *slot;
20481 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
20482 gdb_byte val[8];
20483
20484 write_psymbols (info->symtab,
20485 info->psyms_seen,
20486 info->objfile->global_psymbols.list
20487 + psymtab->globals_offset,
20488 psymtab->n_global_syms, info->cu_index,
20489 0);
20490 write_psymbols (info->symtab,
20491 info->psyms_seen,
20492 info->objfile->static_psymbols.list
20493 + psymtab->statics_offset,
20494 psymtab->n_static_syms, info->cu_index,
20495 1);
20496
20497 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20498 entry->per_cu.offset.sect_off);
20499 obstack_grow (info->types_list, val, 8);
20500 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20501 entry->type_offset_in_tu.cu_off);
20502 obstack_grow (info->types_list, val, 8);
20503 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
20504 obstack_grow (info->types_list, val, 8);
20505
20506 ++info->cu_index;
20507
20508 return 1;
20509 }
20510
20511 /* Recurse into all "included" dependencies and write their symbols as
20512 if they appeared in this psymtab. */
20513
20514 static void
20515 recursively_write_psymbols (struct objfile *objfile,
20516 struct partial_symtab *psymtab,
20517 struct mapped_symtab *symtab,
20518 htab_t psyms_seen,
20519 offset_type cu_index)
20520 {
20521 int i;
20522
20523 for (i = 0; i < psymtab->number_of_dependencies; ++i)
20524 if (psymtab->dependencies[i]->user != NULL)
20525 recursively_write_psymbols (objfile, psymtab->dependencies[i],
20526 symtab, psyms_seen, cu_index);
20527
20528 write_psymbols (symtab,
20529 psyms_seen,
20530 objfile->global_psymbols.list + psymtab->globals_offset,
20531 psymtab->n_global_syms, cu_index,
20532 0);
20533 write_psymbols (symtab,
20534 psyms_seen,
20535 objfile->static_psymbols.list + psymtab->statics_offset,
20536 psymtab->n_static_syms, cu_index,
20537 1);
20538 }
20539
20540 /* Create an index file for OBJFILE in the directory DIR. */
20541
20542 static void
20543 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
20544 {
20545 struct cleanup *cleanup;
20546 char *filename, *cleanup_filename;
20547 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
20548 struct obstack cu_list, types_cu_list;
20549 int i;
20550 FILE *out_file;
20551 struct mapped_symtab *symtab;
20552 offset_type val, size_of_contents, total_len;
20553 struct stat st;
20554 htab_t psyms_seen;
20555 htab_t cu_index_htab;
20556 struct psymtab_cu_index_map *psymtab_cu_index_map;
20557
20558 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
20559 return;
20560
20561 if (dwarf2_per_objfile->using_index)
20562 error (_("Cannot use an index to create the index"));
20563
20564 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
20565 error (_("Cannot make an index when the file has multiple .debug_types sections"));
20566
20567 if (stat (objfile->name, &st) < 0)
20568 perror_with_name (objfile->name);
20569
20570 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
20571 INDEX_SUFFIX, (char *) NULL);
20572 cleanup = make_cleanup (xfree, filename);
20573
20574 out_file = fopen (filename, "wb");
20575 if (!out_file)
20576 error (_("Can't open `%s' for writing"), filename);
20577
20578 cleanup_filename = filename;
20579 make_cleanup (unlink_if_set, &cleanup_filename);
20580
20581 symtab = create_mapped_symtab ();
20582 make_cleanup (cleanup_mapped_symtab, symtab);
20583
20584 obstack_init (&addr_obstack);
20585 make_cleanup_obstack_free (&addr_obstack);
20586
20587 obstack_init (&cu_list);
20588 make_cleanup_obstack_free (&cu_list);
20589
20590 obstack_init (&types_cu_list);
20591 make_cleanup_obstack_free (&types_cu_list);
20592
20593 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
20594 NULL, xcalloc, xfree);
20595 make_cleanup_htab_delete (psyms_seen);
20596
20597 /* While we're scanning CU's create a table that maps a psymtab pointer
20598 (which is what addrmap records) to its index (which is what is recorded
20599 in the index file). This will later be needed to write the address
20600 table. */
20601 cu_index_htab = htab_create_alloc (100,
20602 hash_psymtab_cu_index,
20603 eq_psymtab_cu_index,
20604 NULL, xcalloc, xfree);
20605 make_cleanup_htab_delete (cu_index_htab);
20606 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
20607 xmalloc (sizeof (struct psymtab_cu_index_map)
20608 * dwarf2_per_objfile->n_comp_units);
20609 make_cleanup (xfree, psymtab_cu_index_map);
20610
20611 /* The CU list is already sorted, so we don't need to do additional
20612 work here. Also, the debug_types entries do not appear in
20613 all_comp_units, but only in their own hash table. */
20614 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
20615 {
20616 struct dwarf2_per_cu_data *per_cu
20617 = dwarf2_per_objfile->all_comp_units[i];
20618 struct partial_symtab *psymtab = per_cu->v.psymtab;
20619 gdb_byte val[8];
20620 struct psymtab_cu_index_map *map;
20621 void **slot;
20622
20623 if (psymtab->user == NULL)
20624 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
20625
20626 map = &psymtab_cu_index_map[i];
20627 map->psymtab = psymtab;
20628 map->cu_index = i;
20629 slot = htab_find_slot (cu_index_htab, map, INSERT);
20630 gdb_assert (slot != NULL);
20631 gdb_assert (*slot == NULL);
20632 *slot = map;
20633
20634 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20635 per_cu->offset.sect_off);
20636 obstack_grow (&cu_list, val, 8);
20637 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
20638 obstack_grow (&cu_list, val, 8);
20639 }
20640
20641 /* Dump the address map. */
20642 write_address_map (objfile, &addr_obstack, cu_index_htab);
20643
20644 /* Write out the .debug_type entries, if any. */
20645 if (dwarf2_per_objfile->signatured_types)
20646 {
20647 struct signatured_type_index_data sig_data;
20648
20649 sig_data.objfile = objfile;
20650 sig_data.symtab = symtab;
20651 sig_data.types_list = &types_cu_list;
20652 sig_data.psyms_seen = psyms_seen;
20653 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
20654 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
20655 write_one_signatured_type, &sig_data);
20656 }
20657
20658 /* Now that we've processed all symbols we can shrink their cu_indices
20659 lists. */
20660 uniquify_cu_indices (symtab);
20661
20662 obstack_init (&constant_pool);
20663 make_cleanup_obstack_free (&constant_pool);
20664 obstack_init (&symtab_obstack);
20665 make_cleanup_obstack_free (&symtab_obstack);
20666 write_hash_table (symtab, &symtab_obstack, &constant_pool);
20667
20668 obstack_init (&contents);
20669 make_cleanup_obstack_free (&contents);
20670 size_of_contents = 6 * sizeof (offset_type);
20671 total_len = size_of_contents;
20672
20673 /* The version number. */
20674 val = MAYBE_SWAP (8);
20675 obstack_grow (&contents, &val, sizeof (val));
20676
20677 /* The offset of the CU list from the start of the file. */
20678 val = MAYBE_SWAP (total_len);
20679 obstack_grow (&contents, &val, sizeof (val));
20680 total_len += obstack_object_size (&cu_list);
20681
20682 /* The offset of the types CU list from the start of the file. */
20683 val = MAYBE_SWAP (total_len);
20684 obstack_grow (&contents, &val, sizeof (val));
20685 total_len += obstack_object_size (&types_cu_list);
20686
20687 /* The offset of the address table from the start of the file. */
20688 val = MAYBE_SWAP (total_len);
20689 obstack_grow (&contents, &val, sizeof (val));
20690 total_len += obstack_object_size (&addr_obstack);
20691
20692 /* The offset of the symbol table from the start of the file. */
20693 val = MAYBE_SWAP (total_len);
20694 obstack_grow (&contents, &val, sizeof (val));
20695 total_len += obstack_object_size (&symtab_obstack);
20696
20697 /* The offset of the constant pool from the start of the file. */
20698 val = MAYBE_SWAP (total_len);
20699 obstack_grow (&contents, &val, sizeof (val));
20700 total_len += obstack_object_size (&constant_pool);
20701
20702 gdb_assert (obstack_object_size (&contents) == size_of_contents);
20703
20704 write_obstack (out_file, &contents);
20705 write_obstack (out_file, &cu_list);
20706 write_obstack (out_file, &types_cu_list);
20707 write_obstack (out_file, &addr_obstack);
20708 write_obstack (out_file, &symtab_obstack);
20709 write_obstack (out_file, &constant_pool);
20710
20711 fclose (out_file);
20712
20713 /* We want to keep the file, so we set cleanup_filename to NULL
20714 here. See unlink_if_set. */
20715 cleanup_filename = NULL;
20716
20717 do_cleanups (cleanup);
20718 }
20719
20720 /* Implementation of the `save gdb-index' command.
20721
20722 Note that the file format used by this command is documented in the
20723 GDB manual. Any changes here must be documented there. */
20724
20725 static void
20726 save_gdb_index_command (char *arg, int from_tty)
20727 {
20728 struct objfile *objfile;
20729
20730 if (!arg || !*arg)
20731 error (_("usage: save gdb-index DIRECTORY"));
20732
20733 ALL_OBJFILES (objfile)
20734 {
20735 struct stat st;
20736
20737 /* If the objfile does not correspond to an actual file, skip it. */
20738 if (stat (objfile->name, &st) < 0)
20739 continue;
20740
20741 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
20742 if (dwarf2_per_objfile)
20743 {
20744 volatile struct gdb_exception except;
20745
20746 TRY_CATCH (except, RETURN_MASK_ERROR)
20747 {
20748 write_psymtabs_to_index (objfile, arg);
20749 }
20750 if (except.reason < 0)
20751 exception_fprintf (gdb_stderr, except,
20752 _("Error while writing index for `%s': "),
20753 objfile->name);
20754 }
20755 }
20756 }
20757
20758 \f
20759
20760 int dwarf2_always_disassemble;
20761
20762 static void
20763 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
20764 struct cmd_list_element *c, const char *value)
20765 {
20766 fprintf_filtered (file,
20767 _("Whether to always disassemble "
20768 "DWARF expressions is %s.\n"),
20769 value);
20770 }
20771
20772 static void
20773 show_check_physname (struct ui_file *file, int from_tty,
20774 struct cmd_list_element *c, const char *value)
20775 {
20776 fprintf_filtered (file,
20777 _("Whether to check \"physname\" is %s.\n"),
20778 value);
20779 }
20780
20781 void _initialize_dwarf2_read (void);
20782
20783 void
20784 _initialize_dwarf2_read (void)
20785 {
20786 struct cmd_list_element *c;
20787
20788 dwarf2_objfile_data_key
20789 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
20790
20791 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
20792 Set DWARF 2 specific variables.\n\
20793 Configure DWARF 2 variables such as the cache size"),
20794 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
20795 0/*allow-unknown*/, &maintenance_set_cmdlist);
20796
20797 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
20798 Show DWARF 2 specific variables\n\
20799 Show DWARF 2 variables such as the cache size"),
20800 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
20801 0/*allow-unknown*/, &maintenance_show_cmdlist);
20802
20803 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
20804 &dwarf2_max_cache_age, _("\
20805 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
20806 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
20807 A higher limit means that cached compilation units will be stored\n\
20808 in memory longer, and more total memory will be used. Zero disables\n\
20809 caching, which can slow down startup."),
20810 NULL,
20811 show_dwarf2_max_cache_age,
20812 &set_dwarf2_cmdlist,
20813 &show_dwarf2_cmdlist);
20814
20815 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
20816 &dwarf2_always_disassemble, _("\
20817 Set whether `info address' always disassembles DWARF expressions."), _("\
20818 Show whether `info address' always disassembles DWARF expressions."), _("\
20819 When enabled, DWARF expressions are always printed in an assembly-like\n\
20820 syntax. When disabled, expressions will be printed in a more\n\
20821 conversational style, when possible."),
20822 NULL,
20823 show_dwarf2_always_disassemble,
20824 &set_dwarf2_cmdlist,
20825 &show_dwarf2_cmdlist);
20826
20827 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
20828 Set debugging of the dwarf2 reader."), _("\
20829 Show debugging of the dwarf2 reader."), _("\
20830 When enabled, debugging messages are printed during dwarf2 reading\n\
20831 and symtab expansion."),
20832 NULL,
20833 NULL,
20834 &setdebuglist, &showdebuglist);
20835
20836 add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
20837 Set debugging of the dwarf2 DIE reader."), _("\
20838 Show debugging of the dwarf2 DIE reader."), _("\
20839 When enabled (non-zero), DIEs are dumped after they are read in.\n\
20840 The value is the maximum depth to print."),
20841 NULL,
20842 NULL,
20843 &setdebuglist, &showdebuglist);
20844
20845 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
20846 Set cross-checking of \"physname\" code against demangler."), _("\
20847 Show cross-checking of \"physname\" code against demangler."), _("\
20848 When enabled, GDB's internal \"physname\" code is checked against\n\
20849 the demangler."),
20850 NULL, show_check_physname,
20851 &setdebuglist, &showdebuglist);
20852
20853 add_setshow_boolean_cmd ("use-deprecated-index-sections",
20854 no_class, &use_deprecated_index_sections, _("\
20855 Set whether to use deprecated gdb_index sections."), _("\
20856 Show whether to use deprecated gdb_index sections."), _("\
20857 When enabled, deprecated .gdb_index sections are used anyway.\n\
20858 Normally they are ignored either because of a missing feature or\n\
20859 performance issue.\n\
20860 Warning: This option must be enabled before gdb reads the file."),
20861 NULL,
20862 NULL,
20863 &setlist, &showlist);
20864
20865 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
20866 _("\
20867 Save a gdb-index file.\n\
20868 Usage: save gdb-index DIRECTORY"),
20869 &save_cmdlist);
20870 set_cmd_completer (c, filename_completer);
20871
20872 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
20873 &dwarf2_locexpr_funcs);
20874 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
20875 &dwarf2_loclist_funcs);
20876
20877 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
20878 &dwarf2_block_frame_base_locexpr_funcs);
20879 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
20880 &dwarf2_block_frame_base_loclist_funcs);
20881 }
This page took 0.434148 seconds and 5 git commands to generate.