* ada-lang.c (ada_decode_symbol): Check and set 'ada_mangled'.
[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 + 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 Reminder: We can't read the abbrev table until we've read the header. */
4531 if (abbrev_table_provided)
4532 {
4533 /* Don't free the provided abbrev table, the caller of
4534 init_cutu_and_read_dies owns it. */
4535 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4536 /* Ensure the DWO abbrev table gets freed. */
4537 make_cleanup (dwarf2_free_abbrev_table, cu);
4538 }
4539 else
4540 {
4541 dwarf2_free_abbrev_table (cu);
4542 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4543 /* Leave any existing abbrev table cleanup as is. */
4544 }
4545
4546 /* Read in the die, but leave space to copy over the attributes
4547 from the stub. This has the benefit of simplifying the rest of
4548 the code - all the work to maintain the illusion of a single
4549 DW_TAG_{compile,type}_unit DIE is done here. */
4550 num_extra_attrs = ((stmt_list != NULL)
4551 + (low_pc != NULL)
4552 + (high_pc != NULL)
4553 + (ranges != NULL)
4554 + (comp_dir != NULL));
4555 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
4556 result_has_children, num_extra_attrs);
4557
4558 /* Copy over the attributes from the stub to the DIE we just read in. */
4559 comp_unit_die = *result_comp_unit_die;
4560 i = comp_unit_die->num_attrs;
4561 if (stmt_list != NULL)
4562 comp_unit_die->attrs[i++] = *stmt_list;
4563 if (low_pc != NULL)
4564 comp_unit_die->attrs[i++] = *low_pc;
4565 if (high_pc != NULL)
4566 comp_unit_die->attrs[i++] = *high_pc;
4567 if (ranges != NULL)
4568 comp_unit_die->attrs[i++] = *ranges;
4569 if (comp_dir != NULL)
4570 comp_unit_die->attrs[i++] = *comp_dir;
4571 comp_unit_die->num_attrs += num_extra_attrs;
4572
4573 if (dwarf2_die_debug)
4574 {
4575 fprintf_unfiltered (gdb_stdlog,
4576 "Read die from %s@0x%x of %s:\n",
4577 bfd_section_name (abfd, section->asection),
4578 (unsigned) (begin_info_ptr - section->buffer),
4579 bfd_get_filename (abfd));
4580 dump_die (comp_unit_die, dwarf2_die_debug);
4581 }
4582
4583 /* Skip dummy compilation units. */
4584 if (info_ptr >= begin_info_ptr + dwo_unit->length
4585 || peek_abbrev_code (abfd, info_ptr) == 0)
4586 return 0;
4587
4588 *result_info_ptr = info_ptr;
4589 return 1;
4590 }
4591
4592 /* Subroutine of init_cutu_and_read_dies to simplify it.
4593 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
4594 If the specified DWO unit cannot be found an error is thrown. */
4595
4596 static struct dwo_unit *
4597 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
4598 struct die_info *comp_unit_die)
4599 {
4600 struct dwarf2_cu *cu = this_cu->cu;
4601 struct attribute *attr;
4602 ULONGEST signature;
4603 struct dwo_unit *dwo_unit;
4604 const char *comp_dir, *dwo_name;
4605
4606 /* Yeah, we look dwo_name up again, but it simplifies the code. */
4607 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4608 gdb_assert (attr != NULL);
4609 dwo_name = DW_STRING (attr);
4610 comp_dir = NULL;
4611 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4612 if (attr)
4613 comp_dir = DW_STRING (attr);
4614
4615 if (this_cu->is_debug_types)
4616 {
4617 struct signatured_type *sig_type;
4618
4619 /* Since this_cu is the first member of struct signatured_type,
4620 we can go from a pointer to one to a pointer to the other. */
4621 sig_type = (struct signatured_type *) this_cu;
4622 signature = sig_type->signature;
4623 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
4624 }
4625 else
4626 {
4627 struct attribute *attr;
4628
4629 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4630 if (! attr)
4631 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
4632 " [in module %s]"),
4633 dwo_name, this_cu->objfile->name);
4634 signature = DW_UNSND (attr);
4635 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
4636 signature);
4637 }
4638
4639 if (dwo_unit == NULL)
4640 {
4641 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
4642 " with ID %s [in module %s]"),
4643 this_cu->offset.sect_off,
4644 phex (signature, sizeof (signature)),
4645 this_cu->objfile->name);
4646 }
4647
4648 return dwo_unit;
4649 }
4650
4651 /* Initialize a CU (or TU) and read its DIEs.
4652 If the CU defers to a DWO file, read the DWO file as well.
4653
4654 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4655 Otherwise the table specified in the comp unit header is read in and used.
4656 This is an optimization for when we already have the abbrev table.
4657
4658 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4659 Otherwise, a new CU is allocated with xmalloc.
4660
4661 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4662 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
4663
4664 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4665 linker) then DIE_READER_FUNC will not get called. */
4666
4667 static void
4668 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
4669 struct abbrev_table *abbrev_table,
4670 int use_existing_cu, int keep,
4671 die_reader_func_ftype *die_reader_func,
4672 void *data)
4673 {
4674 struct objfile *objfile = dwarf2_per_objfile->objfile;
4675 struct dwarf2_section_info *section = this_cu->section;
4676 bfd *abfd = section->asection->owner;
4677 struct dwarf2_cu *cu;
4678 gdb_byte *begin_info_ptr, *info_ptr;
4679 struct die_reader_specs reader;
4680 struct die_info *comp_unit_die;
4681 int has_children;
4682 struct attribute *attr;
4683 struct cleanup *cleanups, *free_cu_cleanup = NULL;
4684 struct signatured_type *sig_type = NULL;
4685 struct dwarf2_section_info *abbrev_section;
4686 /* Non-zero if CU currently points to a DWO file and we need to
4687 reread it. When this happens we need to reread the skeleton die
4688 before we can reread the DWO file. */
4689 int rereading_dwo_cu = 0;
4690
4691 if (dwarf2_die_debug)
4692 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4693 this_cu->is_debug_types ? "type" : "comp",
4694 this_cu->offset.sect_off);
4695
4696 if (use_existing_cu)
4697 gdb_assert (keep);
4698
4699 cleanups = make_cleanup (null_cleanup, NULL);
4700
4701 /* This is cheap if the section is already read in. */
4702 dwarf2_read_section (objfile, section);
4703
4704 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4705
4706 abbrev_section = get_abbrev_section_for_cu (this_cu);
4707
4708 if (use_existing_cu && this_cu->cu != NULL)
4709 {
4710 cu = this_cu->cu;
4711
4712 /* If this CU is from a DWO file we need to start over, we need to
4713 refetch the attributes from the skeleton CU.
4714 This could be optimized by retrieving those attributes from when we
4715 were here the first time: the previous comp_unit_die was stored in
4716 comp_unit_obstack. But there's no data yet that we need this
4717 optimization. */
4718 if (cu->dwo_unit != NULL)
4719 rereading_dwo_cu = 1;
4720 }
4721 else
4722 {
4723 /* If !use_existing_cu, this_cu->cu must be NULL. */
4724 gdb_assert (this_cu->cu == NULL);
4725
4726 cu = xmalloc (sizeof (*cu));
4727 init_one_comp_unit (cu, this_cu);
4728
4729 /* If an error occurs while loading, release our storage. */
4730 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
4731 }
4732
4733 /* Get the header. */
4734 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
4735 {
4736 /* We already have the header, there's no need to read it in again. */
4737 info_ptr += cu->header.first_die_offset.cu_off;
4738 }
4739 else
4740 {
4741 if (this_cu->is_debug_types)
4742 {
4743 ULONGEST signature;
4744 cu_offset type_offset_in_tu;
4745
4746 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4747 abbrev_section, info_ptr,
4748 &signature,
4749 &type_offset_in_tu);
4750
4751 /* Since per_cu is the first member of struct signatured_type,
4752 we can go from a pointer to one to a pointer to the other. */
4753 sig_type = (struct signatured_type *) this_cu;
4754 gdb_assert (sig_type->signature == signature);
4755 gdb_assert (sig_type->type_offset_in_tu.cu_off
4756 == type_offset_in_tu.cu_off);
4757 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4758
4759 /* LENGTH has not been set yet for type units if we're
4760 using .gdb_index. */
4761 this_cu->length = get_cu_length (&cu->header);
4762
4763 /* Establish the type offset that can be used to lookup the type. */
4764 sig_type->type_offset_in_section.sect_off =
4765 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
4766 }
4767 else
4768 {
4769 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4770 abbrev_section,
4771 info_ptr, 0);
4772
4773 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4774 gdb_assert (this_cu->length == get_cu_length (&cu->header));
4775 }
4776 }
4777
4778 /* Skip dummy compilation units. */
4779 if (info_ptr >= begin_info_ptr + this_cu->length
4780 || peek_abbrev_code (abfd, info_ptr) == 0)
4781 {
4782 do_cleanups (cleanups);
4783 return;
4784 }
4785
4786 /* If we don't have them yet, read the abbrevs for this compilation unit.
4787 And if we need to read them now, make sure they're freed when we're
4788 done. Note that it's important that if the CU had an abbrev table
4789 on entry we don't free it when we're done: Somewhere up the call stack
4790 it may be in use. */
4791 if (abbrev_table != NULL)
4792 {
4793 gdb_assert (cu->abbrev_table == NULL);
4794 gdb_assert (cu->header.abbrev_offset.sect_off
4795 == abbrev_table->offset.sect_off);
4796 cu->abbrev_table = abbrev_table;
4797 }
4798 else if (cu->abbrev_table == NULL)
4799 {
4800 dwarf2_read_abbrevs (cu, abbrev_section);
4801 make_cleanup (dwarf2_free_abbrev_table, cu);
4802 }
4803 else if (rereading_dwo_cu)
4804 {
4805 dwarf2_free_abbrev_table (cu);
4806 dwarf2_read_abbrevs (cu, abbrev_section);
4807 }
4808
4809 /* Read the top level CU/TU die. */
4810 init_cu_die_reader (&reader, cu, section, NULL);
4811 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4812
4813 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
4814 from the DWO file.
4815 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
4816 DWO CU, that this test will fail (the attribute will not be present). */
4817 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4818 if (attr)
4819 {
4820 struct dwo_unit *dwo_unit;
4821 struct die_info *dwo_comp_unit_die;
4822
4823 if (has_children)
4824 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
4825 " has children (offset 0x%x) [in module %s]"),
4826 this_cu->offset.sect_off, bfd_get_filename (abfd));
4827 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
4828 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
4829 abbrev_table != NULL,
4830 comp_unit_die,
4831 &reader, &info_ptr,
4832 &dwo_comp_unit_die, &has_children) == 0)
4833 {
4834 /* Dummy die. */
4835 do_cleanups (cleanups);
4836 return;
4837 }
4838 comp_unit_die = dwo_comp_unit_die;
4839 }
4840
4841 /* All of the above is setup for this call. Yikes. */
4842 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4843
4844 /* Done, clean up. */
4845 if (free_cu_cleanup != NULL)
4846 {
4847 if (keep)
4848 {
4849 /* We've successfully allocated this compilation unit. Let our
4850 caller clean it up when finished with it. */
4851 discard_cleanups (free_cu_cleanup);
4852
4853 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4854 So we have to manually free the abbrev table. */
4855 dwarf2_free_abbrev_table (cu);
4856
4857 /* Link this CU into read_in_chain. */
4858 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4859 dwarf2_per_objfile->read_in_chain = this_cu;
4860 }
4861 else
4862 do_cleanups (free_cu_cleanup);
4863 }
4864
4865 do_cleanups (cleanups);
4866 }
4867
4868 /* Read CU/TU THIS_CU in section SECTION,
4869 but do not follow DW_AT_GNU_dwo_name if present.
4870 DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
4871 to have already done the lookup to find the DWO/DWP file).
4872
4873 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
4874 THIS_CU->is_debug_types, but nothing else.
4875
4876 We fill in THIS_CU->length.
4877
4878 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4879 linker) then DIE_READER_FUNC will not get called.
4880
4881 THIS_CU->cu is always freed when done.
4882 This is done in order to not leave THIS_CU->cu in a state where we have
4883 to care whether it refers to the "main" CU or the DWO CU. */
4884
4885 static void
4886 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4887 struct dwarf2_section_info *abbrev_section,
4888 struct dwo_file *dwo_file,
4889 die_reader_func_ftype *die_reader_func,
4890 void *data)
4891 {
4892 struct objfile *objfile = dwarf2_per_objfile->objfile;
4893 struct dwarf2_section_info *section = this_cu->section;
4894 bfd *abfd = section->asection->owner;
4895 struct dwarf2_cu cu;
4896 gdb_byte *begin_info_ptr, *info_ptr;
4897 struct die_reader_specs reader;
4898 struct cleanup *cleanups;
4899 struct die_info *comp_unit_die;
4900 int has_children;
4901
4902 if (dwarf2_die_debug)
4903 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4904 this_cu->is_debug_types ? "type" : "comp",
4905 this_cu->offset.sect_off);
4906
4907 gdb_assert (this_cu->cu == NULL);
4908
4909 /* This is cheap if the section is already read in. */
4910 dwarf2_read_section (objfile, section);
4911
4912 init_one_comp_unit (&cu, this_cu);
4913
4914 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4915
4916 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4917 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
4918 abbrev_section, info_ptr,
4919 this_cu->is_debug_types);
4920
4921 this_cu->length = get_cu_length (&cu.header);
4922
4923 /* Skip dummy compilation units. */
4924 if (info_ptr >= begin_info_ptr + this_cu->length
4925 || peek_abbrev_code (abfd, info_ptr) == 0)
4926 {
4927 do_cleanups (cleanups);
4928 return;
4929 }
4930
4931 dwarf2_read_abbrevs (&cu, abbrev_section);
4932 make_cleanup (dwarf2_free_abbrev_table, &cu);
4933
4934 init_cu_die_reader (&reader, &cu, section, dwo_file);
4935 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4936
4937 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4938
4939 do_cleanups (cleanups);
4940 }
4941
4942 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4943 does not lookup the specified DWO file.
4944 This cannot be used to read DWO files.
4945
4946 THIS_CU->cu is always freed when done.
4947 This is done in order to not leave THIS_CU->cu in a state where we have
4948 to care whether it refers to the "main" CU or the DWO CU.
4949 We can revisit this if the data shows there's a performance issue. */
4950
4951 static void
4952 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4953 die_reader_func_ftype *die_reader_func,
4954 void *data)
4955 {
4956 init_cutu_and_read_dies_no_follow (this_cu,
4957 get_abbrev_section_for_cu (this_cu),
4958 NULL,
4959 die_reader_func, data);
4960 }
4961 \f
4962 /* Type Unit Groups.
4963
4964 Type Unit Groups are a way to collapse the set of all TUs (type units) into
4965 a more manageable set. The grouping is done by DW_AT_stmt_list entry
4966 so that all types coming from the same compilation (.o file) are grouped
4967 together. A future step could be to put the types in the same symtab as
4968 the CU the types ultimately came from. */
4969
4970 static hashval_t
4971 hash_type_unit_group (const void *item)
4972 {
4973 const struct type_unit_group *tu_group = item;
4974
4975 return hash_stmt_list_entry (&tu_group->hash);
4976 }
4977
4978 static int
4979 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
4980 {
4981 const struct type_unit_group *lhs = item_lhs;
4982 const struct type_unit_group *rhs = item_rhs;
4983
4984 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
4985 }
4986
4987 /* Allocate a hash table for type unit groups. */
4988
4989 static htab_t
4990 allocate_type_unit_groups_table (void)
4991 {
4992 return htab_create_alloc_ex (3,
4993 hash_type_unit_group,
4994 eq_type_unit_group,
4995 NULL,
4996 &dwarf2_per_objfile->objfile->objfile_obstack,
4997 hashtab_obstack_allocate,
4998 dummy_obstack_deallocate);
4999 }
5000
5001 /* Type units that don't have DW_AT_stmt_list are grouped into their own
5002 partial symtabs. We combine several TUs per psymtab to not let the size
5003 of any one psymtab grow too big. */
5004 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5005 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
5006
5007 /* Helper routine for get_type_unit_group.
5008 Create the type_unit_group object used to hold one or more TUs. */
5009
5010 static struct type_unit_group *
5011 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
5012 {
5013 struct objfile *objfile = dwarf2_per_objfile->objfile;
5014 struct dwarf2_per_cu_data *per_cu;
5015 struct type_unit_group *tu_group;
5016
5017 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5018 struct type_unit_group);
5019 per_cu = &tu_group->per_cu;
5020 per_cu->objfile = objfile;
5021
5022 if (dwarf2_per_objfile->using_index)
5023 {
5024 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5025 struct dwarf2_per_cu_quick_data);
5026 }
5027 else
5028 {
5029 unsigned int line_offset = line_offset_struct.sect_off;
5030 struct partial_symtab *pst;
5031 char *name;
5032
5033 /* Give the symtab a useful name for debug purposes. */
5034 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5035 name = xstrprintf ("<type_units_%d>",
5036 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5037 else
5038 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5039
5040 pst = create_partial_symtab (per_cu, name);
5041 pst->anonymous = 1;
5042
5043 xfree (name);
5044 }
5045
5046 tu_group->hash.dwo_unit = cu->dwo_unit;
5047 tu_group->hash.line_offset = line_offset_struct;
5048
5049 return tu_group;
5050 }
5051
5052 /* Look up the type_unit_group for type unit CU, and create it if necessary.
5053 STMT_LIST is a DW_AT_stmt_list attribute. */
5054
5055 static struct type_unit_group *
5056 get_type_unit_group (struct dwarf2_cu *cu, struct attribute *stmt_list)
5057 {
5058 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5059 struct type_unit_group *tu_group;
5060 void **slot;
5061 unsigned int line_offset;
5062 struct type_unit_group type_unit_group_for_lookup;
5063
5064 if (dwarf2_per_objfile->type_unit_groups == NULL)
5065 {
5066 dwarf2_per_objfile->type_unit_groups =
5067 allocate_type_unit_groups_table ();
5068 }
5069
5070 /* Do we need to create a new group, or can we use an existing one? */
5071
5072 if (stmt_list)
5073 {
5074 line_offset = DW_UNSND (stmt_list);
5075 ++tu_stats->nr_symtab_sharers;
5076 }
5077 else
5078 {
5079 /* Ugh, no stmt_list. Rare, but we have to handle it.
5080 We can do various things here like create one group per TU or
5081 spread them over multiple groups to split up the expansion work.
5082 To avoid worst case scenarios (too many groups or too large groups)
5083 we, umm, group them in bunches. */
5084 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5085 | (tu_stats->nr_stmt_less_type_units
5086 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5087 ++tu_stats->nr_stmt_less_type_units;
5088 }
5089
5090 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5091 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
5092 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5093 &type_unit_group_for_lookup, INSERT);
5094 if (*slot != NULL)
5095 {
5096 tu_group = *slot;
5097 gdb_assert (tu_group != NULL);
5098 }
5099 else
5100 {
5101 sect_offset line_offset_struct;
5102
5103 line_offset_struct.sect_off = line_offset;
5104 tu_group = create_type_unit_group (cu, line_offset_struct);
5105 *slot = tu_group;
5106 ++tu_stats->nr_symtabs;
5107 }
5108
5109 return tu_group;
5110 }
5111
5112 /* Struct used to sort TUs by their abbreviation table offset. */
5113
5114 struct tu_abbrev_offset
5115 {
5116 struct signatured_type *sig_type;
5117 sect_offset abbrev_offset;
5118 };
5119
5120 /* Helper routine for build_type_unit_groups, passed to qsort. */
5121
5122 static int
5123 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5124 {
5125 const struct tu_abbrev_offset * const *a = ap;
5126 const struct tu_abbrev_offset * const *b = bp;
5127 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5128 unsigned int boff = (*b)->abbrev_offset.sect_off;
5129
5130 return (aoff > boff) - (aoff < boff);
5131 }
5132
5133 /* A helper function to add a type_unit_group to a table. */
5134
5135 static int
5136 add_type_unit_group_to_table (void **slot, void *datum)
5137 {
5138 struct type_unit_group *tu_group = *slot;
5139 struct type_unit_group ***datap = datum;
5140
5141 **datap = tu_group;
5142 ++*datap;
5143
5144 return 1;
5145 }
5146
5147 /* Efficiently read all the type units, calling init_cutu_and_read_dies on
5148 each one passing FUNC,DATA.
5149
5150 The efficiency is because we sort TUs by the abbrev table they use and
5151 only read each abbrev table once. In one program there are 200K TUs
5152 sharing 8K abbrev tables.
5153
5154 The main purpose of this function is to support building the
5155 dwarf2_per_objfile->type_unit_groups table.
5156 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5157 can collapse the search space by grouping them by stmt_list.
5158 The savings can be significant, in the same program from above the 200K TUs
5159 share 8K stmt_list tables.
5160
5161 FUNC is expected to call get_type_unit_group, which will create the
5162 struct type_unit_group if necessary and add it to
5163 dwarf2_per_objfile->type_unit_groups. */
5164
5165 static void
5166 build_type_unit_groups (die_reader_func_ftype *func, void *data)
5167 {
5168 struct objfile *objfile = dwarf2_per_objfile->objfile;
5169 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5170 struct cleanup *cleanups;
5171 struct abbrev_table *abbrev_table;
5172 sect_offset abbrev_offset;
5173 struct tu_abbrev_offset *sorted_by_abbrev;
5174 struct type_unit_group **iter;
5175 int i;
5176
5177 /* It's up to the caller to not call us multiple times. */
5178 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5179
5180 if (dwarf2_per_objfile->n_type_units == 0)
5181 return;
5182
5183 /* TUs typically share abbrev tables, and there can be way more TUs than
5184 abbrev tables. Sort by abbrev table to reduce the number of times we
5185 read each abbrev table in.
5186 Alternatives are to punt or to maintain a cache of abbrev tables.
5187 This is simpler and efficient enough for now.
5188
5189 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5190 symtab to use). Typically TUs with the same abbrev offset have the same
5191 stmt_list value too so in practice this should work well.
5192
5193 The basic algorithm here is:
5194
5195 sort TUs by abbrev table
5196 for each TU with same abbrev table:
5197 read abbrev table if first user
5198 read TU top level DIE
5199 [IWBN if DWO skeletons had DW_AT_stmt_list]
5200 call FUNC */
5201
5202 if (dwarf2_read_debug)
5203 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5204
5205 /* Sort in a separate table to maintain the order of all_type_units
5206 for .gdb_index: TU indices directly index all_type_units. */
5207 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5208 dwarf2_per_objfile->n_type_units);
5209 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5210 {
5211 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5212
5213 sorted_by_abbrev[i].sig_type = sig_type;
5214 sorted_by_abbrev[i].abbrev_offset =
5215 read_abbrev_offset (sig_type->per_cu.section,
5216 sig_type->per_cu.offset);
5217 }
5218 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5219 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5220 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5221
5222 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5223 called any number of times, so we don't reset tu_stats here. */
5224
5225 abbrev_offset.sect_off = ~(unsigned) 0;
5226 abbrev_table = NULL;
5227 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5228
5229 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5230 {
5231 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5232
5233 /* Switch to the next abbrev table if necessary. */
5234 if (abbrev_table == NULL
5235 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5236 {
5237 if (abbrev_table != NULL)
5238 {
5239 abbrev_table_free (abbrev_table);
5240 /* Reset to NULL in case abbrev_table_read_table throws
5241 an error: abbrev_table_free_cleanup will get called. */
5242 abbrev_table = NULL;
5243 }
5244 abbrev_offset = tu->abbrev_offset;
5245 abbrev_table =
5246 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5247 abbrev_offset);
5248 ++tu_stats->nr_uniq_abbrev_tables;
5249 }
5250
5251 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5252 func, data);
5253 }
5254
5255 /* Create a vector of pointers to primary type units to make it easy to
5256 iterate over them and CUs. See dw2_get_primary_cu. */
5257 dwarf2_per_objfile->n_type_unit_groups =
5258 htab_elements (dwarf2_per_objfile->type_unit_groups);
5259 dwarf2_per_objfile->all_type_unit_groups =
5260 obstack_alloc (&objfile->objfile_obstack,
5261 dwarf2_per_objfile->n_type_unit_groups
5262 * sizeof (struct type_unit_group *));
5263 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5264 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5265 add_type_unit_group_to_table, &iter);
5266 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5267 == dwarf2_per_objfile->n_type_unit_groups);
5268
5269 do_cleanups (cleanups);
5270
5271 if (dwarf2_read_debug)
5272 {
5273 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5274 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5275 dwarf2_per_objfile->n_type_units);
5276 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5277 tu_stats->nr_uniq_abbrev_tables);
5278 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5279 tu_stats->nr_symtabs);
5280 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5281 tu_stats->nr_symtab_sharers);
5282 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5283 tu_stats->nr_stmt_less_type_units);
5284 }
5285 }
5286 \f
5287 /* Partial symbol tables. */
5288
5289 /* Create a psymtab named NAME and assign it to PER_CU.
5290
5291 The caller must fill in the following details:
5292 dirname, textlow, texthigh. */
5293
5294 static struct partial_symtab *
5295 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
5296 {
5297 struct objfile *objfile = per_cu->objfile;
5298 struct partial_symtab *pst;
5299
5300 pst = start_psymtab_common (objfile, objfile->section_offsets,
5301 name, 0,
5302 objfile->global_psymbols.next,
5303 objfile->static_psymbols.next);
5304
5305 pst->psymtabs_addrmap_supported = 1;
5306
5307 /* This is the glue that links PST into GDB's symbol API. */
5308 pst->read_symtab_private = per_cu;
5309 pst->read_symtab = dwarf2_read_symtab;
5310 per_cu->v.psymtab = pst;
5311
5312 return pst;
5313 }
5314
5315 /* die_reader_func for process_psymtab_comp_unit. */
5316
5317 static void
5318 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
5319 gdb_byte *info_ptr,
5320 struct die_info *comp_unit_die,
5321 int has_children,
5322 void *data)
5323 {
5324 struct dwarf2_cu *cu = reader->cu;
5325 struct objfile *objfile = cu->objfile;
5326 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5327 struct attribute *attr;
5328 CORE_ADDR baseaddr;
5329 CORE_ADDR best_lowpc = 0, best_highpc = 0;
5330 struct partial_symtab *pst;
5331 int has_pc_info;
5332 const char *filename;
5333 int *want_partial_unit_ptr = data;
5334
5335 if (comp_unit_die->tag == DW_TAG_partial_unit
5336 && (want_partial_unit_ptr == NULL
5337 || !*want_partial_unit_ptr))
5338 return;
5339
5340 gdb_assert (! per_cu->is_debug_types);
5341
5342 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
5343
5344 cu->list_in_scope = &file_symbols;
5345
5346 /* Allocate a new partial symbol table structure. */
5347 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
5348 if (attr == NULL || !DW_STRING (attr))
5349 filename = "";
5350 else
5351 filename = DW_STRING (attr);
5352
5353 pst = create_partial_symtab (per_cu, filename);
5354
5355 /* This must be done before calling dwarf2_build_include_psymtabs. */
5356 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
5357 if (attr != NULL)
5358 pst->dirname = DW_STRING (attr);
5359
5360 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5361
5362 dwarf2_find_base_address (comp_unit_die, cu);
5363
5364 /* Possibly set the default values of LOWPC and HIGHPC from
5365 `DW_AT_ranges'. */
5366 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
5367 &best_highpc, cu, pst);
5368 if (has_pc_info == 1 && best_lowpc < best_highpc)
5369 /* Store the contiguous range if it is not empty; it can be empty for
5370 CUs with no code. */
5371 addrmap_set_empty (objfile->psymtabs_addrmap,
5372 best_lowpc + baseaddr,
5373 best_highpc + baseaddr - 1, pst);
5374
5375 /* Check if comp unit has_children.
5376 If so, read the rest of the partial symbols from this comp unit.
5377 If not, there's no more debug_info for this comp unit. */
5378 if (has_children)
5379 {
5380 struct partial_die_info *first_die;
5381 CORE_ADDR lowpc, highpc;
5382
5383 lowpc = ((CORE_ADDR) -1);
5384 highpc = ((CORE_ADDR) 0);
5385
5386 first_die = load_partial_dies (reader, info_ptr, 1);
5387
5388 scan_partial_symbols (first_die, &lowpc, &highpc,
5389 ! has_pc_info, cu);
5390
5391 /* If we didn't find a lowpc, set it to highpc to avoid
5392 complaints from `maint check'. */
5393 if (lowpc == ((CORE_ADDR) -1))
5394 lowpc = highpc;
5395
5396 /* If the compilation unit didn't have an explicit address range,
5397 then use the information extracted from its child dies. */
5398 if (! has_pc_info)
5399 {
5400 best_lowpc = lowpc;
5401 best_highpc = highpc;
5402 }
5403 }
5404 pst->textlow = best_lowpc + baseaddr;
5405 pst->texthigh = best_highpc + baseaddr;
5406
5407 pst->n_global_syms = objfile->global_psymbols.next -
5408 (objfile->global_psymbols.list + pst->globals_offset);
5409 pst->n_static_syms = objfile->static_psymbols.next -
5410 (objfile->static_psymbols.list + pst->statics_offset);
5411 sort_pst_symbols (objfile, pst);
5412
5413 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
5414 {
5415 int i;
5416 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5417 struct dwarf2_per_cu_data *iter;
5418
5419 /* Fill in 'dependencies' here; we fill in 'users' in a
5420 post-pass. */
5421 pst->number_of_dependencies = len;
5422 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5423 len * sizeof (struct symtab *));
5424 for (i = 0;
5425 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
5426 i, iter);
5427 ++i)
5428 pst->dependencies[i] = iter->v.psymtab;
5429
5430 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5431 }
5432
5433 /* Get the list of files included in the current compilation unit,
5434 and build a psymtab for each of them. */
5435 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
5436
5437 if (dwarf2_read_debug)
5438 {
5439 struct gdbarch *gdbarch = get_objfile_arch (objfile);
5440
5441 fprintf_unfiltered (gdb_stdlog,
5442 "Psymtab for %s unit @0x%x: %s - %s"
5443 ", %d global, %d static syms\n",
5444 per_cu->is_debug_types ? "type" : "comp",
5445 per_cu->offset.sect_off,
5446 paddress (gdbarch, pst->textlow),
5447 paddress (gdbarch, pst->texthigh),
5448 pst->n_global_syms, pst->n_static_syms);
5449 }
5450 }
5451
5452 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5453 Process compilation unit THIS_CU for a psymtab. */
5454
5455 static void
5456 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
5457 int want_partial_unit)
5458 {
5459 /* If this compilation unit was already read in, free the
5460 cached copy in order to read it in again. This is
5461 necessary because we skipped some symbols when we first
5462 read in the compilation unit (see load_partial_dies).
5463 This problem could be avoided, but the benefit is unclear. */
5464 if (this_cu->cu != NULL)
5465 free_one_cached_comp_unit (this_cu);
5466
5467 gdb_assert (! this_cu->is_debug_types);
5468 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
5469 process_psymtab_comp_unit_reader,
5470 &want_partial_unit);
5471
5472 /* Age out any secondary CUs. */
5473 age_cached_comp_units ();
5474 }
5475
5476 /* Reader function for build_type_psymtabs. */
5477
5478 static void
5479 build_type_psymtabs_reader (const struct die_reader_specs *reader,
5480 gdb_byte *info_ptr,
5481 struct die_info *type_unit_die,
5482 int has_children,
5483 void *data)
5484 {
5485 struct objfile *objfile = dwarf2_per_objfile->objfile;
5486 struct dwarf2_cu *cu = reader->cu;
5487 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5488 struct signatured_type *sig_type;
5489 struct type_unit_group *tu_group;
5490 struct attribute *attr;
5491 struct partial_die_info *first_die;
5492 CORE_ADDR lowpc, highpc;
5493 struct partial_symtab *pst;
5494
5495 gdb_assert (data == NULL);
5496 gdb_assert (per_cu->is_debug_types);
5497 sig_type = (struct signatured_type *) per_cu;
5498
5499 if (! has_children)
5500 return;
5501
5502 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
5503 tu_group = get_type_unit_group (cu, attr);
5504
5505 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
5506
5507 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5508 cu->list_in_scope = &file_symbols;
5509 pst = create_partial_symtab (per_cu, "");
5510 pst->anonymous = 1;
5511
5512 first_die = load_partial_dies (reader, info_ptr, 1);
5513
5514 lowpc = (CORE_ADDR) -1;
5515 highpc = (CORE_ADDR) 0;
5516 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5517
5518 pst->n_global_syms = objfile->global_psymbols.next -
5519 (objfile->global_psymbols.list + pst->globals_offset);
5520 pst->n_static_syms = objfile->static_psymbols.next -
5521 (objfile->static_psymbols.list + pst->statics_offset);
5522 sort_pst_symbols (objfile, pst);
5523 }
5524
5525 /* Traversal function for build_type_psymtabs. */
5526
5527 static int
5528 build_type_psymtab_dependencies (void **slot, void *info)
5529 {
5530 struct objfile *objfile = dwarf2_per_objfile->objfile;
5531 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
5532 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
5533 struct partial_symtab *pst = per_cu->v.psymtab;
5534 int len = VEC_length (sig_type_ptr, tu_group->tus);
5535 struct signatured_type *iter;
5536 int i;
5537
5538 gdb_assert (len > 0);
5539 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
5540
5541 pst->number_of_dependencies = len;
5542 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5543 len * sizeof (struct psymtab *));
5544 for (i = 0;
5545 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
5546 ++i)
5547 {
5548 gdb_assert (iter->per_cu.is_debug_types);
5549 pst->dependencies[i] = iter->per_cu.v.psymtab;
5550 iter->type_unit_group = tu_group;
5551 }
5552
5553 VEC_free (sig_type_ptr, tu_group->tus);
5554
5555 return 1;
5556 }
5557
5558 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5559 Build partial symbol tables for the .debug_types comp-units. */
5560
5561 static void
5562 build_type_psymtabs (struct objfile *objfile)
5563 {
5564 if (! create_all_type_units (objfile))
5565 return;
5566
5567 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5568
5569 /* Now that all TUs have been processed we can fill in the dependencies. */
5570 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5571 build_type_psymtab_dependencies, NULL);
5572 }
5573
5574 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
5575
5576 static void
5577 psymtabs_addrmap_cleanup (void *o)
5578 {
5579 struct objfile *objfile = o;
5580
5581 objfile->psymtabs_addrmap = NULL;
5582 }
5583
5584 /* Compute the 'user' field for each psymtab in OBJFILE. */
5585
5586 static void
5587 set_partial_user (struct objfile *objfile)
5588 {
5589 int i;
5590
5591 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5592 {
5593 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5594 struct partial_symtab *pst = per_cu->v.psymtab;
5595 int j;
5596
5597 if (pst == NULL)
5598 continue;
5599
5600 for (j = 0; j < pst->number_of_dependencies; ++j)
5601 {
5602 /* Set the 'user' field only if it is not already set. */
5603 if (pst->dependencies[j]->user == NULL)
5604 pst->dependencies[j]->user = pst;
5605 }
5606 }
5607 }
5608
5609 /* Build the partial symbol table by doing a quick pass through the
5610 .debug_info and .debug_abbrev sections. */
5611
5612 static void
5613 dwarf2_build_psymtabs_hard (struct objfile *objfile)
5614 {
5615 struct cleanup *back_to, *addrmap_cleanup;
5616 struct obstack temp_obstack;
5617 int i;
5618
5619 if (dwarf2_read_debug)
5620 {
5621 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5622 objfile->name);
5623 }
5624
5625 dwarf2_per_objfile->reading_partial_symbols = 1;
5626
5627 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
5628
5629 /* Any cached compilation units will be linked by the per-objfile
5630 read_in_chain. Make sure to free them when we're done. */
5631 back_to = make_cleanup (free_cached_comp_units, NULL);
5632
5633 build_type_psymtabs (objfile);
5634
5635 create_all_comp_units (objfile);
5636
5637 /* Create a temporary address map on a temporary obstack. We later
5638 copy this to the final obstack. */
5639 obstack_init (&temp_obstack);
5640 make_cleanup_obstack_free (&temp_obstack);
5641 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
5642 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
5643
5644 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5645 {
5646 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5647
5648 process_psymtab_comp_unit (per_cu, 0);
5649 }
5650
5651 set_partial_user (objfile);
5652
5653 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
5654 &objfile->objfile_obstack);
5655 discard_cleanups (addrmap_cleanup);
5656
5657 do_cleanups (back_to);
5658
5659 if (dwarf2_read_debug)
5660 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
5661 objfile->name);
5662 }
5663
5664 /* die_reader_func for load_partial_comp_unit. */
5665
5666 static void
5667 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
5668 gdb_byte *info_ptr,
5669 struct die_info *comp_unit_die,
5670 int has_children,
5671 void *data)
5672 {
5673 struct dwarf2_cu *cu = reader->cu;
5674
5675 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
5676
5677 /* Check if comp unit has_children.
5678 If so, read the rest of the partial symbols from this comp unit.
5679 If not, there's no more debug_info for this comp unit. */
5680 if (has_children)
5681 load_partial_dies (reader, info_ptr, 0);
5682 }
5683
5684 /* Load the partial DIEs for a secondary CU into memory.
5685 This is also used when rereading a primary CU with load_all_dies. */
5686
5687 static void
5688 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
5689 {
5690 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
5691 load_partial_comp_unit_reader, NULL);
5692 }
5693
5694 static void
5695 read_comp_units_from_section (struct objfile *objfile,
5696 struct dwarf2_section_info *section,
5697 unsigned int is_dwz,
5698 int *n_allocated,
5699 int *n_comp_units,
5700 struct dwarf2_per_cu_data ***all_comp_units)
5701 {
5702 gdb_byte *info_ptr;
5703 bfd *abfd = section->asection->owner;
5704
5705 if (dwarf2_read_debug)
5706 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
5707 section->asection->name, bfd_get_filename (abfd));
5708
5709 dwarf2_read_section (objfile, section);
5710
5711 info_ptr = section->buffer;
5712
5713 while (info_ptr < section->buffer + section->size)
5714 {
5715 unsigned int length, initial_length_size;
5716 struct dwarf2_per_cu_data *this_cu;
5717 sect_offset offset;
5718
5719 offset.sect_off = info_ptr - section->buffer;
5720
5721 /* Read just enough information to find out where the next
5722 compilation unit is. */
5723 length = read_initial_length (abfd, info_ptr, &initial_length_size);
5724
5725 /* Save the compilation unit for later lookup. */
5726 this_cu = obstack_alloc (&objfile->objfile_obstack,
5727 sizeof (struct dwarf2_per_cu_data));
5728 memset (this_cu, 0, sizeof (*this_cu));
5729 this_cu->offset = offset;
5730 this_cu->length = length + initial_length_size;
5731 this_cu->is_dwz = is_dwz;
5732 this_cu->objfile = objfile;
5733 this_cu->section = section;
5734
5735 if (*n_comp_units == *n_allocated)
5736 {
5737 *n_allocated *= 2;
5738 *all_comp_units = xrealloc (*all_comp_units,
5739 *n_allocated
5740 * sizeof (struct dwarf2_per_cu_data *));
5741 }
5742 (*all_comp_units)[*n_comp_units] = this_cu;
5743 ++*n_comp_units;
5744
5745 info_ptr = info_ptr + this_cu->length;
5746 }
5747 }
5748
5749 /* Create a list of all compilation units in OBJFILE.
5750 This is only done for -readnow and building partial symtabs. */
5751
5752 static void
5753 create_all_comp_units (struct objfile *objfile)
5754 {
5755 int n_allocated;
5756 int n_comp_units;
5757 struct dwarf2_per_cu_data **all_comp_units;
5758
5759 n_comp_units = 0;
5760 n_allocated = 10;
5761 all_comp_units = xmalloc (n_allocated
5762 * sizeof (struct dwarf2_per_cu_data *));
5763
5764 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
5765 &n_allocated, &n_comp_units, &all_comp_units);
5766
5767 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
5768 {
5769 struct dwz_file *dwz = dwarf2_get_dwz_file ();
5770
5771 read_comp_units_from_section (objfile, &dwz->info, 1,
5772 &n_allocated, &n_comp_units,
5773 &all_comp_units);
5774 }
5775
5776 dwarf2_per_objfile->all_comp_units
5777 = obstack_alloc (&objfile->objfile_obstack,
5778 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5779 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
5780 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5781 xfree (all_comp_units);
5782 dwarf2_per_objfile->n_comp_units = n_comp_units;
5783 }
5784
5785 /* Process all loaded DIEs for compilation unit CU, starting at
5786 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
5787 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
5788 DW_AT_ranges). If NEED_PC is set, then this function will set
5789 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
5790 and record the covered ranges in the addrmap. */
5791
5792 static void
5793 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5794 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
5795 {
5796 struct partial_die_info *pdi;
5797
5798 /* Now, march along the PDI's, descending into ones which have
5799 interesting children but skipping the children of the other ones,
5800 until we reach the end of the compilation unit. */
5801
5802 pdi = first_die;
5803
5804 while (pdi != NULL)
5805 {
5806 fixup_partial_die (pdi, cu);
5807
5808 /* Anonymous namespaces or modules have no name but have interesting
5809 children, so we need to look at them. Ditto for anonymous
5810 enums. */
5811
5812 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
5813 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
5814 || pdi->tag == DW_TAG_imported_unit)
5815 {
5816 switch (pdi->tag)
5817 {
5818 case DW_TAG_subprogram:
5819 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
5820 break;
5821 case DW_TAG_constant:
5822 case DW_TAG_variable:
5823 case DW_TAG_typedef:
5824 case DW_TAG_union_type:
5825 if (!pdi->is_declaration)
5826 {
5827 add_partial_symbol (pdi, cu);
5828 }
5829 break;
5830 case DW_TAG_class_type:
5831 case DW_TAG_interface_type:
5832 case DW_TAG_structure_type:
5833 if (!pdi->is_declaration)
5834 {
5835 add_partial_symbol (pdi, cu);
5836 }
5837 break;
5838 case DW_TAG_enumeration_type:
5839 if (!pdi->is_declaration)
5840 add_partial_enumeration (pdi, cu);
5841 break;
5842 case DW_TAG_base_type:
5843 case DW_TAG_subrange_type:
5844 /* File scope base type definitions are added to the partial
5845 symbol table. */
5846 add_partial_symbol (pdi, cu);
5847 break;
5848 case DW_TAG_namespace:
5849 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
5850 break;
5851 case DW_TAG_module:
5852 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
5853 break;
5854 case DW_TAG_imported_unit:
5855 {
5856 struct dwarf2_per_cu_data *per_cu;
5857
5858 /* For now we don't handle imported units in type units. */
5859 if (cu->per_cu->is_debug_types)
5860 {
5861 error (_("Dwarf Error: DW_TAG_imported_unit is not"
5862 " supported in type units [in module %s]"),
5863 cu->objfile->name);
5864 }
5865
5866 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
5867 pdi->is_dwz,
5868 cu->objfile);
5869
5870 /* Go read the partial unit, if needed. */
5871 if (per_cu->v.psymtab == NULL)
5872 process_psymtab_comp_unit (per_cu, 1);
5873
5874 VEC_safe_push (dwarf2_per_cu_ptr,
5875 cu->per_cu->imported_symtabs, per_cu);
5876 }
5877 break;
5878 default:
5879 break;
5880 }
5881 }
5882
5883 /* If the die has a sibling, skip to the sibling. */
5884
5885 pdi = pdi->die_sibling;
5886 }
5887 }
5888
5889 /* Functions used to compute the fully scoped name of a partial DIE.
5890
5891 Normally, this is simple. For C++, the parent DIE's fully scoped
5892 name is concatenated with "::" and the partial DIE's name. For
5893 Java, the same thing occurs except that "." is used instead of "::".
5894 Enumerators are an exception; they use the scope of their parent
5895 enumeration type, i.e. the name of the enumeration type is not
5896 prepended to the enumerator.
5897
5898 There are two complexities. One is DW_AT_specification; in this
5899 case "parent" means the parent of the target of the specification,
5900 instead of the direct parent of the DIE. The other is compilers
5901 which do not emit DW_TAG_namespace; in this case we try to guess
5902 the fully qualified name of structure types from their members'
5903 linkage names. This must be done using the DIE's children rather
5904 than the children of any DW_AT_specification target. We only need
5905 to do this for structures at the top level, i.e. if the target of
5906 any DW_AT_specification (if any; otherwise the DIE itself) does not
5907 have a parent. */
5908
5909 /* Compute the scope prefix associated with PDI's parent, in
5910 compilation unit CU. The result will be allocated on CU's
5911 comp_unit_obstack, or a copy of the already allocated PDI->NAME
5912 field. NULL is returned if no prefix is necessary. */
5913 static const char *
5914 partial_die_parent_scope (struct partial_die_info *pdi,
5915 struct dwarf2_cu *cu)
5916 {
5917 const char *grandparent_scope;
5918 struct partial_die_info *parent, *real_pdi;
5919
5920 /* We need to look at our parent DIE; if we have a DW_AT_specification,
5921 then this means the parent of the specification DIE. */
5922
5923 real_pdi = pdi;
5924 while (real_pdi->has_specification)
5925 real_pdi = find_partial_die (real_pdi->spec_offset,
5926 real_pdi->spec_is_dwz, cu);
5927
5928 parent = real_pdi->die_parent;
5929 if (parent == NULL)
5930 return NULL;
5931
5932 if (parent->scope_set)
5933 return parent->scope;
5934
5935 fixup_partial_die (parent, cu);
5936
5937 grandparent_scope = partial_die_parent_scope (parent, cu);
5938
5939 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
5940 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
5941 Work around this problem here. */
5942 if (cu->language == language_cplus
5943 && parent->tag == DW_TAG_namespace
5944 && strcmp (parent->name, "::") == 0
5945 && grandparent_scope == NULL)
5946 {
5947 parent->scope = NULL;
5948 parent->scope_set = 1;
5949 return NULL;
5950 }
5951
5952 if (pdi->tag == DW_TAG_enumerator)
5953 /* Enumerators should not get the name of the enumeration as a prefix. */
5954 parent->scope = grandparent_scope;
5955 else if (parent->tag == DW_TAG_namespace
5956 || parent->tag == DW_TAG_module
5957 || parent->tag == DW_TAG_structure_type
5958 || parent->tag == DW_TAG_class_type
5959 || parent->tag == DW_TAG_interface_type
5960 || parent->tag == DW_TAG_union_type
5961 || parent->tag == DW_TAG_enumeration_type)
5962 {
5963 if (grandparent_scope == NULL)
5964 parent->scope = parent->name;
5965 else
5966 parent->scope = typename_concat (&cu->comp_unit_obstack,
5967 grandparent_scope,
5968 parent->name, 0, cu);
5969 }
5970 else
5971 {
5972 /* FIXME drow/2004-04-01: What should we be doing with
5973 function-local names? For partial symbols, we should probably be
5974 ignoring them. */
5975 complaint (&symfile_complaints,
5976 _("unhandled containing DIE tag %d for DIE at %d"),
5977 parent->tag, pdi->offset.sect_off);
5978 parent->scope = grandparent_scope;
5979 }
5980
5981 parent->scope_set = 1;
5982 return parent->scope;
5983 }
5984
5985 /* Return the fully scoped name associated with PDI, from compilation unit
5986 CU. The result will be allocated with malloc. */
5987
5988 static char *
5989 partial_die_full_name (struct partial_die_info *pdi,
5990 struct dwarf2_cu *cu)
5991 {
5992 const char *parent_scope;
5993
5994 /* If this is a template instantiation, we can not work out the
5995 template arguments from partial DIEs. So, unfortunately, we have
5996 to go through the full DIEs. At least any work we do building
5997 types here will be reused if full symbols are loaded later. */
5998 if (pdi->has_template_arguments)
5999 {
6000 fixup_partial_die (pdi, cu);
6001
6002 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
6003 {
6004 struct die_info *die;
6005 struct attribute attr;
6006 struct dwarf2_cu *ref_cu = cu;
6007
6008 /* DW_FORM_ref_addr is using section offset. */
6009 attr.name = 0;
6010 attr.form = DW_FORM_ref_addr;
6011 attr.u.unsnd = pdi->offset.sect_off;
6012 die = follow_die_ref (NULL, &attr, &ref_cu);
6013
6014 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
6015 }
6016 }
6017
6018 parent_scope = partial_die_parent_scope (pdi, cu);
6019 if (parent_scope == NULL)
6020 return NULL;
6021 else
6022 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
6023 }
6024
6025 static void
6026 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
6027 {
6028 struct objfile *objfile = cu->objfile;
6029 CORE_ADDR addr = 0;
6030 const char *actual_name = NULL;
6031 CORE_ADDR baseaddr;
6032 char *built_actual_name;
6033
6034 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6035
6036 built_actual_name = partial_die_full_name (pdi, cu);
6037 if (built_actual_name != NULL)
6038 actual_name = built_actual_name;
6039
6040 if (actual_name == NULL)
6041 actual_name = pdi->name;
6042
6043 switch (pdi->tag)
6044 {
6045 case DW_TAG_subprogram:
6046 if (pdi->is_external || cu->language == language_ada)
6047 {
6048 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
6049 of the global scope. But in Ada, we want to be able to access
6050 nested procedures globally. So all Ada subprograms are stored
6051 in the global scope. */
6052 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
6053 mst_text, objfile); */
6054 add_psymbol_to_list (actual_name, strlen (actual_name),
6055 built_actual_name != NULL,
6056 VAR_DOMAIN, LOC_BLOCK,
6057 &objfile->global_psymbols,
6058 0, pdi->lowpc + baseaddr,
6059 cu->language, objfile);
6060 }
6061 else
6062 {
6063 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
6064 mst_file_text, objfile); */
6065 add_psymbol_to_list (actual_name, strlen (actual_name),
6066 built_actual_name != NULL,
6067 VAR_DOMAIN, LOC_BLOCK,
6068 &objfile->static_psymbols,
6069 0, pdi->lowpc + baseaddr,
6070 cu->language, objfile);
6071 }
6072 break;
6073 case DW_TAG_constant:
6074 {
6075 struct psymbol_allocation_list *list;
6076
6077 if (pdi->is_external)
6078 list = &objfile->global_psymbols;
6079 else
6080 list = &objfile->static_psymbols;
6081 add_psymbol_to_list (actual_name, strlen (actual_name),
6082 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
6083 list, 0, 0, cu->language, objfile);
6084 }
6085 break;
6086 case DW_TAG_variable:
6087 if (pdi->d.locdesc)
6088 addr = decode_locdesc (pdi->d.locdesc, cu);
6089
6090 if (pdi->d.locdesc
6091 && addr == 0
6092 && !dwarf2_per_objfile->has_section_at_zero)
6093 {
6094 /* A global or static variable may also have been stripped
6095 out by the linker if unused, in which case its address
6096 will be nullified; do not add such variables into partial
6097 symbol table then. */
6098 }
6099 else if (pdi->is_external)
6100 {
6101 /* Global Variable.
6102 Don't enter into the minimal symbol tables as there is
6103 a minimal symbol table entry from the ELF symbols already.
6104 Enter into partial symbol table if it has a location
6105 descriptor or a type.
6106 If the location descriptor is missing, new_symbol will create
6107 a LOC_UNRESOLVED symbol, the address of the variable will then
6108 be determined from the minimal symbol table whenever the variable
6109 is referenced.
6110 The address for the partial symbol table entry is not
6111 used by GDB, but it comes in handy for debugging partial symbol
6112 table building. */
6113
6114 if (pdi->d.locdesc || pdi->has_type)
6115 add_psymbol_to_list (actual_name, strlen (actual_name),
6116 built_actual_name != NULL,
6117 VAR_DOMAIN, LOC_STATIC,
6118 &objfile->global_psymbols,
6119 0, addr + baseaddr,
6120 cu->language, objfile);
6121 }
6122 else
6123 {
6124 /* Static Variable. Skip symbols without location descriptors. */
6125 if (pdi->d.locdesc == NULL)
6126 {
6127 xfree (built_actual_name);
6128 return;
6129 }
6130 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
6131 mst_file_data, objfile); */
6132 add_psymbol_to_list (actual_name, strlen (actual_name),
6133 built_actual_name != NULL,
6134 VAR_DOMAIN, LOC_STATIC,
6135 &objfile->static_psymbols,
6136 0, addr + baseaddr,
6137 cu->language, objfile);
6138 }
6139 break;
6140 case DW_TAG_typedef:
6141 case DW_TAG_base_type:
6142 case DW_TAG_subrange_type:
6143 add_psymbol_to_list (actual_name, strlen (actual_name),
6144 built_actual_name != NULL,
6145 VAR_DOMAIN, LOC_TYPEDEF,
6146 &objfile->static_psymbols,
6147 0, (CORE_ADDR) 0, cu->language, objfile);
6148 break;
6149 case DW_TAG_namespace:
6150 add_psymbol_to_list (actual_name, strlen (actual_name),
6151 built_actual_name != NULL,
6152 VAR_DOMAIN, LOC_TYPEDEF,
6153 &objfile->global_psymbols,
6154 0, (CORE_ADDR) 0, cu->language, objfile);
6155 break;
6156 case DW_TAG_class_type:
6157 case DW_TAG_interface_type:
6158 case DW_TAG_structure_type:
6159 case DW_TAG_union_type:
6160 case DW_TAG_enumeration_type:
6161 /* Skip external references. The DWARF standard says in the section
6162 about "Structure, Union, and Class Type Entries": "An incomplete
6163 structure, union or class type is represented by a structure,
6164 union or class entry that does not have a byte size attribute
6165 and that has a DW_AT_declaration attribute." */
6166 if (!pdi->has_byte_size && pdi->is_declaration)
6167 {
6168 xfree (built_actual_name);
6169 return;
6170 }
6171
6172 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
6173 static vs. global. */
6174 add_psymbol_to_list (actual_name, strlen (actual_name),
6175 built_actual_name != NULL,
6176 STRUCT_DOMAIN, LOC_TYPEDEF,
6177 (cu->language == language_cplus
6178 || cu->language == language_java)
6179 ? &objfile->global_psymbols
6180 : &objfile->static_psymbols,
6181 0, (CORE_ADDR) 0, cu->language, objfile);
6182
6183 break;
6184 case DW_TAG_enumerator:
6185 add_psymbol_to_list (actual_name, strlen (actual_name),
6186 built_actual_name != NULL,
6187 VAR_DOMAIN, LOC_CONST,
6188 (cu->language == language_cplus
6189 || cu->language == language_java)
6190 ? &objfile->global_psymbols
6191 : &objfile->static_psymbols,
6192 0, (CORE_ADDR) 0, cu->language, objfile);
6193 break;
6194 default:
6195 break;
6196 }
6197
6198 xfree (built_actual_name);
6199 }
6200
6201 /* Read a partial die corresponding to a namespace; also, add a symbol
6202 corresponding to that namespace to the symbol table. NAMESPACE is
6203 the name of the enclosing namespace. */
6204
6205 static void
6206 add_partial_namespace (struct partial_die_info *pdi,
6207 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6208 int need_pc, struct dwarf2_cu *cu)
6209 {
6210 /* Add a symbol for the namespace. */
6211
6212 add_partial_symbol (pdi, cu);
6213
6214 /* Now scan partial symbols in that namespace. */
6215
6216 if (pdi->has_children)
6217 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6218 }
6219
6220 /* Read a partial die corresponding to a Fortran module. */
6221
6222 static void
6223 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
6224 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6225 {
6226 /* Now scan partial symbols in that module. */
6227
6228 if (pdi->has_children)
6229 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6230 }
6231
6232 /* Read a partial die corresponding to a subprogram and create a partial
6233 symbol for that subprogram. When the CU language allows it, this
6234 routine also defines a partial symbol for each nested subprogram
6235 that this subprogram contains.
6236
6237 DIE my also be a lexical block, in which case we simply search
6238 recursively for suprograms defined inside that lexical block.
6239 Again, this is only performed when the CU language allows this
6240 type of definitions. */
6241
6242 static void
6243 add_partial_subprogram (struct partial_die_info *pdi,
6244 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6245 int need_pc, struct dwarf2_cu *cu)
6246 {
6247 if (pdi->tag == DW_TAG_subprogram)
6248 {
6249 if (pdi->has_pc_info)
6250 {
6251 if (pdi->lowpc < *lowpc)
6252 *lowpc = pdi->lowpc;
6253 if (pdi->highpc > *highpc)
6254 *highpc = pdi->highpc;
6255 if (need_pc)
6256 {
6257 CORE_ADDR baseaddr;
6258 struct objfile *objfile = cu->objfile;
6259
6260 baseaddr = ANOFFSET (objfile->section_offsets,
6261 SECT_OFF_TEXT (objfile));
6262 addrmap_set_empty (objfile->psymtabs_addrmap,
6263 pdi->lowpc + baseaddr,
6264 pdi->highpc - 1 + baseaddr,
6265 cu->per_cu->v.psymtab);
6266 }
6267 }
6268
6269 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6270 {
6271 if (!pdi->is_declaration)
6272 /* Ignore subprogram DIEs that do not have a name, they are
6273 illegal. Do not emit a complaint at this point, we will
6274 do so when we convert this psymtab into a symtab. */
6275 if (pdi->name)
6276 add_partial_symbol (pdi, cu);
6277 }
6278 }
6279
6280 if (! pdi->has_children)
6281 return;
6282
6283 if (cu->language == language_ada)
6284 {
6285 pdi = pdi->die_child;
6286 while (pdi != NULL)
6287 {
6288 fixup_partial_die (pdi, cu);
6289 if (pdi->tag == DW_TAG_subprogram
6290 || pdi->tag == DW_TAG_lexical_block)
6291 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
6292 pdi = pdi->die_sibling;
6293 }
6294 }
6295 }
6296
6297 /* Read a partial die corresponding to an enumeration type. */
6298
6299 static void
6300 add_partial_enumeration (struct partial_die_info *enum_pdi,
6301 struct dwarf2_cu *cu)
6302 {
6303 struct partial_die_info *pdi;
6304
6305 if (enum_pdi->name != NULL)
6306 add_partial_symbol (enum_pdi, cu);
6307
6308 pdi = enum_pdi->die_child;
6309 while (pdi)
6310 {
6311 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
6312 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
6313 else
6314 add_partial_symbol (pdi, cu);
6315 pdi = pdi->die_sibling;
6316 }
6317 }
6318
6319 /* Return the initial uleb128 in the die at INFO_PTR. */
6320
6321 static unsigned int
6322 peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
6323 {
6324 unsigned int bytes_read;
6325
6326 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6327 }
6328
6329 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6330 Return the corresponding abbrev, or NULL if the number is zero (indicating
6331 an empty DIE). In either case *BYTES_READ will be set to the length of
6332 the initial number. */
6333
6334 static struct abbrev_info *
6335 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
6336 struct dwarf2_cu *cu)
6337 {
6338 bfd *abfd = cu->objfile->obfd;
6339 unsigned int abbrev_number;
6340 struct abbrev_info *abbrev;
6341
6342 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6343
6344 if (abbrev_number == 0)
6345 return NULL;
6346
6347 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
6348 if (!abbrev)
6349 {
6350 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6351 abbrev_number, bfd_get_filename (abfd));
6352 }
6353
6354 return abbrev;
6355 }
6356
6357 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6358 Returns a pointer to the end of a series of DIEs, terminated by an empty
6359 DIE. Any children of the skipped DIEs will also be skipped. */
6360
6361 static gdb_byte *
6362 skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
6363 {
6364 struct dwarf2_cu *cu = reader->cu;
6365 struct abbrev_info *abbrev;
6366 unsigned int bytes_read;
6367
6368 while (1)
6369 {
6370 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6371 if (abbrev == NULL)
6372 return info_ptr + bytes_read;
6373 else
6374 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
6375 }
6376 }
6377
6378 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6379 INFO_PTR should point just after the initial uleb128 of a DIE, and the
6380 abbrev corresponding to that skipped uleb128 should be passed in
6381 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6382 children. */
6383
6384 static gdb_byte *
6385 skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
6386 struct abbrev_info *abbrev)
6387 {
6388 unsigned int bytes_read;
6389 struct attribute attr;
6390 bfd *abfd = reader->abfd;
6391 struct dwarf2_cu *cu = reader->cu;
6392 gdb_byte *buffer = reader->buffer;
6393 const gdb_byte *buffer_end = reader->buffer_end;
6394 gdb_byte *start_info_ptr = info_ptr;
6395 unsigned int form, i;
6396
6397 for (i = 0; i < abbrev->num_attrs; i++)
6398 {
6399 /* The only abbrev we care about is DW_AT_sibling. */
6400 if (abbrev->attrs[i].name == DW_AT_sibling)
6401 {
6402 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
6403 if (attr.form == DW_FORM_ref_addr)
6404 complaint (&symfile_complaints,
6405 _("ignoring absolute DW_AT_sibling"));
6406 else
6407 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
6408 }
6409
6410 /* If it isn't DW_AT_sibling, skip this attribute. */
6411 form = abbrev->attrs[i].form;
6412 skip_attribute:
6413 switch (form)
6414 {
6415 case DW_FORM_ref_addr:
6416 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6417 and later it is offset sized. */
6418 if (cu->header.version == 2)
6419 info_ptr += cu->header.addr_size;
6420 else
6421 info_ptr += cu->header.offset_size;
6422 break;
6423 case DW_FORM_GNU_ref_alt:
6424 info_ptr += cu->header.offset_size;
6425 break;
6426 case DW_FORM_addr:
6427 info_ptr += cu->header.addr_size;
6428 break;
6429 case DW_FORM_data1:
6430 case DW_FORM_ref1:
6431 case DW_FORM_flag:
6432 info_ptr += 1;
6433 break;
6434 case DW_FORM_flag_present:
6435 break;
6436 case DW_FORM_data2:
6437 case DW_FORM_ref2:
6438 info_ptr += 2;
6439 break;
6440 case DW_FORM_data4:
6441 case DW_FORM_ref4:
6442 info_ptr += 4;
6443 break;
6444 case DW_FORM_data8:
6445 case DW_FORM_ref8:
6446 case DW_FORM_ref_sig8:
6447 info_ptr += 8;
6448 break;
6449 case DW_FORM_string:
6450 read_direct_string (abfd, info_ptr, &bytes_read);
6451 info_ptr += bytes_read;
6452 break;
6453 case DW_FORM_sec_offset:
6454 case DW_FORM_strp:
6455 case DW_FORM_GNU_strp_alt:
6456 info_ptr += cu->header.offset_size;
6457 break;
6458 case DW_FORM_exprloc:
6459 case DW_FORM_block:
6460 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6461 info_ptr += bytes_read;
6462 break;
6463 case DW_FORM_block1:
6464 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6465 break;
6466 case DW_FORM_block2:
6467 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6468 break;
6469 case DW_FORM_block4:
6470 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6471 break;
6472 case DW_FORM_sdata:
6473 case DW_FORM_udata:
6474 case DW_FORM_ref_udata:
6475 case DW_FORM_GNU_addr_index:
6476 case DW_FORM_GNU_str_index:
6477 info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
6478 break;
6479 case DW_FORM_indirect:
6480 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6481 info_ptr += bytes_read;
6482 /* We need to continue parsing from here, so just go back to
6483 the top. */
6484 goto skip_attribute;
6485
6486 default:
6487 error (_("Dwarf Error: Cannot handle %s "
6488 "in DWARF reader [in module %s]"),
6489 dwarf_form_name (form),
6490 bfd_get_filename (abfd));
6491 }
6492 }
6493
6494 if (abbrev->has_children)
6495 return skip_children (reader, info_ptr);
6496 else
6497 return info_ptr;
6498 }
6499
6500 /* Locate ORIG_PDI's sibling.
6501 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
6502
6503 static gdb_byte *
6504 locate_pdi_sibling (const struct die_reader_specs *reader,
6505 struct partial_die_info *orig_pdi,
6506 gdb_byte *info_ptr)
6507 {
6508 /* Do we know the sibling already? */
6509
6510 if (orig_pdi->sibling)
6511 return orig_pdi->sibling;
6512
6513 /* Are there any children to deal with? */
6514
6515 if (!orig_pdi->has_children)
6516 return info_ptr;
6517
6518 /* Skip the children the long way. */
6519
6520 return skip_children (reader, info_ptr);
6521 }
6522
6523 /* Expand this partial symbol table into a full symbol table. SELF is
6524 not NULL. */
6525
6526 static void
6527 dwarf2_read_symtab (struct partial_symtab *self,
6528 struct objfile *objfile)
6529 {
6530 if (self->readin)
6531 {
6532 warning (_("bug: psymtab for %s is already read in."),
6533 self->filename);
6534 }
6535 else
6536 {
6537 if (info_verbose)
6538 {
6539 printf_filtered (_("Reading in symbols for %s..."),
6540 self->filename);
6541 gdb_flush (gdb_stdout);
6542 }
6543
6544 /* Restore our global data. */
6545 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
6546
6547 /* If this psymtab is constructed from a debug-only objfile, the
6548 has_section_at_zero flag will not necessarily be correct. We
6549 can get the correct value for this flag by looking at the data
6550 associated with the (presumably stripped) associated objfile. */
6551 if (objfile->separate_debug_objfile_backlink)
6552 {
6553 struct dwarf2_per_objfile *dpo_backlink
6554 = objfile_data (objfile->separate_debug_objfile_backlink,
6555 dwarf2_objfile_data_key);
6556
6557 dwarf2_per_objfile->has_section_at_zero
6558 = dpo_backlink->has_section_at_zero;
6559 }
6560
6561 dwarf2_per_objfile->reading_partial_symbols = 0;
6562
6563 psymtab_to_symtab_1 (self);
6564
6565 /* Finish up the debug error message. */
6566 if (info_verbose)
6567 printf_filtered (_("done.\n"));
6568 }
6569
6570 process_cu_includes ();
6571 }
6572 \f
6573 /* Reading in full CUs. */
6574
6575 /* Add PER_CU to the queue. */
6576
6577 static void
6578 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6579 enum language pretend_language)
6580 {
6581 struct dwarf2_queue_item *item;
6582
6583 per_cu->queued = 1;
6584 item = xmalloc (sizeof (*item));
6585 item->per_cu = per_cu;
6586 item->pretend_language = pretend_language;
6587 item->next = NULL;
6588
6589 if (dwarf2_queue == NULL)
6590 dwarf2_queue = item;
6591 else
6592 dwarf2_queue_tail->next = item;
6593
6594 dwarf2_queue_tail = item;
6595 }
6596
6597 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
6598 unit and add it to our queue.
6599 The result is non-zero if PER_CU was queued, otherwise the result is zero
6600 meaning either PER_CU is already queued or it is already loaded. */
6601
6602 static int
6603 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6604 struct dwarf2_per_cu_data *per_cu,
6605 enum language pretend_language)
6606 {
6607 /* We may arrive here during partial symbol reading, if we need full
6608 DIEs to process an unusual case (e.g. template arguments). Do
6609 not queue PER_CU, just tell our caller to load its DIEs. */
6610 if (dwarf2_per_objfile->reading_partial_symbols)
6611 {
6612 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6613 return 1;
6614 return 0;
6615 }
6616
6617 /* Mark the dependence relation so that we don't flush PER_CU
6618 too early. */
6619 dwarf2_add_dependence (this_cu, per_cu);
6620
6621 /* If it's already on the queue, we have nothing to do. */
6622 if (per_cu->queued)
6623 return 0;
6624
6625 /* If the compilation unit is already loaded, just mark it as
6626 used. */
6627 if (per_cu->cu != NULL)
6628 {
6629 per_cu->cu->last_used = 0;
6630 return 0;
6631 }
6632
6633 /* Add it to the queue. */
6634 queue_comp_unit (per_cu, pretend_language);
6635
6636 return 1;
6637 }
6638
6639 /* Process the queue. */
6640
6641 static void
6642 process_queue (void)
6643 {
6644 struct dwarf2_queue_item *item, *next_item;
6645
6646 if (dwarf2_read_debug)
6647 {
6648 fprintf_unfiltered (gdb_stdlog,
6649 "Expanding one or more symtabs of objfile %s ...\n",
6650 dwarf2_per_objfile->objfile->name);
6651 }
6652
6653 /* The queue starts out with one item, but following a DIE reference
6654 may load a new CU, adding it to the end of the queue. */
6655 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
6656 {
6657 if (dwarf2_per_objfile->using_index
6658 ? !item->per_cu->v.quick->symtab
6659 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
6660 {
6661 struct dwarf2_per_cu_data *per_cu = item->per_cu;
6662
6663 if (dwarf2_read_debug)
6664 {
6665 fprintf_unfiltered (gdb_stdlog,
6666 "Expanding symtab of %s at offset 0x%x\n",
6667 per_cu->is_debug_types ? "TU" : "CU",
6668 per_cu->offset.sect_off);
6669 }
6670
6671 if (per_cu->is_debug_types)
6672 process_full_type_unit (per_cu, item->pretend_language);
6673 else
6674 process_full_comp_unit (per_cu, item->pretend_language);
6675
6676 if (dwarf2_read_debug)
6677 {
6678 fprintf_unfiltered (gdb_stdlog,
6679 "Done expanding %s at offset 0x%x\n",
6680 per_cu->is_debug_types ? "TU" : "CU",
6681 per_cu->offset.sect_off);
6682 }
6683 }
6684
6685 item->per_cu->queued = 0;
6686 next_item = item->next;
6687 xfree (item);
6688 }
6689
6690 dwarf2_queue_tail = NULL;
6691
6692 if (dwarf2_read_debug)
6693 {
6694 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
6695 dwarf2_per_objfile->objfile->name);
6696 }
6697 }
6698
6699 /* Free all allocated queue entries. This function only releases anything if
6700 an error was thrown; if the queue was processed then it would have been
6701 freed as we went along. */
6702
6703 static void
6704 dwarf2_release_queue (void *dummy)
6705 {
6706 struct dwarf2_queue_item *item, *last;
6707
6708 item = dwarf2_queue;
6709 while (item)
6710 {
6711 /* Anything still marked queued is likely to be in an
6712 inconsistent state, so discard it. */
6713 if (item->per_cu->queued)
6714 {
6715 if (item->per_cu->cu != NULL)
6716 free_one_cached_comp_unit (item->per_cu);
6717 item->per_cu->queued = 0;
6718 }
6719
6720 last = item;
6721 item = item->next;
6722 xfree (last);
6723 }
6724
6725 dwarf2_queue = dwarf2_queue_tail = NULL;
6726 }
6727
6728 /* Read in full symbols for PST, and anything it depends on. */
6729
6730 static void
6731 psymtab_to_symtab_1 (struct partial_symtab *pst)
6732 {
6733 struct dwarf2_per_cu_data *per_cu;
6734 int i;
6735
6736 if (pst->readin)
6737 return;
6738
6739 for (i = 0; i < pst->number_of_dependencies; i++)
6740 if (!pst->dependencies[i]->readin
6741 && pst->dependencies[i]->user == NULL)
6742 {
6743 /* Inform about additional files that need to be read in. */
6744 if (info_verbose)
6745 {
6746 /* FIXME: i18n: Need to make this a single string. */
6747 fputs_filtered (" ", gdb_stdout);
6748 wrap_here ("");
6749 fputs_filtered ("and ", gdb_stdout);
6750 wrap_here ("");
6751 printf_filtered ("%s...", pst->dependencies[i]->filename);
6752 wrap_here (""); /* Flush output. */
6753 gdb_flush (gdb_stdout);
6754 }
6755 psymtab_to_symtab_1 (pst->dependencies[i]);
6756 }
6757
6758 per_cu = pst->read_symtab_private;
6759
6760 if (per_cu == NULL)
6761 {
6762 /* It's an include file, no symbols to read for it.
6763 Everything is in the parent symtab. */
6764 pst->readin = 1;
6765 return;
6766 }
6767
6768 dw2_do_instantiate_symtab (per_cu);
6769 }
6770
6771 /* Trivial hash function for die_info: the hash value of a DIE
6772 is its offset in .debug_info for this objfile. */
6773
6774 static hashval_t
6775 die_hash (const void *item)
6776 {
6777 const struct die_info *die = item;
6778
6779 return die->offset.sect_off;
6780 }
6781
6782 /* Trivial comparison function for die_info structures: two DIEs
6783 are equal if they have the same offset. */
6784
6785 static int
6786 die_eq (const void *item_lhs, const void *item_rhs)
6787 {
6788 const struct die_info *die_lhs = item_lhs;
6789 const struct die_info *die_rhs = item_rhs;
6790
6791 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
6792 }
6793
6794 /* die_reader_func for load_full_comp_unit.
6795 This is identical to read_signatured_type_reader,
6796 but is kept separate for now. */
6797
6798 static void
6799 load_full_comp_unit_reader (const struct die_reader_specs *reader,
6800 gdb_byte *info_ptr,
6801 struct die_info *comp_unit_die,
6802 int has_children,
6803 void *data)
6804 {
6805 struct dwarf2_cu *cu = reader->cu;
6806 enum language *language_ptr = data;
6807
6808 gdb_assert (cu->die_hash == NULL);
6809 cu->die_hash =
6810 htab_create_alloc_ex (cu->header.length / 12,
6811 die_hash,
6812 die_eq,
6813 NULL,
6814 &cu->comp_unit_obstack,
6815 hashtab_obstack_allocate,
6816 dummy_obstack_deallocate);
6817
6818 if (has_children)
6819 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
6820 &info_ptr, comp_unit_die);
6821 cu->dies = comp_unit_die;
6822 /* comp_unit_die is not stored in die_hash, no need. */
6823
6824 /* We try not to read any attributes in this function, because not
6825 all CUs needed for references have been loaded yet, and symbol
6826 table processing isn't initialized. But we have to set the CU language,
6827 or we won't be able to build types correctly.
6828 Similarly, if we do not read the producer, we can not apply
6829 producer-specific interpretation. */
6830 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
6831 }
6832
6833 /* Load the DIEs associated with PER_CU into memory. */
6834
6835 static void
6836 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
6837 enum language pretend_language)
6838 {
6839 gdb_assert (! this_cu->is_debug_types);
6840
6841 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6842 load_full_comp_unit_reader, &pretend_language);
6843 }
6844
6845 /* Add a DIE to the delayed physname list. */
6846
6847 static void
6848 add_to_method_list (struct type *type, int fnfield_index, int index,
6849 const char *name, struct die_info *die,
6850 struct dwarf2_cu *cu)
6851 {
6852 struct delayed_method_info mi;
6853 mi.type = type;
6854 mi.fnfield_index = fnfield_index;
6855 mi.index = index;
6856 mi.name = name;
6857 mi.die = die;
6858 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
6859 }
6860
6861 /* A cleanup for freeing the delayed method list. */
6862
6863 static void
6864 free_delayed_list (void *ptr)
6865 {
6866 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
6867 if (cu->method_list != NULL)
6868 {
6869 VEC_free (delayed_method_info, cu->method_list);
6870 cu->method_list = NULL;
6871 }
6872 }
6873
6874 /* Compute the physnames of any methods on the CU's method list.
6875
6876 The computation of method physnames is delayed in order to avoid the
6877 (bad) condition that one of the method's formal parameters is of an as yet
6878 incomplete type. */
6879
6880 static void
6881 compute_delayed_physnames (struct dwarf2_cu *cu)
6882 {
6883 int i;
6884 struct delayed_method_info *mi;
6885 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
6886 {
6887 const char *physname;
6888 struct fn_fieldlist *fn_flp
6889 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
6890 physname = dwarf2_physname (mi->name, mi->die, cu);
6891 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
6892 }
6893 }
6894
6895 /* Go objects should be embedded in a DW_TAG_module DIE,
6896 and it's not clear if/how imported objects will appear.
6897 To keep Go support simple until that's worked out,
6898 go back through what we've read and create something usable.
6899 We could do this while processing each DIE, and feels kinda cleaner,
6900 but that way is more invasive.
6901 This is to, for example, allow the user to type "p var" or "b main"
6902 without having to specify the package name, and allow lookups
6903 of module.object to work in contexts that use the expression
6904 parser. */
6905
6906 static void
6907 fixup_go_packaging (struct dwarf2_cu *cu)
6908 {
6909 char *package_name = NULL;
6910 struct pending *list;
6911 int i;
6912
6913 for (list = global_symbols; list != NULL; list = list->next)
6914 {
6915 for (i = 0; i < list->nsyms; ++i)
6916 {
6917 struct symbol *sym = list->symbol[i];
6918
6919 if (SYMBOL_LANGUAGE (sym) == language_go
6920 && SYMBOL_CLASS (sym) == LOC_BLOCK)
6921 {
6922 char *this_package_name = go_symbol_package_name (sym);
6923
6924 if (this_package_name == NULL)
6925 continue;
6926 if (package_name == NULL)
6927 package_name = this_package_name;
6928 else
6929 {
6930 if (strcmp (package_name, this_package_name) != 0)
6931 complaint (&symfile_complaints,
6932 _("Symtab %s has objects from two different Go packages: %s and %s"),
6933 (SYMBOL_SYMTAB (sym)
6934 ? symtab_to_filename_for_display (SYMBOL_SYMTAB (sym))
6935 : cu->objfile->name),
6936 this_package_name, package_name);
6937 xfree (this_package_name);
6938 }
6939 }
6940 }
6941 }
6942
6943 if (package_name != NULL)
6944 {
6945 struct objfile *objfile = cu->objfile;
6946 const char *saved_package_name = obstack_copy0 (&objfile->objfile_obstack,
6947 package_name,
6948 strlen (package_name));
6949 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
6950 saved_package_name, objfile);
6951 struct symbol *sym;
6952
6953 TYPE_TAG_NAME (type) = TYPE_NAME (type);
6954
6955 sym = allocate_symbol (objfile);
6956 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
6957 SYMBOL_SET_NAMES (sym, saved_package_name,
6958 strlen (saved_package_name), 0, objfile);
6959 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
6960 e.g., "main" finds the "main" module and not C's main(). */
6961 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
6962 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
6963 SYMBOL_TYPE (sym) = type;
6964
6965 add_symbol_to_list (sym, &global_symbols);
6966
6967 xfree (package_name);
6968 }
6969 }
6970
6971 /* Return the symtab for PER_CU. This works properly regardless of
6972 whether we're using the index or psymtabs. */
6973
6974 static struct symtab *
6975 get_symtab (struct dwarf2_per_cu_data *per_cu)
6976 {
6977 return (dwarf2_per_objfile->using_index
6978 ? per_cu->v.quick->symtab
6979 : per_cu->v.psymtab->symtab);
6980 }
6981
6982 /* A helper function for computing the list of all symbol tables
6983 included by PER_CU. */
6984
6985 static void
6986 recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
6987 htab_t all_children,
6988 struct dwarf2_per_cu_data *per_cu)
6989 {
6990 void **slot;
6991 int ix;
6992 struct dwarf2_per_cu_data *iter;
6993
6994 slot = htab_find_slot (all_children, per_cu, INSERT);
6995 if (*slot != NULL)
6996 {
6997 /* This inclusion and its children have been processed. */
6998 return;
6999 }
7000
7001 *slot = per_cu;
7002 /* Only add a CU if it has a symbol table. */
7003 if (get_symtab (per_cu) != NULL)
7004 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
7005
7006 for (ix = 0;
7007 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
7008 ++ix)
7009 recursively_compute_inclusions (result, all_children, iter);
7010 }
7011
7012 /* Compute the symtab 'includes' fields for the symtab related to
7013 PER_CU. */
7014
7015 static void
7016 compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
7017 {
7018 gdb_assert (! per_cu->is_debug_types);
7019
7020 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
7021 {
7022 int ix, len;
7023 struct dwarf2_per_cu_data *iter;
7024 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
7025 htab_t all_children;
7026 struct symtab *symtab = get_symtab (per_cu);
7027
7028 /* If we don't have a symtab, we can just skip this case. */
7029 if (symtab == NULL)
7030 return;
7031
7032 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
7033 NULL, xcalloc, xfree);
7034
7035 for (ix = 0;
7036 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
7037 ix, iter);
7038 ++ix)
7039 recursively_compute_inclusions (&result_children, all_children, iter);
7040
7041 /* Now we have a transitive closure of all the included CUs, and
7042 for .gdb_index version 7 the included TUs, so we can convert it
7043 to a list of symtabs. */
7044 len = VEC_length (dwarf2_per_cu_ptr, result_children);
7045 symtab->includes
7046 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
7047 (len + 1) * sizeof (struct symtab *));
7048 for (ix = 0;
7049 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
7050 ++ix)
7051 symtab->includes[ix] = get_symtab (iter);
7052 symtab->includes[len] = NULL;
7053
7054 VEC_free (dwarf2_per_cu_ptr, result_children);
7055 htab_delete (all_children);
7056 }
7057 }
7058
7059 /* Compute the 'includes' field for the symtabs of all the CUs we just
7060 read. */
7061
7062 static void
7063 process_cu_includes (void)
7064 {
7065 int ix;
7066 struct dwarf2_per_cu_data *iter;
7067
7068 for (ix = 0;
7069 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
7070 ix, iter);
7071 ++ix)
7072 {
7073 if (! iter->is_debug_types)
7074 compute_symtab_includes (iter);
7075 }
7076
7077 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
7078 }
7079
7080 /* Generate full symbol information for PER_CU, whose DIEs have
7081 already been loaded into memory. */
7082
7083 static void
7084 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
7085 enum language pretend_language)
7086 {
7087 struct dwarf2_cu *cu = per_cu->cu;
7088 struct objfile *objfile = per_cu->objfile;
7089 CORE_ADDR lowpc, highpc;
7090 struct symtab *symtab;
7091 struct cleanup *back_to, *delayed_list_cleanup;
7092 CORE_ADDR baseaddr;
7093 struct block *static_block;
7094
7095 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7096
7097 buildsym_init ();
7098 back_to = make_cleanup (really_free_pendings, NULL);
7099 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7100
7101 cu->list_in_scope = &file_symbols;
7102
7103 cu->language = pretend_language;
7104 cu->language_defn = language_def (cu->language);
7105
7106 /* Do line number decoding in read_file_scope () */
7107 process_die (cu->dies, cu);
7108
7109 /* For now fudge the Go package. */
7110 if (cu->language == language_go)
7111 fixup_go_packaging (cu);
7112
7113 /* Now that we have processed all the DIEs in the CU, all the types
7114 should be complete, and it should now be safe to compute all of the
7115 physnames. */
7116 compute_delayed_physnames (cu);
7117 do_cleanups (delayed_list_cleanup);
7118
7119 /* Some compilers don't define a DW_AT_high_pc attribute for the
7120 compilation unit. If the DW_AT_high_pc is missing, synthesize
7121 it, by scanning the DIE's below the compilation unit. */
7122 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
7123
7124 static_block
7125 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0,
7126 per_cu->imported_symtabs != NULL);
7127
7128 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
7129 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
7130 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
7131 addrmap to help ensure it has an accurate map of pc values belonging to
7132 this comp unit. */
7133 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
7134
7135 symtab = end_symtab_from_static_block (static_block, objfile,
7136 SECT_OFF_TEXT (objfile), 0);
7137
7138 if (symtab != NULL)
7139 {
7140 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
7141
7142 /* Set symtab language to language from DW_AT_language. If the
7143 compilation is from a C file generated by language preprocessors, do
7144 not set the language if it was already deduced by start_subfile. */
7145 if (!(cu->language == language_c && symtab->language != language_c))
7146 symtab->language = cu->language;
7147
7148 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
7149 produce DW_AT_location with location lists but it can be possibly
7150 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
7151 there were bugs in prologue debug info, fixed later in GCC-4.5
7152 by "unwind info for epilogues" patch (which is not directly related).
7153
7154 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
7155 needed, it would be wrong due to missing DW_AT_producer there.
7156
7157 Still one can confuse GDB by using non-standard GCC compilation
7158 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
7159 */
7160 if (cu->has_loclist && gcc_4_minor >= 5)
7161 symtab->locations_valid = 1;
7162
7163 if (gcc_4_minor >= 5)
7164 symtab->epilogue_unwind_valid = 1;
7165
7166 symtab->call_site_htab = cu->call_site_htab;
7167 }
7168
7169 if (dwarf2_per_objfile->using_index)
7170 per_cu->v.quick->symtab = symtab;
7171 else
7172 {
7173 struct partial_symtab *pst = per_cu->v.psymtab;
7174 pst->symtab = symtab;
7175 pst->readin = 1;
7176 }
7177
7178 /* Push it for inclusion processing later. */
7179 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
7180
7181 do_cleanups (back_to);
7182 }
7183
7184 /* Generate full symbol information for type unit PER_CU, whose DIEs have
7185 already been loaded into memory. */
7186
7187 static void
7188 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
7189 enum language pretend_language)
7190 {
7191 struct dwarf2_cu *cu = per_cu->cu;
7192 struct objfile *objfile = per_cu->objfile;
7193 struct symtab *symtab;
7194 struct cleanup *back_to, *delayed_list_cleanup;
7195 struct signatured_type *sig_type;
7196
7197 gdb_assert (per_cu->is_debug_types);
7198 sig_type = (struct signatured_type *) per_cu;
7199
7200 buildsym_init ();
7201 back_to = make_cleanup (really_free_pendings, NULL);
7202 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7203
7204 cu->list_in_scope = &file_symbols;
7205
7206 cu->language = pretend_language;
7207 cu->language_defn = language_def (cu->language);
7208
7209 /* The symbol tables are set up in read_type_unit_scope. */
7210 process_die (cu->dies, cu);
7211
7212 /* For now fudge the Go package. */
7213 if (cu->language == language_go)
7214 fixup_go_packaging (cu);
7215
7216 /* Now that we have processed all the DIEs in the CU, all the types
7217 should be complete, and it should now be safe to compute all of the
7218 physnames. */
7219 compute_delayed_physnames (cu);
7220 do_cleanups (delayed_list_cleanup);
7221
7222 /* TUs share symbol tables.
7223 If this is the first TU to use this symtab, complete the construction
7224 of it with end_expandable_symtab. Otherwise, complete the addition of
7225 this TU's symbols to the existing symtab. */
7226 if (sig_type->type_unit_group->primary_symtab == NULL)
7227 {
7228 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
7229 sig_type->type_unit_group->primary_symtab = symtab;
7230
7231 if (symtab != NULL)
7232 {
7233 /* Set symtab language to language from DW_AT_language. If the
7234 compilation is from a C file generated by language preprocessors,
7235 do not set the language if it was already deduced by
7236 start_subfile. */
7237 if (!(cu->language == language_c && symtab->language != language_c))
7238 symtab->language = cu->language;
7239 }
7240 }
7241 else
7242 {
7243 augment_type_symtab (objfile,
7244 sig_type->type_unit_group->primary_symtab);
7245 symtab = sig_type->type_unit_group->primary_symtab;
7246 }
7247
7248 if (dwarf2_per_objfile->using_index)
7249 per_cu->v.quick->symtab = symtab;
7250 else
7251 {
7252 struct partial_symtab *pst = per_cu->v.psymtab;
7253 pst->symtab = symtab;
7254 pst->readin = 1;
7255 }
7256
7257 do_cleanups (back_to);
7258 }
7259
7260 /* Process an imported unit DIE. */
7261
7262 static void
7263 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7264 {
7265 struct attribute *attr;
7266
7267 /* For now we don't handle imported units in type units. */
7268 if (cu->per_cu->is_debug_types)
7269 {
7270 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7271 " supported in type units [in module %s]"),
7272 cu->objfile->name);
7273 }
7274
7275 attr = dwarf2_attr (die, DW_AT_import, cu);
7276 if (attr != NULL)
7277 {
7278 struct dwarf2_per_cu_data *per_cu;
7279 struct symtab *imported_symtab;
7280 sect_offset offset;
7281 int is_dwz;
7282
7283 offset = dwarf2_get_ref_die_offset (attr);
7284 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7285 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
7286
7287 /* Queue the unit, if needed. */
7288 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7289 load_full_comp_unit (per_cu, cu->language);
7290
7291 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
7292 per_cu);
7293 }
7294 }
7295
7296 /* Process a die and its children. */
7297
7298 static void
7299 process_die (struct die_info *die, struct dwarf2_cu *cu)
7300 {
7301 switch (die->tag)
7302 {
7303 case DW_TAG_padding:
7304 break;
7305 case DW_TAG_compile_unit:
7306 case DW_TAG_partial_unit:
7307 read_file_scope (die, cu);
7308 break;
7309 case DW_TAG_type_unit:
7310 read_type_unit_scope (die, cu);
7311 break;
7312 case DW_TAG_subprogram:
7313 case DW_TAG_inlined_subroutine:
7314 read_func_scope (die, cu);
7315 break;
7316 case DW_TAG_lexical_block:
7317 case DW_TAG_try_block:
7318 case DW_TAG_catch_block:
7319 read_lexical_block_scope (die, cu);
7320 break;
7321 case DW_TAG_GNU_call_site:
7322 read_call_site_scope (die, cu);
7323 break;
7324 case DW_TAG_class_type:
7325 case DW_TAG_interface_type:
7326 case DW_TAG_structure_type:
7327 case DW_TAG_union_type:
7328 process_structure_scope (die, cu);
7329 break;
7330 case DW_TAG_enumeration_type:
7331 process_enumeration_scope (die, cu);
7332 break;
7333
7334 /* These dies have a type, but processing them does not create
7335 a symbol or recurse to process the children. Therefore we can
7336 read them on-demand through read_type_die. */
7337 case DW_TAG_subroutine_type:
7338 case DW_TAG_set_type:
7339 case DW_TAG_array_type:
7340 case DW_TAG_pointer_type:
7341 case DW_TAG_ptr_to_member_type:
7342 case DW_TAG_reference_type:
7343 case DW_TAG_string_type:
7344 break;
7345
7346 case DW_TAG_base_type:
7347 case DW_TAG_subrange_type:
7348 case DW_TAG_typedef:
7349 /* Add a typedef symbol for the type definition, if it has a
7350 DW_AT_name. */
7351 new_symbol (die, read_type_die (die, cu), cu);
7352 break;
7353 case DW_TAG_common_block:
7354 read_common_block (die, cu);
7355 break;
7356 case DW_TAG_common_inclusion:
7357 break;
7358 case DW_TAG_namespace:
7359 cu->processing_has_namespace_info = 1;
7360 read_namespace (die, cu);
7361 break;
7362 case DW_TAG_module:
7363 cu->processing_has_namespace_info = 1;
7364 read_module (die, cu);
7365 break;
7366 case DW_TAG_imported_declaration:
7367 case DW_TAG_imported_module:
7368 cu->processing_has_namespace_info = 1;
7369 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7370 || cu->language != language_fortran))
7371 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7372 dwarf_tag_name (die->tag));
7373 read_import_statement (die, cu);
7374 break;
7375
7376 case DW_TAG_imported_unit:
7377 process_imported_unit_die (die, cu);
7378 break;
7379
7380 default:
7381 new_symbol (die, NULL, cu);
7382 break;
7383 }
7384 }
7385 \f
7386 /* DWARF name computation. */
7387
7388 /* A helper function for dwarf2_compute_name which determines whether DIE
7389 needs to have the name of the scope prepended to the name listed in the
7390 die. */
7391
7392 static int
7393 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7394 {
7395 struct attribute *attr;
7396
7397 switch (die->tag)
7398 {
7399 case DW_TAG_namespace:
7400 case DW_TAG_typedef:
7401 case DW_TAG_class_type:
7402 case DW_TAG_interface_type:
7403 case DW_TAG_structure_type:
7404 case DW_TAG_union_type:
7405 case DW_TAG_enumeration_type:
7406 case DW_TAG_enumerator:
7407 case DW_TAG_subprogram:
7408 case DW_TAG_member:
7409 return 1;
7410
7411 case DW_TAG_variable:
7412 case DW_TAG_constant:
7413 /* We only need to prefix "globally" visible variables. These include
7414 any variable marked with DW_AT_external or any variable that
7415 lives in a namespace. [Variables in anonymous namespaces
7416 require prefixing, but they are not DW_AT_external.] */
7417
7418 if (dwarf2_attr (die, DW_AT_specification, cu))
7419 {
7420 struct dwarf2_cu *spec_cu = cu;
7421
7422 return die_needs_namespace (die_specification (die, &spec_cu),
7423 spec_cu);
7424 }
7425
7426 attr = dwarf2_attr (die, DW_AT_external, cu);
7427 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7428 && die->parent->tag != DW_TAG_module)
7429 return 0;
7430 /* A variable in a lexical block of some kind does not need a
7431 namespace, even though in C++ such variables may be external
7432 and have a mangled name. */
7433 if (die->parent->tag == DW_TAG_lexical_block
7434 || die->parent->tag == DW_TAG_try_block
7435 || die->parent->tag == DW_TAG_catch_block
7436 || die->parent->tag == DW_TAG_subprogram)
7437 return 0;
7438 return 1;
7439
7440 default:
7441 return 0;
7442 }
7443 }
7444
7445 /* Retrieve the last character from a mem_file. */
7446
7447 static void
7448 do_ui_file_peek_last (void *object, const char *buffer, long length)
7449 {
7450 char *last_char_p = (char *) object;
7451
7452 if (length > 0)
7453 *last_char_p = buffer[length - 1];
7454 }
7455
7456 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
7457 compute the physname for the object, which include a method's:
7458 - formal parameters (C++/Java),
7459 - receiver type (Go),
7460 - return type (Java).
7461
7462 The term "physname" is a bit confusing.
7463 For C++, for example, it is the demangled name.
7464 For Go, for example, it's the mangled name.
7465
7466 For Ada, return the DIE's linkage name rather than the fully qualified
7467 name. PHYSNAME is ignored..
7468
7469 The result is allocated on the objfile_obstack and canonicalized. */
7470
7471 static const char *
7472 dwarf2_compute_name (const char *name,
7473 struct die_info *die, struct dwarf2_cu *cu,
7474 int physname)
7475 {
7476 struct objfile *objfile = cu->objfile;
7477
7478 if (name == NULL)
7479 name = dwarf2_name (die, cu);
7480
7481 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7482 compute it by typename_concat inside GDB. */
7483 if (cu->language == language_ada
7484 || (cu->language == language_fortran && physname))
7485 {
7486 /* For Ada unit, we prefer the linkage name over the name, as
7487 the former contains the exported name, which the user expects
7488 to be able to reference. Ideally, we want the user to be able
7489 to reference this entity using either natural or linkage name,
7490 but we haven't started looking at this enhancement yet. */
7491 struct attribute *attr;
7492
7493 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7494 if (attr == NULL)
7495 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7496 if (attr && DW_STRING (attr))
7497 return DW_STRING (attr);
7498 }
7499
7500 /* These are the only languages we know how to qualify names in. */
7501 if (name != NULL
7502 && (cu->language == language_cplus || cu->language == language_java
7503 || cu->language == language_fortran))
7504 {
7505 if (die_needs_namespace (die, cu))
7506 {
7507 long length;
7508 const char *prefix;
7509 struct ui_file *buf;
7510
7511 prefix = determine_prefix (die, cu);
7512 buf = mem_fileopen ();
7513 if (*prefix != '\0')
7514 {
7515 char *prefixed_name = typename_concat (NULL, prefix, name,
7516 physname, cu);
7517
7518 fputs_unfiltered (prefixed_name, buf);
7519 xfree (prefixed_name);
7520 }
7521 else
7522 fputs_unfiltered (name, buf);
7523
7524 /* Template parameters may be specified in the DIE's DW_AT_name, or
7525 as children with DW_TAG_template_type_param or
7526 DW_TAG_value_type_param. If the latter, add them to the name
7527 here. If the name already has template parameters, then
7528 skip this step; some versions of GCC emit both, and
7529 it is more efficient to use the pre-computed name.
7530
7531 Something to keep in mind about this process: it is very
7532 unlikely, or in some cases downright impossible, to produce
7533 something that will match the mangled name of a function.
7534 If the definition of the function has the same debug info,
7535 we should be able to match up with it anyway. But fallbacks
7536 using the minimal symbol, for instance to find a method
7537 implemented in a stripped copy of libstdc++, will not work.
7538 If we do not have debug info for the definition, we will have to
7539 match them up some other way.
7540
7541 When we do name matching there is a related problem with function
7542 templates; two instantiated function templates are allowed to
7543 differ only by their return types, which we do not add here. */
7544
7545 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7546 {
7547 struct attribute *attr;
7548 struct die_info *child;
7549 int first = 1;
7550
7551 die->building_fullname = 1;
7552
7553 for (child = die->child; child != NULL; child = child->sibling)
7554 {
7555 struct type *type;
7556 LONGEST value;
7557 gdb_byte *bytes;
7558 struct dwarf2_locexpr_baton *baton;
7559 struct value *v;
7560
7561 if (child->tag != DW_TAG_template_type_param
7562 && child->tag != DW_TAG_template_value_param)
7563 continue;
7564
7565 if (first)
7566 {
7567 fputs_unfiltered ("<", buf);
7568 first = 0;
7569 }
7570 else
7571 fputs_unfiltered (", ", buf);
7572
7573 attr = dwarf2_attr (child, DW_AT_type, cu);
7574 if (attr == NULL)
7575 {
7576 complaint (&symfile_complaints,
7577 _("template parameter missing DW_AT_type"));
7578 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7579 continue;
7580 }
7581 type = die_type (child, cu);
7582
7583 if (child->tag == DW_TAG_template_type_param)
7584 {
7585 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
7586 continue;
7587 }
7588
7589 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7590 if (attr == NULL)
7591 {
7592 complaint (&symfile_complaints,
7593 _("template parameter missing "
7594 "DW_AT_const_value"));
7595 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7596 continue;
7597 }
7598
7599 dwarf2_const_value_attr (attr, type, name,
7600 &cu->comp_unit_obstack, cu,
7601 &value, &bytes, &baton);
7602
7603 if (TYPE_NOSIGN (type))
7604 /* GDB prints characters as NUMBER 'CHAR'. If that's
7605 changed, this can use value_print instead. */
7606 c_printchar (value, type, buf);
7607 else
7608 {
7609 struct value_print_options opts;
7610
7611 if (baton != NULL)
7612 v = dwarf2_evaluate_loc_desc (type, NULL,
7613 baton->data,
7614 baton->size,
7615 baton->per_cu);
7616 else if (bytes != NULL)
7617 {
7618 v = allocate_value (type);
7619 memcpy (value_contents_writeable (v), bytes,
7620 TYPE_LENGTH (type));
7621 }
7622 else
7623 v = value_from_longest (type, value);
7624
7625 /* Specify decimal so that we do not depend on
7626 the radix. */
7627 get_formatted_print_options (&opts, 'd');
7628 opts.raw = 1;
7629 value_print (v, buf, &opts);
7630 release_value (v);
7631 value_free (v);
7632 }
7633 }
7634
7635 die->building_fullname = 0;
7636
7637 if (!first)
7638 {
7639 /* Close the argument list, with a space if necessary
7640 (nested templates). */
7641 char last_char = '\0';
7642 ui_file_put (buf, do_ui_file_peek_last, &last_char);
7643 if (last_char == '>')
7644 fputs_unfiltered (" >", buf);
7645 else
7646 fputs_unfiltered (">", buf);
7647 }
7648 }
7649
7650 /* For Java and C++ methods, append formal parameter type
7651 information, if PHYSNAME. */
7652
7653 if (physname && die->tag == DW_TAG_subprogram
7654 && (cu->language == language_cplus
7655 || cu->language == language_java))
7656 {
7657 struct type *type = read_type_die (die, cu);
7658
7659 c_type_print_args (type, buf, 1, cu->language,
7660 &type_print_raw_options);
7661
7662 if (cu->language == language_java)
7663 {
7664 /* For java, we must append the return type to method
7665 names. */
7666 if (die->tag == DW_TAG_subprogram)
7667 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
7668 0, 0, &type_print_raw_options);
7669 }
7670 else if (cu->language == language_cplus)
7671 {
7672 /* Assume that an artificial first parameter is
7673 "this", but do not crash if it is not. RealView
7674 marks unnamed (and thus unused) parameters as
7675 artificial; there is no way to differentiate
7676 the two cases. */
7677 if (TYPE_NFIELDS (type) > 0
7678 && TYPE_FIELD_ARTIFICIAL (type, 0)
7679 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
7680 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
7681 0))))
7682 fputs_unfiltered (" const", buf);
7683 }
7684 }
7685
7686 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
7687 &length);
7688 ui_file_delete (buf);
7689
7690 if (cu->language == language_cplus)
7691 {
7692 const char *cname
7693 = dwarf2_canonicalize_name (name, cu,
7694 &objfile->objfile_obstack);
7695
7696 if (cname != NULL)
7697 name = cname;
7698 }
7699 }
7700 }
7701
7702 return name;
7703 }
7704
7705 /* Return the fully qualified name of DIE, based on its DW_AT_name.
7706 If scope qualifiers are appropriate they will be added. The result
7707 will be allocated on the objfile_obstack, or NULL if the DIE does
7708 not have a name. NAME may either be from a previous call to
7709 dwarf2_name or NULL.
7710
7711 The output string will be canonicalized (if C++/Java). */
7712
7713 static const char *
7714 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
7715 {
7716 return dwarf2_compute_name (name, die, cu, 0);
7717 }
7718
7719 /* Construct a physname for the given DIE in CU. NAME may either be
7720 from a previous call to dwarf2_name or NULL. The result will be
7721 allocated on the objfile_objstack or NULL if the DIE does not have a
7722 name.
7723
7724 The output string will be canonicalized (if C++/Java). */
7725
7726 static const char *
7727 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
7728 {
7729 struct objfile *objfile = cu->objfile;
7730 struct attribute *attr;
7731 const char *retval, *mangled = NULL, *canon = NULL;
7732 struct cleanup *back_to;
7733 int need_copy = 1;
7734
7735 /* In this case dwarf2_compute_name is just a shortcut not building anything
7736 on its own. */
7737 if (!die_needs_namespace (die, cu))
7738 return dwarf2_compute_name (name, die, cu, 1);
7739
7740 back_to = make_cleanup (null_cleanup, NULL);
7741
7742 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7743 if (!attr)
7744 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7745
7746 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
7747 has computed. */
7748 if (attr && DW_STRING (attr))
7749 {
7750 char *demangled;
7751
7752 mangled = DW_STRING (attr);
7753
7754 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
7755 type. It is easier for GDB users to search for such functions as
7756 `name(params)' than `long name(params)'. In such case the minimal
7757 symbol names do not match the full symbol names but for template
7758 functions there is never a need to look up their definition from their
7759 declaration so the only disadvantage remains the minimal symbol
7760 variant `long name(params)' does not have the proper inferior type.
7761 */
7762
7763 if (cu->language == language_go)
7764 {
7765 /* This is a lie, but we already lie to the caller new_symbol_full.
7766 new_symbol_full assumes we return the mangled name.
7767 This just undoes that lie until things are cleaned up. */
7768 demangled = NULL;
7769 }
7770 else
7771 {
7772 demangled = cplus_demangle (mangled,
7773 (DMGL_PARAMS | DMGL_ANSI
7774 | (cu->language == language_java
7775 ? DMGL_JAVA | DMGL_RET_POSTFIX
7776 : DMGL_RET_DROP)));
7777 }
7778 if (demangled)
7779 {
7780 make_cleanup (xfree, demangled);
7781 canon = demangled;
7782 }
7783 else
7784 {
7785 canon = mangled;
7786 need_copy = 0;
7787 }
7788 }
7789
7790 if (canon == NULL || check_physname)
7791 {
7792 const char *physname = dwarf2_compute_name (name, die, cu, 1);
7793
7794 if (canon != NULL && strcmp (physname, canon) != 0)
7795 {
7796 /* It may not mean a bug in GDB. The compiler could also
7797 compute DW_AT_linkage_name incorrectly. But in such case
7798 GDB would need to be bug-to-bug compatible. */
7799
7800 complaint (&symfile_complaints,
7801 _("Computed physname <%s> does not match demangled <%s> "
7802 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
7803 physname, canon, mangled, die->offset.sect_off, objfile->name);
7804
7805 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
7806 is available here - over computed PHYSNAME. It is safer
7807 against both buggy GDB and buggy compilers. */
7808
7809 retval = canon;
7810 }
7811 else
7812 {
7813 retval = physname;
7814 need_copy = 0;
7815 }
7816 }
7817 else
7818 retval = canon;
7819
7820 if (need_copy)
7821 retval = obstack_copy0 (&objfile->objfile_obstack, retval, strlen (retval));
7822
7823 do_cleanups (back_to);
7824 return retval;
7825 }
7826
7827 /* Read the import statement specified by the given die and record it. */
7828
7829 static void
7830 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
7831 {
7832 struct objfile *objfile = cu->objfile;
7833 struct attribute *import_attr;
7834 struct die_info *imported_die, *child_die;
7835 struct dwarf2_cu *imported_cu;
7836 const char *imported_name;
7837 const char *imported_name_prefix;
7838 const char *canonical_name;
7839 const char *import_alias;
7840 const char *imported_declaration = NULL;
7841 const char *import_prefix;
7842 VEC (const_char_ptr) *excludes = NULL;
7843 struct cleanup *cleanups;
7844
7845 import_attr = dwarf2_attr (die, DW_AT_import, cu);
7846 if (import_attr == NULL)
7847 {
7848 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7849 dwarf_tag_name (die->tag));
7850 return;
7851 }
7852
7853 imported_cu = cu;
7854 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
7855 imported_name = dwarf2_name (imported_die, imported_cu);
7856 if (imported_name == NULL)
7857 {
7858 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
7859
7860 The import in the following code:
7861 namespace A
7862 {
7863 typedef int B;
7864 }
7865
7866 int main ()
7867 {
7868 using A::B;
7869 B b;
7870 return b;
7871 }
7872
7873 ...
7874 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
7875 <52> DW_AT_decl_file : 1
7876 <53> DW_AT_decl_line : 6
7877 <54> DW_AT_import : <0x75>
7878 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
7879 <59> DW_AT_name : B
7880 <5b> DW_AT_decl_file : 1
7881 <5c> DW_AT_decl_line : 2
7882 <5d> DW_AT_type : <0x6e>
7883 ...
7884 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
7885 <76> DW_AT_byte_size : 4
7886 <77> DW_AT_encoding : 5 (signed)
7887
7888 imports the wrong die ( 0x75 instead of 0x58 ).
7889 This case will be ignored until the gcc bug is fixed. */
7890 return;
7891 }
7892
7893 /* Figure out the local name after import. */
7894 import_alias = dwarf2_name (die, cu);
7895
7896 /* Figure out where the statement is being imported to. */
7897 import_prefix = determine_prefix (die, cu);
7898
7899 /* Figure out what the scope of the imported die is and prepend it
7900 to the name of the imported die. */
7901 imported_name_prefix = determine_prefix (imported_die, imported_cu);
7902
7903 if (imported_die->tag != DW_TAG_namespace
7904 && imported_die->tag != DW_TAG_module)
7905 {
7906 imported_declaration = imported_name;
7907 canonical_name = imported_name_prefix;
7908 }
7909 else if (strlen (imported_name_prefix) > 0)
7910 canonical_name = obconcat (&objfile->objfile_obstack,
7911 imported_name_prefix, "::", imported_name,
7912 (char *) NULL);
7913 else
7914 canonical_name = imported_name;
7915
7916 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
7917
7918 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
7919 for (child_die = die->child; child_die && child_die->tag;
7920 child_die = sibling_die (child_die))
7921 {
7922 /* DWARF-4: A Fortran use statement with a “rename list” may be
7923 represented by an imported module entry with an import attribute
7924 referring to the module and owned entries corresponding to those
7925 entities that are renamed as part of being imported. */
7926
7927 if (child_die->tag != DW_TAG_imported_declaration)
7928 {
7929 complaint (&symfile_complaints,
7930 _("child DW_TAG_imported_declaration expected "
7931 "- DIE at 0x%x [in module %s]"),
7932 child_die->offset.sect_off, objfile->name);
7933 continue;
7934 }
7935
7936 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
7937 if (import_attr == NULL)
7938 {
7939 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7940 dwarf_tag_name (child_die->tag));
7941 continue;
7942 }
7943
7944 imported_cu = cu;
7945 imported_die = follow_die_ref_or_sig (child_die, import_attr,
7946 &imported_cu);
7947 imported_name = dwarf2_name (imported_die, imported_cu);
7948 if (imported_name == NULL)
7949 {
7950 complaint (&symfile_complaints,
7951 _("child DW_TAG_imported_declaration has unknown "
7952 "imported name - DIE at 0x%x [in module %s]"),
7953 child_die->offset.sect_off, objfile->name);
7954 continue;
7955 }
7956
7957 VEC_safe_push (const_char_ptr, excludes, imported_name);
7958
7959 process_die (child_die, cu);
7960 }
7961
7962 cp_add_using_directive (import_prefix,
7963 canonical_name,
7964 import_alias,
7965 imported_declaration,
7966 excludes,
7967 0,
7968 &objfile->objfile_obstack);
7969
7970 do_cleanups (cleanups);
7971 }
7972
7973 /* Cleanup function for handle_DW_AT_stmt_list. */
7974
7975 static void
7976 free_cu_line_header (void *arg)
7977 {
7978 struct dwarf2_cu *cu = arg;
7979
7980 free_line_header (cu->line_header);
7981 cu->line_header = NULL;
7982 }
7983
7984 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
7985 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
7986 this, it was first present in GCC release 4.3.0. */
7987
7988 static int
7989 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
7990 {
7991 if (!cu->checked_producer)
7992 check_producer (cu);
7993
7994 return cu->producer_is_gcc_lt_4_3;
7995 }
7996
7997 static void
7998 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
7999 const char **name, const char **comp_dir)
8000 {
8001 struct attribute *attr;
8002
8003 *name = NULL;
8004 *comp_dir = NULL;
8005
8006 /* Find the filename. Do not use dwarf2_name here, since the filename
8007 is not a source language identifier. */
8008 attr = dwarf2_attr (die, DW_AT_name, cu);
8009 if (attr)
8010 {
8011 *name = DW_STRING (attr);
8012 }
8013
8014 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
8015 if (attr)
8016 *comp_dir = DW_STRING (attr);
8017 else if (producer_is_gcc_lt_4_3 (cu) && *name != NULL
8018 && IS_ABSOLUTE_PATH (*name))
8019 {
8020 char *d = ldirname (*name);
8021
8022 *comp_dir = d;
8023 if (d != NULL)
8024 make_cleanup (xfree, d);
8025 }
8026 if (*comp_dir != NULL)
8027 {
8028 /* Irix 6.2 native cc prepends <machine>.: to the compilation
8029 directory, get rid of it. */
8030 char *cp = strchr (*comp_dir, ':');
8031
8032 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
8033 *comp_dir = cp + 1;
8034 }
8035
8036 if (*name == NULL)
8037 *name = "<unknown>";
8038 }
8039
8040 /* Handle DW_AT_stmt_list for a compilation unit.
8041 DIE is the DW_TAG_compile_unit die for CU.
8042 COMP_DIR is the compilation directory.
8043 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
8044
8045 static void
8046 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
8047 const char *comp_dir)
8048 {
8049 struct attribute *attr;
8050
8051 gdb_assert (! cu->per_cu->is_debug_types);
8052
8053 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
8054 if (attr)
8055 {
8056 unsigned int line_offset = DW_UNSND (attr);
8057 struct line_header *line_header
8058 = dwarf_decode_line_header (line_offset, cu);
8059
8060 if (line_header)
8061 {
8062 cu->line_header = line_header;
8063 make_cleanup (free_cu_line_header, cu);
8064 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
8065 }
8066 }
8067 }
8068
8069 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
8070
8071 static void
8072 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
8073 {
8074 struct objfile *objfile = dwarf2_per_objfile->objfile;
8075 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
8076 CORE_ADDR lowpc = ((CORE_ADDR) -1);
8077 CORE_ADDR highpc = ((CORE_ADDR) 0);
8078 struct attribute *attr;
8079 const char *name = NULL;
8080 const char *comp_dir = NULL;
8081 struct die_info *child_die;
8082 bfd *abfd = objfile->obfd;
8083 CORE_ADDR baseaddr;
8084
8085 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8086
8087 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
8088
8089 /* If we didn't find a lowpc, set it to highpc to avoid complaints
8090 from finish_block. */
8091 if (lowpc == ((CORE_ADDR) -1))
8092 lowpc = highpc;
8093 lowpc += baseaddr;
8094 highpc += baseaddr;
8095
8096 find_file_and_directory (die, cu, &name, &comp_dir);
8097
8098 prepare_one_comp_unit (cu, die, cu->language);
8099
8100 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
8101 standardised yet. As a workaround for the language detection we fall
8102 back to the DW_AT_producer string. */
8103 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
8104 cu->language = language_opencl;
8105
8106 /* Similar hack for Go. */
8107 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
8108 set_cu_language (DW_LANG_Go, cu);
8109
8110 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
8111
8112 /* Decode line number information if present. We do this before
8113 processing child DIEs, so that the line header table is available
8114 for DW_AT_decl_file. */
8115 handle_DW_AT_stmt_list (die, cu, comp_dir);
8116
8117 /* Process all dies in compilation unit. */
8118 if (die->child != NULL)
8119 {
8120 child_die = die->child;
8121 while (child_die && child_die->tag)
8122 {
8123 process_die (child_die, cu);
8124 child_die = sibling_die (child_die);
8125 }
8126 }
8127
8128 /* Decode macro information, if present. Dwarf 2 macro information
8129 refers to information in the line number info statement program
8130 header, so we can only read it if we've read the header
8131 successfully. */
8132 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
8133 if (attr && cu->line_header)
8134 {
8135 if (dwarf2_attr (die, DW_AT_macro_info, cu))
8136 complaint (&symfile_complaints,
8137 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
8138
8139 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
8140 }
8141 else
8142 {
8143 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
8144 if (attr && cu->line_header)
8145 {
8146 unsigned int macro_offset = DW_UNSND (attr);
8147
8148 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
8149 }
8150 }
8151
8152 do_cleanups (back_to);
8153 }
8154
8155 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
8156 Create the set of symtabs used by this TU, or if this TU is sharing
8157 symtabs with another TU and the symtabs have already been created
8158 then restore those symtabs in the line header.
8159 We don't need the pc/line-number mapping for type units. */
8160
8161 static void
8162 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
8163 {
8164 struct objfile *objfile = dwarf2_per_objfile->objfile;
8165 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8166 struct type_unit_group *tu_group;
8167 int first_time;
8168 struct line_header *lh;
8169 struct attribute *attr;
8170 unsigned int i, line_offset;
8171 struct signatured_type *sig_type;
8172
8173 gdb_assert (per_cu->is_debug_types);
8174 sig_type = (struct signatured_type *) per_cu;
8175
8176 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
8177
8178 /* If we're using .gdb_index (includes -readnow) then
8179 per_cu->s.type_unit_group may not have been set up yet. */
8180 if (sig_type->type_unit_group == NULL)
8181 sig_type->type_unit_group = get_type_unit_group (cu, attr);
8182 tu_group = sig_type->type_unit_group;
8183
8184 /* If we've already processed this stmt_list there's no real need to
8185 do it again, we could fake it and just recreate the part we need
8186 (file name,index -> symtab mapping). If data shows this optimization
8187 is useful we can do it then. */
8188 first_time = tu_group->primary_symtab == NULL;
8189
8190 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
8191 debug info. */
8192 lh = NULL;
8193 if (attr != NULL)
8194 {
8195 line_offset = DW_UNSND (attr);
8196 lh = dwarf_decode_line_header (line_offset, cu);
8197 }
8198 if (lh == NULL)
8199 {
8200 if (first_time)
8201 dwarf2_start_symtab (cu, "", NULL, 0);
8202 else
8203 {
8204 gdb_assert (tu_group->symtabs == NULL);
8205 restart_symtab (0);
8206 }
8207 /* Note: The primary symtab will get allocated at the end. */
8208 return;
8209 }
8210
8211 cu->line_header = lh;
8212 make_cleanup (free_cu_line_header, cu);
8213
8214 if (first_time)
8215 {
8216 dwarf2_start_symtab (cu, "", NULL, 0);
8217
8218 tu_group->num_symtabs = lh->num_file_names;
8219 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
8220
8221 for (i = 0; i < lh->num_file_names; ++i)
8222 {
8223 char *dir = NULL;
8224 struct file_entry *fe = &lh->file_names[i];
8225
8226 if (fe->dir_index)
8227 dir = lh->include_dirs[fe->dir_index - 1];
8228 dwarf2_start_subfile (fe->name, dir, NULL);
8229
8230 /* Note: We don't have to watch for the main subfile here, type units
8231 don't have DW_AT_name. */
8232
8233 if (current_subfile->symtab == NULL)
8234 {
8235 /* NOTE: start_subfile will recognize when it's been passed
8236 a file it has already seen. So we can't assume there's a
8237 simple mapping from lh->file_names to subfiles,
8238 lh->file_names may contain dups. */
8239 current_subfile->symtab = allocate_symtab (current_subfile->name,
8240 objfile);
8241 }
8242
8243 fe->symtab = current_subfile->symtab;
8244 tu_group->symtabs[i] = fe->symtab;
8245 }
8246 }
8247 else
8248 {
8249 restart_symtab (0);
8250
8251 for (i = 0; i < lh->num_file_names; ++i)
8252 {
8253 struct file_entry *fe = &lh->file_names[i];
8254
8255 fe->symtab = tu_group->symtabs[i];
8256 }
8257 }
8258
8259 /* The main symtab is allocated last. Type units don't have DW_AT_name
8260 so they don't have a "real" (so to speak) symtab anyway.
8261 There is later code that will assign the main symtab to all symbols
8262 that don't have one. We need to handle the case of a symbol with a
8263 missing symtab (DW_AT_decl_file) anyway. */
8264 }
8265
8266 /* Process DW_TAG_type_unit.
8267 For TUs we want to skip the first top level sibling if it's not the
8268 actual type being defined by this TU. In this case the first top
8269 level sibling is there to provide context only. */
8270
8271 static void
8272 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8273 {
8274 struct die_info *child_die;
8275
8276 prepare_one_comp_unit (cu, die, language_minimal);
8277
8278 /* Initialize (or reinitialize) the machinery for building symtabs.
8279 We do this before processing child DIEs, so that the line header table
8280 is available for DW_AT_decl_file. */
8281 setup_type_unit_groups (die, cu);
8282
8283 if (die->child != NULL)
8284 {
8285 child_die = die->child;
8286 while (child_die && child_die->tag)
8287 {
8288 process_die (child_die, cu);
8289 child_die = sibling_die (child_die);
8290 }
8291 }
8292 }
8293 \f
8294 /* DWO/DWP files.
8295
8296 http://gcc.gnu.org/wiki/DebugFission
8297 http://gcc.gnu.org/wiki/DebugFissionDWP
8298
8299 To simplify handling of both DWO files ("object" files with the DWARF info)
8300 and DWP files (a file with the DWOs packaged up into one file), we treat
8301 DWP files as having a collection of virtual DWO files. */
8302
8303 static hashval_t
8304 hash_dwo_file (const void *item)
8305 {
8306 const struct dwo_file *dwo_file = item;
8307
8308 return (htab_hash_string (dwo_file->dwo_name)
8309 + htab_hash_string (dwo_file->comp_dir));
8310 }
8311
8312 static int
8313 eq_dwo_file (const void *item_lhs, const void *item_rhs)
8314 {
8315 const struct dwo_file *lhs = item_lhs;
8316 const struct dwo_file *rhs = item_rhs;
8317
8318 return (strcmp (lhs->dwo_name, rhs->dwo_name) == 0
8319 && strcmp (lhs->comp_dir, rhs->comp_dir) == 0);
8320 }
8321
8322 /* Allocate a hash table for DWO files. */
8323
8324 static htab_t
8325 allocate_dwo_file_hash_table (void)
8326 {
8327 struct objfile *objfile = dwarf2_per_objfile->objfile;
8328
8329 return htab_create_alloc_ex (41,
8330 hash_dwo_file,
8331 eq_dwo_file,
8332 NULL,
8333 &objfile->objfile_obstack,
8334 hashtab_obstack_allocate,
8335 dummy_obstack_deallocate);
8336 }
8337
8338 /* Lookup DWO file DWO_NAME. */
8339
8340 static void **
8341 lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
8342 {
8343 struct dwo_file find_entry;
8344 void **slot;
8345
8346 if (dwarf2_per_objfile->dwo_files == NULL)
8347 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8348
8349 memset (&find_entry, 0, sizeof (find_entry));
8350 find_entry.dwo_name = dwo_name;
8351 find_entry.comp_dir = comp_dir;
8352 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8353
8354 return slot;
8355 }
8356
8357 static hashval_t
8358 hash_dwo_unit (const void *item)
8359 {
8360 const struct dwo_unit *dwo_unit = item;
8361
8362 /* This drops the top 32 bits of the id, but is ok for a hash. */
8363 return dwo_unit->signature;
8364 }
8365
8366 static int
8367 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8368 {
8369 const struct dwo_unit *lhs = item_lhs;
8370 const struct dwo_unit *rhs = item_rhs;
8371
8372 /* The signature is assumed to be unique within the DWO file.
8373 So while object file CU dwo_id's always have the value zero,
8374 that's OK, assuming each object file DWO file has only one CU,
8375 and that's the rule for now. */
8376 return lhs->signature == rhs->signature;
8377 }
8378
8379 /* Allocate a hash table for DWO CUs,TUs.
8380 There is one of these tables for each of CUs,TUs for each DWO file. */
8381
8382 static htab_t
8383 allocate_dwo_unit_table (struct objfile *objfile)
8384 {
8385 /* Start out with a pretty small number.
8386 Generally DWO files contain only one CU and maybe some TUs. */
8387 return htab_create_alloc_ex (3,
8388 hash_dwo_unit,
8389 eq_dwo_unit,
8390 NULL,
8391 &objfile->objfile_obstack,
8392 hashtab_obstack_allocate,
8393 dummy_obstack_deallocate);
8394 }
8395
8396 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
8397
8398 struct create_dwo_info_table_data
8399 {
8400 struct dwo_file *dwo_file;
8401 htab_t cu_htab;
8402 };
8403
8404 /* die_reader_func for create_dwo_debug_info_hash_table. */
8405
8406 static void
8407 create_dwo_debug_info_hash_table_reader (const struct die_reader_specs *reader,
8408 gdb_byte *info_ptr,
8409 struct die_info *comp_unit_die,
8410 int has_children,
8411 void *datap)
8412 {
8413 struct dwarf2_cu *cu = reader->cu;
8414 struct objfile *objfile = dwarf2_per_objfile->objfile;
8415 sect_offset offset = cu->per_cu->offset;
8416 struct dwarf2_section_info *section = cu->per_cu->section;
8417 struct create_dwo_info_table_data *data = datap;
8418 struct dwo_file *dwo_file = data->dwo_file;
8419 htab_t cu_htab = data->cu_htab;
8420 void **slot;
8421 struct attribute *attr;
8422 struct dwo_unit *dwo_unit;
8423
8424 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8425 if (attr == NULL)
8426 {
8427 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
8428 " its dwo_id [in module %s]"),
8429 offset.sect_off, dwo_file->dwo_name);
8430 return;
8431 }
8432
8433 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8434 dwo_unit->dwo_file = dwo_file;
8435 dwo_unit->signature = DW_UNSND (attr);
8436 dwo_unit->section = section;
8437 dwo_unit->offset = offset;
8438 dwo_unit->length = cu->per_cu->length;
8439
8440 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
8441 gdb_assert (slot != NULL);
8442 if (*slot != NULL)
8443 {
8444 const struct dwo_unit *dup_dwo_unit = *slot;
8445
8446 complaint (&symfile_complaints,
8447 _("debug entry at offset 0x%x is duplicate to the entry at"
8448 " offset 0x%x, dwo_id 0x%s [in module %s]"),
8449 offset.sect_off, dup_dwo_unit->offset.sect_off,
8450 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
8451 dwo_file->dwo_name);
8452 }
8453 else
8454 *slot = dwo_unit;
8455
8456 if (dwarf2_read_debug)
8457 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
8458 offset.sect_off,
8459 phex (dwo_unit->signature,
8460 sizeof (dwo_unit->signature)));
8461 }
8462
8463 /* Create a hash table to map DWO IDs to their CU entry in
8464 .debug_info.dwo in DWO_FILE.
8465 Note: This function processes DWO files only, not DWP files.
8466 Note: A DWO file generally contains one CU, but we don't assume this. */
8467
8468 static htab_t
8469 create_dwo_debug_info_hash_table (struct dwo_file *dwo_file)
8470 {
8471 struct objfile *objfile = dwarf2_per_objfile->objfile;
8472 struct dwarf2_section_info *section = &dwo_file->sections.info;
8473 bfd *abfd;
8474 htab_t cu_htab;
8475 gdb_byte *info_ptr, *end_ptr;
8476 struct create_dwo_info_table_data create_dwo_info_table_data;
8477
8478 dwarf2_read_section (objfile, section);
8479 info_ptr = section->buffer;
8480
8481 if (info_ptr == NULL)
8482 return NULL;
8483
8484 /* We can't set abfd until now because the section may be empty or
8485 not present, in which case section->asection will be NULL. */
8486 abfd = section->asection->owner;
8487
8488 if (dwarf2_read_debug)
8489 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
8490 bfd_get_filename (abfd));
8491
8492 cu_htab = allocate_dwo_unit_table (objfile);
8493
8494 create_dwo_info_table_data.dwo_file = dwo_file;
8495 create_dwo_info_table_data.cu_htab = cu_htab;
8496
8497 end_ptr = info_ptr + section->size;
8498 while (info_ptr < end_ptr)
8499 {
8500 struct dwarf2_per_cu_data per_cu;
8501
8502 memset (&per_cu, 0, sizeof (per_cu));
8503 per_cu.objfile = objfile;
8504 per_cu.is_debug_types = 0;
8505 per_cu.offset.sect_off = info_ptr - section->buffer;
8506 per_cu.section = section;
8507
8508 init_cutu_and_read_dies_no_follow (&per_cu,
8509 &dwo_file->sections.abbrev,
8510 dwo_file,
8511 create_dwo_debug_info_hash_table_reader,
8512 &create_dwo_info_table_data);
8513
8514 info_ptr += per_cu.length;
8515 }
8516
8517 return cu_htab;
8518 }
8519
8520 /* DWP file .debug_{cu,tu}_index section format:
8521 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8522
8523 Both index sections have the same format, and serve to map a 64-bit
8524 signature to a set of section numbers. Each section begins with a header,
8525 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8526 indexes, and a pool of 32-bit section numbers. The index sections will be
8527 aligned at 8-byte boundaries in the file.
8528
8529 The index section header contains two unsigned 32-bit values (using the
8530 byte order of the application binary):
8531
8532 N, the number of compilation units or type units in the index
8533 M, the number of slots in the hash table
8534
8535 (We assume that N and M will not exceed 2^32 - 1.)
8536
8537 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
8538
8539 The hash table begins at offset 8 in the section, and consists of an array
8540 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
8541 order of the application binary). Unused slots in the hash table are 0.
8542 (We rely on the extreme unlikeliness of a signature being exactly 0.)
8543
8544 The parallel table begins immediately after the hash table
8545 (at offset 8 + 8 * M from the beginning of the section), and consists of an
8546 array of 32-bit indexes (using the byte order of the application binary),
8547 corresponding 1-1 with slots in the hash table. Each entry in the parallel
8548 table contains a 32-bit index into the pool of section numbers. For unused
8549 hash table slots, the corresponding entry in the parallel table will be 0.
8550
8551 Given a 64-bit compilation unit signature or a type signature S, an entry
8552 in the hash table is located as follows:
8553
8554 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8555 the low-order k bits all set to 1.
8556
8557 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
8558
8559 3) If the hash table entry at index H matches the signature, use that
8560 entry. If the hash table entry at index H is unused (all zeroes),
8561 terminate the search: the signature is not present in the table.
8562
8563 4) Let H = (H + H') modulo M. Repeat at Step 3.
8564
8565 Because M > N and H' and M are relatively prime, the search is guaranteed
8566 to stop at an unused slot or find the match.
8567
8568 The pool of section numbers begins immediately following the hash table
8569 (at offset 8 + 12 * M from the beginning of the section). The pool of
8570 section numbers consists of an array of 32-bit words (using the byte order
8571 of the application binary). Each item in the array is indexed starting
8572 from 0. The hash table entry provides the index of the first section
8573 number in the set. Additional section numbers in the set follow, and the
8574 set is terminated by a 0 entry (section number 0 is not used in ELF).
8575
8576 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8577 section must be the first entry in the set, and the .debug_abbrev.dwo must
8578 be the second entry. Other members of the set may follow in any order. */
8579
8580 /* Create a hash table to map DWO IDs to their CU/TU entry in
8581 .debug_{info,types}.dwo in DWP_FILE.
8582 Returns NULL if there isn't one.
8583 Note: This function processes DWP files only, not DWO files. */
8584
8585 static struct dwp_hash_table *
8586 create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
8587 {
8588 struct objfile *objfile = dwarf2_per_objfile->objfile;
8589 bfd *dbfd = dwp_file->dbfd;
8590 char *index_ptr, *index_end;
8591 struct dwarf2_section_info *index;
8592 uint32_t version, nr_units, nr_slots;
8593 struct dwp_hash_table *htab;
8594
8595 if (is_debug_types)
8596 index = &dwp_file->sections.tu_index;
8597 else
8598 index = &dwp_file->sections.cu_index;
8599
8600 if (dwarf2_section_empty_p (index))
8601 return NULL;
8602 dwarf2_read_section (objfile, index);
8603
8604 index_ptr = index->buffer;
8605 index_end = index_ptr + index->size;
8606
8607 version = read_4_bytes (dbfd, index_ptr);
8608 index_ptr += 8; /* Skip the unused word. */
8609 nr_units = read_4_bytes (dbfd, index_ptr);
8610 index_ptr += 4;
8611 nr_slots = read_4_bytes (dbfd, index_ptr);
8612 index_ptr += 4;
8613
8614 if (version != 1)
8615 {
8616 error (_("Dwarf Error: unsupported DWP file version (%u)"
8617 " [in module %s]"),
8618 version, dwp_file->name);
8619 }
8620 if (nr_slots != (nr_slots & -nr_slots))
8621 {
8622 error (_("Dwarf Error: number of slots in DWP hash table (%u)"
8623 " is not power of 2 [in module %s]"),
8624 nr_slots, dwp_file->name);
8625 }
8626
8627 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
8628 htab->nr_units = nr_units;
8629 htab->nr_slots = nr_slots;
8630 htab->hash_table = index_ptr;
8631 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
8632 htab->section_pool = htab->unit_table + sizeof (uint32_t) * nr_slots;
8633
8634 return htab;
8635 }
8636
8637 /* Update SECTIONS with the data from SECTP.
8638
8639 This function is like the other "locate" section routines that are
8640 passed to bfd_map_over_sections, but in this context the sections to
8641 read comes from the DWP hash table, not the full ELF section table.
8642
8643 The result is non-zero for success, or zero if an error was found. */
8644
8645 static int
8646 locate_virtual_dwo_sections (asection *sectp,
8647 struct virtual_dwo_sections *sections)
8648 {
8649 const struct dwop_section_names *names = &dwop_section_names;
8650
8651 if (section_is_p (sectp->name, &names->abbrev_dwo))
8652 {
8653 /* There can be only one. */
8654 if (sections->abbrev.asection != NULL)
8655 return 0;
8656 sections->abbrev.asection = sectp;
8657 sections->abbrev.size = bfd_get_section_size (sectp);
8658 }
8659 else if (section_is_p (sectp->name, &names->info_dwo)
8660 || section_is_p (sectp->name, &names->types_dwo))
8661 {
8662 /* There can be only one. */
8663 if (sections->info_or_types.asection != NULL)
8664 return 0;
8665 sections->info_or_types.asection = sectp;
8666 sections->info_or_types.size = bfd_get_section_size (sectp);
8667 }
8668 else if (section_is_p (sectp->name, &names->line_dwo))
8669 {
8670 /* There can be only one. */
8671 if (sections->line.asection != NULL)
8672 return 0;
8673 sections->line.asection = sectp;
8674 sections->line.size = bfd_get_section_size (sectp);
8675 }
8676 else if (section_is_p (sectp->name, &names->loc_dwo))
8677 {
8678 /* There can be only one. */
8679 if (sections->loc.asection != NULL)
8680 return 0;
8681 sections->loc.asection = sectp;
8682 sections->loc.size = bfd_get_section_size (sectp);
8683 }
8684 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8685 {
8686 /* There can be only one. */
8687 if (sections->macinfo.asection != NULL)
8688 return 0;
8689 sections->macinfo.asection = sectp;
8690 sections->macinfo.size = bfd_get_section_size (sectp);
8691 }
8692 else if (section_is_p (sectp->name, &names->macro_dwo))
8693 {
8694 /* There can be only one. */
8695 if (sections->macro.asection != NULL)
8696 return 0;
8697 sections->macro.asection = sectp;
8698 sections->macro.size = bfd_get_section_size (sectp);
8699 }
8700 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8701 {
8702 /* There can be only one. */
8703 if (sections->str_offsets.asection != NULL)
8704 return 0;
8705 sections->str_offsets.asection = sectp;
8706 sections->str_offsets.size = bfd_get_section_size (sectp);
8707 }
8708 else
8709 {
8710 /* No other kind of section is valid. */
8711 return 0;
8712 }
8713
8714 return 1;
8715 }
8716
8717 /* Create a dwo_unit object for the DWO with signature SIGNATURE.
8718 HTAB is the hash table from the DWP file.
8719 SECTION_INDEX is the index of the DWO in HTAB.
8720 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU. */
8721
8722 static struct dwo_unit *
8723 create_dwo_in_dwp (struct dwp_file *dwp_file,
8724 const struct dwp_hash_table *htab,
8725 uint32_t section_index,
8726 const char *comp_dir,
8727 ULONGEST signature, int is_debug_types)
8728 {
8729 struct objfile *objfile = dwarf2_per_objfile->objfile;
8730 bfd *dbfd = dwp_file->dbfd;
8731 const char *kind = is_debug_types ? "TU" : "CU";
8732 struct dwo_file *dwo_file;
8733 struct dwo_unit *dwo_unit;
8734 struct virtual_dwo_sections sections;
8735 void **dwo_file_slot;
8736 char *virtual_dwo_name;
8737 struct dwarf2_section_info *cutu;
8738 struct cleanup *cleanups;
8739 int i;
8740
8741 if (dwarf2_read_debug)
8742 {
8743 fprintf_unfiltered (gdb_stdlog, "Reading %s %u/0x%s in DWP file: %s\n",
8744 kind,
8745 section_index, phex (signature, sizeof (signature)),
8746 dwp_file->name);
8747 }
8748
8749 /* Fetch the sections of this DWO.
8750 Put a limit on the number of sections we look for so that bad data
8751 doesn't cause us to loop forever. */
8752
8753 #define MAX_NR_DWO_SECTIONS \
8754 (1 /* .debug_info or .debug_types */ \
8755 + 1 /* .debug_abbrev */ \
8756 + 1 /* .debug_line */ \
8757 + 1 /* .debug_loc */ \
8758 + 1 /* .debug_str_offsets */ \
8759 + 1 /* .debug_macro */ \
8760 + 1 /* .debug_macinfo */ \
8761 + 1 /* trailing zero */)
8762
8763 memset (&sections, 0, sizeof (sections));
8764 cleanups = make_cleanup (null_cleanup, 0);
8765
8766 for (i = 0; i < MAX_NR_DWO_SECTIONS; ++i)
8767 {
8768 asection *sectp;
8769 uint32_t section_nr =
8770 read_4_bytes (dbfd,
8771 htab->section_pool
8772 + (section_index + i) * sizeof (uint32_t));
8773
8774 if (section_nr == 0)
8775 break;
8776 if (section_nr >= dwp_file->num_sections)
8777 {
8778 error (_("Dwarf Error: bad DWP hash table, section number too large"
8779 " [in module %s]"),
8780 dwp_file->name);
8781 }
8782
8783 sectp = dwp_file->elf_sections[section_nr];
8784 if (! locate_virtual_dwo_sections (sectp, &sections))
8785 {
8786 error (_("Dwarf Error: bad DWP hash table, invalid section found"
8787 " [in module %s]"),
8788 dwp_file->name);
8789 }
8790 }
8791
8792 if (i < 2
8793 || sections.info_or_types.asection == NULL
8794 || sections.abbrev.asection == NULL)
8795 {
8796 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
8797 " [in module %s]"),
8798 dwp_file->name);
8799 }
8800 if (i == MAX_NR_DWO_SECTIONS)
8801 {
8802 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
8803 " [in module %s]"),
8804 dwp_file->name);
8805 }
8806
8807 /* It's easier for the rest of the code if we fake a struct dwo_file and
8808 have dwo_unit "live" in that. At least for now.
8809
8810 The DWP file can be made up of a random collection of CUs and TUs.
8811 However, for each CU + set of TUs that came from the same original DWO
8812 file, we want to combine them back into a virtual DWO file to save space
8813 (fewer struct dwo_file objects to allocated). Remember that for really
8814 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8815
8816 virtual_dwo_name =
8817 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
8818 sections.abbrev.asection ? sections.abbrev.asection->id : 0,
8819 sections.line.asection ? sections.line.asection->id : 0,
8820 sections.loc.asection ? sections.loc.asection->id : 0,
8821 (sections.str_offsets.asection
8822 ? sections.str_offsets.asection->id
8823 : 0));
8824 make_cleanup (xfree, virtual_dwo_name);
8825 /* Can we use an existing virtual DWO file? */
8826 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
8827 /* Create one if necessary. */
8828 if (*dwo_file_slot == NULL)
8829 {
8830 if (dwarf2_read_debug)
8831 {
8832 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
8833 virtual_dwo_name);
8834 }
8835 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8836 dwo_file->dwo_name = obstack_copy0 (&objfile->objfile_obstack,
8837 virtual_dwo_name,
8838 strlen (virtual_dwo_name));
8839 dwo_file->comp_dir = comp_dir;
8840 dwo_file->sections.abbrev = sections.abbrev;
8841 dwo_file->sections.line = sections.line;
8842 dwo_file->sections.loc = sections.loc;
8843 dwo_file->sections.macinfo = sections.macinfo;
8844 dwo_file->sections.macro = sections.macro;
8845 dwo_file->sections.str_offsets = sections.str_offsets;
8846 /* The "str" section is global to the entire DWP file. */
8847 dwo_file->sections.str = dwp_file->sections.str;
8848 /* The info or types section is assigned later to dwo_unit,
8849 there's no need to record it in dwo_file.
8850 Also, we can't simply record type sections in dwo_file because
8851 we record a pointer into the vector in dwo_unit. As we collect more
8852 types we'll grow the vector and eventually have to reallocate space
8853 for it, invalidating all the pointers into the current copy. */
8854 *dwo_file_slot = dwo_file;
8855 }
8856 else
8857 {
8858 if (dwarf2_read_debug)
8859 {
8860 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
8861 virtual_dwo_name);
8862 }
8863 dwo_file = *dwo_file_slot;
8864 }
8865 do_cleanups (cleanups);
8866
8867 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8868 dwo_unit->dwo_file = dwo_file;
8869 dwo_unit->signature = signature;
8870 dwo_unit->section = obstack_alloc (&objfile->objfile_obstack,
8871 sizeof (struct dwarf2_section_info));
8872 *dwo_unit->section = sections.info_or_types;
8873 /* offset, length, type_offset_in_tu are set later. */
8874
8875 return dwo_unit;
8876 }
8877
8878 /* Lookup the DWO with SIGNATURE in DWP_FILE. */
8879
8880 static struct dwo_unit *
8881 lookup_dwo_in_dwp (struct dwp_file *dwp_file,
8882 const struct dwp_hash_table *htab,
8883 const char *comp_dir,
8884 ULONGEST signature, int is_debug_types)
8885 {
8886 bfd *dbfd = dwp_file->dbfd;
8887 uint32_t mask = htab->nr_slots - 1;
8888 uint32_t hash = signature & mask;
8889 uint32_t hash2 = ((signature >> 32) & mask) | 1;
8890 unsigned int i;
8891 void **slot;
8892 struct dwo_unit find_dwo_cu, *dwo_cu;
8893
8894 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
8895 find_dwo_cu.signature = signature;
8896 slot = htab_find_slot (dwp_file->loaded_cutus, &find_dwo_cu, INSERT);
8897
8898 if (*slot != NULL)
8899 return *slot;
8900
8901 /* Use a for loop so that we don't loop forever on bad debug info. */
8902 for (i = 0; i < htab->nr_slots; ++i)
8903 {
8904 ULONGEST signature_in_table;
8905
8906 signature_in_table =
8907 read_8_bytes (dbfd, htab->hash_table + hash * sizeof (uint64_t));
8908 if (signature_in_table == signature)
8909 {
8910 uint32_t section_index =
8911 read_4_bytes (dbfd, htab->unit_table + hash * sizeof (uint32_t));
8912
8913 *slot = create_dwo_in_dwp (dwp_file, htab, section_index,
8914 comp_dir, signature, is_debug_types);
8915 return *slot;
8916 }
8917 if (signature_in_table == 0)
8918 return NULL;
8919 hash = (hash + hash2) & mask;
8920 }
8921
8922 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
8923 " [in module %s]"),
8924 dwp_file->name);
8925 }
8926
8927 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
8928 Open the file specified by FILE_NAME and hand it off to BFD for
8929 preliminary analysis. Return a newly initialized bfd *, which
8930 includes a canonicalized copy of FILE_NAME.
8931 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
8932 In case of trouble, return NULL.
8933 NOTE: This function is derived from symfile_bfd_open. */
8934
8935 static bfd *
8936 try_open_dwop_file (const char *file_name, int is_dwp)
8937 {
8938 bfd *sym_bfd;
8939 int desc, flags;
8940 char *absolute_name;
8941
8942 flags = OPF_TRY_CWD_FIRST;
8943 if (is_dwp)
8944 flags |= OPF_SEARCH_IN_PATH;
8945 desc = openp (debug_file_directory, flags, file_name,
8946 O_RDONLY | O_BINARY, &absolute_name);
8947 if (desc < 0)
8948 return NULL;
8949
8950 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
8951 if (!sym_bfd)
8952 {
8953 xfree (absolute_name);
8954 return NULL;
8955 }
8956 xfree (absolute_name);
8957 bfd_set_cacheable (sym_bfd, 1);
8958
8959 if (!bfd_check_format (sym_bfd, bfd_object))
8960 {
8961 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
8962 return NULL;
8963 }
8964
8965 return sym_bfd;
8966 }
8967
8968 /* Try to open DWO file FILE_NAME.
8969 COMP_DIR is the DW_AT_comp_dir attribute.
8970 The result is the bfd handle of the file.
8971 If there is a problem finding or opening the file, return NULL.
8972 Upon success, the canonicalized path of the file is stored in the bfd,
8973 same as symfile_bfd_open. */
8974
8975 static bfd *
8976 open_dwo_file (const char *file_name, const char *comp_dir)
8977 {
8978 bfd *abfd;
8979
8980 if (IS_ABSOLUTE_PATH (file_name))
8981 return try_open_dwop_file (file_name, 0 /*is_dwp*/);
8982
8983 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
8984
8985 if (comp_dir != NULL)
8986 {
8987 char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
8988
8989 /* NOTE: If comp_dir is a relative path, this will also try the
8990 search path, which seems useful. */
8991 abfd = try_open_dwop_file (path_to_try, 0 /*is_dwp*/);
8992 xfree (path_to_try);
8993 if (abfd != NULL)
8994 return abfd;
8995 }
8996
8997 /* That didn't work, try debug-file-directory, which, despite its name,
8998 is a list of paths. */
8999
9000 if (*debug_file_directory == '\0')
9001 return NULL;
9002
9003 return try_open_dwop_file (file_name, 0 /*is_dwp*/);
9004 }
9005
9006 /* This function is mapped across the sections and remembers the offset and
9007 size of each of the DWO debugging sections we are interested in. */
9008
9009 static void
9010 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
9011 {
9012 struct dwo_sections *dwo_sections = dwo_sections_ptr;
9013 const struct dwop_section_names *names = &dwop_section_names;
9014
9015 if (section_is_p (sectp->name, &names->abbrev_dwo))
9016 {
9017 dwo_sections->abbrev.asection = sectp;
9018 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
9019 }
9020 else if (section_is_p (sectp->name, &names->info_dwo))
9021 {
9022 dwo_sections->info.asection = sectp;
9023 dwo_sections->info.size = bfd_get_section_size (sectp);
9024 }
9025 else if (section_is_p (sectp->name, &names->line_dwo))
9026 {
9027 dwo_sections->line.asection = sectp;
9028 dwo_sections->line.size = bfd_get_section_size (sectp);
9029 }
9030 else if (section_is_p (sectp->name, &names->loc_dwo))
9031 {
9032 dwo_sections->loc.asection = sectp;
9033 dwo_sections->loc.size = bfd_get_section_size (sectp);
9034 }
9035 else if (section_is_p (sectp->name, &names->macinfo_dwo))
9036 {
9037 dwo_sections->macinfo.asection = sectp;
9038 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
9039 }
9040 else if (section_is_p (sectp->name, &names->macro_dwo))
9041 {
9042 dwo_sections->macro.asection = sectp;
9043 dwo_sections->macro.size = bfd_get_section_size (sectp);
9044 }
9045 else if (section_is_p (sectp->name, &names->str_dwo))
9046 {
9047 dwo_sections->str.asection = sectp;
9048 dwo_sections->str.size = bfd_get_section_size (sectp);
9049 }
9050 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
9051 {
9052 dwo_sections->str_offsets.asection = sectp;
9053 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
9054 }
9055 else if (section_is_p (sectp->name, &names->types_dwo))
9056 {
9057 struct dwarf2_section_info type_section;
9058
9059 memset (&type_section, 0, sizeof (type_section));
9060 type_section.asection = sectp;
9061 type_section.size = bfd_get_section_size (sectp);
9062 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
9063 &type_section);
9064 }
9065 }
9066
9067 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
9068 by PER_CU.
9069 The result is NULL if DWO_NAME can't be found. */
9070
9071 static struct dwo_file *
9072 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
9073 const char *dwo_name, const char *comp_dir)
9074 {
9075 struct objfile *objfile = dwarf2_per_objfile->objfile;
9076 struct dwo_file *dwo_file;
9077 bfd *dbfd;
9078 struct cleanup *cleanups;
9079
9080 dbfd = open_dwo_file (dwo_name, comp_dir);
9081 if (dbfd == NULL)
9082 {
9083 if (dwarf2_read_debug)
9084 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
9085 return NULL;
9086 }
9087 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
9088 dwo_file->dwo_name = dwo_name;
9089 dwo_file->comp_dir = comp_dir;
9090 dwo_file->dbfd = dbfd;
9091
9092 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
9093
9094 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
9095
9096 dwo_file->cus = create_dwo_debug_info_hash_table (dwo_file);
9097
9098 dwo_file->tus = create_debug_types_hash_table (dwo_file,
9099 dwo_file->sections.types);
9100
9101 discard_cleanups (cleanups);
9102
9103 if (dwarf2_read_debug)
9104 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
9105
9106 return dwo_file;
9107 }
9108
9109 /* This function is mapped across the sections and remembers the offset and
9110 size of each of the DWP debugging sections we are interested in. */
9111
9112 static void
9113 dwarf2_locate_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
9114 {
9115 struct dwp_file *dwp_file = dwp_file_ptr;
9116 const struct dwop_section_names *names = &dwop_section_names;
9117 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
9118
9119 /* Record the ELF section number for later lookup: this is what the
9120 .debug_cu_index,.debug_tu_index tables use. */
9121 gdb_assert (elf_section_nr < dwp_file->num_sections);
9122 dwp_file->elf_sections[elf_section_nr] = sectp;
9123
9124 /* Look for specific sections that we need. */
9125 if (section_is_p (sectp->name, &names->str_dwo))
9126 {
9127 dwp_file->sections.str.asection = sectp;
9128 dwp_file->sections.str.size = bfd_get_section_size (sectp);
9129 }
9130 else if (section_is_p (sectp->name, &names->cu_index))
9131 {
9132 dwp_file->sections.cu_index.asection = sectp;
9133 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
9134 }
9135 else if (section_is_p (sectp->name, &names->tu_index))
9136 {
9137 dwp_file->sections.tu_index.asection = sectp;
9138 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
9139 }
9140 }
9141
9142 /* Hash function for dwp_file loaded CUs/TUs. */
9143
9144 static hashval_t
9145 hash_dwp_loaded_cutus (const void *item)
9146 {
9147 const struct dwo_unit *dwo_unit = item;
9148
9149 /* This drops the top 32 bits of the signature, but is ok for a hash. */
9150 return dwo_unit->signature;
9151 }
9152
9153 /* Equality function for dwp_file loaded CUs/TUs. */
9154
9155 static int
9156 eq_dwp_loaded_cutus (const void *a, const void *b)
9157 {
9158 const struct dwo_unit *dua = a;
9159 const struct dwo_unit *dub = b;
9160
9161 return dua->signature == dub->signature;
9162 }
9163
9164 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
9165
9166 static htab_t
9167 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
9168 {
9169 return htab_create_alloc_ex (3,
9170 hash_dwp_loaded_cutus,
9171 eq_dwp_loaded_cutus,
9172 NULL,
9173 &objfile->objfile_obstack,
9174 hashtab_obstack_allocate,
9175 dummy_obstack_deallocate);
9176 }
9177
9178 /* Try to open DWP file FILE_NAME.
9179 The result is the bfd handle of the file.
9180 If there is a problem finding or opening the file, return NULL.
9181 Upon success, the canonicalized path of the file is stored in the bfd,
9182 same as symfile_bfd_open. */
9183
9184 static bfd *
9185 open_dwp_file (const char *file_name)
9186 {
9187 return try_open_dwop_file (file_name, 1 /*is_dwp*/);
9188 }
9189
9190 /* Initialize the use of the DWP file for the current objfile.
9191 By convention the name of the DWP file is ${objfile}.dwp.
9192 The result is NULL if it can't be found. */
9193
9194 static struct dwp_file *
9195 open_and_init_dwp_file (void)
9196 {
9197 struct objfile *objfile = dwarf2_per_objfile->objfile;
9198 struct dwp_file *dwp_file;
9199 char *dwp_name;
9200 bfd *dbfd;
9201 struct cleanup *cleanups;
9202
9203 dwp_name = xstrprintf ("%s.dwp", dwarf2_per_objfile->objfile->name);
9204 cleanups = make_cleanup (xfree, dwp_name);
9205
9206 dbfd = open_dwp_file (dwp_name);
9207 if (dbfd == NULL)
9208 {
9209 if (dwarf2_read_debug)
9210 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
9211 do_cleanups (cleanups);
9212 return NULL;
9213 }
9214 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
9215 dwp_file->name = obstack_copy0 (&objfile->objfile_obstack,
9216 dwp_name, strlen (dwp_name));
9217 dwp_file->dbfd = dbfd;
9218 do_cleanups (cleanups);
9219
9220 /* +1: section 0 is unused */
9221 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
9222 dwp_file->elf_sections =
9223 OBSTACK_CALLOC (&objfile->objfile_obstack,
9224 dwp_file->num_sections, asection *);
9225
9226 bfd_map_over_sections (dbfd, dwarf2_locate_dwp_sections, dwp_file);
9227
9228 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
9229
9230 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
9231
9232 dwp_file->loaded_cutus = allocate_dwp_loaded_cutus_table (objfile);
9233
9234 if (dwarf2_read_debug)
9235 {
9236 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
9237 fprintf_unfiltered (gdb_stdlog,
9238 " %u CUs, %u TUs\n",
9239 dwp_file->cus ? dwp_file->cus->nr_units : 0,
9240 dwp_file->tus ? dwp_file->tus->nr_units : 0);
9241 }
9242
9243 return dwp_file;
9244 }
9245
9246 /* Wrapper around open_and_init_dwp_file, only open it once. */
9247
9248 static struct dwp_file *
9249 get_dwp_file (void)
9250 {
9251 if (! dwarf2_per_objfile->dwp_checked)
9252 {
9253 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
9254 dwarf2_per_objfile->dwp_checked = 1;
9255 }
9256 return dwarf2_per_objfile->dwp_file;
9257 }
9258
9259 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9260 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9261 or in the DWP file for the objfile, referenced by THIS_UNIT.
9262 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
9263 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9264
9265 This is called, for example, when wanting to read a variable with a
9266 complex location. Therefore we don't want to do file i/o for every call.
9267 Therefore we don't want to look for a DWO file on every call.
9268 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9269 then we check if we've already seen DWO_NAME, and only THEN do we check
9270 for a DWO file.
9271
9272 The result is a pointer to the dwo_unit object or NULL if we didn't find it
9273 (dwo_id mismatch or couldn't find the DWO/DWP file). */
9274
9275 static struct dwo_unit *
9276 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
9277 const char *dwo_name, const char *comp_dir,
9278 ULONGEST signature, int is_debug_types)
9279 {
9280 struct objfile *objfile = dwarf2_per_objfile->objfile;
9281 const char *kind = is_debug_types ? "TU" : "CU";
9282 void **dwo_file_slot;
9283 struct dwo_file *dwo_file;
9284 struct dwp_file *dwp_file;
9285
9286 /* Have we already read SIGNATURE from a DWP file? */
9287
9288 dwp_file = get_dwp_file ();
9289 if (dwp_file != NULL)
9290 {
9291 const struct dwp_hash_table *dwp_htab =
9292 is_debug_types ? dwp_file->tus : dwp_file->cus;
9293
9294 if (dwp_htab != NULL)
9295 {
9296 struct dwo_unit *dwo_cutu =
9297 lookup_dwo_in_dwp (dwp_file, dwp_htab, comp_dir,
9298 signature, is_debug_types);
9299
9300 if (dwo_cutu != NULL)
9301 {
9302 if (dwarf2_read_debug)
9303 {
9304 fprintf_unfiltered (gdb_stdlog,
9305 "Virtual DWO %s %s found: @%s\n",
9306 kind, hex_string (signature),
9307 host_address_to_string (dwo_cutu));
9308 }
9309 return dwo_cutu;
9310 }
9311 }
9312 }
9313
9314 /* Have we already seen DWO_NAME? */
9315
9316 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
9317 if (*dwo_file_slot == NULL)
9318 {
9319 /* Read in the file and build a table of the DWOs it contains. */
9320 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
9321 }
9322 /* NOTE: This will be NULL if unable to open the file. */
9323 dwo_file = *dwo_file_slot;
9324
9325 if (dwo_file != NULL)
9326 {
9327 htab_t htab = is_debug_types ? dwo_file->tus : dwo_file->cus;
9328
9329 if (htab != NULL)
9330 {
9331 struct dwo_unit find_dwo_cutu, *dwo_cutu;
9332
9333 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9334 find_dwo_cutu.signature = signature;
9335 dwo_cutu = htab_find (htab, &find_dwo_cutu);
9336
9337 if (dwo_cutu != NULL)
9338 {
9339 if (dwarf2_read_debug)
9340 {
9341 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
9342 kind, dwo_name, hex_string (signature),
9343 host_address_to_string (dwo_cutu));
9344 }
9345 return dwo_cutu;
9346 }
9347 }
9348 }
9349
9350 /* We didn't find it. This could mean a dwo_id mismatch, or
9351 someone deleted the DWO/DWP file, or the search path isn't set up
9352 correctly to find the file. */
9353
9354 if (dwarf2_read_debug)
9355 {
9356 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
9357 kind, dwo_name, hex_string (signature));
9358 }
9359
9360 complaint (&symfile_complaints,
9361 _("Could not find DWO %s referenced by CU at offset 0x%x"
9362 " [in module %s]"),
9363 kind, this_unit->offset.sect_off, objfile->name);
9364 return NULL;
9365 }
9366
9367 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9368 See lookup_dwo_cutu_unit for details. */
9369
9370 static struct dwo_unit *
9371 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
9372 const char *dwo_name, const char *comp_dir,
9373 ULONGEST signature)
9374 {
9375 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
9376 }
9377
9378 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9379 See lookup_dwo_cutu_unit for details. */
9380
9381 static struct dwo_unit *
9382 lookup_dwo_type_unit (struct signatured_type *this_tu,
9383 const char *dwo_name, const char *comp_dir)
9384 {
9385 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
9386 }
9387
9388 /* Free all resources associated with DWO_FILE.
9389 Close the DWO file and munmap the sections.
9390 All memory should be on the objfile obstack. */
9391
9392 static void
9393 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
9394 {
9395 int ix;
9396 struct dwarf2_section_info *section;
9397
9398 /* Note: dbfd is NULL for virtual DWO files. */
9399 gdb_bfd_unref (dwo_file->dbfd);
9400
9401 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
9402 }
9403
9404 /* Wrapper for free_dwo_file for use in cleanups. */
9405
9406 static void
9407 free_dwo_file_cleanup (void *arg)
9408 {
9409 struct dwo_file *dwo_file = (struct dwo_file *) arg;
9410 struct objfile *objfile = dwarf2_per_objfile->objfile;
9411
9412 free_dwo_file (dwo_file, objfile);
9413 }
9414
9415 /* Traversal function for free_dwo_files. */
9416
9417 static int
9418 free_dwo_file_from_slot (void **slot, void *info)
9419 {
9420 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
9421 struct objfile *objfile = (struct objfile *) info;
9422
9423 free_dwo_file (dwo_file, objfile);
9424
9425 return 1;
9426 }
9427
9428 /* Free all resources associated with DWO_FILES. */
9429
9430 static void
9431 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
9432 {
9433 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
9434 }
9435 \f
9436 /* Read in various DIEs. */
9437
9438 /* qsort helper for inherit_abstract_dies. */
9439
9440 static int
9441 unsigned_int_compar (const void *ap, const void *bp)
9442 {
9443 unsigned int a = *(unsigned int *) ap;
9444 unsigned int b = *(unsigned int *) bp;
9445
9446 return (a > b) - (b > a);
9447 }
9448
9449 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
9450 Inherit only the children of the DW_AT_abstract_origin DIE not being
9451 already referenced by DW_AT_abstract_origin from the children of the
9452 current DIE. */
9453
9454 static void
9455 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9456 {
9457 struct die_info *child_die;
9458 unsigned die_children_count;
9459 /* CU offsets which were referenced by children of the current DIE. */
9460 sect_offset *offsets;
9461 sect_offset *offsets_end, *offsetp;
9462 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9463 struct die_info *origin_die;
9464 /* Iterator of the ORIGIN_DIE children. */
9465 struct die_info *origin_child_die;
9466 struct cleanup *cleanups;
9467 struct attribute *attr;
9468 struct dwarf2_cu *origin_cu;
9469 struct pending **origin_previous_list_in_scope;
9470
9471 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9472 if (!attr)
9473 return;
9474
9475 /* Note that following die references may follow to a die in a
9476 different cu. */
9477
9478 origin_cu = cu;
9479 origin_die = follow_die_ref (die, attr, &origin_cu);
9480
9481 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9482 symbols in. */
9483 origin_previous_list_in_scope = origin_cu->list_in_scope;
9484 origin_cu->list_in_scope = cu->list_in_scope;
9485
9486 if (die->tag != origin_die->tag
9487 && !(die->tag == DW_TAG_inlined_subroutine
9488 && origin_die->tag == DW_TAG_subprogram))
9489 complaint (&symfile_complaints,
9490 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
9491 die->offset.sect_off, origin_die->offset.sect_off);
9492
9493 child_die = die->child;
9494 die_children_count = 0;
9495 while (child_die && child_die->tag)
9496 {
9497 child_die = sibling_die (child_die);
9498 die_children_count++;
9499 }
9500 offsets = xmalloc (sizeof (*offsets) * die_children_count);
9501 cleanups = make_cleanup (xfree, offsets);
9502
9503 offsets_end = offsets;
9504 child_die = die->child;
9505 while (child_die && child_die->tag)
9506 {
9507 /* For each CHILD_DIE, find the corresponding child of
9508 ORIGIN_DIE. If there is more than one layer of
9509 DW_AT_abstract_origin, follow them all; there shouldn't be,
9510 but GCC versions at least through 4.4 generate this (GCC PR
9511 40573). */
9512 struct die_info *child_origin_die = child_die;
9513 struct dwarf2_cu *child_origin_cu = cu;
9514
9515 while (1)
9516 {
9517 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
9518 child_origin_cu);
9519 if (attr == NULL)
9520 break;
9521 child_origin_die = follow_die_ref (child_origin_die, attr,
9522 &child_origin_cu);
9523 }
9524
9525 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
9526 counterpart may exist. */
9527 if (child_origin_die != child_die)
9528 {
9529 if (child_die->tag != child_origin_die->tag
9530 && !(child_die->tag == DW_TAG_inlined_subroutine
9531 && child_origin_die->tag == DW_TAG_subprogram))
9532 complaint (&symfile_complaints,
9533 _("Child DIE 0x%x and its abstract origin 0x%x have "
9534 "different tags"), child_die->offset.sect_off,
9535 child_origin_die->offset.sect_off);
9536 if (child_origin_die->parent != origin_die)
9537 complaint (&symfile_complaints,
9538 _("Child DIE 0x%x and its abstract origin 0x%x have "
9539 "different parents"), child_die->offset.sect_off,
9540 child_origin_die->offset.sect_off);
9541 else
9542 *offsets_end++ = child_origin_die->offset;
9543 }
9544 child_die = sibling_die (child_die);
9545 }
9546 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
9547 unsigned_int_compar);
9548 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
9549 if (offsetp[-1].sect_off == offsetp->sect_off)
9550 complaint (&symfile_complaints,
9551 _("Multiple children of DIE 0x%x refer "
9552 "to DIE 0x%x as their abstract origin"),
9553 die->offset.sect_off, offsetp->sect_off);
9554
9555 offsetp = offsets;
9556 origin_child_die = origin_die->child;
9557 while (origin_child_die && origin_child_die->tag)
9558 {
9559 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
9560 while (offsetp < offsets_end
9561 && offsetp->sect_off < origin_child_die->offset.sect_off)
9562 offsetp++;
9563 if (offsetp >= offsets_end
9564 || offsetp->sect_off > origin_child_die->offset.sect_off)
9565 {
9566 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
9567 process_die (origin_child_die, origin_cu);
9568 }
9569 origin_child_die = sibling_die (origin_child_die);
9570 }
9571 origin_cu->list_in_scope = origin_previous_list_in_scope;
9572
9573 do_cleanups (cleanups);
9574 }
9575
9576 static void
9577 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
9578 {
9579 struct objfile *objfile = cu->objfile;
9580 struct context_stack *new;
9581 CORE_ADDR lowpc;
9582 CORE_ADDR highpc;
9583 struct die_info *child_die;
9584 struct attribute *attr, *call_line, *call_file;
9585 const char *name;
9586 CORE_ADDR baseaddr;
9587 struct block *block;
9588 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
9589 VEC (symbolp) *template_args = NULL;
9590 struct template_symbol *templ_func = NULL;
9591
9592 if (inlined_func)
9593 {
9594 /* If we do not have call site information, we can't show the
9595 caller of this inlined function. That's too confusing, so
9596 only use the scope for local variables. */
9597 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
9598 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
9599 if (call_line == NULL || call_file == NULL)
9600 {
9601 read_lexical_block_scope (die, cu);
9602 return;
9603 }
9604 }
9605
9606 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9607
9608 name = dwarf2_name (die, cu);
9609
9610 /* Ignore functions with missing or empty names. These are actually
9611 illegal according to the DWARF standard. */
9612 if (name == NULL)
9613 {
9614 complaint (&symfile_complaints,
9615 _("missing name for subprogram DIE at %d"),
9616 die->offset.sect_off);
9617 return;
9618 }
9619
9620 /* Ignore functions with missing or invalid low and high pc attributes. */
9621 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9622 {
9623 attr = dwarf2_attr (die, DW_AT_external, cu);
9624 if (!attr || !DW_UNSND (attr))
9625 complaint (&symfile_complaints,
9626 _("cannot get low and high bounds "
9627 "for subprogram DIE at %d"),
9628 die->offset.sect_off);
9629 return;
9630 }
9631
9632 lowpc += baseaddr;
9633 highpc += baseaddr;
9634
9635 /* If we have any template arguments, then we must allocate a
9636 different sort of symbol. */
9637 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
9638 {
9639 if (child_die->tag == DW_TAG_template_type_param
9640 || child_die->tag == DW_TAG_template_value_param)
9641 {
9642 templ_func = allocate_template_symbol (objfile);
9643 templ_func->base.is_cplus_template_function = 1;
9644 break;
9645 }
9646 }
9647
9648 new = push_context (0, lowpc);
9649 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
9650 (struct symbol *) templ_func);
9651
9652 /* If there is a location expression for DW_AT_frame_base, record
9653 it. */
9654 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
9655 if (attr)
9656 dwarf2_symbol_mark_computed (attr, new->name, cu, 1);
9657
9658 cu->list_in_scope = &local_symbols;
9659
9660 if (die->child != NULL)
9661 {
9662 child_die = die->child;
9663 while (child_die && child_die->tag)
9664 {
9665 if (child_die->tag == DW_TAG_template_type_param
9666 || child_die->tag == DW_TAG_template_value_param)
9667 {
9668 struct symbol *arg = new_symbol (child_die, NULL, cu);
9669
9670 if (arg != NULL)
9671 VEC_safe_push (symbolp, template_args, arg);
9672 }
9673 else
9674 process_die (child_die, cu);
9675 child_die = sibling_die (child_die);
9676 }
9677 }
9678
9679 inherit_abstract_dies (die, cu);
9680
9681 /* If we have a DW_AT_specification, we might need to import using
9682 directives from the context of the specification DIE. See the
9683 comment in determine_prefix. */
9684 if (cu->language == language_cplus
9685 && dwarf2_attr (die, DW_AT_specification, cu))
9686 {
9687 struct dwarf2_cu *spec_cu = cu;
9688 struct die_info *spec_die = die_specification (die, &spec_cu);
9689
9690 while (spec_die)
9691 {
9692 child_die = spec_die->child;
9693 while (child_die && child_die->tag)
9694 {
9695 if (child_die->tag == DW_TAG_imported_module)
9696 process_die (child_die, spec_cu);
9697 child_die = sibling_die (child_die);
9698 }
9699
9700 /* In some cases, GCC generates specification DIEs that
9701 themselves contain DW_AT_specification attributes. */
9702 spec_die = die_specification (spec_die, &spec_cu);
9703 }
9704 }
9705
9706 new = pop_context ();
9707 /* Make a block for the local symbols within. */
9708 block = finish_block (new->name, &local_symbols, new->old_blocks,
9709 lowpc, highpc, objfile);
9710
9711 /* For C++, set the block's scope. */
9712 if ((cu->language == language_cplus || cu->language == language_fortran)
9713 && cu->processing_has_namespace_info)
9714 block_set_scope (block, determine_prefix (die, cu),
9715 &objfile->objfile_obstack);
9716
9717 /* If we have address ranges, record them. */
9718 dwarf2_record_block_ranges (die, block, baseaddr, cu);
9719
9720 /* Attach template arguments to function. */
9721 if (! VEC_empty (symbolp, template_args))
9722 {
9723 gdb_assert (templ_func != NULL);
9724
9725 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
9726 templ_func->template_arguments
9727 = obstack_alloc (&objfile->objfile_obstack,
9728 (templ_func->n_template_arguments
9729 * sizeof (struct symbol *)));
9730 memcpy (templ_func->template_arguments,
9731 VEC_address (symbolp, template_args),
9732 (templ_func->n_template_arguments * sizeof (struct symbol *)));
9733 VEC_free (symbolp, template_args);
9734 }
9735
9736 /* In C++, we can have functions nested inside functions (e.g., when
9737 a function declares a class that has methods). This means that
9738 when we finish processing a function scope, we may need to go
9739 back to building a containing block's symbol lists. */
9740 local_symbols = new->locals;
9741 using_directives = new->using_directives;
9742
9743 /* If we've finished processing a top-level function, subsequent
9744 symbols go in the file symbol list. */
9745 if (outermost_context_p ())
9746 cu->list_in_scope = &file_symbols;
9747 }
9748
9749 /* Process all the DIES contained within a lexical block scope. Start
9750 a new scope, process the dies, and then close the scope. */
9751
9752 static void
9753 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
9754 {
9755 struct objfile *objfile = cu->objfile;
9756 struct context_stack *new;
9757 CORE_ADDR lowpc, highpc;
9758 struct die_info *child_die;
9759 CORE_ADDR baseaddr;
9760
9761 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9762
9763 /* Ignore blocks with missing or invalid low and high pc attributes. */
9764 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
9765 as multiple lexical blocks? Handling children in a sane way would
9766 be nasty. Might be easier to properly extend generic blocks to
9767 describe ranges. */
9768 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9769 return;
9770 lowpc += baseaddr;
9771 highpc += baseaddr;
9772
9773 push_context (0, lowpc);
9774 if (die->child != NULL)
9775 {
9776 child_die = die->child;
9777 while (child_die && child_die->tag)
9778 {
9779 process_die (child_die, cu);
9780 child_die = sibling_die (child_die);
9781 }
9782 }
9783 new = pop_context ();
9784
9785 if (local_symbols != NULL || using_directives != NULL)
9786 {
9787 struct block *block
9788 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
9789 highpc, objfile);
9790
9791 /* Note that recording ranges after traversing children, as we
9792 do here, means that recording a parent's ranges entails
9793 walking across all its children's ranges as they appear in
9794 the address map, which is quadratic behavior.
9795
9796 It would be nicer to record the parent's ranges before
9797 traversing its children, simply overriding whatever you find
9798 there. But since we don't even decide whether to create a
9799 block until after we've traversed its children, that's hard
9800 to do. */
9801 dwarf2_record_block_ranges (die, block, baseaddr, cu);
9802 }
9803 local_symbols = new->locals;
9804 using_directives = new->using_directives;
9805 }
9806
9807 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
9808
9809 static void
9810 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
9811 {
9812 struct objfile *objfile = cu->objfile;
9813 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9814 CORE_ADDR pc, baseaddr;
9815 struct attribute *attr;
9816 struct call_site *call_site, call_site_local;
9817 void **slot;
9818 int nparams;
9819 struct die_info *child_die;
9820
9821 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9822
9823 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9824 if (!attr)
9825 {
9826 complaint (&symfile_complaints,
9827 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
9828 "DIE 0x%x [in module %s]"),
9829 die->offset.sect_off, objfile->name);
9830 return;
9831 }
9832 pc = DW_ADDR (attr) + baseaddr;
9833
9834 if (cu->call_site_htab == NULL)
9835 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
9836 NULL, &objfile->objfile_obstack,
9837 hashtab_obstack_allocate, NULL);
9838 call_site_local.pc = pc;
9839 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
9840 if (*slot != NULL)
9841 {
9842 complaint (&symfile_complaints,
9843 _("Duplicate PC %s for DW_TAG_GNU_call_site "
9844 "DIE 0x%x [in module %s]"),
9845 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
9846 return;
9847 }
9848
9849 /* Count parameters at the caller. */
9850
9851 nparams = 0;
9852 for (child_die = die->child; child_die && child_die->tag;
9853 child_die = sibling_die (child_die))
9854 {
9855 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9856 {
9857 complaint (&symfile_complaints,
9858 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
9859 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9860 child_die->tag, child_die->offset.sect_off, objfile->name);
9861 continue;
9862 }
9863
9864 nparams++;
9865 }
9866
9867 call_site = obstack_alloc (&objfile->objfile_obstack,
9868 (sizeof (*call_site)
9869 + (sizeof (*call_site->parameter)
9870 * (nparams - 1))));
9871 *slot = call_site;
9872 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
9873 call_site->pc = pc;
9874
9875 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
9876 {
9877 struct die_info *func_die;
9878
9879 /* Skip also over DW_TAG_inlined_subroutine. */
9880 for (func_die = die->parent;
9881 func_die && func_die->tag != DW_TAG_subprogram
9882 && func_die->tag != DW_TAG_subroutine_type;
9883 func_die = func_die->parent);
9884
9885 /* DW_AT_GNU_all_call_sites is a superset
9886 of DW_AT_GNU_all_tail_call_sites. */
9887 if (func_die
9888 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
9889 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
9890 {
9891 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
9892 not complete. But keep CALL_SITE for look ups via call_site_htab,
9893 both the initial caller containing the real return address PC and
9894 the final callee containing the current PC of a chain of tail
9895 calls do not need to have the tail call list complete. But any
9896 function candidate for a virtual tail call frame searched via
9897 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
9898 determined unambiguously. */
9899 }
9900 else
9901 {
9902 struct type *func_type = NULL;
9903
9904 if (func_die)
9905 func_type = get_die_type (func_die, cu);
9906 if (func_type != NULL)
9907 {
9908 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
9909
9910 /* Enlist this call site to the function. */
9911 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
9912 TYPE_TAIL_CALL_LIST (func_type) = call_site;
9913 }
9914 else
9915 complaint (&symfile_complaints,
9916 _("Cannot find function owning DW_TAG_GNU_call_site "
9917 "DIE 0x%x [in module %s]"),
9918 die->offset.sect_off, objfile->name);
9919 }
9920 }
9921
9922 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
9923 if (attr == NULL)
9924 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9925 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9926 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
9927 /* Keep NULL DWARF_BLOCK. */;
9928 else if (attr_form_is_block (attr))
9929 {
9930 struct dwarf2_locexpr_baton *dlbaton;
9931
9932 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
9933 dlbaton->data = DW_BLOCK (attr)->data;
9934 dlbaton->size = DW_BLOCK (attr)->size;
9935 dlbaton->per_cu = cu->per_cu;
9936
9937 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
9938 }
9939 else if (is_ref_attr (attr))
9940 {
9941 struct dwarf2_cu *target_cu = cu;
9942 struct die_info *target_die;
9943
9944 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
9945 gdb_assert (target_cu->objfile == objfile);
9946 if (die_is_declaration (target_die, target_cu))
9947 {
9948 const char *target_physname = NULL;
9949 struct attribute *target_attr;
9950
9951 /* Prefer the mangled name; otherwise compute the demangled one. */
9952 target_attr = dwarf2_attr (target_die, DW_AT_linkage_name, target_cu);
9953 if (target_attr == NULL)
9954 target_attr = dwarf2_attr (target_die, DW_AT_MIPS_linkage_name,
9955 target_cu);
9956 if (target_attr != NULL && DW_STRING (target_attr) != NULL)
9957 target_physname = DW_STRING (target_attr);
9958 else
9959 target_physname = dwarf2_physname (NULL, target_die, target_cu);
9960 if (target_physname == NULL)
9961 complaint (&symfile_complaints,
9962 _("DW_AT_GNU_call_site_target target DIE has invalid "
9963 "physname, for referencing DIE 0x%x [in module %s]"),
9964 die->offset.sect_off, objfile->name);
9965 else
9966 SET_FIELD_PHYSNAME (call_site->target, target_physname);
9967 }
9968 else
9969 {
9970 CORE_ADDR lowpc;
9971
9972 /* DW_AT_entry_pc should be preferred. */
9973 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
9974 complaint (&symfile_complaints,
9975 _("DW_AT_GNU_call_site_target target DIE has invalid "
9976 "low pc, for referencing DIE 0x%x [in module %s]"),
9977 die->offset.sect_off, objfile->name);
9978 else
9979 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
9980 }
9981 }
9982 else
9983 complaint (&symfile_complaints,
9984 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
9985 "block nor reference, for DIE 0x%x [in module %s]"),
9986 die->offset.sect_off, objfile->name);
9987
9988 call_site->per_cu = cu->per_cu;
9989
9990 for (child_die = die->child;
9991 child_die && child_die->tag;
9992 child_die = sibling_die (child_die))
9993 {
9994 struct call_site_parameter *parameter;
9995 struct attribute *loc, *origin;
9996
9997 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9998 {
9999 /* Already printed the complaint above. */
10000 continue;
10001 }
10002
10003 gdb_assert (call_site->parameter_count < nparams);
10004 parameter = &call_site->parameter[call_site->parameter_count];
10005
10006 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
10007 specifies DW_TAG_formal_parameter. Value of the data assumed for the
10008 register is contained in DW_AT_GNU_call_site_value. */
10009
10010 loc = dwarf2_attr (child_die, DW_AT_location, cu);
10011 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
10012 if (loc == NULL && origin != NULL && is_ref_attr (origin))
10013 {
10014 sect_offset offset;
10015
10016 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
10017 offset = dwarf2_get_ref_die_offset (origin);
10018 if (!offset_in_cu_p (&cu->header, offset))
10019 {
10020 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
10021 binding can be done only inside one CU. Such referenced DIE
10022 therefore cannot be even moved to DW_TAG_partial_unit. */
10023 complaint (&symfile_complaints,
10024 _("DW_AT_abstract_origin offset is not in CU for "
10025 "DW_TAG_GNU_call_site child DIE 0x%x "
10026 "[in module %s]"),
10027 child_die->offset.sect_off, objfile->name);
10028 continue;
10029 }
10030 parameter->u.param_offset.cu_off = (offset.sect_off
10031 - cu->header.offset.sect_off);
10032 }
10033 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
10034 {
10035 complaint (&symfile_complaints,
10036 _("No DW_FORM_block* DW_AT_location for "
10037 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
10038 child_die->offset.sect_off, objfile->name);
10039 continue;
10040 }
10041 else
10042 {
10043 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
10044 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
10045 if (parameter->u.dwarf_reg != -1)
10046 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
10047 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
10048 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
10049 &parameter->u.fb_offset))
10050 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
10051 else
10052 {
10053 complaint (&symfile_complaints,
10054 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
10055 "for DW_FORM_block* DW_AT_location is supported for "
10056 "DW_TAG_GNU_call_site child DIE 0x%x "
10057 "[in module %s]"),
10058 child_die->offset.sect_off, objfile->name);
10059 continue;
10060 }
10061 }
10062
10063 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
10064 if (!attr_form_is_block (attr))
10065 {
10066 complaint (&symfile_complaints,
10067 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
10068 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
10069 child_die->offset.sect_off, objfile->name);
10070 continue;
10071 }
10072 parameter->value = DW_BLOCK (attr)->data;
10073 parameter->value_size = DW_BLOCK (attr)->size;
10074
10075 /* Parameters are not pre-cleared by memset above. */
10076 parameter->data_value = NULL;
10077 parameter->data_value_size = 0;
10078 call_site->parameter_count++;
10079
10080 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
10081 if (attr)
10082 {
10083 if (!attr_form_is_block (attr))
10084 complaint (&symfile_complaints,
10085 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
10086 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
10087 child_die->offset.sect_off, objfile->name);
10088 else
10089 {
10090 parameter->data_value = DW_BLOCK (attr)->data;
10091 parameter->data_value_size = DW_BLOCK (attr)->size;
10092 }
10093 }
10094 }
10095 }
10096
10097 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
10098 Return 1 if the attributes are present and valid, otherwise, return 0.
10099 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
10100
10101 static int
10102 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
10103 CORE_ADDR *high_return, struct dwarf2_cu *cu,
10104 struct partial_symtab *ranges_pst)
10105 {
10106 struct objfile *objfile = cu->objfile;
10107 struct comp_unit_head *cu_header = &cu->header;
10108 bfd *obfd = objfile->obfd;
10109 unsigned int addr_size = cu_header->addr_size;
10110 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10111 /* Base address selection entry. */
10112 CORE_ADDR base;
10113 int found_base;
10114 unsigned int dummy;
10115 gdb_byte *buffer;
10116 CORE_ADDR marker;
10117 int low_set;
10118 CORE_ADDR low = 0;
10119 CORE_ADDR high = 0;
10120 CORE_ADDR baseaddr;
10121
10122 found_base = cu->base_known;
10123 base = cu->base_address;
10124
10125 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
10126 if (offset >= dwarf2_per_objfile->ranges.size)
10127 {
10128 complaint (&symfile_complaints,
10129 _("Offset %d out of bounds for DW_AT_ranges attribute"),
10130 offset);
10131 return 0;
10132 }
10133 buffer = dwarf2_per_objfile->ranges.buffer + offset;
10134
10135 /* Read in the largest possible address. */
10136 marker = read_address (obfd, buffer, cu, &dummy);
10137 if ((marker & mask) == mask)
10138 {
10139 /* If we found the largest possible address, then
10140 read the base address. */
10141 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10142 buffer += 2 * addr_size;
10143 offset += 2 * addr_size;
10144 found_base = 1;
10145 }
10146
10147 low_set = 0;
10148
10149 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10150
10151 while (1)
10152 {
10153 CORE_ADDR range_beginning, range_end;
10154
10155 range_beginning = read_address (obfd, buffer, cu, &dummy);
10156 buffer += addr_size;
10157 range_end = read_address (obfd, buffer, cu, &dummy);
10158 buffer += addr_size;
10159 offset += 2 * addr_size;
10160
10161 /* An end of list marker is a pair of zero addresses. */
10162 if (range_beginning == 0 && range_end == 0)
10163 /* Found the end of list entry. */
10164 break;
10165
10166 /* Each base address selection entry is a pair of 2 values.
10167 The first is the largest possible address, the second is
10168 the base address. Check for a base address here. */
10169 if ((range_beginning & mask) == mask)
10170 {
10171 /* If we found the largest possible address, then
10172 read the base address. */
10173 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10174 found_base = 1;
10175 continue;
10176 }
10177
10178 if (!found_base)
10179 {
10180 /* We have no valid base address for the ranges
10181 data. */
10182 complaint (&symfile_complaints,
10183 _("Invalid .debug_ranges data (no base address)"));
10184 return 0;
10185 }
10186
10187 if (range_beginning > range_end)
10188 {
10189 /* Inverted range entries are invalid. */
10190 complaint (&symfile_complaints,
10191 _("Invalid .debug_ranges data (inverted range)"));
10192 return 0;
10193 }
10194
10195 /* Empty range entries have no effect. */
10196 if (range_beginning == range_end)
10197 continue;
10198
10199 range_beginning += base;
10200 range_end += base;
10201
10202 /* A not-uncommon case of bad debug info.
10203 Don't pollute the addrmap with bad data. */
10204 if (range_beginning + baseaddr == 0
10205 && !dwarf2_per_objfile->has_section_at_zero)
10206 {
10207 complaint (&symfile_complaints,
10208 _(".debug_ranges entry has start address of zero"
10209 " [in module %s]"), objfile->name);
10210 continue;
10211 }
10212
10213 if (ranges_pst != NULL)
10214 addrmap_set_empty (objfile->psymtabs_addrmap,
10215 range_beginning + baseaddr,
10216 range_end - 1 + baseaddr,
10217 ranges_pst);
10218
10219 /* FIXME: This is recording everything as a low-high
10220 segment of consecutive addresses. We should have a
10221 data structure for discontiguous block ranges
10222 instead. */
10223 if (! low_set)
10224 {
10225 low = range_beginning;
10226 high = range_end;
10227 low_set = 1;
10228 }
10229 else
10230 {
10231 if (range_beginning < low)
10232 low = range_beginning;
10233 if (range_end > high)
10234 high = range_end;
10235 }
10236 }
10237
10238 if (! low_set)
10239 /* If the first entry is an end-of-list marker, the range
10240 describes an empty scope, i.e. no instructions. */
10241 return 0;
10242
10243 if (low_return)
10244 *low_return = low;
10245 if (high_return)
10246 *high_return = high;
10247 return 1;
10248 }
10249
10250 /* Get low and high pc attributes from a die. Return 1 if the attributes
10251 are present and valid, otherwise, return 0. Return -1 if the range is
10252 discontinuous, i.e. derived from DW_AT_ranges information. */
10253
10254 static int
10255 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
10256 CORE_ADDR *highpc, struct dwarf2_cu *cu,
10257 struct partial_symtab *pst)
10258 {
10259 struct attribute *attr;
10260 struct attribute *attr_high;
10261 CORE_ADDR low = 0;
10262 CORE_ADDR high = 0;
10263 int ret = 0;
10264
10265 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10266 if (attr_high)
10267 {
10268 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10269 if (attr)
10270 {
10271 low = DW_ADDR (attr);
10272 if (attr_high->form == DW_FORM_addr
10273 || attr_high->form == DW_FORM_GNU_addr_index)
10274 high = DW_ADDR (attr_high);
10275 else
10276 high = low + DW_UNSND (attr_high);
10277 }
10278 else
10279 /* Found high w/o low attribute. */
10280 return 0;
10281
10282 /* Found consecutive range of addresses. */
10283 ret = 1;
10284 }
10285 else
10286 {
10287 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10288 if (attr != NULL)
10289 {
10290 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10291 We take advantage of the fact that DW_AT_ranges does not appear
10292 in DW_TAG_compile_unit of DWO files. */
10293 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10294 unsigned int ranges_offset = (DW_UNSND (attr)
10295 + (need_ranges_base
10296 ? cu->ranges_base
10297 : 0));
10298
10299 /* Value of the DW_AT_ranges attribute is the offset in the
10300 .debug_ranges section. */
10301 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
10302 return 0;
10303 /* Found discontinuous range of addresses. */
10304 ret = -1;
10305 }
10306 }
10307
10308 /* read_partial_die has also the strict LOW < HIGH requirement. */
10309 if (high <= low)
10310 return 0;
10311
10312 /* When using the GNU linker, .gnu.linkonce. sections are used to
10313 eliminate duplicate copies of functions and vtables and such.
10314 The linker will arbitrarily choose one and discard the others.
10315 The AT_*_pc values for such functions refer to local labels in
10316 these sections. If the section from that file was discarded, the
10317 labels are not in the output, so the relocs get a value of 0.
10318 If this is a discarded function, mark the pc bounds as invalid,
10319 so that GDB will ignore it. */
10320 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
10321 return 0;
10322
10323 *lowpc = low;
10324 if (highpc)
10325 *highpc = high;
10326 return ret;
10327 }
10328
10329 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
10330 its low and high PC addresses. Do nothing if these addresses could not
10331 be determined. Otherwise, set LOWPC to the low address if it is smaller,
10332 and HIGHPC to the high address if greater than HIGHPC. */
10333
10334 static void
10335 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
10336 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10337 struct dwarf2_cu *cu)
10338 {
10339 CORE_ADDR low, high;
10340 struct die_info *child = die->child;
10341
10342 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
10343 {
10344 *lowpc = min (*lowpc, low);
10345 *highpc = max (*highpc, high);
10346 }
10347
10348 /* If the language does not allow nested subprograms (either inside
10349 subprograms or lexical blocks), we're done. */
10350 if (cu->language != language_ada)
10351 return;
10352
10353 /* Check all the children of the given DIE. If it contains nested
10354 subprograms, then check their pc bounds. Likewise, we need to
10355 check lexical blocks as well, as they may also contain subprogram
10356 definitions. */
10357 while (child && child->tag)
10358 {
10359 if (child->tag == DW_TAG_subprogram
10360 || child->tag == DW_TAG_lexical_block)
10361 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
10362 child = sibling_die (child);
10363 }
10364 }
10365
10366 /* Get the low and high pc's represented by the scope DIE, and store
10367 them in *LOWPC and *HIGHPC. If the correct values can't be
10368 determined, set *LOWPC to -1 and *HIGHPC to 0. */
10369
10370 static void
10371 get_scope_pc_bounds (struct die_info *die,
10372 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10373 struct dwarf2_cu *cu)
10374 {
10375 CORE_ADDR best_low = (CORE_ADDR) -1;
10376 CORE_ADDR best_high = (CORE_ADDR) 0;
10377 CORE_ADDR current_low, current_high;
10378
10379 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
10380 {
10381 best_low = current_low;
10382 best_high = current_high;
10383 }
10384 else
10385 {
10386 struct die_info *child = die->child;
10387
10388 while (child && child->tag)
10389 {
10390 switch (child->tag) {
10391 case DW_TAG_subprogram:
10392 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
10393 break;
10394 case DW_TAG_namespace:
10395 case DW_TAG_module:
10396 /* FIXME: carlton/2004-01-16: Should we do this for
10397 DW_TAG_class_type/DW_TAG_structure_type, too? I think
10398 that current GCC's always emit the DIEs corresponding
10399 to definitions of methods of classes as children of a
10400 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
10401 the DIEs giving the declarations, which could be
10402 anywhere). But I don't see any reason why the
10403 standards says that they have to be there. */
10404 get_scope_pc_bounds (child, &current_low, &current_high, cu);
10405
10406 if (current_low != ((CORE_ADDR) -1))
10407 {
10408 best_low = min (best_low, current_low);
10409 best_high = max (best_high, current_high);
10410 }
10411 break;
10412 default:
10413 /* Ignore. */
10414 break;
10415 }
10416
10417 child = sibling_die (child);
10418 }
10419 }
10420
10421 *lowpc = best_low;
10422 *highpc = best_high;
10423 }
10424
10425 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
10426 in DIE. */
10427
10428 static void
10429 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
10430 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
10431 {
10432 struct objfile *objfile = cu->objfile;
10433 struct attribute *attr;
10434 struct attribute *attr_high;
10435
10436 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10437 if (attr_high)
10438 {
10439 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10440 if (attr)
10441 {
10442 CORE_ADDR low = DW_ADDR (attr);
10443 CORE_ADDR high;
10444 if (attr_high->form == DW_FORM_addr
10445 || attr_high->form == DW_FORM_GNU_addr_index)
10446 high = DW_ADDR (attr_high);
10447 else
10448 high = low + DW_UNSND (attr_high);
10449
10450 record_block_range (block, baseaddr + low, baseaddr + high - 1);
10451 }
10452 }
10453
10454 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10455 if (attr)
10456 {
10457 bfd *obfd = objfile->obfd;
10458 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10459 We take advantage of the fact that DW_AT_ranges does not appear
10460 in DW_TAG_compile_unit of DWO files. */
10461 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10462
10463 /* The value of the DW_AT_ranges attribute is the offset of the
10464 address range list in the .debug_ranges section. */
10465 unsigned long offset = (DW_UNSND (attr)
10466 + (need_ranges_base ? cu->ranges_base : 0));
10467 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
10468
10469 /* For some target architectures, but not others, the
10470 read_address function sign-extends the addresses it returns.
10471 To recognize base address selection entries, we need a
10472 mask. */
10473 unsigned int addr_size = cu->header.addr_size;
10474 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10475
10476 /* The base address, to which the next pair is relative. Note
10477 that this 'base' is a DWARF concept: most entries in a range
10478 list are relative, to reduce the number of relocs against the
10479 debugging information. This is separate from this function's
10480 'baseaddr' argument, which GDB uses to relocate debugging
10481 information from a shared library based on the address at
10482 which the library was loaded. */
10483 CORE_ADDR base = cu->base_address;
10484 int base_known = cu->base_known;
10485
10486 gdb_assert (dwarf2_per_objfile->ranges.readin);
10487 if (offset >= dwarf2_per_objfile->ranges.size)
10488 {
10489 complaint (&symfile_complaints,
10490 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
10491 offset);
10492 return;
10493 }
10494
10495 for (;;)
10496 {
10497 unsigned int bytes_read;
10498 CORE_ADDR start, end;
10499
10500 start = read_address (obfd, buffer, cu, &bytes_read);
10501 buffer += bytes_read;
10502 end = read_address (obfd, buffer, cu, &bytes_read);
10503 buffer += bytes_read;
10504
10505 /* Did we find the end of the range list? */
10506 if (start == 0 && end == 0)
10507 break;
10508
10509 /* Did we find a base address selection entry? */
10510 else if ((start & base_select_mask) == base_select_mask)
10511 {
10512 base = end;
10513 base_known = 1;
10514 }
10515
10516 /* We found an ordinary address range. */
10517 else
10518 {
10519 if (!base_known)
10520 {
10521 complaint (&symfile_complaints,
10522 _("Invalid .debug_ranges data "
10523 "(no base address)"));
10524 return;
10525 }
10526
10527 if (start > end)
10528 {
10529 /* Inverted range entries are invalid. */
10530 complaint (&symfile_complaints,
10531 _("Invalid .debug_ranges data "
10532 "(inverted range)"));
10533 return;
10534 }
10535
10536 /* Empty range entries have no effect. */
10537 if (start == end)
10538 continue;
10539
10540 start += base + baseaddr;
10541 end += base + baseaddr;
10542
10543 /* A not-uncommon case of bad debug info.
10544 Don't pollute the addrmap with bad data. */
10545 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
10546 {
10547 complaint (&symfile_complaints,
10548 _(".debug_ranges entry has start address of zero"
10549 " [in module %s]"), objfile->name);
10550 continue;
10551 }
10552
10553 record_block_range (block, start, end - 1);
10554 }
10555 }
10556 }
10557 }
10558
10559 /* Check whether the producer field indicates either of GCC < 4.6, or the
10560 Intel C/C++ compiler, and cache the result in CU. */
10561
10562 static void
10563 check_producer (struct dwarf2_cu *cu)
10564 {
10565 const char *cs;
10566 int major, minor, release;
10567
10568 if (cu->producer == NULL)
10569 {
10570 /* For unknown compilers expect their behavior is DWARF version
10571 compliant.
10572
10573 GCC started to support .debug_types sections by -gdwarf-4 since
10574 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
10575 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
10576 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
10577 interpreted incorrectly by GDB now - GCC PR debug/48229. */
10578 }
10579 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
10580 {
10581 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
10582
10583 cs = &cu->producer[strlen ("GNU ")];
10584 while (*cs && !isdigit (*cs))
10585 cs++;
10586 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
10587 {
10588 /* Not recognized as GCC. */
10589 }
10590 else
10591 {
10592 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
10593 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
10594 }
10595 }
10596 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
10597 cu->producer_is_icc = 1;
10598 else
10599 {
10600 /* For other non-GCC compilers, expect their behavior is DWARF version
10601 compliant. */
10602 }
10603
10604 cu->checked_producer = 1;
10605 }
10606
10607 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
10608 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
10609 during 4.6.0 experimental. */
10610
10611 static int
10612 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
10613 {
10614 if (!cu->checked_producer)
10615 check_producer (cu);
10616
10617 return cu->producer_is_gxx_lt_4_6;
10618 }
10619
10620 /* Return the default accessibility type if it is not overriden by
10621 DW_AT_accessibility. */
10622
10623 static enum dwarf_access_attribute
10624 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
10625 {
10626 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
10627 {
10628 /* The default DWARF 2 accessibility for members is public, the default
10629 accessibility for inheritance is private. */
10630
10631 if (die->tag != DW_TAG_inheritance)
10632 return DW_ACCESS_public;
10633 else
10634 return DW_ACCESS_private;
10635 }
10636 else
10637 {
10638 /* DWARF 3+ defines the default accessibility a different way. The same
10639 rules apply now for DW_TAG_inheritance as for the members and it only
10640 depends on the container kind. */
10641
10642 if (die->parent->tag == DW_TAG_class_type)
10643 return DW_ACCESS_private;
10644 else
10645 return DW_ACCESS_public;
10646 }
10647 }
10648
10649 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
10650 offset. If the attribute was not found return 0, otherwise return
10651 1. If it was found but could not properly be handled, set *OFFSET
10652 to 0. */
10653
10654 static int
10655 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
10656 LONGEST *offset)
10657 {
10658 struct attribute *attr;
10659
10660 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
10661 if (attr != NULL)
10662 {
10663 *offset = 0;
10664
10665 /* Note that we do not check for a section offset first here.
10666 This is because DW_AT_data_member_location is new in DWARF 4,
10667 so if we see it, we can assume that a constant form is really
10668 a constant and not a section offset. */
10669 if (attr_form_is_constant (attr))
10670 *offset = dwarf2_get_attr_constant_value (attr, 0);
10671 else if (attr_form_is_section_offset (attr))
10672 dwarf2_complex_location_expr_complaint ();
10673 else if (attr_form_is_block (attr))
10674 *offset = decode_locdesc (DW_BLOCK (attr), cu);
10675 else
10676 dwarf2_complex_location_expr_complaint ();
10677
10678 return 1;
10679 }
10680
10681 return 0;
10682 }
10683
10684 /* Add an aggregate field to the field list. */
10685
10686 static void
10687 dwarf2_add_field (struct field_info *fip, struct die_info *die,
10688 struct dwarf2_cu *cu)
10689 {
10690 struct objfile *objfile = cu->objfile;
10691 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10692 struct nextfield *new_field;
10693 struct attribute *attr;
10694 struct field *fp;
10695 const char *fieldname = "";
10696
10697 /* Allocate a new field list entry and link it in. */
10698 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
10699 make_cleanup (xfree, new_field);
10700 memset (new_field, 0, sizeof (struct nextfield));
10701
10702 if (die->tag == DW_TAG_inheritance)
10703 {
10704 new_field->next = fip->baseclasses;
10705 fip->baseclasses = new_field;
10706 }
10707 else
10708 {
10709 new_field->next = fip->fields;
10710 fip->fields = new_field;
10711 }
10712 fip->nfields++;
10713
10714 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
10715 if (attr)
10716 new_field->accessibility = DW_UNSND (attr);
10717 else
10718 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
10719 if (new_field->accessibility != DW_ACCESS_public)
10720 fip->non_public_fields = 1;
10721
10722 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
10723 if (attr)
10724 new_field->virtuality = DW_UNSND (attr);
10725 else
10726 new_field->virtuality = DW_VIRTUALITY_none;
10727
10728 fp = &new_field->field;
10729
10730 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
10731 {
10732 LONGEST offset;
10733
10734 /* Data member other than a C++ static data member. */
10735
10736 /* Get type of field. */
10737 fp->type = die_type (die, cu);
10738
10739 SET_FIELD_BITPOS (*fp, 0);
10740
10741 /* Get bit size of field (zero if none). */
10742 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
10743 if (attr)
10744 {
10745 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
10746 }
10747 else
10748 {
10749 FIELD_BITSIZE (*fp) = 0;
10750 }
10751
10752 /* Get bit offset of field. */
10753 if (handle_data_member_location (die, cu, &offset))
10754 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
10755 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
10756 if (attr)
10757 {
10758 if (gdbarch_bits_big_endian (gdbarch))
10759 {
10760 /* For big endian bits, the DW_AT_bit_offset gives the
10761 additional bit offset from the MSB of the containing
10762 anonymous object to the MSB of the field. We don't
10763 have to do anything special since we don't need to
10764 know the size of the anonymous object. */
10765 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
10766 }
10767 else
10768 {
10769 /* For little endian bits, compute the bit offset to the
10770 MSB of the anonymous object, subtract off the number of
10771 bits from the MSB of the field to the MSB of the
10772 object, and then subtract off the number of bits of
10773 the field itself. The result is the bit offset of
10774 the LSB of the field. */
10775 int anonymous_size;
10776 int bit_offset = DW_UNSND (attr);
10777
10778 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10779 if (attr)
10780 {
10781 /* The size of the anonymous object containing
10782 the bit field is explicit, so use the
10783 indicated size (in bytes). */
10784 anonymous_size = DW_UNSND (attr);
10785 }
10786 else
10787 {
10788 /* The size of the anonymous object containing
10789 the bit field must be inferred from the type
10790 attribute of the data member containing the
10791 bit field. */
10792 anonymous_size = TYPE_LENGTH (fp->type);
10793 }
10794 SET_FIELD_BITPOS (*fp,
10795 (FIELD_BITPOS (*fp)
10796 + anonymous_size * bits_per_byte
10797 - bit_offset - FIELD_BITSIZE (*fp)));
10798 }
10799 }
10800
10801 /* Get name of field. */
10802 fieldname = dwarf2_name (die, cu);
10803 if (fieldname == NULL)
10804 fieldname = "";
10805
10806 /* The name is already allocated along with this objfile, so we don't
10807 need to duplicate it for the type. */
10808 fp->name = fieldname;
10809
10810 /* Change accessibility for artificial fields (e.g. virtual table
10811 pointer or virtual base class pointer) to private. */
10812 if (dwarf2_attr (die, DW_AT_artificial, cu))
10813 {
10814 FIELD_ARTIFICIAL (*fp) = 1;
10815 new_field->accessibility = DW_ACCESS_private;
10816 fip->non_public_fields = 1;
10817 }
10818 }
10819 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
10820 {
10821 /* C++ static member. */
10822
10823 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
10824 is a declaration, but all versions of G++ as of this writing
10825 (so through at least 3.2.1) incorrectly generate
10826 DW_TAG_variable tags. */
10827
10828 const char *physname;
10829
10830 /* Get name of field. */
10831 fieldname = dwarf2_name (die, cu);
10832 if (fieldname == NULL)
10833 return;
10834
10835 attr = dwarf2_attr (die, DW_AT_const_value, cu);
10836 if (attr
10837 /* Only create a symbol if this is an external value.
10838 new_symbol checks this and puts the value in the global symbol
10839 table, which we want. If it is not external, new_symbol
10840 will try to put the value in cu->list_in_scope which is wrong. */
10841 && dwarf2_flag_true_p (die, DW_AT_external, cu))
10842 {
10843 /* A static const member, not much different than an enum as far as
10844 we're concerned, except that we can support more types. */
10845 new_symbol (die, NULL, cu);
10846 }
10847
10848 /* Get physical name. */
10849 physname = dwarf2_physname (fieldname, die, cu);
10850
10851 /* The name is already allocated along with this objfile, so we don't
10852 need to duplicate it for the type. */
10853 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
10854 FIELD_TYPE (*fp) = die_type (die, cu);
10855 FIELD_NAME (*fp) = fieldname;
10856 }
10857 else if (die->tag == DW_TAG_inheritance)
10858 {
10859 LONGEST offset;
10860
10861 /* C++ base class field. */
10862 if (handle_data_member_location (die, cu, &offset))
10863 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
10864 FIELD_BITSIZE (*fp) = 0;
10865 FIELD_TYPE (*fp) = die_type (die, cu);
10866 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
10867 fip->nbaseclasses++;
10868 }
10869 }
10870
10871 /* Add a typedef defined in the scope of the FIP's class. */
10872
10873 static void
10874 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
10875 struct dwarf2_cu *cu)
10876 {
10877 struct objfile *objfile = cu->objfile;
10878 struct typedef_field_list *new_field;
10879 struct attribute *attr;
10880 struct typedef_field *fp;
10881 char *fieldname = "";
10882
10883 /* Allocate a new field list entry and link it in. */
10884 new_field = xzalloc (sizeof (*new_field));
10885 make_cleanup (xfree, new_field);
10886
10887 gdb_assert (die->tag == DW_TAG_typedef);
10888
10889 fp = &new_field->field;
10890
10891 /* Get name of field. */
10892 fp->name = dwarf2_name (die, cu);
10893 if (fp->name == NULL)
10894 return;
10895
10896 fp->type = read_type_die (die, cu);
10897
10898 new_field->next = fip->typedef_field_list;
10899 fip->typedef_field_list = new_field;
10900 fip->typedef_field_list_count++;
10901 }
10902
10903 /* Create the vector of fields, and attach it to the type. */
10904
10905 static void
10906 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
10907 struct dwarf2_cu *cu)
10908 {
10909 int nfields = fip->nfields;
10910
10911 /* Record the field count, allocate space for the array of fields,
10912 and create blank accessibility bitfields if necessary. */
10913 TYPE_NFIELDS (type) = nfields;
10914 TYPE_FIELDS (type) = (struct field *)
10915 TYPE_ALLOC (type, sizeof (struct field) * nfields);
10916 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
10917
10918 if (fip->non_public_fields && cu->language != language_ada)
10919 {
10920 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10921
10922 TYPE_FIELD_PRIVATE_BITS (type) =
10923 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10924 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
10925
10926 TYPE_FIELD_PROTECTED_BITS (type) =
10927 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10928 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
10929
10930 TYPE_FIELD_IGNORE_BITS (type) =
10931 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10932 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
10933 }
10934
10935 /* If the type has baseclasses, allocate and clear a bit vector for
10936 TYPE_FIELD_VIRTUAL_BITS. */
10937 if (fip->nbaseclasses && cu->language != language_ada)
10938 {
10939 int num_bytes = B_BYTES (fip->nbaseclasses);
10940 unsigned char *pointer;
10941
10942 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10943 pointer = TYPE_ALLOC (type, num_bytes);
10944 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
10945 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
10946 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
10947 }
10948
10949 /* Copy the saved-up fields into the field vector. Start from the head of
10950 the list, adding to the tail of the field array, so that they end up in
10951 the same order in the array in which they were added to the list. */
10952 while (nfields-- > 0)
10953 {
10954 struct nextfield *fieldp;
10955
10956 if (fip->fields)
10957 {
10958 fieldp = fip->fields;
10959 fip->fields = fieldp->next;
10960 }
10961 else
10962 {
10963 fieldp = fip->baseclasses;
10964 fip->baseclasses = fieldp->next;
10965 }
10966
10967 TYPE_FIELD (type, nfields) = fieldp->field;
10968 switch (fieldp->accessibility)
10969 {
10970 case DW_ACCESS_private:
10971 if (cu->language != language_ada)
10972 SET_TYPE_FIELD_PRIVATE (type, nfields);
10973 break;
10974
10975 case DW_ACCESS_protected:
10976 if (cu->language != language_ada)
10977 SET_TYPE_FIELD_PROTECTED (type, nfields);
10978 break;
10979
10980 case DW_ACCESS_public:
10981 break;
10982
10983 default:
10984 /* Unknown accessibility. Complain and treat it as public. */
10985 {
10986 complaint (&symfile_complaints, _("unsupported accessibility %d"),
10987 fieldp->accessibility);
10988 }
10989 break;
10990 }
10991 if (nfields < fip->nbaseclasses)
10992 {
10993 switch (fieldp->virtuality)
10994 {
10995 case DW_VIRTUALITY_virtual:
10996 case DW_VIRTUALITY_pure_virtual:
10997 if (cu->language == language_ada)
10998 error (_("unexpected virtuality in component of Ada type"));
10999 SET_TYPE_FIELD_VIRTUAL (type, nfields);
11000 break;
11001 }
11002 }
11003 }
11004 }
11005
11006 /* Return true if this member function is a constructor, false
11007 otherwise. */
11008
11009 static int
11010 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
11011 {
11012 const char *fieldname;
11013 const char *typename;
11014 int len;
11015
11016 if (die->parent == NULL)
11017 return 0;
11018
11019 if (die->parent->tag != DW_TAG_structure_type
11020 && die->parent->tag != DW_TAG_union_type
11021 && die->parent->tag != DW_TAG_class_type)
11022 return 0;
11023
11024 fieldname = dwarf2_name (die, cu);
11025 typename = dwarf2_name (die->parent, cu);
11026 if (fieldname == NULL || typename == NULL)
11027 return 0;
11028
11029 len = strlen (fieldname);
11030 return (strncmp (fieldname, typename, len) == 0
11031 && (typename[len] == '\0' || typename[len] == '<'));
11032 }
11033
11034 /* Add a member function to the proper fieldlist. */
11035
11036 static void
11037 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
11038 struct type *type, struct dwarf2_cu *cu)
11039 {
11040 struct objfile *objfile = cu->objfile;
11041 struct attribute *attr;
11042 struct fnfieldlist *flp;
11043 int i;
11044 struct fn_field *fnp;
11045 const char *fieldname;
11046 struct nextfnfield *new_fnfield;
11047 struct type *this_type;
11048 enum dwarf_access_attribute accessibility;
11049
11050 if (cu->language == language_ada)
11051 error (_("unexpected member function in Ada type"));
11052
11053 /* Get name of member function. */
11054 fieldname = dwarf2_name (die, cu);
11055 if (fieldname == NULL)
11056 return;
11057
11058 /* Look up member function name in fieldlist. */
11059 for (i = 0; i < fip->nfnfields; i++)
11060 {
11061 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
11062 break;
11063 }
11064
11065 /* Create new list element if necessary. */
11066 if (i < fip->nfnfields)
11067 flp = &fip->fnfieldlists[i];
11068 else
11069 {
11070 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
11071 {
11072 fip->fnfieldlists = (struct fnfieldlist *)
11073 xrealloc (fip->fnfieldlists,
11074 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
11075 * sizeof (struct fnfieldlist));
11076 if (fip->nfnfields == 0)
11077 make_cleanup (free_current_contents, &fip->fnfieldlists);
11078 }
11079 flp = &fip->fnfieldlists[fip->nfnfields];
11080 flp->name = fieldname;
11081 flp->length = 0;
11082 flp->head = NULL;
11083 i = fip->nfnfields++;
11084 }
11085
11086 /* Create a new member function field and chain it to the field list
11087 entry. */
11088 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
11089 make_cleanup (xfree, new_fnfield);
11090 memset (new_fnfield, 0, sizeof (struct nextfnfield));
11091 new_fnfield->next = flp->head;
11092 flp->head = new_fnfield;
11093 flp->length++;
11094
11095 /* Fill in the member function field info. */
11096 fnp = &new_fnfield->fnfield;
11097
11098 /* Delay processing of the physname until later. */
11099 if (cu->language == language_cplus || cu->language == language_java)
11100 {
11101 add_to_method_list (type, i, flp->length - 1, fieldname,
11102 die, cu);
11103 }
11104 else
11105 {
11106 const char *physname = dwarf2_physname (fieldname, die, cu);
11107 fnp->physname = physname ? physname : "";
11108 }
11109
11110 fnp->type = alloc_type (objfile);
11111 this_type = read_type_die (die, cu);
11112 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
11113 {
11114 int nparams = TYPE_NFIELDS (this_type);
11115
11116 /* TYPE is the domain of this method, and THIS_TYPE is the type
11117 of the method itself (TYPE_CODE_METHOD). */
11118 smash_to_method_type (fnp->type, type,
11119 TYPE_TARGET_TYPE (this_type),
11120 TYPE_FIELDS (this_type),
11121 TYPE_NFIELDS (this_type),
11122 TYPE_VARARGS (this_type));
11123
11124 /* Handle static member functions.
11125 Dwarf2 has no clean way to discern C++ static and non-static
11126 member functions. G++ helps GDB by marking the first
11127 parameter for non-static member functions (which is the this
11128 pointer) as artificial. We obtain this information from
11129 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
11130 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
11131 fnp->voffset = VOFFSET_STATIC;
11132 }
11133 else
11134 complaint (&symfile_complaints, _("member function type missing for '%s'"),
11135 dwarf2_full_name (fieldname, die, cu));
11136
11137 /* Get fcontext from DW_AT_containing_type if present. */
11138 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
11139 fnp->fcontext = die_containing_type (die, cu);
11140
11141 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
11142 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
11143
11144 /* Get accessibility. */
11145 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
11146 if (attr)
11147 accessibility = DW_UNSND (attr);
11148 else
11149 accessibility = dwarf2_default_access_attribute (die, cu);
11150 switch (accessibility)
11151 {
11152 case DW_ACCESS_private:
11153 fnp->is_private = 1;
11154 break;
11155 case DW_ACCESS_protected:
11156 fnp->is_protected = 1;
11157 break;
11158 }
11159
11160 /* Check for artificial methods. */
11161 attr = dwarf2_attr (die, DW_AT_artificial, cu);
11162 if (attr && DW_UNSND (attr) != 0)
11163 fnp->is_artificial = 1;
11164
11165 fnp->is_constructor = dwarf2_is_constructor (die, cu);
11166
11167 /* Get index in virtual function table if it is a virtual member
11168 function. For older versions of GCC, this is an offset in the
11169 appropriate virtual table, as specified by DW_AT_containing_type.
11170 For everyone else, it is an expression to be evaluated relative
11171 to the object address. */
11172
11173 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
11174 if (attr)
11175 {
11176 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
11177 {
11178 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
11179 {
11180 /* Old-style GCC. */
11181 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
11182 }
11183 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
11184 || (DW_BLOCK (attr)->size > 1
11185 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
11186 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
11187 {
11188 struct dwarf_block blk;
11189 int offset;
11190
11191 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
11192 ? 1 : 2);
11193 blk.size = DW_BLOCK (attr)->size - offset;
11194 blk.data = DW_BLOCK (attr)->data + offset;
11195 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
11196 if ((fnp->voffset % cu->header.addr_size) != 0)
11197 dwarf2_complex_location_expr_complaint ();
11198 else
11199 fnp->voffset /= cu->header.addr_size;
11200 fnp->voffset += 2;
11201 }
11202 else
11203 dwarf2_complex_location_expr_complaint ();
11204
11205 if (!fnp->fcontext)
11206 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
11207 }
11208 else if (attr_form_is_section_offset (attr))
11209 {
11210 dwarf2_complex_location_expr_complaint ();
11211 }
11212 else
11213 {
11214 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
11215 fieldname);
11216 }
11217 }
11218 else
11219 {
11220 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11221 if (attr && DW_UNSND (attr))
11222 {
11223 /* GCC does this, as of 2008-08-25; PR debug/37237. */
11224 complaint (&symfile_complaints,
11225 _("Member function \"%s\" (offset %d) is virtual "
11226 "but the vtable offset is not specified"),
11227 fieldname, die->offset.sect_off);
11228 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11229 TYPE_CPLUS_DYNAMIC (type) = 1;
11230 }
11231 }
11232 }
11233
11234 /* Create the vector of member function fields, and attach it to the type. */
11235
11236 static void
11237 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
11238 struct dwarf2_cu *cu)
11239 {
11240 struct fnfieldlist *flp;
11241 int i;
11242
11243 if (cu->language == language_ada)
11244 error (_("unexpected member functions in Ada type"));
11245
11246 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11247 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
11248 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
11249
11250 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
11251 {
11252 struct nextfnfield *nfp = flp->head;
11253 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
11254 int k;
11255
11256 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
11257 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
11258 fn_flp->fn_fields = (struct fn_field *)
11259 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
11260 for (k = flp->length; (k--, nfp); nfp = nfp->next)
11261 fn_flp->fn_fields[k] = nfp->fnfield;
11262 }
11263
11264 TYPE_NFN_FIELDS (type) = fip->nfnfields;
11265 }
11266
11267 /* Returns non-zero if NAME is the name of a vtable member in CU's
11268 language, zero otherwise. */
11269 static int
11270 is_vtable_name (const char *name, struct dwarf2_cu *cu)
11271 {
11272 static const char vptr[] = "_vptr";
11273 static const char vtable[] = "vtable";
11274
11275 /* Look for the C++ and Java forms of the vtable. */
11276 if ((cu->language == language_java
11277 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
11278 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
11279 && is_cplus_marker (name[sizeof (vptr) - 1])))
11280 return 1;
11281
11282 return 0;
11283 }
11284
11285 /* GCC outputs unnamed structures that are really pointers to member
11286 functions, with the ABI-specified layout. If TYPE describes
11287 such a structure, smash it into a member function type.
11288
11289 GCC shouldn't do this; it should just output pointer to member DIEs.
11290 This is GCC PR debug/28767. */
11291
11292 static void
11293 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
11294 {
11295 struct type *pfn_type, *domain_type, *new_type;
11296
11297 /* Check for a structure with no name and two children. */
11298 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
11299 return;
11300
11301 /* Check for __pfn and __delta members. */
11302 if (TYPE_FIELD_NAME (type, 0) == NULL
11303 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
11304 || TYPE_FIELD_NAME (type, 1) == NULL
11305 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
11306 return;
11307
11308 /* Find the type of the method. */
11309 pfn_type = TYPE_FIELD_TYPE (type, 0);
11310 if (pfn_type == NULL
11311 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
11312 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
11313 return;
11314
11315 /* Look for the "this" argument. */
11316 pfn_type = TYPE_TARGET_TYPE (pfn_type);
11317 if (TYPE_NFIELDS (pfn_type) == 0
11318 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
11319 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
11320 return;
11321
11322 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
11323 new_type = alloc_type (objfile);
11324 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
11325 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
11326 TYPE_VARARGS (pfn_type));
11327 smash_to_methodptr_type (type, new_type);
11328 }
11329
11330 /* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
11331 (icc). */
11332
11333 static int
11334 producer_is_icc (struct dwarf2_cu *cu)
11335 {
11336 if (!cu->checked_producer)
11337 check_producer (cu);
11338
11339 return cu->producer_is_icc;
11340 }
11341
11342 /* Called when we find the DIE that starts a structure or union scope
11343 (definition) to create a type for the structure or union. Fill in
11344 the type's name and general properties; the members will not be
11345 processed until process_structure_scope.
11346
11347 NOTE: we need to call these functions regardless of whether or not the
11348 DIE has a DW_AT_name attribute, since it might be an anonymous
11349 structure or union. This gets the type entered into our set of
11350 user defined types.
11351
11352 However, if the structure is incomplete (an opaque struct/union)
11353 then suppress creating a symbol table entry for it since gdb only
11354 wants to find the one with the complete definition. Note that if
11355 it is complete, we just call new_symbol, which does it's own
11356 checking about whether the struct/union is anonymous or not (and
11357 suppresses creating a symbol table entry itself). */
11358
11359 static struct type *
11360 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
11361 {
11362 struct objfile *objfile = cu->objfile;
11363 struct type *type;
11364 struct attribute *attr;
11365 const char *name;
11366
11367 /* If the definition of this type lives in .debug_types, read that type.
11368 Don't follow DW_AT_specification though, that will take us back up
11369 the chain and we want to go down. */
11370 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
11371 if (attr)
11372 {
11373 struct dwarf2_cu *type_cu = cu;
11374 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11375
11376 /* We could just recurse on read_structure_type, but we need to call
11377 get_die_type to ensure only one type for this DIE is created.
11378 This is important, for example, because for c++ classes we need
11379 TYPE_NAME set which is only done by new_symbol. Blech. */
11380 type = read_type_die (type_die, type_cu);
11381
11382 /* TYPE_CU may not be the same as CU.
11383 Ensure TYPE is recorded with CU in die_type_hash. */
11384 return set_die_type (die, type, cu);
11385 }
11386
11387 type = alloc_type (objfile);
11388 INIT_CPLUS_SPECIFIC (type);
11389
11390 name = dwarf2_name (die, cu);
11391 if (name != NULL)
11392 {
11393 if (cu->language == language_cplus
11394 || cu->language == language_java)
11395 {
11396 const char *full_name = dwarf2_full_name (name, die, cu);
11397
11398 /* dwarf2_full_name might have already finished building the DIE's
11399 type. If so, there is no need to continue. */
11400 if (get_die_type (die, cu) != NULL)
11401 return get_die_type (die, cu);
11402
11403 TYPE_TAG_NAME (type) = full_name;
11404 if (die->tag == DW_TAG_structure_type
11405 || die->tag == DW_TAG_class_type)
11406 TYPE_NAME (type) = TYPE_TAG_NAME (type);
11407 }
11408 else
11409 {
11410 /* The name is already allocated along with this objfile, so
11411 we don't need to duplicate it for the type. */
11412 TYPE_TAG_NAME (type) = name;
11413 if (die->tag == DW_TAG_class_type)
11414 TYPE_NAME (type) = TYPE_TAG_NAME (type);
11415 }
11416 }
11417
11418 if (die->tag == DW_TAG_structure_type)
11419 {
11420 TYPE_CODE (type) = TYPE_CODE_STRUCT;
11421 }
11422 else if (die->tag == DW_TAG_union_type)
11423 {
11424 TYPE_CODE (type) = TYPE_CODE_UNION;
11425 }
11426 else
11427 {
11428 TYPE_CODE (type) = TYPE_CODE_CLASS;
11429 }
11430
11431 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
11432 TYPE_DECLARED_CLASS (type) = 1;
11433
11434 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11435 if (attr)
11436 {
11437 TYPE_LENGTH (type) = DW_UNSND (attr);
11438 }
11439 else
11440 {
11441 TYPE_LENGTH (type) = 0;
11442 }
11443
11444 if (producer_is_icc (cu))
11445 {
11446 /* ICC does not output the required DW_AT_declaration
11447 on incomplete types, but gives them a size of zero. */
11448 }
11449 else
11450 TYPE_STUB_SUPPORTED (type) = 1;
11451
11452 if (die_is_declaration (die, cu))
11453 TYPE_STUB (type) = 1;
11454 else if (attr == NULL && die->child == NULL
11455 && producer_is_realview (cu->producer))
11456 /* RealView does not output the required DW_AT_declaration
11457 on incomplete types. */
11458 TYPE_STUB (type) = 1;
11459
11460 /* We need to add the type field to the die immediately so we don't
11461 infinitely recurse when dealing with pointers to the structure
11462 type within the structure itself. */
11463 set_die_type (die, type, cu);
11464
11465 /* set_die_type should be already done. */
11466 set_descriptive_type (type, die, cu);
11467
11468 return type;
11469 }
11470
11471 /* Finish creating a structure or union type, including filling in
11472 its members and creating a symbol for it. */
11473
11474 static void
11475 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
11476 {
11477 struct objfile *objfile = cu->objfile;
11478 struct die_info *child_die = die->child;
11479 struct type *type;
11480
11481 type = get_die_type (die, cu);
11482 if (type == NULL)
11483 type = read_structure_type (die, cu);
11484
11485 if (die->child != NULL && ! die_is_declaration (die, cu))
11486 {
11487 struct field_info fi;
11488 struct die_info *child_die;
11489 VEC (symbolp) *template_args = NULL;
11490 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
11491
11492 memset (&fi, 0, sizeof (struct field_info));
11493
11494 child_die = die->child;
11495
11496 while (child_die && child_die->tag)
11497 {
11498 if (child_die->tag == DW_TAG_member
11499 || child_die->tag == DW_TAG_variable)
11500 {
11501 /* NOTE: carlton/2002-11-05: A C++ static data member
11502 should be a DW_TAG_member that is a declaration, but
11503 all versions of G++ as of this writing (so through at
11504 least 3.2.1) incorrectly generate DW_TAG_variable
11505 tags for them instead. */
11506 dwarf2_add_field (&fi, child_die, cu);
11507 }
11508 else if (child_die->tag == DW_TAG_subprogram)
11509 {
11510 /* C++ member function. */
11511 dwarf2_add_member_fn (&fi, child_die, type, cu);
11512 }
11513 else if (child_die->tag == DW_TAG_inheritance)
11514 {
11515 /* C++ base class field. */
11516 dwarf2_add_field (&fi, child_die, cu);
11517 }
11518 else if (child_die->tag == DW_TAG_typedef)
11519 dwarf2_add_typedef (&fi, child_die, cu);
11520 else if (child_die->tag == DW_TAG_template_type_param
11521 || child_die->tag == DW_TAG_template_value_param)
11522 {
11523 struct symbol *arg = new_symbol (child_die, NULL, cu);
11524
11525 if (arg != NULL)
11526 VEC_safe_push (symbolp, template_args, arg);
11527 }
11528
11529 child_die = sibling_die (child_die);
11530 }
11531
11532 /* Attach template arguments to type. */
11533 if (! VEC_empty (symbolp, template_args))
11534 {
11535 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11536 TYPE_N_TEMPLATE_ARGUMENTS (type)
11537 = VEC_length (symbolp, template_args);
11538 TYPE_TEMPLATE_ARGUMENTS (type)
11539 = obstack_alloc (&objfile->objfile_obstack,
11540 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11541 * sizeof (struct symbol *)));
11542 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
11543 VEC_address (symbolp, template_args),
11544 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11545 * sizeof (struct symbol *)));
11546 VEC_free (symbolp, template_args);
11547 }
11548
11549 /* Attach fields and member functions to the type. */
11550 if (fi.nfields)
11551 dwarf2_attach_fields_to_type (&fi, type, cu);
11552 if (fi.nfnfields)
11553 {
11554 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
11555
11556 /* Get the type which refers to the base class (possibly this
11557 class itself) which contains the vtable pointer for the current
11558 class from the DW_AT_containing_type attribute. This use of
11559 DW_AT_containing_type is a GNU extension. */
11560
11561 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
11562 {
11563 struct type *t = die_containing_type (die, cu);
11564
11565 TYPE_VPTR_BASETYPE (type) = t;
11566 if (type == t)
11567 {
11568 int i;
11569
11570 /* Our own class provides vtbl ptr. */
11571 for (i = TYPE_NFIELDS (t) - 1;
11572 i >= TYPE_N_BASECLASSES (t);
11573 --i)
11574 {
11575 const char *fieldname = TYPE_FIELD_NAME (t, i);
11576
11577 if (is_vtable_name (fieldname, cu))
11578 {
11579 TYPE_VPTR_FIELDNO (type) = i;
11580 break;
11581 }
11582 }
11583
11584 /* Complain if virtual function table field not found. */
11585 if (i < TYPE_N_BASECLASSES (t))
11586 complaint (&symfile_complaints,
11587 _("virtual function table pointer "
11588 "not found when defining class '%s'"),
11589 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
11590 "");
11591 }
11592 else
11593 {
11594 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
11595 }
11596 }
11597 else if (cu->producer
11598 && strncmp (cu->producer,
11599 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
11600 {
11601 /* The IBM XLC compiler does not provide direct indication
11602 of the containing type, but the vtable pointer is
11603 always named __vfp. */
11604
11605 int i;
11606
11607 for (i = TYPE_NFIELDS (type) - 1;
11608 i >= TYPE_N_BASECLASSES (type);
11609 --i)
11610 {
11611 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
11612 {
11613 TYPE_VPTR_FIELDNO (type) = i;
11614 TYPE_VPTR_BASETYPE (type) = type;
11615 break;
11616 }
11617 }
11618 }
11619 }
11620
11621 /* Copy fi.typedef_field_list linked list elements content into the
11622 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
11623 if (fi.typedef_field_list)
11624 {
11625 int i = fi.typedef_field_list_count;
11626
11627 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11628 TYPE_TYPEDEF_FIELD_ARRAY (type)
11629 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
11630 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
11631
11632 /* Reverse the list order to keep the debug info elements order. */
11633 while (--i >= 0)
11634 {
11635 struct typedef_field *dest, *src;
11636
11637 dest = &TYPE_TYPEDEF_FIELD (type, i);
11638 src = &fi.typedef_field_list->field;
11639 fi.typedef_field_list = fi.typedef_field_list->next;
11640 *dest = *src;
11641 }
11642 }
11643
11644 do_cleanups (back_to);
11645
11646 if (HAVE_CPLUS_STRUCT (type))
11647 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
11648 }
11649
11650 quirk_gcc_member_function_pointer (type, objfile);
11651
11652 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
11653 snapshots) has been known to create a die giving a declaration
11654 for a class that has, as a child, a die giving a definition for a
11655 nested class. So we have to process our children even if the
11656 current die is a declaration. Normally, of course, a declaration
11657 won't have any children at all. */
11658
11659 while (child_die != NULL && child_die->tag)
11660 {
11661 if (child_die->tag == DW_TAG_member
11662 || child_die->tag == DW_TAG_variable
11663 || child_die->tag == DW_TAG_inheritance
11664 || child_die->tag == DW_TAG_template_value_param
11665 || child_die->tag == DW_TAG_template_type_param)
11666 {
11667 /* Do nothing. */
11668 }
11669 else
11670 process_die (child_die, cu);
11671
11672 child_die = sibling_die (child_die);
11673 }
11674
11675 /* Do not consider external references. According to the DWARF standard,
11676 these DIEs are identified by the fact that they have no byte_size
11677 attribute, and a declaration attribute. */
11678 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
11679 || !die_is_declaration (die, cu))
11680 new_symbol (die, type, cu);
11681 }
11682
11683 /* Given a DW_AT_enumeration_type die, set its type. We do not
11684 complete the type's fields yet, or create any symbols. */
11685
11686 static struct type *
11687 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
11688 {
11689 struct objfile *objfile = cu->objfile;
11690 struct type *type;
11691 struct attribute *attr;
11692 const char *name;
11693
11694 /* If the definition of this type lives in .debug_types, read that type.
11695 Don't follow DW_AT_specification though, that will take us back up
11696 the chain and we want to go down. */
11697 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
11698 if (attr)
11699 {
11700 struct dwarf2_cu *type_cu = cu;
11701 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11702
11703 type = read_type_die (type_die, type_cu);
11704
11705 /* TYPE_CU may not be the same as CU.
11706 Ensure TYPE is recorded with CU in die_type_hash. */
11707 return set_die_type (die, type, cu);
11708 }
11709
11710 type = alloc_type (objfile);
11711
11712 TYPE_CODE (type) = TYPE_CODE_ENUM;
11713 name = dwarf2_full_name (NULL, die, cu);
11714 if (name != NULL)
11715 TYPE_TAG_NAME (type) = name;
11716
11717 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11718 if (attr)
11719 {
11720 TYPE_LENGTH (type) = DW_UNSND (attr);
11721 }
11722 else
11723 {
11724 TYPE_LENGTH (type) = 0;
11725 }
11726
11727 /* The enumeration DIE can be incomplete. In Ada, any type can be
11728 declared as private in the package spec, and then defined only
11729 inside the package body. Such types are known as Taft Amendment
11730 Types. When another package uses such a type, an incomplete DIE
11731 may be generated by the compiler. */
11732 if (die_is_declaration (die, cu))
11733 TYPE_STUB (type) = 1;
11734
11735 return set_die_type (die, type, cu);
11736 }
11737
11738 /* Given a pointer to a die which begins an enumeration, process all
11739 the dies that define the members of the enumeration, and create the
11740 symbol for the enumeration type.
11741
11742 NOTE: We reverse the order of the element list. */
11743
11744 static void
11745 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
11746 {
11747 struct type *this_type;
11748
11749 this_type = get_die_type (die, cu);
11750 if (this_type == NULL)
11751 this_type = read_enumeration_type (die, cu);
11752
11753 if (die->child != NULL)
11754 {
11755 struct die_info *child_die;
11756 struct symbol *sym;
11757 struct field *fields = NULL;
11758 int num_fields = 0;
11759 int unsigned_enum = 1;
11760 const char *name;
11761 int flag_enum = 1;
11762 ULONGEST mask = 0;
11763
11764 child_die = die->child;
11765 while (child_die && child_die->tag)
11766 {
11767 if (child_die->tag != DW_TAG_enumerator)
11768 {
11769 process_die (child_die, cu);
11770 }
11771 else
11772 {
11773 name = dwarf2_name (child_die, cu);
11774 if (name)
11775 {
11776 sym = new_symbol (child_die, this_type, cu);
11777 if (SYMBOL_VALUE (sym) < 0)
11778 {
11779 unsigned_enum = 0;
11780 flag_enum = 0;
11781 }
11782 else if ((mask & SYMBOL_VALUE (sym)) != 0)
11783 flag_enum = 0;
11784 else
11785 mask |= SYMBOL_VALUE (sym);
11786
11787 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
11788 {
11789 fields = (struct field *)
11790 xrealloc (fields,
11791 (num_fields + DW_FIELD_ALLOC_CHUNK)
11792 * sizeof (struct field));
11793 }
11794
11795 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
11796 FIELD_TYPE (fields[num_fields]) = NULL;
11797 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
11798 FIELD_BITSIZE (fields[num_fields]) = 0;
11799
11800 num_fields++;
11801 }
11802 }
11803
11804 child_die = sibling_die (child_die);
11805 }
11806
11807 if (num_fields)
11808 {
11809 TYPE_NFIELDS (this_type) = num_fields;
11810 TYPE_FIELDS (this_type) = (struct field *)
11811 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
11812 memcpy (TYPE_FIELDS (this_type), fields,
11813 sizeof (struct field) * num_fields);
11814 xfree (fields);
11815 }
11816 if (unsigned_enum)
11817 TYPE_UNSIGNED (this_type) = 1;
11818 if (flag_enum)
11819 TYPE_FLAG_ENUM (this_type) = 1;
11820 }
11821
11822 /* If we are reading an enum from a .debug_types unit, and the enum
11823 is a declaration, and the enum is not the signatured type in the
11824 unit, then we do not want to add a symbol for it. Adding a
11825 symbol would in some cases obscure the true definition of the
11826 enum, giving users an incomplete type when the definition is
11827 actually available. Note that we do not want to do this for all
11828 enums which are just declarations, because C++0x allows forward
11829 enum declarations. */
11830 if (cu->per_cu->is_debug_types
11831 && die_is_declaration (die, cu))
11832 {
11833 struct signatured_type *sig_type;
11834
11835 sig_type = (struct signatured_type *) cu->per_cu;
11836 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
11837 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
11838 return;
11839 }
11840
11841 new_symbol (die, this_type, cu);
11842 }
11843
11844 /* Extract all information from a DW_TAG_array_type DIE and put it in
11845 the DIE's type field. For now, this only handles one dimensional
11846 arrays. */
11847
11848 static struct type *
11849 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
11850 {
11851 struct objfile *objfile = cu->objfile;
11852 struct die_info *child_die;
11853 struct type *type;
11854 struct type *element_type, *range_type, *index_type;
11855 struct type **range_types = NULL;
11856 struct attribute *attr;
11857 int ndim = 0;
11858 struct cleanup *back_to;
11859 const char *name;
11860
11861 element_type = die_type (die, cu);
11862
11863 /* The die_type call above may have already set the type for this DIE. */
11864 type = get_die_type (die, cu);
11865 if (type)
11866 return type;
11867
11868 /* Irix 6.2 native cc creates array types without children for
11869 arrays with unspecified length. */
11870 if (die->child == NULL)
11871 {
11872 index_type = objfile_type (objfile)->builtin_int;
11873 range_type = create_range_type (NULL, index_type, 0, -1);
11874 type = create_array_type (NULL, element_type, range_type);
11875 return set_die_type (die, type, cu);
11876 }
11877
11878 back_to = make_cleanup (null_cleanup, NULL);
11879 child_die = die->child;
11880 while (child_die && child_die->tag)
11881 {
11882 if (child_die->tag == DW_TAG_subrange_type)
11883 {
11884 struct type *child_type = read_type_die (child_die, cu);
11885
11886 if (child_type != NULL)
11887 {
11888 /* The range type was succesfully read. Save it for the
11889 array type creation. */
11890 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
11891 {
11892 range_types = (struct type **)
11893 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
11894 * sizeof (struct type *));
11895 if (ndim == 0)
11896 make_cleanup (free_current_contents, &range_types);
11897 }
11898 range_types[ndim++] = child_type;
11899 }
11900 }
11901 child_die = sibling_die (child_die);
11902 }
11903
11904 /* Dwarf2 dimensions are output from left to right, create the
11905 necessary array types in backwards order. */
11906
11907 type = element_type;
11908
11909 if (read_array_order (die, cu) == DW_ORD_col_major)
11910 {
11911 int i = 0;
11912
11913 while (i < ndim)
11914 type = create_array_type (NULL, type, range_types[i++]);
11915 }
11916 else
11917 {
11918 while (ndim-- > 0)
11919 type = create_array_type (NULL, type, range_types[ndim]);
11920 }
11921
11922 /* Understand Dwarf2 support for vector types (like they occur on
11923 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
11924 array type. This is not part of the Dwarf2/3 standard yet, but a
11925 custom vendor extension. The main difference between a regular
11926 array and the vector variant is that vectors are passed by value
11927 to functions. */
11928 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
11929 if (attr)
11930 make_vector_type (type);
11931
11932 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
11933 implementation may choose to implement triple vectors using this
11934 attribute. */
11935 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11936 if (attr)
11937 {
11938 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
11939 TYPE_LENGTH (type) = DW_UNSND (attr);
11940 else
11941 complaint (&symfile_complaints,
11942 _("DW_AT_byte_size for array type smaller "
11943 "than the total size of elements"));
11944 }
11945
11946 name = dwarf2_name (die, cu);
11947 if (name)
11948 TYPE_NAME (type) = name;
11949
11950 /* Install the type in the die. */
11951 set_die_type (die, type, cu);
11952
11953 /* set_die_type should be already done. */
11954 set_descriptive_type (type, die, cu);
11955
11956 do_cleanups (back_to);
11957
11958 return type;
11959 }
11960
11961 static enum dwarf_array_dim_ordering
11962 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
11963 {
11964 struct attribute *attr;
11965
11966 attr = dwarf2_attr (die, DW_AT_ordering, cu);
11967
11968 if (attr) return DW_SND (attr);
11969
11970 /* GNU F77 is a special case, as at 08/2004 array type info is the
11971 opposite order to the dwarf2 specification, but data is still
11972 laid out as per normal fortran.
11973
11974 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
11975 version checking. */
11976
11977 if (cu->language == language_fortran
11978 && cu->producer && strstr (cu->producer, "GNU F77"))
11979 {
11980 return DW_ORD_row_major;
11981 }
11982
11983 switch (cu->language_defn->la_array_ordering)
11984 {
11985 case array_column_major:
11986 return DW_ORD_col_major;
11987 case array_row_major:
11988 default:
11989 return DW_ORD_row_major;
11990 };
11991 }
11992
11993 /* Extract all information from a DW_TAG_set_type DIE and put it in
11994 the DIE's type field. */
11995
11996 static struct type *
11997 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
11998 {
11999 struct type *domain_type, *set_type;
12000 struct attribute *attr;
12001
12002 domain_type = die_type (die, cu);
12003
12004 /* The die_type call above may have already set the type for this DIE. */
12005 set_type = get_die_type (die, cu);
12006 if (set_type)
12007 return set_type;
12008
12009 set_type = create_set_type (NULL, domain_type);
12010
12011 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12012 if (attr)
12013 TYPE_LENGTH (set_type) = DW_UNSND (attr);
12014
12015 return set_die_type (die, set_type, cu);
12016 }
12017
12018 /* A helper for read_common_block that creates a locexpr baton.
12019 SYM is the symbol which we are marking as computed.
12020 COMMON_DIE is the DIE for the common block.
12021 COMMON_LOC is the location expression attribute for the common
12022 block itself.
12023 MEMBER_LOC is the location expression attribute for the particular
12024 member of the common block that we are processing.
12025 CU is the CU from which the above come. */
12026
12027 static void
12028 mark_common_block_symbol_computed (struct symbol *sym,
12029 struct die_info *common_die,
12030 struct attribute *common_loc,
12031 struct attribute *member_loc,
12032 struct dwarf2_cu *cu)
12033 {
12034 struct objfile *objfile = dwarf2_per_objfile->objfile;
12035 struct dwarf2_locexpr_baton *baton;
12036 gdb_byte *ptr;
12037 unsigned int cu_off;
12038 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
12039 LONGEST offset = 0;
12040
12041 gdb_assert (common_loc && member_loc);
12042 gdb_assert (attr_form_is_block (common_loc));
12043 gdb_assert (attr_form_is_block (member_loc)
12044 || attr_form_is_constant (member_loc));
12045
12046 baton = obstack_alloc (&objfile->objfile_obstack,
12047 sizeof (struct dwarf2_locexpr_baton));
12048 baton->per_cu = cu->per_cu;
12049 gdb_assert (baton->per_cu);
12050
12051 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
12052
12053 if (attr_form_is_constant (member_loc))
12054 {
12055 offset = dwarf2_get_attr_constant_value (member_loc, 0);
12056 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
12057 }
12058 else
12059 baton->size += DW_BLOCK (member_loc)->size;
12060
12061 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
12062 baton->data = ptr;
12063
12064 *ptr++ = DW_OP_call4;
12065 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
12066 store_unsigned_integer (ptr, 4, byte_order, cu_off);
12067 ptr += 4;
12068
12069 if (attr_form_is_constant (member_loc))
12070 {
12071 *ptr++ = DW_OP_addr;
12072 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
12073 ptr += cu->header.addr_size;
12074 }
12075 else
12076 {
12077 /* We have to copy the data here, because DW_OP_call4 will only
12078 use a DW_AT_location attribute. */
12079 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
12080 ptr += DW_BLOCK (member_loc)->size;
12081 }
12082
12083 *ptr++ = DW_OP_plus;
12084 gdb_assert (ptr - baton->data == baton->size);
12085
12086 SYMBOL_LOCATION_BATON (sym) = baton;
12087 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
12088 }
12089
12090 /* Create appropriate locally-scoped variables for all the
12091 DW_TAG_common_block entries. Also create a struct common_block
12092 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
12093 is used to sepate the common blocks name namespace from regular
12094 variable names. */
12095
12096 static void
12097 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
12098 {
12099 struct attribute *attr;
12100
12101 attr = dwarf2_attr (die, DW_AT_location, cu);
12102 if (attr)
12103 {
12104 /* Support the .debug_loc offsets. */
12105 if (attr_form_is_block (attr))
12106 {
12107 /* Ok. */
12108 }
12109 else if (attr_form_is_section_offset (attr))
12110 {
12111 dwarf2_complex_location_expr_complaint ();
12112 attr = NULL;
12113 }
12114 else
12115 {
12116 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
12117 "common block member");
12118 attr = NULL;
12119 }
12120 }
12121
12122 if (die->child != NULL)
12123 {
12124 struct objfile *objfile = cu->objfile;
12125 struct die_info *child_die;
12126 size_t n_entries = 0, size;
12127 struct common_block *common_block;
12128 struct symbol *sym;
12129
12130 for (child_die = die->child;
12131 child_die && child_die->tag;
12132 child_die = sibling_die (child_die))
12133 ++n_entries;
12134
12135 size = (sizeof (struct common_block)
12136 + (n_entries - 1) * sizeof (struct symbol *));
12137 common_block = obstack_alloc (&objfile->objfile_obstack, size);
12138 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
12139 common_block->n_entries = 0;
12140
12141 for (child_die = die->child;
12142 child_die && child_die->tag;
12143 child_die = sibling_die (child_die))
12144 {
12145 /* Create the symbol in the DW_TAG_common_block block in the current
12146 symbol scope. */
12147 sym = new_symbol (child_die, NULL, cu);
12148 if (sym != NULL)
12149 {
12150 struct attribute *member_loc;
12151
12152 common_block->contents[common_block->n_entries++] = sym;
12153
12154 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
12155 cu);
12156 if (member_loc)
12157 {
12158 /* GDB has handled this for a long time, but it is
12159 not specified by DWARF. It seems to have been
12160 emitted by gfortran at least as recently as:
12161 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
12162 complaint (&symfile_complaints,
12163 _("Variable in common block has "
12164 "DW_AT_data_member_location "
12165 "- DIE at 0x%x [in module %s]"),
12166 child_die->offset.sect_off, cu->objfile->name);
12167
12168 if (attr_form_is_section_offset (member_loc))
12169 dwarf2_complex_location_expr_complaint ();
12170 else if (attr_form_is_constant (member_loc)
12171 || attr_form_is_block (member_loc))
12172 {
12173 if (attr)
12174 mark_common_block_symbol_computed (sym, die, attr,
12175 member_loc, cu);
12176 }
12177 else
12178 dwarf2_complex_location_expr_complaint ();
12179 }
12180 }
12181 }
12182
12183 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
12184 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
12185 }
12186 }
12187
12188 /* Create a type for a C++ namespace. */
12189
12190 static struct type *
12191 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
12192 {
12193 struct objfile *objfile = cu->objfile;
12194 const char *previous_prefix, *name;
12195 int is_anonymous;
12196 struct type *type;
12197
12198 /* For extensions, reuse the type of the original namespace. */
12199 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
12200 {
12201 struct die_info *ext_die;
12202 struct dwarf2_cu *ext_cu = cu;
12203
12204 ext_die = dwarf2_extension (die, &ext_cu);
12205 type = read_type_die (ext_die, ext_cu);
12206
12207 /* EXT_CU may not be the same as CU.
12208 Ensure TYPE is recorded with CU in die_type_hash. */
12209 return set_die_type (die, type, cu);
12210 }
12211
12212 name = namespace_name (die, &is_anonymous, cu);
12213
12214 /* Now build the name of the current namespace. */
12215
12216 previous_prefix = determine_prefix (die, cu);
12217 if (previous_prefix[0] != '\0')
12218 name = typename_concat (&objfile->objfile_obstack,
12219 previous_prefix, name, 0, cu);
12220
12221 /* Create the type. */
12222 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
12223 objfile);
12224 TYPE_NAME (type) = name;
12225 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12226
12227 return set_die_type (die, type, cu);
12228 }
12229
12230 /* Read a C++ namespace. */
12231
12232 static void
12233 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
12234 {
12235 struct objfile *objfile = cu->objfile;
12236 int is_anonymous;
12237
12238 /* Add a symbol associated to this if we haven't seen the namespace
12239 before. Also, add a using directive if it's an anonymous
12240 namespace. */
12241
12242 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
12243 {
12244 struct type *type;
12245
12246 type = read_type_die (die, cu);
12247 new_symbol (die, type, cu);
12248
12249 namespace_name (die, &is_anonymous, cu);
12250 if (is_anonymous)
12251 {
12252 const char *previous_prefix = determine_prefix (die, cu);
12253
12254 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
12255 NULL, NULL, 0, &objfile->objfile_obstack);
12256 }
12257 }
12258
12259 if (die->child != NULL)
12260 {
12261 struct die_info *child_die = die->child;
12262
12263 while (child_die && child_die->tag)
12264 {
12265 process_die (child_die, cu);
12266 child_die = sibling_die (child_die);
12267 }
12268 }
12269 }
12270
12271 /* Read a Fortran module as type. This DIE can be only a declaration used for
12272 imported module. Still we need that type as local Fortran "use ... only"
12273 declaration imports depend on the created type in determine_prefix. */
12274
12275 static struct type *
12276 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
12277 {
12278 struct objfile *objfile = cu->objfile;
12279 const char *module_name;
12280 struct type *type;
12281
12282 module_name = dwarf2_name (die, cu);
12283 if (!module_name)
12284 complaint (&symfile_complaints,
12285 _("DW_TAG_module has no name, offset 0x%x"),
12286 die->offset.sect_off);
12287 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
12288
12289 /* determine_prefix uses TYPE_TAG_NAME. */
12290 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12291
12292 return set_die_type (die, type, cu);
12293 }
12294
12295 /* Read a Fortran module. */
12296
12297 static void
12298 read_module (struct die_info *die, struct dwarf2_cu *cu)
12299 {
12300 struct die_info *child_die = die->child;
12301
12302 while (child_die && child_die->tag)
12303 {
12304 process_die (child_die, cu);
12305 child_die = sibling_die (child_die);
12306 }
12307 }
12308
12309 /* Return the name of the namespace represented by DIE. Set
12310 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
12311 namespace. */
12312
12313 static const char *
12314 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
12315 {
12316 struct die_info *current_die;
12317 const char *name = NULL;
12318
12319 /* Loop through the extensions until we find a name. */
12320
12321 for (current_die = die;
12322 current_die != NULL;
12323 current_die = dwarf2_extension (die, &cu))
12324 {
12325 name = dwarf2_name (current_die, cu);
12326 if (name != NULL)
12327 break;
12328 }
12329
12330 /* Is it an anonymous namespace? */
12331
12332 *is_anonymous = (name == NULL);
12333 if (*is_anonymous)
12334 name = CP_ANONYMOUS_NAMESPACE_STR;
12335
12336 return name;
12337 }
12338
12339 /* Extract all information from a DW_TAG_pointer_type DIE and add to
12340 the user defined type vector. */
12341
12342 static struct type *
12343 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
12344 {
12345 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
12346 struct comp_unit_head *cu_header = &cu->header;
12347 struct type *type;
12348 struct attribute *attr_byte_size;
12349 struct attribute *attr_address_class;
12350 int byte_size, addr_class;
12351 struct type *target_type;
12352
12353 target_type = die_type (die, cu);
12354
12355 /* The die_type call above may have already set the type for this DIE. */
12356 type = get_die_type (die, cu);
12357 if (type)
12358 return type;
12359
12360 type = lookup_pointer_type (target_type);
12361
12362 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
12363 if (attr_byte_size)
12364 byte_size = DW_UNSND (attr_byte_size);
12365 else
12366 byte_size = cu_header->addr_size;
12367
12368 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
12369 if (attr_address_class)
12370 addr_class = DW_UNSND (attr_address_class);
12371 else
12372 addr_class = DW_ADDR_none;
12373
12374 /* If the pointer size or address class is different than the
12375 default, create a type variant marked as such and set the
12376 length accordingly. */
12377 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
12378 {
12379 if (gdbarch_address_class_type_flags_p (gdbarch))
12380 {
12381 int type_flags;
12382
12383 type_flags = gdbarch_address_class_type_flags
12384 (gdbarch, byte_size, addr_class);
12385 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
12386 == 0);
12387 type = make_type_with_address_space (type, type_flags);
12388 }
12389 else if (TYPE_LENGTH (type) != byte_size)
12390 {
12391 complaint (&symfile_complaints,
12392 _("invalid pointer size %d"), byte_size);
12393 }
12394 else
12395 {
12396 /* Should we also complain about unhandled address classes? */
12397 }
12398 }
12399
12400 TYPE_LENGTH (type) = byte_size;
12401 return set_die_type (die, type, cu);
12402 }
12403
12404 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
12405 the user defined type vector. */
12406
12407 static struct type *
12408 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
12409 {
12410 struct type *type;
12411 struct type *to_type;
12412 struct type *domain;
12413
12414 to_type = die_type (die, cu);
12415 domain = die_containing_type (die, cu);
12416
12417 /* The calls above may have already set the type for this DIE. */
12418 type = get_die_type (die, cu);
12419 if (type)
12420 return type;
12421
12422 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
12423 type = lookup_methodptr_type (to_type);
12424 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
12425 {
12426 struct type *new_type = alloc_type (cu->objfile);
12427
12428 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
12429 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
12430 TYPE_VARARGS (to_type));
12431 type = lookup_methodptr_type (new_type);
12432 }
12433 else
12434 type = lookup_memberptr_type (to_type, domain);
12435
12436 return set_die_type (die, type, cu);
12437 }
12438
12439 /* Extract all information from a DW_TAG_reference_type DIE and add to
12440 the user defined type vector. */
12441
12442 static struct type *
12443 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
12444 {
12445 struct comp_unit_head *cu_header = &cu->header;
12446 struct type *type, *target_type;
12447 struct attribute *attr;
12448
12449 target_type = die_type (die, cu);
12450
12451 /* The die_type call above may have already set the type for this DIE. */
12452 type = get_die_type (die, cu);
12453 if (type)
12454 return type;
12455
12456 type = lookup_reference_type (target_type);
12457 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12458 if (attr)
12459 {
12460 TYPE_LENGTH (type) = DW_UNSND (attr);
12461 }
12462 else
12463 {
12464 TYPE_LENGTH (type) = cu_header->addr_size;
12465 }
12466 return set_die_type (die, type, cu);
12467 }
12468
12469 static struct type *
12470 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
12471 {
12472 struct type *base_type, *cv_type;
12473
12474 base_type = die_type (die, cu);
12475
12476 /* The die_type call above may have already set the type for this DIE. */
12477 cv_type = get_die_type (die, cu);
12478 if (cv_type)
12479 return cv_type;
12480
12481 /* In case the const qualifier is applied to an array type, the element type
12482 is so qualified, not the array type (section 6.7.3 of C99). */
12483 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
12484 {
12485 struct type *el_type, *inner_array;
12486
12487 base_type = copy_type (base_type);
12488 inner_array = base_type;
12489
12490 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
12491 {
12492 TYPE_TARGET_TYPE (inner_array) =
12493 copy_type (TYPE_TARGET_TYPE (inner_array));
12494 inner_array = TYPE_TARGET_TYPE (inner_array);
12495 }
12496
12497 el_type = TYPE_TARGET_TYPE (inner_array);
12498 TYPE_TARGET_TYPE (inner_array) =
12499 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
12500
12501 return set_die_type (die, base_type, cu);
12502 }
12503
12504 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
12505 return set_die_type (die, cv_type, cu);
12506 }
12507
12508 static struct type *
12509 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
12510 {
12511 struct type *base_type, *cv_type;
12512
12513 base_type = die_type (die, cu);
12514
12515 /* The die_type call above may have already set the type for this DIE. */
12516 cv_type = get_die_type (die, cu);
12517 if (cv_type)
12518 return cv_type;
12519
12520 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
12521 return set_die_type (die, cv_type, cu);
12522 }
12523
12524 /* Handle DW_TAG_restrict_type. */
12525
12526 static struct type *
12527 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
12528 {
12529 struct type *base_type, *cv_type;
12530
12531 base_type = die_type (die, cu);
12532
12533 /* The die_type call above may have already set the type for this DIE. */
12534 cv_type = get_die_type (die, cu);
12535 if (cv_type)
12536 return cv_type;
12537
12538 cv_type = make_restrict_type (base_type);
12539 return set_die_type (die, cv_type, cu);
12540 }
12541
12542 /* Extract all information from a DW_TAG_string_type DIE and add to
12543 the user defined type vector. It isn't really a user defined type,
12544 but it behaves like one, with other DIE's using an AT_user_def_type
12545 attribute to reference it. */
12546
12547 static struct type *
12548 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
12549 {
12550 struct objfile *objfile = cu->objfile;
12551 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12552 struct type *type, *range_type, *index_type, *char_type;
12553 struct attribute *attr;
12554 unsigned int length;
12555
12556 attr = dwarf2_attr (die, DW_AT_string_length, cu);
12557 if (attr)
12558 {
12559 length = DW_UNSND (attr);
12560 }
12561 else
12562 {
12563 /* Check for the DW_AT_byte_size attribute. */
12564 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12565 if (attr)
12566 {
12567 length = DW_UNSND (attr);
12568 }
12569 else
12570 {
12571 length = 1;
12572 }
12573 }
12574
12575 index_type = objfile_type (objfile)->builtin_int;
12576 range_type = create_range_type (NULL, index_type, 1, length);
12577 char_type = language_string_char_type (cu->language_defn, gdbarch);
12578 type = create_string_type (NULL, char_type, range_type);
12579
12580 return set_die_type (die, type, cu);
12581 }
12582
12583 /* Handle DIES due to C code like:
12584
12585 struct foo
12586 {
12587 int (*funcp)(int a, long l);
12588 int b;
12589 };
12590
12591 ('funcp' generates a DW_TAG_subroutine_type DIE). */
12592
12593 static struct type *
12594 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
12595 {
12596 struct objfile *objfile = cu->objfile;
12597 struct type *type; /* Type that this function returns. */
12598 struct type *ftype; /* Function that returns above type. */
12599 struct attribute *attr;
12600
12601 type = die_type (die, cu);
12602
12603 /* The die_type call above may have already set the type for this DIE. */
12604 ftype = get_die_type (die, cu);
12605 if (ftype)
12606 return ftype;
12607
12608 ftype = lookup_function_type (type);
12609
12610 /* All functions in C++, Pascal and Java have prototypes. */
12611 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
12612 if ((attr && (DW_UNSND (attr) != 0))
12613 || cu->language == language_cplus
12614 || cu->language == language_java
12615 || cu->language == language_pascal)
12616 TYPE_PROTOTYPED (ftype) = 1;
12617 else if (producer_is_realview (cu->producer))
12618 /* RealView does not emit DW_AT_prototyped. We can not
12619 distinguish prototyped and unprototyped functions; default to
12620 prototyped, since that is more common in modern code (and
12621 RealView warns about unprototyped functions). */
12622 TYPE_PROTOTYPED (ftype) = 1;
12623
12624 /* Store the calling convention in the type if it's available in
12625 the subroutine die. Otherwise set the calling convention to
12626 the default value DW_CC_normal. */
12627 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
12628 if (attr)
12629 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
12630 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
12631 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
12632 else
12633 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
12634
12635 /* We need to add the subroutine type to the die immediately so
12636 we don't infinitely recurse when dealing with parameters
12637 declared as the same subroutine type. */
12638 set_die_type (die, ftype, cu);
12639
12640 if (die->child != NULL)
12641 {
12642 struct type *void_type = objfile_type (objfile)->builtin_void;
12643 struct die_info *child_die;
12644 int nparams, iparams;
12645
12646 /* Count the number of parameters.
12647 FIXME: GDB currently ignores vararg functions, but knows about
12648 vararg member functions. */
12649 nparams = 0;
12650 child_die = die->child;
12651 while (child_die && child_die->tag)
12652 {
12653 if (child_die->tag == DW_TAG_formal_parameter)
12654 nparams++;
12655 else if (child_die->tag == DW_TAG_unspecified_parameters)
12656 TYPE_VARARGS (ftype) = 1;
12657 child_die = sibling_die (child_die);
12658 }
12659
12660 /* Allocate storage for parameters and fill them in. */
12661 TYPE_NFIELDS (ftype) = nparams;
12662 TYPE_FIELDS (ftype) = (struct field *)
12663 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
12664
12665 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
12666 even if we error out during the parameters reading below. */
12667 for (iparams = 0; iparams < nparams; iparams++)
12668 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
12669
12670 iparams = 0;
12671 child_die = die->child;
12672 while (child_die && child_die->tag)
12673 {
12674 if (child_die->tag == DW_TAG_formal_parameter)
12675 {
12676 struct type *arg_type;
12677
12678 /* DWARF version 2 has no clean way to discern C++
12679 static and non-static member functions. G++ helps
12680 GDB by marking the first parameter for non-static
12681 member functions (which is the this pointer) as
12682 artificial. We pass this information to
12683 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
12684
12685 DWARF version 3 added DW_AT_object_pointer, which GCC
12686 4.5 does not yet generate. */
12687 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
12688 if (attr)
12689 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
12690 else
12691 {
12692 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
12693
12694 /* GCC/43521: In java, the formal parameter
12695 "this" is sometimes not marked with DW_AT_artificial. */
12696 if (cu->language == language_java)
12697 {
12698 const char *name = dwarf2_name (child_die, cu);
12699
12700 if (name && !strcmp (name, "this"))
12701 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
12702 }
12703 }
12704 arg_type = die_type (child_die, cu);
12705
12706 /* RealView does not mark THIS as const, which the testsuite
12707 expects. GCC marks THIS as const in method definitions,
12708 but not in the class specifications (GCC PR 43053). */
12709 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
12710 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
12711 {
12712 int is_this = 0;
12713 struct dwarf2_cu *arg_cu = cu;
12714 const char *name = dwarf2_name (child_die, cu);
12715
12716 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
12717 if (attr)
12718 {
12719 /* If the compiler emits this, use it. */
12720 if (follow_die_ref (die, attr, &arg_cu) == child_die)
12721 is_this = 1;
12722 }
12723 else if (name && strcmp (name, "this") == 0)
12724 /* Function definitions will have the argument names. */
12725 is_this = 1;
12726 else if (name == NULL && iparams == 0)
12727 /* Declarations may not have the names, so like
12728 elsewhere in GDB, assume an artificial first
12729 argument is "this". */
12730 is_this = 1;
12731
12732 if (is_this)
12733 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
12734 arg_type, 0);
12735 }
12736
12737 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
12738 iparams++;
12739 }
12740 child_die = sibling_die (child_die);
12741 }
12742 }
12743
12744 return ftype;
12745 }
12746
12747 static struct type *
12748 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
12749 {
12750 struct objfile *objfile = cu->objfile;
12751 const char *name = NULL;
12752 struct type *this_type, *target_type;
12753
12754 name = dwarf2_full_name (NULL, die, cu);
12755 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
12756 TYPE_FLAG_TARGET_STUB, NULL, objfile);
12757 TYPE_NAME (this_type) = name;
12758 set_die_type (die, this_type, cu);
12759 target_type = die_type (die, cu);
12760 if (target_type != this_type)
12761 TYPE_TARGET_TYPE (this_type) = target_type;
12762 else
12763 {
12764 /* Self-referential typedefs are, it seems, not allowed by the DWARF
12765 spec and cause infinite loops in GDB. */
12766 complaint (&symfile_complaints,
12767 _("Self-referential DW_TAG_typedef "
12768 "- DIE at 0x%x [in module %s]"),
12769 die->offset.sect_off, objfile->name);
12770 TYPE_TARGET_TYPE (this_type) = NULL;
12771 }
12772 return this_type;
12773 }
12774
12775 /* Find a representation of a given base type and install
12776 it in the TYPE field of the die. */
12777
12778 static struct type *
12779 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
12780 {
12781 struct objfile *objfile = cu->objfile;
12782 struct type *type;
12783 struct attribute *attr;
12784 int encoding = 0, size = 0;
12785 const char *name;
12786 enum type_code code = TYPE_CODE_INT;
12787 int type_flags = 0;
12788 struct type *target_type = NULL;
12789
12790 attr = dwarf2_attr (die, DW_AT_encoding, cu);
12791 if (attr)
12792 {
12793 encoding = DW_UNSND (attr);
12794 }
12795 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12796 if (attr)
12797 {
12798 size = DW_UNSND (attr);
12799 }
12800 name = dwarf2_name (die, cu);
12801 if (!name)
12802 {
12803 complaint (&symfile_complaints,
12804 _("DW_AT_name missing from DW_TAG_base_type"));
12805 }
12806
12807 switch (encoding)
12808 {
12809 case DW_ATE_address:
12810 /* Turn DW_ATE_address into a void * pointer. */
12811 code = TYPE_CODE_PTR;
12812 type_flags |= TYPE_FLAG_UNSIGNED;
12813 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
12814 break;
12815 case DW_ATE_boolean:
12816 code = TYPE_CODE_BOOL;
12817 type_flags |= TYPE_FLAG_UNSIGNED;
12818 break;
12819 case DW_ATE_complex_float:
12820 code = TYPE_CODE_COMPLEX;
12821 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
12822 break;
12823 case DW_ATE_decimal_float:
12824 code = TYPE_CODE_DECFLOAT;
12825 break;
12826 case DW_ATE_float:
12827 code = TYPE_CODE_FLT;
12828 break;
12829 case DW_ATE_signed:
12830 break;
12831 case DW_ATE_unsigned:
12832 type_flags |= TYPE_FLAG_UNSIGNED;
12833 if (cu->language == language_fortran
12834 && name
12835 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
12836 code = TYPE_CODE_CHAR;
12837 break;
12838 case DW_ATE_signed_char:
12839 if (cu->language == language_ada || cu->language == language_m2
12840 || cu->language == language_pascal
12841 || cu->language == language_fortran)
12842 code = TYPE_CODE_CHAR;
12843 break;
12844 case DW_ATE_unsigned_char:
12845 if (cu->language == language_ada || cu->language == language_m2
12846 || cu->language == language_pascal
12847 || cu->language == language_fortran)
12848 code = TYPE_CODE_CHAR;
12849 type_flags |= TYPE_FLAG_UNSIGNED;
12850 break;
12851 case DW_ATE_UTF:
12852 /* We just treat this as an integer and then recognize the
12853 type by name elsewhere. */
12854 break;
12855
12856 default:
12857 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
12858 dwarf_type_encoding_name (encoding));
12859 break;
12860 }
12861
12862 type = init_type (code, size, type_flags, NULL, objfile);
12863 TYPE_NAME (type) = name;
12864 TYPE_TARGET_TYPE (type) = target_type;
12865
12866 if (name && strcmp (name, "char") == 0)
12867 TYPE_NOSIGN (type) = 1;
12868
12869 return set_die_type (die, type, cu);
12870 }
12871
12872 /* Read the given DW_AT_subrange DIE. */
12873
12874 static struct type *
12875 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
12876 {
12877 struct type *base_type, *orig_base_type;
12878 struct type *range_type;
12879 struct attribute *attr;
12880 LONGEST low, high;
12881 int low_default_is_valid;
12882 const char *name;
12883 LONGEST negative_mask;
12884
12885 orig_base_type = die_type (die, cu);
12886 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
12887 whereas the real type might be. So, we use ORIG_BASE_TYPE when
12888 creating the range type, but we use the result of check_typedef
12889 when examining properties of the type. */
12890 base_type = check_typedef (orig_base_type);
12891
12892 /* The die_type call above may have already set the type for this DIE. */
12893 range_type = get_die_type (die, cu);
12894 if (range_type)
12895 return range_type;
12896
12897 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
12898 omitting DW_AT_lower_bound. */
12899 switch (cu->language)
12900 {
12901 case language_c:
12902 case language_cplus:
12903 low = 0;
12904 low_default_is_valid = 1;
12905 break;
12906 case language_fortran:
12907 low = 1;
12908 low_default_is_valid = 1;
12909 break;
12910 case language_d:
12911 case language_java:
12912 case language_objc:
12913 low = 0;
12914 low_default_is_valid = (cu->header.version >= 4);
12915 break;
12916 case language_ada:
12917 case language_m2:
12918 case language_pascal:
12919 low = 1;
12920 low_default_is_valid = (cu->header.version >= 4);
12921 break;
12922 default:
12923 low = 0;
12924 low_default_is_valid = 0;
12925 break;
12926 }
12927
12928 /* FIXME: For variable sized arrays either of these could be
12929 a variable rather than a constant value. We'll allow it,
12930 but we don't know how to handle it. */
12931 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
12932 if (attr)
12933 low = dwarf2_get_attr_constant_value (attr, low);
12934 else if (!low_default_is_valid)
12935 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
12936 "- DIE at 0x%x [in module %s]"),
12937 die->offset.sect_off, cu->objfile->name);
12938
12939 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
12940 if (attr)
12941 {
12942 if (attr_form_is_block (attr) || is_ref_attr (attr))
12943 {
12944 /* GCC encodes arrays with unspecified or dynamic length
12945 with a DW_FORM_block1 attribute or a reference attribute.
12946 FIXME: GDB does not yet know how to handle dynamic
12947 arrays properly, treat them as arrays with unspecified
12948 length for now.
12949
12950 FIXME: jimb/2003-09-22: GDB does not really know
12951 how to handle arrays of unspecified length
12952 either; we just represent them as zero-length
12953 arrays. Choose an appropriate upper bound given
12954 the lower bound we've computed above. */
12955 high = low - 1;
12956 }
12957 else
12958 high = dwarf2_get_attr_constant_value (attr, 1);
12959 }
12960 else
12961 {
12962 attr = dwarf2_attr (die, DW_AT_count, cu);
12963 if (attr)
12964 {
12965 int count = dwarf2_get_attr_constant_value (attr, 1);
12966 high = low + count - 1;
12967 }
12968 else
12969 {
12970 /* Unspecified array length. */
12971 high = low - 1;
12972 }
12973 }
12974
12975 /* Dwarf-2 specifications explicitly allows to create subrange types
12976 without specifying a base type.
12977 In that case, the base type must be set to the type of
12978 the lower bound, upper bound or count, in that order, if any of these
12979 three attributes references an object that has a type.
12980 If no base type is found, the Dwarf-2 specifications say that
12981 a signed integer type of size equal to the size of an address should
12982 be used.
12983 For the following C code: `extern char gdb_int [];'
12984 GCC produces an empty range DIE.
12985 FIXME: muller/2010-05-28: Possible references to object for low bound,
12986 high bound or count are not yet handled by this code. */
12987 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
12988 {
12989 struct objfile *objfile = cu->objfile;
12990 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12991 int addr_size = gdbarch_addr_bit (gdbarch) /8;
12992 struct type *int_type = objfile_type (objfile)->builtin_int;
12993
12994 /* Test "int", "long int", and "long long int" objfile types,
12995 and select the first one having a size above or equal to the
12996 architecture address size. */
12997 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12998 base_type = int_type;
12999 else
13000 {
13001 int_type = objfile_type (objfile)->builtin_long;
13002 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13003 base_type = int_type;
13004 else
13005 {
13006 int_type = objfile_type (objfile)->builtin_long_long;
13007 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13008 base_type = int_type;
13009 }
13010 }
13011 }
13012
13013 negative_mask =
13014 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
13015 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
13016 low |= negative_mask;
13017 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
13018 high |= negative_mask;
13019
13020 range_type = create_range_type (NULL, orig_base_type, low, high);
13021
13022 /* Mark arrays with dynamic length at least as an array of unspecified
13023 length. GDB could check the boundary but before it gets implemented at
13024 least allow accessing the array elements. */
13025 if (attr && attr_form_is_block (attr))
13026 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
13027
13028 /* Ada expects an empty array on no boundary attributes. */
13029 if (attr == NULL && cu->language != language_ada)
13030 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
13031
13032 name = dwarf2_name (die, cu);
13033 if (name)
13034 TYPE_NAME (range_type) = name;
13035
13036 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13037 if (attr)
13038 TYPE_LENGTH (range_type) = DW_UNSND (attr);
13039
13040 set_die_type (die, range_type, cu);
13041
13042 /* set_die_type should be already done. */
13043 set_descriptive_type (range_type, die, cu);
13044
13045 return range_type;
13046 }
13047
13048 static struct type *
13049 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
13050 {
13051 struct type *type;
13052
13053 /* For now, we only support the C meaning of an unspecified type: void. */
13054
13055 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
13056 TYPE_NAME (type) = dwarf2_name (die, cu);
13057
13058 return set_die_type (die, type, cu);
13059 }
13060
13061 /* Read a single die and all its descendents. Set the die's sibling
13062 field to NULL; set other fields in the die correctly, and set all
13063 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
13064 location of the info_ptr after reading all of those dies. PARENT
13065 is the parent of the die in question. */
13066
13067 static struct die_info *
13068 read_die_and_children (const struct die_reader_specs *reader,
13069 gdb_byte *info_ptr,
13070 gdb_byte **new_info_ptr,
13071 struct die_info *parent)
13072 {
13073 struct die_info *die;
13074 gdb_byte *cur_ptr;
13075 int has_children;
13076
13077 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
13078 if (die == NULL)
13079 {
13080 *new_info_ptr = cur_ptr;
13081 return NULL;
13082 }
13083 store_in_ref_table (die, reader->cu);
13084
13085 if (has_children)
13086 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
13087 else
13088 {
13089 die->child = NULL;
13090 *new_info_ptr = cur_ptr;
13091 }
13092
13093 die->sibling = NULL;
13094 die->parent = parent;
13095 return die;
13096 }
13097
13098 /* Read a die, all of its descendents, and all of its siblings; set
13099 all of the fields of all of the dies correctly. Arguments are as
13100 in read_die_and_children. */
13101
13102 static struct die_info *
13103 read_die_and_siblings_1 (const struct die_reader_specs *reader,
13104 gdb_byte *info_ptr,
13105 gdb_byte **new_info_ptr,
13106 struct die_info *parent)
13107 {
13108 struct die_info *first_die, *last_sibling;
13109 gdb_byte *cur_ptr;
13110
13111 cur_ptr = info_ptr;
13112 first_die = last_sibling = NULL;
13113
13114 while (1)
13115 {
13116 struct die_info *die
13117 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
13118
13119 if (die == NULL)
13120 {
13121 *new_info_ptr = cur_ptr;
13122 return first_die;
13123 }
13124
13125 if (!first_die)
13126 first_die = die;
13127 else
13128 last_sibling->sibling = die;
13129
13130 last_sibling = die;
13131 }
13132 }
13133
13134 /* Read a die, all of its descendents, and all of its siblings; set
13135 all of the fields of all of the dies correctly. Arguments are as
13136 in read_die_and_children.
13137 This the main entry point for reading a DIE and all its children. */
13138
13139 static struct die_info *
13140 read_die_and_siblings (const struct die_reader_specs *reader,
13141 gdb_byte *info_ptr,
13142 gdb_byte **new_info_ptr,
13143 struct die_info *parent)
13144 {
13145 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
13146 new_info_ptr, parent);
13147
13148 if (dwarf2_die_debug)
13149 {
13150 fprintf_unfiltered (gdb_stdlog,
13151 "Read die from %s@0x%x of %s:\n",
13152 bfd_section_name (reader->abfd,
13153 reader->die_section->asection),
13154 (unsigned) (info_ptr - reader->die_section->buffer),
13155 bfd_get_filename (reader->abfd));
13156 dump_die (die, dwarf2_die_debug);
13157 }
13158
13159 return die;
13160 }
13161
13162 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
13163 attributes.
13164 The caller is responsible for filling in the extra attributes
13165 and updating (*DIEP)->num_attrs.
13166 Set DIEP to point to a newly allocated die with its information,
13167 except for its child, sibling, and parent fields.
13168 Set HAS_CHILDREN to tell whether the die has children or not. */
13169
13170 static gdb_byte *
13171 read_full_die_1 (const struct die_reader_specs *reader,
13172 struct die_info **diep, gdb_byte *info_ptr,
13173 int *has_children, int num_extra_attrs)
13174 {
13175 unsigned int abbrev_number, bytes_read, i;
13176 sect_offset offset;
13177 struct abbrev_info *abbrev;
13178 struct die_info *die;
13179 struct dwarf2_cu *cu = reader->cu;
13180 bfd *abfd = reader->abfd;
13181
13182 offset.sect_off = info_ptr - reader->buffer;
13183 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13184 info_ptr += bytes_read;
13185 if (!abbrev_number)
13186 {
13187 *diep = NULL;
13188 *has_children = 0;
13189 return info_ptr;
13190 }
13191
13192 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
13193 if (!abbrev)
13194 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
13195 abbrev_number,
13196 bfd_get_filename (abfd));
13197
13198 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
13199 die->offset = offset;
13200 die->tag = abbrev->tag;
13201 die->abbrev = abbrev_number;
13202
13203 /* Make the result usable.
13204 The caller needs to update num_attrs after adding the extra
13205 attributes. */
13206 die->num_attrs = abbrev->num_attrs;
13207
13208 for (i = 0; i < abbrev->num_attrs; ++i)
13209 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
13210 info_ptr);
13211
13212 *diep = die;
13213 *has_children = abbrev->has_children;
13214 return info_ptr;
13215 }
13216
13217 /* Read a die and all its attributes.
13218 Set DIEP to point to a newly allocated die with its information,
13219 except for its child, sibling, and parent fields.
13220 Set HAS_CHILDREN to tell whether the die has children or not. */
13221
13222 static gdb_byte *
13223 read_full_die (const struct die_reader_specs *reader,
13224 struct die_info **diep, gdb_byte *info_ptr,
13225 int *has_children)
13226 {
13227 gdb_byte *result;
13228
13229 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
13230
13231 if (dwarf2_die_debug)
13232 {
13233 fprintf_unfiltered (gdb_stdlog,
13234 "Read die from %s@0x%x of %s:\n",
13235 bfd_section_name (reader->abfd,
13236 reader->die_section->asection),
13237 (unsigned) (info_ptr - reader->die_section->buffer),
13238 bfd_get_filename (reader->abfd));
13239 dump_die (*diep, dwarf2_die_debug);
13240 }
13241
13242 return result;
13243 }
13244 \f
13245 /* Abbreviation tables.
13246
13247 In DWARF version 2, the description of the debugging information is
13248 stored in a separate .debug_abbrev section. Before we read any
13249 dies from a section we read in all abbreviations and install them
13250 in a hash table. */
13251
13252 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
13253
13254 static struct abbrev_info *
13255 abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
13256 {
13257 struct abbrev_info *abbrev;
13258
13259 abbrev = (struct abbrev_info *)
13260 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
13261 memset (abbrev, 0, sizeof (struct abbrev_info));
13262 return abbrev;
13263 }
13264
13265 /* Add an abbreviation to the table. */
13266
13267 static void
13268 abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
13269 unsigned int abbrev_number,
13270 struct abbrev_info *abbrev)
13271 {
13272 unsigned int hash_number;
13273
13274 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13275 abbrev->next = abbrev_table->abbrevs[hash_number];
13276 abbrev_table->abbrevs[hash_number] = abbrev;
13277 }
13278
13279 /* Look up an abbrev in the table.
13280 Returns NULL if the abbrev is not found. */
13281
13282 static struct abbrev_info *
13283 abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
13284 unsigned int abbrev_number)
13285 {
13286 unsigned int hash_number;
13287 struct abbrev_info *abbrev;
13288
13289 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13290 abbrev = abbrev_table->abbrevs[hash_number];
13291
13292 while (abbrev)
13293 {
13294 if (abbrev->number == abbrev_number)
13295 return abbrev;
13296 abbrev = abbrev->next;
13297 }
13298 return NULL;
13299 }
13300
13301 /* Read in an abbrev table. */
13302
13303 static struct abbrev_table *
13304 abbrev_table_read_table (struct dwarf2_section_info *section,
13305 sect_offset offset)
13306 {
13307 struct objfile *objfile = dwarf2_per_objfile->objfile;
13308 bfd *abfd = section->asection->owner;
13309 struct abbrev_table *abbrev_table;
13310 gdb_byte *abbrev_ptr;
13311 struct abbrev_info *cur_abbrev;
13312 unsigned int abbrev_number, bytes_read, abbrev_name;
13313 unsigned int abbrev_form;
13314 struct attr_abbrev *cur_attrs;
13315 unsigned int allocated_attrs;
13316
13317 abbrev_table = XMALLOC (struct abbrev_table);
13318 abbrev_table->offset = offset;
13319 obstack_init (&abbrev_table->abbrev_obstack);
13320 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
13321 (ABBREV_HASH_SIZE
13322 * sizeof (struct abbrev_info *)));
13323 memset (abbrev_table->abbrevs, 0,
13324 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
13325
13326 dwarf2_read_section (objfile, section);
13327 abbrev_ptr = section->buffer + offset.sect_off;
13328 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13329 abbrev_ptr += bytes_read;
13330
13331 allocated_attrs = ATTR_ALLOC_CHUNK;
13332 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
13333
13334 /* Loop until we reach an abbrev number of 0. */
13335 while (abbrev_number)
13336 {
13337 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
13338
13339 /* read in abbrev header */
13340 cur_abbrev->number = abbrev_number;
13341 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13342 abbrev_ptr += bytes_read;
13343 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
13344 abbrev_ptr += 1;
13345
13346 /* now read in declarations */
13347 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13348 abbrev_ptr += bytes_read;
13349 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13350 abbrev_ptr += bytes_read;
13351 while (abbrev_name)
13352 {
13353 if (cur_abbrev->num_attrs == allocated_attrs)
13354 {
13355 allocated_attrs += ATTR_ALLOC_CHUNK;
13356 cur_attrs
13357 = xrealloc (cur_attrs, (allocated_attrs
13358 * sizeof (struct attr_abbrev)));
13359 }
13360
13361 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
13362 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
13363 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13364 abbrev_ptr += bytes_read;
13365 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13366 abbrev_ptr += bytes_read;
13367 }
13368
13369 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
13370 (cur_abbrev->num_attrs
13371 * sizeof (struct attr_abbrev)));
13372 memcpy (cur_abbrev->attrs, cur_attrs,
13373 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
13374
13375 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
13376
13377 /* Get next abbreviation.
13378 Under Irix6 the abbreviations for a compilation unit are not
13379 always properly terminated with an abbrev number of 0.
13380 Exit loop if we encounter an abbreviation which we have
13381 already read (which means we are about to read the abbreviations
13382 for the next compile unit) or if the end of the abbreviation
13383 table is reached. */
13384 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
13385 break;
13386 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13387 abbrev_ptr += bytes_read;
13388 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
13389 break;
13390 }
13391
13392 xfree (cur_attrs);
13393 return abbrev_table;
13394 }
13395
13396 /* Free the resources held by ABBREV_TABLE. */
13397
13398 static void
13399 abbrev_table_free (struct abbrev_table *abbrev_table)
13400 {
13401 obstack_free (&abbrev_table->abbrev_obstack, NULL);
13402 xfree (abbrev_table);
13403 }
13404
13405 /* Same as abbrev_table_free but as a cleanup.
13406 We pass in a pointer to the pointer to the table so that we can
13407 set the pointer to NULL when we're done. It also simplifies
13408 build_type_unit_groups. */
13409
13410 static void
13411 abbrev_table_free_cleanup (void *table_ptr)
13412 {
13413 struct abbrev_table **abbrev_table_ptr = table_ptr;
13414
13415 if (*abbrev_table_ptr != NULL)
13416 abbrev_table_free (*abbrev_table_ptr);
13417 *abbrev_table_ptr = NULL;
13418 }
13419
13420 /* Read the abbrev table for CU from ABBREV_SECTION. */
13421
13422 static void
13423 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
13424 struct dwarf2_section_info *abbrev_section)
13425 {
13426 cu->abbrev_table =
13427 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
13428 }
13429
13430 /* Release the memory used by the abbrev table for a compilation unit. */
13431
13432 static void
13433 dwarf2_free_abbrev_table (void *ptr_to_cu)
13434 {
13435 struct dwarf2_cu *cu = ptr_to_cu;
13436
13437 abbrev_table_free (cu->abbrev_table);
13438 /* Set this to NULL so that we SEGV if we try to read it later,
13439 and also because free_comp_unit verifies this is NULL. */
13440 cu->abbrev_table = NULL;
13441 }
13442 \f
13443 /* Returns nonzero if TAG represents a type that we might generate a partial
13444 symbol for. */
13445
13446 static int
13447 is_type_tag_for_partial (int tag)
13448 {
13449 switch (tag)
13450 {
13451 #if 0
13452 /* Some types that would be reasonable to generate partial symbols for,
13453 that we don't at present. */
13454 case DW_TAG_array_type:
13455 case DW_TAG_file_type:
13456 case DW_TAG_ptr_to_member_type:
13457 case DW_TAG_set_type:
13458 case DW_TAG_string_type:
13459 case DW_TAG_subroutine_type:
13460 #endif
13461 case DW_TAG_base_type:
13462 case DW_TAG_class_type:
13463 case DW_TAG_interface_type:
13464 case DW_TAG_enumeration_type:
13465 case DW_TAG_structure_type:
13466 case DW_TAG_subrange_type:
13467 case DW_TAG_typedef:
13468 case DW_TAG_union_type:
13469 return 1;
13470 default:
13471 return 0;
13472 }
13473 }
13474
13475 /* Load all DIEs that are interesting for partial symbols into memory. */
13476
13477 static struct partial_die_info *
13478 load_partial_dies (const struct die_reader_specs *reader,
13479 gdb_byte *info_ptr, int building_psymtab)
13480 {
13481 struct dwarf2_cu *cu = reader->cu;
13482 struct objfile *objfile = cu->objfile;
13483 struct partial_die_info *part_die;
13484 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
13485 struct abbrev_info *abbrev;
13486 unsigned int bytes_read;
13487 unsigned int load_all = 0;
13488 int nesting_level = 1;
13489
13490 parent_die = NULL;
13491 last_die = NULL;
13492
13493 gdb_assert (cu->per_cu != NULL);
13494 if (cu->per_cu->load_all_dies)
13495 load_all = 1;
13496
13497 cu->partial_dies
13498 = htab_create_alloc_ex (cu->header.length / 12,
13499 partial_die_hash,
13500 partial_die_eq,
13501 NULL,
13502 &cu->comp_unit_obstack,
13503 hashtab_obstack_allocate,
13504 dummy_obstack_deallocate);
13505
13506 part_die = obstack_alloc (&cu->comp_unit_obstack,
13507 sizeof (struct partial_die_info));
13508
13509 while (1)
13510 {
13511 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
13512
13513 /* A NULL abbrev means the end of a series of children. */
13514 if (abbrev == NULL)
13515 {
13516 if (--nesting_level == 0)
13517 {
13518 /* PART_DIE was probably the last thing allocated on the
13519 comp_unit_obstack, so we could call obstack_free
13520 here. We don't do that because the waste is small,
13521 and will be cleaned up when we're done with this
13522 compilation unit. This way, we're also more robust
13523 against other users of the comp_unit_obstack. */
13524 return first_die;
13525 }
13526 info_ptr += bytes_read;
13527 last_die = parent_die;
13528 parent_die = parent_die->die_parent;
13529 continue;
13530 }
13531
13532 /* Check for template arguments. We never save these; if
13533 they're seen, we just mark the parent, and go on our way. */
13534 if (parent_die != NULL
13535 && cu->language == language_cplus
13536 && (abbrev->tag == DW_TAG_template_type_param
13537 || abbrev->tag == DW_TAG_template_value_param))
13538 {
13539 parent_die->has_template_arguments = 1;
13540
13541 if (!load_all)
13542 {
13543 /* We don't need a partial DIE for the template argument. */
13544 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
13545 continue;
13546 }
13547 }
13548
13549 /* We only recurse into c++ subprograms looking for template arguments.
13550 Skip their other children. */
13551 if (!load_all
13552 && cu->language == language_cplus
13553 && parent_die != NULL
13554 && parent_die->tag == DW_TAG_subprogram)
13555 {
13556 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
13557 continue;
13558 }
13559
13560 /* Check whether this DIE is interesting enough to save. Normally
13561 we would not be interested in members here, but there may be
13562 later variables referencing them via DW_AT_specification (for
13563 static members). */
13564 if (!load_all
13565 && !is_type_tag_for_partial (abbrev->tag)
13566 && abbrev->tag != DW_TAG_constant
13567 && abbrev->tag != DW_TAG_enumerator
13568 && abbrev->tag != DW_TAG_subprogram
13569 && abbrev->tag != DW_TAG_lexical_block
13570 && abbrev->tag != DW_TAG_variable
13571 && abbrev->tag != DW_TAG_namespace
13572 && abbrev->tag != DW_TAG_module
13573 && abbrev->tag != DW_TAG_member
13574 && abbrev->tag != DW_TAG_imported_unit)
13575 {
13576 /* Otherwise we skip to the next sibling, if any. */
13577 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
13578 continue;
13579 }
13580
13581 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
13582 info_ptr);
13583
13584 /* This two-pass algorithm for processing partial symbols has a
13585 high cost in cache pressure. Thus, handle some simple cases
13586 here which cover the majority of C partial symbols. DIEs
13587 which neither have specification tags in them, nor could have
13588 specification tags elsewhere pointing at them, can simply be
13589 processed and discarded.
13590
13591 This segment is also optional; scan_partial_symbols and
13592 add_partial_symbol will handle these DIEs if we chain
13593 them in normally. When compilers which do not emit large
13594 quantities of duplicate debug information are more common,
13595 this code can probably be removed. */
13596
13597 /* Any complete simple types at the top level (pretty much all
13598 of them, for a language without namespaces), can be processed
13599 directly. */
13600 if (parent_die == NULL
13601 && part_die->has_specification == 0
13602 && part_die->is_declaration == 0
13603 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
13604 || part_die->tag == DW_TAG_base_type
13605 || part_die->tag == DW_TAG_subrange_type))
13606 {
13607 if (building_psymtab && part_die->name != NULL)
13608 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
13609 VAR_DOMAIN, LOC_TYPEDEF,
13610 &objfile->static_psymbols,
13611 0, (CORE_ADDR) 0, cu->language, objfile);
13612 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
13613 continue;
13614 }
13615
13616 /* The exception for DW_TAG_typedef with has_children above is
13617 a workaround of GCC PR debug/47510. In the case of this complaint
13618 type_name_no_tag_or_error will error on such types later.
13619
13620 GDB skipped children of DW_TAG_typedef by the shortcut above and then
13621 it could not find the child DIEs referenced later, this is checked
13622 above. In correct DWARF DW_TAG_typedef should have no children. */
13623
13624 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
13625 complaint (&symfile_complaints,
13626 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
13627 "- DIE at 0x%x [in module %s]"),
13628 part_die->offset.sect_off, objfile->name);
13629
13630 /* If we're at the second level, and we're an enumerator, and
13631 our parent has no specification (meaning possibly lives in a
13632 namespace elsewhere), then we can add the partial symbol now
13633 instead of queueing it. */
13634 if (part_die->tag == DW_TAG_enumerator
13635 && parent_die != NULL
13636 && parent_die->die_parent == NULL
13637 && parent_die->tag == DW_TAG_enumeration_type
13638 && parent_die->has_specification == 0)
13639 {
13640 if (part_die->name == NULL)
13641 complaint (&symfile_complaints,
13642 _("malformed enumerator DIE ignored"));
13643 else if (building_psymtab)
13644 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
13645 VAR_DOMAIN, LOC_CONST,
13646 (cu->language == language_cplus
13647 || cu->language == language_java)
13648 ? &objfile->global_psymbols
13649 : &objfile->static_psymbols,
13650 0, (CORE_ADDR) 0, cu->language, objfile);
13651
13652 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
13653 continue;
13654 }
13655
13656 /* We'll save this DIE so link it in. */
13657 part_die->die_parent = parent_die;
13658 part_die->die_sibling = NULL;
13659 part_die->die_child = NULL;
13660
13661 if (last_die && last_die == parent_die)
13662 last_die->die_child = part_die;
13663 else if (last_die)
13664 last_die->die_sibling = part_die;
13665
13666 last_die = part_die;
13667
13668 if (first_die == NULL)
13669 first_die = part_die;
13670
13671 /* Maybe add the DIE to the hash table. Not all DIEs that we
13672 find interesting need to be in the hash table, because we
13673 also have the parent/sibling/child chains; only those that we
13674 might refer to by offset later during partial symbol reading.
13675
13676 For now this means things that might have be the target of a
13677 DW_AT_specification, DW_AT_abstract_origin, or
13678 DW_AT_extension. DW_AT_extension will refer only to
13679 namespaces; DW_AT_abstract_origin refers to functions (and
13680 many things under the function DIE, but we do not recurse
13681 into function DIEs during partial symbol reading) and
13682 possibly variables as well; DW_AT_specification refers to
13683 declarations. Declarations ought to have the DW_AT_declaration
13684 flag. It happens that GCC forgets to put it in sometimes, but
13685 only for functions, not for types.
13686
13687 Adding more things than necessary to the hash table is harmless
13688 except for the performance cost. Adding too few will result in
13689 wasted time in find_partial_die, when we reread the compilation
13690 unit with load_all_dies set. */
13691
13692 if (load_all
13693 || abbrev->tag == DW_TAG_constant
13694 || abbrev->tag == DW_TAG_subprogram
13695 || abbrev->tag == DW_TAG_variable
13696 || abbrev->tag == DW_TAG_namespace
13697 || part_die->is_declaration)
13698 {
13699 void **slot;
13700
13701 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
13702 part_die->offset.sect_off, INSERT);
13703 *slot = part_die;
13704 }
13705
13706 part_die = obstack_alloc (&cu->comp_unit_obstack,
13707 sizeof (struct partial_die_info));
13708
13709 /* For some DIEs we want to follow their children (if any). For C
13710 we have no reason to follow the children of structures; for other
13711 languages we have to, so that we can get at method physnames
13712 to infer fully qualified class names, for DW_AT_specification,
13713 and for C++ template arguments. For C++, we also look one level
13714 inside functions to find template arguments (if the name of the
13715 function does not already contain the template arguments).
13716
13717 For Ada, we need to scan the children of subprograms and lexical
13718 blocks as well because Ada allows the definition of nested
13719 entities that could be interesting for the debugger, such as
13720 nested subprograms for instance. */
13721 if (last_die->has_children
13722 && (load_all
13723 || last_die->tag == DW_TAG_namespace
13724 || last_die->tag == DW_TAG_module
13725 || last_die->tag == DW_TAG_enumeration_type
13726 || (cu->language == language_cplus
13727 && last_die->tag == DW_TAG_subprogram
13728 && (last_die->name == NULL
13729 || strchr (last_die->name, '<') == NULL))
13730 || (cu->language != language_c
13731 && (last_die->tag == DW_TAG_class_type
13732 || last_die->tag == DW_TAG_interface_type
13733 || last_die->tag == DW_TAG_structure_type
13734 || last_die->tag == DW_TAG_union_type))
13735 || (cu->language == language_ada
13736 && (last_die->tag == DW_TAG_subprogram
13737 || last_die->tag == DW_TAG_lexical_block))))
13738 {
13739 nesting_level++;
13740 parent_die = last_die;
13741 continue;
13742 }
13743
13744 /* Otherwise we skip to the next sibling, if any. */
13745 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
13746
13747 /* Back to the top, do it again. */
13748 }
13749 }
13750
13751 /* Read a minimal amount of information into the minimal die structure. */
13752
13753 static gdb_byte *
13754 read_partial_die (const struct die_reader_specs *reader,
13755 struct partial_die_info *part_die,
13756 struct abbrev_info *abbrev, unsigned int abbrev_len,
13757 gdb_byte *info_ptr)
13758 {
13759 struct dwarf2_cu *cu = reader->cu;
13760 struct objfile *objfile = cu->objfile;
13761 gdb_byte *buffer = reader->buffer;
13762 unsigned int i;
13763 struct attribute attr;
13764 int has_low_pc_attr = 0;
13765 int has_high_pc_attr = 0;
13766 int high_pc_relative = 0;
13767
13768 memset (part_die, 0, sizeof (struct partial_die_info));
13769
13770 part_die->offset.sect_off = info_ptr - buffer;
13771
13772 info_ptr += abbrev_len;
13773
13774 if (abbrev == NULL)
13775 return info_ptr;
13776
13777 part_die->tag = abbrev->tag;
13778 part_die->has_children = abbrev->has_children;
13779
13780 for (i = 0; i < abbrev->num_attrs; ++i)
13781 {
13782 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
13783
13784 /* Store the data if it is of an attribute we want to keep in a
13785 partial symbol table. */
13786 switch (attr.name)
13787 {
13788 case DW_AT_name:
13789 switch (part_die->tag)
13790 {
13791 case DW_TAG_compile_unit:
13792 case DW_TAG_partial_unit:
13793 case DW_TAG_type_unit:
13794 /* Compilation units have a DW_AT_name that is a filename, not
13795 a source language identifier. */
13796 case DW_TAG_enumeration_type:
13797 case DW_TAG_enumerator:
13798 /* These tags always have simple identifiers already; no need
13799 to canonicalize them. */
13800 part_die->name = DW_STRING (&attr);
13801 break;
13802 default:
13803 part_die->name
13804 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
13805 &objfile->objfile_obstack);
13806 break;
13807 }
13808 break;
13809 case DW_AT_linkage_name:
13810 case DW_AT_MIPS_linkage_name:
13811 /* Note that both forms of linkage name might appear. We
13812 assume they will be the same, and we only store the last
13813 one we see. */
13814 if (cu->language == language_ada)
13815 part_die->name = DW_STRING (&attr);
13816 part_die->linkage_name = DW_STRING (&attr);
13817 break;
13818 case DW_AT_low_pc:
13819 has_low_pc_attr = 1;
13820 part_die->lowpc = DW_ADDR (&attr);
13821 break;
13822 case DW_AT_high_pc:
13823 has_high_pc_attr = 1;
13824 if (attr.form == DW_FORM_addr
13825 || attr.form == DW_FORM_GNU_addr_index)
13826 part_die->highpc = DW_ADDR (&attr);
13827 else
13828 {
13829 high_pc_relative = 1;
13830 part_die->highpc = DW_UNSND (&attr);
13831 }
13832 break;
13833 case DW_AT_location:
13834 /* Support the .debug_loc offsets. */
13835 if (attr_form_is_block (&attr))
13836 {
13837 part_die->d.locdesc = DW_BLOCK (&attr);
13838 }
13839 else if (attr_form_is_section_offset (&attr))
13840 {
13841 dwarf2_complex_location_expr_complaint ();
13842 }
13843 else
13844 {
13845 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
13846 "partial symbol information");
13847 }
13848 break;
13849 case DW_AT_external:
13850 part_die->is_external = DW_UNSND (&attr);
13851 break;
13852 case DW_AT_declaration:
13853 part_die->is_declaration = DW_UNSND (&attr);
13854 break;
13855 case DW_AT_type:
13856 part_die->has_type = 1;
13857 break;
13858 case DW_AT_abstract_origin:
13859 case DW_AT_specification:
13860 case DW_AT_extension:
13861 part_die->has_specification = 1;
13862 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
13863 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13864 || cu->per_cu->is_dwz);
13865 break;
13866 case DW_AT_sibling:
13867 /* Ignore absolute siblings, they might point outside of
13868 the current compile unit. */
13869 if (attr.form == DW_FORM_ref_addr)
13870 complaint (&symfile_complaints,
13871 _("ignoring absolute DW_AT_sibling"));
13872 else
13873 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
13874 break;
13875 case DW_AT_byte_size:
13876 part_die->has_byte_size = 1;
13877 break;
13878 case DW_AT_calling_convention:
13879 /* DWARF doesn't provide a way to identify a program's source-level
13880 entry point. DW_AT_calling_convention attributes are only meant
13881 to describe functions' calling conventions.
13882
13883 However, because it's a necessary piece of information in
13884 Fortran, and because DW_CC_program is the only piece of debugging
13885 information whose definition refers to a 'main program' at all,
13886 several compilers have begun marking Fortran main programs with
13887 DW_CC_program --- even when those functions use the standard
13888 calling conventions.
13889
13890 So until DWARF specifies a way to provide this information and
13891 compilers pick up the new representation, we'll support this
13892 practice. */
13893 if (DW_UNSND (&attr) == DW_CC_program
13894 && cu->language == language_fortran)
13895 {
13896 set_main_name (part_die->name);
13897
13898 /* As this DIE has a static linkage the name would be difficult
13899 to look up later. */
13900 language_of_main = language_fortran;
13901 }
13902 break;
13903 case DW_AT_inline:
13904 if (DW_UNSND (&attr) == DW_INL_inlined
13905 || DW_UNSND (&attr) == DW_INL_declared_inlined)
13906 part_die->may_be_inlined = 1;
13907 break;
13908
13909 case DW_AT_import:
13910 if (part_die->tag == DW_TAG_imported_unit)
13911 {
13912 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
13913 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13914 || cu->per_cu->is_dwz);
13915 }
13916 break;
13917
13918 default:
13919 break;
13920 }
13921 }
13922
13923 if (high_pc_relative)
13924 part_die->highpc += part_die->lowpc;
13925
13926 if (has_low_pc_attr && has_high_pc_attr)
13927 {
13928 /* When using the GNU linker, .gnu.linkonce. sections are used to
13929 eliminate duplicate copies of functions and vtables and such.
13930 The linker will arbitrarily choose one and discard the others.
13931 The AT_*_pc values for such functions refer to local labels in
13932 these sections. If the section from that file was discarded, the
13933 labels are not in the output, so the relocs get a value of 0.
13934 If this is a discarded function, mark the pc bounds as invalid,
13935 so that GDB will ignore it. */
13936 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
13937 {
13938 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13939
13940 complaint (&symfile_complaints,
13941 _("DW_AT_low_pc %s is zero "
13942 "for DIE at 0x%x [in module %s]"),
13943 paddress (gdbarch, part_die->lowpc),
13944 part_die->offset.sect_off, objfile->name);
13945 }
13946 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
13947 else if (part_die->lowpc >= part_die->highpc)
13948 {
13949 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13950
13951 complaint (&symfile_complaints,
13952 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
13953 "for DIE at 0x%x [in module %s]"),
13954 paddress (gdbarch, part_die->lowpc),
13955 paddress (gdbarch, part_die->highpc),
13956 part_die->offset.sect_off, objfile->name);
13957 }
13958 else
13959 part_die->has_pc_info = 1;
13960 }
13961
13962 return info_ptr;
13963 }
13964
13965 /* Find a cached partial DIE at OFFSET in CU. */
13966
13967 static struct partial_die_info *
13968 find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
13969 {
13970 struct partial_die_info *lookup_die = NULL;
13971 struct partial_die_info part_die;
13972
13973 part_die.offset = offset;
13974 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
13975 offset.sect_off);
13976
13977 return lookup_die;
13978 }
13979
13980 /* Find a partial DIE at OFFSET, which may or may not be in CU,
13981 except in the case of .debug_types DIEs which do not reference
13982 outside their CU (they do however referencing other types via
13983 DW_FORM_ref_sig8). */
13984
13985 static struct partial_die_info *
13986 find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
13987 {
13988 struct objfile *objfile = cu->objfile;
13989 struct dwarf2_per_cu_data *per_cu = NULL;
13990 struct partial_die_info *pd = NULL;
13991
13992 if (offset_in_dwz == cu->per_cu->is_dwz
13993 && offset_in_cu_p (&cu->header, offset))
13994 {
13995 pd = find_partial_die_in_comp_unit (offset, cu);
13996 if (pd != NULL)
13997 return pd;
13998 /* We missed recording what we needed.
13999 Load all dies and try again. */
14000 per_cu = cu->per_cu;
14001 }
14002 else
14003 {
14004 /* TUs don't reference other CUs/TUs (except via type signatures). */
14005 if (cu->per_cu->is_debug_types)
14006 {
14007 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
14008 " external reference to offset 0x%lx [in module %s].\n"),
14009 (long) cu->header.offset.sect_off, (long) offset.sect_off,
14010 bfd_get_filename (objfile->obfd));
14011 }
14012 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
14013 objfile);
14014
14015 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
14016 load_partial_comp_unit (per_cu);
14017
14018 per_cu->cu->last_used = 0;
14019 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
14020 }
14021
14022 /* If we didn't find it, and not all dies have been loaded,
14023 load them all and try again. */
14024
14025 if (pd == NULL && per_cu->load_all_dies == 0)
14026 {
14027 per_cu->load_all_dies = 1;
14028
14029 /* This is nasty. When we reread the DIEs, somewhere up the call chain
14030 THIS_CU->cu may already be in use. So we can't just free it and
14031 replace its DIEs with the ones we read in. Instead, we leave those
14032 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
14033 and clobber THIS_CU->cu->partial_dies with the hash table for the new
14034 set. */
14035 load_partial_comp_unit (per_cu);
14036
14037 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
14038 }
14039
14040 if (pd == NULL)
14041 internal_error (__FILE__, __LINE__,
14042 _("could not find partial DIE 0x%x "
14043 "in cache [from module %s]\n"),
14044 offset.sect_off, bfd_get_filename (objfile->obfd));
14045 return pd;
14046 }
14047
14048 /* See if we can figure out if the class lives in a namespace. We do
14049 this by looking for a member function; its demangled name will
14050 contain namespace info, if there is any. */
14051
14052 static void
14053 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
14054 struct dwarf2_cu *cu)
14055 {
14056 /* NOTE: carlton/2003-10-07: Getting the info this way changes
14057 what template types look like, because the demangler
14058 frequently doesn't give the same name as the debug info. We
14059 could fix this by only using the demangled name to get the
14060 prefix (but see comment in read_structure_type). */
14061
14062 struct partial_die_info *real_pdi;
14063 struct partial_die_info *child_pdi;
14064
14065 /* If this DIE (this DIE's specification, if any) has a parent, then
14066 we should not do this. We'll prepend the parent's fully qualified
14067 name when we create the partial symbol. */
14068
14069 real_pdi = struct_pdi;
14070 while (real_pdi->has_specification)
14071 real_pdi = find_partial_die (real_pdi->spec_offset,
14072 real_pdi->spec_is_dwz, cu);
14073
14074 if (real_pdi->die_parent != NULL)
14075 return;
14076
14077 for (child_pdi = struct_pdi->die_child;
14078 child_pdi != NULL;
14079 child_pdi = child_pdi->die_sibling)
14080 {
14081 if (child_pdi->tag == DW_TAG_subprogram
14082 && child_pdi->linkage_name != NULL)
14083 {
14084 char *actual_class_name
14085 = language_class_name_from_physname (cu->language_defn,
14086 child_pdi->linkage_name);
14087 if (actual_class_name != NULL)
14088 {
14089 struct_pdi->name
14090 = obstack_copy0 (&cu->objfile->objfile_obstack,
14091 actual_class_name,
14092 strlen (actual_class_name));
14093 xfree (actual_class_name);
14094 }
14095 break;
14096 }
14097 }
14098 }
14099
14100 /* Adjust PART_DIE before generating a symbol for it. This function
14101 may set the is_external flag or change the DIE's name. */
14102
14103 static void
14104 fixup_partial_die (struct partial_die_info *part_die,
14105 struct dwarf2_cu *cu)
14106 {
14107 /* Once we've fixed up a die, there's no point in doing so again.
14108 This also avoids a memory leak if we were to call
14109 guess_partial_die_structure_name multiple times. */
14110 if (part_die->fixup_called)
14111 return;
14112
14113 /* If we found a reference attribute and the DIE has no name, try
14114 to find a name in the referred to DIE. */
14115
14116 if (part_die->name == NULL && part_die->has_specification)
14117 {
14118 struct partial_die_info *spec_die;
14119
14120 spec_die = find_partial_die (part_die->spec_offset,
14121 part_die->spec_is_dwz, cu);
14122
14123 fixup_partial_die (spec_die, cu);
14124
14125 if (spec_die->name)
14126 {
14127 part_die->name = spec_die->name;
14128
14129 /* Copy DW_AT_external attribute if it is set. */
14130 if (spec_die->is_external)
14131 part_die->is_external = spec_die->is_external;
14132 }
14133 }
14134
14135 /* Set default names for some unnamed DIEs. */
14136
14137 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
14138 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
14139
14140 /* If there is no parent die to provide a namespace, and there are
14141 children, see if we can determine the namespace from their linkage
14142 name. */
14143 if (cu->language == language_cplus
14144 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
14145 && part_die->die_parent == NULL
14146 && part_die->has_children
14147 && (part_die->tag == DW_TAG_class_type
14148 || part_die->tag == DW_TAG_structure_type
14149 || part_die->tag == DW_TAG_union_type))
14150 guess_partial_die_structure_name (part_die, cu);
14151
14152 /* GCC might emit a nameless struct or union that has a linkage
14153 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14154 if (part_die->name == NULL
14155 && (part_die->tag == DW_TAG_class_type
14156 || part_die->tag == DW_TAG_interface_type
14157 || part_die->tag == DW_TAG_structure_type
14158 || part_die->tag == DW_TAG_union_type)
14159 && part_die->linkage_name != NULL)
14160 {
14161 char *demangled;
14162
14163 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
14164 if (demangled)
14165 {
14166 const char *base;
14167
14168 /* Strip any leading namespaces/classes, keep only the base name.
14169 DW_AT_name for named DIEs does not contain the prefixes. */
14170 base = strrchr (demangled, ':');
14171 if (base && base > demangled && base[-1] == ':')
14172 base++;
14173 else
14174 base = demangled;
14175
14176 part_die->name = obstack_copy0 (&cu->objfile->objfile_obstack,
14177 base, strlen (base));
14178 xfree (demangled);
14179 }
14180 }
14181
14182 part_die->fixup_called = 1;
14183 }
14184
14185 /* Read an attribute value described by an attribute form. */
14186
14187 static gdb_byte *
14188 read_attribute_value (const struct die_reader_specs *reader,
14189 struct attribute *attr, unsigned form,
14190 gdb_byte *info_ptr)
14191 {
14192 struct dwarf2_cu *cu = reader->cu;
14193 bfd *abfd = reader->abfd;
14194 struct comp_unit_head *cu_header = &cu->header;
14195 unsigned int bytes_read;
14196 struct dwarf_block *blk;
14197
14198 attr->form = form;
14199 switch (form)
14200 {
14201 case DW_FORM_ref_addr:
14202 if (cu->header.version == 2)
14203 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
14204 else
14205 DW_UNSND (attr) = read_offset (abfd, info_ptr,
14206 &cu->header, &bytes_read);
14207 info_ptr += bytes_read;
14208 break;
14209 case DW_FORM_GNU_ref_alt:
14210 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14211 info_ptr += bytes_read;
14212 break;
14213 case DW_FORM_addr:
14214 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
14215 info_ptr += bytes_read;
14216 break;
14217 case DW_FORM_block2:
14218 blk = dwarf_alloc_block (cu);
14219 blk->size = read_2_bytes (abfd, info_ptr);
14220 info_ptr += 2;
14221 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14222 info_ptr += blk->size;
14223 DW_BLOCK (attr) = blk;
14224 break;
14225 case DW_FORM_block4:
14226 blk = dwarf_alloc_block (cu);
14227 blk->size = read_4_bytes (abfd, info_ptr);
14228 info_ptr += 4;
14229 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14230 info_ptr += blk->size;
14231 DW_BLOCK (attr) = blk;
14232 break;
14233 case DW_FORM_data2:
14234 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
14235 info_ptr += 2;
14236 break;
14237 case DW_FORM_data4:
14238 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
14239 info_ptr += 4;
14240 break;
14241 case DW_FORM_data8:
14242 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
14243 info_ptr += 8;
14244 break;
14245 case DW_FORM_sec_offset:
14246 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14247 info_ptr += bytes_read;
14248 break;
14249 case DW_FORM_string:
14250 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
14251 DW_STRING_IS_CANONICAL (attr) = 0;
14252 info_ptr += bytes_read;
14253 break;
14254 case DW_FORM_strp:
14255 if (!cu->per_cu->is_dwz)
14256 {
14257 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
14258 &bytes_read);
14259 DW_STRING_IS_CANONICAL (attr) = 0;
14260 info_ptr += bytes_read;
14261 break;
14262 }
14263 /* FALLTHROUGH */
14264 case DW_FORM_GNU_strp_alt:
14265 {
14266 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14267 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
14268 &bytes_read);
14269
14270 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
14271 DW_STRING_IS_CANONICAL (attr) = 0;
14272 info_ptr += bytes_read;
14273 }
14274 break;
14275 case DW_FORM_exprloc:
14276 case DW_FORM_block:
14277 blk = dwarf_alloc_block (cu);
14278 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14279 info_ptr += bytes_read;
14280 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14281 info_ptr += blk->size;
14282 DW_BLOCK (attr) = blk;
14283 break;
14284 case DW_FORM_block1:
14285 blk = dwarf_alloc_block (cu);
14286 blk->size = read_1_byte (abfd, info_ptr);
14287 info_ptr += 1;
14288 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14289 info_ptr += blk->size;
14290 DW_BLOCK (attr) = blk;
14291 break;
14292 case DW_FORM_data1:
14293 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14294 info_ptr += 1;
14295 break;
14296 case DW_FORM_flag:
14297 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14298 info_ptr += 1;
14299 break;
14300 case DW_FORM_flag_present:
14301 DW_UNSND (attr) = 1;
14302 break;
14303 case DW_FORM_sdata:
14304 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
14305 info_ptr += bytes_read;
14306 break;
14307 case DW_FORM_udata:
14308 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14309 info_ptr += bytes_read;
14310 break;
14311 case DW_FORM_ref1:
14312 DW_UNSND (attr) = (cu->header.offset.sect_off
14313 + read_1_byte (abfd, info_ptr));
14314 info_ptr += 1;
14315 break;
14316 case DW_FORM_ref2:
14317 DW_UNSND (attr) = (cu->header.offset.sect_off
14318 + read_2_bytes (abfd, info_ptr));
14319 info_ptr += 2;
14320 break;
14321 case DW_FORM_ref4:
14322 DW_UNSND (attr) = (cu->header.offset.sect_off
14323 + read_4_bytes (abfd, info_ptr));
14324 info_ptr += 4;
14325 break;
14326 case DW_FORM_ref8:
14327 DW_UNSND (attr) = (cu->header.offset.sect_off
14328 + read_8_bytes (abfd, info_ptr));
14329 info_ptr += 8;
14330 break;
14331 case DW_FORM_ref_sig8:
14332 /* Convert the signature to something we can record in DW_UNSND
14333 for later lookup.
14334 NOTE: This is NULL if the type wasn't found. */
14335 DW_SIGNATURED_TYPE (attr) =
14336 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
14337 info_ptr += 8;
14338 break;
14339 case DW_FORM_ref_udata:
14340 DW_UNSND (attr) = (cu->header.offset.sect_off
14341 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
14342 info_ptr += bytes_read;
14343 break;
14344 case DW_FORM_indirect:
14345 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14346 info_ptr += bytes_read;
14347 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
14348 break;
14349 case DW_FORM_GNU_addr_index:
14350 if (reader->dwo_file == NULL)
14351 {
14352 /* For now flag a hard error.
14353 Later we can turn this into a complaint. */
14354 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14355 dwarf_form_name (form),
14356 bfd_get_filename (abfd));
14357 }
14358 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
14359 info_ptr += bytes_read;
14360 break;
14361 case DW_FORM_GNU_str_index:
14362 if (reader->dwo_file == NULL)
14363 {
14364 /* For now flag a hard error.
14365 Later we can turn this into a complaint if warranted. */
14366 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14367 dwarf_form_name (form),
14368 bfd_get_filename (abfd));
14369 }
14370 {
14371 ULONGEST str_index =
14372 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14373
14374 DW_STRING (attr) = read_str_index (reader, cu, str_index);
14375 DW_STRING_IS_CANONICAL (attr) = 0;
14376 info_ptr += bytes_read;
14377 }
14378 break;
14379 default:
14380 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
14381 dwarf_form_name (form),
14382 bfd_get_filename (abfd));
14383 }
14384
14385 /* Super hack. */
14386 if (cu->per_cu->is_dwz && is_ref_attr (attr))
14387 attr->form = DW_FORM_GNU_ref_alt;
14388
14389 /* We have seen instances where the compiler tried to emit a byte
14390 size attribute of -1 which ended up being encoded as an unsigned
14391 0xffffffff. Although 0xffffffff is technically a valid size value,
14392 an object of this size seems pretty unlikely so we can relatively
14393 safely treat these cases as if the size attribute was invalid and
14394 treat them as zero by default. */
14395 if (attr->name == DW_AT_byte_size
14396 && form == DW_FORM_data4
14397 && DW_UNSND (attr) >= 0xffffffff)
14398 {
14399 complaint
14400 (&symfile_complaints,
14401 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
14402 hex_string (DW_UNSND (attr)));
14403 DW_UNSND (attr) = 0;
14404 }
14405
14406 return info_ptr;
14407 }
14408
14409 /* Read an attribute described by an abbreviated attribute. */
14410
14411 static gdb_byte *
14412 read_attribute (const struct die_reader_specs *reader,
14413 struct attribute *attr, struct attr_abbrev *abbrev,
14414 gdb_byte *info_ptr)
14415 {
14416 attr->name = abbrev->name;
14417 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
14418 }
14419
14420 /* Read dwarf information from a buffer. */
14421
14422 static unsigned int
14423 read_1_byte (bfd *abfd, const gdb_byte *buf)
14424 {
14425 return bfd_get_8 (abfd, buf);
14426 }
14427
14428 static int
14429 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
14430 {
14431 return bfd_get_signed_8 (abfd, buf);
14432 }
14433
14434 static unsigned int
14435 read_2_bytes (bfd *abfd, const gdb_byte *buf)
14436 {
14437 return bfd_get_16 (abfd, buf);
14438 }
14439
14440 static int
14441 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
14442 {
14443 return bfd_get_signed_16 (abfd, buf);
14444 }
14445
14446 static unsigned int
14447 read_4_bytes (bfd *abfd, const gdb_byte *buf)
14448 {
14449 return bfd_get_32 (abfd, buf);
14450 }
14451
14452 static int
14453 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
14454 {
14455 return bfd_get_signed_32 (abfd, buf);
14456 }
14457
14458 static ULONGEST
14459 read_8_bytes (bfd *abfd, const gdb_byte *buf)
14460 {
14461 return bfd_get_64 (abfd, buf);
14462 }
14463
14464 static CORE_ADDR
14465 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
14466 unsigned int *bytes_read)
14467 {
14468 struct comp_unit_head *cu_header = &cu->header;
14469 CORE_ADDR retval = 0;
14470
14471 if (cu_header->signed_addr_p)
14472 {
14473 switch (cu_header->addr_size)
14474 {
14475 case 2:
14476 retval = bfd_get_signed_16 (abfd, buf);
14477 break;
14478 case 4:
14479 retval = bfd_get_signed_32 (abfd, buf);
14480 break;
14481 case 8:
14482 retval = bfd_get_signed_64 (abfd, buf);
14483 break;
14484 default:
14485 internal_error (__FILE__, __LINE__,
14486 _("read_address: bad switch, signed [in module %s]"),
14487 bfd_get_filename (abfd));
14488 }
14489 }
14490 else
14491 {
14492 switch (cu_header->addr_size)
14493 {
14494 case 2:
14495 retval = bfd_get_16 (abfd, buf);
14496 break;
14497 case 4:
14498 retval = bfd_get_32 (abfd, buf);
14499 break;
14500 case 8:
14501 retval = bfd_get_64 (abfd, buf);
14502 break;
14503 default:
14504 internal_error (__FILE__, __LINE__,
14505 _("read_address: bad switch, "
14506 "unsigned [in module %s]"),
14507 bfd_get_filename (abfd));
14508 }
14509 }
14510
14511 *bytes_read = cu_header->addr_size;
14512 return retval;
14513 }
14514
14515 /* Read the initial length from a section. The (draft) DWARF 3
14516 specification allows the initial length to take up either 4 bytes
14517 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
14518 bytes describe the length and all offsets will be 8 bytes in length
14519 instead of 4.
14520
14521 An older, non-standard 64-bit format is also handled by this
14522 function. The older format in question stores the initial length
14523 as an 8-byte quantity without an escape value. Lengths greater
14524 than 2^32 aren't very common which means that the initial 4 bytes
14525 is almost always zero. Since a length value of zero doesn't make
14526 sense for the 32-bit format, this initial zero can be considered to
14527 be an escape value which indicates the presence of the older 64-bit
14528 format. As written, the code can't detect (old format) lengths
14529 greater than 4GB. If it becomes necessary to handle lengths
14530 somewhat larger than 4GB, we could allow other small values (such
14531 as the non-sensical values of 1, 2, and 3) to also be used as
14532 escape values indicating the presence of the old format.
14533
14534 The value returned via bytes_read should be used to increment the
14535 relevant pointer after calling read_initial_length().
14536
14537 [ Note: read_initial_length() and read_offset() are based on the
14538 document entitled "DWARF Debugging Information Format", revision
14539 3, draft 8, dated November 19, 2001. This document was obtained
14540 from:
14541
14542 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
14543
14544 This document is only a draft and is subject to change. (So beware.)
14545
14546 Details regarding the older, non-standard 64-bit format were
14547 determined empirically by examining 64-bit ELF files produced by
14548 the SGI toolchain on an IRIX 6.5 machine.
14549
14550 - Kevin, July 16, 2002
14551 ] */
14552
14553 static LONGEST
14554 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
14555 {
14556 LONGEST length = bfd_get_32 (abfd, buf);
14557
14558 if (length == 0xffffffff)
14559 {
14560 length = bfd_get_64 (abfd, buf + 4);
14561 *bytes_read = 12;
14562 }
14563 else if (length == 0)
14564 {
14565 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
14566 length = bfd_get_64 (abfd, buf);
14567 *bytes_read = 8;
14568 }
14569 else
14570 {
14571 *bytes_read = 4;
14572 }
14573
14574 return length;
14575 }
14576
14577 /* Cover function for read_initial_length.
14578 Returns the length of the object at BUF, and stores the size of the
14579 initial length in *BYTES_READ and stores the size that offsets will be in
14580 *OFFSET_SIZE.
14581 If the initial length size is not equivalent to that specified in
14582 CU_HEADER then issue a complaint.
14583 This is useful when reading non-comp-unit headers. */
14584
14585 static LONGEST
14586 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
14587 const struct comp_unit_head *cu_header,
14588 unsigned int *bytes_read,
14589 unsigned int *offset_size)
14590 {
14591 LONGEST length = read_initial_length (abfd, buf, bytes_read);
14592
14593 gdb_assert (cu_header->initial_length_size == 4
14594 || cu_header->initial_length_size == 8
14595 || cu_header->initial_length_size == 12);
14596
14597 if (cu_header->initial_length_size != *bytes_read)
14598 complaint (&symfile_complaints,
14599 _("intermixed 32-bit and 64-bit DWARF sections"));
14600
14601 *offset_size = (*bytes_read == 4) ? 4 : 8;
14602 return length;
14603 }
14604
14605 /* Read an offset from the data stream. The size of the offset is
14606 given by cu_header->offset_size. */
14607
14608 static LONGEST
14609 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
14610 unsigned int *bytes_read)
14611 {
14612 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
14613
14614 *bytes_read = cu_header->offset_size;
14615 return offset;
14616 }
14617
14618 /* Read an offset from the data stream. */
14619
14620 static LONGEST
14621 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
14622 {
14623 LONGEST retval = 0;
14624
14625 switch (offset_size)
14626 {
14627 case 4:
14628 retval = bfd_get_32 (abfd, buf);
14629 break;
14630 case 8:
14631 retval = bfd_get_64 (abfd, buf);
14632 break;
14633 default:
14634 internal_error (__FILE__, __LINE__,
14635 _("read_offset_1: bad switch [in module %s]"),
14636 bfd_get_filename (abfd));
14637 }
14638
14639 return retval;
14640 }
14641
14642 static gdb_byte *
14643 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
14644 {
14645 /* If the size of a host char is 8 bits, we can return a pointer
14646 to the buffer, otherwise we have to copy the data to a buffer
14647 allocated on the temporary obstack. */
14648 gdb_assert (HOST_CHAR_BIT == 8);
14649 return buf;
14650 }
14651
14652 static char *
14653 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
14654 {
14655 /* If the size of a host char is 8 bits, we can return a pointer
14656 to the string, otherwise we have to copy the string to a buffer
14657 allocated on the temporary obstack. */
14658 gdb_assert (HOST_CHAR_BIT == 8);
14659 if (*buf == '\0')
14660 {
14661 *bytes_read_ptr = 1;
14662 return NULL;
14663 }
14664 *bytes_read_ptr = strlen ((char *) buf) + 1;
14665 return (char *) buf;
14666 }
14667
14668 static char *
14669 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
14670 {
14671 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
14672 if (dwarf2_per_objfile->str.buffer == NULL)
14673 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
14674 bfd_get_filename (abfd));
14675 if (str_offset >= dwarf2_per_objfile->str.size)
14676 error (_("DW_FORM_strp pointing outside of "
14677 ".debug_str section [in module %s]"),
14678 bfd_get_filename (abfd));
14679 gdb_assert (HOST_CHAR_BIT == 8);
14680 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
14681 return NULL;
14682 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
14683 }
14684
14685 /* Read a string at offset STR_OFFSET in the .debug_str section from
14686 the .dwz file DWZ. Throw an error if the offset is too large. If
14687 the string consists of a single NUL byte, return NULL; otherwise
14688 return a pointer to the string. */
14689
14690 static char *
14691 read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
14692 {
14693 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
14694
14695 if (dwz->str.buffer == NULL)
14696 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
14697 "section [in module %s]"),
14698 bfd_get_filename (dwz->dwz_bfd));
14699 if (str_offset >= dwz->str.size)
14700 error (_("DW_FORM_GNU_strp_alt pointing outside of "
14701 ".debug_str section [in module %s]"),
14702 bfd_get_filename (dwz->dwz_bfd));
14703 gdb_assert (HOST_CHAR_BIT == 8);
14704 if (dwz->str.buffer[str_offset] == '\0')
14705 return NULL;
14706 return (char *) (dwz->str.buffer + str_offset);
14707 }
14708
14709 static char *
14710 read_indirect_string (bfd *abfd, gdb_byte *buf,
14711 const struct comp_unit_head *cu_header,
14712 unsigned int *bytes_read_ptr)
14713 {
14714 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
14715
14716 return read_indirect_string_at_offset (abfd, str_offset);
14717 }
14718
14719 static ULONGEST
14720 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
14721 {
14722 ULONGEST result;
14723 unsigned int num_read;
14724 int i, shift;
14725 unsigned char byte;
14726
14727 result = 0;
14728 shift = 0;
14729 num_read = 0;
14730 i = 0;
14731 while (1)
14732 {
14733 byte = bfd_get_8 (abfd, buf);
14734 buf++;
14735 num_read++;
14736 result |= ((ULONGEST) (byte & 127) << shift);
14737 if ((byte & 128) == 0)
14738 {
14739 break;
14740 }
14741 shift += 7;
14742 }
14743 *bytes_read_ptr = num_read;
14744 return result;
14745 }
14746
14747 static LONGEST
14748 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
14749 {
14750 LONGEST result;
14751 int i, shift, num_read;
14752 unsigned char byte;
14753
14754 result = 0;
14755 shift = 0;
14756 num_read = 0;
14757 i = 0;
14758 while (1)
14759 {
14760 byte = bfd_get_8 (abfd, buf);
14761 buf++;
14762 num_read++;
14763 result |= ((LONGEST) (byte & 127) << shift);
14764 shift += 7;
14765 if ((byte & 128) == 0)
14766 {
14767 break;
14768 }
14769 }
14770 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
14771 result |= -(((LONGEST) 1) << shift);
14772 *bytes_read_ptr = num_read;
14773 return result;
14774 }
14775
14776 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
14777 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
14778 ADDR_SIZE is the size of addresses from the CU header. */
14779
14780 static CORE_ADDR
14781 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
14782 {
14783 struct objfile *objfile = dwarf2_per_objfile->objfile;
14784 bfd *abfd = objfile->obfd;
14785 const gdb_byte *info_ptr;
14786
14787 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
14788 if (dwarf2_per_objfile->addr.buffer == NULL)
14789 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
14790 objfile->name);
14791 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
14792 error (_("DW_FORM_addr_index pointing outside of "
14793 ".debug_addr section [in module %s]"),
14794 objfile->name);
14795 info_ptr = (dwarf2_per_objfile->addr.buffer
14796 + addr_base + addr_index * addr_size);
14797 if (addr_size == 4)
14798 return bfd_get_32 (abfd, info_ptr);
14799 else
14800 return bfd_get_64 (abfd, info_ptr);
14801 }
14802
14803 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
14804
14805 static CORE_ADDR
14806 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
14807 {
14808 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
14809 }
14810
14811 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
14812
14813 static CORE_ADDR
14814 read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
14815 unsigned int *bytes_read)
14816 {
14817 bfd *abfd = cu->objfile->obfd;
14818 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
14819
14820 return read_addr_index (cu, addr_index);
14821 }
14822
14823 /* Data structure to pass results from dwarf2_read_addr_index_reader
14824 back to dwarf2_read_addr_index. */
14825
14826 struct dwarf2_read_addr_index_data
14827 {
14828 ULONGEST addr_base;
14829 int addr_size;
14830 };
14831
14832 /* die_reader_func for dwarf2_read_addr_index. */
14833
14834 static void
14835 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
14836 gdb_byte *info_ptr,
14837 struct die_info *comp_unit_die,
14838 int has_children,
14839 void *data)
14840 {
14841 struct dwarf2_cu *cu = reader->cu;
14842 struct dwarf2_read_addr_index_data *aidata =
14843 (struct dwarf2_read_addr_index_data *) data;
14844
14845 aidata->addr_base = cu->addr_base;
14846 aidata->addr_size = cu->header.addr_size;
14847 }
14848
14849 /* Given an index in .debug_addr, fetch the value.
14850 NOTE: This can be called during dwarf expression evaluation,
14851 long after the debug information has been read, and thus per_cu->cu
14852 may no longer exist. */
14853
14854 CORE_ADDR
14855 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
14856 unsigned int addr_index)
14857 {
14858 struct objfile *objfile = per_cu->objfile;
14859 struct dwarf2_cu *cu = per_cu->cu;
14860 ULONGEST addr_base;
14861 int addr_size;
14862
14863 /* This is intended to be called from outside this file. */
14864 dw2_setup (objfile);
14865
14866 /* We need addr_base and addr_size.
14867 If we don't have PER_CU->cu, we have to get it.
14868 Nasty, but the alternative is storing the needed info in PER_CU,
14869 which at this point doesn't seem justified: it's not clear how frequently
14870 it would get used and it would increase the size of every PER_CU.
14871 Entry points like dwarf2_per_cu_addr_size do a similar thing
14872 so we're not in uncharted territory here.
14873 Alas we need to be a bit more complicated as addr_base is contained
14874 in the DIE.
14875
14876 We don't need to read the entire CU(/TU).
14877 We just need the header and top level die.
14878
14879 IWBN to use the aging mechanism to let us lazily later discard the CU.
14880 For now we skip this optimization. */
14881
14882 if (cu != NULL)
14883 {
14884 addr_base = cu->addr_base;
14885 addr_size = cu->header.addr_size;
14886 }
14887 else
14888 {
14889 struct dwarf2_read_addr_index_data aidata;
14890
14891 /* Note: We can't use init_cutu_and_read_dies_simple here,
14892 we need addr_base. */
14893 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
14894 dwarf2_read_addr_index_reader, &aidata);
14895 addr_base = aidata.addr_base;
14896 addr_size = aidata.addr_size;
14897 }
14898
14899 return read_addr_index_1 (addr_index, addr_base, addr_size);
14900 }
14901
14902 /* Given a DW_AT_str_index, fetch the string. */
14903
14904 static char *
14905 read_str_index (const struct die_reader_specs *reader,
14906 struct dwarf2_cu *cu, ULONGEST str_index)
14907 {
14908 struct objfile *objfile = dwarf2_per_objfile->objfile;
14909 const char *dwo_name = objfile->name;
14910 bfd *abfd = objfile->obfd;
14911 struct dwo_sections *sections = &reader->dwo_file->sections;
14912 gdb_byte *info_ptr;
14913 ULONGEST str_offset;
14914
14915 dwarf2_read_section (objfile, &sections->str);
14916 dwarf2_read_section (objfile, &sections->str_offsets);
14917 if (sections->str.buffer == NULL)
14918 error (_("DW_FORM_str_index used without .debug_str.dwo section"
14919 " in CU at offset 0x%lx [in module %s]"),
14920 (long) cu->header.offset.sect_off, dwo_name);
14921 if (sections->str_offsets.buffer == NULL)
14922 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
14923 " in CU at offset 0x%lx [in module %s]"),
14924 (long) cu->header.offset.sect_off, dwo_name);
14925 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
14926 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
14927 " section in CU at offset 0x%lx [in module %s]"),
14928 (long) cu->header.offset.sect_off, dwo_name);
14929 info_ptr = (sections->str_offsets.buffer
14930 + str_index * cu->header.offset_size);
14931 if (cu->header.offset_size == 4)
14932 str_offset = bfd_get_32 (abfd, info_ptr);
14933 else
14934 str_offset = bfd_get_64 (abfd, info_ptr);
14935 if (str_offset >= sections->str.size)
14936 error (_("Offset from DW_FORM_str_index pointing outside of"
14937 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
14938 (long) cu->header.offset.sect_off, dwo_name);
14939 return (char *) (sections->str.buffer + str_offset);
14940 }
14941
14942 /* Return the length of an LEB128 number in BUF. */
14943
14944 static int
14945 leb128_size (const gdb_byte *buf)
14946 {
14947 const gdb_byte *begin = buf;
14948 gdb_byte byte;
14949
14950 while (1)
14951 {
14952 byte = *buf++;
14953 if ((byte & 128) == 0)
14954 return buf - begin;
14955 }
14956 }
14957
14958 static void
14959 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
14960 {
14961 switch (lang)
14962 {
14963 case DW_LANG_C89:
14964 case DW_LANG_C99:
14965 case DW_LANG_C:
14966 cu->language = language_c;
14967 break;
14968 case DW_LANG_C_plus_plus:
14969 cu->language = language_cplus;
14970 break;
14971 case DW_LANG_D:
14972 cu->language = language_d;
14973 break;
14974 case DW_LANG_Fortran77:
14975 case DW_LANG_Fortran90:
14976 case DW_LANG_Fortran95:
14977 cu->language = language_fortran;
14978 break;
14979 case DW_LANG_Go:
14980 cu->language = language_go;
14981 break;
14982 case DW_LANG_Mips_Assembler:
14983 cu->language = language_asm;
14984 break;
14985 case DW_LANG_Java:
14986 cu->language = language_java;
14987 break;
14988 case DW_LANG_Ada83:
14989 case DW_LANG_Ada95:
14990 cu->language = language_ada;
14991 break;
14992 case DW_LANG_Modula2:
14993 cu->language = language_m2;
14994 break;
14995 case DW_LANG_Pascal83:
14996 cu->language = language_pascal;
14997 break;
14998 case DW_LANG_ObjC:
14999 cu->language = language_objc;
15000 break;
15001 case DW_LANG_Cobol74:
15002 case DW_LANG_Cobol85:
15003 default:
15004 cu->language = language_minimal;
15005 break;
15006 }
15007 cu->language_defn = language_def (cu->language);
15008 }
15009
15010 /* Return the named attribute or NULL if not there. */
15011
15012 static struct attribute *
15013 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
15014 {
15015 for (;;)
15016 {
15017 unsigned int i;
15018 struct attribute *spec = NULL;
15019
15020 for (i = 0; i < die->num_attrs; ++i)
15021 {
15022 if (die->attrs[i].name == name)
15023 return &die->attrs[i];
15024 if (die->attrs[i].name == DW_AT_specification
15025 || die->attrs[i].name == DW_AT_abstract_origin)
15026 spec = &die->attrs[i];
15027 }
15028
15029 if (!spec)
15030 break;
15031
15032 die = follow_die_ref (die, spec, &cu);
15033 }
15034
15035 return NULL;
15036 }
15037
15038 /* Return the named attribute or NULL if not there,
15039 but do not follow DW_AT_specification, etc.
15040 This is for use in contexts where we're reading .debug_types dies.
15041 Following DW_AT_specification, DW_AT_abstract_origin will take us
15042 back up the chain, and we want to go down. */
15043
15044 static struct attribute *
15045 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
15046 {
15047 unsigned int i;
15048
15049 for (i = 0; i < die->num_attrs; ++i)
15050 if (die->attrs[i].name == name)
15051 return &die->attrs[i];
15052
15053 return NULL;
15054 }
15055
15056 /* Return non-zero iff the attribute NAME is defined for the given DIE,
15057 and holds a non-zero value. This function should only be used for
15058 DW_FORM_flag or DW_FORM_flag_present attributes. */
15059
15060 static int
15061 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
15062 {
15063 struct attribute *attr = dwarf2_attr (die, name, cu);
15064
15065 return (attr && DW_UNSND (attr));
15066 }
15067
15068 static int
15069 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
15070 {
15071 /* A DIE is a declaration if it has a DW_AT_declaration attribute
15072 which value is non-zero. However, we have to be careful with
15073 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
15074 (via dwarf2_flag_true_p) follows this attribute. So we may
15075 end up accidently finding a declaration attribute that belongs
15076 to a different DIE referenced by the specification attribute,
15077 even though the given DIE does not have a declaration attribute. */
15078 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
15079 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
15080 }
15081
15082 /* Return the die giving the specification for DIE, if there is
15083 one. *SPEC_CU is the CU containing DIE on input, and the CU
15084 containing the return value on output. If there is no
15085 specification, but there is an abstract origin, that is
15086 returned. */
15087
15088 static struct die_info *
15089 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
15090 {
15091 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
15092 *spec_cu);
15093
15094 if (spec_attr == NULL)
15095 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
15096
15097 if (spec_attr == NULL)
15098 return NULL;
15099 else
15100 return follow_die_ref (die, spec_attr, spec_cu);
15101 }
15102
15103 /* Free the line_header structure *LH, and any arrays and strings it
15104 refers to.
15105 NOTE: This is also used as a "cleanup" function. */
15106
15107 static void
15108 free_line_header (struct line_header *lh)
15109 {
15110 if (lh->standard_opcode_lengths)
15111 xfree (lh->standard_opcode_lengths);
15112
15113 /* Remember that all the lh->file_names[i].name pointers are
15114 pointers into debug_line_buffer, and don't need to be freed. */
15115 if (lh->file_names)
15116 xfree (lh->file_names);
15117
15118 /* Similarly for the include directory names. */
15119 if (lh->include_dirs)
15120 xfree (lh->include_dirs);
15121
15122 xfree (lh);
15123 }
15124
15125 /* Add an entry to LH's include directory table. */
15126
15127 static void
15128 add_include_dir (struct line_header *lh, char *include_dir)
15129 {
15130 /* Grow the array if necessary. */
15131 if (lh->include_dirs_size == 0)
15132 {
15133 lh->include_dirs_size = 1; /* for testing */
15134 lh->include_dirs = xmalloc (lh->include_dirs_size
15135 * sizeof (*lh->include_dirs));
15136 }
15137 else if (lh->num_include_dirs >= lh->include_dirs_size)
15138 {
15139 lh->include_dirs_size *= 2;
15140 lh->include_dirs = xrealloc (lh->include_dirs,
15141 (lh->include_dirs_size
15142 * sizeof (*lh->include_dirs)));
15143 }
15144
15145 lh->include_dirs[lh->num_include_dirs++] = include_dir;
15146 }
15147
15148 /* Add an entry to LH's file name table. */
15149
15150 static void
15151 add_file_name (struct line_header *lh,
15152 char *name,
15153 unsigned int dir_index,
15154 unsigned int mod_time,
15155 unsigned int length)
15156 {
15157 struct file_entry *fe;
15158
15159 /* Grow the array if necessary. */
15160 if (lh->file_names_size == 0)
15161 {
15162 lh->file_names_size = 1; /* for testing */
15163 lh->file_names = xmalloc (lh->file_names_size
15164 * sizeof (*lh->file_names));
15165 }
15166 else if (lh->num_file_names >= lh->file_names_size)
15167 {
15168 lh->file_names_size *= 2;
15169 lh->file_names = xrealloc (lh->file_names,
15170 (lh->file_names_size
15171 * sizeof (*lh->file_names)));
15172 }
15173
15174 fe = &lh->file_names[lh->num_file_names++];
15175 fe->name = name;
15176 fe->dir_index = dir_index;
15177 fe->mod_time = mod_time;
15178 fe->length = length;
15179 fe->included_p = 0;
15180 fe->symtab = NULL;
15181 }
15182
15183 /* A convenience function to find the proper .debug_line section for a
15184 CU. */
15185
15186 static struct dwarf2_section_info *
15187 get_debug_line_section (struct dwarf2_cu *cu)
15188 {
15189 struct dwarf2_section_info *section;
15190
15191 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
15192 DWO file. */
15193 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15194 section = &cu->dwo_unit->dwo_file->sections.line;
15195 else if (cu->per_cu->is_dwz)
15196 {
15197 struct dwz_file *dwz = dwarf2_get_dwz_file ();
15198
15199 section = &dwz->line;
15200 }
15201 else
15202 section = &dwarf2_per_objfile->line;
15203
15204 return section;
15205 }
15206
15207 /* Read the statement program header starting at OFFSET in
15208 .debug_line, or .debug_line.dwo. Return a pointer
15209 to a struct line_header, allocated using xmalloc.
15210
15211 NOTE: the strings in the include directory and file name tables of
15212 the returned object point into the dwarf line section buffer,
15213 and must not be freed. */
15214
15215 static struct line_header *
15216 dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
15217 {
15218 struct cleanup *back_to;
15219 struct line_header *lh;
15220 gdb_byte *line_ptr;
15221 unsigned int bytes_read, offset_size;
15222 int i;
15223 char *cur_dir, *cur_file;
15224 struct dwarf2_section_info *section;
15225 bfd *abfd;
15226
15227 section = get_debug_line_section (cu);
15228 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15229 if (section->buffer == NULL)
15230 {
15231 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15232 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
15233 else
15234 complaint (&symfile_complaints, _("missing .debug_line section"));
15235 return 0;
15236 }
15237
15238 /* We can't do this until we know the section is non-empty.
15239 Only then do we know we have such a section. */
15240 abfd = section->asection->owner;
15241
15242 /* Make sure that at least there's room for the total_length field.
15243 That could be 12 bytes long, but we're just going to fudge that. */
15244 if (offset + 4 >= section->size)
15245 {
15246 dwarf2_statement_list_fits_in_line_number_section_complaint ();
15247 return 0;
15248 }
15249
15250 lh = xmalloc (sizeof (*lh));
15251 memset (lh, 0, sizeof (*lh));
15252 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
15253 (void *) lh);
15254
15255 line_ptr = section->buffer + offset;
15256
15257 /* Read in the header. */
15258 lh->total_length =
15259 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
15260 &bytes_read, &offset_size);
15261 line_ptr += bytes_read;
15262 if (line_ptr + lh->total_length > (section->buffer + section->size))
15263 {
15264 dwarf2_statement_list_fits_in_line_number_section_complaint ();
15265 return 0;
15266 }
15267 lh->statement_program_end = line_ptr + lh->total_length;
15268 lh->version = read_2_bytes (abfd, line_ptr);
15269 line_ptr += 2;
15270 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
15271 line_ptr += offset_size;
15272 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
15273 line_ptr += 1;
15274 if (lh->version >= 4)
15275 {
15276 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
15277 line_ptr += 1;
15278 }
15279 else
15280 lh->maximum_ops_per_instruction = 1;
15281
15282 if (lh->maximum_ops_per_instruction == 0)
15283 {
15284 lh->maximum_ops_per_instruction = 1;
15285 complaint (&symfile_complaints,
15286 _("invalid maximum_ops_per_instruction "
15287 "in `.debug_line' section"));
15288 }
15289
15290 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
15291 line_ptr += 1;
15292 lh->line_base = read_1_signed_byte (abfd, line_ptr);
15293 line_ptr += 1;
15294 lh->line_range = read_1_byte (abfd, line_ptr);
15295 line_ptr += 1;
15296 lh->opcode_base = read_1_byte (abfd, line_ptr);
15297 line_ptr += 1;
15298 lh->standard_opcode_lengths
15299 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
15300
15301 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
15302 for (i = 1; i < lh->opcode_base; ++i)
15303 {
15304 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
15305 line_ptr += 1;
15306 }
15307
15308 /* Read directory table. */
15309 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
15310 {
15311 line_ptr += bytes_read;
15312 add_include_dir (lh, cur_dir);
15313 }
15314 line_ptr += bytes_read;
15315
15316 /* Read file name table. */
15317 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
15318 {
15319 unsigned int dir_index, mod_time, length;
15320
15321 line_ptr += bytes_read;
15322 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15323 line_ptr += bytes_read;
15324 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15325 line_ptr += bytes_read;
15326 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15327 line_ptr += bytes_read;
15328
15329 add_file_name (lh, cur_file, dir_index, mod_time, length);
15330 }
15331 line_ptr += bytes_read;
15332 lh->statement_program_start = line_ptr;
15333
15334 if (line_ptr > (section->buffer + section->size))
15335 complaint (&symfile_complaints,
15336 _("line number info header doesn't "
15337 "fit in `.debug_line' section"));
15338
15339 discard_cleanups (back_to);
15340 return lh;
15341 }
15342
15343 /* Subroutine of dwarf_decode_lines to simplify it.
15344 Return the file name of the psymtab for included file FILE_INDEX
15345 in line header LH of PST.
15346 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15347 If space for the result is malloc'd, it will be freed by a cleanup.
15348 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
15349
15350 The function creates dangling cleanup registration. */
15351
15352 static char *
15353 psymtab_include_file_name (const struct line_header *lh, int file_index,
15354 const struct partial_symtab *pst,
15355 const char *comp_dir)
15356 {
15357 const struct file_entry fe = lh->file_names [file_index];
15358 char *include_name = fe.name;
15359 char *include_name_to_compare = include_name;
15360 char *dir_name = NULL;
15361 const char *pst_filename;
15362 char *copied_name = NULL;
15363 int file_is_pst;
15364
15365 if (fe.dir_index)
15366 dir_name = lh->include_dirs[fe.dir_index - 1];
15367
15368 if (!IS_ABSOLUTE_PATH (include_name)
15369 && (dir_name != NULL || comp_dir != NULL))
15370 {
15371 /* Avoid creating a duplicate psymtab for PST.
15372 We do this by comparing INCLUDE_NAME and PST_FILENAME.
15373 Before we do the comparison, however, we need to account
15374 for DIR_NAME and COMP_DIR.
15375 First prepend dir_name (if non-NULL). If we still don't
15376 have an absolute path prepend comp_dir (if non-NULL).
15377 However, the directory we record in the include-file's
15378 psymtab does not contain COMP_DIR (to match the
15379 corresponding symtab(s)).
15380
15381 Example:
15382
15383 bash$ cd /tmp
15384 bash$ gcc -g ./hello.c
15385 include_name = "hello.c"
15386 dir_name = "."
15387 DW_AT_comp_dir = comp_dir = "/tmp"
15388 DW_AT_name = "./hello.c" */
15389
15390 if (dir_name != NULL)
15391 {
15392 include_name = concat (dir_name, SLASH_STRING,
15393 include_name, (char *)NULL);
15394 include_name_to_compare = include_name;
15395 make_cleanup (xfree, include_name);
15396 }
15397 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
15398 {
15399 include_name_to_compare = concat (comp_dir, SLASH_STRING,
15400 include_name, (char *)NULL);
15401 }
15402 }
15403
15404 pst_filename = pst->filename;
15405 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
15406 {
15407 copied_name = concat (pst->dirname, SLASH_STRING,
15408 pst_filename, (char *)NULL);
15409 pst_filename = copied_name;
15410 }
15411
15412 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
15413
15414 if (include_name_to_compare != include_name)
15415 xfree (include_name_to_compare);
15416 if (copied_name != NULL)
15417 xfree (copied_name);
15418
15419 if (file_is_pst)
15420 return NULL;
15421 return include_name;
15422 }
15423
15424 /* Ignore this record_line request. */
15425
15426 static void
15427 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
15428 {
15429 return;
15430 }
15431
15432 /* Subroutine of dwarf_decode_lines to simplify it.
15433 Process the line number information in LH. */
15434
15435 static void
15436 dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
15437 struct dwarf2_cu *cu, struct partial_symtab *pst)
15438 {
15439 gdb_byte *line_ptr, *extended_end;
15440 gdb_byte *line_end;
15441 unsigned int bytes_read, extended_len;
15442 unsigned char op_code, extended_op, adj_opcode;
15443 CORE_ADDR baseaddr;
15444 struct objfile *objfile = cu->objfile;
15445 bfd *abfd = objfile->obfd;
15446 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15447 const int decode_for_pst_p = (pst != NULL);
15448 struct subfile *last_subfile = NULL;
15449 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
15450 = record_line;
15451
15452 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15453
15454 line_ptr = lh->statement_program_start;
15455 line_end = lh->statement_program_end;
15456
15457 /* Read the statement sequences until there's nothing left. */
15458 while (line_ptr < line_end)
15459 {
15460 /* state machine registers */
15461 CORE_ADDR address = 0;
15462 unsigned int file = 1;
15463 unsigned int line = 1;
15464 unsigned int column = 0;
15465 int is_stmt = lh->default_is_stmt;
15466 int basic_block = 0;
15467 int end_sequence = 0;
15468 CORE_ADDR addr;
15469 unsigned char op_index = 0;
15470
15471 if (!decode_for_pst_p && lh->num_file_names >= file)
15472 {
15473 /* Start a subfile for the current file of the state machine. */
15474 /* lh->include_dirs and lh->file_names are 0-based, but the
15475 directory and file name numbers in the statement program
15476 are 1-based. */
15477 struct file_entry *fe = &lh->file_names[file - 1];
15478 char *dir = NULL;
15479
15480 if (fe->dir_index)
15481 dir = lh->include_dirs[fe->dir_index - 1];
15482
15483 dwarf2_start_subfile (fe->name, dir, comp_dir);
15484 }
15485
15486 /* Decode the table. */
15487 while (!end_sequence)
15488 {
15489 op_code = read_1_byte (abfd, line_ptr);
15490 line_ptr += 1;
15491 if (line_ptr > line_end)
15492 {
15493 dwarf2_debug_line_missing_end_sequence_complaint ();
15494 break;
15495 }
15496
15497 if (op_code >= lh->opcode_base)
15498 {
15499 /* Special operand. */
15500 adj_opcode = op_code - lh->opcode_base;
15501 address += (((op_index + (adj_opcode / lh->line_range))
15502 / lh->maximum_ops_per_instruction)
15503 * lh->minimum_instruction_length);
15504 op_index = ((op_index + (adj_opcode / lh->line_range))
15505 % lh->maximum_ops_per_instruction);
15506 line += lh->line_base + (adj_opcode % lh->line_range);
15507 if (lh->num_file_names < file || file == 0)
15508 dwarf2_debug_line_missing_file_complaint ();
15509 /* For now we ignore lines not starting on an
15510 instruction boundary. */
15511 else if (op_index == 0)
15512 {
15513 lh->file_names[file - 1].included_p = 1;
15514 if (!decode_for_pst_p && is_stmt)
15515 {
15516 if (last_subfile != current_subfile)
15517 {
15518 addr = gdbarch_addr_bits_remove (gdbarch, address);
15519 if (last_subfile)
15520 (*p_record_line) (last_subfile, 0, addr);
15521 last_subfile = current_subfile;
15522 }
15523 /* Append row to matrix using current values. */
15524 addr = gdbarch_addr_bits_remove (gdbarch, address);
15525 (*p_record_line) (current_subfile, line, addr);
15526 }
15527 }
15528 basic_block = 0;
15529 }
15530 else switch (op_code)
15531 {
15532 case DW_LNS_extended_op:
15533 extended_len = read_unsigned_leb128 (abfd, line_ptr,
15534 &bytes_read);
15535 line_ptr += bytes_read;
15536 extended_end = line_ptr + extended_len;
15537 extended_op = read_1_byte (abfd, line_ptr);
15538 line_ptr += 1;
15539 switch (extended_op)
15540 {
15541 case DW_LNE_end_sequence:
15542 p_record_line = record_line;
15543 end_sequence = 1;
15544 break;
15545 case DW_LNE_set_address:
15546 address = read_address (abfd, line_ptr, cu, &bytes_read);
15547
15548 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
15549 {
15550 /* This line table is for a function which has been
15551 GCd by the linker. Ignore it. PR gdb/12528 */
15552
15553 long line_offset
15554 = line_ptr - get_debug_line_section (cu)->buffer;
15555
15556 complaint (&symfile_complaints,
15557 _(".debug_line address at offset 0x%lx is 0 "
15558 "[in module %s]"),
15559 line_offset, objfile->name);
15560 p_record_line = noop_record_line;
15561 }
15562
15563 op_index = 0;
15564 line_ptr += bytes_read;
15565 address += baseaddr;
15566 break;
15567 case DW_LNE_define_file:
15568 {
15569 char *cur_file;
15570 unsigned int dir_index, mod_time, length;
15571
15572 cur_file = read_direct_string (abfd, line_ptr,
15573 &bytes_read);
15574 line_ptr += bytes_read;
15575 dir_index =
15576 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15577 line_ptr += bytes_read;
15578 mod_time =
15579 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15580 line_ptr += bytes_read;
15581 length =
15582 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15583 line_ptr += bytes_read;
15584 add_file_name (lh, cur_file, dir_index, mod_time, length);
15585 }
15586 break;
15587 case DW_LNE_set_discriminator:
15588 /* The discriminator is not interesting to the debugger;
15589 just ignore it. */
15590 line_ptr = extended_end;
15591 break;
15592 default:
15593 complaint (&symfile_complaints,
15594 _("mangled .debug_line section"));
15595 return;
15596 }
15597 /* Make sure that we parsed the extended op correctly. If e.g.
15598 we expected a different address size than the producer used,
15599 we may have read the wrong number of bytes. */
15600 if (line_ptr != extended_end)
15601 {
15602 complaint (&symfile_complaints,
15603 _("mangled .debug_line section"));
15604 return;
15605 }
15606 break;
15607 case DW_LNS_copy:
15608 if (lh->num_file_names < file || file == 0)
15609 dwarf2_debug_line_missing_file_complaint ();
15610 else
15611 {
15612 lh->file_names[file - 1].included_p = 1;
15613 if (!decode_for_pst_p && is_stmt)
15614 {
15615 if (last_subfile != current_subfile)
15616 {
15617 addr = gdbarch_addr_bits_remove (gdbarch, address);
15618 if (last_subfile)
15619 (*p_record_line) (last_subfile, 0, addr);
15620 last_subfile = current_subfile;
15621 }
15622 addr = gdbarch_addr_bits_remove (gdbarch, address);
15623 (*p_record_line) (current_subfile, line, addr);
15624 }
15625 }
15626 basic_block = 0;
15627 break;
15628 case DW_LNS_advance_pc:
15629 {
15630 CORE_ADDR adjust
15631 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15632
15633 address += (((op_index + adjust)
15634 / lh->maximum_ops_per_instruction)
15635 * lh->minimum_instruction_length);
15636 op_index = ((op_index + adjust)
15637 % lh->maximum_ops_per_instruction);
15638 line_ptr += bytes_read;
15639 }
15640 break;
15641 case DW_LNS_advance_line:
15642 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
15643 line_ptr += bytes_read;
15644 break;
15645 case DW_LNS_set_file:
15646 {
15647 /* The arrays lh->include_dirs and lh->file_names are
15648 0-based, but the directory and file name numbers in
15649 the statement program are 1-based. */
15650 struct file_entry *fe;
15651 char *dir = NULL;
15652
15653 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15654 line_ptr += bytes_read;
15655 if (lh->num_file_names < file || file == 0)
15656 dwarf2_debug_line_missing_file_complaint ();
15657 else
15658 {
15659 fe = &lh->file_names[file - 1];
15660 if (fe->dir_index)
15661 dir = lh->include_dirs[fe->dir_index - 1];
15662 if (!decode_for_pst_p)
15663 {
15664 last_subfile = current_subfile;
15665 dwarf2_start_subfile (fe->name, dir, comp_dir);
15666 }
15667 }
15668 }
15669 break;
15670 case DW_LNS_set_column:
15671 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15672 line_ptr += bytes_read;
15673 break;
15674 case DW_LNS_negate_stmt:
15675 is_stmt = (!is_stmt);
15676 break;
15677 case DW_LNS_set_basic_block:
15678 basic_block = 1;
15679 break;
15680 /* Add to the address register of the state machine the
15681 address increment value corresponding to special opcode
15682 255. I.e., this value is scaled by the minimum
15683 instruction length since special opcode 255 would have
15684 scaled the increment. */
15685 case DW_LNS_const_add_pc:
15686 {
15687 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
15688
15689 address += (((op_index + adjust)
15690 / lh->maximum_ops_per_instruction)
15691 * lh->minimum_instruction_length);
15692 op_index = ((op_index + adjust)
15693 % lh->maximum_ops_per_instruction);
15694 }
15695 break;
15696 case DW_LNS_fixed_advance_pc:
15697 address += read_2_bytes (abfd, line_ptr);
15698 op_index = 0;
15699 line_ptr += 2;
15700 break;
15701 default:
15702 {
15703 /* Unknown standard opcode, ignore it. */
15704 int i;
15705
15706 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
15707 {
15708 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15709 line_ptr += bytes_read;
15710 }
15711 }
15712 }
15713 }
15714 if (lh->num_file_names < file || file == 0)
15715 dwarf2_debug_line_missing_file_complaint ();
15716 else
15717 {
15718 lh->file_names[file - 1].included_p = 1;
15719 if (!decode_for_pst_p)
15720 {
15721 addr = gdbarch_addr_bits_remove (gdbarch, address);
15722 (*p_record_line) (current_subfile, 0, addr);
15723 }
15724 }
15725 }
15726 }
15727
15728 /* Decode the Line Number Program (LNP) for the given line_header
15729 structure and CU. The actual information extracted and the type
15730 of structures created from the LNP depends on the value of PST.
15731
15732 1. If PST is NULL, then this procedure uses the data from the program
15733 to create all necessary symbol tables, and their linetables.
15734
15735 2. If PST is not NULL, this procedure reads the program to determine
15736 the list of files included by the unit represented by PST, and
15737 builds all the associated partial symbol tables.
15738
15739 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15740 It is used for relative paths in the line table.
15741 NOTE: When processing partial symtabs (pst != NULL),
15742 comp_dir == pst->dirname.
15743
15744 NOTE: It is important that psymtabs have the same file name (via strcmp)
15745 as the corresponding symtab. Since COMP_DIR is not used in the name of the
15746 symtab we don't use it in the name of the psymtabs we create.
15747 E.g. expand_line_sal requires this when finding psymtabs to expand.
15748 A good testcase for this is mb-inline.exp. */
15749
15750 static void
15751 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
15752 struct dwarf2_cu *cu, struct partial_symtab *pst,
15753 int want_line_info)
15754 {
15755 struct objfile *objfile = cu->objfile;
15756 const int decode_for_pst_p = (pst != NULL);
15757 struct subfile *first_subfile = current_subfile;
15758
15759 if (want_line_info)
15760 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
15761
15762 if (decode_for_pst_p)
15763 {
15764 int file_index;
15765
15766 /* Now that we're done scanning the Line Header Program, we can
15767 create the psymtab of each included file. */
15768 for (file_index = 0; file_index < lh->num_file_names; file_index++)
15769 if (lh->file_names[file_index].included_p == 1)
15770 {
15771 char *include_name =
15772 psymtab_include_file_name (lh, file_index, pst, comp_dir);
15773 if (include_name != NULL)
15774 dwarf2_create_include_psymtab (include_name, pst, objfile);
15775 }
15776 }
15777 else
15778 {
15779 /* Make sure a symtab is created for every file, even files
15780 which contain only variables (i.e. no code with associated
15781 line numbers). */
15782 int i;
15783
15784 for (i = 0; i < lh->num_file_names; i++)
15785 {
15786 char *dir = NULL;
15787 struct file_entry *fe;
15788
15789 fe = &lh->file_names[i];
15790 if (fe->dir_index)
15791 dir = lh->include_dirs[fe->dir_index - 1];
15792 dwarf2_start_subfile (fe->name, dir, comp_dir);
15793
15794 /* Skip the main file; we don't need it, and it must be
15795 allocated last, so that it will show up before the
15796 non-primary symtabs in the objfile's symtab list. */
15797 if (current_subfile == first_subfile)
15798 continue;
15799
15800 if (current_subfile->symtab == NULL)
15801 current_subfile->symtab = allocate_symtab (current_subfile->name,
15802 objfile);
15803 fe->symtab = current_subfile->symtab;
15804 }
15805 }
15806 }
15807
15808 /* Start a subfile for DWARF. FILENAME is the name of the file and
15809 DIRNAME the name of the source directory which contains FILENAME
15810 or NULL if not known. COMP_DIR is the compilation directory for the
15811 linetable's compilation unit or NULL if not known.
15812 This routine tries to keep line numbers from identical absolute and
15813 relative file names in a common subfile.
15814
15815 Using the `list' example from the GDB testsuite, which resides in
15816 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
15817 of /srcdir/list0.c yields the following debugging information for list0.c:
15818
15819 DW_AT_name: /srcdir/list0.c
15820 DW_AT_comp_dir: /compdir
15821 files.files[0].name: list0.h
15822 files.files[0].dir: /srcdir
15823 files.files[1].name: list0.c
15824 files.files[1].dir: /srcdir
15825
15826 The line number information for list0.c has to end up in a single
15827 subfile, so that `break /srcdir/list0.c:1' works as expected.
15828 start_subfile will ensure that this happens provided that we pass the
15829 concatenation of files.files[1].dir and files.files[1].name as the
15830 subfile's name. */
15831
15832 static void
15833 dwarf2_start_subfile (char *filename, const char *dirname,
15834 const char *comp_dir)
15835 {
15836 char *fullname;
15837
15838 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
15839 `start_symtab' will always pass the contents of DW_AT_comp_dir as
15840 second argument to start_subfile. To be consistent, we do the
15841 same here. In order not to lose the line information directory,
15842 we concatenate it to the filename when it makes sense.
15843 Note that the Dwarf3 standard says (speaking of filenames in line
15844 information): ``The directory index is ignored for file names
15845 that represent full path names''. Thus ignoring dirname in the
15846 `else' branch below isn't an issue. */
15847
15848 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
15849 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
15850 else
15851 fullname = filename;
15852
15853 start_subfile (fullname, comp_dir);
15854
15855 if (fullname != filename)
15856 xfree (fullname);
15857 }
15858
15859 /* Start a symtab for DWARF.
15860 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
15861
15862 static void
15863 dwarf2_start_symtab (struct dwarf2_cu *cu,
15864 const char *name, const char *comp_dir, CORE_ADDR low_pc)
15865 {
15866 start_symtab (name, comp_dir, low_pc);
15867 record_debugformat ("DWARF 2");
15868 record_producer (cu->producer);
15869
15870 /* We assume that we're processing GCC output. */
15871 processing_gcc_compilation = 2;
15872
15873 cu->processing_has_namespace_info = 0;
15874 }
15875
15876 static void
15877 var_decode_location (struct attribute *attr, struct symbol *sym,
15878 struct dwarf2_cu *cu)
15879 {
15880 struct objfile *objfile = cu->objfile;
15881 struct comp_unit_head *cu_header = &cu->header;
15882
15883 /* NOTE drow/2003-01-30: There used to be a comment and some special
15884 code here to turn a symbol with DW_AT_external and a
15885 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
15886 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
15887 with some versions of binutils) where shared libraries could have
15888 relocations against symbols in their debug information - the
15889 minimal symbol would have the right address, but the debug info
15890 would not. It's no longer necessary, because we will explicitly
15891 apply relocations when we read in the debug information now. */
15892
15893 /* A DW_AT_location attribute with no contents indicates that a
15894 variable has been optimized away. */
15895 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
15896 {
15897 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
15898 return;
15899 }
15900
15901 /* Handle one degenerate form of location expression specially, to
15902 preserve GDB's previous behavior when section offsets are
15903 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
15904 then mark this symbol as LOC_STATIC. */
15905
15906 if (attr_form_is_block (attr)
15907 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
15908 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
15909 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
15910 && (DW_BLOCK (attr)->size
15911 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
15912 {
15913 unsigned int dummy;
15914
15915 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
15916 SYMBOL_VALUE_ADDRESS (sym) =
15917 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
15918 else
15919 SYMBOL_VALUE_ADDRESS (sym) =
15920 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
15921 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
15922 fixup_symbol_section (sym, objfile);
15923 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
15924 SYMBOL_SECTION (sym));
15925 return;
15926 }
15927
15928 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
15929 expression evaluator, and use LOC_COMPUTED only when necessary
15930 (i.e. when the value of a register or memory location is
15931 referenced, or a thread-local block, etc.). Then again, it might
15932 not be worthwhile. I'm assuming that it isn't unless performance
15933 or memory numbers show me otherwise. */
15934
15935 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
15936
15937 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
15938 cu->has_loclist = 1;
15939 }
15940
15941 /* Given a pointer to a DWARF information entry, figure out if we need
15942 to make a symbol table entry for it, and if so, create a new entry
15943 and return a pointer to it.
15944 If TYPE is NULL, determine symbol type from the die, otherwise
15945 used the passed type.
15946 If SPACE is not NULL, use it to hold the new symbol. If it is
15947 NULL, allocate a new symbol on the objfile's obstack. */
15948
15949 static struct symbol *
15950 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
15951 struct symbol *space)
15952 {
15953 struct objfile *objfile = cu->objfile;
15954 struct symbol *sym = NULL;
15955 const char *name;
15956 struct attribute *attr = NULL;
15957 struct attribute *attr2 = NULL;
15958 CORE_ADDR baseaddr;
15959 struct pending **list_to_add = NULL;
15960
15961 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
15962
15963 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15964
15965 name = dwarf2_name (die, cu);
15966 if (name)
15967 {
15968 const char *linkagename;
15969 int suppress_add = 0;
15970
15971 if (space)
15972 sym = space;
15973 else
15974 sym = allocate_symbol (objfile);
15975 OBJSTAT (objfile, n_syms++);
15976
15977 /* Cache this symbol's name and the name's demangled form (if any). */
15978 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
15979 linkagename = dwarf2_physname (name, die, cu);
15980 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
15981
15982 /* Fortran does not have mangling standard and the mangling does differ
15983 between gfortran, iFort etc. */
15984 if (cu->language == language_fortran
15985 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
15986 symbol_set_demangled_name (&(sym->ginfo),
15987 dwarf2_full_name (name, die, cu),
15988 NULL);
15989
15990 /* Default assumptions.
15991 Use the passed type or decode it from the die. */
15992 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
15993 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
15994 if (type != NULL)
15995 SYMBOL_TYPE (sym) = type;
15996 else
15997 SYMBOL_TYPE (sym) = die_type (die, cu);
15998 attr = dwarf2_attr (die,
15999 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
16000 cu);
16001 if (attr)
16002 {
16003 SYMBOL_LINE (sym) = DW_UNSND (attr);
16004 }
16005
16006 attr = dwarf2_attr (die,
16007 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
16008 cu);
16009 if (attr)
16010 {
16011 int file_index = DW_UNSND (attr);
16012
16013 if (cu->line_header == NULL
16014 || file_index > cu->line_header->num_file_names)
16015 complaint (&symfile_complaints,
16016 _("file index out of range"));
16017 else if (file_index > 0)
16018 {
16019 struct file_entry *fe;
16020
16021 fe = &cu->line_header->file_names[file_index - 1];
16022 SYMBOL_SYMTAB (sym) = fe->symtab;
16023 }
16024 }
16025
16026 switch (die->tag)
16027 {
16028 case DW_TAG_label:
16029 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
16030 if (attr)
16031 {
16032 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
16033 }
16034 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
16035 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
16036 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
16037 add_symbol_to_list (sym, cu->list_in_scope);
16038 break;
16039 case DW_TAG_subprogram:
16040 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
16041 finish_block. */
16042 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
16043 attr2 = dwarf2_attr (die, DW_AT_external, cu);
16044 if ((attr2 && (DW_UNSND (attr2) != 0))
16045 || cu->language == language_ada)
16046 {
16047 /* Subprograms marked external are stored as a global symbol.
16048 Ada subprograms, whether marked external or not, are always
16049 stored as a global symbol, because we want to be able to
16050 access them globally. For instance, we want to be able
16051 to break on a nested subprogram without having to
16052 specify the context. */
16053 list_to_add = &global_symbols;
16054 }
16055 else
16056 {
16057 list_to_add = cu->list_in_scope;
16058 }
16059 break;
16060 case DW_TAG_inlined_subroutine:
16061 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
16062 finish_block. */
16063 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
16064 SYMBOL_INLINED (sym) = 1;
16065 list_to_add = cu->list_in_scope;
16066 break;
16067 case DW_TAG_template_value_param:
16068 suppress_add = 1;
16069 /* Fall through. */
16070 case DW_TAG_constant:
16071 case DW_TAG_variable:
16072 case DW_TAG_member:
16073 /* Compilation with minimal debug info may result in
16074 variables with missing type entries. Change the
16075 misleading `void' type to something sensible. */
16076 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
16077 SYMBOL_TYPE (sym)
16078 = objfile_type (objfile)->nodebug_data_symbol;
16079
16080 attr = dwarf2_attr (die, DW_AT_const_value, cu);
16081 /* In the case of DW_TAG_member, we should only be called for
16082 static const members. */
16083 if (die->tag == DW_TAG_member)
16084 {
16085 /* dwarf2_add_field uses die_is_declaration,
16086 so we do the same. */
16087 gdb_assert (die_is_declaration (die, cu));
16088 gdb_assert (attr);
16089 }
16090 if (attr)
16091 {
16092 dwarf2_const_value (attr, sym, cu);
16093 attr2 = dwarf2_attr (die, DW_AT_external, cu);
16094 if (!suppress_add)
16095 {
16096 if (attr2 && (DW_UNSND (attr2) != 0))
16097 list_to_add = &global_symbols;
16098 else
16099 list_to_add = cu->list_in_scope;
16100 }
16101 break;
16102 }
16103 attr = dwarf2_attr (die, DW_AT_location, cu);
16104 if (attr)
16105 {
16106 var_decode_location (attr, sym, cu);
16107 attr2 = dwarf2_attr (die, DW_AT_external, cu);
16108
16109 /* Fortran explicitly imports any global symbols to the local
16110 scope by DW_TAG_common_block. */
16111 if (cu->language == language_fortran && die->parent
16112 && die->parent->tag == DW_TAG_common_block)
16113 attr2 = NULL;
16114
16115 if (SYMBOL_CLASS (sym) == LOC_STATIC
16116 && SYMBOL_VALUE_ADDRESS (sym) == 0
16117 && !dwarf2_per_objfile->has_section_at_zero)
16118 {
16119 /* When a static variable is eliminated by the linker,
16120 the corresponding debug information is not stripped
16121 out, but the variable address is set to null;
16122 do not add such variables into symbol table. */
16123 }
16124 else if (attr2 && (DW_UNSND (attr2) != 0))
16125 {
16126 /* Workaround gfortran PR debug/40040 - it uses
16127 DW_AT_location for variables in -fPIC libraries which may
16128 get overriden by other libraries/executable and get
16129 a different address. Resolve it by the minimal symbol
16130 which may come from inferior's executable using copy
16131 relocation. Make this workaround only for gfortran as for
16132 other compilers GDB cannot guess the minimal symbol
16133 Fortran mangling kind. */
16134 if (cu->language == language_fortran && die->parent
16135 && die->parent->tag == DW_TAG_module
16136 && cu->producer
16137 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
16138 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
16139
16140 /* A variable with DW_AT_external is never static,
16141 but it may be block-scoped. */
16142 list_to_add = (cu->list_in_scope == &file_symbols
16143 ? &global_symbols : cu->list_in_scope);
16144 }
16145 else
16146 list_to_add = cu->list_in_scope;
16147 }
16148 else
16149 {
16150 /* We do not know the address of this symbol.
16151 If it is an external symbol and we have type information
16152 for it, enter the symbol as a LOC_UNRESOLVED symbol.
16153 The address of the variable will then be determined from
16154 the minimal symbol table whenever the variable is
16155 referenced. */
16156 attr2 = dwarf2_attr (die, DW_AT_external, cu);
16157
16158 /* Fortran explicitly imports any global symbols to the local
16159 scope by DW_TAG_common_block. */
16160 if (cu->language == language_fortran && die->parent
16161 && die->parent->tag == DW_TAG_common_block)
16162 {
16163 /* SYMBOL_CLASS doesn't matter here because
16164 read_common_block is going to reset it. */
16165 if (!suppress_add)
16166 list_to_add = cu->list_in_scope;
16167 }
16168 else if (attr2 && (DW_UNSND (attr2) != 0)
16169 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
16170 {
16171 /* A variable with DW_AT_external is never static, but it
16172 may be block-scoped. */
16173 list_to_add = (cu->list_in_scope == &file_symbols
16174 ? &global_symbols : cu->list_in_scope);
16175
16176 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
16177 }
16178 else if (!die_is_declaration (die, cu))
16179 {
16180 /* Use the default LOC_OPTIMIZED_OUT class. */
16181 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
16182 if (!suppress_add)
16183 list_to_add = cu->list_in_scope;
16184 }
16185 }
16186 break;
16187 case DW_TAG_formal_parameter:
16188 /* If we are inside a function, mark this as an argument. If
16189 not, we might be looking at an argument to an inlined function
16190 when we do not have enough information to show inlined frames;
16191 pretend it's a local variable in that case so that the user can
16192 still see it. */
16193 if (context_stack_depth > 0
16194 && context_stack[context_stack_depth - 1].name != NULL)
16195 SYMBOL_IS_ARGUMENT (sym) = 1;
16196 attr = dwarf2_attr (die, DW_AT_location, cu);
16197 if (attr)
16198 {
16199 var_decode_location (attr, sym, cu);
16200 }
16201 attr = dwarf2_attr (die, DW_AT_const_value, cu);
16202 if (attr)
16203 {
16204 dwarf2_const_value (attr, sym, cu);
16205 }
16206
16207 list_to_add = cu->list_in_scope;
16208 break;
16209 case DW_TAG_unspecified_parameters:
16210 /* From varargs functions; gdb doesn't seem to have any
16211 interest in this information, so just ignore it for now.
16212 (FIXME?) */
16213 break;
16214 case DW_TAG_template_type_param:
16215 suppress_add = 1;
16216 /* Fall through. */
16217 case DW_TAG_class_type:
16218 case DW_TAG_interface_type:
16219 case DW_TAG_structure_type:
16220 case DW_TAG_union_type:
16221 case DW_TAG_set_type:
16222 case DW_TAG_enumeration_type:
16223 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16224 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
16225
16226 {
16227 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
16228 really ever be static objects: otherwise, if you try
16229 to, say, break of a class's method and you're in a file
16230 which doesn't mention that class, it won't work unless
16231 the check for all static symbols in lookup_symbol_aux
16232 saves you. See the OtherFileClass tests in
16233 gdb.c++/namespace.exp. */
16234
16235 if (!suppress_add)
16236 {
16237 list_to_add = (cu->list_in_scope == &file_symbols
16238 && (cu->language == language_cplus
16239 || cu->language == language_java)
16240 ? &global_symbols : cu->list_in_scope);
16241
16242 /* The semantics of C++ state that "struct foo {
16243 ... }" also defines a typedef for "foo". A Java
16244 class declaration also defines a typedef for the
16245 class. */
16246 if (cu->language == language_cplus
16247 || cu->language == language_java
16248 || cu->language == language_ada)
16249 {
16250 /* The symbol's name is already allocated along
16251 with this objfile, so we don't need to
16252 duplicate it for the type. */
16253 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
16254 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
16255 }
16256 }
16257 }
16258 break;
16259 case DW_TAG_typedef:
16260 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16261 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
16262 list_to_add = cu->list_in_scope;
16263 break;
16264 case DW_TAG_base_type:
16265 case DW_TAG_subrange_type:
16266 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16267 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
16268 list_to_add = cu->list_in_scope;
16269 break;
16270 case DW_TAG_enumerator:
16271 attr = dwarf2_attr (die, DW_AT_const_value, cu);
16272 if (attr)
16273 {
16274 dwarf2_const_value (attr, sym, cu);
16275 }
16276 {
16277 /* NOTE: carlton/2003-11-10: See comment above in the
16278 DW_TAG_class_type, etc. block. */
16279
16280 list_to_add = (cu->list_in_scope == &file_symbols
16281 && (cu->language == language_cplus
16282 || cu->language == language_java)
16283 ? &global_symbols : cu->list_in_scope);
16284 }
16285 break;
16286 case DW_TAG_namespace:
16287 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16288 list_to_add = &global_symbols;
16289 break;
16290 case DW_TAG_common_block:
16291 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
16292 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
16293 add_symbol_to_list (sym, cu->list_in_scope);
16294 break;
16295 default:
16296 /* Not a tag we recognize. Hopefully we aren't processing
16297 trash data, but since we must specifically ignore things
16298 we don't recognize, there is nothing else we should do at
16299 this point. */
16300 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
16301 dwarf_tag_name (die->tag));
16302 break;
16303 }
16304
16305 if (suppress_add)
16306 {
16307 sym->hash_next = objfile->template_symbols;
16308 objfile->template_symbols = sym;
16309 list_to_add = NULL;
16310 }
16311
16312 if (list_to_add != NULL)
16313 add_symbol_to_list (sym, list_to_add);
16314
16315 /* For the benefit of old versions of GCC, check for anonymous
16316 namespaces based on the demangled name. */
16317 if (!cu->processing_has_namespace_info
16318 && cu->language == language_cplus)
16319 cp_scan_for_anonymous_namespaces (sym, objfile);
16320 }
16321 return (sym);
16322 }
16323
16324 /* A wrapper for new_symbol_full that always allocates a new symbol. */
16325
16326 static struct symbol *
16327 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16328 {
16329 return new_symbol_full (die, type, cu, NULL);
16330 }
16331
16332 /* Given an attr with a DW_FORM_dataN value in host byte order,
16333 zero-extend it as appropriate for the symbol's type. The DWARF
16334 standard (v4) is not entirely clear about the meaning of using
16335 DW_FORM_dataN for a constant with a signed type, where the type is
16336 wider than the data. The conclusion of a discussion on the DWARF
16337 list was that this is unspecified. We choose to always zero-extend
16338 because that is the interpretation long in use by GCC. */
16339
16340 static gdb_byte *
16341 dwarf2_const_value_data (struct attribute *attr, struct type *type,
16342 const char *name, struct obstack *obstack,
16343 struct dwarf2_cu *cu, LONGEST *value, int bits)
16344 {
16345 struct objfile *objfile = cu->objfile;
16346 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
16347 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
16348 LONGEST l = DW_UNSND (attr);
16349
16350 if (bits < sizeof (*value) * 8)
16351 {
16352 l &= ((LONGEST) 1 << bits) - 1;
16353 *value = l;
16354 }
16355 else if (bits == sizeof (*value) * 8)
16356 *value = l;
16357 else
16358 {
16359 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
16360 store_unsigned_integer (bytes, bits / 8, byte_order, l);
16361 return bytes;
16362 }
16363
16364 return NULL;
16365 }
16366
16367 /* Read a constant value from an attribute. Either set *VALUE, or if
16368 the value does not fit in *VALUE, set *BYTES - either already
16369 allocated on the objfile obstack, or newly allocated on OBSTACK,
16370 or, set *BATON, if we translated the constant to a location
16371 expression. */
16372
16373 static void
16374 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
16375 const char *name, struct obstack *obstack,
16376 struct dwarf2_cu *cu,
16377 LONGEST *value, gdb_byte **bytes,
16378 struct dwarf2_locexpr_baton **baton)
16379 {
16380 struct objfile *objfile = cu->objfile;
16381 struct comp_unit_head *cu_header = &cu->header;
16382 struct dwarf_block *blk;
16383 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
16384 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
16385
16386 *value = 0;
16387 *bytes = NULL;
16388 *baton = NULL;
16389
16390 switch (attr->form)
16391 {
16392 case DW_FORM_addr:
16393 case DW_FORM_GNU_addr_index:
16394 {
16395 gdb_byte *data;
16396
16397 if (TYPE_LENGTH (type) != cu_header->addr_size)
16398 dwarf2_const_value_length_mismatch_complaint (name,
16399 cu_header->addr_size,
16400 TYPE_LENGTH (type));
16401 /* Symbols of this form are reasonably rare, so we just
16402 piggyback on the existing location code rather than writing
16403 a new implementation of symbol_computed_ops. */
16404 *baton = obstack_alloc (&objfile->objfile_obstack,
16405 sizeof (struct dwarf2_locexpr_baton));
16406 (*baton)->per_cu = cu->per_cu;
16407 gdb_assert ((*baton)->per_cu);
16408
16409 (*baton)->size = 2 + cu_header->addr_size;
16410 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
16411 (*baton)->data = data;
16412
16413 data[0] = DW_OP_addr;
16414 store_unsigned_integer (&data[1], cu_header->addr_size,
16415 byte_order, DW_ADDR (attr));
16416 data[cu_header->addr_size + 1] = DW_OP_stack_value;
16417 }
16418 break;
16419 case DW_FORM_string:
16420 case DW_FORM_strp:
16421 case DW_FORM_GNU_str_index:
16422 case DW_FORM_GNU_strp_alt:
16423 /* DW_STRING is already allocated on the objfile obstack, point
16424 directly to it. */
16425 *bytes = (gdb_byte *) DW_STRING (attr);
16426 break;
16427 case DW_FORM_block1:
16428 case DW_FORM_block2:
16429 case DW_FORM_block4:
16430 case DW_FORM_block:
16431 case DW_FORM_exprloc:
16432 blk = DW_BLOCK (attr);
16433 if (TYPE_LENGTH (type) != blk->size)
16434 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
16435 TYPE_LENGTH (type));
16436 *bytes = blk->data;
16437 break;
16438
16439 /* The DW_AT_const_value attributes are supposed to carry the
16440 symbol's value "represented as it would be on the target
16441 architecture." By the time we get here, it's already been
16442 converted to host endianness, so we just need to sign- or
16443 zero-extend it as appropriate. */
16444 case DW_FORM_data1:
16445 *bytes = dwarf2_const_value_data (attr, type, name,
16446 obstack, cu, value, 8);
16447 break;
16448 case DW_FORM_data2:
16449 *bytes = dwarf2_const_value_data (attr, type, name,
16450 obstack, cu, value, 16);
16451 break;
16452 case DW_FORM_data4:
16453 *bytes = dwarf2_const_value_data (attr, type, name,
16454 obstack, cu, value, 32);
16455 break;
16456 case DW_FORM_data8:
16457 *bytes = dwarf2_const_value_data (attr, type, name,
16458 obstack, cu, value, 64);
16459 break;
16460
16461 case DW_FORM_sdata:
16462 *value = DW_SND (attr);
16463 break;
16464
16465 case DW_FORM_udata:
16466 *value = DW_UNSND (attr);
16467 break;
16468
16469 default:
16470 complaint (&symfile_complaints,
16471 _("unsupported const value attribute form: '%s'"),
16472 dwarf_form_name (attr->form));
16473 *value = 0;
16474 break;
16475 }
16476 }
16477
16478
16479 /* Copy constant value from an attribute to a symbol. */
16480
16481 static void
16482 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
16483 struct dwarf2_cu *cu)
16484 {
16485 struct objfile *objfile = cu->objfile;
16486 struct comp_unit_head *cu_header = &cu->header;
16487 LONGEST value;
16488 gdb_byte *bytes;
16489 struct dwarf2_locexpr_baton *baton;
16490
16491 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
16492 SYMBOL_PRINT_NAME (sym),
16493 &objfile->objfile_obstack, cu,
16494 &value, &bytes, &baton);
16495
16496 if (baton != NULL)
16497 {
16498 SYMBOL_LOCATION_BATON (sym) = baton;
16499 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16500 }
16501 else if (bytes != NULL)
16502 {
16503 SYMBOL_VALUE_BYTES (sym) = bytes;
16504 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
16505 }
16506 else
16507 {
16508 SYMBOL_VALUE (sym) = value;
16509 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
16510 }
16511 }
16512
16513 /* Return the type of the die in question using its DW_AT_type attribute. */
16514
16515 static struct type *
16516 die_type (struct die_info *die, struct dwarf2_cu *cu)
16517 {
16518 struct attribute *type_attr;
16519
16520 type_attr = dwarf2_attr (die, DW_AT_type, cu);
16521 if (!type_attr)
16522 {
16523 /* A missing DW_AT_type represents a void type. */
16524 return objfile_type (cu->objfile)->builtin_void;
16525 }
16526
16527 return lookup_die_type (die, type_attr, cu);
16528 }
16529
16530 /* True iff CU's producer generates GNAT Ada auxiliary information
16531 that allows to find parallel types through that information instead
16532 of having to do expensive parallel lookups by type name. */
16533
16534 static int
16535 need_gnat_info (struct dwarf2_cu *cu)
16536 {
16537 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
16538 of GNAT produces this auxiliary information, without any indication
16539 that it is produced. Part of enhancing the FSF version of GNAT
16540 to produce that information will be to put in place an indicator
16541 that we can use in order to determine whether the descriptive type
16542 info is available or not. One suggestion that has been made is
16543 to use a new attribute, attached to the CU die. For now, assume
16544 that the descriptive type info is not available. */
16545 return 0;
16546 }
16547
16548 /* Return the auxiliary type of the die in question using its
16549 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
16550 attribute is not present. */
16551
16552 static struct type *
16553 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
16554 {
16555 struct attribute *type_attr;
16556
16557 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
16558 if (!type_attr)
16559 return NULL;
16560
16561 return lookup_die_type (die, type_attr, cu);
16562 }
16563
16564 /* If DIE has a descriptive_type attribute, then set the TYPE's
16565 descriptive type accordingly. */
16566
16567 static void
16568 set_descriptive_type (struct type *type, struct die_info *die,
16569 struct dwarf2_cu *cu)
16570 {
16571 struct type *descriptive_type = die_descriptive_type (die, cu);
16572
16573 if (descriptive_type)
16574 {
16575 ALLOCATE_GNAT_AUX_TYPE (type);
16576 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
16577 }
16578 }
16579
16580 /* Return the containing type of the die in question using its
16581 DW_AT_containing_type attribute. */
16582
16583 static struct type *
16584 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
16585 {
16586 struct attribute *type_attr;
16587
16588 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
16589 if (!type_attr)
16590 error (_("Dwarf Error: Problem turning containing type into gdb type "
16591 "[in module %s]"), cu->objfile->name);
16592
16593 return lookup_die_type (die, type_attr, cu);
16594 }
16595
16596 /* Look up the type of DIE in CU using its type attribute ATTR.
16597 If there is no type substitute an error marker. */
16598
16599 static struct type *
16600 lookup_die_type (struct die_info *die, struct attribute *attr,
16601 struct dwarf2_cu *cu)
16602 {
16603 struct objfile *objfile = cu->objfile;
16604 struct type *this_type;
16605
16606 /* First see if we have it cached. */
16607
16608 if (attr->form == DW_FORM_GNU_ref_alt)
16609 {
16610 struct dwarf2_per_cu_data *per_cu;
16611 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16612
16613 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
16614 this_type = get_die_type_at_offset (offset, per_cu);
16615 }
16616 else if (is_ref_attr (attr))
16617 {
16618 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16619
16620 this_type = get_die_type_at_offset (offset, cu->per_cu);
16621 }
16622 else if (attr->form == DW_FORM_ref_sig8)
16623 {
16624 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
16625
16626 /* sig_type will be NULL if the signatured type is missing from
16627 the debug info. */
16628 if (sig_type == NULL)
16629 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
16630 "at 0x%x [in module %s]"),
16631 die->offset.sect_off, objfile->name);
16632
16633 gdb_assert (sig_type->per_cu.is_debug_types);
16634 /* If we haven't filled in type_offset_in_section yet, then we
16635 haven't read the type in yet. */
16636 this_type = NULL;
16637 if (sig_type->type_offset_in_section.sect_off != 0)
16638 {
16639 this_type =
16640 get_die_type_at_offset (sig_type->type_offset_in_section,
16641 &sig_type->per_cu);
16642 }
16643 }
16644 else
16645 {
16646 dump_die_for_error (die);
16647 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
16648 dwarf_attr_name (attr->name), objfile->name);
16649 }
16650
16651 /* If not cached we need to read it in. */
16652
16653 if (this_type == NULL)
16654 {
16655 struct die_info *type_die;
16656 struct dwarf2_cu *type_cu = cu;
16657
16658 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
16659 /* If we found the type now, it's probably because the type came
16660 from an inter-CU reference and the type's CU got expanded before
16661 ours. */
16662 this_type = get_die_type (type_die, type_cu);
16663 if (this_type == NULL)
16664 this_type = read_type_die_1 (type_die, type_cu);
16665 }
16666
16667 /* If we still don't have a type use an error marker. */
16668
16669 if (this_type == NULL)
16670 {
16671 char *message, *saved;
16672
16673 /* read_type_die already issued a complaint. */
16674 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
16675 objfile->name,
16676 cu->header.offset.sect_off,
16677 die->offset.sect_off);
16678 saved = obstack_copy0 (&objfile->objfile_obstack,
16679 message, strlen (message));
16680 xfree (message);
16681
16682 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
16683 }
16684
16685 return this_type;
16686 }
16687
16688 /* Return the type in DIE, CU.
16689 Returns NULL for invalid types.
16690
16691 This first does a lookup in die_type_hash,
16692 and only reads the die in if necessary.
16693
16694 NOTE: This can be called when reading in partial or full symbols. */
16695
16696 static struct type *
16697 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
16698 {
16699 struct type *this_type;
16700
16701 this_type = get_die_type (die, cu);
16702 if (this_type)
16703 return this_type;
16704
16705 return read_type_die_1 (die, cu);
16706 }
16707
16708 /* Read the type in DIE, CU.
16709 Returns NULL for invalid types. */
16710
16711 static struct type *
16712 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
16713 {
16714 struct type *this_type = NULL;
16715
16716 switch (die->tag)
16717 {
16718 case DW_TAG_class_type:
16719 case DW_TAG_interface_type:
16720 case DW_TAG_structure_type:
16721 case DW_TAG_union_type:
16722 this_type = read_structure_type (die, cu);
16723 break;
16724 case DW_TAG_enumeration_type:
16725 this_type = read_enumeration_type (die, cu);
16726 break;
16727 case DW_TAG_subprogram:
16728 case DW_TAG_subroutine_type:
16729 case DW_TAG_inlined_subroutine:
16730 this_type = read_subroutine_type (die, cu);
16731 break;
16732 case DW_TAG_array_type:
16733 this_type = read_array_type (die, cu);
16734 break;
16735 case DW_TAG_set_type:
16736 this_type = read_set_type (die, cu);
16737 break;
16738 case DW_TAG_pointer_type:
16739 this_type = read_tag_pointer_type (die, cu);
16740 break;
16741 case DW_TAG_ptr_to_member_type:
16742 this_type = read_tag_ptr_to_member_type (die, cu);
16743 break;
16744 case DW_TAG_reference_type:
16745 this_type = read_tag_reference_type (die, cu);
16746 break;
16747 case DW_TAG_const_type:
16748 this_type = read_tag_const_type (die, cu);
16749 break;
16750 case DW_TAG_volatile_type:
16751 this_type = read_tag_volatile_type (die, cu);
16752 break;
16753 case DW_TAG_restrict_type:
16754 this_type = read_tag_restrict_type (die, cu);
16755 break;
16756 case DW_TAG_string_type:
16757 this_type = read_tag_string_type (die, cu);
16758 break;
16759 case DW_TAG_typedef:
16760 this_type = read_typedef (die, cu);
16761 break;
16762 case DW_TAG_subrange_type:
16763 this_type = read_subrange_type (die, cu);
16764 break;
16765 case DW_TAG_base_type:
16766 this_type = read_base_type (die, cu);
16767 break;
16768 case DW_TAG_unspecified_type:
16769 this_type = read_unspecified_type (die, cu);
16770 break;
16771 case DW_TAG_namespace:
16772 this_type = read_namespace_type (die, cu);
16773 break;
16774 case DW_TAG_module:
16775 this_type = read_module_type (die, cu);
16776 break;
16777 default:
16778 complaint (&symfile_complaints,
16779 _("unexpected tag in read_type_die: '%s'"),
16780 dwarf_tag_name (die->tag));
16781 break;
16782 }
16783
16784 return this_type;
16785 }
16786
16787 /* See if we can figure out if the class lives in a namespace. We do
16788 this by looking for a member function; its demangled name will
16789 contain namespace info, if there is any.
16790 Return the computed name or NULL.
16791 Space for the result is allocated on the objfile's obstack.
16792 This is the full-die version of guess_partial_die_structure_name.
16793 In this case we know DIE has no useful parent. */
16794
16795 static char *
16796 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
16797 {
16798 struct die_info *spec_die;
16799 struct dwarf2_cu *spec_cu;
16800 struct die_info *child;
16801
16802 spec_cu = cu;
16803 spec_die = die_specification (die, &spec_cu);
16804 if (spec_die != NULL)
16805 {
16806 die = spec_die;
16807 cu = spec_cu;
16808 }
16809
16810 for (child = die->child;
16811 child != NULL;
16812 child = child->sibling)
16813 {
16814 if (child->tag == DW_TAG_subprogram)
16815 {
16816 struct attribute *attr;
16817
16818 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
16819 if (attr == NULL)
16820 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
16821 if (attr != NULL)
16822 {
16823 char *actual_name
16824 = language_class_name_from_physname (cu->language_defn,
16825 DW_STRING (attr));
16826 char *name = NULL;
16827
16828 if (actual_name != NULL)
16829 {
16830 const char *die_name = dwarf2_name (die, cu);
16831
16832 if (die_name != NULL
16833 && strcmp (die_name, actual_name) != 0)
16834 {
16835 /* Strip off the class name from the full name.
16836 We want the prefix. */
16837 int die_name_len = strlen (die_name);
16838 int actual_name_len = strlen (actual_name);
16839
16840 /* Test for '::' as a sanity check. */
16841 if (actual_name_len > die_name_len + 2
16842 && actual_name[actual_name_len
16843 - die_name_len - 1] == ':')
16844 name =
16845 obstack_copy0 (&cu->objfile->objfile_obstack,
16846 actual_name,
16847 actual_name_len - die_name_len - 2);
16848 }
16849 }
16850 xfree (actual_name);
16851 return name;
16852 }
16853 }
16854 }
16855
16856 return NULL;
16857 }
16858
16859 /* GCC might emit a nameless typedef that has a linkage name. Determine the
16860 prefix part in such case. See
16861 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16862
16863 static char *
16864 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
16865 {
16866 struct attribute *attr;
16867 char *base;
16868
16869 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
16870 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
16871 return NULL;
16872
16873 attr = dwarf2_attr (die, DW_AT_name, cu);
16874 if (attr != NULL && DW_STRING (attr) != NULL)
16875 return NULL;
16876
16877 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16878 if (attr == NULL)
16879 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16880 if (attr == NULL || DW_STRING (attr) == NULL)
16881 return NULL;
16882
16883 /* dwarf2_name had to be already called. */
16884 gdb_assert (DW_STRING_IS_CANONICAL (attr));
16885
16886 /* Strip the base name, keep any leading namespaces/classes. */
16887 base = strrchr (DW_STRING (attr), ':');
16888 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
16889 return "";
16890
16891 return obstack_copy0 (&cu->objfile->objfile_obstack,
16892 DW_STRING (attr), &base[-1] - DW_STRING (attr));
16893 }
16894
16895 /* Return the name of the namespace/class that DIE is defined within,
16896 or "" if we can't tell. The caller should not xfree the result.
16897
16898 For example, if we're within the method foo() in the following
16899 code:
16900
16901 namespace N {
16902 class C {
16903 void foo () {
16904 }
16905 };
16906 }
16907
16908 then determine_prefix on foo's die will return "N::C". */
16909
16910 static const char *
16911 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
16912 {
16913 struct die_info *parent, *spec_die;
16914 struct dwarf2_cu *spec_cu;
16915 struct type *parent_type;
16916 char *retval;
16917
16918 if (cu->language != language_cplus && cu->language != language_java
16919 && cu->language != language_fortran)
16920 return "";
16921
16922 retval = anonymous_struct_prefix (die, cu);
16923 if (retval)
16924 return retval;
16925
16926 /* We have to be careful in the presence of DW_AT_specification.
16927 For example, with GCC 3.4, given the code
16928
16929 namespace N {
16930 void foo() {
16931 // Definition of N::foo.
16932 }
16933 }
16934
16935 then we'll have a tree of DIEs like this:
16936
16937 1: DW_TAG_compile_unit
16938 2: DW_TAG_namespace // N
16939 3: DW_TAG_subprogram // declaration of N::foo
16940 4: DW_TAG_subprogram // definition of N::foo
16941 DW_AT_specification // refers to die #3
16942
16943 Thus, when processing die #4, we have to pretend that we're in
16944 the context of its DW_AT_specification, namely the contex of die
16945 #3. */
16946 spec_cu = cu;
16947 spec_die = die_specification (die, &spec_cu);
16948 if (spec_die == NULL)
16949 parent = die->parent;
16950 else
16951 {
16952 parent = spec_die->parent;
16953 cu = spec_cu;
16954 }
16955
16956 if (parent == NULL)
16957 return "";
16958 else if (parent->building_fullname)
16959 {
16960 const char *name;
16961 const char *parent_name;
16962
16963 /* It has been seen on RealView 2.2 built binaries,
16964 DW_TAG_template_type_param types actually _defined_ as
16965 children of the parent class:
16966
16967 enum E {};
16968 template class <class Enum> Class{};
16969 Class<enum E> class_e;
16970
16971 1: DW_TAG_class_type (Class)
16972 2: DW_TAG_enumeration_type (E)
16973 3: DW_TAG_enumerator (enum1:0)
16974 3: DW_TAG_enumerator (enum2:1)
16975 ...
16976 2: DW_TAG_template_type_param
16977 DW_AT_type DW_FORM_ref_udata (E)
16978
16979 Besides being broken debug info, it can put GDB into an
16980 infinite loop. Consider:
16981
16982 When we're building the full name for Class<E>, we'll start
16983 at Class, and go look over its template type parameters,
16984 finding E. We'll then try to build the full name of E, and
16985 reach here. We're now trying to build the full name of E,
16986 and look over the parent DIE for containing scope. In the
16987 broken case, if we followed the parent DIE of E, we'd again
16988 find Class, and once again go look at its template type
16989 arguments, etc., etc. Simply don't consider such parent die
16990 as source-level parent of this die (it can't be, the language
16991 doesn't allow it), and break the loop here. */
16992 name = dwarf2_name (die, cu);
16993 parent_name = dwarf2_name (parent, cu);
16994 complaint (&symfile_complaints,
16995 _("template param type '%s' defined within parent '%s'"),
16996 name ? name : "<unknown>",
16997 parent_name ? parent_name : "<unknown>");
16998 return "";
16999 }
17000 else
17001 switch (parent->tag)
17002 {
17003 case DW_TAG_namespace:
17004 parent_type = read_type_die (parent, cu);
17005 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
17006 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
17007 Work around this problem here. */
17008 if (cu->language == language_cplus
17009 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
17010 return "";
17011 /* We give a name to even anonymous namespaces. */
17012 return TYPE_TAG_NAME (parent_type);
17013 case DW_TAG_class_type:
17014 case DW_TAG_interface_type:
17015 case DW_TAG_structure_type:
17016 case DW_TAG_union_type:
17017 case DW_TAG_module:
17018 parent_type = read_type_die (parent, cu);
17019 if (TYPE_TAG_NAME (parent_type) != NULL)
17020 return TYPE_TAG_NAME (parent_type);
17021 else
17022 /* An anonymous structure is only allowed non-static data
17023 members; no typedefs, no member functions, et cetera.
17024 So it does not need a prefix. */
17025 return "";
17026 case DW_TAG_compile_unit:
17027 case DW_TAG_partial_unit:
17028 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
17029 if (cu->language == language_cplus
17030 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
17031 && die->child != NULL
17032 && (die->tag == DW_TAG_class_type
17033 || die->tag == DW_TAG_structure_type
17034 || die->tag == DW_TAG_union_type))
17035 {
17036 char *name = guess_full_die_structure_name (die, cu);
17037 if (name != NULL)
17038 return name;
17039 }
17040 return "";
17041 default:
17042 return determine_prefix (parent, cu);
17043 }
17044 }
17045
17046 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
17047 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
17048 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
17049 an obconcat, otherwise allocate storage for the result. The CU argument is
17050 used to determine the language and hence, the appropriate separator. */
17051
17052 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
17053
17054 static char *
17055 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
17056 int physname, struct dwarf2_cu *cu)
17057 {
17058 const char *lead = "";
17059 const char *sep;
17060
17061 if (suffix == NULL || suffix[0] == '\0'
17062 || prefix == NULL || prefix[0] == '\0')
17063 sep = "";
17064 else if (cu->language == language_java)
17065 sep = ".";
17066 else if (cu->language == language_fortran && physname)
17067 {
17068 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
17069 DW_AT_MIPS_linkage_name is preferred and used instead. */
17070
17071 lead = "__";
17072 sep = "_MOD_";
17073 }
17074 else
17075 sep = "::";
17076
17077 if (prefix == NULL)
17078 prefix = "";
17079 if (suffix == NULL)
17080 suffix = "";
17081
17082 if (obs == NULL)
17083 {
17084 char *retval
17085 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
17086
17087 strcpy (retval, lead);
17088 strcat (retval, prefix);
17089 strcat (retval, sep);
17090 strcat (retval, suffix);
17091 return retval;
17092 }
17093 else
17094 {
17095 /* We have an obstack. */
17096 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
17097 }
17098 }
17099
17100 /* Return sibling of die, NULL if no sibling. */
17101
17102 static struct die_info *
17103 sibling_die (struct die_info *die)
17104 {
17105 return die->sibling;
17106 }
17107
17108 /* Get name of a die, return NULL if not found. */
17109
17110 static const char *
17111 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
17112 struct obstack *obstack)
17113 {
17114 if (name && cu->language == language_cplus)
17115 {
17116 char *canon_name = cp_canonicalize_string (name);
17117
17118 if (canon_name != NULL)
17119 {
17120 if (strcmp (canon_name, name) != 0)
17121 name = obstack_copy0 (obstack, canon_name, strlen (canon_name));
17122 xfree (canon_name);
17123 }
17124 }
17125
17126 return name;
17127 }
17128
17129 /* Get name of a die, return NULL if not found. */
17130
17131 static const char *
17132 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
17133 {
17134 struct attribute *attr;
17135
17136 attr = dwarf2_attr (die, DW_AT_name, cu);
17137 if ((!attr || !DW_STRING (attr))
17138 && die->tag != DW_TAG_class_type
17139 && die->tag != DW_TAG_interface_type
17140 && die->tag != DW_TAG_structure_type
17141 && die->tag != DW_TAG_union_type)
17142 return NULL;
17143
17144 switch (die->tag)
17145 {
17146 case DW_TAG_compile_unit:
17147 case DW_TAG_partial_unit:
17148 /* Compilation units have a DW_AT_name that is a filename, not
17149 a source language identifier. */
17150 case DW_TAG_enumeration_type:
17151 case DW_TAG_enumerator:
17152 /* These tags always have simple identifiers already; no need
17153 to canonicalize them. */
17154 return DW_STRING (attr);
17155
17156 case DW_TAG_subprogram:
17157 /* Java constructors will all be named "<init>", so return
17158 the class name when we see this special case. */
17159 if (cu->language == language_java
17160 && DW_STRING (attr) != NULL
17161 && strcmp (DW_STRING (attr), "<init>") == 0)
17162 {
17163 struct dwarf2_cu *spec_cu = cu;
17164 struct die_info *spec_die;
17165
17166 /* GCJ will output '<init>' for Java constructor names.
17167 For this special case, return the name of the parent class. */
17168
17169 /* GCJ may output suprogram DIEs with AT_specification set.
17170 If so, use the name of the specified DIE. */
17171 spec_die = die_specification (die, &spec_cu);
17172 if (spec_die != NULL)
17173 return dwarf2_name (spec_die, spec_cu);
17174
17175 do
17176 {
17177 die = die->parent;
17178 if (die->tag == DW_TAG_class_type)
17179 return dwarf2_name (die, cu);
17180 }
17181 while (die->tag != DW_TAG_compile_unit
17182 && die->tag != DW_TAG_partial_unit);
17183 }
17184 break;
17185
17186 case DW_TAG_class_type:
17187 case DW_TAG_interface_type:
17188 case DW_TAG_structure_type:
17189 case DW_TAG_union_type:
17190 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
17191 structures or unions. These were of the form "._%d" in GCC 4.1,
17192 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
17193 and GCC 4.4. We work around this problem by ignoring these. */
17194 if (attr && DW_STRING (attr)
17195 && (strncmp (DW_STRING (attr), "._", 2) == 0
17196 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
17197 return NULL;
17198
17199 /* GCC might emit a nameless typedef that has a linkage name. See
17200 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17201 if (!attr || DW_STRING (attr) == NULL)
17202 {
17203 char *demangled = NULL;
17204
17205 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
17206 if (attr == NULL)
17207 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
17208
17209 if (attr == NULL || DW_STRING (attr) == NULL)
17210 return NULL;
17211
17212 /* Avoid demangling DW_STRING (attr) the second time on a second
17213 call for the same DIE. */
17214 if (!DW_STRING_IS_CANONICAL (attr))
17215 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
17216
17217 if (demangled)
17218 {
17219 char *base;
17220
17221 /* FIXME: we already did this for the partial symbol... */
17222 DW_STRING (attr) = obstack_copy0 (&cu->objfile->objfile_obstack,
17223 demangled, strlen (demangled));
17224 DW_STRING_IS_CANONICAL (attr) = 1;
17225 xfree (demangled);
17226
17227 /* Strip any leading namespaces/classes, keep only the base name.
17228 DW_AT_name for named DIEs does not contain the prefixes. */
17229 base = strrchr (DW_STRING (attr), ':');
17230 if (base && base > DW_STRING (attr) && base[-1] == ':')
17231 return &base[1];
17232 else
17233 return DW_STRING (attr);
17234 }
17235 }
17236 break;
17237
17238 default:
17239 break;
17240 }
17241
17242 if (!DW_STRING_IS_CANONICAL (attr))
17243 {
17244 DW_STRING (attr)
17245 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
17246 &cu->objfile->objfile_obstack);
17247 DW_STRING_IS_CANONICAL (attr) = 1;
17248 }
17249 return DW_STRING (attr);
17250 }
17251
17252 /* Return the die that this die in an extension of, or NULL if there
17253 is none. *EXT_CU is the CU containing DIE on input, and the CU
17254 containing the return value on output. */
17255
17256 static struct die_info *
17257 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
17258 {
17259 struct attribute *attr;
17260
17261 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
17262 if (attr == NULL)
17263 return NULL;
17264
17265 return follow_die_ref (die, attr, ext_cu);
17266 }
17267
17268 /* Convert a DIE tag into its string name. */
17269
17270 static const char *
17271 dwarf_tag_name (unsigned tag)
17272 {
17273 const char *name = get_DW_TAG_name (tag);
17274
17275 if (name == NULL)
17276 return "DW_TAG_<unknown>";
17277
17278 return name;
17279 }
17280
17281 /* Convert a DWARF attribute code into its string name. */
17282
17283 static const char *
17284 dwarf_attr_name (unsigned attr)
17285 {
17286 const char *name;
17287
17288 #ifdef MIPS /* collides with DW_AT_HP_block_index */
17289 if (attr == DW_AT_MIPS_fde)
17290 return "DW_AT_MIPS_fde";
17291 #else
17292 if (attr == DW_AT_HP_block_index)
17293 return "DW_AT_HP_block_index";
17294 #endif
17295
17296 name = get_DW_AT_name (attr);
17297
17298 if (name == NULL)
17299 return "DW_AT_<unknown>";
17300
17301 return name;
17302 }
17303
17304 /* Convert a DWARF value form code into its string name. */
17305
17306 static const char *
17307 dwarf_form_name (unsigned form)
17308 {
17309 const char *name = get_DW_FORM_name (form);
17310
17311 if (name == NULL)
17312 return "DW_FORM_<unknown>";
17313
17314 return name;
17315 }
17316
17317 static char *
17318 dwarf_bool_name (unsigned mybool)
17319 {
17320 if (mybool)
17321 return "TRUE";
17322 else
17323 return "FALSE";
17324 }
17325
17326 /* Convert a DWARF type code into its string name. */
17327
17328 static const char *
17329 dwarf_type_encoding_name (unsigned enc)
17330 {
17331 const char *name = get_DW_ATE_name (enc);
17332
17333 if (name == NULL)
17334 return "DW_ATE_<unknown>";
17335
17336 return name;
17337 }
17338
17339 static void
17340 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
17341 {
17342 unsigned int i;
17343
17344 print_spaces (indent, f);
17345 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
17346 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
17347
17348 if (die->parent != NULL)
17349 {
17350 print_spaces (indent, f);
17351 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
17352 die->parent->offset.sect_off);
17353 }
17354
17355 print_spaces (indent, f);
17356 fprintf_unfiltered (f, " has children: %s\n",
17357 dwarf_bool_name (die->child != NULL));
17358
17359 print_spaces (indent, f);
17360 fprintf_unfiltered (f, " attributes:\n");
17361
17362 for (i = 0; i < die->num_attrs; ++i)
17363 {
17364 print_spaces (indent, f);
17365 fprintf_unfiltered (f, " %s (%s) ",
17366 dwarf_attr_name (die->attrs[i].name),
17367 dwarf_form_name (die->attrs[i].form));
17368
17369 switch (die->attrs[i].form)
17370 {
17371 case DW_FORM_addr:
17372 case DW_FORM_GNU_addr_index:
17373 fprintf_unfiltered (f, "address: ");
17374 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
17375 break;
17376 case DW_FORM_block2:
17377 case DW_FORM_block4:
17378 case DW_FORM_block:
17379 case DW_FORM_block1:
17380 fprintf_unfiltered (f, "block: size %s",
17381 pulongest (DW_BLOCK (&die->attrs[i])->size));
17382 break;
17383 case DW_FORM_exprloc:
17384 fprintf_unfiltered (f, "expression: size %s",
17385 pulongest (DW_BLOCK (&die->attrs[i])->size));
17386 break;
17387 case DW_FORM_ref_addr:
17388 fprintf_unfiltered (f, "ref address: ");
17389 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17390 break;
17391 case DW_FORM_GNU_ref_alt:
17392 fprintf_unfiltered (f, "alt ref address: ");
17393 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17394 break;
17395 case DW_FORM_ref1:
17396 case DW_FORM_ref2:
17397 case DW_FORM_ref4:
17398 case DW_FORM_ref8:
17399 case DW_FORM_ref_udata:
17400 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
17401 (long) (DW_UNSND (&die->attrs[i])));
17402 break;
17403 case DW_FORM_data1:
17404 case DW_FORM_data2:
17405 case DW_FORM_data4:
17406 case DW_FORM_data8:
17407 case DW_FORM_udata:
17408 case DW_FORM_sdata:
17409 fprintf_unfiltered (f, "constant: %s",
17410 pulongest (DW_UNSND (&die->attrs[i])));
17411 break;
17412 case DW_FORM_sec_offset:
17413 fprintf_unfiltered (f, "section offset: %s",
17414 pulongest (DW_UNSND (&die->attrs[i])));
17415 break;
17416 case DW_FORM_ref_sig8:
17417 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
17418 {
17419 struct signatured_type *sig_type =
17420 DW_SIGNATURED_TYPE (&die->attrs[i]);
17421
17422 fprintf_unfiltered (f, "signatured type: 0x%s, offset 0x%x",
17423 hex_string (sig_type->signature),
17424 sig_type->per_cu.offset.sect_off);
17425 }
17426 else
17427 fprintf_unfiltered (f, "signatured type, unknown");
17428 break;
17429 case DW_FORM_string:
17430 case DW_FORM_strp:
17431 case DW_FORM_GNU_str_index:
17432 case DW_FORM_GNU_strp_alt:
17433 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
17434 DW_STRING (&die->attrs[i])
17435 ? DW_STRING (&die->attrs[i]) : "",
17436 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
17437 break;
17438 case DW_FORM_flag:
17439 if (DW_UNSND (&die->attrs[i]))
17440 fprintf_unfiltered (f, "flag: TRUE");
17441 else
17442 fprintf_unfiltered (f, "flag: FALSE");
17443 break;
17444 case DW_FORM_flag_present:
17445 fprintf_unfiltered (f, "flag: TRUE");
17446 break;
17447 case DW_FORM_indirect:
17448 /* The reader will have reduced the indirect form to
17449 the "base form" so this form should not occur. */
17450 fprintf_unfiltered (f,
17451 "unexpected attribute form: DW_FORM_indirect");
17452 break;
17453 default:
17454 fprintf_unfiltered (f, "unsupported attribute form: %d.",
17455 die->attrs[i].form);
17456 break;
17457 }
17458 fprintf_unfiltered (f, "\n");
17459 }
17460 }
17461
17462 static void
17463 dump_die_for_error (struct die_info *die)
17464 {
17465 dump_die_shallow (gdb_stderr, 0, die);
17466 }
17467
17468 static void
17469 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
17470 {
17471 int indent = level * 4;
17472
17473 gdb_assert (die != NULL);
17474
17475 if (level >= max_level)
17476 return;
17477
17478 dump_die_shallow (f, indent, die);
17479
17480 if (die->child != NULL)
17481 {
17482 print_spaces (indent, f);
17483 fprintf_unfiltered (f, " Children:");
17484 if (level + 1 < max_level)
17485 {
17486 fprintf_unfiltered (f, "\n");
17487 dump_die_1 (f, level + 1, max_level, die->child);
17488 }
17489 else
17490 {
17491 fprintf_unfiltered (f,
17492 " [not printed, max nesting level reached]\n");
17493 }
17494 }
17495
17496 if (die->sibling != NULL && level > 0)
17497 {
17498 dump_die_1 (f, level, max_level, die->sibling);
17499 }
17500 }
17501
17502 /* This is called from the pdie macro in gdbinit.in.
17503 It's not static so gcc will keep a copy callable from gdb. */
17504
17505 void
17506 dump_die (struct die_info *die, int max_level)
17507 {
17508 dump_die_1 (gdb_stdlog, 0, max_level, die);
17509 }
17510
17511 static void
17512 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
17513 {
17514 void **slot;
17515
17516 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
17517 INSERT);
17518
17519 *slot = die;
17520 }
17521
17522 /* DW_ADDR is always stored already as sect_offset; despite for the forms
17523 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
17524
17525 static int
17526 is_ref_attr (struct attribute *attr)
17527 {
17528 switch (attr->form)
17529 {
17530 case DW_FORM_ref_addr:
17531 case DW_FORM_ref1:
17532 case DW_FORM_ref2:
17533 case DW_FORM_ref4:
17534 case DW_FORM_ref8:
17535 case DW_FORM_ref_udata:
17536 case DW_FORM_GNU_ref_alt:
17537 return 1;
17538 default:
17539 return 0;
17540 }
17541 }
17542
17543 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
17544 required kind. */
17545
17546 static sect_offset
17547 dwarf2_get_ref_die_offset (struct attribute *attr)
17548 {
17549 sect_offset retval = { DW_UNSND (attr) };
17550
17551 if (is_ref_attr (attr))
17552 return retval;
17553
17554 retval.sect_off = 0;
17555 complaint (&symfile_complaints,
17556 _("unsupported die ref attribute form: '%s'"),
17557 dwarf_form_name (attr->form));
17558 return retval;
17559 }
17560
17561 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
17562 * the value held by the attribute is not constant. */
17563
17564 static LONGEST
17565 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
17566 {
17567 if (attr->form == DW_FORM_sdata)
17568 return DW_SND (attr);
17569 else if (attr->form == DW_FORM_udata
17570 || attr->form == DW_FORM_data1
17571 || attr->form == DW_FORM_data2
17572 || attr->form == DW_FORM_data4
17573 || attr->form == DW_FORM_data8)
17574 return DW_UNSND (attr);
17575 else
17576 {
17577 complaint (&symfile_complaints,
17578 _("Attribute value is not a constant (%s)"),
17579 dwarf_form_name (attr->form));
17580 return default_value;
17581 }
17582 }
17583
17584 /* Follow reference or signature attribute ATTR of SRC_DIE.
17585 On entry *REF_CU is the CU of SRC_DIE.
17586 On exit *REF_CU is the CU of the result. */
17587
17588 static struct die_info *
17589 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
17590 struct dwarf2_cu **ref_cu)
17591 {
17592 struct die_info *die;
17593
17594 if (is_ref_attr (attr))
17595 die = follow_die_ref (src_die, attr, ref_cu);
17596 else if (attr->form == DW_FORM_ref_sig8)
17597 die = follow_die_sig (src_die, attr, ref_cu);
17598 else
17599 {
17600 dump_die_for_error (src_die);
17601 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
17602 (*ref_cu)->objfile->name);
17603 }
17604
17605 return die;
17606 }
17607
17608 /* Follow reference OFFSET.
17609 On entry *REF_CU is the CU of the source die referencing OFFSET.
17610 On exit *REF_CU is the CU of the result.
17611 Returns NULL if OFFSET is invalid. */
17612
17613 static struct die_info *
17614 follow_die_offset (sect_offset offset, int offset_in_dwz,
17615 struct dwarf2_cu **ref_cu)
17616 {
17617 struct die_info temp_die;
17618 struct dwarf2_cu *target_cu, *cu = *ref_cu;
17619
17620 gdb_assert (cu->per_cu != NULL);
17621
17622 target_cu = cu;
17623
17624 if (cu->per_cu->is_debug_types)
17625 {
17626 /* .debug_types CUs cannot reference anything outside their CU.
17627 If they need to, they have to reference a signatured type via
17628 DW_FORM_ref_sig8. */
17629 if (! offset_in_cu_p (&cu->header, offset))
17630 return NULL;
17631 }
17632 else if (offset_in_dwz != cu->per_cu->is_dwz
17633 || ! offset_in_cu_p (&cu->header, offset))
17634 {
17635 struct dwarf2_per_cu_data *per_cu;
17636
17637 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
17638 cu->objfile);
17639
17640 /* If necessary, add it to the queue and load its DIEs. */
17641 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
17642 load_full_comp_unit (per_cu, cu->language);
17643
17644 target_cu = per_cu->cu;
17645 }
17646 else if (cu->dies == NULL)
17647 {
17648 /* We're loading full DIEs during partial symbol reading. */
17649 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
17650 load_full_comp_unit (cu->per_cu, language_minimal);
17651 }
17652
17653 *ref_cu = target_cu;
17654 temp_die.offset = offset;
17655 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
17656 }
17657
17658 /* Follow reference attribute ATTR of SRC_DIE.
17659 On entry *REF_CU is the CU of SRC_DIE.
17660 On exit *REF_CU is the CU of the result. */
17661
17662 static struct die_info *
17663 follow_die_ref (struct die_info *src_die, struct attribute *attr,
17664 struct dwarf2_cu **ref_cu)
17665 {
17666 sect_offset offset = dwarf2_get_ref_die_offset (attr);
17667 struct dwarf2_cu *cu = *ref_cu;
17668 struct die_info *die;
17669
17670 die = follow_die_offset (offset,
17671 (attr->form == DW_FORM_GNU_ref_alt
17672 || cu->per_cu->is_dwz),
17673 ref_cu);
17674 if (!die)
17675 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
17676 "at 0x%x [in module %s]"),
17677 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
17678
17679 return die;
17680 }
17681
17682 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
17683 Returned value is intended for DW_OP_call*. Returned
17684 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
17685
17686 struct dwarf2_locexpr_baton
17687 dwarf2_fetch_die_loc_sect_off (sect_offset offset,
17688 struct dwarf2_per_cu_data *per_cu,
17689 CORE_ADDR (*get_frame_pc) (void *baton),
17690 void *baton)
17691 {
17692 struct dwarf2_cu *cu;
17693 struct die_info *die;
17694 struct attribute *attr;
17695 struct dwarf2_locexpr_baton retval;
17696
17697 dw2_setup (per_cu->objfile);
17698
17699 if (per_cu->cu == NULL)
17700 load_cu (per_cu);
17701 cu = per_cu->cu;
17702
17703 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
17704 if (!die)
17705 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
17706 offset.sect_off, per_cu->objfile->name);
17707
17708 attr = dwarf2_attr (die, DW_AT_location, cu);
17709 if (!attr)
17710 {
17711 /* DWARF: "If there is no such attribute, then there is no effect.".
17712 DATA is ignored if SIZE is 0. */
17713
17714 retval.data = NULL;
17715 retval.size = 0;
17716 }
17717 else if (attr_form_is_section_offset (attr))
17718 {
17719 struct dwarf2_loclist_baton loclist_baton;
17720 CORE_ADDR pc = (*get_frame_pc) (baton);
17721 size_t size;
17722
17723 fill_in_loclist_baton (cu, &loclist_baton, attr);
17724
17725 retval.data = dwarf2_find_location_expression (&loclist_baton,
17726 &size, pc);
17727 retval.size = size;
17728 }
17729 else
17730 {
17731 if (!attr_form_is_block (attr))
17732 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
17733 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
17734 offset.sect_off, per_cu->objfile->name);
17735
17736 retval.data = DW_BLOCK (attr)->data;
17737 retval.size = DW_BLOCK (attr)->size;
17738 }
17739 retval.per_cu = cu->per_cu;
17740
17741 age_cached_comp_units ();
17742
17743 return retval;
17744 }
17745
17746 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
17747 offset. */
17748
17749 struct dwarf2_locexpr_baton
17750 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
17751 struct dwarf2_per_cu_data *per_cu,
17752 CORE_ADDR (*get_frame_pc) (void *baton),
17753 void *baton)
17754 {
17755 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
17756
17757 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
17758 }
17759
17760 /* Return the type of the DIE at DIE_OFFSET in the CU named by
17761 PER_CU. */
17762
17763 struct type *
17764 dwarf2_get_die_type (cu_offset die_offset,
17765 struct dwarf2_per_cu_data *per_cu)
17766 {
17767 sect_offset die_offset_sect;
17768
17769 dw2_setup (per_cu->objfile);
17770
17771 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
17772 return get_die_type_at_offset (die_offset_sect, per_cu);
17773 }
17774
17775 /* Follow the signature attribute ATTR in SRC_DIE.
17776 On entry *REF_CU is the CU of SRC_DIE.
17777 On exit *REF_CU is the CU of the result. */
17778
17779 static struct die_info *
17780 follow_die_sig (struct die_info *src_die, struct attribute *attr,
17781 struct dwarf2_cu **ref_cu)
17782 {
17783 struct objfile *objfile = (*ref_cu)->objfile;
17784 struct die_info temp_die;
17785 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
17786 struct dwarf2_cu *sig_cu;
17787 struct die_info *die;
17788
17789 /* sig_type will be NULL if the signatured type is missing from
17790 the debug info. */
17791 if (sig_type == NULL)
17792 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
17793 "at 0x%x [in module %s]"),
17794 src_die->offset.sect_off, objfile->name);
17795
17796 /* If necessary, add it to the queue and load its DIEs. */
17797
17798 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
17799 read_signatured_type (sig_type);
17800
17801 gdb_assert (sig_type->per_cu.cu != NULL);
17802
17803 sig_cu = sig_type->per_cu.cu;
17804 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
17805 temp_die.offset = sig_type->type_offset_in_section;
17806 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
17807 temp_die.offset.sect_off);
17808 if (die)
17809 {
17810 /* For .gdb_index version 7 keep track of included TUs.
17811 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
17812 if (dwarf2_per_objfile->index_table != NULL
17813 && dwarf2_per_objfile->index_table->version <= 7)
17814 {
17815 VEC_safe_push (dwarf2_per_cu_ptr,
17816 (*ref_cu)->per_cu->imported_symtabs,
17817 sig_cu->per_cu);
17818 }
17819
17820 *ref_cu = sig_cu;
17821 return die;
17822 }
17823
17824 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
17825 "from DIE at 0x%x [in module %s]"),
17826 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
17827 }
17828
17829 /* Load the DIEs associated with type unit PER_CU into memory. */
17830
17831 static void
17832 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
17833 {
17834 struct signatured_type *sig_type;
17835
17836 /* Caller is responsible for ensuring type_unit_groups don't get here. */
17837 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
17838
17839 /* We have the per_cu, but we need the signatured_type.
17840 Fortunately this is an easy translation. */
17841 gdb_assert (per_cu->is_debug_types);
17842 sig_type = (struct signatured_type *) per_cu;
17843
17844 gdb_assert (per_cu->cu == NULL);
17845
17846 read_signatured_type (sig_type);
17847
17848 gdb_assert (per_cu->cu != NULL);
17849 }
17850
17851 /* die_reader_func for read_signatured_type.
17852 This is identical to load_full_comp_unit_reader,
17853 but is kept separate for now. */
17854
17855 static void
17856 read_signatured_type_reader (const struct die_reader_specs *reader,
17857 gdb_byte *info_ptr,
17858 struct die_info *comp_unit_die,
17859 int has_children,
17860 void *data)
17861 {
17862 struct dwarf2_cu *cu = reader->cu;
17863
17864 gdb_assert (cu->die_hash == NULL);
17865 cu->die_hash =
17866 htab_create_alloc_ex (cu->header.length / 12,
17867 die_hash,
17868 die_eq,
17869 NULL,
17870 &cu->comp_unit_obstack,
17871 hashtab_obstack_allocate,
17872 dummy_obstack_deallocate);
17873
17874 if (has_children)
17875 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
17876 &info_ptr, comp_unit_die);
17877 cu->dies = comp_unit_die;
17878 /* comp_unit_die is not stored in die_hash, no need. */
17879
17880 /* We try not to read any attributes in this function, because not
17881 all CUs needed for references have been loaded yet, and symbol
17882 table processing isn't initialized. But we have to set the CU language,
17883 or we won't be able to build types correctly.
17884 Similarly, if we do not read the producer, we can not apply
17885 producer-specific interpretation. */
17886 prepare_one_comp_unit (cu, cu->dies, language_minimal);
17887 }
17888
17889 /* Read in a signatured type and build its CU and DIEs.
17890 If the type is a stub for the real type in a DWO file,
17891 read in the real type from the DWO file as well. */
17892
17893 static void
17894 read_signatured_type (struct signatured_type *sig_type)
17895 {
17896 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
17897
17898 gdb_assert (per_cu->is_debug_types);
17899 gdb_assert (per_cu->cu == NULL);
17900
17901 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
17902 read_signatured_type_reader, NULL);
17903 }
17904
17905 /* Decode simple location descriptions.
17906 Given a pointer to a dwarf block that defines a location, compute
17907 the location and return the value.
17908
17909 NOTE drow/2003-11-18: This function is called in two situations
17910 now: for the address of static or global variables (partial symbols
17911 only) and for offsets into structures which are expected to be
17912 (more or less) constant. The partial symbol case should go away,
17913 and only the constant case should remain. That will let this
17914 function complain more accurately. A few special modes are allowed
17915 without complaint for global variables (for instance, global
17916 register values and thread-local values).
17917
17918 A location description containing no operations indicates that the
17919 object is optimized out. The return value is 0 for that case.
17920 FIXME drow/2003-11-16: No callers check for this case any more; soon all
17921 callers will only want a very basic result and this can become a
17922 complaint.
17923
17924 Note that stack[0] is unused except as a default error return. */
17925
17926 static CORE_ADDR
17927 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
17928 {
17929 struct objfile *objfile = cu->objfile;
17930 size_t i;
17931 size_t size = blk->size;
17932 gdb_byte *data = blk->data;
17933 CORE_ADDR stack[64];
17934 int stacki;
17935 unsigned int bytes_read, unsnd;
17936 gdb_byte op;
17937
17938 i = 0;
17939 stacki = 0;
17940 stack[stacki] = 0;
17941 stack[++stacki] = 0;
17942
17943 while (i < size)
17944 {
17945 op = data[i++];
17946 switch (op)
17947 {
17948 case DW_OP_lit0:
17949 case DW_OP_lit1:
17950 case DW_OP_lit2:
17951 case DW_OP_lit3:
17952 case DW_OP_lit4:
17953 case DW_OP_lit5:
17954 case DW_OP_lit6:
17955 case DW_OP_lit7:
17956 case DW_OP_lit8:
17957 case DW_OP_lit9:
17958 case DW_OP_lit10:
17959 case DW_OP_lit11:
17960 case DW_OP_lit12:
17961 case DW_OP_lit13:
17962 case DW_OP_lit14:
17963 case DW_OP_lit15:
17964 case DW_OP_lit16:
17965 case DW_OP_lit17:
17966 case DW_OP_lit18:
17967 case DW_OP_lit19:
17968 case DW_OP_lit20:
17969 case DW_OP_lit21:
17970 case DW_OP_lit22:
17971 case DW_OP_lit23:
17972 case DW_OP_lit24:
17973 case DW_OP_lit25:
17974 case DW_OP_lit26:
17975 case DW_OP_lit27:
17976 case DW_OP_lit28:
17977 case DW_OP_lit29:
17978 case DW_OP_lit30:
17979 case DW_OP_lit31:
17980 stack[++stacki] = op - DW_OP_lit0;
17981 break;
17982
17983 case DW_OP_reg0:
17984 case DW_OP_reg1:
17985 case DW_OP_reg2:
17986 case DW_OP_reg3:
17987 case DW_OP_reg4:
17988 case DW_OP_reg5:
17989 case DW_OP_reg6:
17990 case DW_OP_reg7:
17991 case DW_OP_reg8:
17992 case DW_OP_reg9:
17993 case DW_OP_reg10:
17994 case DW_OP_reg11:
17995 case DW_OP_reg12:
17996 case DW_OP_reg13:
17997 case DW_OP_reg14:
17998 case DW_OP_reg15:
17999 case DW_OP_reg16:
18000 case DW_OP_reg17:
18001 case DW_OP_reg18:
18002 case DW_OP_reg19:
18003 case DW_OP_reg20:
18004 case DW_OP_reg21:
18005 case DW_OP_reg22:
18006 case DW_OP_reg23:
18007 case DW_OP_reg24:
18008 case DW_OP_reg25:
18009 case DW_OP_reg26:
18010 case DW_OP_reg27:
18011 case DW_OP_reg28:
18012 case DW_OP_reg29:
18013 case DW_OP_reg30:
18014 case DW_OP_reg31:
18015 stack[++stacki] = op - DW_OP_reg0;
18016 if (i < size)
18017 dwarf2_complex_location_expr_complaint ();
18018 break;
18019
18020 case DW_OP_regx:
18021 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
18022 i += bytes_read;
18023 stack[++stacki] = unsnd;
18024 if (i < size)
18025 dwarf2_complex_location_expr_complaint ();
18026 break;
18027
18028 case DW_OP_addr:
18029 stack[++stacki] = read_address (objfile->obfd, &data[i],
18030 cu, &bytes_read);
18031 i += bytes_read;
18032 break;
18033
18034 case DW_OP_const1u:
18035 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
18036 i += 1;
18037 break;
18038
18039 case DW_OP_const1s:
18040 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
18041 i += 1;
18042 break;
18043
18044 case DW_OP_const2u:
18045 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
18046 i += 2;
18047 break;
18048
18049 case DW_OP_const2s:
18050 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
18051 i += 2;
18052 break;
18053
18054 case DW_OP_const4u:
18055 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
18056 i += 4;
18057 break;
18058
18059 case DW_OP_const4s:
18060 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
18061 i += 4;
18062 break;
18063
18064 case DW_OP_const8u:
18065 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
18066 i += 8;
18067 break;
18068
18069 case DW_OP_constu:
18070 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
18071 &bytes_read);
18072 i += bytes_read;
18073 break;
18074
18075 case DW_OP_consts:
18076 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
18077 i += bytes_read;
18078 break;
18079
18080 case DW_OP_dup:
18081 stack[stacki + 1] = stack[stacki];
18082 stacki++;
18083 break;
18084
18085 case DW_OP_plus:
18086 stack[stacki - 1] += stack[stacki];
18087 stacki--;
18088 break;
18089
18090 case DW_OP_plus_uconst:
18091 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
18092 &bytes_read);
18093 i += bytes_read;
18094 break;
18095
18096 case DW_OP_minus:
18097 stack[stacki - 1] -= stack[stacki];
18098 stacki--;
18099 break;
18100
18101 case DW_OP_deref:
18102 /* If we're not the last op, then we definitely can't encode
18103 this using GDB's address_class enum. This is valid for partial
18104 global symbols, although the variable's address will be bogus
18105 in the psymtab. */
18106 if (i < size)
18107 dwarf2_complex_location_expr_complaint ();
18108 break;
18109
18110 case DW_OP_GNU_push_tls_address:
18111 /* The top of the stack has the offset from the beginning
18112 of the thread control block at which the variable is located. */
18113 /* Nothing should follow this operator, so the top of stack would
18114 be returned. */
18115 /* This is valid for partial global symbols, but the variable's
18116 address will be bogus in the psymtab. Make it always at least
18117 non-zero to not look as a variable garbage collected by linker
18118 which have DW_OP_addr 0. */
18119 if (i < size)
18120 dwarf2_complex_location_expr_complaint ();
18121 stack[stacki]++;
18122 break;
18123
18124 case DW_OP_GNU_uninit:
18125 break;
18126
18127 case DW_OP_GNU_addr_index:
18128 case DW_OP_GNU_const_index:
18129 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
18130 &bytes_read);
18131 i += bytes_read;
18132 break;
18133
18134 default:
18135 {
18136 const char *name = get_DW_OP_name (op);
18137
18138 if (name)
18139 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
18140 name);
18141 else
18142 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
18143 op);
18144 }
18145
18146 return (stack[stacki]);
18147 }
18148
18149 /* Enforce maximum stack depth of SIZE-1 to avoid writing
18150 outside of the allocated space. Also enforce minimum>0. */
18151 if (stacki >= ARRAY_SIZE (stack) - 1)
18152 {
18153 complaint (&symfile_complaints,
18154 _("location description stack overflow"));
18155 return 0;
18156 }
18157
18158 if (stacki <= 0)
18159 {
18160 complaint (&symfile_complaints,
18161 _("location description stack underflow"));
18162 return 0;
18163 }
18164 }
18165 return (stack[stacki]);
18166 }
18167
18168 /* memory allocation interface */
18169
18170 static struct dwarf_block *
18171 dwarf_alloc_block (struct dwarf2_cu *cu)
18172 {
18173 struct dwarf_block *blk;
18174
18175 blk = (struct dwarf_block *)
18176 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
18177 return (blk);
18178 }
18179
18180 static struct die_info *
18181 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
18182 {
18183 struct die_info *die;
18184 size_t size = sizeof (struct die_info);
18185
18186 if (num_attrs > 1)
18187 size += (num_attrs - 1) * sizeof (struct attribute);
18188
18189 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
18190 memset (die, 0, sizeof (struct die_info));
18191 return (die);
18192 }
18193
18194 \f
18195 /* Macro support. */
18196
18197 /* Return file name relative to the compilation directory of file number I in
18198 *LH's file name table. The result is allocated using xmalloc; the caller is
18199 responsible for freeing it. */
18200
18201 static char *
18202 file_file_name (int file, struct line_header *lh)
18203 {
18204 /* Is the file number a valid index into the line header's file name
18205 table? Remember that file numbers start with one, not zero. */
18206 if (1 <= file && file <= lh->num_file_names)
18207 {
18208 struct file_entry *fe = &lh->file_names[file - 1];
18209
18210 if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0)
18211 return xstrdup (fe->name);
18212 return concat (lh->include_dirs[fe->dir_index - 1], SLASH_STRING,
18213 fe->name, NULL);
18214 }
18215 else
18216 {
18217 /* The compiler produced a bogus file number. We can at least
18218 record the macro definitions made in the file, even if we
18219 won't be able to find the file by name. */
18220 char fake_name[80];
18221
18222 xsnprintf (fake_name, sizeof (fake_name),
18223 "<bad macro file number %d>", file);
18224
18225 complaint (&symfile_complaints,
18226 _("bad file number in macro information (%d)"),
18227 file);
18228
18229 return xstrdup (fake_name);
18230 }
18231 }
18232
18233 /* Return the full name of file number I in *LH's file name table.
18234 Use COMP_DIR as the name of the current directory of the
18235 compilation. The result is allocated using xmalloc; the caller is
18236 responsible for freeing it. */
18237 static char *
18238 file_full_name (int file, struct line_header *lh, const char *comp_dir)
18239 {
18240 /* Is the file number a valid index into the line header's file name
18241 table? Remember that file numbers start with one, not zero. */
18242 if (1 <= file && file <= lh->num_file_names)
18243 {
18244 char *relative = file_file_name (file, lh);
18245
18246 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
18247 return relative;
18248 return reconcat (relative, comp_dir, SLASH_STRING, relative, NULL);
18249 }
18250 else
18251 return file_file_name (file, lh);
18252 }
18253
18254
18255 static struct macro_source_file *
18256 macro_start_file (int file, int line,
18257 struct macro_source_file *current_file,
18258 const char *comp_dir,
18259 struct line_header *lh, struct objfile *objfile)
18260 {
18261 /* File name relative to the compilation directory of this source file. */
18262 char *file_name = file_file_name (file, lh);
18263
18264 /* We don't create a macro table for this compilation unit
18265 at all until we actually get a filename. */
18266 if (! pending_macros)
18267 pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
18268 objfile->per_bfd->macro_cache,
18269 comp_dir);
18270
18271 if (! current_file)
18272 {
18273 /* If we have no current file, then this must be the start_file
18274 directive for the compilation unit's main source file. */
18275 current_file = macro_set_main (pending_macros, file_name);
18276 macro_define_special (pending_macros);
18277 }
18278 else
18279 current_file = macro_include (current_file, line, file_name);
18280
18281 xfree (file_name);
18282
18283 return current_file;
18284 }
18285
18286
18287 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
18288 followed by a null byte. */
18289 static char *
18290 copy_string (const char *buf, int len)
18291 {
18292 char *s = xmalloc (len + 1);
18293
18294 memcpy (s, buf, len);
18295 s[len] = '\0';
18296 return s;
18297 }
18298
18299
18300 static const char *
18301 consume_improper_spaces (const char *p, const char *body)
18302 {
18303 if (*p == ' ')
18304 {
18305 complaint (&symfile_complaints,
18306 _("macro definition contains spaces "
18307 "in formal argument list:\n`%s'"),
18308 body);
18309
18310 while (*p == ' ')
18311 p++;
18312 }
18313
18314 return p;
18315 }
18316
18317
18318 static void
18319 parse_macro_definition (struct macro_source_file *file, int line,
18320 const char *body)
18321 {
18322 const char *p;
18323
18324 /* The body string takes one of two forms. For object-like macro
18325 definitions, it should be:
18326
18327 <macro name> " " <definition>
18328
18329 For function-like macro definitions, it should be:
18330
18331 <macro name> "() " <definition>
18332 or
18333 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
18334
18335 Spaces may appear only where explicitly indicated, and in the
18336 <definition>.
18337
18338 The Dwarf 2 spec says that an object-like macro's name is always
18339 followed by a space, but versions of GCC around March 2002 omit
18340 the space when the macro's definition is the empty string.
18341
18342 The Dwarf 2 spec says that there should be no spaces between the
18343 formal arguments in a function-like macro's formal argument list,
18344 but versions of GCC around March 2002 include spaces after the
18345 commas. */
18346
18347
18348 /* Find the extent of the macro name. The macro name is terminated
18349 by either a space or null character (for an object-like macro) or
18350 an opening paren (for a function-like macro). */
18351 for (p = body; *p; p++)
18352 if (*p == ' ' || *p == '(')
18353 break;
18354
18355 if (*p == ' ' || *p == '\0')
18356 {
18357 /* It's an object-like macro. */
18358 int name_len = p - body;
18359 char *name = copy_string (body, name_len);
18360 const char *replacement;
18361
18362 if (*p == ' ')
18363 replacement = body + name_len + 1;
18364 else
18365 {
18366 dwarf2_macro_malformed_definition_complaint (body);
18367 replacement = body + name_len;
18368 }
18369
18370 macro_define_object (file, line, name, replacement);
18371
18372 xfree (name);
18373 }
18374 else if (*p == '(')
18375 {
18376 /* It's a function-like macro. */
18377 char *name = copy_string (body, p - body);
18378 int argc = 0;
18379 int argv_size = 1;
18380 char **argv = xmalloc (argv_size * sizeof (*argv));
18381
18382 p++;
18383
18384 p = consume_improper_spaces (p, body);
18385
18386 /* Parse the formal argument list. */
18387 while (*p && *p != ')')
18388 {
18389 /* Find the extent of the current argument name. */
18390 const char *arg_start = p;
18391
18392 while (*p && *p != ',' && *p != ')' && *p != ' ')
18393 p++;
18394
18395 if (! *p || p == arg_start)
18396 dwarf2_macro_malformed_definition_complaint (body);
18397 else
18398 {
18399 /* Make sure argv has room for the new argument. */
18400 if (argc >= argv_size)
18401 {
18402 argv_size *= 2;
18403 argv = xrealloc (argv, argv_size * sizeof (*argv));
18404 }
18405
18406 argv[argc++] = copy_string (arg_start, p - arg_start);
18407 }
18408
18409 p = consume_improper_spaces (p, body);
18410
18411 /* Consume the comma, if present. */
18412 if (*p == ',')
18413 {
18414 p++;
18415
18416 p = consume_improper_spaces (p, body);
18417 }
18418 }
18419
18420 if (*p == ')')
18421 {
18422 p++;
18423
18424 if (*p == ' ')
18425 /* Perfectly formed definition, no complaints. */
18426 macro_define_function (file, line, name,
18427 argc, (const char **) argv,
18428 p + 1);
18429 else if (*p == '\0')
18430 {
18431 /* Complain, but do define it. */
18432 dwarf2_macro_malformed_definition_complaint (body);
18433 macro_define_function (file, line, name,
18434 argc, (const char **) argv,
18435 p);
18436 }
18437 else
18438 /* Just complain. */
18439 dwarf2_macro_malformed_definition_complaint (body);
18440 }
18441 else
18442 /* Just complain. */
18443 dwarf2_macro_malformed_definition_complaint (body);
18444
18445 xfree (name);
18446 {
18447 int i;
18448
18449 for (i = 0; i < argc; i++)
18450 xfree (argv[i]);
18451 }
18452 xfree (argv);
18453 }
18454 else
18455 dwarf2_macro_malformed_definition_complaint (body);
18456 }
18457
18458 /* Skip some bytes from BYTES according to the form given in FORM.
18459 Returns the new pointer. */
18460
18461 static gdb_byte *
18462 skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
18463 enum dwarf_form form,
18464 unsigned int offset_size,
18465 struct dwarf2_section_info *section)
18466 {
18467 unsigned int bytes_read;
18468
18469 switch (form)
18470 {
18471 case DW_FORM_data1:
18472 case DW_FORM_flag:
18473 ++bytes;
18474 break;
18475
18476 case DW_FORM_data2:
18477 bytes += 2;
18478 break;
18479
18480 case DW_FORM_data4:
18481 bytes += 4;
18482 break;
18483
18484 case DW_FORM_data8:
18485 bytes += 8;
18486 break;
18487
18488 case DW_FORM_string:
18489 read_direct_string (abfd, bytes, &bytes_read);
18490 bytes += bytes_read;
18491 break;
18492
18493 case DW_FORM_sec_offset:
18494 case DW_FORM_strp:
18495 case DW_FORM_GNU_strp_alt:
18496 bytes += offset_size;
18497 break;
18498
18499 case DW_FORM_block:
18500 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
18501 bytes += bytes_read;
18502 break;
18503
18504 case DW_FORM_block1:
18505 bytes += 1 + read_1_byte (abfd, bytes);
18506 break;
18507 case DW_FORM_block2:
18508 bytes += 2 + read_2_bytes (abfd, bytes);
18509 break;
18510 case DW_FORM_block4:
18511 bytes += 4 + read_4_bytes (abfd, bytes);
18512 break;
18513
18514 case DW_FORM_sdata:
18515 case DW_FORM_udata:
18516 case DW_FORM_GNU_addr_index:
18517 case DW_FORM_GNU_str_index:
18518 bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
18519 if (bytes == NULL)
18520 {
18521 dwarf2_section_buffer_overflow_complaint (section);
18522 return NULL;
18523 }
18524 break;
18525
18526 default:
18527 {
18528 complain:
18529 complaint (&symfile_complaints,
18530 _("invalid form 0x%x in `%s'"),
18531 form,
18532 section->asection->name);
18533 return NULL;
18534 }
18535 }
18536
18537 return bytes;
18538 }
18539
18540 /* A helper for dwarf_decode_macros that handles skipping an unknown
18541 opcode. Returns an updated pointer to the macro data buffer; or,
18542 on error, issues a complaint and returns NULL. */
18543
18544 static gdb_byte *
18545 skip_unknown_opcode (unsigned int opcode,
18546 gdb_byte **opcode_definitions,
18547 gdb_byte *mac_ptr, gdb_byte *mac_end,
18548 bfd *abfd,
18549 unsigned int offset_size,
18550 struct dwarf2_section_info *section)
18551 {
18552 unsigned int bytes_read, i;
18553 unsigned long arg;
18554 gdb_byte *defn;
18555
18556 if (opcode_definitions[opcode] == NULL)
18557 {
18558 complaint (&symfile_complaints,
18559 _("unrecognized DW_MACFINO opcode 0x%x"),
18560 opcode);
18561 return NULL;
18562 }
18563
18564 defn = opcode_definitions[opcode];
18565 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
18566 defn += bytes_read;
18567
18568 for (i = 0; i < arg; ++i)
18569 {
18570 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
18571 section);
18572 if (mac_ptr == NULL)
18573 {
18574 /* skip_form_bytes already issued the complaint. */
18575 return NULL;
18576 }
18577 }
18578
18579 return mac_ptr;
18580 }
18581
18582 /* A helper function which parses the header of a macro section.
18583 If the macro section is the extended (for now called "GNU") type,
18584 then this updates *OFFSET_SIZE. Returns a pointer to just after
18585 the header, or issues a complaint and returns NULL on error. */
18586
18587 static gdb_byte *
18588 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
18589 bfd *abfd,
18590 gdb_byte *mac_ptr,
18591 unsigned int *offset_size,
18592 int section_is_gnu)
18593 {
18594 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
18595
18596 if (section_is_gnu)
18597 {
18598 unsigned int version, flags;
18599
18600 version = read_2_bytes (abfd, mac_ptr);
18601 if (version != 4)
18602 {
18603 complaint (&symfile_complaints,
18604 _("unrecognized version `%d' in .debug_macro section"),
18605 version);
18606 return NULL;
18607 }
18608 mac_ptr += 2;
18609
18610 flags = read_1_byte (abfd, mac_ptr);
18611 ++mac_ptr;
18612 *offset_size = (flags & 1) ? 8 : 4;
18613
18614 if ((flags & 2) != 0)
18615 /* We don't need the line table offset. */
18616 mac_ptr += *offset_size;
18617
18618 /* Vendor opcode descriptions. */
18619 if ((flags & 4) != 0)
18620 {
18621 unsigned int i, count;
18622
18623 count = read_1_byte (abfd, mac_ptr);
18624 ++mac_ptr;
18625 for (i = 0; i < count; ++i)
18626 {
18627 unsigned int opcode, bytes_read;
18628 unsigned long arg;
18629
18630 opcode = read_1_byte (abfd, mac_ptr);
18631 ++mac_ptr;
18632 opcode_definitions[opcode] = mac_ptr;
18633 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18634 mac_ptr += bytes_read;
18635 mac_ptr += arg;
18636 }
18637 }
18638 }
18639
18640 return mac_ptr;
18641 }
18642
18643 /* A helper for dwarf_decode_macros that handles the GNU extensions,
18644 including DW_MACRO_GNU_transparent_include. */
18645
18646 static void
18647 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
18648 struct macro_source_file *current_file,
18649 struct line_header *lh, const char *comp_dir,
18650 struct dwarf2_section_info *section,
18651 int section_is_gnu, int section_is_dwz,
18652 unsigned int offset_size,
18653 struct objfile *objfile,
18654 htab_t include_hash)
18655 {
18656 enum dwarf_macro_record_type macinfo_type;
18657 int at_commandline;
18658 gdb_byte *opcode_definitions[256];
18659
18660 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18661 &offset_size, section_is_gnu);
18662 if (mac_ptr == NULL)
18663 {
18664 /* We already issued a complaint. */
18665 return;
18666 }
18667
18668 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
18669 GDB is still reading the definitions from command line. First
18670 DW_MACINFO_start_file will need to be ignored as it was already executed
18671 to create CURRENT_FILE for the main source holding also the command line
18672 definitions. On first met DW_MACINFO_start_file this flag is reset to
18673 normally execute all the remaining DW_MACINFO_start_file macinfos. */
18674
18675 at_commandline = 1;
18676
18677 do
18678 {
18679 /* Do we at least have room for a macinfo type byte? */
18680 if (mac_ptr >= mac_end)
18681 {
18682 dwarf2_section_buffer_overflow_complaint (section);
18683 break;
18684 }
18685
18686 macinfo_type = read_1_byte (abfd, mac_ptr);
18687 mac_ptr++;
18688
18689 /* Note that we rely on the fact that the corresponding GNU and
18690 DWARF constants are the same. */
18691 switch (macinfo_type)
18692 {
18693 /* A zero macinfo type indicates the end of the macro
18694 information. */
18695 case 0:
18696 break;
18697
18698 case DW_MACRO_GNU_define:
18699 case DW_MACRO_GNU_undef:
18700 case DW_MACRO_GNU_define_indirect:
18701 case DW_MACRO_GNU_undef_indirect:
18702 case DW_MACRO_GNU_define_indirect_alt:
18703 case DW_MACRO_GNU_undef_indirect_alt:
18704 {
18705 unsigned int bytes_read;
18706 int line;
18707 char *body;
18708 int is_define;
18709
18710 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18711 mac_ptr += bytes_read;
18712
18713 if (macinfo_type == DW_MACRO_GNU_define
18714 || macinfo_type == DW_MACRO_GNU_undef)
18715 {
18716 body = read_direct_string (abfd, mac_ptr, &bytes_read);
18717 mac_ptr += bytes_read;
18718 }
18719 else
18720 {
18721 LONGEST str_offset;
18722
18723 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
18724 mac_ptr += offset_size;
18725
18726 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
18727 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
18728 || section_is_dwz)
18729 {
18730 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18731
18732 body = read_indirect_string_from_dwz (dwz, str_offset);
18733 }
18734 else
18735 body = read_indirect_string_at_offset (abfd, str_offset);
18736 }
18737
18738 is_define = (macinfo_type == DW_MACRO_GNU_define
18739 || macinfo_type == DW_MACRO_GNU_define_indirect
18740 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
18741 if (! current_file)
18742 {
18743 /* DWARF violation as no main source is present. */
18744 complaint (&symfile_complaints,
18745 _("debug info with no main source gives macro %s "
18746 "on line %d: %s"),
18747 is_define ? _("definition") : _("undefinition"),
18748 line, body);
18749 break;
18750 }
18751 if ((line == 0 && !at_commandline)
18752 || (line != 0 && at_commandline))
18753 complaint (&symfile_complaints,
18754 _("debug info gives %s macro %s with %s line %d: %s"),
18755 at_commandline ? _("command-line") : _("in-file"),
18756 is_define ? _("definition") : _("undefinition"),
18757 line == 0 ? _("zero") : _("non-zero"), line, body);
18758
18759 if (is_define)
18760 parse_macro_definition (current_file, line, body);
18761 else
18762 {
18763 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
18764 || macinfo_type == DW_MACRO_GNU_undef_indirect
18765 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
18766 macro_undef (current_file, line, body);
18767 }
18768 }
18769 break;
18770
18771 case DW_MACRO_GNU_start_file:
18772 {
18773 unsigned int bytes_read;
18774 int line, file;
18775
18776 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18777 mac_ptr += bytes_read;
18778 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18779 mac_ptr += bytes_read;
18780
18781 if ((line == 0 && !at_commandline)
18782 || (line != 0 && at_commandline))
18783 complaint (&symfile_complaints,
18784 _("debug info gives source %d included "
18785 "from %s at %s line %d"),
18786 file, at_commandline ? _("command-line") : _("file"),
18787 line == 0 ? _("zero") : _("non-zero"), line);
18788
18789 if (at_commandline)
18790 {
18791 /* This DW_MACRO_GNU_start_file was executed in the
18792 pass one. */
18793 at_commandline = 0;
18794 }
18795 else
18796 current_file = macro_start_file (file, line,
18797 current_file, comp_dir,
18798 lh, objfile);
18799 }
18800 break;
18801
18802 case DW_MACRO_GNU_end_file:
18803 if (! current_file)
18804 complaint (&symfile_complaints,
18805 _("macro debug info has an unmatched "
18806 "`close_file' directive"));
18807 else
18808 {
18809 current_file = current_file->included_by;
18810 if (! current_file)
18811 {
18812 enum dwarf_macro_record_type next_type;
18813
18814 /* GCC circa March 2002 doesn't produce the zero
18815 type byte marking the end of the compilation
18816 unit. Complain if it's not there, but exit no
18817 matter what. */
18818
18819 /* Do we at least have room for a macinfo type byte? */
18820 if (mac_ptr >= mac_end)
18821 {
18822 dwarf2_section_buffer_overflow_complaint (section);
18823 return;
18824 }
18825
18826 /* We don't increment mac_ptr here, so this is just
18827 a look-ahead. */
18828 next_type = read_1_byte (abfd, mac_ptr);
18829 if (next_type != 0)
18830 complaint (&symfile_complaints,
18831 _("no terminating 0-type entry for "
18832 "macros in `.debug_macinfo' section"));
18833
18834 return;
18835 }
18836 }
18837 break;
18838
18839 case DW_MACRO_GNU_transparent_include:
18840 case DW_MACRO_GNU_transparent_include_alt:
18841 {
18842 LONGEST offset;
18843 void **slot;
18844 bfd *include_bfd = abfd;
18845 struct dwarf2_section_info *include_section = section;
18846 struct dwarf2_section_info alt_section;
18847 gdb_byte *include_mac_end = mac_end;
18848 int is_dwz = section_is_dwz;
18849 gdb_byte *new_mac_ptr;
18850
18851 offset = read_offset_1 (abfd, mac_ptr, offset_size);
18852 mac_ptr += offset_size;
18853
18854 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
18855 {
18856 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18857
18858 dwarf2_read_section (dwarf2_per_objfile->objfile,
18859 &dwz->macro);
18860
18861 include_bfd = dwz->macro.asection->owner;
18862 include_section = &dwz->macro;
18863 include_mac_end = dwz->macro.buffer + dwz->macro.size;
18864 is_dwz = 1;
18865 }
18866
18867 new_mac_ptr = include_section->buffer + offset;
18868 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
18869
18870 if (*slot != NULL)
18871 {
18872 /* This has actually happened; see
18873 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
18874 complaint (&symfile_complaints,
18875 _("recursive DW_MACRO_GNU_transparent_include in "
18876 ".debug_macro section"));
18877 }
18878 else
18879 {
18880 *slot = new_mac_ptr;
18881
18882 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
18883 include_mac_end, current_file,
18884 lh, comp_dir,
18885 section, section_is_gnu, is_dwz,
18886 offset_size, objfile, include_hash);
18887
18888 htab_remove_elt (include_hash, new_mac_ptr);
18889 }
18890 }
18891 break;
18892
18893 case DW_MACINFO_vendor_ext:
18894 if (!section_is_gnu)
18895 {
18896 unsigned int bytes_read;
18897 int constant;
18898
18899 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18900 mac_ptr += bytes_read;
18901 read_direct_string (abfd, mac_ptr, &bytes_read);
18902 mac_ptr += bytes_read;
18903
18904 /* We don't recognize any vendor extensions. */
18905 break;
18906 }
18907 /* FALLTHROUGH */
18908
18909 default:
18910 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
18911 mac_ptr, mac_end, abfd, offset_size,
18912 section);
18913 if (mac_ptr == NULL)
18914 return;
18915 break;
18916 }
18917 } while (macinfo_type != 0);
18918 }
18919
18920 static void
18921 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
18922 const char *comp_dir, int section_is_gnu)
18923 {
18924 struct objfile *objfile = dwarf2_per_objfile->objfile;
18925 struct line_header *lh = cu->line_header;
18926 bfd *abfd;
18927 gdb_byte *mac_ptr, *mac_end;
18928 struct macro_source_file *current_file = 0;
18929 enum dwarf_macro_record_type macinfo_type;
18930 unsigned int offset_size = cu->header.offset_size;
18931 gdb_byte *opcode_definitions[256];
18932 struct cleanup *cleanup;
18933 htab_t include_hash;
18934 void **slot;
18935 struct dwarf2_section_info *section;
18936 const char *section_name;
18937
18938 if (cu->dwo_unit != NULL)
18939 {
18940 if (section_is_gnu)
18941 {
18942 section = &cu->dwo_unit->dwo_file->sections.macro;
18943 section_name = ".debug_macro.dwo";
18944 }
18945 else
18946 {
18947 section = &cu->dwo_unit->dwo_file->sections.macinfo;
18948 section_name = ".debug_macinfo.dwo";
18949 }
18950 }
18951 else
18952 {
18953 if (section_is_gnu)
18954 {
18955 section = &dwarf2_per_objfile->macro;
18956 section_name = ".debug_macro";
18957 }
18958 else
18959 {
18960 section = &dwarf2_per_objfile->macinfo;
18961 section_name = ".debug_macinfo";
18962 }
18963 }
18964
18965 dwarf2_read_section (objfile, section);
18966 if (section->buffer == NULL)
18967 {
18968 complaint (&symfile_complaints, _("missing %s section"), section_name);
18969 return;
18970 }
18971 abfd = section->asection->owner;
18972
18973 /* First pass: Find the name of the base filename.
18974 This filename is needed in order to process all macros whose definition
18975 (or undefinition) comes from the command line. These macros are defined
18976 before the first DW_MACINFO_start_file entry, and yet still need to be
18977 associated to the base file.
18978
18979 To determine the base file name, we scan the macro definitions until we
18980 reach the first DW_MACINFO_start_file entry. We then initialize
18981 CURRENT_FILE accordingly so that any macro definition found before the
18982 first DW_MACINFO_start_file can still be associated to the base file. */
18983
18984 mac_ptr = section->buffer + offset;
18985 mac_end = section->buffer + section->size;
18986
18987 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18988 &offset_size, section_is_gnu);
18989 if (mac_ptr == NULL)
18990 {
18991 /* We already issued a complaint. */
18992 return;
18993 }
18994
18995 do
18996 {
18997 /* Do we at least have room for a macinfo type byte? */
18998 if (mac_ptr >= mac_end)
18999 {
19000 /* Complaint is printed during the second pass as GDB will probably
19001 stop the first pass earlier upon finding
19002 DW_MACINFO_start_file. */
19003 break;
19004 }
19005
19006 macinfo_type = read_1_byte (abfd, mac_ptr);
19007 mac_ptr++;
19008
19009 /* Note that we rely on the fact that the corresponding GNU and
19010 DWARF constants are the same. */
19011 switch (macinfo_type)
19012 {
19013 /* A zero macinfo type indicates the end of the macro
19014 information. */
19015 case 0:
19016 break;
19017
19018 case DW_MACRO_GNU_define:
19019 case DW_MACRO_GNU_undef:
19020 /* Only skip the data by MAC_PTR. */
19021 {
19022 unsigned int bytes_read;
19023
19024 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19025 mac_ptr += bytes_read;
19026 read_direct_string (abfd, mac_ptr, &bytes_read);
19027 mac_ptr += bytes_read;
19028 }
19029 break;
19030
19031 case DW_MACRO_GNU_start_file:
19032 {
19033 unsigned int bytes_read;
19034 int line, file;
19035
19036 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19037 mac_ptr += bytes_read;
19038 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19039 mac_ptr += bytes_read;
19040
19041 current_file = macro_start_file (file, line, current_file,
19042 comp_dir, lh, objfile);
19043 }
19044 break;
19045
19046 case DW_MACRO_GNU_end_file:
19047 /* No data to skip by MAC_PTR. */
19048 break;
19049
19050 case DW_MACRO_GNU_define_indirect:
19051 case DW_MACRO_GNU_undef_indirect:
19052 case DW_MACRO_GNU_define_indirect_alt:
19053 case DW_MACRO_GNU_undef_indirect_alt:
19054 {
19055 unsigned int bytes_read;
19056
19057 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19058 mac_ptr += bytes_read;
19059 mac_ptr += offset_size;
19060 }
19061 break;
19062
19063 case DW_MACRO_GNU_transparent_include:
19064 case DW_MACRO_GNU_transparent_include_alt:
19065 /* Note that, according to the spec, a transparent include
19066 chain cannot call DW_MACRO_GNU_start_file. So, we can just
19067 skip this opcode. */
19068 mac_ptr += offset_size;
19069 break;
19070
19071 case DW_MACINFO_vendor_ext:
19072 /* Only skip the data by MAC_PTR. */
19073 if (!section_is_gnu)
19074 {
19075 unsigned int bytes_read;
19076
19077 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19078 mac_ptr += bytes_read;
19079 read_direct_string (abfd, mac_ptr, &bytes_read);
19080 mac_ptr += bytes_read;
19081 }
19082 /* FALLTHROUGH */
19083
19084 default:
19085 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
19086 mac_ptr, mac_end, abfd, offset_size,
19087 section);
19088 if (mac_ptr == NULL)
19089 return;
19090 break;
19091 }
19092 } while (macinfo_type != 0 && current_file == NULL);
19093
19094 /* Second pass: Process all entries.
19095
19096 Use the AT_COMMAND_LINE flag to determine whether we are still processing
19097 command-line macro definitions/undefinitions. This flag is unset when we
19098 reach the first DW_MACINFO_start_file entry. */
19099
19100 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
19101 NULL, xcalloc, xfree);
19102 cleanup = make_cleanup_htab_delete (include_hash);
19103 mac_ptr = section->buffer + offset;
19104 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
19105 *slot = mac_ptr;
19106 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
19107 current_file, lh, comp_dir, section,
19108 section_is_gnu, 0,
19109 offset_size, objfile, include_hash);
19110 do_cleanups (cleanup);
19111 }
19112
19113 /* Check if the attribute's form is a DW_FORM_block*
19114 if so return true else false. */
19115
19116 static int
19117 attr_form_is_block (struct attribute *attr)
19118 {
19119 return (attr == NULL ? 0 :
19120 attr->form == DW_FORM_block1
19121 || attr->form == DW_FORM_block2
19122 || attr->form == DW_FORM_block4
19123 || attr->form == DW_FORM_block
19124 || attr->form == DW_FORM_exprloc);
19125 }
19126
19127 /* Return non-zero if ATTR's value is a section offset --- classes
19128 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
19129 You may use DW_UNSND (attr) to retrieve such offsets.
19130
19131 Section 7.5.4, "Attribute Encodings", explains that no attribute
19132 may have a value that belongs to more than one of these classes; it
19133 would be ambiguous if we did, because we use the same forms for all
19134 of them. */
19135
19136 static int
19137 attr_form_is_section_offset (struct attribute *attr)
19138 {
19139 return (attr->form == DW_FORM_data4
19140 || attr->form == DW_FORM_data8
19141 || attr->form == DW_FORM_sec_offset);
19142 }
19143
19144 /* Return non-zero if ATTR's value falls in the 'constant' class, or
19145 zero otherwise. When this function returns true, you can apply
19146 dwarf2_get_attr_constant_value to it.
19147
19148 However, note that for some attributes you must check
19149 attr_form_is_section_offset before using this test. DW_FORM_data4
19150 and DW_FORM_data8 are members of both the constant class, and of
19151 the classes that contain offsets into other debug sections
19152 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
19153 that, if an attribute's can be either a constant or one of the
19154 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
19155 taken as section offsets, not constants. */
19156
19157 static int
19158 attr_form_is_constant (struct attribute *attr)
19159 {
19160 switch (attr->form)
19161 {
19162 case DW_FORM_sdata:
19163 case DW_FORM_udata:
19164 case DW_FORM_data1:
19165 case DW_FORM_data2:
19166 case DW_FORM_data4:
19167 case DW_FORM_data8:
19168 return 1;
19169 default:
19170 return 0;
19171 }
19172 }
19173
19174 /* Return the .debug_loc section to use for CU.
19175 For DWO files use .debug_loc.dwo. */
19176
19177 static struct dwarf2_section_info *
19178 cu_debug_loc_section (struct dwarf2_cu *cu)
19179 {
19180 if (cu->dwo_unit)
19181 return &cu->dwo_unit->dwo_file->sections.loc;
19182 return &dwarf2_per_objfile->loc;
19183 }
19184
19185 /* A helper function that fills in a dwarf2_loclist_baton. */
19186
19187 static void
19188 fill_in_loclist_baton (struct dwarf2_cu *cu,
19189 struct dwarf2_loclist_baton *baton,
19190 struct attribute *attr)
19191 {
19192 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19193
19194 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
19195
19196 baton->per_cu = cu->per_cu;
19197 gdb_assert (baton->per_cu);
19198 /* We don't know how long the location list is, but make sure we
19199 don't run off the edge of the section. */
19200 baton->size = section->size - DW_UNSND (attr);
19201 baton->data = section->buffer + DW_UNSND (attr);
19202 baton->base_address = cu->base_address;
19203 baton->from_dwo = cu->dwo_unit != NULL;
19204 }
19205
19206 static void
19207 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
19208 struct dwarf2_cu *cu, int is_block)
19209 {
19210 struct objfile *objfile = dwarf2_per_objfile->objfile;
19211 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19212
19213 if (attr_form_is_section_offset (attr)
19214 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
19215 the section. If so, fall through to the complaint in the
19216 other branch. */
19217 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
19218 {
19219 struct dwarf2_loclist_baton *baton;
19220
19221 baton = obstack_alloc (&objfile->objfile_obstack,
19222 sizeof (struct dwarf2_loclist_baton));
19223
19224 fill_in_loclist_baton (cu, baton, attr);
19225
19226 if (cu->base_known == 0)
19227 complaint (&symfile_complaints,
19228 _("Location list used without "
19229 "specifying the CU base address."));
19230
19231 SYMBOL_ACLASS_INDEX (sym) = (is_block
19232 ? dwarf2_loclist_block_index
19233 : dwarf2_loclist_index);
19234 SYMBOL_LOCATION_BATON (sym) = baton;
19235 }
19236 else
19237 {
19238 struct dwarf2_locexpr_baton *baton;
19239
19240 baton = obstack_alloc (&objfile->objfile_obstack,
19241 sizeof (struct dwarf2_locexpr_baton));
19242 baton->per_cu = cu->per_cu;
19243 gdb_assert (baton->per_cu);
19244
19245 if (attr_form_is_block (attr))
19246 {
19247 /* Note that we're just copying the block's data pointer
19248 here, not the actual data. We're still pointing into the
19249 info_buffer for SYM's objfile; right now we never release
19250 that buffer, but when we do clean up properly this may
19251 need to change. */
19252 baton->size = DW_BLOCK (attr)->size;
19253 baton->data = DW_BLOCK (attr)->data;
19254 }
19255 else
19256 {
19257 dwarf2_invalid_attrib_class_complaint ("location description",
19258 SYMBOL_NATURAL_NAME (sym));
19259 baton->size = 0;
19260 }
19261
19262 SYMBOL_ACLASS_INDEX (sym) = (is_block
19263 ? dwarf2_locexpr_block_index
19264 : dwarf2_locexpr_index);
19265 SYMBOL_LOCATION_BATON (sym) = baton;
19266 }
19267 }
19268
19269 /* Return the OBJFILE associated with the compilation unit CU. If CU
19270 came from a separate debuginfo file, then the master objfile is
19271 returned. */
19272
19273 struct objfile *
19274 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
19275 {
19276 struct objfile *objfile = per_cu->objfile;
19277
19278 /* Return the master objfile, so that we can report and look up the
19279 correct file containing this variable. */
19280 if (objfile->separate_debug_objfile_backlink)
19281 objfile = objfile->separate_debug_objfile_backlink;
19282
19283 return objfile;
19284 }
19285
19286 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
19287 (CU_HEADERP is unused in such case) or prepare a temporary copy at
19288 CU_HEADERP first. */
19289
19290 static const struct comp_unit_head *
19291 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
19292 struct dwarf2_per_cu_data *per_cu)
19293 {
19294 gdb_byte *info_ptr;
19295
19296 if (per_cu->cu)
19297 return &per_cu->cu->header;
19298
19299 info_ptr = per_cu->section->buffer + per_cu->offset.sect_off;
19300
19301 memset (cu_headerp, 0, sizeof (*cu_headerp));
19302 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
19303
19304 return cu_headerp;
19305 }
19306
19307 /* Return the address size given in the compilation unit header for CU. */
19308
19309 int
19310 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
19311 {
19312 struct comp_unit_head cu_header_local;
19313 const struct comp_unit_head *cu_headerp;
19314
19315 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19316
19317 return cu_headerp->addr_size;
19318 }
19319
19320 /* Return the offset size given in the compilation unit header for CU. */
19321
19322 int
19323 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
19324 {
19325 struct comp_unit_head cu_header_local;
19326 const struct comp_unit_head *cu_headerp;
19327
19328 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19329
19330 return cu_headerp->offset_size;
19331 }
19332
19333 /* See its dwarf2loc.h declaration. */
19334
19335 int
19336 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
19337 {
19338 struct comp_unit_head cu_header_local;
19339 const struct comp_unit_head *cu_headerp;
19340
19341 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19342
19343 if (cu_headerp->version == 2)
19344 return cu_headerp->addr_size;
19345 else
19346 return cu_headerp->offset_size;
19347 }
19348
19349 /* Return the text offset of the CU. The returned offset comes from
19350 this CU's objfile. If this objfile came from a separate debuginfo
19351 file, then the offset may be different from the corresponding
19352 offset in the parent objfile. */
19353
19354 CORE_ADDR
19355 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
19356 {
19357 struct objfile *objfile = per_cu->objfile;
19358
19359 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19360 }
19361
19362 /* Locate the .debug_info compilation unit from CU's objfile which contains
19363 the DIE at OFFSET. Raises an error on failure. */
19364
19365 static struct dwarf2_per_cu_data *
19366 dwarf2_find_containing_comp_unit (sect_offset offset,
19367 unsigned int offset_in_dwz,
19368 struct objfile *objfile)
19369 {
19370 struct dwarf2_per_cu_data *this_cu;
19371 int low, high;
19372 const sect_offset *cu_off;
19373
19374 low = 0;
19375 high = dwarf2_per_objfile->n_comp_units - 1;
19376 while (high > low)
19377 {
19378 struct dwarf2_per_cu_data *mid_cu;
19379 int mid = low + (high - low) / 2;
19380
19381 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
19382 cu_off = &mid_cu->offset;
19383 if (mid_cu->is_dwz > offset_in_dwz
19384 || (mid_cu->is_dwz == offset_in_dwz
19385 && cu_off->sect_off >= offset.sect_off))
19386 high = mid;
19387 else
19388 low = mid + 1;
19389 }
19390 gdb_assert (low == high);
19391 this_cu = dwarf2_per_objfile->all_comp_units[low];
19392 cu_off = &this_cu->offset;
19393 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
19394 {
19395 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
19396 error (_("Dwarf Error: could not find partial DIE containing "
19397 "offset 0x%lx [in module %s]"),
19398 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
19399
19400 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
19401 <= offset.sect_off);
19402 return dwarf2_per_objfile->all_comp_units[low-1];
19403 }
19404 else
19405 {
19406 this_cu = dwarf2_per_objfile->all_comp_units[low];
19407 if (low == dwarf2_per_objfile->n_comp_units - 1
19408 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
19409 error (_("invalid dwarf2 offset %u"), offset.sect_off);
19410 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
19411 return this_cu;
19412 }
19413 }
19414
19415 /* Initialize dwarf2_cu CU, owned by PER_CU. */
19416
19417 static void
19418 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
19419 {
19420 memset (cu, 0, sizeof (*cu));
19421 per_cu->cu = cu;
19422 cu->per_cu = per_cu;
19423 cu->objfile = per_cu->objfile;
19424 obstack_init (&cu->comp_unit_obstack);
19425 }
19426
19427 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
19428
19429 static void
19430 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
19431 enum language pretend_language)
19432 {
19433 struct attribute *attr;
19434
19435 /* Set the language we're debugging. */
19436 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
19437 if (attr)
19438 set_cu_language (DW_UNSND (attr), cu);
19439 else
19440 {
19441 cu->language = pretend_language;
19442 cu->language_defn = language_def (cu->language);
19443 }
19444
19445 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
19446 if (attr)
19447 cu->producer = DW_STRING (attr);
19448 }
19449
19450 /* Release one cached compilation unit, CU. We unlink it from the tree
19451 of compilation units, but we don't remove it from the read_in_chain;
19452 the caller is responsible for that.
19453 NOTE: DATA is a void * because this function is also used as a
19454 cleanup routine. */
19455
19456 static void
19457 free_heap_comp_unit (void *data)
19458 {
19459 struct dwarf2_cu *cu = data;
19460
19461 gdb_assert (cu->per_cu != NULL);
19462 cu->per_cu->cu = NULL;
19463 cu->per_cu = NULL;
19464
19465 obstack_free (&cu->comp_unit_obstack, NULL);
19466
19467 xfree (cu);
19468 }
19469
19470 /* This cleanup function is passed the address of a dwarf2_cu on the stack
19471 when we're finished with it. We can't free the pointer itself, but be
19472 sure to unlink it from the cache. Also release any associated storage. */
19473
19474 static void
19475 free_stack_comp_unit (void *data)
19476 {
19477 struct dwarf2_cu *cu = data;
19478
19479 gdb_assert (cu->per_cu != NULL);
19480 cu->per_cu->cu = NULL;
19481 cu->per_cu = NULL;
19482
19483 obstack_free (&cu->comp_unit_obstack, NULL);
19484 cu->partial_dies = NULL;
19485 }
19486
19487 /* Free all cached compilation units. */
19488
19489 static void
19490 free_cached_comp_units (void *data)
19491 {
19492 struct dwarf2_per_cu_data *per_cu, **last_chain;
19493
19494 per_cu = dwarf2_per_objfile->read_in_chain;
19495 last_chain = &dwarf2_per_objfile->read_in_chain;
19496 while (per_cu != NULL)
19497 {
19498 struct dwarf2_per_cu_data *next_cu;
19499
19500 next_cu = per_cu->cu->read_in_chain;
19501
19502 free_heap_comp_unit (per_cu->cu);
19503 *last_chain = next_cu;
19504
19505 per_cu = next_cu;
19506 }
19507 }
19508
19509 /* Increase the age counter on each cached compilation unit, and free
19510 any that are too old. */
19511
19512 static void
19513 age_cached_comp_units (void)
19514 {
19515 struct dwarf2_per_cu_data *per_cu, **last_chain;
19516
19517 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
19518 per_cu = dwarf2_per_objfile->read_in_chain;
19519 while (per_cu != NULL)
19520 {
19521 per_cu->cu->last_used ++;
19522 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
19523 dwarf2_mark (per_cu->cu);
19524 per_cu = per_cu->cu->read_in_chain;
19525 }
19526
19527 per_cu = dwarf2_per_objfile->read_in_chain;
19528 last_chain = &dwarf2_per_objfile->read_in_chain;
19529 while (per_cu != NULL)
19530 {
19531 struct dwarf2_per_cu_data *next_cu;
19532
19533 next_cu = per_cu->cu->read_in_chain;
19534
19535 if (!per_cu->cu->mark)
19536 {
19537 free_heap_comp_unit (per_cu->cu);
19538 *last_chain = next_cu;
19539 }
19540 else
19541 last_chain = &per_cu->cu->read_in_chain;
19542
19543 per_cu = next_cu;
19544 }
19545 }
19546
19547 /* Remove a single compilation unit from the cache. */
19548
19549 static void
19550 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
19551 {
19552 struct dwarf2_per_cu_data *per_cu, **last_chain;
19553
19554 per_cu = dwarf2_per_objfile->read_in_chain;
19555 last_chain = &dwarf2_per_objfile->read_in_chain;
19556 while (per_cu != NULL)
19557 {
19558 struct dwarf2_per_cu_data *next_cu;
19559
19560 next_cu = per_cu->cu->read_in_chain;
19561
19562 if (per_cu == target_per_cu)
19563 {
19564 free_heap_comp_unit (per_cu->cu);
19565 per_cu->cu = NULL;
19566 *last_chain = next_cu;
19567 break;
19568 }
19569 else
19570 last_chain = &per_cu->cu->read_in_chain;
19571
19572 per_cu = next_cu;
19573 }
19574 }
19575
19576 /* Release all extra memory associated with OBJFILE. */
19577
19578 void
19579 dwarf2_free_objfile (struct objfile *objfile)
19580 {
19581 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
19582
19583 if (dwarf2_per_objfile == NULL)
19584 return;
19585
19586 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
19587 free_cached_comp_units (NULL);
19588
19589 if (dwarf2_per_objfile->quick_file_names_table)
19590 htab_delete (dwarf2_per_objfile->quick_file_names_table);
19591
19592 /* Everything else should be on the objfile obstack. */
19593 }
19594
19595 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
19596 We store these in a hash table separate from the DIEs, and preserve them
19597 when the DIEs are flushed out of cache.
19598
19599 The CU "per_cu" pointer is needed because offset alone is not enough to
19600 uniquely identify the type. A file may have multiple .debug_types sections,
19601 or the type may come from a DWO file. Furthermore, while it's more logical
19602 to use per_cu->section+offset, with Fission the section with the data is in
19603 the DWO file but we don't know that section at the point we need it.
19604 We have to use something in dwarf2_per_cu_data (or the pointer to it)
19605 because we can enter the lookup routine, get_die_type_at_offset, from
19606 outside this file, and thus won't necessarily have PER_CU->cu.
19607 Fortunately, PER_CU is stable for the life of the objfile. */
19608
19609 struct dwarf2_per_cu_offset_and_type
19610 {
19611 const struct dwarf2_per_cu_data *per_cu;
19612 sect_offset offset;
19613 struct type *type;
19614 };
19615
19616 /* Hash function for a dwarf2_per_cu_offset_and_type. */
19617
19618 static hashval_t
19619 per_cu_offset_and_type_hash (const void *item)
19620 {
19621 const struct dwarf2_per_cu_offset_and_type *ofs = item;
19622
19623 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
19624 }
19625
19626 /* Equality function for a dwarf2_per_cu_offset_and_type. */
19627
19628 static int
19629 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
19630 {
19631 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
19632 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
19633
19634 return (ofs_lhs->per_cu == ofs_rhs->per_cu
19635 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
19636 }
19637
19638 /* Set the type associated with DIE to TYPE. Save it in CU's hash
19639 table if necessary. For convenience, return TYPE.
19640
19641 The DIEs reading must have careful ordering to:
19642 * Not cause infite loops trying to read in DIEs as a prerequisite for
19643 reading current DIE.
19644 * Not trying to dereference contents of still incompletely read in types
19645 while reading in other DIEs.
19646 * Enable referencing still incompletely read in types just by a pointer to
19647 the type without accessing its fields.
19648
19649 Therefore caller should follow these rules:
19650 * Try to fetch any prerequisite types we may need to build this DIE type
19651 before building the type and calling set_die_type.
19652 * After building type call set_die_type for current DIE as soon as
19653 possible before fetching more types to complete the current type.
19654 * Make the type as complete as possible before fetching more types. */
19655
19656 static struct type *
19657 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19658 {
19659 struct dwarf2_per_cu_offset_and_type **slot, ofs;
19660 struct objfile *objfile = cu->objfile;
19661
19662 /* For Ada types, make sure that the gnat-specific data is always
19663 initialized (if not already set). There are a few types where
19664 we should not be doing so, because the type-specific area is
19665 already used to hold some other piece of info (eg: TYPE_CODE_FLT
19666 where the type-specific area is used to store the floatformat).
19667 But this is not a problem, because the gnat-specific information
19668 is actually not needed for these types. */
19669 if (need_gnat_info (cu)
19670 && TYPE_CODE (type) != TYPE_CODE_FUNC
19671 && TYPE_CODE (type) != TYPE_CODE_FLT
19672 && !HAVE_GNAT_AUX_INFO (type))
19673 INIT_GNAT_SPECIFIC (type);
19674
19675 if (dwarf2_per_objfile->die_type_hash == NULL)
19676 {
19677 dwarf2_per_objfile->die_type_hash =
19678 htab_create_alloc_ex (127,
19679 per_cu_offset_and_type_hash,
19680 per_cu_offset_and_type_eq,
19681 NULL,
19682 &objfile->objfile_obstack,
19683 hashtab_obstack_allocate,
19684 dummy_obstack_deallocate);
19685 }
19686
19687 ofs.per_cu = cu->per_cu;
19688 ofs.offset = die->offset;
19689 ofs.type = type;
19690 slot = (struct dwarf2_per_cu_offset_and_type **)
19691 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
19692 if (*slot)
19693 complaint (&symfile_complaints,
19694 _("A problem internal to GDB: DIE 0x%x has type already set"),
19695 die->offset.sect_off);
19696 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
19697 **slot = ofs;
19698 return type;
19699 }
19700
19701 /* Look up the type for the die at OFFSET in PER_CU in die_type_hash,
19702 or return NULL if the die does not have a saved type. */
19703
19704 static struct type *
19705 get_die_type_at_offset (sect_offset offset,
19706 struct dwarf2_per_cu_data *per_cu)
19707 {
19708 struct dwarf2_per_cu_offset_and_type *slot, ofs;
19709
19710 if (dwarf2_per_objfile->die_type_hash == NULL)
19711 return NULL;
19712
19713 ofs.per_cu = per_cu;
19714 ofs.offset = offset;
19715 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
19716 if (slot)
19717 return slot->type;
19718 else
19719 return NULL;
19720 }
19721
19722 /* Look up the type for DIE in CU in die_type_hash,
19723 or return NULL if DIE does not have a saved type. */
19724
19725 static struct type *
19726 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
19727 {
19728 return get_die_type_at_offset (die->offset, cu->per_cu);
19729 }
19730
19731 /* Add a dependence relationship from CU to REF_PER_CU. */
19732
19733 static void
19734 dwarf2_add_dependence (struct dwarf2_cu *cu,
19735 struct dwarf2_per_cu_data *ref_per_cu)
19736 {
19737 void **slot;
19738
19739 if (cu->dependencies == NULL)
19740 cu->dependencies
19741 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
19742 NULL, &cu->comp_unit_obstack,
19743 hashtab_obstack_allocate,
19744 dummy_obstack_deallocate);
19745
19746 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
19747 if (*slot == NULL)
19748 *slot = ref_per_cu;
19749 }
19750
19751 /* Subroutine of dwarf2_mark to pass to htab_traverse.
19752 Set the mark field in every compilation unit in the
19753 cache that we must keep because we are keeping CU. */
19754
19755 static int
19756 dwarf2_mark_helper (void **slot, void *data)
19757 {
19758 struct dwarf2_per_cu_data *per_cu;
19759
19760 per_cu = (struct dwarf2_per_cu_data *) *slot;
19761
19762 /* cu->dependencies references may not yet have been ever read if QUIT aborts
19763 reading of the chain. As such dependencies remain valid it is not much
19764 useful to track and undo them during QUIT cleanups. */
19765 if (per_cu->cu == NULL)
19766 return 1;
19767
19768 if (per_cu->cu->mark)
19769 return 1;
19770 per_cu->cu->mark = 1;
19771
19772 if (per_cu->cu->dependencies != NULL)
19773 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
19774
19775 return 1;
19776 }
19777
19778 /* Set the mark field in CU and in every other compilation unit in the
19779 cache that we must keep because we are keeping CU. */
19780
19781 static void
19782 dwarf2_mark (struct dwarf2_cu *cu)
19783 {
19784 if (cu->mark)
19785 return;
19786 cu->mark = 1;
19787 if (cu->dependencies != NULL)
19788 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
19789 }
19790
19791 static void
19792 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
19793 {
19794 while (per_cu)
19795 {
19796 per_cu->cu->mark = 0;
19797 per_cu = per_cu->cu->read_in_chain;
19798 }
19799 }
19800
19801 /* Trivial hash function for partial_die_info: the hash value of a DIE
19802 is its offset in .debug_info for this objfile. */
19803
19804 static hashval_t
19805 partial_die_hash (const void *item)
19806 {
19807 const struct partial_die_info *part_die = item;
19808
19809 return part_die->offset.sect_off;
19810 }
19811
19812 /* Trivial comparison function for partial_die_info structures: two DIEs
19813 are equal if they have the same offset. */
19814
19815 static int
19816 partial_die_eq (const void *item_lhs, const void *item_rhs)
19817 {
19818 const struct partial_die_info *part_die_lhs = item_lhs;
19819 const struct partial_die_info *part_die_rhs = item_rhs;
19820
19821 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
19822 }
19823
19824 static struct cmd_list_element *set_dwarf2_cmdlist;
19825 static struct cmd_list_element *show_dwarf2_cmdlist;
19826
19827 static void
19828 set_dwarf2_cmd (char *args, int from_tty)
19829 {
19830 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
19831 }
19832
19833 static void
19834 show_dwarf2_cmd (char *args, int from_tty)
19835 {
19836 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
19837 }
19838
19839 /* Free data associated with OBJFILE, if necessary. */
19840
19841 static void
19842 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
19843 {
19844 struct dwarf2_per_objfile *data = d;
19845 int ix;
19846
19847 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
19848 VEC_free (dwarf2_per_cu_ptr,
19849 dwarf2_per_objfile->all_comp_units[ix]->imported_symtabs);
19850
19851 for (ix = 0; ix < dwarf2_per_objfile->n_type_units; ++ix)
19852 VEC_free (dwarf2_per_cu_ptr,
19853 dwarf2_per_objfile->all_type_units[ix]->per_cu.imported_symtabs);
19854
19855 VEC_free (dwarf2_section_info_def, data->types);
19856
19857 if (data->dwo_files)
19858 free_dwo_files (data->dwo_files, objfile);
19859 if (data->dwp_file)
19860 gdb_bfd_unref (data->dwp_file->dbfd);
19861
19862 if (data->dwz_file && data->dwz_file->dwz_bfd)
19863 gdb_bfd_unref (data->dwz_file->dwz_bfd);
19864 }
19865
19866 \f
19867 /* The "save gdb-index" command. */
19868
19869 /* The contents of the hash table we create when building the string
19870 table. */
19871 struct strtab_entry
19872 {
19873 offset_type offset;
19874 const char *str;
19875 };
19876
19877 /* Hash function for a strtab_entry.
19878
19879 Function is used only during write_hash_table so no index format backward
19880 compatibility is needed. */
19881
19882 static hashval_t
19883 hash_strtab_entry (const void *e)
19884 {
19885 const struct strtab_entry *entry = e;
19886 return mapped_index_string_hash (INT_MAX, entry->str);
19887 }
19888
19889 /* Equality function for a strtab_entry. */
19890
19891 static int
19892 eq_strtab_entry (const void *a, const void *b)
19893 {
19894 const struct strtab_entry *ea = a;
19895 const struct strtab_entry *eb = b;
19896 return !strcmp (ea->str, eb->str);
19897 }
19898
19899 /* Create a strtab_entry hash table. */
19900
19901 static htab_t
19902 create_strtab (void)
19903 {
19904 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
19905 xfree, xcalloc, xfree);
19906 }
19907
19908 /* Add a string to the constant pool. Return the string's offset in
19909 host order. */
19910
19911 static offset_type
19912 add_string (htab_t table, struct obstack *cpool, const char *str)
19913 {
19914 void **slot;
19915 struct strtab_entry entry;
19916 struct strtab_entry *result;
19917
19918 entry.str = str;
19919 slot = htab_find_slot (table, &entry, INSERT);
19920 if (*slot)
19921 result = *slot;
19922 else
19923 {
19924 result = XNEW (struct strtab_entry);
19925 result->offset = obstack_object_size (cpool);
19926 result->str = str;
19927 obstack_grow_str0 (cpool, str);
19928 *slot = result;
19929 }
19930 return result->offset;
19931 }
19932
19933 /* An entry in the symbol table. */
19934 struct symtab_index_entry
19935 {
19936 /* The name of the symbol. */
19937 const char *name;
19938 /* The offset of the name in the constant pool. */
19939 offset_type index_offset;
19940 /* A sorted vector of the indices of all the CUs that hold an object
19941 of this name. */
19942 VEC (offset_type) *cu_indices;
19943 };
19944
19945 /* The symbol table. This is a power-of-2-sized hash table. */
19946 struct mapped_symtab
19947 {
19948 offset_type n_elements;
19949 offset_type size;
19950 struct symtab_index_entry **data;
19951 };
19952
19953 /* Hash function for a symtab_index_entry. */
19954
19955 static hashval_t
19956 hash_symtab_entry (const void *e)
19957 {
19958 const struct symtab_index_entry *entry = e;
19959 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
19960 sizeof (offset_type) * VEC_length (offset_type,
19961 entry->cu_indices),
19962 0);
19963 }
19964
19965 /* Equality function for a symtab_index_entry. */
19966
19967 static int
19968 eq_symtab_entry (const void *a, const void *b)
19969 {
19970 const struct symtab_index_entry *ea = a;
19971 const struct symtab_index_entry *eb = b;
19972 int len = VEC_length (offset_type, ea->cu_indices);
19973 if (len != VEC_length (offset_type, eb->cu_indices))
19974 return 0;
19975 return !memcmp (VEC_address (offset_type, ea->cu_indices),
19976 VEC_address (offset_type, eb->cu_indices),
19977 sizeof (offset_type) * len);
19978 }
19979
19980 /* Destroy a symtab_index_entry. */
19981
19982 static void
19983 delete_symtab_entry (void *p)
19984 {
19985 struct symtab_index_entry *entry = p;
19986 VEC_free (offset_type, entry->cu_indices);
19987 xfree (entry);
19988 }
19989
19990 /* Create a hash table holding symtab_index_entry objects. */
19991
19992 static htab_t
19993 create_symbol_hash_table (void)
19994 {
19995 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
19996 delete_symtab_entry, xcalloc, xfree);
19997 }
19998
19999 /* Create a new mapped symtab object. */
20000
20001 static struct mapped_symtab *
20002 create_mapped_symtab (void)
20003 {
20004 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
20005 symtab->n_elements = 0;
20006 symtab->size = 1024;
20007 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
20008 return symtab;
20009 }
20010
20011 /* Destroy a mapped_symtab. */
20012
20013 static void
20014 cleanup_mapped_symtab (void *p)
20015 {
20016 struct mapped_symtab *symtab = p;
20017 /* The contents of the array are freed when the other hash table is
20018 destroyed. */
20019 xfree (symtab->data);
20020 xfree (symtab);
20021 }
20022
20023 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
20024 the slot.
20025
20026 Function is used only during write_hash_table so no index format backward
20027 compatibility is needed. */
20028
20029 static struct symtab_index_entry **
20030 find_slot (struct mapped_symtab *symtab, const char *name)
20031 {
20032 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
20033
20034 index = hash & (symtab->size - 1);
20035 step = ((hash * 17) & (symtab->size - 1)) | 1;
20036
20037 for (;;)
20038 {
20039 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
20040 return &symtab->data[index];
20041 index = (index + step) & (symtab->size - 1);
20042 }
20043 }
20044
20045 /* Expand SYMTAB's hash table. */
20046
20047 static void
20048 hash_expand (struct mapped_symtab *symtab)
20049 {
20050 offset_type old_size = symtab->size;
20051 offset_type i;
20052 struct symtab_index_entry **old_entries = symtab->data;
20053
20054 symtab->size *= 2;
20055 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
20056
20057 for (i = 0; i < old_size; ++i)
20058 {
20059 if (old_entries[i])
20060 {
20061 struct symtab_index_entry **slot = find_slot (symtab,
20062 old_entries[i]->name);
20063 *slot = old_entries[i];
20064 }
20065 }
20066
20067 xfree (old_entries);
20068 }
20069
20070 /* Add an entry to SYMTAB. NAME is the name of the symbol.
20071 CU_INDEX is the index of the CU in which the symbol appears.
20072 IS_STATIC is one if the symbol is static, otherwise zero (global). */
20073
20074 static void
20075 add_index_entry (struct mapped_symtab *symtab, const char *name,
20076 int is_static, gdb_index_symbol_kind kind,
20077 offset_type cu_index)
20078 {
20079 struct symtab_index_entry **slot;
20080 offset_type cu_index_and_attrs;
20081
20082 ++symtab->n_elements;
20083 if (4 * symtab->n_elements / 3 >= symtab->size)
20084 hash_expand (symtab);
20085
20086 slot = find_slot (symtab, name);
20087 if (!*slot)
20088 {
20089 *slot = XNEW (struct symtab_index_entry);
20090 (*slot)->name = name;
20091 /* index_offset is set later. */
20092 (*slot)->cu_indices = NULL;
20093 }
20094
20095 cu_index_and_attrs = 0;
20096 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
20097 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
20098 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
20099
20100 /* We don't want to record an index value twice as we want to avoid the
20101 duplication.
20102 We process all global symbols and then all static symbols
20103 (which would allow us to avoid the duplication by only having to check
20104 the last entry pushed), but a symbol could have multiple kinds in one CU.
20105 To keep things simple we don't worry about the duplication here and
20106 sort and uniqufy the list after we've processed all symbols. */
20107 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
20108 }
20109
20110 /* qsort helper routine for uniquify_cu_indices. */
20111
20112 static int
20113 offset_type_compare (const void *ap, const void *bp)
20114 {
20115 offset_type a = *(offset_type *) ap;
20116 offset_type b = *(offset_type *) bp;
20117
20118 return (a > b) - (b > a);
20119 }
20120
20121 /* Sort and remove duplicates of all symbols' cu_indices lists. */
20122
20123 static void
20124 uniquify_cu_indices (struct mapped_symtab *symtab)
20125 {
20126 int i;
20127
20128 for (i = 0; i < symtab->size; ++i)
20129 {
20130 struct symtab_index_entry *entry = symtab->data[i];
20131
20132 if (entry
20133 && entry->cu_indices != NULL)
20134 {
20135 unsigned int next_to_insert, next_to_check;
20136 offset_type last_value;
20137
20138 qsort (VEC_address (offset_type, entry->cu_indices),
20139 VEC_length (offset_type, entry->cu_indices),
20140 sizeof (offset_type), offset_type_compare);
20141
20142 last_value = VEC_index (offset_type, entry->cu_indices, 0);
20143 next_to_insert = 1;
20144 for (next_to_check = 1;
20145 next_to_check < VEC_length (offset_type, entry->cu_indices);
20146 ++next_to_check)
20147 {
20148 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
20149 != last_value)
20150 {
20151 last_value = VEC_index (offset_type, entry->cu_indices,
20152 next_to_check);
20153 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
20154 last_value);
20155 ++next_to_insert;
20156 }
20157 }
20158 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
20159 }
20160 }
20161 }
20162
20163 /* Add a vector of indices to the constant pool. */
20164
20165 static offset_type
20166 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
20167 struct symtab_index_entry *entry)
20168 {
20169 void **slot;
20170
20171 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
20172 if (!*slot)
20173 {
20174 offset_type len = VEC_length (offset_type, entry->cu_indices);
20175 offset_type val = MAYBE_SWAP (len);
20176 offset_type iter;
20177 int i;
20178
20179 *slot = entry;
20180 entry->index_offset = obstack_object_size (cpool);
20181
20182 obstack_grow (cpool, &val, sizeof (val));
20183 for (i = 0;
20184 VEC_iterate (offset_type, entry->cu_indices, i, iter);
20185 ++i)
20186 {
20187 val = MAYBE_SWAP (iter);
20188 obstack_grow (cpool, &val, sizeof (val));
20189 }
20190 }
20191 else
20192 {
20193 struct symtab_index_entry *old_entry = *slot;
20194 entry->index_offset = old_entry->index_offset;
20195 entry = old_entry;
20196 }
20197 return entry->index_offset;
20198 }
20199
20200 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
20201 constant pool entries going into the obstack CPOOL. */
20202
20203 static void
20204 write_hash_table (struct mapped_symtab *symtab,
20205 struct obstack *output, struct obstack *cpool)
20206 {
20207 offset_type i;
20208 htab_t symbol_hash_table;
20209 htab_t str_table;
20210
20211 symbol_hash_table = create_symbol_hash_table ();
20212 str_table = create_strtab ();
20213
20214 /* We add all the index vectors to the constant pool first, to
20215 ensure alignment is ok. */
20216 for (i = 0; i < symtab->size; ++i)
20217 {
20218 if (symtab->data[i])
20219 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
20220 }
20221
20222 /* Now write out the hash table. */
20223 for (i = 0; i < symtab->size; ++i)
20224 {
20225 offset_type str_off, vec_off;
20226
20227 if (symtab->data[i])
20228 {
20229 str_off = add_string (str_table, cpool, symtab->data[i]->name);
20230 vec_off = symtab->data[i]->index_offset;
20231 }
20232 else
20233 {
20234 /* While 0 is a valid constant pool index, it is not valid
20235 to have 0 for both offsets. */
20236 str_off = 0;
20237 vec_off = 0;
20238 }
20239
20240 str_off = MAYBE_SWAP (str_off);
20241 vec_off = MAYBE_SWAP (vec_off);
20242
20243 obstack_grow (output, &str_off, sizeof (str_off));
20244 obstack_grow (output, &vec_off, sizeof (vec_off));
20245 }
20246
20247 htab_delete (str_table);
20248 htab_delete (symbol_hash_table);
20249 }
20250
20251 /* Struct to map psymtab to CU index in the index file. */
20252 struct psymtab_cu_index_map
20253 {
20254 struct partial_symtab *psymtab;
20255 unsigned int cu_index;
20256 };
20257
20258 static hashval_t
20259 hash_psymtab_cu_index (const void *item)
20260 {
20261 const struct psymtab_cu_index_map *map = item;
20262
20263 return htab_hash_pointer (map->psymtab);
20264 }
20265
20266 static int
20267 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
20268 {
20269 const struct psymtab_cu_index_map *lhs = item_lhs;
20270 const struct psymtab_cu_index_map *rhs = item_rhs;
20271
20272 return lhs->psymtab == rhs->psymtab;
20273 }
20274
20275 /* Helper struct for building the address table. */
20276 struct addrmap_index_data
20277 {
20278 struct objfile *objfile;
20279 struct obstack *addr_obstack;
20280 htab_t cu_index_htab;
20281
20282 /* Non-zero if the previous_* fields are valid.
20283 We can't write an entry until we see the next entry (since it is only then
20284 that we know the end of the entry). */
20285 int previous_valid;
20286 /* Index of the CU in the table of all CUs in the index file. */
20287 unsigned int previous_cu_index;
20288 /* Start address of the CU. */
20289 CORE_ADDR previous_cu_start;
20290 };
20291
20292 /* Write an address entry to OBSTACK. */
20293
20294 static void
20295 add_address_entry (struct objfile *objfile, struct obstack *obstack,
20296 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
20297 {
20298 offset_type cu_index_to_write;
20299 char addr[8];
20300 CORE_ADDR baseaddr;
20301
20302 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20303
20304 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
20305 obstack_grow (obstack, addr, 8);
20306 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
20307 obstack_grow (obstack, addr, 8);
20308 cu_index_to_write = MAYBE_SWAP (cu_index);
20309 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
20310 }
20311
20312 /* Worker function for traversing an addrmap to build the address table. */
20313
20314 static int
20315 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
20316 {
20317 struct addrmap_index_data *data = datap;
20318 struct partial_symtab *pst = obj;
20319
20320 if (data->previous_valid)
20321 add_address_entry (data->objfile, data->addr_obstack,
20322 data->previous_cu_start, start_addr,
20323 data->previous_cu_index);
20324
20325 data->previous_cu_start = start_addr;
20326 if (pst != NULL)
20327 {
20328 struct psymtab_cu_index_map find_map, *map;
20329 find_map.psymtab = pst;
20330 map = htab_find (data->cu_index_htab, &find_map);
20331 gdb_assert (map != NULL);
20332 data->previous_cu_index = map->cu_index;
20333 data->previous_valid = 1;
20334 }
20335 else
20336 data->previous_valid = 0;
20337
20338 return 0;
20339 }
20340
20341 /* Write OBJFILE's address map to OBSTACK.
20342 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
20343 in the index file. */
20344
20345 static void
20346 write_address_map (struct objfile *objfile, struct obstack *obstack,
20347 htab_t cu_index_htab)
20348 {
20349 struct addrmap_index_data addrmap_index_data;
20350
20351 /* When writing the address table, we have to cope with the fact that
20352 the addrmap iterator only provides the start of a region; we have to
20353 wait until the next invocation to get the start of the next region. */
20354
20355 addrmap_index_data.objfile = objfile;
20356 addrmap_index_data.addr_obstack = obstack;
20357 addrmap_index_data.cu_index_htab = cu_index_htab;
20358 addrmap_index_data.previous_valid = 0;
20359
20360 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
20361 &addrmap_index_data);
20362
20363 /* It's highly unlikely the last entry (end address = 0xff...ff)
20364 is valid, but we should still handle it.
20365 The end address is recorded as the start of the next region, but that
20366 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
20367 anyway. */
20368 if (addrmap_index_data.previous_valid)
20369 add_address_entry (objfile, obstack,
20370 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
20371 addrmap_index_data.previous_cu_index);
20372 }
20373
20374 /* Return the symbol kind of PSYM. */
20375
20376 static gdb_index_symbol_kind
20377 symbol_kind (struct partial_symbol *psym)
20378 {
20379 domain_enum domain = PSYMBOL_DOMAIN (psym);
20380 enum address_class aclass = PSYMBOL_CLASS (psym);
20381
20382 switch (domain)
20383 {
20384 case VAR_DOMAIN:
20385 switch (aclass)
20386 {
20387 case LOC_BLOCK:
20388 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
20389 case LOC_TYPEDEF:
20390 return GDB_INDEX_SYMBOL_KIND_TYPE;
20391 case LOC_COMPUTED:
20392 case LOC_CONST_BYTES:
20393 case LOC_OPTIMIZED_OUT:
20394 case LOC_STATIC:
20395 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20396 case LOC_CONST:
20397 /* Note: It's currently impossible to recognize psyms as enum values
20398 short of reading the type info. For now punt. */
20399 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20400 default:
20401 /* There are other LOC_FOO values that one might want to classify
20402 as variables, but dwarf2read.c doesn't currently use them. */
20403 return GDB_INDEX_SYMBOL_KIND_OTHER;
20404 }
20405 case STRUCT_DOMAIN:
20406 return GDB_INDEX_SYMBOL_KIND_TYPE;
20407 default:
20408 return GDB_INDEX_SYMBOL_KIND_OTHER;
20409 }
20410 }
20411
20412 /* Add a list of partial symbols to SYMTAB. */
20413
20414 static void
20415 write_psymbols (struct mapped_symtab *symtab,
20416 htab_t psyms_seen,
20417 struct partial_symbol **psymp,
20418 int count,
20419 offset_type cu_index,
20420 int is_static)
20421 {
20422 for (; count-- > 0; ++psymp)
20423 {
20424 struct partial_symbol *psym = *psymp;
20425 void **slot;
20426
20427 if (SYMBOL_LANGUAGE (psym) == language_ada)
20428 error (_("Ada is not currently supported by the index"));
20429
20430 /* Only add a given psymbol once. */
20431 slot = htab_find_slot (psyms_seen, psym, INSERT);
20432 if (!*slot)
20433 {
20434 gdb_index_symbol_kind kind = symbol_kind (psym);
20435
20436 *slot = psym;
20437 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
20438 is_static, kind, cu_index);
20439 }
20440 }
20441 }
20442
20443 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
20444 exception if there is an error. */
20445
20446 static void
20447 write_obstack (FILE *file, struct obstack *obstack)
20448 {
20449 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
20450 file)
20451 != obstack_object_size (obstack))
20452 error (_("couldn't data write to file"));
20453 }
20454
20455 /* Unlink a file if the argument is not NULL. */
20456
20457 static void
20458 unlink_if_set (void *p)
20459 {
20460 char **filename = p;
20461 if (*filename)
20462 unlink (*filename);
20463 }
20464
20465 /* A helper struct used when iterating over debug_types. */
20466 struct signatured_type_index_data
20467 {
20468 struct objfile *objfile;
20469 struct mapped_symtab *symtab;
20470 struct obstack *types_list;
20471 htab_t psyms_seen;
20472 int cu_index;
20473 };
20474
20475 /* A helper function that writes a single signatured_type to an
20476 obstack. */
20477
20478 static int
20479 write_one_signatured_type (void **slot, void *d)
20480 {
20481 struct signatured_type_index_data *info = d;
20482 struct signatured_type *entry = (struct signatured_type *) *slot;
20483 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
20484 gdb_byte val[8];
20485
20486 write_psymbols (info->symtab,
20487 info->psyms_seen,
20488 info->objfile->global_psymbols.list
20489 + psymtab->globals_offset,
20490 psymtab->n_global_syms, info->cu_index,
20491 0);
20492 write_psymbols (info->symtab,
20493 info->psyms_seen,
20494 info->objfile->static_psymbols.list
20495 + psymtab->statics_offset,
20496 psymtab->n_static_syms, info->cu_index,
20497 1);
20498
20499 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20500 entry->per_cu.offset.sect_off);
20501 obstack_grow (info->types_list, val, 8);
20502 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20503 entry->type_offset_in_tu.cu_off);
20504 obstack_grow (info->types_list, val, 8);
20505 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
20506 obstack_grow (info->types_list, val, 8);
20507
20508 ++info->cu_index;
20509
20510 return 1;
20511 }
20512
20513 /* Recurse into all "included" dependencies and write their symbols as
20514 if they appeared in this psymtab. */
20515
20516 static void
20517 recursively_write_psymbols (struct objfile *objfile,
20518 struct partial_symtab *psymtab,
20519 struct mapped_symtab *symtab,
20520 htab_t psyms_seen,
20521 offset_type cu_index)
20522 {
20523 int i;
20524
20525 for (i = 0; i < psymtab->number_of_dependencies; ++i)
20526 if (psymtab->dependencies[i]->user != NULL)
20527 recursively_write_psymbols (objfile, psymtab->dependencies[i],
20528 symtab, psyms_seen, cu_index);
20529
20530 write_psymbols (symtab,
20531 psyms_seen,
20532 objfile->global_psymbols.list + psymtab->globals_offset,
20533 psymtab->n_global_syms, cu_index,
20534 0);
20535 write_psymbols (symtab,
20536 psyms_seen,
20537 objfile->static_psymbols.list + psymtab->statics_offset,
20538 psymtab->n_static_syms, cu_index,
20539 1);
20540 }
20541
20542 /* Create an index file for OBJFILE in the directory DIR. */
20543
20544 static void
20545 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
20546 {
20547 struct cleanup *cleanup;
20548 char *filename, *cleanup_filename;
20549 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
20550 struct obstack cu_list, types_cu_list;
20551 int i;
20552 FILE *out_file;
20553 struct mapped_symtab *symtab;
20554 offset_type val, size_of_contents, total_len;
20555 struct stat st;
20556 htab_t psyms_seen;
20557 htab_t cu_index_htab;
20558 struct psymtab_cu_index_map *psymtab_cu_index_map;
20559
20560 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
20561 return;
20562
20563 if (dwarf2_per_objfile->using_index)
20564 error (_("Cannot use an index to create the index"));
20565
20566 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
20567 error (_("Cannot make an index when the file has multiple .debug_types sections"));
20568
20569 if (stat (objfile->name, &st) < 0)
20570 perror_with_name (objfile->name);
20571
20572 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
20573 INDEX_SUFFIX, (char *) NULL);
20574 cleanup = make_cleanup (xfree, filename);
20575
20576 out_file = fopen (filename, "wb");
20577 if (!out_file)
20578 error (_("Can't open `%s' for writing"), filename);
20579
20580 cleanup_filename = filename;
20581 make_cleanup (unlink_if_set, &cleanup_filename);
20582
20583 symtab = create_mapped_symtab ();
20584 make_cleanup (cleanup_mapped_symtab, symtab);
20585
20586 obstack_init (&addr_obstack);
20587 make_cleanup_obstack_free (&addr_obstack);
20588
20589 obstack_init (&cu_list);
20590 make_cleanup_obstack_free (&cu_list);
20591
20592 obstack_init (&types_cu_list);
20593 make_cleanup_obstack_free (&types_cu_list);
20594
20595 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
20596 NULL, xcalloc, xfree);
20597 make_cleanup_htab_delete (psyms_seen);
20598
20599 /* While we're scanning CU's create a table that maps a psymtab pointer
20600 (which is what addrmap records) to its index (which is what is recorded
20601 in the index file). This will later be needed to write the address
20602 table. */
20603 cu_index_htab = htab_create_alloc (100,
20604 hash_psymtab_cu_index,
20605 eq_psymtab_cu_index,
20606 NULL, xcalloc, xfree);
20607 make_cleanup_htab_delete (cu_index_htab);
20608 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
20609 xmalloc (sizeof (struct psymtab_cu_index_map)
20610 * dwarf2_per_objfile->n_comp_units);
20611 make_cleanup (xfree, psymtab_cu_index_map);
20612
20613 /* The CU list is already sorted, so we don't need to do additional
20614 work here. Also, the debug_types entries do not appear in
20615 all_comp_units, but only in their own hash table. */
20616 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
20617 {
20618 struct dwarf2_per_cu_data *per_cu
20619 = dwarf2_per_objfile->all_comp_units[i];
20620 struct partial_symtab *psymtab = per_cu->v.psymtab;
20621 gdb_byte val[8];
20622 struct psymtab_cu_index_map *map;
20623 void **slot;
20624
20625 if (psymtab->user == NULL)
20626 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
20627
20628 map = &psymtab_cu_index_map[i];
20629 map->psymtab = psymtab;
20630 map->cu_index = i;
20631 slot = htab_find_slot (cu_index_htab, map, INSERT);
20632 gdb_assert (slot != NULL);
20633 gdb_assert (*slot == NULL);
20634 *slot = map;
20635
20636 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20637 per_cu->offset.sect_off);
20638 obstack_grow (&cu_list, val, 8);
20639 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
20640 obstack_grow (&cu_list, val, 8);
20641 }
20642
20643 /* Dump the address map. */
20644 write_address_map (objfile, &addr_obstack, cu_index_htab);
20645
20646 /* Write out the .debug_type entries, if any. */
20647 if (dwarf2_per_objfile->signatured_types)
20648 {
20649 struct signatured_type_index_data sig_data;
20650
20651 sig_data.objfile = objfile;
20652 sig_data.symtab = symtab;
20653 sig_data.types_list = &types_cu_list;
20654 sig_data.psyms_seen = psyms_seen;
20655 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
20656 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
20657 write_one_signatured_type, &sig_data);
20658 }
20659
20660 /* Now that we've processed all symbols we can shrink their cu_indices
20661 lists. */
20662 uniquify_cu_indices (symtab);
20663
20664 obstack_init (&constant_pool);
20665 make_cleanup_obstack_free (&constant_pool);
20666 obstack_init (&symtab_obstack);
20667 make_cleanup_obstack_free (&symtab_obstack);
20668 write_hash_table (symtab, &symtab_obstack, &constant_pool);
20669
20670 obstack_init (&contents);
20671 make_cleanup_obstack_free (&contents);
20672 size_of_contents = 6 * sizeof (offset_type);
20673 total_len = size_of_contents;
20674
20675 /* The version number. */
20676 val = MAYBE_SWAP (8);
20677 obstack_grow (&contents, &val, sizeof (val));
20678
20679 /* The offset of the CU list from the start of the file. */
20680 val = MAYBE_SWAP (total_len);
20681 obstack_grow (&contents, &val, sizeof (val));
20682 total_len += obstack_object_size (&cu_list);
20683
20684 /* The offset of the types CU list from the start of the file. */
20685 val = MAYBE_SWAP (total_len);
20686 obstack_grow (&contents, &val, sizeof (val));
20687 total_len += obstack_object_size (&types_cu_list);
20688
20689 /* The offset of the address table from the start of the file. */
20690 val = MAYBE_SWAP (total_len);
20691 obstack_grow (&contents, &val, sizeof (val));
20692 total_len += obstack_object_size (&addr_obstack);
20693
20694 /* The offset of the symbol table from the start of the file. */
20695 val = MAYBE_SWAP (total_len);
20696 obstack_grow (&contents, &val, sizeof (val));
20697 total_len += obstack_object_size (&symtab_obstack);
20698
20699 /* The offset of the constant pool from the start of the file. */
20700 val = MAYBE_SWAP (total_len);
20701 obstack_grow (&contents, &val, sizeof (val));
20702 total_len += obstack_object_size (&constant_pool);
20703
20704 gdb_assert (obstack_object_size (&contents) == size_of_contents);
20705
20706 write_obstack (out_file, &contents);
20707 write_obstack (out_file, &cu_list);
20708 write_obstack (out_file, &types_cu_list);
20709 write_obstack (out_file, &addr_obstack);
20710 write_obstack (out_file, &symtab_obstack);
20711 write_obstack (out_file, &constant_pool);
20712
20713 fclose (out_file);
20714
20715 /* We want to keep the file, so we set cleanup_filename to NULL
20716 here. See unlink_if_set. */
20717 cleanup_filename = NULL;
20718
20719 do_cleanups (cleanup);
20720 }
20721
20722 /* Implementation of the `save gdb-index' command.
20723
20724 Note that the file format used by this command is documented in the
20725 GDB manual. Any changes here must be documented there. */
20726
20727 static void
20728 save_gdb_index_command (char *arg, int from_tty)
20729 {
20730 struct objfile *objfile;
20731
20732 if (!arg || !*arg)
20733 error (_("usage: save gdb-index DIRECTORY"));
20734
20735 ALL_OBJFILES (objfile)
20736 {
20737 struct stat st;
20738
20739 /* If the objfile does not correspond to an actual file, skip it. */
20740 if (stat (objfile->name, &st) < 0)
20741 continue;
20742
20743 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
20744 if (dwarf2_per_objfile)
20745 {
20746 volatile struct gdb_exception except;
20747
20748 TRY_CATCH (except, RETURN_MASK_ERROR)
20749 {
20750 write_psymtabs_to_index (objfile, arg);
20751 }
20752 if (except.reason < 0)
20753 exception_fprintf (gdb_stderr, except,
20754 _("Error while writing index for `%s': "),
20755 objfile->name);
20756 }
20757 }
20758 }
20759
20760 \f
20761
20762 int dwarf2_always_disassemble;
20763
20764 static void
20765 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
20766 struct cmd_list_element *c, const char *value)
20767 {
20768 fprintf_filtered (file,
20769 _("Whether to always disassemble "
20770 "DWARF expressions is %s.\n"),
20771 value);
20772 }
20773
20774 static void
20775 show_check_physname (struct ui_file *file, int from_tty,
20776 struct cmd_list_element *c, const char *value)
20777 {
20778 fprintf_filtered (file,
20779 _("Whether to check \"physname\" is %s.\n"),
20780 value);
20781 }
20782
20783 void _initialize_dwarf2_read (void);
20784
20785 void
20786 _initialize_dwarf2_read (void)
20787 {
20788 struct cmd_list_element *c;
20789
20790 dwarf2_objfile_data_key
20791 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
20792
20793 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
20794 Set DWARF 2 specific variables.\n\
20795 Configure DWARF 2 variables such as the cache size"),
20796 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
20797 0/*allow-unknown*/, &maintenance_set_cmdlist);
20798
20799 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
20800 Show DWARF 2 specific variables\n\
20801 Show DWARF 2 variables such as the cache size"),
20802 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
20803 0/*allow-unknown*/, &maintenance_show_cmdlist);
20804
20805 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
20806 &dwarf2_max_cache_age, _("\
20807 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
20808 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
20809 A higher limit means that cached compilation units will be stored\n\
20810 in memory longer, and more total memory will be used. Zero disables\n\
20811 caching, which can slow down startup."),
20812 NULL,
20813 show_dwarf2_max_cache_age,
20814 &set_dwarf2_cmdlist,
20815 &show_dwarf2_cmdlist);
20816
20817 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
20818 &dwarf2_always_disassemble, _("\
20819 Set whether `info address' always disassembles DWARF expressions."), _("\
20820 Show whether `info address' always disassembles DWARF expressions."), _("\
20821 When enabled, DWARF expressions are always printed in an assembly-like\n\
20822 syntax. When disabled, expressions will be printed in a more\n\
20823 conversational style, when possible."),
20824 NULL,
20825 show_dwarf2_always_disassemble,
20826 &set_dwarf2_cmdlist,
20827 &show_dwarf2_cmdlist);
20828
20829 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
20830 Set debugging of the dwarf2 reader."), _("\
20831 Show debugging of the dwarf2 reader."), _("\
20832 When enabled, debugging messages are printed during dwarf2 reading\n\
20833 and symtab expansion."),
20834 NULL,
20835 NULL,
20836 &setdebuglist, &showdebuglist);
20837
20838 add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
20839 Set debugging of the dwarf2 DIE reader."), _("\
20840 Show debugging of the dwarf2 DIE reader."), _("\
20841 When enabled (non-zero), DIEs are dumped after they are read in.\n\
20842 The value is the maximum depth to print."),
20843 NULL,
20844 NULL,
20845 &setdebuglist, &showdebuglist);
20846
20847 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
20848 Set cross-checking of \"physname\" code against demangler."), _("\
20849 Show cross-checking of \"physname\" code against demangler."), _("\
20850 When enabled, GDB's internal \"physname\" code is checked against\n\
20851 the demangler."),
20852 NULL, show_check_physname,
20853 &setdebuglist, &showdebuglist);
20854
20855 add_setshow_boolean_cmd ("use-deprecated-index-sections",
20856 no_class, &use_deprecated_index_sections, _("\
20857 Set whether to use deprecated gdb_index sections."), _("\
20858 Show whether to use deprecated gdb_index sections."), _("\
20859 When enabled, deprecated .gdb_index sections are used anyway.\n\
20860 Normally they are ignored either because of a missing feature or\n\
20861 performance issue.\n\
20862 Warning: This option must be enabled before gdb reads the file."),
20863 NULL,
20864 NULL,
20865 &setlist, &showlist);
20866
20867 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
20868 _("\
20869 Save a gdb-index file.\n\
20870 Usage: save gdb-index DIRECTORY"),
20871 &save_cmdlist);
20872 set_cmd_completer (c, filename_completer);
20873
20874 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
20875 &dwarf2_locexpr_funcs);
20876 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
20877 &dwarf2_loclist_funcs);
20878
20879 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
20880 &dwarf2_block_frame_base_locexpr_funcs);
20881 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
20882 &dwarf2_block_frame_base_loclist_funcs);
20883 }
This page took 0.66875 seconds and 4 git commands to generate.