4bdd6457ff08d51bdbaa32425ffdca2f944fb0ae
[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 struct partial_symtab *create_partial_symtab
1282 (struct dwarf2_per_cu_data *per_cu, const char *name);
1283
1284 static void dwarf2_build_psymtabs_hard (struct objfile *);
1285
1286 static void scan_partial_symbols (struct partial_die_info *,
1287 CORE_ADDR *, CORE_ADDR *,
1288 int, struct dwarf2_cu *);
1289
1290 static void add_partial_symbol (struct partial_die_info *,
1291 struct dwarf2_cu *);
1292
1293 static void add_partial_namespace (struct partial_die_info *pdi,
1294 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1295 int need_pc, struct dwarf2_cu *cu);
1296
1297 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1298 CORE_ADDR *highpc, int need_pc,
1299 struct dwarf2_cu *cu);
1300
1301 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1302 struct dwarf2_cu *cu);
1303
1304 static void add_partial_subprogram (struct partial_die_info *pdi,
1305 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1306 int need_pc, struct dwarf2_cu *cu);
1307
1308 static void dwarf2_read_symtab (struct partial_symtab *,
1309 struct objfile *);
1310
1311 static void psymtab_to_symtab_1 (struct partial_symtab *);
1312
1313 static struct abbrev_info *abbrev_table_lookup_abbrev
1314 (const struct abbrev_table *, unsigned int);
1315
1316 static struct abbrev_table *abbrev_table_read_table
1317 (struct dwarf2_section_info *, sect_offset);
1318
1319 static void abbrev_table_free (struct abbrev_table *);
1320
1321 static void abbrev_table_free_cleanup (void *);
1322
1323 static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1324 struct dwarf2_section_info *);
1325
1326 static void dwarf2_free_abbrev_table (void *);
1327
1328 static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1329
1330 static struct partial_die_info *load_partial_dies
1331 (const struct die_reader_specs *, gdb_byte *, int);
1332
1333 static gdb_byte *read_partial_die (const struct die_reader_specs *,
1334 struct partial_die_info *,
1335 struct abbrev_info *,
1336 unsigned int,
1337 gdb_byte *);
1338
1339 static struct partial_die_info *find_partial_die (sect_offset, int,
1340 struct dwarf2_cu *);
1341
1342 static void fixup_partial_die (struct partial_die_info *,
1343 struct dwarf2_cu *);
1344
1345 static gdb_byte *read_attribute (const struct die_reader_specs *,
1346 struct attribute *, struct attr_abbrev *,
1347 gdb_byte *);
1348
1349 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1350
1351 static int read_1_signed_byte (bfd *, const gdb_byte *);
1352
1353 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1354
1355 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1356
1357 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1358
1359 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
1360 unsigned int *);
1361
1362 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1363
1364 static LONGEST read_checked_initial_length_and_offset
1365 (bfd *, gdb_byte *, const struct comp_unit_head *,
1366 unsigned int *, unsigned int *);
1367
1368 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
1369 unsigned int *);
1370
1371 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
1372
1373 static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1374 sect_offset);
1375
1376 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
1377
1378 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
1379
1380 static char *read_indirect_string (bfd *, gdb_byte *,
1381 const struct comp_unit_head *,
1382 unsigned int *);
1383
1384 static char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1385
1386 static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
1387
1388 static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
1389
1390 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1391 unsigned int *);
1392
1393 static char *read_str_index (const struct die_reader_specs *reader,
1394 struct dwarf2_cu *cu, ULONGEST str_index);
1395
1396 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1397
1398 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1399 struct dwarf2_cu *);
1400
1401 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1402 unsigned int);
1403
1404 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1405 struct dwarf2_cu *cu);
1406
1407 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1408
1409 static struct die_info *die_specification (struct die_info *die,
1410 struct dwarf2_cu **);
1411
1412 static void free_line_header (struct line_header *lh);
1413
1414 static void add_file_name (struct line_header *, char *, unsigned int,
1415 unsigned int, unsigned int);
1416
1417 static struct line_header *dwarf_decode_line_header (unsigned int offset,
1418 struct dwarf2_cu *cu);
1419
1420 static void dwarf_decode_lines (struct line_header *, const char *,
1421 struct dwarf2_cu *, struct partial_symtab *,
1422 int);
1423
1424 static void dwarf2_start_subfile (char *, const char *, const char *);
1425
1426 static void dwarf2_start_symtab (struct dwarf2_cu *,
1427 const char *, const char *, CORE_ADDR);
1428
1429 static struct symbol *new_symbol (struct die_info *, struct type *,
1430 struct dwarf2_cu *);
1431
1432 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1433 struct dwarf2_cu *, struct symbol *);
1434
1435 static void dwarf2_const_value (struct attribute *, struct symbol *,
1436 struct dwarf2_cu *);
1437
1438 static void dwarf2_const_value_attr (struct attribute *attr,
1439 struct type *type,
1440 const char *name,
1441 struct obstack *obstack,
1442 struct dwarf2_cu *cu, LONGEST *value,
1443 gdb_byte **bytes,
1444 struct dwarf2_locexpr_baton **baton);
1445
1446 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1447
1448 static int need_gnat_info (struct dwarf2_cu *);
1449
1450 static struct type *die_descriptive_type (struct die_info *,
1451 struct dwarf2_cu *);
1452
1453 static void set_descriptive_type (struct type *, struct die_info *,
1454 struct dwarf2_cu *);
1455
1456 static struct type *die_containing_type (struct die_info *,
1457 struct dwarf2_cu *);
1458
1459 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1460 struct dwarf2_cu *);
1461
1462 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1463
1464 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1465
1466 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1467
1468 static char *typename_concat (struct obstack *obs, const char *prefix,
1469 const char *suffix, int physname,
1470 struct dwarf2_cu *cu);
1471
1472 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1473
1474 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1475
1476 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1477
1478 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1479
1480 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1481
1482 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1483 struct dwarf2_cu *, struct partial_symtab *);
1484
1485 static int dwarf2_get_pc_bounds (struct die_info *,
1486 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1487 struct partial_symtab *);
1488
1489 static void get_scope_pc_bounds (struct die_info *,
1490 CORE_ADDR *, CORE_ADDR *,
1491 struct dwarf2_cu *);
1492
1493 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1494 CORE_ADDR, struct dwarf2_cu *);
1495
1496 static void dwarf2_add_field (struct field_info *, struct die_info *,
1497 struct dwarf2_cu *);
1498
1499 static void dwarf2_attach_fields_to_type (struct field_info *,
1500 struct type *, struct dwarf2_cu *);
1501
1502 static void dwarf2_add_member_fn (struct field_info *,
1503 struct die_info *, struct type *,
1504 struct dwarf2_cu *);
1505
1506 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1507 struct type *,
1508 struct dwarf2_cu *);
1509
1510 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1511
1512 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1513
1514 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1515
1516 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1517
1518 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1519
1520 static struct type *read_module_type (struct die_info *die,
1521 struct dwarf2_cu *cu);
1522
1523 static const char *namespace_name (struct die_info *die,
1524 int *is_anonymous, struct dwarf2_cu *);
1525
1526 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1527
1528 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1529
1530 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1531 struct dwarf2_cu *);
1532
1533 static struct die_info *read_die_and_children (const struct die_reader_specs *,
1534 gdb_byte *info_ptr,
1535 gdb_byte **new_info_ptr,
1536 struct die_info *parent);
1537
1538 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1539 gdb_byte *info_ptr,
1540 gdb_byte **new_info_ptr,
1541 struct die_info *parent);
1542
1543 static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1544 struct die_info **, gdb_byte *, int *, int);
1545
1546 static gdb_byte *read_full_die (const struct die_reader_specs *,
1547 struct die_info **, gdb_byte *, int *);
1548
1549 static void process_die (struct die_info *, struct dwarf2_cu *);
1550
1551 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1552 struct obstack *);
1553
1554 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1555
1556 static const char *dwarf2_full_name (const char *name,
1557 struct die_info *die,
1558 struct dwarf2_cu *cu);
1559
1560 static const char *dwarf2_physname (const char *name, struct die_info *die,
1561 struct dwarf2_cu *cu);
1562
1563 static struct die_info *dwarf2_extension (struct die_info *die,
1564 struct dwarf2_cu **);
1565
1566 static const char *dwarf_tag_name (unsigned int);
1567
1568 static const char *dwarf_attr_name (unsigned int);
1569
1570 static const char *dwarf_form_name (unsigned int);
1571
1572 static char *dwarf_bool_name (unsigned int);
1573
1574 static const char *dwarf_type_encoding_name (unsigned int);
1575
1576 static struct die_info *sibling_die (struct die_info *);
1577
1578 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1579
1580 static void dump_die_for_error (struct die_info *);
1581
1582 static void dump_die_1 (struct ui_file *, int level, int max_level,
1583 struct die_info *);
1584
1585 /*static*/ void dump_die (struct die_info *, int max_level);
1586
1587 static void store_in_ref_table (struct die_info *,
1588 struct dwarf2_cu *);
1589
1590 static int is_ref_attr (struct attribute *);
1591
1592 static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
1593
1594 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1595
1596 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1597 struct attribute *,
1598 struct dwarf2_cu **);
1599
1600 static struct die_info *follow_die_ref (struct die_info *,
1601 struct attribute *,
1602 struct dwarf2_cu **);
1603
1604 static struct die_info *follow_die_sig (struct die_info *,
1605 struct attribute *,
1606 struct dwarf2_cu **);
1607
1608 static struct signatured_type *lookup_signatured_type_at_offset
1609 (struct objfile *objfile,
1610 struct dwarf2_section_info *section, sect_offset offset);
1611
1612 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1613
1614 static void read_signatured_type (struct signatured_type *);
1615
1616 static struct type_unit_group *get_type_unit_group
1617 (struct dwarf2_cu *, struct attribute *);
1618
1619 static void build_type_unit_groups (die_reader_func_ftype *, void *);
1620
1621 /* memory allocation interface */
1622
1623 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1624
1625 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1626
1627 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
1628 const char *, int);
1629
1630 static int attr_form_is_block (struct attribute *);
1631
1632 static int attr_form_is_section_offset (struct attribute *);
1633
1634 static int attr_form_is_constant (struct attribute *);
1635
1636 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1637 struct dwarf2_loclist_baton *baton,
1638 struct attribute *attr);
1639
1640 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1641 struct symbol *sym,
1642 struct dwarf2_cu *cu,
1643 int is_block);
1644
1645 static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1646 gdb_byte *info_ptr,
1647 struct abbrev_info *abbrev);
1648
1649 static void free_stack_comp_unit (void *);
1650
1651 static hashval_t partial_die_hash (const void *item);
1652
1653 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1654
1655 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1656 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
1657
1658 static void init_one_comp_unit (struct dwarf2_cu *cu,
1659 struct dwarf2_per_cu_data *per_cu);
1660
1661 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1662 struct die_info *comp_unit_die,
1663 enum language pretend_language);
1664
1665 static void free_heap_comp_unit (void *);
1666
1667 static void free_cached_comp_units (void *);
1668
1669 static void age_cached_comp_units (void);
1670
1671 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1672
1673 static struct type *set_die_type (struct die_info *, struct type *,
1674 struct dwarf2_cu *);
1675
1676 static void create_all_comp_units (struct objfile *);
1677
1678 static int create_all_type_units (struct objfile *);
1679
1680 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1681 enum language);
1682
1683 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1684 enum language);
1685
1686 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1687 enum language);
1688
1689 static void dwarf2_add_dependence (struct dwarf2_cu *,
1690 struct dwarf2_per_cu_data *);
1691
1692 static void dwarf2_mark (struct dwarf2_cu *);
1693
1694 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1695
1696 static struct type *get_die_type_at_offset (sect_offset,
1697 struct dwarf2_per_cu_data *per_cu);
1698
1699 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1700
1701 static void dwarf2_release_queue (void *dummy);
1702
1703 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1704 enum language pretend_language);
1705
1706 static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1707 struct dwarf2_per_cu_data *per_cu,
1708 enum language pretend_language);
1709
1710 static void process_queue (void);
1711
1712 static void find_file_and_directory (struct die_info *die,
1713 struct dwarf2_cu *cu,
1714 const char **name, const char **comp_dir);
1715
1716 static char *file_full_name (int file, struct line_header *lh,
1717 const char *comp_dir);
1718
1719 static gdb_byte *read_and_check_comp_unit_head
1720 (struct comp_unit_head *header,
1721 struct dwarf2_section_info *section,
1722 struct dwarf2_section_info *abbrev_section, gdb_byte *info_ptr,
1723 int is_debug_types_section);
1724
1725 static void init_cutu_and_read_dies
1726 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1727 int use_existing_cu, int keep,
1728 die_reader_func_ftype *die_reader_func, void *data);
1729
1730 static void init_cutu_and_read_dies_simple
1731 (struct dwarf2_per_cu_data *this_cu,
1732 die_reader_func_ftype *die_reader_func, void *data);
1733
1734 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1735
1736 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1737
1738 static struct dwo_unit *lookup_dwo_comp_unit
1739 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1740
1741 static struct dwo_unit *lookup_dwo_type_unit
1742 (struct signatured_type *, const char *, const char *);
1743
1744 static void free_dwo_file_cleanup (void *);
1745
1746 static void process_cu_includes (void);
1747
1748 static void check_producer (struct dwarf2_cu *cu);
1749
1750 #if WORDS_BIGENDIAN
1751
1752 /* Convert VALUE between big- and little-endian. */
1753 static offset_type
1754 byte_swap (offset_type value)
1755 {
1756 offset_type result;
1757
1758 result = (value & 0xff) << 24;
1759 result |= (value & 0xff00) << 8;
1760 result |= (value & 0xff0000) >> 8;
1761 result |= (value & 0xff000000) >> 24;
1762 return result;
1763 }
1764
1765 #define MAYBE_SWAP(V) byte_swap (V)
1766
1767 #else
1768 #define MAYBE_SWAP(V) (V)
1769 #endif /* WORDS_BIGENDIAN */
1770
1771 /* The suffix for an index file. */
1772 #define INDEX_SUFFIX ".gdb-index"
1773
1774 /* Try to locate the sections we need for DWARF 2 debugging
1775 information and return true if we have enough to do something.
1776 NAMES points to the dwarf2 section names, or is NULL if the standard
1777 ELF names are used. */
1778
1779 int
1780 dwarf2_has_info (struct objfile *objfile,
1781 const struct dwarf2_debug_sections *names)
1782 {
1783 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1784 if (!dwarf2_per_objfile)
1785 {
1786 /* Initialize per-objfile state. */
1787 struct dwarf2_per_objfile *data
1788 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1789
1790 memset (data, 0, sizeof (*data));
1791 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1792 dwarf2_per_objfile = data;
1793
1794 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1795 (void *) names);
1796 dwarf2_per_objfile->objfile = objfile;
1797 }
1798 return (dwarf2_per_objfile->info.asection != NULL
1799 && dwarf2_per_objfile->abbrev.asection != NULL);
1800 }
1801
1802 /* When loading sections, we look either for uncompressed section or for
1803 compressed section names. */
1804
1805 static int
1806 section_is_p (const char *section_name,
1807 const struct dwarf2_section_names *names)
1808 {
1809 if (names->normal != NULL
1810 && strcmp (section_name, names->normal) == 0)
1811 return 1;
1812 if (names->compressed != NULL
1813 && strcmp (section_name, names->compressed) == 0)
1814 return 1;
1815 return 0;
1816 }
1817
1818 /* This function is mapped across the sections and remembers the
1819 offset and size of each of the debugging sections we are interested
1820 in. */
1821
1822 static void
1823 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1824 {
1825 const struct dwarf2_debug_sections *names;
1826 flagword aflag = bfd_get_section_flags (abfd, sectp);
1827
1828 if (vnames == NULL)
1829 names = &dwarf2_elf_names;
1830 else
1831 names = (const struct dwarf2_debug_sections *) vnames;
1832
1833 if ((aflag & SEC_HAS_CONTENTS) == 0)
1834 {
1835 }
1836 else if (section_is_p (sectp->name, &names->info))
1837 {
1838 dwarf2_per_objfile->info.asection = sectp;
1839 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1840 }
1841 else if (section_is_p (sectp->name, &names->abbrev))
1842 {
1843 dwarf2_per_objfile->abbrev.asection = sectp;
1844 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1845 }
1846 else if (section_is_p (sectp->name, &names->line))
1847 {
1848 dwarf2_per_objfile->line.asection = sectp;
1849 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1850 }
1851 else if (section_is_p (sectp->name, &names->loc))
1852 {
1853 dwarf2_per_objfile->loc.asection = sectp;
1854 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1855 }
1856 else if (section_is_p (sectp->name, &names->macinfo))
1857 {
1858 dwarf2_per_objfile->macinfo.asection = sectp;
1859 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1860 }
1861 else if (section_is_p (sectp->name, &names->macro))
1862 {
1863 dwarf2_per_objfile->macro.asection = sectp;
1864 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1865 }
1866 else if (section_is_p (sectp->name, &names->str))
1867 {
1868 dwarf2_per_objfile->str.asection = sectp;
1869 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1870 }
1871 else if (section_is_p (sectp->name, &names->addr))
1872 {
1873 dwarf2_per_objfile->addr.asection = sectp;
1874 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1875 }
1876 else if (section_is_p (sectp->name, &names->frame))
1877 {
1878 dwarf2_per_objfile->frame.asection = sectp;
1879 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1880 }
1881 else if (section_is_p (sectp->name, &names->eh_frame))
1882 {
1883 dwarf2_per_objfile->eh_frame.asection = sectp;
1884 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1885 }
1886 else if (section_is_p (sectp->name, &names->ranges))
1887 {
1888 dwarf2_per_objfile->ranges.asection = sectp;
1889 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1890 }
1891 else if (section_is_p (sectp->name, &names->types))
1892 {
1893 struct dwarf2_section_info type_section;
1894
1895 memset (&type_section, 0, sizeof (type_section));
1896 type_section.asection = sectp;
1897 type_section.size = bfd_get_section_size (sectp);
1898
1899 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1900 &type_section);
1901 }
1902 else if (section_is_p (sectp->name, &names->gdb_index))
1903 {
1904 dwarf2_per_objfile->gdb_index.asection = sectp;
1905 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1906 }
1907
1908 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1909 && bfd_section_vma (abfd, sectp) == 0)
1910 dwarf2_per_objfile->has_section_at_zero = 1;
1911 }
1912
1913 /* A helper function that decides whether a section is empty,
1914 or not present. */
1915
1916 static int
1917 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1918 {
1919 return info->asection == NULL || info->size == 0;
1920 }
1921
1922 /* Read the contents of the section INFO.
1923 OBJFILE is the main object file, but not necessarily the file where
1924 the section comes from. E.g., for DWO files INFO->asection->owner
1925 is the bfd of the DWO file.
1926 If the section is compressed, uncompress it before returning. */
1927
1928 static void
1929 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1930 {
1931 asection *sectp = info->asection;
1932 bfd *abfd;
1933 gdb_byte *buf, *retbuf;
1934 unsigned char header[4];
1935
1936 if (info->readin)
1937 return;
1938 info->buffer = NULL;
1939 info->readin = 1;
1940
1941 if (dwarf2_section_empty_p (info))
1942 return;
1943
1944 abfd = sectp->owner;
1945
1946 /* If the section has relocations, we must read it ourselves.
1947 Otherwise we attach it to the BFD. */
1948 if ((sectp->flags & SEC_RELOC) == 0)
1949 {
1950 const gdb_byte *bytes = gdb_bfd_map_section (sectp, &info->size);
1951
1952 /* We have to cast away const here for historical reasons.
1953 Fixing dwarf2read to be const-correct would be quite nice. */
1954 info->buffer = (gdb_byte *) bytes;
1955 return;
1956 }
1957
1958 buf = obstack_alloc (&objfile->objfile_obstack, info->size);
1959 info->buffer = buf;
1960
1961 /* When debugging .o files, we may need to apply relocations; see
1962 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1963 We never compress sections in .o files, so we only need to
1964 try this when the section is not compressed. */
1965 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1966 if (retbuf != NULL)
1967 {
1968 info->buffer = retbuf;
1969 return;
1970 }
1971
1972 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1973 || bfd_bread (buf, info->size, abfd) != info->size)
1974 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1975 bfd_get_filename (abfd));
1976 }
1977
1978 /* A helper function that returns the size of a section in a safe way.
1979 If you are positive that the section has been read before using the
1980 size, then it is safe to refer to the dwarf2_section_info object's
1981 "size" field directly. In other cases, you must call this
1982 function, because for compressed sections the size field is not set
1983 correctly until the section has been read. */
1984
1985 static bfd_size_type
1986 dwarf2_section_size (struct objfile *objfile,
1987 struct dwarf2_section_info *info)
1988 {
1989 if (!info->readin)
1990 dwarf2_read_section (objfile, info);
1991 return info->size;
1992 }
1993
1994 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1995 SECTION_NAME. */
1996
1997 void
1998 dwarf2_get_section_info (struct objfile *objfile,
1999 enum dwarf2_section_enum sect,
2000 asection **sectp, gdb_byte **bufp,
2001 bfd_size_type *sizep)
2002 {
2003 struct dwarf2_per_objfile *data
2004 = objfile_data (objfile, dwarf2_objfile_data_key);
2005 struct dwarf2_section_info *info;
2006
2007 /* We may see an objfile without any DWARF, in which case we just
2008 return nothing. */
2009 if (data == NULL)
2010 {
2011 *sectp = NULL;
2012 *bufp = NULL;
2013 *sizep = 0;
2014 return;
2015 }
2016 switch (sect)
2017 {
2018 case DWARF2_DEBUG_FRAME:
2019 info = &data->frame;
2020 break;
2021 case DWARF2_EH_FRAME:
2022 info = &data->eh_frame;
2023 break;
2024 default:
2025 gdb_assert_not_reached ("unexpected section");
2026 }
2027
2028 dwarf2_read_section (objfile, info);
2029
2030 *sectp = info->asection;
2031 *bufp = info->buffer;
2032 *sizep = info->size;
2033 }
2034
2035 /* A helper function to find the sections for a .dwz file. */
2036
2037 static void
2038 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2039 {
2040 struct dwz_file *dwz_file = arg;
2041
2042 /* Note that we only support the standard ELF names, because .dwz
2043 is ELF-only (at the time of writing). */
2044 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2045 {
2046 dwz_file->abbrev.asection = sectp;
2047 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2048 }
2049 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2050 {
2051 dwz_file->info.asection = sectp;
2052 dwz_file->info.size = bfd_get_section_size (sectp);
2053 }
2054 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2055 {
2056 dwz_file->str.asection = sectp;
2057 dwz_file->str.size = bfd_get_section_size (sectp);
2058 }
2059 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2060 {
2061 dwz_file->line.asection = sectp;
2062 dwz_file->line.size = bfd_get_section_size (sectp);
2063 }
2064 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2065 {
2066 dwz_file->macro.asection = sectp;
2067 dwz_file->macro.size = bfd_get_section_size (sectp);
2068 }
2069 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2070 {
2071 dwz_file->gdb_index.asection = sectp;
2072 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2073 }
2074 }
2075
2076 /* Open the separate '.dwz' debug file, if needed. Error if the file
2077 cannot be found. */
2078
2079 static struct dwz_file *
2080 dwarf2_get_dwz_file (void)
2081 {
2082 bfd *abfd, *dwz_bfd;
2083 asection *section;
2084 gdb_byte *data;
2085 struct cleanup *cleanup;
2086 const char *filename;
2087 struct dwz_file *result;
2088
2089 if (dwarf2_per_objfile->dwz_file != NULL)
2090 return dwarf2_per_objfile->dwz_file;
2091
2092 abfd = dwarf2_per_objfile->objfile->obfd;
2093 section = bfd_get_section_by_name (abfd, ".gnu_debugaltlink");
2094 if (section == NULL)
2095 error (_("could not find '.gnu_debugaltlink' section"));
2096 if (!bfd_malloc_and_get_section (abfd, section, &data))
2097 error (_("could not read '.gnu_debugaltlink' section: %s"),
2098 bfd_errmsg (bfd_get_error ()));
2099 cleanup = make_cleanup (xfree, data);
2100
2101 filename = data;
2102 if (!IS_ABSOLUTE_PATH (filename))
2103 {
2104 char *abs = gdb_realpath (dwarf2_per_objfile->objfile->name);
2105 char *rel;
2106
2107 make_cleanup (xfree, abs);
2108 abs = ldirname (abs);
2109 make_cleanup (xfree, abs);
2110
2111 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2112 make_cleanup (xfree, rel);
2113 filename = rel;
2114 }
2115
2116 /* The format is just a NUL-terminated file name, followed by the
2117 build-id. For now, though, we ignore the build-id. */
2118 dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2119 if (dwz_bfd == NULL)
2120 error (_("could not read '%s': %s"), filename,
2121 bfd_errmsg (bfd_get_error ()));
2122
2123 if (!bfd_check_format (dwz_bfd, bfd_object))
2124 {
2125 gdb_bfd_unref (dwz_bfd);
2126 error (_("file '%s' was not usable: %s"), filename,
2127 bfd_errmsg (bfd_get_error ()));
2128 }
2129
2130 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2131 struct dwz_file);
2132 result->dwz_bfd = dwz_bfd;
2133
2134 bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2135
2136 do_cleanups (cleanup);
2137
2138 dwarf2_per_objfile->dwz_file = result;
2139 return result;
2140 }
2141 \f
2142 /* DWARF quick_symbols_functions support. */
2143
2144 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2145 unique line tables, so we maintain a separate table of all .debug_line
2146 derived entries to support the sharing.
2147 All the quick functions need is the list of file names. We discard the
2148 line_header when we're done and don't need to record it here. */
2149 struct quick_file_names
2150 {
2151 /* The data used to construct the hash key. */
2152 struct stmt_list_hash hash;
2153
2154 /* The number of entries in file_names, real_names. */
2155 unsigned int num_file_names;
2156
2157 /* The file names from the line table, after being run through
2158 file_full_name. */
2159 const char **file_names;
2160
2161 /* The file names from the line table after being run through
2162 gdb_realpath. These are computed lazily. */
2163 const char **real_names;
2164 };
2165
2166 /* When using the index (and thus not using psymtabs), each CU has an
2167 object of this type. This is used to hold information needed by
2168 the various "quick" methods. */
2169 struct dwarf2_per_cu_quick_data
2170 {
2171 /* The file table. This can be NULL if there was no file table
2172 or it's currently not read in.
2173 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2174 struct quick_file_names *file_names;
2175
2176 /* The corresponding symbol table. This is NULL if symbols for this
2177 CU have not yet been read. */
2178 struct symtab *symtab;
2179
2180 /* A temporary mark bit used when iterating over all CUs in
2181 expand_symtabs_matching. */
2182 unsigned int mark : 1;
2183
2184 /* True if we've tried to read the file table and found there isn't one.
2185 There will be no point in trying to read it again next time. */
2186 unsigned int no_file_data : 1;
2187 };
2188
2189 /* Utility hash function for a stmt_list_hash. */
2190
2191 static hashval_t
2192 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2193 {
2194 hashval_t v = 0;
2195
2196 if (stmt_list_hash->dwo_unit != NULL)
2197 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2198 v += stmt_list_hash->line_offset.sect_off;
2199 return v;
2200 }
2201
2202 /* Utility equality function for a stmt_list_hash. */
2203
2204 static int
2205 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2206 const struct stmt_list_hash *rhs)
2207 {
2208 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2209 return 0;
2210 if (lhs->dwo_unit != NULL
2211 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2212 return 0;
2213
2214 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2215 }
2216
2217 /* Hash function for a quick_file_names. */
2218
2219 static hashval_t
2220 hash_file_name_entry (const void *e)
2221 {
2222 const struct quick_file_names *file_data = e;
2223
2224 return hash_stmt_list_entry (&file_data->hash);
2225 }
2226
2227 /* Equality function for a quick_file_names. */
2228
2229 static int
2230 eq_file_name_entry (const void *a, const void *b)
2231 {
2232 const struct quick_file_names *ea = a;
2233 const struct quick_file_names *eb = b;
2234
2235 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2236 }
2237
2238 /* Delete function for a quick_file_names. */
2239
2240 static void
2241 delete_file_name_entry (void *e)
2242 {
2243 struct quick_file_names *file_data = e;
2244 int i;
2245
2246 for (i = 0; i < file_data->num_file_names; ++i)
2247 {
2248 xfree ((void*) file_data->file_names[i]);
2249 if (file_data->real_names)
2250 xfree ((void*) file_data->real_names[i]);
2251 }
2252
2253 /* The space for the struct itself lives on objfile_obstack,
2254 so we don't free it here. */
2255 }
2256
2257 /* Create a quick_file_names hash table. */
2258
2259 static htab_t
2260 create_quick_file_names_table (unsigned int nr_initial_entries)
2261 {
2262 return htab_create_alloc (nr_initial_entries,
2263 hash_file_name_entry, eq_file_name_entry,
2264 delete_file_name_entry, xcalloc, xfree);
2265 }
2266
2267 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2268 have to be created afterwards. You should call age_cached_comp_units after
2269 processing PER_CU->CU. dw2_setup must have been already called. */
2270
2271 static void
2272 load_cu (struct dwarf2_per_cu_data *per_cu)
2273 {
2274 if (per_cu->is_debug_types)
2275 load_full_type_unit (per_cu);
2276 else
2277 load_full_comp_unit (per_cu, language_minimal);
2278
2279 gdb_assert (per_cu->cu != NULL);
2280
2281 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2282 }
2283
2284 /* Read in the symbols for PER_CU. */
2285
2286 static void
2287 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2288 {
2289 struct cleanup *back_to;
2290
2291 /* Skip type_unit_groups, reading the type units they contain
2292 is handled elsewhere. */
2293 if (IS_TYPE_UNIT_GROUP (per_cu))
2294 return;
2295
2296 back_to = make_cleanup (dwarf2_release_queue, NULL);
2297
2298 if (dwarf2_per_objfile->using_index
2299 ? per_cu->v.quick->symtab == NULL
2300 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2301 {
2302 queue_comp_unit (per_cu, language_minimal);
2303 load_cu (per_cu);
2304 }
2305
2306 process_queue ();
2307
2308 /* Age the cache, releasing compilation units that have not
2309 been used recently. */
2310 age_cached_comp_units ();
2311
2312 do_cleanups (back_to);
2313 }
2314
2315 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2316 the objfile from which this CU came. Returns the resulting symbol
2317 table. */
2318
2319 static struct symtab *
2320 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2321 {
2322 gdb_assert (dwarf2_per_objfile->using_index);
2323 if (!per_cu->v.quick->symtab)
2324 {
2325 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2326 increment_reading_symtab ();
2327 dw2_do_instantiate_symtab (per_cu);
2328 process_cu_includes ();
2329 do_cleanups (back_to);
2330 }
2331 return per_cu->v.quick->symtab;
2332 }
2333
2334 /* Return the CU given its index.
2335
2336 This is intended for loops like:
2337
2338 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2339 + dwarf2_per_objfile->n_type_units); ++i)
2340 {
2341 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2342
2343 ...;
2344 }
2345 */
2346
2347 static struct dwarf2_per_cu_data *
2348 dw2_get_cu (int index)
2349 {
2350 if (index >= dwarf2_per_objfile->n_comp_units)
2351 {
2352 index -= dwarf2_per_objfile->n_comp_units;
2353 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2354 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
2355 }
2356
2357 return dwarf2_per_objfile->all_comp_units[index];
2358 }
2359
2360 /* Return the primary CU given its index.
2361 The difference between this function and dw2_get_cu is in the handling
2362 of type units (TUs). Here we return the type_unit_group object.
2363
2364 This is intended for loops like:
2365
2366 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2367 + dwarf2_per_objfile->n_type_unit_groups); ++i)
2368 {
2369 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2370
2371 ...;
2372 }
2373 */
2374
2375 static struct dwarf2_per_cu_data *
2376 dw2_get_primary_cu (int index)
2377 {
2378 if (index >= dwarf2_per_objfile->n_comp_units)
2379 {
2380 index -= dwarf2_per_objfile->n_comp_units;
2381 gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2382 return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
2383 }
2384
2385 return dwarf2_per_objfile->all_comp_units[index];
2386 }
2387
2388 /* A helper for create_cus_from_index that handles a given list of
2389 CUs. */
2390
2391 static void
2392 create_cus_from_index_list (struct objfile *objfile,
2393 const gdb_byte *cu_list, offset_type n_elements,
2394 struct dwarf2_section_info *section,
2395 int is_dwz,
2396 int base_offset)
2397 {
2398 offset_type i;
2399
2400 for (i = 0; i < n_elements; i += 2)
2401 {
2402 struct dwarf2_per_cu_data *the_cu;
2403 ULONGEST offset, length;
2404
2405 gdb_static_assert (sizeof (ULONGEST) >= 8);
2406 offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2407 length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2408 cu_list += 2 * 8;
2409
2410 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2411 struct dwarf2_per_cu_data);
2412 the_cu->offset.sect_off = offset;
2413 the_cu->length = length;
2414 the_cu->objfile = objfile;
2415 the_cu->info_or_types_section = section;
2416 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2417 struct dwarf2_per_cu_quick_data);
2418 the_cu->is_dwz = is_dwz;
2419 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
2420 }
2421 }
2422
2423 /* Read the CU list from the mapped index, and use it to create all
2424 the CU objects for this objfile. */
2425
2426 static void
2427 create_cus_from_index (struct objfile *objfile,
2428 const gdb_byte *cu_list, offset_type cu_list_elements,
2429 const gdb_byte *dwz_list, offset_type dwz_elements)
2430 {
2431 struct dwz_file *dwz;
2432
2433 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2434 dwarf2_per_objfile->all_comp_units
2435 = obstack_alloc (&objfile->objfile_obstack,
2436 dwarf2_per_objfile->n_comp_units
2437 * sizeof (struct dwarf2_per_cu_data *));
2438
2439 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2440 &dwarf2_per_objfile->info, 0, 0);
2441
2442 if (dwz_elements == 0)
2443 return;
2444
2445 dwz = dwarf2_get_dwz_file ();
2446 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2447 cu_list_elements / 2);
2448 }
2449
2450 /* Create the signatured type hash table from the index. */
2451
2452 static void
2453 create_signatured_type_table_from_index (struct objfile *objfile,
2454 struct dwarf2_section_info *section,
2455 const gdb_byte *bytes,
2456 offset_type elements)
2457 {
2458 offset_type i;
2459 htab_t sig_types_hash;
2460
2461 dwarf2_per_objfile->n_type_units = elements / 3;
2462 dwarf2_per_objfile->all_type_units
2463 = obstack_alloc (&objfile->objfile_obstack,
2464 dwarf2_per_objfile->n_type_units
2465 * sizeof (struct signatured_type *));
2466
2467 sig_types_hash = allocate_signatured_type_table (objfile);
2468
2469 for (i = 0; i < elements; i += 3)
2470 {
2471 struct signatured_type *sig_type;
2472 ULONGEST offset, type_offset_in_tu, signature;
2473 void **slot;
2474
2475 gdb_static_assert (sizeof (ULONGEST) >= 8);
2476 offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2477 type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
2478 BFD_ENDIAN_LITTLE);
2479 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2480 bytes += 3 * 8;
2481
2482 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2483 struct signatured_type);
2484 sig_type->signature = signature;
2485 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2486 sig_type->per_cu.is_debug_types = 1;
2487 sig_type->per_cu.info_or_types_section = section;
2488 sig_type->per_cu.offset.sect_off = offset;
2489 sig_type->per_cu.objfile = objfile;
2490 sig_type->per_cu.v.quick
2491 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2492 struct dwarf2_per_cu_quick_data);
2493
2494 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2495 *slot = sig_type;
2496
2497 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
2498 }
2499
2500 dwarf2_per_objfile->signatured_types = sig_types_hash;
2501 }
2502
2503 /* Read the address map data from the mapped index, and use it to
2504 populate the objfile's psymtabs_addrmap. */
2505
2506 static void
2507 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2508 {
2509 const gdb_byte *iter, *end;
2510 struct obstack temp_obstack;
2511 struct addrmap *mutable_map;
2512 struct cleanup *cleanup;
2513 CORE_ADDR baseaddr;
2514
2515 obstack_init (&temp_obstack);
2516 cleanup = make_cleanup_obstack_free (&temp_obstack);
2517 mutable_map = addrmap_create_mutable (&temp_obstack);
2518
2519 iter = index->address_table;
2520 end = iter + index->address_table_size;
2521
2522 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2523
2524 while (iter < end)
2525 {
2526 ULONGEST hi, lo, cu_index;
2527 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2528 iter += 8;
2529 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2530 iter += 8;
2531 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2532 iter += 4;
2533
2534 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2535 dw2_get_cu (cu_index));
2536 }
2537
2538 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2539 &objfile->objfile_obstack);
2540 do_cleanups (cleanup);
2541 }
2542
2543 /* The hash function for strings in the mapped index. This is the same as
2544 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2545 implementation. This is necessary because the hash function is tied to the
2546 format of the mapped index file. The hash values do not have to match with
2547 SYMBOL_HASH_NEXT.
2548
2549 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2550
2551 static hashval_t
2552 mapped_index_string_hash (int index_version, const void *p)
2553 {
2554 const unsigned char *str = (const unsigned char *) p;
2555 hashval_t r = 0;
2556 unsigned char c;
2557
2558 while ((c = *str++) != 0)
2559 {
2560 if (index_version >= 5)
2561 c = tolower (c);
2562 r = r * 67 + c - 113;
2563 }
2564
2565 return r;
2566 }
2567
2568 /* Find a slot in the mapped index INDEX for the object named NAME.
2569 If NAME is found, set *VEC_OUT to point to the CU vector in the
2570 constant pool and return 1. If NAME cannot be found, return 0. */
2571
2572 static int
2573 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2574 offset_type **vec_out)
2575 {
2576 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2577 offset_type hash;
2578 offset_type slot, step;
2579 int (*cmp) (const char *, const char *);
2580
2581 if (current_language->la_language == language_cplus
2582 || current_language->la_language == language_java
2583 || current_language->la_language == language_fortran)
2584 {
2585 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2586 not contain any. */
2587 const char *paren = strchr (name, '(');
2588
2589 if (paren)
2590 {
2591 char *dup;
2592
2593 dup = xmalloc (paren - name + 1);
2594 memcpy (dup, name, paren - name);
2595 dup[paren - name] = 0;
2596
2597 make_cleanup (xfree, dup);
2598 name = dup;
2599 }
2600 }
2601
2602 /* Index version 4 did not support case insensitive searches. But the
2603 indices for case insensitive languages are built in lowercase, therefore
2604 simulate our NAME being searched is also lowercased. */
2605 hash = mapped_index_string_hash ((index->version == 4
2606 && case_sensitivity == case_sensitive_off
2607 ? 5 : index->version),
2608 name);
2609
2610 slot = hash & (index->symbol_table_slots - 1);
2611 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2612 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2613
2614 for (;;)
2615 {
2616 /* Convert a slot number to an offset into the table. */
2617 offset_type i = 2 * slot;
2618 const char *str;
2619 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2620 {
2621 do_cleanups (back_to);
2622 return 0;
2623 }
2624
2625 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2626 if (!cmp (name, str))
2627 {
2628 *vec_out = (offset_type *) (index->constant_pool
2629 + MAYBE_SWAP (index->symbol_table[i + 1]));
2630 do_cleanups (back_to);
2631 return 1;
2632 }
2633
2634 slot = (slot + step) & (index->symbol_table_slots - 1);
2635 }
2636 }
2637
2638 /* A helper function that reads the .gdb_index from SECTION and fills
2639 in MAP. FILENAME is the name of the file containing the section;
2640 it is used for error reporting. DEPRECATED_OK is nonzero if it is
2641 ok to use deprecated sections.
2642
2643 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2644 out parameters that are filled in with information about the CU and
2645 TU lists in the section.
2646
2647 Returns 1 if all went well, 0 otherwise. */
2648
2649 static int
2650 read_index_from_section (struct objfile *objfile,
2651 const char *filename,
2652 int deprecated_ok,
2653 struct dwarf2_section_info *section,
2654 struct mapped_index *map,
2655 const gdb_byte **cu_list,
2656 offset_type *cu_list_elements,
2657 const gdb_byte **types_list,
2658 offset_type *types_list_elements)
2659 {
2660 char *addr;
2661 offset_type version;
2662 offset_type *metadata;
2663 int i;
2664
2665 if (dwarf2_section_empty_p (section))
2666 return 0;
2667
2668 /* Older elfutils strip versions could keep the section in the main
2669 executable while splitting it for the separate debug info file. */
2670 if ((bfd_get_file_flags (section->asection) & SEC_HAS_CONTENTS) == 0)
2671 return 0;
2672
2673 dwarf2_read_section (objfile, section);
2674
2675 addr = section->buffer;
2676 /* Version check. */
2677 version = MAYBE_SWAP (*(offset_type *) addr);
2678 /* Versions earlier than 3 emitted every copy of a psymbol. This
2679 causes the index to behave very poorly for certain requests. Version 3
2680 contained incomplete addrmap. So, it seems better to just ignore such
2681 indices. */
2682 if (version < 4)
2683 {
2684 static int warning_printed = 0;
2685 if (!warning_printed)
2686 {
2687 warning (_("Skipping obsolete .gdb_index section in %s."),
2688 filename);
2689 warning_printed = 1;
2690 }
2691 return 0;
2692 }
2693 /* Index version 4 uses a different hash function than index version
2694 5 and later.
2695
2696 Versions earlier than 6 did not emit psymbols for inlined
2697 functions. Using these files will cause GDB not to be able to
2698 set breakpoints on inlined functions by name, so we ignore these
2699 indices unless the user has done
2700 "set use-deprecated-index-sections on". */
2701 if (version < 6 && !deprecated_ok)
2702 {
2703 static int warning_printed = 0;
2704 if (!warning_printed)
2705 {
2706 warning (_("\
2707 Skipping deprecated .gdb_index section in %s.\n\
2708 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2709 to use the section anyway."),
2710 filename);
2711 warning_printed = 1;
2712 }
2713 return 0;
2714 }
2715 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2716 of the TU (for symbols coming from TUs). It's just a performance bug, and
2717 we can't distinguish gdb-generated indices from gold-generated ones, so
2718 nothing to do here. */
2719
2720 /* Indexes with higher version than the one supported by GDB may be no
2721 longer backward compatible. */
2722 if (version > 8)
2723 return 0;
2724
2725 map->version = version;
2726 map->total_size = section->size;
2727
2728 metadata = (offset_type *) (addr + sizeof (offset_type));
2729
2730 i = 0;
2731 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2732 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2733 / 8);
2734 ++i;
2735
2736 *types_list = addr + MAYBE_SWAP (metadata[i]);
2737 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2738 - MAYBE_SWAP (metadata[i]))
2739 / 8);
2740 ++i;
2741
2742 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2743 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2744 - MAYBE_SWAP (metadata[i]));
2745 ++i;
2746
2747 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2748 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2749 - MAYBE_SWAP (metadata[i]))
2750 / (2 * sizeof (offset_type)));
2751 ++i;
2752
2753 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2754
2755 return 1;
2756 }
2757
2758
2759 /* Read the index file. If everything went ok, initialize the "quick"
2760 elements of all the CUs and return 1. Otherwise, return 0. */
2761
2762 static int
2763 dwarf2_read_index (struct objfile *objfile)
2764 {
2765 struct mapped_index local_map, *map;
2766 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2767 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2768
2769 if (!read_index_from_section (objfile, objfile->name,
2770 use_deprecated_index_sections,
2771 &dwarf2_per_objfile->gdb_index, &local_map,
2772 &cu_list, &cu_list_elements,
2773 &types_list, &types_list_elements))
2774 return 0;
2775
2776 /* Don't use the index if it's empty. */
2777 if (local_map.symbol_table_slots == 0)
2778 return 0;
2779
2780 /* If there is a .dwz file, read it so we can get its CU list as
2781 well. */
2782 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
2783 {
2784 struct dwz_file *dwz = dwarf2_get_dwz_file ();
2785 struct mapped_index dwz_map;
2786 const gdb_byte *dwz_types_ignore;
2787 offset_type dwz_types_elements_ignore;
2788
2789 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2790 1,
2791 &dwz->gdb_index, &dwz_map,
2792 &dwz_list, &dwz_list_elements,
2793 &dwz_types_ignore,
2794 &dwz_types_elements_ignore))
2795 {
2796 warning (_("could not read '.gdb_index' section from %s; skipping"),
2797 bfd_get_filename (dwz->dwz_bfd));
2798 return 0;
2799 }
2800 }
2801
2802 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
2803 dwz_list_elements);
2804
2805 if (types_list_elements)
2806 {
2807 struct dwarf2_section_info *section;
2808
2809 /* We can only handle a single .debug_types when we have an
2810 index. */
2811 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2812 return 0;
2813
2814 section = VEC_index (dwarf2_section_info_def,
2815 dwarf2_per_objfile->types, 0);
2816
2817 create_signatured_type_table_from_index (objfile, section, types_list,
2818 types_list_elements);
2819 }
2820
2821 create_addrmap_from_index (objfile, &local_map);
2822
2823 map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2824 *map = local_map;
2825
2826 dwarf2_per_objfile->index_table = map;
2827 dwarf2_per_objfile->using_index = 1;
2828 dwarf2_per_objfile->quick_file_names_table =
2829 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2830
2831 return 1;
2832 }
2833
2834 /* A helper for the "quick" functions which sets the global
2835 dwarf2_per_objfile according to OBJFILE. */
2836
2837 static void
2838 dw2_setup (struct objfile *objfile)
2839 {
2840 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2841 gdb_assert (dwarf2_per_objfile);
2842 }
2843
2844 /* die_reader_func for dw2_get_file_names. */
2845
2846 static void
2847 dw2_get_file_names_reader (const struct die_reader_specs *reader,
2848 gdb_byte *info_ptr,
2849 struct die_info *comp_unit_die,
2850 int has_children,
2851 void *data)
2852 {
2853 struct dwarf2_cu *cu = reader->cu;
2854 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2855 struct objfile *objfile = dwarf2_per_objfile->objfile;
2856 struct dwarf2_per_cu_data *lh_cu;
2857 struct line_header *lh;
2858 struct attribute *attr;
2859 int i;
2860 const char *name, *comp_dir;
2861 void **slot;
2862 struct quick_file_names *qfn;
2863 unsigned int line_offset;
2864
2865 /* Our callers never want to match partial units -- instead they
2866 will match the enclosing full CU. */
2867 if (comp_unit_die->tag == DW_TAG_partial_unit)
2868 {
2869 this_cu->v.quick->no_file_data = 1;
2870 return;
2871 }
2872
2873 /* If we're reading the line header for TUs, store it in the "per_cu"
2874 for tu_group. */
2875 if (this_cu->is_debug_types)
2876 {
2877 struct type_unit_group *tu_group = data;
2878
2879 gdb_assert (tu_group != NULL);
2880 lh_cu = &tu_group->per_cu;
2881 }
2882 else
2883 lh_cu = this_cu;
2884
2885 lh = NULL;
2886 slot = NULL;
2887 line_offset = 0;
2888
2889 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2890 if (attr)
2891 {
2892 struct quick_file_names find_entry;
2893
2894 line_offset = DW_UNSND (attr);
2895
2896 /* We may have already read in this line header (TU line header sharing).
2897 If we have we're done. */
2898 find_entry.hash.dwo_unit = cu->dwo_unit;
2899 find_entry.hash.line_offset.sect_off = line_offset;
2900 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2901 &find_entry, INSERT);
2902 if (*slot != NULL)
2903 {
2904 lh_cu->v.quick->file_names = *slot;
2905 return;
2906 }
2907
2908 lh = dwarf_decode_line_header (line_offset, cu);
2909 }
2910 if (lh == NULL)
2911 {
2912 lh_cu->v.quick->no_file_data = 1;
2913 return;
2914 }
2915
2916 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2917 qfn->hash.dwo_unit = cu->dwo_unit;
2918 qfn->hash.line_offset.sect_off = line_offset;
2919 gdb_assert (slot != NULL);
2920 *slot = qfn;
2921
2922 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
2923
2924 qfn->num_file_names = lh->num_file_names;
2925 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2926 lh->num_file_names * sizeof (char *));
2927 for (i = 0; i < lh->num_file_names; ++i)
2928 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2929 qfn->real_names = NULL;
2930
2931 free_line_header (lh);
2932
2933 lh_cu->v.quick->file_names = qfn;
2934 }
2935
2936 /* A helper for the "quick" functions which attempts to read the line
2937 table for THIS_CU. */
2938
2939 static struct quick_file_names *
2940 dw2_get_file_names (struct objfile *objfile,
2941 struct dwarf2_per_cu_data *this_cu)
2942 {
2943 /* For TUs this should only be called on the parent group. */
2944 if (this_cu->is_debug_types)
2945 gdb_assert (IS_TYPE_UNIT_GROUP (this_cu));
2946
2947 if (this_cu->v.quick->file_names != NULL)
2948 return this_cu->v.quick->file_names;
2949 /* If we know there is no line data, no point in looking again. */
2950 if (this_cu->v.quick->no_file_data)
2951 return NULL;
2952
2953 /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2954 in the stub for CUs, there's is no need to lookup the DWO file.
2955 However, that's not the case for TUs where DW_AT_stmt_list lives in the
2956 DWO file. */
2957 if (this_cu->is_debug_types)
2958 {
2959 struct type_unit_group *tu_group = this_cu->type_unit_group;
2960
2961 init_cutu_and_read_dies (tu_group->t.first_tu, NULL, 0, 0,
2962 dw2_get_file_names_reader, tu_group);
2963 }
2964 else
2965 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
2966
2967 if (this_cu->v.quick->no_file_data)
2968 return NULL;
2969 return this_cu->v.quick->file_names;
2970 }
2971
2972 /* A helper for the "quick" functions which computes and caches the
2973 real path for a given file name from the line table. */
2974
2975 static const char *
2976 dw2_get_real_path (struct objfile *objfile,
2977 struct quick_file_names *qfn, int index)
2978 {
2979 if (qfn->real_names == NULL)
2980 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2981 qfn->num_file_names, sizeof (char *));
2982
2983 if (qfn->real_names[index] == NULL)
2984 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2985
2986 return qfn->real_names[index];
2987 }
2988
2989 static struct symtab *
2990 dw2_find_last_source_symtab (struct objfile *objfile)
2991 {
2992 int index;
2993
2994 dw2_setup (objfile);
2995 index = dwarf2_per_objfile->n_comp_units - 1;
2996 return dw2_instantiate_symtab (dw2_get_cu (index));
2997 }
2998
2999 /* Traversal function for dw2_forget_cached_source_info. */
3000
3001 static int
3002 dw2_free_cached_file_names (void **slot, void *info)
3003 {
3004 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3005
3006 if (file_data->real_names)
3007 {
3008 int i;
3009
3010 for (i = 0; i < file_data->num_file_names; ++i)
3011 {
3012 xfree ((void*) file_data->real_names[i]);
3013 file_data->real_names[i] = NULL;
3014 }
3015 }
3016
3017 return 1;
3018 }
3019
3020 static void
3021 dw2_forget_cached_source_info (struct objfile *objfile)
3022 {
3023 dw2_setup (objfile);
3024
3025 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3026 dw2_free_cached_file_names, NULL);
3027 }
3028
3029 /* Helper function for dw2_map_symtabs_matching_filename that expands
3030 the symtabs and calls the iterator. */
3031
3032 static int
3033 dw2_map_expand_apply (struct objfile *objfile,
3034 struct dwarf2_per_cu_data *per_cu,
3035 const char *name, const char *real_path,
3036 int (*callback) (struct symtab *, void *),
3037 void *data)
3038 {
3039 struct symtab *last_made = objfile->symtabs;
3040
3041 /* Don't visit already-expanded CUs. */
3042 if (per_cu->v.quick->symtab)
3043 return 0;
3044
3045 /* This may expand more than one symtab, and we want to iterate over
3046 all of them. */
3047 dw2_instantiate_symtab (per_cu);
3048
3049 return iterate_over_some_symtabs (name, real_path, callback, data,
3050 objfile->symtabs, last_made);
3051 }
3052
3053 /* Implementation of the map_symtabs_matching_filename method. */
3054
3055 static int
3056 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
3057 const char *real_path,
3058 int (*callback) (struct symtab *, void *),
3059 void *data)
3060 {
3061 int i;
3062 const char *name_basename = lbasename (name);
3063
3064 dw2_setup (objfile);
3065
3066 /* The rule is CUs specify all the files, including those used by
3067 any TU, so there's no need to scan TUs here. */
3068
3069 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3070 {
3071 int j;
3072 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3073 struct quick_file_names *file_data;
3074
3075 /* We only need to look at symtabs not already expanded. */
3076 if (per_cu->v.quick->symtab)
3077 continue;
3078
3079 file_data = dw2_get_file_names (objfile, per_cu);
3080 if (file_data == NULL)
3081 continue;
3082
3083 for (j = 0; j < file_data->num_file_names; ++j)
3084 {
3085 const char *this_name = file_data->file_names[j];
3086 const char *this_real_name;
3087
3088 if (compare_filenames_for_search (this_name, name))
3089 {
3090 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3091 callback, data))
3092 return 1;
3093 continue;
3094 }
3095
3096 /* Before we invoke realpath, which can get expensive when many
3097 files are involved, do a quick comparison of the basenames. */
3098 if (! basenames_may_differ
3099 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3100 continue;
3101
3102 this_real_name = dw2_get_real_path (objfile, file_data, j);
3103 if (compare_filenames_for_search (this_real_name, name))
3104 {
3105 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3106 callback, data))
3107 return 1;
3108 continue;
3109 }
3110
3111 if (real_path != NULL)
3112 {
3113 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3114 gdb_assert (IS_ABSOLUTE_PATH (name));
3115 if (this_real_name != NULL
3116 && FILENAME_CMP (real_path, this_real_name) == 0)
3117 {
3118 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3119 callback, data))
3120 return 1;
3121 continue;
3122 }
3123 }
3124 }
3125 }
3126
3127 return 0;
3128 }
3129
3130 /* Struct used to manage iterating over all CUs looking for a symbol. */
3131
3132 struct dw2_symtab_iterator
3133 {
3134 /* The internalized form of .gdb_index. */
3135 struct mapped_index *index;
3136 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3137 int want_specific_block;
3138 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3139 Unused if !WANT_SPECIFIC_BLOCK. */
3140 int block_index;
3141 /* The kind of symbol we're looking for. */
3142 domain_enum domain;
3143 /* The list of CUs from the index entry of the symbol,
3144 or NULL if not found. */
3145 offset_type *vec;
3146 /* The next element in VEC to look at. */
3147 int next;
3148 /* The number of elements in VEC, or zero if there is no match. */
3149 int length;
3150 };
3151
3152 /* Initialize the index symtab iterator ITER.
3153 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3154 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
3155
3156 static void
3157 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3158 struct mapped_index *index,
3159 int want_specific_block,
3160 int block_index,
3161 domain_enum domain,
3162 const char *name)
3163 {
3164 iter->index = index;
3165 iter->want_specific_block = want_specific_block;
3166 iter->block_index = block_index;
3167 iter->domain = domain;
3168 iter->next = 0;
3169
3170 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3171 iter->length = MAYBE_SWAP (*iter->vec);
3172 else
3173 {
3174 iter->vec = NULL;
3175 iter->length = 0;
3176 }
3177 }
3178
3179 /* Return the next matching CU or NULL if there are no more. */
3180
3181 static struct dwarf2_per_cu_data *
3182 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3183 {
3184 for ( ; iter->next < iter->length; ++iter->next)
3185 {
3186 offset_type cu_index_and_attrs =
3187 MAYBE_SWAP (iter->vec[iter->next + 1]);
3188 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3189 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
3190 int want_static = iter->block_index != GLOBAL_BLOCK;
3191 /* This value is only valid for index versions >= 7. */
3192 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3193 gdb_index_symbol_kind symbol_kind =
3194 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3195 /* Only check the symbol attributes if they're present.
3196 Indices prior to version 7 don't record them,
3197 and indices >= 7 may elide them for certain symbols
3198 (gold does this). */
3199 int attrs_valid =
3200 (iter->index->version >= 7
3201 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3202
3203 /* Skip if already read in. */
3204 if (per_cu->v.quick->symtab)
3205 continue;
3206
3207 if (attrs_valid
3208 && iter->want_specific_block
3209 && want_static != is_static)
3210 continue;
3211
3212 /* Only check the symbol's kind if it has one. */
3213 if (attrs_valid)
3214 {
3215 switch (iter->domain)
3216 {
3217 case VAR_DOMAIN:
3218 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3219 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3220 /* Some types are also in VAR_DOMAIN. */
3221 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3222 continue;
3223 break;
3224 case STRUCT_DOMAIN:
3225 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3226 continue;
3227 break;
3228 case LABEL_DOMAIN:
3229 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3230 continue;
3231 break;
3232 default:
3233 break;
3234 }
3235 }
3236
3237 ++iter->next;
3238 return per_cu;
3239 }
3240
3241 return NULL;
3242 }
3243
3244 static struct symtab *
3245 dw2_lookup_symbol (struct objfile *objfile, int block_index,
3246 const char *name, domain_enum domain)
3247 {
3248 struct symtab *stab_best = NULL;
3249 struct mapped_index *index;
3250
3251 dw2_setup (objfile);
3252
3253 index = dwarf2_per_objfile->index_table;
3254
3255 /* index is NULL if OBJF_READNOW. */
3256 if (index)
3257 {
3258 struct dw2_symtab_iterator iter;
3259 struct dwarf2_per_cu_data *per_cu;
3260
3261 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
3262
3263 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3264 {
3265 struct symbol *sym = NULL;
3266 struct symtab *stab = dw2_instantiate_symtab (per_cu);
3267
3268 /* Some caution must be observed with overloaded functions
3269 and methods, since the index will not contain any overload
3270 information (but NAME might contain it). */
3271 if (stab->primary)
3272 {
3273 struct blockvector *bv = BLOCKVECTOR (stab);
3274 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3275
3276 sym = lookup_block_symbol (block, name, domain);
3277 }
3278
3279 if (sym && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3280 {
3281 if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
3282 return stab;
3283
3284 stab_best = stab;
3285 }
3286
3287 /* Keep looking through other CUs. */
3288 }
3289 }
3290
3291 return stab_best;
3292 }
3293
3294 static void
3295 dw2_print_stats (struct objfile *objfile)
3296 {
3297 int i, count;
3298
3299 dw2_setup (objfile);
3300 count = 0;
3301 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3302 + dwarf2_per_objfile->n_type_units); ++i)
3303 {
3304 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3305
3306 if (!per_cu->v.quick->symtab)
3307 ++count;
3308 }
3309 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3310 }
3311
3312 static void
3313 dw2_dump (struct objfile *objfile)
3314 {
3315 /* Nothing worth printing. */
3316 }
3317
3318 static void
3319 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
3320 struct section_offsets *delta)
3321 {
3322 /* There's nothing to relocate here. */
3323 }
3324
3325 static void
3326 dw2_expand_symtabs_for_function (struct objfile *objfile,
3327 const char *func_name)
3328 {
3329 struct mapped_index *index;
3330
3331 dw2_setup (objfile);
3332
3333 index = dwarf2_per_objfile->index_table;
3334
3335 /* index is NULL if OBJF_READNOW. */
3336 if (index)
3337 {
3338 struct dw2_symtab_iterator iter;
3339 struct dwarf2_per_cu_data *per_cu;
3340
3341 /* Note: It doesn't matter what we pass for block_index here. */
3342 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3343 func_name);
3344
3345 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3346 dw2_instantiate_symtab (per_cu);
3347 }
3348 }
3349
3350 static void
3351 dw2_expand_all_symtabs (struct objfile *objfile)
3352 {
3353 int i;
3354
3355 dw2_setup (objfile);
3356
3357 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3358 + dwarf2_per_objfile->n_type_units); ++i)
3359 {
3360 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3361
3362 dw2_instantiate_symtab (per_cu);
3363 }
3364 }
3365
3366 static void
3367 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3368 const char *fullname)
3369 {
3370 int i;
3371
3372 dw2_setup (objfile);
3373
3374 /* We don't need to consider type units here.
3375 This is only called for examining code, e.g. expand_line_sal.
3376 There can be an order of magnitude (or more) more type units
3377 than comp units, and we avoid them if we can. */
3378
3379 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3380 {
3381 int j;
3382 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3383 struct quick_file_names *file_data;
3384
3385 /* We only need to look at symtabs not already expanded. */
3386 if (per_cu->v.quick->symtab)
3387 continue;
3388
3389 file_data = dw2_get_file_names (objfile, per_cu);
3390 if (file_data == NULL)
3391 continue;
3392
3393 for (j = 0; j < file_data->num_file_names; ++j)
3394 {
3395 const char *this_fullname = file_data->file_names[j];
3396
3397 if (filename_cmp (this_fullname, fullname) == 0)
3398 {
3399 dw2_instantiate_symtab (per_cu);
3400 break;
3401 }
3402 }
3403 }
3404 }
3405
3406 /* A helper function for dw2_find_symbol_file that finds the primary
3407 file name for a given CU. This is a die_reader_func. */
3408
3409 static void
3410 dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
3411 gdb_byte *info_ptr,
3412 struct die_info *comp_unit_die,
3413 int has_children,
3414 void *data)
3415 {
3416 const char **result_ptr = data;
3417 struct dwarf2_cu *cu = reader->cu;
3418 struct attribute *attr;
3419
3420 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3421 if (attr == NULL)
3422 *result_ptr = NULL;
3423 else
3424 *result_ptr = DW_STRING (attr);
3425 }
3426
3427 static const char *
3428 dw2_find_symbol_file (struct objfile *objfile, const char *name)
3429 {
3430 struct dwarf2_per_cu_data *per_cu;
3431 offset_type *vec;
3432 const char *filename;
3433
3434 dw2_setup (objfile);
3435
3436 /* index_table is NULL if OBJF_READNOW. */
3437 if (!dwarf2_per_objfile->index_table)
3438 {
3439 struct symtab *s;
3440
3441 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
3442 {
3443 struct blockvector *bv = BLOCKVECTOR (s);
3444 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
3445 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
3446
3447 if (sym)
3448 {
3449 /* Only file extension of returned filename is recognized. */
3450 return SYMBOL_SYMTAB (sym)->filename;
3451 }
3452 }
3453 return NULL;
3454 }
3455
3456 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
3457 name, &vec))
3458 return NULL;
3459
3460 /* Note that this just looks at the very first one named NAME -- but
3461 actually we are looking for a function. find_main_filename
3462 should be rewritten so that it doesn't require a custom hook. It
3463 could just use the ordinary symbol tables. */
3464 /* vec[0] is the length, which must always be >0. */
3465 per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
3466
3467 if (per_cu->v.quick->symtab != NULL)
3468 {
3469 /* Only file extension of returned filename is recognized. */
3470 return per_cu->v.quick->symtab->filename;
3471 }
3472
3473 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
3474 dw2_get_primary_filename_reader, &filename);
3475
3476 /* Only file extension of returned filename is recognized. */
3477 return filename;
3478 }
3479
3480 static void
3481 dw2_map_matching_symbols (const char * name, domain_enum namespace,
3482 struct objfile *objfile, int global,
3483 int (*callback) (struct block *,
3484 struct symbol *, void *),
3485 void *data, symbol_compare_ftype *match,
3486 symbol_compare_ftype *ordered_compare)
3487 {
3488 /* Currently unimplemented; used for Ada. The function can be called if the
3489 current language is Ada for a non-Ada objfile using GNU index. As Ada
3490 does not look for non-Ada symbols this function should just return. */
3491 }
3492
3493 static void
3494 dw2_expand_symtabs_matching
3495 (struct objfile *objfile,
3496 int (*file_matcher) (const char *, void *, int basenames),
3497 int (*name_matcher) (const char *, void *),
3498 enum search_domain kind,
3499 void *data)
3500 {
3501 int i;
3502 offset_type iter;
3503 struct mapped_index *index;
3504
3505 dw2_setup (objfile);
3506
3507 /* index_table is NULL if OBJF_READNOW. */
3508 if (!dwarf2_per_objfile->index_table)
3509 return;
3510 index = dwarf2_per_objfile->index_table;
3511
3512 if (file_matcher != NULL)
3513 {
3514 struct cleanup *cleanup;
3515 htab_t visited_found, visited_not_found;
3516
3517 visited_found = htab_create_alloc (10,
3518 htab_hash_pointer, htab_eq_pointer,
3519 NULL, xcalloc, xfree);
3520 cleanup = make_cleanup_htab_delete (visited_found);
3521 visited_not_found = htab_create_alloc (10,
3522 htab_hash_pointer, htab_eq_pointer,
3523 NULL, xcalloc, xfree);
3524 make_cleanup_htab_delete (visited_not_found);
3525
3526 /* The rule is CUs specify all the files, including those used by
3527 any TU, so there's no need to scan TUs here. */
3528
3529 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3530 {
3531 int j;
3532 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3533 struct quick_file_names *file_data;
3534 void **slot;
3535
3536 per_cu->v.quick->mark = 0;
3537
3538 /* We only need to look at symtabs not already expanded. */
3539 if (per_cu->v.quick->symtab)
3540 continue;
3541
3542 file_data = dw2_get_file_names (objfile, per_cu);
3543 if (file_data == NULL)
3544 continue;
3545
3546 if (htab_find (visited_not_found, file_data) != NULL)
3547 continue;
3548 else if (htab_find (visited_found, file_data) != NULL)
3549 {
3550 per_cu->v.quick->mark = 1;
3551 continue;
3552 }
3553
3554 for (j = 0; j < file_data->num_file_names; ++j)
3555 {
3556 const char *this_real_name;
3557
3558 if (file_matcher (file_data->file_names[j], data, 0))
3559 {
3560 per_cu->v.quick->mark = 1;
3561 break;
3562 }
3563
3564 /* Before we invoke realpath, which can get expensive when many
3565 files are involved, do a quick comparison of the basenames. */
3566 if (!basenames_may_differ
3567 && !file_matcher (lbasename (file_data->file_names[j]),
3568 data, 1))
3569 continue;
3570
3571 this_real_name = dw2_get_real_path (objfile, file_data, j);
3572 if (file_matcher (this_real_name, data, 0))
3573 {
3574 per_cu->v.quick->mark = 1;
3575 break;
3576 }
3577 }
3578
3579 slot = htab_find_slot (per_cu->v.quick->mark
3580 ? visited_found
3581 : visited_not_found,
3582 file_data, INSERT);
3583 *slot = file_data;
3584 }
3585
3586 do_cleanups (cleanup);
3587 }
3588
3589 for (iter = 0; iter < index->symbol_table_slots; ++iter)
3590 {
3591 offset_type idx = 2 * iter;
3592 const char *name;
3593 offset_type *vec, vec_len, vec_idx;
3594
3595 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
3596 continue;
3597
3598 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
3599
3600 if (! (*name_matcher) (name, data))
3601 continue;
3602
3603 /* The name was matched, now expand corresponding CUs that were
3604 marked. */
3605 vec = (offset_type *) (index->constant_pool
3606 + MAYBE_SWAP (index->symbol_table[idx + 1]));
3607 vec_len = MAYBE_SWAP (vec[0]);
3608 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3609 {
3610 struct dwarf2_per_cu_data *per_cu;
3611 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3612 gdb_index_symbol_kind symbol_kind =
3613 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3614 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3615
3616 /* Don't crash on bad data. */
3617 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3618 + dwarf2_per_objfile->n_type_units))
3619 continue;
3620
3621 /* Only check the symbol's kind if it has one.
3622 Indices prior to version 7 don't record it. */
3623 if (index->version >= 7)
3624 {
3625 switch (kind)
3626 {
3627 case VARIABLES_DOMAIN:
3628 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3629 continue;
3630 break;
3631 case FUNCTIONS_DOMAIN:
3632 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3633 continue;
3634 break;
3635 case TYPES_DOMAIN:
3636 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3637 continue;
3638 break;
3639 default:
3640 break;
3641 }
3642 }
3643
3644 per_cu = dw2_get_cu (cu_index);
3645 if (file_matcher == NULL || per_cu->v.quick->mark)
3646 dw2_instantiate_symtab (per_cu);
3647 }
3648 }
3649 }
3650
3651 /* A helper for dw2_find_pc_sect_symtab which finds the most specific
3652 symtab. */
3653
3654 static struct symtab *
3655 recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3656 {
3657 int i;
3658
3659 if (BLOCKVECTOR (symtab) != NULL
3660 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3661 return symtab;
3662
3663 if (symtab->includes == NULL)
3664 return NULL;
3665
3666 for (i = 0; symtab->includes[i]; ++i)
3667 {
3668 struct symtab *s = symtab->includes[i];
3669
3670 s = recursively_find_pc_sect_symtab (s, pc);
3671 if (s != NULL)
3672 return s;
3673 }
3674
3675 return NULL;
3676 }
3677
3678 static struct symtab *
3679 dw2_find_pc_sect_symtab (struct objfile *objfile,
3680 struct minimal_symbol *msymbol,
3681 CORE_ADDR pc,
3682 struct obj_section *section,
3683 int warn_if_readin)
3684 {
3685 struct dwarf2_per_cu_data *data;
3686 struct symtab *result;
3687
3688 dw2_setup (objfile);
3689
3690 if (!objfile->psymtabs_addrmap)
3691 return NULL;
3692
3693 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3694 if (!data)
3695 return NULL;
3696
3697 if (warn_if_readin && data->v.quick->symtab)
3698 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
3699 paddress (get_objfile_arch (objfile), pc));
3700
3701 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3702 gdb_assert (result != NULL);
3703 return result;
3704 }
3705
3706 static void
3707 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
3708 void *data, int need_fullname)
3709 {
3710 int i;
3711 struct cleanup *cleanup;
3712 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3713 NULL, xcalloc, xfree);
3714
3715 cleanup = make_cleanup_htab_delete (visited);
3716 dw2_setup (objfile);
3717
3718 /* The rule is CUs specify all the files, including those used by
3719 any TU, so there's no need to scan TUs here.
3720 We can ignore file names coming from already-expanded CUs. */
3721
3722 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3723 {
3724 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3725
3726 if (per_cu->v.quick->symtab)
3727 {
3728 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3729 INSERT);
3730
3731 *slot = per_cu->v.quick->file_names;
3732 }
3733 }
3734
3735 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3736 {
3737 int j;
3738 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3739 struct quick_file_names *file_data;
3740 void **slot;
3741
3742 /* We only need to look at symtabs not already expanded. */
3743 if (per_cu->v.quick->symtab)
3744 continue;
3745
3746 file_data = dw2_get_file_names (objfile, per_cu);
3747 if (file_data == NULL)
3748 continue;
3749
3750 slot = htab_find_slot (visited, file_data, INSERT);
3751 if (*slot)
3752 {
3753 /* Already visited. */
3754 continue;
3755 }
3756 *slot = file_data;
3757
3758 for (j = 0; j < file_data->num_file_names; ++j)
3759 {
3760 const char *this_real_name;
3761
3762 if (need_fullname)
3763 this_real_name = dw2_get_real_path (objfile, file_data, j);
3764 else
3765 this_real_name = NULL;
3766 (*fun) (file_data->file_names[j], this_real_name, data);
3767 }
3768 }
3769
3770 do_cleanups (cleanup);
3771 }
3772
3773 static int
3774 dw2_has_symbols (struct objfile *objfile)
3775 {
3776 return 1;
3777 }
3778
3779 const struct quick_symbol_functions dwarf2_gdb_index_functions =
3780 {
3781 dw2_has_symbols,
3782 dw2_find_last_source_symtab,
3783 dw2_forget_cached_source_info,
3784 dw2_map_symtabs_matching_filename,
3785 dw2_lookup_symbol,
3786 dw2_print_stats,
3787 dw2_dump,
3788 dw2_relocate,
3789 dw2_expand_symtabs_for_function,
3790 dw2_expand_all_symtabs,
3791 dw2_expand_symtabs_with_fullname,
3792 dw2_find_symbol_file,
3793 dw2_map_matching_symbols,
3794 dw2_expand_symtabs_matching,
3795 dw2_find_pc_sect_symtab,
3796 dw2_map_symbol_filenames
3797 };
3798
3799 /* Initialize for reading DWARF for this objfile. Return 0 if this
3800 file will use psymtabs, or 1 if using the GNU index. */
3801
3802 int
3803 dwarf2_initialize_objfile (struct objfile *objfile)
3804 {
3805 /* If we're about to read full symbols, don't bother with the
3806 indices. In this case we also don't care if some other debug
3807 format is making psymtabs, because they are all about to be
3808 expanded anyway. */
3809 if ((objfile->flags & OBJF_READNOW))
3810 {
3811 int i;
3812
3813 dwarf2_per_objfile->using_index = 1;
3814 create_all_comp_units (objfile);
3815 create_all_type_units (objfile);
3816 dwarf2_per_objfile->quick_file_names_table =
3817 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3818
3819 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3820 + dwarf2_per_objfile->n_type_units); ++i)
3821 {
3822 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3823
3824 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3825 struct dwarf2_per_cu_quick_data);
3826 }
3827
3828 /* Return 1 so that gdb sees the "quick" functions. However,
3829 these functions will be no-ops because we will have expanded
3830 all symtabs. */
3831 return 1;
3832 }
3833
3834 if (dwarf2_read_index (objfile))
3835 return 1;
3836
3837 return 0;
3838 }
3839
3840 \f
3841
3842 /* Build a partial symbol table. */
3843
3844 void
3845 dwarf2_build_psymtabs (struct objfile *objfile)
3846 {
3847 volatile struct gdb_exception except;
3848
3849 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
3850 {
3851 init_psymbol_list (objfile, 1024);
3852 }
3853
3854 TRY_CATCH (except, RETURN_MASK_ERROR)
3855 {
3856 /* This isn't really ideal: all the data we allocate on the
3857 objfile's obstack is still uselessly kept around. However,
3858 freeing it seems unsafe. */
3859 struct cleanup *cleanups = make_cleanup_discard_psymtabs (objfile);
3860
3861 dwarf2_build_psymtabs_hard (objfile);
3862 discard_cleanups (cleanups);
3863 }
3864 if (except.reason < 0)
3865 exception_print (gdb_stderr, except);
3866 }
3867
3868 /* Return the total length of the CU described by HEADER. */
3869
3870 static unsigned int
3871 get_cu_length (const struct comp_unit_head *header)
3872 {
3873 return header->initial_length_size + header->length;
3874 }
3875
3876 /* Return TRUE if OFFSET is within CU_HEADER. */
3877
3878 static inline int
3879 offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
3880 {
3881 sect_offset bottom = { cu_header->offset.sect_off };
3882 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
3883
3884 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
3885 }
3886
3887 /* Find the base address of the compilation unit for range lists and
3888 location lists. It will normally be specified by DW_AT_low_pc.
3889 In DWARF-3 draft 4, the base address could be overridden by
3890 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3891 compilation units with discontinuous ranges. */
3892
3893 static void
3894 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3895 {
3896 struct attribute *attr;
3897
3898 cu->base_known = 0;
3899 cu->base_address = 0;
3900
3901 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3902 if (attr)
3903 {
3904 cu->base_address = DW_ADDR (attr);
3905 cu->base_known = 1;
3906 }
3907 else
3908 {
3909 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3910 if (attr)
3911 {
3912 cu->base_address = DW_ADDR (attr);
3913 cu->base_known = 1;
3914 }
3915 }
3916 }
3917
3918 /* Read in the comp unit header information from the debug_info at info_ptr.
3919 NOTE: This leaves members offset, first_die_offset to be filled in
3920 by the caller. */
3921
3922 static gdb_byte *
3923 read_comp_unit_head (struct comp_unit_head *cu_header,
3924 gdb_byte *info_ptr, bfd *abfd)
3925 {
3926 int signed_addr;
3927 unsigned int bytes_read;
3928
3929 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3930 cu_header->initial_length_size = bytes_read;
3931 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
3932 info_ptr += bytes_read;
3933 cu_header->version = read_2_bytes (abfd, info_ptr);
3934 info_ptr += 2;
3935 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3936 &bytes_read);
3937 info_ptr += bytes_read;
3938 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3939 info_ptr += 1;
3940 signed_addr = bfd_get_sign_extend_vma (abfd);
3941 if (signed_addr < 0)
3942 internal_error (__FILE__, __LINE__,
3943 _("read_comp_unit_head: dwarf from non elf file"));
3944 cu_header->signed_addr_p = signed_addr;
3945
3946 return info_ptr;
3947 }
3948
3949 /* Helper function that returns the proper abbrev section for
3950 THIS_CU. */
3951
3952 static struct dwarf2_section_info *
3953 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3954 {
3955 struct dwarf2_section_info *abbrev;
3956
3957 if (this_cu->is_dwz)
3958 abbrev = &dwarf2_get_dwz_file ()->abbrev;
3959 else
3960 abbrev = &dwarf2_per_objfile->abbrev;
3961
3962 return abbrev;
3963 }
3964
3965 /* Subroutine of read_and_check_comp_unit_head and
3966 read_and_check_type_unit_head to simplify them.
3967 Perform various error checking on the header. */
3968
3969 static void
3970 error_check_comp_unit_head (struct comp_unit_head *header,
3971 struct dwarf2_section_info *section,
3972 struct dwarf2_section_info *abbrev_section)
3973 {
3974 bfd *abfd = section->asection->owner;
3975 const char *filename = bfd_get_filename (abfd);
3976
3977 if (header->version != 2 && header->version != 3 && header->version != 4)
3978 error (_("Dwarf Error: wrong version in compilation unit header "
3979 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3980 filename);
3981
3982 if (header->abbrev_offset.sect_off
3983 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
3984 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3985 "(offset 0x%lx + 6) [in module %s]"),
3986 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
3987 filename);
3988
3989 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3990 avoid potential 32-bit overflow. */
3991 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
3992 > section->size)
3993 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3994 "(offset 0x%lx + 0) [in module %s]"),
3995 (long) header->length, (long) header->offset.sect_off,
3996 filename);
3997 }
3998
3999 /* Read in a CU/TU header and perform some basic error checking.
4000 The contents of the header are stored in HEADER.
4001 The result is a pointer to the start of the first DIE. */
4002
4003 static gdb_byte *
4004 read_and_check_comp_unit_head (struct comp_unit_head *header,
4005 struct dwarf2_section_info *section,
4006 struct dwarf2_section_info *abbrev_section,
4007 gdb_byte *info_ptr,
4008 int is_debug_types_section)
4009 {
4010 gdb_byte *beg_of_comp_unit = info_ptr;
4011 bfd *abfd = section->asection->owner;
4012
4013 header->offset.sect_off = beg_of_comp_unit - section->buffer;
4014
4015 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4016
4017 /* If we're reading a type unit, skip over the signature and
4018 type_offset fields. */
4019 if (is_debug_types_section)
4020 info_ptr += 8 /*signature*/ + header->offset_size;
4021
4022 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
4023
4024 error_check_comp_unit_head (header, section, abbrev_section);
4025
4026 return info_ptr;
4027 }
4028
4029 /* Read in the types comp unit header information from .debug_types entry at
4030 types_ptr. The result is a pointer to one past the end of the header. */
4031
4032 static gdb_byte *
4033 read_and_check_type_unit_head (struct comp_unit_head *header,
4034 struct dwarf2_section_info *section,
4035 struct dwarf2_section_info *abbrev_section,
4036 gdb_byte *info_ptr,
4037 ULONGEST *signature,
4038 cu_offset *type_offset_in_tu)
4039 {
4040 gdb_byte *beg_of_comp_unit = info_ptr;
4041 bfd *abfd = section->asection->owner;
4042
4043 header->offset.sect_off = beg_of_comp_unit - section->buffer;
4044
4045 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4046
4047 /* If we're reading a type unit, skip over the signature and
4048 type_offset fields. */
4049 if (signature != NULL)
4050 *signature = read_8_bytes (abfd, info_ptr);
4051 info_ptr += 8;
4052 if (type_offset_in_tu != NULL)
4053 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
4054 header->offset_size);
4055 info_ptr += header->offset_size;
4056
4057 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
4058
4059 error_check_comp_unit_head (header, section, abbrev_section);
4060
4061 return info_ptr;
4062 }
4063
4064 /* Fetch the abbreviation table offset from a comp or type unit header. */
4065
4066 static sect_offset
4067 read_abbrev_offset (struct dwarf2_section_info *section,
4068 sect_offset offset)
4069 {
4070 bfd *abfd = section->asection->owner;
4071 gdb_byte *info_ptr;
4072 unsigned int length, initial_length_size, offset_size;
4073 sect_offset abbrev_offset;
4074
4075 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4076 info_ptr = section->buffer + offset.sect_off;
4077 length = read_initial_length (abfd, info_ptr, &initial_length_size);
4078 offset_size = initial_length_size == 4 ? 4 : 8;
4079 info_ptr += initial_length_size + 2 /*version*/;
4080 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4081 return abbrev_offset;
4082 }
4083
4084 /* Allocate a new partial symtab for file named NAME and mark this new
4085 partial symtab as being an include of PST. */
4086
4087 static void
4088 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
4089 struct objfile *objfile)
4090 {
4091 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4092
4093 if (!IS_ABSOLUTE_PATH (subpst->filename))
4094 {
4095 /* It shares objfile->objfile_obstack. */
4096 subpst->dirname = pst->dirname;
4097 }
4098
4099 subpst->section_offsets = pst->section_offsets;
4100 subpst->textlow = 0;
4101 subpst->texthigh = 0;
4102
4103 subpst->dependencies = (struct partial_symtab **)
4104 obstack_alloc (&objfile->objfile_obstack,
4105 sizeof (struct partial_symtab *));
4106 subpst->dependencies[0] = pst;
4107 subpst->number_of_dependencies = 1;
4108
4109 subpst->globals_offset = 0;
4110 subpst->n_global_syms = 0;
4111 subpst->statics_offset = 0;
4112 subpst->n_static_syms = 0;
4113 subpst->symtab = NULL;
4114 subpst->read_symtab = pst->read_symtab;
4115 subpst->readin = 0;
4116
4117 /* No private part is necessary for include psymtabs. This property
4118 can be used to differentiate between such include psymtabs and
4119 the regular ones. */
4120 subpst->read_symtab_private = NULL;
4121 }
4122
4123 /* Read the Line Number Program data and extract the list of files
4124 included by the source file represented by PST. Build an include
4125 partial symtab for each of these included files. */
4126
4127 static void
4128 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
4129 struct die_info *die,
4130 struct partial_symtab *pst)
4131 {
4132 struct line_header *lh = NULL;
4133 struct attribute *attr;
4134
4135 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4136 if (attr)
4137 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
4138 if (lh == NULL)
4139 return; /* No linetable, so no includes. */
4140
4141 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
4142 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
4143
4144 free_line_header (lh);
4145 }
4146
4147 static hashval_t
4148 hash_signatured_type (const void *item)
4149 {
4150 const struct signatured_type *sig_type = item;
4151
4152 /* This drops the top 32 bits of the signature, but is ok for a hash. */
4153 return sig_type->signature;
4154 }
4155
4156 static int
4157 eq_signatured_type (const void *item_lhs, const void *item_rhs)
4158 {
4159 const struct signatured_type *lhs = item_lhs;
4160 const struct signatured_type *rhs = item_rhs;
4161
4162 return lhs->signature == rhs->signature;
4163 }
4164
4165 /* Allocate a hash table for signatured types. */
4166
4167 static htab_t
4168 allocate_signatured_type_table (struct objfile *objfile)
4169 {
4170 return htab_create_alloc_ex (41,
4171 hash_signatured_type,
4172 eq_signatured_type,
4173 NULL,
4174 &objfile->objfile_obstack,
4175 hashtab_obstack_allocate,
4176 dummy_obstack_deallocate);
4177 }
4178
4179 /* A helper function to add a signatured type CU to a table. */
4180
4181 static int
4182 add_signatured_type_cu_to_table (void **slot, void *datum)
4183 {
4184 struct signatured_type *sigt = *slot;
4185 struct signatured_type ***datap = datum;
4186
4187 **datap = sigt;
4188 ++*datap;
4189
4190 return 1;
4191 }
4192
4193 /* Create the hash table of all entries in the .debug_types section.
4194 DWO_FILE is a pointer to the DWO file for .debug_types.dwo,
4195 NULL otherwise.
4196 Note: This function processes DWO files only, not DWP files.
4197 The result is a pointer to the hash table or NULL if there are
4198 no types. */
4199
4200 static htab_t
4201 create_debug_types_hash_table (struct dwo_file *dwo_file,
4202 VEC (dwarf2_section_info_def) *types)
4203 {
4204 struct objfile *objfile = dwarf2_per_objfile->objfile;
4205 htab_t types_htab = NULL;
4206 int ix;
4207 struct dwarf2_section_info *section;
4208 struct dwarf2_section_info *abbrev_section;
4209
4210 if (VEC_empty (dwarf2_section_info_def, types))
4211 return NULL;
4212
4213 abbrev_section = (dwo_file != NULL
4214 ? &dwo_file->sections.abbrev
4215 : &dwarf2_per_objfile->abbrev);
4216
4217 if (dwarf2_read_debug)
4218 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4219 dwo_file ? ".dwo" : "",
4220 bfd_get_filename (abbrev_section->asection->owner));
4221
4222 for (ix = 0;
4223 VEC_iterate (dwarf2_section_info_def, types, ix, section);
4224 ++ix)
4225 {
4226 bfd *abfd;
4227 gdb_byte *info_ptr, *end_ptr;
4228 struct dwarf2_section_info *abbrev_section;
4229
4230 dwarf2_read_section (objfile, section);
4231 info_ptr = section->buffer;
4232
4233 if (info_ptr == NULL)
4234 continue;
4235
4236 /* We can't set abfd until now because the section may be empty or
4237 not present, in which case section->asection will be NULL. */
4238 abfd = section->asection->owner;
4239
4240 if (dwo_file)
4241 abbrev_section = &dwo_file->sections.abbrev;
4242 else
4243 abbrev_section = &dwarf2_per_objfile->abbrev;
4244
4245 if (types_htab == NULL)
4246 {
4247 if (dwo_file)
4248 types_htab = allocate_dwo_unit_table (objfile);
4249 else
4250 types_htab = allocate_signatured_type_table (objfile);
4251 }
4252
4253 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4254 because we don't need to read any dies: the signature is in the
4255 header. */
4256
4257 end_ptr = info_ptr + section->size;
4258 while (info_ptr < end_ptr)
4259 {
4260 sect_offset offset;
4261 cu_offset type_offset_in_tu;
4262 ULONGEST signature;
4263 struct signatured_type *sig_type;
4264 struct dwo_unit *dwo_tu;
4265 void **slot;
4266 gdb_byte *ptr = info_ptr;
4267 struct comp_unit_head header;
4268 unsigned int length;
4269
4270 offset.sect_off = ptr - section->buffer;
4271
4272 /* We need to read the type's signature in order to build the hash
4273 table, but we don't need anything else just yet. */
4274
4275 ptr = read_and_check_type_unit_head (&header, section,
4276 abbrev_section, ptr,
4277 &signature, &type_offset_in_tu);
4278
4279 length = get_cu_length (&header);
4280
4281 /* Skip dummy type units. */
4282 if (ptr >= info_ptr + length
4283 || peek_abbrev_code (abfd, ptr) == 0)
4284 {
4285 info_ptr += length;
4286 continue;
4287 }
4288
4289 if (dwo_file)
4290 {
4291 sig_type = NULL;
4292 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4293 struct dwo_unit);
4294 dwo_tu->dwo_file = dwo_file;
4295 dwo_tu->signature = signature;
4296 dwo_tu->type_offset_in_tu = type_offset_in_tu;
4297 dwo_tu->info_or_types_section = section;
4298 dwo_tu->offset = offset;
4299 dwo_tu->length = length;
4300 }
4301 else
4302 {
4303 /* N.B.: type_offset is not usable if this type uses a DWO file.
4304 The real type_offset is in the DWO file. */
4305 dwo_tu = NULL;
4306 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4307 struct signatured_type);
4308 sig_type->signature = signature;
4309 sig_type->type_offset_in_tu = type_offset_in_tu;
4310 sig_type->per_cu.objfile = objfile;
4311 sig_type->per_cu.is_debug_types = 1;
4312 sig_type->per_cu.info_or_types_section = section;
4313 sig_type->per_cu.offset = offset;
4314 sig_type->per_cu.length = length;
4315 }
4316
4317 slot = htab_find_slot (types_htab,
4318 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4319 INSERT);
4320 gdb_assert (slot != NULL);
4321 if (*slot != NULL)
4322 {
4323 sect_offset dup_offset;
4324
4325 if (dwo_file)
4326 {
4327 const struct dwo_unit *dup_tu = *slot;
4328
4329 dup_offset = dup_tu->offset;
4330 }
4331 else
4332 {
4333 const struct signatured_type *dup_tu = *slot;
4334
4335 dup_offset = dup_tu->per_cu.offset;
4336 }
4337
4338 complaint (&symfile_complaints,
4339 _("debug type entry at offset 0x%x is duplicate to the "
4340 "entry at offset 0x%x, signature 0x%s"),
4341 offset.sect_off, dup_offset.sect_off,
4342 phex (signature, sizeof (signature)));
4343 }
4344 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
4345
4346 if (dwarf2_read_debug)
4347 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
4348 offset.sect_off,
4349 phex (signature, sizeof (signature)));
4350
4351 info_ptr += length;
4352 }
4353 }
4354
4355 return types_htab;
4356 }
4357
4358 /* Create the hash table of all entries in the .debug_types section,
4359 and initialize all_type_units.
4360 The result is zero if there is an error (e.g. missing .debug_types section),
4361 otherwise non-zero. */
4362
4363 static int
4364 create_all_type_units (struct objfile *objfile)
4365 {
4366 htab_t types_htab;
4367 struct signatured_type **iter;
4368
4369 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4370 if (types_htab == NULL)
4371 {
4372 dwarf2_per_objfile->signatured_types = NULL;
4373 return 0;
4374 }
4375
4376 dwarf2_per_objfile->signatured_types = types_htab;
4377
4378 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4379 dwarf2_per_objfile->all_type_units
4380 = obstack_alloc (&objfile->objfile_obstack,
4381 dwarf2_per_objfile->n_type_units
4382 * sizeof (struct signatured_type *));
4383 iter = &dwarf2_per_objfile->all_type_units[0];
4384 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4385 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4386 == dwarf2_per_objfile->n_type_units);
4387
4388 return 1;
4389 }
4390
4391 /* Lookup a signature based type for DW_FORM_ref_sig8.
4392 Returns NULL if signature SIG is not present in the table. */
4393
4394 static struct signatured_type *
4395 lookup_signatured_type (ULONGEST sig)
4396 {
4397 struct signatured_type find_entry, *entry;
4398
4399 if (dwarf2_per_objfile->signatured_types == NULL)
4400 {
4401 complaint (&symfile_complaints,
4402 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
4403 return NULL;
4404 }
4405
4406 find_entry.signature = sig;
4407 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4408 return entry;
4409 }
4410 \f
4411 /* Low level DIE reading support. */
4412
4413 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4414
4415 static void
4416 init_cu_die_reader (struct die_reader_specs *reader,
4417 struct dwarf2_cu *cu,
4418 struct dwarf2_section_info *section,
4419 struct dwo_file *dwo_file)
4420 {
4421 gdb_assert (section->readin && section->buffer != NULL);
4422 reader->abfd = section->asection->owner;
4423 reader->cu = cu;
4424 reader->dwo_file = dwo_file;
4425 reader->die_section = section;
4426 reader->buffer = section->buffer;
4427 reader->buffer_end = section->buffer + section->size;
4428 }
4429
4430 /* Initialize a CU (or TU) and read its DIEs.
4431 If the CU defers to a DWO file, read the DWO file as well.
4432
4433 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4434 Otherwise the table specified in the comp unit header is read in and used.
4435 This is an optimization for when we already have the abbrev table.
4436
4437 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4438 Otherwise, a new CU is allocated with xmalloc.
4439
4440 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4441 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
4442
4443 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4444 linker) then DIE_READER_FUNC will not get called. */
4445
4446 static void
4447 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
4448 struct abbrev_table *abbrev_table,
4449 int use_existing_cu, int keep,
4450 die_reader_func_ftype *die_reader_func,
4451 void *data)
4452 {
4453 struct objfile *objfile = dwarf2_per_objfile->objfile;
4454 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4455 bfd *abfd = section->asection->owner;
4456 struct dwarf2_cu *cu;
4457 gdb_byte *begin_info_ptr, *info_ptr;
4458 struct die_reader_specs reader;
4459 struct die_info *comp_unit_die;
4460 int has_children;
4461 struct attribute *attr;
4462 struct cleanup *cleanups, *free_cu_cleanup = NULL;
4463 struct signatured_type *sig_type = NULL;
4464 struct dwarf2_section_info *abbrev_section;
4465 /* Non-zero if CU currently points to a DWO file and we need to
4466 reread it. When this happens we need to reread the skeleton die
4467 before we can reread the DWO file. */
4468 int rereading_dwo_cu = 0;
4469
4470 if (dwarf2_die_debug)
4471 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4472 this_cu->is_debug_types ? "type" : "comp",
4473 this_cu->offset.sect_off);
4474
4475 if (use_existing_cu)
4476 gdb_assert (keep);
4477
4478 cleanups = make_cleanup (null_cleanup, NULL);
4479
4480 /* This is cheap if the section is already read in. */
4481 dwarf2_read_section (objfile, section);
4482
4483 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4484
4485 abbrev_section = get_abbrev_section_for_cu (this_cu);
4486
4487 if (use_existing_cu && this_cu->cu != NULL)
4488 {
4489 cu = this_cu->cu;
4490
4491 /* If this CU is from a DWO file we need to start over, we need to
4492 refetch the attributes from the skeleton CU.
4493 This could be optimized by retrieving those attributes from when we
4494 were here the first time: the previous comp_unit_die was stored in
4495 comp_unit_obstack. But there's no data yet that we need this
4496 optimization. */
4497 if (cu->dwo_unit != NULL)
4498 rereading_dwo_cu = 1;
4499 }
4500 else
4501 {
4502 /* If !use_existing_cu, this_cu->cu must be NULL. */
4503 gdb_assert (this_cu->cu == NULL);
4504
4505 cu = xmalloc (sizeof (*cu));
4506 init_one_comp_unit (cu, this_cu);
4507
4508 /* If an error occurs while loading, release our storage. */
4509 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
4510 }
4511
4512 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
4513 {
4514 /* We already have the header, there's no need to read it in again. */
4515 info_ptr += cu->header.first_die_offset.cu_off;
4516 }
4517 else
4518 {
4519 if (this_cu->is_debug_types)
4520 {
4521 ULONGEST signature;
4522 cu_offset type_offset_in_tu;
4523
4524 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4525 abbrev_section, info_ptr,
4526 &signature,
4527 &type_offset_in_tu);
4528
4529 /* Since per_cu is the first member of struct signatured_type,
4530 we can go from a pointer to one to a pointer to the other. */
4531 sig_type = (struct signatured_type *) this_cu;
4532 gdb_assert (sig_type->signature == signature);
4533 gdb_assert (sig_type->type_offset_in_tu.cu_off
4534 == type_offset_in_tu.cu_off);
4535 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4536
4537 /* LENGTH has not been set yet for type units if we're
4538 using .gdb_index. */
4539 this_cu->length = get_cu_length (&cu->header);
4540
4541 /* Establish the type offset that can be used to lookup the type. */
4542 sig_type->type_offset_in_section.sect_off =
4543 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
4544 }
4545 else
4546 {
4547 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4548 abbrev_section,
4549 info_ptr, 0);
4550
4551 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4552 gdb_assert (this_cu->length == get_cu_length (&cu->header));
4553 }
4554 }
4555
4556 /* Skip dummy compilation units. */
4557 if (info_ptr >= begin_info_ptr + this_cu->length
4558 || peek_abbrev_code (abfd, info_ptr) == 0)
4559 {
4560 do_cleanups (cleanups);
4561 return;
4562 }
4563
4564 /* If we don't have them yet, read the abbrevs for this compilation unit.
4565 And if we need to read them now, make sure they're freed when we're
4566 done. Note that it's important that if the CU had an abbrev table
4567 on entry we don't free it when we're done: Somewhere up the call stack
4568 it may be in use. */
4569 if (abbrev_table != NULL)
4570 {
4571 gdb_assert (cu->abbrev_table == NULL);
4572 gdb_assert (cu->header.abbrev_offset.sect_off
4573 == abbrev_table->offset.sect_off);
4574 cu->abbrev_table = abbrev_table;
4575 }
4576 else if (cu->abbrev_table == NULL)
4577 {
4578 dwarf2_read_abbrevs (cu, abbrev_section);
4579 make_cleanup (dwarf2_free_abbrev_table, cu);
4580 }
4581 else if (rereading_dwo_cu)
4582 {
4583 dwarf2_free_abbrev_table (cu);
4584 dwarf2_read_abbrevs (cu, abbrev_section);
4585 }
4586
4587 /* Read the top level CU/TU die. */
4588 init_cu_die_reader (&reader, cu, section, NULL);
4589 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4590
4591 /* If we have a DWO stub, process it and then read in the DWO file.
4592 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
4593 a DWO CU, that this test will fail. */
4594 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4595 if (attr)
4596 {
4597 const char *dwo_name = DW_STRING (attr);
4598 const char *comp_dir_string;
4599 struct dwo_unit *dwo_unit;
4600 ULONGEST signature; /* Or dwo_id. */
4601 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
4602 int i,num_extra_attrs;
4603 struct dwarf2_section_info *dwo_abbrev_section;
4604
4605 if (has_children)
4606 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
4607 " has children (offset 0x%x) [in module %s]"),
4608 this_cu->offset.sect_off, bfd_get_filename (abfd));
4609
4610 /* These attributes aren't processed until later:
4611 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4612 However, the attribute is found in the stub which we won't have later.
4613 In order to not impose this complication on the rest of the code,
4614 we read them here and copy them to the DWO CU/TU die. */
4615
4616 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4617 DWO file. */
4618 stmt_list = NULL;
4619 if (! this_cu->is_debug_types)
4620 stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
4621 low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
4622 high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
4623 ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
4624 comp_dir = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4625
4626 /* There should be a DW_AT_addr_base attribute here (if needed).
4627 We need the value before we can process DW_FORM_GNU_addr_index. */
4628 cu->addr_base = 0;
4629 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
4630 if (attr)
4631 cu->addr_base = DW_UNSND (attr);
4632
4633 /* There should be a DW_AT_ranges_base attribute here (if needed).
4634 We need the value before we can process DW_AT_ranges. */
4635 cu->ranges_base = 0;
4636 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_ranges_base, cu);
4637 if (attr)
4638 cu->ranges_base = DW_UNSND (attr);
4639
4640 if (this_cu->is_debug_types)
4641 {
4642 gdb_assert (sig_type != NULL);
4643 signature = sig_type->signature;
4644 }
4645 else
4646 {
4647 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4648 if (! attr)
4649 error (_("Dwarf Error: missing dwo_id [in module %s]"),
4650 dwo_name);
4651 signature = DW_UNSND (attr);
4652 }
4653
4654 /* We may need the comp_dir in order to find the DWO file. */
4655 comp_dir_string = NULL;
4656 if (comp_dir)
4657 comp_dir_string = DW_STRING (comp_dir);
4658
4659 if (this_cu->is_debug_types)
4660 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir_string);
4661 else
4662 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir_string,
4663 signature);
4664
4665 if (dwo_unit == NULL)
4666 {
4667 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
4668 " with ID %s [in module %s]"),
4669 this_cu->offset.sect_off,
4670 phex (signature, sizeof (signature)),
4671 objfile->name);
4672 }
4673
4674 /* Set up for reading the DWO CU/TU. */
4675 cu->dwo_unit = dwo_unit;
4676 section = dwo_unit->info_or_types_section;
4677 dwarf2_read_section (objfile, section);
4678 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4679 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
4680 init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
4681
4682 if (this_cu->is_debug_types)
4683 {
4684 ULONGEST signature;
4685 cu_offset type_offset_in_tu;
4686
4687 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4688 dwo_abbrev_section,
4689 info_ptr,
4690 &signature,
4691 &type_offset_in_tu);
4692 gdb_assert (sig_type->signature == signature);
4693 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4694 /* For DWOs coming from DWP files, we don't know the CU length
4695 nor the type's offset in the TU until now. */
4696 dwo_unit->length = get_cu_length (&cu->header);
4697 dwo_unit->type_offset_in_tu = type_offset_in_tu;
4698
4699 /* Establish the type offset that can be used to lookup the type.
4700 For DWO files, we don't know it until now. */
4701 sig_type->type_offset_in_section.sect_off =
4702 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4703 }
4704 else
4705 {
4706 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4707 dwo_abbrev_section,
4708 info_ptr, 0);
4709 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4710 /* For DWOs coming from DWP files, we don't know the CU length
4711 until now. */
4712 dwo_unit->length = get_cu_length (&cu->header);
4713 }
4714
4715 /* Discard the original CU's abbrev table, and read the DWO's. */
4716 if (abbrev_table == NULL)
4717 {
4718 dwarf2_free_abbrev_table (cu);
4719 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4720 }
4721 else
4722 {
4723 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4724 make_cleanup (dwarf2_free_abbrev_table, cu);
4725 }
4726
4727 /* Read in the die, but leave space to copy over the attributes
4728 from the stub. This has the benefit of simplifying the rest of
4729 the code - all the real work is done here. */
4730 num_extra_attrs = ((stmt_list != NULL)
4731 + (low_pc != NULL)
4732 + (high_pc != NULL)
4733 + (ranges != NULL)
4734 + (comp_dir != NULL));
4735 info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
4736 &has_children, num_extra_attrs);
4737
4738 /* Copy over the attributes from the stub to the DWO die. */
4739 i = comp_unit_die->num_attrs;
4740 if (stmt_list != NULL)
4741 comp_unit_die->attrs[i++] = *stmt_list;
4742 if (low_pc != NULL)
4743 comp_unit_die->attrs[i++] = *low_pc;
4744 if (high_pc != NULL)
4745 comp_unit_die->attrs[i++] = *high_pc;
4746 if (ranges != NULL)
4747 comp_unit_die->attrs[i++] = *ranges;
4748 if (comp_dir != NULL)
4749 comp_unit_die->attrs[i++] = *comp_dir;
4750 comp_unit_die->num_attrs += num_extra_attrs;
4751
4752 /* Skip dummy compilation units. */
4753 if (info_ptr >= begin_info_ptr + dwo_unit->length
4754 || peek_abbrev_code (abfd, info_ptr) == 0)
4755 {
4756 do_cleanups (cleanups);
4757 return;
4758 }
4759 }
4760
4761 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4762
4763 if (free_cu_cleanup != NULL)
4764 {
4765 if (keep)
4766 {
4767 /* We've successfully allocated this compilation unit. Let our
4768 caller clean it up when finished with it. */
4769 discard_cleanups (free_cu_cleanup);
4770
4771 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4772 So we have to manually free the abbrev table. */
4773 dwarf2_free_abbrev_table (cu);
4774
4775 /* Link this CU into read_in_chain. */
4776 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4777 dwarf2_per_objfile->read_in_chain = this_cu;
4778 }
4779 else
4780 do_cleanups (free_cu_cleanup);
4781 }
4782
4783 do_cleanups (cleanups);
4784 }
4785
4786 /* Read CU/TU THIS_CU in section SECTION,
4787 but do not follow DW_AT_GNU_dwo_name if present.
4788 DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
4789 to have already done the lookup to find the DWO/DWP file).
4790
4791 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
4792 THIS_CU->is_debug_types, but nothing else.
4793
4794 We fill in THIS_CU->length.
4795
4796 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4797 linker) then DIE_READER_FUNC will not get called.
4798
4799 THIS_CU->cu is always freed when done.
4800 This is done in order to not leave THIS_CU->cu in a state where we have
4801 to care whether it refers to the "main" CU or the DWO CU. */
4802
4803 static void
4804 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4805 struct dwarf2_section_info *abbrev_section,
4806 struct dwo_file *dwo_file,
4807 die_reader_func_ftype *die_reader_func,
4808 void *data)
4809 {
4810 struct objfile *objfile = dwarf2_per_objfile->objfile;
4811 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4812 bfd *abfd = section->asection->owner;
4813 struct dwarf2_cu cu;
4814 gdb_byte *begin_info_ptr, *info_ptr;
4815 struct die_reader_specs reader;
4816 struct cleanup *cleanups;
4817 struct die_info *comp_unit_die;
4818 int has_children;
4819
4820 if (dwarf2_die_debug)
4821 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4822 this_cu->is_debug_types ? "type" : "comp",
4823 this_cu->offset.sect_off);
4824
4825 gdb_assert (this_cu->cu == NULL);
4826
4827 /* This is cheap if the section is already read in. */
4828 dwarf2_read_section (objfile, section);
4829
4830 init_one_comp_unit (&cu, this_cu);
4831
4832 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4833
4834 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4835 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
4836 abbrev_section, info_ptr,
4837 this_cu->is_debug_types);
4838
4839 this_cu->length = get_cu_length (&cu.header);
4840
4841 /* Skip dummy compilation units. */
4842 if (info_ptr >= begin_info_ptr + this_cu->length
4843 || peek_abbrev_code (abfd, info_ptr) == 0)
4844 {
4845 do_cleanups (cleanups);
4846 return;
4847 }
4848
4849 dwarf2_read_abbrevs (&cu, abbrev_section);
4850 make_cleanup (dwarf2_free_abbrev_table, &cu);
4851
4852 init_cu_die_reader (&reader, &cu, section, dwo_file);
4853 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4854
4855 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4856
4857 do_cleanups (cleanups);
4858 }
4859
4860 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4861 does not lookup the specified DWO file.
4862 This cannot be used to read DWO files.
4863
4864 THIS_CU->cu is always freed when done.
4865 This is done in order to not leave THIS_CU->cu in a state where we have
4866 to care whether it refers to the "main" CU or the DWO CU.
4867 We can revisit this if the data shows there's a performance issue. */
4868
4869 static void
4870 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4871 die_reader_func_ftype *die_reader_func,
4872 void *data)
4873 {
4874 init_cutu_and_read_dies_no_follow (this_cu,
4875 get_abbrev_section_for_cu (this_cu),
4876 NULL,
4877 die_reader_func, data);
4878 }
4879 \f
4880 /* Type Unit Groups.
4881
4882 Type Unit Groups are a way to collapse the set of all TUs (type units) into
4883 a more manageable set. The grouping is done by DW_AT_stmt_list entry
4884 so that all types coming from the same compilation (.o file) are grouped
4885 together. A future step could be to put the types in the same symtab as
4886 the CU the types ultimately came from. */
4887
4888 static hashval_t
4889 hash_type_unit_group (const void *item)
4890 {
4891 const struct type_unit_group *tu_group = item;
4892
4893 return hash_stmt_list_entry (&tu_group->hash);
4894 }
4895
4896 static int
4897 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
4898 {
4899 const struct type_unit_group *lhs = item_lhs;
4900 const struct type_unit_group *rhs = item_rhs;
4901
4902 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
4903 }
4904
4905 /* Allocate a hash table for type unit groups. */
4906
4907 static htab_t
4908 allocate_type_unit_groups_table (void)
4909 {
4910 return htab_create_alloc_ex (3,
4911 hash_type_unit_group,
4912 eq_type_unit_group,
4913 NULL,
4914 &dwarf2_per_objfile->objfile->objfile_obstack,
4915 hashtab_obstack_allocate,
4916 dummy_obstack_deallocate);
4917 }
4918
4919 /* Type units that don't have DW_AT_stmt_list are grouped into their own
4920 partial symtabs. We combine several TUs per psymtab to not let the size
4921 of any one psymtab grow too big. */
4922 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
4923 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
4924
4925 /* Helper routine for get_type_unit_group.
4926 Create the type_unit_group object used to hold one or more TUs. */
4927
4928 static struct type_unit_group *
4929 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
4930 {
4931 struct objfile *objfile = dwarf2_per_objfile->objfile;
4932 struct dwarf2_per_cu_data *per_cu;
4933 struct type_unit_group *tu_group;
4934
4935 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4936 struct type_unit_group);
4937 per_cu = &tu_group->per_cu;
4938 per_cu->objfile = objfile;
4939 per_cu->is_debug_types = 1;
4940 per_cu->type_unit_group = tu_group;
4941
4942 if (dwarf2_per_objfile->using_index)
4943 {
4944 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4945 struct dwarf2_per_cu_quick_data);
4946 tu_group->t.first_tu = cu->per_cu;
4947 }
4948 else
4949 {
4950 unsigned int line_offset = line_offset_struct.sect_off;
4951 struct partial_symtab *pst;
4952 char *name;
4953
4954 /* Give the symtab a useful name for debug purposes. */
4955 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
4956 name = xstrprintf ("<type_units_%d>",
4957 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
4958 else
4959 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
4960
4961 pst = create_partial_symtab (per_cu, name);
4962 pst->anonymous = 1;
4963
4964 xfree (name);
4965 }
4966
4967 tu_group->hash.dwo_unit = cu->dwo_unit;
4968 tu_group->hash.line_offset = line_offset_struct;
4969
4970 return tu_group;
4971 }
4972
4973 /* Look up the type_unit_group for type unit CU, and create it if necessary.
4974 STMT_LIST is a DW_AT_stmt_list attribute. */
4975
4976 static struct type_unit_group *
4977 get_type_unit_group (struct dwarf2_cu *cu, struct attribute *stmt_list)
4978 {
4979 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
4980 struct type_unit_group *tu_group;
4981 void **slot;
4982 unsigned int line_offset;
4983 struct type_unit_group type_unit_group_for_lookup;
4984
4985 if (dwarf2_per_objfile->type_unit_groups == NULL)
4986 {
4987 dwarf2_per_objfile->type_unit_groups =
4988 allocate_type_unit_groups_table ();
4989 }
4990
4991 /* Do we need to create a new group, or can we use an existing one? */
4992
4993 if (stmt_list)
4994 {
4995 line_offset = DW_UNSND (stmt_list);
4996 ++tu_stats->nr_symtab_sharers;
4997 }
4998 else
4999 {
5000 /* Ugh, no stmt_list. Rare, but we have to handle it.
5001 We can do various things here like create one group per TU or
5002 spread them over multiple groups to split up the expansion work.
5003 To avoid worst case scenarios (too many groups or too large groups)
5004 we, umm, group them in bunches. */
5005 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5006 | (tu_stats->nr_stmt_less_type_units
5007 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5008 ++tu_stats->nr_stmt_less_type_units;
5009 }
5010
5011 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5012 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
5013 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5014 &type_unit_group_for_lookup, INSERT);
5015 if (*slot != NULL)
5016 {
5017 tu_group = *slot;
5018 gdb_assert (tu_group != NULL);
5019 }
5020 else
5021 {
5022 sect_offset line_offset_struct;
5023
5024 line_offset_struct.sect_off = line_offset;
5025 tu_group = create_type_unit_group (cu, line_offset_struct);
5026 *slot = tu_group;
5027 ++tu_stats->nr_symtabs;
5028 }
5029
5030 return tu_group;
5031 }
5032
5033 /* Struct used to sort TUs by their abbreviation table offset. */
5034
5035 struct tu_abbrev_offset
5036 {
5037 struct signatured_type *sig_type;
5038 sect_offset abbrev_offset;
5039 };
5040
5041 /* Helper routine for build_type_unit_groups, passed to qsort. */
5042
5043 static int
5044 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5045 {
5046 const struct tu_abbrev_offset * const *a = ap;
5047 const struct tu_abbrev_offset * const *b = bp;
5048 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5049 unsigned int boff = (*b)->abbrev_offset.sect_off;
5050
5051 return (aoff > boff) - (aoff < boff);
5052 }
5053
5054 /* A helper function to add a type_unit_group to a table. */
5055
5056 static int
5057 add_type_unit_group_to_table (void **slot, void *datum)
5058 {
5059 struct type_unit_group *tu_group = *slot;
5060 struct type_unit_group ***datap = datum;
5061
5062 **datap = tu_group;
5063 ++*datap;
5064
5065 return 1;
5066 }
5067
5068 /* Efficiently read all the type units, calling init_cutu_and_read_dies on
5069 each one passing FUNC,DATA.
5070
5071 The efficiency is because we sort TUs by the abbrev table they use and
5072 only read each abbrev table once. In one program there are 200K TUs
5073 sharing 8K abbrev tables.
5074
5075 The main purpose of this function is to support building the
5076 dwarf2_per_objfile->type_unit_groups table.
5077 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5078 can collapse the search space by grouping them by stmt_list.
5079 The savings can be significant, in the same program from above the 200K TUs
5080 share 8K stmt_list tables.
5081
5082 FUNC is expected to call get_type_unit_group, which will create the
5083 struct type_unit_group if necessary and add it to
5084 dwarf2_per_objfile->type_unit_groups. */
5085
5086 static void
5087 build_type_unit_groups (die_reader_func_ftype *func, void *data)
5088 {
5089 struct objfile *objfile = dwarf2_per_objfile->objfile;
5090 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5091 struct cleanup *cleanups;
5092 struct abbrev_table *abbrev_table;
5093 sect_offset abbrev_offset;
5094 struct tu_abbrev_offset *sorted_by_abbrev;
5095 struct type_unit_group **iter;
5096 int i;
5097
5098 /* It's up to the caller to not call us multiple times. */
5099 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5100
5101 if (dwarf2_per_objfile->n_type_units == 0)
5102 return;
5103
5104 /* TUs typically share abbrev tables, and there can be way more TUs than
5105 abbrev tables. Sort by abbrev table to reduce the number of times we
5106 read each abbrev table in.
5107 Alternatives are to punt or to maintain a cache of abbrev tables.
5108 This is simpler and efficient enough for now.
5109
5110 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5111 symtab to use). Typically TUs with the same abbrev offset have the same
5112 stmt_list value too so in practice this should work well.
5113
5114 The basic algorithm here is:
5115
5116 sort TUs by abbrev table
5117 for each TU with same abbrev table:
5118 read abbrev table if first user
5119 read TU top level DIE
5120 [IWBN if DWO skeletons had DW_AT_stmt_list]
5121 call FUNC */
5122
5123 if (dwarf2_read_debug)
5124 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5125
5126 /* Sort in a separate table to maintain the order of all_type_units
5127 for .gdb_index: TU indices directly index all_type_units. */
5128 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5129 dwarf2_per_objfile->n_type_units);
5130 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5131 {
5132 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5133
5134 sorted_by_abbrev[i].sig_type = sig_type;
5135 sorted_by_abbrev[i].abbrev_offset =
5136 read_abbrev_offset (sig_type->per_cu.info_or_types_section,
5137 sig_type->per_cu.offset);
5138 }
5139 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5140 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5141 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5142
5143 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5144 called any number of times, so we don't reset tu_stats here. */
5145
5146 abbrev_offset.sect_off = ~(unsigned) 0;
5147 abbrev_table = NULL;
5148 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5149
5150 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5151 {
5152 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5153
5154 /* Switch to the next abbrev table if necessary. */
5155 if (abbrev_table == NULL
5156 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5157 {
5158 if (abbrev_table != NULL)
5159 {
5160 abbrev_table_free (abbrev_table);
5161 /* Reset to NULL in case abbrev_table_read_table throws
5162 an error: abbrev_table_free_cleanup will get called. */
5163 abbrev_table = NULL;
5164 }
5165 abbrev_offset = tu->abbrev_offset;
5166 abbrev_table =
5167 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5168 abbrev_offset);
5169 ++tu_stats->nr_uniq_abbrev_tables;
5170 }
5171
5172 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5173 func, data);
5174 }
5175
5176 /* Create a vector of pointers to primary type units to make it easy to
5177 iterate over them and CUs. See dw2_get_primary_cu. */
5178 dwarf2_per_objfile->n_type_unit_groups =
5179 htab_elements (dwarf2_per_objfile->type_unit_groups);
5180 dwarf2_per_objfile->all_type_unit_groups =
5181 obstack_alloc (&objfile->objfile_obstack,
5182 dwarf2_per_objfile->n_type_unit_groups
5183 * sizeof (struct type_unit_group *));
5184 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5185 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5186 add_type_unit_group_to_table, &iter);
5187 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5188 == dwarf2_per_objfile->n_type_unit_groups);
5189
5190 do_cleanups (cleanups);
5191
5192 if (dwarf2_read_debug)
5193 {
5194 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5195 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5196 dwarf2_per_objfile->n_type_units);
5197 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5198 tu_stats->nr_uniq_abbrev_tables);
5199 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5200 tu_stats->nr_symtabs);
5201 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5202 tu_stats->nr_symtab_sharers);
5203 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5204 tu_stats->nr_stmt_less_type_units);
5205 }
5206 }
5207 \f
5208 /* Partial symbol tables. */
5209
5210 /* Create a psymtab named NAME and assign it to PER_CU.
5211
5212 The caller must fill in the following details:
5213 dirname, textlow, texthigh. */
5214
5215 static struct partial_symtab *
5216 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
5217 {
5218 struct objfile *objfile = per_cu->objfile;
5219 struct partial_symtab *pst;
5220
5221 pst = start_psymtab_common (objfile, objfile->section_offsets,
5222 name, 0,
5223 objfile->global_psymbols.next,
5224 objfile->static_psymbols.next);
5225
5226 pst->psymtabs_addrmap_supported = 1;
5227
5228 /* This is the glue that links PST into GDB's symbol API. */
5229 pst->read_symtab_private = per_cu;
5230 pst->read_symtab = dwarf2_read_symtab;
5231 per_cu->v.psymtab = pst;
5232
5233 return pst;
5234 }
5235
5236 /* die_reader_func for process_psymtab_comp_unit. */
5237
5238 static void
5239 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
5240 gdb_byte *info_ptr,
5241 struct die_info *comp_unit_die,
5242 int has_children,
5243 void *data)
5244 {
5245 struct dwarf2_cu *cu = reader->cu;
5246 struct objfile *objfile = cu->objfile;
5247 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5248 struct attribute *attr;
5249 CORE_ADDR baseaddr;
5250 CORE_ADDR best_lowpc = 0, best_highpc = 0;
5251 struct partial_symtab *pst;
5252 int has_pc_info;
5253 const char *filename;
5254 int *want_partial_unit_ptr = data;
5255
5256 if (comp_unit_die->tag == DW_TAG_partial_unit
5257 && (want_partial_unit_ptr == NULL
5258 || !*want_partial_unit_ptr))
5259 return;
5260
5261 gdb_assert (! per_cu->is_debug_types);
5262
5263 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
5264
5265 cu->list_in_scope = &file_symbols;
5266
5267 /* Allocate a new partial symbol table structure. */
5268 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
5269 if (attr == NULL || !DW_STRING (attr))
5270 filename = "";
5271 else
5272 filename = DW_STRING (attr);
5273
5274 pst = create_partial_symtab (per_cu, filename);
5275
5276 /* This must be done before calling dwarf2_build_include_psymtabs. */
5277 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
5278 if (attr != NULL)
5279 pst->dirname = DW_STRING (attr);
5280
5281 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5282
5283 dwarf2_find_base_address (comp_unit_die, cu);
5284
5285 /* Possibly set the default values of LOWPC and HIGHPC from
5286 `DW_AT_ranges'. */
5287 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
5288 &best_highpc, cu, pst);
5289 if (has_pc_info == 1 && best_lowpc < best_highpc)
5290 /* Store the contiguous range if it is not empty; it can be empty for
5291 CUs with no code. */
5292 addrmap_set_empty (objfile->psymtabs_addrmap,
5293 best_lowpc + baseaddr,
5294 best_highpc + baseaddr - 1, pst);
5295
5296 /* Check if comp unit has_children.
5297 If so, read the rest of the partial symbols from this comp unit.
5298 If not, there's no more debug_info for this comp unit. */
5299 if (has_children)
5300 {
5301 struct partial_die_info *first_die;
5302 CORE_ADDR lowpc, highpc;
5303
5304 lowpc = ((CORE_ADDR) -1);
5305 highpc = ((CORE_ADDR) 0);
5306
5307 first_die = load_partial_dies (reader, info_ptr, 1);
5308
5309 scan_partial_symbols (first_die, &lowpc, &highpc,
5310 ! has_pc_info, cu);
5311
5312 /* If we didn't find a lowpc, set it to highpc to avoid
5313 complaints from `maint check'. */
5314 if (lowpc == ((CORE_ADDR) -1))
5315 lowpc = highpc;
5316
5317 /* If the compilation unit didn't have an explicit address range,
5318 then use the information extracted from its child dies. */
5319 if (! has_pc_info)
5320 {
5321 best_lowpc = lowpc;
5322 best_highpc = highpc;
5323 }
5324 }
5325 pst->textlow = best_lowpc + baseaddr;
5326 pst->texthigh = best_highpc + baseaddr;
5327
5328 pst->n_global_syms = objfile->global_psymbols.next -
5329 (objfile->global_psymbols.list + pst->globals_offset);
5330 pst->n_static_syms = objfile->static_psymbols.next -
5331 (objfile->static_psymbols.list + pst->statics_offset);
5332 sort_pst_symbols (objfile, pst);
5333
5334 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
5335 {
5336 int i;
5337 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5338 struct dwarf2_per_cu_data *iter;
5339
5340 /* Fill in 'dependencies' here; we fill in 'users' in a
5341 post-pass. */
5342 pst->number_of_dependencies = len;
5343 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5344 len * sizeof (struct symtab *));
5345 for (i = 0;
5346 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
5347 i, iter);
5348 ++i)
5349 pst->dependencies[i] = iter->v.psymtab;
5350
5351 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5352 }
5353
5354 /* Get the list of files included in the current compilation unit,
5355 and build a psymtab for each of them. */
5356 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
5357
5358 if (dwarf2_read_debug)
5359 {
5360 struct gdbarch *gdbarch = get_objfile_arch (objfile);
5361
5362 fprintf_unfiltered (gdb_stdlog,
5363 "Psymtab for %s unit @0x%x: %s - %s"
5364 ", %d global, %d static syms\n",
5365 per_cu->is_debug_types ? "type" : "comp",
5366 per_cu->offset.sect_off,
5367 paddress (gdbarch, pst->textlow),
5368 paddress (gdbarch, pst->texthigh),
5369 pst->n_global_syms, pst->n_static_syms);
5370 }
5371 }
5372
5373 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5374 Process compilation unit THIS_CU for a psymtab. */
5375
5376 static void
5377 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
5378 int want_partial_unit)
5379 {
5380 /* If this compilation unit was already read in, free the
5381 cached copy in order to read it in again. This is
5382 necessary because we skipped some symbols when we first
5383 read in the compilation unit (see load_partial_dies).
5384 This problem could be avoided, but the benefit is unclear. */
5385 if (this_cu->cu != NULL)
5386 free_one_cached_comp_unit (this_cu);
5387
5388 gdb_assert (! this_cu->is_debug_types);
5389 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
5390 process_psymtab_comp_unit_reader,
5391 &want_partial_unit);
5392
5393 /* Age out any secondary CUs. */
5394 age_cached_comp_units ();
5395 }
5396
5397 /* Reader function for build_type_psymtabs. */
5398
5399 static void
5400 build_type_psymtabs_reader (const struct die_reader_specs *reader,
5401 gdb_byte *info_ptr,
5402 struct die_info *type_unit_die,
5403 int has_children,
5404 void *data)
5405 {
5406 struct objfile *objfile = dwarf2_per_objfile->objfile;
5407 struct dwarf2_cu *cu = reader->cu;
5408 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5409 struct type_unit_group *tu_group;
5410 struct attribute *attr;
5411 struct partial_die_info *first_die;
5412 CORE_ADDR lowpc, highpc;
5413 struct partial_symtab *pst;
5414
5415 gdb_assert (data == NULL);
5416
5417 if (! has_children)
5418 return;
5419
5420 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
5421 tu_group = get_type_unit_group (cu, attr);
5422
5423 VEC_safe_push (dwarf2_per_cu_ptr, tu_group->t.tus, per_cu);
5424
5425 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5426 cu->list_in_scope = &file_symbols;
5427 pst = create_partial_symtab (per_cu, "");
5428 pst->anonymous = 1;
5429
5430 first_die = load_partial_dies (reader, info_ptr, 1);
5431
5432 lowpc = (CORE_ADDR) -1;
5433 highpc = (CORE_ADDR) 0;
5434 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5435
5436 pst->n_global_syms = objfile->global_psymbols.next -
5437 (objfile->global_psymbols.list + pst->globals_offset);
5438 pst->n_static_syms = objfile->static_psymbols.next -
5439 (objfile->static_psymbols.list + pst->statics_offset);
5440 sort_pst_symbols (objfile, pst);
5441 }
5442
5443 /* Traversal function for build_type_psymtabs. */
5444
5445 static int
5446 build_type_psymtab_dependencies (void **slot, void *info)
5447 {
5448 struct objfile *objfile = dwarf2_per_objfile->objfile;
5449 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
5450 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
5451 struct partial_symtab *pst = per_cu->v.psymtab;
5452 int len = VEC_length (dwarf2_per_cu_ptr, tu_group->t.tus);
5453 struct dwarf2_per_cu_data *iter;
5454 int i;
5455
5456 gdb_assert (len > 0);
5457
5458 pst->number_of_dependencies = len;
5459 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5460 len * sizeof (struct psymtab *));
5461 for (i = 0;
5462 VEC_iterate (dwarf2_per_cu_ptr, tu_group->t.tus, i, iter);
5463 ++i)
5464 {
5465 pst->dependencies[i] = iter->v.psymtab;
5466 iter->type_unit_group = tu_group;
5467 }
5468
5469 VEC_free (dwarf2_per_cu_ptr, tu_group->t.tus);
5470
5471 return 1;
5472 }
5473
5474 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5475 Build partial symbol tables for the .debug_types comp-units. */
5476
5477 static void
5478 build_type_psymtabs (struct objfile *objfile)
5479 {
5480 if (! create_all_type_units (objfile))
5481 return;
5482
5483 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5484
5485 /* Now that all TUs have been processed we can fill in the dependencies. */
5486 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5487 build_type_psymtab_dependencies, NULL);
5488 }
5489
5490 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
5491
5492 static void
5493 psymtabs_addrmap_cleanup (void *o)
5494 {
5495 struct objfile *objfile = o;
5496
5497 objfile->psymtabs_addrmap = NULL;
5498 }
5499
5500 /* Compute the 'user' field for each psymtab in OBJFILE. */
5501
5502 static void
5503 set_partial_user (struct objfile *objfile)
5504 {
5505 int i;
5506
5507 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5508 {
5509 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5510 struct partial_symtab *pst = per_cu->v.psymtab;
5511 int j;
5512
5513 if (pst == NULL)
5514 continue;
5515
5516 for (j = 0; j < pst->number_of_dependencies; ++j)
5517 {
5518 /* Set the 'user' field only if it is not already set. */
5519 if (pst->dependencies[j]->user == NULL)
5520 pst->dependencies[j]->user = pst;
5521 }
5522 }
5523 }
5524
5525 /* Build the partial symbol table by doing a quick pass through the
5526 .debug_info and .debug_abbrev sections. */
5527
5528 static void
5529 dwarf2_build_psymtabs_hard (struct objfile *objfile)
5530 {
5531 struct cleanup *back_to, *addrmap_cleanup;
5532 struct obstack temp_obstack;
5533 int i;
5534
5535 if (dwarf2_read_debug)
5536 {
5537 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5538 objfile->name);
5539 }
5540
5541 dwarf2_per_objfile->reading_partial_symbols = 1;
5542
5543 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
5544
5545 /* Any cached compilation units will be linked by the per-objfile
5546 read_in_chain. Make sure to free them when we're done. */
5547 back_to = make_cleanup (free_cached_comp_units, NULL);
5548
5549 build_type_psymtabs (objfile);
5550
5551 create_all_comp_units (objfile);
5552
5553 /* Create a temporary address map on a temporary obstack. We later
5554 copy this to the final obstack. */
5555 obstack_init (&temp_obstack);
5556 make_cleanup_obstack_free (&temp_obstack);
5557 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
5558 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
5559
5560 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5561 {
5562 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5563
5564 process_psymtab_comp_unit (per_cu, 0);
5565 }
5566
5567 set_partial_user (objfile);
5568
5569 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
5570 &objfile->objfile_obstack);
5571 discard_cleanups (addrmap_cleanup);
5572
5573 do_cleanups (back_to);
5574
5575 if (dwarf2_read_debug)
5576 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
5577 objfile->name);
5578 }
5579
5580 /* die_reader_func for load_partial_comp_unit. */
5581
5582 static void
5583 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
5584 gdb_byte *info_ptr,
5585 struct die_info *comp_unit_die,
5586 int has_children,
5587 void *data)
5588 {
5589 struct dwarf2_cu *cu = reader->cu;
5590
5591 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
5592
5593 /* Check if comp unit has_children.
5594 If so, read the rest of the partial symbols from this comp unit.
5595 If not, there's no more debug_info for this comp unit. */
5596 if (has_children)
5597 load_partial_dies (reader, info_ptr, 0);
5598 }
5599
5600 /* Load the partial DIEs for a secondary CU into memory.
5601 This is also used when rereading a primary CU with load_all_dies. */
5602
5603 static void
5604 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
5605 {
5606 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
5607 load_partial_comp_unit_reader, NULL);
5608 }
5609
5610 static void
5611 read_comp_units_from_section (struct objfile *objfile,
5612 struct dwarf2_section_info *section,
5613 unsigned int is_dwz,
5614 int *n_allocated,
5615 int *n_comp_units,
5616 struct dwarf2_per_cu_data ***all_comp_units)
5617 {
5618 gdb_byte *info_ptr;
5619 bfd *abfd = section->asection->owner;
5620
5621 dwarf2_read_section (objfile, section);
5622
5623 info_ptr = section->buffer;
5624
5625 while (info_ptr < section->buffer + section->size)
5626 {
5627 unsigned int length, initial_length_size;
5628 struct dwarf2_per_cu_data *this_cu;
5629 sect_offset offset;
5630
5631 offset.sect_off = info_ptr - section->buffer;
5632
5633 /* Read just enough information to find out where the next
5634 compilation unit is. */
5635 length = read_initial_length (abfd, info_ptr, &initial_length_size);
5636
5637 /* Save the compilation unit for later lookup. */
5638 this_cu = obstack_alloc (&objfile->objfile_obstack,
5639 sizeof (struct dwarf2_per_cu_data));
5640 memset (this_cu, 0, sizeof (*this_cu));
5641 this_cu->offset = offset;
5642 this_cu->length = length + initial_length_size;
5643 this_cu->is_dwz = is_dwz;
5644 this_cu->objfile = objfile;
5645 this_cu->info_or_types_section = section;
5646
5647 if (*n_comp_units == *n_allocated)
5648 {
5649 *n_allocated *= 2;
5650 *all_comp_units = xrealloc (*all_comp_units,
5651 *n_allocated
5652 * sizeof (struct dwarf2_per_cu_data *));
5653 }
5654 (*all_comp_units)[*n_comp_units] = this_cu;
5655 ++*n_comp_units;
5656
5657 info_ptr = info_ptr + this_cu->length;
5658 }
5659 }
5660
5661 /* Create a list of all compilation units in OBJFILE.
5662 This is only done for -readnow and building partial symtabs. */
5663
5664 static void
5665 create_all_comp_units (struct objfile *objfile)
5666 {
5667 int n_allocated;
5668 int n_comp_units;
5669 struct dwarf2_per_cu_data **all_comp_units;
5670
5671 n_comp_units = 0;
5672 n_allocated = 10;
5673 all_comp_units = xmalloc (n_allocated
5674 * sizeof (struct dwarf2_per_cu_data *));
5675
5676 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
5677 &n_allocated, &n_comp_units, &all_comp_units);
5678
5679 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
5680 {
5681 struct dwz_file *dwz = dwarf2_get_dwz_file ();
5682
5683 read_comp_units_from_section (objfile, &dwz->info, 1,
5684 &n_allocated, &n_comp_units,
5685 &all_comp_units);
5686 }
5687
5688 dwarf2_per_objfile->all_comp_units
5689 = obstack_alloc (&objfile->objfile_obstack,
5690 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5691 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
5692 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5693 xfree (all_comp_units);
5694 dwarf2_per_objfile->n_comp_units = n_comp_units;
5695 }
5696
5697 /* Process all loaded DIEs for compilation unit CU, starting at
5698 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
5699 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
5700 DW_AT_ranges). If NEED_PC is set, then this function will set
5701 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
5702 and record the covered ranges in the addrmap. */
5703
5704 static void
5705 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5706 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
5707 {
5708 struct partial_die_info *pdi;
5709
5710 /* Now, march along the PDI's, descending into ones which have
5711 interesting children but skipping the children of the other ones,
5712 until we reach the end of the compilation unit. */
5713
5714 pdi = first_die;
5715
5716 while (pdi != NULL)
5717 {
5718 fixup_partial_die (pdi, cu);
5719
5720 /* Anonymous namespaces or modules have no name but have interesting
5721 children, so we need to look at them. Ditto for anonymous
5722 enums. */
5723
5724 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
5725 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
5726 || pdi->tag == DW_TAG_imported_unit)
5727 {
5728 switch (pdi->tag)
5729 {
5730 case DW_TAG_subprogram:
5731 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
5732 break;
5733 case DW_TAG_constant:
5734 case DW_TAG_variable:
5735 case DW_TAG_typedef:
5736 case DW_TAG_union_type:
5737 if (!pdi->is_declaration)
5738 {
5739 add_partial_symbol (pdi, cu);
5740 }
5741 break;
5742 case DW_TAG_class_type:
5743 case DW_TAG_interface_type:
5744 case DW_TAG_structure_type:
5745 if (!pdi->is_declaration)
5746 {
5747 add_partial_symbol (pdi, cu);
5748 }
5749 break;
5750 case DW_TAG_enumeration_type:
5751 if (!pdi->is_declaration)
5752 add_partial_enumeration (pdi, cu);
5753 break;
5754 case DW_TAG_base_type:
5755 case DW_TAG_subrange_type:
5756 /* File scope base type definitions are added to the partial
5757 symbol table. */
5758 add_partial_symbol (pdi, cu);
5759 break;
5760 case DW_TAG_namespace:
5761 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
5762 break;
5763 case DW_TAG_module:
5764 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
5765 break;
5766 case DW_TAG_imported_unit:
5767 {
5768 struct dwarf2_per_cu_data *per_cu;
5769
5770 /* For now we don't handle imported units in type units. */
5771 if (cu->per_cu->is_debug_types)
5772 {
5773 error (_("Dwarf Error: DW_TAG_imported_unit is not"
5774 " supported in type units [in module %s]"),
5775 cu->objfile->name);
5776 }
5777
5778 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
5779 pdi->is_dwz,
5780 cu->objfile);
5781
5782 /* Go read the partial unit, if needed. */
5783 if (per_cu->v.psymtab == NULL)
5784 process_psymtab_comp_unit (per_cu, 1);
5785
5786 VEC_safe_push (dwarf2_per_cu_ptr,
5787 cu->per_cu->imported_symtabs, per_cu);
5788 }
5789 break;
5790 default:
5791 break;
5792 }
5793 }
5794
5795 /* If the die has a sibling, skip to the sibling. */
5796
5797 pdi = pdi->die_sibling;
5798 }
5799 }
5800
5801 /* Functions used to compute the fully scoped name of a partial DIE.
5802
5803 Normally, this is simple. For C++, the parent DIE's fully scoped
5804 name is concatenated with "::" and the partial DIE's name. For
5805 Java, the same thing occurs except that "." is used instead of "::".
5806 Enumerators are an exception; they use the scope of their parent
5807 enumeration type, i.e. the name of the enumeration type is not
5808 prepended to the enumerator.
5809
5810 There are two complexities. One is DW_AT_specification; in this
5811 case "parent" means the parent of the target of the specification,
5812 instead of the direct parent of the DIE. The other is compilers
5813 which do not emit DW_TAG_namespace; in this case we try to guess
5814 the fully qualified name of structure types from their members'
5815 linkage names. This must be done using the DIE's children rather
5816 than the children of any DW_AT_specification target. We only need
5817 to do this for structures at the top level, i.e. if the target of
5818 any DW_AT_specification (if any; otherwise the DIE itself) does not
5819 have a parent. */
5820
5821 /* Compute the scope prefix associated with PDI's parent, in
5822 compilation unit CU. The result will be allocated on CU's
5823 comp_unit_obstack, or a copy of the already allocated PDI->NAME
5824 field. NULL is returned if no prefix is necessary. */
5825 static const char *
5826 partial_die_parent_scope (struct partial_die_info *pdi,
5827 struct dwarf2_cu *cu)
5828 {
5829 const char *grandparent_scope;
5830 struct partial_die_info *parent, *real_pdi;
5831
5832 /* We need to look at our parent DIE; if we have a DW_AT_specification,
5833 then this means the parent of the specification DIE. */
5834
5835 real_pdi = pdi;
5836 while (real_pdi->has_specification)
5837 real_pdi = find_partial_die (real_pdi->spec_offset,
5838 real_pdi->spec_is_dwz, cu);
5839
5840 parent = real_pdi->die_parent;
5841 if (parent == NULL)
5842 return NULL;
5843
5844 if (parent->scope_set)
5845 return parent->scope;
5846
5847 fixup_partial_die (parent, cu);
5848
5849 grandparent_scope = partial_die_parent_scope (parent, cu);
5850
5851 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
5852 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
5853 Work around this problem here. */
5854 if (cu->language == language_cplus
5855 && parent->tag == DW_TAG_namespace
5856 && strcmp (parent->name, "::") == 0
5857 && grandparent_scope == NULL)
5858 {
5859 parent->scope = NULL;
5860 parent->scope_set = 1;
5861 return NULL;
5862 }
5863
5864 if (pdi->tag == DW_TAG_enumerator)
5865 /* Enumerators should not get the name of the enumeration as a prefix. */
5866 parent->scope = grandparent_scope;
5867 else if (parent->tag == DW_TAG_namespace
5868 || parent->tag == DW_TAG_module
5869 || parent->tag == DW_TAG_structure_type
5870 || parent->tag == DW_TAG_class_type
5871 || parent->tag == DW_TAG_interface_type
5872 || parent->tag == DW_TAG_union_type
5873 || parent->tag == DW_TAG_enumeration_type)
5874 {
5875 if (grandparent_scope == NULL)
5876 parent->scope = parent->name;
5877 else
5878 parent->scope = typename_concat (&cu->comp_unit_obstack,
5879 grandparent_scope,
5880 parent->name, 0, cu);
5881 }
5882 else
5883 {
5884 /* FIXME drow/2004-04-01: What should we be doing with
5885 function-local names? For partial symbols, we should probably be
5886 ignoring them. */
5887 complaint (&symfile_complaints,
5888 _("unhandled containing DIE tag %d for DIE at %d"),
5889 parent->tag, pdi->offset.sect_off);
5890 parent->scope = grandparent_scope;
5891 }
5892
5893 parent->scope_set = 1;
5894 return parent->scope;
5895 }
5896
5897 /* Return the fully scoped name associated with PDI, from compilation unit
5898 CU. The result will be allocated with malloc. */
5899
5900 static char *
5901 partial_die_full_name (struct partial_die_info *pdi,
5902 struct dwarf2_cu *cu)
5903 {
5904 const char *parent_scope;
5905
5906 /* If this is a template instantiation, we can not work out the
5907 template arguments from partial DIEs. So, unfortunately, we have
5908 to go through the full DIEs. At least any work we do building
5909 types here will be reused if full symbols are loaded later. */
5910 if (pdi->has_template_arguments)
5911 {
5912 fixup_partial_die (pdi, cu);
5913
5914 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
5915 {
5916 struct die_info *die;
5917 struct attribute attr;
5918 struct dwarf2_cu *ref_cu = cu;
5919
5920 /* DW_FORM_ref_addr is using section offset. */
5921 attr.name = 0;
5922 attr.form = DW_FORM_ref_addr;
5923 attr.u.unsnd = pdi->offset.sect_off;
5924 die = follow_die_ref (NULL, &attr, &ref_cu);
5925
5926 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
5927 }
5928 }
5929
5930 parent_scope = partial_die_parent_scope (pdi, cu);
5931 if (parent_scope == NULL)
5932 return NULL;
5933 else
5934 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
5935 }
5936
5937 static void
5938 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
5939 {
5940 struct objfile *objfile = cu->objfile;
5941 CORE_ADDR addr = 0;
5942 const char *actual_name = NULL;
5943 CORE_ADDR baseaddr;
5944 char *built_actual_name;
5945
5946 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5947
5948 built_actual_name = partial_die_full_name (pdi, cu);
5949 if (built_actual_name != NULL)
5950 actual_name = built_actual_name;
5951
5952 if (actual_name == NULL)
5953 actual_name = pdi->name;
5954
5955 switch (pdi->tag)
5956 {
5957 case DW_TAG_subprogram:
5958 if (pdi->is_external || cu->language == language_ada)
5959 {
5960 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
5961 of the global scope. But in Ada, we want to be able to access
5962 nested procedures globally. So all Ada subprograms are stored
5963 in the global scope. */
5964 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
5965 mst_text, objfile); */
5966 add_psymbol_to_list (actual_name, strlen (actual_name),
5967 built_actual_name != NULL,
5968 VAR_DOMAIN, LOC_BLOCK,
5969 &objfile->global_psymbols,
5970 0, pdi->lowpc + baseaddr,
5971 cu->language, objfile);
5972 }
5973 else
5974 {
5975 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
5976 mst_file_text, objfile); */
5977 add_psymbol_to_list (actual_name, strlen (actual_name),
5978 built_actual_name != NULL,
5979 VAR_DOMAIN, LOC_BLOCK,
5980 &objfile->static_psymbols,
5981 0, pdi->lowpc + baseaddr,
5982 cu->language, objfile);
5983 }
5984 break;
5985 case DW_TAG_constant:
5986 {
5987 struct psymbol_allocation_list *list;
5988
5989 if (pdi->is_external)
5990 list = &objfile->global_psymbols;
5991 else
5992 list = &objfile->static_psymbols;
5993 add_psymbol_to_list (actual_name, strlen (actual_name),
5994 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
5995 list, 0, 0, cu->language, objfile);
5996 }
5997 break;
5998 case DW_TAG_variable:
5999 if (pdi->d.locdesc)
6000 addr = decode_locdesc (pdi->d.locdesc, cu);
6001
6002 if (pdi->d.locdesc
6003 && addr == 0
6004 && !dwarf2_per_objfile->has_section_at_zero)
6005 {
6006 /* A global or static variable may also have been stripped
6007 out by the linker if unused, in which case its address
6008 will be nullified; do not add such variables into partial
6009 symbol table then. */
6010 }
6011 else if (pdi->is_external)
6012 {
6013 /* Global Variable.
6014 Don't enter into the minimal symbol tables as there is
6015 a minimal symbol table entry from the ELF symbols already.
6016 Enter into partial symbol table if it has a location
6017 descriptor or a type.
6018 If the location descriptor is missing, new_symbol will create
6019 a LOC_UNRESOLVED symbol, the address of the variable will then
6020 be determined from the minimal symbol table whenever the variable
6021 is referenced.
6022 The address for the partial symbol table entry is not
6023 used by GDB, but it comes in handy for debugging partial symbol
6024 table building. */
6025
6026 if (pdi->d.locdesc || pdi->has_type)
6027 add_psymbol_to_list (actual_name, strlen (actual_name),
6028 built_actual_name != NULL,
6029 VAR_DOMAIN, LOC_STATIC,
6030 &objfile->global_psymbols,
6031 0, addr + baseaddr,
6032 cu->language, objfile);
6033 }
6034 else
6035 {
6036 /* Static Variable. Skip symbols without location descriptors. */
6037 if (pdi->d.locdesc == NULL)
6038 {
6039 xfree (built_actual_name);
6040 return;
6041 }
6042 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
6043 mst_file_data, objfile); */
6044 add_psymbol_to_list (actual_name, strlen (actual_name),
6045 built_actual_name != NULL,
6046 VAR_DOMAIN, LOC_STATIC,
6047 &objfile->static_psymbols,
6048 0, addr + baseaddr,
6049 cu->language, objfile);
6050 }
6051 break;
6052 case DW_TAG_typedef:
6053 case DW_TAG_base_type:
6054 case DW_TAG_subrange_type:
6055 add_psymbol_to_list (actual_name, strlen (actual_name),
6056 built_actual_name != NULL,
6057 VAR_DOMAIN, LOC_TYPEDEF,
6058 &objfile->static_psymbols,
6059 0, (CORE_ADDR) 0, cu->language, objfile);
6060 break;
6061 case DW_TAG_namespace:
6062 add_psymbol_to_list (actual_name, strlen (actual_name),
6063 built_actual_name != NULL,
6064 VAR_DOMAIN, LOC_TYPEDEF,
6065 &objfile->global_psymbols,
6066 0, (CORE_ADDR) 0, cu->language, objfile);
6067 break;
6068 case DW_TAG_class_type:
6069 case DW_TAG_interface_type:
6070 case DW_TAG_structure_type:
6071 case DW_TAG_union_type:
6072 case DW_TAG_enumeration_type:
6073 /* Skip external references. The DWARF standard says in the section
6074 about "Structure, Union, and Class Type Entries": "An incomplete
6075 structure, union or class type is represented by a structure,
6076 union or class entry that does not have a byte size attribute
6077 and that has a DW_AT_declaration attribute." */
6078 if (!pdi->has_byte_size && pdi->is_declaration)
6079 {
6080 xfree (built_actual_name);
6081 return;
6082 }
6083
6084 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
6085 static vs. global. */
6086 add_psymbol_to_list (actual_name, strlen (actual_name),
6087 built_actual_name != NULL,
6088 STRUCT_DOMAIN, LOC_TYPEDEF,
6089 (cu->language == language_cplus
6090 || cu->language == language_java)
6091 ? &objfile->global_psymbols
6092 : &objfile->static_psymbols,
6093 0, (CORE_ADDR) 0, cu->language, objfile);
6094
6095 break;
6096 case DW_TAG_enumerator:
6097 add_psymbol_to_list (actual_name, strlen (actual_name),
6098 built_actual_name != NULL,
6099 VAR_DOMAIN, LOC_CONST,
6100 (cu->language == language_cplus
6101 || cu->language == language_java)
6102 ? &objfile->global_psymbols
6103 : &objfile->static_psymbols,
6104 0, (CORE_ADDR) 0, cu->language, objfile);
6105 break;
6106 default:
6107 break;
6108 }
6109
6110 xfree (built_actual_name);
6111 }
6112
6113 /* Read a partial die corresponding to a namespace; also, add a symbol
6114 corresponding to that namespace to the symbol table. NAMESPACE is
6115 the name of the enclosing namespace. */
6116
6117 static void
6118 add_partial_namespace (struct partial_die_info *pdi,
6119 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6120 int need_pc, struct dwarf2_cu *cu)
6121 {
6122 /* Add a symbol for the namespace. */
6123
6124 add_partial_symbol (pdi, cu);
6125
6126 /* Now scan partial symbols in that namespace. */
6127
6128 if (pdi->has_children)
6129 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6130 }
6131
6132 /* Read a partial die corresponding to a Fortran module. */
6133
6134 static void
6135 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
6136 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6137 {
6138 /* Now scan partial symbols in that module. */
6139
6140 if (pdi->has_children)
6141 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6142 }
6143
6144 /* Read a partial die corresponding to a subprogram and create a partial
6145 symbol for that subprogram. When the CU language allows it, this
6146 routine also defines a partial symbol for each nested subprogram
6147 that this subprogram contains.
6148
6149 DIE my also be a lexical block, in which case we simply search
6150 recursively for suprograms defined inside that lexical block.
6151 Again, this is only performed when the CU language allows this
6152 type of definitions. */
6153
6154 static void
6155 add_partial_subprogram (struct partial_die_info *pdi,
6156 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6157 int need_pc, struct dwarf2_cu *cu)
6158 {
6159 if (pdi->tag == DW_TAG_subprogram)
6160 {
6161 if (pdi->has_pc_info)
6162 {
6163 if (pdi->lowpc < *lowpc)
6164 *lowpc = pdi->lowpc;
6165 if (pdi->highpc > *highpc)
6166 *highpc = pdi->highpc;
6167 if (need_pc)
6168 {
6169 CORE_ADDR baseaddr;
6170 struct objfile *objfile = cu->objfile;
6171
6172 baseaddr = ANOFFSET (objfile->section_offsets,
6173 SECT_OFF_TEXT (objfile));
6174 addrmap_set_empty (objfile->psymtabs_addrmap,
6175 pdi->lowpc + baseaddr,
6176 pdi->highpc - 1 + baseaddr,
6177 cu->per_cu->v.psymtab);
6178 }
6179 }
6180
6181 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6182 {
6183 if (!pdi->is_declaration)
6184 /* Ignore subprogram DIEs that do not have a name, they are
6185 illegal. Do not emit a complaint at this point, we will
6186 do so when we convert this psymtab into a symtab. */
6187 if (pdi->name)
6188 add_partial_symbol (pdi, cu);
6189 }
6190 }
6191
6192 if (! pdi->has_children)
6193 return;
6194
6195 if (cu->language == language_ada)
6196 {
6197 pdi = pdi->die_child;
6198 while (pdi != NULL)
6199 {
6200 fixup_partial_die (pdi, cu);
6201 if (pdi->tag == DW_TAG_subprogram
6202 || pdi->tag == DW_TAG_lexical_block)
6203 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
6204 pdi = pdi->die_sibling;
6205 }
6206 }
6207 }
6208
6209 /* Read a partial die corresponding to an enumeration type. */
6210
6211 static void
6212 add_partial_enumeration (struct partial_die_info *enum_pdi,
6213 struct dwarf2_cu *cu)
6214 {
6215 struct partial_die_info *pdi;
6216
6217 if (enum_pdi->name != NULL)
6218 add_partial_symbol (enum_pdi, cu);
6219
6220 pdi = enum_pdi->die_child;
6221 while (pdi)
6222 {
6223 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
6224 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
6225 else
6226 add_partial_symbol (pdi, cu);
6227 pdi = pdi->die_sibling;
6228 }
6229 }
6230
6231 /* Return the initial uleb128 in the die at INFO_PTR. */
6232
6233 static unsigned int
6234 peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
6235 {
6236 unsigned int bytes_read;
6237
6238 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6239 }
6240
6241 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6242 Return the corresponding abbrev, or NULL if the number is zero (indicating
6243 an empty DIE). In either case *BYTES_READ will be set to the length of
6244 the initial number. */
6245
6246 static struct abbrev_info *
6247 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
6248 struct dwarf2_cu *cu)
6249 {
6250 bfd *abfd = cu->objfile->obfd;
6251 unsigned int abbrev_number;
6252 struct abbrev_info *abbrev;
6253
6254 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6255
6256 if (abbrev_number == 0)
6257 return NULL;
6258
6259 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
6260 if (!abbrev)
6261 {
6262 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6263 abbrev_number, bfd_get_filename (abfd));
6264 }
6265
6266 return abbrev;
6267 }
6268
6269 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6270 Returns a pointer to the end of a series of DIEs, terminated by an empty
6271 DIE. Any children of the skipped DIEs will also be skipped. */
6272
6273 static gdb_byte *
6274 skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
6275 {
6276 struct dwarf2_cu *cu = reader->cu;
6277 struct abbrev_info *abbrev;
6278 unsigned int bytes_read;
6279
6280 while (1)
6281 {
6282 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6283 if (abbrev == NULL)
6284 return info_ptr + bytes_read;
6285 else
6286 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
6287 }
6288 }
6289
6290 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6291 INFO_PTR should point just after the initial uleb128 of a DIE, and the
6292 abbrev corresponding to that skipped uleb128 should be passed in
6293 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6294 children. */
6295
6296 static gdb_byte *
6297 skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
6298 struct abbrev_info *abbrev)
6299 {
6300 unsigned int bytes_read;
6301 struct attribute attr;
6302 bfd *abfd = reader->abfd;
6303 struct dwarf2_cu *cu = reader->cu;
6304 gdb_byte *buffer = reader->buffer;
6305 const gdb_byte *buffer_end = reader->buffer_end;
6306 gdb_byte *start_info_ptr = info_ptr;
6307 unsigned int form, i;
6308
6309 for (i = 0; i < abbrev->num_attrs; i++)
6310 {
6311 /* The only abbrev we care about is DW_AT_sibling. */
6312 if (abbrev->attrs[i].name == DW_AT_sibling)
6313 {
6314 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
6315 if (attr.form == DW_FORM_ref_addr)
6316 complaint (&symfile_complaints,
6317 _("ignoring absolute DW_AT_sibling"));
6318 else
6319 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
6320 }
6321
6322 /* If it isn't DW_AT_sibling, skip this attribute. */
6323 form = abbrev->attrs[i].form;
6324 skip_attribute:
6325 switch (form)
6326 {
6327 case DW_FORM_ref_addr:
6328 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6329 and later it is offset sized. */
6330 if (cu->header.version == 2)
6331 info_ptr += cu->header.addr_size;
6332 else
6333 info_ptr += cu->header.offset_size;
6334 break;
6335 case DW_FORM_GNU_ref_alt:
6336 info_ptr += cu->header.offset_size;
6337 break;
6338 case DW_FORM_addr:
6339 info_ptr += cu->header.addr_size;
6340 break;
6341 case DW_FORM_data1:
6342 case DW_FORM_ref1:
6343 case DW_FORM_flag:
6344 info_ptr += 1;
6345 break;
6346 case DW_FORM_flag_present:
6347 break;
6348 case DW_FORM_data2:
6349 case DW_FORM_ref2:
6350 info_ptr += 2;
6351 break;
6352 case DW_FORM_data4:
6353 case DW_FORM_ref4:
6354 info_ptr += 4;
6355 break;
6356 case DW_FORM_data8:
6357 case DW_FORM_ref8:
6358 case DW_FORM_ref_sig8:
6359 info_ptr += 8;
6360 break;
6361 case DW_FORM_string:
6362 read_direct_string (abfd, info_ptr, &bytes_read);
6363 info_ptr += bytes_read;
6364 break;
6365 case DW_FORM_sec_offset:
6366 case DW_FORM_strp:
6367 case DW_FORM_GNU_strp_alt:
6368 info_ptr += cu->header.offset_size;
6369 break;
6370 case DW_FORM_exprloc:
6371 case DW_FORM_block:
6372 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6373 info_ptr += bytes_read;
6374 break;
6375 case DW_FORM_block1:
6376 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6377 break;
6378 case DW_FORM_block2:
6379 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6380 break;
6381 case DW_FORM_block4:
6382 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6383 break;
6384 case DW_FORM_sdata:
6385 case DW_FORM_udata:
6386 case DW_FORM_ref_udata:
6387 case DW_FORM_GNU_addr_index:
6388 case DW_FORM_GNU_str_index:
6389 info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
6390 break;
6391 case DW_FORM_indirect:
6392 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6393 info_ptr += bytes_read;
6394 /* We need to continue parsing from here, so just go back to
6395 the top. */
6396 goto skip_attribute;
6397
6398 default:
6399 error (_("Dwarf Error: Cannot handle %s "
6400 "in DWARF reader [in module %s]"),
6401 dwarf_form_name (form),
6402 bfd_get_filename (abfd));
6403 }
6404 }
6405
6406 if (abbrev->has_children)
6407 return skip_children (reader, info_ptr);
6408 else
6409 return info_ptr;
6410 }
6411
6412 /* Locate ORIG_PDI's sibling.
6413 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
6414
6415 static gdb_byte *
6416 locate_pdi_sibling (const struct die_reader_specs *reader,
6417 struct partial_die_info *orig_pdi,
6418 gdb_byte *info_ptr)
6419 {
6420 /* Do we know the sibling already? */
6421
6422 if (orig_pdi->sibling)
6423 return orig_pdi->sibling;
6424
6425 /* Are there any children to deal with? */
6426
6427 if (!orig_pdi->has_children)
6428 return info_ptr;
6429
6430 /* Skip the children the long way. */
6431
6432 return skip_children (reader, info_ptr);
6433 }
6434
6435 /* Expand this partial symbol table into a full symbol table. SELF is
6436 not NULL. */
6437
6438 static void
6439 dwarf2_read_symtab (struct partial_symtab *self,
6440 struct objfile *objfile)
6441 {
6442 if (self->readin)
6443 {
6444 warning (_("bug: psymtab for %s is already read in."),
6445 self->filename);
6446 }
6447 else
6448 {
6449 if (info_verbose)
6450 {
6451 printf_filtered (_("Reading in symbols for %s..."),
6452 self->filename);
6453 gdb_flush (gdb_stdout);
6454 }
6455
6456 /* Restore our global data. */
6457 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
6458
6459 /* If this psymtab is constructed from a debug-only objfile, the
6460 has_section_at_zero flag will not necessarily be correct. We
6461 can get the correct value for this flag by looking at the data
6462 associated with the (presumably stripped) associated objfile. */
6463 if (objfile->separate_debug_objfile_backlink)
6464 {
6465 struct dwarf2_per_objfile *dpo_backlink
6466 = objfile_data (objfile->separate_debug_objfile_backlink,
6467 dwarf2_objfile_data_key);
6468
6469 dwarf2_per_objfile->has_section_at_zero
6470 = dpo_backlink->has_section_at_zero;
6471 }
6472
6473 dwarf2_per_objfile->reading_partial_symbols = 0;
6474
6475 psymtab_to_symtab_1 (self);
6476
6477 /* Finish up the debug error message. */
6478 if (info_verbose)
6479 printf_filtered (_("done.\n"));
6480 }
6481
6482 process_cu_includes ();
6483 }
6484 \f
6485 /* Reading in full CUs. */
6486
6487 /* Add PER_CU to the queue. */
6488
6489 static void
6490 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6491 enum language pretend_language)
6492 {
6493 struct dwarf2_queue_item *item;
6494
6495 per_cu->queued = 1;
6496 item = xmalloc (sizeof (*item));
6497 item->per_cu = per_cu;
6498 item->pretend_language = pretend_language;
6499 item->next = NULL;
6500
6501 if (dwarf2_queue == NULL)
6502 dwarf2_queue = item;
6503 else
6504 dwarf2_queue_tail->next = item;
6505
6506 dwarf2_queue_tail = item;
6507 }
6508
6509 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
6510 unit and add it to our queue.
6511 The result is non-zero if PER_CU was queued, otherwise the result is zero
6512 meaning either PER_CU is already queued or it is already loaded. */
6513
6514 static int
6515 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6516 struct dwarf2_per_cu_data *per_cu,
6517 enum language pretend_language)
6518 {
6519 /* We may arrive here during partial symbol reading, if we need full
6520 DIEs to process an unusual case (e.g. template arguments). Do
6521 not queue PER_CU, just tell our caller to load its DIEs. */
6522 if (dwarf2_per_objfile->reading_partial_symbols)
6523 {
6524 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6525 return 1;
6526 return 0;
6527 }
6528
6529 /* Mark the dependence relation so that we don't flush PER_CU
6530 too early. */
6531 dwarf2_add_dependence (this_cu, per_cu);
6532
6533 /* If it's already on the queue, we have nothing to do. */
6534 if (per_cu->queued)
6535 return 0;
6536
6537 /* If the compilation unit is already loaded, just mark it as
6538 used. */
6539 if (per_cu->cu != NULL)
6540 {
6541 per_cu->cu->last_used = 0;
6542 return 0;
6543 }
6544
6545 /* Add it to the queue. */
6546 queue_comp_unit (per_cu, pretend_language);
6547
6548 return 1;
6549 }
6550
6551 /* Process the queue. */
6552
6553 static void
6554 process_queue (void)
6555 {
6556 struct dwarf2_queue_item *item, *next_item;
6557
6558 if (dwarf2_read_debug)
6559 {
6560 fprintf_unfiltered (gdb_stdlog,
6561 "Expanding one or more symtabs of objfile %s ...\n",
6562 dwarf2_per_objfile->objfile->name);
6563 }
6564
6565 /* The queue starts out with one item, but following a DIE reference
6566 may load a new CU, adding it to the end of the queue. */
6567 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
6568 {
6569 if (dwarf2_per_objfile->using_index
6570 ? !item->per_cu->v.quick->symtab
6571 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
6572 {
6573 struct dwarf2_per_cu_data *per_cu = item->per_cu;
6574
6575 if (dwarf2_read_debug)
6576 {
6577 fprintf_unfiltered (gdb_stdlog,
6578 "Expanding symtab of %s at offset 0x%x\n",
6579 per_cu->is_debug_types ? "TU" : "CU",
6580 per_cu->offset.sect_off);
6581 }
6582
6583 if (per_cu->is_debug_types)
6584 process_full_type_unit (per_cu, item->pretend_language);
6585 else
6586 process_full_comp_unit (per_cu, item->pretend_language);
6587
6588 if (dwarf2_read_debug)
6589 {
6590 fprintf_unfiltered (gdb_stdlog,
6591 "Done expanding %s at offset 0x%x\n",
6592 per_cu->is_debug_types ? "TU" : "CU",
6593 per_cu->offset.sect_off);
6594 }
6595 }
6596
6597 item->per_cu->queued = 0;
6598 next_item = item->next;
6599 xfree (item);
6600 }
6601
6602 dwarf2_queue_tail = NULL;
6603
6604 if (dwarf2_read_debug)
6605 {
6606 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
6607 dwarf2_per_objfile->objfile->name);
6608 }
6609 }
6610
6611 /* Free all allocated queue entries. This function only releases anything if
6612 an error was thrown; if the queue was processed then it would have been
6613 freed as we went along. */
6614
6615 static void
6616 dwarf2_release_queue (void *dummy)
6617 {
6618 struct dwarf2_queue_item *item, *last;
6619
6620 item = dwarf2_queue;
6621 while (item)
6622 {
6623 /* Anything still marked queued is likely to be in an
6624 inconsistent state, so discard it. */
6625 if (item->per_cu->queued)
6626 {
6627 if (item->per_cu->cu != NULL)
6628 free_one_cached_comp_unit (item->per_cu);
6629 item->per_cu->queued = 0;
6630 }
6631
6632 last = item;
6633 item = item->next;
6634 xfree (last);
6635 }
6636
6637 dwarf2_queue = dwarf2_queue_tail = NULL;
6638 }
6639
6640 /* Read in full symbols for PST, and anything it depends on. */
6641
6642 static void
6643 psymtab_to_symtab_1 (struct partial_symtab *pst)
6644 {
6645 struct dwarf2_per_cu_data *per_cu;
6646 int i;
6647
6648 if (pst->readin)
6649 return;
6650
6651 for (i = 0; i < pst->number_of_dependencies; i++)
6652 if (!pst->dependencies[i]->readin
6653 && pst->dependencies[i]->user == NULL)
6654 {
6655 /* Inform about additional files that need to be read in. */
6656 if (info_verbose)
6657 {
6658 /* FIXME: i18n: Need to make this a single string. */
6659 fputs_filtered (" ", gdb_stdout);
6660 wrap_here ("");
6661 fputs_filtered ("and ", gdb_stdout);
6662 wrap_here ("");
6663 printf_filtered ("%s...", pst->dependencies[i]->filename);
6664 wrap_here (""); /* Flush output. */
6665 gdb_flush (gdb_stdout);
6666 }
6667 psymtab_to_symtab_1 (pst->dependencies[i]);
6668 }
6669
6670 per_cu = pst->read_symtab_private;
6671
6672 if (per_cu == NULL)
6673 {
6674 /* It's an include file, no symbols to read for it.
6675 Everything is in the parent symtab. */
6676 pst->readin = 1;
6677 return;
6678 }
6679
6680 dw2_do_instantiate_symtab (per_cu);
6681 }
6682
6683 /* Trivial hash function for die_info: the hash value of a DIE
6684 is its offset in .debug_info for this objfile. */
6685
6686 static hashval_t
6687 die_hash (const void *item)
6688 {
6689 const struct die_info *die = item;
6690
6691 return die->offset.sect_off;
6692 }
6693
6694 /* Trivial comparison function for die_info structures: two DIEs
6695 are equal if they have the same offset. */
6696
6697 static int
6698 die_eq (const void *item_lhs, const void *item_rhs)
6699 {
6700 const struct die_info *die_lhs = item_lhs;
6701 const struct die_info *die_rhs = item_rhs;
6702
6703 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
6704 }
6705
6706 /* die_reader_func for load_full_comp_unit.
6707 This is identical to read_signatured_type_reader,
6708 but is kept separate for now. */
6709
6710 static void
6711 load_full_comp_unit_reader (const struct die_reader_specs *reader,
6712 gdb_byte *info_ptr,
6713 struct die_info *comp_unit_die,
6714 int has_children,
6715 void *data)
6716 {
6717 struct dwarf2_cu *cu = reader->cu;
6718 enum language *language_ptr = data;
6719
6720 gdb_assert (cu->die_hash == NULL);
6721 cu->die_hash =
6722 htab_create_alloc_ex (cu->header.length / 12,
6723 die_hash,
6724 die_eq,
6725 NULL,
6726 &cu->comp_unit_obstack,
6727 hashtab_obstack_allocate,
6728 dummy_obstack_deallocate);
6729
6730 if (has_children)
6731 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
6732 &info_ptr, comp_unit_die);
6733 cu->dies = comp_unit_die;
6734 /* comp_unit_die is not stored in die_hash, no need. */
6735
6736 /* We try not to read any attributes in this function, because not
6737 all CUs needed for references have been loaded yet, and symbol
6738 table processing isn't initialized. But we have to set the CU language,
6739 or we won't be able to build types correctly.
6740 Similarly, if we do not read the producer, we can not apply
6741 producer-specific interpretation. */
6742 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
6743 }
6744
6745 /* Load the DIEs associated with PER_CU into memory. */
6746
6747 static void
6748 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
6749 enum language pretend_language)
6750 {
6751 gdb_assert (! this_cu->is_debug_types);
6752
6753 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6754 load_full_comp_unit_reader, &pretend_language);
6755 }
6756
6757 /* Add a DIE to the delayed physname list. */
6758
6759 static void
6760 add_to_method_list (struct type *type, int fnfield_index, int index,
6761 const char *name, struct die_info *die,
6762 struct dwarf2_cu *cu)
6763 {
6764 struct delayed_method_info mi;
6765 mi.type = type;
6766 mi.fnfield_index = fnfield_index;
6767 mi.index = index;
6768 mi.name = name;
6769 mi.die = die;
6770 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
6771 }
6772
6773 /* A cleanup for freeing the delayed method list. */
6774
6775 static void
6776 free_delayed_list (void *ptr)
6777 {
6778 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
6779 if (cu->method_list != NULL)
6780 {
6781 VEC_free (delayed_method_info, cu->method_list);
6782 cu->method_list = NULL;
6783 }
6784 }
6785
6786 /* Compute the physnames of any methods on the CU's method list.
6787
6788 The computation of method physnames is delayed in order to avoid the
6789 (bad) condition that one of the method's formal parameters is of an as yet
6790 incomplete type. */
6791
6792 static void
6793 compute_delayed_physnames (struct dwarf2_cu *cu)
6794 {
6795 int i;
6796 struct delayed_method_info *mi;
6797 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
6798 {
6799 const char *physname;
6800 struct fn_fieldlist *fn_flp
6801 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
6802 physname = dwarf2_physname (mi->name, mi->die, cu);
6803 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
6804 }
6805 }
6806
6807 /* Go objects should be embedded in a DW_TAG_module DIE,
6808 and it's not clear if/how imported objects will appear.
6809 To keep Go support simple until that's worked out,
6810 go back through what we've read and create something usable.
6811 We could do this while processing each DIE, and feels kinda cleaner,
6812 but that way is more invasive.
6813 This is to, for example, allow the user to type "p var" or "b main"
6814 without having to specify the package name, and allow lookups
6815 of module.object to work in contexts that use the expression
6816 parser. */
6817
6818 static void
6819 fixup_go_packaging (struct dwarf2_cu *cu)
6820 {
6821 char *package_name = NULL;
6822 struct pending *list;
6823 int i;
6824
6825 for (list = global_symbols; list != NULL; list = list->next)
6826 {
6827 for (i = 0; i < list->nsyms; ++i)
6828 {
6829 struct symbol *sym = list->symbol[i];
6830
6831 if (SYMBOL_LANGUAGE (sym) == language_go
6832 && SYMBOL_CLASS (sym) == LOC_BLOCK)
6833 {
6834 char *this_package_name = go_symbol_package_name (sym);
6835
6836 if (this_package_name == NULL)
6837 continue;
6838 if (package_name == NULL)
6839 package_name = this_package_name;
6840 else
6841 {
6842 if (strcmp (package_name, this_package_name) != 0)
6843 complaint (&symfile_complaints,
6844 _("Symtab %s has objects from two different Go packages: %s and %s"),
6845 (SYMBOL_SYMTAB (sym)
6846 ? symtab_to_filename_for_display (SYMBOL_SYMTAB (sym))
6847 : cu->objfile->name),
6848 this_package_name, package_name);
6849 xfree (this_package_name);
6850 }
6851 }
6852 }
6853 }
6854
6855 if (package_name != NULL)
6856 {
6857 struct objfile *objfile = cu->objfile;
6858 const char *saved_package_name = obstack_copy0 (&objfile->objfile_obstack,
6859 package_name,
6860 strlen (package_name));
6861 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
6862 saved_package_name, objfile);
6863 struct symbol *sym;
6864
6865 TYPE_TAG_NAME (type) = TYPE_NAME (type);
6866
6867 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
6868 SYMBOL_SET_LANGUAGE (sym, language_go);
6869 SYMBOL_SET_NAMES (sym, saved_package_name,
6870 strlen (saved_package_name), 0, objfile);
6871 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
6872 e.g., "main" finds the "main" module and not C's main(). */
6873 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
6874 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
6875 SYMBOL_TYPE (sym) = type;
6876
6877 add_symbol_to_list (sym, &global_symbols);
6878
6879 xfree (package_name);
6880 }
6881 }
6882
6883 /* Return the symtab for PER_CU. This works properly regardless of
6884 whether we're using the index or psymtabs. */
6885
6886 static struct symtab *
6887 get_symtab (struct dwarf2_per_cu_data *per_cu)
6888 {
6889 return (dwarf2_per_objfile->using_index
6890 ? per_cu->v.quick->symtab
6891 : per_cu->v.psymtab->symtab);
6892 }
6893
6894 /* A helper function for computing the list of all symbol tables
6895 included by PER_CU. */
6896
6897 static void
6898 recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
6899 htab_t all_children,
6900 struct dwarf2_per_cu_data *per_cu)
6901 {
6902 void **slot;
6903 int ix;
6904 struct dwarf2_per_cu_data *iter;
6905
6906 slot = htab_find_slot (all_children, per_cu, INSERT);
6907 if (*slot != NULL)
6908 {
6909 /* This inclusion and its children have been processed. */
6910 return;
6911 }
6912
6913 *slot = per_cu;
6914 /* Only add a CU if it has a symbol table. */
6915 if (get_symtab (per_cu) != NULL)
6916 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
6917
6918 for (ix = 0;
6919 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
6920 ++ix)
6921 recursively_compute_inclusions (result, all_children, iter);
6922 }
6923
6924 /* Compute the symtab 'includes' fields for the symtab related to
6925 PER_CU. */
6926
6927 static void
6928 compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
6929 {
6930 gdb_assert (! per_cu->is_debug_types);
6931
6932 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
6933 {
6934 int ix, len;
6935 struct dwarf2_per_cu_data *iter;
6936 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
6937 htab_t all_children;
6938 struct symtab *symtab = get_symtab (per_cu);
6939
6940 /* If we don't have a symtab, we can just skip this case. */
6941 if (symtab == NULL)
6942 return;
6943
6944 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
6945 NULL, xcalloc, xfree);
6946
6947 for (ix = 0;
6948 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
6949 ix, iter);
6950 ++ix)
6951 recursively_compute_inclusions (&result_children, all_children, iter);
6952
6953 /* Now we have a transitive closure of all the included CUs, and
6954 for .gdb_index version 7 the included TUs, so we can convert it
6955 to a list of symtabs. */
6956 len = VEC_length (dwarf2_per_cu_ptr, result_children);
6957 symtab->includes
6958 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
6959 (len + 1) * sizeof (struct symtab *));
6960 for (ix = 0;
6961 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
6962 ++ix)
6963 symtab->includes[ix] = get_symtab (iter);
6964 symtab->includes[len] = NULL;
6965
6966 VEC_free (dwarf2_per_cu_ptr, result_children);
6967 htab_delete (all_children);
6968 }
6969 }
6970
6971 /* Compute the 'includes' field for the symtabs of all the CUs we just
6972 read. */
6973
6974 static void
6975 process_cu_includes (void)
6976 {
6977 int ix;
6978 struct dwarf2_per_cu_data *iter;
6979
6980 for (ix = 0;
6981 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
6982 ix, iter);
6983 ++ix)
6984 {
6985 if (! iter->is_debug_types)
6986 compute_symtab_includes (iter);
6987 }
6988
6989 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
6990 }
6991
6992 /* Generate full symbol information for PER_CU, whose DIEs have
6993 already been loaded into memory. */
6994
6995 static void
6996 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
6997 enum language pretend_language)
6998 {
6999 struct dwarf2_cu *cu = per_cu->cu;
7000 struct objfile *objfile = per_cu->objfile;
7001 CORE_ADDR lowpc, highpc;
7002 struct symtab *symtab;
7003 struct cleanup *back_to, *delayed_list_cleanup;
7004 CORE_ADDR baseaddr;
7005 struct block *static_block;
7006
7007 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7008
7009 buildsym_init ();
7010 back_to = make_cleanup (really_free_pendings, NULL);
7011 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7012
7013 cu->list_in_scope = &file_symbols;
7014
7015 cu->language = pretend_language;
7016 cu->language_defn = language_def (cu->language);
7017
7018 /* Do line number decoding in read_file_scope () */
7019 process_die (cu->dies, cu);
7020
7021 /* For now fudge the Go package. */
7022 if (cu->language == language_go)
7023 fixup_go_packaging (cu);
7024
7025 /* Now that we have processed all the DIEs in the CU, all the types
7026 should be complete, and it should now be safe to compute all of the
7027 physnames. */
7028 compute_delayed_physnames (cu);
7029 do_cleanups (delayed_list_cleanup);
7030
7031 /* Some compilers don't define a DW_AT_high_pc attribute for the
7032 compilation unit. If the DW_AT_high_pc is missing, synthesize
7033 it, by scanning the DIE's below the compilation unit. */
7034 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
7035
7036 static_block
7037 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0,
7038 per_cu->imported_symtabs != NULL);
7039
7040 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
7041 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
7042 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
7043 addrmap to help ensure it has an accurate map of pc values belonging to
7044 this comp unit. */
7045 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
7046
7047 symtab = end_symtab_from_static_block (static_block, objfile,
7048 SECT_OFF_TEXT (objfile), 0);
7049
7050 if (symtab != NULL)
7051 {
7052 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
7053
7054 /* Set symtab language to language from DW_AT_language. If the
7055 compilation is from a C file generated by language preprocessors, do
7056 not set the language if it was already deduced by start_subfile. */
7057 if (!(cu->language == language_c && symtab->language != language_c))
7058 symtab->language = cu->language;
7059
7060 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
7061 produce DW_AT_location with location lists but it can be possibly
7062 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
7063 there were bugs in prologue debug info, fixed later in GCC-4.5
7064 by "unwind info for epilogues" patch (which is not directly related).
7065
7066 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
7067 needed, it would be wrong due to missing DW_AT_producer there.
7068
7069 Still one can confuse GDB by using non-standard GCC compilation
7070 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
7071 */
7072 if (cu->has_loclist && gcc_4_minor >= 5)
7073 symtab->locations_valid = 1;
7074
7075 if (gcc_4_minor >= 5)
7076 symtab->epilogue_unwind_valid = 1;
7077
7078 symtab->call_site_htab = cu->call_site_htab;
7079 }
7080
7081 if (dwarf2_per_objfile->using_index)
7082 per_cu->v.quick->symtab = symtab;
7083 else
7084 {
7085 struct partial_symtab *pst = per_cu->v.psymtab;
7086 pst->symtab = symtab;
7087 pst->readin = 1;
7088 }
7089
7090 /* Push it for inclusion processing later. */
7091 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
7092
7093 do_cleanups (back_to);
7094 }
7095
7096 /* Generate full symbol information for type unit PER_CU, whose DIEs have
7097 already been loaded into memory. */
7098
7099 static void
7100 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
7101 enum language pretend_language)
7102 {
7103 struct dwarf2_cu *cu = per_cu->cu;
7104 struct objfile *objfile = per_cu->objfile;
7105 struct symtab *symtab;
7106 struct cleanup *back_to, *delayed_list_cleanup;
7107
7108 buildsym_init ();
7109 back_to = make_cleanup (really_free_pendings, NULL);
7110 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7111
7112 cu->list_in_scope = &file_symbols;
7113
7114 cu->language = pretend_language;
7115 cu->language_defn = language_def (cu->language);
7116
7117 /* The symbol tables are set up in read_type_unit_scope. */
7118 process_die (cu->dies, cu);
7119
7120 /* For now fudge the Go package. */
7121 if (cu->language == language_go)
7122 fixup_go_packaging (cu);
7123
7124 /* Now that we have processed all the DIEs in the CU, all the types
7125 should be complete, and it should now be safe to compute all of the
7126 physnames. */
7127 compute_delayed_physnames (cu);
7128 do_cleanups (delayed_list_cleanup);
7129
7130 /* TUs share symbol tables.
7131 If this is the first TU to use this symtab, complete the construction
7132 of it with end_expandable_symtab. Otherwise, complete the addition of
7133 this TU's symbols to the existing symtab. */
7134 if (per_cu->type_unit_group->primary_symtab == NULL)
7135 {
7136 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
7137 per_cu->type_unit_group->primary_symtab = symtab;
7138
7139 if (symtab != NULL)
7140 {
7141 /* Set symtab language to language from DW_AT_language. If the
7142 compilation is from a C file generated by language preprocessors,
7143 do not set the language if it was already deduced by
7144 start_subfile. */
7145 if (!(cu->language == language_c && symtab->language != language_c))
7146 symtab->language = cu->language;
7147 }
7148 }
7149 else
7150 {
7151 augment_type_symtab (objfile,
7152 per_cu->type_unit_group->primary_symtab);
7153 symtab = per_cu->type_unit_group->primary_symtab;
7154 }
7155
7156 if (dwarf2_per_objfile->using_index)
7157 per_cu->v.quick->symtab = symtab;
7158 else
7159 {
7160 struct partial_symtab *pst = per_cu->v.psymtab;
7161 pst->symtab = symtab;
7162 pst->readin = 1;
7163 }
7164
7165 do_cleanups (back_to);
7166 }
7167
7168 /* Process an imported unit DIE. */
7169
7170 static void
7171 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7172 {
7173 struct attribute *attr;
7174
7175 /* For now we don't handle imported units in type units. */
7176 if (cu->per_cu->is_debug_types)
7177 {
7178 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7179 " supported in type units [in module %s]"),
7180 cu->objfile->name);
7181 }
7182
7183 attr = dwarf2_attr (die, DW_AT_import, cu);
7184 if (attr != NULL)
7185 {
7186 struct dwarf2_per_cu_data *per_cu;
7187 struct symtab *imported_symtab;
7188 sect_offset offset;
7189 int is_dwz;
7190
7191 offset = dwarf2_get_ref_die_offset (attr);
7192 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7193 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
7194
7195 /* Queue the unit, if needed. */
7196 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7197 load_full_comp_unit (per_cu, cu->language);
7198
7199 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
7200 per_cu);
7201 }
7202 }
7203
7204 /* Process a die and its children. */
7205
7206 static void
7207 process_die (struct die_info *die, struct dwarf2_cu *cu)
7208 {
7209 switch (die->tag)
7210 {
7211 case DW_TAG_padding:
7212 break;
7213 case DW_TAG_compile_unit:
7214 case DW_TAG_partial_unit:
7215 read_file_scope (die, cu);
7216 break;
7217 case DW_TAG_type_unit:
7218 read_type_unit_scope (die, cu);
7219 break;
7220 case DW_TAG_subprogram:
7221 case DW_TAG_inlined_subroutine:
7222 read_func_scope (die, cu);
7223 break;
7224 case DW_TAG_lexical_block:
7225 case DW_TAG_try_block:
7226 case DW_TAG_catch_block:
7227 read_lexical_block_scope (die, cu);
7228 break;
7229 case DW_TAG_GNU_call_site:
7230 read_call_site_scope (die, cu);
7231 break;
7232 case DW_TAG_class_type:
7233 case DW_TAG_interface_type:
7234 case DW_TAG_structure_type:
7235 case DW_TAG_union_type:
7236 process_structure_scope (die, cu);
7237 break;
7238 case DW_TAG_enumeration_type:
7239 process_enumeration_scope (die, cu);
7240 break;
7241
7242 /* These dies have a type, but processing them does not create
7243 a symbol or recurse to process the children. Therefore we can
7244 read them on-demand through read_type_die. */
7245 case DW_TAG_subroutine_type:
7246 case DW_TAG_set_type:
7247 case DW_TAG_array_type:
7248 case DW_TAG_pointer_type:
7249 case DW_TAG_ptr_to_member_type:
7250 case DW_TAG_reference_type:
7251 case DW_TAG_string_type:
7252 break;
7253
7254 case DW_TAG_base_type:
7255 case DW_TAG_subrange_type:
7256 case DW_TAG_typedef:
7257 /* Add a typedef symbol for the type definition, if it has a
7258 DW_AT_name. */
7259 new_symbol (die, read_type_die (die, cu), cu);
7260 break;
7261 case DW_TAG_common_block:
7262 read_common_block (die, cu);
7263 break;
7264 case DW_TAG_common_inclusion:
7265 break;
7266 case DW_TAG_namespace:
7267 cu->processing_has_namespace_info = 1;
7268 read_namespace (die, cu);
7269 break;
7270 case DW_TAG_module:
7271 cu->processing_has_namespace_info = 1;
7272 read_module (die, cu);
7273 break;
7274 case DW_TAG_imported_declaration:
7275 case DW_TAG_imported_module:
7276 cu->processing_has_namespace_info = 1;
7277 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7278 || cu->language != language_fortran))
7279 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7280 dwarf_tag_name (die->tag));
7281 read_import_statement (die, cu);
7282 break;
7283
7284 case DW_TAG_imported_unit:
7285 process_imported_unit_die (die, cu);
7286 break;
7287
7288 default:
7289 new_symbol (die, NULL, cu);
7290 break;
7291 }
7292 }
7293 \f
7294 /* DWARF name computation. */
7295
7296 /* A helper function for dwarf2_compute_name which determines whether DIE
7297 needs to have the name of the scope prepended to the name listed in the
7298 die. */
7299
7300 static int
7301 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7302 {
7303 struct attribute *attr;
7304
7305 switch (die->tag)
7306 {
7307 case DW_TAG_namespace:
7308 case DW_TAG_typedef:
7309 case DW_TAG_class_type:
7310 case DW_TAG_interface_type:
7311 case DW_TAG_structure_type:
7312 case DW_TAG_union_type:
7313 case DW_TAG_enumeration_type:
7314 case DW_TAG_enumerator:
7315 case DW_TAG_subprogram:
7316 case DW_TAG_member:
7317 return 1;
7318
7319 case DW_TAG_variable:
7320 case DW_TAG_constant:
7321 /* We only need to prefix "globally" visible variables. These include
7322 any variable marked with DW_AT_external or any variable that
7323 lives in a namespace. [Variables in anonymous namespaces
7324 require prefixing, but they are not DW_AT_external.] */
7325
7326 if (dwarf2_attr (die, DW_AT_specification, cu))
7327 {
7328 struct dwarf2_cu *spec_cu = cu;
7329
7330 return die_needs_namespace (die_specification (die, &spec_cu),
7331 spec_cu);
7332 }
7333
7334 attr = dwarf2_attr (die, DW_AT_external, cu);
7335 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7336 && die->parent->tag != DW_TAG_module)
7337 return 0;
7338 /* A variable in a lexical block of some kind does not need a
7339 namespace, even though in C++ such variables may be external
7340 and have a mangled name. */
7341 if (die->parent->tag == DW_TAG_lexical_block
7342 || die->parent->tag == DW_TAG_try_block
7343 || die->parent->tag == DW_TAG_catch_block
7344 || die->parent->tag == DW_TAG_subprogram)
7345 return 0;
7346 return 1;
7347
7348 default:
7349 return 0;
7350 }
7351 }
7352
7353 /* Retrieve the last character from a mem_file. */
7354
7355 static void
7356 do_ui_file_peek_last (void *object, const char *buffer, long length)
7357 {
7358 char *last_char_p = (char *) object;
7359
7360 if (length > 0)
7361 *last_char_p = buffer[length - 1];
7362 }
7363
7364 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
7365 compute the physname for the object, which include a method's:
7366 - formal parameters (C++/Java),
7367 - receiver type (Go),
7368 - return type (Java).
7369
7370 The term "physname" is a bit confusing.
7371 For C++, for example, it is the demangled name.
7372 For Go, for example, it's the mangled name.
7373
7374 For Ada, return the DIE's linkage name rather than the fully qualified
7375 name. PHYSNAME is ignored..
7376
7377 The result is allocated on the objfile_obstack and canonicalized. */
7378
7379 static const char *
7380 dwarf2_compute_name (const char *name,
7381 struct die_info *die, struct dwarf2_cu *cu,
7382 int physname)
7383 {
7384 struct objfile *objfile = cu->objfile;
7385
7386 if (name == NULL)
7387 name = dwarf2_name (die, cu);
7388
7389 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7390 compute it by typename_concat inside GDB. */
7391 if (cu->language == language_ada
7392 || (cu->language == language_fortran && physname))
7393 {
7394 /* For Ada unit, we prefer the linkage name over the name, as
7395 the former contains the exported name, which the user expects
7396 to be able to reference. Ideally, we want the user to be able
7397 to reference this entity using either natural or linkage name,
7398 but we haven't started looking at this enhancement yet. */
7399 struct attribute *attr;
7400
7401 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7402 if (attr == NULL)
7403 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7404 if (attr && DW_STRING (attr))
7405 return DW_STRING (attr);
7406 }
7407
7408 /* These are the only languages we know how to qualify names in. */
7409 if (name != NULL
7410 && (cu->language == language_cplus || cu->language == language_java
7411 || cu->language == language_fortran))
7412 {
7413 if (die_needs_namespace (die, cu))
7414 {
7415 long length;
7416 const char *prefix;
7417 struct ui_file *buf;
7418
7419 prefix = determine_prefix (die, cu);
7420 buf = mem_fileopen ();
7421 if (*prefix != '\0')
7422 {
7423 char *prefixed_name = typename_concat (NULL, prefix, name,
7424 physname, cu);
7425
7426 fputs_unfiltered (prefixed_name, buf);
7427 xfree (prefixed_name);
7428 }
7429 else
7430 fputs_unfiltered (name, buf);
7431
7432 /* Template parameters may be specified in the DIE's DW_AT_name, or
7433 as children with DW_TAG_template_type_param or
7434 DW_TAG_value_type_param. If the latter, add them to the name
7435 here. If the name already has template parameters, then
7436 skip this step; some versions of GCC emit both, and
7437 it is more efficient to use the pre-computed name.
7438
7439 Something to keep in mind about this process: it is very
7440 unlikely, or in some cases downright impossible, to produce
7441 something that will match the mangled name of a function.
7442 If the definition of the function has the same debug info,
7443 we should be able to match up with it anyway. But fallbacks
7444 using the minimal symbol, for instance to find a method
7445 implemented in a stripped copy of libstdc++, will not work.
7446 If we do not have debug info for the definition, we will have to
7447 match them up some other way.
7448
7449 When we do name matching there is a related problem with function
7450 templates; two instantiated function templates are allowed to
7451 differ only by their return types, which we do not add here. */
7452
7453 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7454 {
7455 struct attribute *attr;
7456 struct die_info *child;
7457 int first = 1;
7458
7459 die->building_fullname = 1;
7460
7461 for (child = die->child; child != NULL; child = child->sibling)
7462 {
7463 struct type *type;
7464 LONGEST value;
7465 gdb_byte *bytes;
7466 struct dwarf2_locexpr_baton *baton;
7467 struct value *v;
7468
7469 if (child->tag != DW_TAG_template_type_param
7470 && child->tag != DW_TAG_template_value_param)
7471 continue;
7472
7473 if (first)
7474 {
7475 fputs_unfiltered ("<", buf);
7476 first = 0;
7477 }
7478 else
7479 fputs_unfiltered (", ", buf);
7480
7481 attr = dwarf2_attr (child, DW_AT_type, cu);
7482 if (attr == NULL)
7483 {
7484 complaint (&symfile_complaints,
7485 _("template parameter missing DW_AT_type"));
7486 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7487 continue;
7488 }
7489 type = die_type (child, cu);
7490
7491 if (child->tag == DW_TAG_template_type_param)
7492 {
7493 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
7494 continue;
7495 }
7496
7497 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7498 if (attr == NULL)
7499 {
7500 complaint (&symfile_complaints,
7501 _("template parameter missing "
7502 "DW_AT_const_value"));
7503 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7504 continue;
7505 }
7506
7507 dwarf2_const_value_attr (attr, type, name,
7508 &cu->comp_unit_obstack, cu,
7509 &value, &bytes, &baton);
7510
7511 if (TYPE_NOSIGN (type))
7512 /* GDB prints characters as NUMBER 'CHAR'. If that's
7513 changed, this can use value_print instead. */
7514 c_printchar (value, type, buf);
7515 else
7516 {
7517 struct value_print_options opts;
7518
7519 if (baton != NULL)
7520 v = dwarf2_evaluate_loc_desc (type, NULL,
7521 baton->data,
7522 baton->size,
7523 baton->per_cu);
7524 else if (bytes != NULL)
7525 {
7526 v = allocate_value (type);
7527 memcpy (value_contents_writeable (v), bytes,
7528 TYPE_LENGTH (type));
7529 }
7530 else
7531 v = value_from_longest (type, value);
7532
7533 /* Specify decimal so that we do not depend on
7534 the radix. */
7535 get_formatted_print_options (&opts, 'd');
7536 opts.raw = 1;
7537 value_print (v, buf, &opts);
7538 release_value (v);
7539 value_free (v);
7540 }
7541 }
7542
7543 die->building_fullname = 0;
7544
7545 if (!first)
7546 {
7547 /* Close the argument list, with a space if necessary
7548 (nested templates). */
7549 char last_char = '\0';
7550 ui_file_put (buf, do_ui_file_peek_last, &last_char);
7551 if (last_char == '>')
7552 fputs_unfiltered (" >", buf);
7553 else
7554 fputs_unfiltered (">", buf);
7555 }
7556 }
7557
7558 /* For Java and C++ methods, append formal parameter type
7559 information, if PHYSNAME. */
7560
7561 if (physname && die->tag == DW_TAG_subprogram
7562 && (cu->language == language_cplus
7563 || cu->language == language_java))
7564 {
7565 struct type *type = read_type_die (die, cu);
7566
7567 c_type_print_args (type, buf, 1, cu->language,
7568 &type_print_raw_options);
7569
7570 if (cu->language == language_java)
7571 {
7572 /* For java, we must append the return type to method
7573 names. */
7574 if (die->tag == DW_TAG_subprogram)
7575 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
7576 0, 0, &type_print_raw_options);
7577 }
7578 else if (cu->language == language_cplus)
7579 {
7580 /* Assume that an artificial first parameter is
7581 "this", but do not crash if it is not. RealView
7582 marks unnamed (and thus unused) parameters as
7583 artificial; there is no way to differentiate
7584 the two cases. */
7585 if (TYPE_NFIELDS (type) > 0
7586 && TYPE_FIELD_ARTIFICIAL (type, 0)
7587 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
7588 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
7589 0))))
7590 fputs_unfiltered (" const", buf);
7591 }
7592 }
7593
7594 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
7595 &length);
7596 ui_file_delete (buf);
7597
7598 if (cu->language == language_cplus)
7599 {
7600 const char *cname
7601 = dwarf2_canonicalize_name (name, cu,
7602 &objfile->objfile_obstack);
7603
7604 if (cname != NULL)
7605 name = cname;
7606 }
7607 }
7608 }
7609
7610 return name;
7611 }
7612
7613 /* Return the fully qualified name of DIE, based on its DW_AT_name.
7614 If scope qualifiers are appropriate they will be added. The result
7615 will be allocated on the objfile_obstack, or NULL if the DIE does
7616 not have a name. NAME may either be from a previous call to
7617 dwarf2_name or NULL.
7618
7619 The output string will be canonicalized (if C++/Java). */
7620
7621 static const char *
7622 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
7623 {
7624 return dwarf2_compute_name (name, die, cu, 0);
7625 }
7626
7627 /* Construct a physname for the given DIE in CU. NAME may either be
7628 from a previous call to dwarf2_name or NULL. The result will be
7629 allocated on the objfile_objstack or NULL if the DIE does not have a
7630 name.
7631
7632 The output string will be canonicalized (if C++/Java). */
7633
7634 static const char *
7635 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
7636 {
7637 struct objfile *objfile = cu->objfile;
7638 struct attribute *attr;
7639 const char *retval, *mangled = NULL, *canon = NULL;
7640 struct cleanup *back_to;
7641 int need_copy = 1;
7642
7643 /* In this case dwarf2_compute_name is just a shortcut not building anything
7644 on its own. */
7645 if (!die_needs_namespace (die, cu))
7646 return dwarf2_compute_name (name, die, cu, 1);
7647
7648 back_to = make_cleanup (null_cleanup, NULL);
7649
7650 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7651 if (!attr)
7652 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7653
7654 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
7655 has computed. */
7656 if (attr && DW_STRING (attr))
7657 {
7658 char *demangled;
7659
7660 mangled = DW_STRING (attr);
7661
7662 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
7663 type. It is easier for GDB users to search for such functions as
7664 `name(params)' than `long name(params)'. In such case the minimal
7665 symbol names do not match the full symbol names but for template
7666 functions there is never a need to look up their definition from their
7667 declaration so the only disadvantage remains the minimal symbol
7668 variant `long name(params)' does not have the proper inferior type.
7669 */
7670
7671 if (cu->language == language_go)
7672 {
7673 /* This is a lie, but we already lie to the caller new_symbol_full.
7674 new_symbol_full assumes we return the mangled name.
7675 This just undoes that lie until things are cleaned up. */
7676 demangled = NULL;
7677 }
7678 else
7679 {
7680 demangled = cplus_demangle (mangled,
7681 (DMGL_PARAMS | DMGL_ANSI
7682 | (cu->language == language_java
7683 ? DMGL_JAVA | DMGL_RET_POSTFIX
7684 : DMGL_RET_DROP)));
7685 }
7686 if (demangled)
7687 {
7688 make_cleanup (xfree, demangled);
7689 canon = demangled;
7690 }
7691 else
7692 {
7693 canon = mangled;
7694 need_copy = 0;
7695 }
7696 }
7697
7698 if (canon == NULL || check_physname)
7699 {
7700 const char *physname = dwarf2_compute_name (name, die, cu, 1);
7701
7702 if (canon != NULL && strcmp (physname, canon) != 0)
7703 {
7704 /* It may not mean a bug in GDB. The compiler could also
7705 compute DW_AT_linkage_name incorrectly. But in such case
7706 GDB would need to be bug-to-bug compatible. */
7707
7708 complaint (&symfile_complaints,
7709 _("Computed physname <%s> does not match demangled <%s> "
7710 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
7711 physname, canon, mangled, die->offset.sect_off, objfile->name);
7712
7713 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
7714 is available here - over computed PHYSNAME. It is safer
7715 against both buggy GDB and buggy compilers. */
7716
7717 retval = canon;
7718 }
7719 else
7720 {
7721 retval = physname;
7722 need_copy = 0;
7723 }
7724 }
7725 else
7726 retval = canon;
7727
7728 if (need_copy)
7729 retval = obstack_copy0 (&objfile->objfile_obstack, retval, strlen (retval));
7730
7731 do_cleanups (back_to);
7732 return retval;
7733 }
7734
7735 /* Read the import statement specified by the given die and record it. */
7736
7737 static void
7738 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
7739 {
7740 struct objfile *objfile = cu->objfile;
7741 struct attribute *import_attr;
7742 struct die_info *imported_die, *child_die;
7743 struct dwarf2_cu *imported_cu;
7744 const char *imported_name;
7745 const char *imported_name_prefix;
7746 const char *canonical_name;
7747 const char *import_alias;
7748 const char *imported_declaration = NULL;
7749 const char *import_prefix;
7750 VEC (const_char_ptr) *excludes = NULL;
7751 struct cleanup *cleanups;
7752
7753 import_attr = dwarf2_attr (die, DW_AT_import, cu);
7754 if (import_attr == NULL)
7755 {
7756 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7757 dwarf_tag_name (die->tag));
7758 return;
7759 }
7760
7761 imported_cu = cu;
7762 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
7763 imported_name = dwarf2_name (imported_die, imported_cu);
7764 if (imported_name == NULL)
7765 {
7766 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
7767
7768 The import in the following code:
7769 namespace A
7770 {
7771 typedef int B;
7772 }
7773
7774 int main ()
7775 {
7776 using A::B;
7777 B b;
7778 return b;
7779 }
7780
7781 ...
7782 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
7783 <52> DW_AT_decl_file : 1
7784 <53> DW_AT_decl_line : 6
7785 <54> DW_AT_import : <0x75>
7786 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
7787 <59> DW_AT_name : B
7788 <5b> DW_AT_decl_file : 1
7789 <5c> DW_AT_decl_line : 2
7790 <5d> DW_AT_type : <0x6e>
7791 ...
7792 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
7793 <76> DW_AT_byte_size : 4
7794 <77> DW_AT_encoding : 5 (signed)
7795
7796 imports the wrong die ( 0x75 instead of 0x58 ).
7797 This case will be ignored until the gcc bug is fixed. */
7798 return;
7799 }
7800
7801 /* Figure out the local name after import. */
7802 import_alias = dwarf2_name (die, cu);
7803
7804 /* Figure out where the statement is being imported to. */
7805 import_prefix = determine_prefix (die, cu);
7806
7807 /* Figure out what the scope of the imported die is and prepend it
7808 to the name of the imported die. */
7809 imported_name_prefix = determine_prefix (imported_die, imported_cu);
7810
7811 if (imported_die->tag != DW_TAG_namespace
7812 && imported_die->tag != DW_TAG_module)
7813 {
7814 imported_declaration = imported_name;
7815 canonical_name = imported_name_prefix;
7816 }
7817 else if (strlen (imported_name_prefix) > 0)
7818 canonical_name = obconcat (&objfile->objfile_obstack,
7819 imported_name_prefix, "::", imported_name,
7820 (char *) NULL);
7821 else
7822 canonical_name = imported_name;
7823
7824 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
7825
7826 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
7827 for (child_die = die->child; child_die && child_die->tag;
7828 child_die = sibling_die (child_die))
7829 {
7830 /* DWARF-4: A Fortran use statement with a “rename list” may be
7831 represented by an imported module entry with an import attribute
7832 referring to the module and owned entries corresponding to those
7833 entities that are renamed as part of being imported. */
7834
7835 if (child_die->tag != DW_TAG_imported_declaration)
7836 {
7837 complaint (&symfile_complaints,
7838 _("child DW_TAG_imported_declaration expected "
7839 "- DIE at 0x%x [in module %s]"),
7840 child_die->offset.sect_off, objfile->name);
7841 continue;
7842 }
7843
7844 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
7845 if (import_attr == NULL)
7846 {
7847 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7848 dwarf_tag_name (child_die->tag));
7849 continue;
7850 }
7851
7852 imported_cu = cu;
7853 imported_die = follow_die_ref_or_sig (child_die, import_attr,
7854 &imported_cu);
7855 imported_name = dwarf2_name (imported_die, imported_cu);
7856 if (imported_name == NULL)
7857 {
7858 complaint (&symfile_complaints,
7859 _("child DW_TAG_imported_declaration has unknown "
7860 "imported name - DIE at 0x%x [in module %s]"),
7861 child_die->offset.sect_off, objfile->name);
7862 continue;
7863 }
7864
7865 VEC_safe_push (const_char_ptr, excludes, imported_name);
7866
7867 process_die (child_die, cu);
7868 }
7869
7870 cp_add_using_directive (import_prefix,
7871 canonical_name,
7872 import_alias,
7873 imported_declaration,
7874 excludes,
7875 0,
7876 &objfile->objfile_obstack);
7877
7878 do_cleanups (cleanups);
7879 }
7880
7881 /* Cleanup function for handle_DW_AT_stmt_list. */
7882
7883 static void
7884 free_cu_line_header (void *arg)
7885 {
7886 struct dwarf2_cu *cu = arg;
7887
7888 free_line_header (cu->line_header);
7889 cu->line_header = NULL;
7890 }
7891
7892 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
7893 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
7894 this, it was first present in GCC release 4.3.0. */
7895
7896 static int
7897 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
7898 {
7899 if (!cu->checked_producer)
7900 check_producer (cu);
7901
7902 return cu->producer_is_gcc_lt_4_3;
7903 }
7904
7905 static void
7906 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
7907 const char **name, const char **comp_dir)
7908 {
7909 struct attribute *attr;
7910
7911 *name = NULL;
7912 *comp_dir = NULL;
7913
7914 /* Find the filename. Do not use dwarf2_name here, since the filename
7915 is not a source language identifier. */
7916 attr = dwarf2_attr (die, DW_AT_name, cu);
7917 if (attr)
7918 {
7919 *name = DW_STRING (attr);
7920 }
7921
7922 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
7923 if (attr)
7924 *comp_dir = DW_STRING (attr);
7925 else if (producer_is_gcc_lt_4_3 (cu) && *name != NULL
7926 && IS_ABSOLUTE_PATH (*name))
7927 {
7928 char *d = ldirname (*name);
7929
7930 *comp_dir = d;
7931 if (d != NULL)
7932 make_cleanup (xfree, d);
7933 }
7934 if (*comp_dir != NULL)
7935 {
7936 /* Irix 6.2 native cc prepends <machine>.: to the compilation
7937 directory, get rid of it. */
7938 char *cp = strchr (*comp_dir, ':');
7939
7940 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
7941 *comp_dir = cp + 1;
7942 }
7943
7944 if (*name == NULL)
7945 *name = "<unknown>";
7946 }
7947
7948 /* Handle DW_AT_stmt_list for a compilation unit.
7949 DIE is the DW_TAG_compile_unit die for CU.
7950 COMP_DIR is the compilation directory.
7951 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
7952
7953 static void
7954 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
7955 const char *comp_dir)
7956 {
7957 struct attribute *attr;
7958
7959 gdb_assert (! cu->per_cu->is_debug_types);
7960
7961 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7962 if (attr)
7963 {
7964 unsigned int line_offset = DW_UNSND (attr);
7965 struct line_header *line_header
7966 = dwarf_decode_line_header (line_offset, cu);
7967
7968 if (line_header)
7969 {
7970 cu->line_header = line_header;
7971 make_cleanup (free_cu_line_header, cu);
7972 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
7973 }
7974 }
7975 }
7976
7977 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
7978
7979 static void
7980 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
7981 {
7982 struct objfile *objfile = dwarf2_per_objfile->objfile;
7983 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
7984 CORE_ADDR lowpc = ((CORE_ADDR) -1);
7985 CORE_ADDR highpc = ((CORE_ADDR) 0);
7986 struct attribute *attr;
7987 const char *name = NULL;
7988 const char *comp_dir = NULL;
7989 struct die_info *child_die;
7990 bfd *abfd = objfile->obfd;
7991 CORE_ADDR baseaddr;
7992
7993 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7994
7995 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
7996
7997 /* If we didn't find a lowpc, set it to highpc to avoid complaints
7998 from finish_block. */
7999 if (lowpc == ((CORE_ADDR) -1))
8000 lowpc = highpc;
8001 lowpc += baseaddr;
8002 highpc += baseaddr;
8003
8004 find_file_and_directory (die, cu, &name, &comp_dir);
8005
8006 prepare_one_comp_unit (cu, die, cu->language);
8007
8008 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
8009 standardised yet. As a workaround for the language detection we fall
8010 back to the DW_AT_producer string. */
8011 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
8012 cu->language = language_opencl;
8013
8014 /* Similar hack for Go. */
8015 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
8016 set_cu_language (DW_LANG_Go, cu);
8017
8018 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
8019
8020 /* Decode line number information if present. We do this before
8021 processing child DIEs, so that the line header table is available
8022 for DW_AT_decl_file. */
8023 handle_DW_AT_stmt_list (die, cu, comp_dir);
8024
8025 /* Process all dies in compilation unit. */
8026 if (die->child != NULL)
8027 {
8028 child_die = die->child;
8029 while (child_die && child_die->tag)
8030 {
8031 process_die (child_die, cu);
8032 child_die = sibling_die (child_die);
8033 }
8034 }
8035
8036 /* Decode macro information, if present. Dwarf 2 macro information
8037 refers to information in the line number info statement program
8038 header, so we can only read it if we've read the header
8039 successfully. */
8040 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
8041 if (attr && cu->line_header)
8042 {
8043 if (dwarf2_attr (die, DW_AT_macro_info, cu))
8044 complaint (&symfile_complaints,
8045 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
8046
8047 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
8048 }
8049 else
8050 {
8051 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
8052 if (attr && cu->line_header)
8053 {
8054 unsigned int macro_offset = DW_UNSND (attr);
8055
8056 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
8057 }
8058 }
8059
8060 do_cleanups (back_to);
8061 }
8062
8063 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
8064 Create the set of symtabs used by this TU, or if this TU is sharing
8065 symtabs with another TU and the symtabs have already been created
8066 then restore those symtabs in the line header.
8067 We don't need the pc/line-number mapping for type units. */
8068
8069 static void
8070 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
8071 {
8072 struct objfile *objfile = dwarf2_per_objfile->objfile;
8073 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8074 struct type_unit_group *tu_group;
8075 int first_time;
8076 struct line_header *lh;
8077 struct attribute *attr;
8078 unsigned int i, line_offset;
8079
8080 gdb_assert (per_cu->is_debug_types);
8081
8082 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
8083
8084 /* If we're using .gdb_index (includes -readnow) then
8085 per_cu->s.type_unit_group may not have been set up yet. */
8086 if (per_cu->type_unit_group == NULL)
8087 per_cu->type_unit_group = get_type_unit_group (cu, attr);
8088 tu_group = per_cu->type_unit_group;
8089
8090 /* If we've already processed this stmt_list there's no real need to
8091 do it again, we could fake it and just recreate the part we need
8092 (file name,index -> symtab mapping). If data shows this optimization
8093 is useful we can do it then. */
8094 first_time = tu_group->primary_symtab == NULL;
8095
8096 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
8097 debug info. */
8098 lh = NULL;
8099 if (attr != NULL)
8100 {
8101 line_offset = DW_UNSND (attr);
8102 lh = dwarf_decode_line_header (line_offset, cu);
8103 }
8104 if (lh == NULL)
8105 {
8106 if (first_time)
8107 dwarf2_start_symtab (cu, "", NULL, 0);
8108 else
8109 {
8110 gdb_assert (tu_group->symtabs == NULL);
8111 restart_symtab (0);
8112 }
8113 /* Note: The primary symtab will get allocated at the end. */
8114 return;
8115 }
8116
8117 cu->line_header = lh;
8118 make_cleanup (free_cu_line_header, cu);
8119
8120 if (first_time)
8121 {
8122 dwarf2_start_symtab (cu, "", NULL, 0);
8123
8124 tu_group->num_symtabs = lh->num_file_names;
8125 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
8126
8127 for (i = 0; i < lh->num_file_names; ++i)
8128 {
8129 char *dir = NULL;
8130 struct file_entry *fe = &lh->file_names[i];
8131
8132 if (fe->dir_index)
8133 dir = lh->include_dirs[fe->dir_index - 1];
8134 dwarf2_start_subfile (fe->name, dir, NULL);
8135
8136 /* Note: We don't have to watch for the main subfile here, type units
8137 don't have DW_AT_name. */
8138
8139 if (current_subfile->symtab == NULL)
8140 {
8141 /* NOTE: start_subfile will recognize when it's been passed
8142 a file it has already seen. So we can't assume there's a
8143 simple mapping from lh->file_names to subfiles,
8144 lh->file_names may contain dups. */
8145 current_subfile->symtab = allocate_symtab (current_subfile->name,
8146 objfile);
8147 }
8148
8149 fe->symtab = current_subfile->symtab;
8150 tu_group->symtabs[i] = fe->symtab;
8151 }
8152 }
8153 else
8154 {
8155 restart_symtab (0);
8156
8157 for (i = 0; i < lh->num_file_names; ++i)
8158 {
8159 struct file_entry *fe = &lh->file_names[i];
8160
8161 fe->symtab = tu_group->symtabs[i];
8162 }
8163 }
8164
8165 /* The main symtab is allocated last. Type units don't have DW_AT_name
8166 so they don't have a "real" (so to speak) symtab anyway.
8167 There is later code that will assign the main symtab to all symbols
8168 that don't have one. We need to handle the case of a symbol with a
8169 missing symtab (DW_AT_decl_file) anyway. */
8170 }
8171
8172 /* Process DW_TAG_type_unit.
8173 For TUs we want to skip the first top level sibling if it's not the
8174 actual type being defined by this TU. In this case the first top
8175 level sibling is there to provide context only. */
8176
8177 static void
8178 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8179 {
8180 struct die_info *child_die;
8181
8182 prepare_one_comp_unit (cu, die, language_minimal);
8183
8184 /* Initialize (or reinitialize) the machinery for building symtabs.
8185 We do this before processing child DIEs, so that the line header table
8186 is available for DW_AT_decl_file. */
8187 setup_type_unit_groups (die, cu);
8188
8189 if (die->child != NULL)
8190 {
8191 child_die = die->child;
8192 while (child_die && child_die->tag)
8193 {
8194 process_die (child_die, cu);
8195 child_die = sibling_die (child_die);
8196 }
8197 }
8198 }
8199 \f
8200 /* DWO/DWP files.
8201
8202 http://gcc.gnu.org/wiki/DebugFission
8203 http://gcc.gnu.org/wiki/DebugFissionDWP
8204
8205 To simplify handling of both DWO files ("object" files with the DWARF info)
8206 and DWP files (a file with the DWOs packaged up into one file), we treat
8207 DWP files as having a collection of virtual DWO files. */
8208
8209 static hashval_t
8210 hash_dwo_file (const void *item)
8211 {
8212 const struct dwo_file *dwo_file = item;
8213
8214 return htab_hash_string (dwo_file->name);
8215 }
8216
8217 static int
8218 eq_dwo_file (const void *item_lhs, const void *item_rhs)
8219 {
8220 const struct dwo_file *lhs = item_lhs;
8221 const struct dwo_file *rhs = item_rhs;
8222
8223 return strcmp (lhs->name, rhs->name) == 0;
8224 }
8225
8226 /* Allocate a hash table for DWO files. */
8227
8228 static htab_t
8229 allocate_dwo_file_hash_table (void)
8230 {
8231 struct objfile *objfile = dwarf2_per_objfile->objfile;
8232
8233 return htab_create_alloc_ex (41,
8234 hash_dwo_file,
8235 eq_dwo_file,
8236 NULL,
8237 &objfile->objfile_obstack,
8238 hashtab_obstack_allocate,
8239 dummy_obstack_deallocate);
8240 }
8241
8242 /* Lookup DWO file DWO_NAME. */
8243
8244 static void **
8245 lookup_dwo_file_slot (const char *dwo_name)
8246 {
8247 struct dwo_file find_entry;
8248 void **slot;
8249
8250 if (dwarf2_per_objfile->dwo_files == NULL)
8251 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8252
8253 memset (&find_entry, 0, sizeof (find_entry));
8254 find_entry.name = dwo_name;
8255 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8256
8257 return slot;
8258 }
8259
8260 static hashval_t
8261 hash_dwo_unit (const void *item)
8262 {
8263 const struct dwo_unit *dwo_unit = item;
8264
8265 /* This drops the top 32 bits of the id, but is ok for a hash. */
8266 return dwo_unit->signature;
8267 }
8268
8269 static int
8270 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8271 {
8272 const struct dwo_unit *lhs = item_lhs;
8273 const struct dwo_unit *rhs = item_rhs;
8274
8275 /* The signature is assumed to be unique within the DWO file.
8276 So while object file CU dwo_id's always have the value zero,
8277 that's OK, assuming each object file DWO file has only one CU,
8278 and that's the rule for now. */
8279 return lhs->signature == rhs->signature;
8280 }
8281
8282 /* Allocate a hash table for DWO CUs,TUs.
8283 There is one of these tables for each of CUs,TUs for each DWO file. */
8284
8285 static htab_t
8286 allocate_dwo_unit_table (struct objfile *objfile)
8287 {
8288 /* Start out with a pretty small number.
8289 Generally DWO files contain only one CU and maybe some TUs. */
8290 return htab_create_alloc_ex (3,
8291 hash_dwo_unit,
8292 eq_dwo_unit,
8293 NULL,
8294 &objfile->objfile_obstack,
8295 hashtab_obstack_allocate,
8296 dummy_obstack_deallocate);
8297 }
8298
8299 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
8300
8301 struct create_dwo_info_table_data
8302 {
8303 struct dwo_file *dwo_file;
8304 htab_t cu_htab;
8305 };
8306
8307 /* die_reader_func for create_dwo_debug_info_hash_table. */
8308
8309 static void
8310 create_dwo_debug_info_hash_table_reader (const struct die_reader_specs *reader,
8311 gdb_byte *info_ptr,
8312 struct die_info *comp_unit_die,
8313 int has_children,
8314 void *datap)
8315 {
8316 struct dwarf2_cu *cu = reader->cu;
8317 struct objfile *objfile = dwarf2_per_objfile->objfile;
8318 sect_offset offset = cu->per_cu->offset;
8319 struct dwarf2_section_info *section = cu->per_cu->info_or_types_section;
8320 struct create_dwo_info_table_data *data = datap;
8321 struct dwo_file *dwo_file = data->dwo_file;
8322 htab_t cu_htab = data->cu_htab;
8323 void **slot;
8324 struct attribute *attr;
8325 struct dwo_unit *dwo_unit;
8326
8327 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8328 if (attr == NULL)
8329 {
8330 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
8331 " its dwo_id [in module %s]"),
8332 offset.sect_off, dwo_file->name);
8333 return;
8334 }
8335
8336 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8337 dwo_unit->dwo_file = dwo_file;
8338 dwo_unit->signature = DW_UNSND (attr);
8339 dwo_unit->info_or_types_section = section;
8340 dwo_unit->offset = offset;
8341 dwo_unit->length = cu->per_cu->length;
8342
8343 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
8344 gdb_assert (slot != NULL);
8345 if (*slot != NULL)
8346 {
8347 const struct dwo_unit *dup_dwo_unit = *slot;
8348
8349 complaint (&symfile_complaints,
8350 _("debug entry at offset 0x%x is duplicate to the entry at"
8351 " offset 0x%x, dwo_id 0x%s [in module %s]"),
8352 offset.sect_off, dup_dwo_unit->offset.sect_off,
8353 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
8354 dwo_file->name);
8355 }
8356 else
8357 *slot = dwo_unit;
8358
8359 if (dwarf2_read_debug)
8360 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
8361 offset.sect_off,
8362 phex (dwo_unit->signature,
8363 sizeof (dwo_unit->signature)));
8364 }
8365
8366 /* Create a hash table to map DWO IDs to their CU entry in
8367 .debug_info.dwo in DWO_FILE.
8368 Note: This function processes DWO files only, not DWP files. */
8369
8370 static htab_t
8371 create_dwo_debug_info_hash_table (struct dwo_file *dwo_file)
8372 {
8373 struct objfile *objfile = dwarf2_per_objfile->objfile;
8374 struct dwarf2_section_info *section = &dwo_file->sections.info;
8375 bfd *abfd;
8376 htab_t cu_htab;
8377 gdb_byte *info_ptr, *end_ptr;
8378 struct create_dwo_info_table_data create_dwo_info_table_data;
8379
8380 dwarf2_read_section (objfile, section);
8381 info_ptr = section->buffer;
8382
8383 if (info_ptr == NULL)
8384 return NULL;
8385
8386 /* We can't set abfd until now because the section may be empty or
8387 not present, in which case section->asection will be NULL. */
8388 abfd = section->asection->owner;
8389
8390 if (dwarf2_read_debug)
8391 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
8392 bfd_get_filename (abfd));
8393
8394 cu_htab = allocate_dwo_unit_table (objfile);
8395
8396 create_dwo_info_table_data.dwo_file = dwo_file;
8397 create_dwo_info_table_data.cu_htab = cu_htab;
8398
8399 end_ptr = info_ptr + section->size;
8400 while (info_ptr < end_ptr)
8401 {
8402 struct dwarf2_per_cu_data per_cu;
8403
8404 memset (&per_cu, 0, sizeof (per_cu));
8405 per_cu.objfile = objfile;
8406 per_cu.is_debug_types = 0;
8407 per_cu.offset.sect_off = info_ptr - section->buffer;
8408 per_cu.info_or_types_section = section;
8409
8410 init_cutu_and_read_dies_no_follow (&per_cu,
8411 &dwo_file->sections.abbrev,
8412 dwo_file,
8413 create_dwo_debug_info_hash_table_reader,
8414 &create_dwo_info_table_data);
8415
8416 info_ptr += per_cu.length;
8417 }
8418
8419 return cu_htab;
8420 }
8421
8422 /* DWP file .debug_{cu,tu}_index section format:
8423 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8424
8425 Both index sections have the same format, and serve to map a 64-bit
8426 signature to a set of section numbers. Each section begins with a header,
8427 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8428 indexes, and a pool of 32-bit section numbers. The index sections will be
8429 aligned at 8-byte boundaries in the file.
8430
8431 The index section header contains two unsigned 32-bit values (using the
8432 byte order of the application binary):
8433
8434 N, the number of compilation units or type units in the index
8435 M, the number of slots in the hash table
8436
8437 (We assume that N and M will not exceed 2^32 - 1.)
8438
8439 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
8440
8441 The hash table begins at offset 8 in the section, and consists of an array
8442 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
8443 order of the application binary). Unused slots in the hash table are 0.
8444 (We rely on the extreme unlikeliness of a signature being exactly 0.)
8445
8446 The parallel table begins immediately after the hash table
8447 (at offset 8 + 8 * M from the beginning of the section), and consists of an
8448 array of 32-bit indexes (using the byte order of the application binary),
8449 corresponding 1-1 with slots in the hash table. Each entry in the parallel
8450 table contains a 32-bit index into the pool of section numbers. For unused
8451 hash table slots, the corresponding entry in the parallel table will be 0.
8452
8453 Given a 64-bit compilation unit signature or a type signature S, an entry
8454 in the hash table is located as follows:
8455
8456 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8457 the low-order k bits all set to 1.
8458
8459 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
8460
8461 3) If the hash table entry at index H matches the signature, use that
8462 entry. If the hash table entry at index H is unused (all zeroes),
8463 terminate the search: the signature is not present in the table.
8464
8465 4) Let H = (H + H') modulo M. Repeat at Step 3.
8466
8467 Because M > N and H' and M are relatively prime, the search is guaranteed
8468 to stop at an unused slot or find the match.
8469
8470 The pool of section numbers begins immediately following the hash table
8471 (at offset 8 + 12 * M from the beginning of the section). The pool of
8472 section numbers consists of an array of 32-bit words (using the byte order
8473 of the application binary). Each item in the array is indexed starting
8474 from 0. The hash table entry provides the index of the first section
8475 number in the set. Additional section numbers in the set follow, and the
8476 set is terminated by a 0 entry (section number 0 is not used in ELF).
8477
8478 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8479 section must be the first entry in the set, and the .debug_abbrev.dwo must
8480 be the second entry. Other members of the set may follow in any order. */
8481
8482 /* Create a hash table to map DWO IDs to their CU/TU entry in
8483 .debug_{info,types}.dwo in DWP_FILE.
8484 Returns NULL if there isn't one.
8485 Note: This function processes DWP files only, not DWO files. */
8486
8487 static struct dwp_hash_table *
8488 create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
8489 {
8490 struct objfile *objfile = dwarf2_per_objfile->objfile;
8491 bfd *dbfd = dwp_file->dbfd;
8492 char *index_ptr, *index_end;
8493 struct dwarf2_section_info *index;
8494 uint32_t version, nr_units, nr_slots;
8495 struct dwp_hash_table *htab;
8496
8497 if (is_debug_types)
8498 index = &dwp_file->sections.tu_index;
8499 else
8500 index = &dwp_file->sections.cu_index;
8501
8502 if (dwarf2_section_empty_p (index))
8503 return NULL;
8504 dwarf2_read_section (objfile, index);
8505
8506 index_ptr = index->buffer;
8507 index_end = index_ptr + index->size;
8508
8509 version = read_4_bytes (dbfd, index_ptr);
8510 index_ptr += 8; /* Skip the unused word. */
8511 nr_units = read_4_bytes (dbfd, index_ptr);
8512 index_ptr += 4;
8513 nr_slots = read_4_bytes (dbfd, index_ptr);
8514 index_ptr += 4;
8515
8516 if (version != 1)
8517 {
8518 error (_("Dwarf Error: unsupported DWP file version (%u)"
8519 " [in module %s]"),
8520 version, dwp_file->name);
8521 }
8522 if (nr_slots != (nr_slots & -nr_slots))
8523 {
8524 error (_("Dwarf Error: number of slots in DWP hash table (%u)"
8525 " is not power of 2 [in module %s]"),
8526 nr_slots, dwp_file->name);
8527 }
8528
8529 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
8530 htab->nr_units = nr_units;
8531 htab->nr_slots = nr_slots;
8532 htab->hash_table = index_ptr;
8533 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
8534 htab->section_pool = htab->unit_table + sizeof (uint32_t) * nr_slots;
8535
8536 return htab;
8537 }
8538
8539 /* Update SECTIONS with the data from SECTP.
8540
8541 This function is like the other "locate" section routines that are
8542 passed to bfd_map_over_sections, but in this context the sections to
8543 read comes from the DWP hash table, not the full ELF section table.
8544
8545 The result is non-zero for success, or zero if an error was found. */
8546
8547 static int
8548 locate_virtual_dwo_sections (asection *sectp,
8549 struct virtual_dwo_sections *sections)
8550 {
8551 const struct dwop_section_names *names = &dwop_section_names;
8552
8553 if (section_is_p (sectp->name, &names->abbrev_dwo))
8554 {
8555 /* There can be only one. */
8556 if (sections->abbrev.asection != NULL)
8557 return 0;
8558 sections->abbrev.asection = sectp;
8559 sections->abbrev.size = bfd_get_section_size (sectp);
8560 }
8561 else if (section_is_p (sectp->name, &names->info_dwo)
8562 || section_is_p (sectp->name, &names->types_dwo))
8563 {
8564 /* There can be only one. */
8565 if (sections->info_or_types.asection != NULL)
8566 return 0;
8567 sections->info_or_types.asection = sectp;
8568 sections->info_or_types.size = bfd_get_section_size (sectp);
8569 }
8570 else if (section_is_p (sectp->name, &names->line_dwo))
8571 {
8572 /* There can be only one. */
8573 if (sections->line.asection != NULL)
8574 return 0;
8575 sections->line.asection = sectp;
8576 sections->line.size = bfd_get_section_size (sectp);
8577 }
8578 else if (section_is_p (sectp->name, &names->loc_dwo))
8579 {
8580 /* There can be only one. */
8581 if (sections->loc.asection != NULL)
8582 return 0;
8583 sections->loc.asection = sectp;
8584 sections->loc.size = bfd_get_section_size (sectp);
8585 }
8586 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8587 {
8588 /* There can be only one. */
8589 if (sections->macinfo.asection != NULL)
8590 return 0;
8591 sections->macinfo.asection = sectp;
8592 sections->macinfo.size = bfd_get_section_size (sectp);
8593 }
8594 else if (section_is_p (sectp->name, &names->macro_dwo))
8595 {
8596 /* There can be only one. */
8597 if (sections->macro.asection != NULL)
8598 return 0;
8599 sections->macro.asection = sectp;
8600 sections->macro.size = bfd_get_section_size (sectp);
8601 }
8602 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8603 {
8604 /* There can be only one. */
8605 if (sections->str_offsets.asection != NULL)
8606 return 0;
8607 sections->str_offsets.asection = sectp;
8608 sections->str_offsets.size = bfd_get_section_size (sectp);
8609 }
8610 else
8611 {
8612 /* No other kind of section is valid. */
8613 return 0;
8614 }
8615
8616 return 1;
8617 }
8618
8619 /* Create a dwo_unit object for the DWO with signature SIGNATURE.
8620 HTAB is the hash table from the DWP file.
8621 SECTION_INDEX is the index of the DWO in HTAB. */
8622
8623 static struct dwo_unit *
8624 create_dwo_in_dwp (struct dwp_file *dwp_file,
8625 const struct dwp_hash_table *htab,
8626 uint32_t section_index,
8627 ULONGEST signature, int is_debug_types)
8628 {
8629 struct objfile *objfile = dwarf2_per_objfile->objfile;
8630 bfd *dbfd = dwp_file->dbfd;
8631 const char *kind = is_debug_types ? "TU" : "CU";
8632 struct dwo_file *dwo_file;
8633 struct dwo_unit *dwo_unit;
8634 struct virtual_dwo_sections sections;
8635 void **dwo_file_slot;
8636 char *virtual_dwo_name;
8637 struct dwarf2_section_info *cutu;
8638 struct cleanup *cleanups;
8639 int i;
8640
8641 if (dwarf2_read_debug)
8642 {
8643 fprintf_unfiltered (gdb_stdlog, "Reading %s %u/0x%s in DWP file: %s\n",
8644 kind,
8645 section_index, phex (signature, sizeof (signature)),
8646 dwp_file->name);
8647 }
8648
8649 /* Fetch the sections of this DWO.
8650 Put a limit on the number of sections we look for so that bad data
8651 doesn't cause us to loop forever. */
8652
8653 #define MAX_NR_DWO_SECTIONS \
8654 (1 /* .debug_info or .debug_types */ \
8655 + 1 /* .debug_abbrev */ \
8656 + 1 /* .debug_line */ \
8657 + 1 /* .debug_loc */ \
8658 + 1 /* .debug_str_offsets */ \
8659 + 1 /* .debug_macro */ \
8660 + 1 /* .debug_macinfo */ \
8661 + 1 /* trailing zero */)
8662
8663 memset (&sections, 0, sizeof (sections));
8664 cleanups = make_cleanup (null_cleanup, 0);
8665
8666 for (i = 0; i < MAX_NR_DWO_SECTIONS; ++i)
8667 {
8668 asection *sectp;
8669 uint32_t section_nr =
8670 read_4_bytes (dbfd,
8671 htab->section_pool
8672 + (section_index + i) * sizeof (uint32_t));
8673
8674 if (section_nr == 0)
8675 break;
8676 if (section_nr >= dwp_file->num_sections)
8677 {
8678 error (_("Dwarf Error: bad DWP hash table, section number too large"
8679 " [in module %s]"),
8680 dwp_file->name);
8681 }
8682
8683 sectp = dwp_file->elf_sections[section_nr];
8684 if (! locate_virtual_dwo_sections (sectp, &sections))
8685 {
8686 error (_("Dwarf Error: bad DWP hash table, invalid section found"
8687 " [in module %s]"),
8688 dwp_file->name);
8689 }
8690 }
8691
8692 if (i < 2
8693 || sections.info_or_types.asection == NULL
8694 || sections.abbrev.asection == NULL)
8695 {
8696 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
8697 " [in module %s]"),
8698 dwp_file->name);
8699 }
8700 if (i == MAX_NR_DWO_SECTIONS)
8701 {
8702 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
8703 " [in module %s]"),
8704 dwp_file->name);
8705 }
8706
8707 /* It's easier for the rest of the code if we fake a struct dwo_file and
8708 have dwo_unit "live" in that. At least for now.
8709
8710 The DWP file can be made up of a random collection of CUs and TUs.
8711 However, for each CU + set of TUs that came from the same original DWO
8712 file, we want to combine them back into a virtual DWO file to save space
8713 (fewer struct dwo_file objects to allocated). Remember that for really
8714 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8715
8716 virtual_dwo_name =
8717 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
8718 sections.abbrev.asection ? sections.abbrev.asection->id : 0,
8719 sections.line.asection ? sections.line.asection->id : 0,
8720 sections.loc.asection ? sections.loc.asection->id : 0,
8721 (sections.str_offsets.asection
8722 ? sections.str_offsets.asection->id
8723 : 0));
8724 make_cleanup (xfree, virtual_dwo_name);
8725 /* Can we use an existing virtual DWO file? */
8726 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name);
8727 /* Create one if necessary. */
8728 if (*dwo_file_slot == NULL)
8729 {
8730 if (dwarf2_read_debug)
8731 {
8732 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
8733 virtual_dwo_name);
8734 }
8735 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8736 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8737 virtual_dwo_name,
8738 strlen (virtual_dwo_name));
8739 dwo_file->sections.abbrev = sections.abbrev;
8740 dwo_file->sections.line = sections.line;
8741 dwo_file->sections.loc = sections.loc;
8742 dwo_file->sections.macinfo = sections.macinfo;
8743 dwo_file->sections.macro = sections.macro;
8744 dwo_file->sections.str_offsets = sections.str_offsets;
8745 /* The "str" section is global to the entire DWP file. */
8746 dwo_file->sections.str = dwp_file->sections.str;
8747 /* The info or types section is assigned later to dwo_unit,
8748 there's no need to record it in dwo_file.
8749 Also, we can't simply record type sections in dwo_file because
8750 we record a pointer into the vector in dwo_unit. As we collect more
8751 types we'll grow the vector and eventually have to reallocate space
8752 for it, invalidating all the pointers into the current copy. */
8753 *dwo_file_slot = dwo_file;
8754 }
8755 else
8756 {
8757 if (dwarf2_read_debug)
8758 {
8759 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
8760 virtual_dwo_name);
8761 }
8762 dwo_file = *dwo_file_slot;
8763 }
8764 do_cleanups (cleanups);
8765
8766 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8767 dwo_unit->dwo_file = dwo_file;
8768 dwo_unit->signature = signature;
8769 dwo_unit->info_or_types_section =
8770 obstack_alloc (&objfile->objfile_obstack,
8771 sizeof (struct dwarf2_section_info));
8772 *dwo_unit->info_or_types_section = sections.info_or_types;
8773 /* offset, length, type_offset_in_tu are set later. */
8774
8775 return dwo_unit;
8776 }
8777
8778 /* Lookup the DWO with SIGNATURE in DWP_FILE. */
8779
8780 static struct dwo_unit *
8781 lookup_dwo_in_dwp (struct dwp_file *dwp_file,
8782 const struct dwp_hash_table *htab,
8783 ULONGEST signature, int is_debug_types)
8784 {
8785 bfd *dbfd = dwp_file->dbfd;
8786 uint32_t mask = htab->nr_slots - 1;
8787 uint32_t hash = signature & mask;
8788 uint32_t hash2 = ((signature >> 32) & mask) | 1;
8789 unsigned int i;
8790 void **slot;
8791 struct dwo_unit find_dwo_cu, *dwo_cu;
8792
8793 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
8794 find_dwo_cu.signature = signature;
8795 slot = htab_find_slot (dwp_file->loaded_cutus, &find_dwo_cu, INSERT);
8796
8797 if (*slot != NULL)
8798 return *slot;
8799
8800 /* Use a for loop so that we don't loop forever on bad debug info. */
8801 for (i = 0; i < htab->nr_slots; ++i)
8802 {
8803 ULONGEST signature_in_table;
8804
8805 signature_in_table =
8806 read_8_bytes (dbfd, htab->hash_table + hash * sizeof (uint64_t));
8807 if (signature_in_table == signature)
8808 {
8809 uint32_t section_index =
8810 read_4_bytes (dbfd, htab->unit_table + hash * sizeof (uint32_t));
8811
8812 *slot = create_dwo_in_dwp (dwp_file, htab, section_index,
8813 signature, is_debug_types);
8814 return *slot;
8815 }
8816 if (signature_in_table == 0)
8817 return NULL;
8818 hash = (hash + hash2) & mask;
8819 }
8820
8821 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
8822 " [in module %s]"),
8823 dwp_file->name);
8824 }
8825
8826 /* Subroutine of open_dwop_file to simplify it.
8827 Open the file specified by FILE_NAME and hand it off to BFD for
8828 preliminary analysis. Return a newly initialized bfd *, which
8829 includes a canonicalized copy of FILE_NAME.
8830 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
8831 In case of trouble, return NULL.
8832 NOTE: This function is derived from symfile_bfd_open. */
8833
8834 static bfd *
8835 try_open_dwop_file (const char *file_name, int is_dwp)
8836 {
8837 bfd *sym_bfd;
8838 int desc, flags;
8839 char *absolute_name;
8840
8841 flags = OPF_TRY_CWD_FIRST;
8842 if (is_dwp)
8843 flags |= OPF_SEARCH_IN_PATH;
8844 desc = openp (debug_file_directory, flags, file_name,
8845 O_RDONLY | O_BINARY, &absolute_name);
8846 if (desc < 0)
8847 return NULL;
8848
8849 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
8850 if (!sym_bfd)
8851 {
8852 xfree (absolute_name);
8853 return NULL;
8854 }
8855 xfree (absolute_name);
8856 bfd_set_cacheable (sym_bfd, 1);
8857
8858 if (!bfd_check_format (sym_bfd, bfd_object))
8859 {
8860 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
8861 return NULL;
8862 }
8863
8864 return sym_bfd;
8865 }
8866
8867 /* Try to open DWO/DWP file FILE_NAME.
8868 COMP_DIR is the DW_AT_comp_dir attribute.
8869 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
8870 The result is the bfd handle of the file.
8871 If there is a problem finding or opening the file, return NULL.
8872 Upon success, the canonicalized path of the file is stored in the bfd,
8873 same as symfile_bfd_open. */
8874
8875 static bfd *
8876 open_dwop_file (const char *file_name, const char *comp_dir, int is_dwp)
8877 {
8878 bfd *abfd;
8879
8880 if (IS_ABSOLUTE_PATH (file_name))
8881 return try_open_dwop_file (file_name, is_dwp);
8882
8883 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
8884
8885 if (comp_dir != NULL)
8886 {
8887 char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
8888
8889 /* NOTE: If comp_dir is a relative path, this will also try the
8890 search path, which seems useful. */
8891 abfd = try_open_dwop_file (path_to_try, is_dwp);
8892 xfree (path_to_try);
8893 if (abfd != NULL)
8894 return abfd;
8895 }
8896
8897 /* That didn't work, try debug-file-directory, which, despite its name,
8898 is a list of paths. */
8899
8900 if (*debug_file_directory == '\0')
8901 return NULL;
8902
8903 return try_open_dwop_file (file_name, is_dwp);
8904 }
8905
8906 /* This function is mapped across the sections and remembers the offset and
8907 size of each of the DWO debugging sections we are interested in. */
8908
8909 static void
8910 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
8911 {
8912 struct dwo_sections *dwo_sections = dwo_sections_ptr;
8913 const struct dwop_section_names *names = &dwop_section_names;
8914
8915 if (section_is_p (sectp->name, &names->abbrev_dwo))
8916 {
8917 dwo_sections->abbrev.asection = sectp;
8918 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
8919 }
8920 else if (section_is_p (sectp->name, &names->info_dwo))
8921 {
8922 dwo_sections->info.asection = sectp;
8923 dwo_sections->info.size = bfd_get_section_size (sectp);
8924 }
8925 else if (section_is_p (sectp->name, &names->line_dwo))
8926 {
8927 dwo_sections->line.asection = sectp;
8928 dwo_sections->line.size = bfd_get_section_size (sectp);
8929 }
8930 else if (section_is_p (sectp->name, &names->loc_dwo))
8931 {
8932 dwo_sections->loc.asection = sectp;
8933 dwo_sections->loc.size = bfd_get_section_size (sectp);
8934 }
8935 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8936 {
8937 dwo_sections->macinfo.asection = sectp;
8938 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
8939 }
8940 else if (section_is_p (sectp->name, &names->macro_dwo))
8941 {
8942 dwo_sections->macro.asection = sectp;
8943 dwo_sections->macro.size = bfd_get_section_size (sectp);
8944 }
8945 else if (section_is_p (sectp->name, &names->str_dwo))
8946 {
8947 dwo_sections->str.asection = sectp;
8948 dwo_sections->str.size = bfd_get_section_size (sectp);
8949 }
8950 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8951 {
8952 dwo_sections->str_offsets.asection = sectp;
8953 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
8954 }
8955 else if (section_is_p (sectp->name, &names->types_dwo))
8956 {
8957 struct dwarf2_section_info type_section;
8958
8959 memset (&type_section, 0, sizeof (type_section));
8960 type_section.asection = sectp;
8961 type_section.size = bfd_get_section_size (sectp);
8962 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
8963 &type_section);
8964 }
8965 }
8966
8967 /* Initialize the use of the DWO file specified by DWO_NAME.
8968 The result is NULL if DWO_NAME can't be found. */
8969
8970 static struct dwo_file *
8971 open_and_init_dwo_file (const char *dwo_name, const char *comp_dir)
8972 {
8973 struct objfile *objfile = dwarf2_per_objfile->objfile;
8974 struct dwo_file *dwo_file;
8975 bfd *dbfd;
8976 struct cleanup *cleanups;
8977
8978 dbfd = open_dwop_file (dwo_name, comp_dir, 0);
8979 if (dbfd == NULL)
8980 {
8981 if (dwarf2_read_debug)
8982 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
8983 return NULL;
8984 }
8985 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8986 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8987 dwo_name, strlen (dwo_name));
8988 dwo_file->dbfd = dbfd;
8989
8990 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
8991
8992 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
8993
8994 dwo_file->cus = create_dwo_debug_info_hash_table (dwo_file);
8995
8996 dwo_file->tus = create_debug_types_hash_table (dwo_file,
8997 dwo_file->sections.types);
8998
8999 discard_cleanups (cleanups);
9000
9001 if (dwarf2_read_debug)
9002 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
9003
9004 return dwo_file;
9005 }
9006
9007 /* This function is mapped across the sections and remembers the offset and
9008 size of each of the DWP debugging sections we are interested in. */
9009
9010 static void
9011 dwarf2_locate_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
9012 {
9013 struct dwp_file *dwp_file = dwp_file_ptr;
9014 const struct dwop_section_names *names = &dwop_section_names;
9015 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
9016
9017 /* Record the ELF section number for later lookup: this is what the
9018 .debug_cu_index,.debug_tu_index tables use. */
9019 gdb_assert (elf_section_nr < dwp_file->num_sections);
9020 dwp_file->elf_sections[elf_section_nr] = sectp;
9021
9022 /* Look for specific sections that we need. */
9023 if (section_is_p (sectp->name, &names->str_dwo))
9024 {
9025 dwp_file->sections.str.asection = sectp;
9026 dwp_file->sections.str.size = bfd_get_section_size (sectp);
9027 }
9028 else if (section_is_p (sectp->name, &names->cu_index))
9029 {
9030 dwp_file->sections.cu_index.asection = sectp;
9031 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
9032 }
9033 else if (section_is_p (sectp->name, &names->tu_index))
9034 {
9035 dwp_file->sections.tu_index.asection = sectp;
9036 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
9037 }
9038 }
9039
9040 /* Hash function for dwp_file loaded CUs/TUs. */
9041
9042 static hashval_t
9043 hash_dwp_loaded_cutus (const void *item)
9044 {
9045 const struct dwo_unit *dwo_unit = item;
9046
9047 /* This drops the top 32 bits of the signature, but is ok for a hash. */
9048 return dwo_unit->signature;
9049 }
9050
9051 /* Equality function for dwp_file loaded CUs/TUs. */
9052
9053 static int
9054 eq_dwp_loaded_cutus (const void *a, const void *b)
9055 {
9056 const struct dwo_unit *dua = a;
9057 const struct dwo_unit *dub = b;
9058
9059 return dua->signature == dub->signature;
9060 }
9061
9062 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
9063
9064 static htab_t
9065 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
9066 {
9067 return htab_create_alloc_ex (3,
9068 hash_dwp_loaded_cutus,
9069 eq_dwp_loaded_cutus,
9070 NULL,
9071 &objfile->objfile_obstack,
9072 hashtab_obstack_allocate,
9073 dummy_obstack_deallocate);
9074 }
9075
9076 /* Initialize the use of the DWP file for the current objfile.
9077 By convention the name of the DWP file is ${objfile}.dwp.
9078 The result is NULL if it can't be found. */
9079
9080 static struct dwp_file *
9081 open_and_init_dwp_file (const char *comp_dir)
9082 {
9083 struct objfile *objfile = dwarf2_per_objfile->objfile;
9084 struct dwp_file *dwp_file;
9085 char *dwp_name;
9086 bfd *dbfd;
9087 struct cleanup *cleanups;
9088
9089 dwp_name = xstrprintf ("%s.dwp", dwarf2_per_objfile->objfile->name);
9090 cleanups = make_cleanup (xfree, dwp_name);
9091
9092 dbfd = open_dwop_file (dwp_name, comp_dir, 1);
9093 if (dbfd == NULL)
9094 {
9095 if (dwarf2_read_debug)
9096 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
9097 do_cleanups (cleanups);
9098 return NULL;
9099 }
9100 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
9101 dwp_file->name = obstack_copy0 (&objfile->objfile_obstack,
9102 dwp_name, strlen (dwp_name));
9103 dwp_file->dbfd = dbfd;
9104 do_cleanups (cleanups);
9105
9106 cleanups = make_cleanup (free_dwo_file_cleanup, dwp_file);
9107
9108 /* +1: section 0 is unused */
9109 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
9110 dwp_file->elf_sections =
9111 OBSTACK_CALLOC (&objfile->objfile_obstack,
9112 dwp_file->num_sections, asection *);
9113
9114 bfd_map_over_sections (dbfd, dwarf2_locate_dwp_sections, dwp_file);
9115
9116 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
9117
9118 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
9119
9120 dwp_file->loaded_cutus = allocate_dwp_loaded_cutus_table (objfile);
9121
9122 discard_cleanups (cleanups);
9123
9124 if (dwarf2_read_debug)
9125 {
9126 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
9127 fprintf_unfiltered (gdb_stdlog,
9128 " %u CUs, %u TUs\n",
9129 dwp_file->cus ? dwp_file->cus->nr_units : 0,
9130 dwp_file->tus ? dwp_file->tus->nr_units : 0);
9131 }
9132
9133 return dwp_file;
9134 }
9135
9136 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9137 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9138 or in the DWP file for the objfile, referenced by THIS_UNIT.
9139 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
9140 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9141
9142 This is called, for example, when wanting to read a variable with a
9143 complex location. Therefore we don't want to do file i/o for every call.
9144 Therefore we don't want to look for a DWO file on every call.
9145 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9146 then we check if we've already seen DWO_NAME, and only THEN do we check
9147 for a DWO file.
9148
9149 The result is a pointer to the dwo_unit object or NULL if we didn't find it
9150 (dwo_id mismatch or couldn't find the DWO/DWP file). */
9151
9152 static struct dwo_unit *
9153 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
9154 const char *dwo_name, const char *comp_dir,
9155 ULONGEST signature, int is_debug_types)
9156 {
9157 struct objfile *objfile = dwarf2_per_objfile->objfile;
9158 const char *kind = is_debug_types ? "TU" : "CU";
9159 void **dwo_file_slot;
9160 struct dwo_file *dwo_file;
9161 struct dwp_file *dwp_file;
9162
9163 /* Have we already read SIGNATURE from a DWP file? */
9164
9165 if (! dwarf2_per_objfile->dwp_checked)
9166 {
9167 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file (comp_dir);
9168 dwarf2_per_objfile->dwp_checked = 1;
9169 }
9170 dwp_file = dwarf2_per_objfile->dwp_file;
9171
9172 if (dwp_file != NULL)
9173 {
9174 const struct dwp_hash_table *dwp_htab =
9175 is_debug_types ? dwp_file->tus : dwp_file->cus;
9176
9177 if (dwp_htab != NULL)
9178 {
9179 struct dwo_unit *dwo_cutu =
9180 lookup_dwo_in_dwp (dwp_file, dwp_htab, signature, is_debug_types);
9181
9182 if (dwo_cutu != NULL)
9183 {
9184 if (dwarf2_read_debug)
9185 {
9186 fprintf_unfiltered (gdb_stdlog,
9187 "Virtual DWO %s %s found: @%s\n",
9188 kind, hex_string (signature),
9189 host_address_to_string (dwo_cutu));
9190 }
9191 return dwo_cutu;
9192 }
9193 }
9194 }
9195
9196 /* Have we already seen DWO_NAME? */
9197
9198 dwo_file_slot = lookup_dwo_file_slot (dwo_name);
9199 if (*dwo_file_slot == NULL)
9200 {
9201 /* Read in the file and build a table of the DWOs it contains. */
9202 *dwo_file_slot = open_and_init_dwo_file (dwo_name, comp_dir);
9203 }
9204 /* NOTE: This will be NULL if unable to open the file. */
9205 dwo_file = *dwo_file_slot;
9206
9207 if (dwo_file != NULL)
9208 {
9209 htab_t htab = is_debug_types ? dwo_file->tus : dwo_file->cus;
9210
9211 if (htab != NULL)
9212 {
9213 struct dwo_unit find_dwo_cutu, *dwo_cutu;
9214
9215 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9216 find_dwo_cutu.signature = signature;
9217 dwo_cutu = htab_find (htab, &find_dwo_cutu);
9218
9219 if (dwo_cutu != NULL)
9220 {
9221 if (dwarf2_read_debug)
9222 {
9223 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
9224 kind, dwo_name, hex_string (signature),
9225 host_address_to_string (dwo_cutu));
9226 }
9227 return dwo_cutu;
9228 }
9229 }
9230 }
9231
9232 /* We didn't find it. This could mean a dwo_id mismatch, or
9233 someone deleted the DWO/DWP file, or the search path isn't set up
9234 correctly to find the file. */
9235
9236 if (dwarf2_read_debug)
9237 {
9238 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
9239 kind, dwo_name, hex_string (signature));
9240 }
9241
9242 complaint (&symfile_complaints,
9243 _("Could not find DWO CU referenced by CU at offset 0x%x"
9244 " [in module %s]"),
9245 this_unit->offset.sect_off, objfile->name);
9246 return NULL;
9247 }
9248
9249 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9250 See lookup_dwo_cutu_unit for details. */
9251
9252 static struct dwo_unit *
9253 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
9254 const char *dwo_name, const char *comp_dir,
9255 ULONGEST signature)
9256 {
9257 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
9258 }
9259
9260 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9261 See lookup_dwo_cutu_unit for details. */
9262
9263 static struct dwo_unit *
9264 lookup_dwo_type_unit (struct signatured_type *this_tu,
9265 const char *dwo_name, const char *comp_dir)
9266 {
9267 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
9268 }
9269
9270 /* Free all resources associated with DWO_FILE.
9271 Close the DWO file and munmap the sections.
9272 All memory should be on the objfile obstack. */
9273
9274 static void
9275 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
9276 {
9277 int ix;
9278 struct dwarf2_section_info *section;
9279
9280 gdb_bfd_unref (dwo_file->dbfd);
9281
9282 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
9283 }
9284
9285 /* Wrapper for free_dwo_file for use in cleanups. */
9286
9287 static void
9288 free_dwo_file_cleanup (void *arg)
9289 {
9290 struct dwo_file *dwo_file = (struct dwo_file *) arg;
9291 struct objfile *objfile = dwarf2_per_objfile->objfile;
9292
9293 free_dwo_file (dwo_file, objfile);
9294 }
9295
9296 /* Traversal function for free_dwo_files. */
9297
9298 static int
9299 free_dwo_file_from_slot (void **slot, void *info)
9300 {
9301 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
9302 struct objfile *objfile = (struct objfile *) info;
9303
9304 free_dwo_file (dwo_file, objfile);
9305
9306 return 1;
9307 }
9308
9309 /* Free all resources associated with DWO_FILES. */
9310
9311 static void
9312 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
9313 {
9314 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
9315 }
9316 \f
9317 /* Read in various DIEs. */
9318
9319 /* qsort helper for inherit_abstract_dies. */
9320
9321 static int
9322 unsigned_int_compar (const void *ap, const void *bp)
9323 {
9324 unsigned int a = *(unsigned int *) ap;
9325 unsigned int b = *(unsigned int *) bp;
9326
9327 return (a > b) - (b > a);
9328 }
9329
9330 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
9331 Inherit only the children of the DW_AT_abstract_origin DIE not being
9332 already referenced by DW_AT_abstract_origin from the children of the
9333 current DIE. */
9334
9335 static void
9336 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9337 {
9338 struct die_info *child_die;
9339 unsigned die_children_count;
9340 /* CU offsets which were referenced by children of the current DIE. */
9341 sect_offset *offsets;
9342 sect_offset *offsets_end, *offsetp;
9343 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9344 struct die_info *origin_die;
9345 /* Iterator of the ORIGIN_DIE children. */
9346 struct die_info *origin_child_die;
9347 struct cleanup *cleanups;
9348 struct attribute *attr;
9349 struct dwarf2_cu *origin_cu;
9350 struct pending **origin_previous_list_in_scope;
9351
9352 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9353 if (!attr)
9354 return;
9355
9356 /* Note that following die references may follow to a die in a
9357 different cu. */
9358
9359 origin_cu = cu;
9360 origin_die = follow_die_ref (die, attr, &origin_cu);
9361
9362 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9363 symbols in. */
9364 origin_previous_list_in_scope = origin_cu->list_in_scope;
9365 origin_cu->list_in_scope = cu->list_in_scope;
9366
9367 if (die->tag != origin_die->tag
9368 && !(die->tag == DW_TAG_inlined_subroutine
9369 && origin_die->tag == DW_TAG_subprogram))
9370 complaint (&symfile_complaints,
9371 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
9372 die->offset.sect_off, origin_die->offset.sect_off);
9373
9374 child_die = die->child;
9375 die_children_count = 0;
9376 while (child_die && child_die->tag)
9377 {
9378 child_die = sibling_die (child_die);
9379 die_children_count++;
9380 }
9381 offsets = xmalloc (sizeof (*offsets) * die_children_count);
9382 cleanups = make_cleanup (xfree, offsets);
9383
9384 offsets_end = offsets;
9385 child_die = die->child;
9386 while (child_die && child_die->tag)
9387 {
9388 /* For each CHILD_DIE, find the corresponding child of
9389 ORIGIN_DIE. If there is more than one layer of
9390 DW_AT_abstract_origin, follow them all; there shouldn't be,
9391 but GCC versions at least through 4.4 generate this (GCC PR
9392 40573). */
9393 struct die_info *child_origin_die = child_die;
9394 struct dwarf2_cu *child_origin_cu = cu;
9395
9396 while (1)
9397 {
9398 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
9399 child_origin_cu);
9400 if (attr == NULL)
9401 break;
9402 child_origin_die = follow_die_ref (child_origin_die, attr,
9403 &child_origin_cu);
9404 }
9405
9406 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
9407 counterpart may exist. */
9408 if (child_origin_die != child_die)
9409 {
9410 if (child_die->tag != child_origin_die->tag
9411 && !(child_die->tag == DW_TAG_inlined_subroutine
9412 && child_origin_die->tag == DW_TAG_subprogram))
9413 complaint (&symfile_complaints,
9414 _("Child DIE 0x%x and its abstract origin 0x%x have "
9415 "different tags"), child_die->offset.sect_off,
9416 child_origin_die->offset.sect_off);
9417 if (child_origin_die->parent != origin_die)
9418 complaint (&symfile_complaints,
9419 _("Child DIE 0x%x and its abstract origin 0x%x have "
9420 "different parents"), child_die->offset.sect_off,
9421 child_origin_die->offset.sect_off);
9422 else
9423 *offsets_end++ = child_origin_die->offset;
9424 }
9425 child_die = sibling_die (child_die);
9426 }
9427 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
9428 unsigned_int_compar);
9429 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
9430 if (offsetp[-1].sect_off == offsetp->sect_off)
9431 complaint (&symfile_complaints,
9432 _("Multiple children of DIE 0x%x refer "
9433 "to DIE 0x%x as their abstract origin"),
9434 die->offset.sect_off, offsetp->sect_off);
9435
9436 offsetp = offsets;
9437 origin_child_die = origin_die->child;
9438 while (origin_child_die && origin_child_die->tag)
9439 {
9440 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
9441 while (offsetp < offsets_end
9442 && offsetp->sect_off < origin_child_die->offset.sect_off)
9443 offsetp++;
9444 if (offsetp >= offsets_end
9445 || offsetp->sect_off > origin_child_die->offset.sect_off)
9446 {
9447 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
9448 process_die (origin_child_die, origin_cu);
9449 }
9450 origin_child_die = sibling_die (origin_child_die);
9451 }
9452 origin_cu->list_in_scope = origin_previous_list_in_scope;
9453
9454 do_cleanups (cleanups);
9455 }
9456
9457 static void
9458 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
9459 {
9460 struct objfile *objfile = cu->objfile;
9461 struct context_stack *new;
9462 CORE_ADDR lowpc;
9463 CORE_ADDR highpc;
9464 struct die_info *child_die;
9465 struct attribute *attr, *call_line, *call_file;
9466 const char *name;
9467 CORE_ADDR baseaddr;
9468 struct block *block;
9469 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
9470 VEC (symbolp) *template_args = NULL;
9471 struct template_symbol *templ_func = NULL;
9472
9473 if (inlined_func)
9474 {
9475 /* If we do not have call site information, we can't show the
9476 caller of this inlined function. That's too confusing, so
9477 only use the scope for local variables. */
9478 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
9479 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
9480 if (call_line == NULL || call_file == NULL)
9481 {
9482 read_lexical_block_scope (die, cu);
9483 return;
9484 }
9485 }
9486
9487 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9488
9489 name = dwarf2_name (die, cu);
9490
9491 /* Ignore functions with missing or empty names. These are actually
9492 illegal according to the DWARF standard. */
9493 if (name == NULL)
9494 {
9495 complaint (&symfile_complaints,
9496 _("missing name for subprogram DIE at %d"),
9497 die->offset.sect_off);
9498 return;
9499 }
9500
9501 /* Ignore functions with missing or invalid low and high pc attributes. */
9502 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9503 {
9504 attr = dwarf2_attr (die, DW_AT_external, cu);
9505 if (!attr || !DW_UNSND (attr))
9506 complaint (&symfile_complaints,
9507 _("cannot get low and high bounds "
9508 "for subprogram DIE at %d"),
9509 die->offset.sect_off);
9510 return;
9511 }
9512
9513 lowpc += baseaddr;
9514 highpc += baseaddr;
9515
9516 /* If we have any template arguments, then we must allocate a
9517 different sort of symbol. */
9518 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
9519 {
9520 if (child_die->tag == DW_TAG_template_type_param
9521 || child_die->tag == DW_TAG_template_value_param)
9522 {
9523 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
9524 struct template_symbol);
9525 templ_func->base.is_cplus_template_function = 1;
9526 break;
9527 }
9528 }
9529
9530 new = push_context (0, lowpc);
9531 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
9532 (struct symbol *) templ_func);
9533
9534 /* If there is a location expression for DW_AT_frame_base, record
9535 it. */
9536 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
9537 if (attr)
9538 dwarf2_symbol_mark_computed (attr, new->name, cu, 1);
9539
9540 cu->list_in_scope = &local_symbols;
9541
9542 if (die->child != NULL)
9543 {
9544 child_die = die->child;
9545 while (child_die && child_die->tag)
9546 {
9547 if (child_die->tag == DW_TAG_template_type_param
9548 || child_die->tag == DW_TAG_template_value_param)
9549 {
9550 struct symbol *arg = new_symbol (child_die, NULL, cu);
9551
9552 if (arg != NULL)
9553 VEC_safe_push (symbolp, template_args, arg);
9554 }
9555 else
9556 process_die (child_die, cu);
9557 child_die = sibling_die (child_die);
9558 }
9559 }
9560
9561 inherit_abstract_dies (die, cu);
9562
9563 /* If we have a DW_AT_specification, we might need to import using
9564 directives from the context of the specification DIE. See the
9565 comment in determine_prefix. */
9566 if (cu->language == language_cplus
9567 && dwarf2_attr (die, DW_AT_specification, cu))
9568 {
9569 struct dwarf2_cu *spec_cu = cu;
9570 struct die_info *spec_die = die_specification (die, &spec_cu);
9571
9572 while (spec_die)
9573 {
9574 child_die = spec_die->child;
9575 while (child_die && child_die->tag)
9576 {
9577 if (child_die->tag == DW_TAG_imported_module)
9578 process_die (child_die, spec_cu);
9579 child_die = sibling_die (child_die);
9580 }
9581
9582 /* In some cases, GCC generates specification DIEs that
9583 themselves contain DW_AT_specification attributes. */
9584 spec_die = die_specification (spec_die, &spec_cu);
9585 }
9586 }
9587
9588 new = pop_context ();
9589 /* Make a block for the local symbols within. */
9590 block = finish_block (new->name, &local_symbols, new->old_blocks,
9591 lowpc, highpc, objfile);
9592
9593 /* For C++, set the block's scope. */
9594 if ((cu->language == language_cplus || cu->language == language_fortran)
9595 && cu->processing_has_namespace_info)
9596 block_set_scope (block, determine_prefix (die, cu),
9597 &objfile->objfile_obstack);
9598
9599 /* If we have address ranges, record them. */
9600 dwarf2_record_block_ranges (die, block, baseaddr, cu);
9601
9602 /* Attach template arguments to function. */
9603 if (! VEC_empty (symbolp, template_args))
9604 {
9605 gdb_assert (templ_func != NULL);
9606
9607 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
9608 templ_func->template_arguments
9609 = obstack_alloc (&objfile->objfile_obstack,
9610 (templ_func->n_template_arguments
9611 * sizeof (struct symbol *)));
9612 memcpy (templ_func->template_arguments,
9613 VEC_address (symbolp, template_args),
9614 (templ_func->n_template_arguments * sizeof (struct symbol *)));
9615 VEC_free (symbolp, template_args);
9616 }
9617
9618 /* In C++, we can have functions nested inside functions (e.g., when
9619 a function declares a class that has methods). This means that
9620 when we finish processing a function scope, we may need to go
9621 back to building a containing block's symbol lists. */
9622 local_symbols = new->locals;
9623 using_directives = new->using_directives;
9624
9625 /* If we've finished processing a top-level function, subsequent
9626 symbols go in the file symbol list. */
9627 if (outermost_context_p ())
9628 cu->list_in_scope = &file_symbols;
9629 }
9630
9631 /* Process all the DIES contained within a lexical block scope. Start
9632 a new scope, process the dies, and then close the scope. */
9633
9634 static void
9635 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
9636 {
9637 struct objfile *objfile = cu->objfile;
9638 struct context_stack *new;
9639 CORE_ADDR lowpc, highpc;
9640 struct die_info *child_die;
9641 CORE_ADDR baseaddr;
9642
9643 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9644
9645 /* Ignore blocks with missing or invalid low and high pc attributes. */
9646 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
9647 as multiple lexical blocks? Handling children in a sane way would
9648 be nasty. Might be easier to properly extend generic blocks to
9649 describe ranges. */
9650 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9651 return;
9652 lowpc += baseaddr;
9653 highpc += baseaddr;
9654
9655 push_context (0, lowpc);
9656 if (die->child != NULL)
9657 {
9658 child_die = die->child;
9659 while (child_die && child_die->tag)
9660 {
9661 process_die (child_die, cu);
9662 child_die = sibling_die (child_die);
9663 }
9664 }
9665 new = pop_context ();
9666
9667 if (local_symbols != NULL || using_directives != NULL)
9668 {
9669 struct block *block
9670 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
9671 highpc, objfile);
9672
9673 /* Note that recording ranges after traversing children, as we
9674 do here, means that recording a parent's ranges entails
9675 walking across all its children's ranges as they appear in
9676 the address map, which is quadratic behavior.
9677
9678 It would be nicer to record the parent's ranges before
9679 traversing its children, simply overriding whatever you find
9680 there. But since we don't even decide whether to create a
9681 block until after we've traversed its children, that's hard
9682 to do. */
9683 dwarf2_record_block_ranges (die, block, baseaddr, cu);
9684 }
9685 local_symbols = new->locals;
9686 using_directives = new->using_directives;
9687 }
9688
9689 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
9690
9691 static void
9692 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
9693 {
9694 struct objfile *objfile = cu->objfile;
9695 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9696 CORE_ADDR pc, baseaddr;
9697 struct attribute *attr;
9698 struct call_site *call_site, call_site_local;
9699 void **slot;
9700 int nparams;
9701 struct die_info *child_die;
9702
9703 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9704
9705 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9706 if (!attr)
9707 {
9708 complaint (&symfile_complaints,
9709 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
9710 "DIE 0x%x [in module %s]"),
9711 die->offset.sect_off, objfile->name);
9712 return;
9713 }
9714 pc = DW_ADDR (attr) + baseaddr;
9715
9716 if (cu->call_site_htab == NULL)
9717 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
9718 NULL, &objfile->objfile_obstack,
9719 hashtab_obstack_allocate, NULL);
9720 call_site_local.pc = pc;
9721 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
9722 if (*slot != NULL)
9723 {
9724 complaint (&symfile_complaints,
9725 _("Duplicate PC %s for DW_TAG_GNU_call_site "
9726 "DIE 0x%x [in module %s]"),
9727 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
9728 return;
9729 }
9730
9731 /* Count parameters at the caller. */
9732
9733 nparams = 0;
9734 for (child_die = die->child; child_die && child_die->tag;
9735 child_die = sibling_die (child_die))
9736 {
9737 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9738 {
9739 complaint (&symfile_complaints,
9740 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
9741 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9742 child_die->tag, child_die->offset.sect_off, objfile->name);
9743 continue;
9744 }
9745
9746 nparams++;
9747 }
9748
9749 call_site = obstack_alloc (&objfile->objfile_obstack,
9750 (sizeof (*call_site)
9751 + (sizeof (*call_site->parameter)
9752 * (nparams - 1))));
9753 *slot = call_site;
9754 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
9755 call_site->pc = pc;
9756
9757 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
9758 {
9759 struct die_info *func_die;
9760
9761 /* Skip also over DW_TAG_inlined_subroutine. */
9762 for (func_die = die->parent;
9763 func_die && func_die->tag != DW_TAG_subprogram
9764 && func_die->tag != DW_TAG_subroutine_type;
9765 func_die = func_die->parent);
9766
9767 /* DW_AT_GNU_all_call_sites is a superset
9768 of DW_AT_GNU_all_tail_call_sites. */
9769 if (func_die
9770 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
9771 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
9772 {
9773 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
9774 not complete. But keep CALL_SITE for look ups via call_site_htab,
9775 both the initial caller containing the real return address PC and
9776 the final callee containing the current PC of a chain of tail
9777 calls do not need to have the tail call list complete. But any
9778 function candidate for a virtual tail call frame searched via
9779 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
9780 determined unambiguously. */
9781 }
9782 else
9783 {
9784 struct type *func_type = NULL;
9785
9786 if (func_die)
9787 func_type = get_die_type (func_die, cu);
9788 if (func_type != NULL)
9789 {
9790 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
9791
9792 /* Enlist this call site to the function. */
9793 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
9794 TYPE_TAIL_CALL_LIST (func_type) = call_site;
9795 }
9796 else
9797 complaint (&symfile_complaints,
9798 _("Cannot find function owning DW_TAG_GNU_call_site "
9799 "DIE 0x%x [in module %s]"),
9800 die->offset.sect_off, objfile->name);
9801 }
9802 }
9803
9804 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
9805 if (attr == NULL)
9806 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9807 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9808 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
9809 /* Keep NULL DWARF_BLOCK. */;
9810 else if (attr_form_is_block (attr))
9811 {
9812 struct dwarf2_locexpr_baton *dlbaton;
9813
9814 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
9815 dlbaton->data = DW_BLOCK (attr)->data;
9816 dlbaton->size = DW_BLOCK (attr)->size;
9817 dlbaton->per_cu = cu->per_cu;
9818
9819 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
9820 }
9821 else if (is_ref_attr (attr))
9822 {
9823 struct dwarf2_cu *target_cu = cu;
9824 struct die_info *target_die;
9825
9826 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
9827 gdb_assert (target_cu->objfile == objfile);
9828 if (die_is_declaration (target_die, target_cu))
9829 {
9830 const char *target_physname = NULL;
9831 struct attribute *target_attr;
9832
9833 /* Prefer the mangled name; otherwise compute the demangled one. */
9834 target_attr = dwarf2_attr (target_die, DW_AT_linkage_name, target_cu);
9835 if (target_attr == NULL)
9836 target_attr = dwarf2_attr (target_die, DW_AT_MIPS_linkage_name,
9837 target_cu);
9838 if (target_attr != NULL && DW_STRING (target_attr) != NULL)
9839 target_physname = DW_STRING (target_attr);
9840 else
9841 target_physname = dwarf2_physname (NULL, target_die, target_cu);
9842 if (target_physname == NULL)
9843 complaint (&symfile_complaints,
9844 _("DW_AT_GNU_call_site_target target DIE has invalid "
9845 "physname, for referencing DIE 0x%x [in module %s]"),
9846 die->offset.sect_off, objfile->name);
9847 else
9848 SET_FIELD_PHYSNAME (call_site->target, target_physname);
9849 }
9850 else
9851 {
9852 CORE_ADDR lowpc;
9853
9854 /* DW_AT_entry_pc should be preferred. */
9855 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
9856 complaint (&symfile_complaints,
9857 _("DW_AT_GNU_call_site_target target DIE has invalid "
9858 "low pc, for referencing DIE 0x%x [in module %s]"),
9859 die->offset.sect_off, objfile->name);
9860 else
9861 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
9862 }
9863 }
9864 else
9865 complaint (&symfile_complaints,
9866 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
9867 "block nor reference, for DIE 0x%x [in module %s]"),
9868 die->offset.sect_off, objfile->name);
9869
9870 call_site->per_cu = cu->per_cu;
9871
9872 for (child_die = die->child;
9873 child_die && child_die->tag;
9874 child_die = sibling_die (child_die))
9875 {
9876 struct call_site_parameter *parameter;
9877 struct attribute *loc, *origin;
9878
9879 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9880 {
9881 /* Already printed the complaint above. */
9882 continue;
9883 }
9884
9885 gdb_assert (call_site->parameter_count < nparams);
9886 parameter = &call_site->parameter[call_site->parameter_count];
9887
9888 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
9889 specifies DW_TAG_formal_parameter. Value of the data assumed for the
9890 register is contained in DW_AT_GNU_call_site_value. */
9891
9892 loc = dwarf2_attr (child_die, DW_AT_location, cu);
9893 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
9894 if (loc == NULL && origin != NULL && is_ref_attr (origin))
9895 {
9896 sect_offset offset;
9897
9898 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9899 offset = dwarf2_get_ref_die_offset (origin);
9900 if (!offset_in_cu_p (&cu->header, offset))
9901 {
9902 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
9903 binding can be done only inside one CU. Such referenced DIE
9904 therefore cannot be even moved to DW_TAG_partial_unit. */
9905 complaint (&symfile_complaints,
9906 _("DW_AT_abstract_origin offset is not in CU for "
9907 "DW_TAG_GNU_call_site child DIE 0x%x "
9908 "[in module %s]"),
9909 child_die->offset.sect_off, objfile->name);
9910 continue;
9911 }
9912 parameter->u.param_offset.cu_off = (offset.sect_off
9913 - cu->header.offset.sect_off);
9914 }
9915 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
9916 {
9917 complaint (&symfile_complaints,
9918 _("No DW_FORM_block* DW_AT_location for "
9919 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9920 child_die->offset.sect_off, objfile->name);
9921 continue;
9922 }
9923 else
9924 {
9925 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9926 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
9927 if (parameter->u.dwarf_reg != -1)
9928 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9929 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
9930 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
9931 &parameter->u.fb_offset))
9932 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
9933 else
9934 {
9935 complaint (&symfile_complaints,
9936 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
9937 "for DW_FORM_block* DW_AT_location is supported for "
9938 "DW_TAG_GNU_call_site child DIE 0x%x "
9939 "[in module %s]"),
9940 child_die->offset.sect_off, objfile->name);
9941 continue;
9942 }
9943 }
9944
9945 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
9946 if (!attr_form_is_block (attr))
9947 {
9948 complaint (&symfile_complaints,
9949 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
9950 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9951 child_die->offset.sect_off, objfile->name);
9952 continue;
9953 }
9954 parameter->value = DW_BLOCK (attr)->data;
9955 parameter->value_size = DW_BLOCK (attr)->size;
9956
9957 /* Parameters are not pre-cleared by memset above. */
9958 parameter->data_value = NULL;
9959 parameter->data_value_size = 0;
9960 call_site->parameter_count++;
9961
9962 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
9963 if (attr)
9964 {
9965 if (!attr_form_is_block (attr))
9966 complaint (&symfile_complaints,
9967 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
9968 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9969 child_die->offset.sect_off, objfile->name);
9970 else
9971 {
9972 parameter->data_value = DW_BLOCK (attr)->data;
9973 parameter->data_value_size = DW_BLOCK (attr)->size;
9974 }
9975 }
9976 }
9977 }
9978
9979 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
9980 Return 1 if the attributes are present and valid, otherwise, return 0.
9981 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
9982
9983 static int
9984 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
9985 CORE_ADDR *high_return, struct dwarf2_cu *cu,
9986 struct partial_symtab *ranges_pst)
9987 {
9988 struct objfile *objfile = cu->objfile;
9989 struct comp_unit_head *cu_header = &cu->header;
9990 bfd *obfd = objfile->obfd;
9991 unsigned int addr_size = cu_header->addr_size;
9992 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
9993 /* Base address selection entry. */
9994 CORE_ADDR base;
9995 int found_base;
9996 unsigned int dummy;
9997 gdb_byte *buffer;
9998 CORE_ADDR marker;
9999 int low_set;
10000 CORE_ADDR low = 0;
10001 CORE_ADDR high = 0;
10002 CORE_ADDR baseaddr;
10003
10004 found_base = cu->base_known;
10005 base = cu->base_address;
10006
10007 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
10008 if (offset >= dwarf2_per_objfile->ranges.size)
10009 {
10010 complaint (&symfile_complaints,
10011 _("Offset %d out of bounds for DW_AT_ranges attribute"),
10012 offset);
10013 return 0;
10014 }
10015 buffer = dwarf2_per_objfile->ranges.buffer + offset;
10016
10017 /* Read in the largest possible address. */
10018 marker = read_address (obfd, buffer, cu, &dummy);
10019 if ((marker & mask) == mask)
10020 {
10021 /* If we found the largest possible address, then
10022 read the base address. */
10023 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10024 buffer += 2 * addr_size;
10025 offset += 2 * addr_size;
10026 found_base = 1;
10027 }
10028
10029 low_set = 0;
10030
10031 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10032
10033 while (1)
10034 {
10035 CORE_ADDR range_beginning, range_end;
10036
10037 range_beginning = read_address (obfd, buffer, cu, &dummy);
10038 buffer += addr_size;
10039 range_end = read_address (obfd, buffer, cu, &dummy);
10040 buffer += addr_size;
10041 offset += 2 * addr_size;
10042
10043 /* An end of list marker is a pair of zero addresses. */
10044 if (range_beginning == 0 && range_end == 0)
10045 /* Found the end of list entry. */
10046 break;
10047
10048 /* Each base address selection entry is a pair of 2 values.
10049 The first is the largest possible address, the second is
10050 the base address. Check for a base address here. */
10051 if ((range_beginning & mask) == mask)
10052 {
10053 /* If we found the largest possible address, then
10054 read the base address. */
10055 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10056 found_base = 1;
10057 continue;
10058 }
10059
10060 if (!found_base)
10061 {
10062 /* We have no valid base address for the ranges
10063 data. */
10064 complaint (&symfile_complaints,
10065 _("Invalid .debug_ranges data (no base address)"));
10066 return 0;
10067 }
10068
10069 if (range_beginning > range_end)
10070 {
10071 /* Inverted range entries are invalid. */
10072 complaint (&symfile_complaints,
10073 _("Invalid .debug_ranges data (inverted range)"));
10074 return 0;
10075 }
10076
10077 /* Empty range entries have no effect. */
10078 if (range_beginning == range_end)
10079 continue;
10080
10081 range_beginning += base;
10082 range_end += base;
10083
10084 /* A not-uncommon case of bad debug info.
10085 Don't pollute the addrmap with bad data. */
10086 if (range_beginning + baseaddr == 0
10087 && !dwarf2_per_objfile->has_section_at_zero)
10088 {
10089 complaint (&symfile_complaints,
10090 _(".debug_ranges entry has start address of zero"
10091 " [in module %s]"), objfile->name);
10092 continue;
10093 }
10094
10095 if (ranges_pst != NULL)
10096 addrmap_set_empty (objfile->psymtabs_addrmap,
10097 range_beginning + baseaddr,
10098 range_end - 1 + baseaddr,
10099 ranges_pst);
10100
10101 /* FIXME: This is recording everything as a low-high
10102 segment of consecutive addresses. We should have a
10103 data structure for discontiguous block ranges
10104 instead. */
10105 if (! low_set)
10106 {
10107 low = range_beginning;
10108 high = range_end;
10109 low_set = 1;
10110 }
10111 else
10112 {
10113 if (range_beginning < low)
10114 low = range_beginning;
10115 if (range_end > high)
10116 high = range_end;
10117 }
10118 }
10119
10120 if (! low_set)
10121 /* If the first entry is an end-of-list marker, the range
10122 describes an empty scope, i.e. no instructions. */
10123 return 0;
10124
10125 if (low_return)
10126 *low_return = low;
10127 if (high_return)
10128 *high_return = high;
10129 return 1;
10130 }
10131
10132 /* Get low and high pc attributes from a die. Return 1 if the attributes
10133 are present and valid, otherwise, return 0. Return -1 if the range is
10134 discontinuous, i.e. derived from DW_AT_ranges information. */
10135
10136 static int
10137 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
10138 CORE_ADDR *highpc, struct dwarf2_cu *cu,
10139 struct partial_symtab *pst)
10140 {
10141 struct attribute *attr;
10142 struct attribute *attr_high;
10143 CORE_ADDR low = 0;
10144 CORE_ADDR high = 0;
10145 int ret = 0;
10146
10147 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10148 if (attr_high)
10149 {
10150 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10151 if (attr)
10152 {
10153 low = DW_ADDR (attr);
10154 if (attr_high->form == DW_FORM_addr
10155 || attr_high->form == DW_FORM_GNU_addr_index)
10156 high = DW_ADDR (attr_high);
10157 else
10158 high = low + DW_UNSND (attr_high);
10159 }
10160 else
10161 /* Found high w/o low attribute. */
10162 return 0;
10163
10164 /* Found consecutive range of addresses. */
10165 ret = 1;
10166 }
10167 else
10168 {
10169 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10170 if (attr != NULL)
10171 {
10172 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10173 We take advantage of the fact that DW_AT_ranges does not appear
10174 in DW_TAG_compile_unit of DWO files. */
10175 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10176 unsigned int ranges_offset = (DW_UNSND (attr)
10177 + (need_ranges_base
10178 ? cu->ranges_base
10179 : 0));
10180
10181 /* Value of the DW_AT_ranges attribute is the offset in the
10182 .debug_ranges section. */
10183 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
10184 return 0;
10185 /* Found discontinuous range of addresses. */
10186 ret = -1;
10187 }
10188 }
10189
10190 /* read_partial_die has also the strict LOW < HIGH requirement. */
10191 if (high <= low)
10192 return 0;
10193
10194 /* When using the GNU linker, .gnu.linkonce. sections are used to
10195 eliminate duplicate copies of functions and vtables and such.
10196 The linker will arbitrarily choose one and discard the others.
10197 The AT_*_pc values for such functions refer to local labels in
10198 these sections. If the section from that file was discarded, the
10199 labels are not in the output, so the relocs get a value of 0.
10200 If this is a discarded function, mark the pc bounds as invalid,
10201 so that GDB will ignore it. */
10202 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
10203 return 0;
10204
10205 *lowpc = low;
10206 if (highpc)
10207 *highpc = high;
10208 return ret;
10209 }
10210
10211 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
10212 its low and high PC addresses. Do nothing if these addresses could not
10213 be determined. Otherwise, set LOWPC to the low address if it is smaller,
10214 and HIGHPC to the high address if greater than HIGHPC. */
10215
10216 static void
10217 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
10218 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10219 struct dwarf2_cu *cu)
10220 {
10221 CORE_ADDR low, high;
10222 struct die_info *child = die->child;
10223
10224 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
10225 {
10226 *lowpc = min (*lowpc, low);
10227 *highpc = max (*highpc, high);
10228 }
10229
10230 /* If the language does not allow nested subprograms (either inside
10231 subprograms or lexical blocks), we're done. */
10232 if (cu->language != language_ada)
10233 return;
10234
10235 /* Check all the children of the given DIE. If it contains nested
10236 subprograms, then check their pc bounds. Likewise, we need to
10237 check lexical blocks as well, as they may also contain subprogram
10238 definitions. */
10239 while (child && child->tag)
10240 {
10241 if (child->tag == DW_TAG_subprogram
10242 || child->tag == DW_TAG_lexical_block)
10243 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
10244 child = sibling_die (child);
10245 }
10246 }
10247
10248 /* Get the low and high pc's represented by the scope DIE, and store
10249 them in *LOWPC and *HIGHPC. If the correct values can't be
10250 determined, set *LOWPC to -1 and *HIGHPC to 0. */
10251
10252 static void
10253 get_scope_pc_bounds (struct die_info *die,
10254 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10255 struct dwarf2_cu *cu)
10256 {
10257 CORE_ADDR best_low = (CORE_ADDR) -1;
10258 CORE_ADDR best_high = (CORE_ADDR) 0;
10259 CORE_ADDR current_low, current_high;
10260
10261 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
10262 {
10263 best_low = current_low;
10264 best_high = current_high;
10265 }
10266 else
10267 {
10268 struct die_info *child = die->child;
10269
10270 while (child && child->tag)
10271 {
10272 switch (child->tag) {
10273 case DW_TAG_subprogram:
10274 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
10275 break;
10276 case DW_TAG_namespace:
10277 case DW_TAG_module:
10278 /* FIXME: carlton/2004-01-16: Should we do this for
10279 DW_TAG_class_type/DW_TAG_structure_type, too? I think
10280 that current GCC's always emit the DIEs corresponding
10281 to definitions of methods of classes as children of a
10282 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
10283 the DIEs giving the declarations, which could be
10284 anywhere). But I don't see any reason why the
10285 standards says that they have to be there. */
10286 get_scope_pc_bounds (child, &current_low, &current_high, cu);
10287
10288 if (current_low != ((CORE_ADDR) -1))
10289 {
10290 best_low = min (best_low, current_low);
10291 best_high = max (best_high, current_high);
10292 }
10293 break;
10294 default:
10295 /* Ignore. */
10296 break;
10297 }
10298
10299 child = sibling_die (child);
10300 }
10301 }
10302
10303 *lowpc = best_low;
10304 *highpc = best_high;
10305 }
10306
10307 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
10308 in DIE. */
10309
10310 static void
10311 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
10312 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
10313 {
10314 struct objfile *objfile = cu->objfile;
10315 struct attribute *attr;
10316 struct attribute *attr_high;
10317
10318 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10319 if (attr_high)
10320 {
10321 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10322 if (attr)
10323 {
10324 CORE_ADDR low = DW_ADDR (attr);
10325 CORE_ADDR high;
10326 if (attr_high->form == DW_FORM_addr
10327 || attr_high->form == DW_FORM_GNU_addr_index)
10328 high = DW_ADDR (attr_high);
10329 else
10330 high = low + DW_UNSND (attr_high);
10331
10332 record_block_range (block, baseaddr + low, baseaddr + high - 1);
10333 }
10334 }
10335
10336 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10337 if (attr)
10338 {
10339 bfd *obfd = objfile->obfd;
10340 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10341 We take advantage of the fact that DW_AT_ranges does not appear
10342 in DW_TAG_compile_unit of DWO files. */
10343 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10344
10345 /* The value of the DW_AT_ranges attribute is the offset of the
10346 address range list in the .debug_ranges section. */
10347 unsigned long offset = (DW_UNSND (attr)
10348 + (need_ranges_base ? cu->ranges_base : 0));
10349 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
10350
10351 /* For some target architectures, but not others, the
10352 read_address function sign-extends the addresses it returns.
10353 To recognize base address selection entries, we need a
10354 mask. */
10355 unsigned int addr_size = cu->header.addr_size;
10356 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10357
10358 /* The base address, to which the next pair is relative. Note
10359 that this 'base' is a DWARF concept: most entries in a range
10360 list are relative, to reduce the number of relocs against the
10361 debugging information. This is separate from this function's
10362 'baseaddr' argument, which GDB uses to relocate debugging
10363 information from a shared library based on the address at
10364 which the library was loaded. */
10365 CORE_ADDR base = cu->base_address;
10366 int base_known = cu->base_known;
10367
10368 gdb_assert (dwarf2_per_objfile->ranges.readin);
10369 if (offset >= dwarf2_per_objfile->ranges.size)
10370 {
10371 complaint (&symfile_complaints,
10372 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
10373 offset);
10374 return;
10375 }
10376
10377 for (;;)
10378 {
10379 unsigned int bytes_read;
10380 CORE_ADDR start, end;
10381
10382 start = read_address (obfd, buffer, cu, &bytes_read);
10383 buffer += bytes_read;
10384 end = read_address (obfd, buffer, cu, &bytes_read);
10385 buffer += bytes_read;
10386
10387 /* Did we find the end of the range list? */
10388 if (start == 0 && end == 0)
10389 break;
10390
10391 /* Did we find a base address selection entry? */
10392 else if ((start & base_select_mask) == base_select_mask)
10393 {
10394 base = end;
10395 base_known = 1;
10396 }
10397
10398 /* We found an ordinary address range. */
10399 else
10400 {
10401 if (!base_known)
10402 {
10403 complaint (&symfile_complaints,
10404 _("Invalid .debug_ranges data "
10405 "(no base address)"));
10406 return;
10407 }
10408
10409 if (start > end)
10410 {
10411 /* Inverted range entries are invalid. */
10412 complaint (&symfile_complaints,
10413 _("Invalid .debug_ranges data "
10414 "(inverted range)"));
10415 return;
10416 }
10417
10418 /* Empty range entries have no effect. */
10419 if (start == end)
10420 continue;
10421
10422 start += base + baseaddr;
10423 end += base + baseaddr;
10424
10425 /* A not-uncommon case of bad debug info.
10426 Don't pollute the addrmap with bad data. */
10427 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
10428 {
10429 complaint (&symfile_complaints,
10430 _(".debug_ranges entry has start address of zero"
10431 " [in module %s]"), objfile->name);
10432 continue;
10433 }
10434
10435 record_block_range (block, start, end - 1);
10436 }
10437 }
10438 }
10439 }
10440
10441 /* Check whether the producer field indicates either of GCC < 4.6, or the
10442 Intel C/C++ compiler, and cache the result in CU. */
10443
10444 static void
10445 check_producer (struct dwarf2_cu *cu)
10446 {
10447 const char *cs;
10448 int major, minor, release;
10449
10450 if (cu->producer == NULL)
10451 {
10452 /* For unknown compilers expect their behavior is DWARF version
10453 compliant.
10454
10455 GCC started to support .debug_types sections by -gdwarf-4 since
10456 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
10457 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
10458 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
10459 interpreted incorrectly by GDB now - GCC PR debug/48229. */
10460 }
10461 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
10462 {
10463 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
10464
10465 cs = &cu->producer[strlen ("GNU ")];
10466 while (*cs && !isdigit (*cs))
10467 cs++;
10468 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
10469 {
10470 /* Not recognized as GCC. */
10471 }
10472 else
10473 {
10474 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
10475 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
10476 }
10477 }
10478 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
10479 cu->producer_is_icc = 1;
10480 else
10481 {
10482 /* For other non-GCC compilers, expect their behavior is DWARF version
10483 compliant. */
10484 }
10485
10486 cu->checked_producer = 1;
10487 }
10488
10489 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
10490 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
10491 during 4.6.0 experimental. */
10492
10493 static int
10494 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
10495 {
10496 if (!cu->checked_producer)
10497 check_producer (cu);
10498
10499 return cu->producer_is_gxx_lt_4_6;
10500 }
10501
10502 /* Return the default accessibility type if it is not overriden by
10503 DW_AT_accessibility. */
10504
10505 static enum dwarf_access_attribute
10506 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
10507 {
10508 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
10509 {
10510 /* The default DWARF 2 accessibility for members is public, the default
10511 accessibility for inheritance is private. */
10512
10513 if (die->tag != DW_TAG_inheritance)
10514 return DW_ACCESS_public;
10515 else
10516 return DW_ACCESS_private;
10517 }
10518 else
10519 {
10520 /* DWARF 3+ defines the default accessibility a different way. The same
10521 rules apply now for DW_TAG_inheritance as for the members and it only
10522 depends on the container kind. */
10523
10524 if (die->parent->tag == DW_TAG_class_type)
10525 return DW_ACCESS_private;
10526 else
10527 return DW_ACCESS_public;
10528 }
10529 }
10530
10531 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
10532 offset. If the attribute was not found return 0, otherwise return
10533 1. If it was found but could not properly be handled, set *OFFSET
10534 to 0. */
10535
10536 static int
10537 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
10538 LONGEST *offset)
10539 {
10540 struct attribute *attr;
10541
10542 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
10543 if (attr != NULL)
10544 {
10545 *offset = 0;
10546
10547 /* Note that we do not check for a section offset first here.
10548 This is because DW_AT_data_member_location is new in DWARF 4,
10549 so if we see it, we can assume that a constant form is really
10550 a constant and not a section offset. */
10551 if (attr_form_is_constant (attr))
10552 *offset = dwarf2_get_attr_constant_value (attr, 0);
10553 else if (attr_form_is_section_offset (attr))
10554 dwarf2_complex_location_expr_complaint ();
10555 else if (attr_form_is_block (attr))
10556 *offset = decode_locdesc (DW_BLOCK (attr), cu);
10557 else
10558 dwarf2_complex_location_expr_complaint ();
10559
10560 return 1;
10561 }
10562
10563 return 0;
10564 }
10565
10566 /* Add an aggregate field to the field list. */
10567
10568 static void
10569 dwarf2_add_field (struct field_info *fip, struct die_info *die,
10570 struct dwarf2_cu *cu)
10571 {
10572 struct objfile *objfile = cu->objfile;
10573 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10574 struct nextfield *new_field;
10575 struct attribute *attr;
10576 struct field *fp;
10577 const char *fieldname = "";
10578
10579 /* Allocate a new field list entry and link it in. */
10580 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
10581 make_cleanup (xfree, new_field);
10582 memset (new_field, 0, sizeof (struct nextfield));
10583
10584 if (die->tag == DW_TAG_inheritance)
10585 {
10586 new_field->next = fip->baseclasses;
10587 fip->baseclasses = new_field;
10588 }
10589 else
10590 {
10591 new_field->next = fip->fields;
10592 fip->fields = new_field;
10593 }
10594 fip->nfields++;
10595
10596 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
10597 if (attr)
10598 new_field->accessibility = DW_UNSND (attr);
10599 else
10600 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
10601 if (new_field->accessibility != DW_ACCESS_public)
10602 fip->non_public_fields = 1;
10603
10604 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
10605 if (attr)
10606 new_field->virtuality = DW_UNSND (attr);
10607 else
10608 new_field->virtuality = DW_VIRTUALITY_none;
10609
10610 fp = &new_field->field;
10611
10612 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
10613 {
10614 LONGEST offset;
10615
10616 /* Data member other than a C++ static data member. */
10617
10618 /* Get type of field. */
10619 fp->type = die_type (die, cu);
10620
10621 SET_FIELD_BITPOS (*fp, 0);
10622
10623 /* Get bit size of field (zero if none). */
10624 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
10625 if (attr)
10626 {
10627 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
10628 }
10629 else
10630 {
10631 FIELD_BITSIZE (*fp) = 0;
10632 }
10633
10634 /* Get bit offset of field. */
10635 if (handle_data_member_location (die, cu, &offset))
10636 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
10637 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
10638 if (attr)
10639 {
10640 if (gdbarch_bits_big_endian (gdbarch))
10641 {
10642 /* For big endian bits, the DW_AT_bit_offset gives the
10643 additional bit offset from the MSB of the containing
10644 anonymous object to the MSB of the field. We don't
10645 have to do anything special since we don't need to
10646 know the size of the anonymous object. */
10647 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
10648 }
10649 else
10650 {
10651 /* For little endian bits, compute the bit offset to the
10652 MSB of the anonymous object, subtract off the number of
10653 bits from the MSB of the field to the MSB of the
10654 object, and then subtract off the number of bits of
10655 the field itself. The result is the bit offset of
10656 the LSB of the field. */
10657 int anonymous_size;
10658 int bit_offset = DW_UNSND (attr);
10659
10660 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10661 if (attr)
10662 {
10663 /* The size of the anonymous object containing
10664 the bit field is explicit, so use the
10665 indicated size (in bytes). */
10666 anonymous_size = DW_UNSND (attr);
10667 }
10668 else
10669 {
10670 /* The size of the anonymous object containing
10671 the bit field must be inferred from the type
10672 attribute of the data member containing the
10673 bit field. */
10674 anonymous_size = TYPE_LENGTH (fp->type);
10675 }
10676 SET_FIELD_BITPOS (*fp,
10677 (FIELD_BITPOS (*fp)
10678 + anonymous_size * bits_per_byte
10679 - bit_offset - FIELD_BITSIZE (*fp)));
10680 }
10681 }
10682
10683 /* Get name of field. */
10684 fieldname = dwarf2_name (die, cu);
10685 if (fieldname == NULL)
10686 fieldname = "";
10687
10688 /* The name is already allocated along with this objfile, so we don't
10689 need to duplicate it for the type. */
10690 fp->name = fieldname;
10691
10692 /* Change accessibility for artificial fields (e.g. virtual table
10693 pointer or virtual base class pointer) to private. */
10694 if (dwarf2_attr (die, DW_AT_artificial, cu))
10695 {
10696 FIELD_ARTIFICIAL (*fp) = 1;
10697 new_field->accessibility = DW_ACCESS_private;
10698 fip->non_public_fields = 1;
10699 }
10700 }
10701 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
10702 {
10703 /* C++ static member. */
10704
10705 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
10706 is a declaration, but all versions of G++ as of this writing
10707 (so through at least 3.2.1) incorrectly generate
10708 DW_TAG_variable tags. */
10709
10710 const char *physname;
10711
10712 /* Get name of field. */
10713 fieldname = dwarf2_name (die, cu);
10714 if (fieldname == NULL)
10715 return;
10716
10717 attr = dwarf2_attr (die, DW_AT_const_value, cu);
10718 if (attr
10719 /* Only create a symbol if this is an external value.
10720 new_symbol checks this and puts the value in the global symbol
10721 table, which we want. If it is not external, new_symbol
10722 will try to put the value in cu->list_in_scope which is wrong. */
10723 && dwarf2_flag_true_p (die, DW_AT_external, cu))
10724 {
10725 /* A static const member, not much different than an enum as far as
10726 we're concerned, except that we can support more types. */
10727 new_symbol (die, NULL, cu);
10728 }
10729
10730 /* Get physical name. */
10731 physname = dwarf2_physname (fieldname, die, cu);
10732
10733 /* The name is already allocated along with this objfile, so we don't
10734 need to duplicate it for the type. */
10735 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
10736 FIELD_TYPE (*fp) = die_type (die, cu);
10737 FIELD_NAME (*fp) = fieldname;
10738 }
10739 else if (die->tag == DW_TAG_inheritance)
10740 {
10741 LONGEST offset;
10742
10743 /* C++ base class field. */
10744 if (handle_data_member_location (die, cu, &offset))
10745 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
10746 FIELD_BITSIZE (*fp) = 0;
10747 FIELD_TYPE (*fp) = die_type (die, cu);
10748 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
10749 fip->nbaseclasses++;
10750 }
10751 }
10752
10753 /* Add a typedef defined in the scope of the FIP's class. */
10754
10755 static void
10756 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
10757 struct dwarf2_cu *cu)
10758 {
10759 struct objfile *objfile = cu->objfile;
10760 struct typedef_field_list *new_field;
10761 struct attribute *attr;
10762 struct typedef_field *fp;
10763 char *fieldname = "";
10764
10765 /* Allocate a new field list entry and link it in. */
10766 new_field = xzalloc (sizeof (*new_field));
10767 make_cleanup (xfree, new_field);
10768
10769 gdb_assert (die->tag == DW_TAG_typedef);
10770
10771 fp = &new_field->field;
10772
10773 /* Get name of field. */
10774 fp->name = dwarf2_name (die, cu);
10775 if (fp->name == NULL)
10776 return;
10777
10778 fp->type = read_type_die (die, cu);
10779
10780 new_field->next = fip->typedef_field_list;
10781 fip->typedef_field_list = new_field;
10782 fip->typedef_field_list_count++;
10783 }
10784
10785 /* Create the vector of fields, and attach it to the type. */
10786
10787 static void
10788 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
10789 struct dwarf2_cu *cu)
10790 {
10791 int nfields = fip->nfields;
10792
10793 /* Record the field count, allocate space for the array of fields,
10794 and create blank accessibility bitfields if necessary. */
10795 TYPE_NFIELDS (type) = nfields;
10796 TYPE_FIELDS (type) = (struct field *)
10797 TYPE_ALLOC (type, sizeof (struct field) * nfields);
10798 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
10799
10800 if (fip->non_public_fields && cu->language != language_ada)
10801 {
10802 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10803
10804 TYPE_FIELD_PRIVATE_BITS (type) =
10805 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10806 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
10807
10808 TYPE_FIELD_PROTECTED_BITS (type) =
10809 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10810 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
10811
10812 TYPE_FIELD_IGNORE_BITS (type) =
10813 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10814 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
10815 }
10816
10817 /* If the type has baseclasses, allocate and clear a bit vector for
10818 TYPE_FIELD_VIRTUAL_BITS. */
10819 if (fip->nbaseclasses && cu->language != language_ada)
10820 {
10821 int num_bytes = B_BYTES (fip->nbaseclasses);
10822 unsigned char *pointer;
10823
10824 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10825 pointer = TYPE_ALLOC (type, num_bytes);
10826 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
10827 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
10828 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
10829 }
10830
10831 /* Copy the saved-up fields into the field vector. Start from the head of
10832 the list, adding to the tail of the field array, so that they end up in
10833 the same order in the array in which they were added to the list. */
10834 while (nfields-- > 0)
10835 {
10836 struct nextfield *fieldp;
10837
10838 if (fip->fields)
10839 {
10840 fieldp = fip->fields;
10841 fip->fields = fieldp->next;
10842 }
10843 else
10844 {
10845 fieldp = fip->baseclasses;
10846 fip->baseclasses = fieldp->next;
10847 }
10848
10849 TYPE_FIELD (type, nfields) = fieldp->field;
10850 switch (fieldp->accessibility)
10851 {
10852 case DW_ACCESS_private:
10853 if (cu->language != language_ada)
10854 SET_TYPE_FIELD_PRIVATE (type, nfields);
10855 break;
10856
10857 case DW_ACCESS_protected:
10858 if (cu->language != language_ada)
10859 SET_TYPE_FIELD_PROTECTED (type, nfields);
10860 break;
10861
10862 case DW_ACCESS_public:
10863 break;
10864
10865 default:
10866 /* Unknown accessibility. Complain and treat it as public. */
10867 {
10868 complaint (&symfile_complaints, _("unsupported accessibility %d"),
10869 fieldp->accessibility);
10870 }
10871 break;
10872 }
10873 if (nfields < fip->nbaseclasses)
10874 {
10875 switch (fieldp->virtuality)
10876 {
10877 case DW_VIRTUALITY_virtual:
10878 case DW_VIRTUALITY_pure_virtual:
10879 if (cu->language == language_ada)
10880 error (_("unexpected virtuality in component of Ada type"));
10881 SET_TYPE_FIELD_VIRTUAL (type, nfields);
10882 break;
10883 }
10884 }
10885 }
10886 }
10887
10888 /* Return true if this member function is a constructor, false
10889 otherwise. */
10890
10891 static int
10892 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
10893 {
10894 const char *fieldname;
10895 const char *typename;
10896 int len;
10897
10898 if (die->parent == NULL)
10899 return 0;
10900
10901 if (die->parent->tag != DW_TAG_structure_type
10902 && die->parent->tag != DW_TAG_union_type
10903 && die->parent->tag != DW_TAG_class_type)
10904 return 0;
10905
10906 fieldname = dwarf2_name (die, cu);
10907 typename = dwarf2_name (die->parent, cu);
10908 if (fieldname == NULL || typename == NULL)
10909 return 0;
10910
10911 len = strlen (fieldname);
10912 return (strncmp (fieldname, typename, len) == 0
10913 && (typename[len] == '\0' || typename[len] == '<'));
10914 }
10915
10916 /* Add a member function to the proper fieldlist. */
10917
10918 static void
10919 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
10920 struct type *type, struct dwarf2_cu *cu)
10921 {
10922 struct objfile *objfile = cu->objfile;
10923 struct attribute *attr;
10924 struct fnfieldlist *flp;
10925 int i;
10926 struct fn_field *fnp;
10927 const char *fieldname;
10928 struct nextfnfield *new_fnfield;
10929 struct type *this_type;
10930 enum dwarf_access_attribute accessibility;
10931
10932 if (cu->language == language_ada)
10933 error (_("unexpected member function in Ada type"));
10934
10935 /* Get name of member function. */
10936 fieldname = dwarf2_name (die, cu);
10937 if (fieldname == NULL)
10938 return;
10939
10940 /* Look up member function name in fieldlist. */
10941 for (i = 0; i < fip->nfnfields; i++)
10942 {
10943 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
10944 break;
10945 }
10946
10947 /* Create new list element if necessary. */
10948 if (i < fip->nfnfields)
10949 flp = &fip->fnfieldlists[i];
10950 else
10951 {
10952 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
10953 {
10954 fip->fnfieldlists = (struct fnfieldlist *)
10955 xrealloc (fip->fnfieldlists,
10956 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
10957 * sizeof (struct fnfieldlist));
10958 if (fip->nfnfields == 0)
10959 make_cleanup (free_current_contents, &fip->fnfieldlists);
10960 }
10961 flp = &fip->fnfieldlists[fip->nfnfields];
10962 flp->name = fieldname;
10963 flp->length = 0;
10964 flp->head = NULL;
10965 i = fip->nfnfields++;
10966 }
10967
10968 /* Create a new member function field and chain it to the field list
10969 entry. */
10970 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
10971 make_cleanup (xfree, new_fnfield);
10972 memset (new_fnfield, 0, sizeof (struct nextfnfield));
10973 new_fnfield->next = flp->head;
10974 flp->head = new_fnfield;
10975 flp->length++;
10976
10977 /* Fill in the member function field info. */
10978 fnp = &new_fnfield->fnfield;
10979
10980 /* Delay processing of the physname until later. */
10981 if (cu->language == language_cplus || cu->language == language_java)
10982 {
10983 add_to_method_list (type, i, flp->length - 1, fieldname,
10984 die, cu);
10985 }
10986 else
10987 {
10988 const char *physname = dwarf2_physname (fieldname, die, cu);
10989 fnp->physname = physname ? physname : "";
10990 }
10991
10992 fnp->type = alloc_type (objfile);
10993 this_type = read_type_die (die, cu);
10994 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
10995 {
10996 int nparams = TYPE_NFIELDS (this_type);
10997
10998 /* TYPE is the domain of this method, and THIS_TYPE is the type
10999 of the method itself (TYPE_CODE_METHOD). */
11000 smash_to_method_type (fnp->type, type,
11001 TYPE_TARGET_TYPE (this_type),
11002 TYPE_FIELDS (this_type),
11003 TYPE_NFIELDS (this_type),
11004 TYPE_VARARGS (this_type));
11005
11006 /* Handle static member functions.
11007 Dwarf2 has no clean way to discern C++ static and non-static
11008 member functions. G++ helps GDB by marking the first
11009 parameter for non-static member functions (which is the this
11010 pointer) as artificial. We obtain this information from
11011 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
11012 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
11013 fnp->voffset = VOFFSET_STATIC;
11014 }
11015 else
11016 complaint (&symfile_complaints, _("member function type missing for '%s'"),
11017 dwarf2_full_name (fieldname, die, cu));
11018
11019 /* Get fcontext from DW_AT_containing_type if present. */
11020 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
11021 fnp->fcontext = die_containing_type (die, cu);
11022
11023 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
11024 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
11025
11026 /* Get accessibility. */
11027 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
11028 if (attr)
11029 accessibility = DW_UNSND (attr);
11030 else
11031 accessibility = dwarf2_default_access_attribute (die, cu);
11032 switch (accessibility)
11033 {
11034 case DW_ACCESS_private:
11035 fnp->is_private = 1;
11036 break;
11037 case DW_ACCESS_protected:
11038 fnp->is_protected = 1;
11039 break;
11040 }
11041
11042 /* Check for artificial methods. */
11043 attr = dwarf2_attr (die, DW_AT_artificial, cu);
11044 if (attr && DW_UNSND (attr) != 0)
11045 fnp->is_artificial = 1;
11046
11047 fnp->is_constructor = dwarf2_is_constructor (die, cu);
11048
11049 /* Get index in virtual function table if it is a virtual member
11050 function. For older versions of GCC, this is an offset in the
11051 appropriate virtual table, as specified by DW_AT_containing_type.
11052 For everyone else, it is an expression to be evaluated relative
11053 to the object address. */
11054
11055 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
11056 if (attr)
11057 {
11058 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
11059 {
11060 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
11061 {
11062 /* Old-style GCC. */
11063 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
11064 }
11065 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
11066 || (DW_BLOCK (attr)->size > 1
11067 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
11068 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
11069 {
11070 struct dwarf_block blk;
11071 int offset;
11072
11073 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
11074 ? 1 : 2);
11075 blk.size = DW_BLOCK (attr)->size - offset;
11076 blk.data = DW_BLOCK (attr)->data + offset;
11077 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
11078 if ((fnp->voffset % cu->header.addr_size) != 0)
11079 dwarf2_complex_location_expr_complaint ();
11080 else
11081 fnp->voffset /= cu->header.addr_size;
11082 fnp->voffset += 2;
11083 }
11084 else
11085 dwarf2_complex_location_expr_complaint ();
11086
11087 if (!fnp->fcontext)
11088 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
11089 }
11090 else if (attr_form_is_section_offset (attr))
11091 {
11092 dwarf2_complex_location_expr_complaint ();
11093 }
11094 else
11095 {
11096 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
11097 fieldname);
11098 }
11099 }
11100 else
11101 {
11102 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11103 if (attr && DW_UNSND (attr))
11104 {
11105 /* GCC does this, as of 2008-08-25; PR debug/37237. */
11106 complaint (&symfile_complaints,
11107 _("Member function \"%s\" (offset %d) is virtual "
11108 "but the vtable offset is not specified"),
11109 fieldname, die->offset.sect_off);
11110 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11111 TYPE_CPLUS_DYNAMIC (type) = 1;
11112 }
11113 }
11114 }
11115
11116 /* Create the vector of member function fields, and attach it to the type. */
11117
11118 static void
11119 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
11120 struct dwarf2_cu *cu)
11121 {
11122 struct fnfieldlist *flp;
11123 int i;
11124
11125 if (cu->language == language_ada)
11126 error (_("unexpected member functions in Ada type"));
11127
11128 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11129 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
11130 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
11131
11132 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
11133 {
11134 struct nextfnfield *nfp = flp->head;
11135 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
11136 int k;
11137
11138 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
11139 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
11140 fn_flp->fn_fields = (struct fn_field *)
11141 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
11142 for (k = flp->length; (k--, nfp); nfp = nfp->next)
11143 fn_flp->fn_fields[k] = nfp->fnfield;
11144 }
11145
11146 TYPE_NFN_FIELDS (type) = fip->nfnfields;
11147 }
11148
11149 /* Returns non-zero if NAME is the name of a vtable member in CU's
11150 language, zero otherwise. */
11151 static int
11152 is_vtable_name (const char *name, struct dwarf2_cu *cu)
11153 {
11154 static const char vptr[] = "_vptr";
11155 static const char vtable[] = "vtable";
11156
11157 /* Look for the C++ and Java forms of the vtable. */
11158 if ((cu->language == language_java
11159 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
11160 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
11161 && is_cplus_marker (name[sizeof (vptr) - 1])))
11162 return 1;
11163
11164 return 0;
11165 }
11166
11167 /* GCC outputs unnamed structures that are really pointers to member
11168 functions, with the ABI-specified layout. If TYPE describes
11169 such a structure, smash it into a member function type.
11170
11171 GCC shouldn't do this; it should just output pointer to member DIEs.
11172 This is GCC PR debug/28767. */
11173
11174 static void
11175 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
11176 {
11177 struct type *pfn_type, *domain_type, *new_type;
11178
11179 /* Check for a structure with no name and two children. */
11180 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
11181 return;
11182
11183 /* Check for __pfn and __delta members. */
11184 if (TYPE_FIELD_NAME (type, 0) == NULL
11185 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
11186 || TYPE_FIELD_NAME (type, 1) == NULL
11187 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
11188 return;
11189
11190 /* Find the type of the method. */
11191 pfn_type = TYPE_FIELD_TYPE (type, 0);
11192 if (pfn_type == NULL
11193 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
11194 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
11195 return;
11196
11197 /* Look for the "this" argument. */
11198 pfn_type = TYPE_TARGET_TYPE (pfn_type);
11199 if (TYPE_NFIELDS (pfn_type) == 0
11200 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
11201 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
11202 return;
11203
11204 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
11205 new_type = alloc_type (objfile);
11206 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
11207 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
11208 TYPE_VARARGS (pfn_type));
11209 smash_to_methodptr_type (type, new_type);
11210 }
11211
11212 /* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
11213 (icc). */
11214
11215 static int
11216 producer_is_icc (struct dwarf2_cu *cu)
11217 {
11218 if (!cu->checked_producer)
11219 check_producer (cu);
11220
11221 return cu->producer_is_icc;
11222 }
11223
11224 /* Called when we find the DIE that starts a structure or union scope
11225 (definition) to create a type for the structure or union. Fill in
11226 the type's name and general properties; the members will not be
11227 processed until process_structure_type.
11228
11229 NOTE: we need to call these functions regardless of whether or not the
11230 DIE has a DW_AT_name attribute, since it might be an anonymous
11231 structure or union. This gets the type entered into our set of
11232 user defined types.
11233
11234 However, if the structure is incomplete (an opaque struct/union)
11235 then suppress creating a symbol table entry for it since gdb only
11236 wants to find the one with the complete definition. Note that if
11237 it is complete, we just call new_symbol, which does it's own
11238 checking about whether the struct/union is anonymous or not (and
11239 suppresses creating a symbol table entry itself). */
11240
11241 static struct type *
11242 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
11243 {
11244 struct objfile *objfile = cu->objfile;
11245 struct type *type;
11246 struct attribute *attr;
11247 const char *name;
11248
11249 /* If the definition of this type lives in .debug_types, read that type.
11250 Don't follow DW_AT_specification though, that will take us back up
11251 the chain and we want to go down. */
11252 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
11253 if (attr)
11254 {
11255 struct dwarf2_cu *type_cu = cu;
11256 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11257
11258 /* We could just recurse on read_structure_type, but we need to call
11259 get_die_type to ensure only one type for this DIE is created.
11260 This is important, for example, because for c++ classes we need
11261 TYPE_NAME set which is only done by new_symbol. Blech. */
11262 type = read_type_die (type_die, type_cu);
11263
11264 /* TYPE_CU may not be the same as CU.
11265 Ensure TYPE is recorded in CU's type_hash table. */
11266 return set_die_type (die, type, cu);
11267 }
11268
11269 type = alloc_type (objfile);
11270 INIT_CPLUS_SPECIFIC (type);
11271
11272 name = dwarf2_name (die, cu);
11273 if (name != NULL)
11274 {
11275 if (cu->language == language_cplus
11276 || cu->language == language_java)
11277 {
11278 const char *full_name = dwarf2_full_name (name, die, cu);
11279
11280 /* dwarf2_full_name might have already finished building the DIE's
11281 type. If so, there is no need to continue. */
11282 if (get_die_type (die, cu) != NULL)
11283 return get_die_type (die, cu);
11284
11285 TYPE_TAG_NAME (type) = full_name;
11286 if (die->tag == DW_TAG_structure_type
11287 || die->tag == DW_TAG_class_type)
11288 TYPE_NAME (type) = TYPE_TAG_NAME (type);
11289 }
11290 else
11291 {
11292 /* The name is already allocated along with this objfile, so
11293 we don't need to duplicate it for the type. */
11294 TYPE_TAG_NAME (type) = name;
11295 if (die->tag == DW_TAG_class_type)
11296 TYPE_NAME (type) = TYPE_TAG_NAME (type);
11297 }
11298 }
11299
11300 if (die->tag == DW_TAG_structure_type)
11301 {
11302 TYPE_CODE (type) = TYPE_CODE_STRUCT;
11303 }
11304 else if (die->tag == DW_TAG_union_type)
11305 {
11306 TYPE_CODE (type) = TYPE_CODE_UNION;
11307 }
11308 else
11309 {
11310 TYPE_CODE (type) = TYPE_CODE_CLASS;
11311 }
11312
11313 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
11314 TYPE_DECLARED_CLASS (type) = 1;
11315
11316 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11317 if (attr)
11318 {
11319 TYPE_LENGTH (type) = DW_UNSND (attr);
11320 }
11321 else
11322 {
11323 TYPE_LENGTH (type) = 0;
11324 }
11325
11326 if (producer_is_icc (cu))
11327 {
11328 /* ICC does not output the required DW_AT_declaration
11329 on incomplete types, but gives them a size of zero. */
11330 }
11331 else
11332 TYPE_STUB_SUPPORTED (type) = 1;
11333
11334 if (die_is_declaration (die, cu))
11335 TYPE_STUB (type) = 1;
11336 else if (attr == NULL && die->child == NULL
11337 && producer_is_realview (cu->producer))
11338 /* RealView does not output the required DW_AT_declaration
11339 on incomplete types. */
11340 TYPE_STUB (type) = 1;
11341
11342 /* We need to add the type field to the die immediately so we don't
11343 infinitely recurse when dealing with pointers to the structure
11344 type within the structure itself. */
11345 set_die_type (die, type, cu);
11346
11347 /* set_die_type should be already done. */
11348 set_descriptive_type (type, die, cu);
11349
11350 return type;
11351 }
11352
11353 /* Finish creating a structure or union type, including filling in
11354 its members and creating a symbol for it. */
11355
11356 static void
11357 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
11358 {
11359 struct objfile *objfile = cu->objfile;
11360 struct die_info *child_die = die->child;
11361 struct type *type;
11362
11363 type = get_die_type (die, cu);
11364 if (type == NULL)
11365 type = read_structure_type (die, cu);
11366
11367 if (die->child != NULL && ! die_is_declaration (die, cu))
11368 {
11369 struct field_info fi;
11370 struct die_info *child_die;
11371 VEC (symbolp) *template_args = NULL;
11372 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
11373
11374 memset (&fi, 0, sizeof (struct field_info));
11375
11376 child_die = die->child;
11377
11378 while (child_die && child_die->tag)
11379 {
11380 if (child_die->tag == DW_TAG_member
11381 || child_die->tag == DW_TAG_variable)
11382 {
11383 /* NOTE: carlton/2002-11-05: A C++ static data member
11384 should be a DW_TAG_member that is a declaration, but
11385 all versions of G++ as of this writing (so through at
11386 least 3.2.1) incorrectly generate DW_TAG_variable
11387 tags for them instead. */
11388 dwarf2_add_field (&fi, child_die, cu);
11389 }
11390 else if (child_die->tag == DW_TAG_subprogram)
11391 {
11392 /* C++ member function. */
11393 dwarf2_add_member_fn (&fi, child_die, type, cu);
11394 }
11395 else if (child_die->tag == DW_TAG_inheritance)
11396 {
11397 /* C++ base class field. */
11398 dwarf2_add_field (&fi, child_die, cu);
11399 }
11400 else if (child_die->tag == DW_TAG_typedef)
11401 dwarf2_add_typedef (&fi, child_die, cu);
11402 else if (child_die->tag == DW_TAG_template_type_param
11403 || child_die->tag == DW_TAG_template_value_param)
11404 {
11405 struct symbol *arg = new_symbol (child_die, NULL, cu);
11406
11407 if (arg != NULL)
11408 VEC_safe_push (symbolp, template_args, arg);
11409 }
11410
11411 child_die = sibling_die (child_die);
11412 }
11413
11414 /* Attach template arguments to type. */
11415 if (! VEC_empty (symbolp, template_args))
11416 {
11417 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11418 TYPE_N_TEMPLATE_ARGUMENTS (type)
11419 = VEC_length (symbolp, template_args);
11420 TYPE_TEMPLATE_ARGUMENTS (type)
11421 = obstack_alloc (&objfile->objfile_obstack,
11422 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11423 * sizeof (struct symbol *)));
11424 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
11425 VEC_address (symbolp, template_args),
11426 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11427 * sizeof (struct symbol *)));
11428 VEC_free (symbolp, template_args);
11429 }
11430
11431 /* Attach fields and member functions to the type. */
11432 if (fi.nfields)
11433 dwarf2_attach_fields_to_type (&fi, type, cu);
11434 if (fi.nfnfields)
11435 {
11436 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
11437
11438 /* Get the type which refers to the base class (possibly this
11439 class itself) which contains the vtable pointer for the current
11440 class from the DW_AT_containing_type attribute. This use of
11441 DW_AT_containing_type is a GNU extension. */
11442
11443 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
11444 {
11445 struct type *t = die_containing_type (die, cu);
11446
11447 TYPE_VPTR_BASETYPE (type) = t;
11448 if (type == t)
11449 {
11450 int i;
11451
11452 /* Our own class provides vtbl ptr. */
11453 for (i = TYPE_NFIELDS (t) - 1;
11454 i >= TYPE_N_BASECLASSES (t);
11455 --i)
11456 {
11457 const char *fieldname = TYPE_FIELD_NAME (t, i);
11458
11459 if (is_vtable_name (fieldname, cu))
11460 {
11461 TYPE_VPTR_FIELDNO (type) = i;
11462 break;
11463 }
11464 }
11465
11466 /* Complain if virtual function table field not found. */
11467 if (i < TYPE_N_BASECLASSES (t))
11468 complaint (&symfile_complaints,
11469 _("virtual function table pointer "
11470 "not found when defining class '%s'"),
11471 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
11472 "");
11473 }
11474 else
11475 {
11476 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
11477 }
11478 }
11479 else if (cu->producer
11480 && strncmp (cu->producer,
11481 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
11482 {
11483 /* The IBM XLC compiler does not provide direct indication
11484 of the containing type, but the vtable pointer is
11485 always named __vfp. */
11486
11487 int i;
11488
11489 for (i = TYPE_NFIELDS (type) - 1;
11490 i >= TYPE_N_BASECLASSES (type);
11491 --i)
11492 {
11493 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
11494 {
11495 TYPE_VPTR_FIELDNO (type) = i;
11496 TYPE_VPTR_BASETYPE (type) = type;
11497 break;
11498 }
11499 }
11500 }
11501 }
11502
11503 /* Copy fi.typedef_field_list linked list elements content into the
11504 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
11505 if (fi.typedef_field_list)
11506 {
11507 int i = fi.typedef_field_list_count;
11508
11509 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11510 TYPE_TYPEDEF_FIELD_ARRAY (type)
11511 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
11512 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
11513
11514 /* Reverse the list order to keep the debug info elements order. */
11515 while (--i >= 0)
11516 {
11517 struct typedef_field *dest, *src;
11518
11519 dest = &TYPE_TYPEDEF_FIELD (type, i);
11520 src = &fi.typedef_field_list->field;
11521 fi.typedef_field_list = fi.typedef_field_list->next;
11522 *dest = *src;
11523 }
11524 }
11525
11526 do_cleanups (back_to);
11527
11528 if (HAVE_CPLUS_STRUCT (type))
11529 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
11530 }
11531
11532 quirk_gcc_member_function_pointer (type, objfile);
11533
11534 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
11535 snapshots) has been known to create a die giving a declaration
11536 for a class that has, as a child, a die giving a definition for a
11537 nested class. So we have to process our children even if the
11538 current die is a declaration. Normally, of course, a declaration
11539 won't have any children at all. */
11540
11541 while (child_die != NULL && child_die->tag)
11542 {
11543 if (child_die->tag == DW_TAG_member
11544 || child_die->tag == DW_TAG_variable
11545 || child_die->tag == DW_TAG_inheritance
11546 || child_die->tag == DW_TAG_template_value_param
11547 || child_die->tag == DW_TAG_template_type_param)
11548 {
11549 /* Do nothing. */
11550 }
11551 else
11552 process_die (child_die, cu);
11553
11554 child_die = sibling_die (child_die);
11555 }
11556
11557 /* Do not consider external references. According to the DWARF standard,
11558 these DIEs are identified by the fact that they have no byte_size
11559 attribute, and a declaration attribute. */
11560 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
11561 || !die_is_declaration (die, cu))
11562 new_symbol (die, type, cu);
11563 }
11564
11565 /* Given a DW_AT_enumeration_type die, set its type. We do not
11566 complete the type's fields yet, or create any symbols. */
11567
11568 static struct type *
11569 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
11570 {
11571 struct objfile *objfile = cu->objfile;
11572 struct type *type;
11573 struct attribute *attr;
11574 const char *name;
11575
11576 /* If the definition of this type lives in .debug_types, read that type.
11577 Don't follow DW_AT_specification though, that will take us back up
11578 the chain and we want to go down. */
11579 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
11580 if (attr)
11581 {
11582 struct dwarf2_cu *type_cu = cu;
11583 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11584
11585 type = read_type_die (type_die, type_cu);
11586
11587 /* TYPE_CU may not be the same as CU.
11588 Ensure TYPE is recorded in CU's type_hash table. */
11589 return set_die_type (die, type, cu);
11590 }
11591
11592 type = alloc_type (objfile);
11593
11594 TYPE_CODE (type) = TYPE_CODE_ENUM;
11595 name = dwarf2_full_name (NULL, die, cu);
11596 if (name != NULL)
11597 TYPE_TAG_NAME (type) = name;
11598
11599 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11600 if (attr)
11601 {
11602 TYPE_LENGTH (type) = DW_UNSND (attr);
11603 }
11604 else
11605 {
11606 TYPE_LENGTH (type) = 0;
11607 }
11608
11609 /* The enumeration DIE can be incomplete. In Ada, any type can be
11610 declared as private in the package spec, and then defined only
11611 inside the package body. Such types are known as Taft Amendment
11612 Types. When another package uses such a type, an incomplete DIE
11613 may be generated by the compiler. */
11614 if (die_is_declaration (die, cu))
11615 TYPE_STUB (type) = 1;
11616
11617 return set_die_type (die, type, cu);
11618 }
11619
11620 /* Given a pointer to a die which begins an enumeration, process all
11621 the dies that define the members of the enumeration, and create the
11622 symbol for the enumeration type.
11623
11624 NOTE: We reverse the order of the element list. */
11625
11626 static void
11627 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
11628 {
11629 struct type *this_type;
11630
11631 this_type = get_die_type (die, cu);
11632 if (this_type == NULL)
11633 this_type = read_enumeration_type (die, cu);
11634
11635 if (die->child != NULL)
11636 {
11637 struct die_info *child_die;
11638 struct symbol *sym;
11639 struct field *fields = NULL;
11640 int num_fields = 0;
11641 int unsigned_enum = 1;
11642 const char *name;
11643 int flag_enum = 1;
11644 ULONGEST mask = 0;
11645
11646 child_die = die->child;
11647 while (child_die && child_die->tag)
11648 {
11649 if (child_die->tag != DW_TAG_enumerator)
11650 {
11651 process_die (child_die, cu);
11652 }
11653 else
11654 {
11655 name = dwarf2_name (child_die, cu);
11656 if (name)
11657 {
11658 sym = new_symbol (child_die, this_type, cu);
11659 if (SYMBOL_VALUE (sym) < 0)
11660 {
11661 unsigned_enum = 0;
11662 flag_enum = 0;
11663 }
11664 else if ((mask & SYMBOL_VALUE (sym)) != 0)
11665 flag_enum = 0;
11666 else
11667 mask |= SYMBOL_VALUE (sym);
11668
11669 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
11670 {
11671 fields = (struct field *)
11672 xrealloc (fields,
11673 (num_fields + DW_FIELD_ALLOC_CHUNK)
11674 * sizeof (struct field));
11675 }
11676
11677 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
11678 FIELD_TYPE (fields[num_fields]) = NULL;
11679 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
11680 FIELD_BITSIZE (fields[num_fields]) = 0;
11681
11682 num_fields++;
11683 }
11684 }
11685
11686 child_die = sibling_die (child_die);
11687 }
11688
11689 if (num_fields)
11690 {
11691 TYPE_NFIELDS (this_type) = num_fields;
11692 TYPE_FIELDS (this_type) = (struct field *)
11693 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
11694 memcpy (TYPE_FIELDS (this_type), fields,
11695 sizeof (struct field) * num_fields);
11696 xfree (fields);
11697 }
11698 if (unsigned_enum)
11699 TYPE_UNSIGNED (this_type) = 1;
11700 if (flag_enum)
11701 TYPE_FLAG_ENUM (this_type) = 1;
11702 }
11703
11704 /* If we are reading an enum from a .debug_types unit, and the enum
11705 is a declaration, and the enum is not the signatured type in the
11706 unit, then we do not want to add a symbol for it. Adding a
11707 symbol would in some cases obscure the true definition of the
11708 enum, giving users an incomplete type when the definition is
11709 actually available. Note that we do not want to do this for all
11710 enums which are just declarations, because C++0x allows forward
11711 enum declarations. */
11712 if (cu->per_cu->is_debug_types
11713 && die_is_declaration (die, cu))
11714 {
11715 struct signatured_type *sig_type;
11716
11717 sig_type
11718 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
11719 cu->per_cu->info_or_types_section,
11720 cu->per_cu->offset);
11721 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
11722 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
11723 return;
11724 }
11725
11726 new_symbol (die, this_type, cu);
11727 }
11728
11729 /* Extract all information from a DW_TAG_array_type DIE and put it in
11730 the DIE's type field. For now, this only handles one dimensional
11731 arrays. */
11732
11733 static struct type *
11734 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
11735 {
11736 struct objfile *objfile = cu->objfile;
11737 struct die_info *child_die;
11738 struct type *type;
11739 struct type *element_type, *range_type, *index_type;
11740 struct type **range_types = NULL;
11741 struct attribute *attr;
11742 int ndim = 0;
11743 struct cleanup *back_to;
11744 const char *name;
11745
11746 element_type = die_type (die, cu);
11747
11748 /* The die_type call above may have already set the type for this DIE. */
11749 type = get_die_type (die, cu);
11750 if (type)
11751 return type;
11752
11753 /* Irix 6.2 native cc creates array types without children for
11754 arrays with unspecified length. */
11755 if (die->child == NULL)
11756 {
11757 index_type = objfile_type (objfile)->builtin_int;
11758 range_type = create_range_type (NULL, index_type, 0, -1);
11759 type = create_array_type (NULL, element_type, range_type);
11760 return set_die_type (die, type, cu);
11761 }
11762
11763 back_to = make_cleanup (null_cleanup, NULL);
11764 child_die = die->child;
11765 while (child_die && child_die->tag)
11766 {
11767 if (child_die->tag == DW_TAG_subrange_type)
11768 {
11769 struct type *child_type = read_type_die (child_die, cu);
11770
11771 if (child_type != NULL)
11772 {
11773 /* The range type was succesfully read. Save it for the
11774 array type creation. */
11775 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
11776 {
11777 range_types = (struct type **)
11778 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
11779 * sizeof (struct type *));
11780 if (ndim == 0)
11781 make_cleanup (free_current_contents, &range_types);
11782 }
11783 range_types[ndim++] = child_type;
11784 }
11785 }
11786 child_die = sibling_die (child_die);
11787 }
11788
11789 /* Dwarf2 dimensions are output from left to right, create the
11790 necessary array types in backwards order. */
11791
11792 type = element_type;
11793
11794 if (read_array_order (die, cu) == DW_ORD_col_major)
11795 {
11796 int i = 0;
11797
11798 while (i < ndim)
11799 type = create_array_type (NULL, type, range_types[i++]);
11800 }
11801 else
11802 {
11803 while (ndim-- > 0)
11804 type = create_array_type (NULL, type, range_types[ndim]);
11805 }
11806
11807 /* Understand Dwarf2 support for vector types (like they occur on
11808 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
11809 array type. This is not part of the Dwarf2/3 standard yet, but a
11810 custom vendor extension. The main difference between a regular
11811 array and the vector variant is that vectors are passed by value
11812 to functions. */
11813 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
11814 if (attr)
11815 make_vector_type (type);
11816
11817 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
11818 implementation may choose to implement triple vectors using this
11819 attribute. */
11820 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11821 if (attr)
11822 {
11823 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
11824 TYPE_LENGTH (type) = DW_UNSND (attr);
11825 else
11826 complaint (&symfile_complaints,
11827 _("DW_AT_byte_size for array type smaller "
11828 "than the total size of elements"));
11829 }
11830
11831 name = dwarf2_name (die, cu);
11832 if (name)
11833 TYPE_NAME (type) = name;
11834
11835 /* Install the type in the die. */
11836 set_die_type (die, type, cu);
11837
11838 /* set_die_type should be already done. */
11839 set_descriptive_type (type, die, cu);
11840
11841 do_cleanups (back_to);
11842
11843 return type;
11844 }
11845
11846 static enum dwarf_array_dim_ordering
11847 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
11848 {
11849 struct attribute *attr;
11850
11851 attr = dwarf2_attr (die, DW_AT_ordering, cu);
11852
11853 if (attr) return DW_SND (attr);
11854
11855 /* GNU F77 is a special case, as at 08/2004 array type info is the
11856 opposite order to the dwarf2 specification, but data is still
11857 laid out as per normal fortran.
11858
11859 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
11860 version checking. */
11861
11862 if (cu->language == language_fortran
11863 && cu->producer && strstr (cu->producer, "GNU F77"))
11864 {
11865 return DW_ORD_row_major;
11866 }
11867
11868 switch (cu->language_defn->la_array_ordering)
11869 {
11870 case array_column_major:
11871 return DW_ORD_col_major;
11872 case array_row_major:
11873 default:
11874 return DW_ORD_row_major;
11875 };
11876 }
11877
11878 /* Extract all information from a DW_TAG_set_type DIE and put it in
11879 the DIE's type field. */
11880
11881 static struct type *
11882 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
11883 {
11884 struct type *domain_type, *set_type;
11885 struct attribute *attr;
11886
11887 domain_type = die_type (die, cu);
11888
11889 /* The die_type call above may have already set the type for this DIE. */
11890 set_type = get_die_type (die, cu);
11891 if (set_type)
11892 return set_type;
11893
11894 set_type = create_set_type (NULL, domain_type);
11895
11896 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11897 if (attr)
11898 TYPE_LENGTH (set_type) = DW_UNSND (attr);
11899
11900 return set_die_type (die, set_type, cu);
11901 }
11902
11903 /* A helper for read_common_block that creates a locexpr baton.
11904 SYM is the symbol which we are marking as computed.
11905 COMMON_DIE is the DIE for the common block.
11906 COMMON_LOC is the location expression attribute for the common
11907 block itself.
11908 MEMBER_LOC is the location expression attribute for the particular
11909 member of the common block that we are processing.
11910 CU is the CU from which the above come. */
11911
11912 static void
11913 mark_common_block_symbol_computed (struct symbol *sym,
11914 struct die_info *common_die,
11915 struct attribute *common_loc,
11916 struct attribute *member_loc,
11917 struct dwarf2_cu *cu)
11918 {
11919 struct objfile *objfile = dwarf2_per_objfile->objfile;
11920 struct dwarf2_locexpr_baton *baton;
11921 gdb_byte *ptr;
11922 unsigned int cu_off;
11923 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
11924 LONGEST offset = 0;
11925
11926 gdb_assert (common_loc && member_loc);
11927 gdb_assert (attr_form_is_block (common_loc));
11928 gdb_assert (attr_form_is_block (member_loc)
11929 || attr_form_is_constant (member_loc));
11930
11931 baton = obstack_alloc (&objfile->objfile_obstack,
11932 sizeof (struct dwarf2_locexpr_baton));
11933 baton->per_cu = cu->per_cu;
11934 gdb_assert (baton->per_cu);
11935
11936 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
11937
11938 if (attr_form_is_constant (member_loc))
11939 {
11940 offset = dwarf2_get_attr_constant_value (member_loc, 0);
11941 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
11942 }
11943 else
11944 baton->size += DW_BLOCK (member_loc)->size;
11945
11946 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
11947 baton->data = ptr;
11948
11949 *ptr++ = DW_OP_call4;
11950 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
11951 store_unsigned_integer (ptr, 4, byte_order, cu_off);
11952 ptr += 4;
11953
11954 if (attr_form_is_constant (member_loc))
11955 {
11956 *ptr++ = DW_OP_addr;
11957 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
11958 ptr += cu->header.addr_size;
11959 }
11960 else
11961 {
11962 /* We have to copy the data here, because DW_OP_call4 will only
11963 use a DW_AT_location attribute. */
11964 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
11965 ptr += DW_BLOCK (member_loc)->size;
11966 }
11967
11968 *ptr++ = DW_OP_plus;
11969 gdb_assert (ptr - baton->data == baton->size);
11970
11971 SYMBOL_LOCATION_BATON (sym) = baton;
11972 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
11973 }
11974
11975 /* Create appropriate locally-scoped variables for all the
11976 DW_TAG_common_block entries. Also create a struct common_block
11977 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
11978 is used to sepate the common blocks name namespace from regular
11979 variable names. */
11980
11981 static void
11982 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
11983 {
11984 struct attribute *attr;
11985
11986 attr = dwarf2_attr (die, DW_AT_location, cu);
11987 if (attr)
11988 {
11989 /* Support the .debug_loc offsets. */
11990 if (attr_form_is_block (attr))
11991 {
11992 /* Ok. */
11993 }
11994 else if (attr_form_is_section_offset (attr))
11995 {
11996 dwarf2_complex_location_expr_complaint ();
11997 attr = NULL;
11998 }
11999 else
12000 {
12001 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
12002 "common block member");
12003 attr = NULL;
12004 }
12005 }
12006
12007 if (die->child != NULL)
12008 {
12009 struct objfile *objfile = cu->objfile;
12010 struct die_info *child_die;
12011 size_t n_entries = 0, size;
12012 struct common_block *common_block;
12013 struct symbol *sym;
12014
12015 for (child_die = die->child;
12016 child_die && child_die->tag;
12017 child_die = sibling_die (child_die))
12018 ++n_entries;
12019
12020 size = (sizeof (struct common_block)
12021 + (n_entries - 1) * sizeof (struct symbol *));
12022 common_block = obstack_alloc (&objfile->objfile_obstack, size);
12023 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
12024 common_block->n_entries = 0;
12025
12026 for (child_die = die->child;
12027 child_die && child_die->tag;
12028 child_die = sibling_die (child_die))
12029 {
12030 /* Create the symbol in the DW_TAG_common_block block in the current
12031 symbol scope. */
12032 sym = new_symbol (child_die, NULL, cu);
12033 if (sym != NULL)
12034 {
12035 struct attribute *member_loc;
12036
12037 common_block->contents[common_block->n_entries++] = sym;
12038
12039 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
12040 cu);
12041 if (member_loc)
12042 {
12043 /* GDB has handled this for a long time, but it is
12044 not specified by DWARF. It seems to have been
12045 emitted by gfortran at least as recently as:
12046 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
12047 complaint (&symfile_complaints,
12048 _("Variable in common block has "
12049 "DW_AT_data_member_location "
12050 "- DIE at 0x%x [in module %s]"),
12051 child_die->offset.sect_off, cu->objfile->name);
12052
12053 if (attr_form_is_section_offset (member_loc))
12054 dwarf2_complex_location_expr_complaint ();
12055 else if (attr_form_is_constant (member_loc)
12056 || attr_form_is_block (member_loc))
12057 {
12058 if (attr)
12059 mark_common_block_symbol_computed (sym, die, attr,
12060 member_loc, cu);
12061 }
12062 else
12063 dwarf2_complex_location_expr_complaint ();
12064 }
12065 }
12066 }
12067
12068 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
12069 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
12070 }
12071 }
12072
12073 /* Create a type for a C++ namespace. */
12074
12075 static struct type *
12076 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
12077 {
12078 struct objfile *objfile = cu->objfile;
12079 const char *previous_prefix, *name;
12080 int is_anonymous;
12081 struct type *type;
12082
12083 /* For extensions, reuse the type of the original namespace. */
12084 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
12085 {
12086 struct die_info *ext_die;
12087 struct dwarf2_cu *ext_cu = cu;
12088
12089 ext_die = dwarf2_extension (die, &ext_cu);
12090 type = read_type_die (ext_die, ext_cu);
12091
12092 /* EXT_CU may not be the same as CU.
12093 Ensure TYPE is recorded in CU's type_hash table. */
12094 return set_die_type (die, type, cu);
12095 }
12096
12097 name = namespace_name (die, &is_anonymous, cu);
12098
12099 /* Now build the name of the current namespace. */
12100
12101 previous_prefix = determine_prefix (die, cu);
12102 if (previous_prefix[0] != '\0')
12103 name = typename_concat (&objfile->objfile_obstack,
12104 previous_prefix, name, 0, cu);
12105
12106 /* Create the type. */
12107 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
12108 objfile);
12109 TYPE_NAME (type) = name;
12110 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12111
12112 return set_die_type (die, type, cu);
12113 }
12114
12115 /* Read a C++ namespace. */
12116
12117 static void
12118 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
12119 {
12120 struct objfile *objfile = cu->objfile;
12121 int is_anonymous;
12122
12123 /* Add a symbol associated to this if we haven't seen the namespace
12124 before. Also, add a using directive if it's an anonymous
12125 namespace. */
12126
12127 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
12128 {
12129 struct type *type;
12130
12131 type = read_type_die (die, cu);
12132 new_symbol (die, type, cu);
12133
12134 namespace_name (die, &is_anonymous, cu);
12135 if (is_anonymous)
12136 {
12137 const char *previous_prefix = determine_prefix (die, cu);
12138
12139 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
12140 NULL, NULL, 0, &objfile->objfile_obstack);
12141 }
12142 }
12143
12144 if (die->child != NULL)
12145 {
12146 struct die_info *child_die = die->child;
12147
12148 while (child_die && child_die->tag)
12149 {
12150 process_die (child_die, cu);
12151 child_die = sibling_die (child_die);
12152 }
12153 }
12154 }
12155
12156 /* Read a Fortran module as type. This DIE can be only a declaration used for
12157 imported module. Still we need that type as local Fortran "use ... only"
12158 declaration imports depend on the created type in determine_prefix. */
12159
12160 static struct type *
12161 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
12162 {
12163 struct objfile *objfile = cu->objfile;
12164 const char *module_name;
12165 struct type *type;
12166
12167 module_name = dwarf2_name (die, cu);
12168 if (!module_name)
12169 complaint (&symfile_complaints,
12170 _("DW_TAG_module has no name, offset 0x%x"),
12171 die->offset.sect_off);
12172 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
12173
12174 /* determine_prefix uses TYPE_TAG_NAME. */
12175 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12176
12177 return set_die_type (die, type, cu);
12178 }
12179
12180 /* Read a Fortran module. */
12181
12182 static void
12183 read_module (struct die_info *die, struct dwarf2_cu *cu)
12184 {
12185 struct die_info *child_die = die->child;
12186
12187 while (child_die && child_die->tag)
12188 {
12189 process_die (child_die, cu);
12190 child_die = sibling_die (child_die);
12191 }
12192 }
12193
12194 /* Return the name of the namespace represented by DIE. Set
12195 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
12196 namespace. */
12197
12198 static const char *
12199 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
12200 {
12201 struct die_info *current_die;
12202 const char *name = NULL;
12203
12204 /* Loop through the extensions until we find a name. */
12205
12206 for (current_die = die;
12207 current_die != NULL;
12208 current_die = dwarf2_extension (die, &cu))
12209 {
12210 name = dwarf2_name (current_die, cu);
12211 if (name != NULL)
12212 break;
12213 }
12214
12215 /* Is it an anonymous namespace? */
12216
12217 *is_anonymous = (name == NULL);
12218 if (*is_anonymous)
12219 name = CP_ANONYMOUS_NAMESPACE_STR;
12220
12221 return name;
12222 }
12223
12224 /* Extract all information from a DW_TAG_pointer_type DIE and add to
12225 the user defined type vector. */
12226
12227 static struct type *
12228 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
12229 {
12230 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
12231 struct comp_unit_head *cu_header = &cu->header;
12232 struct type *type;
12233 struct attribute *attr_byte_size;
12234 struct attribute *attr_address_class;
12235 int byte_size, addr_class;
12236 struct type *target_type;
12237
12238 target_type = die_type (die, cu);
12239
12240 /* The die_type call above may have already set the type for this DIE. */
12241 type = get_die_type (die, cu);
12242 if (type)
12243 return type;
12244
12245 type = lookup_pointer_type (target_type);
12246
12247 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
12248 if (attr_byte_size)
12249 byte_size = DW_UNSND (attr_byte_size);
12250 else
12251 byte_size = cu_header->addr_size;
12252
12253 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
12254 if (attr_address_class)
12255 addr_class = DW_UNSND (attr_address_class);
12256 else
12257 addr_class = DW_ADDR_none;
12258
12259 /* If the pointer size or address class is different than the
12260 default, create a type variant marked as such and set the
12261 length accordingly. */
12262 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
12263 {
12264 if (gdbarch_address_class_type_flags_p (gdbarch))
12265 {
12266 int type_flags;
12267
12268 type_flags = gdbarch_address_class_type_flags
12269 (gdbarch, byte_size, addr_class);
12270 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
12271 == 0);
12272 type = make_type_with_address_space (type, type_flags);
12273 }
12274 else if (TYPE_LENGTH (type) != byte_size)
12275 {
12276 complaint (&symfile_complaints,
12277 _("invalid pointer size %d"), byte_size);
12278 }
12279 else
12280 {
12281 /* Should we also complain about unhandled address classes? */
12282 }
12283 }
12284
12285 TYPE_LENGTH (type) = byte_size;
12286 return set_die_type (die, type, cu);
12287 }
12288
12289 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
12290 the user defined type vector. */
12291
12292 static struct type *
12293 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
12294 {
12295 struct type *type;
12296 struct type *to_type;
12297 struct type *domain;
12298
12299 to_type = die_type (die, cu);
12300 domain = die_containing_type (die, cu);
12301
12302 /* The calls above may have already set the type for this DIE. */
12303 type = get_die_type (die, cu);
12304 if (type)
12305 return type;
12306
12307 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
12308 type = lookup_methodptr_type (to_type);
12309 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
12310 {
12311 struct type *new_type = alloc_type (cu->objfile);
12312
12313 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
12314 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
12315 TYPE_VARARGS (to_type));
12316 type = lookup_methodptr_type (new_type);
12317 }
12318 else
12319 type = lookup_memberptr_type (to_type, domain);
12320
12321 return set_die_type (die, type, cu);
12322 }
12323
12324 /* Extract all information from a DW_TAG_reference_type DIE and add to
12325 the user defined type vector. */
12326
12327 static struct type *
12328 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
12329 {
12330 struct comp_unit_head *cu_header = &cu->header;
12331 struct type *type, *target_type;
12332 struct attribute *attr;
12333
12334 target_type = die_type (die, cu);
12335
12336 /* The die_type call above may have already set the type for this DIE. */
12337 type = get_die_type (die, cu);
12338 if (type)
12339 return type;
12340
12341 type = lookup_reference_type (target_type);
12342 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12343 if (attr)
12344 {
12345 TYPE_LENGTH (type) = DW_UNSND (attr);
12346 }
12347 else
12348 {
12349 TYPE_LENGTH (type) = cu_header->addr_size;
12350 }
12351 return set_die_type (die, type, cu);
12352 }
12353
12354 static struct type *
12355 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
12356 {
12357 struct type *base_type, *cv_type;
12358
12359 base_type = die_type (die, cu);
12360
12361 /* The die_type call above may have already set the type for this DIE. */
12362 cv_type = get_die_type (die, cu);
12363 if (cv_type)
12364 return cv_type;
12365
12366 /* In case the const qualifier is applied to an array type, the element type
12367 is so qualified, not the array type (section 6.7.3 of C99). */
12368 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
12369 {
12370 struct type *el_type, *inner_array;
12371
12372 base_type = copy_type (base_type);
12373 inner_array = base_type;
12374
12375 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
12376 {
12377 TYPE_TARGET_TYPE (inner_array) =
12378 copy_type (TYPE_TARGET_TYPE (inner_array));
12379 inner_array = TYPE_TARGET_TYPE (inner_array);
12380 }
12381
12382 el_type = TYPE_TARGET_TYPE (inner_array);
12383 TYPE_TARGET_TYPE (inner_array) =
12384 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
12385
12386 return set_die_type (die, base_type, cu);
12387 }
12388
12389 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
12390 return set_die_type (die, cv_type, cu);
12391 }
12392
12393 static struct type *
12394 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
12395 {
12396 struct type *base_type, *cv_type;
12397
12398 base_type = die_type (die, cu);
12399
12400 /* The die_type call above may have already set the type for this DIE. */
12401 cv_type = get_die_type (die, cu);
12402 if (cv_type)
12403 return cv_type;
12404
12405 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
12406 return set_die_type (die, cv_type, cu);
12407 }
12408
12409 /* Handle DW_TAG_restrict_type. */
12410
12411 static struct type *
12412 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
12413 {
12414 struct type *base_type, *cv_type;
12415
12416 base_type = die_type (die, cu);
12417
12418 /* The die_type call above may have already set the type for this DIE. */
12419 cv_type = get_die_type (die, cu);
12420 if (cv_type)
12421 return cv_type;
12422
12423 cv_type = make_restrict_type (base_type);
12424 return set_die_type (die, cv_type, cu);
12425 }
12426
12427 /* Extract all information from a DW_TAG_string_type DIE and add to
12428 the user defined type vector. It isn't really a user defined type,
12429 but it behaves like one, with other DIE's using an AT_user_def_type
12430 attribute to reference it. */
12431
12432 static struct type *
12433 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
12434 {
12435 struct objfile *objfile = cu->objfile;
12436 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12437 struct type *type, *range_type, *index_type, *char_type;
12438 struct attribute *attr;
12439 unsigned int length;
12440
12441 attr = dwarf2_attr (die, DW_AT_string_length, cu);
12442 if (attr)
12443 {
12444 length = DW_UNSND (attr);
12445 }
12446 else
12447 {
12448 /* Check for the DW_AT_byte_size attribute. */
12449 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12450 if (attr)
12451 {
12452 length = DW_UNSND (attr);
12453 }
12454 else
12455 {
12456 length = 1;
12457 }
12458 }
12459
12460 index_type = objfile_type (objfile)->builtin_int;
12461 range_type = create_range_type (NULL, index_type, 1, length);
12462 char_type = language_string_char_type (cu->language_defn, gdbarch);
12463 type = create_string_type (NULL, char_type, range_type);
12464
12465 return set_die_type (die, type, cu);
12466 }
12467
12468 /* Handle DIES due to C code like:
12469
12470 struct foo
12471 {
12472 int (*funcp)(int a, long l);
12473 int b;
12474 };
12475
12476 ('funcp' generates a DW_TAG_subroutine_type DIE). */
12477
12478 static struct type *
12479 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
12480 {
12481 struct objfile *objfile = cu->objfile;
12482 struct type *type; /* Type that this function returns. */
12483 struct type *ftype; /* Function that returns above type. */
12484 struct attribute *attr;
12485
12486 type = die_type (die, cu);
12487
12488 /* The die_type call above may have already set the type for this DIE. */
12489 ftype = get_die_type (die, cu);
12490 if (ftype)
12491 return ftype;
12492
12493 ftype = lookup_function_type (type);
12494
12495 /* All functions in C++, Pascal and Java have prototypes. */
12496 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
12497 if ((attr && (DW_UNSND (attr) != 0))
12498 || cu->language == language_cplus
12499 || cu->language == language_java
12500 || cu->language == language_pascal)
12501 TYPE_PROTOTYPED (ftype) = 1;
12502 else if (producer_is_realview (cu->producer))
12503 /* RealView does not emit DW_AT_prototyped. We can not
12504 distinguish prototyped and unprototyped functions; default to
12505 prototyped, since that is more common in modern code (and
12506 RealView warns about unprototyped functions). */
12507 TYPE_PROTOTYPED (ftype) = 1;
12508
12509 /* Store the calling convention in the type if it's available in
12510 the subroutine die. Otherwise set the calling convention to
12511 the default value DW_CC_normal. */
12512 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
12513 if (attr)
12514 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
12515 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
12516 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
12517 else
12518 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
12519
12520 /* We need to add the subroutine type to the die immediately so
12521 we don't infinitely recurse when dealing with parameters
12522 declared as the same subroutine type. */
12523 set_die_type (die, ftype, cu);
12524
12525 if (die->child != NULL)
12526 {
12527 struct type *void_type = objfile_type (objfile)->builtin_void;
12528 struct die_info *child_die;
12529 int nparams, iparams;
12530
12531 /* Count the number of parameters.
12532 FIXME: GDB currently ignores vararg functions, but knows about
12533 vararg member functions. */
12534 nparams = 0;
12535 child_die = die->child;
12536 while (child_die && child_die->tag)
12537 {
12538 if (child_die->tag == DW_TAG_formal_parameter)
12539 nparams++;
12540 else if (child_die->tag == DW_TAG_unspecified_parameters)
12541 TYPE_VARARGS (ftype) = 1;
12542 child_die = sibling_die (child_die);
12543 }
12544
12545 /* Allocate storage for parameters and fill them in. */
12546 TYPE_NFIELDS (ftype) = nparams;
12547 TYPE_FIELDS (ftype) = (struct field *)
12548 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
12549
12550 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
12551 even if we error out during the parameters reading below. */
12552 for (iparams = 0; iparams < nparams; iparams++)
12553 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
12554
12555 iparams = 0;
12556 child_die = die->child;
12557 while (child_die && child_die->tag)
12558 {
12559 if (child_die->tag == DW_TAG_formal_parameter)
12560 {
12561 struct type *arg_type;
12562
12563 /* DWARF version 2 has no clean way to discern C++
12564 static and non-static member functions. G++ helps
12565 GDB by marking the first parameter for non-static
12566 member functions (which is the this pointer) as
12567 artificial. We pass this information to
12568 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
12569
12570 DWARF version 3 added DW_AT_object_pointer, which GCC
12571 4.5 does not yet generate. */
12572 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
12573 if (attr)
12574 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
12575 else
12576 {
12577 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
12578
12579 /* GCC/43521: In java, the formal parameter
12580 "this" is sometimes not marked with DW_AT_artificial. */
12581 if (cu->language == language_java)
12582 {
12583 const char *name = dwarf2_name (child_die, cu);
12584
12585 if (name && !strcmp (name, "this"))
12586 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
12587 }
12588 }
12589 arg_type = die_type (child_die, cu);
12590
12591 /* RealView does not mark THIS as const, which the testsuite
12592 expects. GCC marks THIS as const in method definitions,
12593 but not in the class specifications (GCC PR 43053). */
12594 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
12595 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
12596 {
12597 int is_this = 0;
12598 struct dwarf2_cu *arg_cu = cu;
12599 const char *name = dwarf2_name (child_die, cu);
12600
12601 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
12602 if (attr)
12603 {
12604 /* If the compiler emits this, use it. */
12605 if (follow_die_ref (die, attr, &arg_cu) == child_die)
12606 is_this = 1;
12607 }
12608 else if (name && strcmp (name, "this") == 0)
12609 /* Function definitions will have the argument names. */
12610 is_this = 1;
12611 else if (name == NULL && iparams == 0)
12612 /* Declarations may not have the names, so like
12613 elsewhere in GDB, assume an artificial first
12614 argument is "this". */
12615 is_this = 1;
12616
12617 if (is_this)
12618 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
12619 arg_type, 0);
12620 }
12621
12622 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
12623 iparams++;
12624 }
12625 child_die = sibling_die (child_die);
12626 }
12627 }
12628
12629 return ftype;
12630 }
12631
12632 static struct type *
12633 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
12634 {
12635 struct objfile *objfile = cu->objfile;
12636 const char *name = NULL;
12637 struct type *this_type, *target_type;
12638
12639 name = dwarf2_full_name (NULL, die, cu);
12640 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
12641 TYPE_FLAG_TARGET_STUB, NULL, objfile);
12642 TYPE_NAME (this_type) = name;
12643 set_die_type (die, this_type, cu);
12644 target_type = die_type (die, cu);
12645 if (target_type != this_type)
12646 TYPE_TARGET_TYPE (this_type) = target_type;
12647 else
12648 {
12649 /* Self-referential typedefs are, it seems, not allowed by the DWARF
12650 spec and cause infinite loops in GDB. */
12651 complaint (&symfile_complaints,
12652 _("Self-referential DW_TAG_typedef "
12653 "- DIE at 0x%x [in module %s]"),
12654 die->offset.sect_off, objfile->name);
12655 TYPE_TARGET_TYPE (this_type) = NULL;
12656 }
12657 return this_type;
12658 }
12659
12660 /* Find a representation of a given base type and install
12661 it in the TYPE field of the die. */
12662
12663 static struct type *
12664 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
12665 {
12666 struct objfile *objfile = cu->objfile;
12667 struct type *type;
12668 struct attribute *attr;
12669 int encoding = 0, size = 0;
12670 const char *name;
12671 enum type_code code = TYPE_CODE_INT;
12672 int type_flags = 0;
12673 struct type *target_type = NULL;
12674
12675 attr = dwarf2_attr (die, DW_AT_encoding, cu);
12676 if (attr)
12677 {
12678 encoding = DW_UNSND (attr);
12679 }
12680 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12681 if (attr)
12682 {
12683 size = DW_UNSND (attr);
12684 }
12685 name = dwarf2_name (die, cu);
12686 if (!name)
12687 {
12688 complaint (&symfile_complaints,
12689 _("DW_AT_name missing from DW_TAG_base_type"));
12690 }
12691
12692 switch (encoding)
12693 {
12694 case DW_ATE_address:
12695 /* Turn DW_ATE_address into a void * pointer. */
12696 code = TYPE_CODE_PTR;
12697 type_flags |= TYPE_FLAG_UNSIGNED;
12698 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
12699 break;
12700 case DW_ATE_boolean:
12701 code = TYPE_CODE_BOOL;
12702 type_flags |= TYPE_FLAG_UNSIGNED;
12703 break;
12704 case DW_ATE_complex_float:
12705 code = TYPE_CODE_COMPLEX;
12706 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
12707 break;
12708 case DW_ATE_decimal_float:
12709 code = TYPE_CODE_DECFLOAT;
12710 break;
12711 case DW_ATE_float:
12712 code = TYPE_CODE_FLT;
12713 break;
12714 case DW_ATE_signed:
12715 break;
12716 case DW_ATE_unsigned:
12717 type_flags |= TYPE_FLAG_UNSIGNED;
12718 if (cu->language == language_fortran
12719 && name
12720 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
12721 code = TYPE_CODE_CHAR;
12722 break;
12723 case DW_ATE_signed_char:
12724 if (cu->language == language_ada || cu->language == language_m2
12725 || cu->language == language_pascal
12726 || cu->language == language_fortran)
12727 code = TYPE_CODE_CHAR;
12728 break;
12729 case DW_ATE_unsigned_char:
12730 if (cu->language == language_ada || cu->language == language_m2
12731 || cu->language == language_pascal
12732 || cu->language == language_fortran)
12733 code = TYPE_CODE_CHAR;
12734 type_flags |= TYPE_FLAG_UNSIGNED;
12735 break;
12736 case DW_ATE_UTF:
12737 /* We just treat this as an integer and then recognize the
12738 type by name elsewhere. */
12739 break;
12740
12741 default:
12742 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
12743 dwarf_type_encoding_name (encoding));
12744 break;
12745 }
12746
12747 type = init_type (code, size, type_flags, NULL, objfile);
12748 TYPE_NAME (type) = name;
12749 TYPE_TARGET_TYPE (type) = target_type;
12750
12751 if (name && strcmp (name, "char") == 0)
12752 TYPE_NOSIGN (type) = 1;
12753
12754 return set_die_type (die, type, cu);
12755 }
12756
12757 /* Read the given DW_AT_subrange DIE. */
12758
12759 static struct type *
12760 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
12761 {
12762 struct type *base_type, *orig_base_type;
12763 struct type *range_type;
12764 struct attribute *attr;
12765 LONGEST low, high;
12766 int low_default_is_valid;
12767 const char *name;
12768 LONGEST negative_mask;
12769
12770 orig_base_type = die_type (die, cu);
12771 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
12772 whereas the real type might be. So, we use ORIG_BASE_TYPE when
12773 creating the range type, but we use the result of check_typedef
12774 when examining properties of the type. */
12775 base_type = check_typedef (orig_base_type);
12776
12777 /* The die_type call above may have already set the type for this DIE. */
12778 range_type = get_die_type (die, cu);
12779 if (range_type)
12780 return range_type;
12781
12782 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
12783 omitting DW_AT_lower_bound. */
12784 switch (cu->language)
12785 {
12786 case language_c:
12787 case language_cplus:
12788 low = 0;
12789 low_default_is_valid = 1;
12790 break;
12791 case language_fortran:
12792 low = 1;
12793 low_default_is_valid = 1;
12794 break;
12795 case language_d:
12796 case language_java:
12797 case language_objc:
12798 low = 0;
12799 low_default_is_valid = (cu->header.version >= 4);
12800 break;
12801 case language_ada:
12802 case language_m2:
12803 case language_pascal:
12804 low = 1;
12805 low_default_is_valid = (cu->header.version >= 4);
12806 break;
12807 default:
12808 low = 0;
12809 low_default_is_valid = 0;
12810 break;
12811 }
12812
12813 /* FIXME: For variable sized arrays either of these could be
12814 a variable rather than a constant value. We'll allow it,
12815 but we don't know how to handle it. */
12816 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
12817 if (attr)
12818 low = dwarf2_get_attr_constant_value (attr, low);
12819 else if (!low_default_is_valid)
12820 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
12821 "- DIE at 0x%x [in module %s]"),
12822 die->offset.sect_off, cu->objfile->name);
12823
12824 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
12825 if (attr)
12826 {
12827 if (attr_form_is_block (attr) || is_ref_attr (attr))
12828 {
12829 /* GCC encodes arrays with unspecified or dynamic length
12830 with a DW_FORM_block1 attribute or a reference attribute.
12831 FIXME: GDB does not yet know how to handle dynamic
12832 arrays properly, treat them as arrays with unspecified
12833 length for now.
12834
12835 FIXME: jimb/2003-09-22: GDB does not really know
12836 how to handle arrays of unspecified length
12837 either; we just represent them as zero-length
12838 arrays. Choose an appropriate upper bound given
12839 the lower bound we've computed above. */
12840 high = low - 1;
12841 }
12842 else
12843 high = dwarf2_get_attr_constant_value (attr, 1);
12844 }
12845 else
12846 {
12847 attr = dwarf2_attr (die, DW_AT_count, cu);
12848 if (attr)
12849 {
12850 int count = dwarf2_get_attr_constant_value (attr, 1);
12851 high = low + count - 1;
12852 }
12853 else
12854 {
12855 /* Unspecified array length. */
12856 high = low - 1;
12857 }
12858 }
12859
12860 /* Dwarf-2 specifications explicitly allows to create subrange types
12861 without specifying a base type.
12862 In that case, the base type must be set to the type of
12863 the lower bound, upper bound or count, in that order, if any of these
12864 three attributes references an object that has a type.
12865 If no base type is found, the Dwarf-2 specifications say that
12866 a signed integer type of size equal to the size of an address should
12867 be used.
12868 For the following C code: `extern char gdb_int [];'
12869 GCC produces an empty range DIE.
12870 FIXME: muller/2010-05-28: Possible references to object for low bound,
12871 high bound or count are not yet handled by this code. */
12872 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
12873 {
12874 struct objfile *objfile = cu->objfile;
12875 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12876 int addr_size = gdbarch_addr_bit (gdbarch) /8;
12877 struct type *int_type = objfile_type (objfile)->builtin_int;
12878
12879 /* Test "int", "long int", and "long long int" objfile types,
12880 and select the first one having a size above or equal to the
12881 architecture address size. */
12882 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12883 base_type = int_type;
12884 else
12885 {
12886 int_type = objfile_type (objfile)->builtin_long;
12887 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12888 base_type = int_type;
12889 else
12890 {
12891 int_type = objfile_type (objfile)->builtin_long_long;
12892 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12893 base_type = int_type;
12894 }
12895 }
12896 }
12897
12898 negative_mask =
12899 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
12900 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
12901 low |= negative_mask;
12902 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
12903 high |= negative_mask;
12904
12905 range_type = create_range_type (NULL, orig_base_type, low, high);
12906
12907 /* Mark arrays with dynamic length at least as an array of unspecified
12908 length. GDB could check the boundary but before it gets implemented at
12909 least allow accessing the array elements. */
12910 if (attr && attr_form_is_block (attr))
12911 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12912
12913 /* Ada expects an empty array on no boundary attributes. */
12914 if (attr == NULL && cu->language != language_ada)
12915 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12916
12917 name = dwarf2_name (die, cu);
12918 if (name)
12919 TYPE_NAME (range_type) = name;
12920
12921 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12922 if (attr)
12923 TYPE_LENGTH (range_type) = DW_UNSND (attr);
12924
12925 set_die_type (die, range_type, cu);
12926
12927 /* set_die_type should be already done. */
12928 set_descriptive_type (range_type, die, cu);
12929
12930 return range_type;
12931 }
12932
12933 static struct type *
12934 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
12935 {
12936 struct type *type;
12937
12938 /* For now, we only support the C meaning of an unspecified type: void. */
12939
12940 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
12941 TYPE_NAME (type) = dwarf2_name (die, cu);
12942
12943 return set_die_type (die, type, cu);
12944 }
12945
12946 /* Read a single die and all its descendents. Set the die's sibling
12947 field to NULL; set other fields in the die correctly, and set all
12948 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
12949 location of the info_ptr after reading all of those dies. PARENT
12950 is the parent of the die in question. */
12951
12952 static struct die_info *
12953 read_die_and_children (const struct die_reader_specs *reader,
12954 gdb_byte *info_ptr,
12955 gdb_byte **new_info_ptr,
12956 struct die_info *parent)
12957 {
12958 struct die_info *die;
12959 gdb_byte *cur_ptr;
12960 int has_children;
12961
12962 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
12963 if (die == NULL)
12964 {
12965 *new_info_ptr = cur_ptr;
12966 return NULL;
12967 }
12968 store_in_ref_table (die, reader->cu);
12969
12970 if (has_children)
12971 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
12972 else
12973 {
12974 die->child = NULL;
12975 *new_info_ptr = cur_ptr;
12976 }
12977
12978 die->sibling = NULL;
12979 die->parent = parent;
12980 return die;
12981 }
12982
12983 /* Read a die, all of its descendents, and all of its siblings; set
12984 all of the fields of all of the dies correctly. Arguments are as
12985 in read_die_and_children. */
12986
12987 static struct die_info *
12988 read_die_and_siblings (const struct die_reader_specs *reader,
12989 gdb_byte *info_ptr,
12990 gdb_byte **new_info_ptr,
12991 struct die_info *parent)
12992 {
12993 struct die_info *first_die, *last_sibling;
12994 gdb_byte *cur_ptr;
12995
12996 cur_ptr = info_ptr;
12997 first_die = last_sibling = NULL;
12998
12999 while (1)
13000 {
13001 struct die_info *die
13002 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
13003
13004 if (die == NULL)
13005 {
13006 *new_info_ptr = cur_ptr;
13007 return first_die;
13008 }
13009
13010 if (!first_die)
13011 first_die = die;
13012 else
13013 last_sibling->sibling = die;
13014
13015 last_sibling = die;
13016 }
13017 }
13018
13019 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
13020 attributes.
13021 The caller is responsible for filling in the extra attributes
13022 and updating (*DIEP)->num_attrs.
13023 Set DIEP to point to a newly allocated die with its information,
13024 except for its child, sibling, and parent fields.
13025 Set HAS_CHILDREN to tell whether the die has children or not. */
13026
13027 static gdb_byte *
13028 read_full_die_1 (const struct die_reader_specs *reader,
13029 struct die_info **diep, gdb_byte *info_ptr,
13030 int *has_children, int num_extra_attrs)
13031 {
13032 unsigned int abbrev_number, bytes_read, i;
13033 sect_offset offset;
13034 struct abbrev_info *abbrev;
13035 struct die_info *die;
13036 struct dwarf2_cu *cu = reader->cu;
13037 bfd *abfd = reader->abfd;
13038
13039 offset.sect_off = info_ptr - reader->buffer;
13040 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13041 info_ptr += bytes_read;
13042 if (!abbrev_number)
13043 {
13044 *diep = NULL;
13045 *has_children = 0;
13046 return info_ptr;
13047 }
13048
13049 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
13050 if (!abbrev)
13051 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
13052 abbrev_number,
13053 bfd_get_filename (abfd));
13054
13055 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
13056 die->offset = offset;
13057 die->tag = abbrev->tag;
13058 die->abbrev = abbrev_number;
13059
13060 /* Make the result usable.
13061 The caller needs to update num_attrs after adding the extra
13062 attributes. */
13063 die->num_attrs = abbrev->num_attrs;
13064
13065 for (i = 0; i < abbrev->num_attrs; ++i)
13066 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
13067 info_ptr);
13068
13069 *diep = die;
13070 *has_children = abbrev->has_children;
13071 return info_ptr;
13072 }
13073
13074 /* Read a die and all its attributes.
13075 Set DIEP to point to a newly allocated die with its information,
13076 except for its child, sibling, and parent fields.
13077 Set HAS_CHILDREN to tell whether the die has children or not. */
13078
13079 static gdb_byte *
13080 read_full_die (const struct die_reader_specs *reader,
13081 struct die_info **diep, gdb_byte *info_ptr,
13082 int *has_children)
13083 {
13084 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
13085 }
13086 \f
13087 /* Abbreviation tables.
13088
13089 In DWARF version 2, the description of the debugging information is
13090 stored in a separate .debug_abbrev section. Before we read any
13091 dies from a section we read in all abbreviations and install them
13092 in a hash table. */
13093
13094 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
13095
13096 static struct abbrev_info *
13097 abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
13098 {
13099 struct abbrev_info *abbrev;
13100
13101 abbrev = (struct abbrev_info *)
13102 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
13103 memset (abbrev, 0, sizeof (struct abbrev_info));
13104 return abbrev;
13105 }
13106
13107 /* Add an abbreviation to the table. */
13108
13109 static void
13110 abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
13111 unsigned int abbrev_number,
13112 struct abbrev_info *abbrev)
13113 {
13114 unsigned int hash_number;
13115
13116 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13117 abbrev->next = abbrev_table->abbrevs[hash_number];
13118 abbrev_table->abbrevs[hash_number] = abbrev;
13119 }
13120
13121 /* Look up an abbrev in the table.
13122 Returns NULL if the abbrev is not found. */
13123
13124 static struct abbrev_info *
13125 abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
13126 unsigned int abbrev_number)
13127 {
13128 unsigned int hash_number;
13129 struct abbrev_info *abbrev;
13130
13131 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13132 abbrev = abbrev_table->abbrevs[hash_number];
13133
13134 while (abbrev)
13135 {
13136 if (abbrev->number == abbrev_number)
13137 return abbrev;
13138 abbrev = abbrev->next;
13139 }
13140 return NULL;
13141 }
13142
13143 /* Read in an abbrev table. */
13144
13145 static struct abbrev_table *
13146 abbrev_table_read_table (struct dwarf2_section_info *section,
13147 sect_offset offset)
13148 {
13149 struct objfile *objfile = dwarf2_per_objfile->objfile;
13150 bfd *abfd = section->asection->owner;
13151 struct abbrev_table *abbrev_table;
13152 gdb_byte *abbrev_ptr;
13153 struct abbrev_info *cur_abbrev;
13154 unsigned int abbrev_number, bytes_read, abbrev_name;
13155 unsigned int abbrev_form;
13156 struct attr_abbrev *cur_attrs;
13157 unsigned int allocated_attrs;
13158
13159 abbrev_table = XMALLOC (struct abbrev_table);
13160 abbrev_table->offset = offset;
13161 obstack_init (&abbrev_table->abbrev_obstack);
13162 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
13163 (ABBREV_HASH_SIZE
13164 * sizeof (struct abbrev_info *)));
13165 memset (abbrev_table->abbrevs, 0,
13166 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
13167
13168 dwarf2_read_section (objfile, section);
13169 abbrev_ptr = section->buffer + offset.sect_off;
13170 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13171 abbrev_ptr += bytes_read;
13172
13173 allocated_attrs = ATTR_ALLOC_CHUNK;
13174 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
13175
13176 /* Loop until we reach an abbrev number of 0. */
13177 while (abbrev_number)
13178 {
13179 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
13180
13181 /* read in abbrev header */
13182 cur_abbrev->number = abbrev_number;
13183 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13184 abbrev_ptr += bytes_read;
13185 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
13186 abbrev_ptr += 1;
13187
13188 /* now read in declarations */
13189 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13190 abbrev_ptr += bytes_read;
13191 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13192 abbrev_ptr += bytes_read;
13193 while (abbrev_name)
13194 {
13195 if (cur_abbrev->num_attrs == allocated_attrs)
13196 {
13197 allocated_attrs += ATTR_ALLOC_CHUNK;
13198 cur_attrs
13199 = xrealloc (cur_attrs, (allocated_attrs
13200 * sizeof (struct attr_abbrev)));
13201 }
13202
13203 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
13204 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
13205 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13206 abbrev_ptr += bytes_read;
13207 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13208 abbrev_ptr += bytes_read;
13209 }
13210
13211 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
13212 (cur_abbrev->num_attrs
13213 * sizeof (struct attr_abbrev)));
13214 memcpy (cur_abbrev->attrs, cur_attrs,
13215 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
13216
13217 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
13218
13219 /* Get next abbreviation.
13220 Under Irix6 the abbreviations for a compilation unit are not
13221 always properly terminated with an abbrev number of 0.
13222 Exit loop if we encounter an abbreviation which we have
13223 already read (which means we are about to read the abbreviations
13224 for the next compile unit) or if the end of the abbreviation
13225 table is reached. */
13226 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
13227 break;
13228 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13229 abbrev_ptr += bytes_read;
13230 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
13231 break;
13232 }
13233
13234 xfree (cur_attrs);
13235 return abbrev_table;
13236 }
13237
13238 /* Free the resources held by ABBREV_TABLE. */
13239
13240 static void
13241 abbrev_table_free (struct abbrev_table *abbrev_table)
13242 {
13243 obstack_free (&abbrev_table->abbrev_obstack, NULL);
13244 xfree (abbrev_table);
13245 }
13246
13247 /* Same as abbrev_table_free but as a cleanup.
13248 We pass in a pointer to the pointer to the table so that we can
13249 set the pointer to NULL when we're done. It also simplifies
13250 build_type_unit_groups. */
13251
13252 static void
13253 abbrev_table_free_cleanup (void *table_ptr)
13254 {
13255 struct abbrev_table **abbrev_table_ptr = table_ptr;
13256
13257 if (*abbrev_table_ptr != NULL)
13258 abbrev_table_free (*abbrev_table_ptr);
13259 *abbrev_table_ptr = NULL;
13260 }
13261
13262 /* Read the abbrev table for CU from ABBREV_SECTION. */
13263
13264 static void
13265 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
13266 struct dwarf2_section_info *abbrev_section)
13267 {
13268 cu->abbrev_table =
13269 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
13270 }
13271
13272 /* Release the memory used by the abbrev table for a compilation unit. */
13273
13274 static void
13275 dwarf2_free_abbrev_table (void *ptr_to_cu)
13276 {
13277 struct dwarf2_cu *cu = ptr_to_cu;
13278
13279 abbrev_table_free (cu->abbrev_table);
13280 /* Set this to NULL so that we SEGV if we try to read it later,
13281 and also because free_comp_unit verifies this is NULL. */
13282 cu->abbrev_table = NULL;
13283 }
13284 \f
13285 /* Returns nonzero if TAG represents a type that we might generate a partial
13286 symbol for. */
13287
13288 static int
13289 is_type_tag_for_partial (int tag)
13290 {
13291 switch (tag)
13292 {
13293 #if 0
13294 /* Some types that would be reasonable to generate partial symbols for,
13295 that we don't at present. */
13296 case DW_TAG_array_type:
13297 case DW_TAG_file_type:
13298 case DW_TAG_ptr_to_member_type:
13299 case DW_TAG_set_type:
13300 case DW_TAG_string_type:
13301 case DW_TAG_subroutine_type:
13302 #endif
13303 case DW_TAG_base_type:
13304 case DW_TAG_class_type:
13305 case DW_TAG_interface_type:
13306 case DW_TAG_enumeration_type:
13307 case DW_TAG_structure_type:
13308 case DW_TAG_subrange_type:
13309 case DW_TAG_typedef:
13310 case DW_TAG_union_type:
13311 return 1;
13312 default:
13313 return 0;
13314 }
13315 }
13316
13317 /* Load all DIEs that are interesting for partial symbols into memory. */
13318
13319 static struct partial_die_info *
13320 load_partial_dies (const struct die_reader_specs *reader,
13321 gdb_byte *info_ptr, int building_psymtab)
13322 {
13323 struct dwarf2_cu *cu = reader->cu;
13324 struct objfile *objfile = cu->objfile;
13325 struct partial_die_info *part_die;
13326 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
13327 struct abbrev_info *abbrev;
13328 unsigned int bytes_read;
13329 unsigned int load_all = 0;
13330 int nesting_level = 1;
13331
13332 parent_die = NULL;
13333 last_die = NULL;
13334
13335 gdb_assert (cu->per_cu != NULL);
13336 if (cu->per_cu->load_all_dies)
13337 load_all = 1;
13338
13339 cu->partial_dies
13340 = htab_create_alloc_ex (cu->header.length / 12,
13341 partial_die_hash,
13342 partial_die_eq,
13343 NULL,
13344 &cu->comp_unit_obstack,
13345 hashtab_obstack_allocate,
13346 dummy_obstack_deallocate);
13347
13348 part_die = obstack_alloc (&cu->comp_unit_obstack,
13349 sizeof (struct partial_die_info));
13350
13351 while (1)
13352 {
13353 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
13354
13355 /* A NULL abbrev means the end of a series of children. */
13356 if (abbrev == NULL)
13357 {
13358 if (--nesting_level == 0)
13359 {
13360 /* PART_DIE was probably the last thing allocated on the
13361 comp_unit_obstack, so we could call obstack_free
13362 here. We don't do that because the waste is small,
13363 and will be cleaned up when we're done with this
13364 compilation unit. This way, we're also more robust
13365 against other users of the comp_unit_obstack. */
13366 return first_die;
13367 }
13368 info_ptr += bytes_read;
13369 last_die = parent_die;
13370 parent_die = parent_die->die_parent;
13371 continue;
13372 }
13373
13374 /* Check for template arguments. We never save these; if
13375 they're seen, we just mark the parent, and go on our way. */
13376 if (parent_die != NULL
13377 && cu->language == language_cplus
13378 && (abbrev->tag == DW_TAG_template_type_param
13379 || abbrev->tag == DW_TAG_template_value_param))
13380 {
13381 parent_die->has_template_arguments = 1;
13382
13383 if (!load_all)
13384 {
13385 /* We don't need a partial DIE for the template argument. */
13386 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
13387 continue;
13388 }
13389 }
13390
13391 /* We only recurse into c++ subprograms looking for template arguments.
13392 Skip their other children. */
13393 if (!load_all
13394 && cu->language == language_cplus
13395 && parent_die != NULL
13396 && parent_die->tag == DW_TAG_subprogram)
13397 {
13398 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
13399 continue;
13400 }
13401
13402 /* Check whether this DIE is interesting enough to save. Normally
13403 we would not be interested in members here, but there may be
13404 later variables referencing them via DW_AT_specification (for
13405 static members). */
13406 if (!load_all
13407 && !is_type_tag_for_partial (abbrev->tag)
13408 && abbrev->tag != DW_TAG_constant
13409 && abbrev->tag != DW_TAG_enumerator
13410 && abbrev->tag != DW_TAG_subprogram
13411 && abbrev->tag != DW_TAG_lexical_block
13412 && abbrev->tag != DW_TAG_variable
13413 && abbrev->tag != DW_TAG_namespace
13414 && abbrev->tag != DW_TAG_module
13415 && abbrev->tag != DW_TAG_member
13416 && abbrev->tag != DW_TAG_imported_unit)
13417 {
13418 /* Otherwise we skip to the next sibling, if any. */
13419 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
13420 continue;
13421 }
13422
13423 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
13424 info_ptr);
13425
13426 /* This two-pass algorithm for processing partial symbols has a
13427 high cost in cache pressure. Thus, handle some simple cases
13428 here which cover the majority of C partial symbols. DIEs
13429 which neither have specification tags in them, nor could have
13430 specification tags elsewhere pointing at them, can simply be
13431 processed and discarded.
13432
13433 This segment is also optional; scan_partial_symbols and
13434 add_partial_symbol will handle these DIEs if we chain
13435 them in normally. When compilers which do not emit large
13436 quantities of duplicate debug information are more common,
13437 this code can probably be removed. */
13438
13439 /* Any complete simple types at the top level (pretty much all
13440 of them, for a language without namespaces), can be processed
13441 directly. */
13442 if (parent_die == NULL
13443 && part_die->has_specification == 0
13444 && part_die->is_declaration == 0
13445 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
13446 || part_die->tag == DW_TAG_base_type
13447 || part_die->tag == DW_TAG_subrange_type))
13448 {
13449 if (building_psymtab && part_die->name != NULL)
13450 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
13451 VAR_DOMAIN, LOC_TYPEDEF,
13452 &objfile->static_psymbols,
13453 0, (CORE_ADDR) 0, cu->language, objfile);
13454 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
13455 continue;
13456 }
13457
13458 /* The exception for DW_TAG_typedef with has_children above is
13459 a workaround of GCC PR debug/47510. In the case of this complaint
13460 type_name_no_tag_or_error will error on such types later.
13461
13462 GDB skipped children of DW_TAG_typedef by the shortcut above and then
13463 it could not find the child DIEs referenced later, this is checked
13464 above. In correct DWARF DW_TAG_typedef should have no children. */
13465
13466 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
13467 complaint (&symfile_complaints,
13468 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
13469 "- DIE at 0x%x [in module %s]"),
13470 part_die->offset.sect_off, objfile->name);
13471
13472 /* If we're at the second level, and we're an enumerator, and
13473 our parent has no specification (meaning possibly lives in a
13474 namespace elsewhere), then we can add the partial symbol now
13475 instead of queueing it. */
13476 if (part_die->tag == DW_TAG_enumerator
13477 && parent_die != NULL
13478 && parent_die->die_parent == NULL
13479 && parent_die->tag == DW_TAG_enumeration_type
13480 && parent_die->has_specification == 0)
13481 {
13482 if (part_die->name == NULL)
13483 complaint (&symfile_complaints,
13484 _("malformed enumerator DIE ignored"));
13485 else if (building_psymtab)
13486 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
13487 VAR_DOMAIN, LOC_CONST,
13488 (cu->language == language_cplus
13489 || cu->language == language_java)
13490 ? &objfile->global_psymbols
13491 : &objfile->static_psymbols,
13492 0, (CORE_ADDR) 0, cu->language, objfile);
13493
13494 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
13495 continue;
13496 }
13497
13498 /* We'll save this DIE so link it in. */
13499 part_die->die_parent = parent_die;
13500 part_die->die_sibling = NULL;
13501 part_die->die_child = NULL;
13502
13503 if (last_die && last_die == parent_die)
13504 last_die->die_child = part_die;
13505 else if (last_die)
13506 last_die->die_sibling = part_die;
13507
13508 last_die = part_die;
13509
13510 if (first_die == NULL)
13511 first_die = part_die;
13512
13513 /* Maybe add the DIE to the hash table. Not all DIEs that we
13514 find interesting need to be in the hash table, because we
13515 also have the parent/sibling/child chains; only those that we
13516 might refer to by offset later during partial symbol reading.
13517
13518 For now this means things that might have be the target of a
13519 DW_AT_specification, DW_AT_abstract_origin, or
13520 DW_AT_extension. DW_AT_extension will refer only to
13521 namespaces; DW_AT_abstract_origin refers to functions (and
13522 many things under the function DIE, but we do not recurse
13523 into function DIEs during partial symbol reading) and
13524 possibly variables as well; DW_AT_specification refers to
13525 declarations. Declarations ought to have the DW_AT_declaration
13526 flag. It happens that GCC forgets to put it in sometimes, but
13527 only for functions, not for types.
13528
13529 Adding more things than necessary to the hash table is harmless
13530 except for the performance cost. Adding too few will result in
13531 wasted time in find_partial_die, when we reread the compilation
13532 unit with load_all_dies set. */
13533
13534 if (load_all
13535 || abbrev->tag == DW_TAG_constant
13536 || abbrev->tag == DW_TAG_subprogram
13537 || abbrev->tag == DW_TAG_variable
13538 || abbrev->tag == DW_TAG_namespace
13539 || part_die->is_declaration)
13540 {
13541 void **slot;
13542
13543 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
13544 part_die->offset.sect_off, INSERT);
13545 *slot = part_die;
13546 }
13547
13548 part_die = obstack_alloc (&cu->comp_unit_obstack,
13549 sizeof (struct partial_die_info));
13550
13551 /* For some DIEs we want to follow their children (if any). For C
13552 we have no reason to follow the children of structures; for other
13553 languages we have to, so that we can get at method physnames
13554 to infer fully qualified class names, for DW_AT_specification,
13555 and for C++ template arguments. For C++, we also look one level
13556 inside functions to find template arguments (if the name of the
13557 function does not already contain the template arguments).
13558
13559 For Ada, we need to scan the children of subprograms and lexical
13560 blocks as well because Ada allows the definition of nested
13561 entities that could be interesting for the debugger, such as
13562 nested subprograms for instance. */
13563 if (last_die->has_children
13564 && (load_all
13565 || last_die->tag == DW_TAG_namespace
13566 || last_die->tag == DW_TAG_module
13567 || last_die->tag == DW_TAG_enumeration_type
13568 || (cu->language == language_cplus
13569 && last_die->tag == DW_TAG_subprogram
13570 && (last_die->name == NULL
13571 || strchr (last_die->name, '<') == NULL))
13572 || (cu->language != language_c
13573 && (last_die->tag == DW_TAG_class_type
13574 || last_die->tag == DW_TAG_interface_type
13575 || last_die->tag == DW_TAG_structure_type
13576 || last_die->tag == DW_TAG_union_type))
13577 || (cu->language == language_ada
13578 && (last_die->tag == DW_TAG_subprogram
13579 || last_die->tag == DW_TAG_lexical_block))))
13580 {
13581 nesting_level++;
13582 parent_die = last_die;
13583 continue;
13584 }
13585
13586 /* Otherwise we skip to the next sibling, if any. */
13587 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
13588
13589 /* Back to the top, do it again. */
13590 }
13591 }
13592
13593 /* Read a minimal amount of information into the minimal die structure. */
13594
13595 static gdb_byte *
13596 read_partial_die (const struct die_reader_specs *reader,
13597 struct partial_die_info *part_die,
13598 struct abbrev_info *abbrev, unsigned int abbrev_len,
13599 gdb_byte *info_ptr)
13600 {
13601 struct dwarf2_cu *cu = reader->cu;
13602 struct objfile *objfile = cu->objfile;
13603 gdb_byte *buffer = reader->buffer;
13604 unsigned int i;
13605 struct attribute attr;
13606 int has_low_pc_attr = 0;
13607 int has_high_pc_attr = 0;
13608 int high_pc_relative = 0;
13609
13610 memset (part_die, 0, sizeof (struct partial_die_info));
13611
13612 part_die->offset.sect_off = info_ptr - buffer;
13613
13614 info_ptr += abbrev_len;
13615
13616 if (abbrev == NULL)
13617 return info_ptr;
13618
13619 part_die->tag = abbrev->tag;
13620 part_die->has_children = abbrev->has_children;
13621
13622 for (i = 0; i < abbrev->num_attrs; ++i)
13623 {
13624 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
13625
13626 /* Store the data if it is of an attribute we want to keep in a
13627 partial symbol table. */
13628 switch (attr.name)
13629 {
13630 case DW_AT_name:
13631 switch (part_die->tag)
13632 {
13633 case DW_TAG_compile_unit:
13634 case DW_TAG_partial_unit:
13635 case DW_TAG_type_unit:
13636 /* Compilation units have a DW_AT_name that is a filename, not
13637 a source language identifier. */
13638 case DW_TAG_enumeration_type:
13639 case DW_TAG_enumerator:
13640 /* These tags always have simple identifiers already; no need
13641 to canonicalize them. */
13642 part_die->name = DW_STRING (&attr);
13643 break;
13644 default:
13645 part_die->name
13646 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
13647 &objfile->objfile_obstack);
13648 break;
13649 }
13650 break;
13651 case DW_AT_linkage_name:
13652 case DW_AT_MIPS_linkage_name:
13653 /* Note that both forms of linkage name might appear. We
13654 assume they will be the same, and we only store the last
13655 one we see. */
13656 if (cu->language == language_ada)
13657 part_die->name = DW_STRING (&attr);
13658 part_die->linkage_name = DW_STRING (&attr);
13659 break;
13660 case DW_AT_low_pc:
13661 has_low_pc_attr = 1;
13662 part_die->lowpc = DW_ADDR (&attr);
13663 break;
13664 case DW_AT_high_pc:
13665 has_high_pc_attr = 1;
13666 if (attr.form == DW_FORM_addr
13667 || attr.form == DW_FORM_GNU_addr_index)
13668 part_die->highpc = DW_ADDR (&attr);
13669 else
13670 {
13671 high_pc_relative = 1;
13672 part_die->highpc = DW_UNSND (&attr);
13673 }
13674 break;
13675 case DW_AT_location:
13676 /* Support the .debug_loc offsets. */
13677 if (attr_form_is_block (&attr))
13678 {
13679 part_die->d.locdesc = DW_BLOCK (&attr);
13680 }
13681 else if (attr_form_is_section_offset (&attr))
13682 {
13683 dwarf2_complex_location_expr_complaint ();
13684 }
13685 else
13686 {
13687 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
13688 "partial symbol information");
13689 }
13690 break;
13691 case DW_AT_external:
13692 part_die->is_external = DW_UNSND (&attr);
13693 break;
13694 case DW_AT_declaration:
13695 part_die->is_declaration = DW_UNSND (&attr);
13696 break;
13697 case DW_AT_type:
13698 part_die->has_type = 1;
13699 break;
13700 case DW_AT_abstract_origin:
13701 case DW_AT_specification:
13702 case DW_AT_extension:
13703 part_die->has_specification = 1;
13704 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
13705 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13706 || cu->per_cu->is_dwz);
13707 break;
13708 case DW_AT_sibling:
13709 /* Ignore absolute siblings, they might point outside of
13710 the current compile unit. */
13711 if (attr.form == DW_FORM_ref_addr)
13712 complaint (&symfile_complaints,
13713 _("ignoring absolute DW_AT_sibling"));
13714 else
13715 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
13716 break;
13717 case DW_AT_byte_size:
13718 part_die->has_byte_size = 1;
13719 break;
13720 case DW_AT_calling_convention:
13721 /* DWARF doesn't provide a way to identify a program's source-level
13722 entry point. DW_AT_calling_convention attributes are only meant
13723 to describe functions' calling conventions.
13724
13725 However, because it's a necessary piece of information in
13726 Fortran, and because DW_CC_program is the only piece of debugging
13727 information whose definition refers to a 'main program' at all,
13728 several compilers have begun marking Fortran main programs with
13729 DW_CC_program --- even when those functions use the standard
13730 calling conventions.
13731
13732 So until DWARF specifies a way to provide this information and
13733 compilers pick up the new representation, we'll support this
13734 practice. */
13735 if (DW_UNSND (&attr) == DW_CC_program
13736 && cu->language == language_fortran)
13737 {
13738 set_main_name (part_die->name);
13739
13740 /* As this DIE has a static linkage the name would be difficult
13741 to look up later. */
13742 language_of_main = language_fortran;
13743 }
13744 break;
13745 case DW_AT_inline:
13746 if (DW_UNSND (&attr) == DW_INL_inlined
13747 || DW_UNSND (&attr) == DW_INL_declared_inlined)
13748 part_die->may_be_inlined = 1;
13749 break;
13750
13751 case DW_AT_import:
13752 if (part_die->tag == DW_TAG_imported_unit)
13753 {
13754 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
13755 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13756 || cu->per_cu->is_dwz);
13757 }
13758 break;
13759
13760 default:
13761 break;
13762 }
13763 }
13764
13765 if (high_pc_relative)
13766 part_die->highpc += part_die->lowpc;
13767
13768 if (has_low_pc_attr && has_high_pc_attr)
13769 {
13770 /* When using the GNU linker, .gnu.linkonce. sections are used to
13771 eliminate duplicate copies of functions and vtables and such.
13772 The linker will arbitrarily choose one and discard the others.
13773 The AT_*_pc values for such functions refer to local labels in
13774 these sections. If the section from that file was discarded, the
13775 labels are not in the output, so the relocs get a value of 0.
13776 If this is a discarded function, mark the pc bounds as invalid,
13777 so that GDB will ignore it. */
13778 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
13779 {
13780 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13781
13782 complaint (&symfile_complaints,
13783 _("DW_AT_low_pc %s is zero "
13784 "for DIE at 0x%x [in module %s]"),
13785 paddress (gdbarch, part_die->lowpc),
13786 part_die->offset.sect_off, objfile->name);
13787 }
13788 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
13789 else if (part_die->lowpc >= part_die->highpc)
13790 {
13791 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13792
13793 complaint (&symfile_complaints,
13794 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
13795 "for DIE at 0x%x [in module %s]"),
13796 paddress (gdbarch, part_die->lowpc),
13797 paddress (gdbarch, part_die->highpc),
13798 part_die->offset.sect_off, objfile->name);
13799 }
13800 else
13801 part_die->has_pc_info = 1;
13802 }
13803
13804 return info_ptr;
13805 }
13806
13807 /* Find a cached partial DIE at OFFSET in CU. */
13808
13809 static struct partial_die_info *
13810 find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
13811 {
13812 struct partial_die_info *lookup_die = NULL;
13813 struct partial_die_info part_die;
13814
13815 part_die.offset = offset;
13816 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
13817 offset.sect_off);
13818
13819 return lookup_die;
13820 }
13821
13822 /* Find a partial DIE at OFFSET, which may or may not be in CU,
13823 except in the case of .debug_types DIEs which do not reference
13824 outside their CU (they do however referencing other types via
13825 DW_FORM_ref_sig8). */
13826
13827 static struct partial_die_info *
13828 find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
13829 {
13830 struct objfile *objfile = cu->objfile;
13831 struct dwarf2_per_cu_data *per_cu = NULL;
13832 struct partial_die_info *pd = NULL;
13833
13834 if (offset_in_dwz == cu->per_cu->is_dwz
13835 && offset_in_cu_p (&cu->header, offset))
13836 {
13837 pd = find_partial_die_in_comp_unit (offset, cu);
13838 if (pd != NULL)
13839 return pd;
13840 /* We missed recording what we needed.
13841 Load all dies and try again. */
13842 per_cu = cu->per_cu;
13843 }
13844 else
13845 {
13846 /* TUs don't reference other CUs/TUs (except via type signatures). */
13847 if (cu->per_cu->is_debug_types)
13848 {
13849 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
13850 " external reference to offset 0x%lx [in module %s].\n"),
13851 (long) cu->header.offset.sect_off, (long) offset.sect_off,
13852 bfd_get_filename (objfile->obfd));
13853 }
13854 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
13855 objfile);
13856
13857 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
13858 load_partial_comp_unit (per_cu);
13859
13860 per_cu->cu->last_used = 0;
13861 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13862 }
13863
13864 /* If we didn't find it, and not all dies have been loaded,
13865 load them all and try again. */
13866
13867 if (pd == NULL && per_cu->load_all_dies == 0)
13868 {
13869 per_cu->load_all_dies = 1;
13870
13871 /* This is nasty. When we reread the DIEs, somewhere up the call chain
13872 THIS_CU->cu may already be in use. So we can't just free it and
13873 replace its DIEs with the ones we read in. Instead, we leave those
13874 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
13875 and clobber THIS_CU->cu->partial_dies with the hash table for the new
13876 set. */
13877 load_partial_comp_unit (per_cu);
13878
13879 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13880 }
13881
13882 if (pd == NULL)
13883 internal_error (__FILE__, __LINE__,
13884 _("could not find partial DIE 0x%x "
13885 "in cache [from module %s]\n"),
13886 offset.sect_off, bfd_get_filename (objfile->obfd));
13887 return pd;
13888 }
13889
13890 /* See if we can figure out if the class lives in a namespace. We do
13891 this by looking for a member function; its demangled name will
13892 contain namespace info, if there is any. */
13893
13894 static void
13895 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
13896 struct dwarf2_cu *cu)
13897 {
13898 /* NOTE: carlton/2003-10-07: Getting the info this way changes
13899 what template types look like, because the demangler
13900 frequently doesn't give the same name as the debug info. We
13901 could fix this by only using the demangled name to get the
13902 prefix (but see comment in read_structure_type). */
13903
13904 struct partial_die_info *real_pdi;
13905 struct partial_die_info *child_pdi;
13906
13907 /* If this DIE (this DIE's specification, if any) has a parent, then
13908 we should not do this. We'll prepend the parent's fully qualified
13909 name when we create the partial symbol. */
13910
13911 real_pdi = struct_pdi;
13912 while (real_pdi->has_specification)
13913 real_pdi = find_partial_die (real_pdi->spec_offset,
13914 real_pdi->spec_is_dwz, cu);
13915
13916 if (real_pdi->die_parent != NULL)
13917 return;
13918
13919 for (child_pdi = struct_pdi->die_child;
13920 child_pdi != NULL;
13921 child_pdi = child_pdi->die_sibling)
13922 {
13923 if (child_pdi->tag == DW_TAG_subprogram
13924 && child_pdi->linkage_name != NULL)
13925 {
13926 char *actual_class_name
13927 = language_class_name_from_physname (cu->language_defn,
13928 child_pdi->linkage_name);
13929 if (actual_class_name != NULL)
13930 {
13931 struct_pdi->name
13932 = obstack_copy0 (&cu->objfile->objfile_obstack,
13933 actual_class_name,
13934 strlen (actual_class_name));
13935 xfree (actual_class_name);
13936 }
13937 break;
13938 }
13939 }
13940 }
13941
13942 /* Adjust PART_DIE before generating a symbol for it. This function
13943 may set the is_external flag or change the DIE's name. */
13944
13945 static void
13946 fixup_partial_die (struct partial_die_info *part_die,
13947 struct dwarf2_cu *cu)
13948 {
13949 /* Once we've fixed up a die, there's no point in doing so again.
13950 This also avoids a memory leak if we were to call
13951 guess_partial_die_structure_name multiple times. */
13952 if (part_die->fixup_called)
13953 return;
13954
13955 /* If we found a reference attribute and the DIE has no name, try
13956 to find a name in the referred to DIE. */
13957
13958 if (part_die->name == NULL && part_die->has_specification)
13959 {
13960 struct partial_die_info *spec_die;
13961
13962 spec_die = find_partial_die (part_die->spec_offset,
13963 part_die->spec_is_dwz, cu);
13964
13965 fixup_partial_die (spec_die, cu);
13966
13967 if (spec_die->name)
13968 {
13969 part_die->name = spec_die->name;
13970
13971 /* Copy DW_AT_external attribute if it is set. */
13972 if (spec_die->is_external)
13973 part_die->is_external = spec_die->is_external;
13974 }
13975 }
13976
13977 /* Set default names for some unnamed DIEs. */
13978
13979 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
13980 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
13981
13982 /* If there is no parent die to provide a namespace, and there are
13983 children, see if we can determine the namespace from their linkage
13984 name. */
13985 if (cu->language == language_cplus
13986 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
13987 && part_die->die_parent == NULL
13988 && part_die->has_children
13989 && (part_die->tag == DW_TAG_class_type
13990 || part_die->tag == DW_TAG_structure_type
13991 || part_die->tag == DW_TAG_union_type))
13992 guess_partial_die_structure_name (part_die, cu);
13993
13994 /* GCC might emit a nameless struct or union that has a linkage
13995 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
13996 if (part_die->name == NULL
13997 && (part_die->tag == DW_TAG_class_type
13998 || part_die->tag == DW_TAG_interface_type
13999 || part_die->tag == DW_TAG_structure_type
14000 || part_die->tag == DW_TAG_union_type)
14001 && part_die->linkage_name != NULL)
14002 {
14003 char *demangled;
14004
14005 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
14006 if (demangled)
14007 {
14008 const char *base;
14009
14010 /* Strip any leading namespaces/classes, keep only the base name.
14011 DW_AT_name for named DIEs does not contain the prefixes. */
14012 base = strrchr (demangled, ':');
14013 if (base && base > demangled && base[-1] == ':')
14014 base++;
14015 else
14016 base = demangled;
14017
14018 part_die->name = obstack_copy0 (&cu->objfile->objfile_obstack,
14019 base, strlen (base));
14020 xfree (demangled);
14021 }
14022 }
14023
14024 part_die->fixup_called = 1;
14025 }
14026
14027 /* Read an attribute value described by an attribute form. */
14028
14029 static gdb_byte *
14030 read_attribute_value (const struct die_reader_specs *reader,
14031 struct attribute *attr, unsigned form,
14032 gdb_byte *info_ptr)
14033 {
14034 struct dwarf2_cu *cu = reader->cu;
14035 bfd *abfd = reader->abfd;
14036 struct comp_unit_head *cu_header = &cu->header;
14037 unsigned int bytes_read;
14038 struct dwarf_block *blk;
14039
14040 attr->form = form;
14041 switch (form)
14042 {
14043 case DW_FORM_ref_addr:
14044 if (cu->header.version == 2)
14045 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
14046 else
14047 DW_UNSND (attr) = read_offset (abfd, info_ptr,
14048 &cu->header, &bytes_read);
14049 info_ptr += bytes_read;
14050 break;
14051 case DW_FORM_GNU_ref_alt:
14052 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14053 info_ptr += bytes_read;
14054 break;
14055 case DW_FORM_addr:
14056 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
14057 info_ptr += bytes_read;
14058 break;
14059 case DW_FORM_block2:
14060 blk = dwarf_alloc_block (cu);
14061 blk->size = read_2_bytes (abfd, info_ptr);
14062 info_ptr += 2;
14063 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14064 info_ptr += blk->size;
14065 DW_BLOCK (attr) = blk;
14066 break;
14067 case DW_FORM_block4:
14068 blk = dwarf_alloc_block (cu);
14069 blk->size = read_4_bytes (abfd, info_ptr);
14070 info_ptr += 4;
14071 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14072 info_ptr += blk->size;
14073 DW_BLOCK (attr) = blk;
14074 break;
14075 case DW_FORM_data2:
14076 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
14077 info_ptr += 2;
14078 break;
14079 case DW_FORM_data4:
14080 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
14081 info_ptr += 4;
14082 break;
14083 case DW_FORM_data8:
14084 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
14085 info_ptr += 8;
14086 break;
14087 case DW_FORM_sec_offset:
14088 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14089 info_ptr += bytes_read;
14090 break;
14091 case DW_FORM_string:
14092 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
14093 DW_STRING_IS_CANONICAL (attr) = 0;
14094 info_ptr += bytes_read;
14095 break;
14096 case DW_FORM_strp:
14097 if (!cu->per_cu->is_dwz)
14098 {
14099 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
14100 &bytes_read);
14101 DW_STRING_IS_CANONICAL (attr) = 0;
14102 info_ptr += bytes_read;
14103 break;
14104 }
14105 /* FALLTHROUGH */
14106 case DW_FORM_GNU_strp_alt:
14107 {
14108 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14109 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
14110 &bytes_read);
14111
14112 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
14113 DW_STRING_IS_CANONICAL (attr) = 0;
14114 info_ptr += bytes_read;
14115 }
14116 break;
14117 case DW_FORM_exprloc:
14118 case DW_FORM_block:
14119 blk = dwarf_alloc_block (cu);
14120 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14121 info_ptr += bytes_read;
14122 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14123 info_ptr += blk->size;
14124 DW_BLOCK (attr) = blk;
14125 break;
14126 case DW_FORM_block1:
14127 blk = dwarf_alloc_block (cu);
14128 blk->size = read_1_byte (abfd, info_ptr);
14129 info_ptr += 1;
14130 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14131 info_ptr += blk->size;
14132 DW_BLOCK (attr) = blk;
14133 break;
14134 case DW_FORM_data1:
14135 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14136 info_ptr += 1;
14137 break;
14138 case DW_FORM_flag:
14139 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14140 info_ptr += 1;
14141 break;
14142 case DW_FORM_flag_present:
14143 DW_UNSND (attr) = 1;
14144 break;
14145 case DW_FORM_sdata:
14146 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
14147 info_ptr += bytes_read;
14148 break;
14149 case DW_FORM_udata:
14150 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14151 info_ptr += bytes_read;
14152 break;
14153 case DW_FORM_ref1:
14154 DW_UNSND (attr) = (cu->header.offset.sect_off
14155 + read_1_byte (abfd, info_ptr));
14156 info_ptr += 1;
14157 break;
14158 case DW_FORM_ref2:
14159 DW_UNSND (attr) = (cu->header.offset.sect_off
14160 + read_2_bytes (abfd, info_ptr));
14161 info_ptr += 2;
14162 break;
14163 case DW_FORM_ref4:
14164 DW_UNSND (attr) = (cu->header.offset.sect_off
14165 + read_4_bytes (abfd, info_ptr));
14166 info_ptr += 4;
14167 break;
14168 case DW_FORM_ref8:
14169 DW_UNSND (attr) = (cu->header.offset.sect_off
14170 + read_8_bytes (abfd, info_ptr));
14171 info_ptr += 8;
14172 break;
14173 case DW_FORM_ref_sig8:
14174 /* Convert the signature to something we can record in DW_UNSND
14175 for later lookup.
14176 NOTE: This is NULL if the type wasn't found. */
14177 DW_SIGNATURED_TYPE (attr) =
14178 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
14179 info_ptr += 8;
14180 break;
14181 case DW_FORM_ref_udata:
14182 DW_UNSND (attr) = (cu->header.offset.sect_off
14183 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
14184 info_ptr += bytes_read;
14185 break;
14186 case DW_FORM_indirect:
14187 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14188 info_ptr += bytes_read;
14189 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
14190 break;
14191 case DW_FORM_GNU_addr_index:
14192 if (reader->dwo_file == NULL)
14193 {
14194 /* For now flag a hard error.
14195 Later we can turn this into a complaint. */
14196 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14197 dwarf_form_name (form),
14198 bfd_get_filename (abfd));
14199 }
14200 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
14201 info_ptr += bytes_read;
14202 break;
14203 case DW_FORM_GNU_str_index:
14204 if (reader->dwo_file == NULL)
14205 {
14206 /* For now flag a hard error.
14207 Later we can turn this into a complaint if warranted. */
14208 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14209 dwarf_form_name (form),
14210 bfd_get_filename (abfd));
14211 }
14212 {
14213 ULONGEST str_index =
14214 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14215
14216 DW_STRING (attr) = read_str_index (reader, cu, str_index);
14217 DW_STRING_IS_CANONICAL (attr) = 0;
14218 info_ptr += bytes_read;
14219 }
14220 break;
14221 default:
14222 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
14223 dwarf_form_name (form),
14224 bfd_get_filename (abfd));
14225 }
14226
14227 /* Super hack. */
14228 if (cu->per_cu->is_dwz && is_ref_attr (attr))
14229 attr->form = DW_FORM_GNU_ref_alt;
14230
14231 /* We have seen instances where the compiler tried to emit a byte
14232 size attribute of -1 which ended up being encoded as an unsigned
14233 0xffffffff. Although 0xffffffff is technically a valid size value,
14234 an object of this size seems pretty unlikely so we can relatively
14235 safely treat these cases as if the size attribute was invalid and
14236 treat them as zero by default. */
14237 if (attr->name == DW_AT_byte_size
14238 && form == DW_FORM_data4
14239 && DW_UNSND (attr) >= 0xffffffff)
14240 {
14241 complaint
14242 (&symfile_complaints,
14243 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
14244 hex_string (DW_UNSND (attr)));
14245 DW_UNSND (attr) = 0;
14246 }
14247
14248 return info_ptr;
14249 }
14250
14251 /* Read an attribute described by an abbreviated attribute. */
14252
14253 static gdb_byte *
14254 read_attribute (const struct die_reader_specs *reader,
14255 struct attribute *attr, struct attr_abbrev *abbrev,
14256 gdb_byte *info_ptr)
14257 {
14258 attr->name = abbrev->name;
14259 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
14260 }
14261
14262 /* Read dwarf information from a buffer. */
14263
14264 static unsigned int
14265 read_1_byte (bfd *abfd, const gdb_byte *buf)
14266 {
14267 return bfd_get_8 (abfd, buf);
14268 }
14269
14270 static int
14271 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
14272 {
14273 return bfd_get_signed_8 (abfd, buf);
14274 }
14275
14276 static unsigned int
14277 read_2_bytes (bfd *abfd, const gdb_byte *buf)
14278 {
14279 return bfd_get_16 (abfd, buf);
14280 }
14281
14282 static int
14283 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
14284 {
14285 return bfd_get_signed_16 (abfd, buf);
14286 }
14287
14288 static unsigned int
14289 read_4_bytes (bfd *abfd, const gdb_byte *buf)
14290 {
14291 return bfd_get_32 (abfd, buf);
14292 }
14293
14294 static int
14295 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
14296 {
14297 return bfd_get_signed_32 (abfd, buf);
14298 }
14299
14300 static ULONGEST
14301 read_8_bytes (bfd *abfd, const gdb_byte *buf)
14302 {
14303 return bfd_get_64 (abfd, buf);
14304 }
14305
14306 static CORE_ADDR
14307 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
14308 unsigned int *bytes_read)
14309 {
14310 struct comp_unit_head *cu_header = &cu->header;
14311 CORE_ADDR retval = 0;
14312
14313 if (cu_header->signed_addr_p)
14314 {
14315 switch (cu_header->addr_size)
14316 {
14317 case 2:
14318 retval = bfd_get_signed_16 (abfd, buf);
14319 break;
14320 case 4:
14321 retval = bfd_get_signed_32 (abfd, buf);
14322 break;
14323 case 8:
14324 retval = bfd_get_signed_64 (abfd, buf);
14325 break;
14326 default:
14327 internal_error (__FILE__, __LINE__,
14328 _("read_address: bad switch, signed [in module %s]"),
14329 bfd_get_filename (abfd));
14330 }
14331 }
14332 else
14333 {
14334 switch (cu_header->addr_size)
14335 {
14336 case 2:
14337 retval = bfd_get_16 (abfd, buf);
14338 break;
14339 case 4:
14340 retval = bfd_get_32 (abfd, buf);
14341 break;
14342 case 8:
14343 retval = bfd_get_64 (abfd, buf);
14344 break;
14345 default:
14346 internal_error (__FILE__, __LINE__,
14347 _("read_address: bad switch, "
14348 "unsigned [in module %s]"),
14349 bfd_get_filename (abfd));
14350 }
14351 }
14352
14353 *bytes_read = cu_header->addr_size;
14354 return retval;
14355 }
14356
14357 /* Read the initial length from a section. The (draft) DWARF 3
14358 specification allows the initial length to take up either 4 bytes
14359 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
14360 bytes describe the length and all offsets will be 8 bytes in length
14361 instead of 4.
14362
14363 An older, non-standard 64-bit format is also handled by this
14364 function. The older format in question stores the initial length
14365 as an 8-byte quantity without an escape value. Lengths greater
14366 than 2^32 aren't very common which means that the initial 4 bytes
14367 is almost always zero. Since a length value of zero doesn't make
14368 sense for the 32-bit format, this initial zero can be considered to
14369 be an escape value which indicates the presence of the older 64-bit
14370 format. As written, the code can't detect (old format) lengths
14371 greater than 4GB. If it becomes necessary to handle lengths
14372 somewhat larger than 4GB, we could allow other small values (such
14373 as the non-sensical values of 1, 2, and 3) to also be used as
14374 escape values indicating the presence of the old format.
14375
14376 The value returned via bytes_read should be used to increment the
14377 relevant pointer after calling read_initial_length().
14378
14379 [ Note: read_initial_length() and read_offset() are based on the
14380 document entitled "DWARF Debugging Information Format", revision
14381 3, draft 8, dated November 19, 2001. This document was obtained
14382 from:
14383
14384 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
14385
14386 This document is only a draft and is subject to change. (So beware.)
14387
14388 Details regarding the older, non-standard 64-bit format were
14389 determined empirically by examining 64-bit ELF files produced by
14390 the SGI toolchain on an IRIX 6.5 machine.
14391
14392 - Kevin, July 16, 2002
14393 ] */
14394
14395 static LONGEST
14396 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
14397 {
14398 LONGEST length = bfd_get_32 (abfd, buf);
14399
14400 if (length == 0xffffffff)
14401 {
14402 length = bfd_get_64 (abfd, buf + 4);
14403 *bytes_read = 12;
14404 }
14405 else if (length == 0)
14406 {
14407 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
14408 length = bfd_get_64 (abfd, buf);
14409 *bytes_read = 8;
14410 }
14411 else
14412 {
14413 *bytes_read = 4;
14414 }
14415
14416 return length;
14417 }
14418
14419 /* Cover function for read_initial_length.
14420 Returns the length of the object at BUF, and stores the size of the
14421 initial length in *BYTES_READ and stores the size that offsets will be in
14422 *OFFSET_SIZE.
14423 If the initial length size is not equivalent to that specified in
14424 CU_HEADER then issue a complaint.
14425 This is useful when reading non-comp-unit headers. */
14426
14427 static LONGEST
14428 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
14429 const struct comp_unit_head *cu_header,
14430 unsigned int *bytes_read,
14431 unsigned int *offset_size)
14432 {
14433 LONGEST length = read_initial_length (abfd, buf, bytes_read);
14434
14435 gdb_assert (cu_header->initial_length_size == 4
14436 || cu_header->initial_length_size == 8
14437 || cu_header->initial_length_size == 12);
14438
14439 if (cu_header->initial_length_size != *bytes_read)
14440 complaint (&symfile_complaints,
14441 _("intermixed 32-bit and 64-bit DWARF sections"));
14442
14443 *offset_size = (*bytes_read == 4) ? 4 : 8;
14444 return length;
14445 }
14446
14447 /* Read an offset from the data stream. The size of the offset is
14448 given by cu_header->offset_size. */
14449
14450 static LONGEST
14451 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
14452 unsigned int *bytes_read)
14453 {
14454 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
14455
14456 *bytes_read = cu_header->offset_size;
14457 return offset;
14458 }
14459
14460 /* Read an offset from the data stream. */
14461
14462 static LONGEST
14463 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
14464 {
14465 LONGEST retval = 0;
14466
14467 switch (offset_size)
14468 {
14469 case 4:
14470 retval = bfd_get_32 (abfd, buf);
14471 break;
14472 case 8:
14473 retval = bfd_get_64 (abfd, buf);
14474 break;
14475 default:
14476 internal_error (__FILE__, __LINE__,
14477 _("read_offset_1: bad switch [in module %s]"),
14478 bfd_get_filename (abfd));
14479 }
14480
14481 return retval;
14482 }
14483
14484 static gdb_byte *
14485 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
14486 {
14487 /* If the size of a host char is 8 bits, we can return a pointer
14488 to the buffer, otherwise we have to copy the data to a buffer
14489 allocated on the temporary obstack. */
14490 gdb_assert (HOST_CHAR_BIT == 8);
14491 return buf;
14492 }
14493
14494 static char *
14495 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
14496 {
14497 /* If the size of a host char is 8 bits, we can return a pointer
14498 to the string, otherwise we have to copy the string to a buffer
14499 allocated on the temporary obstack. */
14500 gdb_assert (HOST_CHAR_BIT == 8);
14501 if (*buf == '\0')
14502 {
14503 *bytes_read_ptr = 1;
14504 return NULL;
14505 }
14506 *bytes_read_ptr = strlen ((char *) buf) + 1;
14507 return (char *) buf;
14508 }
14509
14510 static char *
14511 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
14512 {
14513 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
14514 if (dwarf2_per_objfile->str.buffer == NULL)
14515 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
14516 bfd_get_filename (abfd));
14517 if (str_offset >= dwarf2_per_objfile->str.size)
14518 error (_("DW_FORM_strp pointing outside of "
14519 ".debug_str section [in module %s]"),
14520 bfd_get_filename (abfd));
14521 gdb_assert (HOST_CHAR_BIT == 8);
14522 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
14523 return NULL;
14524 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
14525 }
14526
14527 /* Read a string at offset STR_OFFSET in the .debug_str section from
14528 the .dwz file DWZ. Throw an error if the offset is too large. If
14529 the string consists of a single NUL byte, return NULL; otherwise
14530 return a pointer to the string. */
14531
14532 static char *
14533 read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
14534 {
14535 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
14536
14537 if (dwz->str.buffer == NULL)
14538 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
14539 "section [in module %s]"),
14540 bfd_get_filename (dwz->dwz_bfd));
14541 if (str_offset >= dwz->str.size)
14542 error (_("DW_FORM_GNU_strp_alt pointing outside of "
14543 ".debug_str section [in module %s]"),
14544 bfd_get_filename (dwz->dwz_bfd));
14545 gdb_assert (HOST_CHAR_BIT == 8);
14546 if (dwz->str.buffer[str_offset] == '\0')
14547 return NULL;
14548 return (char *) (dwz->str.buffer + str_offset);
14549 }
14550
14551 static char *
14552 read_indirect_string (bfd *abfd, gdb_byte *buf,
14553 const struct comp_unit_head *cu_header,
14554 unsigned int *bytes_read_ptr)
14555 {
14556 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
14557
14558 return read_indirect_string_at_offset (abfd, str_offset);
14559 }
14560
14561 static ULONGEST
14562 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
14563 {
14564 ULONGEST result;
14565 unsigned int num_read;
14566 int i, shift;
14567 unsigned char byte;
14568
14569 result = 0;
14570 shift = 0;
14571 num_read = 0;
14572 i = 0;
14573 while (1)
14574 {
14575 byte = bfd_get_8 (abfd, buf);
14576 buf++;
14577 num_read++;
14578 result |= ((ULONGEST) (byte & 127) << shift);
14579 if ((byte & 128) == 0)
14580 {
14581 break;
14582 }
14583 shift += 7;
14584 }
14585 *bytes_read_ptr = num_read;
14586 return result;
14587 }
14588
14589 static LONGEST
14590 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
14591 {
14592 LONGEST result;
14593 int i, shift, num_read;
14594 unsigned char byte;
14595
14596 result = 0;
14597 shift = 0;
14598 num_read = 0;
14599 i = 0;
14600 while (1)
14601 {
14602 byte = bfd_get_8 (abfd, buf);
14603 buf++;
14604 num_read++;
14605 result |= ((LONGEST) (byte & 127) << shift);
14606 shift += 7;
14607 if ((byte & 128) == 0)
14608 {
14609 break;
14610 }
14611 }
14612 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
14613 result |= -(((LONGEST) 1) << shift);
14614 *bytes_read_ptr = num_read;
14615 return result;
14616 }
14617
14618 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
14619 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
14620 ADDR_SIZE is the size of addresses from the CU header. */
14621
14622 static CORE_ADDR
14623 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
14624 {
14625 struct objfile *objfile = dwarf2_per_objfile->objfile;
14626 bfd *abfd = objfile->obfd;
14627 const gdb_byte *info_ptr;
14628
14629 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
14630 if (dwarf2_per_objfile->addr.buffer == NULL)
14631 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
14632 objfile->name);
14633 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
14634 error (_("DW_FORM_addr_index pointing outside of "
14635 ".debug_addr section [in module %s]"),
14636 objfile->name);
14637 info_ptr = (dwarf2_per_objfile->addr.buffer
14638 + addr_base + addr_index * addr_size);
14639 if (addr_size == 4)
14640 return bfd_get_32 (abfd, info_ptr);
14641 else
14642 return bfd_get_64 (abfd, info_ptr);
14643 }
14644
14645 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
14646
14647 static CORE_ADDR
14648 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
14649 {
14650 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
14651 }
14652
14653 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
14654
14655 static CORE_ADDR
14656 read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
14657 unsigned int *bytes_read)
14658 {
14659 bfd *abfd = cu->objfile->obfd;
14660 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
14661
14662 return read_addr_index (cu, addr_index);
14663 }
14664
14665 /* Data structure to pass results from dwarf2_read_addr_index_reader
14666 back to dwarf2_read_addr_index. */
14667
14668 struct dwarf2_read_addr_index_data
14669 {
14670 ULONGEST addr_base;
14671 int addr_size;
14672 };
14673
14674 /* die_reader_func for dwarf2_read_addr_index. */
14675
14676 static void
14677 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
14678 gdb_byte *info_ptr,
14679 struct die_info *comp_unit_die,
14680 int has_children,
14681 void *data)
14682 {
14683 struct dwarf2_cu *cu = reader->cu;
14684 struct dwarf2_read_addr_index_data *aidata =
14685 (struct dwarf2_read_addr_index_data *) data;
14686
14687 aidata->addr_base = cu->addr_base;
14688 aidata->addr_size = cu->header.addr_size;
14689 }
14690
14691 /* Given an index in .debug_addr, fetch the value.
14692 NOTE: This can be called during dwarf expression evaluation,
14693 long after the debug information has been read, and thus per_cu->cu
14694 may no longer exist. */
14695
14696 CORE_ADDR
14697 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
14698 unsigned int addr_index)
14699 {
14700 struct objfile *objfile = per_cu->objfile;
14701 struct dwarf2_cu *cu = per_cu->cu;
14702 ULONGEST addr_base;
14703 int addr_size;
14704
14705 /* This is intended to be called from outside this file. */
14706 dw2_setup (objfile);
14707
14708 /* We need addr_base and addr_size.
14709 If we don't have PER_CU->cu, we have to get it.
14710 Nasty, but the alternative is storing the needed info in PER_CU,
14711 which at this point doesn't seem justified: it's not clear how frequently
14712 it would get used and it would increase the size of every PER_CU.
14713 Entry points like dwarf2_per_cu_addr_size do a similar thing
14714 so we're not in uncharted territory here.
14715 Alas we need to be a bit more complicated as addr_base is contained
14716 in the DIE.
14717
14718 We don't need to read the entire CU(/TU).
14719 We just need the header and top level die.
14720
14721 IWBN to use the aging mechanism to let us lazily later discard the CU.
14722 For now we skip this optimization. */
14723
14724 if (cu != NULL)
14725 {
14726 addr_base = cu->addr_base;
14727 addr_size = cu->header.addr_size;
14728 }
14729 else
14730 {
14731 struct dwarf2_read_addr_index_data aidata;
14732
14733 /* Note: We can't use init_cutu_and_read_dies_simple here,
14734 we need addr_base. */
14735 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
14736 dwarf2_read_addr_index_reader, &aidata);
14737 addr_base = aidata.addr_base;
14738 addr_size = aidata.addr_size;
14739 }
14740
14741 return read_addr_index_1 (addr_index, addr_base, addr_size);
14742 }
14743
14744 /* Given a DW_AT_str_index, fetch the string. */
14745
14746 static char *
14747 read_str_index (const struct die_reader_specs *reader,
14748 struct dwarf2_cu *cu, ULONGEST str_index)
14749 {
14750 struct objfile *objfile = dwarf2_per_objfile->objfile;
14751 const char *dwo_name = objfile->name;
14752 bfd *abfd = objfile->obfd;
14753 struct dwo_sections *sections = &reader->dwo_file->sections;
14754 gdb_byte *info_ptr;
14755 ULONGEST str_offset;
14756
14757 dwarf2_read_section (objfile, &sections->str);
14758 dwarf2_read_section (objfile, &sections->str_offsets);
14759 if (sections->str.buffer == NULL)
14760 error (_("DW_FORM_str_index used without .debug_str.dwo section"
14761 " in CU at offset 0x%lx [in module %s]"),
14762 (long) cu->header.offset.sect_off, dwo_name);
14763 if (sections->str_offsets.buffer == NULL)
14764 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
14765 " in CU at offset 0x%lx [in module %s]"),
14766 (long) cu->header.offset.sect_off, dwo_name);
14767 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
14768 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
14769 " section in CU at offset 0x%lx [in module %s]"),
14770 (long) cu->header.offset.sect_off, dwo_name);
14771 info_ptr = (sections->str_offsets.buffer
14772 + str_index * cu->header.offset_size);
14773 if (cu->header.offset_size == 4)
14774 str_offset = bfd_get_32 (abfd, info_ptr);
14775 else
14776 str_offset = bfd_get_64 (abfd, info_ptr);
14777 if (str_offset >= sections->str.size)
14778 error (_("Offset from DW_FORM_str_index pointing outside of"
14779 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
14780 (long) cu->header.offset.sect_off, dwo_name);
14781 return (char *) (sections->str.buffer + str_offset);
14782 }
14783
14784 /* Return the length of an LEB128 number in BUF. */
14785
14786 static int
14787 leb128_size (const gdb_byte *buf)
14788 {
14789 const gdb_byte *begin = buf;
14790 gdb_byte byte;
14791
14792 while (1)
14793 {
14794 byte = *buf++;
14795 if ((byte & 128) == 0)
14796 return buf - begin;
14797 }
14798 }
14799
14800 static void
14801 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
14802 {
14803 switch (lang)
14804 {
14805 case DW_LANG_C89:
14806 case DW_LANG_C99:
14807 case DW_LANG_C:
14808 cu->language = language_c;
14809 break;
14810 case DW_LANG_C_plus_plus:
14811 cu->language = language_cplus;
14812 break;
14813 case DW_LANG_D:
14814 cu->language = language_d;
14815 break;
14816 case DW_LANG_Fortran77:
14817 case DW_LANG_Fortran90:
14818 case DW_LANG_Fortran95:
14819 cu->language = language_fortran;
14820 break;
14821 case DW_LANG_Go:
14822 cu->language = language_go;
14823 break;
14824 case DW_LANG_Mips_Assembler:
14825 cu->language = language_asm;
14826 break;
14827 case DW_LANG_Java:
14828 cu->language = language_java;
14829 break;
14830 case DW_LANG_Ada83:
14831 case DW_LANG_Ada95:
14832 cu->language = language_ada;
14833 break;
14834 case DW_LANG_Modula2:
14835 cu->language = language_m2;
14836 break;
14837 case DW_LANG_Pascal83:
14838 cu->language = language_pascal;
14839 break;
14840 case DW_LANG_ObjC:
14841 cu->language = language_objc;
14842 break;
14843 case DW_LANG_Cobol74:
14844 case DW_LANG_Cobol85:
14845 default:
14846 cu->language = language_minimal;
14847 break;
14848 }
14849 cu->language_defn = language_def (cu->language);
14850 }
14851
14852 /* Return the named attribute or NULL if not there. */
14853
14854 static struct attribute *
14855 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
14856 {
14857 for (;;)
14858 {
14859 unsigned int i;
14860 struct attribute *spec = NULL;
14861
14862 for (i = 0; i < die->num_attrs; ++i)
14863 {
14864 if (die->attrs[i].name == name)
14865 return &die->attrs[i];
14866 if (die->attrs[i].name == DW_AT_specification
14867 || die->attrs[i].name == DW_AT_abstract_origin)
14868 spec = &die->attrs[i];
14869 }
14870
14871 if (!spec)
14872 break;
14873
14874 die = follow_die_ref (die, spec, &cu);
14875 }
14876
14877 return NULL;
14878 }
14879
14880 /* Return the named attribute or NULL if not there,
14881 but do not follow DW_AT_specification, etc.
14882 This is for use in contexts where we're reading .debug_types dies.
14883 Following DW_AT_specification, DW_AT_abstract_origin will take us
14884 back up the chain, and we want to go down. */
14885
14886 static struct attribute *
14887 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
14888 {
14889 unsigned int i;
14890
14891 for (i = 0; i < die->num_attrs; ++i)
14892 if (die->attrs[i].name == name)
14893 return &die->attrs[i];
14894
14895 return NULL;
14896 }
14897
14898 /* Return non-zero iff the attribute NAME is defined for the given DIE,
14899 and holds a non-zero value. This function should only be used for
14900 DW_FORM_flag or DW_FORM_flag_present attributes. */
14901
14902 static int
14903 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
14904 {
14905 struct attribute *attr = dwarf2_attr (die, name, cu);
14906
14907 return (attr && DW_UNSND (attr));
14908 }
14909
14910 static int
14911 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
14912 {
14913 /* A DIE is a declaration if it has a DW_AT_declaration attribute
14914 which value is non-zero. However, we have to be careful with
14915 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
14916 (via dwarf2_flag_true_p) follows this attribute. So we may
14917 end up accidently finding a declaration attribute that belongs
14918 to a different DIE referenced by the specification attribute,
14919 even though the given DIE does not have a declaration attribute. */
14920 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
14921 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
14922 }
14923
14924 /* Return the die giving the specification for DIE, if there is
14925 one. *SPEC_CU is the CU containing DIE on input, and the CU
14926 containing the return value on output. If there is no
14927 specification, but there is an abstract origin, that is
14928 returned. */
14929
14930 static struct die_info *
14931 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
14932 {
14933 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
14934 *spec_cu);
14935
14936 if (spec_attr == NULL)
14937 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
14938
14939 if (spec_attr == NULL)
14940 return NULL;
14941 else
14942 return follow_die_ref (die, spec_attr, spec_cu);
14943 }
14944
14945 /* Free the line_header structure *LH, and any arrays and strings it
14946 refers to.
14947 NOTE: This is also used as a "cleanup" function. */
14948
14949 static void
14950 free_line_header (struct line_header *lh)
14951 {
14952 if (lh->standard_opcode_lengths)
14953 xfree (lh->standard_opcode_lengths);
14954
14955 /* Remember that all the lh->file_names[i].name pointers are
14956 pointers into debug_line_buffer, and don't need to be freed. */
14957 if (lh->file_names)
14958 xfree (lh->file_names);
14959
14960 /* Similarly for the include directory names. */
14961 if (lh->include_dirs)
14962 xfree (lh->include_dirs);
14963
14964 xfree (lh);
14965 }
14966
14967 /* Add an entry to LH's include directory table. */
14968
14969 static void
14970 add_include_dir (struct line_header *lh, char *include_dir)
14971 {
14972 /* Grow the array if necessary. */
14973 if (lh->include_dirs_size == 0)
14974 {
14975 lh->include_dirs_size = 1; /* for testing */
14976 lh->include_dirs = xmalloc (lh->include_dirs_size
14977 * sizeof (*lh->include_dirs));
14978 }
14979 else if (lh->num_include_dirs >= lh->include_dirs_size)
14980 {
14981 lh->include_dirs_size *= 2;
14982 lh->include_dirs = xrealloc (lh->include_dirs,
14983 (lh->include_dirs_size
14984 * sizeof (*lh->include_dirs)));
14985 }
14986
14987 lh->include_dirs[lh->num_include_dirs++] = include_dir;
14988 }
14989
14990 /* Add an entry to LH's file name table. */
14991
14992 static void
14993 add_file_name (struct line_header *lh,
14994 char *name,
14995 unsigned int dir_index,
14996 unsigned int mod_time,
14997 unsigned int length)
14998 {
14999 struct file_entry *fe;
15000
15001 /* Grow the array if necessary. */
15002 if (lh->file_names_size == 0)
15003 {
15004 lh->file_names_size = 1; /* for testing */
15005 lh->file_names = xmalloc (lh->file_names_size
15006 * sizeof (*lh->file_names));
15007 }
15008 else if (lh->num_file_names >= lh->file_names_size)
15009 {
15010 lh->file_names_size *= 2;
15011 lh->file_names = xrealloc (lh->file_names,
15012 (lh->file_names_size
15013 * sizeof (*lh->file_names)));
15014 }
15015
15016 fe = &lh->file_names[lh->num_file_names++];
15017 fe->name = name;
15018 fe->dir_index = dir_index;
15019 fe->mod_time = mod_time;
15020 fe->length = length;
15021 fe->included_p = 0;
15022 fe->symtab = NULL;
15023 }
15024
15025 /* A convenience function to find the proper .debug_line section for a
15026 CU. */
15027
15028 static struct dwarf2_section_info *
15029 get_debug_line_section (struct dwarf2_cu *cu)
15030 {
15031 struct dwarf2_section_info *section;
15032
15033 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
15034 DWO file. */
15035 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15036 section = &cu->dwo_unit->dwo_file->sections.line;
15037 else if (cu->per_cu->is_dwz)
15038 {
15039 struct dwz_file *dwz = dwarf2_get_dwz_file ();
15040
15041 section = &dwz->line;
15042 }
15043 else
15044 section = &dwarf2_per_objfile->line;
15045
15046 return section;
15047 }
15048
15049 /* Read the statement program header starting at OFFSET in
15050 .debug_line, or .debug_line.dwo. Return a pointer
15051 to a struct line_header, allocated using xmalloc.
15052
15053 NOTE: the strings in the include directory and file name tables of
15054 the returned object point into the dwarf line section buffer,
15055 and must not be freed. */
15056
15057 static struct line_header *
15058 dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
15059 {
15060 struct cleanup *back_to;
15061 struct line_header *lh;
15062 gdb_byte *line_ptr;
15063 unsigned int bytes_read, offset_size;
15064 int i;
15065 char *cur_dir, *cur_file;
15066 struct dwarf2_section_info *section;
15067 bfd *abfd;
15068
15069 section = get_debug_line_section (cu);
15070 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15071 if (section->buffer == NULL)
15072 {
15073 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15074 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
15075 else
15076 complaint (&symfile_complaints, _("missing .debug_line section"));
15077 return 0;
15078 }
15079
15080 /* We can't do this until we know the section is non-empty.
15081 Only then do we know we have such a section. */
15082 abfd = section->asection->owner;
15083
15084 /* Make sure that at least there's room for the total_length field.
15085 That could be 12 bytes long, but we're just going to fudge that. */
15086 if (offset + 4 >= section->size)
15087 {
15088 dwarf2_statement_list_fits_in_line_number_section_complaint ();
15089 return 0;
15090 }
15091
15092 lh = xmalloc (sizeof (*lh));
15093 memset (lh, 0, sizeof (*lh));
15094 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
15095 (void *) lh);
15096
15097 line_ptr = section->buffer + offset;
15098
15099 /* Read in the header. */
15100 lh->total_length =
15101 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
15102 &bytes_read, &offset_size);
15103 line_ptr += bytes_read;
15104 if (line_ptr + lh->total_length > (section->buffer + section->size))
15105 {
15106 dwarf2_statement_list_fits_in_line_number_section_complaint ();
15107 return 0;
15108 }
15109 lh->statement_program_end = line_ptr + lh->total_length;
15110 lh->version = read_2_bytes (abfd, line_ptr);
15111 line_ptr += 2;
15112 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
15113 line_ptr += offset_size;
15114 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
15115 line_ptr += 1;
15116 if (lh->version >= 4)
15117 {
15118 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
15119 line_ptr += 1;
15120 }
15121 else
15122 lh->maximum_ops_per_instruction = 1;
15123
15124 if (lh->maximum_ops_per_instruction == 0)
15125 {
15126 lh->maximum_ops_per_instruction = 1;
15127 complaint (&symfile_complaints,
15128 _("invalid maximum_ops_per_instruction "
15129 "in `.debug_line' section"));
15130 }
15131
15132 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
15133 line_ptr += 1;
15134 lh->line_base = read_1_signed_byte (abfd, line_ptr);
15135 line_ptr += 1;
15136 lh->line_range = read_1_byte (abfd, line_ptr);
15137 line_ptr += 1;
15138 lh->opcode_base = read_1_byte (abfd, line_ptr);
15139 line_ptr += 1;
15140 lh->standard_opcode_lengths
15141 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
15142
15143 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
15144 for (i = 1; i < lh->opcode_base; ++i)
15145 {
15146 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
15147 line_ptr += 1;
15148 }
15149
15150 /* Read directory table. */
15151 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
15152 {
15153 line_ptr += bytes_read;
15154 add_include_dir (lh, cur_dir);
15155 }
15156 line_ptr += bytes_read;
15157
15158 /* Read file name table. */
15159 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
15160 {
15161 unsigned int dir_index, mod_time, length;
15162
15163 line_ptr += bytes_read;
15164 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15165 line_ptr += bytes_read;
15166 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15167 line_ptr += bytes_read;
15168 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15169 line_ptr += bytes_read;
15170
15171 add_file_name (lh, cur_file, dir_index, mod_time, length);
15172 }
15173 line_ptr += bytes_read;
15174 lh->statement_program_start = line_ptr;
15175
15176 if (line_ptr > (section->buffer + section->size))
15177 complaint (&symfile_complaints,
15178 _("line number info header doesn't "
15179 "fit in `.debug_line' section"));
15180
15181 discard_cleanups (back_to);
15182 return lh;
15183 }
15184
15185 /* Subroutine of dwarf_decode_lines to simplify it.
15186 Return the file name of the psymtab for included file FILE_INDEX
15187 in line header LH of PST.
15188 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15189 If space for the result is malloc'd, it will be freed by a cleanup.
15190 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
15191
15192 The function creates dangling cleanup registration. */
15193
15194 static char *
15195 psymtab_include_file_name (const struct line_header *lh, int file_index,
15196 const struct partial_symtab *pst,
15197 const char *comp_dir)
15198 {
15199 const struct file_entry fe = lh->file_names [file_index];
15200 char *include_name = fe.name;
15201 char *include_name_to_compare = include_name;
15202 char *dir_name = NULL;
15203 const char *pst_filename;
15204 char *copied_name = NULL;
15205 int file_is_pst;
15206
15207 if (fe.dir_index)
15208 dir_name = lh->include_dirs[fe.dir_index - 1];
15209
15210 if (!IS_ABSOLUTE_PATH (include_name)
15211 && (dir_name != NULL || comp_dir != NULL))
15212 {
15213 /* Avoid creating a duplicate psymtab for PST.
15214 We do this by comparing INCLUDE_NAME and PST_FILENAME.
15215 Before we do the comparison, however, we need to account
15216 for DIR_NAME and COMP_DIR.
15217 First prepend dir_name (if non-NULL). If we still don't
15218 have an absolute path prepend comp_dir (if non-NULL).
15219 However, the directory we record in the include-file's
15220 psymtab does not contain COMP_DIR (to match the
15221 corresponding symtab(s)).
15222
15223 Example:
15224
15225 bash$ cd /tmp
15226 bash$ gcc -g ./hello.c
15227 include_name = "hello.c"
15228 dir_name = "."
15229 DW_AT_comp_dir = comp_dir = "/tmp"
15230 DW_AT_name = "./hello.c" */
15231
15232 if (dir_name != NULL)
15233 {
15234 include_name = concat (dir_name, SLASH_STRING,
15235 include_name, (char *)NULL);
15236 include_name_to_compare = include_name;
15237 make_cleanup (xfree, include_name);
15238 }
15239 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
15240 {
15241 include_name_to_compare = concat (comp_dir, SLASH_STRING,
15242 include_name, (char *)NULL);
15243 }
15244 }
15245
15246 pst_filename = pst->filename;
15247 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
15248 {
15249 copied_name = concat (pst->dirname, SLASH_STRING,
15250 pst_filename, (char *)NULL);
15251 pst_filename = copied_name;
15252 }
15253
15254 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
15255
15256 if (include_name_to_compare != include_name)
15257 xfree (include_name_to_compare);
15258 if (copied_name != NULL)
15259 xfree (copied_name);
15260
15261 if (file_is_pst)
15262 return NULL;
15263 return include_name;
15264 }
15265
15266 /* Ignore this record_line request. */
15267
15268 static void
15269 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
15270 {
15271 return;
15272 }
15273
15274 /* Subroutine of dwarf_decode_lines to simplify it.
15275 Process the line number information in LH. */
15276
15277 static void
15278 dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
15279 struct dwarf2_cu *cu, struct partial_symtab *pst)
15280 {
15281 gdb_byte *line_ptr, *extended_end;
15282 gdb_byte *line_end;
15283 unsigned int bytes_read, extended_len;
15284 unsigned char op_code, extended_op, adj_opcode;
15285 CORE_ADDR baseaddr;
15286 struct objfile *objfile = cu->objfile;
15287 bfd *abfd = objfile->obfd;
15288 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15289 const int decode_for_pst_p = (pst != NULL);
15290 struct subfile *last_subfile = NULL;
15291 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
15292 = record_line;
15293
15294 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15295
15296 line_ptr = lh->statement_program_start;
15297 line_end = lh->statement_program_end;
15298
15299 /* Read the statement sequences until there's nothing left. */
15300 while (line_ptr < line_end)
15301 {
15302 /* state machine registers */
15303 CORE_ADDR address = 0;
15304 unsigned int file = 1;
15305 unsigned int line = 1;
15306 unsigned int column = 0;
15307 int is_stmt = lh->default_is_stmt;
15308 int basic_block = 0;
15309 int end_sequence = 0;
15310 CORE_ADDR addr;
15311 unsigned char op_index = 0;
15312
15313 if (!decode_for_pst_p && lh->num_file_names >= file)
15314 {
15315 /* Start a subfile for the current file of the state machine. */
15316 /* lh->include_dirs and lh->file_names are 0-based, but the
15317 directory and file name numbers in the statement program
15318 are 1-based. */
15319 struct file_entry *fe = &lh->file_names[file - 1];
15320 char *dir = NULL;
15321
15322 if (fe->dir_index)
15323 dir = lh->include_dirs[fe->dir_index - 1];
15324
15325 dwarf2_start_subfile (fe->name, dir, comp_dir);
15326 }
15327
15328 /* Decode the table. */
15329 while (!end_sequence)
15330 {
15331 op_code = read_1_byte (abfd, line_ptr);
15332 line_ptr += 1;
15333 if (line_ptr > line_end)
15334 {
15335 dwarf2_debug_line_missing_end_sequence_complaint ();
15336 break;
15337 }
15338
15339 if (op_code >= lh->opcode_base)
15340 {
15341 /* Special operand. */
15342 adj_opcode = op_code - lh->opcode_base;
15343 address += (((op_index + (adj_opcode / lh->line_range))
15344 / lh->maximum_ops_per_instruction)
15345 * lh->minimum_instruction_length);
15346 op_index = ((op_index + (adj_opcode / lh->line_range))
15347 % lh->maximum_ops_per_instruction);
15348 line += lh->line_base + (adj_opcode % lh->line_range);
15349 if (lh->num_file_names < file || file == 0)
15350 dwarf2_debug_line_missing_file_complaint ();
15351 /* For now we ignore lines not starting on an
15352 instruction boundary. */
15353 else if (op_index == 0)
15354 {
15355 lh->file_names[file - 1].included_p = 1;
15356 if (!decode_for_pst_p && is_stmt)
15357 {
15358 if (last_subfile != current_subfile)
15359 {
15360 addr = gdbarch_addr_bits_remove (gdbarch, address);
15361 if (last_subfile)
15362 (*p_record_line) (last_subfile, 0, addr);
15363 last_subfile = current_subfile;
15364 }
15365 /* Append row to matrix using current values. */
15366 addr = gdbarch_addr_bits_remove (gdbarch, address);
15367 (*p_record_line) (current_subfile, line, addr);
15368 }
15369 }
15370 basic_block = 0;
15371 }
15372 else switch (op_code)
15373 {
15374 case DW_LNS_extended_op:
15375 extended_len = read_unsigned_leb128 (abfd, line_ptr,
15376 &bytes_read);
15377 line_ptr += bytes_read;
15378 extended_end = line_ptr + extended_len;
15379 extended_op = read_1_byte (abfd, line_ptr);
15380 line_ptr += 1;
15381 switch (extended_op)
15382 {
15383 case DW_LNE_end_sequence:
15384 p_record_line = record_line;
15385 end_sequence = 1;
15386 break;
15387 case DW_LNE_set_address:
15388 address = read_address (abfd, line_ptr, cu, &bytes_read);
15389
15390 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
15391 {
15392 /* This line table is for a function which has been
15393 GCd by the linker. Ignore it. PR gdb/12528 */
15394
15395 long line_offset
15396 = line_ptr - get_debug_line_section (cu)->buffer;
15397
15398 complaint (&symfile_complaints,
15399 _(".debug_line address at offset 0x%lx is 0 "
15400 "[in module %s]"),
15401 line_offset, objfile->name);
15402 p_record_line = noop_record_line;
15403 }
15404
15405 op_index = 0;
15406 line_ptr += bytes_read;
15407 address += baseaddr;
15408 break;
15409 case DW_LNE_define_file:
15410 {
15411 char *cur_file;
15412 unsigned int dir_index, mod_time, length;
15413
15414 cur_file = read_direct_string (abfd, line_ptr,
15415 &bytes_read);
15416 line_ptr += bytes_read;
15417 dir_index =
15418 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15419 line_ptr += bytes_read;
15420 mod_time =
15421 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15422 line_ptr += bytes_read;
15423 length =
15424 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15425 line_ptr += bytes_read;
15426 add_file_name (lh, cur_file, dir_index, mod_time, length);
15427 }
15428 break;
15429 case DW_LNE_set_discriminator:
15430 /* The discriminator is not interesting to the debugger;
15431 just ignore it. */
15432 line_ptr = extended_end;
15433 break;
15434 default:
15435 complaint (&symfile_complaints,
15436 _("mangled .debug_line section"));
15437 return;
15438 }
15439 /* Make sure that we parsed the extended op correctly. If e.g.
15440 we expected a different address size than the producer used,
15441 we may have read the wrong number of bytes. */
15442 if (line_ptr != extended_end)
15443 {
15444 complaint (&symfile_complaints,
15445 _("mangled .debug_line section"));
15446 return;
15447 }
15448 break;
15449 case DW_LNS_copy:
15450 if (lh->num_file_names < file || file == 0)
15451 dwarf2_debug_line_missing_file_complaint ();
15452 else
15453 {
15454 lh->file_names[file - 1].included_p = 1;
15455 if (!decode_for_pst_p && is_stmt)
15456 {
15457 if (last_subfile != current_subfile)
15458 {
15459 addr = gdbarch_addr_bits_remove (gdbarch, address);
15460 if (last_subfile)
15461 (*p_record_line) (last_subfile, 0, addr);
15462 last_subfile = current_subfile;
15463 }
15464 addr = gdbarch_addr_bits_remove (gdbarch, address);
15465 (*p_record_line) (current_subfile, line, addr);
15466 }
15467 }
15468 basic_block = 0;
15469 break;
15470 case DW_LNS_advance_pc:
15471 {
15472 CORE_ADDR adjust
15473 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15474
15475 address += (((op_index + adjust)
15476 / lh->maximum_ops_per_instruction)
15477 * lh->minimum_instruction_length);
15478 op_index = ((op_index + adjust)
15479 % lh->maximum_ops_per_instruction);
15480 line_ptr += bytes_read;
15481 }
15482 break;
15483 case DW_LNS_advance_line:
15484 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
15485 line_ptr += bytes_read;
15486 break;
15487 case DW_LNS_set_file:
15488 {
15489 /* The arrays lh->include_dirs and lh->file_names are
15490 0-based, but the directory and file name numbers in
15491 the statement program are 1-based. */
15492 struct file_entry *fe;
15493 char *dir = NULL;
15494
15495 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15496 line_ptr += bytes_read;
15497 if (lh->num_file_names < file || file == 0)
15498 dwarf2_debug_line_missing_file_complaint ();
15499 else
15500 {
15501 fe = &lh->file_names[file - 1];
15502 if (fe->dir_index)
15503 dir = lh->include_dirs[fe->dir_index - 1];
15504 if (!decode_for_pst_p)
15505 {
15506 last_subfile = current_subfile;
15507 dwarf2_start_subfile (fe->name, dir, comp_dir);
15508 }
15509 }
15510 }
15511 break;
15512 case DW_LNS_set_column:
15513 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15514 line_ptr += bytes_read;
15515 break;
15516 case DW_LNS_negate_stmt:
15517 is_stmt = (!is_stmt);
15518 break;
15519 case DW_LNS_set_basic_block:
15520 basic_block = 1;
15521 break;
15522 /* Add to the address register of the state machine the
15523 address increment value corresponding to special opcode
15524 255. I.e., this value is scaled by the minimum
15525 instruction length since special opcode 255 would have
15526 scaled the increment. */
15527 case DW_LNS_const_add_pc:
15528 {
15529 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
15530
15531 address += (((op_index + adjust)
15532 / lh->maximum_ops_per_instruction)
15533 * lh->minimum_instruction_length);
15534 op_index = ((op_index + adjust)
15535 % lh->maximum_ops_per_instruction);
15536 }
15537 break;
15538 case DW_LNS_fixed_advance_pc:
15539 address += read_2_bytes (abfd, line_ptr);
15540 op_index = 0;
15541 line_ptr += 2;
15542 break;
15543 default:
15544 {
15545 /* Unknown standard opcode, ignore it. */
15546 int i;
15547
15548 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
15549 {
15550 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15551 line_ptr += bytes_read;
15552 }
15553 }
15554 }
15555 }
15556 if (lh->num_file_names < file || file == 0)
15557 dwarf2_debug_line_missing_file_complaint ();
15558 else
15559 {
15560 lh->file_names[file - 1].included_p = 1;
15561 if (!decode_for_pst_p)
15562 {
15563 addr = gdbarch_addr_bits_remove (gdbarch, address);
15564 (*p_record_line) (current_subfile, 0, addr);
15565 }
15566 }
15567 }
15568 }
15569
15570 /* Decode the Line Number Program (LNP) for the given line_header
15571 structure and CU. The actual information extracted and the type
15572 of structures created from the LNP depends on the value of PST.
15573
15574 1. If PST is NULL, then this procedure uses the data from the program
15575 to create all necessary symbol tables, and their linetables.
15576
15577 2. If PST is not NULL, this procedure reads the program to determine
15578 the list of files included by the unit represented by PST, and
15579 builds all the associated partial symbol tables.
15580
15581 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15582 It is used for relative paths in the line table.
15583 NOTE: When processing partial symtabs (pst != NULL),
15584 comp_dir == pst->dirname.
15585
15586 NOTE: It is important that psymtabs have the same file name (via strcmp)
15587 as the corresponding symtab. Since COMP_DIR is not used in the name of the
15588 symtab we don't use it in the name of the psymtabs we create.
15589 E.g. expand_line_sal requires this when finding psymtabs to expand.
15590 A good testcase for this is mb-inline.exp. */
15591
15592 static void
15593 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
15594 struct dwarf2_cu *cu, struct partial_symtab *pst,
15595 int want_line_info)
15596 {
15597 struct objfile *objfile = cu->objfile;
15598 const int decode_for_pst_p = (pst != NULL);
15599 struct subfile *first_subfile = current_subfile;
15600
15601 if (want_line_info)
15602 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
15603
15604 if (decode_for_pst_p)
15605 {
15606 int file_index;
15607
15608 /* Now that we're done scanning the Line Header Program, we can
15609 create the psymtab of each included file. */
15610 for (file_index = 0; file_index < lh->num_file_names; file_index++)
15611 if (lh->file_names[file_index].included_p == 1)
15612 {
15613 char *include_name =
15614 psymtab_include_file_name (lh, file_index, pst, comp_dir);
15615 if (include_name != NULL)
15616 dwarf2_create_include_psymtab (include_name, pst, objfile);
15617 }
15618 }
15619 else
15620 {
15621 /* Make sure a symtab is created for every file, even files
15622 which contain only variables (i.e. no code with associated
15623 line numbers). */
15624 int i;
15625
15626 for (i = 0; i < lh->num_file_names; i++)
15627 {
15628 char *dir = NULL;
15629 struct file_entry *fe;
15630
15631 fe = &lh->file_names[i];
15632 if (fe->dir_index)
15633 dir = lh->include_dirs[fe->dir_index - 1];
15634 dwarf2_start_subfile (fe->name, dir, comp_dir);
15635
15636 /* Skip the main file; we don't need it, and it must be
15637 allocated last, so that it will show up before the
15638 non-primary symtabs in the objfile's symtab list. */
15639 if (current_subfile == first_subfile)
15640 continue;
15641
15642 if (current_subfile->symtab == NULL)
15643 current_subfile->symtab = allocate_symtab (current_subfile->name,
15644 objfile);
15645 fe->symtab = current_subfile->symtab;
15646 }
15647 }
15648 }
15649
15650 /* Start a subfile for DWARF. FILENAME is the name of the file and
15651 DIRNAME the name of the source directory which contains FILENAME
15652 or NULL if not known. COMP_DIR is the compilation directory for the
15653 linetable's compilation unit or NULL if not known.
15654 This routine tries to keep line numbers from identical absolute and
15655 relative file names in a common subfile.
15656
15657 Using the `list' example from the GDB testsuite, which resides in
15658 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
15659 of /srcdir/list0.c yields the following debugging information for list0.c:
15660
15661 DW_AT_name: /srcdir/list0.c
15662 DW_AT_comp_dir: /compdir
15663 files.files[0].name: list0.h
15664 files.files[0].dir: /srcdir
15665 files.files[1].name: list0.c
15666 files.files[1].dir: /srcdir
15667
15668 The line number information for list0.c has to end up in a single
15669 subfile, so that `break /srcdir/list0.c:1' works as expected.
15670 start_subfile will ensure that this happens provided that we pass the
15671 concatenation of files.files[1].dir and files.files[1].name as the
15672 subfile's name. */
15673
15674 static void
15675 dwarf2_start_subfile (char *filename, const char *dirname,
15676 const char *comp_dir)
15677 {
15678 char *fullname;
15679
15680 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
15681 `start_symtab' will always pass the contents of DW_AT_comp_dir as
15682 second argument to start_subfile. To be consistent, we do the
15683 same here. In order not to lose the line information directory,
15684 we concatenate it to the filename when it makes sense.
15685 Note that the Dwarf3 standard says (speaking of filenames in line
15686 information): ``The directory index is ignored for file names
15687 that represent full path names''. Thus ignoring dirname in the
15688 `else' branch below isn't an issue. */
15689
15690 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
15691 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
15692 else
15693 fullname = filename;
15694
15695 start_subfile (fullname, comp_dir);
15696
15697 if (fullname != filename)
15698 xfree (fullname);
15699 }
15700
15701 /* Start a symtab for DWARF.
15702 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
15703
15704 static void
15705 dwarf2_start_symtab (struct dwarf2_cu *cu,
15706 const char *name, const char *comp_dir, CORE_ADDR low_pc)
15707 {
15708 start_symtab (name, comp_dir, low_pc);
15709 record_debugformat ("DWARF 2");
15710 record_producer (cu->producer);
15711
15712 /* We assume that we're processing GCC output. */
15713 processing_gcc_compilation = 2;
15714
15715 cu->processing_has_namespace_info = 0;
15716 }
15717
15718 static void
15719 var_decode_location (struct attribute *attr, struct symbol *sym,
15720 struct dwarf2_cu *cu)
15721 {
15722 struct objfile *objfile = cu->objfile;
15723 struct comp_unit_head *cu_header = &cu->header;
15724
15725 /* NOTE drow/2003-01-30: There used to be a comment and some special
15726 code here to turn a symbol with DW_AT_external and a
15727 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
15728 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
15729 with some versions of binutils) where shared libraries could have
15730 relocations against symbols in their debug information - the
15731 minimal symbol would have the right address, but the debug info
15732 would not. It's no longer necessary, because we will explicitly
15733 apply relocations when we read in the debug information now. */
15734
15735 /* A DW_AT_location attribute with no contents indicates that a
15736 variable has been optimized away. */
15737 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
15738 {
15739 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
15740 return;
15741 }
15742
15743 /* Handle one degenerate form of location expression specially, to
15744 preserve GDB's previous behavior when section offsets are
15745 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
15746 then mark this symbol as LOC_STATIC. */
15747
15748 if (attr_form_is_block (attr)
15749 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
15750 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
15751 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
15752 && (DW_BLOCK (attr)->size
15753 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
15754 {
15755 unsigned int dummy;
15756
15757 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
15758 SYMBOL_VALUE_ADDRESS (sym) =
15759 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
15760 else
15761 SYMBOL_VALUE_ADDRESS (sym) =
15762 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
15763 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
15764 fixup_symbol_section (sym, objfile);
15765 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
15766 SYMBOL_SECTION (sym));
15767 return;
15768 }
15769
15770 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
15771 expression evaluator, and use LOC_COMPUTED only when necessary
15772 (i.e. when the value of a register or memory location is
15773 referenced, or a thread-local block, etc.). Then again, it might
15774 not be worthwhile. I'm assuming that it isn't unless performance
15775 or memory numbers show me otherwise. */
15776
15777 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
15778
15779 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
15780 cu->has_loclist = 1;
15781 }
15782
15783 /* Given a pointer to a DWARF information entry, figure out if we need
15784 to make a symbol table entry for it, and if so, create a new entry
15785 and return a pointer to it.
15786 If TYPE is NULL, determine symbol type from the die, otherwise
15787 used the passed type.
15788 If SPACE is not NULL, use it to hold the new symbol. If it is
15789 NULL, allocate a new symbol on the objfile's obstack. */
15790
15791 static struct symbol *
15792 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
15793 struct symbol *space)
15794 {
15795 struct objfile *objfile = cu->objfile;
15796 struct symbol *sym = NULL;
15797 const char *name;
15798 struct attribute *attr = NULL;
15799 struct attribute *attr2 = NULL;
15800 CORE_ADDR baseaddr;
15801 struct pending **list_to_add = NULL;
15802
15803 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
15804
15805 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15806
15807 name = dwarf2_name (die, cu);
15808 if (name)
15809 {
15810 const char *linkagename;
15811 int suppress_add = 0;
15812
15813 if (space)
15814 sym = space;
15815 else
15816 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
15817 OBJSTAT (objfile, n_syms++);
15818
15819 /* Cache this symbol's name and the name's demangled form (if any). */
15820 SYMBOL_SET_LANGUAGE (sym, cu->language);
15821 linkagename = dwarf2_physname (name, die, cu);
15822 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
15823
15824 /* Fortran does not have mangling standard and the mangling does differ
15825 between gfortran, iFort etc. */
15826 if (cu->language == language_fortran
15827 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
15828 symbol_set_demangled_name (&(sym->ginfo),
15829 dwarf2_full_name (name, die, cu),
15830 NULL);
15831
15832 /* Default assumptions.
15833 Use the passed type or decode it from the die. */
15834 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
15835 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
15836 if (type != NULL)
15837 SYMBOL_TYPE (sym) = type;
15838 else
15839 SYMBOL_TYPE (sym) = die_type (die, cu);
15840 attr = dwarf2_attr (die,
15841 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
15842 cu);
15843 if (attr)
15844 {
15845 SYMBOL_LINE (sym) = DW_UNSND (attr);
15846 }
15847
15848 attr = dwarf2_attr (die,
15849 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
15850 cu);
15851 if (attr)
15852 {
15853 int file_index = DW_UNSND (attr);
15854
15855 if (cu->line_header == NULL
15856 || file_index > cu->line_header->num_file_names)
15857 complaint (&symfile_complaints,
15858 _("file index out of range"));
15859 else if (file_index > 0)
15860 {
15861 struct file_entry *fe;
15862
15863 fe = &cu->line_header->file_names[file_index - 1];
15864 SYMBOL_SYMTAB (sym) = fe->symtab;
15865 }
15866 }
15867
15868 switch (die->tag)
15869 {
15870 case DW_TAG_label:
15871 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
15872 if (attr)
15873 {
15874 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
15875 }
15876 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
15877 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
15878 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
15879 add_symbol_to_list (sym, cu->list_in_scope);
15880 break;
15881 case DW_TAG_subprogram:
15882 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15883 finish_block. */
15884 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
15885 attr2 = dwarf2_attr (die, DW_AT_external, cu);
15886 if ((attr2 && (DW_UNSND (attr2) != 0))
15887 || cu->language == language_ada)
15888 {
15889 /* Subprograms marked external are stored as a global symbol.
15890 Ada subprograms, whether marked external or not, are always
15891 stored as a global symbol, because we want to be able to
15892 access them globally. For instance, we want to be able
15893 to break on a nested subprogram without having to
15894 specify the context. */
15895 list_to_add = &global_symbols;
15896 }
15897 else
15898 {
15899 list_to_add = cu->list_in_scope;
15900 }
15901 break;
15902 case DW_TAG_inlined_subroutine:
15903 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15904 finish_block. */
15905 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
15906 SYMBOL_INLINED (sym) = 1;
15907 list_to_add = cu->list_in_scope;
15908 break;
15909 case DW_TAG_template_value_param:
15910 suppress_add = 1;
15911 /* Fall through. */
15912 case DW_TAG_constant:
15913 case DW_TAG_variable:
15914 case DW_TAG_member:
15915 /* Compilation with minimal debug info may result in
15916 variables with missing type entries. Change the
15917 misleading `void' type to something sensible. */
15918 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
15919 SYMBOL_TYPE (sym)
15920 = objfile_type (objfile)->nodebug_data_symbol;
15921
15922 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15923 /* In the case of DW_TAG_member, we should only be called for
15924 static const members. */
15925 if (die->tag == DW_TAG_member)
15926 {
15927 /* dwarf2_add_field uses die_is_declaration,
15928 so we do the same. */
15929 gdb_assert (die_is_declaration (die, cu));
15930 gdb_assert (attr);
15931 }
15932 if (attr)
15933 {
15934 dwarf2_const_value (attr, sym, cu);
15935 attr2 = dwarf2_attr (die, DW_AT_external, cu);
15936 if (!suppress_add)
15937 {
15938 if (attr2 && (DW_UNSND (attr2) != 0))
15939 list_to_add = &global_symbols;
15940 else
15941 list_to_add = cu->list_in_scope;
15942 }
15943 break;
15944 }
15945 attr = dwarf2_attr (die, DW_AT_location, cu);
15946 if (attr)
15947 {
15948 var_decode_location (attr, sym, cu);
15949 attr2 = dwarf2_attr (die, DW_AT_external, cu);
15950
15951 /* Fortran explicitly imports any global symbols to the local
15952 scope by DW_TAG_common_block. */
15953 if (cu->language == language_fortran && die->parent
15954 && die->parent->tag == DW_TAG_common_block)
15955 attr2 = NULL;
15956
15957 if (SYMBOL_CLASS (sym) == LOC_STATIC
15958 && SYMBOL_VALUE_ADDRESS (sym) == 0
15959 && !dwarf2_per_objfile->has_section_at_zero)
15960 {
15961 /* When a static variable is eliminated by the linker,
15962 the corresponding debug information is not stripped
15963 out, but the variable address is set to null;
15964 do not add such variables into symbol table. */
15965 }
15966 else if (attr2 && (DW_UNSND (attr2) != 0))
15967 {
15968 /* Workaround gfortran PR debug/40040 - it uses
15969 DW_AT_location for variables in -fPIC libraries which may
15970 get overriden by other libraries/executable and get
15971 a different address. Resolve it by the minimal symbol
15972 which may come from inferior's executable using copy
15973 relocation. Make this workaround only for gfortran as for
15974 other compilers GDB cannot guess the minimal symbol
15975 Fortran mangling kind. */
15976 if (cu->language == language_fortran && die->parent
15977 && die->parent->tag == DW_TAG_module
15978 && cu->producer
15979 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
15980 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
15981
15982 /* A variable with DW_AT_external is never static,
15983 but it may be block-scoped. */
15984 list_to_add = (cu->list_in_scope == &file_symbols
15985 ? &global_symbols : cu->list_in_scope);
15986 }
15987 else
15988 list_to_add = cu->list_in_scope;
15989 }
15990 else
15991 {
15992 /* We do not know the address of this symbol.
15993 If it is an external symbol and we have type information
15994 for it, enter the symbol as a LOC_UNRESOLVED symbol.
15995 The address of the variable will then be determined from
15996 the minimal symbol table whenever the variable is
15997 referenced. */
15998 attr2 = dwarf2_attr (die, DW_AT_external, cu);
15999
16000 /* Fortran explicitly imports any global symbols to the local
16001 scope by DW_TAG_common_block. */
16002 if (cu->language == language_fortran && die->parent
16003 && die->parent->tag == DW_TAG_common_block)
16004 {
16005 /* SYMBOL_CLASS doesn't matter here because
16006 read_common_block is going to reset it. */
16007 if (!suppress_add)
16008 list_to_add = cu->list_in_scope;
16009 }
16010 else if (attr2 && (DW_UNSND (attr2) != 0)
16011 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
16012 {
16013 /* A variable with DW_AT_external is never static, but it
16014 may be block-scoped. */
16015 list_to_add = (cu->list_in_scope == &file_symbols
16016 ? &global_symbols : cu->list_in_scope);
16017
16018 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
16019 }
16020 else if (!die_is_declaration (die, cu))
16021 {
16022 /* Use the default LOC_OPTIMIZED_OUT class. */
16023 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
16024 if (!suppress_add)
16025 list_to_add = cu->list_in_scope;
16026 }
16027 }
16028 break;
16029 case DW_TAG_formal_parameter:
16030 /* If we are inside a function, mark this as an argument. If
16031 not, we might be looking at an argument to an inlined function
16032 when we do not have enough information to show inlined frames;
16033 pretend it's a local variable in that case so that the user can
16034 still see it. */
16035 if (context_stack_depth > 0
16036 && context_stack[context_stack_depth - 1].name != NULL)
16037 SYMBOL_IS_ARGUMENT (sym) = 1;
16038 attr = dwarf2_attr (die, DW_AT_location, cu);
16039 if (attr)
16040 {
16041 var_decode_location (attr, sym, cu);
16042 }
16043 attr = dwarf2_attr (die, DW_AT_const_value, cu);
16044 if (attr)
16045 {
16046 dwarf2_const_value (attr, sym, cu);
16047 }
16048
16049 list_to_add = cu->list_in_scope;
16050 break;
16051 case DW_TAG_unspecified_parameters:
16052 /* From varargs functions; gdb doesn't seem to have any
16053 interest in this information, so just ignore it for now.
16054 (FIXME?) */
16055 break;
16056 case DW_TAG_template_type_param:
16057 suppress_add = 1;
16058 /* Fall through. */
16059 case DW_TAG_class_type:
16060 case DW_TAG_interface_type:
16061 case DW_TAG_structure_type:
16062 case DW_TAG_union_type:
16063 case DW_TAG_set_type:
16064 case DW_TAG_enumeration_type:
16065 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16066 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
16067
16068 {
16069 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
16070 really ever be static objects: otherwise, if you try
16071 to, say, break of a class's method and you're in a file
16072 which doesn't mention that class, it won't work unless
16073 the check for all static symbols in lookup_symbol_aux
16074 saves you. See the OtherFileClass tests in
16075 gdb.c++/namespace.exp. */
16076
16077 if (!suppress_add)
16078 {
16079 list_to_add = (cu->list_in_scope == &file_symbols
16080 && (cu->language == language_cplus
16081 || cu->language == language_java)
16082 ? &global_symbols : cu->list_in_scope);
16083
16084 /* The semantics of C++ state that "struct foo {
16085 ... }" also defines a typedef for "foo". A Java
16086 class declaration also defines a typedef for the
16087 class. */
16088 if (cu->language == language_cplus
16089 || cu->language == language_java
16090 || cu->language == language_ada)
16091 {
16092 /* The symbol's name is already allocated along
16093 with this objfile, so we don't need to
16094 duplicate it for the type. */
16095 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
16096 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
16097 }
16098 }
16099 }
16100 break;
16101 case DW_TAG_typedef:
16102 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16103 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
16104 list_to_add = cu->list_in_scope;
16105 break;
16106 case DW_TAG_base_type:
16107 case DW_TAG_subrange_type:
16108 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16109 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
16110 list_to_add = cu->list_in_scope;
16111 break;
16112 case DW_TAG_enumerator:
16113 attr = dwarf2_attr (die, DW_AT_const_value, cu);
16114 if (attr)
16115 {
16116 dwarf2_const_value (attr, sym, cu);
16117 }
16118 {
16119 /* NOTE: carlton/2003-11-10: See comment above in the
16120 DW_TAG_class_type, etc. block. */
16121
16122 list_to_add = (cu->list_in_scope == &file_symbols
16123 && (cu->language == language_cplus
16124 || cu->language == language_java)
16125 ? &global_symbols : cu->list_in_scope);
16126 }
16127 break;
16128 case DW_TAG_namespace:
16129 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16130 list_to_add = &global_symbols;
16131 break;
16132 case DW_TAG_common_block:
16133 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
16134 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
16135 add_symbol_to_list (sym, cu->list_in_scope);
16136 break;
16137 default:
16138 /* Not a tag we recognize. Hopefully we aren't processing
16139 trash data, but since we must specifically ignore things
16140 we don't recognize, there is nothing else we should do at
16141 this point. */
16142 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
16143 dwarf_tag_name (die->tag));
16144 break;
16145 }
16146
16147 if (suppress_add)
16148 {
16149 sym->hash_next = objfile->template_symbols;
16150 objfile->template_symbols = sym;
16151 list_to_add = NULL;
16152 }
16153
16154 if (list_to_add != NULL)
16155 add_symbol_to_list (sym, list_to_add);
16156
16157 /* For the benefit of old versions of GCC, check for anonymous
16158 namespaces based on the demangled name. */
16159 if (!cu->processing_has_namespace_info
16160 && cu->language == language_cplus)
16161 cp_scan_for_anonymous_namespaces (sym, objfile);
16162 }
16163 return (sym);
16164 }
16165
16166 /* A wrapper for new_symbol_full that always allocates a new symbol. */
16167
16168 static struct symbol *
16169 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16170 {
16171 return new_symbol_full (die, type, cu, NULL);
16172 }
16173
16174 /* Given an attr with a DW_FORM_dataN value in host byte order,
16175 zero-extend it as appropriate for the symbol's type. The DWARF
16176 standard (v4) is not entirely clear about the meaning of using
16177 DW_FORM_dataN for a constant with a signed type, where the type is
16178 wider than the data. The conclusion of a discussion on the DWARF
16179 list was that this is unspecified. We choose to always zero-extend
16180 because that is the interpretation long in use by GCC. */
16181
16182 static gdb_byte *
16183 dwarf2_const_value_data (struct attribute *attr, struct type *type,
16184 const char *name, struct obstack *obstack,
16185 struct dwarf2_cu *cu, LONGEST *value, int bits)
16186 {
16187 struct objfile *objfile = cu->objfile;
16188 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
16189 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
16190 LONGEST l = DW_UNSND (attr);
16191
16192 if (bits < sizeof (*value) * 8)
16193 {
16194 l &= ((LONGEST) 1 << bits) - 1;
16195 *value = l;
16196 }
16197 else if (bits == sizeof (*value) * 8)
16198 *value = l;
16199 else
16200 {
16201 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
16202 store_unsigned_integer (bytes, bits / 8, byte_order, l);
16203 return bytes;
16204 }
16205
16206 return NULL;
16207 }
16208
16209 /* Read a constant value from an attribute. Either set *VALUE, or if
16210 the value does not fit in *VALUE, set *BYTES - either already
16211 allocated on the objfile obstack, or newly allocated on OBSTACK,
16212 or, set *BATON, if we translated the constant to a location
16213 expression. */
16214
16215 static void
16216 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
16217 const char *name, struct obstack *obstack,
16218 struct dwarf2_cu *cu,
16219 LONGEST *value, gdb_byte **bytes,
16220 struct dwarf2_locexpr_baton **baton)
16221 {
16222 struct objfile *objfile = cu->objfile;
16223 struct comp_unit_head *cu_header = &cu->header;
16224 struct dwarf_block *blk;
16225 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
16226 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
16227
16228 *value = 0;
16229 *bytes = NULL;
16230 *baton = NULL;
16231
16232 switch (attr->form)
16233 {
16234 case DW_FORM_addr:
16235 case DW_FORM_GNU_addr_index:
16236 {
16237 gdb_byte *data;
16238
16239 if (TYPE_LENGTH (type) != cu_header->addr_size)
16240 dwarf2_const_value_length_mismatch_complaint (name,
16241 cu_header->addr_size,
16242 TYPE_LENGTH (type));
16243 /* Symbols of this form are reasonably rare, so we just
16244 piggyback on the existing location code rather than writing
16245 a new implementation of symbol_computed_ops. */
16246 *baton = obstack_alloc (&objfile->objfile_obstack,
16247 sizeof (struct dwarf2_locexpr_baton));
16248 (*baton)->per_cu = cu->per_cu;
16249 gdb_assert ((*baton)->per_cu);
16250
16251 (*baton)->size = 2 + cu_header->addr_size;
16252 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
16253 (*baton)->data = data;
16254
16255 data[0] = DW_OP_addr;
16256 store_unsigned_integer (&data[1], cu_header->addr_size,
16257 byte_order, DW_ADDR (attr));
16258 data[cu_header->addr_size + 1] = DW_OP_stack_value;
16259 }
16260 break;
16261 case DW_FORM_string:
16262 case DW_FORM_strp:
16263 case DW_FORM_GNU_str_index:
16264 case DW_FORM_GNU_strp_alt:
16265 /* DW_STRING is already allocated on the objfile obstack, point
16266 directly to it. */
16267 *bytes = (gdb_byte *) DW_STRING (attr);
16268 break;
16269 case DW_FORM_block1:
16270 case DW_FORM_block2:
16271 case DW_FORM_block4:
16272 case DW_FORM_block:
16273 case DW_FORM_exprloc:
16274 blk = DW_BLOCK (attr);
16275 if (TYPE_LENGTH (type) != blk->size)
16276 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
16277 TYPE_LENGTH (type));
16278 *bytes = blk->data;
16279 break;
16280
16281 /* The DW_AT_const_value attributes are supposed to carry the
16282 symbol's value "represented as it would be on the target
16283 architecture." By the time we get here, it's already been
16284 converted to host endianness, so we just need to sign- or
16285 zero-extend it as appropriate. */
16286 case DW_FORM_data1:
16287 *bytes = dwarf2_const_value_data (attr, type, name,
16288 obstack, cu, value, 8);
16289 break;
16290 case DW_FORM_data2:
16291 *bytes = dwarf2_const_value_data (attr, type, name,
16292 obstack, cu, value, 16);
16293 break;
16294 case DW_FORM_data4:
16295 *bytes = dwarf2_const_value_data (attr, type, name,
16296 obstack, cu, value, 32);
16297 break;
16298 case DW_FORM_data8:
16299 *bytes = dwarf2_const_value_data (attr, type, name,
16300 obstack, cu, value, 64);
16301 break;
16302
16303 case DW_FORM_sdata:
16304 *value = DW_SND (attr);
16305 break;
16306
16307 case DW_FORM_udata:
16308 *value = DW_UNSND (attr);
16309 break;
16310
16311 default:
16312 complaint (&symfile_complaints,
16313 _("unsupported const value attribute form: '%s'"),
16314 dwarf_form_name (attr->form));
16315 *value = 0;
16316 break;
16317 }
16318 }
16319
16320
16321 /* Copy constant value from an attribute to a symbol. */
16322
16323 static void
16324 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
16325 struct dwarf2_cu *cu)
16326 {
16327 struct objfile *objfile = cu->objfile;
16328 struct comp_unit_head *cu_header = &cu->header;
16329 LONGEST value;
16330 gdb_byte *bytes;
16331 struct dwarf2_locexpr_baton *baton;
16332
16333 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
16334 SYMBOL_PRINT_NAME (sym),
16335 &objfile->objfile_obstack, cu,
16336 &value, &bytes, &baton);
16337
16338 if (baton != NULL)
16339 {
16340 SYMBOL_LOCATION_BATON (sym) = baton;
16341 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16342 }
16343 else if (bytes != NULL)
16344 {
16345 SYMBOL_VALUE_BYTES (sym) = bytes;
16346 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
16347 }
16348 else
16349 {
16350 SYMBOL_VALUE (sym) = value;
16351 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
16352 }
16353 }
16354
16355 /* Return the type of the die in question using its DW_AT_type attribute. */
16356
16357 static struct type *
16358 die_type (struct die_info *die, struct dwarf2_cu *cu)
16359 {
16360 struct attribute *type_attr;
16361
16362 type_attr = dwarf2_attr (die, DW_AT_type, cu);
16363 if (!type_attr)
16364 {
16365 /* A missing DW_AT_type represents a void type. */
16366 return objfile_type (cu->objfile)->builtin_void;
16367 }
16368
16369 return lookup_die_type (die, type_attr, cu);
16370 }
16371
16372 /* True iff CU's producer generates GNAT Ada auxiliary information
16373 that allows to find parallel types through that information instead
16374 of having to do expensive parallel lookups by type name. */
16375
16376 static int
16377 need_gnat_info (struct dwarf2_cu *cu)
16378 {
16379 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
16380 of GNAT produces this auxiliary information, without any indication
16381 that it is produced. Part of enhancing the FSF version of GNAT
16382 to produce that information will be to put in place an indicator
16383 that we can use in order to determine whether the descriptive type
16384 info is available or not. One suggestion that has been made is
16385 to use a new attribute, attached to the CU die. For now, assume
16386 that the descriptive type info is not available. */
16387 return 0;
16388 }
16389
16390 /* Return the auxiliary type of the die in question using its
16391 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
16392 attribute is not present. */
16393
16394 static struct type *
16395 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
16396 {
16397 struct attribute *type_attr;
16398
16399 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
16400 if (!type_attr)
16401 return NULL;
16402
16403 return lookup_die_type (die, type_attr, cu);
16404 }
16405
16406 /* If DIE has a descriptive_type attribute, then set the TYPE's
16407 descriptive type accordingly. */
16408
16409 static void
16410 set_descriptive_type (struct type *type, struct die_info *die,
16411 struct dwarf2_cu *cu)
16412 {
16413 struct type *descriptive_type = die_descriptive_type (die, cu);
16414
16415 if (descriptive_type)
16416 {
16417 ALLOCATE_GNAT_AUX_TYPE (type);
16418 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
16419 }
16420 }
16421
16422 /* Return the containing type of the die in question using its
16423 DW_AT_containing_type attribute. */
16424
16425 static struct type *
16426 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
16427 {
16428 struct attribute *type_attr;
16429
16430 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
16431 if (!type_attr)
16432 error (_("Dwarf Error: Problem turning containing type into gdb type "
16433 "[in module %s]"), cu->objfile->name);
16434
16435 return lookup_die_type (die, type_attr, cu);
16436 }
16437
16438 /* Look up the type of DIE in CU using its type attribute ATTR.
16439 If there is no type substitute an error marker. */
16440
16441 static struct type *
16442 lookup_die_type (struct die_info *die, struct attribute *attr,
16443 struct dwarf2_cu *cu)
16444 {
16445 struct objfile *objfile = cu->objfile;
16446 struct type *this_type;
16447
16448 /* First see if we have it cached. */
16449
16450 if (attr->form == DW_FORM_GNU_ref_alt)
16451 {
16452 struct dwarf2_per_cu_data *per_cu;
16453 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16454
16455 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
16456 this_type = get_die_type_at_offset (offset, per_cu);
16457 }
16458 else if (is_ref_attr (attr))
16459 {
16460 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16461
16462 this_type = get_die_type_at_offset (offset, cu->per_cu);
16463 }
16464 else if (attr->form == DW_FORM_ref_sig8)
16465 {
16466 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
16467
16468 /* sig_type will be NULL if the signatured type is missing from
16469 the debug info. */
16470 if (sig_type == NULL)
16471 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
16472 "at 0x%x [in module %s]"),
16473 die->offset.sect_off, objfile->name);
16474
16475 gdb_assert (sig_type->per_cu.is_debug_types);
16476 /* If we haven't filled in type_offset_in_section yet, then we
16477 haven't read the type in yet. */
16478 this_type = NULL;
16479 if (sig_type->type_offset_in_section.sect_off != 0)
16480 {
16481 this_type =
16482 get_die_type_at_offset (sig_type->type_offset_in_section,
16483 &sig_type->per_cu);
16484 }
16485 }
16486 else
16487 {
16488 dump_die_for_error (die);
16489 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
16490 dwarf_attr_name (attr->name), objfile->name);
16491 }
16492
16493 /* If not cached we need to read it in. */
16494
16495 if (this_type == NULL)
16496 {
16497 struct die_info *type_die;
16498 struct dwarf2_cu *type_cu = cu;
16499
16500 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
16501 /* If we found the type now, it's probably because the type came
16502 from an inter-CU reference and the type's CU got expanded before
16503 ours. */
16504 this_type = get_die_type (type_die, type_cu);
16505 if (this_type == NULL)
16506 this_type = read_type_die_1 (type_die, type_cu);
16507 }
16508
16509 /* If we still don't have a type use an error marker. */
16510
16511 if (this_type == NULL)
16512 {
16513 char *message, *saved;
16514
16515 /* read_type_die already issued a complaint. */
16516 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
16517 objfile->name,
16518 cu->header.offset.sect_off,
16519 die->offset.sect_off);
16520 saved = obstack_copy0 (&objfile->objfile_obstack,
16521 message, strlen (message));
16522 xfree (message);
16523
16524 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
16525 }
16526
16527 return this_type;
16528 }
16529
16530 /* Return the type in DIE, CU.
16531 Returns NULL for invalid types.
16532
16533 This first does a lookup in the appropriate type_hash table,
16534 and only reads the die in if necessary.
16535
16536 NOTE: This can be called when reading in partial or full symbols. */
16537
16538 static struct type *
16539 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
16540 {
16541 struct type *this_type;
16542
16543 this_type = get_die_type (die, cu);
16544 if (this_type)
16545 return this_type;
16546
16547 return read_type_die_1 (die, cu);
16548 }
16549
16550 /* Read the type in DIE, CU.
16551 Returns NULL for invalid types. */
16552
16553 static struct type *
16554 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
16555 {
16556 struct type *this_type = NULL;
16557
16558 switch (die->tag)
16559 {
16560 case DW_TAG_class_type:
16561 case DW_TAG_interface_type:
16562 case DW_TAG_structure_type:
16563 case DW_TAG_union_type:
16564 this_type = read_structure_type (die, cu);
16565 break;
16566 case DW_TAG_enumeration_type:
16567 this_type = read_enumeration_type (die, cu);
16568 break;
16569 case DW_TAG_subprogram:
16570 case DW_TAG_subroutine_type:
16571 case DW_TAG_inlined_subroutine:
16572 this_type = read_subroutine_type (die, cu);
16573 break;
16574 case DW_TAG_array_type:
16575 this_type = read_array_type (die, cu);
16576 break;
16577 case DW_TAG_set_type:
16578 this_type = read_set_type (die, cu);
16579 break;
16580 case DW_TAG_pointer_type:
16581 this_type = read_tag_pointer_type (die, cu);
16582 break;
16583 case DW_TAG_ptr_to_member_type:
16584 this_type = read_tag_ptr_to_member_type (die, cu);
16585 break;
16586 case DW_TAG_reference_type:
16587 this_type = read_tag_reference_type (die, cu);
16588 break;
16589 case DW_TAG_const_type:
16590 this_type = read_tag_const_type (die, cu);
16591 break;
16592 case DW_TAG_volatile_type:
16593 this_type = read_tag_volatile_type (die, cu);
16594 break;
16595 case DW_TAG_restrict_type:
16596 this_type = read_tag_restrict_type (die, cu);
16597 break;
16598 case DW_TAG_string_type:
16599 this_type = read_tag_string_type (die, cu);
16600 break;
16601 case DW_TAG_typedef:
16602 this_type = read_typedef (die, cu);
16603 break;
16604 case DW_TAG_subrange_type:
16605 this_type = read_subrange_type (die, cu);
16606 break;
16607 case DW_TAG_base_type:
16608 this_type = read_base_type (die, cu);
16609 break;
16610 case DW_TAG_unspecified_type:
16611 this_type = read_unspecified_type (die, cu);
16612 break;
16613 case DW_TAG_namespace:
16614 this_type = read_namespace_type (die, cu);
16615 break;
16616 case DW_TAG_module:
16617 this_type = read_module_type (die, cu);
16618 break;
16619 default:
16620 complaint (&symfile_complaints,
16621 _("unexpected tag in read_type_die: '%s'"),
16622 dwarf_tag_name (die->tag));
16623 break;
16624 }
16625
16626 return this_type;
16627 }
16628
16629 /* See if we can figure out if the class lives in a namespace. We do
16630 this by looking for a member function; its demangled name will
16631 contain namespace info, if there is any.
16632 Return the computed name or NULL.
16633 Space for the result is allocated on the objfile's obstack.
16634 This is the full-die version of guess_partial_die_structure_name.
16635 In this case we know DIE has no useful parent. */
16636
16637 static char *
16638 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
16639 {
16640 struct die_info *spec_die;
16641 struct dwarf2_cu *spec_cu;
16642 struct die_info *child;
16643
16644 spec_cu = cu;
16645 spec_die = die_specification (die, &spec_cu);
16646 if (spec_die != NULL)
16647 {
16648 die = spec_die;
16649 cu = spec_cu;
16650 }
16651
16652 for (child = die->child;
16653 child != NULL;
16654 child = child->sibling)
16655 {
16656 if (child->tag == DW_TAG_subprogram)
16657 {
16658 struct attribute *attr;
16659
16660 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
16661 if (attr == NULL)
16662 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
16663 if (attr != NULL)
16664 {
16665 char *actual_name
16666 = language_class_name_from_physname (cu->language_defn,
16667 DW_STRING (attr));
16668 char *name = NULL;
16669
16670 if (actual_name != NULL)
16671 {
16672 const char *die_name = dwarf2_name (die, cu);
16673
16674 if (die_name != NULL
16675 && strcmp (die_name, actual_name) != 0)
16676 {
16677 /* Strip off the class name from the full name.
16678 We want the prefix. */
16679 int die_name_len = strlen (die_name);
16680 int actual_name_len = strlen (actual_name);
16681
16682 /* Test for '::' as a sanity check. */
16683 if (actual_name_len > die_name_len + 2
16684 && actual_name[actual_name_len
16685 - die_name_len - 1] == ':')
16686 name =
16687 obstack_copy0 (&cu->objfile->objfile_obstack,
16688 actual_name,
16689 actual_name_len - die_name_len - 2);
16690 }
16691 }
16692 xfree (actual_name);
16693 return name;
16694 }
16695 }
16696 }
16697
16698 return NULL;
16699 }
16700
16701 /* GCC might emit a nameless typedef that has a linkage name. Determine the
16702 prefix part in such case. See
16703 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16704
16705 static char *
16706 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
16707 {
16708 struct attribute *attr;
16709 char *base;
16710
16711 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
16712 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
16713 return NULL;
16714
16715 attr = dwarf2_attr (die, DW_AT_name, cu);
16716 if (attr != NULL && DW_STRING (attr) != NULL)
16717 return NULL;
16718
16719 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16720 if (attr == NULL)
16721 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16722 if (attr == NULL || DW_STRING (attr) == NULL)
16723 return NULL;
16724
16725 /* dwarf2_name had to be already called. */
16726 gdb_assert (DW_STRING_IS_CANONICAL (attr));
16727
16728 /* Strip the base name, keep any leading namespaces/classes. */
16729 base = strrchr (DW_STRING (attr), ':');
16730 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
16731 return "";
16732
16733 return obstack_copy0 (&cu->objfile->objfile_obstack,
16734 DW_STRING (attr), &base[-1] - DW_STRING (attr));
16735 }
16736
16737 /* Return the name of the namespace/class that DIE is defined within,
16738 or "" if we can't tell. The caller should not xfree the result.
16739
16740 For example, if we're within the method foo() in the following
16741 code:
16742
16743 namespace N {
16744 class C {
16745 void foo () {
16746 }
16747 };
16748 }
16749
16750 then determine_prefix on foo's die will return "N::C". */
16751
16752 static const char *
16753 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
16754 {
16755 struct die_info *parent, *spec_die;
16756 struct dwarf2_cu *spec_cu;
16757 struct type *parent_type;
16758 char *retval;
16759
16760 if (cu->language != language_cplus && cu->language != language_java
16761 && cu->language != language_fortran)
16762 return "";
16763
16764 retval = anonymous_struct_prefix (die, cu);
16765 if (retval)
16766 return retval;
16767
16768 /* We have to be careful in the presence of DW_AT_specification.
16769 For example, with GCC 3.4, given the code
16770
16771 namespace N {
16772 void foo() {
16773 // Definition of N::foo.
16774 }
16775 }
16776
16777 then we'll have a tree of DIEs like this:
16778
16779 1: DW_TAG_compile_unit
16780 2: DW_TAG_namespace // N
16781 3: DW_TAG_subprogram // declaration of N::foo
16782 4: DW_TAG_subprogram // definition of N::foo
16783 DW_AT_specification // refers to die #3
16784
16785 Thus, when processing die #4, we have to pretend that we're in
16786 the context of its DW_AT_specification, namely the contex of die
16787 #3. */
16788 spec_cu = cu;
16789 spec_die = die_specification (die, &spec_cu);
16790 if (spec_die == NULL)
16791 parent = die->parent;
16792 else
16793 {
16794 parent = spec_die->parent;
16795 cu = spec_cu;
16796 }
16797
16798 if (parent == NULL)
16799 return "";
16800 else if (parent->building_fullname)
16801 {
16802 const char *name;
16803 const char *parent_name;
16804
16805 /* It has been seen on RealView 2.2 built binaries,
16806 DW_TAG_template_type_param types actually _defined_ as
16807 children of the parent class:
16808
16809 enum E {};
16810 template class <class Enum> Class{};
16811 Class<enum E> class_e;
16812
16813 1: DW_TAG_class_type (Class)
16814 2: DW_TAG_enumeration_type (E)
16815 3: DW_TAG_enumerator (enum1:0)
16816 3: DW_TAG_enumerator (enum2:1)
16817 ...
16818 2: DW_TAG_template_type_param
16819 DW_AT_type DW_FORM_ref_udata (E)
16820
16821 Besides being broken debug info, it can put GDB into an
16822 infinite loop. Consider:
16823
16824 When we're building the full name for Class<E>, we'll start
16825 at Class, and go look over its template type parameters,
16826 finding E. We'll then try to build the full name of E, and
16827 reach here. We're now trying to build the full name of E,
16828 and look over the parent DIE for containing scope. In the
16829 broken case, if we followed the parent DIE of E, we'd again
16830 find Class, and once again go look at its template type
16831 arguments, etc., etc. Simply don't consider such parent die
16832 as source-level parent of this die (it can't be, the language
16833 doesn't allow it), and break the loop here. */
16834 name = dwarf2_name (die, cu);
16835 parent_name = dwarf2_name (parent, cu);
16836 complaint (&symfile_complaints,
16837 _("template param type '%s' defined within parent '%s'"),
16838 name ? name : "<unknown>",
16839 parent_name ? parent_name : "<unknown>");
16840 return "";
16841 }
16842 else
16843 switch (parent->tag)
16844 {
16845 case DW_TAG_namespace:
16846 parent_type = read_type_die (parent, cu);
16847 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
16848 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
16849 Work around this problem here. */
16850 if (cu->language == language_cplus
16851 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
16852 return "";
16853 /* We give a name to even anonymous namespaces. */
16854 return TYPE_TAG_NAME (parent_type);
16855 case DW_TAG_class_type:
16856 case DW_TAG_interface_type:
16857 case DW_TAG_structure_type:
16858 case DW_TAG_union_type:
16859 case DW_TAG_module:
16860 parent_type = read_type_die (parent, cu);
16861 if (TYPE_TAG_NAME (parent_type) != NULL)
16862 return TYPE_TAG_NAME (parent_type);
16863 else
16864 /* An anonymous structure is only allowed non-static data
16865 members; no typedefs, no member functions, et cetera.
16866 So it does not need a prefix. */
16867 return "";
16868 case DW_TAG_compile_unit:
16869 case DW_TAG_partial_unit:
16870 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
16871 if (cu->language == language_cplus
16872 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
16873 && die->child != NULL
16874 && (die->tag == DW_TAG_class_type
16875 || die->tag == DW_TAG_structure_type
16876 || die->tag == DW_TAG_union_type))
16877 {
16878 char *name = guess_full_die_structure_name (die, cu);
16879 if (name != NULL)
16880 return name;
16881 }
16882 return "";
16883 default:
16884 return determine_prefix (parent, cu);
16885 }
16886 }
16887
16888 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
16889 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
16890 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
16891 an obconcat, otherwise allocate storage for the result. The CU argument is
16892 used to determine the language and hence, the appropriate separator. */
16893
16894 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
16895
16896 static char *
16897 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
16898 int physname, struct dwarf2_cu *cu)
16899 {
16900 const char *lead = "";
16901 const char *sep;
16902
16903 if (suffix == NULL || suffix[0] == '\0'
16904 || prefix == NULL || prefix[0] == '\0')
16905 sep = "";
16906 else if (cu->language == language_java)
16907 sep = ".";
16908 else if (cu->language == language_fortran && physname)
16909 {
16910 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
16911 DW_AT_MIPS_linkage_name is preferred and used instead. */
16912
16913 lead = "__";
16914 sep = "_MOD_";
16915 }
16916 else
16917 sep = "::";
16918
16919 if (prefix == NULL)
16920 prefix = "";
16921 if (suffix == NULL)
16922 suffix = "";
16923
16924 if (obs == NULL)
16925 {
16926 char *retval
16927 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
16928
16929 strcpy (retval, lead);
16930 strcat (retval, prefix);
16931 strcat (retval, sep);
16932 strcat (retval, suffix);
16933 return retval;
16934 }
16935 else
16936 {
16937 /* We have an obstack. */
16938 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
16939 }
16940 }
16941
16942 /* Return sibling of die, NULL if no sibling. */
16943
16944 static struct die_info *
16945 sibling_die (struct die_info *die)
16946 {
16947 return die->sibling;
16948 }
16949
16950 /* Get name of a die, return NULL if not found. */
16951
16952 static const char *
16953 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
16954 struct obstack *obstack)
16955 {
16956 if (name && cu->language == language_cplus)
16957 {
16958 char *canon_name = cp_canonicalize_string (name);
16959
16960 if (canon_name != NULL)
16961 {
16962 if (strcmp (canon_name, name) != 0)
16963 name = obstack_copy0 (obstack, canon_name, strlen (canon_name));
16964 xfree (canon_name);
16965 }
16966 }
16967
16968 return name;
16969 }
16970
16971 /* Get name of a die, return NULL if not found. */
16972
16973 static const char *
16974 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
16975 {
16976 struct attribute *attr;
16977
16978 attr = dwarf2_attr (die, DW_AT_name, cu);
16979 if ((!attr || !DW_STRING (attr))
16980 && die->tag != DW_TAG_class_type
16981 && die->tag != DW_TAG_interface_type
16982 && die->tag != DW_TAG_structure_type
16983 && die->tag != DW_TAG_union_type)
16984 return NULL;
16985
16986 switch (die->tag)
16987 {
16988 case DW_TAG_compile_unit:
16989 case DW_TAG_partial_unit:
16990 /* Compilation units have a DW_AT_name that is a filename, not
16991 a source language identifier. */
16992 case DW_TAG_enumeration_type:
16993 case DW_TAG_enumerator:
16994 /* These tags always have simple identifiers already; no need
16995 to canonicalize them. */
16996 return DW_STRING (attr);
16997
16998 case DW_TAG_subprogram:
16999 /* Java constructors will all be named "<init>", so return
17000 the class name when we see this special case. */
17001 if (cu->language == language_java
17002 && DW_STRING (attr) != NULL
17003 && strcmp (DW_STRING (attr), "<init>") == 0)
17004 {
17005 struct dwarf2_cu *spec_cu = cu;
17006 struct die_info *spec_die;
17007
17008 /* GCJ will output '<init>' for Java constructor names.
17009 For this special case, return the name of the parent class. */
17010
17011 /* GCJ may output suprogram DIEs with AT_specification set.
17012 If so, use the name of the specified DIE. */
17013 spec_die = die_specification (die, &spec_cu);
17014 if (spec_die != NULL)
17015 return dwarf2_name (spec_die, spec_cu);
17016
17017 do
17018 {
17019 die = die->parent;
17020 if (die->tag == DW_TAG_class_type)
17021 return dwarf2_name (die, cu);
17022 }
17023 while (die->tag != DW_TAG_compile_unit
17024 && die->tag != DW_TAG_partial_unit);
17025 }
17026 break;
17027
17028 case DW_TAG_class_type:
17029 case DW_TAG_interface_type:
17030 case DW_TAG_structure_type:
17031 case DW_TAG_union_type:
17032 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
17033 structures or unions. These were of the form "._%d" in GCC 4.1,
17034 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
17035 and GCC 4.4. We work around this problem by ignoring these. */
17036 if (attr && DW_STRING (attr)
17037 && (strncmp (DW_STRING (attr), "._", 2) == 0
17038 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
17039 return NULL;
17040
17041 /* GCC might emit a nameless typedef that has a linkage name. See
17042 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17043 if (!attr || DW_STRING (attr) == NULL)
17044 {
17045 char *demangled = NULL;
17046
17047 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
17048 if (attr == NULL)
17049 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
17050
17051 if (attr == NULL || DW_STRING (attr) == NULL)
17052 return NULL;
17053
17054 /* Avoid demangling DW_STRING (attr) the second time on a second
17055 call for the same DIE. */
17056 if (!DW_STRING_IS_CANONICAL (attr))
17057 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
17058
17059 if (demangled)
17060 {
17061 char *base;
17062
17063 /* FIXME: we already did this for the partial symbol... */
17064 DW_STRING (attr) = obstack_copy0 (&cu->objfile->objfile_obstack,
17065 demangled, strlen (demangled));
17066 DW_STRING_IS_CANONICAL (attr) = 1;
17067 xfree (demangled);
17068
17069 /* Strip any leading namespaces/classes, keep only the base name.
17070 DW_AT_name for named DIEs does not contain the prefixes. */
17071 base = strrchr (DW_STRING (attr), ':');
17072 if (base && base > DW_STRING (attr) && base[-1] == ':')
17073 return &base[1];
17074 else
17075 return DW_STRING (attr);
17076 }
17077 }
17078 break;
17079
17080 default:
17081 break;
17082 }
17083
17084 if (!DW_STRING_IS_CANONICAL (attr))
17085 {
17086 DW_STRING (attr)
17087 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
17088 &cu->objfile->objfile_obstack);
17089 DW_STRING_IS_CANONICAL (attr) = 1;
17090 }
17091 return DW_STRING (attr);
17092 }
17093
17094 /* Return the die that this die in an extension of, or NULL if there
17095 is none. *EXT_CU is the CU containing DIE on input, and the CU
17096 containing the return value on output. */
17097
17098 static struct die_info *
17099 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
17100 {
17101 struct attribute *attr;
17102
17103 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
17104 if (attr == NULL)
17105 return NULL;
17106
17107 return follow_die_ref (die, attr, ext_cu);
17108 }
17109
17110 /* Convert a DIE tag into its string name. */
17111
17112 static const char *
17113 dwarf_tag_name (unsigned tag)
17114 {
17115 const char *name = get_DW_TAG_name (tag);
17116
17117 if (name == NULL)
17118 return "DW_TAG_<unknown>";
17119
17120 return name;
17121 }
17122
17123 /* Convert a DWARF attribute code into its string name. */
17124
17125 static const char *
17126 dwarf_attr_name (unsigned attr)
17127 {
17128 const char *name;
17129
17130 #ifdef MIPS /* collides with DW_AT_HP_block_index */
17131 if (attr == DW_AT_MIPS_fde)
17132 return "DW_AT_MIPS_fde";
17133 #else
17134 if (attr == DW_AT_HP_block_index)
17135 return "DW_AT_HP_block_index";
17136 #endif
17137
17138 name = get_DW_AT_name (attr);
17139
17140 if (name == NULL)
17141 return "DW_AT_<unknown>";
17142
17143 return name;
17144 }
17145
17146 /* Convert a DWARF value form code into its string name. */
17147
17148 static const char *
17149 dwarf_form_name (unsigned form)
17150 {
17151 const char *name = get_DW_FORM_name (form);
17152
17153 if (name == NULL)
17154 return "DW_FORM_<unknown>";
17155
17156 return name;
17157 }
17158
17159 static char *
17160 dwarf_bool_name (unsigned mybool)
17161 {
17162 if (mybool)
17163 return "TRUE";
17164 else
17165 return "FALSE";
17166 }
17167
17168 /* Convert a DWARF type code into its string name. */
17169
17170 static const char *
17171 dwarf_type_encoding_name (unsigned enc)
17172 {
17173 const char *name = get_DW_ATE_name (enc);
17174
17175 if (name == NULL)
17176 return "DW_ATE_<unknown>";
17177
17178 return name;
17179 }
17180
17181 static void
17182 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
17183 {
17184 unsigned int i;
17185
17186 print_spaces (indent, f);
17187 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
17188 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
17189
17190 if (die->parent != NULL)
17191 {
17192 print_spaces (indent, f);
17193 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
17194 die->parent->offset.sect_off);
17195 }
17196
17197 print_spaces (indent, f);
17198 fprintf_unfiltered (f, " has children: %s\n",
17199 dwarf_bool_name (die->child != NULL));
17200
17201 print_spaces (indent, f);
17202 fprintf_unfiltered (f, " attributes:\n");
17203
17204 for (i = 0; i < die->num_attrs; ++i)
17205 {
17206 print_spaces (indent, f);
17207 fprintf_unfiltered (f, " %s (%s) ",
17208 dwarf_attr_name (die->attrs[i].name),
17209 dwarf_form_name (die->attrs[i].form));
17210
17211 switch (die->attrs[i].form)
17212 {
17213 case DW_FORM_addr:
17214 case DW_FORM_GNU_addr_index:
17215 fprintf_unfiltered (f, "address: ");
17216 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
17217 break;
17218 case DW_FORM_block2:
17219 case DW_FORM_block4:
17220 case DW_FORM_block:
17221 case DW_FORM_block1:
17222 fprintf_unfiltered (f, "block: size %s",
17223 pulongest (DW_BLOCK (&die->attrs[i])->size));
17224 break;
17225 case DW_FORM_exprloc:
17226 fprintf_unfiltered (f, "expression: size %s",
17227 pulongest (DW_BLOCK (&die->attrs[i])->size));
17228 break;
17229 case DW_FORM_ref_addr:
17230 fprintf_unfiltered (f, "ref address: ");
17231 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17232 break;
17233 case DW_FORM_GNU_ref_alt:
17234 fprintf_unfiltered (f, "alt ref address: ");
17235 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17236 break;
17237 case DW_FORM_ref1:
17238 case DW_FORM_ref2:
17239 case DW_FORM_ref4:
17240 case DW_FORM_ref8:
17241 case DW_FORM_ref_udata:
17242 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
17243 (long) (DW_UNSND (&die->attrs[i])));
17244 break;
17245 case DW_FORM_data1:
17246 case DW_FORM_data2:
17247 case DW_FORM_data4:
17248 case DW_FORM_data8:
17249 case DW_FORM_udata:
17250 case DW_FORM_sdata:
17251 fprintf_unfiltered (f, "constant: %s",
17252 pulongest (DW_UNSND (&die->attrs[i])));
17253 break;
17254 case DW_FORM_sec_offset:
17255 fprintf_unfiltered (f, "section offset: %s",
17256 pulongest (DW_UNSND (&die->attrs[i])));
17257 break;
17258 case DW_FORM_ref_sig8:
17259 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
17260 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
17261 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
17262 else
17263 fprintf_unfiltered (f, "signatured type, offset: unknown");
17264 break;
17265 case DW_FORM_string:
17266 case DW_FORM_strp:
17267 case DW_FORM_GNU_str_index:
17268 case DW_FORM_GNU_strp_alt:
17269 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
17270 DW_STRING (&die->attrs[i])
17271 ? DW_STRING (&die->attrs[i]) : "",
17272 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
17273 break;
17274 case DW_FORM_flag:
17275 if (DW_UNSND (&die->attrs[i]))
17276 fprintf_unfiltered (f, "flag: TRUE");
17277 else
17278 fprintf_unfiltered (f, "flag: FALSE");
17279 break;
17280 case DW_FORM_flag_present:
17281 fprintf_unfiltered (f, "flag: TRUE");
17282 break;
17283 case DW_FORM_indirect:
17284 /* The reader will have reduced the indirect form to
17285 the "base form" so this form should not occur. */
17286 fprintf_unfiltered (f,
17287 "unexpected attribute form: DW_FORM_indirect");
17288 break;
17289 default:
17290 fprintf_unfiltered (f, "unsupported attribute form: %d.",
17291 die->attrs[i].form);
17292 break;
17293 }
17294 fprintf_unfiltered (f, "\n");
17295 }
17296 }
17297
17298 static void
17299 dump_die_for_error (struct die_info *die)
17300 {
17301 dump_die_shallow (gdb_stderr, 0, die);
17302 }
17303
17304 static void
17305 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
17306 {
17307 int indent = level * 4;
17308
17309 gdb_assert (die != NULL);
17310
17311 if (level >= max_level)
17312 return;
17313
17314 dump_die_shallow (f, indent, die);
17315
17316 if (die->child != NULL)
17317 {
17318 print_spaces (indent, f);
17319 fprintf_unfiltered (f, " Children:");
17320 if (level + 1 < max_level)
17321 {
17322 fprintf_unfiltered (f, "\n");
17323 dump_die_1 (f, level + 1, max_level, die->child);
17324 }
17325 else
17326 {
17327 fprintf_unfiltered (f,
17328 " [not printed, max nesting level reached]\n");
17329 }
17330 }
17331
17332 if (die->sibling != NULL && level > 0)
17333 {
17334 dump_die_1 (f, level, max_level, die->sibling);
17335 }
17336 }
17337
17338 /* This is called from the pdie macro in gdbinit.in.
17339 It's not static so gcc will keep a copy callable from gdb. */
17340
17341 void
17342 dump_die (struct die_info *die, int max_level)
17343 {
17344 dump_die_1 (gdb_stdlog, 0, max_level, die);
17345 }
17346
17347 static void
17348 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
17349 {
17350 void **slot;
17351
17352 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
17353 INSERT);
17354
17355 *slot = die;
17356 }
17357
17358 /* DW_ADDR is always stored already as sect_offset; despite for the forms
17359 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
17360
17361 static int
17362 is_ref_attr (struct attribute *attr)
17363 {
17364 switch (attr->form)
17365 {
17366 case DW_FORM_ref_addr:
17367 case DW_FORM_ref1:
17368 case DW_FORM_ref2:
17369 case DW_FORM_ref4:
17370 case DW_FORM_ref8:
17371 case DW_FORM_ref_udata:
17372 case DW_FORM_GNU_ref_alt:
17373 return 1;
17374 default:
17375 return 0;
17376 }
17377 }
17378
17379 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
17380 required kind. */
17381
17382 static sect_offset
17383 dwarf2_get_ref_die_offset (struct attribute *attr)
17384 {
17385 sect_offset retval = { DW_UNSND (attr) };
17386
17387 if (is_ref_attr (attr))
17388 return retval;
17389
17390 retval.sect_off = 0;
17391 complaint (&symfile_complaints,
17392 _("unsupported die ref attribute form: '%s'"),
17393 dwarf_form_name (attr->form));
17394 return retval;
17395 }
17396
17397 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
17398 * the value held by the attribute is not constant. */
17399
17400 static LONGEST
17401 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
17402 {
17403 if (attr->form == DW_FORM_sdata)
17404 return DW_SND (attr);
17405 else if (attr->form == DW_FORM_udata
17406 || attr->form == DW_FORM_data1
17407 || attr->form == DW_FORM_data2
17408 || attr->form == DW_FORM_data4
17409 || attr->form == DW_FORM_data8)
17410 return DW_UNSND (attr);
17411 else
17412 {
17413 complaint (&symfile_complaints,
17414 _("Attribute value is not a constant (%s)"),
17415 dwarf_form_name (attr->form));
17416 return default_value;
17417 }
17418 }
17419
17420 /* Follow reference or signature attribute ATTR of SRC_DIE.
17421 On entry *REF_CU is the CU of SRC_DIE.
17422 On exit *REF_CU is the CU of the result. */
17423
17424 static struct die_info *
17425 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
17426 struct dwarf2_cu **ref_cu)
17427 {
17428 struct die_info *die;
17429
17430 if (is_ref_attr (attr))
17431 die = follow_die_ref (src_die, attr, ref_cu);
17432 else if (attr->form == DW_FORM_ref_sig8)
17433 die = follow_die_sig (src_die, attr, ref_cu);
17434 else
17435 {
17436 dump_die_for_error (src_die);
17437 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
17438 (*ref_cu)->objfile->name);
17439 }
17440
17441 return die;
17442 }
17443
17444 /* Follow reference OFFSET.
17445 On entry *REF_CU is the CU of the source die referencing OFFSET.
17446 On exit *REF_CU is the CU of the result.
17447 Returns NULL if OFFSET is invalid. */
17448
17449 static struct die_info *
17450 follow_die_offset (sect_offset offset, int offset_in_dwz,
17451 struct dwarf2_cu **ref_cu)
17452 {
17453 struct die_info temp_die;
17454 struct dwarf2_cu *target_cu, *cu = *ref_cu;
17455
17456 gdb_assert (cu->per_cu != NULL);
17457
17458 target_cu = cu;
17459
17460 if (cu->per_cu->is_debug_types)
17461 {
17462 /* .debug_types CUs cannot reference anything outside their CU.
17463 If they need to, they have to reference a signatured type via
17464 DW_FORM_ref_sig8. */
17465 if (! offset_in_cu_p (&cu->header, offset))
17466 return NULL;
17467 }
17468 else if (offset_in_dwz != cu->per_cu->is_dwz
17469 || ! offset_in_cu_p (&cu->header, offset))
17470 {
17471 struct dwarf2_per_cu_data *per_cu;
17472
17473 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
17474 cu->objfile);
17475
17476 /* If necessary, add it to the queue and load its DIEs. */
17477 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
17478 load_full_comp_unit (per_cu, cu->language);
17479
17480 target_cu = per_cu->cu;
17481 }
17482 else if (cu->dies == NULL)
17483 {
17484 /* We're loading full DIEs during partial symbol reading. */
17485 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
17486 load_full_comp_unit (cu->per_cu, language_minimal);
17487 }
17488
17489 *ref_cu = target_cu;
17490 temp_die.offset = offset;
17491 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
17492 }
17493
17494 /* Follow reference attribute ATTR of SRC_DIE.
17495 On entry *REF_CU is the CU of SRC_DIE.
17496 On exit *REF_CU is the CU of the result. */
17497
17498 static struct die_info *
17499 follow_die_ref (struct die_info *src_die, struct attribute *attr,
17500 struct dwarf2_cu **ref_cu)
17501 {
17502 sect_offset offset = dwarf2_get_ref_die_offset (attr);
17503 struct dwarf2_cu *cu = *ref_cu;
17504 struct die_info *die;
17505
17506 die = follow_die_offset (offset,
17507 (attr->form == DW_FORM_GNU_ref_alt
17508 || cu->per_cu->is_dwz),
17509 ref_cu);
17510 if (!die)
17511 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
17512 "at 0x%x [in module %s]"),
17513 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
17514
17515 return die;
17516 }
17517
17518 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
17519 Returned value is intended for DW_OP_call*. Returned
17520 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
17521
17522 struct dwarf2_locexpr_baton
17523 dwarf2_fetch_die_loc_sect_off (sect_offset offset,
17524 struct dwarf2_per_cu_data *per_cu,
17525 CORE_ADDR (*get_frame_pc) (void *baton),
17526 void *baton)
17527 {
17528 struct dwarf2_cu *cu;
17529 struct die_info *die;
17530 struct attribute *attr;
17531 struct dwarf2_locexpr_baton retval;
17532
17533 dw2_setup (per_cu->objfile);
17534
17535 if (per_cu->cu == NULL)
17536 load_cu (per_cu);
17537 cu = per_cu->cu;
17538
17539 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
17540 if (!die)
17541 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
17542 offset.sect_off, per_cu->objfile->name);
17543
17544 attr = dwarf2_attr (die, DW_AT_location, cu);
17545 if (!attr)
17546 {
17547 /* DWARF: "If there is no such attribute, then there is no effect.".
17548 DATA is ignored if SIZE is 0. */
17549
17550 retval.data = NULL;
17551 retval.size = 0;
17552 }
17553 else if (attr_form_is_section_offset (attr))
17554 {
17555 struct dwarf2_loclist_baton loclist_baton;
17556 CORE_ADDR pc = (*get_frame_pc) (baton);
17557 size_t size;
17558
17559 fill_in_loclist_baton (cu, &loclist_baton, attr);
17560
17561 retval.data = dwarf2_find_location_expression (&loclist_baton,
17562 &size, pc);
17563 retval.size = size;
17564 }
17565 else
17566 {
17567 if (!attr_form_is_block (attr))
17568 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
17569 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
17570 offset.sect_off, per_cu->objfile->name);
17571
17572 retval.data = DW_BLOCK (attr)->data;
17573 retval.size = DW_BLOCK (attr)->size;
17574 }
17575 retval.per_cu = cu->per_cu;
17576
17577 age_cached_comp_units ();
17578
17579 return retval;
17580 }
17581
17582 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
17583 offset. */
17584
17585 struct dwarf2_locexpr_baton
17586 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
17587 struct dwarf2_per_cu_data *per_cu,
17588 CORE_ADDR (*get_frame_pc) (void *baton),
17589 void *baton)
17590 {
17591 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
17592
17593 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
17594 }
17595
17596 /* Return the type of the DIE at DIE_OFFSET in the CU named by
17597 PER_CU. */
17598
17599 struct type *
17600 dwarf2_get_die_type (cu_offset die_offset,
17601 struct dwarf2_per_cu_data *per_cu)
17602 {
17603 sect_offset die_offset_sect;
17604
17605 dw2_setup (per_cu->objfile);
17606
17607 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
17608 return get_die_type_at_offset (die_offset_sect, per_cu);
17609 }
17610
17611 /* Follow the signature attribute ATTR in SRC_DIE.
17612 On entry *REF_CU is the CU of SRC_DIE.
17613 On exit *REF_CU is the CU of the result. */
17614
17615 static struct die_info *
17616 follow_die_sig (struct die_info *src_die, struct attribute *attr,
17617 struct dwarf2_cu **ref_cu)
17618 {
17619 struct objfile *objfile = (*ref_cu)->objfile;
17620 struct die_info temp_die;
17621 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
17622 struct dwarf2_cu *sig_cu;
17623 struct die_info *die;
17624
17625 /* sig_type will be NULL if the signatured type is missing from
17626 the debug info. */
17627 if (sig_type == NULL)
17628 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
17629 "at 0x%x [in module %s]"),
17630 src_die->offset.sect_off, objfile->name);
17631
17632 /* If necessary, add it to the queue and load its DIEs. */
17633
17634 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
17635 read_signatured_type (sig_type);
17636
17637 gdb_assert (sig_type->per_cu.cu != NULL);
17638
17639 sig_cu = sig_type->per_cu.cu;
17640 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
17641 temp_die.offset = sig_type->type_offset_in_section;
17642 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
17643 temp_die.offset.sect_off);
17644 if (die)
17645 {
17646 /* For .gdb_index version 7 keep track of included TUs.
17647 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
17648 if (dwarf2_per_objfile->index_table != NULL
17649 && dwarf2_per_objfile->index_table->version <= 7)
17650 {
17651 VEC_safe_push (dwarf2_per_cu_ptr,
17652 (*ref_cu)->per_cu->imported_symtabs,
17653 sig_cu->per_cu);
17654 }
17655
17656 *ref_cu = sig_cu;
17657 return die;
17658 }
17659
17660 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
17661 "from DIE at 0x%x [in module %s]"),
17662 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
17663 }
17664
17665 /* Given an offset of a signatured type, return its signatured_type. */
17666
17667 static struct signatured_type *
17668 lookup_signatured_type_at_offset (struct objfile *objfile,
17669 struct dwarf2_section_info *section,
17670 sect_offset offset)
17671 {
17672 gdb_byte *info_ptr = section->buffer + offset.sect_off;
17673 unsigned int length, initial_length_size;
17674 unsigned int sig_offset;
17675 struct signatured_type find_entry, *sig_type;
17676
17677 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
17678 sig_offset = (initial_length_size
17679 + 2 /*version*/
17680 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
17681 + 1 /*address_size*/);
17682 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
17683 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
17684
17685 /* This is only used to lookup previously recorded types.
17686 If we didn't find it, it's our bug. */
17687 gdb_assert (sig_type != NULL);
17688 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
17689
17690 return sig_type;
17691 }
17692
17693 /* Load the DIEs associated with type unit PER_CU into memory. */
17694
17695 static void
17696 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
17697 {
17698 struct signatured_type *sig_type;
17699
17700 /* Caller is responsible for ensuring type_unit_groups don't get here. */
17701 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
17702
17703 /* We have the per_cu, but we need the signatured_type.
17704 Fortunately this is an easy translation. */
17705 gdb_assert (per_cu->is_debug_types);
17706 sig_type = (struct signatured_type *) per_cu;
17707
17708 gdb_assert (per_cu->cu == NULL);
17709
17710 read_signatured_type (sig_type);
17711
17712 gdb_assert (per_cu->cu != NULL);
17713 }
17714
17715 /* die_reader_func for read_signatured_type.
17716 This is identical to load_full_comp_unit_reader,
17717 but is kept separate for now. */
17718
17719 static void
17720 read_signatured_type_reader (const struct die_reader_specs *reader,
17721 gdb_byte *info_ptr,
17722 struct die_info *comp_unit_die,
17723 int has_children,
17724 void *data)
17725 {
17726 struct dwarf2_cu *cu = reader->cu;
17727
17728 gdb_assert (cu->die_hash == NULL);
17729 cu->die_hash =
17730 htab_create_alloc_ex (cu->header.length / 12,
17731 die_hash,
17732 die_eq,
17733 NULL,
17734 &cu->comp_unit_obstack,
17735 hashtab_obstack_allocate,
17736 dummy_obstack_deallocate);
17737
17738 if (has_children)
17739 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
17740 &info_ptr, comp_unit_die);
17741 cu->dies = comp_unit_die;
17742 /* comp_unit_die is not stored in die_hash, no need. */
17743
17744 /* We try not to read any attributes in this function, because not
17745 all CUs needed for references have been loaded yet, and symbol
17746 table processing isn't initialized. But we have to set the CU language,
17747 or we won't be able to build types correctly.
17748 Similarly, if we do not read the producer, we can not apply
17749 producer-specific interpretation. */
17750 prepare_one_comp_unit (cu, cu->dies, language_minimal);
17751 }
17752
17753 /* Read in a signatured type and build its CU and DIEs.
17754 If the type is a stub for the real type in a DWO file,
17755 read in the real type from the DWO file as well. */
17756
17757 static void
17758 read_signatured_type (struct signatured_type *sig_type)
17759 {
17760 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
17761
17762 gdb_assert (per_cu->is_debug_types);
17763 gdb_assert (per_cu->cu == NULL);
17764
17765 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
17766 read_signatured_type_reader, NULL);
17767 }
17768
17769 /* Decode simple location descriptions.
17770 Given a pointer to a dwarf block that defines a location, compute
17771 the location and return the value.
17772
17773 NOTE drow/2003-11-18: This function is called in two situations
17774 now: for the address of static or global variables (partial symbols
17775 only) and for offsets into structures which are expected to be
17776 (more or less) constant. The partial symbol case should go away,
17777 and only the constant case should remain. That will let this
17778 function complain more accurately. A few special modes are allowed
17779 without complaint for global variables (for instance, global
17780 register values and thread-local values).
17781
17782 A location description containing no operations indicates that the
17783 object is optimized out. The return value is 0 for that case.
17784 FIXME drow/2003-11-16: No callers check for this case any more; soon all
17785 callers will only want a very basic result and this can become a
17786 complaint.
17787
17788 Note that stack[0] is unused except as a default error return. */
17789
17790 static CORE_ADDR
17791 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
17792 {
17793 struct objfile *objfile = cu->objfile;
17794 size_t i;
17795 size_t size = blk->size;
17796 gdb_byte *data = blk->data;
17797 CORE_ADDR stack[64];
17798 int stacki;
17799 unsigned int bytes_read, unsnd;
17800 gdb_byte op;
17801
17802 i = 0;
17803 stacki = 0;
17804 stack[stacki] = 0;
17805 stack[++stacki] = 0;
17806
17807 while (i < size)
17808 {
17809 op = data[i++];
17810 switch (op)
17811 {
17812 case DW_OP_lit0:
17813 case DW_OP_lit1:
17814 case DW_OP_lit2:
17815 case DW_OP_lit3:
17816 case DW_OP_lit4:
17817 case DW_OP_lit5:
17818 case DW_OP_lit6:
17819 case DW_OP_lit7:
17820 case DW_OP_lit8:
17821 case DW_OP_lit9:
17822 case DW_OP_lit10:
17823 case DW_OP_lit11:
17824 case DW_OP_lit12:
17825 case DW_OP_lit13:
17826 case DW_OP_lit14:
17827 case DW_OP_lit15:
17828 case DW_OP_lit16:
17829 case DW_OP_lit17:
17830 case DW_OP_lit18:
17831 case DW_OP_lit19:
17832 case DW_OP_lit20:
17833 case DW_OP_lit21:
17834 case DW_OP_lit22:
17835 case DW_OP_lit23:
17836 case DW_OP_lit24:
17837 case DW_OP_lit25:
17838 case DW_OP_lit26:
17839 case DW_OP_lit27:
17840 case DW_OP_lit28:
17841 case DW_OP_lit29:
17842 case DW_OP_lit30:
17843 case DW_OP_lit31:
17844 stack[++stacki] = op - DW_OP_lit0;
17845 break;
17846
17847 case DW_OP_reg0:
17848 case DW_OP_reg1:
17849 case DW_OP_reg2:
17850 case DW_OP_reg3:
17851 case DW_OP_reg4:
17852 case DW_OP_reg5:
17853 case DW_OP_reg6:
17854 case DW_OP_reg7:
17855 case DW_OP_reg8:
17856 case DW_OP_reg9:
17857 case DW_OP_reg10:
17858 case DW_OP_reg11:
17859 case DW_OP_reg12:
17860 case DW_OP_reg13:
17861 case DW_OP_reg14:
17862 case DW_OP_reg15:
17863 case DW_OP_reg16:
17864 case DW_OP_reg17:
17865 case DW_OP_reg18:
17866 case DW_OP_reg19:
17867 case DW_OP_reg20:
17868 case DW_OP_reg21:
17869 case DW_OP_reg22:
17870 case DW_OP_reg23:
17871 case DW_OP_reg24:
17872 case DW_OP_reg25:
17873 case DW_OP_reg26:
17874 case DW_OP_reg27:
17875 case DW_OP_reg28:
17876 case DW_OP_reg29:
17877 case DW_OP_reg30:
17878 case DW_OP_reg31:
17879 stack[++stacki] = op - DW_OP_reg0;
17880 if (i < size)
17881 dwarf2_complex_location_expr_complaint ();
17882 break;
17883
17884 case DW_OP_regx:
17885 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
17886 i += bytes_read;
17887 stack[++stacki] = unsnd;
17888 if (i < size)
17889 dwarf2_complex_location_expr_complaint ();
17890 break;
17891
17892 case DW_OP_addr:
17893 stack[++stacki] = read_address (objfile->obfd, &data[i],
17894 cu, &bytes_read);
17895 i += bytes_read;
17896 break;
17897
17898 case DW_OP_const1u:
17899 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
17900 i += 1;
17901 break;
17902
17903 case DW_OP_const1s:
17904 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
17905 i += 1;
17906 break;
17907
17908 case DW_OP_const2u:
17909 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
17910 i += 2;
17911 break;
17912
17913 case DW_OP_const2s:
17914 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
17915 i += 2;
17916 break;
17917
17918 case DW_OP_const4u:
17919 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
17920 i += 4;
17921 break;
17922
17923 case DW_OP_const4s:
17924 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
17925 i += 4;
17926 break;
17927
17928 case DW_OP_const8u:
17929 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
17930 i += 8;
17931 break;
17932
17933 case DW_OP_constu:
17934 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
17935 &bytes_read);
17936 i += bytes_read;
17937 break;
17938
17939 case DW_OP_consts:
17940 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
17941 i += bytes_read;
17942 break;
17943
17944 case DW_OP_dup:
17945 stack[stacki + 1] = stack[stacki];
17946 stacki++;
17947 break;
17948
17949 case DW_OP_plus:
17950 stack[stacki - 1] += stack[stacki];
17951 stacki--;
17952 break;
17953
17954 case DW_OP_plus_uconst:
17955 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
17956 &bytes_read);
17957 i += bytes_read;
17958 break;
17959
17960 case DW_OP_minus:
17961 stack[stacki - 1] -= stack[stacki];
17962 stacki--;
17963 break;
17964
17965 case DW_OP_deref:
17966 /* If we're not the last op, then we definitely can't encode
17967 this using GDB's address_class enum. This is valid for partial
17968 global symbols, although the variable's address will be bogus
17969 in the psymtab. */
17970 if (i < size)
17971 dwarf2_complex_location_expr_complaint ();
17972 break;
17973
17974 case DW_OP_GNU_push_tls_address:
17975 /* The top of the stack has the offset from the beginning
17976 of the thread control block at which the variable is located. */
17977 /* Nothing should follow this operator, so the top of stack would
17978 be returned. */
17979 /* This is valid for partial global symbols, but the variable's
17980 address will be bogus in the psymtab. Make it always at least
17981 non-zero to not look as a variable garbage collected by linker
17982 which have DW_OP_addr 0. */
17983 if (i < size)
17984 dwarf2_complex_location_expr_complaint ();
17985 stack[stacki]++;
17986 break;
17987
17988 case DW_OP_GNU_uninit:
17989 break;
17990
17991 case DW_OP_GNU_addr_index:
17992 case DW_OP_GNU_const_index:
17993 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
17994 &bytes_read);
17995 i += bytes_read;
17996 break;
17997
17998 default:
17999 {
18000 const char *name = get_DW_OP_name (op);
18001
18002 if (name)
18003 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
18004 name);
18005 else
18006 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
18007 op);
18008 }
18009
18010 return (stack[stacki]);
18011 }
18012
18013 /* Enforce maximum stack depth of SIZE-1 to avoid writing
18014 outside of the allocated space. Also enforce minimum>0. */
18015 if (stacki >= ARRAY_SIZE (stack) - 1)
18016 {
18017 complaint (&symfile_complaints,
18018 _("location description stack overflow"));
18019 return 0;
18020 }
18021
18022 if (stacki <= 0)
18023 {
18024 complaint (&symfile_complaints,
18025 _("location description stack underflow"));
18026 return 0;
18027 }
18028 }
18029 return (stack[stacki]);
18030 }
18031
18032 /* memory allocation interface */
18033
18034 static struct dwarf_block *
18035 dwarf_alloc_block (struct dwarf2_cu *cu)
18036 {
18037 struct dwarf_block *blk;
18038
18039 blk = (struct dwarf_block *)
18040 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
18041 return (blk);
18042 }
18043
18044 static struct die_info *
18045 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
18046 {
18047 struct die_info *die;
18048 size_t size = sizeof (struct die_info);
18049
18050 if (num_attrs > 1)
18051 size += (num_attrs - 1) * sizeof (struct attribute);
18052
18053 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
18054 memset (die, 0, sizeof (struct die_info));
18055 return (die);
18056 }
18057
18058 \f
18059 /* Macro support. */
18060
18061 /* Return file name relative to the compilation directory of file number I in
18062 *LH's file name table. The result is allocated using xmalloc; the caller is
18063 responsible for freeing it. */
18064
18065 static char *
18066 file_file_name (int file, struct line_header *lh)
18067 {
18068 /* Is the file number a valid index into the line header's file name
18069 table? Remember that file numbers start with one, not zero. */
18070 if (1 <= file && file <= lh->num_file_names)
18071 {
18072 struct file_entry *fe = &lh->file_names[file - 1];
18073
18074 if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0)
18075 return xstrdup (fe->name);
18076 return concat (lh->include_dirs[fe->dir_index - 1], SLASH_STRING,
18077 fe->name, NULL);
18078 }
18079 else
18080 {
18081 /* The compiler produced a bogus file number. We can at least
18082 record the macro definitions made in the file, even if we
18083 won't be able to find the file by name. */
18084 char fake_name[80];
18085
18086 xsnprintf (fake_name, sizeof (fake_name),
18087 "<bad macro file number %d>", file);
18088
18089 complaint (&symfile_complaints,
18090 _("bad file number in macro information (%d)"),
18091 file);
18092
18093 return xstrdup (fake_name);
18094 }
18095 }
18096
18097 /* Return the full name of file number I in *LH's file name table.
18098 Use COMP_DIR as the name of the current directory of the
18099 compilation. The result is allocated using xmalloc; the caller is
18100 responsible for freeing it. */
18101 static char *
18102 file_full_name (int file, struct line_header *lh, const char *comp_dir)
18103 {
18104 /* Is the file number a valid index into the line header's file name
18105 table? Remember that file numbers start with one, not zero. */
18106 if (1 <= file && file <= lh->num_file_names)
18107 {
18108 char *relative = file_file_name (file, lh);
18109
18110 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
18111 return relative;
18112 return reconcat (relative, comp_dir, SLASH_STRING, relative, NULL);
18113 }
18114 else
18115 return file_file_name (file, lh);
18116 }
18117
18118
18119 static struct macro_source_file *
18120 macro_start_file (int file, int line,
18121 struct macro_source_file *current_file,
18122 const char *comp_dir,
18123 struct line_header *lh, struct objfile *objfile)
18124 {
18125 /* File name relative to the compilation directory of this source file. */
18126 char *file_name = file_file_name (file, lh);
18127
18128 /* We don't create a macro table for this compilation unit
18129 at all until we actually get a filename. */
18130 if (! pending_macros)
18131 pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
18132 objfile->per_bfd->macro_cache,
18133 comp_dir);
18134
18135 if (! current_file)
18136 {
18137 /* If we have no current file, then this must be the start_file
18138 directive for the compilation unit's main source file. */
18139 current_file = macro_set_main (pending_macros, file_name);
18140 macro_define_special (pending_macros);
18141 }
18142 else
18143 current_file = macro_include (current_file, line, file_name);
18144
18145 xfree (file_name);
18146
18147 return current_file;
18148 }
18149
18150
18151 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
18152 followed by a null byte. */
18153 static char *
18154 copy_string (const char *buf, int len)
18155 {
18156 char *s = xmalloc (len + 1);
18157
18158 memcpy (s, buf, len);
18159 s[len] = '\0';
18160 return s;
18161 }
18162
18163
18164 static const char *
18165 consume_improper_spaces (const char *p, const char *body)
18166 {
18167 if (*p == ' ')
18168 {
18169 complaint (&symfile_complaints,
18170 _("macro definition contains spaces "
18171 "in formal argument list:\n`%s'"),
18172 body);
18173
18174 while (*p == ' ')
18175 p++;
18176 }
18177
18178 return p;
18179 }
18180
18181
18182 static void
18183 parse_macro_definition (struct macro_source_file *file, int line,
18184 const char *body)
18185 {
18186 const char *p;
18187
18188 /* The body string takes one of two forms. For object-like macro
18189 definitions, it should be:
18190
18191 <macro name> " " <definition>
18192
18193 For function-like macro definitions, it should be:
18194
18195 <macro name> "() " <definition>
18196 or
18197 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
18198
18199 Spaces may appear only where explicitly indicated, and in the
18200 <definition>.
18201
18202 The Dwarf 2 spec says that an object-like macro's name is always
18203 followed by a space, but versions of GCC around March 2002 omit
18204 the space when the macro's definition is the empty string.
18205
18206 The Dwarf 2 spec says that there should be no spaces between the
18207 formal arguments in a function-like macro's formal argument list,
18208 but versions of GCC around March 2002 include spaces after the
18209 commas. */
18210
18211
18212 /* Find the extent of the macro name. The macro name is terminated
18213 by either a space or null character (for an object-like macro) or
18214 an opening paren (for a function-like macro). */
18215 for (p = body; *p; p++)
18216 if (*p == ' ' || *p == '(')
18217 break;
18218
18219 if (*p == ' ' || *p == '\0')
18220 {
18221 /* It's an object-like macro. */
18222 int name_len = p - body;
18223 char *name = copy_string (body, name_len);
18224 const char *replacement;
18225
18226 if (*p == ' ')
18227 replacement = body + name_len + 1;
18228 else
18229 {
18230 dwarf2_macro_malformed_definition_complaint (body);
18231 replacement = body + name_len;
18232 }
18233
18234 macro_define_object (file, line, name, replacement);
18235
18236 xfree (name);
18237 }
18238 else if (*p == '(')
18239 {
18240 /* It's a function-like macro. */
18241 char *name = copy_string (body, p - body);
18242 int argc = 0;
18243 int argv_size = 1;
18244 char **argv = xmalloc (argv_size * sizeof (*argv));
18245
18246 p++;
18247
18248 p = consume_improper_spaces (p, body);
18249
18250 /* Parse the formal argument list. */
18251 while (*p && *p != ')')
18252 {
18253 /* Find the extent of the current argument name. */
18254 const char *arg_start = p;
18255
18256 while (*p && *p != ',' && *p != ')' && *p != ' ')
18257 p++;
18258
18259 if (! *p || p == arg_start)
18260 dwarf2_macro_malformed_definition_complaint (body);
18261 else
18262 {
18263 /* Make sure argv has room for the new argument. */
18264 if (argc >= argv_size)
18265 {
18266 argv_size *= 2;
18267 argv = xrealloc (argv, argv_size * sizeof (*argv));
18268 }
18269
18270 argv[argc++] = copy_string (arg_start, p - arg_start);
18271 }
18272
18273 p = consume_improper_spaces (p, body);
18274
18275 /* Consume the comma, if present. */
18276 if (*p == ',')
18277 {
18278 p++;
18279
18280 p = consume_improper_spaces (p, body);
18281 }
18282 }
18283
18284 if (*p == ')')
18285 {
18286 p++;
18287
18288 if (*p == ' ')
18289 /* Perfectly formed definition, no complaints. */
18290 macro_define_function (file, line, name,
18291 argc, (const char **) argv,
18292 p + 1);
18293 else if (*p == '\0')
18294 {
18295 /* Complain, but do define it. */
18296 dwarf2_macro_malformed_definition_complaint (body);
18297 macro_define_function (file, line, name,
18298 argc, (const char **) argv,
18299 p);
18300 }
18301 else
18302 /* Just complain. */
18303 dwarf2_macro_malformed_definition_complaint (body);
18304 }
18305 else
18306 /* Just complain. */
18307 dwarf2_macro_malformed_definition_complaint (body);
18308
18309 xfree (name);
18310 {
18311 int i;
18312
18313 for (i = 0; i < argc; i++)
18314 xfree (argv[i]);
18315 }
18316 xfree (argv);
18317 }
18318 else
18319 dwarf2_macro_malformed_definition_complaint (body);
18320 }
18321
18322 /* Skip some bytes from BYTES according to the form given in FORM.
18323 Returns the new pointer. */
18324
18325 static gdb_byte *
18326 skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
18327 enum dwarf_form form,
18328 unsigned int offset_size,
18329 struct dwarf2_section_info *section)
18330 {
18331 unsigned int bytes_read;
18332
18333 switch (form)
18334 {
18335 case DW_FORM_data1:
18336 case DW_FORM_flag:
18337 ++bytes;
18338 break;
18339
18340 case DW_FORM_data2:
18341 bytes += 2;
18342 break;
18343
18344 case DW_FORM_data4:
18345 bytes += 4;
18346 break;
18347
18348 case DW_FORM_data8:
18349 bytes += 8;
18350 break;
18351
18352 case DW_FORM_string:
18353 read_direct_string (abfd, bytes, &bytes_read);
18354 bytes += bytes_read;
18355 break;
18356
18357 case DW_FORM_sec_offset:
18358 case DW_FORM_strp:
18359 case DW_FORM_GNU_strp_alt:
18360 bytes += offset_size;
18361 break;
18362
18363 case DW_FORM_block:
18364 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
18365 bytes += bytes_read;
18366 break;
18367
18368 case DW_FORM_block1:
18369 bytes += 1 + read_1_byte (abfd, bytes);
18370 break;
18371 case DW_FORM_block2:
18372 bytes += 2 + read_2_bytes (abfd, bytes);
18373 break;
18374 case DW_FORM_block4:
18375 bytes += 4 + read_4_bytes (abfd, bytes);
18376 break;
18377
18378 case DW_FORM_sdata:
18379 case DW_FORM_udata:
18380 case DW_FORM_GNU_addr_index:
18381 case DW_FORM_GNU_str_index:
18382 bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
18383 if (bytes == NULL)
18384 {
18385 dwarf2_section_buffer_overflow_complaint (section);
18386 return NULL;
18387 }
18388 break;
18389
18390 default:
18391 {
18392 complain:
18393 complaint (&symfile_complaints,
18394 _("invalid form 0x%x in `%s'"),
18395 form,
18396 section->asection->name);
18397 return NULL;
18398 }
18399 }
18400
18401 return bytes;
18402 }
18403
18404 /* A helper for dwarf_decode_macros that handles skipping an unknown
18405 opcode. Returns an updated pointer to the macro data buffer; or,
18406 on error, issues a complaint and returns NULL. */
18407
18408 static gdb_byte *
18409 skip_unknown_opcode (unsigned int opcode,
18410 gdb_byte **opcode_definitions,
18411 gdb_byte *mac_ptr, gdb_byte *mac_end,
18412 bfd *abfd,
18413 unsigned int offset_size,
18414 struct dwarf2_section_info *section)
18415 {
18416 unsigned int bytes_read, i;
18417 unsigned long arg;
18418 gdb_byte *defn;
18419
18420 if (opcode_definitions[opcode] == NULL)
18421 {
18422 complaint (&symfile_complaints,
18423 _("unrecognized DW_MACFINO opcode 0x%x"),
18424 opcode);
18425 return NULL;
18426 }
18427
18428 defn = opcode_definitions[opcode];
18429 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
18430 defn += bytes_read;
18431
18432 for (i = 0; i < arg; ++i)
18433 {
18434 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
18435 section);
18436 if (mac_ptr == NULL)
18437 {
18438 /* skip_form_bytes already issued the complaint. */
18439 return NULL;
18440 }
18441 }
18442
18443 return mac_ptr;
18444 }
18445
18446 /* A helper function which parses the header of a macro section.
18447 If the macro section is the extended (for now called "GNU") type,
18448 then this updates *OFFSET_SIZE. Returns a pointer to just after
18449 the header, or issues a complaint and returns NULL on error. */
18450
18451 static gdb_byte *
18452 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
18453 bfd *abfd,
18454 gdb_byte *mac_ptr,
18455 unsigned int *offset_size,
18456 int section_is_gnu)
18457 {
18458 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
18459
18460 if (section_is_gnu)
18461 {
18462 unsigned int version, flags;
18463
18464 version = read_2_bytes (abfd, mac_ptr);
18465 if (version != 4)
18466 {
18467 complaint (&symfile_complaints,
18468 _("unrecognized version `%d' in .debug_macro section"),
18469 version);
18470 return NULL;
18471 }
18472 mac_ptr += 2;
18473
18474 flags = read_1_byte (abfd, mac_ptr);
18475 ++mac_ptr;
18476 *offset_size = (flags & 1) ? 8 : 4;
18477
18478 if ((flags & 2) != 0)
18479 /* We don't need the line table offset. */
18480 mac_ptr += *offset_size;
18481
18482 /* Vendor opcode descriptions. */
18483 if ((flags & 4) != 0)
18484 {
18485 unsigned int i, count;
18486
18487 count = read_1_byte (abfd, mac_ptr);
18488 ++mac_ptr;
18489 for (i = 0; i < count; ++i)
18490 {
18491 unsigned int opcode, bytes_read;
18492 unsigned long arg;
18493
18494 opcode = read_1_byte (abfd, mac_ptr);
18495 ++mac_ptr;
18496 opcode_definitions[opcode] = mac_ptr;
18497 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18498 mac_ptr += bytes_read;
18499 mac_ptr += arg;
18500 }
18501 }
18502 }
18503
18504 return mac_ptr;
18505 }
18506
18507 /* A helper for dwarf_decode_macros that handles the GNU extensions,
18508 including DW_MACRO_GNU_transparent_include. */
18509
18510 static void
18511 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
18512 struct macro_source_file *current_file,
18513 struct line_header *lh, const char *comp_dir,
18514 struct dwarf2_section_info *section,
18515 int section_is_gnu, int section_is_dwz,
18516 unsigned int offset_size,
18517 struct objfile *objfile,
18518 htab_t include_hash)
18519 {
18520 enum dwarf_macro_record_type macinfo_type;
18521 int at_commandline;
18522 gdb_byte *opcode_definitions[256];
18523
18524 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18525 &offset_size, section_is_gnu);
18526 if (mac_ptr == NULL)
18527 {
18528 /* We already issued a complaint. */
18529 return;
18530 }
18531
18532 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
18533 GDB is still reading the definitions from command line. First
18534 DW_MACINFO_start_file will need to be ignored as it was already executed
18535 to create CURRENT_FILE for the main source holding also the command line
18536 definitions. On first met DW_MACINFO_start_file this flag is reset to
18537 normally execute all the remaining DW_MACINFO_start_file macinfos. */
18538
18539 at_commandline = 1;
18540
18541 do
18542 {
18543 /* Do we at least have room for a macinfo type byte? */
18544 if (mac_ptr >= mac_end)
18545 {
18546 dwarf2_section_buffer_overflow_complaint (section);
18547 break;
18548 }
18549
18550 macinfo_type = read_1_byte (abfd, mac_ptr);
18551 mac_ptr++;
18552
18553 /* Note that we rely on the fact that the corresponding GNU and
18554 DWARF constants are the same. */
18555 switch (macinfo_type)
18556 {
18557 /* A zero macinfo type indicates the end of the macro
18558 information. */
18559 case 0:
18560 break;
18561
18562 case DW_MACRO_GNU_define:
18563 case DW_MACRO_GNU_undef:
18564 case DW_MACRO_GNU_define_indirect:
18565 case DW_MACRO_GNU_undef_indirect:
18566 case DW_MACRO_GNU_define_indirect_alt:
18567 case DW_MACRO_GNU_undef_indirect_alt:
18568 {
18569 unsigned int bytes_read;
18570 int line;
18571 char *body;
18572 int is_define;
18573
18574 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18575 mac_ptr += bytes_read;
18576
18577 if (macinfo_type == DW_MACRO_GNU_define
18578 || macinfo_type == DW_MACRO_GNU_undef)
18579 {
18580 body = read_direct_string (abfd, mac_ptr, &bytes_read);
18581 mac_ptr += bytes_read;
18582 }
18583 else
18584 {
18585 LONGEST str_offset;
18586
18587 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
18588 mac_ptr += offset_size;
18589
18590 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
18591 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
18592 || section_is_dwz)
18593 {
18594 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18595
18596 body = read_indirect_string_from_dwz (dwz, str_offset);
18597 }
18598 else
18599 body = read_indirect_string_at_offset (abfd, str_offset);
18600 }
18601
18602 is_define = (macinfo_type == DW_MACRO_GNU_define
18603 || macinfo_type == DW_MACRO_GNU_define_indirect
18604 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
18605 if (! current_file)
18606 {
18607 /* DWARF violation as no main source is present. */
18608 complaint (&symfile_complaints,
18609 _("debug info with no main source gives macro %s "
18610 "on line %d: %s"),
18611 is_define ? _("definition") : _("undefinition"),
18612 line, body);
18613 break;
18614 }
18615 if ((line == 0 && !at_commandline)
18616 || (line != 0 && at_commandline))
18617 complaint (&symfile_complaints,
18618 _("debug info gives %s macro %s with %s line %d: %s"),
18619 at_commandline ? _("command-line") : _("in-file"),
18620 is_define ? _("definition") : _("undefinition"),
18621 line == 0 ? _("zero") : _("non-zero"), line, body);
18622
18623 if (is_define)
18624 parse_macro_definition (current_file, line, body);
18625 else
18626 {
18627 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
18628 || macinfo_type == DW_MACRO_GNU_undef_indirect
18629 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
18630 macro_undef (current_file, line, body);
18631 }
18632 }
18633 break;
18634
18635 case DW_MACRO_GNU_start_file:
18636 {
18637 unsigned int bytes_read;
18638 int line, file;
18639
18640 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18641 mac_ptr += bytes_read;
18642 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18643 mac_ptr += bytes_read;
18644
18645 if ((line == 0 && !at_commandline)
18646 || (line != 0 && at_commandline))
18647 complaint (&symfile_complaints,
18648 _("debug info gives source %d included "
18649 "from %s at %s line %d"),
18650 file, at_commandline ? _("command-line") : _("file"),
18651 line == 0 ? _("zero") : _("non-zero"), line);
18652
18653 if (at_commandline)
18654 {
18655 /* This DW_MACRO_GNU_start_file was executed in the
18656 pass one. */
18657 at_commandline = 0;
18658 }
18659 else
18660 current_file = macro_start_file (file, line,
18661 current_file, comp_dir,
18662 lh, objfile);
18663 }
18664 break;
18665
18666 case DW_MACRO_GNU_end_file:
18667 if (! current_file)
18668 complaint (&symfile_complaints,
18669 _("macro debug info has an unmatched "
18670 "`close_file' directive"));
18671 else
18672 {
18673 current_file = current_file->included_by;
18674 if (! current_file)
18675 {
18676 enum dwarf_macro_record_type next_type;
18677
18678 /* GCC circa March 2002 doesn't produce the zero
18679 type byte marking the end of the compilation
18680 unit. Complain if it's not there, but exit no
18681 matter what. */
18682
18683 /* Do we at least have room for a macinfo type byte? */
18684 if (mac_ptr >= mac_end)
18685 {
18686 dwarf2_section_buffer_overflow_complaint (section);
18687 return;
18688 }
18689
18690 /* We don't increment mac_ptr here, so this is just
18691 a look-ahead. */
18692 next_type = read_1_byte (abfd, mac_ptr);
18693 if (next_type != 0)
18694 complaint (&symfile_complaints,
18695 _("no terminating 0-type entry for "
18696 "macros in `.debug_macinfo' section"));
18697
18698 return;
18699 }
18700 }
18701 break;
18702
18703 case DW_MACRO_GNU_transparent_include:
18704 case DW_MACRO_GNU_transparent_include_alt:
18705 {
18706 LONGEST offset;
18707 void **slot;
18708 bfd *include_bfd = abfd;
18709 struct dwarf2_section_info *include_section = section;
18710 struct dwarf2_section_info alt_section;
18711 gdb_byte *include_mac_end = mac_end;
18712 int is_dwz = section_is_dwz;
18713 gdb_byte *new_mac_ptr;
18714
18715 offset = read_offset_1 (abfd, mac_ptr, offset_size);
18716 mac_ptr += offset_size;
18717
18718 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
18719 {
18720 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18721
18722 dwarf2_read_section (dwarf2_per_objfile->objfile,
18723 &dwz->macro);
18724
18725 include_bfd = dwz->macro.asection->owner;
18726 include_section = &dwz->macro;
18727 include_mac_end = dwz->macro.buffer + dwz->macro.size;
18728 is_dwz = 1;
18729 }
18730
18731 new_mac_ptr = include_section->buffer + offset;
18732 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
18733
18734 if (*slot != NULL)
18735 {
18736 /* This has actually happened; see
18737 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
18738 complaint (&symfile_complaints,
18739 _("recursive DW_MACRO_GNU_transparent_include in "
18740 ".debug_macro section"));
18741 }
18742 else
18743 {
18744 *slot = new_mac_ptr;
18745
18746 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
18747 include_mac_end, current_file,
18748 lh, comp_dir,
18749 section, section_is_gnu, is_dwz,
18750 offset_size, objfile, include_hash);
18751
18752 htab_remove_elt (include_hash, new_mac_ptr);
18753 }
18754 }
18755 break;
18756
18757 case DW_MACINFO_vendor_ext:
18758 if (!section_is_gnu)
18759 {
18760 unsigned int bytes_read;
18761 int constant;
18762
18763 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18764 mac_ptr += bytes_read;
18765 read_direct_string (abfd, mac_ptr, &bytes_read);
18766 mac_ptr += bytes_read;
18767
18768 /* We don't recognize any vendor extensions. */
18769 break;
18770 }
18771 /* FALLTHROUGH */
18772
18773 default:
18774 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
18775 mac_ptr, mac_end, abfd, offset_size,
18776 section);
18777 if (mac_ptr == NULL)
18778 return;
18779 break;
18780 }
18781 } while (macinfo_type != 0);
18782 }
18783
18784 static void
18785 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
18786 const char *comp_dir, int section_is_gnu)
18787 {
18788 struct objfile *objfile = dwarf2_per_objfile->objfile;
18789 struct line_header *lh = cu->line_header;
18790 bfd *abfd;
18791 gdb_byte *mac_ptr, *mac_end;
18792 struct macro_source_file *current_file = 0;
18793 enum dwarf_macro_record_type macinfo_type;
18794 unsigned int offset_size = cu->header.offset_size;
18795 gdb_byte *opcode_definitions[256];
18796 struct cleanup *cleanup;
18797 htab_t include_hash;
18798 void **slot;
18799 struct dwarf2_section_info *section;
18800 const char *section_name;
18801
18802 if (cu->dwo_unit != NULL)
18803 {
18804 if (section_is_gnu)
18805 {
18806 section = &cu->dwo_unit->dwo_file->sections.macro;
18807 section_name = ".debug_macro.dwo";
18808 }
18809 else
18810 {
18811 section = &cu->dwo_unit->dwo_file->sections.macinfo;
18812 section_name = ".debug_macinfo.dwo";
18813 }
18814 }
18815 else
18816 {
18817 if (section_is_gnu)
18818 {
18819 section = &dwarf2_per_objfile->macro;
18820 section_name = ".debug_macro";
18821 }
18822 else
18823 {
18824 section = &dwarf2_per_objfile->macinfo;
18825 section_name = ".debug_macinfo";
18826 }
18827 }
18828
18829 dwarf2_read_section (objfile, section);
18830 if (section->buffer == NULL)
18831 {
18832 complaint (&symfile_complaints, _("missing %s section"), section_name);
18833 return;
18834 }
18835 abfd = section->asection->owner;
18836
18837 /* First pass: Find the name of the base filename.
18838 This filename is needed in order to process all macros whose definition
18839 (or undefinition) comes from the command line. These macros are defined
18840 before the first DW_MACINFO_start_file entry, and yet still need to be
18841 associated to the base file.
18842
18843 To determine the base file name, we scan the macro definitions until we
18844 reach the first DW_MACINFO_start_file entry. We then initialize
18845 CURRENT_FILE accordingly so that any macro definition found before the
18846 first DW_MACINFO_start_file can still be associated to the base file. */
18847
18848 mac_ptr = section->buffer + offset;
18849 mac_end = section->buffer + section->size;
18850
18851 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18852 &offset_size, section_is_gnu);
18853 if (mac_ptr == NULL)
18854 {
18855 /* We already issued a complaint. */
18856 return;
18857 }
18858
18859 do
18860 {
18861 /* Do we at least have room for a macinfo type byte? */
18862 if (mac_ptr >= mac_end)
18863 {
18864 /* Complaint is printed during the second pass as GDB will probably
18865 stop the first pass earlier upon finding
18866 DW_MACINFO_start_file. */
18867 break;
18868 }
18869
18870 macinfo_type = read_1_byte (abfd, mac_ptr);
18871 mac_ptr++;
18872
18873 /* Note that we rely on the fact that the corresponding GNU and
18874 DWARF constants are the same. */
18875 switch (macinfo_type)
18876 {
18877 /* A zero macinfo type indicates the end of the macro
18878 information. */
18879 case 0:
18880 break;
18881
18882 case DW_MACRO_GNU_define:
18883 case DW_MACRO_GNU_undef:
18884 /* Only skip the data by MAC_PTR. */
18885 {
18886 unsigned int bytes_read;
18887
18888 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18889 mac_ptr += bytes_read;
18890 read_direct_string (abfd, mac_ptr, &bytes_read);
18891 mac_ptr += bytes_read;
18892 }
18893 break;
18894
18895 case DW_MACRO_GNU_start_file:
18896 {
18897 unsigned int bytes_read;
18898 int line, file;
18899
18900 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18901 mac_ptr += bytes_read;
18902 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18903 mac_ptr += bytes_read;
18904
18905 current_file = macro_start_file (file, line, current_file,
18906 comp_dir, lh, objfile);
18907 }
18908 break;
18909
18910 case DW_MACRO_GNU_end_file:
18911 /* No data to skip by MAC_PTR. */
18912 break;
18913
18914 case DW_MACRO_GNU_define_indirect:
18915 case DW_MACRO_GNU_undef_indirect:
18916 case DW_MACRO_GNU_define_indirect_alt:
18917 case DW_MACRO_GNU_undef_indirect_alt:
18918 {
18919 unsigned int bytes_read;
18920
18921 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18922 mac_ptr += bytes_read;
18923 mac_ptr += offset_size;
18924 }
18925 break;
18926
18927 case DW_MACRO_GNU_transparent_include:
18928 case DW_MACRO_GNU_transparent_include_alt:
18929 /* Note that, according to the spec, a transparent include
18930 chain cannot call DW_MACRO_GNU_start_file. So, we can just
18931 skip this opcode. */
18932 mac_ptr += offset_size;
18933 break;
18934
18935 case DW_MACINFO_vendor_ext:
18936 /* Only skip the data by MAC_PTR. */
18937 if (!section_is_gnu)
18938 {
18939 unsigned int bytes_read;
18940
18941 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18942 mac_ptr += bytes_read;
18943 read_direct_string (abfd, mac_ptr, &bytes_read);
18944 mac_ptr += bytes_read;
18945 }
18946 /* FALLTHROUGH */
18947
18948 default:
18949 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
18950 mac_ptr, mac_end, abfd, offset_size,
18951 section);
18952 if (mac_ptr == NULL)
18953 return;
18954 break;
18955 }
18956 } while (macinfo_type != 0 && current_file == NULL);
18957
18958 /* Second pass: Process all entries.
18959
18960 Use the AT_COMMAND_LINE flag to determine whether we are still processing
18961 command-line macro definitions/undefinitions. This flag is unset when we
18962 reach the first DW_MACINFO_start_file entry. */
18963
18964 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
18965 NULL, xcalloc, xfree);
18966 cleanup = make_cleanup_htab_delete (include_hash);
18967 mac_ptr = section->buffer + offset;
18968 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
18969 *slot = mac_ptr;
18970 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
18971 current_file, lh, comp_dir, section,
18972 section_is_gnu, 0,
18973 offset_size, objfile, include_hash);
18974 do_cleanups (cleanup);
18975 }
18976
18977 /* Check if the attribute's form is a DW_FORM_block*
18978 if so return true else false. */
18979
18980 static int
18981 attr_form_is_block (struct attribute *attr)
18982 {
18983 return (attr == NULL ? 0 :
18984 attr->form == DW_FORM_block1
18985 || attr->form == DW_FORM_block2
18986 || attr->form == DW_FORM_block4
18987 || attr->form == DW_FORM_block
18988 || attr->form == DW_FORM_exprloc);
18989 }
18990
18991 /* Return non-zero if ATTR's value is a section offset --- classes
18992 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
18993 You may use DW_UNSND (attr) to retrieve such offsets.
18994
18995 Section 7.5.4, "Attribute Encodings", explains that no attribute
18996 may have a value that belongs to more than one of these classes; it
18997 would be ambiguous if we did, because we use the same forms for all
18998 of them. */
18999
19000 static int
19001 attr_form_is_section_offset (struct attribute *attr)
19002 {
19003 return (attr->form == DW_FORM_data4
19004 || attr->form == DW_FORM_data8
19005 || attr->form == DW_FORM_sec_offset);
19006 }
19007
19008 /* Return non-zero if ATTR's value falls in the 'constant' class, or
19009 zero otherwise. When this function returns true, you can apply
19010 dwarf2_get_attr_constant_value to it.
19011
19012 However, note that for some attributes you must check
19013 attr_form_is_section_offset before using this test. DW_FORM_data4
19014 and DW_FORM_data8 are members of both the constant class, and of
19015 the classes that contain offsets into other debug sections
19016 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
19017 that, if an attribute's can be either a constant or one of the
19018 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
19019 taken as section offsets, not constants. */
19020
19021 static int
19022 attr_form_is_constant (struct attribute *attr)
19023 {
19024 switch (attr->form)
19025 {
19026 case DW_FORM_sdata:
19027 case DW_FORM_udata:
19028 case DW_FORM_data1:
19029 case DW_FORM_data2:
19030 case DW_FORM_data4:
19031 case DW_FORM_data8:
19032 return 1;
19033 default:
19034 return 0;
19035 }
19036 }
19037
19038 /* Return the .debug_loc section to use for CU.
19039 For DWO files use .debug_loc.dwo. */
19040
19041 static struct dwarf2_section_info *
19042 cu_debug_loc_section (struct dwarf2_cu *cu)
19043 {
19044 if (cu->dwo_unit)
19045 return &cu->dwo_unit->dwo_file->sections.loc;
19046 return &dwarf2_per_objfile->loc;
19047 }
19048
19049 /* A helper function that fills in a dwarf2_loclist_baton. */
19050
19051 static void
19052 fill_in_loclist_baton (struct dwarf2_cu *cu,
19053 struct dwarf2_loclist_baton *baton,
19054 struct attribute *attr)
19055 {
19056 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19057
19058 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
19059
19060 baton->per_cu = cu->per_cu;
19061 gdb_assert (baton->per_cu);
19062 /* We don't know how long the location list is, but make sure we
19063 don't run off the edge of the section. */
19064 baton->size = section->size - DW_UNSND (attr);
19065 baton->data = section->buffer + DW_UNSND (attr);
19066 baton->base_address = cu->base_address;
19067 baton->from_dwo = cu->dwo_unit != NULL;
19068 }
19069
19070 static void
19071 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
19072 struct dwarf2_cu *cu, int is_block)
19073 {
19074 struct objfile *objfile = dwarf2_per_objfile->objfile;
19075 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19076
19077 if (attr_form_is_section_offset (attr)
19078 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
19079 the section. If so, fall through to the complaint in the
19080 other branch. */
19081 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
19082 {
19083 struct dwarf2_loclist_baton *baton;
19084
19085 baton = obstack_alloc (&objfile->objfile_obstack,
19086 sizeof (struct dwarf2_loclist_baton));
19087
19088 fill_in_loclist_baton (cu, baton, attr);
19089
19090 if (cu->base_known == 0)
19091 complaint (&symfile_complaints,
19092 _("Location list used without "
19093 "specifying the CU base address."));
19094
19095 SYMBOL_ACLASS_INDEX (sym) = (is_block
19096 ? dwarf2_loclist_block_index
19097 : dwarf2_loclist_index);
19098 SYMBOL_LOCATION_BATON (sym) = baton;
19099 }
19100 else
19101 {
19102 struct dwarf2_locexpr_baton *baton;
19103
19104 baton = obstack_alloc (&objfile->objfile_obstack,
19105 sizeof (struct dwarf2_locexpr_baton));
19106 baton->per_cu = cu->per_cu;
19107 gdb_assert (baton->per_cu);
19108
19109 if (attr_form_is_block (attr))
19110 {
19111 /* Note that we're just copying the block's data pointer
19112 here, not the actual data. We're still pointing into the
19113 info_buffer for SYM's objfile; right now we never release
19114 that buffer, but when we do clean up properly this may
19115 need to change. */
19116 baton->size = DW_BLOCK (attr)->size;
19117 baton->data = DW_BLOCK (attr)->data;
19118 }
19119 else
19120 {
19121 dwarf2_invalid_attrib_class_complaint ("location description",
19122 SYMBOL_NATURAL_NAME (sym));
19123 baton->size = 0;
19124 }
19125
19126 SYMBOL_ACLASS_INDEX (sym) = (is_block
19127 ? dwarf2_locexpr_block_index
19128 : dwarf2_locexpr_index);
19129 SYMBOL_LOCATION_BATON (sym) = baton;
19130 }
19131 }
19132
19133 /* Return the OBJFILE associated with the compilation unit CU. If CU
19134 came from a separate debuginfo file, then the master objfile is
19135 returned. */
19136
19137 struct objfile *
19138 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
19139 {
19140 struct objfile *objfile = per_cu->objfile;
19141
19142 /* Return the master objfile, so that we can report and look up the
19143 correct file containing this variable. */
19144 if (objfile->separate_debug_objfile_backlink)
19145 objfile = objfile->separate_debug_objfile_backlink;
19146
19147 return objfile;
19148 }
19149
19150 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
19151 (CU_HEADERP is unused in such case) or prepare a temporary copy at
19152 CU_HEADERP first. */
19153
19154 static const struct comp_unit_head *
19155 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
19156 struct dwarf2_per_cu_data *per_cu)
19157 {
19158 gdb_byte *info_ptr;
19159
19160 if (per_cu->cu)
19161 return &per_cu->cu->header;
19162
19163 info_ptr = per_cu->info_or_types_section->buffer + per_cu->offset.sect_off;
19164
19165 memset (cu_headerp, 0, sizeof (*cu_headerp));
19166 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
19167
19168 return cu_headerp;
19169 }
19170
19171 /* Return the address size given in the compilation unit header for CU. */
19172
19173 int
19174 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
19175 {
19176 struct comp_unit_head cu_header_local;
19177 const struct comp_unit_head *cu_headerp;
19178
19179 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19180
19181 return cu_headerp->addr_size;
19182 }
19183
19184 /* Return the offset size given in the compilation unit header for CU. */
19185
19186 int
19187 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
19188 {
19189 struct comp_unit_head cu_header_local;
19190 const struct comp_unit_head *cu_headerp;
19191
19192 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19193
19194 return cu_headerp->offset_size;
19195 }
19196
19197 /* See its dwarf2loc.h declaration. */
19198
19199 int
19200 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
19201 {
19202 struct comp_unit_head cu_header_local;
19203 const struct comp_unit_head *cu_headerp;
19204
19205 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19206
19207 if (cu_headerp->version == 2)
19208 return cu_headerp->addr_size;
19209 else
19210 return cu_headerp->offset_size;
19211 }
19212
19213 /* Return the text offset of the CU. The returned offset comes from
19214 this CU's objfile. If this objfile came from a separate debuginfo
19215 file, then the offset may be different from the corresponding
19216 offset in the parent objfile. */
19217
19218 CORE_ADDR
19219 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
19220 {
19221 struct objfile *objfile = per_cu->objfile;
19222
19223 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19224 }
19225
19226 /* Locate the .debug_info compilation unit from CU's objfile which contains
19227 the DIE at OFFSET. Raises an error on failure. */
19228
19229 static struct dwarf2_per_cu_data *
19230 dwarf2_find_containing_comp_unit (sect_offset offset,
19231 unsigned int offset_in_dwz,
19232 struct objfile *objfile)
19233 {
19234 struct dwarf2_per_cu_data *this_cu;
19235 int low, high;
19236 const sect_offset *cu_off;
19237
19238 low = 0;
19239 high = dwarf2_per_objfile->n_comp_units - 1;
19240 while (high > low)
19241 {
19242 struct dwarf2_per_cu_data *mid_cu;
19243 int mid = low + (high - low) / 2;
19244
19245 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
19246 cu_off = &mid_cu->offset;
19247 if (mid_cu->is_dwz > offset_in_dwz
19248 || (mid_cu->is_dwz == offset_in_dwz
19249 && cu_off->sect_off >= offset.sect_off))
19250 high = mid;
19251 else
19252 low = mid + 1;
19253 }
19254 gdb_assert (low == high);
19255 this_cu = dwarf2_per_objfile->all_comp_units[low];
19256 cu_off = &this_cu->offset;
19257 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
19258 {
19259 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
19260 error (_("Dwarf Error: could not find partial DIE containing "
19261 "offset 0x%lx [in module %s]"),
19262 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
19263
19264 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
19265 <= offset.sect_off);
19266 return dwarf2_per_objfile->all_comp_units[low-1];
19267 }
19268 else
19269 {
19270 this_cu = dwarf2_per_objfile->all_comp_units[low];
19271 if (low == dwarf2_per_objfile->n_comp_units - 1
19272 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
19273 error (_("invalid dwarf2 offset %u"), offset.sect_off);
19274 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
19275 return this_cu;
19276 }
19277 }
19278
19279 /* Initialize dwarf2_cu CU, owned by PER_CU. */
19280
19281 static void
19282 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
19283 {
19284 memset (cu, 0, sizeof (*cu));
19285 per_cu->cu = cu;
19286 cu->per_cu = per_cu;
19287 cu->objfile = per_cu->objfile;
19288 obstack_init (&cu->comp_unit_obstack);
19289 }
19290
19291 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
19292
19293 static void
19294 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
19295 enum language pretend_language)
19296 {
19297 struct attribute *attr;
19298
19299 /* Set the language we're debugging. */
19300 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
19301 if (attr)
19302 set_cu_language (DW_UNSND (attr), cu);
19303 else
19304 {
19305 cu->language = pretend_language;
19306 cu->language_defn = language_def (cu->language);
19307 }
19308
19309 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
19310 if (attr)
19311 cu->producer = DW_STRING (attr);
19312 }
19313
19314 /* Release one cached compilation unit, CU. We unlink it from the tree
19315 of compilation units, but we don't remove it from the read_in_chain;
19316 the caller is responsible for that.
19317 NOTE: DATA is a void * because this function is also used as a
19318 cleanup routine. */
19319
19320 static void
19321 free_heap_comp_unit (void *data)
19322 {
19323 struct dwarf2_cu *cu = data;
19324
19325 gdb_assert (cu->per_cu != NULL);
19326 cu->per_cu->cu = NULL;
19327 cu->per_cu = NULL;
19328
19329 obstack_free (&cu->comp_unit_obstack, NULL);
19330
19331 xfree (cu);
19332 }
19333
19334 /* This cleanup function is passed the address of a dwarf2_cu on the stack
19335 when we're finished with it. We can't free the pointer itself, but be
19336 sure to unlink it from the cache. Also release any associated storage. */
19337
19338 static void
19339 free_stack_comp_unit (void *data)
19340 {
19341 struct dwarf2_cu *cu = data;
19342
19343 gdb_assert (cu->per_cu != NULL);
19344 cu->per_cu->cu = NULL;
19345 cu->per_cu = NULL;
19346
19347 obstack_free (&cu->comp_unit_obstack, NULL);
19348 cu->partial_dies = NULL;
19349 }
19350
19351 /* Free all cached compilation units. */
19352
19353 static void
19354 free_cached_comp_units (void *data)
19355 {
19356 struct dwarf2_per_cu_data *per_cu, **last_chain;
19357
19358 per_cu = dwarf2_per_objfile->read_in_chain;
19359 last_chain = &dwarf2_per_objfile->read_in_chain;
19360 while (per_cu != NULL)
19361 {
19362 struct dwarf2_per_cu_data *next_cu;
19363
19364 next_cu = per_cu->cu->read_in_chain;
19365
19366 free_heap_comp_unit (per_cu->cu);
19367 *last_chain = next_cu;
19368
19369 per_cu = next_cu;
19370 }
19371 }
19372
19373 /* Increase the age counter on each cached compilation unit, and free
19374 any that are too old. */
19375
19376 static void
19377 age_cached_comp_units (void)
19378 {
19379 struct dwarf2_per_cu_data *per_cu, **last_chain;
19380
19381 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
19382 per_cu = dwarf2_per_objfile->read_in_chain;
19383 while (per_cu != NULL)
19384 {
19385 per_cu->cu->last_used ++;
19386 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
19387 dwarf2_mark (per_cu->cu);
19388 per_cu = per_cu->cu->read_in_chain;
19389 }
19390
19391 per_cu = dwarf2_per_objfile->read_in_chain;
19392 last_chain = &dwarf2_per_objfile->read_in_chain;
19393 while (per_cu != NULL)
19394 {
19395 struct dwarf2_per_cu_data *next_cu;
19396
19397 next_cu = per_cu->cu->read_in_chain;
19398
19399 if (!per_cu->cu->mark)
19400 {
19401 free_heap_comp_unit (per_cu->cu);
19402 *last_chain = next_cu;
19403 }
19404 else
19405 last_chain = &per_cu->cu->read_in_chain;
19406
19407 per_cu = next_cu;
19408 }
19409 }
19410
19411 /* Remove a single compilation unit from the cache. */
19412
19413 static void
19414 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
19415 {
19416 struct dwarf2_per_cu_data *per_cu, **last_chain;
19417
19418 per_cu = dwarf2_per_objfile->read_in_chain;
19419 last_chain = &dwarf2_per_objfile->read_in_chain;
19420 while (per_cu != NULL)
19421 {
19422 struct dwarf2_per_cu_data *next_cu;
19423
19424 next_cu = per_cu->cu->read_in_chain;
19425
19426 if (per_cu == target_per_cu)
19427 {
19428 free_heap_comp_unit (per_cu->cu);
19429 per_cu->cu = NULL;
19430 *last_chain = next_cu;
19431 break;
19432 }
19433 else
19434 last_chain = &per_cu->cu->read_in_chain;
19435
19436 per_cu = next_cu;
19437 }
19438 }
19439
19440 /* Release all extra memory associated with OBJFILE. */
19441
19442 void
19443 dwarf2_free_objfile (struct objfile *objfile)
19444 {
19445 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
19446
19447 if (dwarf2_per_objfile == NULL)
19448 return;
19449
19450 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
19451 free_cached_comp_units (NULL);
19452
19453 if (dwarf2_per_objfile->quick_file_names_table)
19454 htab_delete (dwarf2_per_objfile->quick_file_names_table);
19455
19456 /* Everything else should be on the objfile obstack. */
19457 }
19458
19459 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
19460 We store these in a hash table separate from the DIEs, and preserve them
19461 when the DIEs are flushed out of cache.
19462
19463 The CU "per_cu" pointer is needed because offset alone is not enough to
19464 uniquely identify the type. A file may have multiple .debug_types sections,
19465 or the type may come from a DWO file. We have to use something in
19466 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
19467 routine, get_die_type_at_offset, from outside this file, and thus won't
19468 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
19469 of the objfile. */
19470
19471 struct dwarf2_per_cu_offset_and_type
19472 {
19473 const struct dwarf2_per_cu_data *per_cu;
19474 sect_offset offset;
19475 struct type *type;
19476 };
19477
19478 /* Hash function for a dwarf2_per_cu_offset_and_type. */
19479
19480 static hashval_t
19481 per_cu_offset_and_type_hash (const void *item)
19482 {
19483 const struct dwarf2_per_cu_offset_and_type *ofs = item;
19484
19485 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
19486 }
19487
19488 /* Equality function for a dwarf2_per_cu_offset_and_type. */
19489
19490 static int
19491 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
19492 {
19493 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
19494 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
19495
19496 return (ofs_lhs->per_cu == ofs_rhs->per_cu
19497 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
19498 }
19499
19500 /* Set the type associated with DIE to TYPE. Save it in CU's hash
19501 table if necessary. For convenience, return TYPE.
19502
19503 The DIEs reading must have careful ordering to:
19504 * Not cause infite loops trying to read in DIEs as a prerequisite for
19505 reading current DIE.
19506 * Not trying to dereference contents of still incompletely read in types
19507 while reading in other DIEs.
19508 * Enable referencing still incompletely read in types just by a pointer to
19509 the type without accessing its fields.
19510
19511 Therefore caller should follow these rules:
19512 * Try to fetch any prerequisite types we may need to build this DIE type
19513 before building the type and calling set_die_type.
19514 * After building type call set_die_type for current DIE as soon as
19515 possible before fetching more types to complete the current type.
19516 * Make the type as complete as possible before fetching more types. */
19517
19518 static struct type *
19519 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19520 {
19521 struct dwarf2_per_cu_offset_and_type **slot, ofs;
19522 struct objfile *objfile = cu->objfile;
19523
19524 /* For Ada types, make sure that the gnat-specific data is always
19525 initialized (if not already set). There are a few types where
19526 we should not be doing so, because the type-specific area is
19527 already used to hold some other piece of info (eg: TYPE_CODE_FLT
19528 where the type-specific area is used to store the floatformat).
19529 But this is not a problem, because the gnat-specific information
19530 is actually not needed for these types. */
19531 if (need_gnat_info (cu)
19532 && TYPE_CODE (type) != TYPE_CODE_FUNC
19533 && TYPE_CODE (type) != TYPE_CODE_FLT
19534 && !HAVE_GNAT_AUX_INFO (type))
19535 INIT_GNAT_SPECIFIC (type);
19536
19537 if (dwarf2_per_objfile->die_type_hash == NULL)
19538 {
19539 dwarf2_per_objfile->die_type_hash =
19540 htab_create_alloc_ex (127,
19541 per_cu_offset_and_type_hash,
19542 per_cu_offset_and_type_eq,
19543 NULL,
19544 &objfile->objfile_obstack,
19545 hashtab_obstack_allocate,
19546 dummy_obstack_deallocate);
19547 }
19548
19549 ofs.per_cu = cu->per_cu;
19550 ofs.offset = die->offset;
19551 ofs.type = type;
19552 slot = (struct dwarf2_per_cu_offset_and_type **)
19553 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
19554 if (*slot)
19555 complaint (&symfile_complaints,
19556 _("A problem internal to GDB: DIE 0x%x has type already set"),
19557 die->offset.sect_off);
19558 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
19559 **slot = ofs;
19560 return type;
19561 }
19562
19563 /* Look up the type for the die at OFFSET in the appropriate type_hash
19564 table, or return NULL if the die does not have a saved type. */
19565
19566 static struct type *
19567 get_die_type_at_offset (sect_offset offset,
19568 struct dwarf2_per_cu_data *per_cu)
19569 {
19570 struct dwarf2_per_cu_offset_and_type *slot, ofs;
19571
19572 if (dwarf2_per_objfile->die_type_hash == NULL)
19573 return NULL;
19574
19575 ofs.per_cu = per_cu;
19576 ofs.offset = offset;
19577 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
19578 if (slot)
19579 return slot->type;
19580 else
19581 return NULL;
19582 }
19583
19584 /* Look up the type for DIE in the appropriate type_hash table,
19585 or return NULL if DIE does not have a saved type. */
19586
19587 static struct type *
19588 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
19589 {
19590 return get_die_type_at_offset (die->offset, cu->per_cu);
19591 }
19592
19593 /* Add a dependence relationship from CU to REF_PER_CU. */
19594
19595 static void
19596 dwarf2_add_dependence (struct dwarf2_cu *cu,
19597 struct dwarf2_per_cu_data *ref_per_cu)
19598 {
19599 void **slot;
19600
19601 if (cu->dependencies == NULL)
19602 cu->dependencies
19603 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
19604 NULL, &cu->comp_unit_obstack,
19605 hashtab_obstack_allocate,
19606 dummy_obstack_deallocate);
19607
19608 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
19609 if (*slot == NULL)
19610 *slot = ref_per_cu;
19611 }
19612
19613 /* Subroutine of dwarf2_mark to pass to htab_traverse.
19614 Set the mark field in every compilation unit in the
19615 cache that we must keep because we are keeping CU. */
19616
19617 static int
19618 dwarf2_mark_helper (void **slot, void *data)
19619 {
19620 struct dwarf2_per_cu_data *per_cu;
19621
19622 per_cu = (struct dwarf2_per_cu_data *) *slot;
19623
19624 /* cu->dependencies references may not yet have been ever read if QUIT aborts
19625 reading of the chain. As such dependencies remain valid it is not much
19626 useful to track and undo them during QUIT cleanups. */
19627 if (per_cu->cu == NULL)
19628 return 1;
19629
19630 if (per_cu->cu->mark)
19631 return 1;
19632 per_cu->cu->mark = 1;
19633
19634 if (per_cu->cu->dependencies != NULL)
19635 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
19636
19637 return 1;
19638 }
19639
19640 /* Set the mark field in CU and in every other compilation unit in the
19641 cache that we must keep because we are keeping CU. */
19642
19643 static void
19644 dwarf2_mark (struct dwarf2_cu *cu)
19645 {
19646 if (cu->mark)
19647 return;
19648 cu->mark = 1;
19649 if (cu->dependencies != NULL)
19650 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
19651 }
19652
19653 static void
19654 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
19655 {
19656 while (per_cu)
19657 {
19658 per_cu->cu->mark = 0;
19659 per_cu = per_cu->cu->read_in_chain;
19660 }
19661 }
19662
19663 /* Trivial hash function for partial_die_info: the hash value of a DIE
19664 is its offset in .debug_info for this objfile. */
19665
19666 static hashval_t
19667 partial_die_hash (const void *item)
19668 {
19669 const struct partial_die_info *part_die = item;
19670
19671 return part_die->offset.sect_off;
19672 }
19673
19674 /* Trivial comparison function for partial_die_info structures: two DIEs
19675 are equal if they have the same offset. */
19676
19677 static int
19678 partial_die_eq (const void *item_lhs, const void *item_rhs)
19679 {
19680 const struct partial_die_info *part_die_lhs = item_lhs;
19681 const struct partial_die_info *part_die_rhs = item_rhs;
19682
19683 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
19684 }
19685
19686 static struct cmd_list_element *set_dwarf2_cmdlist;
19687 static struct cmd_list_element *show_dwarf2_cmdlist;
19688
19689 static void
19690 set_dwarf2_cmd (char *args, int from_tty)
19691 {
19692 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
19693 }
19694
19695 static void
19696 show_dwarf2_cmd (char *args, int from_tty)
19697 {
19698 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
19699 }
19700
19701 /* Free data associated with OBJFILE, if necessary. */
19702
19703 static void
19704 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
19705 {
19706 struct dwarf2_per_objfile *data = d;
19707 int ix;
19708
19709 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
19710 VEC_free (dwarf2_per_cu_ptr,
19711 dwarf2_per_objfile->all_comp_units[ix]->imported_symtabs);
19712
19713 for (ix = 0; ix < dwarf2_per_objfile->n_type_units; ++ix)
19714 VEC_free (dwarf2_per_cu_ptr,
19715 dwarf2_per_objfile->all_type_units[ix]->per_cu.imported_symtabs);
19716
19717 VEC_free (dwarf2_section_info_def, data->types);
19718
19719 if (data->dwo_files)
19720 free_dwo_files (data->dwo_files, objfile);
19721
19722 if (data->dwz_file && data->dwz_file->dwz_bfd)
19723 gdb_bfd_unref (data->dwz_file->dwz_bfd);
19724 }
19725
19726 \f
19727 /* The "save gdb-index" command. */
19728
19729 /* The contents of the hash table we create when building the string
19730 table. */
19731 struct strtab_entry
19732 {
19733 offset_type offset;
19734 const char *str;
19735 };
19736
19737 /* Hash function for a strtab_entry.
19738
19739 Function is used only during write_hash_table so no index format backward
19740 compatibility is needed. */
19741
19742 static hashval_t
19743 hash_strtab_entry (const void *e)
19744 {
19745 const struct strtab_entry *entry = e;
19746 return mapped_index_string_hash (INT_MAX, entry->str);
19747 }
19748
19749 /* Equality function for a strtab_entry. */
19750
19751 static int
19752 eq_strtab_entry (const void *a, const void *b)
19753 {
19754 const struct strtab_entry *ea = a;
19755 const struct strtab_entry *eb = b;
19756 return !strcmp (ea->str, eb->str);
19757 }
19758
19759 /* Create a strtab_entry hash table. */
19760
19761 static htab_t
19762 create_strtab (void)
19763 {
19764 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
19765 xfree, xcalloc, xfree);
19766 }
19767
19768 /* Add a string to the constant pool. Return the string's offset in
19769 host order. */
19770
19771 static offset_type
19772 add_string (htab_t table, struct obstack *cpool, const char *str)
19773 {
19774 void **slot;
19775 struct strtab_entry entry;
19776 struct strtab_entry *result;
19777
19778 entry.str = str;
19779 slot = htab_find_slot (table, &entry, INSERT);
19780 if (*slot)
19781 result = *slot;
19782 else
19783 {
19784 result = XNEW (struct strtab_entry);
19785 result->offset = obstack_object_size (cpool);
19786 result->str = str;
19787 obstack_grow_str0 (cpool, str);
19788 *slot = result;
19789 }
19790 return result->offset;
19791 }
19792
19793 /* An entry in the symbol table. */
19794 struct symtab_index_entry
19795 {
19796 /* The name of the symbol. */
19797 const char *name;
19798 /* The offset of the name in the constant pool. */
19799 offset_type index_offset;
19800 /* A sorted vector of the indices of all the CUs that hold an object
19801 of this name. */
19802 VEC (offset_type) *cu_indices;
19803 };
19804
19805 /* The symbol table. This is a power-of-2-sized hash table. */
19806 struct mapped_symtab
19807 {
19808 offset_type n_elements;
19809 offset_type size;
19810 struct symtab_index_entry **data;
19811 };
19812
19813 /* Hash function for a symtab_index_entry. */
19814
19815 static hashval_t
19816 hash_symtab_entry (const void *e)
19817 {
19818 const struct symtab_index_entry *entry = e;
19819 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
19820 sizeof (offset_type) * VEC_length (offset_type,
19821 entry->cu_indices),
19822 0);
19823 }
19824
19825 /* Equality function for a symtab_index_entry. */
19826
19827 static int
19828 eq_symtab_entry (const void *a, const void *b)
19829 {
19830 const struct symtab_index_entry *ea = a;
19831 const struct symtab_index_entry *eb = b;
19832 int len = VEC_length (offset_type, ea->cu_indices);
19833 if (len != VEC_length (offset_type, eb->cu_indices))
19834 return 0;
19835 return !memcmp (VEC_address (offset_type, ea->cu_indices),
19836 VEC_address (offset_type, eb->cu_indices),
19837 sizeof (offset_type) * len);
19838 }
19839
19840 /* Destroy a symtab_index_entry. */
19841
19842 static void
19843 delete_symtab_entry (void *p)
19844 {
19845 struct symtab_index_entry *entry = p;
19846 VEC_free (offset_type, entry->cu_indices);
19847 xfree (entry);
19848 }
19849
19850 /* Create a hash table holding symtab_index_entry objects. */
19851
19852 static htab_t
19853 create_symbol_hash_table (void)
19854 {
19855 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
19856 delete_symtab_entry, xcalloc, xfree);
19857 }
19858
19859 /* Create a new mapped symtab object. */
19860
19861 static struct mapped_symtab *
19862 create_mapped_symtab (void)
19863 {
19864 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
19865 symtab->n_elements = 0;
19866 symtab->size = 1024;
19867 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19868 return symtab;
19869 }
19870
19871 /* Destroy a mapped_symtab. */
19872
19873 static void
19874 cleanup_mapped_symtab (void *p)
19875 {
19876 struct mapped_symtab *symtab = p;
19877 /* The contents of the array are freed when the other hash table is
19878 destroyed. */
19879 xfree (symtab->data);
19880 xfree (symtab);
19881 }
19882
19883 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
19884 the slot.
19885
19886 Function is used only during write_hash_table so no index format backward
19887 compatibility is needed. */
19888
19889 static struct symtab_index_entry **
19890 find_slot (struct mapped_symtab *symtab, const char *name)
19891 {
19892 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
19893
19894 index = hash & (symtab->size - 1);
19895 step = ((hash * 17) & (symtab->size - 1)) | 1;
19896
19897 for (;;)
19898 {
19899 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
19900 return &symtab->data[index];
19901 index = (index + step) & (symtab->size - 1);
19902 }
19903 }
19904
19905 /* Expand SYMTAB's hash table. */
19906
19907 static void
19908 hash_expand (struct mapped_symtab *symtab)
19909 {
19910 offset_type old_size = symtab->size;
19911 offset_type i;
19912 struct symtab_index_entry **old_entries = symtab->data;
19913
19914 symtab->size *= 2;
19915 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19916
19917 for (i = 0; i < old_size; ++i)
19918 {
19919 if (old_entries[i])
19920 {
19921 struct symtab_index_entry **slot = find_slot (symtab,
19922 old_entries[i]->name);
19923 *slot = old_entries[i];
19924 }
19925 }
19926
19927 xfree (old_entries);
19928 }
19929
19930 /* Add an entry to SYMTAB. NAME is the name of the symbol.
19931 CU_INDEX is the index of the CU in which the symbol appears.
19932 IS_STATIC is one if the symbol is static, otherwise zero (global). */
19933
19934 static void
19935 add_index_entry (struct mapped_symtab *symtab, const char *name,
19936 int is_static, gdb_index_symbol_kind kind,
19937 offset_type cu_index)
19938 {
19939 struct symtab_index_entry **slot;
19940 offset_type cu_index_and_attrs;
19941
19942 ++symtab->n_elements;
19943 if (4 * symtab->n_elements / 3 >= symtab->size)
19944 hash_expand (symtab);
19945
19946 slot = find_slot (symtab, name);
19947 if (!*slot)
19948 {
19949 *slot = XNEW (struct symtab_index_entry);
19950 (*slot)->name = name;
19951 /* index_offset is set later. */
19952 (*slot)->cu_indices = NULL;
19953 }
19954
19955 cu_index_and_attrs = 0;
19956 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
19957 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
19958 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
19959
19960 /* We don't want to record an index value twice as we want to avoid the
19961 duplication.
19962 We process all global symbols and then all static symbols
19963 (which would allow us to avoid the duplication by only having to check
19964 the last entry pushed), but a symbol could have multiple kinds in one CU.
19965 To keep things simple we don't worry about the duplication here and
19966 sort and uniqufy the list after we've processed all symbols. */
19967 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
19968 }
19969
19970 /* qsort helper routine for uniquify_cu_indices. */
19971
19972 static int
19973 offset_type_compare (const void *ap, const void *bp)
19974 {
19975 offset_type a = *(offset_type *) ap;
19976 offset_type b = *(offset_type *) bp;
19977
19978 return (a > b) - (b > a);
19979 }
19980
19981 /* Sort and remove duplicates of all symbols' cu_indices lists. */
19982
19983 static void
19984 uniquify_cu_indices (struct mapped_symtab *symtab)
19985 {
19986 int i;
19987
19988 for (i = 0; i < symtab->size; ++i)
19989 {
19990 struct symtab_index_entry *entry = symtab->data[i];
19991
19992 if (entry
19993 && entry->cu_indices != NULL)
19994 {
19995 unsigned int next_to_insert, next_to_check;
19996 offset_type last_value;
19997
19998 qsort (VEC_address (offset_type, entry->cu_indices),
19999 VEC_length (offset_type, entry->cu_indices),
20000 sizeof (offset_type), offset_type_compare);
20001
20002 last_value = VEC_index (offset_type, entry->cu_indices, 0);
20003 next_to_insert = 1;
20004 for (next_to_check = 1;
20005 next_to_check < VEC_length (offset_type, entry->cu_indices);
20006 ++next_to_check)
20007 {
20008 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
20009 != last_value)
20010 {
20011 last_value = VEC_index (offset_type, entry->cu_indices,
20012 next_to_check);
20013 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
20014 last_value);
20015 ++next_to_insert;
20016 }
20017 }
20018 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
20019 }
20020 }
20021 }
20022
20023 /* Add a vector of indices to the constant pool. */
20024
20025 static offset_type
20026 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
20027 struct symtab_index_entry *entry)
20028 {
20029 void **slot;
20030
20031 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
20032 if (!*slot)
20033 {
20034 offset_type len = VEC_length (offset_type, entry->cu_indices);
20035 offset_type val = MAYBE_SWAP (len);
20036 offset_type iter;
20037 int i;
20038
20039 *slot = entry;
20040 entry->index_offset = obstack_object_size (cpool);
20041
20042 obstack_grow (cpool, &val, sizeof (val));
20043 for (i = 0;
20044 VEC_iterate (offset_type, entry->cu_indices, i, iter);
20045 ++i)
20046 {
20047 val = MAYBE_SWAP (iter);
20048 obstack_grow (cpool, &val, sizeof (val));
20049 }
20050 }
20051 else
20052 {
20053 struct symtab_index_entry *old_entry = *slot;
20054 entry->index_offset = old_entry->index_offset;
20055 entry = old_entry;
20056 }
20057 return entry->index_offset;
20058 }
20059
20060 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
20061 constant pool entries going into the obstack CPOOL. */
20062
20063 static void
20064 write_hash_table (struct mapped_symtab *symtab,
20065 struct obstack *output, struct obstack *cpool)
20066 {
20067 offset_type i;
20068 htab_t symbol_hash_table;
20069 htab_t str_table;
20070
20071 symbol_hash_table = create_symbol_hash_table ();
20072 str_table = create_strtab ();
20073
20074 /* We add all the index vectors to the constant pool first, to
20075 ensure alignment is ok. */
20076 for (i = 0; i < symtab->size; ++i)
20077 {
20078 if (symtab->data[i])
20079 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
20080 }
20081
20082 /* Now write out the hash table. */
20083 for (i = 0; i < symtab->size; ++i)
20084 {
20085 offset_type str_off, vec_off;
20086
20087 if (symtab->data[i])
20088 {
20089 str_off = add_string (str_table, cpool, symtab->data[i]->name);
20090 vec_off = symtab->data[i]->index_offset;
20091 }
20092 else
20093 {
20094 /* While 0 is a valid constant pool index, it is not valid
20095 to have 0 for both offsets. */
20096 str_off = 0;
20097 vec_off = 0;
20098 }
20099
20100 str_off = MAYBE_SWAP (str_off);
20101 vec_off = MAYBE_SWAP (vec_off);
20102
20103 obstack_grow (output, &str_off, sizeof (str_off));
20104 obstack_grow (output, &vec_off, sizeof (vec_off));
20105 }
20106
20107 htab_delete (str_table);
20108 htab_delete (symbol_hash_table);
20109 }
20110
20111 /* Struct to map psymtab to CU index in the index file. */
20112 struct psymtab_cu_index_map
20113 {
20114 struct partial_symtab *psymtab;
20115 unsigned int cu_index;
20116 };
20117
20118 static hashval_t
20119 hash_psymtab_cu_index (const void *item)
20120 {
20121 const struct psymtab_cu_index_map *map = item;
20122
20123 return htab_hash_pointer (map->psymtab);
20124 }
20125
20126 static int
20127 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
20128 {
20129 const struct psymtab_cu_index_map *lhs = item_lhs;
20130 const struct psymtab_cu_index_map *rhs = item_rhs;
20131
20132 return lhs->psymtab == rhs->psymtab;
20133 }
20134
20135 /* Helper struct for building the address table. */
20136 struct addrmap_index_data
20137 {
20138 struct objfile *objfile;
20139 struct obstack *addr_obstack;
20140 htab_t cu_index_htab;
20141
20142 /* Non-zero if the previous_* fields are valid.
20143 We can't write an entry until we see the next entry (since it is only then
20144 that we know the end of the entry). */
20145 int previous_valid;
20146 /* Index of the CU in the table of all CUs in the index file. */
20147 unsigned int previous_cu_index;
20148 /* Start address of the CU. */
20149 CORE_ADDR previous_cu_start;
20150 };
20151
20152 /* Write an address entry to OBSTACK. */
20153
20154 static void
20155 add_address_entry (struct objfile *objfile, struct obstack *obstack,
20156 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
20157 {
20158 offset_type cu_index_to_write;
20159 char addr[8];
20160 CORE_ADDR baseaddr;
20161
20162 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20163
20164 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
20165 obstack_grow (obstack, addr, 8);
20166 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
20167 obstack_grow (obstack, addr, 8);
20168 cu_index_to_write = MAYBE_SWAP (cu_index);
20169 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
20170 }
20171
20172 /* Worker function for traversing an addrmap to build the address table. */
20173
20174 static int
20175 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
20176 {
20177 struct addrmap_index_data *data = datap;
20178 struct partial_symtab *pst = obj;
20179
20180 if (data->previous_valid)
20181 add_address_entry (data->objfile, data->addr_obstack,
20182 data->previous_cu_start, start_addr,
20183 data->previous_cu_index);
20184
20185 data->previous_cu_start = start_addr;
20186 if (pst != NULL)
20187 {
20188 struct psymtab_cu_index_map find_map, *map;
20189 find_map.psymtab = pst;
20190 map = htab_find (data->cu_index_htab, &find_map);
20191 gdb_assert (map != NULL);
20192 data->previous_cu_index = map->cu_index;
20193 data->previous_valid = 1;
20194 }
20195 else
20196 data->previous_valid = 0;
20197
20198 return 0;
20199 }
20200
20201 /* Write OBJFILE's address map to OBSTACK.
20202 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
20203 in the index file. */
20204
20205 static void
20206 write_address_map (struct objfile *objfile, struct obstack *obstack,
20207 htab_t cu_index_htab)
20208 {
20209 struct addrmap_index_data addrmap_index_data;
20210
20211 /* When writing the address table, we have to cope with the fact that
20212 the addrmap iterator only provides the start of a region; we have to
20213 wait until the next invocation to get the start of the next region. */
20214
20215 addrmap_index_data.objfile = objfile;
20216 addrmap_index_data.addr_obstack = obstack;
20217 addrmap_index_data.cu_index_htab = cu_index_htab;
20218 addrmap_index_data.previous_valid = 0;
20219
20220 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
20221 &addrmap_index_data);
20222
20223 /* It's highly unlikely the last entry (end address = 0xff...ff)
20224 is valid, but we should still handle it.
20225 The end address is recorded as the start of the next region, but that
20226 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
20227 anyway. */
20228 if (addrmap_index_data.previous_valid)
20229 add_address_entry (objfile, obstack,
20230 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
20231 addrmap_index_data.previous_cu_index);
20232 }
20233
20234 /* Return the symbol kind of PSYM. */
20235
20236 static gdb_index_symbol_kind
20237 symbol_kind (struct partial_symbol *psym)
20238 {
20239 domain_enum domain = PSYMBOL_DOMAIN (psym);
20240 enum address_class aclass = PSYMBOL_CLASS (psym);
20241
20242 switch (domain)
20243 {
20244 case VAR_DOMAIN:
20245 switch (aclass)
20246 {
20247 case LOC_BLOCK:
20248 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
20249 case LOC_TYPEDEF:
20250 return GDB_INDEX_SYMBOL_KIND_TYPE;
20251 case LOC_COMPUTED:
20252 case LOC_CONST_BYTES:
20253 case LOC_OPTIMIZED_OUT:
20254 case LOC_STATIC:
20255 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20256 case LOC_CONST:
20257 /* Note: It's currently impossible to recognize psyms as enum values
20258 short of reading the type info. For now punt. */
20259 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20260 default:
20261 /* There are other LOC_FOO values that one might want to classify
20262 as variables, but dwarf2read.c doesn't currently use them. */
20263 return GDB_INDEX_SYMBOL_KIND_OTHER;
20264 }
20265 case STRUCT_DOMAIN:
20266 return GDB_INDEX_SYMBOL_KIND_TYPE;
20267 default:
20268 return GDB_INDEX_SYMBOL_KIND_OTHER;
20269 }
20270 }
20271
20272 /* Add a list of partial symbols to SYMTAB. */
20273
20274 static void
20275 write_psymbols (struct mapped_symtab *symtab,
20276 htab_t psyms_seen,
20277 struct partial_symbol **psymp,
20278 int count,
20279 offset_type cu_index,
20280 int is_static)
20281 {
20282 for (; count-- > 0; ++psymp)
20283 {
20284 struct partial_symbol *psym = *psymp;
20285 void **slot;
20286
20287 if (SYMBOL_LANGUAGE (psym) == language_ada)
20288 error (_("Ada is not currently supported by the index"));
20289
20290 /* Only add a given psymbol once. */
20291 slot = htab_find_slot (psyms_seen, psym, INSERT);
20292 if (!*slot)
20293 {
20294 gdb_index_symbol_kind kind = symbol_kind (psym);
20295
20296 *slot = psym;
20297 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
20298 is_static, kind, cu_index);
20299 }
20300 }
20301 }
20302
20303 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
20304 exception if there is an error. */
20305
20306 static void
20307 write_obstack (FILE *file, struct obstack *obstack)
20308 {
20309 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
20310 file)
20311 != obstack_object_size (obstack))
20312 error (_("couldn't data write to file"));
20313 }
20314
20315 /* Unlink a file if the argument is not NULL. */
20316
20317 static void
20318 unlink_if_set (void *p)
20319 {
20320 char **filename = p;
20321 if (*filename)
20322 unlink (*filename);
20323 }
20324
20325 /* A helper struct used when iterating over debug_types. */
20326 struct signatured_type_index_data
20327 {
20328 struct objfile *objfile;
20329 struct mapped_symtab *symtab;
20330 struct obstack *types_list;
20331 htab_t psyms_seen;
20332 int cu_index;
20333 };
20334
20335 /* A helper function that writes a single signatured_type to an
20336 obstack. */
20337
20338 static int
20339 write_one_signatured_type (void **slot, void *d)
20340 {
20341 struct signatured_type_index_data *info = d;
20342 struct signatured_type *entry = (struct signatured_type *) *slot;
20343 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
20344 struct partial_symtab *psymtab = per_cu->v.psymtab;
20345 gdb_byte val[8];
20346
20347 write_psymbols (info->symtab,
20348 info->psyms_seen,
20349 info->objfile->global_psymbols.list
20350 + psymtab->globals_offset,
20351 psymtab->n_global_syms, info->cu_index,
20352 0);
20353 write_psymbols (info->symtab,
20354 info->psyms_seen,
20355 info->objfile->static_psymbols.list
20356 + psymtab->statics_offset,
20357 psymtab->n_static_syms, info->cu_index,
20358 1);
20359
20360 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20361 entry->per_cu.offset.sect_off);
20362 obstack_grow (info->types_list, val, 8);
20363 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20364 entry->type_offset_in_tu.cu_off);
20365 obstack_grow (info->types_list, val, 8);
20366 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
20367 obstack_grow (info->types_list, val, 8);
20368
20369 ++info->cu_index;
20370
20371 return 1;
20372 }
20373
20374 /* Recurse into all "included" dependencies and write their symbols as
20375 if they appeared in this psymtab. */
20376
20377 static void
20378 recursively_write_psymbols (struct objfile *objfile,
20379 struct partial_symtab *psymtab,
20380 struct mapped_symtab *symtab,
20381 htab_t psyms_seen,
20382 offset_type cu_index)
20383 {
20384 int i;
20385
20386 for (i = 0; i < psymtab->number_of_dependencies; ++i)
20387 if (psymtab->dependencies[i]->user != NULL)
20388 recursively_write_psymbols (objfile, psymtab->dependencies[i],
20389 symtab, psyms_seen, cu_index);
20390
20391 write_psymbols (symtab,
20392 psyms_seen,
20393 objfile->global_psymbols.list + psymtab->globals_offset,
20394 psymtab->n_global_syms, cu_index,
20395 0);
20396 write_psymbols (symtab,
20397 psyms_seen,
20398 objfile->static_psymbols.list + psymtab->statics_offset,
20399 psymtab->n_static_syms, cu_index,
20400 1);
20401 }
20402
20403 /* Create an index file for OBJFILE in the directory DIR. */
20404
20405 static void
20406 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
20407 {
20408 struct cleanup *cleanup;
20409 char *filename, *cleanup_filename;
20410 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
20411 struct obstack cu_list, types_cu_list;
20412 int i;
20413 FILE *out_file;
20414 struct mapped_symtab *symtab;
20415 offset_type val, size_of_contents, total_len;
20416 struct stat st;
20417 htab_t psyms_seen;
20418 htab_t cu_index_htab;
20419 struct psymtab_cu_index_map *psymtab_cu_index_map;
20420
20421 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
20422 return;
20423
20424 if (dwarf2_per_objfile->using_index)
20425 error (_("Cannot use an index to create the index"));
20426
20427 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
20428 error (_("Cannot make an index when the file has multiple .debug_types sections"));
20429
20430 if (stat (objfile->name, &st) < 0)
20431 perror_with_name (objfile->name);
20432
20433 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
20434 INDEX_SUFFIX, (char *) NULL);
20435 cleanup = make_cleanup (xfree, filename);
20436
20437 out_file = fopen (filename, "wb");
20438 if (!out_file)
20439 error (_("Can't open `%s' for writing"), filename);
20440
20441 cleanup_filename = filename;
20442 make_cleanup (unlink_if_set, &cleanup_filename);
20443
20444 symtab = create_mapped_symtab ();
20445 make_cleanup (cleanup_mapped_symtab, symtab);
20446
20447 obstack_init (&addr_obstack);
20448 make_cleanup_obstack_free (&addr_obstack);
20449
20450 obstack_init (&cu_list);
20451 make_cleanup_obstack_free (&cu_list);
20452
20453 obstack_init (&types_cu_list);
20454 make_cleanup_obstack_free (&types_cu_list);
20455
20456 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
20457 NULL, xcalloc, xfree);
20458 make_cleanup_htab_delete (psyms_seen);
20459
20460 /* While we're scanning CU's create a table that maps a psymtab pointer
20461 (which is what addrmap records) to its index (which is what is recorded
20462 in the index file). This will later be needed to write the address
20463 table. */
20464 cu_index_htab = htab_create_alloc (100,
20465 hash_psymtab_cu_index,
20466 eq_psymtab_cu_index,
20467 NULL, xcalloc, xfree);
20468 make_cleanup_htab_delete (cu_index_htab);
20469 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
20470 xmalloc (sizeof (struct psymtab_cu_index_map)
20471 * dwarf2_per_objfile->n_comp_units);
20472 make_cleanup (xfree, psymtab_cu_index_map);
20473
20474 /* The CU list is already sorted, so we don't need to do additional
20475 work here. Also, the debug_types entries do not appear in
20476 all_comp_units, but only in their own hash table. */
20477 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
20478 {
20479 struct dwarf2_per_cu_data *per_cu
20480 = dwarf2_per_objfile->all_comp_units[i];
20481 struct partial_symtab *psymtab = per_cu->v.psymtab;
20482 gdb_byte val[8];
20483 struct psymtab_cu_index_map *map;
20484 void **slot;
20485
20486 if (psymtab->user == NULL)
20487 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
20488
20489 map = &psymtab_cu_index_map[i];
20490 map->psymtab = psymtab;
20491 map->cu_index = i;
20492 slot = htab_find_slot (cu_index_htab, map, INSERT);
20493 gdb_assert (slot != NULL);
20494 gdb_assert (*slot == NULL);
20495 *slot = map;
20496
20497 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20498 per_cu->offset.sect_off);
20499 obstack_grow (&cu_list, val, 8);
20500 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
20501 obstack_grow (&cu_list, val, 8);
20502 }
20503
20504 /* Dump the address map. */
20505 write_address_map (objfile, &addr_obstack, cu_index_htab);
20506
20507 /* Write out the .debug_type entries, if any. */
20508 if (dwarf2_per_objfile->signatured_types)
20509 {
20510 struct signatured_type_index_data sig_data;
20511
20512 sig_data.objfile = objfile;
20513 sig_data.symtab = symtab;
20514 sig_data.types_list = &types_cu_list;
20515 sig_data.psyms_seen = psyms_seen;
20516 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
20517 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
20518 write_one_signatured_type, &sig_data);
20519 }
20520
20521 /* Now that we've processed all symbols we can shrink their cu_indices
20522 lists. */
20523 uniquify_cu_indices (symtab);
20524
20525 obstack_init (&constant_pool);
20526 make_cleanup_obstack_free (&constant_pool);
20527 obstack_init (&symtab_obstack);
20528 make_cleanup_obstack_free (&symtab_obstack);
20529 write_hash_table (symtab, &symtab_obstack, &constant_pool);
20530
20531 obstack_init (&contents);
20532 make_cleanup_obstack_free (&contents);
20533 size_of_contents = 6 * sizeof (offset_type);
20534 total_len = size_of_contents;
20535
20536 /* The version number. */
20537 val = MAYBE_SWAP (8);
20538 obstack_grow (&contents, &val, sizeof (val));
20539
20540 /* The offset of the CU list from the start of the file. */
20541 val = MAYBE_SWAP (total_len);
20542 obstack_grow (&contents, &val, sizeof (val));
20543 total_len += obstack_object_size (&cu_list);
20544
20545 /* The offset of the types CU list from the start of the file. */
20546 val = MAYBE_SWAP (total_len);
20547 obstack_grow (&contents, &val, sizeof (val));
20548 total_len += obstack_object_size (&types_cu_list);
20549
20550 /* The offset of the address table from the start of the file. */
20551 val = MAYBE_SWAP (total_len);
20552 obstack_grow (&contents, &val, sizeof (val));
20553 total_len += obstack_object_size (&addr_obstack);
20554
20555 /* The offset of the symbol table from the start of the file. */
20556 val = MAYBE_SWAP (total_len);
20557 obstack_grow (&contents, &val, sizeof (val));
20558 total_len += obstack_object_size (&symtab_obstack);
20559
20560 /* The offset of the constant pool from the start of the file. */
20561 val = MAYBE_SWAP (total_len);
20562 obstack_grow (&contents, &val, sizeof (val));
20563 total_len += obstack_object_size (&constant_pool);
20564
20565 gdb_assert (obstack_object_size (&contents) == size_of_contents);
20566
20567 write_obstack (out_file, &contents);
20568 write_obstack (out_file, &cu_list);
20569 write_obstack (out_file, &types_cu_list);
20570 write_obstack (out_file, &addr_obstack);
20571 write_obstack (out_file, &symtab_obstack);
20572 write_obstack (out_file, &constant_pool);
20573
20574 fclose (out_file);
20575
20576 /* We want to keep the file, so we set cleanup_filename to NULL
20577 here. See unlink_if_set. */
20578 cleanup_filename = NULL;
20579
20580 do_cleanups (cleanup);
20581 }
20582
20583 /* Implementation of the `save gdb-index' command.
20584
20585 Note that the file format used by this command is documented in the
20586 GDB manual. Any changes here must be documented there. */
20587
20588 static void
20589 save_gdb_index_command (char *arg, int from_tty)
20590 {
20591 struct objfile *objfile;
20592
20593 if (!arg || !*arg)
20594 error (_("usage: save gdb-index DIRECTORY"));
20595
20596 ALL_OBJFILES (objfile)
20597 {
20598 struct stat st;
20599
20600 /* If the objfile does not correspond to an actual file, skip it. */
20601 if (stat (objfile->name, &st) < 0)
20602 continue;
20603
20604 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
20605 if (dwarf2_per_objfile)
20606 {
20607 volatile struct gdb_exception except;
20608
20609 TRY_CATCH (except, RETURN_MASK_ERROR)
20610 {
20611 write_psymtabs_to_index (objfile, arg);
20612 }
20613 if (except.reason < 0)
20614 exception_fprintf (gdb_stderr, except,
20615 _("Error while writing index for `%s': "),
20616 objfile->name);
20617 }
20618 }
20619 }
20620
20621 \f
20622
20623 int dwarf2_always_disassemble;
20624
20625 static void
20626 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
20627 struct cmd_list_element *c, const char *value)
20628 {
20629 fprintf_filtered (file,
20630 _("Whether to always disassemble "
20631 "DWARF expressions is %s.\n"),
20632 value);
20633 }
20634
20635 static void
20636 show_check_physname (struct ui_file *file, int from_tty,
20637 struct cmd_list_element *c, const char *value)
20638 {
20639 fprintf_filtered (file,
20640 _("Whether to check \"physname\" is %s.\n"),
20641 value);
20642 }
20643
20644 void _initialize_dwarf2_read (void);
20645
20646 void
20647 _initialize_dwarf2_read (void)
20648 {
20649 struct cmd_list_element *c;
20650
20651 dwarf2_objfile_data_key
20652 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
20653
20654 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
20655 Set DWARF 2 specific variables.\n\
20656 Configure DWARF 2 variables such as the cache size"),
20657 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
20658 0/*allow-unknown*/, &maintenance_set_cmdlist);
20659
20660 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
20661 Show DWARF 2 specific variables\n\
20662 Show DWARF 2 variables such as the cache size"),
20663 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
20664 0/*allow-unknown*/, &maintenance_show_cmdlist);
20665
20666 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
20667 &dwarf2_max_cache_age, _("\
20668 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
20669 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
20670 A higher limit means that cached compilation units will be stored\n\
20671 in memory longer, and more total memory will be used. Zero disables\n\
20672 caching, which can slow down startup."),
20673 NULL,
20674 show_dwarf2_max_cache_age,
20675 &set_dwarf2_cmdlist,
20676 &show_dwarf2_cmdlist);
20677
20678 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
20679 &dwarf2_always_disassemble, _("\
20680 Set whether `info address' always disassembles DWARF expressions."), _("\
20681 Show whether `info address' always disassembles DWARF expressions."), _("\
20682 When enabled, DWARF expressions are always printed in an assembly-like\n\
20683 syntax. When disabled, expressions will be printed in a more\n\
20684 conversational style, when possible."),
20685 NULL,
20686 show_dwarf2_always_disassemble,
20687 &set_dwarf2_cmdlist,
20688 &show_dwarf2_cmdlist);
20689
20690 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
20691 Set debugging of the dwarf2 reader."), _("\
20692 Show debugging of the dwarf2 reader."), _("\
20693 When enabled, debugging messages are printed during dwarf2 reading\n\
20694 and symtab expansion."),
20695 NULL,
20696 NULL,
20697 &setdebuglist, &showdebuglist);
20698
20699 add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
20700 Set debugging of the dwarf2 DIE reader."), _("\
20701 Show debugging of the dwarf2 DIE reader."), _("\
20702 When enabled (non-zero), DIEs are dumped after they are read in.\n\
20703 The value is the maximum depth to print."),
20704 NULL,
20705 NULL,
20706 &setdebuglist, &showdebuglist);
20707
20708 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
20709 Set cross-checking of \"physname\" code against demangler."), _("\
20710 Show cross-checking of \"physname\" code against demangler."), _("\
20711 When enabled, GDB's internal \"physname\" code is checked against\n\
20712 the demangler."),
20713 NULL, show_check_physname,
20714 &setdebuglist, &showdebuglist);
20715
20716 add_setshow_boolean_cmd ("use-deprecated-index-sections",
20717 no_class, &use_deprecated_index_sections, _("\
20718 Set whether to use deprecated gdb_index sections."), _("\
20719 Show whether to use deprecated gdb_index sections."), _("\
20720 When enabled, deprecated .gdb_index sections are used anyway.\n\
20721 Normally they are ignored either because of a missing feature or\n\
20722 performance issue.\n\
20723 Warning: This option must be enabled before gdb reads the file."),
20724 NULL,
20725 NULL,
20726 &setlist, &showlist);
20727
20728 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
20729 _("\
20730 Save a gdb-index file.\n\
20731 Usage: save gdb-index DIRECTORY"),
20732 &save_cmdlist);
20733 set_cmd_completer (c, filename_completer);
20734
20735 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
20736 &dwarf2_locexpr_funcs);
20737 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
20738 &dwarf2_loclist_funcs);
20739
20740 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
20741 &dwarf2_block_frame_base_locexpr_funcs);
20742 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
20743 &dwarf2_block_frame_base_loclist_funcs);
20744 }
This page took 0.476628 seconds and 4 git commands to generate.