DWARF-5: call sites
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2017 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 "psympriv.h"
57 #include <sys/stat.h>
58 #include "completer.h"
59 #include "vec.h"
60 #include "c-lang.h"
61 #include "go-lang.h"
62 #include "valprint.h"
63 #include "gdbcore.h" /* for gnutarget */
64 #include "gdb/gdb-index.h"
65 #include <ctype.h>
66 #include "gdb_bfd.h"
67 #include "f-lang.h"
68 #include "source.h"
69 #include "filestuff.h"
70 #include "build-id.h"
71 #include "namespace.h"
72 #include "common/gdb_unlinker.h"
73
74 #include <fcntl.h>
75 #include <sys/types.h>
76 #include <algorithm>
77
78 typedef struct symbol *symbolp;
79 DEF_VEC_P (symbolp);
80
81 /* When == 1, print basic high level tracing messages.
82 When > 1, be more verbose.
83 This is in contrast to the low level DIE reading of dwarf_die_debug. */
84 static unsigned int dwarf_read_debug = 0;
85
86 /* When non-zero, dump DIEs after they are read in. */
87 static unsigned int dwarf_die_debug = 0;
88
89 /* When non-zero, dump line number entries as they are read in. */
90 static unsigned int dwarf_line_debug = 0;
91
92 /* When non-zero, cross-check physname against demangler. */
93 static int check_physname = 0;
94
95 /* When non-zero, do not reject deprecated .gdb_index sections. */
96 static int use_deprecated_index_sections = 0;
97
98 static const struct objfile_data *dwarf2_objfile_data_key;
99
100 /* The "aclass" indices for various kinds of computed DWARF symbols. */
101
102 static int dwarf2_locexpr_index;
103 static int dwarf2_loclist_index;
104 static int dwarf2_locexpr_block_index;
105 static int dwarf2_loclist_block_index;
106
107 /* A descriptor for dwarf sections.
108
109 S.ASECTION, SIZE are typically initialized when the objfile is first
110 scanned. BUFFER, READIN are filled in later when the section is read.
111 If the section contained compressed data then SIZE is updated to record
112 the uncompressed size of the section.
113
114 DWP file format V2 introduces a wrinkle that is easiest to handle by
115 creating the concept of virtual sections contained within a real section.
116 In DWP V2 the sections of the input DWO files are concatenated together
117 into one section, but section offsets are kept relative to the original
118 input section.
119 If this is a virtual dwp-v2 section, S.CONTAINING_SECTION is a backlink to
120 the real section this "virtual" section is contained in, and BUFFER,SIZE
121 describe the virtual section. */
122
123 struct dwarf2_section_info
124 {
125 union
126 {
127 /* If this is a real section, the bfd section. */
128 asection *section;
129 /* If this is a virtual section, pointer to the containing ("real")
130 section. */
131 struct dwarf2_section_info *containing_section;
132 } s;
133 /* Pointer to section data, only valid if readin. */
134 const gdb_byte *buffer;
135 /* The size of the section, real or virtual. */
136 bfd_size_type size;
137 /* If this is a virtual section, the offset in the real section.
138 Only valid if is_virtual. */
139 bfd_size_type virtual_offset;
140 /* True if we have tried to read this section. */
141 char readin;
142 /* True if this is a virtual section, False otherwise.
143 This specifies which of s.section and s.containing_section to use. */
144 char is_virtual;
145 };
146
147 typedef struct dwarf2_section_info dwarf2_section_info_def;
148 DEF_VEC_O (dwarf2_section_info_def);
149
150 /* All offsets in the index are of this type. It must be
151 architecture-independent. */
152 typedef uint32_t offset_type;
153
154 DEF_VEC_I (offset_type);
155
156 /* Ensure only legit values are used. */
157 #define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
158 do { \
159 gdb_assert ((unsigned int) (value) <= 1); \
160 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
161 } while (0)
162
163 /* Ensure only legit values are used. */
164 #define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
165 do { \
166 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
167 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
168 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
169 } while (0)
170
171 /* Ensure we don't use more than the alloted nuber of bits for the CU. */
172 #define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
173 do { \
174 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
175 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
176 } while (0)
177
178 /* A description of the mapped index. The file format is described in
179 a comment by the code that writes the index. */
180 struct mapped_index
181 {
182 /* Index data format version. */
183 int version;
184
185 /* The total length of the buffer. */
186 off_t total_size;
187
188 /* A pointer to the address table data. */
189 const gdb_byte *address_table;
190
191 /* Size of the address table data in bytes. */
192 offset_type address_table_size;
193
194 /* The symbol table, implemented as a hash table. */
195 const offset_type *symbol_table;
196
197 /* Size in slots, each slot is 2 offset_types. */
198 offset_type symbol_table_slots;
199
200 /* A pointer to the constant pool. */
201 const char *constant_pool;
202 };
203
204 typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
205 DEF_VEC_P (dwarf2_per_cu_ptr);
206
207 struct tu_stats
208 {
209 int nr_uniq_abbrev_tables;
210 int nr_symtabs;
211 int nr_symtab_sharers;
212 int nr_stmt_less_type_units;
213 int nr_all_type_units_reallocs;
214 };
215
216 /* Collection of data recorded per objfile.
217 This hangs off of dwarf2_objfile_data_key. */
218
219 struct dwarf2_per_objfile
220 {
221 struct dwarf2_section_info info;
222 struct dwarf2_section_info abbrev;
223 struct dwarf2_section_info line;
224 struct dwarf2_section_info loc;
225 struct dwarf2_section_info loclists;
226 struct dwarf2_section_info macinfo;
227 struct dwarf2_section_info macro;
228 struct dwarf2_section_info str;
229 struct dwarf2_section_info line_str;
230 struct dwarf2_section_info ranges;
231 struct dwarf2_section_info rnglists;
232 struct dwarf2_section_info addr;
233 struct dwarf2_section_info frame;
234 struct dwarf2_section_info eh_frame;
235 struct dwarf2_section_info gdb_index;
236
237 VEC (dwarf2_section_info_def) *types;
238
239 /* Back link. */
240 struct objfile *objfile;
241
242 /* Table of all the compilation units. This is used to locate
243 the target compilation unit of a particular reference. */
244 struct dwarf2_per_cu_data **all_comp_units;
245
246 /* The number of compilation units in ALL_COMP_UNITS. */
247 int n_comp_units;
248
249 /* The number of .debug_types-related CUs. */
250 int n_type_units;
251
252 /* The number of elements allocated in all_type_units.
253 If there are skeleton-less TUs, we add them to all_type_units lazily. */
254 int n_allocated_type_units;
255
256 /* The .debug_types-related CUs (TUs).
257 This is stored in malloc space because we may realloc it. */
258 struct signatured_type **all_type_units;
259
260 /* Table of struct type_unit_group objects.
261 The hash key is the DW_AT_stmt_list value. */
262 htab_t type_unit_groups;
263
264 /* A table mapping .debug_types signatures to its signatured_type entry.
265 This is NULL if the .debug_types section hasn't been read in yet. */
266 htab_t signatured_types;
267
268 /* Type unit statistics, to see how well the scaling improvements
269 are doing. */
270 struct tu_stats tu_stats;
271
272 /* A chain of compilation units that are currently read in, so that
273 they can be freed later. */
274 struct dwarf2_per_cu_data *read_in_chain;
275
276 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
277 This is NULL if the table hasn't been allocated yet. */
278 htab_t dwo_files;
279
280 /* Non-zero if we've check for whether there is a DWP file. */
281 int dwp_checked;
282
283 /* The DWP file if there is one, or NULL. */
284 struct dwp_file *dwp_file;
285
286 /* The shared '.dwz' file, if one exists. This is used when the
287 original data was compressed using 'dwz -m'. */
288 struct dwz_file *dwz_file;
289
290 /* A flag indicating wether this objfile has a section loaded at a
291 VMA of 0. */
292 int has_section_at_zero;
293
294 /* True if we are using the mapped index,
295 or we are faking it for OBJF_READNOW's sake. */
296 unsigned char using_index;
297
298 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
299 struct mapped_index *index_table;
300
301 /* When using index_table, this keeps track of all quick_file_names entries.
302 TUs typically share line table entries with a CU, so we maintain a
303 separate table of all line table entries to support the sharing.
304 Note that while there can be way more TUs than CUs, we've already
305 sorted all the TUs into "type unit groups", grouped by their
306 DW_AT_stmt_list value. Therefore the only sharing done here is with a
307 CU and its associated TU group if there is one. */
308 htab_t quick_file_names_table;
309
310 /* Set during partial symbol reading, to prevent queueing of full
311 symbols. */
312 int reading_partial_symbols;
313
314 /* Table mapping type DIEs to their struct type *.
315 This is NULL if not allocated yet.
316 The mapping is done via (CU/TU + DIE offset) -> type. */
317 htab_t die_type_hash;
318
319 /* The CUs we recently read. */
320 VEC (dwarf2_per_cu_ptr) *just_read_cus;
321
322 /* Table containing line_header indexed by offset and offset_in_dwz. */
323 htab_t line_header_hash;
324 };
325
326 static struct dwarf2_per_objfile *dwarf2_per_objfile;
327
328 /* Default names of the debugging sections. */
329
330 /* Note that if the debugging section has been compressed, it might
331 have a name like .zdebug_info. */
332
333 static const struct dwarf2_debug_sections dwarf2_elf_names =
334 {
335 { ".debug_info", ".zdebug_info" },
336 { ".debug_abbrev", ".zdebug_abbrev" },
337 { ".debug_line", ".zdebug_line" },
338 { ".debug_loc", ".zdebug_loc" },
339 { ".debug_loclists", ".zdebug_loclists" },
340 { ".debug_macinfo", ".zdebug_macinfo" },
341 { ".debug_macro", ".zdebug_macro" },
342 { ".debug_str", ".zdebug_str" },
343 { ".debug_line_str", ".zdebug_line_str" },
344 { ".debug_ranges", ".zdebug_ranges" },
345 { ".debug_rnglists", ".zdebug_rnglists" },
346 { ".debug_types", ".zdebug_types" },
347 { ".debug_addr", ".zdebug_addr" },
348 { ".debug_frame", ".zdebug_frame" },
349 { ".eh_frame", NULL },
350 { ".gdb_index", ".zgdb_index" },
351 23
352 };
353
354 /* List of DWO/DWP sections. */
355
356 static const struct dwop_section_names
357 {
358 struct dwarf2_section_names abbrev_dwo;
359 struct dwarf2_section_names info_dwo;
360 struct dwarf2_section_names line_dwo;
361 struct dwarf2_section_names loc_dwo;
362 struct dwarf2_section_names loclists_dwo;
363 struct dwarf2_section_names macinfo_dwo;
364 struct dwarf2_section_names macro_dwo;
365 struct dwarf2_section_names str_dwo;
366 struct dwarf2_section_names str_offsets_dwo;
367 struct dwarf2_section_names types_dwo;
368 struct dwarf2_section_names cu_index;
369 struct dwarf2_section_names tu_index;
370 }
371 dwop_section_names =
372 {
373 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
374 { ".debug_info.dwo", ".zdebug_info.dwo" },
375 { ".debug_line.dwo", ".zdebug_line.dwo" },
376 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
377 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
378 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
379 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
380 { ".debug_str.dwo", ".zdebug_str.dwo" },
381 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
382 { ".debug_types.dwo", ".zdebug_types.dwo" },
383 { ".debug_cu_index", ".zdebug_cu_index" },
384 { ".debug_tu_index", ".zdebug_tu_index" },
385 };
386
387 /* local data types */
388
389 /* The data in a compilation unit header, after target2host
390 translation, looks like this. */
391 struct comp_unit_head
392 {
393 unsigned int length;
394 short version;
395 unsigned char addr_size;
396 unsigned char signed_addr_p;
397 sect_offset abbrev_offset;
398
399 /* Size of file offsets; either 4 or 8. */
400 unsigned int offset_size;
401
402 /* Size of the length field; either 4 or 12. */
403 unsigned int initial_length_size;
404
405 enum dwarf_unit_type unit_type;
406
407 /* Offset to the first byte of this compilation unit header in the
408 .debug_info section, for resolving relative reference dies. */
409 sect_offset offset;
410
411 /* Offset to first die in this cu from the start of the cu.
412 This will be the first byte following the compilation unit header. */
413 cu_offset first_die_offset;
414
415 /* 64-bit signature of this type unit - it is valid only for
416 UNIT_TYPE DW_UT_type. */
417 ULONGEST signature;
418
419 /* For types, offset in the type's DIE of the type defined by this TU. */
420 cu_offset type_offset_in_tu;
421 };
422
423 /* Type used for delaying computation of method physnames.
424 See comments for compute_delayed_physnames. */
425 struct delayed_method_info
426 {
427 /* The type to which the method is attached, i.e., its parent class. */
428 struct type *type;
429
430 /* The index of the method in the type's function fieldlists. */
431 int fnfield_index;
432
433 /* The index of the method in the fieldlist. */
434 int index;
435
436 /* The name of the DIE. */
437 const char *name;
438
439 /* The DIE associated with this method. */
440 struct die_info *die;
441 };
442
443 typedef struct delayed_method_info delayed_method_info;
444 DEF_VEC_O (delayed_method_info);
445
446 /* Internal state when decoding a particular compilation unit. */
447 struct dwarf2_cu
448 {
449 /* The objfile containing this compilation unit. */
450 struct objfile *objfile;
451
452 /* The header of the compilation unit. */
453 struct comp_unit_head header;
454
455 /* Base address of this compilation unit. */
456 CORE_ADDR base_address;
457
458 /* Non-zero if base_address has been set. */
459 int base_known;
460
461 /* The language we are debugging. */
462 enum language language;
463 const struct language_defn *language_defn;
464
465 const char *producer;
466
467 /* The generic symbol table building routines have separate lists for
468 file scope symbols and all all other scopes (local scopes). So
469 we need to select the right one to pass to add_symbol_to_list().
470 We do it by keeping a pointer to the correct list in list_in_scope.
471
472 FIXME: The original dwarf code just treated the file scope as the
473 first local scope, and all other local scopes as nested local
474 scopes, and worked fine. Check to see if we really need to
475 distinguish these in buildsym.c. */
476 struct pending **list_in_scope;
477
478 /* The abbrev table for this CU.
479 Normally this points to the abbrev table in the objfile.
480 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
481 struct abbrev_table *abbrev_table;
482
483 /* Hash table holding all the loaded partial DIEs
484 with partial_die->offset.SECT_OFF as hash. */
485 htab_t partial_dies;
486
487 /* Storage for things with the same lifetime as this read-in compilation
488 unit, including partial DIEs. */
489 struct obstack comp_unit_obstack;
490
491 /* When multiple dwarf2_cu structures are living in memory, this field
492 chains them all together, so that they can be released efficiently.
493 We will probably also want a generation counter so that most-recently-used
494 compilation units are cached... */
495 struct dwarf2_per_cu_data *read_in_chain;
496
497 /* Backlink to our per_cu entry. */
498 struct dwarf2_per_cu_data *per_cu;
499
500 /* How many compilation units ago was this CU last referenced? */
501 int last_used;
502
503 /* A hash table of DIE cu_offset for following references with
504 die_info->offset.sect_off as hash. */
505 htab_t die_hash;
506
507 /* Full DIEs if read in. */
508 struct die_info *dies;
509
510 /* A set of pointers to dwarf2_per_cu_data objects for compilation
511 units referenced by this one. Only set during full symbol processing;
512 partial symbol tables do not have dependencies. */
513 htab_t dependencies;
514
515 /* Header data from the line table, during full symbol processing. */
516 struct line_header *line_header;
517
518 /* A list of methods which need to have physnames computed
519 after all type information has been read. */
520 VEC (delayed_method_info) *method_list;
521
522 /* To be copied to symtab->call_site_htab. */
523 htab_t call_site_htab;
524
525 /* Non-NULL if this CU came from a DWO file.
526 There is an invariant here that is important to remember:
527 Except for attributes copied from the top level DIE in the "main"
528 (or "stub") file in preparation for reading the DWO file
529 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
530 Either there isn't a DWO file (in which case this is NULL and the point
531 is moot), or there is and either we're not going to read it (in which
532 case this is NULL) or there is and we are reading it (in which case this
533 is non-NULL). */
534 struct dwo_unit *dwo_unit;
535
536 /* The DW_AT_addr_base attribute if present, zero otherwise
537 (zero is a valid value though).
538 Note this value comes from the Fission stub CU/TU's DIE. */
539 ULONGEST addr_base;
540
541 /* The DW_AT_ranges_base attribute if present, zero otherwise
542 (zero is a valid value though).
543 Note this value comes from the Fission stub CU/TU's DIE.
544 Also note that the value is zero in the non-DWO case so this value can
545 be used without needing to know whether DWO files are in use or not.
546 N.B. This does not apply to DW_AT_ranges appearing in
547 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
548 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
549 DW_AT_ranges_base *would* have to be applied, and we'd have to care
550 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
551 ULONGEST ranges_base;
552
553 /* Mark used when releasing cached dies. */
554 unsigned int mark : 1;
555
556 /* This CU references .debug_loc. See the symtab->locations_valid field.
557 This test is imperfect as there may exist optimized debug code not using
558 any location list and still facing inlining issues if handled as
559 unoptimized code. For a future better test see GCC PR other/32998. */
560 unsigned int has_loclist : 1;
561
562 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
563 if all the producer_is_* fields are valid. This information is cached
564 because profiling CU expansion showed excessive time spent in
565 producer_is_gxx_lt_4_6. */
566 unsigned int checked_producer : 1;
567 unsigned int producer_is_gxx_lt_4_6 : 1;
568 unsigned int producer_is_gcc_lt_4_3 : 1;
569 unsigned int producer_is_icc : 1;
570
571 /* When set, the file that we're processing is known to have
572 debugging info for C++ namespaces. GCC 3.3.x did not produce
573 this information, but later versions do. */
574
575 unsigned int processing_has_namespace_info : 1;
576 };
577
578 /* Persistent data held for a compilation unit, even when not
579 processing it. We put a pointer to this structure in the
580 read_symtab_private field of the psymtab. */
581
582 struct dwarf2_per_cu_data
583 {
584 /* The start offset and length of this compilation unit.
585 NOTE: Unlike comp_unit_head.length, this length includes
586 initial_length_size.
587 If the DIE refers to a DWO file, this is always of the original die,
588 not the DWO file. */
589 sect_offset offset;
590 unsigned int length;
591
592 /* DWARF standard version this data has been read from (such as 4 or 5). */
593 short dwarf_version;
594
595 /* Flag indicating this compilation unit will be read in before
596 any of the current compilation units are processed. */
597 unsigned int queued : 1;
598
599 /* This flag will be set when reading partial DIEs if we need to load
600 absolutely all DIEs for this compilation unit, instead of just the ones
601 we think are interesting. It gets set if we look for a DIE in the
602 hash table and don't find it. */
603 unsigned int load_all_dies : 1;
604
605 /* Non-zero if this CU is from .debug_types.
606 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
607 this is non-zero. */
608 unsigned int is_debug_types : 1;
609
610 /* Non-zero if this CU is from the .dwz file. */
611 unsigned int is_dwz : 1;
612
613 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
614 This flag is only valid if is_debug_types is true.
615 We can't read a CU directly from a DWO file: There are required
616 attributes in the stub. */
617 unsigned int reading_dwo_directly : 1;
618
619 /* Non-zero if the TU has been read.
620 This is used to assist the "Stay in DWO Optimization" for Fission:
621 When reading a DWO, it's faster to read TUs from the DWO instead of
622 fetching them from random other DWOs (due to comdat folding).
623 If the TU has already been read, the optimization is unnecessary
624 (and unwise - we don't want to change where gdb thinks the TU lives
625 "midflight").
626 This flag is only valid if is_debug_types is true. */
627 unsigned int tu_read : 1;
628
629 /* The section this CU/TU lives in.
630 If the DIE refers to a DWO file, this is always the original die,
631 not the DWO file. */
632 struct dwarf2_section_info *section;
633
634 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
635 of the CU cache it gets reset to NULL again. This is left as NULL for
636 dummy CUs (a CU header, but nothing else). */
637 struct dwarf2_cu *cu;
638
639 /* The corresponding objfile.
640 Normally we can get the objfile from dwarf2_per_objfile.
641 However we can enter this file with just a "per_cu" handle. */
642 struct objfile *objfile;
643
644 /* When dwarf2_per_objfile->using_index is true, the 'quick' field
645 is active. Otherwise, the 'psymtab' field is active. */
646 union
647 {
648 /* The partial symbol table associated with this compilation unit,
649 or NULL for unread partial units. */
650 struct partial_symtab *psymtab;
651
652 /* Data needed by the "quick" functions. */
653 struct dwarf2_per_cu_quick_data *quick;
654 } v;
655
656 /* The CUs we import using DW_TAG_imported_unit. This is filled in
657 while reading psymtabs, used to compute the psymtab dependencies,
658 and then cleared. Then it is filled in again while reading full
659 symbols, and only deleted when the objfile is destroyed.
660
661 This is also used to work around a difference between the way gold
662 generates .gdb_index version <=7 and the way gdb does. Arguably this
663 is a gold bug. For symbols coming from TUs, gold records in the index
664 the CU that includes the TU instead of the TU itself. This breaks
665 dw2_lookup_symbol: It assumes that if the index says symbol X lives
666 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
667 will find X. Alas TUs live in their own symtab, so after expanding CU Y
668 we need to look in TU Z to find X. Fortunately, this is akin to
669 DW_TAG_imported_unit, so we just use the same mechanism: For
670 .gdb_index version <=7 this also records the TUs that the CU referred
671 to. Concurrently with this change gdb was modified to emit version 8
672 indices so we only pay a price for gold generated indices.
673 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
674 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
675 };
676
677 /* Entry in the signatured_types hash table. */
678
679 struct signatured_type
680 {
681 /* The "per_cu" object of this type.
682 This struct is used iff per_cu.is_debug_types.
683 N.B.: This is the first member so that it's easy to convert pointers
684 between them. */
685 struct dwarf2_per_cu_data per_cu;
686
687 /* The type's signature. */
688 ULONGEST signature;
689
690 /* Offset in the TU of the type's DIE, as read from the TU header.
691 If this TU is a DWO stub and the definition lives in a DWO file
692 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
693 cu_offset type_offset_in_tu;
694
695 /* Offset in the section of the type's DIE.
696 If the definition lives in a DWO file, this is the offset in the
697 .debug_types.dwo section.
698 The value is zero until the actual value is known.
699 Zero is otherwise not a valid section offset. */
700 sect_offset type_offset_in_section;
701
702 /* Type units are grouped by their DW_AT_stmt_list entry so that they
703 can share them. This points to the containing symtab. */
704 struct type_unit_group *type_unit_group;
705
706 /* The type.
707 The first time we encounter this type we fully read it in and install it
708 in the symbol tables. Subsequent times we only need the type. */
709 struct type *type;
710
711 /* Containing DWO unit.
712 This field is valid iff per_cu.reading_dwo_directly. */
713 struct dwo_unit *dwo_unit;
714 };
715
716 typedef struct signatured_type *sig_type_ptr;
717 DEF_VEC_P (sig_type_ptr);
718
719 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
720 This includes type_unit_group and quick_file_names. */
721
722 struct stmt_list_hash
723 {
724 /* The DWO unit this table is from or NULL if there is none. */
725 struct dwo_unit *dwo_unit;
726
727 /* Offset in .debug_line or .debug_line.dwo. */
728 sect_offset line_offset;
729 };
730
731 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
732 an object of this type. */
733
734 struct type_unit_group
735 {
736 /* dwarf2read.c's main "handle" on a TU symtab.
737 To simplify things we create an artificial CU that "includes" all the
738 type units using this stmt_list so that the rest of the code still has
739 a "per_cu" handle on the symtab.
740 This PER_CU is recognized by having no section. */
741 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
742 struct dwarf2_per_cu_data per_cu;
743
744 /* The TUs that share this DW_AT_stmt_list entry.
745 This is added to while parsing type units to build partial symtabs,
746 and is deleted afterwards and not used again. */
747 VEC (sig_type_ptr) *tus;
748
749 /* The compunit symtab.
750 Type units in a group needn't all be defined in the same source file,
751 so we create an essentially anonymous symtab as the compunit symtab. */
752 struct compunit_symtab *compunit_symtab;
753
754 /* The data used to construct the hash key. */
755 struct stmt_list_hash hash;
756
757 /* The number of symtabs from the line header.
758 The value here must match line_header.num_file_names. */
759 unsigned int num_symtabs;
760
761 /* The symbol tables for this TU (obtained from the files listed in
762 DW_AT_stmt_list).
763 WARNING: The order of entries here must match the order of entries
764 in the line header. After the first TU using this type_unit_group, the
765 line header for the subsequent TUs is recreated from this. This is done
766 because we need to use the same symtabs for each TU using the same
767 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
768 there's no guarantee the line header doesn't have duplicate entries. */
769 struct symtab **symtabs;
770 };
771
772 /* These sections are what may appear in a (real or virtual) DWO file. */
773
774 struct dwo_sections
775 {
776 struct dwarf2_section_info abbrev;
777 struct dwarf2_section_info line;
778 struct dwarf2_section_info loc;
779 struct dwarf2_section_info loclists;
780 struct dwarf2_section_info macinfo;
781 struct dwarf2_section_info macro;
782 struct dwarf2_section_info str;
783 struct dwarf2_section_info str_offsets;
784 /* In the case of a virtual DWO file, these two are unused. */
785 struct dwarf2_section_info info;
786 VEC (dwarf2_section_info_def) *types;
787 };
788
789 /* CUs/TUs in DWP/DWO files. */
790
791 struct dwo_unit
792 {
793 /* Backlink to the containing struct dwo_file. */
794 struct dwo_file *dwo_file;
795
796 /* The "id" that distinguishes this CU/TU.
797 .debug_info calls this "dwo_id", .debug_types calls this "signature".
798 Since signatures came first, we stick with it for consistency. */
799 ULONGEST signature;
800
801 /* The section this CU/TU lives in, in the DWO file. */
802 struct dwarf2_section_info *section;
803
804 /* Same as dwarf2_per_cu_data:{offset,length} but in the DWO section. */
805 sect_offset offset;
806 unsigned int length;
807
808 /* For types, offset in the type's DIE of the type defined by this TU. */
809 cu_offset type_offset_in_tu;
810 };
811
812 /* include/dwarf2.h defines the DWP section codes.
813 It defines a max value but it doesn't define a min value, which we
814 use for error checking, so provide one. */
815
816 enum dwp_v2_section_ids
817 {
818 DW_SECT_MIN = 1
819 };
820
821 /* Data for one DWO file.
822
823 This includes virtual DWO files (a virtual DWO file is a DWO file as it
824 appears in a DWP file). DWP files don't really have DWO files per se -
825 comdat folding of types "loses" the DWO file they came from, and from
826 a high level view DWP files appear to contain a mass of random types.
827 However, to maintain consistency with the non-DWP case we pretend DWP
828 files contain virtual DWO files, and we assign each TU with one virtual
829 DWO file (generally based on the line and abbrev section offsets -
830 a heuristic that seems to work in practice). */
831
832 struct dwo_file
833 {
834 /* The DW_AT_GNU_dwo_name attribute.
835 For virtual DWO files the name is constructed from the section offsets
836 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
837 from related CU+TUs. */
838 const char *dwo_name;
839
840 /* The DW_AT_comp_dir attribute. */
841 const char *comp_dir;
842
843 /* The bfd, when the file is open. Otherwise this is NULL.
844 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
845 bfd *dbfd;
846
847 /* The sections that make up this DWO file.
848 Remember that for virtual DWO files in DWP V2, these are virtual
849 sections (for lack of a better name). */
850 struct dwo_sections sections;
851
852 /* The CU in the file.
853 We only support one because having more than one requires hacking the
854 dwo_name of each to match, which is highly unlikely to happen.
855 Doing this means all TUs can share comp_dir: We also assume that
856 DW_AT_comp_dir across all TUs in a DWO file will be identical. */
857 struct dwo_unit *cu;
858
859 /* Table of TUs in the file.
860 Each element is a struct dwo_unit. */
861 htab_t tus;
862 };
863
864 /* These sections are what may appear in a DWP file. */
865
866 struct dwp_sections
867 {
868 /* These are used by both DWP version 1 and 2. */
869 struct dwarf2_section_info str;
870 struct dwarf2_section_info cu_index;
871 struct dwarf2_section_info tu_index;
872
873 /* These are only used by DWP version 2 files.
874 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
875 sections are referenced by section number, and are not recorded here.
876 In DWP version 2 there is at most one copy of all these sections, each
877 section being (effectively) comprised of the concatenation of all of the
878 individual sections that exist in the version 1 format.
879 To keep the code simple we treat each of these concatenated pieces as a
880 section itself (a virtual section?). */
881 struct dwarf2_section_info abbrev;
882 struct dwarf2_section_info info;
883 struct dwarf2_section_info line;
884 struct dwarf2_section_info loc;
885 struct dwarf2_section_info macinfo;
886 struct dwarf2_section_info macro;
887 struct dwarf2_section_info str_offsets;
888 struct dwarf2_section_info types;
889 };
890
891 /* These sections are what may appear in a virtual DWO file in DWP version 1.
892 A virtual DWO file is a DWO file as it appears in a DWP file. */
893
894 struct virtual_v1_dwo_sections
895 {
896 struct dwarf2_section_info abbrev;
897 struct dwarf2_section_info line;
898 struct dwarf2_section_info loc;
899 struct dwarf2_section_info macinfo;
900 struct dwarf2_section_info macro;
901 struct dwarf2_section_info str_offsets;
902 /* Each DWP hash table entry records one CU or one TU.
903 That is recorded here, and copied to dwo_unit.section. */
904 struct dwarf2_section_info info_or_types;
905 };
906
907 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
908 In version 2, the sections of the DWO files are concatenated together
909 and stored in one section of that name. Thus each ELF section contains
910 several "virtual" sections. */
911
912 struct virtual_v2_dwo_sections
913 {
914 bfd_size_type abbrev_offset;
915 bfd_size_type abbrev_size;
916
917 bfd_size_type line_offset;
918 bfd_size_type line_size;
919
920 bfd_size_type loc_offset;
921 bfd_size_type loc_size;
922
923 bfd_size_type macinfo_offset;
924 bfd_size_type macinfo_size;
925
926 bfd_size_type macro_offset;
927 bfd_size_type macro_size;
928
929 bfd_size_type str_offsets_offset;
930 bfd_size_type str_offsets_size;
931
932 /* Each DWP hash table entry records one CU or one TU.
933 That is recorded here, and copied to dwo_unit.section. */
934 bfd_size_type info_or_types_offset;
935 bfd_size_type info_or_types_size;
936 };
937
938 /* Contents of DWP hash tables. */
939
940 struct dwp_hash_table
941 {
942 uint32_t version, nr_columns;
943 uint32_t nr_units, nr_slots;
944 const gdb_byte *hash_table, *unit_table;
945 union
946 {
947 struct
948 {
949 const gdb_byte *indices;
950 } v1;
951 struct
952 {
953 /* This is indexed by column number and gives the id of the section
954 in that column. */
955 #define MAX_NR_V2_DWO_SECTIONS \
956 (1 /* .debug_info or .debug_types */ \
957 + 1 /* .debug_abbrev */ \
958 + 1 /* .debug_line */ \
959 + 1 /* .debug_loc */ \
960 + 1 /* .debug_str_offsets */ \
961 + 1 /* .debug_macro or .debug_macinfo */)
962 int section_ids[MAX_NR_V2_DWO_SECTIONS];
963 const gdb_byte *offsets;
964 const gdb_byte *sizes;
965 } v2;
966 } section_pool;
967 };
968
969 /* Data for one DWP file. */
970
971 struct dwp_file
972 {
973 /* Name of the file. */
974 const char *name;
975
976 /* File format version. */
977 int version;
978
979 /* The bfd. */
980 bfd *dbfd;
981
982 /* Section info for this file. */
983 struct dwp_sections sections;
984
985 /* Table of CUs in the file. */
986 const struct dwp_hash_table *cus;
987
988 /* Table of TUs in the file. */
989 const struct dwp_hash_table *tus;
990
991 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
992 htab_t loaded_cus;
993 htab_t loaded_tus;
994
995 /* Table to map ELF section numbers to their sections.
996 This is only needed for the DWP V1 file format. */
997 unsigned int num_sections;
998 asection **elf_sections;
999 };
1000
1001 /* This represents a '.dwz' file. */
1002
1003 struct dwz_file
1004 {
1005 /* A dwz file can only contain a few sections. */
1006 struct dwarf2_section_info abbrev;
1007 struct dwarf2_section_info info;
1008 struct dwarf2_section_info str;
1009 struct dwarf2_section_info line;
1010 struct dwarf2_section_info macro;
1011 struct dwarf2_section_info gdb_index;
1012
1013 /* The dwz's BFD. */
1014 bfd *dwz_bfd;
1015 };
1016
1017 /* Struct used to pass misc. parameters to read_die_and_children, et
1018 al. which are used for both .debug_info and .debug_types dies.
1019 All parameters here are unchanging for the life of the call. This
1020 struct exists to abstract away the constant parameters of die reading. */
1021
1022 struct die_reader_specs
1023 {
1024 /* The bfd of die_section. */
1025 bfd* abfd;
1026
1027 /* The CU of the DIE we are parsing. */
1028 struct dwarf2_cu *cu;
1029
1030 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
1031 struct dwo_file *dwo_file;
1032
1033 /* The section the die comes from.
1034 This is either .debug_info or .debug_types, or the .dwo variants. */
1035 struct dwarf2_section_info *die_section;
1036
1037 /* die_section->buffer. */
1038 const gdb_byte *buffer;
1039
1040 /* The end of the buffer. */
1041 const gdb_byte *buffer_end;
1042
1043 /* The value of the DW_AT_comp_dir attribute. */
1044 const char *comp_dir;
1045 };
1046
1047 /* Type of function passed to init_cutu_and_read_dies, et.al. */
1048 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
1049 const gdb_byte *info_ptr,
1050 struct die_info *comp_unit_die,
1051 int has_children,
1052 void *data);
1053
1054 struct file_entry
1055 {
1056 const char *name;
1057 unsigned int dir_index;
1058 unsigned int mod_time;
1059 unsigned int length;
1060 /* Non-zero if referenced by the Line Number Program. */
1061 int included_p;
1062 /* The associated symbol table, if any. */
1063 struct symtab *symtab;
1064 };
1065
1066 /* The line number information for a compilation unit (found in the
1067 .debug_line section) begins with a "statement program header",
1068 which contains the following information. */
1069 struct line_header
1070 {
1071 /* Offset of line number information in .debug_line section. */
1072 sect_offset offset;
1073
1074 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1075 unsigned offset_in_dwz : 1;
1076
1077 unsigned int total_length;
1078 unsigned short version;
1079 unsigned int header_length;
1080 unsigned char minimum_instruction_length;
1081 unsigned char maximum_ops_per_instruction;
1082 unsigned char default_is_stmt;
1083 int line_base;
1084 unsigned char line_range;
1085 unsigned char opcode_base;
1086
1087 /* standard_opcode_lengths[i] is the number of operands for the
1088 standard opcode whose value is i. This means that
1089 standard_opcode_lengths[0] is unused, and the last meaningful
1090 element is standard_opcode_lengths[opcode_base - 1]. */
1091 unsigned char *standard_opcode_lengths;
1092
1093 /* The include_directories table. NOTE! These strings are not
1094 allocated with xmalloc; instead, they are pointers into
1095 debug_line_buffer. If you try to free them, `free' will get
1096 indigestion. */
1097 unsigned int num_include_dirs, include_dirs_size;
1098 const char **include_dirs;
1099
1100 /* The file_names table. NOTE! These strings are not allocated
1101 with xmalloc; instead, they are pointers into debug_line_buffer.
1102 Don't try to free them directly. */
1103 unsigned int num_file_names, file_names_size;
1104 struct file_entry *file_names;
1105
1106 /* The start and end of the statement program following this
1107 header. These point into dwarf2_per_objfile->line_buffer. */
1108 const gdb_byte *statement_program_start, *statement_program_end;
1109 };
1110
1111 /* When we construct a partial symbol table entry we only
1112 need this much information. */
1113 struct partial_die_info
1114 {
1115 /* Offset of this DIE. */
1116 sect_offset offset;
1117
1118 /* DWARF-2 tag for this DIE. */
1119 ENUM_BITFIELD(dwarf_tag) tag : 16;
1120
1121 /* Assorted flags describing the data found in this DIE. */
1122 unsigned int has_children : 1;
1123 unsigned int is_external : 1;
1124 unsigned int is_declaration : 1;
1125 unsigned int has_type : 1;
1126 unsigned int has_specification : 1;
1127 unsigned int has_pc_info : 1;
1128 unsigned int may_be_inlined : 1;
1129
1130 /* This DIE has been marked DW_AT_main_subprogram. */
1131 unsigned int main_subprogram : 1;
1132
1133 /* Flag set if the SCOPE field of this structure has been
1134 computed. */
1135 unsigned int scope_set : 1;
1136
1137 /* Flag set if the DIE has a byte_size attribute. */
1138 unsigned int has_byte_size : 1;
1139
1140 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1141 unsigned int has_const_value : 1;
1142
1143 /* Flag set if any of the DIE's children are template arguments. */
1144 unsigned int has_template_arguments : 1;
1145
1146 /* Flag set if fixup_partial_die has been called on this die. */
1147 unsigned int fixup_called : 1;
1148
1149 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1150 unsigned int is_dwz : 1;
1151
1152 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1153 unsigned int spec_is_dwz : 1;
1154
1155 /* The name of this DIE. Normally the value of DW_AT_name, but
1156 sometimes a default name for unnamed DIEs. */
1157 const char *name;
1158
1159 /* The linkage name, if present. */
1160 const char *linkage_name;
1161
1162 /* The scope to prepend to our children. This is generally
1163 allocated on the comp_unit_obstack, so will disappear
1164 when this compilation unit leaves the cache. */
1165 const char *scope;
1166
1167 /* Some data associated with the partial DIE. The tag determines
1168 which field is live. */
1169 union
1170 {
1171 /* The location description associated with this DIE, if any. */
1172 struct dwarf_block *locdesc;
1173 /* The offset of an import, for DW_TAG_imported_unit. */
1174 sect_offset offset;
1175 } d;
1176
1177 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1178 CORE_ADDR lowpc;
1179 CORE_ADDR highpc;
1180
1181 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1182 DW_AT_sibling, if any. */
1183 /* NOTE: This member isn't strictly necessary, read_partial_die could
1184 return DW_AT_sibling values to its caller load_partial_dies. */
1185 const gdb_byte *sibling;
1186
1187 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1188 DW_AT_specification (or DW_AT_abstract_origin or
1189 DW_AT_extension). */
1190 sect_offset spec_offset;
1191
1192 /* Pointers to this DIE's parent, first child, and next sibling,
1193 if any. */
1194 struct partial_die_info *die_parent, *die_child, *die_sibling;
1195 };
1196
1197 /* This data structure holds the information of an abbrev. */
1198 struct abbrev_info
1199 {
1200 unsigned int number; /* number identifying abbrev */
1201 enum dwarf_tag tag; /* dwarf tag */
1202 unsigned short has_children; /* boolean */
1203 unsigned short num_attrs; /* number of attributes */
1204 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1205 struct abbrev_info *next; /* next in chain */
1206 };
1207
1208 struct attr_abbrev
1209 {
1210 ENUM_BITFIELD(dwarf_attribute) name : 16;
1211 ENUM_BITFIELD(dwarf_form) form : 16;
1212
1213 /* It is valid only if FORM is DW_FORM_implicit_const. */
1214 LONGEST implicit_const;
1215 };
1216
1217 /* Size of abbrev_table.abbrev_hash_table. */
1218 #define ABBREV_HASH_SIZE 121
1219
1220 /* Top level data structure to contain an abbreviation table. */
1221
1222 struct abbrev_table
1223 {
1224 /* Where the abbrev table came from.
1225 This is used as a sanity check when the table is used. */
1226 sect_offset offset;
1227
1228 /* Storage for the abbrev table. */
1229 struct obstack abbrev_obstack;
1230
1231 /* Hash table of abbrevs.
1232 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1233 It could be statically allocated, but the previous code didn't so we
1234 don't either. */
1235 struct abbrev_info **abbrevs;
1236 };
1237
1238 /* Attributes have a name and a value. */
1239 struct attribute
1240 {
1241 ENUM_BITFIELD(dwarf_attribute) name : 16;
1242 ENUM_BITFIELD(dwarf_form) form : 15;
1243
1244 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1245 field should be in u.str (existing only for DW_STRING) but it is kept
1246 here for better struct attribute alignment. */
1247 unsigned int string_is_canonical : 1;
1248
1249 union
1250 {
1251 const char *str;
1252 struct dwarf_block *blk;
1253 ULONGEST unsnd;
1254 LONGEST snd;
1255 CORE_ADDR addr;
1256 ULONGEST signature;
1257 }
1258 u;
1259 };
1260
1261 /* This data structure holds a complete die structure. */
1262 struct die_info
1263 {
1264 /* DWARF-2 tag for this DIE. */
1265 ENUM_BITFIELD(dwarf_tag) tag : 16;
1266
1267 /* Number of attributes */
1268 unsigned char num_attrs;
1269
1270 /* True if we're presently building the full type name for the
1271 type derived from this DIE. */
1272 unsigned char building_fullname : 1;
1273
1274 /* True if this die is in process. PR 16581. */
1275 unsigned char in_process : 1;
1276
1277 /* Abbrev number */
1278 unsigned int abbrev;
1279
1280 /* Offset in .debug_info or .debug_types section. */
1281 sect_offset offset;
1282
1283 /* The dies in a compilation unit form an n-ary tree. PARENT
1284 points to this die's parent; CHILD points to the first child of
1285 this node; and all the children of a given node are chained
1286 together via their SIBLING fields. */
1287 struct die_info *child; /* Its first child, if any. */
1288 struct die_info *sibling; /* Its next sibling, if any. */
1289 struct die_info *parent; /* Its parent, if any. */
1290
1291 /* An array of attributes, with NUM_ATTRS elements. There may be
1292 zero, but it's not common and zero-sized arrays are not
1293 sufficiently portable C. */
1294 struct attribute attrs[1];
1295 };
1296
1297 /* Get at parts of an attribute structure. */
1298
1299 #define DW_STRING(attr) ((attr)->u.str)
1300 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1301 #define DW_UNSND(attr) ((attr)->u.unsnd)
1302 #define DW_BLOCK(attr) ((attr)->u.blk)
1303 #define DW_SND(attr) ((attr)->u.snd)
1304 #define DW_ADDR(attr) ((attr)->u.addr)
1305 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1306
1307 /* Blocks are a bunch of untyped bytes. */
1308 struct dwarf_block
1309 {
1310 size_t size;
1311
1312 /* Valid only if SIZE is not zero. */
1313 const gdb_byte *data;
1314 };
1315
1316 #ifndef ATTR_ALLOC_CHUNK
1317 #define ATTR_ALLOC_CHUNK 4
1318 #endif
1319
1320 /* Allocate fields for structs, unions and enums in this size. */
1321 #ifndef DW_FIELD_ALLOC_CHUNK
1322 #define DW_FIELD_ALLOC_CHUNK 4
1323 #endif
1324
1325 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1326 but this would require a corresponding change in unpack_field_as_long
1327 and friends. */
1328 static int bits_per_byte = 8;
1329
1330 struct nextfield
1331 {
1332 struct nextfield *next;
1333 int accessibility;
1334 int virtuality;
1335 struct field field;
1336 };
1337
1338 struct nextfnfield
1339 {
1340 struct nextfnfield *next;
1341 struct fn_field fnfield;
1342 };
1343
1344 struct fnfieldlist
1345 {
1346 const char *name;
1347 int length;
1348 struct nextfnfield *head;
1349 };
1350
1351 struct typedef_field_list
1352 {
1353 struct typedef_field field;
1354 struct typedef_field_list *next;
1355 };
1356
1357 /* The routines that read and process dies for a C struct or C++ class
1358 pass lists of data member fields and lists of member function fields
1359 in an instance of a field_info structure, as defined below. */
1360 struct field_info
1361 {
1362 /* List of data member and baseclasses fields. */
1363 struct nextfield *fields, *baseclasses;
1364
1365 /* Number of fields (including baseclasses). */
1366 int nfields;
1367
1368 /* Number of baseclasses. */
1369 int nbaseclasses;
1370
1371 /* Set if the accesibility of one of the fields is not public. */
1372 int non_public_fields;
1373
1374 /* Member function fields array, entries are allocated in the order they
1375 are encountered in the object file. */
1376 struct nextfnfield *fnfields;
1377
1378 /* Member function fieldlist array, contains name of possibly overloaded
1379 member function, number of overloaded member functions and a pointer
1380 to the head of the member function field chain. */
1381 struct fnfieldlist *fnfieldlists;
1382
1383 /* Number of entries in the fnfieldlists array. */
1384 int nfnfields;
1385
1386 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1387 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1388 struct typedef_field_list *typedef_field_list;
1389 unsigned typedef_field_list_count;
1390 };
1391
1392 /* One item on the queue of compilation units to read in full symbols
1393 for. */
1394 struct dwarf2_queue_item
1395 {
1396 struct dwarf2_per_cu_data *per_cu;
1397 enum language pretend_language;
1398 struct dwarf2_queue_item *next;
1399 };
1400
1401 /* The current queue. */
1402 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1403
1404 /* Loaded secondary compilation units are kept in memory until they
1405 have not been referenced for the processing of this many
1406 compilation units. Set this to zero to disable caching. Cache
1407 sizes of up to at least twenty will improve startup time for
1408 typical inter-CU-reference binaries, at an obvious memory cost. */
1409 static int dwarf_max_cache_age = 5;
1410 static void
1411 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1412 struct cmd_list_element *c, const char *value)
1413 {
1414 fprintf_filtered (file, _("The upper bound on the age of cached "
1415 "DWARF compilation units is %s.\n"),
1416 value);
1417 }
1418 \f
1419 /* local function prototypes */
1420
1421 static const char *get_section_name (const struct dwarf2_section_info *);
1422
1423 static const char *get_section_file_name (const struct dwarf2_section_info *);
1424
1425 static void dwarf2_locate_sections (bfd *, asection *, void *);
1426
1427 static void dwarf2_find_base_address (struct die_info *die,
1428 struct dwarf2_cu *cu);
1429
1430 static struct partial_symtab *create_partial_symtab
1431 (struct dwarf2_per_cu_data *per_cu, const char *name);
1432
1433 static void dwarf2_build_psymtabs_hard (struct objfile *);
1434
1435 static void scan_partial_symbols (struct partial_die_info *,
1436 CORE_ADDR *, CORE_ADDR *,
1437 int, struct dwarf2_cu *);
1438
1439 static void add_partial_symbol (struct partial_die_info *,
1440 struct dwarf2_cu *);
1441
1442 static void add_partial_namespace (struct partial_die_info *pdi,
1443 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1444 int set_addrmap, struct dwarf2_cu *cu);
1445
1446 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1447 CORE_ADDR *highpc, int set_addrmap,
1448 struct dwarf2_cu *cu);
1449
1450 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1451 struct dwarf2_cu *cu);
1452
1453 static void add_partial_subprogram (struct partial_die_info *pdi,
1454 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1455 int need_pc, struct dwarf2_cu *cu);
1456
1457 static void dwarf2_read_symtab (struct partial_symtab *,
1458 struct objfile *);
1459
1460 static void psymtab_to_symtab_1 (struct partial_symtab *);
1461
1462 static struct abbrev_info *abbrev_table_lookup_abbrev
1463 (const struct abbrev_table *, unsigned int);
1464
1465 static struct abbrev_table *abbrev_table_read_table
1466 (struct dwarf2_section_info *, sect_offset);
1467
1468 static void abbrev_table_free (struct abbrev_table *);
1469
1470 static void abbrev_table_free_cleanup (void *);
1471
1472 static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1473 struct dwarf2_section_info *);
1474
1475 static void dwarf2_free_abbrev_table (void *);
1476
1477 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1478
1479 static struct partial_die_info *load_partial_dies
1480 (const struct die_reader_specs *, const gdb_byte *, int);
1481
1482 static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1483 struct partial_die_info *,
1484 struct abbrev_info *,
1485 unsigned int,
1486 const gdb_byte *);
1487
1488 static struct partial_die_info *find_partial_die (sect_offset, int,
1489 struct dwarf2_cu *);
1490
1491 static void fixup_partial_die (struct partial_die_info *,
1492 struct dwarf2_cu *);
1493
1494 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1495 struct attribute *, struct attr_abbrev *,
1496 const gdb_byte *);
1497
1498 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1499
1500 static int read_1_signed_byte (bfd *, const gdb_byte *);
1501
1502 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1503
1504 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1505
1506 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1507
1508 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1509 unsigned int *);
1510
1511 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1512
1513 static LONGEST read_checked_initial_length_and_offset
1514 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1515 unsigned int *, unsigned int *);
1516
1517 static LONGEST read_offset (bfd *, const gdb_byte *,
1518 const struct comp_unit_head *,
1519 unsigned int *);
1520
1521 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1522
1523 static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1524 sect_offset);
1525
1526 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1527
1528 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1529
1530 static const char *read_indirect_string (bfd *, const gdb_byte *,
1531 const struct comp_unit_head *,
1532 unsigned int *);
1533
1534 static const char *read_indirect_line_string (bfd *, const gdb_byte *,
1535 const struct comp_unit_head *,
1536 unsigned int *);
1537
1538 static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1539
1540 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1541
1542 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1543 const gdb_byte *,
1544 unsigned int *);
1545
1546 static const char *read_str_index (const struct die_reader_specs *reader,
1547 ULONGEST str_index);
1548
1549 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1550
1551 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1552 struct dwarf2_cu *);
1553
1554 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1555 unsigned int);
1556
1557 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1558 struct dwarf2_cu *cu);
1559
1560 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1561 struct dwarf2_cu *cu);
1562
1563 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1564
1565 static struct die_info *die_specification (struct die_info *die,
1566 struct dwarf2_cu **);
1567
1568 static void free_line_header (struct line_header *lh);
1569
1570 static struct line_header *dwarf_decode_line_header (unsigned int offset,
1571 struct dwarf2_cu *cu);
1572
1573 static void dwarf_decode_lines (struct line_header *, const char *,
1574 struct dwarf2_cu *, struct partial_symtab *,
1575 CORE_ADDR, int decode_mapping);
1576
1577 static void dwarf2_start_subfile (const char *, const char *);
1578
1579 static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1580 const char *, const char *,
1581 CORE_ADDR);
1582
1583 static struct symbol *new_symbol (struct die_info *, struct type *,
1584 struct dwarf2_cu *);
1585
1586 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1587 struct dwarf2_cu *, struct symbol *);
1588
1589 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1590 struct dwarf2_cu *);
1591
1592 static void dwarf2_const_value_attr (const struct attribute *attr,
1593 struct type *type,
1594 const char *name,
1595 struct obstack *obstack,
1596 struct dwarf2_cu *cu, LONGEST *value,
1597 const gdb_byte **bytes,
1598 struct dwarf2_locexpr_baton **baton);
1599
1600 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1601
1602 static int need_gnat_info (struct dwarf2_cu *);
1603
1604 static struct type *die_descriptive_type (struct die_info *,
1605 struct dwarf2_cu *);
1606
1607 static void set_descriptive_type (struct type *, struct die_info *,
1608 struct dwarf2_cu *);
1609
1610 static struct type *die_containing_type (struct die_info *,
1611 struct dwarf2_cu *);
1612
1613 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1614 struct dwarf2_cu *);
1615
1616 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1617
1618 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1619
1620 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1621
1622 static char *typename_concat (struct obstack *obs, const char *prefix,
1623 const char *suffix, int physname,
1624 struct dwarf2_cu *cu);
1625
1626 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1627
1628 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1629
1630 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1631
1632 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1633
1634 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1635
1636 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1637 struct dwarf2_cu *, struct partial_symtab *);
1638
1639 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1640 values. Keep the items ordered with increasing constraints compliance. */
1641 enum pc_bounds_kind
1642 {
1643 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1644 PC_BOUNDS_NOT_PRESENT,
1645
1646 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1647 were present but they do not form a valid range of PC addresses. */
1648 PC_BOUNDS_INVALID,
1649
1650 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1651 PC_BOUNDS_RANGES,
1652
1653 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1654 PC_BOUNDS_HIGH_LOW,
1655 };
1656
1657 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1658 CORE_ADDR *, CORE_ADDR *,
1659 struct dwarf2_cu *,
1660 struct partial_symtab *);
1661
1662 static void get_scope_pc_bounds (struct die_info *,
1663 CORE_ADDR *, CORE_ADDR *,
1664 struct dwarf2_cu *);
1665
1666 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1667 CORE_ADDR, struct dwarf2_cu *);
1668
1669 static void dwarf2_add_field (struct field_info *, struct die_info *,
1670 struct dwarf2_cu *);
1671
1672 static void dwarf2_attach_fields_to_type (struct field_info *,
1673 struct type *, struct dwarf2_cu *);
1674
1675 static void dwarf2_add_member_fn (struct field_info *,
1676 struct die_info *, struct type *,
1677 struct dwarf2_cu *);
1678
1679 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1680 struct type *,
1681 struct dwarf2_cu *);
1682
1683 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1684
1685 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1686
1687 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1688
1689 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1690
1691 static struct using_direct **using_directives (enum language);
1692
1693 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1694
1695 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1696
1697 static struct type *read_module_type (struct die_info *die,
1698 struct dwarf2_cu *cu);
1699
1700 static const char *namespace_name (struct die_info *die,
1701 int *is_anonymous, struct dwarf2_cu *);
1702
1703 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1704
1705 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1706
1707 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1708 struct dwarf2_cu *);
1709
1710 static struct die_info *read_die_and_siblings_1
1711 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1712 struct die_info *);
1713
1714 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1715 const gdb_byte *info_ptr,
1716 const gdb_byte **new_info_ptr,
1717 struct die_info *parent);
1718
1719 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1720 struct die_info **, const gdb_byte *,
1721 int *, int);
1722
1723 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1724 struct die_info **, const gdb_byte *,
1725 int *);
1726
1727 static void process_die (struct die_info *, struct dwarf2_cu *);
1728
1729 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1730 struct obstack *);
1731
1732 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1733
1734 static const char *dwarf2_full_name (const char *name,
1735 struct die_info *die,
1736 struct dwarf2_cu *cu);
1737
1738 static const char *dwarf2_physname (const char *name, struct die_info *die,
1739 struct dwarf2_cu *cu);
1740
1741 static struct die_info *dwarf2_extension (struct die_info *die,
1742 struct dwarf2_cu **);
1743
1744 static const char *dwarf_tag_name (unsigned int);
1745
1746 static const char *dwarf_attr_name (unsigned int);
1747
1748 static const char *dwarf_form_name (unsigned int);
1749
1750 static char *dwarf_bool_name (unsigned int);
1751
1752 static const char *dwarf_type_encoding_name (unsigned int);
1753
1754 static struct die_info *sibling_die (struct die_info *);
1755
1756 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1757
1758 static void dump_die_for_error (struct die_info *);
1759
1760 static void dump_die_1 (struct ui_file *, int level, int max_level,
1761 struct die_info *);
1762
1763 /*static*/ void dump_die (struct die_info *, int max_level);
1764
1765 static void store_in_ref_table (struct die_info *,
1766 struct dwarf2_cu *);
1767
1768 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1769
1770 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1771
1772 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1773 const struct attribute *,
1774 struct dwarf2_cu **);
1775
1776 static struct die_info *follow_die_ref (struct die_info *,
1777 const struct attribute *,
1778 struct dwarf2_cu **);
1779
1780 static struct die_info *follow_die_sig (struct die_info *,
1781 const struct attribute *,
1782 struct dwarf2_cu **);
1783
1784 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1785 struct dwarf2_cu *);
1786
1787 static struct type *get_DW_AT_signature_type (struct die_info *,
1788 const struct attribute *,
1789 struct dwarf2_cu *);
1790
1791 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1792
1793 static void read_signatured_type (struct signatured_type *);
1794
1795 static int attr_to_dynamic_prop (const struct attribute *attr,
1796 struct die_info *die, struct dwarf2_cu *cu,
1797 struct dynamic_prop *prop);
1798
1799 /* memory allocation interface */
1800
1801 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1802
1803 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1804
1805 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1806
1807 static int attr_form_is_block (const struct attribute *);
1808
1809 static int attr_form_is_section_offset (const struct attribute *);
1810
1811 static int attr_form_is_constant (const struct attribute *);
1812
1813 static int attr_form_is_ref (const struct attribute *);
1814
1815 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1816 struct dwarf2_loclist_baton *baton,
1817 const struct attribute *attr);
1818
1819 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1820 struct symbol *sym,
1821 struct dwarf2_cu *cu,
1822 int is_block);
1823
1824 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1825 const gdb_byte *info_ptr,
1826 struct abbrev_info *abbrev);
1827
1828 static void free_stack_comp_unit (void *);
1829
1830 static hashval_t partial_die_hash (const void *item);
1831
1832 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1833
1834 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1835 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
1836
1837 static void init_one_comp_unit (struct dwarf2_cu *cu,
1838 struct dwarf2_per_cu_data *per_cu);
1839
1840 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1841 struct die_info *comp_unit_die,
1842 enum language pretend_language);
1843
1844 static void free_heap_comp_unit (void *);
1845
1846 static void free_cached_comp_units (void *);
1847
1848 static void age_cached_comp_units (void);
1849
1850 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1851
1852 static struct type *set_die_type (struct die_info *, struct type *,
1853 struct dwarf2_cu *);
1854
1855 static void create_all_comp_units (struct objfile *);
1856
1857 static int create_all_type_units (struct objfile *);
1858
1859 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1860 enum language);
1861
1862 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1863 enum language);
1864
1865 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1866 enum language);
1867
1868 static void dwarf2_add_dependence (struct dwarf2_cu *,
1869 struct dwarf2_per_cu_data *);
1870
1871 static void dwarf2_mark (struct dwarf2_cu *);
1872
1873 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1874
1875 static struct type *get_die_type_at_offset (sect_offset,
1876 struct dwarf2_per_cu_data *);
1877
1878 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1879
1880 static void dwarf2_release_queue (void *dummy);
1881
1882 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1883 enum language pretend_language);
1884
1885 static void process_queue (void);
1886
1887 static void find_file_and_directory (struct die_info *die,
1888 struct dwarf2_cu *cu,
1889 const char **name, const char **comp_dir);
1890
1891 static char *file_full_name (int file, struct line_header *lh,
1892 const char *comp_dir);
1893
1894 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
1895 enum class rcuh_kind { COMPILE, TYPE };
1896
1897 static const gdb_byte *read_and_check_comp_unit_head
1898 (struct comp_unit_head *header,
1899 struct dwarf2_section_info *section,
1900 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
1901 rcuh_kind section_kind);
1902
1903 static void init_cutu_and_read_dies
1904 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1905 int use_existing_cu, int keep,
1906 die_reader_func_ftype *die_reader_func, void *data);
1907
1908 static void init_cutu_and_read_dies_simple
1909 (struct dwarf2_per_cu_data *this_cu,
1910 die_reader_func_ftype *die_reader_func, void *data);
1911
1912 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1913
1914 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1915
1916 static struct dwo_unit *lookup_dwo_unit_in_dwp
1917 (struct dwp_file *dwp_file, const char *comp_dir,
1918 ULONGEST signature, int is_debug_types);
1919
1920 static struct dwp_file *get_dwp_file (void);
1921
1922 static struct dwo_unit *lookup_dwo_comp_unit
1923 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1924
1925 static struct dwo_unit *lookup_dwo_type_unit
1926 (struct signatured_type *, const char *, const char *);
1927
1928 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1929
1930 static void free_dwo_file_cleanup (void *);
1931
1932 static void process_cu_includes (void);
1933
1934 static void check_producer (struct dwarf2_cu *cu);
1935
1936 static void free_line_header_voidp (void *arg);
1937 \f
1938 /* Various complaints about symbol reading that don't abort the process. */
1939
1940 static void
1941 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1942 {
1943 complaint (&symfile_complaints,
1944 _("statement list doesn't fit in .debug_line section"));
1945 }
1946
1947 static void
1948 dwarf2_debug_line_missing_file_complaint (void)
1949 {
1950 complaint (&symfile_complaints,
1951 _(".debug_line section has line data without a file"));
1952 }
1953
1954 static void
1955 dwarf2_debug_line_missing_end_sequence_complaint (void)
1956 {
1957 complaint (&symfile_complaints,
1958 _(".debug_line section has line "
1959 "program sequence without an end"));
1960 }
1961
1962 static void
1963 dwarf2_complex_location_expr_complaint (void)
1964 {
1965 complaint (&symfile_complaints, _("location expression too complex"));
1966 }
1967
1968 static void
1969 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1970 int arg3)
1971 {
1972 complaint (&symfile_complaints,
1973 _("const value length mismatch for '%s', got %d, expected %d"),
1974 arg1, arg2, arg3);
1975 }
1976
1977 static void
1978 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1979 {
1980 complaint (&symfile_complaints,
1981 _("debug info runs off end of %s section"
1982 " [in module %s]"),
1983 get_section_name (section),
1984 get_section_file_name (section));
1985 }
1986
1987 static void
1988 dwarf2_macro_malformed_definition_complaint (const char *arg1)
1989 {
1990 complaint (&symfile_complaints,
1991 _("macro debug info contains a "
1992 "malformed macro definition:\n`%s'"),
1993 arg1);
1994 }
1995
1996 static void
1997 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1998 {
1999 complaint (&symfile_complaints,
2000 _("invalid attribute class or form for '%s' in '%s'"),
2001 arg1, arg2);
2002 }
2003
2004 /* Hash function for line_header_hash. */
2005
2006 static hashval_t
2007 line_header_hash (const struct line_header *ofs)
2008 {
2009 return ofs->offset.sect_off ^ ofs->offset_in_dwz;
2010 }
2011
2012 /* Hash function for htab_create_alloc_ex for line_header_hash. */
2013
2014 static hashval_t
2015 line_header_hash_voidp (const void *item)
2016 {
2017 const struct line_header *ofs = (const struct line_header *) item;
2018
2019 return line_header_hash (ofs);
2020 }
2021
2022 /* Equality function for line_header_hash. */
2023
2024 static int
2025 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2026 {
2027 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2028 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
2029
2030 return (ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off
2031 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2032 }
2033
2034 \f
2035 #if WORDS_BIGENDIAN
2036
2037 /* Convert VALUE between big- and little-endian. */
2038 static offset_type
2039 byte_swap (offset_type value)
2040 {
2041 offset_type result;
2042
2043 result = (value & 0xff) << 24;
2044 result |= (value & 0xff00) << 8;
2045 result |= (value & 0xff0000) >> 8;
2046 result |= (value & 0xff000000) >> 24;
2047 return result;
2048 }
2049
2050 #define MAYBE_SWAP(V) byte_swap (V)
2051
2052 #else
2053 #define MAYBE_SWAP(V) (V)
2054 #endif /* WORDS_BIGENDIAN */
2055
2056 /* Read the given attribute value as an address, taking the attribute's
2057 form into account. */
2058
2059 static CORE_ADDR
2060 attr_value_as_address (struct attribute *attr)
2061 {
2062 CORE_ADDR addr;
2063
2064 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2065 {
2066 /* Aside from a few clearly defined exceptions, attributes that
2067 contain an address must always be in DW_FORM_addr form.
2068 Unfortunately, some compilers happen to be violating this
2069 requirement by encoding addresses using other forms, such
2070 as DW_FORM_data4 for example. For those broken compilers,
2071 we try to do our best, without any guarantee of success,
2072 to interpret the address correctly. It would also be nice
2073 to generate a complaint, but that would require us to maintain
2074 a list of legitimate cases where a non-address form is allowed,
2075 as well as update callers to pass in at least the CU's DWARF
2076 version. This is more overhead than what we're willing to
2077 expand for a pretty rare case. */
2078 addr = DW_UNSND (attr);
2079 }
2080 else
2081 addr = DW_ADDR (attr);
2082
2083 return addr;
2084 }
2085
2086 /* The suffix for an index file. */
2087 #define INDEX_SUFFIX ".gdb-index"
2088
2089 /* Try to locate the sections we need for DWARF 2 debugging
2090 information and return true if we have enough to do something.
2091 NAMES points to the dwarf2 section names, or is NULL if the standard
2092 ELF names are used. */
2093
2094 int
2095 dwarf2_has_info (struct objfile *objfile,
2096 const struct dwarf2_debug_sections *names)
2097 {
2098 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
2099 objfile_data (objfile, dwarf2_objfile_data_key));
2100 if (!dwarf2_per_objfile)
2101 {
2102 /* Initialize per-objfile state. */
2103 struct dwarf2_per_objfile *data
2104 = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_objfile);
2105
2106 memset (data, 0, sizeof (*data));
2107 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
2108 dwarf2_per_objfile = data;
2109
2110 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
2111 (void *) names);
2112 dwarf2_per_objfile->objfile = objfile;
2113 }
2114 return (!dwarf2_per_objfile->info.is_virtual
2115 && dwarf2_per_objfile->info.s.section != NULL
2116 && !dwarf2_per_objfile->abbrev.is_virtual
2117 && dwarf2_per_objfile->abbrev.s.section != NULL);
2118 }
2119
2120 /* Return the containing section of virtual section SECTION. */
2121
2122 static struct dwarf2_section_info *
2123 get_containing_section (const struct dwarf2_section_info *section)
2124 {
2125 gdb_assert (section->is_virtual);
2126 return section->s.containing_section;
2127 }
2128
2129 /* Return the bfd owner of SECTION. */
2130
2131 static struct bfd *
2132 get_section_bfd_owner (const struct dwarf2_section_info *section)
2133 {
2134 if (section->is_virtual)
2135 {
2136 section = get_containing_section (section);
2137 gdb_assert (!section->is_virtual);
2138 }
2139 return section->s.section->owner;
2140 }
2141
2142 /* Return the bfd section of SECTION.
2143 Returns NULL if the section is not present. */
2144
2145 static asection *
2146 get_section_bfd_section (const struct dwarf2_section_info *section)
2147 {
2148 if (section->is_virtual)
2149 {
2150 section = get_containing_section (section);
2151 gdb_assert (!section->is_virtual);
2152 }
2153 return section->s.section;
2154 }
2155
2156 /* Return the name of SECTION. */
2157
2158 static const char *
2159 get_section_name (const struct dwarf2_section_info *section)
2160 {
2161 asection *sectp = get_section_bfd_section (section);
2162
2163 gdb_assert (sectp != NULL);
2164 return bfd_section_name (get_section_bfd_owner (section), sectp);
2165 }
2166
2167 /* Return the name of the file SECTION is in. */
2168
2169 static const char *
2170 get_section_file_name (const struct dwarf2_section_info *section)
2171 {
2172 bfd *abfd = get_section_bfd_owner (section);
2173
2174 return bfd_get_filename (abfd);
2175 }
2176
2177 /* Return the id of SECTION.
2178 Returns 0 if SECTION doesn't exist. */
2179
2180 static int
2181 get_section_id (const struct dwarf2_section_info *section)
2182 {
2183 asection *sectp = get_section_bfd_section (section);
2184
2185 if (sectp == NULL)
2186 return 0;
2187 return sectp->id;
2188 }
2189
2190 /* Return the flags of SECTION.
2191 SECTION (or containing section if this is a virtual section) must exist. */
2192
2193 static int
2194 get_section_flags (const struct dwarf2_section_info *section)
2195 {
2196 asection *sectp = get_section_bfd_section (section);
2197
2198 gdb_assert (sectp != NULL);
2199 return bfd_get_section_flags (sectp->owner, sectp);
2200 }
2201
2202 /* When loading sections, we look either for uncompressed section or for
2203 compressed section names. */
2204
2205 static int
2206 section_is_p (const char *section_name,
2207 const struct dwarf2_section_names *names)
2208 {
2209 if (names->normal != NULL
2210 && strcmp (section_name, names->normal) == 0)
2211 return 1;
2212 if (names->compressed != NULL
2213 && strcmp (section_name, names->compressed) == 0)
2214 return 1;
2215 return 0;
2216 }
2217
2218 /* This function is mapped across the sections and remembers the
2219 offset and size of each of the debugging sections we are interested
2220 in. */
2221
2222 static void
2223 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
2224 {
2225 const struct dwarf2_debug_sections *names;
2226 flagword aflag = bfd_get_section_flags (abfd, sectp);
2227
2228 if (vnames == NULL)
2229 names = &dwarf2_elf_names;
2230 else
2231 names = (const struct dwarf2_debug_sections *) vnames;
2232
2233 if ((aflag & SEC_HAS_CONTENTS) == 0)
2234 {
2235 }
2236 else if (section_is_p (sectp->name, &names->info))
2237 {
2238 dwarf2_per_objfile->info.s.section = sectp;
2239 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
2240 }
2241 else if (section_is_p (sectp->name, &names->abbrev))
2242 {
2243 dwarf2_per_objfile->abbrev.s.section = sectp;
2244 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
2245 }
2246 else if (section_is_p (sectp->name, &names->line))
2247 {
2248 dwarf2_per_objfile->line.s.section = sectp;
2249 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
2250 }
2251 else if (section_is_p (sectp->name, &names->loc))
2252 {
2253 dwarf2_per_objfile->loc.s.section = sectp;
2254 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
2255 }
2256 else if (section_is_p (sectp->name, &names->loclists))
2257 {
2258 dwarf2_per_objfile->loclists.s.section = sectp;
2259 dwarf2_per_objfile->loclists.size = bfd_get_section_size (sectp);
2260 }
2261 else if (section_is_p (sectp->name, &names->macinfo))
2262 {
2263 dwarf2_per_objfile->macinfo.s.section = sectp;
2264 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
2265 }
2266 else if (section_is_p (sectp->name, &names->macro))
2267 {
2268 dwarf2_per_objfile->macro.s.section = sectp;
2269 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
2270 }
2271 else if (section_is_p (sectp->name, &names->str))
2272 {
2273 dwarf2_per_objfile->str.s.section = sectp;
2274 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
2275 }
2276 else if (section_is_p (sectp->name, &names->line_str))
2277 {
2278 dwarf2_per_objfile->line_str.s.section = sectp;
2279 dwarf2_per_objfile->line_str.size = bfd_get_section_size (sectp);
2280 }
2281 else if (section_is_p (sectp->name, &names->addr))
2282 {
2283 dwarf2_per_objfile->addr.s.section = sectp;
2284 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
2285 }
2286 else if (section_is_p (sectp->name, &names->frame))
2287 {
2288 dwarf2_per_objfile->frame.s.section = sectp;
2289 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
2290 }
2291 else if (section_is_p (sectp->name, &names->eh_frame))
2292 {
2293 dwarf2_per_objfile->eh_frame.s.section = sectp;
2294 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
2295 }
2296 else if (section_is_p (sectp->name, &names->ranges))
2297 {
2298 dwarf2_per_objfile->ranges.s.section = sectp;
2299 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
2300 }
2301 else if (section_is_p (sectp->name, &names->rnglists))
2302 {
2303 dwarf2_per_objfile->rnglists.s.section = sectp;
2304 dwarf2_per_objfile->rnglists.size = bfd_get_section_size (sectp);
2305 }
2306 else if (section_is_p (sectp->name, &names->types))
2307 {
2308 struct dwarf2_section_info type_section;
2309
2310 memset (&type_section, 0, sizeof (type_section));
2311 type_section.s.section = sectp;
2312 type_section.size = bfd_get_section_size (sectp);
2313
2314 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
2315 &type_section);
2316 }
2317 else if (section_is_p (sectp->name, &names->gdb_index))
2318 {
2319 dwarf2_per_objfile->gdb_index.s.section = sectp;
2320 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
2321 }
2322
2323 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
2324 && bfd_section_vma (abfd, sectp) == 0)
2325 dwarf2_per_objfile->has_section_at_zero = 1;
2326 }
2327
2328 /* A helper function that decides whether a section is empty,
2329 or not present. */
2330
2331 static int
2332 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2333 {
2334 if (section->is_virtual)
2335 return section->size == 0;
2336 return section->s.section == NULL || section->size == 0;
2337 }
2338
2339 /* Read the contents of the section INFO.
2340 OBJFILE is the main object file, but not necessarily the file where
2341 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2342 of the DWO file.
2343 If the section is compressed, uncompress it before returning. */
2344
2345 static void
2346 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
2347 {
2348 asection *sectp;
2349 bfd *abfd;
2350 gdb_byte *buf, *retbuf;
2351
2352 if (info->readin)
2353 return;
2354 info->buffer = NULL;
2355 info->readin = 1;
2356
2357 if (dwarf2_section_empty_p (info))
2358 return;
2359
2360 sectp = get_section_bfd_section (info);
2361
2362 /* If this is a virtual section we need to read in the real one first. */
2363 if (info->is_virtual)
2364 {
2365 struct dwarf2_section_info *containing_section =
2366 get_containing_section (info);
2367
2368 gdb_assert (sectp != NULL);
2369 if ((sectp->flags & SEC_RELOC) != 0)
2370 {
2371 error (_("Dwarf Error: DWP format V2 with relocations is not"
2372 " supported in section %s [in module %s]"),
2373 get_section_name (info), get_section_file_name (info));
2374 }
2375 dwarf2_read_section (objfile, containing_section);
2376 /* Other code should have already caught virtual sections that don't
2377 fit. */
2378 gdb_assert (info->virtual_offset + info->size
2379 <= containing_section->size);
2380 /* If the real section is empty or there was a problem reading the
2381 section we shouldn't get here. */
2382 gdb_assert (containing_section->buffer != NULL);
2383 info->buffer = containing_section->buffer + info->virtual_offset;
2384 return;
2385 }
2386
2387 /* If the section has relocations, we must read it ourselves.
2388 Otherwise we attach it to the BFD. */
2389 if ((sectp->flags & SEC_RELOC) == 0)
2390 {
2391 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2392 return;
2393 }
2394
2395 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
2396 info->buffer = buf;
2397
2398 /* When debugging .o files, we may need to apply relocations; see
2399 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2400 We never compress sections in .o files, so we only need to
2401 try this when the section is not compressed. */
2402 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2403 if (retbuf != NULL)
2404 {
2405 info->buffer = retbuf;
2406 return;
2407 }
2408
2409 abfd = get_section_bfd_owner (info);
2410 gdb_assert (abfd != NULL);
2411
2412 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2413 || bfd_bread (buf, info->size, abfd) != info->size)
2414 {
2415 error (_("Dwarf Error: Can't read DWARF data"
2416 " in section %s [in module %s]"),
2417 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2418 }
2419 }
2420
2421 /* A helper function that returns the size of a section in a safe way.
2422 If you are positive that the section has been read before using the
2423 size, then it is safe to refer to the dwarf2_section_info object's
2424 "size" field directly. In other cases, you must call this
2425 function, because for compressed sections the size field is not set
2426 correctly until the section has been read. */
2427
2428 static bfd_size_type
2429 dwarf2_section_size (struct objfile *objfile,
2430 struct dwarf2_section_info *info)
2431 {
2432 if (!info->readin)
2433 dwarf2_read_section (objfile, info);
2434 return info->size;
2435 }
2436
2437 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2438 SECTION_NAME. */
2439
2440 void
2441 dwarf2_get_section_info (struct objfile *objfile,
2442 enum dwarf2_section_enum sect,
2443 asection **sectp, const gdb_byte **bufp,
2444 bfd_size_type *sizep)
2445 {
2446 struct dwarf2_per_objfile *data
2447 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2448 dwarf2_objfile_data_key);
2449 struct dwarf2_section_info *info;
2450
2451 /* We may see an objfile without any DWARF, in which case we just
2452 return nothing. */
2453 if (data == NULL)
2454 {
2455 *sectp = NULL;
2456 *bufp = NULL;
2457 *sizep = 0;
2458 return;
2459 }
2460 switch (sect)
2461 {
2462 case DWARF2_DEBUG_FRAME:
2463 info = &data->frame;
2464 break;
2465 case DWARF2_EH_FRAME:
2466 info = &data->eh_frame;
2467 break;
2468 default:
2469 gdb_assert_not_reached ("unexpected section");
2470 }
2471
2472 dwarf2_read_section (objfile, info);
2473
2474 *sectp = get_section_bfd_section (info);
2475 *bufp = info->buffer;
2476 *sizep = info->size;
2477 }
2478
2479 /* A helper function to find the sections for a .dwz file. */
2480
2481 static void
2482 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2483 {
2484 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2485
2486 /* Note that we only support the standard ELF names, because .dwz
2487 is ELF-only (at the time of writing). */
2488 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2489 {
2490 dwz_file->abbrev.s.section = sectp;
2491 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2492 }
2493 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2494 {
2495 dwz_file->info.s.section = sectp;
2496 dwz_file->info.size = bfd_get_section_size (sectp);
2497 }
2498 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2499 {
2500 dwz_file->str.s.section = sectp;
2501 dwz_file->str.size = bfd_get_section_size (sectp);
2502 }
2503 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2504 {
2505 dwz_file->line.s.section = sectp;
2506 dwz_file->line.size = bfd_get_section_size (sectp);
2507 }
2508 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2509 {
2510 dwz_file->macro.s.section = sectp;
2511 dwz_file->macro.size = bfd_get_section_size (sectp);
2512 }
2513 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2514 {
2515 dwz_file->gdb_index.s.section = sectp;
2516 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2517 }
2518 }
2519
2520 /* Open the separate '.dwz' debug file, if needed. Return NULL if
2521 there is no .gnu_debugaltlink section in the file. Error if there
2522 is such a section but the file cannot be found. */
2523
2524 static struct dwz_file *
2525 dwarf2_get_dwz_file (void)
2526 {
2527 char *data;
2528 struct cleanup *cleanup;
2529 const char *filename;
2530 struct dwz_file *result;
2531 bfd_size_type buildid_len_arg;
2532 size_t buildid_len;
2533 bfd_byte *buildid;
2534
2535 if (dwarf2_per_objfile->dwz_file != NULL)
2536 return dwarf2_per_objfile->dwz_file;
2537
2538 bfd_set_error (bfd_error_no_error);
2539 data = bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2540 &buildid_len_arg, &buildid);
2541 if (data == NULL)
2542 {
2543 if (bfd_get_error () == bfd_error_no_error)
2544 return NULL;
2545 error (_("could not read '.gnu_debugaltlink' section: %s"),
2546 bfd_errmsg (bfd_get_error ()));
2547 }
2548 cleanup = make_cleanup (xfree, data);
2549 make_cleanup (xfree, buildid);
2550
2551 buildid_len = (size_t) buildid_len_arg;
2552
2553 filename = (const char *) data;
2554 if (!IS_ABSOLUTE_PATH (filename))
2555 {
2556 char *abs = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2557 char *rel;
2558
2559 make_cleanup (xfree, abs);
2560 abs = ldirname (abs);
2561 make_cleanup (xfree, abs);
2562
2563 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2564 make_cleanup (xfree, rel);
2565 filename = rel;
2566 }
2567
2568 /* First try the file name given in the section. If that doesn't
2569 work, try to use the build-id instead. */
2570 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2571 if (dwz_bfd != NULL)
2572 {
2573 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2574 dwz_bfd.release ();
2575 }
2576
2577 if (dwz_bfd == NULL)
2578 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2579
2580 if (dwz_bfd == NULL)
2581 error (_("could not find '.gnu_debugaltlink' file for %s"),
2582 objfile_name (dwarf2_per_objfile->objfile));
2583
2584 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2585 struct dwz_file);
2586 result->dwz_bfd = dwz_bfd.release ();
2587
2588 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
2589
2590 do_cleanups (cleanup);
2591
2592 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
2593 dwarf2_per_objfile->dwz_file = result;
2594 return result;
2595 }
2596 \f
2597 /* DWARF quick_symbols_functions support. */
2598
2599 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2600 unique line tables, so we maintain a separate table of all .debug_line
2601 derived entries to support the sharing.
2602 All the quick functions need is the list of file names. We discard the
2603 line_header when we're done and don't need to record it here. */
2604 struct quick_file_names
2605 {
2606 /* The data used to construct the hash key. */
2607 struct stmt_list_hash hash;
2608
2609 /* The number of entries in file_names, real_names. */
2610 unsigned int num_file_names;
2611
2612 /* The file names from the line table, after being run through
2613 file_full_name. */
2614 const char **file_names;
2615
2616 /* The file names from the line table after being run through
2617 gdb_realpath. These are computed lazily. */
2618 const char **real_names;
2619 };
2620
2621 /* When using the index (and thus not using psymtabs), each CU has an
2622 object of this type. This is used to hold information needed by
2623 the various "quick" methods. */
2624 struct dwarf2_per_cu_quick_data
2625 {
2626 /* The file table. This can be NULL if there was no file table
2627 or it's currently not read in.
2628 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2629 struct quick_file_names *file_names;
2630
2631 /* The corresponding symbol table. This is NULL if symbols for this
2632 CU have not yet been read. */
2633 struct compunit_symtab *compunit_symtab;
2634
2635 /* A temporary mark bit used when iterating over all CUs in
2636 expand_symtabs_matching. */
2637 unsigned int mark : 1;
2638
2639 /* True if we've tried to read the file table and found there isn't one.
2640 There will be no point in trying to read it again next time. */
2641 unsigned int no_file_data : 1;
2642 };
2643
2644 /* Utility hash function for a stmt_list_hash. */
2645
2646 static hashval_t
2647 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2648 {
2649 hashval_t v = 0;
2650
2651 if (stmt_list_hash->dwo_unit != NULL)
2652 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2653 v += stmt_list_hash->line_offset.sect_off;
2654 return v;
2655 }
2656
2657 /* Utility equality function for a stmt_list_hash. */
2658
2659 static int
2660 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2661 const struct stmt_list_hash *rhs)
2662 {
2663 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2664 return 0;
2665 if (lhs->dwo_unit != NULL
2666 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2667 return 0;
2668
2669 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2670 }
2671
2672 /* Hash function for a quick_file_names. */
2673
2674 static hashval_t
2675 hash_file_name_entry (const void *e)
2676 {
2677 const struct quick_file_names *file_data
2678 = (const struct quick_file_names *) e;
2679
2680 return hash_stmt_list_entry (&file_data->hash);
2681 }
2682
2683 /* Equality function for a quick_file_names. */
2684
2685 static int
2686 eq_file_name_entry (const void *a, const void *b)
2687 {
2688 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2689 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2690
2691 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2692 }
2693
2694 /* Delete function for a quick_file_names. */
2695
2696 static void
2697 delete_file_name_entry (void *e)
2698 {
2699 struct quick_file_names *file_data = (struct quick_file_names *) e;
2700 int i;
2701
2702 for (i = 0; i < file_data->num_file_names; ++i)
2703 {
2704 xfree ((void*) file_data->file_names[i]);
2705 if (file_data->real_names)
2706 xfree ((void*) file_data->real_names[i]);
2707 }
2708
2709 /* The space for the struct itself lives on objfile_obstack,
2710 so we don't free it here. */
2711 }
2712
2713 /* Create a quick_file_names hash table. */
2714
2715 static htab_t
2716 create_quick_file_names_table (unsigned int nr_initial_entries)
2717 {
2718 return htab_create_alloc (nr_initial_entries,
2719 hash_file_name_entry, eq_file_name_entry,
2720 delete_file_name_entry, xcalloc, xfree);
2721 }
2722
2723 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2724 have to be created afterwards. You should call age_cached_comp_units after
2725 processing PER_CU->CU. dw2_setup must have been already called. */
2726
2727 static void
2728 load_cu (struct dwarf2_per_cu_data *per_cu)
2729 {
2730 if (per_cu->is_debug_types)
2731 load_full_type_unit (per_cu);
2732 else
2733 load_full_comp_unit (per_cu, language_minimal);
2734
2735 if (per_cu->cu == NULL)
2736 return; /* Dummy CU. */
2737
2738 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2739 }
2740
2741 /* Read in the symbols for PER_CU. */
2742
2743 static void
2744 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2745 {
2746 struct cleanup *back_to;
2747
2748 /* Skip type_unit_groups, reading the type units they contain
2749 is handled elsewhere. */
2750 if (IS_TYPE_UNIT_GROUP (per_cu))
2751 return;
2752
2753 back_to = make_cleanup (dwarf2_release_queue, NULL);
2754
2755 if (dwarf2_per_objfile->using_index
2756 ? per_cu->v.quick->compunit_symtab == NULL
2757 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2758 {
2759 queue_comp_unit (per_cu, language_minimal);
2760 load_cu (per_cu);
2761
2762 /* If we just loaded a CU from a DWO, and we're working with an index
2763 that may badly handle TUs, load all the TUs in that DWO as well.
2764 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2765 if (!per_cu->is_debug_types
2766 && per_cu->cu != NULL
2767 && per_cu->cu->dwo_unit != NULL
2768 && dwarf2_per_objfile->index_table != NULL
2769 && dwarf2_per_objfile->index_table->version <= 7
2770 /* DWP files aren't supported yet. */
2771 && get_dwp_file () == NULL)
2772 queue_and_load_all_dwo_tus (per_cu);
2773 }
2774
2775 process_queue ();
2776
2777 /* Age the cache, releasing compilation units that have not
2778 been used recently. */
2779 age_cached_comp_units ();
2780
2781 do_cleanups (back_to);
2782 }
2783
2784 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2785 the objfile from which this CU came. Returns the resulting symbol
2786 table. */
2787
2788 static struct compunit_symtab *
2789 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2790 {
2791 gdb_assert (dwarf2_per_objfile->using_index);
2792 if (!per_cu->v.quick->compunit_symtab)
2793 {
2794 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2795 increment_reading_symtab ();
2796 dw2_do_instantiate_symtab (per_cu);
2797 process_cu_includes ();
2798 do_cleanups (back_to);
2799 }
2800
2801 return per_cu->v.quick->compunit_symtab;
2802 }
2803
2804 /* Return the CU/TU given its index.
2805
2806 This is intended for loops like:
2807
2808 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2809 + dwarf2_per_objfile->n_type_units); ++i)
2810 {
2811 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
2812
2813 ...;
2814 }
2815 */
2816
2817 static struct dwarf2_per_cu_data *
2818 dw2_get_cutu (int index)
2819 {
2820 if (index >= dwarf2_per_objfile->n_comp_units)
2821 {
2822 index -= dwarf2_per_objfile->n_comp_units;
2823 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2824 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
2825 }
2826
2827 return dwarf2_per_objfile->all_comp_units[index];
2828 }
2829
2830 /* Return the CU given its index.
2831 This differs from dw2_get_cutu in that it's for when you know INDEX
2832 refers to a CU. */
2833
2834 static struct dwarf2_per_cu_data *
2835 dw2_get_cu (int index)
2836 {
2837 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
2838
2839 return dwarf2_per_objfile->all_comp_units[index];
2840 }
2841
2842 /* A helper for create_cus_from_index that handles a given list of
2843 CUs. */
2844
2845 static void
2846 create_cus_from_index_list (struct objfile *objfile,
2847 const gdb_byte *cu_list, offset_type n_elements,
2848 struct dwarf2_section_info *section,
2849 int is_dwz,
2850 int base_offset)
2851 {
2852 offset_type i;
2853
2854 for (i = 0; i < n_elements; i += 2)
2855 {
2856 struct dwarf2_per_cu_data *the_cu;
2857 ULONGEST offset, length;
2858
2859 gdb_static_assert (sizeof (ULONGEST) >= 8);
2860 offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2861 length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2862 cu_list += 2 * 8;
2863
2864 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2865 struct dwarf2_per_cu_data);
2866 the_cu->offset.sect_off = offset;
2867 the_cu->length = length;
2868 the_cu->objfile = objfile;
2869 the_cu->section = section;
2870 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2871 struct dwarf2_per_cu_quick_data);
2872 the_cu->is_dwz = is_dwz;
2873 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
2874 }
2875 }
2876
2877 /* Read the CU list from the mapped index, and use it to create all
2878 the CU objects for this objfile. */
2879
2880 static void
2881 create_cus_from_index (struct objfile *objfile,
2882 const gdb_byte *cu_list, offset_type cu_list_elements,
2883 const gdb_byte *dwz_list, offset_type dwz_elements)
2884 {
2885 struct dwz_file *dwz;
2886
2887 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2888 dwarf2_per_objfile->all_comp_units =
2889 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
2890 dwarf2_per_objfile->n_comp_units);
2891
2892 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2893 &dwarf2_per_objfile->info, 0, 0);
2894
2895 if (dwz_elements == 0)
2896 return;
2897
2898 dwz = dwarf2_get_dwz_file ();
2899 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2900 cu_list_elements / 2);
2901 }
2902
2903 /* Create the signatured type hash table from the index. */
2904
2905 static void
2906 create_signatured_type_table_from_index (struct objfile *objfile,
2907 struct dwarf2_section_info *section,
2908 const gdb_byte *bytes,
2909 offset_type elements)
2910 {
2911 offset_type i;
2912 htab_t sig_types_hash;
2913
2914 dwarf2_per_objfile->n_type_units
2915 = dwarf2_per_objfile->n_allocated_type_units
2916 = elements / 3;
2917 dwarf2_per_objfile->all_type_units =
2918 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
2919
2920 sig_types_hash = allocate_signatured_type_table (objfile);
2921
2922 for (i = 0; i < elements; i += 3)
2923 {
2924 struct signatured_type *sig_type;
2925 ULONGEST offset, type_offset_in_tu, signature;
2926 void **slot;
2927
2928 gdb_static_assert (sizeof (ULONGEST) >= 8);
2929 offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2930 type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
2931 BFD_ENDIAN_LITTLE);
2932 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2933 bytes += 3 * 8;
2934
2935 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2936 struct signatured_type);
2937 sig_type->signature = signature;
2938 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2939 sig_type->per_cu.is_debug_types = 1;
2940 sig_type->per_cu.section = section;
2941 sig_type->per_cu.offset.sect_off = offset;
2942 sig_type->per_cu.objfile = objfile;
2943 sig_type->per_cu.v.quick
2944 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2945 struct dwarf2_per_cu_quick_data);
2946
2947 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2948 *slot = sig_type;
2949
2950 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
2951 }
2952
2953 dwarf2_per_objfile->signatured_types = sig_types_hash;
2954 }
2955
2956 /* Read the address map data from the mapped index, and use it to
2957 populate the objfile's psymtabs_addrmap. */
2958
2959 static void
2960 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2961 {
2962 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2963 const gdb_byte *iter, *end;
2964 struct obstack temp_obstack;
2965 struct addrmap *mutable_map;
2966 struct cleanup *cleanup;
2967 CORE_ADDR baseaddr;
2968
2969 obstack_init (&temp_obstack);
2970 cleanup = make_cleanup_obstack_free (&temp_obstack);
2971 mutable_map = addrmap_create_mutable (&temp_obstack);
2972
2973 iter = index->address_table;
2974 end = iter + index->address_table_size;
2975
2976 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2977
2978 while (iter < end)
2979 {
2980 ULONGEST hi, lo, cu_index;
2981 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2982 iter += 8;
2983 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2984 iter += 8;
2985 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2986 iter += 4;
2987
2988 if (lo > hi)
2989 {
2990 complaint (&symfile_complaints,
2991 _(".gdb_index address table has invalid range (%s - %s)"),
2992 hex_string (lo), hex_string (hi));
2993 continue;
2994 }
2995
2996 if (cu_index >= dwarf2_per_objfile->n_comp_units)
2997 {
2998 complaint (&symfile_complaints,
2999 _(".gdb_index address table has invalid CU number %u"),
3000 (unsigned) cu_index);
3001 continue;
3002 }
3003
3004 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3005 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
3006 addrmap_set_empty (mutable_map, lo, hi - 1, dw2_get_cutu (cu_index));
3007 }
3008
3009 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3010 &objfile->objfile_obstack);
3011 do_cleanups (cleanup);
3012 }
3013
3014 /* The hash function for strings in the mapped index. This is the same as
3015 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
3016 implementation. This is necessary because the hash function is tied to the
3017 format of the mapped index file. The hash values do not have to match with
3018 SYMBOL_HASH_NEXT.
3019
3020 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
3021
3022 static hashval_t
3023 mapped_index_string_hash (int index_version, const void *p)
3024 {
3025 const unsigned char *str = (const unsigned char *) p;
3026 hashval_t r = 0;
3027 unsigned char c;
3028
3029 while ((c = *str++) != 0)
3030 {
3031 if (index_version >= 5)
3032 c = tolower (c);
3033 r = r * 67 + c - 113;
3034 }
3035
3036 return r;
3037 }
3038
3039 /* Find a slot in the mapped index INDEX for the object named NAME.
3040 If NAME is found, set *VEC_OUT to point to the CU vector in the
3041 constant pool and return 1. If NAME cannot be found, return 0. */
3042
3043 static int
3044 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3045 offset_type **vec_out)
3046 {
3047 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
3048 offset_type hash;
3049 offset_type slot, step;
3050 int (*cmp) (const char *, const char *);
3051
3052 if (current_language->la_language == language_cplus
3053 || current_language->la_language == language_fortran
3054 || current_language->la_language == language_d)
3055 {
3056 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3057 not contain any. */
3058
3059 if (strchr (name, '(') != NULL)
3060 {
3061 char *without_params = cp_remove_params (name);
3062
3063 if (without_params != NULL)
3064 {
3065 make_cleanup (xfree, without_params);
3066 name = without_params;
3067 }
3068 }
3069 }
3070
3071 /* Index version 4 did not support case insensitive searches. But the
3072 indices for case insensitive languages are built in lowercase, therefore
3073 simulate our NAME being searched is also lowercased. */
3074 hash = mapped_index_string_hash ((index->version == 4
3075 && case_sensitivity == case_sensitive_off
3076 ? 5 : index->version),
3077 name);
3078
3079 slot = hash & (index->symbol_table_slots - 1);
3080 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
3081 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3082
3083 for (;;)
3084 {
3085 /* Convert a slot number to an offset into the table. */
3086 offset_type i = 2 * slot;
3087 const char *str;
3088 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
3089 {
3090 do_cleanups (back_to);
3091 return 0;
3092 }
3093
3094 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
3095 if (!cmp (name, str))
3096 {
3097 *vec_out = (offset_type *) (index->constant_pool
3098 + MAYBE_SWAP (index->symbol_table[i + 1]));
3099 do_cleanups (back_to);
3100 return 1;
3101 }
3102
3103 slot = (slot + step) & (index->symbol_table_slots - 1);
3104 }
3105 }
3106
3107 /* A helper function that reads the .gdb_index from SECTION and fills
3108 in MAP. FILENAME is the name of the file containing the section;
3109 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3110 ok to use deprecated sections.
3111
3112 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3113 out parameters that are filled in with information about the CU and
3114 TU lists in the section.
3115
3116 Returns 1 if all went well, 0 otherwise. */
3117
3118 static int
3119 read_index_from_section (struct objfile *objfile,
3120 const char *filename,
3121 int deprecated_ok,
3122 struct dwarf2_section_info *section,
3123 struct mapped_index *map,
3124 const gdb_byte **cu_list,
3125 offset_type *cu_list_elements,
3126 const gdb_byte **types_list,
3127 offset_type *types_list_elements)
3128 {
3129 const gdb_byte *addr;
3130 offset_type version;
3131 offset_type *metadata;
3132 int i;
3133
3134 if (dwarf2_section_empty_p (section))
3135 return 0;
3136
3137 /* Older elfutils strip versions could keep the section in the main
3138 executable while splitting it for the separate debug info file. */
3139 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
3140 return 0;
3141
3142 dwarf2_read_section (objfile, section);
3143
3144 addr = section->buffer;
3145 /* Version check. */
3146 version = MAYBE_SWAP (*(offset_type *) addr);
3147 /* Versions earlier than 3 emitted every copy of a psymbol. This
3148 causes the index to behave very poorly for certain requests. Version 3
3149 contained incomplete addrmap. So, it seems better to just ignore such
3150 indices. */
3151 if (version < 4)
3152 {
3153 static int warning_printed = 0;
3154 if (!warning_printed)
3155 {
3156 warning (_("Skipping obsolete .gdb_index section in %s."),
3157 filename);
3158 warning_printed = 1;
3159 }
3160 return 0;
3161 }
3162 /* Index version 4 uses a different hash function than index version
3163 5 and later.
3164
3165 Versions earlier than 6 did not emit psymbols for inlined
3166 functions. Using these files will cause GDB not to be able to
3167 set breakpoints on inlined functions by name, so we ignore these
3168 indices unless the user has done
3169 "set use-deprecated-index-sections on". */
3170 if (version < 6 && !deprecated_ok)
3171 {
3172 static int warning_printed = 0;
3173 if (!warning_printed)
3174 {
3175 warning (_("\
3176 Skipping deprecated .gdb_index section in %s.\n\
3177 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3178 to use the section anyway."),
3179 filename);
3180 warning_printed = 1;
3181 }
3182 return 0;
3183 }
3184 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3185 of the TU (for symbols coming from TUs),
3186 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3187 Plus gold-generated indices can have duplicate entries for global symbols,
3188 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3189 These are just performance bugs, and we can't distinguish gdb-generated
3190 indices from gold-generated ones, so issue no warning here. */
3191
3192 /* Indexes with higher version than the one supported by GDB may be no
3193 longer backward compatible. */
3194 if (version > 8)
3195 return 0;
3196
3197 map->version = version;
3198 map->total_size = section->size;
3199
3200 metadata = (offset_type *) (addr + sizeof (offset_type));
3201
3202 i = 0;
3203 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3204 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3205 / 8);
3206 ++i;
3207
3208 *types_list = addr + MAYBE_SWAP (metadata[i]);
3209 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3210 - MAYBE_SWAP (metadata[i]))
3211 / 8);
3212 ++i;
3213
3214 map->address_table = addr + MAYBE_SWAP (metadata[i]);
3215 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
3216 - MAYBE_SWAP (metadata[i]));
3217 ++i;
3218
3219 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
3220 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
3221 - MAYBE_SWAP (metadata[i]))
3222 / (2 * sizeof (offset_type)));
3223 ++i;
3224
3225 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3226
3227 return 1;
3228 }
3229
3230
3231 /* Read the index file. If everything went ok, initialize the "quick"
3232 elements of all the CUs and return 1. Otherwise, return 0. */
3233
3234 static int
3235 dwarf2_read_index (struct objfile *objfile)
3236 {
3237 struct mapped_index local_map, *map;
3238 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3239 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3240 struct dwz_file *dwz;
3241
3242 if (!read_index_from_section (objfile, objfile_name (objfile),
3243 use_deprecated_index_sections,
3244 &dwarf2_per_objfile->gdb_index, &local_map,
3245 &cu_list, &cu_list_elements,
3246 &types_list, &types_list_elements))
3247 return 0;
3248
3249 /* Don't use the index if it's empty. */
3250 if (local_map.symbol_table_slots == 0)
3251 return 0;
3252
3253 /* If there is a .dwz file, read it so we can get its CU list as
3254 well. */
3255 dwz = dwarf2_get_dwz_file ();
3256 if (dwz != NULL)
3257 {
3258 struct mapped_index dwz_map;
3259 const gdb_byte *dwz_types_ignore;
3260 offset_type dwz_types_elements_ignore;
3261
3262 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3263 1,
3264 &dwz->gdb_index, &dwz_map,
3265 &dwz_list, &dwz_list_elements,
3266 &dwz_types_ignore,
3267 &dwz_types_elements_ignore))
3268 {
3269 warning (_("could not read '.gdb_index' section from %s; skipping"),
3270 bfd_get_filename (dwz->dwz_bfd));
3271 return 0;
3272 }
3273 }
3274
3275 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3276 dwz_list_elements);
3277
3278 if (types_list_elements)
3279 {
3280 struct dwarf2_section_info *section;
3281
3282 /* We can only handle a single .debug_types when we have an
3283 index. */
3284 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3285 return 0;
3286
3287 section = VEC_index (dwarf2_section_info_def,
3288 dwarf2_per_objfile->types, 0);
3289
3290 create_signatured_type_table_from_index (objfile, section, types_list,
3291 types_list_elements);
3292 }
3293
3294 create_addrmap_from_index (objfile, &local_map);
3295
3296 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
3297 *map = local_map;
3298
3299 dwarf2_per_objfile->index_table = map;
3300 dwarf2_per_objfile->using_index = 1;
3301 dwarf2_per_objfile->quick_file_names_table =
3302 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3303
3304 return 1;
3305 }
3306
3307 /* A helper for the "quick" functions which sets the global
3308 dwarf2_per_objfile according to OBJFILE. */
3309
3310 static void
3311 dw2_setup (struct objfile *objfile)
3312 {
3313 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
3314 objfile_data (objfile, dwarf2_objfile_data_key));
3315 gdb_assert (dwarf2_per_objfile);
3316 }
3317
3318 /* die_reader_func for dw2_get_file_names. */
3319
3320 static void
3321 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3322 const gdb_byte *info_ptr,
3323 struct die_info *comp_unit_die,
3324 int has_children,
3325 void *data)
3326 {
3327 struct dwarf2_cu *cu = reader->cu;
3328 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3329 struct objfile *objfile = dwarf2_per_objfile->objfile;
3330 struct dwarf2_per_cu_data *lh_cu;
3331 struct line_header *lh;
3332 struct attribute *attr;
3333 int i;
3334 const char *name, *comp_dir;
3335 void **slot;
3336 struct quick_file_names *qfn;
3337 unsigned int line_offset;
3338
3339 gdb_assert (! this_cu->is_debug_types);
3340
3341 /* Our callers never want to match partial units -- instead they
3342 will match the enclosing full CU. */
3343 if (comp_unit_die->tag == DW_TAG_partial_unit)
3344 {
3345 this_cu->v.quick->no_file_data = 1;
3346 return;
3347 }
3348
3349 lh_cu = this_cu;
3350 lh = NULL;
3351 slot = NULL;
3352 line_offset = 0;
3353
3354 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3355 if (attr)
3356 {
3357 struct quick_file_names find_entry;
3358
3359 line_offset = DW_UNSND (attr);
3360
3361 /* We may have already read in this line header (TU line header sharing).
3362 If we have we're done. */
3363 find_entry.hash.dwo_unit = cu->dwo_unit;
3364 find_entry.hash.line_offset.sect_off = line_offset;
3365 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3366 &find_entry, INSERT);
3367 if (*slot != NULL)
3368 {
3369 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3370 return;
3371 }
3372
3373 lh = dwarf_decode_line_header (line_offset, cu);
3374 }
3375 if (lh == NULL)
3376 {
3377 lh_cu->v.quick->no_file_data = 1;
3378 return;
3379 }
3380
3381 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3382 qfn->hash.dwo_unit = cu->dwo_unit;
3383 qfn->hash.line_offset.sect_off = line_offset;
3384 gdb_assert (slot != NULL);
3385 *slot = qfn;
3386
3387 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
3388
3389 qfn->num_file_names = lh->num_file_names;
3390 qfn->file_names =
3391 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->num_file_names);
3392 for (i = 0; i < lh->num_file_names; ++i)
3393 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
3394 qfn->real_names = NULL;
3395
3396 free_line_header (lh);
3397
3398 lh_cu->v.quick->file_names = qfn;
3399 }
3400
3401 /* A helper for the "quick" functions which attempts to read the line
3402 table for THIS_CU. */
3403
3404 static struct quick_file_names *
3405 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3406 {
3407 /* This should never be called for TUs. */
3408 gdb_assert (! this_cu->is_debug_types);
3409 /* Nor type unit groups. */
3410 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3411
3412 if (this_cu->v.quick->file_names != NULL)
3413 return this_cu->v.quick->file_names;
3414 /* If we know there is no line data, no point in looking again. */
3415 if (this_cu->v.quick->no_file_data)
3416 return NULL;
3417
3418 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
3419
3420 if (this_cu->v.quick->no_file_data)
3421 return NULL;
3422 return this_cu->v.quick->file_names;
3423 }
3424
3425 /* A helper for the "quick" functions which computes and caches the
3426 real path for a given file name from the line table. */
3427
3428 static const char *
3429 dw2_get_real_path (struct objfile *objfile,
3430 struct quick_file_names *qfn, int index)
3431 {
3432 if (qfn->real_names == NULL)
3433 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3434 qfn->num_file_names, const char *);
3435
3436 if (qfn->real_names[index] == NULL)
3437 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
3438
3439 return qfn->real_names[index];
3440 }
3441
3442 static struct symtab *
3443 dw2_find_last_source_symtab (struct objfile *objfile)
3444 {
3445 struct compunit_symtab *cust;
3446 int index;
3447
3448 dw2_setup (objfile);
3449 index = dwarf2_per_objfile->n_comp_units - 1;
3450 cust = dw2_instantiate_symtab (dw2_get_cutu (index));
3451 if (cust == NULL)
3452 return NULL;
3453 return compunit_primary_filetab (cust);
3454 }
3455
3456 /* Traversal function for dw2_forget_cached_source_info. */
3457
3458 static int
3459 dw2_free_cached_file_names (void **slot, void *info)
3460 {
3461 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3462
3463 if (file_data->real_names)
3464 {
3465 int i;
3466
3467 for (i = 0; i < file_data->num_file_names; ++i)
3468 {
3469 xfree ((void*) file_data->real_names[i]);
3470 file_data->real_names[i] = NULL;
3471 }
3472 }
3473
3474 return 1;
3475 }
3476
3477 static void
3478 dw2_forget_cached_source_info (struct objfile *objfile)
3479 {
3480 dw2_setup (objfile);
3481
3482 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3483 dw2_free_cached_file_names, NULL);
3484 }
3485
3486 /* Helper function for dw2_map_symtabs_matching_filename that expands
3487 the symtabs and calls the iterator. */
3488
3489 static int
3490 dw2_map_expand_apply (struct objfile *objfile,
3491 struct dwarf2_per_cu_data *per_cu,
3492 const char *name, const char *real_path,
3493 int (*callback) (struct symtab *, void *),
3494 void *data)
3495 {
3496 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3497
3498 /* Don't visit already-expanded CUs. */
3499 if (per_cu->v.quick->compunit_symtab)
3500 return 0;
3501
3502 /* This may expand more than one symtab, and we want to iterate over
3503 all of them. */
3504 dw2_instantiate_symtab (per_cu);
3505
3506 return iterate_over_some_symtabs (name, real_path, callback, data,
3507 objfile->compunit_symtabs, last_made);
3508 }
3509
3510 /* Implementation of the map_symtabs_matching_filename method. */
3511
3512 static int
3513 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
3514 const char *real_path,
3515 int (*callback) (struct symtab *, void *),
3516 void *data)
3517 {
3518 int i;
3519 const char *name_basename = lbasename (name);
3520
3521 dw2_setup (objfile);
3522
3523 /* The rule is CUs specify all the files, including those used by
3524 any TU, so there's no need to scan TUs here. */
3525
3526 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3527 {
3528 int j;
3529 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3530 struct quick_file_names *file_data;
3531
3532 /* We only need to look at symtabs not already expanded. */
3533 if (per_cu->v.quick->compunit_symtab)
3534 continue;
3535
3536 file_data = dw2_get_file_names (per_cu);
3537 if (file_data == NULL)
3538 continue;
3539
3540 for (j = 0; j < file_data->num_file_names; ++j)
3541 {
3542 const char *this_name = file_data->file_names[j];
3543 const char *this_real_name;
3544
3545 if (compare_filenames_for_search (this_name, name))
3546 {
3547 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3548 callback, data))
3549 return 1;
3550 continue;
3551 }
3552
3553 /* Before we invoke realpath, which can get expensive when many
3554 files are involved, do a quick comparison of the basenames. */
3555 if (! basenames_may_differ
3556 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3557 continue;
3558
3559 this_real_name = dw2_get_real_path (objfile, file_data, j);
3560 if (compare_filenames_for_search (this_real_name, name))
3561 {
3562 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3563 callback, data))
3564 return 1;
3565 continue;
3566 }
3567
3568 if (real_path != NULL)
3569 {
3570 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3571 gdb_assert (IS_ABSOLUTE_PATH (name));
3572 if (this_real_name != NULL
3573 && FILENAME_CMP (real_path, this_real_name) == 0)
3574 {
3575 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3576 callback, data))
3577 return 1;
3578 continue;
3579 }
3580 }
3581 }
3582 }
3583
3584 return 0;
3585 }
3586
3587 /* Struct used to manage iterating over all CUs looking for a symbol. */
3588
3589 struct dw2_symtab_iterator
3590 {
3591 /* The internalized form of .gdb_index. */
3592 struct mapped_index *index;
3593 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3594 int want_specific_block;
3595 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3596 Unused if !WANT_SPECIFIC_BLOCK. */
3597 int block_index;
3598 /* The kind of symbol we're looking for. */
3599 domain_enum domain;
3600 /* The list of CUs from the index entry of the symbol,
3601 or NULL if not found. */
3602 offset_type *vec;
3603 /* The next element in VEC to look at. */
3604 int next;
3605 /* The number of elements in VEC, or zero if there is no match. */
3606 int length;
3607 /* Have we seen a global version of the symbol?
3608 If so we can ignore all further global instances.
3609 This is to work around gold/15646, inefficient gold-generated
3610 indices. */
3611 int global_seen;
3612 };
3613
3614 /* Initialize the index symtab iterator ITER.
3615 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3616 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
3617
3618 static void
3619 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3620 struct mapped_index *index,
3621 int want_specific_block,
3622 int block_index,
3623 domain_enum domain,
3624 const char *name)
3625 {
3626 iter->index = index;
3627 iter->want_specific_block = want_specific_block;
3628 iter->block_index = block_index;
3629 iter->domain = domain;
3630 iter->next = 0;
3631 iter->global_seen = 0;
3632
3633 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3634 iter->length = MAYBE_SWAP (*iter->vec);
3635 else
3636 {
3637 iter->vec = NULL;
3638 iter->length = 0;
3639 }
3640 }
3641
3642 /* Return the next matching CU or NULL if there are no more. */
3643
3644 static struct dwarf2_per_cu_data *
3645 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3646 {
3647 for ( ; iter->next < iter->length; ++iter->next)
3648 {
3649 offset_type cu_index_and_attrs =
3650 MAYBE_SWAP (iter->vec[iter->next + 1]);
3651 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3652 struct dwarf2_per_cu_data *per_cu;
3653 int want_static = iter->block_index != GLOBAL_BLOCK;
3654 /* This value is only valid for index versions >= 7. */
3655 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3656 gdb_index_symbol_kind symbol_kind =
3657 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3658 /* Only check the symbol attributes if they're present.
3659 Indices prior to version 7 don't record them,
3660 and indices >= 7 may elide them for certain symbols
3661 (gold does this). */
3662 int attrs_valid =
3663 (iter->index->version >= 7
3664 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3665
3666 /* Don't crash on bad data. */
3667 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3668 + dwarf2_per_objfile->n_type_units))
3669 {
3670 complaint (&symfile_complaints,
3671 _(".gdb_index entry has bad CU index"
3672 " [in module %s]"),
3673 objfile_name (dwarf2_per_objfile->objfile));
3674 continue;
3675 }
3676
3677 per_cu = dw2_get_cutu (cu_index);
3678
3679 /* Skip if already read in. */
3680 if (per_cu->v.quick->compunit_symtab)
3681 continue;
3682
3683 /* Check static vs global. */
3684 if (attrs_valid)
3685 {
3686 if (iter->want_specific_block
3687 && want_static != is_static)
3688 continue;
3689 /* Work around gold/15646. */
3690 if (!is_static && iter->global_seen)
3691 continue;
3692 if (!is_static)
3693 iter->global_seen = 1;
3694 }
3695
3696 /* Only check the symbol's kind if it has one. */
3697 if (attrs_valid)
3698 {
3699 switch (iter->domain)
3700 {
3701 case VAR_DOMAIN:
3702 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3703 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3704 /* Some types are also in VAR_DOMAIN. */
3705 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3706 continue;
3707 break;
3708 case STRUCT_DOMAIN:
3709 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3710 continue;
3711 break;
3712 case LABEL_DOMAIN:
3713 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3714 continue;
3715 break;
3716 default:
3717 break;
3718 }
3719 }
3720
3721 ++iter->next;
3722 return per_cu;
3723 }
3724
3725 return NULL;
3726 }
3727
3728 static struct compunit_symtab *
3729 dw2_lookup_symbol (struct objfile *objfile, int block_index,
3730 const char *name, domain_enum domain)
3731 {
3732 struct compunit_symtab *stab_best = NULL;
3733 struct mapped_index *index;
3734
3735 dw2_setup (objfile);
3736
3737 index = dwarf2_per_objfile->index_table;
3738
3739 /* index is NULL if OBJF_READNOW. */
3740 if (index)
3741 {
3742 struct dw2_symtab_iterator iter;
3743 struct dwarf2_per_cu_data *per_cu;
3744
3745 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
3746
3747 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3748 {
3749 struct symbol *sym, *with_opaque = NULL;
3750 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
3751 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3752 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3753
3754 sym = block_find_symbol (block, name, domain,
3755 block_find_non_opaque_type_preferred,
3756 &with_opaque);
3757
3758 /* Some caution must be observed with overloaded functions
3759 and methods, since the index will not contain any overload
3760 information (but NAME might contain it). */
3761
3762 if (sym != NULL
3763 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3764 return stab;
3765 if (with_opaque != NULL
3766 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
3767 stab_best = stab;
3768
3769 /* Keep looking through other CUs. */
3770 }
3771 }
3772
3773 return stab_best;
3774 }
3775
3776 static void
3777 dw2_print_stats (struct objfile *objfile)
3778 {
3779 int i, total, count;
3780
3781 dw2_setup (objfile);
3782 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
3783 count = 0;
3784 for (i = 0; i < total; ++i)
3785 {
3786 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
3787
3788 if (!per_cu->v.quick->compunit_symtab)
3789 ++count;
3790 }
3791 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3792 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3793 }
3794
3795 /* This dumps minimal information about the index.
3796 It is called via "mt print objfiles".
3797 One use is to verify .gdb_index has been loaded by the
3798 gdb.dwarf2/gdb-index.exp testcase. */
3799
3800 static void
3801 dw2_dump (struct objfile *objfile)
3802 {
3803 dw2_setup (objfile);
3804 gdb_assert (dwarf2_per_objfile->using_index);
3805 printf_filtered (".gdb_index:");
3806 if (dwarf2_per_objfile->index_table != NULL)
3807 {
3808 printf_filtered (" version %d\n",
3809 dwarf2_per_objfile->index_table->version);
3810 }
3811 else
3812 printf_filtered (" faked for \"readnow\"\n");
3813 printf_filtered ("\n");
3814 }
3815
3816 static void
3817 dw2_relocate (struct objfile *objfile,
3818 const struct section_offsets *new_offsets,
3819 const struct section_offsets *delta)
3820 {
3821 /* There's nothing to relocate here. */
3822 }
3823
3824 static void
3825 dw2_expand_symtabs_for_function (struct objfile *objfile,
3826 const char *func_name)
3827 {
3828 struct mapped_index *index;
3829
3830 dw2_setup (objfile);
3831
3832 index = dwarf2_per_objfile->index_table;
3833
3834 /* index is NULL if OBJF_READNOW. */
3835 if (index)
3836 {
3837 struct dw2_symtab_iterator iter;
3838 struct dwarf2_per_cu_data *per_cu;
3839
3840 /* Note: It doesn't matter what we pass for block_index here. */
3841 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3842 func_name);
3843
3844 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3845 dw2_instantiate_symtab (per_cu);
3846 }
3847 }
3848
3849 static void
3850 dw2_expand_all_symtabs (struct objfile *objfile)
3851 {
3852 int i;
3853
3854 dw2_setup (objfile);
3855
3856 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3857 + dwarf2_per_objfile->n_type_units); ++i)
3858 {
3859 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
3860
3861 dw2_instantiate_symtab (per_cu);
3862 }
3863 }
3864
3865 static void
3866 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3867 const char *fullname)
3868 {
3869 int i;
3870
3871 dw2_setup (objfile);
3872
3873 /* We don't need to consider type units here.
3874 This is only called for examining code, e.g. expand_line_sal.
3875 There can be an order of magnitude (or more) more type units
3876 than comp units, and we avoid them if we can. */
3877
3878 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3879 {
3880 int j;
3881 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
3882 struct quick_file_names *file_data;
3883
3884 /* We only need to look at symtabs not already expanded. */
3885 if (per_cu->v.quick->compunit_symtab)
3886 continue;
3887
3888 file_data = dw2_get_file_names (per_cu);
3889 if (file_data == NULL)
3890 continue;
3891
3892 for (j = 0; j < file_data->num_file_names; ++j)
3893 {
3894 const char *this_fullname = file_data->file_names[j];
3895
3896 if (filename_cmp (this_fullname, fullname) == 0)
3897 {
3898 dw2_instantiate_symtab (per_cu);
3899 break;
3900 }
3901 }
3902 }
3903 }
3904
3905 static void
3906 dw2_map_matching_symbols (struct objfile *objfile,
3907 const char * name, domain_enum domain,
3908 int global,
3909 int (*callback) (struct block *,
3910 struct symbol *, void *),
3911 void *data, symbol_compare_ftype *match,
3912 symbol_compare_ftype *ordered_compare)
3913 {
3914 /* Currently unimplemented; used for Ada. The function can be called if the
3915 current language is Ada for a non-Ada objfile using GNU index. As Ada
3916 does not look for non-Ada symbols this function should just return. */
3917 }
3918
3919 static void
3920 dw2_expand_symtabs_matching
3921 (struct objfile *objfile,
3922 expand_symtabs_file_matcher_ftype *file_matcher,
3923 expand_symtabs_symbol_matcher_ftype *symbol_matcher,
3924 expand_symtabs_exp_notify_ftype *expansion_notify,
3925 enum search_domain kind,
3926 void *data)
3927 {
3928 int i;
3929 offset_type iter;
3930 struct mapped_index *index;
3931
3932 dw2_setup (objfile);
3933
3934 /* index_table is NULL if OBJF_READNOW. */
3935 if (!dwarf2_per_objfile->index_table)
3936 return;
3937 index = dwarf2_per_objfile->index_table;
3938
3939 if (file_matcher != NULL)
3940 {
3941 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
3942 htab_eq_pointer,
3943 NULL, xcalloc, xfree));
3944 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
3945 htab_eq_pointer,
3946 NULL, xcalloc, xfree));
3947
3948 /* The rule is CUs specify all the files, including those used by
3949 any TU, so there's no need to scan TUs here. */
3950
3951 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3952 {
3953 int j;
3954 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3955 struct quick_file_names *file_data;
3956 void **slot;
3957
3958 QUIT;
3959
3960 per_cu->v.quick->mark = 0;
3961
3962 /* We only need to look at symtabs not already expanded. */
3963 if (per_cu->v.quick->compunit_symtab)
3964 continue;
3965
3966 file_data = dw2_get_file_names (per_cu);
3967 if (file_data == NULL)
3968 continue;
3969
3970 if (htab_find (visited_not_found.get (), file_data) != NULL)
3971 continue;
3972 else if (htab_find (visited_found.get (), file_data) != NULL)
3973 {
3974 per_cu->v.quick->mark = 1;
3975 continue;
3976 }
3977
3978 for (j = 0; j < file_data->num_file_names; ++j)
3979 {
3980 const char *this_real_name;
3981
3982 if (file_matcher (file_data->file_names[j], data, 0))
3983 {
3984 per_cu->v.quick->mark = 1;
3985 break;
3986 }
3987
3988 /* Before we invoke realpath, which can get expensive when many
3989 files are involved, do a quick comparison of the basenames. */
3990 if (!basenames_may_differ
3991 && !file_matcher (lbasename (file_data->file_names[j]),
3992 data, 1))
3993 continue;
3994
3995 this_real_name = dw2_get_real_path (objfile, file_data, j);
3996 if (file_matcher (this_real_name, data, 0))
3997 {
3998 per_cu->v.quick->mark = 1;
3999 break;
4000 }
4001 }
4002
4003 slot = htab_find_slot (per_cu->v.quick->mark
4004 ? visited_found.get ()
4005 : visited_not_found.get (),
4006 file_data, INSERT);
4007 *slot = file_data;
4008 }
4009 }
4010
4011 for (iter = 0; iter < index->symbol_table_slots; ++iter)
4012 {
4013 offset_type idx = 2 * iter;
4014 const char *name;
4015 offset_type *vec, vec_len, vec_idx;
4016 int global_seen = 0;
4017
4018 QUIT;
4019
4020 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
4021 continue;
4022
4023 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
4024
4025 if (! (*symbol_matcher) (name, data))
4026 continue;
4027
4028 /* The name was matched, now expand corresponding CUs that were
4029 marked. */
4030 vec = (offset_type *) (index->constant_pool
4031 + MAYBE_SWAP (index->symbol_table[idx + 1]));
4032 vec_len = MAYBE_SWAP (vec[0]);
4033 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4034 {
4035 struct dwarf2_per_cu_data *per_cu;
4036 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4037 /* This value is only valid for index versions >= 7. */
4038 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4039 gdb_index_symbol_kind symbol_kind =
4040 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4041 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4042 /* Only check the symbol attributes if they're present.
4043 Indices prior to version 7 don't record them,
4044 and indices >= 7 may elide them for certain symbols
4045 (gold does this). */
4046 int attrs_valid =
4047 (index->version >= 7
4048 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4049
4050 /* Work around gold/15646. */
4051 if (attrs_valid)
4052 {
4053 if (!is_static && global_seen)
4054 continue;
4055 if (!is_static)
4056 global_seen = 1;
4057 }
4058
4059 /* Only check the symbol's kind if it has one. */
4060 if (attrs_valid)
4061 {
4062 switch (kind)
4063 {
4064 case VARIABLES_DOMAIN:
4065 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4066 continue;
4067 break;
4068 case FUNCTIONS_DOMAIN:
4069 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4070 continue;
4071 break;
4072 case TYPES_DOMAIN:
4073 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4074 continue;
4075 break;
4076 default:
4077 break;
4078 }
4079 }
4080
4081 /* Don't crash on bad data. */
4082 if (cu_index >= (dwarf2_per_objfile->n_comp_units
4083 + dwarf2_per_objfile->n_type_units))
4084 {
4085 complaint (&symfile_complaints,
4086 _(".gdb_index entry has bad CU index"
4087 " [in module %s]"), objfile_name (objfile));
4088 continue;
4089 }
4090
4091 per_cu = dw2_get_cutu (cu_index);
4092 if (file_matcher == NULL || per_cu->v.quick->mark)
4093 {
4094 int symtab_was_null =
4095 (per_cu->v.quick->compunit_symtab == NULL);
4096
4097 dw2_instantiate_symtab (per_cu);
4098
4099 if (expansion_notify != NULL
4100 && symtab_was_null
4101 && per_cu->v.quick->compunit_symtab != NULL)
4102 {
4103 expansion_notify (per_cu->v.quick->compunit_symtab,
4104 data);
4105 }
4106 }
4107 }
4108 }
4109 }
4110
4111 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4112 symtab. */
4113
4114 static struct compunit_symtab *
4115 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4116 CORE_ADDR pc)
4117 {
4118 int i;
4119
4120 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4121 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4122 return cust;
4123
4124 if (cust->includes == NULL)
4125 return NULL;
4126
4127 for (i = 0; cust->includes[i]; ++i)
4128 {
4129 struct compunit_symtab *s = cust->includes[i];
4130
4131 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4132 if (s != NULL)
4133 return s;
4134 }
4135
4136 return NULL;
4137 }
4138
4139 static struct compunit_symtab *
4140 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4141 struct bound_minimal_symbol msymbol,
4142 CORE_ADDR pc,
4143 struct obj_section *section,
4144 int warn_if_readin)
4145 {
4146 struct dwarf2_per_cu_data *data;
4147 struct compunit_symtab *result;
4148
4149 dw2_setup (objfile);
4150
4151 if (!objfile->psymtabs_addrmap)
4152 return NULL;
4153
4154 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
4155 pc);
4156 if (!data)
4157 return NULL;
4158
4159 if (warn_if_readin && data->v.quick->compunit_symtab)
4160 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4161 paddress (get_objfile_arch (objfile), pc));
4162
4163 result
4164 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
4165 pc);
4166 gdb_assert (result != NULL);
4167 return result;
4168 }
4169
4170 static void
4171 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4172 void *data, int need_fullname)
4173 {
4174 int i;
4175 htab_up visited (htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
4176 NULL, xcalloc, xfree));
4177
4178 dw2_setup (objfile);
4179
4180 /* The rule is CUs specify all the files, including those used by
4181 any TU, so there's no need to scan TUs here.
4182 We can ignore file names coming from already-expanded CUs. */
4183
4184 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4185 {
4186 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
4187
4188 if (per_cu->v.quick->compunit_symtab)
4189 {
4190 void **slot = htab_find_slot (visited.get (),
4191 per_cu->v.quick->file_names,
4192 INSERT);
4193
4194 *slot = per_cu->v.quick->file_names;
4195 }
4196 }
4197
4198 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4199 {
4200 int j;
4201 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4202 struct quick_file_names *file_data;
4203 void **slot;
4204
4205 /* We only need to look at symtabs not already expanded. */
4206 if (per_cu->v.quick->compunit_symtab)
4207 continue;
4208
4209 file_data = dw2_get_file_names (per_cu);
4210 if (file_data == NULL)
4211 continue;
4212
4213 slot = htab_find_slot (visited.get (), file_data, INSERT);
4214 if (*slot)
4215 {
4216 /* Already visited. */
4217 continue;
4218 }
4219 *slot = file_data;
4220
4221 for (j = 0; j < file_data->num_file_names; ++j)
4222 {
4223 const char *this_real_name;
4224
4225 if (need_fullname)
4226 this_real_name = dw2_get_real_path (objfile, file_data, j);
4227 else
4228 this_real_name = NULL;
4229 (*fun) (file_data->file_names[j], this_real_name, data);
4230 }
4231 }
4232 }
4233
4234 static int
4235 dw2_has_symbols (struct objfile *objfile)
4236 {
4237 return 1;
4238 }
4239
4240 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4241 {
4242 dw2_has_symbols,
4243 dw2_find_last_source_symtab,
4244 dw2_forget_cached_source_info,
4245 dw2_map_symtabs_matching_filename,
4246 dw2_lookup_symbol,
4247 dw2_print_stats,
4248 dw2_dump,
4249 dw2_relocate,
4250 dw2_expand_symtabs_for_function,
4251 dw2_expand_all_symtabs,
4252 dw2_expand_symtabs_with_fullname,
4253 dw2_map_matching_symbols,
4254 dw2_expand_symtabs_matching,
4255 dw2_find_pc_sect_compunit_symtab,
4256 dw2_map_symbol_filenames
4257 };
4258
4259 /* Initialize for reading DWARF for this objfile. Return 0 if this
4260 file will use psymtabs, or 1 if using the GNU index. */
4261
4262 int
4263 dwarf2_initialize_objfile (struct objfile *objfile)
4264 {
4265 /* If we're about to read full symbols, don't bother with the
4266 indices. In this case we also don't care if some other debug
4267 format is making psymtabs, because they are all about to be
4268 expanded anyway. */
4269 if ((objfile->flags & OBJF_READNOW))
4270 {
4271 int i;
4272
4273 dwarf2_per_objfile->using_index = 1;
4274 create_all_comp_units (objfile);
4275 create_all_type_units (objfile);
4276 dwarf2_per_objfile->quick_file_names_table =
4277 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
4278
4279 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
4280 + dwarf2_per_objfile->n_type_units); ++i)
4281 {
4282 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
4283
4284 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4285 struct dwarf2_per_cu_quick_data);
4286 }
4287
4288 /* Return 1 so that gdb sees the "quick" functions. However,
4289 these functions will be no-ops because we will have expanded
4290 all symtabs. */
4291 return 1;
4292 }
4293
4294 if (dwarf2_read_index (objfile))
4295 return 1;
4296
4297 return 0;
4298 }
4299
4300 \f
4301
4302 /* Build a partial symbol table. */
4303
4304 void
4305 dwarf2_build_psymtabs (struct objfile *objfile)
4306 {
4307
4308 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
4309 {
4310 init_psymbol_list (objfile, 1024);
4311 }
4312
4313 TRY
4314 {
4315 /* This isn't really ideal: all the data we allocate on the
4316 objfile's obstack is still uselessly kept around. However,
4317 freeing it seems unsafe. */
4318 psymtab_discarder psymtabs (objfile);
4319 dwarf2_build_psymtabs_hard (objfile);
4320 psymtabs.keep ();
4321 }
4322 CATCH (except, RETURN_MASK_ERROR)
4323 {
4324 exception_print (gdb_stderr, except);
4325 }
4326 END_CATCH
4327 }
4328
4329 /* Return the total length of the CU described by HEADER. */
4330
4331 static unsigned int
4332 get_cu_length (const struct comp_unit_head *header)
4333 {
4334 return header->initial_length_size + header->length;
4335 }
4336
4337 /* Return TRUE if OFFSET is within CU_HEADER. */
4338
4339 static inline int
4340 offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
4341 {
4342 sect_offset bottom = { cu_header->offset.sect_off };
4343 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
4344
4345 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
4346 }
4347
4348 /* Find the base address of the compilation unit for range lists and
4349 location lists. It will normally be specified by DW_AT_low_pc.
4350 In DWARF-3 draft 4, the base address could be overridden by
4351 DW_AT_entry_pc. It's been removed, but GCC still uses this for
4352 compilation units with discontinuous ranges. */
4353
4354 static void
4355 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
4356 {
4357 struct attribute *attr;
4358
4359 cu->base_known = 0;
4360 cu->base_address = 0;
4361
4362 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
4363 if (attr)
4364 {
4365 cu->base_address = attr_value_as_address (attr);
4366 cu->base_known = 1;
4367 }
4368 else
4369 {
4370 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
4371 if (attr)
4372 {
4373 cu->base_address = attr_value_as_address (attr);
4374 cu->base_known = 1;
4375 }
4376 }
4377 }
4378
4379 /* Read in the comp unit header information from the debug_info at info_ptr.
4380 Use rcuh_kind::COMPILE as the default type if not known by the caller.
4381 NOTE: This leaves members offset, first_die_offset to be filled in
4382 by the caller. */
4383
4384 static const gdb_byte *
4385 read_comp_unit_head (struct comp_unit_head *cu_header,
4386 const gdb_byte *info_ptr,
4387 struct dwarf2_section_info *section,
4388 rcuh_kind section_kind)
4389 {
4390 int signed_addr;
4391 unsigned int bytes_read;
4392 const char *filename = get_section_file_name (section);
4393 bfd *abfd = get_section_bfd_owner (section);
4394
4395 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
4396 cu_header->initial_length_size = bytes_read;
4397 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
4398 info_ptr += bytes_read;
4399 cu_header->version = read_2_bytes (abfd, info_ptr);
4400 info_ptr += 2;
4401 if (cu_header->version < 5)
4402 switch (section_kind)
4403 {
4404 case rcuh_kind::COMPILE:
4405 cu_header->unit_type = DW_UT_compile;
4406 break;
4407 case rcuh_kind::TYPE:
4408 cu_header->unit_type = DW_UT_type;
4409 break;
4410 default:
4411 internal_error (__FILE__, __LINE__,
4412 _("read_comp_unit_head: invalid section_kind"));
4413 }
4414 else
4415 {
4416 cu_header->unit_type = static_cast<enum dwarf_unit_type>
4417 (read_1_byte (abfd, info_ptr));
4418 info_ptr += 1;
4419 switch (cu_header->unit_type)
4420 {
4421 case DW_UT_compile:
4422 if (section_kind != rcuh_kind::COMPILE)
4423 error (_("Dwarf Error: wrong unit_type in compilation unit header "
4424 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
4425 filename);
4426 break;
4427 case DW_UT_type:
4428 section_kind = rcuh_kind::TYPE;
4429 break;
4430 default:
4431 error (_("Dwarf Error: wrong unit_type in compilation unit header "
4432 "(is %d, should be %d or %d) [in module %s]"),
4433 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
4434 }
4435
4436 cu_header->addr_size = read_1_byte (abfd, info_ptr);
4437 info_ptr += 1;
4438 }
4439 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
4440 &bytes_read);
4441 info_ptr += bytes_read;
4442 if (cu_header->version < 5)
4443 {
4444 cu_header->addr_size = read_1_byte (abfd, info_ptr);
4445 info_ptr += 1;
4446 }
4447 signed_addr = bfd_get_sign_extend_vma (abfd);
4448 if (signed_addr < 0)
4449 internal_error (__FILE__, __LINE__,
4450 _("read_comp_unit_head: dwarf from non elf file"));
4451 cu_header->signed_addr_p = signed_addr;
4452
4453 if (section_kind == rcuh_kind::TYPE)
4454 {
4455 LONGEST type_offset;
4456
4457 cu_header->signature = read_8_bytes (abfd, info_ptr);
4458 info_ptr += 8;
4459
4460 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
4461 info_ptr += bytes_read;
4462 cu_header->type_offset_in_tu.cu_off = type_offset;
4463 if (cu_header->type_offset_in_tu.cu_off != type_offset)
4464 error (_("Dwarf Error: Too big type_offset in compilation unit "
4465 "header (is %s) [in module %s]"), plongest (type_offset),
4466 filename);
4467 }
4468
4469 return info_ptr;
4470 }
4471
4472 /* Helper function that returns the proper abbrev section for
4473 THIS_CU. */
4474
4475 static struct dwarf2_section_info *
4476 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
4477 {
4478 struct dwarf2_section_info *abbrev;
4479
4480 if (this_cu->is_dwz)
4481 abbrev = &dwarf2_get_dwz_file ()->abbrev;
4482 else
4483 abbrev = &dwarf2_per_objfile->abbrev;
4484
4485 return abbrev;
4486 }
4487
4488 /* Subroutine of read_and_check_comp_unit_head and
4489 read_and_check_type_unit_head to simplify them.
4490 Perform various error checking on the header. */
4491
4492 static void
4493 error_check_comp_unit_head (struct comp_unit_head *header,
4494 struct dwarf2_section_info *section,
4495 struct dwarf2_section_info *abbrev_section)
4496 {
4497 const char *filename = get_section_file_name (section);
4498
4499 if (header->version < 2 || header->version > 5)
4500 error (_("Dwarf Error: wrong version in compilation unit header "
4501 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
4502 filename);
4503
4504 if (header->abbrev_offset.sect_off
4505 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
4506 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
4507 "(offset 0x%lx + 6) [in module %s]"),
4508 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
4509 filename);
4510
4511 /* Cast to unsigned long to use 64-bit arithmetic when possible to
4512 avoid potential 32-bit overflow. */
4513 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
4514 > section->size)
4515 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
4516 "(offset 0x%lx + 0) [in module %s]"),
4517 (long) header->length, (long) header->offset.sect_off,
4518 filename);
4519 }
4520
4521 /* Read in a CU/TU header and perform some basic error checking.
4522 The contents of the header are stored in HEADER.
4523 The result is a pointer to the start of the first DIE. */
4524
4525 static const gdb_byte *
4526 read_and_check_comp_unit_head (struct comp_unit_head *header,
4527 struct dwarf2_section_info *section,
4528 struct dwarf2_section_info *abbrev_section,
4529 const gdb_byte *info_ptr,
4530 rcuh_kind section_kind)
4531 {
4532 const gdb_byte *beg_of_comp_unit = info_ptr;
4533 bfd *abfd = get_section_bfd_owner (section);
4534
4535 header->offset.sect_off = beg_of_comp_unit - section->buffer;
4536
4537 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
4538
4539 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
4540
4541 error_check_comp_unit_head (header, section, abbrev_section);
4542
4543 return info_ptr;
4544 }
4545
4546 /* Fetch the abbreviation table offset from a comp or type unit header. */
4547
4548 static sect_offset
4549 read_abbrev_offset (struct dwarf2_section_info *section,
4550 sect_offset offset)
4551 {
4552 bfd *abfd = get_section_bfd_owner (section);
4553 const gdb_byte *info_ptr;
4554 unsigned int initial_length_size, offset_size;
4555 sect_offset abbrev_offset;
4556 uint16_t version;
4557
4558 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4559 info_ptr = section->buffer + offset.sect_off;
4560 read_initial_length (abfd, info_ptr, &initial_length_size);
4561 offset_size = initial_length_size == 4 ? 4 : 8;
4562 info_ptr += initial_length_size;
4563
4564 version = read_2_bytes (abfd, info_ptr);
4565 info_ptr += 2;
4566 if (version >= 5)
4567 {
4568 /* Skip unit type and address size. */
4569 info_ptr += 2;
4570 }
4571
4572 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4573 return abbrev_offset;
4574 }
4575
4576 /* Allocate a new partial symtab for file named NAME and mark this new
4577 partial symtab as being an include of PST. */
4578
4579 static void
4580 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
4581 struct objfile *objfile)
4582 {
4583 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4584
4585 if (!IS_ABSOLUTE_PATH (subpst->filename))
4586 {
4587 /* It shares objfile->objfile_obstack. */
4588 subpst->dirname = pst->dirname;
4589 }
4590
4591 subpst->textlow = 0;
4592 subpst->texthigh = 0;
4593
4594 subpst->dependencies
4595 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
4596 subpst->dependencies[0] = pst;
4597 subpst->number_of_dependencies = 1;
4598
4599 subpst->globals_offset = 0;
4600 subpst->n_global_syms = 0;
4601 subpst->statics_offset = 0;
4602 subpst->n_static_syms = 0;
4603 subpst->compunit_symtab = NULL;
4604 subpst->read_symtab = pst->read_symtab;
4605 subpst->readin = 0;
4606
4607 /* No private part is necessary for include psymtabs. This property
4608 can be used to differentiate between such include psymtabs and
4609 the regular ones. */
4610 subpst->read_symtab_private = NULL;
4611 }
4612
4613 /* Read the Line Number Program data and extract the list of files
4614 included by the source file represented by PST. Build an include
4615 partial symtab for each of these included files. */
4616
4617 static void
4618 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
4619 struct die_info *die,
4620 struct partial_symtab *pst)
4621 {
4622 struct line_header *lh = NULL;
4623 struct attribute *attr;
4624
4625 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4626 if (attr)
4627 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
4628 if (lh == NULL)
4629 return; /* No linetable, so no includes. */
4630
4631 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
4632 dwarf_decode_lines (lh, pst->dirname, cu, pst, pst->textlow, 1);
4633
4634 free_line_header (lh);
4635 }
4636
4637 static hashval_t
4638 hash_signatured_type (const void *item)
4639 {
4640 const struct signatured_type *sig_type
4641 = (const struct signatured_type *) item;
4642
4643 /* This drops the top 32 bits of the signature, but is ok for a hash. */
4644 return sig_type->signature;
4645 }
4646
4647 static int
4648 eq_signatured_type (const void *item_lhs, const void *item_rhs)
4649 {
4650 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
4651 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
4652
4653 return lhs->signature == rhs->signature;
4654 }
4655
4656 /* Allocate a hash table for signatured types. */
4657
4658 static htab_t
4659 allocate_signatured_type_table (struct objfile *objfile)
4660 {
4661 return htab_create_alloc_ex (41,
4662 hash_signatured_type,
4663 eq_signatured_type,
4664 NULL,
4665 &objfile->objfile_obstack,
4666 hashtab_obstack_allocate,
4667 dummy_obstack_deallocate);
4668 }
4669
4670 /* A helper function to add a signatured type CU to a table. */
4671
4672 static int
4673 add_signatured_type_cu_to_table (void **slot, void *datum)
4674 {
4675 struct signatured_type *sigt = (struct signatured_type *) *slot;
4676 struct signatured_type ***datap = (struct signatured_type ***) datum;
4677
4678 **datap = sigt;
4679 ++*datap;
4680
4681 return 1;
4682 }
4683
4684 /* A helper for create_debug_types_hash_table. Read types from SECTION
4685 and fill them into TYPES_HTAB. It will process only type units,
4686 therefore DW_UT_type. */
4687
4688 static void
4689 create_debug_type_hash_table (struct dwo_file *dwo_file,
4690 dwarf2_section_info *section, htab_t &types_htab,
4691 rcuh_kind section_kind)
4692 {
4693 struct objfile *objfile = dwarf2_per_objfile->objfile;
4694 struct dwarf2_section_info *abbrev_section;
4695 bfd *abfd;
4696 const gdb_byte *info_ptr, *end_ptr;
4697
4698 abbrev_section = (dwo_file != NULL
4699 ? &dwo_file->sections.abbrev
4700 : &dwarf2_per_objfile->abbrev);
4701
4702 if (dwarf_read_debug)
4703 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
4704 get_section_name (section),
4705 get_section_file_name (abbrev_section));
4706
4707 dwarf2_read_section (objfile, section);
4708 info_ptr = section->buffer;
4709
4710 if (info_ptr == NULL)
4711 return;
4712
4713 /* We can't set abfd until now because the section may be empty or
4714 not present, in which case the bfd is unknown. */
4715 abfd = get_section_bfd_owner (section);
4716
4717 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4718 because we don't need to read any dies: the signature is in the
4719 header. */
4720
4721 end_ptr = info_ptr + section->size;
4722 while (info_ptr < end_ptr)
4723 {
4724 sect_offset offset;
4725 struct signatured_type *sig_type;
4726 struct dwo_unit *dwo_tu;
4727 void **slot;
4728 const gdb_byte *ptr = info_ptr;
4729 struct comp_unit_head header;
4730 unsigned int length;
4731
4732 offset.sect_off = ptr - section->buffer;
4733
4734 /* We need to read the type's signature in order to build the hash
4735 table, but we don't need anything else just yet. */
4736
4737 ptr = read_and_check_comp_unit_head (&header, section,
4738 abbrev_section, ptr, section_kind);
4739
4740 length = get_cu_length (&header);
4741
4742 /* Skip dummy type units. */
4743 if (ptr >= info_ptr + length
4744 || peek_abbrev_code (abfd, ptr) == 0
4745 || header.unit_type != DW_UT_type)
4746 {
4747 info_ptr += length;
4748 continue;
4749 }
4750
4751 if (types_htab == NULL)
4752 {
4753 if (dwo_file)
4754 types_htab = allocate_dwo_unit_table (objfile);
4755 else
4756 types_htab = allocate_signatured_type_table (objfile);
4757 }
4758
4759 if (dwo_file)
4760 {
4761 sig_type = NULL;
4762 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4763 struct dwo_unit);
4764 dwo_tu->dwo_file = dwo_file;
4765 dwo_tu->signature = header.signature;
4766 dwo_tu->type_offset_in_tu = header.type_offset_in_tu;
4767 dwo_tu->section = section;
4768 dwo_tu->offset = offset;
4769 dwo_tu->length = length;
4770 }
4771 else
4772 {
4773 /* N.B.: type_offset is not usable if this type uses a DWO file.
4774 The real type_offset is in the DWO file. */
4775 dwo_tu = NULL;
4776 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4777 struct signatured_type);
4778 sig_type->signature = header.signature;
4779 sig_type->type_offset_in_tu = header.type_offset_in_tu;
4780 sig_type->per_cu.objfile = objfile;
4781 sig_type->per_cu.is_debug_types = 1;
4782 sig_type->per_cu.section = section;
4783 sig_type->per_cu.offset = offset;
4784 sig_type->per_cu.length = length;
4785 }
4786
4787 slot = htab_find_slot (types_htab,
4788 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4789 INSERT);
4790 gdb_assert (slot != NULL);
4791 if (*slot != NULL)
4792 {
4793 sect_offset dup_offset;
4794
4795 if (dwo_file)
4796 {
4797 const struct dwo_unit *dup_tu
4798 = (const struct dwo_unit *) *slot;
4799
4800 dup_offset = dup_tu->offset;
4801 }
4802 else
4803 {
4804 const struct signatured_type *dup_tu
4805 = (const struct signatured_type *) *slot;
4806
4807 dup_offset = dup_tu->per_cu.offset;
4808 }
4809
4810 complaint (&symfile_complaints,
4811 _("debug type entry at offset 0x%x is duplicate to"
4812 " the entry at offset 0x%x, signature %s"),
4813 offset.sect_off, dup_offset.sect_off,
4814 hex_string (header.signature));
4815 }
4816 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
4817
4818 if (dwarf_read_debug > 1)
4819 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
4820 offset.sect_off,
4821 hex_string (header.signature));
4822
4823 info_ptr += length;
4824 }
4825 }
4826
4827 /* Create the hash table of all entries in the .debug_types
4828 (or .debug_types.dwo) section(s).
4829 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
4830 otherwise it is NULL.
4831
4832 The result is a pointer to the hash table or NULL if there are no types.
4833
4834 Note: This function processes DWO files only, not DWP files. */
4835
4836 static void
4837 create_debug_types_hash_table (struct dwo_file *dwo_file,
4838 VEC (dwarf2_section_info_def) *types,
4839 htab_t &types_htab)
4840 {
4841 int ix;
4842 struct dwarf2_section_info *section;
4843
4844 if (VEC_empty (dwarf2_section_info_def, types))
4845 return;
4846
4847 for (ix = 0;
4848 VEC_iterate (dwarf2_section_info_def, types, ix, section);
4849 ++ix)
4850 create_debug_type_hash_table (dwo_file, section, types_htab,
4851 rcuh_kind::TYPE);
4852 }
4853
4854 /* Create the hash table of all entries in the .debug_types section,
4855 and initialize all_type_units.
4856 The result is zero if there is an error (e.g. missing .debug_types section),
4857 otherwise non-zero. */
4858
4859 static int
4860 create_all_type_units (struct objfile *objfile)
4861 {
4862 htab_t types_htab = NULL;
4863 struct signatured_type **iter;
4864
4865 create_debug_type_hash_table (NULL, &dwarf2_per_objfile->info, types_htab,
4866 rcuh_kind::COMPILE);
4867 create_debug_types_hash_table (NULL, dwarf2_per_objfile->types, types_htab);
4868 if (types_htab == NULL)
4869 {
4870 dwarf2_per_objfile->signatured_types = NULL;
4871 return 0;
4872 }
4873
4874 dwarf2_per_objfile->signatured_types = types_htab;
4875
4876 dwarf2_per_objfile->n_type_units
4877 = dwarf2_per_objfile->n_allocated_type_units
4878 = htab_elements (types_htab);
4879 dwarf2_per_objfile->all_type_units =
4880 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
4881 iter = &dwarf2_per_objfile->all_type_units[0];
4882 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4883 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4884 == dwarf2_per_objfile->n_type_units);
4885
4886 return 1;
4887 }
4888
4889 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
4890 If SLOT is non-NULL, it is the entry to use in the hash table.
4891 Otherwise we find one. */
4892
4893 static struct signatured_type *
4894 add_type_unit (ULONGEST sig, void **slot)
4895 {
4896 struct objfile *objfile = dwarf2_per_objfile->objfile;
4897 int n_type_units = dwarf2_per_objfile->n_type_units;
4898 struct signatured_type *sig_type;
4899
4900 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
4901 ++n_type_units;
4902 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
4903 {
4904 if (dwarf2_per_objfile->n_allocated_type_units == 0)
4905 dwarf2_per_objfile->n_allocated_type_units = 1;
4906 dwarf2_per_objfile->n_allocated_type_units *= 2;
4907 dwarf2_per_objfile->all_type_units
4908 = XRESIZEVEC (struct signatured_type *,
4909 dwarf2_per_objfile->all_type_units,
4910 dwarf2_per_objfile->n_allocated_type_units);
4911 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
4912 }
4913 dwarf2_per_objfile->n_type_units = n_type_units;
4914
4915 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4916 struct signatured_type);
4917 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
4918 sig_type->signature = sig;
4919 sig_type->per_cu.is_debug_types = 1;
4920 if (dwarf2_per_objfile->using_index)
4921 {
4922 sig_type->per_cu.v.quick =
4923 OBSTACK_ZALLOC (&objfile->objfile_obstack,
4924 struct dwarf2_per_cu_quick_data);
4925 }
4926
4927 if (slot == NULL)
4928 {
4929 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
4930 sig_type, INSERT);
4931 }
4932 gdb_assert (*slot == NULL);
4933 *slot = sig_type;
4934 /* The rest of sig_type must be filled in by the caller. */
4935 return sig_type;
4936 }
4937
4938 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
4939 Fill in SIG_ENTRY with DWO_ENTRY. */
4940
4941 static void
4942 fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
4943 struct signatured_type *sig_entry,
4944 struct dwo_unit *dwo_entry)
4945 {
4946 /* Make sure we're not clobbering something we don't expect to. */
4947 gdb_assert (! sig_entry->per_cu.queued);
4948 gdb_assert (sig_entry->per_cu.cu == NULL);
4949 if (dwarf2_per_objfile->using_index)
4950 {
4951 gdb_assert (sig_entry->per_cu.v.quick != NULL);
4952 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
4953 }
4954 else
4955 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
4956 gdb_assert (sig_entry->signature == dwo_entry->signature);
4957 gdb_assert (sig_entry->type_offset_in_section.sect_off == 0);
4958 gdb_assert (sig_entry->type_unit_group == NULL);
4959 gdb_assert (sig_entry->dwo_unit == NULL);
4960
4961 sig_entry->per_cu.section = dwo_entry->section;
4962 sig_entry->per_cu.offset = dwo_entry->offset;
4963 sig_entry->per_cu.length = dwo_entry->length;
4964 sig_entry->per_cu.reading_dwo_directly = 1;
4965 sig_entry->per_cu.objfile = objfile;
4966 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
4967 sig_entry->dwo_unit = dwo_entry;
4968 }
4969
4970 /* Subroutine of lookup_signatured_type.
4971 If we haven't read the TU yet, create the signatured_type data structure
4972 for a TU to be read in directly from a DWO file, bypassing the stub.
4973 This is the "Stay in DWO Optimization": When there is no DWP file and we're
4974 using .gdb_index, then when reading a CU we want to stay in the DWO file
4975 containing that CU. Otherwise we could end up reading several other DWO
4976 files (due to comdat folding) to process the transitive closure of all the
4977 mentioned TUs, and that can be slow. The current DWO file will have every
4978 type signature that it needs.
4979 We only do this for .gdb_index because in the psymtab case we already have
4980 to read all the DWOs to build the type unit groups. */
4981
4982 static struct signatured_type *
4983 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
4984 {
4985 struct objfile *objfile = dwarf2_per_objfile->objfile;
4986 struct dwo_file *dwo_file;
4987 struct dwo_unit find_dwo_entry, *dwo_entry;
4988 struct signatured_type find_sig_entry, *sig_entry;
4989 void **slot;
4990
4991 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
4992
4993 /* If TU skeletons have been removed then we may not have read in any
4994 TUs yet. */
4995 if (dwarf2_per_objfile->signatured_types == NULL)
4996 {
4997 dwarf2_per_objfile->signatured_types
4998 = allocate_signatured_type_table (objfile);
4999 }
5000
5001 /* We only ever need to read in one copy of a signatured type.
5002 Use the global signatured_types array to do our own comdat-folding
5003 of types. If this is the first time we're reading this TU, and
5004 the TU has an entry in .gdb_index, replace the recorded data from
5005 .gdb_index with this TU. */
5006
5007 find_sig_entry.signature = sig;
5008 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5009 &find_sig_entry, INSERT);
5010 sig_entry = (struct signatured_type *) *slot;
5011
5012 /* We can get here with the TU already read, *or* in the process of being
5013 read. Don't reassign the global entry to point to this DWO if that's
5014 the case. Also note that if the TU is already being read, it may not
5015 have come from a DWO, the program may be a mix of Fission-compiled
5016 code and non-Fission-compiled code. */
5017
5018 /* Have we already tried to read this TU?
5019 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5020 needn't exist in the global table yet). */
5021 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
5022 return sig_entry;
5023
5024 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
5025 dwo_unit of the TU itself. */
5026 dwo_file = cu->dwo_unit->dwo_file;
5027
5028 /* Ok, this is the first time we're reading this TU. */
5029 if (dwo_file->tus == NULL)
5030 return NULL;
5031 find_dwo_entry.signature = sig;
5032 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
5033 if (dwo_entry == NULL)
5034 return NULL;
5035
5036 /* If the global table doesn't have an entry for this TU, add one. */
5037 if (sig_entry == NULL)
5038 sig_entry = add_type_unit (sig, slot);
5039
5040 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
5041 sig_entry->per_cu.tu_read = 1;
5042 return sig_entry;
5043 }
5044
5045 /* Subroutine of lookup_signatured_type.
5046 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
5047 then try the DWP file. If the TU stub (skeleton) has been removed then
5048 it won't be in .gdb_index. */
5049
5050 static struct signatured_type *
5051 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5052 {
5053 struct objfile *objfile = dwarf2_per_objfile->objfile;
5054 struct dwp_file *dwp_file = get_dwp_file ();
5055 struct dwo_unit *dwo_entry;
5056 struct signatured_type find_sig_entry, *sig_entry;
5057 void **slot;
5058
5059 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
5060 gdb_assert (dwp_file != NULL);
5061
5062 /* If TU skeletons have been removed then we may not have read in any
5063 TUs yet. */
5064 if (dwarf2_per_objfile->signatured_types == NULL)
5065 {
5066 dwarf2_per_objfile->signatured_types
5067 = allocate_signatured_type_table (objfile);
5068 }
5069
5070 find_sig_entry.signature = sig;
5071 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5072 &find_sig_entry, INSERT);
5073 sig_entry = (struct signatured_type *) *slot;
5074
5075 /* Have we already tried to read this TU?
5076 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5077 needn't exist in the global table yet). */
5078 if (sig_entry != NULL)
5079 return sig_entry;
5080
5081 if (dwp_file->tus == NULL)
5082 return NULL;
5083 dwo_entry = lookup_dwo_unit_in_dwp (dwp_file, NULL,
5084 sig, 1 /* is_debug_types */);
5085 if (dwo_entry == NULL)
5086 return NULL;
5087
5088 sig_entry = add_type_unit (sig, slot);
5089 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
5090
5091 return sig_entry;
5092 }
5093
5094 /* Lookup a signature based type for DW_FORM_ref_sig8.
5095 Returns NULL if signature SIG is not present in the table.
5096 It is up to the caller to complain about this. */
5097
5098 static struct signatured_type *
5099 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5100 {
5101 if (cu->dwo_unit
5102 && dwarf2_per_objfile->using_index)
5103 {
5104 /* We're in a DWO/DWP file, and we're using .gdb_index.
5105 These cases require special processing. */
5106 if (get_dwp_file () == NULL)
5107 return lookup_dwo_signatured_type (cu, sig);
5108 else
5109 return lookup_dwp_signatured_type (cu, sig);
5110 }
5111 else
5112 {
5113 struct signatured_type find_entry, *entry;
5114
5115 if (dwarf2_per_objfile->signatured_types == NULL)
5116 return NULL;
5117 find_entry.signature = sig;
5118 entry = ((struct signatured_type *)
5119 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
5120 return entry;
5121 }
5122 }
5123 \f
5124 /* Low level DIE reading support. */
5125
5126 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
5127
5128 static void
5129 init_cu_die_reader (struct die_reader_specs *reader,
5130 struct dwarf2_cu *cu,
5131 struct dwarf2_section_info *section,
5132 struct dwo_file *dwo_file)
5133 {
5134 gdb_assert (section->readin && section->buffer != NULL);
5135 reader->abfd = get_section_bfd_owner (section);
5136 reader->cu = cu;
5137 reader->dwo_file = dwo_file;
5138 reader->die_section = section;
5139 reader->buffer = section->buffer;
5140 reader->buffer_end = section->buffer + section->size;
5141 reader->comp_dir = NULL;
5142 }
5143
5144 /* Subroutine of init_cutu_and_read_dies to simplify it.
5145 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
5146 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
5147 already.
5148
5149 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
5150 from it to the DIE in the DWO. If NULL we are skipping the stub.
5151 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
5152 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
5153 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
5154 STUB_COMP_DIR may be non-NULL.
5155 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
5156 are filled in with the info of the DIE from the DWO file.
5157 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
5158 provided an abbrev table to use.
5159 The result is non-zero if a valid (non-dummy) DIE was found. */
5160
5161 static int
5162 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
5163 struct dwo_unit *dwo_unit,
5164 int abbrev_table_provided,
5165 struct die_info *stub_comp_unit_die,
5166 const char *stub_comp_dir,
5167 struct die_reader_specs *result_reader,
5168 const gdb_byte **result_info_ptr,
5169 struct die_info **result_comp_unit_die,
5170 int *result_has_children)
5171 {
5172 struct objfile *objfile = dwarf2_per_objfile->objfile;
5173 struct dwarf2_cu *cu = this_cu->cu;
5174 struct dwarf2_section_info *section;
5175 bfd *abfd;
5176 const gdb_byte *begin_info_ptr, *info_ptr;
5177 ULONGEST signature; /* Or dwo_id. */
5178 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
5179 int i,num_extra_attrs;
5180 struct dwarf2_section_info *dwo_abbrev_section;
5181 struct attribute *attr;
5182 struct die_info *comp_unit_die;
5183
5184 /* At most one of these may be provided. */
5185 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
5186
5187 /* These attributes aren't processed until later:
5188 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
5189 DW_AT_comp_dir is used now, to find the DWO file, but it is also
5190 referenced later. However, these attributes are found in the stub
5191 which we won't have later. In order to not impose this complication
5192 on the rest of the code, we read them here and copy them to the
5193 DWO CU/TU die. */
5194
5195 stmt_list = NULL;
5196 low_pc = NULL;
5197 high_pc = NULL;
5198 ranges = NULL;
5199 comp_dir = NULL;
5200
5201 if (stub_comp_unit_die != NULL)
5202 {
5203 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
5204 DWO file. */
5205 if (! this_cu->is_debug_types)
5206 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
5207 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
5208 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
5209 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
5210 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
5211
5212 /* There should be a DW_AT_addr_base attribute here (if needed).
5213 We need the value before we can process DW_FORM_GNU_addr_index. */
5214 cu->addr_base = 0;
5215 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
5216 if (attr)
5217 cu->addr_base = DW_UNSND (attr);
5218
5219 /* There should be a DW_AT_ranges_base attribute here (if needed).
5220 We need the value before we can process DW_AT_ranges. */
5221 cu->ranges_base = 0;
5222 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
5223 if (attr)
5224 cu->ranges_base = DW_UNSND (attr);
5225 }
5226 else if (stub_comp_dir != NULL)
5227 {
5228 /* Reconstruct the comp_dir attribute to simplify the code below. */
5229 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
5230 comp_dir->name = DW_AT_comp_dir;
5231 comp_dir->form = DW_FORM_string;
5232 DW_STRING_IS_CANONICAL (comp_dir) = 0;
5233 DW_STRING (comp_dir) = stub_comp_dir;
5234 }
5235
5236 /* Set up for reading the DWO CU/TU. */
5237 cu->dwo_unit = dwo_unit;
5238 section = dwo_unit->section;
5239 dwarf2_read_section (objfile, section);
5240 abfd = get_section_bfd_owner (section);
5241 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
5242 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
5243 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
5244
5245 if (this_cu->is_debug_types)
5246 {
5247 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
5248
5249 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5250 dwo_abbrev_section,
5251 info_ptr, rcuh_kind::TYPE);
5252 /* This is not an assert because it can be caused by bad debug info. */
5253 if (sig_type->signature != cu->header.signature)
5254 {
5255 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
5256 " TU at offset 0x%x [in module %s]"),
5257 hex_string (sig_type->signature),
5258 hex_string (cu->header.signature),
5259 dwo_unit->offset.sect_off,
5260 bfd_get_filename (abfd));
5261 }
5262 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
5263 /* For DWOs coming from DWP files, we don't know the CU length
5264 nor the type's offset in the TU until now. */
5265 dwo_unit->length = get_cu_length (&cu->header);
5266 dwo_unit->type_offset_in_tu = cu->header.type_offset_in_tu;
5267
5268 /* Establish the type offset that can be used to lookup the type.
5269 For DWO files, we don't know it until now. */
5270 sig_type->type_offset_in_section.sect_off =
5271 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
5272 }
5273 else
5274 {
5275 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5276 dwo_abbrev_section,
5277 info_ptr, rcuh_kind::COMPILE);
5278 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
5279 /* For DWOs coming from DWP files, we don't know the CU length
5280 until now. */
5281 dwo_unit->length = get_cu_length (&cu->header);
5282 }
5283
5284 /* Replace the CU's original abbrev table with the DWO's.
5285 Reminder: We can't read the abbrev table until we've read the header. */
5286 if (abbrev_table_provided)
5287 {
5288 /* Don't free the provided abbrev table, the caller of
5289 init_cutu_and_read_dies owns it. */
5290 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
5291 /* Ensure the DWO abbrev table gets freed. */
5292 make_cleanup (dwarf2_free_abbrev_table, cu);
5293 }
5294 else
5295 {
5296 dwarf2_free_abbrev_table (cu);
5297 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
5298 /* Leave any existing abbrev table cleanup as is. */
5299 }
5300
5301 /* Read in the die, but leave space to copy over the attributes
5302 from the stub. This has the benefit of simplifying the rest of
5303 the code - all the work to maintain the illusion of a single
5304 DW_TAG_{compile,type}_unit DIE is done here. */
5305 num_extra_attrs = ((stmt_list != NULL)
5306 + (low_pc != NULL)
5307 + (high_pc != NULL)
5308 + (ranges != NULL)
5309 + (comp_dir != NULL));
5310 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
5311 result_has_children, num_extra_attrs);
5312
5313 /* Copy over the attributes from the stub to the DIE we just read in. */
5314 comp_unit_die = *result_comp_unit_die;
5315 i = comp_unit_die->num_attrs;
5316 if (stmt_list != NULL)
5317 comp_unit_die->attrs[i++] = *stmt_list;
5318 if (low_pc != NULL)
5319 comp_unit_die->attrs[i++] = *low_pc;
5320 if (high_pc != NULL)
5321 comp_unit_die->attrs[i++] = *high_pc;
5322 if (ranges != NULL)
5323 comp_unit_die->attrs[i++] = *ranges;
5324 if (comp_dir != NULL)
5325 comp_unit_die->attrs[i++] = *comp_dir;
5326 comp_unit_die->num_attrs += num_extra_attrs;
5327
5328 if (dwarf_die_debug)
5329 {
5330 fprintf_unfiltered (gdb_stdlog,
5331 "Read die from %s@0x%x of %s:\n",
5332 get_section_name (section),
5333 (unsigned) (begin_info_ptr - section->buffer),
5334 bfd_get_filename (abfd));
5335 dump_die (comp_unit_die, dwarf_die_debug);
5336 }
5337
5338 /* Save the comp_dir attribute. If there is no DWP file then we'll read
5339 TUs by skipping the stub and going directly to the entry in the DWO file.
5340 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
5341 to get it via circuitous means. Blech. */
5342 if (comp_dir != NULL)
5343 result_reader->comp_dir = DW_STRING (comp_dir);
5344
5345 /* Skip dummy compilation units. */
5346 if (info_ptr >= begin_info_ptr + dwo_unit->length
5347 || peek_abbrev_code (abfd, info_ptr) == 0)
5348 return 0;
5349
5350 *result_info_ptr = info_ptr;
5351 return 1;
5352 }
5353
5354 /* Subroutine of init_cutu_and_read_dies to simplify it.
5355 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
5356 Returns NULL if the specified DWO unit cannot be found. */
5357
5358 static struct dwo_unit *
5359 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
5360 struct die_info *comp_unit_die)
5361 {
5362 struct dwarf2_cu *cu = this_cu->cu;
5363 struct attribute *attr;
5364 ULONGEST signature;
5365 struct dwo_unit *dwo_unit;
5366 const char *comp_dir, *dwo_name;
5367
5368 gdb_assert (cu != NULL);
5369
5370 /* Yeah, we look dwo_name up again, but it simplifies the code. */
5371 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5372 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
5373
5374 if (this_cu->is_debug_types)
5375 {
5376 struct signatured_type *sig_type;
5377
5378 /* Since this_cu is the first member of struct signatured_type,
5379 we can go from a pointer to one to a pointer to the other. */
5380 sig_type = (struct signatured_type *) this_cu;
5381 signature = sig_type->signature;
5382 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
5383 }
5384 else
5385 {
5386 struct attribute *attr;
5387
5388 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
5389 if (! attr)
5390 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
5391 " [in module %s]"),
5392 dwo_name, objfile_name (this_cu->objfile));
5393 signature = DW_UNSND (attr);
5394 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
5395 signature);
5396 }
5397
5398 return dwo_unit;
5399 }
5400
5401 /* Subroutine of init_cutu_and_read_dies to simplify it.
5402 See it for a description of the parameters.
5403 Read a TU directly from a DWO file, bypassing the stub.
5404
5405 Note: This function could be a little bit simpler if we shared cleanups
5406 with our caller, init_cutu_and_read_dies. That's generally a fragile thing
5407 to do, so we keep this function self-contained. Or we could move this
5408 into our caller, but it's complex enough already. */
5409
5410 static void
5411 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
5412 int use_existing_cu, int keep,
5413 die_reader_func_ftype *die_reader_func,
5414 void *data)
5415 {
5416 struct dwarf2_cu *cu;
5417 struct signatured_type *sig_type;
5418 struct cleanup *cleanups, *free_cu_cleanup = NULL;
5419 struct die_reader_specs reader;
5420 const gdb_byte *info_ptr;
5421 struct die_info *comp_unit_die;
5422 int has_children;
5423
5424 /* Verify we can do the following downcast, and that we have the
5425 data we need. */
5426 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
5427 sig_type = (struct signatured_type *) this_cu;
5428 gdb_assert (sig_type->dwo_unit != NULL);
5429
5430 cleanups = make_cleanup (null_cleanup, NULL);
5431
5432 if (use_existing_cu && this_cu->cu != NULL)
5433 {
5434 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
5435 cu = this_cu->cu;
5436 /* There's no need to do the rereading_dwo_cu handling that
5437 init_cutu_and_read_dies does since we don't read the stub. */
5438 }
5439 else
5440 {
5441 /* If !use_existing_cu, this_cu->cu must be NULL. */
5442 gdb_assert (this_cu->cu == NULL);
5443 cu = XNEW (struct dwarf2_cu);
5444 init_one_comp_unit (cu, this_cu);
5445 /* If an error occurs while loading, release our storage. */
5446 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
5447 }
5448
5449 /* A future optimization, if needed, would be to use an existing
5450 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
5451 could share abbrev tables. */
5452
5453 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
5454 0 /* abbrev_table_provided */,
5455 NULL /* stub_comp_unit_die */,
5456 sig_type->dwo_unit->dwo_file->comp_dir,
5457 &reader, &info_ptr,
5458 &comp_unit_die, &has_children) == 0)
5459 {
5460 /* Dummy die. */
5461 do_cleanups (cleanups);
5462 return;
5463 }
5464
5465 /* All the "real" work is done here. */
5466 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5467
5468 /* This duplicates the code in init_cutu_and_read_dies,
5469 but the alternative is making the latter more complex.
5470 This function is only for the special case of using DWO files directly:
5471 no point in overly complicating the general case just to handle this. */
5472 if (free_cu_cleanup != NULL)
5473 {
5474 if (keep)
5475 {
5476 /* We've successfully allocated this compilation unit. Let our
5477 caller clean it up when finished with it. */
5478 discard_cleanups (free_cu_cleanup);
5479
5480 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5481 So we have to manually free the abbrev table. */
5482 dwarf2_free_abbrev_table (cu);
5483
5484 /* Link this CU into read_in_chain. */
5485 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5486 dwarf2_per_objfile->read_in_chain = this_cu;
5487 }
5488 else
5489 do_cleanups (free_cu_cleanup);
5490 }
5491
5492 do_cleanups (cleanups);
5493 }
5494
5495 /* Initialize a CU (or TU) and read its DIEs.
5496 If the CU defers to a DWO file, read the DWO file as well.
5497
5498 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
5499 Otherwise the table specified in the comp unit header is read in and used.
5500 This is an optimization for when we already have the abbrev table.
5501
5502 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
5503 Otherwise, a new CU is allocated with xmalloc.
5504
5505 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
5506 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
5507
5508 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
5509 linker) then DIE_READER_FUNC will not get called. */
5510
5511 static void
5512 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
5513 struct abbrev_table *abbrev_table,
5514 int use_existing_cu, int keep,
5515 die_reader_func_ftype *die_reader_func,
5516 void *data)
5517 {
5518 struct objfile *objfile = dwarf2_per_objfile->objfile;
5519 struct dwarf2_section_info *section = this_cu->section;
5520 bfd *abfd = get_section_bfd_owner (section);
5521 struct dwarf2_cu *cu;
5522 const gdb_byte *begin_info_ptr, *info_ptr;
5523 struct die_reader_specs reader;
5524 struct die_info *comp_unit_die;
5525 int has_children;
5526 struct attribute *attr;
5527 struct cleanup *cleanups, *free_cu_cleanup = NULL;
5528 struct signatured_type *sig_type = NULL;
5529 struct dwarf2_section_info *abbrev_section;
5530 /* Non-zero if CU currently points to a DWO file and we need to
5531 reread it. When this happens we need to reread the skeleton die
5532 before we can reread the DWO file (this only applies to CUs, not TUs). */
5533 int rereading_dwo_cu = 0;
5534
5535 if (dwarf_die_debug)
5536 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5537 this_cu->is_debug_types ? "type" : "comp",
5538 this_cu->offset.sect_off);
5539
5540 if (use_existing_cu)
5541 gdb_assert (keep);
5542
5543 /* If we're reading a TU directly from a DWO file, including a virtual DWO
5544 file (instead of going through the stub), short-circuit all of this. */
5545 if (this_cu->reading_dwo_directly)
5546 {
5547 /* Narrow down the scope of possibilities to have to understand. */
5548 gdb_assert (this_cu->is_debug_types);
5549 gdb_assert (abbrev_table == NULL);
5550 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
5551 die_reader_func, data);
5552 return;
5553 }
5554
5555 cleanups = make_cleanup (null_cleanup, NULL);
5556
5557 /* This is cheap if the section is already read in. */
5558 dwarf2_read_section (objfile, section);
5559
5560 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
5561
5562 abbrev_section = get_abbrev_section_for_cu (this_cu);
5563
5564 if (use_existing_cu && this_cu->cu != NULL)
5565 {
5566 cu = this_cu->cu;
5567 /* If this CU is from a DWO file we need to start over, we need to
5568 refetch the attributes from the skeleton CU.
5569 This could be optimized by retrieving those attributes from when we
5570 were here the first time: the previous comp_unit_die was stored in
5571 comp_unit_obstack. But there's no data yet that we need this
5572 optimization. */
5573 if (cu->dwo_unit != NULL)
5574 rereading_dwo_cu = 1;
5575 }
5576 else
5577 {
5578 /* If !use_existing_cu, this_cu->cu must be NULL. */
5579 gdb_assert (this_cu->cu == NULL);
5580 cu = XNEW (struct dwarf2_cu);
5581 init_one_comp_unit (cu, this_cu);
5582 /* If an error occurs while loading, release our storage. */
5583 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
5584 }
5585
5586 /* Get the header. */
5587 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
5588 {
5589 /* We already have the header, there's no need to read it in again. */
5590 info_ptr += cu->header.first_die_offset.cu_off;
5591 }
5592 else
5593 {
5594 if (this_cu->is_debug_types)
5595 {
5596 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5597 abbrev_section, info_ptr,
5598 rcuh_kind::TYPE);
5599
5600 /* Since per_cu is the first member of struct signatured_type,
5601 we can go from a pointer to one to a pointer to the other. */
5602 sig_type = (struct signatured_type *) this_cu;
5603 gdb_assert (sig_type->signature == cu->header.signature);
5604 gdb_assert (sig_type->type_offset_in_tu.cu_off
5605 == cu->header.type_offset_in_tu.cu_off);
5606 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
5607
5608 /* LENGTH has not been set yet for type units if we're
5609 using .gdb_index. */
5610 this_cu->length = get_cu_length (&cu->header);
5611
5612 /* Establish the type offset that can be used to lookup the type. */
5613 sig_type->type_offset_in_section.sect_off =
5614 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
5615
5616 this_cu->dwarf_version = cu->header.version;
5617 }
5618 else
5619 {
5620 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5621 abbrev_section,
5622 info_ptr,
5623 rcuh_kind::COMPILE);
5624
5625 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
5626 gdb_assert (this_cu->length == get_cu_length (&cu->header));
5627 this_cu->dwarf_version = cu->header.version;
5628 }
5629 }
5630
5631 /* Skip dummy compilation units. */
5632 if (info_ptr >= begin_info_ptr + this_cu->length
5633 || peek_abbrev_code (abfd, info_ptr) == 0)
5634 {
5635 do_cleanups (cleanups);
5636 return;
5637 }
5638
5639 /* If we don't have them yet, read the abbrevs for this compilation unit.
5640 And if we need to read them now, make sure they're freed when we're
5641 done. Note that it's important that if the CU had an abbrev table
5642 on entry we don't free it when we're done: Somewhere up the call stack
5643 it may be in use. */
5644 if (abbrev_table != NULL)
5645 {
5646 gdb_assert (cu->abbrev_table == NULL);
5647 gdb_assert (cu->header.abbrev_offset.sect_off
5648 == abbrev_table->offset.sect_off);
5649 cu->abbrev_table = abbrev_table;
5650 }
5651 else if (cu->abbrev_table == NULL)
5652 {
5653 dwarf2_read_abbrevs (cu, abbrev_section);
5654 make_cleanup (dwarf2_free_abbrev_table, cu);
5655 }
5656 else if (rereading_dwo_cu)
5657 {
5658 dwarf2_free_abbrev_table (cu);
5659 dwarf2_read_abbrevs (cu, abbrev_section);
5660 }
5661
5662 /* Read the top level CU/TU die. */
5663 init_cu_die_reader (&reader, cu, section, NULL);
5664 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5665
5666 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
5667 from the DWO file.
5668 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
5669 DWO CU, that this test will fail (the attribute will not be present). */
5670 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5671 if (attr)
5672 {
5673 struct dwo_unit *dwo_unit;
5674 struct die_info *dwo_comp_unit_die;
5675
5676 if (has_children)
5677 {
5678 complaint (&symfile_complaints,
5679 _("compilation unit with DW_AT_GNU_dwo_name"
5680 " has children (offset 0x%x) [in module %s]"),
5681 this_cu->offset.sect_off, bfd_get_filename (abfd));
5682 }
5683 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
5684 if (dwo_unit != NULL)
5685 {
5686 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
5687 abbrev_table != NULL,
5688 comp_unit_die, NULL,
5689 &reader, &info_ptr,
5690 &dwo_comp_unit_die, &has_children) == 0)
5691 {
5692 /* Dummy die. */
5693 do_cleanups (cleanups);
5694 return;
5695 }
5696 comp_unit_die = dwo_comp_unit_die;
5697 }
5698 else
5699 {
5700 /* Yikes, we couldn't find the rest of the DIE, we only have
5701 the stub. A complaint has already been logged. There's
5702 not much more we can do except pass on the stub DIE to
5703 die_reader_func. We don't want to throw an error on bad
5704 debug info. */
5705 }
5706 }
5707
5708 /* All of the above is setup for this call. Yikes. */
5709 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5710
5711 /* Done, clean up. */
5712 if (free_cu_cleanup != NULL)
5713 {
5714 if (keep)
5715 {
5716 /* We've successfully allocated this compilation unit. Let our
5717 caller clean it up when finished with it. */
5718 discard_cleanups (free_cu_cleanup);
5719
5720 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5721 So we have to manually free the abbrev table. */
5722 dwarf2_free_abbrev_table (cu);
5723
5724 /* Link this CU into read_in_chain. */
5725 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5726 dwarf2_per_objfile->read_in_chain = this_cu;
5727 }
5728 else
5729 do_cleanups (free_cu_cleanup);
5730 }
5731
5732 do_cleanups (cleanups);
5733 }
5734
5735 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
5736 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
5737 to have already done the lookup to find the DWO file).
5738
5739 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
5740 THIS_CU->is_debug_types, but nothing else.
5741
5742 We fill in THIS_CU->length.
5743
5744 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
5745 linker) then DIE_READER_FUNC will not get called.
5746
5747 THIS_CU->cu is always freed when done.
5748 This is done in order to not leave THIS_CU->cu in a state where we have
5749 to care whether it refers to the "main" CU or the DWO CU. */
5750
5751 static void
5752 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
5753 struct dwo_file *dwo_file,
5754 die_reader_func_ftype *die_reader_func,
5755 void *data)
5756 {
5757 struct objfile *objfile = dwarf2_per_objfile->objfile;
5758 struct dwarf2_section_info *section = this_cu->section;
5759 bfd *abfd = get_section_bfd_owner (section);
5760 struct dwarf2_section_info *abbrev_section;
5761 struct dwarf2_cu cu;
5762 const gdb_byte *begin_info_ptr, *info_ptr;
5763 struct die_reader_specs reader;
5764 struct cleanup *cleanups;
5765 struct die_info *comp_unit_die;
5766 int has_children;
5767
5768 if (dwarf_die_debug)
5769 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5770 this_cu->is_debug_types ? "type" : "comp",
5771 this_cu->offset.sect_off);
5772
5773 gdb_assert (this_cu->cu == NULL);
5774
5775 abbrev_section = (dwo_file != NULL
5776 ? &dwo_file->sections.abbrev
5777 : get_abbrev_section_for_cu (this_cu));
5778
5779 /* This is cheap if the section is already read in. */
5780 dwarf2_read_section (objfile, section);
5781
5782 init_one_comp_unit (&cu, this_cu);
5783
5784 cleanups = make_cleanup (free_stack_comp_unit, &cu);
5785
5786 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
5787 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
5788 abbrev_section, info_ptr,
5789 (this_cu->is_debug_types
5790 ? rcuh_kind::TYPE
5791 : rcuh_kind::COMPILE));
5792
5793 this_cu->length = get_cu_length (&cu.header);
5794
5795 /* Skip dummy compilation units. */
5796 if (info_ptr >= begin_info_ptr + this_cu->length
5797 || peek_abbrev_code (abfd, info_ptr) == 0)
5798 {
5799 do_cleanups (cleanups);
5800 return;
5801 }
5802
5803 dwarf2_read_abbrevs (&cu, abbrev_section);
5804 make_cleanup (dwarf2_free_abbrev_table, &cu);
5805
5806 init_cu_die_reader (&reader, &cu, section, dwo_file);
5807 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5808
5809 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5810
5811 do_cleanups (cleanups);
5812 }
5813
5814 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
5815 does not lookup the specified DWO file.
5816 This cannot be used to read DWO files.
5817
5818 THIS_CU->cu is always freed when done.
5819 This is done in order to not leave THIS_CU->cu in a state where we have
5820 to care whether it refers to the "main" CU or the DWO CU.
5821 We can revisit this if the data shows there's a performance issue. */
5822
5823 static void
5824 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
5825 die_reader_func_ftype *die_reader_func,
5826 void *data)
5827 {
5828 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
5829 }
5830 \f
5831 /* Type Unit Groups.
5832
5833 Type Unit Groups are a way to collapse the set of all TUs (type units) into
5834 a more manageable set. The grouping is done by DW_AT_stmt_list entry
5835 so that all types coming from the same compilation (.o file) are grouped
5836 together. A future step could be to put the types in the same symtab as
5837 the CU the types ultimately came from. */
5838
5839 static hashval_t
5840 hash_type_unit_group (const void *item)
5841 {
5842 const struct type_unit_group *tu_group
5843 = (const struct type_unit_group *) item;
5844
5845 return hash_stmt_list_entry (&tu_group->hash);
5846 }
5847
5848 static int
5849 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
5850 {
5851 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
5852 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
5853
5854 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
5855 }
5856
5857 /* Allocate a hash table for type unit groups. */
5858
5859 static htab_t
5860 allocate_type_unit_groups_table (void)
5861 {
5862 return htab_create_alloc_ex (3,
5863 hash_type_unit_group,
5864 eq_type_unit_group,
5865 NULL,
5866 &dwarf2_per_objfile->objfile->objfile_obstack,
5867 hashtab_obstack_allocate,
5868 dummy_obstack_deallocate);
5869 }
5870
5871 /* Type units that don't have DW_AT_stmt_list are grouped into their own
5872 partial symtabs. We combine several TUs per psymtab to not let the size
5873 of any one psymtab grow too big. */
5874 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5875 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
5876
5877 /* Helper routine for get_type_unit_group.
5878 Create the type_unit_group object used to hold one or more TUs. */
5879
5880 static struct type_unit_group *
5881 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
5882 {
5883 struct objfile *objfile = dwarf2_per_objfile->objfile;
5884 struct dwarf2_per_cu_data *per_cu;
5885 struct type_unit_group *tu_group;
5886
5887 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5888 struct type_unit_group);
5889 per_cu = &tu_group->per_cu;
5890 per_cu->objfile = objfile;
5891
5892 if (dwarf2_per_objfile->using_index)
5893 {
5894 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5895 struct dwarf2_per_cu_quick_data);
5896 }
5897 else
5898 {
5899 unsigned int line_offset = line_offset_struct.sect_off;
5900 struct partial_symtab *pst;
5901 char *name;
5902
5903 /* Give the symtab a useful name for debug purposes. */
5904 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5905 name = xstrprintf ("<type_units_%d>",
5906 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5907 else
5908 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5909
5910 pst = create_partial_symtab (per_cu, name);
5911 pst->anonymous = 1;
5912
5913 xfree (name);
5914 }
5915
5916 tu_group->hash.dwo_unit = cu->dwo_unit;
5917 tu_group->hash.line_offset = line_offset_struct;
5918
5919 return tu_group;
5920 }
5921
5922 /* Look up the type_unit_group for type unit CU, and create it if necessary.
5923 STMT_LIST is a DW_AT_stmt_list attribute. */
5924
5925 static struct type_unit_group *
5926 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
5927 {
5928 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5929 struct type_unit_group *tu_group;
5930 void **slot;
5931 unsigned int line_offset;
5932 struct type_unit_group type_unit_group_for_lookup;
5933
5934 if (dwarf2_per_objfile->type_unit_groups == NULL)
5935 {
5936 dwarf2_per_objfile->type_unit_groups =
5937 allocate_type_unit_groups_table ();
5938 }
5939
5940 /* Do we need to create a new group, or can we use an existing one? */
5941
5942 if (stmt_list)
5943 {
5944 line_offset = DW_UNSND (stmt_list);
5945 ++tu_stats->nr_symtab_sharers;
5946 }
5947 else
5948 {
5949 /* Ugh, no stmt_list. Rare, but we have to handle it.
5950 We can do various things here like create one group per TU or
5951 spread them over multiple groups to split up the expansion work.
5952 To avoid worst case scenarios (too many groups or too large groups)
5953 we, umm, group them in bunches. */
5954 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5955 | (tu_stats->nr_stmt_less_type_units
5956 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5957 ++tu_stats->nr_stmt_less_type_units;
5958 }
5959
5960 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5961 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
5962 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5963 &type_unit_group_for_lookup, INSERT);
5964 if (*slot != NULL)
5965 {
5966 tu_group = (struct type_unit_group *) *slot;
5967 gdb_assert (tu_group != NULL);
5968 }
5969 else
5970 {
5971 sect_offset line_offset_struct;
5972
5973 line_offset_struct.sect_off = line_offset;
5974 tu_group = create_type_unit_group (cu, line_offset_struct);
5975 *slot = tu_group;
5976 ++tu_stats->nr_symtabs;
5977 }
5978
5979 return tu_group;
5980 }
5981 \f
5982 /* Partial symbol tables. */
5983
5984 /* Create a psymtab named NAME and assign it to PER_CU.
5985
5986 The caller must fill in the following details:
5987 dirname, textlow, texthigh. */
5988
5989 static struct partial_symtab *
5990 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
5991 {
5992 struct objfile *objfile = per_cu->objfile;
5993 struct partial_symtab *pst;
5994
5995 pst = start_psymtab_common (objfile, name, 0,
5996 objfile->global_psymbols.next,
5997 objfile->static_psymbols.next);
5998
5999 pst->psymtabs_addrmap_supported = 1;
6000
6001 /* This is the glue that links PST into GDB's symbol API. */
6002 pst->read_symtab_private = per_cu;
6003 pst->read_symtab = dwarf2_read_symtab;
6004 per_cu->v.psymtab = pst;
6005
6006 return pst;
6007 }
6008
6009 /* The DATA object passed to process_psymtab_comp_unit_reader has this
6010 type. */
6011
6012 struct process_psymtab_comp_unit_data
6013 {
6014 /* True if we are reading a DW_TAG_partial_unit. */
6015
6016 int want_partial_unit;
6017
6018 /* The "pretend" language that is used if the CU doesn't declare a
6019 language. */
6020
6021 enum language pretend_language;
6022 };
6023
6024 /* die_reader_func for process_psymtab_comp_unit. */
6025
6026 static void
6027 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
6028 const gdb_byte *info_ptr,
6029 struct die_info *comp_unit_die,
6030 int has_children,
6031 void *data)
6032 {
6033 struct dwarf2_cu *cu = reader->cu;
6034 struct objfile *objfile = cu->objfile;
6035 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6036 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
6037 CORE_ADDR baseaddr;
6038 CORE_ADDR best_lowpc = 0, best_highpc = 0;
6039 struct partial_symtab *pst;
6040 enum pc_bounds_kind cu_bounds_kind;
6041 const char *filename;
6042 struct process_psymtab_comp_unit_data *info
6043 = (struct process_psymtab_comp_unit_data *) data;
6044
6045 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
6046 return;
6047
6048 gdb_assert (! per_cu->is_debug_types);
6049
6050 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
6051
6052 cu->list_in_scope = &file_symbols;
6053
6054 /* Allocate a new partial symbol table structure. */
6055 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
6056 if (filename == NULL)
6057 filename = "";
6058
6059 pst = create_partial_symtab (per_cu, filename);
6060
6061 /* This must be done before calling dwarf2_build_include_psymtabs. */
6062 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6063
6064 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6065
6066 dwarf2_find_base_address (comp_unit_die, cu);
6067
6068 /* Possibly set the default values of LOWPC and HIGHPC from
6069 `DW_AT_ranges'. */
6070 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
6071 &best_highpc, cu, pst);
6072 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
6073 /* Store the contiguous range if it is not empty; it can be empty for
6074 CUs with no code. */
6075 addrmap_set_empty (objfile->psymtabs_addrmap,
6076 gdbarch_adjust_dwarf2_addr (gdbarch,
6077 best_lowpc + baseaddr),
6078 gdbarch_adjust_dwarf2_addr (gdbarch,
6079 best_highpc + baseaddr) - 1,
6080 pst);
6081
6082 /* Check if comp unit has_children.
6083 If so, read the rest of the partial symbols from this comp unit.
6084 If not, there's no more debug_info for this comp unit. */
6085 if (has_children)
6086 {
6087 struct partial_die_info *first_die;
6088 CORE_ADDR lowpc, highpc;
6089
6090 lowpc = ((CORE_ADDR) -1);
6091 highpc = ((CORE_ADDR) 0);
6092
6093 first_die = load_partial_dies (reader, info_ptr, 1);
6094
6095 scan_partial_symbols (first_die, &lowpc, &highpc,
6096 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
6097
6098 /* If we didn't find a lowpc, set it to highpc to avoid
6099 complaints from `maint check'. */
6100 if (lowpc == ((CORE_ADDR) -1))
6101 lowpc = highpc;
6102
6103 /* If the compilation unit didn't have an explicit address range,
6104 then use the information extracted from its child dies. */
6105 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
6106 {
6107 best_lowpc = lowpc;
6108 best_highpc = highpc;
6109 }
6110 }
6111 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
6112 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
6113
6114 end_psymtab_common (objfile, pst);
6115
6116 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
6117 {
6118 int i;
6119 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6120 struct dwarf2_per_cu_data *iter;
6121
6122 /* Fill in 'dependencies' here; we fill in 'users' in a
6123 post-pass. */
6124 pst->number_of_dependencies = len;
6125 pst->dependencies =
6126 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
6127 for (i = 0;
6128 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
6129 i, iter);
6130 ++i)
6131 pst->dependencies[i] = iter->v.psymtab;
6132
6133 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6134 }
6135
6136 /* Get the list of files included in the current compilation unit,
6137 and build a psymtab for each of them. */
6138 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
6139
6140 if (dwarf_read_debug)
6141 {
6142 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6143
6144 fprintf_unfiltered (gdb_stdlog,
6145 "Psymtab for %s unit @0x%x: %s - %s"
6146 ", %d global, %d static syms\n",
6147 per_cu->is_debug_types ? "type" : "comp",
6148 per_cu->offset.sect_off,
6149 paddress (gdbarch, pst->textlow),
6150 paddress (gdbarch, pst->texthigh),
6151 pst->n_global_syms, pst->n_static_syms);
6152 }
6153 }
6154
6155 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6156 Process compilation unit THIS_CU for a psymtab. */
6157
6158 static void
6159 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
6160 int want_partial_unit,
6161 enum language pretend_language)
6162 {
6163 struct process_psymtab_comp_unit_data info;
6164
6165 /* If this compilation unit was already read in, free the
6166 cached copy in order to read it in again. This is
6167 necessary because we skipped some symbols when we first
6168 read in the compilation unit (see load_partial_dies).
6169 This problem could be avoided, but the benefit is unclear. */
6170 if (this_cu->cu != NULL)
6171 free_one_cached_comp_unit (this_cu);
6172
6173 gdb_assert (! this_cu->is_debug_types);
6174 info.want_partial_unit = want_partial_unit;
6175 info.pretend_language = pretend_language;
6176 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
6177 process_psymtab_comp_unit_reader,
6178 &info);
6179
6180 /* Age out any secondary CUs. */
6181 age_cached_comp_units ();
6182 }
6183
6184 /* Reader function for build_type_psymtabs. */
6185
6186 static void
6187 build_type_psymtabs_reader (const struct die_reader_specs *reader,
6188 const gdb_byte *info_ptr,
6189 struct die_info *type_unit_die,
6190 int has_children,
6191 void *data)
6192 {
6193 struct objfile *objfile = dwarf2_per_objfile->objfile;
6194 struct dwarf2_cu *cu = reader->cu;
6195 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
6196 struct signatured_type *sig_type;
6197 struct type_unit_group *tu_group;
6198 struct attribute *attr;
6199 struct partial_die_info *first_die;
6200 CORE_ADDR lowpc, highpc;
6201 struct partial_symtab *pst;
6202
6203 gdb_assert (data == NULL);
6204 gdb_assert (per_cu->is_debug_types);
6205 sig_type = (struct signatured_type *) per_cu;
6206
6207 if (! has_children)
6208 return;
6209
6210 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
6211 tu_group = get_type_unit_group (cu, attr);
6212
6213 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
6214
6215 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
6216 cu->list_in_scope = &file_symbols;
6217 pst = create_partial_symtab (per_cu, "");
6218 pst->anonymous = 1;
6219
6220 first_die = load_partial_dies (reader, info_ptr, 1);
6221
6222 lowpc = (CORE_ADDR) -1;
6223 highpc = (CORE_ADDR) 0;
6224 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
6225
6226 end_psymtab_common (objfile, pst);
6227 }
6228
6229 /* Struct used to sort TUs by their abbreviation table offset. */
6230
6231 struct tu_abbrev_offset
6232 {
6233 struct signatured_type *sig_type;
6234 sect_offset abbrev_offset;
6235 };
6236
6237 /* Helper routine for build_type_psymtabs_1, passed to qsort. */
6238
6239 static int
6240 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
6241 {
6242 const struct tu_abbrev_offset * const *a
6243 = (const struct tu_abbrev_offset * const*) ap;
6244 const struct tu_abbrev_offset * const *b
6245 = (const struct tu_abbrev_offset * const*) bp;
6246 unsigned int aoff = (*a)->abbrev_offset.sect_off;
6247 unsigned int boff = (*b)->abbrev_offset.sect_off;
6248
6249 return (aoff > boff) - (aoff < boff);
6250 }
6251
6252 /* Efficiently read all the type units.
6253 This does the bulk of the work for build_type_psymtabs.
6254
6255 The efficiency is because we sort TUs by the abbrev table they use and
6256 only read each abbrev table once. In one program there are 200K TUs
6257 sharing 8K abbrev tables.
6258
6259 The main purpose of this function is to support building the
6260 dwarf2_per_objfile->type_unit_groups table.
6261 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
6262 can collapse the search space by grouping them by stmt_list.
6263 The savings can be significant, in the same program from above the 200K TUs
6264 share 8K stmt_list tables.
6265
6266 FUNC is expected to call get_type_unit_group, which will create the
6267 struct type_unit_group if necessary and add it to
6268 dwarf2_per_objfile->type_unit_groups. */
6269
6270 static void
6271 build_type_psymtabs_1 (void)
6272 {
6273 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6274 struct cleanup *cleanups;
6275 struct abbrev_table *abbrev_table;
6276 sect_offset abbrev_offset;
6277 struct tu_abbrev_offset *sorted_by_abbrev;
6278 int i;
6279
6280 /* It's up to the caller to not call us multiple times. */
6281 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
6282
6283 if (dwarf2_per_objfile->n_type_units == 0)
6284 return;
6285
6286 /* TUs typically share abbrev tables, and there can be way more TUs than
6287 abbrev tables. Sort by abbrev table to reduce the number of times we
6288 read each abbrev table in.
6289 Alternatives are to punt or to maintain a cache of abbrev tables.
6290 This is simpler and efficient enough for now.
6291
6292 Later we group TUs by their DW_AT_stmt_list value (as this defines the
6293 symtab to use). Typically TUs with the same abbrev offset have the same
6294 stmt_list value too so in practice this should work well.
6295
6296 The basic algorithm here is:
6297
6298 sort TUs by abbrev table
6299 for each TU with same abbrev table:
6300 read abbrev table if first user
6301 read TU top level DIE
6302 [IWBN if DWO skeletons had DW_AT_stmt_list]
6303 call FUNC */
6304
6305 if (dwarf_read_debug)
6306 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
6307
6308 /* Sort in a separate table to maintain the order of all_type_units
6309 for .gdb_index: TU indices directly index all_type_units. */
6310 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
6311 dwarf2_per_objfile->n_type_units);
6312 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
6313 {
6314 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
6315
6316 sorted_by_abbrev[i].sig_type = sig_type;
6317 sorted_by_abbrev[i].abbrev_offset =
6318 read_abbrev_offset (sig_type->per_cu.section,
6319 sig_type->per_cu.offset);
6320 }
6321 cleanups = make_cleanup (xfree, sorted_by_abbrev);
6322 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
6323 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
6324
6325 abbrev_offset.sect_off = ~(unsigned) 0;
6326 abbrev_table = NULL;
6327 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
6328
6329 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
6330 {
6331 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
6332
6333 /* Switch to the next abbrev table if necessary. */
6334 if (abbrev_table == NULL
6335 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
6336 {
6337 if (abbrev_table != NULL)
6338 {
6339 abbrev_table_free (abbrev_table);
6340 /* Reset to NULL in case abbrev_table_read_table throws
6341 an error: abbrev_table_free_cleanup will get called. */
6342 abbrev_table = NULL;
6343 }
6344 abbrev_offset = tu->abbrev_offset;
6345 abbrev_table =
6346 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
6347 abbrev_offset);
6348 ++tu_stats->nr_uniq_abbrev_tables;
6349 }
6350
6351 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
6352 build_type_psymtabs_reader, NULL);
6353 }
6354
6355 do_cleanups (cleanups);
6356 }
6357
6358 /* Print collected type unit statistics. */
6359
6360 static void
6361 print_tu_stats (void)
6362 {
6363 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6364
6365 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
6366 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
6367 dwarf2_per_objfile->n_type_units);
6368 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
6369 tu_stats->nr_uniq_abbrev_tables);
6370 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
6371 tu_stats->nr_symtabs);
6372 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
6373 tu_stats->nr_symtab_sharers);
6374 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
6375 tu_stats->nr_stmt_less_type_units);
6376 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
6377 tu_stats->nr_all_type_units_reallocs);
6378 }
6379
6380 /* Traversal function for build_type_psymtabs. */
6381
6382 static int
6383 build_type_psymtab_dependencies (void **slot, void *info)
6384 {
6385 struct objfile *objfile = dwarf2_per_objfile->objfile;
6386 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
6387 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
6388 struct partial_symtab *pst = per_cu->v.psymtab;
6389 int len = VEC_length (sig_type_ptr, tu_group->tus);
6390 struct signatured_type *iter;
6391 int i;
6392
6393 gdb_assert (len > 0);
6394 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
6395
6396 pst->number_of_dependencies = len;
6397 pst->dependencies =
6398 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
6399 for (i = 0;
6400 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
6401 ++i)
6402 {
6403 gdb_assert (iter->per_cu.is_debug_types);
6404 pst->dependencies[i] = iter->per_cu.v.psymtab;
6405 iter->type_unit_group = tu_group;
6406 }
6407
6408 VEC_free (sig_type_ptr, tu_group->tus);
6409
6410 return 1;
6411 }
6412
6413 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6414 Build partial symbol tables for the .debug_types comp-units. */
6415
6416 static void
6417 build_type_psymtabs (struct objfile *objfile)
6418 {
6419 if (! create_all_type_units (objfile))
6420 return;
6421
6422 build_type_psymtabs_1 ();
6423 }
6424
6425 /* Traversal function for process_skeletonless_type_unit.
6426 Read a TU in a DWO file and build partial symbols for it. */
6427
6428 static int
6429 process_skeletonless_type_unit (void **slot, void *info)
6430 {
6431 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
6432 struct objfile *objfile = (struct objfile *) info;
6433 struct signatured_type find_entry, *entry;
6434
6435 /* If this TU doesn't exist in the global table, add it and read it in. */
6436
6437 if (dwarf2_per_objfile->signatured_types == NULL)
6438 {
6439 dwarf2_per_objfile->signatured_types
6440 = allocate_signatured_type_table (objfile);
6441 }
6442
6443 find_entry.signature = dwo_unit->signature;
6444 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
6445 INSERT);
6446 /* If we've already seen this type there's nothing to do. What's happening
6447 is we're doing our own version of comdat-folding here. */
6448 if (*slot != NULL)
6449 return 1;
6450
6451 /* This does the job that create_all_type_units would have done for
6452 this TU. */
6453 entry = add_type_unit (dwo_unit->signature, slot);
6454 fill_in_sig_entry_from_dwo_entry (objfile, entry, dwo_unit);
6455 *slot = entry;
6456
6457 /* This does the job that build_type_psymtabs_1 would have done. */
6458 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
6459 build_type_psymtabs_reader, NULL);
6460
6461 return 1;
6462 }
6463
6464 /* Traversal function for process_skeletonless_type_units. */
6465
6466 static int
6467 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
6468 {
6469 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
6470
6471 if (dwo_file->tus != NULL)
6472 {
6473 htab_traverse_noresize (dwo_file->tus,
6474 process_skeletonless_type_unit, info);
6475 }
6476
6477 return 1;
6478 }
6479
6480 /* Scan all TUs of DWO files, verifying we've processed them.
6481 This is needed in case a TU was emitted without its skeleton.
6482 Note: This can't be done until we know what all the DWO files are. */
6483
6484 static void
6485 process_skeletonless_type_units (struct objfile *objfile)
6486 {
6487 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
6488 if (get_dwp_file () == NULL
6489 && dwarf2_per_objfile->dwo_files != NULL)
6490 {
6491 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
6492 process_dwo_file_for_skeletonless_type_units,
6493 objfile);
6494 }
6495 }
6496
6497 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
6498
6499 static void
6500 psymtabs_addrmap_cleanup (void *o)
6501 {
6502 struct objfile *objfile = (struct objfile *) o;
6503
6504 objfile->psymtabs_addrmap = NULL;
6505 }
6506
6507 /* Compute the 'user' field for each psymtab in OBJFILE. */
6508
6509 static void
6510 set_partial_user (struct objfile *objfile)
6511 {
6512 int i;
6513
6514 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
6515 {
6516 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
6517 struct partial_symtab *pst = per_cu->v.psymtab;
6518 int j;
6519
6520 if (pst == NULL)
6521 continue;
6522
6523 for (j = 0; j < pst->number_of_dependencies; ++j)
6524 {
6525 /* Set the 'user' field only if it is not already set. */
6526 if (pst->dependencies[j]->user == NULL)
6527 pst->dependencies[j]->user = pst;
6528 }
6529 }
6530 }
6531
6532 /* Build the partial symbol table by doing a quick pass through the
6533 .debug_info and .debug_abbrev sections. */
6534
6535 static void
6536 dwarf2_build_psymtabs_hard (struct objfile *objfile)
6537 {
6538 struct cleanup *back_to, *addrmap_cleanup;
6539 struct obstack temp_obstack;
6540 int i;
6541
6542 if (dwarf_read_debug)
6543 {
6544 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
6545 objfile_name (objfile));
6546 }
6547
6548 dwarf2_per_objfile->reading_partial_symbols = 1;
6549
6550 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
6551
6552 /* Any cached compilation units will be linked by the per-objfile
6553 read_in_chain. Make sure to free them when we're done. */
6554 back_to = make_cleanup (free_cached_comp_units, NULL);
6555
6556 build_type_psymtabs (objfile);
6557
6558 create_all_comp_units (objfile);
6559
6560 /* Create a temporary address map on a temporary obstack. We later
6561 copy this to the final obstack. */
6562 obstack_init (&temp_obstack);
6563 make_cleanup_obstack_free (&temp_obstack);
6564 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
6565 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
6566
6567 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
6568 {
6569 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
6570
6571 process_psymtab_comp_unit (per_cu, 0, language_minimal);
6572 }
6573
6574 /* This has to wait until we read the CUs, we need the list of DWOs. */
6575 process_skeletonless_type_units (objfile);
6576
6577 /* Now that all TUs have been processed we can fill in the dependencies. */
6578 if (dwarf2_per_objfile->type_unit_groups != NULL)
6579 {
6580 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
6581 build_type_psymtab_dependencies, NULL);
6582 }
6583
6584 if (dwarf_read_debug)
6585 print_tu_stats ();
6586
6587 set_partial_user (objfile);
6588
6589 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
6590 &objfile->objfile_obstack);
6591 discard_cleanups (addrmap_cleanup);
6592
6593 do_cleanups (back_to);
6594
6595 if (dwarf_read_debug)
6596 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
6597 objfile_name (objfile));
6598 }
6599
6600 /* die_reader_func for load_partial_comp_unit. */
6601
6602 static void
6603 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
6604 const gdb_byte *info_ptr,
6605 struct die_info *comp_unit_die,
6606 int has_children,
6607 void *data)
6608 {
6609 struct dwarf2_cu *cu = reader->cu;
6610
6611 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
6612
6613 /* Check if comp unit has_children.
6614 If so, read the rest of the partial symbols from this comp unit.
6615 If not, there's no more debug_info for this comp unit. */
6616 if (has_children)
6617 load_partial_dies (reader, info_ptr, 0);
6618 }
6619
6620 /* Load the partial DIEs for a secondary CU into memory.
6621 This is also used when rereading a primary CU with load_all_dies. */
6622
6623 static void
6624 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
6625 {
6626 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6627 load_partial_comp_unit_reader, NULL);
6628 }
6629
6630 static void
6631 read_comp_units_from_section (struct objfile *objfile,
6632 struct dwarf2_section_info *section,
6633 unsigned int is_dwz,
6634 int *n_allocated,
6635 int *n_comp_units,
6636 struct dwarf2_per_cu_data ***all_comp_units)
6637 {
6638 const gdb_byte *info_ptr;
6639 bfd *abfd = get_section_bfd_owner (section);
6640
6641 if (dwarf_read_debug)
6642 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
6643 get_section_name (section),
6644 get_section_file_name (section));
6645
6646 dwarf2_read_section (objfile, section);
6647
6648 info_ptr = section->buffer;
6649
6650 while (info_ptr < section->buffer + section->size)
6651 {
6652 unsigned int length, initial_length_size;
6653 struct dwarf2_per_cu_data *this_cu;
6654 sect_offset offset;
6655
6656 offset.sect_off = info_ptr - section->buffer;
6657
6658 /* Read just enough information to find out where the next
6659 compilation unit is. */
6660 length = read_initial_length (abfd, info_ptr, &initial_length_size);
6661
6662 /* Save the compilation unit for later lookup. */
6663 this_cu = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_cu_data);
6664 memset (this_cu, 0, sizeof (*this_cu));
6665 this_cu->offset = offset;
6666 this_cu->length = length + initial_length_size;
6667 this_cu->is_dwz = is_dwz;
6668 this_cu->objfile = objfile;
6669 this_cu->section = section;
6670
6671 if (*n_comp_units == *n_allocated)
6672 {
6673 *n_allocated *= 2;
6674 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
6675 *all_comp_units, *n_allocated);
6676 }
6677 (*all_comp_units)[*n_comp_units] = this_cu;
6678 ++*n_comp_units;
6679
6680 info_ptr = info_ptr + this_cu->length;
6681 }
6682 }
6683
6684 /* Create a list of all compilation units in OBJFILE.
6685 This is only done for -readnow and building partial symtabs. */
6686
6687 static void
6688 create_all_comp_units (struct objfile *objfile)
6689 {
6690 int n_allocated;
6691 int n_comp_units;
6692 struct dwarf2_per_cu_data **all_comp_units;
6693 struct dwz_file *dwz;
6694
6695 n_comp_units = 0;
6696 n_allocated = 10;
6697 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
6698
6699 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
6700 &n_allocated, &n_comp_units, &all_comp_units);
6701
6702 dwz = dwarf2_get_dwz_file ();
6703 if (dwz != NULL)
6704 read_comp_units_from_section (objfile, &dwz->info, 1,
6705 &n_allocated, &n_comp_units,
6706 &all_comp_units);
6707
6708 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
6709 struct dwarf2_per_cu_data *,
6710 n_comp_units);
6711 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
6712 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6713 xfree (all_comp_units);
6714 dwarf2_per_objfile->n_comp_units = n_comp_units;
6715 }
6716
6717 /* Process all loaded DIEs for compilation unit CU, starting at
6718 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
6719 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
6720 DW_AT_ranges). See the comments of add_partial_subprogram on how
6721 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
6722
6723 static void
6724 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
6725 CORE_ADDR *highpc, int set_addrmap,
6726 struct dwarf2_cu *cu)
6727 {
6728 struct partial_die_info *pdi;
6729
6730 /* Now, march along the PDI's, descending into ones which have
6731 interesting children but skipping the children of the other ones,
6732 until we reach the end of the compilation unit. */
6733
6734 pdi = first_die;
6735
6736 while (pdi != NULL)
6737 {
6738 fixup_partial_die (pdi, cu);
6739
6740 /* Anonymous namespaces or modules have no name but have interesting
6741 children, so we need to look at them. Ditto for anonymous
6742 enums. */
6743
6744 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
6745 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
6746 || pdi->tag == DW_TAG_imported_unit)
6747 {
6748 switch (pdi->tag)
6749 {
6750 case DW_TAG_subprogram:
6751 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
6752 break;
6753 case DW_TAG_constant:
6754 case DW_TAG_variable:
6755 case DW_TAG_typedef:
6756 case DW_TAG_union_type:
6757 if (!pdi->is_declaration)
6758 {
6759 add_partial_symbol (pdi, cu);
6760 }
6761 break;
6762 case DW_TAG_class_type:
6763 case DW_TAG_interface_type:
6764 case DW_TAG_structure_type:
6765 if (!pdi->is_declaration)
6766 {
6767 add_partial_symbol (pdi, cu);
6768 }
6769 if (cu->language == language_rust && pdi->has_children)
6770 scan_partial_symbols (pdi->die_child, lowpc, highpc,
6771 set_addrmap, cu);
6772 break;
6773 case DW_TAG_enumeration_type:
6774 if (!pdi->is_declaration)
6775 add_partial_enumeration (pdi, cu);
6776 break;
6777 case DW_TAG_base_type:
6778 case DW_TAG_subrange_type:
6779 /* File scope base type definitions are added to the partial
6780 symbol table. */
6781 add_partial_symbol (pdi, cu);
6782 break;
6783 case DW_TAG_namespace:
6784 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
6785 break;
6786 case DW_TAG_module:
6787 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
6788 break;
6789 case DW_TAG_imported_unit:
6790 {
6791 struct dwarf2_per_cu_data *per_cu;
6792
6793 /* For now we don't handle imported units in type units. */
6794 if (cu->per_cu->is_debug_types)
6795 {
6796 error (_("Dwarf Error: DW_TAG_imported_unit is not"
6797 " supported in type units [in module %s]"),
6798 objfile_name (cu->objfile));
6799 }
6800
6801 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
6802 pdi->is_dwz,
6803 cu->objfile);
6804
6805 /* Go read the partial unit, if needed. */
6806 if (per_cu->v.psymtab == NULL)
6807 process_psymtab_comp_unit (per_cu, 1, cu->language);
6808
6809 VEC_safe_push (dwarf2_per_cu_ptr,
6810 cu->per_cu->imported_symtabs, per_cu);
6811 }
6812 break;
6813 case DW_TAG_imported_declaration:
6814 add_partial_symbol (pdi, cu);
6815 break;
6816 default:
6817 break;
6818 }
6819 }
6820
6821 /* If the die has a sibling, skip to the sibling. */
6822
6823 pdi = pdi->die_sibling;
6824 }
6825 }
6826
6827 /* Functions used to compute the fully scoped name of a partial DIE.
6828
6829 Normally, this is simple. For C++, the parent DIE's fully scoped
6830 name is concatenated with "::" and the partial DIE's name.
6831 Enumerators are an exception; they use the scope of their parent
6832 enumeration type, i.e. the name of the enumeration type is not
6833 prepended to the enumerator.
6834
6835 There are two complexities. One is DW_AT_specification; in this
6836 case "parent" means the parent of the target of the specification,
6837 instead of the direct parent of the DIE. The other is compilers
6838 which do not emit DW_TAG_namespace; in this case we try to guess
6839 the fully qualified name of structure types from their members'
6840 linkage names. This must be done using the DIE's children rather
6841 than the children of any DW_AT_specification target. We only need
6842 to do this for structures at the top level, i.e. if the target of
6843 any DW_AT_specification (if any; otherwise the DIE itself) does not
6844 have a parent. */
6845
6846 /* Compute the scope prefix associated with PDI's parent, in
6847 compilation unit CU. The result will be allocated on CU's
6848 comp_unit_obstack, or a copy of the already allocated PDI->NAME
6849 field. NULL is returned if no prefix is necessary. */
6850 static const char *
6851 partial_die_parent_scope (struct partial_die_info *pdi,
6852 struct dwarf2_cu *cu)
6853 {
6854 const char *grandparent_scope;
6855 struct partial_die_info *parent, *real_pdi;
6856
6857 /* We need to look at our parent DIE; if we have a DW_AT_specification,
6858 then this means the parent of the specification DIE. */
6859
6860 real_pdi = pdi;
6861 while (real_pdi->has_specification)
6862 real_pdi = find_partial_die (real_pdi->spec_offset,
6863 real_pdi->spec_is_dwz, cu);
6864
6865 parent = real_pdi->die_parent;
6866 if (parent == NULL)
6867 return NULL;
6868
6869 if (parent->scope_set)
6870 return parent->scope;
6871
6872 fixup_partial_die (parent, cu);
6873
6874 grandparent_scope = partial_die_parent_scope (parent, cu);
6875
6876 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
6877 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
6878 Work around this problem here. */
6879 if (cu->language == language_cplus
6880 && parent->tag == DW_TAG_namespace
6881 && strcmp (parent->name, "::") == 0
6882 && grandparent_scope == NULL)
6883 {
6884 parent->scope = NULL;
6885 parent->scope_set = 1;
6886 return NULL;
6887 }
6888
6889 if (pdi->tag == DW_TAG_enumerator)
6890 /* Enumerators should not get the name of the enumeration as a prefix. */
6891 parent->scope = grandparent_scope;
6892 else if (parent->tag == DW_TAG_namespace
6893 || parent->tag == DW_TAG_module
6894 || parent->tag == DW_TAG_structure_type
6895 || parent->tag == DW_TAG_class_type
6896 || parent->tag == DW_TAG_interface_type
6897 || parent->tag == DW_TAG_union_type
6898 || parent->tag == DW_TAG_enumeration_type)
6899 {
6900 if (grandparent_scope == NULL)
6901 parent->scope = parent->name;
6902 else
6903 parent->scope = typename_concat (&cu->comp_unit_obstack,
6904 grandparent_scope,
6905 parent->name, 0, cu);
6906 }
6907 else
6908 {
6909 /* FIXME drow/2004-04-01: What should we be doing with
6910 function-local names? For partial symbols, we should probably be
6911 ignoring them. */
6912 complaint (&symfile_complaints,
6913 _("unhandled containing DIE tag %d for DIE at %d"),
6914 parent->tag, pdi->offset.sect_off);
6915 parent->scope = grandparent_scope;
6916 }
6917
6918 parent->scope_set = 1;
6919 return parent->scope;
6920 }
6921
6922 /* Return the fully scoped name associated with PDI, from compilation unit
6923 CU. The result will be allocated with malloc. */
6924
6925 static char *
6926 partial_die_full_name (struct partial_die_info *pdi,
6927 struct dwarf2_cu *cu)
6928 {
6929 const char *parent_scope;
6930
6931 /* If this is a template instantiation, we can not work out the
6932 template arguments from partial DIEs. So, unfortunately, we have
6933 to go through the full DIEs. At least any work we do building
6934 types here will be reused if full symbols are loaded later. */
6935 if (pdi->has_template_arguments)
6936 {
6937 fixup_partial_die (pdi, cu);
6938
6939 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
6940 {
6941 struct die_info *die;
6942 struct attribute attr;
6943 struct dwarf2_cu *ref_cu = cu;
6944
6945 /* DW_FORM_ref_addr is using section offset. */
6946 attr.name = (enum dwarf_attribute) 0;
6947 attr.form = DW_FORM_ref_addr;
6948 attr.u.unsnd = pdi->offset.sect_off;
6949 die = follow_die_ref (NULL, &attr, &ref_cu);
6950
6951 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
6952 }
6953 }
6954
6955 parent_scope = partial_die_parent_scope (pdi, cu);
6956 if (parent_scope == NULL)
6957 return NULL;
6958 else
6959 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
6960 }
6961
6962 static void
6963 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
6964 {
6965 struct objfile *objfile = cu->objfile;
6966 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6967 CORE_ADDR addr = 0;
6968 const char *actual_name = NULL;
6969 CORE_ADDR baseaddr;
6970 char *built_actual_name;
6971
6972 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6973
6974 built_actual_name = partial_die_full_name (pdi, cu);
6975 if (built_actual_name != NULL)
6976 actual_name = built_actual_name;
6977
6978 if (actual_name == NULL)
6979 actual_name = pdi->name;
6980
6981 switch (pdi->tag)
6982 {
6983 case DW_TAG_subprogram:
6984 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
6985 if (pdi->is_external || cu->language == language_ada)
6986 {
6987 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
6988 of the global scope. But in Ada, we want to be able to access
6989 nested procedures globally. So all Ada subprograms are stored
6990 in the global scope. */
6991 add_psymbol_to_list (actual_name, strlen (actual_name),
6992 built_actual_name != NULL,
6993 VAR_DOMAIN, LOC_BLOCK,
6994 &objfile->global_psymbols,
6995 addr, cu->language, objfile);
6996 }
6997 else
6998 {
6999 add_psymbol_to_list (actual_name, strlen (actual_name),
7000 built_actual_name != NULL,
7001 VAR_DOMAIN, LOC_BLOCK,
7002 &objfile->static_psymbols,
7003 addr, cu->language, objfile);
7004 }
7005
7006 if (pdi->main_subprogram && actual_name != NULL)
7007 set_objfile_main_name (objfile, actual_name, cu->language);
7008 break;
7009 case DW_TAG_constant:
7010 {
7011 struct psymbol_allocation_list *list;
7012
7013 if (pdi->is_external)
7014 list = &objfile->global_psymbols;
7015 else
7016 list = &objfile->static_psymbols;
7017 add_psymbol_to_list (actual_name, strlen (actual_name),
7018 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
7019 list, 0, cu->language, objfile);
7020 }
7021 break;
7022 case DW_TAG_variable:
7023 if (pdi->d.locdesc)
7024 addr = decode_locdesc (pdi->d.locdesc, cu);
7025
7026 if (pdi->d.locdesc
7027 && addr == 0
7028 && !dwarf2_per_objfile->has_section_at_zero)
7029 {
7030 /* A global or static variable may also have been stripped
7031 out by the linker if unused, in which case its address
7032 will be nullified; do not add such variables into partial
7033 symbol table then. */
7034 }
7035 else if (pdi->is_external)
7036 {
7037 /* Global Variable.
7038 Don't enter into the minimal symbol tables as there is
7039 a minimal symbol table entry from the ELF symbols already.
7040 Enter into partial symbol table if it has a location
7041 descriptor or a type.
7042 If the location descriptor is missing, new_symbol will create
7043 a LOC_UNRESOLVED symbol, the address of the variable will then
7044 be determined from the minimal symbol table whenever the variable
7045 is referenced.
7046 The address for the partial symbol table entry is not
7047 used by GDB, but it comes in handy for debugging partial symbol
7048 table building. */
7049
7050 if (pdi->d.locdesc || pdi->has_type)
7051 add_psymbol_to_list (actual_name, strlen (actual_name),
7052 built_actual_name != NULL,
7053 VAR_DOMAIN, LOC_STATIC,
7054 &objfile->global_psymbols,
7055 addr + baseaddr,
7056 cu->language, objfile);
7057 }
7058 else
7059 {
7060 int has_loc = pdi->d.locdesc != NULL;
7061
7062 /* Static Variable. Skip symbols whose value we cannot know (those
7063 without location descriptors or constant values). */
7064 if (!has_loc && !pdi->has_const_value)
7065 {
7066 xfree (built_actual_name);
7067 return;
7068 }
7069
7070 add_psymbol_to_list (actual_name, strlen (actual_name),
7071 built_actual_name != NULL,
7072 VAR_DOMAIN, LOC_STATIC,
7073 &objfile->static_psymbols,
7074 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
7075 cu->language, objfile);
7076 }
7077 break;
7078 case DW_TAG_typedef:
7079 case DW_TAG_base_type:
7080 case DW_TAG_subrange_type:
7081 add_psymbol_to_list (actual_name, strlen (actual_name),
7082 built_actual_name != NULL,
7083 VAR_DOMAIN, LOC_TYPEDEF,
7084 &objfile->static_psymbols,
7085 0, cu->language, objfile);
7086 break;
7087 case DW_TAG_imported_declaration:
7088 case DW_TAG_namespace:
7089 add_psymbol_to_list (actual_name, strlen (actual_name),
7090 built_actual_name != NULL,
7091 VAR_DOMAIN, LOC_TYPEDEF,
7092 &objfile->global_psymbols,
7093 0, cu->language, objfile);
7094 break;
7095 case DW_TAG_module:
7096 add_psymbol_to_list (actual_name, strlen (actual_name),
7097 built_actual_name != NULL,
7098 MODULE_DOMAIN, LOC_TYPEDEF,
7099 &objfile->global_psymbols,
7100 0, cu->language, objfile);
7101 break;
7102 case DW_TAG_class_type:
7103 case DW_TAG_interface_type:
7104 case DW_TAG_structure_type:
7105 case DW_TAG_union_type:
7106 case DW_TAG_enumeration_type:
7107 /* Skip external references. The DWARF standard says in the section
7108 about "Structure, Union, and Class Type Entries": "An incomplete
7109 structure, union or class type is represented by a structure,
7110 union or class entry that does not have a byte size attribute
7111 and that has a DW_AT_declaration attribute." */
7112 if (!pdi->has_byte_size && pdi->is_declaration)
7113 {
7114 xfree (built_actual_name);
7115 return;
7116 }
7117
7118 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
7119 static vs. global. */
7120 add_psymbol_to_list (actual_name, strlen (actual_name),
7121 built_actual_name != NULL,
7122 STRUCT_DOMAIN, LOC_TYPEDEF,
7123 cu->language == language_cplus
7124 ? &objfile->global_psymbols
7125 : &objfile->static_psymbols,
7126 0, cu->language, objfile);
7127
7128 break;
7129 case DW_TAG_enumerator:
7130 add_psymbol_to_list (actual_name, strlen (actual_name),
7131 built_actual_name != NULL,
7132 VAR_DOMAIN, LOC_CONST,
7133 cu->language == language_cplus
7134 ? &objfile->global_psymbols
7135 : &objfile->static_psymbols,
7136 0, cu->language, objfile);
7137 break;
7138 default:
7139 break;
7140 }
7141
7142 xfree (built_actual_name);
7143 }
7144
7145 /* Read a partial die corresponding to a namespace; also, add a symbol
7146 corresponding to that namespace to the symbol table. NAMESPACE is
7147 the name of the enclosing namespace. */
7148
7149 static void
7150 add_partial_namespace (struct partial_die_info *pdi,
7151 CORE_ADDR *lowpc, CORE_ADDR *highpc,
7152 int set_addrmap, struct dwarf2_cu *cu)
7153 {
7154 /* Add a symbol for the namespace. */
7155
7156 add_partial_symbol (pdi, cu);
7157
7158 /* Now scan partial symbols in that namespace. */
7159
7160 if (pdi->has_children)
7161 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
7162 }
7163
7164 /* Read a partial die corresponding to a Fortran module. */
7165
7166 static void
7167 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
7168 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
7169 {
7170 /* Add a symbol for the namespace. */
7171
7172 add_partial_symbol (pdi, cu);
7173
7174 /* Now scan partial symbols in that module. */
7175
7176 if (pdi->has_children)
7177 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
7178 }
7179
7180 /* Read a partial die corresponding to a subprogram and create a partial
7181 symbol for that subprogram. When the CU language allows it, this
7182 routine also defines a partial symbol for each nested subprogram
7183 that this subprogram contains. If SET_ADDRMAP is true, record the
7184 covered ranges in the addrmap. Set *LOWPC and *HIGHPC to the lowest
7185 and highest PC values found in PDI.
7186
7187 PDI may also be a lexical block, in which case we simply search
7188 recursively for subprograms defined inside that lexical block.
7189 Again, this is only performed when the CU language allows this
7190 type of definitions. */
7191
7192 static void
7193 add_partial_subprogram (struct partial_die_info *pdi,
7194 CORE_ADDR *lowpc, CORE_ADDR *highpc,
7195 int set_addrmap, struct dwarf2_cu *cu)
7196 {
7197 if (pdi->tag == DW_TAG_subprogram)
7198 {
7199 if (pdi->has_pc_info)
7200 {
7201 if (pdi->lowpc < *lowpc)
7202 *lowpc = pdi->lowpc;
7203 if (pdi->highpc > *highpc)
7204 *highpc = pdi->highpc;
7205 if (set_addrmap)
7206 {
7207 struct objfile *objfile = cu->objfile;
7208 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7209 CORE_ADDR baseaddr;
7210 CORE_ADDR highpc;
7211 CORE_ADDR lowpc;
7212
7213 baseaddr = ANOFFSET (objfile->section_offsets,
7214 SECT_OFF_TEXT (objfile));
7215 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
7216 pdi->lowpc + baseaddr);
7217 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
7218 pdi->highpc + baseaddr);
7219 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
7220 cu->per_cu->v.psymtab);
7221 }
7222 }
7223
7224 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
7225 {
7226 if (!pdi->is_declaration)
7227 /* Ignore subprogram DIEs that do not have a name, they are
7228 illegal. Do not emit a complaint at this point, we will
7229 do so when we convert this psymtab into a symtab. */
7230 if (pdi->name)
7231 add_partial_symbol (pdi, cu);
7232 }
7233 }
7234
7235 if (! pdi->has_children)
7236 return;
7237
7238 if (cu->language == language_ada)
7239 {
7240 pdi = pdi->die_child;
7241 while (pdi != NULL)
7242 {
7243 fixup_partial_die (pdi, cu);
7244 if (pdi->tag == DW_TAG_subprogram
7245 || pdi->tag == DW_TAG_lexical_block)
7246 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7247 pdi = pdi->die_sibling;
7248 }
7249 }
7250 }
7251
7252 /* Read a partial die corresponding to an enumeration type. */
7253
7254 static void
7255 add_partial_enumeration (struct partial_die_info *enum_pdi,
7256 struct dwarf2_cu *cu)
7257 {
7258 struct partial_die_info *pdi;
7259
7260 if (enum_pdi->name != NULL)
7261 add_partial_symbol (enum_pdi, cu);
7262
7263 pdi = enum_pdi->die_child;
7264 while (pdi)
7265 {
7266 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
7267 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
7268 else
7269 add_partial_symbol (pdi, cu);
7270 pdi = pdi->die_sibling;
7271 }
7272 }
7273
7274 /* Return the initial uleb128 in the die at INFO_PTR. */
7275
7276 static unsigned int
7277 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
7278 {
7279 unsigned int bytes_read;
7280
7281 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7282 }
7283
7284 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
7285 Return the corresponding abbrev, or NULL if the number is zero (indicating
7286 an empty DIE). In either case *BYTES_READ will be set to the length of
7287 the initial number. */
7288
7289 static struct abbrev_info *
7290 peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
7291 struct dwarf2_cu *cu)
7292 {
7293 bfd *abfd = cu->objfile->obfd;
7294 unsigned int abbrev_number;
7295 struct abbrev_info *abbrev;
7296
7297 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
7298
7299 if (abbrev_number == 0)
7300 return NULL;
7301
7302 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
7303 if (!abbrev)
7304 {
7305 error (_("Dwarf Error: Could not find abbrev number %d in %s"
7306 " at offset 0x%x [in module %s]"),
7307 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
7308 cu->header.offset.sect_off, bfd_get_filename (abfd));
7309 }
7310
7311 return abbrev;
7312 }
7313
7314 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7315 Returns a pointer to the end of a series of DIEs, terminated by an empty
7316 DIE. Any children of the skipped DIEs will also be skipped. */
7317
7318 static const gdb_byte *
7319 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
7320 {
7321 struct dwarf2_cu *cu = reader->cu;
7322 struct abbrev_info *abbrev;
7323 unsigned int bytes_read;
7324
7325 while (1)
7326 {
7327 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
7328 if (abbrev == NULL)
7329 return info_ptr + bytes_read;
7330 else
7331 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
7332 }
7333 }
7334
7335 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7336 INFO_PTR should point just after the initial uleb128 of a DIE, and the
7337 abbrev corresponding to that skipped uleb128 should be passed in
7338 ABBREV. Returns a pointer to this DIE's sibling, skipping any
7339 children. */
7340
7341 static const gdb_byte *
7342 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
7343 struct abbrev_info *abbrev)
7344 {
7345 unsigned int bytes_read;
7346 struct attribute attr;
7347 bfd *abfd = reader->abfd;
7348 struct dwarf2_cu *cu = reader->cu;
7349 const gdb_byte *buffer = reader->buffer;
7350 const gdb_byte *buffer_end = reader->buffer_end;
7351 unsigned int form, i;
7352
7353 for (i = 0; i < abbrev->num_attrs; i++)
7354 {
7355 /* The only abbrev we care about is DW_AT_sibling. */
7356 if (abbrev->attrs[i].name == DW_AT_sibling)
7357 {
7358 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
7359 if (attr.form == DW_FORM_ref_addr)
7360 complaint (&symfile_complaints,
7361 _("ignoring absolute DW_AT_sibling"));
7362 else
7363 {
7364 unsigned int off = dwarf2_get_ref_die_offset (&attr).sect_off;
7365 const gdb_byte *sibling_ptr = buffer + off;
7366
7367 if (sibling_ptr < info_ptr)
7368 complaint (&symfile_complaints,
7369 _("DW_AT_sibling points backwards"));
7370 else if (sibling_ptr > reader->buffer_end)
7371 dwarf2_section_buffer_overflow_complaint (reader->die_section);
7372 else
7373 return sibling_ptr;
7374 }
7375 }
7376
7377 /* If it isn't DW_AT_sibling, skip this attribute. */
7378 form = abbrev->attrs[i].form;
7379 skip_attribute:
7380 switch (form)
7381 {
7382 case DW_FORM_ref_addr:
7383 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
7384 and later it is offset sized. */
7385 if (cu->header.version == 2)
7386 info_ptr += cu->header.addr_size;
7387 else
7388 info_ptr += cu->header.offset_size;
7389 break;
7390 case DW_FORM_GNU_ref_alt:
7391 info_ptr += cu->header.offset_size;
7392 break;
7393 case DW_FORM_addr:
7394 info_ptr += cu->header.addr_size;
7395 break;
7396 case DW_FORM_data1:
7397 case DW_FORM_ref1:
7398 case DW_FORM_flag:
7399 info_ptr += 1;
7400 break;
7401 case DW_FORM_flag_present:
7402 case DW_FORM_implicit_const:
7403 break;
7404 case DW_FORM_data2:
7405 case DW_FORM_ref2:
7406 info_ptr += 2;
7407 break;
7408 case DW_FORM_data4:
7409 case DW_FORM_ref4:
7410 info_ptr += 4;
7411 break;
7412 case DW_FORM_data8:
7413 case DW_FORM_ref8:
7414 case DW_FORM_ref_sig8:
7415 info_ptr += 8;
7416 break;
7417 case DW_FORM_string:
7418 read_direct_string (abfd, info_ptr, &bytes_read);
7419 info_ptr += bytes_read;
7420 break;
7421 case DW_FORM_sec_offset:
7422 case DW_FORM_strp:
7423 case DW_FORM_GNU_strp_alt:
7424 info_ptr += cu->header.offset_size;
7425 break;
7426 case DW_FORM_exprloc:
7427 case DW_FORM_block:
7428 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7429 info_ptr += bytes_read;
7430 break;
7431 case DW_FORM_block1:
7432 info_ptr += 1 + read_1_byte (abfd, info_ptr);
7433 break;
7434 case DW_FORM_block2:
7435 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
7436 break;
7437 case DW_FORM_block4:
7438 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
7439 break;
7440 case DW_FORM_sdata:
7441 case DW_FORM_udata:
7442 case DW_FORM_ref_udata:
7443 case DW_FORM_GNU_addr_index:
7444 case DW_FORM_GNU_str_index:
7445 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
7446 break;
7447 case DW_FORM_indirect:
7448 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7449 info_ptr += bytes_read;
7450 /* We need to continue parsing from here, so just go back to
7451 the top. */
7452 goto skip_attribute;
7453
7454 default:
7455 error (_("Dwarf Error: Cannot handle %s "
7456 "in DWARF reader [in module %s]"),
7457 dwarf_form_name (form),
7458 bfd_get_filename (abfd));
7459 }
7460 }
7461
7462 if (abbrev->has_children)
7463 return skip_children (reader, info_ptr);
7464 else
7465 return info_ptr;
7466 }
7467
7468 /* Locate ORIG_PDI's sibling.
7469 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
7470
7471 static const gdb_byte *
7472 locate_pdi_sibling (const struct die_reader_specs *reader,
7473 struct partial_die_info *orig_pdi,
7474 const gdb_byte *info_ptr)
7475 {
7476 /* Do we know the sibling already? */
7477
7478 if (orig_pdi->sibling)
7479 return orig_pdi->sibling;
7480
7481 /* Are there any children to deal with? */
7482
7483 if (!orig_pdi->has_children)
7484 return info_ptr;
7485
7486 /* Skip the children the long way. */
7487
7488 return skip_children (reader, info_ptr);
7489 }
7490
7491 /* Expand this partial symbol table into a full symbol table. SELF is
7492 not NULL. */
7493
7494 static void
7495 dwarf2_read_symtab (struct partial_symtab *self,
7496 struct objfile *objfile)
7497 {
7498 if (self->readin)
7499 {
7500 warning (_("bug: psymtab for %s is already read in."),
7501 self->filename);
7502 }
7503 else
7504 {
7505 if (info_verbose)
7506 {
7507 printf_filtered (_("Reading in symbols for %s..."),
7508 self->filename);
7509 gdb_flush (gdb_stdout);
7510 }
7511
7512 /* Restore our global data. */
7513 dwarf2_per_objfile
7514 = (struct dwarf2_per_objfile *) objfile_data (objfile,
7515 dwarf2_objfile_data_key);
7516
7517 /* If this psymtab is constructed from a debug-only objfile, the
7518 has_section_at_zero flag will not necessarily be correct. We
7519 can get the correct value for this flag by looking at the data
7520 associated with the (presumably stripped) associated objfile. */
7521 if (objfile->separate_debug_objfile_backlink)
7522 {
7523 struct dwarf2_per_objfile *dpo_backlink
7524 = ((struct dwarf2_per_objfile *)
7525 objfile_data (objfile->separate_debug_objfile_backlink,
7526 dwarf2_objfile_data_key));
7527
7528 dwarf2_per_objfile->has_section_at_zero
7529 = dpo_backlink->has_section_at_zero;
7530 }
7531
7532 dwarf2_per_objfile->reading_partial_symbols = 0;
7533
7534 psymtab_to_symtab_1 (self);
7535
7536 /* Finish up the debug error message. */
7537 if (info_verbose)
7538 printf_filtered (_("done.\n"));
7539 }
7540
7541 process_cu_includes ();
7542 }
7543 \f
7544 /* Reading in full CUs. */
7545
7546 /* Add PER_CU to the queue. */
7547
7548 static void
7549 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
7550 enum language pretend_language)
7551 {
7552 struct dwarf2_queue_item *item;
7553
7554 per_cu->queued = 1;
7555 item = XNEW (struct dwarf2_queue_item);
7556 item->per_cu = per_cu;
7557 item->pretend_language = pretend_language;
7558 item->next = NULL;
7559
7560 if (dwarf2_queue == NULL)
7561 dwarf2_queue = item;
7562 else
7563 dwarf2_queue_tail->next = item;
7564
7565 dwarf2_queue_tail = item;
7566 }
7567
7568 /* If PER_CU is not yet queued, add it to the queue.
7569 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
7570 dependency.
7571 The result is non-zero if PER_CU was queued, otherwise the result is zero
7572 meaning either PER_CU is already queued or it is already loaded.
7573
7574 N.B. There is an invariant here that if a CU is queued then it is loaded.
7575 The caller is required to load PER_CU if we return non-zero. */
7576
7577 static int
7578 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
7579 struct dwarf2_per_cu_data *per_cu,
7580 enum language pretend_language)
7581 {
7582 /* We may arrive here during partial symbol reading, if we need full
7583 DIEs to process an unusual case (e.g. template arguments). Do
7584 not queue PER_CU, just tell our caller to load its DIEs. */
7585 if (dwarf2_per_objfile->reading_partial_symbols)
7586 {
7587 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
7588 return 1;
7589 return 0;
7590 }
7591
7592 /* Mark the dependence relation so that we don't flush PER_CU
7593 too early. */
7594 if (dependent_cu != NULL)
7595 dwarf2_add_dependence (dependent_cu, per_cu);
7596
7597 /* If it's already on the queue, we have nothing to do. */
7598 if (per_cu->queued)
7599 return 0;
7600
7601 /* If the compilation unit is already loaded, just mark it as
7602 used. */
7603 if (per_cu->cu != NULL)
7604 {
7605 per_cu->cu->last_used = 0;
7606 return 0;
7607 }
7608
7609 /* Add it to the queue. */
7610 queue_comp_unit (per_cu, pretend_language);
7611
7612 return 1;
7613 }
7614
7615 /* Process the queue. */
7616
7617 static void
7618 process_queue (void)
7619 {
7620 struct dwarf2_queue_item *item, *next_item;
7621
7622 if (dwarf_read_debug)
7623 {
7624 fprintf_unfiltered (gdb_stdlog,
7625 "Expanding one or more symtabs of objfile %s ...\n",
7626 objfile_name (dwarf2_per_objfile->objfile));
7627 }
7628
7629 /* The queue starts out with one item, but following a DIE reference
7630 may load a new CU, adding it to the end of the queue. */
7631 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
7632 {
7633 if ((dwarf2_per_objfile->using_index
7634 ? !item->per_cu->v.quick->compunit_symtab
7635 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
7636 /* Skip dummy CUs. */
7637 && item->per_cu->cu != NULL)
7638 {
7639 struct dwarf2_per_cu_data *per_cu = item->per_cu;
7640 unsigned int debug_print_threshold;
7641 char buf[100];
7642
7643 if (per_cu->is_debug_types)
7644 {
7645 struct signatured_type *sig_type =
7646 (struct signatured_type *) per_cu;
7647
7648 sprintf (buf, "TU %s at offset 0x%x",
7649 hex_string (sig_type->signature),
7650 per_cu->offset.sect_off);
7651 /* There can be 100s of TUs.
7652 Only print them in verbose mode. */
7653 debug_print_threshold = 2;
7654 }
7655 else
7656 {
7657 sprintf (buf, "CU at offset 0x%x", per_cu->offset.sect_off);
7658 debug_print_threshold = 1;
7659 }
7660
7661 if (dwarf_read_debug >= debug_print_threshold)
7662 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
7663
7664 if (per_cu->is_debug_types)
7665 process_full_type_unit (per_cu, item->pretend_language);
7666 else
7667 process_full_comp_unit (per_cu, item->pretend_language);
7668
7669 if (dwarf_read_debug >= debug_print_threshold)
7670 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
7671 }
7672
7673 item->per_cu->queued = 0;
7674 next_item = item->next;
7675 xfree (item);
7676 }
7677
7678 dwarf2_queue_tail = NULL;
7679
7680 if (dwarf_read_debug)
7681 {
7682 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
7683 objfile_name (dwarf2_per_objfile->objfile));
7684 }
7685 }
7686
7687 /* Free all allocated queue entries. This function only releases anything if
7688 an error was thrown; if the queue was processed then it would have been
7689 freed as we went along. */
7690
7691 static void
7692 dwarf2_release_queue (void *dummy)
7693 {
7694 struct dwarf2_queue_item *item, *last;
7695
7696 item = dwarf2_queue;
7697 while (item)
7698 {
7699 /* Anything still marked queued is likely to be in an
7700 inconsistent state, so discard it. */
7701 if (item->per_cu->queued)
7702 {
7703 if (item->per_cu->cu != NULL)
7704 free_one_cached_comp_unit (item->per_cu);
7705 item->per_cu->queued = 0;
7706 }
7707
7708 last = item;
7709 item = item->next;
7710 xfree (last);
7711 }
7712
7713 dwarf2_queue = dwarf2_queue_tail = NULL;
7714 }
7715
7716 /* Read in full symbols for PST, and anything it depends on. */
7717
7718 static void
7719 psymtab_to_symtab_1 (struct partial_symtab *pst)
7720 {
7721 struct dwarf2_per_cu_data *per_cu;
7722 int i;
7723
7724 if (pst->readin)
7725 return;
7726
7727 for (i = 0; i < pst->number_of_dependencies; i++)
7728 if (!pst->dependencies[i]->readin
7729 && pst->dependencies[i]->user == NULL)
7730 {
7731 /* Inform about additional files that need to be read in. */
7732 if (info_verbose)
7733 {
7734 /* FIXME: i18n: Need to make this a single string. */
7735 fputs_filtered (" ", gdb_stdout);
7736 wrap_here ("");
7737 fputs_filtered ("and ", gdb_stdout);
7738 wrap_here ("");
7739 printf_filtered ("%s...", pst->dependencies[i]->filename);
7740 wrap_here (""); /* Flush output. */
7741 gdb_flush (gdb_stdout);
7742 }
7743 psymtab_to_symtab_1 (pst->dependencies[i]);
7744 }
7745
7746 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
7747
7748 if (per_cu == NULL)
7749 {
7750 /* It's an include file, no symbols to read for it.
7751 Everything is in the parent symtab. */
7752 pst->readin = 1;
7753 return;
7754 }
7755
7756 dw2_do_instantiate_symtab (per_cu);
7757 }
7758
7759 /* Trivial hash function for die_info: the hash value of a DIE
7760 is its offset in .debug_info for this objfile. */
7761
7762 static hashval_t
7763 die_hash (const void *item)
7764 {
7765 const struct die_info *die = (const struct die_info *) item;
7766
7767 return die->offset.sect_off;
7768 }
7769
7770 /* Trivial comparison function for die_info structures: two DIEs
7771 are equal if they have the same offset. */
7772
7773 static int
7774 die_eq (const void *item_lhs, const void *item_rhs)
7775 {
7776 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
7777 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
7778
7779 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
7780 }
7781
7782 /* die_reader_func for load_full_comp_unit.
7783 This is identical to read_signatured_type_reader,
7784 but is kept separate for now. */
7785
7786 static void
7787 load_full_comp_unit_reader (const struct die_reader_specs *reader,
7788 const gdb_byte *info_ptr,
7789 struct die_info *comp_unit_die,
7790 int has_children,
7791 void *data)
7792 {
7793 struct dwarf2_cu *cu = reader->cu;
7794 enum language *language_ptr = (enum language *) data;
7795
7796 gdb_assert (cu->die_hash == NULL);
7797 cu->die_hash =
7798 htab_create_alloc_ex (cu->header.length / 12,
7799 die_hash,
7800 die_eq,
7801 NULL,
7802 &cu->comp_unit_obstack,
7803 hashtab_obstack_allocate,
7804 dummy_obstack_deallocate);
7805
7806 if (has_children)
7807 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
7808 &info_ptr, comp_unit_die);
7809 cu->dies = comp_unit_die;
7810 /* comp_unit_die is not stored in die_hash, no need. */
7811
7812 /* We try not to read any attributes in this function, because not
7813 all CUs needed for references have been loaded yet, and symbol
7814 table processing isn't initialized. But we have to set the CU language,
7815 or we won't be able to build types correctly.
7816 Similarly, if we do not read the producer, we can not apply
7817 producer-specific interpretation. */
7818 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
7819 }
7820
7821 /* Load the DIEs associated with PER_CU into memory. */
7822
7823 static void
7824 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
7825 enum language pretend_language)
7826 {
7827 gdb_assert (! this_cu->is_debug_types);
7828
7829 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
7830 load_full_comp_unit_reader, &pretend_language);
7831 }
7832
7833 /* Add a DIE to the delayed physname list. */
7834
7835 static void
7836 add_to_method_list (struct type *type, int fnfield_index, int index,
7837 const char *name, struct die_info *die,
7838 struct dwarf2_cu *cu)
7839 {
7840 struct delayed_method_info mi;
7841 mi.type = type;
7842 mi.fnfield_index = fnfield_index;
7843 mi.index = index;
7844 mi.name = name;
7845 mi.die = die;
7846 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
7847 }
7848
7849 /* A cleanup for freeing the delayed method list. */
7850
7851 static void
7852 free_delayed_list (void *ptr)
7853 {
7854 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
7855 if (cu->method_list != NULL)
7856 {
7857 VEC_free (delayed_method_info, cu->method_list);
7858 cu->method_list = NULL;
7859 }
7860 }
7861
7862 /* Compute the physnames of any methods on the CU's method list.
7863
7864 The computation of method physnames is delayed in order to avoid the
7865 (bad) condition that one of the method's formal parameters is of an as yet
7866 incomplete type. */
7867
7868 static void
7869 compute_delayed_physnames (struct dwarf2_cu *cu)
7870 {
7871 int i;
7872 struct delayed_method_info *mi;
7873 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
7874 {
7875 const char *physname;
7876 struct fn_fieldlist *fn_flp
7877 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7878 physname = dwarf2_physname (mi->name, mi->die, cu);
7879 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi->index)
7880 = physname ? physname : "";
7881 }
7882 }
7883
7884 /* Go objects should be embedded in a DW_TAG_module DIE,
7885 and it's not clear if/how imported objects will appear.
7886 To keep Go support simple until that's worked out,
7887 go back through what we've read and create something usable.
7888 We could do this while processing each DIE, and feels kinda cleaner,
7889 but that way is more invasive.
7890 This is to, for example, allow the user to type "p var" or "b main"
7891 without having to specify the package name, and allow lookups
7892 of module.object to work in contexts that use the expression
7893 parser. */
7894
7895 static void
7896 fixup_go_packaging (struct dwarf2_cu *cu)
7897 {
7898 char *package_name = NULL;
7899 struct pending *list;
7900 int i;
7901
7902 for (list = global_symbols; list != NULL; list = list->next)
7903 {
7904 for (i = 0; i < list->nsyms; ++i)
7905 {
7906 struct symbol *sym = list->symbol[i];
7907
7908 if (SYMBOL_LANGUAGE (sym) == language_go
7909 && SYMBOL_CLASS (sym) == LOC_BLOCK)
7910 {
7911 char *this_package_name = go_symbol_package_name (sym);
7912
7913 if (this_package_name == NULL)
7914 continue;
7915 if (package_name == NULL)
7916 package_name = this_package_name;
7917 else
7918 {
7919 if (strcmp (package_name, this_package_name) != 0)
7920 complaint (&symfile_complaints,
7921 _("Symtab %s has objects from two different Go packages: %s and %s"),
7922 (symbol_symtab (sym) != NULL
7923 ? symtab_to_filename_for_display
7924 (symbol_symtab (sym))
7925 : objfile_name (cu->objfile)),
7926 this_package_name, package_name);
7927 xfree (this_package_name);
7928 }
7929 }
7930 }
7931 }
7932
7933 if (package_name != NULL)
7934 {
7935 struct objfile *objfile = cu->objfile;
7936 const char *saved_package_name
7937 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
7938 package_name,
7939 strlen (package_name));
7940 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
7941 saved_package_name);
7942 struct symbol *sym;
7943
7944 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7945
7946 sym = allocate_symbol (objfile);
7947 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
7948 SYMBOL_SET_NAMES (sym, saved_package_name,
7949 strlen (saved_package_name), 0, objfile);
7950 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
7951 e.g., "main" finds the "main" module and not C's main(). */
7952 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
7953 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
7954 SYMBOL_TYPE (sym) = type;
7955
7956 add_symbol_to_list (sym, &global_symbols);
7957
7958 xfree (package_name);
7959 }
7960 }
7961
7962 /* Return the symtab for PER_CU. This works properly regardless of
7963 whether we're using the index or psymtabs. */
7964
7965 static struct compunit_symtab *
7966 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
7967 {
7968 return (dwarf2_per_objfile->using_index
7969 ? per_cu->v.quick->compunit_symtab
7970 : per_cu->v.psymtab->compunit_symtab);
7971 }
7972
7973 /* A helper function for computing the list of all symbol tables
7974 included by PER_CU. */
7975
7976 static void
7977 recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
7978 htab_t all_children, htab_t all_type_symtabs,
7979 struct dwarf2_per_cu_data *per_cu,
7980 struct compunit_symtab *immediate_parent)
7981 {
7982 void **slot;
7983 int ix;
7984 struct compunit_symtab *cust;
7985 struct dwarf2_per_cu_data *iter;
7986
7987 slot = htab_find_slot (all_children, per_cu, INSERT);
7988 if (*slot != NULL)
7989 {
7990 /* This inclusion and its children have been processed. */
7991 return;
7992 }
7993
7994 *slot = per_cu;
7995 /* Only add a CU if it has a symbol table. */
7996 cust = get_compunit_symtab (per_cu);
7997 if (cust != NULL)
7998 {
7999 /* If this is a type unit only add its symbol table if we haven't
8000 seen it yet (type unit per_cu's can share symtabs). */
8001 if (per_cu->is_debug_types)
8002 {
8003 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
8004 if (*slot == NULL)
8005 {
8006 *slot = cust;
8007 VEC_safe_push (compunit_symtab_ptr, *result, cust);
8008 if (cust->user == NULL)
8009 cust->user = immediate_parent;
8010 }
8011 }
8012 else
8013 {
8014 VEC_safe_push (compunit_symtab_ptr, *result, cust);
8015 if (cust->user == NULL)
8016 cust->user = immediate_parent;
8017 }
8018 }
8019
8020 for (ix = 0;
8021 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
8022 ++ix)
8023 {
8024 recursively_compute_inclusions (result, all_children,
8025 all_type_symtabs, iter, cust);
8026 }
8027 }
8028
8029 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
8030 PER_CU. */
8031
8032 static void
8033 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
8034 {
8035 gdb_assert (! per_cu->is_debug_types);
8036
8037 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
8038 {
8039 int ix, len;
8040 struct dwarf2_per_cu_data *per_cu_iter;
8041 struct compunit_symtab *compunit_symtab_iter;
8042 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
8043 htab_t all_children, all_type_symtabs;
8044 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
8045
8046 /* If we don't have a symtab, we can just skip this case. */
8047 if (cust == NULL)
8048 return;
8049
8050 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
8051 NULL, xcalloc, xfree);
8052 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
8053 NULL, xcalloc, xfree);
8054
8055 for (ix = 0;
8056 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
8057 ix, per_cu_iter);
8058 ++ix)
8059 {
8060 recursively_compute_inclusions (&result_symtabs, all_children,
8061 all_type_symtabs, per_cu_iter,
8062 cust);
8063 }
8064
8065 /* Now we have a transitive closure of all the included symtabs. */
8066 len = VEC_length (compunit_symtab_ptr, result_symtabs);
8067 cust->includes
8068 = XOBNEWVEC (&dwarf2_per_objfile->objfile->objfile_obstack,
8069 struct compunit_symtab *, len + 1);
8070 for (ix = 0;
8071 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
8072 compunit_symtab_iter);
8073 ++ix)
8074 cust->includes[ix] = compunit_symtab_iter;
8075 cust->includes[len] = NULL;
8076
8077 VEC_free (compunit_symtab_ptr, result_symtabs);
8078 htab_delete (all_children);
8079 htab_delete (all_type_symtabs);
8080 }
8081 }
8082
8083 /* Compute the 'includes' field for the symtabs of all the CUs we just
8084 read. */
8085
8086 static void
8087 process_cu_includes (void)
8088 {
8089 int ix;
8090 struct dwarf2_per_cu_data *iter;
8091
8092 for (ix = 0;
8093 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
8094 ix, iter);
8095 ++ix)
8096 {
8097 if (! iter->is_debug_types)
8098 compute_compunit_symtab_includes (iter);
8099 }
8100
8101 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
8102 }
8103
8104 /* Generate full symbol information for PER_CU, whose DIEs have
8105 already been loaded into memory. */
8106
8107 static void
8108 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
8109 enum language pretend_language)
8110 {
8111 struct dwarf2_cu *cu = per_cu->cu;
8112 struct objfile *objfile = per_cu->objfile;
8113 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8114 CORE_ADDR lowpc, highpc;
8115 struct compunit_symtab *cust;
8116 struct cleanup *back_to, *delayed_list_cleanup;
8117 CORE_ADDR baseaddr;
8118 struct block *static_block;
8119 CORE_ADDR addr;
8120
8121 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8122
8123 buildsym_init ();
8124 back_to = make_cleanup (really_free_pendings, NULL);
8125 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
8126
8127 cu->list_in_scope = &file_symbols;
8128
8129 cu->language = pretend_language;
8130 cu->language_defn = language_def (cu->language);
8131
8132 /* Do line number decoding in read_file_scope () */
8133 process_die (cu->dies, cu);
8134
8135 /* For now fudge the Go package. */
8136 if (cu->language == language_go)
8137 fixup_go_packaging (cu);
8138
8139 /* Now that we have processed all the DIEs in the CU, all the types
8140 should be complete, and it should now be safe to compute all of the
8141 physnames. */
8142 compute_delayed_physnames (cu);
8143 do_cleanups (delayed_list_cleanup);
8144
8145 /* Some compilers don't define a DW_AT_high_pc attribute for the
8146 compilation unit. If the DW_AT_high_pc is missing, synthesize
8147 it, by scanning the DIE's below the compilation unit. */
8148 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
8149
8150 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
8151 static_block = end_symtab_get_static_block (addr, 0, 1);
8152
8153 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
8154 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
8155 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
8156 addrmap to help ensure it has an accurate map of pc values belonging to
8157 this comp unit. */
8158 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
8159
8160 cust = end_symtab_from_static_block (static_block,
8161 SECT_OFF_TEXT (objfile), 0);
8162
8163 if (cust != NULL)
8164 {
8165 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
8166
8167 /* Set symtab language to language from DW_AT_language. If the
8168 compilation is from a C file generated by language preprocessors, do
8169 not set the language if it was already deduced by start_subfile. */
8170 if (!(cu->language == language_c
8171 && COMPUNIT_FILETABS (cust)->language != language_unknown))
8172 COMPUNIT_FILETABS (cust)->language = cu->language;
8173
8174 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
8175 produce DW_AT_location with location lists but it can be possibly
8176 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
8177 there were bugs in prologue debug info, fixed later in GCC-4.5
8178 by "unwind info for epilogues" patch (which is not directly related).
8179
8180 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
8181 needed, it would be wrong due to missing DW_AT_producer there.
8182
8183 Still one can confuse GDB by using non-standard GCC compilation
8184 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
8185 */
8186 if (cu->has_loclist && gcc_4_minor >= 5)
8187 cust->locations_valid = 1;
8188
8189 if (gcc_4_minor >= 5)
8190 cust->epilogue_unwind_valid = 1;
8191
8192 cust->call_site_htab = cu->call_site_htab;
8193 }
8194
8195 if (dwarf2_per_objfile->using_index)
8196 per_cu->v.quick->compunit_symtab = cust;
8197 else
8198 {
8199 struct partial_symtab *pst = per_cu->v.psymtab;
8200 pst->compunit_symtab = cust;
8201 pst->readin = 1;
8202 }
8203
8204 /* Push it for inclusion processing later. */
8205 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
8206
8207 do_cleanups (back_to);
8208 }
8209
8210 /* Generate full symbol information for type unit PER_CU, whose DIEs have
8211 already been loaded into memory. */
8212
8213 static void
8214 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
8215 enum language pretend_language)
8216 {
8217 struct dwarf2_cu *cu = per_cu->cu;
8218 struct objfile *objfile = per_cu->objfile;
8219 struct compunit_symtab *cust;
8220 struct cleanup *back_to, *delayed_list_cleanup;
8221 struct signatured_type *sig_type;
8222
8223 gdb_assert (per_cu->is_debug_types);
8224 sig_type = (struct signatured_type *) per_cu;
8225
8226 buildsym_init ();
8227 back_to = make_cleanup (really_free_pendings, NULL);
8228 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
8229
8230 cu->list_in_scope = &file_symbols;
8231
8232 cu->language = pretend_language;
8233 cu->language_defn = language_def (cu->language);
8234
8235 /* The symbol tables are set up in read_type_unit_scope. */
8236 process_die (cu->dies, cu);
8237
8238 /* For now fudge the Go package. */
8239 if (cu->language == language_go)
8240 fixup_go_packaging (cu);
8241
8242 /* Now that we have processed all the DIEs in the CU, all the types
8243 should be complete, and it should now be safe to compute all of the
8244 physnames. */
8245 compute_delayed_physnames (cu);
8246 do_cleanups (delayed_list_cleanup);
8247
8248 /* TUs share symbol tables.
8249 If this is the first TU to use this symtab, complete the construction
8250 of it with end_expandable_symtab. Otherwise, complete the addition of
8251 this TU's symbols to the existing symtab. */
8252 if (sig_type->type_unit_group->compunit_symtab == NULL)
8253 {
8254 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8255 sig_type->type_unit_group->compunit_symtab = cust;
8256
8257 if (cust != NULL)
8258 {
8259 /* Set symtab language to language from DW_AT_language. If the
8260 compilation is from a C file generated by language preprocessors,
8261 do not set the language if it was already deduced by
8262 start_subfile. */
8263 if (!(cu->language == language_c
8264 && COMPUNIT_FILETABS (cust)->language != language_c))
8265 COMPUNIT_FILETABS (cust)->language = cu->language;
8266 }
8267 }
8268 else
8269 {
8270 augment_type_symtab ();
8271 cust = sig_type->type_unit_group->compunit_symtab;
8272 }
8273
8274 if (dwarf2_per_objfile->using_index)
8275 per_cu->v.quick->compunit_symtab = cust;
8276 else
8277 {
8278 struct partial_symtab *pst = per_cu->v.psymtab;
8279 pst->compunit_symtab = cust;
8280 pst->readin = 1;
8281 }
8282
8283 do_cleanups (back_to);
8284 }
8285
8286 /* Process an imported unit DIE. */
8287
8288 static void
8289 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
8290 {
8291 struct attribute *attr;
8292
8293 /* For now we don't handle imported units in type units. */
8294 if (cu->per_cu->is_debug_types)
8295 {
8296 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8297 " supported in type units [in module %s]"),
8298 objfile_name (cu->objfile));
8299 }
8300
8301 attr = dwarf2_attr (die, DW_AT_import, cu);
8302 if (attr != NULL)
8303 {
8304 struct dwarf2_per_cu_data *per_cu;
8305 sect_offset offset;
8306 int is_dwz;
8307
8308 offset = dwarf2_get_ref_die_offset (attr);
8309 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
8310 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
8311
8312 /* If necessary, add it to the queue and load its DIEs. */
8313 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
8314 load_full_comp_unit (per_cu, cu->language);
8315
8316 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8317 per_cu);
8318 }
8319 }
8320
8321 /* Reset the in_process bit of a die. */
8322
8323 static void
8324 reset_die_in_process (void *arg)
8325 {
8326 struct die_info *die = (struct die_info *) arg;
8327
8328 die->in_process = 0;
8329 }
8330
8331 /* Process a die and its children. */
8332
8333 static void
8334 process_die (struct die_info *die, struct dwarf2_cu *cu)
8335 {
8336 struct cleanup *in_process;
8337
8338 /* We should only be processing those not already in process. */
8339 gdb_assert (!die->in_process);
8340
8341 die->in_process = 1;
8342 in_process = make_cleanup (reset_die_in_process,die);
8343
8344 switch (die->tag)
8345 {
8346 case DW_TAG_padding:
8347 break;
8348 case DW_TAG_compile_unit:
8349 case DW_TAG_partial_unit:
8350 read_file_scope (die, cu);
8351 break;
8352 case DW_TAG_type_unit:
8353 read_type_unit_scope (die, cu);
8354 break;
8355 case DW_TAG_subprogram:
8356 case DW_TAG_inlined_subroutine:
8357 read_func_scope (die, cu);
8358 break;
8359 case DW_TAG_lexical_block:
8360 case DW_TAG_try_block:
8361 case DW_TAG_catch_block:
8362 read_lexical_block_scope (die, cu);
8363 break;
8364 case DW_TAG_call_site:
8365 case DW_TAG_GNU_call_site:
8366 read_call_site_scope (die, cu);
8367 break;
8368 case DW_TAG_class_type:
8369 case DW_TAG_interface_type:
8370 case DW_TAG_structure_type:
8371 case DW_TAG_union_type:
8372 process_structure_scope (die, cu);
8373 break;
8374 case DW_TAG_enumeration_type:
8375 process_enumeration_scope (die, cu);
8376 break;
8377
8378 /* These dies have a type, but processing them does not create
8379 a symbol or recurse to process the children. Therefore we can
8380 read them on-demand through read_type_die. */
8381 case DW_TAG_subroutine_type:
8382 case DW_TAG_set_type:
8383 case DW_TAG_array_type:
8384 case DW_TAG_pointer_type:
8385 case DW_TAG_ptr_to_member_type:
8386 case DW_TAG_reference_type:
8387 case DW_TAG_string_type:
8388 break;
8389
8390 case DW_TAG_base_type:
8391 case DW_TAG_subrange_type:
8392 case DW_TAG_typedef:
8393 /* Add a typedef symbol for the type definition, if it has a
8394 DW_AT_name. */
8395 new_symbol (die, read_type_die (die, cu), cu);
8396 break;
8397 case DW_TAG_common_block:
8398 read_common_block (die, cu);
8399 break;
8400 case DW_TAG_common_inclusion:
8401 break;
8402 case DW_TAG_namespace:
8403 cu->processing_has_namespace_info = 1;
8404 read_namespace (die, cu);
8405 break;
8406 case DW_TAG_module:
8407 cu->processing_has_namespace_info = 1;
8408 read_module (die, cu);
8409 break;
8410 case DW_TAG_imported_declaration:
8411 cu->processing_has_namespace_info = 1;
8412 if (read_namespace_alias (die, cu))
8413 break;
8414 /* The declaration is not a global namespace alias: fall through. */
8415 case DW_TAG_imported_module:
8416 cu->processing_has_namespace_info = 1;
8417 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
8418 || cu->language != language_fortran))
8419 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
8420 dwarf_tag_name (die->tag));
8421 read_import_statement (die, cu);
8422 break;
8423
8424 case DW_TAG_imported_unit:
8425 process_imported_unit_die (die, cu);
8426 break;
8427
8428 default:
8429 new_symbol (die, NULL, cu);
8430 break;
8431 }
8432
8433 do_cleanups (in_process);
8434 }
8435 \f
8436 /* DWARF name computation. */
8437
8438 /* A helper function for dwarf2_compute_name which determines whether DIE
8439 needs to have the name of the scope prepended to the name listed in the
8440 die. */
8441
8442 static int
8443 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
8444 {
8445 struct attribute *attr;
8446
8447 switch (die->tag)
8448 {
8449 case DW_TAG_namespace:
8450 case DW_TAG_typedef:
8451 case DW_TAG_class_type:
8452 case DW_TAG_interface_type:
8453 case DW_TAG_structure_type:
8454 case DW_TAG_union_type:
8455 case DW_TAG_enumeration_type:
8456 case DW_TAG_enumerator:
8457 case DW_TAG_subprogram:
8458 case DW_TAG_inlined_subroutine:
8459 case DW_TAG_member:
8460 case DW_TAG_imported_declaration:
8461 return 1;
8462
8463 case DW_TAG_variable:
8464 case DW_TAG_constant:
8465 /* We only need to prefix "globally" visible variables. These include
8466 any variable marked with DW_AT_external or any variable that
8467 lives in a namespace. [Variables in anonymous namespaces
8468 require prefixing, but they are not DW_AT_external.] */
8469
8470 if (dwarf2_attr (die, DW_AT_specification, cu))
8471 {
8472 struct dwarf2_cu *spec_cu = cu;
8473
8474 return die_needs_namespace (die_specification (die, &spec_cu),
8475 spec_cu);
8476 }
8477
8478 attr = dwarf2_attr (die, DW_AT_external, cu);
8479 if (attr == NULL && die->parent->tag != DW_TAG_namespace
8480 && die->parent->tag != DW_TAG_module)
8481 return 0;
8482 /* A variable in a lexical block of some kind does not need a
8483 namespace, even though in C++ such variables may be external
8484 and have a mangled name. */
8485 if (die->parent->tag == DW_TAG_lexical_block
8486 || die->parent->tag == DW_TAG_try_block
8487 || die->parent->tag == DW_TAG_catch_block
8488 || die->parent->tag == DW_TAG_subprogram)
8489 return 0;
8490 return 1;
8491
8492 default:
8493 return 0;
8494 }
8495 }
8496
8497 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
8498 compute the physname for the object, which include a method's:
8499 - formal parameters (C++),
8500 - receiver type (Go),
8501
8502 The term "physname" is a bit confusing.
8503 For C++, for example, it is the demangled name.
8504 For Go, for example, it's the mangled name.
8505
8506 For Ada, return the DIE's linkage name rather than the fully qualified
8507 name. PHYSNAME is ignored..
8508
8509 The result is allocated on the objfile_obstack and canonicalized. */
8510
8511 static const char *
8512 dwarf2_compute_name (const char *name,
8513 struct die_info *die, struct dwarf2_cu *cu,
8514 int physname)
8515 {
8516 struct objfile *objfile = cu->objfile;
8517
8518 if (name == NULL)
8519 name = dwarf2_name (die, cu);
8520
8521 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
8522 but otherwise compute it by typename_concat inside GDB.
8523 FIXME: Actually this is not really true, or at least not always true.
8524 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
8525 Fortran names because there is no mangling standard. So new_symbol_full
8526 will set the demangled name to the result of dwarf2_full_name, and it is
8527 the demangled name that GDB uses if it exists. */
8528 if (cu->language == language_ada
8529 || (cu->language == language_fortran && physname))
8530 {
8531 /* For Ada unit, we prefer the linkage name over the name, as
8532 the former contains the exported name, which the user expects
8533 to be able to reference. Ideally, we want the user to be able
8534 to reference this entity using either natural or linkage name,
8535 but we haven't started looking at this enhancement yet. */
8536 const char *linkage_name;
8537
8538 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
8539 if (linkage_name == NULL)
8540 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
8541 if (linkage_name != NULL)
8542 return linkage_name;
8543 }
8544
8545 /* These are the only languages we know how to qualify names in. */
8546 if (name != NULL
8547 && (cu->language == language_cplus
8548 || cu->language == language_fortran || cu->language == language_d
8549 || cu->language == language_rust))
8550 {
8551 if (die_needs_namespace (die, cu))
8552 {
8553 long length;
8554 const char *prefix;
8555 const char *canonical_name = NULL;
8556
8557 string_file buf;
8558
8559 prefix = determine_prefix (die, cu);
8560 if (*prefix != '\0')
8561 {
8562 char *prefixed_name = typename_concat (NULL, prefix, name,
8563 physname, cu);
8564
8565 buf.puts (prefixed_name);
8566 xfree (prefixed_name);
8567 }
8568 else
8569 buf.puts (name);
8570
8571 /* Template parameters may be specified in the DIE's DW_AT_name, or
8572 as children with DW_TAG_template_type_param or
8573 DW_TAG_value_type_param. If the latter, add them to the name
8574 here. If the name already has template parameters, then
8575 skip this step; some versions of GCC emit both, and
8576 it is more efficient to use the pre-computed name.
8577
8578 Something to keep in mind about this process: it is very
8579 unlikely, or in some cases downright impossible, to produce
8580 something that will match the mangled name of a function.
8581 If the definition of the function has the same debug info,
8582 we should be able to match up with it anyway. But fallbacks
8583 using the minimal symbol, for instance to find a method
8584 implemented in a stripped copy of libstdc++, will not work.
8585 If we do not have debug info for the definition, we will have to
8586 match them up some other way.
8587
8588 When we do name matching there is a related problem with function
8589 templates; two instantiated function templates are allowed to
8590 differ only by their return types, which we do not add here. */
8591
8592 if (cu->language == language_cplus && strchr (name, '<') == NULL)
8593 {
8594 struct attribute *attr;
8595 struct die_info *child;
8596 int first = 1;
8597
8598 die->building_fullname = 1;
8599
8600 for (child = die->child; child != NULL; child = child->sibling)
8601 {
8602 struct type *type;
8603 LONGEST value;
8604 const gdb_byte *bytes;
8605 struct dwarf2_locexpr_baton *baton;
8606 struct value *v;
8607
8608 if (child->tag != DW_TAG_template_type_param
8609 && child->tag != DW_TAG_template_value_param)
8610 continue;
8611
8612 if (first)
8613 {
8614 buf.puts ("<");
8615 first = 0;
8616 }
8617 else
8618 buf.puts (", ");
8619
8620 attr = dwarf2_attr (child, DW_AT_type, cu);
8621 if (attr == NULL)
8622 {
8623 complaint (&symfile_complaints,
8624 _("template parameter missing DW_AT_type"));
8625 buf.puts ("UNKNOWN_TYPE");
8626 continue;
8627 }
8628 type = die_type (child, cu);
8629
8630 if (child->tag == DW_TAG_template_type_param)
8631 {
8632 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
8633 continue;
8634 }
8635
8636 attr = dwarf2_attr (child, DW_AT_const_value, cu);
8637 if (attr == NULL)
8638 {
8639 complaint (&symfile_complaints,
8640 _("template parameter missing "
8641 "DW_AT_const_value"));
8642 buf.puts ("UNKNOWN_VALUE");
8643 continue;
8644 }
8645
8646 dwarf2_const_value_attr (attr, type, name,
8647 &cu->comp_unit_obstack, cu,
8648 &value, &bytes, &baton);
8649
8650 if (TYPE_NOSIGN (type))
8651 /* GDB prints characters as NUMBER 'CHAR'. If that's
8652 changed, this can use value_print instead. */
8653 c_printchar (value, type, &buf);
8654 else
8655 {
8656 struct value_print_options opts;
8657
8658 if (baton != NULL)
8659 v = dwarf2_evaluate_loc_desc (type, NULL,
8660 baton->data,
8661 baton->size,
8662 baton->per_cu);
8663 else if (bytes != NULL)
8664 {
8665 v = allocate_value (type);
8666 memcpy (value_contents_writeable (v), bytes,
8667 TYPE_LENGTH (type));
8668 }
8669 else
8670 v = value_from_longest (type, value);
8671
8672 /* Specify decimal so that we do not depend on
8673 the radix. */
8674 get_formatted_print_options (&opts, 'd');
8675 opts.raw = 1;
8676 value_print (v, &buf, &opts);
8677 release_value (v);
8678 value_free (v);
8679 }
8680 }
8681
8682 die->building_fullname = 0;
8683
8684 if (!first)
8685 {
8686 /* Close the argument list, with a space if necessary
8687 (nested templates). */
8688 if (!buf.empty () && buf.string ().back () == '>')
8689 buf.puts (" >");
8690 else
8691 buf.puts (">");
8692 }
8693 }
8694
8695 /* For C++ methods, append formal parameter type
8696 information, if PHYSNAME. */
8697
8698 if (physname && die->tag == DW_TAG_subprogram
8699 && cu->language == language_cplus)
8700 {
8701 struct type *type = read_type_die (die, cu);
8702
8703 c_type_print_args (type, &buf, 1, cu->language,
8704 &type_print_raw_options);
8705
8706 if (cu->language == language_cplus)
8707 {
8708 /* Assume that an artificial first parameter is
8709 "this", but do not crash if it is not. RealView
8710 marks unnamed (and thus unused) parameters as
8711 artificial; there is no way to differentiate
8712 the two cases. */
8713 if (TYPE_NFIELDS (type) > 0
8714 && TYPE_FIELD_ARTIFICIAL (type, 0)
8715 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
8716 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
8717 0))))
8718 buf.puts (" const");
8719 }
8720 }
8721
8722 const std::string &intermediate_name = buf.string ();
8723
8724 if (cu->language == language_cplus)
8725 canonical_name
8726 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
8727 &objfile->per_bfd->storage_obstack);
8728
8729 /* If we only computed INTERMEDIATE_NAME, or if
8730 INTERMEDIATE_NAME is already canonical, then we need to
8731 copy it to the appropriate obstack. */
8732 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
8733 name = ((const char *)
8734 obstack_copy0 (&objfile->per_bfd->storage_obstack,
8735 intermediate_name.c_str (),
8736 intermediate_name.length ()));
8737 else
8738 name = canonical_name;
8739 }
8740 }
8741
8742 return name;
8743 }
8744
8745 /* Return the fully qualified name of DIE, based on its DW_AT_name.
8746 If scope qualifiers are appropriate they will be added. The result
8747 will be allocated on the storage_obstack, or NULL if the DIE does
8748 not have a name. NAME may either be from a previous call to
8749 dwarf2_name or NULL.
8750
8751 The output string will be canonicalized (if C++). */
8752
8753 static const char *
8754 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
8755 {
8756 return dwarf2_compute_name (name, die, cu, 0);
8757 }
8758
8759 /* Construct a physname for the given DIE in CU. NAME may either be
8760 from a previous call to dwarf2_name or NULL. The result will be
8761 allocated on the objfile_objstack or NULL if the DIE does not have a
8762 name.
8763
8764 The output string will be canonicalized (if C++). */
8765
8766 static const char *
8767 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
8768 {
8769 struct objfile *objfile = cu->objfile;
8770 const char *retval, *mangled = NULL, *canon = NULL;
8771 struct cleanup *back_to;
8772 int need_copy = 1;
8773
8774 /* In this case dwarf2_compute_name is just a shortcut not building anything
8775 on its own. */
8776 if (!die_needs_namespace (die, cu))
8777 return dwarf2_compute_name (name, die, cu, 1);
8778
8779 back_to = make_cleanup (null_cleanup, NULL);
8780
8781 mangled = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
8782 if (mangled == NULL)
8783 mangled = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
8784
8785 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
8786 See https://github.com/rust-lang/rust/issues/32925. */
8787 if (cu->language == language_rust && mangled != NULL
8788 && strchr (mangled, '{') != NULL)
8789 mangled = NULL;
8790
8791 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
8792 has computed. */
8793 if (mangled != NULL)
8794 {
8795 char *demangled;
8796
8797 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
8798 type. It is easier for GDB users to search for such functions as
8799 `name(params)' than `long name(params)'. In such case the minimal
8800 symbol names do not match the full symbol names but for template
8801 functions there is never a need to look up their definition from their
8802 declaration so the only disadvantage remains the minimal symbol
8803 variant `long name(params)' does not have the proper inferior type.
8804 */
8805
8806 if (cu->language == language_go)
8807 {
8808 /* This is a lie, but we already lie to the caller new_symbol_full.
8809 new_symbol_full assumes we return the mangled name.
8810 This just undoes that lie until things are cleaned up. */
8811 demangled = NULL;
8812 }
8813 else
8814 {
8815 demangled = gdb_demangle (mangled,
8816 (DMGL_PARAMS | DMGL_ANSI | DMGL_RET_DROP));
8817 }
8818 if (demangled)
8819 {
8820 make_cleanup (xfree, demangled);
8821 canon = demangled;
8822 }
8823 else
8824 {
8825 canon = mangled;
8826 need_copy = 0;
8827 }
8828 }
8829
8830 if (canon == NULL || check_physname)
8831 {
8832 const char *physname = dwarf2_compute_name (name, die, cu, 1);
8833
8834 if (canon != NULL && strcmp (physname, canon) != 0)
8835 {
8836 /* It may not mean a bug in GDB. The compiler could also
8837 compute DW_AT_linkage_name incorrectly. But in such case
8838 GDB would need to be bug-to-bug compatible. */
8839
8840 complaint (&symfile_complaints,
8841 _("Computed physname <%s> does not match demangled <%s> "
8842 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
8843 physname, canon, mangled, die->offset.sect_off,
8844 objfile_name (objfile));
8845
8846 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
8847 is available here - over computed PHYSNAME. It is safer
8848 against both buggy GDB and buggy compilers. */
8849
8850 retval = canon;
8851 }
8852 else
8853 {
8854 retval = physname;
8855 need_copy = 0;
8856 }
8857 }
8858 else
8859 retval = canon;
8860
8861 if (need_copy)
8862 retval = ((const char *)
8863 obstack_copy0 (&objfile->per_bfd->storage_obstack,
8864 retval, strlen (retval)));
8865
8866 do_cleanups (back_to);
8867 return retval;
8868 }
8869
8870 /* Inspect DIE in CU for a namespace alias. If one exists, record
8871 a new symbol for it.
8872
8873 Returns 1 if a namespace alias was recorded, 0 otherwise. */
8874
8875 static int
8876 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
8877 {
8878 struct attribute *attr;
8879
8880 /* If the die does not have a name, this is not a namespace
8881 alias. */
8882 attr = dwarf2_attr (die, DW_AT_name, cu);
8883 if (attr != NULL)
8884 {
8885 int num;
8886 struct die_info *d = die;
8887 struct dwarf2_cu *imported_cu = cu;
8888
8889 /* If the compiler has nested DW_AT_imported_declaration DIEs,
8890 keep inspecting DIEs until we hit the underlying import. */
8891 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
8892 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
8893 {
8894 attr = dwarf2_attr (d, DW_AT_import, cu);
8895 if (attr == NULL)
8896 break;
8897
8898 d = follow_die_ref (d, attr, &imported_cu);
8899 if (d->tag != DW_TAG_imported_declaration)
8900 break;
8901 }
8902
8903 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
8904 {
8905 complaint (&symfile_complaints,
8906 _("DIE at 0x%x has too many recursively imported "
8907 "declarations"), d->offset.sect_off);
8908 return 0;
8909 }
8910
8911 if (attr != NULL)
8912 {
8913 struct type *type;
8914 sect_offset offset = dwarf2_get_ref_die_offset (attr);
8915
8916 type = get_die_type_at_offset (offset, cu->per_cu);
8917 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
8918 {
8919 /* This declaration is a global namespace alias. Add
8920 a symbol for it whose type is the aliased namespace. */
8921 new_symbol (die, type, cu);
8922 return 1;
8923 }
8924 }
8925 }
8926
8927 return 0;
8928 }
8929
8930 /* Return the using directives repository (global or local?) to use in the
8931 current context for LANGUAGE.
8932
8933 For Ada, imported declarations can materialize renamings, which *may* be
8934 global. However it is impossible (for now?) in DWARF to distinguish
8935 "external" imported declarations and "static" ones. As all imported
8936 declarations seem to be static in all other languages, make them all CU-wide
8937 global only in Ada. */
8938
8939 static struct using_direct **
8940 using_directives (enum language language)
8941 {
8942 if (language == language_ada && context_stack_depth == 0)
8943 return &global_using_directives;
8944 else
8945 return &local_using_directives;
8946 }
8947
8948 /* Read the import statement specified by the given die and record it. */
8949
8950 static void
8951 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
8952 {
8953 struct objfile *objfile = cu->objfile;
8954 struct attribute *import_attr;
8955 struct die_info *imported_die, *child_die;
8956 struct dwarf2_cu *imported_cu;
8957 const char *imported_name;
8958 const char *imported_name_prefix;
8959 const char *canonical_name;
8960 const char *import_alias;
8961 const char *imported_declaration = NULL;
8962 const char *import_prefix;
8963 VEC (const_char_ptr) *excludes = NULL;
8964 struct cleanup *cleanups;
8965
8966 import_attr = dwarf2_attr (die, DW_AT_import, cu);
8967 if (import_attr == NULL)
8968 {
8969 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
8970 dwarf_tag_name (die->tag));
8971 return;
8972 }
8973
8974 imported_cu = cu;
8975 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
8976 imported_name = dwarf2_name (imported_die, imported_cu);
8977 if (imported_name == NULL)
8978 {
8979 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
8980
8981 The import in the following code:
8982 namespace A
8983 {
8984 typedef int B;
8985 }
8986
8987 int main ()
8988 {
8989 using A::B;
8990 B b;
8991 return b;
8992 }
8993
8994 ...
8995 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
8996 <52> DW_AT_decl_file : 1
8997 <53> DW_AT_decl_line : 6
8998 <54> DW_AT_import : <0x75>
8999 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
9000 <59> DW_AT_name : B
9001 <5b> DW_AT_decl_file : 1
9002 <5c> DW_AT_decl_line : 2
9003 <5d> DW_AT_type : <0x6e>
9004 ...
9005 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
9006 <76> DW_AT_byte_size : 4
9007 <77> DW_AT_encoding : 5 (signed)
9008
9009 imports the wrong die ( 0x75 instead of 0x58 ).
9010 This case will be ignored until the gcc bug is fixed. */
9011 return;
9012 }
9013
9014 /* Figure out the local name after import. */
9015 import_alias = dwarf2_name (die, cu);
9016
9017 /* Figure out where the statement is being imported to. */
9018 import_prefix = determine_prefix (die, cu);
9019
9020 /* Figure out what the scope of the imported die is and prepend it
9021 to the name of the imported die. */
9022 imported_name_prefix = determine_prefix (imported_die, imported_cu);
9023
9024 if (imported_die->tag != DW_TAG_namespace
9025 && imported_die->tag != DW_TAG_module)
9026 {
9027 imported_declaration = imported_name;
9028 canonical_name = imported_name_prefix;
9029 }
9030 else if (strlen (imported_name_prefix) > 0)
9031 canonical_name = obconcat (&objfile->objfile_obstack,
9032 imported_name_prefix,
9033 (cu->language == language_d ? "." : "::"),
9034 imported_name, (char *) NULL);
9035 else
9036 canonical_name = imported_name;
9037
9038 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
9039
9040 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
9041 for (child_die = die->child; child_die && child_die->tag;
9042 child_die = sibling_die (child_die))
9043 {
9044 /* DWARF-4: A Fortran use statement with a “rename list” may be
9045 represented by an imported module entry with an import attribute
9046 referring to the module and owned entries corresponding to those
9047 entities that are renamed as part of being imported. */
9048
9049 if (child_die->tag != DW_TAG_imported_declaration)
9050 {
9051 complaint (&symfile_complaints,
9052 _("child DW_TAG_imported_declaration expected "
9053 "- DIE at 0x%x [in module %s]"),
9054 child_die->offset.sect_off, objfile_name (objfile));
9055 continue;
9056 }
9057
9058 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
9059 if (import_attr == NULL)
9060 {
9061 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
9062 dwarf_tag_name (child_die->tag));
9063 continue;
9064 }
9065
9066 imported_cu = cu;
9067 imported_die = follow_die_ref_or_sig (child_die, import_attr,
9068 &imported_cu);
9069 imported_name = dwarf2_name (imported_die, imported_cu);
9070 if (imported_name == NULL)
9071 {
9072 complaint (&symfile_complaints,
9073 _("child DW_TAG_imported_declaration has unknown "
9074 "imported name - DIE at 0x%x [in module %s]"),
9075 child_die->offset.sect_off, objfile_name (objfile));
9076 continue;
9077 }
9078
9079 VEC_safe_push (const_char_ptr, excludes, imported_name);
9080
9081 process_die (child_die, cu);
9082 }
9083
9084 add_using_directive (using_directives (cu->language),
9085 import_prefix,
9086 canonical_name,
9087 import_alias,
9088 imported_declaration,
9089 excludes,
9090 0,
9091 &objfile->objfile_obstack);
9092
9093 do_cleanups (cleanups);
9094 }
9095
9096 /* Cleanup function for handle_DW_AT_stmt_list. */
9097
9098 static void
9099 free_cu_line_header (void *arg)
9100 {
9101 struct dwarf2_cu *cu = (struct dwarf2_cu *) arg;
9102
9103 free_line_header (cu->line_header);
9104 cu->line_header = NULL;
9105 }
9106
9107 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
9108 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
9109 this, it was first present in GCC release 4.3.0. */
9110
9111 static int
9112 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
9113 {
9114 if (!cu->checked_producer)
9115 check_producer (cu);
9116
9117 return cu->producer_is_gcc_lt_4_3;
9118 }
9119
9120 static void
9121 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
9122 const char **name, const char **comp_dir)
9123 {
9124 /* Find the filename. Do not use dwarf2_name here, since the filename
9125 is not a source language identifier. */
9126 *name = dwarf2_string_attr (die, DW_AT_name, cu);
9127 *comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9128
9129 if (*comp_dir == NULL
9130 && producer_is_gcc_lt_4_3 (cu) && *name != NULL
9131 && IS_ABSOLUTE_PATH (*name))
9132 {
9133 char *d = ldirname (*name);
9134
9135 *comp_dir = d;
9136 if (d != NULL)
9137 make_cleanup (xfree, d);
9138 }
9139 if (*comp_dir != NULL)
9140 {
9141 /* Irix 6.2 native cc prepends <machine>.: to the compilation
9142 directory, get rid of it. */
9143 const char *cp = strchr (*comp_dir, ':');
9144
9145 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
9146 *comp_dir = cp + 1;
9147 }
9148
9149 if (*name == NULL)
9150 *name = "<unknown>";
9151 }
9152
9153 /* Handle DW_AT_stmt_list for a compilation unit.
9154 DIE is the DW_TAG_compile_unit die for CU.
9155 COMP_DIR is the compilation directory. LOWPC is passed to
9156 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
9157
9158 static void
9159 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
9160 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
9161 {
9162 struct objfile *objfile = dwarf2_per_objfile->objfile;
9163 struct attribute *attr;
9164 unsigned int line_offset;
9165 struct line_header line_header_local;
9166 hashval_t line_header_local_hash;
9167 unsigned u;
9168 void **slot;
9169 int decode_mapping;
9170
9171 gdb_assert (! cu->per_cu->is_debug_types);
9172
9173 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
9174 if (attr == NULL)
9175 return;
9176
9177 line_offset = DW_UNSND (attr);
9178
9179 /* The line header hash table is only created if needed (it exists to
9180 prevent redundant reading of the line table for partial_units).
9181 If we're given a partial_unit, we'll need it. If we're given a
9182 compile_unit, then use the line header hash table if it's already
9183 created, but don't create one just yet. */
9184
9185 if (dwarf2_per_objfile->line_header_hash == NULL
9186 && die->tag == DW_TAG_partial_unit)
9187 {
9188 dwarf2_per_objfile->line_header_hash
9189 = htab_create_alloc_ex (127, line_header_hash_voidp,
9190 line_header_eq_voidp,
9191 free_line_header_voidp,
9192 &objfile->objfile_obstack,
9193 hashtab_obstack_allocate,
9194 dummy_obstack_deallocate);
9195 }
9196
9197 line_header_local.offset.sect_off = line_offset;
9198 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
9199 line_header_local_hash = line_header_hash (&line_header_local);
9200 if (dwarf2_per_objfile->line_header_hash != NULL)
9201 {
9202 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
9203 &line_header_local,
9204 line_header_local_hash, NO_INSERT);
9205
9206 /* For DW_TAG_compile_unit we need info like symtab::linetable which
9207 is not present in *SLOT (since if there is something in *SLOT then
9208 it will be for a partial_unit). */
9209 if (die->tag == DW_TAG_partial_unit && slot != NULL)
9210 {
9211 gdb_assert (*slot != NULL);
9212 cu->line_header = (struct line_header *) *slot;
9213 return;
9214 }
9215 }
9216
9217 /* dwarf_decode_line_header does not yet provide sufficient information.
9218 We always have to call also dwarf_decode_lines for it. */
9219 cu->line_header = dwarf_decode_line_header (line_offset, cu);
9220 if (cu->line_header == NULL)
9221 return;
9222
9223 if (dwarf2_per_objfile->line_header_hash == NULL)
9224 slot = NULL;
9225 else
9226 {
9227 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
9228 &line_header_local,
9229 line_header_local_hash, INSERT);
9230 gdb_assert (slot != NULL);
9231 }
9232 if (slot != NULL && *slot == NULL)
9233 {
9234 /* This newly decoded line number information unit will be owned
9235 by line_header_hash hash table. */
9236 *slot = cu->line_header;
9237 }
9238 else
9239 {
9240 /* We cannot free any current entry in (*slot) as that struct line_header
9241 may be already used by multiple CUs. Create only temporary decoded
9242 line_header for this CU - it may happen at most once for each line
9243 number information unit. And if we're not using line_header_hash
9244 then this is what we want as well. */
9245 gdb_assert (die->tag != DW_TAG_partial_unit);
9246 make_cleanup (free_cu_line_header, cu);
9247 }
9248 decode_mapping = (die->tag != DW_TAG_partial_unit);
9249 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
9250 decode_mapping);
9251 }
9252
9253 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
9254
9255 static void
9256 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
9257 {
9258 struct objfile *objfile = dwarf2_per_objfile->objfile;
9259 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9260 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
9261 CORE_ADDR lowpc = ((CORE_ADDR) -1);
9262 CORE_ADDR highpc = ((CORE_ADDR) 0);
9263 struct attribute *attr;
9264 const char *name = NULL;
9265 const char *comp_dir = NULL;
9266 struct die_info *child_die;
9267 CORE_ADDR baseaddr;
9268
9269 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9270
9271 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
9272
9273 /* If we didn't find a lowpc, set it to highpc to avoid complaints
9274 from finish_block. */
9275 if (lowpc == ((CORE_ADDR) -1))
9276 lowpc = highpc;
9277 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
9278
9279 find_file_and_directory (die, cu, &name, &comp_dir);
9280
9281 prepare_one_comp_unit (cu, die, cu->language);
9282
9283 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
9284 standardised yet. As a workaround for the language detection we fall
9285 back to the DW_AT_producer string. */
9286 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
9287 cu->language = language_opencl;
9288
9289 /* Similar hack for Go. */
9290 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
9291 set_cu_language (DW_LANG_Go, cu);
9292
9293 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
9294
9295 /* Decode line number information if present. We do this before
9296 processing child DIEs, so that the line header table is available
9297 for DW_AT_decl_file. */
9298 handle_DW_AT_stmt_list (die, cu, comp_dir, lowpc);
9299
9300 /* Process all dies in compilation unit. */
9301 if (die->child != NULL)
9302 {
9303 child_die = die->child;
9304 while (child_die && child_die->tag)
9305 {
9306 process_die (child_die, cu);
9307 child_die = sibling_die (child_die);
9308 }
9309 }
9310
9311 /* Decode macro information, if present. Dwarf 2 macro information
9312 refers to information in the line number info statement program
9313 header, so we can only read it if we've read the header
9314 successfully. */
9315 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
9316 if (attr && cu->line_header)
9317 {
9318 if (dwarf2_attr (die, DW_AT_macro_info, cu))
9319 complaint (&symfile_complaints,
9320 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
9321
9322 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
9323 }
9324 else
9325 {
9326 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
9327 if (attr && cu->line_header)
9328 {
9329 unsigned int macro_offset = DW_UNSND (attr);
9330
9331 dwarf_decode_macros (cu, macro_offset, 0);
9332 }
9333 }
9334
9335 do_cleanups (back_to);
9336 }
9337
9338 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
9339 Create the set of symtabs used by this TU, or if this TU is sharing
9340 symtabs with another TU and the symtabs have already been created
9341 then restore those symtabs in the line header.
9342 We don't need the pc/line-number mapping for type units. */
9343
9344 static void
9345 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
9346 {
9347 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
9348 struct type_unit_group *tu_group;
9349 int first_time;
9350 struct line_header *lh;
9351 struct attribute *attr;
9352 unsigned int i, line_offset;
9353 struct signatured_type *sig_type;
9354
9355 gdb_assert (per_cu->is_debug_types);
9356 sig_type = (struct signatured_type *) per_cu;
9357
9358 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
9359
9360 /* If we're using .gdb_index (includes -readnow) then
9361 per_cu->type_unit_group may not have been set up yet. */
9362 if (sig_type->type_unit_group == NULL)
9363 sig_type->type_unit_group = get_type_unit_group (cu, attr);
9364 tu_group = sig_type->type_unit_group;
9365
9366 /* If we've already processed this stmt_list there's no real need to
9367 do it again, we could fake it and just recreate the part we need
9368 (file name,index -> symtab mapping). If data shows this optimization
9369 is useful we can do it then. */
9370 first_time = tu_group->compunit_symtab == NULL;
9371
9372 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
9373 debug info. */
9374 lh = NULL;
9375 if (attr != NULL)
9376 {
9377 line_offset = DW_UNSND (attr);
9378 lh = dwarf_decode_line_header (line_offset, cu);
9379 }
9380 if (lh == NULL)
9381 {
9382 if (first_time)
9383 dwarf2_start_symtab (cu, "", NULL, 0);
9384 else
9385 {
9386 gdb_assert (tu_group->symtabs == NULL);
9387 restart_symtab (tu_group->compunit_symtab, "", 0);
9388 }
9389 return;
9390 }
9391
9392 cu->line_header = lh;
9393 make_cleanup (free_cu_line_header, cu);
9394
9395 if (first_time)
9396 {
9397 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
9398
9399 /* Note: We don't assign tu_group->compunit_symtab yet because we're
9400 still initializing it, and our caller (a few levels up)
9401 process_full_type_unit still needs to know if this is the first
9402 time. */
9403
9404 tu_group->num_symtabs = lh->num_file_names;
9405 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
9406
9407 for (i = 0; i < lh->num_file_names; ++i)
9408 {
9409 const char *dir = NULL;
9410 struct file_entry *fe = &lh->file_names[i];
9411
9412 if (fe->dir_index && lh->include_dirs != NULL)
9413 dir = lh->include_dirs[fe->dir_index - 1];
9414 dwarf2_start_subfile (fe->name, dir);
9415
9416 if (current_subfile->symtab == NULL)
9417 {
9418 /* NOTE: start_subfile will recognize when it's been passed
9419 a file it has already seen. So we can't assume there's a
9420 simple mapping from lh->file_names to subfiles, plus
9421 lh->file_names may contain dups. */
9422 current_subfile->symtab
9423 = allocate_symtab (cust, current_subfile->name);
9424 }
9425
9426 fe->symtab = current_subfile->symtab;
9427 tu_group->symtabs[i] = fe->symtab;
9428 }
9429 }
9430 else
9431 {
9432 restart_symtab (tu_group->compunit_symtab, "", 0);
9433
9434 for (i = 0; i < lh->num_file_names; ++i)
9435 {
9436 struct file_entry *fe = &lh->file_names[i];
9437
9438 fe->symtab = tu_group->symtabs[i];
9439 }
9440 }
9441
9442 /* The main symtab is allocated last. Type units don't have DW_AT_name
9443 so they don't have a "real" (so to speak) symtab anyway.
9444 There is later code that will assign the main symtab to all symbols
9445 that don't have one. We need to handle the case of a symbol with a
9446 missing symtab (DW_AT_decl_file) anyway. */
9447 }
9448
9449 /* Process DW_TAG_type_unit.
9450 For TUs we want to skip the first top level sibling if it's not the
9451 actual type being defined by this TU. In this case the first top
9452 level sibling is there to provide context only. */
9453
9454 static void
9455 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
9456 {
9457 struct die_info *child_die;
9458
9459 prepare_one_comp_unit (cu, die, language_minimal);
9460
9461 /* Initialize (or reinitialize) the machinery for building symtabs.
9462 We do this before processing child DIEs, so that the line header table
9463 is available for DW_AT_decl_file. */
9464 setup_type_unit_groups (die, cu);
9465
9466 if (die->child != NULL)
9467 {
9468 child_die = die->child;
9469 while (child_die && child_die->tag)
9470 {
9471 process_die (child_die, cu);
9472 child_die = sibling_die (child_die);
9473 }
9474 }
9475 }
9476 \f
9477 /* DWO/DWP files.
9478
9479 http://gcc.gnu.org/wiki/DebugFission
9480 http://gcc.gnu.org/wiki/DebugFissionDWP
9481
9482 To simplify handling of both DWO files ("object" files with the DWARF info)
9483 and DWP files (a file with the DWOs packaged up into one file), we treat
9484 DWP files as having a collection of virtual DWO files. */
9485
9486 static hashval_t
9487 hash_dwo_file (const void *item)
9488 {
9489 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
9490 hashval_t hash;
9491
9492 hash = htab_hash_string (dwo_file->dwo_name);
9493 if (dwo_file->comp_dir != NULL)
9494 hash += htab_hash_string (dwo_file->comp_dir);
9495 return hash;
9496 }
9497
9498 static int
9499 eq_dwo_file (const void *item_lhs, const void *item_rhs)
9500 {
9501 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
9502 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
9503
9504 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
9505 return 0;
9506 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
9507 return lhs->comp_dir == rhs->comp_dir;
9508 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
9509 }
9510
9511 /* Allocate a hash table for DWO files. */
9512
9513 static htab_t
9514 allocate_dwo_file_hash_table (void)
9515 {
9516 struct objfile *objfile = dwarf2_per_objfile->objfile;
9517
9518 return htab_create_alloc_ex (41,
9519 hash_dwo_file,
9520 eq_dwo_file,
9521 NULL,
9522 &objfile->objfile_obstack,
9523 hashtab_obstack_allocate,
9524 dummy_obstack_deallocate);
9525 }
9526
9527 /* Lookup DWO file DWO_NAME. */
9528
9529 static void **
9530 lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
9531 {
9532 struct dwo_file find_entry;
9533 void **slot;
9534
9535 if (dwarf2_per_objfile->dwo_files == NULL)
9536 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
9537
9538 memset (&find_entry, 0, sizeof (find_entry));
9539 find_entry.dwo_name = dwo_name;
9540 find_entry.comp_dir = comp_dir;
9541 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
9542
9543 return slot;
9544 }
9545
9546 static hashval_t
9547 hash_dwo_unit (const void *item)
9548 {
9549 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
9550
9551 /* This drops the top 32 bits of the id, but is ok for a hash. */
9552 return dwo_unit->signature;
9553 }
9554
9555 static int
9556 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
9557 {
9558 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
9559 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
9560
9561 /* The signature is assumed to be unique within the DWO file.
9562 So while object file CU dwo_id's always have the value zero,
9563 that's OK, assuming each object file DWO file has only one CU,
9564 and that's the rule for now. */
9565 return lhs->signature == rhs->signature;
9566 }
9567
9568 /* Allocate a hash table for DWO CUs,TUs.
9569 There is one of these tables for each of CUs,TUs for each DWO file. */
9570
9571 static htab_t
9572 allocate_dwo_unit_table (struct objfile *objfile)
9573 {
9574 /* Start out with a pretty small number.
9575 Generally DWO files contain only one CU and maybe some TUs. */
9576 return htab_create_alloc_ex (3,
9577 hash_dwo_unit,
9578 eq_dwo_unit,
9579 NULL,
9580 &objfile->objfile_obstack,
9581 hashtab_obstack_allocate,
9582 dummy_obstack_deallocate);
9583 }
9584
9585 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
9586
9587 struct create_dwo_cu_data
9588 {
9589 struct dwo_file *dwo_file;
9590 struct dwo_unit dwo_unit;
9591 };
9592
9593 /* die_reader_func for create_dwo_cu. */
9594
9595 static void
9596 create_dwo_cu_reader (const struct die_reader_specs *reader,
9597 const gdb_byte *info_ptr,
9598 struct die_info *comp_unit_die,
9599 int has_children,
9600 void *datap)
9601 {
9602 struct dwarf2_cu *cu = reader->cu;
9603 sect_offset offset = cu->per_cu->offset;
9604 struct dwarf2_section_info *section = cu->per_cu->section;
9605 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
9606 struct dwo_file *dwo_file = data->dwo_file;
9607 struct dwo_unit *dwo_unit = &data->dwo_unit;
9608 struct attribute *attr;
9609
9610 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
9611 if (attr == NULL)
9612 {
9613 complaint (&symfile_complaints,
9614 _("Dwarf Error: debug entry at offset 0x%x is missing"
9615 " its dwo_id [in module %s]"),
9616 offset.sect_off, dwo_file->dwo_name);
9617 return;
9618 }
9619
9620 dwo_unit->dwo_file = dwo_file;
9621 dwo_unit->signature = DW_UNSND (attr);
9622 dwo_unit->section = section;
9623 dwo_unit->offset = offset;
9624 dwo_unit->length = cu->per_cu->length;
9625
9626 if (dwarf_read_debug)
9627 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
9628 offset.sect_off, hex_string (dwo_unit->signature));
9629 }
9630
9631 /* Create the dwo_unit for the lone CU in DWO_FILE.
9632 Note: This function processes DWO files only, not DWP files. */
9633
9634 static struct dwo_unit *
9635 create_dwo_cu (struct dwo_file *dwo_file)
9636 {
9637 struct objfile *objfile = dwarf2_per_objfile->objfile;
9638 struct dwarf2_section_info *section = &dwo_file->sections.info;
9639 const gdb_byte *info_ptr, *end_ptr;
9640 struct create_dwo_cu_data create_dwo_cu_data;
9641 struct dwo_unit *dwo_unit;
9642
9643 dwarf2_read_section (objfile, section);
9644 info_ptr = section->buffer;
9645
9646 if (info_ptr == NULL)
9647 return NULL;
9648
9649 if (dwarf_read_debug)
9650 {
9651 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
9652 get_section_name (section),
9653 get_section_file_name (section));
9654 }
9655
9656 create_dwo_cu_data.dwo_file = dwo_file;
9657 dwo_unit = NULL;
9658
9659 end_ptr = info_ptr + section->size;
9660 while (info_ptr < end_ptr)
9661 {
9662 struct dwarf2_per_cu_data per_cu;
9663
9664 memset (&create_dwo_cu_data.dwo_unit, 0,
9665 sizeof (create_dwo_cu_data.dwo_unit));
9666 memset (&per_cu, 0, sizeof (per_cu));
9667 per_cu.objfile = objfile;
9668 per_cu.is_debug_types = 0;
9669 per_cu.offset.sect_off = info_ptr - section->buffer;
9670 per_cu.section = section;
9671
9672 init_cutu_and_read_dies_no_follow (&per_cu, dwo_file,
9673 create_dwo_cu_reader,
9674 &create_dwo_cu_data);
9675
9676 if (create_dwo_cu_data.dwo_unit.dwo_file != NULL)
9677 {
9678 /* If we've already found one, complain. We only support one
9679 because having more than one requires hacking the dwo_name of
9680 each to match, which is highly unlikely to happen. */
9681 if (dwo_unit != NULL)
9682 {
9683 complaint (&symfile_complaints,
9684 _("Multiple CUs in DWO file %s [in module %s]"),
9685 dwo_file->dwo_name, objfile_name (objfile));
9686 break;
9687 }
9688
9689 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
9690 *dwo_unit = create_dwo_cu_data.dwo_unit;
9691 }
9692
9693 info_ptr += per_cu.length;
9694 }
9695
9696 return dwo_unit;
9697 }
9698
9699 /* DWP file .debug_{cu,tu}_index section format:
9700 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
9701
9702 DWP Version 1:
9703
9704 Both index sections have the same format, and serve to map a 64-bit
9705 signature to a set of section numbers. Each section begins with a header,
9706 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
9707 indexes, and a pool of 32-bit section numbers. The index sections will be
9708 aligned at 8-byte boundaries in the file.
9709
9710 The index section header consists of:
9711
9712 V, 32 bit version number
9713 -, 32 bits unused
9714 N, 32 bit number of compilation units or type units in the index
9715 M, 32 bit number of slots in the hash table
9716
9717 Numbers are recorded using the byte order of the application binary.
9718
9719 The hash table begins at offset 16 in the section, and consists of an array
9720 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
9721 order of the application binary). Unused slots in the hash table are 0.
9722 (We rely on the extreme unlikeliness of a signature being exactly 0.)
9723
9724 The parallel table begins immediately after the hash table
9725 (at offset 16 + 8 * M from the beginning of the section), and consists of an
9726 array of 32-bit indexes (using the byte order of the application binary),
9727 corresponding 1-1 with slots in the hash table. Each entry in the parallel
9728 table contains a 32-bit index into the pool of section numbers. For unused
9729 hash table slots, the corresponding entry in the parallel table will be 0.
9730
9731 The pool of section numbers begins immediately following the hash table
9732 (at offset 16 + 12 * M from the beginning of the section). The pool of
9733 section numbers consists of an array of 32-bit words (using the byte order
9734 of the application binary). Each item in the array is indexed starting
9735 from 0. The hash table entry provides the index of the first section
9736 number in the set. Additional section numbers in the set follow, and the
9737 set is terminated by a 0 entry (section number 0 is not used in ELF).
9738
9739 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
9740 section must be the first entry in the set, and the .debug_abbrev.dwo must
9741 be the second entry. Other members of the set may follow in any order.
9742
9743 ---
9744
9745 DWP Version 2:
9746
9747 DWP Version 2 combines all the .debug_info, etc. sections into one,
9748 and the entries in the index tables are now offsets into these sections.
9749 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
9750 section.
9751
9752 Index Section Contents:
9753 Header
9754 Hash Table of Signatures dwp_hash_table.hash_table
9755 Parallel Table of Indices dwp_hash_table.unit_table
9756 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
9757 Table of Section Sizes dwp_hash_table.v2.sizes
9758
9759 The index section header consists of:
9760
9761 V, 32 bit version number
9762 L, 32 bit number of columns in the table of section offsets
9763 N, 32 bit number of compilation units or type units in the index
9764 M, 32 bit number of slots in the hash table
9765
9766 Numbers are recorded using the byte order of the application binary.
9767
9768 The hash table has the same format as version 1.
9769 The parallel table of indices has the same format as version 1,
9770 except that the entries are origin-1 indices into the table of sections
9771 offsets and the table of section sizes.
9772
9773 The table of offsets begins immediately following the parallel table
9774 (at offset 16 + 12 * M from the beginning of the section). The table is
9775 a two-dimensional array of 32-bit words (using the byte order of the
9776 application binary), with L columns and N+1 rows, in row-major order.
9777 Each row in the array is indexed starting from 0. The first row provides
9778 a key to the remaining rows: each column in this row provides an identifier
9779 for a debug section, and the offsets in the same column of subsequent rows
9780 refer to that section. The section identifiers are:
9781
9782 DW_SECT_INFO 1 .debug_info.dwo
9783 DW_SECT_TYPES 2 .debug_types.dwo
9784 DW_SECT_ABBREV 3 .debug_abbrev.dwo
9785 DW_SECT_LINE 4 .debug_line.dwo
9786 DW_SECT_LOC 5 .debug_loc.dwo
9787 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
9788 DW_SECT_MACINFO 7 .debug_macinfo.dwo
9789 DW_SECT_MACRO 8 .debug_macro.dwo
9790
9791 The offsets provided by the CU and TU index sections are the base offsets
9792 for the contributions made by each CU or TU to the corresponding section
9793 in the package file. Each CU and TU header contains an abbrev_offset
9794 field, used to find the abbreviations table for that CU or TU within the
9795 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
9796 be interpreted as relative to the base offset given in the index section.
9797 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
9798 should be interpreted as relative to the base offset for .debug_line.dwo,
9799 and offsets into other debug sections obtained from DWARF attributes should
9800 also be interpreted as relative to the corresponding base offset.
9801
9802 The table of sizes begins immediately following the table of offsets.
9803 Like the table of offsets, it is a two-dimensional array of 32-bit words,
9804 with L columns and N rows, in row-major order. Each row in the array is
9805 indexed starting from 1 (row 0 is shared by the two tables).
9806
9807 ---
9808
9809 Hash table lookup is handled the same in version 1 and 2:
9810
9811 We assume that N and M will not exceed 2^32 - 1.
9812 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
9813
9814 Given a 64-bit compilation unit signature or a type signature S, an entry
9815 in the hash table is located as follows:
9816
9817 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
9818 the low-order k bits all set to 1.
9819
9820 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
9821
9822 3) If the hash table entry at index H matches the signature, use that
9823 entry. If the hash table entry at index H is unused (all zeroes),
9824 terminate the search: the signature is not present in the table.
9825
9826 4) Let H = (H + H') modulo M. Repeat at Step 3.
9827
9828 Because M > N and H' and M are relatively prime, the search is guaranteed
9829 to stop at an unused slot or find the match. */
9830
9831 /* Create a hash table to map DWO IDs to their CU/TU entry in
9832 .debug_{info,types}.dwo in DWP_FILE.
9833 Returns NULL if there isn't one.
9834 Note: This function processes DWP files only, not DWO files. */
9835
9836 static struct dwp_hash_table *
9837 create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
9838 {
9839 struct objfile *objfile = dwarf2_per_objfile->objfile;
9840 bfd *dbfd = dwp_file->dbfd;
9841 const gdb_byte *index_ptr, *index_end;
9842 struct dwarf2_section_info *index;
9843 uint32_t version, nr_columns, nr_units, nr_slots;
9844 struct dwp_hash_table *htab;
9845
9846 if (is_debug_types)
9847 index = &dwp_file->sections.tu_index;
9848 else
9849 index = &dwp_file->sections.cu_index;
9850
9851 if (dwarf2_section_empty_p (index))
9852 return NULL;
9853 dwarf2_read_section (objfile, index);
9854
9855 index_ptr = index->buffer;
9856 index_end = index_ptr + index->size;
9857
9858 version = read_4_bytes (dbfd, index_ptr);
9859 index_ptr += 4;
9860 if (version == 2)
9861 nr_columns = read_4_bytes (dbfd, index_ptr);
9862 else
9863 nr_columns = 0;
9864 index_ptr += 4;
9865 nr_units = read_4_bytes (dbfd, index_ptr);
9866 index_ptr += 4;
9867 nr_slots = read_4_bytes (dbfd, index_ptr);
9868 index_ptr += 4;
9869
9870 if (version != 1 && version != 2)
9871 {
9872 error (_("Dwarf Error: unsupported DWP file version (%s)"
9873 " [in module %s]"),
9874 pulongest (version), dwp_file->name);
9875 }
9876 if (nr_slots != (nr_slots & -nr_slots))
9877 {
9878 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
9879 " is not power of 2 [in module %s]"),
9880 pulongest (nr_slots), dwp_file->name);
9881 }
9882
9883 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
9884 htab->version = version;
9885 htab->nr_columns = nr_columns;
9886 htab->nr_units = nr_units;
9887 htab->nr_slots = nr_slots;
9888 htab->hash_table = index_ptr;
9889 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
9890
9891 /* Exit early if the table is empty. */
9892 if (nr_slots == 0 || nr_units == 0
9893 || (version == 2 && nr_columns == 0))
9894 {
9895 /* All must be zero. */
9896 if (nr_slots != 0 || nr_units != 0
9897 || (version == 2 && nr_columns != 0))
9898 {
9899 complaint (&symfile_complaints,
9900 _("Empty DWP but nr_slots,nr_units,nr_columns not"
9901 " all zero [in modules %s]"),
9902 dwp_file->name);
9903 }
9904 return htab;
9905 }
9906
9907 if (version == 1)
9908 {
9909 htab->section_pool.v1.indices =
9910 htab->unit_table + sizeof (uint32_t) * nr_slots;
9911 /* It's harder to decide whether the section is too small in v1.
9912 V1 is deprecated anyway so we punt. */
9913 }
9914 else
9915 {
9916 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
9917 int *ids = htab->section_pool.v2.section_ids;
9918 /* Reverse map for error checking. */
9919 int ids_seen[DW_SECT_MAX + 1];
9920 int i;
9921
9922 if (nr_columns < 2)
9923 {
9924 error (_("Dwarf Error: bad DWP hash table, too few columns"
9925 " in section table [in module %s]"),
9926 dwp_file->name);
9927 }
9928 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
9929 {
9930 error (_("Dwarf Error: bad DWP hash table, too many columns"
9931 " in section table [in module %s]"),
9932 dwp_file->name);
9933 }
9934 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
9935 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
9936 for (i = 0; i < nr_columns; ++i)
9937 {
9938 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
9939
9940 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
9941 {
9942 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
9943 " in section table [in module %s]"),
9944 id, dwp_file->name);
9945 }
9946 if (ids_seen[id] != -1)
9947 {
9948 error (_("Dwarf Error: bad DWP hash table, duplicate section"
9949 " id %d in section table [in module %s]"),
9950 id, dwp_file->name);
9951 }
9952 ids_seen[id] = i;
9953 ids[i] = id;
9954 }
9955 /* Must have exactly one info or types section. */
9956 if (((ids_seen[DW_SECT_INFO] != -1)
9957 + (ids_seen[DW_SECT_TYPES] != -1))
9958 != 1)
9959 {
9960 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
9961 " DWO info/types section [in module %s]"),
9962 dwp_file->name);
9963 }
9964 /* Must have an abbrev section. */
9965 if (ids_seen[DW_SECT_ABBREV] == -1)
9966 {
9967 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
9968 " section [in module %s]"),
9969 dwp_file->name);
9970 }
9971 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
9972 htab->section_pool.v2.sizes =
9973 htab->section_pool.v2.offsets + (sizeof (uint32_t)
9974 * nr_units * nr_columns);
9975 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
9976 * nr_units * nr_columns))
9977 > index_end)
9978 {
9979 error (_("Dwarf Error: DWP index section is corrupt (too small)"
9980 " [in module %s]"),
9981 dwp_file->name);
9982 }
9983 }
9984
9985 return htab;
9986 }
9987
9988 /* Update SECTIONS with the data from SECTP.
9989
9990 This function is like the other "locate" section routines that are
9991 passed to bfd_map_over_sections, but in this context the sections to
9992 read comes from the DWP V1 hash table, not the full ELF section table.
9993
9994 The result is non-zero for success, or zero if an error was found. */
9995
9996 static int
9997 locate_v1_virtual_dwo_sections (asection *sectp,
9998 struct virtual_v1_dwo_sections *sections)
9999 {
10000 const struct dwop_section_names *names = &dwop_section_names;
10001
10002 if (section_is_p (sectp->name, &names->abbrev_dwo))
10003 {
10004 /* There can be only one. */
10005 if (sections->abbrev.s.section != NULL)
10006 return 0;
10007 sections->abbrev.s.section = sectp;
10008 sections->abbrev.size = bfd_get_section_size (sectp);
10009 }
10010 else if (section_is_p (sectp->name, &names->info_dwo)
10011 || section_is_p (sectp->name, &names->types_dwo))
10012 {
10013 /* There can be only one. */
10014 if (sections->info_or_types.s.section != NULL)
10015 return 0;
10016 sections->info_or_types.s.section = sectp;
10017 sections->info_or_types.size = bfd_get_section_size (sectp);
10018 }
10019 else if (section_is_p (sectp->name, &names->line_dwo))
10020 {
10021 /* There can be only one. */
10022 if (sections->line.s.section != NULL)
10023 return 0;
10024 sections->line.s.section = sectp;
10025 sections->line.size = bfd_get_section_size (sectp);
10026 }
10027 else if (section_is_p (sectp->name, &names->loc_dwo))
10028 {
10029 /* There can be only one. */
10030 if (sections->loc.s.section != NULL)
10031 return 0;
10032 sections->loc.s.section = sectp;
10033 sections->loc.size = bfd_get_section_size (sectp);
10034 }
10035 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10036 {
10037 /* There can be only one. */
10038 if (sections->macinfo.s.section != NULL)
10039 return 0;
10040 sections->macinfo.s.section = sectp;
10041 sections->macinfo.size = bfd_get_section_size (sectp);
10042 }
10043 else if (section_is_p (sectp->name, &names->macro_dwo))
10044 {
10045 /* There can be only one. */
10046 if (sections->macro.s.section != NULL)
10047 return 0;
10048 sections->macro.s.section = sectp;
10049 sections->macro.size = bfd_get_section_size (sectp);
10050 }
10051 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10052 {
10053 /* There can be only one. */
10054 if (sections->str_offsets.s.section != NULL)
10055 return 0;
10056 sections->str_offsets.s.section = sectp;
10057 sections->str_offsets.size = bfd_get_section_size (sectp);
10058 }
10059 else
10060 {
10061 /* No other kind of section is valid. */
10062 return 0;
10063 }
10064
10065 return 1;
10066 }
10067
10068 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10069 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10070 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10071 This is for DWP version 1 files. */
10072
10073 static struct dwo_unit *
10074 create_dwo_unit_in_dwp_v1 (struct dwp_file *dwp_file,
10075 uint32_t unit_index,
10076 const char *comp_dir,
10077 ULONGEST signature, int is_debug_types)
10078 {
10079 struct objfile *objfile = dwarf2_per_objfile->objfile;
10080 const struct dwp_hash_table *dwp_htab =
10081 is_debug_types ? dwp_file->tus : dwp_file->cus;
10082 bfd *dbfd = dwp_file->dbfd;
10083 const char *kind = is_debug_types ? "TU" : "CU";
10084 struct dwo_file *dwo_file;
10085 struct dwo_unit *dwo_unit;
10086 struct virtual_v1_dwo_sections sections;
10087 void **dwo_file_slot;
10088 char *virtual_dwo_name;
10089 struct cleanup *cleanups;
10090 int i;
10091
10092 gdb_assert (dwp_file->version == 1);
10093
10094 if (dwarf_read_debug)
10095 {
10096 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
10097 kind,
10098 pulongest (unit_index), hex_string (signature),
10099 dwp_file->name);
10100 }
10101
10102 /* Fetch the sections of this DWO unit.
10103 Put a limit on the number of sections we look for so that bad data
10104 doesn't cause us to loop forever. */
10105
10106 #define MAX_NR_V1_DWO_SECTIONS \
10107 (1 /* .debug_info or .debug_types */ \
10108 + 1 /* .debug_abbrev */ \
10109 + 1 /* .debug_line */ \
10110 + 1 /* .debug_loc */ \
10111 + 1 /* .debug_str_offsets */ \
10112 + 1 /* .debug_macro or .debug_macinfo */ \
10113 + 1 /* trailing zero */)
10114
10115 memset (&sections, 0, sizeof (sections));
10116 cleanups = make_cleanup (null_cleanup, 0);
10117
10118 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
10119 {
10120 asection *sectp;
10121 uint32_t section_nr =
10122 read_4_bytes (dbfd,
10123 dwp_htab->section_pool.v1.indices
10124 + (unit_index + i) * sizeof (uint32_t));
10125
10126 if (section_nr == 0)
10127 break;
10128 if (section_nr >= dwp_file->num_sections)
10129 {
10130 error (_("Dwarf Error: bad DWP hash table, section number too large"
10131 " [in module %s]"),
10132 dwp_file->name);
10133 }
10134
10135 sectp = dwp_file->elf_sections[section_nr];
10136 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
10137 {
10138 error (_("Dwarf Error: bad DWP hash table, invalid section found"
10139 " [in module %s]"),
10140 dwp_file->name);
10141 }
10142 }
10143
10144 if (i < 2
10145 || dwarf2_section_empty_p (&sections.info_or_types)
10146 || dwarf2_section_empty_p (&sections.abbrev))
10147 {
10148 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
10149 " [in module %s]"),
10150 dwp_file->name);
10151 }
10152 if (i == MAX_NR_V1_DWO_SECTIONS)
10153 {
10154 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
10155 " [in module %s]"),
10156 dwp_file->name);
10157 }
10158
10159 /* It's easier for the rest of the code if we fake a struct dwo_file and
10160 have dwo_unit "live" in that. At least for now.
10161
10162 The DWP file can be made up of a random collection of CUs and TUs.
10163 However, for each CU + set of TUs that came from the same original DWO
10164 file, we can combine them back into a virtual DWO file to save space
10165 (fewer struct dwo_file objects to allocate). Remember that for really
10166 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10167
10168 virtual_dwo_name =
10169 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
10170 get_section_id (&sections.abbrev),
10171 get_section_id (&sections.line),
10172 get_section_id (&sections.loc),
10173 get_section_id (&sections.str_offsets));
10174 make_cleanup (xfree, virtual_dwo_name);
10175 /* Can we use an existing virtual DWO file? */
10176 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
10177 /* Create one if necessary. */
10178 if (*dwo_file_slot == NULL)
10179 {
10180 if (dwarf_read_debug)
10181 {
10182 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
10183 virtual_dwo_name);
10184 }
10185 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
10186 dwo_file->dwo_name
10187 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
10188 virtual_dwo_name,
10189 strlen (virtual_dwo_name));
10190 dwo_file->comp_dir = comp_dir;
10191 dwo_file->sections.abbrev = sections.abbrev;
10192 dwo_file->sections.line = sections.line;
10193 dwo_file->sections.loc = sections.loc;
10194 dwo_file->sections.macinfo = sections.macinfo;
10195 dwo_file->sections.macro = sections.macro;
10196 dwo_file->sections.str_offsets = sections.str_offsets;
10197 /* The "str" section is global to the entire DWP file. */
10198 dwo_file->sections.str = dwp_file->sections.str;
10199 /* The info or types section is assigned below to dwo_unit,
10200 there's no need to record it in dwo_file.
10201 Also, we can't simply record type sections in dwo_file because
10202 we record a pointer into the vector in dwo_unit. As we collect more
10203 types we'll grow the vector and eventually have to reallocate space
10204 for it, invalidating all copies of pointers into the previous
10205 contents. */
10206 *dwo_file_slot = dwo_file;
10207 }
10208 else
10209 {
10210 if (dwarf_read_debug)
10211 {
10212 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
10213 virtual_dwo_name);
10214 }
10215 dwo_file = (struct dwo_file *) *dwo_file_slot;
10216 }
10217 do_cleanups (cleanups);
10218
10219 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10220 dwo_unit->dwo_file = dwo_file;
10221 dwo_unit->signature = signature;
10222 dwo_unit->section =
10223 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
10224 *dwo_unit->section = sections.info_or_types;
10225 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10226
10227 return dwo_unit;
10228 }
10229
10230 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
10231 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
10232 piece within that section used by a TU/CU, return a virtual section
10233 of just that piece. */
10234
10235 static struct dwarf2_section_info
10236 create_dwp_v2_section (struct dwarf2_section_info *section,
10237 bfd_size_type offset, bfd_size_type size)
10238 {
10239 struct dwarf2_section_info result;
10240 asection *sectp;
10241
10242 gdb_assert (section != NULL);
10243 gdb_assert (!section->is_virtual);
10244
10245 memset (&result, 0, sizeof (result));
10246 result.s.containing_section = section;
10247 result.is_virtual = 1;
10248
10249 if (size == 0)
10250 return result;
10251
10252 sectp = get_section_bfd_section (section);
10253
10254 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
10255 bounds of the real section. This is a pretty-rare event, so just
10256 flag an error (easier) instead of a warning and trying to cope. */
10257 if (sectp == NULL
10258 || offset + size > bfd_get_section_size (sectp))
10259 {
10260 bfd *abfd = sectp->owner;
10261
10262 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
10263 " in section %s [in module %s]"),
10264 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
10265 objfile_name (dwarf2_per_objfile->objfile));
10266 }
10267
10268 result.virtual_offset = offset;
10269 result.size = size;
10270 return result;
10271 }
10272
10273 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10274 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10275 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10276 This is for DWP version 2 files. */
10277
10278 static struct dwo_unit *
10279 create_dwo_unit_in_dwp_v2 (struct dwp_file *dwp_file,
10280 uint32_t unit_index,
10281 const char *comp_dir,
10282 ULONGEST signature, int is_debug_types)
10283 {
10284 struct objfile *objfile = dwarf2_per_objfile->objfile;
10285 const struct dwp_hash_table *dwp_htab =
10286 is_debug_types ? dwp_file->tus : dwp_file->cus;
10287 bfd *dbfd = dwp_file->dbfd;
10288 const char *kind = is_debug_types ? "TU" : "CU";
10289 struct dwo_file *dwo_file;
10290 struct dwo_unit *dwo_unit;
10291 struct virtual_v2_dwo_sections sections;
10292 void **dwo_file_slot;
10293 char *virtual_dwo_name;
10294 struct cleanup *cleanups;
10295 int i;
10296
10297 gdb_assert (dwp_file->version == 2);
10298
10299 if (dwarf_read_debug)
10300 {
10301 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
10302 kind,
10303 pulongest (unit_index), hex_string (signature),
10304 dwp_file->name);
10305 }
10306
10307 /* Fetch the section offsets of this DWO unit. */
10308
10309 memset (&sections, 0, sizeof (sections));
10310 cleanups = make_cleanup (null_cleanup, 0);
10311
10312 for (i = 0; i < dwp_htab->nr_columns; ++i)
10313 {
10314 uint32_t offset = read_4_bytes (dbfd,
10315 dwp_htab->section_pool.v2.offsets
10316 + (((unit_index - 1) * dwp_htab->nr_columns
10317 + i)
10318 * sizeof (uint32_t)));
10319 uint32_t size = read_4_bytes (dbfd,
10320 dwp_htab->section_pool.v2.sizes
10321 + (((unit_index - 1) * dwp_htab->nr_columns
10322 + i)
10323 * sizeof (uint32_t)));
10324
10325 switch (dwp_htab->section_pool.v2.section_ids[i])
10326 {
10327 case DW_SECT_INFO:
10328 case DW_SECT_TYPES:
10329 sections.info_or_types_offset = offset;
10330 sections.info_or_types_size = size;
10331 break;
10332 case DW_SECT_ABBREV:
10333 sections.abbrev_offset = offset;
10334 sections.abbrev_size = size;
10335 break;
10336 case DW_SECT_LINE:
10337 sections.line_offset = offset;
10338 sections.line_size = size;
10339 break;
10340 case DW_SECT_LOC:
10341 sections.loc_offset = offset;
10342 sections.loc_size = size;
10343 break;
10344 case DW_SECT_STR_OFFSETS:
10345 sections.str_offsets_offset = offset;
10346 sections.str_offsets_size = size;
10347 break;
10348 case DW_SECT_MACINFO:
10349 sections.macinfo_offset = offset;
10350 sections.macinfo_size = size;
10351 break;
10352 case DW_SECT_MACRO:
10353 sections.macro_offset = offset;
10354 sections.macro_size = size;
10355 break;
10356 }
10357 }
10358
10359 /* It's easier for the rest of the code if we fake a struct dwo_file and
10360 have dwo_unit "live" in that. At least for now.
10361
10362 The DWP file can be made up of a random collection of CUs and TUs.
10363 However, for each CU + set of TUs that came from the same original DWO
10364 file, we can combine them back into a virtual DWO file to save space
10365 (fewer struct dwo_file objects to allocate). Remember that for really
10366 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10367
10368 virtual_dwo_name =
10369 xstrprintf ("virtual-dwo/%ld-%ld-%ld-%ld",
10370 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
10371 (long) (sections.line_size ? sections.line_offset : 0),
10372 (long) (sections.loc_size ? sections.loc_offset : 0),
10373 (long) (sections.str_offsets_size
10374 ? sections.str_offsets_offset : 0));
10375 make_cleanup (xfree, virtual_dwo_name);
10376 /* Can we use an existing virtual DWO file? */
10377 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
10378 /* Create one if necessary. */
10379 if (*dwo_file_slot == NULL)
10380 {
10381 if (dwarf_read_debug)
10382 {
10383 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
10384 virtual_dwo_name);
10385 }
10386 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
10387 dwo_file->dwo_name
10388 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
10389 virtual_dwo_name,
10390 strlen (virtual_dwo_name));
10391 dwo_file->comp_dir = comp_dir;
10392 dwo_file->sections.abbrev =
10393 create_dwp_v2_section (&dwp_file->sections.abbrev,
10394 sections.abbrev_offset, sections.abbrev_size);
10395 dwo_file->sections.line =
10396 create_dwp_v2_section (&dwp_file->sections.line,
10397 sections.line_offset, sections.line_size);
10398 dwo_file->sections.loc =
10399 create_dwp_v2_section (&dwp_file->sections.loc,
10400 sections.loc_offset, sections.loc_size);
10401 dwo_file->sections.macinfo =
10402 create_dwp_v2_section (&dwp_file->sections.macinfo,
10403 sections.macinfo_offset, sections.macinfo_size);
10404 dwo_file->sections.macro =
10405 create_dwp_v2_section (&dwp_file->sections.macro,
10406 sections.macro_offset, sections.macro_size);
10407 dwo_file->sections.str_offsets =
10408 create_dwp_v2_section (&dwp_file->sections.str_offsets,
10409 sections.str_offsets_offset,
10410 sections.str_offsets_size);
10411 /* The "str" section is global to the entire DWP file. */
10412 dwo_file->sections.str = dwp_file->sections.str;
10413 /* The info or types section is assigned below to dwo_unit,
10414 there's no need to record it in dwo_file.
10415 Also, we can't simply record type sections in dwo_file because
10416 we record a pointer into the vector in dwo_unit. As we collect more
10417 types we'll grow the vector and eventually have to reallocate space
10418 for it, invalidating all copies of pointers into the previous
10419 contents. */
10420 *dwo_file_slot = dwo_file;
10421 }
10422 else
10423 {
10424 if (dwarf_read_debug)
10425 {
10426 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
10427 virtual_dwo_name);
10428 }
10429 dwo_file = (struct dwo_file *) *dwo_file_slot;
10430 }
10431 do_cleanups (cleanups);
10432
10433 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10434 dwo_unit->dwo_file = dwo_file;
10435 dwo_unit->signature = signature;
10436 dwo_unit->section =
10437 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
10438 *dwo_unit->section = create_dwp_v2_section (is_debug_types
10439 ? &dwp_file->sections.types
10440 : &dwp_file->sections.info,
10441 sections.info_or_types_offset,
10442 sections.info_or_types_size);
10443 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10444
10445 return dwo_unit;
10446 }
10447
10448 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
10449 Returns NULL if the signature isn't found. */
10450
10451 static struct dwo_unit *
10452 lookup_dwo_unit_in_dwp (struct dwp_file *dwp_file, const char *comp_dir,
10453 ULONGEST signature, int is_debug_types)
10454 {
10455 const struct dwp_hash_table *dwp_htab =
10456 is_debug_types ? dwp_file->tus : dwp_file->cus;
10457 bfd *dbfd = dwp_file->dbfd;
10458 uint32_t mask = dwp_htab->nr_slots - 1;
10459 uint32_t hash = signature & mask;
10460 uint32_t hash2 = ((signature >> 32) & mask) | 1;
10461 unsigned int i;
10462 void **slot;
10463 struct dwo_unit find_dwo_cu;
10464
10465 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
10466 find_dwo_cu.signature = signature;
10467 slot = htab_find_slot (is_debug_types
10468 ? dwp_file->loaded_tus
10469 : dwp_file->loaded_cus,
10470 &find_dwo_cu, INSERT);
10471
10472 if (*slot != NULL)
10473 return (struct dwo_unit *) *slot;
10474
10475 /* Use a for loop so that we don't loop forever on bad debug info. */
10476 for (i = 0; i < dwp_htab->nr_slots; ++i)
10477 {
10478 ULONGEST signature_in_table;
10479
10480 signature_in_table =
10481 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
10482 if (signature_in_table == signature)
10483 {
10484 uint32_t unit_index =
10485 read_4_bytes (dbfd,
10486 dwp_htab->unit_table + hash * sizeof (uint32_t));
10487
10488 if (dwp_file->version == 1)
10489 {
10490 *slot = create_dwo_unit_in_dwp_v1 (dwp_file, unit_index,
10491 comp_dir, signature,
10492 is_debug_types);
10493 }
10494 else
10495 {
10496 *slot = create_dwo_unit_in_dwp_v2 (dwp_file, unit_index,
10497 comp_dir, signature,
10498 is_debug_types);
10499 }
10500 return (struct dwo_unit *) *slot;
10501 }
10502 if (signature_in_table == 0)
10503 return NULL;
10504 hash = (hash + hash2) & mask;
10505 }
10506
10507 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
10508 " [in module %s]"),
10509 dwp_file->name);
10510 }
10511
10512 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
10513 Open the file specified by FILE_NAME and hand it off to BFD for
10514 preliminary analysis. Return a newly initialized bfd *, which
10515 includes a canonicalized copy of FILE_NAME.
10516 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
10517 SEARCH_CWD is true if the current directory is to be searched.
10518 It will be searched before debug-file-directory.
10519 If successful, the file is added to the bfd include table of the
10520 objfile's bfd (see gdb_bfd_record_inclusion).
10521 If unable to find/open the file, return NULL.
10522 NOTE: This function is derived from symfile_bfd_open. */
10523
10524 static gdb_bfd_ref_ptr
10525 try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
10526 {
10527 int desc, flags;
10528 char *absolute_name;
10529 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
10530 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
10531 to debug_file_directory. */
10532 char *search_path;
10533 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
10534
10535 if (search_cwd)
10536 {
10537 if (*debug_file_directory != '\0')
10538 search_path = concat (".", dirname_separator_string,
10539 debug_file_directory, (char *) NULL);
10540 else
10541 search_path = xstrdup (".");
10542 }
10543 else
10544 search_path = xstrdup (debug_file_directory);
10545
10546 flags = OPF_RETURN_REALPATH;
10547 if (is_dwp)
10548 flags |= OPF_SEARCH_IN_PATH;
10549 desc = openp (search_path, flags, file_name,
10550 O_RDONLY | O_BINARY, &absolute_name);
10551 xfree (search_path);
10552 if (desc < 0)
10553 return NULL;
10554
10555 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name, gnutarget, desc));
10556 xfree (absolute_name);
10557 if (sym_bfd == NULL)
10558 return NULL;
10559 bfd_set_cacheable (sym_bfd.get (), 1);
10560
10561 if (!bfd_check_format (sym_bfd.get (), bfd_object))
10562 return NULL;
10563
10564 /* Success. Record the bfd as having been included by the objfile's bfd.
10565 This is important because things like demangled_names_hash lives in the
10566 objfile's per_bfd space and may have references to things like symbol
10567 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
10568 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
10569
10570 return sym_bfd;
10571 }
10572
10573 /* Try to open DWO file FILE_NAME.
10574 COMP_DIR is the DW_AT_comp_dir attribute.
10575 The result is the bfd handle of the file.
10576 If there is a problem finding or opening the file, return NULL.
10577 Upon success, the canonicalized path of the file is stored in the bfd,
10578 same as symfile_bfd_open. */
10579
10580 static gdb_bfd_ref_ptr
10581 open_dwo_file (const char *file_name, const char *comp_dir)
10582 {
10583 if (IS_ABSOLUTE_PATH (file_name))
10584 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
10585
10586 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
10587
10588 if (comp_dir != NULL)
10589 {
10590 char *path_to_try = concat (comp_dir, SLASH_STRING,
10591 file_name, (char *) NULL);
10592
10593 /* NOTE: If comp_dir is a relative path, this will also try the
10594 search path, which seems useful. */
10595 gdb_bfd_ref_ptr abfd (try_open_dwop_file (path_to_try, 0 /*is_dwp*/,
10596 1 /*search_cwd*/));
10597 xfree (path_to_try);
10598 if (abfd != NULL)
10599 return abfd;
10600 }
10601
10602 /* That didn't work, try debug-file-directory, which, despite its name,
10603 is a list of paths. */
10604
10605 if (*debug_file_directory == '\0')
10606 return NULL;
10607
10608 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
10609 }
10610
10611 /* This function is mapped across the sections and remembers the offset and
10612 size of each of the DWO debugging sections we are interested in. */
10613
10614 static void
10615 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
10616 {
10617 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
10618 const struct dwop_section_names *names = &dwop_section_names;
10619
10620 if (section_is_p (sectp->name, &names->abbrev_dwo))
10621 {
10622 dwo_sections->abbrev.s.section = sectp;
10623 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
10624 }
10625 else if (section_is_p (sectp->name, &names->info_dwo))
10626 {
10627 dwo_sections->info.s.section = sectp;
10628 dwo_sections->info.size = bfd_get_section_size (sectp);
10629 }
10630 else if (section_is_p (sectp->name, &names->line_dwo))
10631 {
10632 dwo_sections->line.s.section = sectp;
10633 dwo_sections->line.size = bfd_get_section_size (sectp);
10634 }
10635 else if (section_is_p (sectp->name, &names->loc_dwo))
10636 {
10637 dwo_sections->loc.s.section = sectp;
10638 dwo_sections->loc.size = bfd_get_section_size (sectp);
10639 }
10640 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10641 {
10642 dwo_sections->macinfo.s.section = sectp;
10643 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
10644 }
10645 else if (section_is_p (sectp->name, &names->macro_dwo))
10646 {
10647 dwo_sections->macro.s.section = sectp;
10648 dwo_sections->macro.size = bfd_get_section_size (sectp);
10649 }
10650 else if (section_is_p (sectp->name, &names->str_dwo))
10651 {
10652 dwo_sections->str.s.section = sectp;
10653 dwo_sections->str.size = bfd_get_section_size (sectp);
10654 }
10655 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10656 {
10657 dwo_sections->str_offsets.s.section = sectp;
10658 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
10659 }
10660 else if (section_is_p (sectp->name, &names->types_dwo))
10661 {
10662 struct dwarf2_section_info type_section;
10663
10664 memset (&type_section, 0, sizeof (type_section));
10665 type_section.s.section = sectp;
10666 type_section.size = bfd_get_section_size (sectp);
10667 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
10668 &type_section);
10669 }
10670 }
10671
10672 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
10673 by PER_CU. This is for the non-DWP case.
10674 The result is NULL if DWO_NAME can't be found. */
10675
10676 static struct dwo_file *
10677 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
10678 const char *dwo_name, const char *comp_dir)
10679 {
10680 struct objfile *objfile = dwarf2_per_objfile->objfile;
10681 struct dwo_file *dwo_file;
10682 struct cleanup *cleanups;
10683
10684 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwo_name, comp_dir));
10685 if (dbfd == NULL)
10686 {
10687 if (dwarf_read_debug)
10688 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
10689 return NULL;
10690 }
10691 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
10692 dwo_file->dwo_name = dwo_name;
10693 dwo_file->comp_dir = comp_dir;
10694 dwo_file->dbfd = dbfd.release ();
10695
10696 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
10697
10698 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
10699 &dwo_file->sections);
10700
10701 dwo_file->cu = create_dwo_cu (dwo_file);
10702
10703 create_debug_types_hash_table (dwo_file, dwo_file->sections.types,
10704 dwo_file->tus);
10705
10706 discard_cleanups (cleanups);
10707
10708 if (dwarf_read_debug)
10709 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
10710
10711 return dwo_file;
10712 }
10713
10714 /* This function is mapped across the sections and remembers the offset and
10715 size of each of the DWP debugging sections common to version 1 and 2 that
10716 we are interested in. */
10717
10718 static void
10719 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
10720 void *dwp_file_ptr)
10721 {
10722 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
10723 const struct dwop_section_names *names = &dwop_section_names;
10724 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
10725
10726 /* Record the ELF section number for later lookup: this is what the
10727 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
10728 gdb_assert (elf_section_nr < dwp_file->num_sections);
10729 dwp_file->elf_sections[elf_section_nr] = sectp;
10730
10731 /* Look for specific sections that we need. */
10732 if (section_is_p (sectp->name, &names->str_dwo))
10733 {
10734 dwp_file->sections.str.s.section = sectp;
10735 dwp_file->sections.str.size = bfd_get_section_size (sectp);
10736 }
10737 else if (section_is_p (sectp->name, &names->cu_index))
10738 {
10739 dwp_file->sections.cu_index.s.section = sectp;
10740 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
10741 }
10742 else if (section_is_p (sectp->name, &names->tu_index))
10743 {
10744 dwp_file->sections.tu_index.s.section = sectp;
10745 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
10746 }
10747 }
10748
10749 /* This function is mapped across the sections and remembers the offset and
10750 size of each of the DWP version 2 debugging sections that we are interested
10751 in. This is split into a separate function because we don't know if we
10752 have version 1 or 2 until we parse the cu_index/tu_index sections. */
10753
10754 static void
10755 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
10756 {
10757 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
10758 const struct dwop_section_names *names = &dwop_section_names;
10759 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
10760
10761 /* Record the ELF section number for later lookup: this is what the
10762 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
10763 gdb_assert (elf_section_nr < dwp_file->num_sections);
10764 dwp_file->elf_sections[elf_section_nr] = sectp;
10765
10766 /* Look for specific sections that we need. */
10767 if (section_is_p (sectp->name, &names->abbrev_dwo))
10768 {
10769 dwp_file->sections.abbrev.s.section = sectp;
10770 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
10771 }
10772 else if (section_is_p (sectp->name, &names->info_dwo))
10773 {
10774 dwp_file->sections.info.s.section = sectp;
10775 dwp_file->sections.info.size = bfd_get_section_size (sectp);
10776 }
10777 else if (section_is_p (sectp->name, &names->line_dwo))
10778 {
10779 dwp_file->sections.line.s.section = sectp;
10780 dwp_file->sections.line.size = bfd_get_section_size (sectp);
10781 }
10782 else if (section_is_p (sectp->name, &names->loc_dwo))
10783 {
10784 dwp_file->sections.loc.s.section = sectp;
10785 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
10786 }
10787 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10788 {
10789 dwp_file->sections.macinfo.s.section = sectp;
10790 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
10791 }
10792 else if (section_is_p (sectp->name, &names->macro_dwo))
10793 {
10794 dwp_file->sections.macro.s.section = sectp;
10795 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
10796 }
10797 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10798 {
10799 dwp_file->sections.str_offsets.s.section = sectp;
10800 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
10801 }
10802 else if (section_is_p (sectp->name, &names->types_dwo))
10803 {
10804 dwp_file->sections.types.s.section = sectp;
10805 dwp_file->sections.types.size = bfd_get_section_size (sectp);
10806 }
10807 }
10808
10809 /* Hash function for dwp_file loaded CUs/TUs. */
10810
10811 static hashval_t
10812 hash_dwp_loaded_cutus (const void *item)
10813 {
10814 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
10815
10816 /* This drops the top 32 bits of the signature, but is ok for a hash. */
10817 return dwo_unit->signature;
10818 }
10819
10820 /* Equality function for dwp_file loaded CUs/TUs. */
10821
10822 static int
10823 eq_dwp_loaded_cutus (const void *a, const void *b)
10824 {
10825 const struct dwo_unit *dua = (const struct dwo_unit *) a;
10826 const struct dwo_unit *dub = (const struct dwo_unit *) b;
10827
10828 return dua->signature == dub->signature;
10829 }
10830
10831 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
10832
10833 static htab_t
10834 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
10835 {
10836 return htab_create_alloc_ex (3,
10837 hash_dwp_loaded_cutus,
10838 eq_dwp_loaded_cutus,
10839 NULL,
10840 &objfile->objfile_obstack,
10841 hashtab_obstack_allocate,
10842 dummy_obstack_deallocate);
10843 }
10844
10845 /* Try to open DWP file FILE_NAME.
10846 The result is the bfd handle of the file.
10847 If there is a problem finding or opening the file, return NULL.
10848 Upon success, the canonicalized path of the file is stored in the bfd,
10849 same as symfile_bfd_open. */
10850
10851 static gdb_bfd_ref_ptr
10852 open_dwp_file (const char *file_name)
10853 {
10854 gdb_bfd_ref_ptr abfd (try_open_dwop_file (file_name, 1 /*is_dwp*/,
10855 1 /*search_cwd*/));
10856 if (abfd != NULL)
10857 return abfd;
10858
10859 /* Work around upstream bug 15652.
10860 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
10861 [Whether that's a "bug" is debatable, but it is getting in our way.]
10862 We have no real idea where the dwp file is, because gdb's realpath-ing
10863 of the executable's path may have discarded the needed info.
10864 [IWBN if the dwp file name was recorded in the executable, akin to
10865 .gnu_debuglink, but that doesn't exist yet.]
10866 Strip the directory from FILE_NAME and search again. */
10867 if (*debug_file_directory != '\0')
10868 {
10869 /* Don't implicitly search the current directory here.
10870 If the user wants to search "." to handle this case,
10871 it must be added to debug-file-directory. */
10872 return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
10873 0 /*search_cwd*/);
10874 }
10875
10876 return NULL;
10877 }
10878
10879 /* Initialize the use of the DWP file for the current objfile.
10880 By convention the name of the DWP file is ${objfile}.dwp.
10881 The result is NULL if it can't be found. */
10882
10883 static struct dwp_file *
10884 open_and_init_dwp_file (void)
10885 {
10886 struct objfile *objfile = dwarf2_per_objfile->objfile;
10887 struct dwp_file *dwp_file;
10888 char *dwp_name;
10889 struct cleanup *cleanups = make_cleanup (null_cleanup, 0);
10890
10891 /* Try to find first .dwp for the binary file before any symbolic links
10892 resolving. */
10893
10894 /* If the objfile is a debug file, find the name of the real binary
10895 file and get the name of dwp file from there. */
10896 if (objfile->separate_debug_objfile_backlink != NULL)
10897 {
10898 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
10899 const char *backlink_basename = lbasename (backlink->original_name);
10900 char *debug_dirname = ldirname (objfile->original_name);
10901
10902 make_cleanup (xfree, debug_dirname);
10903 dwp_name = xstrprintf ("%s%s%s.dwp", debug_dirname,
10904 SLASH_STRING, backlink_basename);
10905 }
10906 else
10907 dwp_name = xstrprintf ("%s.dwp", objfile->original_name);
10908 make_cleanup (xfree, dwp_name);
10909
10910 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwp_name));
10911 if (dbfd == NULL
10912 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
10913 {
10914 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
10915 dwp_name = xstrprintf ("%s.dwp", objfile_name (objfile));
10916 make_cleanup (xfree, dwp_name);
10917 dbfd = open_dwp_file (dwp_name);
10918 }
10919
10920 if (dbfd == NULL)
10921 {
10922 if (dwarf_read_debug)
10923 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
10924 do_cleanups (cleanups);
10925 return NULL;
10926 }
10927 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
10928 dwp_file->name = bfd_get_filename (dbfd.get ());
10929 dwp_file->dbfd = dbfd.release ();
10930 do_cleanups (cleanups);
10931
10932 /* +1: section 0 is unused */
10933 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
10934 dwp_file->elf_sections =
10935 OBSTACK_CALLOC (&objfile->objfile_obstack,
10936 dwp_file->num_sections, asection *);
10937
10938 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
10939 dwp_file);
10940
10941 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
10942
10943 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
10944
10945 /* The DWP file version is stored in the hash table. Oh well. */
10946 if (dwp_file->cus->version != dwp_file->tus->version)
10947 {
10948 /* Technically speaking, we should try to limp along, but this is
10949 pretty bizarre. We use pulongest here because that's the established
10950 portability solution (e.g, we cannot use %u for uint32_t). */
10951 error (_("Dwarf Error: DWP file CU version %s doesn't match"
10952 " TU version %s [in DWP file %s]"),
10953 pulongest (dwp_file->cus->version),
10954 pulongest (dwp_file->tus->version), dwp_name);
10955 }
10956 dwp_file->version = dwp_file->cus->version;
10957
10958 if (dwp_file->version == 2)
10959 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
10960 dwp_file);
10961
10962 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
10963 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
10964
10965 if (dwarf_read_debug)
10966 {
10967 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
10968 fprintf_unfiltered (gdb_stdlog,
10969 " %s CUs, %s TUs\n",
10970 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
10971 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
10972 }
10973
10974 return dwp_file;
10975 }
10976
10977 /* Wrapper around open_and_init_dwp_file, only open it once. */
10978
10979 static struct dwp_file *
10980 get_dwp_file (void)
10981 {
10982 if (! dwarf2_per_objfile->dwp_checked)
10983 {
10984 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
10985 dwarf2_per_objfile->dwp_checked = 1;
10986 }
10987 return dwarf2_per_objfile->dwp_file;
10988 }
10989
10990 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
10991 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
10992 or in the DWP file for the objfile, referenced by THIS_UNIT.
10993 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
10994 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
10995
10996 This is called, for example, when wanting to read a variable with a
10997 complex location. Therefore we don't want to do file i/o for every call.
10998 Therefore we don't want to look for a DWO file on every call.
10999 Therefore we first see if we've already seen SIGNATURE in a DWP file,
11000 then we check if we've already seen DWO_NAME, and only THEN do we check
11001 for a DWO file.
11002
11003 The result is a pointer to the dwo_unit object or NULL if we didn't find it
11004 (dwo_id mismatch or couldn't find the DWO/DWP file). */
11005
11006 static struct dwo_unit *
11007 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
11008 const char *dwo_name, const char *comp_dir,
11009 ULONGEST signature, int is_debug_types)
11010 {
11011 struct objfile *objfile = dwarf2_per_objfile->objfile;
11012 const char *kind = is_debug_types ? "TU" : "CU";
11013 void **dwo_file_slot;
11014 struct dwo_file *dwo_file;
11015 struct dwp_file *dwp_file;
11016
11017 /* First see if there's a DWP file.
11018 If we have a DWP file but didn't find the DWO inside it, don't
11019 look for the original DWO file. It makes gdb behave differently
11020 depending on whether one is debugging in the build tree. */
11021
11022 dwp_file = get_dwp_file ();
11023 if (dwp_file != NULL)
11024 {
11025 const struct dwp_hash_table *dwp_htab =
11026 is_debug_types ? dwp_file->tus : dwp_file->cus;
11027
11028 if (dwp_htab != NULL)
11029 {
11030 struct dwo_unit *dwo_cutu =
11031 lookup_dwo_unit_in_dwp (dwp_file, comp_dir,
11032 signature, is_debug_types);
11033
11034 if (dwo_cutu != NULL)
11035 {
11036 if (dwarf_read_debug)
11037 {
11038 fprintf_unfiltered (gdb_stdlog,
11039 "Virtual DWO %s %s found: @%s\n",
11040 kind, hex_string (signature),
11041 host_address_to_string (dwo_cutu));
11042 }
11043 return dwo_cutu;
11044 }
11045 }
11046 }
11047 else
11048 {
11049 /* No DWP file, look for the DWO file. */
11050
11051 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
11052 if (*dwo_file_slot == NULL)
11053 {
11054 /* Read in the file and build a table of the CUs/TUs it contains. */
11055 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
11056 }
11057 /* NOTE: This will be NULL if unable to open the file. */
11058 dwo_file = (struct dwo_file *) *dwo_file_slot;
11059
11060 if (dwo_file != NULL)
11061 {
11062 struct dwo_unit *dwo_cutu = NULL;
11063
11064 if (is_debug_types && dwo_file->tus)
11065 {
11066 struct dwo_unit find_dwo_cutu;
11067
11068 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
11069 find_dwo_cutu.signature = signature;
11070 dwo_cutu
11071 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
11072 }
11073 else if (!is_debug_types && dwo_file->cu)
11074 {
11075 if (signature == dwo_file->cu->signature)
11076 dwo_cutu = dwo_file->cu;
11077 }
11078
11079 if (dwo_cutu != NULL)
11080 {
11081 if (dwarf_read_debug)
11082 {
11083 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
11084 kind, dwo_name, hex_string (signature),
11085 host_address_to_string (dwo_cutu));
11086 }
11087 return dwo_cutu;
11088 }
11089 }
11090 }
11091
11092 /* We didn't find it. This could mean a dwo_id mismatch, or
11093 someone deleted the DWO/DWP file, or the search path isn't set up
11094 correctly to find the file. */
11095
11096 if (dwarf_read_debug)
11097 {
11098 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
11099 kind, dwo_name, hex_string (signature));
11100 }
11101
11102 /* This is a warning and not a complaint because it can be caused by
11103 pilot error (e.g., user accidentally deleting the DWO). */
11104 {
11105 /* Print the name of the DWP file if we looked there, helps the user
11106 better diagnose the problem. */
11107 char *dwp_text = NULL;
11108 struct cleanup *cleanups;
11109
11110 if (dwp_file != NULL)
11111 dwp_text = xstrprintf (" [in DWP file %s]", lbasename (dwp_file->name));
11112 cleanups = make_cleanup (xfree, dwp_text);
11113
11114 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset 0x%x"
11115 " [in module %s]"),
11116 kind, dwo_name, hex_string (signature),
11117 dwp_text != NULL ? dwp_text : "",
11118 this_unit->is_debug_types ? "TU" : "CU",
11119 this_unit->offset.sect_off, objfile_name (objfile));
11120
11121 do_cleanups (cleanups);
11122 }
11123 return NULL;
11124 }
11125
11126 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
11127 See lookup_dwo_cutu_unit for details. */
11128
11129 static struct dwo_unit *
11130 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
11131 const char *dwo_name, const char *comp_dir,
11132 ULONGEST signature)
11133 {
11134 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
11135 }
11136
11137 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
11138 See lookup_dwo_cutu_unit for details. */
11139
11140 static struct dwo_unit *
11141 lookup_dwo_type_unit (struct signatured_type *this_tu,
11142 const char *dwo_name, const char *comp_dir)
11143 {
11144 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
11145 }
11146
11147 /* Traversal function for queue_and_load_all_dwo_tus. */
11148
11149 static int
11150 queue_and_load_dwo_tu (void **slot, void *info)
11151 {
11152 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
11153 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
11154 ULONGEST signature = dwo_unit->signature;
11155 struct signatured_type *sig_type =
11156 lookup_dwo_signatured_type (per_cu->cu, signature);
11157
11158 if (sig_type != NULL)
11159 {
11160 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
11161
11162 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
11163 a real dependency of PER_CU on SIG_TYPE. That is detected later
11164 while processing PER_CU. */
11165 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
11166 load_full_type_unit (sig_cu);
11167 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
11168 }
11169
11170 return 1;
11171 }
11172
11173 /* Queue all TUs contained in the DWO of PER_CU to be read in.
11174 The DWO may have the only definition of the type, though it may not be
11175 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
11176 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
11177
11178 static void
11179 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
11180 {
11181 struct dwo_unit *dwo_unit;
11182 struct dwo_file *dwo_file;
11183
11184 gdb_assert (!per_cu->is_debug_types);
11185 gdb_assert (get_dwp_file () == NULL);
11186 gdb_assert (per_cu->cu != NULL);
11187
11188 dwo_unit = per_cu->cu->dwo_unit;
11189 gdb_assert (dwo_unit != NULL);
11190
11191 dwo_file = dwo_unit->dwo_file;
11192 if (dwo_file->tus != NULL)
11193 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
11194 }
11195
11196 /* Free all resources associated with DWO_FILE.
11197 Close the DWO file and munmap the sections.
11198 All memory should be on the objfile obstack. */
11199
11200 static void
11201 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
11202 {
11203
11204 /* Note: dbfd is NULL for virtual DWO files. */
11205 gdb_bfd_unref (dwo_file->dbfd);
11206
11207 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
11208 }
11209
11210 /* Wrapper for free_dwo_file for use in cleanups. */
11211
11212 static void
11213 free_dwo_file_cleanup (void *arg)
11214 {
11215 struct dwo_file *dwo_file = (struct dwo_file *) arg;
11216 struct objfile *objfile = dwarf2_per_objfile->objfile;
11217
11218 free_dwo_file (dwo_file, objfile);
11219 }
11220
11221 /* Traversal function for free_dwo_files. */
11222
11223 static int
11224 free_dwo_file_from_slot (void **slot, void *info)
11225 {
11226 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
11227 struct objfile *objfile = (struct objfile *) info;
11228
11229 free_dwo_file (dwo_file, objfile);
11230
11231 return 1;
11232 }
11233
11234 /* Free all resources associated with DWO_FILES. */
11235
11236 static void
11237 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
11238 {
11239 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
11240 }
11241 \f
11242 /* Read in various DIEs. */
11243
11244 /* qsort helper for inherit_abstract_dies. */
11245
11246 static int
11247 unsigned_int_compar (const void *ap, const void *bp)
11248 {
11249 unsigned int a = *(unsigned int *) ap;
11250 unsigned int b = *(unsigned int *) bp;
11251
11252 return (a > b) - (b > a);
11253 }
11254
11255 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
11256 Inherit only the children of the DW_AT_abstract_origin DIE not being
11257 already referenced by DW_AT_abstract_origin from the children of the
11258 current DIE. */
11259
11260 static void
11261 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
11262 {
11263 struct die_info *child_die;
11264 unsigned die_children_count;
11265 /* CU offsets which were referenced by children of the current DIE. */
11266 sect_offset *offsets;
11267 sect_offset *offsets_end, *offsetp;
11268 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
11269 struct die_info *origin_die;
11270 /* Iterator of the ORIGIN_DIE children. */
11271 struct die_info *origin_child_die;
11272 struct cleanup *cleanups;
11273 struct attribute *attr;
11274 struct dwarf2_cu *origin_cu;
11275 struct pending **origin_previous_list_in_scope;
11276
11277 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
11278 if (!attr)
11279 return;
11280
11281 /* Note that following die references may follow to a die in a
11282 different cu. */
11283
11284 origin_cu = cu;
11285 origin_die = follow_die_ref (die, attr, &origin_cu);
11286
11287 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
11288 symbols in. */
11289 origin_previous_list_in_scope = origin_cu->list_in_scope;
11290 origin_cu->list_in_scope = cu->list_in_scope;
11291
11292 if (die->tag != origin_die->tag
11293 && !(die->tag == DW_TAG_inlined_subroutine
11294 && origin_die->tag == DW_TAG_subprogram))
11295 complaint (&symfile_complaints,
11296 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
11297 die->offset.sect_off, origin_die->offset.sect_off);
11298
11299 child_die = die->child;
11300 die_children_count = 0;
11301 while (child_die && child_die->tag)
11302 {
11303 child_die = sibling_die (child_die);
11304 die_children_count++;
11305 }
11306 offsets = XNEWVEC (sect_offset, die_children_count);
11307 cleanups = make_cleanup (xfree, offsets);
11308
11309 offsets_end = offsets;
11310 for (child_die = die->child;
11311 child_die && child_die->tag;
11312 child_die = sibling_die (child_die))
11313 {
11314 struct die_info *child_origin_die;
11315 struct dwarf2_cu *child_origin_cu;
11316
11317 /* We are trying to process concrete instance entries:
11318 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
11319 it's not relevant to our analysis here. i.e. detecting DIEs that are
11320 present in the abstract instance but not referenced in the concrete
11321 one. */
11322 if (child_die->tag == DW_TAG_call_site
11323 || child_die->tag == DW_TAG_GNU_call_site)
11324 continue;
11325
11326 /* For each CHILD_DIE, find the corresponding child of
11327 ORIGIN_DIE. If there is more than one layer of
11328 DW_AT_abstract_origin, follow them all; there shouldn't be,
11329 but GCC versions at least through 4.4 generate this (GCC PR
11330 40573). */
11331 child_origin_die = child_die;
11332 child_origin_cu = cu;
11333 while (1)
11334 {
11335 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
11336 child_origin_cu);
11337 if (attr == NULL)
11338 break;
11339 child_origin_die = follow_die_ref (child_origin_die, attr,
11340 &child_origin_cu);
11341 }
11342
11343 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
11344 counterpart may exist. */
11345 if (child_origin_die != child_die)
11346 {
11347 if (child_die->tag != child_origin_die->tag
11348 && !(child_die->tag == DW_TAG_inlined_subroutine
11349 && child_origin_die->tag == DW_TAG_subprogram))
11350 complaint (&symfile_complaints,
11351 _("Child DIE 0x%x and its abstract origin 0x%x have "
11352 "different tags"), child_die->offset.sect_off,
11353 child_origin_die->offset.sect_off);
11354 if (child_origin_die->parent != origin_die)
11355 complaint (&symfile_complaints,
11356 _("Child DIE 0x%x and its abstract origin 0x%x have "
11357 "different parents"), child_die->offset.sect_off,
11358 child_origin_die->offset.sect_off);
11359 else
11360 *offsets_end++ = child_origin_die->offset;
11361 }
11362 }
11363 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
11364 unsigned_int_compar);
11365 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
11366 if (offsetp[-1].sect_off == offsetp->sect_off)
11367 complaint (&symfile_complaints,
11368 _("Multiple children of DIE 0x%x refer "
11369 "to DIE 0x%x as their abstract origin"),
11370 die->offset.sect_off, offsetp->sect_off);
11371
11372 offsetp = offsets;
11373 origin_child_die = origin_die->child;
11374 while (origin_child_die && origin_child_die->tag)
11375 {
11376 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
11377 while (offsetp < offsets_end
11378 && offsetp->sect_off < origin_child_die->offset.sect_off)
11379 offsetp++;
11380 if (offsetp >= offsets_end
11381 || offsetp->sect_off > origin_child_die->offset.sect_off)
11382 {
11383 /* Found that ORIGIN_CHILD_DIE is really not referenced.
11384 Check whether we're already processing ORIGIN_CHILD_DIE.
11385 This can happen with mutually referenced abstract_origins.
11386 PR 16581. */
11387 if (!origin_child_die->in_process)
11388 process_die (origin_child_die, origin_cu);
11389 }
11390 origin_child_die = sibling_die (origin_child_die);
11391 }
11392 origin_cu->list_in_scope = origin_previous_list_in_scope;
11393
11394 do_cleanups (cleanups);
11395 }
11396
11397 static void
11398 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
11399 {
11400 struct objfile *objfile = cu->objfile;
11401 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11402 struct context_stack *newobj;
11403 CORE_ADDR lowpc;
11404 CORE_ADDR highpc;
11405 struct die_info *child_die;
11406 struct attribute *attr, *call_line, *call_file;
11407 const char *name;
11408 CORE_ADDR baseaddr;
11409 struct block *block;
11410 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
11411 VEC (symbolp) *template_args = NULL;
11412 struct template_symbol *templ_func = NULL;
11413
11414 if (inlined_func)
11415 {
11416 /* If we do not have call site information, we can't show the
11417 caller of this inlined function. That's too confusing, so
11418 only use the scope for local variables. */
11419 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
11420 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
11421 if (call_line == NULL || call_file == NULL)
11422 {
11423 read_lexical_block_scope (die, cu);
11424 return;
11425 }
11426 }
11427
11428 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11429
11430 name = dwarf2_name (die, cu);
11431
11432 /* Ignore functions with missing or empty names. These are actually
11433 illegal according to the DWARF standard. */
11434 if (name == NULL)
11435 {
11436 complaint (&symfile_complaints,
11437 _("missing name for subprogram DIE at %d"),
11438 die->offset.sect_off);
11439 return;
11440 }
11441
11442 /* Ignore functions with missing or invalid low and high pc attributes. */
11443 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
11444 <= PC_BOUNDS_INVALID)
11445 {
11446 attr = dwarf2_attr (die, DW_AT_external, cu);
11447 if (!attr || !DW_UNSND (attr))
11448 complaint (&symfile_complaints,
11449 _("cannot get low and high bounds "
11450 "for subprogram DIE at %d"),
11451 die->offset.sect_off);
11452 return;
11453 }
11454
11455 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11456 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
11457
11458 /* If we have any template arguments, then we must allocate a
11459 different sort of symbol. */
11460 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
11461 {
11462 if (child_die->tag == DW_TAG_template_type_param
11463 || child_die->tag == DW_TAG_template_value_param)
11464 {
11465 templ_func = allocate_template_symbol (objfile);
11466 templ_func->base.is_cplus_template_function = 1;
11467 break;
11468 }
11469 }
11470
11471 newobj = push_context (0, lowpc);
11472 newobj->name = new_symbol_full (die, read_type_die (die, cu), cu,
11473 (struct symbol *) templ_func);
11474
11475 /* If there is a location expression for DW_AT_frame_base, record
11476 it. */
11477 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
11478 if (attr)
11479 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
11480
11481 /* If there is a location for the static link, record it. */
11482 newobj->static_link = NULL;
11483 attr = dwarf2_attr (die, DW_AT_static_link, cu);
11484 if (attr)
11485 {
11486 newobj->static_link
11487 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
11488 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
11489 }
11490
11491 cu->list_in_scope = &local_symbols;
11492
11493 if (die->child != NULL)
11494 {
11495 child_die = die->child;
11496 while (child_die && child_die->tag)
11497 {
11498 if (child_die->tag == DW_TAG_template_type_param
11499 || child_die->tag == DW_TAG_template_value_param)
11500 {
11501 struct symbol *arg = new_symbol (child_die, NULL, cu);
11502
11503 if (arg != NULL)
11504 VEC_safe_push (symbolp, template_args, arg);
11505 }
11506 else
11507 process_die (child_die, cu);
11508 child_die = sibling_die (child_die);
11509 }
11510 }
11511
11512 inherit_abstract_dies (die, cu);
11513
11514 /* If we have a DW_AT_specification, we might need to import using
11515 directives from the context of the specification DIE. See the
11516 comment in determine_prefix. */
11517 if (cu->language == language_cplus
11518 && dwarf2_attr (die, DW_AT_specification, cu))
11519 {
11520 struct dwarf2_cu *spec_cu = cu;
11521 struct die_info *spec_die = die_specification (die, &spec_cu);
11522
11523 while (spec_die)
11524 {
11525 child_die = spec_die->child;
11526 while (child_die && child_die->tag)
11527 {
11528 if (child_die->tag == DW_TAG_imported_module)
11529 process_die (child_die, spec_cu);
11530 child_die = sibling_die (child_die);
11531 }
11532
11533 /* In some cases, GCC generates specification DIEs that
11534 themselves contain DW_AT_specification attributes. */
11535 spec_die = die_specification (spec_die, &spec_cu);
11536 }
11537 }
11538
11539 newobj = pop_context ();
11540 /* Make a block for the local symbols within. */
11541 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
11542 newobj->static_link, lowpc, highpc);
11543
11544 /* For C++, set the block's scope. */
11545 if ((cu->language == language_cplus
11546 || cu->language == language_fortran
11547 || cu->language == language_d
11548 || cu->language == language_rust)
11549 && cu->processing_has_namespace_info)
11550 block_set_scope (block, determine_prefix (die, cu),
11551 &objfile->objfile_obstack);
11552
11553 /* If we have address ranges, record them. */
11554 dwarf2_record_block_ranges (die, block, baseaddr, cu);
11555
11556 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
11557
11558 /* Attach template arguments to function. */
11559 if (! VEC_empty (symbolp, template_args))
11560 {
11561 gdb_assert (templ_func != NULL);
11562
11563 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
11564 templ_func->template_arguments
11565 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
11566 templ_func->n_template_arguments);
11567 memcpy (templ_func->template_arguments,
11568 VEC_address (symbolp, template_args),
11569 (templ_func->n_template_arguments * sizeof (struct symbol *)));
11570 VEC_free (symbolp, template_args);
11571 }
11572
11573 /* In C++, we can have functions nested inside functions (e.g., when
11574 a function declares a class that has methods). This means that
11575 when we finish processing a function scope, we may need to go
11576 back to building a containing block's symbol lists. */
11577 local_symbols = newobj->locals;
11578 local_using_directives = newobj->local_using_directives;
11579
11580 /* If we've finished processing a top-level function, subsequent
11581 symbols go in the file symbol list. */
11582 if (outermost_context_p ())
11583 cu->list_in_scope = &file_symbols;
11584 }
11585
11586 /* Process all the DIES contained within a lexical block scope. Start
11587 a new scope, process the dies, and then close the scope. */
11588
11589 static void
11590 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
11591 {
11592 struct objfile *objfile = cu->objfile;
11593 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11594 struct context_stack *newobj;
11595 CORE_ADDR lowpc, highpc;
11596 struct die_info *child_die;
11597 CORE_ADDR baseaddr;
11598
11599 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11600
11601 /* Ignore blocks with missing or invalid low and high pc attributes. */
11602 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
11603 as multiple lexical blocks? Handling children in a sane way would
11604 be nasty. Might be easier to properly extend generic blocks to
11605 describe ranges. */
11606 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
11607 {
11608 case PC_BOUNDS_NOT_PRESENT:
11609 /* DW_TAG_lexical_block has no attributes, process its children as if
11610 there was no wrapping by that DW_TAG_lexical_block.
11611 GCC does no longer produces such DWARF since GCC r224161. */
11612 for (child_die = die->child;
11613 child_die != NULL && child_die->tag;
11614 child_die = sibling_die (child_die))
11615 process_die (child_die, cu);
11616 return;
11617 case PC_BOUNDS_INVALID:
11618 return;
11619 }
11620 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11621 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
11622
11623 push_context (0, lowpc);
11624 if (die->child != NULL)
11625 {
11626 child_die = die->child;
11627 while (child_die && child_die->tag)
11628 {
11629 process_die (child_die, cu);
11630 child_die = sibling_die (child_die);
11631 }
11632 }
11633 inherit_abstract_dies (die, cu);
11634 newobj = pop_context ();
11635
11636 if (local_symbols != NULL || local_using_directives != NULL)
11637 {
11638 struct block *block
11639 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
11640 newobj->start_addr, highpc);
11641
11642 /* Note that recording ranges after traversing children, as we
11643 do here, means that recording a parent's ranges entails
11644 walking across all its children's ranges as they appear in
11645 the address map, which is quadratic behavior.
11646
11647 It would be nicer to record the parent's ranges before
11648 traversing its children, simply overriding whatever you find
11649 there. But since we don't even decide whether to create a
11650 block until after we've traversed its children, that's hard
11651 to do. */
11652 dwarf2_record_block_ranges (die, block, baseaddr, cu);
11653 }
11654 local_symbols = newobj->locals;
11655 local_using_directives = newobj->local_using_directives;
11656 }
11657
11658 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
11659
11660 static void
11661 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
11662 {
11663 struct objfile *objfile = cu->objfile;
11664 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11665 CORE_ADDR pc, baseaddr;
11666 struct attribute *attr;
11667 struct call_site *call_site, call_site_local;
11668 void **slot;
11669 int nparams;
11670 struct die_info *child_die;
11671
11672 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11673
11674 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
11675 if (attr == NULL)
11676 {
11677 /* This was a pre-DWARF-5 GNU extension alias
11678 for DW_AT_call_return_pc. */
11679 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11680 }
11681 if (!attr)
11682 {
11683 complaint (&symfile_complaints,
11684 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
11685 "DIE 0x%x [in module %s]"),
11686 die->offset.sect_off, objfile_name (objfile));
11687 return;
11688 }
11689 pc = attr_value_as_address (attr) + baseaddr;
11690 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
11691
11692 if (cu->call_site_htab == NULL)
11693 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
11694 NULL, &objfile->objfile_obstack,
11695 hashtab_obstack_allocate, NULL);
11696 call_site_local.pc = pc;
11697 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
11698 if (*slot != NULL)
11699 {
11700 complaint (&symfile_complaints,
11701 _("Duplicate PC %s for DW_TAG_call_site "
11702 "DIE 0x%x [in module %s]"),
11703 paddress (gdbarch, pc), die->offset.sect_off,
11704 objfile_name (objfile));
11705 return;
11706 }
11707
11708 /* Count parameters at the caller. */
11709
11710 nparams = 0;
11711 for (child_die = die->child; child_die && child_die->tag;
11712 child_die = sibling_die (child_die))
11713 {
11714 if (child_die->tag != DW_TAG_call_site_parameter
11715 && child_die->tag != DW_TAG_GNU_call_site_parameter)
11716 {
11717 complaint (&symfile_complaints,
11718 _("Tag %d is not DW_TAG_call_site_parameter in "
11719 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
11720 child_die->tag, child_die->offset.sect_off,
11721 objfile_name (objfile));
11722 continue;
11723 }
11724
11725 nparams++;
11726 }
11727
11728 call_site
11729 = ((struct call_site *)
11730 obstack_alloc (&objfile->objfile_obstack,
11731 sizeof (*call_site)
11732 + (sizeof (*call_site->parameter) * (nparams - 1))));
11733 *slot = call_site;
11734 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
11735 call_site->pc = pc;
11736
11737 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
11738 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
11739 {
11740 struct die_info *func_die;
11741
11742 /* Skip also over DW_TAG_inlined_subroutine. */
11743 for (func_die = die->parent;
11744 func_die && func_die->tag != DW_TAG_subprogram
11745 && func_die->tag != DW_TAG_subroutine_type;
11746 func_die = func_die->parent);
11747
11748 /* DW_AT_call_all_calls is a superset
11749 of DW_AT_call_all_tail_calls. */
11750 if (func_die
11751 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
11752 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
11753 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
11754 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
11755 {
11756 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
11757 not complete. But keep CALL_SITE for look ups via call_site_htab,
11758 both the initial caller containing the real return address PC and
11759 the final callee containing the current PC of a chain of tail
11760 calls do not need to have the tail call list complete. But any
11761 function candidate for a virtual tail call frame searched via
11762 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
11763 determined unambiguously. */
11764 }
11765 else
11766 {
11767 struct type *func_type = NULL;
11768
11769 if (func_die)
11770 func_type = get_die_type (func_die, cu);
11771 if (func_type != NULL)
11772 {
11773 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
11774
11775 /* Enlist this call site to the function. */
11776 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
11777 TYPE_TAIL_CALL_LIST (func_type) = call_site;
11778 }
11779 else
11780 complaint (&symfile_complaints,
11781 _("Cannot find function owning DW_TAG_call_site "
11782 "DIE 0x%x [in module %s]"),
11783 die->offset.sect_off, objfile_name (objfile));
11784 }
11785 }
11786
11787 attr = dwarf2_attr (die, DW_AT_call_target, cu);
11788 if (attr == NULL)
11789 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
11790 if (attr == NULL)
11791 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
11792 if (attr == NULL)
11793 {
11794 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
11795 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
11796 }
11797 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
11798 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
11799 /* Keep NULL DWARF_BLOCK. */;
11800 else if (attr_form_is_block (attr))
11801 {
11802 struct dwarf2_locexpr_baton *dlbaton;
11803
11804 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
11805 dlbaton->data = DW_BLOCK (attr)->data;
11806 dlbaton->size = DW_BLOCK (attr)->size;
11807 dlbaton->per_cu = cu->per_cu;
11808
11809 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
11810 }
11811 else if (attr_form_is_ref (attr))
11812 {
11813 struct dwarf2_cu *target_cu = cu;
11814 struct die_info *target_die;
11815
11816 target_die = follow_die_ref (die, attr, &target_cu);
11817 gdb_assert (target_cu->objfile == objfile);
11818 if (die_is_declaration (target_die, target_cu))
11819 {
11820 const char *target_physname;
11821
11822 /* Prefer the mangled name; otherwise compute the demangled one. */
11823 target_physname = dwarf2_string_attr (target_die,
11824 DW_AT_linkage_name,
11825 target_cu);
11826 if (target_physname == NULL)
11827 target_physname = dwarf2_string_attr (target_die,
11828 DW_AT_MIPS_linkage_name,
11829 target_cu);
11830 if (target_physname == NULL)
11831 target_physname = dwarf2_physname (NULL, target_die, target_cu);
11832 if (target_physname == NULL)
11833 complaint (&symfile_complaints,
11834 _("DW_AT_call_target target DIE has invalid "
11835 "physname, for referencing DIE 0x%x [in module %s]"),
11836 die->offset.sect_off, objfile_name (objfile));
11837 else
11838 SET_FIELD_PHYSNAME (call_site->target, target_physname);
11839 }
11840 else
11841 {
11842 CORE_ADDR lowpc;
11843
11844 /* DW_AT_entry_pc should be preferred. */
11845 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
11846 <= PC_BOUNDS_INVALID)
11847 complaint (&symfile_complaints,
11848 _("DW_AT_call_target target DIE has invalid "
11849 "low pc, for referencing DIE 0x%x [in module %s]"),
11850 die->offset.sect_off, objfile_name (objfile));
11851 else
11852 {
11853 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11854 SET_FIELD_PHYSADDR (call_site->target, lowpc);
11855 }
11856 }
11857 }
11858 else
11859 complaint (&symfile_complaints,
11860 _("DW_TAG_call_site DW_AT_call_target is neither "
11861 "block nor reference, for DIE 0x%x [in module %s]"),
11862 die->offset.sect_off, objfile_name (objfile));
11863
11864 call_site->per_cu = cu->per_cu;
11865
11866 for (child_die = die->child;
11867 child_die && child_die->tag;
11868 child_die = sibling_die (child_die))
11869 {
11870 struct call_site_parameter *parameter;
11871 struct attribute *loc, *origin;
11872
11873 if (child_die->tag != DW_TAG_call_site_parameter
11874 && child_die->tag != DW_TAG_GNU_call_site_parameter)
11875 {
11876 /* Already printed the complaint above. */
11877 continue;
11878 }
11879
11880 gdb_assert (call_site->parameter_count < nparams);
11881 parameter = &call_site->parameter[call_site->parameter_count];
11882
11883 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
11884 specifies DW_TAG_formal_parameter. Value of the data assumed for the
11885 register is contained in DW_AT_call_value. */
11886
11887 loc = dwarf2_attr (child_die, DW_AT_location, cu);
11888 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
11889 if (origin == NULL)
11890 {
11891 /* This was a pre-DWARF-5 GNU extension alias
11892 for DW_AT_call_parameter. */
11893 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
11894 }
11895 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
11896 {
11897 sect_offset offset;
11898
11899 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
11900 offset = dwarf2_get_ref_die_offset (origin);
11901 if (!offset_in_cu_p (&cu->header, offset))
11902 {
11903 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
11904 binding can be done only inside one CU. Such referenced DIE
11905 therefore cannot be even moved to DW_TAG_partial_unit. */
11906 complaint (&symfile_complaints,
11907 _("DW_AT_call_parameter offset is not in CU for "
11908 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
11909 child_die->offset.sect_off, objfile_name (objfile));
11910 continue;
11911 }
11912 parameter->u.param_offset.cu_off = (offset.sect_off
11913 - cu->header.offset.sect_off);
11914 }
11915 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
11916 {
11917 complaint (&symfile_complaints,
11918 _("No DW_FORM_block* DW_AT_location for "
11919 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
11920 child_die->offset.sect_off, objfile_name (objfile));
11921 continue;
11922 }
11923 else
11924 {
11925 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
11926 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
11927 if (parameter->u.dwarf_reg != -1)
11928 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
11929 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
11930 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
11931 &parameter->u.fb_offset))
11932 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
11933 else
11934 {
11935 complaint (&symfile_complaints,
11936 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
11937 "for DW_FORM_block* DW_AT_location is supported for "
11938 "DW_TAG_call_site child DIE 0x%x "
11939 "[in module %s]"),
11940 child_die->offset.sect_off, objfile_name (objfile));
11941 continue;
11942 }
11943 }
11944
11945 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
11946 if (attr == NULL)
11947 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
11948 if (!attr_form_is_block (attr))
11949 {
11950 complaint (&symfile_complaints,
11951 _("No DW_FORM_block* DW_AT_call_value for "
11952 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
11953 child_die->offset.sect_off, objfile_name (objfile));
11954 continue;
11955 }
11956 parameter->value = DW_BLOCK (attr)->data;
11957 parameter->value_size = DW_BLOCK (attr)->size;
11958
11959 /* Parameters are not pre-cleared by memset above. */
11960 parameter->data_value = NULL;
11961 parameter->data_value_size = 0;
11962 call_site->parameter_count++;
11963
11964 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
11965 if (attr == NULL)
11966 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
11967 if (attr)
11968 {
11969 if (!attr_form_is_block (attr))
11970 complaint (&symfile_complaints,
11971 _("No DW_FORM_block* DW_AT_call_data_value for "
11972 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
11973 child_die->offset.sect_off, objfile_name (objfile));
11974 else
11975 {
11976 parameter->data_value = DW_BLOCK (attr)->data;
11977 parameter->data_value_size = DW_BLOCK (attr)->size;
11978 }
11979 }
11980 }
11981 }
11982
11983 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
11984 reading .debug_rnglists.
11985 Callback's type should be:
11986 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
11987 Return true if the attributes are present and valid, otherwise,
11988 return false. */
11989
11990 template <typename Callback>
11991 static bool
11992 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
11993 Callback &&callback)
11994 {
11995 struct objfile *objfile = cu->objfile;
11996 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11997 struct comp_unit_head *cu_header = &cu->header;
11998 bfd *obfd = objfile->obfd;
11999 unsigned int addr_size = cu_header->addr_size;
12000 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12001 /* Base address selection entry. */
12002 CORE_ADDR base;
12003 int found_base;
12004 unsigned int dummy;
12005 const gdb_byte *buffer;
12006 CORE_ADDR low = 0;
12007 CORE_ADDR high = 0;
12008 CORE_ADDR baseaddr;
12009 bool overflow = false;
12010
12011 found_base = cu->base_known;
12012 base = cu->base_address;
12013
12014 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
12015 if (offset >= dwarf2_per_objfile->rnglists.size)
12016 {
12017 complaint (&symfile_complaints,
12018 _("Offset %d out of bounds for DW_AT_ranges attribute"),
12019 offset);
12020 return false;
12021 }
12022 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
12023
12024 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12025
12026 while (1)
12027 {
12028 CORE_ADDR range_beginning, range_end;
12029 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
12030 + dwarf2_per_objfile->rnglists.size);
12031 unsigned int bytes_read;
12032
12033 if (buffer == buf_end)
12034 {
12035 overflow = true;
12036 break;
12037 }
12038 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
12039 switch (rlet)
12040 {
12041 case DW_RLE_end_of_list:
12042 break;
12043 case DW_RLE_base_address:
12044 if (buffer + cu->header.addr_size > buf_end)
12045 {
12046 overflow = true;
12047 break;
12048 }
12049 base = read_address (obfd, buffer, cu, &bytes_read);
12050 found_base = 1;
12051 buffer += bytes_read;
12052 break;
12053 case DW_RLE_start_length:
12054 if (buffer + cu->header.addr_size > buf_end)
12055 {
12056 overflow = true;
12057 break;
12058 }
12059 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
12060 buffer += bytes_read;
12061 range_end = (range_beginning
12062 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
12063 buffer += bytes_read;
12064 if (buffer > buf_end)
12065 {
12066 overflow = true;
12067 break;
12068 }
12069 break;
12070 case DW_RLE_offset_pair:
12071 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12072 buffer += bytes_read;
12073 if (buffer > buf_end)
12074 {
12075 overflow = true;
12076 break;
12077 }
12078 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12079 buffer += bytes_read;
12080 if (buffer > buf_end)
12081 {
12082 overflow = true;
12083 break;
12084 }
12085 break;
12086 case DW_RLE_start_end:
12087 if (buffer + 2 * cu->header.addr_size > buf_end)
12088 {
12089 overflow = true;
12090 break;
12091 }
12092 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
12093 buffer += bytes_read;
12094 range_end = read_address (obfd, buffer, cu, &bytes_read);
12095 buffer += bytes_read;
12096 break;
12097 default:
12098 complaint (&symfile_complaints,
12099 _("Invalid .debug_rnglists data (no base address)"));
12100 return false;
12101 }
12102 if (rlet == DW_RLE_end_of_list || overflow)
12103 break;
12104 if (rlet == DW_RLE_base_address)
12105 continue;
12106
12107 if (!found_base)
12108 {
12109 /* We have no valid base address for the ranges
12110 data. */
12111 complaint (&symfile_complaints,
12112 _("Invalid .debug_rnglists data (no base address)"));
12113 return false;
12114 }
12115
12116 if (range_beginning > range_end)
12117 {
12118 /* Inverted range entries are invalid. */
12119 complaint (&symfile_complaints,
12120 _("Invalid .debug_rnglists data (inverted range)"));
12121 return false;
12122 }
12123
12124 /* Empty range entries have no effect. */
12125 if (range_beginning == range_end)
12126 continue;
12127
12128 range_beginning += base;
12129 range_end += base;
12130
12131 /* A not-uncommon case of bad debug info.
12132 Don't pollute the addrmap with bad data. */
12133 if (range_beginning + baseaddr == 0
12134 && !dwarf2_per_objfile->has_section_at_zero)
12135 {
12136 complaint (&symfile_complaints,
12137 _(".debug_rnglists entry has start address of zero"
12138 " [in module %s]"), objfile_name (objfile));
12139 continue;
12140 }
12141
12142 callback (range_beginning, range_end);
12143 }
12144
12145 if (overflow)
12146 {
12147 complaint (&symfile_complaints,
12148 _("Offset %d is not terminated "
12149 "for DW_AT_ranges attribute"),
12150 offset);
12151 return false;
12152 }
12153
12154 return true;
12155 }
12156
12157 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
12158 Callback's type should be:
12159 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
12160 Return 1 if the attributes are present and valid, otherwise, return 0. */
12161
12162 template <typename Callback>
12163 static int
12164 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
12165 Callback &&callback)
12166 {
12167 struct objfile *objfile = cu->objfile;
12168 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12169 struct comp_unit_head *cu_header = &cu->header;
12170 bfd *obfd = objfile->obfd;
12171 unsigned int addr_size = cu_header->addr_size;
12172 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12173 /* Base address selection entry. */
12174 CORE_ADDR base;
12175 int found_base;
12176 unsigned int dummy;
12177 const gdb_byte *buffer;
12178 CORE_ADDR baseaddr;
12179
12180 if (cu_header->version >= 5)
12181 return dwarf2_rnglists_process (offset, cu, callback);
12182
12183 found_base = cu->base_known;
12184 base = cu->base_address;
12185
12186 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
12187 if (offset >= dwarf2_per_objfile->ranges.size)
12188 {
12189 complaint (&symfile_complaints,
12190 _("Offset %d out of bounds for DW_AT_ranges attribute"),
12191 offset);
12192 return 0;
12193 }
12194 buffer = dwarf2_per_objfile->ranges.buffer + offset;
12195
12196 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12197
12198 while (1)
12199 {
12200 CORE_ADDR range_beginning, range_end;
12201
12202 range_beginning = read_address (obfd, buffer, cu, &dummy);
12203 buffer += addr_size;
12204 range_end = read_address (obfd, buffer, cu, &dummy);
12205 buffer += addr_size;
12206 offset += 2 * addr_size;
12207
12208 /* An end of list marker is a pair of zero addresses. */
12209 if (range_beginning == 0 && range_end == 0)
12210 /* Found the end of list entry. */
12211 break;
12212
12213 /* Each base address selection entry is a pair of 2 values.
12214 The first is the largest possible address, the second is
12215 the base address. Check for a base address here. */
12216 if ((range_beginning & mask) == mask)
12217 {
12218 /* If we found the largest possible address, then we already
12219 have the base address in range_end. */
12220 base = range_end;
12221 found_base = 1;
12222 continue;
12223 }
12224
12225 if (!found_base)
12226 {
12227 /* We have no valid base address for the ranges
12228 data. */
12229 complaint (&symfile_complaints,
12230 _("Invalid .debug_ranges data (no base address)"));
12231 return 0;
12232 }
12233
12234 if (range_beginning > range_end)
12235 {
12236 /* Inverted range entries are invalid. */
12237 complaint (&symfile_complaints,
12238 _("Invalid .debug_ranges data (inverted range)"));
12239 return 0;
12240 }
12241
12242 /* Empty range entries have no effect. */
12243 if (range_beginning == range_end)
12244 continue;
12245
12246 range_beginning += base;
12247 range_end += base;
12248
12249 /* A not-uncommon case of bad debug info.
12250 Don't pollute the addrmap with bad data. */
12251 if (range_beginning + baseaddr == 0
12252 && !dwarf2_per_objfile->has_section_at_zero)
12253 {
12254 complaint (&symfile_complaints,
12255 _(".debug_ranges entry has start address of zero"
12256 " [in module %s]"), objfile_name (objfile));
12257 continue;
12258 }
12259
12260 callback (range_beginning, range_end);
12261 }
12262
12263 return 1;
12264 }
12265
12266 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
12267 Return 1 if the attributes are present and valid, otherwise, return 0.
12268 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
12269
12270 static int
12271 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
12272 CORE_ADDR *high_return, struct dwarf2_cu *cu,
12273 struct partial_symtab *ranges_pst)
12274 {
12275 struct objfile *objfile = cu->objfile;
12276 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12277 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
12278 SECT_OFF_TEXT (objfile));
12279 int low_set = 0;
12280 CORE_ADDR low = 0;
12281 CORE_ADDR high = 0;
12282 int retval;
12283
12284 retval = dwarf2_ranges_process (offset, cu,
12285 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
12286 {
12287 if (ranges_pst != NULL)
12288 {
12289 CORE_ADDR lowpc;
12290 CORE_ADDR highpc;
12291
12292 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
12293 range_beginning + baseaddr);
12294 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
12295 range_end + baseaddr);
12296 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
12297 ranges_pst);
12298 }
12299
12300 /* FIXME: This is recording everything as a low-high
12301 segment of consecutive addresses. We should have a
12302 data structure for discontiguous block ranges
12303 instead. */
12304 if (! low_set)
12305 {
12306 low = range_beginning;
12307 high = range_end;
12308 low_set = 1;
12309 }
12310 else
12311 {
12312 if (range_beginning < low)
12313 low = range_beginning;
12314 if (range_end > high)
12315 high = range_end;
12316 }
12317 });
12318 if (!retval)
12319 return 0;
12320
12321 if (! low_set)
12322 /* If the first entry is an end-of-list marker, the range
12323 describes an empty scope, i.e. no instructions. */
12324 return 0;
12325
12326 if (low_return)
12327 *low_return = low;
12328 if (high_return)
12329 *high_return = high;
12330 return 1;
12331 }
12332
12333 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
12334 definition for the return value. *LOWPC and *HIGHPC are set iff
12335 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
12336
12337 static enum pc_bounds_kind
12338 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
12339 CORE_ADDR *highpc, struct dwarf2_cu *cu,
12340 struct partial_symtab *pst)
12341 {
12342 struct attribute *attr;
12343 struct attribute *attr_high;
12344 CORE_ADDR low = 0;
12345 CORE_ADDR high = 0;
12346 enum pc_bounds_kind ret;
12347
12348 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
12349 if (attr_high)
12350 {
12351 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12352 if (attr)
12353 {
12354 low = attr_value_as_address (attr);
12355 high = attr_value_as_address (attr_high);
12356 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
12357 high += low;
12358 }
12359 else
12360 /* Found high w/o low attribute. */
12361 return PC_BOUNDS_INVALID;
12362
12363 /* Found consecutive range of addresses. */
12364 ret = PC_BOUNDS_HIGH_LOW;
12365 }
12366 else
12367 {
12368 attr = dwarf2_attr (die, DW_AT_ranges, cu);
12369 if (attr != NULL)
12370 {
12371 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
12372 We take advantage of the fact that DW_AT_ranges does not appear
12373 in DW_TAG_compile_unit of DWO files. */
12374 int need_ranges_base = die->tag != DW_TAG_compile_unit;
12375 unsigned int ranges_offset = (DW_UNSND (attr)
12376 + (need_ranges_base
12377 ? cu->ranges_base
12378 : 0));
12379
12380 /* Value of the DW_AT_ranges attribute is the offset in the
12381 .debug_ranges section. */
12382 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
12383 return PC_BOUNDS_INVALID;
12384 /* Found discontinuous range of addresses. */
12385 ret = PC_BOUNDS_RANGES;
12386 }
12387 else
12388 return PC_BOUNDS_NOT_PRESENT;
12389 }
12390
12391 /* read_partial_die has also the strict LOW < HIGH requirement. */
12392 if (high <= low)
12393 return PC_BOUNDS_INVALID;
12394
12395 /* When using the GNU linker, .gnu.linkonce. sections are used to
12396 eliminate duplicate copies of functions and vtables and such.
12397 The linker will arbitrarily choose one and discard the others.
12398 The AT_*_pc values for such functions refer to local labels in
12399 these sections. If the section from that file was discarded, the
12400 labels are not in the output, so the relocs get a value of 0.
12401 If this is a discarded function, mark the pc bounds as invalid,
12402 so that GDB will ignore it. */
12403 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
12404 return PC_BOUNDS_INVALID;
12405
12406 *lowpc = low;
12407 if (highpc)
12408 *highpc = high;
12409 return ret;
12410 }
12411
12412 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
12413 its low and high PC addresses. Do nothing if these addresses could not
12414 be determined. Otherwise, set LOWPC to the low address if it is smaller,
12415 and HIGHPC to the high address if greater than HIGHPC. */
12416
12417 static void
12418 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
12419 CORE_ADDR *lowpc, CORE_ADDR *highpc,
12420 struct dwarf2_cu *cu)
12421 {
12422 CORE_ADDR low, high;
12423 struct die_info *child = die->child;
12424
12425 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
12426 {
12427 *lowpc = std::min (*lowpc, low);
12428 *highpc = std::max (*highpc, high);
12429 }
12430
12431 /* If the language does not allow nested subprograms (either inside
12432 subprograms or lexical blocks), we're done. */
12433 if (cu->language != language_ada)
12434 return;
12435
12436 /* Check all the children of the given DIE. If it contains nested
12437 subprograms, then check their pc bounds. Likewise, we need to
12438 check lexical blocks as well, as they may also contain subprogram
12439 definitions. */
12440 while (child && child->tag)
12441 {
12442 if (child->tag == DW_TAG_subprogram
12443 || child->tag == DW_TAG_lexical_block)
12444 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
12445 child = sibling_die (child);
12446 }
12447 }
12448
12449 /* Get the low and high pc's represented by the scope DIE, and store
12450 them in *LOWPC and *HIGHPC. If the correct values can't be
12451 determined, set *LOWPC to -1 and *HIGHPC to 0. */
12452
12453 static void
12454 get_scope_pc_bounds (struct die_info *die,
12455 CORE_ADDR *lowpc, CORE_ADDR *highpc,
12456 struct dwarf2_cu *cu)
12457 {
12458 CORE_ADDR best_low = (CORE_ADDR) -1;
12459 CORE_ADDR best_high = (CORE_ADDR) 0;
12460 CORE_ADDR current_low, current_high;
12461
12462 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
12463 >= PC_BOUNDS_RANGES)
12464 {
12465 best_low = current_low;
12466 best_high = current_high;
12467 }
12468 else
12469 {
12470 struct die_info *child = die->child;
12471
12472 while (child && child->tag)
12473 {
12474 switch (child->tag) {
12475 case DW_TAG_subprogram:
12476 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
12477 break;
12478 case DW_TAG_namespace:
12479 case DW_TAG_module:
12480 /* FIXME: carlton/2004-01-16: Should we do this for
12481 DW_TAG_class_type/DW_TAG_structure_type, too? I think
12482 that current GCC's always emit the DIEs corresponding
12483 to definitions of methods of classes as children of a
12484 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
12485 the DIEs giving the declarations, which could be
12486 anywhere). But I don't see any reason why the
12487 standards says that they have to be there. */
12488 get_scope_pc_bounds (child, &current_low, &current_high, cu);
12489
12490 if (current_low != ((CORE_ADDR) -1))
12491 {
12492 best_low = std::min (best_low, current_low);
12493 best_high = std::max (best_high, current_high);
12494 }
12495 break;
12496 default:
12497 /* Ignore. */
12498 break;
12499 }
12500
12501 child = sibling_die (child);
12502 }
12503 }
12504
12505 *lowpc = best_low;
12506 *highpc = best_high;
12507 }
12508
12509 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
12510 in DIE. */
12511
12512 static void
12513 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
12514 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
12515 {
12516 struct objfile *objfile = cu->objfile;
12517 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12518 struct attribute *attr;
12519 struct attribute *attr_high;
12520
12521 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
12522 if (attr_high)
12523 {
12524 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12525 if (attr)
12526 {
12527 CORE_ADDR low = attr_value_as_address (attr);
12528 CORE_ADDR high = attr_value_as_address (attr_high);
12529
12530 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
12531 high += low;
12532
12533 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
12534 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
12535 record_block_range (block, low, high - 1);
12536 }
12537 }
12538
12539 attr = dwarf2_attr (die, DW_AT_ranges, cu);
12540 if (attr)
12541 {
12542 bfd *obfd = objfile->obfd;
12543 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
12544 We take advantage of the fact that DW_AT_ranges does not appear
12545 in DW_TAG_compile_unit of DWO files. */
12546 int need_ranges_base = die->tag != DW_TAG_compile_unit;
12547
12548 /* The value of the DW_AT_ranges attribute is the offset of the
12549 address range list in the .debug_ranges section. */
12550 unsigned long offset = (DW_UNSND (attr)
12551 + (need_ranges_base ? cu->ranges_base : 0));
12552 const gdb_byte *buffer;
12553
12554 /* For some target architectures, but not others, the
12555 read_address function sign-extends the addresses it returns.
12556 To recognize base address selection entries, we need a
12557 mask. */
12558 unsigned int addr_size = cu->header.addr_size;
12559 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12560
12561 /* The base address, to which the next pair is relative. Note
12562 that this 'base' is a DWARF concept: most entries in a range
12563 list are relative, to reduce the number of relocs against the
12564 debugging information. This is separate from this function's
12565 'baseaddr' argument, which GDB uses to relocate debugging
12566 information from a shared library based on the address at
12567 which the library was loaded. */
12568 CORE_ADDR base = cu->base_address;
12569 int base_known = cu->base_known;
12570
12571 dwarf2_ranges_process (offset, cu,
12572 [&] (CORE_ADDR start, CORE_ADDR end)
12573 {
12574 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
12575 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
12576 record_block_range (block, start, end - 1);
12577 });
12578 }
12579 }
12580
12581 /* Check whether the producer field indicates either of GCC < 4.6, or the
12582 Intel C/C++ compiler, and cache the result in CU. */
12583
12584 static void
12585 check_producer (struct dwarf2_cu *cu)
12586 {
12587 int major, minor;
12588
12589 if (cu->producer == NULL)
12590 {
12591 /* For unknown compilers expect their behavior is DWARF version
12592 compliant.
12593
12594 GCC started to support .debug_types sections by -gdwarf-4 since
12595 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
12596 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
12597 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
12598 interpreted incorrectly by GDB now - GCC PR debug/48229. */
12599 }
12600 else if (producer_is_gcc (cu->producer, &major, &minor))
12601 {
12602 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
12603 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
12604 }
12605 else if (startswith (cu->producer, "Intel(R) C"))
12606 cu->producer_is_icc = 1;
12607 else
12608 {
12609 /* For other non-GCC compilers, expect their behavior is DWARF version
12610 compliant. */
12611 }
12612
12613 cu->checked_producer = 1;
12614 }
12615
12616 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
12617 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
12618 during 4.6.0 experimental. */
12619
12620 static int
12621 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
12622 {
12623 if (!cu->checked_producer)
12624 check_producer (cu);
12625
12626 return cu->producer_is_gxx_lt_4_6;
12627 }
12628
12629 /* Return the default accessibility type if it is not overriden by
12630 DW_AT_accessibility. */
12631
12632 static enum dwarf_access_attribute
12633 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
12634 {
12635 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
12636 {
12637 /* The default DWARF 2 accessibility for members is public, the default
12638 accessibility for inheritance is private. */
12639
12640 if (die->tag != DW_TAG_inheritance)
12641 return DW_ACCESS_public;
12642 else
12643 return DW_ACCESS_private;
12644 }
12645 else
12646 {
12647 /* DWARF 3+ defines the default accessibility a different way. The same
12648 rules apply now for DW_TAG_inheritance as for the members and it only
12649 depends on the container kind. */
12650
12651 if (die->parent->tag == DW_TAG_class_type)
12652 return DW_ACCESS_private;
12653 else
12654 return DW_ACCESS_public;
12655 }
12656 }
12657
12658 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
12659 offset. If the attribute was not found return 0, otherwise return
12660 1. If it was found but could not properly be handled, set *OFFSET
12661 to 0. */
12662
12663 static int
12664 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
12665 LONGEST *offset)
12666 {
12667 struct attribute *attr;
12668
12669 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
12670 if (attr != NULL)
12671 {
12672 *offset = 0;
12673
12674 /* Note that we do not check for a section offset first here.
12675 This is because DW_AT_data_member_location is new in DWARF 4,
12676 so if we see it, we can assume that a constant form is really
12677 a constant and not a section offset. */
12678 if (attr_form_is_constant (attr))
12679 *offset = dwarf2_get_attr_constant_value (attr, 0);
12680 else if (attr_form_is_section_offset (attr))
12681 dwarf2_complex_location_expr_complaint ();
12682 else if (attr_form_is_block (attr))
12683 *offset = decode_locdesc (DW_BLOCK (attr), cu);
12684 else
12685 dwarf2_complex_location_expr_complaint ();
12686
12687 return 1;
12688 }
12689
12690 return 0;
12691 }
12692
12693 /* Add an aggregate field to the field list. */
12694
12695 static void
12696 dwarf2_add_field (struct field_info *fip, struct die_info *die,
12697 struct dwarf2_cu *cu)
12698 {
12699 struct objfile *objfile = cu->objfile;
12700 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12701 struct nextfield *new_field;
12702 struct attribute *attr;
12703 struct field *fp;
12704 const char *fieldname = "";
12705
12706 /* Allocate a new field list entry and link it in. */
12707 new_field = XNEW (struct nextfield);
12708 make_cleanup (xfree, new_field);
12709 memset (new_field, 0, sizeof (struct nextfield));
12710
12711 if (die->tag == DW_TAG_inheritance)
12712 {
12713 new_field->next = fip->baseclasses;
12714 fip->baseclasses = new_field;
12715 }
12716 else
12717 {
12718 new_field->next = fip->fields;
12719 fip->fields = new_field;
12720 }
12721 fip->nfields++;
12722
12723 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
12724 if (attr)
12725 new_field->accessibility = DW_UNSND (attr);
12726 else
12727 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
12728 if (new_field->accessibility != DW_ACCESS_public)
12729 fip->non_public_fields = 1;
12730
12731 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
12732 if (attr)
12733 new_field->virtuality = DW_UNSND (attr);
12734 else
12735 new_field->virtuality = DW_VIRTUALITY_none;
12736
12737 fp = &new_field->field;
12738
12739 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
12740 {
12741 LONGEST offset;
12742
12743 /* Data member other than a C++ static data member. */
12744
12745 /* Get type of field. */
12746 fp->type = die_type (die, cu);
12747
12748 SET_FIELD_BITPOS (*fp, 0);
12749
12750 /* Get bit size of field (zero if none). */
12751 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
12752 if (attr)
12753 {
12754 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
12755 }
12756 else
12757 {
12758 FIELD_BITSIZE (*fp) = 0;
12759 }
12760
12761 /* Get bit offset of field. */
12762 if (handle_data_member_location (die, cu, &offset))
12763 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
12764 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
12765 if (attr)
12766 {
12767 if (gdbarch_bits_big_endian (gdbarch))
12768 {
12769 /* For big endian bits, the DW_AT_bit_offset gives the
12770 additional bit offset from the MSB of the containing
12771 anonymous object to the MSB of the field. We don't
12772 have to do anything special since we don't need to
12773 know the size of the anonymous object. */
12774 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
12775 }
12776 else
12777 {
12778 /* For little endian bits, compute the bit offset to the
12779 MSB of the anonymous object, subtract off the number of
12780 bits from the MSB of the field to the MSB of the
12781 object, and then subtract off the number of bits of
12782 the field itself. The result is the bit offset of
12783 the LSB of the field. */
12784 int anonymous_size;
12785 int bit_offset = DW_UNSND (attr);
12786
12787 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12788 if (attr)
12789 {
12790 /* The size of the anonymous object containing
12791 the bit field is explicit, so use the
12792 indicated size (in bytes). */
12793 anonymous_size = DW_UNSND (attr);
12794 }
12795 else
12796 {
12797 /* The size of the anonymous object containing
12798 the bit field must be inferred from the type
12799 attribute of the data member containing the
12800 bit field. */
12801 anonymous_size = TYPE_LENGTH (fp->type);
12802 }
12803 SET_FIELD_BITPOS (*fp,
12804 (FIELD_BITPOS (*fp)
12805 + anonymous_size * bits_per_byte
12806 - bit_offset - FIELD_BITSIZE (*fp)));
12807 }
12808 }
12809 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
12810 if (attr != NULL)
12811 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
12812 + dwarf2_get_attr_constant_value (attr, 0)));
12813
12814 /* Get name of field. */
12815 fieldname = dwarf2_name (die, cu);
12816 if (fieldname == NULL)
12817 fieldname = "";
12818
12819 /* The name is already allocated along with this objfile, so we don't
12820 need to duplicate it for the type. */
12821 fp->name = fieldname;
12822
12823 /* Change accessibility for artificial fields (e.g. virtual table
12824 pointer or virtual base class pointer) to private. */
12825 if (dwarf2_attr (die, DW_AT_artificial, cu))
12826 {
12827 FIELD_ARTIFICIAL (*fp) = 1;
12828 new_field->accessibility = DW_ACCESS_private;
12829 fip->non_public_fields = 1;
12830 }
12831 }
12832 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
12833 {
12834 /* C++ static member. */
12835
12836 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
12837 is a declaration, but all versions of G++ as of this writing
12838 (so through at least 3.2.1) incorrectly generate
12839 DW_TAG_variable tags. */
12840
12841 const char *physname;
12842
12843 /* Get name of field. */
12844 fieldname = dwarf2_name (die, cu);
12845 if (fieldname == NULL)
12846 return;
12847
12848 attr = dwarf2_attr (die, DW_AT_const_value, cu);
12849 if (attr
12850 /* Only create a symbol if this is an external value.
12851 new_symbol checks this and puts the value in the global symbol
12852 table, which we want. If it is not external, new_symbol
12853 will try to put the value in cu->list_in_scope which is wrong. */
12854 && dwarf2_flag_true_p (die, DW_AT_external, cu))
12855 {
12856 /* A static const member, not much different than an enum as far as
12857 we're concerned, except that we can support more types. */
12858 new_symbol (die, NULL, cu);
12859 }
12860
12861 /* Get physical name. */
12862 physname = dwarf2_physname (fieldname, die, cu);
12863
12864 /* The name is already allocated along with this objfile, so we don't
12865 need to duplicate it for the type. */
12866 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
12867 FIELD_TYPE (*fp) = die_type (die, cu);
12868 FIELD_NAME (*fp) = fieldname;
12869 }
12870 else if (die->tag == DW_TAG_inheritance)
12871 {
12872 LONGEST offset;
12873
12874 /* C++ base class field. */
12875 if (handle_data_member_location (die, cu, &offset))
12876 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
12877 FIELD_BITSIZE (*fp) = 0;
12878 FIELD_TYPE (*fp) = die_type (die, cu);
12879 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
12880 fip->nbaseclasses++;
12881 }
12882 }
12883
12884 /* Add a typedef defined in the scope of the FIP's class. */
12885
12886 static void
12887 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
12888 struct dwarf2_cu *cu)
12889 {
12890 struct typedef_field_list *new_field;
12891 struct typedef_field *fp;
12892
12893 /* Allocate a new field list entry and link it in. */
12894 new_field = XCNEW (struct typedef_field_list);
12895 make_cleanup (xfree, new_field);
12896
12897 gdb_assert (die->tag == DW_TAG_typedef);
12898
12899 fp = &new_field->field;
12900
12901 /* Get name of field. */
12902 fp->name = dwarf2_name (die, cu);
12903 if (fp->name == NULL)
12904 return;
12905
12906 fp->type = read_type_die (die, cu);
12907
12908 new_field->next = fip->typedef_field_list;
12909 fip->typedef_field_list = new_field;
12910 fip->typedef_field_list_count++;
12911 }
12912
12913 /* Create the vector of fields, and attach it to the type. */
12914
12915 static void
12916 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
12917 struct dwarf2_cu *cu)
12918 {
12919 int nfields = fip->nfields;
12920
12921 /* Record the field count, allocate space for the array of fields,
12922 and create blank accessibility bitfields if necessary. */
12923 TYPE_NFIELDS (type) = nfields;
12924 TYPE_FIELDS (type) = (struct field *)
12925 TYPE_ALLOC (type, sizeof (struct field) * nfields);
12926 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
12927
12928 if (fip->non_public_fields && cu->language != language_ada)
12929 {
12930 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12931
12932 TYPE_FIELD_PRIVATE_BITS (type) =
12933 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
12934 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
12935
12936 TYPE_FIELD_PROTECTED_BITS (type) =
12937 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
12938 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
12939
12940 TYPE_FIELD_IGNORE_BITS (type) =
12941 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
12942 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
12943 }
12944
12945 /* If the type has baseclasses, allocate and clear a bit vector for
12946 TYPE_FIELD_VIRTUAL_BITS. */
12947 if (fip->nbaseclasses && cu->language != language_ada)
12948 {
12949 int num_bytes = B_BYTES (fip->nbaseclasses);
12950 unsigned char *pointer;
12951
12952 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12953 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
12954 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
12955 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
12956 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
12957 }
12958
12959 /* Copy the saved-up fields into the field vector. Start from the head of
12960 the list, adding to the tail of the field array, so that they end up in
12961 the same order in the array in which they were added to the list. */
12962 while (nfields-- > 0)
12963 {
12964 struct nextfield *fieldp;
12965
12966 if (fip->fields)
12967 {
12968 fieldp = fip->fields;
12969 fip->fields = fieldp->next;
12970 }
12971 else
12972 {
12973 fieldp = fip->baseclasses;
12974 fip->baseclasses = fieldp->next;
12975 }
12976
12977 TYPE_FIELD (type, nfields) = fieldp->field;
12978 switch (fieldp->accessibility)
12979 {
12980 case DW_ACCESS_private:
12981 if (cu->language != language_ada)
12982 SET_TYPE_FIELD_PRIVATE (type, nfields);
12983 break;
12984
12985 case DW_ACCESS_protected:
12986 if (cu->language != language_ada)
12987 SET_TYPE_FIELD_PROTECTED (type, nfields);
12988 break;
12989
12990 case DW_ACCESS_public:
12991 break;
12992
12993 default:
12994 /* Unknown accessibility. Complain and treat it as public. */
12995 {
12996 complaint (&symfile_complaints, _("unsupported accessibility %d"),
12997 fieldp->accessibility);
12998 }
12999 break;
13000 }
13001 if (nfields < fip->nbaseclasses)
13002 {
13003 switch (fieldp->virtuality)
13004 {
13005 case DW_VIRTUALITY_virtual:
13006 case DW_VIRTUALITY_pure_virtual:
13007 if (cu->language == language_ada)
13008 error (_("unexpected virtuality in component of Ada type"));
13009 SET_TYPE_FIELD_VIRTUAL (type, nfields);
13010 break;
13011 }
13012 }
13013 }
13014 }
13015
13016 /* Return true if this member function is a constructor, false
13017 otherwise. */
13018
13019 static int
13020 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
13021 {
13022 const char *fieldname;
13023 const char *type_name;
13024 int len;
13025
13026 if (die->parent == NULL)
13027 return 0;
13028
13029 if (die->parent->tag != DW_TAG_structure_type
13030 && die->parent->tag != DW_TAG_union_type
13031 && die->parent->tag != DW_TAG_class_type)
13032 return 0;
13033
13034 fieldname = dwarf2_name (die, cu);
13035 type_name = dwarf2_name (die->parent, cu);
13036 if (fieldname == NULL || type_name == NULL)
13037 return 0;
13038
13039 len = strlen (fieldname);
13040 return (strncmp (fieldname, type_name, len) == 0
13041 && (type_name[len] == '\0' || type_name[len] == '<'));
13042 }
13043
13044 /* Add a member function to the proper fieldlist. */
13045
13046 static void
13047 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
13048 struct type *type, struct dwarf2_cu *cu)
13049 {
13050 struct objfile *objfile = cu->objfile;
13051 struct attribute *attr;
13052 struct fnfieldlist *flp;
13053 int i;
13054 struct fn_field *fnp;
13055 const char *fieldname;
13056 struct nextfnfield *new_fnfield;
13057 struct type *this_type;
13058 enum dwarf_access_attribute accessibility;
13059
13060 if (cu->language == language_ada)
13061 error (_("unexpected member function in Ada type"));
13062
13063 /* Get name of member function. */
13064 fieldname = dwarf2_name (die, cu);
13065 if (fieldname == NULL)
13066 return;
13067
13068 /* Look up member function name in fieldlist. */
13069 for (i = 0; i < fip->nfnfields; i++)
13070 {
13071 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
13072 break;
13073 }
13074
13075 /* Create new list element if necessary. */
13076 if (i < fip->nfnfields)
13077 flp = &fip->fnfieldlists[i];
13078 else
13079 {
13080 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
13081 {
13082 fip->fnfieldlists = (struct fnfieldlist *)
13083 xrealloc (fip->fnfieldlists,
13084 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
13085 * sizeof (struct fnfieldlist));
13086 if (fip->nfnfields == 0)
13087 make_cleanup (free_current_contents, &fip->fnfieldlists);
13088 }
13089 flp = &fip->fnfieldlists[fip->nfnfields];
13090 flp->name = fieldname;
13091 flp->length = 0;
13092 flp->head = NULL;
13093 i = fip->nfnfields++;
13094 }
13095
13096 /* Create a new member function field and chain it to the field list
13097 entry. */
13098 new_fnfield = XNEW (struct nextfnfield);
13099 make_cleanup (xfree, new_fnfield);
13100 memset (new_fnfield, 0, sizeof (struct nextfnfield));
13101 new_fnfield->next = flp->head;
13102 flp->head = new_fnfield;
13103 flp->length++;
13104
13105 /* Fill in the member function field info. */
13106 fnp = &new_fnfield->fnfield;
13107
13108 /* Delay processing of the physname until later. */
13109 if (cu->language == language_cplus)
13110 {
13111 add_to_method_list (type, i, flp->length - 1, fieldname,
13112 die, cu);
13113 }
13114 else
13115 {
13116 const char *physname = dwarf2_physname (fieldname, die, cu);
13117 fnp->physname = physname ? physname : "";
13118 }
13119
13120 fnp->type = alloc_type (objfile);
13121 this_type = read_type_die (die, cu);
13122 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
13123 {
13124 int nparams = TYPE_NFIELDS (this_type);
13125
13126 /* TYPE is the domain of this method, and THIS_TYPE is the type
13127 of the method itself (TYPE_CODE_METHOD). */
13128 smash_to_method_type (fnp->type, type,
13129 TYPE_TARGET_TYPE (this_type),
13130 TYPE_FIELDS (this_type),
13131 TYPE_NFIELDS (this_type),
13132 TYPE_VARARGS (this_type));
13133
13134 /* Handle static member functions.
13135 Dwarf2 has no clean way to discern C++ static and non-static
13136 member functions. G++ helps GDB by marking the first
13137 parameter for non-static member functions (which is the this
13138 pointer) as artificial. We obtain this information from
13139 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
13140 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
13141 fnp->voffset = VOFFSET_STATIC;
13142 }
13143 else
13144 complaint (&symfile_complaints, _("member function type missing for '%s'"),
13145 dwarf2_full_name (fieldname, die, cu));
13146
13147 /* Get fcontext from DW_AT_containing_type if present. */
13148 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
13149 fnp->fcontext = die_containing_type (die, cu);
13150
13151 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
13152 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
13153
13154 /* Get accessibility. */
13155 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
13156 if (attr)
13157 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
13158 else
13159 accessibility = dwarf2_default_access_attribute (die, cu);
13160 switch (accessibility)
13161 {
13162 case DW_ACCESS_private:
13163 fnp->is_private = 1;
13164 break;
13165 case DW_ACCESS_protected:
13166 fnp->is_protected = 1;
13167 break;
13168 }
13169
13170 /* Check for artificial methods. */
13171 attr = dwarf2_attr (die, DW_AT_artificial, cu);
13172 if (attr && DW_UNSND (attr) != 0)
13173 fnp->is_artificial = 1;
13174
13175 fnp->is_constructor = dwarf2_is_constructor (die, cu);
13176
13177 /* Get index in virtual function table if it is a virtual member
13178 function. For older versions of GCC, this is an offset in the
13179 appropriate virtual table, as specified by DW_AT_containing_type.
13180 For everyone else, it is an expression to be evaluated relative
13181 to the object address. */
13182
13183 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
13184 if (attr)
13185 {
13186 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
13187 {
13188 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
13189 {
13190 /* Old-style GCC. */
13191 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
13192 }
13193 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
13194 || (DW_BLOCK (attr)->size > 1
13195 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
13196 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
13197 {
13198 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
13199 if ((fnp->voffset % cu->header.addr_size) != 0)
13200 dwarf2_complex_location_expr_complaint ();
13201 else
13202 fnp->voffset /= cu->header.addr_size;
13203 fnp->voffset += 2;
13204 }
13205 else
13206 dwarf2_complex_location_expr_complaint ();
13207
13208 if (!fnp->fcontext)
13209 {
13210 /* If there is no `this' field and no DW_AT_containing_type,
13211 we cannot actually find a base class context for the
13212 vtable! */
13213 if (TYPE_NFIELDS (this_type) == 0
13214 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
13215 {
13216 complaint (&symfile_complaints,
13217 _("cannot determine context for virtual member "
13218 "function \"%s\" (offset %d)"),
13219 fieldname, die->offset.sect_off);
13220 }
13221 else
13222 {
13223 fnp->fcontext
13224 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
13225 }
13226 }
13227 }
13228 else if (attr_form_is_section_offset (attr))
13229 {
13230 dwarf2_complex_location_expr_complaint ();
13231 }
13232 else
13233 {
13234 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
13235 fieldname);
13236 }
13237 }
13238 else
13239 {
13240 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
13241 if (attr && DW_UNSND (attr))
13242 {
13243 /* GCC does this, as of 2008-08-25; PR debug/37237. */
13244 complaint (&symfile_complaints,
13245 _("Member function \"%s\" (offset %d) is virtual "
13246 "but the vtable offset is not specified"),
13247 fieldname, die->offset.sect_off);
13248 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13249 TYPE_CPLUS_DYNAMIC (type) = 1;
13250 }
13251 }
13252 }
13253
13254 /* Create the vector of member function fields, and attach it to the type. */
13255
13256 static void
13257 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
13258 struct dwarf2_cu *cu)
13259 {
13260 struct fnfieldlist *flp;
13261 int i;
13262
13263 if (cu->language == language_ada)
13264 error (_("unexpected member functions in Ada type"));
13265
13266 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13267 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
13268 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
13269
13270 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
13271 {
13272 struct nextfnfield *nfp = flp->head;
13273 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
13274 int k;
13275
13276 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
13277 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
13278 fn_flp->fn_fields = (struct fn_field *)
13279 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
13280 for (k = flp->length; (k--, nfp); nfp = nfp->next)
13281 fn_flp->fn_fields[k] = nfp->fnfield;
13282 }
13283
13284 TYPE_NFN_FIELDS (type) = fip->nfnfields;
13285 }
13286
13287 /* Returns non-zero if NAME is the name of a vtable member in CU's
13288 language, zero otherwise. */
13289 static int
13290 is_vtable_name (const char *name, struct dwarf2_cu *cu)
13291 {
13292 static const char vptr[] = "_vptr";
13293 static const char vtable[] = "vtable";
13294
13295 /* Look for the C++ form of the vtable. */
13296 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
13297 return 1;
13298
13299 return 0;
13300 }
13301
13302 /* GCC outputs unnamed structures that are really pointers to member
13303 functions, with the ABI-specified layout. If TYPE describes
13304 such a structure, smash it into a member function type.
13305
13306 GCC shouldn't do this; it should just output pointer to member DIEs.
13307 This is GCC PR debug/28767. */
13308
13309 static void
13310 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
13311 {
13312 struct type *pfn_type, *self_type, *new_type;
13313
13314 /* Check for a structure with no name and two children. */
13315 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
13316 return;
13317
13318 /* Check for __pfn and __delta members. */
13319 if (TYPE_FIELD_NAME (type, 0) == NULL
13320 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
13321 || TYPE_FIELD_NAME (type, 1) == NULL
13322 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
13323 return;
13324
13325 /* Find the type of the method. */
13326 pfn_type = TYPE_FIELD_TYPE (type, 0);
13327 if (pfn_type == NULL
13328 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
13329 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
13330 return;
13331
13332 /* Look for the "this" argument. */
13333 pfn_type = TYPE_TARGET_TYPE (pfn_type);
13334 if (TYPE_NFIELDS (pfn_type) == 0
13335 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
13336 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
13337 return;
13338
13339 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
13340 new_type = alloc_type (objfile);
13341 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
13342 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
13343 TYPE_VARARGS (pfn_type));
13344 smash_to_methodptr_type (type, new_type);
13345 }
13346
13347 /* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
13348 (icc). */
13349
13350 static int
13351 producer_is_icc (struct dwarf2_cu *cu)
13352 {
13353 if (!cu->checked_producer)
13354 check_producer (cu);
13355
13356 return cu->producer_is_icc;
13357 }
13358
13359 /* Called when we find the DIE that starts a structure or union scope
13360 (definition) to create a type for the structure or union. Fill in
13361 the type's name and general properties; the members will not be
13362 processed until process_structure_scope. A symbol table entry for
13363 the type will also not be done until process_structure_scope (assuming
13364 the type has a name).
13365
13366 NOTE: we need to call these functions regardless of whether or not the
13367 DIE has a DW_AT_name attribute, since it might be an anonymous
13368 structure or union. This gets the type entered into our set of
13369 user defined types. */
13370
13371 static struct type *
13372 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
13373 {
13374 struct objfile *objfile = cu->objfile;
13375 struct type *type;
13376 struct attribute *attr;
13377 const char *name;
13378
13379 /* If the definition of this type lives in .debug_types, read that type.
13380 Don't follow DW_AT_specification though, that will take us back up
13381 the chain and we want to go down. */
13382 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
13383 if (attr)
13384 {
13385 type = get_DW_AT_signature_type (die, attr, cu);
13386
13387 /* The type's CU may not be the same as CU.
13388 Ensure TYPE is recorded with CU in die_type_hash. */
13389 return set_die_type (die, type, cu);
13390 }
13391
13392 type = alloc_type (objfile);
13393 INIT_CPLUS_SPECIFIC (type);
13394
13395 name = dwarf2_name (die, cu);
13396 if (name != NULL)
13397 {
13398 if (cu->language == language_cplus
13399 || cu->language == language_d
13400 || cu->language == language_rust)
13401 {
13402 const char *full_name = dwarf2_full_name (name, die, cu);
13403
13404 /* dwarf2_full_name might have already finished building the DIE's
13405 type. If so, there is no need to continue. */
13406 if (get_die_type (die, cu) != NULL)
13407 return get_die_type (die, cu);
13408
13409 TYPE_TAG_NAME (type) = full_name;
13410 if (die->tag == DW_TAG_structure_type
13411 || die->tag == DW_TAG_class_type)
13412 TYPE_NAME (type) = TYPE_TAG_NAME (type);
13413 }
13414 else
13415 {
13416 /* The name is already allocated along with this objfile, so
13417 we don't need to duplicate it for the type. */
13418 TYPE_TAG_NAME (type) = name;
13419 if (die->tag == DW_TAG_class_type)
13420 TYPE_NAME (type) = TYPE_TAG_NAME (type);
13421 }
13422 }
13423
13424 if (die->tag == DW_TAG_structure_type)
13425 {
13426 TYPE_CODE (type) = TYPE_CODE_STRUCT;
13427 }
13428 else if (die->tag == DW_TAG_union_type)
13429 {
13430 TYPE_CODE (type) = TYPE_CODE_UNION;
13431 }
13432 else
13433 {
13434 TYPE_CODE (type) = TYPE_CODE_STRUCT;
13435 }
13436
13437 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
13438 TYPE_DECLARED_CLASS (type) = 1;
13439
13440 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13441 if (attr)
13442 {
13443 if (attr_form_is_constant (attr))
13444 TYPE_LENGTH (type) = DW_UNSND (attr);
13445 else
13446 {
13447 /* For the moment, dynamic type sizes are not supported
13448 by GDB's struct type. The actual size is determined
13449 on-demand when resolving the type of a given object,
13450 so set the type's length to zero for now. Otherwise,
13451 we record an expression as the length, and that expression
13452 could lead to a very large value, which could eventually
13453 lead to us trying to allocate that much memory when creating
13454 a value of that type. */
13455 TYPE_LENGTH (type) = 0;
13456 }
13457 }
13458 else
13459 {
13460 TYPE_LENGTH (type) = 0;
13461 }
13462
13463 if (producer_is_icc (cu) && (TYPE_LENGTH (type) == 0))
13464 {
13465 /* ICC does not output the required DW_AT_declaration
13466 on incomplete types, but gives them a size of zero. */
13467 TYPE_STUB (type) = 1;
13468 }
13469 else
13470 TYPE_STUB_SUPPORTED (type) = 1;
13471
13472 if (die_is_declaration (die, cu))
13473 TYPE_STUB (type) = 1;
13474 else if (attr == NULL && die->child == NULL
13475 && producer_is_realview (cu->producer))
13476 /* RealView does not output the required DW_AT_declaration
13477 on incomplete types. */
13478 TYPE_STUB (type) = 1;
13479
13480 /* We need to add the type field to the die immediately so we don't
13481 infinitely recurse when dealing with pointers to the structure
13482 type within the structure itself. */
13483 set_die_type (die, type, cu);
13484
13485 /* set_die_type should be already done. */
13486 set_descriptive_type (type, die, cu);
13487
13488 return type;
13489 }
13490
13491 /* Finish creating a structure or union type, including filling in
13492 its members and creating a symbol for it. */
13493
13494 static void
13495 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
13496 {
13497 struct objfile *objfile = cu->objfile;
13498 struct die_info *child_die;
13499 struct type *type;
13500
13501 type = get_die_type (die, cu);
13502 if (type == NULL)
13503 type = read_structure_type (die, cu);
13504
13505 if (die->child != NULL && ! die_is_declaration (die, cu))
13506 {
13507 struct field_info fi;
13508 VEC (symbolp) *template_args = NULL;
13509 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
13510
13511 memset (&fi, 0, sizeof (struct field_info));
13512
13513 child_die = die->child;
13514
13515 while (child_die && child_die->tag)
13516 {
13517 if (child_die->tag == DW_TAG_member
13518 || child_die->tag == DW_TAG_variable)
13519 {
13520 /* NOTE: carlton/2002-11-05: A C++ static data member
13521 should be a DW_TAG_member that is a declaration, but
13522 all versions of G++ as of this writing (so through at
13523 least 3.2.1) incorrectly generate DW_TAG_variable
13524 tags for them instead. */
13525 dwarf2_add_field (&fi, child_die, cu);
13526 }
13527 else if (child_die->tag == DW_TAG_subprogram)
13528 {
13529 /* Rust doesn't have member functions in the C++ sense.
13530 However, it does emit ordinary functions as children
13531 of a struct DIE. */
13532 if (cu->language == language_rust)
13533 read_func_scope (child_die, cu);
13534 else
13535 {
13536 /* C++ member function. */
13537 dwarf2_add_member_fn (&fi, child_die, type, cu);
13538 }
13539 }
13540 else if (child_die->tag == DW_TAG_inheritance)
13541 {
13542 /* C++ base class field. */
13543 dwarf2_add_field (&fi, child_die, cu);
13544 }
13545 else if (child_die->tag == DW_TAG_typedef)
13546 dwarf2_add_typedef (&fi, child_die, cu);
13547 else if (child_die->tag == DW_TAG_template_type_param
13548 || child_die->tag == DW_TAG_template_value_param)
13549 {
13550 struct symbol *arg = new_symbol (child_die, NULL, cu);
13551
13552 if (arg != NULL)
13553 VEC_safe_push (symbolp, template_args, arg);
13554 }
13555
13556 child_die = sibling_die (child_die);
13557 }
13558
13559 /* Attach template arguments to type. */
13560 if (! VEC_empty (symbolp, template_args))
13561 {
13562 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13563 TYPE_N_TEMPLATE_ARGUMENTS (type)
13564 = VEC_length (symbolp, template_args);
13565 TYPE_TEMPLATE_ARGUMENTS (type)
13566 = XOBNEWVEC (&objfile->objfile_obstack,
13567 struct symbol *,
13568 TYPE_N_TEMPLATE_ARGUMENTS (type));
13569 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
13570 VEC_address (symbolp, template_args),
13571 (TYPE_N_TEMPLATE_ARGUMENTS (type)
13572 * sizeof (struct symbol *)));
13573 VEC_free (symbolp, template_args);
13574 }
13575
13576 /* Attach fields and member functions to the type. */
13577 if (fi.nfields)
13578 dwarf2_attach_fields_to_type (&fi, type, cu);
13579 if (fi.nfnfields)
13580 {
13581 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
13582
13583 /* Get the type which refers to the base class (possibly this
13584 class itself) which contains the vtable pointer for the current
13585 class from the DW_AT_containing_type attribute. This use of
13586 DW_AT_containing_type is a GNU extension. */
13587
13588 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
13589 {
13590 struct type *t = die_containing_type (die, cu);
13591
13592 set_type_vptr_basetype (type, t);
13593 if (type == t)
13594 {
13595 int i;
13596
13597 /* Our own class provides vtbl ptr. */
13598 for (i = TYPE_NFIELDS (t) - 1;
13599 i >= TYPE_N_BASECLASSES (t);
13600 --i)
13601 {
13602 const char *fieldname = TYPE_FIELD_NAME (t, i);
13603
13604 if (is_vtable_name (fieldname, cu))
13605 {
13606 set_type_vptr_fieldno (type, i);
13607 break;
13608 }
13609 }
13610
13611 /* Complain if virtual function table field not found. */
13612 if (i < TYPE_N_BASECLASSES (t))
13613 complaint (&symfile_complaints,
13614 _("virtual function table pointer "
13615 "not found when defining class '%s'"),
13616 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
13617 "");
13618 }
13619 else
13620 {
13621 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
13622 }
13623 }
13624 else if (cu->producer
13625 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
13626 {
13627 /* The IBM XLC compiler does not provide direct indication
13628 of the containing type, but the vtable pointer is
13629 always named __vfp. */
13630
13631 int i;
13632
13633 for (i = TYPE_NFIELDS (type) - 1;
13634 i >= TYPE_N_BASECLASSES (type);
13635 --i)
13636 {
13637 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
13638 {
13639 set_type_vptr_fieldno (type, i);
13640 set_type_vptr_basetype (type, type);
13641 break;
13642 }
13643 }
13644 }
13645 }
13646
13647 /* Copy fi.typedef_field_list linked list elements content into the
13648 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
13649 if (fi.typedef_field_list)
13650 {
13651 int i = fi.typedef_field_list_count;
13652
13653 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13654 TYPE_TYPEDEF_FIELD_ARRAY (type)
13655 = ((struct typedef_field *)
13656 TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i));
13657 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
13658
13659 /* Reverse the list order to keep the debug info elements order. */
13660 while (--i >= 0)
13661 {
13662 struct typedef_field *dest, *src;
13663
13664 dest = &TYPE_TYPEDEF_FIELD (type, i);
13665 src = &fi.typedef_field_list->field;
13666 fi.typedef_field_list = fi.typedef_field_list->next;
13667 *dest = *src;
13668 }
13669 }
13670
13671 do_cleanups (back_to);
13672 }
13673
13674 quirk_gcc_member_function_pointer (type, objfile);
13675
13676 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
13677 snapshots) has been known to create a die giving a declaration
13678 for a class that has, as a child, a die giving a definition for a
13679 nested class. So we have to process our children even if the
13680 current die is a declaration. Normally, of course, a declaration
13681 won't have any children at all. */
13682
13683 child_die = die->child;
13684
13685 while (child_die != NULL && child_die->tag)
13686 {
13687 if (child_die->tag == DW_TAG_member
13688 || child_die->tag == DW_TAG_variable
13689 || child_die->tag == DW_TAG_inheritance
13690 || child_die->tag == DW_TAG_template_value_param
13691 || child_die->tag == DW_TAG_template_type_param)
13692 {
13693 /* Do nothing. */
13694 }
13695 else
13696 process_die (child_die, cu);
13697
13698 child_die = sibling_die (child_die);
13699 }
13700
13701 /* Do not consider external references. According to the DWARF standard,
13702 these DIEs are identified by the fact that they have no byte_size
13703 attribute, and a declaration attribute. */
13704 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
13705 || !die_is_declaration (die, cu))
13706 new_symbol (die, type, cu);
13707 }
13708
13709 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
13710 update TYPE using some information only available in DIE's children. */
13711
13712 static void
13713 update_enumeration_type_from_children (struct die_info *die,
13714 struct type *type,
13715 struct dwarf2_cu *cu)
13716 {
13717 struct obstack obstack;
13718 struct die_info *child_die;
13719 int unsigned_enum = 1;
13720 int flag_enum = 1;
13721 ULONGEST mask = 0;
13722 struct cleanup *old_chain;
13723
13724 obstack_init (&obstack);
13725 old_chain = make_cleanup_obstack_free (&obstack);
13726
13727 for (child_die = die->child;
13728 child_die != NULL && child_die->tag;
13729 child_die = sibling_die (child_die))
13730 {
13731 struct attribute *attr;
13732 LONGEST value;
13733 const gdb_byte *bytes;
13734 struct dwarf2_locexpr_baton *baton;
13735 const char *name;
13736
13737 if (child_die->tag != DW_TAG_enumerator)
13738 continue;
13739
13740 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
13741 if (attr == NULL)
13742 continue;
13743
13744 name = dwarf2_name (child_die, cu);
13745 if (name == NULL)
13746 name = "<anonymous enumerator>";
13747
13748 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
13749 &value, &bytes, &baton);
13750 if (value < 0)
13751 {
13752 unsigned_enum = 0;
13753 flag_enum = 0;
13754 }
13755 else if ((mask & value) != 0)
13756 flag_enum = 0;
13757 else
13758 mask |= value;
13759
13760 /* If we already know that the enum type is neither unsigned, nor
13761 a flag type, no need to look at the rest of the enumerates. */
13762 if (!unsigned_enum && !flag_enum)
13763 break;
13764 }
13765
13766 if (unsigned_enum)
13767 TYPE_UNSIGNED (type) = 1;
13768 if (flag_enum)
13769 TYPE_FLAG_ENUM (type) = 1;
13770
13771 do_cleanups (old_chain);
13772 }
13773
13774 /* Given a DW_AT_enumeration_type die, set its type. We do not
13775 complete the type's fields yet, or create any symbols. */
13776
13777 static struct type *
13778 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
13779 {
13780 struct objfile *objfile = cu->objfile;
13781 struct type *type;
13782 struct attribute *attr;
13783 const char *name;
13784
13785 /* If the definition of this type lives in .debug_types, read that type.
13786 Don't follow DW_AT_specification though, that will take us back up
13787 the chain and we want to go down. */
13788 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
13789 if (attr)
13790 {
13791 type = get_DW_AT_signature_type (die, attr, cu);
13792
13793 /* The type's CU may not be the same as CU.
13794 Ensure TYPE is recorded with CU in die_type_hash. */
13795 return set_die_type (die, type, cu);
13796 }
13797
13798 type = alloc_type (objfile);
13799
13800 TYPE_CODE (type) = TYPE_CODE_ENUM;
13801 name = dwarf2_full_name (NULL, die, cu);
13802 if (name != NULL)
13803 TYPE_TAG_NAME (type) = name;
13804
13805 attr = dwarf2_attr (die, DW_AT_type, cu);
13806 if (attr != NULL)
13807 {
13808 struct type *underlying_type = die_type (die, cu);
13809
13810 TYPE_TARGET_TYPE (type) = underlying_type;
13811 }
13812
13813 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13814 if (attr)
13815 {
13816 TYPE_LENGTH (type) = DW_UNSND (attr);
13817 }
13818 else
13819 {
13820 TYPE_LENGTH (type) = 0;
13821 }
13822
13823 /* The enumeration DIE can be incomplete. In Ada, any type can be
13824 declared as private in the package spec, and then defined only
13825 inside the package body. Such types are known as Taft Amendment
13826 Types. When another package uses such a type, an incomplete DIE
13827 may be generated by the compiler. */
13828 if (die_is_declaration (die, cu))
13829 TYPE_STUB (type) = 1;
13830
13831 /* Finish the creation of this type by using the enum's children.
13832 We must call this even when the underlying type has been provided
13833 so that we can determine if we're looking at a "flag" enum. */
13834 update_enumeration_type_from_children (die, type, cu);
13835
13836 /* If this type has an underlying type that is not a stub, then we
13837 may use its attributes. We always use the "unsigned" attribute
13838 in this situation, because ordinarily we guess whether the type
13839 is unsigned -- but the guess can be wrong and the underlying type
13840 can tell us the reality. However, we defer to a local size
13841 attribute if one exists, because this lets the compiler override
13842 the underlying type if needed. */
13843 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
13844 {
13845 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
13846 if (TYPE_LENGTH (type) == 0)
13847 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
13848 }
13849
13850 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
13851
13852 return set_die_type (die, type, cu);
13853 }
13854
13855 /* Given a pointer to a die which begins an enumeration, process all
13856 the dies that define the members of the enumeration, and create the
13857 symbol for the enumeration type.
13858
13859 NOTE: We reverse the order of the element list. */
13860
13861 static void
13862 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
13863 {
13864 struct type *this_type;
13865
13866 this_type = get_die_type (die, cu);
13867 if (this_type == NULL)
13868 this_type = read_enumeration_type (die, cu);
13869
13870 if (die->child != NULL)
13871 {
13872 struct die_info *child_die;
13873 struct symbol *sym;
13874 struct field *fields = NULL;
13875 int num_fields = 0;
13876 const char *name;
13877
13878 child_die = die->child;
13879 while (child_die && child_die->tag)
13880 {
13881 if (child_die->tag != DW_TAG_enumerator)
13882 {
13883 process_die (child_die, cu);
13884 }
13885 else
13886 {
13887 name = dwarf2_name (child_die, cu);
13888 if (name)
13889 {
13890 sym = new_symbol (child_die, this_type, cu);
13891
13892 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
13893 {
13894 fields = (struct field *)
13895 xrealloc (fields,
13896 (num_fields + DW_FIELD_ALLOC_CHUNK)
13897 * sizeof (struct field));
13898 }
13899
13900 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
13901 FIELD_TYPE (fields[num_fields]) = NULL;
13902 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
13903 FIELD_BITSIZE (fields[num_fields]) = 0;
13904
13905 num_fields++;
13906 }
13907 }
13908
13909 child_die = sibling_die (child_die);
13910 }
13911
13912 if (num_fields)
13913 {
13914 TYPE_NFIELDS (this_type) = num_fields;
13915 TYPE_FIELDS (this_type) = (struct field *)
13916 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
13917 memcpy (TYPE_FIELDS (this_type), fields,
13918 sizeof (struct field) * num_fields);
13919 xfree (fields);
13920 }
13921 }
13922
13923 /* If we are reading an enum from a .debug_types unit, and the enum
13924 is a declaration, and the enum is not the signatured type in the
13925 unit, then we do not want to add a symbol for it. Adding a
13926 symbol would in some cases obscure the true definition of the
13927 enum, giving users an incomplete type when the definition is
13928 actually available. Note that we do not want to do this for all
13929 enums which are just declarations, because C++0x allows forward
13930 enum declarations. */
13931 if (cu->per_cu->is_debug_types
13932 && die_is_declaration (die, cu))
13933 {
13934 struct signatured_type *sig_type;
13935
13936 sig_type = (struct signatured_type *) cu->per_cu;
13937 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
13938 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
13939 return;
13940 }
13941
13942 new_symbol (die, this_type, cu);
13943 }
13944
13945 /* Extract all information from a DW_TAG_array_type DIE and put it in
13946 the DIE's type field. For now, this only handles one dimensional
13947 arrays. */
13948
13949 static struct type *
13950 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
13951 {
13952 struct objfile *objfile = cu->objfile;
13953 struct die_info *child_die;
13954 struct type *type;
13955 struct type *element_type, *range_type, *index_type;
13956 struct type **range_types = NULL;
13957 struct attribute *attr;
13958 int ndim = 0;
13959 struct cleanup *back_to;
13960 const char *name;
13961 unsigned int bit_stride = 0;
13962
13963 element_type = die_type (die, cu);
13964
13965 /* The die_type call above may have already set the type for this DIE. */
13966 type = get_die_type (die, cu);
13967 if (type)
13968 return type;
13969
13970 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
13971 if (attr != NULL)
13972 bit_stride = DW_UNSND (attr) * 8;
13973
13974 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
13975 if (attr != NULL)
13976 bit_stride = DW_UNSND (attr);
13977
13978 /* Irix 6.2 native cc creates array types without children for
13979 arrays with unspecified length. */
13980 if (die->child == NULL)
13981 {
13982 index_type = objfile_type (objfile)->builtin_int;
13983 range_type = create_static_range_type (NULL, index_type, 0, -1);
13984 type = create_array_type_with_stride (NULL, element_type, range_type,
13985 bit_stride);
13986 return set_die_type (die, type, cu);
13987 }
13988
13989 back_to = make_cleanup (null_cleanup, NULL);
13990 child_die = die->child;
13991 while (child_die && child_die->tag)
13992 {
13993 if (child_die->tag == DW_TAG_subrange_type)
13994 {
13995 struct type *child_type = read_type_die (child_die, cu);
13996
13997 if (child_type != NULL)
13998 {
13999 /* The range type was succesfully read. Save it for the
14000 array type creation. */
14001 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
14002 {
14003 range_types = (struct type **)
14004 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
14005 * sizeof (struct type *));
14006 if (ndim == 0)
14007 make_cleanup (free_current_contents, &range_types);
14008 }
14009 range_types[ndim++] = child_type;
14010 }
14011 }
14012 child_die = sibling_die (child_die);
14013 }
14014
14015 /* Dwarf2 dimensions are output from left to right, create the
14016 necessary array types in backwards order. */
14017
14018 type = element_type;
14019
14020 if (read_array_order (die, cu) == DW_ORD_col_major)
14021 {
14022 int i = 0;
14023
14024 while (i < ndim)
14025 type = create_array_type_with_stride (NULL, type, range_types[i++],
14026 bit_stride);
14027 }
14028 else
14029 {
14030 while (ndim-- > 0)
14031 type = create_array_type_with_stride (NULL, type, range_types[ndim],
14032 bit_stride);
14033 }
14034
14035 /* Understand Dwarf2 support for vector types (like they occur on
14036 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
14037 array type. This is not part of the Dwarf2/3 standard yet, but a
14038 custom vendor extension. The main difference between a regular
14039 array and the vector variant is that vectors are passed by value
14040 to functions. */
14041 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
14042 if (attr)
14043 make_vector_type (type);
14044
14045 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
14046 implementation may choose to implement triple vectors using this
14047 attribute. */
14048 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14049 if (attr)
14050 {
14051 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
14052 TYPE_LENGTH (type) = DW_UNSND (attr);
14053 else
14054 complaint (&symfile_complaints,
14055 _("DW_AT_byte_size for array type smaller "
14056 "than the total size of elements"));
14057 }
14058
14059 name = dwarf2_name (die, cu);
14060 if (name)
14061 TYPE_NAME (type) = name;
14062
14063 /* Install the type in the die. */
14064 set_die_type (die, type, cu);
14065
14066 /* set_die_type should be already done. */
14067 set_descriptive_type (type, die, cu);
14068
14069 do_cleanups (back_to);
14070
14071 return type;
14072 }
14073
14074 static enum dwarf_array_dim_ordering
14075 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
14076 {
14077 struct attribute *attr;
14078
14079 attr = dwarf2_attr (die, DW_AT_ordering, cu);
14080
14081 if (attr)
14082 return (enum dwarf_array_dim_ordering) DW_SND (attr);
14083
14084 /* GNU F77 is a special case, as at 08/2004 array type info is the
14085 opposite order to the dwarf2 specification, but data is still
14086 laid out as per normal fortran.
14087
14088 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
14089 version checking. */
14090
14091 if (cu->language == language_fortran
14092 && cu->producer && strstr (cu->producer, "GNU F77"))
14093 {
14094 return DW_ORD_row_major;
14095 }
14096
14097 switch (cu->language_defn->la_array_ordering)
14098 {
14099 case array_column_major:
14100 return DW_ORD_col_major;
14101 case array_row_major:
14102 default:
14103 return DW_ORD_row_major;
14104 };
14105 }
14106
14107 /* Extract all information from a DW_TAG_set_type DIE and put it in
14108 the DIE's type field. */
14109
14110 static struct type *
14111 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
14112 {
14113 struct type *domain_type, *set_type;
14114 struct attribute *attr;
14115
14116 domain_type = die_type (die, cu);
14117
14118 /* The die_type call above may have already set the type for this DIE. */
14119 set_type = get_die_type (die, cu);
14120 if (set_type)
14121 return set_type;
14122
14123 set_type = create_set_type (NULL, domain_type);
14124
14125 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14126 if (attr)
14127 TYPE_LENGTH (set_type) = DW_UNSND (attr);
14128
14129 return set_die_type (die, set_type, cu);
14130 }
14131
14132 /* A helper for read_common_block that creates a locexpr baton.
14133 SYM is the symbol which we are marking as computed.
14134 COMMON_DIE is the DIE for the common block.
14135 COMMON_LOC is the location expression attribute for the common
14136 block itself.
14137 MEMBER_LOC is the location expression attribute for the particular
14138 member of the common block that we are processing.
14139 CU is the CU from which the above come. */
14140
14141 static void
14142 mark_common_block_symbol_computed (struct symbol *sym,
14143 struct die_info *common_die,
14144 struct attribute *common_loc,
14145 struct attribute *member_loc,
14146 struct dwarf2_cu *cu)
14147 {
14148 struct objfile *objfile = dwarf2_per_objfile->objfile;
14149 struct dwarf2_locexpr_baton *baton;
14150 gdb_byte *ptr;
14151 unsigned int cu_off;
14152 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
14153 LONGEST offset = 0;
14154
14155 gdb_assert (common_loc && member_loc);
14156 gdb_assert (attr_form_is_block (common_loc));
14157 gdb_assert (attr_form_is_block (member_loc)
14158 || attr_form_is_constant (member_loc));
14159
14160 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14161 baton->per_cu = cu->per_cu;
14162 gdb_assert (baton->per_cu);
14163
14164 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
14165
14166 if (attr_form_is_constant (member_loc))
14167 {
14168 offset = dwarf2_get_attr_constant_value (member_loc, 0);
14169 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
14170 }
14171 else
14172 baton->size += DW_BLOCK (member_loc)->size;
14173
14174 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
14175 baton->data = ptr;
14176
14177 *ptr++ = DW_OP_call4;
14178 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
14179 store_unsigned_integer (ptr, 4, byte_order, cu_off);
14180 ptr += 4;
14181
14182 if (attr_form_is_constant (member_loc))
14183 {
14184 *ptr++ = DW_OP_addr;
14185 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
14186 ptr += cu->header.addr_size;
14187 }
14188 else
14189 {
14190 /* We have to copy the data here, because DW_OP_call4 will only
14191 use a DW_AT_location attribute. */
14192 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
14193 ptr += DW_BLOCK (member_loc)->size;
14194 }
14195
14196 *ptr++ = DW_OP_plus;
14197 gdb_assert (ptr - baton->data == baton->size);
14198
14199 SYMBOL_LOCATION_BATON (sym) = baton;
14200 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
14201 }
14202
14203 /* Create appropriate locally-scoped variables for all the
14204 DW_TAG_common_block entries. Also create a struct common_block
14205 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
14206 is used to sepate the common blocks name namespace from regular
14207 variable names. */
14208
14209 static void
14210 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
14211 {
14212 struct attribute *attr;
14213
14214 attr = dwarf2_attr (die, DW_AT_location, cu);
14215 if (attr)
14216 {
14217 /* Support the .debug_loc offsets. */
14218 if (attr_form_is_block (attr))
14219 {
14220 /* Ok. */
14221 }
14222 else if (attr_form_is_section_offset (attr))
14223 {
14224 dwarf2_complex_location_expr_complaint ();
14225 attr = NULL;
14226 }
14227 else
14228 {
14229 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
14230 "common block member");
14231 attr = NULL;
14232 }
14233 }
14234
14235 if (die->child != NULL)
14236 {
14237 struct objfile *objfile = cu->objfile;
14238 struct die_info *child_die;
14239 size_t n_entries = 0, size;
14240 struct common_block *common_block;
14241 struct symbol *sym;
14242
14243 for (child_die = die->child;
14244 child_die && child_die->tag;
14245 child_die = sibling_die (child_die))
14246 ++n_entries;
14247
14248 size = (sizeof (struct common_block)
14249 + (n_entries - 1) * sizeof (struct symbol *));
14250 common_block
14251 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
14252 size);
14253 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
14254 common_block->n_entries = 0;
14255
14256 for (child_die = die->child;
14257 child_die && child_die->tag;
14258 child_die = sibling_die (child_die))
14259 {
14260 /* Create the symbol in the DW_TAG_common_block block in the current
14261 symbol scope. */
14262 sym = new_symbol (child_die, NULL, cu);
14263 if (sym != NULL)
14264 {
14265 struct attribute *member_loc;
14266
14267 common_block->contents[common_block->n_entries++] = sym;
14268
14269 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
14270 cu);
14271 if (member_loc)
14272 {
14273 /* GDB has handled this for a long time, but it is
14274 not specified by DWARF. It seems to have been
14275 emitted by gfortran at least as recently as:
14276 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
14277 complaint (&symfile_complaints,
14278 _("Variable in common block has "
14279 "DW_AT_data_member_location "
14280 "- DIE at 0x%x [in module %s]"),
14281 child_die->offset.sect_off,
14282 objfile_name (cu->objfile));
14283
14284 if (attr_form_is_section_offset (member_loc))
14285 dwarf2_complex_location_expr_complaint ();
14286 else if (attr_form_is_constant (member_loc)
14287 || attr_form_is_block (member_loc))
14288 {
14289 if (attr)
14290 mark_common_block_symbol_computed (sym, die, attr,
14291 member_loc, cu);
14292 }
14293 else
14294 dwarf2_complex_location_expr_complaint ();
14295 }
14296 }
14297 }
14298
14299 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
14300 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
14301 }
14302 }
14303
14304 /* Create a type for a C++ namespace. */
14305
14306 static struct type *
14307 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
14308 {
14309 struct objfile *objfile = cu->objfile;
14310 const char *previous_prefix, *name;
14311 int is_anonymous;
14312 struct type *type;
14313
14314 /* For extensions, reuse the type of the original namespace. */
14315 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
14316 {
14317 struct die_info *ext_die;
14318 struct dwarf2_cu *ext_cu = cu;
14319
14320 ext_die = dwarf2_extension (die, &ext_cu);
14321 type = read_type_die (ext_die, ext_cu);
14322
14323 /* EXT_CU may not be the same as CU.
14324 Ensure TYPE is recorded with CU in die_type_hash. */
14325 return set_die_type (die, type, cu);
14326 }
14327
14328 name = namespace_name (die, &is_anonymous, cu);
14329
14330 /* Now build the name of the current namespace. */
14331
14332 previous_prefix = determine_prefix (die, cu);
14333 if (previous_prefix[0] != '\0')
14334 name = typename_concat (&objfile->objfile_obstack,
14335 previous_prefix, name, 0, cu);
14336
14337 /* Create the type. */
14338 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
14339 TYPE_TAG_NAME (type) = TYPE_NAME (type);
14340
14341 return set_die_type (die, type, cu);
14342 }
14343
14344 /* Read a namespace scope. */
14345
14346 static void
14347 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
14348 {
14349 struct objfile *objfile = cu->objfile;
14350 int is_anonymous;
14351
14352 /* Add a symbol associated to this if we haven't seen the namespace
14353 before. Also, add a using directive if it's an anonymous
14354 namespace. */
14355
14356 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
14357 {
14358 struct type *type;
14359
14360 type = read_type_die (die, cu);
14361 new_symbol (die, type, cu);
14362
14363 namespace_name (die, &is_anonymous, cu);
14364 if (is_anonymous)
14365 {
14366 const char *previous_prefix = determine_prefix (die, cu);
14367
14368 add_using_directive (using_directives (cu->language),
14369 previous_prefix, TYPE_NAME (type), NULL,
14370 NULL, NULL, 0, &objfile->objfile_obstack);
14371 }
14372 }
14373
14374 if (die->child != NULL)
14375 {
14376 struct die_info *child_die = die->child;
14377
14378 while (child_die && child_die->tag)
14379 {
14380 process_die (child_die, cu);
14381 child_die = sibling_die (child_die);
14382 }
14383 }
14384 }
14385
14386 /* Read a Fortran module as type. This DIE can be only a declaration used for
14387 imported module. Still we need that type as local Fortran "use ... only"
14388 declaration imports depend on the created type in determine_prefix. */
14389
14390 static struct type *
14391 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
14392 {
14393 struct objfile *objfile = cu->objfile;
14394 const char *module_name;
14395 struct type *type;
14396
14397 module_name = dwarf2_name (die, cu);
14398 if (!module_name)
14399 complaint (&symfile_complaints,
14400 _("DW_TAG_module has no name, offset 0x%x"),
14401 die->offset.sect_off);
14402 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
14403
14404 /* determine_prefix uses TYPE_TAG_NAME. */
14405 TYPE_TAG_NAME (type) = TYPE_NAME (type);
14406
14407 return set_die_type (die, type, cu);
14408 }
14409
14410 /* Read a Fortran module. */
14411
14412 static void
14413 read_module (struct die_info *die, struct dwarf2_cu *cu)
14414 {
14415 struct die_info *child_die = die->child;
14416 struct type *type;
14417
14418 type = read_type_die (die, cu);
14419 new_symbol (die, type, cu);
14420
14421 while (child_die && child_die->tag)
14422 {
14423 process_die (child_die, cu);
14424 child_die = sibling_die (child_die);
14425 }
14426 }
14427
14428 /* Return the name of the namespace represented by DIE. Set
14429 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
14430 namespace. */
14431
14432 static const char *
14433 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
14434 {
14435 struct die_info *current_die;
14436 const char *name = NULL;
14437
14438 /* Loop through the extensions until we find a name. */
14439
14440 for (current_die = die;
14441 current_die != NULL;
14442 current_die = dwarf2_extension (die, &cu))
14443 {
14444 /* We don't use dwarf2_name here so that we can detect the absence
14445 of a name -> anonymous namespace. */
14446 name = dwarf2_string_attr (die, DW_AT_name, cu);
14447
14448 if (name != NULL)
14449 break;
14450 }
14451
14452 /* Is it an anonymous namespace? */
14453
14454 *is_anonymous = (name == NULL);
14455 if (*is_anonymous)
14456 name = CP_ANONYMOUS_NAMESPACE_STR;
14457
14458 return name;
14459 }
14460
14461 /* Extract all information from a DW_TAG_pointer_type DIE and add to
14462 the user defined type vector. */
14463
14464 static struct type *
14465 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
14466 {
14467 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
14468 struct comp_unit_head *cu_header = &cu->header;
14469 struct type *type;
14470 struct attribute *attr_byte_size;
14471 struct attribute *attr_address_class;
14472 int byte_size, addr_class;
14473 struct type *target_type;
14474
14475 target_type = die_type (die, cu);
14476
14477 /* The die_type call above may have already set the type for this DIE. */
14478 type = get_die_type (die, cu);
14479 if (type)
14480 return type;
14481
14482 type = lookup_pointer_type (target_type);
14483
14484 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
14485 if (attr_byte_size)
14486 byte_size = DW_UNSND (attr_byte_size);
14487 else
14488 byte_size = cu_header->addr_size;
14489
14490 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
14491 if (attr_address_class)
14492 addr_class = DW_UNSND (attr_address_class);
14493 else
14494 addr_class = DW_ADDR_none;
14495
14496 /* If the pointer size or address class is different than the
14497 default, create a type variant marked as such and set the
14498 length accordingly. */
14499 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
14500 {
14501 if (gdbarch_address_class_type_flags_p (gdbarch))
14502 {
14503 int type_flags;
14504
14505 type_flags = gdbarch_address_class_type_flags
14506 (gdbarch, byte_size, addr_class);
14507 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
14508 == 0);
14509 type = make_type_with_address_space (type, type_flags);
14510 }
14511 else if (TYPE_LENGTH (type) != byte_size)
14512 {
14513 complaint (&symfile_complaints,
14514 _("invalid pointer size %d"), byte_size);
14515 }
14516 else
14517 {
14518 /* Should we also complain about unhandled address classes? */
14519 }
14520 }
14521
14522 TYPE_LENGTH (type) = byte_size;
14523 return set_die_type (die, type, cu);
14524 }
14525
14526 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
14527 the user defined type vector. */
14528
14529 static struct type *
14530 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
14531 {
14532 struct type *type;
14533 struct type *to_type;
14534 struct type *domain;
14535
14536 to_type = die_type (die, cu);
14537 domain = die_containing_type (die, cu);
14538
14539 /* The calls above may have already set the type for this DIE. */
14540 type = get_die_type (die, cu);
14541 if (type)
14542 return type;
14543
14544 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
14545 type = lookup_methodptr_type (to_type);
14546 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
14547 {
14548 struct type *new_type = alloc_type (cu->objfile);
14549
14550 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
14551 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
14552 TYPE_VARARGS (to_type));
14553 type = lookup_methodptr_type (new_type);
14554 }
14555 else
14556 type = lookup_memberptr_type (to_type, domain);
14557
14558 return set_die_type (die, type, cu);
14559 }
14560
14561 /* Extract all information from a DW_TAG_reference_type DIE and add to
14562 the user defined type vector. */
14563
14564 static struct type *
14565 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
14566 {
14567 struct comp_unit_head *cu_header = &cu->header;
14568 struct type *type, *target_type;
14569 struct attribute *attr;
14570
14571 target_type = die_type (die, cu);
14572
14573 /* The die_type call above may have already set the type for this DIE. */
14574 type = get_die_type (die, cu);
14575 if (type)
14576 return type;
14577
14578 type = lookup_reference_type (target_type);
14579 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14580 if (attr)
14581 {
14582 TYPE_LENGTH (type) = DW_UNSND (attr);
14583 }
14584 else
14585 {
14586 TYPE_LENGTH (type) = cu_header->addr_size;
14587 }
14588 return set_die_type (die, type, cu);
14589 }
14590
14591 /* Add the given cv-qualifiers to the element type of the array. GCC
14592 outputs DWARF type qualifiers that apply to an array, not the
14593 element type. But GDB relies on the array element type to carry
14594 the cv-qualifiers. This mimics section 6.7.3 of the C99
14595 specification. */
14596
14597 static struct type *
14598 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
14599 struct type *base_type, int cnst, int voltl)
14600 {
14601 struct type *el_type, *inner_array;
14602
14603 base_type = copy_type (base_type);
14604 inner_array = base_type;
14605
14606 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
14607 {
14608 TYPE_TARGET_TYPE (inner_array) =
14609 copy_type (TYPE_TARGET_TYPE (inner_array));
14610 inner_array = TYPE_TARGET_TYPE (inner_array);
14611 }
14612
14613 el_type = TYPE_TARGET_TYPE (inner_array);
14614 cnst |= TYPE_CONST (el_type);
14615 voltl |= TYPE_VOLATILE (el_type);
14616 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
14617
14618 return set_die_type (die, base_type, cu);
14619 }
14620
14621 static struct type *
14622 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
14623 {
14624 struct type *base_type, *cv_type;
14625
14626 base_type = die_type (die, cu);
14627
14628 /* The die_type call above may have already set the type for this DIE. */
14629 cv_type = get_die_type (die, cu);
14630 if (cv_type)
14631 return cv_type;
14632
14633 /* In case the const qualifier is applied to an array type, the element type
14634 is so qualified, not the array type (section 6.7.3 of C99). */
14635 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
14636 return add_array_cv_type (die, cu, base_type, 1, 0);
14637
14638 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
14639 return set_die_type (die, cv_type, cu);
14640 }
14641
14642 static struct type *
14643 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
14644 {
14645 struct type *base_type, *cv_type;
14646
14647 base_type = die_type (die, cu);
14648
14649 /* The die_type call above may have already set the type for this DIE. */
14650 cv_type = get_die_type (die, cu);
14651 if (cv_type)
14652 return cv_type;
14653
14654 /* In case the volatile qualifier is applied to an array type, the
14655 element type is so qualified, not the array type (section 6.7.3
14656 of C99). */
14657 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
14658 return add_array_cv_type (die, cu, base_type, 0, 1);
14659
14660 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
14661 return set_die_type (die, cv_type, cu);
14662 }
14663
14664 /* Handle DW_TAG_restrict_type. */
14665
14666 static struct type *
14667 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
14668 {
14669 struct type *base_type, *cv_type;
14670
14671 base_type = die_type (die, cu);
14672
14673 /* The die_type call above may have already set the type for this DIE. */
14674 cv_type = get_die_type (die, cu);
14675 if (cv_type)
14676 return cv_type;
14677
14678 cv_type = make_restrict_type (base_type);
14679 return set_die_type (die, cv_type, cu);
14680 }
14681
14682 /* Handle DW_TAG_atomic_type. */
14683
14684 static struct type *
14685 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
14686 {
14687 struct type *base_type, *cv_type;
14688
14689 base_type = die_type (die, cu);
14690
14691 /* The die_type call above may have already set the type for this DIE. */
14692 cv_type = get_die_type (die, cu);
14693 if (cv_type)
14694 return cv_type;
14695
14696 cv_type = make_atomic_type (base_type);
14697 return set_die_type (die, cv_type, cu);
14698 }
14699
14700 /* Extract all information from a DW_TAG_string_type DIE and add to
14701 the user defined type vector. It isn't really a user defined type,
14702 but it behaves like one, with other DIE's using an AT_user_def_type
14703 attribute to reference it. */
14704
14705 static struct type *
14706 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
14707 {
14708 struct objfile *objfile = cu->objfile;
14709 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14710 struct type *type, *range_type, *index_type, *char_type;
14711 struct attribute *attr;
14712 unsigned int length;
14713
14714 attr = dwarf2_attr (die, DW_AT_string_length, cu);
14715 if (attr)
14716 {
14717 length = DW_UNSND (attr);
14718 }
14719 else
14720 {
14721 /* Check for the DW_AT_byte_size attribute. */
14722 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14723 if (attr)
14724 {
14725 length = DW_UNSND (attr);
14726 }
14727 else
14728 {
14729 length = 1;
14730 }
14731 }
14732
14733 index_type = objfile_type (objfile)->builtin_int;
14734 range_type = create_static_range_type (NULL, index_type, 1, length);
14735 char_type = language_string_char_type (cu->language_defn, gdbarch);
14736 type = create_string_type (NULL, char_type, range_type);
14737
14738 return set_die_type (die, type, cu);
14739 }
14740
14741 /* Assuming that DIE corresponds to a function, returns nonzero
14742 if the function is prototyped. */
14743
14744 static int
14745 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
14746 {
14747 struct attribute *attr;
14748
14749 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
14750 if (attr && (DW_UNSND (attr) != 0))
14751 return 1;
14752
14753 /* The DWARF standard implies that the DW_AT_prototyped attribute
14754 is only meaninful for C, but the concept also extends to other
14755 languages that allow unprototyped functions (Eg: Objective C).
14756 For all other languages, assume that functions are always
14757 prototyped. */
14758 if (cu->language != language_c
14759 && cu->language != language_objc
14760 && cu->language != language_opencl)
14761 return 1;
14762
14763 /* RealView does not emit DW_AT_prototyped. We can not distinguish
14764 prototyped and unprototyped functions; default to prototyped,
14765 since that is more common in modern code (and RealView warns
14766 about unprototyped functions). */
14767 if (producer_is_realview (cu->producer))
14768 return 1;
14769
14770 return 0;
14771 }
14772
14773 /* Handle DIES due to C code like:
14774
14775 struct foo
14776 {
14777 int (*funcp)(int a, long l);
14778 int b;
14779 };
14780
14781 ('funcp' generates a DW_TAG_subroutine_type DIE). */
14782
14783 static struct type *
14784 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
14785 {
14786 struct objfile *objfile = cu->objfile;
14787 struct type *type; /* Type that this function returns. */
14788 struct type *ftype; /* Function that returns above type. */
14789 struct attribute *attr;
14790
14791 type = die_type (die, cu);
14792
14793 /* The die_type call above may have already set the type for this DIE. */
14794 ftype = get_die_type (die, cu);
14795 if (ftype)
14796 return ftype;
14797
14798 ftype = lookup_function_type (type);
14799
14800 if (prototyped_function_p (die, cu))
14801 TYPE_PROTOTYPED (ftype) = 1;
14802
14803 /* Store the calling convention in the type if it's available in
14804 the subroutine die. Otherwise set the calling convention to
14805 the default value DW_CC_normal. */
14806 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
14807 if (attr)
14808 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
14809 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
14810 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
14811 else
14812 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
14813
14814 /* Record whether the function returns normally to its caller or not
14815 if the DWARF producer set that information. */
14816 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
14817 if (attr && (DW_UNSND (attr) != 0))
14818 TYPE_NO_RETURN (ftype) = 1;
14819
14820 /* We need to add the subroutine type to the die immediately so
14821 we don't infinitely recurse when dealing with parameters
14822 declared as the same subroutine type. */
14823 set_die_type (die, ftype, cu);
14824
14825 if (die->child != NULL)
14826 {
14827 struct type *void_type = objfile_type (objfile)->builtin_void;
14828 struct die_info *child_die;
14829 int nparams, iparams;
14830
14831 /* Count the number of parameters.
14832 FIXME: GDB currently ignores vararg functions, but knows about
14833 vararg member functions. */
14834 nparams = 0;
14835 child_die = die->child;
14836 while (child_die && child_die->tag)
14837 {
14838 if (child_die->tag == DW_TAG_formal_parameter)
14839 nparams++;
14840 else if (child_die->tag == DW_TAG_unspecified_parameters)
14841 TYPE_VARARGS (ftype) = 1;
14842 child_die = sibling_die (child_die);
14843 }
14844
14845 /* Allocate storage for parameters and fill them in. */
14846 TYPE_NFIELDS (ftype) = nparams;
14847 TYPE_FIELDS (ftype) = (struct field *)
14848 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
14849
14850 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
14851 even if we error out during the parameters reading below. */
14852 for (iparams = 0; iparams < nparams; iparams++)
14853 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
14854
14855 iparams = 0;
14856 child_die = die->child;
14857 while (child_die && child_die->tag)
14858 {
14859 if (child_die->tag == DW_TAG_formal_parameter)
14860 {
14861 struct type *arg_type;
14862
14863 /* DWARF version 2 has no clean way to discern C++
14864 static and non-static member functions. G++ helps
14865 GDB by marking the first parameter for non-static
14866 member functions (which is the this pointer) as
14867 artificial. We pass this information to
14868 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
14869
14870 DWARF version 3 added DW_AT_object_pointer, which GCC
14871 4.5 does not yet generate. */
14872 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
14873 if (attr)
14874 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
14875 else
14876 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
14877 arg_type = die_type (child_die, cu);
14878
14879 /* RealView does not mark THIS as const, which the testsuite
14880 expects. GCC marks THIS as const in method definitions,
14881 but not in the class specifications (GCC PR 43053). */
14882 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
14883 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
14884 {
14885 int is_this = 0;
14886 struct dwarf2_cu *arg_cu = cu;
14887 const char *name = dwarf2_name (child_die, cu);
14888
14889 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
14890 if (attr)
14891 {
14892 /* If the compiler emits this, use it. */
14893 if (follow_die_ref (die, attr, &arg_cu) == child_die)
14894 is_this = 1;
14895 }
14896 else if (name && strcmp (name, "this") == 0)
14897 /* Function definitions will have the argument names. */
14898 is_this = 1;
14899 else if (name == NULL && iparams == 0)
14900 /* Declarations may not have the names, so like
14901 elsewhere in GDB, assume an artificial first
14902 argument is "this". */
14903 is_this = 1;
14904
14905 if (is_this)
14906 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
14907 arg_type, 0);
14908 }
14909
14910 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
14911 iparams++;
14912 }
14913 child_die = sibling_die (child_die);
14914 }
14915 }
14916
14917 return ftype;
14918 }
14919
14920 static struct type *
14921 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
14922 {
14923 struct objfile *objfile = cu->objfile;
14924 const char *name = NULL;
14925 struct type *this_type, *target_type;
14926
14927 name = dwarf2_full_name (NULL, die, cu);
14928 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
14929 TYPE_TARGET_STUB (this_type) = 1;
14930 set_die_type (die, this_type, cu);
14931 target_type = die_type (die, cu);
14932 if (target_type != this_type)
14933 TYPE_TARGET_TYPE (this_type) = target_type;
14934 else
14935 {
14936 /* Self-referential typedefs are, it seems, not allowed by the DWARF
14937 spec and cause infinite loops in GDB. */
14938 complaint (&symfile_complaints,
14939 _("Self-referential DW_TAG_typedef "
14940 "- DIE at 0x%x [in module %s]"),
14941 die->offset.sect_off, objfile_name (objfile));
14942 TYPE_TARGET_TYPE (this_type) = NULL;
14943 }
14944 return this_type;
14945 }
14946
14947 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
14948 (which may be different from NAME) to the architecture back-end to allow
14949 it to guess the correct format if necessary. */
14950
14951 static struct type *
14952 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
14953 const char *name_hint)
14954 {
14955 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14956 const struct floatformat **format;
14957 struct type *type;
14958
14959 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
14960 if (format)
14961 type = init_float_type (objfile, bits, name, format);
14962 else
14963 type = init_type (objfile, TYPE_CODE_ERROR, bits / TARGET_CHAR_BIT, name);
14964
14965 return type;
14966 }
14967
14968 /* Find a representation of a given base type and install
14969 it in the TYPE field of the die. */
14970
14971 static struct type *
14972 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
14973 {
14974 struct objfile *objfile = cu->objfile;
14975 struct type *type;
14976 struct attribute *attr;
14977 int encoding = 0, bits = 0;
14978 const char *name;
14979
14980 attr = dwarf2_attr (die, DW_AT_encoding, cu);
14981 if (attr)
14982 {
14983 encoding = DW_UNSND (attr);
14984 }
14985 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14986 if (attr)
14987 {
14988 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
14989 }
14990 name = dwarf2_name (die, cu);
14991 if (!name)
14992 {
14993 complaint (&symfile_complaints,
14994 _("DW_AT_name missing from DW_TAG_base_type"));
14995 }
14996
14997 switch (encoding)
14998 {
14999 case DW_ATE_address:
15000 /* Turn DW_ATE_address into a void * pointer. */
15001 type = init_type (objfile, TYPE_CODE_VOID, 1, NULL);
15002 type = init_pointer_type (objfile, bits, name, type);
15003 break;
15004 case DW_ATE_boolean:
15005 type = init_boolean_type (objfile, bits, 1, name);
15006 break;
15007 case DW_ATE_complex_float:
15008 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
15009 type = init_complex_type (objfile, name, type);
15010 break;
15011 case DW_ATE_decimal_float:
15012 type = init_decfloat_type (objfile, bits, name);
15013 break;
15014 case DW_ATE_float:
15015 type = dwarf2_init_float_type (objfile, bits, name, name);
15016 break;
15017 case DW_ATE_signed:
15018 type = init_integer_type (objfile, bits, 0, name);
15019 break;
15020 case DW_ATE_unsigned:
15021 if (cu->language == language_fortran
15022 && name
15023 && startswith (name, "character("))
15024 type = init_character_type (objfile, bits, 1, name);
15025 else
15026 type = init_integer_type (objfile, bits, 1, name);
15027 break;
15028 case DW_ATE_signed_char:
15029 if (cu->language == language_ada || cu->language == language_m2
15030 || cu->language == language_pascal
15031 || cu->language == language_fortran)
15032 type = init_character_type (objfile, bits, 0, name);
15033 else
15034 type = init_integer_type (objfile, bits, 0, name);
15035 break;
15036 case DW_ATE_unsigned_char:
15037 if (cu->language == language_ada || cu->language == language_m2
15038 || cu->language == language_pascal
15039 || cu->language == language_fortran
15040 || cu->language == language_rust)
15041 type = init_character_type (objfile, bits, 1, name);
15042 else
15043 type = init_integer_type (objfile, bits, 1, name);
15044 break;
15045 case DW_ATE_UTF:
15046 /* We just treat this as an integer and then recognize the
15047 type by name elsewhere. */
15048 type = init_integer_type (objfile, bits, 0, name);
15049 break;
15050
15051 default:
15052 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
15053 dwarf_type_encoding_name (encoding));
15054 type = init_type (objfile, TYPE_CODE_ERROR,
15055 bits / TARGET_CHAR_BIT, name);
15056 break;
15057 }
15058
15059 if (name && strcmp (name, "char") == 0)
15060 TYPE_NOSIGN (type) = 1;
15061
15062 return set_die_type (die, type, cu);
15063 }
15064
15065 /* Parse dwarf attribute if it's a block, reference or constant and put the
15066 resulting value of the attribute into struct bound_prop.
15067 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
15068
15069 static int
15070 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
15071 struct dwarf2_cu *cu, struct dynamic_prop *prop)
15072 {
15073 struct dwarf2_property_baton *baton;
15074 struct obstack *obstack = &cu->objfile->objfile_obstack;
15075
15076 if (attr == NULL || prop == NULL)
15077 return 0;
15078
15079 if (attr_form_is_block (attr))
15080 {
15081 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15082 baton->referenced_type = NULL;
15083 baton->locexpr.per_cu = cu->per_cu;
15084 baton->locexpr.size = DW_BLOCK (attr)->size;
15085 baton->locexpr.data = DW_BLOCK (attr)->data;
15086 prop->data.baton = baton;
15087 prop->kind = PROP_LOCEXPR;
15088 gdb_assert (prop->data.baton != NULL);
15089 }
15090 else if (attr_form_is_ref (attr))
15091 {
15092 struct dwarf2_cu *target_cu = cu;
15093 struct die_info *target_die;
15094 struct attribute *target_attr;
15095
15096 target_die = follow_die_ref (die, attr, &target_cu);
15097 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
15098 if (target_attr == NULL)
15099 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
15100 target_cu);
15101 if (target_attr == NULL)
15102 return 0;
15103
15104 switch (target_attr->name)
15105 {
15106 case DW_AT_location:
15107 if (attr_form_is_section_offset (target_attr))
15108 {
15109 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15110 baton->referenced_type = die_type (target_die, target_cu);
15111 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
15112 prop->data.baton = baton;
15113 prop->kind = PROP_LOCLIST;
15114 gdb_assert (prop->data.baton != NULL);
15115 }
15116 else if (attr_form_is_block (target_attr))
15117 {
15118 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15119 baton->referenced_type = die_type (target_die, target_cu);
15120 baton->locexpr.per_cu = cu->per_cu;
15121 baton->locexpr.size = DW_BLOCK (target_attr)->size;
15122 baton->locexpr.data = DW_BLOCK (target_attr)->data;
15123 prop->data.baton = baton;
15124 prop->kind = PROP_LOCEXPR;
15125 gdb_assert (prop->data.baton != NULL);
15126 }
15127 else
15128 {
15129 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15130 "dynamic property");
15131 return 0;
15132 }
15133 break;
15134 case DW_AT_data_member_location:
15135 {
15136 LONGEST offset;
15137
15138 if (!handle_data_member_location (target_die, target_cu,
15139 &offset))
15140 return 0;
15141
15142 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15143 baton->referenced_type = read_type_die (target_die->parent,
15144 target_cu);
15145 baton->offset_info.offset = offset;
15146 baton->offset_info.type = die_type (target_die, target_cu);
15147 prop->data.baton = baton;
15148 prop->kind = PROP_ADDR_OFFSET;
15149 break;
15150 }
15151 }
15152 }
15153 else if (attr_form_is_constant (attr))
15154 {
15155 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
15156 prop->kind = PROP_CONST;
15157 }
15158 else
15159 {
15160 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
15161 dwarf2_name (die, cu));
15162 return 0;
15163 }
15164
15165 return 1;
15166 }
15167
15168 /* Read the given DW_AT_subrange DIE. */
15169
15170 static struct type *
15171 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
15172 {
15173 struct type *base_type, *orig_base_type;
15174 struct type *range_type;
15175 struct attribute *attr;
15176 struct dynamic_prop low, high;
15177 int low_default_is_valid;
15178 int high_bound_is_count = 0;
15179 const char *name;
15180 LONGEST negative_mask;
15181
15182 orig_base_type = die_type (die, cu);
15183 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
15184 whereas the real type might be. So, we use ORIG_BASE_TYPE when
15185 creating the range type, but we use the result of check_typedef
15186 when examining properties of the type. */
15187 base_type = check_typedef (orig_base_type);
15188
15189 /* The die_type call above may have already set the type for this DIE. */
15190 range_type = get_die_type (die, cu);
15191 if (range_type)
15192 return range_type;
15193
15194 low.kind = PROP_CONST;
15195 high.kind = PROP_CONST;
15196 high.data.const_val = 0;
15197
15198 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
15199 omitting DW_AT_lower_bound. */
15200 switch (cu->language)
15201 {
15202 case language_c:
15203 case language_cplus:
15204 low.data.const_val = 0;
15205 low_default_is_valid = 1;
15206 break;
15207 case language_fortran:
15208 low.data.const_val = 1;
15209 low_default_is_valid = 1;
15210 break;
15211 case language_d:
15212 case language_objc:
15213 case language_rust:
15214 low.data.const_val = 0;
15215 low_default_is_valid = (cu->header.version >= 4);
15216 break;
15217 case language_ada:
15218 case language_m2:
15219 case language_pascal:
15220 low.data.const_val = 1;
15221 low_default_is_valid = (cu->header.version >= 4);
15222 break;
15223 default:
15224 low.data.const_val = 0;
15225 low_default_is_valid = 0;
15226 break;
15227 }
15228
15229 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
15230 if (attr)
15231 attr_to_dynamic_prop (attr, die, cu, &low);
15232 else if (!low_default_is_valid)
15233 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
15234 "- DIE at 0x%x [in module %s]"),
15235 die->offset.sect_off, objfile_name (cu->objfile));
15236
15237 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
15238 if (!attr_to_dynamic_prop (attr, die, cu, &high))
15239 {
15240 attr = dwarf2_attr (die, DW_AT_count, cu);
15241 if (attr_to_dynamic_prop (attr, die, cu, &high))
15242 {
15243 /* If bounds are constant do the final calculation here. */
15244 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
15245 high.data.const_val = low.data.const_val + high.data.const_val - 1;
15246 else
15247 high_bound_is_count = 1;
15248 }
15249 }
15250
15251 /* Dwarf-2 specifications explicitly allows to create subrange types
15252 without specifying a base type.
15253 In that case, the base type must be set to the type of
15254 the lower bound, upper bound or count, in that order, if any of these
15255 three attributes references an object that has a type.
15256 If no base type is found, the Dwarf-2 specifications say that
15257 a signed integer type of size equal to the size of an address should
15258 be used.
15259 For the following C code: `extern char gdb_int [];'
15260 GCC produces an empty range DIE.
15261 FIXME: muller/2010-05-28: Possible references to object for low bound,
15262 high bound or count are not yet handled by this code. */
15263 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
15264 {
15265 struct objfile *objfile = cu->objfile;
15266 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15267 int addr_size = gdbarch_addr_bit (gdbarch) /8;
15268 struct type *int_type = objfile_type (objfile)->builtin_int;
15269
15270 /* Test "int", "long int", and "long long int" objfile types,
15271 and select the first one having a size above or equal to the
15272 architecture address size. */
15273 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15274 base_type = int_type;
15275 else
15276 {
15277 int_type = objfile_type (objfile)->builtin_long;
15278 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15279 base_type = int_type;
15280 else
15281 {
15282 int_type = objfile_type (objfile)->builtin_long_long;
15283 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15284 base_type = int_type;
15285 }
15286 }
15287 }
15288
15289 /* Normally, the DWARF producers are expected to use a signed
15290 constant form (Eg. DW_FORM_sdata) to express negative bounds.
15291 But this is unfortunately not always the case, as witnessed
15292 with GCC, for instance, where the ambiguous DW_FORM_dataN form
15293 is used instead. To work around that ambiguity, we treat
15294 the bounds as signed, and thus sign-extend their values, when
15295 the base type is signed. */
15296 negative_mask =
15297 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
15298 if (low.kind == PROP_CONST
15299 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
15300 low.data.const_val |= negative_mask;
15301 if (high.kind == PROP_CONST
15302 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
15303 high.data.const_val |= negative_mask;
15304
15305 range_type = create_range_type (NULL, orig_base_type, &low, &high);
15306
15307 if (high_bound_is_count)
15308 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
15309
15310 /* Ada expects an empty array on no boundary attributes. */
15311 if (attr == NULL && cu->language != language_ada)
15312 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
15313
15314 name = dwarf2_name (die, cu);
15315 if (name)
15316 TYPE_NAME (range_type) = name;
15317
15318 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15319 if (attr)
15320 TYPE_LENGTH (range_type) = DW_UNSND (attr);
15321
15322 set_die_type (die, range_type, cu);
15323
15324 /* set_die_type should be already done. */
15325 set_descriptive_type (range_type, die, cu);
15326
15327 return range_type;
15328 }
15329
15330 static struct type *
15331 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
15332 {
15333 struct type *type;
15334
15335 /* For now, we only support the C meaning of an unspecified type: void. */
15336
15337 type = init_type (cu->objfile, TYPE_CODE_VOID, 0, NULL);
15338 TYPE_NAME (type) = dwarf2_name (die, cu);
15339
15340 return set_die_type (die, type, cu);
15341 }
15342
15343 /* Read a single die and all its descendents. Set the die's sibling
15344 field to NULL; set other fields in the die correctly, and set all
15345 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
15346 location of the info_ptr after reading all of those dies. PARENT
15347 is the parent of the die in question. */
15348
15349 static struct die_info *
15350 read_die_and_children (const struct die_reader_specs *reader,
15351 const gdb_byte *info_ptr,
15352 const gdb_byte **new_info_ptr,
15353 struct die_info *parent)
15354 {
15355 struct die_info *die;
15356 const gdb_byte *cur_ptr;
15357 int has_children;
15358
15359 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
15360 if (die == NULL)
15361 {
15362 *new_info_ptr = cur_ptr;
15363 return NULL;
15364 }
15365 store_in_ref_table (die, reader->cu);
15366
15367 if (has_children)
15368 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
15369 else
15370 {
15371 die->child = NULL;
15372 *new_info_ptr = cur_ptr;
15373 }
15374
15375 die->sibling = NULL;
15376 die->parent = parent;
15377 return die;
15378 }
15379
15380 /* Read a die, all of its descendents, and all of its siblings; set
15381 all of the fields of all of the dies correctly. Arguments are as
15382 in read_die_and_children. */
15383
15384 static struct die_info *
15385 read_die_and_siblings_1 (const struct die_reader_specs *reader,
15386 const gdb_byte *info_ptr,
15387 const gdb_byte **new_info_ptr,
15388 struct die_info *parent)
15389 {
15390 struct die_info *first_die, *last_sibling;
15391 const gdb_byte *cur_ptr;
15392
15393 cur_ptr = info_ptr;
15394 first_die = last_sibling = NULL;
15395
15396 while (1)
15397 {
15398 struct die_info *die
15399 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
15400
15401 if (die == NULL)
15402 {
15403 *new_info_ptr = cur_ptr;
15404 return first_die;
15405 }
15406
15407 if (!first_die)
15408 first_die = die;
15409 else
15410 last_sibling->sibling = die;
15411
15412 last_sibling = die;
15413 }
15414 }
15415
15416 /* Read a die, all of its descendents, and all of its siblings; set
15417 all of the fields of all of the dies correctly. Arguments are as
15418 in read_die_and_children.
15419 This the main entry point for reading a DIE and all its children. */
15420
15421 static struct die_info *
15422 read_die_and_siblings (const struct die_reader_specs *reader,
15423 const gdb_byte *info_ptr,
15424 const gdb_byte **new_info_ptr,
15425 struct die_info *parent)
15426 {
15427 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
15428 new_info_ptr, parent);
15429
15430 if (dwarf_die_debug)
15431 {
15432 fprintf_unfiltered (gdb_stdlog,
15433 "Read die from %s@0x%x of %s:\n",
15434 get_section_name (reader->die_section),
15435 (unsigned) (info_ptr - reader->die_section->buffer),
15436 bfd_get_filename (reader->abfd));
15437 dump_die (die, dwarf_die_debug);
15438 }
15439
15440 return die;
15441 }
15442
15443 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
15444 attributes.
15445 The caller is responsible for filling in the extra attributes
15446 and updating (*DIEP)->num_attrs.
15447 Set DIEP to point to a newly allocated die with its information,
15448 except for its child, sibling, and parent fields.
15449 Set HAS_CHILDREN to tell whether the die has children or not. */
15450
15451 static const gdb_byte *
15452 read_full_die_1 (const struct die_reader_specs *reader,
15453 struct die_info **diep, const gdb_byte *info_ptr,
15454 int *has_children, int num_extra_attrs)
15455 {
15456 unsigned int abbrev_number, bytes_read, i;
15457 sect_offset offset;
15458 struct abbrev_info *abbrev;
15459 struct die_info *die;
15460 struct dwarf2_cu *cu = reader->cu;
15461 bfd *abfd = reader->abfd;
15462
15463 offset.sect_off = info_ptr - reader->buffer;
15464 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
15465 info_ptr += bytes_read;
15466 if (!abbrev_number)
15467 {
15468 *diep = NULL;
15469 *has_children = 0;
15470 return info_ptr;
15471 }
15472
15473 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
15474 if (!abbrev)
15475 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
15476 abbrev_number,
15477 bfd_get_filename (abfd));
15478
15479 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
15480 die->offset = offset;
15481 die->tag = abbrev->tag;
15482 die->abbrev = abbrev_number;
15483
15484 /* Make the result usable.
15485 The caller needs to update num_attrs after adding the extra
15486 attributes. */
15487 die->num_attrs = abbrev->num_attrs;
15488
15489 for (i = 0; i < abbrev->num_attrs; ++i)
15490 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
15491 info_ptr);
15492
15493 *diep = die;
15494 *has_children = abbrev->has_children;
15495 return info_ptr;
15496 }
15497
15498 /* Read a die and all its attributes.
15499 Set DIEP to point to a newly allocated die with its information,
15500 except for its child, sibling, and parent fields.
15501 Set HAS_CHILDREN to tell whether the die has children or not. */
15502
15503 static const gdb_byte *
15504 read_full_die (const struct die_reader_specs *reader,
15505 struct die_info **diep, const gdb_byte *info_ptr,
15506 int *has_children)
15507 {
15508 const gdb_byte *result;
15509
15510 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
15511
15512 if (dwarf_die_debug)
15513 {
15514 fprintf_unfiltered (gdb_stdlog,
15515 "Read die from %s@0x%x of %s:\n",
15516 get_section_name (reader->die_section),
15517 (unsigned) (info_ptr - reader->die_section->buffer),
15518 bfd_get_filename (reader->abfd));
15519 dump_die (*diep, dwarf_die_debug);
15520 }
15521
15522 return result;
15523 }
15524 \f
15525 /* Abbreviation tables.
15526
15527 In DWARF version 2, the description of the debugging information is
15528 stored in a separate .debug_abbrev section. Before we read any
15529 dies from a section we read in all abbreviations and install them
15530 in a hash table. */
15531
15532 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
15533
15534 static struct abbrev_info *
15535 abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
15536 {
15537 struct abbrev_info *abbrev;
15538
15539 abbrev = XOBNEW (&abbrev_table->abbrev_obstack, struct abbrev_info);
15540 memset (abbrev, 0, sizeof (struct abbrev_info));
15541
15542 return abbrev;
15543 }
15544
15545 /* Add an abbreviation to the table. */
15546
15547 static void
15548 abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
15549 unsigned int abbrev_number,
15550 struct abbrev_info *abbrev)
15551 {
15552 unsigned int hash_number;
15553
15554 hash_number = abbrev_number % ABBREV_HASH_SIZE;
15555 abbrev->next = abbrev_table->abbrevs[hash_number];
15556 abbrev_table->abbrevs[hash_number] = abbrev;
15557 }
15558
15559 /* Look up an abbrev in the table.
15560 Returns NULL if the abbrev is not found. */
15561
15562 static struct abbrev_info *
15563 abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
15564 unsigned int abbrev_number)
15565 {
15566 unsigned int hash_number;
15567 struct abbrev_info *abbrev;
15568
15569 hash_number = abbrev_number % ABBREV_HASH_SIZE;
15570 abbrev = abbrev_table->abbrevs[hash_number];
15571
15572 while (abbrev)
15573 {
15574 if (abbrev->number == abbrev_number)
15575 return abbrev;
15576 abbrev = abbrev->next;
15577 }
15578 return NULL;
15579 }
15580
15581 /* Read in an abbrev table. */
15582
15583 static struct abbrev_table *
15584 abbrev_table_read_table (struct dwarf2_section_info *section,
15585 sect_offset offset)
15586 {
15587 struct objfile *objfile = dwarf2_per_objfile->objfile;
15588 bfd *abfd = get_section_bfd_owner (section);
15589 struct abbrev_table *abbrev_table;
15590 const gdb_byte *abbrev_ptr;
15591 struct abbrev_info *cur_abbrev;
15592 unsigned int abbrev_number, bytes_read, abbrev_name;
15593 unsigned int abbrev_form;
15594 struct attr_abbrev *cur_attrs;
15595 unsigned int allocated_attrs;
15596
15597 abbrev_table = XNEW (struct abbrev_table);
15598 abbrev_table->offset = offset;
15599 obstack_init (&abbrev_table->abbrev_obstack);
15600 abbrev_table->abbrevs =
15601 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct abbrev_info *,
15602 ABBREV_HASH_SIZE);
15603 memset (abbrev_table->abbrevs, 0,
15604 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
15605
15606 dwarf2_read_section (objfile, section);
15607 abbrev_ptr = section->buffer + offset.sect_off;
15608 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15609 abbrev_ptr += bytes_read;
15610
15611 allocated_attrs = ATTR_ALLOC_CHUNK;
15612 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
15613
15614 /* Loop until we reach an abbrev number of 0. */
15615 while (abbrev_number)
15616 {
15617 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
15618
15619 /* read in abbrev header */
15620 cur_abbrev->number = abbrev_number;
15621 cur_abbrev->tag
15622 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15623 abbrev_ptr += bytes_read;
15624 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
15625 abbrev_ptr += 1;
15626
15627 /* now read in declarations */
15628 for (;;)
15629 {
15630 LONGEST implicit_const;
15631
15632 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15633 abbrev_ptr += bytes_read;
15634 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15635 abbrev_ptr += bytes_read;
15636 if (abbrev_form == DW_FORM_implicit_const)
15637 {
15638 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
15639 &bytes_read);
15640 abbrev_ptr += bytes_read;
15641 }
15642 else
15643 {
15644 /* Initialize it due to a false compiler warning. */
15645 implicit_const = -1;
15646 }
15647
15648 if (abbrev_name == 0)
15649 break;
15650
15651 if (cur_abbrev->num_attrs == allocated_attrs)
15652 {
15653 allocated_attrs += ATTR_ALLOC_CHUNK;
15654 cur_attrs
15655 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
15656 }
15657
15658 cur_attrs[cur_abbrev->num_attrs].name
15659 = (enum dwarf_attribute) abbrev_name;
15660 cur_attrs[cur_abbrev->num_attrs].form
15661 = (enum dwarf_form) abbrev_form;
15662 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
15663 ++cur_abbrev->num_attrs;
15664 }
15665
15666 cur_abbrev->attrs =
15667 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
15668 cur_abbrev->num_attrs);
15669 memcpy (cur_abbrev->attrs, cur_attrs,
15670 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
15671
15672 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
15673
15674 /* Get next abbreviation.
15675 Under Irix6 the abbreviations for a compilation unit are not
15676 always properly terminated with an abbrev number of 0.
15677 Exit loop if we encounter an abbreviation which we have
15678 already read (which means we are about to read the abbreviations
15679 for the next compile unit) or if the end of the abbreviation
15680 table is reached. */
15681 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
15682 break;
15683 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15684 abbrev_ptr += bytes_read;
15685 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
15686 break;
15687 }
15688
15689 xfree (cur_attrs);
15690 return abbrev_table;
15691 }
15692
15693 /* Free the resources held by ABBREV_TABLE. */
15694
15695 static void
15696 abbrev_table_free (struct abbrev_table *abbrev_table)
15697 {
15698 obstack_free (&abbrev_table->abbrev_obstack, NULL);
15699 xfree (abbrev_table);
15700 }
15701
15702 /* Same as abbrev_table_free but as a cleanup.
15703 We pass in a pointer to the pointer to the table so that we can
15704 set the pointer to NULL when we're done. It also simplifies
15705 build_type_psymtabs_1. */
15706
15707 static void
15708 abbrev_table_free_cleanup (void *table_ptr)
15709 {
15710 struct abbrev_table **abbrev_table_ptr = (struct abbrev_table **) table_ptr;
15711
15712 if (*abbrev_table_ptr != NULL)
15713 abbrev_table_free (*abbrev_table_ptr);
15714 *abbrev_table_ptr = NULL;
15715 }
15716
15717 /* Read the abbrev table for CU from ABBREV_SECTION. */
15718
15719 static void
15720 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
15721 struct dwarf2_section_info *abbrev_section)
15722 {
15723 cu->abbrev_table =
15724 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
15725 }
15726
15727 /* Release the memory used by the abbrev table for a compilation unit. */
15728
15729 static void
15730 dwarf2_free_abbrev_table (void *ptr_to_cu)
15731 {
15732 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr_to_cu;
15733
15734 if (cu->abbrev_table != NULL)
15735 abbrev_table_free (cu->abbrev_table);
15736 /* Set this to NULL so that we SEGV if we try to read it later,
15737 and also because free_comp_unit verifies this is NULL. */
15738 cu->abbrev_table = NULL;
15739 }
15740 \f
15741 /* Returns nonzero if TAG represents a type that we might generate a partial
15742 symbol for. */
15743
15744 static int
15745 is_type_tag_for_partial (int tag)
15746 {
15747 switch (tag)
15748 {
15749 #if 0
15750 /* Some types that would be reasonable to generate partial symbols for,
15751 that we don't at present. */
15752 case DW_TAG_array_type:
15753 case DW_TAG_file_type:
15754 case DW_TAG_ptr_to_member_type:
15755 case DW_TAG_set_type:
15756 case DW_TAG_string_type:
15757 case DW_TAG_subroutine_type:
15758 #endif
15759 case DW_TAG_base_type:
15760 case DW_TAG_class_type:
15761 case DW_TAG_interface_type:
15762 case DW_TAG_enumeration_type:
15763 case DW_TAG_structure_type:
15764 case DW_TAG_subrange_type:
15765 case DW_TAG_typedef:
15766 case DW_TAG_union_type:
15767 return 1;
15768 default:
15769 return 0;
15770 }
15771 }
15772
15773 /* Load all DIEs that are interesting for partial symbols into memory. */
15774
15775 static struct partial_die_info *
15776 load_partial_dies (const struct die_reader_specs *reader,
15777 const gdb_byte *info_ptr, int building_psymtab)
15778 {
15779 struct dwarf2_cu *cu = reader->cu;
15780 struct objfile *objfile = cu->objfile;
15781 struct partial_die_info *part_die;
15782 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
15783 struct abbrev_info *abbrev;
15784 unsigned int bytes_read;
15785 unsigned int load_all = 0;
15786 int nesting_level = 1;
15787
15788 parent_die = NULL;
15789 last_die = NULL;
15790
15791 gdb_assert (cu->per_cu != NULL);
15792 if (cu->per_cu->load_all_dies)
15793 load_all = 1;
15794
15795 cu->partial_dies
15796 = htab_create_alloc_ex (cu->header.length / 12,
15797 partial_die_hash,
15798 partial_die_eq,
15799 NULL,
15800 &cu->comp_unit_obstack,
15801 hashtab_obstack_allocate,
15802 dummy_obstack_deallocate);
15803
15804 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
15805
15806 while (1)
15807 {
15808 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
15809
15810 /* A NULL abbrev means the end of a series of children. */
15811 if (abbrev == NULL)
15812 {
15813 if (--nesting_level == 0)
15814 {
15815 /* PART_DIE was probably the last thing allocated on the
15816 comp_unit_obstack, so we could call obstack_free
15817 here. We don't do that because the waste is small,
15818 and will be cleaned up when we're done with this
15819 compilation unit. This way, we're also more robust
15820 against other users of the comp_unit_obstack. */
15821 return first_die;
15822 }
15823 info_ptr += bytes_read;
15824 last_die = parent_die;
15825 parent_die = parent_die->die_parent;
15826 continue;
15827 }
15828
15829 /* Check for template arguments. We never save these; if
15830 they're seen, we just mark the parent, and go on our way. */
15831 if (parent_die != NULL
15832 && cu->language == language_cplus
15833 && (abbrev->tag == DW_TAG_template_type_param
15834 || abbrev->tag == DW_TAG_template_value_param))
15835 {
15836 parent_die->has_template_arguments = 1;
15837
15838 if (!load_all)
15839 {
15840 /* We don't need a partial DIE for the template argument. */
15841 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
15842 continue;
15843 }
15844 }
15845
15846 /* We only recurse into c++ subprograms looking for template arguments.
15847 Skip their other children. */
15848 if (!load_all
15849 && cu->language == language_cplus
15850 && parent_die != NULL
15851 && parent_die->tag == DW_TAG_subprogram)
15852 {
15853 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
15854 continue;
15855 }
15856
15857 /* Check whether this DIE is interesting enough to save. Normally
15858 we would not be interested in members here, but there may be
15859 later variables referencing them via DW_AT_specification (for
15860 static members). */
15861 if (!load_all
15862 && !is_type_tag_for_partial (abbrev->tag)
15863 && abbrev->tag != DW_TAG_constant
15864 && abbrev->tag != DW_TAG_enumerator
15865 && abbrev->tag != DW_TAG_subprogram
15866 && abbrev->tag != DW_TAG_lexical_block
15867 && abbrev->tag != DW_TAG_variable
15868 && abbrev->tag != DW_TAG_namespace
15869 && abbrev->tag != DW_TAG_module
15870 && abbrev->tag != DW_TAG_member
15871 && abbrev->tag != DW_TAG_imported_unit
15872 && abbrev->tag != DW_TAG_imported_declaration)
15873 {
15874 /* Otherwise we skip to the next sibling, if any. */
15875 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
15876 continue;
15877 }
15878
15879 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
15880 info_ptr);
15881
15882 /* This two-pass algorithm for processing partial symbols has a
15883 high cost in cache pressure. Thus, handle some simple cases
15884 here which cover the majority of C partial symbols. DIEs
15885 which neither have specification tags in them, nor could have
15886 specification tags elsewhere pointing at them, can simply be
15887 processed and discarded.
15888
15889 This segment is also optional; scan_partial_symbols and
15890 add_partial_symbol will handle these DIEs if we chain
15891 them in normally. When compilers which do not emit large
15892 quantities of duplicate debug information are more common,
15893 this code can probably be removed. */
15894
15895 /* Any complete simple types at the top level (pretty much all
15896 of them, for a language without namespaces), can be processed
15897 directly. */
15898 if (parent_die == NULL
15899 && part_die->has_specification == 0
15900 && part_die->is_declaration == 0
15901 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
15902 || part_die->tag == DW_TAG_base_type
15903 || part_die->tag == DW_TAG_subrange_type))
15904 {
15905 if (building_psymtab && part_die->name != NULL)
15906 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
15907 VAR_DOMAIN, LOC_TYPEDEF,
15908 &objfile->static_psymbols,
15909 0, cu->language, objfile);
15910 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
15911 continue;
15912 }
15913
15914 /* The exception for DW_TAG_typedef with has_children above is
15915 a workaround of GCC PR debug/47510. In the case of this complaint
15916 type_name_no_tag_or_error will error on such types later.
15917
15918 GDB skipped children of DW_TAG_typedef by the shortcut above and then
15919 it could not find the child DIEs referenced later, this is checked
15920 above. In correct DWARF DW_TAG_typedef should have no children. */
15921
15922 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
15923 complaint (&symfile_complaints,
15924 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
15925 "- DIE at 0x%x [in module %s]"),
15926 part_die->offset.sect_off, objfile_name (objfile));
15927
15928 /* If we're at the second level, and we're an enumerator, and
15929 our parent has no specification (meaning possibly lives in a
15930 namespace elsewhere), then we can add the partial symbol now
15931 instead of queueing it. */
15932 if (part_die->tag == DW_TAG_enumerator
15933 && parent_die != NULL
15934 && parent_die->die_parent == NULL
15935 && parent_die->tag == DW_TAG_enumeration_type
15936 && parent_die->has_specification == 0)
15937 {
15938 if (part_die->name == NULL)
15939 complaint (&symfile_complaints,
15940 _("malformed enumerator DIE ignored"));
15941 else if (building_psymtab)
15942 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
15943 VAR_DOMAIN, LOC_CONST,
15944 cu->language == language_cplus
15945 ? &objfile->global_psymbols
15946 : &objfile->static_psymbols,
15947 0, cu->language, objfile);
15948
15949 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
15950 continue;
15951 }
15952
15953 /* We'll save this DIE so link it in. */
15954 part_die->die_parent = parent_die;
15955 part_die->die_sibling = NULL;
15956 part_die->die_child = NULL;
15957
15958 if (last_die && last_die == parent_die)
15959 last_die->die_child = part_die;
15960 else if (last_die)
15961 last_die->die_sibling = part_die;
15962
15963 last_die = part_die;
15964
15965 if (first_die == NULL)
15966 first_die = part_die;
15967
15968 /* Maybe add the DIE to the hash table. Not all DIEs that we
15969 find interesting need to be in the hash table, because we
15970 also have the parent/sibling/child chains; only those that we
15971 might refer to by offset later during partial symbol reading.
15972
15973 For now this means things that might have be the target of a
15974 DW_AT_specification, DW_AT_abstract_origin, or
15975 DW_AT_extension. DW_AT_extension will refer only to
15976 namespaces; DW_AT_abstract_origin refers to functions (and
15977 many things under the function DIE, but we do not recurse
15978 into function DIEs during partial symbol reading) and
15979 possibly variables as well; DW_AT_specification refers to
15980 declarations. Declarations ought to have the DW_AT_declaration
15981 flag. It happens that GCC forgets to put it in sometimes, but
15982 only for functions, not for types.
15983
15984 Adding more things than necessary to the hash table is harmless
15985 except for the performance cost. Adding too few will result in
15986 wasted time in find_partial_die, when we reread the compilation
15987 unit with load_all_dies set. */
15988
15989 if (load_all
15990 || abbrev->tag == DW_TAG_constant
15991 || abbrev->tag == DW_TAG_subprogram
15992 || abbrev->tag == DW_TAG_variable
15993 || abbrev->tag == DW_TAG_namespace
15994 || part_die->is_declaration)
15995 {
15996 void **slot;
15997
15998 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
15999 part_die->offset.sect_off, INSERT);
16000 *slot = part_die;
16001 }
16002
16003 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
16004
16005 /* For some DIEs we want to follow their children (if any). For C
16006 we have no reason to follow the children of structures; for other
16007 languages we have to, so that we can get at method physnames
16008 to infer fully qualified class names, for DW_AT_specification,
16009 and for C++ template arguments. For C++, we also look one level
16010 inside functions to find template arguments (if the name of the
16011 function does not already contain the template arguments).
16012
16013 For Ada, we need to scan the children of subprograms and lexical
16014 blocks as well because Ada allows the definition of nested
16015 entities that could be interesting for the debugger, such as
16016 nested subprograms for instance. */
16017 if (last_die->has_children
16018 && (load_all
16019 || last_die->tag == DW_TAG_namespace
16020 || last_die->tag == DW_TAG_module
16021 || last_die->tag == DW_TAG_enumeration_type
16022 || (cu->language == language_cplus
16023 && last_die->tag == DW_TAG_subprogram
16024 && (last_die->name == NULL
16025 || strchr (last_die->name, '<') == NULL))
16026 || (cu->language != language_c
16027 && (last_die->tag == DW_TAG_class_type
16028 || last_die->tag == DW_TAG_interface_type
16029 || last_die->tag == DW_TAG_structure_type
16030 || last_die->tag == DW_TAG_union_type))
16031 || (cu->language == language_ada
16032 && (last_die->tag == DW_TAG_subprogram
16033 || last_die->tag == DW_TAG_lexical_block))))
16034 {
16035 nesting_level++;
16036 parent_die = last_die;
16037 continue;
16038 }
16039
16040 /* Otherwise we skip to the next sibling, if any. */
16041 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
16042
16043 /* Back to the top, do it again. */
16044 }
16045 }
16046
16047 /* Read a minimal amount of information into the minimal die structure. */
16048
16049 static const gdb_byte *
16050 read_partial_die (const struct die_reader_specs *reader,
16051 struct partial_die_info *part_die,
16052 struct abbrev_info *abbrev, unsigned int abbrev_len,
16053 const gdb_byte *info_ptr)
16054 {
16055 struct dwarf2_cu *cu = reader->cu;
16056 struct objfile *objfile = cu->objfile;
16057 const gdb_byte *buffer = reader->buffer;
16058 unsigned int i;
16059 struct attribute attr;
16060 int has_low_pc_attr = 0;
16061 int has_high_pc_attr = 0;
16062 int high_pc_relative = 0;
16063
16064 memset (part_die, 0, sizeof (struct partial_die_info));
16065
16066 part_die->offset.sect_off = info_ptr - buffer;
16067
16068 info_ptr += abbrev_len;
16069
16070 if (abbrev == NULL)
16071 return info_ptr;
16072
16073 part_die->tag = abbrev->tag;
16074 part_die->has_children = abbrev->has_children;
16075
16076 for (i = 0; i < abbrev->num_attrs; ++i)
16077 {
16078 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
16079
16080 /* Store the data if it is of an attribute we want to keep in a
16081 partial symbol table. */
16082 switch (attr.name)
16083 {
16084 case DW_AT_name:
16085 switch (part_die->tag)
16086 {
16087 case DW_TAG_compile_unit:
16088 case DW_TAG_partial_unit:
16089 case DW_TAG_type_unit:
16090 /* Compilation units have a DW_AT_name that is a filename, not
16091 a source language identifier. */
16092 case DW_TAG_enumeration_type:
16093 case DW_TAG_enumerator:
16094 /* These tags always have simple identifiers already; no need
16095 to canonicalize them. */
16096 part_die->name = DW_STRING (&attr);
16097 break;
16098 default:
16099 part_die->name
16100 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
16101 &objfile->per_bfd->storage_obstack);
16102 break;
16103 }
16104 break;
16105 case DW_AT_linkage_name:
16106 case DW_AT_MIPS_linkage_name:
16107 /* Note that both forms of linkage name might appear. We
16108 assume they will be the same, and we only store the last
16109 one we see. */
16110 if (cu->language == language_ada)
16111 part_die->name = DW_STRING (&attr);
16112 part_die->linkage_name = DW_STRING (&attr);
16113 break;
16114 case DW_AT_low_pc:
16115 has_low_pc_attr = 1;
16116 part_die->lowpc = attr_value_as_address (&attr);
16117 break;
16118 case DW_AT_high_pc:
16119 has_high_pc_attr = 1;
16120 part_die->highpc = attr_value_as_address (&attr);
16121 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
16122 high_pc_relative = 1;
16123 break;
16124 case DW_AT_location:
16125 /* Support the .debug_loc offsets. */
16126 if (attr_form_is_block (&attr))
16127 {
16128 part_die->d.locdesc = DW_BLOCK (&attr);
16129 }
16130 else if (attr_form_is_section_offset (&attr))
16131 {
16132 dwarf2_complex_location_expr_complaint ();
16133 }
16134 else
16135 {
16136 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16137 "partial symbol information");
16138 }
16139 break;
16140 case DW_AT_external:
16141 part_die->is_external = DW_UNSND (&attr);
16142 break;
16143 case DW_AT_declaration:
16144 part_die->is_declaration = DW_UNSND (&attr);
16145 break;
16146 case DW_AT_type:
16147 part_die->has_type = 1;
16148 break;
16149 case DW_AT_abstract_origin:
16150 case DW_AT_specification:
16151 case DW_AT_extension:
16152 part_die->has_specification = 1;
16153 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
16154 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
16155 || cu->per_cu->is_dwz);
16156 break;
16157 case DW_AT_sibling:
16158 /* Ignore absolute siblings, they might point outside of
16159 the current compile unit. */
16160 if (attr.form == DW_FORM_ref_addr)
16161 complaint (&symfile_complaints,
16162 _("ignoring absolute DW_AT_sibling"));
16163 else
16164 {
16165 unsigned int off = dwarf2_get_ref_die_offset (&attr).sect_off;
16166 const gdb_byte *sibling_ptr = buffer + off;
16167
16168 if (sibling_ptr < info_ptr)
16169 complaint (&symfile_complaints,
16170 _("DW_AT_sibling points backwards"));
16171 else if (sibling_ptr > reader->buffer_end)
16172 dwarf2_section_buffer_overflow_complaint (reader->die_section);
16173 else
16174 part_die->sibling = sibling_ptr;
16175 }
16176 break;
16177 case DW_AT_byte_size:
16178 part_die->has_byte_size = 1;
16179 break;
16180 case DW_AT_const_value:
16181 part_die->has_const_value = 1;
16182 break;
16183 case DW_AT_calling_convention:
16184 /* DWARF doesn't provide a way to identify a program's source-level
16185 entry point. DW_AT_calling_convention attributes are only meant
16186 to describe functions' calling conventions.
16187
16188 However, because it's a necessary piece of information in
16189 Fortran, and before DWARF 4 DW_CC_program was the only
16190 piece of debugging information whose definition refers to
16191 a 'main program' at all, several compilers marked Fortran
16192 main programs with DW_CC_program --- even when those
16193 functions use the standard calling conventions.
16194
16195 Although DWARF now specifies a way to provide this
16196 information, we support this practice for backward
16197 compatibility. */
16198 if (DW_UNSND (&attr) == DW_CC_program
16199 && cu->language == language_fortran)
16200 part_die->main_subprogram = 1;
16201 break;
16202 case DW_AT_inline:
16203 if (DW_UNSND (&attr) == DW_INL_inlined
16204 || DW_UNSND (&attr) == DW_INL_declared_inlined)
16205 part_die->may_be_inlined = 1;
16206 break;
16207
16208 case DW_AT_import:
16209 if (part_die->tag == DW_TAG_imported_unit)
16210 {
16211 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
16212 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
16213 || cu->per_cu->is_dwz);
16214 }
16215 break;
16216
16217 case DW_AT_main_subprogram:
16218 part_die->main_subprogram = DW_UNSND (&attr);
16219 break;
16220
16221 default:
16222 break;
16223 }
16224 }
16225
16226 if (high_pc_relative)
16227 part_die->highpc += part_die->lowpc;
16228
16229 if (has_low_pc_attr && has_high_pc_attr)
16230 {
16231 /* When using the GNU linker, .gnu.linkonce. sections are used to
16232 eliminate duplicate copies of functions and vtables and such.
16233 The linker will arbitrarily choose one and discard the others.
16234 The AT_*_pc values for such functions refer to local labels in
16235 these sections. If the section from that file was discarded, the
16236 labels are not in the output, so the relocs get a value of 0.
16237 If this is a discarded function, mark the pc bounds as invalid,
16238 so that GDB will ignore it. */
16239 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
16240 {
16241 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16242
16243 complaint (&symfile_complaints,
16244 _("DW_AT_low_pc %s is zero "
16245 "for DIE at 0x%x [in module %s]"),
16246 paddress (gdbarch, part_die->lowpc),
16247 part_die->offset.sect_off, objfile_name (objfile));
16248 }
16249 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
16250 else if (part_die->lowpc >= part_die->highpc)
16251 {
16252 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16253
16254 complaint (&symfile_complaints,
16255 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
16256 "for DIE at 0x%x [in module %s]"),
16257 paddress (gdbarch, part_die->lowpc),
16258 paddress (gdbarch, part_die->highpc),
16259 part_die->offset.sect_off, objfile_name (objfile));
16260 }
16261 else
16262 part_die->has_pc_info = 1;
16263 }
16264
16265 return info_ptr;
16266 }
16267
16268 /* Find a cached partial DIE at OFFSET in CU. */
16269
16270 static struct partial_die_info *
16271 find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
16272 {
16273 struct partial_die_info *lookup_die = NULL;
16274 struct partial_die_info part_die;
16275
16276 part_die.offset = offset;
16277 lookup_die = ((struct partial_die_info *)
16278 htab_find_with_hash (cu->partial_dies, &part_die,
16279 offset.sect_off));
16280
16281 return lookup_die;
16282 }
16283
16284 /* Find a partial DIE at OFFSET, which may or may not be in CU,
16285 except in the case of .debug_types DIEs which do not reference
16286 outside their CU (they do however referencing other types via
16287 DW_FORM_ref_sig8). */
16288
16289 static struct partial_die_info *
16290 find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
16291 {
16292 struct objfile *objfile = cu->objfile;
16293 struct dwarf2_per_cu_data *per_cu = NULL;
16294 struct partial_die_info *pd = NULL;
16295
16296 if (offset_in_dwz == cu->per_cu->is_dwz
16297 && offset_in_cu_p (&cu->header, offset))
16298 {
16299 pd = find_partial_die_in_comp_unit (offset, cu);
16300 if (pd != NULL)
16301 return pd;
16302 /* We missed recording what we needed.
16303 Load all dies and try again. */
16304 per_cu = cu->per_cu;
16305 }
16306 else
16307 {
16308 /* TUs don't reference other CUs/TUs (except via type signatures). */
16309 if (cu->per_cu->is_debug_types)
16310 {
16311 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
16312 " external reference to offset 0x%lx [in module %s].\n"),
16313 (long) cu->header.offset.sect_off, (long) offset.sect_off,
16314 bfd_get_filename (objfile->obfd));
16315 }
16316 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
16317 objfile);
16318
16319 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
16320 load_partial_comp_unit (per_cu);
16321
16322 per_cu->cu->last_used = 0;
16323 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
16324 }
16325
16326 /* If we didn't find it, and not all dies have been loaded,
16327 load them all and try again. */
16328
16329 if (pd == NULL && per_cu->load_all_dies == 0)
16330 {
16331 per_cu->load_all_dies = 1;
16332
16333 /* This is nasty. When we reread the DIEs, somewhere up the call chain
16334 THIS_CU->cu may already be in use. So we can't just free it and
16335 replace its DIEs with the ones we read in. Instead, we leave those
16336 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
16337 and clobber THIS_CU->cu->partial_dies with the hash table for the new
16338 set. */
16339 load_partial_comp_unit (per_cu);
16340
16341 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
16342 }
16343
16344 if (pd == NULL)
16345 internal_error (__FILE__, __LINE__,
16346 _("could not find partial DIE 0x%x "
16347 "in cache [from module %s]\n"),
16348 offset.sect_off, bfd_get_filename (objfile->obfd));
16349 return pd;
16350 }
16351
16352 /* See if we can figure out if the class lives in a namespace. We do
16353 this by looking for a member function; its demangled name will
16354 contain namespace info, if there is any. */
16355
16356 static void
16357 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
16358 struct dwarf2_cu *cu)
16359 {
16360 /* NOTE: carlton/2003-10-07: Getting the info this way changes
16361 what template types look like, because the demangler
16362 frequently doesn't give the same name as the debug info. We
16363 could fix this by only using the demangled name to get the
16364 prefix (but see comment in read_structure_type). */
16365
16366 struct partial_die_info *real_pdi;
16367 struct partial_die_info *child_pdi;
16368
16369 /* If this DIE (this DIE's specification, if any) has a parent, then
16370 we should not do this. We'll prepend the parent's fully qualified
16371 name when we create the partial symbol. */
16372
16373 real_pdi = struct_pdi;
16374 while (real_pdi->has_specification)
16375 real_pdi = find_partial_die (real_pdi->spec_offset,
16376 real_pdi->spec_is_dwz, cu);
16377
16378 if (real_pdi->die_parent != NULL)
16379 return;
16380
16381 for (child_pdi = struct_pdi->die_child;
16382 child_pdi != NULL;
16383 child_pdi = child_pdi->die_sibling)
16384 {
16385 if (child_pdi->tag == DW_TAG_subprogram
16386 && child_pdi->linkage_name != NULL)
16387 {
16388 char *actual_class_name
16389 = language_class_name_from_physname (cu->language_defn,
16390 child_pdi->linkage_name);
16391 if (actual_class_name != NULL)
16392 {
16393 struct_pdi->name
16394 = ((const char *)
16395 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
16396 actual_class_name,
16397 strlen (actual_class_name)));
16398 xfree (actual_class_name);
16399 }
16400 break;
16401 }
16402 }
16403 }
16404
16405 /* Adjust PART_DIE before generating a symbol for it. This function
16406 may set the is_external flag or change the DIE's name. */
16407
16408 static void
16409 fixup_partial_die (struct partial_die_info *part_die,
16410 struct dwarf2_cu *cu)
16411 {
16412 /* Once we've fixed up a die, there's no point in doing so again.
16413 This also avoids a memory leak if we were to call
16414 guess_partial_die_structure_name multiple times. */
16415 if (part_die->fixup_called)
16416 return;
16417
16418 /* If we found a reference attribute and the DIE has no name, try
16419 to find a name in the referred to DIE. */
16420
16421 if (part_die->name == NULL && part_die->has_specification)
16422 {
16423 struct partial_die_info *spec_die;
16424
16425 spec_die = find_partial_die (part_die->spec_offset,
16426 part_die->spec_is_dwz, cu);
16427
16428 fixup_partial_die (spec_die, cu);
16429
16430 if (spec_die->name)
16431 {
16432 part_die->name = spec_die->name;
16433
16434 /* Copy DW_AT_external attribute if it is set. */
16435 if (spec_die->is_external)
16436 part_die->is_external = spec_die->is_external;
16437 }
16438 }
16439
16440 /* Set default names for some unnamed DIEs. */
16441
16442 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
16443 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
16444
16445 /* If there is no parent die to provide a namespace, and there are
16446 children, see if we can determine the namespace from their linkage
16447 name. */
16448 if (cu->language == language_cplus
16449 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
16450 && part_die->die_parent == NULL
16451 && part_die->has_children
16452 && (part_die->tag == DW_TAG_class_type
16453 || part_die->tag == DW_TAG_structure_type
16454 || part_die->tag == DW_TAG_union_type))
16455 guess_partial_die_structure_name (part_die, cu);
16456
16457 /* GCC might emit a nameless struct or union that has a linkage
16458 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16459 if (part_die->name == NULL
16460 && (part_die->tag == DW_TAG_class_type
16461 || part_die->tag == DW_TAG_interface_type
16462 || part_die->tag == DW_TAG_structure_type
16463 || part_die->tag == DW_TAG_union_type)
16464 && part_die->linkage_name != NULL)
16465 {
16466 char *demangled;
16467
16468 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
16469 if (demangled)
16470 {
16471 const char *base;
16472
16473 /* Strip any leading namespaces/classes, keep only the base name.
16474 DW_AT_name for named DIEs does not contain the prefixes. */
16475 base = strrchr (demangled, ':');
16476 if (base && base > demangled && base[-1] == ':')
16477 base++;
16478 else
16479 base = demangled;
16480
16481 part_die->name
16482 = ((const char *)
16483 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
16484 base, strlen (base)));
16485 xfree (demangled);
16486 }
16487 }
16488
16489 part_die->fixup_called = 1;
16490 }
16491
16492 /* Read an attribute value described by an attribute form. */
16493
16494 static const gdb_byte *
16495 read_attribute_value (const struct die_reader_specs *reader,
16496 struct attribute *attr, unsigned form,
16497 LONGEST implicit_const, const gdb_byte *info_ptr)
16498 {
16499 struct dwarf2_cu *cu = reader->cu;
16500 struct objfile *objfile = cu->objfile;
16501 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16502 bfd *abfd = reader->abfd;
16503 struct comp_unit_head *cu_header = &cu->header;
16504 unsigned int bytes_read;
16505 struct dwarf_block *blk;
16506
16507 attr->form = (enum dwarf_form) form;
16508 switch (form)
16509 {
16510 case DW_FORM_ref_addr:
16511 if (cu->header.version == 2)
16512 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
16513 else
16514 DW_UNSND (attr) = read_offset (abfd, info_ptr,
16515 &cu->header, &bytes_read);
16516 info_ptr += bytes_read;
16517 break;
16518 case DW_FORM_GNU_ref_alt:
16519 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
16520 info_ptr += bytes_read;
16521 break;
16522 case DW_FORM_addr:
16523 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
16524 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
16525 info_ptr += bytes_read;
16526 break;
16527 case DW_FORM_block2:
16528 blk = dwarf_alloc_block (cu);
16529 blk->size = read_2_bytes (abfd, info_ptr);
16530 info_ptr += 2;
16531 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16532 info_ptr += blk->size;
16533 DW_BLOCK (attr) = blk;
16534 break;
16535 case DW_FORM_block4:
16536 blk = dwarf_alloc_block (cu);
16537 blk->size = read_4_bytes (abfd, info_ptr);
16538 info_ptr += 4;
16539 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16540 info_ptr += blk->size;
16541 DW_BLOCK (attr) = blk;
16542 break;
16543 case DW_FORM_data2:
16544 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
16545 info_ptr += 2;
16546 break;
16547 case DW_FORM_data4:
16548 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
16549 info_ptr += 4;
16550 break;
16551 case DW_FORM_data8:
16552 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
16553 info_ptr += 8;
16554 break;
16555 case DW_FORM_sec_offset:
16556 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
16557 info_ptr += bytes_read;
16558 break;
16559 case DW_FORM_string:
16560 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
16561 DW_STRING_IS_CANONICAL (attr) = 0;
16562 info_ptr += bytes_read;
16563 break;
16564 case DW_FORM_strp:
16565 if (!cu->per_cu->is_dwz)
16566 {
16567 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
16568 &bytes_read);
16569 DW_STRING_IS_CANONICAL (attr) = 0;
16570 info_ptr += bytes_read;
16571 break;
16572 }
16573 /* FALLTHROUGH */
16574 case DW_FORM_line_strp:
16575 if (!cu->per_cu->is_dwz)
16576 {
16577 DW_STRING (attr) = read_indirect_line_string (abfd, info_ptr,
16578 cu_header, &bytes_read);
16579 DW_STRING_IS_CANONICAL (attr) = 0;
16580 info_ptr += bytes_read;
16581 break;
16582 }
16583 /* FALLTHROUGH */
16584 case DW_FORM_GNU_strp_alt:
16585 {
16586 struct dwz_file *dwz = dwarf2_get_dwz_file ();
16587 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
16588 &bytes_read);
16589
16590 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
16591 DW_STRING_IS_CANONICAL (attr) = 0;
16592 info_ptr += bytes_read;
16593 }
16594 break;
16595 case DW_FORM_exprloc:
16596 case DW_FORM_block:
16597 blk = dwarf_alloc_block (cu);
16598 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16599 info_ptr += bytes_read;
16600 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16601 info_ptr += blk->size;
16602 DW_BLOCK (attr) = blk;
16603 break;
16604 case DW_FORM_block1:
16605 blk = dwarf_alloc_block (cu);
16606 blk->size = read_1_byte (abfd, info_ptr);
16607 info_ptr += 1;
16608 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16609 info_ptr += blk->size;
16610 DW_BLOCK (attr) = blk;
16611 break;
16612 case DW_FORM_data1:
16613 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
16614 info_ptr += 1;
16615 break;
16616 case DW_FORM_flag:
16617 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
16618 info_ptr += 1;
16619 break;
16620 case DW_FORM_flag_present:
16621 DW_UNSND (attr) = 1;
16622 break;
16623 case DW_FORM_sdata:
16624 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
16625 info_ptr += bytes_read;
16626 break;
16627 case DW_FORM_udata:
16628 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16629 info_ptr += bytes_read;
16630 break;
16631 case DW_FORM_ref1:
16632 DW_UNSND (attr) = (cu->header.offset.sect_off
16633 + read_1_byte (abfd, info_ptr));
16634 info_ptr += 1;
16635 break;
16636 case DW_FORM_ref2:
16637 DW_UNSND (attr) = (cu->header.offset.sect_off
16638 + read_2_bytes (abfd, info_ptr));
16639 info_ptr += 2;
16640 break;
16641 case DW_FORM_ref4:
16642 DW_UNSND (attr) = (cu->header.offset.sect_off
16643 + read_4_bytes (abfd, info_ptr));
16644 info_ptr += 4;
16645 break;
16646 case DW_FORM_ref8:
16647 DW_UNSND (attr) = (cu->header.offset.sect_off
16648 + read_8_bytes (abfd, info_ptr));
16649 info_ptr += 8;
16650 break;
16651 case DW_FORM_ref_sig8:
16652 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
16653 info_ptr += 8;
16654 break;
16655 case DW_FORM_ref_udata:
16656 DW_UNSND (attr) = (cu->header.offset.sect_off
16657 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
16658 info_ptr += bytes_read;
16659 break;
16660 case DW_FORM_indirect:
16661 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16662 info_ptr += bytes_read;
16663 if (form == DW_FORM_implicit_const)
16664 {
16665 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
16666 info_ptr += bytes_read;
16667 }
16668 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
16669 info_ptr);
16670 break;
16671 case DW_FORM_implicit_const:
16672 DW_SND (attr) = implicit_const;
16673 break;
16674 case DW_FORM_GNU_addr_index:
16675 if (reader->dwo_file == NULL)
16676 {
16677 /* For now flag a hard error.
16678 Later we can turn this into a complaint. */
16679 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
16680 dwarf_form_name (form),
16681 bfd_get_filename (abfd));
16682 }
16683 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
16684 info_ptr += bytes_read;
16685 break;
16686 case DW_FORM_GNU_str_index:
16687 if (reader->dwo_file == NULL)
16688 {
16689 /* For now flag a hard error.
16690 Later we can turn this into a complaint if warranted. */
16691 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
16692 dwarf_form_name (form),
16693 bfd_get_filename (abfd));
16694 }
16695 {
16696 ULONGEST str_index =
16697 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16698
16699 DW_STRING (attr) = read_str_index (reader, str_index);
16700 DW_STRING_IS_CANONICAL (attr) = 0;
16701 info_ptr += bytes_read;
16702 }
16703 break;
16704 default:
16705 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
16706 dwarf_form_name (form),
16707 bfd_get_filename (abfd));
16708 }
16709
16710 /* Super hack. */
16711 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
16712 attr->form = DW_FORM_GNU_ref_alt;
16713
16714 /* We have seen instances where the compiler tried to emit a byte
16715 size attribute of -1 which ended up being encoded as an unsigned
16716 0xffffffff. Although 0xffffffff is technically a valid size value,
16717 an object of this size seems pretty unlikely so we can relatively
16718 safely treat these cases as if the size attribute was invalid and
16719 treat them as zero by default. */
16720 if (attr->name == DW_AT_byte_size
16721 && form == DW_FORM_data4
16722 && DW_UNSND (attr) >= 0xffffffff)
16723 {
16724 complaint
16725 (&symfile_complaints,
16726 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
16727 hex_string (DW_UNSND (attr)));
16728 DW_UNSND (attr) = 0;
16729 }
16730
16731 return info_ptr;
16732 }
16733
16734 /* Read an attribute described by an abbreviated attribute. */
16735
16736 static const gdb_byte *
16737 read_attribute (const struct die_reader_specs *reader,
16738 struct attribute *attr, struct attr_abbrev *abbrev,
16739 const gdb_byte *info_ptr)
16740 {
16741 attr->name = abbrev->name;
16742 return read_attribute_value (reader, attr, abbrev->form,
16743 abbrev->implicit_const, info_ptr);
16744 }
16745
16746 /* Read dwarf information from a buffer. */
16747
16748 static unsigned int
16749 read_1_byte (bfd *abfd, const gdb_byte *buf)
16750 {
16751 return bfd_get_8 (abfd, buf);
16752 }
16753
16754 static int
16755 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
16756 {
16757 return bfd_get_signed_8 (abfd, buf);
16758 }
16759
16760 static unsigned int
16761 read_2_bytes (bfd *abfd, const gdb_byte *buf)
16762 {
16763 return bfd_get_16 (abfd, buf);
16764 }
16765
16766 static int
16767 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
16768 {
16769 return bfd_get_signed_16 (abfd, buf);
16770 }
16771
16772 static unsigned int
16773 read_4_bytes (bfd *abfd, const gdb_byte *buf)
16774 {
16775 return bfd_get_32 (abfd, buf);
16776 }
16777
16778 static int
16779 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
16780 {
16781 return bfd_get_signed_32 (abfd, buf);
16782 }
16783
16784 static ULONGEST
16785 read_8_bytes (bfd *abfd, const gdb_byte *buf)
16786 {
16787 return bfd_get_64 (abfd, buf);
16788 }
16789
16790 static CORE_ADDR
16791 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
16792 unsigned int *bytes_read)
16793 {
16794 struct comp_unit_head *cu_header = &cu->header;
16795 CORE_ADDR retval = 0;
16796
16797 if (cu_header->signed_addr_p)
16798 {
16799 switch (cu_header->addr_size)
16800 {
16801 case 2:
16802 retval = bfd_get_signed_16 (abfd, buf);
16803 break;
16804 case 4:
16805 retval = bfd_get_signed_32 (abfd, buf);
16806 break;
16807 case 8:
16808 retval = bfd_get_signed_64 (abfd, buf);
16809 break;
16810 default:
16811 internal_error (__FILE__, __LINE__,
16812 _("read_address: bad switch, signed [in module %s]"),
16813 bfd_get_filename (abfd));
16814 }
16815 }
16816 else
16817 {
16818 switch (cu_header->addr_size)
16819 {
16820 case 2:
16821 retval = bfd_get_16 (abfd, buf);
16822 break;
16823 case 4:
16824 retval = bfd_get_32 (abfd, buf);
16825 break;
16826 case 8:
16827 retval = bfd_get_64 (abfd, buf);
16828 break;
16829 default:
16830 internal_error (__FILE__, __LINE__,
16831 _("read_address: bad switch, "
16832 "unsigned [in module %s]"),
16833 bfd_get_filename (abfd));
16834 }
16835 }
16836
16837 *bytes_read = cu_header->addr_size;
16838 return retval;
16839 }
16840
16841 /* Read the initial length from a section. The (draft) DWARF 3
16842 specification allows the initial length to take up either 4 bytes
16843 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
16844 bytes describe the length and all offsets will be 8 bytes in length
16845 instead of 4.
16846
16847 An older, non-standard 64-bit format is also handled by this
16848 function. The older format in question stores the initial length
16849 as an 8-byte quantity without an escape value. Lengths greater
16850 than 2^32 aren't very common which means that the initial 4 bytes
16851 is almost always zero. Since a length value of zero doesn't make
16852 sense for the 32-bit format, this initial zero can be considered to
16853 be an escape value which indicates the presence of the older 64-bit
16854 format. As written, the code can't detect (old format) lengths
16855 greater than 4GB. If it becomes necessary to handle lengths
16856 somewhat larger than 4GB, we could allow other small values (such
16857 as the non-sensical values of 1, 2, and 3) to also be used as
16858 escape values indicating the presence of the old format.
16859
16860 The value returned via bytes_read should be used to increment the
16861 relevant pointer after calling read_initial_length().
16862
16863 [ Note: read_initial_length() and read_offset() are based on the
16864 document entitled "DWARF Debugging Information Format", revision
16865 3, draft 8, dated November 19, 2001. This document was obtained
16866 from:
16867
16868 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
16869
16870 This document is only a draft and is subject to change. (So beware.)
16871
16872 Details regarding the older, non-standard 64-bit format were
16873 determined empirically by examining 64-bit ELF files produced by
16874 the SGI toolchain on an IRIX 6.5 machine.
16875
16876 - Kevin, July 16, 2002
16877 ] */
16878
16879 static LONGEST
16880 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
16881 {
16882 LONGEST length = bfd_get_32 (abfd, buf);
16883
16884 if (length == 0xffffffff)
16885 {
16886 length = bfd_get_64 (abfd, buf + 4);
16887 *bytes_read = 12;
16888 }
16889 else if (length == 0)
16890 {
16891 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
16892 length = bfd_get_64 (abfd, buf);
16893 *bytes_read = 8;
16894 }
16895 else
16896 {
16897 *bytes_read = 4;
16898 }
16899
16900 return length;
16901 }
16902
16903 /* Cover function for read_initial_length.
16904 Returns the length of the object at BUF, and stores the size of the
16905 initial length in *BYTES_READ and stores the size that offsets will be in
16906 *OFFSET_SIZE.
16907 If the initial length size is not equivalent to that specified in
16908 CU_HEADER then issue a complaint.
16909 This is useful when reading non-comp-unit headers. */
16910
16911 static LONGEST
16912 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
16913 const struct comp_unit_head *cu_header,
16914 unsigned int *bytes_read,
16915 unsigned int *offset_size)
16916 {
16917 LONGEST length = read_initial_length (abfd, buf, bytes_read);
16918
16919 gdb_assert (cu_header->initial_length_size == 4
16920 || cu_header->initial_length_size == 8
16921 || cu_header->initial_length_size == 12);
16922
16923 if (cu_header->initial_length_size != *bytes_read)
16924 complaint (&symfile_complaints,
16925 _("intermixed 32-bit and 64-bit DWARF sections"));
16926
16927 *offset_size = (*bytes_read == 4) ? 4 : 8;
16928 return length;
16929 }
16930
16931 /* Read an offset from the data stream. The size of the offset is
16932 given by cu_header->offset_size. */
16933
16934 static LONGEST
16935 read_offset (bfd *abfd, const gdb_byte *buf,
16936 const struct comp_unit_head *cu_header,
16937 unsigned int *bytes_read)
16938 {
16939 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
16940
16941 *bytes_read = cu_header->offset_size;
16942 return offset;
16943 }
16944
16945 /* Read an offset from the data stream. */
16946
16947 static LONGEST
16948 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
16949 {
16950 LONGEST retval = 0;
16951
16952 switch (offset_size)
16953 {
16954 case 4:
16955 retval = bfd_get_32 (abfd, buf);
16956 break;
16957 case 8:
16958 retval = bfd_get_64 (abfd, buf);
16959 break;
16960 default:
16961 internal_error (__FILE__, __LINE__,
16962 _("read_offset_1: bad switch [in module %s]"),
16963 bfd_get_filename (abfd));
16964 }
16965
16966 return retval;
16967 }
16968
16969 static const gdb_byte *
16970 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
16971 {
16972 /* If the size of a host char is 8 bits, we can return a pointer
16973 to the buffer, otherwise we have to copy the data to a buffer
16974 allocated on the temporary obstack. */
16975 gdb_assert (HOST_CHAR_BIT == 8);
16976 return buf;
16977 }
16978
16979 static const char *
16980 read_direct_string (bfd *abfd, const gdb_byte *buf,
16981 unsigned int *bytes_read_ptr)
16982 {
16983 /* If the size of a host char is 8 bits, we can return a pointer
16984 to the string, otherwise we have to copy the string to a buffer
16985 allocated on the temporary obstack. */
16986 gdb_assert (HOST_CHAR_BIT == 8);
16987 if (*buf == '\0')
16988 {
16989 *bytes_read_ptr = 1;
16990 return NULL;
16991 }
16992 *bytes_read_ptr = strlen ((const char *) buf) + 1;
16993 return (const char *) buf;
16994 }
16995
16996 /* Return pointer to string at section SECT offset STR_OFFSET with error
16997 reporting strings FORM_NAME and SECT_NAME. */
16998
16999 static const char *
17000 read_indirect_string_at_offset_from (bfd *abfd, LONGEST str_offset,
17001 struct dwarf2_section_info *sect,
17002 const char *form_name,
17003 const char *sect_name)
17004 {
17005 dwarf2_read_section (dwarf2_per_objfile->objfile, sect);
17006 if (sect->buffer == NULL)
17007 error (_("%s used without %s section [in module %s]"),
17008 form_name, sect_name, bfd_get_filename (abfd));
17009 if (str_offset >= sect->size)
17010 error (_("%s pointing outside of %s section [in module %s]"),
17011 form_name, sect_name, bfd_get_filename (abfd));
17012 gdb_assert (HOST_CHAR_BIT == 8);
17013 if (sect->buffer[str_offset] == '\0')
17014 return NULL;
17015 return (const char *) (sect->buffer + str_offset);
17016 }
17017
17018 /* Return pointer to string at .debug_str offset STR_OFFSET. */
17019
17020 static const char *
17021 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
17022 {
17023 return read_indirect_string_at_offset_from (abfd, str_offset,
17024 &dwarf2_per_objfile->str,
17025 "DW_FORM_strp", ".debug_str");
17026 }
17027
17028 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
17029
17030 static const char *
17031 read_indirect_line_string_at_offset (bfd *abfd, LONGEST str_offset)
17032 {
17033 return read_indirect_string_at_offset_from (abfd, str_offset,
17034 &dwarf2_per_objfile->line_str,
17035 "DW_FORM_line_strp",
17036 ".debug_line_str");
17037 }
17038
17039 /* Read a string at offset STR_OFFSET in the .debug_str section from
17040 the .dwz file DWZ. Throw an error if the offset is too large. If
17041 the string consists of a single NUL byte, return NULL; otherwise
17042 return a pointer to the string. */
17043
17044 static const char *
17045 read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
17046 {
17047 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
17048
17049 if (dwz->str.buffer == NULL)
17050 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
17051 "section [in module %s]"),
17052 bfd_get_filename (dwz->dwz_bfd));
17053 if (str_offset >= dwz->str.size)
17054 error (_("DW_FORM_GNU_strp_alt pointing outside of "
17055 ".debug_str section [in module %s]"),
17056 bfd_get_filename (dwz->dwz_bfd));
17057 gdb_assert (HOST_CHAR_BIT == 8);
17058 if (dwz->str.buffer[str_offset] == '\0')
17059 return NULL;
17060 return (const char *) (dwz->str.buffer + str_offset);
17061 }
17062
17063 /* Return pointer to string at .debug_str offset as read from BUF.
17064 BUF is assumed to be in a compilation unit described by CU_HEADER.
17065 Return *BYTES_READ_PTR count of bytes read from BUF. */
17066
17067 static const char *
17068 read_indirect_string (bfd *abfd, const gdb_byte *buf,
17069 const struct comp_unit_head *cu_header,
17070 unsigned int *bytes_read_ptr)
17071 {
17072 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
17073
17074 return read_indirect_string_at_offset (abfd, str_offset);
17075 }
17076
17077 /* Return pointer to string at .debug_line_str offset as read from BUF.
17078 BUF is assumed to be in a compilation unit described by CU_HEADER.
17079 Return *BYTES_READ_PTR count of bytes read from BUF. */
17080
17081 static const char *
17082 read_indirect_line_string (bfd *abfd, const gdb_byte *buf,
17083 const struct comp_unit_head *cu_header,
17084 unsigned int *bytes_read_ptr)
17085 {
17086 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
17087
17088 return read_indirect_line_string_at_offset (abfd, str_offset);
17089 }
17090
17091 ULONGEST
17092 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
17093 unsigned int *bytes_read_ptr)
17094 {
17095 ULONGEST result;
17096 unsigned int num_read;
17097 int shift;
17098 unsigned char byte;
17099
17100 result = 0;
17101 shift = 0;
17102 num_read = 0;
17103 while (1)
17104 {
17105 byte = bfd_get_8 (abfd, buf);
17106 buf++;
17107 num_read++;
17108 result |= ((ULONGEST) (byte & 127) << shift);
17109 if ((byte & 128) == 0)
17110 {
17111 break;
17112 }
17113 shift += 7;
17114 }
17115 *bytes_read_ptr = num_read;
17116 return result;
17117 }
17118
17119 static LONGEST
17120 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
17121 unsigned int *bytes_read_ptr)
17122 {
17123 LONGEST result;
17124 int shift, num_read;
17125 unsigned char byte;
17126
17127 result = 0;
17128 shift = 0;
17129 num_read = 0;
17130 while (1)
17131 {
17132 byte = bfd_get_8 (abfd, buf);
17133 buf++;
17134 num_read++;
17135 result |= ((LONGEST) (byte & 127) << shift);
17136 shift += 7;
17137 if ((byte & 128) == 0)
17138 {
17139 break;
17140 }
17141 }
17142 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
17143 result |= -(((LONGEST) 1) << shift);
17144 *bytes_read_ptr = num_read;
17145 return result;
17146 }
17147
17148 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
17149 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
17150 ADDR_SIZE is the size of addresses from the CU header. */
17151
17152 static CORE_ADDR
17153 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
17154 {
17155 struct objfile *objfile = dwarf2_per_objfile->objfile;
17156 bfd *abfd = objfile->obfd;
17157 const gdb_byte *info_ptr;
17158
17159 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
17160 if (dwarf2_per_objfile->addr.buffer == NULL)
17161 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
17162 objfile_name (objfile));
17163 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
17164 error (_("DW_FORM_addr_index pointing outside of "
17165 ".debug_addr section [in module %s]"),
17166 objfile_name (objfile));
17167 info_ptr = (dwarf2_per_objfile->addr.buffer
17168 + addr_base + addr_index * addr_size);
17169 if (addr_size == 4)
17170 return bfd_get_32 (abfd, info_ptr);
17171 else
17172 return bfd_get_64 (abfd, info_ptr);
17173 }
17174
17175 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
17176
17177 static CORE_ADDR
17178 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
17179 {
17180 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
17181 }
17182
17183 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
17184
17185 static CORE_ADDR
17186 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
17187 unsigned int *bytes_read)
17188 {
17189 bfd *abfd = cu->objfile->obfd;
17190 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
17191
17192 return read_addr_index (cu, addr_index);
17193 }
17194
17195 /* Data structure to pass results from dwarf2_read_addr_index_reader
17196 back to dwarf2_read_addr_index. */
17197
17198 struct dwarf2_read_addr_index_data
17199 {
17200 ULONGEST addr_base;
17201 int addr_size;
17202 };
17203
17204 /* die_reader_func for dwarf2_read_addr_index. */
17205
17206 static void
17207 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
17208 const gdb_byte *info_ptr,
17209 struct die_info *comp_unit_die,
17210 int has_children,
17211 void *data)
17212 {
17213 struct dwarf2_cu *cu = reader->cu;
17214 struct dwarf2_read_addr_index_data *aidata =
17215 (struct dwarf2_read_addr_index_data *) data;
17216
17217 aidata->addr_base = cu->addr_base;
17218 aidata->addr_size = cu->header.addr_size;
17219 }
17220
17221 /* Given an index in .debug_addr, fetch the value.
17222 NOTE: This can be called during dwarf expression evaluation,
17223 long after the debug information has been read, and thus per_cu->cu
17224 may no longer exist. */
17225
17226 CORE_ADDR
17227 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
17228 unsigned int addr_index)
17229 {
17230 struct objfile *objfile = per_cu->objfile;
17231 struct dwarf2_cu *cu = per_cu->cu;
17232 ULONGEST addr_base;
17233 int addr_size;
17234
17235 /* This is intended to be called from outside this file. */
17236 dw2_setup (objfile);
17237
17238 /* We need addr_base and addr_size.
17239 If we don't have PER_CU->cu, we have to get it.
17240 Nasty, but the alternative is storing the needed info in PER_CU,
17241 which at this point doesn't seem justified: it's not clear how frequently
17242 it would get used and it would increase the size of every PER_CU.
17243 Entry points like dwarf2_per_cu_addr_size do a similar thing
17244 so we're not in uncharted territory here.
17245 Alas we need to be a bit more complicated as addr_base is contained
17246 in the DIE.
17247
17248 We don't need to read the entire CU(/TU).
17249 We just need the header and top level die.
17250
17251 IWBN to use the aging mechanism to let us lazily later discard the CU.
17252 For now we skip this optimization. */
17253
17254 if (cu != NULL)
17255 {
17256 addr_base = cu->addr_base;
17257 addr_size = cu->header.addr_size;
17258 }
17259 else
17260 {
17261 struct dwarf2_read_addr_index_data aidata;
17262
17263 /* Note: We can't use init_cutu_and_read_dies_simple here,
17264 we need addr_base. */
17265 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
17266 dwarf2_read_addr_index_reader, &aidata);
17267 addr_base = aidata.addr_base;
17268 addr_size = aidata.addr_size;
17269 }
17270
17271 return read_addr_index_1 (addr_index, addr_base, addr_size);
17272 }
17273
17274 /* Given a DW_FORM_GNU_str_index, fetch the string.
17275 This is only used by the Fission support. */
17276
17277 static const char *
17278 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
17279 {
17280 struct objfile *objfile = dwarf2_per_objfile->objfile;
17281 const char *objf_name = objfile_name (objfile);
17282 bfd *abfd = objfile->obfd;
17283 struct dwarf2_cu *cu = reader->cu;
17284 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
17285 struct dwarf2_section_info *str_offsets_section =
17286 &reader->dwo_file->sections.str_offsets;
17287 const gdb_byte *info_ptr;
17288 ULONGEST str_offset;
17289 static const char form_name[] = "DW_FORM_GNU_str_index";
17290
17291 dwarf2_read_section (objfile, str_section);
17292 dwarf2_read_section (objfile, str_offsets_section);
17293 if (str_section->buffer == NULL)
17294 error (_("%s used without .debug_str.dwo section"
17295 " in CU at offset 0x%lx [in module %s]"),
17296 form_name, (long) cu->header.offset.sect_off, objf_name);
17297 if (str_offsets_section->buffer == NULL)
17298 error (_("%s used without .debug_str_offsets.dwo section"
17299 " in CU at offset 0x%lx [in module %s]"),
17300 form_name, (long) cu->header.offset.sect_off, objf_name);
17301 if (str_index * cu->header.offset_size >= str_offsets_section->size)
17302 error (_("%s pointing outside of .debug_str_offsets.dwo"
17303 " section in CU at offset 0x%lx [in module %s]"),
17304 form_name, (long) cu->header.offset.sect_off, objf_name);
17305 info_ptr = (str_offsets_section->buffer
17306 + str_index * cu->header.offset_size);
17307 if (cu->header.offset_size == 4)
17308 str_offset = bfd_get_32 (abfd, info_ptr);
17309 else
17310 str_offset = bfd_get_64 (abfd, info_ptr);
17311 if (str_offset >= str_section->size)
17312 error (_("Offset from %s pointing outside of"
17313 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
17314 form_name, (long) cu->header.offset.sect_off, objf_name);
17315 return (const char *) (str_section->buffer + str_offset);
17316 }
17317
17318 /* Return the length of an LEB128 number in BUF. */
17319
17320 static int
17321 leb128_size (const gdb_byte *buf)
17322 {
17323 const gdb_byte *begin = buf;
17324 gdb_byte byte;
17325
17326 while (1)
17327 {
17328 byte = *buf++;
17329 if ((byte & 128) == 0)
17330 return buf - begin;
17331 }
17332 }
17333
17334 static void
17335 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
17336 {
17337 switch (lang)
17338 {
17339 case DW_LANG_C89:
17340 case DW_LANG_C99:
17341 case DW_LANG_C11:
17342 case DW_LANG_C:
17343 case DW_LANG_UPC:
17344 cu->language = language_c;
17345 break;
17346 case DW_LANG_Java:
17347 case DW_LANG_C_plus_plus:
17348 case DW_LANG_C_plus_plus_11:
17349 case DW_LANG_C_plus_plus_14:
17350 cu->language = language_cplus;
17351 break;
17352 case DW_LANG_D:
17353 cu->language = language_d;
17354 break;
17355 case DW_LANG_Fortran77:
17356 case DW_LANG_Fortran90:
17357 case DW_LANG_Fortran95:
17358 case DW_LANG_Fortran03:
17359 case DW_LANG_Fortran08:
17360 cu->language = language_fortran;
17361 break;
17362 case DW_LANG_Go:
17363 cu->language = language_go;
17364 break;
17365 case DW_LANG_Mips_Assembler:
17366 cu->language = language_asm;
17367 break;
17368 case DW_LANG_Ada83:
17369 case DW_LANG_Ada95:
17370 cu->language = language_ada;
17371 break;
17372 case DW_LANG_Modula2:
17373 cu->language = language_m2;
17374 break;
17375 case DW_LANG_Pascal83:
17376 cu->language = language_pascal;
17377 break;
17378 case DW_LANG_ObjC:
17379 cu->language = language_objc;
17380 break;
17381 case DW_LANG_Rust:
17382 case DW_LANG_Rust_old:
17383 cu->language = language_rust;
17384 break;
17385 case DW_LANG_Cobol74:
17386 case DW_LANG_Cobol85:
17387 default:
17388 cu->language = language_minimal;
17389 break;
17390 }
17391 cu->language_defn = language_def (cu->language);
17392 }
17393
17394 /* Return the named attribute or NULL if not there. */
17395
17396 static struct attribute *
17397 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
17398 {
17399 for (;;)
17400 {
17401 unsigned int i;
17402 struct attribute *spec = NULL;
17403
17404 for (i = 0; i < die->num_attrs; ++i)
17405 {
17406 if (die->attrs[i].name == name)
17407 return &die->attrs[i];
17408 if (die->attrs[i].name == DW_AT_specification
17409 || die->attrs[i].name == DW_AT_abstract_origin)
17410 spec = &die->attrs[i];
17411 }
17412
17413 if (!spec)
17414 break;
17415
17416 die = follow_die_ref (die, spec, &cu);
17417 }
17418
17419 return NULL;
17420 }
17421
17422 /* Return the named attribute or NULL if not there,
17423 but do not follow DW_AT_specification, etc.
17424 This is for use in contexts where we're reading .debug_types dies.
17425 Following DW_AT_specification, DW_AT_abstract_origin will take us
17426 back up the chain, and we want to go down. */
17427
17428 static struct attribute *
17429 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
17430 {
17431 unsigned int i;
17432
17433 for (i = 0; i < die->num_attrs; ++i)
17434 if (die->attrs[i].name == name)
17435 return &die->attrs[i];
17436
17437 return NULL;
17438 }
17439
17440 /* Return the string associated with a string-typed attribute, or NULL if it
17441 is either not found or is of an incorrect type. */
17442
17443 static const char *
17444 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
17445 {
17446 struct attribute *attr;
17447 const char *str = NULL;
17448
17449 attr = dwarf2_attr (die, name, cu);
17450
17451 if (attr != NULL)
17452 {
17453 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
17454 || attr->form == DW_FORM_string || attr->form == DW_FORM_GNU_strp_alt)
17455 str = DW_STRING (attr);
17456 else
17457 complaint (&symfile_complaints,
17458 _("string type expected for attribute %s for "
17459 "DIE at 0x%x in module %s"),
17460 dwarf_attr_name (name), die->offset.sect_off,
17461 objfile_name (cu->objfile));
17462 }
17463
17464 return str;
17465 }
17466
17467 /* Return non-zero iff the attribute NAME is defined for the given DIE,
17468 and holds a non-zero value. This function should only be used for
17469 DW_FORM_flag or DW_FORM_flag_present attributes. */
17470
17471 static int
17472 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
17473 {
17474 struct attribute *attr = dwarf2_attr (die, name, cu);
17475
17476 return (attr && DW_UNSND (attr));
17477 }
17478
17479 static int
17480 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
17481 {
17482 /* A DIE is a declaration if it has a DW_AT_declaration attribute
17483 which value is non-zero. However, we have to be careful with
17484 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
17485 (via dwarf2_flag_true_p) follows this attribute. So we may
17486 end up accidently finding a declaration attribute that belongs
17487 to a different DIE referenced by the specification attribute,
17488 even though the given DIE does not have a declaration attribute. */
17489 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
17490 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
17491 }
17492
17493 /* Return the die giving the specification for DIE, if there is
17494 one. *SPEC_CU is the CU containing DIE on input, and the CU
17495 containing the return value on output. If there is no
17496 specification, but there is an abstract origin, that is
17497 returned. */
17498
17499 static struct die_info *
17500 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
17501 {
17502 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
17503 *spec_cu);
17504
17505 if (spec_attr == NULL)
17506 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
17507
17508 if (spec_attr == NULL)
17509 return NULL;
17510 else
17511 return follow_die_ref (die, spec_attr, spec_cu);
17512 }
17513
17514 /* Free the line_header structure *LH, and any arrays and strings it
17515 refers to.
17516 NOTE: This is also used as a "cleanup" function. */
17517
17518 static void
17519 free_line_header (struct line_header *lh)
17520 {
17521 if (lh->standard_opcode_lengths)
17522 xfree (lh->standard_opcode_lengths);
17523
17524 /* Remember that all the lh->file_names[i].name pointers are
17525 pointers into debug_line_buffer, and don't need to be freed. */
17526 if (lh->file_names)
17527 xfree (lh->file_names);
17528
17529 /* Similarly for the include directory names. */
17530 if (lh->include_dirs)
17531 xfree (lh->include_dirs);
17532
17533 xfree (lh);
17534 }
17535
17536 /* Stub for free_line_header to match void * callback types. */
17537
17538 static void
17539 free_line_header_voidp (void *arg)
17540 {
17541 struct line_header *lh = (struct line_header *) arg;
17542
17543 free_line_header (lh);
17544 }
17545
17546 /* Add an entry to LH's include directory table. */
17547
17548 static void
17549 add_include_dir (struct line_header *lh, const char *include_dir)
17550 {
17551 if (dwarf_line_debug >= 2)
17552 fprintf_unfiltered (gdb_stdlog, "Adding dir %u: %s\n",
17553 lh->num_include_dirs + 1, include_dir);
17554
17555 /* Grow the array if necessary. */
17556 if (lh->include_dirs_size == 0)
17557 {
17558 lh->include_dirs_size = 1; /* for testing */
17559 lh->include_dirs = XNEWVEC (const char *, lh->include_dirs_size);
17560 }
17561 else if (lh->num_include_dirs >= lh->include_dirs_size)
17562 {
17563 lh->include_dirs_size *= 2;
17564 lh->include_dirs = XRESIZEVEC (const char *, lh->include_dirs,
17565 lh->include_dirs_size);
17566 }
17567
17568 lh->include_dirs[lh->num_include_dirs++] = include_dir;
17569 }
17570
17571 /* Add an entry to LH's file name table. */
17572
17573 static void
17574 add_file_name (struct line_header *lh,
17575 const char *name,
17576 unsigned int dir_index,
17577 unsigned int mod_time,
17578 unsigned int length)
17579 {
17580 struct file_entry *fe;
17581
17582 if (dwarf_line_debug >= 2)
17583 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
17584 lh->num_file_names + 1, name);
17585
17586 /* Grow the array if necessary. */
17587 if (lh->file_names_size == 0)
17588 {
17589 lh->file_names_size = 1; /* for testing */
17590 lh->file_names = XNEWVEC (struct file_entry, lh->file_names_size);
17591 }
17592 else if (lh->num_file_names >= lh->file_names_size)
17593 {
17594 lh->file_names_size *= 2;
17595 lh->file_names
17596 = XRESIZEVEC (struct file_entry, lh->file_names, lh->file_names_size);
17597 }
17598
17599 fe = &lh->file_names[lh->num_file_names++];
17600 fe->name = name;
17601 fe->dir_index = dir_index;
17602 fe->mod_time = mod_time;
17603 fe->length = length;
17604 fe->included_p = 0;
17605 fe->symtab = NULL;
17606 }
17607
17608 /* A convenience function to find the proper .debug_line section for a CU. */
17609
17610 static struct dwarf2_section_info *
17611 get_debug_line_section (struct dwarf2_cu *cu)
17612 {
17613 struct dwarf2_section_info *section;
17614
17615 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
17616 DWO file. */
17617 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17618 section = &cu->dwo_unit->dwo_file->sections.line;
17619 else if (cu->per_cu->is_dwz)
17620 {
17621 struct dwz_file *dwz = dwarf2_get_dwz_file ();
17622
17623 section = &dwz->line;
17624 }
17625 else
17626 section = &dwarf2_per_objfile->line;
17627
17628 return section;
17629 }
17630
17631 /* Forwarding function for read_formatted_entries. */
17632
17633 static void
17634 add_include_dir_stub (struct line_header *lh, const char *name,
17635 unsigned int dir_index, unsigned int mod_time,
17636 unsigned int length)
17637 {
17638 add_include_dir (lh, name);
17639 }
17640
17641 /* Read directory or file name entry format, starting with byte of
17642 format count entries, ULEB128 pairs of entry formats, ULEB128 of
17643 entries count and the entries themselves in the described entry
17644 format. */
17645
17646 static void
17647 read_formatted_entries (bfd *abfd, const gdb_byte **bufp,
17648 struct line_header *lh,
17649 const struct comp_unit_head *cu_header,
17650 void (*callback) (struct line_header *lh,
17651 const char *name,
17652 unsigned int dir_index,
17653 unsigned int mod_time,
17654 unsigned int length))
17655 {
17656 gdb_byte format_count, formati;
17657 ULONGEST data_count, datai;
17658 const gdb_byte *buf = *bufp;
17659 const gdb_byte *format_header_data;
17660 int i;
17661 unsigned int bytes_read;
17662
17663 format_count = read_1_byte (abfd, buf);
17664 buf += 1;
17665 format_header_data = buf;
17666 for (formati = 0; formati < format_count; formati++)
17667 {
17668 read_unsigned_leb128 (abfd, buf, &bytes_read);
17669 buf += bytes_read;
17670 read_unsigned_leb128 (abfd, buf, &bytes_read);
17671 buf += bytes_read;
17672 }
17673
17674 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
17675 buf += bytes_read;
17676 for (datai = 0; datai < data_count; datai++)
17677 {
17678 const gdb_byte *format = format_header_data;
17679 struct file_entry fe;
17680
17681 memset (&fe, 0, sizeof (fe));
17682
17683 for (formati = 0; formati < format_count; formati++)
17684 {
17685 ULONGEST content_type, form;
17686 const char *string_trash;
17687 const char **stringp = &string_trash;
17688 unsigned int uint_trash, *uintp = &uint_trash;
17689
17690 content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
17691 format += bytes_read;
17692 switch (content_type)
17693 {
17694 case DW_LNCT_path:
17695 stringp = &fe.name;
17696 break;
17697 case DW_LNCT_directory_index:
17698 uintp = &fe.dir_index;
17699 break;
17700 case DW_LNCT_timestamp:
17701 uintp = &fe.mod_time;
17702 break;
17703 case DW_LNCT_size:
17704 uintp = &fe.length;
17705 break;
17706 case DW_LNCT_MD5:
17707 break;
17708 default:
17709 complaint (&symfile_complaints,
17710 _("Unknown format content type %s"),
17711 pulongest (content_type));
17712 }
17713
17714 form = read_unsigned_leb128 (abfd, format, &bytes_read);
17715 format += bytes_read;
17716 switch (form)
17717 {
17718 case DW_FORM_string:
17719 *stringp = read_direct_string (abfd, buf, &bytes_read);
17720 buf += bytes_read;
17721 break;
17722
17723 case DW_FORM_line_strp:
17724 *stringp = read_indirect_line_string (abfd, buf, cu_header, &bytes_read);
17725 buf += bytes_read;
17726 break;
17727
17728 case DW_FORM_data1:
17729 *uintp = read_1_byte (abfd, buf);
17730 buf += 1;
17731 break;
17732
17733 case DW_FORM_data2:
17734 *uintp = read_2_bytes (abfd, buf);
17735 buf += 2;
17736 break;
17737
17738 case DW_FORM_data4:
17739 *uintp = read_4_bytes (abfd, buf);
17740 buf += 4;
17741 break;
17742
17743 case DW_FORM_data8:
17744 *uintp = read_8_bytes (abfd, buf);
17745 buf += 8;
17746 break;
17747
17748 case DW_FORM_udata:
17749 *uintp = read_unsigned_leb128 (abfd, buf, &bytes_read);
17750 buf += bytes_read;
17751 break;
17752
17753 case DW_FORM_block:
17754 /* It is valid only for DW_LNCT_timestamp which is ignored by
17755 current GDB. */
17756 break;
17757 }
17758 }
17759
17760 callback (lh, fe.name, fe.dir_index, fe.mod_time, fe.length);
17761 }
17762
17763 *bufp = buf;
17764 }
17765
17766 /* Read the statement program header starting at OFFSET in
17767 .debug_line, or .debug_line.dwo. Return a pointer
17768 to a struct line_header, allocated using xmalloc.
17769 Returns NULL if there is a problem reading the header, e.g., if it
17770 has a version we don't understand.
17771
17772 NOTE: the strings in the include directory and file name tables of
17773 the returned object point into the dwarf line section buffer,
17774 and must not be freed. */
17775
17776 static struct line_header *
17777 dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
17778 {
17779 struct cleanup *back_to;
17780 struct line_header *lh;
17781 const gdb_byte *line_ptr;
17782 unsigned int bytes_read, offset_size;
17783 int i;
17784 const char *cur_dir, *cur_file;
17785 struct dwarf2_section_info *section;
17786 bfd *abfd;
17787
17788 section = get_debug_line_section (cu);
17789 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
17790 if (section->buffer == NULL)
17791 {
17792 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17793 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
17794 else
17795 complaint (&symfile_complaints, _("missing .debug_line section"));
17796 return 0;
17797 }
17798
17799 /* We can't do this until we know the section is non-empty.
17800 Only then do we know we have such a section. */
17801 abfd = get_section_bfd_owner (section);
17802
17803 /* Make sure that at least there's room for the total_length field.
17804 That could be 12 bytes long, but we're just going to fudge that. */
17805 if (offset + 4 >= section->size)
17806 {
17807 dwarf2_statement_list_fits_in_line_number_section_complaint ();
17808 return 0;
17809 }
17810
17811 lh = XNEW (struct line_header);
17812 memset (lh, 0, sizeof (*lh));
17813 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
17814 (void *) lh);
17815
17816 lh->offset.sect_off = offset;
17817 lh->offset_in_dwz = cu->per_cu->is_dwz;
17818
17819 line_ptr = section->buffer + offset;
17820
17821 /* Read in the header. */
17822 lh->total_length =
17823 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
17824 &bytes_read, &offset_size);
17825 line_ptr += bytes_read;
17826 if (line_ptr + lh->total_length > (section->buffer + section->size))
17827 {
17828 dwarf2_statement_list_fits_in_line_number_section_complaint ();
17829 do_cleanups (back_to);
17830 return 0;
17831 }
17832 lh->statement_program_end = line_ptr + lh->total_length;
17833 lh->version = read_2_bytes (abfd, line_ptr);
17834 line_ptr += 2;
17835 if (lh->version > 5)
17836 {
17837 /* This is a version we don't understand. The format could have
17838 changed in ways we don't handle properly so just punt. */
17839 complaint (&symfile_complaints,
17840 _("unsupported version in .debug_line section"));
17841 return NULL;
17842 }
17843 if (lh->version >= 5)
17844 {
17845 gdb_byte segment_selector_size;
17846
17847 /* Skip address size. */
17848 read_1_byte (abfd, line_ptr);
17849 line_ptr += 1;
17850
17851 segment_selector_size = read_1_byte (abfd, line_ptr);
17852 line_ptr += 1;
17853 if (segment_selector_size != 0)
17854 {
17855 complaint (&symfile_complaints,
17856 _("unsupported segment selector size %u "
17857 "in .debug_line section"),
17858 segment_selector_size);
17859 return NULL;
17860 }
17861 }
17862 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
17863 line_ptr += offset_size;
17864 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
17865 line_ptr += 1;
17866 if (lh->version >= 4)
17867 {
17868 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
17869 line_ptr += 1;
17870 }
17871 else
17872 lh->maximum_ops_per_instruction = 1;
17873
17874 if (lh->maximum_ops_per_instruction == 0)
17875 {
17876 lh->maximum_ops_per_instruction = 1;
17877 complaint (&symfile_complaints,
17878 _("invalid maximum_ops_per_instruction "
17879 "in `.debug_line' section"));
17880 }
17881
17882 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
17883 line_ptr += 1;
17884 lh->line_base = read_1_signed_byte (abfd, line_ptr);
17885 line_ptr += 1;
17886 lh->line_range = read_1_byte (abfd, line_ptr);
17887 line_ptr += 1;
17888 lh->opcode_base = read_1_byte (abfd, line_ptr);
17889 line_ptr += 1;
17890 lh->standard_opcode_lengths = XNEWVEC (unsigned char, lh->opcode_base);
17891
17892 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
17893 for (i = 1; i < lh->opcode_base; ++i)
17894 {
17895 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
17896 line_ptr += 1;
17897 }
17898
17899 if (lh->version >= 5)
17900 {
17901 /* Read directory table. */
17902 read_formatted_entries (abfd, &line_ptr, lh, &cu->header,
17903 add_include_dir_stub);
17904
17905 /* Read file name table. */
17906 read_formatted_entries (abfd, &line_ptr, lh, &cu->header, add_file_name);
17907 }
17908 else
17909 {
17910 /* Read directory table. */
17911 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
17912 {
17913 line_ptr += bytes_read;
17914 add_include_dir (lh, cur_dir);
17915 }
17916 line_ptr += bytes_read;
17917
17918 /* Read file name table. */
17919 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
17920 {
17921 unsigned int dir_index, mod_time, length;
17922
17923 line_ptr += bytes_read;
17924 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17925 line_ptr += bytes_read;
17926 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17927 line_ptr += bytes_read;
17928 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17929 line_ptr += bytes_read;
17930
17931 add_file_name (lh, cur_file, dir_index, mod_time, length);
17932 }
17933 line_ptr += bytes_read;
17934 }
17935 lh->statement_program_start = line_ptr;
17936
17937 if (line_ptr > (section->buffer + section->size))
17938 complaint (&symfile_complaints,
17939 _("line number info header doesn't "
17940 "fit in `.debug_line' section"));
17941
17942 discard_cleanups (back_to);
17943 return lh;
17944 }
17945
17946 /* Subroutine of dwarf_decode_lines to simplify it.
17947 Return the file name of the psymtab for included file FILE_INDEX
17948 in line header LH of PST.
17949 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
17950 If space for the result is malloc'd, it will be freed by a cleanup.
17951 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
17952
17953 The function creates dangling cleanup registration. */
17954
17955 static const char *
17956 psymtab_include_file_name (const struct line_header *lh, int file_index,
17957 const struct partial_symtab *pst,
17958 const char *comp_dir)
17959 {
17960 const struct file_entry fe = lh->file_names [file_index];
17961 const char *include_name = fe.name;
17962 const char *include_name_to_compare = include_name;
17963 const char *dir_name = NULL;
17964 const char *pst_filename;
17965 char *copied_name = NULL;
17966 int file_is_pst;
17967
17968 if (fe.dir_index && lh->include_dirs != NULL)
17969 dir_name = lh->include_dirs[fe.dir_index - 1];
17970
17971 if (!IS_ABSOLUTE_PATH (include_name)
17972 && (dir_name != NULL || comp_dir != NULL))
17973 {
17974 /* Avoid creating a duplicate psymtab for PST.
17975 We do this by comparing INCLUDE_NAME and PST_FILENAME.
17976 Before we do the comparison, however, we need to account
17977 for DIR_NAME and COMP_DIR.
17978 First prepend dir_name (if non-NULL). If we still don't
17979 have an absolute path prepend comp_dir (if non-NULL).
17980 However, the directory we record in the include-file's
17981 psymtab does not contain COMP_DIR (to match the
17982 corresponding symtab(s)).
17983
17984 Example:
17985
17986 bash$ cd /tmp
17987 bash$ gcc -g ./hello.c
17988 include_name = "hello.c"
17989 dir_name = "."
17990 DW_AT_comp_dir = comp_dir = "/tmp"
17991 DW_AT_name = "./hello.c"
17992
17993 */
17994
17995 if (dir_name != NULL)
17996 {
17997 char *tem = concat (dir_name, SLASH_STRING,
17998 include_name, (char *)NULL);
17999
18000 make_cleanup (xfree, tem);
18001 include_name = tem;
18002 include_name_to_compare = include_name;
18003 }
18004 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
18005 {
18006 char *tem = concat (comp_dir, SLASH_STRING,
18007 include_name, (char *)NULL);
18008
18009 make_cleanup (xfree, tem);
18010 include_name_to_compare = tem;
18011 }
18012 }
18013
18014 pst_filename = pst->filename;
18015 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
18016 {
18017 copied_name = concat (pst->dirname, SLASH_STRING,
18018 pst_filename, (char *)NULL);
18019 pst_filename = copied_name;
18020 }
18021
18022 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
18023
18024 if (copied_name != NULL)
18025 xfree (copied_name);
18026
18027 if (file_is_pst)
18028 return NULL;
18029 return include_name;
18030 }
18031
18032 /* State machine to track the state of the line number program. */
18033
18034 typedef struct
18035 {
18036 /* These are part of the standard DWARF line number state machine. */
18037
18038 unsigned char op_index;
18039 unsigned int file;
18040 unsigned int line;
18041 CORE_ADDR address;
18042 int is_stmt;
18043 unsigned int discriminator;
18044
18045 /* Additional bits of state we need to track. */
18046
18047 /* The last file that we called dwarf2_start_subfile for.
18048 This is only used for TLLs. */
18049 unsigned int last_file;
18050 /* The last file a line number was recorded for. */
18051 struct subfile *last_subfile;
18052
18053 /* The function to call to record a line. */
18054 record_line_ftype *record_line;
18055
18056 /* The last line number that was recorded, used to coalesce
18057 consecutive entries for the same line. This can happen, for
18058 example, when discriminators are present. PR 17276. */
18059 unsigned int last_line;
18060 int line_has_non_zero_discriminator;
18061 } lnp_state_machine;
18062
18063 /* There's a lot of static state to pass to dwarf_record_line.
18064 This keeps it all together. */
18065
18066 typedef struct
18067 {
18068 /* The gdbarch. */
18069 struct gdbarch *gdbarch;
18070
18071 /* The line number header. */
18072 struct line_header *line_header;
18073
18074 /* Non-zero if we're recording lines.
18075 Otherwise we're building partial symtabs and are just interested in
18076 finding include files mentioned by the line number program. */
18077 int record_lines_p;
18078 } lnp_reader_state;
18079
18080 /* Ignore this record_line request. */
18081
18082 static void
18083 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
18084 {
18085 return;
18086 }
18087
18088 /* Return non-zero if we should add LINE to the line number table.
18089 LINE is the line to add, LAST_LINE is the last line that was added,
18090 LAST_SUBFILE is the subfile for LAST_LINE.
18091 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
18092 had a non-zero discriminator.
18093
18094 We have to be careful in the presence of discriminators.
18095 E.g., for this line:
18096
18097 for (i = 0; i < 100000; i++);
18098
18099 clang can emit four line number entries for that one line,
18100 each with a different discriminator.
18101 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
18102
18103 However, we want gdb to coalesce all four entries into one.
18104 Otherwise the user could stepi into the middle of the line and
18105 gdb would get confused about whether the pc really was in the
18106 middle of the line.
18107
18108 Things are further complicated by the fact that two consecutive
18109 line number entries for the same line is a heuristic used by gcc
18110 to denote the end of the prologue. So we can't just discard duplicate
18111 entries, we have to be selective about it. The heuristic we use is
18112 that we only collapse consecutive entries for the same line if at least
18113 one of those entries has a non-zero discriminator. PR 17276.
18114
18115 Note: Addresses in the line number state machine can never go backwards
18116 within one sequence, thus this coalescing is ok. */
18117
18118 static int
18119 dwarf_record_line_p (unsigned int line, unsigned int last_line,
18120 int line_has_non_zero_discriminator,
18121 struct subfile *last_subfile)
18122 {
18123 if (current_subfile != last_subfile)
18124 return 1;
18125 if (line != last_line)
18126 return 1;
18127 /* Same line for the same file that we've seen already.
18128 As a last check, for pr 17276, only record the line if the line
18129 has never had a non-zero discriminator. */
18130 if (!line_has_non_zero_discriminator)
18131 return 1;
18132 return 0;
18133 }
18134
18135 /* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
18136 in the line table of subfile SUBFILE. */
18137
18138 static void
18139 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
18140 unsigned int line, CORE_ADDR address,
18141 record_line_ftype p_record_line)
18142 {
18143 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
18144
18145 if (dwarf_line_debug)
18146 {
18147 fprintf_unfiltered (gdb_stdlog,
18148 "Recording line %u, file %s, address %s\n",
18149 line, lbasename (subfile->name),
18150 paddress (gdbarch, address));
18151 }
18152
18153 (*p_record_line) (subfile, line, addr);
18154 }
18155
18156 /* Subroutine of dwarf_decode_lines_1 to simplify it.
18157 Mark the end of a set of line number records.
18158 The arguments are the same as for dwarf_record_line_1.
18159 If SUBFILE is NULL the request is ignored. */
18160
18161 static void
18162 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
18163 CORE_ADDR address, record_line_ftype p_record_line)
18164 {
18165 if (subfile == NULL)
18166 return;
18167
18168 if (dwarf_line_debug)
18169 {
18170 fprintf_unfiltered (gdb_stdlog,
18171 "Finishing current line, file %s, address %s\n",
18172 lbasename (subfile->name),
18173 paddress (gdbarch, address));
18174 }
18175
18176 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
18177 }
18178
18179 /* Record the line in STATE.
18180 END_SEQUENCE is non-zero if we're processing the end of a sequence. */
18181
18182 static void
18183 dwarf_record_line (lnp_reader_state *reader, lnp_state_machine *state,
18184 int end_sequence)
18185 {
18186 const struct line_header *lh = reader->line_header;
18187 unsigned int file, line, discriminator;
18188 int is_stmt;
18189
18190 file = state->file;
18191 line = state->line;
18192 is_stmt = state->is_stmt;
18193 discriminator = state->discriminator;
18194
18195 if (dwarf_line_debug)
18196 {
18197 fprintf_unfiltered (gdb_stdlog,
18198 "Processing actual line %u: file %u,"
18199 " address %s, is_stmt %u, discrim %u\n",
18200 line, file,
18201 paddress (reader->gdbarch, state->address),
18202 is_stmt, discriminator);
18203 }
18204
18205 if (file == 0 || file - 1 >= lh->num_file_names)
18206 dwarf2_debug_line_missing_file_complaint ();
18207 /* For now we ignore lines not starting on an instruction boundary.
18208 But not when processing end_sequence for compatibility with the
18209 previous version of the code. */
18210 else if (state->op_index == 0 || end_sequence)
18211 {
18212 lh->file_names[file - 1].included_p = 1;
18213 if (reader->record_lines_p && is_stmt)
18214 {
18215 if (state->last_subfile != current_subfile || end_sequence)
18216 {
18217 dwarf_finish_line (reader->gdbarch, state->last_subfile,
18218 state->address, state->record_line);
18219 }
18220
18221 if (!end_sequence)
18222 {
18223 if (dwarf_record_line_p (line, state->last_line,
18224 state->line_has_non_zero_discriminator,
18225 state->last_subfile))
18226 {
18227 dwarf_record_line_1 (reader->gdbarch, current_subfile,
18228 line, state->address,
18229 state->record_line);
18230 }
18231 state->last_subfile = current_subfile;
18232 state->last_line = line;
18233 }
18234 }
18235 }
18236 }
18237
18238 /* Initialize STATE for the start of a line number program. */
18239
18240 static void
18241 init_lnp_state_machine (lnp_state_machine *state,
18242 const lnp_reader_state *reader)
18243 {
18244 memset (state, 0, sizeof (*state));
18245
18246 /* Just starting, there is no "last file". */
18247 state->last_file = 0;
18248 state->last_subfile = NULL;
18249
18250 state->record_line = record_line;
18251
18252 state->last_line = 0;
18253 state->line_has_non_zero_discriminator = 0;
18254
18255 /* Initialize these according to the DWARF spec. */
18256 state->op_index = 0;
18257 state->file = 1;
18258 state->line = 1;
18259 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
18260 was a line entry for it so that the backend has a chance to adjust it
18261 and also record it in case it needs it. This is currently used by MIPS
18262 code, cf. `mips_adjust_dwarf2_line'. */
18263 state->address = gdbarch_adjust_dwarf2_line (reader->gdbarch, 0, 0);
18264 state->is_stmt = reader->line_header->default_is_stmt;
18265 state->discriminator = 0;
18266 }
18267
18268 /* Check address and if invalid nop-out the rest of the lines in this
18269 sequence. */
18270
18271 static void
18272 check_line_address (struct dwarf2_cu *cu, lnp_state_machine *state,
18273 const gdb_byte *line_ptr,
18274 CORE_ADDR lowpc, CORE_ADDR address)
18275 {
18276 /* If address < lowpc then it's not a usable value, it's outside the
18277 pc range of the CU. However, we restrict the test to only address
18278 values of zero to preserve GDB's previous behaviour which is to
18279 handle the specific case of a function being GC'd by the linker. */
18280
18281 if (address == 0 && address < lowpc)
18282 {
18283 /* This line table is for a function which has been
18284 GCd by the linker. Ignore it. PR gdb/12528 */
18285
18286 struct objfile *objfile = cu->objfile;
18287 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
18288
18289 complaint (&symfile_complaints,
18290 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
18291 line_offset, objfile_name (objfile));
18292 state->record_line = noop_record_line;
18293 /* Note: sm.record_line is left as noop_record_line
18294 until we see DW_LNE_end_sequence. */
18295 }
18296 }
18297
18298 /* Subroutine of dwarf_decode_lines to simplify it.
18299 Process the line number information in LH.
18300 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
18301 program in order to set included_p for every referenced header. */
18302
18303 static void
18304 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
18305 const int decode_for_pst_p, CORE_ADDR lowpc)
18306 {
18307 const gdb_byte *line_ptr, *extended_end;
18308 const gdb_byte *line_end;
18309 unsigned int bytes_read, extended_len;
18310 unsigned char op_code, extended_op;
18311 CORE_ADDR baseaddr;
18312 struct objfile *objfile = cu->objfile;
18313 bfd *abfd = objfile->obfd;
18314 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18315 /* Non-zero if we're recording line info (as opposed to building partial
18316 symtabs). */
18317 int record_lines_p = !decode_for_pst_p;
18318 /* A collection of things we need to pass to dwarf_record_line. */
18319 lnp_reader_state reader_state;
18320
18321 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
18322
18323 line_ptr = lh->statement_program_start;
18324 line_end = lh->statement_program_end;
18325
18326 reader_state.gdbarch = gdbarch;
18327 reader_state.line_header = lh;
18328 reader_state.record_lines_p = record_lines_p;
18329
18330 /* Read the statement sequences until there's nothing left. */
18331 while (line_ptr < line_end)
18332 {
18333 /* The DWARF line number program state machine. */
18334 lnp_state_machine state_machine;
18335 int end_sequence = 0;
18336
18337 /* Reset the state machine at the start of each sequence. */
18338 init_lnp_state_machine (&state_machine, &reader_state);
18339
18340 if (record_lines_p && lh->num_file_names >= state_machine.file)
18341 {
18342 /* Start a subfile for the current file of the state machine. */
18343 /* lh->include_dirs and lh->file_names are 0-based, but the
18344 directory and file name numbers in the statement program
18345 are 1-based. */
18346 struct file_entry *fe = &lh->file_names[state_machine.file - 1];
18347 const char *dir = NULL;
18348
18349 if (fe->dir_index && lh->include_dirs != NULL)
18350 dir = lh->include_dirs[fe->dir_index - 1];
18351
18352 dwarf2_start_subfile (fe->name, dir);
18353 }
18354
18355 /* Decode the table. */
18356 while (line_ptr < line_end && !end_sequence)
18357 {
18358 op_code = read_1_byte (abfd, line_ptr);
18359 line_ptr += 1;
18360
18361 if (op_code >= lh->opcode_base)
18362 {
18363 /* Special opcode. */
18364 unsigned char adj_opcode;
18365 CORE_ADDR addr_adj;
18366 int line_delta;
18367
18368 adj_opcode = op_code - lh->opcode_base;
18369 addr_adj = (((state_machine.op_index
18370 + (adj_opcode / lh->line_range))
18371 / lh->maximum_ops_per_instruction)
18372 * lh->minimum_instruction_length);
18373 state_machine.address
18374 += gdbarch_adjust_dwarf2_line (gdbarch, addr_adj, 1);
18375 state_machine.op_index = ((state_machine.op_index
18376 + (adj_opcode / lh->line_range))
18377 % lh->maximum_ops_per_instruction);
18378 line_delta = lh->line_base + (adj_opcode % lh->line_range);
18379 state_machine.line += line_delta;
18380 if (line_delta != 0)
18381 state_machine.line_has_non_zero_discriminator
18382 = state_machine.discriminator != 0;
18383
18384 dwarf_record_line (&reader_state, &state_machine, 0);
18385 state_machine.discriminator = 0;
18386 }
18387 else switch (op_code)
18388 {
18389 case DW_LNS_extended_op:
18390 extended_len = read_unsigned_leb128 (abfd, line_ptr,
18391 &bytes_read);
18392 line_ptr += bytes_read;
18393 extended_end = line_ptr + extended_len;
18394 extended_op = read_1_byte (abfd, line_ptr);
18395 line_ptr += 1;
18396 switch (extended_op)
18397 {
18398 case DW_LNE_end_sequence:
18399 state_machine.record_line = record_line;
18400 end_sequence = 1;
18401 break;
18402 case DW_LNE_set_address:
18403 {
18404 CORE_ADDR address
18405 = read_address (abfd, line_ptr, cu, &bytes_read);
18406
18407 line_ptr += bytes_read;
18408 check_line_address (cu, &state_machine, line_ptr,
18409 lowpc, address);
18410 state_machine.op_index = 0;
18411 address += baseaddr;
18412 state_machine.address
18413 = gdbarch_adjust_dwarf2_line (gdbarch, address, 0);
18414 }
18415 break;
18416 case DW_LNE_define_file:
18417 {
18418 const char *cur_file;
18419 unsigned int dir_index, mod_time, length;
18420
18421 cur_file = read_direct_string (abfd, line_ptr,
18422 &bytes_read);
18423 line_ptr += bytes_read;
18424 dir_index =
18425 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18426 line_ptr += bytes_read;
18427 mod_time =
18428 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18429 line_ptr += bytes_read;
18430 length =
18431 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18432 line_ptr += bytes_read;
18433 add_file_name (lh, cur_file, dir_index, mod_time, length);
18434 }
18435 break;
18436 case DW_LNE_set_discriminator:
18437 /* The discriminator is not interesting to the debugger;
18438 just ignore it. We still need to check its value though:
18439 if there are consecutive entries for the same
18440 (non-prologue) line we want to coalesce them.
18441 PR 17276. */
18442 state_machine.discriminator
18443 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18444 state_machine.line_has_non_zero_discriminator
18445 |= state_machine.discriminator != 0;
18446 line_ptr += bytes_read;
18447 break;
18448 default:
18449 complaint (&symfile_complaints,
18450 _("mangled .debug_line section"));
18451 return;
18452 }
18453 /* Make sure that we parsed the extended op correctly. If e.g.
18454 we expected a different address size than the producer used,
18455 we may have read the wrong number of bytes. */
18456 if (line_ptr != extended_end)
18457 {
18458 complaint (&symfile_complaints,
18459 _("mangled .debug_line section"));
18460 return;
18461 }
18462 break;
18463 case DW_LNS_copy:
18464 dwarf_record_line (&reader_state, &state_machine, 0);
18465 state_machine.discriminator = 0;
18466 break;
18467 case DW_LNS_advance_pc:
18468 {
18469 CORE_ADDR adjust
18470 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18471 CORE_ADDR addr_adj;
18472
18473 addr_adj = (((state_machine.op_index + adjust)
18474 / lh->maximum_ops_per_instruction)
18475 * lh->minimum_instruction_length);
18476 state_machine.address
18477 += gdbarch_adjust_dwarf2_line (gdbarch, addr_adj, 1);
18478 state_machine.op_index = ((state_machine.op_index + adjust)
18479 % lh->maximum_ops_per_instruction);
18480 line_ptr += bytes_read;
18481 }
18482 break;
18483 case DW_LNS_advance_line:
18484 {
18485 int line_delta
18486 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
18487
18488 state_machine.line += line_delta;
18489 if (line_delta != 0)
18490 state_machine.line_has_non_zero_discriminator
18491 = state_machine.discriminator != 0;
18492 line_ptr += bytes_read;
18493 }
18494 break;
18495 case DW_LNS_set_file:
18496 {
18497 /* The arrays lh->include_dirs and lh->file_names are
18498 0-based, but the directory and file name numbers in
18499 the statement program are 1-based. */
18500 struct file_entry *fe;
18501 const char *dir = NULL;
18502
18503 state_machine.file = read_unsigned_leb128 (abfd, line_ptr,
18504 &bytes_read);
18505 line_ptr += bytes_read;
18506 if (state_machine.file == 0
18507 || state_machine.file - 1 >= lh->num_file_names)
18508 dwarf2_debug_line_missing_file_complaint ();
18509 else
18510 {
18511 fe = &lh->file_names[state_machine.file - 1];
18512 if (fe->dir_index && lh->include_dirs != NULL)
18513 dir = lh->include_dirs[fe->dir_index - 1];
18514 if (record_lines_p)
18515 {
18516 state_machine.last_subfile = current_subfile;
18517 state_machine.line_has_non_zero_discriminator
18518 = state_machine.discriminator != 0;
18519 dwarf2_start_subfile (fe->name, dir);
18520 }
18521 }
18522 }
18523 break;
18524 case DW_LNS_set_column:
18525 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18526 line_ptr += bytes_read;
18527 break;
18528 case DW_LNS_negate_stmt:
18529 state_machine.is_stmt = (!state_machine.is_stmt);
18530 break;
18531 case DW_LNS_set_basic_block:
18532 break;
18533 /* Add to the address register of the state machine the
18534 address increment value corresponding to special opcode
18535 255. I.e., this value is scaled by the minimum
18536 instruction length since special opcode 255 would have
18537 scaled the increment. */
18538 case DW_LNS_const_add_pc:
18539 {
18540 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
18541 CORE_ADDR addr_adj;
18542
18543 addr_adj = (((state_machine.op_index + adjust)
18544 / lh->maximum_ops_per_instruction)
18545 * lh->minimum_instruction_length);
18546 state_machine.address
18547 += gdbarch_adjust_dwarf2_line (gdbarch, addr_adj, 1);
18548 state_machine.op_index = ((state_machine.op_index + adjust)
18549 % lh->maximum_ops_per_instruction);
18550 }
18551 break;
18552 case DW_LNS_fixed_advance_pc:
18553 {
18554 CORE_ADDR addr_adj;
18555
18556 addr_adj = read_2_bytes (abfd, line_ptr);
18557 state_machine.address
18558 += gdbarch_adjust_dwarf2_line (gdbarch, addr_adj, 1);
18559 state_machine.op_index = 0;
18560 line_ptr += 2;
18561 }
18562 break;
18563 default:
18564 {
18565 /* Unknown standard opcode, ignore it. */
18566 int i;
18567
18568 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
18569 {
18570 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18571 line_ptr += bytes_read;
18572 }
18573 }
18574 }
18575 }
18576
18577 if (!end_sequence)
18578 dwarf2_debug_line_missing_end_sequence_complaint ();
18579
18580 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
18581 in which case we still finish recording the last line). */
18582 dwarf_record_line (&reader_state, &state_machine, 1);
18583 }
18584 }
18585
18586 /* Decode the Line Number Program (LNP) for the given line_header
18587 structure and CU. The actual information extracted and the type
18588 of structures created from the LNP depends on the value of PST.
18589
18590 1. If PST is NULL, then this procedure uses the data from the program
18591 to create all necessary symbol tables, and their linetables.
18592
18593 2. If PST is not NULL, this procedure reads the program to determine
18594 the list of files included by the unit represented by PST, and
18595 builds all the associated partial symbol tables.
18596
18597 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
18598 It is used for relative paths in the line table.
18599 NOTE: When processing partial symtabs (pst != NULL),
18600 comp_dir == pst->dirname.
18601
18602 NOTE: It is important that psymtabs have the same file name (via strcmp)
18603 as the corresponding symtab. Since COMP_DIR is not used in the name of the
18604 symtab we don't use it in the name of the psymtabs we create.
18605 E.g. expand_line_sal requires this when finding psymtabs to expand.
18606 A good testcase for this is mb-inline.exp.
18607
18608 LOWPC is the lowest address in CU (or 0 if not known).
18609
18610 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
18611 for its PC<->lines mapping information. Otherwise only the filename
18612 table is read in. */
18613
18614 static void
18615 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
18616 struct dwarf2_cu *cu, struct partial_symtab *pst,
18617 CORE_ADDR lowpc, int decode_mapping)
18618 {
18619 struct objfile *objfile = cu->objfile;
18620 const int decode_for_pst_p = (pst != NULL);
18621
18622 if (decode_mapping)
18623 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
18624
18625 if (decode_for_pst_p)
18626 {
18627 int file_index;
18628
18629 /* Now that we're done scanning the Line Header Program, we can
18630 create the psymtab of each included file. */
18631 for (file_index = 0; file_index < lh->num_file_names; file_index++)
18632 if (lh->file_names[file_index].included_p == 1)
18633 {
18634 const char *include_name =
18635 psymtab_include_file_name (lh, file_index, pst, comp_dir);
18636 if (include_name != NULL)
18637 dwarf2_create_include_psymtab (include_name, pst, objfile);
18638 }
18639 }
18640 else
18641 {
18642 /* Make sure a symtab is created for every file, even files
18643 which contain only variables (i.e. no code with associated
18644 line numbers). */
18645 struct compunit_symtab *cust = buildsym_compunit_symtab ();
18646 int i;
18647
18648 for (i = 0; i < lh->num_file_names; i++)
18649 {
18650 const char *dir = NULL;
18651 struct file_entry *fe;
18652
18653 fe = &lh->file_names[i];
18654 if (fe->dir_index && lh->include_dirs != NULL)
18655 dir = lh->include_dirs[fe->dir_index - 1];
18656 dwarf2_start_subfile (fe->name, dir);
18657
18658 if (current_subfile->symtab == NULL)
18659 {
18660 current_subfile->symtab
18661 = allocate_symtab (cust, current_subfile->name);
18662 }
18663 fe->symtab = current_subfile->symtab;
18664 }
18665 }
18666 }
18667
18668 /* Start a subfile for DWARF. FILENAME is the name of the file and
18669 DIRNAME the name of the source directory which contains FILENAME
18670 or NULL if not known.
18671 This routine tries to keep line numbers from identical absolute and
18672 relative file names in a common subfile.
18673
18674 Using the `list' example from the GDB testsuite, which resides in
18675 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
18676 of /srcdir/list0.c yields the following debugging information for list0.c:
18677
18678 DW_AT_name: /srcdir/list0.c
18679 DW_AT_comp_dir: /compdir
18680 files.files[0].name: list0.h
18681 files.files[0].dir: /srcdir
18682 files.files[1].name: list0.c
18683 files.files[1].dir: /srcdir
18684
18685 The line number information for list0.c has to end up in a single
18686 subfile, so that `break /srcdir/list0.c:1' works as expected.
18687 start_subfile will ensure that this happens provided that we pass the
18688 concatenation of files.files[1].dir and files.files[1].name as the
18689 subfile's name. */
18690
18691 static void
18692 dwarf2_start_subfile (const char *filename, const char *dirname)
18693 {
18694 char *copy = NULL;
18695
18696 /* In order not to lose the line information directory,
18697 we concatenate it to the filename when it makes sense.
18698 Note that the Dwarf3 standard says (speaking of filenames in line
18699 information): ``The directory index is ignored for file names
18700 that represent full path names''. Thus ignoring dirname in the
18701 `else' branch below isn't an issue. */
18702
18703 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
18704 {
18705 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
18706 filename = copy;
18707 }
18708
18709 start_subfile (filename);
18710
18711 if (copy != NULL)
18712 xfree (copy);
18713 }
18714
18715 /* Start a symtab for DWARF.
18716 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
18717
18718 static struct compunit_symtab *
18719 dwarf2_start_symtab (struct dwarf2_cu *cu,
18720 const char *name, const char *comp_dir, CORE_ADDR low_pc)
18721 {
18722 struct compunit_symtab *cust
18723 = start_symtab (cu->objfile, name, comp_dir, low_pc);
18724
18725 record_debugformat ("DWARF 2");
18726 record_producer (cu->producer);
18727
18728 /* We assume that we're processing GCC output. */
18729 processing_gcc_compilation = 2;
18730
18731 cu->processing_has_namespace_info = 0;
18732
18733 return cust;
18734 }
18735
18736 static void
18737 var_decode_location (struct attribute *attr, struct symbol *sym,
18738 struct dwarf2_cu *cu)
18739 {
18740 struct objfile *objfile = cu->objfile;
18741 struct comp_unit_head *cu_header = &cu->header;
18742
18743 /* NOTE drow/2003-01-30: There used to be a comment and some special
18744 code here to turn a symbol with DW_AT_external and a
18745 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
18746 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
18747 with some versions of binutils) where shared libraries could have
18748 relocations against symbols in their debug information - the
18749 minimal symbol would have the right address, but the debug info
18750 would not. It's no longer necessary, because we will explicitly
18751 apply relocations when we read in the debug information now. */
18752
18753 /* A DW_AT_location attribute with no contents indicates that a
18754 variable has been optimized away. */
18755 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
18756 {
18757 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
18758 return;
18759 }
18760
18761 /* Handle one degenerate form of location expression specially, to
18762 preserve GDB's previous behavior when section offsets are
18763 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
18764 then mark this symbol as LOC_STATIC. */
18765
18766 if (attr_form_is_block (attr)
18767 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
18768 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
18769 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
18770 && (DW_BLOCK (attr)->size
18771 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
18772 {
18773 unsigned int dummy;
18774
18775 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
18776 SYMBOL_VALUE_ADDRESS (sym) =
18777 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
18778 else
18779 SYMBOL_VALUE_ADDRESS (sym) =
18780 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
18781 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
18782 fixup_symbol_section (sym, objfile);
18783 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
18784 SYMBOL_SECTION (sym));
18785 return;
18786 }
18787
18788 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
18789 expression evaluator, and use LOC_COMPUTED only when necessary
18790 (i.e. when the value of a register or memory location is
18791 referenced, or a thread-local block, etc.). Then again, it might
18792 not be worthwhile. I'm assuming that it isn't unless performance
18793 or memory numbers show me otherwise. */
18794
18795 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
18796
18797 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
18798 cu->has_loclist = 1;
18799 }
18800
18801 /* Given a pointer to a DWARF information entry, figure out if we need
18802 to make a symbol table entry for it, and if so, create a new entry
18803 and return a pointer to it.
18804 If TYPE is NULL, determine symbol type from the die, otherwise
18805 used the passed type.
18806 If SPACE is not NULL, use it to hold the new symbol. If it is
18807 NULL, allocate a new symbol on the objfile's obstack. */
18808
18809 static struct symbol *
18810 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
18811 struct symbol *space)
18812 {
18813 struct objfile *objfile = cu->objfile;
18814 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18815 struct symbol *sym = NULL;
18816 const char *name;
18817 struct attribute *attr = NULL;
18818 struct attribute *attr2 = NULL;
18819 CORE_ADDR baseaddr;
18820 struct pending **list_to_add = NULL;
18821
18822 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
18823
18824 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
18825
18826 name = dwarf2_name (die, cu);
18827 if (name)
18828 {
18829 const char *linkagename;
18830 int suppress_add = 0;
18831
18832 if (space)
18833 sym = space;
18834 else
18835 sym = allocate_symbol (objfile);
18836 OBJSTAT (objfile, n_syms++);
18837
18838 /* Cache this symbol's name and the name's demangled form (if any). */
18839 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
18840 linkagename = dwarf2_physname (name, die, cu);
18841 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
18842
18843 /* Fortran does not have mangling standard and the mangling does differ
18844 between gfortran, iFort etc. */
18845 if (cu->language == language_fortran
18846 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
18847 symbol_set_demangled_name (&(sym->ginfo),
18848 dwarf2_full_name (name, die, cu),
18849 NULL);
18850
18851 /* Default assumptions.
18852 Use the passed type or decode it from the die. */
18853 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
18854 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
18855 if (type != NULL)
18856 SYMBOL_TYPE (sym) = type;
18857 else
18858 SYMBOL_TYPE (sym) = die_type (die, cu);
18859 attr = dwarf2_attr (die,
18860 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
18861 cu);
18862 if (attr)
18863 {
18864 SYMBOL_LINE (sym) = DW_UNSND (attr);
18865 }
18866
18867 attr = dwarf2_attr (die,
18868 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
18869 cu);
18870 if (attr)
18871 {
18872 int file_index = DW_UNSND (attr);
18873
18874 if (cu->line_header == NULL
18875 || file_index > cu->line_header->num_file_names)
18876 complaint (&symfile_complaints,
18877 _("file index out of range"));
18878 else if (file_index > 0)
18879 {
18880 struct file_entry *fe;
18881
18882 fe = &cu->line_header->file_names[file_index - 1];
18883 symbol_set_symtab (sym, fe->symtab);
18884 }
18885 }
18886
18887 switch (die->tag)
18888 {
18889 case DW_TAG_label:
18890 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
18891 if (attr)
18892 {
18893 CORE_ADDR addr;
18894
18895 addr = attr_value_as_address (attr);
18896 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
18897 SYMBOL_VALUE_ADDRESS (sym) = addr;
18898 }
18899 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
18900 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
18901 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
18902 add_symbol_to_list (sym, cu->list_in_scope);
18903 break;
18904 case DW_TAG_subprogram:
18905 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
18906 finish_block. */
18907 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
18908 attr2 = dwarf2_attr (die, DW_AT_external, cu);
18909 if ((attr2 && (DW_UNSND (attr2) != 0))
18910 || cu->language == language_ada)
18911 {
18912 /* Subprograms marked external are stored as a global symbol.
18913 Ada subprograms, whether marked external or not, are always
18914 stored as a global symbol, because we want to be able to
18915 access them globally. For instance, we want to be able
18916 to break on a nested subprogram without having to
18917 specify the context. */
18918 list_to_add = &global_symbols;
18919 }
18920 else
18921 {
18922 list_to_add = cu->list_in_scope;
18923 }
18924 break;
18925 case DW_TAG_inlined_subroutine:
18926 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
18927 finish_block. */
18928 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
18929 SYMBOL_INLINED (sym) = 1;
18930 list_to_add = cu->list_in_scope;
18931 break;
18932 case DW_TAG_template_value_param:
18933 suppress_add = 1;
18934 /* Fall through. */
18935 case DW_TAG_constant:
18936 case DW_TAG_variable:
18937 case DW_TAG_member:
18938 /* Compilation with minimal debug info may result in
18939 variables with missing type entries. Change the
18940 misleading `void' type to something sensible. */
18941 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
18942 SYMBOL_TYPE (sym)
18943 = objfile_type (objfile)->nodebug_data_symbol;
18944
18945 attr = dwarf2_attr (die, DW_AT_const_value, cu);
18946 /* In the case of DW_TAG_member, we should only be called for
18947 static const members. */
18948 if (die->tag == DW_TAG_member)
18949 {
18950 /* dwarf2_add_field uses die_is_declaration,
18951 so we do the same. */
18952 gdb_assert (die_is_declaration (die, cu));
18953 gdb_assert (attr);
18954 }
18955 if (attr)
18956 {
18957 dwarf2_const_value (attr, sym, cu);
18958 attr2 = dwarf2_attr (die, DW_AT_external, cu);
18959 if (!suppress_add)
18960 {
18961 if (attr2 && (DW_UNSND (attr2) != 0))
18962 list_to_add = &global_symbols;
18963 else
18964 list_to_add = cu->list_in_scope;
18965 }
18966 break;
18967 }
18968 attr = dwarf2_attr (die, DW_AT_location, cu);
18969 if (attr)
18970 {
18971 var_decode_location (attr, sym, cu);
18972 attr2 = dwarf2_attr (die, DW_AT_external, cu);
18973
18974 /* Fortran explicitly imports any global symbols to the local
18975 scope by DW_TAG_common_block. */
18976 if (cu->language == language_fortran && die->parent
18977 && die->parent->tag == DW_TAG_common_block)
18978 attr2 = NULL;
18979
18980 if (SYMBOL_CLASS (sym) == LOC_STATIC
18981 && SYMBOL_VALUE_ADDRESS (sym) == 0
18982 && !dwarf2_per_objfile->has_section_at_zero)
18983 {
18984 /* When a static variable is eliminated by the linker,
18985 the corresponding debug information is not stripped
18986 out, but the variable address is set to null;
18987 do not add such variables into symbol table. */
18988 }
18989 else if (attr2 && (DW_UNSND (attr2) != 0))
18990 {
18991 /* Workaround gfortran PR debug/40040 - it uses
18992 DW_AT_location for variables in -fPIC libraries which may
18993 get overriden by other libraries/executable and get
18994 a different address. Resolve it by the minimal symbol
18995 which may come from inferior's executable using copy
18996 relocation. Make this workaround only for gfortran as for
18997 other compilers GDB cannot guess the minimal symbol
18998 Fortran mangling kind. */
18999 if (cu->language == language_fortran && die->parent
19000 && die->parent->tag == DW_TAG_module
19001 && cu->producer
19002 && startswith (cu->producer, "GNU Fortran"))
19003 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
19004
19005 /* A variable with DW_AT_external is never static,
19006 but it may be block-scoped. */
19007 list_to_add = (cu->list_in_scope == &file_symbols
19008 ? &global_symbols : cu->list_in_scope);
19009 }
19010 else
19011 list_to_add = cu->list_in_scope;
19012 }
19013 else
19014 {
19015 /* We do not know the address of this symbol.
19016 If it is an external symbol and we have type information
19017 for it, enter the symbol as a LOC_UNRESOLVED symbol.
19018 The address of the variable will then be determined from
19019 the minimal symbol table whenever the variable is
19020 referenced. */
19021 attr2 = dwarf2_attr (die, DW_AT_external, cu);
19022
19023 /* Fortran explicitly imports any global symbols to the local
19024 scope by DW_TAG_common_block. */
19025 if (cu->language == language_fortran && die->parent
19026 && die->parent->tag == DW_TAG_common_block)
19027 {
19028 /* SYMBOL_CLASS doesn't matter here because
19029 read_common_block is going to reset it. */
19030 if (!suppress_add)
19031 list_to_add = cu->list_in_scope;
19032 }
19033 else if (attr2 && (DW_UNSND (attr2) != 0)
19034 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
19035 {
19036 /* A variable with DW_AT_external is never static, but it
19037 may be block-scoped. */
19038 list_to_add = (cu->list_in_scope == &file_symbols
19039 ? &global_symbols : cu->list_in_scope);
19040
19041 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
19042 }
19043 else if (!die_is_declaration (die, cu))
19044 {
19045 /* Use the default LOC_OPTIMIZED_OUT class. */
19046 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
19047 if (!suppress_add)
19048 list_to_add = cu->list_in_scope;
19049 }
19050 }
19051 break;
19052 case DW_TAG_formal_parameter:
19053 /* If we are inside a function, mark this as an argument. If
19054 not, we might be looking at an argument to an inlined function
19055 when we do not have enough information to show inlined frames;
19056 pretend it's a local variable in that case so that the user can
19057 still see it. */
19058 if (context_stack_depth > 0
19059 && context_stack[context_stack_depth - 1].name != NULL)
19060 SYMBOL_IS_ARGUMENT (sym) = 1;
19061 attr = dwarf2_attr (die, DW_AT_location, cu);
19062 if (attr)
19063 {
19064 var_decode_location (attr, sym, cu);
19065 }
19066 attr = dwarf2_attr (die, DW_AT_const_value, cu);
19067 if (attr)
19068 {
19069 dwarf2_const_value (attr, sym, cu);
19070 }
19071
19072 list_to_add = cu->list_in_scope;
19073 break;
19074 case DW_TAG_unspecified_parameters:
19075 /* From varargs functions; gdb doesn't seem to have any
19076 interest in this information, so just ignore it for now.
19077 (FIXME?) */
19078 break;
19079 case DW_TAG_template_type_param:
19080 suppress_add = 1;
19081 /* Fall through. */
19082 case DW_TAG_class_type:
19083 case DW_TAG_interface_type:
19084 case DW_TAG_structure_type:
19085 case DW_TAG_union_type:
19086 case DW_TAG_set_type:
19087 case DW_TAG_enumeration_type:
19088 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19089 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
19090
19091 {
19092 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
19093 really ever be static objects: otherwise, if you try
19094 to, say, break of a class's method and you're in a file
19095 which doesn't mention that class, it won't work unless
19096 the check for all static symbols in lookup_symbol_aux
19097 saves you. See the OtherFileClass tests in
19098 gdb.c++/namespace.exp. */
19099
19100 if (!suppress_add)
19101 {
19102 list_to_add = (cu->list_in_scope == &file_symbols
19103 && cu->language == language_cplus
19104 ? &global_symbols : cu->list_in_scope);
19105
19106 /* The semantics of C++ state that "struct foo {
19107 ... }" also defines a typedef for "foo". */
19108 if (cu->language == language_cplus
19109 || cu->language == language_ada
19110 || cu->language == language_d
19111 || cu->language == language_rust)
19112 {
19113 /* The symbol's name is already allocated along
19114 with this objfile, so we don't need to
19115 duplicate it for the type. */
19116 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
19117 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
19118 }
19119 }
19120 }
19121 break;
19122 case DW_TAG_typedef:
19123 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19124 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
19125 list_to_add = cu->list_in_scope;
19126 break;
19127 case DW_TAG_base_type:
19128 case DW_TAG_subrange_type:
19129 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19130 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
19131 list_to_add = cu->list_in_scope;
19132 break;
19133 case DW_TAG_enumerator:
19134 attr = dwarf2_attr (die, DW_AT_const_value, cu);
19135 if (attr)
19136 {
19137 dwarf2_const_value (attr, sym, cu);
19138 }
19139 {
19140 /* NOTE: carlton/2003-11-10: See comment above in the
19141 DW_TAG_class_type, etc. block. */
19142
19143 list_to_add = (cu->list_in_scope == &file_symbols
19144 && cu->language == language_cplus
19145 ? &global_symbols : cu->list_in_scope);
19146 }
19147 break;
19148 case DW_TAG_imported_declaration:
19149 case DW_TAG_namespace:
19150 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19151 list_to_add = &global_symbols;
19152 break;
19153 case DW_TAG_module:
19154 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19155 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
19156 list_to_add = &global_symbols;
19157 break;
19158 case DW_TAG_common_block:
19159 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
19160 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
19161 add_symbol_to_list (sym, cu->list_in_scope);
19162 break;
19163 default:
19164 /* Not a tag we recognize. Hopefully we aren't processing
19165 trash data, but since we must specifically ignore things
19166 we don't recognize, there is nothing else we should do at
19167 this point. */
19168 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
19169 dwarf_tag_name (die->tag));
19170 break;
19171 }
19172
19173 if (suppress_add)
19174 {
19175 sym->hash_next = objfile->template_symbols;
19176 objfile->template_symbols = sym;
19177 list_to_add = NULL;
19178 }
19179
19180 if (list_to_add != NULL)
19181 add_symbol_to_list (sym, list_to_add);
19182
19183 /* For the benefit of old versions of GCC, check for anonymous
19184 namespaces based on the demangled name. */
19185 if (!cu->processing_has_namespace_info
19186 && cu->language == language_cplus)
19187 cp_scan_for_anonymous_namespaces (sym, objfile);
19188 }
19189 return (sym);
19190 }
19191
19192 /* A wrapper for new_symbol_full that always allocates a new symbol. */
19193
19194 static struct symbol *
19195 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19196 {
19197 return new_symbol_full (die, type, cu, NULL);
19198 }
19199
19200 /* Given an attr with a DW_FORM_dataN value in host byte order,
19201 zero-extend it as appropriate for the symbol's type. The DWARF
19202 standard (v4) is not entirely clear about the meaning of using
19203 DW_FORM_dataN for a constant with a signed type, where the type is
19204 wider than the data. The conclusion of a discussion on the DWARF
19205 list was that this is unspecified. We choose to always zero-extend
19206 because that is the interpretation long in use by GCC. */
19207
19208 static gdb_byte *
19209 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
19210 struct dwarf2_cu *cu, LONGEST *value, int bits)
19211 {
19212 struct objfile *objfile = cu->objfile;
19213 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
19214 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
19215 LONGEST l = DW_UNSND (attr);
19216
19217 if (bits < sizeof (*value) * 8)
19218 {
19219 l &= ((LONGEST) 1 << bits) - 1;
19220 *value = l;
19221 }
19222 else if (bits == sizeof (*value) * 8)
19223 *value = l;
19224 else
19225 {
19226 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
19227 store_unsigned_integer (bytes, bits / 8, byte_order, l);
19228 return bytes;
19229 }
19230
19231 return NULL;
19232 }
19233
19234 /* Read a constant value from an attribute. Either set *VALUE, or if
19235 the value does not fit in *VALUE, set *BYTES - either already
19236 allocated on the objfile obstack, or newly allocated on OBSTACK,
19237 or, set *BATON, if we translated the constant to a location
19238 expression. */
19239
19240 static void
19241 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
19242 const char *name, struct obstack *obstack,
19243 struct dwarf2_cu *cu,
19244 LONGEST *value, const gdb_byte **bytes,
19245 struct dwarf2_locexpr_baton **baton)
19246 {
19247 struct objfile *objfile = cu->objfile;
19248 struct comp_unit_head *cu_header = &cu->header;
19249 struct dwarf_block *blk;
19250 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
19251 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
19252
19253 *value = 0;
19254 *bytes = NULL;
19255 *baton = NULL;
19256
19257 switch (attr->form)
19258 {
19259 case DW_FORM_addr:
19260 case DW_FORM_GNU_addr_index:
19261 {
19262 gdb_byte *data;
19263
19264 if (TYPE_LENGTH (type) != cu_header->addr_size)
19265 dwarf2_const_value_length_mismatch_complaint (name,
19266 cu_header->addr_size,
19267 TYPE_LENGTH (type));
19268 /* Symbols of this form are reasonably rare, so we just
19269 piggyback on the existing location code rather than writing
19270 a new implementation of symbol_computed_ops. */
19271 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
19272 (*baton)->per_cu = cu->per_cu;
19273 gdb_assert ((*baton)->per_cu);
19274
19275 (*baton)->size = 2 + cu_header->addr_size;
19276 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
19277 (*baton)->data = data;
19278
19279 data[0] = DW_OP_addr;
19280 store_unsigned_integer (&data[1], cu_header->addr_size,
19281 byte_order, DW_ADDR (attr));
19282 data[cu_header->addr_size + 1] = DW_OP_stack_value;
19283 }
19284 break;
19285 case DW_FORM_string:
19286 case DW_FORM_strp:
19287 case DW_FORM_GNU_str_index:
19288 case DW_FORM_GNU_strp_alt:
19289 /* DW_STRING is already allocated on the objfile obstack, point
19290 directly to it. */
19291 *bytes = (const gdb_byte *) DW_STRING (attr);
19292 break;
19293 case DW_FORM_block1:
19294 case DW_FORM_block2:
19295 case DW_FORM_block4:
19296 case DW_FORM_block:
19297 case DW_FORM_exprloc:
19298 blk = DW_BLOCK (attr);
19299 if (TYPE_LENGTH (type) != blk->size)
19300 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
19301 TYPE_LENGTH (type));
19302 *bytes = blk->data;
19303 break;
19304
19305 /* The DW_AT_const_value attributes are supposed to carry the
19306 symbol's value "represented as it would be on the target
19307 architecture." By the time we get here, it's already been
19308 converted to host endianness, so we just need to sign- or
19309 zero-extend it as appropriate. */
19310 case DW_FORM_data1:
19311 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
19312 break;
19313 case DW_FORM_data2:
19314 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
19315 break;
19316 case DW_FORM_data4:
19317 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
19318 break;
19319 case DW_FORM_data8:
19320 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
19321 break;
19322
19323 case DW_FORM_sdata:
19324 *value = DW_SND (attr);
19325 break;
19326
19327 case DW_FORM_udata:
19328 *value = DW_UNSND (attr);
19329 break;
19330
19331 default:
19332 complaint (&symfile_complaints,
19333 _("unsupported const value attribute form: '%s'"),
19334 dwarf_form_name (attr->form));
19335 *value = 0;
19336 break;
19337 }
19338 }
19339
19340
19341 /* Copy constant value from an attribute to a symbol. */
19342
19343 static void
19344 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
19345 struct dwarf2_cu *cu)
19346 {
19347 struct objfile *objfile = cu->objfile;
19348 LONGEST value;
19349 const gdb_byte *bytes;
19350 struct dwarf2_locexpr_baton *baton;
19351
19352 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
19353 SYMBOL_PRINT_NAME (sym),
19354 &objfile->objfile_obstack, cu,
19355 &value, &bytes, &baton);
19356
19357 if (baton != NULL)
19358 {
19359 SYMBOL_LOCATION_BATON (sym) = baton;
19360 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
19361 }
19362 else if (bytes != NULL)
19363 {
19364 SYMBOL_VALUE_BYTES (sym) = bytes;
19365 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
19366 }
19367 else
19368 {
19369 SYMBOL_VALUE (sym) = value;
19370 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
19371 }
19372 }
19373
19374 /* Return the type of the die in question using its DW_AT_type attribute. */
19375
19376 static struct type *
19377 die_type (struct die_info *die, struct dwarf2_cu *cu)
19378 {
19379 struct attribute *type_attr;
19380
19381 type_attr = dwarf2_attr (die, DW_AT_type, cu);
19382 if (!type_attr)
19383 {
19384 /* A missing DW_AT_type represents a void type. */
19385 return objfile_type (cu->objfile)->builtin_void;
19386 }
19387
19388 return lookup_die_type (die, type_attr, cu);
19389 }
19390
19391 /* True iff CU's producer generates GNAT Ada auxiliary information
19392 that allows to find parallel types through that information instead
19393 of having to do expensive parallel lookups by type name. */
19394
19395 static int
19396 need_gnat_info (struct dwarf2_cu *cu)
19397 {
19398 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
19399 of GNAT produces this auxiliary information, without any indication
19400 that it is produced. Part of enhancing the FSF version of GNAT
19401 to produce that information will be to put in place an indicator
19402 that we can use in order to determine whether the descriptive type
19403 info is available or not. One suggestion that has been made is
19404 to use a new attribute, attached to the CU die. For now, assume
19405 that the descriptive type info is not available. */
19406 return 0;
19407 }
19408
19409 /* Return the auxiliary type of the die in question using its
19410 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
19411 attribute is not present. */
19412
19413 static struct type *
19414 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
19415 {
19416 struct attribute *type_attr;
19417
19418 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
19419 if (!type_attr)
19420 return NULL;
19421
19422 return lookup_die_type (die, type_attr, cu);
19423 }
19424
19425 /* If DIE has a descriptive_type attribute, then set the TYPE's
19426 descriptive type accordingly. */
19427
19428 static void
19429 set_descriptive_type (struct type *type, struct die_info *die,
19430 struct dwarf2_cu *cu)
19431 {
19432 struct type *descriptive_type = die_descriptive_type (die, cu);
19433
19434 if (descriptive_type)
19435 {
19436 ALLOCATE_GNAT_AUX_TYPE (type);
19437 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
19438 }
19439 }
19440
19441 /* Return the containing type of the die in question using its
19442 DW_AT_containing_type attribute. */
19443
19444 static struct type *
19445 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
19446 {
19447 struct attribute *type_attr;
19448
19449 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
19450 if (!type_attr)
19451 error (_("Dwarf Error: Problem turning containing type into gdb type "
19452 "[in module %s]"), objfile_name (cu->objfile));
19453
19454 return lookup_die_type (die, type_attr, cu);
19455 }
19456
19457 /* Return an error marker type to use for the ill formed type in DIE/CU. */
19458
19459 static struct type *
19460 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
19461 {
19462 struct objfile *objfile = dwarf2_per_objfile->objfile;
19463 char *message, *saved;
19464
19465 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
19466 objfile_name (objfile),
19467 cu->header.offset.sect_off,
19468 die->offset.sect_off);
19469 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
19470 message, strlen (message));
19471 xfree (message);
19472
19473 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
19474 }
19475
19476 /* Look up the type of DIE in CU using its type attribute ATTR.
19477 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
19478 DW_AT_containing_type.
19479 If there is no type substitute an error marker. */
19480
19481 static struct type *
19482 lookup_die_type (struct die_info *die, const struct attribute *attr,
19483 struct dwarf2_cu *cu)
19484 {
19485 struct objfile *objfile = cu->objfile;
19486 struct type *this_type;
19487
19488 gdb_assert (attr->name == DW_AT_type
19489 || attr->name == DW_AT_GNAT_descriptive_type
19490 || attr->name == DW_AT_containing_type);
19491
19492 /* First see if we have it cached. */
19493
19494 if (attr->form == DW_FORM_GNU_ref_alt)
19495 {
19496 struct dwarf2_per_cu_data *per_cu;
19497 sect_offset offset = dwarf2_get_ref_die_offset (attr);
19498
19499 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
19500 this_type = get_die_type_at_offset (offset, per_cu);
19501 }
19502 else if (attr_form_is_ref (attr))
19503 {
19504 sect_offset offset = dwarf2_get_ref_die_offset (attr);
19505
19506 this_type = get_die_type_at_offset (offset, cu->per_cu);
19507 }
19508 else if (attr->form == DW_FORM_ref_sig8)
19509 {
19510 ULONGEST signature = DW_SIGNATURE (attr);
19511
19512 return get_signatured_type (die, signature, cu);
19513 }
19514 else
19515 {
19516 complaint (&symfile_complaints,
19517 _("Dwarf Error: Bad type attribute %s in DIE"
19518 " at 0x%x [in module %s]"),
19519 dwarf_attr_name (attr->name), die->offset.sect_off,
19520 objfile_name (objfile));
19521 return build_error_marker_type (cu, die);
19522 }
19523
19524 /* If not cached we need to read it in. */
19525
19526 if (this_type == NULL)
19527 {
19528 struct die_info *type_die = NULL;
19529 struct dwarf2_cu *type_cu = cu;
19530
19531 if (attr_form_is_ref (attr))
19532 type_die = follow_die_ref (die, attr, &type_cu);
19533 if (type_die == NULL)
19534 return build_error_marker_type (cu, die);
19535 /* If we find the type now, it's probably because the type came
19536 from an inter-CU reference and the type's CU got expanded before
19537 ours. */
19538 this_type = read_type_die (type_die, type_cu);
19539 }
19540
19541 /* If we still don't have a type use an error marker. */
19542
19543 if (this_type == NULL)
19544 return build_error_marker_type (cu, die);
19545
19546 return this_type;
19547 }
19548
19549 /* Return the type in DIE, CU.
19550 Returns NULL for invalid types.
19551
19552 This first does a lookup in die_type_hash,
19553 and only reads the die in if necessary.
19554
19555 NOTE: This can be called when reading in partial or full symbols. */
19556
19557 static struct type *
19558 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
19559 {
19560 struct type *this_type;
19561
19562 this_type = get_die_type (die, cu);
19563 if (this_type)
19564 return this_type;
19565
19566 return read_type_die_1 (die, cu);
19567 }
19568
19569 /* Read the type in DIE, CU.
19570 Returns NULL for invalid types. */
19571
19572 static struct type *
19573 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
19574 {
19575 struct type *this_type = NULL;
19576
19577 switch (die->tag)
19578 {
19579 case DW_TAG_class_type:
19580 case DW_TAG_interface_type:
19581 case DW_TAG_structure_type:
19582 case DW_TAG_union_type:
19583 this_type = read_structure_type (die, cu);
19584 break;
19585 case DW_TAG_enumeration_type:
19586 this_type = read_enumeration_type (die, cu);
19587 break;
19588 case DW_TAG_subprogram:
19589 case DW_TAG_subroutine_type:
19590 case DW_TAG_inlined_subroutine:
19591 this_type = read_subroutine_type (die, cu);
19592 break;
19593 case DW_TAG_array_type:
19594 this_type = read_array_type (die, cu);
19595 break;
19596 case DW_TAG_set_type:
19597 this_type = read_set_type (die, cu);
19598 break;
19599 case DW_TAG_pointer_type:
19600 this_type = read_tag_pointer_type (die, cu);
19601 break;
19602 case DW_TAG_ptr_to_member_type:
19603 this_type = read_tag_ptr_to_member_type (die, cu);
19604 break;
19605 case DW_TAG_reference_type:
19606 this_type = read_tag_reference_type (die, cu);
19607 break;
19608 case DW_TAG_const_type:
19609 this_type = read_tag_const_type (die, cu);
19610 break;
19611 case DW_TAG_volatile_type:
19612 this_type = read_tag_volatile_type (die, cu);
19613 break;
19614 case DW_TAG_restrict_type:
19615 this_type = read_tag_restrict_type (die, cu);
19616 break;
19617 case DW_TAG_string_type:
19618 this_type = read_tag_string_type (die, cu);
19619 break;
19620 case DW_TAG_typedef:
19621 this_type = read_typedef (die, cu);
19622 break;
19623 case DW_TAG_subrange_type:
19624 this_type = read_subrange_type (die, cu);
19625 break;
19626 case DW_TAG_base_type:
19627 this_type = read_base_type (die, cu);
19628 break;
19629 case DW_TAG_unspecified_type:
19630 this_type = read_unspecified_type (die, cu);
19631 break;
19632 case DW_TAG_namespace:
19633 this_type = read_namespace_type (die, cu);
19634 break;
19635 case DW_TAG_module:
19636 this_type = read_module_type (die, cu);
19637 break;
19638 case DW_TAG_atomic_type:
19639 this_type = read_tag_atomic_type (die, cu);
19640 break;
19641 default:
19642 complaint (&symfile_complaints,
19643 _("unexpected tag in read_type_die: '%s'"),
19644 dwarf_tag_name (die->tag));
19645 break;
19646 }
19647
19648 return this_type;
19649 }
19650
19651 /* See if we can figure out if the class lives in a namespace. We do
19652 this by looking for a member function; its demangled name will
19653 contain namespace info, if there is any.
19654 Return the computed name or NULL.
19655 Space for the result is allocated on the objfile's obstack.
19656 This is the full-die version of guess_partial_die_structure_name.
19657 In this case we know DIE has no useful parent. */
19658
19659 static char *
19660 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
19661 {
19662 struct die_info *spec_die;
19663 struct dwarf2_cu *spec_cu;
19664 struct die_info *child;
19665
19666 spec_cu = cu;
19667 spec_die = die_specification (die, &spec_cu);
19668 if (spec_die != NULL)
19669 {
19670 die = spec_die;
19671 cu = spec_cu;
19672 }
19673
19674 for (child = die->child;
19675 child != NULL;
19676 child = child->sibling)
19677 {
19678 if (child->tag == DW_TAG_subprogram)
19679 {
19680 const char *linkage_name;
19681
19682 linkage_name = dwarf2_string_attr (child, DW_AT_linkage_name, cu);
19683 if (linkage_name == NULL)
19684 linkage_name = dwarf2_string_attr (child, DW_AT_MIPS_linkage_name,
19685 cu);
19686 if (linkage_name != NULL)
19687 {
19688 char *actual_name
19689 = language_class_name_from_physname (cu->language_defn,
19690 linkage_name);
19691 char *name = NULL;
19692
19693 if (actual_name != NULL)
19694 {
19695 const char *die_name = dwarf2_name (die, cu);
19696
19697 if (die_name != NULL
19698 && strcmp (die_name, actual_name) != 0)
19699 {
19700 /* Strip off the class name from the full name.
19701 We want the prefix. */
19702 int die_name_len = strlen (die_name);
19703 int actual_name_len = strlen (actual_name);
19704
19705 /* Test for '::' as a sanity check. */
19706 if (actual_name_len > die_name_len + 2
19707 && actual_name[actual_name_len
19708 - die_name_len - 1] == ':')
19709 name = (char *) obstack_copy0 (
19710 &cu->objfile->per_bfd->storage_obstack,
19711 actual_name, actual_name_len - die_name_len - 2);
19712 }
19713 }
19714 xfree (actual_name);
19715 return name;
19716 }
19717 }
19718 }
19719
19720 return NULL;
19721 }
19722
19723 /* GCC might emit a nameless typedef that has a linkage name. Determine the
19724 prefix part in such case. See
19725 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19726
19727 static char *
19728 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
19729 {
19730 struct attribute *attr;
19731 const char *base;
19732
19733 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
19734 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
19735 return NULL;
19736
19737 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
19738 return NULL;
19739
19740 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
19741 if (attr == NULL)
19742 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
19743 if (attr == NULL || DW_STRING (attr) == NULL)
19744 return NULL;
19745
19746 /* dwarf2_name had to be already called. */
19747 gdb_assert (DW_STRING_IS_CANONICAL (attr));
19748
19749 /* Strip the base name, keep any leading namespaces/classes. */
19750 base = strrchr (DW_STRING (attr), ':');
19751 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
19752 return "";
19753
19754 return (char *) obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
19755 DW_STRING (attr),
19756 &base[-1] - DW_STRING (attr));
19757 }
19758
19759 /* Return the name of the namespace/class that DIE is defined within,
19760 or "" if we can't tell. The caller should not xfree the result.
19761
19762 For example, if we're within the method foo() in the following
19763 code:
19764
19765 namespace N {
19766 class C {
19767 void foo () {
19768 }
19769 };
19770 }
19771
19772 then determine_prefix on foo's die will return "N::C". */
19773
19774 static const char *
19775 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
19776 {
19777 struct die_info *parent, *spec_die;
19778 struct dwarf2_cu *spec_cu;
19779 struct type *parent_type;
19780 char *retval;
19781
19782 if (cu->language != language_cplus
19783 && cu->language != language_fortran && cu->language != language_d
19784 && cu->language != language_rust)
19785 return "";
19786
19787 retval = anonymous_struct_prefix (die, cu);
19788 if (retval)
19789 return retval;
19790
19791 /* We have to be careful in the presence of DW_AT_specification.
19792 For example, with GCC 3.4, given the code
19793
19794 namespace N {
19795 void foo() {
19796 // Definition of N::foo.
19797 }
19798 }
19799
19800 then we'll have a tree of DIEs like this:
19801
19802 1: DW_TAG_compile_unit
19803 2: DW_TAG_namespace // N
19804 3: DW_TAG_subprogram // declaration of N::foo
19805 4: DW_TAG_subprogram // definition of N::foo
19806 DW_AT_specification // refers to die #3
19807
19808 Thus, when processing die #4, we have to pretend that we're in
19809 the context of its DW_AT_specification, namely the contex of die
19810 #3. */
19811 spec_cu = cu;
19812 spec_die = die_specification (die, &spec_cu);
19813 if (spec_die == NULL)
19814 parent = die->parent;
19815 else
19816 {
19817 parent = spec_die->parent;
19818 cu = spec_cu;
19819 }
19820
19821 if (parent == NULL)
19822 return "";
19823 else if (parent->building_fullname)
19824 {
19825 const char *name;
19826 const char *parent_name;
19827
19828 /* It has been seen on RealView 2.2 built binaries,
19829 DW_TAG_template_type_param types actually _defined_ as
19830 children of the parent class:
19831
19832 enum E {};
19833 template class <class Enum> Class{};
19834 Class<enum E> class_e;
19835
19836 1: DW_TAG_class_type (Class)
19837 2: DW_TAG_enumeration_type (E)
19838 3: DW_TAG_enumerator (enum1:0)
19839 3: DW_TAG_enumerator (enum2:1)
19840 ...
19841 2: DW_TAG_template_type_param
19842 DW_AT_type DW_FORM_ref_udata (E)
19843
19844 Besides being broken debug info, it can put GDB into an
19845 infinite loop. Consider:
19846
19847 When we're building the full name for Class<E>, we'll start
19848 at Class, and go look over its template type parameters,
19849 finding E. We'll then try to build the full name of E, and
19850 reach here. We're now trying to build the full name of E,
19851 and look over the parent DIE for containing scope. In the
19852 broken case, if we followed the parent DIE of E, we'd again
19853 find Class, and once again go look at its template type
19854 arguments, etc., etc. Simply don't consider such parent die
19855 as source-level parent of this die (it can't be, the language
19856 doesn't allow it), and break the loop here. */
19857 name = dwarf2_name (die, cu);
19858 parent_name = dwarf2_name (parent, cu);
19859 complaint (&symfile_complaints,
19860 _("template param type '%s' defined within parent '%s'"),
19861 name ? name : "<unknown>",
19862 parent_name ? parent_name : "<unknown>");
19863 return "";
19864 }
19865 else
19866 switch (parent->tag)
19867 {
19868 case DW_TAG_namespace:
19869 parent_type = read_type_die (parent, cu);
19870 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
19871 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
19872 Work around this problem here. */
19873 if (cu->language == language_cplus
19874 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
19875 return "";
19876 /* We give a name to even anonymous namespaces. */
19877 return TYPE_TAG_NAME (parent_type);
19878 case DW_TAG_class_type:
19879 case DW_TAG_interface_type:
19880 case DW_TAG_structure_type:
19881 case DW_TAG_union_type:
19882 case DW_TAG_module:
19883 parent_type = read_type_die (parent, cu);
19884 if (TYPE_TAG_NAME (parent_type) != NULL)
19885 return TYPE_TAG_NAME (parent_type);
19886 else
19887 /* An anonymous structure is only allowed non-static data
19888 members; no typedefs, no member functions, et cetera.
19889 So it does not need a prefix. */
19890 return "";
19891 case DW_TAG_compile_unit:
19892 case DW_TAG_partial_unit:
19893 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
19894 if (cu->language == language_cplus
19895 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
19896 && die->child != NULL
19897 && (die->tag == DW_TAG_class_type
19898 || die->tag == DW_TAG_structure_type
19899 || die->tag == DW_TAG_union_type))
19900 {
19901 char *name = guess_full_die_structure_name (die, cu);
19902 if (name != NULL)
19903 return name;
19904 }
19905 return "";
19906 case DW_TAG_enumeration_type:
19907 parent_type = read_type_die (parent, cu);
19908 if (TYPE_DECLARED_CLASS (parent_type))
19909 {
19910 if (TYPE_TAG_NAME (parent_type) != NULL)
19911 return TYPE_TAG_NAME (parent_type);
19912 return "";
19913 }
19914 /* Fall through. */
19915 default:
19916 return determine_prefix (parent, cu);
19917 }
19918 }
19919
19920 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
19921 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
19922 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
19923 an obconcat, otherwise allocate storage for the result. The CU argument is
19924 used to determine the language and hence, the appropriate separator. */
19925
19926 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
19927
19928 static char *
19929 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
19930 int physname, struct dwarf2_cu *cu)
19931 {
19932 const char *lead = "";
19933 const char *sep;
19934
19935 if (suffix == NULL || suffix[0] == '\0'
19936 || prefix == NULL || prefix[0] == '\0')
19937 sep = "";
19938 else if (cu->language == language_d)
19939 {
19940 /* For D, the 'main' function could be defined in any module, but it
19941 should never be prefixed. */
19942 if (strcmp (suffix, "D main") == 0)
19943 {
19944 prefix = "";
19945 sep = "";
19946 }
19947 else
19948 sep = ".";
19949 }
19950 else if (cu->language == language_fortran && physname)
19951 {
19952 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
19953 DW_AT_MIPS_linkage_name is preferred and used instead. */
19954
19955 lead = "__";
19956 sep = "_MOD_";
19957 }
19958 else
19959 sep = "::";
19960
19961 if (prefix == NULL)
19962 prefix = "";
19963 if (suffix == NULL)
19964 suffix = "";
19965
19966 if (obs == NULL)
19967 {
19968 char *retval
19969 = ((char *)
19970 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
19971
19972 strcpy (retval, lead);
19973 strcat (retval, prefix);
19974 strcat (retval, sep);
19975 strcat (retval, suffix);
19976 return retval;
19977 }
19978 else
19979 {
19980 /* We have an obstack. */
19981 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
19982 }
19983 }
19984
19985 /* Return sibling of die, NULL if no sibling. */
19986
19987 static struct die_info *
19988 sibling_die (struct die_info *die)
19989 {
19990 return die->sibling;
19991 }
19992
19993 /* Get name of a die, return NULL if not found. */
19994
19995 static const char *
19996 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
19997 struct obstack *obstack)
19998 {
19999 if (name && cu->language == language_cplus)
20000 {
20001 std::string canon_name = cp_canonicalize_string (name);
20002
20003 if (!canon_name.empty ())
20004 {
20005 if (canon_name != name)
20006 name = (const char *) obstack_copy0 (obstack,
20007 canon_name.c_str (),
20008 canon_name.length ());
20009 }
20010 }
20011
20012 return name;
20013 }
20014
20015 /* Get name of a die, return NULL if not found.
20016 Anonymous namespaces are converted to their magic string. */
20017
20018 static const char *
20019 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
20020 {
20021 struct attribute *attr;
20022
20023 attr = dwarf2_attr (die, DW_AT_name, cu);
20024 if ((!attr || !DW_STRING (attr))
20025 && die->tag != DW_TAG_namespace
20026 && die->tag != DW_TAG_class_type
20027 && die->tag != DW_TAG_interface_type
20028 && die->tag != DW_TAG_structure_type
20029 && die->tag != DW_TAG_union_type)
20030 return NULL;
20031
20032 switch (die->tag)
20033 {
20034 case DW_TAG_compile_unit:
20035 case DW_TAG_partial_unit:
20036 /* Compilation units have a DW_AT_name that is a filename, not
20037 a source language identifier. */
20038 case DW_TAG_enumeration_type:
20039 case DW_TAG_enumerator:
20040 /* These tags always have simple identifiers already; no need
20041 to canonicalize them. */
20042 return DW_STRING (attr);
20043
20044 case DW_TAG_namespace:
20045 if (attr != NULL && DW_STRING (attr) != NULL)
20046 return DW_STRING (attr);
20047 return CP_ANONYMOUS_NAMESPACE_STR;
20048
20049 case DW_TAG_class_type:
20050 case DW_TAG_interface_type:
20051 case DW_TAG_structure_type:
20052 case DW_TAG_union_type:
20053 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
20054 structures or unions. These were of the form "._%d" in GCC 4.1,
20055 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
20056 and GCC 4.4. We work around this problem by ignoring these. */
20057 if (attr && DW_STRING (attr)
20058 && (startswith (DW_STRING (attr), "._")
20059 || startswith (DW_STRING (attr), "<anonymous")))
20060 return NULL;
20061
20062 /* GCC might emit a nameless typedef that has a linkage name. See
20063 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20064 if (!attr || DW_STRING (attr) == NULL)
20065 {
20066 char *demangled = NULL;
20067
20068 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
20069 if (attr == NULL)
20070 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
20071
20072 if (attr == NULL || DW_STRING (attr) == NULL)
20073 return NULL;
20074
20075 /* Avoid demangling DW_STRING (attr) the second time on a second
20076 call for the same DIE. */
20077 if (!DW_STRING_IS_CANONICAL (attr))
20078 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
20079
20080 if (demangled)
20081 {
20082 const char *base;
20083
20084 /* FIXME: we already did this for the partial symbol... */
20085 DW_STRING (attr)
20086 = ((const char *)
20087 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
20088 demangled, strlen (demangled)));
20089 DW_STRING_IS_CANONICAL (attr) = 1;
20090 xfree (demangled);
20091
20092 /* Strip any leading namespaces/classes, keep only the base name.
20093 DW_AT_name for named DIEs does not contain the prefixes. */
20094 base = strrchr (DW_STRING (attr), ':');
20095 if (base && base > DW_STRING (attr) && base[-1] == ':')
20096 return &base[1];
20097 else
20098 return DW_STRING (attr);
20099 }
20100 }
20101 break;
20102
20103 default:
20104 break;
20105 }
20106
20107 if (!DW_STRING_IS_CANONICAL (attr))
20108 {
20109 DW_STRING (attr)
20110 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
20111 &cu->objfile->per_bfd->storage_obstack);
20112 DW_STRING_IS_CANONICAL (attr) = 1;
20113 }
20114 return DW_STRING (attr);
20115 }
20116
20117 /* Return the die that this die in an extension of, or NULL if there
20118 is none. *EXT_CU is the CU containing DIE on input, and the CU
20119 containing the return value on output. */
20120
20121 static struct die_info *
20122 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
20123 {
20124 struct attribute *attr;
20125
20126 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
20127 if (attr == NULL)
20128 return NULL;
20129
20130 return follow_die_ref (die, attr, ext_cu);
20131 }
20132
20133 /* Convert a DIE tag into its string name. */
20134
20135 static const char *
20136 dwarf_tag_name (unsigned tag)
20137 {
20138 const char *name = get_DW_TAG_name (tag);
20139
20140 if (name == NULL)
20141 return "DW_TAG_<unknown>";
20142
20143 return name;
20144 }
20145
20146 /* Convert a DWARF attribute code into its string name. */
20147
20148 static const char *
20149 dwarf_attr_name (unsigned attr)
20150 {
20151 const char *name;
20152
20153 #ifdef MIPS /* collides with DW_AT_HP_block_index */
20154 if (attr == DW_AT_MIPS_fde)
20155 return "DW_AT_MIPS_fde";
20156 #else
20157 if (attr == DW_AT_HP_block_index)
20158 return "DW_AT_HP_block_index";
20159 #endif
20160
20161 name = get_DW_AT_name (attr);
20162
20163 if (name == NULL)
20164 return "DW_AT_<unknown>";
20165
20166 return name;
20167 }
20168
20169 /* Convert a DWARF value form code into its string name. */
20170
20171 static const char *
20172 dwarf_form_name (unsigned form)
20173 {
20174 const char *name = get_DW_FORM_name (form);
20175
20176 if (name == NULL)
20177 return "DW_FORM_<unknown>";
20178
20179 return name;
20180 }
20181
20182 static char *
20183 dwarf_bool_name (unsigned mybool)
20184 {
20185 if (mybool)
20186 return "TRUE";
20187 else
20188 return "FALSE";
20189 }
20190
20191 /* Convert a DWARF type code into its string name. */
20192
20193 static const char *
20194 dwarf_type_encoding_name (unsigned enc)
20195 {
20196 const char *name = get_DW_ATE_name (enc);
20197
20198 if (name == NULL)
20199 return "DW_ATE_<unknown>";
20200
20201 return name;
20202 }
20203
20204 static void
20205 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
20206 {
20207 unsigned int i;
20208
20209 print_spaces (indent, f);
20210 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
20211 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
20212
20213 if (die->parent != NULL)
20214 {
20215 print_spaces (indent, f);
20216 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
20217 die->parent->offset.sect_off);
20218 }
20219
20220 print_spaces (indent, f);
20221 fprintf_unfiltered (f, " has children: %s\n",
20222 dwarf_bool_name (die->child != NULL));
20223
20224 print_spaces (indent, f);
20225 fprintf_unfiltered (f, " attributes:\n");
20226
20227 for (i = 0; i < die->num_attrs; ++i)
20228 {
20229 print_spaces (indent, f);
20230 fprintf_unfiltered (f, " %s (%s) ",
20231 dwarf_attr_name (die->attrs[i].name),
20232 dwarf_form_name (die->attrs[i].form));
20233
20234 switch (die->attrs[i].form)
20235 {
20236 case DW_FORM_addr:
20237 case DW_FORM_GNU_addr_index:
20238 fprintf_unfiltered (f, "address: ");
20239 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
20240 break;
20241 case DW_FORM_block2:
20242 case DW_FORM_block4:
20243 case DW_FORM_block:
20244 case DW_FORM_block1:
20245 fprintf_unfiltered (f, "block: size %s",
20246 pulongest (DW_BLOCK (&die->attrs[i])->size));
20247 break;
20248 case DW_FORM_exprloc:
20249 fprintf_unfiltered (f, "expression: size %s",
20250 pulongest (DW_BLOCK (&die->attrs[i])->size));
20251 break;
20252 case DW_FORM_ref_addr:
20253 fprintf_unfiltered (f, "ref address: ");
20254 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
20255 break;
20256 case DW_FORM_GNU_ref_alt:
20257 fprintf_unfiltered (f, "alt ref address: ");
20258 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
20259 break;
20260 case DW_FORM_ref1:
20261 case DW_FORM_ref2:
20262 case DW_FORM_ref4:
20263 case DW_FORM_ref8:
20264 case DW_FORM_ref_udata:
20265 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
20266 (long) (DW_UNSND (&die->attrs[i])));
20267 break;
20268 case DW_FORM_data1:
20269 case DW_FORM_data2:
20270 case DW_FORM_data4:
20271 case DW_FORM_data8:
20272 case DW_FORM_udata:
20273 case DW_FORM_sdata:
20274 fprintf_unfiltered (f, "constant: %s",
20275 pulongest (DW_UNSND (&die->attrs[i])));
20276 break;
20277 case DW_FORM_sec_offset:
20278 fprintf_unfiltered (f, "section offset: %s",
20279 pulongest (DW_UNSND (&die->attrs[i])));
20280 break;
20281 case DW_FORM_ref_sig8:
20282 fprintf_unfiltered (f, "signature: %s",
20283 hex_string (DW_SIGNATURE (&die->attrs[i])));
20284 break;
20285 case DW_FORM_string:
20286 case DW_FORM_strp:
20287 case DW_FORM_line_strp:
20288 case DW_FORM_GNU_str_index:
20289 case DW_FORM_GNU_strp_alt:
20290 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
20291 DW_STRING (&die->attrs[i])
20292 ? DW_STRING (&die->attrs[i]) : "",
20293 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
20294 break;
20295 case DW_FORM_flag:
20296 if (DW_UNSND (&die->attrs[i]))
20297 fprintf_unfiltered (f, "flag: TRUE");
20298 else
20299 fprintf_unfiltered (f, "flag: FALSE");
20300 break;
20301 case DW_FORM_flag_present:
20302 fprintf_unfiltered (f, "flag: TRUE");
20303 break;
20304 case DW_FORM_indirect:
20305 /* The reader will have reduced the indirect form to
20306 the "base form" so this form should not occur. */
20307 fprintf_unfiltered (f,
20308 "unexpected attribute form: DW_FORM_indirect");
20309 break;
20310 default:
20311 fprintf_unfiltered (f, "unsupported attribute form: %d.",
20312 die->attrs[i].form);
20313 break;
20314 }
20315 fprintf_unfiltered (f, "\n");
20316 }
20317 }
20318
20319 static void
20320 dump_die_for_error (struct die_info *die)
20321 {
20322 dump_die_shallow (gdb_stderr, 0, die);
20323 }
20324
20325 static void
20326 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
20327 {
20328 int indent = level * 4;
20329
20330 gdb_assert (die != NULL);
20331
20332 if (level >= max_level)
20333 return;
20334
20335 dump_die_shallow (f, indent, die);
20336
20337 if (die->child != NULL)
20338 {
20339 print_spaces (indent, f);
20340 fprintf_unfiltered (f, " Children:");
20341 if (level + 1 < max_level)
20342 {
20343 fprintf_unfiltered (f, "\n");
20344 dump_die_1 (f, level + 1, max_level, die->child);
20345 }
20346 else
20347 {
20348 fprintf_unfiltered (f,
20349 " [not printed, max nesting level reached]\n");
20350 }
20351 }
20352
20353 if (die->sibling != NULL && level > 0)
20354 {
20355 dump_die_1 (f, level, max_level, die->sibling);
20356 }
20357 }
20358
20359 /* This is called from the pdie macro in gdbinit.in.
20360 It's not static so gcc will keep a copy callable from gdb. */
20361
20362 void
20363 dump_die (struct die_info *die, int max_level)
20364 {
20365 dump_die_1 (gdb_stdlog, 0, max_level, die);
20366 }
20367
20368 static void
20369 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
20370 {
20371 void **slot;
20372
20373 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
20374 INSERT);
20375
20376 *slot = die;
20377 }
20378
20379 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
20380 required kind. */
20381
20382 static sect_offset
20383 dwarf2_get_ref_die_offset (const struct attribute *attr)
20384 {
20385 sect_offset retval = { DW_UNSND (attr) };
20386
20387 if (attr_form_is_ref (attr))
20388 return retval;
20389
20390 retval.sect_off = 0;
20391 complaint (&symfile_complaints,
20392 _("unsupported die ref attribute form: '%s'"),
20393 dwarf_form_name (attr->form));
20394 return retval;
20395 }
20396
20397 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
20398 * the value held by the attribute is not constant. */
20399
20400 static LONGEST
20401 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
20402 {
20403 if (attr->form == DW_FORM_sdata)
20404 return DW_SND (attr);
20405 else if (attr->form == DW_FORM_udata
20406 || attr->form == DW_FORM_data1
20407 || attr->form == DW_FORM_data2
20408 || attr->form == DW_FORM_data4
20409 || attr->form == DW_FORM_data8)
20410 return DW_UNSND (attr);
20411 else
20412 {
20413 complaint (&symfile_complaints,
20414 _("Attribute value is not a constant (%s)"),
20415 dwarf_form_name (attr->form));
20416 return default_value;
20417 }
20418 }
20419
20420 /* Follow reference or signature attribute ATTR of SRC_DIE.
20421 On entry *REF_CU is the CU of SRC_DIE.
20422 On exit *REF_CU is the CU of the result. */
20423
20424 static struct die_info *
20425 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
20426 struct dwarf2_cu **ref_cu)
20427 {
20428 struct die_info *die;
20429
20430 if (attr_form_is_ref (attr))
20431 die = follow_die_ref (src_die, attr, ref_cu);
20432 else if (attr->form == DW_FORM_ref_sig8)
20433 die = follow_die_sig (src_die, attr, ref_cu);
20434 else
20435 {
20436 dump_die_for_error (src_die);
20437 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
20438 objfile_name ((*ref_cu)->objfile));
20439 }
20440
20441 return die;
20442 }
20443
20444 /* Follow reference OFFSET.
20445 On entry *REF_CU is the CU of the source die referencing OFFSET.
20446 On exit *REF_CU is the CU of the result.
20447 Returns NULL if OFFSET is invalid. */
20448
20449 static struct die_info *
20450 follow_die_offset (sect_offset offset, int offset_in_dwz,
20451 struct dwarf2_cu **ref_cu)
20452 {
20453 struct die_info temp_die;
20454 struct dwarf2_cu *target_cu, *cu = *ref_cu;
20455
20456 gdb_assert (cu->per_cu != NULL);
20457
20458 target_cu = cu;
20459
20460 if (cu->per_cu->is_debug_types)
20461 {
20462 /* .debug_types CUs cannot reference anything outside their CU.
20463 If they need to, they have to reference a signatured type via
20464 DW_FORM_ref_sig8. */
20465 if (! offset_in_cu_p (&cu->header, offset))
20466 return NULL;
20467 }
20468 else if (offset_in_dwz != cu->per_cu->is_dwz
20469 || ! offset_in_cu_p (&cu->header, offset))
20470 {
20471 struct dwarf2_per_cu_data *per_cu;
20472
20473 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
20474 cu->objfile);
20475
20476 /* If necessary, add it to the queue and load its DIEs. */
20477 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
20478 load_full_comp_unit (per_cu, cu->language);
20479
20480 target_cu = per_cu->cu;
20481 }
20482 else if (cu->dies == NULL)
20483 {
20484 /* We're loading full DIEs during partial symbol reading. */
20485 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
20486 load_full_comp_unit (cu->per_cu, language_minimal);
20487 }
20488
20489 *ref_cu = target_cu;
20490 temp_die.offset = offset;
20491 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
20492 &temp_die, offset.sect_off);
20493 }
20494
20495 /* Follow reference attribute ATTR of SRC_DIE.
20496 On entry *REF_CU is the CU of SRC_DIE.
20497 On exit *REF_CU is the CU of the result. */
20498
20499 static struct die_info *
20500 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
20501 struct dwarf2_cu **ref_cu)
20502 {
20503 sect_offset offset = dwarf2_get_ref_die_offset (attr);
20504 struct dwarf2_cu *cu = *ref_cu;
20505 struct die_info *die;
20506
20507 die = follow_die_offset (offset,
20508 (attr->form == DW_FORM_GNU_ref_alt
20509 || cu->per_cu->is_dwz),
20510 ref_cu);
20511 if (!die)
20512 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
20513 "at 0x%x [in module %s]"),
20514 offset.sect_off, src_die->offset.sect_off,
20515 objfile_name (cu->objfile));
20516
20517 return die;
20518 }
20519
20520 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
20521 Returned value is intended for DW_OP_call*. Returned
20522 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
20523
20524 struct dwarf2_locexpr_baton
20525 dwarf2_fetch_die_loc_sect_off (sect_offset offset,
20526 struct dwarf2_per_cu_data *per_cu,
20527 CORE_ADDR (*get_frame_pc) (void *baton),
20528 void *baton)
20529 {
20530 struct dwarf2_cu *cu;
20531 struct die_info *die;
20532 struct attribute *attr;
20533 struct dwarf2_locexpr_baton retval;
20534
20535 dw2_setup (per_cu->objfile);
20536
20537 if (per_cu->cu == NULL)
20538 load_cu (per_cu);
20539 cu = per_cu->cu;
20540 if (cu == NULL)
20541 {
20542 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20543 Instead just throw an error, not much else we can do. */
20544 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
20545 offset.sect_off, objfile_name (per_cu->objfile));
20546 }
20547
20548 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
20549 if (!die)
20550 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
20551 offset.sect_off, objfile_name (per_cu->objfile));
20552
20553 attr = dwarf2_attr (die, DW_AT_location, cu);
20554 if (!attr)
20555 {
20556 /* DWARF: "If there is no such attribute, then there is no effect.".
20557 DATA is ignored if SIZE is 0. */
20558
20559 retval.data = NULL;
20560 retval.size = 0;
20561 }
20562 else if (attr_form_is_section_offset (attr))
20563 {
20564 struct dwarf2_loclist_baton loclist_baton;
20565 CORE_ADDR pc = (*get_frame_pc) (baton);
20566 size_t size;
20567
20568 fill_in_loclist_baton (cu, &loclist_baton, attr);
20569
20570 retval.data = dwarf2_find_location_expression (&loclist_baton,
20571 &size, pc);
20572 retval.size = size;
20573 }
20574 else
20575 {
20576 if (!attr_form_is_block (attr))
20577 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
20578 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
20579 offset.sect_off, objfile_name (per_cu->objfile));
20580
20581 retval.data = DW_BLOCK (attr)->data;
20582 retval.size = DW_BLOCK (attr)->size;
20583 }
20584 retval.per_cu = cu->per_cu;
20585
20586 age_cached_comp_units ();
20587
20588 return retval;
20589 }
20590
20591 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
20592 offset. */
20593
20594 struct dwarf2_locexpr_baton
20595 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
20596 struct dwarf2_per_cu_data *per_cu,
20597 CORE_ADDR (*get_frame_pc) (void *baton),
20598 void *baton)
20599 {
20600 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
20601
20602 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
20603 }
20604
20605 /* Write a constant of a given type as target-ordered bytes into
20606 OBSTACK. */
20607
20608 static const gdb_byte *
20609 write_constant_as_bytes (struct obstack *obstack,
20610 enum bfd_endian byte_order,
20611 struct type *type,
20612 ULONGEST value,
20613 LONGEST *len)
20614 {
20615 gdb_byte *result;
20616
20617 *len = TYPE_LENGTH (type);
20618 result = (gdb_byte *) obstack_alloc (obstack, *len);
20619 store_unsigned_integer (result, *len, byte_order, value);
20620
20621 return result;
20622 }
20623
20624 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
20625 pointer to the constant bytes and set LEN to the length of the
20626 data. If memory is needed, allocate it on OBSTACK. If the DIE
20627 does not have a DW_AT_const_value, return NULL. */
20628
20629 const gdb_byte *
20630 dwarf2_fetch_constant_bytes (sect_offset offset,
20631 struct dwarf2_per_cu_data *per_cu,
20632 struct obstack *obstack,
20633 LONGEST *len)
20634 {
20635 struct dwarf2_cu *cu;
20636 struct die_info *die;
20637 struct attribute *attr;
20638 const gdb_byte *result = NULL;
20639 struct type *type;
20640 LONGEST value;
20641 enum bfd_endian byte_order;
20642
20643 dw2_setup (per_cu->objfile);
20644
20645 if (per_cu->cu == NULL)
20646 load_cu (per_cu);
20647 cu = per_cu->cu;
20648 if (cu == NULL)
20649 {
20650 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20651 Instead just throw an error, not much else we can do. */
20652 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
20653 offset.sect_off, objfile_name (per_cu->objfile));
20654 }
20655
20656 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
20657 if (!die)
20658 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
20659 offset.sect_off, objfile_name (per_cu->objfile));
20660
20661
20662 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20663 if (attr == NULL)
20664 return NULL;
20665
20666 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
20667 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20668
20669 switch (attr->form)
20670 {
20671 case DW_FORM_addr:
20672 case DW_FORM_GNU_addr_index:
20673 {
20674 gdb_byte *tem;
20675
20676 *len = cu->header.addr_size;
20677 tem = (gdb_byte *) obstack_alloc (obstack, *len);
20678 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
20679 result = tem;
20680 }
20681 break;
20682 case DW_FORM_string:
20683 case DW_FORM_strp:
20684 case DW_FORM_GNU_str_index:
20685 case DW_FORM_GNU_strp_alt:
20686 /* DW_STRING is already allocated on the objfile obstack, point
20687 directly to it. */
20688 result = (const gdb_byte *) DW_STRING (attr);
20689 *len = strlen (DW_STRING (attr));
20690 break;
20691 case DW_FORM_block1:
20692 case DW_FORM_block2:
20693 case DW_FORM_block4:
20694 case DW_FORM_block:
20695 case DW_FORM_exprloc:
20696 result = DW_BLOCK (attr)->data;
20697 *len = DW_BLOCK (attr)->size;
20698 break;
20699
20700 /* The DW_AT_const_value attributes are supposed to carry the
20701 symbol's value "represented as it would be on the target
20702 architecture." By the time we get here, it's already been
20703 converted to host endianness, so we just need to sign- or
20704 zero-extend it as appropriate. */
20705 case DW_FORM_data1:
20706 type = die_type (die, cu);
20707 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
20708 if (result == NULL)
20709 result = write_constant_as_bytes (obstack, byte_order,
20710 type, value, len);
20711 break;
20712 case DW_FORM_data2:
20713 type = die_type (die, cu);
20714 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
20715 if (result == NULL)
20716 result = write_constant_as_bytes (obstack, byte_order,
20717 type, value, len);
20718 break;
20719 case DW_FORM_data4:
20720 type = die_type (die, cu);
20721 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
20722 if (result == NULL)
20723 result = write_constant_as_bytes (obstack, byte_order,
20724 type, value, len);
20725 break;
20726 case DW_FORM_data8:
20727 type = die_type (die, cu);
20728 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
20729 if (result == NULL)
20730 result = write_constant_as_bytes (obstack, byte_order,
20731 type, value, len);
20732 break;
20733
20734 case DW_FORM_sdata:
20735 type = die_type (die, cu);
20736 result = write_constant_as_bytes (obstack, byte_order,
20737 type, DW_SND (attr), len);
20738 break;
20739
20740 case DW_FORM_udata:
20741 type = die_type (die, cu);
20742 result = write_constant_as_bytes (obstack, byte_order,
20743 type, DW_UNSND (attr), len);
20744 break;
20745
20746 default:
20747 complaint (&symfile_complaints,
20748 _("unsupported const value attribute form: '%s'"),
20749 dwarf_form_name (attr->form));
20750 break;
20751 }
20752
20753 return result;
20754 }
20755
20756 /* Return the type of the DIE at DIE_OFFSET in the CU named by
20757 PER_CU. */
20758
20759 struct type *
20760 dwarf2_get_die_type (cu_offset die_offset,
20761 struct dwarf2_per_cu_data *per_cu)
20762 {
20763 sect_offset die_offset_sect;
20764
20765 dw2_setup (per_cu->objfile);
20766
20767 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
20768 return get_die_type_at_offset (die_offset_sect, per_cu);
20769 }
20770
20771 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
20772 On entry *REF_CU is the CU of SRC_DIE.
20773 On exit *REF_CU is the CU of the result.
20774 Returns NULL if the referenced DIE isn't found. */
20775
20776 static struct die_info *
20777 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
20778 struct dwarf2_cu **ref_cu)
20779 {
20780 struct die_info temp_die;
20781 struct dwarf2_cu *sig_cu;
20782 struct die_info *die;
20783
20784 /* While it might be nice to assert sig_type->type == NULL here,
20785 we can get here for DW_AT_imported_declaration where we need
20786 the DIE not the type. */
20787
20788 /* If necessary, add it to the queue and load its DIEs. */
20789
20790 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
20791 read_signatured_type (sig_type);
20792
20793 sig_cu = sig_type->per_cu.cu;
20794 gdb_assert (sig_cu != NULL);
20795 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
20796 temp_die.offset = sig_type->type_offset_in_section;
20797 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
20798 temp_die.offset.sect_off);
20799 if (die)
20800 {
20801 /* For .gdb_index version 7 keep track of included TUs.
20802 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
20803 if (dwarf2_per_objfile->index_table != NULL
20804 && dwarf2_per_objfile->index_table->version <= 7)
20805 {
20806 VEC_safe_push (dwarf2_per_cu_ptr,
20807 (*ref_cu)->per_cu->imported_symtabs,
20808 sig_cu->per_cu);
20809 }
20810
20811 *ref_cu = sig_cu;
20812 return die;
20813 }
20814
20815 return NULL;
20816 }
20817
20818 /* Follow signatured type referenced by ATTR in SRC_DIE.
20819 On entry *REF_CU is the CU of SRC_DIE.
20820 On exit *REF_CU is the CU of the result.
20821 The result is the DIE of the type.
20822 If the referenced type cannot be found an error is thrown. */
20823
20824 static struct die_info *
20825 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
20826 struct dwarf2_cu **ref_cu)
20827 {
20828 ULONGEST signature = DW_SIGNATURE (attr);
20829 struct signatured_type *sig_type;
20830 struct die_info *die;
20831
20832 gdb_assert (attr->form == DW_FORM_ref_sig8);
20833
20834 sig_type = lookup_signatured_type (*ref_cu, signature);
20835 /* sig_type will be NULL if the signatured type is missing from
20836 the debug info. */
20837 if (sig_type == NULL)
20838 {
20839 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
20840 " from DIE at 0x%x [in module %s]"),
20841 hex_string (signature), src_die->offset.sect_off,
20842 objfile_name ((*ref_cu)->objfile));
20843 }
20844
20845 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
20846 if (die == NULL)
20847 {
20848 dump_die_for_error (src_die);
20849 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
20850 " from DIE at 0x%x [in module %s]"),
20851 hex_string (signature), src_die->offset.sect_off,
20852 objfile_name ((*ref_cu)->objfile));
20853 }
20854
20855 return die;
20856 }
20857
20858 /* Get the type specified by SIGNATURE referenced in DIE/CU,
20859 reading in and processing the type unit if necessary. */
20860
20861 static struct type *
20862 get_signatured_type (struct die_info *die, ULONGEST signature,
20863 struct dwarf2_cu *cu)
20864 {
20865 struct signatured_type *sig_type;
20866 struct dwarf2_cu *type_cu;
20867 struct die_info *type_die;
20868 struct type *type;
20869
20870 sig_type = lookup_signatured_type (cu, signature);
20871 /* sig_type will be NULL if the signatured type is missing from
20872 the debug info. */
20873 if (sig_type == NULL)
20874 {
20875 complaint (&symfile_complaints,
20876 _("Dwarf Error: Cannot find signatured DIE %s referenced"
20877 " from DIE at 0x%x [in module %s]"),
20878 hex_string (signature), die->offset.sect_off,
20879 objfile_name (dwarf2_per_objfile->objfile));
20880 return build_error_marker_type (cu, die);
20881 }
20882
20883 /* If we already know the type we're done. */
20884 if (sig_type->type != NULL)
20885 return sig_type->type;
20886
20887 type_cu = cu;
20888 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
20889 if (type_die != NULL)
20890 {
20891 /* N.B. We need to call get_die_type to ensure only one type for this DIE
20892 is created. This is important, for example, because for c++ classes
20893 we need TYPE_NAME set which is only done by new_symbol. Blech. */
20894 type = read_type_die (type_die, type_cu);
20895 if (type == NULL)
20896 {
20897 complaint (&symfile_complaints,
20898 _("Dwarf Error: Cannot build signatured type %s"
20899 " referenced from DIE at 0x%x [in module %s]"),
20900 hex_string (signature), die->offset.sect_off,
20901 objfile_name (dwarf2_per_objfile->objfile));
20902 type = build_error_marker_type (cu, die);
20903 }
20904 }
20905 else
20906 {
20907 complaint (&symfile_complaints,
20908 _("Dwarf Error: Problem reading signatured DIE %s referenced"
20909 " from DIE at 0x%x [in module %s]"),
20910 hex_string (signature), die->offset.sect_off,
20911 objfile_name (dwarf2_per_objfile->objfile));
20912 type = build_error_marker_type (cu, die);
20913 }
20914 sig_type->type = type;
20915
20916 return type;
20917 }
20918
20919 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
20920 reading in and processing the type unit if necessary. */
20921
20922 static struct type *
20923 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
20924 struct dwarf2_cu *cu) /* ARI: editCase function */
20925 {
20926 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
20927 if (attr_form_is_ref (attr))
20928 {
20929 struct dwarf2_cu *type_cu = cu;
20930 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
20931
20932 return read_type_die (type_die, type_cu);
20933 }
20934 else if (attr->form == DW_FORM_ref_sig8)
20935 {
20936 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
20937 }
20938 else
20939 {
20940 complaint (&symfile_complaints,
20941 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
20942 " at 0x%x [in module %s]"),
20943 dwarf_form_name (attr->form), die->offset.sect_off,
20944 objfile_name (dwarf2_per_objfile->objfile));
20945 return build_error_marker_type (cu, die);
20946 }
20947 }
20948
20949 /* Load the DIEs associated with type unit PER_CU into memory. */
20950
20951 static void
20952 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
20953 {
20954 struct signatured_type *sig_type;
20955
20956 /* Caller is responsible for ensuring type_unit_groups don't get here. */
20957 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
20958
20959 /* We have the per_cu, but we need the signatured_type.
20960 Fortunately this is an easy translation. */
20961 gdb_assert (per_cu->is_debug_types);
20962 sig_type = (struct signatured_type *) per_cu;
20963
20964 gdb_assert (per_cu->cu == NULL);
20965
20966 read_signatured_type (sig_type);
20967
20968 gdb_assert (per_cu->cu != NULL);
20969 }
20970
20971 /* die_reader_func for read_signatured_type.
20972 This is identical to load_full_comp_unit_reader,
20973 but is kept separate for now. */
20974
20975 static void
20976 read_signatured_type_reader (const struct die_reader_specs *reader,
20977 const gdb_byte *info_ptr,
20978 struct die_info *comp_unit_die,
20979 int has_children,
20980 void *data)
20981 {
20982 struct dwarf2_cu *cu = reader->cu;
20983
20984 gdb_assert (cu->die_hash == NULL);
20985 cu->die_hash =
20986 htab_create_alloc_ex (cu->header.length / 12,
20987 die_hash,
20988 die_eq,
20989 NULL,
20990 &cu->comp_unit_obstack,
20991 hashtab_obstack_allocate,
20992 dummy_obstack_deallocate);
20993
20994 if (has_children)
20995 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
20996 &info_ptr, comp_unit_die);
20997 cu->dies = comp_unit_die;
20998 /* comp_unit_die is not stored in die_hash, no need. */
20999
21000 /* We try not to read any attributes in this function, because not
21001 all CUs needed for references have been loaded yet, and symbol
21002 table processing isn't initialized. But we have to set the CU language,
21003 or we won't be able to build types correctly.
21004 Similarly, if we do not read the producer, we can not apply
21005 producer-specific interpretation. */
21006 prepare_one_comp_unit (cu, cu->dies, language_minimal);
21007 }
21008
21009 /* Read in a signatured type and build its CU and DIEs.
21010 If the type is a stub for the real type in a DWO file,
21011 read in the real type from the DWO file as well. */
21012
21013 static void
21014 read_signatured_type (struct signatured_type *sig_type)
21015 {
21016 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
21017
21018 gdb_assert (per_cu->is_debug_types);
21019 gdb_assert (per_cu->cu == NULL);
21020
21021 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
21022 read_signatured_type_reader, NULL);
21023 sig_type->per_cu.tu_read = 1;
21024 }
21025
21026 /* Decode simple location descriptions.
21027 Given a pointer to a dwarf block that defines a location, compute
21028 the location and return the value.
21029
21030 NOTE drow/2003-11-18: This function is called in two situations
21031 now: for the address of static or global variables (partial symbols
21032 only) and for offsets into structures which are expected to be
21033 (more or less) constant. The partial symbol case should go away,
21034 and only the constant case should remain. That will let this
21035 function complain more accurately. A few special modes are allowed
21036 without complaint for global variables (for instance, global
21037 register values and thread-local values).
21038
21039 A location description containing no operations indicates that the
21040 object is optimized out. The return value is 0 for that case.
21041 FIXME drow/2003-11-16: No callers check for this case any more; soon all
21042 callers will only want a very basic result and this can become a
21043 complaint.
21044
21045 Note that stack[0] is unused except as a default error return. */
21046
21047 static CORE_ADDR
21048 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
21049 {
21050 struct objfile *objfile = cu->objfile;
21051 size_t i;
21052 size_t size = blk->size;
21053 const gdb_byte *data = blk->data;
21054 CORE_ADDR stack[64];
21055 int stacki;
21056 unsigned int bytes_read, unsnd;
21057 gdb_byte op;
21058
21059 i = 0;
21060 stacki = 0;
21061 stack[stacki] = 0;
21062 stack[++stacki] = 0;
21063
21064 while (i < size)
21065 {
21066 op = data[i++];
21067 switch (op)
21068 {
21069 case DW_OP_lit0:
21070 case DW_OP_lit1:
21071 case DW_OP_lit2:
21072 case DW_OP_lit3:
21073 case DW_OP_lit4:
21074 case DW_OP_lit5:
21075 case DW_OP_lit6:
21076 case DW_OP_lit7:
21077 case DW_OP_lit8:
21078 case DW_OP_lit9:
21079 case DW_OP_lit10:
21080 case DW_OP_lit11:
21081 case DW_OP_lit12:
21082 case DW_OP_lit13:
21083 case DW_OP_lit14:
21084 case DW_OP_lit15:
21085 case DW_OP_lit16:
21086 case DW_OP_lit17:
21087 case DW_OP_lit18:
21088 case DW_OP_lit19:
21089 case DW_OP_lit20:
21090 case DW_OP_lit21:
21091 case DW_OP_lit22:
21092 case DW_OP_lit23:
21093 case DW_OP_lit24:
21094 case DW_OP_lit25:
21095 case DW_OP_lit26:
21096 case DW_OP_lit27:
21097 case DW_OP_lit28:
21098 case DW_OP_lit29:
21099 case DW_OP_lit30:
21100 case DW_OP_lit31:
21101 stack[++stacki] = op - DW_OP_lit0;
21102 break;
21103
21104 case DW_OP_reg0:
21105 case DW_OP_reg1:
21106 case DW_OP_reg2:
21107 case DW_OP_reg3:
21108 case DW_OP_reg4:
21109 case DW_OP_reg5:
21110 case DW_OP_reg6:
21111 case DW_OP_reg7:
21112 case DW_OP_reg8:
21113 case DW_OP_reg9:
21114 case DW_OP_reg10:
21115 case DW_OP_reg11:
21116 case DW_OP_reg12:
21117 case DW_OP_reg13:
21118 case DW_OP_reg14:
21119 case DW_OP_reg15:
21120 case DW_OP_reg16:
21121 case DW_OP_reg17:
21122 case DW_OP_reg18:
21123 case DW_OP_reg19:
21124 case DW_OP_reg20:
21125 case DW_OP_reg21:
21126 case DW_OP_reg22:
21127 case DW_OP_reg23:
21128 case DW_OP_reg24:
21129 case DW_OP_reg25:
21130 case DW_OP_reg26:
21131 case DW_OP_reg27:
21132 case DW_OP_reg28:
21133 case DW_OP_reg29:
21134 case DW_OP_reg30:
21135 case DW_OP_reg31:
21136 stack[++stacki] = op - DW_OP_reg0;
21137 if (i < size)
21138 dwarf2_complex_location_expr_complaint ();
21139 break;
21140
21141 case DW_OP_regx:
21142 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
21143 i += bytes_read;
21144 stack[++stacki] = unsnd;
21145 if (i < size)
21146 dwarf2_complex_location_expr_complaint ();
21147 break;
21148
21149 case DW_OP_addr:
21150 stack[++stacki] = read_address (objfile->obfd, &data[i],
21151 cu, &bytes_read);
21152 i += bytes_read;
21153 break;
21154
21155 case DW_OP_const1u:
21156 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
21157 i += 1;
21158 break;
21159
21160 case DW_OP_const1s:
21161 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
21162 i += 1;
21163 break;
21164
21165 case DW_OP_const2u:
21166 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
21167 i += 2;
21168 break;
21169
21170 case DW_OP_const2s:
21171 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
21172 i += 2;
21173 break;
21174
21175 case DW_OP_const4u:
21176 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
21177 i += 4;
21178 break;
21179
21180 case DW_OP_const4s:
21181 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
21182 i += 4;
21183 break;
21184
21185 case DW_OP_const8u:
21186 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
21187 i += 8;
21188 break;
21189
21190 case DW_OP_constu:
21191 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
21192 &bytes_read);
21193 i += bytes_read;
21194 break;
21195
21196 case DW_OP_consts:
21197 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
21198 i += bytes_read;
21199 break;
21200
21201 case DW_OP_dup:
21202 stack[stacki + 1] = stack[stacki];
21203 stacki++;
21204 break;
21205
21206 case DW_OP_plus:
21207 stack[stacki - 1] += stack[stacki];
21208 stacki--;
21209 break;
21210
21211 case DW_OP_plus_uconst:
21212 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
21213 &bytes_read);
21214 i += bytes_read;
21215 break;
21216
21217 case DW_OP_minus:
21218 stack[stacki - 1] -= stack[stacki];
21219 stacki--;
21220 break;
21221
21222 case DW_OP_deref:
21223 /* If we're not the last op, then we definitely can't encode
21224 this using GDB's address_class enum. This is valid for partial
21225 global symbols, although the variable's address will be bogus
21226 in the psymtab. */
21227 if (i < size)
21228 dwarf2_complex_location_expr_complaint ();
21229 break;
21230
21231 case DW_OP_GNU_push_tls_address:
21232 case DW_OP_form_tls_address:
21233 /* The top of the stack has the offset from the beginning
21234 of the thread control block at which the variable is located. */
21235 /* Nothing should follow this operator, so the top of stack would
21236 be returned. */
21237 /* This is valid for partial global symbols, but the variable's
21238 address will be bogus in the psymtab. Make it always at least
21239 non-zero to not look as a variable garbage collected by linker
21240 which have DW_OP_addr 0. */
21241 if (i < size)
21242 dwarf2_complex_location_expr_complaint ();
21243 stack[stacki]++;
21244 break;
21245
21246 case DW_OP_GNU_uninit:
21247 break;
21248
21249 case DW_OP_GNU_addr_index:
21250 case DW_OP_GNU_const_index:
21251 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
21252 &bytes_read);
21253 i += bytes_read;
21254 break;
21255
21256 default:
21257 {
21258 const char *name = get_DW_OP_name (op);
21259
21260 if (name)
21261 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
21262 name);
21263 else
21264 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
21265 op);
21266 }
21267
21268 return (stack[stacki]);
21269 }
21270
21271 /* Enforce maximum stack depth of SIZE-1 to avoid writing
21272 outside of the allocated space. Also enforce minimum>0. */
21273 if (stacki >= ARRAY_SIZE (stack) - 1)
21274 {
21275 complaint (&symfile_complaints,
21276 _("location description stack overflow"));
21277 return 0;
21278 }
21279
21280 if (stacki <= 0)
21281 {
21282 complaint (&symfile_complaints,
21283 _("location description stack underflow"));
21284 return 0;
21285 }
21286 }
21287 return (stack[stacki]);
21288 }
21289
21290 /* memory allocation interface */
21291
21292 static struct dwarf_block *
21293 dwarf_alloc_block (struct dwarf2_cu *cu)
21294 {
21295 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
21296 }
21297
21298 static struct die_info *
21299 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
21300 {
21301 struct die_info *die;
21302 size_t size = sizeof (struct die_info);
21303
21304 if (num_attrs > 1)
21305 size += (num_attrs - 1) * sizeof (struct attribute);
21306
21307 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
21308 memset (die, 0, sizeof (struct die_info));
21309 return (die);
21310 }
21311
21312 \f
21313 /* Macro support. */
21314
21315 /* Return file name relative to the compilation directory of file number I in
21316 *LH's file name table. The result is allocated using xmalloc; the caller is
21317 responsible for freeing it. */
21318
21319 static char *
21320 file_file_name (int file, struct line_header *lh)
21321 {
21322 /* Is the file number a valid index into the line header's file name
21323 table? Remember that file numbers start with one, not zero. */
21324 if (1 <= file && file <= lh->num_file_names)
21325 {
21326 struct file_entry *fe = &lh->file_names[file - 1];
21327
21328 if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0
21329 || lh->include_dirs == NULL)
21330 return xstrdup (fe->name);
21331 return concat (lh->include_dirs[fe->dir_index - 1], SLASH_STRING,
21332 fe->name, (char *) NULL);
21333 }
21334 else
21335 {
21336 /* The compiler produced a bogus file number. We can at least
21337 record the macro definitions made in the file, even if we
21338 won't be able to find the file by name. */
21339 char fake_name[80];
21340
21341 xsnprintf (fake_name, sizeof (fake_name),
21342 "<bad macro file number %d>", file);
21343
21344 complaint (&symfile_complaints,
21345 _("bad file number in macro information (%d)"),
21346 file);
21347
21348 return xstrdup (fake_name);
21349 }
21350 }
21351
21352 /* Return the full name of file number I in *LH's file name table.
21353 Use COMP_DIR as the name of the current directory of the
21354 compilation. The result is allocated using xmalloc; the caller is
21355 responsible for freeing it. */
21356 static char *
21357 file_full_name (int file, struct line_header *lh, const char *comp_dir)
21358 {
21359 /* Is the file number a valid index into the line header's file name
21360 table? Remember that file numbers start with one, not zero. */
21361 if (1 <= file && file <= lh->num_file_names)
21362 {
21363 char *relative = file_file_name (file, lh);
21364
21365 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
21366 return relative;
21367 return reconcat (relative, comp_dir, SLASH_STRING,
21368 relative, (char *) NULL);
21369 }
21370 else
21371 return file_file_name (file, lh);
21372 }
21373
21374
21375 static struct macro_source_file *
21376 macro_start_file (int file, int line,
21377 struct macro_source_file *current_file,
21378 struct line_header *lh)
21379 {
21380 /* File name relative to the compilation directory of this source file. */
21381 char *file_name = file_file_name (file, lh);
21382
21383 if (! current_file)
21384 {
21385 /* Note: We don't create a macro table for this compilation unit
21386 at all until we actually get a filename. */
21387 struct macro_table *macro_table = get_macro_table ();
21388
21389 /* If we have no current file, then this must be the start_file
21390 directive for the compilation unit's main source file. */
21391 current_file = macro_set_main (macro_table, file_name);
21392 macro_define_special (macro_table);
21393 }
21394 else
21395 current_file = macro_include (current_file, line, file_name);
21396
21397 xfree (file_name);
21398
21399 return current_file;
21400 }
21401
21402
21403 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
21404 followed by a null byte. */
21405 static char *
21406 copy_string (const char *buf, int len)
21407 {
21408 char *s = (char *) xmalloc (len + 1);
21409
21410 memcpy (s, buf, len);
21411 s[len] = '\0';
21412 return s;
21413 }
21414
21415
21416 static const char *
21417 consume_improper_spaces (const char *p, const char *body)
21418 {
21419 if (*p == ' ')
21420 {
21421 complaint (&symfile_complaints,
21422 _("macro definition contains spaces "
21423 "in formal argument list:\n`%s'"),
21424 body);
21425
21426 while (*p == ' ')
21427 p++;
21428 }
21429
21430 return p;
21431 }
21432
21433
21434 static void
21435 parse_macro_definition (struct macro_source_file *file, int line,
21436 const char *body)
21437 {
21438 const char *p;
21439
21440 /* The body string takes one of two forms. For object-like macro
21441 definitions, it should be:
21442
21443 <macro name> " " <definition>
21444
21445 For function-like macro definitions, it should be:
21446
21447 <macro name> "() " <definition>
21448 or
21449 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
21450
21451 Spaces may appear only where explicitly indicated, and in the
21452 <definition>.
21453
21454 The Dwarf 2 spec says that an object-like macro's name is always
21455 followed by a space, but versions of GCC around March 2002 omit
21456 the space when the macro's definition is the empty string.
21457
21458 The Dwarf 2 spec says that there should be no spaces between the
21459 formal arguments in a function-like macro's formal argument list,
21460 but versions of GCC around March 2002 include spaces after the
21461 commas. */
21462
21463
21464 /* Find the extent of the macro name. The macro name is terminated
21465 by either a space or null character (for an object-like macro) or
21466 an opening paren (for a function-like macro). */
21467 for (p = body; *p; p++)
21468 if (*p == ' ' || *p == '(')
21469 break;
21470
21471 if (*p == ' ' || *p == '\0')
21472 {
21473 /* It's an object-like macro. */
21474 int name_len = p - body;
21475 char *name = copy_string (body, name_len);
21476 const char *replacement;
21477
21478 if (*p == ' ')
21479 replacement = body + name_len + 1;
21480 else
21481 {
21482 dwarf2_macro_malformed_definition_complaint (body);
21483 replacement = body + name_len;
21484 }
21485
21486 macro_define_object (file, line, name, replacement);
21487
21488 xfree (name);
21489 }
21490 else if (*p == '(')
21491 {
21492 /* It's a function-like macro. */
21493 char *name = copy_string (body, p - body);
21494 int argc = 0;
21495 int argv_size = 1;
21496 char **argv = XNEWVEC (char *, argv_size);
21497
21498 p++;
21499
21500 p = consume_improper_spaces (p, body);
21501
21502 /* Parse the formal argument list. */
21503 while (*p && *p != ')')
21504 {
21505 /* Find the extent of the current argument name. */
21506 const char *arg_start = p;
21507
21508 while (*p && *p != ',' && *p != ')' && *p != ' ')
21509 p++;
21510
21511 if (! *p || p == arg_start)
21512 dwarf2_macro_malformed_definition_complaint (body);
21513 else
21514 {
21515 /* Make sure argv has room for the new argument. */
21516 if (argc >= argv_size)
21517 {
21518 argv_size *= 2;
21519 argv = XRESIZEVEC (char *, argv, argv_size);
21520 }
21521
21522 argv[argc++] = copy_string (arg_start, p - arg_start);
21523 }
21524
21525 p = consume_improper_spaces (p, body);
21526
21527 /* Consume the comma, if present. */
21528 if (*p == ',')
21529 {
21530 p++;
21531
21532 p = consume_improper_spaces (p, body);
21533 }
21534 }
21535
21536 if (*p == ')')
21537 {
21538 p++;
21539
21540 if (*p == ' ')
21541 /* Perfectly formed definition, no complaints. */
21542 macro_define_function (file, line, name,
21543 argc, (const char **) argv,
21544 p + 1);
21545 else if (*p == '\0')
21546 {
21547 /* Complain, but do define it. */
21548 dwarf2_macro_malformed_definition_complaint (body);
21549 macro_define_function (file, line, name,
21550 argc, (const char **) argv,
21551 p);
21552 }
21553 else
21554 /* Just complain. */
21555 dwarf2_macro_malformed_definition_complaint (body);
21556 }
21557 else
21558 /* Just complain. */
21559 dwarf2_macro_malformed_definition_complaint (body);
21560
21561 xfree (name);
21562 {
21563 int i;
21564
21565 for (i = 0; i < argc; i++)
21566 xfree (argv[i]);
21567 }
21568 xfree (argv);
21569 }
21570 else
21571 dwarf2_macro_malformed_definition_complaint (body);
21572 }
21573
21574 /* Skip some bytes from BYTES according to the form given in FORM.
21575 Returns the new pointer. */
21576
21577 static const gdb_byte *
21578 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
21579 enum dwarf_form form,
21580 unsigned int offset_size,
21581 struct dwarf2_section_info *section)
21582 {
21583 unsigned int bytes_read;
21584
21585 switch (form)
21586 {
21587 case DW_FORM_data1:
21588 case DW_FORM_flag:
21589 ++bytes;
21590 break;
21591
21592 case DW_FORM_data2:
21593 bytes += 2;
21594 break;
21595
21596 case DW_FORM_data4:
21597 bytes += 4;
21598 break;
21599
21600 case DW_FORM_data8:
21601 bytes += 8;
21602 break;
21603
21604 case DW_FORM_string:
21605 read_direct_string (abfd, bytes, &bytes_read);
21606 bytes += bytes_read;
21607 break;
21608
21609 case DW_FORM_sec_offset:
21610 case DW_FORM_strp:
21611 case DW_FORM_GNU_strp_alt:
21612 bytes += offset_size;
21613 break;
21614
21615 case DW_FORM_block:
21616 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
21617 bytes += bytes_read;
21618 break;
21619
21620 case DW_FORM_block1:
21621 bytes += 1 + read_1_byte (abfd, bytes);
21622 break;
21623 case DW_FORM_block2:
21624 bytes += 2 + read_2_bytes (abfd, bytes);
21625 break;
21626 case DW_FORM_block4:
21627 bytes += 4 + read_4_bytes (abfd, bytes);
21628 break;
21629
21630 case DW_FORM_sdata:
21631 case DW_FORM_udata:
21632 case DW_FORM_GNU_addr_index:
21633 case DW_FORM_GNU_str_index:
21634 bytes = gdb_skip_leb128 (bytes, buffer_end);
21635 if (bytes == NULL)
21636 {
21637 dwarf2_section_buffer_overflow_complaint (section);
21638 return NULL;
21639 }
21640 break;
21641
21642 default:
21643 {
21644 complain:
21645 complaint (&symfile_complaints,
21646 _("invalid form 0x%x in `%s'"),
21647 form, get_section_name (section));
21648 return NULL;
21649 }
21650 }
21651
21652 return bytes;
21653 }
21654
21655 /* A helper for dwarf_decode_macros that handles skipping an unknown
21656 opcode. Returns an updated pointer to the macro data buffer; or,
21657 on error, issues a complaint and returns NULL. */
21658
21659 static const gdb_byte *
21660 skip_unknown_opcode (unsigned int opcode,
21661 const gdb_byte **opcode_definitions,
21662 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
21663 bfd *abfd,
21664 unsigned int offset_size,
21665 struct dwarf2_section_info *section)
21666 {
21667 unsigned int bytes_read, i;
21668 unsigned long arg;
21669 const gdb_byte *defn;
21670
21671 if (opcode_definitions[opcode] == NULL)
21672 {
21673 complaint (&symfile_complaints,
21674 _("unrecognized DW_MACFINO opcode 0x%x"),
21675 opcode);
21676 return NULL;
21677 }
21678
21679 defn = opcode_definitions[opcode];
21680 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
21681 defn += bytes_read;
21682
21683 for (i = 0; i < arg; ++i)
21684 {
21685 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
21686 (enum dwarf_form) defn[i], offset_size,
21687 section);
21688 if (mac_ptr == NULL)
21689 {
21690 /* skip_form_bytes already issued the complaint. */
21691 return NULL;
21692 }
21693 }
21694
21695 return mac_ptr;
21696 }
21697
21698 /* A helper function which parses the header of a macro section.
21699 If the macro section is the extended (for now called "GNU") type,
21700 then this updates *OFFSET_SIZE. Returns a pointer to just after
21701 the header, or issues a complaint and returns NULL on error. */
21702
21703 static const gdb_byte *
21704 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
21705 bfd *abfd,
21706 const gdb_byte *mac_ptr,
21707 unsigned int *offset_size,
21708 int section_is_gnu)
21709 {
21710 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
21711
21712 if (section_is_gnu)
21713 {
21714 unsigned int version, flags;
21715
21716 version = read_2_bytes (abfd, mac_ptr);
21717 if (version != 4)
21718 {
21719 complaint (&symfile_complaints,
21720 _("unrecognized version `%d' in .debug_macro section"),
21721 version);
21722 return NULL;
21723 }
21724 mac_ptr += 2;
21725
21726 flags = read_1_byte (abfd, mac_ptr);
21727 ++mac_ptr;
21728 *offset_size = (flags & 1) ? 8 : 4;
21729
21730 if ((flags & 2) != 0)
21731 /* We don't need the line table offset. */
21732 mac_ptr += *offset_size;
21733
21734 /* Vendor opcode descriptions. */
21735 if ((flags & 4) != 0)
21736 {
21737 unsigned int i, count;
21738
21739 count = read_1_byte (abfd, mac_ptr);
21740 ++mac_ptr;
21741 for (i = 0; i < count; ++i)
21742 {
21743 unsigned int opcode, bytes_read;
21744 unsigned long arg;
21745
21746 opcode = read_1_byte (abfd, mac_ptr);
21747 ++mac_ptr;
21748 opcode_definitions[opcode] = mac_ptr;
21749 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21750 mac_ptr += bytes_read;
21751 mac_ptr += arg;
21752 }
21753 }
21754 }
21755
21756 return mac_ptr;
21757 }
21758
21759 /* A helper for dwarf_decode_macros that handles the GNU extensions,
21760 including DW_MACRO_GNU_transparent_include. */
21761
21762 static void
21763 dwarf_decode_macro_bytes (bfd *abfd,
21764 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
21765 struct macro_source_file *current_file,
21766 struct line_header *lh,
21767 struct dwarf2_section_info *section,
21768 int section_is_gnu, int section_is_dwz,
21769 unsigned int offset_size,
21770 htab_t include_hash)
21771 {
21772 struct objfile *objfile = dwarf2_per_objfile->objfile;
21773 enum dwarf_macro_record_type macinfo_type;
21774 int at_commandline;
21775 const gdb_byte *opcode_definitions[256];
21776
21777 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
21778 &offset_size, section_is_gnu);
21779 if (mac_ptr == NULL)
21780 {
21781 /* We already issued a complaint. */
21782 return;
21783 }
21784
21785 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
21786 GDB is still reading the definitions from command line. First
21787 DW_MACINFO_start_file will need to be ignored as it was already executed
21788 to create CURRENT_FILE for the main source holding also the command line
21789 definitions. On first met DW_MACINFO_start_file this flag is reset to
21790 normally execute all the remaining DW_MACINFO_start_file macinfos. */
21791
21792 at_commandline = 1;
21793
21794 do
21795 {
21796 /* Do we at least have room for a macinfo type byte? */
21797 if (mac_ptr >= mac_end)
21798 {
21799 dwarf2_section_buffer_overflow_complaint (section);
21800 break;
21801 }
21802
21803 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
21804 mac_ptr++;
21805
21806 /* Note that we rely on the fact that the corresponding GNU and
21807 DWARF constants are the same. */
21808 switch (macinfo_type)
21809 {
21810 /* A zero macinfo type indicates the end of the macro
21811 information. */
21812 case 0:
21813 break;
21814
21815 case DW_MACRO_GNU_define:
21816 case DW_MACRO_GNU_undef:
21817 case DW_MACRO_GNU_define_indirect:
21818 case DW_MACRO_GNU_undef_indirect:
21819 case DW_MACRO_GNU_define_indirect_alt:
21820 case DW_MACRO_GNU_undef_indirect_alt:
21821 {
21822 unsigned int bytes_read;
21823 int line;
21824 const char *body;
21825 int is_define;
21826
21827 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21828 mac_ptr += bytes_read;
21829
21830 if (macinfo_type == DW_MACRO_GNU_define
21831 || macinfo_type == DW_MACRO_GNU_undef)
21832 {
21833 body = read_direct_string (abfd, mac_ptr, &bytes_read);
21834 mac_ptr += bytes_read;
21835 }
21836 else
21837 {
21838 LONGEST str_offset;
21839
21840 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
21841 mac_ptr += offset_size;
21842
21843 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
21844 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
21845 || section_is_dwz)
21846 {
21847 struct dwz_file *dwz = dwarf2_get_dwz_file ();
21848
21849 body = read_indirect_string_from_dwz (dwz, str_offset);
21850 }
21851 else
21852 body = read_indirect_string_at_offset (abfd, str_offset);
21853 }
21854
21855 is_define = (macinfo_type == DW_MACRO_GNU_define
21856 || macinfo_type == DW_MACRO_GNU_define_indirect
21857 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
21858 if (! current_file)
21859 {
21860 /* DWARF violation as no main source is present. */
21861 complaint (&symfile_complaints,
21862 _("debug info with no main source gives macro %s "
21863 "on line %d: %s"),
21864 is_define ? _("definition") : _("undefinition"),
21865 line, body);
21866 break;
21867 }
21868 if ((line == 0 && !at_commandline)
21869 || (line != 0 && at_commandline))
21870 complaint (&symfile_complaints,
21871 _("debug info gives %s macro %s with %s line %d: %s"),
21872 at_commandline ? _("command-line") : _("in-file"),
21873 is_define ? _("definition") : _("undefinition"),
21874 line == 0 ? _("zero") : _("non-zero"), line, body);
21875
21876 if (is_define)
21877 parse_macro_definition (current_file, line, body);
21878 else
21879 {
21880 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
21881 || macinfo_type == DW_MACRO_GNU_undef_indirect
21882 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
21883 macro_undef (current_file, line, body);
21884 }
21885 }
21886 break;
21887
21888 case DW_MACRO_GNU_start_file:
21889 {
21890 unsigned int bytes_read;
21891 int line, file;
21892
21893 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21894 mac_ptr += bytes_read;
21895 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21896 mac_ptr += bytes_read;
21897
21898 if ((line == 0 && !at_commandline)
21899 || (line != 0 && at_commandline))
21900 complaint (&symfile_complaints,
21901 _("debug info gives source %d included "
21902 "from %s at %s line %d"),
21903 file, at_commandline ? _("command-line") : _("file"),
21904 line == 0 ? _("zero") : _("non-zero"), line);
21905
21906 if (at_commandline)
21907 {
21908 /* This DW_MACRO_GNU_start_file was executed in the
21909 pass one. */
21910 at_commandline = 0;
21911 }
21912 else
21913 current_file = macro_start_file (file, line, current_file, lh);
21914 }
21915 break;
21916
21917 case DW_MACRO_GNU_end_file:
21918 if (! current_file)
21919 complaint (&symfile_complaints,
21920 _("macro debug info has an unmatched "
21921 "`close_file' directive"));
21922 else
21923 {
21924 current_file = current_file->included_by;
21925 if (! current_file)
21926 {
21927 enum dwarf_macro_record_type next_type;
21928
21929 /* GCC circa March 2002 doesn't produce the zero
21930 type byte marking the end of the compilation
21931 unit. Complain if it's not there, but exit no
21932 matter what. */
21933
21934 /* Do we at least have room for a macinfo type byte? */
21935 if (mac_ptr >= mac_end)
21936 {
21937 dwarf2_section_buffer_overflow_complaint (section);
21938 return;
21939 }
21940
21941 /* We don't increment mac_ptr here, so this is just
21942 a look-ahead. */
21943 next_type
21944 = (enum dwarf_macro_record_type) read_1_byte (abfd,
21945 mac_ptr);
21946 if (next_type != 0)
21947 complaint (&symfile_complaints,
21948 _("no terminating 0-type entry for "
21949 "macros in `.debug_macinfo' section"));
21950
21951 return;
21952 }
21953 }
21954 break;
21955
21956 case DW_MACRO_GNU_transparent_include:
21957 case DW_MACRO_GNU_transparent_include_alt:
21958 {
21959 LONGEST offset;
21960 void **slot;
21961 bfd *include_bfd = abfd;
21962 struct dwarf2_section_info *include_section = section;
21963 const gdb_byte *include_mac_end = mac_end;
21964 int is_dwz = section_is_dwz;
21965 const gdb_byte *new_mac_ptr;
21966
21967 offset = read_offset_1 (abfd, mac_ptr, offset_size);
21968 mac_ptr += offset_size;
21969
21970 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
21971 {
21972 struct dwz_file *dwz = dwarf2_get_dwz_file ();
21973
21974 dwarf2_read_section (objfile, &dwz->macro);
21975
21976 include_section = &dwz->macro;
21977 include_bfd = get_section_bfd_owner (include_section);
21978 include_mac_end = dwz->macro.buffer + dwz->macro.size;
21979 is_dwz = 1;
21980 }
21981
21982 new_mac_ptr = include_section->buffer + offset;
21983 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
21984
21985 if (*slot != NULL)
21986 {
21987 /* This has actually happened; see
21988 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
21989 complaint (&symfile_complaints,
21990 _("recursive DW_MACRO_GNU_transparent_include in "
21991 ".debug_macro section"));
21992 }
21993 else
21994 {
21995 *slot = (void *) new_mac_ptr;
21996
21997 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
21998 include_mac_end, current_file, lh,
21999 section, section_is_gnu, is_dwz,
22000 offset_size, include_hash);
22001
22002 htab_remove_elt (include_hash, (void *) new_mac_ptr);
22003 }
22004 }
22005 break;
22006
22007 case DW_MACINFO_vendor_ext:
22008 if (!section_is_gnu)
22009 {
22010 unsigned int bytes_read;
22011
22012 /* This reads the constant, but since we don't recognize
22013 any vendor extensions, we ignore it. */
22014 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22015 mac_ptr += bytes_read;
22016 read_direct_string (abfd, mac_ptr, &bytes_read);
22017 mac_ptr += bytes_read;
22018
22019 /* We don't recognize any vendor extensions. */
22020 break;
22021 }
22022 /* FALLTHROUGH */
22023
22024 default:
22025 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
22026 mac_ptr, mac_end, abfd, offset_size,
22027 section);
22028 if (mac_ptr == NULL)
22029 return;
22030 break;
22031 }
22032 } while (macinfo_type != 0);
22033 }
22034
22035 static void
22036 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
22037 int section_is_gnu)
22038 {
22039 struct objfile *objfile = dwarf2_per_objfile->objfile;
22040 struct line_header *lh = cu->line_header;
22041 bfd *abfd;
22042 const gdb_byte *mac_ptr, *mac_end;
22043 struct macro_source_file *current_file = 0;
22044 enum dwarf_macro_record_type macinfo_type;
22045 unsigned int offset_size = cu->header.offset_size;
22046 const gdb_byte *opcode_definitions[256];
22047 struct cleanup *cleanup;
22048 void **slot;
22049 struct dwarf2_section_info *section;
22050 const char *section_name;
22051
22052 if (cu->dwo_unit != NULL)
22053 {
22054 if (section_is_gnu)
22055 {
22056 section = &cu->dwo_unit->dwo_file->sections.macro;
22057 section_name = ".debug_macro.dwo";
22058 }
22059 else
22060 {
22061 section = &cu->dwo_unit->dwo_file->sections.macinfo;
22062 section_name = ".debug_macinfo.dwo";
22063 }
22064 }
22065 else
22066 {
22067 if (section_is_gnu)
22068 {
22069 section = &dwarf2_per_objfile->macro;
22070 section_name = ".debug_macro";
22071 }
22072 else
22073 {
22074 section = &dwarf2_per_objfile->macinfo;
22075 section_name = ".debug_macinfo";
22076 }
22077 }
22078
22079 dwarf2_read_section (objfile, section);
22080 if (section->buffer == NULL)
22081 {
22082 complaint (&symfile_complaints, _("missing %s section"), section_name);
22083 return;
22084 }
22085 abfd = get_section_bfd_owner (section);
22086
22087 /* First pass: Find the name of the base filename.
22088 This filename is needed in order to process all macros whose definition
22089 (or undefinition) comes from the command line. These macros are defined
22090 before the first DW_MACINFO_start_file entry, and yet still need to be
22091 associated to the base file.
22092
22093 To determine the base file name, we scan the macro definitions until we
22094 reach the first DW_MACINFO_start_file entry. We then initialize
22095 CURRENT_FILE accordingly so that any macro definition found before the
22096 first DW_MACINFO_start_file can still be associated to the base file. */
22097
22098 mac_ptr = section->buffer + offset;
22099 mac_end = section->buffer + section->size;
22100
22101 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
22102 &offset_size, section_is_gnu);
22103 if (mac_ptr == NULL)
22104 {
22105 /* We already issued a complaint. */
22106 return;
22107 }
22108
22109 do
22110 {
22111 /* Do we at least have room for a macinfo type byte? */
22112 if (mac_ptr >= mac_end)
22113 {
22114 /* Complaint is printed during the second pass as GDB will probably
22115 stop the first pass earlier upon finding
22116 DW_MACINFO_start_file. */
22117 break;
22118 }
22119
22120 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
22121 mac_ptr++;
22122
22123 /* Note that we rely on the fact that the corresponding GNU and
22124 DWARF constants are the same. */
22125 switch (macinfo_type)
22126 {
22127 /* A zero macinfo type indicates the end of the macro
22128 information. */
22129 case 0:
22130 break;
22131
22132 case DW_MACRO_GNU_define:
22133 case DW_MACRO_GNU_undef:
22134 /* Only skip the data by MAC_PTR. */
22135 {
22136 unsigned int bytes_read;
22137
22138 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22139 mac_ptr += bytes_read;
22140 read_direct_string (abfd, mac_ptr, &bytes_read);
22141 mac_ptr += bytes_read;
22142 }
22143 break;
22144
22145 case DW_MACRO_GNU_start_file:
22146 {
22147 unsigned int bytes_read;
22148 int line, file;
22149
22150 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22151 mac_ptr += bytes_read;
22152 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22153 mac_ptr += bytes_read;
22154
22155 current_file = macro_start_file (file, line, current_file, lh);
22156 }
22157 break;
22158
22159 case DW_MACRO_GNU_end_file:
22160 /* No data to skip by MAC_PTR. */
22161 break;
22162
22163 case DW_MACRO_GNU_define_indirect:
22164 case DW_MACRO_GNU_undef_indirect:
22165 case DW_MACRO_GNU_define_indirect_alt:
22166 case DW_MACRO_GNU_undef_indirect_alt:
22167 {
22168 unsigned int bytes_read;
22169
22170 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22171 mac_ptr += bytes_read;
22172 mac_ptr += offset_size;
22173 }
22174 break;
22175
22176 case DW_MACRO_GNU_transparent_include:
22177 case DW_MACRO_GNU_transparent_include_alt:
22178 /* Note that, according to the spec, a transparent include
22179 chain cannot call DW_MACRO_GNU_start_file. So, we can just
22180 skip this opcode. */
22181 mac_ptr += offset_size;
22182 break;
22183
22184 case DW_MACINFO_vendor_ext:
22185 /* Only skip the data by MAC_PTR. */
22186 if (!section_is_gnu)
22187 {
22188 unsigned int bytes_read;
22189
22190 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22191 mac_ptr += bytes_read;
22192 read_direct_string (abfd, mac_ptr, &bytes_read);
22193 mac_ptr += bytes_read;
22194 }
22195 /* FALLTHROUGH */
22196
22197 default:
22198 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
22199 mac_ptr, mac_end, abfd, offset_size,
22200 section);
22201 if (mac_ptr == NULL)
22202 return;
22203 break;
22204 }
22205 } while (macinfo_type != 0 && current_file == NULL);
22206
22207 /* Second pass: Process all entries.
22208
22209 Use the AT_COMMAND_LINE flag to determine whether we are still processing
22210 command-line macro definitions/undefinitions. This flag is unset when we
22211 reach the first DW_MACINFO_start_file entry. */
22212
22213 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
22214 htab_eq_pointer,
22215 NULL, xcalloc, xfree));
22216 mac_ptr = section->buffer + offset;
22217 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
22218 *slot = (void *) mac_ptr;
22219 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
22220 current_file, lh, section,
22221 section_is_gnu, 0, offset_size,
22222 include_hash.get ());
22223 }
22224
22225 /* Check if the attribute's form is a DW_FORM_block*
22226 if so return true else false. */
22227
22228 static int
22229 attr_form_is_block (const struct attribute *attr)
22230 {
22231 return (attr == NULL ? 0 :
22232 attr->form == DW_FORM_block1
22233 || attr->form == DW_FORM_block2
22234 || attr->form == DW_FORM_block4
22235 || attr->form == DW_FORM_block
22236 || attr->form == DW_FORM_exprloc);
22237 }
22238
22239 /* Return non-zero if ATTR's value is a section offset --- classes
22240 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
22241 You may use DW_UNSND (attr) to retrieve such offsets.
22242
22243 Section 7.5.4, "Attribute Encodings", explains that no attribute
22244 may have a value that belongs to more than one of these classes; it
22245 would be ambiguous if we did, because we use the same forms for all
22246 of them. */
22247
22248 static int
22249 attr_form_is_section_offset (const struct attribute *attr)
22250 {
22251 return (attr->form == DW_FORM_data4
22252 || attr->form == DW_FORM_data8
22253 || attr->form == DW_FORM_sec_offset);
22254 }
22255
22256 /* Return non-zero if ATTR's value falls in the 'constant' class, or
22257 zero otherwise. When this function returns true, you can apply
22258 dwarf2_get_attr_constant_value to it.
22259
22260 However, note that for some attributes you must check
22261 attr_form_is_section_offset before using this test. DW_FORM_data4
22262 and DW_FORM_data8 are members of both the constant class, and of
22263 the classes that contain offsets into other debug sections
22264 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
22265 that, if an attribute's can be either a constant or one of the
22266 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
22267 taken as section offsets, not constants. */
22268
22269 static int
22270 attr_form_is_constant (const struct attribute *attr)
22271 {
22272 switch (attr->form)
22273 {
22274 case DW_FORM_sdata:
22275 case DW_FORM_udata:
22276 case DW_FORM_data1:
22277 case DW_FORM_data2:
22278 case DW_FORM_data4:
22279 case DW_FORM_data8:
22280 return 1;
22281 default:
22282 return 0;
22283 }
22284 }
22285
22286
22287 /* DW_ADDR is always stored already as sect_offset; despite for the forms
22288 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
22289
22290 static int
22291 attr_form_is_ref (const struct attribute *attr)
22292 {
22293 switch (attr->form)
22294 {
22295 case DW_FORM_ref_addr:
22296 case DW_FORM_ref1:
22297 case DW_FORM_ref2:
22298 case DW_FORM_ref4:
22299 case DW_FORM_ref8:
22300 case DW_FORM_ref_udata:
22301 case DW_FORM_GNU_ref_alt:
22302 return 1;
22303 default:
22304 return 0;
22305 }
22306 }
22307
22308 /* Return the .debug_loc section to use for CU.
22309 For DWO files use .debug_loc.dwo. */
22310
22311 static struct dwarf2_section_info *
22312 cu_debug_loc_section (struct dwarf2_cu *cu)
22313 {
22314 if (cu->dwo_unit)
22315 {
22316 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
22317
22318 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
22319 }
22320 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
22321 : &dwarf2_per_objfile->loc);
22322 }
22323
22324 /* A helper function that fills in a dwarf2_loclist_baton. */
22325
22326 static void
22327 fill_in_loclist_baton (struct dwarf2_cu *cu,
22328 struct dwarf2_loclist_baton *baton,
22329 const struct attribute *attr)
22330 {
22331 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22332
22333 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
22334
22335 baton->per_cu = cu->per_cu;
22336 gdb_assert (baton->per_cu);
22337 /* We don't know how long the location list is, but make sure we
22338 don't run off the edge of the section. */
22339 baton->size = section->size - DW_UNSND (attr);
22340 baton->data = section->buffer + DW_UNSND (attr);
22341 baton->base_address = cu->base_address;
22342 baton->from_dwo = cu->dwo_unit != NULL;
22343 }
22344
22345 static void
22346 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
22347 struct dwarf2_cu *cu, int is_block)
22348 {
22349 struct objfile *objfile = dwarf2_per_objfile->objfile;
22350 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22351
22352 if (attr_form_is_section_offset (attr)
22353 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
22354 the section. If so, fall through to the complaint in the
22355 other branch. */
22356 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
22357 {
22358 struct dwarf2_loclist_baton *baton;
22359
22360 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
22361
22362 fill_in_loclist_baton (cu, baton, attr);
22363
22364 if (cu->base_known == 0)
22365 complaint (&symfile_complaints,
22366 _("Location list used without "
22367 "specifying the CU base address."));
22368
22369 SYMBOL_ACLASS_INDEX (sym) = (is_block
22370 ? dwarf2_loclist_block_index
22371 : dwarf2_loclist_index);
22372 SYMBOL_LOCATION_BATON (sym) = baton;
22373 }
22374 else
22375 {
22376 struct dwarf2_locexpr_baton *baton;
22377
22378 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
22379 baton->per_cu = cu->per_cu;
22380 gdb_assert (baton->per_cu);
22381
22382 if (attr_form_is_block (attr))
22383 {
22384 /* Note that we're just copying the block's data pointer
22385 here, not the actual data. We're still pointing into the
22386 info_buffer for SYM's objfile; right now we never release
22387 that buffer, but when we do clean up properly this may
22388 need to change. */
22389 baton->size = DW_BLOCK (attr)->size;
22390 baton->data = DW_BLOCK (attr)->data;
22391 }
22392 else
22393 {
22394 dwarf2_invalid_attrib_class_complaint ("location description",
22395 SYMBOL_NATURAL_NAME (sym));
22396 baton->size = 0;
22397 }
22398
22399 SYMBOL_ACLASS_INDEX (sym) = (is_block
22400 ? dwarf2_locexpr_block_index
22401 : dwarf2_locexpr_index);
22402 SYMBOL_LOCATION_BATON (sym) = baton;
22403 }
22404 }
22405
22406 /* Return the OBJFILE associated with the compilation unit CU. If CU
22407 came from a separate debuginfo file, then the master objfile is
22408 returned. */
22409
22410 struct objfile *
22411 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
22412 {
22413 struct objfile *objfile = per_cu->objfile;
22414
22415 /* Return the master objfile, so that we can report and look up the
22416 correct file containing this variable. */
22417 if (objfile->separate_debug_objfile_backlink)
22418 objfile = objfile->separate_debug_objfile_backlink;
22419
22420 return objfile;
22421 }
22422
22423 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
22424 (CU_HEADERP is unused in such case) or prepare a temporary copy at
22425 CU_HEADERP first. */
22426
22427 static const struct comp_unit_head *
22428 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
22429 struct dwarf2_per_cu_data *per_cu)
22430 {
22431 const gdb_byte *info_ptr;
22432
22433 if (per_cu->cu)
22434 return &per_cu->cu->header;
22435
22436 info_ptr = per_cu->section->buffer + per_cu->offset.sect_off;
22437
22438 memset (cu_headerp, 0, sizeof (*cu_headerp));
22439 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
22440 rcuh_kind::COMPILE);
22441
22442 return cu_headerp;
22443 }
22444
22445 /* Return the address size given in the compilation unit header for CU. */
22446
22447 int
22448 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
22449 {
22450 struct comp_unit_head cu_header_local;
22451 const struct comp_unit_head *cu_headerp;
22452
22453 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22454
22455 return cu_headerp->addr_size;
22456 }
22457
22458 /* Return the offset size given in the compilation unit header for CU. */
22459
22460 int
22461 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
22462 {
22463 struct comp_unit_head cu_header_local;
22464 const struct comp_unit_head *cu_headerp;
22465
22466 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22467
22468 return cu_headerp->offset_size;
22469 }
22470
22471 /* See its dwarf2loc.h declaration. */
22472
22473 int
22474 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
22475 {
22476 struct comp_unit_head cu_header_local;
22477 const struct comp_unit_head *cu_headerp;
22478
22479 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22480
22481 if (cu_headerp->version == 2)
22482 return cu_headerp->addr_size;
22483 else
22484 return cu_headerp->offset_size;
22485 }
22486
22487 /* Return the text offset of the CU. The returned offset comes from
22488 this CU's objfile. If this objfile came from a separate debuginfo
22489 file, then the offset may be different from the corresponding
22490 offset in the parent objfile. */
22491
22492 CORE_ADDR
22493 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
22494 {
22495 struct objfile *objfile = per_cu->objfile;
22496
22497 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
22498 }
22499
22500 /* Return DWARF version number of PER_CU. */
22501
22502 short
22503 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
22504 {
22505 return per_cu->dwarf_version;
22506 }
22507
22508 /* Locate the .debug_info compilation unit from CU's objfile which contains
22509 the DIE at OFFSET. Raises an error on failure. */
22510
22511 static struct dwarf2_per_cu_data *
22512 dwarf2_find_containing_comp_unit (sect_offset offset,
22513 unsigned int offset_in_dwz,
22514 struct objfile *objfile)
22515 {
22516 struct dwarf2_per_cu_data *this_cu;
22517 int low, high;
22518 const sect_offset *cu_off;
22519
22520 low = 0;
22521 high = dwarf2_per_objfile->n_comp_units - 1;
22522 while (high > low)
22523 {
22524 struct dwarf2_per_cu_data *mid_cu;
22525 int mid = low + (high - low) / 2;
22526
22527 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
22528 cu_off = &mid_cu->offset;
22529 if (mid_cu->is_dwz > offset_in_dwz
22530 || (mid_cu->is_dwz == offset_in_dwz
22531 && cu_off->sect_off >= offset.sect_off))
22532 high = mid;
22533 else
22534 low = mid + 1;
22535 }
22536 gdb_assert (low == high);
22537 this_cu = dwarf2_per_objfile->all_comp_units[low];
22538 cu_off = &this_cu->offset;
22539 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
22540 {
22541 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
22542 error (_("Dwarf Error: could not find partial DIE containing "
22543 "offset 0x%lx [in module %s]"),
22544 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
22545
22546 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
22547 <= offset.sect_off);
22548 return dwarf2_per_objfile->all_comp_units[low-1];
22549 }
22550 else
22551 {
22552 this_cu = dwarf2_per_objfile->all_comp_units[low];
22553 if (low == dwarf2_per_objfile->n_comp_units - 1
22554 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
22555 error (_("invalid dwarf2 offset %u"), offset.sect_off);
22556 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
22557 return this_cu;
22558 }
22559 }
22560
22561 /* Initialize dwarf2_cu CU, owned by PER_CU. */
22562
22563 static void
22564 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
22565 {
22566 memset (cu, 0, sizeof (*cu));
22567 per_cu->cu = cu;
22568 cu->per_cu = per_cu;
22569 cu->objfile = per_cu->objfile;
22570 obstack_init (&cu->comp_unit_obstack);
22571 }
22572
22573 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
22574
22575 static void
22576 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
22577 enum language pretend_language)
22578 {
22579 struct attribute *attr;
22580
22581 /* Set the language we're debugging. */
22582 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
22583 if (attr)
22584 set_cu_language (DW_UNSND (attr), cu);
22585 else
22586 {
22587 cu->language = pretend_language;
22588 cu->language_defn = language_def (cu->language);
22589 }
22590
22591 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
22592 }
22593
22594 /* Release one cached compilation unit, CU. We unlink it from the tree
22595 of compilation units, but we don't remove it from the read_in_chain;
22596 the caller is responsible for that.
22597 NOTE: DATA is a void * because this function is also used as a
22598 cleanup routine. */
22599
22600 static void
22601 free_heap_comp_unit (void *data)
22602 {
22603 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
22604
22605 gdb_assert (cu->per_cu != NULL);
22606 cu->per_cu->cu = NULL;
22607 cu->per_cu = NULL;
22608
22609 obstack_free (&cu->comp_unit_obstack, NULL);
22610
22611 xfree (cu);
22612 }
22613
22614 /* This cleanup function is passed the address of a dwarf2_cu on the stack
22615 when we're finished with it. We can't free the pointer itself, but be
22616 sure to unlink it from the cache. Also release any associated storage. */
22617
22618 static void
22619 free_stack_comp_unit (void *data)
22620 {
22621 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
22622
22623 gdb_assert (cu->per_cu != NULL);
22624 cu->per_cu->cu = NULL;
22625 cu->per_cu = NULL;
22626
22627 obstack_free (&cu->comp_unit_obstack, NULL);
22628 cu->partial_dies = NULL;
22629 }
22630
22631 /* Free all cached compilation units. */
22632
22633 static void
22634 free_cached_comp_units (void *data)
22635 {
22636 struct dwarf2_per_cu_data *per_cu, **last_chain;
22637
22638 per_cu = dwarf2_per_objfile->read_in_chain;
22639 last_chain = &dwarf2_per_objfile->read_in_chain;
22640 while (per_cu != NULL)
22641 {
22642 struct dwarf2_per_cu_data *next_cu;
22643
22644 next_cu = per_cu->cu->read_in_chain;
22645
22646 free_heap_comp_unit (per_cu->cu);
22647 *last_chain = next_cu;
22648
22649 per_cu = next_cu;
22650 }
22651 }
22652
22653 /* Increase the age counter on each cached compilation unit, and free
22654 any that are too old. */
22655
22656 static void
22657 age_cached_comp_units (void)
22658 {
22659 struct dwarf2_per_cu_data *per_cu, **last_chain;
22660
22661 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
22662 per_cu = dwarf2_per_objfile->read_in_chain;
22663 while (per_cu != NULL)
22664 {
22665 per_cu->cu->last_used ++;
22666 if (per_cu->cu->last_used <= dwarf_max_cache_age)
22667 dwarf2_mark (per_cu->cu);
22668 per_cu = per_cu->cu->read_in_chain;
22669 }
22670
22671 per_cu = dwarf2_per_objfile->read_in_chain;
22672 last_chain = &dwarf2_per_objfile->read_in_chain;
22673 while (per_cu != NULL)
22674 {
22675 struct dwarf2_per_cu_data *next_cu;
22676
22677 next_cu = per_cu->cu->read_in_chain;
22678
22679 if (!per_cu->cu->mark)
22680 {
22681 free_heap_comp_unit (per_cu->cu);
22682 *last_chain = next_cu;
22683 }
22684 else
22685 last_chain = &per_cu->cu->read_in_chain;
22686
22687 per_cu = next_cu;
22688 }
22689 }
22690
22691 /* Remove a single compilation unit from the cache. */
22692
22693 static void
22694 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
22695 {
22696 struct dwarf2_per_cu_data *per_cu, **last_chain;
22697
22698 per_cu = dwarf2_per_objfile->read_in_chain;
22699 last_chain = &dwarf2_per_objfile->read_in_chain;
22700 while (per_cu != NULL)
22701 {
22702 struct dwarf2_per_cu_data *next_cu;
22703
22704 next_cu = per_cu->cu->read_in_chain;
22705
22706 if (per_cu == target_per_cu)
22707 {
22708 free_heap_comp_unit (per_cu->cu);
22709 per_cu->cu = NULL;
22710 *last_chain = next_cu;
22711 break;
22712 }
22713 else
22714 last_chain = &per_cu->cu->read_in_chain;
22715
22716 per_cu = next_cu;
22717 }
22718 }
22719
22720 /* Release all extra memory associated with OBJFILE. */
22721
22722 void
22723 dwarf2_free_objfile (struct objfile *objfile)
22724 {
22725 dwarf2_per_objfile
22726 = (struct dwarf2_per_objfile *) objfile_data (objfile,
22727 dwarf2_objfile_data_key);
22728
22729 if (dwarf2_per_objfile == NULL)
22730 return;
22731
22732 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
22733 free_cached_comp_units (NULL);
22734
22735 if (dwarf2_per_objfile->quick_file_names_table)
22736 htab_delete (dwarf2_per_objfile->quick_file_names_table);
22737
22738 if (dwarf2_per_objfile->line_header_hash)
22739 htab_delete (dwarf2_per_objfile->line_header_hash);
22740
22741 /* Everything else should be on the objfile obstack. */
22742 }
22743
22744 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
22745 We store these in a hash table separate from the DIEs, and preserve them
22746 when the DIEs are flushed out of cache.
22747
22748 The CU "per_cu" pointer is needed because offset alone is not enough to
22749 uniquely identify the type. A file may have multiple .debug_types sections,
22750 or the type may come from a DWO file. Furthermore, while it's more logical
22751 to use per_cu->section+offset, with Fission the section with the data is in
22752 the DWO file but we don't know that section at the point we need it.
22753 We have to use something in dwarf2_per_cu_data (or the pointer to it)
22754 because we can enter the lookup routine, get_die_type_at_offset, from
22755 outside this file, and thus won't necessarily have PER_CU->cu.
22756 Fortunately, PER_CU is stable for the life of the objfile. */
22757
22758 struct dwarf2_per_cu_offset_and_type
22759 {
22760 const struct dwarf2_per_cu_data *per_cu;
22761 sect_offset offset;
22762 struct type *type;
22763 };
22764
22765 /* Hash function for a dwarf2_per_cu_offset_and_type. */
22766
22767 static hashval_t
22768 per_cu_offset_and_type_hash (const void *item)
22769 {
22770 const struct dwarf2_per_cu_offset_and_type *ofs
22771 = (const struct dwarf2_per_cu_offset_and_type *) item;
22772
22773 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
22774 }
22775
22776 /* Equality function for a dwarf2_per_cu_offset_and_type. */
22777
22778 static int
22779 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
22780 {
22781 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
22782 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
22783 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
22784 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
22785
22786 return (ofs_lhs->per_cu == ofs_rhs->per_cu
22787 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
22788 }
22789
22790 /* Set the type associated with DIE to TYPE. Save it in CU's hash
22791 table if necessary. For convenience, return TYPE.
22792
22793 The DIEs reading must have careful ordering to:
22794 * Not cause infite loops trying to read in DIEs as a prerequisite for
22795 reading current DIE.
22796 * Not trying to dereference contents of still incompletely read in types
22797 while reading in other DIEs.
22798 * Enable referencing still incompletely read in types just by a pointer to
22799 the type without accessing its fields.
22800
22801 Therefore caller should follow these rules:
22802 * Try to fetch any prerequisite types we may need to build this DIE type
22803 before building the type and calling set_die_type.
22804 * After building type call set_die_type for current DIE as soon as
22805 possible before fetching more types to complete the current type.
22806 * Make the type as complete as possible before fetching more types. */
22807
22808 static struct type *
22809 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
22810 {
22811 struct dwarf2_per_cu_offset_and_type **slot, ofs;
22812 struct objfile *objfile = cu->objfile;
22813 struct attribute *attr;
22814 struct dynamic_prop prop;
22815
22816 /* For Ada types, make sure that the gnat-specific data is always
22817 initialized (if not already set). There are a few types where
22818 we should not be doing so, because the type-specific area is
22819 already used to hold some other piece of info (eg: TYPE_CODE_FLT
22820 where the type-specific area is used to store the floatformat).
22821 But this is not a problem, because the gnat-specific information
22822 is actually not needed for these types. */
22823 if (need_gnat_info (cu)
22824 && TYPE_CODE (type) != TYPE_CODE_FUNC
22825 && TYPE_CODE (type) != TYPE_CODE_FLT
22826 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
22827 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
22828 && TYPE_CODE (type) != TYPE_CODE_METHOD
22829 && !HAVE_GNAT_AUX_INFO (type))
22830 INIT_GNAT_SPECIFIC (type);
22831
22832 /* Read DW_AT_allocated and set in type. */
22833 attr = dwarf2_attr (die, DW_AT_allocated, cu);
22834 if (attr_form_is_block (attr))
22835 {
22836 if (attr_to_dynamic_prop (attr, die, cu, &prop))
22837 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type, objfile);
22838 }
22839 else if (attr != NULL)
22840 {
22841 complaint (&symfile_complaints,
22842 _("DW_AT_allocated has the wrong form (%s) at DIE 0x%x"),
22843 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
22844 die->offset.sect_off);
22845 }
22846
22847 /* Read DW_AT_associated and set in type. */
22848 attr = dwarf2_attr (die, DW_AT_associated, cu);
22849 if (attr_form_is_block (attr))
22850 {
22851 if (attr_to_dynamic_prop (attr, die, cu, &prop))
22852 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type, objfile);
22853 }
22854 else if (attr != NULL)
22855 {
22856 complaint (&symfile_complaints,
22857 _("DW_AT_associated has the wrong form (%s) at DIE 0x%x"),
22858 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
22859 die->offset.sect_off);
22860 }
22861
22862 /* Read DW_AT_data_location and set in type. */
22863 attr = dwarf2_attr (die, DW_AT_data_location, cu);
22864 if (attr_to_dynamic_prop (attr, die, cu, &prop))
22865 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type, objfile);
22866
22867 if (dwarf2_per_objfile->die_type_hash == NULL)
22868 {
22869 dwarf2_per_objfile->die_type_hash =
22870 htab_create_alloc_ex (127,
22871 per_cu_offset_and_type_hash,
22872 per_cu_offset_and_type_eq,
22873 NULL,
22874 &objfile->objfile_obstack,
22875 hashtab_obstack_allocate,
22876 dummy_obstack_deallocate);
22877 }
22878
22879 ofs.per_cu = cu->per_cu;
22880 ofs.offset = die->offset;
22881 ofs.type = type;
22882 slot = (struct dwarf2_per_cu_offset_and_type **)
22883 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
22884 if (*slot)
22885 complaint (&symfile_complaints,
22886 _("A problem internal to GDB: DIE 0x%x has type already set"),
22887 die->offset.sect_off);
22888 *slot = XOBNEW (&objfile->objfile_obstack,
22889 struct dwarf2_per_cu_offset_and_type);
22890 **slot = ofs;
22891 return type;
22892 }
22893
22894 /* Look up the type for the die at OFFSET in PER_CU in die_type_hash,
22895 or return NULL if the die does not have a saved type. */
22896
22897 static struct type *
22898 get_die_type_at_offset (sect_offset offset,
22899 struct dwarf2_per_cu_data *per_cu)
22900 {
22901 struct dwarf2_per_cu_offset_and_type *slot, ofs;
22902
22903 if (dwarf2_per_objfile->die_type_hash == NULL)
22904 return NULL;
22905
22906 ofs.per_cu = per_cu;
22907 ofs.offset = offset;
22908 slot = ((struct dwarf2_per_cu_offset_and_type *)
22909 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
22910 if (slot)
22911 return slot->type;
22912 else
22913 return NULL;
22914 }
22915
22916 /* Look up the type for DIE in CU in die_type_hash,
22917 or return NULL if DIE does not have a saved type. */
22918
22919 static struct type *
22920 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
22921 {
22922 return get_die_type_at_offset (die->offset, cu->per_cu);
22923 }
22924
22925 /* Add a dependence relationship from CU to REF_PER_CU. */
22926
22927 static void
22928 dwarf2_add_dependence (struct dwarf2_cu *cu,
22929 struct dwarf2_per_cu_data *ref_per_cu)
22930 {
22931 void **slot;
22932
22933 if (cu->dependencies == NULL)
22934 cu->dependencies
22935 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
22936 NULL, &cu->comp_unit_obstack,
22937 hashtab_obstack_allocate,
22938 dummy_obstack_deallocate);
22939
22940 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
22941 if (*slot == NULL)
22942 *slot = ref_per_cu;
22943 }
22944
22945 /* Subroutine of dwarf2_mark to pass to htab_traverse.
22946 Set the mark field in every compilation unit in the
22947 cache that we must keep because we are keeping CU. */
22948
22949 static int
22950 dwarf2_mark_helper (void **slot, void *data)
22951 {
22952 struct dwarf2_per_cu_data *per_cu;
22953
22954 per_cu = (struct dwarf2_per_cu_data *) *slot;
22955
22956 /* cu->dependencies references may not yet have been ever read if QUIT aborts
22957 reading of the chain. As such dependencies remain valid it is not much
22958 useful to track and undo them during QUIT cleanups. */
22959 if (per_cu->cu == NULL)
22960 return 1;
22961
22962 if (per_cu->cu->mark)
22963 return 1;
22964 per_cu->cu->mark = 1;
22965
22966 if (per_cu->cu->dependencies != NULL)
22967 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
22968
22969 return 1;
22970 }
22971
22972 /* Set the mark field in CU and in every other compilation unit in the
22973 cache that we must keep because we are keeping CU. */
22974
22975 static void
22976 dwarf2_mark (struct dwarf2_cu *cu)
22977 {
22978 if (cu->mark)
22979 return;
22980 cu->mark = 1;
22981 if (cu->dependencies != NULL)
22982 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
22983 }
22984
22985 static void
22986 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
22987 {
22988 while (per_cu)
22989 {
22990 per_cu->cu->mark = 0;
22991 per_cu = per_cu->cu->read_in_chain;
22992 }
22993 }
22994
22995 /* Trivial hash function for partial_die_info: the hash value of a DIE
22996 is its offset in .debug_info for this objfile. */
22997
22998 static hashval_t
22999 partial_die_hash (const void *item)
23000 {
23001 const struct partial_die_info *part_die
23002 = (const struct partial_die_info *) item;
23003
23004 return part_die->offset.sect_off;
23005 }
23006
23007 /* Trivial comparison function for partial_die_info structures: two DIEs
23008 are equal if they have the same offset. */
23009
23010 static int
23011 partial_die_eq (const void *item_lhs, const void *item_rhs)
23012 {
23013 const struct partial_die_info *part_die_lhs
23014 = (const struct partial_die_info *) item_lhs;
23015 const struct partial_die_info *part_die_rhs
23016 = (const struct partial_die_info *) item_rhs;
23017
23018 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
23019 }
23020
23021 static struct cmd_list_element *set_dwarf_cmdlist;
23022 static struct cmd_list_element *show_dwarf_cmdlist;
23023
23024 static void
23025 set_dwarf_cmd (char *args, int from_tty)
23026 {
23027 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
23028 gdb_stdout);
23029 }
23030
23031 static void
23032 show_dwarf_cmd (char *args, int from_tty)
23033 {
23034 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
23035 }
23036
23037 /* Free data associated with OBJFILE, if necessary. */
23038
23039 static void
23040 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
23041 {
23042 struct dwarf2_per_objfile *data = (struct dwarf2_per_objfile *) d;
23043 int ix;
23044
23045 /* Make sure we don't accidentally use dwarf2_per_objfile while
23046 cleaning up. */
23047 dwarf2_per_objfile = NULL;
23048
23049 for (ix = 0; ix < data->n_comp_units; ++ix)
23050 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
23051
23052 for (ix = 0; ix < data->n_type_units; ++ix)
23053 VEC_free (dwarf2_per_cu_ptr,
23054 data->all_type_units[ix]->per_cu.imported_symtabs);
23055 xfree (data->all_type_units);
23056
23057 VEC_free (dwarf2_section_info_def, data->types);
23058
23059 if (data->dwo_files)
23060 free_dwo_files (data->dwo_files, objfile);
23061 if (data->dwp_file)
23062 gdb_bfd_unref (data->dwp_file->dbfd);
23063
23064 if (data->dwz_file && data->dwz_file->dwz_bfd)
23065 gdb_bfd_unref (data->dwz_file->dwz_bfd);
23066 }
23067
23068 \f
23069 /* The "save gdb-index" command. */
23070
23071 /* The contents of the hash table we create when building the string
23072 table. */
23073 struct strtab_entry
23074 {
23075 offset_type offset;
23076 const char *str;
23077 };
23078
23079 /* Hash function for a strtab_entry.
23080
23081 Function is used only during write_hash_table so no index format backward
23082 compatibility is needed. */
23083
23084 static hashval_t
23085 hash_strtab_entry (const void *e)
23086 {
23087 const struct strtab_entry *entry = (const struct strtab_entry *) e;
23088 return mapped_index_string_hash (INT_MAX, entry->str);
23089 }
23090
23091 /* Equality function for a strtab_entry. */
23092
23093 static int
23094 eq_strtab_entry (const void *a, const void *b)
23095 {
23096 const struct strtab_entry *ea = (const struct strtab_entry *) a;
23097 const struct strtab_entry *eb = (const struct strtab_entry *) b;
23098 return !strcmp (ea->str, eb->str);
23099 }
23100
23101 /* Create a strtab_entry hash table. */
23102
23103 static htab_t
23104 create_strtab (void)
23105 {
23106 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
23107 xfree, xcalloc, xfree);
23108 }
23109
23110 /* Add a string to the constant pool. Return the string's offset in
23111 host order. */
23112
23113 static offset_type
23114 add_string (htab_t table, struct obstack *cpool, const char *str)
23115 {
23116 void **slot;
23117 struct strtab_entry entry;
23118 struct strtab_entry *result;
23119
23120 entry.str = str;
23121 slot = htab_find_slot (table, &entry, INSERT);
23122 if (*slot)
23123 result = (struct strtab_entry *) *slot;
23124 else
23125 {
23126 result = XNEW (struct strtab_entry);
23127 result->offset = obstack_object_size (cpool);
23128 result->str = str;
23129 obstack_grow_str0 (cpool, str);
23130 *slot = result;
23131 }
23132 return result->offset;
23133 }
23134
23135 /* An entry in the symbol table. */
23136 struct symtab_index_entry
23137 {
23138 /* The name of the symbol. */
23139 const char *name;
23140 /* The offset of the name in the constant pool. */
23141 offset_type index_offset;
23142 /* A sorted vector of the indices of all the CUs that hold an object
23143 of this name. */
23144 VEC (offset_type) *cu_indices;
23145 };
23146
23147 /* The symbol table. This is a power-of-2-sized hash table. */
23148 struct mapped_symtab
23149 {
23150 offset_type n_elements;
23151 offset_type size;
23152 struct symtab_index_entry **data;
23153 };
23154
23155 /* Hash function for a symtab_index_entry. */
23156
23157 static hashval_t
23158 hash_symtab_entry (const void *e)
23159 {
23160 const struct symtab_index_entry *entry
23161 = (const struct symtab_index_entry *) e;
23162 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
23163 sizeof (offset_type) * VEC_length (offset_type,
23164 entry->cu_indices),
23165 0);
23166 }
23167
23168 /* Equality function for a symtab_index_entry. */
23169
23170 static int
23171 eq_symtab_entry (const void *a, const void *b)
23172 {
23173 const struct symtab_index_entry *ea = (const struct symtab_index_entry *) a;
23174 const struct symtab_index_entry *eb = (const struct symtab_index_entry *) b;
23175 int len = VEC_length (offset_type, ea->cu_indices);
23176 if (len != VEC_length (offset_type, eb->cu_indices))
23177 return 0;
23178 return !memcmp (VEC_address (offset_type, ea->cu_indices),
23179 VEC_address (offset_type, eb->cu_indices),
23180 sizeof (offset_type) * len);
23181 }
23182
23183 /* Destroy a symtab_index_entry. */
23184
23185 static void
23186 delete_symtab_entry (void *p)
23187 {
23188 struct symtab_index_entry *entry = (struct symtab_index_entry *) p;
23189 VEC_free (offset_type, entry->cu_indices);
23190 xfree (entry);
23191 }
23192
23193 /* Create a hash table holding symtab_index_entry objects. */
23194
23195 static htab_t
23196 create_symbol_hash_table (void)
23197 {
23198 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
23199 delete_symtab_entry, xcalloc, xfree);
23200 }
23201
23202 /* Create a new mapped symtab object. */
23203
23204 static struct mapped_symtab *
23205 create_mapped_symtab (void)
23206 {
23207 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
23208 symtab->n_elements = 0;
23209 symtab->size = 1024;
23210 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
23211 return symtab;
23212 }
23213
23214 /* Destroy a mapped_symtab. */
23215
23216 static void
23217 cleanup_mapped_symtab (void *p)
23218 {
23219 struct mapped_symtab *symtab = (struct mapped_symtab *) p;
23220 /* The contents of the array are freed when the other hash table is
23221 destroyed. */
23222 xfree (symtab->data);
23223 xfree (symtab);
23224 }
23225
23226 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
23227 the slot.
23228
23229 Function is used only during write_hash_table so no index format backward
23230 compatibility is needed. */
23231
23232 static struct symtab_index_entry **
23233 find_slot (struct mapped_symtab *symtab, const char *name)
23234 {
23235 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
23236
23237 index = hash & (symtab->size - 1);
23238 step = ((hash * 17) & (symtab->size - 1)) | 1;
23239
23240 for (;;)
23241 {
23242 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
23243 return &symtab->data[index];
23244 index = (index + step) & (symtab->size - 1);
23245 }
23246 }
23247
23248 /* Expand SYMTAB's hash table. */
23249
23250 static void
23251 hash_expand (struct mapped_symtab *symtab)
23252 {
23253 offset_type old_size = symtab->size;
23254 offset_type i;
23255 struct symtab_index_entry **old_entries = symtab->data;
23256
23257 symtab->size *= 2;
23258 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
23259
23260 for (i = 0; i < old_size; ++i)
23261 {
23262 if (old_entries[i])
23263 {
23264 struct symtab_index_entry **slot = find_slot (symtab,
23265 old_entries[i]->name);
23266 *slot = old_entries[i];
23267 }
23268 }
23269
23270 xfree (old_entries);
23271 }
23272
23273 /* Add an entry to SYMTAB. NAME is the name of the symbol.
23274 CU_INDEX is the index of the CU in which the symbol appears.
23275 IS_STATIC is one if the symbol is static, otherwise zero (global). */
23276
23277 static void
23278 add_index_entry (struct mapped_symtab *symtab, const char *name,
23279 int is_static, gdb_index_symbol_kind kind,
23280 offset_type cu_index)
23281 {
23282 struct symtab_index_entry **slot;
23283 offset_type cu_index_and_attrs;
23284
23285 ++symtab->n_elements;
23286 if (4 * symtab->n_elements / 3 >= symtab->size)
23287 hash_expand (symtab);
23288
23289 slot = find_slot (symtab, name);
23290 if (!*slot)
23291 {
23292 *slot = XNEW (struct symtab_index_entry);
23293 (*slot)->name = name;
23294 /* index_offset is set later. */
23295 (*slot)->cu_indices = NULL;
23296 }
23297
23298 cu_index_and_attrs = 0;
23299 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
23300 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
23301 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
23302
23303 /* We don't want to record an index value twice as we want to avoid the
23304 duplication.
23305 We process all global symbols and then all static symbols
23306 (which would allow us to avoid the duplication by only having to check
23307 the last entry pushed), but a symbol could have multiple kinds in one CU.
23308 To keep things simple we don't worry about the duplication here and
23309 sort and uniqufy the list after we've processed all symbols. */
23310 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
23311 }
23312
23313 /* qsort helper routine for uniquify_cu_indices. */
23314
23315 static int
23316 offset_type_compare (const void *ap, const void *bp)
23317 {
23318 offset_type a = *(offset_type *) ap;
23319 offset_type b = *(offset_type *) bp;
23320
23321 return (a > b) - (b > a);
23322 }
23323
23324 /* Sort and remove duplicates of all symbols' cu_indices lists. */
23325
23326 static void
23327 uniquify_cu_indices (struct mapped_symtab *symtab)
23328 {
23329 int i;
23330
23331 for (i = 0; i < symtab->size; ++i)
23332 {
23333 struct symtab_index_entry *entry = symtab->data[i];
23334
23335 if (entry
23336 && entry->cu_indices != NULL)
23337 {
23338 unsigned int next_to_insert, next_to_check;
23339 offset_type last_value;
23340
23341 qsort (VEC_address (offset_type, entry->cu_indices),
23342 VEC_length (offset_type, entry->cu_indices),
23343 sizeof (offset_type), offset_type_compare);
23344
23345 last_value = VEC_index (offset_type, entry->cu_indices, 0);
23346 next_to_insert = 1;
23347 for (next_to_check = 1;
23348 next_to_check < VEC_length (offset_type, entry->cu_indices);
23349 ++next_to_check)
23350 {
23351 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
23352 != last_value)
23353 {
23354 last_value = VEC_index (offset_type, entry->cu_indices,
23355 next_to_check);
23356 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
23357 last_value);
23358 ++next_to_insert;
23359 }
23360 }
23361 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
23362 }
23363 }
23364 }
23365
23366 /* Add a vector of indices to the constant pool. */
23367
23368 static offset_type
23369 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
23370 struct symtab_index_entry *entry)
23371 {
23372 void **slot;
23373
23374 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
23375 if (!*slot)
23376 {
23377 offset_type len = VEC_length (offset_type, entry->cu_indices);
23378 offset_type val = MAYBE_SWAP (len);
23379 offset_type iter;
23380 int i;
23381
23382 *slot = entry;
23383 entry->index_offset = obstack_object_size (cpool);
23384
23385 obstack_grow (cpool, &val, sizeof (val));
23386 for (i = 0;
23387 VEC_iterate (offset_type, entry->cu_indices, i, iter);
23388 ++i)
23389 {
23390 val = MAYBE_SWAP (iter);
23391 obstack_grow (cpool, &val, sizeof (val));
23392 }
23393 }
23394 else
23395 {
23396 struct symtab_index_entry *old_entry
23397 = (struct symtab_index_entry *) *slot;
23398 entry->index_offset = old_entry->index_offset;
23399 entry = old_entry;
23400 }
23401 return entry->index_offset;
23402 }
23403
23404 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
23405 constant pool entries going into the obstack CPOOL. */
23406
23407 static void
23408 write_hash_table (struct mapped_symtab *symtab,
23409 struct obstack *output, struct obstack *cpool)
23410 {
23411 offset_type i;
23412 htab_t symbol_hash_table;
23413 htab_t str_table;
23414
23415 symbol_hash_table = create_symbol_hash_table ();
23416 str_table = create_strtab ();
23417
23418 /* We add all the index vectors to the constant pool first, to
23419 ensure alignment is ok. */
23420 for (i = 0; i < symtab->size; ++i)
23421 {
23422 if (symtab->data[i])
23423 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
23424 }
23425
23426 /* Now write out the hash table. */
23427 for (i = 0; i < symtab->size; ++i)
23428 {
23429 offset_type str_off, vec_off;
23430
23431 if (symtab->data[i])
23432 {
23433 str_off = add_string (str_table, cpool, symtab->data[i]->name);
23434 vec_off = symtab->data[i]->index_offset;
23435 }
23436 else
23437 {
23438 /* While 0 is a valid constant pool index, it is not valid
23439 to have 0 for both offsets. */
23440 str_off = 0;
23441 vec_off = 0;
23442 }
23443
23444 str_off = MAYBE_SWAP (str_off);
23445 vec_off = MAYBE_SWAP (vec_off);
23446
23447 obstack_grow (output, &str_off, sizeof (str_off));
23448 obstack_grow (output, &vec_off, sizeof (vec_off));
23449 }
23450
23451 htab_delete (str_table);
23452 htab_delete (symbol_hash_table);
23453 }
23454
23455 /* Struct to map psymtab to CU index in the index file. */
23456 struct psymtab_cu_index_map
23457 {
23458 struct partial_symtab *psymtab;
23459 unsigned int cu_index;
23460 };
23461
23462 static hashval_t
23463 hash_psymtab_cu_index (const void *item)
23464 {
23465 const struct psymtab_cu_index_map *map
23466 = (const struct psymtab_cu_index_map *) item;
23467
23468 return htab_hash_pointer (map->psymtab);
23469 }
23470
23471 static int
23472 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
23473 {
23474 const struct psymtab_cu_index_map *lhs
23475 = (const struct psymtab_cu_index_map *) item_lhs;
23476 const struct psymtab_cu_index_map *rhs
23477 = (const struct psymtab_cu_index_map *) item_rhs;
23478
23479 return lhs->psymtab == rhs->psymtab;
23480 }
23481
23482 /* Helper struct for building the address table. */
23483 struct addrmap_index_data
23484 {
23485 struct objfile *objfile;
23486 struct obstack *addr_obstack;
23487 htab_t cu_index_htab;
23488
23489 /* Non-zero if the previous_* fields are valid.
23490 We can't write an entry until we see the next entry (since it is only then
23491 that we know the end of the entry). */
23492 int previous_valid;
23493 /* Index of the CU in the table of all CUs in the index file. */
23494 unsigned int previous_cu_index;
23495 /* Start address of the CU. */
23496 CORE_ADDR previous_cu_start;
23497 };
23498
23499 /* Write an address entry to OBSTACK. */
23500
23501 static void
23502 add_address_entry (struct objfile *objfile, struct obstack *obstack,
23503 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
23504 {
23505 offset_type cu_index_to_write;
23506 gdb_byte addr[8];
23507 CORE_ADDR baseaddr;
23508
23509 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23510
23511 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
23512 obstack_grow (obstack, addr, 8);
23513 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
23514 obstack_grow (obstack, addr, 8);
23515 cu_index_to_write = MAYBE_SWAP (cu_index);
23516 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
23517 }
23518
23519 /* Worker function for traversing an addrmap to build the address table. */
23520
23521 static int
23522 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
23523 {
23524 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
23525 struct partial_symtab *pst = (struct partial_symtab *) obj;
23526
23527 if (data->previous_valid)
23528 add_address_entry (data->objfile, data->addr_obstack,
23529 data->previous_cu_start, start_addr,
23530 data->previous_cu_index);
23531
23532 data->previous_cu_start = start_addr;
23533 if (pst != NULL)
23534 {
23535 struct psymtab_cu_index_map find_map, *map;
23536 find_map.psymtab = pst;
23537 map = ((struct psymtab_cu_index_map *)
23538 htab_find (data->cu_index_htab, &find_map));
23539 gdb_assert (map != NULL);
23540 data->previous_cu_index = map->cu_index;
23541 data->previous_valid = 1;
23542 }
23543 else
23544 data->previous_valid = 0;
23545
23546 return 0;
23547 }
23548
23549 /* Write OBJFILE's address map to OBSTACK.
23550 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
23551 in the index file. */
23552
23553 static void
23554 write_address_map (struct objfile *objfile, struct obstack *obstack,
23555 htab_t cu_index_htab)
23556 {
23557 struct addrmap_index_data addrmap_index_data;
23558
23559 /* When writing the address table, we have to cope with the fact that
23560 the addrmap iterator only provides the start of a region; we have to
23561 wait until the next invocation to get the start of the next region. */
23562
23563 addrmap_index_data.objfile = objfile;
23564 addrmap_index_data.addr_obstack = obstack;
23565 addrmap_index_data.cu_index_htab = cu_index_htab;
23566 addrmap_index_data.previous_valid = 0;
23567
23568 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
23569 &addrmap_index_data);
23570
23571 /* It's highly unlikely the last entry (end address = 0xff...ff)
23572 is valid, but we should still handle it.
23573 The end address is recorded as the start of the next region, but that
23574 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
23575 anyway. */
23576 if (addrmap_index_data.previous_valid)
23577 add_address_entry (objfile, obstack,
23578 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
23579 addrmap_index_data.previous_cu_index);
23580 }
23581
23582 /* Return the symbol kind of PSYM. */
23583
23584 static gdb_index_symbol_kind
23585 symbol_kind (struct partial_symbol *psym)
23586 {
23587 domain_enum domain = PSYMBOL_DOMAIN (psym);
23588 enum address_class aclass = PSYMBOL_CLASS (psym);
23589
23590 switch (domain)
23591 {
23592 case VAR_DOMAIN:
23593 switch (aclass)
23594 {
23595 case LOC_BLOCK:
23596 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
23597 case LOC_TYPEDEF:
23598 return GDB_INDEX_SYMBOL_KIND_TYPE;
23599 case LOC_COMPUTED:
23600 case LOC_CONST_BYTES:
23601 case LOC_OPTIMIZED_OUT:
23602 case LOC_STATIC:
23603 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
23604 case LOC_CONST:
23605 /* Note: It's currently impossible to recognize psyms as enum values
23606 short of reading the type info. For now punt. */
23607 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
23608 default:
23609 /* There are other LOC_FOO values that one might want to classify
23610 as variables, but dwarf2read.c doesn't currently use them. */
23611 return GDB_INDEX_SYMBOL_KIND_OTHER;
23612 }
23613 case STRUCT_DOMAIN:
23614 return GDB_INDEX_SYMBOL_KIND_TYPE;
23615 default:
23616 return GDB_INDEX_SYMBOL_KIND_OTHER;
23617 }
23618 }
23619
23620 /* Add a list of partial symbols to SYMTAB. */
23621
23622 static void
23623 write_psymbols (struct mapped_symtab *symtab,
23624 htab_t psyms_seen,
23625 struct partial_symbol **psymp,
23626 int count,
23627 offset_type cu_index,
23628 int is_static)
23629 {
23630 for (; count-- > 0; ++psymp)
23631 {
23632 struct partial_symbol *psym = *psymp;
23633 void **slot;
23634
23635 if (SYMBOL_LANGUAGE (psym) == language_ada)
23636 error (_("Ada is not currently supported by the index"));
23637
23638 /* Only add a given psymbol once. */
23639 slot = htab_find_slot (psyms_seen, psym, INSERT);
23640 if (!*slot)
23641 {
23642 gdb_index_symbol_kind kind = symbol_kind (psym);
23643
23644 *slot = psym;
23645 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
23646 is_static, kind, cu_index);
23647 }
23648 }
23649 }
23650
23651 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
23652 exception if there is an error. */
23653
23654 static void
23655 write_obstack (FILE *file, struct obstack *obstack)
23656 {
23657 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
23658 file)
23659 != obstack_object_size (obstack))
23660 error (_("couldn't data write to file"));
23661 }
23662
23663 /* A helper struct used when iterating over debug_types. */
23664 struct signatured_type_index_data
23665 {
23666 struct objfile *objfile;
23667 struct mapped_symtab *symtab;
23668 struct obstack *types_list;
23669 htab_t psyms_seen;
23670 int cu_index;
23671 };
23672
23673 /* A helper function that writes a single signatured_type to an
23674 obstack. */
23675
23676 static int
23677 write_one_signatured_type (void **slot, void *d)
23678 {
23679 struct signatured_type_index_data *info
23680 = (struct signatured_type_index_data *) d;
23681 struct signatured_type *entry = (struct signatured_type *) *slot;
23682 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
23683 gdb_byte val[8];
23684
23685 write_psymbols (info->symtab,
23686 info->psyms_seen,
23687 info->objfile->global_psymbols.list
23688 + psymtab->globals_offset,
23689 psymtab->n_global_syms, info->cu_index,
23690 0);
23691 write_psymbols (info->symtab,
23692 info->psyms_seen,
23693 info->objfile->static_psymbols.list
23694 + psymtab->statics_offset,
23695 psymtab->n_static_syms, info->cu_index,
23696 1);
23697
23698 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
23699 entry->per_cu.offset.sect_off);
23700 obstack_grow (info->types_list, val, 8);
23701 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
23702 entry->type_offset_in_tu.cu_off);
23703 obstack_grow (info->types_list, val, 8);
23704 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
23705 obstack_grow (info->types_list, val, 8);
23706
23707 ++info->cu_index;
23708
23709 return 1;
23710 }
23711
23712 /* Recurse into all "included" dependencies and write their symbols as
23713 if they appeared in this psymtab. */
23714
23715 static void
23716 recursively_write_psymbols (struct objfile *objfile,
23717 struct partial_symtab *psymtab,
23718 struct mapped_symtab *symtab,
23719 htab_t psyms_seen,
23720 offset_type cu_index)
23721 {
23722 int i;
23723
23724 for (i = 0; i < psymtab->number_of_dependencies; ++i)
23725 if (psymtab->dependencies[i]->user != NULL)
23726 recursively_write_psymbols (objfile, psymtab->dependencies[i],
23727 symtab, psyms_seen, cu_index);
23728
23729 write_psymbols (symtab,
23730 psyms_seen,
23731 objfile->global_psymbols.list + psymtab->globals_offset,
23732 psymtab->n_global_syms, cu_index,
23733 0);
23734 write_psymbols (symtab,
23735 psyms_seen,
23736 objfile->static_psymbols.list + psymtab->statics_offset,
23737 psymtab->n_static_syms, cu_index,
23738 1);
23739 }
23740
23741 /* Create an index file for OBJFILE in the directory DIR. */
23742
23743 static void
23744 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
23745 {
23746 struct cleanup *cleanup;
23747 char *filename;
23748 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
23749 struct obstack cu_list, types_cu_list;
23750 int i;
23751 FILE *out_file;
23752 struct mapped_symtab *symtab;
23753 offset_type val, size_of_contents, total_len;
23754 struct stat st;
23755 struct psymtab_cu_index_map *psymtab_cu_index_map;
23756
23757 if (dwarf2_per_objfile->using_index)
23758 error (_("Cannot use an index to create the index"));
23759
23760 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
23761 error (_("Cannot make an index when the file has multiple .debug_types sections"));
23762
23763 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
23764 return;
23765
23766 if (stat (objfile_name (objfile), &st) < 0)
23767 perror_with_name (objfile_name (objfile));
23768
23769 filename = concat (dir, SLASH_STRING, lbasename (objfile_name (objfile)),
23770 INDEX_SUFFIX, (char *) NULL);
23771 cleanup = make_cleanup (xfree, filename);
23772
23773 out_file = gdb_fopen_cloexec (filename, "wb");
23774 if (!out_file)
23775 error (_("Can't open `%s' for writing"), filename);
23776
23777 gdb::unlinker unlink_file (filename);
23778
23779 symtab = create_mapped_symtab ();
23780 make_cleanup (cleanup_mapped_symtab, symtab);
23781
23782 obstack_init (&addr_obstack);
23783 make_cleanup_obstack_free (&addr_obstack);
23784
23785 obstack_init (&cu_list);
23786 make_cleanup_obstack_free (&cu_list);
23787
23788 obstack_init (&types_cu_list);
23789 make_cleanup_obstack_free (&types_cu_list);
23790
23791 htab_up psyms_seen (htab_create_alloc (100, htab_hash_pointer,
23792 htab_eq_pointer,
23793 NULL, xcalloc, xfree));
23794
23795 /* While we're scanning CU's create a table that maps a psymtab pointer
23796 (which is what addrmap records) to its index (which is what is recorded
23797 in the index file). This will later be needed to write the address
23798 table. */
23799 htab_up cu_index_htab (htab_create_alloc (100,
23800 hash_psymtab_cu_index,
23801 eq_psymtab_cu_index,
23802 NULL, xcalloc, xfree));
23803 psymtab_cu_index_map = XNEWVEC (struct psymtab_cu_index_map,
23804 dwarf2_per_objfile->n_comp_units);
23805 make_cleanup (xfree, psymtab_cu_index_map);
23806
23807 /* The CU list is already sorted, so we don't need to do additional
23808 work here. Also, the debug_types entries do not appear in
23809 all_comp_units, but only in their own hash table. */
23810 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
23811 {
23812 struct dwarf2_per_cu_data *per_cu
23813 = dwarf2_per_objfile->all_comp_units[i];
23814 struct partial_symtab *psymtab = per_cu->v.psymtab;
23815 gdb_byte val[8];
23816 struct psymtab_cu_index_map *map;
23817 void **slot;
23818
23819 /* CU of a shared file from 'dwz -m' may be unused by this main file.
23820 It may be referenced from a local scope but in such case it does not
23821 need to be present in .gdb_index. */
23822 if (psymtab == NULL)
23823 continue;
23824
23825 if (psymtab->user == NULL)
23826 recursively_write_psymbols (objfile, psymtab, symtab,
23827 psyms_seen.get (), i);
23828
23829 map = &psymtab_cu_index_map[i];
23830 map->psymtab = psymtab;
23831 map->cu_index = i;
23832 slot = htab_find_slot (cu_index_htab.get (), map, INSERT);
23833 gdb_assert (slot != NULL);
23834 gdb_assert (*slot == NULL);
23835 *slot = map;
23836
23837 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
23838 per_cu->offset.sect_off);
23839 obstack_grow (&cu_list, val, 8);
23840 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
23841 obstack_grow (&cu_list, val, 8);
23842 }
23843
23844 /* Dump the address map. */
23845 write_address_map (objfile, &addr_obstack, cu_index_htab.get ());
23846
23847 /* Write out the .debug_type entries, if any. */
23848 if (dwarf2_per_objfile->signatured_types)
23849 {
23850 struct signatured_type_index_data sig_data;
23851
23852 sig_data.objfile = objfile;
23853 sig_data.symtab = symtab;
23854 sig_data.types_list = &types_cu_list;
23855 sig_data.psyms_seen = psyms_seen.get ();
23856 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
23857 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
23858 write_one_signatured_type, &sig_data);
23859 }
23860
23861 /* Now that we've processed all symbols we can shrink their cu_indices
23862 lists. */
23863 uniquify_cu_indices (symtab);
23864
23865 obstack_init (&constant_pool);
23866 make_cleanup_obstack_free (&constant_pool);
23867 obstack_init (&symtab_obstack);
23868 make_cleanup_obstack_free (&symtab_obstack);
23869 write_hash_table (symtab, &symtab_obstack, &constant_pool);
23870
23871 obstack_init (&contents);
23872 make_cleanup_obstack_free (&contents);
23873 size_of_contents = 6 * sizeof (offset_type);
23874 total_len = size_of_contents;
23875
23876 /* The version number. */
23877 val = MAYBE_SWAP (8);
23878 obstack_grow (&contents, &val, sizeof (val));
23879
23880 /* The offset of the CU list from the start of the file. */
23881 val = MAYBE_SWAP (total_len);
23882 obstack_grow (&contents, &val, sizeof (val));
23883 total_len += obstack_object_size (&cu_list);
23884
23885 /* The offset of the types CU list from the start of the file. */
23886 val = MAYBE_SWAP (total_len);
23887 obstack_grow (&contents, &val, sizeof (val));
23888 total_len += obstack_object_size (&types_cu_list);
23889
23890 /* The offset of the address table from the start of the file. */
23891 val = MAYBE_SWAP (total_len);
23892 obstack_grow (&contents, &val, sizeof (val));
23893 total_len += obstack_object_size (&addr_obstack);
23894
23895 /* The offset of the symbol table from the start of the file. */
23896 val = MAYBE_SWAP (total_len);
23897 obstack_grow (&contents, &val, sizeof (val));
23898 total_len += obstack_object_size (&symtab_obstack);
23899
23900 /* The offset of the constant pool from the start of the file. */
23901 val = MAYBE_SWAP (total_len);
23902 obstack_grow (&contents, &val, sizeof (val));
23903 total_len += obstack_object_size (&constant_pool);
23904
23905 gdb_assert (obstack_object_size (&contents) == size_of_contents);
23906
23907 write_obstack (out_file, &contents);
23908 write_obstack (out_file, &cu_list);
23909 write_obstack (out_file, &types_cu_list);
23910 write_obstack (out_file, &addr_obstack);
23911 write_obstack (out_file, &symtab_obstack);
23912 write_obstack (out_file, &constant_pool);
23913
23914 fclose (out_file);
23915
23916 /* We want to keep the file. */
23917 unlink_file.keep ();
23918
23919 do_cleanups (cleanup);
23920 }
23921
23922 /* Implementation of the `save gdb-index' command.
23923
23924 Note that the file format used by this command is documented in the
23925 GDB manual. Any changes here must be documented there. */
23926
23927 static void
23928 save_gdb_index_command (char *arg, int from_tty)
23929 {
23930 struct objfile *objfile;
23931
23932 if (!arg || !*arg)
23933 error (_("usage: save gdb-index DIRECTORY"));
23934
23935 ALL_OBJFILES (objfile)
23936 {
23937 struct stat st;
23938
23939 /* If the objfile does not correspond to an actual file, skip it. */
23940 if (stat (objfile_name (objfile), &st) < 0)
23941 continue;
23942
23943 dwarf2_per_objfile
23944 = (struct dwarf2_per_objfile *) objfile_data (objfile,
23945 dwarf2_objfile_data_key);
23946 if (dwarf2_per_objfile)
23947 {
23948
23949 TRY
23950 {
23951 write_psymtabs_to_index (objfile, arg);
23952 }
23953 CATCH (except, RETURN_MASK_ERROR)
23954 {
23955 exception_fprintf (gdb_stderr, except,
23956 _("Error while writing index for `%s': "),
23957 objfile_name (objfile));
23958 }
23959 END_CATCH
23960 }
23961 }
23962 }
23963
23964 \f
23965
23966 int dwarf_always_disassemble;
23967
23968 static void
23969 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
23970 struct cmd_list_element *c, const char *value)
23971 {
23972 fprintf_filtered (file,
23973 _("Whether to always disassemble "
23974 "DWARF expressions is %s.\n"),
23975 value);
23976 }
23977
23978 static void
23979 show_check_physname (struct ui_file *file, int from_tty,
23980 struct cmd_list_element *c, const char *value)
23981 {
23982 fprintf_filtered (file,
23983 _("Whether to check \"physname\" is %s.\n"),
23984 value);
23985 }
23986
23987 void _initialize_dwarf2_read (void);
23988
23989 void
23990 _initialize_dwarf2_read (void)
23991 {
23992 struct cmd_list_element *c;
23993
23994 dwarf2_objfile_data_key
23995 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
23996
23997 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
23998 Set DWARF specific variables.\n\
23999 Configure DWARF variables such as the cache size"),
24000 &set_dwarf_cmdlist, "maintenance set dwarf ",
24001 0/*allow-unknown*/, &maintenance_set_cmdlist);
24002
24003 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
24004 Show DWARF specific variables\n\
24005 Show DWARF variables such as the cache size"),
24006 &show_dwarf_cmdlist, "maintenance show dwarf ",
24007 0/*allow-unknown*/, &maintenance_show_cmdlist);
24008
24009 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24010 &dwarf_max_cache_age, _("\
24011 Set the upper bound on the age of cached DWARF compilation units."), _("\
24012 Show the upper bound on the age of cached DWARF compilation units."), _("\
24013 A higher limit means that cached compilation units will be stored\n\
24014 in memory longer, and more total memory will be used. Zero disables\n\
24015 caching, which can slow down startup."),
24016 NULL,
24017 show_dwarf_max_cache_age,
24018 &set_dwarf_cmdlist,
24019 &show_dwarf_cmdlist);
24020
24021 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
24022 &dwarf_always_disassemble, _("\
24023 Set whether `info address' always disassembles DWARF expressions."), _("\
24024 Show whether `info address' always disassembles DWARF expressions."), _("\
24025 When enabled, DWARF expressions are always printed in an assembly-like\n\
24026 syntax. When disabled, expressions will be printed in a more\n\
24027 conversational style, when possible."),
24028 NULL,
24029 show_dwarf_always_disassemble,
24030 &set_dwarf_cmdlist,
24031 &show_dwarf_cmdlist);
24032
24033 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24034 Set debugging of the DWARF reader."), _("\
24035 Show debugging of the DWARF reader."), _("\
24036 When enabled (non-zero), debugging messages are printed during DWARF\n\
24037 reading and symtab expansion. A value of 1 (one) provides basic\n\
24038 information. A value greater than 1 provides more verbose information."),
24039 NULL,
24040 NULL,
24041 &setdebuglist, &showdebuglist);
24042
24043 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24044 Set debugging of the DWARF DIE reader."), _("\
24045 Show debugging of the DWARF DIE reader."), _("\
24046 When enabled (non-zero), DIEs are dumped after they are read in.\n\
24047 The value is the maximum depth to print."),
24048 NULL,
24049 NULL,
24050 &setdebuglist, &showdebuglist);
24051
24052 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24053 Set debugging of the dwarf line reader."), _("\
24054 Show debugging of the dwarf line reader."), _("\
24055 When enabled (non-zero), line number entries are dumped as they are read in.\n\
24056 A value of 1 (one) provides basic information.\n\
24057 A value greater than 1 provides more verbose information."),
24058 NULL,
24059 NULL,
24060 &setdebuglist, &showdebuglist);
24061
24062 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24063 Set cross-checking of \"physname\" code against demangler."), _("\
24064 Show cross-checking of \"physname\" code against demangler."), _("\
24065 When enabled, GDB's internal \"physname\" code is checked against\n\
24066 the demangler."),
24067 NULL, show_check_physname,
24068 &setdebuglist, &showdebuglist);
24069
24070 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24071 no_class, &use_deprecated_index_sections, _("\
24072 Set whether to use deprecated gdb_index sections."), _("\
24073 Show whether to use deprecated gdb_index sections."), _("\
24074 When enabled, deprecated .gdb_index sections are used anyway.\n\
24075 Normally they are ignored either because of a missing feature or\n\
24076 performance issue.\n\
24077 Warning: This option must be enabled before gdb reads the file."),
24078 NULL,
24079 NULL,
24080 &setlist, &showlist);
24081
24082 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
24083 _("\
24084 Save a gdb-index file.\n\
24085 Usage: save gdb-index DIRECTORY"),
24086 &save_cmdlist);
24087 set_cmd_completer (c, filename_completer);
24088
24089 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24090 &dwarf2_locexpr_funcs);
24091 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24092 &dwarf2_loclist_funcs);
24093
24094 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24095 &dwarf2_block_frame_base_locexpr_funcs);
24096 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24097 &dwarf2_block_frame_base_loclist_funcs);
24098 }
This page took 0.511366 seconds and 5 git commands to generate.