* dwarf2read.c (lookup_signatured_type_at_offset): Delete.
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2013 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "bfd.h"
33 #include "elf-bfd.h"
34 #include "symtab.h"
35 #include "gdbtypes.h"
36 #include "objfiles.h"
37 #include "dwarf2.h"
38 #include "buildsym.h"
39 #include "demangle.h"
40 #include "gdb-demangle.h"
41 #include "expression.h"
42 #include "filenames.h" /* for DOSish file names */
43 #include "macrotab.h"
44 #include "language.h"
45 #include "complaints.h"
46 #include "bcache.h"
47 #include "dwarf2expr.h"
48 #include "dwarf2loc.h"
49 #include "cp-support.h"
50 #include "hashtab.h"
51 #include "command.h"
52 #include "gdbcmd.h"
53 #include "block.h"
54 #include "addrmap.h"
55 #include "typeprint.h"
56 #include "jv-lang.h"
57 #include "psympriv.h"
58 #include "exceptions.h"
59 #include "gdb_stat.h"
60 #include "completer.h"
61 #include "vec.h"
62 #include "c-lang.h"
63 #include "go-lang.h"
64 #include "valprint.h"
65 #include "gdbcore.h" /* for gnutarget */
66 #include "gdb/gdb-index.h"
67 #include <ctype.h>
68 #include "gdb_bfd.h"
69 #include "f-lang.h"
70 #include "source.h"
71
72 #include <fcntl.h>
73 #include "gdb_string.h"
74 #include "gdb_assert.h"
75 #include <sys/types.h>
76
77 typedef struct symbol *symbolp;
78 DEF_VEC_P (symbolp);
79
80 /* When non-zero, print basic high level tracing messages.
81 This is in contrast to the low level DIE reading of dwarf2_die_debug. */
82 static int dwarf2_read_debug = 0;
83
84 /* When non-zero, dump DIEs after they are read in. */
85 static unsigned int dwarf2_die_debug = 0;
86
87 /* When non-zero, cross-check physname against demangler. */
88 static int check_physname = 0;
89
90 /* When non-zero, do not reject deprecated .gdb_index sections. */
91 static int use_deprecated_index_sections = 0;
92
93 static const struct objfile_data *dwarf2_objfile_data_key;
94
95 /* The "aclass" indices for various kinds of computed DWARF symbols. */
96
97 static int dwarf2_locexpr_index;
98 static int dwarf2_loclist_index;
99 static int dwarf2_locexpr_block_index;
100 static int dwarf2_loclist_block_index;
101
102 struct dwarf2_section_info
103 {
104 asection *asection;
105 gdb_byte *buffer;
106 bfd_size_type size;
107 /* True if we have tried to read this section. */
108 int readin;
109 };
110
111 typedef struct dwarf2_section_info dwarf2_section_info_def;
112 DEF_VEC_O (dwarf2_section_info_def);
113
114 /* All offsets in the index are of this type. It must be
115 architecture-independent. */
116 typedef uint32_t offset_type;
117
118 DEF_VEC_I (offset_type);
119
120 /* Ensure only legit values are used. */
121 #define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
122 do { \
123 gdb_assert ((unsigned int) (value) <= 1); \
124 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
125 } while (0)
126
127 /* Ensure only legit values are used. */
128 #define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
129 do { \
130 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
131 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
132 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
133 } while (0)
134
135 /* Ensure we don't use more than the alloted nuber of bits for the CU. */
136 #define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
137 do { \
138 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
139 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
140 } while (0)
141
142 /* A description of the mapped index. The file format is described in
143 a comment by the code that writes the index. */
144 struct mapped_index
145 {
146 /* Index data format version. */
147 int version;
148
149 /* The total length of the buffer. */
150 off_t total_size;
151
152 /* A pointer to the address table data. */
153 const gdb_byte *address_table;
154
155 /* Size of the address table data in bytes. */
156 offset_type address_table_size;
157
158 /* The symbol table, implemented as a hash table. */
159 const offset_type *symbol_table;
160
161 /* Size in slots, each slot is 2 offset_types. */
162 offset_type symbol_table_slots;
163
164 /* A pointer to the constant pool. */
165 const char *constant_pool;
166 };
167
168 typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
169 DEF_VEC_P (dwarf2_per_cu_ptr);
170
171 /* Collection of data recorded per objfile.
172 This hangs off of dwarf2_objfile_data_key. */
173
174 struct dwarf2_per_objfile
175 {
176 struct dwarf2_section_info info;
177 struct dwarf2_section_info abbrev;
178 struct dwarf2_section_info line;
179 struct dwarf2_section_info loc;
180 struct dwarf2_section_info macinfo;
181 struct dwarf2_section_info macro;
182 struct dwarf2_section_info str;
183 struct dwarf2_section_info ranges;
184 struct dwarf2_section_info addr;
185 struct dwarf2_section_info frame;
186 struct dwarf2_section_info eh_frame;
187 struct dwarf2_section_info gdb_index;
188
189 VEC (dwarf2_section_info_def) *types;
190
191 /* Back link. */
192 struct objfile *objfile;
193
194 /* Table of all the compilation units. This is used to locate
195 the target compilation unit of a particular reference. */
196 struct dwarf2_per_cu_data **all_comp_units;
197
198 /* The number of compilation units in ALL_COMP_UNITS. */
199 int n_comp_units;
200
201 /* The number of .debug_types-related CUs. */
202 int n_type_units;
203
204 /* The .debug_types-related CUs (TUs). */
205 struct signatured_type **all_type_units;
206
207 /* The number of entries in all_type_unit_groups. */
208 int n_type_unit_groups;
209
210 /* Table of type unit groups.
211 This exists to make it easy to iterate over all CUs and TU groups. */
212 struct type_unit_group **all_type_unit_groups;
213
214 /* Table of struct type_unit_group objects.
215 The hash key is the DW_AT_stmt_list value. */
216 htab_t type_unit_groups;
217
218 /* A table mapping .debug_types signatures to its signatured_type entry.
219 This is NULL if the .debug_types section hasn't been read in yet. */
220 htab_t signatured_types;
221
222 /* Type unit statistics, to see how well the scaling improvements
223 are doing. */
224 struct tu_stats
225 {
226 int nr_uniq_abbrev_tables;
227 int nr_symtabs;
228 int nr_symtab_sharers;
229 int nr_stmt_less_type_units;
230 } tu_stats;
231
232 /* A chain of compilation units that are currently read in, so that
233 they can be freed later. */
234 struct dwarf2_per_cu_data *read_in_chain;
235
236 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
237 This is NULL if the table hasn't been allocated yet. */
238 htab_t dwo_files;
239
240 /* Non-zero if we've check for whether there is a DWP file. */
241 int dwp_checked;
242
243 /* The DWP file if there is one, or NULL. */
244 struct dwp_file *dwp_file;
245
246 /* The shared '.dwz' file, if one exists. This is used when the
247 original data was compressed using 'dwz -m'. */
248 struct dwz_file *dwz_file;
249
250 /* A flag indicating wether this objfile has a section loaded at a
251 VMA of 0. */
252 int has_section_at_zero;
253
254 /* True if we are using the mapped index,
255 or we are faking it for OBJF_READNOW's sake. */
256 unsigned char using_index;
257
258 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
259 struct mapped_index *index_table;
260
261 /* When using index_table, this keeps track of all quick_file_names entries.
262 TUs typically share line table entries with a CU, so we maintain a
263 separate table of all line table entries to support the sharing.
264 Note that while there can be way more TUs than CUs, we've already
265 sorted all the TUs into "type unit groups", grouped by their
266 DW_AT_stmt_list value. Therefore the only sharing done here is with a
267 CU and its associated TU group if there is one. */
268 htab_t quick_file_names_table;
269
270 /* Set during partial symbol reading, to prevent queueing of full
271 symbols. */
272 int reading_partial_symbols;
273
274 /* Table mapping type DIEs to their struct type *.
275 This is NULL if not allocated yet.
276 The mapping is done via (CU/TU signature + DIE offset) -> type. */
277 htab_t die_type_hash;
278
279 /* The CUs we recently read. */
280 VEC (dwarf2_per_cu_ptr) *just_read_cus;
281 };
282
283 static struct dwarf2_per_objfile *dwarf2_per_objfile;
284
285 /* Default names of the debugging sections. */
286
287 /* Note that if the debugging section has been compressed, it might
288 have a name like .zdebug_info. */
289
290 static const struct dwarf2_debug_sections dwarf2_elf_names =
291 {
292 { ".debug_info", ".zdebug_info" },
293 { ".debug_abbrev", ".zdebug_abbrev" },
294 { ".debug_line", ".zdebug_line" },
295 { ".debug_loc", ".zdebug_loc" },
296 { ".debug_macinfo", ".zdebug_macinfo" },
297 { ".debug_macro", ".zdebug_macro" },
298 { ".debug_str", ".zdebug_str" },
299 { ".debug_ranges", ".zdebug_ranges" },
300 { ".debug_types", ".zdebug_types" },
301 { ".debug_addr", ".zdebug_addr" },
302 { ".debug_frame", ".zdebug_frame" },
303 { ".eh_frame", NULL },
304 { ".gdb_index", ".zgdb_index" },
305 23
306 };
307
308 /* List of DWO/DWP sections. */
309
310 static const struct dwop_section_names
311 {
312 struct dwarf2_section_names abbrev_dwo;
313 struct dwarf2_section_names info_dwo;
314 struct dwarf2_section_names line_dwo;
315 struct dwarf2_section_names loc_dwo;
316 struct dwarf2_section_names macinfo_dwo;
317 struct dwarf2_section_names macro_dwo;
318 struct dwarf2_section_names str_dwo;
319 struct dwarf2_section_names str_offsets_dwo;
320 struct dwarf2_section_names types_dwo;
321 struct dwarf2_section_names cu_index;
322 struct dwarf2_section_names tu_index;
323 }
324 dwop_section_names =
325 {
326 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
327 { ".debug_info.dwo", ".zdebug_info.dwo" },
328 { ".debug_line.dwo", ".zdebug_line.dwo" },
329 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
330 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
331 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
332 { ".debug_str.dwo", ".zdebug_str.dwo" },
333 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
334 { ".debug_types.dwo", ".zdebug_types.dwo" },
335 { ".debug_cu_index", ".zdebug_cu_index" },
336 { ".debug_tu_index", ".zdebug_tu_index" },
337 };
338
339 /* local data types */
340
341 /* The data in a compilation unit header, after target2host
342 translation, looks like this. */
343 struct comp_unit_head
344 {
345 unsigned int length;
346 short version;
347 unsigned char addr_size;
348 unsigned char signed_addr_p;
349 sect_offset abbrev_offset;
350
351 /* Size of file offsets; either 4 or 8. */
352 unsigned int offset_size;
353
354 /* Size of the length field; either 4 or 12. */
355 unsigned int initial_length_size;
356
357 /* Offset to the first byte of this compilation unit header in the
358 .debug_info section, for resolving relative reference dies. */
359 sect_offset offset;
360
361 /* Offset to first die in this cu from the start of the cu.
362 This will be the first byte following the compilation unit header. */
363 cu_offset first_die_offset;
364 };
365
366 /* Type used for delaying computation of method physnames.
367 See comments for compute_delayed_physnames. */
368 struct delayed_method_info
369 {
370 /* The type to which the method is attached, i.e., its parent class. */
371 struct type *type;
372
373 /* The index of the method in the type's function fieldlists. */
374 int fnfield_index;
375
376 /* The index of the method in the fieldlist. */
377 int index;
378
379 /* The name of the DIE. */
380 const char *name;
381
382 /* The DIE associated with this method. */
383 struct die_info *die;
384 };
385
386 typedef struct delayed_method_info delayed_method_info;
387 DEF_VEC_O (delayed_method_info);
388
389 /* Internal state when decoding a particular compilation unit. */
390 struct dwarf2_cu
391 {
392 /* The objfile containing this compilation unit. */
393 struct objfile *objfile;
394
395 /* The header of the compilation unit. */
396 struct comp_unit_head header;
397
398 /* Base address of this compilation unit. */
399 CORE_ADDR base_address;
400
401 /* Non-zero if base_address has been set. */
402 int base_known;
403
404 /* The language we are debugging. */
405 enum language language;
406 const struct language_defn *language_defn;
407
408 const char *producer;
409
410 /* The generic symbol table building routines have separate lists for
411 file scope symbols and all all other scopes (local scopes). So
412 we need to select the right one to pass to add_symbol_to_list().
413 We do it by keeping a pointer to the correct list in list_in_scope.
414
415 FIXME: The original dwarf code just treated the file scope as the
416 first local scope, and all other local scopes as nested local
417 scopes, and worked fine. Check to see if we really need to
418 distinguish these in buildsym.c. */
419 struct pending **list_in_scope;
420
421 /* The abbrev table for this CU.
422 Normally this points to the abbrev table in the objfile.
423 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
424 struct abbrev_table *abbrev_table;
425
426 /* Hash table holding all the loaded partial DIEs
427 with partial_die->offset.SECT_OFF as hash. */
428 htab_t partial_dies;
429
430 /* Storage for things with the same lifetime as this read-in compilation
431 unit, including partial DIEs. */
432 struct obstack comp_unit_obstack;
433
434 /* When multiple dwarf2_cu structures are living in memory, this field
435 chains them all together, so that they can be released efficiently.
436 We will probably also want a generation counter so that most-recently-used
437 compilation units are cached... */
438 struct dwarf2_per_cu_data *read_in_chain;
439
440 /* Backchain to our per_cu entry if the tree has been built. */
441 struct dwarf2_per_cu_data *per_cu;
442
443 /* How many compilation units ago was this CU last referenced? */
444 int last_used;
445
446 /* A hash table of DIE cu_offset for following references with
447 die_info->offset.sect_off as hash. */
448 htab_t die_hash;
449
450 /* Full DIEs if read in. */
451 struct die_info *dies;
452
453 /* A set of pointers to dwarf2_per_cu_data objects for compilation
454 units referenced by this one. Only set during full symbol processing;
455 partial symbol tables do not have dependencies. */
456 htab_t dependencies;
457
458 /* Header data from the line table, during full symbol processing. */
459 struct line_header *line_header;
460
461 /* A list of methods which need to have physnames computed
462 after all type information has been read. */
463 VEC (delayed_method_info) *method_list;
464
465 /* To be copied to symtab->call_site_htab. */
466 htab_t call_site_htab;
467
468 /* Non-NULL if this CU came from a DWO file.
469 There is an invariant here that is important to remember:
470 Except for attributes copied from the top level DIE in the "main"
471 (or "stub") file in preparation for reading the DWO file
472 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
473 Either there isn't a DWO file (in which case this is NULL and the point
474 is moot), or there is and either we're not going to read it (in which
475 case this is NULL) or there is and we are reading it (in which case this
476 is non-NULL). */
477 struct dwo_unit *dwo_unit;
478
479 /* The DW_AT_addr_base attribute if present, zero otherwise
480 (zero is a valid value though).
481 Note this value comes from the stub CU/TU's DIE. */
482 ULONGEST addr_base;
483
484 /* The DW_AT_ranges_base attribute if present, zero otherwise
485 (zero is a valid value though).
486 Note this value comes from the stub CU/TU's DIE.
487 Also note that the value is zero in the non-DWO case so this value can
488 be used without needing to know whether DWO files are in use or not.
489 N.B. This does not apply to DW_AT_ranges appearing in
490 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
491 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
492 DW_AT_ranges_base *would* have to be applied, and we'd have to care
493 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
494 ULONGEST ranges_base;
495
496 /* Mark used when releasing cached dies. */
497 unsigned int mark : 1;
498
499 /* This CU references .debug_loc. See the symtab->locations_valid field.
500 This test is imperfect as there may exist optimized debug code not using
501 any location list and still facing inlining issues if handled as
502 unoptimized code. For a future better test see GCC PR other/32998. */
503 unsigned int has_loclist : 1;
504
505 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
506 if all the producer_is_* fields are valid. This information is cached
507 because profiling CU expansion showed excessive time spent in
508 producer_is_gxx_lt_4_6. */
509 unsigned int checked_producer : 1;
510 unsigned int producer_is_gxx_lt_4_6 : 1;
511 unsigned int producer_is_gcc_lt_4_3 : 1;
512 unsigned int producer_is_icc : 1;
513
514 /* When set, the file that we're processing is known to have
515 debugging info for C++ namespaces. GCC 3.3.x did not produce
516 this information, but later versions do. */
517
518 unsigned int processing_has_namespace_info : 1;
519 };
520
521 /* Persistent data held for a compilation unit, even when not
522 processing it. We put a pointer to this structure in the
523 read_symtab_private field of the psymtab. */
524
525 struct dwarf2_per_cu_data
526 {
527 /* The start offset and length of this compilation unit.
528 NOTE: Unlike comp_unit_head.length, this length includes
529 initial_length_size.
530 If the DIE refers to a DWO file, this is always of the original die,
531 not the DWO file. */
532 sect_offset offset;
533 unsigned int length;
534
535 /* Flag indicating this compilation unit will be read in before
536 any of the current compilation units are processed. */
537 unsigned int queued : 1;
538
539 /* This flag will be set when reading partial DIEs if we need to load
540 absolutely all DIEs for this compilation unit, instead of just the ones
541 we think are interesting. It gets set if we look for a DIE in the
542 hash table and don't find it. */
543 unsigned int load_all_dies : 1;
544
545 /* Non-zero if this CU is from .debug_types.
546 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
547 this is non-zero. */
548 unsigned int is_debug_types : 1;
549
550 /* Non-zero if this CU is from the .dwz file. */
551 unsigned int is_dwz : 1;
552
553 /* The section this CU/TU lives in.
554 If the DIE refers to a DWO file, this is always the original die,
555 not the DWO file. */
556 struct dwarf2_section_info *section;
557
558 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
559 of the CU cache it gets reset to NULL again. */
560 struct dwarf2_cu *cu;
561
562 /* The corresponding objfile.
563 Normally we can get the objfile from dwarf2_per_objfile.
564 However we can enter this file with just a "per_cu" handle. */
565 struct objfile *objfile;
566
567 /* When using partial symbol tables, the 'psymtab' field is active.
568 Otherwise the 'quick' field is active. */
569 union
570 {
571 /* The partial symbol table associated with this compilation unit,
572 or NULL for unread partial units. */
573 struct partial_symtab *psymtab;
574
575 /* Data needed by the "quick" functions. */
576 struct dwarf2_per_cu_quick_data *quick;
577 } v;
578
579 /* The CUs we import using DW_TAG_imported_unit. This is filled in
580 while reading psymtabs, used to compute the psymtab dependencies,
581 and then cleared. Then it is filled in again while reading full
582 symbols, and only deleted when the objfile is destroyed.
583
584 This is also used to work around a difference between the way gold
585 generates .gdb_index version <=7 and the way gdb does. Arguably this
586 is a gold bug. For symbols coming from TUs, gold records in the index
587 the CU that includes the TU instead of the TU itself. This breaks
588 dw2_lookup_symbol: It assumes that if the index says symbol X lives
589 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
590 will find X. Alas TUs live in their own symtab, so after expanding CU Y
591 we need to look in TU Z to find X. Fortunately, this is akin to
592 DW_TAG_imported_unit, so we just use the same mechanism: For
593 .gdb_index version <=7 this also records the TUs that the CU referred
594 to. Concurrently with this change gdb was modified to emit version 8
595 indices so we only pay a price for gold generated indices. */
596 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
597 };
598
599 /* Entry in the signatured_types hash table. */
600
601 struct signatured_type
602 {
603 /* The "per_cu" object of this type.
604 N.B.: This is the first member so that it's easy to convert pointers
605 between them. */
606 struct dwarf2_per_cu_data per_cu;
607
608 /* The type's signature. */
609 ULONGEST signature;
610
611 /* Offset in the TU of the type's DIE, as read from the TU header.
612 If this TU is a DWO stub and the definition lives in a DWO file
613 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
614 cu_offset type_offset_in_tu;
615
616 /* Offset in the section of the type's DIE.
617 If the definition lives in a DWO file, this is the offset in the
618 .debug_types.dwo section.
619 The value is zero until the actual value is known.
620 Zero is otherwise not a valid section offset. */
621 sect_offset type_offset_in_section;
622
623 /* Type units are grouped by their DW_AT_stmt_list entry so that they
624 can share them. This points to the containing symtab. */
625 struct type_unit_group *type_unit_group;
626 };
627
628 typedef struct signatured_type *sig_type_ptr;
629 DEF_VEC_P (sig_type_ptr);
630
631 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
632 This includes type_unit_group and quick_file_names. */
633
634 struct stmt_list_hash
635 {
636 /* The DWO unit this table is from or NULL if there is none. */
637 struct dwo_unit *dwo_unit;
638
639 /* Offset in .debug_line or .debug_line.dwo. */
640 sect_offset line_offset;
641 };
642
643 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
644 an object of this type. */
645
646 struct type_unit_group
647 {
648 /* dwarf2read.c's main "handle" on a TU symtab.
649 To simplify things we create an artificial CU that "includes" all the
650 type units using this stmt_list so that the rest of the code still has
651 a "per_cu" handle on the symtab.
652 This PER_CU is recognized by having no section. */
653 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
654 struct dwarf2_per_cu_data per_cu;
655
656 /* The TUs that share this DW_AT_stmt_list entry.
657 This is added to while parsing type units to build partial symtabs,
658 and is deleted afterwards and not used again. */
659 VEC (sig_type_ptr) *tus;
660
661 /* The primary symtab.
662 Type units in a group needn't all be defined in the same source file,
663 so we create an essentially anonymous symtab as the primary symtab. */
664 struct symtab *primary_symtab;
665
666 /* The data used to construct the hash key. */
667 struct stmt_list_hash hash;
668
669 /* The number of symtabs from the line header.
670 The value here must match line_header.num_file_names. */
671 unsigned int num_symtabs;
672
673 /* The symbol tables for this TU (obtained from the files listed in
674 DW_AT_stmt_list).
675 WARNING: The order of entries here must match the order of entries
676 in the line header. After the first TU using this type_unit_group, the
677 line header for the subsequent TUs is recreated from this. This is done
678 because we need to use the same symtabs for each TU using the same
679 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
680 there's no guarantee the line header doesn't have duplicate entries. */
681 struct symtab **symtabs;
682 };
683
684 /* These sections are what may appear in a DWO file. */
685
686 struct dwo_sections
687 {
688 struct dwarf2_section_info abbrev;
689 struct dwarf2_section_info line;
690 struct dwarf2_section_info loc;
691 struct dwarf2_section_info macinfo;
692 struct dwarf2_section_info macro;
693 struct dwarf2_section_info str;
694 struct dwarf2_section_info str_offsets;
695 /* In the case of a virtual DWO file, these two are unused. */
696 struct dwarf2_section_info info;
697 VEC (dwarf2_section_info_def) *types;
698 };
699
700 /* CUs/TUs in DWP/DWO files. */
701
702 struct dwo_unit
703 {
704 /* Backlink to the containing struct dwo_file. */
705 struct dwo_file *dwo_file;
706
707 /* The "id" that distinguishes this CU/TU.
708 .debug_info calls this "dwo_id", .debug_types calls this "signature".
709 Since signatures came first, we stick with it for consistency. */
710 ULONGEST signature;
711
712 /* The section this CU/TU lives in, in the DWO file. */
713 struct dwarf2_section_info *section;
714
715 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
716 sect_offset offset;
717 unsigned int length;
718
719 /* For types, offset in the type's DIE of the type defined by this TU. */
720 cu_offset type_offset_in_tu;
721 };
722
723 /* Data for one DWO file.
724 This includes virtual DWO files that have been packaged into a
725 DWP file. */
726
727 struct dwo_file
728 {
729 /* The DW_AT_GNU_dwo_name attribute. This is the hash key.
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 *name;
734
735 /* The bfd, when the file is open. Otherwise this is NULL.
736 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
737 bfd *dbfd;
738
739 /* Section info for this file. */
740 struct dwo_sections sections;
741
742 /* Table of CUs in the file.
743 Each element is a struct dwo_unit. */
744 htab_t cus;
745
746 /* Table of TUs in the file.
747 Each element is a struct dwo_unit. */
748 htab_t tus;
749 };
750
751 /* These sections are what may appear in a DWP file. */
752
753 struct dwp_sections
754 {
755 struct dwarf2_section_info str;
756 struct dwarf2_section_info cu_index;
757 struct dwarf2_section_info tu_index;
758 /* The .debug_info.dwo, .debug_types.dwo, and other sections are referenced
759 by section number. We don't need to record them here. */
760 };
761
762 /* These sections are what may appear in a virtual DWO file. */
763
764 struct virtual_dwo_sections
765 {
766 struct dwarf2_section_info abbrev;
767 struct dwarf2_section_info line;
768 struct dwarf2_section_info loc;
769 struct dwarf2_section_info macinfo;
770 struct dwarf2_section_info macro;
771 struct dwarf2_section_info str_offsets;
772 /* Each DWP hash table entry records one CU or one TU.
773 That is recorded here, and copied to dwo_unit.section. */
774 struct dwarf2_section_info info_or_types;
775 };
776
777 /* Contents of DWP hash tables. */
778
779 struct dwp_hash_table
780 {
781 uint32_t nr_units, nr_slots;
782 const gdb_byte *hash_table, *unit_table, *section_pool;
783 };
784
785 /* Data for one DWP file. */
786
787 struct dwp_file
788 {
789 /* Name of the file. */
790 const char *name;
791
792 /* The bfd, when the file is open. Otherwise this is NULL. */
793 bfd *dbfd;
794
795 /* Section info for this file. */
796 struct dwp_sections sections;
797
798 /* Table of CUs in the file. */
799 const struct dwp_hash_table *cus;
800
801 /* Table of TUs in the file. */
802 const struct dwp_hash_table *tus;
803
804 /* Table of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
805 htab_t loaded_cutus;
806
807 /* Table to map ELF section numbers to their sections. */
808 unsigned int num_sections;
809 asection **elf_sections;
810 };
811
812 /* This represents a '.dwz' file. */
813
814 struct dwz_file
815 {
816 /* A dwz file can only contain a few sections. */
817 struct dwarf2_section_info abbrev;
818 struct dwarf2_section_info info;
819 struct dwarf2_section_info str;
820 struct dwarf2_section_info line;
821 struct dwarf2_section_info macro;
822 struct dwarf2_section_info gdb_index;
823
824 /* The dwz's BFD. */
825 bfd *dwz_bfd;
826 };
827
828 /* Struct used to pass misc. parameters to read_die_and_children, et
829 al. which are used for both .debug_info and .debug_types dies.
830 All parameters here are unchanging for the life of the call. This
831 struct exists to abstract away the constant parameters of die reading. */
832
833 struct die_reader_specs
834 {
835 /* die_section->asection->owner. */
836 bfd* abfd;
837
838 /* The CU of the DIE we are parsing. */
839 struct dwarf2_cu *cu;
840
841 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
842 struct dwo_file *dwo_file;
843
844 /* The section the die comes from.
845 This is either .debug_info or .debug_types, or the .dwo variants. */
846 struct dwarf2_section_info *die_section;
847
848 /* die_section->buffer. */
849 gdb_byte *buffer;
850
851 /* The end of the buffer. */
852 const gdb_byte *buffer_end;
853 };
854
855 /* Type of function passed to init_cutu_and_read_dies, et.al. */
856 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
857 gdb_byte *info_ptr,
858 struct die_info *comp_unit_die,
859 int has_children,
860 void *data);
861
862 /* The line number information for a compilation unit (found in the
863 .debug_line section) begins with a "statement program header",
864 which contains the following information. */
865 struct line_header
866 {
867 unsigned int total_length;
868 unsigned short version;
869 unsigned int header_length;
870 unsigned char minimum_instruction_length;
871 unsigned char maximum_ops_per_instruction;
872 unsigned char default_is_stmt;
873 int line_base;
874 unsigned char line_range;
875 unsigned char opcode_base;
876
877 /* standard_opcode_lengths[i] is the number of operands for the
878 standard opcode whose value is i. This means that
879 standard_opcode_lengths[0] is unused, and the last meaningful
880 element is standard_opcode_lengths[opcode_base - 1]. */
881 unsigned char *standard_opcode_lengths;
882
883 /* The include_directories table. NOTE! These strings are not
884 allocated with xmalloc; instead, they are pointers into
885 debug_line_buffer. If you try to free them, `free' will get
886 indigestion. */
887 unsigned int num_include_dirs, include_dirs_size;
888 char **include_dirs;
889
890 /* The file_names table. NOTE! These strings are not allocated
891 with xmalloc; instead, they are pointers into debug_line_buffer.
892 Don't try to free them directly. */
893 unsigned int num_file_names, file_names_size;
894 struct file_entry
895 {
896 char *name;
897 unsigned int dir_index;
898 unsigned int mod_time;
899 unsigned int length;
900 int included_p; /* Non-zero if referenced by the Line Number Program. */
901 struct symtab *symtab; /* The associated symbol table, if any. */
902 } *file_names;
903
904 /* The start and end of the statement program following this
905 header. These point into dwarf2_per_objfile->line_buffer. */
906 gdb_byte *statement_program_start, *statement_program_end;
907 };
908
909 /* When we construct a partial symbol table entry we only
910 need this much information. */
911 struct partial_die_info
912 {
913 /* Offset of this DIE. */
914 sect_offset offset;
915
916 /* DWARF-2 tag for this DIE. */
917 ENUM_BITFIELD(dwarf_tag) tag : 16;
918
919 /* Assorted flags describing the data found in this DIE. */
920 unsigned int has_children : 1;
921 unsigned int is_external : 1;
922 unsigned int is_declaration : 1;
923 unsigned int has_type : 1;
924 unsigned int has_specification : 1;
925 unsigned int has_pc_info : 1;
926 unsigned int may_be_inlined : 1;
927
928 /* Flag set if the SCOPE field of this structure has been
929 computed. */
930 unsigned int scope_set : 1;
931
932 /* Flag set if the DIE has a byte_size attribute. */
933 unsigned int has_byte_size : 1;
934
935 /* Flag set if any of the DIE's children are template arguments. */
936 unsigned int has_template_arguments : 1;
937
938 /* Flag set if fixup_partial_die has been called on this die. */
939 unsigned int fixup_called : 1;
940
941 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
942 unsigned int is_dwz : 1;
943
944 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
945 unsigned int spec_is_dwz : 1;
946
947 /* The name of this DIE. Normally the value of DW_AT_name, but
948 sometimes a default name for unnamed DIEs. */
949 const char *name;
950
951 /* The linkage name, if present. */
952 const char *linkage_name;
953
954 /* The scope to prepend to our children. This is generally
955 allocated on the comp_unit_obstack, so will disappear
956 when this compilation unit leaves the cache. */
957 const char *scope;
958
959 /* Some data associated with the partial DIE. The tag determines
960 which field is live. */
961 union
962 {
963 /* The location description associated with this DIE, if any. */
964 struct dwarf_block *locdesc;
965 /* The offset of an import, for DW_TAG_imported_unit. */
966 sect_offset offset;
967 } d;
968
969 /* If HAS_PC_INFO, the PC range associated with this DIE. */
970 CORE_ADDR lowpc;
971 CORE_ADDR highpc;
972
973 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
974 DW_AT_sibling, if any. */
975 /* NOTE: This member isn't strictly necessary, read_partial_die could
976 return DW_AT_sibling values to its caller load_partial_dies. */
977 gdb_byte *sibling;
978
979 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
980 DW_AT_specification (or DW_AT_abstract_origin or
981 DW_AT_extension). */
982 sect_offset spec_offset;
983
984 /* Pointers to this DIE's parent, first child, and next sibling,
985 if any. */
986 struct partial_die_info *die_parent, *die_child, *die_sibling;
987 };
988
989 /* This data structure holds the information of an abbrev. */
990 struct abbrev_info
991 {
992 unsigned int number; /* number identifying abbrev */
993 enum dwarf_tag tag; /* dwarf tag */
994 unsigned short has_children; /* boolean */
995 unsigned short num_attrs; /* number of attributes */
996 struct attr_abbrev *attrs; /* an array of attribute descriptions */
997 struct abbrev_info *next; /* next in chain */
998 };
999
1000 struct attr_abbrev
1001 {
1002 ENUM_BITFIELD(dwarf_attribute) name : 16;
1003 ENUM_BITFIELD(dwarf_form) form : 16;
1004 };
1005
1006 /* Size of abbrev_table.abbrev_hash_table. */
1007 #define ABBREV_HASH_SIZE 121
1008
1009 /* Top level data structure to contain an abbreviation table. */
1010
1011 struct abbrev_table
1012 {
1013 /* Where the abbrev table came from.
1014 This is used as a sanity check when the table is used. */
1015 sect_offset offset;
1016
1017 /* Storage for the abbrev table. */
1018 struct obstack abbrev_obstack;
1019
1020 /* Hash table of abbrevs.
1021 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1022 It could be statically allocated, but the previous code didn't so we
1023 don't either. */
1024 struct abbrev_info **abbrevs;
1025 };
1026
1027 /* Attributes have a name and a value. */
1028 struct attribute
1029 {
1030 ENUM_BITFIELD(dwarf_attribute) name : 16;
1031 ENUM_BITFIELD(dwarf_form) form : 15;
1032
1033 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1034 field should be in u.str (existing only for DW_STRING) but it is kept
1035 here for better struct attribute alignment. */
1036 unsigned int string_is_canonical : 1;
1037
1038 union
1039 {
1040 const char *str;
1041 struct dwarf_block *blk;
1042 ULONGEST unsnd;
1043 LONGEST snd;
1044 CORE_ADDR addr;
1045 struct signatured_type *signatured_type;
1046 }
1047 u;
1048 };
1049
1050 /* This data structure holds a complete die structure. */
1051 struct die_info
1052 {
1053 /* DWARF-2 tag for this DIE. */
1054 ENUM_BITFIELD(dwarf_tag) tag : 16;
1055
1056 /* Number of attributes */
1057 unsigned char num_attrs;
1058
1059 /* True if we're presently building the full type name for the
1060 type derived from this DIE. */
1061 unsigned char building_fullname : 1;
1062
1063 /* Abbrev number */
1064 unsigned int abbrev;
1065
1066 /* Offset in .debug_info or .debug_types section. */
1067 sect_offset offset;
1068
1069 /* The dies in a compilation unit form an n-ary tree. PARENT
1070 points to this die's parent; CHILD points to the first child of
1071 this node; and all the children of a given node are chained
1072 together via their SIBLING fields. */
1073 struct die_info *child; /* Its first child, if any. */
1074 struct die_info *sibling; /* Its next sibling, if any. */
1075 struct die_info *parent; /* Its parent, if any. */
1076
1077 /* An array of attributes, with NUM_ATTRS elements. There may be
1078 zero, but it's not common and zero-sized arrays are not
1079 sufficiently portable C. */
1080 struct attribute attrs[1];
1081 };
1082
1083 /* Get at parts of an attribute structure. */
1084
1085 #define DW_STRING(attr) ((attr)->u.str)
1086 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1087 #define DW_UNSND(attr) ((attr)->u.unsnd)
1088 #define DW_BLOCK(attr) ((attr)->u.blk)
1089 #define DW_SND(attr) ((attr)->u.snd)
1090 #define DW_ADDR(attr) ((attr)->u.addr)
1091 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
1092
1093 /* Blocks are a bunch of untyped bytes. */
1094 struct dwarf_block
1095 {
1096 size_t size;
1097
1098 /* Valid only if SIZE is not zero. */
1099 gdb_byte *data;
1100 };
1101
1102 #ifndef ATTR_ALLOC_CHUNK
1103 #define ATTR_ALLOC_CHUNK 4
1104 #endif
1105
1106 /* Allocate fields for structs, unions and enums in this size. */
1107 #ifndef DW_FIELD_ALLOC_CHUNK
1108 #define DW_FIELD_ALLOC_CHUNK 4
1109 #endif
1110
1111 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1112 but this would require a corresponding change in unpack_field_as_long
1113 and friends. */
1114 static int bits_per_byte = 8;
1115
1116 /* The routines that read and process dies for a C struct or C++ class
1117 pass lists of data member fields and lists of member function fields
1118 in an instance of a field_info structure, as defined below. */
1119 struct field_info
1120 {
1121 /* List of data member and baseclasses fields. */
1122 struct nextfield
1123 {
1124 struct nextfield *next;
1125 int accessibility;
1126 int virtuality;
1127 struct field field;
1128 }
1129 *fields, *baseclasses;
1130
1131 /* Number of fields (including baseclasses). */
1132 int nfields;
1133
1134 /* Number of baseclasses. */
1135 int nbaseclasses;
1136
1137 /* Set if the accesibility of one of the fields is not public. */
1138 int non_public_fields;
1139
1140 /* Member function fields array, entries are allocated in the order they
1141 are encountered in the object file. */
1142 struct nextfnfield
1143 {
1144 struct nextfnfield *next;
1145 struct fn_field fnfield;
1146 }
1147 *fnfields;
1148
1149 /* Member function fieldlist array, contains name of possibly overloaded
1150 member function, number of overloaded member functions and a pointer
1151 to the head of the member function field chain. */
1152 struct fnfieldlist
1153 {
1154 const char *name;
1155 int length;
1156 struct nextfnfield *head;
1157 }
1158 *fnfieldlists;
1159
1160 /* Number of entries in the fnfieldlists array. */
1161 int nfnfields;
1162
1163 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1164 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1165 struct typedef_field_list
1166 {
1167 struct typedef_field field;
1168 struct typedef_field_list *next;
1169 }
1170 *typedef_field_list;
1171 unsigned typedef_field_list_count;
1172 };
1173
1174 /* One item on the queue of compilation units to read in full symbols
1175 for. */
1176 struct dwarf2_queue_item
1177 {
1178 struct dwarf2_per_cu_data *per_cu;
1179 enum language pretend_language;
1180 struct dwarf2_queue_item *next;
1181 };
1182
1183 /* The current queue. */
1184 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1185
1186 /* Loaded secondary compilation units are kept in memory until they
1187 have not been referenced for the processing of this many
1188 compilation units. Set this to zero to disable caching. Cache
1189 sizes of up to at least twenty will improve startup time for
1190 typical inter-CU-reference binaries, at an obvious memory cost. */
1191 static int dwarf2_max_cache_age = 5;
1192 static void
1193 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
1194 struct cmd_list_element *c, const char *value)
1195 {
1196 fprintf_filtered (file, _("The upper bound on the age of cached "
1197 "dwarf2 compilation units is %s.\n"),
1198 value);
1199 }
1200
1201
1202 /* Various complaints about symbol reading that don't abort the process. */
1203
1204 static void
1205 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1206 {
1207 complaint (&symfile_complaints,
1208 _("statement list doesn't fit in .debug_line section"));
1209 }
1210
1211 static void
1212 dwarf2_debug_line_missing_file_complaint (void)
1213 {
1214 complaint (&symfile_complaints,
1215 _(".debug_line section has line data without a file"));
1216 }
1217
1218 static void
1219 dwarf2_debug_line_missing_end_sequence_complaint (void)
1220 {
1221 complaint (&symfile_complaints,
1222 _(".debug_line section has line "
1223 "program sequence without an end"));
1224 }
1225
1226 static void
1227 dwarf2_complex_location_expr_complaint (void)
1228 {
1229 complaint (&symfile_complaints, _("location expression too complex"));
1230 }
1231
1232 static void
1233 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1234 int arg3)
1235 {
1236 complaint (&symfile_complaints,
1237 _("const value length mismatch for '%s', got %d, expected %d"),
1238 arg1, arg2, arg3);
1239 }
1240
1241 static void
1242 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1243 {
1244 complaint (&symfile_complaints,
1245 _("debug info runs off end of %s section"
1246 " [in module %s]"),
1247 section->asection->name,
1248 bfd_get_filename (section->asection->owner));
1249 }
1250
1251 static void
1252 dwarf2_macro_malformed_definition_complaint (const char *arg1)
1253 {
1254 complaint (&symfile_complaints,
1255 _("macro debug info contains a "
1256 "malformed macro definition:\n`%s'"),
1257 arg1);
1258 }
1259
1260 static void
1261 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1262 {
1263 complaint (&symfile_complaints,
1264 _("invalid attribute class or form for '%s' in '%s'"),
1265 arg1, arg2);
1266 }
1267
1268 /* local function prototypes */
1269
1270 static void dwarf2_locate_sections (bfd *, asection *, void *);
1271
1272 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
1273 struct objfile *);
1274
1275 static void dwarf2_find_base_address (struct die_info *die,
1276 struct dwarf2_cu *cu);
1277
1278 static struct partial_symtab *create_partial_symtab
1279 (struct dwarf2_per_cu_data *per_cu, const char *name);
1280
1281 static void dwarf2_build_psymtabs_hard (struct objfile *);
1282
1283 static void scan_partial_symbols (struct partial_die_info *,
1284 CORE_ADDR *, CORE_ADDR *,
1285 int, struct dwarf2_cu *);
1286
1287 static void add_partial_symbol (struct partial_die_info *,
1288 struct dwarf2_cu *);
1289
1290 static void add_partial_namespace (struct partial_die_info *pdi,
1291 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1292 int need_pc, struct dwarf2_cu *cu);
1293
1294 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1295 CORE_ADDR *highpc, int need_pc,
1296 struct dwarf2_cu *cu);
1297
1298 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1299 struct dwarf2_cu *cu);
1300
1301 static void add_partial_subprogram (struct partial_die_info *pdi,
1302 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1303 int need_pc, struct dwarf2_cu *cu);
1304
1305 static void dwarf2_read_symtab (struct partial_symtab *,
1306 struct objfile *);
1307
1308 static void psymtab_to_symtab_1 (struct partial_symtab *);
1309
1310 static struct abbrev_info *abbrev_table_lookup_abbrev
1311 (const struct abbrev_table *, unsigned int);
1312
1313 static struct abbrev_table *abbrev_table_read_table
1314 (struct dwarf2_section_info *, sect_offset);
1315
1316 static void abbrev_table_free (struct abbrev_table *);
1317
1318 static void abbrev_table_free_cleanup (void *);
1319
1320 static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1321 struct dwarf2_section_info *);
1322
1323 static void dwarf2_free_abbrev_table (void *);
1324
1325 static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1326
1327 static struct partial_die_info *load_partial_dies
1328 (const struct die_reader_specs *, gdb_byte *, int);
1329
1330 static gdb_byte *read_partial_die (const struct die_reader_specs *,
1331 struct partial_die_info *,
1332 struct abbrev_info *,
1333 unsigned int,
1334 gdb_byte *);
1335
1336 static struct partial_die_info *find_partial_die (sect_offset, int,
1337 struct dwarf2_cu *);
1338
1339 static void fixup_partial_die (struct partial_die_info *,
1340 struct dwarf2_cu *);
1341
1342 static gdb_byte *read_attribute (const struct die_reader_specs *,
1343 struct attribute *, struct attr_abbrev *,
1344 gdb_byte *);
1345
1346 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1347
1348 static int read_1_signed_byte (bfd *, const gdb_byte *);
1349
1350 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1351
1352 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1353
1354 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1355
1356 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
1357 unsigned int *);
1358
1359 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1360
1361 static LONGEST read_checked_initial_length_and_offset
1362 (bfd *, gdb_byte *, const struct comp_unit_head *,
1363 unsigned int *, unsigned int *);
1364
1365 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
1366 unsigned int *);
1367
1368 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
1369
1370 static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1371 sect_offset);
1372
1373 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
1374
1375 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
1376
1377 static char *read_indirect_string (bfd *, gdb_byte *,
1378 const struct comp_unit_head *,
1379 unsigned int *);
1380
1381 static char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1382
1383 static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
1384
1385 static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
1386
1387 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1388 unsigned int *);
1389
1390 static char *read_str_index (const struct die_reader_specs *reader,
1391 struct dwarf2_cu *cu, ULONGEST str_index);
1392
1393 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1394
1395 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1396 struct dwarf2_cu *);
1397
1398 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1399 unsigned int);
1400
1401 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1402 struct dwarf2_cu *cu);
1403
1404 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1405
1406 static struct die_info *die_specification (struct die_info *die,
1407 struct dwarf2_cu **);
1408
1409 static void free_line_header (struct line_header *lh);
1410
1411 static void add_file_name (struct line_header *, char *, unsigned int,
1412 unsigned int, unsigned int);
1413
1414 static struct line_header *dwarf_decode_line_header (unsigned int offset,
1415 struct dwarf2_cu *cu);
1416
1417 static void dwarf_decode_lines (struct line_header *, const char *,
1418 struct dwarf2_cu *, struct partial_symtab *,
1419 int);
1420
1421 static void dwarf2_start_subfile (char *, const char *, const char *);
1422
1423 static void dwarf2_start_symtab (struct dwarf2_cu *,
1424 const char *, const char *, CORE_ADDR);
1425
1426 static struct symbol *new_symbol (struct die_info *, struct type *,
1427 struct dwarf2_cu *);
1428
1429 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1430 struct dwarf2_cu *, struct symbol *);
1431
1432 static void dwarf2_const_value (struct attribute *, struct symbol *,
1433 struct dwarf2_cu *);
1434
1435 static void dwarf2_const_value_attr (struct attribute *attr,
1436 struct type *type,
1437 const char *name,
1438 struct obstack *obstack,
1439 struct dwarf2_cu *cu, LONGEST *value,
1440 gdb_byte **bytes,
1441 struct dwarf2_locexpr_baton **baton);
1442
1443 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1444
1445 static int need_gnat_info (struct dwarf2_cu *);
1446
1447 static struct type *die_descriptive_type (struct die_info *,
1448 struct dwarf2_cu *);
1449
1450 static void set_descriptive_type (struct type *, struct die_info *,
1451 struct dwarf2_cu *);
1452
1453 static struct type *die_containing_type (struct die_info *,
1454 struct dwarf2_cu *);
1455
1456 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1457 struct dwarf2_cu *);
1458
1459 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1460
1461 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1462
1463 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1464
1465 static char *typename_concat (struct obstack *obs, const char *prefix,
1466 const char *suffix, int physname,
1467 struct dwarf2_cu *cu);
1468
1469 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1470
1471 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1472
1473 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1474
1475 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1476
1477 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1478
1479 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1480 struct dwarf2_cu *, struct partial_symtab *);
1481
1482 static int dwarf2_get_pc_bounds (struct die_info *,
1483 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1484 struct partial_symtab *);
1485
1486 static void get_scope_pc_bounds (struct die_info *,
1487 CORE_ADDR *, CORE_ADDR *,
1488 struct dwarf2_cu *);
1489
1490 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1491 CORE_ADDR, struct dwarf2_cu *);
1492
1493 static void dwarf2_add_field (struct field_info *, struct die_info *,
1494 struct dwarf2_cu *);
1495
1496 static void dwarf2_attach_fields_to_type (struct field_info *,
1497 struct type *, struct dwarf2_cu *);
1498
1499 static void dwarf2_add_member_fn (struct field_info *,
1500 struct die_info *, struct type *,
1501 struct dwarf2_cu *);
1502
1503 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1504 struct type *,
1505 struct dwarf2_cu *);
1506
1507 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1508
1509 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1510
1511 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1512
1513 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1514
1515 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1516
1517 static struct type *read_module_type (struct die_info *die,
1518 struct dwarf2_cu *cu);
1519
1520 static const char *namespace_name (struct die_info *die,
1521 int *is_anonymous, struct dwarf2_cu *);
1522
1523 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1524
1525 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1526
1527 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1528 struct dwarf2_cu *);
1529
1530 static struct die_info *read_die_and_children (const struct die_reader_specs *,
1531 gdb_byte *info_ptr,
1532 gdb_byte **new_info_ptr,
1533 struct die_info *parent);
1534
1535 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1536 gdb_byte *info_ptr,
1537 gdb_byte **new_info_ptr,
1538 struct die_info *parent);
1539
1540 static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1541 struct die_info **, gdb_byte *, int *, int);
1542
1543 static gdb_byte *read_full_die (const struct die_reader_specs *,
1544 struct die_info **, gdb_byte *, int *);
1545
1546 static void process_die (struct die_info *, struct dwarf2_cu *);
1547
1548 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1549 struct obstack *);
1550
1551 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1552
1553 static const char *dwarf2_full_name (const char *name,
1554 struct die_info *die,
1555 struct dwarf2_cu *cu);
1556
1557 static const char *dwarf2_physname (const char *name, struct die_info *die,
1558 struct dwarf2_cu *cu);
1559
1560 static struct die_info *dwarf2_extension (struct die_info *die,
1561 struct dwarf2_cu **);
1562
1563 static const char *dwarf_tag_name (unsigned int);
1564
1565 static const char *dwarf_attr_name (unsigned int);
1566
1567 static const char *dwarf_form_name (unsigned int);
1568
1569 static char *dwarf_bool_name (unsigned int);
1570
1571 static const char *dwarf_type_encoding_name (unsigned int);
1572
1573 static struct die_info *sibling_die (struct die_info *);
1574
1575 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1576
1577 static void dump_die_for_error (struct die_info *);
1578
1579 static void dump_die_1 (struct ui_file *, int level, int max_level,
1580 struct die_info *);
1581
1582 /*static*/ void dump_die (struct die_info *, int max_level);
1583
1584 static void store_in_ref_table (struct die_info *,
1585 struct dwarf2_cu *);
1586
1587 static int is_ref_attr (struct attribute *);
1588
1589 static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
1590
1591 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1592
1593 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1594 struct attribute *,
1595 struct dwarf2_cu **);
1596
1597 static struct die_info *follow_die_ref (struct die_info *,
1598 struct attribute *,
1599 struct dwarf2_cu **);
1600
1601 static struct die_info *follow_die_sig (struct die_info *,
1602 struct attribute *,
1603 struct dwarf2_cu **);
1604
1605 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1606
1607 static void read_signatured_type (struct signatured_type *);
1608
1609 static struct type_unit_group *get_type_unit_group
1610 (struct dwarf2_cu *, struct attribute *);
1611
1612 static void build_type_unit_groups (die_reader_func_ftype *, void *);
1613
1614 /* memory allocation interface */
1615
1616 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1617
1618 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1619
1620 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
1621 const char *, int);
1622
1623 static int attr_form_is_block (struct attribute *);
1624
1625 static int attr_form_is_section_offset (struct attribute *);
1626
1627 static int attr_form_is_constant (struct attribute *);
1628
1629 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1630 struct dwarf2_loclist_baton *baton,
1631 struct attribute *attr);
1632
1633 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1634 struct symbol *sym,
1635 struct dwarf2_cu *cu,
1636 int is_block);
1637
1638 static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1639 gdb_byte *info_ptr,
1640 struct abbrev_info *abbrev);
1641
1642 static void free_stack_comp_unit (void *);
1643
1644 static hashval_t partial_die_hash (const void *item);
1645
1646 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1647
1648 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1649 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
1650
1651 static void init_one_comp_unit (struct dwarf2_cu *cu,
1652 struct dwarf2_per_cu_data *per_cu);
1653
1654 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1655 struct die_info *comp_unit_die,
1656 enum language pretend_language);
1657
1658 static void free_heap_comp_unit (void *);
1659
1660 static void free_cached_comp_units (void *);
1661
1662 static void age_cached_comp_units (void);
1663
1664 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1665
1666 static struct type *set_die_type (struct die_info *, struct type *,
1667 struct dwarf2_cu *);
1668
1669 static void create_all_comp_units (struct objfile *);
1670
1671 static int create_all_type_units (struct objfile *);
1672
1673 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1674 enum language);
1675
1676 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1677 enum language);
1678
1679 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1680 enum language);
1681
1682 static void dwarf2_add_dependence (struct dwarf2_cu *,
1683 struct dwarf2_per_cu_data *);
1684
1685 static void dwarf2_mark (struct dwarf2_cu *);
1686
1687 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1688
1689 static struct type *get_die_type_at_offset (sect_offset,
1690 struct dwarf2_per_cu_data *per_cu);
1691
1692 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1693
1694 static void dwarf2_release_queue (void *dummy);
1695
1696 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1697 enum language pretend_language);
1698
1699 static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1700 struct dwarf2_per_cu_data *per_cu,
1701 enum language pretend_language);
1702
1703 static void process_queue (void);
1704
1705 static void find_file_and_directory (struct die_info *die,
1706 struct dwarf2_cu *cu,
1707 const char **name, const char **comp_dir);
1708
1709 static char *file_full_name (int file, struct line_header *lh,
1710 const char *comp_dir);
1711
1712 static gdb_byte *read_and_check_comp_unit_head
1713 (struct comp_unit_head *header,
1714 struct dwarf2_section_info *section,
1715 struct dwarf2_section_info *abbrev_section, gdb_byte *info_ptr,
1716 int is_debug_types_section);
1717
1718 static void init_cutu_and_read_dies
1719 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1720 int use_existing_cu, int keep,
1721 die_reader_func_ftype *die_reader_func, void *data);
1722
1723 static void init_cutu_and_read_dies_simple
1724 (struct dwarf2_per_cu_data *this_cu,
1725 die_reader_func_ftype *die_reader_func, void *data);
1726
1727 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1728
1729 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1730
1731 static struct dwo_unit *lookup_dwo_comp_unit
1732 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1733
1734 static struct dwo_unit *lookup_dwo_type_unit
1735 (struct signatured_type *, const char *, const char *);
1736
1737 static void free_dwo_file_cleanup (void *);
1738
1739 static void process_cu_includes (void);
1740
1741 static void check_producer (struct dwarf2_cu *cu);
1742
1743 #if WORDS_BIGENDIAN
1744
1745 /* Convert VALUE between big- and little-endian. */
1746 static offset_type
1747 byte_swap (offset_type value)
1748 {
1749 offset_type result;
1750
1751 result = (value & 0xff) << 24;
1752 result |= (value & 0xff00) << 8;
1753 result |= (value & 0xff0000) >> 8;
1754 result |= (value & 0xff000000) >> 24;
1755 return result;
1756 }
1757
1758 #define MAYBE_SWAP(V) byte_swap (V)
1759
1760 #else
1761 #define MAYBE_SWAP(V) (V)
1762 #endif /* WORDS_BIGENDIAN */
1763
1764 /* The suffix for an index file. */
1765 #define INDEX_SUFFIX ".gdb-index"
1766
1767 /* Try to locate the sections we need for DWARF 2 debugging
1768 information and return true if we have enough to do something.
1769 NAMES points to the dwarf2 section names, or is NULL if the standard
1770 ELF names are used. */
1771
1772 int
1773 dwarf2_has_info (struct objfile *objfile,
1774 const struct dwarf2_debug_sections *names)
1775 {
1776 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1777 if (!dwarf2_per_objfile)
1778 {
1779 /* Initialize per-objfile state. */
1780 struct dwarf2_per_objfile *data
1781 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1782
1783 memset (data, 0, sizeof (*data));
1784 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1785 dwarf2_per_objfile = data;
1786
1787 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1788 (void *) names);
1789 dwarf2_per_objfile->objfile = objfile;
1790 }
1791 return (dwarf2_per_objfile->info.asection != NULL
1792 && dwarf2_per_objfile->abbrev.asection != NULL);
1793 }
1794
1795 /* When loading sections, we look either for uncompressed section or for
1796 compressed section names. */
1797
1798 static int
1799 section_is_p (const char *section_name,
1800 const struct dwarf2_section_names *names)
1801 {
1802 if (names->normal != NULL
1803 && strcmp (section_name, names->normal) == 0)
1804 return 1;
1805 if (names->compressed != NULL
1806 && strcmp (section_name, names->compressed) == 0)
1807 return 1;
1808 return 0;
1809 }
1810
1811 /* This function is mapped across the sections and remembers the
1812 offset and size of each of the debugging sections we are interested
1813 in. */
1814
1815 static void
1816 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1817 {
1818 const struct dwarf2_debug_sections *names;
1819 flagword aflag = bfd_get_section_flags (abfd, sectp);
1820
1821 if (vnames == NULL)
1822 names = &dwarf2_elf_names;
1823 else
1824 names = (const struct dwarf2_debug_sections *) vnames;
1825
1826 if ((aflag & SEC_HAS_CONTENTS) == 0)
1827 {
1828 }
1829 else if (section_is_p (sectp->name, &names->info))
1830 {
1831 dwarf2_per_objfile->info.asection = sectp;
1832 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1833 }
1834 else if (section_is_p (sectp->name, &names->abbrev))
1835 {
1836 dwarf2_per_objfile->abbrev.asection = sectp;
1837 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1838 }
1839 else if (section_is_p (sectp->name, &names->line))
1840 {
1841 dwarf2_per_objfile->line.asection = sectp;
1842 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1843 }
1844 else if (section_is_p (sectp->name, &names->loc))
1845 {
1846 dwarf2_per_objfile->loc.asection = sectp;
1847 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1848 }
1849 else if (section_is_p (sectp->name, &names->macinfo))
1850 {
1851 dwarf2_per_objfile->macinfo.asection = sectp;
1852 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1853 }
1854 else if (section_is_p (sectp->name, &names->macro))
1855 {
1856 dwarf2_per_objfile->macro.asection = sectp;
1857 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1858 }
1859 else if (section_is_p (sectp->name, &names->str))
1860 {
1861 dwarf2_per_objfile->str.asection = sectp;
1862 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1863 }
1864 else if (section_is_p (sectp->name, &names->addr))
1865 {
1866 dwarf2_per_objfile->addr.asection = sectp;
1867 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1868 }
1869 else if (section_is_p (sectp->name, &names->frame))
1870 {
1871 dwarf2_per_objfile->frame.asection = sectp;
1872 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1873 }
1874 else if (section_is_p (sectp->name, &names->eh_frame))
1875 {
1876 dwarf2_per_objfile->eh_frame.asection = sectp;
1877 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1878 }
1879 else if (section_is_p (sectp->name, &names->ranges))
1880 {
1881 dwarf2_per_objfile->ranges.asection = sectp;
1882 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1883 }
1884 else if (section_is_p (sectp->name, &names->types))
1885 {
1886 struct dwarf2_section_info type_section;
1887
1888 memset (&type_section, 0, sizeof (type_section));
1889 type_section.asection = sectp;
1890 type_section.size = bfd_get_section_size (sectp);
1891
1892 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1893 &type_section);
1894 }
1895 else if (section_is_p (sectp->name, &names->gdb_index))
1896 {
1897 dwarf2_per_objfile->gdb_index.asection = sectp;
1898 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1899 }
1900
1901 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1902 && bfd_section_vma (abfd, sectp) == 0)
1903 dwarf2_per_objfile->has_section_at_zero = 1;
1904 }
1905
1906 /* A helper function that decides whether a section is empty,
1907 or not present. */
1908
1909 static int
1910 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1911 {
1912 return info->asection == NULL || info->size == 0;
1913 }
1914
1915 /* Read the contents of the section INFO.
1916 OBJFILE is the main object file, but not necessarily the file where
1917 the section comes from. E.g., for DWO files INFO->asection->owner
1918 is the bfd of the DWO file.
1919 If the section is compressed, uncompress it before returning. */
1920
1921 static void
1922 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1923 {
1924 asection *sectp = info->asection;
1925 bfd *abfd;
1926 gdb_byte *buf, *retbuf;
1927 unsigned char header[4];
1928
1929 if (info->readin)
1930 return;
1931 info->buffer = NULL;
1932 info->readin = 1;
1933
1934 if (dwarf2_section_empty_p (info))
1935 return;
1936
1937 abfd = sectp->owner;
1938
1939 /* If the section has relocations, we must read it ourselves.
1940 Otherwise we attach it to the BFD. */
1941 if ((sectp->flags & SEC_RELOC) == 0)
1942 {
1943 const gdb_byte *bytes = gdb_bfd_map_section (sectp, &info->size);
1944
1945 /* We have to cast away const here for historical reasons.
1946 Fixing dwarf2read to be const-correct would be quite nice. */
1947 info->buffer = (gdb_byte *) bytes;
1948 return;
1949 }
1950
1951 buf = obstack_alloc (&objfile->objfile_obstack, info->size);
1952 info->buffer = buf;
1953
1954 /* When debugging .o files, we may need to apply relocations; see
1955 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1956 We never compress sections in .o files, so we only need to
1957 try this when the section is not compressed. */
1958 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1959 if (retbuf != NULL)
1960 {
1961 info->buffer = retbuf;
1962 return;
1963 }
1964
1965 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1966 || bfd_bread (buf, info->size, abfd) != info->size)
1967 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1968 bfd_get_filename (abfd));
1969 }
1970
1971 /* A helper function that returns the size of a section in a safe way.
1972 If you are positive that the section has been read before using the
1973 size, then it is safe to refer to the dwarf2_section_info object's
1974 "size" field directly. In other cases, you must call this
1975 function, because for compressed sections the size field is not set
1976 correctly until the section has been read. */
1977
1978 static bfd_size_type
1979 dwarf2_section_size (struct objfile *objfile,
1980 struct dwarf2_section_info *info)
1981 {
1982 if (!info->readin)
1983 dwarf2_read_section (objfile, info);
1984 return info->size;
1985 }
1986
1987 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1988 SECTION_NAME. */
1989
1990 void
1991 dwarf2_get_section_info (struct objfile *objfile,
1992 enum dwarf2_section_enum sect,
1993 asection **sectp, gdb_byte **bufp,
1994 bfd_size_type *sizep)
1995 {
1996 struct dwarf2_per_objfile *data
1997 = objfile_data (objfile, dwarf2_objfile_data_key);
1998 struct dwarf2_section_info *info;
1999
2000 /* We may see an objfile without any DWARF, in which case we just
2001 return nothing. */
2002 if (data == NULL)
2003 {
2004 *sectp = NULL;
2005 *bufp = NULL;
2006 *sizep = 0;
2007 return;
2008 }
2009 switch (sect)
2010 {
2011 case DWARF2_DEBUG_FRAME:
2012 info = &data->frame;
2013 break;
2014 case DWARF2_EH_FRAME:
2015 info = &data->eh_frame;
2016 break;
2017 default:
2018 gdb_assert_not_reached ("unexpected section");
2019 }
2020
2021 dwarf2_read_section (objfile, info);
2022
2023 *sectp = info->asection;
2024 *bufp = info->buffer;
2025 *sizep = info->size;
2026 }
2027
2028 /* A helper function to find the sections for a .dwz file. */
2029
2030 static void
2031 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2032 {
2033 struct dwz_file *dwz_file = arg;
2034
2035 /* Note that we only support the standard ELF names, because .dwz
2036 is ELF-only (at the time of writing). */
2037 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2038 {
2039 dwz_file->abbrev.asection = sectp;
2040 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2041 }
2042 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2043 {
2044 dwz_file->info.asection = sectp;
2045 dwz_file->info.size = bfd_get_section_size (sectp);
2046 }
2047 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2048 {
2049 dwz_file->str.asection = sectp;
2050 dwz_file->str.size = bfd_get_section_size (sectp);
2051 }
2052 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2053 {
2054 dwz_file->line.asection = sectp;
2055 dwz_file->line.size = bfd_get_section_size (sectp);
2056 }
2057 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2058 {
2059 dwz_file->macro.asection = sectp;
2060 dwz_file->macro.size = bfd_get_section_size (sectp);
2061 }
2062 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2063 {
2064 dwz_file->gdb_index.asection = sectp;
2065 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2066 }
2067 }
2068
2069 /* Open the separate '.dwz' debug file, if needed. Error if the file
2070 cannot be found. */
2071
2072 static struct dwz_file *
2073 dwarf2_get_dwz_file (void)
2074 {
2075 bfd *abfd, *dwz_bfd;
2076 asection *section;
2077 gdb_byte *data;
2078 struct cleanup *cleanup;
2079 const char *filename;
2080 struct dwz_file *result;
2081
2082 if (dwarf2_per_objfile->dwz_file != NULL)
2083 return dwarf2_per_objfile->dwz_file;
2084
2085 abfd = dwarf2_per_objfile->objfile->obfd;
2086 section = bfd_get_section_by_name (abfd, ".gnu_debugaltlink");
2087 if (section == NULL)
2088 error (_("could not find '.gnu_debugaltlink' section"));
2089 if (!bfd_malloc_and_get_section (abfd, section, &data))
2090 error (_("could not read '.gnu_debugaltlink' section: %s"),
2091 bfd_errmsg (bfd_get_error ()));
2092 cleanup = make_cleanup (xfree, data);
2093
2094 filename = data;
2095 if (!IS_ABSOLUTE_PATH (filename))
2096 {
2097 char *abs = gdb_realpath (dwarf2_per_objfile->objfile->name);
2098 char *rel;
2099
2100 make_cleanup (xfree, abs);
2101 abs = ldirname (abs);
2102 make_cleanup (xfree, abs);
2103
2104 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2105 make_cleanup (xfree, rel);
2106 filename = rel;
2107 }
2108
2109 /* The format is just a NUL-terminated file name, followed by the
2110 build-id. For now, though, we ignore the build-id. */
2111 dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2112 if (dwz_bfd == NULL)
2113 error (_("could not read '%s': %s"), filename,
2114 bfd_errmsg (bfd_get_error ()));
2115
2116 if (!bfd_check_format (dwz_bfd, bfd_object))
2117 {
2118 gdb_bfd_unref (dwz_bfd);
2119 error (_("file '%s' was not usable: %s"), filename,
2120 bfd_errmsg (bfd_get_error ()));
2121 }
2122
2123 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2124 struct dwz_file);
2125 result->dwz_bfd = dwz_bfd;
2126
2127 bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2128
2129 do_cleanups (cleanup);
2130
2131 dwarf2_per_objfile->dwz_file = result;
2132 return result;
2133 }
2134 \f
2135 /* DWARF quick_symbols_functions support. */
2136
2137 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2138 unique line tables, so we maintain a separate table of all .debug_line
2139 derived entries to support the sharing.
2140 All the quick functions need is the list of file names. We discard the
2141 line_header when we're done and don't need to record it here. */
2142 struct quick_file_names
2143 {
2144 /* The data used to construct the hash key. */
2145 struct stmt_list_hash hash;
2146
2147 /* The number of entries in file_names, real_names. */
2148 unsigned int num_file_names;
2149
2150 /* The file names from the line table, after being run through
2151 file_full_name. */
2152 const char **file_names;
2153
2154 /* The file names from the line table after being run through
2155 gdb_realpath. These are computed lazily. */
2156 const char **real_names;
2157 };
2158
2159 /* When using the index (and thus not using psymtabs), each CU has an
2160 object of this type. This is used to hold information needed by
2161 the various "quick" methods. */
2162 struct dwarf2_per_cu_quick_data
2163 {
2164 /* The file table. This can be NULL if there was no file table
2165 or it's currently not read in.
2166 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2167 struct quick_file_names *file_names;
2168
2169 /* The corresponding symbol table. This is NULL if symbols for this
2170 CU have not yet been read. */
2171 struct symtab *symtab;
2172
2173 /* A temporary mark bit used when iterating over all CUs in
2174 expand_symtabs_matching. */
2175 unsigned int mark : 1;
2176
2177 /* True if we've tried to read the file table and found there isn't one.
2178 There will be no point in trying to read it again next time. */
2179 unsigned int no_file_data : 1;
2180 };
2181
2182 /* Utility hash function for a stmt_list_hash. */
2183
2184 static hashval_t
2185 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2186 {
2187 hashval_t v = 0;
2188
2189 if (stmt_list_hash->dwo_unit != NULL)
2190 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2191 v += stmt_list_hash->line_offset.sect_off;
2192 return v;
2193 }
2194
2195 /* Utility equality function for a stmt_list_hash. */
2196
2197 static int
2198 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2199 const struct stmt_list_hash *rhs)
2200 {
2201 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2202 return 0;
2203 if (lhs->dwo_unit != NULL
2204 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2205 return 0;
2206
2207 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2208 }
2209
2210 /* Hash function for a quick_file_names. */
2211
2212 static hashval_t
2213 hash_file_name_entry (const void *e)
2214 {
2215 const struct quick_file_names *file_data = e;
2216
2217 return hash_stmt_list_entry (&file_data->hash);
2218 }
2219
2220 /* Equality function for a quick_file_names. */
2221
2222 static int
2223 eq_file_name_entry (const void *a, const void *b)
2224 {
2225 const struct quick_file_names *ea = a;
2226 const struct quick_file_names *eb = b;
2227
2228 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2229 }
2230
2231 /* Delete function for a quick_file_names. */
2232
2233 static void
2234 delete_file_name_entry (void *e)
2235 {
2236 struct quick_file_names *file_data = e;
2237 int i;
2238
2239 for (i = 0; i < file_data->num_file_names; ++i)
2240 {
2241 xfree ((void*) file_data->file_names[i]);
2242 if (file_data->real_names)
2243 xfree ((void*) file_data->real_names[i]);
2244 }
2245
2246 /* The space for the struct itself lives on objfile_obstack,
2247 so we don't free it here. */
2248 }
2249
2250 /* Create a quick_file_names hash table. */
2251
2252 static htab_t
2253 create_quick_file_names_table (unsigned int nr_initial_entries)
2254 {
2255 return htab_create_alloc (nr_initial_entries,
2256 hash_file_name_entry, eq_file_name_entry,
2257 delete_file_name_entry, xcalloc, xfree);
2258 }
2259
2260 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2261 have to be created afterwards. You should call age_cached_comp_units after
2262 processing PER_CU->CU. dw2_setup must have been already called. */
2263
2264 static void
2265 load_cu (struct dwarf2_per_cu_data *per_cu)
2266 {
2267 if (per_cu->is_debug_types)
2268 load_full_type_unit (per_cu);
2269 else
2270 load_full_comp_unit (per_cu, language_minimal);
2271
2272 gdb_assert (per_cu->cu != NULL);
2273
2274 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2275 }
2276
2277 /* Read in the symbols for PER_CU. */
2278
2279 static void
2280 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2281 {
2282 struct cleanup *back_to;
2283
2284 /* Skip type_unit_groups, reading the type units they contain
2285 is handled elsewhere. */
2286 if (IS_TYPE_UNIT_GROUP (per_cu))
2287 return;
2288
2289 back_to = make_cleanup (dwarf2_release_queue, NULL);
2290
2291 if (dwarf2_per_objfile->using_index
2292 ? per_cu->v.quick->symtab == NULL
2293 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2294 {
2295 queue_comp_unit (per_cu, language_minimal);
2296 load_cu (per_cu);
2297 }
2298
2299 process_queue ();
2300
2301 /* Age the cache, releasing compilation units that have not
2302 been used recently. */
2303 age_cached_comp_units ();
2304
2305 do_cleanups (back_to);
2306 }
2307
2308 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2309 the objfile from which this CU came. Returns the resulting symbol
2310 table. */
2311
2312 static struct symtab *
2313 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2314 {
2315 gdb_assert (dwarf2_per_objfile->using_index);
2316 if (!per_cu->v.quick->symtab)
2317 {
2318 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2319 increment_reading_symtab ();
2320 dw2_do_instantiate_symtab (per_cu);
2321 process_cu_includes ();
2322 do_cleanups (back_to);
2323 }
2324 return per_cu->v.quick->symtab;
2325 }
2326
2327 /* Return the CU given its index.
2328
2329 This is intended for loops like:
2330
2331 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2332 + dwarf2_per_objfile->n_type_units); ++i)
2333 {
2334 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2335
2336 ...;
2337 }
2338 */
2339
2340 static struct dwarf2_per_cu_data *
2341 dw2_get_cu (int index)
2342 {
2343 if (index >= dwarf2_per_objfile->n_comp_units)
2344 {
2345 index -= dwarf2_per_objfile->n_comp_units;
2346 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2347 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
2348 }
2349
2350 return dwarf2_per_objfile->all_comp_units[index];
2351 }
2352
2353 /* Return the primary CU given its index.
2354 The difference between this function and dw2_get_cu is in the handling
2355 of type units (TUs). Here we return the type_unit_group object.
2356
2357 This is intended for loops like:
2358
2359 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2360 + dwarf2_per_objfile->n_type_unit_groups); ++i)
2361 {
2362 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2363
2364 ...;
2365 }
2366 */
2367
2368 static struct dwarf2_per_cu_data *
2369 dw2_get_primary_cu (int index)
2370 {
2371 if (index >= dwarf2_per_objfile->n_comp_units)
2372 {
2373 index -= dwarf2_per_objfile->n_comp_units;
2374 gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2375 return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
2376 }
2377
2378 return dwarf2_per_objfile->all_comp_units[index];
2379 }
2380
2381 /* A helper for create_cus_from_index that handles a given list of
2382 CUs. */
2383
2384 static void
2385 create_cus_from_index_list (struct objfile *objfile,
2386 const gdb_byte *cu_list, offset_type n_elements,
2387 struct dwarf2_section_info *section,
2388 int is_dwz,
2389 int base_offset)
2390 {
2391 offset_type i;
2392
2393 for (i = 0; i < n_elements; i += 2)
2394 {
2395 struct dwarf2_per_cu_data *the_cu;
2396 ULONGEST offset, length;
2397
2398 gdb_static_assert (sizeof (ULONGEST) >= 8);
2399 offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2400 length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2401 cu_list += 2 * 8;
2402
2403 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2404 struct dwarf2_per_cu_data);
2405 the_cu->offset.sect_off = offset;
2406 the_cu->length = length;
2407 the_cu->objfile = objfile;
2408 the_cu->section = section;
2409 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2410 struct dwarf2_per_cu_quick_data);
2411 the_cu->is_dwz = is_dwz;
2412 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
2413 }
2414 }
2415
2416 /* Read the CU list from the mapped index, and use it to create all
2417 the CU objects for this objfile. */
2418
2419 static void
2420 create_cus_from_index (struct objfile *objfile,
2421 const gdb_byte *cu_list, offset_type cu_list_elements,
2422 const gdb_byte *dwz_list, offset_type dwz_elements)
2423 {
2424 struct dwz_file *dwz;
2425
2426 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2427 dwarf2_per_objfile->all_comp_units
2428 = obstack_alloc (&objfile->objfile_obstack,
2429 dwarf2_per_objfile->n_comp_units
2430 * sizeof (struct dwarf2_per_cu_data *));
2431
2432 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2433 &dwarf2_per_objfile->info, 0, 0);
2434
2435 if (dwz_elements == 0)
2436 return;
2437
2438 dwz = dwarf2_get_dwz_file ();
2439 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2440 cu_list_elements / 2);
2441 }
2442
2443 /* Create the signatured type hash table from the index. */
2444
2445 static void
2446 create_signatured_type_table_from_index (struct objfile *objfile,
2447 struct dwarf2_section_info *section,
2448 const gdb_byte *bytes,
2449 offset_type elements)
2450 {
2451 offset_type i;
2452 htab_t sig_types_hash;
2453
2454 dwarf2_per_objfile->n_type_units = elements / 3;
2455 dwarf2_per_objfile->all_type_units
2456 = obstack_alloc (&objfile->objfile_obstack,
2457 dwarf2_per_objfile->n_type_units
2458 * sizeof (struct signatured_type *));
2459
2460 sig_types_hash = allocate_signatured_type_table (objfile);
2461
2462 for (i = 0; i < elements; i += 3)
2463 {
2464 struct signatured_type *sig_type;
2465 ULONGEST offset, type_offset_in_tu, signature;
2466 void **slot;
2467
2468 gdb_static_assert (sizeof (ULONGEST) >= 8);
2469 offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2470 type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
2471 BFD_ENDIAN_LITTLE);
2472 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2473 bytes += 3 * 8;
2474
2475 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2476 struct signatured_type);
2477 sig_type->signature = signature;
2478 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2479 sig_type->per_cu.is_debug_types = 1;
2480 sig_type->per_cu.section = section;
2481 sig_type->per_cu.offset.sect_off = offset;
2482 sig_type->per_cu.objfile = objfile;
2483 sig_type->per_cu.v.quick
2484 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2485 struct dwarf2_per_cu_quick_data);
2486
2487 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2488 *slot = sig_type;
2489
2490 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
2491 }
2492
2493 dwarf2_per_objfile->signatured_types = sig_types_hash;
2494 }
2495
2496 /* Read the address map data from the mapped index, and use it to
2497 populate the objfile's psymtabs_addrmap. */
2498
2499 static void
2500 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2501 {
2502 const gdb_byte *iter, *end;
2503 struct obstack temp_obstack;
2504 struct addrmap *mutable_map;
2505 struct cleanup *cleanup;
2506 CORE_ADDR baseaddr;
2507
2508 obstack_init (&temp_obstack);
2509 cleanup = make_cleanup_obstack_free (&temp_obstack);
2510 mutable_map = addrmap_create_mutable (&temp_obstack);
2511
2512 iter = index->address_table;
2513 end = iter + index->address_table_size;
2514
2515 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2516
2517 while (iter < end)
2518 {
2519 ULONGEST hi, lo, cu_index;
2520 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2521 iter += 8;
2522 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2523 iter += 8;
2524 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2525 iter += 4;
2526
2527 if (cu_index < dwarf2_per_objfile->n_comp_units)
2528 {
2529 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2530 dw2_get_cu (cu_index));
2531 }
2532 else
2533 {
2534 complaint (&symfile_complaints,
2535 _(".gdb_index address table has invalid CU number %u"),
2536 (unsigned) cu_index);
2537 }
2538 }
2539
2540 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2541 &objfile->objfile_obstack);
2542 do_cleanups (cleanup);
2543 }
2544
2545 /* The hash function for strings in the mapped index. This is the same as
2546 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2547 implementation. This is necessary because the hash function is tied to the
2548 format of the mapped index file. The hash values do not have to match with
2549 SYMBOL_HASH_NEXT.
2550
2551 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2552
2553 static hashval_t
2554 mapped_index_string_hash (int index_version, const void *p)
2555 {
2556 const unsigned char *str = (const unsigned char *) p;
2557 hashval_t r = 0;
2558 unsigned char c;
2559
2560 while ((c = *str++) != 0)
2561 {
2562 if (index_version >= 5)
2563 c = tolower (c);
2564 r = r * 67 + c - 113;
2565 }
2566
2567 return r;
2568 }
2569
2570 /* Find a slot in the mapped index INDEX for the object named NAME.
2571 If NAME is found, set *VEC_OUT to point to the CU vector in the
2572 constant pool and return 1. If NAME cannot be found, return 0. */
2573
2574 static int
2575 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2576 offset_type **vec_out)
2577 {
2578 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2579 offset_type hash;
2580 offset_type slot, step;
2581 int (*cmp) (const char *, const char *);
2582
2583 if (current_language->la_language == language_cplus
2584 || current_language->la_language == language_java
2585 || current_language->la_language == language_fortran)
2586 {
2587 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2588 not contain any. */
2589 const char *paren = strchr (name, '(');
2590
2591 if (paren)
2592 {
2593 char *dup;
2594
2595 dup = xmalloc (paren - name + 1);
2596 memcpy (dup, name, paren - name);
2597 dup[paren - name] = 0;
2598
2599 make_cleanup (xfree, dup);
2600 name = dup;
2601 }
2602 }
2603
2604 /* Index version 4 did not support case insensitive searches. But the
2605 indices for case insensitive languages are built in lowercase, therefore
2606 simulate our NAME being searched is also lowercased. */
2607 hash = mapped_index_string_hash ((index->version == 4
2608 && case_sensitivity == case_sensitive_off
2609 ? 5 : index->version),
2610 name);
2611
2612 slot = hash & (index->symbol_table_slots - 1);
2613 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2614 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2615
2616 for (;;)
2617 {
2618 /* Convert a slot number to an offset into the table. */
2619 offset_type i = 2 * slot;
2620 const char *str;
2621 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2622 {
2623 do_cleanups (back_to);
2624 return 0;
2625 }
2626
2627 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2628 if (!cmp (name, str))
2629 {
2630 *vec_out = (offset_type *) (index->constant_pool
2631 + MAYBE_SWAP (index->symbol_table[i + 1]));
2632 do_cleanups (back_to);
2633 return 1;
2634 }
2635
2636 slot = (slot + step) & (index->symbol_table_slots - 1);
2637 }
2638 }
2639
2640 /* A helper function that reads the .gdb_index from SECTION and fills
2641 in MAP. FILENAME is the name of the file containing the section;
2642 it is used for error reporting. DEPRECATED_OK is nonzero if it is
2643 ok to use deprecated sections.
2644
2645 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2646 out parameters that are filled in with information about the CU and
2647 TU lists in the section.
2648
2649 Returns 1 if all went well, 0 otherwise. */
2650
2651 static int
2652 read_index_from_section (struct objfile *objfile,
2653 const char *filename,
2654 int deprecated_ok,
2655 struct dwarf2_section_info *section,
2656 struct mapped_index *map,
2657 const gdb_byte **cu_list,
2658 offset_type *cu_list_elements,
2659 const gdb_byte **types_list,
2660 offset_type *types_list_elements)
2661 {
2662 char *addr;
2663 offset_type version;
2664 offset_type *metadata;
2665 int i;
2666
2667 if (dwarf2_section_empty_p (section))
2668 return 0;
2669
2670 /* Older elfutils strip versions could keep the section in the main
2671 executable while splitting it for the separate debug info file. */
2672 if ((bfd_get_file_flags (section->asection) & SEC_HAS_CONTENTS) == 0)
2673 return 0;
2674
2675 dwarf2_read_section (objfile, section);
2676
2677 addr = section->buffer;
2678 /* Version check. */
2679 version = MAYBE_SWAP (*(offset_type *) addr);
2680 /* Versions earlier than 3 emitted every copy of a psymbol. This
2681 causes the index to behave very poorly for certain requests. Version 3
2682 contained incomplete addrmap. So, it seems better to just ignore such
2683 indices. */
2684 if (version < 4)
2685 {
2686 static int warning_printed = 0;
2687 if (!warning_printed)
2688 {
2689 warning (_("Skipping obsolete .gdb_index section in %s."),
2690 filename);
2691 warning_printed = 1;
2692 }
2693 return 0;
2694 }
2695 /* Index version 4 uses a different hash function than index version
2696 5 and later.
2697
2698 Versions earlier than 6 did not emit psymbols for inlined
2699 functions. Using these files will cause GDB not to be able to
2700 set breakpoints on inlined functions by name, so we ignore these
2701 indices unless the user has done
2702 "set use-deprecated-index-sections on". */
2703 if (version < 6 && !deprecated_ok)
2704 {
2705 static int warning_printed = 0;
2706 if (!warning_printed)
2707 {
2708 warning (_("\
2709 Skipping deprecated .gdb_index section in %s.\n\
2710 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2711 to use the section anyway."),
2712 filename);
2713 warning_printed = 1;
2714 }
2715 return 0;
2716 }
2717 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2718 of the TU (for symbols coming from TUs). It's just a performance bug, and
2719 we can't distinguish gdb-generated indices from gold-generated ones, so
2720 nothing to do here. */
2721
2722 /* Indexes with higher version than the one supported by GDB may be no
2723 longer backward compatible. */
2724 if (version > 8)
2725 return 0;
2726
2727 map->version = version;
2728 map->total_size = section->size;
2729
2730 metadata = (offset_type *) (addr + sizeof (offset_type));
2731
2732 i = 0;
2733 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2734 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2735 / 8);
2736 ++i;
2737
2738 *types_list = addr + MAYBE_SWAP (metadata[i]);
2739 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2740 - MAYBE_SWAP (metadata[i]))
2741 / 8);
2742 ++i;
2743
2744 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2745 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2746 - MAYBE_SWAP (metadata[i]));
2747 ++i;
2748
2749 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2750 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2751 - MAYBE_SWAP (metadata[i]))
2752 / (2 * sizeof (offset_type)));
2753 ++i;
2754
2755 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2756
2757 return 1;
2758 }
2759
2760
2761 /* Read the index file. If everything went ok, initialize the "quick"
2762 elements of all the CUs and return 1. Otherwise, return 0. */
2763
2764 static int
2765 dwarf2_read_index (struct objfile *objfile)
2766 {
2767 struct mapped_index local_map, *map;
2768 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2769 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2770
2771 if (!read_index_from_section (objfile, objfile->name,
2772 use_deprecated_index_sections,
2773 &dwarf2_per_objfile->gdb_index, &local_map,
2774 &cu_list, &cu_list_elements,
2775 &types_list, &types_list_elements))
2776 return 0;
2777
2778 /* Don't use the index if it's empty. */
2779 if (local_map.symbol_table_slots == 0)
2780 return 0;
2781
2782 /* If there is a .dwz file, read it so we can get its CU list as
2783 well. */
2784 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
2785 {
2786 struct dwz_file *dwz = dwarf2_get_dwz_file ();
2787 struct mapped_index dwz_map;
2788 const gdb_byte *dwz_types_ignore;
2789 offset_type dwz_types_elements_ignore;
2790
2791 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2792 1,
2793 &dwz->gdb_index, &dwz_map,
2794 &dwz_list, &dwz_list_elements,
2795 &dwz_types_ignore,
2796 &dwz_types_elements_ignore))
2797 {
2798 warning (_("could not read '.gdb_index' section from %s; skipping"),
2799 bfd_get_filename (dwz->dwz_bfd));
2800 return 0;
2801 }
2802 }
2803
2804 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
2805 dwz_list_elements);
2806
2807 if (types_list_elements)
2808 {
2809 struct dwarf2_section_info *section;
2810
2811 /* We can only handle a single .debug_types when we have an
2812 index. */
2813 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2814 return 0;
2815
2816 section = VEC_index (dwarf2_section_info_def,
2817 dwarf2_per_objfile->types, 0);
2818
2819 create_signatured_type_table_from_index (objfile, section, types_list,
2820 types_list_elements);
2821 }
2822
2823 create_addrmap_from_index (objfile, &local_map);
2824
2825 map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2826 *map = local_map;
2827
2828 dwarf2_per_objfile->index_table = map;
2829 dwarf2_per_objfile->using_index = 1;
2830 dwarf2_per_objfile->quick_file_names_table =
2831 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2832
2833 return 1;
2834 }
2835
2836 /* A helper for the "quick" functions which sets the global
2837 dwarf2_per_objfile according to OBJFILE. */
2838
2839 static void
2840 dw2_setup (struct objfile *objfile)
2841 {
2842 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2843 gdb_assert (dwarf2_per_objfile);
2844 }
2845
2846 /* die_reader_func for dw2_get_file_names. */
2847
2848 static void
2849 dw2_get_file_names_reader (const struct die_reader_specs *reader,
2850 gdb_byte *info_ptr,
2851 struct die_info *comp_unit_die,
2852 int has_children,
2853 void *data)
2854 {
2855 struct dwarf2_cu *cu = reader->cu;
2856 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2857 struct objfile *objfile = dwarf2_per_objfile->objfile;
2858 struct dwarf2_per_cu_data *lh_cu;
2859 struct line_header *lh;
2860 struct attribute *attr;
2861 int i;
2862 const char *name, *comp_dir;
2863 void **slot;
2864 struct quick_file_names *qfn;
2865 unsigned int line_offset;
2866
2867 gdb_assert (! this_cu->is_debug_types);
2868
2869 /* Our callers never want to match partial units -- instead they
2870 will match the enclosing full CU. */
2871 if (comp_unit_die->tag == DW_TAG_partial_unit)
2872 {
2873 this_cu->v.quick->no_file_data = 1;
2874 return;
2875 }
2876
2877 lh_cu = this_cu;
2878 lh = NULL;
2879 slot = NULL;
2880 line_offset = 0;
2881
2882 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2883 if (attr)
2884 {
2885 struct quick_file_names find_entry;
2886
2887 line_offset = DW_UNSND (attr);
2888
2889 /* We may have already read in this line header (TU line header sharing).
2890 If we have we're done. */
2891 find_entry.hash.dwo_unit = cu->dwo_unit;
2892 find_entry.hash.line_offset.sect_off = line_offset;
2893 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2894 &find_entry, INSERT);
2895 if (*slot != NULL)
2896 {
2897 lh_cu->v.quick->file_names = *slot;
2898 return;
2899 }
2900
2901 lh = dwarf_decode_line_header (line_offset, cu);
2902 }
2903 if (lh == NULL)
2904 {
2905 lh_cu->v.quick->no_file_data = 1;
2906 return;
2907 }
2908
2909 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2910 qfn->hash.dwo_unit = cu->dwo_unit;
2911 qfn->hash.line_offset.sect_off = line_offset;
2912 gdb_assert (slot != NULL);
2913 *slot = qfn;
2914
2915 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
2916
2917 qfn->num_file_names = lh->num_file_names;
2918 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2919 lh->num_file_names * sizeof (char *));
2920 for (i = 0; i < lh->num_file_names; ++i)
2921 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2922 qfn->real_names = NULL;
2923
2924 free_line_header (lh);
2925
2926 lh_cu->v.quick->file_names = qfn;
2927 }
2928
2929 /* A helper for the "quick" functions which attempts to read the line
2930 table for THIS_CU. */
2931
2932 static struct quick_file_names *
2933 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
2934 {
2935 /* This should never be called for TUs. */
2936 gdb_assert (! this_cu->is_debug_types);
2937 /* Nor type unit groups. */
2938 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
2939
2940 if (this_cu->v.quick->file_names != NULL)
2941 return this_cu->v.quick->file_names;
2942 /* If we know there is no line data, no point in looking again. */
2943 if (this_cu->v.quick->no_file_data)
2944 return NULL;
2945
2946 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
2947
2948 if (this_cu->v.quick->no_file_data)
2949 return NULL;
2950 return this_cu->v.quick->file_names;
2951 }
2952
2953 /* A helper for the "quick" functions which computes and caches the
2954 real path for a given file name from the line table. */
2955
2956 static const char *
2957 dw2_get_real_path (struct objfile *objfile,
2958 struct quick_file_names *qfn, int index)
2959 {
2960 if (qfn->real_names == NULL)
2961 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2962 qfn->num_file_names, sizeof (char *));
2963
2964 if (qfn->real_names[index] == NULL)
2965 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2966
2967 return qfn->real_names[index];
2968 }
2969
2970 static struct symtab *
2971 dw2_find_last_source_symtab (struct objfile *objfile)
2972 {
2973 int index;
2974
2975 dw2_setup (objfile);
2976 index = dwarf2_per_objfile->n_comp_units - 1;
2977 return dw2_instantiate_symtab (dw2_get_cu (index));
2978 }
2979
2980 /* Traversal function for dw2_forget_cached_source_info. */
2981
2982 static int
2983 dw2_free_cached_file_names (void **slot, void *info)
2984 {
2985 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2986
2987 if (file_data->real_names)
2988 {
2989 int i;
2990
2991 for (i = 0; i < file_data->num_file_names; ++i)
2992 {
2993 xfree ((void*) file_data->real_names[i]);
2994 file_data->real_names[i] = NULL;
2995 }
2996 }
2997
2998 return 1;
2999 }
3000
3001 static void
3002 dw2_forget_cached_source_info (struct objfile *objfile)
3003 {
3004 dw2_setup (objfile);
3005
3006 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3007 dw2_free_cached_file_names, NULL);
3008 }
3009
3010 /* Helper function for dw2_map_symtabs_matching_filename that expands
3011 the symtabs and calls the iterator. */
3012
3013 static int
3014 dw2_map_expand_apply (struct objfile *objfile,
3015 struct dwarf2_per_cu_data *per_cu,
3016 const char *name, const char *real_path,
3017 int (*callback) (struct symtab *, void *),
3018 void *data)
3019 {
3020 struct symtab *last_made = objfile->symtabs;
3021
3022 /* Don't visit already-expanded CUs. */
3023 if (per_cu->v.quick->symtab)
3024 return 0;
3025
3026 /* This may expand more than one symtab, and we want to iterate over
3027 all of them. */
3028 dw2_instantiate_symtab (per_cu);
3029
3030 return iterate_over_some_symtabs (name, real_path, callback, data,
3031 objfile->symtabs, last_made);
3032 }
3033
3034 /* Implementation of the map_symtabs_matching_filename method. */
3035
3036 static int
3037 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
3038 const char *real_path,
3039 int (*callback) (struct symtab *, void *),
3040 void *data)
3041 {
3042 int i;
3043 const char *name_basename = lbasename (name);
3044
3045 dw2_setup (objfile);
3046
3047 /* The rule is CUs specify all the files, including those used by
3048 any TU, so there's no need to scan TUs here. */
3049
3050 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3051 {
3052 int j;
3053 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3054 struct quick_file_names *file_data;
3055
3056 /* We only need to look at symtabs not already expanded. */
3057 if (per_cu->v.quick->symtab)
3058 continue;
3059
3060 file_data = dw2_get_file_names (per_cu);
3061 if (file_data == NULL)
3062 continue;
3063
3064 for (j = 0; j < file_data->num_file_names; ++j)
3065 {
3066 const char *this_name = file_data->file_names[j];
3067 const char *this_real_name;
3068
3069 if (compare_filenames_for_search (this_name, name))
3070 {
3071 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3072 callback, data))
3073 return 1;
3074 continue;
3075 }
3076
3077 /* Before we invoke realpath, which can get expensive when many
3078 files are involved, do a quick comparison of the basenames. */
3079 if (! basenames_may_differ
3080 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3081 continue;
3082
3083 this_real_name = dw2_get_real_path (objfile, file_data, j);
3084 if (compare_filenames_for_search (this_real_name, name))
3085 {
3086 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3087 callback, data))
3088 return 1;
3089 continue;
3090 }
3091
3092 if (real_path != NULL)
3093 {
3094 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3095 gdb_assert (IS_ABSOLUTE_PATH (name));
3096 if (this_real_name != NULL
3097 && FILENAME_CMP (real_path, this_real_name) == 0)
3098 {
3099 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3100 callback, data))
3101 return 1;
3102 continue;
3103 }
3104 }
3105 }
3106 }
3107
3108 return 0;
3109 }
3110
3111 /* Struct used to manage iterating over all CUs looking for a symbol. */
3112
3113 struct dw2_symtab_iterator
3114 {
3115 /* The internalized form of .gdb_index. */
3116 struct mapped_index *index;
3117 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3118 int want_specific_block;
3119 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3120 Unused if !WANT_SPECIFIC_BLOCK. */
3121 int block_index;
3122 /* The kind of symbol we're looking for. */
3123 domain_enum domain;
3124 /* The list of CUs from the index entry of the symbol,
3125 or NULL if not found. */
3126 offset_type *vec;
3127 /* The next element in VEC to look at. */
3128 int next;
3129 /* The number of elements in VEC, or zero if there is no match. */
3130 int length;
3131 };
3132
3133 /* Initialize the index symtab iterator ITER.
3134 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3135 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
3136
3137 static void
3138 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3139 struct mapped_index *index,
3140 int want_specific_block,
3141 int block_index,
3142 domain_enum domain,
3143 const char *name)
3144 {
3145 iter->index = index;
3146 iter->want_specific_block = want_specific_block;
3147 iter->block_index = block_index;
3148 iter->domain = domain;
3149 iter->next = 0;
3150
3151 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3152 iter->length = MAYBE_SWAP (*iter->vec);
3153 else
3154 {
3155 iter->vec = NULL;
3156 iter->length = 0;
3157 }
3158 }
3159
3160 /* Return the next matching CU or NULL if there are no more. */
3161
3162 static struct dwarf2_per_cu_data *
3163 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3164 {
3165 for ( ; iter->next < iter->length; ++iter->next)
3166 {
3167 offset_type cu_index_and_attrs =
3168 MAYBE_SWAP (iter->vec[iter->next + 1]);
3169 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3170 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
3171 int want_static = iter->block_index != GLOBAL_BLOCK;
3172 /* This value is only valid for index versions >= 7. */
3173 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3174 gdb_index_symbol_kind symbol_kind =
3175 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3176 /* Only check the symbol attributes if they're present.
3177 Indices prior to version 7 don't record them,
3178 and indices >= 7 may elide them for certain symbols
3179 (gold does this). */
3180 int attrs_valid =
3181 (iter->index->version >= 7
3182 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3183
3184 /* Skip if already read in. */
3185 if (per_cu->v.quick->symtab)
3186 continue;
3187
3188 if (attrs_valid
3189 && iter->want_specific_block
3190 && want_static != is_static)
3191 continue;
3192
3193 /* Only check the symbol's kind if it has one. */
3194 if (attrs_valid)
3195 {
3196 switch (iter->domain)
3197 {
3198 case VAR_DOMAIN:
3199 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3200 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3201 /* Some types are also in VAR_DOMAIN. */
3202 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3203 continue;
3204 break;
3205 case STRUCT_DOMAIN:
3206 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3207 continue;
3208 break;
3209 case LABEL_DOMAIN:
3210 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3211 continue;
3212 break;
3213 default:
3214 break;
3215 }
3216 }
3217
3218 ++iter->next;
3219 return per_cu;
3220 }
3221
3222 return NULL;
3223 }
3224
3225 static struct symtab *
3226 dw2_lookup_symbol (struct objfile *objfile, int block_index,
3227 const char *name, domain_enum domain)
3228 {
3229 struct symtab *stab_best = NULL;
3230 struct mapped_index *index;
3231
3232 dw2_setup (objfile);
3233
3234 index = dwarf2_per_objfile->index_table;
3235
3236 /* index is NULL if OBJF_READNOW. */
3237 if (index)
3238 {
3239 struct dw2_symtab_iterator iter;
3240 struct dwarf2_per_cu_data *per_cu;
3241
3242 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
3243
3244 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3245 {
3246 struct symbol *sym = NULL;
3247 struct symtab *stab = dw2_instantiate_symtab (per_cu);
3248
3249 /* Some caution must be observed with overloaded functions
3250 and methods, since the index will not contain any overload
3251 information (but NAME might contain it). */
3252 if (stab->primary)
3253 {
3254 struct blockvector *bv = BLOCKVECTOR (stab);
3255 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3256
3257 sym = lookup_block_symbol (block, name, domain);
3258 }
3259
3260 if (sym && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3261 {
3262 if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
3263 return stab;
3264
3265 stab_best = stab;
3266 }
3267
3268 /* Keep looking through other CUs. */
3269 }
3270 }
3271
3272 return stab_best;
3273 }
3274
3275 static void
3276 dw2_print_stats (struct objfile *objfile)
3277 {
3278 int i, total, count;
3279
3280 dw2_setup (objfile);
3281 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
3282 count = 0;
3283 for (i = 0; i < total; ++i)
3284 {
3285 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3286
3287 if (!per_cu->v.quick->symtab)
3288 ++count;
3289 }
3290 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3291 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3292 }
3293
3294 static void
3295 dw2_dump (struct objfile *objfile)
3296 {
3297 /* Nothing worth printing. */
3298 }
3299
3300 static void
3301 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
3302 struct section_offsets *delta)
3303 {
3304 /* There's nothing to relocate here. */
3305 }
3306
3307 static void
3308 dw2_expand_symtabs_for_function (struct objfile *objfile,
3309 const char *func_name)
3310 {
3311 struct mapped_index *index;
3312
3313 dw2_setup (objfile);
3314
3315 index = dwarf2_per_objfile->index_table;
3316
3317 /* index is NULL if OBJF_READNOW. */
3318 if (index)
3319 {
3320 struct dw2_symtab_iterator iter;
3321 struct dwarf2_per_cu_data *per_cu;
3322
3323 /* Note: It doesn't matter what we pass for block_index here. */
3324 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3325 func_name);
3326
3327 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3328 dw2_instantiate_symtab (per_cu);
3329 }
3330 }
3331
3332 static void
3333 dw2_expand_all_symtabs (struct objfile *objfile)
3334 {
3335 int i;
3336
3337 dw2_setup (objfile);
3338
3339 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3340 + dwarf2_per_objfile->n_type_units); ++i)
3341 {
3342 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3343
3344 dw2_instantiate_symtab (per_cu);
3345 }
3346 }
3347
3348 static void
3349 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3350 const char *fullname)
3351 {
3352 int i;
3353
3354 dw2_setup (objfile);
3355
3356 /* We don't need to consider type units here.
3357 This is only called for examining code, e.g. expand_line_sal.
3358 There can be an order of magnitude (or more) more type units
3359 than comp units, and we avoid them if we can. */
3360
3361 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3362 {
3363 int j;
3364 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3365 struct quick_file_names *file_data;
3366
3367 /* We only need to look at symtabs not already expanded. */
3368 if (per_cu->v.quick->symtab)
3369 continue;
3370
3371 file_data = dw2_get_file_names (per_cu);
3372 if (file_data == NULL)
3373 continue;
3374
3375 for (j = 0; j < file_data->num_file_names; ++j)
3376 {
3377 const char *this_fullname = file_data->file_names[j];
3378
3379 if (filename_cmp (this_fullname, fullname) == 0)
3380 {
3381 dw2_instantiate_symtab (per_cu);
3382 break;
3383 }
3384 }
3385 }
3386 }
3387
3388 /* A helper function for dw2_find_symbol_file that finds the primary
3389 file name for a given CU. This is a die_reader_func. */
3390
3391 static void
3392 dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
3393 gdb_byte *info_ptr,
3394 struct die_info *comp_unit_die,
3395 int has_children,
3396 void *data)
3397 {
3398 const char **result_ptr = data;
3399 struct dwarf2_cu *cu = reader->cu;
3400 struct attribute *attr;
3401
3402 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3403 if (attr == NULL)
3404 *result_ptr = NULL;
3405 else
3406 *result_ptr = DW_STRING (attr);
3407 }
3408
3409 static const char *
3410 dw2_find_symbol_file (struct objfile *objfile, const char *name)
3411 {
3412 struct dwarf2_per_cu_data *per_cu;
3413 offset_type *vec;
3414 const char *filename;
3415
3416 dw2_setup (objfile);
3417
3418 /* index_table is NULL if OBJF_READNOW. */
3419 if (!dwarf2_per_objfile->index_table)
3420 {
3421 struct symtab *s;
3422
3423 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
3424 {
3425 struct blockvector *bv = BLOCKVECTOR (s);
3426 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
3427 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
3428
3429 if (sym)
3430 {
3431 /* Only file extension of returned filename is recognized. */
3432 return SYMBOL_SYMTAB (sym)->filename;
3433 }
3434 }
3435 return NULL;
3436 }
3437
3438 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
3439 name, &vec))
3440 return NULL;
3441
3442 /* Note that this just looks at the very first one named NAME -- but
3443 actually we are looking for a function. find_main_filename
3444 should be rewritten so that it doesn't require a custom hook. It
3445 could just use the ordinary symbol tables. */
3446 /* vec[0] is the length, which must always be >0. */
3447 per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
3448
3449 if (per_cu->v.quick->symtab != NULL)
3450 {
3451 /* Only file extension of returned filename is recognized. */
3452 return per_cu->v.quick->symtab->filename;
3453 }
3454
3455 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
3456 dw2_get_primary_filename_reader, &filename);
3457
3458 /* Only file extension of returned filename is recognized. */
3459 return filename;
3460 }
3461
3462 static void
3463 dw2_map_matching_symbols (const char * name, domain_enum namespace,
3464 struct objfile *objfile, int global,
3465 int (*callback) (struct block *,
3466 struct symbol *, void *),
3467 void *data, symbol_compare_ftype *match,
3468 symbol_compare_ftype *ordered_compare)
3469 {
3470 /* Currently unimplemented; used for Ada. The function can be called if the
3471 current language is Ada for a non-Ada objfile using GNU index. As Ada
3472 does not look for non-Ada symbols this function should just return. */
3473 }
3474
3475 static void
3476 dw2_expand_symtabs_matching
3477 (struct objfile *objfile,
3478 int (*file_matcher) (const char *, void *, int basenames),
3479 int (*name_matcher) (const char *, void *),
3480 enum search_domain kind,
3481 void *data)
3482 {
3483 int i;
3484 offset_type iter;
3485 struct mapped_index *index;
3486
3487 dw2_setup (objfile);
3488
3489 /* index_table is NULL if OBJF_READNOW. */
3490 if (!dwarf2_per_objfile->index_table)
3491 return;
3492 index = dwarf2_per_objfile->index_table;
3493
3494 if (file_matcher != NULL)
3495 {
3496 struct cleanup *cleanup;
3497 htab_t visited_found, visited_not_found;
3498
3499 visited_found = htab_create_alloc (10,
3500 htab_hash_pointer, htab_eq_pointer,
3501 NULL, xcalloc, xfree);
3502 cleanup = make_cleanup_htab_delete (visited_found);
3503 visited_not_found = htab_create_alloc (10,
3504 htab_hash_pointer, htab_eq_pointer,
3505 NULL, xcalloc, xfree);
3506 make_cleanup_htab_delete (visited_not_found);
3507
3508 /* The rule is CUs specify all the files, including those used by
3509 any TU, so there's no need to scan TUs here. */
3510
3511 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3512 {
3513 int j;
3514 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3515 struct quick_file_names *file_data;
3516 void **slot;
3517
3518 per_cu->v.quick->mark = 0;
3519
3520 /* We only need to look at symtabs not already expanded. */
3521 if (per_cu->v.quick->symtab)
3522 continue;
3523
3524 file_data = dw2_get_file_names (per_cu);
3525 if (file_data == NULL)
3526 continue;
3527
3528 if (htab_find (visited_not_found, file_data) != NULL)
3529 continue;
3530 else if (htab_find (visited_found, file_data) != NULL)
3531 {
3532 per_cu->v.quick->mark = 1;
3533 continue;
3534 }
3535
3536 for (j = 0; j < file_data->num_file_names; ++j)
3537 {
3538 const char *this_real_name;
3539
3540 if (file_matcher (file_data->file_names[j], data, 0))
3541 {
3542 per_cu->v.quick->mark = 1;
3543 break;
3544 }
3545
3546 /* Before we invoke realpath, which can get expensive when many
3547 files are involved, do a quick comparison of the basenames. */
3548 if (!basenames_may_differ
3549 && !file_matcher (lbasename (file_data->file_names[j]),
3550 data, 1))
3551 continue;
3552
3553 this_real_name = dw2_get_real_path (objfile, file_data, j);
3554 if (file_matcher (this_real_name, data, 0))
3555 {
3556 per_cu->v.quick->mark = 1;
3557 break;
3558 }
3559 }
3560
3561 slot = htab_find_slot (per_cu->v.quick->mark
3562 ? visited_found
3563 : visited_not_found,
3564 file_data, INSERT);
3565 *slot = file_data;
3566 }
3567
3568 do_cleanups (cleanup);
3569 }
3570
3571 for (iter = 0; iter < index->symbol_table_slots; ++iter)
3572 {
3573 offset_type idx = 2 * iter;
3574 const char *name;
3575 offset_type *vec, vec_len, vec_idx;
3576
3577 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
3578 continue;
3579
3580 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
3581
3582 if (! (*name_matcher) (name, data))
3583 continue;
3584
3585 /* The name was matched, now expand corresponding CUs that were
3586 marked. */
3587 vec = (offset_type *) (index->constant_pool
3588 + MAYBE_SWAP (index->symbol_table[idx + 1]));
3589 vec_len = MAYBE_SWAP (vec[0]);
3590 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3591 {
3592 struct dwarf2_per_cu_data *per_cu;
3593 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3594 gdb_index_symbol_kind symbol_kind =
3595 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3596 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3597
3598 /* Don't crash on bad data. */
3599 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3600 + dwarf2_per_objfile->n_type_units))
3601 continue;
3602
3603 /* Only check the symbol's kind if it has one.
3604 Indices prior to version 7 don't record it. */
3605 if (index->version >= 7)
3606 {
3607 switch (kind)
3608 {
3609 case VARIABLES_DOMAIN:
3610 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3611 continue;
3612 break;
3613 case FUNCTIONS_DOMAIN:
3614 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3615 continue;
3616 break;
3617 case TYPES_DOMAIN:
3618 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3619 continue;
3620 break;
3621 default:
3622 break;
3623 }
3624 }
3625
3626 per_cu = dw2_get_cu (cu_index);
3627 if (file_matcher == NULL || per_cu->v.quick->mark)
3628 dw2_instantiate_symtab (per_cu);
3629 }
3630 }
3631 }
3632
3633 /* A helper for dw2_find_pc_sect_symtab which finds the most specific
3634 symtab. */
3635
3636 static struct symtab *
3637 recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3638 {
3639 int i;
3640
3641 if (BLOCKVECTOR (symtab) != NULL
3642 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3643 return symtab;
3644
3645 if (symtab->includes == NULL)
3646 return NULL;
3647
3648 for (i = 0; symtab->includes[i]; ++i)
3649 {
3650 struct symtab *s = symtab->includes[i];
3651
3652 s = recursively_find_pc_sect_symtab (s, pc);
3653 if (s != NULL)
3654 return s;
3655 }
3656
3657 return NULL;
3658 }
3659
3660 static struct symtab *
3661 dw2_find_pc_sect_symtab (struct objfile *objfile,
3662 struct minimal_symbol *msymbol,
3663 CORE_ADDR pc,
3664 struct obj_section *section,
3665 int warn_if_readin)
3666 {
3667 struct dwarf2_per_cu_data *data;
3668 struct symtab *result;
3669
3670 dw2_setup (objfile);
3671
3672 if (!objfile->psymtabs_addrmap)
3673 return NULL;
3674
3675 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3676 if (!data)
3677 return NULL;
3678
3679 if (warn_if_readin && data->v.quick->symtab)
3680 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
3681 paddress (get_objfile_arch (objfile), pc));
3682
3683 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3684 gdb_assert (result != NULL);
3685 return result;
3686 }
3687
3688 static void
3689 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
3690 void *data, int need_fullname)
3691 {
3692 int i;
3693 struct cleanup *cleanup;
3694 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3695 NULL, xcalloc, xfree);
3696
3697 cleanup = make_cleanup_htab_delete (visited);
3698 dw2_setup (objfile);
3699
3700 /* The rule is CUs specify all the files, including those used by
3701 any TU, so there's no need to scan TUs here.
3702 We can ignore file names coming from already-expanded CUs. */
3703
3704 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3705 {
3706 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3707
3708 if (per_cu->v.quick->symtab)
3709 {
3710 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3711 INSERT);
3712
3713 *slot = per_cu->v.quick->file_names;
3714 }
3715 }
3716
3717 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3718 {
3719 int j;
3720 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3721 struct quick_file_names *file_data;
3722 void **slot;
3723
3724 /* We only need to look at symtabs not already expanded. */
3725 if (per_cu->v.quick->symtab)
3726 continue;
3727
3728 file_data = dw2_get_file_names (per_cu);
3729 if (file_data == NULL)
3730 continue;
3731
3732 slot = htab_find_slot (visited, file_data, INSERT);
3733 if (*slot)
3734 {
3735 /* Already visited. */
3736 continue;
3737 }
3738 *slot = file_data;
3739
3740 for (j = 0; j < file_data->num_file_names; ++j)
3741 {
3742 const char *this_real_name;
3743
3744 if (need_fullname)
3745 this_real_name = dw2_get_real_path (objfile, file_data, j);
3746 else
3747 this_real_name = NULL;
3748 (*fun) (file_data->file_names[j], this_real_name, data);
3749 }
3750 }
3751
3752 do_cleanups (cleanup);
3753 }
3754
3755 static int
3756 dw2_has_symbols (struct objfile *objfile)
3757 {
3758 return 1;
3759 }
3760
3761 const struct quick_symbol_functions dwarf2_gdb_index_functions =
3762 {
3763 dw2_has_symbols,
3764 dw2_find_last_source_symtab,
3765 dw2_forget_cached_source_info,
3766 dw2_map_symtabs_matching_filename,
3767 dw2_lookup_symbol,
3768 dw2_print_stats,
3769 dw2_dump,
3770 dw2_relocate,
3771 dw2_expand_symtabs_for_function,
3772 dw2_expand_all_symtabs,
3773 dw2_expand_symtabs_with_fullname,
3774 dw2_find_symbol_file,
3775 dw2_map_matching_symbols,
3776 dw2_expand_symtabs_matching,
3777 dw2_find_pc_sect_symtab,
3778 dw2_map_symbol_filenames
3779 };
3780
3781 /* Initialize for reading DWARF for this objfile. Return 0 if this
3782 file will use psymtabs, or 1 if using the GNU index. */
3783
3784 int
3785 dwarf2_initialize_objfile (struct objfile *objfile)
3786 {
3787 /* If we're about to read full symbols, don't bother with the
3788 indices. In this case we also don't care if some other debug
3789 format is making psymtabs, because they are all about to be
3790 expanded anyway. */
3791 if ((objfile->flags & OBJF_READNOW))
3792 {
3793 int i;
3794
3795 dwarf2_per_objfile->using_index = 1;
3796 create_all_comp_units (objfile);
3797 create_all_type_units (objfile);
3798 dwarf2_per_objfile->quick_file_names_table =
3799 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3800
3801 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3802 + dwarf2_per_objfile->n_type_units); ++i)
3803 {
3804 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3805
3806 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3807 struct dwarf2_per_cu_quick_data);
3808 }
3809
3810 /* Return 1 so that gdb sees the "quick" functions. However,
3811 these functions will be no-ops because we will have expanded
3812 all symtabs. */
3813 return 1;
3814 }
3815
3816 if (dwarf2_read_index (objfile))
3817 return 1;
3818
3819 return 0;
3820 }
3821
3822 \f
3823
3824 /* Build a partial symbol table. */
3825
3826 void
3827 dwarf2_build_psymtabs (struct objfile *objfile)
3828 {
3829 volatile struct gdb_exception except;
3830
3831 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
3832 {
3833 init_psymbol_list (objfile, 1024);
3834 }
3835
3836 TRY_CATCH (except, RETURN_MASK_ERROR)
3837 {
3838 /* This isn't really ideal: all the data we allocate on the
3839 objfile's obstack is still uselessly kept around. However,
3840 freeing it seems unsafe. */
3841 struct cleanup *cleanups = make_cleanup_discard_psymtabs (objfile);
3842
3843 dwarf2_build_psymtabs_hard (objfile);
3844 discard_cleanups (cleanups);
3845 }
3846 if (except.reason < 0)
3847 exception_print (gdb_stderr, except);
3848 }
3849
3850 /* Return the total length of the CU described by HEADER. */
3851
3852 static unsigned int
3853 get_cu_length (const struct comp_unit_head *header)
3854 {
3855 return header->initial_length_size + header->length;
3856 }
3857
3858 /* Return TRUE if OFFSET is within CU_HEADER. */
3859
3860 static inline int
3861 offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
3862 {
3863 sect_offset bottom = { cu_header->offset.sect_off };
3864 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
3865
3866 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
3867 }
3868
3869 /* Find the base address of the compilation unit for range lists and
3870 location lists. It will normally be specified by DW_AT_low_pc.
3871 In DWARF-3 draft 4, the base address could be overridden by
3872 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3873 compilation units with discontinuous ranges. */
3874
3875 static void
3876 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3877 {
3878 struct attribute *attr;
3879
3880 cu->base_known = 0;
3881 cu->base_address = 0;
3882
3883 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3884 if (attr)
3885 {
3886 cu->base_address = DW_ADDR (attr);
3887 cu->base_known = 1;
3888 }
3889 else
3890 {
3891 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3892 if (attr)
3893 {
3894 cu->base_address = DW_ADDR (attr);
3895 cu->base_known = 1;
3896 }
3897 }
3898 }
3899
3900 /* Read in the comp unit header information from the debug_info at info_ptr.
3901 NOTE: This leaves members offset, first_die_offset to be filled in
3902 by the caller. */
3903
3904 static gdb_byte *
3905 read_comp_unit_head (struct comp_unit_head *cu_header,
3906 gdb_byte *info_ptr, bfd *abfd)
3907 {
3908 int signed_addr;
3909 unsigned int bytes_read;
3910
3911 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3912 cu_header->initial_length_size = bytes_read;
3913 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
3914 info_ptr += bytes_read;
3915 cu_header->version = read_2_bytes (abfd, info_ptr);
3916 info_ptr += 2;
3917 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3918 &bytes_read);
3919 info_ptr += bytes_read;
3920 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3921 info_ptr += 1;
3922 signed_addr = bfd_get_sign_extend_vma (abfd);
3923 if (signed_addr < 0)
3924 internal_error (__FILE__, __LINE__,
3925 _("read_comp_unit_head: dwarf from non elf file"));
3926 cu_header->signed_addr_p = signed_addr;
3927
3928 return info_ptr;
3929 }
3930
3931 /* Helper function that returns the proper abbrev section for
3932 THIS_CU. */
3933
3934 static struct dwarf2_section_info *
3935 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3936 {
3937 struct dwarf2_section_info *abbrev;
3938
3939 if (this_cu->is_dwz)
3940 abbrev = &dwarf2_get_dwz_file ()->abbrev;
3941 else
3942 abbrev = &dwarf2_per_objfile->abbrev;
3943
3944 return abbrev;
3945 }
3946
3947 /* Subroutine of read_and_check_comp_unit_head and
3948 read_and_check_type_unit_head to simplify them.
3949 Perform various error checking on the header. */
3950
3951 static void
3952 error_check_comp_unit_head (struct comp_unit_head *header,
3953 struct dwarf2_section_info *section,
3954 struct dwarf2_section_info *abbrev_section)
3955 {
3956 bfd *abfd = section->asection->owner;
3957 const char *filename = bfd_get_filename (abfd);
3958
3959 if (header->version != 2 && header->version != 3 && header->version != 4)
3960 error (_("Dwarf Error: wrong version in compilation unit header "
3961 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3962 filename);
3963
3964 if (header->abbrev_offset.sect_off
3965 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
3966 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3967 "(offset 0x%lx + 6) [in module %s]"),
3968 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
3969 filename);
3970
3971 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3972 avoid potential 32-bit overflow. */
3973 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
3974 > section->size)
3975 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3976 "(offset 0x%lx + 0) [in module %s]"),
3977 (long) header->length, (long) header->offset.sect_off,
3978 filename);
3979 }
3980
3981 /* Read in a CU/TU header and perform some basic error checking.
3982 The contents of the header are stored in HEADER.
3983 The result is a pointer to the start of the first DIE. */
3984
3985 static gdb_byte *
3986 read_and_check_comp_unit_head (struct comp_unit_head *header,
3987 struct dwarf2_section_info *section,
3988 struct dwarf2_section_info *abbrev_section,
3989 gdb_byte *info_ptr,
3990 int is_debug_types_section)
3991 {
3992 gdb_byte *beg_of_comp_unit = info_ptr;
3993 bfd *abfd = section->asection->owner;
3994
3995 header->offset.sect_off = beg_of_comp_unit - section->buffer;
3996
3997 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3998
3999 /* If we're reading a type unit, skip over the signature and
4000 type_offset fields. */
4001 if (is_debug_types_section)
4002 info_ptr += 8 /*signature*/ + header->offset_size;
4003
4004 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
4005
4006 error_check_comp_unit_head (header, section, abbrev_section);
4007
4008 return info_ptr;
4009 }
4010
4011 /* Read in the types comp unit header information from .debug_types entry at
4012 types_ptr. The result is a pointer to one past the end of the header. */
4013
4014 static gdb_byte *
4015 read_and_check_type_unit_head (struct comp_unit_head *header,
4016 struct dwarf2_section_info *section,
4017 struct dwarf2_section_info *abbrev_section,
4018 gdb_byte *info_ptr,
4019 ULONGEST *signature,
4020 cu_offset *type_offset_in_tu)
4021 {
4022 gdb_byte *beg_of_comp_unit = info_ptr;
4023 bfd *abfd = section->asection->owner;
4024
4025 header->offset.sect_off = beg_of_comp_unit - section->buffer;
4026
4027 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4028
4029 /* If we're reading a type unit, skip over the signature and
4030 type_offset fields. */
4031 if (signature != NULL)
4032 *signature = read_8_bytes (abfd, info_ptr);
4033 info_ptr += 8;
4034 if (type_offset_in_tu != NULL)
4035 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
4036 header->offset_size);
4037 info_ptr += header->offset_size;
4038
4039 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
4040
4041 error_check_comp_unit_head (header, section, abbrev_section);
4042
4043 return info_ptr;
4044 }
4045
4046 /* Fetch the abbreviation table offset from a comp or type unit header. */
4047
4048 static sect_offset
4049 read_abbrev_offset (struct dwarf2_section_info *section,
4050 sect_offset offset)
4051 {
4052 bfd *abfd = section->asection->owner;
4053 gdb_byte *info_ptr;
4054 unsigned int length, initial_length_size, offset_size;
4055 sect_offset abbrev_offset;
4056
4057 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4058 info_ptr = section->buffer + offset.sect_off;
4059 length = read_initial_length (abfd, info_ptr, &initial_length_size);
4060 offset_size = initial_length_size == 4 ? 4 : 8;
4061 info_ptr += initial_length_size + 2 /*version*/;
4062 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4063 return abbrev_offset;
4064 }
4065
4066 /* Allocate a new partial symtab for file named NAME and mark this new
4067 partial symtab as being an include of PST. */
4068
4069 static void
4070 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
4071 struct objfile *objfile)
4072 {
4073 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4074
4075 if (!IS_ABSOLUTE_PATH (subpst->filename))
4076 {
4077 /* It shares objfile->objfile_obstack. */
4078 subpst->dirname = pst->dirname;
4079 }
4080
4081 subpst->section_offsets = pst->section_offsets;
4082 subpst->textlow = 0;
4083 subpst->texthigh = 0;
4084
4085 subpst->dependencies = (struct partial_symtab **)
4086 obstack_alloc (&objfile->objfile_obstack,
4087 sizeof (struct partial_symtab *));
4088 subpst->dependencies[0] = pst;
4089 subpst->number_of_dependencies = 1;
4090
4091 subpst->globals_offset = 0;
4092 subpst->n_global_syms = 0;
4093 subpst->statics_offset = 0;
4094 subpst->n_static_syms = 0;
4095 subpst->symtab = NULL;
4096 subpst->read_symtab = pst->read_symtab;
4097 subpst->readin = 0;
4098
4099 /* No private part is necessary for include psymtabs. This property
4100 can be used to differentiate between such include psymtabs and
4101 the regular ones. */
4102 subpst->read_symtab_private = NULL;
4103 }
4104
4105 /* Read the Line Number Program data and extract the list of files
4106 included by the source file represented by PST. Build an include
4107 partial symtab for each of these included files. */
4108
4109 static void
4110 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
4111 struct die_info *die,
4112 struct partial_symtab *pst)
4113 {
4114 struct line_header *lh = NULL;
4115 struct attribute *attr;
4116
4117 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4118 if (attr)
4119 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
4120 if (lh == NULL)
4121 return; /* No linetable, so no includes. */
4122
4123 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
4124 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
4125
4126 free_line_header (lh);
4127 }
4128
4129 static hashval_t
4130 hash_signatured_type (const void *item)
4131 {
4132 const struct signatured_type *sig_type = item;
4133
4134 /* This drops the top 32 bits of the signature, but is ok for a hash. */
4135 return sig_type->signature;
4136 }
4137
4138 static int
4139 eq_signatured_type (const void *item_lhs, const void *item_rhs)
4140 {
4141 const struct signatured_type *lhs = item_lhs;
4142 const struct signatured_type *rhs = item_rhs;
4143
4144 return lhs->signature == rhs->signature;
4145 }
4146
4147 /* Allocate a hash table for signatured types. */
4148
4149 static htab_t
4150 allocate_signatured_type_table (struct objfile *objfile)
4151 {
4152 return htab_create_alloc_ex (41,
4153 hash_signatured_type,
4154 eq_signatured_type,
4155 NULL,
4156 &objfile->objfile_obstack,
4157 hashtab_obstack_allocate,
4158 dummy_obstack_deallocate);
4159 }
4160
4161 /* A helper function to add a signatured type CU to a table. */
4162
4163 static int
4164 add_signatured_type_cu_to_table (void **slot, void *datum)
4165 {
4166 struct signatured_type *sigt = *slot;
4167 struct signatured_type ***datap = datum;
4168
4169 **datap = sigt;
4170 ++*datap;
4171
4172 return 1;
4173 }
4174
4175 /* Create the hash table of all entries in the .debug_types
4176 (or .debug_types.dwo) section(s).
4177 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
4178 otherwise it is NULL.
4179
4180 The result is a pointer to the hash table or NULL if there are no types.
4181
4182 Note: This function processes DWO files only, not DWP files. */
4183
4184 static htab_t
4185 create_debug_types_hash_table (struct dwo_file *dwo_file,
4186 VEC (dwarf2_section_info_def) *types)
4187 {
4188 struct objfile *objfile = dwarf2_per_objfile->objfile;
4189 htab_t types_htab = NULL;
4190 int ix;
4191 struct dwarf2_section_info *section;
4192 struct dwarf2_section_info *abbrev_section;
4193
4194 if (VEC_empty (dwarf2_section_info_def, types))
4195 return NULL;
4196
4197 abbrev_section = (dwo_file != NULL
4198 ? &dwo_file->sections.abbrev
4199 : &dwarf2_per_objfile->abbrev);
4200
4201 if (dwarf2_read_debug)
4202 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4203 dwo_file ? ".dwo" : "",
4204 bfd_get_filename (abbrev_section->asection->owner));
4205
4206 for (ix = 0;
4207 VEC_iterate (dwarf2_section_info_def, types, ix, section);
4208 ++ix)
4209 {
4210 bfd *abfd;
4211 gdb_byte *info_ptr, *end_ptr;
4212 struct dwarf2_section_info *abbrev_section;
4213
4214 dwarf2_read_section (objfile, section);
4215 info_ptr = section->buffer;
4216
4217 if (info_ptr == NULL)
4218 continue;
4219
4220 /* We can't set abfd until now because the section may be empty or
4221 not present, in which case section->asection will be NULL. */
4222 abfd = section->asection->owner;
4223
4224 if (dwo_file)
4225 abbrev_section = &dwo_file->sections.abbrev;
4226 else
4227 abbrev_section = &dwarf2_per_objfile->abbrev;
4228
4229 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4230 because we don't need to read any dies: the signature is in the
4231 header. */
4232
4233 end_ptr = info_ptr + section->size;
4234 while (info_ptr < end_ptr)
4235 {
4236 sect_offset offset;
4237 cu_offset type_offset_in_tu;
4238 ULONGEST signature;
4239 struct signatured_type *sig_type;
4240 struct dwo_unit *dwo_tu;
4241 void **slot;
4242 gdb_byte *ptr = info_ptr;
4243 struct comp_unit_head header;
4244 unsigned int length;
4245
4246 offset.sect_off = ptr - section->buffer;
4247
4248 /* We need to read the type's signature in order to build the hash
4249 table, but we don't need anything else just yet. */
4250
4251 ptr = read_and_check_type_unit_head (&header, section,
4252 abbrev_section, ptr,
4253 &signature, &type_offset_in_tu);
4254
4255 length = get_cu_length (&header);
4256
4257 /* Skip dummy type units. */
4258 if (ptr >= info_ptr + length
4259 || peek_abbrev_code (abfd, ptr) == 0)
4260 {
4261 info_ptr += length;
4262 continue;
4263 }
4264
4265 if (types_htab == NULL)
4266 {
4267 if (dwo_file)
4268 types_htab = allocate_dwo_unit_table (objfile);
4269 else
4270 types_htab = allocate_signatured_type_table (objfile);
4271 }
4272
4273 if (dwo_file)
4274 {
4275 sig_type = NULL;
4276 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4277 struct dwo_unit);
4278 dwo_tu->dwo_file = dwo_file;
4279 dwo_tu->signature = signature;
4280 dwo_tu->type_offset_in_tu = type_offset_in_tu;
4281 dwo_tu->section = section;
4282 dwo_tu->offset = offset;
4283 dwo_tu->length = length;
4284 }
4285 else
4286 {
4287 /* N.B.: type_offset is not usable if this type uses a DWO file.
4288 The real type_offset is in the DWO file. */
4289 dwo_tu = NULL;
4290 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4291 struct signatured_type);
4292 sig_type->signature = signature;
4293 sig_type->type_offset_in_tu = type_offset_in_tu;
4294 sig_type->per_cu.objfile = objfile;
4295 sig_type->per_cu.is_debug_types = 1;
4296 sig_type->per_cu.section = section;
4297 sig_type->per_cu.offset = offset;
4298 sig_type->per_cu.length = length;
4299 }
4300
4301 slot = htab_find_slot (types_htab,
4302 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4303 INSERT);
4304 gdb_assert (slot != NULL);
4305 if (*slot != NULL)
4306 {
4307 sect_offset dup_offset;
4308
4309 if (dwo_file)
4310 {
4311 const struct dwo_unit *dup_tu = *slot;
4312
4313 dup_offset = dup_tu->offset;
4314 }
4315 else
4316 {
4317 const struct signatured_type *dup_tu = *slot;
4318
4319 dup_offset = dup_tu->per_cu.offset;
4320 }
4321
4322 complaint (&symfile_complaints,
4323 _("debug type entry at offset 0x%x is duplicate to"
4324 " the entry at offset 0x%x, signature 0x%s"),
4325 offset.sect_off, dup_offset.sect_off,
4326 phex (signature, sizeof (signature)));
4327 }
4328 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
4329
4330 if (dwarf2_read_debug)
4331 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
4332 offset.sect_off,
4333 phex (signature, sizeof (signature)));
4334
4335 info_ptr += length;
4336 }
4337 }
4338
4339 return types_htab;
4340 }
4341
4342 /* Create the hash table of all entries in the .debug_types section,
4343 and initialize all_type_units.
4344 The result is zero if there is an error (e.g. missing .debug_types section),
4345 otherwise non-zero. */
4346
4347 static int
4348 create_all_type_units (struct objfile *objfile)
4349 {
4350 htab_t types_htab;
4351 struct signatured_type **iter;
4352
4353 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4354 if (types_htab == NULL)
4355 {
4356 dwarf2_per_objfile->signatured_types = NULL;
4357 return 0;
4358 }
4359
4360 dwarf2_per_objfile->signatured_types = types_htab;
4361
4362 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4363 dwarf2_per_objfile->all_type_units
4364 = obstack_alloc (&objfile->objfile_obstack,
4365 dwarf2_per_objfile->n_type_units
4366 * sizeof (struct signatured_type *));
4367 iter = &dwarf2_per_objfile->all_type_units[0];
4368 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4369 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4370 == dwarf2_per_objfile->n_type_units);
4371
4372 return 1;
4373 }
4374
4375 /* Lookup a signature based type for DW_FORM_ref_sig8.
4376 Returns NULL if signature SIG is not present in the table.
4377 It is up to the caller to complain about this. */
4378
4379 static struct signatured_type *
4380 lookup_signatured_type (ULONGEST sig)
4381 {
4382 struct signatured_type find_entry, *entry;
4383
4384 if (dwarf2_per_objfile->signatured_types == NULL)
4385 return NULL;
4386 find_entry.signature = sig;
4387 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4388 return entry;
4389 }
4390 \f
4391 /* Low level DIE reading support. */
4392
4393 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4394
4395 static void
4396 init_cu_die_reader (struct die_reader_specs *reader,
4397 struct dwarf2_cu *cu,
4398 struct dwarf2_section_info *section,
4399 struct dwo_file *dwo_file)
4400 {
4401 gdb_assert (section->readin && section->buffer != NULL);
4402 reader->abfd = section->asection->owner;
4403 reader->cu = cu;
4404 reader->dwo_file = dwo_file;
4405 reader->die_section = section;
4406 reader->buffer = section->buffer;
4407 reader->buffer_end = section->buffer + section->size;
4408 }
4409
4410 /* Subroutine of init_cutu_and_read_dies to simplify it.
4411 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
4412 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
4413 already.
4414
4415 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
4416 from it to the DIE in the DWO. If NULL we are skipping the stub.
4417 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
4418 are filled in with the info of the DIE from the DWO file.
4419 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
4420 provided an abbrev table to use.
4421 The result is non-zero if a valid (non-dummy) DIE was found. */
4422
4423 static int
4424 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
4425 struct dwo_unit *dwo_unit,
4426 int abbrev_table_provided,
4427 struct die_info *stub_comp_unit_die,
4428 struct die_reader_specs *result_reader,
4429 gdb_byte **result_info_ptr,
4430 struct die_info **result_comp_unit_die,
4431 int *result_has_children)
4432 {
4433 struct objfile *objfile = dwarf2_per_objfile->objfile;
4434 struct dwarf2_cu *cu = this_cu->cu;
4435 struct dwarf2_section_info *section;
4436 bfd *abfd;
4437 gdb_byte *begin_info_ptr, *info_ptr;
4438 const char *comp_dir_string;
4439 ULONGEST signature; /* Or dwo_id. */
4440 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
4441 int i,num_extra_attrs;
4442 struct dwarf2_section_info *dwo_abbrev_section;
4443 struct attribute *attr;
4444 struct die_info *comp_unit_die;
4445
4446 /* These attributes aren't processed until later:
4447 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4448 However, the attribute is found in the stub which we won't have later.
4449 In order to not impose this complication on the rest of the code,
4450 we read them here and copy them to the DWO CU/TU die. */
4451
4452 stmt_list = NULL;
4453 low_pc = NULL;
4454 high_pc = NULL;
4455 ranges = NULL;
4456 comp_dir = NULL;
4457
4458 if (stub_comp_unit_die != NULL)
4459 {
4460 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4461 DWO file. */
4462 if (! this_cu->is_debug_types)
4463 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
4464 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
4465 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
4466 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
4467 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
4468
4469 /* There should be a DW_AT_addr_base attribute here (if needed).
4470 We need the value before we can process DW_FORM_GNU_addr_index. */
4471 cu->addr_base = 0;
4472 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
4473 if (attr)
4474 cu->addr_base = DW_UNSND (attr);
4475
4476 /* There should be a DW_AT_ranges_base attribute here (if needed).
4477 We need the value before we can process DW_AT_ranges. */
4478 cu->ranges_base = 0;
4479 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
4480 if (attr)
4481 cu->ranges_base = DW_UNSND (attr);
4482 }
4483
4484 /* Set up for reading the DWO CU/TU. */
4485 cu->dwo_unit = dwo_unit;
4486 section = dwo_unit->section;
4487 dwarf2_read_section (objfile, section);
4488 abfd = section->asection->owner;
4489 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4490 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
4491 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
4492
4493 if (this_cu->is_debug_types)
4494 {
4495 ULONGEST header_signature;
4496 cu_offset type_offset_in_tu;
4497 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
4498
4499 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4500 dwo_abbrev_section,
4501 info_ptr,
4502 &header_signature,
4503 &type_offset_in_tu);
4504 gdb_assert (sig_type->signature == header_signature);
4505 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4506 /* For DWOs coming from DWP files, we don't know the CU length
4507 nor the type's offset in the TU until now. */
4508 dwo_unit->length = get_cu_length (&cu->header);
4509 dwo_unit->type_offset_in_tu = type_offset_in_tu;
4510
4511 /* Establish the type offset that can be used to lookup the type.
4512 For DWO files, we don't know it until now. */
4513 sig_type->type_offset_in_section.sect_off =
4514 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4515 }
4516 else
4517 {
4518 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4519 dwo_abbrev_section,
4520 info_ptr, 0);
4521 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4522 /* For DWOs coming from DWP files, we don't know the CU length
4523 until now. */
4524 dwo_unit->length = get_cu_length (&cu->header);
4525 }
4526
4527 /* Replace the CU's original abbrev table with the DWO's. */
4528 if (abbrev_table_provided)
4529 {
4530 /* Don't free the provided abbrev table, the caller of
4531 init_cutu_and_read_dies owns it. */
4532 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4533 make_cleanup (dwarf2_free_abbrev_table, cu);
4534 }
4535 else
4536 {
4537 dwarf2_free_abbrev_table (cu);
4538 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4539 }
4540
4541 /* Read in the die, but leave space to copy over the attributes
4542 from the stub. This has the benefit of simplifying the rest of
4543 the code - all the work to maintain the illusion of a single
4544 DW_TAG_{compile,type}_unit DIE is done here. */
4545 num_extra_attrs = ((stmt_list != NULL)
4546 + (low_pc != NULL)
4547 + (high_pc != NULL)
4548 + (ranges != NULL)
4549 + (comp_dir != NULL));
4550 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
4551 result_has_children, num_extra_attrs);
4552
4553 /* Copy over the attributes from the stub to the DIE we just read in. */
4554 comp_unit_die = *result_comp_unit_die;
4555 i = comp_unit_die->num_attrs;
4556 if (stmt_list != NULL)
4557 comp_unit_die->attrs[i++] = *stmt_list;
4558 if (low_pc != NULL)
4559 comp_unit_die->attrs[i++] = *low_pc;
4560 if (high_pc != NULL)
4561 comp_unit_die->attrs[i++] = *high_pc;
4562 if (ranges != NULL)
4563 comp_unit_die->attrs[i++] = *ranges;
4564 if (comp_dir != NULL)
4565 comp_unit_die->attrs[i++] = *comp_dir;
4566 comp_unit_die->num_attrs += num_extra_attrs;
4567
4568 /* Skip dummy compilation units. */
4569 if (info_ptr >= begin_info_ptr + dwo_unit->length
4570 || peek_abbrev_code (abfd, info_ptr) == 0)
4571 return 0;
4572
4573 *result_info_ptr = info_ptr;
4574 return 1;
4575 }
4576
4577 /* Subroutine of init_cutu_and_read_dies to simplify it.
4578 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
4579 If the specified DWO unit cannot be found an error is thrown. */
4580
4581 static struct dwo_unit *
4582 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
4583 struct die_info *comp_unit_die)
4584 {
4585 struct dwarf2_cu *cu = this_cu->cu;
4586 struct attribute *attr;
4587 ULONGEST signature;
4588 struct dwo_unit *dwo_unit;
4589 const char *comp_dir, *dwo_name;
4590
4591 /* Yeah, we look dwo_name up again, but it simplifies the code. */
4592 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4593 gdb_assert (attr != NULL);
4594 dwo_name = DW_STRING (attr);
4595 comp_dir = NULL;
4596 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4597 if (attr)
4598 comp_dir = DW_STRING (attr);
4599
4600 if (this_cu->is_debug_types)
4601 {
4602 struct signatured_type *sig_type;
4603
4604 /* Since this_cu is the first member of struct signatured_type,
4605 we can go from a pointer to one to a pointer to the other. */
4606 sig_type = (struct signatured_type *) this_cu;
4607 signature = sig_type->signature;
4608 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
4609 }
4610 else
4611 {
4612 struct attribute *attr;
4613
4614 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4615 if (! attr)
4616 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
4617 " [in module %s]"),
4618 dwo_name, this_cu->objfile->name);
4619 signature = DW_UNSND (attr);
4620 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
4621 signature);
4622 }
4623
4624 if (dwo_unit == NULL)
4625 {
4626 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
4627 " with ID %s [in module %s]"),
4628 this_cu->offset.sect_off,
4629 phex (signature, sizeof (signature)),
4630 this_cu->objfile->name);
4631 }
4632
4633 return dwo_unit;
4634 }
4635
4636 /* Initialize a CU (or TU) and read its DIEs.
4637 If the CU defers to a DWO file, read the DWO file as well.
4638
4639 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4640 Otherwise the table specified in the comp unit header is read in and used.
4641 This is an optimization for when we already have the abbrev table.
4642
4643 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4644 Otherwise, a new CU is allocated with xmalloc.
4645
4646 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4647 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
4648
4649 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4650 linker) then DIE_READER_FUNC will not get called. */
4651
4652 static void
4653 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
4654 struct abbrev_table *abbrev_table,
4655 int use_existing_cu, int keep,
4656 die_reader_func_ftype *die_reader_func,
4657 void *data)
4658 {
4659 struct objfile *objfile = dwarf2_per_objfile->objfile;
4660 struct dwarf2_section_info *section = this_cu->section;
4661 bfd *abfd = section->asection->owner;
4662 struct dwarf2_cu *cu;
4663 gdb_byte *begin_info_ptr, *info_ptr;
4664 struct die_reader_specs reader;
4665 struct die_info *comp_unit_die;
4666 int has_children;
4667 struct attribute *attr;
4668 struct cleanup *cleanups, *free_cu_cleanup = NULL;
4669 struct signatured_type *sig_type = NULL;
4670 struct dwarf2_section_info *abbrev_section;
4671 /* Non-zero if CU currently points to a DWO file and we need to
4672 reread it. When this happens we need to reread the skeleton die
4673 before we can reread the DWO file. */
4674 int rereading_dwo_cu = 0;
4675
4676 if (dwarf2_die_debug)
4677 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4678 this_cu->is_debug_types ? "type" : "comp",
4679 this_cu->offset.sect_off);
4680
4681 if (use_existing_cu)
4682 gdb_assert (keep);
4683
4684 cleanups = make_cleanup (null_cleanup, NULL);
4685
4686 /* This is cheap if the section is already read in. */
4687 dwarf2_read_section (objfile, section);
4688
4689 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4690
4691 abbrev_section = get_abbrev_section_for_cu (this_cu);
4692
4693 if (use_existing_cu && this_cu->cu != NULL)
4694 {
4695 cu = this_cu->cu;
4696
4697 /* If this CU is from a DWO file we need to start over, we need to
4698 refetch the attributes from the skeleton CU.
4699 This could be optimized by retrieving those attributes from when we
4700 were here the first time: the previous comp_unit_die was stored in
4701 comp_unit_obstack. But there's no data yet that we need this
4702 optimization. */
4703 if (cu->dwo_unit != NULL)
4704 rereading_dwo_cu = 1;
4705 }
4706 else
4707 {
4708 /* If !use_existing_cu, this_cu->cu must be NULL. */
4709 gdb_assert (this_cu->cu == NULL);
4710
4711 cu = xmalloc (sizeof (*cu));
4712 init_one_comp_unit (cu, this_cu);
4713
4714 /* If an error occurs while loading, release our storage. */
4715 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
4716 }
4717
4718 /* Get the header. */
4719 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
4720 {
4721 /* We already have the header, there's no need to read it in again. */
4722 info_ptr += cu->header.first_die_offset.cu_off;
4723 }
4724 else
4725 {
4726 if (this_cu->is_debug_types)
4727 {
4728 ULONGEST signature;
4729 cu_offset type_offset_in_tu;
4730
4731 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4732 abbrev_section, info_ptr,
4733 &signature,
4734 &type_offset_in_tu);
4735
4736 /* Since per_cu is the first member of struct signatured_type,
4737 we can go from a pointer to one to a pointer to the other. */
4738 sig_type = (struct signatured_type *) this_cu;
4739 gdb_assert (sig_type->signature == signature);
4740 gdb_assert (sig_type->type_offset_in_tu.cu_off
4741 == type_offset_in_tu.cu_off);
4742 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4743
4744 /* LENGTH has not been set yet for type units if we're
4745 using .gdb_index. */
4746 this_cu->length = get_cu_length (&cu->header);
4747
4748 /* Establish the type offset that can be used to lookup the type. */
4749 sig_type->type_offset_in_section.sect_off =
4750 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
4751 }
4752 else
4753 {
4754 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4755 abbrev_section,
4756 info_ptr, 0);
4757
4758 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4759 gdb_assert (this_cu->length == get_cu_length (&cu->header));
4760 }
4761 }
4762
4763 /* Skip dummy compilation units. */
4764 if (info_ptr >= begin_info_ptr + this_cu->length
4765 || peek_abbrev_code (abfd, info_ptr) == 0)
4766 {
4767 do_cleanups (cleanups);
4768 return;
4769 }
4770
4771 /* If we don't have them yet, read the abbrevs for this compilation unit.
4772 And if we need to read them now, make sure they're freed when we're
4773 done. Note that it's important that if the CU had an abbrev table
4774 on entry we don't free it when we're done: Somewhere up the call stack
4775 it may be in use. */
4776 if (abbrev_table != NULL)
4777 {
4778 gdb_assert (cu->abbrev_table == NULL);
4779 gdb_assert (cu->header.abbrev_offset.sect_off
4780 == abbrev_table->offset.sect_off);
4781 cu->abbrev_table = abbrev_table;
4782 }
4783 else if (cu->abbrev_table == NULL)
4784 {
4785 dwarf2_read_abbrevs (cu, abbrev_section);
4786 make_cleanup (dwarf2_free_abbrev_table, cu);
4787 }
4788 else if (rereading_dwo_cu)
4789 {
4790 dwarf2_free_abbrev_table (cu);
4791 dwarf2_read_abbrevs (cu, abbrev_section);
4792 }
4793
4794 /* Read the top level CU/TU die. */
4795 init_cu_die_reader (&reader, cu, section, NULL);
4796 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4797
4798 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
4799 from the DWO file.
4800 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
4801 DWO CU, that this test will fail (the attribute will not be present). */
4802 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4803 if (attr)
4804 {
4805 struct dwo_unit *dwo_unit;
4806 struct die_info *dwo_comp_unit_die;
4807
4808 if (has_children)
4809 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
4810 " has children (offset 0x%x) [in module %s]"),
4811 this_cu->offset.sect_off, bfd_get_filename (abfd));
4812 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
4813 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
4814 abbrev_table != NULL,
4815 comp_unit_die,
4816 &reader, &info_ptr,
4817 &dwo_comp_unit_die, &has_children) == 0)
4818 {
4819 /* Dummy die. */
4820 do_cleanups (cleanups);
4821 return;
4822 }
4823 comp_unit_die = dwo_comp_unit_die;
4824 }
4825
4826 /* All of the above is setup for this call. Yikes. */
4827 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4828
4829 /* Done, clean up. */
4830 if (free_cu_cleanup != NULL)
4831 {
4832 if (keep)
4833 {
4834 /* We've successfully allocated this compilation unit. Let our
4835 caller clean it up when finished with it. */
4836 discard_cleanups (free_cu_cleanup);
4837
4838 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4839 So we have to manually free the abbrev table. */
4840 dwarf2_free_abbrev_table (cu);
4841
4842 /* Link this CU into read_in_chain. */
4843 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4844 dwarf2_per_objfile->read_in_chain = this_cu;
4845 }
4846 else
4847 do_cleanups (free_cu_cleanup);
4848 }
4849
4850 do_cleanups (cleanups);
4851 }
4852
4853 /* Read CU/TU THIS_CU in section SECTION,
4854 but do not follow DW_AT_GNU_dwo_name if present.
4855 DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
4856 to have already done the lookup to find the DWO/DWP file).
4857
4858 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
4859 THIS_CU->is_debug_types, but nothing else.
4860
4861 We fill in THIS_CU->length.
4862
4863 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4864 linker) then DIE_READER_FUNC will not get called.
4865
4866 THIS_CU->cu is always freed when done.
4867 This is done in order to not leave THIS_CU->cu in a state where we have
4868 to care whether it refers to the "main" CU or the DWO CU. */
4869
4870 static void
4871 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4872 struct dwarf2_section_info *abbrev_section,
4873 struct dwo_file *dwo_file,
4874 die_reader_func_ftype *die_reader_func,
4875 void *data)
4876 {
4877 struct objfile *objfile = dwarf2_per_objfile->objfile;
4878 struct dwarf2_section_info *section = this_cu->section;
4879 bfd *abfd = section->asection->owner;
4880 struct dwarf2_cu cu;
4881 gdb_byte *begin_info_ptr, *info_ptr;
4882 struct die_reader_specs reader;
4883 struct cleanup *cleanups;
4884 struct die_info *comp_unit_die;
4885 int has_children;
4886
4887 if (dwarf2_die_debug)
4888 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4889 this_cu->is_debug_types ? "type" : "comp",
4890 this_cu->offset.sect_off);
4891
4892 gdb_assert (this_cu->cu == NULL);
4893
4894 /* This is cheap if the section is already read in. */
4895 dwarf2_read_section (objfile, section);
4896
4897 init_one_comp_unit (&cu, this_cu);
4898
4899 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4900
4901 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4902 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
4903 abbrev_section, info_ptr,
4904 this_cu->is_debug_types);
4905
4906 this_cu->length = get_cu_length (&cu.header);
4907
4908 /* Skip dummy compilation units. */
4909 if (info_ptr >= begin_info_ptr + this_cu->length
4910 || peek_abbrev_code (abfd, info_ptr) == 0)
4911 {
4912 do_cleanups (cleanups);
4913 return;
4914 }
4915
4916 dwarf2_read_abbrevs (&cu, abbrev_section);
4917 make_cleanup (dwarf2_free_abbrev_table, &cu);
4918
4919 init_cu_die_reader (&reader, &cu, section, dwo_file);
4920 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4921
4922 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4923
4924 do_cleanups (cleanups);
4925 }
4926
4927 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4928 does not lookup the specified DWO file.
4929 This cannot be used to read DWO files.
4930
4931 THIS_CU->cu is always freed when done.
4932 This is done in order to not leave THIS_CU->cu in a state where we have
4933 to care whether it refers to the "main" CU or the DWO CU.
4934 We can revisit this if the data shows there's a performance issue. */
4935
4936 static void
4937 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4938 die_reader_func_ftype *die_reader_func,
4939 void *data)
4940 {
4941 init_cutu_and_read_dies_no_follow (this_cu,
4942 get_abbrev_section_for_cu (this_cu),
4943 NULL,
4944 die_reader_func, data);
4945 }
4946 \f
4947 /* Type Unit Groups.
4948
4949 Type Unit Groups are a way to collapse the set of all TUs (type units) into
4950 a more manageable set. The grouping is done by DW_AT_stmt_list entry
4951 so that all types coming from the same compilation (.o file) are grouped
4952 together. A future step could be to put the types in the same symtab as
4953 the CU the types ultimately came from. */
4954
4955 static hashval_t
4956 hash_type_unit_group (const void *item)
4957 {
4958 const struct type_unit_group *tu_group = item;
4959
4960 return hash_stmt_list_entry (&tu_group->hash);
4961 }
4962
4963 static int
4964 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
4965 {
4966 const struct type_unit_group *lhs = item_lhs;
4967 const struct type_unit_group *rhs = item_rhs;
4968
4969 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
4970 }
4971
4972 /* Allocate a hash table for type unit groups. */
4973
4974 static htab_t
4975 allocate_type_unit_groups_table (void)
4976 {
4977 return htab_create_alloc_ex (3,
4978 hash_type_unit_group,
4979 eq_type_unit_group,
4980 NULL,
4981 &dwarf2_per_objfile->objfile->objfile_obstack,
4982 hashtab_obstack_allocate,
4983 dummy_obstack_deallocate);
4984 }
4985
4986 /* Type units that don't have DW_AT_stmt_list are grouped into their own
4987 partial symtabs. We combine several TUs per psymtab to not let the size
4988 of any one psymtab grow too big. */
4989 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
4990 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
4991
4992 /* Helper routine for get_type_unit_group.
4993 Create the type_unit_group object used to hold one or more TUs. */
4994
4995 static struct type_unit_group *
4996 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
4997 {
4998 struct objfile *objfile = dwarf2_per_objfile->objfile;
4999 struct dwarf2_per_cu_data *per_cu;
5000 struct type_unit_group *tu_group;
5001
5002 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5003 struct type_unit_group);
5004 per_cu = &tu_group->per_cu;
5005 per_cu->objfile = objfile;
5006
5007 if (dwarf2_per_objfile->using_index)
5008 {
5009 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5010 struct dwarf2_per_cu_quick_data);
5011 }
5012 else
5013 {
5014 unsigned int line_offset = line_offset_struct.sect_off;
5015 struct partial_symtab *pst;
5016 char *name;
5017
5018 /* Give the symtab a useful name for debug purposes. */
5019 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5020 name = xstrprintf ("<type_units_%d>",
5021 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5022 else
5023 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5024
5025 pst = create_partial_symtab (per_cu, name);
5026 pst->anonymous = 1;
5027
5028 xfree (name);
5029 }
5030
5031 tu_group->hash.dwo_unit = cu->dwo_unit;
5032 tu_group->hash.line_offset = line_offset_struct;
5033
5034 return tu_group;
5035 }
5036
5037 /* Look up the type_unit_group for type unit CU, and create it if necessary.
5038 STMT_LIST is a DW_AT_stmt_list attribute. */
5039
5040 static struct type_unit_group *
5041 get_type_unit_group (struct dwarf2_cu *cu, struct attribute *stmt_list)
5042 {
5043 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5044 struct type_unit_group *tu_group;
5045 void **slot;
5046 unsigned int line_offset;
5047 struct type_unit_group type_unit_group_for_lookup;
5048
5049 if (dwarf2_per_objfile->type_unit_groups == NULL)
5050 {
5051 dwarf2_per_objfile->type_unit_groups =
5052 allocate_type_unit_groups_table ();
5053 }
5054
5055 /* Do we need to create a new group, or can we use an existing one? */
5056
5057 if (stmt_list)
5058 {
5059 line_offset = DW_UNSND (stmt_list);
5060 ++tu_stats->nr_symtab_sharers;
5061 }
5062 else
5063 {
5064 /* Ugh, no stmt_list. Rare, but we have to handle it.
5065 We can do various things here like create one group per TU or
5066 spread them over multiple groups to split up the expansion work.
5067 To avoid worst case scenarios (too many groups or too large groups)
5068 we, umm, group them in bunches. */
5069 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5070 | (tu_stats->nr_stmt_less_type_units
5071 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5072 ++tu_stats->nr_stmt_less_type_units;
5073 }
5074
5075 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5076 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
5077 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5078 &type_unit_group_for_lookup, INSERT);
5079 if (*slot != NULL)
5080 {
5081 tu_group = *slot;
5082 gdb_assert (tu_group != NULL);
5083 }
5084 else
5085 {
5086 sect_offset line_offset_struct;
5087
5088 line_offset_struct.sect_off = line_offset;
5089 tu_group = create_type_unit_group (cu, line_offset_struct);
5090 *slot = tu_group;
5091 ++tu_stats->nr_symtabs;
5092 }
5093
5094 return tu_group;
5095 }
5096
5097 /* Struct used to sort TUs by their abbreviation table offset. */
5098
5099 struct tu_abbrev_offset
5100 {
5101 struct signatured_type *sig_type;
5102 sect_offset abbrev_offset;
5103 };
5104
5105 /* Helper routine for build_type_unit_groups, passed to qsort. */
5106
5107 static int
5108 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5109 {
5110 const struct tu_abbrev_offset * const *a = ap;
5111 const struct tu_abbrev_offset * const *b = bp;
5112 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5113 unsigned int boff = (*b)->abbrev_offset.sect_off;
5114
5115 return (aoff > boff) - (aoff < boff);
5116 }
5117
5118 /* A helper function to add a type_unit_group to a table. */
5119
5120 static int
5121 add_type_unit_group_to_table (void **slot, void *datum)
5122 {
5123 struct type_unit_group *tu_group = *slot;
5124 struct type_unit_group ***datap = datum;
5125
5126 **datap = tu_group;
5127 ++*datap;
5128
5129 return 1;
5130 }
5131
5132 /* Efficiently read all the type units, calling init_cutu_and_read_dies on
5133 each one passing FUNC,DATA.
5134
5135 The efficiency is because we sort TUs by the abbrev table they use and
5136 only read each abbrev table once. In one program there are 200K TUs
5137 sharing 8K abbrev tables.
5138
5139 The main purpose of this function is to support building the
5140 dwarf2_per_objfile->type_unit_groups table.
5141 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5142 can collapse the search space by grouping them by stmt_list.
5143 The savings can be significant, in the same program from above the 200K TUs
5144 share 8K stmt_list tables.
5145
5146 FUNC is expected to call get_type_unit_group, which will create the
5147 struct type_unit_group if necessary and add it to
5148 dwarf2_per_objfile->type_unit_groups. */
5149
5150 static void
5151 build_type_unit_groups (die_reader_func_ftype *func, void *data)
5152 {
5153 struct objfile *objfile = dwarf2_per_objfile->objfile;
5154 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5155 struct cleanup *cleanups;
5156 struct abbrev_table *abbrev_table;
5157 sect_offset abbrev_offset;
5158 struct tu_abbrev_offset *sorted_by_abbrev;
5159 struct type_unit_group **iter;
5160 int i;
5161
5162 /* It's up to the caller to not call us multiple times. */
5163 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5164
5165 if (dwarf2_per_objfile->n_type_units == 0)
5166 return;
5167
5168 /* TUs typically share abbrev tables, and there can be way more TUs than
5169 abbrev tables. Sort by abbrev table to reduce the number of times we
5170 read each abbrev table in.
5171 Alternatives are to punt or to maintain a cache of abbrev tables.
5172 This is simpler and efficient enough for now.
5173
5174 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5175 symtab to use). Typically TUs with the same abbrev offset have the same
5176 stmt_list value too so in practice this should work well.
5177
5178 The basic algorithm here is:
5179
5180 sort TUs by abbrev table
5181 for each TU with same abbrev table:
5182 read abbrev table if first user
5183 read TU top level DIE
5184 [IWBN if DWO skeletons had DW_AT_stmt_list]
5185 call FUNC */
5186
5187 if (dwarf2_read_debug)
5188 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5189
5190 /* Sort in a separate table to maintain the order of all_type_units
5191 for .gdb_index: TU indices directly index all_type_units. */
5192 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5193 dwarf2_per_objfile->n_type_units);
5194 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5195 {
5196 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5197
5198 sorted_by_abbrev[i].sig_type = sig_type;
5199 sorted_by_abbrev[i].abbrev_offset =
5200 read_abbrev_offset (sig_type->per_cu.section,
5201 sig_type->per_cu.offset);
5202 }
5203 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5204 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5205 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5206
5207 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5208 called any number of times, so we don't reset tu_stats here. */
5209
5210 abbrev_offset.sect_off = ~(unsigned) 0;
5211 abbrev_table = NULL;
5212 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5213
5214 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5215 {
5216 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5217
5218 /* Switch to the next abbrev table if necessary. */
5219 if (abbrev_table == NULL
5220 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5221 {
5222 if (abbrev_table != NULL)
5223 {
5224 abbrev_table_free (abbrev_table);
5225 /* Reset to NULL in case abbrev_table_read_table throws
5226 an error: abbrev_table_free_cleanup will get called. */
5227 abbrev_table = NULL;
5228 }
5229 abbrev_offset = tu->abbrev_offset;
5230 abbrev_table =
5231 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5232 abbrev_offset);
5233 ++tu_stats->nr_uniq_abbrev_tables;
5234 }
5235
5236 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5237 func, data);
5238 }
5239
5240 /* Create a vector of pointers to primary type units to make it easy to
5241 iterate over them and CUs. See dw2_get_primary_cu. */
5242 dwarf2_per_objfile->n_type_unit_groups =
5243 htab_elements (dwarf2_per_objfile->type_unit_groups);
5244 dwarf2_per_objfile->all_type_unit_groups =
5245 obstack_alloc (&objfile->objfile_obstack,
5246 dwarf2_per_objfile->n_type_unit_groups
5247 * sizeof (struct type_unit_group *));
5248 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5249 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5250 add_type_unit_group_to_table, &iter);
5251 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5252 == dwarf2_per_objfile->n_type_unit_groups);
5253
5254 do_cleanups (cleanups);
5255
5256 if (dwarf2_read_debug)
5257 {
5258 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5259 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5260 dwarf2_per_objfile->n_type_units);
5261 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5262 tu_stats->nr_uniq_abbrev_tables);
5263 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5264 tu_stats->nr_symtabs);
5265 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5266 tu_stats->nr_symtab_sharers);
5267 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5268 tu_stats->nr_stmt_less_type_units);
5269 }
5270 }
5271 \f
5272 /* Partial symbol tables. */
5273
5274 /* Create a psymtab named NAME and assign it to PER_CU.
5275
5276 The caller must fill in the following details:
5277 dirname, textlow, texthigh. */
5278
5279 static struct partial_symtab *
5280 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
5281 {
5282 struct objfile *objfile = per_cu->objfile;
5283 struct partial_symtab *pst;
5284
5285 pst = start_psymtab_common (objfile, objfile->section_offsets,
5286 name, 0,
5287 objfile->global_psymbols.next,
5288 objfile->static_psymbols.next);
5289
5290 pst->psymtabs_addrmap_supported = 1;
5291
5292 /* This is the glue that links PST into GDB's symbol API. */
5293 pst->read_symtab_private = per_cu;
5294 pst->read_symtab = dwarf2_read_symtab;
5295 per_cu->v.psymtab = pst;
5296
5297 return pst;
5298 }
5299
5300 /* die_reader_func for process_psymtab_comp_unit. */
5301
5302 static void
5303 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
5304 gdb_byte *info_ptr,
5305 struct die_info *comp_unit_die,
5306 int has_children,
5307 void *data)
5308 {
5309 struct dwarf2_cu *cu = reader->cu;
5310 struct objfile *objfile = cu->objfile;
5311 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5312 struct attribute *attr;
5313 CORE_ADDR baseaddr;
5314 CORE_ADDR best_lowpc = 0, best_highpc = 0;
5315 struct partial_symtab *pst;
5316 int has_pc_info;
5317 const char *filename;
5318 int *want_partial_unit_ptr = data;
5319
5320 if (comp_unit_die->tag == DW_TAG_partial_unit
5321 && (want_partial_unit_ptr == NULL
5322 || !*want_partial_unit_ptr))
5323 return;
5324
5325 gdb_assert (! per_cu->is_debug_types);
5326
5327 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
5328
5329 cu->list_in_scope = &file_symbols;
5330
5331 /* Allocate a new partial symbol table structure. */
5332 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
5333 if (attr == NULL || !DW_STRING (attr))
5334 filename = "";
5335 else
5336 filename = DW_STRING (attr);
5337
5338 pst = create_partial_symtab (per_cu, filename);
5339
5340 /* This must be done before calling dwarf2_build_include_psymtabs. */
5341 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
5342 if (attr != NULL)
5343 pst->dirname = DW_STRING (attr);
5344
5345 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5346
5347 dwarf2_find_base_address (comp_unit_die, cu);
5348
5349 /* Possibly set the default values of LOWPC and HIGHPC from
5350 `DW_AT_ranges'. */
5351 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
5352 &best_highpc, cu, pst);
5353 if (has_pc_info == 1 && best_lowpc < best_highpc)
5354 /* Store the contiguous range if it is not empty; it can be empty for
5355 CUs with no code. */
5356 addrmap_set_empty (objfile->psymtabs_addrmap,
5357 best_lowpc + baseaddr,
5358 best_highpc + baseaddr - 1, pst);
5359
5360 /* Check if comp unit has_children.
5361 If so, read the rest of the partial symbols from this comp unit.
5362 If not, there's no more debug_info for this comp unit. */
5363 if (has_children)
5364 {
5365 struct partial_die_info *first_die;
5366 CORE_ADDR lowpc, highpc;
5367
5368 lowpc = ((CORE_ADDR) -1);
5369 highpc = ((CORE_ADDR) 0);
5370
5371 first_die = load_partial_dies (reader, info_ptr, 1);
5372
5373 scan_partial_symbols (first_die, &lowpc, &highpc,
5374 ! has_pc_info, cu);
5375
5376 /* If we didn't find a lowpc, set it to highpc to avoid
5377 complaints from `maint check'. */
5378 if (lowpc == ((CORE_ADDR) -1))
5379 lowpc = highpc;
5380
5381 /* If the compilation unit didn't have an explicit address range,
5382 then use the information extracted from its child dies. */
5383 if (! has_pc_info)
5384 {
5385 best_lowpc = lowpc;
5386 best_highpc = highpc;
5387 }
5388 }
5389 pst->textlow = best_lowpc + baseaddr;
5390 pst->texthigh = best_highpc + baseaddr;
5391
5392 pst->n_global_syms = objfile->global_psymbols.next -
5393 (objfile->global_psymbols.list + pst->globals_offset);
5394 pst->n_static_syms = objfile->static_psymbols.next -
5395 (objfile->static_psymbols.list + pst->statics_offset);
5396 sort_pst_symbols (objfile, pst);
5397
5398 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
5399 {
5400 int i;
5401 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5402 struct dwarf2_per_cu_data *iter;
5403
5404 /* Fill in 'dependencies' here; we fill in 'users' in a
5405 post-pass. */
5406 pst->number_of_dependencies = len;
5407 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5408 len * sizeof (struct symtab *));
5409 for (i = 0;
5410 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
5411 i, iter);
5412 ++i)
5413 pst->dependencies[i] = iter->v.psymtab;
5414
5415 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5416 }
5417
5418 /* Get the list of files included in the current compilation unit,
5419 and build a psymtab for each of them. */
5420 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
5421
5422 if (dwarf2_read_debug)
5423 {
5424 struct gdbarch *gdbarch = get_objfile_arch (objfile);
5425
5426 fprintf_unfiltered (gdb_stdlog,
5427 "Psymtab for %s unit @0x%x: %s - %s"
5428 ", %d global, %d static syms\n",
5429 per_cu->is_debug_types ? "type" : "comp",
5430 per_cu->offset.sect_off,
5431 paddress (gdbarch, pst->textlow),
5432 paddress (gdbarch, pst->texthigh),
5433 pst->n_global_syms, pst->n_static_syms);
5434 }
5435 }
5436
5437 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5438 Process compilation unit THIS_CU for a psymtab. */
5439
5440 static void
5441 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
5442 int want_partial_unit)
5443 {
5444 /* If this compilation unit was already read in, free the
5445 cached copy in order to read it in again. This is
5446 necessary because we skipped some symbols when we first
5447 read in the compilation unit (see load_partial_dies).
5448 This problem could be avoided, but the benefit is unclear. */
5449 if (this_cu->cu != NULL)
5450 free_one_cached_comp_unit (this_cu);
5451
5452 gdb_assert (! this_cu->is_debug_types);
5453 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
5454 process_psymtab_comp_unit_reader,
5455 &want_partial_unit);
5456
5457 /* Age out any secondary CUs. */
5458 age_cached_comp_units ();
5459 }
5460
5461 /* Reader function for build_type_psymtabs. */
5462
5463 static void
5464 build_type_psymtabs_reader (const struct die_reader_specs *reader,
5465 gdb_byte *info_ptr,
5466 struct die_info *type_unit_die,
5467 int has_children,
5468 void *data)
5469 {
5470 struct objfile *objfile = dwarf2_per_objfile->objfile;
5471 struct dwarf2_cu *cu = reader->cu;
5472 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5473 struct signatured_type *sig_type;
5474 struct type_unit_group *tu_group;
5475 struct attribute *attr;
5476 struct partial_die_info *first_die;
5477 CORE_ADDR lowpc, highpc;
5478 struct partial_symtab *pst;
5479
5480 gdb_assert (data == NULL);
5481 gdb_assert (per_cu->is_debug_types);
5482 sig_type = (struct signatured_type *) per_cu;
5483
5484 if (! has_children)
5485 return;
5486
5487 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
5488 tu_group = get_type_unit_group (cu, attr);
5489
5490 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
5491
5492 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5493 cu->list_in_scope = &file_symbols;
5494 pst = create_partial_symtab (per_cu, "");
5495 pst->anonymous = 1;
5496
5497 first_die = load_partial_dies (reader, info_ptr, 1);
5498
5499 lowpc = (CORE_ADDR) -1;
5500 highpc = (CORE_ADDR) 0;
5501 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5502
5503 pst->n_global_syms = objfile->global_psymbols.next -
5504 (objfile->global_psymbols.list + pst->globals_offset);
5505 pst->n_static_syms = objfile->static_psymbols.next -
5506 (objfile->static_psymbols.list + pst->statics_offset);
5507 sort_pst_symbols (objfile, pst);
5508 }
5509
5510 /* Traversal function for build_type_psymtabs. */
5511
5512 static int
5513 build_type_psymtab_dependencies (void **slot, void *info)
5514 {
5515 struct objfile *objfile = dwarf2_per_objfile->objfile;
5516 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
5517 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
5518 struct partial_symtab *pst = per_cu->v.psymtab;
5519 int len = VEC_length (sig_type_ptr, tu_group->tus);
5520 struct signatured_type *iter;
5521 int i;
5522
5523 gdb_assert (len > 0);
5524 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
5525
5526 pst->number_of_dependencies = len;
5527 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5528 len * sizeof (struct psymtab *));
5529 for (i = 0;
5530 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
5531 ++i)
5532 {
5533 gdb_assert (iter->per_cu.is_debug_types);
5534 pst->dependencies[i] = iter->per_cu.v.psymtab;
5535 iter->type_unit_group = tu_group;
5536 }
5537
5538 VEC_free (sig_type_ptr, tu_group->tus);
5539
5540 return 1;
5541 }
5542
5543 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5544 Build partial symbol tables for the .debug_types comp-units. */
5545
5546 static void
5547 build_type_psymtabs (struct objfile *objfile)
5548 {
5549 if (! create_all_type_units (objfile))
5550 return;
5551
5552 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5553
5554 /* Now that all TUs have been processed we can fill in the dependencies. */
5555 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5556 build_type_psymtab_dependencies, NULL);
5557 }
5558
5559 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
5560
5561 static void
5562 psymtabs_addrmap_cleanup (void *o)
5563 {
5564 struct objfile *objfile = o;
5565
5566 objfile->psymtabs_addrmap = NULL;
5567 }
5568
5569 /* Compute the 'user' field for each psymtab in OBJFILE. */
5570
5571 static void
5572 set_partial_user (struct objfile *objfile)
5573 {
5574 int i;
5575
5576 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5577 {
5578 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5579 struct partial_symtab *pst = per_cu->v.psymtab;
5580 int j;
5581
5582 if (pst == NULL)
5583 continue;
5584
5585 for (j = 0; j < pst->number_of_dependencies; ++j)
5586 {
5587 /* Set the 'user' field only if it is not already set. */
5588 if (pst->dependencies[j]->user == NULL)
5589 pst->dependencies[j]->user = pst;
5590 }
5591 }
5592 }
5593
5594 /* Build the partial symbol table by doing a quick pass through the
5595 .debug_info and .debug_abbrev sections. */
5596
5597 static void
5598 dwarf2_build_psymtabs_hard (struct objfile *objfile)
5599 {
5600 struct cleanup *back_to, *addrmap_cleanup;
5601 struct obstack temp_obstack;
5602 int i;
5603
5604 if (dwarf2_read_debug)
5605 {
5606 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5607 objfile->name);
5608 }
5609
5610 dwarf2_per_objfile->reading_partial_symbols = 1;
5611
5612 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
5613
5614 /* Any cached compilation units will be linked by the per-objfile
5615 read_in_chain. Make sure to free them when we're done. */
5616 back_to = make_cleanup (free_cached_comp_units, NULL);
5617
5618 build_type_psymtabs (objfile);
5619
5620 create_all_comp_units (objfile);
5621
5622 /* Create a temporary address map on a temporary obstack. We later
5623 copy this to the final obstack. */
5624 obstack_init (&temp_obstack);
5625 make_cleanup_obstack_free (&temp_obstack);
5626 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
5627 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
5628
5629 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5630 {
5631 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5632
5633 process_psymtab_comp_unit (per_cu, 0);
5634 }
5635
5636 set_partial_user (objfile);
5637
5638 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
5639 &objfile->objfile_obstack);
5640 discard_cleanups (addrmap_cleanup);
5641
5642 do_cleanups (back_to);
5643
5644 if (dwarf2_read_debug)
5645 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
5646 objfile->name);
5647 }
5648
5649 /* die_reader_func for load_partial_comp_unit. */
5650
5651 static void
5652 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
5653 gdb_byte *info_ptr,
5654 struct die_info *comp_unit_die,
5655 int has_children,
5656 void *data)
5657 {
5658 struct dwarf2_cu *cu = reader->cu;
5659
5660 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
5661
5662 /* Check if comp unit has_children.
5663 If so, read the rest of the partial symbols from this comp unit.
5664 If not, there's no more debug_info for this comp unit. */
5665 if (has_children)
5666 load_partial_dies (reader, info_ptr, 0);
5667 }
5668
5669 /* Load the partial DIEs for a secondary CU into memory.
5670 This is also used when rereading a primary CU with load_all_dies. */
5671
5672 static void
5673 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
5674 {
5675 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
5676 load_partial_comp_unit_reader, NULL);
5677 }
5678
5679 static void
5680 read_comp_units_from_section (struct objfile *objfile,
5681 struct dwarf2_section_info *section,
5682 unsigned int is_dwz,
5683 int *n_allocated,
5684 int *n_comp_units,
5685 struct dwarf2_per_cu_data ***all_comp_units)
5686 {
5687 gdb_byte *info_ptr;
5688 bfd *abfd = section->asection->owner;
5689
5690 dwarf2_read_section (objfile, section);
5691
5692 info_ptr = section->buffer;
5693
5694 while (info_ptr < section->buffer + section->size)
5695 {
5696 unsigned int length, initial_length_size;
5697 struct dwarf2_per_cu_data *this_cu;
5698 sect_offset offset;
5699
5700 offset.sect_off = info_ptr - section->buffer;
5701
5702 /* Read just enough information to find out where the next
5703 compilation unit is. */
5704 length = read_initial_length (abfd, info_ptr, &initial_length_size);
5705
5706 /* Save the compilation unit for later lookup. */
5707 this_cu = obstack_alloc (&objfile->objfile_obstack,
5708 sizeof (struct dwarf2_per_cu_data));
5709 memset (this_cu, 0, sizeof (*this_cu));
5710 this_cu->offset = offset;
5711 this_cu->length = length + initial_length_size;
5712 this_cu->is_dwz = is_dwz;
5713 this_cu->objfile = objfile;
5714 this_cu->section = section;
5715
5716 if (*n_comp_units == *n_allocated)
5717 {
5718 *n_allocated *= 2;
5719 *all_comp_units = xrealloc (*all_comp_units,
5720 *n_allocated
5721 * sizeof (struct dwarf2_per_cu_data *));
5722 }
5723 (*all_comp_units)[*n_comp_units] = this_cu;
5724 ++*n_comp_units;
5725
5726 info_ptr = info_ptr + this_cu->length;
5727 }
5728 }
5729
5730 /* Create a list of all compilation units in OBJFILE.
5731 This is only done for -readnow and building partial symtabs. */
5732
5733 static void
5734 create_all_comp_units (struct objfile *objfile)
5735 {
5736 int n_allocated;
5737 int n_comp_units;
5738 struct dwarf2_per_cu_data **all_comp_units;
5739
5740 n_comp_units = 0;
5741 n_allocated = 10;
5742 all_comp_units = xmalloc (n_allocated
5743 * sizeof (struct dwarf2_per_cu_data *));
5744
5745 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
5746 &n_allocated, &n_comp_units, &all_comp_units);
5747
5748 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
5749 {
5750 struct dwz_file *dwz = dwarf2_get_dwz_file ();
5751
5752 read_comp_units_from_section (objfile, &dwz->info, 1,
5753 &n_allocated, &n_comp_units,
5754 &all_comp_units);
5755 }
5756
5757 dwarf2_per_objfile->all_comp_units
5758 = obstack_alloc (&objfile->objfile_obstack,
5759 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5760 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
5761 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5762 xfree (all_comp_units);
5763 dwarf2_per_objfile->n_comp_units = n_comp_units;
5764 }
5765
5766 /* Process all loaded DIEs for compilation unit CU, starting at
5767 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
5768 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
5769 DW_AT_ranges). If NEED_PC is set, then this function will set
5770 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
5771 and record the covered ranges in the addrmap. */
5772
5773 static void
5774 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5775 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
5776 {
5777 struct partial_die_info *pdi;
5778
5779 /* Now, march along the PDI's, descending into ones which have
5780 interesting children but skipping the children of the other ones,
5781 until we reach the end of the compilation unit. */
5782
5783 pdi = first_die;
5784
5785 while (pdi != NULL)
5786 {
5787 fixup_partial_die (pdi, cu);
5788
5789 /* Anonymous namespaces or modules have no name but have interesting
5790 children, so we need to look at them. Ditto for anonymous
5791 enums. */
5792
5793 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
5794 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
5795 || pdi->tag == DW_TAG_imported_unit)
5796 {
5797 switch (pdi->tag)
5798 {
5799 case DW_TAG_subprogram:
5800 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
5801 break;
5802 case DW_TAG_constant:
5803 case DW_TAG_variable:
5804 case DW_TAG_typedef:
5805 case DW_TAG_union_type:
5806 if (!pdi->is_declaration)
5807 {
5808 add_partial_symbol (pdi, cu);
5809 }
5810 break;
5811 case DW_TAG_class_type:
5812 case DW_TAG_interface_type:
5813 case DW_TAG_structure_type:
5814 if (!pdi->is_declaration)
5815 {
5816 add_partial_symbol (pdi, cu);
5817 }
5818 break;
5819 case DW_TAG_enumeration_type:
5820 if (!pdi->is_declaration)
5821 add_partial_enumeration (pdi, cu);
5822 break;
5823 case DW_TAG_base_type:
5824 case DW_TAG_subrange_type:
5825 /* File scope base type definitions are added to the partial
5826 symbol table. */
5827 add_partial_symbol (pdi, cu);
5828 break;
5829 case DW_TAG_namespace:
5830 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
5831 break;
5832 case DW_TAG_module:
5833 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
5834 break;
5835 case DW_TAG_imported_unit:
5836 {
5837 struct dwarf2_per_cu_data *per_cu;
5838
5839 /* For now we don't handle imported units in type units. */
5840 if (cu->per_cu->is_debug_types)
5841 {
5842 error (_("Dwarf Error: DW_TAG_imported_unit is not"
5843 " supported in type units [in module %s]"),
5844 cu->objfile->name);
5845 }
5846
5847 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
5848 pdi->is_dwz,
5849 cu->objfile);
5850
5851 /* Go read the partial unit, if needed. */
5852 if (per_cu->v.psymtab == NULL)
5853 process_psymtab_comp_unit (per_cu, 1);
5854
5855 VEC_safe_push (dwarf2_per_cu_ptr,
5856 cu->per_cu->imported_symtabs, per_cu);
5857 }
5858 break;
5859 default:
5860 break;
5861 }
5862 }
5863
5864 /* If the die has a sibling, skip to the sibling. */
5865
5866 pdi = pdi->die_sibling;
5867 }
5868 }
5869
5870 /* Functions used to compute the fully scoped name of a partial DIE.
5871
5872 Normally, this is simple. For C++, the parent DIE's fully scoped
5873 name is concatenated with "::" and the partial DIE's name. For
5874 Java, the same thing occurs except that "." is used instead of "::".
5875 Enumerators are an exception; they use the scope of their parent
5876 enumeration type, i.e. the name of the enumeration type is not
5877 prepended to the enumerator.
5878
5879 There are two complexities. One is DW_AT_specification; in this
5880 case "parent" means the parent of the target of the specification,
5881 instead of the direct parent of the DIE. The other is compilers
5882 which do not emit DW_TAG_namespace; in this case we try to guess
5883 the fully qualified name of structure types from their members'
5884 linkage names. This must be done using the DIE's children rather
5885 than the children of any DW_AT_specification target. We only need
5886 to do this for structures at the top level, i.e. if the target of
5887 any DW_AT_specification (if any; otherwise the DIE itself) does not
5888 have a parent. */
5889
5890 /* Compute the scope prefix associated with PDI's parent, in
5891 compilation unit CU. The result will be allocated on CU's
5892 comp_unit_obstack, or a copy of the already allocated PDI->NAME
5893 field. NULL is returned if no prefix is necessary. */
5894 static const char *
5895 partial_die_parent_scope (struct partial_die_info *pdi,
5896 struct dwarf2_cu *cu)
5897 {
5898 const char *grandparent_scope;
5899 struct partial_die_info *parent, *real_pdi;
5900
5901 /* We need to look at our parent DIE; if we have a DW_AT_specification,
5902 then this means the parent of the specification DIE. */
5903
5904 real_pdi = pdi;
5905 while (real_pdi->has_specification)
5906 real_pdi = find_partial_die (real_pdi->spec_offset,
5907 real_pdi->spec_is_dwz, cu);
5908
5909 parent = real_pdi->die_parent;
5910 if (parent == NULL)
5911 return NULL;
5912
5913 if (parent->scope_set)
5914 return parent->scope;
5915
5916 fixup_partial_die (parent, cu);
5917
5918 grandparent_scope = partial_die_parent_scope (parent, cu);
5919
5920 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
5921 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
5922 Work around this problem here. */
5923 if (cu->language == language_cplus
5924 && parent->tag == DW_TAG_namespace
5925 && strcmp (parent->name, "::") == 0
5926 && grandparent_scope == NULL)
5927 {
5928 parent->scope = NULL;
5929 parent->scope_set = 1;
5930 return NULL;
5931 }
5932
5933 if (pdi->tag == DW_TAG_enumerator)
5934 /* Enumerators should not get the name of the enumeration as a prefix. */
5935 parent->scope = grandparent_scope;
5936 else if (parent->tag == DW_TAG_namespace
5937 || parent->tag == DW_TAG_module
5938 || parent->tag == DW_TAG_structure_type
5939 || parent->tag == DW_TAG_class_type
5940 || parent->tag == DW_TAG_interface_type
5941 || parent->tag == DW_TAG_union_type
5942 || parent->tag == DW_TAG_enumeration_type)
5943 {
5944 if (grandparent_scope == NULL)
5945 parent->scope = parent->name;
5946 else
5947 parent->scope = typename_concat (&cu->comp_unit_obstack,
5948 grandparent_scope,
5949 parent->name, 0, cu);
5950 }
5951 else
5952 {
5953 /* FIXME drow/2004-04-01: What should we be doing with
5954 function-local names? For partial symbols, we should probably be
5955 ignoring them. */
5956 complaint (&symfile_complaints,
5957 _("unhandled containing DIE tag %d for DIE at %d"),
5958 parent->tag, pdi->offset.sect_off);
5959 parent->scope = grandparent_scope;
5960 }
5961
5962 parent->scope_set = 1;
5963 return parent->scope;
5964 }
5965
5966 /* Return the fully scoped name associated with PDI, from compilation unit
5967 CU. The result will be allocated with malloc. */
5968
5969 static char *
5970 partial_die_full_name (struct partial_die_info *pdi,
5971 struct dwarf2_cu *cu)
5972 {
5973 const char *parent_scope;
5974
5975 /* If this is a template instantiation, we can not work out the
5976 template arguments from partial DIEs. So, unfortunately, we have
5977 to go through the full DIEs. At least any work we do building
5978 types here will be reused if full symbols are loaded later. */
5979 if (pdi->has_template_arguments)
5980 {
5981 fixup_partial_die (pdi, cu);
5982
5983 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
5984 {
5985 struct die_info *die;
5986 struct attribute attr;
5987 struct dwarf2_cu *ref_cu = cu;
5988
5989 /* DW_FORM_ref_addr is using section offset. */
5990 attr.name = 0;
5991 attr.form = DW_FORM_ref_addr;
5992 attr.u.unsnd = pdi->offset.sect_off;
5993 die = follow_die_ref (NULL, &attr, &ref_cu);
5994
5995 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
5996 }
5997 }
5998
5999 parent_scope = partial_die_parent_scope (pdi, cu);
6000 if (parent_scope == NULL)
6001 return NULL;
6002 else
6003 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
6004 }
6005
6006 static void
6007 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
6008 {
6009 struct objfile *objfile = cu->objfile;
6010 CORE_ADDR addr = 0;
6011 const char *actual_name = NULL;
6012 CORE_ADDR baseaddr;
6013 char *built_actual_name;
6014
6015 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6016
6017 built_actual_name = partial_die_full_name (pdi, cu);
6018 if (built_actual_name != NULL)
6019 actual_name = built_actual_name;
6020
6021 if (actual_name == NULL)
6022 actual_name = pdi->name;
6023
6024 switch (pdi->tag)
6025 {
6026 case DW_TAG_subprogram:
6027 if (pdi->is_external || cu->language == language_ada)
6028 {
6029 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
6030 of the global scope. But in Ada, we want to be able to access
6031 nested procedures globally. So all Ada subprograms are stored
6032 in the global scope. */
6033 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
6034 mst_text, objfile); */
6035 add_psymbol_to_list (actual_name, strlen (actual_name),
6036 built_actual_name != NULL,
6037 VAR_DOMAIN, LOC_BLOCK,
6038 &objfile->global_psymbols,
6039 0, pdi->lowpc + baseaddr,
6040 cu->language, objfile);
6041 }
6042 else
6043 {
6044 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
6045 mst_file_text, objfile); */
6046 add_psymbol_to_list (actual_name, strlen (actual_name),
6047 built_actual_name != NULL,
6048 VAR_DOMAIN, LOC_BLOCK,
6049 &objfile->static_psymbols,
6050 0, pdi->lowpc + baseaddr,
6051 cu->language, objfile);
6052 }
6053 break;
6054 case DW_TAG_constant:
6055 {
6056 struct psymbol_allocation_list *list;
6057
6058 if (pdi->is_external)
6059 list = &objfile->global_psymbols;
6060 else
6061 list = &objfile->static_psymbols;
6062 add_psymbol_to_list (actual_name, strlen (actual_name),
6063 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
6064 list, 0, 0, cu->language, objfile);
6065 }
6066 break;
6067 case DW_TAG_variable:
6068 if (pdi->d.locdesc)
6069 addr = decode_locdesc (pdi->d.locdesc, cu);
6070
6071 if (pdi->d.locdesc
6072 && addr == 0
6073 && !dwarf2_per_objfile->has_section_at_zero)
6074 {
6075 /* A global or static variable may also have been stripped
6076 out by the linker if unused, in which case its address
6077 will be nullified; do not add such variables into partial
6078 symbol table then. */
6079 }
6080 else if (pdi->is_external)
6081 {
6082 /* Global Variable.
6083 Don't enter into the minimal symbol tables as there is
6084 a minimal symbol table entry from the ELF symbols already.
6085 Enter into partial symbol table if it has a location
6086 descriptor or a type.
6087 If the location descriptor is missing, new_symbol will create
6088 a LOC_UNRESOLVED symbol, the address of the variable will then
6089 be determined from the minimal symbol table whenever the variable
6090 is referenced.
6091 The address for the partial symbol table entry is not
6092 used by GDB, but it comes in handy for debugging partial symbol
6093 table building. */
6094
6095 if (pdi->d.locdesc || pdi->has_type)
6096 add_psymbol_to_list (actual_name, strlen (actual_name),
6097 built_actual_name != NULL,
6098 VAR_DOMAIN, LOC_STATIC,
6099 &objfile->global_psymbols,
6100 0, addr + baseaddr,
6101 cu->language, objfile);
6102 }
6103 else
6104 {
6105 /* Static Variable. Skip symbols without location descriptors. */
6106 if (pdi->d.locdesc == NULL)
6107 {
6108 xfree (built_actual_name);
6109 return;
6110 }
6111 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
6112 mst_file_data, objfile); */
6113 add_psymbol_to_list (actual_name, strlen (actual_name),
6114 built_actual_name != NULL,
6115 VAR_DOMAIN, LOC_STATIC,
6116 &objfile->static_psymbols,
6117 0, addr + baseaddr,
6118 cu->language, objfile);
6119 }
6120 break;
6121 case DW_TAG_typedef:
6122 case DW_TAG_base_type:
6123 case DW_TAG_subrange_type:
6124 add_psymbol_to_list (actual_name, strlen (actual_name),
6125 built_actual_name != NULL,
6126 VAR_DOMAIN, LOC_TYPEDEF,
6127 &objfile->static_psymbols,
6128 0, (CORE_ADDR) 0, cu->language, objfile);
6129 break;
6130 case DW_TAG_namespace:
6131 add_psymbol_to_list (actual_name, strlen (actual_name),
6132 built_actual_name != NULL,
6133 VAR_DOMAIN, LOC_TYPEDEF,
6134 &objfile->global_psymbols,
6135 0, (CORE_ADDR) 0, cu->language, objfile);
6136 break;
6137 case DW_TAG_class_type:
6138 case DW_TAG_interface_type:
6139 case DW_TAG_structure_type:
6140 case DW_TAG_union_type:
6141 case DW_TAG_enumeration_type:
6142 /* Skip external references. The DWARF standard says in the section
6143 about "Structure, Union, and Class Type Entries": "An incomplete
6144 structure, union or class type is represented by a structure,
6145 union or class entry that does not have a byte size attribute
6146 and that has a DW_AT_declaration attribute." */
6147 if (!pdi->has_byte_size && pdi->is_declaration)
6148 {
6149 xfree (built_actual_name);
6150 return;
6151 }
6152
6153 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
6154 static vs. global. */
6155 add_psymbol_to_list (actual_name, strlen (actual_name),
6156 built_actual_name != NULL,
6157 STRUCT_DOMAIN, LOC_TYPEDEF,
6158 (cu->language == language_cplus
6159 || cu->language == language_java)
6160 ? &objfile->global_psymbols
6161 : &objfile->static_psymbols,
6162 0, (CORE_ADDR) 0, cu->language, objfile);
6163
6164 break;
6165 case DW_TAG_enumerator:
6166 add_psymbol_to_list (actual_name, strlen (actual_name),
6167 built_actual_name != NULL,
6168 VAR_DOMAIN, LOC_CONST,
6169 (cu->language == language_cplus
6170 || cu->language == language_java)
6171 ? &objfile->global_psymbols
6172 : &objfile->static_psymbols,
6173 0, (CORE_ADDR) 0, cu->language, objfile);
6174 break;
6175 default:
6176 break;
6177 }
6178
6179 xfree (built_actual_name);
6180 }
6181
6182 /* Read a partial die corresponding to a namespace; also, add a symbol
6183 corresponding to that namespace to the symbol table. NAMESPACE is
6184 the name of the enclosing namespace. */
6185
6186 static void
6187 add_partial_namespace (struct partial_die_info *pdi,
6188 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6189 int need_pc, struct dwarf2_cu *cu)
6190 {
6191 /* Add a symbol for the namespace. */
6192
6193 add_partial_symbol (pdi, cu);
6194
6195 /* Now scan partial symbols in that namespace. */
6196
6197 if (pdi->has_children)
6198 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6199 }
6200
6201 /* Read a partial die corresponding to a Fortran module. */
6202
6203 static void
6204 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
6205 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6206 {
6207 /* Now scan partial symbols in that module. */
6208
6209 if (pdi->has_children)
6210 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6211 }
6212
6213 /* Read a partial die corresponding to a subprogram and create a partial
6214 symbol for that subprogram. When the CU language allows it, this
6215 routine also defines a partial symbol for each nested subprogram
6216 that this subprogram contains.
6217
6218 DIE my also be a lexical block, in which case we simply search
6219 recursively for suprograms defined inside that lexical block.
6220 Again, this is only performed when the CU language allows this
6221 type of definitions. */
6222
6223 static void
6224 add_partial_subprogram (struct partial_die_info *pdi,
6225 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6226 int need_pc, struct dwarf2_cu *cu)
6227 {
6228 if (pdi->tag == DW_TAG_subprogram)
6229 {
6230 if (pdi->has_pc_info)
6231 {
6232 if (pdi->lowpc < *lowpc)
6233 *lowpc = pdi->lowpc;
6234 if (pdi->highpc > *highpc)
6235 *highpc = pdi->highpc;
6236 if (need_pc)
6237 {
6238 CORE_ADDR baseaddr;
6239 struct objfile *objfile = cu->objfile;
6240
6241 baseaddr = ANOFFSET (objfile->section_offsets,
6242 SECT_OFF_TEXT (objfile));
6243 addrmap_set_empty (objfile->psymtabs_addrmap,
6244 pdi->lowpc + baseaddr,
6245 pdi->highpc - 1 + baseaddr,
6246 cu->per_cu->v.psymtab);
6247 }
6248 }
6249
6250 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6251 {
6252 if (!pdi->is_declaration)
6253 /* Ignore subprogram DIEs that do not have a name, they are
6254 illegal. Do not emit a complaint at this point, we will
6255 do so when we convert this psymtab into a symtab. */
6256 if (pdi->name)
6257 add_partial_symbol (pdi, cu);
6258 }
6259 }
6260
6261 if (! pdi->has_children)
6262 return;
6263
6264 if (cu->language == language_ada)
6265 {
6266 pdi = pdi->die_child;
6267 while (pdi != NULL)
6268 {
6269 fixup_partial_die (pdi, cu);
6270 if (pdi->tag == DW_TAG_subprogram
6271 || pdi->tag == DW_TAG_lexical_block)
6272 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
6273 pdi = pdi->die_sibling;
6274 }
6275 }
6276 }
6277
6278 /* Read a partial die corresponding to an enumeration type. */
6279
6280 static void
6281 add_partial_enumeration (struct partial_die_info *enum_pdi,
6282 struct dwarf2_cu *cu)
6283 {
6284 struct partial_die_info *pdi;
6285
6286 if (enum_pdi->name != NULL)
6287 add_partial_symbol (enum_pdi, cu);
6288
6289 pdi = enum_pdi->die_child;
6290 while (pdi)
6291 {
6292 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
6293 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
6294 else
6295 add_partial_symbol (pdi, cu);
6296 pdi = pdi->die_sibling;
6297 }
6298 }
6299
6300 /* Return the initial uleb128 in the die at INFO_PTR. */
6301
6302 static unsigned int
6303 peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
6304 {
6305 unsigned int bytes_read;
6306
6307 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6308 }
6309
6310 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6311 Return the corresponding abbrev, or NULL if the number is zero (indicating
6312 an empty DIE). In either case *BYTES_READ will be set to the length of
6313 the initial number. */
6314
6315 static struct abbrev_info *
6316 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
6317 struct dwarf2_cu *cu)
6318 {
6319 bfd *abfd = cu->objfile->obfd;
6320 unsigned int abbrev_number;
6321 struct abbrev_info *abbrev;
6322
6323 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6324
6325 if (abbrev_number == 0)
6326 return NULL;
6327
6328 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
6329 if (!abbrev)
6330 {
6331 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6332 abbrev_number, bfd_get_filename (abfd));
6333 }
6334
6335 return abbrev;
6336 }
6337
6338 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6339 Returns a pointer to the end of a series of DIEs, terminated by an empty
6340 DIE. Any children of the skipped DIEs will also be skipped. */
6341
6342 static gdb_byte *
6343 skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
6344 {
6345 struct dwarf2_cu *cu = reader->cu;
6346 struct abbrev_info *abbrev;
6347 unsigned int bytes_read;
6348
6349 while (1)
6350 {
6351 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6352 if (abbrev == NULL)
6353 return info_ptr + bytes_read;
6354 else
6355 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
6356 }
6357 }
6358
6359 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6360 INFO_PTR should point just after the initial uleb128 of a DIE, and the
6361 abbrev corresponding to that skipped uleb128 should be passed in
6362 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6363 children. */
6364
6365 static gdb_byte *
6366 skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
6367 struct abbrev_info *abbrev)
6368 {
6369 unsigned int bytes_read;
6370 struct attribute attr;
6371 bfd *abfd = reader->abfd;
6372 struct dwarf2_cu *cu = reader->cu;
6373 gdb_byte *buffer = reader->buffer;
6374 const gdb_byte *buffer_end = reader->buffer_end;
6375 gdb_byte *start_info_ptr = info_ptr;
6376 unsigned int form, i;
6377
6378 for (i = 0; i < abbrev->num_attrs; i++)
6379 {
6380 /* The only abbrev we care about is DW_AT_sibling. */
6381 if (abbrev->attrs[i].name == DW_AT_sibling)
6382 {
6383 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
6384 if (attr.form == DW_FORM_ref_addr)
6385 complaint (&symfile_complaints,
6386 _("ignoring absolute DW_AT_sibling"));
6387 else
6388 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
6389 }
6390
6391 /* If it isn't DW_AT_sibling, skip this attribute. */
6392 form = abbrev->attrs[i].form;
6393 skip_attribute:
6394 switch (form)
6395 {
6396 case DW_FORM_ref_addr:
6397 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6398 and later it is offset sized. */
6399 if (cu->header.version == 2)
6400 info_ptr += cu->header.addr_size;
6401 else
6402 info_ptr += cu->header.offset_size;
6403 break;
6404 case DW_FORM_GNU_ref_alt:
6405 info_ptr += cu->header.offset_size;
6406 break;
6407 case DW_FORM_addr:
6408 info_ptr += cu->header.addr_size;
6409 break;
6410 case DW_FORM_data1:
6411 case DW_FORM_ref1:
6412 case DW_FORM_flag:
6413 info_ptr += 1;
6414 break;
6415 case DW_FORM_flag_present:
6416 break;
6417 case DW_FORM_data2:
6418 case DW_FORM_ref2:
6419 info_ptr += 2;
6420 break;
6421 case DW_FORM_data4:
6422 case DW_FORM_ref4:
6423 info_ptr += 4;
6424 break;
6425 case DW_FORM_data8:
6426 case DW_FORM_ref8:
6427 case DW_FORM_ref_sig8:
6428 info_ptr += 8;
6429 break;
6430 case DW_FORM_string:
6431 read_direct_string (abfd, info_ptr, &bytes_read);
6432 info_ptr += bytes_read;
6433 break;
6434 case DW_FORM_sec_offset:
6435 case DW_FORM_strp:
6436 case DW_FORM_GNU_strp_alt:
6437 info_ptr += cu->header.offset_size;
6438 break;
6439 case DW_FORM_exprloc:
6440 case DW_FORM_block:
6441 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6442 info_ptr += bytes_read;
6443 break;
6444 case DW_FORM_block1:
6445 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6446 break;
6447 case DW_FORM_block2:
6448 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6449 break;
6450 case DW_FORM_block4:
6451 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6452 break;
6453 case DW_FORM_sdata:
6454 case DW_FORM_udata:
6455 case DW_FORM_ref_udata:
6456 case DW_FORM_GNU_addr_index:
6457 case DW_FORM_GNU_str_index:
6458 info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
6459 break;
6460 case DW_FORM_indirect:
6461 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6462 info_ptr += bytes_read;
6463 /* We need to continue parsing from here, so just go back to
6464 the top. */
6465 goto skip_attribute;
6466
6467 default:
6468 error (_("Dwarf Error: Cannot handle %s "
6469 "in DWARF reader [in module %s]"),
6470 dwarf_form_name (form),
6471 bfd_get_filename (abfd));
6472 }
6473 }
6474
6475 if (abbrev->has_children)
6476 return skip_children (reader, info_ptr);
6477 else
6478 return info_ptr;
6479 }
6480
6481 /* Locate ORIG_PDI's sibling.
6482 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
6483
6484 static gdb_byte *
6485 locate_pdi_sibling (const struct die_reader_specs *reader,
6486 struct partial_die_info *orig_pdi,
6487 gdb_byte *info_ptr)
6488 {
6489 /* Do we know the sibling already? */
6490
6491 if (orig_pdi->sibling)
6492 return orig_pdi->sibling;
6493
6494 /* Are there any children to deal with? */
6495
6496 if (!orig_pdi->has_children)
6497 return info_ptr;
6498
6499 /* Skip the children the long way. */
6500
6501 return skip_children (reader, info_ptr);
6502 }
6503
6504 /* Expand this partial symbol table into a full symbol table. SELF is
6505 not NULL. */
6506
6507 static void
6508 dwarf2_read_symtab (struct partial_symtab *self,
6509 struct objfile *objfile)
6510 {
6511 if (self->readin)
6512 {
6513 warning (_("bug: psymtab for %s is already read in."),
6514 self->filename);
6515 }
6516 else
6517 {
6518 if (info_verbose)
6519 {
6520 printf_filtered (_("Reading in symbols for %s..."),
6521 self->filename);
6522 gdb_flush (gdb_stdout);
6523 }
6524
6525 /* Restore our global data. */
6526 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
6527
6528 /* If this psymtab is constructed from a debug-only objfile, the
6529 has_section_at_zero flag will not necessarily be correct. We
6530 can get the correct value for this flag by looking at the data
6531 associated with the (presumably stripped) associated objfile. */
6532 if (objfile->separate_debug_objfile_backlink)
6533 {
6534 struct dwarf2_per_objfile *dpo_backlink
6535 = objfile_data (objfile->separate_debug_objfile_backlink,
6536 dwarf2_objfile_data_key);
6537
6538 dwarf2_per_objfile->has_section_at_zero
6539 = dpo_backlink->has_section_at_zero;
6540 }
6541
6542 dwarf2_per_objfile->reading_partial_symbols = 0;
6543
6544 psymtab_to_symtab_1 (self);
6545
6546 /* Finish up the debug error message. */
6547 if (info_verbose)
6548 printf_filtered (_("done.\n"));
6549 }
6550
6551 process_cu_includes ();
6552 }
6553 \f
6554 /* Reading in full CUs. */
6555
6556 /* Add PER_CU to the queue. */
6557
6558 static void
6559 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6560 enum language pretend_language)
6561 {
6562 struct dwarf2_queue_item *item;
6563
6564 per_cu->queued = 1;
6565 item = xmalloc (sizeof (*item));
6566 item->per_cu = per_cu;
6567 item->pretend_language = pretend_language;
6568 item->next = NULL;
6569
6570 if (dwarf2_queue == NULL)
6571 dwarf2_queue = item;
6572 else
6573 dwarf2_queue_tail->next = item;
6574
6575 dwarf2_queue_tail = item;
6576 }
6577
6578 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
6579 unit and add it to our queue.
6580 The result is non-zero if PER_CU was queued, otherwise the result is zero
6581 meaning either PER_CU is already queued or it is already loaded. */
6582
6583 static int
6584 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6585 struct dwarf2_per_cu_data *per_cu,
6586 enum language pretend_language)
6587 {
6588 /* We may arrive here during partial symbol reading, if we need full
6589 DIEs to process an unusual case (e.g. template arguments). Do
6590 not queue PER_CU, just tell our caller to load its DIEs. */
6591 if (dwarf2_per_objfile->reading_partial_symbols)
6592 {
6593 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6594 return 1;
6595 return 0;
6596 }
6597
6598 /* Mark the dependence relation so that we don't flush PER_CU
6599 too early. */
6600 dwarf2_add_dependence (this_cu, per_cu);
6601
6602 /* If it's already on the queue, we have nothing to do. */
6603 if (per_cu->queued)
6604 return 0;
6605
6606 /* If the compilation unit is already loaded, just mark it as
6607 used. */
6608 if (per_cu->cu != NULL)
6609 {
6610 per_cu->cu->last_used = 0;
6611 return 0;
6612 }
6613
6614 /* Add it to the queue. */
6615 queue_comp_unit (per_cu, pretend_language);
6616
6617 return 1;
6618 }
6619
6620 /* Process the queue. */
6621
6622 static void
6623 process_queue (void)
6624 {
6625 struct dwarf2_queue_item *item, *next_item;
6626
6627 if (dwarf2_read_debug)
6628 {
6629 fprintf_unfiltered (gdb_stdlog,
6630 "Expanding one or more symtabs of objfile %s ...\n",
6631 dwarf2_per_objfile->objfile->name);
6632 }
6633
6634 /* The queue starts out with one item, but following a DIE reference
6635 may load a new CU, adding it to the end of the queue. */
6636 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
6637 {
6638 if (dwarf2_per_objfile->using_index
6639 ? !item->per_cu->v.quick->symtab
6640 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
6641 {
6642 struct dwarf2_per_cu_data *per_cu = item->per_cu;
6643
6644 if (dwarf2_read_debug)
6645 {
6646 fprintf_unfiltered (gdb_stdlog,
6647 "Expanding symtab of %s at offset 0x%x\n",
6648 per_cu->is_debug_types ? "TU" : "CU",
6649 per_cu->offset.sect_off);
6650 }
6651
6652 if (per_cu->is_debug_types)
6653 process_full_type_unit (per_cu, item->pretend_language);
6654 else
6655 process_full_comp_unit (per_cu, item->pretend_language);
6656
6657 if (dwarf2_read_debug)
6658 {
6659 fprintf_unfiltered (gdb_stdlog,
6660 "Done expanding %s at offset 0x%x\n",
6661 per_cu->is_debug_types ? "TU" : "CU",
6662 per_cu->offset.sect_off);
6663 }
6664 }
6665
6666 item->per_cu->queued = 0;
6667 next_item = item->next;
6668 xfree (item);
6669 }
6670
6671 dwarf2_queue_tail = NULL;
6672
6673 if (dwarf2_read_debug)
6674 {
6675 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
6676 dwarf2_per_objfile->objfile->name);
6677 }
6678 }
6679
6680 /* Free all allocated queue entries. This function only releases anything if
6681 an error was thrown; if the queue was processed then it would have been
6682 freed as we went along. */
6683
6684 static void
6685 dwarf2_release_queue (void *dummy)
6686 {
6687 struct dwarf2_queue_item *item, *last;
6688
6689 item = dwarf2_queue;
6690 while (item)
6691 {
6692 /* Anything still marked queued is likely to be in an
6693 inconsistent state, so discard it. */
6694 if (item->per_cu->queued)
6695 {
6696 if (item->per_cu->cu != NULL)
6697 free_one_cached_comp_unit (item->per_cu);
6698 item->per_cu->queued = 0;
6699 }
6700
6701 last = item;
6702 item = item->next;
6703 xfree (last);
6704 }
6705
6706 dwarf2_queue = dwarf2_queue_tail = NULL;
6707 }
6708
6709 /* Read in full symbols for PST, and anything it depends on. */
6710
6711 static void
6712 psymtab_to_symtab_1 (struct partial_symtab *pst)
6713 {
6714 struct dwarf2_per_cu_data *per_cu;
6715 int i;
6716
6717 if (pst->readin)
6718 return;
6719
6720 for (i = 0; i < pst->number_of_dependencies; i++)
6721 if (!pst->dependencies[i]->readin
6722 && pst->dependencies[i]->user == NULL)
6723 {
6724 /* Inform about additional files that need to be read in. */
6725 if (info_verbose)
6726 {
6727 /* FIXME: i18n: Need to make this a single string. */
6728 fputs_filtered (" ", gdb_stdout);
6729 wrap_here ("");
6730 fputs_filtered ("and ", gdb_stdout);
6731 wrap_here ("");
6732 printf_filtered ("%s...", pst->dependencies[i]->filename);
6733 wrap_here (""); /* Flush output. */
6734 gdb_flush (gdb_stdout);
6735 }
6736 psymtab_to_symtab_1 (pst->dependencies[i]);
6737 }
6738
6739 per_cu = pst->read_symtab_private;
6740
6741 if (per_cu == NULL)
6742 {
6743 /* It's an include file, no symbols to read for it.
6744 Everything is in the parent symtab. */
6745 pst->readin = 1;
6746 return;
6747 }
6748
6749 dw2_do_instantiate_symtab (per_cu);
6750 }
6751
6752 /* Trivial hash function for die_info: the hash value of a DIE
6753 is its offset in .debug_info for this objfile. */
6754
6755 static hashval_t
6756 die_hash (const void *item)
6757 {
6758 const struct die_info *die = item;
6759
6760 return die->offset.sect_off;
6761 }
6762
6763 /* Trivial comparison function for die_info structures: two DIEs
6764 are equal if they have the same offset. */
6765
6766 static int
6767 die_eq (const void *item_lhs, const void *item_rhs)
6768 {
6769 const struct die_info *die_lhs = item_lhs;
6770 const struct die_info *die_rhs = item_rhs;
6771
6772 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
6773 }
6774
6775 /* die_reader_func for load_full_comp_unit.
6776 This is identical to read_signatured_type_reader,
6777 but is kept separate for now. */
6778
6779 static void
6780 load_full_comp_unit_reader (const struct die_reader_specs *reader,
6781 gdb_byte *info_ptr,
6782 struct die_info *comp_unit_die,
6783 int has_children,
6784 void *data)
6785 {
6786 struct dwarf2_cu *cu = reader->cu;
6787 enum language *language_ptr = data;
6788
6789 gdb_assert (cu->die_hash == NULL);
6790 cu->die_hash =
6791 htab_create_alloc_ex (cu->header.length / 12,
6792 die_hash,
6793 die_eq,
6794 NULL,
6795 &cu->comp_unit_obstack,
6796 hashtab_obstack_allocate,
6797 dummy_obstack_deallocate);
6798
6799 if (has_children)
6800 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
6801 &info_ptr, comp_unit_die);
6802 cu->dies = comp_unit_die;
6803 /* comp_unit_die is not stored in die_hash, no need. */
6804
6805 /* We try not to read any attributes in this function, because not
6806 all CUs needed for references have been loaded yet, and symbol
6807 table processing isn't initialized. But we have to set the CU language,
6808 or we won't be able to build types correctly.
6809 Similarly, if we do not read the producer, we can not apply
6810 producer-specific interpretation. */
6811 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
6812 }
6813
6814 /* Load the DIEs associated with PER_CU into memory. */
6815
6816 static void
6817 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
6818 enum language pretend_language)
6819 {
6820 gdb_assert (! this_cu->is_debug_types);
6821
6822 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6823 load_full_comp_unit_reader, &pretend_language);
6824 }
6825
6826 /* Add a DIE to the delayed physname list. */
6827
6828 static void
6829 add_to_method_list (struct type *type, int fnfield_index, int index,
6830 const char *name, struct die_info *die,
6831 struct dwarf2_cu *cu)
6832 {
6833 struct delayed_method_info mi;
6834 mi.type = type;
6835 mi.fnfield_index = fnfield_index;
6836 mi.index = index;
6837 mi.name = name;
6838 mi.die = die;
6839 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
6840 }
6841
6842 /* A cleanup for freeing the delayed method list. */
6843
6844 static void
6845 free_delayed_list (void *ptr)
6846 {
6847 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
6848 if (cu->method_list != NULL)
6849 {
6850 VEC_free (delayed_method_info, cu->method_list);
6851 cu->method_list = NULL;
6852 }
6853 }
6854
6855 /* Compute the physnames of any methods on the CU's method list.
6856
6857 The computation of method physnames is delayed in order to avoid the
6858 (bad) condition that one of the method's formal parameters is of an as yet
6859 incomplete type. */
6860
6861 static void
6862 compute_delayed_physnames (struct dwarf2_cu *cu)
6863 {
6864 int i;
6865 struct delayed_method_info *mi;
6866 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
6867 {
6868 const char *physname;
6869 struct fn_fieldlist *fn_flp
6870 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
6871 physname = dwarf2_physname (mi->name, mi->die, cu);
6872 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
6873 }
6874 }
6875
6876 /* Go objects should be embedded in a DW_TAG_module DIE,
6877 and it's not clear if/how imported objects will appear.
6878 To keep Go support simple until that's worked out,
6879 go back through what we've read and create something usable.
6880 We could do this while processing each DIE, and feels kinda cleaner,
6881 but that way is more invasive.
6882 This is to, for example, allow the user to type "p var" or "b main"
6883 without having to specify the package name, and allow lookups
6884 of module.object to work in contexts that use the expression
6885 parser. */
6886
6887 static void
6888 fixup_go_packaging (struct dwarf2_cu *cu)
6889 {
6890 char *package_name = NULL;
6891 struct pending *list;
6892 int i;
6893
6894 for (list = global_symbols; list != NULL; list = list->next)
6895 {
6896 for (i = 0; i < list->nsyms; ++i)
6897 {
6898 struct symbol *sym = list->symbol[i];
6899
6900 if (SYMBOL_LANGUAGE (sym) == language_go
6901 && SYMBOL_CLASS (sym) == LOC_BLOCK)
6902 {
6903 char *this_package_name = go_symbol_package_name (sym);
6904
6905 if (this_package_name == NULL)
6906 continue;
6907 if (package_name == NULL)
6908 package_name = this_package_name;
6909 else
6910 {
6911 if (strcmp (package_name, this_package_name) != 0)
6912 complaint (&symfile_complaints,
6913 _("Symtab %s has objects from two different Go packages: %s and %s"),
6914 (SYMBOL_SYMTAB (sym)
6915 ? symtab_to_filename_for_display (SYMBOL_SYMTAB (sym))
6916 : cu->objfile->name),
6917 this_package_name, package_name);
6918 xfree (this_package_name);
6919 }
6920 }
6921 }
6922 }
6923
6924 if (package_name != NULL)
6925 {
6926 struct objfile *objfile = cu->objfile;
6927 const char *saved_package_name = obstack_copy0 (&objfile->objfile_obstack,
6928 package_name,
6929 strlen (package_name));
6930 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
6931 saved_package_name, objfile);
6932 struct symbol *sym;
6933
6934 TYPE_TAG_NAME (type) = TYPE_NAME (type);
6935
6936 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
6937 SYMBOL_SET_LANGUAGE (sym, language_go);
6938 SYMBOL_SET_NAMES (sym, saved_package_name,
6939 strlen (saved_package_name), 0, objfile);
6940 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
6941 e.g., "main" finds the "main" module and not C's main(). */
6942 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
6943 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
6944 SYMBOL_TYPE (sym) = type;
6945
6946 add_symbol_to_list (sym, &global_symbols);
6947
6948 xfree (package_name);
6949 }
6950 }
6951
6952 /* Return the symtab for PER_CU. This works properly regardless of
6953 whether we're using the index or psymtabs. */
6954
6955 static struct symtab *
6956 get_symtab (struct dwarf2_per_cu_data *per_cu)
6957 {
6958 return (dwarf2_per_objfile->using_index
6959 ? per_cu->v.quick->symtab
6960 : per_cu->v.psymtab->symtab);
6961 }
6962
6963 /* A helper function for computing the list of all symbol tables
6964 included by PER_CU. */
6965
6966 static void
6967 recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
6968 htab_t all_children,
6969 struct dwarf2_per_cu_data *per_cu)
6970 {
6971 void **slot;
6972 int ix;
6973 struct dwarf2_per_cu_data *iter;
6974
6975 slot = htab_find_slot (all_children, per_cu, INSERT);
6976 if (*slot != NULL)
6977 {
6978 /* This inclusion and its children have been processed. */
6979 return;
6980 }
6981
6982 *slot = per_cu;
6983 /* Only add a CU if it has a symbol table. */
6984 if (get_symtab (per_cu) != NULL)
6985 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
6986
6987 for (ix = 0;
6988 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
6989 ++ix)
6990 recursively_compute_inclusions (result, all_children, iter);
6991 }
6992
6993 /* Compute the symtab 'includes' fields for the symtab related to
6994 PER_CU. */
6995
6996 static void
6997 compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
6998 {
6999 gdb_assert (! per_cu->is_debug_types);
7000
7001 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
7002 {
7003 int ix, len;
7004 struct dwarf2_per_cu_data *iter;
7005 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
7006 htab_t all_children;
7007 struct symtab *symtab = get_symtab (per_cu);
7008
7009 /* If we don't have a symtab, we can just skip this case. */
7010 if (symtab == NULL)
7011 return;
7012
7013 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
7014 NULL, xcalloc, xfree);
7015
7016 for (ix = 0;
7017 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
7018 ix, iter);
7019 ++ix)
7020 recursively_compute_inclusions (&result_children, all_children, iter);
7021
7022 /* Now we have a transitive closure of all the included CUs, and
7023 for .gdb_index version 7 the included TUs, so we can convert it
7024 to a list of symtabs. */
7025 len = VEC_length (dwarf2_per_cu_ptr, result_children);
7026 symtab->includes
7027 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
7028 (len + 1) * sizeof (struct symtab *));
7029 for (ix = 0;
7030 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
7031 ++ix)
7032 symtab->includes[ix] = get_symtab (iter);
7033 symtab->includes[len] = NULL;
7034
7035 VEC_free (dwarf2_per_cu_ptr, result_children);
7036 htab_delete (all_children);
7037 }
7038 }
7039
7040 /* Compute the 'includes' field for the symtabs of all the CUs we just
7041 read. */
7042
7043 static void
7044 process_cu_includes (void)
7045 {
7046 int ix;
7047 struct dwarf2_per_cu_data *iter;
7048
7049 for (ix = 0;
7050 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
7051 ix, iter);
7052 ++ix)
7053 {
7054 if (! iter->is_debug_types)
7055 compute_symtab_includes (iter);
7056 }
7057
7058 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
7059 }
7060
7061 /* Generate full symbol information for PER_CU, whose DIEs have
7062 already been loaded into memory. */
7063
7064 static void
7065 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
7066 enum language pretend_language)
7067 {
7068 struct dwarf2_cu *cu = per_cu->cu;
7069 struct objfile *objfile = per_cu->objfile;
7070 CORE_ADDR lowpc, highpc;
7071 struct symtab *symtab;
7072 struct cleanup *back_to, *delayed_list_cleanup;
7073 CORE_ADDR baseaddr;
7074 struct block *static_block;
7075
7076 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7077
7078 buildsym_init ();
7079 back_to = make_cleanup (really_free_pendings, NULL);
7080 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7081
7082 cu->list_in_scope = &file_symbols;
7083
7084 cu->language = pretend_language;
7085 cu->language_defn = language_def (cu->language);
7086
7087 /* Do line number decoding in read_file_scope () */
7088 process_die (cu->dies, cu);
7089
7090 /* For now fudge the Go package. */
7091 if (cu->language == language_go)
7092 fixup_go_packaging (cu);
7093
7094 /* Now that we have processed all the DIEs in the CU, all the types
7095 should be complete, and it should now be safe to compute all of the
7096 physnames. */
7097 compute_delayed_physnames (cu);
7098 do_cleanups (delayed_list_cleanup);
7099
7100 /* Some compilers don't define a DW_AT_high_pc attribute for the
7101 compilation unit. If the DW_AT_high_pc is missing, synthesize
7102 it, by scanning the DIE's below the compilation unit. */
7103 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
7104
7105 static_block
7106 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0,
7107 per_cu->imported_symtabs != NULL);
7108
7109 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
7110 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
7111 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
7112 addrmap to help ensure it has an accurate map of pc values belonging to
7113 this comp unit. */
7114 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
7115
7116 symtab = end_symtab_from_static_block (static_block, objfile,
7117 SECT_OFF_TEXT (objfile), 0);
7118
7119 if (symtab != NULL)
7120 {
7121 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
7122
7123 /* Set symtab language to language from DW_AT_language. If the
7124 compilation is from a C file generated by language preprocessors, do
7125 not set the language if it was already deduced by start_subfile. */
7126 if (!(cu->language == language_c && symtab->language != language_c))
7127 symtab->language = cu->language;
7128
7129 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
7130 produce DW_AT_location with location lists but it can be possibly
7131 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
7132 there were bugs in prologue debug info, fixed later in GCC-4.5
7133 by "unwind info for epilogues" patch (which is not directly related).
7134
7135 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
7136 needed, it would be wrong due to missing DW_AT_producer there.
7137
7138 Still one can confuse GDB by using non-standard GCC compilation
7139 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
7140 */
7141 if (cu->has_loclist && gcc_4_minor >= 5)
7142 symtab->locations_valid = 1;
7143
7144 if (gcc_4_minor >= 5)
7145 symtab->epilogue_unwind_valid = 1;
7146
7147 symtab->call_site_htab = cu->call_site_htab;
7148 }
7149
7150 if (dwarf2_per_objfile->using_index)
7151 per_cu->v.quick->symtab = symtab;
7152 else
7153 {
7154 struct partial_symtab *pst = per_cu->v.psymtab;
7155 pst->symtab = symtab;
7156 pst->readin = 1;
7157 }
7158
7159 /* Push it for inclusion processing later. */
7160 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
7161
7162 do_cleanups (back_to);
7163 }
7164
7165 /* Generate full symbol information for type unit PER_CU, whose DIEs have
7166 already been loaded into memory. */
7167
7168 static void
7169 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
7170 enum language pretend_language)
7171 {
7172 struct dwarf2_cu *cu = per_cu->cu;
7173 struct objfile *objfile = per_cu->objfile;
7174 struct symtab *symtab;
7175 struct cleanup *back_to, *delayed_list_cleanup;
7176 struct signatured_type *sig_type;
7177
7178 gdb_assert (per_cu->is_debug_types);
7179 sig_type = (struct signatured_type *) per_cu;
7180
7181 buildsym_init ();
7182 back_to = make_cleanup (really_free_pendings, NULL);
7183 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7184
7185 cu->list_in_scope = &file_symbols;
7186
7187 cu->language = pretend_language;
7188 cu->language_defn = language_def (cu->language);
7189
7190 /* The symbol tables are set up in read_type_unit_scope. */
7191 process_die (cu->dies, cu);
7192
7193 /* For now fudge the Go package. */
7194 if (cu->language == language_go)
7195 fixup_go_packaging (cu);
7196
7197 /* Now that we have processed all the DIEs in the CU, all the types
7198 should be complete, and it should now be safe to compute all of the
7199 physnames. */
7200 compute_delayed_physnames (cu);
7201 do_cleanups (delayed_list_cleanup);
7202
7203 /* TUs share symbol tables.
7204 If this is the first TU to use this symtab, complete the construction
7205 of it with end_expandable_symtab. Otherwise, complete the addition of
7206 this TU's symbols to the existing symtab. */
7207 if (sig_type->type_unit_group->primary_symtab == NULL)
7208 {
7209 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
7210 sig_type->type_unit_group->primary_symtab = symtab;
7211
7212 if (symtab != NULL)
7213 {
7214 /* Set symtab language to language from DW_AT_language. If the
7215 compilation is from a C file generated by language preprocessors,
7216 do not set the language if it was already deduced by
7217 start_subfile. */
7218 if (!(cu->language == language_c && symtab->language != language_c))
7219 symtab->language = cu->language;
7220 }
7221 }
7222 else
7223 {
7224 augment_type_symtab (objfile,
7225 sig_type->type_unit_group->primary_symtab);
7226 symtab = sig_type->type_unit_group->primary_symtab;
7227 }
7228
7229 if (dwarf2_per_objfile->using_index)
7230 per_cu->v.quick->symtab = symtab;
7231 else
7232 {
7233 struct partial_symtab *pst = per_cu->v.psymtab;
7234 pst->symtab = symtab;
7235 pst->readin = 1;
7236 }
7237
7238 do_cleanups (back_to);
7239 }
7240
7241 /* Process an imported unit DIE. */
7242
7243 static void
7244 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7245 {
7246 struct attribute *attr;
7247
7248 /* For now we don't handle imported units in type units. */
7249 if (cu->per_cu->is_debug_types)
7250 {
7251 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7252 " supported in type units [in module %s]"),
7253 cu->objfile->name);
7254 }
7255
7256 attr = dwarf2_attr (die, DW_AT_import, cu);
7257 if (attr != NULL)
7258 {
7259 struct dwarf2_per_cu_data *per_cu;
7260 struct symtab *imported_symtab;
7261 sect_offset offset;
7262 int is_dwz;
7263
7264 offset = dwarf2_get_ref_die_offset (attr);
7265 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7266 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
7267
7268 /* Queue the unit, if needed. */
7269 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7270 load_full_comp_unit (per_cu, cu->language);
7271
7272 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
7273 per_cu);
7274 }
7275 }
7276
7277 /* Process a die and its children. */
7278
7279 static void
7280 process_die (struct die_info *die, struct dwarf2_cu *cu)
7281 {
7282 switch (die->tag)
7283 {
7284 case DW_TAG_padding:
7285 break;
7286 case DW_TAG_compile_unit:
7287 case DW_TAG_partial_unit:
7288 read_file_scope (die, cu);
7289 break;
7290 case DW_TAG_type_unit:
7291 read_type_unit_scope (die, cu);
7292 break;
7293 case DW_TAG_subprogram:
7294 case DW_TAG_inlined_subroutine:
7295 read_func_scope (die, cu);
7296 break;
7297 case DW_TAG_lexical_block:
7298 case DW_TAG_try_block:
7299 case DW_TAG_catch_block:
7300 read_lexical_block_scope (die, cu);
7301 break;
7302 case DW_TAG_GNU_call_site:
7303 read_call_site_scope (die, cu);
7304 break;
7305 case DW_TAG_class_type:
7306 case DW_TAG_interface_type:
7307 case DW_TAG_structure_type:
7308 case DW_TAG_union_type:
7309 process_structure_scope (die, cu);
7310 break;
7311 case DW_TAG_enumeration_type:
7312 process_enumeration_scope (die, cu);
7313 break;
7314
7315 /* These dies have a type, but processing them does not create
7316 a symbol or recurse to process the children. Therefore we can
7317 read them on-demand through read_type_die. */
7318 case DW_TAG_subroutine_type:
7319 case DW_TAG_set_type:
7320 case DW_TAG_array_type:
7321 case DW_TAG_pointer_type:
7322 case DW_TAG_ptr_to_member_type:
7323 case DW_TAG_reference_type:
7324 case DW_TAG_string_type:
7325 break;
7326
7327 case DW_TAG_base_type:
7328 case DW_TAG_subrange_type:
7329 case DW_TAG_typedef:
7330 /* Add a typedef symbol for the type definition, if it has a
7331 DW_AT_name. */
7332 new_symbol (die, read_type_die (die, cu), cu);
7333 break;
7334 case DW_TAG_common_block:
7335 read_common_block (die, cu);
7336 break;
7337 case DW_TAG_common_inclusion:
7338 break;
7339 case DW_TAG_namespace:
7340 cu->processing_has_namespace_info = 1;
7341 read_namespace (die, cu);
7342 break;
7343 case DW_TAG_module:
7344 cu->processing_has_namespace_info = 1;
7345 read_module (die, cu);
7346 break;
7347 case DW_TAG_imported_declaration:
7348 case DW_TAG_imported_module:
7349 cu->processing_has_namespace_info = 1;
7350 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7351 || cu->language != language_fortran))
7352 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7353 dwarf_tag_name (die->tag));
7354 read_import_statement (die, cu);
7355 break;
7356
7357 case DW_TAG_imported_unit:
7358 process_imported_unit_die (die, cu);
7359 break;
7360
7361 default:
7362 new_symbol (die, NULL, cu);
7363 break;
7364 }
7365 }
7366 \f
7367 /* DWARF name computation. */
7368
7369 /* A helper function for dwarf2_compute_name which determines whether DIE
7370 needs to have the name of the scope prepended to the name listed in the
7371 die. */
7372
7373 static int
7374 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7375 {
7376 struct attribute *attr;
7377
7378 switch (die->tag)
7379 {
7380 case DW_TAG_namespace:
7381 case DW_TAG_typedef:
7382 case DW_TAG_class_type:
7383 case DW_TAG_interface_type:
7384 case DW_TAG_structure_type:
7385 case DW_TAG_union_type:
7386 case DW_TAG_enumeration_type:
7387 case DW_TAG_enumerator:
7388 case DW_TAG_subprogram:
7389 case DW_TAG_member:
7390 return 1;
7391
7392 case DW_TAG_variable:
7393 case DW_TAG_constant:
7394 /* We only need to prefix "globally" visible variables. These include
7395 any variable marked with DW_AT_external or any variable that
7396 lives in a namespace. [Variables in anonymous namespaces
7397 require prefixing, but they are not DW_AT_external.] */
7398
7399 if (dwarf2_attr (die, DW_AT_specification, cu))
7400 {
7401 struct dwarf2_cu *spec_cu = cu;
7402
7403 return die_needs_namespace (die_specification (die, &spec_cu),
7404 spec_cu);
7405 }
7406
7407 attr = dwarf2_attr (die, DW_AT_external, cu);
7408 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7409 && die->parent->tag != DW_TAG_module)
7410 return 0;
7411 /* A variable in a lexical block of some kind does not need a
7412 namespace, even though in C++ such variables may be external
7413 and have a mangled name. */
7414 if (die->parent->tag == DW_TAG_lexical_block
7415 || die->parent->tag == DW_TAG_try_block
7416 || die->parent->tag == DW_TAG_catch_block
7417 || die->parent->tag == DW_TAG_subprogram)
7418 return 0;
7419 return 1;
7420
7421 default:
7422 return 0;
7423 }
7424 }
7425
7426 /* Retrieve the last character from a mem_file. */
7427
7428 static void
7429 do_ui_file_peek_last (void *object, const char *buffer, long length)
7430 {
7431 char *last_char_p = (char *) object;
7432
7433 if (length > 0)
7434 *last_char_p = buffer[length - 1];
7435 }
7436
7437 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
7438 compute the physname for the object, which include a method's:
7439 - formal parameters (C++/Java),
7440 - receiver type (Go),
7441 - return type (Java).
7442
7443 The term "physname" is a bit confusing.
7444 For C++, for example, it is the demangled name.
7445 For Go, for example, it's the mangled name.
7446
7447 For Ada, return the DIE's linkage name rather than the fully qualified
7448 name. PHYSNAME is ignored..
7449
7450 The result is allocated on the objfile_obstack and canonicalized. */
7451
7452 static const char *
7453 dwarf2_compute_name (const char *name,
7454 struct die_info *die, struct dwarf2_cu *cu,
7455 int physname)
7456 {
7457 struct objfile *objfile = cu->objfile;
7458
7459 if (name == NULL)
7460 name = dwarf2_name (die, cu);
7461
7462 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7463 compute it by typename_concat inside GDB. */
7464 if (cu->language == language_ada
7465 || (cu->language == language_fortran && physname))
7466 {
7467 /* For Ada unit, we prefer the linkage name over the name, as
7468 the former contains the exported name, which the user expects
7469 to be able to reference. Ideally, we want the user to be able
7470 to reference this entity using either natural or linkage name,
7471 but we haven't started looking at this enhancement yet. */
7472 struct attribute *attr;
7473
7474 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7475 if (attr == NULL)
7476 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7477 if (attr && DW_STRING (attr))
7478 return DW_STRING (attr);
7479 }
7480
7481 /* These are the only languages we know how to qualify names in. */
7482 if (name != NULL
7483 && (cu->language == language_cplus || cu->language == language_java
7484 || cu->language == language_fortran))
7485 {
7486 if (die_needs_namespace (die, cu))
7487 {
7488 long length;
7489 const char *prefix;
7490 struct ui_file *buf;
7491
7492 prefix = determine_prefix (die, cu);
7493 buf = mem_fileopen ();
7494 if (*prefix != '\0')
7495 {
7496 char *prefixed_name = typename_concat (NULL, prefix, name,
7497 physname, cu);
7498
7499 fputs_unfiltered (prefixed_name, buf);
7500 xfree (prefixed_name);
7501 }
7502 else
7503 fputs_unfiltered (name, buf);
7504
7505 /* Template parameters may be specified in the DIE's DW_AT_name, or
7506 as children with DW_TAG_template_type_param or
7507 DW_TAG_value_type_param. If the latter, add them to the name
7508 here. If the name already has template parameters, then
7509 skip this step; some versions of GCC emit both, and
7510 it is more efficient to use the pre-computed name.
7511
7512 Something to keep in mind about this process: it is very
7513 unlikely, or in some cases downright impossible, to produce
7514 something that will match the mangled name of a function.
7515 If the definition of the function has the same debug info,
7516 we should be able to match up with it anyway. But fallbacks
7517 using the minimal symbol, for instance to find a method
7518 implemented in a stripped copy of libstdc++, will not work.
7519 If we do not have debug info for the definition, we will have to
7520 match them up some other way.
7521
7522 When we do name matching there is a related problem with function
7523 templates; two instantiated function templates are allowed to
7524 differ only by their return types, which we do not add here. */
7525
7526 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7527 {
7528 struct attribute *attr;
7529 struct die_info *child;
7530 int first = 1;
7531
7532 die->building_fullname = 1;
7533
7534 for (child = die->child; child != NULL; child = child->sibling)
7535 {
7536 struct type *type;
7537 LONGEST value;
7538 gdb_byte *bytes;
7539 struct dwarf2_locexpr_baton *baton;
7540 struct value *v;
7541
7542 if (child->tag != DW_TAG_template_type_param
7543 && child->tag != DW_TAG_template_value_param)
7544 continue;
7545
7546 if (first)
7547 {
7548 fputs_unfiltered ("<", buf);
7549 first = 0;
7550 }
7551 else
7552 fputs_unfiltered (", ", buf);
7553
7554 attr = dwarf2_attr (child, DW_AT_type, cu);
7555 if (attr == NULL)
7556 {
7557 complaint (&symfile_complaints,
7558 _("template parameter missing DW_AT_type"));
7559 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7560 continue;
7561 }
7562 type = die_type (child, cu);
7563
7564 if (child->tag == DW_TAG_template_type_param)
7565 {
7566 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
7567 continue;
7568 }
7569
7570 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7571 if (attr == NULL)
7572 {
7573 complaint (&symfile_complaints,
7574 _("template parameter missing "
7575 "DW_AT_const_value"));
7576 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7577 continue;
7578 }
7579
7580 dwarf2_const_value_attr (attr, type, name,
7581 &cu->comp_unit_obstack, cu,
7582 &value, &bytes, &baton);
7583
7584 if (TYPE_NOSIGN (type))
7585 /* GDB prints characters as NUMBER 'CHAR'. If that's
7586 changed, this can use value_print instead. */
7587 c_printchar (value, type, buf);
7588 else
7589 {
7590 struct value_print_options opts;
7591
7592 if (baton != NULL)
7593 v = dwarf2_evaluate_loc_desc (type, NULL,
7594 baton->data,
7595 baton->size,
7596 baton->per_cu);
7597 else if (bytes != NULL)
7598 {
7599 v = allocate_value (type);
7600 memcpy (value_contents_writeable (v), bytes,
7601 TYPE_LENGTH (type));
7602 }
7603 else
7604 v = value_from_longest (type, value);
7605
7606 /* Specify decimal so that we do not depend on
7607 the radix. */
7608 get_formatted_print_options (&opts, 'd');
7609 opts.raw = 1;
7610 value_print (v, buf, &opts);
7611 release_value (v);
7612 value_free (v);
7613 }
7614 }
7615
7616 die->building_fullname = 0;
7617
7618 if (!first)
7619 {
7620 /* Close the argument list, with a space if necessary
7621 (nested templates). */
7622 char last_char = '\0';
7623 ui_file_put (buf, do_ui_file_peek_last, &last_char);
7624 if (last_char == '>')
7625 fputs_unfiltered (" >", buf);
7626 else
7627 fputs_unfiltered (">", buf);
7628 }
7629 }
7630
7631 /* For Java and C++ methods, append formal parameter type
7632 information, if PHYSNAME. */
7633
7634 if (physname && die->tag == DW_TAG_subprogram
7635 && (cu->language == language_cplus
7636 || cu->language == language_java))
7637 {
7638 struct type *type = read_type_die (die, cu);
7639
7640 c_type_print_args (type, buf, 1, cu->language,
7641 &type_print_raw_options);
7642
7643 if (cu->language == language_java)
7644 {
7645 /* For java, we must append the return type to method
7646 names. */
7647 if (die->tag == DW_TAG_subprogram)
7648 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
7649 0, 0, &type_print_raw_options);
7650 }
7651 else if (cu->language == language_cplus)
7652 {
7653 /* Assume that an artificial first parameter is
7654 "this", but do not crash if it is not. RealView
7655 marks unnamed (and thus unused) parameters as
7656 artificial; there is no way to differentiate
7657 the two cases. */
7658 if (TYPE_NFIELDS (type) > 0
7659 && TYPE_FIELD_ARTIFICIAL (type, 0)
7660 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
7661 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
7662 0))))
7663 fputs_unfiltered (" const", buf);
7664 }
7665 }
7666
7667 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
7668 &length);
7669 ui_file_delete (buf);
7670
7671 if (cu->language == language_cplus)
7672 {
7673 const char *cname
7674 = dwarf2_canonicalize_name (name, cu,
7675 &objfile->objfile_obstack);
7676
7677 if (cname != NULL)
7678 name = cname;
7679 }
7680 }
7681 }
7682
7683 return name;
7684 }
7685
7686 /* Return the fully qualified name of DIE, based on its DW_AT_name.
7687 If scope qualifiers are appropriate they will be added. The result
7688 will be allocated on the objfile_obstack, or NULL if the DIE does
7689 not have a name. NAME may either be from a previous call to
7690 dwarf2_name or NULL.
7691
7692 The output string will be canonicalized (if C++/Java). */
7693
7694 static const char *
7695 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
7696 {
7697 return dwarf2_compute_name (name, die, cu, 0);
7698 }
7699
7700 /* Construct a physname for the given DIE in CU. NAME may either be
7701 from a previous call to dwarf2_name or NULL. The result will be
7702 allocated on the objfile_objstack or NULL if the DIE does not have a
7703 name.
7704
7705 The output string will be canonicalized (if C++/Java). */
7706
7707 static const char *
7708 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
7709 {
7710 struct objfile *objfile = cu->objfile;
7711 struct attribute *attr;
7712 const char *retval, *mangled = NULL, *canon = NULL;
7713 struct cleanup *back_to;
7714 int need_copy = 1;
7715
7716 /* In this case dwarf2_compute_name is just a shortcut not building anything
7717 on its own. */
7718 if (!die_needs_namespace (die, cu))
7719 return dwarf2_compute_name (name, die, cu, 1);
7720
7721 back_to = make_cleanup (null_cleanup, NULL);
7722
7723 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7724 if (!attr)
7725 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7726
7727 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
7728 has computed. */
7729 if (attr && DW_STRING (attr))
7730 {
7731 char *demangled;
7732
7733 mangled = DW_STRING (attr);
7734
7735 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
7736 type. It is easier for GDB users to search for such functions as
7737 `name(params)' than `long name(params)'. In such case the minimal
7738 symbol names do not match the full symbol names but for template
7739 functions there is never a need to look up their definition from their
7740 declaration so the only disadvantage remains the minimal symbol
7741 variant `long name(params)' does not have the proper inferior type.
7742 */
7743
7744 if (cu->language == language_go)
7745 {
7746 /* This is a lie, but we already lie to the caller new_symbol_full.
7747 new_symbol_full assumes we return the mangled name.
7748 This just undoes that lie until things are cleaned up. */
7749 demangled = NULL;
7750 }
7751 else
7752 {
7753 demangled = cplus_demangle (mangled,
7754 (DMGL_PARAMS | DMGL_ANSI
7755 | (cu->language == language_java
7756 ? DMGL_JAVA | DMGL_RET_POSTFIX
7757 : DMGL_RET_DROP)));
7758 }
7759 if (demangled)
7760 {
7761 make_cleanup (xfree, demangled);
7762 canon = demangled;
7763 }
7764 else
7765 {
7766 canon = mangled;
7767 need_copy = 0;
7768 }
7769 }
7770
7771 if (canon == NULL || check_physname)
7772 {
7773 const char *physname = dwarf2_compute_name (name, die, cu, 1);
7774
7775 if (canon != NULL && strcmp (physname, canon) != 0)
7776 {
7777 /* It may not mean a bug in GDB. The compiler could also
7778 compute DW_AT_linkage_name incorrectly. But in such case
7779 GDB would need to be bug-to-bug compatible. */
7780
7781 complaint (&symfile_complaints,
7782 _("Computed physname <%s> does not match demangled <%s> "
7783 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
7784 physname, canon, mangled, die->offset.sect_off, objfile->name);
7785
7786 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
7787 is available here - over computed PHYSNAME. It is safer
7788 against both buggy GDB and buggy compilers. */
7789
7790 retval = canon;
7791 }
7792 else
7793 {
7794 retval = physname;
7795 need_copy = 0;
7796 }
7797 }
7798 else
7799 retval = canon;
7800
7801 if (need_copy)
7802 retval = obstack_copy0 (&objfile->objfile_obstack, retval, strlen (retval));
7803
7804 do_cleanups (back_to);
7805 return retval;
7806 }
7807
7808 /* Read the import statement specified by the given die and record it. */
7809
7810 static void
7811 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
7812 {
7813 struct objfile *objfile = cu->objfile;
7814 struct attribute *import_attr;
7815 struct die_info *imported_die, *child_die;
7816 struct dwarf2_cu *imported_cu;
7817 const char *imported_name;
7818 const char *imported_name_prefix;
7819 const char *canonical_name;
7820 const char *import_alias;
7821 const char *imported_declaration = NULL;
7822 const char *import_prefix;
7823 VEC (const_char_ptr) *excludes = NULL;
7824 struct cleanup *cleanups;
7825
7826 import_attr = dwarf2_attr (die, DW_AT_import, cu);
7827 if (import_attr == NULL)
7828 {
7829 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7830 dwarf_tag_name (die->tag));
7831 return;
7832 }
7833
7834 imported_cu = cu;
7835 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
7836 imported_name = dwarf2_name (imported_die, imported_cu);
7837 if (imported_name == NULL)
7838 {
7839 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
7840
7841 The import in the following code:
7842 namespace A
7843 {
7844 typedef int B;
7845 }
7846
7847 int main ()
7848 {
7849 using A::B;
7850 B b;
7851 return b;
7852 }
7853
7854 ...
7855 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
7856 <52> DW_AT_decl_file : 1
7857 <53> DW_AT_decl_line : 6
7858 <54> DW_AT_import : <0x75>
7859 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
7860 <59> DW_AT_name : B
7861 <5b> DW_AT_decl_file : 1
7862 <5c> DW_AT_decl_line : 2
7863 <5d> DW_AT_type : <0x6e>
7864 ...
7865 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
7866 <76> DW_AT_byte_size : 4
7867 <77> DW_AT_encoding : 5 (signed)
7868
7869 imports the wrong die ( 0x75 instead of 0x58 ).
7870 This case will be ignored until the gcc bug is fixed. */
7871 return;
7872 }
7873
7874 /* Figure out the local name after import. */
7875 import_alias = dwarf2_name (die, cu);
7876
7877 /* Figure out where the statement is being imported to. */
7878 import_prefix = determine_prefix (die, cu);
7879
7880 /* Figure out what the scope of the imported die is and prepend it
7881 to the name of the imported die. */
7882 imported_name_prefix = determine_prefix (imported_die, imported_cu);
7883
7884 if (imported_die->tag != DW_TAG_namespace
7885 && imported_die->tag != DW_TAG_module)
7886 {
7887 imported_declaration = imported_name;
7888 canonical_name = imported_name_prefix;
7889 }
7890 else if (strlen (imported_name_prefix) > 0)
7891 canonical_name = obconcat (&objfile->objfile_obstack,
7892 imported_name_prefix, "::", imported_name,
7893 (char *) NULL);
7894 else
7895 canonical_name = imported_name;
7896
7897 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
7898
7899 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
7900 for (child_die = die->child; child_die && child_die->tag;
7901 child_die = sibling_die (child_die))
7902 {
7903 /* DWARF-4: A Fortran use statement with a “rename list” may be
7904 represented by an imported module entry with an import attribute
7905 referring to the module and owned entries corresponding to those
7906 entities that are renamed as part of being imported. */
7907
7908 if (child_die->tag != DW_TAG_imported_declaration)
7909 {
7910 complaint (&symfile_complaints,
7911 _("child DW_TAG_imported_declaration expected "
7912 "- DIE at 0x%x [in module %s]"),
7913 child_die->offset.sect_off, objfile->name);
7914 continue;
7915 }
7916
7917 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
7918 if (import_attr == NULL)
7919 {
7920 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7921 dwarf_tag_name (child_die->tag));
7922 continue;
7923 }
7924
7925 imported_cu = cu;
7926 imported_die = follow_die_ref_or_sig (child_die, import_attr,
7927 &imported_cu);
7928 imported_name = dwarf2_name (imported_die, imported_cu);
7929 if (imported_name == NULL)
7930 {
7931 complaint (&symfile_complaints,
7932 _("child DW_TAG_imported_declaration has unknown "
7933 "imported name - DIE at 0x%x [in module %s]"),
7934 child_die->offset.sect_off, objfile->name);
7935 continue;
7936 }
7937
7938 VEC_safe_push (const_char_ptr, excludes, imported_name);
7939
7940 process_die (child_die, cu);
7941 }
7942
7943 cp_add_using_directive (import_prefix,
7944 canonical_name,
7945 import_alias,
7946 imported_declaration,
7947 excludes,
7948 0,
7949 &objfile->objfile_obstack);
7950
7951 do_cleanups (cleanups);
7952 }
7953
7954 /* Cleanup function for handle_DW_AT_stmt_list. */
7955
7956 static void
7957 free_cu_line_header (void *arg)
7958 {
7959 struct dwarf2_cu *cu = arg;
7960
7961 free_line_header (cu->line_header);
7962 cu->line_header = NULL;
7963 }
7964
7965 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
7966 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
7967 this, it was first present in GCC release 4.3.0. */
7968
7969 static int
7970 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
7971 {
7972 if (!cu->checked_producer)
7973 check_producer (cu);
7974
7975 return cu->producer_is_gcc_lt_4_3;
7976 }
7977
7978 static void
7979 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
7980 const char **name, const char **comp_dir)
7981 {
7982 struct attribute *attr;
7983
7984 *name = NULL;
7985 *comp_dir = NULL;
7986
7987 /* Find the filename. Do not use dwarf2_name here, since the filename
7988 is not a source language identifier. */
7989 attr = dwarf2_attr (die, DW_AT_name, cu);
7990 if (attr)
7991 {
7992 *name = DW_STRING (attr);
7993 }
7994
7995 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
7996 if (attr)
7997 *comp_dir = DW_STRING (attr);
7998 else if (producer_is_gcc_lt_4_3 (cu) && *name != NULL
7999 && IS_ABSOLUTE_PATH (*name))
8000 {
8001 char *d = ldirname (*name);
8002
8003 *comp_dir = d;
8004 if (d != NULL)
8005 make_cleanup (xfree, d);
8006 }
8007 if (*comp_dir != NULL)
8008 {
8009 /* Irix 6.2 native cc prepends <machine>.: to the compilation
8010 directory, get rid of it. */
8011 char *cp = strchr (*comp_dir, ':');
8012
8013 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
8014 *comp_dir = cp + 1;
8015 }
8016
8017 if (*name == NULL)
8018 *name = "<unknown>";
8019 }
8020
8021 /* Handle DW_AT_stmt_list for a compilation unit.
8022 DIE is the DW_TAG_compile_unit die for CU.
8023 COMP_DIR is the compilation directory.
8024 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
8025
8026 static void
8027 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
8028 const char *comp_dir)
8029 {
8030 struct attribute *attr;
8031
8032 gdb_assert (! cu->per_cu->is_debug_types);
8033
8034 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
8035 if (attr)
8036 {
8037 unsigned int line_offset = DW_UNSND (attr);
8038 struct line_header *line_header
8039 = dwarf_decode_line_header (line_offset, cu);
8040
8041 if (line_header)
8042 {
8043 cu->line_header = line_header;
8044 make_cleanup (free_cu_line_header, cu);
8045 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
8046 }
8047 }
8048 }
8049
8050 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
8051
8052 static void
8053 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
8054 {
8055 struct objfile *objfile = dwarf2_per_objfile->objfile;
8056 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
8057 CORE_ADDR lowpc = ((CORE_ADDR) -1);
8058 CORE_ADDR highpc = ((CORE_ADDR) 0);
8059 struct attribute *attr;
8060 const char *name = NULL;
8061 const char *comp_dir = NULL;
8062 struct die_info *child_die;
8063 bfd *abfd = objfile->obfd;
8064 CORE_ADDR baseaddr;
8065
8066 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8067
8068 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
8069
8070 /* If we didn't find a lowpc, set it to highpc to avoid complaints
8071 from finish_block. */
8072 if (lowpc == ((CORE_ADDR) -1))
8073 lowpc = highpc;
8074 lowpc += baseaddr;
8075 highpc += baseaddr;
8076
8077 find_file_and_directory (die, cu, &name, &comp_dir);
8078
8079 prepare_one_comp_unit (cu, die, cu->language);
8080
8081 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
8082 standardised yet. As a workaround for the language detection we fall
8083 back to the DW_AT_producer string. */
8084 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
8085 cu->language = language_opencl;
8086
8087 /* Similar hack for Go. */
8088 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
8089 set_cu_language (DW_LANG_Go, cu);
8090
8091 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
8092
8093 /* Decode line number information if present. We do this before
8094 processing child DIEs, so that the line header table is available
8095 for DW_AT_decl_file. */
8096 handle_DW_AT_stmt_list (die, cu, comp_dir);
8097
8098 /* Process all dies in compilation unit. */
8099 if (die->child != NULL)
8100 {
8101 child_die = die->child;
8102 while (child_die && child_die->tag)
8103 {
8104 process_die (child_die, cu);
8105 child_die = sibling_die (child_die);
8106 }
8107 }
8108
8109 /* Decode macro information, if present. Dwarf 2 macro information
8110 refers to information in the line number info statement program
8111 header, so we can only read it if we've read the header
8112 successfully. */
8113 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
8114 if (attr && cu->line_header)
8115 {
8116 if (dwarf2_attr (die, DW_AT_macro_info, cu))
8117 complaint (&symfile_complaints,
8118 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
8119
8120 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
8121 }
8122 else
8123 {
8124 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
8125 if (attr && cu->line_header)
8126 {
8127 unsigned int macro_offset = DW_UNSND (attr);
8128
8129 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
8130 }
8131 }
8132
8133 do_cleanups (back_to);
8134 }
8135
8136 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
8137 Create the set of symtabs used by this TU, or if this TU is sharing
8138 symtabs with another TU and the symtabs have already been created
8139 then restore those symtabs in the line header.
8140 We don't need the pc/line-number mapping for type units. */
8141
8142 static void
8143 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
8144 {
8145 struct objfile *objfile = dwarf2_per_objfile->objfile;
8146 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8147 struct type_unit_group *tu_group;
8148 int first_time;
8149 struct line_header *lh;
8150 struct attribute *attr;
8151 unsigned int i, line_offset;
8152 struct signatured_type *sig_type;
8153
8154 gdb_assert (per_cu->is_debug_types);
8155 sig_type = (struct signatured_type *) per_cu;
8156
8157 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
8158
8159 /* If we're using .gdb_index (includes -readnow) then
8160 per_cu->s.type_unit_group may not have been set up yet. */
8161 if (sig_type->type_unit_group == NULL)
8162 sig_type->type_unit_group = get_type_unit_group (cu, attr);
8163 tu_group = sig_type->type_unit_group;
8164
8165 /* If we've already processed this stmt_list there's no real need to
8166 do it again, we could fake it and just recreate the part we need
8167 (file name,index -> symtab mapping). If data shows this optimization
8168 is useful we can do it then. */
8169 first_time = tu_group->primary_symtab == NULL;
8170
8171 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
8172 debug info. */
8173 lh = NULL;
8174 if (attr != NULL)
8175 {
8176 line_offset = DW_UNSND (attr);
8177 lh = dwarf_decode_line_header (line_offset, cu);
8178 }
8179 if (lh == NULL)
8180 {
8181 if (first_time)
8182 dwarf2_start_symtab (cu, "", NULL, 0);
8183 else
8184 {
8185 gdb_assert (tu_group->symtabs == NULL);
8186 restart_symtab (0);
8187 }
8188 /* Note: The primary symtab will get allocated at the end. */
8189 return;
8190 }
8191
8192 cu->line_header = lh;
8193 make_cleanup (free_cu_line_header, cu);
8194
8195 if (first_time)
8196 {
8197 dwarf2_start_symtab (cu, "", NULL, 0);
8198
8199 tu_group->num_symtabs = lh->num_file_names;
8200 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
8201
8202 for (i = 0; i < lh->num_file_names; ++i)
8203 {
8204 char *dir = NULL;
8205 struct file_entry *fe = &lh->file_names[i];
8206
8207 if (fe->dir_index)
8208 dir = lh->include_dirs[fe->dir_index - 1];
8209 dwarf2_start_subfile (fe->name, dir, NULL);
8210
8211 /* Note: We don't have to watch for the main subfile here, type units
8212 don't have DW_AT_name. */
8213
8214 if (current_subfile->symtab == NULL)
8215 {
8216 /* NOTE: start_subfile will recognize when it's been passed
8217 a file it has already seen. So we can't assume there's a
8218 simple mapping from lh->file_names to subfiles,
8219 lh->file_names may contain dups. */
8220 current_subfile->symtab = allocate_symtab (current_subfile->name,
8221 objfile);
8222 }
8223
8224 fe->symtab = current_subfile->symtab;
8225 tu_group->symtabs[i] = fe->symtab;
8226 }
8227 }
8228 else
8229 {
8230 restart_symtab (0);
8231
8232 for (i = 0; i < lh->num_file_names; ++i)
8233 {
8234 struct file_entry *fe = &lh->file_names[i];
8235
8236 fe->symtab = tu_group->symtabs[i];
8237 }
8238 }
8239
8240 /* The main symtab is allocated last. Type units don't have DW_AT_name
8241 so they don't have a "real" (so to speak) symtab anyway.
8242 There is later code that will assign the main symtab to all symbols
8243 that don't have one. We need to handle the case of a symbol with a
8244 missing symtab (DW_AT_decl_file) anyway. */
8245 }
8246
8247 /* Process DW_TAG_type_unit.
8248 For TUs we want to skip the first top level sibling if it's not the
8249 actual type being defined by this TU. In this case the first top
8250 level sibling is there to provide context only. */
8251
8252 static void
8253 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8254 {
8255 struct die_info *child_die;
8256
8257 prepare_one_comp_unit (cu, die, language_minimal);
8258
8259 /* Initialize (or reinitialize) the machinery for building symtabs.
8260 We do this before processing child DIEs, so that the line header table
8261 is available for DW_AT_decl_file. */
8262 setup_type_unit_groups (die, cu);
8263
8264 if (die->child != NULL)
8265 {
8266 child_die = die->child;
8267 while (child_die && child_die->tag)
8268 {
8269 process_die (child_die, cu);
8270 child_die = sibling_die (child_die);
8271 }
8272 }
8273 }
8274 \f
8275 /* DWO/DWP files.
8276
8277 http://gcc.gnu.org/wiki/DebugFission
8278 http://gcc.gnu.org/wiki/DebugFissionDWP
8279
8280 To simplify handling of both DWO files ("object" files with the DWARF info)
8281 and DWP files (a file with the DWOs packaged up into one file), we treat
8282 DWP files as having a collection of virtual DWO files. */
8283
8284 static hashval_t
8285 hash_dwo_file (const void *item)
8286 {
8287 const struct dwo_file *dwo_file = item;
8288
8289 return htab_hash_string (dwo_file->name);
8290 }
8291
8292 static int
8293 eq_dwo_file (const void *item_lhs, const void *item_rhs)
8294 {
8295 const struct dwo_file *lhs = item_lhs;
8296 const struct dwo_file *rhs = item_rhs;
8297
8298 return strcmp (lhs->name, rhs->name) == 0;
8299 }
8300
8301 /* Allocate a hash table for DWO files. */
8302
8303 static htab_t
8304 allocate_dwo_file_hash_table (void)
8305 {
8306 struct objfile *objfile = dwarf2_per_objfile->objfile;
8307
8308 return htab_create_alloc_ex (41,
8309 hash_dwo_file,
8310 eq_dwo_file,
8311 NULL,
8312 &objfile->objfile_obstack,
8313 hashtab_obstack_allocate,
8314 dummy_obstack_deallocate);
8315 }
8316
8317 /* Lookup DWO file DWO_NAME. */
8318
8319 static void **
8320 lookup_dwo_file_slot (const char *dwo_name)
8321 {
8322 struct dwo_file find_entry;
8323 void **slot;
8324
8325 if (dwarf2_per_objfile->dwo_files == NULL)
8326 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8327
8328 memset (&find_entry, 0, sizeof (find_entry));
8329 find_entry.name = dwo_name;
8330 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8331
8332 return slot;
8333 }
8334
8335 static hashval_t
8336 hash_dwo_unit (const void *item)
8337 {
8338 const struct dwo_unit *dwo_unit = item;
8339
8340 /* This drops the top 32 bits of the id, but is ok for a hash. */
8341 return dwo_unit->signature;
8342 }
8343
8344 static int
8345 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8346 {
8347 const struct dwo_unit *lhs = item_lhs;
8348 const struct dwo_unit *rhs = item_rhs;
8349
8350 /* The signature is assumed to be unique within the DWO file.
8351 So while object file CU dwo_id's always have the value zero,
8352 that's OK, assuming each object file DWO file has only one CU,
8353 and that's the rule for now. */
8354 return lhs->signature == rhs->signature;
8355 }
8356
8357 /* Allocate a hash table for DWO CUs,TUs.
8358 There is one of these tables for each of CUs,TUs for each DWO file. */
8359
8360 static htab_t
8361 allocate_dwo_unit_table (struct objfile *objfile)
8362 {
8363 /* Start out with a pretty small number.
8364 Generally DWO files contain only one CU and maybe some TUs. */
8365 return htab_create_alloc_ex (3,
8366 hash_dwo_unit,
8367 eq_dwo_unit,
8368 NULL,
8369 &objfile->objfile_obstack,
8370 hashtab_obstack_allocate,
8371 dummy_obstack_deallocate);
8372 }
8373
8374 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
8375
8376 struct create_dwo_info_table_data
8377 {
8378 struct dwo_file *dwo_file;
8379 htab_t cu_htab;
8380 };
8381
8382 /* die_reader_func for create_dwo_debug_info_hash_table. */
8383
8384 static void
8385 create_dwo_debug_info_hash_table_reader (const struct die_reader_specs *reader,
8386 gdb_byte *info_ptr,
8387 struct die_info *comp_unit_die,
8388 int has_children,
8389 void *datap)
8390 {
8391 struct dwarf2_cu *cu = reader->cu;
8392 struct objfile *objfile = dwarf2_per_objfile->objfile;
8393 sect_offset offset = cu->per_cu->offset;
8394 struct dwarf2_section_info *section = cu->per_cu->section;
8395 struct create_dwo_info_table_data *data = datap;
8396 struct dwo_file *dwo_file = data->dwo_file;
8397 htab_t cu_htab = data->cu_htab;
8398 void **slot;
8399 struct attribute *attr;
8400 struct dwo_unit *dwo_unit;
8401
8402 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8403 if (attr == NULL)
8404 {
8405 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
8406 " its dwo_id [in module %s]"),
8407 offset.sect_off, dwo_file->name);
8408 return;
8409 }
8410
8411 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8412 dwo_unit->dwo_file = dwo_file;
8413 dwo_unit->signature = DW_UNSND (attr);
8414 dwo_unit->section = section;
8415 dwo_unit->offset = offset;
8416 dwo_unit->length = cu->per_cu->length;
8417
8418 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
8419 gdb_assert (slot != NULL);
8420 if (*slot != NULL)
8421 {
8422 const struct dwo_unit *dup_dwo_unit = *slot;
8423
8424 complaint (&symfile_complaints,
8425 _("debug entry at offset 0x%x is duplicate to the entry at"
8426 " offset 0x%x, dwo_id 0x%s [in module %s]"),
8427 offset.sect_off, dup_dwo_unit->offset.sect_off,
8428 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
8429 dwo_file->name);
8430 }
8431 else
8432 *slot = dwo_unit;
8433
8434 if (dwarf2_read_debug)
8435 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
8436 offset.sect_off,
8437 phex (dwo_unit->signature,
8438 sizeof (dwo_unit->signature)));
8439 }
8440
8441 /* Create a hash table to map DWO IDs to their CU entry in
8442 .debug_info.dwo in DWO_FILE.
8443 Note: This function processes DWO files only, not DWP files.
8444 Note: A DWO file generally contains one CU, but we don't assume this. */
8445
8446 static htab_t
8447 create_dwo_debug_info_hash_table (struct dwo_file *dwo_file)
8448 {
8449 struct objfile *objfile = dwarf2_per_objfile->objfile;
8450 struct dwarf2_section_info *section = &dwo_file->sections.info;
8451 bfd *abfd;
8452 htab_t cu_htab;
8453 gdb_byte *info_ptr, *end_ptr;
8454 struct create_dwo_info_table_data create_dwo_info_table_data;
8455
8456 dwarf2_read_section (objfile, section);
8457 info_ptr = section->buffer;
8458
8459 if (info_ptr == NULL)
8460 return NULL;
8461
8462 /* We can't set abfd until now because the section may be empty or
8463 not present, in which case section->asection will be NULL. */
8464 abfd = section->asection->owner;
8465
8466 if (dwarf2_read_debug)
8467 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
8468 bfd_get_filename (abfd));
8469
8470 cu_htab = allocate_dwo_unit_table (objfile);
8471
8472 create_dwo_info_table_data.dwo_file = dwo_file;
8473 create_dwo_info_table_data.cu_htab = cu_htab;
8474
8475 end_ptr = info_ptr + section->size;
8476 while (info_ptr < end_ptr)
8477 {
8478 struct dwarf2_per_cu_data per_cu;
8479
8480 memset (&per_cu, 0, sizeof (per_cu));
8481 per_cu.objfile = objfile;
8482 per_cu.is_debug_types = 0;
8483 per_cu.offset.sect_off = info_ptr - section->buffer;
8484 per_cu.section = section;
8485
8486 init_cutu_and_read_dies_no_follow (&per_cu,
8487 &dwo_file->sections.abbrev,
8488 dwo_file,
8489 create_dwo_debug_info_hash_table_reader,
8490 &create_dwo_info_table_data);
8491
8492 info_ptr += per_cu.length;
8493 }
8494
8495 return cu_htab;
8496 }
8497
8498 /* DWP file .debug_{cu,tu}_index section format:
8499 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8500
8501 Both index sections have the same format, and serve to map a 64-bit
8502 signature to a set of section numbers. Each section begins with a header,
8503 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8504 indexes, and a pool of 32-bit section numbers. The index sections will be
8505 aligned at 8-byte boundaries in the file.
8506
8507 The index section header contains two unsigned 32-bit values (using the
8508 byte order of the application binary):
8509
8510 N, the number of compilation units or type units in the index
8511 M, the number of slots in the hash table
8512
8513 (We assume that N and M will not exceed 2^32 - 1.)
8514
8515 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
8516
8517 The hash table begins at offset 8 in the section, and consists of an array
8518 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
8519 order of the application binary). Unused slots in the hash table are 0.
8520 (We rely on the extreme unlikeliness of a signature being exactly 0.)
8521
8522 The parallel table begins immediately after the hash table
8523 (at offset 8 + 8 * M from the beginning of the section), and consists of an
8524 array of 32-bit indexes (using the byte order of the application binary),
8525 corresponding 1-1 with slots in the hash table. Each entry in the parallel
8526 table contains a 32-bit index into the pool of section numbers. For unused
8527 hash table slots, the corresponding entry in the parallel table will be 0.
8528
8529 Given a 64-bit compilation unit signature or a type signature S, an entry
8530 in the hash table is located as follows:
8531
8532 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8533 the low-order k bits all set to 1.
8534
8535 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
8536
8537 3) If the hash table entry at index H matches the signature, use that
8538 entry. If the hash table entry at index H is unused (all zeroes),
8539 terminate the search: the signature is not present in the table.
8540
8541 4) Let H = (H + H') modulo M. Repeat at Step 3.
8542
8543 Because M > N and H' and M are relatively prime, the search is guaranteed
8544 to stop at an unused slot or find the match.
8545
8546 The pool of section numbers begins immediately following the hash table
8547 (at offset 8 + 12 * M from the beginning of the section). The pool of
8548 section numbers consists of an array of 32-bit words (using the byte order
8549 of the application binary). Each item in the array is indexed starting
8550 from 0. The hash table entry provides the index of the first section
8551 number in the set. Additional section numbers in the set follow, and the
8552 set is terminated by a 0 entry (section number 0 is not used in ELF).
8553
8554 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8555 section must be the first entry in the set, and the .debug_abbrev.dwo must
8556 be the second entry. Other members of the set may follow in any order. */
8557
8558 /* Create a hash table to map DWO IDs to their CU/TU entry in
8559 .debug_{info,types}.dwo in DWP_FILE.
8560 Returns NULL if there isn't one.
8561 Note: This function processes DWP files only, not DWO files. */
8562
8563 static struct dwp_hash_table *
8564 create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
8565 {
8566 struct objfile *objfile = dwarf2_per_objfile->objfile;
8567 bfd *dbfd = dwp_file->dbfd;
8568 char *index_ptr, *index_end;
8569 struct dwarf2_section_info *index;
8570 uint32_t version, nr_units, nr_slots;
8571 struct dwp_hash_table *htab;
8572
8573 if (is_debug_types)
8574 index = &dwp_file->sections.tu_index;
8575 else
8576 index = &dwp_file->sections.cu_index;
8577
8578 if (dwarf2_section_empty_p (index))
8579 return NULL;
8580 dwarf2_read_section (objfile, index);
8581
8582 index_ptr = index->buffer;
8583 index_end = index_ptr + index->size;
8584
8585 version = read_4_bytes (dbfd, index_ptr);
8586 index_ptr += 8; /* Skip the unused word. */
8587 nr_units = read_4_bytes (dbfd, index_ptr);
8588 index_ptr += 4;
8589 nr_slots = read_4_bytes (dbfd, index_ptr);
8590 index_ptr += 4;
8591
8592 if (version != 1)
8593 {
8594 error (_("Dwarf Error: unsupported DWP file version (%u)"
8595 " [in module %s]"),
8596 version, dwp_file->name);
8597 }
8598 if (nr_slots != (nr_slots & -nr_slots))
8599 {
8600 error (_("Dwarf Error: number of slots in DWP hash table (%u)"
8601 " is not power of 2 [in module %s]"),
8602 nr_slots, dwp_file->name);
8603 }
8604
8605 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
8606 htab->nr_units = nr_units;
8607 htab->nr_slots = nr_slots;
8608 htab->hash_table = index_ptr;
8609 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
8610 htab->section_pool = htab->unit_table + sizeof (uint32_t) * nr_slots;
8611
8612 return htab;
8613 }
8614
8615 /* Update SECTIONS with the data from SECTP.
8616
8617 This function is like the other "locate" section routines that are
8618 passed to bfd_map_over_sections, but in this context the sections to
8619 read comes from the DWP hash table, not the full ELF section table.
8620
8621 The result is non-zero for success, or zero if an error was found. */
8622
8623 static int
8624 locate_virtual_dwo_sections (asection *sectp,
8625 struct virtual_dwo_sections *sections)
8626 {
8627 const struct dwop_section_names *names = &dwop_section_names;
8628
8629 if (section_is_p (sectp->name, &names->abbrev_dwo))
8630 {
8631 /* There can be only one. */
8632 if (sections->abbrev.asection != NULL)
8633 return 0;
8634 sections->abbrev.asection = sectp;
8635 sections->abbrev.size = bfd_get_section_size (sectp);
8636 }
8637 else if (section_is_p (sectp->name, &names->info_dwo)
8638 || section_is_p (sectp->name, &names->types_dwo))
8639 {
8640 /* There can be only one. */
8641 if (sections->info_or_types.asection != NULL)
8642 return 0;
8643 sections->info_or_types.asection = sectp;
8644 sections->info_or_types.size = bfd_get_section_size (sectp);
8645 }
8646 else if (section_is_p (sectp->name, &names->line_dwo))
8647 {
8648 /* There can be only one. */
8649 if (sections->line.asection != NULL)
8650 return 0;
8651 sections->line.asection = sectp;
8652 sections->line.size = bfd_get_section_size (sectp);
8653 }
8654 else if (section_is_p (sectp->name, &names->loc_dwo))
8655 {
8656 /* There can be only one. */
8657 if (sections->loc.asection != NULL)
8658 return 0;
8659 sections->loc.asection = sectp;
8660 sections->loc.size = bfd_get_section_size (sectp);
8661 }
8662 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8663 {
8664 /* There can be only one. */
8665 if (sections->macinfo.asection != NULL)
8666 return 0;
8667 sections->macinfo.asection = sectp;
8668 sections->macinfo.size = bfd_get_section_size (sectp);
8669 }
8670 else if (section_is_p (sectp->name, &names->macro_dwo))
8671 {
8672 /* There can be only one. */
8673 if (sections->macro.asection != NULL)
8674 return 0;
8675 sections->macro.asection = sectp;
8676 sections->macro.size = bfd_get_section_size (sectp);
8677 }
8678 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8679 {
8680 /* There can be only one. */
8681 if (sections->str_offsets.asection != NULL)
8682 return 0;
8683 sections->str_offsets.asection = sectp;
8684 sections->str_offsets.size = bfd_get_section_size (sectp);
8685 }
8686 else
8687 {
8688 /* No other kind of section is valid. */
8689 return 0;
8690 }
8691
8692 return 1;
8693 }
8694
8695 /* Create a dwo_unit object for the DWO with signature SIGNATURE.
8696 HTAB is the hash table from the DWP file.
8697 SECTION_INDEX is the index of the DWO in HTAB. */
8698
8699 static struct dwo_unit *
8700 create_dwo_in_dwp (struct dwp_file *dwp_file,
8701 const struct dwp_hash_table *htab,
8702 uint32_t section_index,
8703 ULONGEST signature, int is_debug_types)
8704 {
8705 struct objfile *objfile = dwarf2_per_objfile->objfile;
8706 bfd *dbfd = dwp_file->dbfd;
8707 const char *kind = is_debug_types ? "TU" : "CU";
8708 struct dwo_file *dwo_file;
8709 struct dwo_unit *dwo_unit;
8710 struct virtual_dwo_sections sections;
8711 void **dwo_file_slot;
8712 char *virtual_dwo_name;
8713 struct dwarf2_section_info *cutu;
8714 struct cleanup *cleanups;
8715 int i;
8716
8717 if (dwarf2_read_debug)
8718 {
8719 fprintf_unfiltered (gdb_stdlog, "Reading %s %u/0x%s in DWP file: %s\n",
8720 kind,
8721 section_index, phex (signature, sizeof (signature)),
8722 dwp_file->name);
8723 }
8724
8725 /* Fetch the sections of this DWO.
8726 Put a limit on the number of sections we look for so that bad data
8727 doesn't cause us to loop forever. */
8728
8729 #define MAX_NR_DWO_SECTIONS \
8730 (1 /* .debug_info or .debug_types */ \
8731 + 1 /* .debug_abbrev */ \
8732 + 1 /* .debug_line */ \
8733 + 1 /* .debug_loc */ \
8734 + 1 /* .debug_str_offsets */ \
8735 + 1 /* .debug_macro */ \
8736 + 1 /* .debug_macinfo */ \
8737 + 1 /* trailing zero */)
8738
8739 memset (&sections, 0, sizeof (sections));
8740 cleanups = make_cleanup (null_cleanup, 0);
8741
8742 for (i = 0; i < MAX_NR_DWO_SECTIONS; ++i)
8743 {
8744 asection *sectp;
8745 uint32_t section_nr =
8746 read_4_bytes (dbfd,
8747 htab->section_pool
8748 + (section_index + i) * sizeof (uint32_t));
8749
8750 if (section_nr == 0)
8751 break;
8752 if (section_nr >= dwp_file->num_sections)
8753 {
8754 error (_("Dwarf Error: bad DWP hash table, section number too large"
8755 " [in module %s]"),
8756 dwp_file->name);
8757 }
8758
8759 sectp = dwp_file->elf_sections[section_nr];
8760 if (! locate_virtual_dwo_sections (sectp, &sections))
8761 {
8762 error (_("Dwarf Error: bad DWP hash table, invalid section found"
8763 " [in module %s]"),
8764 dwp_file->name);
8765 }
8766 }
8767
8768 if (i < 2
8769 || sections.info_or_types.asection == NULL
8770 || sections.abbrev.asection == NULL)
8771 {
8772 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
8773 " [in module %s]"),
8774 dwp_file->name);
8775 }
8776 if (i == MAX_NR_DWO_SECTIONS)
8777 {
8778 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
8779 " [in module %s]"),
8780 dwp_file->name);
8781 }
8782
8783 /* It's easier for the rest of the code if we fake a struct dwo_file and
8784 have dwo_unit "live" in that. At least for now.
8785
8786 The DWP file can be made up of a random collection of CUs and TUs.
8787 However, for each CU + set of TUs that came from the same original DWO
8788 file, we want to combine them back into a virtual DWO file to save space
8789 (fewer struct dwo_file objects to allocated). Remember that for really
8790 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8791
8792 virtual_dwo_name =
8793 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
8794 sections.abbrev.asection ? sections.abbrev.asection->id : 0,
8795 sections.line.asection ? sections.line.asection->id : 0,
8796 sections.loc.asection ? sections.loc.asection->id : 0,
8797 (sections.str_offsets.asection
8798 ? sections.str_offsets.asection->id
8799 : 0));
8800 make_cleanup (xfree, virtual_dwo_name);
8801 /* Can we use an existing virtual DWO file? */
8802 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name);
8803 /* Create one if necessary. */
8804 if (*dwo_file_slot == NULL)
8805 {
8806 if (dwarf2_read_debug)
8807 {
8808 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
8809 virtual_dwo_name);
8810 }
8811 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8812 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8813 virtual_dwo_name,
8814 strlen (virtual_dwo_name));
8815 dwo_file->sections.abbrev = sections.abbrev;
8816 dwo_file->sections.line = sections.line;
8817 dwo_file->sections.loc = sections.loc;
8818 dwo_file->sections.macinfo = sections.macinfo;
8819 dwo_file->sections.macro = sections.macro;
8820 dwo_file->sections.str_offsets = sections.str_offsets;
8821 /* The "str" section is global to the entire DWP file. */
8822 dwo_file->sections.str = dwp_file->sections.str;
8823 /* The info or types section is assigned later to dwo_unit,
8824 there's no need to record it in dwo_file.
8825 Also, we can't simply record type sections in dwo_file because
8826 we record a pointer into the vector in dwo_unit. As we collect more
8827 types we'll grow the vector and eventually have to reallocate space
8828 for it, invalidating all the pointers into the current copy. */
8829 *dwo_file_slot = dwo_file;
8830 }
8831 else
8832 {
8833 if (dwarf2_read_debug)
8834 {
8835 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
8836 virtual_dwo_name);
8837 }
8838 dwo_file = *dwo_file_slot;
8839 }
8840 do_cleanups (cleanups);
8841
8842 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8843 dwo_unit->dwo_file = dwo_file;
8844 dwo_unit->signature = signature;
8845 dwo_unit->section = obstack_alloc (&objfile->objfile_obstack,
8846 sizeof (struct dwarf2_section_info));
8847 *dwo_unit->section = sections.info_or_types;
8848 /* offset, length, type_offset_in_tu are set later. */
8849
8850 return dwo_unit;
8851 }
8852
8853 /* Lookup the DWO with SIGNATURE in DWP_FILE. */
8854
8855 static struct dwo_unit *
8856 lookup_dwo_in_dwp (struct dwp_file *dwp_file,
8857 const struct dwp_hash_table *htab,
8858 ULONGEST signature, int is_debug_types)
8859 {
8860 bfd *dbfd = dwp_file->dbfd;
8861 uint32_t mask = htab->nr_slots - 1;
8862 uint32_t hash = signature & mask;
8863 uint32_t hash2 = ((signature >> 32) & mask) | 1;
8864 unsigned int i;
8865 void **slot;
8866 struct dwo_unit find_dwo_cu, *dwo_cu;
8867
8868 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
8869 find_dwo_cu.signature = signature;
8870 slot = htab_find_slot (dwp_file->loaded_cutus, &find_dwo_cu, INSERT);
8871
8872 if (*slot != NULL)
8873 return *slot;
8874
8875 /* Use a for loop so that we don't loop forever on bad debug info. */
8876 for (i = 0; i < htab->nr_slots; ++i)
8877 {
8878 ULONGEST signature_in_table;
8879
8880 signature_in_table =
8881 read_8_bytes (dbfd, htab->hash_table + hash * sizeof (uint64_t));
8882 if (signature_in_table == signature)
8883 {
8884 uint32_t section_index =
8885 read_4_bytes (dbfd, htab->unit_table + hash * sizeof (uint32_t));
8886
8887 *slot = create_dwo_in_dwp (dwp_file, htab, section_index,
8888 signature, is_debug_types);
8889 return *slot;
8890 }
8891 if (signature_in_table == 0)
8892 return NULL;
8893 hash = (hash + hash2) & mask;
8894 }
8895
8896 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
8897 " [in module %s]"),
8898 dwp_file->name);
8899 }
8900
8901 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
8902 Open the file specified by FILE_NAME and hand it off to BFD for
8903 preliminary analysis. Return a newly initialized bfd *, which
8904 includes a canonicalized copy of FILE_NAME.
8905 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
8906 In case of trouble, return NULL.
8907 NOTE: This function is derived from symfile_bfd_open. */
8908
8909 static bfd *
8910 try_open_dwop_file (const char *file_name, int is_dwp)
8911 {
8912 bfd *sym_bfd;
8913 int desc, flags;
8914 char *absolute_name;
8915
8916 flags = OPF_TRY_CWD_FIRST;
8917 if (is_dwp)
8918 flags |= OPF_SEARCH_IN_PATH;
8919 desc = openp (debug_file_directory, flags, file_name,
8920 O_RDONLY | O_BINARY, &absolute_name);
8921 if (desc < 0)
8922 return NULL;
8923
8924 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
8925 if (!sym_bfd)
8926 {
8927 xfree (absolute_name);
8928 return NULL;
8929 }
8930 xfree (absolute_name);
8931 bfd_set_cacheable (sym_bfd, 1);
8932
8933 if (!bfd_check_format (sym_bfd, bfd_object))
8934 {
8935 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
8936 return NULL;
8937 }
8938
8939 return sym_bfd;
8940 }
8941
8942 /* Try to open DWO file FILE_NAME.
8943 COMP_DIR is the DW_AT_comp_dir attribute.
8944 The result is the bfd handle of the file.
8945 If there is a problem finding or opening the file, return NULL.
8946 Upon success, the canonicalized path of the file is stored in the bfd,
8947 same as symfile_bfd_open. */
8948
8949 static bfd *
8950 open_dwo_file (const char *file_name, const char *comp_dir)
8951 {
8952 bfd *abfd;
8953
8954 if (IS_ABSOLUTE_PATH (file_name))
8955 return try_open_dwop_file (file_name, 0 /*is_dwp*/);
8956
8957 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
8958
8959 if (comp_dir != NULL)
8960 {
8961 char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
8962
8963 /* NOTE: If comp_dir is a relative path, this will also try the
8964 search path, which seems useful. */
8965 abfd = try_open_dwop_file (path_to_try, 0 /*is_dwp*/);
8966 xfree (path_to_try);
8967 if (abfd != NULL)
8968 return abfd;
8969 }
8970
8971 /* That didn't work, try debug-file-directory, which, despite its name,
8972 is a list of paths. */
8973
8974 if (*debug_file_directory == '\0')
8975 return NULL;
8976
8977 return try_open_dwop_file (file_name, 0 /*is_dwp*/);
8978 }
8979
8980 /* This function is mapped across the sections and remembers the offset and
8981 size of each of the DWO debugging sections we are interested in. */
8982
8983 static void
8984 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
8985 {
8986 struct dwo_sections *dwo_sections = dwo_sections_ptr;
8987 const struct dwop_section_names *names = &dwop_section_names;
8988
8989 if (section_is_p (sectp->name, &names->abbrev_dwo))
8990 {
8991 dwo_sections->abbrev.asection = sectp;
8992 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
8993 }
8994 else if (section_is_p (sectp->name, &names->info_dwo))
8995 {
8996 dwo_sections->info.asection = sectp;
8997 dwo_sections->info.size = bfd_get_section_size (sectp);
8998 }
8999 else if (section_is_p (sectp->name, &names->line_dwo))
9000 {
9001 dwo_sections->line.asection = sectp;
9002 dwo_sections->line.size = bfd_get_section_size (sectp);
9003 }
9004 else if (section_is_p (sectp->name, &names->loc_dwo))
9005 {
9006 dwo_sections->loc.asection = sectp;
9007 dwo_sections->loc.size = bfd_get_section_size (sectp);
9008 }
9009 else if (section_is_p (sectp->name, &names->macinfo_dwo))
9010 {
9011 dwo_sections->macinfo.asection = sectp;
9012 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
9013 }
9014 else if (section_is_p (sectp->name, &names->macro_dwo))
9015 {
9016 dwo_sections->macro.asection = sectp;
9017 dwo_sections->macro.size = bfd_get_section_size (sectp);
9018 }
9019 else if (section_is_p (sectp->name, &names->str_dwo))
9020 {
9021 dwo_sections->str.asection = sectp;
9022 dwo_sections->str.size = bfd_get_section_size (sectp);
9023 }
9024 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
9025 {
9026 dwo_sections->str_offsets.asection = sectp;
9027 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
9028 }
9029 else if (section_is_p (sectp->name, &names->types_dwo))
9030 {
9031 struct dwarf2_section_info type_section;
9032
9033 memset (&type_section, 0, sizeof (type_section));
9034 type_section.asection = sectp;
9035 type_section.size = bfd_get_section_size (sectp);
9036 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
9037 &type_section);
9038 }
9039 }
9040
9041 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
9042 by PER_CU.
9043 The result is NULL if DWO_NAME can't be found. */
9044
9045 static struct dwo_file *
9046 open_and_init_dwo_file (const char *dwo_name, const char *comp_dir)
9047 {
9048 struct objfile *objfile = dwarf2_per_objfile->objfile;
9049 struct dwo_file *dwo_file;
9050 bfd *dbfd;
9051 struct cleanup *cleanups;
9052
9053 dbfd = open_dwo_file (dwo_name, comp_dir);
9054 if (dbfd == NULL)
9055 {
9056 if (dwarf2_read_debug)
9057 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
9058 return NULL;
9059 }
9060 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
9061 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
9062 dwo_name, strlen (dwo_name));
9063 dwo_file->dbfd = dbfd;
9064
9065 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
9066
9067 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
9068
9069 dwo_file->cus = create_dwo_debug_info_hash_table (dwo_file);
9070
9071 dwo_file->tus = create_debug_types_hash_table (dwo_file,
9072 dwo_file->sections.types);
9073
9074 discard_cleanups (cleanups);
9075
9076 if (dwarf2_read_debug)
9077 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
9078
9079 return dwo_file;
9080 }
9081
9082 /* This function is mapped across the sections and remembers the offset and
9083 size of each of the DWP debugging sections we are interested in. */
9084
9085 static void
9086 dwarf2_locate_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
9087 {
9088 struct dwp_file *dwp_file = dwp_file_ptr;
9089 const struct dwop_section_names *names = &dwop_section_names;
9090 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
9091
9092 /* Record the ELF section number for later lookup: this is what the
9093 .debug_cu_index,.debug_tu_index tables use. */
9094 gdb_assert (elf_section_nr < dwp_file->num_sections);
9095 dwp_file->elf_sections[elf_section_nr] = sectp;
9096
9097 /* Look for specific sections that we need. */
9098 if (section_is_p (sectp->name, &names->str_dwo))
9099 {
9100 dwp_file->sections.str.asection = sectp;
9101 dwp_file->sections.str.size = bfd_get_section_size (sectp);
9102 }
9103 else if (section_is_p (sectp->name, &names->cu_index))
9104 {
9105 dwp_file->sections.cu_index.asection = sectp;
9106 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
9107 }
9108 else if (section_is_p (sectp->name, &names->tu_index))
9109 {
9110 dwp_file->sections.tu_index.asection = sectp;
9111 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
9112 }
9113 }
9114
9115 /* Hash function for dwp_file loaded CUs/TUs. */
9116
9117 static hashval_t
9118 hash_dwp_loaded_cutus (const void *item)
9119 {
9120 const struct dwo_unit *dwo_unit = item;
9121
9122 /* This drops the top 32 bits of the signature, but is ok for a hash. */
9123 return dwo_unit->signature;
9124 }
9125
9126 /* Equality function for dwp_file loaded CUs/TUs. */
9127
9128 static int
9129 eq_dwp_loaded_cutus (const void *a, const void *b)
9130 {
9131 const struct dwo_unit *dua = a;
9132 const struct dwo_unit *dub = b;
9133
9134 return dua->signature == dub->signature;
9135 }
9136
9137 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
9138
9139 static htab_t
9140 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
9141 {
9142 return htab_create_alloc_ex (3,
9143 hash_dwp_loaded_cutus,
9144 eq_dwp_loaded_cutus,
9145 NULL,
9146 &objfile->objfile_obstack,
9147 hashtab_obstack_allocate,
9148 dummy_obstack_deallocate);
9149 }
9150
9151 /* Try to open DWP file FILE_NAME.
9152 The result is the bfd handle of the file.
9153 If there is a problem finding or opening the file, return NULL.
9154 Upon success, the canonicalized path of the file is stored in the bfd,
9155 same as symfile_bfd_open. */
9156
9157 static bfd *
9158 open_dwp_file (const char *file_name)
9159 {
9160 return try_open_dwop_file (file_name, 1 /*is_dwp*/);
9161 }
9162
9163 /* Initialize the use of the DWP file for the current objfile.
9164 By convention the name of the DWP file is ${objfile}.dwp.
9165 The result is NULL if it can't be found. */
9166
9167 static struct dwp_file *
9168 open_and_init_dwp_file (void)
9169 {
9170 struct objfile *objfile = dwarf2_per_objfile->objfile;
9171 struct dwp_file *dwp_file;
9172 char *dwp_name;
9173 bfd *dbfd;
9174 struct cleanup *cleanups;
9175
9176 dwp_name = xstrprintf ("%s.dwp", dwarf2_per_objfile->objfile->name);
9177 cleanups = make_cleanup (xfree, dwp_name);
9178
9179 dbfd = open_dwp_file (dwp_name);
9180 if (dbfd == NULL)
9181 {
9182 if (dwarf2_read_debug)
9183 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
9184 do_cleanups (cleanups);
9185 return NULL;
9186 }
9187 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
9188 dwp_file->name = obstack_copy0 (&objfile->objfile_obstack,
9189 dwp_name, strlen (dwp_name));
9190 dwp_file->dbfd = dbfd;
9191 do_cleanups (cleanups);
9192
9193 /* +1: section 0 is unused */
9194 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
9195 dwp_file->elf_sections =
9196 OBSTACK_CALLOC (&objfile->objfile_obstack,
9197 dwp_file->num_sections, asection *);
9198
9199 bfd_map_over_sections (dbfd, dwarf2_locate_dwp_sections, dwp_file);
9200
9201 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
9202
9203 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
9204
9205 dwp_file->loaded_cutus = allocate_dwp_loaded_cutus_table (objfile);
9206
9207 if (dwarf2_read_debug)
9208 {
9209 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
9210 fprintf_unfiltered (gdb_stdlog,
9211 " %u CUs, %u TUs\n",
9212 dwp_file->cus ? dwp_file->cus->nr_units : 0,
9213 dwp_file->tus ? dwp_file->tus->nr_units : 0);
9214 }
9215
9216 return dwp_file;
9217 }
9218
9219 /* Wrapper around open_and_init_dwp_file, only open it once. */
9220
9221 static struct dwp_file *
9222 get_dwp_file (void)
9223 {
9224 if (! dwarf2_per_objfile->dwp_checked)
9225 {
9226 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
9227 dwarf2_per_objfile->dwp_checked = 1;
9228 }
9229 return dwarf2_per_objfile->dwp_file;
9230 }
9231
9232 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9233 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9234 or in the DWP file for the objfile, referenced by THIS_UNIT.
9235 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
9236 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9237
9238 This is called, for example, when wanting to read a variable with a
9239 complex location. Therefore we don't want to do file i/o for every call.
9240 Therefore we don't want to look for a DWO file on every call.
9241 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9242 then we check if we've already seen DWO_NAME, and only THEN do we check
9243 for a DWO file.
9244
9245 The result is a pointer to the dwo_unit object or NULL if we didn't find it
9246 (dwo_id mismatch or couldn't find the DWO/DWP file). */
9247
9248 static struct dwo_unit *
9249 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
9250 const char *dwo_name, const char *comp_dir,
9251 ULONGEST signature, int is_debug_types)
9252 {
9253 struct objfile *objfile = dwarf2_per_objfile->objfile;
9254 const char *kind = is_debug_types ? "TU" : "CU";
9255 void **dwo_file_slot;
9256 struct dwo_file *dwo_file;
9257 struct dwp_file *dwp_file;
9258
9259 /* Have we already read SIGNATURE from a DWP file? */
9260
9261 dwp_file = get_dwp_file ();
9262 if (dwp_file != NULL)
9263 {
9264 const struct dwp_hash_table *dwp_htab =
9265 is_debug_types ? dwp_file->tus : dwp_file->cus;
9266
9267 if (dwp_htab != NULL)
9268 {
9269 struct dwo_unit *dwo_cutu =
9270 lookup_dwo_in_dwp (dwp_file, dwp_htab, signature, is_debug_types);
9271
9272 if (dwo_cutu != NULL)
9273 {
9274 if (dwarf2_read_debug)
9275 {
9276 fprintf_unfiltered (gdb_stdlog,
9277 "Virtual DWO %s %s found: @%s\n",
9278 kind, hex_string (signature),
9279 host_address_to_string (dwo_cutu));
9280 }
9281 return dwo_cutu;
9282 }
9283 }
9284 }
9285
9286 /* Have we already seen DWO_NAME? */
9287
9288 dwo_file_slot = lookup_dwo_file_slot (dwo_name);
9289 if (*dwo_file_slot == NULL)
9290 {
9291 /* Read in the file and build a table of the DWOs it contains. */
9292 *dwo_file_slot = open_and_init_dwo_file (dwo_name, comp_dir);
9293 }
9294 /* NOTE: This will be NULL if unable to open the file. */
9295 dwo_file = *dwo_file_slot;
9296
9297 if (dwo_file != NULL)
9298 {
9299 htab_t htab = is_debug_types ? dwo_file->tus : dwo_file->cus;
9300
9301 if (htab != NULL)
9302 {
9303 struct dwo_unit find_dwo_cutu, *dwo_cutu;
9304
9305 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9306 find_dwo_cutu.signature = signature;
9307 dwo_cutu = htab_find (htab, &find_dwo_cutu);
9308
9309 if (dwo_cutu != NULL)
9310 {
9311 if (dwarf2_read_debug)
9312 {
9313 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
9314 kind, dwo_name, hex_string (signature),
9315 host_address_to_string (dwo_cutu));
9316 }
9317 return dwo_cutu;
9318 }
9319 }
9320 }
9321
9322 /* We didn't find it. This could mean a dwo_id mismatch, or
9323 someone deleted the DWO/DWP file, or the search path isn't set up
9324 correctly to find the file. */
9325
9326 if (dwarf2_read_debug)
9327 {
9328 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
9329 kind, dwo_name, hex_string (signature));
9330 }
9331
9332 complaint (&symfile_complaints,
9333 _("Could not find DWO %s referenced by CU at offset 0x%x"
9334 " [in module %s]"),
9335 kind, this_unit->offset.sect_off, objfile->name);
9336 return NULL;
9337 }
9338
9339 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9340 See lookup_dwo_cutu_unit for details. */
9341
9342 static struct dwo_unit *
9343 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
9344 const char *dwo_name, const char *comp_dir,
9345 ULONGEST signature)
9346 {
9347 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
9348 }
9349
9350 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9351 See lookup_dwo_cutu_unit for details. */
9352
9353 static struct dwo_unit *
9354 lookup_dwo_type_unit (struct signatured_type *this_tu,
9355 const char *dwo_name, const char *comp_dir)
9356 {
9357 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
9358 }
9359
9360 /* Free all resources associated with DWO_FILE.
9361 Close the DWO file and munmap the sections.
9362 All memory should be on the objfile obstack. */
9363
9364 static void
9365 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
9366 {
9367 int ix;
9368 struct dwarf2_section_info *section;
9369
9370 /* Note: dbfd is NULL for virtual DWO files. */
9371 gdb_bfd_unref (dwo_file->dbfd);
9372
9373 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
9374 }
9375
9376 /* Wrapper for free_dwo_file for use in cleanups. */
9377
9378 static void
9379 free_dwo_file_cleanup (void *arg)
9380 {
9381 struct dwo_file *dwo_file = (struct dwo_file *) arg;
9382 struct objfile *objfile = dwarf2_per_objfile->objfile;
9383
9384 free_dwo_file (dwo_file, objfile);
9385 }
9386
9387 /* Traversal function for free_dwo_files. */
9388
9389 static int
9390 free_dwo_file_from_slot (void **slot, void *info)
9391 {
9392 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
9393 struct objfile *objfile = (struct objfile *) info;
9394
9395 free_dwo_file (dwo_file, objfile);
9396
9397 return 1;
9398 }
9399
9400 /* Free all resources associated with DWO_FILES. */
9401
9402 static void
9403 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
9404 {
9405 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
9406 }
9407 \f
9408 /* Read in various DIEs. */
9409
9410 /* qsort helper for inherit_abstract_dies. */
9411
9412 static int
9413 unsigned_int_compar (const void *ap, const void *bp)
9414 {
9415 unsigned int a = *(unsigned int *) ap;
9416 unsigned int b = *(unsigned int *) bp;
9417
9418 return (a > b) - (b > a);
9419 }
9420
9421 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
9422 Inherit only the children of the DW_AT_abstract_origin DIE not being
9423 already referenced by DW_AT_abstract_origin from the children of the
9424 current DIE. */
9425
9426 static void
9427 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9428 {
9429 struct die_info *child_die;
9430 unsigned die_children_count;
9431 /* CU offsets which were referenced by children of the current DIE. */
9432 sect_offset *offsets;
9433 sect_offset *offsets_end, *offsetp;
9434 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9435 struct die_info *origin_die;
9436 /* Iterator of the ORIGIN_DIE children. */
9437 struct die_info *origin_child_die;
9438 struct cleanup *cleanups;
9439 struct attribute *attr;
9440 struct dwarf2_cu *origin_cu;
9441 struct pending **origin_previous_list_in_scope;
9442
9443 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9444 if (!attr)
9445 return;
9446
9447 /* Note that following die references may follow to a die in a
9448 different cu. */
9449
9450 origin_cu = cu;
9451 origin_die = follow_die_ref (die, attr, &origin_cu);
9452
9453 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9454 symbols in. */
9455 origin_previous_list_in_scope = origin_cu->list_in_scope;
9456 origin_cu->list_in_scope = cu->list_in_scope;
9457
9458 if (die->tag != origin_die->tag
9459 && !(die->tag == DW_TAG_inlined_subroutine
9460 && origin_die->tag == DW_TAG_subprogram))
9461 complaint (&symfile_complaints,
9462 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
9463 die->offset.sect_off, origin_die->offset.sect_off);
9464
9465 child_die = die->child;
9466 die_children_count = 0;
9467 while (child_die && child_die->tag)
9468 {
9469 child_die = sibling_die (child_die);
9470 die_children_count++;
9471 }
9472 offsets = xmalloc (sizeof (*offsets) * die_children_count);
9473 cleanups = make_cleanup (xfree, offsets);
9474
9475 offsets_end = offsets;
9476 child_die = die->child;
9477 while (child_die && child_die->tag)
9478 {
9479 /* For each CHILD_DIE, find the corresponding child of
9480 ORIGIN_DIE. If there is more than one layer of
9481 DW_AT_abstract_origin, follow them all; there shouldn't be,
9482 but GCC versions at least through 4.4 generate this (GCC PR
9483 40573). */
9484 struct die_info *child_origin_die = child_die;
9485 struct dwarf2_cu *child_origin_cu = cu;
9486
9487 while (1)
9488 {
9489 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
9490 child_origin_cu);
9491 if (attr == NULL)
9492 break;
9493 child_origin_die = follow_die_ref (child_origin_die, attr,
9494 &child_origin_cu);
9495 }
9496
9497 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
9498 counterpart may exist. */
9499 if (child_origin_die != child_die)
9500 {
9501 if (child_die->tag != child_origin_die->tag
9502 && !(child_die->tag == DW_TAG_inlined_subroutine
9503 && child_origin_die->tag == DW_TAG_subprogram))
9504 complaint (&symfile_complaints,
9505 _("Child DIE 0x%x and its abstract origin 0x%x have "
9506 "different tags"), child_die->offset.sect_off,
9507 child_origin_die->offset.sect_off);
9508 if (child_origin_die->parent != origin_die)
9509 complaint (&symfile_complaints,
9510 _("Child DIE 0x%x and its abstract origin 0x%x have "
9511 "different parents"), child_die->offset.sect_off,
9512 child_origin_die->offset.sect_off);
9513 else
9514 *offsets_end++ = child_origin_die->offset;
9515 }
9516 child_die = sibling_die (child_die);
9517 }
9518 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
9519 unsigned_int_compar);
9520 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
9521 if (offsetp[-1].sect_off == offsetp->sect_off)
9522 complaint (&symfile_complaints,
9523 _("Multiple children of DIE 0x%x refer "
9524 "to DIE 0x%x as their abstract origin"),
9525 die->offset.sect_off, offsetp->sect_off);
9526
9527 offsetp = offsets;
9528 origin_child_die = origin_die->child;
9529 while (origin_child_die && origin_child_die->tag)
9530 {
9531 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
9532 while (offsetp < offsets_end
9533 && offsetp->sect_off < origin_child_die->offset.sect_off)
9534 offsetp++;
9535 if (offsetp >= offsets_end
9536 || offsetp->sect_off > origin_child_die->offset.sect_off)
9537 {
9538 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
9539 process_die (origin_child_die, origin_cu);
9540 }
9541 origin_child_die = sibling_die (origin_child_die);
9542 }
9543 origin_cu->list_in_scope = origin_previous_list_in_scope;
9544
9545 do_cleanups (cleanups);
9546 }
9547
9548 static void
9549 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
9550 {
9551 struct objfile *objfile = cu->objfile;
9552 struct context_stack *new;
9553 CORE_ADDR lowpc;
9554 CORE_ADDR highpc;
9555 struct die_info *child_die;
9556 struct attribute *attr, *call_line, *call_file;
9557 const char *name;
9558 CORE_ADDR baseaddr;
9559 struct block *block;
9560 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
9561 VEC (symbolp) *template_args = NULL;
9562 struct template_symbol *templ_func = NULL;
9563
9564 if (inlined_func)
9565 {
9566 /* If we do not have call site information, we can't show the
9567 caller of this inlined function. That's too confusing, so
9568 only use the scope for local variables. */
9569 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
9570 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
9571 if (call_line == NULL || call_file == NULL)
9572 {
9573 read_lexical_block_scope (die, cu);
9574 return;
9575 }
9576 }
9577
9578 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9579
9580 name = dwarf2_name (die, cu);
9581
9582 /* Ignore functions with missing or empty names. These are actually
9583 illegal according to the DWARF standard. */
9584 if (name == NULL)
9585 {
9586 complaint (&symfile_complaints,
9587 _("missing name for subprogram DIE at %d"),
9588 die->offset.sect_off);
9589 return;
9590 }
9591
9592 /* Ignore functions with missing or invalid low and high pc attributes. */
9593 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9594 {
9595 attr = dwarf2_attr (die, DW_AT_external, cu);
9596 if (!attr || !DW_UNSND (attr))
9597 complaint (&symfile_complaints,
9598 _("cannot get low and high bounds "
9599 "for subprogram DIE at %d"),
9600 die->offset.sect_off);
9601 return;
9602 }
9603
9604 lowpc += baseaddr;
9605 highpc += baseaddr;
9606
9607 /* If we have any template arguments, then we must allocate a
9608 different sort of symbol. */
9609 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
9610 {
9611 if (child_die->tag == DW_TAG_template_type_param
9612 || child_die->tag == DW_TAG_template_value_param)
9613 {
9614 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
9615 struct template_symbol);
9616 templ_func->base.is_cplus_template_function = 1;
9617 break;
9618 }
9619 }
9620
9621 new = push_context (0, lowpc);
9622 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
9623 (struct symbol *) templ_func);
9624
9625 /* If there is a location expression for DW_AT_frame_base, record
9626 it. */
9627 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
9628 if (attr)
9629 dwarf2_symbol_mark_computed (attr, new->name, cu, 1);
9630
9631 cu->list_in_scope = &local_symbols;
9632
9633 if (die->child != NULL)
9634 {
9635 child_die = die->child;
9636 while (child_die && child_die->tag)
9637 {
9638 if (child_die->tag == DW_TAG_template_type_param
9639 || child_die->tag == DW_TAG_template_value_param)
9640 {
9641 struct symbol *arg = new_symbol (child_die, NULL, cu);
9642
9643 if (arg != NULL)
9644 VEC_safe_push (symbolp, template_args, arg);
9645 }
9646 else
9647 process_die (child_die, cu);
9648 child_die = sibling_die (child_die);
9649 }
9650 }
9651
9652 inherit_abstract_dies (die, cu);
9653
9654 /* If we have a DW_AT_specification, we might need to import using
9655 directives from the context of the specification DIE. See the
9656 comment in determine_prefix. */
9657 if (cu->language == language_cplus
9658 && dwarf2_attr (die, DW_AT_specification, cu))
9659 {
9660 struct dwarf2_cu *spec_cu = cu;
9661 struct die_info *spec_die = die_specification (die, &spec_cu);
9662
9663 while (spec_die)
9664 {
9665 child_die = spec_die->child;
9666 while (child_die && child_die->tag)
9667 {
9668 if (child_die->tag == DW_TAG_imported_module)
9669 process_die (child_die, spec_cu);
9670 child_die = sibling_die (child_die);
9671 }
9672
9673 /* In some cases, GCC generates specification DIEs that
9674 themselves contain DW_AT_specification attributes. */
9675 spec_die = die_specification (spec_die, &spec_cu);
9676 }
9677 }
9678
9679 new = pop_context ();
9680 /* Make a block for the local symbols within. */
9681 block = finish_block (new->name, &local_symbols, new->old_blocks,
9682 lowpc, highpc, objfile);
9683
9684 /* For C++, set the block's scope. */
9685 if ((cu->language == language_cplus || cu->language == language_fortran)
9686 && cu->processing_has_namespace_info)
9687 block_set_scope (block, determine_prefix (die, cu),
9688 &objfile->objfile_obstack);
9689
9690 /* If we have address ranges, record them. */
9691 dwarf2_record_block_ranges (die, block, baseaddr, cu);
9692
9693 /* Attach template arguments to function. */
9694 if (! VEC_empty (symbolp, template_args))
9695 {
9696 gdb_assert (templ_func != NULL);
9697
9698 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
9699 templ_func->template_arguments
9700 = obstack_alloc (&objfile->objfile_obstack,
9701 (templ_func->n_template_arguments
9702 * sizeof (struct symbol *)));
9703 memcpy (templ_func->template_arguments,
9704 VEC_address (symbolp, template_args),
9705 (templ_func->n_template_arguments * sizeof (struct symbol *)));
9706 VEC_free (symbolp, template_args);
9707 }
9708
9709 /* In C++, we can have functions nested inside functions (e.g., when
9710 a function declares a class that has methods). This means that
9711 when we finish processing a function scope, we may need to go
9712 back to building a containing block's symbol lists. */
9713 local_symbols = new->locals;
9714 using_directives = new->using_directives;
9715
9716 /* If we've finished processing a top-level function, subsequent
9717 symbols go in the file symbol list. */
9718 if (outermost_context_p ())
9719 cu->list_in_scope = &file_symbols;
9720 }
9721
9722 /* Process all the DIES contained within a lexical block scope. Start
9723 a new scope, process the dies, and then close the scope. */
9724
9725 static void
9726 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
9727 {
9728 struct objfile *objfile = cu->objfile;
9729 struct context_stack *new;
9730 CORE_ADDR lowpc, highpc;
9731 struct die_info *child_die;
9732 CORE_ADDR baseaddr;
9733
9734 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9735
9736 /* Ignore blocks with missing or invalid low and high pc attributes. */
9737 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
9738 as multiple lexical blocks? Handling children in a sane way would
9739 be nasty. Might be easier to properly extend generic blocks to
9740 describe ranges. */
9741 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9742 return;
9743 lowpc += baseaddr;
9744 highpc += baseaddr;
9745
9746 push_context (0, lowpc);
9747 if (die->child != NULL)
9748 {
9749 child_die = die->child;
9750 while (child_die && child_die->tag)
9751 {
9752 process_die (child_die, cu);
9753 child_die = sibling_die (child_die);
9754 }
9755 }
9756 new = pop_context ();
9757
9758 if (local_symbols != NULL || using_directives != NULL)
9759 {
9760 struct block *block
9761 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
9762 highpc, objfile);
9763
9764 /* Note that recording ranges after traversing children, as we
9765 do here, means that recording a parent's ranges entails
9766 walking across all its children's ranges as they appear in
9767 the address map, which is quadratic behavior.
9768
9769 It would be nicer to record the parent's ranges before
9770 traversing its children, simply overriding whatever you find
9771 there. But since we don't even decide whether to create a
9772 block until after we've traversed its children, that's hard
9773 to do. */
9774 dwarf2_record_block_ranges (die, block, baseaddr, cu);
9775 }
9776 local_symbols = new->locals;
9777 using_directives = new->using_directives;
9778 }
9779
9780 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
9781
9782 static void
9783 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
9784 {
9785 struct objfile *objfile = cu->objfile;
9786 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9787 CORE_ADDR pc, baseaddr;
9788 struct attribute *attr;
9789 struct call_site *call_site, call_site_local;
9790 void **slot;
9791 int nparams;
9792 struct die_info *child_die;
9793
9794 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9795
9796 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9797 if (!attr)
9798 {
9799 complaint (&symfile_complaints,
9800 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
9801 "DIE 0x%x [in module %s]"),
9802 die->offset.sect_off, objfile->name);
9803 return;
9804 }
9805 pc = DW_ADDR (attr) + baseaddr;
9806
9807 if (cu->call_site_htab == NULL)
9808 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
9809 NULL, &objfile->objfile_obstack,
9810 hashtab_obstack_allocate, NULL);
9811 call_site_local.pc = pc;
9812 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
9813 if (*slot != NULL)
9814 {
9815 complaint (&symfile_complaints,
9816 _("Duplicate PC %s for DW_TAG_GNU_call_site "
9817 "DIE 0x%x [in module %s]"),
9818 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
9819 return;
9820 }
9821
9822 /* Count parameters at the caller. */
9823
9824 nparams = 0;
9825 for (child_die = die->child; child_die && child_die->tag;
9826 child_die = sibling_die (child_die))
9827 {
9828 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9829 {
9830 complaint (&symfile_complaints,
9831 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
9832 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9833 child_die->tag, child_die->offset.sect_off, objfile->name);
9834 continue;
9835 }
9836
9837 nparams++;
9838 }
9839
9840 call_site = obstack_alloc (&objfile->objfile_obstack,
9841 (sizeof (*call_site)
9842 + (sizeof (*call_site->parameter)
9843 * (nparams - 1))));
9844 *slot = call_site;
9845 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
9846 call_site->pc = pc;
9847
9848 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
9849 {
9850 struct die_info *func_die;
9851
9852 /* Skip also over DW_TAG_inlined_subroutine. */
9853 for (func_die = die->parent;
9854 func_die && func_die->tag != DW_TAG_subprogram
9855 && func_die->tag != DW_TAG_subroutine_type;
9856 func_die = func_die->parent);
9857
9858 /* DW_AT_GNU_all_call_sites is a superset
9859 of DW_AT_GNU_all_tail_call_sites. */
9860 if (func_die
9861 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
9862 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
9863 {
9864 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
9865 not complete. But keep CALL_SITE for look ups via call_site_htab,
9866 both the initial caller containing the real return address PC and
9867 the final callee containing the current PC of a chain of tail
9868 calls do not need to have the tail call list complete. But any
9869 function candidate for a virtual tail call frame searched via
9870 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
9871 determined unambiguously. */
9872 }
9873 else
9874 {
9875 struct type *func_type = NULL;
9876
9877 if (func_die)
9878 func_type = get_die_type (func_die, cu);
9879 if (func_type != NULL)
9880 {
9881 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
9882
9883 /* Enlist this call site to the function. */
9884 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
9885 TYPE_TAIL_CALL_LIST (func_type) = call_site;
9886 }
9887 else
9888 complaint (&symfile_complaints,
9889 _("Cannot find function owning DW_TAG_GNU_call_site "
9890 "DIE 0x%x [in module %s]"),
9891 die->offset.sect_off, objfile->name);
9892 }
9893 }
9894
9895 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
9896 if (attr == NULL)
9897 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9898 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9899 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
9900 /* Keep NULL DWARF_BLOCK. */;
9901 else if (attr_form_is_block (attr))
9902 {
9903 struct dwarf2_locexpr_baton *dlbaton;
9904
9905 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
9906 dlbaton->data = DW_BLOCK (attr)->data;
9907 dlbaton->size = DW_BLOCK (attr)->size;
9908 dlbaton->per_cu = cu->per_cu;
9909
9910 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
9911 }
9912 else if (is_ref_attr (attr))
9913 {
9914 struct dwarf2_cu *target_cu = cu;
9915 struct die_info *target_die;
9916
9917 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
9918 gdb_assert (target_cu->objfile == objfile);
9919 if (die_is_declaration (target_die, target_cu))
9920 {
9921 const char *target_physname = NULL;
9922 struct attribute *target_attr;
9923
9924 /* Prefer the mangled name; otherwise compute the demangled one. */
9925 target_attr = dwarf2_attr (target_die, DW_AT_linkage_name, target_cu);
9926 if (target_attr == NULL)
9927 target_attr = dwarf2_attr (target_die, DW_AT_MIPS_linkage_name,
9928 target_cu);
9929 if (target_attr != NULL && DW_STRING (target_attr) != NULL)
9930 target_physname = DW_STRING (target_attr);
9931 else
9932 target_physname = dwarf2_physname (NULL, target_die, target_cu);
9933 if (target_physname == NULL)
9934 complaint (&symfile_complaints,
9935 _("DW_AT_GNU_call_site_target target DIE has invalid "
9936 "physname, for referencing DIE 0x%x [in module %s]"),
9937 die->offset.sect_off, objfile->name);
9938 else
9939 SET_FIELD_PHYSNAME (call_site->target, target_physname);
9940 }
9941 else
9942 {
9943 CORE_ADDR lowpc;
9944
9945 /* DW_AT_entry_pc should be preferred. */
9946 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
9947 complaint (&symfile_complaints,
9948 _("DW_AT_GNU_call_site_target target DIE has invalid "
9949 "low pc, for referencing DIE 0x%x [in module %s]"),
9950 die->offset.sect_off, objfile->name);
9951 else
9952 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
9953 }
9954 }
9955 else
9956 complaint (&symfile_complaints,
9957 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
9958 "block nor reference, for DIE 0x%x [in module %s]"),
9959 die->offset.sect_off, objfile->name);
9960
9961 call_site->per_cu = cu->per_cu;
9962
9963 for (child_die = die->child;
9964 child_die && child_die->tag;
9965 child_die = sibling_die (child_die))
9966 {
9967 struct call_site_parameter *parameter;
9968 struct attribute *loc, *origin;
9969
9970 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9971 {
9972 /* Already printed the complaint above. */
9973 continue;
9974 }
9975
9976 gdb_assert (call_site->parameter_count < nparams);
9977 parameter = &call_site->parameter[call_site->parameter_count];
9978
9979 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
9980 specifies DW_TAG_formal_parameter. Value of the data assumed for the
9981 register is contained in DW_AT_GNU_call_site_value. */
9982
9983 loc = dwarf2_attr (child_die, DW_AT_location, cu);
9984 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
9985 if (loc == NULL && origin != NULL && is_ref_attr (origin))
9986 {
9987 sect_offset offset;
9988
9989 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9990 offset = dwarf2_get_ref_die_offset (origin);
9991 if (!offset_in_cu_p (&cu->header, offset))
9992 {
9993 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
9994 binding can be done only inside one CU. Such referenced DIE
9995 therefore cannot be even moved to DW_TAG_partial_unit. */
9996 complaint (&symfile_complaints,
9997 _("DW_AT_abstract_origin offset is not in CU for "
9998 "DW_TAG_GNU_call_site child DIE 0x%x "
9999 "[in module %s]"),
10000 child_die->offset.sect_off, objfile->name);
10001 continue;
10002 }
10003 parameter->u.param_offset.cu_off = (offset.sect_off
10004 - cu->header.offset.sect_off);
10005 }
10006 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
10007 {
10008 complaint (&symfile_complaints,
10009 _("No DW_FORM_block* DW_AT_location for "
10010 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
10011 child_die->offset.sect_off, objfile->name);
10012 continue;
10013 }
10014 else
10015 {
10016 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
10017 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
10018 if (parameter->u.dwarf_reg != -1)
10019 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
10020 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
10021 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
10022 &parameter->u.fb_offset))
10023 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
10024 else
10025 {
10026 complaint (&symfile_complaints,
10027 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
10028 "for DW_FORM_block* DW_AT_location is supported for "
10029 "DW_TAG_GNU_call_site child DIE 0x%x "
10030 "[in module %s]"),
10031 child_die->offset.sect_off, objfile->name);
10032 continue;
10033 }
10034 }
10035
10036 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
10037 if (!attr_form_is_block (attr))
10038 {
10039 complaint (&symfile_complaints,
10040 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
10041 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
10042 child_die->offset.sect_off, objfile->name);
10043 continue;
10044 }
10045 parameter->value = DW_BLOCK (attr)->data;
10046 parameter->value_size = DW_BLOCK (attr)->size;
10047
10048 /* Parameters are not pre-cleared by memset above. */
10049 parameter->data_value = NULL;
10050 parameter->data_value_size = 0;
10051 call_site->parameter_count++;
10052
10053 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
10054 if (attr)
10055 {
10056 if (!attr_form_is_block (attr))
10057 complaint (&symfile_complaints,
10058 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
10059 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
10060 child_die->offset.sect_off, objfile->name);
10061 else
10062 {
10063 parameter->data_value = DW_BLOCK (attr)->data;
10064 parameter->data_value_size = DW_BLOCK (attr)->size;
10065 }
10066 }
10067 }
10068 }
10069
10070 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
10071 Return 1 if the attributes are present and valid, otherwise, return 0.
10072 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
10073
10074 static int
10075 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
10076 CORE_ADDR *high_return, struct dwarf2_cu *cu,
10077 struct partial_symtab *ranges_pst)
10078 {
10079 struct objfile *objfile = cu->objfile;
10080 struct comp_unit_head *cu_header = &cu->header;
10081 bfd *obfd = objfile->obfd;
10082 unsigned int addr_size = cu_header->addr_size;
10083 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10084 /* Base address selection entry. */
10085 CORE_ADDR base;
10086 int found_base;
10087 unsigned int dummy;
10088 gdb_byte *buffer;
10089 CORE_ADDR marker;
10090 int low_set;
10091 CORE_ADDR low = 0;
10092 CORE_ADDR high = 0;
10093 CORE_ADDR baseaddr;
10094
10095 found_base = cu->base_known;
10096 base = cu->base_address;
10097
10098 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
10099 if (offset >= dwarf2_per_objfile->ranges.size)
10100 {
10101 complaint (&symfile_complaints,
10102 _("Offset %d out of bounds for DW_AT_ranges attribute"),
10103 offset);
10104 return 0;
10105 }
10106 buffer = dwarf2_per_objfile->ranges.buffer + offset;
10107
10108 /* Read in the largest possible address. */
10109 marker = read_address (obfd, buffer, cu, &dummy);
10110 if ((marker & mask) == mask)
10111 {
10112 /* If we found the largest possible address, then
10113 read the base address. */
10114 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10115 buffer += 2 * addr_size;
10116 offset += 2 * addr_size;
10117 found_base = 1;
10118 }
10119
10120 low_set = 0;
10121
10122 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10123
10124 while (1)
10125 {
10126 CORE_ADDR range_beginning, range_end;
10127
10128 range_beginning = read_address (obfd, buffer, cu, &dummy);
10129 buffer += addr_size;
10130 range_end = read_address (obfd, buffer, cu, &dummy);
10131 buffer += addr_size;
10132 offset += 2 * addr_size;
10133
10134 /* An end of list marker is a pair of zero addresses. */
10135 if (range_beginning == 0 && range_end == 0)
10136 /* Found the end of list entry. */
10137 break;
10138
10139 /* Each base address selection entry is a pair of 2 values.
10140 The first is the largest possible address, the second is
10141 the base address. Check for a base address here. */
10142 if ((range_beginning & mask) == mask)
10143 {
10144 /* If we found the largest possible address, then
10145 read the base address. */
10146 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10147 found_base = 1;
10148 continue;
10149 }
10150
10151 if (!found_base)
10152 {
10153 /* We have no valid base address for the ranges
10154 data. */
10155 complaint (&symfile_complaints,
10156 _("Invalid .debug_ranges data (no base address)"));
10157 return 0;
10158 }
10159
10160 if (range_beginning > range_end)
10161 {
10162 /* Inverted range entries are invalid. */
10163 complaint (&symfile_complaints,
10164 _("Invalid .debug_ranges data (inverted range)"));
10165 return 0;
10166 }
10167
10168 /* Empty range entries have no effect. */
10169 if (range_beginning == range_end)
10170 continue;
10171
10172 range_beginning += base;
10173 range_end += base;
10174
10175 /* A not-uncommon case of bad debug info.
10176 Don't pollute the addrmap with bad data. */
10177 if (range_beginning + baseaddr == 0
10178 && !dwarf2_per_objfile->has_section_at_zero)
10179 {
10180 complaint (&symfile_complaints,
10181 _(".debug_ranges entry has start address of zero"
10182 " [in module %s]"), objfile->name);
10183 continue;
10184 }
10185
10186 if (ranges_pst != NULL)
10187 addrmap_set_empty (objfile->psymtabs_addrmap,
10188 range_beginning + baseaddr,
10189 range_end - 1 + baseaddr,
10190 ranges_pst);
10191
10192 /* FIXME: This is recording everything as a low-high
10193 segment of consecutive addresses. We should have a
10194 data structure for discontiguous block ranges
10195 instead. */
10196 if (! low_set)
10197 {
10198 low = range_beginning;
10199 high = range_end;
10200 low_set = 1;
10201 }
10202 else
10203 {
10204 if (range_beginning < low)
10205 low = range_beginning;
10206 if (range_end > high)
10207 high = range_end;
10208 }
10209 }
10210
10211 if (! low_set)
10212 /* If the first entry is an end-of-list marker, the range
10213 describes an empty scope, i.e. no instructions. */
10214 return 0;
10215
10216 if (low_return)
10217 *low_return = low;
10218 if (high_return)
10219 *high_return = high;
10220 return 1;
10221 }
10222
10223 /* Get low and high pc attributes from a die. Return 1 if the attributes
10224 are present and valid, otherwise, return 0. Return -1 if the range is
10225 discontinuous, i.e. derived from DW_AT_ranges information. */
10226
10227 static int
10228 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
10229 CORE_ADDR *highpc, struct dwarf2_cu *cu,
10230 struct partial_symtab *pst)
10231 {
10232 struct attribute *attr;
10233 struct attribute *attr_high;
10234 CORE_ADDR low = 0;
10235 CORE_ADDR high = 0;
10236 int ret = 0;
10237
10238 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10239 if (attr_high)
10240 {
10241 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10242 if (attr)
10243 {
10244 low = DW_ADDR (attr);
10245 if (attr_high->form == DW_FORM_addr
10246 || attr_high->form == DW_FORM_GNU_addr_index)
10247 high = DW_ADDR (attr_high);
10248 else
10249 high = low + DW_UNSND (attr_high);
10250 }
10251 else
10252 /* Found high w/o low attribute. */
10253 return 0;
10254
10255 /* Found consecutive range of addresses. */
10256 ret = 1;
10257 }
10258 else
10259 {
10260 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10261 if (attr != NULL)
10262 {
10263 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10264 We take advantage of the fact that DW_AT_ranges does not appear
10265 in DW_TAG_compile_unit of DWO files. */
10266 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10267 unsigned int ranges_offset = (DW_UNSND (attr)
10268 + (need_ranges_base
10269 ? cu->ranges_base
10270 : 0));
10271
10272 /* Value of the DW_AT_ranges attribute is the offset in the
10273 .debug_ranges section. */
10274 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
10275 return 0;
10276 /* Found discontinuous range of addresses. */
10277 ret = -1;
10278 }
10279 }
10280
10281 /* read_partial_die has also the strict LOW < HIGH requirement. */
10282 if (high <= low)
10283 return 0;
10284
10285 /* When using the GNU linker, .gnu.linkonce. sections are used to
10286 eliminate duplicate copies of functions and vtables and such.
10287 The linker will arbitrarily choose one and discard the others.
10288 The AT_*_pc values for such functions refer to local labels in
10289 these sections. If the section from that file was discarded, the
10290 labels are not in the output, so the relocs get a value of 0.
10291 If this is a discarded function, mark the pc bounds as invalid,
10292 so that GDB will ignore it. */
10293 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
10294 return 0;
10295
10296 *lowpc = low;
10297 if (highpc)
10298 *highpc = high;
10299 return ret;
10300 }
10301
10302 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
10303 its low and high PC addresses. Do nothing if these addresses could not
10304 be determined. Otherwise, set LOWPC to the low address if it is smaller,
10305 and HIGHPC to the high address if greater than HIGHPC. */
10306
10307 static void
10308 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
10309 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10310 struct dwarf2_cu *cu)
10311 {
10312 CORE_ADDR low, high;
10313 struct die_info *child = die->child;
10314
10315 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
10316 {
10317 *lowpc = min (*lowpc, low);
10318 *highpc = max (*highpc, high);
10319 }
10320
10321 /* If the language does not allow nested subprograms (either inside
10322 subprograms or lexical blocks), we're done. */
10323 if (cu->language != language_ada)
10324 return;
10325
10326 /* Check all the children of the given DIE. If it contains nested
10327 subprograms, then check their pc bounds. Likewise, we need to
10328 check lexical blocks as well, as they may also contain subprogram
10329 definitions. */
10330 while (child && child->tag)
10331 {
10332 if (child->tag == DW_TAG_subprogram
10333 || child->tag == DW_TAG_lexical_block)
10334 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
10335 child = sibling_die (child);
10336 }
10337 }
10338
10339 /* Get the low and high pc's represented by the scope DIE, and store
10340 them in *LOWPC and *HIGHPC. If the correct values can't be
10341 determined, set *LOWPC to -1 and *HIGHPC to 0. */
10342
10343 static void
10344 get_scope_pc_bounds (struct die_info *die,
10345 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10346 struct dwarf2_cu *cu)
10347 {
10348 CORE_ADDR best_low = (CORE_ADDR) -1;
10349 CORE_ADDR best_high = (CORE_ADDR) 0;
10350 CORE_ADDR current_low, current_high;
10351
10352 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
10353 {
10354 best_low = current_low;
10355 best_high = current_high;
10356 }
10357 else
10358 {
10359 struct die_info *child = die->child;
10360
10361 while (child && child->tag)
10362 {
10363 switch (child->tag) {
10364 case DW_TAG_subprogram:
10365 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
10366 break;
10367 case DW_TAG_namespace:
10368 case DW_TAG_module:
10369 /* FIXME: carlton/2004-01-16: Should we do this for
10370 DW_TAG_class_type/DW_TAG_structure_type, too? I think
10371 that current GCC's always emit the DIEs corresponding
10372 to definitions of methods of classes as children of a
10373 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
10374 the DIEs giving the declarations, which could be
10375 anywhere). But I don't see any reason why the
10376 standards says that they have to be there. */
10377 get_scope_pc_bounds (child, &current_low, &current_high, cu);
10378
10379 if (current_low != ((CORE_ADDR) -1))
10380 {
10381 best_low = min (best_low, current_low);
10382 best_high = max (best_high, current_high);
10383 }
10384 break;
10385 default:
10386 /* Ignore. */
10387 break;
10388 }
10389
10390 child = sibling_die (child);
10391 }
10392 }
10393
10394 *lowpc = best_low;
10395 *highpc = best_high;
10396 }
10397
10398 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
10399 in DIE. */
10400
10401 static void
10402 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
10403 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
10404 {
10405 struct objfile *objfile = cu->objfile;
10406 struct attribute *attr;
10407 struct attribute *attr_high;
10408
10409 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10410 if (attr_high)
10411 {
10412 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10413 if (attr)
10414 {
10415 CORE_ADDR low = DW_ADDR (attr);
10416 CORE_ADDR high;
10417 if (attr_high->form == DW_FORM_addr
10418 || attr_high->form == DW_FORM_GNU_addr_index)
10419 high = DW_ADDR (attr_high);
10420 else
10421 high = low + DW_UNSND (attr_high);
10422
10423 record_block_range (block, baseaddr + low, baseaddr + high - 1);
10424 }
10425 }
10426
10427 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10428 if (attr)
10429 {
10430 bfd *obfd = objfile->obfd;
10431 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10432 We take advantage of the fact that DW_AT_ranges does not appear
10433 in DW_TAG_compile_unit of DWO files. */
10434 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10435
10436 /* The value of the DW_AT_ranges attribute is the offset of the
10437 address range list in the .debug_ranges section. */
10438 unsigned long offset = (DW_UNSND (attr)
10439 + (need_ranges_base ? cu->ranges_base : 0));
10440 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
10441
10442 /* For some target architectures, but not others, the
10443 read_address function sign-extends the addresses it returns.
10444 To recognize base address selection entries, we need a
10445 mask. */
10446 unsigned int addr_size = cu->header.addr_size;
10447 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10448
10449 /* The base address, to which the next pair is relative. Note
10450 that this 'base' is a DWARF concept: most entries in a range
10451 list are relative, to reduce the number of relocs against the
10452 debugging information. This is separate from this function's
10453 'baseaddr' argument, which GDB uses to relocate debugging
10454 information from a shared library based on the address at
10455 which the library was loaded. */
10456 CORE_ADDR base = cu->base_address;
10457 int base_known = cu->base_known;
10458
10459 gdb_assert (dwarf2_per_objfile->ranges.readin);
10460 if (offset >= dwarf2_per_objfile->ranges.size)
10461 {
10462 complaint (&symfile_complaints,
10463 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
10464 offset);
10465 return;
10466 }
10467
10468 for (;;)
10469 {
10470 unsigned int bytes_read;
10471 CORE_ADDR start, end;
10472
10473 start = read_address (obfd, buffer, cu, &bytes_read);
10474 buffer += bytes_read;
10475 end = read_address (obfd, buffer, cu, &bytes_read);
10476 buffer += bytes_read;
10477
10478 /* Did we find the end of the range list? */
10479 if (start == 0 && end == 0)
10480 break;
10481
10482 /* Did we find a base address selection entry? */
10483 else if ((start & base_select_mask) == base_select_mask)
10484 {
10485 base = end;
10486 base_known = 1;
10487 }
10488
10489 /* We found an ordinary address range. */
10490 else
10491 {
10492 if (!base_known)
10493 {
10494 complaint (&symfile_complaints,
10495 _("Invalid .debug_ranges data "
10496 "(no base address)"));
10497 return;
10498 }
10499
10500 if (start > end)
10501 {
10502 /* Inverted range entries are invalid. */
10503 complaint (&symfile_complaints,
10504 _("Invalid .debug_ranges data "
10505 "(inverted range)"));
10506 return;
10507 }
10508
10509 /* Empty range entries have no effect. */
10510 if (start == end)
10511 continue;
10512
10513 start += base + baseaddr;
10514 end += base + baseaddr;
10515
10516 /* A not-uncommon case of bad debug info.
10517 Don't pollute the addrmap with bad data. */
10518 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
10519 {
10520 complaint (&symfile_complaints,
10521 _(".debug_ranges entry has start address of zero"
10522 " [in module %s]"), objfile->name);
10523 continue;
10524 }
10525
10526 record_block_range (block, start, end - 1);
10527 }
10528 }
10529 }
10530 }
10531
10532 /* Check whether the producer field indicates either of GCC < 4.6, or the
10533 Intel C/C++ compiler, and cache the result in CU. */
10534
10535 static void
10536 check_producer (struct dwarf2_cu *cu)
10537 {
10538 const char *cs;
10539 int major, minor, release;
10540
10541 if (cu->producer == NULL)
10542 {
10543 /* For unknown compilers expect their behavior is DWARF version
10544 compliant.
10545
10546 GCC started to support .debug_types sections by -gdwarf-4 since
10547 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
10548 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
10549 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
10550 interpreted incorrectly by GDB now - GCC PR debug/48229. */
10551 }
10552 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
10553 {
10554 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
10555
10556 cs = &cu->producer[strlen ("GNU ")];
10557 while (*cs && !isdigit (*cs))
10558 cs++;
10559 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
10560 {
10561 /* Not recognized as GCC. */
10562 }
10563 else
10564 {
10565 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
10566 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
10567 }
10568 }
10569 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
10570 cu->producer_is_icc = 1;
10571 else
10572 {
10573 /* For other non-GCC compilers, expect their behavior is DWARF version
10574 compliant. */
10575 }
10576
10577 cu->checked_producer = 1;
10578 }
10579
10580 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
10581 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
10582 during 4.6.0 experimental. */
10583
10584 static int
10585 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
10586 {
10587 if (!cu->checked_producer)
10588 check_producer (cu);
10589
10590 return cu->producer_is_gxx_lt_4_6;
10591 }
10592
10593 /* Return the default accessibility type if it is not overriden by
10594 DW_AT_accessibility. */
10595
10596 static enum dwarf_access_attribute
10597 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
10598 {
10599 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
10600 {
10601 /* The default DWARF 2 accessibility for members is public, the default
10602 accessibility for inheritance is private. */
10603
10604 if (die->tag != DW_TAG_inheritance)
10605 return DW_ACCESS_public;
10606 else
10607 return DW_ACCESS_private;
10608 }
10609 else
10610 {
10611 /* DWARF 3+ defines the default accessibility a different way. The same
10612 rules apply now for DW_TAG_inheritance as for the members and it only
10613 depends on the container kind. */
10614
10615 if (die->parent->tag == DW_TAG_class_type)
10616 return DW_ACCESS_private;
10617 else
10618 return DW_ACCESS_public;
10619 }
10620 }
10621
10622 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
10623 offset. If the attribute was not found return 0, otherwise return
10624 1. If it was found but could not properly be handled, set *OFFSET
10625 to 0. */
10626
10627 static int
10628 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
10629 LONGEST *offset)
10630 {
10631 struct attribute *attr;
10632
10633 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
10634 if (attr != NULL)
10635 {
10636 *offset = 0;
10637
10638 /* Note that we do not check for a section offset first here.
10639 This is because DW_AT_data_member_location is new in DWARF 4,
10640 so if we see it, we can assume that a constant form is really
10641 a constant and not a section offset. */
10642 if (attr_form_is_constant (attr))
10643 *offset = dwarf2_get_attr_constant_value (attr, 0);
10644 else if (attr_form_is_section_offset (attr))
10645 dwarf2_complex_location_expr_complaint ();
10646 else if (attr_form_is_block (attr))
10647 *offset = decode_locdesc (DW_BLOCK (attr), cu);
10648 else
10649 dwarf2_complex_location_expr_complaint ();
10650
10651 return 1;
10652 }
10653
10654 return 0;
10655 }
10656
10657 /* Add an aggregate field to the field list. */
10658
10659 static void
10660 dwarf2_add_field (struct field_info *fip, struct die_info *die,
10661 struct dwarf2_cu *cu)
10662 {
10663 struct objfile *objfile = cu->objfile;
10664 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10665 struct nextfield *new_field;
10666 struct attribute *attr;
10667 struct field *fp;
10668 const char *fieldname = "";
10669
10670 /* Allocate a new field list entry and link it in. */
10671 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
10672 make_cleanup (xfree, new_field);
10673 memset (new_field, 0, sizeof (struct nextfield));
10674
10675 if (die->tag == DW_TAG_inheritance)
10676 {
10677 new_field->next = fip->baseclasses;
10678 fip->baseclasses = new_field;
10679 }
10680 else
10681 {
10682 new_field->next = fip->fields;
10683 fip->fields = new_field;
10684 }
10685 fip->nfields++;
10686
10687 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
10688 if (attr)
10689 new_field->accessibility = DW_UNSND (attr);
10690 else
10691 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
10692 if (new_field->accessibility != DW_ACCESS_public)
10693 fip->non_public_fields = 1;
10694
10695 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
10696 if (attr)
10697 new_field->virtuality = DW_UNSND (attr);
10698 else
10699 new_field->virtuality = DW_VIRTUALITY_none;
10700
10701 fp = &new_field->field;
10702
10703 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
10704 {
10705 LONGEST offset;
10706
10707 /* Data member other than a C++ static data member. */
10708
10709 /* Get type of field. */
10710 fp->type = die_type (die, cu);
10711
10712 SET_FIELD_BITPOS (*fp, 0);
10713
10714 /* Get bit size of field (zero if none). */
10715 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
10716 if (attr)
10717 {
10718 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
10719 }
10720 else
10721 {
10722 FIELD_BITSIZE (*fp) = 0;
10723 }
10724
10725 /* Get bit offset of field. */
10726 if (handle_data_member_location (die, cu, &offset))
10727 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
10728 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
10729 if (attr)
10730 {
10731 if (gdbarch_bits_big_endian (gdbarch))
10732 {
10733 /* For big endian bits, the DW_AT_bit_offset gives the
10734 additional bit offset from the MSB of the containing
10735 anonymous object to the MSB of the field. We don't
10736 have to do anything special since we don't need to
10737 know the size of the anonymous object. */
10738 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
10739 }
10740 else
10741 {
10742 /* For little endian bits, compute the bit offset to the
10743 MSB of the anonymous object, subtract off the number of
10744 bits from the MSB of the field to the MSB of the
10745 object, and then subtract off the number of bits of
10746 the field itself. The result is the bit offset of
10747 the LSB of the field. */
10748 int anonymous_size;
10749 int bit_offset = DW_UNSND (attr);
10750
10751 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10752 if (attr)
10753 {
10754 /* The size of the anonymous object containing
10755 the bit field is explicit, so use the
10756 indicated size (in bytes). */
10757 anonymous_size = DW_UNSND (attr);
10758 }
10759 else
10760 {
10761 /* The size of the anonymous object containing
10762 the bit field must be inferred from the type
10763 attribute of the data member containing the
10764 bit field. */
10765 anonymous_size = TYPE_LENGTH (fp->type);
10766 }
10767 SET_FIELD_BITPOS (*fp,
10768 (FIELD_BITPOS (*fp)
10769 + anonymous_size * bits_per_byte
10770 - bit_offset - FIELD_BITSIZE (*fp)));
10771 }
10772 }
10773
10774 /* Get name of field. */
10775 fieldname = dwarf2_name (die, cu);
10776 if (fieldname == NULL)
10777 fieldname = "";
10778
10779 /* The name is already allocated along with this objfile, so we don't
10780 need to duplicate it for the type. */
10781 fp->name = fieldname;
10782
10783 /* Change accessibility for artificial fields (e.g. virtual table
10784 pointer or virtual base class pointer) to private. */
10785 if (dwarf2_attr (die, DW_AT_artificial, cu))
10786 {
10787 FIELD_ARTIFICIAL (*fp) = 1;
10788 new_field->accessibility = DW_ACCESS_private;
10789 fip->non_public_fields = 1;
10790 }
10791 }
10792 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
10793 {
10794 /* C++ static member. */
10795
10796 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
10797 is a declaration, but all versions of G++ as of this writing
10798 (so through at least 3.2.1) incorrectly generate
10799 DW_TAG_variable tags. */
10800
10801 const char *physname;
10802
10803 /* Get name of field. */
10804 fieldname = dwarf2_name (die, cu);
10805 if (fieldname == NULL)
10806 return;
10807
10808 attr = dwarf2_attr (die, DW_AT_const_value, cu);
10809 if (attr
10810 /* Only create a symbol if this is an external value.
10811 new_symbol checks this and puts the value in the global symbol
10812 table, which we want. If it is not external, new_symbol
10813 will try to put the value in cu->list_in_scope which is wrong. */
10814 && dwarf2_flag_true_p (die, DW_AT_external, cu))
10815 {
10816 /* A static const member, not much different than an enum as far as
10817 we're concerned, except that we can support more types. */
10818 new_symbol (die, NULL, cu);
10819 }
10820
10821 /* Get physical name. */
10822 physname = dwarf2_physname (fieldname, die, cu);
10823
10824 /* The name is already allocated along with this objfile, so we don't
10825 need to duplicate it for the type. */
10826 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
10827 FIELD_TYPE (*fp) = die_type (die, cu);
10828 FIELD_NAME (*fp) = fieldname;
10829 }
10830 else if (die->tag == DW_TAG_inheritance)
10831 {
10832 LONGEST offset;
10833
10834 /* C++ base class field. */
10835 if (handle_data_member_location (die, cu, &offset))
10836 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
10837 FIELD_BITSIZE (*fp) = 0;
10838 FIELD_TYPE (*fp) = die_type (die, cu);
10839 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
10840 fip->nbaseclasses++;
10841 }
10842 }
10843
10844 /* Add a typedef defined in the scope of the FIP's class. */
10845
10846 static void
10847 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
10848 struct dwarf2_cu *cu)
10849 {
10850 struct objfile *objfile = cu->objfile;
10851 struct typedef_field_list *new_field;
10852 struct attribute *attr;
10853 struct typedef_field *fp;
10854 char *fieldname = "";
10855
10856 /* Allocate a new field list entry and link it in. */
10857 new_field = xzalloc (sizeof (*new_field));
10858 make_cleanup (xfree, new_field);
10859
10860 gdb_assert (die->tag == DW_TAG_typedef);
10861
10862 fp = &new_field->field;
10863
10864 /* Get name of field. */
10865 fp->name = dwarf2_name (die, cu);
10866 if (fp->name == NULL)
10867 return;
10868
10869 fp->type = read_type_die (die, cu);
10870
10871 new_field->next = fip->typedef_field_list;
10872 fip->typedef_field_list = new_field;
10873 fip->typedef_field_list_count++;
10874 }
10875
10876 /* Create the vector of fields, and attach it to the type. */
10877
10878 static void
10879 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
10880 struct dwarf2_cu *cu)
10881 {
10882 int nfields = fip->nfields;
10883
10884 /* Record the field count, allocate space for the array of fields,
10885 and create blank accessibility bitfields if necessary. */
10886 TYPE_NFIELDS (type) = nfields;
10887 TYPE_FIELDS (type) = (struct field *)
10888 TYPE_ALLOC (type, sizeof (struct field) * nfields);
10889 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
10890
10891 if (fip->non_public_fields && cu->language != language_ada)
10892 {
10893 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10894
10895 TYPE_FIELD_PRIVATE_BITS (type) =
10896 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10897 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
10898
10899 TYPE_FIELD_PROTECTED_BITS (type) =
10900 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10901 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
10902
10903 TYPE_FIELD_IGNORE_BITS (type) =
10904 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10905 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
10906 }
10907
10908 /* If the type has baseclasses, allocate and clear a bit vector for
10909 TYPE_FIELD_VIRTUAL_BITS. */
10910 if (fip->nbaseclasses && cu->language != language_ada)
10911 {
10912 int num_bytes = B_BYTES (fip->nbaseclasses);
10913 unsigned char *pointer;
10914
10915 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10916 pointer = TYPE_ALLOC (type, num_bytes);
10917 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
10918 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
10919 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
10920 }
10921
10922 /* Copy the saved-up fields into the field vector. Start from the head of
10923 the list, adding to the tail of the field array, so that they end up in
10924 the same order in the array in which they were added to the list. */
10925 while (nfields-- > 0)
10926 {
10927 struct nextfield *fieldp;
10928
10929 if (fip->fields)
10930 {
10931 fieldp = fip->fields;
10932 fip->fields = fieldp->next;
10933 }
10934 else
10935 {
10936 fieldp = fip->baseclasses;
10937 fip->baseclasses = fieldp->next;
10938 }
10939
10940 TYPE_FIELD (type, nfields) = fieldp->field;
10941 switch (fieldp->accessibility)
10942 {
10943 case DW_ACCESS_private:
10944 if (cu->language != language_ada)
10945 SET_TYPE_FIELD_PRIVATE (type, nfields);
10946 break;
10947
10948 case DW_ACCESS_protected:
10949 if (cu->language != language_ada)
10950 SET_TYPE_FIELD_PROTECTED (type, nfields);
10951 break;
10952
10953 case DW_ACCESS_public:
10954 break;
10955
10956 default:
10957 /* Unknown accessibility. Complain and treat it as public. */
10958 {
10959 complaint (&symfile_complaints, _("unsupported accessibility %d"),
10960 fieldp->accessibility);
10961 }
10962 break;
10963 }
10964 if (nfields < fip->nbaseclasses)
10965 {
10966 switch (fieldp->virtuality)
10967 {
10968 case DW_VIRTUALITY_virtual:
10969 case DW_VIRTUALITY_pure_virtual:
10970 if (cu->language == language_ada)
10971 error (_("unexpected virtuality in component of Ada type"));
10972 SET_TYPE_FIELD_VIRTUAL (type, nfields);
10973 break;
10974 }
10975 }
10976 }
10977 }
10978
10979 /* Return true if this member function is a constructor, false
10980 otherwise. */
10981
10982 static int
10983 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
10984 {
10985 const char *fieldname;
10986 const char *typename;
10987 int len;
10988
10989 if (die->parent == NULL)
10990 return 0;
10991
10992 if (die->parent->tag != DW_TAG_structure_type
10993 && die->parent->tag != DW_TAG_union_type
10994 && die->parent->tag != DW_TAG_class_type)
10995 return 0;
10996
10997 fieldname = dwarf2_name (die, cu);
10998 typename = dwarf2_name (die->parent, cu);
10999 if (fieldname == NULL || typename == NULL)
11000 return 0;
11001
11002 len = strlen (fieldname);
11003 return (strncmp (fieldname, typename, len) == 0
11004 && (typename[len] == '\0' || typename[len] == '<'));
11005 }
11006
11007 /* Add a member function to the proper fieldlist. */
11008
11009 static void
11010 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
11011 struct type *type, struct dwarf2_cu *cu)
11012 {
11013 struct objfile *objfile = cu->objfile;
11014 struct attribute *attr;
11015 struct fnfieldlist *flp;
11016 int i;
11017 struct fn_field *fnp;
11018 const char *fieldname;
11019 struct nextfnfield *new_fnfield;
11020 struct type *this_type;
11021 enum dwarf_access_attribute accessibility;
11022
11023 if (cu->language == language_ada)
11024 error (_("unexpected member function in Ada type"));
11025
11026 /* Get name of member function. */
11027 fieldname = dwarf2_name (die, cu);
11028 if (fieldname == NULL)
11029 return;
11030
11031 /* Look up member function name in fieldlist. */
11032 for (i = 0; i < fip->nfnfields; i++)
11033 {
11034 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
11035 break;
11036 }
11037
11038 /* Create new list element if necessary. */
11039 if (i < fip->nfnfields)
11040 flp = &fip->fnfieldlists[i];
11041 else
11042 {
11043 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
11044 {
11045 fip->fnfieldlists = (struct fnfieldlist *)
11046 xrealloc (fip->fnfieldlists,
11047 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
11048 * sizeof (struct fnfieldlist));
11049 if (fip->nfnfields == 0)
11050 make_cleanup (free_current_contents, &fip->fnfieldlists);
11051 }
11052 flp = &fip->fnfieldlists[fip->nfnfields];
11053 flp->name = fieldname;
11054 flp->length = 0;
11055 flp->head = NULL;
11056 i = fip->nfnfields++;
11057 }
11058
11059 /* Create a new member function field and chain it to the field list
11060 entry. */
11061 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
11062 make_cleanup (xfree, new_fnfield);
11063 memset (new_fnfield, 0, sizeof (struct nextfnfield));
11064 new_fnfield->next = flp->head;
11065 flp->head = new_fnfield;
11066 flp->length++;
11067
11068 /* Fill in the member function field info. */
11069 fnp = &new_fnfield->fnfield;
11070
11071 /* Delay processing of the physname until later. */
11072 if (cu->language == language_cplus || cu->language == language_java)
11073 {
11074 add_to_method_list (type, i, flp->length - 1, fieldname,
11075 die, cu);
11076 }
11077 else
11078 {
11079 const char *physname = dwarf2_physname (fieldname, die, cu);
11080 fnp->physname = physname ? physname : "";
11081 }
11082
11083 fnp->type = alloc_type (objfile);
11084 this_type = read_type_die (die, cu);
11085 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
11086 {
11087 int nparams = TYPE_NFIELDS (this_type);
11088
11089 /* TYPE is the domain of this method, and THIS_TYPE is the type
11090 of the method itself (TYPE_CODE_METHOD). */
11091 smash_to_method_type (fnp->type, type,
11092 TYPE_TARGET_TYPE (this_type),
11093 TYPE_FIELDS (this_type),
11094 TYPE_NFIELDS (this_type),
11095 TYPE_VARARGS (this_type));
11096
11097 /* Handle static member functions.
11098 Dwarf2 has no clean way to discern C++ static and non-static
11099 member functions. G++ helps GDB by marking the first
11100 parameter for non-static member functions (which is the this
11101 pointer) as artificial. We obtain this information from
11102 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
11103 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
11104 fnp->voffset = VOFFSET_STATIC;
11105 }
11106 else
11107 complaint (&symfile_complaints, _("member function type missing for '%s'"),
11108 dwarf2_full_name (fieldname, die, cu));
11109
11110 /* Get fcontext from DW_AT_containing_type if present. */
11111 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
11112 fnp->fcontext = die_containing_type (die, cu);
11113
11114 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
11115 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
11116
11117 /* Get accessibility. */
11118 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
11119 if (attr)
11120 accessibility = DW_UNSND (attr);
11121 else
11122 accessibility = dwarf2_default_access_attribute (die, cu);
11123 switch (accessibility)
11124 {
11125 case DW_ACCESS_private:
11126 fnp->is_private = 1;
11127 break;
11128 case DW_ACCESS_protected:
11129 fnp->is_protected = 1;
11130 break;
11131 }
11132
11133 /* Check for artificial methods. */
11134 attr = dwarf2_attr (die, DW_AT_artificial, cu);
11135 if (attr && DW_UNSND (attr) != 0)
11136 fnp->is_artificial = 1;
11137
11138 fnp->is_constructor = dwarf2_is_constructor (die, cu);
11139
11140 /* Get index in virtual function table if it is a virtual member
11141 function. For older versions of GCC, this is an offset in the
11142 appropriate virtual table, as specified by DW_AT_containing_type.
11143 For everyone else, it is an expression to be evaluated relative
11144 to the object address. */
11145
11146 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
11147 if (attr)
11148 {
11149 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
11150 {
11151 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
11152 {
11153 /* Old-style GCC. */
11154 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
11155 }
11156 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
11157 || (DW_BLOCK (attr)->size > 1
11158 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
11159 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
11160 {
11161 struct dwarf_block blk;
11162 int offset;
11163
11164 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
11165 ? 1 : 2);
11166 blk.size = DW_BLOCK (attr)->size - offset;
11167 blk.data = DW_BLOCK (attr)->data + offset;
11168 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
11169 if ((fnp->voffset % cu->header.addr_size) != 0)
11170 dwarf2_complex_location_expr_complaint ();
11171 else
11172 fnp->voffset /= cu->header.addr_size;
11173 fnp->voffset += 2;
11174 }
11175 else
11176 dwarf2_complex_location_expr_complaint ();
11177
11178 if (!fnp->fcontext)
11179 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
11180 }
11181 else if (attr_form_is_section_offset (attr))
11182 {
11183 dwarf2_complex_location_expr_complaint ();
11184 }
11185 else
11186 {
11187 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
11188 fieldname);
11189 }
11190 }
11191 else
11192 {
11193 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11194 if (attr && DW_UNSND (attr))
11195 {
11196 /* GCC does this, as of 2008-08-25; PR debug/37237. */
11197 complaint (&symfile_complaints,
11198 _("Member function \"%s\" (offset %d) is virtual "
11199 "but the vtable offset is not specified"),
11200 fieldname, die->offset.sect_off);
11201 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11202 TYPE_CPLUS_DYNAMIC (type) = 1;
11203 }
11204 }
11205 }
11206
11207 /* Create the vector of member function fields, and attach it to the type. */
11208
11209 static void
11210 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
11211 struct dwarf2_cu *cu)
11212 {
11213 struct fnfieldlist *flp;
11214 int i;
11215
11216 if (cu->language == language_ada)
11217 error (_("unexpected member functions in Ada type"));
11218
11219 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11220 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
11221 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
11222
11223 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
11224 {
11225 struct nextfnfield *nfp = flp->head;
11226 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
11227 int k;
11228
11229 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
11230 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
11231 fn_flp->fn_fields = (struct fn_field *)
11232 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
11233 for (k = flp->length; (k--, nfp); nfp = nfp->next)
11234 fn_flp->fn_fields[k] = nfp->fnfield;
11235 }
11236
11237 TYPE_NFN_FIELDS (type) = fip->nfnfields;
11238 }
11239
11240 /* Returns non-zero if NAME is the name of a vtable member in CU's
11241 language, zero otherwise. */
11242 static int
11243 is_vtable_name (const char *name, struct dwarf2_cu *cu)
11244 {
11245 static const char vptr[] = "_vptr";
11246 static const char vtable[] = "vtable";
11247
11248 /* Look for the C++ and Java forms of the vtable. */
11249 if ((cu->language == language_java
11250 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
11251 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
11252 && is_cplus_marker (name[sizeof (vptr) - 1])))
11253 return 1;
11254
11255 return 0;
11256 }
11257
11258 /* GCC outputs unnamed structures that are really pointers to member
11259 functions, with the ABI-specified layout. If TYPE describes
11260 such a structure, smash it into a member function type.
11261
11262 GCC shouldn't do this; it should just output pointer to member DIEs.
11263 This is GCC PR debug/28767. */
11264
11265 static void
11266 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
11267 {
11268 struct type *pfn_type, *domain_type, *new_type;
11269
11270 /* Check for a structure with no name and two children. */
11271 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
11272 return;
11273
11274 /* Check for __pfn and __delta members. */
11275 if (TYPE_FIELD_NAME (type, 0) == NULL
11276 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
11277 || TYPE_FIELD_NAME (type, 1) == NULL
11278 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
11279 return;
11280
11281 /* Find the type of the method. */
11282 pfn_type = TYPE_FIELD_TYPE (type, 0);
11283 if (pfn_type == NULL
11284 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
11285 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
11286 return;
11287
11288 /* Look for the "this" argument. */
11289 pfn_type = TYPE_TARGET_TYPE (pfn_type);
11290 if (TYPE_NFIELDS (pfn_type) == 0
11291 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
11292 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
11293 return;
11294
11295 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
11296 new_type = alloc_type (objfile);
11297 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
11298 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
11299 TYPE_VARARGS (pfn_type));
11300 smash_to_methodptr_type (type, new_type);
11301 }
11302
11303 /* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
11304 (icc). */
11305
11306 static int
11307 producer_is_icc (struct dwarf2_cu *cu)
11308 {
11309 if (!cu->checked_producer)
11310 check_producer (cu);
11311
11312 return cu->producer_is_icc;
11313 }
11314
11315 /* Called when we find the DIE that starts a structure or union scope
11316 (definition) to create a type for the structure or union. Fill in
11317 the type's name and general properties; the members will not be
11318 processed until process_structure_type.
11319
11320 NOTE: we need to call these functions regardless of whether or not the
11321 DIE has a DW_AT_name attribute, since it might be an anonymous
11322 structure or union. This gets the type entered into our set of
11323 user defined types.
11324
11325 However, if the structure is incomplete (an opaque struct/union)
11326 then suppress creating a symbol table entry for it since gdb only
11327 wants to find the one with the complete definition. Note that if
11328 it is complete, we just call new_symbol, which does it's own
11329 checking about whether the struct/union is anonymous or not (and
11330 suppresses creating a symbol table entry itself). */
11331
11332 static struct type *
11333 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
11334 {
11335 struct objfile *objfile = cu->objfile;
11336 struct type *type;
11337 struct attribute *attr;
11338 const char *name;
11339
11340 /* If the definition of this type lives in .debug_types, read that type.
11341 Don't follow DW_AT_specification though, that will take us back up
11342 the chain and we want to go down. */
11343 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
11344 if (attr)
11345 {
11346 struct dwarf2_cu *type_cu = cu;
11347 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11348
11349 /* We could just recurse on read_structure_type, but we need to call
11350 get_die_type to ensure only one type for this DIE is created.
11351 This is important, for example, because for c++ classes we need
11352 TYPE_NAME set which is only done by new_symbol. Blech. */
11353 type = read_type_die (type_die, type_cu);
11354
11355 /* TYPE_CU may not be the same as CU.
11356 Ensure TYPE is recorded in CU's type_hash table. */
11357 return set_die_type (die, type, cu);
11358 }
11359
11360 type = alloc_type (objfile);
11361 INIT_CPLUS_SPECIFIC (type);
11362
11363 name = dwarf2_name (die, cu);
11364 if (name != NULL)
11365 {
11366 if (cu->language == language_cplus
11367 || cu->language == language_java)
11368 {
11369 const char *full_name = dwarf2_full_name (name, die, cu);
11370
11371 /* dwarf2_full_name might have already finished building the DIE's
11372 type. If so, there is no need to continue. */
11373 if (get_die_type (die, cu) != NULL)
11374 return get_die_type (die, cu);
11375
11376 TYPE_TAG_NAME (type) = full_name;
11377 if (die->tag == DW_TAG_structure_type
11378 || die->tag == DW_TAG_class_type)
11379 TYPE_NAME (type) = TYPE_TAG_NAME (type);
11380 }
11381 else
11382 {
11383 /* The name is already allocated along with this objfile, so
11384 we don't need to duplicate it for the type. */
11385 TYPE_TAG_NAME (type) = name;
11386 if (die->tag == DW_TAG_class_type)
11387 TYPE_NAME (type) = TYPE_TAG_NAME (type);
11388 }
11389 }
11390
11391 if (die->tag == DW_TAG_structure_type)
11392 {
11393 TYPE_CODE (type) = TYPE_CODE_STRUCT;
11394 }
11395 else if (die->tag == DW_TAG_union_type)
11396 {
11397 TYPE_CODE (type) = TYPE_CODE_UNION;
11398 }
11399 else
11400 {
11401 TYPE_CODE (type) = TYPE_CODE_CLASS;
11402 }
11403
11404 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
11405 TYPE_DECLARED_CLASS (type) = 1;
11406
11407 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11408 if (attr)
11409 {
11410 TYPE_LENGTH (type) = DW_UNSND (attr);
11411 }
11412 else
11413 {
11414 TYPE_LENGTH (type) = 0;
11415 }
11416
11417 if (producer_is_icc (cu))
11418 {
11419 /* ICC does not output the required DW_AT_declaration
11420 on incomplete types, but gives them a size of zero. */
11421 }
11422 else
11423 TYPE_STUB_SUPPORTED (type) = 1;
11424
11425 if (die_is_declaration (die, cu))
11426 TYPE_STUB (type) = 1;
11427 else if (attr == NULL && die->child == NULL
11428 && producer_is_realview (cu->producer))
11429 /* RealView does not output the required DW_AT_declaration
11430 on incomplete types. */
11431 TYPE_STUB (type) = 1;
11432
11433 /* We need to add the type field to the die immediately so we don't
11434 infinitely recurse when dealing with pointers to the structure
11435 type within the structure itself. */
11436 set_die_type (die, type, cu);
11437
11438 /* set_die_type should be already done. */
11439 set_descriptive_type (type, die, cu);
11440
11441 return type;
11442 }
11443
11444 /* Finish creating a structure or union type, including filling in
11445 its members and creating a symbol for it. */
11446
11447 static void
11448 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
11449 {
11450 struct objfile *objfile = cu->objfile;
11451 struct die_info *child_die = die->child;
11452 struct type *type;
11453
11454 type = get_die_type (die, cu);
11455 if (type == NULL)
11456 type = read_structure_type (die, cu);
11457
11458 if (die->child != NULL && ! die_is_declaration (die, cu))
11459 {
11460 struct field_info fi;
11461 struct die_info *child_die;
11462 VEC (symbolp) *template_args = NULL;
11463 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
11464
11465 memset (&fi, 0, sizeof (struct field_info));
11466
11467 child_die = die->child;
11468
11469 while (child_die && child_die->tag)
11470 {
11471 if (child_die->tag == DW_TAG_member
11472 || child_die->tag == DW_TAG_variable)
11473 {
11474 /* NOTE: carlton/2002-11-05: A C++ static data member
11475 should be a DW_TAG_member that is a declaration, but
11476 all versions of G++ as of this writing (so through at
11477 least 3.2.1) incorrectly generate DW_TAG_variable
11478 tags for them instead. */
11479 dwarf2_add_field (&fi, child_die, cu);
11480 }
11481 else if (child_die->tag == DW_TAG_subprogram)
11482 {
11483 /* C++ member function. */
11484 dwarf2_add_member_fn (&fi, child_die, type, cu);
11485 }
11486 else if (child_die->tag == DW_TAG_inheritance)
11487 {
11488 /* C++ base class field. */
11489 dwarf2_add_field (&fi, child_die, cu);
11490 }
11491 else if (child_die->tag == DW_TAG_typedef)
11492 dwarf2_add_typedef (&fi, child_die, cu);
11493 else if (child_die->tag == DW_TAG_template_type_param
11494 || child_die->tag == DW_TAG_template_value_param)
11495 {
11496 struct symbol *arg = new_symbol (child_die, NULL, cu);
11497
11498 if (arg != NULL)
11499 VEC_safe_push (symbolp, template_args, arg);
11500 }
11501
11502 child_die = sibling_die (child_die);
11503 }
11504
11505 /* Attach template arguments to type. */
11506 if (! VEC_empty (symbolp, template_args))
11507 {
11508 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11509 TYPE_N_TEMPLATE_ARGUMENTS (type)
11510 = VEC_length (symbolp, template_args);
11511 TYPE_TEMPLATE_ARGUMENTS (type)
11512 = obstack_alloc (&objfile->objfile_obstack,
11513 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11514 * sizeof (struct symbol *)));
11515 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
11516 VEC_address (symbolp, template_args),
11517 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11518 * sizeof (struct symbol *)));
11519 VEC_free (symbolp, template_args);
11520 }
11521
11522 /* Attach fields and member functions to the type. */
11523 if (fi.nfields)
11524 dwarf2_attach_fields_to_type (&fi, type, cu);
11525 if (fi.nfnfields)
11526 {
11527 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
11528
11529 /* Get the type which refers to the base class (possibly this
11530 class itself) which contains the vtable pointer for the current
11531 class from the DW_AT_containing_type attribute. This use of
11532 DW_AT_containing_type is a GNU extension. */
11533
11534 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
11535 {
11536 struct type *t = die_containing_type (die, cu);
11537
11538 TYPE_VPTR_BASETYPE (type) = t;
11539 if (type == t)
11540 {
11541 int i;
11542
11543 /* Our own class provides vtbl ptr. */
11544 for (i = TYPE_NFIELDS (t) - 1;
11545 i >= TYPE_N_BASECLASSES (t);
11546 --i)
11547 {
11548 const char *fieldname = TYPE_FIELD_NAME (t, i);
11549
11550 if (is_vtable_name (fieldname, cu))
11551 {
11552 TYPE_VPTR_FIELDNO (type) = i;
11553 break;
11554 }
11555 }
11556
11557 /* Complain if virtual function table field not found. */
11558 if (i < TYPE_N_BASECLASSES (t))
11559 complaint (&symfile_complaints,
11560 _("virtual function table pointer "
11561 "not found when defining class '%s'"),
11562 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
11563 "");
11564 }
11565 else
11566 {
11567 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
11568 }
11569 }
11570 else if (cu->producer
11571 && strncmp (cu->producer,
11572 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
11573 {
11574 /* The IBM XLC compiler does not provide direct indication
11575 of the containing type, but the vtable pointer is
11576 always named __vfp. */
11577
11578 int i;
11579
11580 for (i = TYPE_NFIELDS (type) - 1;
11581 i >= TYPE_N_BASECLASSES (type);
11582 --i)
11583 {
11584 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
11585 {
11586 TYPE_VPTR_FIELDNO (type) = i;
11587 TYPE_VPTR_BASETYPE (type) = type;
11588 break;
11589 }
11590 }
11591 }
11592 }
11593
11594 /* Copy fi.typedef_field_list linked list elements content into the
11595 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
11596 if (fi.typedef_field_list)
11597 {
11598 int i = fi.typedef_field_list_count;
11599
11600 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11601 TYPE_TYPEDEF_FIELD_ARRAY (type)
11602 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
11603 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
11604
11605 /* Reverse the list order to keep the debug info elements order. */
11606 while (--i >= 0)
11607 {
11608 struct typedef_field *dest, *src;
11609
11610 dest = &TYPE_TYPEDEF_FIELD (type, i);
11611 src = &fi.typedef_field_list->field;
11612 fi.typedef_field_list = fi.typedef_field_list->next;
11613 *dest = *src;
11614 }
11615 }
11616
11617 do_cleanups (back_to);
11618
11619 if (HAVE_CPLUS_STRUCT (type))
11620 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
11621 }
11622
11623 quirk_gcc_member_function_pointer (type, objfile);
11624
11625 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
11626 snapshots) has been known to create a die giving a declaration
11627 for a class that has, as a child, a die giving a definition for a
11628 nested class. So we have to process our children even if the
11629 current die is a declaration. Normally, of course, a declaration
11630 won't have any children at all. */
11631
11632 while (child_die != NULL && child_die->tag)
11633 {
11634 if (child_die->tag == DW_TAG_member
11635 || child_die->tag == DW_TAG_variable
11636 || child_die->tag == DW_TAG_inheritance
11637 || child_die->tag == DW_TAG_template_value_param
11638 || child_die->tag == DW_TAG_template_type_param)
11639 {
11640 /* Do nothing. */
11641 }
11642 else
11643 process_die (child_die, cu);
11644
11645 child_die = sibling_die (child_die);
11646 }
11647
11648 /* Do not consider external references. According to the DWARF standard,
11649 these DIEs are identified by the fact that they have no byte_size
11650 attribute, and a declaration attribute. */
11651 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
11652 || !die_is_declaration (die, cu))
11653 new_symbol (die, type, cu);
11654 }
11655
11656 /* Given a DW_AT_enumeration_type die, set its type. We do not
11657 complete the type's fields yet, or create any symbols. */
11658
11659 static struct type *
11660 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
11661 {
11662 struct objfile *objfile = cu->objfile;
11663 struct type *type;
11664 struct attribute *attr;
11665 const char *name;
11666
11667 /* If the definition of this type lives in .debug_types, read that type.
11668 Don't follow DW_AT_specification though, that will take us back up
11669 the chain and we want to go down. */
11670 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
11671 if (attr)
11672 {
11673 struct dwarf2_cu *type_cu = cu;
11674 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11675
11676 type = read_type_die (type_die, type_cu);
11677
11678 /* TYPE_CU may not be the same as CU.
11679 Ensure TYPE is recorded in CU's type_hash table. */
11680 return set_die_type (die, type, cu);
11681 }
11682
11683 type = alloc_type (objfile);
11684
11685 TYPE_CODE (type) = TYPE_CODE_ENUM;
11686 name = dwarf2_full_name (NULL, die, cu);
11687 if (name != NULL)
11688 TYPE_TAG_NAME (type) = name;
11689
11690 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11691 if (attr)
11692 {
11693 TYPE_LENGTH (type) = DW_UNSND (attr);
11694 }
11695 else
11696 {
11697 TYPE_LENGTH (type) = 0;
11698 }
11699
11700 /* The enumeration DIE can be incomplete. In Ada, any type can be
11701 declared as private in the package spec, and then defined only
11702 inside the package body. Such types are known as Taft Amendment
11703 Types. When another package uses such a type, an incomplete DIE
11704 may be generated by the compiler. */
11705 if (die_is_declaration (die, cu))
11706 TYPE_STUB (type) = 1;
11707
11708 return set_die_type (die, type, cu);
11709 }
11710
11711 /* Given a pointer to a die which begins an enumeration, process all
11712 the dies that define the members of the enumeration, and create the
11713 symbol for the enumeration type.
11714
11715 NOTE: We reverse the order of the element list. */
11716
11717 static void
11718 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
11719 {
11720 struct type *this_type;
11721
11722 this_type = get_die_type (die, cu);
11723 if (this_type == NULL)
11724 this_type = read_enumeration_type (die, cu);
11725
11726 if (die->child != NULL)
11727 {
11728 struct die_info *child_die;
11729 struct symbol *sym;
11730 struct field *fields = NULL;
11731 int num_fields = 0;
11732 int unsigned_enum = 1;
11733 const char *name;
11734 int flag_enum = 1;
11735 ULONGEST mask = 0;
11736
11737 child_die = die->child;
11738 while (child_die && child_die->tag)
11739 {
11740 if (child_die->tag != DW_TAG_enumerator)
11741 {
11742 process_die (child_die, cu);
11743 }
11744 else
11745 {
11746 name = dwarf2_name (child_die, cu);
11747 if (name)
11748 {
11749 sym = new_symbol (child_die, this_type, cu);
11750 if (SYMBOL_VALUE (sym) < 0)
11751 {
11752 unsigned_enum = 0;
11753 flag_enum = 0;
11754 }
11755 else if ((mask & SYMBOL_VALUE (sym)) != 0)
11756 flag_enum = 0;
11757 else
11758 mask |= SYMBOL_VALUE (sym);
11759
11760 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
11761 {
11762 fields = (struct field *)
11763 xrealloc (fields,
11764 (num_fields + DW_FIELD_ALLOC_CHUNK)
11765 * sizeof (struct field));
11766 }
11767
11768 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
11769 FIELD_TYPE (fields[num_fields]) = NULL;
11770 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
11771 FIELD_BITSIZE (fields[num_fields]) = 0;
11772
11773 num_fields++;
11774 }
11775 }
11776
11777 child_die = sibling_die (child_die);
11778 }
11779
11780 if (num_fields)
11781 {
11782 TYPE_NFIELDS (this_type) = num_fields;
11783 TYPE_FIELDS (this_type) = (struct field *)
11784 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
11785 memcpy (TYPE_FIELDS (this_type), fields,
11786 sizeof (struct field) * num_fields);
11787 xfree (fields);
11788 }
11789 if (unsigned_enum)
11790 TYPE_UNSIGNED (this_type) = 1;
11791 if (flag_enum)
11792 TYPE_FLAG_ENUM (this_type) = 1;
11793 }
11794
11795 /* If we are reading an enum from a .debug_types unit, and the enum
11796 is a declaration, and the enum is not the signatured type in the
11797 unit, then we do not want to add a symbol for it. Adding a
11798 symbol would in some cases obscure the true definition of the
11799 enum, giving users an incomplete type when the definition is
11800 actually available. Note that we do not want to do this for all
11801 enums which are just declarations, because C++0x allows forward
11802 enum declarations. */
11803 if (cu->per_cu->is_debug_types
11804 && die_is_declaration (die, cu))
11805 {
11806 struct signatured_type *sig_type;
11807
11808 sig_type = (struct signatured_type *) cu->per_cu;
11809 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
11810 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
11811 return;
11812 }
11813
11814 new_symbol (die, this_type, cu);
11815 }
11816
11817 /* Extract all information from a DW_TAG_array_type DIE and put it in
11818 the DIE's type field. For now, this only handles one dimensional
11819 arrays. */
11820
11821 static struct type *
11822 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
11823 {
11824 struct objfile *objfile = cu->objfile;
11825 struct die_info *child_die;
11826 struct type *type;
11827 struct type *element_type, *range_type, *index_type;
11828 struct type **range_types = NULL;
11829 struct attribute *attr;
11830 int ndim = 0;
11831 struct cleanup *back_to;
11832 const char *name;
11833
11834 element_type = die_type (die, cu);
11835
11836 /* The die_type call above may have already set the type for this DIE. */
11837 type = get_die_type (die, cu);
11838 if (type)
11839 return type;
11840
11841 /* Irix 6.2 native cc creates array types without children for
11842 arrays with unspecified length. */
11843 if (die->child == NULL)
11844 {
11845 index_type = objfile_type (objfile)->builtin_int;
11846 range_type = create_range_type (NULL, index_type, 0, -1);
11847 type = create_array_type (NULL, element_type, range_type);
11848 return set_die_type (die, type, cu);
11849 }
11850
11851 back_to = make_cleanup (null_cleanup, NULL);
11852 child_die = die->child;
11853 while (child_die && child_die->tag)
11854 {
11855 if (child_die->tag == DW_TAG_subrange_type)
11856 {
11857 struct type *child_type = read_type_die (child_die, cu);
11858
11859 if (child_type != NULL)
11860 {
11861 /* The range type was succesfully read. Save it for the
11862 array type creation. */
11863 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
11864 {
11865 range_types = (struct type **)
11866 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
11867 * sizeof (struct type *));
11868 if (ndim == 0)
11869 make_cleanup (free_current_contents, &range_types);
11870 }
11871 range_types[ndim++] = child_type;
11872 }
11873 }
11874 child_die = sibling_die (child_die);
11875 }
11876
11877 /* Dwarf2 dimensions are output from left to right, create the
11878 necessary array types in backwards order. */
11879
11880 type = element_type;
11881
11882 if (read_array_order (die, cu) == DW_ORD_col_major)
11883 {
11884 int i = 0;
11885
11886 while (i < ndim)
11887 type = create_array_type (NULL, type, range_types[i++]);
11888 }
11889 else
11890 {
11891 while (ndim-- > 0)
11892 type = create_array_type (NULL, type, range_types[ndim]);
11893 }
11894
11895 /* Understand Dwarf2 support for vector types (like they occur on
11896 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
11897 array type. This is not part of the Dwarf2/3 standard yet, but a
11898 custom vendor extension. The main difference between a regular
11899 array and the vector variant is that vectors are passed by value
11900 to functions. */
11901 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
11902 if (attr)
11903 make_vector_type (type);
11904
11905 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
11906 implementation may choose to implement triple vectors using this
11907 attribute. */
11908 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11909 if (attr)
11910 {
11911 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
11912 TYPE_LENGTH (type) = DW_UNSND (attr);
11913 else
11914 complaint (&symfile_complaints,
11915 _("DW_AT_byte_size for array type smaller "
11916 "than the total size of elements"));
11917 }
11918
11919 name = dwarf2_name (die, cu);
11920 if (name)
11921 TYPE_NAME (type) = name;
11922
11923 /* Install the type in the die. */
11924 set_die_type (die, type, cu);
11925
11926 /* set_die_type should be already done. */
11927 set_descriptive_type (type, die, cu);
11928
11929 do_cleanups (back_to);
11930
11931 return type;
11932 }
11933
11934 static enum dwarf_array_dim_ordering
11935 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
11936 {
11937 struct attribute *attr;
11938
11939 attr = dwarf2_attr (die, DW_AT_ordering, cu);
11940
11941 if (attr) return DW_SND (attr);
11942
11943 /* GNU F77 is a special case, as at 08/2004 array type info is the
11944 opposite order to the dwarf2 specification, but data is still
11945 laid out as per normal fortran.
11946
11947 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
11948 version checking. */
11949
11950 if (cu->language == language_fortran
11951 && cu->producer && strstr (cu->producer, "GNU F77"))
11952 {
11953 return DW_ORD_row_major;
11954 }
11955
11956 switch (cu->language_defn->la_array_ordering)
11957 {
11958 case array_column_major:
11959 return DW_ORD_col_major;
11960 case array_row_major:
11961 default:
11962 return DW_ORD_row_major;
11963 };
11964 }
11965
11966 /* Extract all information from a DW_TAG_set_type DIE and put it in
11967 the DIE's type field. */
11968
11969 static struct type *
11970 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
11971 {
11972 struct type *domain_type, *set_type;
11973 struct attribute *attr;
11974
11975 domain_type = die_type (die, cu);
11976
11977 /* The die_type call above may have already set the type for this DIE. */
11978 set_type = get_die_type (die, cu);
11979 if (set_type)
11980 return set_type;
11981
11982 set_type = create_set_type (NULL, domain_type);
11983
11984 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11985 if (attr)
11986 TYPE_LENGTH (set_type) = DW_UNSND (attr);
11987
11988 return set_die_type (die, set_type, cu);
11989 }
11990
11991 /* A helper for read_common_block that creates a locexpr baton.
11992 SYM is the symbol which we are marking as computed.
11993 COMMON_DIE is the DIE for the common block.
11994 COMMON_LOC is the location expression attribute for the common
11995 block itself.
11996 MEMBER_LOC is the location expression attribute for the particular
11997 member of the common block that we are processing.
11998 CU is the CU from which the above come. */
11999
12000 static void
12001 mark_common_block_symbol_computed (struct symbol *sym,
12002 struct die_info *common_die,
12003 struct attribute *common_loc,
12004 struct attribute *member_loc,
12005 struct dwarf2_cu *cu)
12006 {
12007 struct objfile *objfile = dwarf2_per_objfile->objfile;
12008 struct dwarf2_locexpr_baton *baton;
12009 gdb_byte *ptr;
12010 unsigned int cu_off;
12011 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
12012 LONGEST offset = 0;
12013
12014 gdb_assert (common_loc && member_loc);
12015 gdb_assert (attr_form_is_block (common_loc));
12016 gdb_assert (attr_form_is_block (member_loc)
12017 || attr_form_is_constant (member_loc));
12018
12019 baton = obstack_alloc (&objfile->objfile_obstack,
12020 sizeof (struct dwarf2_locexpr_baton));
12021 baton->per_cu = cu->per_cu;
12022 gdb_assert (baton->per_cu);
12023
12024 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
12025
12026 if (attr_form_is_constant (member_loc))
12027 {
12028 offset = dwarf2_get_attr_constant_value (member_loc, 0);
12029 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
12030 }
12031 else
12032 baton->size += DW_BLOCK (member_loc)->size;
12033
12034 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
12035 baton->data = ptr;
12036
12037 *ptr++ = DW_OP_call4;
12038 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
12039 store_unsigned_integer (ptr, 4, byte_order, cu_off);
12040 ptr += 4;
12041
12042 if (attr_form_is_constant (member_loc))
12043 {
12044 *ptr++ = DW_OP_addr;
12045 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
12046 ptr += cu->header.addr_size;
12047 }
12048 else
12049 {
12050 /* We have to copy the data here, because DW_OP_call4 will only
12051 use a DW_AT_location attribute. */
12052 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
12053 ptr += DW_BLOCK (member_loc)->size;
12054 }
12055
12056 *ptr++ = DW_OP_plus;
12057 gdb_assert (ptr - baton->data == baton->size);
12058
12059 SYMBOL_LOCATION_BATON (sym) = baton;
12060 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
12061 }
12062
12063 /* Create appropriate locally-scoped variables for all the
12064 DW_TAG_common_block entries. Also create a struct common_block
12065 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
12066 is used to sepate the common blocks name namespace from regular
12067 variable names. */
12068
12069 static void
12070 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
12071 {
12072 struct attribute *attr;
12073
12074 attr = dwarf2_attr (die, DW_AT_location, cu);
12075 if (attr)
12076 {
12077 /* Support the .debug_loc offsets. */
12078 if (attr_form_is_block (attr))
12079 {
12080 /* Ok. */
12081 }
12082 else if (attr_form_is_section_offset (attr))
12083 {
12084 dwarf2_complex_location_expr_complaint ();
12085 attr = NULL;
12086 }
12087 else
12088 {
12089 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
12090 "common block member");
12091 attr = NULL;
12092 }
12093 }
12094
12095 if (die->child != NULL)
12096 {
12097 struct objfile *objfile = cu->objfile;
12098 struct die_info *child_die;
12099 size_t n_entries = 0, size;
12100 struct common_block *common_block;
12101 struct symbol *sym;
12102
12103 for (child_die = die->child;
12104 child_die && child_die->tag;
12105 child_die = sibling_die (child_die))
12106 ++n_entries;
12107
12108 size = (sizeof (struct common_block)
12109 + (n_entries - 1) * sizeof (struct symbol *));
12110 common_block = obstack_alloc (&objfile->objfile_obstack, size);
12111 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
12112 common_block->n_entries = 0;
12113
12114 for (child_die = die->child;
12115 child_die && child_die->tag;
12116 child_die = sibling_die (child_die))
12117 {
12118 /* Create the symbol in the DW_TAG_common_block block in the current
12119 symbol scope. */
12120 sym = new_symbol (child_die, NULL, cu);
12121 if (sym != NULL)
12122 {
12123 struct attribute *member_loc;
12124
12125 common_block->contents[common_block->n_entries++] = sym;
12126
12127 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
12128 cu);
12129 if (member_loc)
12130 {
12131 /* GDB has handled this for a long time, but it is
12132 not specified by DWARF. It seems to have been
12133 emitted by gfortran at least as recently as:
12134 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
12135 complaint (&symfile_complaints,
12136 _("Variable in common block has "
12137 "DW_AT_data_member_location "
12138 "- DIE at 0x%x [in module %s]"),
12139 child_die->offset.sect_off, cu->objfile->name);
12140
12141 if (attr_form_is_section_offset (member_loc))
12142 dwarf2_complex_location_expr_complaint ();
12143 else if (attr_form_is_constant (member_loc)
12144 || attr_form_is_block (member_loc))
12145 {
12146 if (attr)
12147 mark_common_block_symbol_computed (sym, die, attr,
12148 member_loc, cu);
12149 }
12150 else
12151 dwarf2_complex_location_expr_complaint ();
12152 }
12153 }
12154 }
12155
12156 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
12157 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
12158 }
12159 }
12160
12161 /* Create a type for a C++ namespace. */
12162
12163 static struct type *
12164 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
12165 {
12166 struct objfile *objfile = cu->objfile;
12167 const char *previous_prefix, *name;
12168 int is_anonymous;
12169 struct type *type;
12170
12171 /* For extensions, reuse the type of the original namespace. */
12172 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
12173 {
12174 struct die_info *ext_die;
12175 struct dwarf2_cu *ext_cu = cu;
12176
12177 ext_die = dwarf2_extension (die, &ext_cu);
12178 type = read_type_die (ext_die, ext_cu);
12179
12180 /* EXT_CU may not be the same as CU.
12181 Ensure TYPE is recorded in CU's type_hash table. */
12182 return set_die_type (die, type, cu);
12183 }
12184
12185 name = namespace_name (die, &is_anonymous, cu);
12186
12187 /* Now build the name of the current namespace. */
12188
12189 previous_prefix = determine_prefix (die, cu);
12190 if (previous_prefix[0] != '\0')
12191 name = typename_concat (&objfile->objfile_obstack,
12192 previous_prefix, name, 0, cu);
12193
12194 /* Create the type. */
12195 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
12196 objfile);
12197 TYPE_NAME (type) = name;
12198 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12199
12200 return set_die_type (die, type, cu);
12201 }
12202
12203 /* Read a C++ namespace. */
12204
12205 static void
12206 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
12207 {
12208 struct objfile *objfile = cu->objfile;
12209 int is_anonymous;
12210
12211 /* Add a symbol associated to this if we haven't seen the namespace
12212 before. Also, add a using directive if it's an anonymous
12213 namespace. */
12214
12215 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
12216 {
12217 struct type *type;
12218
12219 type = read_type_die (die, cu);
12220 new_symbol (die, type, cu);
12221
12222 namespace_name (die, &is_anonymous, cu);
12223 if (is_anonymous)
12224 {
12225 const char *previous_prefix = determine_prefix (die, cu);
12226
12227 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
12228 NULL, NULL, 0, &objfile->objfile_obstack);
12229 }
12230 }
12231
12232 if (die->child != NULL)
12233 {
12234 struct die_info *child_die = die->child;
12235
12236 while (child_die && child_die->tag)
12237 {
12238 process_die (child_die, cu);
12239 child_die = sibling_die (child_die);
12240 }
12241 }
12242 }
12243
12244 /* Read a Fortran module as type. This DIE can be only a declaration used for
12245 imported module. Still we need that type as local Fortran "use ... only"
12246 declaration imports depend on the created type in determine_prefix. */
12247
12248 static struct type *
12249 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
12250 {
12251 struct objfile *objfile = cu->objfile;
12252 const char *module_name;
12253 struct type *type;
12254
12255 module_name = dwarf2_name (die, cu);
12256 if (!module_name)
12257 complaint (&symfile_complaints,
12258 _("DW_TAG_module has no name, offset 0x%x"),
12259 die->offset.sect_off);
12260 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
12261
12262 /* determine_prefix uses TYPE_TAG_NAME. */
12263 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12264
12265 return set_die_type (die, type, cu);
12266 }
12267
12268 /* Read a Fortran module. */
12269
12270 static void
12271 read_module (struct die_info *die, struct dwarf2_cu *cu)
12272 {
12273 struct die_info *child_die = die->child;
12274
12275 while (child_die && child_die->tag)
12276 {
12277 process_die (child_die, cu);
12278 child_die = sibling_die (child_die);
12279 }
12280 }
12281
12282 /* Return the name of the namespace represented by DIE. Set
12283 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
12284 namespace. */
12285
12286 static const char *
12287 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
12288 {
12289 struct die_info *current_die;
12290 const char *name = NULL;
12291
12292 /* Loop through the extensions until we find a name. */
12293
12294 for (current_die = die;
12295 current_die != NULL;
12296 current_die = dwarf2_extension (die, &cu))
12297 {
12298 name = dwarf2_name (current_die, cu);
12299 if (name != NULL)
12300 break;
12301 }
12302
12303 /* Is it an anonymous namespace? */
12304
12305 *is_anonymous = (name == NULL);
12306 if (*is_anonymous)
12307 name = CP_ANONYMOUS_NAMESPACE_STR;
12308
12309 return name;
12310 }
12311
12312 /* Extract all information from a DW_TAG_pointer_type DIE and add to
12313 the user defined type vector. */
12314
12315 static struct type *
12316 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
12317 {
12318 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
12319 struct comp_unit_head *cu_header = &cu->header;
12320 struct type *type;
12321 struct attribute *attr_byte_size;
12322 struct attribute *attr_address_class;
12323 int byte_size, addr_class;
12324 struct type *target_type;
12325
12326 target_type = die_type (die, cu);
12327
12328 /* The die_type call above may have already set the type for this DIE. */
12329 type = get_die_type (die, cu);
12330 if (type)
12331 return type;
12332
12333 type = lookup_pointer_type (target_type);
12334
12335 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
12336 if (attr_byte_size)
12337 byte_size = DW_UNSND (attr_byte_size);
12338 else
12339 byte_size = cu_header->addr_size;
12340
12341 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
12342 if (attr_address_class)
12343 addr_class = DW_UNSND (attr_address_class);
12344 else
12345 addr_class = DW_ADDR_none;
12346
12347 /* If the pointer size or address class is different than the
12348 default, create a type variant marked as such and set the
12349 length accordingly. */
12350 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
12351 {
12352 if (gdbarch_address_class_type_flags_p (gdbarch))
12353 {
12354 int type_flags;
12355
12356 type_flags = gdbarch_address_class_type_flags
12357 (gdbarch, byte_size, addr_class);
12358 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
12359 == 0);
12360 type = make_type_with_address_space (type, type_flags);
12361 }
12362 else if (TYPE_LENGTH (type) != byte_size)
12363 {
12364 complaint (&symfile_complaints,
12365 _("invalid pointer size %d"), byte_size);
12366 }
12367 else
12368 {
12369 /* Should we also complain about unhandled address classes? */
12370 }
12371 }
12372
12373 TYPE_LENGTH (type) = byte_size;
12374 return set_die_type (die, type, cu);
12375 }
12376
12377 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
12378 the user defined type vector. */
12379
12380 static struct type *
12381 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
12382 {
12383 struct type *type;
12384 struct type *to_type;
12385 struct type *domain;
12386
12387 to_type = die_type (die, cu);
12388 domain = die_containing_type (die, cu);
12389
12390 /* The calls above may have already set the type for this DIE. */
12391 type = get_die_type (die, cu);
12392 if (type)
12393 return type;
12394
12395 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
12396 type = lookup_methodptr_type (to_type);
12397 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
12398 {
12399 struct type *new_type = alloc_type (cu->objfile);
12400
12401 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
12402 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
12403 TYPE_VARARGS (to_type));
12404 type = lookup_methodptr_type (new_type);
12405 }
12406 else
12407 type = lookup_memberptr_type (to_type, domain);
12408
12409 return set_die_type (die, type, cu);
12410 }
12411
12412 /* Extract all information from a DW_TAG_reference_type DIE and add to
12413 the user defined type vector. */
12414
12415 static struct type *
12416 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
12417 {
12418 struct comp_unit_head *cu_header = &cu->header;
12419 struct type *type, *target_type;
12420 struct attribute *attr;
12421
12422 target_type = die_type (die, cu);
12423
12424 /* The die_type call above may have already set the type for this DIE. */
12425 type = get_die_type (die, cu);
12426 if (type)
12427 return type;
12428
12429 type = lookup_reference_type (target_type);
12430 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12431 if (attr)
12432 {
12433 TYPE_LENGTH (type) = DW_UNSND (attr);
12434 }
12435 else
12436 {
12437 TYPE_LENGTH (type) = cu_header->addr_size;
12438 }
12439 return set_die_type (die, type, cu);
12440 }
12441
12442 static struct type *
12443 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
12444 {
12445 struct type *base_type, *cv_type;
12446
12447 base_type = die_type (die, cu);
12448
12449 /* The die_type call above may have already set the type for this DIE. */
12450 cv_type = get_die_type (die, cu);
12451 if (cv_type)
12452 return cv_type;
12453
12454 /* In case the const qualifier is applied to an array type, the element type
12455 is so qualified, not the array type (section 6.7.3 of C99). */
12456 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
12457 {
12458 struct type *el_type, *inner_array;
12459
12460 base_type = copy_type (base_type);
12461 inner_array = base_type;
12462
12463 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
12464 {
12465 TYPE_TARGET_TYPE (inner_array) =
12466 copy_type (TYPE_TARGET_TYPE (inner_array));
12467 inner_array = TYPE_TARGET_TYPE (inner_array);
12468 }
12469
12470 el_type = TYPE_TARGET_TYPE (inner_array);
12471 TYPE_TARGET_TYPE (inner_array) =
12472 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
12473
12474 return set_die_type (die, base_type, cu);
12475 }
12476
12477 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
12478 return set_die_type (die, cv_type, cu);
12479 }
12480
12481 static struct type *
12482 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
12483 {
12484 struct type *base_type, *cv_type;
12485
12486 base_type = die_type (die, cu);
12487
12488 /* The die_type call above may have already set the type for this DIE. */
12489 cv_type = get_die_type (die, cu);
12490 if (cv_type)
12491 return cv_type;
12492
12493 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
12494 return set_die_type (die, cv_type, cu);
12495 }
12496
12497 /* Handle DW_TAG_restrict_type. */
12498
12499 static struct type *
12500 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
12501 {
12502 struct type *base_type, *cv_type;
12503
12504 base_type = die_type (die, cu);
12505
12506 /* The die_type call above may have already set the type for this DIE. */
12507 cv_type = get_die_type (die, cu);
12508 if (cv_type)
12509 return cv_type;
12510
12511 cv_type = make_restrict_type (base_type);
12512 return set_die_type (die, cv_type, cu);
12513 }
12514
12515 /* Extract all information from a DW_TAG_string_type DIE and add to
12516 the user defined type vector. It isn't really a user defined type,
12517 but it behaves like one, with other DIE's using an AT_user_def_type
12518 attribute to reference it. */
12519
12520 static struct type *
12521 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
12522 {
12523 struct objfile *objfile = cu->objfile;
12524 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12525 struct type *type, *range_type, *index_type, *char_type;
12526 struct attribute *attr;
12527 unsigned int length;
12528
12529 attr = dwarf2_attr (die, DW_AT_string_length, cu);
12530 if (attr)
12531 {
12532 length = DW_UNSND (attr);
12533 }
12534 else
12535 {
12536 /* Check for the DW_AT_byte_size attribute. */
12537 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12538 if (attr)
12539 {
12540 length = DW_UNSND (attr);
12541 }
12542 else
12543 {
12544 length = 1;
12545 }
12546 }
12547
12548 index_type = objfile_type (objfile)->builtin_int;
12549 range_type = create_range_type (NULL, index_type, 1, length);
12550 char_type = language_string_char_type (cu->language_defn, gdbarch);
12551 type = create_string_type (NULL, char_type, range_type);
12552
12553 return set_die_type (die, type, cu);
12554 }
12555
12556 /* Handle DIES due to C code like:
12557
12558 struct foo
12559 {
12560 int (*funcp)(int a, long l);
12561 int b;
12562 };
12563
12564 ('funcp' generates a DW_TAG_subroutine_type DIE). */
12565
12566 static struct type *
12567 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
12568 {
12569 struct objfile *objfile = cu->objfile;
12570 struct type *type; /* Type that this function returns. */
12571 struct type *ftype; /* Function that returns above type. */
12572 struct attribute *attr;
12573
12574 type = die_type (die, cu);
12575
12576 /* The die_type call above may have already set the type for this DIE. */
12577 ftype = get_die_type (die, cu);
12578 if (ftype)
12579 return ftype;
12580
12581 ftype = lookup_function_type (type);
12582
12583 /* All functions in C++, Pascal and Java have prototypes. */
12584 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
12585 if ((attr && (DW_UNSND (attr) != 0))
12586 || cu->language == language_cplus
12587 || cu->language == language_java
12588 || cu->language == language_pascal)
12589 TYPE_PROTOTYPED (ftype) = 1;
12590 else if (producer_is_realview (cu->producer))
12591 /* RealView does not emit DW_AT_prototyped. We can not
12592 distinguish prototyped and unprototyped functions; default to
12593 prototyped, since that is more common in modern code (and
12594 RealView warns about unprototyped functions). */
12595 TYPE_PROTOTYPED (ftype) = 1;
12596
12597 /* Store the calling convention in the type if it's available in
12598 the subroutine die. Otherwise set the calling convention to
12599 the default value DW_CC_normal. */
12600 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
12601 if (attr)
12602 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
12603 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
12604 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
12605 else
12606 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
12607
12608 /* We need to add the subroutine type to the die immediately so
12609 we don't infinitely recurse when dealing with parameters
12610 declared as the same subroutine type. */
12611 set_die_type (die, ftype, cu);
12612
12613 if (die->child != NULL)
12614 {
12615 struct type *void_type = objfile_type (objfile)->builtin_void;
12616 struct die_info *child_die;
12617 int nparams, iparams;
12618
12619 /* Count the number of parameters.
12620 FIXME: GDB currently ignores vararg functions, but knows about
12621 vararg member functions. */
12622 nparams = 0;
12623 child_die = die->child;
12624 while (child_die && child_die->tag)
12625 {
12626 if (child_die->tag == DW_TAG_formal_parameter)
12627 nparams++;
12628 else if (child_die->tag == DW_TAG_unspecified_parameters)
12629 TYPE_VARARGS (ftype) = 1;
12630 child_die = sibling_die (child_die);
12631 }
12632
12633 /* Allocate storage for parameters and fill them in. */
12634 TYPE_NFIELDS (ftype) = nparams;
12635 TYPE_FIELDS (ftype) = (struct field *)
12636 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
12637
12638 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
12639 even if we error out during the parameters reading below. */
12640 for (iparams = 0; iparams < nparams; iparams++)
12641 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
12642
12643 iparams = 0;
12644 child_die = die->child;
12645 while (child_die && child_die->tag)
12646 {
12647 if (child_die->tag == DW_TAG_formal_parameter)
12648 {
12649 struct type *arg_type;
12650
12651 /* DWARF version 2 has no clean way to discern C++
12652 static and non-static member functions. G++ helps
12653 GDB by marking the first parameter for non-static
12654 member functions (which is the this pointer) as
12655 artificial. We pass this information to
12656 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
12657
12658 DWARF version 3 added DW_AT_object_pointer, which GCC
12659 4.5 does not yet generate. */
12660 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
12661 if (attr)
12662 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
12663 else
12664 {
12665 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
12666
12667 /* GCC/43521: In java, the formal parameter
12668 "this" is sometimes not marked with DW_AT_artificial. */
12669 if (cu->language == language_java)
12670 {
12671 const char *name = dwarf2_name (child_die, cu);
12672
12673 if (name && !strcmp (name, "this"))
12674 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
12675 }
12676 }
12677 arg_type = die_type (child_die, cu);
12678
12679 /* RealView does not mark THIS as const, which the testsuite
12680 expects. GCC marks THIS as const in method definitions,
12681 but not in the class specifications (GCC PR 43053). */
12682 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
12683 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
12684 {
12685 int is_this = 0;
12686 struct dwarf2_cu *arg_cu = cu;
12687 const char *name = dwarf2_name (child_die, cu);
12688
12689 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
12690 if (attr)
12691 {
12692 /* If the compiler emits this, use it. */
12693 if (follow_die_ref (die, attr, &arg_cu) == child_die)
12694 is_this = 1;
12695 }
12696 else if (name && strcmp (name, "this") == 0)
12697 /* Function definitions will have the argument names. */
12698 is_this = 1;
12699 else if (name == NULL && iparams == 0)
12700 /* Declarations may not have the names, so like
12701 elsewhere in GDB, assume an artificial first
12702 argument is "this". */
12703 is_this = 1;
12704
12705 if (is_this)
12706 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
12707 arg_type, 0);
12708 }
12709
12710 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
12711 iparams++;
12712 }
12713 child_die = sibling_die (child_die);
12714 }
12715 }
12716
12717 return ftype;
12718 }
12719
12720 static struct type *
12721 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
12722 {
12723 struct objfile *objfile = cu->objfile;
12724 const char *name = NULL;
12725 struct type *this_type, *target_type;
12726
12727 name = dwarf2_full_name (NULL, die, cu);
12728 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
12729 TYPE_FLAG_TARGET_STUB, NULL, objfile);
12730 TYPE_NAME (this_type) = name;
12731 set_die_type (die, this_type, cu);
12732 target_type = die_type (die, cu);
12733 if (target_type != this_type)
12734 TYPE_TARGET_TYPE (this_type) = target_type;
12735 else
12736 {
12737 /* Self-referential typedefs are, it seems, not allowed by the DWARF
12738 spec and cause infinite loops in GDB. */
12739 complaint (&symfile_complaints,
12740 _("Self-referential DW_TAG_typedef "
12741 "- DIE at 0x%x [in module %s]"),
12742 die->offset.sect_off, objfile->name);
12743 TYPE_TARGET_TYPE (this_type) = NULL;
12744 }
12745 return this_type;
12746 }
12747
12748 /* Find a representation of a given base type and install
12749 it in the TYPE field of the die. */
12750
12751 static struct type *
12752 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
12753 {
12754 struct objfile *objfile = cu->objfile;
12755 struct type *type;
12756 struct attribute *attr;
12757 int encoding = 0, size = 0;
12758 const char *name;
12759 enum type_code code = TYPE_CODE_INT;
12760 int type_flags = 0;
12761 struct type *target_type = NULL;
12762
12763 attr = dwarf2_attr (die, DW_AT_encoding, cu);
12764 if (attr)
12765 {
12766 encoding = DW_UNSND (attr);
12767 }
12768 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12769 if (attr)
12770 {
12771 size = DW_UNSND (attr);
12772 }
12773 name = dwarf2_name (die, cu);
12774 if (!name)
12775 {
12776 complaint (&symfile_complaints,
12777 _("DW_AT_name missing from DW_TAG_base_type"));
12778 }
12779
12780 switch (encoding)
12781 {
12782 case DW_ATE_address:
12783 /* Turn DW_ATE_address into a void * pointer. */
12784 code = TYPE_CODE_PTR;
12785 type_flags |= TYPE_FLAG_UNSIGNED;
12786 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
12787 break;
12788 case DW_ATE_boolean:
12789 code = TYPE_CODE_BOOL;
12790 type_flags |= TYPE_FLAG_UNSIGNED;
12791 break;
12792 case DW_ATE_complex_float:
12793 code = TYPE_CODE_COMPLEX;
12794 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
12795 break;
12796 case DW_ATE_decimal_float:
12797 code = TYPE_CODE_DECFLOAT;
12798 break;
12799 case DW_ATE_float:
12800 code = TYPE_CODE_FLT;
12801 break;
12802 case DW_ATE_signed:
12803 break;
12804 case DW_ATE_unsigned:
12805 type_flags |= TYPE_FLAG_UNSIGNED;
12806 if (cu->language == language_fortran
12807 && name
12808 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
12809 code = TYPE_CODE_CHAR;
12810 break;
12811 case DW_ATE_signed_char:
12812 if (cu->language == language_ada || cu->language == language_m2
12813 || cu->language == language_pascal
12814 || cu->language == language_fortran)
12815 code = TYPE_CODE_CHAR;
12816 break;
12817 case DW_ATE_unsigned_char:
12818 if (cu->language == language_ada || cu->language == language_m2
12819 || cu->language == language_pascal
12820 || cu->language == language_fortran)
12821 code = TYPE_CODE_CHAR;
12822 type_flags |= TYPE_FLAG_UNSIGNED;
12823 break;
12824 case DW_ATE_UTF:
12825 /* We just treat this as an integer and then recognize the
12826 type by name elsewhere. */
12827 break;
12828
12829 default:
12830 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
12831 dwarf_type_encoding_name (encoding));
12832 break;
12833 }
12834
12835 type = init_type (code, size, type_flags, NULL, objfile);
12836 TYPE_NAME (type) = name;
12837 TYPE_TARGET_TYPE (type) = target_type;
12838
12839 if (name && strcmp (name, "char") == 0)
12840 TYPE_NOSIGN (type) = 1;
12841
12842 return set_die_type (die, type, cu);
12843 }
12844
12845 /* Read the given DW_AT_subrange DIE. */
12846
12847 static struct type *
12848 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
12849 {
12850 struct type *base_type, *orig_base_type;
12851 struct type *range_type;
12852 struct attribute *attr;
12853 LONGEST low, high;
12854 int low_default_is_valid;
12855 const char *name;
12856 LONGEST negative_mask;
12857
12858 orig_base_type = die_type (die, cu);
12859 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
12860 whereas the real type might be. So, we use ORIG_BASE_TYPE when
12861 creating the range type, but we use the result of check_typedef
12862 when examining properties of the type. */
12863 base_type = check_typedef (orig_base_type);
12864
12865 /* The die_type call above may have already set the type for this DIE. */
12866 range_type = get_die_type (die, cu);
12867 if (range_type)
12868 return range_type;
12869
12870 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
12871 omitting DW_AT_lower_bound. */
12872 switch (cu->language)
12873 {
12874 case language_c:
12875 case language_cplus:
12876 low = 0;
12877 low_default_is_valid = 1;
12878 break;
12879 case language_fortran:
12880 low = 1;
12881 low_default_is_valid = 1;
12882 break;
12883 case language_d:
12884 case language_java:
12885 case language_objc:
12886 low = 0;
12887 low_default_is_valid = (cu->header.version >= 4);
12888 break;
12889 case language_ada:
12890 case language_m2:
12891 case language_pascal:
12892 low = 1;
12893 low_default_is_valid = (cu->header.version >= 4);
12894 break;
12895 default:
12896 low = 0;
12897 low_default_is_valid = 0;
12898 break;
12899 }
12900
12901 /* FIXME: For variable sized arrays either of these could be
12902 a variable rather than a constant value. We'll allow it,
12903 but we don't know how to handle it. */
12904 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
12905 if (attr)
12906 low = dwarf2_get_attr_constant_value (attr, low);
12907 else if (!low_default_is_valid)
12908 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
12909 "- DIE at 0x%x [in module %s]"),
12910 die->offset.sect_off, cu->objfile->name);
12911
12912 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
12913 if (attr)
12914 {
12915 if (attr_form_is_block (attr) || is_ref_attr (attr))
12916 {
12917 /* GCC encodes arrays with unspecified or dynamic length
12918 with a DW_FORM_block1 attribute or a reference attribute.
12919 FIXME: GDB does not yet know how to handle dynamic
12920 arrays properly, treat them as arrays with unspecified
12921 length for now.
12922
12923 FIXME: jimb/2003-09-22: GDB does not really know
12924 how to handle arrays of unspecified length
12925 either; we just represent them as zero-length
12926 arrays. Choose an appropriate upper bound given
12927 the lower bound we've computed above. */
12928 high = low - 1;
12929 }
12930 else
12931 high = dwarf2_get_attr_constant_value (attr, 1);
12932 }
12933 else
12934 {
12935 attr = dwarf2_attr (die, DW_AT_count, cu);
12936 if (attr)
12937 {
12938 int count = dwarf2_get_attr_constant_value (attr, 1);
12939 high = low + count - 1;
12940 }
12941 else
12942 {
12943 /* Unspecified array length. */
12944 high = low - 1;
12945 }
12946 }
12947
12948 /* Dwarf-2 specifications explicitly allows to create subrange types
12949 without specifying a base type.
12950 In that case, the base type must be set to the type of
12951 the lower bound, upper bound or count, in that order, if any of these
12952 three attributes references an object that has a type.
12953 If no base type is found, the Dwarf-2 specifications say that
12954 a signed integer type of size equal to the size of an address should
12955 be used.
12956 For the following C code: `extern char gdb_int [];'
12957 GCC produces an empty range DIE.
12958 FIXME: muller/2010-05-28: Possible references to object for low bound,
12959 high bound or count are not yet handled by this code. */
12960 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
12961 {
12962 struct objfile *objfile = cu->objfile;
12963 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12964 int addr_size = gdbarch_addr_bit (gdbarch) /8;
12965 struct type *int_type = objfile_type (objfile)->builtin_int;
12966
12967 /* Test "int", "long int", and "long long int" objfile types,
12968 and select the first one having a size above or equal to the
12969 architecture address size. */
12970 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12971 base_type = int_type;
12972 else
12973 {
12974 int_type = objfile_type (objfile)->builtin_long;
12975 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12976 base_type = int_type;
12977 else
12978 {
12979 int_type = objfile_type (objfile)->builtin_long_long;
12980 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12981 base_type = int_type;
12982 }
12983 }
12984 }
12985
12986 negative_mask =
12987 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
12988 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
12989 low |= negative_mask;
12990 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
12991 high |= negative_mask;
12992
12993 range_type = create_range_type (NULL, orig_base_type, low, high);
12994
12995 /* Mark arrays with dynamic length at least as an array of unspecified
12996 length. GDB could check the boundary but before it gets implemented at
12997 least allow accessing the array elements. */
12998 if (attr && attr_form_is_block (attr))
12999 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
13000
13001 /* Ada expects an empty array on no boundary attributes. */
13002 if (attr == NULL && cu->language != language_ada)
13003 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
13004
13005 name = dwarf2_name (die, cu);
13006 if (name)
13007 TYPE_NAME (range_type) = name;
13008
13009 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13010 if (attr)
13011 TYPE_LENGTH (range_type) = DW_UNSND (attr);
13012
13013 set_die_type (die, range_type, cu);
13014
13015 /* set_die_type should be already done. */
13016 set_descriptive_type (range_type, die, cu);
13017
13018 return range_type;
13019 }
13020
13021 static struct type *
13022 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
13023 {
13024 struct type *type;
13025
13026 /* For now, we only support the C meaning of an unspecified type: void. */
13027
13028 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
13029 TYPE_NAME (type) = dwarf2_name (die, cu);
13030
13031 return set_die_type (die, type, cu);
13032 }
13033
13034 /* Read a single die and all its descendents. Set the die's sibling
13035 field to NULL; set other fields in the die correctly, and set all
13036 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
13037 location of the info_ptr after reading all of those dies. PARENT
13038 is the parent of the die in question. */
13039
13040 static struct die_info *
13041 read_die_and_children (const struct die_reader_specs *reader,
13042 gdb_byte *info_ptr,
13043 gdb_byte **new_info_ptr,
13044 struct die_info *parent)
13045 {
13046 struct die_info *die;
13047 gdb_byte *cur_ptr;
13048 int has_children;
13049
13050 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
13051 if (die == NULL)
13052 {
13053 *new_info_ptr = cur_ptr;
13054 return NULL;
13055 }
13056 store_in_ref_table (die, reader->cu);
13057
13058 if (has_children)
13059 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
13060 else
13061 {
13062 die->child = NULL;
13063 *new_info_ptr = cur_ptr;
13064 }
13065
13066 die->sibling = NULL;
13067 die->parent = parent;
13068 return die;
13069 }
13070
13071 /* Read a die, all of its descendents, and all of its siblings; set
13072 all of the fields of all of the dies correctly. Arguments are as
13073 in read_die_and_children. */
13074
13075 static struct die_info *
13076 read_die_and_siblings (const struct die_reader_specs *reader,
13077 gdb_byte *info_ptr,
13078 gdb_byte **new_info_ptr,
13079 struct die_info *parent)
13080 {
13081 struct die_info *first_die, *last_sibling;
13082 gdb_byte *cur_ptr;
13083
13084 cur_ptr = info_ptr;
13085 first_die = last_sibling = NULL;
13086
13087 while (1)
13088 {
13089 struct die_info *die
13090 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
13091
13092 if (die == NULL)
13093 {
13094 *new_info_ptr = cur_ptr;
13095 return first_die;
13096 }
13097
13098 if (!first_die)
13099 first_die = die;
13100 else
13101 last_sibling->sibling = die;
13102
13103 last_sibling = die;
13104 }
13105 }
13106
13107 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
13108 attributes.
13109 The caller is responsible for filling in the extra attributes
13110 and updating (*DIEP)->num_attrs.
13111 Set DIEP to point to a newly allocated die with its information,
13112 except for its child, sibling, and parent fields.
13113 Set HAS_CHILDREN to tell whether the die has children or not. */
13114
13115 static gdb_byte *
13116 read_full_die_1 (const struct die_reader_specs *reader,
13117 struct die_info **diep, gdb_byte *info_ptr,
13118 int *has_children, int num_extra_attrs)
13119 {
13120 unsigned int abbrev_number, bytes_read, i;
13121 sect_offset offset;
13122 struct abbrev_info *abbrev;
13123 struct die_info *die;
13124 struct dwarf2_cu *cu = reader->cu;
13125 bfd *abfd = reader->abfd;
13126
13127 offset.sect_off = info_ptr - reader->buffer;
13128 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13129 info_ptr += bytes_read;
13130 if (!abbrev_number)
13131 {
13132 *diep = NULL;
13133 *has_children = 0;
13134 return info_ptr;
13135 }
13136
13137 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
13138 if (!abbrev)
13139 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
13140 abbrev_number,
13141 bfd_get_filename (abfd));
13142
13143 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
13144 die->offset = offset;
13145 die->tag = abbrev->tag;
13146 die->abbrev = abbrev_number;
13147
13148 /* Make the result usable.
13149 The caller needs to update num_attrs after adding the extra
13150 attributes. */
13151 die->num_attrs = abbrev->num_attrs;
13152
13153 for (i = 0; i < abbrev->num_attrs; ++i)
13154 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
13155 info_ptr);
13156
13157 *diep = die;
13158 *has_children = abbrev->has_children;
13159 return info_ptr;
13160 }
13161
13162 /* Read a die and all its attributes.
13163 Set DIEP to point to a newly allocated die with its information,
13164 except for its child, sibling, and parent fields.
13165 Set HAS_CHILDREN to tell whether the die has children or not. */
13166
13167 static gdb_byte *
13168 read_full_die (const struct die_reader_specs *reader,
13169 struct die_info **diep, gdb_byte *info_ptr,
13170 int *has_children)
13171 {
13172 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
13173 }
13174 \f
13175 /* Abbreviation tables.
13176
13177 In DWARF version 2, the description of the debugging information is
13178 stored in a separate .debug_abbrev section. Before we read any
13179 dies from a section we read in all abbreviations and install them
13180 in a hash table. */
13181
13182 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
13183
13184 static struct abbrev_info *
13185 abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
13186 {
13187 struct abbrev_info *abbrev;
13188
13189 abbrev = (struct abbrev_info *)
13190 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
13191 memset (abbrev, 0, sizeof (struct abbrev_info));
13192 return abbrev;
13193 }
13194
13195 /* Add an abbreviation to the table. */
13196
13197 static void
13198 abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
13199 unsigned int abbrev_number,
13200 struct abbrev_info *abbrev)
13201 {
13202 unsigned int hash_number;
13203
13204 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13205 abbrev->next = abbrev_table->abbrevs[hash_number];
13206 abbrev_table->abbrevs[hash_number] = abbrev;
13207 }
13208
13209 /* Look up an abbrev in the table.
13210 Returns NULL if the abbrev is not found. */
13211
13212 static struct abbrev_info *
13213 abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
13214 unsigned int abbrev_number)
13215 {
13216 unsigned int hash_number;
13217 struct abbrev_info *abbrev;
13218
13219 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13220 abbrev = abbrev_table->abbrevs[hash_number];
13221
13222 while (abbrev)
13223 {
13224 if (abbrev->number == abbrev_number)
13225 return abbrev;
13226 abbrev = abbrev->next;
13227 }
13228 return NULL;
13229 }
13230
13231 /* Read in an abbrev table. */
13232
13233 static struct abbrev_table *
13234 abbrev_table_read_table (struct dwarf2_section_info *section,
13235 sect_offset offset)
13236 {
13237 struct objfile *objfile = dwarf2_per_objfile->objfile;
13238 bfd *abfd = section->asection->owner;
13239 struct abbrev_table *abbrev_table;
13240 gdb_byte *abbrev_ptr;
13241 struct abbrev_info *cur_abbrev;
13242 unsigned int abbrev_number, bytes_read, abbrev_name;
13243 unsigned int abbrev_form;
13244 struct attr_abbrev *cur_attrs;
13245 unsigned int allocated_attrs;
13246
13247 abbrev_table = XMALLOC (struct abbrev_table);
13248 abbrev_table->offset = offset;
13249 obstack_init (&abbrev_table->abbrev_obstack);
13250 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
13251 (ABBREV_HASH_SIZE
13252 * sizeof (struct abbrev_info *)));
13253 memset (abbrev_table->abbrevs, 0,
13254 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
13255
13256 dwarf2_read_section (objfile, section);
13257 abbrev_ptr = section->buffer + offset.sect_off;
13258 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13259 abbrev_ptr += bytes_read;
13260
13261 allocated_attrs = ATTR_ALLOC_CHUNK;
13262 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
13263
13264 /* Loop until we reach an abbrev number of 0. */
13265 while (abbrev_number)
13266 {
13267 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
13268
13269 /* read in abbrev header */
13270 cur_abbrev->number = abbrev_number;
13271 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13272 abbrev_ptr += bytes_read;
13273 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
13274 abbrev_ptr += 1;
13275
13276 /* now read in declarations */
13277 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13278 abbrev_ptr += bytes_read;
13279 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13280 abbrev_ptr += bytes_read;
13281 while (abbrev_name)
13282 {
13283 if (cur_abbrev->num_attrs == allocated_attrs)
13284 {
13285 allocated_attrs += ATTR_ALLOC_CHUNK;
13286 cur_attrs
13287 = xrealloc (cur_attrs, (allocated_attrs
13288 * sizeof (struct attr_abbrev)));
13289 }
13290
13291 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
13292 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
13293 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13294 abbrev_ptr += bytes_read;
13295 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13296 abbrev_ptr += bytes_read;
13297 }
13298
13299 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
13300 (cur_abbrev->num_attrs
13301 * sizeof (struct attr_abbrev)));
13302 memcpy (cur_abbrev->attrs, cur_attrs,
13303 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
13304
13305 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
13306
13307 /* Get next abbreviation.
13308 Under Irix6 the abbreviations for a compilation unit are not
13309 always properly terminated with an abbrev number of 0.
13310 Exit loop if we encounter an abbreviation which we have
13311 already read (which means we are about to read the abbreviations
13312 for the next compile unit) or if the end of the abbreviation
13313 table is reached. */
13314 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
13315 break;
13316 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13317 abbrev_ptr += bytes_read;
13318 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
13319 break;
13320 }
13321
13322 xfree (cur_attrs);
13323 return abbrev_table;
13324 }
13325
13326 /* Free the resources held by ABBREV_TABLE. */
13327
13328 static void
13329 abbrev_table_free (struct abbrev_table *abbrev_table)
13330 {
13331 obstack_free (&abbrev_table->abbrev_obstack, NULL);
13332 xfree (abbrev_table);
13333 }
13334
13335 /* Same as abbrev_table_free but as a cleanup.
13336 We pass in a pointer to the pointer to the table so that we can
13337 set the pointer to NULL when we're done. It also simplifies
13338 build_type_unit_groups. */
13339
13340 static void
13341 abbrev_table_free_cleanup (void *table_ptr)
13342 {
13343 struct abbrev_table **abbrev_table_ptr = table_ptr;
13344
13345 if (*abbrev_table_ptr != NULL)
13346 abbrev_table_free (*abbrev_table_ptr);
13347 *abbrev_table_ptr = NULL;
13348 }
13349
13350 /* Read the abbrev table for CU from ABBREV_SECTION. */
13351
13352 static void
13353 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
13354 struct dwarf2_section_info *abbrev_section)
13355 {
13356 cu->abbrev_table =
13357 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
13358 }
13359
13360 /* Release the memory used by the abbrev table for a compilation unit. */
13361
13362 static void
13363 dwarf2_free_abbrev_table (void *ptr_to_cu)
13364 {
13365 struct dwarf2_cu *cu = ptr_to_cu;
13366
13367 abbrev_table_free (cu->abbrev_table);
13368 /* Set this to NULL so that we SEGV if we try to read it later,
13369 and also because free_comp_unit verifies this is NULL. */
13370 cu->abbrev_table = NULL;
13371 }
13372 \f
13373 /* Returns nonzero if TAG represents a type that we might generate a partial
13374 symbol for. */
13375
13376 static int
13377 is_type_tag_for_partial (int tag)
13378 {
13379 switch (tag)
13380 {
13381 #if 0
13382 /* Some types that would be reasonable to generate partial symbols for,
13383 that we don't at present. */
13384 case DW_TAG_array_type:
13385 case DW_TAG_file_type:
13386 case DW_TAG_ptr_to_member_type:
13387 case DW_TAG_set_type:
13388 case DW_TAG_string_type:
13389 case DW_TAG_subroutine_type:
13390 #endif
13391 case DW_TAG_base_type:
13392 case DW_TAG_class_type:
13393 case DW_TAG_interface_type:
13394 case DW_TAG_enumeration_type:
13395 case DW_TAG_structure_type:
13396 case DW_TAG_subrange_type:
13397 case DW_TAG_typedef:
13398 case DW_TAG_union_type:
13399 return 1;
13400 default:
13401 return 0;
13402 }
13403 }
13404
13405 /* Load all DIEs that are interesting for partial symbols into memory. */
13406
13407 static struct partial_die_info *
13408 load_partial_dies (const struct die_reader_specs *reader,
13409 gdb_byte *info_ptr, int building_psymtab)
13410 {
13411 struct dwarf2_cu *cu = reader->cu;
13412 struct objfile *objfile = cu->objfile;
13413 struct partial_die_info *part_die;
13414 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
13415 struct abbrev_info *abbrev;
13416 unsigned int bytes_read;
13417 unsigned int load_all = 0;
13418 int nesting_level = 1;
13419
13420 parent_die = NULL;
13421 last_die = NULL;
13422
13423 gdb_assert (cu->per_cu != NULL);
13424 if (cu->per_cu->load_all_dies)
13425 load_all = 1;
13426
13427 cu->partial_dies
13428 = htab_create_alloc_ex (cu->header.length / 12,
13429 partial_die_hash,
13430 partial_die_eq,
13431 NULL,
13432 &cu->comp_unit_obstack,
13433 hashtab_obstack_allocate,
13434 dummy_obstack_deallocate);
13435
13436 part_die = obstack_alloc (&cu->comp_unit_obstack,
13437 sizeof (struct partial_die_info));
13438
13439 while (1)
13440 {
13441 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
13442
13443 /* A NULL abbrev means the end of a series of children. */
13444 if (abbrev == NULL)
13445 {
13446 if (--nesting_level == 0)
13447 {
13448 /* PART_DIE was probably the last thing allocated on the
13449 comp_unit_obstack, so we could call obstack_free
13450 here. We don't do that because the waste is small,
13451 and will be cleaned up when we're done with this
13452 compilation unit. This way, we're also more robust
13453 against other users of the comp_unit_obstack. */
13454 return first_die;
13455 }
13456 info_ptr += bytes_read;
13457 last_die = parent_die;
13458 parent_die = parent_die->die_parent;
13459 continue;
13460 }
13461
13462 /* Check for template arguments. We never save these; if
13463 they're seen, we just mark the parent, and go on our way. */
13464 if (parent_die != NULL
13465 && cu->language == language_cplus
13466 && (abbrev->tag == DW_TAG_template_type_param
13467 || abbrev->tag == DW_TAG_template_value_param))
13468 {
13469 parent_die->has_template_arguments = 1;
13470
13471 if (!load_all)
13472 {
13473 /* We don't need a partial DIE for the template argument. */
13474 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
13475 continue;
13476 }
13477 }
13478
13479 /* We only recurse into c++ subprograms looking for template arguments.
13480 Skip their other children. */
13481 if (!load_all
13482 && cu->language == language_cplus
13483 && parent_die != NULL
13484 && parent_die->tag == DW_TAG_subprogram)
13485 {
13486 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
13487 continue;
13488 }
13489
13490 /* Check whether this DIE is interesting enough to save. Normally
13491 we would not be interested in members here, but there may be
13492 later variables referencing them via DW_AT_specification (for
13493 static members). */
13494 if (!load_all
13495 && !is_type_tag_for_partial (abbrev->tag)
13496 && abbrev->tag != DW_TAG_constant
13497 && abbrev->tag != DW_TAG_enumerator
13498 && abbrev->tag != DW_TAG_subprogram
13499 && abbrev->tag != DW_TAG_lexical_block
13500 && abbrev->tag != DW_TAG_variable
13501 && abbrev->tag != DW_TAG_namespace
13502 && abbrev->tag != DW_TAG_module
13503 && abbrev->tag != DW_TAG_member
13504 && abbrev->tag != DW_TAG_imported_unit)
13505 {
13506 /* Otherwise we skip to the next sibling, if any. */
13507 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
13508 continue;
13509 }
13510
13511 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
13512 info_ptr);
13513
13514 /* This two-pass algorithm for processing partial symbols has a
13515 high cost in cache pressure. Thus, handle some simple cases
13516 here which cover the majority of C partial symbols. DIEs
13517 which neither have specification tags in them, nor could have
13518 specification tags elsewhere pointing at them, can simply be
13519 processed and discarded.
13520
13521 This segment is also optional; scan_partial_symbols and
13522 add_partial_symbol will handle these DIEs if we chain
13523 them in normally. When compilers which do not emit large
13524 quantities of duplicate debug information are more common,
13525 this code can probably be removed. */
13526
13527 /* Any complete simple types at the top level (pretty much all
13528 of them, for a language without namespaces), can be processed
13529 directly. */
13530 if (parent_die == NULL
13531 && part_die->has_specification == 0
13532 && part_die->is_declaration == 0
13533 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
13534 || part_die->tag == DW_TAG_base_type
13535 || part_die->tag == DW_TAG_subrange_type))
13536 {
13537 if (building_psymtab && part_die->name != NULL)
13538 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
13539 VAR_DOMAIN, LOC_TYPEDEF,
13540 &objfile->static_psymbols,
13541 0, (CORE_ADDR) 0, cu->language, objfile);
13542 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
13543 continue;
13544 }
13545
13546 /* The exception for DW_TAG_typedef with has_children above is
13547 a workaround of GCC PR debug/47510. In the case of this complaint
13548 type_name_no_tag_or_error will error on such types later.
13549
13550 GDB skipped children of DW_TAG_typedef by the shortcut above and then
13551 it could not find the child DIEs referenced later, this is checked
13552 above. In correct DWARF DW_TAG_typedef should have no children. */
13553
13554 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
13555 complaint (&symfile_complaints,
13556 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
13557 "- DIE at 0x%x [in module %s]"),
13558 part_die->offset.sect_off, objfile->name);
13559
13560 /* If we're at the second level, and we're an enumerator, and
13561 our parent has no specification (meaning possibly lives in a
13562 namespace elsewhere), then we can add the partial symbol now
13563 instead of queueing it. */
13564 if (part_die->tag == DW_TAG_enumerator
13565 && parent_die != NULL
13566 && parent_die->die_parent == NULL
13567 && parent_die->tag == DW_TAG_enumeration_type
13568 && parent_die->has_specification == 0)
13569 {
13570 if (part_die->name == NULL)
13571 complaint (&symfile_complaints,
13572 _("malformed enumerator DIE ignored"));
13573 else if (building_psymtab)
13574 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
13575 VAR_DOMAIN, LOC_CONST,
13576 (cu->language == language_cplus
13577 || cu->language == language_java)
13578 ? &objfile->global_psymbols
13579 : &objfile->static_psymbols,
13580 0, (CORE_ADDR) 0, cu->language, objfile);
13581
13582 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
13583 continue;
13584 }
13585
13586 /* We'll save this DIE so link it in. */
13587 part_die->die_parent = parent_die;
13588 part_die->die_sibling = NULL;
13589 part_die->die_child = NULL;
13590
13591 if (last_die && last_die == parent_die)
13592 last_die->die_child = part_die;
13593 else if (last_die)
13594 last_die->die_sibling = part_die;
13595
13596 last_die = part_die;
13597
13598 if (first_die == NULL)
13599 first_die = part_die;
13600
13601 /* Maybe add the DIE to the hash table. Not all DIEs that we
13602 find interesting need to be in the hash table, because we
13603 also have the parent/sibling/child chains; only those that we
13604 might refer to by offset later during partial symbol reading.
13605
13606 For now this means things that might have be the target of a
13607 DW_AT_specification, DW_AT_abstract_origin, or
13608 DW_AT_extension. DW_AT_extension will refer only to
13609 namespaces; DW_AT_abstract_origin refers to functions (and
13610 many things under the function DIE, but we do not recurse
13611 into function DIEs during partial symbol reading) and
13612 possibly variables as well; DW_AT_specification refers to
13613 declarations. Declarations ought to have the DW_AT_declaration
13614 flag. It happens that GCC forgets to put it in sometimes, but
13615 only for functions, not for types.
13616
13617 Adding more things than necessary to the hash table is harmless
13618 except for the performance cost. Adding too few will result in
13619 wasted time in find_partial_die, when we reread the compilation
13620 unit with load_all_dies set. */
13621
13622 if (load_all
13623 || abbrev->tag == DW_TAG_constant
13624 || abbrev->tag == DW_TAG_subprogram
13625 || abbrev->tag == DW_TAG_variable
13626 || abbrev->tag == DW_TAG_namespace
13627 || part_die->is_declaration)
13628 {
13629 void **slot;
13630
13631 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
13632 part_die->offset.sect_off, INSERT);
13633 *slot = part_die;
13634 }
13635
13636 part_die = obstack_alloc (&cu->comp_unit_obstack,
13637 sizeof (struct partial_die_info));
13638
13639 /* For some DIEs we want to follow their children (if any). For C
13640 we have no reason to follow the children of structures; for other
13641 languages we have to, so that we can get at method physnames
13642 to infer fully qualified class names, for DW_AT_specification,
13643 and for C++ template arguments. For C++, we also look one level
13644 inside functions to find template arguments (if the name of the
13645 function does not already contain the template arguments).
13646
13647 For Ada, we need to scan the children of subprograms and lexical
13648 blocks as well because Ada allows the definition of nested
13649 entities that could be interesting for the debugger, such as
13650 nested subprograms for instance. */
13651 if (last_die->has_children
13652 && (load_all
13653 || last_die->tag == DW_TAG_namespace
13654 || last_die->tag == DW_TAG_module
13655 || last_die->tag == DW_TAG_enumeration_type
13656 || (cu->language == language_cplus
13657 && last_die->tag == DW_TAG_subprogram
13658 && (last_die->name == NULL
13659 || strchr (last_die->name, '<') == NULL))
13660 || (cu->language != language_c
13661 && (last_die->tag == DW_TAG_class_type
13662 || last_die->tag == DW_TAG_interface_type
13663 || last_die->tag == DW_TAG_structure_type
13664 || last_die->tag == DW_TAG_union_type))
13665 || (cu->language == language_ada
13666 && (last_die->tag == DW_TAG_subprogram
13667 || last_die->tag == DW_TAG_lexical_block))))
13668 {
13669 nesting_level++;
13670 parent_die = last_die;
13671 continue;
13672 }
13673
13674 /* Otherwise we skip to the next sibling, if any. */
13675 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
13676
13677 /* Back to the top, do it again. */
13678 }
13679 }
13680
13681 /* Read a minimal amount of information into the minimal die structure. */
13682
13683 static gdb_byte *
13684 read_partial_die (const struct die_reader_specs *reader,
13685 struct partial_die_info *part_die,
13686 struct abbrev_info *abbrev, unsigned int abbrev_len,
13687 gdb_byte *info_ptr)
13688 {
13689 struct dwarf2_cu *cu = reader->cu;
13690 struct objfile *objfile = cu->objfile;
13691 gdb_byte *buffer = reader->buffer;
13692 unsigned int i;
13693 struct attribute attr;
13694 int has_low_pc_attr = 0;
13695 int has_high_pc_attr = 0;
13696 int high_pc_relative = 0;
13697
13698 memset (part_die, 0, sizeof (struct partial_die_info));
13699
13700 part_die->offset.sect_off = info_ptr - buffer;
13701
13702 info_ptr += abbrev_len;
13703
13704 if (abbrev == NULL)
13705 return info_ptr;
13706
13707 part_die->tag = abbrev->tag;
13708 part_die->has_children = abbrev->has_children;
13709
13710 for (i = 0; i < abbrev->num_attrs; ++i)
13711 {
13712 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
13713
13714 /* Store the data if it is of an attribute we want to keep in a
13715 partial symbol table. */
13716 switch (attr.name)
13717 {
13718 case DW_AT_name:
13719 switch (part_die->tag)
13720 {
13721 case DW_TAG_compile_unit:
13722 case DW_TAG_partial_unit:
13723 case DW_TAG_type_unit:
13724 /* Compilation units have a DW_AT_name that is a filename, not
13725 a source language identifier. */
13726 case DW_TAG_enumeration_type:
13727 case DW_TAG_enumerator:
13728 /* These tags always have simple identifiers already; no need
13729 to canonicalize them. */
13730 part_die->name = DW_STRING (&attr);
13731 break;
13732 default:
13733 part_die->name
13734 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
13735 &objfile->objfile_obstack);
13736 break;
13737 }
13738 break;
13739 case DW_AT_linkage_name:
13740 case DW_AT_MIPS_linkage_name:
13741 /* Note that both forms of linkage name might appear. We
13742 assume they will be the same, and we only store the last
13743 one we see. */
13744 if (cu->language == language_ada)
13745 part_die->name = DW_STRING (&attr);
13746 part_die->linkage_name = DW_STRING (&attr);
13747 break;
13748 case DW_AT_low_pc:
13749 has_low_pc_attr = 1;
13750 part_die->lowpc = DW_ADDR (&attr);
13751 break;
13752 case DW_AT_high_pc:
13753 has_high_pc_attr = 1;
13754 if (attr.form == DW_FORM_addr
13755 || attr.form == DW_FORM_GNU_addr_index)
13756 part_die->highpc = DW_ADDR (&attr);
13757 else
13758 {
13759 high_pc_relative = 1;
13760 part_die->highpc = DW_UNSND (&attr);
13761 }
13762 break;
13763 case DW_AT_location:
13764 /* Support the .debug_loc offsets. */
13765 if (attr_form_is_block (&attr))
13766 {
13767 part_die->d.locdesc = DW_BLOCK (&attr);
13768 }
13769 else if (attr_form_is_section_offset (&attr))
13770 {
13771 dwarf2_complex_location_expr_complaint ();
13772 }
13773 else
13774 {
13775 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
13776 "partial symbol information");
13777 }
13778 break;
13779 case DW_AT_external:
13780 part_die->is_external = DW_UNSND (&attr);
13781 break;
13782 case DW_AT_declaration:
13783 part_die->is_declaration = DW_UNSND (&attr);
13784 break;
13785 case DW_AT_type:
13786 part_die->has_type = 1;
13787 break;
13788 case DW_AT_abstract_origin:
13789 case DW_AT_specification:
13790 case DW_AT_extension:
13791 part_die->has_specification = 1;
13792 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
13793 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13794 || cu->per_cu->is_dwz);
13795 break;
13796 case DW_AT_sibling:
13797 /* Ignore absolute siblings, they might point outside of
13798 the current compile unit. */
13799 if (attr.form == DW_FORM_ref_addr)
13800 complaint (&symfile_complaints,
13801 _("ignoring absolute DW_AT_sibling"));
13802 else
13803 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
13804 break;
13805 case DW_AT_byte_size:
13806 part_die->has_byte_size = 1;
13807 break;
13808 case DW_AT_calling_convention:
13809 /* DWARF doesn't provide a way to identify a program's source-level
13810 entry point. DW_AT_calling_convention attributes are only meant
13811 to describe functions' calling conventions.
13812
13813 However, because it's a necessary piece of information in
13814 Fortran, and because DW_CC_program is the only piece of debugging
13815 information whose definition refers to a 'main program' at all,
13816 several compilers have begun marking Fortran main programs with
13817 DW_CC_program --- even when those functions use the standard
13818 calling conventions.
13819
13820 So until DWARF specifies a way to provide this information and
13821 compilers pick up the new representation, we'll support this
13822 practice. */
13823 if (DW_UNSND (&attr) == DW_CC_program
13824 && cu->language == language_fortran)
13825 {
13826 set_main_name (part_die->name);
13827
13828 /* As this DIE has a static linkage the name would be difficult
13829 to look up later. */
13830 language_of_main = language_fortran;
13831 }
13832 break;
13833 case DW_AT_inline:
13834 if (DW_UNSND (&attr) == DW_INL_inlined
13835 || DW_UNSND (&attr) == DW_INL_declared_inlined)
13836 part_die->may_be_inlined = 1;
13837 break;
13838
13839 case DW_AT_import:
13840 if (part_die->tag == DW_TAG_imported_unit)
13841 {
13842 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
13843 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13844 || cu->per_cu->is_dwz);
13845 }
13846 break;
13847
13848 default:
13849 break;
13850 }
13851 }
13852
13853 if (high_pc_relative)
13854 part_die->highpc += part_die->lowpc;
13855
13856 if (has_low_pc_attr && has_high_pc_attr)
13857 {
13858 /* When using the GNU linker, .gnu.linkonce. sections are used to
13859 eliminate duplicate copies of functions and vtables and such.
13860 The linker will arbitrarily choose one and discard the others.
13861 The AT_*_pc values for such functions refer to local labels in
13862 these sections. If the section from that file was discarded, the
13863 labels are not in the output, so the relocs get a value of 0.
13864 If this is a discarded function, mark the pc bounds as invalid,
13865 so that GDB will ignore it. */
13866 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
13867 {
13868 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13869
13870 complaint (&symfile_complaints,
13871 _("DW_AT_low_pc %s is zero "
13872 "for DIE at 0x%x [in module %s]"),
13873 paddress (gdbarch, part_die->lowpc),
13874 part_die->offset.sect_off, objfile->name);
13875 }
13876 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
13877 else if (part_die->lowpc >= part_die->highpc)
13878 {
13879 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13880
13881 complaint (&symfile_complaints,
13882 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
13883 "for DIE at 0x%x [in module %s]"),
13884 paddress (gdbarch, part_die->lowpc),
13885 paddress (gdbarch, part_die->highpc),
13886 part_die->offset.sect_off, objfile->name);
13887 }
13888 else
13889 part_die->has_pc_info = 1;
13890 }
13891
13892 return info_ptr;
13893 }
13894
13895 /* Find a cached partial DIE at OFFSET in CU. */
13896
13897 static struct partial_die_info *
13898 find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
13899 {
13900 struct partial_die_info *lookup_die = NULL;
13901 struct partial_die_info part_die;
13902
13903 part_die.offset = offset;
13904 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
13905 offset.sect_off);
13906
13907 return lookup_die;
13908 }
13909
13910 /* Find a partial DIE at OFFSET, which may or may not be in CU,
13911 except in the case of .debug_types DIEs which do not reference
13912 outside their CU (they do however referencing other types via
13913 DW_FORM_ref_sig8). */
13914
13915 static struct partial_die_info *
13916 find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
13917 {
13918 struct objfile *objfile = cu->objfile;
13919 struct dwarf2_per_cu_data *per_cu = NULL;
13920 struct partial_die_info *pd = NULL;
13921
13922 if (offset_in_dwz == cu->per_cu->is_dwz
13923 && offset_in_cu_p (&cu->header, offset))
13924 {
13925 pd = find_partial_die_in_comp_unit (offset, cu);
13926 if (pd != NULL)
13927 return pd;
13928 /* We missed recording what we needed.
13929 Load all dies and try again. */
13930 per_cu = cu->per_cu;
13931 }
13932 else
13933 {
13934 /* TUs don't reference other CUs/TUs (except via type signatures). */
13935 if (cu->per_cu->is_debug_types)
13936 {
13937 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
13938 " external reference to offset 0x%lx [in module %s].\n"),
13939 (long) cu->header.offset.sect_off, (long) offset.sect_off,
13940 bfd_get_filename (objfile->obfd));
13941 }
13942 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
13943 objfile);
13944
13945 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
13946 load_partial_comp_unit (per_cu);
13947
13948 per_cu->cu->last_used = 0;
13949 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13950 }
13951
13952 /* If we didn't find it, and not all dies have been loaded,
13953 load them all and try again. */
13954
13955 if (pd == NULL && per_cu->load_all_dies == 0)
13956 {
13957 per_cu->load_all_dies = 1;
13958
13959 /* This is nasty. When we reread the DIEs, somewhere up the call chain
13960 THIS_CU->cu may already be in use. So we can't just free it and
13961 replace its DIEs with the ones we read in. Instead, we leave those
13962 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
13963 and clobber THIS_CU->cu->partial_dies with the hash table for the new
13964 set. */
13965 load_partial_comp_unit (per_cu);
13966
13967 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13968 }
13969
13970 if (pd == NULL)
13971 internal_error (__FILE__, __LINE__,
13972 _("could not find partial DIE 0x%x "
13973 "in cache [from module %s]\n"),
13974 offset.sect_off, bfd_get_filename (objfile->obfd));
13975 return pd;
13976 }
13977
13978 /* See if we can figure out if the class lives in a namespace. We do
13979 this by looking for a member function; its demangled name will
13980 contain namespace info, if there is any. */
13981
13982 static void
13983 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
13984 struct dwarf2_cu *cu)
13985 {
13986 /* NOTE: carlton/2003-10-07: Getting the info this way changes
13987 what template types look like, because the demangler
13988 frequently doesn't give the same name as the debug info. We
13989 could fix this by only using the demangled name to get the
13990 prefix (but see comment in read_structure_type). */
13991
13992 struct partial_die_info *real_pdi;
13993 struct partial_die_info *child_pdi;
13994
13995 /* If this DIE (this DIE's specification, if any) has a parent, then
13996 we should not do this. We'll prepend the parent's fully qualified
13997 name when we create the partial symbol. */
13998
13999 real_pdi = struct_pdi;
14000 while (real_pdi->has_specification)
14001 real_pdi = find_partial_die (real_pdi->spec_offset,
14002 real_pdi->spec_is_dwz, cu);
14003
14004 if (real_pdi->die_parent != NULL)
14005 return;
14006
14007 for (child_pdi = struct_pdi->die_child;
14008 child_pdi != NULL;
14009 child_pdi = child_pdi->die_sibling)
14010 {
14011 if (child_pdi->tag == DW_TAG_subprogram
14012 && child_pdi->linkage_name != NULL)
14013 {
14014 char *actual_class_name
14015 = language_class_name_from_physname (cu->language_defn,
14016 child_pdi->linkage_name);
14017 if (actual_class_name != NULL)
14018 {
14019 struct_pdi->name
14020 = obstack_copy0 (&cu->objfile->objfile_obstack,
14021 actual_class_name,
14022 strlen (actual_class_name));
14023 xfree (actual_class_name);
14024 }
14025 break;
14026 }
14027 }
14028 }
14029
14030 /* Adjust PART_DIE before generating a symbol for it. This function
14031 may set the is_external flag or change the DIE's name. */
14032
14033 static void
14034 fixup_partial_die (struct partial_die_info *part_die,
14035 struct dwarf2_cu *cu)
14036 {
14037 /* Once we've fixed up a die, there's no point in doing so again.
14038 This also avoids a memory leak if we were to call
14039 guess_partial_die_structure_name multiple times. */
14040 if (part_die->fixup_called)
14041 return;
14042
14043 /* If we found a reference attribute and the DIE has no name, try
14044 to find a name in the referred to DIE. */
14045
14046 if (part_die->name == NULL && part_die->has_specification)
14047 {
14048 struct partial_die_info *spec_die;
14049
14050 spec_die = find_partial_die (part_die->spec_offset,
14051 part_die->spec_is_dwz, cu);
14052
14053 fixup_partial_die (spec_die, cu);
14054
14055 if (spec_die->name)
14056 {
14057 part_die->name = spec_die->name;
14058
14059 /* Copy DW_AT_external attribute if it is set. */
14060 if (spec_die->is_external)
14061 part_die->is_external = spec_die->is_external;
14062 }
14063 }
14064
14065 /* Set default names for some unnamed DIEs. */
14066
14067 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
14068 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
14069
14070 /* If there is no parent die to provide a namespace, and there are
14071 children, see if we can determine the namespace from their linkage
14072 name. */
14073 if (cu->language == language_cplus
14074 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
14075 && part_die->die_parent == NULL
14076 && part_die->has_children
14077 && (part_die->tag == DW_TAG_class_type
14078 || part_die->tag == DW_TAG_structure_type
14079 || part_die->tag == DW_TAG_union_type))
14080 guess_partial_die_structure_name (part_die, cu);
14081
14082 /* GCC might emit a nameless struct or union that has a linkage
14083 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14084 if (part_die->name == NULL
14085 && (part_die->tag == DW_TAG_class_type
14086 || part_die->tag == DW_TAG_interface_type
14087 || part_die->tag == DW_TAG_structure_type
14088 || part_die->tag == DW_TAG_union_type)
14089 && part_die->linkage_name != NULL)
14090 {
14091 char *demangled;
14092
14093 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
14094 if (demangled)
14095 {
14096 const char *base;
14097
14098 /* Strip any leading namespaces/classes, keep only the base name.
14099 DW_AT_name for named DIEs does not contain the prefixes. */
14100 base = strrchr (demangled, ':');
14101 if (base && base > demangled && base[-1] == ':')
14102 base++;
14103 else
14104 base = demangled;
14105
14106 part_die->name = obstack_copy0 (&cu->objfile->objfile_obstack,
14107 base, strlen (base));
14108 xfree (demangled);
14109 }
14110 }
14111
14112 part_die->fixup_called = 1;
14113 }
14114
14115 /* Read an attribute value described by an attribute form. */
14116
14117 static gdb_byte *
14118 read_attribute_value (const struct die_reader_specs *reader,
14119 struct attribute *attr, unsigned form,
14120 gdb_byte *info_ptr)
14121 {
14122 struct dwarf2_cu *cu = reader->cu;
14123 bfd *abfd = reader->abfd;
14124 struct comp_unit_head *cu_header = &cu->header;
14125 unsigned int bytes_read;
14126 struct dwarf_block *blk;
14127
14128 attr->form = form;
14129 switch (form)
14130 {
14131 case DW_FORM_ref_addr:
14132 if (cu->header.version == 2)
14133 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
14134 else
14135 DW_UNSND (attr) = read_offset (abfd, info_ptr,
14136 &cu->header, &bytes_read);
14137 info_ptr += bytes_read;
14138 break;
14139 case DW_FORM_GNU_ref_alt:
14140 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14141 info_ptr += bytes_read;
14142 break;
14143 case DW_FORM_addr:
14144 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
14145 info_ptr += bytes_read;
14146 break;
14147 case DW_FORM_block2:
14148 blk = dwarf_alloc_block (cu);
14149 blk->size = read_2_bytes (abfd, info_ptr);
14150 info_ptr += 2;
14151 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14152 info_ptr += blk->size;
14153 DW_BLOCK (attr) = blk;
14154 break;
14155 case DW_FORM_block4:
14156 blk = dwarf_alloc_block (cu);
14157 blk->size = read_4_bytes (abfd, info_ptr);
14158 info_ptr += 4;
14159 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14160 info_ptr += blk->size;
14161 DW_BLOCK (attr) = blk;
14162 break;
14163 case DW_FORM_data2:
14164 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
14165 info_ptr += 2;
14166 break;
14167 case DW_FORM_data4:
14168 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
14169 info_ptr += 4;
14170 break;
14171 case DW_FORM_data8:
14172 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
14173 info_ptr += 8;
14174 break;
14175 case DW_FORM_sec_offset:
14176 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14177 info_ptr += bytes_read;
14178 break;
14179 case DW_FORM_string:
14180 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
14181 DW_STRING_IS_CANONICAL (attr) = 0;
14182 info_ptr += bytes_read;
14183 break;
14184 case DW_FORM_strp:
14185 if (!cu->per_cu->is_dwz)
14186 {
14187 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
14188 &bytes_read);
14189 DW_STRING_IS_CANONICAL (attr) = 0;
14190 info_ptr += bytes_read;
14191 break;
14192 }
14193 /* FALLTHROUGH */
14194 case DW_FORM_GNU_strp_alt:
14195 {
14196 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14197 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
14198 &bytes_read);
14199
14200 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
14201 DW_STRING_IS_CANONICAL (attr) = 0;
14202 info_ptr += bytes_read;
14203 }
14204 break;
14205 case DW_FORM_exprloc:
14206 case DW_FORM_block:
14207 blk = dwarf_alloc_block (cu);
14208 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14209 info_ptr += bytes_read;
14210 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14211 info_ptr += blk->size;
14212 DW_BLOCK (attr) = blk;
14213 break;
14214 case DW_FORM_block1:
14215 blk = dwarf_alloc_block (cu);
14216 blk->size = read_1_byte (abfd, info_ptr);
14217 info_ptr += 1;
14218 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14219 info_ptr += blk->size;
14220 DW_BLOCK (attr) = blk;
14221 break;
14222 case DW_FORM_data1:
14223 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14224 info_ptr += 1;
14225 break;
14226 case DW_FORM_flag:
14227 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14228 info_ptr += 1;
14229 break;
14230 case DW_FORM_flag_present:
14231 DW_UNSND (attr) = 1;
14232 break;
14233 case DW_FORM_sdata:
14234 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
14235 info_ptr += bytes_read;
14236 break;
14237 case DW_FORM_udata:
14238 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14239 info_ptr += bytes_read;
14240 break;
14241 case DW_FORM_ref1:
14242 DW_UNSND (attr) = (cu->header.offset.sect_off
14243 + read_1_byte (abfd, info_ptr));
14244 info_ptr += 1;
14245 break;
14246 case DW_FORM_ref2:
14247 DW_UNSND (attr) = (cu->header.offset.sect_off
14248 + read_2_bytes (abfd, info_ptr));
14249 info_ptr += 2;
14250 break;
14251 case DW_FORM_ref4:
14252 DW_UNSND (attr) = (cu->header.offset.sect_off
14253 + read_4_bytes (abfd, info_ptr));
14254 info_ptr += 4;
14255 break;
14256 case DW_FORM_ref8:
14257 DW_UNSND (attr) = (cu->header.offset.sect_off
14258 + read_8_bytes (abfd, info_ptr));
14259 info_ptr += 8;
14260 break;
14261 case DW_FORM_ref_sig8:
14262 /* Convert the signature to something we can record in DW_UNSND
14263 for later lookup.
14264 NOTE: This is NULL if the type wasn't found. */
14265 DW_SIGNATURED_TYPE (attr) =
14266 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
14267 info_ptr += 8;
14268 break;
14269 case DW_FORM_ref_udata:
14270 DW_UNSND (attr) = (cu->header.offset.sect_off
14271 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
14272 info_ptr += bytes_read;
14273 break;
14274 case DW_FORM_indirect:
14275 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14276 info_ptr += bytes_read;
14277 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
14278 break;
14279 case DW_FORM_GNU_addr_index:
14280 if (reader->dwo_file == NULL)
14281 {
14282 /* For now flag a hard error.
14283 Later we can turn this into a complaint. */
14284 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14285 dwarf_form_name (form),
14286 bfd_get_filename (abfd));
14287 }
14288 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
14289 info_ptr += bytes_read;
14290 break;
14291 case DW_FORM_GNU_str_index:
14292 if (reader->dwo_file == NULL)
14293 {
14294 /* For now flag a hard error.
14295 Later we can turn this into a complaint if warranted. */
14296 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14297 dwarf_form_name (form),
14298 bfd_get_filename (abfd));
14299 }
14300 {
14301 ULONGEST str_index =
14302 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14303
14304 DW_STRING (attr) = read_str_index (reader, cu, str_index);
14305 DW_STRING_IS_CANONICAL (attr) = 0;
14306 info_ptr += bytes_read;
14307 }
14308 break;
14309 default:
14310 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
14311 dwarf_form_name (form),
14312 bfd_get_filename (abfd));
14313 }
14314
14315 /* Super hack. */
14316 if (cu->per_cu->is_dwz && is_ref_attr (attr))
14317 attr->form = DW_FORM_GNU_ref_alt;
14318
14319 /* We have seen instances where the compiler tried to emit a byte
14320 size attribute of -1 which ended up being encoded as an unsigned
14321 0xffffffff. Although 0xffffffff is technically a valid size value,
14322 an object of this size seems pretty unlikely so we can relatively
14323 safely treat these cases as if the size attribute was invalid and
14324 treat them as zero by default. */
14325 if (attr->name == DW_AT_byte_size
14326 && form == DW_FORM_data4
14327 && DW_UNSND (attr) >= 0xffffffff)
14328 {
14329 complaint
14330 (&symfile_complaints,
14331 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
14332 hex_string (DW_UNSND (attr)));
14333 DW_UNSND (attr) = 0;
14334 }
14335
14336 return info_ptr;
14337 }
14338
14339 /* Read an attribute described by an abbreviated attribute. */
14340
14341 static gdb_byte *
14342 read_attribute (const struct die_reader_specs *reader,
14343 struct attribute *attr, struct attr_abbrev *abbrev,
14344 gdb_byte *info_ptr)
14345 {
14346 attr->name = abbrev->name;
14347 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
14348 }
14349
14350 /* Read dwarf information from a buffer. */
14351
14352 static unsigned int
14353 read_1_byte (bfd *abfd, const gdb_byte *buf)
14354 {
14355 return bfd_get_8 (abfd, buf);
14356 }
14357
14358 static int
14359 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
14360 {
14361 return bfd_get_signed_8 (abfd, buf);
14362 }
14363
14364 static unsigned int
14365 read_2_bytes (bfd *abfd, const gdb_byte *buf)
14366 {
14367 return bfd_get_16 (abfd, buf);
14368 }
14369
14370 static int
14371 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
14372 {
14373 return bfd_get_signed_16 (abfd, buf);
14374 }
14375
14376 static unsigned int
14377 read_4_bytes (bfd *abfd, const gdb_byte *buf)
14378 {
14379 return bfd_get_32 (abfd, buf);
14380 }
14381
14382 static int
14383 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
14384 {
14385 return bfd_get_signed_32 (abfd, buf);
14386 }
14387
14388 static ULONGEST
14389 read_8_bytes (bfd *abfd, const gdb_byte *buf)
14390 {
14391 return bfd_get_64 (abfd, buf);
14392 }
14393
14394 static CORE_ADDR
14395 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
14396 unsigned int *bytes_read)
14397 {
14398 struct comp_unit_head *cu_header = &cu->header;
14399 CORE_ADDR retval = 0;
14400
14401 if (cu_header->signed_addr_p)
14402 {
14403 switch (cu_header->addr_size)
14404 {
14405 case 2:
14406 retval = bfd_get_signed_16 (abfd, buf);
14407 break;
14408 case 4:
14409 retval = bfd_get_signed_32 (abfd, buf);
14410 break;
14411 case 8:
14412 retval = bfd_get_signed_64 (abfd, buf);
14413 break;
14414 default:
14415 internal_error (__FILE__, __LINE__,
14416 _("read_address: bad switch, signed [in module %s]"),
14417 bfd_get_filename (abfd));
14418 }
14419 }
14420 else
14421 {
14422 switch (cu_header->addr_size)
14423 {
14424 case 2:
14425 retval = bfd_get_16 (abfd, buf);
14426 break;
14427 case 4:
14428 retval = bfd_get_32 (abfd, buf);
14429 break;
14430 case 8:
14431 retval = bfd_get_64 (abfd, buf);
14432 break;
14433 default:
14434 internal_error (__FILE__, __LINE__,
14435 _("read_address: bad switch, "
14436 "unsigned [in module %s]"),
14437 bfd_get_filename (abfd));
14438 }
14439 }
14440
14441 *bytes_read = cu_header->addr_size;
14442 return retval;
14443 }
14444
14445 /* Read the initial length from a section. The (draft) DWARF 3
14446 specification allows the initial length to take up either 4 bytes
14447 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
14448 bytes describe the length and all offsets will be 8 bytes in length
14449 instead of 4.
14450
14451 An older, non-standard 64-bit format is also handled by this
14452 function. The older format in question stores the initial length
14453 as an 8-byte quantity without an escape value. Lengths greater
14454 than 2^32 aren't very common which means that the initial 4 bytes
14455 is almost always zero. Since a length value of zero doesn't make
14456 sense for the 32-bit format, this initial zero can be considered to
14457 be an escape value which indicates the presence of the older 64-bit
14458 format. As written, the code can't detect (old format) lengths
14459 greater than 4GB. If it becomes necessary to handle lengths
14460 somewhat larger than 4GB, we could allow other small values (such
14461 as the non-sensical values of 1, 2, and 3) to also be used as
14462 escape values indicating the presence of the old format.
14463
14464 The value returned via bytes_read should be used to increment the
14465 relevant pointer after calling read_initial_length().
14466
14467 [ Note: read_initial_length() and read_offset() are based on the
14468 document entitled "DWARF Debugging Information Format", revision
14469 3, draft 8, dated November 19, 2001. This document was obtained
14470 from:
14471
14472 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
14473
14474 This document is only a draft and is subject to change. (So beware.)
14475
14476 Details regarding the older, non-standard 64-bit format were
14477 determined empirically by examining 64-bit ELF files produced by
14478 the SGI toolchain on an IRIX 6.5 machine.
14479
14480 - Kevin, July 16, 2002
14481 ] */
14482
14483 static LONGEST
14484 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
14485 {
14486 LONGEST length = bfd_get_32 (abfd, buf);
14487
14488 if (length == 0xffffffff)
14489 {
14490 length = bfd_get_64 (abfd, buf + 4);
14491 *bytes_read = 12;
14492 }
14493 else if (length == 0)
14494 {
14495 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
14496 length = bfd_get_64 (abfd, buf);
14497 *bytes_read = 8;
14498 }
14499 else
14500 {
14501 *bytes_read = 4;
14502 }
14503
14504 return length;
14505 }
14506
14507 /* Cover function for read_initial_length.
14508 Returns the length of the object at BUF, and stores the size of the
14509 initial length in *BYTES_READ and stores the size that offsets will be in
14510 *OFFSET_SIZE.
14511 If the initial length size is not equivalent to that specified in
14512 CU_HEADER then issue a complaint.
14513 This is useful when reading non-comp-unit headers. */
14514
14515 static LONGEST
14516 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
14517 const struct comp_unit_head *cu_header,
14518 unsigned int *bytes_read,
14519 unsigned int *offset_size)
14520 {
14521 LONGEST length = read_initial_length (abfd, buf, bytes_read);
14522
14523 gdb_assert (cu_header->initial_length_size == 4
14524 || cu_header->initial_length_size == 8
14525 || cu_header->initial_length_size == 12);
14526
14527 if (cu_header->initial_length_size != *bytes_read)
14528 complaint (&symfile_complaints,
14529 _("intermixed 32-bit and 64-bit DWARF sections"));
14530
14531 *offset_size = (*bytes_read == 4) ? 4 : 8;
14532 return length;
14533 }
14534
14535 /* Read an offset from the data stream. The size of the offset is
14536 given by cu_header->offset_size. */
14537
14538 static LONGEST
14539 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
14540 unsigned int *bytes_read)
14541 {
14542 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
14543
14544 *bytes_read = cu_header->offset_size;
14545 return offset;
14546 }
14547
14548 /* Read an offset from the data stream. */
14549
14550 static LONGEST
14551 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
14552 {
14553 LONGEST retval = 0;
14554
14555 switch (offset_size)
14556 {
14557 case 4:
14558 retval = bfd_get_32 (abfd, buf);
14559 break;
14560 case 8:
14561 retval = bfd_get_64 (abfd, buf);
14562 break;
14563 default:
14564 internal_error (__FILE__, __LINE__,
14565 _("read_offset_1: bad switch [in module %s]"),
14566 bfd_get_filename (abfd));
14567 }
14568
14569 return retval;
14570 }
14571
14572 static gdb_byte *
14573 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
14574 {
14575 /* If the size of a host char is 8 bits, we can return a pointer
14576 to the buffer, otherwise we have to copy the data to a buffer
14577 allocated on the temporary obstack. */
14578 gdb_assert (HOST_CHAR_BIT == 8);
14579 return buf;
14580 }
14581
14582 static char *
14583 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
14584 {
14585 /* If the size of a host char is 8 bits, we can return a pointer
14586 to the string, otherwise we have to copy the string to a buffer
14587 allocated on the temporary obstack. */
14588 gdb_assert (HOST_CHAR_BIT == 8);
14589 if (*buf == '\0')
14590 {
14591 *bytes_read_ptr = 1;
14592 return NULL;
14593 }
14594 *bytes_read_ptr = strlen ((char *) buf) + 1;
14595 return (char *) buf;
14596 }
14597
14598 static char *
14599 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
14600 {
14601 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
14602 if (dwarf2_per_objfile->str.buffer == NULL)
14603 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
14604 bfd_get_filename (abfd));
14605 if (str_offset >= dwarf2_per_objfile->str.size)
14606 error (_("DW_FORM_strp pointing outside of "
14607 ".debug_str section [in module %s]"),
14608 bfd_get_filename (abfd));
14609 gdb_assert (HOST_CHAR_BIT == 8);
14610 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
14611 return NULL;
14612 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
14613 }
14614
14615 /* Read a string at offset STR_OFFSET in the .debug_str section from
14616 the .dwz file DWZ. Throw an error if the offset is too large. If
14617 the string consists of a single NUL byte, return NULL; otherwise
14618 return a pointer to the string. */
14619
14620 static char *
14621 read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
14622 {
14623 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
14624
14625 if (dwz->str.buffer == NULL)
14626 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
14627 "section [in module %s]"),
14628 bfd_get_filename (dwz->dwz_bfd));
14629 if (str_offset >= dwz->str.size)
14630 error (_("DW_FORM_GNU_strp_alt pointing outside of "
14631 ".debug_str section [in module %s]"),
14632 bfd_get_filename (dwz->dwz_bfd));
14633 gdb_assert (HOST_CHAR_BIT == 8);
14634 if (dwz->str.buffer[str_offset] == '\0')
14635 return NULL;
14636 return (char *) (dwz->str.buffer + str_offset);
14637 }
14638
14639 static char *
14640 read_indirect_string (bfd *abfd, gdb_byte *buf,
14641 const struct comp_unit_head *cu_header,
14642 unsigned int *bytes_read_ptr)
14643 {
14644 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
14645
14646 return read_indirect_string_at_offset (abfd, str_offset);
14647 }
14648
14649 static ULONGEST
14650 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
14651 {
14652 ULONGEST result;
14653 unsigned int num_read;
14654 int i, shift;
14655 unsigned char byte;
14656
14657 result = 0;
14658 shift = 0;
14659 num_read = 0;
14660 i = 0;
14661 while (1)
14662 {
14663 byte = bfd_get_8 (abfd, buf);
14664 buf++;
14665 num_read++;
14666 result |= ((ULONGEST) (byte & 127) << shift);
14667 if ((byte & 128) == 0)
14668 {
14669 break;
14670 }
14671 shift += 7;
14672 }
14673 *bytes_read_ptr = num_read;
14674 return result;
14675 }
14676
14677 static LONGEST
14678 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
14679 {
14680 LONGEST result;
14681 int i, shift, num_read;
14682 unsigned char byte;
14683
14684 result = 0;
14685 shift = 0;
14686 num_read = 0;
14687 i = 0;
14688 while (1)
14689 {
14690 byte = bfd_get_8 (abfd, buf);
14691 buf++;
14692 num_read++;
14693 result |= ((LONGEST) (byte & 127) << shift);
14694 shift += 7;
14695 if ((byte & 128) == 0)
14696 {
14697 break;
14698 }
14699 }
14700 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
14701 result |= -(((LONGEST) 1) << shift);
14702 *bytes_read_ptr = num_read;
14703 return result;
14704 }
14705
14706 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
14707 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
14708 ADDR_SIZE is the size of addresses from the CU header. */
14709
14710 static CORE_ADDR
14711 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
14712 {
14713 struct objfile *objfile = dwarf2_per_objfile->objfile;
14714 bfd *abfd = objfile->obfd;
14715 const gdb_byte *info_ptr;
14716
14717 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
14718 if (dwarf2_per_objfile->addr.buffer == NULL)
14719 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
14720 objfile->name);
14721 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
14722 error (_("DW_FORM_addr_index pointing outside of "
14723 ".debug_addr section [in module %s]"),
14724 objfile->name);
14725 info_ptr = (dwarf2_per_objfile->addr.buffer
14726 + addr_base + addr_index * addr_size);
14727 if (addr_size == 4)
14728 return bfd_get_32 (abfd, info_ptr);
14729 else
14730 return bfd_get_64 (abfd, info_ptr);
14731 }
14732
14733 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
14734
14735 static CORE_ADDR
14736 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
14737 {
14738 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
14739 }
14740
14741 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
14742
14743 static CORE_ADDR
14744 read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
14745 unsigned int *bytes_read)
14746 {
14747 bfd *abfd = cu->objfile->obfd;
14748 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
14749
14750 return read_addr_index (cu, addr_index);
14751 }
14752
14753 /* Data structure to pass results from dwarf2_read_addr_index_reader
14754 back to dwarf2_read_addr_index. */
14755
14756 struct dwarf2_read_addr_index_data
14757 {
14758 ULONGEST addr_base;
14759 int addr_size;
14760 };
14761
14762 /* die_reader_func for dwarf2_read_addr_index. */
14763
14764 static void
14765 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
14766 gdb_byte *info_ptr,
14767 struct die_info *comp_unit_die,
14768 int has_children,
14769 void *data)
14770 {
14771 struct dwarf2_cu *cu = reader->cu;
14772 struct dwarf2_read_addr_index_data *aidata =
14773 (struct dwarf2_read_addr_index_data *) data;
14774
14775 aidata->addr_base = cu->addr_base;
14776 aidata->addr_size = cu->header.addr_size;
14777 }
14778
14779 /* Given an index in .debug_addr, fetch the value.
14780 NOTE: This can be called during dwarf expression evaluation,
14781 long after the debug information has been read, and thus per_cu->cu
14782 may no longer exist. */
14783
14784 CORE_ADDR
14785 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
14786 unsigned int addr_index)
14787 {
14788 struct objfile *objfile = per_cu->objfile;
14789 struct dwarf2_cu *cu = per_cu->cu;
14790 ULONGEST addr_base;
14791 int addr_size;
14792
14793 /* This is intended to be called from outside this file. */
14794 dw2_setup (objfile);
14795
14796 /* We need addr_base and addr_size.
14797 If we don't have PER_CU->cu, we have to get it.
14798 Nasty, but the alternative is storing the needed info in PER_CU,
14799 which at this point doesn't seem justified: it's not clear how frequently
14800 it would get used and it would increase the size of every PER_CU.
14801 Entry points like dwarf2_per_cu_addr_size do a similar thing
14802 so we're not in uncharted territory here.
14803 Alas we need to be a bit more complicated as addr_base is contained
14804 in the DIE.
14805
14806 We don't need to read the entire CU(/TU).
14807 We just need the header and top level die.
14808
14809 IWBN to use the aging mechanism to let us lazily later discard the CU.
14810 For now we skip this optimization. */
14811
14812 if (cu != NULL)
14813 {
14814 addr_base = cu->addr_base;
14815 addr_size = cu->header.addr_size;
14816 }
14817 else
14818 {
14819 struct dwarf2_read_addr_index_data aidata;
14820
14821 /* Note: We can't use init_cutu_and_read_dies_simple here,
14822 we need addr_base. */
14823 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
14824 dwarf2_read_addr_index_reader, &aidata);
14825 addr_base = aidata.addr_base;
14826 addr_size = aidata.addr_size;
14827 }
14828
14829 return read_addr_index_1 (addr_index, addr_base, addr_size);
14830 }
14831
14832 /* Given a DW_AT_str_index, fetch the string. */
14833
14834 static char *
14835 read_str_index (const struct die_reader_specs *reader,
14836 struct dwarf2_cu *cu, ULONGEST str_index)
14837 {
14838 struct objfile *objfile = dwarf2_per_objfile->objfile;
14839 const char *dwo_name = objfile->name;
14840 bfd *abfd = objfile->obfd;
14841 struct dwo_sections *sections = &reader->dwo_file->sections;
14842 gdb_byte *info_ptr;
14843 ULONGEST str_offset;
14844
14845 dwarf2_read_section (objfile, &sections->str);
14846 dwarf2_read_section (objfile, &sections->str_offsets);
14847 if (sections->str.buffer == NULL)
14848 error (_("DW_FORM_str_index used without .debug_str.dwo section"
14849 " in CU at offset 0x%lx [in module %s]"),
14850 (long) cu->header.offset.sect_off, dwo_name);
14851 if (sections->str_offsets.buffer == NULL)
14852 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
14853 " in CU at offset 0x%lx [in module %s]"),
14854 (long) cu->header.offset.sect_off, dwo_name);
14855 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
14856 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
14857 " section in CU at offset 0x%lx [in module %s]"),
14858 (long) cu->header.offset.sect_off, dwo_name);
14859 info_ptr = (sections->str_offsets.buffer
14860 + str_index * cu->header.offset_size);
14861 if (cu->header.offset_size == 4)
14862 str_offset = bfd_get_32 (abfd, info_ptr);
14863 else
14864 str_offset = bfd_get_64 (abfd, info_ptr);
14865 if (str_offset >= sections->str.size)
14866 error (_("Offset from DW_FORM_str_index pointing outside of"
14867 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
14868 (long) cu->header.offset.sect_off, dwo_name);
14869 return (char *) (sections->str.buffer + str_offset);
14870 }
14871
14872 /* Return the length of an LEB128 number in BUF. */
14873
14874 static int
14875 leb128_size (const gdb_byte *buf)
14876 {
14877 const gdb_byte *begin = buf;
14878 gdb_byte byte;
14879
14880 while (1)
14881 {
14882 byte = *buf++;
14883 if ((byte & 128) == 0)
14884 return buf - begin;
14885 }
14886 }
14887
14888 static void
14889 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
14890 {
14891 switch (lang)
14892 {
14893 case DW_LANG_C89:
14894 case DW_LANG_C99:
14895 case DW_LANG_C:
14896 cu->language = language_c;
14897 break;
14898 case DW_LANG_C_plus_plus:
14899 cu->language = language_cplus;
14900 break;
14901 case DW_LANG_D:
14902 cu->language = language_d;
14903 break;
14904 case DW_LANG_Fortran77:
14905 case DW_LANG_Fortran90:
14906 case DW_LANG_Fortran95:
14907 cu->language = language_fortran;
14908 break;
14909 case DW_LANG_Go:
14910 cu->language = language_go;
14911 break;
14912 case DW_LANG_Mips_Assembler:
14913 cu->language = language_asm;
14914 break;
14915 case DW_LANG_Java:
14916 cu->language = language_java;
14917 break;
14918 case DW_LANG_Ada83:
14919 case DW_LANG_Ada95:
14920 cu->language = language_ada;
14921 break;
14922 case DW_LANG_Modula2:
14923 cu->language = language_m2;
14924 break;
14925 case DW_LANG_Pascal83:
14926 cu->language = language_pascal;
14927 break;
14928 case DW_LANG_ObjC:
14929 cu->language = language_objc;
14930 break;
14931 case DW_LANG_Cobol74:
14932 case DW_LANG_Cobol85:
14933 default:
14934 cu->language = language_minimal;
14935 break;
14936 }
14937 cu->language_defn = language_def (cu->language);
14938 }
14939
14940 /* Return the named attribute or NULL if not there. */
14941
14942 static struct attribute *
14943 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
14944 {
14945 for (;;)
14946 {
14947 unsigned int i;
14948 struct attribute *spec = NULL;
14949
14950 for (i = 0; i < die->num_attrs; ++i)
14951 {
14952 if (die->attrs[i].name == name)
14953 return &die->attrs[i];
14954 if (die->attrs[i].name == DW_AT_specification
14955 || die->attrs[i].name == DW_AT_abstract_origin)
14956 spec = &die->attrs[i];
14957 }
14958
14959 if (!spec)
14960 break;
14961
14962 die = follow_die_ref (die, spec, &cu);
14963 }
14964
14965 return NULL;
14966 }
14967
14968 /* Return the named attribute or NULL if not there,
14969 but do not follow DW_AT_specification, etc.
14970 This is for use in contexts where we're reading .debug_types dies.
14971 Following DW_AT_specification, DW_AT_abstract_origin will take us
14972 back up the chain, and we want to go down. */
14973
14974 static struct attribute *
14975 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
14976 {
14977 unsigned int i;
14978
14979 for (i = 0; i < die->num_attrs; ++i)
14980 if (die->attrs[i].name == name)
14981 return &die->attrs[i];
14982
14983 return NULL;
14984 }
14985
14986 /* Return non-zero iff the attribute NAME is defined for the given DIE,
14987 and holds a non-zero value. This function should only be used for
14988 DW_FORM_flag or DW_FORM_flag_present attributes. */
14989
14990 static int
14991 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
14992 {
14993 struct attribute *attr = dwarf2_attr (die, name, cu);
14994
14995 return (attr && DW_UNSND (attr));
14996 }
14997
14998 static int
14999 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
15000 {
15001 /* A DIE is a declaration if it has a DW_AT_declaration attribute
15002 which value is non-zero. However, we have to be careful with
15003 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
15004 (via dwarf2_flag_true_p) follows this attribute. So we may
15005 end up accidently finding a declaration attribute that belongs
15006 to a different DIE referenced by the specification attribute,
15007 even though the given DIE does not have a declaration attribute. */
15008 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
15009 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
15010 }
15011
15012 /* Return the die giving the specification for DIE, if there is
15013 one. *SPEC_CU is the CU containing DIE on input, and the CU
15014 containing the return value on output. If there is no
15015 specification, but there is an abstract origin, that is
15016 returned. */
15017
15018 static struct die_info *
15019 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
15020 {
15021 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
15022 *spec_cu);
15023
15024 if (spec_attr == NULL)
15025 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
15026
15027 if (spec_attr == NULL)
15028 return NULL;
15029 else
15030 return follow_die_ref (die, spec_attr, spec_cu);
15031 }
15032
15033 /* Free the line_header structure *LH, and any arrays and strings it
15034 refers to.
15035 NOTE: This is also used as a "cleanup" function. */
15036
15037 static void
15038 free_line_header (struct line_header *lh)
15039 {
15040 if (lh->standard_opcode_lengths)
15041 xfree (lh->standard_opcode_lengths);
15042
15043 /* Remember that all the lh->file_names[i].name pointers are
15044 pointers into debug_line_buffer, and don't need to be freed. */
15045 if (lh->file_names)
15046 xfree (lh->file_names);
15047
15048 /* Similarly for the include directory names. */
15049 if (lh->include_dirs)
15050 xfree (lh->include_dirs);
15051
15052 xfree (lh);
15053 }
15054
15055 /* Add an entry to LH's include directory table. */
15056
15057 static void
15058 add_include_dir (struct line_header *lh, char *include_dir)
15059 {
15060 /* Grow the array if necessary. */
15061 if (lh->include_dirs_size == 0)
15062 {
15063 lh->include_dirs_size = 1; /* for testing */
15064 lh->include_dirs = xmalloc (lh->include_dirs_size
15065 * sizeof (*lh->include_dirs));
15066 }
15067 else if (lh->num_include_dirs >= lh->include_dirs_size)
15068 {
15069 lh->include_dirs_size *= 2;
15070 lh->include_dirs = xrealloc (lh->include_dirs,
15071 (lh->include_dirs_size
15072 * sizeof (*lh->include_dirs)));
15073 }
15074
15075 lh->include_dirs[lh->num_include_dirs++] = include_dir;
15076 }
15077
15078 /* Add an entry to LH's file name table. */
15079
15080 static void
15081 add_file_name (struct line_header *lh,
15082 char *name,
15083 unsigned int dir_index,
15084 unsigned int mod_time,
15085 unsigned int length)
15086 {
15087 struct file_entry *fe;
15088
15089 /* Grow the array if necessary. */
15090 if (lh->file_names_size == 0)
15091 {
15092 lh->file_names_size = 1; /* for testing */
15093 lh->file_names = xmalloc (lh->file_names_size
15094 * sizeof (*lh->file_names));
15095 }
15096 else if (lh->num_file_names >= lh->file_names_size)
15097 {
15098 lh->file_names_size *= 2;
15099 lh->file_names = xrealloc (lh->file_names,
15100 (lh->file_names_size
15101 * sizeof (*lh->file_names)));
15102 }
15103
15104 fe = &lh->file_names[lh->num_file_names++];
15105 fe->name = name;
15106 fe->dir_index = dir_index;
15107 fe->mod_time = mod_time;
15108 fe->length = length;
15109 fe->included_p = 0;
15110 fe->symtab = NULL;
15111 }
15112
15113 /* A convenience function to find the proper .debug_line section for a
15114 CU. */
15115
15116 static struct dwarf2_section_info *
15117 get_debug_line_section (struct dwarf2_cu *cu)
15118 {
15119 struct dwarf2_section_info *section;
15120
15121 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
15122 DWO file. */
15123 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15124 section = &cu->dwo_unit->dwo_file->sections.line;
15125 else if (cu->per_cu->is_dwz)
15126 {
15127 struct dwz_file *dwz = dwarf2_get_dwz_file ();
15128
15129 section = &dwz->line;
15130 }
15131 else
15132 section = &dwarf2_per_objfile->line;
15133
15134 return section;
15135 }
15136
15137 /* Read the statement program header starting at OFFSET in
15138 .debug_line, or .debug_line.dwo. Return a pointer
15139 to a struct line_header, allocated using xmalloc.
15140
15141 NOTE: the strings in the include directory and file name tables of
15142 the returned object point into the dwarf line section buffer,
15143 and must not be freed. */
15144
15145 static struct line_header *
15146 dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
15147 {
15148 struct cleanup *back_to;
15149 struct line_header *lh;
15150 gdb_byte *line_ptr;
15151 unsigned int bytes_read, offset_size;
15152 int i;
15153 char *cur_dir, *cur_file;
15154 struct dwarf2_section_info *section;
15155 bfd *abfd;
15156
15157 section = get_debug_line_section (cu);
15158 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15159 if (section->buffer == NULL)
15160 {
15161 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15162 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
15163 else
15164 complaint (&symfile_complaints, _("missing .debug_line section"));
15165 return 0;
15166 }
15167
15168 /* We can't do this until we know the section is non-empty.
15169 Only then do we know we have such a section. */
15170 abfd = section->asection->owner;
15171
15172 /* Make sure that at least there's room for the total_length field.
15173 That could be 12 bytes long, but we're just going to fudge that. */
15174 if (offset + 4 >= section->size)
15175 {
15176 dwarf2_statement_list_fits_in_line_number_section_complaint ();
15177 return 0;
15178 }
15179
15180 lh = xmalloc (sizeof (*lh));
15181 memset (lh, 0, sizeof (*lh));
15182 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
15183 (void *) lh);
15184
15185 line_ptr = section->buffer + offset;
15186
15187 /* Read in the header. */
15188 lh->total_length =
15189 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
15190 &bytes_read, &offset_size);
15191 line_ptr += bytes_read;
15192 if (line_ptr + lh->total_length > (section->buffer + section->size))
15193 {
15194 dwarf2_statement_list_fits_in_line_number_section_complaint ();
15195 return 0;
15196 }
15197 lh->statement_program_end = line_ptr + lh->total_length;
15198 lh->version = read_2_bytes (abfd, line_ptr);
15199 line_ptr += 2;
15200 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
15201 line_ptr += offset_size;
15202 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
15203 line_ptr += 1;
15204 if (lh->version >= 4)
15205 {
15206 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
15207 line_ptr += 1;
15208 }
15209 else
15210 lh->maximum_ops_per_instruction = 1;
15211
15212 if (lh->maximum_ops_per_instruction == 0)
15213 {
15214 lh->maximum_ops_per_instruction = 1;
15215 complaint (&symfile_complaints,
15216 _("invalid maximum_ops_per_instruction "
15217 "in `.debug_line' section"));
15218 }
15219
15220 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
15221 line_ptr += 1;
15222 lh->line_base = read_1_signed_byte (abfd, line_ptr);
15223 line_ptr += 1;
15224 lh->line_range = read_1_byte (abfd, line_ptr);
15225 line_ptr += 1;
15226 lh->opcode_base = read_1_byte (abfd, line_ptr);
15227 line_ptr += 1;
15228 lh->standard_opcode_lengths
15229 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
15230
15231 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
15232 for (i = 1; i < lh->opcode_base; ++i)
15233 {
15234 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
15235 line_ptr += 1;
15236 }
15237
15238 /* Read directory table. */
15239 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
15240 {
15241 line_ptr += bytes_read;
15242 add_include_dir (lh, cur_dir);
15243 }
15244 line_ptr += bytes_read;
15245
15246 /* Read file name table. */
15247 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
15248 {
15249 unsigned int dir_index, mod_time, length;
15250
15251 line_ptr += bytes_read;
15252 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15253 line_ptr += bytes_read;
15254 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15255 line_ptr += bytes_read;
15256 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15257 line_ptr += bytes_read;
15258
15259 add_file_name (lh, cur_file, dir_index, mod_time, length);
15260 }
15261 line_ptr += bytes_read;
15262 lh->statement_program_start = line_ptr;
15263
15264 if (line_ptr > (section->buffer + section->size))
15265 complaint (&symfile_complaints,
15266 _("line number info header doesn't "
15267 "fit in `.debug_line' section"));
15268
15269 discard_cleanups (back_to);
15270 return lh;
15271 }
15272
15273 /* Subroutine of dwarf_decode_lines to simplify it.
15274 Return the file name of the psymtab for included file FILE_INDEX
15275 in line header LH of PST.
15276 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15277 If space for the result is malloc'd, it will be freed by a cleanup.
15278 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
15279
15280 The function creates dangling cleanup registration. */
15281
15282 static char *
15283 psymtab_include_file_name (const struct line_header *lh, int file_index,
15284 const struct partial_symtab *pst,
15285 const char *comp_dir)
15286 {
15287 const struct file_entry fe = lh->file_names [file_index];
15288 char *include_name = fe.name;
15289 char *include_name_to_compare = include_name;
15290 char *dir_name = NULL;
15291 const char *pst_filename;
15292 char *copied_name = NULL;
15293 int file_is_pst;
15294
15295 if (fe.dir_index)
15296 dir_name = lh->include_dirs[fe.dir_index - 1];
15297
15298 if (!IS_ABSOLUTE_PATH (include_name)
15299 && (dir_name != NULL || comp_dir != NULL))
15300 {
15301 /* Avoid creating a duplicate psymtab for PST.
15302 We do this by comparing INCLUDE_NAME and PST_FILENAME.
15303 Before we do the comparison, however, we need to account
15304 for DIR_NAME and COMP_DIR.
15305 First prepend dir_name (if non-NULL). If we still don't
15306 have an absolute path prepend comp_dir (if non-NULL).
15307 However, the directory we record in the include-file's
15308 psymtab does not contain COMP_DIR (to match the
15309 corresponding symtab(s)).
15310
15311 Example:
15312
15313 bash$ cd /tmp
15314 bash$ gcc -g ./hello.c
15315 include_name = "hello.c"
15316 dir_name = "."
15317 DW_AT_comp_dir = comp_dir = "/tmp"
15318 DW_AT_name = "./hello.c" */
15319
15320 if (dir_name != NULL)
15321 {
15322 include_name = concat (dir_name, SLASH_STRING,
15323 include_name, (char *)NULL);
15324 include_name_to_compare = include_name;
15325 make_cleanup (xfree, include_name);
15326 }
15327 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
15328 {
15329 include_name_to_compare = concat (comp_dir, SLASH_STRING,
15330 include_name, (char *)NULL);
15331 }
15332 }
15333
15334 pst_filename = pst->filename;
15335 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
15336 {
15337 copied_name = concat (pst->dirname, SLASH_STRING,
15338 pst_filename, (char *)NULL);
15339 pst_filename = copied_name;
15340 }
15341
15342 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
15343
15344 if (include_name_to_compare != include_name)
15345 xfree (include_name_to_compare);
15346 if (copied_name != NULL)
15347 xfree (copied_name);
15348
15349 if (file_is_pst)
15350 return NULL;
15351 return include_name;
15352 }
15353
15354 /* Ignore this record_line request. */
15355
15356 static void
15357 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
15358 {
15359 return;
15360 }
15361
15362 /* Subroutine of dwarf_decode_lines to simplify it.
15363 Process the line number information in LH. */
15364
15365 static void
15366 dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
15367 struct dwarf2_cu *cu, struct partial_symtab *pst)
15368 {
15369 gdb_byte *line_ptr, *extended_end;
15370 gdb_byte *line_end;
15371 unsigned int bytes_read, extended_len;
15372 unsigned char op_code, extended_op, adj_opcode;
15373 CORE_ADDR baseaddr;
15374 struct objfile *objfile = cu->objfile;
15375 bfd *abfd = objfile->obfd;
15376 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15377 const int decode_for_pst_p = (pst != NULL);
15378 struct subfile *last_subfile = NULL;
15379 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
15380 = record_line;
15381
15382 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15383
15384 line_ptr = lh->statement_program_start;
15385 line_end = lh->statement_program_end;
15386
15387 /* Read the statement sequences until there's nothing left. */
15388 while (line_ptr < line_end)
15389 {
15390 /* state machine registers */
15391 CORE_ADDR address = 0;
15392 unsigned int file = 1;
15393 unsigned int line = 1;
15394 unsigned int column = 0;
15395 int is_stmt = lh->default_is_stmt;
15396 int basic_block = 0;
15397 int end_sequence = 0;
15398 CORE_ADDR addr;
15399 unsigned char op_index = 0;
15400
15401 if (!decode_for_pst_p && lh->num_file_names >= file)
15402 {
15403 /* Start a subfile for the current file of the state machine. */
15404 /* lh->include_dirs and lh->file_names are 0-based, but the
15405 directory and file name numbers in the statement program
15406 are 1-based. */
15407 struct file_entry *fe = &lh->file_names[file - 1];
15408 char *dir = NULL;
15409
15410 if (fe->dir_index)
15411 dir = lh->include_dirs[fe->dir_index - 1];
15412
15413 dwarf2_start_subfile (fe->name, dir, comp_dir);
15414 }
15415
15416 /* Decode the table. */
15417 while (!end_sequence)
15418 {
15419 op_code = read_1_byte (abfd, line_ptr);
15420 line_ptr += 1;
15421 if (line_ptr > line_end)
15422 {
15423 dwarf2_debug_line_missing_end_sequence_complaint ();
15424 break;
15425 }
15426
15427 if (op_code >= lh->opcode_base)
15428 {
15429 /* Special operand. */
15430 adj_opcode = op_code - lh->opcode_base;
15431 address += (((op_index + (adj_opcode / lh->line_range))
15432 / lh->maximum_ops_per_instruction)
15433 * lh->minimum_instruction_length);
15434 op_index = ((op_index + (adj_opcode / lh->line_range))
15435 % lh->maximum_ops_per_instruction);
15436 line += lh->line_base + (adj_opcode % lh->line_range);
15437 if (lh->num_file_names < file || file == 0)
15438 dwarf2_debug_line_missing_file_complaint ();
15439 /* For now we ignore lines not starting on an
15440 instruction boundary. */
15441 else if (op_index == 0)
15442 {
15443 lh->file_names[file - 1].included_p = 1;
15444 if (!decode_for_pst_p && is_stmt)
15445 {
15446 if (last_subfile != current_subfile)
15447 {
15448 addr = gdbarch_addr_bits_remove (gdbarch, address);
15449 if (last_subfile)
15450 (*p_record_line) (last_subfile, 0, addr);
15451 last_subfile = current_subfile;
15452 }
15453 /* Append row to matrix using current values. */
15454 addr = gdbarch_addr_bits_remove (gdbarch, address);
15455 (*p_record_line) (current_subfile, line, addr);
15456 }
15457 }
15458 basic_block = 0;
15459 }
15460 else switch (op_code)
15461 {
15462 case DW_LNS_extended_op:
15463 extended_len = read_unsigned_leb128 (abfd, line_ptr,
15464 &bytes_read);
15465 line_ptr += bytes_read;
15466 extended_end = line_ptr + extended_len;
15467 extended_op = read_1_byte (abfd, line_ptr);
15468 line_ptr += 1;
15469 switch (extended_op)
15470 {
15471 case DW_LNE_end_sequence:
15472 p_record_line = record_line;
15473 end_sequence = 1;
15474 break;
15475 case DW_LNE_set_address:
15476 address = read_address (abfd, line_ptr, cu, &bytes_read);
15477
15478 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
15479 {
15480 /* This line table is for a function which has been
15481 GCd by the linker. Ignore it. PR gdb/12528 */
15482
15483 long line_offset
15484 = line_ptr - get_debug_line_section (cu)->buffer;
15485
15486 complaint (&symfile_complaints,
15487 _(".debug_line address at offset 0x%lx is 0 "
15488 "[in module %s]"),
15489 line_offset, objfile->name);
15490 p_record_line = noop_record_line;
15491 }
15492
15493 op_index = 0;
15494 line_ptr += bytes_read;
15495 address += baseaddr;
15496 break;
15497 case DW_LNE_define_file:
15498 {
15499 char *cur_file;
15500 unsigned int dir_index, mod_time, length;
15501
15502 cur_file = read_direct_string (abfd, line_ptr,
15503 &bytes_read);
15504 line_ptr += bytes_read;
15505 dir_index =
15506 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15507 line_ptr += bytes_read;
15508 mod_time =
15509 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15510 line_ptr += bytes_read;
15511 length =
15512 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15513 line_ptr += bytes_read;
15514 add_file_name (lh, cur_file, dir_index, mod_time, length);
15515 }
15516 break;
15517 case DW_LNE_set_discriminator:
15518 /* The discriminator is not interesting to the debugger;
15519 just ignore it. */
15520 line_ptr = extended_end;
15521 break;
15522 default:
15523 complaint (&symfile_complaints,
15524 _("mangled .debug_line section"));
15525 return;
15526 }
15527 /* Make sure that we parsed the extended op correctly. If e.g.
15528 we expected a different address size than the producer used,
15529 we may have read the wrong number of bytes. */
15530 if (line_ptr != extended_end)
15531 {
15532 complaint (&symfile_complaints,
15533 _("mangled .debug_line section"));
15534 return;
15535 }
15536 break;
15537 case DW_LNS_copy:
15538 if (lh->num_file_names < file || file == 0)
15539 dwarf2_debug_line_missing_file_complaint ();
15540 else
15541 {
15542 lh->file_names[file - 1].included_p = 1;
15543 if (!decode_for_pst_p && is_stmt)
15544 {
15545 if (last_subfile != current_subfile)
15546 {
15547 addr = gdbarch_addr_bits_remove (gdbarch, address);
15548 if (last_subfile)
15549 (*p_record_line) (last_subfile, 0, addr);
15550 last_subfile = current_subfile;
15551 }
15552 addr = gdbarch_addr_bits_remove (gdbarch, address);
15553 (*p_record_line) (current_subfile, line, addr);
15554 }
15555 }
15556 basic_block = 0;
15557 break;
15558 case DW_LNS_advance_pc:
15559 {
15560 CORE_ADDR adjust
15561 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15562
15563 address += (((op_index + adjust)
15564 / lh->maximum_ops_per_instruction)
15565 * lh->minimum_instruction_length);
15566 op_index = ((op_index + adjust)
15567 % lh->maximum_ops_per_instruction);
15568 line_ptr += bytes_read;
15569 }
15570 break;
15571 case DW_LNS_advance_line:
15572 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
15573 line_ptr += bytes_read;
15574 break;
15575 case DW_LNS_set_file:
15576 {
15577 /* The arrays lh->include_dirs and lh->file_names are
15578 0-based, but the directory and file name numbers in
15579 the statement program are 1-based. */
15580 struct file_entry *fe;
15581 char *dir = NULL;
15582
15583 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15584 line_ptr += bytes_read;
15585 if (lh->num_file_names < file || file == 0)
15586 dwarf2_debug_line_missing_file_complaint ();
15587 else
15588 {
15589 fe = &lh->file_names[file - 1];
15590 if (fe->dir_index)
15591 dir = lh->include_dirs[fe->dir_index - 1];
15592 if (!decode_for_pst_p)
15593 {
15594 last_subfile = current_subfile;
15595 dwarf2_start_subfile (fe->name, dir, comp_dir);
15596 }
15597 }
15598 }
15599 break;
15600 case DW_LNS_set_column:
15601 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15602 line_ptr += bytes_read;
15603 break;
15604 case DW_LNS_negate_stmt:
15605 is_stmt = (!is_stmt);
15606 break;
15607 case DW_LNS_set_basic_block:
15608 basic_block = 1;
15609 break;
15610 /* Add to the address register of the state machine the
15611 address increment value corresponding to special opcode
15612 255. I.e., this value is scaled by the minimum
15613 instruction length since special opcode 255 would have
15614 scaled the increment. */
15615 case DW_LNS_const_add_pc:
15616 {
15617 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
15618
15619 address += (((op_index + adjust)
15620 / lh->maximum_ops_per_instruction)
15621 * lh->minimum_instruction_length);
15622 op_index = ((op_index + adjust)
15623 % lh->maximum_ops_per_instruction);
15624 }
15625 break;
15626 case DW_LNS_fixed_advance_pc:
15627 address += read_2_bytes (abfd, line_ptr);
15628 op_index = 0;
15629 line_ptr += 2;
15630 break;
15631 default:
15632 {
15633 /* Unknown standard opcode, ignore it. */
15634 int i;
15635
15636 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
15637 {
15638 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15639 line_ptr += bytes_read;
15640 }
15641 }
15642 }
15643 }
15644 if (lh->num_file_names < file || file == 0)
15645 dwarf2_debug_line_missing_file_complaint ();
15646 else
15647 {
15648 lh->file_names[file - 1].included_p = 1;
15649 if (!decode_for_pst_p)
15650 {
15651 addr = gdbarch_addr_bits_remove (gdbarch, address);
15652 (*p_record_line) (current_subfile, 0, addr);
15653 }
15654 }
15655 }
15656 }
15657
15658 /* Decode the Line Number Program (LNP) for the given line_header
15659 structure and CU. The actual information extracted and the type
15660 of structures created from the LNP depends on the value of PST.
15661
15662 1. If PST is NULL, then this procedure uses the data from the program
15663 to create all necessary symbol tables, and their linetables.
15664
15665 2. If PST is not NULL, this procedure reads the program to determine
15666 the list of files included by the unit represented by PST, and
15667 builds all the associated partial symbol tables.
15668
15669 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15670 It is used for relative paths in the line table.
15671 NOTE: When processing partial symtabs (pst != NULL),
15672 comp_dir == pst->dirname.
15673
15674 NOTE: It is important that psymtabs have the same file name (via strcmp)
15675 as the corresponding symtab. Since COMP_DIR is not used in the name of the
15676 symtab we don't use it in the name of the psymtabs we create.
15677 E.g. expand_line_sal requires this when finding psymtabs to expand.
15678 A good testcase for this is mb-inline.exp. */
15679
15680 static void
15681 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
15682 struct dwarf2_cu *cu, struct partial_symtab *pst,
15683 int want_line_info)
15684 {
15685 struct objfile *objfile = cu->objfile;
15686 const int decode_for_pst_p = (pst != NULL);
15687 struct subfile *first_subfile = current_subfile;
15688
15689 if (want_line_info)
15690 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
15691
15692 if (decode_for_pst_p)
15693 {
15694 int file_index;
15695
15696 /* Now that we're done scanning the Line Header Program, we can
15697 create the psymtab of each included file. */
15698 for (file_index = 0; file_index < lh->num_file_names; file_index++)
15699 if (lh->file_names[file_index].included_p == 1)
15700 {
15701 char *include_name =
15702 psymtab_include_file_name (lh, file_index, pst, comp_dir);
15703 if (include_name != NULL)
15704 dwarf2_create_include_psymtab (include_name, pst, objfile);
15705 }
15706 }
15707 else
15708 {
15709 /* Make sure a symtab is created for every file, even files
15710 which contain only variables (i.e. no code with associated
15711 line numbers). */
15712 int i;
15713
15714 for (i = 0; i < lh->num_file_names; i++)
15715 {
15716 char *dir = NULL;
15717 struct file_entry *fe;
15718
15719 fe = &lh->file_names[i];
15720 if (fe->dir_index)
15721 dir = lh->include_dirs[fe->dir_index - 1];
15722 dwarf2_start_subfile (fe->name, dir, comp_dir);
15723
15724 /* Skip the main file; we don't need it, and it must be
15725 allocated last, so that it will show up before the
15726 non-primary symtabs in the objfile's symtab list. */
15727 if (current_subfile == first_subfile)
15728 continue;
15729
15730 if (current_subfile->symtab == NULL)
15731 current_subfile->symtab = allocate_symtab (current_subfile->name,
15732 objfile);
15733 fe->symtab = current_subfile->symtab;
15734 }
15735 }
15736 }
15737
15738 /* Start a subfile for DWARF. FILENAME is the name of the file and
15739 DIRNAME the name of the source directory which contains FILENAME
15740 or NULL if not known. COMP_DIR is the compilation directory for the
15741 linetable's compilation unit or NULL if not known.
15742 This routine tries to keep line numbers from identical absolute and
15743 relative file names in a common subfile.
15744
15745 Using the `list' example from the GDB testsuite, which resides in
15746 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
15747 of /srcdir/list0.c yields the following debugging information for list0.c:
15748
15749 DW_AT_name: /srcdir/list0.c
15750 DW_AT_comp_dir: /compdir
15751 files.files[0].name: list0.h
15752 files.files[0].dir: /srcdir
15753 files.files[1].name: list0.c
15754 files.files[1].dir: /srcdir
15755
15756 The line number information for list0.c has to end up in a single
15757 subfile, so that `break /srcdir/list0.c:1' works as expected.
15758 start_subfile will ensure that this happens provided that we pass the
15759 concatenation of files.files[1].dir and files.files[1].name as the
15760 subfile's name. */
15761
15762 static void
15763 dwarf2_start_subfile (char *filename, const char *dirname,
15764 const char *comp_dir)
15765 {
15766 char *fullname;
15767
15768 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
15769 `start_symtab' will always pass the contents of DW_AT_comp_dir as
15770 second argument to start_subfile. To be consistent, we do the
15771 same here. In order not to lose the line information directory,
15772 we concatenate it to the filename when it makes sense.
15773 Note that the Dwarf3 standard says (speaking of filenames in line
15774 information): ``The directory index is ignored for file names
15775 that represent full path names''. Thus ignoring dirname in the
15776 `else' branch below isn't an issue. */
15777
15778 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
15779 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
15780 else
15781 fullname = filename;
15782
15783 start_subfile (fullname, comp_dir);
15784
15785 if (fullname != filename)
15786 xfree (fullname);
15787 }
15788
15789 /* Start a symtab for DWARF.
15790 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
15791
15792 static void
15793 dwarf2_start_symtab (struct dwarf2_cu *cu,
15794 const char *name, const char *comp_dir, CORE_ADDR low_pc)
15795 {
15796 start_symtab (name, comp_dir, low_pc);
15797 record_debugformat ("DWARF 2");
15798 record_producer (cu->producer);
15799
15800 /* We assume that we're processing GCC output. */
15801 processing_gcc_compilation = 2;
15802
15803 cu->processing_has_namespace_info = 0;
15804 }
15805
15806 static void
15807 var_decode_location (struct attribute *attr, struct symbol *sym,
15808 struct dwarf2_cu *cu)
15809 {
15810 struct objfile *objfile = cu->objfile;
15811 struct comp_unit_head *cu_header = &cu->header;
15812
15813 /* NOTE drow/2003-01-30: There used to be a comment and some special
15814 code here to turn a symbol with DW_AT_external and a
15815 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
15816 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
15817 with some versions of binutils) where shared libraries could have
15818 relocations against symbols in their debug information - the
15819 minimal symbol would have the right address, but the debug info
15820 would not. It's no longer necessary, because we will explicitly
15821 apply relocations when we read in the debug information now. */
15822
15823 /* A DW_AT_location attribute with no contents indicates that a
15824 variable has been optimized away. */
15825 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
15826 {
15827 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
15828 return;
15829 }
15830
15831 /* Handle one degenerate form of location expression specially, to
15832 preserve GDB's previous behavior when section offsets are
15833 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
15834 then mark this symbol as LOC_STATIC. */
15835
15836 if (attr_form_is_block (attr)
15837 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
15838 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
15839 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
15840 && (DW_BLOCK (attr)->size
15841 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
15842 {
15843 unsigned int dummy;
15844
15845 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
15846 SYMBOL_VALUE_ADDRESS (sym) =
15847 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
15848 else
15849 SYMBOL_VALUE_ADDRESS (sym) =
15850 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
15851 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
15852 fixup_symbol_section (sym, objfile);
15853 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
15854 SYMBOL_SECTION (sym));
15855 return;
15856 }
15857
15858 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
15859 expression evaluator, and use LOC_COMPUTED only when necessary
15860 (i.e. when the value of a register or memory location is
15861 referenced, or a thread-local block, etc.). Then again, it might
15862 not be worthwhile. I'm assuming that it isn't unless performance
15863 or memory numbers show me otherwise. */
15864
15865 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
15866
15867 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
15868 cu->has_loclist = 1;
15869 }
15870
15871 /* Given a pointer to a DWARF information entry, figure out if we need
15872 to make a symbol table entry for it, and if so, create a new entry
15873 and return a pointer to it.
15874 If TYPE is NULL, determine symbol type from the die, otherwise
15875 used the passed type.
15876 If SPACE is not NULL, use it to hold the new symbol. If it is
15877 NULL, allocate a new symbol on the objfile's obstack. */
15878
15879 static struct symbol *
15880 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
15881 struct symbol *space)
15882 {
15883 struct objfile *objfile = cu->objfile;
15884 struct symbol *sym = NULL;
15885 const char *name;
15886 struct attribute *attr = NULL;
15887 struct attribute *attr2 = NULL;
15888 CORE_ADDR baseaddr;
15889 struct pending **list_to_add = NULL;
15890
15891 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
15892
15893 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15894
15895 name = dwarf2_name (die, cu);
15896 if (name)
15897 {
15898 const char *linkagename;
15899 int suppress_add = 0;
15900
15901 if (space)
15902 sym = space;
15903 else
15904 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
15905 OBJSTAT (objfile, n_syms++);
15906
15907 /* Cache this symbol's name and the name's demangled form (if any). */
15908 SYMBOL_SET_LANGUAGE (sym, cu->language);
15909 linkagename = dwarf2_physname (name, die, cu);
15910 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
15911
15912 /* Fortran does not have mangling standard and the mangling does differ
15913 between gfortran, iFort etc. */
15914 if (cu->language == language_fortran
15915 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
15916 symbol_set_demangled_name (&(sym->ginfo),
15917 dwarf2_full_name (name, die, cu),
15918 NULL);
15919
15920 /* Default assumptions.
15921 Use the passed type or decode it from the die. */
15922 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
15923 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
15924 if (type != NULL)
15925 SYMBOL_TYPE (sym) = type;
15926 else
15927 SYMBOL_TYPE (sym) = die_type (die, cu);
15928 attr = dwarf2_attr (die,
15929 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
15930 cu);
15931 if (attr)
15932 {
15933 SYMBOL_LINE (sym) = DW_UNSND (attr);
15934 }
15935
15936 attr = dwarf2_attr (die,
15937 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
15938 cu);
15939 if (attr)
15940 {
15941 int file_index = DW_UNSND (attr);
15942
15943 if (cu->line_header == NULL
15944 || file_index > cu->line_header->num_file_names)
15945 complaint (&symfile_complaints,
15946 _("file index out of range"));
15947 else if (file_index > 0)
15948 {
15949 struct file_entry *fe;
15950
15951 fe = &cu->line_header->file_names[file_index - 1];
15952 SYMBOL_SYMTAB (sym) = fe->symtab;
15953 }
15954 }
15955
15956 switch (die->tag)
15957 {
15958 case DW_TAG_label:
15959 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
15960 if (attr)
15961 {
15962 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
15963 }
15964 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
15965 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
15966 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
15967 add_symbol_to_list (sym, cu->list_in_scope);
15968 break;
15969 case DW_TAG_subprogram:
15970 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15971 finish_block. */
15972 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
15973 attr2 = dwarf2_attr (die, DW_AT_external, cu);
15974 if ((attr2 && (DW_UNSND (attr2) != 0))
15975 || cu->language == language_ada)
15976 {
15977 /* Subprograms marked external are stored as a global symbol.
15978 Ada subprograms, whether marked external or not, are always
15979 stored as a global symbol, because we want to be able to
15980 access them globally. For instance, we want to be able
15981 to break on a nested subprogram without having to
15982 specify the context. */
15983 list_to_add = &global_symbols;
15984 }
15985 else
15986 {
15987 list_to_add = cu->list_in_scope;
15988 }
15989 break;
15990 case DW_TAG_inlined_subroutine:
15991 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15992 finish_block. */
15993 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
15994 SYMBOL_INLINED (sym) = 1;
15995 list_to_add = cu->list_in_scope;
15996 break;
15997 case DW_TAG_template_value_param:
15998 suppress_add = 1;
15999 /* Fall through. */
16000 case DW_TAG_constant:
16001 case DW_TAG_variable:
16002 case DW_TAG_member:
16003 /* Compilation with minimal debug info may result in
16004 variables with missing type entries. Change the
16005 misleading `void' type to something sensible. */
16006 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
16007 SYMBOL_TYPE (sym)
16008 = objfile_type (objfile)->nodebug_data_symbol;
16009
16010 attr = dwarf2_attr (die, DW_AT_const_value, cu);
16011 /* In the case of DW_TAG_member, we should only be called for
16012 static const members. */
16013 if (die->tag == DW_TAG_member)
16014 {
16015 /* dwarf2_add_field uses die_is_declaration,
16016 so we do the same. */
16017 gdb_assert (die_is_declaration (die, cu));
16018 gdb_assert (attr);
16019 }
16020 if (attr)
16021 {
16022 dwarf2_const_value (attr, sym, cu);
16023 attr2 = dwarf2_attr (die, DW_AT_external, cu);
16024 if (!suppress_add)
16025 {
16026 if (attr2 && (DW_UNSND (attr2) != 0))
16027 list_to_add = &global_symbols;
16028 else
16029 list_to_add = cu->list_in_scope;
16030 }
16031 break;
16032 }
16033 attr = dwarf2_attr (die, DW_AT_location, cu);
16034 if (attr)
16035 {
16036 var_decode_location (attr, sym, cu);
16037 attr2 = dwarf2_attr (die, DW_AT_external, cu);
16038
16039 /* Fortran explicitly imports any global symbols to the local
16040 scope by DW_TAG_common_block. */
16041 if (cu->language == language_fortran && die->parent
16042 && die->parent->tag == DW_TAG_common_block)
16043 attr2 = NULL;
16044
16045 if (SYMBOL_CLASS (sym) == LOC_STATIC
16046 && SYMBOL_VALUE_ADDRESS (sym) == 0
16047 && !dwarf2_per_objfile->has_section_at_zero)
16048 {
16049 /* When a static variable is eliminated by the linker,
16050 the corresponding debug information is not stripped
16051 out, but the variable address is set to null;
16052 do not add such variables into symbol table. */
16053 }
16054 else if (attr2 && (DW_UNSND (attr2) != 0))
16055 {
16056 /* Workaround gfortran PR debug/40040 - it uses
16057 DW_AT_location for variables in -fPIC libraries which may
16058 get overriden by other libraries/executable and get
16059 a different address. Resolve it by the minimal symbol
16060 which may come from inferior's executable using copy
16061 relocation. Make this workaround only for gfortran as for
16062 other compilers GDB cannot guess the minimal symbol
16063 Fortran mangling kind. */
16064 if (cu->language == language_fortran && die->parent
16065 && die->parent->tag == DW_TAG_module
16066 && cu->producer
16067 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
16068 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
16069
16070 /* A variable with DW_AT_external is never static,
16071 but it may be block-scoped. */
16072 list_to_add = (cu->list_in_scope == &file_symbols
16073 ? &global_symbols : cu->list_in_scope);
16074 }
16075 else
16076 list_to_add = cu->list_in_scope;
16077 }
16078 else
16079 {
16080 /* We do not know the address of this symbol.
16081 If it is an external symbol and we have type information
16082 for it, enter the symbol as a LOC_UNRESOLVED symbol.
16083 The address of the variable will then be determined from
16084 the minimal symbol table whenever the variable is
16085 referenced. */
16086 attr2 = dwarf2_attr (die, DW_AT_external, cu);
16087
16088 /* Fortran explicitly imports any global symbols to the local
16089 scope by DW_TAG_common_block. */
16090 if (cu->language == language_fortran && die->parent
16091 && die->parent->tag == DW_TAG_common_block)
16092 {
16093 /* SYMBOL_CLASS doesn't matter here because
16094 read_common_block is going to reset it. */
16095 if (!suppress_add)
16096 list_to_add = cu->list_in_scope;
16097 }
16098 else if (attr2 && (DW_UNSND (attr2) != 0)
16099 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
16100 {
16101 /* A variable with DW_AT_external is never static, but it
16102 may be block-scoped. */
16103 list_to_add = (cu->list_in_scope == &file_symbols
16104 ? &global_symbols : cu->list_in_scope);
16105
16106 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
16107 }
16108 else if (!die_is_declaration (die, cu))
16109 {
16110 /* Use the default LOC_OPTIMIZED_OUT class. */
16111 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
16112 if (!suppress_add)
16113 list_to_add = cu->list_in_scope;
16114 }
16115 }
16116 break;
16117 case DW_TAG_formal_parameter:
16118 /* If we are inside a function, mark this as an argument. If
16119 not, we might be looking at an argument to an inlined function
16120 when we do not have enough information to show inlined frames;
16121 pretend it's a local variable in that case so that the user can
16122 still see it. */
16123 if (context_stack_depth > 0
16124 && context_stack[context_stack_depth - 1].name != NULL)
16125 SYMBOL_IS_ARGUMENT (sym) = 1;
16126 attr = dwarf2_attr (die, DW_AT_location, cu);
16127 if (attr)
16128 {
16129 var_decode_location (attr, sym, cu);
16130 }
16131 attr = dwarf2_attr (die, DW_AT_const_value, cu);
16132 if (attr)
16133 {
16134 dwarf2_const_value (attr, sym, cu);
16135 }
16136
16137 list_to_add = cu->list_in_scope;
16138 break;
16139 case DW_TAG_unspecified_parameters:
16140 /* From varargs functions; gdb doesn't seem to have any
16141 interest in this information, so just ignore it for now.
16142 (FIXME?) */
16143 break;
16144 case DW_TAG_template_type_param:
16145 suppress_add = 1;
16146 /* Fall through. */
16147 case DW_TAG_class_type:
16148 case DW_TAG_interface_type:
16149 case DW_TAG_structure_type:
16150 case DW_TAG_union_type:
16151 case DW_TAG_set_type:
16152 case DW_TAG_enumeration_type:
16153 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16154 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
16155
16156 {
16157 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
16158 really ever be static objects: otherwise, if you try
16159 to, say, break of a class's method and you're in a file
16160 which doesn't mention that class, it won't work unless
16161 the check for all static symbols in lookup_symbol_aux
16162 saves you. See the OtherFileClass tests in
16163 gdb.c++/namespace.exp. */
16164
16165 if (!suppress_add)
16166 {
16167 list_to_add = (cu->list_in_scope == &file_symbols
16168 && (cu->language == language_cplus
16169 || cu->language == language_java)
16170 ? &global_symbols : cu->list_in_scope);
16171
16172 /* The semantics of C++ state that "struct foo {
16173 ... }" also defines a typedef for "foo". A Java
16174 class declaration also defines a typedef for the
16175 class. */
16176 if (cu->language == language_cplus
16177 || cu->language == language_java
16178 || cu->language == language_ada)
16179 {
16180 /* The symbol's name is already allocated along
16181 with this objfile, so we don't need to
16182 duplicate it for the type. */
16183 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
16184 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
16185 }
16186 }
16187 }
16188 break;
16189 case DW_TAG_typedef:
16190 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16191 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
16192 list_to_add = cu->list_in_scope;
16193 break;
16194 case DW_TAG_base_type:
16195 case DW_TAG_subrange_type:
16196 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16197 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
16198 list_to_add = cu->list_in_scope;
16199 break;
16200 case DW_TAG_enumerator:
16201 attr = dwarf2_attr (die, DW_AT_const_value, cu);
16202 if (attr)
16203 {
16204 dwarf2_const_value (attr, sym, cu);
16205 }
16206 {
16207 /* NOTE: carlton/2003-11-10: See comment above in the
16208 DW_TAG_class_type, etc. block. */
16209
16210 list_to_add = (cu->list_in_scope == &file_symbols
16211 && (cu->language == language_cplus
16212 || cu->language == language_java)
16213 ? &global_symbols : cu->list_in_scope);
16214 }
16215 break;
16216 case DW_TAG_namespace:
16217 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16218 list_to_add = &global_symbols;
16219 break;
16220 case DW_TAG_common_block:
16221 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
16222 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
16223 add_symbol_to_list (sym, cu->list_in_scope);
16224 break;
16225 default:
16226 /* Not a tag we recognize. Hopefully we aren't processing
16227 trash data, but since we must specifically ignore things
16228 we don't recognize, there is nothing else we should do at
16229 this point. */
16230 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
16231 dwarf_tag_name (die->tag));
16232 break;
16233 }
16234
16235 if (suppress_add)
16236 {
16237 sym->hash_next = objfile->template_symbols;
16238 objfile->template_symbols = sym;
16239 list_to_add = NULL;
16240 }
16241
16242 if (list_to_add != NULL)
16243 add_symbol_to_list (sym, list_to_add);
16244
16245 /* For the benefit of old versions of GCC, check for anonymous
16246 namespaces based on the demangled name. */
16247 if (!cu->processing_has_namespace_info
16248 && cu->language == language_cplus)
16249 cp_scan_for_anonymous_namespaces (sym, objfile);
16250 }
16251 return (sym);
16252 }
16253
16254 /* A wrapper for new_symbol_full that always allocates a new symbol. */
16255
16256 static struct symbol *
16257 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16258 {
16259 return new_symbol_full (die, type, cu, NULL);
16260 }
16261
16262 /* Given an attr with a DW_FORM_dataN value in host byte order,
16263 zero-extend it as appropriate for the symbol's type. The DWARF
16264 standard (v4) is not entirely clear about the meaning of using
16265 DW_FORM_dataN for a constant with a signed type, where the type is
16266 wider than the data. The conclusion of a discussion on the DWARF
16267 list was that this is unspecified. We choose to always zero-extend
16268 because that is the interpretation long in use by GCC. */
16269
16270 static gdb_byte *
16271 dwarf2_const_value_data (struct attribute *attr, struct type *type,
16272 const char *name, struct obstack *obstack,
16273 struct dwarf2_cu *cu, LONGEST *value, int bits)
16274 {
16275 struct objfile *objfile = cu->objfile;
16276 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
16277 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
16278 LONGEST l = DW_UNSND (attr);
16279
16280 if (bits < sizeof (*value) * 8)
16281 {
16282 l &= ((LONGEST) 1 << bits) - 1;
16283 *value = l;
16284 }
16285 else if (bits == sizeof (*value) * 8)
16286 *value = l;
16287 else
16288 {
16289 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
16290 store_unsigned_integer (bytes, bits / 8, byte_order, l);
16291 return bytes;
16292 }
16293
16294 return NULL;
16295 }
16296
16297 /* Read a constant value from an attribute. Either set *VALUE, or if
16298 the value does not fit in *VALUE, set *BYTES - either already
16299 allocated on the objfile obstack, or newly allocated on OBSTACK,
16300 or, set *BATON, if we translated the constant to a location
16301 expression. */
16302
16303 static void
16304 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
16305 const char *name, struct obstack *obstack,
16306 struct dwarf2_cu *cu,
16307 LONGEST *value, gdb_byte **bytes,
16308 struct dwarf2_locexpr_baton **baton)
16309 {
16310 struct objfile *objfile = cu->objfile;
16311 struct comp_unit_head *cu_header = &cu->header;
16312 struct dwarf_block *blk;
16313 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
16314 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
16315
16316 *value = 0;
16317 *bytes = NULL;
16318 *baton = NULL;
16319
16320 switch (attr->form)
16321 {
16322 case DW_FORM_addr:
16323 case DW_FORM_GNU_addr_index:
16324 {
16325 gdb_byte *data;
16326
16327 if (TYPE_LENGTH (type) != cu_header->addr_size)
16328 dwarf2_const_value_length_mismatch_complaint (name,
16329 cu_header->addr_size,
16330 TYPE_LENGTH (type));
16331 /* Symbols of this form are reasonably rare, so we just
16332 piggyback on the existing location code rather than writing
16333 a new implementation of symbol_computed_ops. */
16334 *baton = obstack_alloc (&objfile->objfile_obstack,
16335 sizeof (struct dwarf2_locexpr_baton));
16336 (*baton)->per_cu = cu->per_cu;
16337 gdb_assert ((*baton)->per_cu);
16338
16339 (*baton)->size = 2 + cu_header->addr_size;
16340 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
16341 (*baton)->data = data;
16342
16343 data[0] = DW_OP_addr;
16344 store_unsigned_integer (&data[1], cu_header->addr_size,
16345 byte_order, DW_ADDR (attr));
16346 data[cu_header->addr_size + 1] = DW_OP_stack_value;
16347 }
16348 break;
16349 case DW_FORM_string:
16350 case DW_FORM_strp:
16351 case DW_FORM_GNU_str_index:
16352 case DW_FORM_GNU_strp_alt:
16353 /* DW_STRING is already allocated on the objfile obstack, point
16354 directly to it. */
16355 *bytes = (gdb_byte *) DW_STRING (attr);
16356 break;
16357 case DW_FORM_block1:
16358 case DW_FORM_block2:
16359 case DW_FORM_block4:
16360 case DW_FORM_block:
16361 case DW_FORM_exprloc:
16362 blk = DW_BLOCK (attr);
16363 if (TYPE_LENGTH (type) != blk->size)
16364 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
16365 TYPE_LENGTH (type));
16366 *bytes = blk->data;
16367 break;
16368
16369 /* The DW_AT_const_value attributes are supposed to carry the
16370 symbol's value "represented as it would be on the target
16371 architecture." By the time we get here, it's already been
16372 converted to host endianness, so we just need to sign- or
16373 zero-extend it as appropriate. */
16374 case DW_FORM_data1:
16375 *bytes = dwarf2_const_value_data (attr, type, name,
16376 obstack, cu, value, 8);
16377 break;
16378 case DW_FORM_data2:
16379 *bytes = dwarf2_const_value_data (attr, type, name,
16380 obstack, cu, value, 16);
16381 break;
16382 case DW_FORM_data4:
16383 *bytes = dwarf2_const_value_data (attr, type, name,
16384 obstack, cu, value, 32);
16385 break;
16386 case DW_FORM_data8:
16387 *bytes = dwarf2_const_value_data (attr, type, name,
16388 obstack, cu, value, 64);
16389 break;
16390
16391 case DW_FORM_sdata:
16392 *value = DW_SND (attr);
16393 break;
16394
16395 case DW_FORM_udata:
16396 *value = DW_UNSND (attr);
16397 break;
16398
16399 default:
16400 complaint (&symfile_complaints,
16401 _("unsupported const value attribute form: '%s'"),
16402 dwarf_form_name (attr->form));
16403 *value = 0;
16404 break;
16405 }
16406 }
16407
16408
16409 /* Copy constant value from an attribute to a symbol. */
16410
16411 static void
16412 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
16413 struct dwarf2_cu *cu)
16414 {
16415 struct objfile *objfile = cu->objfile;
16416 struct comp_unit_head *cu_header = &cu->header;
16417 LONGEST value;
16418 gdb_byte *bytes;
16419 struct dwarf2_locexpr_baton *baton;
16420
16421 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
16422 SYMBOL_PRINT_NAME (sym),
16423 &objfile->objfile_obstack, cu,
16424 &value, &bytes, &baton);
16425
16426 if (baton != NULL)
16427 {
16428 SYMBOL_LOCATION_BATON (sym) = baton;
16429 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16430 }
16431 else if (bytes != NULL)
16432 {
16433 SYMBOL_VALUE_BYTES (sym) = bytes;
16434 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
16435 }
16436 else
16437 {
16438 SYMBOL_VALUE (sym) = value;
16439 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
16440 }
16441 }
16442
16443 /* Return the type of the die in question using its DW_AT_type attribute. */
16444
16445 static struct type *
16446 die_type (struct die_info *die, struct dwarf2_cu *cu)
16447 {
16448 struct attribute *type_attr;
16449
16450 type_attr = dwarf2_attr (die, DW_AT_type, cu);
16451 if (!type_attr)
16452 {
16453 /* A missing DW_AT_type represents a void type. */
16454 return objfile_type (cu->objfile)->builtin_void;
16455 }
16456
16457 return lookup_die_type (die, type_attr, cu);
16458 }
16459
16460 /* True iff CU's producer generates GNAT Ada auxiliary information
16461 that allows to find parallel types through that information instead
16462 of having to do expensive parallel lookups by type name. */
16463
16464 static int
16465 need_gnat_info (struct dwarf2_cu *cu)
16466 {
16467 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
16468 of GNAT produces this auxiliary information, without any indication
16469 that it is produced. Part of enhancing the FSF version of GNAT
16470 to produce that information will be to put in place an indicator
16471 that we can use in order to determine whether the descriptive type
16472 info is available or not. One suggestion that has been made is
16473 to use a new attribute, attached to the CU die. For now, assume
16474 that the descriptive type info is not available. */
16475 return 0;
16476 }
16477
16478 /* Return the auxiliary type of the die in question using its
16479 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
16480 attribute is not present. */
16481
16482 static struct type *
16483 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
16484 {
16485 struct attribute *type_attr;
16486
16487 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
16488 if (!type_attr)
16489 return NULL;
16490
16491 return lookup_die_type (die, type_attr, cu);
16492 }
16493
16494 /* If DIE has a descriptive_type attribute, then set the TYPE's
16495 descriptive type accordingly. */
16496
16497 static void
16498 set_descriptive_type (struct type *type, struct die_info *die,
16499 struct dwarf2_cu *cu)
16500 {
16501 struct type *descriptive_type = die_descriptive_type (die, cu);
16502
16503 if (descriptive_type)
16504 {
16505 ALLOCATE_GNAT_AUX_TYPE (type);
16506 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
16507 }
16508 }
16509
16510 /* Return the containing type of the die in question using its
16511 DW_AT_containing_type attribute. */
16512
16513 static struct type *
16514 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
16515 {
16516 struct attribute *type_attr;
16517
16518 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
16519 if (!type_attr)
16520 error (_("Dwarf Error: Problem turning containing type into gdb type "
16521 "[in module %s]"), cu->objfile->name);
16522
16523 return lookup_die_type (die, type_attr, cu);
16524 }
16525
16526 /* Look up the type of DIE in CU using its type attribute ATTR.
16527 If there is no type substitute an error marker. */
16528
16529 static struct type *
16530 lookup_die_type (struct die_info *die, struct attribute *attr,
16531 struct dwarf2_cu *cu)
16532 {
16533 struct objfile *objfile = cu->objfile;
16534 struct type *this_type;
16535
16536 /* First see if we have it cached. */
16537
16538 if (attr->form == DW_FORM_GNU_ref_alt)
16539 {
16540 struct dwarf2_per_cu_data *per_cu;
16541 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16542
16543 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
16544 this_type = get_die_type_at_offset (offset, per_cu);
16545 }
16546 else if (is_ref_attr (attr))
16547 {
16548 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16549
16550 this_type = get_die_type_at_offset (offset, cu->per_cu);
16551 }
16552 else if (attr->form == DW_FORM_ref_sig8)
16553 {
16554 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
16555
16556 /* sig_type will be NULL if the signatured type is missing from
16557 the debug info. */
16558 if (sig_type == NULL)
16559 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
16560 "at 0x%x [in module %s]"),
16561 die->offset.sect_off, objfile->name);
16562
16563 gdb_assert (sig_type->per_cu.is_debug_types);
16564 /* If we haven't filled in type_offset_in_section yet, then we
16565 haven't read the type in yet. */
16566 this_type = NULL;
16567 if (sig_type->type_offset_in_section.sect_off != 0)
16568 {
16569 this_type =
16570 get_die_type_at_offset (sig_type->type_offset_in_section,
16571 &sig_type->per_cu);
16572 }
16573 }
16574 else
16575 {
16576 dump_die_for_error (die);
16577 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
16578 dwarf_attr_name (attr->name), objfile->name);
16579 }
16580
16581 /* If not cached we need to read it in. */
16582
16583 if (this_type == NULL)
16584 {
16585 struct die_info *type_die;
16586 struct dwarf2_cu *type_cu = cu;
16587
16588 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
16589 /* If we found the type now, it's probably because the type came
16590 from an inter-CU reference and the type's CU got expanded before
16591 ours. */
16592 this_type = get_die_type (type_die, type_cu);
16593 if (this_type == NULL)
16594 this_type = read_type_die_1 (type_die, type_cu);
16595 }
16596
16597 /* If we still don't have a type use an error marker. */
16598
16599 if (this_type == NULL)
16600 {
16601 char *message, *saved;
16602
16603 /* read_type_die already issued a complaint. */
16604 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
16605 objfile->name,
16606 cu->header.offset.sect_off,
16607 die->offset.sect_off);
16608 saved = obstack_copy0 (&objfile->objfile_obstack,
16609 message, strlen (message));
16610 xfree (message);
16611
16612 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
16613 }
16614
16615 return this_type;
16616 }
16617
16618 /* Return the type in DIE, CU.
16619 Returns NULL for invalid types.
16620
16621 This first does a lookup in the appropriate type_hash table,
16622 and only reads the die in if necessary.
16623
16624 NOTE: This can be called when reading in partial or full symbols. */
16625
16626 static struct type *
16627 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
16628 {
16629 struct type *this_type;
16630
16631 this_type = get_die_type (die, cu);
16632 if (this_type)
16633 return this_type;
16634
16635 return read_type_die_1 (die, cu);
16636 }
16637
16638 /* Read the type in DIE, CU.
16639 Returns NULL for invalid types. */
16640
16641 static struct type *
16642 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
16643 {
16644 struct type *this_type = NULL;
16645
16646 switch (die->tag)
16647 {
16648 case DW_TAG_class_type:
16649 case DW_TAG_interface_type:
16650 case DW_TAG_structure_type:
16651 case DW_TAG_union_type:
16652 this_type = read_structure_type (die, cu);
16653 break;
16654 case DW_TAG_enumeration_type:
16655 this_type = read_enumeration_type (die, cu);
16656 break;
16657 case DW_TAG_subprogram:
16658 case DW_TAG_subroutine_type:
16659 case DW_TAG_inlined_subroutine:
16660 this_type = read_subroutine_type (die, cu);
16661 break;
16662 case DW_TAG_array_type:
16663 this_type = read_array_type (die, cu);
16664 break;
16665 case DW_TAG_set_type:
16666 this_type = read_set_type (die, cu);
16667 break;
16668 case DW_TAG_pointer_type:
16669 this_type = read_tag_pointer_type (die, cu);
16670 break;
16671 case DW_TAG_ptr_to_member_type:
16672 this_type = read_tag_ptr_to_member_type (die, cu);
16673 break;
16674 case DW_TAG_reference_type:
16675 this_type = read_tag_reference_type (die, cu);
16676 break;
16677 case DW_TAG_const_type:
16678 this_type = read_tag_const_type (die, cu);
16679 break;
16680 case DW_TAG_volatile_type:
16681 this_type = read_tag_volatile_type (die, cu);
16682 break;
16683 case DW_TAG_restrict_type:
16684 this_type = read_tag_restrict_type (die, cu);
16685 break;
16686 case DW_TAG_string_type:
16687 this_type = read_tag_string_type (die, cu);
16688 break;
16689 case DW_TAG_typedef:
16690 this_type = read_typedef (die, cu);
16691 break;
16692 case DW_TAG_subrange_type:
16693 this_type = read_subrange_type (die, cu);
16694 break;
16695 case DW_TAG_base_type:
16696 this_type = read_base_type (die, cu);
16697 break;
16698 case DW_TAG_unspecified_type:
16699 this_type = read_unspecified_type (die, cu);
16700 break;
16701 case DW_TAG_namespace:
16702 this_type = read_namespace_type (die, cu);
16703 break;
16704 case DW_TAG_module:
16705 this_type = read_module_type (die, cu);
16706 break;
16707 default:
16708 complaint (&symfile_complaints,
16709 _("unexpected tag in read_type_die: '%s'"),
16710 dwarf_tag_name (die->tag));
16711 break;
16712 }
16713
16714 return this_type;
16715 }
16716
16717 /* See if we can figure out if the class lives in a namespace. We do
16718 this by looking for a member function; its demangled name will
16719 contain namespace info, if there is any.
16720 Return the computed name or NULL.
16721 Space for the result is allocated on the objfile's obstack.
16722 This is the full-die version of guess_partial_die_structure_name.
16723 In this case we know DIE has no useful parent. */
16724
16725 static char *
16726 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
16727 {
16728 struct die_info *spec_die;
16729 struct dwarf2_cu *spec_cu;
16730 struct die_info *child;
16731
16732 spec_cu = cu;
16733 spec_die = die_specification (die, &spec_cu);
16734 if (spec_die != NULL)
16735 {
16736 die = spec_die;
16737 cu = spec_cu;
16738 }
16739
16740 for (child = die->child;
16741 child != NULL;
16742 child = child->sibling)
16743 {
16744 if (child->tag == DW_TAG_subprogram)
16745 {
16746 struct attribute *attr;
16747
16748 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
16749 if (attr == NULL)
16750 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
16751 if (attr != NULL)
16752 {
16753 char *actual_name
16754 = language_class_name_from_physname (cu->language_defn,
16755 DW_STRING (attr));
16756 char *name = NULL;
16757
16758 if (actual_name != NULL)
16759 {
16760 const char *die_name = dwarf2_name (die, cu);
16761
16762 if (die_name != NULL
16763 && strcmp (die_name, actual_name) != 0)
16764 {
16765 /* Strip off the class name from the full name.
16766 We want the prefix. */
16767 int die_name_len = strlen (die_name);
16768 int actual_name_len = strlen (actual_name);
16769
16770 /* Test for '::' as a sanity check. */
16771 if (actual_name_len > die_name_len + 2
16772 && actual_name[actual_name_len
16773 - die_name_len - 1] == ':')
16774 name =
16775 obstack_copy0 (&cu->objfile->objfile_obstack,
16776 actual_name,
16777 actual_name_len - die_name_len - 2);
16778 }
16779 }
16780 xfree (actual_name);
16781 return name;
16782 }
16783 }
16784 }
16785
16786 return NULL;
16787 }
16788
16789 /* GCC might emit a nameless typedef that has a linkage name. Determine the
16790 prefix part in such case. See
16791 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16792
16793 static char *
16794 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
16795 {
16796 struct attribute *attr;
16797 char *base;
16798
16799 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
16800 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
16801 return NULL;
16802
16803 attr = dwarf2_attr (die, DW_AT_name, cu);
16804 if (attr != NULL && DW_STRING (attr) != NULL)
16805 return NULL;
16806
16807 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16808 if (attr == NULL)
16809 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16810 if (attr == NULL || DW_STRING (attr) == NULL)
16811 return NULL;
16812
16813 /* dwarf2_name had to be already called. */
16814 gdb_assert (DW_STRING_IS_CANONICAL (attr));
16815
16816 /* Strip the base name, keep any leading namespaces/classes. */
16817 base = strrchr (DW_STRING (attr), ':');
16818 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
16819 return "";
16820
16821 return obstack_copy0 (&cu->objfile->objfile_obstack,
16822 DW_STRING (attr), &base[-1] - DW_STRING (attr));
16823 }
16824
16825 /* Return the name of the namespace/class that DIE is defined within,
16826 or "" if we can't tell. The caller should not xfree the result.
16827
16828 For example, if we're within the method foo() in the following
16829 code:
16830
16831 namespace N {
16832 class C {
16833 void foo () {
16834 }
16835 };
16836 }
16837
16838 then determine_prefix on foo's die will return "N::C". */
16839
16840 static const char *
16841 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
16842 {
16843 struct die_info *parent, *spec_die;
16844 struct dwarf2_cu *spec_cu;
16845 struct type *parent_type;
16846 char *retval;
16847
16848 if (cu->language != language_cplus && cu->language != language_java
16849 && cu->language != language_fortran)
16850 return "";
16851
16852 retval = anonymous_struct_prefix (die, cu);
16853 if (retval)
16854 return retval;
16855
16856 /* We have to be careful in the presence of DW_AT_specification.
16857 For example, with GCC 3.4, given the code
16858
16859 namespace N {
16860 void foo() {
16861 // Definition of N::foo.
16862 }
16863 }
16864
16865 then we'll have a tree of DIEs like this:
16866
16867 1: DW_TAG_compile_unit
16868 2: DW_TAG_namespace // N
16869 3: DW_TAG_subprogram // declaration of N::foo
16870 4: DW_TAG_subprogram // definition of N::foo
16871 DW_AT_specification // refers to die #3
16872
16873 Thus, when processing die #4, we have to pretend that we're in
16874 the context of its DW_AT_specification, namely the contex of die
16875 #3. */
16876 spec_cu = cu;
16877 spec_die = die_specification (die, &spec_cu);
16878 if (spec_die == NULL)
16879 parent = die->parent;
16880 else
16881 {
16882 parent = spec_die->parent;
16883 cu = spec_cu;
16884 }
16885
16886 if (parent == NULL)
16887 return "";
16888 else if (parent->building_fullname)
16889 {
16890 const char *name;
16891 const char *parent_name;
16892
16893 /* It has been seen on RealView 2.2 built binaries,
16894 DW_TAG_template_type_param types actually _defined_ as
16895 children of the parent class:
16896
16897 enum E {};
16898 template class <class Enum> Class{};
16899 Class<enum E> class_e;
16900
16901 1: DW_TAG_class_type (Class)
16902 2: DW_TAG_enumeration_type (E)
16903 3: DW_TAG_enumerator (enum1:0)
16904 3: DW_TAG_enumerator (enum2:1)
16905 ...
16906 2: DW_TAG_template_type_param
16907 DW_AT_type DW_FORM_ref_udata (E)
16908
16909 Besides being broken debug info, it can put GDB into an
16910 infinite loop. Consider:
16911
16912 When we're building the full name for Class<E>, we'll start
16913 at Class, and go look over its template type parameters,
16914 finding E. We'll then try to build the full name of E, and
16915 reach here. We're now trying to build the full name of E,
16916 and look over the parent DIE for containing scope. In the
16917 broken case, if we followed the parent DIE of E, we'd again
16918 find Class, and once again go look at its template type
16919 arguments, etc., etc. Simply don't consider such parent die
16920 as source-level parent of this die (it can't be, the language
16921 doesn't allow it), and break the loop here. */
16922 name = dwarf2_name (die, cu);
16923 parent_name = dwarf2_name (parent, cu);
16924 complaint (&symfile_complaints,
16925 _("template param type '%s' defined within parent '%s'"),
16926 name ? name : "<unknown>",
16927 parent_name ? parent_name : "<unknown>");
16928 return "";
16929 }
16930 else
16931 switch (parent->tag)
16932 {
16933 case DW_TAG_namespace:
16934 parent_type = read_type_die (parent, cu);
16935 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
16936 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
16937 Work around this problem here. */
16938 if (cu->language == language_cplus
16939 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
16940 return "";
16941 /* We give a name to even anonymous namespaces. */
16942 return TYPE_TAG_NAME (parent_type);
16943 case DW_TAG_class_type:
16944 case DW_TAG_interface_type:
16945 case DW_TAG_structure_type:
16946 case DW_TAG_union_type:
16947 case DW_TAG_module:
16948 parent_type = read_type_die (parent, cu);
16949 if (TYPE_TAG_NAME (parent_type) != NULL)
16950 return TYPE_TAG_NAME (parent_type);
16951 else
16952 /* An anonymous structure is only allowed non-static data
16953 members; no typedefs, no member functions, et cetera.
16954 So it does not need a prefix. */
16955 return "";
16956 case DW_TAG_compile_unit:
16957 case DW_TAG_partial_unit:
16958 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
16959 if (cu->language == language_cplus
16960 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
16961 && die->child != NULL
16962 && (die->tag == DW_TAG_class_type
16963 || die->tag == DW_TAG_structure_type
16964 || die->tag == DW_TAG_union_type))
16965 {
16966 char *name = guess_full_die_structure_name (die, cu);
16967 if (name != NULL)
16968 return name;
16969 }
16970 return "";
16971 default:
16972 return determine_prefix (parent, cu);
16973 }
16974 }
16975
16976 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
16977 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
16978 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
16979 an obconcat, otherwise allocate storage for the result. The CU argument is
16980 used to determine the language and hence, the appropriate separator. */
16981
16982 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
16983
16984 static char *
16985 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
16986 int physname, struct dwarf2_cu *cu)
16987 {
16988 const char *lead = "";
16989 const char *sep;
16990
16991 if (suffix == NULL || suffix[0] == '\0'
16992 || prefix == NULL || prefix[0] == '\0')
16993 sep = "";
16994 else if (cu->language == language_java)
16995 sep = ".";
16996 else if (cu->language == language_fortran && physname)
16997 {
16998 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
16999 DW_AT_MIPS_linkage_name is preferred and used instead. */
17000
17001 lead = "__";
17002 sep = "_MOD_";
17003 }
17004 else
17005 sep = "::";
17006
17007 if (prefix == NULL)
17008 prefix = "";
17009 if (suffix == NULL)
17010 suffix = "";
17011
17012 if (obs == NULL)
17013 {
17014 char *retval
17015 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
17016
17017 strcpy (retval, lead);
17018 strcat (retval, prefix);
17019 strcat (retval, sep);
17020 strcat (retval, suffix);
17021 return retval;
17022 }
17023 else
17024 {
17025 /* We have an obstack. */
17026 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
17027 }
17028 }
17029
17030 /* Return sibling of die, NULL if no sibling. */
17031
17032 static struct die_info *
17033 sibling_die (struct die_info *die)
17034 {
17035 return die->sibling;
17036 }
17037
17038 /* Get name of a die, return NULL if not found. */
17039
17040 static const char *
17041 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
17042 struct obstack *obstack)
17043 {
17044 if (name && cu->language == language_cplus)
17045 {
17046 char *canon_name = cp_canonicalize_string (name);
17047
17048 if (canon_name != NULL)
17049 {
17050 if (strcmp (canon_name, name) != 0)
17051 name = obstack_copy0 (obstack, canon_name, strlen (canon_name));
17052 xfree (canon_name);
17053 }
17054 }
17055
17056 return name;
17057 }
17058
17059 /* Get name of a die, return NULL if not found. */
17060
17061 static const char *
17062 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
17063 {
17064 struct attribute *attr;
17065
17066 attr = dwarf2_attr (die, DW_AT_name, cu);
17067 if ((!attr || !DW_STRING (attr))
17068 && die->tag != DW_TAG_class_type
17069 && die->tag != DW_TAG_interface_type
17070 && die->tag != DW_TAG_structure_type
17071 && die->tag != DW_TAG_union_type)
17072 return NULL;
17073
17074 switch (die->tag)
17075 {
17076 case DW_TAG_compile_unit:
17077 case DW_TAG_partial_unit:
17078 /* Compilation units have a DW_AT_name that is a filename, not
17079 a source language identifier. */
17080 case DW_TAG_enumeration_type:
17081 case DW_TAG_enumerator:
17082 /* These tags always have simple identifiers already; no need
17083 to canonicalize them. */
17084 return DW_STRING (attr);
17085
17086 case DW_TAG_subprogram:
17087 /* Java constructors will all be named "<init>", so return
17088 the class name when we see this special case. */
17089 if (cu->language == language_java
17090 && DW_STRING (attr) != NULL
17091 && strcmp (DW_STRING (attr), "<init>") == 0)
17092 {
17093 struct dwarf2_cu *spec_cu = cu;
17094 struct die_info *spec_die;
17095
17096 /* GCJ will output '<init>' for Java constructor names.
17097 For this special case, return the name of the parent class. */
17098
17099 /* GCJ may output suprogram DIEs with AT_specification set.
17100 If so, use the name of the specified DIE. */
17101 spec_die = die_specification (die, &spec_cu);
17102 if (spec_die != NULL)
17103 return dwarf2_name (spec_die, spec_cu);
17104
17105 do
17106 {
17107 die = die->parent;
17108 if (die->tag == DW_TAG_class_type)
17109 return dwarf2_name (die, cu);
17110 }
17111 while (die->tag != DW_TAG_compile_unit
17112 && die->tag != DW_TAG_partial_unit);
17113 }
17114 break;
17115
17116 case DW_TAG_class_type:
17117 case DW_TAG_interface_type:
17118 case DW_TAG_structure_type:
17119 case DW_TAG_union_type:
17120 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
17121 structures or unions. These were of the form "._%d" in GCC 4.1,
17122 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
17123 and GCC 4.4. We work around this problem by ignoring these. */
17124 if (attr && DW_STRING (attr)
17125 && (strncmp (DW_STRING (attr), "._", 2) == 0
17126 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
17127 return NULL;
17128
17129 /* GCC might emit a nameless typedef that has a linkage name. See
17130 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17131 if (!attr || DW_STRING (attr) == NULL)
17132 {
17133 char *demangled = NULL;
17134
17135 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
17136 if (attr == NULL)
17137 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
17138
17139 if (attr == NULL || DW_STRING (attr) == NULL)
17140 return NULL;
17141
17142 /* Avoid demangling DW_STRING (attr) the second time on a second
17143 call for the same DIE. */
17144 if (!DW_STRING_IS_CANONICAL (attr))
17145 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
17146
17147 if (demangled)
17148 {
17149 char *base;
17150
17151 /* FIXME: we already did this for the partial symbol... */
17152 DW_STRING (attr) = obstack_copy0 (&cu->objfile->objfile_obstack,
17153 demangled, strlen (demangled));
17154 DW_STRING_IS_CANONICAL (attr) = 1;
17155 xfree (demangled);
17156
17157 /* Strip any leading namespaces/classes, keep only the base name.
17158 DW_AT_name for named DIEs does not contain the prefixes. */
17159 base = strrchr (DW_STRING (attr), ':');
17160 if (base && base > DW_STRING (attr) && base[-1] == ':')
17161 return &base[1];
17162 else
17163 return DW_STRING (attr);
17164 }
17165 }
17166 break;
17167
17168 default:
17169 break;
17170 }
17171
17172 if (!DW_STRING_IS_CANONICAL (attr))
17173 {
17174 DW_STRING (attr)
17175 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
17176 &cu->objfile->objfile_obstack);
17177 DW_STRING_IS_CANONICAL (attr) = 1;
17178 }
17179 return DW_STRING (attr);
17180 }
17181
17182 /* Return the die that this die in an extension of, or NULL if there
17183 is none. *EXT_CU is the CU containing DIE on input, and the CU
17184 containing the return value on output. */
17185
17186 static struct die_info *
17187 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
17188 {
17189 struct attribute *attr;
17190
17191 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
17192 if (attr == NULL)
17193 return NULL;
17194
17195 return follow_die_ref (die, attr, ext_cu);
17196 }
17197
17198 /* Convert a DIE tag into its string name. */
17199
17200 static const char *
17201 dwarf_tag_name (unsigned tag)
17202 {
17203 const char *name = get_DW_TAG_name (tag);
17204
17205 if (name == NULL)
17206 return "DW_TAG_<unknown>";
17207
17208 return name;
17209 }
17210
17211 /* Convert a DWARF attribute code into its string name. */
17212
17213 static const char *
17214 dwarf_attr_name (unsigned attr)
17215 {
17216 const char *name;
17217
17218 #ifdef MIPS /* collides with DW_AT_HP_block_index */
17219 if (attr == DW_AT_MIPS_fde)
17220 return "DW_AT_MIPS_fde";
17221 #else
17222 if (attr == DW_AT_HP_block_index)
17223 return "DW_AT_HP_block_index";
17224 #endif
17225
17226 name = get_DW_AT_name (attr);
17227
17228 if (name == NULL)
17229 return "DW_AT_<unknown>";
17230
17231 return name;
17232 }
17233
17234 /* Convert a DWARF value form code into its string name. */
17235
17236 static const char *
17237 dwarf_form_name (unsigned form)
17238 {
17239 const char *name = get_DW_FORM_name (form);
17240
17241 if (name == NULL)
17242 return "DW_FORM_<unknown>";
17243
17244 return name;
17245 }
17246
17247 static char *
17248 dwarf_bool_name (unsigned mybool)
17249 {
17250 if (mybool)
17251 return "TRUE";
17252 else
17253 return "FALSE";
17254 }
17255
17256 /* Convert a DWARF type code into its string name. */
17257
17258 static const char *
17259 dwarf_type_encoding_name (unsigned enc)
17260 {
17261 const char *name = get_DW_ATE_name (enc);
17262
17263 if (name == NULL)
17264 return "DW_ATE_<unknown>";
17265
17266 return name;
17267 }
17268
17269 static void
17270 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
17271 {
17272 unsigned int i;
17273
17274 print_spaces (indent, f);
17275 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
17276 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
17277
17278 if (die->parent != NULL)
17279 {
17280 print_spaces (indent, f);
17281 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
17282 die->parent->offset.sect_off);
17283 }
17284
17285 print_spaces (indent, f);
17286 fprintf_unfiltered (f, " has children: %s\n",
17287 dwarf_bool_name (die->child != NULL));
17288
17289 print_spaces (indent, f);
17290 fprintf_unfiltered (f, " attributes:\n");
17291
17292 for (i = 0; i < die->num_attrs; ++i)
17293 {
17294 print_spaces (indent, f);
17295 fprintf_unfiltered (f, " %s (%s) ",
17296 dwarf_attr_name (die->attrs[i].name),
17297 dwarf_form_name (die->attrs[i].form));
17298
17299 switch (die->attrs[i].form)
17300 {
17301 case DW_FORM_addr:
17302 case DW_FORM_GNU_addr_index:
17303 fprintf_unfiltered (f, "address: ");
17304 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
17305 break;
17306 case DW_FORM_block2:
17307 case DW_FORM_block4:
17308 case DW_FORM_block:
17309 case DW_FORM_block1:
17310 fprintf_unfiltered (f, "block: size %s",
17311 pulongest (DW_BLOCK (&die->attrs[i])->size));
17312 break;
17313 case DW_FORM_exprloc:
17314 fprintf_unfiltered (f, "expression: size %s",
17315 pulongest (DW_BLOCK (&die->attrs[i])->size));
17316 break;
17317 case DW_FORM_ref_addr:
17318 fprintf_unfiltered (f, "ref address: ");
17319 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17320 break;
17321 case DW_FORM_GNU_ref_alt:
17322 fprintf_unfiltered (f, "alt ref address: ");
17323 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17324 break;
17325 case DW_FORM_ref1:
17326 case DW_FORM_ref2:
17327 case DW_FORM_ref4:
17328 case DW_FORM_ref8:
17329 case DW_FORM_ref_udata:
17330 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
17331 (long) (DW_UNSND (&die->attrs[i])));
17332 break;
17333 case DW_FORM_data1:
17334 case DW_FORM_data2:
17335 case DW_FORM_data4:
17336 case DW_FORM_data8:
17337 case DW_FORM_udata:
17338 case DW_FORM_sdata:
17339 fprintf_unfiltered (f, "constant: %s",
17340 pulongest (DW_UNSND (&die->attrs[i])));
17341 break;
17342 case DW_FORM_sec_offset:
17343 fprintf_unfiltered (f, "section offset: %s",
17344 pulongest (DW_UNSND (&die->attrs[i])));
17345 break;
17346 case DW_FORM_ref_sig8:
17347 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
17348 {
17349 struct signatured_type *sig_type =
17350 DW_SIGNATURED_TYPE (&die->attrs[i]);
17351
17352 fprintf_unfiltered (f, "signatured type: 0x%s, offset 0x%x",
17353 hex_string (sig_type->signature),
17354 sig_type->per_cu.offset.sect_off);
17355 }
17356 else
17357 fprintf_unfiltered (f, "signatured type, unknown");
17358 break;
17359 case DW_FORM_string:
17360 case DW_FORM_strp:
17361 case DW_FORM_GNU_str_index:
17362 case DW_FORM_GNU_strp_alt:
17363 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
17364 DW_STRING (&die->attrs[i])
17365 ? DW_STRING (&die->attrs[i]) : "",
17366 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
17367 break;
17368 case DW_FORM_flag:
17369 if (DW_UNSND (&die->attrs[i]))
17370 fprintf_unfiltered (f, "flag: TRUE");
17371 else
17372 fprintf_unfiltered (f, "flag: FALSE");
17373 break;
17374 case DW_FORM_flag_present:
17375 fprintf_unfiltered (f, "flag: TRUE");
17376 break;
17377 case DW_FORM_indirect:
17378 /* The reader will have reduced the indirect form to
17379 the "base form" so this form should not occur. */
17380 fprintf_unfiltered (f,
17381 "unexpected attribute form: DW_FORM_indirect");
17382 break;
17383 default:
17384 fprintf_unfiltered (f, "unsupported attribute form: %d.",
17385 die->attrs[i].form);
17386 break;
17387 }
17388 fprintf_unfiltered (f, "\n");
17389 }
17390 }
17391
17392 static void
17393 dump_die_for_error (struct die_info *die)
17394 {
17395 dump_die_shallow (gdb_stderr, 0, die);
17396 }
17397
17398 static void
17399 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
17400 {
17401 int indent = level * 4;
17402
17403 gdb_assert (die != NULL);
17404
17405 if (level >= max_level)
17406 return;
17407
17408 dump_die_shallow (f, indent, die);
17409
17410 if (die->child != NULL)
17411 {
17412 print_spaces (indent, f);
17413 fprintf_unfiltered (f, " Children:");
17414 if (level + 1 < max_level)
17415 {
17416 fprintf_unfiltered (f, "\n");
17417 dump_die_1 (f, level + 1, max_level, die->child);
17418 }
17419 else
17420 {
17421 fprintf_unfiltered (f,
17422 " [not printed, max nesting level reached]\n");
17423 }
17424 }
17425
17426 if (die->sibling != NULL && level > 0)
17427 {
17428 dump_die_1 (f, level, max_level, die->sibling);
17429 }
17430 }
17431
17432 /* This is called from the pdie macro in gdbinit.in.
17433 It's not static so gcc will keep a copy callable from gdb. */
17434
17435 void
17436 dump_die (struct die_info *die, int max_level)
17437 {
17438 dump_die_1 (gdb_stdlog, 0, max_level, die);
17439 }
17440
17441 static void
17442 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
17443 {
17444 void **slot;
17445
17446 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
17447 INSERT);
17448
17449 *slot = die;
17450 }
17451
17452 /* DW_ADDR is always stored already as sect_offset; despite for the forms
17453 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
17454
17455 static int
17456 is_ref_attr (struct attribute *attr)
17457 {
17458 switch (attr->form)
17459 {
17460 case DW_FORM_ref_addr:
17461 case DW_FORM_ref1:
17462 case DW_FORM_ref2:
17463 case DW_FORM_ref4:
17464 case DW_FORM_ref8:
17465 case DW_FORM_ref_udata:
17466 case DW_FORM_GNU_ref_alt:
17467 return 1;
17468 default:
17469 return 0;
17470 }
17471 }
17472
17473 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
17474 required kind. */
17475
17476 static sect_offset
17477 dwarf2_get_ref_die_offset (struct attribute *attr)
17478 {
17479 sect_offset retval = { DW_UNSND (attr) };
17480
17481 if (is_ref_attr (attr))
17482 return retval;
17483
17484 retval.sect_off = 0;
17485 complaint (&symfile_complaints,
17486 _("unsupported die ref attribute form: '%s'"),
17487 dwarf_form_name (attr->form));
17488 return retval;
17489 }
17490
17491 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
17492 * the value held by the attribute is not constant. */
17493
17494 static LONGEST
17495 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
17496 {
17497 if (attr->form == DW_FORM_sdata)
17498 return DW_SND (attr);
17499 else if (attr->form == DW_FORM_udata
17500 || attr->form == DW_FORM_data1
17501 || attr->form == DW_FORM_data2
17502 || attr->form == DW_FORM_data4
17503 || attr->form == DW_FORM_data8)
17504 return DW_UNSND (attr);
17505 else
17506 {
17507 complaint (&symfile_complaints,
17508 _("Attribute value is not a constant (%s)"),
17509 dwarf_form_name (attr->form));
17510 return default_value;
17511 }
17512 }
17513
17514 /* Follow reference or signature attribute ATTR of SRC_DIE.
17515 On entry *REF_CU is the CU of SRC_DIE.
17516 On exit *REF_CU is the CU of the result. */
17517
17518 static struct die_info *
17519 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
17520 struct dwarf2_cu **ref_cu)
17521 {
17522 struct die_info *die;
17523
17524 if (is_ref_attr (attr))
17525 die = follow_die_ref (src_die, attr, ref_cu);
17526 else if (attr->form == DW_FORM_ref_sig8)
17527 die = follow_die_sig (src_die, attr, ref_cu);
17528 else
17529 {
17530 dump_die_for_error (src_die);
17531 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
17532 (*ref_cu)->objfile->name);
17533 }
17534
17535 return die;
17536 }
17537
17538 /* Follow reference OFFSET.
17539 On entry *REF_CU is the CU of the source die referencing OFFSET.
17540 On exit *REF_CU is the CU of the result.
17541 Returns NULL if OFFSET is invalid. */
17542
17543 static struct die_info *
17544 follow_die_offset (sect_offset offset, int offset_in_dwz,
17545 struct dwarf2_cu **ref_cu)
17546 {
17547 struct die_info temp_die;
17548 struct dwarf2_cu *target_cu, *cu = *ref_cu;
17549
17550 gdb_assert (cu->per_cu != NULL);
17551
17552 target_cu = cu;
17553
17554 if (cu->per_cu->is_debug_types)
17555 {
17556 /* .debug_types CUs cannot reference anything outside their CU.
17557 If they need to, they have to reference a signatured type via
17558 DW_FORM_ref_sig8. */
17559 if (! offset_in_cu_p (&cu->header, offset))
17560 return NULL;
17561 }
17562 else if (offset_in_dwz != cu->per_cu->is_dwz
17563 || ! offset_in_cu_p (&cu->header, offset))
17564 {
17565 struct dwarf2_per_cu_data *per_cu;
17566
17567 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
17568 cu->objfile);
17569
17570 /* If necessary, add it to the queue and load its DIEs. */
17571 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
17572 load_full_comp_unit (per_cu, cu->language);
17573
17574 target_cu = per_cu->cu;
17575 }
17576 else if (cu->dies == NULL)
17577 {
17578 /* We're loading full DIEs during partial symbol reading. */
17579 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
17580 load_full_comp_unit (cu->per_cu, language_minimal);
17581 }
17582
17583 *ref_cu = target_cu;
17584 temp_die.offset = offset;
17585 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
17586 }
17587
17588 /* Follow reference attribute ATTR of SRC_DIE.
17589 On entry *REF_CU is the CU of SRC_DIE.
17590 On exit *REF_CU is the CU of the result. */
17591
17592 static struct die_info *
17593 follow_die_ref (struct die_info *src_die, struct attribute *attr,
17594 struct dwarf2_cu **ref_cu)
17595 {
17596 sect_offset offset = dwarf2_get_ref_die_offset (attr);
17597 struct dwarf2_cu *cu = *ref_cu;
17598 struct die_info *die;
17599
17600 die = follow_die_offset (offset,
17601 (attr->form == DW_FORM_GNU_ref_alt
17602 || cu->per_cu->is_dwz),
17603 ref_cu);
17604 if (!die)
17605 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
17606 "at 0x%x [in module %s]"),
17607 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
17608
17609 return die;
17610 }
17611
17612 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
17613 Returned value is intended for DW_OP_call*. Returned
17614 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
17615
17616 struct dwarf2_locexpr_baton
17617 dwarf2_fetch_die_loc_sect_off (sect_offset offset,
17618 struct dwarf2_per_cu_data *per_cu,
17619 CORE_ADDR (*get_frame_pc) (void *baton),
17620 void *baton)
17621 {
17622 struct dwarf2_cu *cu;
17623 struct die_info *die;
17624 struct attribute *attr;
17625 struct dwarf2_locexpr_baton retval;
17626
17627 dw2_setup (per_cu->objfile);
17628
17629 if (per_cu->cu == NULL)
17630 load_cu (per_cu);
17631 cu = per_cu->cu;
17632
17633 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
17634 if (!die)
17635 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
17636 offset.sect_off, per_cu->objfile->name);
17637
17638 attr = dwarf2_attr (die, DW_AT_location, cu);
17639 if (!attr)
17640 {
17641 /* DWARF: "If there is no such attribute, then there is no effect.".
17642 DATA is ignored if SIZE is 0. */
17643
17644 retval.data = NULL;
17645 retval.size = 0;
17646 }
17647 else if (attr_form_is_section_offset (attr))
17648 {
17649 struct dwarf2_loclist_baton loclist_baton;
17650 CORE_ADDR pc = (*get_frame_pc) (baton);
17651 size_t size;
17652
17653 fill_in_loclist_baton (cu, &loclist_baton, attr);
17654
17655 retval.data = dwarf2_find_location_expression (&loclist_baton,
17656 &size, pc);
17657 retval.size = size;
17658 }
17659 else
17660 {
17661 if (!attr_form_is_block (attr))
17662 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
17663 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
17664 offset.sect_off, per_cu->objfile->name);
17665
17666 retval.data = DW_BLOCK (attr)->data;
17667 retval.size = DW_BLOCK (attr)->size;
17668 }
17669 retval.per_cu = cu->per_cu;
17670
17671 age_cached_comp_units ();
17672
17673 return retval;
17674 }
17675
17676 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
17677 offset. */
17678
17679 struct dwarf2_locexpr_baton
17680 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
17681 struct dwarf2_per_cu_data *per_cu,
17682 CORE_ADDR (*get_frame_pc) (void *baton),
17683 void *baton)
17684 {
17685 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
17686
17687 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
17688 }
17689
17690 /* Return the type of the DIE at DIE_OFFSET in the CU named by
17691 PER_CU. */
17692
17693 struct type *
17694 dwarf2_get_die_type (cu_offset die_offset,
17695 struct dwarf2_per_cu_data *per_cu)
17696 {
17697 sect_offset die_offset_sect;
17698
17699 dw2_setup (per_cu->objfile);
17700
17701 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
17702 return get_die_type_at_offset (die_offset_sect, per_cu);
17703 }
17704
17705 /* Follow the signature attribute ATTR in SRC_DIE.
17706 On entry *REF_CU is the CU of SRC_DIE.
17707 On exit *REF_CU is the CU of the result. */
17708
17709 static struct die_info *
17710 follow_die_sig (struct die_info *src_die, struct attribute *attr,
17711 struct dwarf2_cu **ref_cu)
17712 {
17713 struct objfile *objfile = (*ref_cu)->objfile;
17714 struct die_info temp_die;
17715 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
17716 struct dwarf2_cu *sig_cu;
17717 struct die_info *die;
17718
17719 /* sig_type will be NULL if the signatured type is missing from
17720 the debug info. */
17721 if (sig_type == NULL)
17722 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
17723 "at 0x%x [in module %s]"),
17724 src_die->offset.sect_off, objfile->name);
17725
17726 /* If necessary, add it to the queue and load its DIEs. */
17727
17728 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
17729 read_signatured_type (sig_type);
17730
17731 gdb_assert (sig_type->per_cu.cu != NULL);
17732
17733 sig_cu = sig_type->per_cu.cu;
17734 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
17735 temp_die.offset = sig_type->type_offset_in_section;
17736 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
17737 temp_die.offset.sect_off);
17738 if (die)
17739 {
17740 /* For .gdb_index version 7 keep track of included TUs.
17741 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
17742 if (dwarf2_per_objfile->index_table != NULL
17743 && dwarf2_per_objfile->index_table->version <= 7)
17744 {
17745 VEC_safe_push (dwarf2_per_cu_ptr,
17746 (*ref_cu)->per_cu->imported_symtabs,
17747 sig_cu->per_cu);
17748 }
17749
17750 *ref_cu = sig_cu;
17751 return die;
17752 }
17753
17754 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
17755 "from DIE at 0x%x [in module %s]"),
17756 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
17757 }
17758
17759 /* Load the DIEs associated with type unit PER_CU into memory. */
17760
17761 static void
17762 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
17763 {
17764 struct signatured_type *sig_type;
17765
17766 /* Caller is responsible for ensuring type_unit_groups don't get here. */
17767 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
17768
17769 /* We have the per_cu, but we need the signatured_type.
17770 Fortunately this is an easy translation. */
17771 gdb_assert (per_cu->is_debug_types);
17772 sig_type = (struct signatured_type *) per_cu;
17773
17774 gdb_assert (per_cu->cu == NULL);
17775
17776 read_signatured_type (sig_type);
17777
17778 gdb_assert (per_cu->cu != NULL);
17779 }
17780
17781 /* die_reader_func for read_signatured_type.
17782 This is identical to load_full_comp_unit_reader,
17783 but is kept separate for now. */
17784
17785 static void
17786 read_signatured_type_reader (const struct die_reader_specs *reader,
17787 gdb_byte *info_ptr,
17788 struct die_info *comp_unit_die,
17789 int has_children,
17790 void *data)
17791 {
17792 struct dwarf2_cu *cu = reader->cu;
17793
17794 gdb_assert (cu->die_hash == NULL);
17795 cu->die_hash =
17796 htab_create_alloc_ex (cu->header.length / 12,
17797 die_hash,
17798 die_eq,
17799 NULL,
17800 &cu->comp_unit_obstack,
17801 hashtab_obstack_allocate,
17802 dummy_obstack_deallocate);
17803
17804 if (has_children)
17805 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
17806 &info_ptr, comp_unit_die);
17807 cu->dies = comp_unit_die;
17808 /* comp_unit_die is not stored in die_hash, no need. */
17809
17810 /* We try not to read any attributes in this function, because not
17811 all CUs needed for references have been loaded yet, and symbol
17812 table processing isn't initialized. But we have to set the CU language,
17813 or we won't be able to build types correctly.
17814 Similarly, if we do not read the producer, we can not apply
17815 producer-specific interpretation. */
17816 prepare_one_comp_unit (cu, cu->dies, language_minimal);
17817 }
17818
17819 /* Read in a signatured type and build its CU and DIEs.
17820 If the type is a stub for the real type in a DWO file,
17821 read in the real type from the DWO file as well. */
17822
17823 static void
17824 read_signatured_type (struct signatured_type *sig_type)
17825 {
17826 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
17827
17828 gdb_assert (per_cu->is_debug_types);
17829 gdb_assert (per_cu->cu == NULL);
17830
17831 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
17832 read_signatured_type_reader, NULL);
17833 }
17834
17835 /* Decode simple location descriptions.
17836 Given a pointer to a dwarf block that defines a location, compute
17837 the location and return the value.
17838
17839 NOTE drow/2003-11-18: This function is called in two situations
17840 now: for the address of static or global variables (partial symbols
17841 only) and for offsets into structures which are expected to be
17842 (more or less) constant. The partial symbol case should go away,
17843 and only the constant case should remain. That will let this
17844 function complain more accurately. A few special modes are allowed
17845 without complaint for global variables (for instance, global
17846 register values and thread-local values).
17847
17848 A location description containing no operations indicates that the
17849 object is optimized out. The return value is 0 for that case.
17850 FIXME drow/2003-11-16: No callers check for this case any more; soon all
17851 callers will only want a very basic result and this can become a
17852 complaint.
17853
17854 Note that stack[0] is unused except as a default error return. */
17855
17856 static CORE_ADDR
17857 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
17858 {
17859 struct objfile *objfile = cu->objfile;
17860 size_t i;
17861 size_t size = blk->size;
17862 gdb_byte *data = blk->data;
17863 CORE_ADDR stack[64];
17864 int stacki;
17865 unsigned int bytes_read, unsnd;
17866 gdb_byte op;
17867
17868 i = 0;
17869 stacki = 0;
17870 stack[stacki] = 0;
17871 stack[++stacki] = 0;
17872
17873 while (i < size)
17874 {
17875 op = data[i++];
17876 switch (op)
17877 {
17878 case DW_OP_lit0:
17879 case DW_OP_lit1:
17880 case DW_OP_lit2:
17881 case DW_OP_lit3:
17882 case DW_OP_lit4:
17883 case DW_OP_lit5:
17884 case DW_OP_lit6:
17885 case DW_OP_lit7:
17886 case DW_OP_lit8:
17887 case DW_OP_lit9:
17888 case DW_OP_lit10:
17889 case DW_OP_lit11:
17890 case DW_OP_lit12:
17891 case DW_OP_lit13:
17892 case DW_OP_lit14:
17893 case DW_OP_lit15:
17894 case DW_OP_lit16:
17895 case DW_OP_lit17:
17896 case DW_OP_lit18:
17897 case DW_OP_lit19:
17898 case DW_OP_lit20:
17899 case DW_OP_lit21:
17900 case DW_OP_lit22:
17901 case DW_OP_lit23:
17902 case DW_OP_lit24:
17903 case DW_OP_lit25:
17904 case DW_OP_lit26:
17905 case DW_OP_lit27:
17906 case DW_OP_lit28:
17907 case DW_OP_lit29:
17908 case DW_OP_lit30:
17909 case DW_OP_lit31:
17910 stack[++stacki] = op - DW_OP_lit0;
17911 break;
17912
17913 case DW_OP_reg0:
17914 case DW_OP_reg1:
17915 case DW_OP_reg2:
17916 case DW_OP_reg3:
17917 case DW_OP_reg4:
17918 case DW_OP_reg5:
17919 case DW_OP_reg6:
17920 case DW_OP_reg7:
17921 case DW_OP_reg8:
17922 case DW_OP_reg9:
17923 case DW_OP_reg10:
17924 case DW_OP_reg11:
17925 case DW_OP_reg12:
17926 case DW_OP_reg13:
17927 case DW_OP_reg14:
17928 case DW_OP_reg15:
17929 case DW_OP_reg16:
17930 case DW_OP_reg17:
17931 case DW_OP_reg18:
17932 case DW_OP_reg19:
17933 case DW_OP_reg20:
17934 case DW_OP_reg21:
17935 case DW_OP_reg22:
17936 case DW_OP_reg23:
17937 case DW_OP_reg24:
17938 case DW_OP_reg25:
17939 case DW_OP_reg26:
17940 case DW_OP_reg27:
17941 case DW_OP_reg28:
17942 case DW_OP_reg29:
17943 case DW_OP_reg30:
17944 case DW_OP_reg31:
17945 stack[++stacki] = op - DW_OP_reg0;
17946 if (i < size)
17947 dwarf2_complex_location_expr_complaint ();
17948 break;
17949
17950 case DW_OP_regx:
17951 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
17952 i += bytes_read;
17953 stack[++stacki] = unsnd;
17954 if (i < size)
17955 dwarf2_complex_location_expr_complaint ();
17956 break;
17957
17958 case DW_OP_addr:
17959 stack[++stacki] = read_address (objfile->obfd, &data[i],
17960 cu, &bytes_read);
17961 i += bytes_read;
17962 break;
17963
17964 case DW_OP_const1u:
17965 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
17966 i += 1;
17967 break;
17968
17969 case DW_OP_const1s:
17970 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
17971 i += 1;
17972 break;
17973
17974 case DW_OP_const2u:
17975 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
17976 i += 2;
17977 break;
17978
17979 case DW_OP_const2s:
17980 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
17981 i += 2;
17982 break;
17983
17984 case DW_OP_const4u:
17985 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
17986 i += 4;
17987 break;
17988
17989 case DW_OP_const4s:
17990 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
17991 i += 4;
17992 break;
17993
17994 case DW_OP_const8u:
17995 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
17996 i += 8;
17997 break;
17998
17999 case DW_OP_constu:
18000 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
18001 &bytes_read);
18002 i += bytes_read;
18003 break;
18004
18005 case DW_OP_consts:
18006 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
18007 i += bytes_read;
18008 break;
18009
18010 case DW_OP_dup:
18011 stack[stacki + 1] = stack[stacki];
18012 stacki++;
18013 break;
18014
18015 case DW_OP_plus:
18016 stack[stacki - 1] += stack[stacki];
18017 stacki--;
18018 break;
18019
18020 case DW_OP_plus_uconst:
18021 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
18022 &bytes_read);
18023 i += bytes_read;
18024 break;
18025
18026 case DW_OP_minus:
18027 stack[stacki - 1] -= stack[stacki];
18028 stacki--;
18029 break;
18030
18031 case DW_OP_deref:
18032 /* If we're not the last op, then we definitely can't encode
18033 this using GDB's address_class enum. This is valid for partial
18034 global symbols, although the variable's address will be bogus
18035 in the psymtab. */
18036 if (i < size)
18037 dwarf2_complex_location_expr_complaint ();
18038 break;
18039
18040 case DW_OP_GNU_push_tls_address:
18041 /* The top of the stack has the offset from the beginning
18042 of the thread control block at which the variable is located. */
18043 /* Nothing should follow this operator, so the top of stack would
18044 be returned. */
18045 /* This is valid for partial global symbols, but the variable's
18046 address will be bogus in the psymtab. Make it always at least
18047 non-zero to not look as a variable garbage collected by linker
18048 which have DW_OP_addr 0. */
18049 if (i < size)
18050 dwarf2_complex_location_expr_complaint ();
18051 stack[stacki]++;
18052 break;
18053
18054 case DW_OP_GNU_uninit:
18055 break;
18056
18057 case DW_OP_GNU_addr_index:
18058 case DW_OP_GNU_const_index:
18059 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
18060 &bytes_read);
18061 i += bytes_read;
18062 break;
18063
18064 default:
18065 {
18066 const char *name = get_DW_OP_name (op);
18067
18068 if (name)
18069 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
18070 name);
18071 else
18072 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
18073 op);
18074 }
18075
18076 return (stack[stacki]);
18077 }
18078
18079 /* Enforce maximum stack depth of SIZE-1 to avoid writing
18080 outside of the allocated space. Also enforce minimum>0. */
18081 if (stacki >= ARRAY_SIZE (stack) - 1)
18082 {
18083 complaint (&symfile_complaints,
18084 _("location description stack overflow"));
18085 return 0;
18086 }
18087
18088 if (stacki <= 0)
18089 {
18090 complaint (&symfile_complaints,
18091 _("location description stack underflow"));
18092 return 0;
18093 }
18094 }
18095 return (stack[stacki]);
18096 }
18097
18098 /* memory allocation interface */
18099
18100 static struct dwarf_block *
18101 dwarf_alloc_block (struct dwarf2_cu *cu)
18102 {
18103 struct dwarf_block *blk;
18104
18105 blk = (struct dwarf_block *)
18106 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
18107 return (blk);
18108 }
18109
18110 static struct die_info *
18111 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
18112 {
18113 struct die_info *die;
18114 size_t size = sizeof (struct die_info);
18115
18116 if (num_attrs > 1)
18117 size += (num_attrs - 1) * sizeof (struct attribute);
18118
18119 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
18120 memset (die, 0, sizeof (struct die_info));
18121 return (die);
18122 }
18123
18124 \f
18125 /* Macro support. */
18126
18127 /* Return file name relative to the compilation directory of file number I in
18128 *LH's file name table. The result is allocated using xmalloc; the caller is
18129 responsible for freeing it. */
18130
18131 static char *
18132 file_file_name (int file, struct line_header *lh)
18133 {
18134 /* Is the file number a valid index into the line header's file name
18135 table? Remember that file numbers start with one, not zero. */
18136 if (1 <= file && file <= lh->num_file_names)
18137 {
18138 struct file_entry *fe = &lh->file_names[file - 1];
18139
18140 if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0)
18141 return xstrdup (fe->name);
18142 return concat (lh->include_dirs[fe->dir_index - 1], SLASH_STRING,
18143 fe->name, NULL);
18144 }
18145 else
18146 {
18147 /* The compiler produced a bogus file number. We can at least
18148 record the macro definitions made in the file, even if we
18149 won't be able to find the file by name. */
18150 char fake_name[80];
18151
18152 xsnprintf (fake_name, sizeof (fake_name),
18153 "<bad macro file number %d>", file);
18154
18155 complaint (&symfile_complaints,
18156 _("bad file number in macro information (%d)"),
18157 file);
18158
18159 return xstrdup (fake_name);
18160 }
18161 }
18162
18163 /* Return the full name of file number I in *LH's file name table.
18164 Use COMP_DIR as the name of the current directory of the
18165 compilation. The result is allocated using xmalloc; the caller is
18166 responsible for freeing it. */
18167 static char *
18168 file_full_name (int file, struct line_header *lh, const char *comp_dir)
18169 {
18170 /* Is the file number a valid index into the line header's file name
18171 table? Remember that file numbers start with one, not zero. */
18172 if (1 <= file && file <= lh->num_file_names)
18173 {
18174 char *relative = file_file_name (file, lh);
18175
18176 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
18177 return relative;
18178 return reconcat (relative, comp_dir, SLASH_STRING, relative, NULL);
18179 }
18180 else
18181 return file_file_name (file, lh);
18182 }
18183
18184
18185 static struct macro_source_file *
18186 macro_start_file (int file, int line,
18187 struct macro_source_file *current_file,
18188 const char *comp_dir,
18189 struct line_header *lh, struct objfile *objfile)
18190 {
18191 /* File name relative to the compilation directory of this source file. */
18192 char *file_name = file_file_name (file, lh);
18193
18194 /* We don't create a macro table for this compilation unit
18195 at all until we actually get a filename. */
18196 if (! pending_macros)
18197 pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
18198 objfile->per_bfd->macro_cache,
18199 comp_dir);
18200
18201 if (! current_file)
18202 {
18203 /* If we have no current file, then this must be the start_file
18204 directive for the compilation unit's main source file. */
18205 current_file = macro_set_main (pending_macros, file_name);
18206 macro_define_special (pending_macros);
18207 }
18208 else
18209 current_file = macro_include (current_file, line, file_name);
18210
18211 xfree (file_name);
18212
18213 return current_file;
18214 }
18215
18216
18217 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
18218 followed by a null byte. */
18219 static char *
18220 copy_string (const char *buf, int len)
18221 {
18222 char *s = xmalloc (len + 1);
18223
18224 memcpy (s, buf, len);
18225 s[len] = '\0';
18226 return s;
18227 }
18228
18229
18230 static const char *
18231 consume_improper_spaces (const char *p, const char *body)
18232 {
18233 if (*p == ' ')
18234 {
18235 complaint (&symfile_complaints,
18236 _("macro definition contains spaces "
18237 "in formal argument list:\n`%s'"),
18238 body);
18239
18240 while (*p == ' ')
18241 p++;
18242 }
18243
18244 return p;
18245 }
18246
18247
18248 static void
18249 parse_macro_definition (struct macro_source_file *file, int line,
18250 const char *body)
18251 {
18252 const char *p;
18253
18254 /* The body string takes one of two forms. For object-like macro
18255 definitions, it should be:
18256
18257 <macro name> " " <definition>
18258
18259 For function-like macro definitions, it should be:
18260
18261 <macro name> "() " <definition>
18262 or
18263 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
18264
18265 Spaces may appear only where explicitly indicated, and in the
18266 <definition>.
18267
18268 The Dwarf 2 spec says that an object-like macro's name is always
18269 followed by a space, but versions of GCC around March 2002 omit
18270 the space when the macro's definition is the empty string.
18271
18272 The Dwarf 2 spec says that there should be no spaces between the
18273 formal arguments in a function-like macro's formal argument list,
18274 but versions of GCC around March 2002 include spaces after the
18275 commas. */
18276
18277
18278 /* Find the extent of the macro name. The macro name is terminated
18279 by either a space or null character (for an object-like macro) or
18280 an opening paren (for a function-like macro). */
18281 for (p = body; *p; p++)
18282 if (*p == ' ' || *p == '(')
18283 break;
18284
18285 if (*p == ' ' || *p == '\0')
18286 {
18287 /* It's an object-like macro. */
18288 int name_len = p - body;
18289 char *name = copy_string (body, name_len);
18290 const char *replacement;
18291
18292 if (*p == ' ')
18293 replacement = body + name_len + 1;
18294 else
18295 {
18296 dwarf2_macro_malformed_definition_complaint (body);
18297 replacement = body + name_len;
18298 }
18299
18300 macro_define_object (file, line, name, replacement);
18301
18302 xfree (name);
18303 }
18304 else if (*p == '(')
18305 {
18306 /* It's a function-like macro. */
18307 char *name = copy_string (body, p - body);
18308 int argc = 0;
18309 int argv_size = 1;
18310 char **argv = xmalloc (argv_size * sizeof (*argv));
18311
18312 p++;
18313
18314 p = consume_improper_spaces (p, body);
18315
18316 /* Parse the formal argument list. */
18317 while (*p && *p != ')')
18318 {
18319 /* Find the extent of the current argument name. */
18320 const char *arg_start = p;
18321
18322 while (*p && *p != ',' && *p != ')' && *p != ' ')
18323 p++;
18324
18325 if (! *p || p == arg_start)
18326 dwarf2_macro_malformed_definition_complaint (body);
18327 else
18328 {
18329 /* Make sure argv has room for the new argument. */
18330 if (argc >= argv_size)
18331 {
18332 argv_size *= 2;
18333 argv = xrealloc (argv, argv_size * sizeof (*argv));
18334 }
18335
18336 argv[argc++] = copy_string (arg_start, p - arg_start);
18337 }
18338
18339 p = consume_improper_spaces (p, body);
18340
18341 /* Consume the comma, if present. */
18342 if (*p == ',')
18343 {
18344 p++;
18345
18346 p = consume_improper_spaces (p, body);
18347 }
18348 }
18349
18350 if (*p == ')')
18351 {
18352 p++;
18353
18354 if (*p == ' ')
18355 /* Perfectly formed definition, no complaints. */
18356 macro_define_function (file, line, name,
18357 argc, (const char **) argv,
18358 p + 1);
18359 else if (*p == '\0')
18360 {
18361 /* Complain, but do define it. */
18362 dwarf2_macro_malformed_definition_complaint (body);
18363 macro_define_function (file, line, name,
18364 argc, (const char **) argv,
18365 p);
18366 }
18367 else
18368 /* Just complain. */
18369 dwarf2_macro_malformed_definition_complaint (body);
18370 }
18371 else
18372 /* Just complain. */
18373 dwarf2_macro_malformed_definition_complaint (body);
18374
18375 xfree (name);
18376 {
18377 int i;
18378
18379 for (i = 0; i < argc; i++)
18380 xfree (argv[i]);
18381 }
18382 xfree (argv);
18383 }
18384 else
18385 dwarf2_macro_malformed_definition_complaint (body);
18386 }
18387
18388 /* Skip some bytes from BYTES according to the form given in FORM.
18389 Returns the new pointer. */
18390
18391 static gdb_byte *
18392 skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
18393 enum dwarf_form form,
18394 unsigned int offset_size,
18395 struct dwarf2_section_info *section)
18396 {
18397 unsigned int bytes_read;
18398
18399 switch (form)
18400 {
18401 case DW_FORM_data1:
18402 case DW_FORM_flag:
18403 ++bytes;
18404 break;
18405
18406 case DW_FORM_data2:
18407 bytes += 2;
18408 break;
18409
18410 case DW_FORM_data4:
18411 bytes += 4;
18412 break;
18413
18414 case DW_FORM_data8:
18415 bytes += 8;
18416 break;
18417
18418 case DW_FORM_string:
18419 read_direct_string (abfd, bytes, &bytes_read);
18420 bytes += bytes_read;
18421 break;
18422
18423 case DW_FORM_sec_offset:
18424 case DW_FORM_strp:
18425 case DW_FORM_GNU_strp_alt:
18426 bytes += offset_size;
18427 break;
18428
18429 case DW_FORM_block:
18430 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
18431 bytes += bytes_read;
18432 break;
18433
18434 case DW_FORM_block1:
18435 bytes += 1 + read_1_byte (abfd, bytes);
18436 break;
18437 case DW_FORM_block2:
18438 bytes += 2 + read_2_bytes (abfd, bytes);
18439 break;
18440 case DW_FORM_block4:
18441 bytes += 4 + read_4_bytes (abfd, bytes);
18442 break;
18443
18444 case DW_FORM_sdata:
18445 case DW_FORM_udata:
18446 case DW_FORM_GNU_addr_index:
18447 case DW_FORM_GNU_str_index:
18448 bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
18449 if (bytes == NULL)
18450 {
18451 dwarf2_section_buffer_overflow_complaint (section);
18452 return NULL;
18453 }
18454 break;
18455
18456 default:
18457 {
18458 complain:
18459 complaint (&symfile_complaints,
18460 _("invalid form 0x%x in `%s'"),
18461 form,
18462 section->asection->name);
18463 return NULL;
18464 }
18465 }
18466
18467 return bytes;
18468 }
18469
18470 /* A helper for dwarf_decode_macros that handles skipping an unknown
18471 opcode. Returns an updated pointer to the macro data buffer; or,
18472 on error, issues a complaint and returns NULL. */
18473
18474 static gdb_byte *
18475 skip_unknown_opcode (unsigned int opcode,
18476 gdb_byte **opcode_definitions,
18477 gdb_byte *mac_ptr, gdb_byte *mac_end,
18478 bfd *abfd,
18479 unsigned int offset_size,
18480 struct dwarf2_section_info *section)
18481 {
18482 unsigned int bytes_read, i;
18483 unsigned long arg;
18484 gdb_byte *defn;
18485
18486 if (opcode_definitions[opcode] == NULL)
18487 {
18488 complaint (&symfile_complaints,
18489 _("unrecognized DW_MACFINO opcode 0x%x"),
18490 opcode);
18491 return NULL;
18492 }
18493
18494 defn = opcode_definitions[opcode];
18495 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
18496 defn += bytes_read;
18497
18498 for (i = 0; i < arg; ++i)
18499 {
18500 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
18501 section);
18502 if (mac_ptr == NULL)
18503 {
18504 /* skip_form_bytes already issued the complaint. */
18505 return NULL;
18506 }
18507 }
18508
18509 return mac_ptr;
18510 }
18511
18512 /* A helper function which parses the header of a macro section.
18513 If the macro section is the extended (for now called "GNU") type,
18514 then this updates *OFFSET_SIZE. Returns a pointer to just after
18515 the header, or issues a complaint and returns NULL on error. */
18516
18517 static gdb_byte *
18518 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
18519 bfd *abfd,
18520 gdb_byte *mac_ptr,
18521 unsigned int *offset_size,
18522 int section_is_gnu)
18523 {
18524 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
18525
18526 if (section_is_gnu)
18527 {
18528 unsigned int version, flags;
18529
18530 version = read_2_bytes (abfd, mac_ptr);
18531 if (version != 4)
18532 {
18533 complaint (&symfile_complaints,
18534 _("unrecognized version `%d' in .debug_macro section"),
18535 version);
18536 return NULL;
18537 }
18538 mac_ptr += 2;
18539
18540 flags = read_1_byte (abfd, mac_ptr);
18541 ++mac_ptr;
18542 *offset_size = (flags & 1) ? 8 : 4;
18543
18544 if ((flags & 2) != 0)
18545 /* We don't need the line table offset. */
18546 mac_ptr += *offset_size;
18547
18548 /* Vendor opcode descriptions. */
18549 if ((flags & 4) != 0)
18550 {
18551 unsigned int i, count;
18552
18553 count = read_1_byte (abfd, mac_ptr);
18554 ++mac_ptr;
18555 for (i = 0; i < count; ++i)
18556 {
18557 unsigned int opcode, bytes_read;
18558 unsigned long arg;
18559
18560 opcode = read_1_byte (abfd, mac_ptr);
18561 ++mac_ptr;
18562 opcode_definitions[opcode] = mac_ptr;
18563 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18564 mac_ptr += bytes_read;
18565 mac_ptr += arg;
18566 }
18567 }
18568 }
18569
18570 return mac_ptr;
18571 }
18572
18573 /* A helper for dwarf_decode_macros that handles the GNU extensions,
18574 including DW_MACRO_GNU_transparent_include. */
18575
18576 static void
18577 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
18578 struct macro_source_file *current_file,
18579 struct line_header *lh, const char *comp_dir,
18580 struct dwarf2_section_info *section,
18581 int section_is_gnu, int section_is_dwz,
18582 unsigned int offset_size,
18583 struct objfile *objfile,
18584 htab_t include_hash)
18585 {
18586 enum dwarf_macro_record_type macinfo_type;
18587 int at_commandline;
18588 gdb_byte *opcode_definitions[256];
18589
18590 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18591 &offset_size, section_is_gnu);
18592 if (mac_ptr == NULL)
18593 {
18594 /* We already issued a complaint. */
18595 return;
18596 }
18597
18598 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
18599 GDB is still reading the definitions from command line. First
18600 DW_MACINFO_start_file will need to be ignored as it was already executed
18601 to create CURRENT_FILE for the main source holding also the command line
18602 definitions. On first met DW_MACINFO_start_file this flag is reset to
18603 normally execute all the remaining DW_MACINFO_start_file macinfos. */
18604
18605 at_commandline = 1;
18606
18607 do
18608 {
18609 /* Do we at least have room for a macinfo type byte? */
18610 if (mac_ptr >= mac_end)
18611 {
18612 dwarf2_section_buffer_overflow_complaint (section);
18613 break;
18614 }
18615
18616 macinfo_type = read_1_byte (abfd, mac_ptr);
18617 mac_ptr++;
18618
18619 /* Note that we rely on the fact that the corresponding GNU and
18620 DWARF constants are the same. */
18621 switch (macinfo_type)
18622 {
18623 /* A zero macinfo type indicates the end of the macro
18624 information. */
18625 case 0:
18626 break;
18627
18628 case DW_MACRO_GNU_define:
18629 case DW_MACRO_GNU_undef:
18630 case DW_MACRO_GNU_define_indirect:
18631 case DW_MACRO_GNU_undef_indirect:
18632 case DW_MACRO_GNU_define_indirect_alt:
18633 case DW_MACRO_GNU_undef_indirect_alt:
18634 {
18635 unsigned int bytes_read;
18636 int line;
18637 char *body;
18638 int is_define;
18639
18640 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18641 mac_ptr += bytes_read;
18642
18643 if (macinfo_type == DW_MACRO_GNU_define
18644 || macinfo_type == DW_MACRO_GNU_undef)
18645 {
18646 body = read_direct_string (abfd, mac_ptr, &bytes_read);
18647 mac_ptr += bytes_read;
18648 }
18649 else
18650 {
18651 LONGEST str_offset;
18652
18653 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
18654 mac_ptr += offset_size;
18655
18656 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
18657 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
18658 || section_is_dwz)
18659 {
18660 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18661
18662 body = read_indirect_string_from_dwz (dwz, str_offset);
18663 }
18664 else
18665 body = read_indirect_string_at_offset (abfd, str_offset);
18666 }
18667
18668 is_define = (macinfo_type == DW_MACRO_GNU_define
18669 || macinfo_type == DW_MACRO_GNU_define_indirect
18670 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
18671 if (! current_file)
18672 {
18673 /* DWARF violation as no main source is present. */
18674 complaint (&symfile_complaints,
18675 _("debug info with no main source gives macro %s "
18676 "on line %d: %s"),
18677 is_define ? _("definition") : _("undefinition"),
18678 line, body);
18679 break;
18680 }
18681 if ((line == 0 && !at_commandline)
18682 || (line != 0 && at_commandline))
18683 complaint (&symfile_complaints,
18684 _("debug info gives %s macro %s with %s line %d: %s"),
18685 at_commandline ? _("command-line") : _("in-file"),
18686 is_define ? _("definition") : _("undefinition"),
18687 line == 0 ? _("zero") : _("non-zero"), line, body);
18688
18689 if (is_define)
18690 parse_macro_definition (current_file, line, body);
18691 else
18692 {
18693 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
18694 || macinfo_type == DW_MACRO_GNU_undef_indirect
18695 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
18696 macro_undef (current_file, line, body);
18697 }
18698 }
18699 break;
18700
18701 case DW_MACRO_GNU_start_file:
18702 {
18703 unsigned int bytes_read;
18704 int line, file;
18705
18706 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18707 mac_ptr += bytes_read;
18708 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18709 mac_ptr += bytes_read;
18710
18711 if ((line == 0 && !at_commandline)
18712 || (line != 0 && at_commandline))
18713 complaint (&symfile_complaints,
18714 _("debug info gives source %d included "
18715 "from %s at %s line %d"),
18716 file, at_commandline ? _("command-line") : _("file"),
18717 line == 0 ? _("zero") : _("non-zero"), line);
18718
18719 if (at_commandline)
18720 {
18721 /* This DW_MACRO_GNU_start_file was executed in the
18722 pass one. */
18723 at_commandline = 0;
18724 }
18725 else
18726 current_file = macro_start_file (file, line,
18727 current_file, comp_dir,
18728 lh, objfile);
18729 }
18730 break;
18731
18732 case DW_MACRO_GNU_end_file:
18733 if (! current_file)
18734 complaint (&symfile_complaints,
18735 _("macro debug info has an unmatched "
18736 "`close_file' directive"));
18737 else
18738 {
18739 current_file = current_file->included_by;
18740 if (! current_file)
18741 {
18742 enum dwarf_macro_record_type next_type;
18743
18744 /* GCC circa March 2002 doesn't produce the zero
18745 type byte marking the end of the compilation
18746 unit. Complain if it's not there, but exit no
18747 matter what. */
18748
18749 /* Do we at least have room for a macinfo type byte? */
18750 if (mac_ptr >= mac_end)
18751 {
18752 dwarf2_section_buffer_overflow_complaint (section);
18753 return;
18754 }
18755
18756 /* We don't increment mac_ptr here, so this is just
18757 a look-ahead. */
18758 next_type = read_1_byte (abfd, mac_ptr);
18759 if (next_type != 0)
18760 complaint (&symfile_complaints,
18761 _("no terminating 0-type entry for "
18762 "macros in `.debug_macinfo' section"));
18763
18764 return;
18765 }
18766 }
18767 break;
18768
18769 case DW_MACRO_GNU_transparent_include:
18770 case DW_MACRO_GNU_transparent_include_alt:
18771 {
18772 LONGEST offset;
18773 void **slot;
18774 bfd *include_bfd = abfd;
18775 struct dwarf2_section_info *include_section = section;
18776 struct dwarf2_section_info alt_section;
18777 gdb_byte *include_mac_end = mac_end;
18778 int is_dwz = section_is_dwz;
18779 gdb_byte *new_mac_ptr;
18780
18781 offset = read_offset_1 (abfd, mac_ptr, offset_size);
18782 mac_ptr += offset_size;
18783
18784 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
18785 {
18786 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18787
18788 dwarf2_read_section (dwarf2_per_objfile->objfile,
18789 &dwz->macro);
18790
18791 include_bfd = dwz->macro.asection->owner;
18792 include_section = &dwz->macro;
18793 include_mac_end = dwz->macro.buffer + dwz->macro.size;
18794 is_dwz = 1;
18795 }
18796
18797 new_mac_ptr = include_section->buffer + offset;
18798 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
18799
18800 if (*slot != NULL)
18801 {
18802 /* This has actually happened; see
18803 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
18804 complaint (&symfile_complaints,
18805 _("recursive DW_MACRO_GNU_transparent_include in "
18806 ".debug_macro section"));
18807 }
18808 else
18809 {
18810 *slot = new_mac_ptr;
18811
18812 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
18813 include_mac_end, current_file,
18814 lh, comp_dir,
18815 section, section_is_gnu, is_dwz,
18816 offset_size, objfile, include_hash);
18817
18818 htab_remove_elt (include_hash, new_mac_ptr);
18819 }
18820 }
18821 break;
18822
18823 case DW_MACINFO_vendor_ext:
18824 if (!section_is_gnu)
18825 {
18826 unsigned int bytes_read;
18827 int constant;
18828
18829 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18830 mac_ptr += bytes_read;
18831 read_direct_string (abfd, mac_ptr, &bytes_read);
18832 mac_ptr += bytes_read;
18833
18834 /* We don't recognize any vendor extensions. */
18835 break;
18836 }
18837 /* FALLTHROUGH */
18838
18839 default:
18840 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
18841 mac_ptr, mac_end, abfd, offset_size,
18842 section);
18843 if (mac_ptr == NULL)
18844 return;
18845 break;
18846 }
18847 } while (macinfo_type != 0);
18848 }
18849
18850 static void
18851 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
18852 const char *comp_dir, int section_is_gnu)
18853 {
18854 struct objfile *objfile = dwarf2_per_objfile->objfile;
18855 struct line_header *lh = cu->line_header;
18856 bfd *abfd;
18857 gdb_byte *mac_ptr, *mac_end;
18858 struct macro_source_file *current_file = 0;
18859 enum dwarf_macro_record_type macinfo_type;
18860 unsigned int offset_size = cu->header.offset_size;
18861 gdb_byte *opcode_definitions[256];
18862 struct cleanup *cleanup;
18863 htab_t include_hash;
18864 void **slot;
18865 struct dwarf2_section_info *section;
18866 const char *section_name;
18867
18868 if (cu->dwo_unit != NULL)
18869 {
18870 if (section_is_gnu)
18871 {
18872 section = &cu->dwo_unit->dwo_file->sections.macro;
18873 section_name = ".debug_macro.dwo";
18874 }
18875 else
18876 {
18877 section = &cu->dwo_unit->dwo_file->sections.macinfo;
18878 section_name = ".debug_macinfo.dwo";
18879 }
18880 }
18881 else
18882 {
18883 if (section_is_gnu)
18884 {
18885 section = &dwarf2_per_objfile->macro;
18886 section_name = ".debug_macro";
18887 }
18888 else
18889 {
18890 section = &dwarf2_per_objfile->macinfo;
18891 section_name = ".debug_macinfo";
18892 }
18893 }
18894
18895 dwarf2_read_section (objfile, section);
18896 if (section->buffer == NULL)
18897 {
18898 complaint (&symfile_complaints, _("missing %s section"), section_name);
18899 return;
18900 }
18901 abfd = section->asection->owner;
18902
18903 /* First pass: Find the name of the base filename.
18904 This filename is needed in order to process all macros whose definition
18905 (or undefinition) comes from the command line. These macros are defined
18906 before the first DW_MACINFO_start_file entry, and yet still need to be
18907 associated to the base file.
18908
18909 To determine the base file name, we scan the macro definitions until we
18910 reach the first DW_MACINFO_start_file entry. We then initialize
18911 CURRENT_FILE accordingly so that any macro definition found before the
18912 first DW_MACINFO_start_file can still be associated to the base file. */
18913
18914 mac_ptr = section->buffer + offset;
18915 mac_end = section->buffer + section->size;
18916
18917 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18918 &offset_size, section_is_gnu);
18919 if (mac_ptr == NULL)
18920 {
18921 /* We already issued a complaint. */
18922 return;
18923 }
18924
18925 do
18926 {
18927 /* Do we at least have room for a macinfo type byte? */
18928 if (mac_ptr >= mac_end)
18929 {
18930 /* Complaint is printed during the second pass as GDB will probably
18931 stop the first pass earlier upon finding
18932 DW_MACINFO_start_file. */
18933 break;
18934 }
18935
18936 macinfo_type = read_1_byte (abfd, mac_ptr);
18937 mac_ptr++;
18938
18939 /* Note that we rely on the fact that the corresponding GNU and
18940 DWARF constants are the same. */
18941 switch (macinfo_type)
18942 {
18943 /* A zero macinfo type indicates the end of the macro
18944 information. */
18945 case 0:
18946 break;
18947
18948 case DW_MACRO_GNU_define:
18949 case DW_MACRO_GNU_undef:
18950 /* Only skip the data by MAC_PTR. */
18951 {
18952 unsigned int bytes_read;
18953
18954 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18955 mac_ptr += bytes_read;
18956 read_direct_string (abfd, mac_ptr, &bytes_read);
18957 mac_ptr += bytes_read;
18958 }
18959 break;
18960
18961 case DW_MACRO_GNU_start_file:
18962 {
18963 unsigned int bytes_read;
18964 int line, file;
18965
18966 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18967 mac_ptr += bytes_read;
18968 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18969 mac_ptr += bytes_read;
18970
18971 current_file = macro_start_file (file, line, current_file,
18972 comp_dir, lh, objfile);
18973 }
18974 break;
18975
18976 case DW_MACRO_GNU_end_file:
18977 /* No data to skip by MAC_PTR. */
18978 break;
18979
18980 case DW_MACRO_GNU_define_indirect:
18981 case DW_MACRO_GNU_undef_indirect:
18982 case DW_MACRO_GNU_define_indirect_alt:
18983 case DW_MACRO_GNU_undef_indirect_alt:
18984 {
18985 unsigned int bytes_read;
18986
18987 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18988 mac_ptr += bytes_read;
18989 mac_ptr += offset_size;
18990 }
18991 break;
18992
18993 case DW_MACRO_GNU_transparent_include:
18994 case DW_MACRO_GNU_transparent_include_alt:
18995 /* Note that, according to the spec, a transparent include
18996 chain cannot call DW_MACRO_GNU_start_file. So, we can just
18997 skip this opcode. */
18998 mac_ptr += offset_size;
18999 break;
19000
19001 case DW_MACINFO_vendor_ext:
19002 /* Only skip the data by MAC_PTR. */
19003 if (!section_is_gnu)
19004 {
19005 unsigned int bytes_read;
19006
19007 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19008 mac_ptr += bytes_read;
19009 read_direct_string (abfd, mac_ptr, &bytes_read);
19010 mac_ptr += bytes_read;
19011 }
19012 /* FALLTHROUGH */
19013
19014 default:
19015 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
19016 mac_ptr, mac_end, abfd, offset_size,
19017 section);
19018 if (mac_ptr == NULL)
19019 return;
19020 break;
19021 }
19022 } while (macinfo_type != 0 && current_file == NULL);
19023
19024 /* Second pass: Process all entries.
19025
19026 Use the AT_COMMAND_LINE flag to determine whether we are still processing
19027 command-line macro definitions/undefinitions. This flag is unset when we
19028 reach the first DW_MACINFO_start_file entry. */
19029
19030 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
19031 NULL, xcalloc, xfree);
19032 cleanup = make_cleanup_htab_delete (include_hash);
19033 mac_ptr = section->buffer + offset;
19034 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
19035 *slot = mac_ptr;
19036 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
19037 current_file, lh, comp_dir, section,
19038 section_is_gnu, 0,
19039 offset_size, objfile, include_hash);
19040 do_cleanups (cleanup);
19041 }
19042
19043 /* Check if the attribute's form is a DW_FORM_block*
19044 if so return true else false. */
19045
19046 static int
19047 attr_form_is_block (struct attribute *attr)
19048 {
19049 return (attr == NULL ? 0 :
19050 attr->form == DW_FORM_block1
19051 || attr->form == DW_FORM_block2
19052 || attr->form == DW_FORM_block4
19053 || attr->form == DW_FORM_block
19054 || attr->form == DW_FORM_exprloc);
19055 }
19056
19057 /* Return non-zero if ATTR's value is a section offset --- classes
19058 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
19059 You may use DW_UNSND (attr) to retrieve such offsets.
19060
19061 Section 7.5.4, "Attribute Encodings", explains that no attribute
19062 may have a value that belongs to more than one of these classes; it
19063 would be ambiguous if we did, because we use the same forms for all
19064 of them. */
19065
19066 static int
19067 attr_form_is_section_offset (struct attribute *attr)
19068 {
19069 return (attr->form == DW_FORM_data4
19070 || attr->form == DW_FORM_data8
19071 || attr->form == DW_FORM_sec_offset);
19072 }
19073
19074 /* Return non-zero if ATTR's value falls in the 'constant' class, or
19075 zero otherwise. When this function returns true, you can apply
19076 dwarf2_get_attr_constant_value to it.
19077
19078 However, note that for some attributes you must check
19079 attr_form_is_section_offset before using this test. DW_FORM_data4
19080 and DW_FORM_data8 are members of both the constant class, and of
19081 the classes that contain offsets into other debug sections
19082 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
19083 that, if an attribute's can be either a constant or one of the
19084 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
19085 taken as section offsets, not constants. */
19086
19087 static int
19088 attr_form_is_constant (struct attribute *attr)
19089 {
19090 switch (attr->form)
19091 {
19092 case DW_FORM_sdata:
19093 case DW_FORM_udata:
19094 case DW_FORM_data1:
19095 case DW_FORM_data2:
19096 case DW_FORM_data4:
19097 case DW_FORM_data8:
19098 return 1;
19099 default:
19100 return 0;
19101 }
19102 }
19103
19104 /* Return the .debug_loc section to use for CU.
19105 For DWO files use .debug_loc.dwo. */
19106
19107 static struct dwarf2_section_info *
19108 cu_debug_loc_section (struct dwarf2_cu *cu)
19109 {
19110 if (cu->dwo_unit)
19111 return &cu->dwo_unit->dwo_file->sections.loc;
19112 return &dwarf2_per_objfile->loc;
19113 }
19114
19115 /* A helper function that fills in a dwarf2_loclist_baton. */
19116
19117 static void
19118 fill_in_loclist_baton (struct dwarf2_cu *cu,
19119 struct dwarf2_loclist_baton *baton,
19120 struct attribute *attr)
19121 {
19122 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19123
19124 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
19125
19126 baton->per_cu = cu->per_cu;
19127 gdb_assert (baton->per_cu);
19128 /* We don't know how long the location list is, but make sure we
19129 don't run off the edge of the section. */
19130 baton->size = section->size - DW_UNSND (attr);
19131 baton->data = section->buffer + DW_UNSND (attr);
19132 baton->base_address = cu->base_address;
19133 baton->from_dwo = cu->dwo_unit != NULL;
19134 }
19135
19136 static void
19137 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
19138 struct dwarf2_cu *cu, int is_block)
19139 {
19140 struct objfile *objfile = dwarf2_per_objfile->objfile;
19141 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19142
19143 if (attr_form_is_section_offset (attr)
19144 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
19145 the section. If so, fall through to the complaint in the
19146 other branch. */
19147 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
19148 {
19149 struct dwarf2_loclist_baton *baton;
19150
19151 baton = obstack_alloc (&objfile->objfile_obstack,
19152 sizeof (struct dwarf2_loclist_baton));
19153
19154 fill_in_loclist_baton (cu, baton, attr);
19155
19156 if (cu->base_known == 0)
19157 complaint (&symfile_complaints,
19158 _("Location list used without "
19159 "specifying the CU base address."));
19160
19161 SYMBOL_ACLASS_INDEX (sym) = (is_block
19162 ? dwarf2_loclist_block_index
19163 : dwarf2_loclist_index);
19164 SYMBOL_LOCATION_BATON (sym) = baton;
19165 }
19166 else
19167 {
19168 struct dwarf2_locexpr_baton *baton;
19169
19170 baton = obstack_alloc (&objfile->objfile_obstack,
19171 sizeof (struct dwarf2_locexpr_baton));
19172 baton->per_cu = cu->per_cu;
19173 gdb_assert (baton->per_cu);
19174
19175 if (attr_form_is_block (attr))
19176 {
19177 /* Note that we're just copying the block's data pointer
19178 here, not the actual data. We're still pointing into the
19179 info_buffer for SYM's objfile; right now we never release
19180 that buffer, but when we do clean up properly this may
19181 need to change. */
19182 baton->size = DW_BLOCK (attr)->size;
19183 baton->data = DW_BLOCK (attr)->data;
19184 }
19185 else
19186 {
19187 dwarf2_invalid_attrib_class_complaint ("location description",
19188 SYMBOL_NATURAL_NAME (sym));
19189 baton->size = 0;
19190 }
19191
19192 SYMBOL_ACLASS_INDEX (sym) = (is_block
19193 ? dwarf2_locexpr_block_index
19194 : dwarf2_locexpr_index);
19195 SYMBOL_LOCATION_BATON (sym) = baton;
19196 }
19197 }
19198
19199 /* Return the OBJFILE associated with the compilation unit CU. If CU
19200 came from a separate debuginfo file, then the master objfile is
19201 returned. */
19202
19203 struct objfile *
19204 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
19205 {
19206 struct objfile *objfile = per_cu->objfile;
19207
19208 /* Return the master objfile, so that we can report and look up the
19209 correct file containing this variable. */
19210 if (objfile->separate_debug_objfile_backlink)
19211 objfile = objfile->separate_debug_objfile_backlink;
19212
19213 return objfile;
19214 }
19215
19216 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
19217 (CU_HEADERP is unused in such case) or prepare a temporary copy at
19218 CU_HEADERP first. */
19219
19220 static const struct comp_unit_head *
19221 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
19222 struct dwarf2_per_cu_data *per_cu)
19223 {
19224 gdb_byte *info_ptr;
19225
19226 if (per_cu->cu)
19227 return &per_cu->cu->header;
19228
19229 info_ptr = per_cu->section->buffer + per_cu->offset.sect_off;
19230
19231 memset (cu_headerp, 0, sizeof (*cu_headerp));
19232 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
19233
19234 return cu_headerp;
19235 }
19236
19237 /* Return the address size given in the compilation unit header for CU. */
19238
19239 int
19240 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
19241 {
19242 struct comp_unit_head cu_header_local;
19243 const struct comp_unit_head *cu_headerp;
19244
19245 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19246
19247 return cu_headerp->addr_size;
19248 }
19249
19250 /* Return the offset size given in the compilation unit header for CU. */
19251
19252 int
19253 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
19254 {
19255 struct comp_unit_head cu_header_local;
19256 const struct comp_unit_head *cu_headerp;
19257
19258 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19259
19260 return cu_headerp->offset_size;
19261 }
19262
19263 /* See its dwarf2loc.h declaration. */
19264
19265 int
19266 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
19267 {
19268 struct comp_unit_head cu_header_local;
19269 const struct comp_unit_head *cu_headerp;
19270
19271 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19272
19273 if (cu_headerp->version == 2)
19274 return cu_headerp->addr_size;
19275 else
19276 return cu_headerp->offset_size;
19277 }
19278
19279 /* Return the text offset of the CU. The returned offset comes from
19280 this CU's objfile. If this objfile came from a separate debuginfo
19281 file, then the offset may be different from the corresponding
19282 offset in the parent objfile. */
19283
19284 CORE_ADDR
19285 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
19286 {
19287 struct objfile *objfile = per_cu->objfile;
19288
19289 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19290 }
19291
19292 /* Locate the .debug_info compilation unit from CU's objfile which contains
19293 the DIE at OFFSET. Raises an error on failure. */
19294
19295 static struct dwarf2_per_cu_data *
19296 dwarf2_find_containing_comp_unit (sect_offset offset,
19297 unsigned int offset_in_dwz,
19298 struct objfile *objfile)
19299 {
19300 struct dwarf2_per_cu_data *this_cu;
19301 int low, high;
19302 const sect_offset *cu_off;
19303
19304 low = 0;
19305 high = dwarf2_per_objfile->n_comp_units - 1;
19306 while (high > low)
19307 {
19308 struct dwarf2_per_cu_data *mid_cu;
19309 int mid = low + (high - low) / 2;
19310
19311 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
19312 cu_off = &mid_cu->offset;
19313 if (mid_cu->is_dwz > offset_in_dwz
19314 || (mid_cu->is_dwz == offset_in_dwz
19315 && cu_off->sect_off >= offset.sect_off))
19316 high = mid;
19317 else
19318 low = mid + 1;
19319 }
19320 gdb_assert (low == high);
19321 this_cu = dwarf2_per_objfile->all_comp_units[low];
19322 cu_off = &this_cu->offset;
19323 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
19324 {
19325 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
19326 error (_("Dwarf Error: could not find partial DIE containing "
19327 "offset 0x%lx [in module %s]"),
19328 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
19329
19330 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
19331 <= offset.sect_off);
19332 return dwarf2_per_objfile->all_comp_units[low-1];
19333 }
19334 else
19335 {
19336 this_cu = dwarf2_per_objfile->all_comp_units[low];
19337 if (low == dwarf2_per_objfile->n_comp_units - 1
19338 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
19339 error (_("invalid dwarf2 offset %u"), offset.sect_off);
19340 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
19341 return this_cu;
19342 }
19343 }
19344
19345 /* Initialize dwarf2_cu CU, owned by PER_CU. */
19346
19347 static void
19348 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
19349 {
19350 memset (cu, 0, sizeof (*cu));
19351 per_cu->cu = cu;
19352 cu->per_cu = per_cu;
19353 cu->objfile = per_cu->objfile;
19354 obstack_init (&cu->comp_unit_obstack);
19355 }
19356
19357 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
19358
19359 static void
19360 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
19361 enum language pretend_language)
19362 {
19363 struct attribute *attr;
19364
19365 /* Set the language we're debugging. */
19366 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
19367 if (attr)
19368 set_cu_language (DW_UNSND (attr), cu);
19369 else
19370 {
19371 cu->language = pretend_language;
19372 cu->language_defn = language_def (cu->language);
19373 }
19374
19375 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
19376 if (attr)
19377 cu->producer = DW_STRING (attr);
19378 }
19379
19380 /* Release one cached compilation unit, CU. We unlink it from the tree
19381 of compilation units, but we don't remove it from the read_in_chain;
19382 the caller is responsible for that.
19383 NOTE: DATA is a void * because this function is also used as a
19384 cleanup routine. */
19385
19386 static void
19387 free_heap_comp_unit (void *data)
19388 {
19389 struct dwarf2_cu *cu = data;
19390
19391 gdb_assert (cu->per_cu != NULL);
19392 cu->per_cu->cu = NULL;
19393 cu->per_cu = NULL;
19394
19395 obstack_free (&cu->comp_unit_obstack, NULL);
19396
19397 xfree (cu);
19398 }
19399
19400 /* This cleanup function is passed the address of a dwarf2_cu on the stack
19401 when we're finished with it. We can't free the pointer itself, but be
19402 sure to unlink it from the cache. Also release any associated storage. */
19403
19404 static void
19405 free_stack_comp_unit (void *data)
19406 {
19407 struct dwarf2_cu *cu = data;
19408
19409 gdb_assert (cu->per_cu != NULL);
19410 cu->per_cu->cu = NULL;
19411 cu->per_cu = NULL;
19412
19413 obstack_free (&cu->comp_unit_obstack, NULL);
19414 cu->partial_dies = NULL;
19415 }
19416
19417 /* Free all cached compilation units. */
19418
19419 static void
19420 free_cached_comp_units (void *data)
19421 {
19422 struct dwarf2_per_cu_data *per_cu, **last_chain;
19423
19424 per_cu = dwarf2_per_objfile->read_in_chain;
19425 last_chain = &dwarf2_per_objfile->read_in_chain;
19426 while (per_cu != NULL)
19427 {
19428 struct dwarf2_per_cu_data *next_cu;
19429
19430 next_cu = per_cu->cu->read_in_chain;
19431
19432 free_heap_comp_unit (per_cu->cu);
19433 *last_chain = next_cu;
19434
19435 per_cu = next_cu;
19436 }
19437 }
19438
19439 /* Increase the age counter on each cached compilation unit, and free
19440 any that are too old. */
19441
19442 static void
19443 age_cached_comp_units (void)
19444 {
19445 struct dwarf2_per_cu_data *per_cu, **last_chain;
19446
19447 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
19448 per_cu = dwarf2_per_objfile->read_in_chain;
19449 while (per_cu != NULL)
19450 {
19451 per_cu->cu->last_used ++;
19452 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
19453 dwarf2_mark (per_cu->cu);
19454 per_cu = per_cu->cu->read_in_chain;
19455 }
19456
19457 per_cu = dwarf2_per_objfile->read_in_chain;
19458 last_chain = &dwarf2_per_objfile->read_in_chain;
19459 while (per_cu != NULL)
19460 {
19461 struct dwarf2_per_cu_data *next_cu;
19462
19463 next_cu = per_cu->cu->read_in_chain;
19464
19465 if (!per_cu->cu->mark)
19466 {
19467 free_heap_comp_unit (per_cu->cu);
19468 *last_chain = next_cu;
19469 }
19470 else
19471 last_chain = &per_cu->cu->read_in_chain;
19472
19473 per_cu = next_cu;
19474 }
19475 }
19476
19477 /* Remove a single compilation unit from the cache. */
19478
19479 static void
19480 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
19481 {
19482 struct dwarf2_per_cu_data *per_cu, **last_chain;
19483
19484 per_cu = dwarf2_per_objfile->read_in_chain;
19485 last_chain = &dwarf2_per_objfile->read_in_chain;
19486 while (per_cu != NULL)
19487 {
19488 struct dwarf2_per_cu_data *next_cu;
19489
19490 next_cu = per_cu->cu->read_in_chain;
19491
19492 if (per_cu == target_per_cu)
19493 {
19494 free_heap_comp_unit (per_cu->cu);
19495 per_cu->cu = NULL;
19496 *last_chain = next_cu;
19497 break;
19498 }
19499 else
19500 last_chain = &per_cu->cu->read_in_chain;
19501
19502 per_cu = next_cu;
19503 }
19504 }
19505
19506 /* Release all extra memory associated with OBJFILE. */
19507
19508 void
19509 dwarf2_free_objfile (struct objfile *objfile)
19510 {
19511 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
19512
19513 if (dwarf2_per_objfile == NULL)
19514 return;
19515
19516 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
19517 free_cached_comp_units (NULL);
19518
19519 if (dwarf2_per_objfile->quick_file_names_table)
19520 htab_delete (dwarf2_per_objfile->quick_file_names_table);
19521
19522 /* Everything else should be on the objfile obstack. */
19523 }
19524
19525 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
19526 We store these in a hash table separate from the DIEs, and preserve them
19527 when the DIEs are flushed out of cache.
19528
19529 The CU "per_cu" pointer is needed because offset alone is not enough to
19530 uniquely identify the type. A file may have multiple .debug_types sections,
19531 or the type may come from a DWO file. Furthermore, while it's more logical
19532 to use per_cu->section+offset, with Fission the section with the data is in
19533 the DWO file but we don't know that section at the point we need it.
19534 We have to use something in dwarf2_per_cu_data (or the pointer to it)
19535 because we can enter the lookup routine, get_die_type_at_offset, from
19536 outside this file, and thus won't necessarily have PER_CU->cu.
19537 Fortunately, PER_CU is stable for the life of the objfile. */
19538
19539 struct dwarf2_per_cu_offset_and_type
19540 {
19541 const struct dwarf2_per_cu_data *per_cu;
19542 sect_offset offset;
19543 struct type *type;
19544 };
19545
19546 /* Hash function for a dwarf2_per_cu_offset_and_type. */
19547
19548 static hashval_t
19549 per_cu_offset_and_type_hash (const void *item)
19550 {
19551 const struct dwarf2_per_cu_offset_and_type *ofs = item;
19552
19553 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
19554 }
19555
19556 /* Equality function for a dwarf2_per_cu_offset_and_type. */
19557
19558 static int
19559 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
19560 {
19561 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
19562 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
19563
19564 return (ofs_lhs->per_cu == ofs_rhs->per_cu
19565 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
19566 }
19567
19568 /* Set the type associated with DIE to TYPE. Save it in CU's hash
19569 table if necessary. For convenience, return TYPE.
19570
19571 The DIEs reading must have careful ordering to:
19572 * Not cause infite loops trying to read in DIEs as a prerequisite for
19573 reading current DIE.
19574 * Not trying to dereference contents of still incompletely read in types
19575 while reading in other DIEs.
19576 * Enable referencing still incompletely read in types just by a pointer to
19577 the type without accessing its fields.
19578
19579 Therefore caller should follow these rules:
19580 * Try to fetch any prerequisite types we may need to build this DIE type
19581 before building the type and calling set_die_type.
19582 * After building type call set_die_type for current DIE as soon as
19583 possible before fetching more types to complete the current type.
19584 * Make the type as complete as possible before fetching more types. */
19585
19586 static struct type *
19587 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19588 {
19589 struct dwarf2_per_cu_offset_and_type **slot, ofs;
19590 struct objfile *objfile = cu->objfile;
19591
19592 /* For Ada types, make sure that the gnat-specific data is always
19593 initialized (if not already set). There are a few types where
19594 we should not be doing so, because the type-specific area is
19595 already used to hold some other piece of info (eg: TYPE_CODE_FLT
19596 where the type-specific area is used to store the floatformat).
19597 But this is not a problem, because the gnat-specific information
19598 is actually not needed for these types. */
19599 if (need_gnat_info (cu)
19600 && TYPE_CODE (type) != TYPE_CODE_FUNC
19601 && TYPE_CODE (type) != TYPE_CODE_FLT
19602 && !HAVE_GNAT_AUX_INFO (type))
19603 INIT_GNAT_SPECIFIC (type);
19604
19605 if (dwarf2_per_objfile->die_type_hash == NULL)
19606 {
19607 dwarf2_per_objfile->die_type_hash =
19608 htab_create_alloc_ex (127,
19609 per_cu_offset_and_type_hash,
19610 per_cu_offset_and_type_eq,
19611 NULL,
19612 &objfile->objfile_obstack,
19613 hashtab_obstack_allocate,
19614 dummy_obstack_deallocate);
19615 }
19616
19617 ofs.per_cu = cu->per_cu;
19618 ofs.offset = die->offset;
19619 ofs.type = type;
19620 slot = (struct dwarf2_per_cu_offset_and_type **)
19621 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
19622 if (*slot)
19623 complaint (&symfile_complaints,
19624 _("A problem internal to GDB: DIE 0x%x has type already set"),
19625 die->offset.sect_off);
19626 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
19627 **slot = ofs;
19628 return type;
19629 }
19630
19631 /* Look up the type for the die at OFFSET in the appropriate type_hash
19632 table, or return NULL if the die does not have a saved type. */
19633
19634 static struct type *
19635 get_die_type_at_offset (sect_offset offset,
19636 struct dwarf2_per_cu_data *per_cu)
19637 {
19638 struct dwarf2_per_cu_offset_and_type *slot, ofs;
19639
19640 if (dwarf2_per_objfile->die_type_hash == NULL)
19641 return NULL;
19642
19643 ofs.per_cu = per_cu;
19644 ofs.offset = offset;
19645 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
19646 if (slot)
19647 return slot->type;
19648 else
19649 return NULL;
19650 }
19651
19652 /* Look up the type for DIE in the appropriate type_hash table,
19653 or return NULL if DIE does not have a saved type. */
19654
19655 static struct type *
19656 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
19657 {
19658 return get_die_type_at_offset (die->offset, cu->per_cu);
19659 }
19660
19661 /* Add a dependence relationship from CU to REF_PER_CU. */
19662
19663 static void
19664 dwarf2_add_dependence (struct dwarf2_cu *cu,
19665 struct dwarf2_per_cu_data *ref_per_cu)
19666 {
19667 void **slot;
19668
19669 if (cu->dependencies == NULL)
19670 cu->dependencies
19671 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
19672 NULL, &cu->comp_unit_obstack,
19673 hashtab_obstack_allocate,
19674 dummy_obstack_deallocate);
19675
19676 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
19677 if (*slot == NULL)
19678 *slot = ref_per_cu;
19679 }
19680
19681 /* Subroutine of dwarf2_mark to pass to htab_traverse.
19682 Set the mark field in every compilation unit in the
19683 cache that we must keep because we are keeping CU. */
19684
19685 static int
19686 dwarf2_mark_helper (void **slot, void *data)
19687 {
19688 struct dwarf2_per_cu_data *per_cu;
19689
19690 per_cu = (struct dwarf2_per_cu_data *) *slot;
19691
19692 /* cu->dependencies references may not yet have been ever read if QUIT aborts
19693 reading of the chain. As such dependencies remain valid it is not much
19694 useful to track and undo them during QUIT cleanups. */
19695 if (per_cu->cu == NULL)
19696 return 1;
19697
19698 if (per_cu->cu->mark)
19699 return 1;
19700 per_cu->cu->mark = 1;
19701
19702 if (per_cu->cu->dependencies != NULL)
19703 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
19704
19705 return 1;
19706 }
19707
19708 /* Set the mark field in CU and in every other compilation unit in the
19709 cache that we must keep because we are keeping CU. */
19710
19711 static void
19712 dwarf2_mark (struct dwarf2_cu *cu)
19713 {
19714 if (cu->mark)
19715 return;
19716 cu->mark = 1;
19717 if (cu->dependencies != NULL)
19718 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
19719 }
19720
19721 static void
19722 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
19723 {
19724 while (per_cu)
19725 {
19726 per_cu->cu->mark = 0;
19727 per_cu = per_cu->cu->read_in_chain;
19728 }
19729 }
19730
19731 /* Trivial hash function for partial_die_info: the hash value of a DIE
19732 is its offset in .debug_info for this objfile. */
19733
19734 static hashval_t
19735 partial_die_hash (const void *item)
19736 {
19737 const struct partial_die_info *part_die = item;
19738
19739 return part_die->offset.sect_off;
19740 }
19741
19742 /* Trivial comparison function for partial_die_info structures: two DIEs
19743 are equal if they have the same offset. */
19744
19745 static int
19746 partial_die_eq (const void *item_lhs, const void *item_rhs)
19747 {
19748 const struct partial_die_info *part_die_lhs = item_lhs;
19749 const struct partial_die_info *part_die_rhs = item_rhs;
19750
19751 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
19752 }
19753
19754 static struct cmd_list_element *set_dwarf2_cmdlist;
19755 static struct cmd_list_element *show_dwarf2_cmdlist;
19756
19757 static void
19758 set_dwarf2_cmd (char *args, int from_tty)
19759 {
19760 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
19761 }
19762
19763 static void
19764 show_dwarf2_cmd (char *args, int from_tty)
19765 {
19766 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
19767 }
19768
19769 /* Free data associated with OBJFILE, if necessary. */
19770
19771 static void
19772 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
19773 {
19774 struct dwarf2_per_objfile *data = d;
19775 int ix;
19776
19777 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
19778 VEC_free (dwarf2_per_cu_ptr,
19779 dwarf2_per_objfile->all_comp_units[ix]->imported_symtabs);
19780
19781 for (ix = 0; ix < dwarf2_per_objfile->n_type_units; ++ix)
19782 VEC_free (dwarf2_per_cu_ptr,
19783 dwarf2_per_objfile->all_type_units[ix]->per_cu.imported_symtabs);
19784
19785 VEC_free (dwarf2_section_info_def, data->types);
19786
19787 if (data->dwo_files)
19788 free_dwo_files (data->dwo_files, objfile);
19789 if (data->dwp_file)
19790 gdb_bfd_unref (data->dwp_file->dbfd);
19791
19792 if (data->dwz_file && data->dwz_file->dwz_bfd)
19793 gdb_bfd_unref (data->dwz_file->dwz_bfd);
19794 }
19795
19796 \f
19797 /* The "save gdb-index" command. */
19798
19799 /* The contents of the hash table we create when building the string
19800 table. */
19801 struct strtab_entry
19802 {
19803 offset_type offset;
19804 const char *str;
19805 };
19806
19807 /* Hash function for a strtab_entry.
19808
19809 Function is used only during write_hash_table so no index format backward
19810 compatibility is needed. */
19811
19812 static hashval_t
19813 hash_strtab_entry (const void *e)
19814 {
19815 const struct strtab_entry *entry = e;
19816 return mapped_index_string_hash (INT_MAX, entry->str);
19817 }
19818
19819 /* Equality function for a strtab_entry. */
19820
19821 static int
19822 eq_strtab_entry (const void *a, const void *b)
19823 {
19824 const struct strtab_entry *ea = a;
19825 const struct strtab_entry *eb = b;
19826 return !strcmp (ea->str, eb->str);
19827 }
19828
19829 /* Create a strtab_entry hash table. */
19830
19831 static htab_t
19832 create_strtab (void)
19833 {
19834 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
19835 xfree, xcalloc, xfree);
19836 }
19837
19838 /* Add a string to the constant pool. Return the string's offset in
19839 host order. */
19840
19841 static offset_type
19842 add_string (htab_t table, struct obstack *cpool, const char *str)
19843 {
19844 void **slot;
19845 struct strtab_entry entry;
19846 struct strtab_entry *result;
19847
19848 entry.str = str;
19849 slot = htab_find_slot (table, &entry, INSERT);
19850 if (*slot)
19851 result = *slot;
19852 else
19853 {
19854 result = XNEW (struct strtab_entry);
19855 result->offset = obstack_object_size (cpool);
19856 result->str = str;
19857 obstack_grow_str0 (cpool, str);
19858 *slot = result;
19859 }
19860 return result->offset;
19861 }
19862
19863 /* An entry in the symbol table. */
19864 struct symtab_index_entry
19865 {
19866 /* The name of the symbol. */
19867 const char *name;
19868 /* The offset of the name in the constant pool. */
19869 offset_type index_offset;
19870 /* A sorted vector of the indices of all the CUs that hold an object
19871 of this name. */
19872 VEC (offset_type) *cu_indices;
19873 };
19874
19875 /* The symbol table. This is a power-of-2-sized hash table. */
19876 struct mapped_symtab
19877 {
19878 offset_type n_elements;
19879 offset_type size;
19880 struct symtab_index_entry **data;
19881 };
19882
19883 /* Hash function for a symtab_index_entry. */
19884
19885 static hashval_t
19886 hash_symtab_entry (const void *e)
19887 {
19888 const struct symtab_index_entry *entry = e;
19889 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
19890 sizeof (offset_type) * VEC_length (offset_type,
19891 entry->cu_indices),
19892 0);
19893 }
19894
19895 /* Equality function for a symtab_index_entry. */
19896
19897 static int
19898 eq_symtab_entry (const void *a, const void *b)
19899 {
19900 const struct symtab_index_entry *ea = a;
19901 const struct symtab_index_entry *eb = b;
19902 int len = VEC_length (offset_type, ea->cu_indices);
19903 if (len != VEC_length (offset_type, eb->cu_indices))
19904 return 0;
19905 return !memcmp (VEC_address (offset_type, ea->cu_indices),
19906 VEC_address (offset_type, eb->cu_indices),
19907 sizeof (offset_type) * len);
19908 }
19909
19910 /* Destroy a symtab_index_entry. */
19911
19912 static void
19913 delete_symtab_entry (void *p)
19914 {
19915 struct symtab_index_entry *entry = p;
19916 VEC_free (offset_type, entry->cu_indices);
19917 xfree (entry);
19918 }
19919
19920 /* Create a hash table holding symtab_index_entry objects. */
19921
19922 static htab_t
19923 create_symbol_hash_table (void)
19924 {
19925 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
19926 delete_symtab_entry, xcalloc, xfree);
19927 }
19928
19929 /* Create a new mapped symtab object. */
19930
19931 static struct mapped_symtab *
19932 create_mapped_symtab (void)
19933 {
19934 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
19935 symtab->n_elements = 0;
19936 symtab->size = 1024;
19937 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19938 return symtab;
19939 }
19940
19941 /* Destroy a mapped_symtab. */
19942
19943 static void
19944 cleanup_mapped_symtab (void *p)
19945 {
19946 struct mapped_symtab *symtab = p;
19947 /* The contents of the array are freed when the other hash table is
19948 destroyed. */
19949 xfree (symtab->data);
19950 xfree (symtab);
19951 }
19952
19953 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
19954 the slot.
19955
19956 Function is used only during write_hash_table so no index format backward
19957 compatibility is needed. */
19958
19959 static struct symtab_index_entry **
19960 find_slot (struct mapped_symtab *symtab, const char *name)
19961 {
19962 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
19963
19964 index = hash & (symtab->size - 1);
19965 step = ((hash * 17) & (symtab->size - 1)) | 1;
19966
19967 for (;;)
19968 {
19969 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
19970 return &symtab->data[index];
19971 index = (index + step) & (symtab->size - 1);
19972 }
19973 }
19974
19975 /* Expand SYMTAB's hash table. */
19976
19977 static void
19978 hash_expand (struct mapped_symtab *symtab)
19979 {
19980 offset_type old_size = symtab->size;
19981 offset_type i;
19982 struct symtab_index_entry **old_entries = symtab->data;
19983
19984 symtab->size *= 2;
19985 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19986
19987 for (i = 0; i < old_size; ++i)
19988 {
19989 if (old_entries[i])
19990 {
19991 struct symtab_index_entry **slot = find_slot (symtab,
19992 old_entries[i]->name);
19993 *slot = old_entries[i];
19994 }
19995 }
19996
19997 xfree (old_entries);
19998 }
19999
20000 /* Add an entry to SYMTAB. NAME is the name of the symbol.
20001 CU_INDEX is the index of the CU in which the symbol appears.
20002 IS_STATIC is one if the symbol is static, otherwise zero (global). */
20003
20004 static void
20005 add_index_entry (struct mapped_symtab *symtab, const char *name,
20006 int is_static, gdb_index_symbol_kind kind,
20007 offset_type cu_index)
20008 {
20009 struct symtab_index_entry **slot;
20010 offset_type cu_index_and_attrs;
20011
20012 ++symtab->n_elements;
20013 if (4 * symtab->n_elements / 3 >= symtab->size)
20014 hash_expand (symtab);
20015
20016 slot = find_slot (symtab, name);
20017 if (!*slot)
20018 {
20019 *slot = XNEW (struct symtab_index_entry);
20020 (*slot)->name = name;
20021 /* index_offset is set later. */
20022 (*slot)->cu_indices = NULL;
20023 }
20024
20025 cu_index_and_attrs = 0;
20026 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
20027 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
20028 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
20029
20030 /* We don't want to record an index value twice as we want to avoid the
20031 duplication.
20032 We process all global symbols and then all static symbols
20033 (which would allow us to avoid the duplication by only having to check
20034 the last entry pushed), but a symbol could have multiple kinds in one CU.
20035 To keep things simple we don't worry about the duplication here and
20036 sort and uniqufy the list after we've processed all symbols. */
20037 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
20038 }
20039
20040 /* qsort helper routine for uniquify_cu_indices. */
20041
20042 static int
20043 offset_type_compare (const void *ap, const void *bp)
20044 {
20045 offset_type a = *(offset_type *) ap;
20046 offset_type b = *(offset_type *) bp;
20047
20048 return (a > b) - (b > a);
20049 }
20050
20051 /* Sort and remove duplicates of all symbols' cu_indices lists. */
20052
20053 static void
20054 uniquify_cu_indices (struct mapped_symtab *symtab)
20055 {
20056 int i;
20057
20058 for (i = 0; i < symtab->size; ++i)
20059 {
20060 struct symtab_index_entry *entry = symtab->data[i];
20061
20062 if (entry
20063 && entry->cu_indices != NULL)
20064 {
20065 unsigned int next_to_insert, next_to_check;
20066 offset_type last_value;
20067
20068 qsort (VEC_address (offset_type, entry->cu_indices),
20069 VEC_length (offset_type, entry->cu_indices),
20070 sizeof (offset_type), offset_type_compare);
20071
20072 last_value = VEC_index (offset_type, entry->cu_indices, 0);
20073 next_to_insert = 1;
20074 for (next_to_check = 1;
20075 next_to_check < VEC_length (offset_type, entry->cu_indices);
20076 ++next_to_check)
20077 {
20078 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
20079 != last_value)
20080 {
20081 last_value = VEC_index (offset_type, entry->cu_indices,
20082 next_to_check);
20083 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
20084 last_value);
20085 ++next_to_insert;
20086 }
20087 }
20088 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
20089 }
20090 }
20091 }
20092
20093 /* Add a vector of indices to the constant pool. */
20094
20095 static offset_type
20096 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
20097 struct symtab_index_entry *entry)
20098 {
20099 void **slot;
20100
20101 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
20102 if (!*slot)
20103 {
20104 offset_type len = VEC_length (offset_type, entry->cu_indices);
20105 offset_type val = MAYBE_SWAP (len);
20106 offset_type iter;
20107 int i;
20108
20109 *slot = entry;
20110 entry->index_offset = obstack_object_size (cpool);
20111
20112 obstack_grow (cpool, &val, sizeof (val));
20113 for (i = 0;
20114 VEC_iterate (offset_type, entry->cu_indices, i, iter);
20115 ++i)
20116 {
20117 val = MAYBE_SWAP (iter);
20118 obstack_grow (cpool, &val, sizeof (val));
20119 }
20120 }
20121 else
20122 {
20123 struct symtab_index_entry *old_entry = *slot;
20124 entry->index_offset = old_entry->index_offset;
20125 entry = old_entry;
20126 }
20127 return entry->index_offset;
20128 }
20129
20130 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
20131 constant pool entries going into the obstack CPOOL. */
20132
20133 static void
20134 write_hash_table (struct mapped_symtab *symtab,
20135 struct obstack *output, struct obstack *cpool)
20136 {
20137 offset_type i;
20138 htab_t symbol_hash_table;
20139 htab_t str_table;
20140
20141 symbol_hash_table = create_symbol_hash_table ();
20142 str_table = create_strtab ();
20143
20144 /* We add all the index vectors to the constant pool first, to
20145 ensure alignment is ok. */
20146 for (i = 0; i < symtab->size; ++i)
20147 {
20148 if (symtab->data[i])
20149 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
20150 }
20151
20152 /* Now write out the hash table. */
20153 for (i = 0; i < symtab->size; ++i)
20154 {
20155 offset_type str_off, vec_off;
20156
20157 if (symtab->data[i])
20158 {
20159 str_off = add_string (str_table, cpool, symtab->data[i]->name);
20160 vec_off = symtab->data[i]->index_offset;
20161 }
20162 else
20163 {
20164 /* While 0 is a valid constant pool index, it is not valid
20165 to have 0 for both offsets. */
20166 str_off = 0;
20167 vec_off = 0;
20168 }
20169
20170 str_off = MAYBE_SWAP (str_off);
20171 vec_off = MAYBE_SWAP (vec_off);
20172
20173 obstack_grow (output, &str_off, sizeof (str_off));
20174 obstack_grow (output, &vec_off, sizeof (vec_off));
20175 }
20176
20177 htab_delete (str_table);
20178 htab_delete (symbol_hash_table);
20179 }
20180
20181 /* Struct to map psymtab to CU index in the index file. */
20182 struct psymtab_cu_index_map
20183 {
20184 struct partial_symtab *psymtab;
20185 unsigned int cu_index;
20186 };
20187
20188 static hashval_t
20189 hash_psymtab_cu_index (const void *item)
20190 {
20191 const struct psymtab_cu_index_map *map = item;
20192
20193 return htab_hash_pointer (map->psymtab);
20194 }
20195
20196 static int
20197 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
20198 {
20199 const struct psymtab_cu_index_map *lhs = item_lhs;
20200 const struct psymtab_cu_index_map *rhs = item_rhs;
20201
20202 return lhs->psymtab == rhs->psymtab;
20203 }
20204
20205 /* Helper struct for building the address table. */
20206 struct addrmap_index_data
20207 {
20208 struct objfile *objfile;
20209 struct obstack *addr_obstack;
20210 htab_t cu_index_htab;
20211
20212 /* Non-zero if the previous_* fields are valid.
20213 We can't write an entry until we see the next entry (since it is only then
20214 that we know the end of the entry). */
20215 int previous_valid;
20216 /* Index of the CU in the table of all CUs in the index file. */
20217 unsigned int previous_cu_index;
20218 /* Start address of the CU. */
20219 CORE_ADDR previous_cu_start;
20220 };
20221
20222 /* Write an address entry to OBSTACK. */
20223
20224 static void
20225 add_address_entry (struct objfile *objfile, struct obstack *obstack,
20226 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
20227 {
20228 offset_type cu_index_to_write;
20229 char addr[8];
20230 CORE_ADDR baseaddr;
20231
20232 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20233
20234 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
20235 obstack_grow (obstack, addr, 8);
20236 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
20237 obstack_grow (obstack, addr, 8);
20238 cu_index_to_write = MAYBE_SWAP (cu_index);
20239 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
20240 }
20241
20242 /* Worker function for traversing an addrmap to build the address table. */
20243
20244 static int
20245 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
20246 {
20247 struct addrmap_index_data *data = datap;
20248 struct partial_symtab *pst = obj;
20249
20250 if (data->previous_valid)
20251 add_address_entry (data->objfile, data->addr_obstack,
20252 data->previous_cu_start, start_addr,
20253 data->previous_cu_index);
20254
20255 data->previous_cu_start = start_addr;
20256 if (pst != NULL)
20257 {
20258 struct psymtab_cu_index_map find_map, *map;
20259 find_map.psymtab = pst;
20260 map = htab_find (data->cu_index_htab, &find_map);
20261 gdb_assert (map != NULL);
20262 data->previous_cu_index = map->cu_index;
20263 data->previous_valid = 1;
20264 }
20265 else
20266 data->previous_valid = 0;
20267
20268 return 0;
20269 }
20270
20271 /* Write OBJFILE's address map to OBSTACK.
20272 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
20273 in the index file. */
20274
20275 static void
20276 write_address_map (struct objfile *objfile, struct obstack *obstack,
20277 htab_t cu_index_htab)
20278 {
20279 struct addrmap_index_data addrmap_index_data;
20280
20281 /* When writing the address table, we have to cope with the fact that
20282 the addrmap iterator only provides the start of a region; we have to
20283 wait until the next invocation to get the start of the next region. */
20284
20285 addrmap_index_data.objfile = objfile;
20286 addrmap_index_data.addr_obstack = obstack;
20287 addrmap_index_data.cu_index_htab = cu_index_htab;
20288 addrmap_index_data.previous_valid = 0;
20289
20290 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
20291 &addrmap_index_data);
20292
20293 /* It's highly unlikely the last entry (end address = 0xff...ff)
20294 is valid, but we should still handle it.
20295 The end address is recorded as the start of the next region, but that
20296 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
20297 anyway. */
20298 if (addrmap_index_data.previous_valid)
20299 add_address_entry (objfile, obstack,
20300 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
20301 addrmap_index_data.previous_cu_index);
20302 }
20303
20304 /* Return the symbol kind of PSYM. */
20305
20306 static gdb_index_symbol_kind
20307 symbol_kind (struct partial_symbol *psym)
20308 {
20309 domain_enum domain = PSYMBOL_DOMAIN (psym);
20310 enum address_class aclass = PSYMBOL_CLASS (psym);
20311
20312 switch (domain)
20313 {
20314 case VAR_DOMAIN:
20315 switch (aclass)
20316 {
20317 case LOC_BLOCK:
20318 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
20319 case LOC_TYPEDEF:
20320 return GDB_INDEX_SYMBOL_KIND_TYPE;
20321 case LOC_COMPUTED:
20322 case LOC_CONST_BYTES:
20323 case LOC_OPTIMIZED_OUT:
20324 case LOC_STATIC:
20325 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20326 case LOC_CONST:
20327 /* Note: It's currently impossible to recognize psyms as enum values
20328 short of reading the type info. For now punt. */
20329 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20330 default:
20331 /* There are other LOC_FOO values that one might want to classify
20332 as variables, but dwarf2read.c doesn't currently use them. */
20333 return GDB_INDEX_SYMBOL_KIND_OTHER;
20334 }
20335 case STRUCT_DOMAIN:
20336 return GDB_INDEX_SYMBOL_KIND_TYPE;
20337 default:
20338 return GDB_INDEX_SYMBOL_KIND_OTHER;
20339 }
20340 }
20341
20342 /* Add a list of partial symbols to SYMTAB. */
20343
20344 static void
20345 write_psymbols (struct mapped_symtab *symtab,
20346 htab_t psyms_seen,
20347 struct partial_symbol **psymp,
20348 int count,
20349 offset_type cu_index,
20350 int is_static)
20351 {
20352 for (; count-- > 0; ++psymp)
20353 {
20354 struct partial_symbol *psym = *psymp;
20355 void **slot;
20356
20357 if (SYMBOL_LANGUAGE (psym) == language_ada)
20358 error (_("Ada is not currently supported by the index"));
20359
20360 /* Only add a given psymbol once. */
20361 slot = htab_find_slot (psyms_seen, psym, INSERT);
20362 if (!*slot)
20363 {
20364 gdb_index_symbol_kind kind = symbol_kind (psym);
20365
20366 *slot = psym;
20367 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
20368 is_static, kind, cu_index);
20369 }
20370 }
20371 }
20372
20373 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
20374 exception if there is an error. */
20375
20376 static void
20377 write_obstack (FILE *file, struct obstack *obstack)
20378 {
20379 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
20380 file)
20381 != obstack_object_size (obstack))
20382 error (_("couldn't data write to file"));
20383 }
20384
20385 /* Unlink a file if the argument is not NULL. */
20386
20387 static void
20388 unlink_if_set (void *p)
20389 {
20390 char **filename = p;
20391 if (*filename)
20392 unlink (*filename);
20393 }
20394
20395 /* A helper struct used when iterating over debug_types. */
20396 struct signatured_type_index_data
20397 {
20398 struct objfile *objfile;
20399 struct mapped_symtab *symtab;
20400 struct obstack *types_list;
20401 htab_t psyms_seen;
20402 int cu_index;
20403 };
20404
20405 /* A helper function that writes a single signatured_type to an
20406 obstack. */
20407
20408 static int
20409 write_one_signatured_type (void **slot, void *d)
20410 {
20411 struct signatured_type_index_data *info = d;
20412 struct signatured_type *entry = (struct signatured_type *) *slot;
20413 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
20414 gdb_byte val[8];
20415
20416 write_psymbols (info->symtab,
20417 info->psyms_seen,
20418 info->objfile->global_psymbols.list
20419 + psymtab->globals_offset,
20420 psymtab->n_global_syms, info->cu_index,
20421 0);
20422 write_psymbols (info->symtab,
20423 info->psyms_seen,
20424 info->objfile->static_psymbols.list
20425 + psymtab->statics_offset,
20426 psymtab->n_static_syms, info->cu_index,
20427 1);
20428
20429 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20430 entry->per_cu.offset.sect_off);
20431 obstack_grow (info->types_list, val, 8);
20432 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20433 entry->type_offset_in_tu.cu_off);
20434 obstack_grow (info->types_list, val, 8);
20435 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
20436 obstack_grow (info->types_list, val, 8);
20437
20438 ++info->cu_index;
20439
20440 return 1;
20441 }
20442
20443 /* Recurse into all "included" dependencies and write their symbols as
20444 if they appeared in this psymtab. */
20445
20446 static void
20447 recursively_write_psymbols (struct objfile *objfile,
20448 struct partial_symtab *psymtab,
20449 struct mapped_symtab *symtab,
20450 htab_t psyms_seen,
20451 offset_type cu_index)
20452 {
20453 int i;
20454
20455 for (i = 0; i < psymtab->number_of_dependencies; ++i)
20456 if (psymtab->dependencies[i]->user != NULL)
20457 recursively_write_psymbols (objfile, psymtab->dependencies[i],
20458 symtab, psyms_seen, cu_index);
20459
20460 write_psymbols (symtab,
20461 psyms_seen,
20462 objfile->global_psymbols.list + psymtab->globals_offset,
20463 psymtab->n_global_syms, cu_index,
20464 0);
20465 write_psymbols (symtab,
20466 psyms_seen,
20467 objfile->static_psymbols.list + psymtab->statics_offset,
20468 psymtab->n_static_syms, cu_index,
20469 1);
20470 }
20471
20472 /* Create an index file for OBJFILE in the directory DIR. */
20473
20474 static void
20475 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
20476 {
20477 struct cleanup *cleanup;
20478 char *filename, *cleanup_filename;
20479 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
20480 struct obstack cu_list, types_cu_list;
20481 int i;
20482 FILE *out_file;
20483 struct mapped_symtab *symtab;
20484 offset_type val, size_of_contents, total_len;
20485 struct stat st;
20486 htab_t psyms_seen;
20487 htab_t cu_index_htab;
20488 struct psymtab_cu_index_map *psymtab_cu_index_map;
20489
20490 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
20491 return;
20492
20493 if (dwarf2_per_objfile->using_index)
20494 error (_("Cannot use an index to create the index"));
20495
20496 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
20497 error (_("Cannot make an index when the file has multiple .debug_types sections"));
20498
20499 if (stat (objfile->name, &st) < 0)
20500 perror_with_name (objfile->name);
20501
20502 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
20503 INDEX_SUFFIX, (char *) NULL);
20504 cleanup = make_cleanup (xfree, filename);
20505
20506 out_file = fopen (filename, "wb");
20507 if (!out_file)
20508 error (_("Can't open `%s' for writing"), filename);
20509
20510 cleanup_filename = filename;
20511 make_cleanup (unlink_if_set, &cleanup_filename);
20512
20513 symtab = create_mapped_symtab ();
20514 make_cleanup (cleanup_mapped_symtab, symtab);
20515
20516 obstack_init (&addr_obstack);
20517 make_cleanup_obstack_free (&addr_obstack);
20518
20519 obstack_init (&cu_list);
20520 make_cleanup_obstack_free (&cu_list);
20521
20522 obstack_init (&types_cu_list);
20523 make_cleanup_obstack_free (&types_cu_list);
20524
20525 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
20526 NULL, xcalloc, xfree);
20527 make_cleanup_htab_delete (psyms_seen);
20528
20529 /* While we're scanning CU's create a table that maps a psymtab pointer
20530 (which is what addrmap records) to its index (which is what is recorded
20531 in the index file). This will later be needed to write the address
20532 table. */
20533 cu_index_htab = htab_create_alloc (100,
20534 hash_psymtab_cu_index,
20535 eq_psymtab_cu_index,
20536 NULL, xcalloc, xfree);
20537 make_cleanup_htab_delete (cu_index_htab);
20538 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
20539 xmalloc (sizeof (struct psymtab_cu_index_map)
20540 * dwarf2_per_objfile->n_comp_units);
20541 make_cleanup (xfree, psymtab_cu_index_map);
20542
20543 /* The CU list is already sorted, so we don't need to do additional
20544 work here. Also, the debug_types entries do not appear in
20545 all_comp_units, but only in their own hash table. */
20546 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
20547 {
20548 struct dwarf2_per_cu_data *per_cu
20549 = dwarf2_per_objfile->all_comp_units[i];
20550 struct partial_symtab *psymtab = per_cu->v.psymtab;
20551 gdb_byte val[8];
20552 struct psymtab_cu_index_map *map;
20553 void **slot;
20554
20555 if (psymtab->user == NULL)
20556 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
20557
20558 map = &psymtab_cu_index_map[i];
20559 map->psymtab = psymtab;
20560 map->cu_index = i;
20561 slot = htab_find_slot (cu_index_htab, map, INSERT);
20562 gdb_assert (slot != NULL);
20563 gdb_assert (*slot == NULL);
20564 *slot = map;
20565
20566 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20567 per_cu->offset.sect_off);
20568 obstack_grow (&cu_list, val, 8);
20569 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
20570 obstack_grow (&cu_list, val, 8);
20571 }
20572
20573 /* Dump the address map. */
20574 write_address_map (objfile, &addr_obstack, cu_index_htab);
20575
20576 /* Write out the .debug_type entries, if any. */
20577 if (dwarf2_per_objfile->signatured_types)
20578 {
20579 struct signatured_type_index_data sig_data;
20580
20581 sig_data.objfile = objfile;
20582 sig_data.symtab = symtab;
20583 sig_data.types_list = &types_cu_list;
20584 sig_data.psyms_seen = psyms_seen;
20585 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
20586 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
20587 write_one_signatured_type, &sig_data);
20588 }
20589
20590 /* Now that we've processed all symbols we can shrink their cu_indices
20591 lists. */
20592 uniquify_cu_indices (symtab);
20593
20594 obstack_init (&constant_pool);
20595 make_cleanup_obstack_free (&constant_pool);
20596 obstack_init (&symtab_obstack);
20597 make_cleanup_obstack_free (&symtab_obstack);
20598 write_hash_table (symtab, &symtab_obstack, &constant_pool);
20599
20600 obstack_init (&contents);
20601 make_cleanup_obstack_free (&contents);
20602 size_of_contents = 6 * sizeof (offset_type);
20603 total_len = size_of_contents;
20604
20605 /* The version number. */
20606 val = MAYBE_SWAP (8);
20607 obstack_grow (&contents, &val, sizeof (val));
20608
20609 /* The offset of the CU list from the start of the file. */
20610 val = MAYBE_SWAP (total_len);
20611 obstack_grow (&contents, &val, sizeof (val));
20612 total_len += obstack_object_size (&cu_list);
20613
20614 /* The offset of the types CU list from the start of the file. */
20615 val = MAYBE_SWAP (total_len);
20616 obstack_grow (&contents, &val, sizeof (val));
20617 total_len += obstack_object_size (&types_cu_list);
20618
20619 /* The offset of the address table from the start of the file. */
20620 val = MAYBE_SWAP (total_len);
20621 obstack_grow (&contents, &val, sizeof (val));
20622 total_len += obstack_object_size (&addr_obstack);
20623
20624 /* The offset of the symbol table from the start of the file. */
20625 val = MAYBE_SWAP (total_len);
20626 obstack_grow (&contents, &val, sizeof (val));
20627 total_len += obstack_object_size (&symtab_obstack);
20628
20629 /* The offset of the constant pool from the start of the file. */
20630 val = MAYBE_SWAP (total_len);
20631 obstack_grow (&contents, &val, sizeof (val));
20632 total_len += obstack_object_size (&constant_pool);
20633
20634 gdb_assert (obstack_object_size (&contents) == size_of_contents);
20635
20636 write_obstack (out_file, &contents);
20637 write_obstack (out_file, &cu_list);
20638 write_obstack (out_file, &types_cu_list);
20639 write_obstack (out_file, &addr_obstack);
20640 write_obstack (out_file, &symtab_obstack);
20641 write_obstack (out_file, &constant_pool);
20642
20643 fclose (out_file);
20644
20645 /* We want to keep the file, so we set cleanup_filename to NULL
20646 here. See unlink_if_set. */
20647 cleanup_filename = NULL;
20648
20649 do_cleanups (cleanup);
20650 }
20651
20652 /* Implementation of the `save gdb-index' command.
20653
20654 Note that the file format used by this command is documented in the
20655 GDB manual. Any changes here must be documented there. */
20656
20657 static void
20658 save_gdb_index_command (char *arg, int from_tty)
20659 {
20660 struct objfile *objfile;
20661
20662 if (!arg || !*arg)
20663 error (_("usage: save gdb-index DIRECTORY"));
20664
20665 ALL_OBJFILES (objfile)
20666 {
20667 struct stat st;
20668
20669 /* If the objfile does not correspond to an actual file, skip it. */
20670 if (stat (objfile->name, &st) < 0)
20671 continue;
20672
20673 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
20674 if (dwarf2_per_objfile)
20675 {
20676 volatile struct gdb_exception except;
20677
20678 TRY_CATCH (except, RETURN_MASK_ERROR)
20679 {
20680 write_psymtabs_to_index (objfile, arg);
20681 }
20682 if (except.reason < 0)
20683 exception_fprintf (gdb_stderr, except,
20684 _("Error while writing index for `%s': "),
20685 objfile->name);
20686 }
20687 }
20688 }
20689
20690 \f
20691
20692 int dwarf2_always_disassemble;
20693
20694 static void
20695 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
20696 struct cmd_list_element *c, const char *value)
20697 {
20698 fprintf_filtered (file,
20699 _("Whether to always disassemble "
20700 "DWARF expressions is %s.\n"),
20701 value);
20702 }
20703
20704 static void
20705 show_check_physname (struct ui_file *file, int from_tty,
20706 struct cmd_list_element *c, const char *value)
20707 {
20708 fprintf_filtered (file,
20709 _("Whether to check \"physname\" is %s.\n"),
20710 value);
20711 }
20712
20713 void _initialize_dwarf2_read (void);
20714
20715 void
20716 _initialize_dwarf2_read (void)
20717 {
20718 struct cmd_list_element *c;
20719
20720 dwarf2_objfile_data_key
20721 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
20722
20723 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
20724 Set DWARF 2 specific variables.\n\
20725 Configure DWARF 2 variables such as the cache size"),
20726 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
20727 0/*allow-unknown*/, &maintenance_set_cmdlist);
20728
20729 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
20730 Show DWARF 2 specific variables\n\
20731 Show DWARF 2 variables such as the cache size"),
20732 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
20733 0/*allow-unknown*/, &maintenance_show_cmdlist);
20734
20735 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
20736 &dwarf2_max_cache_age, _("\
20737 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
20738 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
20739 A higher limit means that cached compilation units will be stored\n\
20740 in memory longer, and more total memory will be used. Zero disables\n\
20741 caching, which can slow down startup."),
20742 NULL,
20743 show_dwarf2_max_cache_age,
20744 &set_dwarf2_cmdlist,
20745 &show_dwarf2_cmdlist);
20746
20747 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
20748 &dwarf2_always_disassemble, _("\
20749 Set whether `info address' always disassembles DWARF expressions."), _("\
20750 Show whether `info address' always disassembles DWARF expressions."), _("\
20751 When enabled, DWARF expressions are always printed in an assembly-like\n\
20752 syntax. When disabled, expressions will be printed in a more\n\
20753 conversational style, when possible."),
20754 NULL,
20755 show_dwarf2_always_disassemble,
20756 &set_dwarf2_cmdlist,
20757 &show_dwarf2_cmdlist);
20758
20759 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
20760 Set debugging of the dwarf2 reader."), _("\
20761 Show debugging of the dwarf2 reader."), _("\
20762 When enabled, debugging messages are printed during dwarf2 reading\n\
20763 and symtab expansion."),
20764 NULL,
20765 NULL,
20766 &setdebuglist, &showdebuglist);
20767
20768 add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
20769 Set debugging of the dwarf2 DIE reader."), _("\
20770 Show debugging of the dwarf2 DIE reader."), _("\
20771 When enabled (non-zero), DIEs are dumped after they are read in.\n\
20772 The value is the maximum depth to print."),
20773 NULL,
20774 NULL,
20775 &setdebuglist, &showdebuglist);
20776
20777 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
20778 Set cross-checking of \"physname\" code against demangler."), _("\
20779 Show cross-checking of \"physname\" code against demangler."), _("\
20780 When enabled, GDB's internal \"physname\" code is checked against\n\
20781 the demangler."),
20782 NULL, show_check_physname,
20783 &setdebuglist, &showdebuglist);
20784
20785 add_setshow_boolean_cmd ("use-deprecated-index-sections",
20786 no_class, &use_deprecated_index_sections, _("\
20787 Set whether to use deprecated gdb_index sections."), _("\
20788 Show whether to use deprecated gdb_index sections."), _("\
20789 When enabled, deprecated .gdb_index sections are used anyway.\n\
20790 Normally they are ignored either because of a missing feature or\n\
20791 performance issue.\n\
20792 Warning: This option must be enabled before gdb reads the file."),
20793 NULL,
20794 NULL,
20795 &setlist, &showlist);
20796
20797 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
20798 _("\
20799 Save a gdb-index file.\n\
20800 Usage: save gdb-index DIRECTORY"),
20801 &save_cmdlist);
20802 set_cmd_completer (c, filename_completer);
20803
20804 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
20805 &dwarf2_locexpr_funcs);
20806 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
20807 &dwarf2_loclist_funcs);
20808
20809 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
20810 &dwarf2_block_frame_base_locexpr_funcs);
20811 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
20812 &dwarf2_block_frame_base_loclist_funcs);
20813 }
This page took 0.580955 seconds and 4 git commands to generate.