2013-03-20 Jan Kratochvil <jan.kratochvil@redhat.com>
[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 unsigned int is_debug_types : 1;
547
548 /* Non-zero if this CU is from the .dwz file. */
549 unsigned int is_dwz : 1;
550
551 /* The section this CU/TU lives in.
552 If the DIE refers to a DWO file, this is always the original die,
553 not the DWO file. */
554 struct dwarf2_section_info *info_or_types_section;
555
556 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
557 of the CU cache it gets reset to NULL again. */
558 struct dwarf2_cu *cu;
559
560 /* The corresponding objfile.
561 Normally we can get the objfile from dwarf2_per_objfile.
562 However we can enter this file with just a "per_cu" handle. */
563 struct objfile *objfile;
564
565 /* When using partial symbol tables, the 'psymtab' field is active.
566 Otherwise the 'quick' field is active. */
567 union
568 {
569 /* The partial symbol table associated with this compilation unit,
570 or NULL for unread partial units. */
571 struct partial_symtab *psymtab;
572
573 /* Data needed by the "quick" functions. */
574 struct dwarf2_per_cu_quick_data *quick;
575 } v;
576
577 /* The CUs we import using DW_TAG_imported_unit. This is filled in
578 while reading psymtabs, used to compute the psymtab dependencies,
579 and then cleared. Then it is filled in again while reading full
580 symbols, and only deleted when the objfile is destroyed.
581
582 This is also used to work around a difference between the way gold
583 generates .gdb_index version <=7 and the way gdb does. Arguably this
584 is a gold bug. For symbols coming from TUs, gold records in the index
585 the CU that includes the TU instead of the TU itself. This breaks
586 dw2_lookup_symbol: It assumes that if the index says symbol X lives
587 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
588 will find X. Alas TUs live in their own symtab, so after expanding CU Y
589 we need to look in TU Z to find X. Fortunately, this is akin to
590 DW_TAG_imported_unit, so we just use the same mechanism: For
591 .gdb_index version <=7 this also records the TUs that the CU referred
592 to. Concurrently with this change gdb was modified to emit version 8
593 indices so we only pay a price for gold generated indices. */
594 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
595
596 /* Type units are grouped by their DW_AT_stmt_list entry so that they
597 can share them. If this is a TU, this points to the containing
598 symtab. */
599 struct type_unit_group *type_unit_group;
600 };
601
602 /* Entry in the signatured_types hash table. */
603
604 struct signatured_type
605 {
606 /* The "per_cu" object of this type.
607 N.B.: This is the first member so that it's easy to convert pointers
608 between them. */
609 struct dwarf2_per_cu_data per_cu;
610
611 /* The type's signature. */
612 ULONGEST signature;
613
614 /* Offset in the TU of the type's DIE, as read from the TU header.
615 If the definition lives in a DWO file, this value is unusable. */
616 cu_offset type_offset_in_tu;
617
618 /* Offset in the section of the type's DIE.
619 If the definition lives in a DWO file, this is the offset in the
620 .debug_types.dwo section.
621 The value is zero until the actual value is known.
622 Zero is otherwise not a valid section offset. */
623 sect_offset type_offset_in_section;
624 };
625
626 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
627 This includes type_unit_group and quick_file_names. */
628
629 struct stmt_list_hash
630 {
631 /* The DWO unit this table is from or NULL if there is none. */
632 struct dwo_unit *dwo_unit;
633
634 /* Offset in .debug_line or .debug_line.dwo. */
635 sect_offset line_offset;
636 };
637
638 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
639 an object of this type. */
640
641 struct type_unit_group
642 {
643 /* dwarf2read.c's main "handle" on the symtab.
644 To simplify things we create an artificial CU that "includes" all the
645 type units using this stmt_list so that the rest of the code still has
646 a "per_cu" handle on the symtab.
647 This PER_CU is recognized by having no section. */
648 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->info_or_types_section == NULL)
649 struct dwarf2_per_cu_data per_cu;
650
651 union
652 {
653 /* The TUs that share this DW_AT_stmt_list entry.
654 This is added to while parsing type units to build partial symtabs,
655 and is deleted afterwards and not used again. */
656 VEC (dwarf2_per_cu_ptr) *tus;
657
658 /* When reading the line table in "quick" functions, we need a real TU.
659 Any will do, we know they all share the same DW_AT_stmt_list entry.
660 For simplicity's sake, we pick the first one. */
661 struct dwarf2_per_cu_data *first_tu;
662 } t;
663
664 /* The primary symtab.
665 Type units in a group needn't all be defined in the same source file,
666 so we create an essentially anonymous symtab as the primary symtab. */
667 struct symtab *primary_symtab;
668
669 /* The data used to construct the hash key. */
670 struct stmt_list_hash hash;
671
672 /* The number of symtabs from the line header.
673 The value here must match line_header.num_file_names. */
674 unsigned int num_symtabs;
675
676 /* The symbol tables for this TU (obtained from the files listed in
677 DW_AT_stmt_list).
678 WARNING: The order of entries here must match the order of entries
679 in the line header. After the first TU using this type_unit_group, the
680 line header for the subsequent TUs is recreated from this. This is done
681 because we need to use the same symtabs for each TU using the same
682 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
683 there's no guarantee the line header doesn't have duplicate entries. */
684 struct symtab **symtabs;
685 };
686
687 /* These sections are what may appear in a DWO file. */
688
689 struct dwo_sections
690 {
691 struct dwarf2_section_info abbrev;
692 struct dwarf2_section_info line;
693 struct dwarf2_section_info loc;
694 struct dwarf2_section_info macinfo;
695 struct dwarf2_section_info macro;
696 struct dwarf2_section_info str;
697 struct dwarf2_section_info str_offsets;
698 /* In the case of a virtual DWO file, these two are unused. */
699 struct dwarf2_section_info info;
700 VEC (dwarf2_section_info_def) *types;
701 };
702
703 /* Common bits of DWO CUs/TUs. */
704
705 struct dwo_unit
706 {
707 /* Backlink to the containing struct dwo_file. */
708 struct dwo_file *dwo_file;
709
710 /* The "id" that distinguishes this CU/TU.
711 .debug_info calls this "dwo_id", .debug_types calls this "signature".
712 Since signatures came first, we stick with it for consistency. */
713 ULONGEST signature;
714
715 /* The section this CU/TU lives in, in the DWO file. */
716 struct dwarf2_section_info *info_or_types_section;
717
718 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
719 sect_offset offset;
720 unsigned int length;
721
722 /* For types, offset in the type's DIE of the type defined by this TU. */
723 cu_offset type_offset_in_tu;
724 };
725
726 /* Data for one DWO file.
727 This includes virtual DWO files that have been packaged into a
728 DWP file. */
729
730 struct dwo_file
731 {
732 /* The DW_AT_GNU_dwo_name attribute. This is the hash key.
733 For virtual DWO files the name is constructed from the section offsets
734 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
735 from related CU+TUs. */
736 const char *name;
737
738 /* The bfd, when the file is open. Otherwise this is NULL.
739 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
740 bfd *dbfd;
741
742 /* Section info for this file. */
743 struct dwo_sections sections;
744
745 /* Table of CUs in the file.
746 Each element is a struct dwo_unit. */
747 htab_t cus;
748
749 /* Table of TUs in the file.
750 Each element is a struct dwo_unit. */
751 htab_t tus;
752 };
753
754 /* These sections are what may appear in a DWP file. */
755
756 struct dwp_sections
757 {
758 struct dwarf2_section_info str;
759 struct dwarf2_section_info cu_index;
760 struct dwarf2_section_info tu_index;
761 /* The .debug_info.dwo, .debug_types.dwo, and other sections are referenced
762 by section number. We don't need to record them here. */
763 };
764
765 /* These sections are what may appear in a virtual DWO file. */
766
767 struct virtual_dwo_sections
768 {
769 struct dwarf2_section_info abbrev;
770 struct dwarf2_section_info line;
771 struct dwarf2_section_info loc;
772 struct dwarf2_section_info macinfo;
773 struct dwarf2_section_info macro;
774 struct dwarf2_section_info str_offsets;
775 /* Each DWP hash table entry records one CU or one TU.
776 That is recorded here, and copied to dwo_unit.info_or_types_section. */
777 struct dwarf2_section_info info_or_types;
778 };
779
780 /* Contents of DWP hash tables. */
781
782 struct dwp_hash_table
783 {
784 uint32_t nr_units, nr_slots;
785 const gdb_byte *hash_table, *unit_table, *section_pool;
786 };
787
788 /* Data for one DWP file. */
789
790 struct dwp_file
791 {
792 /* Name of the file. */
793 const char *name;
794
795 /* The bfd, when the file is open. Otherwise this is NULL. */
796 bfd *dbfd;
797
798 /* Section info for this file. */
799 struct dwp_sections sections;
800
801 /* Table of CUs in the file. */
802 const struct dwp_hash_table *cus;
803
804 /* Table of TUs in the file. */
805 const struct dwp_hash_table *tus;
806
807 /* Table of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
808 htab_t loaded_cutus;
809
810 /* Table to map ELF section numbers to their sections. */
811 unsigned int num_sections;
812 asection **elf_sections;
813 };
814
815 /* This represents a '.dwz' file. */
816
817 struct dwz_file
818 {
819 /* A dwz file can only contain a few sections. */
820 struct dwarf2_section_info abbrev;
821 struct dwarf2_section_info info;
822 struct dwarf2_section_info str;
823 struct dwarf2_section_info line;
824 struct dwarf2_section_info macro;
825 struct dwarf2_section_info gdb_index;
826
827 /* The dwz's BFD. */
828 bfd *dwz_bfd;
829 };
830
831 /* Struct used to pass misc. parameters to read_die_and_children, et
832 al. which are used for both .debug_info and .debug_types dies.
833 All parameters here are unchanging for the life of the call. This
834 struct exists to abstract away the constant parameters of die reading. */
835
836 struct die_reader_specs
837 {
838 /* die_section->asection->owner. */
839 bfd* abfd;
840
841 /* The CU of the DIE we are parsing. */
842 struct dwarf2_cu *cu;
843
844 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
845 struct dwo_file *dwo_file;
846
847 /* The section the die comes from.
848 This is either .debug_info or .debug_types, or the .dwo variants. */
849 struct dwarf2_section_info *die_section;
850
851 /* die_section->buffer. */
852 gdb_byte *buffer;
853
854 /* The end of the buffer. */
855 const gdb_byte *buffer_end;
856 };
857
858 /* Type of function passed to init_cutu_and_read_dies, et.al. */
859 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
860 gdb_byte *info_ptr,
861 struct die_info *comp_unit_die,
862 int has_children,
863 void *data);
864
865 /* The line number information for a compilation unit (found in the
866 .debug_line section) begins with a "statement program header",
867 which contains the following information. */
868 struct line_header
869 {
870 unsigned int total_length;
871 unsigned short version;
872 unsigned int header_length;
873 unsigned char minimum_instruction_length;
874 unsigned char maximum_ops_per_instruction;
875 unsigned char default_is_stmt;
876 int line_base;
877 unsigned char line_range;
878 unsigned char opcode_base;
879
880 /* standard_opcode_lengths[i] is the number of operands for the
881 standard opcode whose value is i. This means that
882 standard_opcode_lengths[0] is unused, and the last meaningful
883 element is standard_opcode_lengths[opcode_base - 1]. */
884 unsigned char *standard_opcode_lengths;
885
886 /* The include_directories table. NOTE! These strings are not
887 allocated with xmalloc; instead, they are pointers into
888 debug_line_buffer. If you try to free them, `free' will get
889 indigestion. */
890 unsigned int num_include_dirs, include_dirs_size;
891 char **include_dirs;
892
893 /* The file_names table. NOTE! These strings are not allocated
894 with xmalloc; instead, they are pointers into debug_line_buffer.
895 Don't try to free them directly. */
896 unsigned int num_file_names, file_names_size;
897 struct file_entry
898 {
899 char *name;
900 unsigned int dir_index;
901 unsigned int mod_time;
902 unsigned int length;
903 int included_p; /* Non-zero if referenced by the Line Number Program. */
904 struct symtab *symtab; /* The associated symbol table, if any. */
905 } *file_names;
906
907 /* The start and end of the statement program following this
908 header. These point into dwarf2_per_objfile->line_buffer. */
909 gdb_byte *statement_program_start, *statement_program_end;
910 };
911
912 /* When we construct a partial symbol table entry we only
913 need this much information. */
914 struct partial_die_info
915 {
916 /* Offset of this DIE. */
917 sect_offset offset;
918
919 /* DWARF-2 tag for this DIE. */
920 ENUM_BITFIELD(dwarf_tag) tag : 16;
921
922 /* Assorted flags describing the data found in this DIE. */
923 unsigned int has_children : 1;
924 unsigned int is_external : 1;
925 unsigned int is_declaration : 1;
926 unsigned int has_type : 1;
927 unsigned int has_specification : 1;
928 unsigned int has_pc_info : 1;
929 unsigned int may_be_inlined : 1;
930
931 /* Flag set if the SCOPE field of this structure has been
932 computed. */
933 unsigned int scope_set : 1;
934
935 /* Flag set if the DIE has a byte_size attribute. */
936 unsigned int has_byte_size : 1;
937
938 /* Flag set if any of the DIE's children are template arguments. */
939 unsigned int has_template_arguments : 1;
940
941 /* Flag set if fixup_partial_die has been called on this die. */
942 unsigned int fixup_called : 1;
943
944 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
945 unsigned int is_dwz : 1;
946
947 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
948 unsigned int spec_is_dwz : 1;
949
950 /* The name of this DIE. Normally the value of DW_AT_name, but
951 sometimes a default name for unnamed DIEs. */
952 const char *name;
953
954 /* The linkage name, if present. */
955 const char *linkage_name;
956
957 /* The scope to prepend to our children. This is generally
958 allocated on the comp_unit_obstack, so will disappear
959 when this compilation unit leaves the cache. */
960 const char *scope;
961
962 /* Some data associated with the partial DIE. The tag determines
963 which field is live. */
964 union
965 {
966 /* The location description associated with this DIE, if any. */
967 struct dwarf_block *locdesc;
968 /* The offset of an import, for DW_TAG_imported_unit. */
969 sect_offset offset;
970 } d;
971
972 /* If HAS_PC_INFO, the PC range associated with this DIE. */
973 CORE_ADDR lowpc;
974 CORE_ADDR highpc;
975
976 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
977 DW_AT_sibling, if any. */
978 /* NOTE: This member isn't strictly necessary, read_partial_die could
979 return DW_AT_sibling values to its caller load_partial_dies. */
980 gdb_byte *sibling;
981
982 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
983 DW_AT_specification (or DW_AT_abstract_origin or
984 DW_AT_extension). */
985 sect_offset spec_offset;
986
987 /* Pointers to this DIE's parent, first child, and next sibling,
988 if any. */
989 struct partial_die_info *die_parent, *die_child, *die_sibling;
990 };
991
992 /* This data structure holds the information of an abbrev. */
993 struct abbrev_info
994 {
995 unsigned int number; /* number identifying abbrev */
996 enum dwarf_tag tag; /* dwarf tag */
997 unsigned short has_children; /* boolean */
998 unsigned short num_attrs; /* number of attributes */
999 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1000 struct abbrev_info *next; /* next in chain */
1001 };
1002
1003 struct attr_abbrev
1004 {
1005 ENUM_BITFIELD(dwarf_attribute) name : 16;
1006 ENUM_BITFIELD(dwarf_form) form : 16;
1007 };
1008
1009 /* Size of abbrev_table.abbrev_hash_table. */
1010 #define ABBREV_HASH_SIZE 121
1011
1012 /* Top level data structure to contain an abbreviation table. */
1013
1014 struct abbrev_table
1015 {
1016 /* Where the abbrev table came from.
1017 This is used as a sanity check when the table is used. */
1018 sect_offset offset;
1019
1020 /* Storage for the abbrev table. */
1021 struct obstack abbrev_obstack;
1022
1023 /* Hash table of abbrevs.
1024 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1025 It could be statically allocated, but the previous code didn't so we
1026 don't either. */
1027 struct abbrev_info **abbrevs;
1028 };
1029
1030 /* Attributes have a name and a value. */
1031 struct attribute
1032 {
1033 ENUM_BITFIELD(dwarf_attribute) name : 16;
1034 ENUM_BITFIELD(dwarf_form) form : 15;
1035
1036 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1037 field should be in u.str (existing only for DW_STRING) but it is kept
1038 here for better struct attribute alignment. */
1039 unsigned int string_is_canonical : 1;
1040
1041 union
1042 {
1043 const char *str;
1044 struct dwarf_block *blk;
1045 ULONGEST unsnd;
1046 LONGEST snd;
1047 CORE_ADDR addr;
1048 struct signatured_type *signatured_type;
1049 }
1050 u;
1051 };
1052
1053 /* This data structure holds a complete die structure. */
1054 struct die_info
1055 {
1056 /* DWARF-2 tag for this DIE. */
1057 ENUM_BITFIELD(dwarf_tag) tag : 16;
1058
1059 /* Number of attributes */
1060 unsigned char num_attrs;
1061
1062 /* True if we're presently building the full type name for the
1063 type derived from this DIE. */
1064 unsigned char building_fullname : 1;
1065
1066 /* Abbrev number */
1067 unsigned int abbrev;
1068
1069 /* Offset in .debug_info or .debug_types section. */
1070 sect_offset offset;
1071
1072 /* The dies in a compilation unit form an n-ary tree. PARENT
1073 points to this die's parent; CHILD points to the first child of
1074 this node; and all the children of a given node are chained
1075 together via their SIBLING fields. */
1076 struct die_info *child; /* Its first child, if any. */
1077 struct die_info *sibling; /* Its next sibling, if any. */
1078 struct die_info *parent; /* Its parent, if any. */
1079
1080 /* An array of attributes, with NUM_ATTRS elements. There may be
1081 zero, but it's not common and zero-sized arrays are not
1082 sufficiently portable C. */
1083 struct attribute attrs[1];
1084 };
1085
1086 /* Get at parts of an attribute structure. */
1087
1088 #define DW_STRING(attr) ((attr)->u.str)
1089 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1090 #define DW_UNSND(attr) ((attr)->u.unsnd)
1091 #define DW_BLOCK(attr) ((attr)->u.blk)
1092 #define DW_SND(attr) ((attr)->u.snd)
1093 #define DW_ADDR(attr) ((attr)->u.addr)
1094 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
1095
1096 /* Blocks are a bunch of untyped bytes. */
1097 struct dwarf_block
1098 {
1099 size_t size;
1100
1101 /* Valid only if SIZE is not zero. */
1102 gdb_byte *data;
1103 };
1104
1105 #ifndef ATTR_ALLOC_CHUNK
1106 #define ATTR_ALLOC_CHUNK 4
1107 #endif
1108
1109 /* Allocate fields for structs, unions and enums in this size. */
1110 #ifndef DW_FIELD_ALLOC_CHUNK
1111 #define DW_FIELD_ALLOC_CHUNK 4
1112 #endif
1113
1114 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1115 but this would require a corresponding change in unpack_field_as_long
1116 and friends. */
1117 static int bits_per_byte = 8;
1118
1119 /* The routines that read and process dies for a C struct or C++ class
1120 pass lists of data member fields and lists of member function fields
1121 in an instance of a field_info structure, as defined below. */
1122 struct field_info
1123 {
1124 /* List of data member and baseclasses fields. */
1125 struct nextfield
1126 {
1127 struct nextfield *next;
1128 int accessibility;
1129 int virtuality;
1130 struct field field;
1131 }
1132 *fields, *baseclasses;
1133
1134 /* Number of fields (including baseclasses). */
1135 int nfields;
1136
1137 /* Number of baseclasses. */
1138 int nbaseclasses;
1139
1140 /* Set if the accesibility of one of the fields is not public. */
1141 int non_public_fields;
1142
1143 /* Member function fields array, entries are allocated in the order they
1144 are encountered in the object file. */
1145 struct nextfnfield
1146 {
1147 struct nextfnfield *next;
1148 struct fn_field fnfield;
1149 }
1150 *fnfields;
1151
1152 /* Member function fieldlist array, contains name of possibly overloaded
1153 member function, number of overloaded member functions and a pointer
1154 to the head of the member function field chain. */
1155 struct fnfieldlist
1156 {
1157 const char *name;
1158 int length;
1159 struct nextfnfield *head;
1160 }
1161 *fnfieldlists;
1162
1163 /* Number of entries in the fnfieldlists array. */
1164 int nfnfields;
1165
1166 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1167 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1168 struct typedef_field_list
1169 {
1170 struct typedef_field field;
1171 struct typedef_field_list *next;
1172 }
1173 *typedef_field_list;
1174 unsigned typedef_field_list_count;
1175 };
1176
1177 /* One item on the queue of compilation units to read in full symbols
1178 for. */
1179 struct dwarf2_queue_item
1180 {
1181 struct dwarf2_per_cu_data *per_cu;
1182 enum language pretend_language;
1183 struct dwarf2_queue_item *next;
1184 };
1185
1186 /* The current queue. */
1187 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1188
1189 /* Loaded secondary compilation units are kept in memory until they
1190 have not been referenced for the processing of this many
1191 compilation units. Set this to zero to disable caching. Cache
1192 sizes of up to at least twenty will improve startup time for
1193 typical inter-CU-reference binaries, at an obvious memory cost. */
1194 static int dwarf2_max_cache_age = 5;
1195 static void
1196 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
1197 struct cmd_list_element *c, const char *value)
1198 {
1199 fprintf_filtered (file, _("The upper bound on the age of cached "
1200 "dwarf2 compilation units is %s.\n"),
1201 value);
1202 }
1203
1204
1205 /* Various complaints about symbol reading that don't abort the process. */
1206
1207 static void
1208 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1209 {
1210 complaint (&symfile_complaints,
1211 _("statement list doesn't fit in .debug_line section"));
1212 }
1213
1214 static void
1215 dwarf2_debug_line_missing_file_complaint (void)
1216 {
1217 complaint (&symfile_complaints,
1218 _(".debug_line section has line data without a file"));
1219 }
1220
1221 static void
1222 dwarf2_debug_line_missing_end_sequence_complaint (void)
1223 {
1224 complaint (&symfile_complaints,
1225 _(".debug_line section has line "
1226 "program sequence without an end"));
1227 }
1228
1229 static void
1230 dwarf2_complex_location_expr_complaint (void)
1231 {
1232 complaint (&symfile_complaints, _("location expression too complex"));
1233 }
1234
1235 static void
1236 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1237 int arg3)
1238 {
1239 complaint (&symfile_complaints,
1240 _("const value length mismatch for '%s', got %d, expected %d"),
1241 arg1, arg2, arg3);
1242 }
1243
1244 static void
1245 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1246 {
1247 complaint (&symfile_complaints,
1248 _("debug info runs off end of %s section"
1249 " [in module %s]"),
1250 section->asection->name,
1251 bfd_get_filename (section->asection->owner));
1252 }
1253
1254 static void
1255 dwarf2_macro_malformed_definition_complaint (const char *arg1)
1256 {
1257 complaint (&symfile_complaints,
1258 _("macro debug info contains a "
1259 "malformed macro definition:\n`%s'"),
1260 arg1);
1261 }
1262
1263 static void
1264 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1265 {
1266 complaint (&symfile_complaints,
1267 _("invalid attribute class or form for '%s' in '%s'"),
1268 arg1, arg2);
1269 }
1270
1271 /* local function prototypes */
1272
1273 static void dwarf2_locate_sections (bfd *, asection *, void *);
1274
1275 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
1276 struct objfile *);
1277
1278 static void dwarf2_find_base_address (struct die_info *die,
1279 struct dwarf2_cu *cu);
1280
1281 static 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 struct die_info *dwarf2_extension (struct die_info *die,
1558 struct dwarf2_cu **);
1559
1560 static const char *dwarf_tag_name (unsigned int);
1561
1562 static const char *dwarf_attr_name (unsigned int);
1563
1564 static const char *dwarf_form_name (unsigned int);
1565
1566 static char *dwarf_bool_name (unsigned int);
1567
1568 static const char *dwarf_type_encoding_name (unsigned int);
1569
1570 static struct die_info *sibling_die (struct die_info *);
1571
1572 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1573
1574 static void dump_die_for_error (struct die_info *);
1575
1576 static void dump_die_1 (struct ui_file *, int level, int max_level,
1577 struct die_info *);
1578
1579 /*static*/ void dump_die (struct die_info *, int max_level);
1580
1581 static void store_in_ref_table (struct die_info *,
1582 struct dwarf2_cu *);
1583
1584 static int is_ref_attr (struct attribute *);
1585
1586 static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
1587
1588 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1589
1590 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1591 struct attribute *,
1592 struct dwarf2_cu **);
1593
1594 static struct die_info *follow_die_ref (struct die_info *,
1595 struct attribute *,
1596 struct dwarf2_cu **);
1597
1598 static struct die_info *follow_die_sig (struct die_info *,
1599 struct attribute *,
1600 struct dwarf2_cu **);
1601
1602 static struct signatured_type *lookup_signatured_type_at_offset
1603 (struct objfile *objfile,
1604 struct dwarf2_section_info *section, sect_offset offset);
1605
1606 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1607
1608 static void read_signatured_type (struct signatured_type *);
1609
1610 static struct type_unit_group *get_type_unit_group
1611 (struct dwarf2_cu *, struct attribute *);
1612
1613 static void build_type_unit_groups (die_reader_func_ftype *, void *);
1614
1615 /* memory allocation interface */
1616
1617 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1618
1619 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1620
1621 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
1622 const char *, int);
1623
1624 static int attr_form_is_block (struct attribute *);
1625
1626 static int attr_form_is_section_offset (struct attribute *);
1627
1628 static int attr_form_is_constant (struct attribute *);
1629
1630 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1631 struct dwarf2_loclist_baton *baton,
1632 struct attribute *attr);
1633
1634 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1635 struct symbol *sym,
1636 struct dwarf2_cu *cu,
1637 int is_block);
1638
1639 static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1640 gdb_byte *info_ptr,
1641 struct abbrev_info *abbrev);
1642
1643 static void free_stack_comp_unit (void *);
1644
1645 static hashval_t partial_die_hash (const void *item);
1646
1647 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1648
1649 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1650 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
1651
1652 static void init_one_comp_unit (struct dwarf2_cu *cu,
1653 struct dwarf2_per_cu_data *per_cu);
1654
1655 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1656 struct die_info *comp_unit_die,
1657 enum language pretend_language);
1658
1659 static void free_heap_comp_unit (void *);
1660
1661 static void free_cached_comp_units (void *);
1662
1663 static void age_cached_comp_units (void);
1664
1665 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1666
1667 static struct type *set_die_type (struct die_info *, struct type *,
1668 struct dwarf2_cu *);
1669
1670 static void create_all_comp_units (struct objfile *);
1671
1672 static int create_all_type_units (struct objfile *);
1673
1674 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1675 enum language);
1676
1677 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1678 enum language);
1679
1680 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1681 enum language);
1682
1683 static void dwarf2_add_dependence (struct dwarf2_cu *,
1684 struct dwarf2_per_cu_data *);
1685
1686 static void dwarf2_mark (struct dwarf2_cu *);
1687
1688 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1689
1690 static struct type *get_die_type_at_offset (sect_offset,
1691 struct dwarf2_per_cu_data *per_cu);
1692
1693 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1694
1695 static void dwarf2_release_queue (void *dummy);
1696
1697 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1698 enum language pretend_language);
1699
1700 static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1701 struct dwarf2_per_cu_data *per_cu,
1702 enum language pretend_language);
1703
1704 static void process_queue (void);
1705
1706 static void find_file_and_directory (struct die_info *die,
1707 struct dwarf2_cu *cu,
1708 const char **name, const char **comp_dir);
1709
1710 static char *file_full_name (int file, struct line_header *lh,
1711 const char *comp_dir);
1712
1713 static gdb_byte *read_and_check_comp_unit_head
1714 (struct comp_unit_head *header,
1715 struct dwarf2_section_info *section,
1716 struct dwarf2_section_info *abbrev_section, gdb_byte *info_ptr,
1717 int is_debug_types_section);
1718
1719 static void init_cutu_and_read_dies
1720 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1721 int use_existing_cu, int keep,
1722 die_reader_func_ftype *die_reader_func, void *data);
1723
1724 static void init_cutu_and_read_dies_simple
1725 (struct dwarf2_per_cu_data *this_cu,
1726 die_reader_func_ftype *die_reader_func, void *data);
1727
1728 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1729
1730 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1731
1732 static struct dwo_unit *lookup_dwo_comp_unit
1733 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1734
1735 static struct dwo_unit *lookup_dwo_type_unit
1736 (struct signatured_type *, const char *, const char *);
1737
1738 static void free_dwo_file_cleanup (void *);
1739
1740 static void process_cu_includes (void);
1741
1742 static void check_producer (struct dwarf2_cu *cu);
1743
1744 #if WORDS_BIGENDIAN
1745
1746 /* Convert VALUE between big- and little-endian. */
1747 static offset_type
1748 byte_swap (offset_type value)
1749 {
1750 offset_type result;
1751
1752 result = (value & 0xff) << 24;
1753 result |= (value & 0xff00) << 8;
1754 result |= (value & 0xff0000) >> 8;
1755 result |= (value & 0xff000000) >> 24;
1756 return result;
1757 }
1758
1759 #define MAYBE_SWAP(V) byte_swap (V)
1760
1761 #else
1762 #define MAYBE_SWAP(V) (V)
1763 #endif /* WORDS_BIGENDIAN */
1764
1765 /* The suffix for an index file. */
1766 #define INDEX_SUFFIX ".gdb-index"
1767
1768 static const char *dwarf2_physname (const char *name, struct die_info *die,
1769 struct dwarf2_cu *cu);
1770
1771 /* Try to locate the sections we need for DWARF 2 debugging
1772 information and return true if we have enough to do something.
1773 NAMES points to the dwarf2 section names, or is NULL if the standard
1774 ELF names are used. */
1775
1776 int
1777 dwarf2_has_info (struct objfile *objfile,
1778 const struct dwarf2_debug_sections *names)
1779 {
1780 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1781 if (!dwarf2_per_objfile)
1782 {
1783 /* Initialize per-objfile state. */
1784 struct dwarf2_per_objfile *data
1785 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1786
1787 memset (data, 0, sizeof (*data));
1788 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1789 dwarf2_per_objfile = data;
1790
1791 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1792 (void *) names);
1793 dwarf2_per_objfile->objfile = objfile;
1794 }
1795 return (dwarf2_per_objfile->info.asection != NULL
1796 && dwarf2_per_objfile->abbrev.asection != NULL);
1797 }
1798
1799 /* When loading sections, we look either for uncompressed section or for
1800 compressed section names. */
1801
1802 static int
1803 section_is_p (const char *section_name,
1804 const struct dwarf2_section_names *names)
1805 {
1806 if (names->normal != NULL
1807 && strcmp (section_name, names->normal) == 0)
1808 return 1;
1809 if (names->compressed != NULL
1810 && strcmp (section_name, names->compressed) == 0)
1811 return 1;
1812 return 0;
1813 }
1814
1815 /* This function is mapped across the sections and remembers the
1816 offset and size of each of the debugging sections we are interested
1817 in. */
1818
1819 static void
1820 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1821 {
1822 const struct dwarf2_debug_sections *names;
1823 flagword aflag = bfd_get_section_flags (abfd, sectp);
1824
1825 if (vnames == NULL)
1826 names = &dwarf2_elf_names;
1827 else
1828 names = (const struct dwarf2_debug_sections *) vnames;
1829
1830 if ((aflag & SEC_HAS_CONTENTS) == 0)
1831 {
1832 }
1833 else if (section_is_p (sectp->name, &names->info))
1834 {
1835 dwarf2_per_objfile->info.asection = sectp;
1836 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1837 }
1838 else if (section_is_p (sectp->name, &names->abbrev))
1839 {
1840 dwarf2_per_objfile->abbrev.asection = sectp;
1841 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1842 }
1843 else if (section_is_p (sectp->name, &names->line))
1844 {
1845 dwarf2_per_objfile->line.asection = sectp;
1846 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1847 }
1848 else if (section_is_p (sectp->name, &names->loc))
1849 {
1850 dwarf2_per_objfile->loc.asection = sectp;
1851 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1852 }
1853 else if (section_is_p (sectp->name, &names->macinfo))
1854 {
1855 dwarf2_per_objfile->macinfo.asection = sectp;
1856 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1857 }
1858 else if (section_is_p (sectp->name, &names->macro))
1859 {
1860 dwarf2_per_objfile->macro.asection = sectp;
1861 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1862 }
1863 else if (section_is_p (sectp->name, &names->str))
1864 {
1865 dwarf2_per_objfile->str.asection = sectp;
1866 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1867 }
1868 else if (section_is_p (sectp->name, &names->addr))
1869 {
1870 dwarf2_per_objfile->addr.asection = sectp;
1871 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1872 }
1873 else if (section_is_p (sectp->name, &names->frame))
1874 {
1875 dwarf2_per_objfile->frame.asection = sectp;
1876 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1877 }
1878 else if (section_is_p (sectp->name, &names->eh_frame))
1879 {
1880 dwarf2_per_objfile->eh_frame.asection = sectp;
1881 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1882 }
1883 else if (section_is_p (sectp->name, &names->ranges))
1884 {
1885 dwarf2_per_objfile->ranges.asection = sectp;
1886 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1887 }
1888 else if (section_is_p (sectp->name, &names->types))
1889 {
1890 struct dwarf2_section_info type_section;
1891
1892 memset (&type_section, 0, sizeof (type_section));
1893 type_section.asection = sectp;
1894 type_section.size = bfd_get_section_size (sectp);
1895
1896 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1897 &type_section);
1898 }
1899 else if (section_is_p (sectp->name, &names->gdb_index))
1900 {
1901 dwarf2_per_objfile->gdb_index.asection = sectp;
1902 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1903 }
1904
1905 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1906 && bfd_section_vma (abfd, sectp) == 0)
1907 dwarf2_per_objfile->has_section_at_zero = 1;
1908 }
1909
1910 /* A helper function that decides whether a section is empty,
1911 or not present. */
1912
1913 static int
1914 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1915 {
1916 return info->asection == NULL || info->size == 0;
1917 }
1918
1919 /* Read the contents of the section INFO.
1920 OBJFILE is the main object file, but not necessarily the file where
1921 the section comes from. E.g., for DWO files INFO->asection->owner
1922 is the bfd of the DWO file.
1923 If the section is compressed, uncompress it before returning. */
1924
1925 static void
1926 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1927 {
1928 asection *sectp = info->asection;
1929 bfd *abfd;
1930 gdb_byte *buf, *retbuf;
1931 unsigned char header[4];
1932
1933 if (info->readin)
1934 return;
1935 info->buffer = NULL;
1936 info->readin = 1;
1937
1938 if (dwarf2_section_empty_p (info))
1939 return;
1940
1941 abfd = sectp->owner;
1942
1943 /* If the section has relocations, we must read it ourselves.
1944 Otherwise we attach it to the BFD. */
1945 if ((sectp->flags & SEC_RELOC) == 0)
1946 {
1947 const gdb_byte *bytes = gdb_bfd_map_section (sectp, &info->size);
1948
1949 /* We have to cast away const here for historical reasons.
1950 Fixing dwarf2read to be const-correct would be quite nice. */
1951 info->buffer = (gdb_byte *) bytes;
1952 return;
1953 }
1954
1955 buf = obstack_alloc (&objfile->objfile_obstack, info->size);
1956 info->buffer = buf;
1957
1958 /* When debugging .o files, we may need to apply relocations; see
1959 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1960 We never compress sections in .o files, so we only need to
1961 try this when the section is not compressed. */
1962 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1963 if (retbuf != NULL)
1964 {
1965 info->buffer = retbuf;
1966 return;
1967 }
1968
1969 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1970 || bfd_bread (buf, info->size, abfd) != info->size)
1971 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1972 bfd_get_filename (abfd));
1973 }
1974
1975 /* A helper function that returns the size of a section in a safe way.
1976 If you are positive that the section has been read before using the
1977 size, then it is safe to refer to the dwarf2_section_info object's
1978 "size" field directly. In other cases, you must call this
1979 function, because for compressed sections the size field is not set
1980 correctly until the section has been read. */
1981
1982 static bfd_size_type
1983 dwarf2_section_size (struct objfile *objfile,
1984 struct dwarf2_section_info *info)
1985 {
1986 if (!info->readin)
1987 dwarf2_read_section (objfile, info);
1988 return info->size;
1989 }
1990
1991 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1992 SECTION_NAME. */
1993
1994 void
1995 dwarf2_get_section_info (struct objfile *objfile,
1996 enum dwarf2_section_enum sect,
1997 asection **sectp, gdb_byte **bufp,
1998 bfd_size_type *sizep)
1999 {
2000 struct dwarf2_per_objfile *data
2001 = objfile_data (objfile, dwarf2_objfile_data_key);
2002 struct dwarf2_section_info *info;
2003
2004 /* We may see an objfile without any DWARF, in which case we just
2005 return nothing. */
2006 if (data == NULL)
2007 {
2008 *sectp = NULL;
2009 *bufp = NULL;
2010 *sizep = 0;
2011 return;
2012 }
2013 switch (sect)
2014 {
2015 case DWARF2_DEBUG_FRAME:
2016 info = &data->frame;
2017 break;
2018 case DWARF2_EH_FRAME:
2019 info = &data->eh_frame;
2020 break;
2021 default:
2022 gdb_assert_not_reached ("unexpected section");
2023 }
2024
2025 dwarf2_read_section (objfile, info);
2026
2027 *sectp = info->asection;
2028 *bufp = info->buffer;
2029 *sizep = info->size;
2030 }
2031
2032 /* A helper function to find the sections for a .dwz file. */
2033
2034 static void
2035 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2036 {
2037 struct dwz_file *dwz_file = arg;
2038
2039 /* Note that we only support the standard ELF names, because .dwz
2040 is ELF-only (at the time of writing). */
2041 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2042 {
2043 dwz_file->abbrev.asection = sectp;
2044 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2045 }
2046 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2047 {
2048 dwz_file->info.asection = sectp;
2049 dwz_file->info.size = bfd_get_section_size (sectp);
2050 }
2051 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2052 {
2053 dwz_file->str.asection = sectp;
2054 dwz_file->str.size = bfd_get_section_size (sectp);
2055 }
2056 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2057 {
2058 dwz_file->line.asection = sectp;
2059 dwz_file->line.size = bfd_get_section_size (sectp);
2060 }
2061 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2062 {
2063 dwz_file->macro.asection = sectp;
2064 dwz_file->macro.size = bfd_get_section_size (sectp);
2065 }
2066 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2067 {
2068 dwz_file->gdb_index.asection = sectp;
2069 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2070 }
2071 }
2072
2073 /* Open the separate '.dwz' debug file, if needed. Error if the file
2074 cannot be found. */
2075
2076 static struct dwz_file *
2077 dwarf2_get_dwz_file (void)
2078 {
2079 bfd *abfd, *dwz_bfd;
2080 asection *section;
2081 gdb_byte *data;
2082 struct cleanup *cleanup;
2083 const char *filename;
2084 struct dwz_file *result;
2085
2086 if (dwarf2_per_objfile->dwz_file != NULL)
2087 return dwarf2_per_objfile->dwz_file;
2088
2089 abfd = dwarf2_per_objfile->objfile->obfd;
2090 section = bfd_get_section_by_name (abfd, ".gnu_debugaltlink");
2091 if (section == NULL)
2092 error (_("could not find '.gnu_debugaltlink' section"));
2093 if (!bfd_malloc_and_get_section (abfd, section, &data))
2094 error (_("could not read '.gnu_debugaltlink' section: %s"),
2095 bfd_errmsg (bfd_get_error ()));
2096 cleanup = make_cleanup (xfree, data);
2097
2098 filename = data;
2099 if (!IS_ABSOLUTE_PATH (filename))
2100 {
2101 char *abs = gdb_realpath (dwarf2_per_objfile->objfile->name);
2102 char *rel;
2103
2104 make_cleanup (xfree, abs);
2105 abs = ldirname (abs);
2106 make_cleanup (xfree, abs);
2107
2108 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2109 make_cleanup (xfree, rel);
2110 filename = rel;
2111 }
2112
2113 /* The format is just a NUL-terminated file name, followed by the
2114 build-id. For now, though, we ignore the build-id. */
2115 dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2116 if (dwz_bfd == NULL)
2117 error (_("could not read '%s': %s"), filename,
2118 bfd_errmsg (bfd_get_error ()));
2119
2120 if (!bfd_check_format (dwz_bfd, bfd_object))
2121 {
2122 gdb_bfd_unref (dwz_bfd);
2123 error (_("file '%s' was not usable: %s"), filename,
2124 bfd_errmsg (bfd_get_error ()));
2125 }
2126
2127 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2128 struct dwz_file);
2129 result->dwz_bfd = dwz_bfd;
2130
2131 bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2132
2133 do_cleanups (cleanup);
2134
2135 dwarf2_per_objfile->dwz_file = result;
2136 return result;
2137 }
2138 \f
2139 /* DWARF quick_symbols_functions support. */
2140
2141 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2142 unique line tables, so we maintain a separate table of all .debug_line
2143 derived entries to support the sharing.
2144 All the quick functions need is the list of file names. We discard the
2145 line_header when we're done and don't need to record it here. */
2146 struct quick_file_names
2147 {
2148 /* The data used to construct the hash key. */
2149 struct stmt_list_hash hash;
2150
2151 /* The number of entries in file_names, real_names. */
2152 unsigned int num_file_names;
2153
2154 /* The file names from the line table, after being run through
2155 file_full_name. */
2156 const char **file_names;
2157
2158 /* The file names from the line table after being run through
2159 gdb_realpath. These are computed lazily. */
2160 const char **real_names;
2161 };
2162
2163 /* When using the index (and thus not using psymtabs), each CU has an
2164 object of this type. This is used to hold information needed by
2165 the various "quick" methods. */
2166 struct dwarf2_per_cu_quick_data
2167 {
2168 /* The file table. This can be NULL if there was no file table
2169 or it's currently not read in.
2170 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2171 struct quick_file_names *file_names;
2172
2173 /* The corresponding symbol table. This is NULL if symbols for this
2174 CU have not yet been read. */
2175 struct symtab *symtab;
2176
2177 /* A temporary mark bit used when iterating over all CUs in
2178 expand_symtabs_matching. */
2179 unsigned int mark : 1;
2180
2181 /* True if we've tried to read the file table and found there isn't one.
2182 There will be no point in trying to read it again next time. */
2183 unsigned int no_file_data : 1;
2184 };
2185
2186 /* Utility hash function for a stmt_list_hash. */
2187
2188 static hashval_t
2189 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2190 {
2191 hashval_t v = 0;
2192
2193 if (stmt_list_hash->dwo_unit != NULL)
2194 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2195 v += stmt_list_hash->line_offset.sect_off;
2196 return v;
2197 }
2198
2199 /* Utility equality function for a stmt_list_hash. */
2200
2201 static int
2202 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2203 const struct stmt_list_hash *rhs)
2204 {
2205 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2206 return 0;
2207 if (lhs->dwo_unit != NULL
2208 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2209 return 0;
2210
2211 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2212 }
2213
2214 /* Hash function for a quick_file_names. */
2215
2216 static hashval_t
2217 hash_file_name_entry (const void *e)
2218 {
2219 const struct quick_file_names *file_data = e;
2220
2221 return hash_stmt_list_entry (&file_data->hash);
2222 }
2223
2224 /* Equality function for a quick_file_names. */
2225
2226 static int
2227 eq_file_name_entry (const void *a, const void *b)
2228 {
2229 const struct quick_file_names *ea = a;
2230 const struct quick_file_names *eb = b;
2231
2232 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2233 }
2234
2235 /* Delete function for a quick_file_names. */
2236
2237 static void
2238 delete_file_name_entry (void *e)
2239 {
2240 struct quick_file_names *file_data = e;
2241 int i;
2242
2243 for (i = 0; i < file_data->num_file_names; ++i)
2244 {
2245 xfree ((void*) file_data->file_names[i]);
2246 if (file_data->real_names)
2247 xfree ((void*) file_data->real_names[i]);
2248 }
2249
2250 /* The space for the struct itself lives on objfile_obstack,
2251 so we don't free it here. */
2252 }
2253
2254 /* Create a quick_file_names hash table. */
2255
2256 static htab_t
2257 create_quick_file_names_table (unsigned int nr_initial_entries)
2258 {
2259 return htab_create_alloc (nr_initial_entries,
2260 hash_file_name_entry, eq_file_name_entry,
2261 delete_file_name_entry, xcalloc, xfree);
2262 }
2263
2264 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2265 have to be created afterwards. You should call age_cached_comp_units after
2266 processing PER_CU->CU. dw2_setup must have been already called. */
2267
2268 static void
2269 load_cu (struct dwarf2_per_cu_data *per_cu)
2270 {
2271 if (per_cu->is_debug_types)
2272 load_full_type_unit (per_cu);
2273 else
2274 load_full_comp_unit (per_cu, language_minimal);
2275
2276 gdb_assert (per_cu->cu != NULL);
2277
2278 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2279 }
2280
2281 /* Read in the symbols for PER_CU. */
2282
2283 static void
2284 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2285 {
2286 struct cleanup *back_to;
2287
2288 /* Skip type_unit_groups, reading the type units they contain
2289 is handled elsewhere. */
2290 if (IS_TYPE_UNIT_GROUP (per_cu))
2291 return;
2292
2293 back_to = make_cleanup (dwarf2_release_queue, NULL);
2294
2295 if (dwarf2_per_objfile->using_index
2296 ? per_cu->v.quick->symtab == NULL
2297 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2298 {
2299 queue_comp_unit (per_cu, language_minimal);
2300 load_cu (per_cu);
2301 }
2302
2303 process_queue ();
2304
2305 /* Age the cache, releasing compilation units that have not
2306 been used recently. */
2307 age_cached_comp_units ();
2308
2309 do_cleanups (back_to);
2310 }
2311
2312 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2313 the objfile from which this CU came. Returns the resulting symbol
2314 table. */
2315
2316 static struct symtab *
2317 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2318 {
2319 gdb_assert (dwarf2_per_objfile->using_index);
2320 if (!per_cu->v.quick->symtab)
2321 {
2322 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2323 increment_reading_symtab ();
2324 dw2_do_instantiate_symtab (per_cu);
2325 process_cu_includes ();
2326 do_cleanups (back_to);
2327 }
2328 return per_cu->v.quick->symtab;
2329 }
2330
2331 /* Return the CU given its index.
2332
2333 This is intended for loops like:
2334
2335 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2336 + dwarf2_per_objfile->n_type_units); ++i)
2337 {
2338 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2339
2340 ...;
2341 }
2342 */
2343
2344 static struct dwarf2_per_cu_data *
2345 dw2_get_cu (int index)
2346 {
2347 if (index >= dwarf2_per_objfile->n_comp_units)
2348 {
2349 index -= dwarf2_per_objfile->n_comp_units;
2350 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2351 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
2352 }
2353
2354 return dwarf2_per_objfile->all_comp_units[index];
2355 }
2356
2357 /* Return the primary CU given its index.
2358 The difference between this function and dw2_get_cu is in the handling
2359 of type units (TUs). Here we return the type_unit_group object.
2360
2361 This is intended for loops like:
2362
2363 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2364 + dwarf2_per_objfile->n_type_unit_groups); ++i)
2365 {
2366 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2367
2368 ...;
2369 }
2370 */
2371
2372 static struct dwarf2_per_cu_data *
2373 dw2_get_primary_cu (int index)
2374 {
2375 if (index >= dwarf2_per_objfile->n_comp_units)
2376 {
2377 index -= dwarf2_per_objfile->n_comp_units;
2378 gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2379 return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
2380 }
2381
2382 return dwarf2_per_objfile->all_comp_units[index];
2383 }
2384
2385 /* A helper for create_cus_from_index that handles a given list of
2386 CUs. */
2387
2388 static void
2389 create_cus_from_index_list (struct objfile *objfile,
2390 const gdb_byte *cu_list, offset_type n_elements,
2391 struct dwarf2_section_info *section,
2392 int is_dwz,
2393 int base_offset)
2394 {
2395 offset_type i;
2396
2397 for (i = 0; i < n_elements; i += 2)
2398 {
2399 struct dwarf2_per_cu_data *the_cu;
2400 ULONGEST offset, length;
2401
2402 gdb_static_assert (sizeof (ULONGEST) >= 8);
2403 offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2404 length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2405 cu_list += 2 * 8;
2406
2407 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2408 struct dwarf2_per_cu_data);
2409 the_cu->offset.sect_off = offset;
2410 the_cu->length = length;
2411 the_cu->objfile = objfile;
2412 the_cu->info_or_types_section = section;
2413 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2414 struct dwarf2_per_cu_quick_data);
2415 the_cu->is_dwz = is_dwz;
2416 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
2417 }
2418 }
2419
2420 /* Read the CU list from the mapped index, and use it to create all
2421 the CU objects for this objfile. */
2422
2423 static void
2424 create_cus_from_index (struct objfile *objfile,
2425 const gdb_byte *cu_list, offset_type cu_list_elements,
2426 const gdb_byte *dwz_list, offset_type dwz_elements)
2427 {
2428 struct dwz_file *dwz;
2429
2430 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2431 dwarf2_per_objfile->all_comp_units
2432 = obstack_alloc (&objfile->objfile_obstack,
2433 dwarf2_per_objfile->n_comp_units
2434 * sizeof (struct dwarf2_per_cu_data *));
2435
2436 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2437 &dwarf2_per_objfile->info, 0, 0);
2438
2439 if (dwz_elements == 0)
2440 return;
2441
2442 dwz = dwarf2_get_dwz_file ();
2443 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2444 cu_list_elements / 2);
2445 }
2446
2447 /* Create the signatured type hash table from the index. */
2448
2449 static void
2450 create_signatured_type_table_from_index (struct objfile *objfile,
2451 struct dwarf2_section_info *section,
2452 const gdb_byte *bytes,
2453 offset_type elements)
2454 {
2455 offset_type i;
2456 htab_t sig_types_hash;
2457
2458 dwarf2_per_objfile->n_type_units = elements / 3;
2459 dwarf2_per_objfile->all_type_units
2460 = obstack_alloc (&objfile->objfile_obstack,
2461 dwarf2_per_objfile->n_type_units
2462 * sizeof (struct signatured_type *));
2463
2464 sig_types_hash = allocate_signatured_type_table (objfile);
2465
2466 for (i = 0; i < elements; i += 3)
2467 {
2468 struct signatured_type *sig_type;
2469 ULONGEST offset, type_offset_in_tu, signature;
2470 void **slot;
2471
2472 gdb_static_assert (sizeof (ULONGEST) >= 8);
2473 offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2474 type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
2475 BFD_ENDIAN_LITTLE);
2476 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2477 bytes += 3 * 8;
2478
2479 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2480 struct signatured_type);
2481 sig_type->signature = signature;
2482 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2483 sig_type->per_cu.is_debug_types = 1;
2484 sig_type->per_cu.info_or_types_section = section;
2485 sig_type->per_cu.offset.sect_off = offset;
2486 sig_type->per_cu.objfile = objfile;
2487 sig_type->per_cu.v.quick
2488 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2489 struct dwarf2_per_cu_quick_data);
2490
2491 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2492 *slot = sig_type;
2493
2494 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
2495 }
2496
2497 dwarf2_per_objfile->signatured_types = sig_types_hash;
2498 }
2499
2500 /* Read the address map data from the mapped index, and use it to
2501 populate the objfile's psymtabs_addrmap. */
2502
2503 static void
2504 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2505 {
2506 const gdb_byte *iter, *end;
2507 struct obstack temp_obstack;
2508 struct addrmap *mutable_map;
2509 struct cleanup *cleanup;
2510 CORE_ADDR baseaddr;
2511
2512 obstack_init (&temp_obstack);
2513 cleanup = make_cleanup_obstack_free (&temp_obstack);
2514 mutable_map = addrmap_create_mutable (&temp_obstack);
2515
2516 iter = index->address_table;
2517 end = iter + index->address_table_size;
2518
2519 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2520
2521 while (iter < end)
2522 {
2523 ULONGEST hi, lo, cu_index;
2524 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2525 iter += 8;
2526 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2527 iter += 8;
2528 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2529 iter += 4;
2530
2531 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2532 dw2_get_cu (cu_index));
2533 }
2534
2535 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2536 &objfile->objfile_obstack);
2537 do_cleanups (cleanup);
2538 }
2539
2540 /* The hash function for strings in the mapped index. This is the same as
2541 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2542 implementation. This is necessary because the hash function is tied to the
2543 format of the mapped index file. The hash values do not have to match with
2544 SYMBOL_HASH_NEXT.
2545
2546 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2547
2548 static hashval_t
2549 mapped_index_string_hash (int index_version, const void *p)
2550 {
2551 const unsigned char *str = (const unsigned char *) p;
2552 hashval_t r = 0;
2553 unsigned char c;
2554
2555 while ((c = *str++) != 0)
2556 {
2557 if (index_version >= 5)
2558 c = tolower (c);
2559 r = r * 67 + c - 113;
2560 }
2561
2562 return r;
2563 }
2564
2565 /* Find a slot in the mapped index INDEX for the object named NAME.
2566 If NAME is found, set *VEC_OUT to point to the CU vector in the
2567 constant pool and return 1. If NAME cannot be found, return 0. */
2568
2569 static int
2570 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2571 offset_type **vec_out)
2572 {
2573 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2574 offset_type hash;
2575 offset_type slot, step;
2576 int (*cmp) (const char *, const char *);
2577
2578 if (current_language->la_language == language_cplus
2579 || current_language->la_language == language_java
2580 || current_language->la_language == language_fortran)
2581 {
2582 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2583 not contain any. */
2584 const char *paren = strchr (name, '(');
2585
2586 if (paren)
2587 {
2588 char *dup;
2589
2590 dup = xmalloc (paren - name + 1);
2591 memcpy (dup, name, paren - name);
2592 dup[paren - name] = 0;
2593
2594 make_cleanup (xfree, dup);
2595 name = dup;
2596 }
2597 }
2598
2599 /* Index version 4 did not support case insensitive searches. But the
2600 indices for case insensitive languages are built in lowercase, therefore
2601 simulate our NAME being searched is also lowercased. */
2602 hash = mapped_index_string_hash ((index->version == 4
2603 && case_sensitivity == case_sensitive_off
2604 ? 5 : index->version),
2605 name);
2606
2607 slot = hash & (index->symbol_table_slots - 1);
2608 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2609 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2610
2611 for (;;)
2612 {
2613 /* Convert a slot number to an offset into the table. */
2614 offset_type i = 2 * slot;
2615 const char *str;
2616 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2617 {
2618 do_cleanups (back_to);
2619 return 0;
2620 }
2621
2622 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2623 if (!cmp (name, str))
2624 {
2625 *vec_out = (offset_type *) (index->constant_pool
2626 + MAYBE_SWAP (index->symbol_table[i + 1]));
2627 do_cleanups (back_to);
2628 return 1;
2629 }
2630
2631 slot = (slot + step) & (index->symbol_table_slots - 1);
2632 }
2633 }
2634
2635 /* A helper function that reads the .gdb_index from SECTION and fills
2636 in MAP. FILENAME is the name of the file containing the section;
2637 it is used for error reporting. DEPRECATED_OK is nonzero if it is
2638 ok to use deprecated sections.
2639
2640 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2641 out parameters that are filled in with information about the CU and
2642 TU lists in the section.
2643
2644 Returns 1 if all went well, 0 otherwise. */
2645
2646 static int
2647 read_index_from_section (struct objfile *objfile,
2648 const char *filename,
2649 int deprecated_ok,
2650 struct dwarf2_section_info *section,
2651 struct mapped_index *map,
2652 const gdb_byte **cu_list,
2653 offset_type *cu_list_elements,
2654 const gdb_byte **types_list,
2655 offset_type *types_list_elements)
2656 {
2657 char *addr;
2658 offset_type version;
2659 offset_type *metadata;
2660 int i;
2661
2662 if (dwarf2_section_empty_p (section))
2663 return 0;
2664
2665 /* Older elfutils strip versions could keep the section in the main
2666 executable while splitting it for the separate debug info file. */
2667 if ((bfd_get_file_flags (section->asection) & SEC_HAS_CONTENTS) == 0)
2668 return 0;
2669
2670 dwarf2_read_section (objfile, section);
2671
2672 addr = section->buffer;
2673 /* Version check. */
2674 version = MAYBE_SWAP (*(offset_type *) addr);
2675 /* Versions earlier than 3 emitted every copy of a psymbol. This
2676 causes the index to behave very poorly for certain requests. Version 3
2677 contained incomplete addrmap. So, it seems better to just ignore such
2678 indices. */
2679 if (version < 4)
2680 {
2681 static int warning_printed = 0;
2682 if (!warning_printed)
2683 {
2684 warning (_("Skipping obsolete .gdb_index section in %s."),
2685 filename);
2686 warning_printed = 1;
2687 }
2688 return 0;
2689 }
2690 /* Index version 4 uses a different hash function than index version
2691 5 and later.
2692
2693 Versions earlier than 6 did not emit psymbols for inlined
2694 functions. Using these files will cause GDB not to be able to
2695 set breakpoints on inlined functions by name, so we ignore these
2696 indices unless the user has done
2697 "set use-deprecated-index-sections on". */
2698 if (version < 6 && !deprecated_ok)
2699 {
2700 static int warning_printed = 0;
2701 if (!warning_printed)
2702 {
2703 warning (_("\
2704 Skipping deprecated .gdb_index section in %s.\n\
2705 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2706 to use the section anyway."),
2707 filename);
2708 warning_printed = 1;
2709 }
2710 return 0;
2711 }
2712 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2713 of the TU (for symbols coming from TUs). It's just a performance bug, and
2714 we can't distinguish gdb-generated indices from gold-generated ones, so
2715 nothing to do here. */
2716
2717 /* Indexes with higher version than the one supported by GDB may be no
2718 longer backward compatible. */
2719 if (version > 8)
2720 return 0;
2721
2722 map->version = version;
2723 map->total_size = section->size;
2724
2725 metadata = (offset_type *) (addr + sizeof (offset_type));
2726
2727 i = 0;
2728 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2729 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2730 / 8);
2731 ++i;
2732
2733 *types_list = addr + MAYBE_SWAP (metadata[i]);
2734 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2735 - MAYBE_SWAP (metadata[i]))
2736 / 8);
2737 ++i;
2738
2739 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2740 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2741 - MAYBE_SWAP (metadata[i]));
2742 ++i;
2743
2744 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2745 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2746 - MAYBE_SWAP (metadata[i]))
2747 / (2 * sizeof (offset_type)));
2748 ++i;
2749
2750 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2751
2752 return 1;
2753 }
2754
2755
2756 /* Read the index file. If everything went ok, initialize the "quick"
2757 elements of all the CUs and return 1. Otherwise, return 0. */
2758
2759 static int
2760 dwarf2_read_index (struct objfile *objfile)
2761 {
2762 struct mapped_index local_map, *map;
2763 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2764 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2765
2766 if (!read_index_from_section (objfile, objfile->name,
2767 use_deprecated_index_sections,
2768 &dwarf2_per_objfile->gdb_index, &local_map,
2769 &cu_list, &cu_list_elements,
2770 &types_list, &types_list_elements))
2771 return 0;
2772
2773 /* Don't use the index if it's empty. */
2774 if (local_map.symbol_table_slots == 0)
2775 return 0;
2776
2777 /* If there is a .dwz file, read it so we can get its CU list as
2778 well. */
2779 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
2780 {
2781 struct dwz_file *dwz = dwarf2_get_dwz_file ();
2782 struct mapped_index dwz_map;
2783 const gdb_byte *dwz_types_ignore;
2784 offset_type dwz_types_elements_ignore;
2785
2786 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2787 1,
2788 &dwz->gdb_index, &dwz_map,
2789 &dwz_list, &dwz_list_elements,
2790 &dwz_types_ignore,
2791 &dwz_types_elements_ignore))
2792 {
2793 warning (_("could not read '.gdb_index' section from %s; skipping"),
2794 bfd_get_filename (dwz->dwz_bfd));
2795 return 0;
2796 }
2797 }
2798
2799 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
2800 dwz_list_elements);
2801
2802 if (types_list_elements)
2803 {
2804 struct dwarf2_section_info *section;
2805
2806 /* We can only handle a single .debug_types when we have an
2807 index. */
2808 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2809 return 0;
2810
2811 section = VEC_index (dwarf2_section_info_def,
2812 dwarf2_per_objfile->types, 0);
2813
2814 create_signatured_type_table_from_index (objfile, section, types_list,
2815 types_list_elements);
2816 }
2817
2818 create_addrmap_from_index (objfile, &local_map);
2819
2820 map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2821 *map = local_map;
2822
2823 dwarf2_per_objfile->index_table = map;
2824 dwarf2_per_objfile->using_index = 1;
2825 dwarf2_per_objfile->quick_file_names_table =
2826 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2827
2828 return 1;
2829 }
2830
2831 /* A helper for the "quick" functions which sets the global
2832 dwarf2_per_objfile according to OBJFILE. */
2833
2834 static void
2835 dw2_setup (struct objfile *objfile)
2836 {
2837 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2838 gdb_assert (dwarf2_per_objfile);
2839 }
2840
2841 /* die_reader_func for dw2_get_file_names. */
2842
2843 static void
2844 dw2_get_file_names_reader (const struct die_reader_specs *reader,
2845 gdb_byte *info_ptr,
2846 struct die_info *comp_unit_die,
2847 int has_children,
2848 void *data)
2849 {
2850 struct dwarf2_cu *cu = reader->cu;
2851 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2852 struct objfile *objfile = dwarf2_per_objfile->objfile;
2853 struct dwarf2_per_cu_data *lh_cu;
2854 struct line_header *lh;
2855 struct attribute *attr;
2856 int i;
2857 const char *name, *comp_dir;
2858 void **slot;
2859 struct quick_file_names *qfn;
2860 unsigned int line_offset;
2861
2862 /* Our callers never want to match partial units -- instead they
2863 will match the enclosing full CU. */
2864 if (comp_unit_die->tag == DW_TAG_partial_unit)
2865 {
2866 this_cu->v.quick->no_file_data = 1;
2867 return;
2868 }
2869
2870 /* If we're reading the line header for TUs, store it in the "per_cu"
2871 for tu_group. */
2872 if (this_cu->is_debug_types)
2873 {
2874 struct type_unit_group *tu_group = data;
2875
2876 gdb_assert (tu_group != NULL);
2877 lh_cu = &tu_group->per_cu;
2878 }
2879 else
2880 lh_cu = this_cu;
2881
2882 lh = NULL;
2883 slot = NULL;
2884 line_offset = 0;
2885
2886 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2887 if (attr)
2888 {
2889 struct quick_file_names find_entry;
2890
2891 line_offset = DW_UNSND (attr);
2892
2893 /* We may have already read in this line header (TU line header sharing).
2894 If we have we're done. */
2895 find_entry.hash.dwo_unit = cu->dwo_unit;
2896 find_entry.hash.line_offset.sect_off = line_offset;
2897 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2898 &find_entry, INSERT);
2899 if (*slot != NULL)
2900 {
2901 lh_cu->v.quick->file_names = *slot;
2902 return;
2903 }
2904
2905 lh = dwarf_decode_line_header (line_offset, cu);
2906 }
2907 if (lh == NULL)
2908 {
2909 lh_cu->v.quick->no_file_data = 1;
2910 return;
2911 }
2912
2913 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2914 qfn->hash.dwo_unit = cu->dwo_unit;
2915 qfn->hash.line_offset.sect_off = line_offset;
2916 gdb_assert (slot != NULL);
2917 *slot = qfn;
2918
2919 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
2920
2921 qfn->num_file_names = lh->num_file_names;
2922 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2923 lh->num_file_names * sizeof (char *));
2924 for (i = 0; i < lh->num_file_names; ++i)
2925 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2926 qfn->real_names = NULL;
2927
2928 free_line_header (lh);
2929
2930 lh_cu->v.quick->file_names = qfn;
2931 }
2932
2933 /* A helper for the "quick" functions which attempts to read the line
2934 table for THIS_CU. */
2935
2936 static struct quick_file_names *
2937 dw2_get_file_names (struct objfile *objfile,
2938 struct dwarf2_per_cu_data *this_cu)
2939 {
2940 /* For TUs this should only be called on the parent group. */
2941 if (this_cu->is_debug_types)
2942 gdb_assert (IS_TYPE_UNIT_GROUP (this_cu));
2943
2944 if (this_cu->v.quick->file_names != NULL)
2945 return this_cu->v.quick->file_names;
2946 /* If we know there is no line data, no point in looking again. */
2947 if (this_cu->v.quick->no_file_data)
2948 return NULL;
2949
2950 /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2951 in the stub for CUs, there's is no need to lookup the DWO file.
2952 However, that's not the case for TUs where DW_AT_stmt_list lives in the
2953 DWO file. */
2954 if (this_cu->is_debug_types)
2955 {
2956 struct type_unit_group *tu_group = this_cu->type_unit_group;
2957
2958 init_cutu_and_read_dies (tu_group->t.first_tu, NULL, 0, 0,
2959 dw2_get_file_names_reader, tu_group);
2960 }
2961 else
2962 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
2963
2964 if (this_cu->v.quick->no_file_data)
2965 return NULL;
2966 return this_cu->v.quick->file_names;
2967 }
2968
2969 /* A helper for the "quick" functions which computes and caches the
2970 real path for a given file name from the line table. */
2971
2972 static const char *
2973 dw2_get_real_path (struct objfile *objfile,
2974 struct quick_file_names *qfn, int index)
2975 {
2976 if (qfn->real_names == NULL)
2977 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2978 qfn->num_file_names, sizeof (char *));
2979
2980 if (qfn->real_names[index] == NULL)
2981 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2982
2983 return qfn->real_names[index];
2984 }
2985
2986 static struct symtab *
2987 dw2_find_last_source_symtab (struct objfile *objfile)
2988 {
2989 int index;
2990
2991 dw2_setup (objfile);
2992 index = dwarf2_per_objfile->n_comp_units - 1;
2993 return dw2_instantiate_symtab (dw2_get_cu (index));
2994 }
2995
2996 /* Traversal function for dw2_forget_cached_source_info. */
2997
2998 static int
2999 dw2_free_cached_file_names (void **slot, void *info)
3000 {
3001 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3002
3003 if (file_data->real_names)
3004 {
3005 int i;
3006
3007 for (i = 0; i < file_data->num_file_names; ++i)
3008 {
3009 xfree ((void*) file_data->real_names[i]);
3010 file_data->real_names[i] = NULL;
3011 }
3012 }
3013
3014 return 1;
3015 }
3016
3017 static void
3018 dw2_forget_cached_source_info (struct objfile *objfile)
3019 {
3020 dw2_setup (objfile);
3021
3022 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3023 dw2_free_cached_file_names, NULL);
3024 }
3025
3026 /* Helper function for dw2_map_symtabs_matching_filename that expands
3027 the symtabs and calls the iterator. */
3028
3029 static int
3030 dw2_map_expand_apply (struct objfile *objfile,
3031 struct dwarf2_per_cu_data *per_cu,
3032 const char *name, const char *real_path,
3033 int (*callback) (struct symtab *, void *),
3034 void *data)
3035 {
3036 struct symtab *last_made = objfile->symtabs;
3037
3038 /* Don't visit already-expanded CUs. */
3039 if (per_cu->v.quick->symtab)
3040 return 0;
3041
3042 /* This may expand more than one symtab, and we want to iterate over
3043 all of them. */
3044 dw2_instantiate_symtab (per_cu);
3045
3046 return iterate_over_some_symtabs (name, real_path, callback, data,
3047 objfile->symtabs, last_made);
3048 }
3049
3050 /* Implementation of the map_symtabs_matching_filename method. */
3051
3052 static int
3053 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
3054 const char *real_path,
3055 int (*callback) (struct symtab *, void *),
3056 void *data)
3057 {
3058 int i;
3059 const char *name_basename = lbasename (name);
3060
3061 dw2_setup (objfile);
3062
3063 /* The rule is CUs specify all the files, including those used by
3064 any TU, so there's no need to scan TUs here. */
3065
3066 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3067 {
3068 int j;
3069 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3070 struct quick_file_names *file_data;
3071
3072 /* We only need to look at symtabs not already expanded. */
3073 if (per_cu->v.quick->symtab)
3074 continue;
3075
3076 file_data = dw2_get_file_names (objfile, per_cu);
3077 if (file_data == NULL)
3078 continue;
3079
3080 for (j = 0; j < file_data->num_file_names; ++j)
3081 {
3082 const char *this_name = file_data->file_names[j];
3083 const char *this_real_name;
3084
3085 if (compare_filenames_for_search (this_name, name))
3086 {
3087 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3088 callback, data))
3089 return 1;
3090 continue;
3091 }
3092
3093 /* Before we invoke realpath, which can get expensive when many
3094 files are involved, do a quick comparison of the basenames. */
3095 if (! basenames_may_differ
3096 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3097 continue;
3098
3099 this_real_name = dw2_get_real_path (objfile, file_data, j);
3100 if (compare_filenames_for_search (this_real_name, name))
3101 {
3102 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3103 callback, data))
3104 return 1;
3105 continue;
3106 }
3107
3108 if (real_path != NULL)
3109 {
3110 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3111 gdb_assert (IS_ABSOLUTE_PATH (name));
3112 if (this_real_name != NULL
3113 && FILENAME_CMP (real_path, this_real_name) == 0)
3114 {
3115 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3116 callback, data))
3117 return 1;
3118 continue;
3119 }
3120 }
3121 }
3122 }
3123
3124 return 0;
3125 }
3126
3127 /* Struct used to manage iterating over all CUs looking for a symbol. */
3128
3129 struct dw2_symtab_iterator
3130 {
3131 /* The internalized form of .gdb_index. */
3132 struct mapped_index *index;
3133 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3134 int want_specific_block;
3135 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3136 Unused if !WANT_SPECIFIC_BLOCK. */
3137 int block_index;
3138 /* The kind of symbol we're looking for. */
3139 domain_enum domain;
3140 /* The list of CUs from the index entry of the symbol,
3141 or NULL if not found. */
3142 offset_type *vec;
3143 /* The next element in VEC to look at. */
3144 int next;
3145 /* The number of elements in VEC, or zero if there is no match. */
3146 int length;
3147 };
3148
3149 /* Initialize the index symtab iterator ITER.
3150 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3151 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
3152
3153 static void
3154 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3155 struct mapped_index *index,
3156 int want_specific_block,
3157 int block_index,
3158 domain_enum domain,
3159 const char *name)
3160 {
3161 iter->index = index;
3162 iter->want_specific_block = want_specific_block;
3163 iter->block_index = block_index;
3164 iter->domain = domain;
3165 iter->next = 0;
3166
3167 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3168 iter->length = MAYBE_SWAP (*iter->vec);
3169 else
3170 {
3171 iter->vec = NULL;
3172 iter->length = 0;
3173 }
3174 }
3175
3176 /* Return the next matching CU or NULL if there are no more. */
3177
3178 static struct dwarf2_per_cu_data *
3179 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3180 {
3181 for ( ; iter->next < iter->length; ++iter->next)
3182 {
3183 offset_type cu_index_and_attrs =
3184 MAYBE_SWAP (iter->vec[iter->next + 1]);
3185 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3186 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
3187 int want_static = iter->block_index != GLOBAL_BLOCK;
3188 /* This value is only valid for index versions >= 7. */
3189 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3190 gdb_index_symbol_kind symbol_kind =
3191 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3192 /* Only check the symbol attributes if they're present.
3193 Indices prior to version 7 don't record them,
3194 and indices >= 7 may elide them for certain symbols
3195 (gold does this). */
3196 int attrs_valid =
3197 (iter->index->version >= 7
3198 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3199
3200 /* Skip if already read in. */
3201 if (per_cu->v.quick->symtab)
3202 continue;
3203
3204 if (attrs_valid
3205 && iter->want_specific_block
3206 && want_static != is_static)
3207 continue;
3208
3209 /* Only check the symbol's kind if it has one. */
3210 if (attrs_valid)
3211 {
3212 switch (iter->domain)
3213 {
3214 case VAR_DOMAIN:
3215 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3216 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3217 /* Some types are also in VAR_DOMAIN. */
3218 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3219 continue;
3220 break;
3221 case STRUCT_DOMAIN:
3222 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3223 continue;
3224 break;
3225 case LABEL_DOMAIN:
3226 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3227 continue;
3228 break;
3229 default:
3230 break;
3231 }
3232 }
3233
3234 ++iter->next;
3235 return per_cu;
3236 }
3237
3238 return NULL;
3239 }
3240
3241 static struct symtab *
3242 dw2_lookup_symbol (struct objfile *objfile, int block_index,
3243 const char *name, domain_enum domain)
3244 {
3245 struct symtab *stab_best = NULL;
3246 struct mapped_index *index;
3247
3248 dw2_setup (objfile);
3249
3250 index = dwarf2_per_objfile->index_table;
3251
3252 /* index is NULL if OBJF_READNOW. */
3253 if (index)
3254 {
3255 struct dw2_symtab_iterator iter;
3256 struct dwarf2_per_cu_data *per_cu;
3257
3258 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
3259
3260 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3261 {
3262 struct symbol *sym = NULL;
3263 struct symtab *stab = dw2_instantiate_symtab (per_cu);
3264
3265 /* Some caution must be observed with overloaded functions
3266 and methods, since the index will not contain any overload
3267 information (but NAME might contain it). */
3268 if (stab->primary)
3269 {
3270 struct blockvector *bv = BLOCKVECTOR (stab);
3271 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3272
3273 sym = lookup_block_symbol (block, name, domain);
3274 }
3275
3276 if (sym && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3277 {
3278 if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
3279 return stab;
3280
3281 stab_best = stab;
3282 }
3283
3284 /* Keep looking through other CUs. */
3285 }
3286 }
3287
3288 return stab_best;
3289 }
3290
3291 static void
3292 dw2_print_stats (struct objfile *objfile)
3293 {
3294 int i, count;
3295
3296 dw2_setup (objfile);
3297 count = 0;
3298 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3299 + dwarf2_per_objfile->n_type_units); ++i)
3300 {
3301 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3302
3303 if (!per_cu->v.quick->symtab)
3304 ++count;
3305 }
3306 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3307 }
3308
3309 static void
3310 dw2_dump (struct objfile *objfile)
3311 {
3312 /* Nothing worth printing. */
3313 }
3314
3315 static void
3316 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
3317 struct section_offsets *delta)
3318 {
3319 /* There's nothing to relocate here. */
3320 }
3321
3322 static void
3323 dw2_expand_symtabs_for_function (struct objfile *objfile,
3324 const char *func_name)
3325 {
3326 struct mapped_index *index;
3327
3328 dw2_setup (objfile);
3329
3330 index = dwarf2_per_objfile->index_table;
3331
3332 /* index is NULL if OBJF_READNOW. */
3333 if (index)
3334 {
3335 struct dw2_symtab_iterator iter;
3336 struct dwarf2_per_cu_data *per_cu;
3337
3338 /* Note: It doesn't matter what we pass for block_index here. */
3339 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3340 func_name);
3341
3342 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3343 dw2_instantiate_symtab (per_cu);
3344 }
3345 }
3346
3347 static void
3348 dw2_expand_all_symtabs (struct objfile *objfile)
3349 {
3350 int i;
3351
3352 dw2_setup (objfile);
3353
3354 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3355 + dwarf2_per_objfile->n_type_units); ++i)
3356 {
3357 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3358
3359 dw2_instantiate_symtab (per_cu);
3360 }
3361 }
3362
3363 static void
3364 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3365 const char *fullname)
3366 {
3367 int i;
3368
3369 dw2_setup (objfile);
3370
3371 /* We don't need to consider type units here.
3372 This is only called for examining code, e.g. expand_line_sal.
3373 There can be an order of magnitude (or more) more type units
3374 than comp units, and we avoid them if we can. */
3375
3376 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3377 {
3378 int j;
3379 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3380 struct quick_file_names *file_data;
3381
3382 /* We only need to look at symtabs not already expanded. */
3383 if (per_cu->v.quick->symtab)
3384 continue;
3385
3386 file_data = dw2_get_file_names (objfile, per_cu);
3387 if (file_data == NULL)
3388 continue;
3389
3390 for (j = 0; j < file_data->num_file_names; ++j)
3391 {
3392 const char *this_fullname = file_data->file_names[j];
3393
3394 if (filename_cmp (this_fullname, fullname) == 0)
3395 {
3396 dw2_instantiate_symtab (per_cu);
3397 break;
3398 }
3399 }
3400 }
3401 }
3402
3403 /* A helper function for dw2_find_symbol_file that finds the primary
3404 file name for a given CU. This is a die_reader_func. */
3405
3406 static void
3407 dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
3408 gdb_byte *info_ptr,
3409 struct die_info *comp_unit_die,
3410 int has_children,
3411 void *data)
3412 {
3413 const char **result_ptr = data;
3414 struct dwarf2_cu *cu = reader->cu;
3415 struct attribute *attr;
3416
3417 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3418 if (attr == NULL)
3419 *result_ptr = NULL;
3420 else
3421 *result_ptr = DW_STRING (attr);
3422 }
3423
3424 static const char *
3425 dw2_find_symbol_file (struct objfile *objfile, const char *name)
3426 {
3427 struct dwarf2_per_cu_data *per_cu;
3428 offset_type *vec;
3429 const char *filename;
3430
3431 dw2_setup (objfile);
3432
3433 /* index_table is NULL if OBJF_READNOW. */
3434 if (!dwarf2_per_objfile->index_table)
3435 {
3436 struct symtab *s;
3437
3438 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
3439 {
3440 struct blockvector *bv = BLOCKVECTOR (s);
3441 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
3442 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
3443
3444 if (sym)
3445 {
3446 /* Only file extension of returned filename is recognized. */
3447 return SYMBOL_SYMTAB (sym)->filename;
3448 }
3449 }
3450 return NULL;
3451 }
3452
3453 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
3454 name, &vec))
3455 return NULL;
3456
3457 /* Note that this just looks at the very first one named NAME -- but
3458 actually we are looking for a function. find_main_filename
3459 should be rewritten so that it doesn't require a custom hook. It
3460 could just use the ordinary symbol tables. */
3461 /* vec[0] is the length, which must always be >0. */
3462 per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
3463
3464 if (per_cu->v.quick->symtab != NULL)
3465 {
3466 /* Only file extension of returned filename is recognized. */
3467 return per_cu->v.quick->symtab->filename;
3468 }
3469
3470 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
3471 dw2_get_primary_filename_reader, &filename);
3472
3473 /* Only file extension of returned filename is recognized. */
3474 return filename;
3475 }
3476
3477 static void
3478 dw2_map_matching_symbols (const char * name, domain_enum namespace,
3479 struct objfile *objfile, int global,
3480 int (*callback) (struct block *,
3481 struct symbol *, void *),
3482 void *data, symbol_compare_ftype *match,
3483 symbol_compare_ftype *ordered_compare)
3484 {
3485 /* Currently unimplemented; used for Ada. The function can be called if the
3486 current language is Ada for a non-Ada objfile using GNU index. As Ada
3487 does not look for non-Ada symbols this function should just return. */
3488 }
3489
3490 static void
3491 dw2_expand_symtabs_matching
3492 (struct objfile *objfile,
3493 int (*file_matcher) (const char *, void *, int basenames),
3494 int (*name_matcher) (const char *, void *),
3495 enum search_domain kind,
3496 void *data)
3497 {
3498 int i;
3499 offset_type iter;
3500 struct mapped_index *index;
3501
3502 dw2_setup (objfile);
3503
3504 /* index_table is NULL if OBJF_READNOW. */
3505 if (!dwarf2_per_objfile->index_table)
3506 return;
3507 index = dwarf2_per_objfile->index_table;
3508
3509 if (file_matcher != NULL)
3510 {
3511 struct cleanup *cleanup;
3512 htab_t visited_found, visited_not_found;
3513
3514 visited_found = htab_create_alloc (10,
3515 htab_hash_pointer, htab_eq_pointer,
3516 NULL, xcalloc, xfree);
3517 cleanup = make_cleanup_htab_delete (visited_found);
3518 visited_not_found = htab_create_alloc (10,
3519 htab_hash_pointer, htab_eq_pointer,
3520 NULL, xcalloc, xfree);
3521 make_cleanup_htab_delete (visited_not_found);
3522
3523 /* The rule is CUs specify all the files, including those used by
3524 any TU, so there's no need to scan TUs here. */
3525
3526 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3527 {
3528 int j;
3529 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3530 struct quick_file_names *file_data;
3531 void **slot;
3532
3533 per_cu->v.quick->mark = 0;
3534
3535 /* We only need to look at symtabs not already expanded. */
3536 if (per_cu->v.quick->symtab)
3537 continue;
3538
3539 file_data = dw2_get_file_names (objfile, per_cu);
3540 if (file_data == NULL)
3541 continue;
3542
3543 if (htab_find (visited_not_found, file_data) != NULL)
3544 continue;
3545 else if (htab_find (visited_found, file_data) != NULL)
3546 {
3547 per_cu->v.quick->mark = 1;
3548 continue;
3549 }
3550
3551 for (j = 0; j < file_data->num_file_names; ++j)
3552 {
3553 const char *this_real_name;
3554
3555 if (file_matcher (file_data->file_names[j], data, 0))
3556 {
3557 per_cu->v.quick->mark = 1;
3558 break;
3559 }
3560
3561 /* Before we invoke realpath, which can get expensive when many
3562 files are involved, do a quick comparison of the basenames. */
3563 if (!basenames_may_differ
3564 && !file_matcher (lbasename (file_data->file_names[j]),
3565 data, 1))
3566 continue;
3567
3568 this_real_name = dw2_get_real_path (objfile, file_data, j);
3569 if (file_matcher (this_real_name, data, 0))
3570 {
3571 per_cu->v.quick->mark = 1;
3572 break;
3573 }
3574 }
3575
3576 slot = htab_find_slot (per_cu->v.quick->mark
3577 ? visited_found
3578 : visited_not_found,
3579 file_data, INSERT);
3580 *slot = file_data;
3581 }
3582
3583 do_cleanups (cleanup);
3584 }
3585
3586 for (iter = 0; iter < index->symbol_table_slots; ++iter)
3587 {
3588 offset_type idx = 2 * iter;
3589 const char *name;
3590 offset_type *vec, vec_len, vec_idx;
3591
3592 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
3593 continue;
3594
3595 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
3596
3597 if (! (*name_matcher) (name, data))
3598 continue;
3599
3600 /* The name was matched, now expand corresponding CUs that were
3601 marked. */
3602 vec = (offset_type *) (index->constant_pool
3603 + MAYBE_SWAP (index->symbol_table[idx + 1]));
3604 vec_len = MAYBE_SWAP (vec[0]);
3605 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3606 {
3607 struct dwarf2_per_cu_data *per_cu;
3608 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3609 gdb_index_symbol_kind symbol_kind =
3610 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3611 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3612
3613 /* Don't crash on bad data. */
3614 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3615 + dwarf2_per_objfile->n_type_units))
3616 continue;
3617
3618 /* Only check the symbol's kind if it has one.
3619 Indices prior to version 7 don't record it. */
3620 if (index->version >= 7)
3621 {
3622 switch (kind)
3623 {
3624 case VARIABLES_DOMAIN:
3625 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3626 continue;
3627 break;
3628 case FUNCTIONS_DOMAIN:
3629 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3630 continue;
3631 break;
3632 case TYPES_DOMAIN:
3633 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3634 continue;
3635 break;
3636 default:
3637 break;
3638 }
3639 }
3640
3641 per_cu = dw2_get_cu (cu_index);
3642 if (file_matcher == NULL || per_cu->v.quick->mark)
3643 dw2_instantiate_symtab (per_cu);
3644 }
3645 }
3646 }
3647
3648 /* A helper for dw2_find_pc_sect_symtab which finds the most specific
3649 symtab. */
3650
3651 static struct symtab *
3652 recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3653 {
3654 int i;
3655
3656 if (BLOCKVECTOR (symtab) != NULL
3657 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3658 return symtab;
3659
3660 if (symtab->includes == NULL)
3661 return NULL;
3662
3663 for (i = 0; symtab->includes[i]; ++i)
3664 {
3665 struct symtab *s = symtab->includes[i];
3666
3667 s = recursively_find_pc_sect_symtab (s, pc);
3668 if (s != NULL)
3669 return s;
3670 }
3671
3672 return NULL;
3673 }
3674
3675 static struct symtab *
3676 dw2_find_pc_sect_symtab (struct objfile *objfile,
3677 struct minimal_symbol *msymbol,
3678 CORE_ADDR pc,
3679 struct obj_section *section,
3680 int warn_if_readin)
3681 {
3682 struct dwarf2_per_cu_data *data;
3683 struct symtab *result;
3684
3685 dw2_setup (objfile);
3686
3687 if (!objfile->psymtabs_addrmap)
3688 return NULL;
3689
3690 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3691 if (!data)
3692 return NULL;
3693
3694 if (warn_if_readin && data->v.quick->symtab)
3695 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
3696 paddress (get_objfile_arch (objfile), pc));
3697
3698 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3699 gdb_assert (result != NULL);
3700 return result;
3701 }
3702
3703 static void
3704 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
3705 void *data, int need_fullname)
3706 {
3707 int i;
3708 struct cleanup *cleanup;
3709 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3710 NULL, xcalloc, xfree);
3711
3712 cleanup = make_cleanup_htab_delete (visited);
3713 dw2_setup (objfile);
3714
3715 /* The rule is CUs specify all the files, including those used by
3716 any TU, so there's no need to scan TUs here.
3717 We can ignore file names coming from already-expanded CUs. */
3718
3719 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3720 {
3721 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3722
3723 if (per_cu->v.quick->symtab)
3724 {
3725 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3726 INSERT);
3727
3728 *slot = per_cu->v.quick->file_names;
3729 }
3730 }
3731
3732 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3733 {
3734 int j;
3735 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3736 struct quick_file_names *file_data;
3737 void **slot;
3738
3739 /* We only need to look at symtabs not already expanded. */
3740 if (per_cu->v.quick->symtab)
3741 continue;
3742
3743 file_data = dw2_get_file_names (objfile, per_cu);
3744 if (file_data == NULL)
3745 continue;
3746
3747 slot = htab_find_slot (visited, file_data, INSERT);
3748 if (*slot)
3749 {
3750 /* Already visited. */
3751 continue;
3752 }
3753 *slot = file_data;
3754
3755 for (j = 0; j < file_data->num_file_names; ++j)
3756 {
3757 const char *this_real_name;
3758
3759 if (need_fullname)
3760 this_real_name = dw2_get_real_path (objfile, file_data, j);
3761 else
3762 this_real_name = NULL;
3763 (*fun) (file_data->file_names[j], this_real_name, data);
3764 }
3765 }
3766
3767 do_cleanups (cleanup);
3768 }
3769
3770 static int
3771 dw2_has_symbols (struct objfile *objfile)
3772 {
3773 return 1;
3774 }
3775
3776 const struct quick_symbol_functions dwarf2_gdb_index_functions =
3777 {
3778 dw2_has_symbols,
3779 dw2_find_last_source_symtab,
3780 dw2_forget_cached_source_info,
3781 dw2_map_symtabs_matching_filename,
3782 dw2_lookup_symbol,
3783 dw2_print_stats,
3784 dw2_dump,
3785 dw2_relocate,
3786 dw2_expand_symtabs_for_function,
3787 dw2_expand_all_symtabs,
3788 dw2_expand_symtabs_with_fullname,
3789 dw2_find_symbol_file,
3790 dw2_map_matching_symbols,
3791 dw2_expand_symtabs_matching,
3792 dw2_find_pc_sect_symtab,
3793 dw2_map_symbol_filenames
3794 };
3795
3796 /* Initialize for reading DWARF for this objfile. Return 0 if this
3797 file will use psymtabs, or 1 if using the GNU index. */
3798
3799 int
3800 dwarf2_initialize_objfile (struct objfile *objfile)
3801 {
3802 /* If we're about to read full symbols, don't bother with the
3803 indices. In this case we also don't care if some other debug
3804 format is making psymtabs, because they are all about to be
3805 expanded anyway. */
3806 if ((objfile->flags & OBJF_READNOW))
3807 {
3808 int i;
3809
3810 dwarf2_per_objfile->using_index = 1;
3811 create_all_comp_units (objfile);
3812 create_all_type_units (objfile);
3813 dwarf2_per_objfile->quick_file_names_table =
3814 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3815
3816 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3817 + dwarf2_per_objfile->n_type_units); ++i)
3818 {
3819 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3820
3821 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3822 struct dwarf2_per_cu_quick_data);
3823 }
3824
3825 /* Return 1 so that gdb sees the "quick" functions. However,
3826 these functions will be no-ops because we will have expanded
3827 all symtabs. */
3828 return 1;
3829 }
3830
3831 if (dwarf2_read_index (objfile))
3832 return 1;
3833
3834 return 0;
3835 }
3836
3837 \f
3838
3839 /* Build a partial symbol table. */
3840
3841 void
3842 dwarf2_build_psymtabs (struct objfile *objfile)
3843 {
3844 volatile struct gdb_exception except;
3845
3846 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
3847 {
3848 init_psymbol_list (objfile, 1024);
3849 }
3850
3851 TRY_CATCH (except, RETURN_MASK_ERROR)
3852 {
3853 /* This isn't really ideal: all the data we allocate on the
3854 objfile's obstack is still uselessly kept around. However,
3855 freeing it seems unsafe. */
3856 struct cleanup *cleanups = make_cleanup_discard_psymtabs (objfile);
3857
3858 dwarf2_build_psymtabs_hard (objfile);
3859 discard_cleanups (cleanups);
3860 }
3861 if (except.reason < 0)
3862 exception_print (gdb_stderr, except);
3863 }
3864
3865 /* Return the total length of the CU described by HEADER. */
3866
3867 static unsigned int
3868 get_cu_length (const struct comp_unit_head *header)
3869 {
3870 return header->initial_length_size + header->length;
3871 }
3872
3873 /* Return TRUE if OFFSET is within CU_HEADER. */
3874
3875 static inline int
3876 offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
3877 {
3878 sect_offset bottom = { cu_header->offset.sect_off };
3879 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
3880
3881 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
3882 }
3883
3884 /* Find the base address of the compilation unit for range lists and
3885 location lists. It will normally be specified by DW_AT_low_pc.
3886 In DWARF-3 draft 4, the base address could be overridden by
3887 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3888 compilation units with discontinuous ranges. */
3889
3890 static void
3891 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3892 {
3893 struct attribute *attr;
3894
3895 cu->base_known = 0;
3896 cu->base_address = 0;
3897
3898 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3899 if (attr)
3900 {
3901 cu->base_address = DW_ADDR (attr);
3902 cu->base_known = 1;
3903 }
3904 else
3905 {
3906 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3907 if (attr)
3908 {
3909 cu->base_address = DW_ADDR (attr);
3910 cu->base_known = 1;
3911 }
3912 }
3913 }
3914
3915 /* Read in the comp unit header information from the debug_info at info_ptr.
3916 NOTE: This leaves members offset, first_die_offset to be filled in
3917 by the caller. */
3918
3919 static gdb_byte *
3920 read_comp_unit_head (struct comp_unit_head *cu_header,
3921 gdb_byte *info_ptr, bfd *abfd)
3922 {
3923 int signed_addr;
3924 unsigned int bytes_read;
3925
3926 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3927 cu_header->initial_length_size = bytes_read;
3928 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
3929 info_ptr += bytes_read;
3930 cu_header->version = read_2_bytes (abfd, info_ptr);
3931 info_ptr += 2;
3932 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3933 &bytes_read);
3934 info_ptr += bytes_read;
3935 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3936 info_ptr += 1;
3937 signed_addr = bfd_get_sign_extend_vma (abfd);
3938 if (signed_addr < 0)
3939 internal_error (__FILE__, __LINE__,
3940 _("read_comp_unit_head: dwarf from non elf file"));
3941 cu_header->signed_addr_p = signed_addr;
3942
3943 return info_ptr;
3944 }
3945
3946 /* Helper function that returns the proper abbrev section for
3947 THIS_CU. */
3948
3949 static struct dwarf2_section_info *
3950 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3951 {
3952 struct dwarf2_section_info *abbrev;
3953
3954 if (this_cu->is_dwz)
3955 abbrev = &dwarf2_get_dwz_file ()->abbrev;
3956 else
3957 abbrev = &dwarf2_per_objfile->abbrev;
3958
3959 return abbrev;
3960 }
3961
3962 /* Subroutine of read_and_check_comp_unit_head and
3963 read_and_check_type_unit_head to simplify them.
3964 Perform various error checking on the header. */
3965
3966 static void
3967 error_check_comp_unit_head (struct comp_unit_head *header,
3968 struct dwarf2_section_info *section,
3969 struct dwarf2_section_info *abbrev_section)
3970 {
3971 bfd *abfd = section->asection->owner;
3972 const char *filename = bfd_get_filename (abfd);
3973
3974 if (header->version != 2 && header->version != 3 && header->version != 4)
3975 error (_("Dwarf Error: wrong version in compilation unit header "
3976 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3977 filename);
3978
3979 if (header->abbrev_offset.sect_off
3980 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
3981 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3982 "(offset 0x%lx + 6) [in module %s]"),
3983 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
3984 filename);
3985
3986 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3987 avoid potential 32-bit overflow. */
3988 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
3989 > section->size)
3990 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3991 "(offset 0x%lx + 0) [in module %s]"),
3992 (long) header->length, (long) header->offset.sect_off,
3993 filename);
3994 }
3995
3996 /* Read in a CU/TU header and perform some basic error checking.
3997 The contents of the header are stored in HEADER.
3998 The result is a pointer to the start of the first DIE. */
3999
4000 static gdb_byte *
4001 read_and_check_comp_unit_head (struct comp_unit_head *header,
4002 struct dwarf2_section_info *section,
4003 struct dwarf2_section_info *abbrev_section,
4004 gdb_byte *info_ptr,
4005 int is_debug_types_section)
4006 {
4007 gdb_byte *beg_of_comp_unit = info_ptr;
4008 bfd *abfd = section->asection->owner;
4009
4010 header->offset.sect_off = beg_of_comp_unit - section->buffer;
4011
4012 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4013
4014 /* If we're reading a type unit, skip over the signature and
4015 type_offset fields. */
4016 if (is_debug_types_section)
4017 info_ptr += 8 /*signature*/ + header->offset_size;
4018
4019 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
4020
4021 error_check_comp_unit_head (header, section, abbrev_section);
4022
4023 return info_ptr;
4024 }
4025
4026 /* Read in the types comp unit header information from .debug_types entry at
4027 types_ptr. The result is a pointer to one past the end of the header. */
4028
4029 static gdb_byte *
4030 read_and_check_type_unit_head (struct comp_unit_head *header,
4031 struct dwarf2_section_info *section,
4032 struct dwarf2_section_info *abbrev_section,
4033 gdb_byte *info_ptr,
4034 ULONGEST *signature,
4035 cu_offset *type_offset_in_tu)
4036 {
4037 gdb_byte *beg_of_comp_unit = info_ptr;
4038 bfd *abfd = section->asection->owner;
4039
4040 header->offset.sect_off = beg_of_comp_unit - section->buffer;
4041
4042 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4043
4044 /* If we're reading a type unit, skip over the signature and
4045 type_offset fields. */
4046 if (signature != NULL)
4047 *signature = read_8_bytes (abfd, info_ptr);
4048 info_ptr += 8;
4049 if (type_offset_in_tu != NULL)
4050 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
4051 header->offset_size);
4052 info_ptr += header->offset_size;
4053
4054 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
4055
4056 error_check_comp_unit_head (header, section, abbrev_section);
4057
4058 return info_ptr;
4059 }
4060
4061 /* Fetch the abbreviation table offset from a comp or type unit header. */
4062
4063 static sect_offset
4064 read_abbrev_offset (struct dwarf2_section_info *section,
4065 sect_offset offset)
4066 {
4067 bfd *abfd = section->asection->owner;
4068 gdb_byte *info_ptr;
4069 unsigned int length, initial_length_size, offset_size;
4070 sect_offset abbrev_offset;
4071
4072 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4073 info_ptr = section->buffer + offset.sect_off;
4074 length = read_initial_length (abfd, info_ptr, &initial_length_size);
4075 offset_size = initial_length_size == 4 ? 4 : 8;
4076 info_ptr += initial_length_size + 2 /*version*/;
4077 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4078 return abbrev_offset;
4079 }
4080
4081 /* Allocate a new partial symtab for file named NAME and mark this new
4082 partial symtab as being an include of PST. */
4083
4084 static void
4085 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
4086 struct objfile *objfile)
4087 {
4088 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4089
4090 if (!IS_ABSOLUTE_PATH (subpst->filename))
4091 {
4092 /* It shares objfile->objfile_obstack. */
4093 subpst->dirname = pst->dirname;
4094 }
4095
4096 subpst->section_offsets = pst->section_offsets;
4097 subpst->textlow = 0;
4098 subpst->texthigh = 0;
4099
4100 subpst->dependencies = (struct partial_symtab **)
4101 obstack_alloc (&objfile->objfile_obstack,
4102 sizeof (struct partial_symtab *));
4103 subpst->dependencies[0] = pst;
4104 subpst->number_of_dependencies = 1;
4105
4106 subpst->globals_offset = 0;
4107 subpst->n_global_syms = 0;
4108 subpst->statics_offset = 0;
4109 subpst->n_static_syms = 0;
4110 subpst->symtab = NULL;
4111 subpst->read_symtab = pst->read_symtab;
4112 subpst->readin = 0;
4113
4114 /* No private part is necessary for include psymtabs. This property
4115 can be used to differentiate between such include psymtabs and
4116 the regular ones. */
4117 subpst->read_symtab_private = NULL;
4118 }
4119
4120 /* Read the Line Number Program data and extract the list of files
4121 included by the source file represented by PST. Build an include
4122 partial symtab for each of these included files. */
4123
4124 static void
4125 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
4126 struct die_info *die,
4127 struct partial_symtab *pst)
4128 {
4129 struct line_header *lh = NULL;
4130 struct attribute *attr;
4131
4132 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4133 if (attr)
4134 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
4135 if (lh == NULL)
4136 return; /* No linetable, so no includes. */
4137
4138 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
4139 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
4140
4141 free_line_header (lh);
4142 }
4143
4144 static hashval_t
4145 hash_signatured_type (const void *item)
4146 {
4147 const struct signatured_type *sig_type = item;
4148
4149 /* This drops the top 32 bits of the signature, but is ok for a hash. */
4150 return sig_type->signature;
4151 }
4152
4153 static int
4154 eq_signatured_type (const void *item_lhs, const void *item_rhs)
4155 {
4156 const struct signatured_type *lhs = item_lhs;
4157 const struct signatured_type *rhs = item_rhs;
4158
4159 return lhs->signature == rhs->signature;
4160 }
4161
4162 /* Allocate a hash table for signatured types. */
4163
4164 static htab_t
4165 allocate_signatured_type_table (struct objfile *objfile)
4166 {
4167 return htab_create_alloc_ex (41,
4168 hash_signatured_type,
4169 eq_signatured_type,
4170 NULL,
4171 &objfile->objfile_obstack,
4172 hashtab_obstack_allocate,
4173 dummy_obstack_deallocate);
4174 }
4175
4176 /* A helper function to add a signatured type CU to a table. */
4177
4178 static int
4179 add_signatured_type_cu_to_table (void **slot, void *datum)
4180 {
4181 struct signatured_type *sigt = *slot;
4182 struct signatured_type ***datap = datum;
4183
4184 **datap = sigt;
4185 ++*datap;
4186
4187 return 1;
4188 }
4189
4190 /* Create the hash table of all entries in the .debug_types section.
4191 DWO_FILE is a pointer to the DWO file for .debug_types.dwo,
4192 NULL otherwise.
4193 Note: This function processes DWO files only, not DWP files.
4194 The result is a pointer to the hash table or NULL if there are
4195 no types. */
4196
4197 static htab_t
4198 create_debug_types_hash_table (struct dwo_file *dwo_file,
4199 VEC (dwarf2_section_info_def) *types)
4200 {
4201 struct objfile *objfile = dwarf2_per_objfile->objfile;
4202 htab_t types_htab = NULL;
4203 int ix;
4204 struct dwarf2_section_info *section;
4205 struct dwarf2_section_info *abbrev_section;
4206
4207 if (VEC_empty (dwarf2_section_info_def, types))
4208 return NULL;
4209
4210 abbrev_section = (dwo_file != NULL
4211 ? &dwo_file->sections.abbrev
4212 : &dwarf2_per_objfile->abbrev);
4213
4214 if (dwarf2_read_debug)
4215 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4216 dwo_file ? ".dwo" : "",
4217 bfd_get_filename (abbrev_section->asection->owner));
4218
4219 for (ix = 0;
4220 VEC_iterate (dwarf2_section_info_def, types, ix, section);
4221 ++ix)
4222 {
4223 bfd *abfd;
4224 gdb_byte *info_ptr, *end_ptr;
4225 struct dwarf2_section_info *abbrev_section;
4226
4227 dwarf2_read_section (objfile, section);
4228 info_ptr = section->buffer;
4229
4230 if (info_ptr == NULL)
4231 continue;
4232
4233 /* We can't set abfd until now because the section may be empty or
4234 not present, in which case section->asection will be NULL. */
4235 abfd = section->asection->owner;
4236
4237 if (dwo_file)
4238 abbrev_section = &dwo_file->sections.abbrev;
4239 else
4240 abbrev_section = &dwarf2_per_objfile->abbrev;
4241
4242 if (types_htab == NULL)
4243 {
4244 if (dwo_file)
4245 types_htab = allocate_dwo_unit_table (objfile);
4246 else
4247 types_htab = allocate_signatured_type_table (objfile);
4248 }
4249
4250 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4251 because we don't need to read any dies: the signature is in the
4252 header. */
4253
4254 end_ptr = info_ptr + section->size;
4255 while (info_ptr < end_ptr)
4256 {
4257 sect_offset offset;
4258 cu_offset type_offset_in_tu;
4259 ULONGEST signature;
4260 struct signatured_type *sig_type;
4261 struct dwo_unit *dwo_tu;
4262 void **slot;
4263 gdb_byte *ptr = info_ptr;
4264 struct comp_unit_head header;
4265 unsigned int length;
4266
4267 offset.sect_off = ptr - section->buffer;
4268
4269 /* We need to read the type's signature in order to build the hash
4270 table, but we don't need anything else just yet. */
4271
4272 ptr = read_and_check_type_unit_head (&header, section,
4273 abbrev_section, ptr,
4274 &signature, &type_offset_in_tu);
4275
4276 length = get_cu_length (&header);
4277
4278 /* Skip dummy type units. */
4279 if (ptr >= info_ptr + length
4280 || peek_abbrev_code (abfd, ptr) == 0)
4281 {
4282 info_ptr += length;
4283 continue;
4284 }
4285
4286 if (dwo_file)
4287 {
4288 sig_type = NULL;
4289 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4290 struct dwo_unit);
4291 dwo_tu->dwo_file = dwo_file;
4292 dwo_tu->signature = signature;
4293 dwo_tu->type_offset_in_tu = type_offset_in_tu;
4294 dwo_tu->info_or_types_section = section;
4295 dwo_tu->offset = offset;
4296 dwo_tu->length = length;
4297 }
4298 else
4299 {
4300 /* N.B.: type_offset is not usable if this type uses a DWO file.
4301 The real type_offset is in the DWO file. */
4302 dwo_tu = NULL;
4303 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4304 struct signatured_type);
4305 sig_type->signature = signature;
4306 sig_type->type_offset_in_tu = type_offset_in_tu;
4307 sig_type->per_cu.objfile = objfile;
4308 sig_type->per_cu.is_debug_types = 1;
4309 sig_type->per_cu.info_or_types_section = section;
4310 sig_type->per_cu.offset = offset;
4311 sig_type->per_cu.length = length;
4312 }
4313
4314 slot = htab_find_slot (types_htab,
4315 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4316 INSERT);
4317 gdb_assert (slot != NULL);
4318 if (*slot != NULL)
4319 {
4320 sect_offset dup_offset;
4321
4322 if (dwo_file)
4323 {
4324 const struct dwo_unit *dup_tu = *slot;
4325
4326 dup_offset = dup_tu->offset;
4327 }
4328 else
4329 {
4330 const struct signatured_type *dup_tu = *slot;
4331
4332 dup_offset = dup_tu->per_cu.offset;
4333 }
4334
4335 complaint (&symfile_complaints,
4336 _("debug type entry at offset 0x%x is duplicate to the "
4337 "entry at offset 0x%x, signature 0x%s"),
4338 offset.sect_off, dup_offset.sect_off,
4339 phex (signature, sizeof (signature)));
4340 }
4341 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
4342
4343 if (dwarf2_read_debug)
4344 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
4345 offset.sect_off,
4346 phex (signature, sizeof (signature)));
4347
4348 info_ptr += length;
4349 }
4350 }
4351
4352 return types_htab;
4353 }
4354
4355 /* Create the hash table of all entries in the .debug_types section,
4356 and initialize all_type_units.
4357 The result is zero if there is an error (e.g. missing .debug_types section),
4358 otherwise non-zero. */
4359
4360 static int
4361 create_all_type_units (struct objfile *objfile)
4362 {
4363 htab_t types_htab;
4364 struct signatured_type **iter;
4365
4366 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4367 if (types_htab == NULL)
4368 {
4369 dwarf2_per_objfile->signatured_types = NULL;
4370 return 0;
4371 }
4372
4373 dwarf2_per_objfile->signatured_types = types_htab;
4374
4375 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4376 dwarf2_per_objfile->all_type_units
4377 = obstack_alloc (&objfile->objfile_obstack,
4378 dwarf2_per_objfile->n_type_units
4379 * sizeof (struct signatured_type *));
4380 iter = &dwarf2_per_objfile->all_type_units[0];
4381 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4382 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4383 == dwarf2_per_objfile->n_type_units);
4384
4385 return 1;
4386 }
4387
4388 /* Lookup a signature based type for DW_FORM_ref_sig8.
4389 Returns NULL if signature SIG is not present in the table. */
4390
4391 static struct signatured_type *
4392 lookup_signatured_type (ULONGEST sig)
4393 {
4394 struct signatured_type find_entry, *entry;
4395
4396 if (dwarf2_per_objfile->signatured_types == NULL)
4397 {
4398 complaint (&symfile_complaints,
4399 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
4400 return NULL;
4401 }
4402
4403 find_entry.signature = sig;
4404 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4405 return entry;
4406 }
4407 \f
4408 /* Low level DIE reading support. */
4409
4410 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4411
4412 static void
4413 init_cu_die_reader (struct die_reader_specs *reader,
4414 struct dwarf2_cu *cu,
4415 struct dwarf2_section_info *section,
4416 struct dwo_file *dwo_file)
4417 {
4418 gdb_assert (section->readin && section->buffer != NULL);
4419 reader->abfd = section->asection->owner;
4420 reader->cu = cu;
4421 reader->dwo_file = dwo_file;
4422 reader->die_section = section;
4423 reader->buffer = section->buffer;
4424 reader->buffer_end = section->buffer + section->size;
4425 }
4426
4427 /* Initialize a CU (or TU) and read its DIEs.
4428 If the CU defers to a DWO file, read the DWO file as well.
4429
4430 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4431 Otherwise the table specified in the comp unit header is read in and used.
4432 This is an optimization for when we already have the abbrev table.
4433
4434 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4435 Otherwise, a new CU is allocated with xmalloc.
4436
4437 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4438 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
4439
4440 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4441 linker) then DIE_READER_FUNC will not get called. */
4442
4443 static void
4444 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
4445 struct abbrev_table *abbrev_table,
4446 int use_existing_cu, int keep,
4447 die_reader_func_ftype *die_reader_func,
4448 void *data)
4449 {
4450 struct objfile *objfile = dwarf2_per_objfile->objfile;
4451 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4452 bfd *abfd = section->asection->owner;
4453 struct dwarf2_cu *cu;
4454 gdb_byte *begin_info_ptr, *info_ptr;
4455 struct die_reader_specs reader;
4456 struct die_info *comp_unit_die;
4457 int has_children;
4458 struct attribute *attr;
4459 struct cleanup *cleanups, *free_cu_cleanup = NULL;
4460 struct signatured_type *sig_type = NULL;
4461 struct dwarf2_section_info *abbrev_section;
4462 /* Non-zero if CU currently points to a DWO file and we need to
4463 reread it. When this happens we need to reread the skeleton die
4464 before we can reread the DWO file. */
4465 int rereading_dwo_cu = 0;
4466
4467 if (dwarf2_die_debug)
4468 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4469 this_cu->is_debug_types ? "type" : "comp",
4470 this_cu->offset.sect_off);
4471
4472 if (use_existing_cu)
4473 gdb_assert (keep);
4474
4475 cleanups = make_cleanup (null_cleanup, NULL);
4476
4477 /* This is cheap if the section is already read in. */
4478 dwarf2_read_section (objfile, section);
4479
4480 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4481
4482 abbrev_section = get_abbrev_section_for_cu (this_cu);
4483
4484 if (use_existing_cu && this_cu->cu != NULL)
4485 {
4486 cu = this_cu->cu;
4487
4488 /* If this CU is from a DWO file we need to start over, we need to
4489 refetch the attributes from the skeleton CU.
4490 This could be optimized by retrieving those attributes from when we
4491 were here the first time: the previous comp_unit_die was stored in
4492 comp_unit_obstack. But there's no data yet that we need this
4493 optimization. */
4494 if (cu->dwo_unit != NULL)
4495 rereading_dwo_cu = 1;
4496 }
4497 else
4498 {
4499 /* If !use_existing_cu, this_cu->cu must be NULL. */
4500 gdb_assert (this_cu->cu == NULL);
4501
4502 cu = xmalloc (sizeof (*cu));
4503 init_one_comp_unit (cu, this_cu);
4504
4505 /* If an error occurs while loading, release our storage. */
4506 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
4507 }
4508
4509 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
4510 {
4511 /* We already have the header, there's no need to read it in again. */
4512 info_ptr += cu->header.first_die_offset.cu_off;
4513 }
4514 else
4515 {
4516 if (this_cu->is_debug_types)
4517 {
4518 ULONGEST signature;
4519 cu_offset type_offset_in_tu;
4520
4521 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4522 abbrev_section, info_ptr,
4523 &signature,
4524 &type_offset_in_tu);
4525
4526 /* Since per_cu is the first member of struct signatured_type,
4527 we can go from a pointer to one to a pointer to the other. */
4528 sig_type = (struct signatured_type *) this_cu;
4529 gdb_assert (sig_type->signature == signature);
4530 gdb_assert (sig_type->type_offset_in_tu.cu_off
4531 == type_offset_in_tu.cu_off);
4532 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4533
4534 /* LENGTH has not been set yet for type units if we're
4535 using .gdb_index. */
4536 this_cu->length = get_cu_length (&cu->header);
4537
4538 /* Establish the type offset that can be used to lookup the type. */
4539 sig_type->type_offset_in_section.sect_off =
4540 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
4541 }
4542 else
4543 {
4544 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4545 abbrev_section,
4546 info_ptr, 0);
4547
4548 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4549 gdb_assert (this_cu->length == get_cu_length (&cu->header));
4550 }
4551 }
4552
4553 /* Skip dummy compilation units. */
4554 if (info_ptr >= begin_info_ptr + this_cu->length
4555 || peek_abbrev_code (abfd, info_ptr) == 0)
4556 {
4557 do_cleanups (cleanups);
4558 return;
4559 }
4560
4561 /* If we don't have them yet, read the abbrevs for this compilation unit.
4562 And if we need to read them now, make sure they're freed when we're
4563 done. Note that it's important that if the CU had an abbrev table
4564 on entry we don't free it when we're done: Somewhere up the call stack
4565 it may be in use. */
4566 if (abbrev_table != NULL)
4567 {
4568 gdb_assert (cu->abbrev_table == NULL);
4569 gdb_assert (cu->header.abbrev_offset.sect_off
4570 == abbrev_table->offset.sect_off);
4571 cu->abbrev_table = abbrev_table;
4572 }
4573 else if (cu->abbrev_table == NULL)
4574 {
4575 dwarf2_read_abbrevs (cu, abbrev_section);
4576 make_cleanup (dwarf2_free_abbrev_table, cu);
4577 }
4578 else if (rereading_dwo_cu)
4579 {
4580 dwarf2_free_abbrev_table (cu);
4581 dwarf2_read_abbrevs (cu, abbrev_section);
4582 }
4583
4584 /* Read the top level CU/TU die. */
4585 init_cu_die_reader (&reader, cu, section, NULL);
4586 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4587
4588 /* If we have a DWO stub, process it and then read in the DWO file.
4589 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
4590 a DWO CU, that this test will fail. */
4591 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4592 if (attr)
4593 {
4594 const char *dwo_name = DW_STRING (attr);
4595 const char *comp_dir_string;
4596 struct dwo_unit *dwo_unit;
4597 ULONGEST signature; /* Or dwo_id. */
4598 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
4599 int i,num_extra_attrs;
4600 struct dwarf2_section_info *dwo_abbrev_section;
4601
4602 if (has_children)
4603 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
4604 " has children (offset 0x%x) [in module %s]"),
4605 this_cu->offset.sect_off, bfd_get_filename (abfd));
4606
4607 /* These attributes aren't processed until later:
4608 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4609 However, the attribute is found in the stub which we won't have later.
4610 In order to not impose this complication on the rest of the code,
4611 we read them here and copy them to the DWO CU/TU die. */
4612
4613 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4614 DWO file. */
4615 stmt_list = NULL;
4616 if (! this_cu->is_debug_types)
4617 stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
4618 low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
4619 high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
4620 ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
4621 comp_dir = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4622
4623 /* There should be a DW_AT_addr_base attribute here (if needed).
4624 We need the value before we can process DW_FORM_GNU_addr_index. */
4625 cu->addr_base = 0;
4626 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
4627 if (attr)
4628 cu->addr_base = DW_UNSND (attr);
4629
4630 /* There should be a DW_AT_ranges_base attribute here (if needed).
4631 We need the value before we can process DW_AT_ranges. */
4632 cu->ranges_base = 0;
4633 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_ranges_base, cu);
4634 if (attr)
4635 cu->ranges_base = DW_UNSND (attr);
4636
4637 if (this_cu->is_debug_types)
4638 {
4639 gdb_assert (sig_type != NULL);
4640 signature = sig_type->signature;
4641 }
4642 else
4643 {
4644 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4645 if (! attr)
4646 error (_("Dwarf Error: missing dwo_id [in module %s]"),
4647 dwo_name);
4648 signature = DW_UNSND (attr);
4649 }
4650
4651 /* We may need the comp_dir in order to find the DWO file. */
4652 comp_dir_string = NULL;
4653 if (comp_dir)
4654 comp_dir_string = DW_STRING (comp_dir);
4655
4656 if (this_cu->is_debug_types)
4657 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir_string);
4658 else
4659 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir_string,
4660 signature);
4661
4662 if (dwo_unit == NULL)
4663 {
4664 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
4665 " with ID %s [in module %s]"),
4666 this_cu->offset.sect_off,
4667 phex (signature, sizeof (signature)),
4668 objfile->name);
4669 }
4670
4671 /* Set up for reading the DWO CU/TU. */
4672 cu->dwo_unit = dwo_unit;
4673 section = dwo_unit->info_or_types_section;
4674 dwarf2_read_section (objfile, section);
4675 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4676 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
4677 init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
4678
4679 if (this_cu->is_debug_types)
4680 {
4681 ULONGEST signature;
4682 cu_offset type_offset_in_tu;
4683
4684 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4685 dwo_abbrev_section,
4686 info_ptr,
4687 &signature,
4688 &type_offset_in_tu);
4689 gdb_assert (sig_type->signature == signature);
4690 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4691 /* For DWOs coming from DWP files, we don't know the CU length
4692 nor the type's offset in the TU until now. */
4693 dwo_unit->length = get_cu_length (&cu->header);
4694 dwo_unit->type_offset_in_tu = type_offset_in_tu;
4695
4696 /* Establish the type offset that can be used to lookup the type.
4697 For DWO files, we don't know it until now. */
4698 sig_type->type_offset_in_section.sect_off =
4699 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4700 }
4701 else
4702 {
4703 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4704 dwo_abbrev_section,
4705 info_ptr, 0);
4706 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4707 /* For DWOs coming from DWP files, we don't know the CU length
4708 until now. */
4709 dwo_unit->length = get_cu_length (&cu->header);
4710 }
4711
4712 /* Discard the original CU's abbrev table, and read the DWO's. */
4713 if (abbrev_table == NULL)
4714 {
4715 dwarf2_free_abbrev_table (cu);
4716 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4717 }
4718 else
4719 {
4720 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4721 make_cleanup (dwarf2_free_abbrev_table, cu);
4722 }
4723
4724 /* Read in the die, but leave space to copy over the attributes
4725 from the stub. This has the benefit of simplifying the rest of
4726 the code - all the real work is done here. */
4727 num_extra_attrs = ((stmt_list != NULL)
4728 + (low_pc != NULL)
4729 + (high_pc != NULL)
4730 + (ranges != NULL)
4731 + (comp_dir != NULL));
4732 info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
4733 &has_children, num_extra_attrs);
4734
4735 /* Copy over the attributes from the stub to the DWO die. */
4736 i = comp_unit_die->num_attrs;
4737 if (stmt_list != NULL)
4738 comp_unit_die->attrs[i++] = *stmt_list;
4739 if (low_pc != NULL)
4740 comp_unit_die->attrs[i++] = *low_pc;
4741 if (high_pc != NULL)
4742 comp_unit_die->attrs[i++] = *high_pc;
4743 if (ranges != NULL)
4744 comp_unit_die->attrs[i++] = *ranges;
4745 if (comp_dir != NULL)
4746 comp_unit_die->attrs[i++] = *comp_dir;
4747 comp_unit_die->num_attrs += num_extra_attrs;
4748
4749 /* Skip dummy compilation units. */
4750 if (info_ptr >= begin_info_ptr + dwo_unit->length
4751 || peek_abbrev_code (abfd, info_ptr) == 0)
4752 {
4753 do_cleanups (cleanups);
4754 return;
4755 }
4756 }
4757
4758 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4759
4760 if (free_cu_cleanup != NULL)
4761 {
4762 if (keep)
4763 {
4764 /* We've successfully allocated this compilation unit. Let our
4765 caller clean it up when finished with it. */
4766 discard_cleanups (free_cu_cleanup);
4767
4768 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4769 So we have to manually free the abbrev table. */
4770 dwarf2_free_abbrev_table (cu);
4771
4772 /* Link this CU into read_in_chain. */
4773 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4774 dwarf2_per_objfile->read_in_chain = this_cu;
4775 }
4776 else
4777 do_cleanups (free_cu_cleanup);
4778 }
4779
4780 do_cleanups (cleanups);
4781 }
4782
4783 /* Read CU/TU THIS_CU in section SECTION,
4784 but do not follow DW_AT_GNU_dwo_name if present.
4785 DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
4786 to have already done the lookup to find the DWO/DWP file).
4787
4788 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
4789 THIS_CU->is_debug_types, but nothing else.
4790
4791 We fill in THIS_CU->length.
4792
4793 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4794 linker) then DIE_READER_FUNC will not get called.
4795
4796 THIS_CU->cu is always freed when done.
4797 This is done in order to not leave THIS_CU->cu in a state where we have
4798 to care whether it refers to the "main" CU or the DWO CU. */
4799
4800 static void
4801 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4802 struct dwarf2_section_info *abbrev_section,
4803 struct dwo_file *dwo_file,
4804 die_reader_func_ftype *die_reader_func,
4805 void *data)
4806 {
4807 struct objfile *objfile = dwarf2_per_objfile->objfile;
4808 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4809 bfd *abfd = section->asection->owner;
4810 struct dwarf2_cu cu;
4811 gdb_byte *begin_info_ptr, *info_ptr;
4812 struct die_reader_specs reader;
4813 struct cleanup *cleanups;
4814 struct die_info *comp_unit_die;
4815 int has_children;
4816
4817 if (dwarf2_die_debug)
4818 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4819 this_cu->is_debug_types ? "type" : "comp",
4820 this_cu->offset.sect_off);
4821
4822 gdb_assert (this_cu->cu == NULL);
4823
4824 /* This is cheap if the section is already read in. */
4825 dwarf2_read_section (objfile, section);
4826
4827 init_one_comp_unit (&cu, this_cu);
4828
4829 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4830
4831 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4832 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
4833 abbrev_section, info_ptr,
4834 this_cu->is_debug_types);
4835
4836 this_cu->length = get_cu_length (&cu.header);
4837
4838 /* Skip dummy compilation units. */
4839 if (info_ptr >= begin_info_ptr + this_cu->length
4840 || peek_abbrev_code (abfd, info_ptr) == 0)
4841 {
4842 do_cleanups (cleanups);
4843 return;
4844 }
4845
4846 dwarf2_read_abbrevs (&cu, abbrev_section);
4847 make_cleanup (dwarf2_free_abbrev_table, &cu);
4848
4849 init_cu_die_reader (&reader, &cu, section, dwo_file);
4850 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4851
4852 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4853
4854 do_cleanups (cleanups);
4855 }
4856
4857 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4858 does not lookup the specified DWO file.
4859 This cannot be used to read DWO files.
4860
4861 THIS_CU->cu is always freed when done.
4862 This is done in order to not leave THIS_CU->cu in a state where we have
4863 to care whether it refers to the "main" CU or the DWO CU.
4864 We can revisit this if the data shows there's a performance issue. */
4865
4866 static void
4867 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4868 die_reader_func_ftype *die_reader_func,
4869 void *data)
4870 {
4871 init_cutu_and_read_dies_no_follow (this_cu,
4872 get_abbrev_section_for_cu (this_cu),
4873 NULL,
4874 die_reader_func, data);
4875 }
4876
4877 /* Create a psymtab named NAME and assign it to PER_CU.
4878
4879 The caller must fill in the following details:
4880 dirname, textlow, texthigh. */
4881
4882 static struct partial_symtab *
4883 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
4884 {
4885 struct objfile *objfile = per_cu->objfile;
4886 struct partial_symtab *pst;
4887
4888 pst = start_psymtab_common (objfile, objfile->section_offsets,
4889 name, 0,
4890 objfile->global_psymbols.next,
4891 objfile->static_psymbols.next);
4892
4893 pst->psymtabs_addrmap_supported = 1;
4894
4895 /* This is the glue that links PST into GDB's symbol API. */
4896 pst->read_symtab_private = per_cu;
4897 pst->read_symtab = dwarf2_read_symtab;
4898 per_cu->v.psymtab = pst;
4899
4900 return pst;
4901 }
4902
4903 /* die_reader_func for process_psymtab_comp_unit. */
4904
4905 static void
4906 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
4907 gdb_byte *info_ptr,
4908 struct die_info *comp_unit_die,
4909 int has_children,
4910 void *data)
4911 {
4912 struct dwarf2_cu *cu = reader->cu;
4913 struct objfile *objfile = cu->objfile;
4914 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
4915 struct attribute *attr;
4916 CORE_ADDR baseaddr;
4917 CORE_ADDR best_lowpc = 0, best_highpc = 0;
4918 struct partial_symtab *pst;
4919 int has_pc_info;
4920 const char *filename;
4921 int *want_partial_unit_ptr = data;
4922
4923 if (comp_unit_die->tag == DW_TAG_partial_unit
4924 && (want_partial_unit_ptr == NULL
4925 || !*want_partial_unit_ptr))
4926 return;
4927
4928 gdb_assert (! per_cu->is_debug_types);
4929
4930 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
4931
4932 cu->list_in_scope = &file_symbols;
4933
4934 /* Allocate a new partial symbol table structure. */
4935 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
4936 if (attr == NULL || !DW_STRING (attr))
4937 filename = "";
4938 else
4939 filename = DW_STRING (attr);
4940
4941 pst = create_partial_symtab (per_cu, filename);
4942
4943 /* This must be done before calling dwarf2_build_include_psymtabs. */
4944 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4945 if (attr != NULL)
4946 pst->dirname = DW_STRING (attr);
4947
4948 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4949
4950 dwarf2_find_base_address (comp_unit_die, cu);
4951
4952 /* Possibly set the default values of LOWPC and HIGHPC from
4953 `DW_AT_ranges'. */
4954 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
4955 &best_highpc, cu, pst);
4956 if (has_pc_info == 1 && best_lowpc < best_highpc)
4957 /* Store the contiguous range if it is not empty; it can be empty for
4958 CUs with no code. */
4959 addrmap_set_empty (objfile->psymtabs_addrmap,
4960 best_lowpc + baseaddr,
4961 best_highpc + baseaddr - 1, pst);
4962
4963 /* Check if comp unit has_children.
4964 If so, read the rest of the partial symbols from this comp unit.
4965 If not, there's no more debug_info for this comp unit. */
4966 if (has_children)
4967 {
4968 struct partial_die_info *first_die;
4969 CORE_ADDR lowpc, highpc;
4970
4971 lowpc = ((CORE_ADDR) -1);
4972 highpc = ((CORE_ADDR) 0);
4973
4974 first_die = load_partial_dies (reader, info_ptr, 1);
4975
4976 scan_partial_symbols (first_die, &lowpc, &highpc,
4977 ! has_pc_info, cu);
4978
4979 /* If we didn't find a lowpc, set it to highpc to avoid
4980 complaints from `maint check'. */
4981 if (lowpc == ((CORE_ADDR) -1))
4982 lowpc = highpc;
4983
4984 /* If the compilation unit didn't have an explicit address range,
4985 then use the information extracted from its child dies. */
4986 if (! has_pc_info)
4987 {
4988 best_lowpc = lowpc;
4989 best_highpc = highpc;
4990 }
4991 }
4992 pst->textlow = best_lowpc + baseaddr;
4993 pst->texthigh = best_highpc + baseaddr;
4994
4995 pst->n_global_syms = objfile->global_psymbols.next -
4996 (objfile->global_psymbols.list + pst->globals_offset);
4997 pst->n_static_syms = objfile->static_psymbols.next -
4998 (objfile->static_psymbols.list + pst->statics_offset);
4999 sort_pst_symbols (objfile, pst);
5000
5001 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
5002 {
5003 int i;
5004 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5005 struct dwarf2_per_cu_data *iter;
5006
5007 /* Fill in 'dependencies' here; we fill in 'users' in a
5008 post-pass. */
5009 pst->number_of_dependencies = len;
5010 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5011 len * sizeof (struct symtab *));
5012 for (i = 0;
5013 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
5014 i, iter);
5015 ++i)
5016 pst->dependencies[i] = iter->v.psymtab;
5017
5018 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5019 }
5020
5021 /* Get the list of files included in the current compilation unit,
5022 and build a psymtab for each of them. */
5023 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
5024
5025 if (dwarf2_read_debug)
5026 {
5027 struct gdbarch *gdbarch = get_objfile_arch (objfile);
5028
5029 fprintf_unfiltered (gdb_stdlog,
5030 "Psymtab for %s unit @0x%x: %s - %s"
5031 ", %d global, %d static syms\n",
5032 per_cu->is_debug_types ? "type" : "comp",
5033 per_cu->offset.sect_off,
5034 paddress (gdbarch, pst->textlow),
5035 paddress (gdbarch, pst->texthigh),
5036 pst->n_global_syms, pst->n_static_syms);
5037 }
5038 }
5039
5040 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5041 Process compilation unit THIS_CU for a psymtab. */
5042
5043 static void
5044 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
5045 int want_partial_unit)
5046 {
5047 /* If this compilation unit was already read in, free the
5048 cached copy in order to read it in again. This is
5049 necessary because we skipped some symbols when we first
5050 read in the compilation unit (see load_partial_dies).
5051 This problem could be avoided, but the benefit is unclear. */
5052 if (this_cu->cu != NULL)
5053 free_one_cached_comp_unit (this_cu);
5054
5055 gdb_assert (! this_cu->is_debug_types);
5056 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
5057 process_psymtab_comp_unit_reader,
5058 &want_partial_unit);
5059
5060 /* Age out any secondary CUs. */
5061 age_cached_comp_units ();
5062 }
5063
5064 static hashval_t
5065 hash_type_unit_group (const void *item)
5066 {
5067 const struct type_unit_group *tu_group = item;
5068
5069 return hash_stmt_list_entry (&tu_group->hash);
5070 }
5071
5072 static int
5073 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
5074 {
5075 const struct type_unit_group *lhs = item_lhs;
5076 const struct type_unit_group *rhs = item_rhs;
5077
5078 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
5079 }
5080
5081 /* Allocate a hash table for type unit groups. */
5082
5083 static htab_t
5084 allocate_type_unit_groups_table (void)
5085 {
5086 return htab_create_alloc_ex (3,
5087 hash_type_unit_group,
5088 eq_type_unit_group,
5089 NULL,
5090 &dwarf2_per_objfile->objfile->objfile_obstack,
5091 hashtab_obstack_allocate,
5092 dummy_obstack_deallocate);
5093 }
5094
5095 /* Type units that don't have DW_AT_stmt_list are grouped into their own
5096 partial symtabs. We combine several TUs per psymtab to not let the size
5097 of any one psymtab grow too big. */
5098 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5099 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
5100
5101 /* Helper routine for get_type_unit_group.
5102 Create the type_unit_group object used to hold one or more TUs. */
5103
5104 static struct type_unit_group *
5105 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
5106 {
5107 struct objfile *objfile = dwarf2_per_objfile->objfile;
5108 struct dwarf2_per_cu_data *per_cu;
5109 struct type_unit_group *tu_group;
5110
5111 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5112 struct type_unit_group);
5113 per_cu = &tu_group->per_cu;
5114 per_cu->objfile = objfile;
5115 per_cu->is_debug_types = 1;
5116 per_cu->type_unit_group = tu_group;
5117
5118 if (dwarf2_per_objfile->using_index)
5119 {
5120 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5121 struct dwarf2_per_cu_quick_data);
5122 tu_group->t.first_tu = cu->per_cu;
5123 }
5124 else
5125 {
5126 unsigned int line_offset = line_offset_struct.sect_off;
5127 struct partial_symtab *pst;
5128 char *name;
5129
5130 /* Give the symtab a useful name for debug purposes. */
5131 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5132 name = xstrprintf ("<type_units_%d>",
5133 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5134 else
5135 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5136
5137 pst = create_partial_symtab (per_cu, name);
5138 pst->anonymous = 1;
5139
5140 xfree (name);
5141 }
5142
5143 tu_group->hash.dwo_unit = cu->dwo_unit;
5144 tu_group->hash.line_offset = line_offset_struct;
5145
5146 return tu_group;
5147 }
5148
5149 /* Look up the type_unit_group for type unit CU, and create it if necessary.
5150 STMT_LIST is a DW_AT_stmt_list attribute. */
5151
5152 static struct type_unit_group *
5153 get_type_unit_group (struct dwarf2_cu *cu, struct attribute *stmt_list)
5154 {
5155 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5156 struct type_unit_group *tu_group;
5157 void **slot;
5158 unsigned int line_offset;
5159 struct type_unit_group type_unit_group_for_lookup;
5160
5161 if (dwarf2_per_objfile->type_unit_groups == NULL)
5162 {
5163 dwarf2_per_objfile->type_unit_groups =
5164 allocate_type_unit_groups_table ();
5165 }
5166
5167 /* Do we need to create a new group, or can we use an existing one? */
5168
5169 if (stmt_list)
5170 {
5171 line_offset = DW_UNSND (stmt_list);
5172 ++tu_stats->nr_symtab_sharers;
5173 }
5174 else
5175 {
5176 /* Ugh, no stmt_list. Rare, but we have to handle it.
5177 We can do various things here like create one group per TU or
5178 spread them over multiple groups to split up the expansion work.
5179 To avoid worst case scenarios (too many groups or too large groups)
5180 we, umm, group them in bunches. */
5181 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5182 | (tu_stats->nr_stmt_less_type_units
5183 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5184 ++tu_stats->nr_stmt_less_type_units;
5185 }
5186
5187 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5188 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
5189 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5190 &type_unit_group_for_lookup, INSERT);
5191 if (*slot != NULL)
5192 {
5193 tu_group = *slot;
5194 gdb_assert (tu_group != NULL);
5195 }
5196 else
5197 {
5198 sect_offset line_offset_struct;
5199
5200 line_offset_struct.sect_off = line_offset;
5201 tu_group = create_type_unit_group (cu, line_offset_struct);
5202 *slot = tu_group;
5203 ++tu_stats->nr_symtabs;
5204 }
5205
5206 return tu_group;
5207 }
5208
5209 /* Struct used to sort TUs by their abbreviation table offset. */
5210
5211 struct tu_abbrev_offset
5212 {
5213 struct signatured_type *sig_type;
5214 sect_offset abbrev_offset;
5215 };
5216
5217 /* Helper routine for build_type_unit_groups, passed to qsort. */
5218
5219 static int
5220 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5221 {
5222 const struct tu_abbrev_offset * const *a = ap;
5223 const struct tu_abbrev_offset * const *b = bp;
5224 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5225 unsigned int boff = (*b)->abbrev_offset.sect_off;
5226
5227 return (aoff > boff) - (aoff < boff);
5228 }
5229
5230 /* A helper function to add a type_unit_group to a table. */
5231
5232 static int
5233 add_type_unit_group_to_table (void **slot, void *datum)
5234 {
5235 struct type_unit_group *tu_group = *slot;
5236 struct type_unit_group ***datap = datum;
5237
5238 **datap = tu_group;
5239 ++*datap;
5240
5241 return 1;
5242 }
5243
5244 /* Efficiently read all the type units, calling init_cutu_and_read_dies on
5245 each one passing FUNC,DATA.
5246
5247 The efficiency is because we sort TUs by the abbrev table they use and
5248 only read each abbrev table once. In one program there are 200K TUs
5249 sharing 8K abbrev tables.
5250
5251 The main purpose of this function is to support building the
5252 dwarf2_per_objfile->type_unit_groups table.
5253 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5254 can collapse the search space by grouping them by stmt_list.
5255 The savings can be significant, in the same program from above the 200K TUs
5256 share 8K stmt_list tables.
5257
5258 FUNC is expected to call get_type_unit_group, which will create the
5259 struct type_unit_group if necessary and add it to
5260 dwarf2_per_objfile->type_unit_groups. */
5261
5262 static void
5263 build_type_unit_groups (die_reader_func_ftype *func, void *data)
5264 {
5265 struct objfile *objfile = dwarf2_per_objfile->objfile;
5266 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5267 struct cleanup *cleanups;
5268 struct abbrev_table *abbrev_table;
5269 sect_offset abbrev_offset;
5270 struct tu_abbrev_offset *sorted_by_abbrev;
5271 struct type_unit_group **iter;
5272 int i;
5273
5274 /* It's up to the caller to not call us multiple times. */
5275 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5276
5277 if (dwarf2_per_objfile->n_type_units == 0)
5278 return;
5279
5280 /* TUs typically share abbrev tables, and there can be way more TUs than
5281 abbrev tables. Sort by abbrev table to reduce the number of times we
5282 read each abbrev table in.
5283 Alternatives are to punt or to maintain a cache of abbrev tables.
5284 This is simpler and efficient enough for now.
5285
5286 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5287 symtab to use). Typically TUs with the same abbrev offset have the same
5288 stmt_list value too so in practice this should work well.
5289
5290 The basic algorithm here is:
5291
5292 sort TUs by abbrev table
5293 for each TU with same abbrev table:
5294 read abbrev table if first user
5295 read TU top level DIE
5296 [IWBN if DWO skeletons had DW_AT_stmt_list]
5297 call FUNC */
5298
5299 if (dwarf2_read_debug)
5300 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5301
5302 /* Sort in a separate table to maintain the order of all_type_units
5303 for .gdb_index: TU indices directly index all_type_units. */
5304 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5305 dwarf2_per_objfile->n_type_units);
5306 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5307 {
5308 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5309
5310 sorted_by_abbrev[i].sig_type = sig_type;
5311 sorted_by_abbrev[i].abbrev_offset =
5312 read_abbrev_offset (sig_type->per_cu.info_or_types_section,
5313 sig_type->per_cu.offset);
5314 }
5315 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5316 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5317 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5318
5319 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5320 called any number of times, so we don't reset tu_stats here. */
5321
5322 abbrev_offset.sect_off = ~(unsigned) 0;
5323 abbrev_table = NULL;
5324 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5325
5326 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5327 {
5328 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5329
5330 /* Switch to the next abbrev table if necessary. */
5331 if (abbrev_table == NULL
5332 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5333 {
5334 if (abbrev_table != NULL)
5335 {
5336 abbrev_table_free (abbrev_table);
5337 /* Reset to NULL in case abbrev_table_read_table throws
5338 an error: abbrev_table_free_cleanup will get called. */
5339 abbrev_table = NULL;
5340 }
5341 abbrev_offset = tu->abbrev_offset;
5342 abbrev_table =
5343 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5344 abbrev_offset);
5345 ++tu_stats->nr_uniq_abbrev_tables;
5346 }
5347
5348 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5349 func, data);
5350 }
5351
5352 /* Create a vector of pointers to primary type units to make it easy to
5353 iterate over them and CUs. See dw2_get_primary_cu. */
5354 dwarf2_per_objfile->n_type_unit_groups =
5355 htab_elements (dwarf2_per_objfile->type_unit_groups);
5356 dwarf2_per_objfile->all_type_unit_groups =
5357 obstack_alloc (&objfile->objfile_obstack,
5358 dwarf2_per_objfile->n_type_unit_groups
5359 * sizeof (struct type_unit_group *));
5360 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5361 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5362 add_type_unit_group_to_table, &iter);
5363 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5364 == dwarf2_per_objfile->n_type_unit_groups);
5365
5366 do_cleanups (cleanups);
5367
5368 if (dwarf2_read_debug)
5369 {
5370 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5371 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5372 dwarf2_per_objfile->n_type_units);
5373 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5374 tu_stats->nr_uniq_abbrev_tables);
5375 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5376 tu_stats->nr_symtabs);
5377 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5378 tu_stats->nr_symtab_sharers);
5379 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5380 tu_stats->nr_stmt_less_type_units);
5381 }
5382 }
5383
5384 /* Reader function for build_type_psymtabs. */
5385
5386 static void
5387 build_type_psymtabs_reader (const struct die_reader_specs *reader,
5388 gdb_byte *info_ptr,
5389 struct die_info *type_unit_die,
5390 int has_children,
5391 void *data)
5392 {
5393 struct objfile *objfile = dwarf2_per_objfile->objfile;
5394 struct dwarf2_cu *cu = reader->cu;
5395 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5396 struct type_unit_group *tu_group;
5397 struct attribute *attr;
5398 struct partial_die_info *first_die;
5399 CORE_ADDR lowpc, highpc;
5400 struct partial_symtab *pst;
5401
5402 gdb_assert (data == NULL);
5403
5404 if (! has_children)
5405 return;
5406
5407 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
5408 tu_group = get_type_unit_group (cu, attr);
5409
5410 VEC_safe_push (dwarf2_per_cu_ptr, tu_group->t.tus, per_cu);
5411
5412 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5413 cu->list_in_scope = &file_symbols;
5414 pst = create_partial_symtab (per_cu, "");
5415 pst->anonymous = 1;
5416
5417 first_die = load_partial_dies (reader, info_ptr, 1);
5418
5419 lowpc = (CORE_ADDR) -1;
5420 highpc = (CORE_ADDR) 0;
5421 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5422
5423 pst->n_global_syms = objfile->global_psymbols.next -
5424 (objfile->global_psymbols.list + pst->globals_offset);
5425 pst->n_static_syms = objfile->static_psymbols.next -
5426 (objfile->static_psymbols.list + pst->statics_offset);
5427 sort_pst_symbols (objfile, pst);
5428 }
5429
5430 /* Traversal function for build_type_psymtabs. */
5431
5432 static int
5433 build_type_psymtab_dependencies (void **slot, void *info)
5434 {
5435 struct objfile *objfile = dwarf2_per_objfile->objfile;
5436 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
5437 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
5438 struct partial_symtab *pst = per_cu->v.psymtab;
5439 int len = VEC_length (dwarf2_per_cu_ptr, tu_group->t.tus);
5440 struct dwarf2_per_cu_data *iter;
5441 int i;
5442
5443 gdb_assert (len > 0);
5444
5445 pst->number_of_dependencies = len;
5446 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5447 len * sizeof (struct psymtab *));
5448 for (i = 0;
5449 VEC_iterate (dwarf2_per_cu_ptr, tu_group->t.tus, i, iter);
5450 ++i)
5451 {
5452 pst->dependencies[i] = iter->v.psymtab;
5453 iter->type_unit_group = tu_group;
5454 }
5455
5456 VEC_free (dwarf2_per_cu_ptr, tu_group->t.tus);
5457
5458 return 1;
5459 }
5460
5461 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5462 Build partial symbol tables for the .debug_types comp-units. */
5463
5464 static void
5465 build_type_psymtabs (struct objfile *objfile)
5466 {
5467 if (! create_all_type_units (objfile))
5468 return;
5469
5470 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5471
5472 /* Now that all TUs have been processed we can fill in the dependencies. */
5473 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5474 build_type_psymtab_dependencies, NULL);
5475 }
5476
5477 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
5478
5479 static void
5480 psymtabs_addrmap_cleanup (void *o)
5481 {
5482 struct objfile *objfile = o;
5483
5484 objfile->psymtabs_addrmap = NULL;
5485 }
5486
5487 /* Compute the 'user' field for each psymtab in OBJFILE. */
5488
5489 static void
5490 set_partial_user (struct objfile *objfile)
5491 {
5492 int i;
5493
5494 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5495 {
5496 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5497 struct partial_symtab *pst = per_cu->v.psymtab;
5498 int j;
5499
5500 if (pst == NULL)
5501 continue;
5502
5503 for (j = 0; j < pst->number_of_dependencies; ++j)
5504 {
5505 /* Set the 'user' field only if it is not already set. */
5506 if (pst->dependencies[j]->user == NULL)
5507 pst->dependencies[j]->user = pst;
5508 }
5509 }
5510 }
5511
5512 /* Build the partial symbol table by doing a quick pass through the
5513 .debug_info and .debug_abbrev sections. */
5514
5515 static void
5516 dwarf2_build_psymtabs_hard (struct objfile *objfile)
5517 {
5518 struct cleanup *back_to, *addrmap_cleanup;
5519 struct obstack temp_obstack;
5520 int i;
5521
5522 if (dwarf2_read_debug)
5523 {
5524 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5525 objfile->name);
5526 }
5527
5528 dwarf2_per_objfile->reading_partial_symbols = 1;
5529
5530 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
5531
5532 /* Any cached compilation units will be linked by the per-objfile
5533 read_in_chain. Make sure to free them when we're done. */
5534 back_to = make_cleanup (free_cached_comp_units, NULL);
5535
5536 build_type_psymtabs (objfile);
5537
5538 create_all_comp_units (objfile);
5539
5540 /* Create a temporary address map on a temporary obstack. We later
5541 copy this to the final obstack. */
5542 obstack_init (&temp_obstack);
5543 make_cleanup_obstack_free (&temp_obstack);
5544 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
5545 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
5546
5547 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5548 {
5549 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5550
5551 process_psymtab_comp_unit (per_cu, 0);
5552 }
5553
5554 set_partial_user (objfile);
5555
5556 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
5557 &objfile->objfile_obstack);
5558 discard_cleanups (addrmap_cleanup);
5559
5560 do_cleanups (back_to);
5561
5562 if (dwarf2_read_debug)
5563 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
5564 objfile->name);
5565 }
5566
5567 /* die_reader_func for load_partial_comp_unit. */
5568
5569 static void
5570 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
5571 gdb_byte *info_ptr,
5572 struct die_info *comp_unit_die,
5573 int has_children,
5574 void *data)
5575 {
5576 struct dwarf2_cu *cu = reader->cu;
5577
5578 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
5579
5580 /* Check if comp unit has_children.
5581 If so, read the rest of the partial symbols from this comp unit.
5582 If not, there's no more debug_info for this comp unit. */
5583 if (has_children)
5584 load_partial_dies (reader, info_ptr, 0);
5585 }
5586
5587 /* Load the partial DIEs for a secondary CU into memory.
5588 This is also used when rereading a primary CU with load_all_dies. */
5589
5590 static void
5591 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
5592 {
5593 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
5594 load_partial_comp_unit_reader, NULL);
5595 }
5596
5597 static void
5598 read_comp_units_from_section (struct objfile *objfile,
5599 struct dwarf2_section_info *section,
5600 unsigned int is_dwz,
5601 int *n_allocated,
5602 int *n_comp_units,
5603 struct dwarf2_per_cu_data ***all_comp_units)
5604 {
5605 gdb_byte *info_ptr;
5606 bfd *abfd = section->asection->owner;
5607
5608 dwarf2_read_section (objfile, section);
5609
5610 info_ptr = section->buffer;
5611
5612 while (info_ptr < section->buffer + section->size)
5613 {
5614 unsigned int length, initial_length_size;
5615 struct dwarf2_per_cu_data *this_cu;
5616 sect_offset offset;
5617
5618 offset.sect_off = info_ptr - section->buffer;
5619
5620 /* Read just enough information to find out where the next
5621 compilation unit is. */
5622 length = read_initial_length (abfd, info_ptr, &initial_length_size);
5623
5624 /* Save the compilation unit for later lookup. */
5625 this_cu = obstack_alloc (&objfile->objfile_obstack,
5626 sizeof (struct dwarf2_per_cu_data));
5627 memset (this_cu, 0, sizeof (*this_cu));
5628 this_cu->offset = offset;
5629 this_cu->length = length + initial_length_size;
5630 this_cu->is_dwz = is_dwz;
5631 this_cu->objfile = objfile;
5632 this_cu->info_or_types_section = section;
5633
5634 if (*n_comp_units == *n_allocated)
5635 {
5636 *n_allocated *= 2;
5637 *all_comp_units = xrealloc (*all_comp_units,
5638 *n_allocated
5639 * sizeof (struct dwarf2_per_cu_data *));
5640 }
5641 (*all_comp_units)[*n_comp_units] = this_cu;
5642 ++*n_comp_units;
5643
5644 info_ptr = info_ptr + this_cu->length;
5645 }
5646 }
5647
5648 /* Create a list of all compilation units in OBJFILE.
5649 This is only done for -readnow and building partial symtabs. */
5650
5651 static void
5652 create_all_comp_units (struct objfile *objfile)
5653 {
5654 int n_allocated;
5655 int n_comp_units;
5656 struct dwarf2_per_cu_data **all_comp_units;
5657
5658 n_comp_units = 0;
5659 n_allocated = 10;
5660 all_comp_units = xmalloc (n_allocated
5661 * sizeof (struct dwarf2_per_cu_data *));
5662
5663 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
5664 &n_allocated, &n_comp_units, &all_comp_units);
5665
5666 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
5667 {
5668 struct dwz_file *dwz = dwarf2_get_dwz_file ();
5669
5670 read_comp_units_from_section (objfile, &dwz->info, 1,
5671 &n_allocated, &n_comp_units,
5672 &all_comp_units);
5673 }
5674
5675 dwarf2_per_objfile->all_comp_units
5676 = obstack_alloc (&objfile->objfile_obstack,
5677 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5678 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
5679 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5680 xfree (all_comp_units);
5681 dwarf2_per_objfile->n_comp_units = n_comp_units;
5682 }
5683
5684 /* Process all loaded DIEs for compilation unit CU, starting at
5685 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
5686 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
5687 DW_AT_ranges). If NEED_PC is set, then this function will set
5688 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
5689 and record the covered ranges in the addrmap. */
5690
5691 static void
5692 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5693 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
5694 {
5695 struct partial_die_info *pdi;
5696
5697 /* Now, march along the PDI's, descending into ones which have
5698 interesting children but skipping the children of the other ones,
5699 until we reach the end of the compilation unit. */
5700
5701 pdi = first_die;
5702
5703 while (pdi != NULL)
5704 {
5705 fixup_partial_die (pdi, cu);
5706
5707 /* Anonymous namespaces or modules have no name but have interesting
5708 children, so we need to look at them. Ditto for anonymous
5709 enums. */
5710
5711 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
5712 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
5713 || pdi->tag == DW_TAG_imported_unit)
5714 {
5715 switch (pdi->tag)
5716 {
5717 case DW_TAG_subprogram:
5718 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
5719 break;
5720 case DW_TAG_constant:
5721 case DW_TAG_variable:
5722 case DW_TAG_typedef:
5723 case DW_TAG_union_type:
5724 if (!pdi->is_declaration)
5725 {
5726 add_partial_symbol (pdi, cu);
5727 }
5728 break;
5729 case DW_TAG_class_type:
5730 case DW_TAG_interface_type:
5731 case DW_TAG_structure_type:
5732 if (!pdi->is_declaration)
5733 {
5734 add_partial_symbol (pdi, cu);
5735 }
5736 break;
5737 case DW_TAG_enumeration_type:
5738 if (!pdi->is_declaration)
5739 add_partial_enumeration (pdi, cu);
5740 break;
5741 case DW_TAG_base_type:
5742 case DW_TAG_subrange_type:
5743 /* File scope base type definitions are added to the partial
5744 symbol table. */
5745 add_partial_symbol (pdi, cu);
5746 break;
5747 case DW_TAG_namespace:
5748 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
5749 break;
5750 case DW_TAG_module:
5751 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
5752 break;
5753 case DW_TAG_imported_unit:
5754 {
5755 struct dwarf2_per_cu_data *per_cu;
5756
5757 /* For now we don't handle imported units in type units. */
5758 if (cu->per_cu->is_debug_types)
5759 {
5760 error (_("Dwarf Error: DW_TAG_imported_unit is not"
5761 " supported in type units [in module %s]"),
5762 cu->objfile->name);
5763 }
5764
5765 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
5766 pdi->is_dwz,
5767 cu->objfile);
5768
5769 /* Go read the partial unit, if needed. */
5770 if (per_cu->v.psymtab == NULL)
5771 process_psymtab_comp_unit (per_cu, 1);
5772
5773 VEC_safe_push (dwarf2_per_cu_ptr,
5774 cu->per_cu->imported_symtabs, per_cu);
5775 }
5776 break;
5777 default:
5778 break;
5779 }
5780 }
5781
5782 /* If the die has a sibling, skip to the sibling. */
5783
5784 pdi = pdi->die_sibling;
5785 }
5786 }
5787
5788 /* Functions used to compute the fully scoped name of a partial DIE.
5789
5790 Normally, this is simple. For C++, the parent DIE's fully scoped
5791 name is concatenated with "::" and the partial DIE's name. For
5792 Java, the same thing occurs except that "." is used instead of "::".
5793 Enumerators are an exception; they use the scope of their parent
5794 enumeration type, i.e. the name of the enumeration type is not
5795 prepended to the enumerator.
5796
5797 There are two complexities. One is DW_AT_specification; in this
5798 case "parent" means the parent of the target of the specification,
5799 instead of the direct parent of the DIE. The other is compilers
5800 which do not emit DW_TAG_namespace; in this case we try to guess
5801 the fully qualified name of structure types from their members'
5802 linkage names. This must be done using the DIE's children rather
5803 than the children of any DW_AT_specification target. We only need
5804 to do this for structures at the top level, i.e. if the target of
5805 any DW_AT_specification (if any; otherwise the DIE itself) does not
5806 have a parent. */
5807
5808 /* Compute the scope prefix associated with PDI's parent, in
5809 compilation unit CU. The result will be allocated on CU's
5810 comp_unit_obstack, or a copy of the already allocated PDI->NAME
5811 field. NULL is returned if no prefix is necessary. */
5812 static const char *
5813 partial_die_parent_scope (struct partial_die_info *pdi,
5814 struct dwarf2_cu *cu)
5815 {
5816 const char *grandparent_scope;
5817 struct partial_die_info *parent, *real_pdi;
5818
5819 /* We need to look at our parent DIE; if we have a DW_AT_specification,
5820 then this means the parent of the specification DIE. */
5821
5822 real_pdi = pdi;
5823 while (real_pdi->has_specification)
5824 real_pdi = find_partial_die (real_pdi->spec_offset,
5825 real_pdi->spec_is_dwz, cu);
5826
5827 parent = real_pdi->die_parent;
5828 if (parent == NULL)
5829 return NULL;
5830
5831 if (parent->scope_set)
5832 return parent->scope;
5833
5834 fixup_partial_die (parent, cu);
5835
5836 grandparent_scope = partial_die_parent_scope (parent, cu);
5837
5838 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
5839 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
5840 Work around this problem here. */
5841 if (cu->language == language_cplus
5842 && parent->tag == DW_TAG_namespace
5843 && strcmp (parent->name, "::") == 0
5844 && grandparent_scope == NULL)
5845 {
5846 parent->scope = NULL;
5847 parent->scope_set = 1;
5848 return NULL;
5849 }
5850
5851 if (pdi->tag == DW_TAG_enumerator)
5852 /* Enumerators should not get the name of the enumeration as a prefix. */
5853 parent->scope = grandparent_scope;
5854 else if (parent->tag == DW_TAG_namespace
5855 || parent->tag == DW_TAG_module
5856 || parent->tag == DW_TAG_structure_type
5857 || parent->tag == DW_TAG_class_type
5858 || parent->tag == DW_TAG_interface_type
5859 || parent->tag == DW_TAG_union_type
5860 || parent->tag == DW_TAG_enumeration_type)
5861 {
5862 if (grandparent_scope == NULL)
5863 parent->scope = parent->name;
5864 else
5865 parent->scope = typename_concat (&cu->comp_unit_obstack,
5866 grandparent_scope,
5867 parent->name, 0, cu);
5868 }
5869 else
5870 {
5871 /* FIXME drow/2004-04-01: What should we be doing with
5872 function-local names? For partial symbols, we should probably be
5873 ignoring them. */
5874 complaint (&symfile_complaints,
5875 _("unhandled containing DIE tag %d for DIE at %d"),
5876 parent->tag, pdi->offset.sect_off);
5877 parent->scope = grandparent_scope;
5878 }
5879
5880 parent->scope_set = 1;
5881 return parent->scope;
5882 }
5883
5884 /* Return the fully scoped name associated with PDI, from compilation unit
5885 CU. The result will be allocated with malloc. */
5886
5887 static char *
5888 partial_die_full_name (struct partial_die_info *pdi,
5889 struct dwarf2_cu *cu)
5890 {
5891 const char *parent_scope;
5892
5893 /* If this is a template instantiation, we can not work out the
5894 template arguments from partial DIEs. So, unfortunately, we have
5895 to go through the full DIEs. At least any work we do building
5896 types here will be reused if full symbols are loaded later. */
5897 if (pdi->has_template_arguments)
5898 {
5899 fixup_partial_die (pdi, cu);
5900
5901 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
5902 {
5903 struct die_info *die;
5904 struct attribute attr;
5905 struct dwarf2_cu *ref_cu = cu;
5906
5907 /* DW_FORM_ref_addr is using section offset. */
5908 attr.name = 0;
5909 attr.form = DW_FORM_ref_addr;
5910 attr.u.unsnd = pdi->offset.sect_off;
5911 die = follow_die_ref (NULL, &attr, &ref_cu);
5912
5913 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
5914 }
5915 }
5916
5917 parent_scope = partial_die_parent_scope (pdi, cu);
5918 if (parent_scope == NULL)
5919 return NULL;
5920 else
5921 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
5922 }
5923
5924 static void
5925 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
5926 {
5927 struct objfile *objfile = cu->objfile;
5928 CORE_ADDR addr = 0;
5929 const char *actual_name = NULL;
5930 CORE_ADDR baseaddr;
5931 char *built_actual_name;
5932
5933 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5934
5935 built_actual_name = partial_die_full_name (pdi, cu);
5936 if (built_actual_name != NULL)
5937 actual_name = built_actual_name;
5938
5939 if (actual_name == NULL)
5940 actual_name = pdi->name;
5941
5942 switch (pdi->tag)
5943 {
5944 case DW_TAG_subprogram:
5945 if (pdi->is_external || cu->language == language_ada)
5946 {
5947 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
5948 of the global scope. But in Ada, we want to be able to access
5949 nested procedures globally. So all Ada subprograms are stored
5950 in the global scope. */
5951 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
5952 mst_text, objfile); */
5953 add_psymbol_to_list (actual_name, strlen (actual_name),
5954 built_actual_name != NULL,
5955 VAR_DOMAIN, LOC_BLOCK,
5956 &objfile->global_psymbols,
5957 0, pdi->lowpc + baseaddr,
5958 cu->language, objfile);
5959 }
5960 else
5961 {
5962 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
5963 mst_file_text, objfile); */
5964 add_psymbol_to_list (actual_name, strlen (actual_name),
5965 built_actual_name != NULL,
5966 VAR_DOMAIN, LOC_BLOCK,
5967 &objfile->static_psymbols,
5968 0, pdi->lowpc + baseaddr,
5969 cu->language, objfile);
5970 }
5971 break;
5972 case DW_TAG_constant:
5973 {
5974 struct psymbol_allocation_list *list;
5975
5976 if (pdi->is_external)
5977 list = &objfile->global_psymbols;
5978 else
5979 list = &objfile->static_psymbols;
5980 add_psymbol_to_list (actual_name, strlen (actual_name),
5981 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
5982 list, 0, 0, cu->language, objfile);
5983 }
5984 break;
5985 case DW_TAG_variable:
5986 if (pdi->d.locdesc)
5987 addr = decode_locdesc (pdi->d.locdesc, cu);
5988
5989 if (pdi->d.locdesc
5990 && addr == 0
5991 && !dwarf2_per_objfile->has_section_at_zero)
5992 {
5993 /* A global or static variable may also have been stripped
5994 out by the linker if unused, in which case its address
5995 will be nullified; do not add such variables into partial
5996 symbol table then. */
5997 }
5998 else if (pdi->is_external)
5999 {
6000 /* Global Variable.
6001 Don't enter into the minimal symbol tables as there is
6002 a minimal symbol table entry from the ELF symbols already.
6003 Enter into partial symbol table if it has a location
6004 descriptor or a type.
6005 If the location descriptor is missing, new_symbol will create
6006 a LOC_UNRESOLVED symbol, the address of the variable will then
6007 be determined from the minimal symbol table whenever the variable
6008 is referenced.
6009 The address for the partial symbol table entry is not
6010 used by GDB, but it comes in handy for debugging partial symbol
6011 table building. */
6012
6013 if (pdi->d.locdesc || pdi->has_type)
6014 add_psymbol_to_list (actual_name, strlen (actual_name),
6015 built_actual_name != NULL,
6016 VAR_DOMAIN, LOC_STATIC,
6017 &objfile->global_psymbols,
6018 0, addr + baseaddr,
6019 cu->language, objfile);
6020 }
6021 else
6022 {
6023 /* Static Variable. Skip symbols without location descriptors. */
6024 if (pdi->d.locdesc == NULL)
6025 {
6026 xfree (built_actual_name);
6027 return;
6028 }
6029 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
6030 mst_file_data, objfile); */
6031 add_psymbol_to_list (actual_name, strlen (actual_name),
6032 built_actual_name != NULL,
6033 VAR_DOMAIN, LOC_STATIC,
6034 &objfile->static_psymbols,
6035 0, addr + baseaddr,
6036 cu->language, objfile);
6037 }
6038 break;
6039 case DW_TAG_typedef:
6040 case DW_TAG_base_type:
6041 case DW_TAG_subrange_type:
6042 add_psymbol_to_list (actual_name, strlen (actual_name),
6043 built_actual_name != NULL,
6044 VAR_DOMAIN, LOC_TYPEDEF,
6045 &objfile->static_psymbols,
6046 0, (CORE_ADDR) 0, cu->language, objfile);
6047 break;
6048 case DW_TAG_namespace:
6049 add_psymbol_to_list (actual_name, strlen (actual_name),
6050 built_actual_name != NULL,
6051 VAR_DOMAIN, LOC_TYPEDEF,
6052 &objfile->global_psymbols,
6053 0, (CORE_ADDR) 0, cu->language, objfile);
6054 break;
6055 case DW_TAG_class_type:
6056 case DW_TAG_interface_type:
6057 case DW_TAG_structure_type:
6058 case DW_TAG_union_type:
6059 case DW_TAG_enumeration_type:
6060 /* Skip external references. The DWARF standard says in the section
6061 about "Structure, Union, and Class Type Entries": "An incomplete
6062 structure, union or class type is represented by a structure,
6063 union or class entry that does not have a byte size attribute
6064 and that has a DW_AT_declaration attribute." */
6065 if (!pdi->has_byte_size && pdi->is_declaration)
6066 {
6067 xfree (built_actual_name);
6068 return;
6069 }
6070
6071 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
6072 static vs. global. */
6073 add_psymbol_to_list (actual_name, strlen (actual_name),
6074 built_actual_name != NULL,
6075 STRUCT_DOMAIN, LOC_TYPEDEF,
6076 (cu->language == language_cplus
6077 || cu->language == language_java)
6078 ? &objfile->global_psymbols
6079 : &objfile->static_psymbols,
6080 0, (CORE_ADDR) 0, cu->language, objfile);
6081
6082 break;
6083 case DW_TAG_enumerator:
6084 add_psymbol_to_list (actual_name, strlen (actual_name),
6085 built_actual_name != NULL,
6086 VAR_DOMAIN, LOC_CONST,
6087 (cu->language == language_cplus
6088 || cu->language == language_java)
6089 ? &objfile->global_psymbols
6090 : &objfile->static_psymbols,
6091 0, (CORE_ADDR) 0, cu->language, objfile);
6092 break;
6093 default:
6094 break;
6095 }
6096
6097 xfree (built_actual_name);
6098 }
6099
6100 /* Read a partial die corresponding to a namespace; also, add a symbol
6101 corresponding to that namespace to the symbol table. NAMESPACE is
6102 the name of the enclosing namespace. */
6103
6104 static void
6105 add_partial_namespace (struct partial_die_info *pdi,
6106 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6107 int need_pc, struct dwarf2_cu *cu)
6108 {
6109 /* Add a symbol for the namespace. */
6110
6111 add_partial_symbol (pdi, cu);
6112
6113 /* Now scan partial symbols in that namespace. */
6114
6115 if (pdi->has_children)
6116 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6117 }
6118
6119 /* Read a partial die corresponding to a Fortran module. */
6120
6121 static void
6122 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
6123 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6124 {
6125 /* Now scan partial symbols in that module. */
6126
6127 if (pdi->has_children)
6128 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6129 }
6130
6131 /* Read a partial die corresponding to a subprogram and create a partial
6132 symbol for that subprogram. When the CU language allows it, this
6133 routine also defines a partial symbol for each nested subprogram
6134 that this subprogram contains.
6135
6136 DIE my also be a lexical block, in which case we simply search
6137 recursively for suprograms defined inside that lexical block.
6138 Again, this is only performed when the CU language allows this
6139 type of definitions. */
6140
6141 static void
6142 add_partial_subprogram (struct partial_die_info *pdi,
6143 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6144 int need_pc, struct dwarf2_cu *cu)
6145 {
6146 if (pdi->tag == DW_TAG_subprogram)
6147 {
6148 if (pdi->has_pc_info)
6149 {
6150 if (pdi->lowpc < *lowpc)
6151 *lowpc = pdi->lowpc;
6152 if (pdi->highpc > *highpc)
6153 *highpc = pdi->highpc;
6154 if (need_pc)
6155 {
6156 CORE_ADDR baseaddr;
6157 struct objfile *objfile = cu->objfile;
6158
6159 baseaddr = ANOFFSET (objfile->section_offsets,
6160 SECT_OFF_TEXT (objfile));
6161 addrmap_set_empty (objfile->psymtabs_addrmap,
6162 pdi->lowpc + baseaddr,
6163 pdi->highpc - 1 + baseaddr,
6164 cu->per_cu->v.psymtab);
6165 }
6166 }
6167
6168 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6169 {
6170 if (!pdi->is_declaration)
6171 /* Ignore subprogram DIEs that do not have a name, they are
6172 illegal. Do not emit a complaint at this point, we will
6173 do so when we convert this psymtab into a symtab. */
6174 if (pdi->name)
6175 add_partial_symbol (pdi, cu);
6176 }
6177 }
6178
6179 if (! pdi->has_children)
6180 return;
6181
6182 if (cu->language == language_ada)
6183 {
6184 pdi = pdi->die_child;
6185 while (pdi != NULL)
6186 {
6187 fixup_partial_die (pdi, cu);
6188 if (pdi->tag == DW_TAG_subprogram
6189 || pdi->tag == DW_TAG_lexical_block)
6190 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
6191 pdi = pdi->die_sibling;
6192 }
6193 }
6194 }
6195
6196 /* Read a partial die corresponding to an enumeration type. */
6197
6198 static void
6199 add_partial_enumeration (struct partial_die_info *enum_pdi,
6200 struct dwarf2_cu *cu)
6201 {
6202 struct partial_die_info *pdi;
6203
6204 if (enum_pdi->name != NULL)
6205 add_partial_symbol (enum_pdi, cu);
6206
6207 pdi = enum_pdi->die_child;
6208 while (pdi)
6209 {
6210 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
6211 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
6212 else
6213 add_partial_symbol (pdi, cu);
6214 pdi = pdi->die_sibling;
6215 }
6216 }
6217
6218 /* Return the initial uleb128 in the die at INFO_PTR. */
6219
6220 static unsigned int
6221 peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
6222 {
6223 unsigned int bytes_read;
6224
6225 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6226 }
6227
6228 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6229 Return the corresponding abbrev, or NULL if the number is zero (indicating
6230 an empty DIE). In either case *BYTES_READ will be set to the length of
6231 the initial number. */
6232
6233 static struct abbrev_info *
6234 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
6235 struct dwarf2_cu *cu)
6236 {
6237 bfd *abfd = cu->objfile->obfd;
6238 unsigned int abbrev_number;
6239 struct abbrev_info *abbrev;
6240
6241 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6242
6243 if (abbrev_number == 0)
6244 return NULL;
6245
6246 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
6247 if (!abbrev)
6248 {
6249 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6250 abbrev_number, bfd_get_filename (abfd));
6251 }
6252
6253 return abbrev;
6254 }
6255
6256 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6257 Returns a pointer to the end of a series of DIEs, terminated by an empty
6258 DIE. Any children of the skipped DIEs will also be skipped. */
6259
6260 static gdb_byte *
6261 skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
6262 {
6263 struct dwarf2_cu *cu = reader->cu;
6264 struct abbrev_info *abbrev;
6265 unsigned int bytes_read;
6266
6267 while (1)
6268 {
6269 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6270 if (abbrev == NULL)
6271 return info_ptr + bytes_read;
6272 else
6273 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
6274 }
6275 }
6276
6277 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6278 INFO_PTR should point just after the initial uleb128 of a DIE, and the
6279 abbrev corresponding to that skipped uleb128 should be passed in
6280 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6281 children. */
6282
6283 static gdb_byte *
6284 skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
6285 struct abbrev_info *abbrev)
6286 {
6287 unsigned int bytes_read;
6288 struct attribute attr;
6289 bfd *abfd = reader->abfd;
6290 struct dwarf2_cu *cu = reader->cu;
6291 gdb_byte *buffer = reader->buffer;
6292 const gdb_byte *buffer_end = reader->buffer_end;
6293 gdb_byte *start_info_ptr = info_ptr;
6294 unsigned int form, i;
6295
6296 for (i = 0; i < abbrev->num_attrs; i++)
6297 {
6298 /* The only abbrev we care about is DW_AT_sibling. */
6299 if (abbrev->attrs[i].name == DW_AT_sibling)
6300 {
6301 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
6302 if (attr.form == DW_FORM_ref_addr)
6303 complaint (&symfile_complaints,
6304 _("ignoring absolute DW_AT_sibling"));
6305 else
6306 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
6307 }
6308
6309 /* If it isn't DW_AT_sibling, skip this attribute. */
6310 form = abbrev->attrs[i].form;
6311 skip_attribute:
6312 switch (form)
6313 {
6314 case DW_FORM_ref_addr:
6315 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6316 and later it is offset sized. */
6317 if (cu->header.version == 2)
6318 info_ptr += cu->header.addr_size;
6319 else
6320 info_ptr += cu->header.offset_size;
6321 break;
6322 case DW_FORM_GNU_ref_alt:
6323 info_ptr += cu->header.offset_size;
6324 break;
6325 case DW_FORM_addr:
6326 info_ptr += cu->header.addr_size;
6327 break;
6328 case DW_FORM_data1:
6329 case DW_FORM_ref1:
6330 case DW_FORM_flag:
6331 info_ptr += 1;
6332 break;
6333 case DW_FORM_flag_present:
6334 break;
6335 case DW_FORM_data2:
6336 case DW_FORM_ref2:
6337 info_ptr += 2;
6338 break;
6339 case DW_FORM_data4:
6340 case DW_FORM_ref4:
6341 info_ptr += 4;
6342 break;
6343 case DW_FORM_data8:
6344 case DW_FORM_ref8:
6345 case DW_FORM_ref_sig8:
6346 info_ptr += 8;
6347 break;
6348 case DW_FORM_string:
6349 read_direct_string (abfd, info_ptr, &bytes_read);
6350 info_ptr += bytes_read;
6351 break;
6352 case DW_FORM_sec_offset:
6353 case DW_FORM_strp:
6354 case DW_FORM_GNU_strp_alt:
6355 info_ptr += cu->header.offset_size;
6356 break;
6357 case DW_FORM_exprloc:
6358 case DW_FORM_block:
6359 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6360 info_ptr += bytes_read;
6361 break;
6362 case DW_FORM_block1:
6363 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6364 break;
6365 case DW_FORM_block2:
6366 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6367 break;
6368 case DW_FORM_block4:
6369 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6370 break;
6371 case DW_FORM_sdata:
6372 case DW_FORM_udata:
6373 case DW_FORM_ref_udata:
6374 case DW_FORM_GNU_addr_index:
6375 case DW_FORM_GNU_str_index:
6376 info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
6377 break;
6378 case DW_FORM_indirect:
6379 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6380 info_ptr += bytes_read;
6381 /* We need to continue parsing from here, so just go back to
6382 the top. */
6383 goto skip_attribute;
6384
6385 default:
6386 error (_("Dwarf Error: Cannot handle %s "
6387 "in DWARF reader [in module %s]"),
6388 dwarf_form_name (form),
6389 bfd_get_filename (abfd));
6390 }
6391 }
6392
6393 if (abbrev->has_children)
6394 return skip_children (reader, info_ptr);
6395 else
6396 return info_ptr;
6397 }
6398
6399 /* Locate ORIG_PDI's sibling.
6400 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
6401
6402 static gdb_byte *
6403 locate_pdi_sibling (const struct die_reader_specs *reader,
6404 struct partial_die_info *orig_pdi,
6405 gdb_byte *info_ptr)
6406 {
6407 /* Do we know the sibling already? */
6408
6409 if (orig_pdi->sibling)
6410 return orig_pdi->sibling;
6411
6412 /* Are there any children to deal with? */
6413
6414 if (!orig_pdi->has_children)
6415 return info_ptr;
6416
6417 /* Skip the children the long way. */
6418
6419 return skip_children (reader, info_ptr);
6420 }
6421
6422 /* Expand this partial symbol table into a full symbol table. SELF is
6423 not NULL. */
6424
6425 static void
6426 dwarf2_read_symtab (struct partial_symtab *self,
6427 struct objfile *objfile)
6428 {
6429 if (self->readin)
6430 {
6431 warning (_("bug: psymtab for %s is already read in."),
6432 self->filename);
6433 }
6434 else
6435 {
6436 if (info_verbose)
6437 {
6438 printf_filtered (_("Reading in symbols for %s..."),
6439 self->filename);
6440 gdb_flush (gdb_stdout);
6441 }
6442
6443 /* Restore our global data. */
6444 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
6445
6446 /* If this psymtab is constructed from a debug-only objfile, the
6447 has_section_at_zero flag will not necessarily be correct. We
6448 can get the correct value for this flag by looking at the data
6449 associated with the (presumably stripped) associated objfile. */
6450 if (objfile->separate_debug_objfile_backlink)
6451 {
6452 struct dwarf2_per_objfile *dpo_backlink
6453 = objfile_data (objfile->separate_debug_objfile_backlink,
6454 dwarf2_objfile_data_key);
6455
6456 dwarf2_per_objfile->has_section_at_zero
6457 = dpo_backlink->has_section_at_zero;
6458 }
6459
6460 dwarf2_per_objfile->reading_partial_symbols = 0;
6461
6462 psymtab_to_symtab_1 (self);
6463
6464 /* Finish up the debug error message. */
6465 if (info_verbose)
6466 printf_filtered (_("done.\n"));
6467 }
6468
6469 process_cu_includes ();
6470 }
6471 \f
6472 /* Reading in full CUs. */
6473
6474 /* Add PER_CU to the queue. */
6475
6476 static void
6477 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6478 enum language pretend_language)
6479 {
6480 struct dwarf2_queue_item *item;
6481
6482 per_cu->queued = 1;
6483 item = xmalloc (sizeof (*item));
6484 item->per_cu = per_cu;
6485 item->pretend_language = pretend_language;
6486 item->next = NULL;
6487
6488 if (dwarf2_queue == NULL)
6489 dwarf2_queue = item;
6490 else
6491 dwarf2_queue_tail->next = item;
6492
6493 dwarf2_queue_tail = item;
6494 }
6495
6496 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
6497 unit and add it to our queue.
6498 The result is non-zero if PER_CU was queued, otherwise the result is zero
6499 meaning either PER_CU is already queued or it is already loaded. */
6500
6501 static int
6502 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6503 struct dwarf2_per_cu_data *per_cu,
6504 enum language pretend_language)
6505 {
6506 /* We may arrive here during partial symbol reading, if we need full
6507 DIEs to process an unusual case (e.g. template arguments). Do
6508 not queue PER_CU, just tell our caller to load its DIEs. */
6509 if (dwarf2_per_objfile->reading_partial_symbols)
6510 {
6511 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6512 return 1;
6513 return 0;
6514 }
6515
6516 /* Mark the dependence relation so that we don't flush PER_CU
6517 too early. */
6518 dwarf2_add_dependence (this_cu, per_cu);
6519
6520 /* If it's already on the queue, we have nothing to do. */
6521 if (per_cu->queued)
6522 return 0;
6523
6524 /* If the compilation unit is already loaded, just mark it as
6525 used. */
6526 if (per_cu->cu != NULL)
6527 {
6528 per_cu->cu->last_used = 0;
6529 return 0;
6530 }
6531
6532 /* Add it to the queue. */
6533 queue_comp_unit (per_cu, pretend_language);
6534
6535 return 1;
6536 }
6537
6538 /* Process the queue. */
6539
6540 static void
6541 process_queue (void)
6542 {
6543 struct dwarf2_queue_item *item, *next_item;
6544
6545 if (dwarf2_read_debug)
6546 {
6547 fprintf_unfiltered (gdb_stdlog,
6548 "Expanding one or more symtabs of objfile %s ...\n",
6549 dwarf2_per_objfile->objfile->name);
6550 }
6551
6552 /* The queue starts out with one item, but following a DIE reference
6553 may load a new CU, adding it to the end of the queue. */
6554 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
6555 {
6556 if (dwarf2_per_objfile->using_index
6557 ? !item->per_cu->v.quick->symtab
6558 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
6559 {
6560 struct dwarf2_per_cu_data *per_cu = item->per_cu;
6561
6562 if (dwarf2_read_debug)
6563 {
6564 fprintf_unfiltered (gdb_stdlog,
6565 "Expanding symtab of %s at offset 0x%x\n",
6566 per_cu->is_debug_types ? "TU" : "CU",
6567 per_cu->offset.sect_off);
6568 }
6569
6570 if (per_cu->is_debug_types)
6571 process_full_type_unit (per_cu, item->pretend_language);
6572 else
6573 process_full_comp_unit (per_cu, item->pretend_language);
6574
6575 if (dwarf2_read_debug)
6576 {
6577 fprintf_unfiltered (gdb_stdlog,
6578 "Done expanding %s at offset 0x%x\n",
6579 per_cu->is_debug_types ? "TU" : "CU",
6580 per_cu->offset.sect_off);
6581 }
6582 }
6583
6584 item->per_cu->queued = 0;
6585 next_item = item->next;
6586 xfree (item);
6587 }
6588
6589 dwarf2_queue_tail = NULL;
6590
6591 if (dwarf2_read_debug)
6592 {
6593 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
6594 dwarf2_per_objfile->objfile->name);
6595 }
6596 }
6597
6598 /* Free all allocated queue entries. This function only releases anything if
6599 an error was thrown; if the queue was processed then it would have been
6600 freed as we went along. */
6601
6602 static void
6603 dwarf2_release_queue (void *dummy)
6604 {
6605 struct dwarf2_queue_item *item, *last;
6606
6607 item = dwarf2_queue;
6608 while (item)
6609 {
6610 /* Anything still marked queued is likely to be in an
6611 inconsistent state, so discard it. */
6612 if (item->per_cu->queued)
6613 {
6614 if (item->per_cu->cu != NULL)
6615 free_one_cached_comp_unit (item->per_cu);
6616 item->per_cu->queued = 0;
6617 }
6618
6619 last = item;
6620 item = item->next;
6621 xfree (last);
6622 }
6623
6624 dwarf2_queue = dwarf2_queue_tail = NULL;
6625 }
6626
6627 /* Read in full symbols for PST, and anything it depends on. */
6628
6629 static void
6630 psymtab_to_symtab_1 (struct partial_symtab *pst)
6631 {
6632 struct dwarf2_per_cu_data *per_cu;
6633 int i;
6634
6635 if (pst->readin)
6636 return;
6637
6638 for (i = 0; i < pst->number_of_dependencies; i++)
6639 if (!pst->dependencies[i]->readin
6640 && pst->dependencies[i]->user == NULL)
6641 {
6642 /* Inform about additional files that need to be read in. */
6643 if (info_verbose)
6644 {
6645 /* FIXME: i18n: Need to make this a single string. */
6646 fputs_filtered (" ", gdb_stdout);
6647 wrap_here ("");
6648 fputs_filtered ("and ", gdb_stdout);
6649 wrap_here ("");
6650 printf_filtered ("%s...", pst->dependencies[i]->filename);
6651 wrap_here (""); /* Flush output. */
6652 gdb_flush (gdb_stdout);
6653 }
6654 psymtab_to_symtab_1 (pst->dependencies[i]);
6655 }
6656
6657 per_cu = pst->read_symtab_private;
6658
6659 if (per_cu == NULL)
6660 {
6661 /* It's an include file, no symbols to read for it.
6662 Everything is in the parent symtab. */
6663 pst->readin = 1;
6664 return;
6665 }
6666
6667 dw2_do_instantiate_symtab (per_cu);
6668 }
6669
6670 /* Trivial hash function for die_info: the hash value of a DIE
6671 is its offset in .debug_info for this objfile. */
6672
6673 static hashval_t
6674 die_hash (const void *item)
6675 {
6676 const struct die_info *die = item;
6677
6678 return die->offset.sect_off;
6679 }
6680
6681 /* Trivial comparison function for die_info structures: two DIEs
6682 are equal if they have the same offset. */
6683
6684 static int
6685 die_eq (const void *item_lhs, const void *item_rhs)
6686 {
6687 const struct die_info *die_lhs = item_lhs;
6688 const struct die_info *die_rhs = item_rhs;
6689
6690 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
6691 }
6692
6693 /* die_reader_func for load_full_comp_unit.
6694 This is identical to read_signatured_type_reader,
6695 but is kept separate for now. */
6696
6697 static void
6698 load_full_comp_unit_reader (const struct die_reader_specs *reader,
6699 gdb_byte *info_ptr,
6700 struct die_info *comp_unit_die,
6701 int has_children,
6702 void *data)
6703 {
6704 struct dwarf2_cu *cu = reader->cu;
6705 enum language *language_ptr = data;
6706
6707 gdb_assert (cu->die_hash == NULL);
6708 cu->die_hash =
6709 htab_create_alloc_ex (cu->header.length / 12,
6710 die_hash,
6711 die_eq,
6712 NULL,
6713 &cu->comp_unit_obstack,
6714 hashtab_obstack_allocate,
6715 dummy_obstack_deallocate);
6716
6717 if (has_children)
6718 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
6719 &info_ptr, comp_unit_die);
6720 cu->dies = comp_unit_die;
6721 /* comp_unit_die is not stored in die_hash, no need. */
6722
6723 /* We try not to read any attributes in this function, because not
6724 all CUs needed for references have been loaded yet, and symbol
6725 table processing isn't initialized. But we have to set the CU language,
6726 or we won't be able to build types correctly.
6727 Similarly, if we do not read the producer, we can not apply
6728 producer-specific interpretation. */
6729 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
6730 }
6731
6732 /* Load the DIEs associated with PER_CU into memory. */
6733
6734 static void
6735 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
6736 enum language pretend_language)
6737 {
6738 gdb_assert (! this_cu->is_debug_types);
6739
6740 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6741 load_full_comp_unit_reader, &pretend_language);
6742 }
6743
6744 /* Add a DIE to the delayed physname list. */
6745
6746 static void
6747 add_to_method_list (struct type *type, int fnfield_index, int index,
6748 const char *name, struct die_info *die,
6749 struct dwarf2_cu *cu)
6750 {
6751 struct delayed_method_info mi;
6752 mi.type = type;
6753 mi.fnfield_index = fnfield_index;
6754 mi.index = index;
6755 mi.name = name;
6756 mi.die = die;
6757 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
6758 }
6759
6760 /* A cleanup for freeing the delayed method list. */
6761
6762 static void
6763 free_delayed_list (void *ptr)
6764 {
6765 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
6766 if (cu->method_list != NULL)
6767 {
6768 VEC_free (delayed_method_info, cu->method_list);
6769 cu->method_list = NULL;
6770 }
6771 }
6772
6773 /* Compute the physnames of any methods on the CU's method list.
6774
6775 The computation of method physnames is delayed in order to avoid the
6776 (bad) condition that one of the method's formal parameters is of an as yet
6777 incomplete type. */
6778
6779 static void
6780 compute_delayed_physnames (struct dwarf2_cu *cu)
6781 {
6782 int i;
6783 struct delayed_method_info *mi;
6784 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
6785 {
6786 const char *physname;
6787 struct fn_fieldlist *fn_flp
6788 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
6789 physname = dwarf2_physname (mi->name, mi->die, cu);
6790 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
6791 }
6792 }
6793
6794 /* Go objects should be embedded in a DW_TAG_module DIE,
6795 and it's not clear if/how imported objects will appear.
6796 To keep Go support simple until that's worked out,
6797 go back through what we've read and create something usable.
6798 We could do this while processing each DIE, and feels kinda cleaner,
6799 but that way is more invasive.
6800 This is to, for example, allow the user to type "p var" or "b main"
6801 without having to specify the package name, and allow lookups
6802 of module.object to work in contexts that use the expression
6803 parser. */
6804
6805 static void
6806 fixup_go_packaging (struct dwarf2_cu *cu)
6807 {
6808 char *package_name = NULL;
6809 struct pending *list;
6810 int i;
6811
6812 for (list = global_symbols; list != NULL; list = list->next)
6813 {
6814 for (i = 0; i < list->nsyms; ++i)
6815 {
6816 struct symbol *sym = list->symbol[i];
6817
6818 if (SYMBOL_LANGUAGE (sym) == language_go
6819 && SYMBOL_CLASS (sym) == LOC_BLOCK)
6820 {
6821 char *this_package_name = go_symbol_package_name (sym);
6822
6823 if (this_package_name == NULL)
6824 continue;
6825 if (package_name == NULL)
6826 package_name = this_package_name;
6827 else
6828 {
6829 if (strcmp (package_name, this_package_name) != 0)
6830 complaint (&symfile_complaints,
6831 _("Symtab %s has objects from two different Go packages: %s and %s"),
6832 (SYMBOL_SYMTAB (sym)
6833 ? symtab_to_filename_for_display (SYMBOL_SYMTAB (sym))
6834 : cu->objfile->name),
6835 this_package_name, package_name);
6836 xfree (this_package_name);
6837 }
6838 }
6839 }
6840 }
6841
6842 if (package_name != NULL)
6843 {
6844 struct objfile *objfile = cu->objfile;
6845 const char *saved_package_name = obstack_copy0 (&objfile->objfile_obstack,
6846 package_name,
6847 strlen (package_name));
6848 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
6849 saved_package_name, objfile);
6850 struct symbol *sym;
6851
6852 TYPE_TAG_NAME (type) = TYPE_NAME (type);
6853
6854 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
6855 SYMBOL_SET_LANGUAGE (sym, language_go);
6856 SYMBOL_SET_NAMES (sym, saved_package_name,
6857 strlen (saved_package_name), 0, objfile);
6858 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
6859 e.g., "main" finds the "main" module and not C's main(). */
6860 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
6861 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
6862 SYMBOL_TYPE (sym) = type;
6863
6864 add_symbol_to_list (sym, &global_symbols);
6865
6866 xfree (package_name);
6867 }
6868 }
6869
6870 static void compute_symtab_includes (struct dwarf2_per_cu_data *per_cu);
6871
6872 /* Return the symtab for PER_CU. This works properly regardless of
6873 whether we're using the index or psymtabs. */
6874
6875 static struct symtab *
6876 get_symtab (struct dwarf2_per_cu_data *per_cu)
6877 {
6878 return (dwarf2_per_objfile->using_index
6879 ? per_cu->v.quick->symtab
6880 : per_cu->v.psymtab->symtab);
6881 }
6882
6883 /* A helper function for computing the list of all symbol tables
6884 included by PER_CU. */
6885
6886 static void
6887 recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
6888 htab_t all_children,
6889 struct dwarf2_per_cu_data *per_cu)
6890 {
6891 void **slot;
6892 int ix;
6893 struct dwarf2_per_cu_data *iter;
6894
6895 slot = htab_find_slot (all_children, per_cu, INSERT);
6896 if (*slot != NULL)
6897 {
6898 /* This inclusion and its children have been processed. */
6899 return;
6900 }
6901
6902 *slot = per_cu;
6903 /* Only add a CU if it has a symbol table. */
6904 if (get_symtab (per_cu) != NULL)
6905 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
6906
6907 for (ix = 0;
6908 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
6909 ++ix)
6910 recursively_compute_inclusions (result, all_children, iter);
6911 }
6912
6913 /* Compute the symtab 'includes' fields for the symtab related to
6914 PER_CU. */
6915
6916 static void
6917 compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
6918 {
6919 gdb_assert (! per_cu->is_debug_types);
6920
6921 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
6922 {
6923 int ix, len;
6924 struct dwarf2_per_cu_data *iter;
6925 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
6926 htab_t all_children;
6927 struct symtab *symtab = get_symtab (per_cu);
6928
6929 /* If we don't have a symtab, we can just skip this case. */
6930 if (symtab == NULL)
6931 return;
6932
6933 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
6934 NULL, xcalloc, xfree);
6935
6936 for (ix = 0;
6937 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
6938 ix, iter);
6939 ++ix)
6940 recursively_compute_inclusions (&result_children, all_children, iter);
6941
6942 /* Now we have a transitive closure of all the included CUs, and
6943 for .gdb_index version 7 the included TUs, so we can convert it
6944 to a list of symtabs. */
6945 len = VEC_length (dwarf2_per_cu_ptr, result_children);
6946 symtab->includes
6947 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
6948 (len + 1) * sizeof (struct symtab *));
6949 for (ix = 0;
6950 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
6951 ++ix)
6952 symtab->includes[ix] = get_symtab (iter);
6953 symtab->includes[len] = NULL;
6954
6955 VEC_free (dwarf2_per_cu_ptr, result_children);
6956 htab_delete (all_children);
6957 }
6958 }
6959
6960 /* Compute the 'includes' field for the symtabs of all the CUs we just
6961 read. */
6962
6963 static void
6964 process_cu_includes (void)
6965 {
6966 int ix;
6967 struct dwarf2_per_cu_data *iter;
6968
6969 for (ix = 0;
6970 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
6971 ix, iter);
6972 ++ix)
6973 {
6974 if (! iter->is_debug_types)
6975 compute_symtab_includes (iter);
6976 }
6977
6978 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
6979 }
6980
6981 /* Generate full symbol information for PER_CU, whose DIEs have
6982 already been loaded into memory. */
6983
6984 static void
6985 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
6986 enum language pretend_language)
6987 {
6988 struct dwarf2_cu *cu = per_cu->cu;
6989 struct objfile *objfile = per_cu->objfile;
6990 CORE_ADDR lowpc, highpc;
6991 struct symtab *symtab;
6992 struct cleanup *back_to, *delayed_list_cleanup;
6993 CORE_ADDR baseaddr;
6994 struct block *static_block;
6995
6996 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6997
6998 buildsym_init ();
6999 back_to = make_cleanup (really_free_pendings, NULL);
7000 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7001
7002 cu->list_in_scope = &file_symbols;
7003
7004 cu->language = pretend_language;
7005 cu->language_defn = language_def (cu->language);
7006
7007 /* Do line number decoding in read_file_scope () */
7008 process_die (cu->dies, cu);
7009
7010 /* For now fudge the Go package. */
7011 if (cu->language == language_go)
7012 fixup_go_packaging (cu);
7013
7014 /* Now that we have processed all the DIEs in the CU, all the types
7015 should be complete, and it should now be safe to compute all of the
7016 physnames. */
7017 compute_delayed_physnames (cu);
7018 do_cleanups (delayed_list_cleanup);
7019
7020 /* Some compilers don't define a DW_AT_high_pc attribute for the
7021 compilation unit. If the DW_AT_high_pc is missing, synthesize
7022 it, by scanning the DIE's below the compilation unit. */
7023 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
7024
7025 static_block
7026 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0,
7027 per_cu->imported_symtabs != NULL);
7028
7029 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
7030 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
7031 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
7032 addrmap to help ensure it has an accurate map of pc values belonging to
7033 this comp unit. */
7034 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
7035
7036 symtab = end_symtab_from_static_block (static_block, objfile,
7037 SECT_OFF_TEXT (objfile), 0);
7038
7039 if (symtab != NULL)
7040 {
7041 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
7042
7043 /* Set symtab language to language from DW_AT_language. If the
7044 compilation is from a C file generated by language preprocessors, do
7045 not set the language if it was already deduced by start_subfile. */
7046 if (!(cu->language == language_c && symtab->language != language_c))
7047 symtab->language = cu->language;
7048
7049 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
7050 produce DW_AT_location with location lists but it can be possibly
7051 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
7052 there were bugs in prologue debug info, fixed later in GCC-4.5
7053 by "unwind info for epilogues" patch (which is not directly related).
7054
7055 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
7056 needed, it would be wrong due to missing DW_AT_producer there.
7057
7058 Still one can confuse GDB by using non-standard GCC compilation
7059 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
7060 */
7061 if (cu->has_loclist && gcc_4_minor >= 5)
7062 symtab->locations_valid = 1;
7063
7064 if (gcc_4_minor >= 5)
7065 symtab->epilogue_unwind_valid = 1;
7066
7067 symtab->call_site_htab = cu->call_site_htab;
7068 }
7069
7070 if (dwarf2_per_objfile->using_index)
7071 per_cu->v.quick->symtab = symtab;
7072 else
7073 {
7074 struct partial_symtab *pst = per_cu->v.psymtab;
7075 pst->symtab = symtab;
7076 pst->readin = 1;
7077 }
7078
7079 /* Push it for inclusion processing later. */
7080 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
7081
7082 do_cleanups (back_to);
7083 }
7084
7085 /* Generate full symbol information for type unit PER_CU, whose DIEs have
7086 already been loaded into memory. */
7087
7088 static void
7089 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
7090 enum language pretend_language)
7091 {
7092 struct dwarf2_cu *cu = per_cu->cu;
7093 struct objfile *objfile = per_cu->objfile;
7094 struct symtab *symtab;
7095 struct cleanup *back_to, *delayed_list_cleanup;
7096
7097 buildsym_init ();
7098 back_to = make_cleanup (really_free_pendings, NULL);
7099 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7100
7101 cu->list_in_scope = &file_symbols;
7102
7103 cu->language = pretend_language;
7104 cu->language_defn = language_def (cu->language);
7105
7106 /* The symbol tables are set up in read_type_unit_scope. */
7107 process_die (cu->dies, cu);
7108
7109 /* For now fudge the Go package. */
7110 if (cu->language == language_go)
7111 fixup_go_packaging (cu);
7112
7113 /* Now that we have processed all the DIEs in the CU, all the types
7114 should be complete, and it should now be safe to compute all of the
7115 physnames. */
7116 compute_delayed_physnames (cu);
7117 do_cleanups (delayed_list_cleanup);
7118
7119 /* TUs share symbol tables.
7120 If this is the first TU to use this symtab, complete the construction
7121 of it with end_expandable_symtab. Otherwise, complete the addition of
7122 this TU's symbols to the existing symtab. */
7123 if (per_cu->type_unit_group->primary_symtab == NULL)
7124 {
7125 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
7126 per_cu->type_unit_group->primary_symtab = symtab;
7127
7128 if (symtab != NULL)
7129 {
7130 /* Set symtab language to language from DW_AT_language. If the
7131 compilation is from a C file generated by language preprocessors,
7132 do not set the language if it was already deduced by
7133 start_subfile. */
7134 if (!(cu->language == language_c && symtab->language != language_c))
7135 symtab->language = cu->language;
7136 }
7137 }
7138 else
7139 {
7140 augment_type_symtab (objfile,
7141 per_cu->type_unit_group->primary_symtab);
7142 symtab = per_cu->type_unit_group->primary_symtab;
7143 }
7144
7145 if (dwarf2_per_objfile->using_index)
7146 per_cu->v.quick->symtab = symtab;
7147 else
7148 {
7149 struct partial_symtab *pst = per_cu->v.psymtab;
7150 pst->symtab = symtab;
7151 pst->readin = 1;
7152 }
7153
7154 do_cleanups (back_to);
7155 }
7156
7157 /* Process an imported unit DIE. */
7158
7159 static void
7160 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7161 {
7162 struct attribute *attr;
7163
7164 /* For now we don't handle imported units in type units. */
7165 if (cu->per_cu->is_debug_types)
7166 {
7167 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7168 " supported in type units [in module %s]"),
7169 cu->objfile->name);
7170 }
7171
7172 attr = dwarf2_attr (die, DW_AT_import, cu);
7173 if (attr != NULL)
7174 {
7175 struct dwarf2_per_cu_data *per_cu;
7176 struct symtab *imported_symtab;
7177 sect_offset offset;
7178 int is_dwz;
7179
7180 offset = dwarf2_get_ref_die_offset (attr);
7181 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7182 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
7183
7184 /* Queue the unit, if needed. */
7185 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7186 load_full_comp_unit (per_cu, cu->language);
7187
7188 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
7189 per_cu);
7190 }
7191 }
7192
7193 /* Process a die and its children. */
7194
7195 static void
7196 process_die (struct die_info *die, struct dwarf2_cu *cu)
7197 {
7198 switch (die->tag)
7199 {
7200 case DW_TAG_padding:
7201 break;
7202 case DW_TAG_compile_unit:
7203 case DW_TAG_partial_unit:
7204 read_file_scope (die, cu);
7205 break;
7206 case DW_TAG_type_unit:
7207 read_type_unit_scope (die, cu);
7208 break;
7209 case DW_TAG_subprogram:
7210 case DW_TAG_inlined_subroutine:
7211 read_func_scope (die, cu);
7212 break;
7213 case DW_TAG_lexical_block:
7214 case DW_TAG_try_block:
7215 case DW_TAG_catch_block:
7216 read_lexical_block_scope (die, cu);
7217 break;
7218 case DW_TAG_GNU_call_site:
7219 read_call_site_scope (die, cu);
7220 break;
7221 case DW_TAG_class_type:
7222 case DW_TAG_interface_type:
7223 case DW_TAG_structure_type:
7224 case DW_TAG_union_type:
7225 process_structure_scope (die, cu);
7226 break;
7227 case DW_TAG_enumeration_type:
7228 process_enumeration_scope (die, cu);
7229 break;
7230
7231 /* These dies have a type, but processing them does not create
7232 a symbol or recurse to process the children. Therefore we can
7233 read them on-demand through read_type_die. */
7234 case DW_TAG_subroutine_type:
7235 case DW_TAG_set_type:
7236 case DW_TAG_array_type:
7237 case DW_TAG_pointer_type:
7238 case DW_TAG_ptr_to_member_type:
7239 case DW_TAG_reference_type:
7240 case DW_TAG_string_type:
7241 break;
7242
7243 case DW_TAG_base_type:
7244 case DW_TAG_subrange_type:
7245 case DW_TAG_typedef:
7246 /* Add a typedef symbol for the type definition, if it has a
7247 DW_AT_name. */
7248 new_symbol (die, read_type_die (die, cu), cu);
7249 break;
7250 case DW_TAG_common_block:
7251 read_common_block (die, cu);
7252 break;
7253 case DW_TAG_common_inclusion:
7254 break;
7255 case DW_TAG_namespace:
7256 cu->processing_has_namespace_info = 1;
7257 read_namespace (die, cu);
7258 break;
7259 case DW_TAG_module:
7260 cu->processing_has_namespace_info = 1;
7261 read_module (die, cu);
7262 break;
7263 case DW_TAG_imported_declaration:
7264 case DW_TAG_imported_module:
7265 cu->processing_has_namespace_info = 1;
7266 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7267 || cu->language != language_fortran))
7268 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7269 dwarf_tag_name (die->tag));
7270 read_import_statement (die, cu);
7271 break;
7272
7273 case DW_TAG_imported_unit:
7274 process_imported_unit_die (die, cu);
7275 break;
7276
7277 default:
7278 new_symbol (die, NULL, cu);
7279 break;
7280 }
7281 }
7282
7283 /* A helper function for dwarf2_compute_name which determines whether DIE
7284 needs to have the name of the scope prepended to the name listed in the
7285 die. */
7286
7287 static int
7288 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7289 {
7290 struct attribute *attr;
7291
7292 switch (die->tag)
7293 {
7294 case DW_TAG_namespace:
7295 case DW_TAG_typedef:
7296 case DW_TAG_class_type:
7297 case DW_TAG_interface_type:
7298 case DW_TAG_structure_type:
7299 case DW_TAG_union_type:
7300 case DW_TAG_enumeration_type:
7301 case DW_TAG_enumerator:
7302 case DW_TAG_subprogram:
7303 case DW_TAG_member:
7304 return 1;
7305
7306 case DW_TAG_variable:
7307 case DW_TAG_constant:
7308 /* We only need to prefix "globally" visible variables. These include
7309 any variable marked with DW_AT_external or any variable that
7310 lives in a namespace. [Variables in anonymous namespaces
7311 require prefixing, but they are not DW_AT_external.] */
7312
7313 if (dwarf2_attr (die, DW_AT_specification, cu))
7314 {
7315 struct dwarf2_cu *spec_cu = cu;
7316
7317 return die_needs_namespace (die_specification (die, &spec_cu),
7318 spec_cu);
7319 }
7320
7321 attr = dwarf2_attr (die, DW_AT_external, cu);
7322 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7323 && die->parent->tag != DW_TAG_module)
7324 return 0;
7325 /* A variable in a lexical block of some kind does not need a
7326 namespace, even though in C++ such variables may be external
7327 and have a mangled name. */
7328 if (die->parent->tag == DW_TAG_lexical_block
7329 || die->parent->tag == DW_TAG_try_block
7330 || die->parent->tag == DW_TAG_catch_block
7331 || die->parent->tag == DW_TAG_subprogram)
7332 return 0;
7333 return 1;
7334
7335 default:
7336 return 0;
7337 }
7338 }
7339
7340 /* Retrieve the last character from a mem_file. */
7341
7342 static void
7343 do_ui_file_peek_last (void *object, const char *buffer, long length)
7344 {
7345 char *last_char_p = (char *) object;
7346
7347 if (length > 0)
7348 *last_char_p = buffer[length - 1];
7349 }
7350
7351 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
7352 compute the physname for the object, which include a method's:
7353 - formal parameters (C++/Java),
7354 - receiver type (Go),
7355 - return type (Java).
7356
7357 The term "physname" is a bit confusing.
7358 For C++, for example, it is the demangled name.
7359 For Go, for example, it's the mangled name.
7360
7361 For Ada, return the DIE's linkage name rather than the fully qualified
7362 name. PHYSNAME is ignored..
7363
7364 The result is allocated on the objfile_obstack and canonicalized. */
7365
7366 static const char *
7367 dwarf2_compute_name (const char *name,
7368 struct die_info *die, struct dwarf2_cu *cu,
7369 int physname)
7370 {
7371 struct objfile *objfile = cu->objfile;
7372
7373 if (name == NULL)
7374 name = dwarf2_name (die, cu);
7375
7376 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7377 compute it by typename_concat inside GDB. */
7378 if (cu->language == language_ada
7379 || (cu->language == language_fortran && physname))
7380 {
7381 /* For Ada unit, we prefer the linkage name over the name, as
7382 the former contains the exported name, which the user expects
7383 to be able to reference. Ideally, we want the user to be able
7384 to reference this entity using either natural or linkage name,
7385 but we haven't started looking at this enhancement yet. */
7386 struct attribute *attr;
7387
7388 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7389 if (attr == NULL)
7390 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7391 if (attr && DW_STRING (attr))
7392 return DW_STRING (attr);
7393 }
7394
7395 /* These are the only languages we know how to qualify names in. */
7396 if (name != NULL
7397 && (cu->language == language_cplus || cu->language == language_java
7398 || cu->language == language_fortran))
7399 {
7400 if (die_needs_namespace (die, cu))
7401 {
7402 long length;
7403 const char *prefix;
7404 struct ui_file *buf;
7405
7406 prefix = determine_prefix (die, cu);
7407 buf = mem_fileopen ();
7408 if (*prefix != '\0')
7409 {
7410 char *prefixed_name = typename_concat (NULL, prefix, name,
7411 physname, cu);
7412
7413 fputs_unfiltered (prefixed_name, buf);
7414 xfree (prefixed_name);
7415 }
7416 else
7417 fputs_unfiltered (name, buf);
7418
7419 /* Template parameters may be specified in the DIE's DW_AT_name, or
7420 as children with DW_TAG_template_type_param or
7421 DW_TAG_value_type_param. If the latter, add them to the name
7422 here. If the name already has template parameters, then
7423 skip this step; some versions of GCC emit both, and
7424 it is more efficient to use the pre-computed name.
7425
7426 Something to keep in mind about this process: it is very
7427 unlikely, or in some cases downright impossible, to produce
7428 something that will match the mangled name of a function.
7429 If the definition of the function has the same debug info,
7430 we should be able to match up with it anyway. But fallbacks
7431 using the minimal symbol, for instance to find a method
7432 implemented in a stripped copy of libstdc++, will not work.
7433 If we do not have debug info for the definition, we will have to
7434 match them up some other way.
7435
7436 When we do name matching there is a related problem with function
7437 templates; two instantiated function templates are allowed to
7438 differ only by their return types, which we do not add here. */
7439
7440 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7441 {
7442 struct attribute *attr;
7443 struct die_info *child;
7444 int first = 1;
7445
7446 die->building_fullname = 1;
7447
7448 for (child = die->child; child != NULL; child = child->sibling)
7449 {
7450 struct type *type;
7451 LONGEST value;
7452 gdb_byte *bytes;
7453 struct dwarf2_locexpr_baton *baton;
7454 struct value *v;
7455
7456 if (child->tag != DW_TAG_template_type_param
7457 && child->tag != DW_TAG_template_value_param)
7458 continue;
7459
7460 if (first)
7461 {
7462 fputs_unfiltered ("<", buf);
7463 first = 0;
7464 }
7465 else
7466 fputs_unfiltered (", ", buf);
7467
7468 attr = dwarf2_attr (child, DW_AT_type, cu);
7469 if (attr == NULL)
7470 {
7471 complaint (&symfile_complaints,
7472 _("template parameter missing DW_AT_type"));
7473 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7474 continue;
7475 }
7476 type = die_type (child, cu);
7477
7478 if (child->tag == DW_TAG_template_type_param)
7479 {
7480 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
7481 continue;
7482 }
7483
7484 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7485 if (attr == NULL)
7486 {
7487 complaint (&symfile_complaints,
7488 _("template parameter missing "
7489 "DW_AT_const_value"));
7490 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7491 continue;
7492 }
7493
7494 dwarf2_const_value_attr (attr, type, name,
7495 &cu->comp_unit_obstack, cu,
7496 &value, &bytes, &baton);
7497
7498 if (TYPE_NOSIGN (type))
7499 /* GDB prints characters as NUMBER 'CHAR'. If that's
7500 changed, this can use value_print instead. */
7501 c_printchar (value, type, buf);
7502 else
7503 {
7504 struct value_print_options opts;
7505
7506 if (baton != NULL)
7507 v = dwarf2_evaluate_loc_desc (type, NULL,
7508 baton->data,
7509 baton->size,
7510 baton->per_cu);
7511 else if (bytes != NULL)
7512 {
7513 v = allocate_value (type);
7514 memcpy (value_contents_writeable (v), bytes,
7515 TYPE_LENGTH (type));
7516 }
7517 else
7518 v = value_from_longest (type, value);
7519
7520 /* Specify decimal so that we do not depend on
7521 the radix. */
7522 get_formatted_print_options (&opts, 'd');
7523 opts.raw = 1;
7524 value_print (v, buf, &opts);
7525 release_value (v);
7526 value_free (v);
7527 }
7528 }
7529
7530 die->building_fullname = 0;
7531
7532 if (!first)
7533 {
7534 /* Close the argument list, with a space if necessary
7535 (nested templates). */
7536 char last_char = '\0';
7537 ui_file_put (buf, do_ui_file_peek_last, &last_char);
7538 if (last_char == '>')
7539 fputs_unfiltered (" >", buf);
7540 else
7541 fputs_unfiltered (">", buf);
7542 }
7543 }
7544
7545 /* For Java and C++ methods, append formal parameter type
7546 information, if PHYSNAME. */
7547
7548 if (physname && die->tag == DW_TAG_subprogram
7549 && (cu->language == language_cplus
7550 || cu->language == language_java))
7551 {
7552 struct type *type = read_type_die (die, cu);
7553
7554 c_type_print_args (type, buf, 1, cu->language,
7555 &type_print_raw_options);
7556
7557 if (cu->language == language_java)
7558 {
7559 /* For java, we must append the return type to method
7560 names. */
7561 if (die->tag == DW_TAG_subprogram)
7562 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
7563 0, 0, &type_print_raw_options);
7564 }
7565 else if (cu->language == language_cplus)
7566 {
7567 /* Assume that an artificial first parameter is
7568 "this", but do not crash if it is not. RealView
7569 marks unnamed (and thus unused) parameters as
7570 artificial; there is no way to differentiate
7571 the two cases. */
7572 if (TYPE_NFIELDS (type) > 0
7573 && TYPE_FIELD_ARTIFICIAL (type, 0)
7574 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
7575 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
7576 0))))
7577 fputs_unfiltered (" const", buf);
7578 }
7579 }
7580
7581 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
7582 &length);
7583 ui_file_delete (buf);
7584
7585 if (cu->language == language_cplus)
7586 {
7587 const char *cname
7588 = dwarf2_canonicalize_name (name, cu,
7589 &objfile->objfile_obstack);
7590
7591 if (cname != NULL)
7592 name = cname;
7593 }
7594 }
7595 }
7596
7597 return name;
7598 }
7599
7600 /* Return the fully qualified name of DIE, based on its DW_AT_name.
7601 If scope qualifiers are appropriate they will be added. The result
7602 will be allocated on the objfile_obstack, or NULL if the DIE does
7603 not have a name. NAME may either be from a previous call to
7604 dwarf2_name or NULL.
7605
7606 The output string will be canonicalized (if C++/Java). */
7607
7608 static const char *
7609 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
7610 {
7611 return dwarf2_compute_name (name, die, cu, 0);
7612 }
7613
7614 /* Construct a physname for the given DIE in CU. NAME may either be
7615 from a previous call to dwarf2_name or NULL. The result will be
7616 allocated on the objfile_objstack or NULL if the DIE does not have a
7617 name.
7618
7619 The output string will be canonicalized (if C++/Java). */
7620
7621 static const char *
7622 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
7623 {
7624 struct objfile *objfile = cu->objfile;
7625 struct attribute *attr;
7626 const char *retval, *mangled = NULL, *canon = NULL;
7627 struct cleanup *back_to;
7628 int need_copy = 1;
7629
7630 /* In this case dwarf2_compute_name is just a shortcut not building anything
7631 on its own. */
7632 if (!die_needs_namespace (die, cu))
7633 return dwarf2_compute_name (name, die, cu, 1);
7634
7635 back_to = make_cleanup (null_cleanup, NULL);
7636
7637 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7638 if (!attr)
7639 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7640
7641 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
7642 has computed. */
7643 if (attr && DW_STRING (attr))
7644 {
7645 char *demangled;
7646
7647 mangled = DW_STRING (attr);
7648
7649 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
7650 type. It is easier for GDB users to search for such functions as
7651 `name(params)' than `long name(params)'. In such case the minimal
7652 symbol names do not match the full symbol names but for template
7653 functions there is never a need to look up their definition from their
7654 declaration so the only disadvantage remains the minimal symbol
7655 variant `long name(params)' does not have the proper inferior type.
7656 */
7657
7658 if (cu->language == language_go)
7659 {
7660 /* This is a lie, but we already lie to the caller new_symbol_full.
7661 new_symbol_full assumes we return the mangled name.
7662 This just undoes that lie until things are cleaned up. */
7663 demangled = NULL;
7664 }
7665 else
7666 {
7667 demangled = cplus_demangle (mangled,
7668 (DMGL_PARAMS | DMGL_ANSI
7669 | (cu->language == language_java
7670 ? DMGL_JAVA | DMGL_RET_POSTFIX
7671 : DMGL_RET_DROP)));
7672 }
7673 if (demangled)
7674 {
7675 make_cleanup (xfree, demangled);
7676 canon = demangled;
7677 }
7678 else
7679 {
7680 canon = mangled;
7681 need_copy = 0;
7682 }
7683 }
7684
7685 if (canon == NULL || check_physname)
7686 {
7687 const char *physname = dwarf2_compute_name (name, die, cu, 1);
7688
7689 if (canon != NULL && strcmp (physname, canon) != 0)
7690 {
7691 /* It may not mean a bug in GDB. The compiler could also
7692 compute DW_AT_linkage_name incorrectly. But in such case
7693 GDB would need to be bug-to-bug compatible. */
7694
7695 complaint (&symfile_complaints,
7696 _("Computed physname <%s> does not match demangled <%s> "
7697 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
7698 physname, canon, mangled, die->offset.sect_off, objfile->name);
7699
7700 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
7701 is available here - over computed PHYSNAME. It is safer
7702 against both buggy GDB and buggy compilers. */
7703
7704 retval = canon;
7705 }
7706 else
7707 {
7708 retval = physname;
7709 need_copy = 0;
7710 }
7711 }
7712 else
7713 retval = canon;
7714
7715 if (need_copy)
7716 retval = obstack_copy0 (&objfile->objfile_obstack, retval, strlen (retval));
7717
7718 do_cleanups (back_to);
7719 return retval;
7720 }
7721
7722 /* Read the import statement specified by the given die and record it. */
7723
7724 static void
7725 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
7726 {
7727 struct objfile *objfile = cu->objfile;
7728 struct attribute *import_attr;
7729 struct die_info *imported_die, *child_die;
7730 struct dwarf2_cu *imported_cu;
7731 const char *imported_name;
7732 const char *imported_name_prefix;
7733 const char *canonical_name;
7734 const char *import_alias;
7735 const char *imported_declaration = NULL;
7736 const char *import_prefix;
7737 VEC (const_char_ptr) *excludes = NULL;
7738 struct cleanup *cleanups;
7739
7740 import_attr = dwarf2_attr (die, DW_AT_import, cu);
7741 if (import_attr == NULL)
7742 {
7743 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7744 dwarf_tag_name (die->tag));
7745 return;
7746 }
7747
7748 imported_cu = cu;
7749 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
7750 imported_name = dwarf2_name (imported_die, imported_cu);
7751 if (imported_name == NULL)
7752 {
7753 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
7754
7755 The import in the following code:
7756 namespace A
7757 {
7758 typedef int B;
7759 }
7760
7761 int main ()
7762 {
7763 using A::B;
7764 B b;
7765 return b;
7766 }
7767
7768 ...
7769 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
7770 <52> DW_AT_decl_file : 1
7771 <53> DW_AT_decl_line : 6
7772 <54> DW_AT_import : <0x75>
7773 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
7774 <59> DW_AT_name : B
7775 <5b> DW_AT_decl_file : 1
7776 <5c> DW_AT_decl_line : 2
7777 <5d> DW_AT_type : <0x6e>
7778 ...
7779 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
7780 <76> DW_AT_byte_size : 4
7781 <77> DW_AT_encoding : 5 (signed)
7782
7783 imports the wrong die ( 0x75 instead of 0x58 ).
7784 This case will be ignored until the gcc bug is fixed. */
7785 return;
7786 }
7787
7788 /* Figure out the local name after import. */
7789 import_alias = dwarf2_name (die, cu);
7790
7791 /* Figure out where the statement is being imported to. */
7792 import_prefix = determine_prefix (die, cu);
7793
7794 /* Figure out what the scope of the imported die is and prepend it
7795 to the name of the imported die. */
7796 imported_name_prefix = determine_prefix (imported_die, imported_cu);
7797
7798 if (imported_die->tag != DW_TAG_namespace
7799 && imported_die->tag != DW_TAG_module)
7800 {
7801 imported_declaration = imported_name;
7802 canonical_name = imported_name_prefix;
7803 }
7804 else if (strlen (imported_name_prefix) > 0)
7805 canonical_name = obconcat (&objfile->objfile_obstack,
7806 imported_name_prefix, "::", imported_name,
7807 (char *) NULL);
7808 else
7809 canonical_name = imported_name;
7810
7811 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
7812
7813 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
7814 for (child_die = die->child; child_die && child_die->tag;
7815 child_die = sibling_die (child_die))
7816 {
7817 /* DWARF-4: A Fortran use statement with a “rename list” may be
7818 represented by an imported module entry with an import attribute
7819 referring to the module and owned entries corresponding to those
7820 entities that are renamed as part of being imported. */
7821
7822 if (child_die->tag != DW_TAG_imported_declaration)
7823 {
7824 complaint (&symfile_complaints,
7825 _("child DW_TAG_imported_declaration expected "
7826 "- DIE at 0x%x [in module %s]"),
7827 child_die->offset.sect_off, objfile->name);
7828 continue;
7829 }
7830
7831 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
7832 if (import_attr == NULL)
7833 {
7834 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7835 dwarf_tag_name (child_die->tag));
7836 continue;
7837 }
7838
7839 imported_cu = cu;
7840 imported_die = follow_die_ref_or_sig (child_die, import_attr,
7841 &imported_cu);
7842 imported_name = dwarf2_name (imported_die, imported_cu);
7843 if (imported_name == NULL)
7844 {
7845 complaint (&symfile_complaints,
7846 _("child DW_TAG_imported_declaration has unknown "
7847 "imported name - DIE at 0x%x [in module %s]"),
7848 child_die->offset.sect_off, objfile->name);
7849 continue;
7850 }
7851
7852 VEC_safe_push (const_char_ptr, excludes, imported_name);
7853
7854 process_die (child_die, cu);
7855 }
7856
7857 cp_add_using_directive (import_prefix,
7858 canonical_name,
7859 import_alias,
7860 imported_declaration,
7861 excludes,
7862 0,
7863 &objfile->objfile_obstack);
7864
7865 do_cleanups (cleanups);
7866 }
7867
7868 /* Cleanup function for handle_DW_AT_stmt_list. */
7869
7870 static void
7871 free_cu_line_header (void *arg)
7872 {
7873 struct dwarf2_cu *cu = arg;
7874
7875 free_line_header (cu->line_header);
7876 cu->line_header = NULL;
7877 }
7878
7879 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
7880 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
7881 this, it was first present in GCC release 4.3.0. */
7882
7883 static int
7884 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
7885 {
7886 if (!cu->checked_producer)
7887 check_producer (cu);
7888
7889 return cu->producer_is_gcc_lt_4_3;
7890 }
7891
7892 static void
7893 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
7894 const char **name, const char **comp_dir)
7895 {
7896 struct attribute *attr;
7897
7898 *name = NULL;
7899 *comp_dir = NULL;
7900
7901 /* Find the filename. Do not use dwarf2_name here, since the filename
7902 is not a source language identifier. */
7903 attr = dwarf2_attr (die, DW_AT_name, cu);
7904 if (attr)
7905 {
7906 *name = DW_STRING (attr);
7907 }
7908
7909 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
7910 if (attr)
7911 *comp_dir = DW_STRING (attr);
7912 else if (producer_is_gcc_lt_4_3 (cu) && *name != NULL
7913 && IS_ABSOLUTE_PATH (*name))
7914 {
7915 char *d = ldirname (*name);
7916
7917 *comp_dir = d;
7918 if (d != NULL)
7919 make_cleanup (xfree, d);
7920 }
7921 if (*comp_dir != NULL)
7922 {
7923 /* Irix 6.2 native cc prepends <machine>.: to the compilation
7924 directory, get rid of it. */
7925 char *cp = strchr (*comp_dir, ':');
7926
7927 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
7928 *comp_dir = cp + 1;
7929 }
7930
7931 if (*name == NULL)
7932 *name = "<unknown>";
7933 }
7934
7935 /* Handle DW_AT_stmt_list for a compilation unit.
7936 DIE is the DW_TAG_compile_unit die for CU.
7937 COMP_DIR is the compilation directory.
7938 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
7939
7940 static void
7941 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
7942 const char *comp_dir)
7943 {
7944 struct attribute *attr;
7945
7946 gdb_assert (! cu->per_cu->is_debug_types);
7947
7948 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7949 if (attr)
7950 {
7951 unsigned int line_offset = DW_UNSND (attr);
7952 struct line_header *line_header
7953 = dwarf_decode_line_header (line_offset, cu);
7954
7955 if (line_header)
7956 {
7957 cu->line_header = line_header;
7958 make_cleanup (free_cu_line_header, cu);
7959 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
7960 }
7961 }
7962 }
7963
7964 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
7965
7966 static void
7967 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
7968 {
7969 struct objfile *objfile = dwarf2_per_objfile->objfile;
7970 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
7971 CORE_ADDR lowpc = ((CORE_ADDR) -1);
7972 CORE_ADDR highpc = ((CORE_ADDR) 0);
7973 struct attribute *attr;
7974 const char *name = NULL;
7975 const char *comp_dir = NULL;
7976 struct die_info *child_die;
7977 bfd *abfd = objfile->obfd;
7978 CORE_ADDR baseaddr;
7979
7980 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7981
7982 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
7983
7984 /* If we didn't find a lowpc, set it to highpc to avoid complaints
7985 from finish_block. */
7986 if (lowpc == ((CORE_ADDR) -1))
7987 lowpc = highpc;
7988 lowpc += baseaddr;
7989 highpc += baseaddr;
7990
7991 find_file_and_directory (die, cu, &name, &comp_dir);
7992
7993 prepare_one_comp_unit (cu, die, cu->language);
7994
7995 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
7996 standardised yet. As a workaround for the language detection we fall
7997 back to the DW_AT_producer string. */
7998 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
7999 cu->language = language_opencl;
8000
8001 /* Similar hack for Go. */
8002 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
8003 set_cu_language (DW_LANG_Go, cu);
8004
8005 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
8006
8007 /* Decode line number information if present. We do this before
8008 processing child DIEs, so that the line header table is available
8009 for DW_AT_decl_file. */
8010 handle_DW_AT_stmt_list (die, cu, comp_dir);
8011
8012 /* Process all dies in compilation unit. */
8013 if (die->child != NULL)
8014 {
8015 child_die = die->child;
8016 while (child_die && child_die->tag)
8017 {
8018 process_die (child_die, cu);
8019 child_die = sibling_die (child_die);
8020 }
8021 }
8022
8023 /* Decode macro information, if present. Dwarf 2 macro information
8024 refers to information in the line number info statement program
8025 header, so we can only read it if we've read the header
8026 successfully. */
8027 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
8028 if (attr && cu->line_header)
8029 {
8030 if (dwarf2_attr (die, DW_AT_macro_info, cu))
8031 complaint (&symfile_complaints,
8032 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
8033
8034 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
8035 }
8036 else
8037 {
8038 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
8039 if (attr && cu->line_header)
8040 {
8041 unsigned int macro_offset = DW_UNSND (attr);
8042
8043 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
8044 }
8045 }
8046
8047 do_cleanups (back_to);
8048 }
8049
8050 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
8051 Create the set of symtabs used by this TU, or if this TU is sharing
8052 symtabs with another TU and the symtabs have already been created
8053 then restore those symtabs in the line header.
8054 We don't need the pc/line-number mapping for type units. */
8055
8056 static void
8057 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
8058 {
8059 struct objfile *objfile = dwarf2_per_objfile->objfile;
8060 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8061 struct type_unit_group *tu_group;
8062 int first_time;
8063 struct line_header *lh;
8064 struct attribute *attr;
8065 unsigned int i, line_offset;
8066
8067 gdb_assert (per_cu->is_debug_types);
8068
8069 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
8070
8071 /* If we're using .gdb_index (includes -readnow) then
8072 per_cu->s.type_unit_group may not have been set up yet. */
8073 if (per_cu->type_unit_group == NULL)
8074 per_cu->type_unit_group = get_type_unit_group (cu, attr);
8075 tu_group = per_cu->type_unit_group;
8076
8077 /* If we've already processed this stmt_list there's no real need to
8078 do it again, we could fake it and just recreate the part we need
8079 (file name,index -> symtab mapping). If data shows this optimization
8080 is useful we can do it then. */
8081 first_time = tu_group->primary_symtab == NULL;
8082
8083 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
8084 debug info. */
8085 lh = NULL;
8086 if (attr != NULL)
8087 {
8088 line_offset = DW_UNSND (attr);
8089 lh = dwarf_decode_line_header (line_offset, cu);
8090 }
8091 if (lh == NULL)
8092 {
8093 if (first_time)
8094 dwarf2_start_symtab (cu, "", NULL, 0);
8095 else
8096 {
8097 gdb_assert (tu_group->symtabs == NULL);
8098 restart_symtab (0);
8099 }
8100 /* Note: The primary symtab will get allocated at the end. */
8101 return;
8102 }
8103
8104 cu->line_header = lh;
8105 make_cleanup (free_cu_line_header, cu);
8106
8107 if (first_time)
8108 {
8109 dwarf2_start_symtab (cu, "", NULL, 0);
8110
8111 tu_group->num_symtabs = lh->num_file_names;
8112 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
8113
8114 for (i = 0; i < lh->num_file_names; ++i)
8115 {
8116 char *dir = NULL;
8117 struct file_entry *fe = &lh->file_names[i];
8118
8119 if (fe->dir_index)
8120 dir = lh->include_dirs[fe->dir_index - 1];
8121 dwarf2_start_subfile (fe->name, dir, NULL);
8122
8123 /* Note: We don't have to watch for the main subfile here, type units
8124 don't have DW_AT_name. */
8125
8126 if (current_subfile->symtab == NULL)
8127 {
8128 /* NOTE: start_subfile will recognize when it's been passed
8129 a file it has already seen. So we can't assume there's a
8130 simple mapping from lh->file_names to subfiles,
8131 lh->file_names may contain dups. */
8132 current_subfile->symtab = allocate_symtab (current_subfile->name,
8133 objfile);
8134 }
8135
8136 fe->symtab = current_subfile->symtab;
8137 tu_group->symtabs[i] = fe->symtab;
8138 }
8139 }
8140 else
8141 {
8142 restart_symtab (0);
8143
8144 for (i = 0; i < lh->num_file_names; ++i)
8145 {
8146 struct file_entry *fe = &lh->file_names[i];
8147
8148 fe->symtab = tu_group->symtabs[i];
8149 }
8150 }
8151
8152 /* The main symtab is allocated last. Type units don't have DW_AT_name
8153 so they don't have a "real" (so to speak) symtab anyway.
8154 There is later code that will assign the main symtab to all symbols
8155 that don't have one. We need to handle the case of a symbol with a
8156 missing symtab (DW_AT_decl_file) anyway. */
8157 }
8158
8159 /* Process DW_TAG_type_unit.
8160 For TUs we want to skip the first top level sibling if it's not the
8161 actual type being defined by this TU. In this case the first top
8162 level sibling is there to provide context only. */
8163
8164 static void
8165 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8166 {
8167 struct die_info *child_die;
8168
8169 prepare_one_comp_unit (cu, die, language_minimal);
8170
8171 /* Initialize (or reinitialize) the machinery for building symtabs.
8172 We do this before processing child DIEs, so that the line header table
8173 is available for DW_AT_decl_file. */
8174 setup_type_unit_groups (die, cu);
8175
8176 if (die->child != NULL)
8177 {
8178 child_die = die->child;
8179 while (child_die && child_die->tag)
8180 {
8181 process_die (child_die, cu);
8182 child_die = sibling_die (child_die);
8183 }
8184 }
8185 }
8186 \f
8187 /* DWO/DWP files.
8188
8189 http://gcc.gnu.org/wiki/DebugFission
8190 http://gcc.gnu.org/wiki/DebugFissionDWP
8191
8192 To simplify handling of both DWO files ("object" files with the DWARF info)
8193 and DWP files (a file with the DWOs packaged up into one file), we treat
8194 DWP files as having a collection of virtual DWO files. */
8195
8196 static hashval_t
8197 hash_dwo_file (const void *item)
8198 {
8199 const struct dwo_file *dwo_file = item;
8200
8201 return htab_hash_string (dwo_file->name);
8202 }
8203
8204 static int
8205 eq_dwo_file (const void *item_lhs, const void *item_rhs)
8206 {
8207 const struct dwo_file *lhs = item_lhs;
8208 const struct dwo_file *rhs = item_rhs;
8209
8210 return strcmp (lhs->name, rhs->name) == 0;
8211 }
8212
8213 /* Allocate a hash table for DWO files. */
8214
8215 static htab_t
8216 allocate_dwo_file_hash_table (void)
8217 {
8218 struct objfile *objfile = dwarf2_per_objfile->objfile;
8219
8220 return htab_create_alloc_ex (41,
8221 hash_dwo_file,
8222 eq_dwo_file,
8223 NULL,
8224 &objfile->objfile_obstack,
8225 hashtab_obstack_allocate,
8226 dummy_obstack_deallocate);
8227 }
8228
8229 /* Lookup DWO file DWO_NAME. */
8230
8231 static void **
8232 lookup_dwo_file_slot (const char *dwo_name)
8233 {
8234 struct dwo_file find_entry;
8235 void **slot;
8236
8237 if (dwarf2_per_objfile->dwo_files == NULL)
8238 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8239
8240 memset (&find_entry, 0, sizeof (find_entry));
8241 find_entry.name = dwo_name;
8242 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8243
8244 return slot;
8245 }
8246
8247 static hashval_t
8248 hash_dwo_unit (const void *item)
8249 {
8250 const struct dwo_unit *dwo_unit = item;
8251
8252 /* This drops the top 32 bits of the id, but is ok for a hash. */
8253 return dwo_unit->signature;
8254 }
8255
8256 static int
8257 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8258 {
8259 const struct dwo_unit *lhs = item_lhs;
8260 const struct dwo_unit *rhs = item_rhs;
8261
8262 /* The signature is assumed to be unique within the DWO file.
8263 So while object file CU dwo_id's always have the value zero,
8264 that's OK, assuming each object file DWO file has only one CU,
8265 and that's the rule for now. */
8266 return lhs->signature == rhs->signature;
8267 }
8268
8269 /* Allocate a hash table for DWO CUs,TUs.
8270 There is one of these tables for each of CUs,TUs for each DWO file. */
8271
8272 static htab_t
8273 allocate_dwo_unit_table (struct objfile *objfile)
8274 {
8275 /* Start out with a pretty small number.
8276 Generally DWO files contain only one CU and maybe some TUs. */
8277 return htab_create_alloc_ex (3,
8278 hash_dwo_unit,
8279 eq_dwo_unit,
8280 NULL,
8281 &objfile->objfile_obstack,
8282 hashtab_obstack_allocate,
8283 dummy_obstack_deallocate);
8284 }
8285
8286 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
8287
8288 struct create_dwo_info_table_data
8289 {
8290 struct dwo_file *dwo_file;
8291 htab_t cu_htab;
8292 };
8293
8294 /* die_reader_func for create_dwo_debug_info_hash_table. */
8295
8296 static void
8297 create_dwo_debug_info_hash_table_reader (const struct die_reader_specs *reader,
8298 gdb_byte *info_ptr,
8299 struct die_info *comp_unit_die,
8300 int has_children,
8301 void *datap)
8302 {
8303 struct dwarf2_cu *cu = reader->cu;
8304 struct objfile *objfile = dwarf2_per_objfile->objfile;
8305 sect_offset offset = cu->per_cu->offset;
8306 struct dwarf2_section_info *section = cu->per_cu->info_or_types_section;
8307 struct create_dwo_info_table_data *data = datap;
8308 struct dwo_file *dwo_file = data->dwo_file;
8309 htab_t cu_htab = data->cu_htab;
8310 void **slot;
8311 struct attribute *attr;
8312 struct dwo_unit *dwo_unit;
8313
8314 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8315 if (attr == NULL)
8316 {
8317 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
8318 " its dwo_id [in module %s]"),
8319 offset.sect_off, dwo_file->name);
8320 return;
8321 }
8322
8323 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8324 dwo_unit->dwo_file = dwo_file;
8325 dwo_unit->signature = DW_UNSND (attr);
8326 dwo_unit->info_or_types_section = section;
8327 dwo_unit->offset = offset;
8328 dwo_unit->length = cu->per_cu->length;
8329
8330 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
8331 gdb_assert (slot != NULL);
8332 if (*slot != NULL)
8333 {
8334 const struct dwo_unit *dup_dwo_unit = *slot;
8335
8336 complaint (&symfile_complaints,
8337 _("debug entry at offset 0x%x is duplicate to the entry at"
8338 " offset 0x%x, dwo_id 0x%s [in module %s]"),
8339 offset.sect_off, dup_dwo_unit->offset.sect_off,
8340 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
8341 dwo_file->name);
8342 }
8343 else
8344 *slot = dwo_unit;
8345
8346 if (dwarf2_read_debug)
8347 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
8348 offset.sect_off,
8349 phex (dwo_unit->signature,
8350 sizeof (dwo_unit->signature)));
8351 }
8352
8353 /* Create a hash table to map DWO IDs to their CU entry in
8354 .debug_info.dwo in DWO_FILE.
8355 Note: This function processes DWO files only, not DWP files. */
8356
8357 static htab_t
8358 create_dwo_debug_info_hash_table (struct dwo_file *dwo_file)
8359 {
8360 struct objfile *objfile = dwarf2_per_objfile->objfile;
8361 struct dwarf2_section_info *section = &dwo_file->sections.info;
8362 bfd *abfd;
8363 htab_t cu_htab;
8364 gdb_byte *info_ptr, *end_ptr;
8365 struct create_dwo_info_table_data create_dwo_info_table_data;
8366
8367 dwarf2_read_section (objfile, section);
8368 info_ptr = section->buffer;
8369
8370 if (info_ptr == NULL)
8371 return NULL;
8372
8373 /* We can't set abfd until now because the section may be empty or
8374 not present, in which case section->asection will be NULL. */
8375 abfd = section->asection->owner;
8376
8377 if (dwarf2_read_debug)
8378 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
8379 bfd_get_filename (abfd));
8380
8381 cu_htab = allocate_dwo_unit_table (objfile);
8382
8383 create_dwo_info_table_data.dwo_file = dwo_file;
8384 create_dwo_info_table_data.cu_htab = cu_htab;
8385
8386 end_ptr = info_ptr + section->size;
8387 while (info_ptr < end_ptr)
8388 {
8389 struct dwarf2_per_cu_data per_cu;
8390
8391 memset (&per_cu, 0, sizeof (per_cu));
8392 per_cu.objfile = objfile;
8393 per_cu.is_debug_types = 0;
8394 per_cu.offset.sect_off = info_ptr - section->buffer;
8395 per_cu.info_or_types_section = section;
8396
8397 init_cutu_and_read_dies_no_follow (&per_cu,
8398 &dwo_file->sections.abbrev,
8399 dwo_file,
8400 create_dwo_debug_info_hash_table_reader,
8401 &create_dwo_info_table_data);
8402
8403 info_ptr += per_cu.length;
8404 }
8405
8406 return cu_htab;
8407 }
8408
8409 /* DWP file .debug_{cu,tu}_index section format:
8410 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8411
8412 Both index sections have the same format, and serve to map a 64-bit
8413 signature to a set of section numbers. Each section begins with a header,
8414 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8415 indexes, and a pool of 32-bit section numbers. The index sections will be
8416 aligned at 8-byte boundaries in the file.
8417
8418 The index section header contains two unsigned 32-bit values (using the
8419 byte order of the application binary):
8420
8421 N, the number of compilation units or type units in the index
8422 M, the number of slots in the hash table
8423
8424 (We assume that N and M will not exceed 2^32 - 1.)
8425
8426 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
8427
8428 The hash table begins at offset 8 in the section, and consists of an array
8429 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
8430 order of the application binary). Unused slots in the hash table are 0.
8431 (We rely on the extreme unlikeliness of a signature being exactly 0.)
8432
8433 The parallel table begins immediately after the hash table
8434 (at offset 8 + 8 * M from the beginning of the section), and consists of an
8435 array of 32-bit indexes (using the byte order of the application binary),
8436 corresponding 1-1 with slots in the hash table. Each entry in the parallel
8437 table contains a 32-bit index into the pool of section numbers. For unused
8438 hash table slots, the corresponding entry in the parallel table will be 0.
8439
8440 Given a 64-bit compilation unit signature or a type signature S, an entry
8441 in the hash table is located as follows:
8442
8443 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8444 the low-order k bits all set to 1.
8445
8446 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
8447
8448 3) If the hash table entry at index H matches the signature, use that
8449 entry. If the hash table entry at index H is unused (all zeroes),
8450 terminate the search: the signature is not present in the table.
8451
8452 4) Let H = (H + H') modulo M. Repeat at Step 3.
8453
8454 Because M > N and H' and M are relatively prime, the search is guaranteed
8455 to stop at an unused slot or find the match.
8456
8457 The pool of section numbers begins immediately following the hash table
8458 (at offset 8 + 12 * M from the beginning of the section). The pool of
8459 section numbers consists of an array of 32-bit words (using the byte order
8460 of the application binary). Each item in the array is indexed starting
8461 from 0. The hash table entry provides the index of the first section
8462 number in the set. Additional section numbers in the set follow, and the
8463 set is terminated by a 0 entry (section number 0 is not used in ELF).
8464
8465 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8466 section must be the first entry in the set, and the .debug_abbrev.dwo must
8467 be the second entry. Other members of the set may follow in any order. */
8468
8469 /* Create a hash table to map DWO IDs to their CU/TU entry in
8470 .debug_{info,types}.dwo in DWP_FILE.
8471 Returns NULL if there isn't one.
8472 Note: This function processes DWP files only, not DWO files. */
8473
8474 static struct dwp_hash_table *
8475 create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
8476 {
8477 struct objfile *objfile = dwarf2_per_objfile->objfile;
8478 bfd *dbfd = dwp_file->dbfd;
8479 char *index_ptr, *index_end;
8480 struct dwarf2_section_info *index;
8481 uint32_t version, nr_units, nr_slots;
8482 struct dwp_hash_table *htab;
8483
8484 if (is_debug_types)
8485 index = &dwp_file->sections.tu_index;
8486 else
8487 index = &dwp_file->sections.cu_index;
8488
8489 if (dwarf2_section_empty_p (index))
8490 return NULL;
8491 dwarf2_read_section (objfile, index);
8492
8493 index_ptr = index->buffer;
8494 index_end = index_ptr + index->size;
8495
8496 version = read_4_bytes (dbfd, index_ptr);
8497 index_ptr += 8; /* Skip the unused word. */
8498 nr_units = read_4_bytes (dbfd, index_ptr);
8499 index_ptr += 4;
8500 nr_slots = read_4_bytes (dbfd, index_ptr);
8501 index_ptr += 4;
8502
8503 if (version != 1)
8504 {
8505 error (_("Dwarf Error: unsupported DWP file version (%u)"
8506 " [in module %s]"),
8507 version, dwp_file->name);
8508 }
8509 if (nr_slots != (nr_slots & -nr_slots))
8510 {
8511 error (_("Dwarf Error: number of slots in DWP hash table (%u)"
8512 " is not power of 2 [in module %s]"),
8513 nr_slots, dwp_file->name);
8514 }
8515
8516 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
8517 htab->nr_units = nr_units;
8518 htab->nr_slots = nr_slots;
8519 htab->hash_table = index_ptr;
8520 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
8521 htab->section_pool = htab->unit_table + sizeof (uint32_t) * nr_slots;
8522
8523 return htab;
8524 }
8525
8526 /* Update SECTIONS with the data from SECTP.
8527
8528 This function is like the other "locate" section routines that are
8529 passed to bfd_map_over_sections, but in this context the sections to
8530 read comes from the DWP hash table, not the full ELF section table.
8531
8532 The result is non-zero for success, or zero if an error was found. */
8533
8534 static int
8535 locate_virtual_dwo_sections (asection *sectp,
8536 struct virtual_dwo_sections *sections)
8537 {
8538 const struct dwop_section_names *names = &dwop_section_names;
8539
8540 if (section_is_p (sectp->name, &names->abbrev_dwo))
8541 {
8542 /* There can be only one. */
8543 if (sections->abbrev.asection != NULL)
8544 return 0;
8545 sections->abbrev.asection = sectp;
8546 sections->abbrev.size = bfd_get_section_size (sectp);
8547 }
8548 else if (section_is_p (sectp->name, &names->info_dwo)
8549 || section_is_p (sectp->name, &names->types_dwo))
8550 {
8551 /* There can be only one. */
8552 if (sections->info_or_types.asection != NULL)
8553 return 0;
8554 sections->info_or_types.asection = sectp;
8555 sections->info_or_types.size = bfd_get_section_size (sectp);
8556 }
8557 else if (section_is_p (sectp->name, &names->line_dwo))
8558 {
8559 /* There can be only one. */
8560 if (sections->line.asection != NULL)
8561 return 0;
8562 sections->line.asection = sectp;
8563 sections->line.size = bfd_get_section_size (sectp);
8564 }
8565 else if (section_is_p (sectp->name, &names->loc_dwo))
8566 {
8567 /* There can be only one. */
8568 if (sections->loc.asection != NULL)
8569 return 0;
8570 sections->loc.asection = sectp;
8571 sections->loc.size = bfd_get_section_size (sectp);
8572 }
8573 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8574 {
8575 /* There can be only one. */
8576 if (sections->macinfo.asection != NULL)
8577 return 0;
8578 sections->macinfo.asection = sectp;
8579 sections->macinfo.size = bfd_get_section_size (sectp);
8580 }
8581 else if (section_is_p (sectp->name, &names->macro_dwo))
8582 {
8583 /* There can be only one. */
8584 if (sections->macro.asection != NULL)
8585 return 0;
8586 sections->macro.asection = sectp;
8587 sections->macro.size = bfd_get_section_size (sectp);
8588 }
8589 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8590 {
8591 /* There can be only one. */
8592 if (sections->str_offsets.asection != NULL)
8593 return 0;
8594 sections->str_offsets.asection = sectp;
8595 sections->str_offsets.size = bfd_get_section_size (sectp);
8596 }
8597 else
8598 {
8599 /* No other kind of section is valid. */
8600 return 0;
8601 }
8602
8603 return 1;
8604 }
8605
8606 /* Create a dwo_unit object for the DWO with signature SIGNATURE.
8607 HTAB is the hash table from the DWP file.
8608 SECTION_INDEX is the index of the DWO in HTAB. */
8609
8610 static struct dwo_unit *
8611 create_dwo_in_dwp (struct dwp_file *dwp_file,
8612 const struct dwp_hash_table *htab,
8613 uint32_t section_index,
8614 ULONGEST signature, int is_debug_types)
8615 {
8616 struct objfile *objfile = dwarf2_per_objfile->objfile;
8617 bfd *dbfd = dwp_file->dbfd;
8618 const char *kind = is_debug_types ? "TU" : "CU";
8619 struct dwo_file *dwo_file;
8620 struct dwo_unit *dwo_unit;
8621 struct virtual_dwo_sections sections;
8622 void **dwo_file_slot;
8623 char *virtual_dwo_name;
8624 struct dwarf2_section_info *cutu;
8625 struct cleanup *cleanups;
8626 int i;
8627
8628 if (dwarf2_read_debug)
8629 {
8630 fprintf_unfiltered (gdb_stdlog, "Reading %s %u/0x%s in DWP file: %s\n",
8631 kind,
8632 section_index, phex (signature, sizeof (signature)),
8633 dwp_file->name);
8634 }
8635
8636 /* Fetch the sections of this DWO.
8637 Put a limit on the number of sections we look for so that bad data
8638 doesn't cause us to loop forever. */
8639
8640 #define MAX_NR_DWO_SECTIONS \
8641 (1 /* .debug_info or .debug_types */ \
8642 + 1 /* .debug_abbrev */ \
8643 + 1 /* .debug_line */ \
8644 + 1 /* .debug_loc */ \
8645 + 1 /* .debug_str_offsets */ \
8646 + 1 /* .debug_macro */ \
8647 + 1 /* .debug_macinfo */ \
8648 + 1 /* trailing zero */)
8649
8650 memset (&sections, 0, sizeof (sections));
8651 cleanups = make_cleanup (null_cleanup, 0);
8652
8653 for (i = 0; i < MAX_NR_DWO_SECTIONS; ++i)
8654 {
8655 asection *sectp;
8656 uint32_t section_nr =
8657 read_4_bytes (dbfd,
8658 htab->section_pool
8659 + (section_index + i) * sizeof (uint32_t));
8660
8661 if (section_nr == 0)
8662 break;
8663 if (section_nr >= dwp_file->num_sections)
8664 {
8665 error (_("Dwarf Error: bad DWP hash table, section number too large"
8666 " [in module %s]"),
8667 dwp_file->name);
8668 }
8669
8670 sectp = dwp_file->elf_sections[section_nr];
8671 if (! locate_virtual_dwo_sections (sectp, &sections))
8672 {
8673 error (_("Dwarf Error: bad DWP hash table, invalid section found"
8674 " [in module %s]"),
8675 dwp_file->name);
8676 }
8677 }
8678
8679 if (i < 2
8680 || sections.info_or_types.asection == NULL
8681 || sections.abbrev.asection == NULL)
8682 {
8683 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
8684 " [in module %s]"),
8685 dwp_file->name);
8686 }
8687 if (i == MAX_NR_DWO_SECTIONS)
8688 {
8689 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
8690 " [in module %s]"),
8691 dwp_file->name);
8692 }
8693
8694 /* It's easier for the rest of the code if we fake a struct dwo_file and
8695 have dwo_unit "live" in that. At least for now.
8696
8697 The DWP file can be made up of a random collection of CUs and TUs.
8698 However, for each CU + set of TUs that came from the same original DWO
8699 file, we want to combine them back into a virtual DWO file to save space
8700 (fewer struct dwo_file objects to allocated). Remember that for really
8701 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8702
8703 virtual_dwo_name =
8704 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
8705 sections.abbrev.asection ? sections.abbrev.asection->id : 0,
8706 sections.line.asection ? sections.line.asection->id : 0,
8707 sections.loc.asection ? sections.loc.asection->id : 0,
8708 (sections.str_offsets.asection
8709 ? sections.str_offsets.asection->id
8710 : 0));
8711 make_cleanup (xfree, virtual_dwo_name);
8712 /* Can we use an existing virtual DWO file? */
8713 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name);
8714 /* Create one if necessary. */
8715 if (*dwo_file_slot == NULL)
8716 {
8717 if (dwarf2_read_debug)
8718 {
8719 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
8720 virtual_dwo_name);
8721 }
8722 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8723 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8724 virtual_dwo_name,
8725 strlen (virtual_dwo_name));
8726 dwo_file->sections.abbrev = sections.abbrev;
8727 dwo_file->sections.line = sections.line;
8728 dwo_file->sections.loc = sections.loc;
8729 dwo_file->sections.macinfo = sections.macinfo;
8730 dwo_file->sections.macro = sections.macro;
8731 dwo_file->sections.str_offsets = sections.str_offsets;
8732 /* The "str" section is global to the entire DWP file. */
8733 dwo_file->sections.str = dwp_file->sections.str;
8734 /* The info or types section is assigned later to dwo_unit,
8735 there's no need to record it in dwo_file.
8736 Also, we can't simply record type sections in dwo_file because
8737 we record a pointer into the vector in dwo_unit. As we collect more
8738 types we'll grow the vector and eventually have to reallocate space
8739 for it, invalidating all the pointers into the current copy. */
8740 *dwo_file_slot = dwo_file;
8741 }
8742 else
8743 {
8744 if (dwarf2_read_debug)
8745 {
8746 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
8747 virtual_dwo_name);
8748 }
8749 dwo_file = *dwo_file_slot;
8750 }
8751 do_cleanups (cleanups);
8752
8753 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8754 dwo_unit->dwo_file = dwo_file;
8755 dwo_unit->signature = signature;
8756 dwo_unit->info_or_types_section =
8757 obstack_alloc (&objfile->objfile_obstack,
8758 sizeof (struct dwarf2_section_info));
8759 *dwo_unit->info_or_types_section = sections.info_or_types;
8760 /* offset, length, type_offset_in_tu are set later. */
8761
8762 return dwo_unit;
8763 }
8764
8765 /* Lookup the DWO with SIGNATURE in DWP_FILE. */
8766
8767 static struct dwo_unit *
8768 lookup_dwo_in_dwp (struct dwp_file *dwp_file,
8769 const struct dwp_hash_table *htab,
8770 ULONGEST signature, int is_debug_types)
8771 {
8772 bfd *dbfd = dwp_file->dbfd;
8773 uint32_t mask = htab->nr_slots - 1;
8774 uint32_t hash = signature & mask;
8775 uint32_t hash2 = ((signature >> 32) & mask) | 1;
8776 unsigned int i;
8777 void **slot;
8778 struct dwo_unit find_dwo_cu, *dwo_cu;
8779
8780 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
8781 find_dwo_cu.signature = signature;
8782 slot = htab_find_slot (dwp_file->loaded_cutus, &find_dwo_cu, INSERT);
8783
8784 if (*slot != NULL)
8785 return *slot;
8786
8787 /* Use a for loop so that we don't loop forever on bad debug info. */
8788 for (i = 0; i < htab->nr_slots; ++i)
8789 {
8790 ULONGEST signature_in_table;
8791
8792 signature_in_table =
8793 read_8_bytes (dbfd, htab->hash_table + hash * sizeof (uint64_t));
8794 if (signature_in_table == signature)
8795 {
8796 uint32_t section_index =
8797 read_4_bytes (dbfd, htab->unit_table + hash * sizeof (uint32_t));
8798
8799 *slot = create_dwo_in_dwp (dwp_file, htab, section_index,
8800 signature, is_debug_types);
8801 return *slot;
8802 }
8803 if (signature_in_table == 0)
8804 return NULL;
8805 hash = (hash + hash2) & mask;
8806 }
8807
8808 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
8809 " [in module %s]"),
8810 dwp_file->name);
8811 }
8812
8813 /* Subroutine of open_dwop_file to simplify it.
8814 Open the file specified by FILE_NAME and hand it off to BFD for
8815 preliminary analysis. Return a newly initialized bfd *, which
8816 includes a canonicalized copy of FILE_NAME.
8817 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
8818 In case of trouble, return NULL.
8819 NOTE: This function is derived from symfile_bfd_open. */
8820
8821 static bfd *
8822 try_open_dwop_file (const char *file_name, int is_dwp)
8823 {
8824 bfd *sym_bfd;
8825 int desc, flags;
8826 char *absolute_name;
8827
8828 flags = OPF_TRY_CWD_FIRST;
8829 if (is_dwp)
8830 flags |= OPF_SEARCH_IN_PATH;
8831 desc = openp (debug_file_directory, flags, file_name,
8832 O_RDONLY | O_BINARY, &absolute_name);
8833 if (desc < 0)
8834 return NULL;
8835
8836 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
8837 if (!sym_bfd)
8838 {
8839 xfree (absolute_name);
8840 return NULL;
8841 }
8842 xfree (absolute_name);
8843 bfd_set_cacheable (sym_bfd, 1);
8844
8845 if (!bfd_check_format (sym_bfd, bfd_object))
8846 {
8847 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
8848 return NULL;
8849 }
8850
8851 return sym_bfd;
8852 }
8853
8854 /* Try to open DWO/DWP file FILE_NAME.
8855 COMP_DIR is the DW_AT_comp_dir attribute.
8856 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
8857 The result is the bfd handle of the file.
8858 If there is a problem finding or opening the file, return NULL.
8859 Upon success, the canonicalized path of the file is stored in the bfd,
8860 same as symfile_bfd_open. */
8861
8862 static bfd *
8863 open_dwop_file (const char *file_name, const char *comp_dir, int is_dwp)
8864 {
8865 bfd *abfd;
8866
8867 if (IS_ABSOLUTE_PATH (file_name))
8868 return try_open_dwop_file (file_name, is_dwp);
8869
8870 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
8871
8872 if (comp_dir != NULL)
8873 {
8874 char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
8875
8876 /* NOTE: If comp_dir is a relative path, this will also try the
8877 search path, which seems useful. */
8878 abfd = try_open_dwop_file (path_to_try, is_dwp);
8879 xfree (path_to_try);
8880 if (abfd != NULL)
8881 return abfd;
8882 }
8883
8884 /* That didn't work, try debug-file-directory, which, despite its name,
8885 is a list of paths. */
8886
8887 if (*debug_file_directory == '\0')
8888 return NULL;
8889
8890 return try_open_dwop_file (file_name, is_dwp);
8891 }
8892
8893 /* This function is mapped across the sections and remembers the offset and
8894 size of each of the DWO debugging sections we are interested in. */
8895
8896 static void
8897 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
8898 {
8899 struct dwo_sections *dwo_sections = dwo_sections_ptr;
8900 const struct dwop_section_names *names = &dwop_section_names;
8901
8902 if (section_is_p (sectp->name, &names->abbrev_dwo))
8903 {
8904 dwo_sections->abbrev.asection = sectp;
8905 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
8906 }
8907 else if (section_is_p (sectp->name, &names->info_dwo))
8908 {
8909 dwo_sections->info.asection = sectp;
8910 dwo_sections->info.size = bfd_get_section_size (sectp);
8911 }
8912 else if (section_is_p (sectp->name, &names->line_dwo))
8913 {
8914 dwo_sections->line.asection = sectp;
8915 dwo_sections->line.size = bfd_get_section_size (sectp);
8916 }
8917 else if (section_is_p (sectp->name, &names->loc_dwo))
8918 {
8919 dwo_sections->loc.asection = sectp;
8920 dwo_sections->loc.size = bfd_get_section_size (sectp);
8921 }
8922 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8923 {
8924 dwo_sections->macinfo.asection = sectp;
8925 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
8926 }
8927 else if (section_is_p (sectp->name, &names->macro_dwo))
8928 {
8929 dwo_sections->macro.asection = sectp;
8930 dwo_sections->macro.size = bfd_get_section_size (sectp);
8931 }
8932 else if (section_is_p (sectp->name, &names->str_dwo))
8933 {
8934 dwo_sections->str.asection = sectp;
8935 dwo_sections->str.size = bfd_get_section_size (sectp);
8936 }
8937 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8938 {
8939 dwo_sections->str_offsets.asection = sectp;
8940 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
8941 }
8942 else if (section_is_p (sectp->name, &names->types_dwo))
8943 {
8944 struct dwarf2_section_info type_section;
8945
8946 memset (&type_section, 0, sizeof (type_section));
8947 type_section.asection = sectp;
8948 type_section.size = bfd_get_section_size (sectp);
8949 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
8950 &type_section);
8951 }
8952 }
8953
8954 /* Initialize the use of the DWO file specified by DWO_NAME.
8955 The result is NULL if DWO_NAME can't be found. */
8956
8957 static struct dwo_file *
8958 open_and_init_dwo_file (const char *dwo_name, const char *comp_dir)
8959 {
8960 struct objfile *objfile = dwarf2_per_objfile->objfile;
8961 struct dwo_file *dwo_file;
8962 bfd *dbfd;
8963 struct cleanup *cleanups;
8964
8965 dbfd = open_dwop_file (dwo_name, comp_dir, 0);
8966 if (dbfd == NULL)
8967 {
8968 if (dwarf2_read_debug)
8969 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
8970 return NULL;
8971 }
8972 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8973 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8974 dwo_name, strlen (dwo_name));
8975 dwo_file->dbfd = dbfd;
8976
8977 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
8978
8979 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
8980
8981 dwo_file->cus = create_dwo_debug_info_hash_table (dwo_file);
8982
8983 dwo_file->tus = create_debug_types_hash_table (dwo_file,
8984 dwo_file->sections.types);
8985
8986 discard_cleanups (cleanups);
8987
8988 if (dwarf2_read_debug)
8989 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
8990
8991 return dwo_file;
8992 }
8993
8994 /* This function is mapped across the sections and remembers the offset and
8995 size of each of the DWP debugging sections we are interested in. */
8996
8997 static void
8998 dwarf2_locate_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
8999 {
9000 struct dwp_file *dwp_file = dwp_file_ptr;
9001 const struct dwop_section_names *names = &dwop_section_names;
9002 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
9003
9004 /* Record the ELF section number for later lookup: this is what the
9005 .debug_cu_index,.debug_tu_index tables use. */
9006 gdb_assert (elf_section_nr < dwp_file->num_sections);
9007 dwp_file->elf_sections[elf_section_nr] = sectp;
9008
9009 /* Look for specific sections that we need. */
9010 if (section_is_p (sectp->name, &names->str_dwo))
9011 {
9012 dwp_file->sections.str.asection = sectp;
9013 dwp_file->sections.str.size = bfd_get_section_size (sectp);
9014 }
9015 else if (section_is_p (sectp->name, &names->cu_index))
9016 {
9017 dwp_file->sections.cu_index.asection = sectp;
9018 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
9019 }
9020 else if (section_is_p (sectp->name, &names->tu_index))
9021 {
9022 dwp_file->sections.tu_index.asection = sectp;
9023 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
9024 }
9025 }
9026
9027 /* Hash function for dwp_file loaded CUs/TUs. */
9028
9029 static hashval_t
9030 hash_dwp_loaded_cutus (const void *item)
9031 {
9032 const struct dwo_unit *dwo_unit = item;
9033
9034 /* This drops the top 32 bits of the signature, but is ok for a hash. */
9035 return dwo_unit->signature;
9036 }
9037
9038 /* Equality function for dwp_file loaded CUs/TUs. */
9039
9040 static int
9041 eq_dwp_loaded_cutus (const void *a, const void *b)
9042 {
9043 const struct dwo_unit *dua = a;
9044 const struct dwo_unit *dub = b;
9045
9046 return dua->signature == dub->signature;
9047 }
9048
9049 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
9050
9051 static htab_t
9052 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
9053 {
9054 return htab_create_alloc_ex (3,
9055 hash_dwp_loaded_cutus,
9056 eq_dwp_loaded_cutus,
9057 NULL,
9058 &objfile->objfile_obstack,
9059 hashtab_obstack_allocate,
9060 dummy_obstack_deallocate);
9061 }
9062
9063 /* Initialize the use of the DWP file for the current objfile.
9064 By convention the name of the DWP file is ${objfile}.dwp.
9065 The result is NULL if it can't be found. */
9066
9067 static struct dwp_file *
9068 open_and_init_dwp_file (const char *comp_dir)
9069 {
9070 struct objfile *objfile = dwarf2_per_objfile->objfile;
9071 struct dwp_file *dwp_file;
9072 char *dwp_name;
9073 bfd *dbfd;
9074 struct cleanup *cleanups;
9075
9076 dwp_name = xstrprintf ("%s.dwp", dwarf2_per_objfile->objfile->name);
9077 cleanups = make_cleanup (xfree, dwp_name);
9078
9079 dbfd = open_dwop_file (dwp_name, comp_dir, 1);
9080 if (dbfd == NULL)
9081 {
9082 if (dwarf2_read_debug)
9083 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
9084 do_cleanups (cleanups);
9085 return NULL;
9086 }
9087 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
9088 dwp_file->name = obstack_copy0 (&objfile->objfile_obstack,
9089 dwp_name, strlen (dwp_name));
9090 dwp_file->dbfd = dbfd;
9091 do_cleanups (cleanups);
9092
9093 cleanups = make_cleanup (free_dwo_file_cleanup, dwp_file);
9094
9095 /* +1: section 0 is unused */
9096 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
9097 dwp_file->elf_sections =
9098 OBSTACK_CALLOC (&objfile->objfile_obstack,
9099 dwp_file->num_sections, asection *);
9100
9101 bfd_map_over_sections (dbfd, dwarf2_locate_dwp_sections, dwp_file);
9102
9103 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
9104
9105 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
9106
9107 dwp_file->loaded_cutus = allocate_dwp_loaded_cutus_table (objfile);
9108
9109 discard_cleanups (cleanups);
9110
9111 if (dwarf2_read_debug)
9112 {
9113 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
9114 fprintf_unfiltered (gdb_stdlog,
9115 " %u CUs, %u TUs\n",
9116 dwp_file->cus ? dwp_file->cus->nr_units : 0,
9117 dwp_file->tus ? dwp_file->tus->nr_units : 0);
9118 }
9119
9120 return dwp_file;
9121 }
9122
9123 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9124 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9125 or in the DWP file for the objfile, referenced by THIS_UNIT.
9126 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
9127 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9128
9129 This is called, for example, when wanting to read a variable with a
9130 complex location. Therefore we don't want to do file i/o for every call.
9131 Therefore we don't want to look for a DWO file on every call.
9132 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9133 then we check if we've already seen DWO_NAME, and only THEN do we check
9134 for a DWO file.
9135
9136 The result is a pointer to the dwo_unit object or NULL if we didn't find it
9137 (dwo_id mismatch or couldn't find the DWO/DWP file). */
9138
9139 static struct dwo_unit *
9140 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
9141 const char *dwo_name, const char *comp_dir,
9142 ULONGEST signature, int is_debug_types)
9143 {
9144 struct objfile *objfile = dwarf2_per_objfile->objfile;
9145 const char *kind = is_debug_types ? "TU" : "CU";
9146 void **dwo_file_slot;
9147 struct dwo_file *dwo_file;
9148 struct dwp_file *dwp_file;
9149
9150 /* Have we already read SIGNATURE from a DWP file? */
9151
9152 if (! dwarf2_per_objfile->dwp_checked)
9153 {
9154 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file (comp_dir);
9155 dwarf2_per_objfile->dwp_checked = 1;
9156 }
9157 dwp_file = dwarf2_per_objfile->dwp_file;
9158
9159 if (dwp_file != NULL)
9160 {
9161 const struct dwp_hash_table *dwp_htab =
9162 is_debug_types ? dwp_file->tus : dwp_file->cus;
9163
9164 if (dwp_htab != NULL)
9165 {
9166 struct dwo_unit *dwo_cutu =
9167 lookup_dwo_in_dwp (dwp_file, dwp_htab, signature, is_debug_types);
9168
9169 if (dwo_cutu != NULL)
9170 {
9171 if (dwarf2_read_debug)
9172 {
9173 fprintf_unfiltered (gdb_stdlog,
9174 "Virtual DWO %s %s found: @%s\n",
9175 kind, hex_string (signature),
9176 host_address_to_string (dwo_cutu));
9177 }
9178 return dwo_cutu;
9179 }
9180 }
9181 }
9182
9183 /* Have we already seen DWO_NAME? */
9184
9185 dwo_file_slot = lookup_dwo_file_slot (dwo_name);
9186 if (*dwo_file_slot == NULL)
9187 {
9188 /* Read in the file and build a table of the DWOs it contains. */
9189 *dwo_file_slot = open_and_init_dwo_file (dwo_name, comp_dir);
9190 }
9191 /* NOTE: This will be NULL if unable to open the file. */
9192 dwo_file = *dwo_file_slot;
9193
9194 if (dwo_file != NULL)
9195 {
9196 htab_t htab = is_debug_types ? dwo_file->tus : dwo_file->cus;
9197
9198 if (htab != NULL)
9199 {
9200 struct dwo_unit find_dwo_cutu, *dwo_cutu;
9201
9202 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9203 find_dwo_cutu.signature = signature;
9204 dwo_cutu = htab_find (htab, &find_dwo_cutu);
9205
9206 if (dwo_cutu != NULL)
9207 {
9208 if (dwarf2_read_debug)
9209 {
9210 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
9211 kind, dwo_name, hex_string (signature),
9212 host_address_to_string (dwo_cutu));
9213 }
9214 return dwo_cutu;
9215 }
9216 }
9217 }
9218
9219 /* We didn't find it. This could mean a dwo_id mismatch, or
9220 someone deleted the DWO/DWP file, or the search path isn't set up
9221 correctly to find the file. */
9222
9223 if (dwarf2_read_debug)
9224 {
9225 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
9226 kind, dwo_name, hex_string (signature));
9227 }
9228
9229 complaint (&symfile_complaints,
9230 _("Could not find DWO CU referenced by CU at offset 0x%x"
9231 " [in module %s]"),
9232 this_unit->offset.sect_off, objfile->name);
9233 return NULL;
9234 }
9235
9236 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9237 See lookup_dwo_cutu_unit for details. */
9238
9239 static struct dwo_unit *
9240 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
9241 const char *dwo_name, const char *comp_dir,
9242 ULONGEST signature)
9243 {
9244 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
9245 }
9246
9247 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9248 See lookup_dwo_cutu_unit for details. */
9249
9250 static struct dwo_unit *
9251 lookup_dwo_type_unit (struct signatured_type *this_tu,
9252 const char *dwo_name, const char *comp_dir)
9253 {
9254 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
9255 }
9256
9257 /* Free all resources associated with DWO_FILE.
9258 Close the DWO file and munmap the sections.
9259 All memory should be on the objfile obstack. */
9260
9261 static void
9262 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
9263 {
9264 int ix;
9265 struct dwarf2_section_info *section;
9266
9267 gdb_bfd_unref (dwo_file->dbfd);
9268
9269 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
9270 }
9271
9272 /* Wrapper for free_dwo_file for use in cleanups. */
9273
9274 static void
9275 free_dwo_file_cleanup (void *arg)
9276 {
9277 struct dwo_file *dwo_file = (struct dwo_file *) arg;
9278 struct objfile *objfile = dwarf2_per_objfile->objfile;
9279
9280 free_dwo_file (dwo_file, objfile);
9281 }
9282
9283 /* Traversal function for free_dwo_files. */
9284
9285 static int
9286 free_dwo_file_from_slot (void **slot, void *info)
9287 {
9288 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
9289 struct objfile *objfile = (struct objfile *) info;
9290
9291 free_dwo_file (dwo_file, objfile);
9292
9293 return 1;
9294 }
9295
9296 /* Free all resources associated with DWO_FILES. */
9297
9298 static void
9299 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
9300 {
9301 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
9302 }
9303 \f
9304 /* Read in various DIEs. */
9305
9306 /* qsort helper for inherit_abstract_dies. */
9307
9308 static int
9309 unsigned_int_compar (const void *ap, const void *bp)
9310 {
9311 unsigned int a = *(unsigned int *) ap;
9312 unsigned int b = *(unsigned int *) bp;
9313
9314 return (a > b) - (b > a);
9315 }
9316
9317 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
9318 Inherit only the children of the DW_AT_abstract_origin DIE not being
9319 already referenced by DW_AT_abstract_origin from the children of the
9320 current DIE. */
9321
9322 static void
9323 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9324 {
9325 struct die_info *child_die;
9326 unsigned die_children_count;
9327 /* CU offsets which were referenced by children of the current DIE. */
9328 sect_offset *offsets;
9329 sect_offset *offsets_end, *offsetp;
9330 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9331 struct die_info *origin_die;
9332 /* Iterator of the ORIGIN_DIE children. */
9333 struct die_info *origin_child_die;
9334 struct cleanup *cleanups;
9335 struct attribute *attr;
9336 struct dwarf2_cu *origin_cu;
9337 struct pending **origin_previous_list_in_scope;
9338
9339 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9340 if (!attr)
9341 return;
9342
9343 /* Note that following die references may follow to a die in a
9344 different cu. */
9345
9346 origin_cu = cu;
9347 origin_die = follow_die_ref (die, attr, &origin_cu);
9348
9349 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9350 symbols in. */
9351 origin_previous_list_in_scope = origin_cu->list_in_scope;
9352 origin_cu->list_in_scope = cu->list_in_scope;
9353
9354 if (die->tag != origin_die->tag
9355 && !(die->tag == DW_TAG_inlined_subroutine
9356 && origin_die->tag == DW_TAG_subprogram))
9357 complaint (&symfile_complaints,
9358 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
9359 die->offset.sect_off, origin_die->offset.sect_off);
9360
9361 child_die = die->child;
9362 die_children_count = 0;
9363 while (child_die && child_die->tag)
9364 {
9365 child_die = sibling_die (child_die);
9366 die_children_count++;
9367 }
9368 offsets = xmalloc (sizeof (*offsets) * die_children_count);
9369 cleanups = make_cleanup (xfree, offsets);
9370
9371 offsets_end = offsets;
9372 child_die = die->child;
9373 while (child_die && child_die->tag)
9374 {
9375 /* For each CHILD_DIE, find the corresponding child of
9376 ORIGIN_DIE. If there is more than one layer of
9377 DW_AT_abstract_origin, follow them all; there shouldn't be,
9378 but GCC versions at least through 4.4 generate this (GCC PR
9379 40573). */
9380 struct die_info *child_origin_die = child_die;
9381 struct dwarf2_cu *child_origin_cu = cu;
9382
9383 while (1)
9384 {
9385 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
9386 child_origin_cu);
9387 if (attr == NULL)
9388 break;
9389 child_origin_die = follow_die_ref (child_origin_die, attr,
9390 &child_origin_cu);
9391 }
9392
9393 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
9394 counterpart may exist. */
9395 if (child_origin_die != child_die)
9396 {
9397 if (child_die->tag != child_origin_die->tag
9398 && !(child_die->tag == DW_TAG_inlined_subroutine
9399 && child_origin_die->tag == DW_TAG_subprogram))
9400 complaint (&symfile_complaints,
9401 _("Child DIE 0x%x and its abstract origin 0x%x have "
9402 "different tags"), child_die->offset.sect_off,
9403 child_origin_die->offset.sect_off);
9404 if (child_origin_die->parent != origin_die)
9405 complaint (&symfile_complaints,
9406 _("Child DIE 0x%x and its abstract origin 0x%x have "
9407 "different parents"), child_die->offset.sect_off,
9408 child_origin_die->offset.sect_off);
9409 else
9410 *offsets_end++ = child_origin_die->offset;
9411 }
9412 child_die = sibling_die (child_die);
9413 }
9414 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
9415 unsigned_int_compar);
9416 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
9417 if (offsetp[-1].sect_off == offsetp->sect_off)
9418 complaint (&symfile_complaints,
9419 _("Multiple children of DIE 0x%x refer "
9420 "to DIE 0x%x as their abstract origin"),
9421 die->offset.sect_off, offsetp->sect_off);
9422
9423 offsetp = offsets;
9424 origin_child_die = origin_die->child;
9425 while (origin_child_die && origin_child_die->tag)
9426 {
9427 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
9428 while (offsetp < offsets_end
9429 && offsetp->sect_off < origin_child_die->offset.sect_off)
9430 offsetp++;
9431 if (offsetp >= offsets_end
9432 || offsetp->sect_off > origin_child_die->offset.sect_off)
9433 {
9434 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
9435 process_die (origin_child_die, origin_cu);
9436 }
9437 origin_child_die = sibling_die (origin_child_die);
9438 }
9439 origin_cu->list_in_scope = origin_previous_list_in_scope;
9440
9441 do_cleanups (cleanups);
9442 }
9443
9444 static void
9445 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
9446 {
9447 struct objfile *objfile = cu->objfile;
9448 struct context_stack *new;
9449 CORE_ADDR lowpc;
9450 CORE_ADDR highpc;
9451 struct die_info *child_die;
9452 struct attribute *attr, *call_line, *call_file;
9453 const char *name;
9454 CORE_ADDR baseaddr;
9455 struct block *block;
9456 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
9457 VEC (symbolp) *template_args = NULL;
9458 struct template_symbol *templ_func = NULL;
9459
9460 if (inlined_func)
9461 {
9462 /* If we do not have call site information, we can't show the
9463 caller of this inlined function. That's too confusing, so
9464 only use the scope for local variables. */
9465 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
9466 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
9467 if (call_line == NULL || call_file == NULL)
9468 {
9469 read_lexical_block_scope (die, cu);
9470 return;
9471 }
9472 }
9473
9474 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9475
9476 name = dwarf2_name (die, cu);
9477
9478 /* Ignore functions with missing or empty names. These are actually
9479 illegal according to the DWARF standard. */
9480 if (name == NULL)
9481 {
9482 complaint (&symfile_complaints,
9483 _("missing name for subprogram DIE at %d"),
9484 die->offset.sect_off);
9485 return;
9486 }
9487
9488 /* Ignore functions with missing or invalid low and high pc attributes. */
9489 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9490 {
9491 attr = dwarf2_attr (die, DW_AT_external, cu);
9492 if (!attr || !DW_UNSND (attr))
9493 complaint (&symfile_complaints,
9494 _("cannot get low and high bounds "
9495 "for subprogram DIE at %d"),
9496 die->offset.sect_off);
9497 return;
9498 }
9499
9500 lowpc += baseaddr;
9501 highpc += baseaddr;
9502
9503 /* If we have any template arguments, then we must allocate a
9504 different sort of symbol. */
9505 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
9506 {
9507 if (child_die->tag == DW_TAG_template_type_param
9508 || child_die->tag == DW_TAG_template_value_param)
9509 {
9510 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
9511 struct template_symbol);
9512 templ_func->base.is_cplus_template_function = 1;
9513 break;
9514 }
9515 }
9516
9517 new = push_context (0, lowpc);
9518 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
9519 (struct symbol *) templ_func);
9520
9521 /* If there is a location expression for DW_AT_frame_base, record
9522 it. */
9523 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
9524 if (attr)
9525 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
9526 expression is being recorded directly in the function's symbol
9527 and not in a separate frame-base object. I guess this hack is
9528 to avoid adding some sort of frame-base adjunct/annex to the
9529 function's symbol :-(. The problem with doing this is that it
9530 results in a function symbol with a location expression that
9531 has nothing to do with the location of the function, ouch! The
9532 relationship should be: a function's symbol has-a frame base; a
9533 frame-base has-a location expression. */
9534 dwarf2_symbol_mark_computed (attr, new->name, cu, 1);
9535
9536 cu->list_in_scope = &local_symbols;
9537
9538 if (die->child != NULL)
9539 {
9540 child_die = die->child;
9541 while (child_die && child_die->tag)
9542 {
9543 if (child_die->tag == DW_TAG_template_type_param
9544 || child_die->tag == DW_TAG_template_value_param)
9545 {
9546 struct symbol *arg = new_symbol (child_die, NULL, cu);
9547
9548 if (arg != NULL)
9549 VEC_safe_push (symbolp, template_args, arg);
9550 }
9551 else
9552 process_die (child_die, cu);
9553 child_die = sibling_die (child_die);
9554 }
9555 }
9556
9557 inherit_abstract_dies (die, cu);
9558
9559 /* If we have a DW_AT_specification, we might need to import using
9560 directives from the context of the specification DIE. See the
9561 comment in determine_prefix. */
9562 if (cu->language == language_cplus
9563 && dwarf2_attr (die, DW_AT_specification, cu))
9564 {
9565 struct dwarf2_cu *spec_cu = cu;
9566 struct die_info *spec_die = die_specification (die, &spec_cu);
9567
9568 while (spec_die)
9569 {
9570 child_die = spec_die->child;
9571 while (child_die && child_die->tag)
9572 {
9573 if (child_die->tag == DW_TAG_imported_module)
9574 process_die (child_die, spec_cu);
9575 child_die = sibling_die (child_die);
9576 }
9577
9578 /* In some cases, GCC generates specification DIEs that
9579 themselves contain DW_AT_specification attributes. */
9580 spec_die = die_specification (spec_die, &spec_cu);
9581 }
9582 }
9583
9584 new = pop_context ();
9585 /* Make a block for the local symbols within. */
9586 block = finish_block (new->name, &local_symbols, new->old_blocks,
9587 lowpc, highpc, objfile);
9588
9589 /* For C++, set the block's scope. */
9590 if ((cu->language == language_cplus || cu->language == language_fortran)
9591 && cu->processing_has_namespace_info)
9592 block_set_scope (block, determine_prefix (die, cu),
9593 &objfile->objfile_obstack);
9594
9595 /* If we have address ranges, record them. */
9596 dwarf2_record_block_ranges (die, block, baseaddr, cu);
9597
9598 /* Attach template arguments to function. */
9599 if (! VEC_empty (symbolp, template_args))
9600 {
9601 gdb_assert (templ_func != NULL);
9602
9603 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
9604 templ_func->template_arguments
9605 = obstack_alloc (&objfile->objfile_obstack,
9606 (templ_func->n_template_arguments
9607 * sizeof (struct symbol *)));
9608 memcpy (templ_func->template_arguments,
9609 VEC_address (symbolp, template_args),
9610 (templ_func->n_template_arguments * sizeof (struct symbol *)));
9611 VEC_free (symbolp, template_args);
9612 }
9613
9614 /* In C++, we can have functions nested inside functions (e.g., when
9615 a function declares a class that has methods). This means that
9616 when we finish processing a function scope, we may need to go
9617 back to building a containing block's symbol lists. */
9618 local_symbols = new->locals;
9619 using_directives = new->using_directives;
9620
9621 /* If we've finished processing a top-level function, subsequent
9622 symbols go in the file symbol list. */
9623 if (outermost_context_p ())
9624 cu->list_in_scope = &file_symbols;
9625 }
9626
9627 /* Process all the DIES contained within a lexical block scope. Start
9628 a new scope, process the dies, and then close the scope. */
9629
9630 static void
9631 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
9632 {
9633 struct objfile *objfile = cu->objfile;
9634 struct context_stack *new;
9635 CORE_ADDR lowpc, highpc;
9636 struct die_info *child_die;
9637 CORE_ADDR baseaddr;
9638
9639 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9640
9641 /* Ignore blocks with missing or invalid low and high pc attributes. */
9642 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
9643 as multiple lexical blocks? Handling children in a sane way would
9644 be nasty. Might be easier to properly extend generic blocks to
9645 describe ranges. */
9646 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9647 return;
9648 lowpc += baseaddr;
9649 highpc += baseaddr;
9650
9651 push_context (0, lowpc);
9652 if (die->child != NULL)
9653 {
9654 child_die = die->child;
9655 while (child_die && child_die->tag)
9656 {
9657 process_die (child_die, cu);
9658 child_die = sibling_die (child_die);
9659 }
9660 }
9661 new = pop_context ();
9662
9663 if (local_symbols != NULL || using_directives != NULL)
9664 {
9665 struct block *block
9666 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
9667 highpc, objfile);
9668
9669 /* Note that recording ranges after traversing children, as we
9670 do here, means that recording a parent's ranges entails
9671 walking across all its children's ranges as they appear in
9672 the address map, which is quadratic behavior.
9673
9674 It would be nicer to record the parent's ranges before
9675 traversing its children, simply overriding whatever you find
9676 there. But since we don't even decide whether to create a
9677 block until after we've traversed its children, that's hard
9678 to do. */
9679 dwarf2_record_block_ranges (die, block, baseaddr, cu);
9680 }
9681 local_symbols = new->locals;
9682 using_directives = new->using_directives;
9683 }
9684
9685 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
9686
9687 static void
9688 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
9689 {
9690 struct objfile *objfile = cu->objfile;
9691 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9692 CORE_ADDR pc, baseaddr;
9693 struct attribute *attr;
9694 struct call_site *call_site, call_site_local;
9695 void **slot;
9696 int nparams;
9697 struct die_info *child_die;
9698
9699 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9700
9701 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9702 if (!attr)
9703 {
9704 complaint (&symfile_complaints,
9705 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
9706 "DIE 0x%x [in module %s]"),
9707 die->offset.sect_off, objfile->name);
9708 return;
9709 }
9710 pc = DW_ADDR (attr) + baseaddr;
9711
9712 if (cu->call_site_htab == NULL)
9713 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
9714 NULL, &objfile->objfile_obstack,
9715 hashtab_obstack_allocate, NULL);
9716 call_site_local.pc = pc;
9717 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
9718 if (*slot != NULL)
9719 {
9720 complaint (&symfile_complaints,
9721 _("Duplicate PC %s for DW_TAG_GNU_call_site "
9722 "DIE 0x%x [in module %s]"),
9723 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
9724 return;
9725 }
9726
9727 /* Count parameters at the caller. */
9728
9729 nparams = 0;
9730 for (child_die = die->child; child_die && child_die->tag;
9731 child_die = sibling_die (child_die))
9732 {
9733 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9734 {
9735 complaint (&symfile_complaints,
9736 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
9737 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9738 child_die->tag, child_die->offset.sect_off, objfile->name);
9739 continue;
9740 }
9741
9742 nparams++;
9743 }
9744
9745 call_site = obstack_alloc (&objfile->objfile_obstack,
9746 (sizeof (*call_site)
9747 + (sizeof (*call_site->parameter)
9748 * (nparams - 1))));
9749 *slot = call_site;
9750 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
9751 call_site->pc = pc;
9752
9753 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
9754 {
9755 struct die_info *func_die;
9756
9757 /* Skip also over DW_TAG_inlined_subroutine. */
9758 for (func_die = die->parent;
9759 func_die && func_die->tag != DW_TAG_subprogram
9760 && func_die->tag != DW_TAG_subroutine_type;
9761 func_die = func_die->parent);
9762
9763 /* DW_AT_GNU_all_call_sites is a superset
9764 of DW_AT_GNU_all_tail_call_sites. */
9765 if (func_die
9766 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
9767 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
9768 {
9769 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
9770 not complete. But keep CALL_SITE for look ups via call_site_htab,
9771 both the initial caller containing the real return address PC and
9772 the final callee containing the current PC of a chain of tail
9773 calls do not need to have the tail call list complete. But any
9774 function candidate for a virtual tail call frame searched via
9775 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
9776 determined unambiguously. */
9777 }
9778 else
9779 {
9780 struct type *func_type = NULL;
9781
9782 if (func_die)
9783 func_type = get_die_type (func_die, cu);
9784 if (func_type != NULL)
9785 {
9786 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
9787
9788 /* Enlist this call site to the function. */
9789 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
9790 TYPE_TAIL_CALL_LIST (func_type) = call_site;
9791 }
9792 else
9793 complaint (&symfile_complaints,
9794 _("Cannot find function owning DW_TAG_GNU_call_site "
9795 "DIE 0x%x [in module %s]"),
9796 die->offset.sect_off, objfile->name);
9797 }
9798 }
9799
9800 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
9801 if (attr == NULL)
9802 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9803 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9804 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
9805 /* Keep NULL DWARF_BLOCK. */;
9806 else if (attr_form_is_block (attr))
9807 {
9808 struct dwarf2_locexpr_baton *dlbaton;
9809
9810 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
9811 dlbaton->data = DW_BLOCK (attr)->data;
9812 dlbaton->size = DW_BLOCK (attr)->size;
9813 dlbaton->per_cu = cu->per_cu;
9814
9815 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
9816 }
9817 else if (is_ref_attr (attr))
9818 {
9819 struct dwarf2_cu *target_cu = cu;
9820 struct die_info *target_die;
9821
9822 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
9823 gdb_assert (target_cu->objfile == objfile);
9824 if (die_is_declaration (target_die, target_cu))
9825 {
9826 const char *target_physname = NULL;
9827 struct attribute *target_attr;
9828
9829 /* Prefer the mangled name; otherwise compute the demangled one. */
9830 target_attr = dwarf2_attr (target_die, DW_AT_linkage_name, target_cu);
9831 if (target_attr == NULL)
9832 target_attr = dwarf2_attr (target_die, DW_AT_MIPS_linkage_name,
9833 target_cu);
9834 if (target_attr != NULL && DW_STRING (target_attr) != NULL)
9835 target_physname = DW_STRING (target_attr);
9836 else
9837 target_physname = dwarf2_physname (NULL, target_die, target_cu);
9838 if (target_physname == NULL)
9839 complaint (&symfile_complaints,
9840 _("DW_AT_GNU_call_site_target target DIE has invalid "
9841 "physname, for referencing DIE 0x%x [in module %s]"),
9842 die->offset.sect_off, objfile->name);
9843 else
9844 SET_FIELD_PHYSNAME (call_site->target, target_physname);
9845 }
9846 else
9847 {
9848 CORE_ADDR lowpc;
9849
9850 /* DW_AT_entry_pc should be preferred. */
9851 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
9852 complaint (&symfile_complaints,
9853 _("DW_AT_GNU_call_site_target target DIE has invalid "
9854 "low pc, for referencing DIE 0x%x [in module %s]"),
9855 die->offset.sect_off, objfile->name);
9856 else
9857 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
9858 }
9859 }
9860 else
9861 complaint (&symfile_complaints,
9862 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
9863 "block nor reference, for DIE 0x%x [in module %s]"),
9864 die->offset.sect_off, objfile->name);
9865
9866 call_site->per_cu = cu->per_cu;
9867
9868 for (child_die = die->child;
9869 child_die && child_die->tag;
9870 child_die = sibling_die (child_die))
9871 {
9872 struct call_site_parameter *parameter;
9873 struct attribute *loc, *origin;
9874
9875 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9876 {
9877 /* Already printed the complaint above. */
9878 continue;
9879 }
9880
9881 gdb_assert (call_site->parameter_count < nparams);
9882 parameter = &call_site->parameter[call_site->parameter_count];
9883
9884 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
9885 specifies DW_TAG_formal_parameter. Value of the data assumed for the
9886 register is contained in DW_AT_GNU_call_site_value. */
9887
9888 loc = dwarf2_attr (child_die, DW_AT_location, cu);
9889 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
9890 if (loc == NULL && origin != NULL && is_ref_attr (origin))
9891 {
9892 sect_offset offset;
9893
9894 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9895 offset = dwarf2_get_ref_die_offset (origin);
9896 if (!offset_in_cu_p (&cu->header, offset))
9897 {
9898 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
9899 binding can be done only inside one CU. Such referenced DIE
9900 therefore cannot be even moved to DW_TAG_partial_unit. */
9901 complaint (&symfile_complaints,
9902 _("DW_AT_abstract_origin offset is not in CU for "
9903 "DW_TAG_GNU_call_site child DIE 0x%x "
9904 "[in module %s]"),
9905 child_die->offset.sect_off, objfile->name);
9906 continue;
9907 }
9908 parameter->u.param_offset.cu_off = (offset.sect_off
9909 - cu->header.offset.sect_off);
9910 }
9911 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
9912 {
9913 complaint (&symfile_complaints,
9914 _("No DW_FORM_block* DW_AT_location for "
9915 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9916 child_die->offset.sect_off, objfile->name);
9917 continue;
9918 }
9919 else
9920 {
9921 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9922 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
9923 if (parameter->u.dwarf_reg != -1)
9924 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9925 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
9926 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
9927 &parameter->u.fb_offset))
9928 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
9929 else
9930 {
9931 complaint (&symfile_complaints,
9932 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
9933 "for DW_FORM_block* DW_AT_location is supported for "
9934 "DW_TAG_GNU_call_site child DIE 0x%x "
9935 "[in module %s]"),
9936 child_die->offset.sect_off, objfile->name);
9937 continue;
9938 }
9939 }
9940
9941 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
9942 if (!attr_form_is_block (attr))
9943 {
9944 complaint (&symfile_complaints,
9945 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
9946 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9947 child_die->offset.sect_off, objfile->name);
9948 continue;
9949 }
9950 parameter->value = DW_BLOCK (attr)->data;
9951 parameter->value_size = DW_BLOCK (attr)->size;
9952
9953 /* Parameters are not pre-cleared by memset above. */
9954 parameter->data_value = NULL;
9955 parameter->data_value_size = 0;
9956 call_site->parameter_count++;
9957
9958 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
9959 if (attr)
9960 {
9961 if (!attr_form_is_block (attr))
9962 complaint (&symfile_complaints,
9963 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
9964 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9965 child_die->offset.sect_off, objfile->name);
9966 else
9967 {
9968 parameter->data_value = DW_BLOCK (attr)->data;
9969 parameter->data_value_size = DW_BLOCK (attr)->size;
9970 }
9971 }
9972 }
9973 }
9974
9975 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
9976 Return 1 if the attributes are present and valid, otherwise, return 0.
9977 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
9978
9979 static int
9980 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
9981 CORE_ADDR *high_return, struct dwarf2_cu *cu,
9982 struct partial_symtab *ranges_pst)
9983 {
9984 struct objfile *objfile = cu->objfile;
9985 struct comp_unit_head *cu_header = &cu->header;
9986 bfd *obfd = objfile->obfd;
9987 unsigned int addr_size = cu_header->addr_size;
9988 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
9989 /* Base address selection entry. */
9990 CORE_ADDR base;
9991 int found_base;
9992 unsigned int dummy;
9993 gdb_byte *buffer;
9994 CORE_ADDR marker;
9995 int low_set;
9996 CORE_ADDR low = 0;
9997 CORE_ADDR high = 0;
9998 CORE_ADDR baseaddr;
9999
10000 found_base = cu->base_known;
10001 base = cu->base_address;
10002
10003 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
10004 if (offset >= dwarf2_per_objfile->ranges.size)
10005 {
10006 complaint (&symfile_complaints,
10007 _("Offset %d out of bounds for DW_AT_ranges attribute"),
10008 offset);
10009 return 0;
10010 }
10011 buffer = dwarf2_per_objfile->ranges.buffer + offset;
10012
10013 /* Read in the largest possible address. */
10014 marker = read_address (obfd, buffer, cu, &dummy);
10015 if ((marker & mask) == mask)
10016 {
10017 /* If we found the largest possible address, then
10018 read the base address. */
10019 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10020 buffer += 2 * addr_size;
10021 offset += 2 * addr_size;
10022 found_base = 1;
10023 }
10024
10025 low_set = 0;
10026
10027 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10028
10029 while (1)
10030 {
10031 CORE_ADDR range_beginning, range_end;
10032
10033 range_beginning = read_address (obfd, buffer, cu, &dummy);
10034 buffer += addr_size;
10035 range_end = read_address (obfd, buffer, cu, &dummy);
10036 buffer += addr_size;
10037 offset += 2 * addr_size;
10038
10039 /* An end of list marker is a pair of zero addresses. */
10040 if (range_beginning == 0 && range_end == 0)
10041 /* Found the end of list entry. */
10042 break;
10043
10044 /* Each base address selection entry is a pair of 2 values.
10045 The first is the largest possible address, the second is
10046 the base address. Check for a base address here. */
10047 if ((range_beginning & mask) == mask)
10048 {
10049 /* If we found the largest possible address, then
10050 read the base address. */
10051 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10052 found_base = 1;
10053 continue;
10054 }
10055
10056 if (!found_base)
10057 {
10058 /* We have no valid base address for the ranges
10059 data. */
10060 complaint (&symfile_complaints,
10061 _("Invalid .debug_ranges data (no base address)"));
10062 return 0;
10063 }
10064
10065 if (range_beginning > range_end)
10066 {
10067 /* Inverted range entries are invalid. */
10068 complaint (&symfile_complaints,
10069 _("Invalid .debug_ranges data (inverted range)"));
10070 return 0;
10071 }
10072
10073 /* Empty range entries have no effect. */
10074 if (range_beginning == range_end)
10075 continue;
10076
10077 range_beginning += base;
10078 range_end += base;
10079
10080 /* A not-uncommon case of bad debug info.
10081 Don't pollute the addrmap with bad data. */
10082 if (range_beginning + baseaddr == 0
10083 && !dwarf2_per_objfile->has_section_at_zero)
10084 {
10085 complaint (&symfile_complaints,
10086 _(".debug_ranges entry has start address of zero"
10087 " [in module %s]"), objfile->name);
10088 continue;
10089 }
10090
10091 if (ranges_pst != NULL)
10092 addrmap_set_empty (objfile->psymtabs_addrmap,
10093 range_beginning + baseaddr,
10094 range_end - 1 + baseaddr,
10095 ranges_pst);
10096
10097 /* FIXME: This is recording everything as a low-high
10098 segment of consecutive addresses. We should have a
10099 data structure for discontiguous block ranges
10100 instead. */
10101 if (! low_set)
10102 {
10103 low = range_beginning;
10104 high = range_end;
10105 low_set = 1;
10106 }
10107 else
10108 {
10109 if (range_beginning < low)
10110 low = range_beginning;
10111 if (range_end > high)
10112 high = range_end;
10113 }
10114 }
10115
10116 if (! low_set)
10117 /* If the first entry is an end-of-list marker, the range
10118 describes an empty scope, i.e. no instructions. */
10119 return 0;
10120
10121 if (low_return)
10122 *low_return = low;
10123 if (high_return)
10124 *high_return = high;
10125 return 1;
10126 }
10127
10128 /* Get low and high pc attributes from a die. Return 1 if the attributes
10129 are present and valid, otherwise, return 0. Return -1 if the range is
10130 discontinuous, i.e. derived from DW_AT_ranges information. */
10131
10132 static int
10133 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
10134 CORE_ADDR *highpc, struct dwarf2_cu *cu,
10135 struct partial_symtab *pst)
10136 {
10137 struct attribute *attr;
10138 struct attribute *attr_high;
10139 CORE_ADDR low = 0;
10140 CORE_ADDR high = 0;
10141 int ret = 0;
10142
10143 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10144 if (attr_high)
10145 {
10146 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10147 if (attr)
10148 {
10149 low = DW_ADDR (attr);
10150 if (attr_high->form == DW_FORM_addr
10151 || attr_high->form == DW_FORM_GNU_addr_index)
10152 high = DW_ADDR (attr_high);
10153 else
10154 high = low + DW_UNSND (attr_high);
10155 }
10156 else
10157 /* Found high w/o low attribute. */
10158 return 0;
10159
10160 /* Found consecutive range of addresses. */
10161 ret = 1;
10162 }
10163 else
10164 {
10165 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10166 if (attr != NULL)
10167 {
10168 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10169 We take advantage of the fact that DW_AT_ranges does not appear
10170 in DW_TAG_compile_unit of DWO files. */
10171 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10172 unsigned int ranges_offset = (DW_UNSND (attr)
10173 + (need_ranges_base
10174 ? cu->ranges_base
10175 : 0));
10176
10177 /* Value of the DW_AT_ranges attribute is the offset in the
10178 .debug_ranges section. */
10179 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
10180 return 0;
10181 /* Found discontinuous range of addresses. */
10182 ret = -1;
10183 }
10184 }
10185
10186 /* read_partial_die has also the strict LOW < HIGH requirement. */
10187 if (high <= low)
10188 return 0;
10189
10190 /* When using the GNU linker, .gnu.linkonce. sections are used to
10191 eliminate duplicate copies of functions and vtables and such.
10192 The linker will arbitrarily choose one and discard the others.
10193 The AT_*_pc values for such functions refer to local labels in
10194 these sections. If the section from that file was discarded, the
10195 labels are not in the output, so the relocs get a value of 0.
10196 If this is a discarded function, mark the pc bounds as invalid,
10197 so that GDB will ignore it. */
10198 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
10199 return 0;
10200
10201 *lowpc = low;
10202 if (highpc)
10203 *highpc = high;
10204 return ret;
10205 }
10206
10207 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
10208 its low and high PC addresses. Do nothing if these addresses could not
10209 be determined. Otherwise, set LOWPC to the low address if it is smaller,
10210 and HIGHPC to the high address if greater than HIGHPC. */
10211
10212 static void
10213 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
10214 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10215 struct dwarf2_cu *cu)
10216 {
10217 CORE_ADDR low, high;
10218 struct die_info *child = die->child;
10219
10220 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
10221 {
10222 *lowpc = min (*lowpc, low);
10223 *highpc = max (*highpc, high);
10224 }
10225
10226 /* If the language does not allow nested subprograms (either inside
10227 subprograms or lexical blocks), we're done. */
10228 if (cu->language != language_ada)
10229 return;
10230
10231 /* Check all the children of the given DIE. If it contains nested
10232 subprograms, then check their pc bounds. Likewise, we need to
10233 check lexical blocks as well, as they may also contain subprogram
10234 definitions. */
10235 while (child && child->tag)
10236 {
10237 if (child->tag == DW_TAG_subprogram
10238 || child->tag == DW_TAG_lexical_block)
10239 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
10240 child = sibling_die (child);
10241 }
10242 }
10243
10244 /* Get the low and high pc's represented by the scope DIE, and store
10245 them in *LOWPC and *HIGHPC. If the correct values can't be
10246 determined, set *LOWPC to -1 and *HIGHPC to 0. */
10247
10248 static void
10249 get_scope_pc_bounds (struct die_info *die,
10250 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10251 struct dwarf2_cu *cu)
10252 {
10253 CORE_ADDR best_low = (CORE_ADDR) -1;
10254 CORE_ADDR best_high = (CORE_ADDR) 0;
10255 CORE_ADDR current_low, current_high;
10256
10257 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
10258 {
10259 best_low = current_low;
10260 best_high = current_high;
10261 }
10262 else
10263 {
10264 struct die_info *child = die->child;
10265
10266 while (child && child->tag)
10267 {
10268 switch (child->tag) {
10269 case DW_TAG_subprogram:
10270 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
10271 break;
10272 case DW_TAG_namespace:
10273 case DW_TAG_module:
10274 /* FIXME: carlton/2004-01-16: Should we do this for
10275 DW_TAG_class_type/DW_TAG_structure_type, too? I think
10276 that current GCC's always emit the DIEs corresponding
10277 to definitions of methods of classes as children of a
10278 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
10279 the DIEs giving the declarations, which could be
10280 anywhere). But I don't see any reason why the
10281 standards says that they have to be there. */
10282 get_scope_pc_bounds (child, &current_low, &current_high, cu);
10283
10284 if (current_low != ((CORE_ADDR) -1))
10285 {
10286 best_low = min (best_low, current_low);
10287 best_high = max (best_high, current_high);
10288 }
10289 break;
10290 default:
10291 /* Ignore. */
10292 break;
10293 }
10294
10295 child = sibling_die (child);
10296 }
10297 }
10298
10299 *lowpc = best_low;
10300 *highpc = best_high;
10301 }
10302
10303 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
10304 in DIE. */
10305
10306 static void
10307 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
10308 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
10309 {
10310 struct objfile *objfile = cu->objfile;
10311 struct attribute *attr;
10312 struct attribute *attr_high;
10313
10314 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10315 if (attr_high)
10316 {
10317 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10318 if (attr)
10319 {
10320 CORE_ADDR low = DW_ADDR (attr);
10321 CORE_ADDR high;
10322 if (attr_high->form == DW_FORM_addr
10323 || attr_high->form == DW_FORM_GNU_addr_index)
10324 high = DW_ADDR (attr_high);
10325 else
10326 high = low + DW_UNSND (attr_high);
10327
10328 record_block_range (block, baseaddr + low, baseaddr + high - 1);
10329 }
10330 }
10331
10332 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10333 if (attr)
10334 {
10335 bfd *obfd = objfile->obfd;
10336 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10337 We take advantage of the fact that DW_AT_ranges does not appear
10338 in DW_TAG_compile_unit of DWO files. */
10339 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10340
10341 /* The value of the DW_AT_ranges attribute is the offset of the
10342 address range list in the .debug_ranges section. */
10343 unsigned long offset = (DW_UNSND (attr)
10344 + (need_ranges_base ? cu->ranges_base : 0));
10345 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
10346
10347 /* For some target architectures, but not others, the
10348 read_address function sign-extends the addresses it returns.
10349 To recognize base address selection entries, we need a
10350 mask. */
10351 unsigned int addr_size = cu->header.addr_size;
10352 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10353
10354 /* The base address, to which the next pair is relative. Note
10355 that this 'base' is a DWARF concept: most entries in a range
10356 list are relative, to reduce the number of relocs against the
10357 debugging information. This is separate from this function's
10358 'baseaddr' argument, which GDB uses to relocate debugging
10359 information from a shared library based on the address at
10360 which the library was loaded. */
10361 CORE_ADDR base = cu->base_address;
10362 int base_known = cu->base_known;
10363
10364 gdb_assert (dwarf2_per_objfile->ranges.readin);
10365 if (offset >= dwarf2_per_objfile->ranges.size)
10366 {
10367 complaint (&symfile_complaints,
10368 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
10369 offset);
10370 return;
10371 }
10372
10373 for (;;)
10374 {
10375 unsigned int bytes_read;
10376 CORE_ADDR start, end;
10377
10378 start = read_address (obfd, buffer, cu, &bytes_read);
10379 buffer += bytes_read;
10380 end = read_address (obfd, buffer, cu, &bytes_read);
10381 buffer += bytes_read;
10382
10383 /* Did we find the end of the range list? */
10384 if (start == 0 && end == 0)
10385 break;
10386
10387 /* Did we find a base address selection entry? */
10388 else if ((start & base_select_mask) == base_select_mask)
10389 {
10390 base = end;
10391 base_known = 1;
10392 }
10393
10394 /* We found an ordinary address range. */
10395 else
10396 {
10397 if (!base_known)
10398 {
10399 complaint (&symfile_complaints,
10400 _("Invalid .debug_ranges data "
10401 "(no base address)"));
10402 return;
10403 }
10404
10405 if (start > end)
10406 {
10407 /* Inverted range entries are invalid. */
10408 complaint (&symfile_complaints,
10409 _("Invalid .debug_ranges data "
10410 "(inverted range)"));
10411 return;
10412 }
10413
10414 /* Empty range entries have no effect. */
10415 if (start == end)
10416 continue;
10417
10418 start += base + baseaddr;
10419 end += base + baseaddr;
10420
10421 /* A not-uncommon case of bad debug info.
10422 Don't pollute the addrmap with bad data. */
10423 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
10424 {
10425 complaint (&symfile_complaints,
10426 _(".debug_ranges entry has start address of zero"
10427 " [in module %s]"), objfile->name);
10428 continue;
10429 }
10430
10431 record_block_range (block, start, end - 1);
10432 }
10433 }
10434 }
10435 }
10436
10437 /* Check whether the producer field indicates either of GCC < 4.6, or the
10438 Intel C/C++ compiler, and cache the result in CU. */
10439
10440 static void
10441 check_producer (struct dwarf2_cu *cu)
10442 {
10443 const char *cs;
10444 int major, minor, release;
10445
10446 if (cu->producer == NULL)
10447 {
10448 /* For unknown compilers expect their behavior is DWARF version
10449 compliant.
10450
10451 GCC started to support .debug_types sections by -gdwarf-4 since
10452 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
10453 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
10454 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
10455 interpreted incorrectly by GDB now - GCC PR debug/48229. */
10456 }
10457 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
10458 {
10459 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
10460
10461 cs = &cu->producer[strlen ("GNU ")];
10462 while (*cs && !isdigit (*cs))
10463 cs++;
10464 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
10465 {
10466 /* Not recognized as GCC. */
10467 }
10468 else
10469 {
10470 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
10471 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
10472 }
10473 }
10474 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
10475 cu->producer_is_icc = 1;
10476 else
10477 {
10478 /* For other non-GCC compilers, expect their behavior is DWARF version
10479 compliant. */
10480 }
10481
10482 cu->checked_producer = 1;
10483 }
10484
10485 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
10486 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
10487 during 4.6.0 experimental. */
10488
10489 static int
10490 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
10491 {
10492 if (!cu->checked_producer)
10493 check_producer (cu);
10494
10495 return cu->producer_is_gxx_lt_4_6;
10496 }
10497
10498 /* Return the default accessibility type if it is not overriden by
10499 DW_AT_accessibility. */
10500
10501 static enum dwarf_access_attribute
10502 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
10503 {
10504 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
10505 {
10506 /* The default DWARF 2 accessibility for members is public, the default
10507 accessibility for inheritance is private. */
10508
10509 if (die->tag != DW_TAG_inheritance)
10510 return DW_ACCESS_public;
10511 else
10512 return DW_ACCESS_private;
10513 }
10514 else
10515 {
10516 /* DWARF 3+ defines the default accessibility a different way. The same
10517 rules apply now for DW_TAG_inheritance as for the members and it only
10518 depends on the container kind. */
10519
10520 if (die->parent->tag == DW_TAG_class_type)
10521 return DW_ACCESS_private;
10522 else
10523 return DW_ACCESS_public;
10524 }
10525 }
10526
10527 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
10528 offset. If the attribute was not found return 0, otherwise return
10529 1. If it was found but could not properly be handled, set *OFFSET
10530 to 0. */
10531
10532 static int
10533 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
10534 LONGEST *offset)
10535 {
10536 struct attribute *attr;
10537
10538 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
10539 if (attr != NULL)
10540 {
10541 *offset = 0;
10542
10543 /* Note that we do not check for a section offset first here.
10544 This is because DW_AT_data_member_location is new in DWARF 4,
10545 so if we see it, we can assume that a constant form is really
10546 a constant and not a section offset. */
10547 if (attr_form_is_constant (attr))
10548 *offset = dwarf2_get_attr_constant_value (attr, 0);
10549 else if (attr_form_is_section_offset (attr))
10550 dwarf2_complex_location_expr_complaint ();
10551 else if (attr_form_is_block (attr))
10552 *offset = decode_locdesc (DW_BLOCK (attr), cu);
10553 else
10554 dwarf2_complex_location_expr_complaint ();
10555
10556 return 1;
10557 }
10558
10559 return 0;
10560 }
10561
10562 /* Add an aggregate field to the field list. */
10563
10564 static void
10565 dwarf2_add_field (struct field_info *fip, struct die_info *die,
10566 struct dwarf2_cu *cu)
10567 {
10568 struct objfile *objfile = cu->objfile;
10569 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10570 struct nextfield *new_field;
10571 struct attribute *attr;
10572 struct field *fp;
10573 const char *fieldname = "";
10574
10575 /* Allocate a new field list entry and link it in. */
10576 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
10577 make_cleanup (xfree, new_field);
10578 memset (new_field, 0, sizeof (struct nextfield));
10579
10580 if (die->tag == DW_TAG_inheritance)
10581 {
10582 new_field->next = fip->baseclasses;
10583 fip->baseclasses = new_field;
10584 }
10585 else
10586 {
10587 new_field->next = fip->fields;
10588 fip->fields = new_field;
10589 }
10590 fip->nfields++;
10591
10592 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
10593 if (attr)
10594 new_field->accessibility = DW_UNSND (attr);
10595 else
10596 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
10597 if (new_field->accessibility != DW_ACCESS_public)
10598 fip->non_public_fields = 1;
10599
10600 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
10601 if (attr)
10602 new_field->virtuality = DW_UNSND (attr);
10603 else
10604 new_field->virtuality = DW_VIRTUALITY_none;
10605
10606 fp = &new_field->field;
10607
10608 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
10609 {
10610 LONGEST offset;
10611
10612 /* Data member other than a C++ static data member. */
10613
10614 /* Get type of field. */
10615 fp->type = die_type (die, cu);
10616
10617 SET_FIELD_BITPOS (*fp, 0);
10618
10619 /* Get bit size of field (zero if none). */
10620 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
10621 if (attr)
10622 {
10623 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
10624 }
10625 else
10626 {
10627 FIELD_BITSIZE (*fp) = 0;
10628 }
10629
10630 /* Get bit offset of field. */
10631 if (handle_data_member_location (die, cu, &offset))
10632 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
10633 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
10634 if (attr)
10635 {
10636 if (gdbarch_bits_big_endian (gdbarch))
10637 {
10638 /* For big endian bits, the DW_AT_bit_offset gives the
10639 additional bit offset from the MSB of the containing
10640 anonymous object to the MSB of the field. We don't
10641 have to do anything special since we don't need to
10642 know the size of the anonymous object. */
10643 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
10644 }
10645 else
10646 {
10647 /* For little endian bits, compute the bit offset to the
10648 MSB of the anonymous object, subtract off the number of
10649 bits from the MSB of the field to the MSB of the
10650 object, and then subtract off the number of bits of
10651 the field itself. The result is the bit offset of
10652 the LSB of the field. */
10653 int anonymous_size;
10654 int bit_offset = DW_UNSND (attr);
10655
10656 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10657 if (attr)
10658 {
10659 /* The size of the anonymous object containing
10660 the bit field is explicit, so use the
10661 indicated size (in bytes). */
10662 anonymous_size = DW_UNSND (attr);
10663 }
10664 else
10665 {
10666 /* The size of the anonymous object containing
10667 the bit field must be inferred from the type
10668 attribute of the data member containing the
10669 bit field. */
10670 anonymous_size = TYPE_LENGTH (fp->type);
10671 }
10672 SET_FIELD_BITPOS (*fp,
10673 (FIELD_BITPOS (*fp)
10674 + anonymous_size * bits_per_byte
10675 - bit_offset - FIELD_BITSIZE (*fp)));
10676 }
10677 }
10678
10679 /* Get name of field. */
10680 fieldname = dwarf2_name (die, cu);
10681 if (fieldname == NULL)
10682 fieldname = "";
10683
10684 /* The name is already allocated along with this objfile, so we don't
10685 need to duplicate it for the type. */
10686 fp->name = fieldname;
10687
10688 /* Change accessibility for artificial fields (e.g. virtual table
10689 pointer or virtual base class pointer) to private. */
10690 if (dwarf2_attr (die, DW_AT_artificial, cu))
10691 {
10692 FIELD_ARTIFICIAL (*fp) = 1;
10693 new_field->accessibility = DW_ACCESS_private;
10694 fip->non_public_fields = 1;
10695 }
10696 }
10697 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
10698 {
10699 /* C++ static member. */
10700
10701 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
10702 is a declaration, but all versions of G++ as of this writing
10703 (so through at least 3.2.1) incorrectly generate
10704 DW_TAG_variable tags. */
10705
10706 const char *physname;
10707
10708 /* Get name of field. */
10709 fieldname = dwarf2_name (die, cu);
10710 if (fieldname == NULL)
10711 return;
10712
10713 attr = dwarf2_attr (die, DW_AT_const_value, cu);
10714 if (attr
10715 /* Only create a symbol if this is an external value.
10716 new_symbol checks this and puts the value in the global symbol
10717 table, which we want. If it is not external, new_symbol
10718 will try to put the value in cu->list_in_scope which is wrong. */
10719 && dwarf2_flag_true_p (die, DW_AT_external, cu))
10720 {
10721 /* A static const member, not much different than an enum as far as
10722 we're concerned, except that we can support more types. */
10723 new_symbol (die, NULL, cu);
10724 }
10725
10726 /* Get physical name. */
10727 physname = dwarf2_physname (fieldname, die, cu);
10728
10729 /* The name is already allocated along with this objfile, so we don't
10730 need to duplicate it for the type. */
10731 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
10732 FIELD_TYPE (*fp) = die_type (die, cu);
10733 FIELD_NAME (*fp) = fieldname;
10734 }
10735 else if (die->tag == DW_TAG_inheritance)
10736 {
10737 LONGEST offset;
10738
10739 /* C++ base class field. */
10740 if (handle_data_member_location (die, cu, &offset))
10741 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
10742 FIELD_BITSIZE (*fp) = 0;
10743 FIELD_TYPE (*fp) = die_type (die, cu);
10744 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
10745 fip->nbaseclasses++;
10746 }
10747 }
10748
10749 /* Add a typedef defined in the scope of the FIP's class. */
10750
10751 static void
10752 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
10753 struct dwarf2_cu *cu)
10754 {
10755 struct objfile *objfile = cu->objfile;
10756 struct typedef_field_list *new_field;
10757 struct attribute *attr;
10758 struct typedef_field *fp;
10759 char *fieldname = "";
10760
10761 /* Allocate a new field list entry and link it in. */
10762 new_field = xzalloc (sizeof (*new_field));
10763 make_cleanup (xfree, new_field);
10764
10765 gdb_assert (die->tag == DW_TAG_typedef);
10766
10767 fp = &new_field->field;
10768
10769 /* Get name of field. */
10770 fp->name = dwarf2_name (die, cu);
10771 if (fp->name == NULL)
10772 return;
10773
10774 fp->type = read_type_die (die, cu);
10775
10776 new_field->next = fip->typedef_field_list;
10777 fip->typedef_field_list = new_field;
10778 fip->typedef_field_list_count++;
10779 }
10780
10781 /* Create the vector of fields, and attach it to the type. */
10782
10783 static void
10784 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
10785 struct dwarf2_cu *cu)
10786 {
10787 int nfields = fip->nfields;
10788
10789 /* Record the field count, allocate space for the array of fields,
10790 and create blank accessibility bitfields if necessary. */
10791 TYPE_NFIELDS (type) = nfields;
10792 TYPE_FIELDS (type) = (struct field *)
10793 TYPE_ALLOC (type, sizeof (struct field) * nfields);
10794 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
10795
10796 if (fip->non_public_fields && cu->language != language_ada)
10797 {
10798 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10799
10800 TYPE_FIELD_PRIVATE_BITS (type) =
10801 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10802 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
10803
10804 TYPE_FIELD_PROTECTED_BITS (type) =
10805 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10806 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
10807
10808 TYPE_FIELD_IGNORE_BITS (type) =
10809 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10810 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
10811 }
10812
10813 /* If the type has baseclasses, allocate and clear a bit vector for
10814 TYPE_FIELD_VIRTUAL_BITS. */
10815 if (fip->nbaseclasses && cu->language != language_ada)
10816 {
10817 int num_bytes = B_BYTES (fip->nbaseclasses);
10818 unsigned char *pointer;
10819
10820 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10821 pointer = TYPE_ALLOC (type, num_bytes);
10822 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
10823 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
10824 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
10825 }
10826
10827 /* Copy the saved-up fields into the field vector. Start from the head of
10828 the list, adding to the tail of the field array, so that they end up in
10829 the same order in the array in which they were added to the list. */
10830 while (nfields-- > 0)
10831 {
10832 struct nextfield *fieldp;
10833
10834 if (fip->fields)
10835 {
10836 fieldp = fip->fields;
10837 fip->fields = fieldp->next;
10838 }
10839 else
10840 {
10841 fieldp = fip->baseclasses;
10842 fip->baseclasses = fieldp->next;
10843 }
10844
10845 TYPE_FIELD (type, nfields) = fieldp->field;
10846 switch (fieldp->accessibility)
10847 {
10848 case DW_ACCESS_private:
10849 if (cu->language != language_ada)
10850 SET_TYPE_FIELD_PRIVATE (type, nfields);
10851 break;
10852
10853 case DW_ACCESS_protected:
10854 if (cu->language != language_ada)
10855 SET_TYPE_FIELD_PROTECTED (type, nfields);
10856 break;
10857
10858 case DW_ACCESS_public:
10859 break;
10860
10861 default:
10862 /* Unknown accessibility. Complain and treat it as public. */
10863 {
10864 complaint (&symfile_complaints, _("unsupported accessibility %d"),
10865 fieldp->accessibility);
10866 }
10867 break;
10868 }
10869 if (nfields < fip->nbaseclasses)
10870 {
10871 switch (fieldp->virtuality)
10872 {
10873 case DW_VIRTUALITY_virtual:
10874 case DW_VIRTUALITY_pure_virtual:
10875 if (cu->language == language_ada)
10876 error (_("unexpected virtuality in component of Ada type"));
10877 SET_TYPE_FIELD_VIRTUAL (type, nfields);
10878 break;
10879 }
10880 }
10881 }
10882 }
10883
10884 /* Return true if this member function is a constructor, false
10885 otherwise. */
10886
10887 static int
10888 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
10889 {
10890 const char *fieldname;
10891 const char *typename;
10892 int len;
10893
10894 if (die->parent == NULL)
10895 return 0;
10896
10897 if (die->parent->tag != DW_TAG_structure_type
10898 && die->parent->tag != DW_TAG_union_type
10899 && die->parent->tag != DW_TAG_class_type)
10900 return 0;
10901
10902 fieldname = dwarf2_name (die, cu);
10903 typename = dwarf2_name (die->parent, cu);
10904 if (fieldname == NULL || typename == NULL)
10905 return 0;
10906
10907 len = strlen (fieldname);
10908 return (strncmp (fieldname, typename, len) == 0
10909 && (typename[len] == '\0' || typename[len] == '<'));
10910 }
10911
10912 /* Add a member function to the proper fieldlist. */
10913
10914 static void
10915 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
10916 struct type *type, struct dwarf2_cu *cu)
10917 {
10918 struct objfile *objfile = cu->objfile;
10919 struct attribute *attr;
10920 struct fnfieldlist *flp;
10921 int i;
10922 struct fn_field *fnp;
10923 const char *fieldname;
10924 struct nextfnfield *new_fnfield;
10925 struct type *this_type;
10926 enum dwarf_access_attribute accessibility;
10927
10928 if (cu->language == language_ada)
10929 error (_("unexpected member function in Ada type"));
10930
10931 /* Get name of member function. */
10932 fieldname = dwarf2_name (die, cu);
10933 if (fieldname == NULL)
10934 return;
10935
10936 /* Look up member function name in fieldlist. */
10937 for (i = 0; i < fip->nfnfields; i++)
10938 {
10939 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
10940 break;
10941 }
10942
10943 /* Create new list element if necessary. */
10944 if (i < fip->nfnfields)
10945 flp = &fip->fnfieldlists[i];
10946 else
10947 {
10948 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
10949 {
10950 fip->fnfieldlists = (struct fnfieldlist *)
10951 xrealloc (fip->fnfieldlists,
10952 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
10953 * sizeof (struct fnfieldlist));
10954 if (fip->nfnfields == 0)
10955 make_cleanup (free_current_contents, &fip->fnfieldlists);
10956 }
10957 flp = &fip->fnfieldlists[fip->nfnfields];
10958 flp->name = fieldname;
10959 flp->length = 0;
10960 flp->head = NULL;
10961 i = fip->nfnfields++;
10962 }
10963
10964 /* Create a new member function field and chain it to the field list
10965 entry. */
10966 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
10967 make_cleanup (xfree, new_fnfield);
10968 memset (new_fnfield, 0, sizeof (struct nextfnfield));
10969 new_fnfield->next = flp->head;
10970 flp->head = new_fnfield;
10971 flp->length++;
10972
10973 /* Fill in the member function field info. */
10974 fnp = &new_fnfield->fnfield;
10975
10976 /* Delay processing of the physname until later. */
10977 if (cu->language == language_cplus || cu->language == language_java)
10978 {
10979 add_to_method_list (type, i, flp->length - 1, fieldname,
10980 die, cu);
10981 }
10982 else
10983 {
10984 const char *physname = dwarf2_physname (fieldname, die, cu);
10985 fnp->physname = physname ? physname : "";
10986 }
10987
10988 fnp->type = alloc_type (objfile);
10989 this_type = read_type_die (die, cu);
10990 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
10991 {
10992 int nparams = TYPE_NFIELDS (this_type);
10993
10994 /* TYPE is the domain of this method, and THIS_TYPE is the type
10995 of the method itself (TYPE_CODE_METHOD). */
10996 smash_to_method_type (fnp->type, type,
10997 TYPE_TARGET_TYPE (this_type),
10998 TYPE_FIELDS (this_type),
10999 TYPE_NFIELDS (this_type),
11000 TYPE_VARARGS (this_type));
11001
11002 /* Handle static member functions.
11003 Dwarf2 has no clean way to discern C++ static and non-static
11004 member functions. G++ helps GDB by marking the first
11005 parameter for non-static member functions (which is the this
11006 pointer) as artificial. We obtain this information from
11007 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
11008 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
11009 fnp->voffset = VOFFSET_STATIC;
11010 }
11011 else
11012 complaint (&symfile_complaints, _("member function type missing for '%s'"),
11013 dwarf2_full_name (fieldname, die, cu));
11014
11015 /* Get fcontext from DW_AT_containing_type if present. */
11016 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
11017 fnp->fcontext = die_containing_type (die, cu);
11018
11019 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
11020 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
11021
11022 /* Get accessibility. */
11023 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
11024 if (attr)
11025 accessibility = DW_UNSND (attr);
11026 else
11027 accessibility = dwarf2_default_access_attribute (die, cu);
11028 switch (accessibility)
11029 {
11030 case DW_ACCESS_private:
11031 fnp->is_private = 1;
11032 break;
11033 case DW_ACCESS_protected:
11034 fnp->is_protected = 1;
11035 break;
11036 }
11037
11038 /* Check for artificial methods. */
11039 attr = dwarf2_attr (die, DW_AT_artificial, cu);
11040 if (attr && DW_UNSND (attr) != 0)
11041 fnp->is_artificial = 1;
11042
11043 fnp->is_constructor = dwarf2_is_constructor (die, cu);
11044
11045 /* Get index in virtual function table if it is a virtual member
11046 function. For older versions of GCC, this is an offset in the
11047 appropriate virtual table, as specified by DW_AT_containing_type.
11048 For everyone else, it is an expression to be evaluated relative
11049 to the object address. */
11050
11051 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
11052 if (attr)
11053 {
11054 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
11055 {
11056 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
11057 {
11058 /* Old-style GCC. */
11059 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
11060 }
11061 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
11062 || (DW_BLOCK (attr)->size > 1
11063 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
11064 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
11065 {
11066 struct dwarf_block blk;
11067 int offset;
11068
11069 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
11070 ? 1 : 2);
11071 blk.size = DW_BLOCK (attr)->size - offset;
11072 blk.data = DW_BLOCK (attr)->data + offset;
11073 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
11074 if ((fnp->voffset % cu->header.addr_size) != 0)
11075 dwarf2_complex_location_expr_complaint ();
11076 else
11077 fnp->voffset /= cu->header.addr_size;
11078 fnp->voffset += 2;
11079 }
11080 else
11081 dwarf2_complex_location_expr_complaint ();
11082
11083 if (!fnp->fcontext)
11084 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
11085 }
11086 else if (attr_form_is_section_offset (attr))
11087 {
11088 dwarf2_complex_location_expr_complaint ();
11089 }
11090 else
11091 {
11092 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
11093 fieldname);
11094 }
11095 }
11096 else
11097 {
11098 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11099 if (attr && DW_UNSND (attr))
11100 {
11101 /* GCC does this, as of 2008-08-25; PR debug/37237. */
11102 complaint (&symfile_complaints,
11103 _("Member function \"%s\" (offset %d) is virtual "
11104 "but the vtable offset is not specified"),
11105 fieldname, die->offset.sect_off);
11106 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11107 TYPE_CPLUS_DYNAMIC (type) = 1;
11108 }
11109 }
11110 }
11111
11112 /* Create the vector of member function fields, and attach it to the type. */
11113
11114 static void
11115 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
11116 struct dwarf2_cu *cu)
11117 {
11118 struct fnfieldlist *flp;
11119 int i;
11120
11121 if (cu->language == language_ada)
11122 error (_("unexpected member functions in Ada type"));
11123
11124 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11125 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
11126 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
11127
11128 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
11129 {
11130 struct nextfnfield *nfp = flp->head;
11131 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
11132 int k;
11133
11134 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
11135 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
11136 fn_flp->fn_fields = (struct fn_field *)
11137 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
11138 for (k = flp->length; (k--, nfp); nfp = nfp->next)
11139 fn_flp->fn_fields[k] = nfp->fnfield;
11140 }
11141
11142 TYPE_NFN_FIELDS (type) = fip->nfnfields;
11143 }
11144
11145 /* Returns non-zero if NAME is the name of a vtable member in CU's
11146 language, zero otherwise. */
11147 static int
11148 is_vtable_name (const char *name, struct dwarf2_cu *cu)
11149 {
11150 static const char vptr[] = "_vptr";
11151 static const char vtable[] = "vtable";
11152
11153 /* Look for the C++ and Java forms of the vtable. */
11154 if ((cu->language == language_java
11155 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
11156 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
11157 && is_cplus_marker (name[sizeof (vptr) - 1])))
11158 return 1;
11159
11160 return 0;
11161 }
11162
11163 /* GCC outputs unnamed structures that are really pointers to member
11164 functions, with the ABI-specified layout. If TYPE describes
11165 such a structure, smash it into a member function type.
11166
11167 GCC shouldn't do this; it should just output pointer to member DIEs.
11168 This is GCC PR debug/28767. */
11169
11170 static void
11171 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
11172 {
11173 struct type *pfn_type, *domain_type, *new_type;
11174
11175 /* Check for a structure with no name and two children. */
11176 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
11177 return;
11178
11179 /* Check for __pfn and __delta members. */
11180 if (TYPE_FIELD_NAME (type, 0) == NULL
11181 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
11182 || TYPE_FIELD_NAME (type, 1) == NULL
11183 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
11184 return;
11185
11186 /* Find the type of the method. */
11187 pfn_type = TYPE_FIELD_TYPE (type, 0);
11188 if (pfn_type == NULL
11189 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
11190 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
11191 return;
11192
11193 /* Look for the "this" argument. */
11194 pfn_type = TYPE_TARGET_TYPE (pfn_type);
11195 if (TYPE_NFIELDS (pfn_type) == 0
11196 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
11197 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
11198 return;
11199
11200 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
11201 new_type = alloc_type (objfile);
11202 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
11203 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
11204 TYPE_VARARGS (pfn_type));
11205 smash_to_methodptr_type (type, new_type);
11206 }
11207
11208 /* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
11209 (icc). */
11210
11211 static int
11212 producer_is_icc (struct dwarf2_cu *cu)
11213 {
11214 if (!cu->checked_producer)
11215 check_producer (cu);
11216
11217 return cu->producer_is_icc;
11218 }
11219
11220 /* Called when we find the DIE that starts a structure or union scope
11221 (definition) to create a type for the structure or union. Fill in
11222 the type's name and general properties; the members will not be
11223 processed until process_structure_type.
11224
11225 NOTE: we need to call these functions regardless of whether or not the
11226 DIE has a DW_AT_name attribute, since it might be an anonymous
11227 structure or union. This gets the type entered into our set of
11228 user defined types.
11229
11230 However, if the structure is incomplete (an opaque struct/union)
11231 then suppress creating a symbol table entry for it since gdb only
11232 wants to find the one with the complete definition. Note that if
11233 it is complete, we just call new_symbol, which does it's own
11234 checking about whether the struct/union is anonymous or not (and
11235 suppresses creating a symbol table entry itself). */
11236
11237 static struct type *
11238 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
11239 {
11240 struct objfile *objfile = cu->objfile;
11241 struct type *type;
11242 struct attribute *attr;
11243 const char *name;
11244
11245 /* If the definition of this type lives in .debug_types, read that type.
11246 Don't follow DW_AT_specification though, that will take us back up
11247 the chain and we want to go down. */
11248 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
11249 if (attr)
11250 {
11251 struct dwarf2_cu *type_cu = cu;
11252 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11253
11254 /* We could just recurse on read_structure_type, but we need to call
11255 get_die_type to ensure only one type for this DIE is created.
11256 This is important, for example, because for c++ classes we need
11257 TYPE_NAME set which is only done by new_symbol. Blech. */
11258 type = read_type_die (type_die, type_cu);
11259
11260 /* TYPE_CU may not be the same as CU.
11261 Ensure TYPE is recorded in CU's type_hash table. */
11262 return set_die_type (die, type, cu);
11263 }
11264
11265 type = alloc_type (objfile);
11266 INIT_CPLUS_SPECIFIC (type);
11267
11268 name = dwarf2_name (die, cu);
11269 if (name != NULL)
11270 {
11271 if (cu->language == language_cplus
11272 || cu->language == language_java)
11273 {
11274 const char *full_name = dwarf2_full_name (name, die, cu);
11275
11276 /* dwarf2_full_name might have already finished building the DIE's
11277 type. If so, there is no need to continue. */
11278 if (get_die_type (die, cu) != NULL)
11279 return get_die_type (die, cu);
11280
11281 TYPE_TAG_NAME (type) = full_name;
11282 if (die->tag == DW_TAG_structure_type
11283 || die->tag == DW_TAG_class_type)
11284 TYPE_NAME (type) = TYPE_TAG_NAME (type);
11285 }
11286 else
11287 {
11288 /* The name is already allocated along with this objfile, so
11289 we don't need to duplicate it for the type. */
11290 TYPE_TAG_NAME (type) = name;
11291 if (die->tag == DW_TAG_class_type)
11292 TYPE_NAME (type) = TYPE_TAG_NAME (type);
11293 }
11294 }
11295
11296 if (die->tag == DW_TAG_structure_type)
11297 {
11298 TYPE_CODE (type) = TYPE_CODE_STRUCT;
11299 }
11300 else if (die->tag == DW_TAG_union_type)
11301 {
11302 TYPE_CODE (type) = TYPE_CODE_UNION;
11303 }
11304 else
11305 {
11306 TYPE_CODE (type) = TYPE_CODE_CLASS;
11307 }
11308
11309 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
11310 TYPE_DECLARED_CLASS (type) = 1;
11311
11312 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11313 if (attr)
11314 {
11315 TYPE_LENGTH (type) = DW_UNSND (attr);
11316 }
11317 else
11318 {
11319 TYPE_LENGTH (type) = 0;
11320 }
11321
11322 if (producer_is_icc (cu))
11323 {
11324 /* ICC does not output the required DW_AT_declaration
11325 on incomplete types, but gives them a size of zero. */
11326 }
11327 else
11328 TYPE_STUB_SUPPORTED (type) = 1;
11329
11330 if (die_is_declaration (die, cu))
11331 TYPE_STUB (type) = 1;
11332 else if (attr == NULL && die->child == NULL
11333 && producer_is_realview (cu->producer))
11334 /* RealView does not output the required DW_AT_declaration
11335 on incomplete types. */
11336 TYPE_STUB (type) = 1;
11337
11338 /* We need to add the type field to the die immediately so we don't
11339 infinitely recurse when dealing with pointers to the structure
11340 type within the structure itself. */
11341 set_die_type (die, type, cu);
11342
11343 /* set_die_type should be already done. */
11344 set_descriptive_type (type, die, cu);
11345
11346 return type;
11347 }
11348
11349 /* Finish creating a structure or union type, including filling in
11350 its members and creating a symbol for it. */
11351
11352 static void
11353 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
11354 {
11355 struct objfile *objfile = cu->objfile;
11356 struct die_info *child_die = die->child;
11357 struct type *type;
11358
11359 type = get_die_type (die, cu);
11360 if (type == NULL)
11361 type = read_structure_type (die, cu);
11362
11363 if (die->child != NULL && ! die_is_declaration (die, cu))
11364 {
11365 struct field_info fi;
11366 struct die_info *child_die;
11367 VEC (symbolp) *template_args = NULL;
11368 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
11369
11370 memset (&fi, 0, sizeof (struct field_info));
11371
11372 child_die = die->child;
11373
11374 while (child_die && child_die->tag)
11375 {
11376 if (child_die->tag == DW_TAG_member
11377 || child_die->tag == DW_TAG_variable)
11378 {
11379 /* NOTE: carlton/2002-11-05: A C++ static data member
11380 should be a DW_TAG_member that is a declaration, but
11381 all versions of G++ as of this writing (so through at
11382 least 3.2.1) incorrectly generate DW_TAG_variable
11383 tags for them instead. */
11384 dwarf2_add_field (&fi, child_die, cu);
11385 }
11386 else if (child_die->tag == DW_TAG_subprogram)
11387 {
11388 /* C++ member function. */
11389 dwarf2_add_member_fn (&fi, child_die, type, cu);
11390 }
11391 else if (child_die->tag == DW_TAG_inheritance)
11392 {
11393 /* C++ base class field. */
11394 dwarf2_add_field (&fi, child_die, cu);
11395 }
11396 else if (child_die->tag == DW_TAG_typedef)
11397 dwarf2_add_typedef (&fi, child_die, cu);
11398 else if (child_die->tag == DW_TAG_template_type_param
11399 || child_die->tag == DW_TAG_template_value_param)
11400 {
11401 struct symbol *arg = new_symbol (child_die, NULL, cu);
11402
11403 if (arg != NULL)
11404 VEC_safe_push (symbolp, template_args, arg);
11405 }
11406
11407 child_die = sibling_die (child_die);
11408 }
11409
11410 /* Attach template arguments to type. */
11411 if (! VEC_empty (symbolp, template_args))
11412 {
11413 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11414 TYPE_N_TEMPLATE_ARGUMENTS (type)
11415 = VEC_length (symbolp, template_args);
11416 TYPE_TEMPLATE_ARGUMENTS (type)
11417 = obstack_alloc (&objfile->objfile_obstack,
11418 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11419 * sizeof (struct symbol *)));
11420 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
11421 VEC_address (symbolp, template_args),
11422 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11423 * sizeof (struct symbol *)));
11424 VEC_free (symbolp, template_args);
11425 }
11426
11427 /* Attach fields and member functions to the type. */
11428 if (fi.nfields)
11429 dwarf2_attach_fields_to_type (&fi, type, cu);
11430 if (fi.nfnfields)
11431 {
11432 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
11433
11434 /* Get the type which refers to the base class (possibly this
11435 class itself) which contains the vtable pointer for the current
11436 class from the DW_AT_containing_type attribute. This use of
11437 DW_AT_containing_type is a GNU extension. */
11438
11439 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
11440 {
11441 struct type *t = die_containing_type (die, cu);
11442
11443 TYPE_VPTR_BASETYPE (type) = t;
11444 if (type == t)
11445 {
11446 int i;
11447
11448 /* Our own class provides vtbl ptr. */
11449 for (i = TYPE_NFIELDS (t) - 1;
11450 i >= TYPE_N_BASECLASSES (t);
11451 --i)
11452 {
11453 const char *fieldname = TYPE_FIELD_NAME (t, i);
11454
11455 if (is_vtable_name (fieldname, cu))
11456 {
11457 TYPE_VPTR_FIELDNO (type) = i;
11458 break;
11459 }
11460 }
11461
11462 /* Complain if virtual function table field not found. */
11463 if (i < TYPE_N_BASECLASSES (t))
11464 complaint (&symfile_complaints,
11465 _("virtual function table pointer "
11466 "not found when defining class '%s'"),
11467 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
11468 "");
11469 }
11470 else
11471 {
11472 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
11473 }
11474 }
11475 else if (cu->producer
11476 && strncmp (cu->producer,
11477 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
11478 {
11479 /* The IBM XLC compiler does not provide direct indication
11480 of the containing type, but the vtable pointer is
11481 always named __vfp. */
11482
11483 int i;
11484
11485 for (i = TYPE_NFIELDS (type) - 1;
11486 i >= TYPE_N_BASECLASSES (type);
11487 --i)
11488 {
11489 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
11490 {
11491 TYPE_VPTR_FIELDNO (type) = i;
11492 TYPE_VPTR_BASETYPE (type) = type;
11493 break;
11494 }
11495 }
11496 }
11497 }
11498
11499 /* Copy fi.typedef_field_list linked list elements content into the
11500 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
11501 if (fi.typedef_field_list)
11502 {
11503 int i = fi.typedef_field_list_count;
11504
11505 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11506 TYPE_TYPEDEF_FIELD_ARRAY (type)
11507 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
11508 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
11509
11510 /* Reverse the list order to keep the debug info elements order. */
11511 while (--i >= 0)
11512 {
11513 struct typedef_field *dest, *src;
11514
11515 dest = &TYPE_TYPEDEF_FIELD (type, i);
11516 src = &fi.typedef_field_list->field;
11517 fi.typedef_field_list = fi.typedef_field_list->next;
11518 *dest = *src;
11519 }
11520 }
11521
11522 do_cleanups (back_to);
11523
11524 if (HAVE_CPLUS_STRUCT (type))
11525 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
11526 }
11527
11528 quirk_gcc_member_function_pointer (type, objfile);
11529
11530 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
11531 snapshots) has been known to create a die giving a declaration
11532 for a class that has, as a child, a die giving a definition for a
11533 nested class. So we have to process our children even if the
11534 current die is a declaration. Normally, of course, a declaration
11535 won't have any children at all. */
11536
11537 while (child_die != NULL && child_die->tag)
11538 {
11539 if (child_die->tag == DW_TAG_member
11540 || child_die->tag == DW_TAG_variable
11541 || child_die->tag == DW_TAG_inheritance
11542 || child_die->tag == DW_TAG_template_value_param
11543 || child_die->tag == DW_TAG_template_type_param)
11544 {
11545 /* Do nothing. */
11546 }
11547 else
11548 process_die (child_die, cu);
11549
11550 child_die = sibling_die (child_die);
11551 }
11552
11553 /* Do not consider external references. According to the DWARF standard,
11554 these DIEs are identified by the fact that they have no byte_size
11555 attribute, and a declaration attribute. */
11556 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
11557 || !die_is_declaration (die, cu))
11558 new_symbol (die, type, cu);
11559 }
11560
11561 /* Given a DW_AT_enumeration_type die, set its type. We do not
11562 complete the type's fields yet, or create any symbols. */
11563
11564 static struct type *
11565 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
11566 {
11567 struct objfile *objfile = cu->objfile;
11568 struct type *type;
11569 struct attribute *attr;
11570 const char *name;
11571
11572 /* If the definition of this type lives in .debug_types, read that type.
11573 Don't follow DW_AT_specification though, that will take us back up
11574 the chain and we want to go down. */
11575 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
11576 if (attr)
11577 {
11578 struct dwarf2_cu *type_cu = cu;
11579 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11580
11581 type = read_type_die (type_die, type_cu);
11582
11583 /* TYPE_CU may not be the same as CU.
11584 Ensure TYPE is recorded in CU's type_hash table. */
11585 return set_die_type (die, type, cu);
11586 }
11587
11588 type = alloc_type (objfile);
11589
11590 TYPE_CODE (type) = TYPE_CODE_ENUM;
11591 name = dwarf2_full_name (NULL, die, cu);
11592 if (name != NULL)
11593 TYPE_TAG_NAME (type) = name;
11594
11595 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11596 if (attr)
11597 {
11598 TYPE_LENGTH (type) = DW_UNSND (attr);
11599 }
11600 else
11601 {
11602 TYPE_LENGTH (type) = 0;
11603 }
11604
11605 /* The enumeration DIE can be incomplete. In Ada, any type can be
11606 declared as private in the package spec, and then defined only
11607 inside the package body. Such types are known as Taft Amendment
11608 Types. When another package uses such a type, an incomplete DIE
11609 may be generated by the compiler. */
11610 if (die_is_declaration (die, cu))
11611 TYPE_STUB (type) = 1;
11612
11613 return set_die_type (die, type, cu);
11614 }
11615
11616 /* Given a pointer to a die which begins an enumeration, process all
11617 the dies that define the members of the enumeration, and create the
11618 symbol for the enumeration type.
11619
11620 NOTE: We reverse the order of the element list. */
11621
11622 static void
11623 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
11624 {
11625 struct type *this_type;
11626
11627 this_type = get_die_type (die, cu);
11628 if (this_type == NULL)
11629 this_type = read_enumeration_type (die, cu);
11630
11631 if (die->child != NULL)
11632 {
11633 struct die_info *child_die;
11634 struct symbol *sym;
11635 struct field *fields = NULL;
11636 int num_fields = 0;
11637 int unsigned_enum = 1;
11638 const char *name;
11639 int flag_enum = 1;
11640 ULONGEST mask = 0;
11641
11642 child_die = die->child;
11643 while (child_die && child_die->tag)
11644 {
11645 if (child_die->tag != DW_TAG_enumerator)
11646 {
11647 process_die (child_die, cu);
11648 }
11649 else
11650 {
11651 name = dwarf2_name (child_die, cu);
11652 if (name)
11653 {
11654 sym = new_symbol (child_die, this_type, cu);
11655 if (SYMBOL_VALUE (sym) < 0)
11656 {
11657 unsigned_enum = 0;
11658 flag_enum = 0;
11659 }
11660 else if ((mask & SYMBOL_VALUE (sym)) != 0)
11661 flag_enum = 0;
11662 else
11663 mask |= SYMBOL_VALUE (sym);
11664
11665 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
11666 {
11667 fields = (struct field *)
11668 xrealloc (fields,
11669 (num_fields + DW_FIELD_ALLOC_CHUNK)
11670 * sizeof (struct field));
11671 }
11672
11673 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
11674 FIELD_TYPE (fields[num_fields]) = NULL;
11675 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
11676 FIELD_BITSIZE (fields[num_fields]) = 0;
11677
11678 num_fields++;
11679 }
11680 }
11681
11682 child_die = sibling_die (child_die);
11683 }
11684
11685 if (num_fields)
11686 {
11687 TYPE_NFIELDS (this_type) = num_fields;
11688 TYPE_FIELDS (this_type) = (struct field *)
11689 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
11690 memcpy (TYPE_FIELDS (this_type), fields,
11691 sizeof (struct field) * num_fields);
11692 xfree (fields);
11693 }
11694 if (unsigned_enum)
11695 TYPE_UNSIGNED (this_type) = 1;
11696 if (flag_enum)
11697 TYPE_FLAG_ENUM (this_type) = 1;
11698 }
11699
11700 /* If we are reading an enum from a .debug_types unit, and the enum
11701 is a declaration, and the enum is not the signatured type in the
11702 unit, then we do not want to add a symbol for it. Adding a
11703 symbol would in some cases obscure the true definition of the
11704 enum, giving users an incomplete type when the definition is
11705 actually available. Note that we do not want to do this for all
11706 enums which are just declarations, because C++0x allows forward
11707 enum declarations. */
11708 if (cu->per_cu->is_debug_types
11709 && die_is_declaration (die, cu))
11710 {
11711 struct signatured_type *sig_type;
11712
11713 sig_type
11714 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
11715 cu->per_cu->info_or_types_section,
11716 cu->per_cu->offset);
11717 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
11718 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
11719 return;
11720 }
11721
11722 new_symbol (die, this_type, cu);
11723 }
11724
11725 /* Extract all information from a DW_TAG_array_type DIE and put it in
11726 the DIE's type field. For now, this only handles one dimensional
11727 arrays. */
11728
11729 static struct type *
11730 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
11731 {
11732 struct objfile *objfile = cu->objfile;
11733 struct die_info *child_die;
11734 struct type *type;
11735 struct type *element_type, *range_type, *index_type;
11736 struct type **range_types = NULL;
11737 struct attribute *attr;
11738 int ndim = 0;
11739 struct cleanup *back_to;
11740 const char *name;
11741
11742 element_type = die_type (die, cu);
11743
11744 /* The die_type call above may have already set the type for this DIE. */
11745 type = get_die_type (die, cu);
11746 if (type)
11747 return type;
11748
11749 /* Irix 6.2 native cc creates array types without children for
11750 arrays with unspecified length. */
11751 if (die->child == NULL)
11752 {
11753 index_type = objfile_type (objfile)->builtin_int;
11754 range_type = create_range_type (NULL, index_type, 0, -1);
11755 type = create_array_type (NULL, element_type, range_type);
11756 return set_die_type (die, type, cu);
11757 }
11758
11759 back_to = make_cleanup (null_cleanup, NULL);
11760 child_die = die->child;
11761 while (child_die && child_die->tag)
11762 {
11763 if (child_die->tag == DW_TAG_subrange_type)
11764 {
11765 struct type *child_type = read_type_die (child_die, cu);
11766
11767 if (child_type != NULL)
11768 {
11769 /* The range type was succesfully read. Save it for the
11770 array type creation. */
11771 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
11772 {
11773 range_types = (struct type **)
11774 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
11775 * sizeof (struct type *));
11776 if (ndim == 0)
11777 make_cleanup (free_current_contents, &range_types);
11778 }
11779 range_types[ndim++] = child_type;
11780 }
11781 }
11782 child_die = sibling_die (child_die);
11783 }
11784
11785 /* Dwarf2 dimensions are output from left to right, create the
11786 necessary array types in backwards order. */
11787
11788 type = element_type;
11789
11790 if (read_array_order (die, cu) == DW_ORD_col_major)
11791 {
11792 int i = 0;
11793
11794 while (i < ndim)
11795 type = create_array_type (NULL, type, range_types[i++]);
11796 }
11797 else
11798 {
11799 while (ndim-- > 0)
11800 type = create_array_type (NULL, type, range_types[ndim]);
11801 }
11802
11803 /* Understand Dwarf2 support for vector types (like they occur on
11804 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
11805 array type. This is not part of the Dwarf2/3 standard yet, but a
11806 custom vendor extension. The main difference between a regular
11807 array and the vector variant is that vectors are passed by value
11808 to functions. */
11809 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
11810 if (attr)
11811 make_vector_type (type);
11812
11813 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
11814 implementation may choose to implement triple vectors using this
11815 attribute. */
11816 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11817 if (attr)
11818 {
11819 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
11820 TYPE_LENGTH (type) = DW_UNSND (attr);
11821 else
11822 complaint (&symfile_complaints,
11823 _("DW_AT_byte_size for array type smaller "
11824 "than the total size of elements"));
11825 }
11826
11827 name = dwarf2_name (die, cu);
11828 if (name)
11829 TYPE_NAME (type) = name;
11830
11831 /* Install the type in the die. */
11832 set_die_type (die, type, cu);
11833
11834 /* set_die_type should be already done. */
11835 set_descriptive_type (type, die, cu);
11836
11837 do_cleanups (back_to);
11838
11839 return type;
11840 }
11841
11842 static enum dwarf_array_dim_ordering
11843 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
11844 {
11845 struct attribute *attr;
11846
11847 attr = dwarf2_attr (die, DW_AT_ordering, cu);
11848
11849 if (attr) return DW_SND (attr);
11850
11851 /* GNU F77 is a special case, as at 08/2004 array type info is the
11852 opposite order to the dwarf2 specification, but data is still
11853 laid out as per normal fortran.
11854
11855 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
11856 version checking. */
11857
11858 if (cu->language == language_fortran
11859 && cu->producer && strstr (cu->producer, "GNU F77"))
11860 {
11861 return DW_ORD_row_major;
11862 }
11863
11864 switch (cu->language_defn->la_array_ordering)
11865 {
11866 case array_column_major:
11867 return DW_ORD_col_major;
11868 case array_row_major:
11869 default:
11870 return DW_ORD_row_major;
11871 };
11872 }
11873
11874 /* Extract all information from a DW_TAG_set_type DIE and put it in
11875 the DIE's type field. */
11876
11877 static struct type *
11878 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
11879 {
11880 struct type *domain_type, *set_type;
11881 struct attribute *attr;
11882
11883 domain_type = die_type (die, cu);
11884
11885 /* The die_type call above may have already set the type for this DIE. */
11886 set_type = get_die_type (die, cu);
11887 if (set_type)
11888 return set_type;
11889
11890 set_type = create_set_type (NULL, domain_type);
11891
11892 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11893 if (attr)
11894 TYPE_LENGTH (set_type) = DW_UNSND (attr);
11895
11896 return set_die_type (die, set_type, cu);
11897 }
11898
11899 /* A helper for read_common_block that creates a locexpr baton.
11900 SYM is the symbol which we are marking as computed.
11901 COMMON_DIE is the DIE for the common block.
11902 COMMON_LOC is the location expression attribute for the common
11903 block itself.
11904 MEMBER_LOC is the location expression attribute for the particular
11905 member of the common block that we are processing.
11906 CU is the CU from which the above come. */
11907
11908 static void
11909 mark_common_block_symbol_computed (struct symbol *sym,
11910 struct die_info *common_die,
11911 struct attribute *common_loc,
11912 struct attribute *member_loc,
11913 struct dwarf2_cu *cu)
11914 {
11915 struct objfile *objfile = dwarf2_per_objfile->objfile;
11916 struct dwarf2_locexpr_baton *baton;
11917 gdb_byte *ptr;
11918 unsigned int cu_off;
11919 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
11920 LONGEST offset = 0;
11921
11922 gdb_assert (common_loc && member_loc);
11923 gdb_assert (attr_form_is_block (common_loc));
11924 gdb_assert (attr_form_is_block (member_loc)
11925 || attr_form_is_constant (member_loc));
11926
11927 baton = obstack_alloc (&objfile->objfile_obstack,
11928 sizeof (struct dwarf2_locexpr_baton));
11929 baton->per_cu = cu->per_cu;
11930 gdb_assert (baton->per_cu);
11931
11932 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
11933
11934 if (attr_form_is_constant (member_loc))
11935 {
11936 offset = dwarf2_get_attr_constant_value (member_loc, 0);
11937 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
11938 }
11939 else
11940 baton->size += DW_BLOCK (member_loc)->size;
11941
11942 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
11943 baton->data = ptr;
11944
11945 *ptr++ = DW_OP_call4;
11946 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
11947 store_unsigned_integer (ptr, 4, byte_order, cu_off);
11948 ptr += 4;
11949
11950 if (attr_form_is_constant (member_loc))
11951 {
11952 *ptr++ = DW_OP_addr;
11953 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
11954 ptr += cu->header.addr_size;
11955 }
11956 else
11957 {
11958 /* We have to copy the data here, because DW_OP_call4 will only
11959 use a DW_AT_location attribute. */
11960 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
11961 ptr += DW_BLOCK (member_loc)->size;
11962 }
11963
11964 *ptr++ = DW_OP_plus;
11965 gdb_assert (ptr - baton->data == baton->size);
11966
11967 SYMBOL_LOCATION_BATON (sym) = baton;
11968 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
11969 }
11970
11971 /* Create appropriate locally-scoped variables for all the
11972 DW_TAG_common_block entries. Also create a struct common_block
11973 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
11974 is used to sepate the common blocks name namespace from regular
11975 variable names. */
11976
11977 static void
11978 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
11979 {
11980 struct attribute *attr;
11981
11982 attr = dwarf2_attr (die, DW_AT_location, cu);
11983 if (attr)
11984 {
11985 /* Support the .debug_loc offsets. */
11986 if (attr_form_is_block (attr))
11987 {
11988 /* Ok. */
11989 }
11990 else if (attr_form_is_section_offset (attr))
11991 {
11992 dwarf2_complex_location_expr_complaint ();
11993 attr = NULL;
11994 }
11995 else
11996 {
11997 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
11998 "common block member");
11999 attr = NULL;
12000 }
12001 }
12002
12003 if (die->child != NULL)
12004 {
12005 struct objfile *objfile = cu->objfile;
12006 struct die_info *child_die;
12007 size_t n_entries = 0, size;
12008 struct common_block *common_block;
12009 struct symbol *sym;
12010
12011 for (child_die = die->child;
12012 child_die && child_die->tag;
12013 child_die = sibling_die (child_die))
12014 ++n_entries;
12015
12016 size = (sizeof (struct common_block)
12017 + (n_entries - 1) * sizeof (struct symbol *));
12018 common_block = obstack_alloc (&objfile->objfile_obstack, size);
12019 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
12020 common_block->n_entries = 0;
12021
12022 for (child_die = die->child;
12023 child_die && child_die->tag;
12024 child_die = sibling_die (child_die))
12025 {
12026 /* Create the symbol in the DW_TAG_common_block block in the current
12027 symbol scope. */
12028 sym = new_symbol (child_die, NULL, cu);
12029 if (sym != NULL)
12030 {
12031 struct attribute *member_loc;
12032
12033 common_block->contents[common_block->n_entries++] = sym;
12034
12035 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
12036 cu);
12037 if (member_loc)
12038 {
12039 /* GDB has handled this for a long time, but it is
12040 not specified by DWARF. It seems to have been
12041 emitted by gfortran at least as recently as:
12042 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
12043 complaint (&symfile_complaints,
12044 _("Variable in common block has "
12045 "DW_AT_data_member_location "
12046 "- DIE at 0x%x [in module %s]"),
12047 child_die->offset.sect_off, cu->objfile->name);
12048
12049 if (attr_form_is_section_offset (member_loc))
12050 dwarf2_complex_location_expr_complaint ();
12051 else if (attr_form_is_constant (member_loc)
12052 || attr_form_is_block (member_loc))
12053 {
12054 if (attr)
12055 mark_common_block_symbol_computed (sym, die, attr,
12056 member_loc, cu);
12057 }
12058 else
12059 dwarf2_complex_location_expr_complaint ();
12060 }
12061 }
12062 }
12063
12064 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
12065 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
12066 }
12067 }
12068
12069 /* Create a type for a C++ namespace. */
12070
12071 static struct type *
12072 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
12073 {
12074 struct objfile *objfile = cu->objfile;
12075 const char *previous_prefix, *name;
12076 int is_anonymous;
12077 struct type *type;
12078
12079 /* For extensions, reuse the type of the original namespace. */
12080 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
12081 {
12082 struct die_info *ext_die;
12083 struct dwarf2_cu *ext_cu = cu;
12084
12085 ext_die = dwarf2_extension (die, &ext_cu);
12086 type = read_type_die (ext_die, ext_cu);
12087
12088 /* EXT_CU may not be the same as CU.
12089 Ensure TYPE is recorded in CU's type_hash table. */
12090 return set_die_type (die, type, cu);
12091 }
12092
12093 name = namespace_name (die, &is_anonymous, cu);
12094
12095 /* Now build the name of the current namespace. */
12096
12097 previous_prefix = determine_prefix (die, cu);
12098 if (previous_prefix[0] != '\0')
12099 name = typename_concat (&objfile->objfile_obstack,
12100 previous_prefix, name, 0, cu);
12101
12102 /* Create the type. */
12103 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
12104 objfile);
12105 TYPE_NAME (type) = name;
12106 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12107
12108 return set_die_type (die, type, cu);
12109 }
12110
12111 /* Read a C++ namespace. */
12112
12113 static void
12114 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
12115 {
12116 struct objfile *objfile = cu->objfile;
12117 int is_anonymous;
12118
12119 /* Add a symbol associated to this if we haven't seen the namespace
12120 before. Also, add a using directive if it's an anonymous
12121 namespace. */
12122
12123 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
12124 {
12125 struct type *type;
12126
12127 type = read_type_die (die, cu);
12128 new_symbol (die, type, cu);
12129
12130 namespace_name (die, &is_anonymous, cu);
12131 if (is_anonymous)
12132 {
12133 const char *previous_prefix = determine_prefix (die, cu);
12134
12135 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
12136 NULL, NULL, 0, &objfile->objfile_obstack);
12137 }
12138 }
12139
12140 if (die->child != NULL)
12141 {
12142 struct die_info *child_die = die->child;
12143
12144 while (child_die && child_die->tag)
12145 {
12146 process_die (child_die, cu);
12147 child_die = sibling_die (child_die);
12148 }
12149 }
12150 }
12151
12152 /* Read a Fortran module as type. This DIE can be only a declaration used for
12153 imported module. Still we need that type as local Fortran "use ... only"
12154 declaration imports depend on the created type in determine_prefix. */
12155
12156 static struct type *
12157 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
12158 {
12159 struct objfile *objfile = cu->objfile;
12160 const char *module_name;
12161 struct type *type;
12162
12163 module_name = dwarf2_name (die, cu);
12164 if (!module_name)
12165 complaint (&symfile_complaints,
12166 _("DW_TAG_module has no name, offset 0x%x"),
12167 die->offset.sect_off);
12168 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
12169
12170 /* determine_prefix uses TYPE_TAG_NAME. */
12171 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12172
12173 return set_die_type (die, type, cu);
12174 }
12175
12176 /* Read a Fortran module. */
12177
12178 static void
12179 read_module (struct die_info *die, struct dwarf2_cu *cu)
12180 {
12181 struct die_info *child_die = die->child;
12182
12183 while (child_die && child_die->tag)
12184 {
12185 process_die (child_die, cu);
12186 child_die = sibling_die (child_die);
12187 }
12188 }
12189
12190 /* Return the name of the namespace represented by DIE. Set
12191 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
12192 namespace. */
12193
12194 static const char *
12195 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
12196 {
12197 struct die_info *current_die;
12198 const char *name = NULL;
12199
12200 /* Loop through the extensions until we find a name. */
12201
12202 for (current_die = die;
12203 current_die != NULL;
12204 current_die = dwarf2_extension (die, &cu))
12205 {
12206 name = dwarf2_name (current_die, cu);
12207 if (name != NULL)
12208 break;
12209 }
12210
12211 /* Is it an anonymous namespace? */
12212
12213 *is_anonymous = (name == NULL);
12214 if (*is_anonymous)
12215 name = CP_ANONYMOUS_NAMESPACE_STR;
12216
12217 return name;
12218 }
12219
12220 /* Extract all information from a DW_TAG_pointer_type DIE and add to
12221 the user defined type vector. */
12222
12223 static struct type *
12224 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
12225 {
12226 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
12227 struct comp_unit_head *cu_header = &cu->header;
12228 struct type *type;
12229 struct attribute *attr_byte_size;
12230 struct attribute *attr_address_class;
12231 int byte_size, addr_class;
12232 struct type *target_type;
12233
12234 target_type = die_type (die, cu);
12235
12236 /* The die_type call above may have already set the type for this DIE. */
12237 type = get_die_type (die, cu);
12238 if (type)
12239 return type;
12240
12241 type = lookup_pointer_type (target_type);
12242
12243 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
12244 if (attr_byte_size)
12245 byte_size = DW_UNSND (attr_byte_size);
12246 else
12247 byte_size = cu_header->addr_size;
12248
12249 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
12250 if (attr_address_class)
12251 addr_class = DW_UNSND (attr_address_class);
12252 else
12253 addr_class = DW_ADDR_none;
12254
12255 /* If the pointer size or address class is different than the
12256 default, create a type variant marked as such and set the
12257 length accordingly. */
12258 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
12259 {
12260 if (gdbarch_address_class_type_flags_p (gdbarch))
12261 {
12262 int type_flags;
12263
12264 type_flags = gdbarch_address_class_type_flags
12265 (gdbarch, byte_size, addr_class);
12266 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
12267 == 0);
12268 type = make_type_with_address_space (type, type_flags);
12269 }
12270 else if (TYPE_LENGTH (type) != byte_size)
12271 {
12272 complaint (&symfile_complaints,
12273 _("invalid pointer size %d"), byte_size);
12274 }
12275 else
12276 {
12277 /* Should we also complain about unhandled address classes? */
12278 }
12279 }
12280
12281 TYPE_LENGTH (type) = byte_size;
12282 return set_die_type (die, type, cu);
12283 }
12284
12285 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
12286 the user defined type vector. */
12287
12288 static struct type *
12289 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
12290 {
12291 struct type *type;
12292 struct type *to_type;
12293 struct type *domain;
12294
12295 to_type = die_type (die, cu);
12296 domain = die_containing_type (die, cu);
12297
12298 /* The calls above may have already set the type for this DIE. */
12299 type = get_die_type (die, cu);
12300 if (type)
12301 return type;
12302
12303 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
12304 type = lookup_methodptr_type (to_type);
12305 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
12306 {
12307 struct type *new_type = alloc_type (cu->objfile);
12308
12309 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
12310 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
12311 TYPE_VARARGS (to_type));
12312 type = lookup_methodptr_type (new_type);
12313 }
12314 else
12315 type = lookup_memberptr_type (to_type, domain);
12316
12317 return set_die_type (die, type, cu);
12318 }
12319
12320 /* Extract all information from a DW_TAG_reference_type DIE and add to
12321 the user defined type vector. */
12322
12323 static struct type *
12324 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
12325 {
12326 struct comp_unit_head *cu_header = &cu->header;
12327 struct type *type, *target_type;
12328 struct attribute *attr;
12329
12330 target_type = die_type (die, cu);
12331
12332 /* The die_type call above may have already set the type for this DIE. */
12333 type = get_die_type (die, cu);
12334 if (type)
12335 return type;
12336
12337 type = lookup_reference_type (target_type);
12338 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12339 if (attr)
12340 {
12341 TYPE_LENGTH (type) = DW_UNSND (attr);
12342 }
12343 else
12344 {
12345 TYPE_LENGTH (type) = cu_header->addr_size;
12346 }
12347 return set_die_type (die, type, cu);
12348 }
12349
12350 static struct type *
12351 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
12352 {
12353 struct type *base_type, *cv_type;
12354
12355 base_type = die_type (die, cu);
12356
12357 /* The die_type call above may have already set the type for this DIE. */
12358 cv_type = get_die_type (die, cu);
12359 if (cv_type)
12360 return cv_type;
12361
12362 /* In case the const qualifier is applied to an array type, the element type
12363 is so qualified, not the array type (section 6.7.3 of C99). */
12364 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
12365 {
12366 struct type *el_type, *inner_array;
12367
12368 base_type = copy_type (base_type);
12369 inner_array = base_type;
12370
12371 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
12372 {
12373 TYPE_TARGET_TYPE (inner_array) =
12374 copy_type (TYPE_TARGET_TYPE (inner_array));
12375 inner_array = TYPE_TARGET_TYPE (inner_array);
12376 }
12377
12378 el_type = TYPE_TARGET_TYPE (inner_array);
12379 TYPE_TARGET_TYPE (inner_array) =
12380 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
12381
12382 return set_die_type (die, base_type, cu);
12383 }
12384
12385 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
12386 return set_die_type (die, cv_type, cu);
12387 }
12388
12389 static struct type *
12390 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
12391 {
12392 struct type *base_type, *cv_type;
12393
12394 base_type = die_type (die, cu);
12395
12396 /* The die_type call above may have already set the type for this DIE. */
12397 cv_type = get_die_type (die, cu);
12398 if (cv_type)
12399 return cv_type;
12400
12401 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
12402 return set_die_type (die, cv_type, cu);
12403 }
12404
12405 /* Handle DW_TAG_restrict_type. */
12406
12407 static struct type *
12408 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
12409 {
12410 struct type *base_type, *cv_type;
12411
12412 base_type = die_type (die, cu);
12413
12414 /* The die_type call above may have already set the type for this DIE. */
12415 cv_type = get_die_type (die, cu);
12416 if (cv_type)
12417 return cv_type;
12418
12419 cv_type = make_restrict_type (base_type);
12420 return set_die_type (die, cv_type, cu);
12421 }
12422
12423 /* Extract all information from a DW_TAG_string_type DIE and add to
12424 the user defined type vector. It isn't really a user defined type,
12425 but it behaves like one, with other DIE's using an AT_user_def_type
12426 attribute to reference it. */
12427
12428 static struct type *
12429 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
12430 {
12431 struct objfile *objfile = cu->objfile;
12432 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12433 struct type *type, *range_type, *index_type, *char_type;
12434 struct attribute *attr;
12435 unsigned int length;
12436
12437 attr = dwarf2_attr (die, DW_AT_string_length, cu);
12438 if (attr)
12439 {
12440 length = DW_UNSND (attr);
12441 }
12442 else
12443 {
12444 /* Check for the DW_AT_byte_size attribute. */
12445 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12446 if (attr)
12447 {
12448 length = DW_UNSND (attr);
12449 }
12450 else
12451 {
12452 length = 1;
12453 }
12454 }
12455
12456 index_type = objfile_type (objfile)->builtin_int;
12457 range_type = create_range_type (NULL, index_type, 1, length);
12458 char_type = language_string_char_type (cu->language_defn, gdbarch);
12459 type = create_string_type (NULL, char_type, range_type);
12460
12461 return set_die_type (die, type, cu);
12462 }
12463
12464 /* Handle DIES due to C code like:
12465
12466 struct foo
12467 {
12468 int (*funcp)(int a, long l);
12469 int b;
12470 };
12471
12472 ('funcp' generates a DW_TAG_subroutine_type DIE). */
12473
12474 static struct type *
12475 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
12476 {
12477 struct objfile *objfile = cu->objfile;
12478 struct type *type; /* Type that this function returns. */
12479 struct type *ftype; /* Function that returns above type. */
12480 struct attribute *attr;
12481
12482 type = die_type (die, cu);
12483
12484 /* The die_type call above may have already set the type for this DIE. */
12485 ftype = get_die_type (die, cu);
12486 if (ftype)
12487 return ftype;
12488
12489 ftype = lookup_function_type (type);
12490
12491 /* All functions in C++, Pascal and Java have prototypes. */
12492 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
12493 if ((attr && (DW_UNSND (attr) != 0))
12494 || cu->language == language_cplus
12495 || cu->language == language_java
12496 || cu->language == language_pascal)
12497 TYPE_PROTOTYPED (ftype) = 1;
12498 else if (producer_is_realview (cu->producer))
12499 /* RealView does not emit DW_AT_prototyped. We can not
12500 distinguish prototyped and unprototyped functions; default to
12501 prototyped, since that is more common in modern code (and
12502 RealView warns about unprototyped functions). */
12503 TYPE_PROTOTYPED (ftype) = 1;
12504
12505 /* Store the calling convention in the type if it's available in
12506 the subroutine die. Otherwise set the calling convention to
12507 the default value DW_CC_normal. */
12508 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
12509 if (attr)
12510 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
12511 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
12512 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
12513 else
12514 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
12515
12516 /* We need to add the subroutine type to the die immediately so
12517 we don't infinitely recurse when dealing with parameters
12518 declared as the same subroutine type. */
12519 set_die_type (die, ftype, cu);
12520
12521 if (die->child != NULL)
12522 {
12523 struct type *void_type = objfile_type (objfile)->builtin_void;
12524 struct die_info *child_die;
12525 int nparams, iparams;
12526
12527 /* Count the number of parameters.
12528 FIXME: GDB currently ignores vararg functions, but knows about
12529 vararg member functions. */
12530 nparams = 0;
12531 child_die = die->child;
12532 while (child_die && child_die->tag)
12533 {
12534 if (child_die->tag == DW_TAG_formal_parameter)
12535 nparams++;
12536 else if (child_die->tag == DW_TAG_unspecified_parameters)
12537 TYPE_VARARGS (ftype) = 1;
12538 child_die = sibling_die (child_die);
12539 }
12540
12541 /* Allocate storage for parameters and fill them in. */
12542 TYPE_NFIELDS (ftype) = nparams;
12543 TYPE_FIELDS (ftype) = (struct field *)
12544 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
12545
12546 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
12547 even if we error out during the parameters reading below. */
12548 for (iparams = 0; iparams < nparams; iparams++)
12549 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
12550
12551 iparams = 0;
12552 child_die = die->child;
12553 while (child_die && child_die->tag)
12554 {
12555 if (child_die->tag == DW_TAG_formal_parameter)
12556 {
12557 struct type *arg_type;
12558
12559 /* DWARF version 2 has no clean way to discern C++
12560 static and non-static member functions. G++ helps
12561 GDB by marking the first parameter for non-static
12562 member functions (which is the this pointer) as
12563 artificial. We pass this information to
12564 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
12565
12566 DWARF version 3 added DW_AT_object_pointer, which GCC
12567 4.5 does not yet generate. */
12568 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
12569 if (attr)
12570 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
12571 else
12572 {
12573 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
12574
12575 /* GCC/43521: In java, the formal parameter
12576 "this" is sometimes not marked with DW_AT_artificial. */
12577 if (cu->language == language_java)
12578 {
12579 const char *name = dwarf2_name (child_die, cu);
12580
12581 if (name && !strcmp (name, "this"))
12582 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
12583 }
12584 }
12585 arg_type = die_type (child_die, cu);
12586
12587 /* RealView does not mark THIS as const, which the testsuite
12588 expects. GCC marks THIS as const in method definitions,
12589 but not in the class specifications (GCC PR 43053). */
12590 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
12591 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
12592 {
12593 int is_this = 0;
12594 struct dwarf2_cu *arg_cu = cu;
12595 const char *name = dwarf2_name (child_die, cu);
12596
12597 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
12598 if (attr)
12599 {
12600 /* If the compiler emits this, use it. */
12601 if (follow_die_ref (die, attr, &arg_cu) == child_die)
12602 is_this = 1;
12603 }
12604 else if (name && strcmp (name, "this") == 0)
12605 /* Function definitions will have the argument names. */
12606 is_this = 1;
12607 else if (name == NULL && iparams == 0)
12608 /* Declarations may not have the names, so like
12609 elsewhere in GDB, assume an artificial first
12610 argument is "this". */
12611 is_this = 1;
12612
12613 if (is_this)
12614 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
12615 arg_type, 0);
12616 }
12617
12618 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
12619 iparams++;
12620 }
12621 child_die = sibling_die (child_die);
12622 }
12623 }
12624
12625 return ftype;
12626 }
12627
12628 static struct type *
12629 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
12630 {
12631 struct objfile *objfile = cu->objfile;
12632 const char *name = NULL;
12633 struct type *this_type, *target_type;
12634
12635 name = dwarf2_full_name (NULL, die, cu);
12636 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
12637 TYPE_FLAG_TARGET_STUB, NULL, objfile);
12638 TYPE_NAME (this_type) = name;
12639 set_die_type (die, this_type, cu);
12640 target_type = die_type (die, cu);
12641 if (target_type != this_type)
12642 TYPE_TARGET_TYPE (this_type) = target_type;
12643 else
12644 {
12645 /* Self-referential typedefs are, it seems, not allowed by the DWARF
12646 spec and cause infinite loops in GDB. */
12647 complaint (&symfile_complaints,
12648 _("Self-referential DW_TAG_typedef "
12649 "- DIE at 0x%x [in module %s]"),
12650 die->offset.sect_off, objfile->name);
12651 TYPE_TARGET_TYPE (this_type) = NULL;
12652 }
12653 return this_type;
12654 }
12655
12656 /* Find a representation of a given base type and install
12657 it in the TYPE field of the die. */
12658
12659 static struct type *
12660 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
12661 {
12662 struct objfile *objfile = cu->objfile;
12663 struct type *type;
12664 struct attribute *attr;
12665 int encoding = 0, size = 0;
12666 const char *name;
12667 enum type_code code = TYPE_CODE_INT;
12668 int type_flags = 0;
12669 struct type *target_type = NULL;
12670
12671 attr = dwarf2_attr (die, DW_AT_encoding, cu);
12672 if (attr)
12673 {
12674 encoding = DW_UNSND (attr);
12675 }
12676 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12677 if (attr)
12678 {
12679 size = DW_UNSND (attr);
12680 }
12681 name = dwarf2_name (die, cu);
12682 if (!name)
12683 {
12684 complaint (&symfile_complaints,
12685 _("DW_AT_name missing from DW_TAG_base_type"));
12686 }
12687
12688 switch (encoding)
12689 {
12690 case DW_ATE_address:
12691 /* Turn DW_ATE_address into a void * pointer. */
12692 code = TYPE_CODE_PTR;
12693 type_flags |= TYPE_FLAG_UNSIGNED;
12694 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
12695 break;
12696 case DW_ATE_boolean:
12697 code = TYPE_CODE_BOOL;
12698 type_flags |= TYPE_FLAG_UNSIGNED;
12699 break;
12700 case DW_ATE_complex_float:
12701 code = TYPE_CODE_COMPLEX;
12702 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
12703 break;
12704 case DW_ATE_decimal_float:
12705 code = TYPE_CODE_DECFLOAT;
12706 break;
12707 case DW_ATE_float:
12708 code = TYPE_CODE_FLT;
12709 break;
12710 case DW_ATE_signed:
12711 break;
12712 case DW_ATE_unsigned:
12713 type_flags |= TYPE_FLAG_UNSIGNED;
12714 if (cu->language == language_fortran
12715 && name
12716 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
12717 code = TYPE_CODE_CHAR;
12718 break;
12719 case DW_ATE_signed_char:
12720 if (cu->language == language_ada || cu->language == language_m2
12721 || cu->language == language_pascal
12722 || cu->language == language_fortran)
12723 code = TYPE_CODE_CHAR;
12724 break;
12725 case DW_ATE_unsigned_char:
12726 if (cu->language == language_ada || cu->language == language_m2
12727 || cu->language == language_pascal
12728 || cu->language == language_fortran)
12729 code = TYPE_CODE_CHAR;
12730 type_flags |= TYPE_FLAG_UNSIGNED;
12731 break;
12732 case DW_ATE_UTF:
12733 /* We just treat this as an integer and then recognize the
12734 type by name elsewhere. */
12735 break;
12736
12737 default:
12738 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
12739 dwarf_type_encoding_name (encoding));
12740 break;
12741 }
12742
12743 type = init_type (code, size, type_flags, NULL, objfile);
12744 TYPE_NAME (type) = name;
12745 TYPE_TARGET_TYPE (type) = target_type;
12746
12747 if (name && strcmp (name, "char") == 0)
12748 TYPE_NOSIGN (type) = 1;
12749
12750 return set_die_type (die, type, cu);
12751 }
12752
12753 /* Read the given DW_AT_subrange DIE. */
12754
12755 static struct type *
12756 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
12757 {
12758 struct type *base_type, *orig_base_type;
12759 struct type *range_type;
12760 struct attribute *attr;
12761 LONGEST low, high;
12762 int low_default_is_valid;
12763 const char *name;
12764 LONGEST negative_mask;
12765
12766 orig_base_type = die_type (die, cu);
12767 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
12768 whereas the real type might be. So, we use ORIG_BASE_TYPE when
12769 creating the range type, but we use the result of check_typedef
12770 when examining properties of the type. */
12771 base_type = check_typedef (orig_base_type);
12772
12773 /* The die_type call above may have already set the type for this DIE. */
12774 range_type = get_die_type (die, cu);
12775 if (range_type)
12776 return range_type;
12777
12778 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
12779 omitting DW_AT_lower_bound. */
12780 switch (cu->language)
12781 {
12782 case language_c:
12783 case language_cplus:
12784 low = 0;
12785 low_default_is_valid = 1;
12786 break;
12787 case language_fortran:
12788 low = 1;
12789 low_default_is_valid = 1;
12790 break;
12791 case language_d:
12792 case language_java:
12793 case language_objc:
12794 low = 0;
12795 low_default_is_valid = (cu->header.version >= 4);
12796 break;
12797 case language_ada:
12798 case language_m2:
12799 case language_pascal:
12800 low = 1;
12801 low_default_is_valid = (cu->header.version >= 4);
12802 break;
12803 default:
12804 low = 0;
12805 low_default_is_valid = 0;
12806 break;
12807 }
12808
12809 /* FIXME: For variable sized arrays either of these could be
12810 a variable rather than a constant value. We'll allow it,
12811 but we don't know how to handle it. */
12812 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
12813 if (attr)
12814 low = dwarf2_get_attr_constant_value (attr, low);
12815 else if (!low_default_is_valid)
12816 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
12817 "- DIE at 0x%x [in module %s]"),
12818 die->offset.sect_off, cu->objfile->name);
12819
12820 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
12821 if (attr)
12822 {
12823 if (attr_form_is_block (attr) || is_ref_attr (attr))
12824 {
12825 /* GCC encodes arrays with unspecified or dynamic length
12826 with a DW_FORM_block1 attribute or a reference attribute.
12827 FIXME: GDB does not yet know how to handle dynamic
12828 arrays properly, treat them as arrays with unspecified
12829 length for now.
12830
12831 FIXME: jimb/2003-09-22: GDB does not really know
12832 how to handle arrays of unspecified length
12833 either; we just represent them as zero-length
12834 arrays. Choose an appropriate upper bound given
12835 the lower bound we've computed above. */
12836 high = low - 1;
12837 }
12838 else
12839 high = dwarf2_get_attr_constant_value (attr, 1);
12840 }
12841 else
12842 {
12843 attr = dwarf2_attr (die, DW_AT_count, cu);
12844 if (attr)
12845 {
12846 int count = dwarf2_get_attr_constant_value (attr, 1);
12847 high = low + count - 1;
12848 }
12849 else
12850 {
12851 /* Unspecified array length. */
12852 high = low - 1;
12853 }
12854 }
12855
12856 /* Dwarf-2 specifications explicitly allows to create subrange types
12857 without specifying a base type.
12858 In that case, the base type must be set to the type of
12859 the lower bound, upper bound or count, in that order, if any of these
12860 three attributes references an object that has a type.
12861 If no base type is found, the Dwarf-2 specifications say that
12862 a signed integer type of size equal to the size of an address should
12863 be used.
12864 For the following C code: `extern char gdb_int [];'
12865 GCC produces an empty range DIE.
12866 FIXME: muller/2010-05-28: Possible references to object for low bound,
12867 high bound or count are not yet handled by this code. */
12868 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
12869 {
12870 struct objfile *objfile = cu->objfile;
12871 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12872 int addr_size = gdbarch_addr_bit (gdbarch) /8;
12873 struct type *int_type = objfile_type (objfile)->builtin_int;
12874
12875 /* Test "int", "long int", and "long long int" objfile types,
12876 and select the first one having a size above or equal to the
12877 architecture address size. */
12878 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12879 base_type = int_type;
12880 else
12881 {
12882 int_type = objfile_type (objfile)->builtin_long;
12883 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12884 base_type = int_type;
12885 else
12886 {
12887 int_type = objfile_type (objfile)->builtin_long_long;
12888 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12889 base_type = int_type;
12890 }
12891 }
12892 }
12893
12894 negative_mask =
12895 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
12896 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
12897 low |= negative_mask;
12898 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
12899 high |= negative_mask;
12900
12901 range_type = create_range_type (NULL, orig_base_type, low, high);
12902
12903 /* Mark arrays with dynamic length at least as an array of unspecified
12904 length. GDB could check the boundary but before it gets implemented at
12905 least allow accessing the array elements. */
12906 if (attr && attr_form_is_block (attr))
12907 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12908
12909 /* Ada expects an empty array on no boundary attributes. */
12910 if (attr == NULL && cu->language != language_ada)
12911 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12912
12913 name = dwarf2_name (die, cu);
12914 if (name)
12915 TYPE_NAME (range_type) = name;
12916
12917 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12918 if (attr)
12919 TYPE_LENGTH (range_type) = DW_UNSND (attr);
12920
12921 set_die_type (die, range_type, cu);
12922
12923 /* set_die_type should be already done. */
12924 set_descriptive_type (range_type, die, cu);
12925
12926 return range_type;
12927 }
12928
12929 static struct type *
12930 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
12931 {
12932 struct type *type;
12933
12934 /* For now, we only support the C meaning of an unspecified type: void. */
12935
12936 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
12937 TYPE_NAME (type) = dwarf2_name (die, cu);
12938
12939 return set_die_type (die, type, cu);
12940 }
12941
12942 /* Read a single die and all its descendents. Set the die's sibling
12943 field to NULL; set other fields in the die correctly, and set all
12944 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
12945 location of the info_ptr after reading all of those dies. PARENT
12946 is the parent of the die in question. */
12947
12948 static struct die_info *
12949 read_die_and_children (const struct die_reader_specs *reader,
12950 gdb_byte *info_ptr,
12951 gdb_byte **new_info_ptr,
12952 struct die_info *parent)
12953 {
12954 struct die_info *die;
12955 gdb_byte *cur_ptr;
12956 int has_children;
12957
12958 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
12959 if (die == NULL)
12960 {
12961 *new_info_ptr = cur_ptr;
12962 return NULL;
12963 }
12964 store_in_ref_table (die, reader->cu);
12965
12966 if (has_children)
12967 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
12968 else
12969 {
12970 die->child = NULL;
12971 *new_info_ptr = cur_ptr;
12972 }
12973
12974 die->sibling = NULL;
12975 die->parent = parent;
12976 return die;
12977 }
12978
12979 /* Read a die, all of its descendents, and all of its siblings; set
12980 all of the fields of all of the dies correctly. Arguments are as
12981 in read_die_and_children. */
12982
12983 static struct die_info *
12984 read_die_and_siblings (const struct die_reader_specs *reader,
12985 gdb_byte *info_ptr,
12986 gdb_byte **new_info_ptr,
12987 struct die_info *parent)
12988 {
12989 struct die_info *first_die, *last_sibling;
12990 gdb_byte *cur_ptr;
12991
12992 cur_ptr = info_ptr;
12993 first_die = last_sibling = NULL;
12994
12995 while (1)
12996 {
12997 struct die_info *die
12998 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
12999
13000 if (die == NULL)
13001 {
13002 *new_info_ptr = cur_ptr;
13003 return first_die;
13004 }
13005
13006 if (!first_die)
13007 first_die = die;
13008 else
13009 last_sibling->sibling = die;
13010
13011 last_sibling = die;
13012 }
13013 }
13014
13015 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
13016 attributes.
13017 The caller is responsible for filling in the extra attributes
13018 and updating (*DIEP)->num_attrs.
13019 Set DIEP to point to a newly allocated die with its information,
13020 except for its child, sibling, and parent fields.
13021 Set HAS_CHILDREN to tell whether the die has children or not. */
13022
13023 static gdb_byte *
13024 read_full_die_1 (const struct die_reader_specs *reader,
13025 struct die_info **diep, gdb_byte *info_ptr,
13026 int *has_children, int num_extra_attrs)
13027 {
13028 unsigned int abbrev_number, bytes_read, i;
13029 sect_offset offset;
13030 struct abbrev_info *abbrev;
13031 struct die_info *die;
13032 struct dwarf2_cu *cu = reader->cu;
13033 bfd *abfd = reader->abfd;
13034
13035 offset.sect_off = info_ptr - reader->buffer;
13036 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13037 info_ptr += bytes_read;
13038 if (!abbrev_number)
13039 {
13040 *diep = NULL;
13041 *has_children = 0;
13042 return info_ptr;
13043 }
13044
13045 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
13046 if (!abbrev)
13047 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
13048 abbrev_number,
13049 bfd_get_filename (abfd));
13050
13051 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
13052 die->offset = offset;
13053 die->tag = abbrev->tag;
13054 die->abbrev = abbrev_number;
13055
13056 /* Make the result usable.
13057 The caller needs to update num_attrs after adding the extra
13058 attributes. */
13059 die->num_attrs = abbrev->num_attrs;
13060
13061 for (i = 0; i < abbrev->num_attrs; ++i)
13062 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
13063 info_ptr);
13064
13065 *diep = die;
13066 *has_children = abbrev->has_children;
13067 return info_ptr;
13068 }
13069
13070 /* Read a die and all its attributes.
13071 Set DIEP to point to a newly allocated die with its information,
13072 except for its child, sibling, and parent fields.
13073 Set HAS_CHILDREN to tell whether the die has children or not. */
13074
13075 static gdb_byte *
13076 read_full_die (const struct die_reader_specs *reader,
13077 struct die_info **diep, gdb_byte *info_ptr,
13078 int *has_children)
13079 {
13080 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
13081 }
13082 \f
13083 /* Abbreviation tables.
13084
13085 In DWARF version 2, the description of the debugging information is
13086 stored in a separate .debug_abbrev section. Before we read any
13087 dies from a section we read in all abbreviations and install them
13088 in a hash table. */
13089
13090 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
13091
13092 static struct abbrev_info *
13093 abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
13094 {
13095 struct abbrev_info *abbrev;
13096
13097 abbrev = (struct abbrev_info *)
13098 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
13099 memset (abbrev, 0, sizeof (struct abbrev_info));
13100 return abbrev;
13101 }
13102
13103 /* Add an abbreviation to the table. */
13104
13105 static void
13106 abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
13107 unsigned int abbrev_number,
13108 struct abbrev_info *abbrev)
13109 {
13110 unsigned int hash_number;
13111
13112 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13113 abbrev->next = abbrev_table->abbrevs[hash_number];
13114 abbrev_table->abbrevs[hash_number] = abbrev;
13115 }
13116
13117 /* Look up an abbrev in the table.
13118 Returns NULL if the abbrev is not found. */
13119
13120 static struct abbrev_info *
13121 abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
13122 unsigned int abbrev_number)
13123 {
13124 unsigned int hash_number;
13125 struct abbrev_info *abbrev;
13126
13127 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13128 abbrev = abbrev_table->abbrevs[hash_number];
13129
13130 while (abbrev)
13131 {
13132 if (abbrev->number == abbrev_number)
13133 return abbrev;
13134 abbrev = abbrev->next;
13135 }
13136 return NULL;
13137 }
13138
13139 /* Read in an abbrev table. */
13140
13141 static struct abbrev_table *
13142 abbrev_table_read_table (struct dwarf2_section_info *section,
13143 sect_offset offset)
13144 {
13145 struct objfile *objfile = dwarf2_per_objfile->objfile;
13146 bfd *abfd = section->asection->owner;
13147 struct abbrev_table *abbrev_table;
13148 gdb_byte *abbrev_ptr;
13149 struct abbrev_info *cur_abbrev;
13150 unsigned int abbrev_number, bytes_read, abbrev_name;
13151 unsigned int abbrev_form;
13152 struct attr_abbrev *cur_attrs;
13153 unsigned int allocated_attrs;
13154
13155 abbrev_table = XMALLOC (struct abbrev_table);
13156 abbrev_table->offset = offset;
13157 obstack_init (&abbrev_table->abbrev_obstack);
13158 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
13159 (ABBREV_HASH_SIZE
13160 * sizeof (struct abbrev_info *)));
13161 memset (abbrev_table->abbrevs, 0,
13162 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
13163
13164 dwarf2_read_section (objfile, section);
13165 abbrev_ptr = section->buffer + offset.sect_off;
13166 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13167 abbrev_ptr += bytes_read;
13168
13169 allocated_attrs = ATTR_ALLOC_CHUNK;
13170 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
13171
13172 /* Loop until we reach an abbrev number of 0. */
13173 while (abbrev_number)
13174 {
13175 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
13176
13177 /* read in abbrev header */
13178 cur_abbrev->number = abbrev_number;
13179 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13180 abbrev_ptr += bytes_read;
13181 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
13182 abbrev_ptr += 1;
13183
13184 /* now read in declarations */
13185 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13186 abbrev_ptr += bytes_read;
13187 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13188 abbrev_ptr += bytes_read;
13189 while (abbrev_name)
13190 {
13191 if (cur_abbrev->num_attrs == allocated_attrs)
13192 {
13193 allocated_attrs += ATTR_ALLOC_CHUNK;
13194 cur_attrs
13195 = xrealloc (cur_attrs, (allocated_attrs
13196 * sizeof (struct attr_abbrev)));
13197 }
13198
13199 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
13200 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
13201 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13202 abbrev_ptr += bytes_read;
13203 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13204 abbrev_ptr += bytes_read;
13205 }
13206
13207 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
13208 (cur_abbrev->num_attrs
13209 * sizeof (struct attr_abbrev)));
13210 memcpy (cur_abbrev->attrs, cur_attrs,
13211 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
13212
13213 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
13214
13215 /* Get next abbreviation.
13216 Under Irix6 the abbreviations for a compilation unit are not
13217 always properly terminated with an abbrev number of 0.
13218 Exit loop if we encounter an abbreviation which we have
13219 already read (which means we are about to read the abbreviations
13220 for the next compile unit) or if the end of the abbreviation
13221 table is reached. */
13222 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
13223 break;
13224 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13225 abbrev_ptr += bytes_read;
13226 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
13227 break;
13228 }
13229
13230 xfree (cur_attrs);
13231 return abbrev_table;
13232 }
13233
13234 /* Free the resources held by ABBREV_TABLE. */
13235
13236 static void
13237 abbrev_table_free (struct abbrev_table *abbrev_table)
13238 {
13239 obstack_free (&abbrev_table->abbrev_obstack, NULL);
13240 xfree (abbrev_table);
13241 }
13242
13243 /* Same as abbrev_table_free but as a cleanup.
13244 We pass in a pointer to the pointer to the table so that we can
13245 set the pointer to NULL when we're done. It also simplifies
13246 build_type_unit_groups. */
13247
13248 static void
13249 abbrev_table_free_cleanup (void *table_ptr)
13250 {
13251 struct abbrev_table **abbrev_table_ptr = table_ptr;
13252
13253 if (*abbrev_table_ptr != NULL)
13254 abbrev_table_free (*abbrev_table_ptr);
13255 *abbrev_table_ptr = NULL;
13256 }
13257
13258 /* Read the abbrev table for CU from ABBREV_SECTION. */
13259
13260 static void
13261 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
13262 struct dwarf2_section_info *abbrev_section)
13263 {
13264 cu->abbrev_table =
13265 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
13266 }
13267
13268 /* Release the memory used by the abbrev table for a compilation unit. */
13269
13270 static void
13271 dwarf2_free_abbrev_table (void *ptr_to_cu)
13272 {
13273 struct dwarf2_cu *cu = ptr_to_cu;
13274
13275 abbrev_table_free (cu->abbrev_table);
13276 /* Set this to NULL so that we SEGV if we try to read it later,
13277 and also because free_comp_unit verifies this is NULL. */
13278 cu->abbrev_table = NULL;
13279 }
13280 \f
13281 /* Returns nonzero if TAG represents a type that we might generate a partial
13282 symbol for. */
13283
13284 static int
13285 is_type_tag_for_partial (int tag)
13286 {
13287 switch (tag)
13288 {
13289 #if 0
13290 /* Some types that would be reasonable to generate partial symbols for,
13291 that we don't at present. */
13292 case DW_TAG_array_type:
13293 case DW_TAG_file_type:
13294 case DW_TAG_ptr_to_member_type:
13295 case DW_TAG_set_type:
13296 case DW_TAG_string_type:
13297 case DW_TAG_subroutine_type:
13298 #endif
13299 case DW_TAG_base_type:
13300 case DW_TAG_class_type:
13301 case DW_TAG_interface_type:
13302 case DW_TAG_enumeration_type:
13303 case DW_TAG_structure_type:
13304 case DW_TAG_subrange_type:
13305 case DW_TAG_typedef:
13306 case DW_TAG_union_type:
13307 return 1;
13308 default:
13309 return 0;
13310 }
13311 }
13312
13313 /* Load all DIEs that are interesting for partial symbols into memory. */
13314
13315 static struct partial_die_info *
13316 load_partial_dies (const struct die_reader_specs *reader,
13317 gdb_byte *info_ptr, int building_psymtab)
13318 {
13319 struct dwarf2_cu *cu = reader->cu;
13320 struct objfile *objfile = cu->objfile;
13321 struct partial_die_info *part_die;
13322 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
13323 struct abbrev_info *abbrev;
13324 unsigned int bytes_read;
13325 unsigned int load_all = 0;
13326 int nesting_level = 1;
13327
13328 parent_die = NULL;
13329 last_die = NULL;
13330
13331 gdb_assert (cu->per_cu != NULL);
13332 if (cu->per_cu->load_all_dies)
13333 load_all = 1;
13334
13335 cu->partial_dies
13336 = htab_create_alloc_ex (cu->header.length / 12,
13337 partial_die_hash,
13338 partial_die_eq,
13339 NULL,
13340 &cu->comp_unit_obstack,
13341 hashtab_obstack_allocate,
13342 dummy_obstack_deallocate);
13343
13344 part_die = obstack_alloc (&cu->comp_unit_obstack,
13345 sizeof (struct partial_die_info));
13346
13347 while (1)
13348 {
13349 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
13350
13351 /* A NULL abbrev means the end of a series of children. */
13352 if (abbrev == NULL)
13353 {
13354 if (--nesting_level == 0)
13355 {
13356 /* PART_DIE was probably the last thing allocated on the
13357 comp_unit_obstack, so we could call obstack_free
13358 here. We don't do that because the waste is small,
13359 and will be cleaned up when we're done with this
13360 compilation unit. This way, we're also more robust
13361 against other users of the comp_unit_obstack. */
13362 return first_die;
13363 }
13364 info_ptr += bytes_read;
13365 last_die = parent_die;
13366 parent_die = parent_die->die_parent;
13367 continue;
13368 }
13369
13370 /* Check for template arguments. We never save these; if
13371 they're seen, we just mark the parent, and go on our way. */
13372 if (parent_die != NULL
13373 && cu->language == language_cplus
13374 && (abbrev->tag == DW_TAG_template_type_param
13375 || abbrev->tag == DW_TAG_template_value_param))
13376 {
13377 parent_die->has_template_arguments = 1;
13378
13379 if (!load_all)
13380 {
13381 /* We don't need a partial DIE for the template argument. */
13382 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
13383 continue;
13384 }
13385 }
13386
13387 /* We only recurse into c++ subprograms looking for template arguments.
13388 Skip their other children. */
13389 if (!load_all
13390 && cu->language == language_cplus
13391 && parent_die != NULL
13392 && parent_die->tag == DW_TAG_subprogram)
13393 {
13394 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
13395 continue;
13396 }
13397
13398 /* Check whether this DIE is interesting enough to save. Normally
13399 we would not be interested in members here, but there may be
13400 later variables referencing them via DW_AT_specification (for
13401 static members). */
13402 if (!load_all
13403 && !is_type_tag_for_partial (abbrev->tag)
13404 && abbrev->tag != DW_TAG_constant
13405 && abbrev->tag != DW_TAG_enumerator
13406 && abbrev->tag != DW_TAG_subprogram
13407 && abbrev->tag != DW_TAG_lexical_block
13408 && abbrev->tag != DW_TAG_variable
13409 && abbrev->tag != DW_TAG_namespace
13410 && abbrev->tag != DW_TAG_module
13411 && abbrev->tag != DW_TAG_member
13412 && abbrev->tag != DW_TAG_imported_unit)
13413 {
13414 /* Otherwise we skip to the next sibling, if any. */
13415 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
13416 continue;
13417 }
13418
13419 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
13420 info_ptr);
13421
13422 /* This two-pass algorithm for processing partial symbols has a
13423 high cost in cache pressure. Thus, handle some simple cases
13424 here which cover the majority of C partial symbols. DIEs
13425 which neither have specification tags in them, nor could have
13426 specification tags elsewhere pointing at them, can simply be
13427 processed and discarded.
13428
13429 This segment is also optional; scan_partial_symbols and
13430 add_partial_symbol will handle these DIEs if we chain
13431 them in normally. When compilers which do not emit large
13432 quantities of duplicate debug information are more common,
13433 this code can probably be removed. */
13434
13435 /* Any complete simple types at the top level (pretty much all
13436 of them, for a language without namespaces), can be processed
13437 directly. */
13438 if (parent_die == NULL
13439 && part_die->has_specification == 0
13440 && part_die->is_declaration == 0
13441 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
13442 || part_die->tag == DW_TAG_base_type
13443 || part_die->tag == DW_TAG_subrange_type))
13444 {
13445 if (building_psymtab && part_die->name != NULL)
13446 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
13447 VAR_DOMAIN, LOC_TYPEDEF,
13448 &objfile->static_psymbols,
13449 0, (CORE_ADDR) 0, cu->language, objfile);
13450 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
13451 continue;
13452 }
13453
13454 /* The exception for DW_TAG_typedef with has_children above is
13455 a workaround of GCC PR debug/47510. In the case of this complaint
13456 type_name_no_tag_or_error will error on such types later.
13457
13458 GDB skipped children of DW_TAG_typedef by the shortcut above and then
13459 it could not find the child DIEs referenced later, this is checked
13460 above. In correct DWARF DW_TAG_typedef should have no children. */
13461
13462 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
13463 complaint (&symfile_complaints,
13464 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
13465 "- DIE at 0x%x [in module %s]"),
13466 part_die->offset.sect_off, objfile->name);
13467
13468 /* If we're at the second level, and we're an enumerator, and
13469 our parent has no specification (meaning possibly lives in a
13470 namespace elsewhere), then we can add the partial symbol now
13471 instead of queueing it. */
13472 if (part_die->tag == DW_TAG_enumerator
13473 && parent_die != NULL
13474 && parent_die->die_parent == NULL
13475 && parent_die->tag == DW_TAG_enumeration_type
13476 && parent_die->has_specification == 0)
13477 {
13478 if (part_die->name == NULL)
13479 complaint (&symfile_complaints,
13480 _("malformed enumerator DIE ignored"));
13481 else if (building_psymtab)
13482 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
13483 VAR_DOMAIN, LOC_CONST,
13484 (cu->language == language_cplus
13485 || cu->language == language_java)
13486 ? &objfile->global_psymbols
13487 : &objfile->static_psymbols,
13488 0, (CORE_ADDR) 0, cu->language, objfile);
13489
13490 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
13491 continue;
13492 }
13493
13494 /* We'll save this DIE so link it in. */
13495 part_die->die_parent = parent_die;
13496 part_die->die_sibling = NULL;
13497 part_die->die_child = NULL;
13498
13499 if (last_die && last_die == parent_die)
13500 last_die->die_child = part_die;
13501 else if (last_die)
13502 last_die->die_sibling = part_die;
13503
13504 last_die = part_die;
13505
13506 if (first_die == NULL)
13507 first_die = part_die;
13508
13509 /* Maybe add the DIE to the hash table. Not all DIEs that we
13510 find interesting need to be in the hash table, because we
13511 also have the parent/sibling/child chains; only those that we
13512 might refer to by offset later during partial symbol reading.
13513
13514 For now this means things that might have be the target of a
13515 DW_AT_specification, DW_AT_abstract_origin, or
13516 DW_AT_extension. DW_AT_extension will refer only to
13517 namespaces; DW_AT_abstract_origin refers to functions (and
13518 many things under the function DIE, but we do not recurse
13519 into function DIEs during partial symbol reading) and
13520 possibly variables as well; DW_AT_specification refers to
13521 declarations. Declarations ought to have the DW_AT_declaration
13522 flag. It happens that GCC forgets to put it in sometimes, but
13523 only for functions, not for types.
13524
13525 Adding more things than necessary to the hash table is harmless
13526 except for the performance cost. Adding too few will result in
13527 wasted time in find_partial_die, when we reread the compilation
13528 unit with load_all_dies set. */
13529
13530 if (load_all
13531 || abbrev->tag == DW_TAG_constant
13532 || abbrev->tag == DW_TAG_subprogram
13533 || abbrev->tag == DW_TAG_variable
13534 || abbrev->tag == DW_TAG_namespace
13535 || part_die->is_declaration)
13536 {
13537 void **slot;
13538
13539 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
13540 part_die->offset.sect_off, INSERT);
13541 *slot = part_die;
13542 }
13543
13544 part_die = obstack_alloc (&cu->comp_unit_obstack,
13545 sizeof (struct partial_die_info));
13546
13547 /* For some DIEs we want to follow their children (if any). For C
13548 we have no reason to follow the children of structures; for other
13549 languages we have to, so that we can get at method physnames
13550 to infer fully qualified class names, for DW_AT_specification,
13551 and for C++ template arguments. For C++, we also look one level
13552 inside functions to find template arguments (if the name of the
13553 function does not already contain the template arguments).
13554
13555 For Ada, we need to scan the children of subprograms and lexical
13556 blocks as well because Ada allows the definition of nested
13557 entities that could be interesting for the debugger, such as
13558 nested subprograms for instance. */
13559 if (last_die->has_children
13560 && (load_all
13561 || last_die->tag == DW_TAG_namespace
13562 || last_die->tag == DW_TAG_module
13563 || last_die->tag == DW_TAG_enumeration_type
13564 || (cu->language == language_cplus
13565 && last_die->tag == DW_TAG_subprogram
13566 && (last_die->name == NULL
13567 || strchr (last_die->name, '<') == NULL))
13568 || (cu->language != language_c
13569 && (last_die->tag == DW_TAG_class_type
13570 || last_die->tag == DW_TAG_interface_type
13571 || last_die->tag == DW_TAG_structure_type
13572 || last_die->tag == DW_TAG_union_type))
13573 || (cu->language == language_ada
13574 && (last_die->tag == DW_TAG_subprogram
13575 || last_die->tag == DW_TAG_lexical_block))))
13576 {
13577 nesting_level++;
13578 parent_die = last_die;
13579 continue;
13580 }
13581
13582 /* Otherwise we skip to the next sibling, if any. */
13583 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
13584
13585 /* Back to the top, do it again. */
13586 }
13587 }
13588
13589 /* Read a minimal amount of information into the minimal die structure. */
13590
13591 static gdb_byte *
13592 read_partial_die (const struct die_reader_specs *reader,
13593 struct partial_die_info *part_die,
13594 struct abbrev_info *abbrev, unsigned int abbrev_len,
13595 gdb_byte *info_ptr)
13596 {
13597 struct dwarf2_cu *cu = reader->cu;
13598 struct objfile *objfile = cu->objfile;
13599 gdb_byte *buffer = reader->buffer;
13600 unsigned int i;
13601 struct attribute attr;
13602 int has_low_pc_attr = 0;
13603 int has_high_pc_attr = 0;
13604 int high_pc_relative = 0;
13605
13606 memset (part_die, 0, sizeof (struct partial_die_info));
13607
13608 part_die->offset.sect_off = info_ptr - buffer;
13609
13610 info_ptr += abbrev_len;
13611
13612 if (abbrev == NULL)
13613 return info_ptr;
13614
13615 part_die->tag = abbrev->tag;
13616 part_die->has_children = abbrev->has_children;
13617
13618 for (i = 0; i < abbrev->num_attrs; ++i)
13619 {
13620 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
13621
13622 /* Store the data if it is of an attribute we want to keep in a
13623 partial symbol table. */
13624 switch (attr.name)
13625 {
13626 case DW_AT_name:
13627 switch (part_die->tag)
13628 {
13629 case DW_TAG_compile_unit:
13630 case DW_TAG_partial_unit:
13631 case DW_TAG_type_unit:
13632 /* Compilation units have a DW_AT_name that is a filename, not
13633 a source language identifier. */
13634 case DW_TAG_enumeration_type:
13635 case DW_TAG_enumerator:
13636 /* These tags always have simple identifiers already; no need
13637 to canonicalize them. */
13638 part_die->name = DW_STRING (&attr);
13639 break;
13640 default:
13641 part_die->name
13642 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
13643 &objfile->objfile_obstack);
13644 break;
13645 }
13646 break;
13647 case DW_AT_linkage_name:
13648 case DW_AT_MIPS_linkage_name:
13649 /* Note that both forms of linkage name might appear. We
13650 assume they will be the same, and we only store the last
13651 one we see. */
13652 if (cu->language == language_ada)
13653 part_die->name = DW_STRING (&attr);
13654 part_die->linkage_name = DW_STRING (&attr);
13655 break;
13656 case DW_AT_low_pc:
13657 has_low_pc_attr = 1;
13658 part_die->lowpc = DW_ADDR (&attr);
13659 break;
13660 case DW_AT_high_pc:
13661 has_high_pc_attr = 1;
13662 if (attr.form == DW_FORM_addr
13663 || attr.form == DW_FORM_GNU_addr_index)
13664 part_die->highpc = DW_ADDR (&attr);
13665 else
13666 {
13667 high_pc_relative = 1;
13668 part_die->highpc = DW_UNSND (&attr);
13669 }
13670 break;
13671 case DW_AT_location:
13672 /* Support the .debug_loc offsets. */
13673 if (attr_form_is_block (&attr))
13674 {
13675 part_die->d.locdesc = DW_BLOCK (&attr);
13676 }
13677 else if (attr_form_is_section_offset (&attr))
13678 {
13679 dwarf2_complex_location_expr_complaint ();
13680 }
13681 else
13682 {
13683 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
13684 "partial symbol information");
13685 }
13686 break;
13687 case DW_AT_external:
13688 part_die->is_external = DW_UNSND (&attr);
13689 break;
13690 case DW_AT_declaration:
13691 part_die->is_declaration = DW_UNSND (&attr);
13692 break;
13693 case DW_AT_type:
13694 part_die->has_type = 1;
13695 break;
13696 case DW_AT_abstract_origin:
13697 case DW_AT_specification:
13698 case DW_AT_extension:
13699 part_die->has_specification = 1;
13700 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
13701 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13702 || cu->per_cu->is_dwz);
13703 break;
13704 case DW_AT_sibling:
13705 /* Ignore absolute siblings, they might point outside of
13706 the current compile unit. */
13707 if (attr.form == DW_FORM_ref_addr)
13708 complaint (&symfile_complaints,
13709 _("ignoring absolute DW_AT_sibling"));
13710 else
13711 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
13712 break;
13713 case DW_AT_byte_size:
13714 part_die->has_byte_size = 1;
13715 break;
13716 case DW_AT_calling_convention:
13717 /* DWARF doesn't provide a way to identify a program's source-level
13718 entry point. DW_AT_calling_convention attributes are only meant
13719 to describe functions' calling conventions.
13720
13721 However, because it's a necessary piece of information in
13722 Fortran, and because DW_CC_program is the only piece of debugging
13723 information whose definition refers to a 'main program' at all,
13724 several compilers have begun marking Fortran main programs with
13725 DW_CC_program --- even when those functions use the standard
13726 calling conventions.
13727
13728 So until DWARF specifies a way to provide this information and
13729 compilers pick up the new representation, we'll support this
13730 practice. */
13731 if (DW_UNSND (&attr) == DW_CC_program
13732 && cu->language == language_fortran)
13733 {
13734 set_main_name (part_die->name);
13735
13736 /* As this DIE has a static linkage the name would be difficult
13737 to look up later. */
13738 language_of_main = language_fortran;
13739 }
13740 break;
13741 case DW_AT_inline:
13742 if (DW_UNSND (&attr) == DW_INL_inlined
13743 || DW_UNSND (&attr) == DW_INL_declared_inlined)
13744 part_die->may_be_inlined = 1;
13745 break;
13746
13747 case DW_AT_import:
13748 if (part_die->tag == DW_TAG_imported_unit)
13749 {
13750 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
13751 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13752 || cu->per_cu->is_dwz);
13753 }
13754 break;
13755
13756 default:
13757 break;
13758 }
13759 }
13760
13761 if (high_pc_relative)
13762 part_die->highpc += part_die->lowpc;
13763
13764 if (has_low_pc_attr && has_high_pc_attr)
13765 {
13766 /* When using the GNU linker, .gnu.linkonce. sections are used to
13767 eliminate duplicate copies of functions and vtables and such.
13768 The linker will arbitrarily choose one and discard the others.
13769 The AT_*_pc values for such functions refer to local labels in
13770 these sections. If the section from that file was discarded, the
13771 labels are not in the output, so the relocs get a value of 0.
13772 If this is a discarded function, mark the pc bounds as invalid,
13773 so that GDB will ignore it. */
13774 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
13775 {
13776 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13777
13778 complaint (&symfile_complaints,
13779 _("DW_AT_low_pc %s is zero "
13780 "for DIE at 0x%x [in module %s]"),
13781 paddress (gdbarch, part_die->lowpc),
13782 part_die->offset.sect_off, objfile->name);
13783 }
13784 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
13785 else if (part_die->lowpc >= part_die->highpc)
13786 {
13787 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13788
13789 complaint (&symfile_complaints,
13790 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
13791 "for DIE at 0x%x [in module %s]"),
13792 paddress (gdbarch, part_die->lowpc),
13793 paddress (gdbarch, part_die->highpc),
13794 part_die->offset.sect_off, objfile->name);
13795 }
13796 else
13797 part_die->has_pc_info = 1;
13798 }
13799
13800 return info_ptr;
13801 }
13802
13803 /* Find a cached partial DIE at OFFSET in CU. */
13804
13805 static struct partial_die_info *
13806 find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
13807 {
13808 struct partial_die_info *lookup_die = NULL;
13809 struct partial_die_info part_die;
13810
13811 part_die.offset = offset;
13812 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
13813 offset.sect_off);
13814
13815 return lookup_die;
13816 }
13817
13818 /* Find a partial DIE at OFFSET, which may or may not be in CU,
13819 except in the case of .debug_types DIEs which do not reference
13820 outside their CU (they do however referencing other types via
13821 DW_FORM_ref_sig8). */
13822
13823 static struct partial_die_info *
13824 find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
13825 {
13826 struct objfile *objfile = cu->objfile;
13827 struct dwarf2_per_cu_data *per_cu = NULL;
13828 struct partial_die_info *pd = NULL;
13829
13830 if (offset_in_dwz == cu->per_cu->is_dwz
13831 && offset_in_cu_p (&cu->header, offset))
13832 {
13833 pd = find_partial_die_in_comp_unit (offset, cu);
13834 if (pd != NULL)
13835 return pd;
13836 /* We missed recording what we needed.
13837 Load all dies and try again. */
13838 per_cu = cu->per_cu;
13839 }
13840 else
13841 {
13842 /* TUs don't reference other CUs/TUs (except via type signatures). */
13843 if (cu->per_cu->is_debug_types)
13844 {
13845 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
13846 " external reference to offset 0x%lx [in module %s].\n"),
13847 (long) cu->header.offset.sect_off, (long) offset.sect_off,
13848 bfd_get_filename (objfile->obfd));
13849 }
13850 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
13851 objfile);
13852
13853 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
13854 load_partial_comp_unit (per_cu);
13855
13856 per_cu->cu->last_used = 0;
13857 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13858 }
13859
13860 /* If we didn't find it, and not all dies have been loaded,
13861 load them all and try again. */
13862
13863 if (pd == NULL && per_cu->load_all_dies == 0)
13864 {
13865 per_cu->load_all_dies = 1;
13866
13867 /* This is nasty. When we reread the DIEs, somewhere up the call chain
13868 THIS_CU->cu may already be in use. So we can't just free it and
13869 replace its DIEs with the ones we read in. Instead, we leave those
13870 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
13871 and clobber THIS_CU->cu->partial_dies with the hash table for the new
13872 set. */
13873 load_partial_comp_unit (per_cu);
13874
13875 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13876 }
13877
13878 if (pd == NULL)
13879 internal_error (__FILE__, __LINE__,
13880 _("could not find partial DIE 0x%x "
13881 "in cache [from module %s]\n"),
13882 offset.sect_off, bfd_get_filename (objfile->obfd));
13883 return pd;
13884 }
13885
13886 /* See if we can figure out if the class lives in a namespace. We do
13887 this by looking for a member function; its demangled name will
13888 contain namespace info, if there is any. */
13889
13890 static void
13891 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
13892 struct dwarf2_cu *cu)
13893 {
13894 /* NOTE: carlton/2003-10-07: Getting the info this way changes
13895 what template types look like, because the demangler
13896 frequently doesn't give the same name as the debug info. We
13897 could fix this by only using the demangled name to get the
13898 prefix (but see comment in read_structure_type). */
13899
13900 struct partial_die_info *real_pdi;
13901 struct partial_die_info *child_pdi;
13902
13903 /* If this DIE (this DIE's specification, if any) has a parent, then
13904 we should not do this. We'll prepend the parent's fully qualified
13905 name when we create the partial symbol. */
13906
13907 real_pdi = struct_pdi;
13908 while (real_pdi->has_specification)
13909 real_pdi = find_partial_die (real_pdi->spec_offset,
13910 real_pdi->spec_is_dwz, cu);
13911
13912 if (real_pdi->die_parent != NULL)
13913 return;
13914
13915 for (child_pdi = struct_pdi->die_child;
13916 child_pdi != NULL;
13917 child_pdi = child_pdi->die_sibling)
13918 {
13919 if (child_pdi->tag == DW_TAG_subprogram
13920 && child_pdi->linkage_name != NULL)
13921 {
13922 char *actual_class_name
13923 = language_class_name_from_physname (cu->language_defn,
13924 child_pdi->linkage_name);
13925 if (actual_class_name != NULL)
13926 {
13927 struct_pdi->name
13928 = obstack_copy0 (&cu->objfile->objfile_obstack,
13929 actual_class_name,
13930 strlen (actual_class_name));
13931 xfree (actual_class_name);
13932 }
13933 break;
13934 }
13935 }
13936 }
13937
13938 /* Adjust PART_DIE before generating a symbol for it. This function
13939 may set the is_external flag or change the DIE's name. */
13940
13941 static void
13942 fixup_partial_die (struct partial_die_info *part_die,
13943 struct dwarf2_cu *cu)
13944 {
13945 /* Once we've fixed up a die, there's no point in doing so again.
13946 This also avoids a memory leak if we were to call
13947 guess_partial_die_structure_name multiple times. */
13948 if (part_die->fixup_called)
13949 return;
13950
13951 /* If we found a reference attribute and the DIE has no name, try
13952 to find a name in the referred to DIE. */
13953
13954 if (part_die->name == NULL && part_die->has_specification)
13955 {
13956 struct partial_die_info *spec_die;
13957
13958 spec_die = find_partial_die (part_die->spec_offset,
13959 part_die->spec_is_dwz, cu);
13960
13961 fixup_partial_die (spec_die, cu);
13962
13963 if (spec_die->name)
13964 {
13965 part_die->name = spec_die->name;
13966
13967 /* Copy DW_AT_external attribute if it is set. */
13968 if (spec_die->is_external)
13969 part_die->is_external = spec_die->is_external;
13970 }
13971 }
13972
13973 /* Set default names for some unnamed DIEs. */
13974
13975 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
13976 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
13977
13978 /* If there is no parent die to provide a namespace, and there are
13979 children, see if we can determine the namespace from their linkage
13980 name. */
13981 if (cu->language == language_cplus
13982 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
13983 && part_die->die_parent == NULL
13984 && part_die->has_children
13985 && (part_die->tag == DW_TAG_class_type
13986 || part_die->tag == DW_TAG_structure_type
13987 || part_die->tag == DW_TAG_union_type))
13988 guess_partial_die_structure_name (part_die, cu);
13989
13990 /* GCC might emit a nameless struct or union that has a linkage
13991 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
13992 if (part_die->name == NULL
13993 && (part_die->tag == DW_TAG_class_type
13994 || part_die->tag == DW_TAG_interface_type
13995 || part_die->tag == DW_TAG_structure_type
13996 || part_die->tag == DW_TAG_union_type)
13997 && part_die->linkage_name != NULL)
13998 {
13999 char *demangled;
14000
14001 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
14002 if (demangled)
14003 {
14004 const char *base;
14005
14006 /* Strip any leading namespaces/classes, keep only the base name.
14007 DW_AT_name for named DIEs does not contain the prefixes. */
14008 base = strrchr (demangled, ':');
14009 if (base && base > demangled && base[-1] == ':')
14010 base++;
14011 else
14012 base = demangled;
14013
14014 part_die->name = obstack_copy0 (&cu->objfile->objfile_obstack,
14015 base, strlen (base));
14016 xfree (demangled);
14017 }
14018 }
14019
14020 part_die->fixup_called = 1;
14021 }
14022
14023 /* Read an attribute value described by an attribute form. */
14024
14025 static gdb_byte *
14026 read_attribute_value (const struct die_reader_specs *reader,
14027 struct attribute *attr, unsigned form,
14028 gdb_byte *info_ptr)
14029 {
14030 struct dwarf2_cu *cu = reader->cu;
14031 bfd *abfd = reader->abfd;
14032 struct comp_unit_head *cu_header = &cu->header;
14033 unsigned int bytes_read;
14034 struct dwarf_block *blk;
14035
14036 attr->form = form;
14037 switch (form)
14038 {
14039 case DW_FORM_ref_addr:
14040 if (cu->header.version == 2)
14041 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
14042 else
14043 DW_UNSND (attr) = read_offset (abfd, info_ptr,
14044 &cu->header, &bytes_read);
14045 info_ptr += bytes_read;
14046 break;
14047 case DW_FORM_GNU_ref_alt:
14048 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14049 info_ptr += bytes_read;
14050 break;
14051 case DW_FORM_addr:
14052 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
14053 info_ptr += bytes_read;
14054 break;
14055 case DW_FORM_block2:
14056 blk = dwarf_alloc_block (cu);
14057 blk->size = read_2_bytes (abfd, info_ptr);
14058 info_ptr += 2;
14059 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14060 info_ptr += blk->size;
14061 DW_BLOCK (attr) = blk;
14062 break;
14063 case DW_FORM_block4:
14064 blk = dwarf_alloc_block (cu);
14065 blk->size = read_4_bytes (abfd, info_ptr);
14066 info_ptr += 4;
14067 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14068 info_ptr += blk->size;
14069 DW_BLOCK (attr) = blk;
14070 break;
14071 case DW_FORM_data2:
14072 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
14073 info_ptr += 2;
14074 break;
14075 case DW_FORM_data4:
14076 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
14077 info_ptr += 4;
14078 break;
14079 case DW_FORM_data8:
14080 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
14081 info_ptr += 8;
14082 break;
14083 case DW_FORM_sec_offset:
14084 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14085 info_ptr += bytes_read;
14086 break;
14087 case DW_FORM_string:
14088 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
14089 DW_STRING_IS_CANONICAL (attr) = 0;
14090 info_ptr += bytes_read;
14091 break;
14092 case DW_FORM_strp:
14093 if (!cu->per_cu->is_dwz)
14094 {
14095 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
14096 &bytes_read);
14097 DW_STRING_IS_CANONICAL (attr) = 0;
14098 info_ptr += bytes_read;
14099 break;
14100 }
14101 /* FALLTHROUGH */
14102 case DW_FORM_GNU_strp_alt:
14103 {
14104 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14105 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
14106 &bytes_read);
14107
14108 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
14109 DW_STRING_IS_CANONICAL (attr) = 0;
14110 info_ptr += bytes_read;
14111 }
14112 break;
14113 case DW_FORM_exprloc:
14114 case DW_FORM_block:
14115 blk = dwarf_alloc_block (cu);
14116 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14117 info_ptr += bytes_read;
14118 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14119 info_ptr += blk->size;
14120 DW_BLOCK (attr) = blk;
14121 break;
14122 case DW_FORM_block1:
14123 blk = dwarf_alloc_block (cu);
14124 blk->size = read_1_byte (abfd, info_ptr);
14125 info_ptr += 1;
14126 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14127 info_ptr += blk->size;
14128 DW_BLOCK (attr) = blk;
14129 break;
14130 case DW_FORM_data1:
14131 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14132 info_ptr += 1;
14133 break;
14134 case DW_FORM_flag:
14135 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14136 info_ptr += 1;
14137 break;
14138 case DW_FORM_flag_present:
14139 DW_UNSND (attr) = 1;
14140 break;
14141 case DW_FORM_sdata:
14142 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
14143 info_ptr += bytes_read;
14144 break;
14145 case DW_FORM_udata:
14146 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14147 info_ptr += bytes_read;
14148 break;
14149 case DW_FORM_ref1:
14150 DW_UNSND (attr) = (cu->header.offset.sect_off
14151 + read_1_byte (abfd, info_ptr));
14152 info_ptr += 1;
14153 break;
14154 case DW_FORM_ref2:
14155 DW_UNSND (attr) = (cu->header.offset.sect_off
14156 + read_2_bytes (abfd, info_ptr));
14157 info_ptr += 2;
14158 break;
14159 case DW_FORM_ref4:
14160 DW_UNSND (attr) = (cu->header.offset.sect_off
14161 + read_4_bytes (abfd, info_ptr));
14162 info_ptr += 4;
14163 break;
14164 case DW_FORM_ref8:
14165 DW_UNSND (attr) = (cu->header.offset.sect_off
14166 + read_8_bytes (abfd, info_ptr));
14167 info_ptr += 8;
14168 break;
14169 case DW_FORM_ref_sig8:
14170 /* Convert the signature to something we can record in DW_UNSND
14171 for later lookup.
14172 NOTE: This is NULL if the type wasn't found. */
14173 DW_SIGNATURED_TYPE (attr) =
14174 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
14175 info_ptr += 8;
14176 break;
14177 case DW_FORM_ref_udata:
14178 DW_UNSND (attr) = (cu->header.offset.sect_off
14179 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
14180 info_ptr += bytes_read;
14181 break;
14182 case DW_FORM_indirect:
14183 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14184 info_ptr += bytes_read;
14185 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
14186 break;
14187 case DW_FORM_GNU_addr_index:
14188 if (reader->dwo_file == NULL)
14189 {
14190 /* For now flag a hard error.
14191 Later we can turn this into a complaint. */
14192 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14193 dwarf_form_name (form),
14194 bfd_get_filename (abfd));
14195 }
14196 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
14197 info_ptr += bytes_read;
14198 break;
14199 case DW_FORM_GNU_str_index:
14200 if (reader->dwo_file == NULL)
14201 {
14202 /* For now flag a hard error.
14203 Later we can turn this into a complaint if warranted. */
14204 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14205 dwarf_form_name (form),
14206 bfd_get_filename (abfd));
14207 }
14208 {
14209 ULONGEST str_index =
14210 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14211
14212 DW_STRING (attr) = read_str_index (reader, cu, str_index);
14213 DW_STRING_IS_CANONICAL (attr) = 0;
14214 info_ptr += bytes_read;
14215 }
14216 break;
14217 default:
14218 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
14219 dwarf_form_name (form),
14220 bfd_get_filename (abfd));
14221 }
14222
14223 /* Super hack. */
14224 if (cu->per_cu->is_dwz && is_ref_attr (attr))
14225 attr->form = DW_FORM_GNU_ref_alt;
14226
14227 /* We have seen instances where the compiler tried to emit a byte
14228 size attribute of -1 which ended up being encoded as an unsigned
14229 0xffffffff. Although 0xffffffff is technically a valid size value,
14230 an object of this size seems pretty unlikely so we can relatively
14231 safely treat these cases as if the size attribute was invalid and
14232 treat them as zero by default. */
14233 if (attr->name == DW_AT_byte_size
14234 && form == DW_FORM_data4
14235 && DW_UNSND (attr) >= 0xffffffff)
14236 {
14237 complaint
14238 (&symfile_complaints,
14239 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
14240 hex_string (DW_UNSND (attr)));
14241 DW_UNSND (attr) = 0;
14242 }
14243
14244 return info_ptr;
14245 }
14246
14247 /* Read an attribute described by an abbreviated attribute. */
14248
14249 static gdb_byte *
14250 read_attribute (const struct die_reader_specs *reader,
14251 struct attribute *attr, struct attr_abbrev *abbrev,
14252 gdb_byte *info_ptr)
14253 {
14254 attr->name = abbrev->name;
14255 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
14256 }
14257
14258 /* Read dwarf information from a buffer. */
14259
14260 static unsigned int
14261 read_1_byte (bfd *abfd, const gdb_byte *buf)
14262 {
14263 return bfd_get_8 (abfd, buf);
14264 }
14265
14266 static int
14267 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
14268 {
14269 return bfd_get_signed_8 (abfd, buf);
14270 }
14271
14272 static unsigned int
14273 read_2_bytes (bfd *abfd, const gdb_byte *buf)
14274 {
14275 return bfd_get_16 (abfd, buf);
14276 }
14277
14278 static int
14279 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
14280 {
14281 return bfd_get_signed_16 (abfd, buf);
14282 }
14283
14284 static unsigned int
14285 read_4_bytes (bfd *abfd, const gdb_byte *buf)
14286 {
14287 return bfd_get_32 (abfd, buf);
14288 }
14289
14290 static int
14291 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
14292 {
14293 return bfd_get_signed_32 (abfd, buf);
14294 }
14295
14296 static ULONGEST
14297 read_8_bytes (bfd *abfd, const gdb_byte *buf)
14298 {
14299 return bfd_get_64 (abfd, buf);
14300 }
14301
14302 static CORE_ADDR
14303 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
14304 unsigned int *bytes_read)
14305 {
14306 struct comp_unit_head *cu_header = &cu->header;
14307 CORE_ADDR retval = 0;
14308
14309 if (cu_header->signed_addr_p)
14310 {
14311 switch (cu_header->addr_size)
14312 {
14313 case 2:
14314 retval = bfd_get_signed_16 (abfd, buf);
14315 break;
14316 case 4:
14317 retval = bfd_get_signed_32 (abfd, buf);
14318 break;
14319 case 8:
14320 retval = bfd_get_signed_64 (abfd, buf);
14321 break;
14322 default:
14323 internal_error (__FILE__, __LINE__,
14324 _("read_address: bad switch, signed [in module %s]"),
14325 bfd_get_filename (abfd));
14326 }
14327 }
14328 else
14329 {
14330 switch (cu_header->addr_size)
14331 {
14332 case 2:
14333 retval = bfd_get_16 (abfd, buf);
14334 break;
14335 case 4:
14336 retval = bfd_get_32 (abfd, buf);
14337 break;
14338 case 8:
14339 retval = bfd_get_64 (abfd, buf);
14340 break;
14341 default:
14342 internal_error (__FILE__, __LINE__,
14343 _("read_address: bad switch, "
14344 "unsigned [in module %s]"),
14345 bfd_get_filename (abfd));
14346 }
14347 }
14348
14349 *bytes_read = cu_header->addr_size;
14350 return retval;
14351 }
14352
14353 /* Read the initial length from a section. The (draft) DWARF 3
14354 specification allows the initial length to take up either 4 bytes
14355 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
14356 bytes describe the length and all offsets will be 8 bytes in length
14357 instead of 4.
14358
14359 An older, non-standard 64-bit format is also handled by this
14360 function. The older format in question stores the initial length
14361 as an 8-byte quantity without an escape value. Lengths greater
14362 than 2^32 aren't very common which means that the initial 4 bytes
14363 is almost always zero. Since a length value of zero doesn't make
14364 sense for the 32-bit format, this initial zero can be considered to
14365 be an escape value which indicates the presence of the older 64-bit
14366 format. As written, the code can't detect (old format) lengths
14367 greater than 4GB. If it becomes necessary to handle lengths
14368 somewhat larger than 4GB, we could allow other small values (such
14369 as the non-sensical values of 1, 2, and 3) to also be used as
14370 escape values indicating the presence of the old format.
14371
14372 The value returned via bytes_read should be used to increment the
14373 relevant pointer after calling read_initial_length().
14374
14375 [ Note: read_initial_length() and read_offset() are based on the
14376 document entitled "DWARF Debugging Information Format", revision
14377 3, draft 8, dated November 19, 2001. This document was obtained
14378 from:
14379
14380 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
14381
14382 This document is only a draft and is subject to change. (So beware.)
14383
14384 Details regarding the older, non-standard 64-bit format were
14385 determined empirically by examining 64-bit ELF files produced by
14386 the SGI toolchain on an IRIX 6.5 machine.
14387
14388 - Kevin, July 16, 2002
14389 ] */
14390
14391 static LONGEST
14392 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
14393 {
14394 LONGEST length = bfd_get_32 (abfd, buf);
14395
14396 if (length == 0xffffffff)
14397 {
14398 length = bfd_get_64 (abfd, buf + 4);
14399 *bytes_read = 12;
14400 }
14401 else if (length == 0)
14402 {
14403 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
14404 length = bfd_get_64 (abfd, buf);
14405 *bytes_read = 8;
14406 }
14407 else
14408 {
14409 *bytes_read = 4;
14410 }
14411
14412 return length;
14413 }
14414
14415 /* Cover function for read_initial_length.
14416 Returns the length of the object at BUF, and stores the size of the
14417 initial length in *BYTES_READ and stores the size that offsets will be in
14418 *OFFSET_SIZE.
14419 If the initial length size is not equivalent to that specified in
14420 CU_HEADER then issue a complaint.
14421 This is useful when reading non-comp-unit headers. */
14422
14423 static LONGEST
14424 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
14425 const struct comp_unit_head *cu_header,
14426 unsigned int *bytes_read,
14427 unsigned int *offset_size)
14428 {
14429 LONGEST length = read_initial_length (abfd, buf, bytes_read);
14430
14431 gdb_assert (cu_header->initial_length_size == 4
14432 || cu_header->initial_length_size == 8
14433 || cu_header->initial_length_size == 12);
14434
14435 if (cu_header->initial_length_size != *bytes_read)
14436 complaint (&symfile_complaints,
14437 _("intermixed 32-bit and 64-bit DWARF sections"));
14438
14439 *offset_size = (*bytes_read == 4) ? 4 : 8;
14440 return length;
14441 }
14442
14443 /* Read an offset from the data stream. The size of the offset is
14444 given by cu_header->offset_size. */
14445
14446 static LONGEST
14447 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
14448 unsigned int *bytes_read)
14449 {
14450 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
14451
14452 *bytes_read = cu_header->offset_size;
14453 return offset;
14454 }
14455
14456 /* Read an offset from the data stream. */
14457
14458 static LONGEST
14459 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
14460 {
14461 LONGEST retval = 0;
14462
14463 switch (offset_size)
14464 {
14465 case 4:
14466 retval = bfd_get_32 (abfd, buf);
14467 break;
14468 case 8:
14469 retval = bfd_get_64 (abfd, buf);
14470 break;
14471 default:
14472 internal_error (__FILE__, __LINE__,
14473 _("read_offset_1: bad switch [in module %s]"),
14474 bfd_get_filename (abfd));
14475 }
14476
14477 return retval;
14478 }
14479
14480 static gdb_byte *
14481 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
14482 {
14483 /* If the size of a host char is 8 bits, we can return a pointer
14484 to the buffer, otherwise we have to copy the data to a buffer
14485 allocated on the temporary obstack. */
14486 gdb_assert (HOST_CHAR_BIT == 8);
14487 return buf;
14488 }
14489
14490 static char *
14491 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
14492 {
14493 /* If the size of a host char is 8 bits, we can return a pointer
14494 to the string, otherwise we have to copy the string to a buffer
14495 allocated on the temporary obstack. */
14496 gdb_assert (HOST_CHAR_BIT == 8);
14497 if (*buf == '\0')
14498 {
14499 *bytes_read_ptr = 1;
14500 return NULL;
14501 }
14502 *bytes_read_ptr = strlen ((char *) buf) + 1;
14503 return (char *) buf;
14504 }
14505
14506 static char *
14507 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
14508 {
14509 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
14510 if (dwarf2_per_objfile->str.buffer == NULL)
14511 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
14512 bfd_get_filename (abfd));
14513 if (str_offset >= dwarf2_per_objfile->str.size)
14514 error (_("DW_FORM_strp pointing outside of "
14515 ".debug_str section [in module %s]"),
14516 bfd_get_filename (abfd));
14517 gdb_assert (HOST_CHAR_BIT == 8);
14518 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
14519 return NULL;
14520 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
14521 }
14522
14523 /* Read a string at offset STR_OFFSET in the .debug_str section from
14524 the .dwz file DWZ. Throw an error if the offset is too large. If
14525 the string consists of a single NUL byte, return NULL; otherwise
14526 return a pointer to the string. */
14527
14528 static char *
14529 read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
14530 {
14531 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
14532
14533 if (dwz->str.buffer == NULL)
14534 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
14535 "section [in module %s]"),
14536 bfd_get_filename (dwz->dwz_bfd));
14537 if (str_offset >= dwz->str.size)
14538 error (_("DW_FORM_GNU_strp_alt pointing outside of "
14539 ".debug_str section [in module %s]"),
14540 bfd_get_filename (dwz->dwz_bfd));
14541 gdb_assert (HOST_CHAR_BIT == 8);
14542 if (dwz->str.buffer[str_offset] == '\0')
14543 return NULL;
14544 return (char *) (dwz->str.buffer + str_offset);
14545 }
14546
14547 static char *
14548 read_indirect_string (bfd *abfd, gdb_byte *buf,
14549 const struct comp_unit_head *cu_header,
14550 unsigned int *bytes_read_ptr)
14551 {
14552 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
14553
14554 return read_indirect_string_at_offset (abfd, str_offset);
14555 }
14556
14557 static ULONGEST
14558 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
14559 {
14560 ULONGEST result;
14561 unsigned int num_read;
14562 int i, shift;
14563 unsigned char byte;
14564
14565 result = 0;
14566 shift = 0;
14567 num_read = 0;
14568 i = 0;
14569 while (1)
14570 {
14571 byte = bfd_get_8 (abfd, buf);
14572 buf++;
14573 num_read++;
14574 result |= ((ULONGEST) (byte & 127) << shift);
14575 if ((byte & 128) == 0)
14576 {
14577 break;
14578 }
14579 shift += 7;
14580 }
14581 *bytes_read_ptr = num_read;
14582 return result;
14583 }
14584
14585 static LONGEST
14586 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
14587 {
14588 LONGEST result;
14589 int i, shift, num_read;
14590 unsigned char byte;
14591
14592 result = 0;
14593 shift = 0;
14594 num_read = 0;
14595 i = 0;
14596 while (1)
14597 {
14598 byte = bfd_get_8 (abfd, buf);
14599 buf++;
14600 num_read++;
14601 result |= ((LONGEST) (byte & 127) << shift);
14602 shift += 7;
14603 if ((byte & 128) == 0)
14604 {
14605 break;
14606 }
14607 }
14608 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
14609 result |= -(((LONGEST) 1) << shift);
14610 *bytes_read_ptr = num_read;
14611 return result;
14612 }
14613
14614 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
14615 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
14616 ADDR_SIZE is the size of addresses from the CU header. */
14617
14618 static CORE_ADDR
14619 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
14620 {
14621 struct objfile *objfile = dwarf2_per_objfile->objfile;
14622 bfd *abfd = objfile->obfd;
14623 const gdb_byte *info_ptr;
14624
14625 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
14626 if (dwarf2_per_objfile->addr.buffer == NULL)
14627 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
14628 objfile->name);
14629 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
14630 error (_("DW_FORM_addr_index pointing outside of "
14631 ".debug_addr section [in module %s]"),
14632 objfile->name);
14633 info_ptr = (dwarf2_per_objfile->addr.buffer
14634 + addr_base + addr_index * addr_size);
14635 if (addr_size == 4)
14636 return bfd_get_32 (abfd, info_ptr);
14637 else
14638 return bfd_get_64 (abfd, info_ptr);
14639 }
14640
14641 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
14642
14643 static CORE_ADDR
14644 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
14645 {
14646 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
14647 }
14648
14649 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
14650
14651 static CORE_ADDR
14652 read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
14653 unsigned int *bytes_read)
14654 {
14655 bfd *abfd = cu->objfile->obfd;
14656 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
14657
14658 return read_addr_index (cu, addr_index);
14659 }
14660
14661 /* Data structure to pass results from dwarf2_read_addr_index_reader
14662 back to dwarf2_read_addr_index. */
14663
14664 struct dwarf2_read_addr_index_data
14665 {
14666 ULONGEST addr_base;
14667 int addr_size;
14668 };
14669
14670 /* die_reader_func for dwarf2_read_addr_index. */
14671
14672 static void
14673 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
14674 gdb_byte *info_ptr,
14675 struct die_info *comp_unit_die,
14676 int has_children,
14677 void *data)
14678 {
14679 struct dwarf2_cu *cu = reader->cu;
14680 struct dwarf2_read_addr_index_data *aidata =
14681 (struct dwarf2_read_addr_index_data *) data;
14682
14683 aidata->addr_base = cu->addr_base;
14684 aidata->addr_size = cu->header.addr_size;
14685 }
14686
14687 /* Given an index in .debug_addr, fetch the value.
14688 NOTE: This can be called during dwarf expression evaluation,
14689 long after the debug information has been read, and thus per_cu->cu
14690 may no longer exist. */
14691
14692 CORE_ADDR
14693 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
14694 unsigned int addr_index)
14695 {
14696 struct objfile *objfile = per_cu->objfile;
14697 struct dwarf2_cu *cu = per_cu->cu;
14698 ULONGEST addr_base;
14699 int addr_size;
14700
14701 /* This is intended to be called from outside this file. */
14702 dw2_setup (objfile);
14703
14704 /* We need addr_base and addr_size.
14705 If we don't have PER_CU->cu, we have to get it.
14706 Nasty, but the alternative is storing the needed info in PER_CU,
14707 which at this point doesn't seem justified: it's not clear how frequently
14708 it would get used and it would increase the size of every PER_CU.
14709 Entry points like dwarf2_per_cu_addr_size do a similar thing
14710 so we're not in uncharted territory here.
14711 Alas we need to be a bit more complicated as addr_base is contained
14712 in the DIE.
14713
14714 We don't need to read the entire CU(/TU).
14715 We just need the header and top level die.
14716
14717 IWBN to use the aging mechanism to let us lazily later discard the CU.
14718 For now we skip this optimization. */
14719
14720 if (cu != NULL)
14721 {
14722 addr_base = cu->addr_base;
14723 addr_size = cu->header.addr_size;
14724 }
14725 else
14726 {
14727 struct dwarf2_read_addr_index_data aidata;
14728
14729 /* Note: We can't use init_cutu_and_read_dies_simple here,
14730 we need addr_base. */
14731 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
14732 dwarf2_read_addr_index_reader, &aidata);
14733 addr_base = aidata.addr_base;
14734 addr_size = aidata.addr_size;
14735 }
14736
14737 return read_addr_index_1 (addr_index, addr_base, addr_size);
14738 }
14739
14740 /* Given a DW_AT_str_index, fetch the string. */
14741
14742 static char *
14743 read_str_index (const struct die_reader_specs *reader,
14744 struct dwarf2_cu *cu, ULONGEST str_index)
14745 {
14746 struct objfile *objfile = dwarf2_per_objfile->objfile;
14747 const char *dwo_name = objfile->name;
14748 bfd *abfd = objfile->obfd;
14749 struct dwo_sections *sections = &reader->dwo_file->sections;
14750 gdb_byte *info_ptr;
14751 ULONGEST str_offset;
14752
14753 dwarf2_read_section (objfile, &sections->str);
14754 dwarf2_read_section (objfile, &sections->str_offsets);
14755 if (sections->str.buffer == NULL)
14756 error (_("DW_FORM_str_index used without .debug_str.dwo section"
14757 " in CU at offset 0x%lx [in module %s]"),
14758 (long) cu->header.offset.sect_off, dwo_name);
14759 if (sections->str_offsets.buffer == NULL)
14760 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
14761 " in CU at offset 0x%lx [in module %s]"),
14762 (long) cu->header.offset.sect_off, dwo_name);
14763 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
14764 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
14765 " section in CU at offset 0x%lx [in module %s]"),
14766 (long) cu->header.offset.sect_off, dwo_name);
14767 info_ptr = (sections->str_offsets.buffer
14768 + str_index * cu->header.offset_size);
14769 if (cu->header.offset_size == 4)
14770 str_offset = bfd_get_32 (abfd, info_ptr);
14771 else
14772 str_offset = bfd_get_64 (abfd, info_ptr);
14773 if (str_offset >= sections->str.size)
14774 error (_("Offset from DW_FORM_str_index pointing outside of"
14775 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
14776 (long) cu->header.offset.sect_off, dwo_name);
14777 return (char *) (sections->str.buffer + str_offset);
14778 }
14779
14780 /* Return the length of an LEB128 number in BUF. */
14781
14782 static int
14783 leb128_size (const gdb_byte *buf)
14784 {
14785 const gdb_byte *begin = buf;
14786 gdb_byte byte;
14787
14788 while (1)
14789 {
14790 byte = *buf++;
14791 if ((byte & 128) == 0)
14792 return buf - begin;
14793 }
14794 }
14795
14796 static void
14797 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
14798 {
14799 switch (lang)
14800 {
14801 case DW_LANG_C89:
14802 case DW_LANG_C99:
14803 case DW_LANG_C:
14804 cu->language = language_c;
14805 break;
14806 case DW_LANG_C_plus_plus:
14807 cu->language = language_cplus;
14808 break;
14809 case DW_LANG_D:
14810 cu->language = language_d;
14811 break;
14812 case DW_LANG_Fortran77:
14813 case DW_LANG_Fortran90:
14814 case DW_LANG_Fortran95:
14815 cu->language = language_fortran;
14816 break;
14817 case DW_LANG_Go:
14818 cu->language = language_go;
14819 break;
14820 case DW_LANG_Mips_Assembler:
14821 cu->language = language_asm;
14822 break;
14823 case DW_LANG_Java:
14824 cu->language = language_java;
14825 break;
14826 case DW_LANG_Ada83:
14827 case DW_LANG_Ada95:
14828 cu->language = language_ada;
14829 break;
14830 case DW_LANG_Modula2:
14831 cu->language = language_m2;
14832 break;
14833 case DW_LANG_Pascal83:
14834 cu->language = language_pascal;
14835 break;
14836 case DW_LANG_ObjC:
14837 cu->language = language_objc;
14838 break;
14839 case DW_LANG_Cobol74:
14840 case DW_LANG_Cobol85:
14841 default:
14842 cu->language = language_minimal;
14843 break;
14844 }
14845 cu->language_defn = language_def (cu->language);
14846 }
14847
14848 /* Return the named attribute or NULL if not there. */
14849
14850 static struct attribute *
14851 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
14852 {
14853 for (;;)
14854 {
14855 unsigned int i;
14856 struct attribute *spec = NULL;
14857
14858 for (i = 0; i < die->num_attrs; ++i)
14859 {
14860 if (die->attrs[i].name == name)
14861 return &die->attrs[i];
14862 if (die->attrs[i].name == DW_AT_specification
14863 || die->attrs[i].name == DW_AT_abstract_origin)
14864 spec = &die->attrs[i];
14865 }
14866
14867 if (!spec)
14868 break;
14869
14870 die = follow_die_ref (die, spec, &cu);
14871 }
14872
14873 return NULL;
14874 }
14875
14876 /* Return the named attribute or NULL if not there,
14877 but do not follow DW_AT_specification, etc.
14878 This is for use in contexts where we're reading .debug_types dies.
14879 Following DW_AT_specification, DW_AT_abstract_origin will take us
14880 back up the chain, and we want to go down. */
14881
14882 static struct attribute *
14883 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
14884 {
14885 unsigned int i;
14886
14887 for (i = 0; i < die->num_attrs; ++i)
14888 if (die->attrs[i].name == name)
14889 return &die->attrs[i];
14890
14891 return NULL;
14892 }
14893
14894 /* Return non-zero iff the attribute NAME is defined for the given DIE,
14895 and holds a non-zero value. This function should only be used for
14896 DW_FORM_flag or DW_FORM_flag_present attributes. */
14897
14898 static int
14899 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
14900 {
14901 struct attribute *attr = dwarf2_attr (die, name, cu);
14902
14903 return (attr && DW_UNSND (attr));
14904 }
14905
14906 static int
14907 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
14908 {
14909 /* A DIE is a declaration if it has a DW_AT_declaration attribute
14910 which value is non-zero. However, we have to be careful with
14911 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
14912 (via dwarf2_flag_true_p) follows this attribute. So we may
14913 end up accidently finding a declaration attribute that belongs
14914 to a different DIE referenced by the specification attribute,
14915 even though the given DIE does not have a declaration attribute. */
14916 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
14917 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
14918 }
14919
14920 /* Return the die giving the specification for DIE, if there is
14921 one. *SPEC_CU is the CU containing DIE on input, and the CU
14922 containing the return value on output. If there is no
14923 specification, but there is an abstract origin, that is
14924 returned. */
14925
14926 static struct die_info *
14927 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
14928 {
14929 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
14930 *spec_cu);
14931
14932 if (spec_attr == NULL)
14933 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
14934
14935 if (spec_attr == NULL)
14936 return NULL;
14937 else
14938 return follow_die_ref (die, spec_attr, spec_cu);
14939 }
14940
14941 /* Free the line_header structure *LH, and any arrays and strings it
14942 refers to.
14943 NOTE: This is also used as a "cleanup" function. */
14944
14945 static void
14946 free_line_header (struct line_header *lh)
14947 {
14948 if (lh->standard_opcode_lengths)
14949 xfree (lh->standard_opcode_lengths);
14950
14951 /* Remember that all the lh->file_names[i].name pointers are
14952 pointers into debug_line_buffer, and don't need to be freed. */
14953 if (lh->file_names)
14954 xfree (lh->file_names);
14955
14956 /* Similarly for the include directory names. */
14957 if (lh->include_dirs)
14958 xfree (lh->include_dirs);
14959
14960 xfree (lh);
14961 }
14962
14963 /* Add an entry to LH's include directory table. */
14964
14965 static void
14966 add_include_dir (struct line_header *lh, char *include_dir)
14967 {
14968 /* Grow the array if necessary. */
14969 if (lh->include_dirs_size == 0)
14970 {
14971 lh->include_dirs_size = 1; /* for testing */
14972 lh->include_dirs = xmalloc (lh->include_dirs_size
14973 * sizeof (*lh->include_dirs));
14974 }
14975 else if (lh->num_include_dirs >= lh->include_dirs_size)
14976 {
14977 lh->include_dirs_size *= 2;
14978 lh->include_dirs = xrealloc (lh->include_dirs,
14979 (lh->include_dirs_size
14980 * sizeof (*lh->include_dirs)));
14981 }
14982
14983 lh->include_dirs[lh->num_include_dirs++] = include_dir;
14984 }
14985
14986 /* Add an entry to LH's file name table. */
14987
14988 static void
14989 add_file_name (struct line_header *lh,
14990 char *name,
14991 unsigned int dir_index,
14992 unsigned int mod_time,
14993 unsigned int length)
14994 {
14995 struct file_entry *fe;
14996
14997 /* Grow the array if necessary. */
14998 if (lh->file_names_size == 0)
14999 {
15000 lh->file_names_size = 1; /* for testing */
15001 lh->file_names = xmalloc (lh->file_names_size
15002 * sizeof (*lh->file_names));
15003 }
15004 else if (lh->num_file_names >= lh->file_names_size)
15005 {
15006 lh->file_names_size *= 2;
15007 lh->file_names = xrealloc (lh->file_names,
15008 (lh->file_names_size
15009 * sizeof (*lh->file_names)));
15010 }
15011
15012 fe = &lh->file_names[lh->num_file_names++];
15013 fe->name = name;
15014 fe->dir_index = dir_index;
15015 fe->mod_time = mod_time;
15016 fe->length = length;
15017 fe->included_p = 0;
15018 fe->symtab = NULL;
15019 }
15020
15021 /* A convenience function to find the proper .debug_line section for a
15022 CU. */
15023
15024 static struct dwarf2_section_info *
15025 get_debug_line_section (struct dwarf2_cu *cu)
15026 {
15027 struct dwarf2_section_info *section;
15028
15029 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
15030 DWO file. */
15031 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15032 section = &cu->dwo_unit->dwo_file->sections.line;
15033 else if (cu->per_cu->is_dwz)
15034 {
15035 struct dwz_file *dwz = dwarf2_get_dwz_file ();
15036
15037 section = &dwz->line;
15038 }
15039 else
15040 section = &dwarf2_per_objfile->line;
15041
15042 return section;
15043 }
15044
15045 /* Read the statement program header starting at OFFSET in
15046 .debug_line, or .debug_line.dwo. Return a pointer
15047 to a struct line_header, allocated using xmalloc.
15048
15049 NOTE: the strings in the include directory and file name tables of
15050 the returned object point into the dwarf line section buffer,
15051 and must not be freed. */
15052
15053 static struct line_header *
15054 dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
15055 {
15056 struct cleanup *back_to;
15057 struct line_header *lh;
15058 gdb_byte *line_ptr;
15059 unsigned int bytes_read, offset_size;
15060 int i;
15061 char *cur_dir, *cur_file;
15062 struct dwarf2_section_info *section;
15063 bfd *abfd;
15064
15065 section = get_debug_line_section (cu);
15066 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15067 if (section->buffer == NULL)
15068 {
15069 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15070 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
15071 else
15072 complaint (&symfile_complaints, _("missing .debug_line section"));
15073 return 0;
15074 }
15075
15076 /* We can't do this until we know the section is non-empty.
15077 Only then do we know we have such a section. */
15078 abfd = section->asection->owner;
15079
15080 /* Make sure that at least there's room for the total_length field.
15081 That could be 12 bytes long, but we're just going to fudge that. */
15082 if (offset + 4 >= section->size)
15083 {
15084 dwarf2_statement_list_fits_in_line_number_section_complaint ();
15085 return 0;
15086 }
15087
15088 lh = xmalloc (sizeof (*lh));
15089 memset (lh, 0, sizeof (*lh));
15090 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
15091 (void *) lh);
15092
15093 line_ptr = section->buffer + offset;
15094
15095 /* Read in the header. */
15096 lh->total_length =
15097 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
15098 &bytes_read, &offset_size);
15099 line_ptr += bytes_read;
15100 if (line_ptr + lh->total_length > (section->buffer + section->size))
15101 {
15102 dwarf2_statement_list_fits_in_line_number_section_complaint ();
15103 return 0;
15104 }
15105 lh->statement_program_end = line_ptr + lh->total_length;
15106 lh->version = read_2_bytes (abfd, line_ptr);
15107 line_ptr += 2;
15108 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
15109 line_ptr += offset_size;
15110 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
15111 line_ptr += 1;
15112 if (lh->version >= 4)
15113 {
15114 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
15115 line_ptr += 1;
15116 }
15117 else
15118 lh->maximum_ops_per_instruction = 1;
15119
15120 if (lh->maximum_ops_per_instruction == 0)
15121 {
15122 lh->maximum_ops_per_instruction = 1;
15123 complaint (&symfile_complaints,
15124 _("invalid maximum_ops_per_instruction "
15125 "in `.debug_line' section"));
15126 }
15127
15128 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
15129 line_ptr += 1;
15130 lh->line_base = read_1_signed_byte (abfd, line_ptr);
15131 line_ptr += 1;
15132 lh->line_range = read_1_byte (abfd, line_ptr);
15133 line_ptr += 1;
15134 lh->opcode_base = read_1_byte (abfd, line_ptr);
15135 line_ptr += 1;
15136 lh->standard_opcode_lengths
15137 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
15138
15139 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
15140 for (i = 1; i < lh->opcode_base; ++i)
15141 {
15142 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
15143 line_ptr += 1;
15144 }
15145
15146 /* Read directory table. */
15147 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
15148 {
15149 line_ptr += bytes_read;
15150 add_include_dir (lh, cur_dir);
15151 }
15152 line_ptr += bytes_read;
15153
15154 /* Read file name table. */
15155 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
15156 {
15157 unsigned int dir_index, mod_time, length;
15158
15159 line_ptr += bytes_read;
15160 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15161 line_ptr += bytes_read;
15162 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15163 line_ptr += bytes_read;
15164 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15165 line_ptr += bytes_read;
15166
15167 add_file_name (lh, cur_file, dir_index, mod_time, length);
15168 }
15169 line_ptr += bytes_read;
15170 lh->statement_program_start = line_ptr;
15171
15172 if (line_ptr > (section->buffer + section->size))
15173 complaint (&symfile_complaints,
15174 _("line number info header doesn't "
15175 "fit in `.debug_line' section"));
15176
15177 discard_cleanups (back_to);
15178 return lh;
15179 }
15180
15181 /* Subroutine of dwarf_decode_lines to simplify it.
15182 Return the file name of the psymtab for included file FILE_INDEX
15183 in line header LH of PST.
15184 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15185 If space for the result is malloc'd, it will be freed by a cleanup.
15186 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
15187
15188 The function creates dangling cleanup registration. */
15189
15190 static char *
15191 psymtab_include_file_name (const struct line_header *lh, int file_index,
15192 const struct partial_symtab *pst,
15193 const char *comp_dir)
15194 {
15195 const struct file_entry fe = lh->file_names [file_index];
15196 char *include_name = fe.name;
15197 char *include_name_to_compare = include_name;
15198 char *dir_name = NULL;
15199 const char *pst_filename;
15200 char *copied_name = NULL;
15201 int file_is_pst;
15202
15203 if (fe.dir_index)
15204 dir_name = lh->include_dirs[fe.dir_index - 1];
15205
15206 if (!IS_ABSOLUTE_PATH (include_name)
15207 && (dir_name != NULL || comp_dir != NULL))
15208 {
15209 /* Avoid creating a duplicate psymtab for PST.
15210 We do this by comparing INCLUDE_NAME and PST_FILENAME.
15211 Before we do the comparison, however, we need to account
15212 for DIR_NAME and COMP_DIR.
15213 First prepend dir_name (if non-NULL). If we still don't
15214 have an absolute path prepend comp_dir (if non-NULL).
15215 However, the directory we record in the include-file's
15216 psymtab does not contain COMP_DIR (to match the
15217 corresponding symtab(s)).
15218
15219 Example:
15220
15221 bash$ cd /tmp
15222 bash$ gcc -g ./hello.c
15223 include_name = "hello.c"
15224 dir_name = "."
15225 DW_AT_comp_dir = comp_dir = "/tmp"
15226 DW_AT_name = "./hello.c" */
15227
15228 if (dir_name != NULL)
15229 {
15230 include_name = concat (dir_name, SLASH_STRING,
15231 include_name, (char *)NULL);
15232 include_name_to_compare = include_name;
15233 make_cleanup (xfree, include_name);
15234 }
15235 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
15236 {
15237 include_name_to_compare = concat (comp_dir, SLASH_STRING,
15238 include_name, (char *)NULL);
15239 }
15240 }
15241
15242 pst_filename = pst->filename;
15243 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
15244 {
15245 copied_name = concat (pst->dirname, SLASH_STRING,
15246 pst_filename, (char *)NULL);
15247 pst_filename = copied_name;
15248 }
15249
15250 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
15251
15252 if (include_name_to_compare != include_name)
15253 xfree (include_name_to_compare);
15254 if (copied_name != NULL)
15255 xfree (copied_name);
15256
15257 if (file_is_pst)
15258 return NULL;
15259 return include_name;
15260 }
15261
15262 /* Ignore this record_line request. */
15263
15264 static void
15265 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
15266 {
15267 return;
15268 }
15269
15270 /* Subroutine of dwarf_decode_lines to simplify it.
15271 Process the line number information in LH. */
15272
15273 static void
15274 dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
15275 struct dwarf2_cu *cu, struct partial_symtab *pst)
15276 {
15277 gdb_byte *line_ptr, *extended_end;
15278 gdb_byte *line_end;
15279 unsigned int bytes_read, extended_len;
15280 unsigned char op_code, extended_op, adj_opcode;
15281 CORE_ADDR baseaddr;
15282 struct objfile *objfile = cu->objfile;
15283 bfd *abfd = objfile->obfd;
15284 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15285 const int decode_for_pst_p = (pst != NULL);
15286 struct subfile *last_subfile = NULL;
15287 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
15288 = record_line;
15289
15290 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15291
15292 line_ptr = lh->statement_program_start;
15293 line_end = lh->statement_program_end;
15294
15295 /* Read the statement sequences until there's nothing left. */
15296 while (line_ptr < line_end)
15297 {
15298 /* state machine registers */
15299 CORE_ADDR address = 0;
15300 unsigned int file = 1;
15301 unsigned int line = 1;
15302 unsigned int column = 0;
15303 int is_stmt = lh->default_is_stmt;
15304 int basic_block = 0;
15305 int end_sequence = 0;
15306 CORE_ADDR addr;
15307 unsigned char op_index = 0;
15308
15309 if (!decode_for_pst_p && lh->num_file_names >= file)
15310 {
15311 /* Start a subfile for the current file of the state machine. */
15312 /* lh->include_dirs and lh->file_names are 0-based, but the
15313 directory and file name numbers in the statement program
15314 are 1-based. */
15315 struct file_entry *fe = &lh->file_names[file - 1];
15316 char *dir = NULL;
15317
15318 if (fe->dir_index)
15319 dir = lh->include_dirs[fe->dir_index - 1];
15320
15321 dwarf2_start_subfile (fe->name, dir, comp_dir);
15322 }
15323
15324 /* Decode the table. */
15325 while (!end_sequence)
15326 {
15327 op_code = read_1_byte (abfd, line_ptr);
15328 line_ptr += 1;
15329 if (line_ptr > line_end)
15330 {
15331 dwarf2_debug_line_missing_end_sequence_complaint ();
15332 break;
15333 }
15334
15335 if (op_code >= lh->opcode_base)
15336 {
15337 /* Special operand. */
15338 adj_opcode = op_code - lh->opcode_base;
15339 address += (((op_index + (adj_opcode / lh->line_range))
15340 / lh->maximum_ops_per_instruction)
15341 * lh->minimum_instruction_length);
15342 op_index = ((op_index + (adj_opcode / lh->line_range))
15343 % lh->maximum_ops_per_instruction);
15344 line += lh->line_base + (adj_opcode % lh->line_range);
15345 if (lh->num_file_names < file || file == 0)
15346 dwarf2_debug_line_missing_file_complaint ();
15347 /* For now we ignore lines not starting on an
15348 instruction boundary. */
15349 else if (op_index == 0)
15350 {
15351 lh->file_names[file - 1].included_p = 1;
15352 if (!decode_for_pst_p && is_stmt)
15353 {
15354 if (last_subfile != current_subfile)
15355 {
15356 addr = gdbarch_addr_bits_remove (gdbarch, address);
15357 if (last_subfile)
15358 (*p_record_line) (last_subfile, 0, addr);
15359 last_subfile = current_subfile;
15360 }
15361 /* Append row to matrix using current values. */
15362 addr = gdbarch_addr_bits_remove (gdbarch, address);
15363 (*p_record_line) (current_subfile, line, addr);
15364 }
15365 }
15366 basic_block = 0;
15367 }
15368 else switch (op_code)
15369 {
15370 case DW_LNS_extended_op:
15371 extended_len = read_unsigned_leb128 (abfd, line_ptr,
15372 &bytes_read);
15373 line_ptr += bytes_read;
15374 extended_end = line_ptr + extended_len;
15375 extended_op = read_1_byte (abfd, line_ptr);
15376 line_ptr += 1;
15377 switch (extended_op)
15378 {
15379 case DW_LNE_end_sequence:
15380 p_record_line = record_line;
15381 end_sequence = 1;
15382 break;
15383 case DW_LNE_set_address:
15384 address = read_address (abfd, line_ptr, cu, &bytes_read);
15385
15386 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
15387 {
15388 /* This line table is for a function which has been
15389 GCd by the linker. Ignore it. PR gdb/12528 */
15390
15391 long line_offset
15392 = line_ptr - get_debug_line_section (cu)->buffer;
15393
15394 complaint (&symfile_complaints,
15395 _(".debug_line address at offset 0x%lx is 0 "
15396 "[in module %s]"),
15397 line_offset, objfile->name);
15398 p_record_line = noop_record_line;
15399 }
15400
15401 op_index = 0;
15402 line_ptr += bytes_read;
15403 address += baseaddr;
15404 break;
15405 case DW_LNE_define_file:
15406 {
15407 char *cur_file;
15408 unsigned int dir_index, mod_time, length;
15409
15410 cur_file = read_direct_string (abfd, line_ptr,
15411 &bytes_read);
15412 line_ptr += bytes_read;
15413 dir_index =
15414 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15415 line_ptr += bytes_read;
15416 mod_time =
15417 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15418 line_ptr += bytes_read;
15419 length =
15420 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15421 line_ptr += bytes_read;
15422 add_file_name (lh, cur_file, dir_index, mod_time, length);
15423 }
15424 break;
15425 case DW_LNE_set_discriminator:
15426 /* The discriminator is not interesting to the debugger;
15427 just ignore it. */
15428 line_ptr = extended_end;
15429 break;
15430 default:
15431 complaint (&symfile_complaints,
15432 _("mangled .debug_line section"));
15433 return;
15434 }
15435 /* Make sure that we parsed the extended op correctly. If e.g.
15436 we expected a different address size than the producer used,
15437 we may have read the wrong number of bytes. */
15438 if (line_ptr != extended_end)
15439 {
15440 complaint (&symfile_complaints,
15441 _("mangled .debug_line section"));
15442 return;
15443 }
15444 break;
15445 case DW_LNS_copy:
15446 if (lh->num_file_names < file || file == 0)
15447 dwarf2_debug_line_missing_file_complaint ();
15448 else
15449 {
15450 lh->file_names[file - 1].included_p = 1;
15451 if (!decode_for_pst_p && is_stmt)
15452 {
15453 if (last_subfile != current_subfile)
15454 {
15455 addr = gdbarch_addr_bits_remove (gdbarch, address);
15456 if (last_subfile)
15457 (*p_record_line) (last_subfile, 0, addr);
15458 last_subfile = current_subfile;
15459 }
15460 addr = gdbarch_addr_bits_remove (gdbarch, address);
15461 (*p_record_line) (current_subfile, line, addr);
15462 }
15463 }
15464 basic_block = 0;
15465 break;
15466 case DW_LNS_advance_pc:
15467 {
15468 CORE_ADDR adjust
15469 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15470
15471 address += (((op_index + adjust)
15472 / lh->maximum_ops_per_instruction)
15473 * lh->minimum_instruction_length);
15474 op_index = ((op_index + adjust)
15475 % lh->maximum_ops_per_instruction);
15476 line_ptr += bytes_read;
15477 }
15478 break;
15479 case DW_LNS_advance_line:
15480 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
15481 line_ptr += bytes_read;
15482 break;
15483 case DW_LNS_set_file:
15484 {
15485 /* The arrays lh->include_dirs and lh->file_names are
15486 0-based, but the directory and file name numbers in
15487 the statement program are 1-based. */
15488 struct file_entry *fe;
15489 char *dir = NULL;
15490
15491 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15492 line_ptr += bytes_read;
15493 if (lh->num_file_names < file || file == 0)
15494 dwarf2_debug_line_missing_file_complaint ();
15495 else
15496 {
15497 fe = &lh->file_names[file - 1];
15498 if (fe->dir_index)
15499 dir = lh->include_dirs[fe->dir_index - 1];
15500 if (!decode_for_pst_p)
15501 {
15502 last_subfile = current_subfile;
15503 dwarf2_start_subfile (fe->name, dir, comp_dir);
15504 }
15505 }
15506 }
15507 break;
15508 case DW_LNS_set_column:
15509 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15510 line_ptr += bytes_read;
15511 break;
15512 case DW_LNS_negate_stmt:
15513 is_stmt = (!is_stmt);
15514 break;
15515 case DW_LNS_set_basic_block:
15516 basic_block = 1;
15517 break;
15518 /* Add to the address register of the state machine the
15519 address increment value corresponding to special opcode
15520 255. I.e., this value is scaled by the minimum
15521 instruction length since special opcode 255 would have
15522 scaled the increment. */
15523 case DW_LNS_const_add_pc:
15524 {
15525 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
15526
15527 address += (((op_index + adjust)
15528 / lh->maximum_ops_per_instruction)
15529 * lh->minimum_instruction_length);
15530 op_index = ((op_index + adjust)
15531 % lh->maximum_ops_per_instruction);
15532 }
15533 break;
15534 case DW_LNS_fixed_advance_pc:
15535 address += read_2_bytes (abfd, line_ptr);
15536 op_index = 0;
15537 line_ptr += 2;
15538 break;
15539 default:
15540 {
15541 /* Unknown standard opcode, ignore it. */
15542 int i;
15543
15544 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
15545 {
15546 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15547 line_ptr += bytes_read;
15548 }
15549 }
15550 }
15551 }
15552 if (lh->num_file_names < file || file == 0)
15553 dwarf2_debug_line_missing_file_complaint ();
15554 else
15555 {
15556 lh->file_names[file - 1].included_p = 1;
15557 if (!decode_for_pst_p)
15558 {
15559 addr = gdbarch_addr_bits_remove (gdbarch, address);
15560 (*p_record_line) (current_subfile, 0, addr);
15561 }
15562 }
15563 }
15564 }
15565
15566 /* Decode the Line Number Program (LNP) for the given line_header
15567 structure and CU. The actual information extracted and the type
15568 of structures created from the LNP depends on the value of PST.
15569
15570 1. If PST is NULL, then this procedure uses the data from the program
15571 to create all necessary symbol tables, and their linetables.
15572
15573 2. If PST is not NULL, this procedure reads the program to determine
15574 the list of files included by the unit represented by PST, and
15575 builds all the associated partial symbol tables.
15576
15577 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15578 It is used for relative paths in the line table.
15579 NOTE: When processing partial symtabs (pst != NULL),
15580 comp_dir == pst->dirname.
15581
15582 NOTE: It is important that psymtabs have the same file name (via strcmp)
15583 as the corresponding symtab. Since COMP_DIR is not used in the name of the
15584 symtab we don't use it in the name of the psymtabs we create.
15585 E.g. expand_line_sal requires this when finding psymtabs to expand.
15586 A good testcase for this is mb-inline.exp. */
15587
15588 static void
15589 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
15590 struct dwarf2_cu *cu, struct partial_symtab *pst,
15591 int want_line_info)
15592 {
15593 struct objfile *objfile = cu->objfile;
15594 const int decode_for_pst_p = (pst != NULL);
15595 struct subfile *first_subfile = current_subfile;
15596
15597 if (want_line_info)
15598 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
15599
15600 if (decode_for_pst_p)
15601 {
15602 int file_index;
15603
15604 /* Now that we're done scanning the Line Header Program, we can
15605 create the psymtab of each included file. */
15606 for (file_index = 0; file_index < lh->num_file_names; file_index++)
15607 if (lh->file_names[file_index].included_p == 1)
15608 {
15609 char *include_name =
15610 psymtab_include_file_name (lh, file_index, pst, comp_dir);
15611 if (include_name != NULL)
15612 dwarf2_create_include_psymtab (include_name, pst, objfile);
15613 }
15614 }
15615 else
15616 {
15617 /* Make sure a symtab is created for every file, even files
15618 which contain only variables (i.e. no code with associated
15619 line numbers). */
15620 int i;
15621
15622 for (i = 0; i < lh->num_file_names; i++)
15623 {
15624 char *dir = NULL;
15625 struct file_entry *fe;
15626
15627 fe = &lh->file_names[i];
15628 if (fe->dir_index)
15629 dir = lh->include_dirs[fe->dir_index - 1];
15630 dwarf2_start_subfile (fe->name, dir, comp_dir);
15631
15632 /* Skip the main file; we don't need it, and it must be
15633 allocated last, so that it will show up before the
15634 non-primary symtabs in the objfile's symtab list. */
15635 if (current_subfile == first_subfile)
15636 continue;
15637
15638 if (current_subfile->symtab == NULL)
15639 current_subfile->symtab = allocate_symtab (current_subfile->name,
15640 objfile);
15641 fe->symtab = current_subfile->symtab;
15642 }
15643 }
15644 }
15645
15646 /* Start a subfile for DWARF. FILENAME is the name of the file and
15647 DIRNAME the name of the source directory which contains FILENAME
15648 or NULL if not known. COMP_DIR is the compilation directory for the
15649 linetable's compilation unit or NULL if not known.
15650 This routine tries to keep line numbers from identical absolute and
15651 relative file names in a common subfile.
15652
15653 Using the `list' example from the GDB testsuite, which resides in
15654 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
15655 of /srcdir/list0.c yields the following debugging information for list0.c:
15656
15657 DW_AT_name: /srcdir/list0.c
15658 DW_AT_comp_dir: /compdir
15659 files.files[0].name: list0.h
15660 files.files[0].dir: /srcdir
15661 files.files[1].name: list0.c
15662 files.files[1].dir: /srcdir
15663
15664 The line number information for list0.c has to end up in a single
15665 subfile, so that `break /srcdir/list0.c:1' works as expected.
15666 start_subfile will ensure that this happens provided that we pass the
15667 concatenation of files.files[1].dir and files.files[1].name as the
15668 subfile's name. */
15669
15670 static void
15671 dwarf2_start_subfile (char *filename, const char *dirname,
15672 const char *comp_dir)
15673 {
15674 char *fullname;
15675
15676 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
15677 `start_symtab' will always pass the contents of DW_AT_comp_dir as
15678 second argument to start_subfile. To be consistent, we do the
15679 same here. In order not to lose the line information directory,
15680 we concatenate it to the filename when it makes sense.
15681 Note that the Dwarf3 standard says (speaking of filenames in line
15682 information): ``The directory index is ignored for file names
15683 that represent full path names''. Thus ignoring dirname in the
15684 `else' branch below isn't an issue. */
15685
15686 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
15687 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
15688 else
15689 fullname = filename;
15690
15691 start_subfile (fullname, comp_dir);
15692
15693 if (fullname != filename)
15694 xfree (fullname);
15695 }
15696
15697 /* Start a symtab for DWARF.
15698 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
15699
15700 static void
15701 dwarf2_start_symtab (struct dwarf2_cu *cu,
15702 const char *name, const char *comp_dir, CORE_ADDR low_pc)
15703 {
15704 start_symtab (name, comp_dir, low_pc);
15705 record_debugformat ("DWARF 2");
15706 record_producer (cu->producer);
15707
15708 /* We assume that we're processing GCC output. */
15709 processing_gcc_compilation = 2;
15710
15711 cu->processing_has_namespace_info = 0;
15712 }
15713
15714 static void
15715 var_decode_location (struct attribute *attr, struct symbol *sym,
15716 struct dwarf2_cu *cu)
15717 {
15718 struct objfile *objfile = cu->objfile;
15719 struct comp_unit_head *cu_header = &cu->header;
15720
15721 /* NOTE drow/2003-01-30: There used to be a comment and some special
15722 code here to turn a symbol with DW_AT_external and a
15723 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
15724 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
15725 with some versions of binutils) where shared libraries could have
15726 relocations against symbols in their debug information - the
15727 minimal symbol would have the right address, but the debug info
15728 would not. It's no longer necessary, because we will explicitly
15729 apply relocations when we read in the debug information now. */
15730
15731 /* A DW_AT_location attribute with no contents indicates that a
15732 variable has been optimized away. */
15733 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
15734 {
15735 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
15736 return;
15737 }
15738
15739 /* Handle one degenerate form of location expression specially, to
15740 preserve GDB's previous behavior when section offsets are
15741 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
15742 then mark this symbol as LOC_STATIC. */
15743
15744 if (attr_form_is_block (attr)
15745 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
15746 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
15747 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
15748 && (DW_BLOCK (attr)->size
15749 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
15750 {
15751 unsigned int dummy;
15752
15753 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
15754 SYMBOL_VALUE_ADDRESS (sym) =
15755 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
15756 else
15757 SYMBOL_VALUE_ADDRESS (sym) =
15758 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
15759 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
15760 fixup_symbol_section (sym, objfile);
15761 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
15762 SYMBOL_SECTION (sym));
15763 return;
15764 }
15765
15766 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
15767 expression evaluator, and use LOC_COMPUTED only when necessary
15768 (i.e. when the value of a register or memory location is
15769 referenced, or a thread-local block, etc.). Then again, it might
15770 not be worthwhile. I'm assuming that it isn't unless performance
15771 or memory numbers show me otherwise. */
15772
15773 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
15774
15775 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
15776 cu->has_loclist = 1;
15777 }
15778
15779 /* Given a pointer to a DWARF information entry, figure out if we need
15780 to make a symbol table entry for it, and if so, create a new entry
15781 and return a pointer to it.
15782 If TYPE is NULL, determine symbol type from the die, otherwise
15783 used the passed type.
15784 If SPACE is not NULL, use it to hold the new symbol. If it is
15785 NULL, allocate a new symbol on the objfile's obstack. */
15786
15787 static struct symbol *
15788 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
15789 struct symbol *space)
15790 {
15791 struct objfile *objfile = cu->objfile;
15792 struct symbol *sym = NULL;
15793 const char *name;
15794 struct attribute *attr = NULL;
15795 struct attribute *attr2 = NULL;
15796 CORE_ADDR baseaddr;
15797 struct pending **list_to_add = NULL;
15798
15799 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
15800
15801 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15802
15803 name = dwarf2_name (die, cu);
15804 if (name)
15805 {
15806 const char *linkagename;
15807 int suppress_add = 0;
15808
15809 if (space)
15810 sym = space;
15811 else
15812 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
15813 OBJSTAT (objfile, n_syms++);
15814
15815 /* Cache this symbol's name and the name's demangled form (if any). */
15816 SYMBOL_SET_LANGUAGE (sym, cu->language);
15817 linkagename = dwarf2_physname (name, die, cu);
15818 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
15819
15820 /* Fortran does not have mangling standard and the mangling does differ
15821 between gfortran, iFort etc. */
15822 if (cu->language == language_fortran
15823 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
15824 symbol_set_demangled_name (&(sym->ginfo),
15825 dwarf2_full_name (name, die, cu),
15826 NULL);
15827
15828 /* Default assumptions.
15829 Use the passed type or decode it from the die. */
15830 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
15831 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
15832 if (type != NULL)
15833 SYMBOL_TYPE (sym) = type;
15834 else
15835 SYMBOL_TYPE (sym) = die_type (die, cu);
15836 attr = dwarf2_attr (die,
15837 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
15838 cu);
15839 if (attr)
15840 {
15841 SYMBOL_LINE (sym) = DW_UNSND (attr);
15842 }
15843
15844 attr = dwarf2_attr (die,
15845 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
15846 cu);
15847 if (attr)
15848 {
15849 int file_index = DW_UNSND (attr);
15850
15851 if (cu->line_header == NULL
15852 || file_index > cu->line_header->num_file_names)
15853 complaint (&symfile_complaints,
15854 _("file index out of range"));
15855 else if (file_index > 0)
15856 {
15857 struct file_entry *fe;
15858
15859 fe = &cu->line_header->file_names[file_index - 1];
15860 SYMBOL_SYMTAB (sym) = fe->symtab;
15861 }
15862 }
15863
15864 switch (die->tag)
15865 {
15866 case DW_TAG_label:
15867 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
15868 if (attr)
15869 {
15870 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
15871 }
15872 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
15873 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
15874 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
15875 add_symbol_to_list (sym, cu->list_in_scope);
15876 break;
15877 case DW_TAG_subprogram:
15878 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15879 finish_block. */
15880 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
15881 attr2 = dwarf2_attr (die, DW_AT_external, cu);
15882 if ((attr2 && (DW_UNSND (attr2) != 0))
15883 || cu->language == language_ada)
15884 {
15885 /* Subprograms marked external are stored as a global symbol.
15886 Ada subprograms, whether marked external or not, are always
15887 stored as a global symbol, because we want to be able to
15888 access them globally. For instance, we want to be able
15889 to break on a nested subprogram without having to
15890 specify the context. */
15891 list_to_add = &global_symbols;
15892 }
15893 else
15894 {
15895 list_to_add = cu->list_in_scope;
15896 }
15897 break;
15898 case DW_TAG_inlined_subroutine:
15899 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15900 finish_block. */
15901 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
15902 SYMBOL_INLINED (sym) = 1;
15903 list_to_add = cu->list_in_scope;
15904 break;
15905 case DW_TAG_template_value_param:
15906 suppress_add = 1;
15907 /* Fall through. */
15908 case DW_TAG_constant:
15909 case DW_TAG_variable:
15910 case DW_TAG_member:
15911 /* Compilation with minimal debug info may result in
15912 variables with missing type entries. Change the
15913 misleading `void' type to something sensible. */
15914 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
15915 SYMBOL_TYPE (sym)
15916 = objfile_type (objfile)->nodebug_data_symbol;
15917
15918 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15919 /* In the case of DW_TAG_member, we should only be called for
15920 static const members. */
15921 if (die->tag == DW_TAG_member)
15922 {
15923 /* dwarf2_add_field uses die_is_declaration,
15924 so we do the same. */
15925 gdb_assert (die_is_declaration (die, cu));
15926 gdb_assert (attr);
15927 }
15928 if (attr)
15929 {
15930 dwarf2_const_value (attr, sym, cu);
15931 attr2 = dwarf2_attr (die, DW_AT_external, cu);
15932 if (!suppress_add)
15933 {
15934 if (attr2 && (DW_UNSND (attr2) != 0))
15935 list_to_add = &global_symbols;
15936 else
15937 list_to_add = cu->list_in_scope;
15938 }
15939 break;
15940 }
15941 attr = dwarf2_attr (die, DW_AT_location, cu);
15942 if (attr)
15943 {
15944 var_decode_location (attr, sym, cu);
15945 attr2 = dwarf2_attr (die, DW_AT_external, cu);
15946
15947 /* Fortran explicitly imports any global symbols to the local
15948 scope by DW_TAG_common_block. */
15949 if (cu->language == language_fortran && die->parent
15950 && die->parent->tag == DW_TAG_common_block)
15951 attr2 = NULL;
15952
15953 if (SYMBOL_CLASS (sym) == LOC_STATIC
15954 && SYMBOL_VALUE_ADDRESS (sym) == 0
15955 && !dwarf2_per_objfile->has_section_at_zero)
15956 {
15957 /* When a static variable is eliminated by the linker,
15958 the corresponding debug information is not stripped
15959 out, but the variable address is set to null;
15960 do not add such variables into symbol table. */
15961 }
15962 else if (attr2 && (DW_UNSND (attr2) != 0))
15963 {
15964 /* Workaround gfortran PR debug/40040 - it uses
15965 DW_AT_location for variables in -fPIC libraries which may
15966 get overriden by other libraries/executable and get
15967 a different address. Resolve it by the minimal symbol
15968 which may come from inferior's executable using copy
15969 relocation. Make this workaround only for gfortran as for
15970 other compilers GDB cannot guess the minimal symbol
15971 Fortran mangling kind. */
15972 if (cu->language == language_fortran && die->parent
15973 && die->parent->tag == DW_TAG_module
15974 && cu->producer
15975 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
15976 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
15977
15978 /* A variable with DW_AT_external is never static,
15979 but it may be block-scoped. */
15980 list_to_add = (cu->list_in_scope == &file_symbols
15981 ? &global_symbols : cu->list_in_scope);
15982 }
15983 else
15984 list_to_add = cu->list_in_scope;
15985 }
15986 else
15987 {
15988 /* We do not know the address of this symbol.
15989 If it is an external symbol and we have type information
15990 for it, enter the symbol as a LOC_UNRESOLVED symbol.
15991 The address of the variable will then be determined from
15992 the minimal symbol table whenever the variable is
15993 referenced. */
15994 attr2 = dwarf2_attr (die, DW_AT_external, cu);
15995
15996 /* Fortran explicitly imports any global symbols to the local
15997 scope by DW_TAG_common_block. */
15998 if (cu->language == language_fortran && die->parent
15999 && die->parent->tag == DW_TAG_common_block)
16000 {
16001 /* SYMBOL_CLASS doesn't matter here because
16002 read_common_block is going to reset it. */
16003 if (!suppress_add)
16004 list_to_add = cu->list_in_scope;
16005 }
16006 else if (attr2 && (DW_UNSND (attr2) != 0)
16007 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
16008 {
16009 /* A variable with DW_AT_external is never static, but it
16010 may be block-scoped. */
16011 list_to_add = (cu->list_in_scope == &file_symbols
16012 ? &global_symbols : cu->list_in_scope);
16013
16014 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
16015 }
16016 else if (!die_is_declaration (die, cu))
16017 {
16018 /* Use the default LOC_OPTIMIZED_OUT class. */
16019 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
16020 if (!suppress_add)
16021 list_to_add = cu->list_in_scope;
16022 }
16023 }
16024 break;
16025 case DW_TAG_formal_parameter:
16026 /* If we are inside a function, mark this as an argument. If
16027 not, we might be looking at an argument to an inlined function
16028 when we do not have enough information to show inlined frames;
16029 pretend it's a local variable in that case so that the user can
16030 still see it. */
16031 if (context_stack_depth > 0
16032 && context_stack[context_stack_depth - 1].name != NULL)
16033 SYMBOL_IS_ARGUMENT (sym) = 1;
16034 attr = dwarf2_attr (die, DW_AT_location, cu);
16035 if (attr)
16036 {
16037 var_decode_location (attr, sym, cu);
16038 }
16039 attr = dwarf2_attr (die, DW_AT_const_value, cu);
16040 if (attr)
16041 {
16042 dwarf2_const_value (attr, sym, cu);
16043 }
16044
16045 list_to_add = cu->list_in_scope;
16046 break;
16047 case DW_TAG_unspecified_parameters:
16048 /* From varargs functions; gdb doesn't seem to have any
16049 interest in this information, so just ignore it for now.
16050 (FIXME?) */
16051 break;
16052 case DW_TAG_template_type_param:
16053 suppress_add = 1;
16054 /* Fall through. */
16055 case DW_TAG_class_type:
16056 case DW_TAG_interface_type:
16057 case DW_TAG_structure_type:
16058 case DW_TAG_union_type:
16059 case DW_TAG_set_type:
16060 case DW_TAG_enumeration_type:
16061 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16062 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
16063
16064 {
16065 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
16066 really ever be static objects: otherwise, if you try
16067 to, say, break of a class's method and you're in a file
16068 which doesn't mention that class, it won't work unless
16069 the check for all static symbols in lookup_symbol_aux
16070 saves you. See the OtherFileClass tests in
16071 gdb.c++/namespace.exp. */
16072
16073 if (!suppress_add)
16074 {
16075 list_to_add = (cu->list_in_scope == &file_symbols
16076 && (cu->language == language_cplus
16077 || cu->language == language_java)
16078 ? &global_symbols : cu->list_in_scope);
16079
16080 /* The semantics of C++ state that "struct foo {
16081 ... }" also defines a typedef for "foo". A Java
16082 class declaration also defines a typedef for the
16083 class. */
16084 if (cu->language == language_cplus
16085 || cu->language == language_java
16086 || cu->language == language_ada)
16087 {
16088 /* The symbol's name is already allocated along
16089 with this objfile, so we don't need to
16090 duplicate it for the type. */
16091 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
16092 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
16093 }
16094 }
16095 }
16096 break;
16097 case DW_TAG_typedef:
16098 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16099 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
16100 list_to_add = cu->list_in_scope;
16101 break;
16102 case DW_TAG_base_type:
16103 case DW_TAG_subrange_type:
16104 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16105 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
16106 list_to_add = cu->list_in_scope;
16107 break;
16108 case DW_TAG_enumerator:
16109 attr = dwarf2_attr (die, DW_AT_const_value, cu);
16110 if (attr)
16111 {
16112 dwarf2_const_value (attr, sym, cu);
16113 }
16114 {
16115 /* NOTE: carlton/2003-11-10: See comment above in the
16116 DW_TAG_class_type, etc. block. */
16117
16118 list_to_add = (cu->list_in_scope == &file_symbols
16119 && (cu->language == language_cplus
16120 || cu->language == language_java)
16121 ? &global_symbols : cu->list_in_scope);
16122 }
16123 break;
16124 case DW_TAG_namespace:
16125 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16126 list_to_add = &global_symbols;
16127 break;
16128 case DW_TAG_common_block:
16129 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
16130 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
16131 add_symbol_to_list (sym, cu->list_in_scope);
16132 break;
16133 default:
16134 /* Not a tag we recognize. Hopefully we aren't processing
16135 trash data, but since we must specifically ignore things
16136 we don't recognize, there is nothing else we should do at
16137 this point. */
16138 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
16139 dwarf_tag_name (die->tag));
16140 break;
16141 }
16142
16143 if (suppress_add)
16144 {
16145 sym->hash_next = objfile->template_symbols;
16146 objfile->template_symbols = sym;
16147 list_to_add = NULL;
16148 }
16149
16150 if (list_to_add != NULL)
16151 add_symbol_to_list (sym, list_to_add);
16152
16153 /* For the benefit of old versions of GCC, check for anonymous
16154 namespaces based on the demangled name. */
16155 if (!cu->processing_has_namespace_info
16156 && cu->language == language_cplus)
16157 cp_scan_for_anonymous_namespaces (sym, objfile);
16158 }
16159 return (sym);
16160 }
16161
16162 /* A wrapper for new_symbol_full that always allocates a new symbol. */
16163
16164 static struct symbol *
16165 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16166 {
16167 return new_symbol_full (die, type, cu, NULL);
16168 }
16169
16170 /* Given an attr with a DW_FORM_dataN value in host byte order,
16171 zero-extend it as appropriate for the symbol's type. The DWARF
16172 standard (v4) is not entirely clear about the meaning of using
16173 DW_FORM_dataN for a constant with a signed type, where the type is
16174 wider than the data. The conclusion of a discussion on the DWARF
16175 list was that this is unspecified. We choose to always zero-extend
16176 because that is the interpretation long in use by GCC. */
16177
16178 static gdb_byte *
16179 dwarf2_const_value_data (struct attribute *attr, struct type *type,
16180 const char *name, struct obstack *obstack,
16181 struct dwarf2_cu *cu, LONGEST *value, int bits)
16182 {
16183 struct objfile *objfile = cu->objfile;
16184 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
16185 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
16186 LONGEST l = DW_UNSND (attr);
16187
16188 if (bits < sizeof (*value) * 8)
16189 {
16190 l &= ((LONGEST) 1 << bits) - 1;
16191 *value = l;
16192 }
16193 else if (bits == sizeof (*value) * 8)
16194 *value = l;
16195 else
16196 {
16197 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
16198 store_unsigned_integer (bytes, bits / 8, byte_order, l);
16199 return bytes;
16200 }
16201
16202 return NULL;
16203 }
16204
16205 /* Read a constant value from an attribute. Either set *VALUE, or if
16206 the value does not fit in *VALUE, set *BYTES - either already
16207 allocated on the objfile obstack, or newly allocated on OBSTACK,
16208 or, set *BATON, if we translated the constant to a location
16209 expression. */
16210
16211 static void
16212 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
16213 const char *name, struct obstack *obstack,
16214 struct dwarf2_cu *cu,
16215 LONGEST *value, gdb_byte **bytes,
16216 struct dwarf2_locexpr_baton **baton)
16217 {
16218 struct objfile *objfile = cu->objfile;
16219 struct comp_unit_head *cu_header = &cu->header;
16220 struct dwarf_block *blk;
16221 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
16222 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
16223
16224 *value = 0;
16225 *bytes = NULL;
16226 *baton = NULL;
16227
16228 switch (attr->form)
16229 {
16230 case DW_FORM_addr:
16231 case DW_FORM_GNU_addr_index:
16232 {
16233 gdb_byte *data;
16234
16235 if (TYPE_LENGTH (type) != cu_header->addr_size)
16236 dwarf2_const_value_length_mismatch_complaint (name,
16237 cu_header->addr_size,
16238 TYPE_LENGTH (type));
16239 /* Symbols of this form are reasonably rare, so we just
16240 piggyback on the existing location code rather than writing
16241 a new implementation of symbol_computed_ops. */
16242 *baton = obstack_alloc (&objfile->objfile_obstack,
16243 sizeof (struct dwarf2_locexpr_baton));
16244 (*baton)->per_cu = cu->per_cu;
16245 gdb_assert ((*baton)->per_cu);
16246
16247 (*baton)->size = 2 + cu_header->addr_size;
16248 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
16249 (*baton)->data = data;
16250
16251 data[0] = DW_OP_addr;
16252 store_unsigned_integer (&data[1], cu_header->addr_size,
16253 byte_order, DW_ADDR (attr));
16254 data[cu_header->addr_size + 1] = DW_OP_stack_value;
16255 }
16256 break;
16257 case DW_FORM_string:
16258 case DW_FORM_strp:
16259 case DW_FORM_GNU_str_index:
16260 case DW_FORM_GNU_strp_alt:
16261 /* DW_STRING is already allocated on the objfile obstack, point
16262 directly to it. */
16263 *bytes = (gdb_byte *) DW_STRING (attr);
16264 break;
16265 case DW_FORM_block1:
16266 case DW_FORM_block2:
16267 case DW_FORM_block4:
16268 case DW_FORM_block:
16269 case DW_FORM_exprloc:
16270 blk = DW_BLOCK (attr);
16271 if (TYPE_LENGTH (type) != blk->size)
16272 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
16273 TYPE_LENGTH (type));
16274 *bytes = blk->data;
16275 break;
16276
16277 /* The DW_AT_const_value attributes are supposed to carry the
16278 symbol's value "represented as it would be on the target
16279 architecture." By the time we get here, it's already been
16280 converted to host endianness, so we just need to sign- or
16281 zero-extend it as appropriate. */
16282 case DW_FORM_data1:
16283 *bytes = dwarf2_const_value_data (attr, type, name,
16284 obstack, cu, value, 8);
16285 break;
16286 case DW_FORM_data2:
16287 *bytes = dwarf2_const_value_data (attr, type, name,
16288 obstack, cu, value, 16);
16289 break;
16290 case DW_FORM_data4:
16291 *bytes = dwarf2_const_value_data (attr, type, name,
16292 obstack, cu, value, 32);
16293 break;
16294 case DW_FORM_data8:
16295 *bytes = dwarf2_const_value_data (attr, type, name,
16296 obstack, cu, value, 64);
16297 break;
16298
16299 case DW_FORM_sdata:
16300 *value = DW_SND (attr);
16301 break;
16302
16303 case DW_FORM_udata:
16304 *value = DW_UNSND (attr);
16305 break;
16306
16307 default:
16308 complaint (&symfile_complaints,
16309 _("unsupported const value attribute form: '%s'"),
16310 dwarf_form_name (attr->form));
16311 *value = 0;
16312 break;
16313 }
16314 }
16315
16316
16317 /* Copy constant value from an attribute to a symbol. */
16318
16319 static void
16320 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
16321 struct dwarf2_cu *cu)
16322 {
16323 struct objfile *objfile = cu->objfile;
16324 struct comp_unit_head *cu_header = &cu->header;
16325 LONGEST value;
16326 gdb_byte *bytes;
16327 struct dwarf2_locexpr_baton *baton;
16328
16329 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
16330 SYMBOL_PRINT_NAME (sym),
16331 &objfile->objfile_obstack, cu,
16332 &value, &bytes, &baton);
16333
16334 if (baton != NULL)
16335 {
16336 SYMBOL_LOCATION_BATON (sym) = baton;
16337 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16338 }
16339 else if (bytes != NULL)
16340 {
16341 SYMBOL_VALUE_BYTES (sym) = bytes;
16342 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
16343 }
16344 else
16345 {
16346 SYMBOL_VALUE (sym) = value;
16347 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
16348 }
16349 }
16350
16351 /* Return the type of the die in question using its DW_AT_type attribute. */
16352
16353 static struct type *
16354 die_type (struct die_info *die, struct dwarf2_cu *cu)
16355 {
16356 struct attribute *type_attr;
16357
16358 type_attr = dwarf2_attr (die, DW_AT_type, cu);
16359 if (!type_attr)
16360 {
16361 /* A missing DW_AT_type represents a void type. */
16362 return objfile_type (cu->objfile)->builtin_void;
16363 }
16364
16365 return lookup_die_type (die, type_attr, cu);
16366 }
16367
16368 /* True iff CU's producer generates GNAT Ada auxiliary information
16369 that allows to find parallel types through that information instead
16370 of having to do expensive parallel lookups by type name. */
16371
16372 static int
16373 need_gnat_info (struct dwarf2_cu *cu)
16374 {
16375 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
16376 of GNAT produces this auxiliary information, without any indication
16377 that it is produced. Part of enhancing the FSF version of GNAT
16378 to produce that information will be to put in place an indicator
16379 that we can use in order to determine whether the descriptive type
16380 info is available or not. One suggestion that has been made is
16381 to use a new attribute, attached to the CU die. For now, assume
16382 that the descriptive type info is not available. */
16383 return 0;
16384 }
16385
16386 /* Return the auxiliary type of the die in question using its
16387 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
16388 attribute is not present. */
16389
16390 static struct type *
16391 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
16392 {
16393 struct attribute *type_attr;
16394
16395 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
16396 if (!type_attr)
16397 return NULL;
16398
16399 return lookup_die_type (die, type_attr, cu);
16400 }
16401
16402 /* If DIE has a descriptive_type attribute, then set the TYPE's
16403 descriptive type accordingly. */
16404
16405 static void
16406 set_descriptive_type (struct type *type, struct die_info *die,
16407 struct dwarf2_cu *cu)
16408 {
16409 struct type *descriptive_type = die_descriptive_type (die, cu);
16410
16411 if (descriptive_type)
16412 {
16413 ALLOCATE_GNAT_AUX_TYPE (type);
16414 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
16415 }
16416 }
16417
16418 /* Return the containing type of the die in question using its
16419 DW_AT_containing_type attribute. */
16420
16421 static struct type *
16422 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
16423 {
16424 struct attribute *type_attr;
16425
16426 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
16427 if (!type_attr)
16428 error (_("Dwarf Error: Problem turning containing type into gdb type "
16429 "[in module %s]"), cu->objfile->name);
16430
16431 return lookup_die_type (die, type_attr, cu);
16432 }
16433
16434 /* Look up the type of DIE in CU using its type attribute ATTR.
16435 If there is no type substitute an error marker. */
16436
16437 static struct type *
16438 lookup_die_type (struct die_info *die, struct attribute *attr,
16439 struct dwarf2_cu *cu)
16440 {
16441 struct objfile *objfile = cu->objfile;
16442 struct type *this_type;
16443
16444 /* First see if we have it cached. */
16445
16446 if (attr->form == DW_FORM_GNU_ref_alt)
16447 {
16448 struct dwarf2_per_cu_data *per_cu;
16449 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16450
16451 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
16452 this_type = get_die_type_at_offset (offset, per_cu);
16453 }
16454 else if (is_ref_attr (attr))
16455 {
16456 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16457
16458 this_type = get_die_type_at_offset (offset, cu->per_cu);
16459 }
16460 else if (attr->form == DW_FORM_ref_sig8)
16461 {
16462 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
16463
16464 /* sig_type will be NULL if the signatured type is missing from
16465 the debug info. */
16466 if (sig_type == NULL)
16467 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
16468 "at 0x%x [in module %s]"),
16469 die->offset.sect_off, objfile->name);
16470
16471 gdb_assert (sig_type->per_cu.is_debug_types);
16472 /* If we haven't filled in type_offset_in_section yet, then we
16473 haven't read the type in yet. */
16474 this_type = NULL;
16475 if (sig_type->type_offset_in_section.sect_off != 0)
16476 {
16477 this_type =
16478 get_die_type_at_offset (sig_type->type_offset_in_section,
16479 &sig_type->per_cu);
16480 }
16481 }
16482 else
16483 {
16484 dump_die_for_error (die);
16485 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
16486 dwarf_attr_name (attr->name), objfile->name);
16487 }
16488
16489 /* If not cached we need to read it in. */
16490
16491 if (this_type == NULL)
16492 {
16493 struct die_info *type_die;
16494 struct dwarf2_cu *type_cu = cu;
16495
16496 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
16497 /* If we found the type now, it's probably because the type came
16498 from an inter-CU reference and the type's CU got expanded before
16499 ours. */
16500 this_type = get_die_type (type_die, type_cu);
16501 if (this_type == NULL)
16502 this_type = read_type_die_1 (type_die, type_cu);
16503 }
16504
16505 /* If we still don't have a type use an error marker. */
16506
16507 if (this_type == NULL)
16508 {
16509 char *message, *saved;
16510
16511 /* read_type_die already issued a complaint. */
16512 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
16513 objfile->name,
16514 cu->header.offset.sect_off,
16515 die->offset.sect_off);
16516 saved = obstack_copy0 (&objfile->objfile_obstack,
16517 message, strlen (message));
16518 xfree (message);
16519
16520 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
16521 }
16522
16523 return this_type;
16524 }
16525
16526 /* Return the type in DIE, CU.
16527 Returns NULL for invalid types.
16528
16529 This first does a lookup in the appropriate type_hash table,
16530 and only reads the die in if necessary.
16531
16532 NOTE: This can be called when reading in partial or full symbols. */
16533
16534 static struct type *
16535 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
16536 {
16537 struct type *this_type;
16538
16539 this_type = get_die_type (die, cu);
16540 if (this_type)
16541 return this_type;
16542
16543 return read_type_die_1 (die, cu);
16544 }
16545
16546 /* Read the type in DIE, CU.
16547 Returns NULL for invalid types. */
16548
16549 static struct type *
16550 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
16551 {
16552 struct type *this_type = NULL;
16553
16554 switch (die->tag)
16555 {
16556 case DW_TAG_class_type:
16557 case DW_TAG_interface_type:
16558 case DW_TAG_structure_type:
16559 case DW_TAG_union_type:
16560 this_type = read_structure_type (die, cu);
16561 break;
16562 case DW_TAG_enumeration_type:
16563 this_type = read_enumeration_type (die, cu);
16564 break;
16565 case DW_TAG_subprogram:
16566 case DW_TAG_subroutine_type:
16567 case DW_TAG_inlined_subroutine:
16568 this_type = read_subroutine_type (die, cu);
16569 break;
16570 case DW_TAG_array_type:
16571 this_type = read_array_type (die, cu);
16572 break;
16573 case DW_TAG_set_type:
16574 this_type = read_set_type (die, cu);
16575 break;
16576 case DW_TAG_pointer_type:
16577 this_type = read_tag_pointer_type (die, cu);
16578 break;
16579 case DW_TAG_ptr_to_member_type:
16580 this_type = read_tag_ptr_to_member_type (die, cu);
16581 break;
16582 case DW_TAG_reference_type:
16583 this_type = read_tag_reference_type (die, cu);
16584 break;
16585 case DW_TAG_const_type:
16586 this_type = read_tag_const_type (die, cu);
16587 break;
16588 case DW_TAG_volatile_type:
16589 this_type = read_tag_volatile_type (die, cu);
16590 break;
16591 case DW_TAG_restrict_type:
16592 this_type = read_tag_restrict_type (die, cu);
16593 break;
16594 case DW_TAG_string_type:
16595 this_type = read_tag_string_type (die, cu);
16596 break;
16597 case DW_TAG_typedef:
16598 this_type = read_typedef (die, cu);
16599 break;
16600 case DW_TAG_subrange_type:
16601 this_type = read_subrange_type (die, cu);
16602 break;
16603 case DW_TAG_base_type:
16604 this_type = read_base_type (die, cu);
16605 break;
16606 case DW_TAG_unspecified_type:
16607 this_type = read_unspecified_type (die, cu);
16608 break;
16609 case DW_TAG_namespace:
16610 this_type = read_namespace_type (die, cu);
16611 break;
16612 case DW_TAG_module:
16613 this_type = read_module_type (die, cu);
16614 break;
16615 default:
16616 complaint (&symfile_complaints,
16617 _("unexpected tag in read_type_die: '%s'"),
16618 dwarf_tag_name (die->tag));
16619 break;
16620 }
16621
16622 return this_type;
16623 }
16624
16625 /* See if we can figure out if the class lives in a namespace. We do
16626 this by looking for a member function; its demangled name will
16627 contain namespace info, if there is any.
16628 Return the computed name or NULL.
16629 Space for the result is allocated on the objfile's obstack.
16630 This is the full-die version of guess_partial_die_structure_name.
16631 In this case we know DIE has no useful parent. */
16632
16633 static char *
16634 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
16635 {
16636 struct die_info *spec_die;
16637 struct dwarf2_cu *spec_cu;
16638 struct die_info *child;
16639
16640 spec_cu = cu;
16641 spec_die = die_specification (die, &spec_cu);
16642 if (spec_die != NULL)
16643 {
16644 die = spec_die;
16645 cu = spec_cu;
16646 }
16647
16648 for (child = die->child;
16649 child != NULL;
16650 child = child->sibling)
16651 {
16652 if (child->tag == DW_TAG_subprogram)
16653 {
16654 struct attribute *attr;
16655
16656 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
16657 if (attr == NULL)
16658 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
16659 if (attr != NULL)
16660 {
16661 char *actual_name
16662 = language_class_name_from_physname (cu->language_defn,
16663 DW_STRING (attr));
16664 char *name = NULL;
16665
16666 if (actual_name != NULL)
16667 {
16668 const char *die_name = dwarf2_name (die, cu);
16669
16670 if (die_name != NULL
16671 && strcmp (die_name, actual_name) != 0)
16672 {
16673 /* Strip off the class name from the full name.
16674 We want the prefix. */
16675 int die_name_len = strlen (die_name);
16676 int actual_name_len = strlen (actual_name);
16677
16678 /* Test for '::' as a sanity check. */
16679 if (actual_name_len > die_name_len + 2
16680 && actual_name[actual_name_len
16681 - die_name_len - 1] == ':')
16682 name =
16683 obstack_copy0 (&cu->objfile->objfile_obstack,
16684 actual_name,
16685 actual_name_len - die_name_len - 2);
16686 }
16687 }
16688 xfree (actual_name);
16689 return name;
16690 }
16691 }
16692 }
16693
16694 return NULL;
16695 }
16696
16697 /* GCC might emit a nameless typedef that has a linkage name. Determine the
16698 prefix part in such case. See
16699 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16700
16701 static char *
16702 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
16703 {
16704 struct attribute *attr;
16705 char *base;
16706
16707 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
16708 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
16709 return NULL;
16710
16711 attr = dwarf2_attr (die, DW_AT_name, cu);
16712 if (attr != NULL && DW_STRING (attr) != NULL)
16713 return NULL;
16714
16715 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16716 if (attr == NULL)
16717 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16718 if (attr == NULL || DW_STRING (attr) == NULL)
16719 return NULL;
16720
16721 /* dwarf2_name had to be already called. */
16722 gdb_assert (DW_STRING_IS_CANONICAL (attr));
16723
16724 /* Strip the base name, keep any leading namespaces/classes. */
16725 base = strrchr (DW_STRING (attr), ':');
16726 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
16727 return "";
16728
16729 return obstack_copy0 (&cu->objfile->objfile_obstack,
16730 DW_STRING (attr), &base[-1] - DW_STRING (attr));
16731 }
16732
16733 /* Return the name of the namespace/class that DIE is defined within,
16734 or "" if we can't tell. The caller should not xfree the result.
16735
16736 For example, if we're within the method foo() in the following
16737 code:
16738
16739 namespace N {
16740 class C {
16741 void foo () {
16742 }
16743 };
16744 }
16745
16746 then determine_prefix on foo's die will return "N::C". */
16747
16748 static const char *
16749 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
16750 {
16751 struct die_info *parent, *spec_die;
16752 struct dwarf2_cu *spec_cu;
16753 struct type *parent_type;
16754 char *retval;
16755
16756 if (cu->language != language_cplus && cu->language != language_java
16757 && cu->language != language_fortran)
16758 return "";
16759
16760 retval = anonymous_struct_prefix (die, cu);
16761 if (retval)
16762 return retval;
16763
16764 /* We have to be careful in the presence of DW_AT_specification.
16765 For example, with GCC 3.4, given the code
16766
16767 namespace N {
16768 void foo() {
16769 // Definition of N::foo.
16770 }
16771 }
16772
16773 then we'll have a tree of DIEs like this:
16774
16775 1: DW_TAG_compile_unit
16776 2: DW_TAG_namespace // N
16777 3: DW_TAG_subprogram // declaration of N::foo
16778 4: DW_TAG_subprogram // definition of N::foo
16779 DW_AT_specification // refers to die #3
16780
16781 Thus, when processing die #4, we have to pretend that we're in
16782 the context of its DW_AT_specification, namely the contex of die
16783 #3. */
16784 spec_cu = cu;
16785 spec_die = die_specification (die, &spec_cu);
16786 if (spec_die == NULL)
16787 parent = die->parent;
16788 else
16789 {
16790 parent = spec_die->parent;
16791 cu = spec_cu;
16792 }
16793
16794 if (parent == NULL)
16795 return "";
16796 else if (parent->building_fullname)
16797 {
16798 const char *name;
16799 const char *parent_name;
16800
16801 /* It has been seen on RealView 2.2 built binaries,
16802 DW_TAG_template_type_param types actually _defined_ as
16803 children of the parent class:
16804
16805 enum E {};
16806 template class <class Enum> Class{};
16807 Class<enum E> class_e;
16808
16809 1: DW_TAG_class_type (Class)
16810 2: DW_TAG_enumeration_type (E)
16811 3: DW_TAG_enumerator (enum1:0)
16812 3: DW_TAG_enumerator (enum2:1)
16813 ...
16814 2: DW_TAG_template_type_param
16815 DW_AT_type DW_FORM_ref_udata (E)
16816
16817 Besides being broken debug info, it can put GDB into an
16818 infinite loop. Consider:
16819
16820 When we're building the full name for Class<E>, we'll start
16821 at Class, and go look over its template type parameters,
16822 finding E. We'll then try to build the full name of E, and
16823 reach here. We're now trying to build the full name of E,
16824 and look over the parent DIE for containing scope. In the
16825 broken case, if we followed the parent DIE of E, we'd again
16826 find Class, and once again go look at its template type
16827 arguments, etc., etc. Simply don't consider such parent die
16828 as source-level parent of this die (it can't be, the language
16829 doesn't allow it), and break the loop here. */
16830 name = dwarf2_name (die, cu);
16831 parent_name = dwarf2_name (parent, cu);
16832 complaint (&symfile_complaints,
16833 _("template param type '%s' defined within parent '%s'"),
16834 name ? name : "<unknown>",
16835 parent_name ? parent_name : "<unknown>");
16836 return "";
16837 }
16838 else
16839 switch (parent->tag)
16840 {
16841 case DW_TAG_namespace:
16842 parent_type = read_type_die (parent, cu);
16843 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
16844 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
16845 Work around this problem here. */
16846 if (cu->language == language_cplus
16847 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
16848 return "";
16849 /* We give a name to even anonymous namespaces. */
16850 return TYPE_TAG_NAME (parent_type);
16851 case DW_TAG_class_type:
16852 case DW_TAG_interface_type:
16853 case DW_TAG_structure_type:
16854 case DW_TAG_union_type:
16855 case DW_TAG_module:
16856 parent_type = read_type_die (parent, cu);
16857 if (TYPE_TAG_NAME (parent_type) != NULL)
16858 return TYPE_TAG_NAME (parent_type);
16859 else
16860 /* An anonymous structure is only allowed non-static data
16861 members; no typedefs, no member functions, et cetera.
16862 So it does not need a prefix. */
16863 return "";
16864 case DW_TAG_compile_unit:
16865 case DW_TAG_partial_unit:
16866 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
16867 if (cu->language == language_cplus
16868 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
16869 && die->child != NULL
16870 && (die->tag == DW_TAG_class_type
16871 || die->tag == DW_TAG_structure_type
16872 || die->tag == DW_TAG_union_type))
16873 {
16874 char *name = guess_full_die_structure_name (die, cu);
16875 if (name != NULL)
16876 return name;
16877 }
16878 return "";
16879 default:
16880 return determine_prefix (parent, cu);
16881 }
16882 }
16883
16884 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
16885 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
16886 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
16887 an obconcat, otherwise allocate storage for the result. The CU argument is
16888 used to determine the language and hence, the appropriate separator. */
16889
16890 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
16891
16892 static char *
16893 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
16894 int physname, struct dwarf2_cu *cu)
16895 {
16896 const char *lead = "";
16897 const char *sep;
16898
16899 if (suffix == NULL || suffix[0] == '\0'
16900 || prefix == NULL || prefix[0] == '\0')
16901 sep = "";
16902 else if (cu->language == language_java)
16903 sep = ".";
16904 else if (cu->language == language_fortran && physname)
16905 {
16906 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
16907 DW_AT_MIPS_linkage_name is preferred and used instead. */
16908
16909 lead = "__";
16910 sep = "_MOD_";
16911 }
16912 else
16913 sep = "::";
16914
16915 if (prefix == NULL)
16916 prefix = "";
16917 if (suffix == NULL)
16918 suffix = "";
16919
16920 if (obs == NULL)
16921 {
16922 char *retval
16923 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
16924
16925 strcpy (retval, lead);
16926 strcat (retval, prefix);
16927 strcat (retval, sep);
16928 strcat (retval, suffix);
16929 return retval;
16930 }
16931 else
16932 {
16933 /* We have an obstack. */
16934 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
16935 }
16936 }
16937
16938 /* Return sibling of die, NULL if no sibling. */
16939
16940 static struct die_info *
16941 sibling_die (struct die_info *die)
16942 {
16943 return die->sibling;
16944 }
16945
16946 /* Get name of a die, return NULL if not found. */
16947
16948 static const char *
16949 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
16950 struct obstack *obstack)
16951 {
16952 if (name && cu->language == language_cplus)
16953 {
16954 char *canon_name = cp_canonicalize_string (name);
16955
16956 if (canon_name != NULL)
16957 {
16958 if (strcmp (canon_name, name) != 0)
16959 name = obstack_copy0 (obstack, canon_name, strlen (canon_name));
16960 xfree (canon_name);
16961 }
16962 }
16963
16964 return name;
16965 }
16966
16967 /* Get name of a die, return NULL if not found. */
16968
16969 static const char *
16970 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
16971 {
16972 struct attribute *attr;
16973
16974 attr = dwarf2_attr (die, DW_AT_name, cu);
16975 if ((!attr || !DW_STRING (attr))
16976 && die->tag != DW_TAG_class_type
16977 && die->tag != DW_TAG_interface_type
16978 && die->tag != DW_TAG_structure_type
16979 && die->tag != DW_TAG_union_type)
16980 return NULL;
16981
16982 switch (die->tag)
16983 {
16984 case DW_TAG_compile_unit:
16985 case DW_TAG_partial_unit:
16986 /* Compilation units have a DW_AT_name that is a filename, not
16987 a source language identifier. */
16988 case DW_TAG_enumeration_type:
16989 case DW_TAG_enumerator:
16990 /* These tags always have simple identifiers already; no need
16991 to canonicalize them. */
16992 return DW_STRING (attr);
16993
16994 case DW_TAG_subprogram:
16995 /* Java constructors will all be named "<init>", so return
16996 the class name when we see this special case. */
16997 if (cu->language == language_java
16998 && DW_STRING (attr) != NULL
16999 && strcmp (DW_STRING (attr), "<init>") == 0)
17000 {
17001 struct dwarf2_cu *spec_cu = cu;
17002 struct die_info *spec_die;
17003
17004 /* GCJ will output '<init>' for Java constructor names.
17005 For this special case, return the name of the parent class. */
17006
17007 /* GCJ may output suprogram DIEs with AT_specification set.
17008 If so, use the name of the specified DIE. */
17009 spec_die = die_specification (die, &spec_cu);
17010 if (spec_die != NULL)
17011 return dwarf2_name (spec_die, spec_cu);
17012
17013 do
17014 {
17015 die = die->parent;
17016 if (die->tag == DW_TAG_class_type)
17017 return dwarf2_name (die, cu);
17018 }
17019 while (die->tag != DW_TAG_compile_unit
17020 && die->tag != DW_TAG_partial_unit);
17021 }
17022 break;
17023
17024 case DW_TAG_class_type:
17025 case DW_TAG_interface_type:
17026 case DW_TAG_structure_type:
17027 case DW_TAG_union_type:
17028 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
17029 structures or unions. These were of the form "._%d" in GCC 4.1,
17030 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
17031 and GCC 4.4. We work around this problem by ignoring these. */
17032 if (attr && DW_STRING (attr)
17033 && (strncmp (DW_STRING (attr), "._", 2) == 0
17034 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
17035 return NULL;
17036
17037 /* GCC might emit a nameless typedef that has a linkage name. See
17038 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17039 if (!attr || DW_STRING (attr) == NULL)
17040 {
17041 char *demangled = NULL;
17042
17043 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
17044 if (attr == NULL)
17045 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
17046
17047 if (attr == NULL || DW_STRING (attr) == NULL)
17048 return NULL;
17049
17050 /* Avoid demangling DW_STRING (attr) the second time on a second
17051 call for the same DIE. */
17052 if (!DW_STRING_IS_CANONICAL (attr))
17053 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
17054
17055 if (demangled)
17056 {
17057 char *base;
17058
17059 /* FIXME: we already did this for the partial symbol... */
17060 DW_STRING (attr) = obstack_copy0 (&cu->objfile->objfile_obstack,
17061 demangled, strlen (demangled));
17062 DW_STRING_IS_CANONICAL (attr) = 1;
17063 xfree (demangled);
17064
17065 /* Strip any leading namespaces/classes, keep only the base name.
17066 DW_AT_name for named DIEs does not contain the prefixes. */
17067 base = strrchr (DW_STRING (attr), ':');
17068 if (base && base > DW_STRING (attr) && base[-1] == ':')
17069 return &base[1];
17070 else
17071 return DW_STRING (attr);
17072 }
17073 }
17074 break;
17075
17076 default:
17077 break;
17078 }
17079
17080 if (!DW_STRING_IS_CANONICAL (attr))
17081 {
17082 DW_STRING (attr)
17083 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
17084 &cu->objfile->objfile_obstack);
17085 DW_STRING_IS_CANONICAL (attr) = 1;
17086 }
17087 return DW_STRING (attr);
17088 }
17089
17090 /* Return the die that this die in an extension of, or NULL if there
17091 is none. *EXT_CU is the CU containing DIE on input, and the CU
17092 containing the return value on output. */
17093
17094 static struct die_info *
17095 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
17096 {
17097 struct attribute *attr;
17098
17099 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
17100 if (attr == NULL)
17101 return NULL;
17102
17103 return follow_die_ref (die, attr, ext_cu);
17104 }
17105
17106 /* Convert a DIE tag into its string name. */
17107
17108 static const char *
17109 dwarf_tag_name (unsigned tag)
17110 {
17111 const char *name = get_DW_TAG_name (tag);
17112
17113 if (name == NULL)
17114 return "DW_TAG_<unknown>";
17115
17116 return name;
17117 }
17118
17119 /* Convert a DWARF attribute code into its string name. */
17120
17121 static const char *
17122 dwarf_attr_name (unsigned attr)
17123 {
17124 const char *name;
17125
17126 #ifdef MIPS /* collides with DW_AT_HP_block_index */
17127 if (attr == DW_AT_MIPS_fde)
17128 return "DW_AT_MIPS_fde";
17129 #else
17130 if (attr == DW_AT_HP_block_index)
17131 return "DW_AT_HP_block_index";
17132 #endif
17133
17134 name = get_DW_AT_name (attr);
17135
17136 if (name == NULL)
17137 return "DW_AT_<unknown>";
17138
17139 return name;
17140 }
17141
17142 /* Convert a DWARF value form code into its string name. */
17143
17144 static const char *
17145 dwarf_form_name (unsigned form)
17146 {
17147 const char *name = get_DW_FORM_name (form);
17148
17149 if (name == NULL)
17150 return "DW_FORM_<unknown>";
17151
17152 return name;
17153 }
17154
17155 static char *
17156 dwarf_bool_name (unsigned mybool)
17157 {
17158 if (mybool)
17159 return "TRUE";
17160 else
17161 return "FALSE";
17162 }
17163
17164 /* Convert a DWARF type code into its string name. */
17165
17166 static const char *
17167 dwarf_type_encoding_name (unsigned enc)
17168 {
17169 const char *name = get_DW_ATE_name (enc);
17170
17171 if (name == NULL)
17172 return "DW_ATE_<unknown>";
17173
17174 return name;
17175 }
17176
17177 static void
17178 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
17179 {
17180 unsigned int i;
17181
17182 print_spaces (indent, f);
17183 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
17184 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
17185
17186 if (die->parent != NULL)
17187 {
17188 print_spaces (indent, f);
17189 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
17190 die->parent->offset.sect_off);
17191 }
17192
17193 print_spaces (indent, f);
17194 fprintf_unfiltered (f, " has children: %s\n",
17195 dwarf_bool_name (die->child != NULL));
17196
17197 print_spaces (indent, f);
17198 fprintf_unfiltered (f, " attributes:\n");
17199
17200 for (i = 0; i < die->num_attrs; ++i)
17201 {
17202 print_spaces (indent, f);
17203 fprintf_unfiltered (f, " %s (%s) ",
17204 dwarf_attr_name (die->attrs[i].name),
17205 dwarf_form_name (die->attrs[i].form));
17206
17207 switch (die->attrs[i].form)
17208 {
17209 case DW_FORM_addr:
17210 case DW_FORM_GNU_addr_index:
17211 fprintf_unfiltered (f, "address: ");
17212 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
17213 break;
17214 case DW_FORM_block2:
17215 case DW_FORM_block4:
17216 case DW_FORM_block:
17217 case DW_FORM_block1:
17218 fprintf_unfiltered (f, "block: size %s",
17219 pulongest (DW_BLOCK (&die->attrs[i])->size));
17220 break;
17221 case DW_FORM_exprloc:
17222 fprintf_unfiltered (f, "expression: size %s",
17223 pulongest (DW_BLOCK (&die->attrs[i])->size));
17224 break;
17225 case DW_FORM_ref_addr:
17226 fprintf_unfiltered (f, "ref address: ");
17227 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17228 break;
17229 case DW_FORM_GNU_ref_alt:
17230 fprintf_unfiltered (f, "alt ref address: ");
17231 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17232 break;
17233 case DW_FORM_ref1:
17234 case DW_FORM_ref2:
17235 case DW_FORM_ref4:
17236 case DW_FORM_ref8:
17237 case DW_FORM_ref_udata:
17238 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
17239 (long) (DW_UNSND (&die->attrs[i])));
17240 break;
17241 case DW_FORM_data1:
17242 case DW_FORM_data2:
17243 case DW_FORM_data4:
17244 case DW_FORM_data8:
17245 case DW_FORM_udata:
17246 case DW_FORM_sdata:
17247 fprintf_unfiltered (f, "constant: %s",
17248 pulongest (DW_UNSND (&die->attrs[i])));
17249 break;
17250 case DW_FORM_sec_offset:
17251 fprintf_unfiltered (f, "section offset: %s",
17252 pulongest (DW_UNSND (&die->attrs[i])));
17253 break;
17254 case DW_FORM_ref_sig8:
17255 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
17256 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
17257 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
17258 else
17259 fprintf_unfiltered (f, "signatured type, offset: unknown");
17260 break;
17261 case DW_FORM_string:
17262 case DW_FORM_strp:
17263 case DW_FORM_GNU_str_index:
17264 case DW_FORM_GNU_strp_alt:
17265 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
17266 DW_STRING (&die->attrs[i])
17267 ? DW_STRING (&die->attrs[i]) : "",
17268 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
17269 break;
17270 case DW_FORM_flag:
17271 if (DW_UNSND (&die->attrs[i]))
17272 fprintf_unfiltered (f, "flag: TRUE");
17273 else
17274 fprintf_unfiltered (f, "flag: FALSE");
17275 break;
17276 case DW_FORM_flag_present:
17277 fprintf_unfiltered (f, "flag: TRUE");
17278 break;
17279 case DW_FORM_indirect:
17280 /* The reader will have reduced the indirect form to
17281 the "base form" so this form should not occur. */
17282 fprintf_unfiltered (f,
17283 "unexpected attribute form: DW_FORM_indirect");
17284 break;
17285 default:
17286 fprintf_unfiltered (f, "unsupported attribute form: %d.",
17287 die->attrs[i].form);
17288 break;
17289 }
17290 fprintf_unfiltered (f, "\n");
17291 }
17292 }
17293
17294 static void
17295 dump_die_for_error (struct die_info *die)
17296 {
17297 dump_die_shallow (gdb_stderr, 0, die);
17298 }
17299
17300 static void
17301 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
17302 {
17303 int indent = level * 4;
17304
17305 gdb_assert (die != NULL);
17306
17307 if (level >= max_level)
17308 return;
17309
17310 dump_die_shallow (f, indent, die);
17311
17312 if (die->child != NULL)
17313 {
17314 print_spaces (indent, f);
17315 fprintf_unfiltered (f, " Children:");
17316 if (level + 1 < max_level)
17317 {
17318 fprintf_unfiltered (f, "\n");
17319 dump_die_1 (f, level + 1, max_level, die->child);
17320 }
17321 else
17322 {
17323 fprintf_unfiltered (f,
17324 " [not printed, max nesting level reached]\n");
17325 }
17326 }
17327
17328 if (die->sibling != NULL && level > 0)
17329 {
17330 dump_die_1 (f, level, max_level, die->sibling);
17331 }
17332 }
17333
17334 /* This is called from the pdie macro in gdbinit.in.
17335 It's not static so gcc will keep a copy callable from gdb. */
17336
17337 void
17338 dump_die (struct die_info *die, int max_level)
17339 {
17340 dump_die_1 (gdb_stdlog, 0, max_level, die);
17341 }
17342
17343 static void
17344 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
17345 {
17346 void **slot;
17347
17348 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
17349 INSERT);
17350
17351 *slot = die;
17352 }
17353
17354 /* DW_ADDR is always stored already as sect_offset; despite for the forms
17355 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
17356
17357 static int
17358 is_ref_attr (struct attribute *attr)
17359 {
17360 switch (attr->form)
17361 {
17362 case DW_FORM_ref_addr:
17363 case DW_FORM_ref1:
17364 case DW_FORM_ref2:
17365 case DW_FORM_ref4:
17366 case DW_FORM_ref8:
17367 case DW_FORM_ref_udata:
17368 case DW_FORM_GNU_ref_alt:
17369 return 1;
17370 default:
17371 return 0;
17372 }
17373 }
17374
17375 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
17376 required kind. */
17377
17378 static sect_offset
17379 dwarf2_get_ref_die_offset (struct attribute *attr)
17380 {
17381 sect_offset retval = { DW_UNSND (attr) };
17382
17383 if (is_ref_attr (attr))
17384 return retval;
17385
17386 retval.sect_off = 0;
17387 complaint (&symfile_complaints,
17388 _("unsupported die ref attribute form: '%s'"),
17389 dwarf_form_name (attr->form));
17390 return retval;
17391 }
17392
17393 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
17394 * the value held by the attribute is not constant. */
17395
17396 static LONGEST
17397 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
17398 {
17399 if (attr->form == DW_FORM_sdata)
17400 return DW_SND (attr);
17401 else if (attr->form == DW_FORM_udata
17402 || attr->form == DW_FORM_data1
17403 || attr->form == DW_FORM_data2
17404 || attr->form == DW_FORM_data4
17405 || attr->form == DW_FORM_data8)
17406 return DW_UNSND (attr);
17407 else
17408 {
17409 complaint (&symfile_complaints,
17410 _("Attribute value is not a constant (%s)"),
17411 dwarf_form_name (attr->form));
17412 return default_value;
17413 }
17414 }
17415
17416 /* Follow reference or signature attribute ATTR of SRC_DIE.
17417 On entry *REF_CU is the CU of SRC_DIE.
17418 On exit *REF_CU is the CU of the result. */
17419
17420 static struct die_info *
17421 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
17422 struct dwarf2_cu **ref_cu)
17423 {
17424 struct die_info *die;
17425
17426 if (is_ref_attr (attr))
17427 die = follow_die_ref (src_die, attr, ref_cu);
17428 else if (attr->form == DW_FORM_ref_sig8)
17429 die = follow_die_sig (src_die, attr, ref_cu);
17430 else
17431 {
17432 dump_die_for_error (src_die);
17433 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
17434 (*ref_cu)->objfile->name);
17435 }
17436
17437 return die;
17438 }
17439
17440 /* Follow reference OFFSET.
17441 On entry *REF_CU is the CU of the source die referencing OFFSET.
17442 On exit *REF_CU is the CU of the result.
17443 Returns NULL if OFFSET is invalid. */
17444
17445 static struct die_info *
17446 follow_die_offset (sect_offset offset, int offset_in_dwz,
17447 struct dwarf2_cu **ref_cu)
17448 {
17449 struct die_info temp_die;
17450 struct dwarf2_cu *target_cu, *cu = *ref_cu;
17451
17452 gdb_assert (cu->per_cu != NULL);
17453
17454 target_cu = cu;
17455
17456 if (cu->per_cu->is_debug_types)
17457 {
17458 /* .debug_types CUs cannot reference anything outside their CU.
17459 If they need to, they have to reference a signatured type via
17460 DW_FORM_ref_sig8. */
17461 if (! offset_in_cu_p (&cu->header, offset))
17462 return NULL;
17463 }
17464 else if (offset_in_dwz != cu->per_cu->is_dwz
17465 || ! offset_in_cu_p (&cu->header, offset))
17466 {
17467 struct dwarf2_per_cu_data *per_cu;
17468
17469 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
17470 cu->objfile);
17471
17472 /* If necessary, add it to the queue and load its DIEs. */
17473 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
17474 load_full_comp_unit (per_cu, cu->language);
17475
17476 target_cu = per_cu->cu;
17477 }
17478 else if (cu->dies == NULL)
17479 {
17480 /* We're loading full DIEs during partial symbol reading. */
17481 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
17482 load_full_comp_unit (cu->per_cu, language_minimal);
17483 }
17484
17485 *ref_cu = target_cu;
17486 temp_die.offset = offset;
17487 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
17488 }
17489
17490 /* Follow reference attribute ATTR of SRC_DIE.
17491 On entry *REF_CU is the CU of SRC_DIE.
17492 On exit *REF_CU is the CU of the result. */
17493
17494 static struct die_info *
17495 follow_die_ref (struct die_info *src_die, struct attribute *attr,
17496 struct dwarf2_cu **ref_cu)
17497 {
17498 sect_offset offset = dwarf2_get_ref_die_offset (attr);
17499 struct dwarf2_cu *cu = *ref_cu;
17500 struct die_info *die;
17501
17502 die = follow_die_offset (offset,
17503 (attr->form == DW_FORM_GNU_ref_alt
17504 || cu->per_cu->is_dwz),
17505 ref_cu);
17506 if (!die)
17507 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
17508 "at 0x%x [in module %s]"),
17509 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
17510
17511 return die;
17512 }
17513
17514 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
17515 Returned value is intended for DW_OP_call*. Returned
17516 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
17517
17518 struct dwarf2_locexpr_baton
17519 dwarf2_fetch_die_loc_sect_off (sect_offset offset,
17520 struct dwarf2_per_cu_data *per_cu,
17521 CORE_ADDR (*get_frame_pc) (void *baton),
17522 void *baton)
17523 {
17524 struct dwarf2_cu *cu;
17525 struct die_info *die;
17526 struct attribute *attr;
17527 struct dwarf2_locexpr_baton retval;
17528
17529 dw2_setup (per_cu->objfile);
17530
17531 if (per_cu->cu == NULL)
17532 load_cu (per_cu);
17533 cu = per_cu->cu;
17534
17535 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
17536 if (!die)
17537 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
17538 offset.sect_off, per_cu->objfile->name);
17539
17540 attr = dwarf2_attr (die, DW_AT_location, cu);
17541 if (!attr)
17542 {
17543 /* DWARF: "If there is no such attribute, then there is no effect.".
17544 DATA is ignored if SIZE is 0. */
17545
17546 retval.data = NULL;
17547 retval.size = 0;
17548 }
17549 else if (attr_form_is_section_offset (attr))
17550 {
17551 struct dwarf2_loclist_baton loclist_baton;
17552 CORE_ADDR pc = (*get_frame_pc) (baton);
17553 size_t size;
17554
17555 fill_in_loclist_baton (cu, &loclist_baton, attr);
17556
17557 retval.data = dwarf2_find_location_expression (&loclist_baton,
17558 &size, pc);
17559 retval.size = size;
17560 }
17561 else
17562 {
17563 if (!attr_form_is_block (attr))
17564 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
17565 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
17566 offset.sect_off, per_cu->objfile->name);
17567
17568 retval.data = DW_BLOCK (attr)->data;
17569 retval.size = DW_BLOCK (attr)->size;
17570 }
17571 retval.per_cu = cu->per_cu;
17572
17573 age_cached_comp_units ();
17574
17575 return retval;
17576 }
17577
17578 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
17579 offset. */
17580
17581 struct dwarf2_locexpr_baton
17582 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
17583 struct dwarf2_per_cu_data *per_cu,
17584 CORE_ADDR (*get_frame_pc) (void *baton),
17585 void *baton)
17586 {
17587 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
17588
17589 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
17590 }
17591
17592 /* Return the type of the DIE at DIE_OFFSET in the CU named by
17593 PER_CU. */
17594
17595 struct type *
17596 dwarf2_get_die_type (cu_offset die_offset,
17597 struct dwarf2_per_cu_data *per_cu)
17598 {
17599 sect_offset die_offset_sect;
17600
17601 dw2_setup (per_cu->objfile);
17602
17603 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
17604 return get_die_type_at_offset (die_offset_sect, per_cu);
17605 }
17606
17607 /* Follow the signature attribute ATTR in SRC_DIE.
17608 On entry *REF_CU is the CU of SRC_DIE.
17609 On exit *REF_CU is the CU of the result. */
17610
17611 static struct die_info *
17612 follow_die_sig (struct die_info *src_die, struct attribute *attr,
17613 struct dwarf2_cu **ref_cu)
17614 {
17615 struct objfile *objfile = (*ref_cu)->objfile;
17616 struct die_info temp_die;
17617 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
17618 struct dwarf2_cu *sig_cu;
17619 struct die_info *die;
17620
17621 /* sig_type will be NULL if the signatured type is missing from
17622 the debug info. */
17623 if (sig_type == NULL)
17624 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
17625 "at 0x%x [in module %s]"),
17626 src_die->offset.sect_off, objfile->name);
17627
17628 /* If necessary, add it to the queue and load its DIEs. */
17629
17630 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
17631 read_signatured_type (sig_type);
17632
17633 gdb_assert (sig_type->per_cu.cu != NULL);
17634
17635 sig_cu = sig_type->per_cu.cu;
17636 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
17637 temp_die.offset = sig_type->type_offset_in_section;
17638 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
17639 temp_die.offset.sect_off);
17640 if (die)
17641 {
17642 /* For .gdb_index version 7 keep track of included TUs.
17643 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
17644 if (dwarf2_per_objfile->index_table != NULL
17645 && dwarf2_per_objfile->index_table->version <= 7)
17646 {
17647 VEC_safe_push (dwarf2_per_cu_ptr,
17648 (*ref_cu)->per_cu->imported_symtabs,
17649 sig_cu->per_cu);
17650 }
17651
17652 *ref_cu = sig_cu;
17653 return die;
17654 }
17655
17656 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
17657 "from DIE at 0x%x [in module %s]"),
17658 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
17659 }
17660
17661 /* Given an offset of a signatured type, return its signatured_type. */
17662
17663 static struct signatured_type *
17664 lookup_signatured_type_at_offset (struct objfile *objfile,
17665 struct dwarf2_section_info *section,
17666 sect_offset offset)
17667 {
17668 gdb_byte *info_ptr = section->buffer + offset.sect_off;
17669 unsigned int length, initial_length_size;
17670 unsigned int sig_offset;
17671 struct signatured_type find_entry, *sig_type;
17672
17673 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
17674 sig_offset = (initial_length_size
17675 + 2 /*version*/
17676 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
17677 + 1 /*address_size*/);
17678 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
17679 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
17680
17681 /* This is only used to lookup previously recorded types.
17682 If we didn't find it, it's our bug. */
17683 gdb_assert (sig_type != NULL);
17684 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
17685
17686 return sig_type;
17687 }
17688
17689 /* Load the DIEs associated with type unit PER_CU into memory. */
17690
17691 static void
17692 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
17693 {
17694 struct signatured_type *sig_type;
17695
17696 /* Caller is responsible for ensuring type_unit_groups don't get here. */
17697 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
17698
17699 /* We have the per_cu, but we need the signatured_type.
17700 Fortunately this is an easy translation. */
17701 gdb_assert (per_cu->is_debug_types);
17702 sig_type = (struct signatured_type *) per_cu;
17703
17704 gdb_assert (per_cu->cu == NULL);
17705
17706 read_signatured_type (sig_type);
17707
17708 gdb_assert (per_cu->cu != NULL);
17709 }
17710
17711 /* die_reader_func for read_signatured_type.
17712 This is identical to load_full_comp_unit_reader,
17713 but is kept separate for now. */
17714
17715 static void
17716 read_signatured_type_reader (const struct die_reader_specs *reader,
17717 gdb_byte *info_ptr,
17718 struct die_info *comp_unit_die,
17719 int has_children,
17720 void *data)
17721 {
17722 struct dwarf2_cu *cu = reader->cu;
17723
17724 gdb_assert (cu->die_hash == NULL);
17725 cu->die_hash =
17726 htab_create_alloc_ex (cu->header.length / 12,
17727 die_hash,
17728 die_eq,
17729 NULL,
17730 &cu->comp_unit_obstack,
17731 hashtab_obstack_allocate,
17732 dummy_obstack_deallocate);
17733
17734 if (has_children)
17735 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
17736 &info_ptr, comp_unit_die);
17737 cu->dies = comp_unit_die;
17738 /* comp_unit_die is not stored in die_hash, no need. */
17739
17740 /* We try not to read any attributes in this function, because not
17741 all CUs needed for references have been loaded yet, and symbol
17742 table processing isn't initialized. But we have to set the CU language,
17743 or we won't be able to build types correctly.
17744 Similarly, if we do not read the producer, we can not apply
17745 producer-specific interpretation. */
17746 prepare_one_comp_unit (cu, cu->dies, language_minimal);
17747 }
17748
17749 /* Read in a signatured type and build its CU and DIEs.
17750 If the type is a stub for the real type in a DWO file,
17751 read in the real type from the DWO file as well. */
17752
17753 static void
17754 read_signatured_type (struct signatured_type *sig_type)
17755 {
17756 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
17757
17758 gdb_assert (per_cu->is_debug_types);
17759 gdb_assert (per_cu->cu == NULL);
17760
17761 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
17762 read_signatured_type_reader, NULL);
17763 }
17764
17765 /* Decode simple location descriptions.
17766 Given a pointer to a dwarf block that defines a location, compute
17767 the location and return the value.
17768
17769 NOTE drow/2003-11-18: This function is called in two situations
17770 now: for the address of static or global variables (partial symbols
17771 only) and for offsets into structures which are expected to be
17772 (more or less) constant. The partial symbol case should go away,
17773 and only the constant case should remain. That will let this
17774 function complain more accurately. A few special modes are allowed
17775 without complaint for global variables (for instance, global
17776 register values and thread-local values).
17777
17778 A location description containing no operations indicates that the
17779 object is optimized out. The return value is 0 for that case.
17780 FIXME drow/2003-11-16: No callers check for this case any more; soon all
17781 callers will only want a very basic result and this can become a
17782 complaint.
17783
17784 Note that stack[0] is unused except as a default error return. */
17785
17786 static CORE_ADDR
17787 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
17788 {
17789 struct objfile *objfile = cu->objfile;
17790 size_t i;
17791 size_t size = blk->size;
17792 gdb_byte *data = blk->data;
17793 CORE_ADDR stack[64];
17794 int stacki;
17795 unsigned int bytes_read, unsnd;
17796 gdb_byte op;
17797
17798 i = 0;
17799 stacki = 0;
17800 stack[stacki] = 0;
17801 stack[++stacki] = 0;
17802
17803 while (i < size)
17804 {
17805 op = data[i++];
17806 switch (op)
17807 {
17808 case DW_OP_lit0:
17809 case DW_OP_lit1:
17810 case DW_OP_lit2:
17811 case DW_OP_lit3:
17812 case DW_OP_lit4:
17813 case DW_OP_lit5:
17814 case DW_OP_lit6:
17815 case DW_OP_lit7:
17816 case DW_OP_lit8:
17817 case DW_OP_lit9:
17818 case DW_OP_lit10:
17819 case DW_OP_lit11:
17820 case DW_OP_lit12:
17821 case DW_OP_lit13:
17822 case DW_OP_lit14:
17823 case DW_OP_lit15:
17824 case DW_OP_lit16:
17825 case DW_OP_lit17:
17826 case DW_OP_lit18:
17827 case DW_OP_lit19:
17828 case DW_OP_lit20:
17829 case DW_OP_lit21:
17830 case DW_OP_lit22:
17831 case DW_OP_lit23:
17832 case DW_OP_lit24:
17833 case DW_OP_lit25:
17834 case DW_OP_lit26:
17835 case DW_OP_lit27:
17836 case DW_OP_lit28:
17837 case DW_OP_lit29:
17838 case DW_OP_lit30:
17839 case DW_OP_lit31:
17840 stack[++stacki] = op - DW_OP_lit0;
17841 break;
17842
17843 case DW_OP_reg0:
17844 case DW_OP_reg1:
17845 case DW_OP_reg2:
17846 case DW_OP_reg3:
17847 case DW_OP_reg4:
17848 case DW_OP_reg5:
17849 case DW_OP_reg6:
17850 case DW_OP_reg7:
17851 case DW_OP_reg8:
17852 case DW_OP_reg9:
17853 case DW_OP_reg10:
17854 case DW_OP_reg11:
17855 case DW_OP_reg12:
17856 case DW_OP_reg13:
17857 case DW_OP_reg14:
17858 case DW_OP_reg15:
17859 case DW_OP_reg16:
17860 case DW_OP_reg17:
17861 case DW_OP_reg18:
17862 case DW_OP_reg19:
17863 case DW_OP_reg20:
17864 case DW_OP_reg21:
17865 case DW_OP_reg22:
17866 case DW_OP_reg23:
17867 case DW_OP_reg24:
17868 case DW_OP_reg25:
17869 case DW_OP_reg26:
17870 case DW_OP_reg27:
17871 case DW_OP_reg28:
17872 case DW_OP_reg29:
17873 case DW_OP_reg30:
17874 case DW_OP_reg31:
17875 stack[++stacki] = op - DW_OP_reg0;
17876 if (i < size)
17877 dwarf2_complex_location_expr_complaint ();
17878 break;
17879
17880 case DW_OP_regx:
17881 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
17882 i += bytes_read;
17883 stack[++stacki] = unsnd;
17884 if (i < size)
17885 dwarf2_complex_location_expr_complaint ();
17886 break;
17887
17888 case DW_OP_addr:
17889 stack[++stacki] = read_address (objfile->obfd, &data[i],
17890 cu, &bytes_read);
17891 i += bytes_read;
17892 break;
17893
17894 case DW_OP_const1u:
17895 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
17896 i += 1;
17897 break;
17898
17899 case DW_OP_const1s:
17900 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
17901 i += 1;
17902 break;
17903
17904 case DW_OP_const2u:
17905 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
17906 i += 2;
17907 break;
17908
17909 case DW_OP_const2s:
17910 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
17911 i += 2;
17912 break;
17913
17914 case DW_OP_const4u:
17915 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
17916 i += 4;
17917 break;
17918
17919 case DW_OP_const4s:
17920 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
17921 i += 4;
17922 break;
17923
17924 case DW_OP_const8u:
17925 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
17926 i += 8;
17927 break;
17928
17929 case DW_OP_constu:
17930 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
17931 &bytes_read);
17932 i += bytes_read;
17933 break;
17934
17935 case DW_OP_consts:
17936 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
17937 i += bytes_read;
17938 break;
17939
17940 case DW_OP_dup:
17941 stack[stacki + 1] = stack[stacki];
17942 stacki++;
17943 break;
17944
17945 case DW_OP_plus:
17946 stack[stacki - 1] += stack[stacki];
17947 stacki--;
17948 break;
17949
17950 case DW_OP_plus_uconst:
17951 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
17952 &bytes_read);
17953 i += bytes_read;
17954 break;
17955
17956 case DW_OP_minus:
17957 stack[stacki - 1] -= stack[stacki];
17958 stacki--;
17959 break;
17960
17961 case DW_OP_deref:
17962 /* If we're not the last op, then we definitely can't encode
17963 this using GDB's address_class enum. This is valid for partial
17964 global symbols, although the variable's address will be bogus
17965 in the psymtab. */
17966 if (i < size)
17967 dwarf2_complex_location_expr_complaint ();
17968 break;
17969
17970 case DW_OP_GNU_push_tls_address:
17971 /* The top of the stack has the offset from the beginning
17972 of the thread control block at which the variable is located. */
17973 /* Nothing should follow this operator, so the top of stack would
17974 be returned. */
17975 /* This is valid for partial global symbols, but the variable's
17976 address will be bogus in the psymtab. Make it always at least
17977 non-zero to not look as a variable garbage collected by linker
17978 which have DW_OP_addr 0. */
17979 if (i < size)
17980 dwarf2_complex_location_expr_complaint ();
17981 stack[stacki]++;
17982 break;
17983
17984 case DW_OP_GNU_uninit:
17985 break;
17986
17987 case DW_OP_GNU_addr_index:
17988 case DW_OP_GNU_const_index:
17989 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
17990 &bytes_read);
17991 i += bytes_read;
17992 break;
17993
17994 default:
17995 {
17996 const char *name = get_DW_OP_name (op);
17997
17998 if (name)
17999 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
18000 name);
18001 else
18002 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
18003 op);
18004 }
18005
18006 return (stack[stacki]);
18007 }
18008
18009 /* Enforce maximum stack depth of SIZE-1 to avoid writing
18010 outside of the allocated space. Also enforce minimum>0. */
18011 if (stacki >= ARRAY_SIZE (stack) - 1)
18012 {
18013 complaint (&symfile_complaints,
18014 _("location description stack overflow"));
18015 return 0;
18016 }
18017
18018 if (stacki <= 0)
18019 {
18020 complaint (&symfile_complaints,
18021 _("location description stack underflow"));
18022 return 0;
18023 }
18024 }
18025 return (stack[stacki]);
18026 }
18027
18028 /* memory allocation interface */
18029
18030 static struct dwarf_block *
18031 dwarf_alloc_block (struct dwarf2_cu *cu)
18032 {
18033 struct dwarf_block *blk;
18034
18035 blk = (struct dwarf_block *)
18036 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
18037 return (blk);
18038 }
18039
18040 static struct die_info *
18041 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
18042 {
18043 struct die_info *die;
18044 size_t size = sizeof (struct die_info);
18045
18046 if (num_attrs > 1)
18047 size += (num_attrs - 1) * sizeof (struct attribute);
18048
18049 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
18050 memset (die, 0, sizeof (struct die_info));
18051 return (die);
18052 }
18053
18054 \f
18055 /* Macro support. */
18056
18057 /* Return file name relative to the compilation directory of file number I in
18058 *LH's file name table. The result is allocated using xmalloc; the caller is
18059 responsible for freeing it. */
18060
18061 static char *
18062 file_file_name (int file, struct line_header *lh)
18063 {
18064 /* Is the file number a valid index into the line header's file name
18065 table? Remember that file numbers start with one, not zero. */
18066 if (1 <= file && file <= lh->num_file_names)
18067 {
18068 struct file_entry *fe = &lh->file_names[file - 1];
18069
18070 if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0)
18071 return xstrdup (fe->name);
18072 return concat (lh->include_dirs[fe->dir_index - 1], SLASH_STRING,
18073 fe->name, NULL);
18074 }
18075 else
18076 {
18077 /* The compiler produced a bogus file number. We can at least
18078 record the macro definitions made in the file, even if we
18079 won't be able to find the file by name. */
18080 char fake_name[80];
18081
18082 xsnprintf (fake_name, sizeof (fake_name),
18083 "<bad macro file number %d>", file);
18084
18085 complaint (&symfile_complaints,
18086 _("bad file number in macro information (%d)"),
18087 file);
18088
18089 return xstrdup (fake_name);
18090 }
18091 }
18092
18093 /* Return the full name of file number I in *LH's file name table.
18094 Use COMP_DIR as the name of the current directory of the
18095 compilation. The result is allocated using xmalloc; the caller is
18096 responsible for freeing it. */
18097 static char *
18098 file_full_name (int file, struct line_header *lh, const char *comp_dir)
18099 {
18100 /* Is the file number a valid index into the line header's file name
18101 table? Remember that file numbers start with one, not zero. */
18102 if (1 <= file && file <= lh->num_file_names)
18103 {
18104 char *relative = file_file_name (file, lh);
18105
18106 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
18107 return relative;
18108 return reconcat (relative, comp_dir, SLASH_STRING, relative, NULL);
18109 }
18110 else
18111 return file_file_name (file, lh);
18112 }
18113
18114
18115 static struct macro_source_file *
18116 macro_start_file (int file, int line,
18117 struct macro_source_file *current_file,
18118 const char *comp_dir,
18119 struct line_header *lh, struct objfile *objfile)
18120 {
18121 /* File name relative to the compilation directory of this source file. */
18122 char *file_name = file_file_name (file, lh);
18123
18124 /* We don't create a macro table for this compilation unit
18125 at all until we actually get a filename. */
18126 if (! pending_macros)
18127 pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
18128 objfile->per_bfd->macro_cache,
18129 comp_dir);
18130
18131 if (! current_file)
18132 {
18133 /* If we have no current file, then this must be the start_file
18134 directive for the compilation unit's main source file. */
18135 current_file = macro_set_main (pending_macros, file_name);
18136 macro_define_special (pending_macros);
18137 }
18138 else
18139 current_file = macro_include (current_file, line, file_name);
18140
18141 xfree (file_name);
18142
18143 return current_file;
18144 }
18145
18146
18147 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
18148 followed by a null byte. */
18149 static char *
18150 copy_string (const char *buf, int len)
18151 {
18152 char *s = xmalloc (len + 1);
18153
18154 memcpy (s, buf, len);
18155 s[len] = '\0';
18156 return s;
18157 }
18158
18159
18160 static const char *
18161 consume_improper_spaces (const char *p, const char *body)
18162 {
18163 if (*p == ' ')
18164 {
18165 complaint (&symfile_complaints,
18166 _("macro definition contains spaces "
18167 "in formal argument list:\n`%s'"),
18168 body);
18169
18170 while (*p == ' ')
18171 p++;
18172 }
18173
18174 return p;
18175 }
18176
18177
18178 static void
18179 parse_macro_definition (struct macro_source_file *file, int line,
18180 const char *body)
18181 {
18182 const char *p;
18183
18184 /* The body string takes one of two forms. For object-like macro
18185 definitions, it should be:
18186
18187 <macro name> " " <definition>
18188
18189 For function-like macro definitions, it should be:
18190
18191 <macro name> "() " <definition>
18192 or
18193 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
18194
18195 Spaces may appear only where explicitly indicated, and in the
18196 <definition>.
18197
18198 The Dwarf 2 spec says that an object-like macro's name is always
18199 followed by a space, but versions of GCC around March 2002 omit
18200 the space when the macro's definition is the empty string.
18201
18202 The Dwarf 2 spec says that there should be no spaces between the
18203 formal arguments in a function-like macro's formal argument list,
18204 but versions of GCC around March 2002 include spaces after the
18205 commas. */
18206
18207
18208 /* Find the extent of the macro name. The macro name is terminated
18209 by either a space or null character (for an object-like macro) or
18210 an opening paren (for a function-like macro). */
18211 for (p = body; *p; p++)
18212 if (*p == ' ' || *p == '(')
18213 break;
18214
18215 if (*p == ' ' || *p == '\0')
18216 {
18217 /* It's an object-like macro. */
18218 int name_len = p - body;
18219 char *name = copy_string (body, name_len);
18220 const char *replacement;
18221
18222 if (*p == ' ')
18223 replacement = body + name_len + 1;
18224 else
18225 {
18226 dwarf2_macro_malformed_definition_complaint (body);
18227 replacement = body + name_len;
18228 }
18229
18230 macro_define_object (file, line, name, replacement);
18231
18232 xfree (name);
18233 }
18234 else if (*p == '(')
18235 {
18236 /* It's a function-like macro. */
18237 char *name = copy_string (body, p - body);
18238 int argc = 0;
18239 int argv_size = 1;
18240 char **argv = xmalloc (argv_size * sizeof (*argv));
18241
18242 p++;
18243
18244 p = consume_improper_spaces (p, body);
18245
18246 /* Parse the formal argument list. */
18247 while (*p && *p != ')')
18248 {
18249 /* Find the extent of the current argument name. */
18250 const char *arg_start = p;
18251
18252 while (*p && *p != ',' && *p != ')' && *p != ' ')
18253 p++;
18254
18255 if (! *p || p == arg_start)
18256 dwarf2_macro_malformed_definition_complaint (body);
18257 else
18258 {
18259 /* Make sure argv has room for the new argument. */
18260 if (argc >= argv_size)
18261 {
18262 argv_size *= 2;
18263 argv = xrealloc (argv, argv_size * sizeof (*argv));
18264 }
18265
18266 argv[argc++] = copy_string (arg_start, p - arg_start);
18267 }
18268
18269 p = consume_improper_spaces (p, body);
18270
18271 /* Consume the comma, if present. */
18272 if (*p == ',')
18273 {
18274 p++;
18275
18276 p = consume_improper_spaces (p, body);
18277 }
18278 }
18279
18280 if (*p == ')')
18281 {
18282 p++;
18283
18284 if (*p == ' ')
18285 /* Perfectly formed definition, no complaints. */
18286 macro_define_function (file, line, name,
18287 argc, (const char **) argv,
18288 p + 1);
18289 else if (*p == '\0')
18290 {
18291 /* Complain, but do define it. */
18292 dwarf2_macro_malformed_definition_complaint (body);
18293 macro_define_function (file, line, name,
18294 argc, (const char **) argv,
18295 p);
18296 }
18297 else
18298 /* Just complain. */
18299 dwarf2_macro_malformed_definition_complaint (body);
18300 }
18301 else
18302 /* Just complain. */
18303 dwarf2_macro_malformed_definition_complaint (body);
18304
18305 xfree (name);
18306 {
18307 int i;
18308
18309 for (i = 0; i < argc; i++)
18310 xfree (argv[i]);
18311 }
18312 xfree (argv);
18313 }
18314 else
18315 dwarf2_macro_malformed_definition_complaint (body);
18316 }
18317
18318 /* Skip some bytes from BYTES according to the form given in FORM.
18319 Returns the new pointer. */
18320
18321 static gdb_byte *
18322 skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
18323 enum dwarf_form form,
18324 unsigned int offset_size,
18325 struct dwarf2_section_info *section)
18326 {
18327 unsigned int bytes_read;
18328
18329 switch (form)
18330 {
18331 case DW_FORM_data1:
18332 case DW_FORM_flag:
18333 ++bytes;
18334 break;
18335
18336 case DW_FORM_data2:
18337 bytes += 2;
18338 break;
18339
18340 case DW_FORM_data4:
18341 bytes += 4;
18342 break;
18343
18344 case DW_FORM_data8:
18345 bytes += 8;
18346 break;
18347
18348 case DW_FORM_string:
18349 read_direct_string (abfd, bytes, &bytes_read);
18350 bytes += bytes_read;
18351 break;
18352
18353 case DW_FORM_sec_offset:
18354 case DW_FORM_strp:
18355 case DW_FORM_GNU_strp_alt:
18356 bytes += offset_size;
18357 break;
18358
18359 case DW_FORM_block:
18360 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
18361 bytes += bytes_read;
18362 break;
18363
18364 case DW_FORM_block1:
18365 bytes += 1 + read_1_byte (abfd, bytes);
18366 break;
18367 case DW_FORM_block2:
18368 bytes += 2 + read_2_bytes (abfd, bytes);
18369 break;
18370 case DW_FORM_block4:
18371 bytes += 4 + read_4_bytes (abfd, bytes);
18372 break;
18373
18374 case DW_FORM_sdata:
18375 case DW_FORM_udata:
18376 case DW_FORM_GNU_addr_index:
18377 case DW_FORM_GNU_str_index:
18378 bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
18379 if (bytes == NULL)
18380 {
18381 dwarf2_section_buffer_overflow_complaint (section);
18382 return NULL;
18383 }
18384 break;
18385
18386 default:
18387 {
18388 complain:
18389 complaint (&symfile_complaints,
18390 _("invalid form 0x%x in `%s'"),
18391 form,
18392 section->asection->name);
18393 return NULL;
18394 }
18395 }
18396
18397 return bytes;
18398 }
18399
18400 /* A helper for dwarf_decode_macros that handles skipping an unknown
18401 opcode. Returns an updated pointer to the macro data buffer; or,
18402 on error, issues a complaint and returns NULL. */
18403
18404 static gdb_byte *
18405 skip_unknown_opcode (unsigned int opcode,
18406 gdb_byte **opcode_definitions,
18407 gdb_byte *mac_ptr, gdb_byte *mac_end,
18408 bfd *abfd,
18409 unsigned int offset_size,
18410 struct dwarf2_section_info *section)
18411 {
18412 unsigned int bytes_read, i;
18413 unsigned long arg;
18414 gdb_byte *defn;
18415
18416 if (opcode_definitions[opcode] == NULL)
18417 {
18418 complaint (&symfile_complaints,
18419 _("unrecognized DW_MACFINO opcode 0x%x"),
18420 opcode);
18421 return NULL;
18422 }
18423
18424 defn = opcode_definitions[opcode];
18425 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
18426 defn += bytes_read;
18427
18428 for (i = 0; i < arg; ++i)
18429 {
18430 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
18431 section);
18432 if (mac_ptr == NULL)
18433 {
18434 /* skip_form_bytes already issued the complaint. */
18435 return NULL;
18436 }
18437 }
18438
18439 return mac_ptr;
18440 }
18441
18442 /* A helper function which parses the header of a macro section.
18443 If the macro section is the extended (for now called "GNU") type,
18444 then this updates *OFFSET_SIZE. Returns a pointer to just after
18445 the header, or issues a complaint and returns NULL on error. */
18446
18447 static gdb_byte *
18448 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
18449 bfd *abfd,
18450 gdb_byte *mac_ptr,
18451 unsigned int *offset_size,
18452 int section_is_gnu)
18453 {
18454 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
18455
18456 if (section_is_gnu)
18457 {
18458 unsigned int version, flags;
18459
18460 version = read_2_bytes (abfd, mac_ptr);
18461 if (version != 4)
18462 {
18463 complaint (&symfile_complaints,
18464 _("unrecognized version `%d' in .debug_macro section"),
18465 version);
18466 return NULL;
18467 }
18468 mac_ptr += 2;
18469
18470 flags = read_1_byte (abfd, mac_ptr);
18471 ++mac_ptr;
18472 *offset_size = (flags & 1) ? 8 : 4;
18473
18474 if ((flags & 2) != 0)
18475 /* We don't need the line table offset. */
18476 mac_ptr += *offset_size;
18477
18478 /* Vendor opcode descriptions. */
18479 if ((flags & 4) != 0)
18480 {
18481 unsigned int i, count;
18482
18483 count = read_1_byte (abfd, mac_ptr);
18484 ++mac_ptr;
18485 for (i = 0; i < count; ++i)
18486 {
18487 unsigned int opcode, bytes_read;
18488 unsigned long arg;
18489
18490 opcode = read_1_byte (abfd, mac_ptr);
18491 ++mac_ptr;
18492 opcode_definitions[opcode] = mac_ptr;
18493 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18494 mac_ptr += bytes_read;
18495 mac_ptr += arg;
18496 }
18497 }
18498 }
18499
18500 return mac_ptr;
18501 }
18502
18503 /* A helper for dwarf_decode_macros that handles the GNU extensions,
18504 including DW_MACRO_GNU_transparent_include. */
18505
18506 static void
18507 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
18508 struct macro_source_file *current_file,
18509 struct line_header *lh, const char *comp_dir,
18510 struct dwarf2_section_info *section,
18511 int section_is_gnu, int section_is_dwz,
18512 unsigned int offset_size,
18513 struct objfile *objfile,
18514 htab_t include_hash)
18515 {
18516 enum dwarf_macro_record_type macinfo_type;
18517 int at_commandline;
18518 gdb_byte *opcode_definitions[256];
18519
18520 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18521 &offset_size, section_is_gnu);
18522 if (mac_ptr == NULL)
18523 {
18524 /* We already issued a complaint. */
18525 return;
18526 }
18527
18528 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
18529 GDB is still reading the definitions from command line. First
18530 DW_MACINFO_start_file will need to be ignored as it was already executed
18531 to create CURRENT_FILE for the main source holding also the command line
18532 definitions. On first met DW_MACINFO_start_file this flag is reset to
18533 normally execute all the remaining DW_MACINFO_start_file macinfos. */
18534
18535 at_commandline = 1;
18536
18537 do
18538 {
18539 /* Do we at least have room for a macinfo type byte? */
18540 if (mac_ptr >= mac_end)
18541 {
18542 dwarf2_section_buffer_overflow_complaint (section);
18543 break;
18544 }
18545
18546 macinfo_type = read_1_byte (abfd, mac_ptr);
18547 mac_ptr++;
18548
18549 /* Note that we rely on the fact that the corresponding GNU and
18550 DWARF constants are the same. */
18551 switch (macinfo_type)
18552 {
18553 /* A zero macinfo type indicates the end of the macro
18554 information. */
18555 case 0:
18556 break;
18557
18558 case DW_MACRO_GNU_define:
18559 case DW_MACRO_GNU_undef:
18560 case DW_MACRO_GNU_define_indirect:
18561 case DW_MACRO_GNU_undef_indirect:
18562 case DW_MACRO_GNU_define_indirect_alt:
18563 case DW_MACRO_GNU_undef_indirect_alt:
18564 {
18565 unsigned int bytes_read;
18566 int line;
18567 char *body;
18568 int is_define;
18569
18570 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18571 mac_ptr += bytes_read;
18572
18573 if (macinfo_type == DW_MACRO_GNU_define
18574 || macinfo_type == DW_MACRO_GNU_undef)
18575 {
18576 body = read_direct_string (abfd, mac_ptr, &bytes_read);
18577 mac_ptr += bytes_read;
18578 }
18579 else
18580 {
18581 LONGEST str_offset;
18582
18583 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
18584 mac_ptr += offset_size;
18585
18586 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
18587 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
18588 || section_is_dwz)
18589 {
18590 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18591
18592 body = read_indirect_string_from_dwz (dwz, str_offset);
18593 }
18594 else
18595 body = read_indirect_string_at_offset (abfd, str_offset);
18596 }
18597
18598 is_define = (macinfo_type == DW_MACRO_GNU_define
18599 || macinfo_type == DW_MACRO_GNU_define_indirect
18600 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
18601 if (! current_file)
18602 {
18603 /* DWARF violation as no main source is present. */
18604 complaint (&symfile_complaints,
18605 _("debug info with no main source gives macro %s "
18606 "on line %d: %s"),
18607 is_define ? _("definition") : _("undefinition"),
18608 line, body);
18609 break;
18610 }
18611 if ((line == 0 && !at_commandline)
18612 || (line != 0 && at_commandline))
18613 complaint (&symfile_complaints,
18614 _("debug info gives %s macro %s with %s line %d: %s"),
18615 at_commandline ? _("command-line") : _("in-file"),
18616 is_define ? _("definition") : _("undefinition"),
18617 line == 0 ? _("zero") : _("non-zero"), line, body);
18618
18619 if (is_define)
18620 parse_macro_definition (current_file, line, body);
18621 else
18622 {
18623 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
18624 || macinfo_type == DW_MACRO_GNU_undef_indirect
18625 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
18626 macro_undef (current_file, line, body);
18627 }
18628 }
18629 break;
18630
18631 case DW_MACRO_GNU_start_file:
18632 {
18633 unsigned int bytes_read;
18634 int line, file;
18635
18636 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18637 mac_ptr += bytes_read;
18638 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18639 mac_ptr += bytes_read;
18640
18641 if ((line == 0 && !at_commandline)
18642 || (line != 0 && at_commandline))
18643 complaint (&symfile_complaints,
18644 _("debug info gives source %d included "
18645 "from %s at %s line %d"),
18646 file, at_commandline ? _("command-line") : _("file"),
18647 line == 0 ? _("zero") : _("non-zero"), line);
18648
18649 if (at_commandline)
18650 {
18651 /* This DW_MACRO_GNU_start_file was executed in the
18652 pass one. */
18653 at_commandline = 0;
18654 }
18655 else
18656 current_file = macro_start_file (file, line,
18657 current_file, comp_dir,
18658 lh, objfile);
18659 }
18660 break;
18661
18662 case DW_MACRO_GNU_end_file:
18663 if (! current_file)
18664 complaint (&symfile_complaints,
18665 _("macro debug info has an unmatched "
18666 "`close_file' directive"));
18667 else
18668 {
18669 current_file = current_file->included_by;
18670 if (! current_file)
18671 {
18672 enum dwarf_macro_record_type next_type;
18673
18674 /* GCC circa March 2002 doesn't produce the zero
18675 type byte marking the end of the compilation
18676 unit. Complain if it's not there, but exit no
18677 matter what. */
18678
18679 /* Do we at least have room for a macinfo type byte? */
18680 if (mac_ptr >= mac_end)
18681 {
18682 dwarf2_section_buffer_overflow_complaint (section);
18683 return;
18684 }
18685
18686 /* We don't increment mac_ptr here, so this is just
18687 a look-ahead. */
18688 next_type = read_1_byte (abfd, mac_ptr);
18689 if (next_type != 0)
18690 complaint (&symfile_complaints,
18691 _("no terminating 0-type entry for "
18692 "macros in `.debug_macinfo' section"));
18693
18694 return;
18695 }
18696 }
18697 break;
18698
18699 case DW_MACRO_GNU_transparent_include:
18700 case DW_MACRO_GNU_transparent_include_alt:
18701 {
18702 LONGEST offset;
18703 void **slot;
18704 bfd *include_bfd = abfd;
18705 struct dwarf2_section_info *include_section = section;
18706 struct dwarf2_section_info alt_section;
18707 gdb_byte *include_mac_end = mac_end;
18708 int is_dwz = section_is_dwz;
18709 gdb_byte *new_mac_ptr;
18710
18711 offset = read_offset_1 (abfd, mac_ptr, offset_size);
18712 mac_ptr += offset_size;
18713
18714 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
18715 {
18716 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18717
18718 dwarf2_read_section (dwarf2_per_objfile->objfile,
18719 &dwz->macro);
18720
18721 include_bfd = dwz->macro.asection->owner;
18722 include_section = &dwz->macro;
18723 include_mac_end = dwz->macro.buffer + dwz->macro.size;
18724 is_dwz = 1;
18725 }
18726
18727 new_mac_ptr = include_section->buffer + offset;
18728 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
18729
18730 if (*slot != NULL)
18731 {
18732 /* This has actually happened; see
18733 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
18734 complaint (&symfile_complaints,
18735 _("recursive DW_MACRO_GNU_transparent_include in "
18736 ".debug_macro section"));
18737 }
18738 else
18739 {
18740 *slot = new_mac_ptr;
18741
18742 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
18743 include_mac_end, current_file,
18744 lh, comp_dir,
18745 section, section_is_gnu, is_dwz,
18746 offset_size, objfile, include_hash);
18747
18748 htab_remove_elt (include_hash, new_mac_ptr);
18749 }
18750 }
18751 break;
18752
18753 case DW_MACINFO_vendor_ext:
18754 if (!section_is_gnu)
18755 {
18756 unsigned int bytes_read;
18757 int constant;
18758
18759 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18760 mac_ptr += bytes_read;
18761 read_direct_string (abfd, mac_ptr, &bytes_read);
18762 mac_ptr += bytes_read;
18763
18764 /* We don't recognize any vendor extensions. */
18765 break;
18766 }
18767 /* FALLTHROUGH */
18768
18769 default:
18770 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
18771 mac_ptr, mac_end, abfd, offset_size,
18772 section);
18773 if (mac_ptr == NULL)
18774 return;
18775 break;
18776 }
18777 } while (macinfo_type != 0);
18778 }
18779
18780 static void
18781 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
18782 const char *comp_dir, int section_is_gnu)
18783 {
18784 struct objfile *objfile = dwarf2_per_objfile->objfile;
18785 struct line_header *lh = cu->line_header;
18786 bfd *abfd;
18787 gdb_byte *mac_ptr, *mac_end;
18788 struct macro_source_file *current_file = 0;
18789 enum dwarf_macro_record_type macinfo_type;
18790 unsigned int offset_size = cu->header.offset_size;
18791 gdb_byte *opcode_definitions[256];
18792 struct cleanup *cleanup;
18793 htab_t include_hash;
18794 void **slot;
18795 struct dwarf2_section_info *section;
18796 const char *section_name;
18797
18798 if (cu->dwo_unit != NULL)
18799 {
18800 if (section_is_gnu)
18801 {
18802 section = &cu->dwo_unit->dwo_file->sections.macro;
18803 section_name = ".debug_macro.dwo";
18804 }
18805 else
18806 {
18807 section = &cu->dwo_unit->dwo_file->sections.macinfo;
18808 section_name = ".debug_macinfo.dwo";
18809 }
18810 }
18811 else
18812 {
18813 if (section_is_gnu)
18814 {
18815 section = &dwarf2_per_objfile->macro;
18816 section_name = ".debug_macro";
18817 }
18818 else
18819 {
18820 section = &dwarf2_per_objfile->macinfo;
18821 section_name = ".debug_macinfo";
18822 }
18823 }
18824
18825 dwarf2_read_section (objfile, section);
18826 if (section->buffer == NULL)
18827 {
18828 complaint (&symfile_complaints, _("missing %s section"), section_name);
18829 return;
18830 }
18831 abfd = section->asection->owner;
18832
18833 /* First pass: Find the name of the base filename.
18834 This filename is needed in order to process all macros whose definition
18835 (or undefinition) comes from the command line. These macros are defined
18836 before the first DW_MACINFO_start_file entry, and yet still need to be
18837 associated to the base file.
18838
18839 To determine the base file name, we scan the macro definitions until we
18840 reach the first DW_MACINFO_start_file entry. We then initialize
18841 CURRENT_FILE accordingly so that any macro definition found before the
18842 first DW_MACINFO_start_file can still be associated to the base file. */
18843
18844 mac_ptr = section->buffer + offset;
18845 mac_end = section->buffer + section->size;
18846
18847 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18848 &offset_size, section_is_gnu);
18849 if (mac_ptr == NULL)
18850 {
18851 /* We already issued a complaint. */
18852 return;
18853 }
18854
18855 do
18856 {
18857 /* Do we at least have room for a macinfo type byte? */
18858 if (mac_ptr >= mac_end)
18859 {
18860 /* Complaint is printed during the second pass as GDB will probably
18861 stop the first pass earlier upon finding
18862 DW_MACINFO_start_file. */
18863 break;
18864 }
18865
18866 macinfo_type = read_1_byte (abfd, mac_ptr);
18867 mac_ptr++;
18868
18869 /* Note that we rely on the fact that the corresponding GNU and
18870 DWARF constants are the same. */
18871 switch (macinfo_type)
18872 {
18873 /* A zero macinfo type indicates the end of the macro
18874 information. */
18875 case 0:
18876 break;
18877
18878 case DW_MACRO_GNU_define:
18879 case DW_MACRO_GNU_undef:
18880 /* Only skip the data by MAC_PTR. */
18881 {
18882 unsigned int bytes_read;
18883
18884 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18885 mac_ptr += bytes_read;
18886 read_direct_string (abfd, mac_ptr, &bytes_read);
18887 mac_ptr += bytes_read;
18888 }
18889 break;
18890
18891 case DW_MACRO_GNU_start_file:
18892 {
18893 unsigned int bytes_read;
18894 int line, file;
18895
18896 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18897 mac_ptr += bytes_read;
18898 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18899 mac_ptr += bytes_read;
18900
18901 current_file = macro_start_file (file, line, current_file,
18902 comp_dir, lh, objfile);
18903 }
18904 break;
18905
18906 case DW_MACRO_GNU_end_file:
18907 /* No data to skip by MAC_PTR. */
18908 break;
18909
18910 case DW_MACRO_GNU_define_indirect:
18911 case DW_MACRO_GNU_undef_indirect:
18912 case DW_MACRO_GNU_define_indirect_alt:
18913 case DW_MACRO_GNU_undef_indirect_alt:
18914 {
18915 unsigned int bytes_read;
18916
18917 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18918 mac_ptr += bytes_read;
18919 mac_ptr += offset_size;
18920 }
18921 break;
18922
18923 case DW_MACRO_GNU_transparent_include:
18924 case DW_MACRO_GNU_transparent_include_alt:
18925 /* Note that, according to the spec, a transparent include
18926 chain cannot call DW_MACRO_GNU_start_file. So, we can just
18927 skip this opcode. */
18928 mac_ptr += offset_size;
18929 break;
18930
18931 case DW_MACINFO_vendor_ext:
18932 /* Only skip the data by MAC_PTR. */
18933 if (!section_is_gnu)
18934 {
18935 unsigned int bytes_read;
18936
18937 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18938 mac_ptr += bytes_read;
18939 read_direct_string (abfd, mac_ptr, &bytes_read);
18940 mac_ptr += bytes_read;
18941 }
18942 /* FALLTHROUGH */
18943
18944 default:
18945 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
18946 mac_ptr, mac_end, abfd, offset_size,
18947 section);
18948 if (mac_ptr == NULL)
18949 return;
18950 break;
18951 }
18952 } while (macinfo_type != 0 && current_file == NULL);
18953
18954 /* Second pass: Process all entries.
18955
18956 Use the AT_COMMAND_LINE flag to determine whether we are still processing
18957 command-line macro definitions/undefinitions. This flag is unset when we
18958 reach the first DW_MACINFO_start_file entry. */
18959
18960 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
18961 NULL, xcalloc, xfree);
18962 cleanup = make_cleanup_htab_delete (include_hash);
18963 mac_ptr = section->buffer + offset;
18964 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
18965 *slot = mac_ptr;
18966 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
18967 current_file, lh, comp_dir, section,
18968 section_is_gnu, 0,
18969 offset_size, objfile, include_hash);
18970 do_cleanups (cleanup);
18971 }
18972
18973 /* Check if the attribute's form is a DW_FORM_block*
18974 if so return true else false. */
18975
18976 static int
18977 attr_form_is_block (struct attribute *attr)
18978 {
18979 return (attr == NULL ? 0 :
18980 attr->form == DW_FORM_block1
18981 || attr->form == DW_FORM_block2
18982 || attr->form == DW_FORM_block4
18983 || attr->form == DW_FORM_block
18984 || attr->form == DW_FORM_exprloc);
18985 }
18986
18987 /* Return non-zero if ATTR's value is a section offset --- classes
18988 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
18989 You may use DW_UNSND (attr) to retrieve such offsets.
18990
18991 Section 7.5.4, "Attribute Encodings", explains that no attribute
18992 may have a value that belongs to more than one of these classes; it
18993 would be ambiguous if we did, because we use the same forms for all
18994 of them. */
18995
18996 static int
18997 attr_form_is_section_offset (struct attribute *attr)
18998 {
18999 return (attr->form == DW_FORM_data4
19000 || attr->form == DW_FORM_data8
19001 || attr->form == DW_FORM_sec_offset);
19002 }
19003
19004 /* Return non-zero if ATTR's value falls in the 'constant' class, or
19005 zero otherwise. When this function returns true, you can apply
19006 dwarf2_get_attr_constant_value to it.
19007
19008 However, note that for some attributes you must check
19009 attr_form_is_section_offset before using this test. DW_FORM_data4
19010 and DW_FORM_data8 are members of both the constant class, and of
19011 the classes that contain offsets into other debug sections
19012 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
19013 that, if an attribute's can be either a constant or one of the
19014 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
19015 taken as section offsets, not constants. */
19016
19017 static int
19018 attr_form_is_constant (struct attribute *attr)
19019 {
19020 switch (attr->form)
19021 {
19022 case DW_FORM_sdata:
19023 case DW_FORM_udata:
19024 case DW_FORM_data1:
19025 case DW_FORM_data2:
19026 case DW_FORM_data4:
19027 case DW_FORM_data8:
19028 return 1;
19029 default:
19030 return 0;
19031 }
19032 }
19033
19034 /* Return the .debug_loc section to use for CU.
19035 For DWO files use .debug_loc.dwo. */
19036
19037 static struct dwarf2_section_info *
19038 cu_debug_loc_section (struct dwarf2_cu *cu)
19039 {
19040 if (cu->dwo_unit)
19041 return &cu->dwo_unit->dwo_file->sections.loc;
19042 return &dwarf2_per_objfile->loc;
19043 }
19044
19045 /* A helper function that fills in a dwarf2_loclist_baton. */
19046
19047 static void
19048 fill_in_loclist_baton (struct dwarf2_cu *cu,
19049 struct dwarf2_loclist_baton *baton,
19050 struct attribute *attr)
19051 {
19052 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19053
19054 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
19055
19056 baton->per_cu = cu->per_cu;
19057 gdb_assert (baton->per_cu);
19058 /* We don't know how long the location list is, but make sure we
19059 don't run off the edge of the section. */
19060 baton->size = section->size - DW_UNSND (attr);
19061 baton->data = section->buffer + DW_UNSND (attr);
19062 baton->base_address = cu->base_address;
19063 baton->from_dwo = cu->dwo_unit != NULL;
19064 }
19065
19066 static void
19067 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
19068 struct dwarf2_cu *cu, int is_block)
19069 {
19070 struct objfile *objfile = dwarf2_per_objfile->objfile;
19071 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19072
19073 if (attr_form_is_section_offset (attr)
19074 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
19075 the section. If so, fall through to the complaint in the
19076 other branch. */
19077 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
19078 {
19079 struct dwarf2_loclist_baton *baton;
19080
19081 baton = obstack_alloc (&objfile->objfile_obstack,
19082 sizeof (struct dwarf2_loclist_baton));
19083
19084 fill_in_loclist_baton (cu, baton, attr);
19085
19086 if (cu->base_known == 0)
19087 complaint (&symfile_complaints,
19088 _("Location list used without "
19089 "specifying the CU base address."));
19090
19091 SYMBOL_ACLASS_INDEX (sym) = (is_block
19092 ? dwarf2_loclist_block_index
19093 : dwarf2_loclist_index);
19094 SYMBOL_LOCATION_BATON (sym) = baton;
19095 }
19096 else
19097 {
19098 struct dwarf2_locexpr_baton *baton;
19099
19100 baton = obstack_alloc (&objfile->objfile_obstack,
19101 sizeof (struct dwarf2_locexpr_baton));
19102 baton->per_cu = cu->per_cu;
19103 gdb_assert (baton->per_cu);
19104
19105 if (attr_form_is_block (attr))
19106 {
19107 /* Note that we're just copying the block's data pointer
19108 here, not the actual data. We're still pointing into the
19109 info_buffer for SYM's objfile; right now we never release
19110 that buffer, but when we do clean up properly this may
19111 need to change. */
19112 baton->size = DW_BLOCK (attr)->size;
19113 baton->data = DW_BLOCK (attr)->data;
19114 }
19115 else
19116 {
19117 dwarf2_invalid_attrib_class_complaint ("location description",
19118 SYMBOL_NATURAL_NAME (sym));
19119 baton->size = 0;
19120 }
19121
19122 SYMBOL_ACLASS_INDEX (sym) = (is_block
19123 ? dwarf2_locexpr_block_index
19124 : dwarf2_locexpr_index);
19125 SYMBOL_LOCATION_BATON (sym) = baton;
19126 }
19127 }
19128
19129 /* Return the OBJFILE associated with the compilation unit CU. If CU
19130 came from a separate debuginfo file, then the master objfile is
19131 returned. */
19132
19133 struct objfile *
19134 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
19135 {
19136 struct objfile *objfile = per_cu->objfile;
19137
19138 /* Return the master objfile, so that we can report and look up the
19139 correct file containing this variable. */
19140 if (objfile->separate_debug_objfile_backlink)
19141 objfile = objfile->separate_debug_objfile_backlink;
19142
19143 return objfile;
19144 }
19145
19146 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
19147 (CU_HEADERP is unused in such case) or prepare a temporary copy at
19148 CU_HEADERP first. */
19149
19150 static const struct comp_unit_head *
19151 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
19152 struct dwarf2_per_cu_data *per_cu)
19153 {
19154 gdb_byte *info_ptr;
19155
19156 if (per_cu->cu)
19157 return &per_cu->cu->header;
19158
19159 info_ptr = per_cu->info_or_types_section->buffer + per_cu->offset.sect_off;
19160
19161 memset (cu_headerp, 0, sizeof (*cu_headerp));
19162 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
19163
19164 return cu_headerp;
19165 }
19166
19167 /* Return the address size given in the compilation unit header for CU. */
19168
19169 int
19170 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
19171 {
19172 struct comp_unit_head cu_header_local;
19173 const struct comp_unit_head *cu_headerp;
19174
19175 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19176
19177 return cu_headerp->addr_size;
19178 }
19179
19180 /* Return the offset size given in the compilation unit header for CU. */
19181
19182 int
19183 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
19184 {
19185 struct comp_unit_head cu_header_local;
19186 const struct comp_unit_head *cu_headerp;
19187
19188 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19189
19190 return cu_headerp->offset_size;
19191 }
19192
19193 /* See its dwarf2loc.h declaration. */
19194
19195 int
19196 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
19197 {
19198 struct comp_unit_head cu_header_local;
19199 const struct comp_unit_head *cu_headerp;
19200
19201 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19202
19203 if (cu_headerp->version == 2)
19204 return cu_headerp->addr_size;
19205 else
19206 return cu_headerp->offset_size;
19207 }
19208
19209 /* Return the text offset of the CU. The returned offset comes from
19210 this CU's objfile. If this objfile came from a separate debuginfo
19211 file, then the offset may be different from the corresponding
19212 offset in the parent objfile. */
19213
19214 CORE_ADDR
19215 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
19216 {
19217 struct objfile *objfile = per_cu->objfile;
19218
19219 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19220 }
19221
19222 /* Locate the .debug_info compilation unit from CU's objfile which contains
19223 the DIE at OFFSET. Raises an error on failure. */
19224
19225 static struct dwarf2_per_cu_data *
19226 dwarf2_find_containing_comp_unit (sect_offset offset,
19227 unsigned int offset_in_dwz,
19228 struct objfile *objfile)
19229 {
19230 struct dwarf2_per_cu_data *this_cu;
19231 int low, high;
19232 const sect_offset *cu_off;
19233
19234 low = 0;
19235 high = dwarf2_per_objfile->n_comp_units - 1;
19236 while (high > low)
19237 {
19238 struct dwarf2_per_cu_data *mid_cu;
19239 int mid = low + (high - low) / 2;
19240
19241 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
19242 cu_off = &mid_cu->offset;
19243 if (mid_cu->is_dwz > offset_in_dwz
19244 || (mid_cu->is_dwz == offset_in_dwz
19245 && cu_off->sect_off >= offset.sect_off))
19246 high = mid;
19247 else
19248 low = mid + 1;
19249 }
19250 gdb_assert (low == high);
19251 this_cu = dwarf2_per_objfile->all_comp_units[low];
19252 cu_off = &this_cu->offset;
19253 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
19254 {
19255 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
19256 error (_("Dwarf Error: could not find partial DIE containing "
19257 "offset 0x%lx [in module %s]"),
19258 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
19259
19260 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
19261 <= offset.sect_off);
19262 return dwarf2_per_objfile->all_comp_units[low-1];
19263 }
19264 else
19265 {
19266 this_cu = dwarf2_per_objfile->all_comp_units[low];
19267 if (low == dwarf2_per_objfile->n_comp_units - 1
19268 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
19269 error (_("invalid dwarf2 offset %u"), offset.sect_off);
19270 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
19271 return this_cu;
19272 }
19273 }
19274
19275 /* Initialize dwarf2_cu CU, owned by PER_CU. */
19276
19277 static void
19278 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
19279 {
19280 memset (cu, 0, sizeof (*cu));
19281 per_cu->cu = cu;
19282 cu->per_cu = per_cu;
19283 cu->objfile = per_cu->objfile;
19284 obstack_init (&cu->comp_unit_obstack);
19285 }
19286
19287 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
19288
19289 static void
19290 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
19291 enum language pretend_language)
19292 {
19293 struct attribute *attr;
19294
19295 /* Set the language we're debugging. */
19296 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
19297 if (attr)
19298 set_cu_language (DW_UNSND (attr), cu);
19299 else
19300 {
19301 cu->language = pretend_language;
19302 cu->language_defn = language_def (cu->language);
19303 }
19304
19305 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
19306 if (attr)
19307 cu->producer = DW_STRING (attr);
19308 }
19309
19310 /* Release one cached compilation unit, CU. We unlink it from the tree
19311 of compilation units, but we don't remove it from the read_in_chain;
19312 the caller is responsible for that.
19313 NOTE: DATA is a void * because this function is also used as a
19314 cleanup routine. */
19315
19316 static void
19317 free_heap_comp_unit (void *data)
19318 {
19319 struct dwarf2_cu *cu = data;
19320
19321 gdb_assert (cu->per_cu != NULL);
19322 cu->per_cu->cu = NULL;
19323 cu->per_cu = NULL;
19324
19325 obstack_free (&cu->comp_unit_obstack, NULL);
19326
19327 xfree (cu);
19328 }
19329
19330 /* This cleanup function is passed the address of a dwarf2_cu on the stack
19331 when we're finished with it. We can't free the pointer itself, but be
19332 sure to unlink it from the cache. Also release any associated storage. */
19333
19334 static void
19335 free_stack_comp_unit (void *data)
19336 {
19337 struct dwarf2_cu *cu = data;
19338
19339 gdb_assert (cu->per_cu != NULL);
19340 cu->per_cu->cu = NULL;
19341 cu->per_cu = NULL;
19342
19343 obstack_free (&cu->comp_unit_obstack, NULL);
19344 cu->partial_dies = NULL;
19345 }
19346
19347 /* Free all cached compilation units. */
19348
19349 static void
19350 free_cached_comp_units (void *data)
19351 {
19352 struct dwarf2_per_cu_data *per_cu, **last_chain;
19353
19354 per_cu = dwarf2_per_objfile->read_in_chain;
19355 last_chain = &dwarf2_per_objfile->read_in_chain;
19356 while (per_cu != NULL)
19357 {
19358 struct dwarf2_per_cu_data *next_cu;
19359
19360 next_cu = per_cu->cu->read_in_chain;
19361
19362 free_heap_comp_unit (per_cu->cu);
19363 *last_chain = next_cu;
19364
19365 per_cu = next_cu;
19366 }
19367 }
19368
19369 /* Increase the age counter on each cached compilation unit, and free
19370 any that are too old. */
19371
19372 static void
19373 age_cached_comp_units (void)
19374 {
19375 struct dwarf2_per_cu_data *per_cu, **last_chain;
19376
19377 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
19378 per_cu = dwarf2_per_objfile->read_in_chain;
19379 while (per_cu != NULL)
19380 {
19381 per_cu->cu->last_used ++;
19382 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
19383 dwarf2_mark (per_cu->cu);
19384 per_cu = per_cu->cu->read_in_chain;
19385 }
19386
19387 per_cu = dwarf2_per_objfile->read_in_chain;
19388 last_chain = &dwarf2_per_objfile->read_in_chain;
19389 while (per_cu != NULL)
19390 {
19391 struct dwarf2_per_cu_data *next_cu;
19392
19393 next_cu = per_cu->cu->read_in_chain;
19394
19395 if (!per_cu->cu->mark)
19396 {
19397 free_heap_comp_unit (per_cu->cu);
19398 *last_chain = next_cu;
19399 }
19400 else
19401 last_chain = &per_cu->cu->read_in_chain;
19402
19403 per_cu = next_cu;
19404 }
19405 }
19406
19407 /* Remove a single compilation unit from the cache. */
19408
19409 static void
19410 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
19411 {
19412 struct dwarf2_per_cu_data *per_cu, **last_chain;
19413
19414 per_cu = dwarf2_per_objfile->read_in_chain;
19415 last_chain = &dwarf2_per_objfile->read_in_chain;
19416 while (per_cu != NULL)
19417 {
19418 struct dwarf2_per_cu_data *next_cu;
19419
19420 next_cu = per_cu->cu->read_in_chain;
19421
19422 if (per_cu == target_per_cu)
19423 {
19424 free_heap_comp_unit (per_cu->cu);
19425 per_cu->cu = NULL;
19426 *last_chain = next_cu;
19427 break;
19428 }
19429 else
19430 last_chain = &per_cu->cu->read_in_chain;
19431
19432 per_cu = next_cu;
19433 }
19434 }
19435
19436 /* Release all extra memory associated with OBJFILE. */
19437
19438 void
19439 dwarf2_free_objfile (struct objfile *objfile)
19440 {
19441 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
19442
19443 if (dwarf2_per_objfile == NULL)
19444 return;
19445
19446 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
19447 free_cached_comp_units (NULL);
19448
19449 if (dwarf2_per_objfile->quick_file_names_table)
19450 htab_delete (dwarf2_per_objfile->quick_file_names_table);
19451
19452 /* Everything else should be on the objfile obstack. */
19453 }
19454
19455 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
19456 We store these in a hash table separate from the DIEs, and preserve them
19457 when the DIEs are flushed out of cache.
19458
19459 The CU "per_cu" pointer is needed because offset alone is not enough to
19460 uniquely identify the type. A file may have multiple .debug_types sections,
19461 or the type may come from a DWO file. We have to use something in
19462 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
19463 routine, get_die_type_at_offset, from outside this file, and thus won't
19464 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
19465 of the objfile. */
19466
19467 struct dwarf2_per_cu_offset_and_type
19468 {
19469 const struct dwarf2_per_cu_data *per_cu;
19470 sect_offset offset;
19471 struct type *type;
19472 };
19473
19474 /* Hash function for a dwarf2_per_cu_offset_and_type. */
19475
19476 static hashval_t
19477 per_cu_offset_and_type_hash (const void *item)
19478 {
19479 const struct dwarf2_per_cu_offset_and_type *ofs = item;
19480
19481 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
19482 }
19483
19484 /* Equality function for a dwarf2_per_cu_offset_and_type. */
19485
19486 static int
19487 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
19488 {
19489 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
19490 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
19491
19492 return (ofs_lhs->per_cu == ofs_rhs->per_cu
19493 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
19494 }
19495
19496 /* Set the type associated with DIE to TYPE. Save it in CU's hash
19497 table if necessary. For convenience, return TYPE.
19498
19499 The DIEs reading must have careful ordering to:
19500 * Not cause infite loops trying to read in DIEs as a prerequisite for
19501 reading current DIE.
19502 * Not trying to dereference contents of still incompletely read in types
19503 while reading in other DIEs.
19504 * Enable referencing still incompletely read in types just by a pointer to
19505 the type without accessing its fields.
19506
19507 Therefore caller should follow these rules:
19508 * Try to fetch any prerequisite types we may need to build this DIE type
19509 before building the type and calling set_die_type.
19510 * After building type call set_die_type for current DIE as soon as
19511 possible before fetching more types to complete the current type.
19512 * Make the type as complete as possible before fetching more types. */
19513
19514 static struct type *
19515 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19516 {
19517 struct dwarf2_per_cu_offset_and_type **slot, ofs;
19518 struct objfile *objfile = cu->objfile;
19519
19520 /* For Ada types, make sure that the gnat-specific data is always
19521 initialized (if not already set). There are a few types where
19522 we should not be doing so, because the type-specific area is
19523 already used to hold some other piece of info (eg: TYPE_CODE_FLT
19524 where the type-specific area is used to store the floatformat).
19525 But this is not a problem, because the gnat-specific information
19526 is actually not needed for these types. */
19527 if (need_gnat_info (cu)
19528 && TYPE_CODE (type) != TYPE_CODE_FUNC
19529 && TYPE_CODE (type) != TYPE_CODE_FLT
19530 && !HAVE_GNAT_AUX_INFO (type))
19531 INIT_GNAT_SPECIFIC (type);
19532
19533 if (dwarf2_per_objfile->die_type_hash == NULL)
19534 {
19535 dwarf2_per_objfile->die_type_hash =
19536 htab_create_alloc_ex (127,
19537 per_cu_offset_and_type_hash,
19538 per_cu_offset_and_type_eq,
19539 NULL,
19540 &objfile->objfile_obstack,
19541 hashtab_obstack_allocate,
19542 dummy_obstack_deallocate);
19543 }
19544
19545 ofs.per_cu = cu->per_cu;
19546 ofs.offset = die->offset;
19547 ofs.type = type;
19548 slot = (struct dwarf2_per_cu_offset_and_type **)
19549 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
19550 if (*slot)
19551 complaint (&symfile_complaints,
19552 _("A problem internal to GDB: DIE 0x%x has type already set"),
19553 die->offset.sect_off);
19554 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
19555 **slot = ofs;
19556 return type;
19557 }
19558
19559 /* Look up the type for the die at OFFSET in the appropriate type_hash
19560 table, or return NULL if the die does not have a saved type. */
19561
19562 static struct type *
19563 get_die_type_at_offset (sect_offset offset,
19564 struct dwarf2_per_cu_data *per_cu)
19565 {
19566 struct dwarf2_per_cu_offset_and_type *slot, ofs;
19567
19568 if (dwarf2_per_objfile->die_type_hash == NULL)
19569 return NULL;
19570
19571 ofs.per_cu = per_cu;
19572 ofs.offset = offset;
19573 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
19574 if (slot)
19575 return slot->type;
19576 else
19577 return NULL;
19578 }
19579
19580 /* Look up the type for DIE in the appropriate type_hash table,
19581 or return NULL if DIE does not have a saved type. */
19582
19583 static struct type *
19584 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
19585 {
19586 return get_die_type_at_offset (die->offset, cu->per_cu);
19587 }
19588
19589 /* Add a dependence relationship from CU to REF_PER_CU. */
19590
19591 static void
19592 dwarf2_add_dependence (struct dwarf2_cu *cu,
19593 struct dwarf2_per_cu_data *ref_per_cu)
19594 {
19595 void **slot;
19596
19597 if (cu->dependencies == NULL)
19598 cu->dependencies
19599 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
19600 NULL, &cu->comp_unit_obstack,
19601 hashtab_obstack_allocate,
19602 dummy_obstack_deallocate);
19603
19604 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
19605 if (*slot == NULL)
19606 *slot = ref_per_cu;
19607 }
19608
19609 /* Subroutine of dwarf2_mark to pass to htab_traverse.
19610 Set the mark field in every compilation unit in the
19611 cache that we must keep because we are keeping CU. */
19612
19613 static int
19614 dwarf2_mark_helper (void **slot, void *data)
19615 {
19616 struct dwarf2_per_cu_data *per_cu;
19617
19618 per_cu = (struct dwarf2_per_cu_data *) *slot;
19619
19620 /* cu->dependencies references may not yet have been ever read if QUIT aborts
19621 reading of the chain. As such dependencies remain valid it is not much
19622 useful to track and undo them during QUIT cleanups. */
19623 if (per_cu->cu == NULL)
19624 return 1;
19625
19626 if (per_cu->cu->mark)
19627 return 1;
19628 per_cu->cu->mark = 1;
19629
19630 if (per_cu->cu->dependencies != NULL)
19631 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
19632
19633 return 1;
19634 }
19635
19636 /* Set the mark field in CU and in every other compilation unit in the
19637 cache that we must keep because we are keeping CU. */
19638
19639 static void
19640 dwarf2_mark (struct dwarf2_cu *cu)
19641 {
19642 if (cu->mark)
19643 return;
19644 cu->mark = 1;
19645 if (cu->dependencies != NULL)
19646 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
19647 }
19648
19649 static void
19650 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
19651 {
19652 while (per_cu)
19653 {
19654 per_cu->cu->mark = 0;
19655 per_cu = per_cu->cu->read_in_chain;
19656 }
19657 }
19658
19659 /* Trivial hash function for partial_die_info: the hash value of a DIE
19660 is its offset in .debug_info for this objfile. */
19661
19662 static hashval_t
19663 partial_die_hash (const void *item)
19664 {
19665 const struct partial_die_info *part_die = item;
19666
19667 return part_die->offset.sect_off;
19668 }
19669
19670 /* Trivial comparison function for partial_die_info structures: two DIEs
19671 are equal if they have the same offset. */
19672
19673 static int
19674 partial_die_eq (const void *item_lhs, const void *item_rhs)
19675 {
19676 const struct partial_die_info *part_die_lhs = item_lhs;
19677 const struct partial_die_info *part_die_rhs = item_rhs;
19678
19679 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
19680 }
19681
19682 static struct cmd_list_element *set_dwarf2_cmdlist;
19683 static struct cmd_list_element *show_dwarf2_cmdlist;
19684
19685 static void
19686 set_dwarf2_cmd (char *args, int from_tty)
19687 {
19688 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
19689 }
19690
19691 static void
19692 show_dwarf2_cmd (char *args, int from_tty)
19693 {
19694 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
19695 }
19696
19697 /* Free data associated with OBJFILE, if necessary. */
19698
19699 static void
19700 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
19701 {
19702 struct dwarf2_per_objfile *data = d;
19703 int ix;
19704
19705 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
19706 VEC_free (dwarf2_per_cu_ptr,
19707 dwarf2_per_objfile->all_comp_units[ix]->imported_symtabs);
19708
19709 for (ix = 0; ix < dwarf2_per_objfile->n_type_units; ++ix)
19710 VEC_free (dwarf2_per_cu_ptr,
19711 dwarf2_per_objfile->all_type_units[ix]->per_cu.imported_symtabs);
19712
19713 VEC_free (dwarf2_section_info_def, data->types);
19714
19715 if (data->dwo_files)
19716 free_dwo_files (data->dwo_files, objfile);
19717
19718 if (data->dwz_file && data->dwz_file->dwz_bfd)
19719 gdb_bfd_unref (data->dwz_file->dwz_bfd);
19720 }
19721
19722 \f
19723 /* The "save gdb-index" command. */
19724
19725 /* The contents of the hash table we create when building the string
19726 table. */
19727 struct strtab_entry
19728 {
19729 offset_type offset;
19730 const char *str;
19731 };
19732
19733 /* Hash function for a strtab_entry.
19734
19735 Function is used only during write_hash_table so no index format backward
19736 compatibility is needed. */
19737
19738 static hashval_t
19739 hash_strtab_entry (const void *e)
19740 {
19741 const struct strtab_entry *entry = e;
19742 return mapped_index_string_hash (INT_MAX, entry->str);
19743 }
19744
19745 /* Equality function for a strtab_entry. */
19746
19747 static int
19748 eq_strtab_entry (const void *a, const void *b)
19749 {
19750 const struct strtab_entry *ea = a;
19751 const struct strtab_entry *eb = b;
19752 return !strcmp (ea->str, eb->str);
19753 }
19754
19755 /* Create a strtab_entry hash table. */
19756
19757 static htab_t
19758 create_strtab (void)
19759 {
19760 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
19761 xfree, xcalloc, xfree);
19762 }
19763
19764 /* Add a string to the constant pool. Return the string's offset in
19765 host order. */
19766
19767 static offset_type
19768 add_string (htab_t table, struct obstack *cpool, const char *str)
19769 {
19770 void **slot;
19771 struct strtab_entry entry;
19772 struct strtab_entry *result;
19773
19774 entry.str = str;
19775 slot = htab_find_slot (table, &entry, INSERT);
19776 if (*slot)
19777 result = *slot;
19778 else
19779 {
19780 result = XNEW (struct strtab_entry);
19781 result->offset = obstack_object_size (cpool);
19782 result->str = str;
19783 obstack_grow_str0 (cpool, str);
19784 *slot = result;
19785 }
19786 return result->offset;
19787 }
19788
19789 /* An entry in the symbol table. */
19790 struct symtab_index_entry
19791 {
19792 /* The name of the symbol. */
19793 const char *name;
19794 /* The offset of the name in the constant pool. */
19795 offset_type index_offset;
19796 /* A sorted vector of the indices of all the CUs that hold an object
19797 of this name. */
19798 VEC (offset_type) *cu_indices;
19799 };
19800
19801 /* The symbol table. This is a power-of-2-sized hash table. */
19802 struct mapped_symtab
19803 {
19804 offset_type n_elements;
19805 offset_type size;
19806 struct symtab_index_entry **data;
19807 };
19808
19809 /* Hash function for a symtab_index_entry. */
19810
19811 static hashval_t
19812 hash_symtab_entry (const void *e)
19813 {
19814 const struct symtab_index_entry *entry = e;
19815 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
19816 sizeof (offset_type) * VEC_length (offset_type,
19817 entry->cu_indices),
19818 0);
19819 }
19820
19821 /* Equality function for a symtab_index_entry. */
19822
19823 static int
19824 eq_symtab_entry (const void *a, const void *b)
19825 {
19826 const struct symtab_index_entry *ea = a;
19827 const struct symtab_index_entry *eb = b;
19828 int len = VEC_length (offset_type, ea->cu_indices);
19829 if (len != VEC_length (offset_type, eb->cu_indices))
19830 return 0;
19831 return !memcmp (VEC_address (offset_type, ea->cu_indices),
19832 VEC_address (offset_type, eb->cu_indices),
19833 sizeof (offset_type) * len);
19834 }
19835
19836 /* Destroy a symtab_index_entry. */
19837
19838 static void
19839 delete_symtab_entry (void *p)
19840 {
19841 struct symtab_index_entry *entry = p;
19842 VEC_free (offset_type, entry->cu_indices);
19843 xfree (entry);
19844 }
19845
19846 /* Create a hash table holding symtab_index_entry objects. */
19847
19848 static htab_t
19849 create_symbol_hash_table (void)
19850 {
19851 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
19852 delete_symtab_entry, xcalloc, xfree);
19853 }
19854
19855 /* Create a new mapped symtab object. */
19856
19857 static struct mapped_symtab *
19858 create_mapped_symtab (void)
19859 {
19860 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
19861 symtab->n_elements = 0;
19862 symtab->size = 1024;
19863 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19864 return symtab;
19865 }
19866
19867 /* Destroy a mapped_symtab. */
19868
19869 static void
19870 cleanup_mapped_symtab (void *p)
19871 {
19872 struct mapped_symtab *symtab = p;
19873 /* The contents of the array are freed when the other hash table is
19874 destroyed. */
19875 xfree (symtab->data);
19876 xfree (symtab);
19877 }
19878
19879 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
19880 the slot.
19881
19882 Function is used only during write_hash_table so no index format backward
19883 compatibility is needed. */
19884
19885 static struct symtab_index_entry **
19886 find_slot (struct mapped_symtab *symtab, const char *name)
19887 {
19888 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
19889
19890 index = hash & (symtab->size - 1);
19891 step = ((hash * 17) & (symtab->size - 1)) | 1;
19892
19893 for (;;)
19894 {
19895 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
19896 return &symtab->data[index];
19897 index = (index + step) & (symtab->size - 1);
19898 }
19899 }
19900
19901 /* Expand SYMTAB's hash table. */
19902
19903 static void
19904 hash_expand (struct mapped_symtab *symtab)
19905 {
19906 offset_type old_size = symtab->size;
19907 offset_type i;
19908 struct symtab_index_entry **old_entries = symtab->data;
19909
19910 symtab->size *= 2;
19911 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19912
19913 for (i = 0; i < old_size; ++i)
19914 {
19915 if (old_entries[i])
19916 {
19917 struct symtab_index_entry **slot = find_slot (symtab,
19918 old_entries[i]->name);
19919 *slot = old_entries[i];
19920 }
19921 }
19922
19923 xfree (old_entries);
19924 }
19925
19926 /* Add an entry to SYMTAB. NAME is the name of the symbol.
19927 CU_INDEX is the index of the CU in which the symbol appears.
19928 IS_STATIC is one if the symbol is static, otherwise zero (global). */
19929
19930 static void
19931 add_index_entry (struct mapped_symtab *symtab, const char *name,
19932 int is_static, gdb_index_symbol_kind kind,
19933 offset_type cu_index)
19934 {
19935 struct symtab_index_entry **slot;
19936 offset_type cu_index_and_attrs;
19937
19938 ++symtab->n_elements;
19939 if (4 * symtab->n_elements / 3 >= symtab->size)
19940 hash_expand (symtab);
19941
19942 slot = find_slot (symtab, name);
19943 if (!*slot)
19944 {
19945 *slot = XNEW (struct symtab_index_entry);
19946 (*slot)->name = name;
19947 /* index_offset is set later. */
19948 (*slot)->cu_indices = NULL;
19949 }
19950
19951 cu_index_and_attrs = 0;
19952 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
19953 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
19954 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
19955
19956 /* We don't want to record an index value twice as we want to avoid the
19957 duplication.
19958 We process all global symbols and then all static symbols
19959 (which would allow us to avoid the duplication by only having to check
19960 the last entry pushed), but a symbol could have multiple kinds in one CU.
19961 To keep things simple we don't worry about the duplication here and
19962 sort and uniqufy the list after we've processed all symbols. */
19963 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
19964 }
19965
19966 /* qsort helper routine for uniquify_cu_indices. */
19967
19968 static int
19969 offset_type_compare (const void *ap, const void *bp)
19970 {
19971 offset_type a = *(offset_type *) ap;
19972 offset_type b = *(offset_type *) bp;
19973
19974 return (a > b) - (b > a);
19975 }
19976
19977 /* Sort and remove duplicates of all symbols' cu_indices lists. */
19978
19979 static void
19980 uniquify_cu_indices (struct mapped_symtab *symtab)
19981 {
19982 int i;
19983
19984 for (i = 0; i < symtab->size; ++i)
19985 {
19986 struct symtab_index_entry *entry = symtab->data[i];
19987
19988 if (entry
19989 && entry->cu_indices != NULL)
19990 {
19991 unsigned int next_to_insert, next_to_check;
19992 offset_type last_value;
19993
19994 qsort (VEC_address (offset_type, entry->cu_indices),
19995 VEC_length (offset_type, entry->cu_indices),
19996 sizeof (offset_type), offset_type_compare);
19997
19998 last_value = VEC_index (offset_type, entry->cu_indices, 0);
19999 next_to_insert = 1;
20000 for (next_to_check = 1;
20001 next_to_check < VEC_length (offset_type, entry->cu_indices);
20002 ++next_to_check)
20003 {
20004 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
20005 != last_value)
20006 {
20007 last_value = VEC_index (offset_type, entry->cu_indices,
20008 next_to_check);
20009 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
20010 last_value);
20011 ++next_to_insert;
20012 }
20013 }
20014 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
20015 }
20016 }
20017 }
20018
20019 /* Add a vector of indices to the constant pool. */
20020
20021 static offset_type
20022 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
20023 struct symtab_index_entry *entry)
20024 {
20025 void **slot;
20026
20027 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
20028 if (!*slot)
20029 {
20030 offset_type len = VEC_length (offset_type, entry->cu_indices);
20031 offset_type val = MAYBE_SWAP (len);
20032 offset_type iter;
20033 int i;
20034
20035 *slot = entry;
20036 entry->index_offset = obstack_object_size (cpool);
20037
20038 obstack_grow (cpool, &val, sizeof (val));
20039 for (i = 0;
20040 VEC_iterate (offset_type, entry->cu_indices, i, iter);
20041 ++i)
20042 {
20043 val = MAYBE_SWAP (iter);
20044 obstack_grow (cpool, &val, sizeof (val));
20045 }
20046 }
20047 else
20048 {
20049 struct symtab_index_entry *old_entry = *slot;
20050 entry->index_offset = old_entry->index_offset;
20051 entry = old_entry;
20052 }
20053 return entry->index_offset;
20054 }
20055
20056 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
20057 constant pool entries going into the obstack CPOOL. */
20058
20059 static void
20060 write_hash_table (struct mapped_symtab *symtab,
20061 struct obstack *output, struct obstack *cpool)
20062 {
20063 offset_type i;
20064 htab_t symbol_hash_table;
20065 htab_t str_table;
20066
20067 symbol_hash_table = create_symbol_hash_table ();
20068 str_table = create_strtab ();
20069
20070 /* We add all the index vectors to the constant pool first, to
20071 ensure alignment is ok. */
20072 for (i = 0; i < symtab->size; ++i)
20073 {
20074 if (symtab->data[i])
20075 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
20076 }
20077
20078 /* Now write out the hash table. */
20079 for (i = 0; i < symtab->size; ++i)
20080 {
20081 offset_type str_off, vec_off;
20082
20083 if (symtab->data[i])
20084 {
20085 str_off = add_string (str_table, cpool, symtab->data[i]->name);
20086 vec_off = symtab->data[i]->index_offset;
20087 }
20088 else
20089 {
20090 /* While 0 is a valid constant pool index, it is not valid
20091 to have 0 for both offsets. */
20092 str_off = 0;
20093 vec_off = 0;
20094 }
20095
20096 str_off = MAYBE_SWAP (str_off);
20097 vec_off = MAYBE_SWAP (vec_off);
20098
20099 obstack_grow (output, &str_off, sizeof (str_off));
20100 obstack_grow (output, &vec_off, sizeof (vec_off));
20101 }
20102
20103 htab_delete (str_table);
20104 htab_delete (symbol_hash_table);
20105 }
20106
20107 /* Struct to map psymtab to CU index in the index file. */
20108 struct psymtab_cu_index_map
20109 {
20110 struct partial_symtab *psymtab;
20111 unsigned int cu_index;
20112 };
20113
20114 static hashval_t
20115 hash_psymtab_cu_index (const void *item)
20116 {
20117 const struct psymtab_cu_index_map *map = item;
20118
20119 return htab_hash_pointer (map->psymtab);
20120 }
20121
20122 static int
20123 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
20124 {
20125 const struct psymtab_cu_index_map *lhs = item_lhs;
20126 const struct psymtab_cu_index_map *rhs = item_rhs;
20127
20128 return lhs->psymtab == rhs->psymtab;
20129 }
20130
20131 /* Helper struct for building the address table. */
20132 struct addrmap_index_data
20133 {
20134 struct objfile *objfile;
20135 struct obstack *addr_obstack;
20136 htab_t cu_index_htab;
20137
20138 /* Non-zero if the previous_* fields are valid.
20139 We can't write an entry until we see the next entry (since it is only then
20140 that we know the end of the entry). */
20141 int previous_valid;
20142 /* Index of the CU in the table of all CUs in the index file. */
20143 unsigned int previous_cu_index;
20144 /* Start address of the CU. */
20145 CORE_ADDR previous_cu_start;
20146 };
20147
20148 /* Write an address entry to OBSTACK. */
20149
20150 static void
20151 add_address_entry (struct objfile *objfile, struct obstack *obstack,
20152 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
20153 {
20154 offset_type cu_index_to_write;
20155 char addr[8];
20156 CORE_ADDR baseaddr;
20157
20158 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20159
20160 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
20161 obstack_grow (obstack, addr, 8);
20162 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
20163 obstack_grow (obstack, addr, 8);
20164 cu_index_to_write = MAYBE_SWAP (cu_index);
20165 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
20166 }
20167
20168 /* Worker function for traversing an addrmap to build the address table. */
20169
20170 static int
20171 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
20172 {
20173 struct addrmap_index_data *data = datap;
20174 struct partial_symtab *pst = obj;
20175
20176 if (data->previous_valid)
20177 add_address_entry (data->objfile, data->addr_obstack,
20178 data->previous_cu_start, start_addr,
20179 data->previous_cu_index);
20180
20181 data->previous_cu_start = start_addr;
20182 if (pst != NULL)
20183 {
20184 struct psymtab_cu_index_map find_map, *map;
20185 find_map.psymtab = pst;
20186 map = htab_find (data->cu_index_htab, &find_map);
20187 gdb_assert (map != NULL);
20188 data->previous_cu_index = map->cu_index;
20189 data->previous_valid = 1;
20190 }
20191 else
20192 data->previous_valid = 0;
20193
20194 return 0;
20195 }
20196
20197 /* Write OBJFILE's address map to OBSTACK.
20198 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
20199 in the index file. */
20200
20201 static void
20202 write_address_map (struct objfile *objfile, struct obstack *obstack,
20203 htab_t cu_index_htab)
20204 {
20205 struct addrmap_index_data addrmap_index_data;
20206
20207 /* When writing the address table, we have to cope with the fact that
20208 the addrmap iterator only provides the start of a region; we have to
20209 wait until the next invocation to get the start of the next region. */
20210
20211 addrmap_index_data.objfile = objfile;
20212 addrmap_index_data.addr_obstack = obstack;
20213 addrmap_index_data.cu_index_htab = cu_index_htab;
20214 addrmap_index_data.previous_valid = 0;
20215
20216 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
20217 &addrmap_index_data);
20218
20219 /* It's highly unlikely the last entry (end address = 0xff...ff)
20220 is valid, but we should still handle it.
20221 The end address is recorded as the start of the next region, but that
20222 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
20223 anyway. */
20224 if (addrmap_index_data.previous_valid)
20225 add_address_entry (objfile, obstack,
20226 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
20227 addrmap_index_data.previous_cu_index);
20228 }
20229
20230 /* Return the symbol kind of PSYM. */
20231
20232 static gdb_index_symbol_kind
20233 symbol_kind (struct partial_symbol *psym)
20234 {
20235 domain_enum domain = PSYMBOL_DOMAIN (psym);
20236 enum address_class aclass = PSYMBOL_CLASS (psym);
20237
20238 switch (domain)
20239 {
20240 case VAR_DOMAIN:
20241 switch (aclass)
20242 {
20243 case LOC_BLOCK:
20244 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
20245 case LOC_TYPEDEF:
20246 return GDB_INDEX_SYMBOL_KIND_TYPE;
20247 case LOC_COMPUTED:
20248 case LOC_CONST_BYTES:
20249 case LOC_OPTIMIZED_OUT:
20250 case LOC_STATIC:
20251 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20252 case LOC_CONST:
20253 /* Note: It's currently impossible to recognize psyms as enum values
20254 short of reading the type info. For now punt. */
20255 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20256 default:
20257 /* There are other LOC_FOO values that one might want to classify
20258 as variables, but dwarf2read.c doesn't currently use them. */
20259 return GDB_INDEX_SYMBOL_KIND_OTHER;
20260 }
20261 case STRUCT_DOMAIN:
20262 return GDB_INDEX_SYMBOL_KIND_TYPE;
20263 default:
20264 return GDB_INDEX_SYMBOL_KIND_OTHER;
20265 }
20266 }
20267
20268 /* Add a list of partial symbols to SYMTAB. */
20269
20270 static void
20271 write_psymbols (struct mapped_symtab *symtab,
20272 htab_t psyms_seen,
20273 struct partial_symbol **psymp,
20274 int count,
20275 offset_type cu_index,
20276 int is_static)
20277 {
20278 for (; count-- > 0; ++psymp)
20279 {
20280 struct partial_symbol *psym = *psymp;
20281 void **slot;
20282
20283 if (SYMBOL_LANGUAGE (psym) == language_ada)
20284 error (_("Ada is not currently supported by the index"));
20285
20286 /* Only add a given psymbol once. */
20287 slot = htab_find_slot (psyms_seen, psym, INSERT);
20288 if (!*slot)
20289 {
20290 gdb_index_symbol_kind kind = symbol_kind (psym);
20291
20292 *slot = psym;
20293 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
20294 is_static, kind, cu_index);
20295 }
20296 }
20297 }
20298
20299 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
20300 exception if there is an error. */
20301
20302 static void
20303 write_obstack (FILE *file, struct obstack *obstack)
20304 {
20305 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
20306 file)
20307 != obstack_object_size (obstack))
20308 error (_("couldn't data write to file"));
20309 }
20310
20311 /* Unlink a file if the argument is not NULL. */
20312
20313 static void
20314 unlink_if_set (void *p)
20315 {
20316 char **filename = p;
20317 if (*filename)
20318 unlink (*filename);
20319 }
20320
20321 /* A helper struct used when iterating over debug_types. */
20322 struct signatured_type_index_data
20323 {
20324 struct objfile *objfile;
20325 struct mapped_symtab *symtab;
20326 struct obstack *types_list;
20327 htab_t psyms_seen;
20328 int cu_index;
20329 };
20330
20331 /* A helper function that writes a single signatured_type to an
20332 obstack. */
20333
20334 static int
20335 write_one_signatured_type (void **slot, void *d)
20336 {
20337 struct signatured_type_index_data *info = d;
20338 struct signatured_type *entry = (struct signatured_type *) *slot;
20339 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
20340 struct partial_symtab *psymtab = per_cu->v.psymtab;
20341 gdb_byte val[8];
20342
20343 write_psymbols (info->symtab,
20344 info->psyms_seen,
20345 info->objfile->global_psymbols.list
20346 + psymtab->globals_offset,
20347 psymtab->n_global_syms, info->cu_index,
20348 0);
20349 write_psymbols (info->symtab,
20350 info->psyms_seen,
20351 info->objfile->static_psymbols.list
20352 + psymtab->statics_offset,
20353 psymtab->n_static_syms, info->cu_index,
20354 1);
20355
20356 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20357 entry->per_cu.offset.sect_off);
20358 obstack_grow (info->types_list, val, 8);
20359 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20360 entry->type_offset_in_tu.cu_off);
20361 obstack_grow (info->types_list, val, 8);
20362 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
20363 obstack_grow (info->types_list, val, 8);
20364
20365 ++info->cu_index;
20366
20367 return 1;
20368 }
20369
20370 /* Recurse into all "included" dependencies and write their symbols as
20371 if they appeared in this psymtab. */
20372
20373 static void
20374 recursively_write_psymbols (struct objfile *objfile,
20375 struct partial_symtab *psymtab,
20376 struct mapped_symtab *symtab,
20377 htab_t psyms_seen,
20378 offset_type cu_index)
20379 {
20380 int i;
20381
20382 for (i = 0; i < psymtab->number_of_dependencies; ++i)
20383 if (psymtab->dependencies[i]->user != NULL)
20384 recursively_write_psymbols (objfile, psymtab->dependencies[i],
20385 symtab, psyms_seen, cu_index);
20386
20387 write_psymbols (symtab,
20388 psyms_seen,
20389 objfile->global_psymbols.list + psymtab->globals_offset,
20390 psymtab->n_global_syms, cu_index,
20391 0);
20392 write_psymbols (symtab,
20393 psyms_seen,
20394 objfile->static_psymbols.list + psymtab->statics_offset,
20395 psymtab->n_static_syms, cu_index,
20396 1);
20397 }
20398
20399 /* Create an index file for OBJFILE in the directory DIR. */
20400
20401 static void
20402 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
20403 {
20404 struct cleanup *cleanup;
20405 char *filename, *cleanup_filename;
20406 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
20407 struct obstack cu_list, types_cu_list;
20408 int i;
20409 FILE *out_file;
20410 struct mapped_symtab *symtab;
20411 offset_type val, size_of_contents, total_len;
20412 struct stat st;
20413 htab_t psyms_seen;
20414 htab_t cu_index_htab;
20415 struct psymtab_cu_index_map *psymtab_cu_index_map;
20416
20417 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
20418 return;
20419
20420 if (dwarf2_per_objfile->using_index)
20421 error (_("Cannot use an index to create the index"));
20422
20423 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
20424 error (_("Cannot make an index when the file has multiple .debug_types sections"));
20425
20426 if (stat (objfile->name, &st) < 0)
20427 perror_with_name (objfile->name);
20428
20429 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
20430 INDEX_SUFFIX, (char *) NULL);
20431 cleanup = make_cleanup (xfree, filename);
20432
20433 out_file = fopen (filename, "wb");
20434 if (!out_file)
20435 error (_("Can't open `%s' for writing"), filename);
20436
20437 cleanup_filename = filename;
20438 make_cleanup (unlink_if_set, &cleanup_filename);
20439
20440 symtab = create_mapped_symtab ();
20441 make_cleanup (cleanup_mapped_symtab, symtab);
20442
20443 obstack_init (&addr_obstack);
20444 make_cleanup_obstack_free (&addr_obstack);
20445
20446 obstack_init (&cu_list);
20447 make_cleanup_obstack_free (&cu_list);
20448
20449 obstack_init (&types_cu_list);
20450 make_cleanup_obstack_free (&types_cu_list);
20451
20452 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
20453 NULL, xcalloc, xfree);
20454 make_cleanup_htab_delete (psyms_seen);
20455
20456 /* While we're scanning CU's create a table that maps a psymtab pointer
20457 (which is what addrmap records) to its index (which is what is recorded
20458 in the index file). This will later be needed to write the address
20459 table. */
20460 cu_index_htab = htab_create_alloc (100,
20461 hash_psymtab_cu_index,
20462 eq_psymtab_cu_index,
20463 NULL, xcalloc, xfree);
20464 make_cleanup_htab_delete (cu_index_htab);
20465 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
20466 xmalloc (sizeof (struct psymtab_cu_index_map)
20467 * dwarf2_per_objfile->n_comp_units);
20468 make_cleanup (xfree, psymtab_cu_index_map);
20469
20470 /* The CU list is already sorted, so we don't need to do additional
20471 work here. Also, the debug_types entries do not appear in
20472 all_comp_units, but only in their own hash table. */
20473 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
20474 {
20475 struct dwarf2_per_cu_data *per_cu
20476 = dwarf2_per_objfile->all_comp_units[i];
20477 struct partial_symtab *psymtab = per_cu->v.psymtab;
20478 gdb_byte val[8];
20479 struct psymtab_cu_index_map *map;
20480 void **slot;
20481
20482 if (psymtab->user == NULL)
20483 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
20484
20485 map = &psymtab_cu_index_map[i];
20486 map->psymtab = psymtab;
20487 map->cu_index = i;
20488 slot = htab_find_slot (cu_index_htab, map, INSERT);
20489 gdb_assert (slot != NULL);
20490 gdb_assert (*slot == NULL);
20491 *slot = map;
20492
20493 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20494 per_cu->offset.sect_off);
20495 obstack_grow (&cu_list, val, 8);
20496 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
20497 obstack_grow (&cu_list, val, 8);
20498 }
20499
20500 /* Dump the address map. */
20501 write_address_map (objfile, &addr_obstack, cu_index_htab);
20502
20503 /* Write out the .debug_type entries, if any. */
20504 if (dwarf2_per_objfile->signatured_types)
20505 {
20506 struct signatured_type_index_data sig_data;
20507
20508 sig_data.objfile = objfile;
20509 sig_data.symtab = symtab;
20510 sig_data.types_list = &types_cu_list;
20511 sig_data.psyms_seen = psyms_seen;
20512 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
20513 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
20514 write_one_signatured_type, &sig_data);
20515 }
20516
20517 /* Now that we've processed all symbols we can shrink their cu_indices
20518 lists. */
20519 uniquify_cu_indices (symtab);
20520
20521 obstack_init (&constant_pool);
20522 make_cleanup_obstack_free (&constant_pool);
20523 obstack_init (&symtab_obstack);
20524 make_cleanup_obstack_free (&symtab_obstack);
20525 write_hash_table (symtab, &symtab_obstack, &constant_pool);
20526
20527 obstack_init (&contents);
20528 make_cleanup_obstack_free (&contents);
20529 size_of_contents = 6 * sizeof (offset_type);
20530 total_len = size_of_contents;
20531
20532 /* The version number. */
20533 val = MAYBE_SWAP (8);
20534 obstack_grow (&contents, &val, sizeof (val));
20535
20536 /* The offset of the CU list from the start of the file. */
20537 val = MAYBE_SWAP (total_len);
20538 obstack_grow (&contents, &val, sizeof (val));
20539 total_len += obstack_object_size (&cu_list);
20540
20541 /* The offset of the types CU list from the start of the file. */
20542 val = MAYBE_SWAP (total_len);
20543 obstack_grow (&contents, &val, sizeof (val));
20544 total_len += obstack_object_size (&types_cu_list);
20545
20546 /* The offset of the address table from the start of the file. */
20547 val = MAYBE_SWAP (total_len);
20548 obstack_grow (&contents, &val, sizeof (val));
20549 total_len += obstack_object_size (&addr_obstack);
20550
20551 /* The offset of the symbol table from the start of the file. */
20552 val = MAYBE_SWAP (total_len);
20553 obstack_grow (&contents, &val, sizeof (val));
20554 total_len += obstack_object_size (&symtab_obstack);
20555
20556 /* The offset of the constant pool from the start of the file. */
20557 val = MAYBE_SWAP (total_len);
20558 obstack_grow (&contents, &val, sizeof (val));
20559 total_len += obstack_object_size (&constant_pool);
20560
20561 gdb_assert (obstack_object_size (&contents) == size_of_contents);
20562
20563 write_obstack (out_file, &contents);
20564 write_obstack (out_file, &cu_list);
20565 write_obstack (out_file, &types_cu_list);
20566 write_obstack (out_file, &addr_obstack);
20567 write_obstack (out_file, &symtab_obstack);
20568 write_obstack (out_file, &constant_pool);
20569
20570 fclose (out_file);
20571
20572 /* We want to keep the file, so we set cleanup_filename to NULL
20573 here. See unlink_if_set. */
20574 cleanup_filename = NULL;
20575
20576 do_cleanups (cleanup);
20577 }
20578
20579 /* Implementation of the `save gdb-index' command.
20580
20581 Note that the file format used by this command is documented in the
20582 GDB manual. Any changes here must be documented there. */
20583
20584 static void
20585 save_gdb_index_command (char *arg, int from_tty)
20586 {
20587 struct objfile *objfile;
20588
20589 if (!arg || !*arg)
20590 error (_("usage: save gdb-index DIRECTORY"));
20591
20592 ALL_OBJFILES (objfile)
20593 {
20594 struct stat st;
20595
20596 /* If the objfile does not correspond to an actual file, skip it. */
20597 if (stat (objfile->name, &st) < 0)
20598 continue;
20599
20600 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
20601 if (dwarf2_per_objfile)
20602 {
20603 volatile struct gdb_exception except;
20604
20605 TRY_CATCH (except, RETURN_MASK_ERROR)
20606 {
20607 write_psymtabs_to_index (objfile, arg);
20608 }
20609 if (except.reason < 0)
20610 exception_fprintf (gdb_stderr, except,
20611 _("Error while writing index for `%s': "),
20612 objfile->name);
20613 }
20614 }
20615 }
20616
20617 \f
20618
20619 int dwarf2_always_disassemble;
20620
20621 static void
20622 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
20623 struct cmd_list_element *c, const char *value)
20624 {
20625 fprintf_filtered (file,
20626 _("Whether to always disassemble "
20627 "DWARF expressions is %s.\n"),
20628 value);
20629 }
20630
20631 static void
20632 show_check_physname (struct ui_file *file, int from_tty,
20633 struct cmd_list_element *c, const char *value)
20634 {
20635 fprintf_filtered (file,
20636 _("Whether to check \"physname\" is %s.\n"),
20637 value);
20638 }
20639
20640 void _initialize_dwarf2_read (void);
20641
20642 void
20643 _initialize_dwarf2_read (void)
20644 {
20645 struct cmd_list_element *c;
20646
20647 dwarf2_objfile_data_key
20648 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
20649
20650 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
20651 Set DWARF 2 specific variables.\n\
20652 Configure DWARF 2 variables such as the cache size"),
20653 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
20654 0/*allow-unknown*/, &maintenance_set_cmdlist);
20655
20656 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
20657 Show DWARF 2 specific variables\n\
20658 Show DWARF 2 variables such as the cache size"),
20659 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
20660 0/*allow-unknown*/, &maintenance_show_cmdlist);
20661
20662 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
20663 &dwarf2_max_cache_age, _("\
20664 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
20665 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
20666 A higher limit means that cached compilation units will be stored\n\
20667 in memory longer, and more total memory will be used. Zero disables\n\
20668 caching, which can slow down startup."),
20669 NULL,
20670 show_dwarf2_max_cache_age,
20671 &set_dwarf2_cmdlist,
20672 &show_dwarf2_cmdlist);
20673
20674 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
20675 &dwarf2_always_disassemble, _("\
20676 Set whether `info address' always disassembles DWARF expressions."), _("\
20677 Show whether `info address' always disassembles DWARF expressions."), _("\
20678 When enabled, DWARF expressions are always printed in an assembly-like\n\
20679 syntax. When disabled, expressions will be printed in a more\n\
20680 conversational style, when possible."),
20681 NULL,
20682 show_dwarf2_always_disassemble,
20683 &set_dwarf2_cmdlist,
20684 &show_dwarf2_cmdlist);
20685
20686 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
20687 Set debugging of the dwarf2 reader."), _("\
20688 Show debugging of the dwarf2 reader."), _("\
20689 When enabled, debugging messages are printed during dwarf2 reading\n\
20690 and symtab expansion."),
20691 NULL,
20692 NULL,
20693 &setdebuglist, &showdebuglist);
20694
20695 add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
20696 Set debugging of the dwarf2 DIE reader."), _("\
20697 Show debugging of the dwarf2 DIE reader."), _("\
20698 When enabled (non-zero), DIEs are dumped after they are read in.\n\
20699 The value is the maximum depth to print."),
20700 NULL,
20701 NULL,
20702 &setdebuglist, &showdebuglist);
20703
20704 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
20705 Set cross-checking of \"physname\" code against demangler."), _("\
20706 Show cross-checking of \"physname\" code against demangler."), _("\
20707 When enabled, GDB's internal \"physname\" code is checked against\n\
20708 the demangler."),
20709 NULL, show_check_physname,
20710 &setdebuglist, &showdebuglist);
20711
20712 add_setshow_boolean_cmd ("use-deprecated-index-sections",
20713 no_class, &use_deprecated_index_sections, _("\
20714 Set whether to use deprecated gdb_index sections."), _("\
20715 Show whether to use deprecated gdb_index sections."), _("\
20716 When enabled, deprecated .gdb_index sections are used anyway.\n\
20717 Normally they are ignored either because of a missing feature or\n\
20718 performance issue.\n\
20719 Warning: This option must be enabled before gdb reads the file."),
20720 NULL,
20721 NULL,
20722 &setlist, &showlist);
20723
20724 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
20725 _("\
20726 Save a gdb-index file.\n\
20727 Usage: save gdb-index DIRECTORY"),
20728 &save_cmdlist);
20729 set_cmd_completer (c, filename_completer);
20730
20731 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
20732 &dwarf2_locexpr_funcs);
20733 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
20734 &dwarf2_loclist_funcs);
20735
20736 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
20737 &dwarf2_block_frame_base_locexpr_funcs);
20738 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
20739 &dwarf2_block_frame_base_loclist_funcs);
20740 }
This page took 0.517488 seconds and 4 git commands to generate.