dwarf2read: Replace copy_string usages with savestring
[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 #include "common/function-view.h"
74 #include "common/gdb_optional.h"
75 #include "common/underlying.h"
76 #include "common/byte-vector.h"
77 #include "filename-seen-cache.h"
78 #include <fcntl.h>
79 #include <sys/types.h>
80 #include <algorithm>
81 #include <unordered_set>
82 #include <unordered_map>
83
84 typedef struct symbol *symbolp;
85 DEF_VEC_P (symbolp);
86
87 /* When == 1, print basic high level tracing messages.
88 When > 1, be more verbose.
89 This is in contrast to the low level DIE reading of dwarf_die_debug. */
90 static unsigned int dwarf_read_debug = 0;
91
92 /* When non-zero, dump DIEs after they are read in. */
93 static unsigned int dwarf_die_debug = 0;
94
95 /* When non-zero, dump line number entries as they are read in. */
96 static unsigned int dwarf_line_debug = 0;
97
98 /* When non-zero, cross-check physname against demangler. */
99 static int check_physname = 0;
100
101 /* When non-zero, do not reject deprecated .gdb_index sections. */
102 static int use_deprecated_index_sections = 0;
103
104 static const struct objfile_data *dwarf2_objfile_data_key;
105
106 /* The "aclass" indices for various kinds of computed DWARF symbols. */
107
108 static int dwarf2_locexpr_index;
109 static int dwarf2_loclist_index;
110 static int dwarf2_locexpr_block_index;
111 static int dwarf2_loclist_block_index;
112
113 /* A descriptor for dwarf sections.
114
115 S.ASECTION, SIZE are typically initialized when the objfile is first
116 scanned. BUFFER, READIN are filled in later when the section is read.
117 If the section contained compressed data then SIZE is updated to record
118 the uncompressed size of the section.
119
120 DWP file format V2 introduces a wrinkle that is easiest to handle by
121 creating the concept of virtual sections contained within a real section.
122 In DWP V2 the sections of the input DWO files are concatenated together
123 into one section, but section offsets are kept relative to the original
124 input section.
125 If this is a virtual dwp-v2 section, S.CONTAINING_SECTION is a backlink to
126 the real section this "virtual" section is contained in, and BUFFER,SIZE
127 describe the virtual section. */
128
129 struct dwarf2_section_info
130 {
131 union
132 {
133 /* If this is a real section, the bfd section. */
134 asection *section;
135 /* If this is a virtual section, pointer to the containing ("real")
136 section. */
137 struct dwarf2_section_info *containing_section;
138 } s;
139 /* Pointer to section data, only valid if readin. */
140 const gdb_byte *buffer;
141 /* The size of the section, real or virtual. */
142 bfd_size_type size;
143 /* If this is a virtual section, the offset in the real section.
144 Only valid if is_virtual. */
145 bfd_size_type virtual_offset;
146 /* True if we have tried to read this section. */
147 char readin;
148 /* True if this is a virtual section, False otherwise.
149 This specifies which of s.section and s.containing_section to use. */
150 char is_virtual;
151 };
152
153 typedef struct dwarf2_section_info dwarf2_section_info_def;
154 DEF_VEC_O (dwarf2_section_info_def);
155
156 /* All offsets in the index are of this type. It must be
157 architecture-independent. */
158 typedef uint32_t offset_type;
159
160 DEF_VEC_I (offset_type);
161
162 /* Ensure only legit values are used. */
163 #define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
164 do { \
165 gdb_assert ((unsigned int) (value) <= 1); \
166 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
167 } while (0)
168
169 /* Ensure only legit values are used. */
170 #define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
171 do { \
172 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
173 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
174 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
175 } while (0)
176
177 /* Ensure we don't use more than the alloted nuber of bits for the CU. */
178 #define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
179 do { \
180 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
181 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
182 } while (0)
183
184 /* A description of the mapped index. The file format is described in
185 a comment by the code that writes the index. */
186 struct mapped_index
187 {
188 /* Index data format version. */
189 int version;
190
191 /* The total length of the buffer. */
192 off_t total_size;
193
194 /* A pointer to the address table data. */
195 const gdb_byte *address_table;
196
197 /* Size of the address table data in bytes. */
198 offset_type address_table_size;
199
200 /* The symbol table, implemented as a hash table. */
201 const offset_type *symbol_table;
202
203 /* Size in slots, each slot is 2 offset_types. */
204 offset_type symbol_table_slots;
205
206 /* A pointer to the constant pool. */
207 const char *constant_pool;
208 };
209
210 typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
211 DEF_VEC_P (dwarf2_per_cu_ptr);
212
213 struct tu_stats
214 {
215 int nr_uniq_abbrev_tables;
216 int nr_symtabs;
217 int nr_symtab_sharers;
218 int nr_stmt_less_type_units;
219 int nr_all_type_units_reallocs;
220 };
221
222 /* Collection of data recorded per objfile.
223 This hangs off of dwarf2_objfile_data_key. */
224
225 struct dwarf2_per_objfile
226 {
227 /* Construct a dwarf2_per_objfile for OBJFILE. NAMES points to the
228 dwarf2 section names, or is NULL if the standard ELF names are
229 used. */
230 dwarf2_per_objfile (struct objfile *objfile,
231 const dwarf2_debug_sections *names);
232
233 ~dwarf2_per_objfile ();
234
235 /* Disable copy. */
236 dwarf2_per_objfile (const dwarf2_per_objfile &) = delete;
237 void operator= (const dwarf2_per_objfile &) = delete;
238
239 /* Free all cached compilation units. */
240 void free_cached_comp_units ();
241 private:
242 /* This function is mapped across the sections and remembers the
243 offset and size of each of the debugging sections we are
244 interested in. */
245 void locate_sections (bfd *abfd, asection *sectp,
246 const dwarf2_debug_sections &names);
247
248 public:
249 dwarf2_section_info info {};
250 dwarf2_section_info abbrev {};
251 dwarf2_section_info line {};
252 dwarf2_section_info loc {};
253 dwarf2_section_info loclists {};
254 dwarf2_section_info macinfo {};
255 dwarf2_section_info macro {};
256 dwarf2_section_info str {};
257 dwarf2_section_info line_str {};
258 dwarf2_section_info ranges {};
259 dwarf2_section_info rnglists {};
260 dwarf2_section_info addr {};
261 dwarf2_section_info frame {};
262 dwarf2_section_info eh_frame {};
263 dwarf2_section_info gdb_index {};
264
265 VEC (dwarf2_section_info_def) *types = NULL;
266
267 /* Back link. */
268 struct objfile *objfile = NULL;
269
270 /* Table of all the compilation units. This is used to locate
271 the target compilation unit of a particular reference. */
272 struct dwarf2_per_cu_data **all_comp_units = NULL;
273
274 /* The number of compilation units in ALL_COMP_UNITS. */
275 int n_comp_units = 0;
276
277 /* The number of .debug_types-related CUs. */
278 int n_type_units = 0;
279
280 /* The number of elements allocated in all_type_units.
281 If there are skeleton-less TUs, we add them to all_type_units lazily. */
282 int n_allocated_type_units = 0;
283
284 /* The .debug_types-related CUs (TUs).
285 This is stored in malloc space because we may realloc it. */
286 struct signatured_type **all_type_units = NULL;
287
288 /* Table of struct type_unit_group objects.
289 The hash key is the DW_AT_stmt_list value. */
290 htab_t type_unit_groups {};
291
292 /* A table mapping .debug_types signatures to its signatured_type entry.
293 This is NULL if the .debug_types section hasn't been read in yet. */
294 htab_t signatured_types {};
295
296 /* Type unit statistics, to see how well the scaling improvements
297 are doing. */
298 struct tu_stats tu_stats {};
299
300 /* A chain of compilation units that are currently read in, so that
301 they can be freed later. */
302 dwarf2_per_cu_data *read_in_chain = NULL;
303
304 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
305 This is NULL if the table hasn't been allocated yet. */
306 htab_t dwo_files {};
307
308 /* True if we've checked for whether there is a DWP file. */
309 bool dwp_checked = false;
310
311 /* The DWP file if there is one, or NULL. */
312 struct dwp_file *dwp_file = NULL;
313
314 /* The shared '.dwz' file, if one exists. This is used when the
315 original data was compressed using 'dwz -m'. */
316 struct dwz_file *dwz_file = NULL;
317
318 /* A flag indicating whether this objfile has a section loaded at a
319 VMA of 0. */
320 bool has_section_at_zero = false;
321
322 /* True if we are using the mapped index,
323 or we are faking it for OBJF_READNOW's sake. */
324 bool using_index = false;
325
326 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
327 mapped_index *index_table = NULL;
328
329 /* When using index_table, this keeps track of all quick_file_names entries.
330 TUs typically share line table entries with a CU, so we maintain a
331 separate table of all line table entries to support the sharing.
332 Note that while there can be way more TUs than CUs, we've already
333 sorted all the TUs into "type unit groups", grouped by their
334 DW_AT_stmt_list value. Therefore the only sharing done here is with a
335 CU and its associated TU group if there is one. */
336 htab_t quick_file_names_table {};
337
338 /* Set during partial symbol reading, to prevent queueing of full
339 symbols. */
340 bool reading_partial_symbols = false;
341
342 /* Table mapping type DIEs to their struct type *.
343 This is NULL if not allocated yet.
344 The mapping is done via (CU/TU + DIE offset) -> type. */
345 htab_t die_type_hash {};
346
347 /* The CUs we recently read. */
348 VEC (dwarf2_per_cu_ptr) *just_read_cus = NULL;
349
350 /* Table containing line_header indexed by offset and offset_in_dwz. */
351 htab_t line_header_hash {};
352
353 /* Table containing all filenames. This is an optional because the
354 table is lazily constructed on first access. */
355 gdb::optional<filename_seen_cache> filenames_cache;
356 };
357
358 static struct dwarf2_per_objfile *dwarf2_per_objfile;
359
360 /* Default names of the debugging sections. */
361
362 /* Note that if the debugging section has been compressed, it might
363 have a name like .zdebug_info. */
364
365 static const struct dwarf2_debug_sections dwarf2_elf_names =
366 {
367 { ".debug_info", ".zdebug_info" },
368 { ".debug_abbrev", ".zdebug_abbrev" },
369 { ".debug_line", ".zdebug_line" },
370 { ".debug_loc", ".zdebug_loc" },
371 { ".debug_loclists", ".zdebug_loclists" },
372 { ".debug_macinfo", ".zdebug_macinfo" },
373 { ".debug_macro", ".zdebug_macro" },
374 { ".debug_str", ".zdebug_str" },
375 { ".debug_line_str", ".zdebug_line_str" },
376 { ".debug_ranges", ".zdebug_ranges" },
377 { ".debug_rnglists", ".zdebug_rnglists" },
378 { ".debug_types", ".zdebug_types" },
379 { ".debug_addr", ".zdebug_addr" },
380 { ".debug_frame", ".zdebug_frame" },
381 { ".eh_frame", NULL },
382 { ".gdb_index", ".zgdb_index" },
383 23
384 };
385
386 /* List of DWO/DWP sections. */
387
388 static const struct dwop_section_names
389 {
390 struct dwarf2_section_names abbrev_dwo;
391 struct dwarf2_section_names info_dwo;
392 struct dwarf2_section_names line_dwo;
393 struct dwarf2_section_names loc_dwo;
394 struct dwarf2_section_names loclists_dwo;
395 struct dwarf2_section_names macinfo_dwo;
396 struct dwarf2_section_names macro_dwo;
397 struct dwarf2_section_names str_dwo;
398 struct dwarf2_section_names str_offsets_dwo;
399 struct dwarf2_section_names types_dwo;
400 struct dwarf2_section_names cu_index;
401 struct dwarf2_section_names tu_index;
402 }
403 dwop_section_names =
404 {
405 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
406 { ".debug_info.dwo", ".zdebug_info.dwo" },
407 { ".debug_line.dwo", ".zdebug_line.dwo" },
408 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
409 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
410 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
411 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
412 { ".debug_str.dwo", ".zdebug_str.dwo" },
413 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
414 { ".debug_types.dwo", ".zdebug_types.dwo" },
415 { ".debug_cu_index", ".zdebug_cu_index" },
416 { ".debug_tu_index", ".zdebug_tu_index" },
417 };
418
419 /* local data types */
420
421 /* The data in a compilation unit header, after target2host
422 translation, looks like this. */
423 struct comp_unit_head
424 {
425 unsigned int length;
426 short version;
427 unsigned char addr_size;
428 unsigned char signed_addr_p;
429 sect_offset abbrev_sect_off;
430
431 /* Size of file offsets; either 4 or 8. */
432 unsigned int offset_size;
433
434 /* Size of the length field; either 4 or 12. */
435 unsigned int initial_length_size;
436
437 enum dwarf_unit_type unit_type;
438
439 /* Offset to the first byte of this compilation unit header in the
440 .debug_info section, for resolving relative reference dies. */
441 sect_offset sect_off;
442
443 /* Offset to first die in this cu from the start of the cu.
444 This will be the first byte following the compilation unit header. */
445 cu_offset first_die_cu_offset;
446
447 /* 64-bit signature of this type unit - it is valid only for
448 UNIT_TYPE DW_UT_type. */
449 ULONGEST signature;
450
451 /* For types, offset in the type's DIE of the type defined by this TU. */
452 cu_offset type_cu_offset_in_tu;
453 };
454
455 /* Type used for delaying computation of method physnames.
456 See comments for compute_delayed_physnames. */
457 struct delayed_method_info
458 {
459 /* The type to which the method is attached, i.e., its parent class. */
460 struct type *type;
461
462 /* The index of the method in the type's function fieldlists. */
463 int fnfield_index;
464
465 /* The index of the method in the fieldlist. */
466 int index;
467
468 /* The name of the DIE. */
469 const char *name;
470
471 /* The DIE associated with this method. */
472 struct die_info *die;
473 };
474
475 typedef struct delayed_method_info delayed_method_info;
476 DEF_VEC_O (delayed_method_info);
477
478 /* Internal state when decoding a particular compilation unit. */
479 struct dwarf2_cu
480 {
481 /* The objfile containing this compilation unit. */
482 struct objfile *objfile;
483
484 /* The header of the compilation unit. */
485 struct comp_unit_head header;
486
487 /* Base address of this compilation unit. */
488 CORE_ADDR base_address;
489
490 /* Non-zero if base_address has been set. */
491 int base_known;
492
493 /* The language we are debugging. */
494 enum language language;
495 const struct language_defn *language_defn;
496
497 const char *producer;
498
499 /* The generic symbol table building routines have separate lists for
500 file scope symbols and all all other scopes (local scopes). So
501 we need to select the right one to pass to add_symbol_to_list().
502 We do it by keeping a pointer to the correct list in list_in_scope.
503
504 FIXME: The original dwarf code just treated the file scope as the
505 first local scope, and all other local scopes as nested local
506 scopes, and worked fine. Check to see if we really need to
507 distinguish these in buildsym.c. */
508 struct pending **list_in_scope;
509
510 /* The abbrev table for this CU.
511 Normally this points to the abbrev table in the objfile.
512 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
513 struct abbrev_table *abbrev_table;
514
515 /* Hash table holding all the loaded partial DIEs
516 with partial_die->offset.SECT_OFF as hash. */
517 htab_t partial_dies;
518
519 /* Storage for things with the same lifetime as this read-in compilation
520 unit, including partial DIEs. */
521 struct obstack comp_unit_obstack;
522
523 /* When multiple dwarf2_cu structures are living in memory, this field
524 chains them all together, so that they can be released efficiently.
525 We will probably also want a generation counter so that most-recently-used
526 compilation units are cached... */
527 struct dwarf2_per_cu_data *read_in_chain;
528
529 /* Backlink to our per_cu entry. */
530 struct dwarf2_per_cu_data *per_cu;
531
532 /* How many compilation units ago was this CU last referenced? */
533 int last_used;
534
535 /* A hash table of DIE cu_offset for following references with
536 die_info->offset.sect_off as hash. */
537 htab_t die_hash;
538
539 /* Full DIEs if read in. */
540 struct die_info *dies;
541
542 /* A set of pointers to dwarf2_per_cu_data objects for compilation
543 units referenced by this one. Only set during full symbol processing;
544 partial symbol tables do not have dependencies. */
545 htab_t dependencies;
546
547 /* Header data from the line table, during full symbol processing. */
548 struct line_header *line_header;
549 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
550 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
551 this is the DW_TAG_compile_unit die for this CU. We'll hold on
552 to the line header as long as this DIE is being processed. See
553 process_die_scope. */
554 die_info *line_header_die_owner;
555
556 /* A list of methods which need to have physnames computed
557 after all type information has been read. */
558 VEC (delayed_method_info) *method_list;
559
560 /* To be copied to symtab->call_site_htab. */
561 htab_t call_site_htab;
562
563 /* Non-NULL if this CU came from a DWO file.
564 There is an invariant here that is important to remember:
565 Except for attributes copied from the top level DIE in the "main"
566 (or "stub") file in preparation for reading the DWO file
567 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
568 Either there isn't a DWO file (in which case this is NULL and the point
569 is moot), or there is and either we're not going to read it (in which
570 case this is NULL) or there is and we are reading it (in which case this
571 is non-NULL). */
572 struct dwo_unit *dwo_unit;
573
574 /* The DW_AT_addr_base attribute if present, zero otherwise
575 (zero is a valid value though).
576 Note this value comes from the Fission stub CU/TU's DIE. */
577 ULONGEST addr_base;
578
579 /* The DW_AT_ranges_base attribute if present, zero otherwise
580 (zero is a valid value though).
581 Note this value comes from the Fission stub CU/TU's DIE.
582 Also note that the value is zero in the non-DWO case so this value can
583 be used without needing to know whether DWO files are in use or not.
584 N.B. This does not apply to DW_AT_ranges appearing in
585 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
586 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
587 DW_AT_ranges_base *would* have to be applied, and we'd have to care
588 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
589 ULONGEST ranges_base;
590
591 /* Mark used when releasing cached dies. */
592 unsigned int mark : 1;
593
594 /* This CU references .debug_loc. See the symtab->locations_valid field.
595 This test is imperfect as there may exist optimized debug code not using
596 any location list and still facing inlining issues if handled as
597 unoptimized code. For a future better test see GCC PR other/32998. */
598 unsigned int has_loclist : 1;
599
600 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
601 if all the producer_is_* fields are valid. This information is cached
602 because profiling CU expansion showed excessive time spent in
603 producer_is_gxx_lt_4_6. */
604 unsigned int checked_producer : 1;
605 unsigned int producer_is_gxx_lt_4_6 : 1;
606 unsigned int producer_is_gcc_lt_4_3 : 1;
607 unsigned int producer_is_icc : 1;
608
609 /* When set, the file that we're processing is known to have
610 debugging info for C++ namespaces. GCC 3.3.x did not produce
611 this information, but later versions do. */
612
613 unsigned int processing_has_namespace_info : 1;
614 };
615
616 /* Persistent data held for a compilation unit, even when not
617 processing it. We put a pointer to this structure in the
618 read_symtab_private field of the psymtab. */
619
620 struct dwarf2_per_cu_data
621 {
622 /* The start offset and length of this compilation unit.
623 NOTE: Unlike comp_unit_head.length, this length includes
624 initial_length_size.
625 If the DIE refers to a DWO file, this is always of the original die,
626 not the DWO file. */
627 sect_offset sect_off;
628 unsigned int length;
629
630 /* DWARF standard version this data has been read from (such as 4 or 5). */
631 short dwarf_version;
632
633 /* Flag indicating this compilation unit will be read in before
634 any of the current compilation units are processed. */
635 unsigned int queued : 1;
636
637 /* This flag will be set when reading partial DIEs if we need to load
638 absolutely all DIEs for this compilation unit, instead of just the ones
639 we think are interesting. It gets set if we look for a DIE in the
640 hash table and don't find it. */
641 unsigned int load_all_dies : 1;
642
643 /* Non-zero if this CU is from .debug_types.
644 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
645 this is non-zero. */
646 unsigned int is_debug_types : 1;
647
648 /* Non-zero if this CU is from the .dwz file. */
649 unsigned int is_dwz : 1;
650
651 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
652 This flag is only valid if is_debug_types is true.
653 We can't read a CU directly from a DWO file: There are required
654 attributes in the stub. */
655 unsigned int reading_dwo_directly : 1;
656
657 /* Non-zero if the TU has been read.
658 This is used to assist the "Stay in DWO Optimization" for Fission:
659 When reading a DWO, it's faster to read TUs from the DWO instead of
660 fetching them from random other DWOs (due to comdat folding).
661 If the TU has already been read, the optimization is unnecessary
662 (and unwise - we don't want to change where gdb thinks the TU lives
663 "midflight").
664 This flag is only valid if is_debug_types is true. */
665 unsigned int tu_read : 1;
666
667 /* The section this CU/TU lives in.
668 If the DIE refers to a DWO file, this is always the original die,
669 not the DWO file. */
670 struct dwarf2_section_info *section;
671
672 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
673 of the CU cache it gets reset to NULL again. This is left as NULL for
674 dummy CUs (a CU header, but nothing else). */
675 struct dwarf2_cu *cu;
676
677 /* The corresponding objfile.
678 Normally we can get the objfile from dwarf2_per_objfile.
679 However we can enter this file with just a "per_cu" handle. */
680 struct objfile *objfile;
681
682 /* When dwarf2_per_objfile->using_index is true, the 'quick' field
683 is active. Otherwise, the 'psymtab' field is active. */
684 union
685 {
686 /* The partial symbol table associated with this compilation unit,
687 or NULL for unread partial units. */
688 struct partial_symtab *psymtab;
689
690 /* Data needed by the "quick" functions. */
691 struct dwarf2_per_cu_quick_data *quick;
692 } v;
693
694 /* The CUs we import using DW_TAG_imported_unit. This is filled in
695 while reading psymtabs, used to compute the psymtab dependencies,
696 and then cleared. Then it is filled in again while reading full
697 symbols, and only deleted when the objfile is destroyed.
698
699 This is also used to work around a difference between the way gold
700 generates .gdb_index version <=7 and the way gdb does. Arguably this
701 is a gold bug. For symbols coming from TUs, gold records in the index
702 the CU that includes the TU instead of the TU itself. This breaks
703 dw2_lookup_symbol: It assumes that if the index says symbol X lives
704 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
705 will find X. Alas TUs live in their own symtab, so after expanding CU Y
706 we need to look in TU Z to find X. Fortunately, this is akin to
707 DW_TAG_imported_unit, so we just use the same mechanism: For
708 .gdb_index version <=7 this also records the TUs that the CU referred
709 to. Concurrently with this change gdb was modified to emit version 8
710 indices so we only pay a price for gold generated indices.
711 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
712 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
713 };
714
715 /* Entry in the signatured_types hash table. */
716
717 struct signatured_type
718 {
719 /* The "per_cu" object of this type.
720 This struct is used iff per_cu.is_debug_types.
721 N.B.: This is the first member so that it's easy to convert pointers
722 between them. */
723 struct dwarf2_per_cu_data per_cu;
724
725 /* The type's signature. */
726 ULONGEST signature;
727
728 /* Offset in the TU of the type's DIE, as read from the TU header.
729 If this TU is a DWO stub and the definition lives in a DWO file
730 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
731 cu_offset type_offset_in_tu;
732
733 /* Offset in the section of the type's DIE.
734 If the definition lives in a DWO file, this is the offset in the
735 .debug_types.dwo section.
736 The value is zero until the actual value is known.
737 Zero is otherwise not a valid section offset. */
738 sect_offset type_offset_in_section;
739
740 /* Type units are grouped by their DW_AT_stmt_list entry so that they
741 can share them. This points to the containing symtab. */
742 struct type_unit_group *type_unit_group;
743
744 /* The type.
745 The first time we encounter this type we fully read it in and install it
746 in the symbol tables. Subsequent times we only need the type. */
747 struct type *type;
748
749 /* Containing DWO unit.
750 This field is valid iff per_cu.reading_dwo_directly. */
751 struct dwo_unit *dwo_unit;
752 };
753
754 typedef struct signatured_type *sig_type_ptr;
755 DEF_VEC_P (sig_type_ptr);
756
757 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
758 This includes type_unit_group and quick_file_names. */
759
760 struct stmt_list_hash
761 {
762 /* The DWO unit this table is from or NULL if there is none. */
763 struct dwo_unit *dwo_unit;
764
765 /* Offset in .debug_line or .debug_line.dwo. */
766 sect_offset line_sect_off;
767 };
768
769 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
770 an object of this type. */
771
772 struct type_unit_group
773 {
774 /* dwarf2read.c's main "handle" on a TU symtab.
775 To simplify things we create an artificial CU that "includes" all the
776 type units using this stmt_list so that the rest of the code still has
777 a "per_cu" handle on the symtab.
778 This PER_CU is recognized by having no section. */
779 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
780 struct dwarf2_per_cu_data per_cu;
781
782 /* The TUs that share this DW_AT_stmt_list entry.
783 This is added to while parsing type units to build partial symtabs,
784 and is deleted afterwards and not used again. */
785 VEC (sig_type_ptr) *tus;
786
787 /* The compunit symtab.
788 Type units in a group needn't all be defined in the same source file,
789 so we create an essentially anonymous symtab as the compunit symtab. */
790 struct compunit_symtab *compunit_symtab;
791
792 /* The data used to construct the hash key. */
793 struct stmt_list_hash hash;
794
795 /* The number of symtabs from the line header.
796 The value here must match line_header.num_file_names. */
797 unsigned int num_symtabs;
798
799 /* The symbol tables for this TU (obtained from the files listed in
800 DW_AT_stmt_list).
801 WARNING: The order of entries here must match the order of entries
802 in the line header. After the first TU using this type_unit_group, the
803 line header for the subsequent TUs is recreated from this. This is done
804 because we need to use the same symtabs for each TU using the same
805 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
806 there's no guarantee the line header doesn't have duplicate entries. */
807 struct symtab **symtabs;
808 };
809
810 /* These sections are what may appear in a (real or virtual) DWO file. */
811
812 struct dwo_sections
813 {
814 struct dwarf2_section_info abbrev;
815 struct dwarf2_section_info line;
816 struct dwarf2_section_info loc;
817 struct dwarf2_section_info loclists;
818 struct dwarf2_section_info macinfo;
819 struct dwarf2_section_info macro;
820 struct dwarf2_section_info str;
821 struct dwarf2_section_info str_offsets;
822 /* In the case of a virtual DWO file, these two are unused. */
823 struct dwarf2_section_info info;
824 VEC (dwarf2_section_info_def) *types;
825 };
826
827 /* CUs/TUs in DWP/DWO files. */
828
829 struct dwo_unit
830 {
831 /* Backlink to the containing struct dwo_file. */
832 struct dwo_file *dwo_file;
833
834 /* The "id" that distinguishes this CU/TU.
835 .debug_info calls this "dwo_id", .debug_types calls this "signature".
836 Since signatures came first, we stick with it for consistency. */
837 ULONGEST signature;
838
839 /* The section this CU/TU lives in, in the DWO file. */
840 struct dwarf2_section_info *section;
841
842 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
843 sect_offset sect_off;
844 unsigned int length;
845
846 /* For types, offset in the type's DIE of the type defined by this TU. */
847 cu_offset type_offset_in_tu;
848 };
849
850 /* include/dwarf2.h defines the DWP section codes.
851 It defines a max value but it doesn't define a min value, which we
852 use for error checking, so provide one. */
853
854 enum dwp_v2_section_ids
855 {
856 DW_SECT_MIN = 1
857 };
858
859 /* Data for one DWO file.
860
861 This includes virtual DWO files (a virtual DWO file is a DWO file as it
862 appears in a DWP file). DWP files don't really have DWO files per se -
863 comdat folding of types "loses" the DWO file they came from, and from
864 a high level view DWP files appear to contain a mass of random types.
865 However, to maintain consistency with the non-DWP case we pretend DWP
866 files contain virtual DWO files, and we assign each TU with one virtual
867 DWO file (generally based on the line and abbrev section offsets -
868 a heuristic that seems to work in practice). */
869
870 struct dwo_file
871 {
872 /* The DW_AT_GNU_dwo_name attribute.
873 For virtual DWO files the name is constructed from the section offsets
874 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
875 from related CU+TUs. */
876 const char *dwo_name;
877
878 /* The DW_AT_comp_dir attribute. */
879 const char *comp_dir;
880
881 /* The bfd, when the file is open. Otherwise this is NULL.
882 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
883 bfd *dbfd;
884
885 /* The sections that make up this DWO file.
886 Remember that for virtual DWO files in DWP V2, these are virtual
887 sections (for lack of a better name). */
888 struct dwo_sections sections;
889
890 /* The CUs in the file.
891 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
892 an extension to handle LLVM's Link Time Optimization output (where
893 multiple source files may be compiled into a single object/dwo pair). */
894 htab_t cus;
895
896 /* Table of TUs in the file.
897 Each element is a struct dwo_unit. */
898 htab_t tus;
899 };
900
901 /* These sections are what may appear in a DWP file. */
902
903 struct dwp_sections
904 {
905 /* These are used by both DWP version 1 and 2. */
906 struct dwarf2_section_info str;
907 struct dwarf2_section_info cu_index;
908 struct dwarf2_section_info tu_index;
909
910 /* These are only used by DWP version 2 files.
911 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
912 sections are referenced by section number, and are not recorded here.
913 In DWP version 2 there is at most one copy of all these sections, each
914 section being (effectively) comprised of the concatenation of all of the
915 individual sections that exist in the version 1 format.
916 To keep the code simple we treat each of these concatenated pieces as a
917 section itself (a virtual section?). */
918 struct dwarf2_section_info abbrev;
919 struct dwarf2_section_info info;
920 struct dwarf2_section_info line;
921 struct dwarf2_section_info loc;
922 struct dwarf2_section_info macinfo;
923 struct dwarf2_section_info macro;
924 struct dwarf2_section_info str_offsets;
925 struct dwarf2_section_info types;
926 };
927
928 /* These sections are what may appear in a virtual DWO file in DWP version 1.
929 A virtual DWO file is a DWO file as it appears in a DWP file. */
930
931 struct virtual_v1_dwo_sections
932 {
933 struct dwarf2_section_info abbrev;
934 struct dwarf2_section_info line;
935 struct dwarf2_section_info loc;
936 struct dwarf2_section_info macinfo;
937 struct dwarf2_section_info macro;
938 struct dwarf2_section_info str_offsets;
939 /* Each DWP hash table entry records one CU or one TU.
940 That is recorded here, and copied to dwo_unit.section. */
941 struct dwarf2_section_info info_or_types;
942 };
943
944 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
945 In version 2, the sections of the DWO files are concatenated together
946 and stored in one section of that name. Thus each ELF section contains
947 several "virtual" sections. */
948
949 struct virtual_v2_dwo_sections
950 {
951 bfd_size_type abbrev_offset;
952 bfd_size_type abbrev_size;
953
954 bfd_size_type line_offset;
955 bfd_size_type line_size;
956
957 bfd_size_type loc_offset;
958 bfd_size_type loc_size;
959
960 bfd_size_type macinfo_offset;
961 bfd_size_type macinfo_size;
962
963 bfd_size_type macro_offset;
964 bfd_size_type macro_size;
965
966 bfd_size_type str_offsets_offset;
967 bfd_size_type str_offsets_size;
968
969 /* Each DWP hash table entry records one CU or one TU.
970 That is recorded here, and copied to dwo_unit.section. */
971 bfd_size_type info_or_types_offset;
972 bfd_size_type info_or_types_size;
973 };
974
975 /* Contents of DWP hash tables. */
976
977 struct dwp_hash_table
978 {
979 uint32_t version, nr_columns;
980 uint32_t nr_units, nr_slots;
981 const gdb_byte *hash_table, *unit_table;
982 union
983 {
984 struct
985 {
986 const gdb_byte *indices;
987 } v1;
988 struct
989 {
990 /* This is indexed by column number and gives the id of the section
991 in that column. */
992 #define MAX_NR_V2_DWO_SECTIONS \
993 (1 /* .debug_info or .debug_types */ \
994 + 1 /* .debug_abbrev */ \
995 + 1 /* .debug_line */ \
996 + 1 /* .debug_loc */ \
997 + 1 /* .debug_str_offsets */ \
998 + 1 /* .debug_macro or .debug_macinfo */)
999 int section_ids[MAX_NR_V2_DWO_SECTIONS];
1000 const gdb_byte *offsets;
1001 const gdb_byte *sizes;
1002 } v2;
1003 } section_pool;
1004 };
1005
1006 /* Data for one DWP file. */
1007
1008 struct dwp_file
1009 {
1010 /* Name of the file. */
1011 const char *name;
1012
1013 /* File format version. */
1014 int version;
1015
1016 /* The bfd. */
1017 bfd *dbfd;
1018
1019 /* Section info for this file. */
1020 struct dwp_sections sections;
1021
1022 /* Table of CUs in the file. */
1023 const struct dwp_hash_table *cus;
1024
1025 /* Table of TUs in the file. */
1026 const struct dwp_hash_table *tus;
1027
1028 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
1029 htab_t loaded_cus;
1030 htab_t loaded_tus;
1031
1032 /* Table to map ELF section numbers to their sections.
1033 This is only needed for the DWP V1 file format. */
1034 unsigned int num_sections;
1035 asection **elf_sections;
1036 };
1037
1038 /* This represents a '.dwz' file. */
1039
1040 struct dwz_file
1041 {
1042 /* A dwz file can only contain a few sections. */
1043 struct dwarf2_section_info abbrev;
1044 struct dwarf2_section_info info;
1045 struct dwarf2_section_info str;
1046 struct dwarf2_section_info line;
1047 struct dwarf2_section_info macro;
1048 struct dwarf2_section_info gdb_index;
1049
1050 /* The dwz's BFD. */
1051 bfd *dwz_bfd;
1052 };
1053
1054 /* Struct used to pass misc. parameters to read_die_and_children, et
1055 al. which are used for both .debug_info and .debug_types dies.
1056 All parameters here are unchanging for the life of the call. This
1057 struct exists to abstract away the constant parameters of die reading. */
1058
1059 struct die_reader_specs
1060 {
1061 /* The bfd of die_section. */
1062 bfd* abfd;
1063
1064 /* The CU of the DIE we are parsing. */
1065 struct dwarf2_cu *cu;
1066
1067 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
1068 struct dwo_file *dwo_file;
1069
1070 /* The section the die comes from.
1071 This is either .debug_info or .debug_types, or the .dwo variants. */
1072 struct dwarf2_section_info *die_section;
1073
1074 /* die_section->buffer. */
1075 const gdb_byte *buffer;
1076
1077 /* The end of the buffer. */
1078 const gdb_byte *buffer_end;
1079
1080 /* The value of the DW_AT_comp_dir attribute. */
1081 const char *comp_dir;
1082 };
1083
1084 /* Type of function passed to init_cutu_and_read_dies, et.al. */
1085 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
1086 const gdb_byte *info_ptr,
1087 struct die_info *comp_unit_die,
1088 int has_children,
1089 void *data);
1090
1091 /* A 1-based directory index. This is a strong typedef to prevent
1092 accidentally using a directory index as a 0-based index into an
1093 array/vector. */
1094 enum class dir_index : unsigned int {};
1095
1096 /* Likewise, a 1-based file name index. */
1097 enum class file_name_index : unsigned int {};
1098
1099 struct file_entry
1100 {
1101 file_entry () = default;
1102
1103 file_entry (const char *name_, dir_index d_index_,
1104 unsigned int mod_time_, unsigned int length_)
1105 : name (name_),
1106 d_index (d_index_),
1107 mod_time (mod_time_),
1108 length (length_)
1109 {}
1110
1111 /* Return the include directory at D_INDEX stored in LH. Returns
1112 NULL if D_INDEX is out of bounds. */
1113 const char *include_dir (const line_header *lh) const;
1114
1115 /* The file name. Note this is an observing pointer. The memory is
1116 owned by debug_line_buffer. */
1117 const char *name {};
1118
1119 /* The directory index (1-based). */
1120 dir_index d_index {};
1121
1122 unsigned int mod_time {};
1123
1124 unsigned int length {};
1125
1126 /* True if referenced by the Line Number Program. */
1127 bool included_p {};
1128
1129 /* The associated symbol table, if any. */
1130 struct symtab *symtab {};
1131 };
1132
1133 /* The line number information for a compilation unit (found in the
1134 .debug_line section) begins with a "statement program header",
1135 which contains the following information. */
1136 struct line_header
1137 {
1138 line_header ()
1139 : offset_in_dwz {}
1140 {}
1141
1142 /* Add an entry to the include directory table. */
1143 void add_include_dir (const char *include_dir);
1144
1145 /* Add an entry to the file name table. */
1146 void add_file_name (const char *name, dir_index d_index,
1147 unsigned int mod_time, unsigned int length);
1148
1149 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
1150 is out of bounds. */
1151 const char *include_dir_at (dir_index index) const
1152 {
1153 /* Convert directory index number (1-based) to vector index
1154 (0-based). */
1155 size_t vec_index = to_underlying (index) - 1;
1156
1157 if (vec_index >= include_dirs.size ())
1158 return NULL;
1159 return include_dirs[vec_index];
1160 }
1161
1162 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
1163 is out of bounds. */
1164 file_entry *file_name_at (file_name_index index)
1165 {
1166 /* Convert file name index number (1-based) to vector index
1167 (0-based). */
1168 size_t vec_index = to_underlying (index) - 1;
1169
1170 if (vec_index >= file_names.size ())
1171 return NULL;
1172 return &file_names[vec_index];
1173 }
1174
1175 /* Const version of the above. */
1176 const file_entry *file_name_at (unsigned int index) const
1177 {
1178 if (index >= file_names.size ())
1179 return NULL;
1180 return &file_names[index];
1181 }
1182
1183 /* Offset of line number information in .debug_line section. */
1184 sect_offset sect_off {};
1185
1186 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1187 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1188
1189 unsigned int total_length {};
1190 unsigned short version {};
1191 unsigned int header_length {};
1192 unsigned char minimum_instruction_length {};
1193 unsigned char maximum_ops_per_instruction {};
1194 unsigned char default_is_stmt {};
1195 int line_base {};
1196 unsigned char line_range {};
1197 unsigned char opcode_base {};
1198
1199 /* standard_opcode_lengths[i] is the number of operands for the
1200 standard opcode whose value is i. This means that
1201 standard_opcode_lengths[0] is unused, and the last meaningful
1202 element is standard_opcode_lengths[opcode_base - 1]. */
1203 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
1204
1205 /* The include_directories table. Note these are observing
1206 pointers. The memory is owned by debug_line_buffer. */
1207 std::vector<const char *> include_dirs;
1208
1209 /* The file_names table. */
1210 std::vector<file_entry> file_names;
1211
1212 /* The start and end of the statement program following this
1213 header. These point into dwarf2_per_objfile->line_buffer. */
1214 const gdb_byte *statement_program_start {}, *statement_program_end {};
1215 };
1216
1217 typedef std::unique_ptr<line_header> line_header_up;
1218
1219 const char *
1220 file_entry::include_dir (const line_header *lh) const
1221 {
1222 return lh->include_dir_at (d_index);
1223 }
1224
1225 /* When we construct a partial symbol table entry we only
1226 need this much information. */
1227 struct partial_die_info
1228 {
1229 /* Offset of this DIE. */
1230 sect_offset sect_off;
1231
1232 /* DWARF-2 tag for this DIE. */
1233 ENUM_BITFIELD(dwarf_tag) tag : 16;
1234
1235 /* Assorted flags describing the data found in this DIE. */
1236 unsigned int has_children : 1;
1237 unsigned int is_external : 1;
1238 unsigned int is_declaration : 1;
1239 unsigned int has_type : 1;
1240 unsigned int has_specification : 1;
1241 unsigned int has_pc_info : 1;
1242 unsigned int may_be_inlined : 1;
1243
1244 /* This DIE has been marked DW_AT_main_subprogram. */
1245 unsigned int main_subprogram : 1;
1246
1247 /* Flag set if the SCOPE field of this structure has been
1248 computed. */
1249 unsigned int scope_set : 1;
1250
1251 /* Flag set if the DIE has a byte_size attribute. */
1252 unsigned int has_byte_size : 1;
1253
1254 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1255 unsigned int has_const_value : 1;
1256
1257 /* Flag set if any of the DIE's children are template arguments. */
1258 unsigned int has_template_arguments : 1;
1259
1260 /* Flag set if fixup_partial_die has been called on this die. */
1261 unsigned int fixup_called : 1;
1262
1263 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1264 unsigned int is_dwz : 1;
1265
1266 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1267 unsigned int spec_is_dwz : 1;
1268
1269 /* The name of this DIE. Normally the value of DW_AT_name, but
1270 sometimes a default name for unnamed DIEs. */
1271 const char *name;
1272
1273 /* The linkage name, if present. */
1274 const char *linkage_name;
1275
1276 /* The scope to prepend to our children. This is generally
1277 allocated on the comp_unit_obstack, so will disappear
1278 when this compilation unit leaves the cache. */
1279 const char *scope;
1280
1281 /* Some data associated with the partial DIE. The tag determines
1282 which field is live. */
1283 union
1284 {
1285 /* The location description associated with this DIE, if any. */
1286 struct dwarf_block *locdesc;
1287 /* The offset of an import, for DW_TAG_imported_unit. */
1288 sect_offset sect_off;
1289 } d;
1290
1291 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1292 CORE_ADDR lowpc;
1293 CORE_ADDR highpc;
1294
1295 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1296 DW_AT_sibling, if any. */
1297 /* NOTE: This member isn't strictly necessary, read_partial_die could
1298 return DW_AT_sibling values to its caller load_partial_dies. */
1299 const gdb_byte *sibling;
1300
1301 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1302 DW_AT_specification (or DW_AT_abstract_origin or
1303 DW_AT_extension). */
1304 sect_offset spec_offset;
1305
1306 /* Pointers to this DIE's parent, first child, and next sibling,
1307 if any. */
1308 struct partial_die_info *die_parent, *die_child, *die_sibling;
1309 };
1310
1311 /* This data structure holds the information of an abbrev. */
1312 struct abbrev_info
1313 {
1314 unsigned int number; /* number identifying abbrev */
1315 enum dwarf_tag tag; /* dwarf tag */
1316 unsigned short has_children; /* boolean */
1317 unsigned short num_attrs; /* number of attributes */
1318 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1319 struct abbrev_info *next; /* next in chain */
1320 };
1321
1322 struct attr_abbrev
1323 {
1324 ENUM_BITFIELD(dwarf_attribute) name : 16;
1325 ENUM_BITFIELD(dwarf_form) form : 16;
1326
1327 /* It is valid only if FORM is DW_FORM_implicit_const. */
1328 LONGEST implicit_const;
1329 };
1330
1331 /* Size of abbrev_table.abbrev_hash_table. */
1332 #define ABBREV_HASH_SIZE 121
1333
1334 /* Top level data structure to contain an abbreviation table. */
1335
1336 struct abbrev_table
1337 {
1338 /* Where the abbrev table came from.
1339 This is used as a sanity check when the table is used. */
1340 sect_offset sect_off;
1341
1342 /* Storage for the abbrev table. */
1343 struct obstack abbrev_obstack;
1344
1345 /* Hash table of abbrevs.
1346 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1347 It could be statically allocated, but the previous code didn't so we
1348 don't either. */
1349 struct abbrev_info **abbrevs;
1350 };
1351
1352 /* Attributes have a name and a value. */
1353 struct attribute
1354 {
1355 ENUM_BITFIELD(dwarf_attribute) name : 16;
1356 ENUM_BITFIELD(dwarf_form) form : 15;
1357
1358 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1359 field should be in u.str (existing only for DW_STRING) but it is kept
1360 here for better struct attribute alignment. */
1361 unsigned int string_is_canonical : 1;
1362
1363 union
1364 {
1365 const char *str;
1366 struct dwarf_block *blk;
1367 ULONGEST unsnd;
1368 LONGEST snd;
1369 CORE_ADDR addr;
1370 ULONGEST signature;
1371 }
1372 u;
1373 };
1374
1375 /* This data structure holds a complete die structure. */
1376 struct die_info
1377 {
1378 /* DWARF-2 tag for this DIE. */
1379 ENUM_BITFIELD(dwarf_tag) tag : 16;
1380
1381 /* Number of attributes */
1382 unsigned char num_attrs;
1383
1384 /* True if we're presently building the full type name for the
1385 type derived from this DIE. */
1386 unsigned char building_fullname : 1;
1387
1388 /* True if this die is in process. PR 16581. */
1389 unsigned char in_process : 1;
1390
1391 /* Abbrev number */
1392 unsigned int abbrev;
1393
1394 /* Offset in .debug_info or .debug_types section. */
1395 sect_offset sect_off;
1396
1397 /* The dies in a compilation unit form an n-ary tree. PARENT
1398 points to this die's parent; CHILD points to the first child of
1399 this node; and all the children of a given node are chained
1400 together via their SIBLING fields. */
1401 struct die_info *child; /* Its first child, if any. */
1402 struct die_info *sibling; /* Its next sibling, if any. */
1403 struct die_info *parent; /* Its parent, if any. */
1404
1405 /* An array of attributes, with NUM_ATTRS elements. There may be
1406 zero, but it's not common and zero-sized arrays are not
1407 sufficiently portable C. */
1408 struct attribute attrs[1];
1409 };
1410
1411 /* Get at parts of an attribute structure. */
1412
1413 #define DW_STRING(attr) ((attr)->u.str)
1414 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1415 #define DW_UNSND(attr) ((attr)->u.unsnd)
1416 #define DW_BLOCK(attr) ((attr)->u.blk)
1417 #define DW_SND(attr) ((attr)->u.snd)
1418 #define DW_ADDR(attr) ((attr)->u.addr)
1419 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1420
1421 /* Blocks are a bunch of untyped bytes. */
1422 struct dwarf_block
1423 {
1424 size_t size;
1425
1426 /* Valid only if SIZE is not zero. */
1427 const gdb_byte *data;
1428 };
1429
1430 #ifndef ATTR_ALLOC_CHUNK
1431 #define ATTR_ALLOC_CHUNK 4
1432 #endif
1433
1434 /* Allocate fields for structs, unions and enums in this size. */
1435 #ifndef DW_FIELD_ALLOC_CHUNK
1436 #define DW_FIELD_ALLOC_CHUNK 4
1437 #endif
1438
1439 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1440 but this would require a corresponding change in unpack_field_as_long
1441 and friends. */
1442 static int bits_per_byte = 8;
1443
1444 struct nextfield
1445 {
1446 struct nextfield *next;
1447 int accessibility;
1448 int virtuality;
1449 struct field field;
1450 };
1451
1452 struct nextfnfield
1453 {
1454 struct nextfnfield *next;
1455 struct fn_field fnfield;
1456 };
1457
1458 struct fnfieldlist
1459 {
1460 const char *name;
1461 int length;
1462 struct nextfnfield *head;
1463 };
1464
1465 struct typedef_field_list
1466 {
1467 struct typedef_field field;
1468 struct typedef_field_list *next;
1469 };
1470
1471 /* The routines that read and process dies for a C struct or C++ class
1472 pass lists of data member fields and lists of member function fields
1473 in an instance of a field_info structure, as defined below. */
1474 struct field_info
1475 {
1476 /* List of data member and baseclasses fields. */
1477 struct nextfield *fields, *baseclasses;
1478
1479 /* Number of fields (including baseclasses). */
1480 int nfields;
1481
1482 /* Number of baseclasses. */
1483 int nbaseclasses;
1484
1485 /* Set if the accesibility of one of the fields is not public. */
1486 int non_public_fields;
1487
1488 /* Member function fieldlist array, contains name of possibly overloaded
1489 member function, number of overloaded member functions and a pointer
1490 to the head of the member function field chain. */
1491 struct fnfieldlist *fnfieldlists;
1492
1493 /* Number of entries in the fnfieldlists array. */
1494 int nfnfields;
1495
1496 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1497 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1498 struct typedef_field_list *typedef_field_list;
1499 unsigned typedef_field_list_count;
1500 };
1501
1502 /* One item on the queue of compilation units to read in full symbols
1503 for. */
1504 struct dwarf2_queue_item
1505 {
1506 struct dwarf2_per_cu_data *per_cu;
1507 enum language pretend_language;
1508 struct dwarf2_queue_item *next;
1509 };
1510
1511 /* The current queue. */
1512 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1513
1514 /* Loaded secondary compilation units are kept in memory until they
1515 have not been referenced for the processing of this many
1516 compilation units. Set this to zero to disable caching. Cache
1517 sizes of up to at least twenty will improve startup time for
1518 typical inter-CU-reference binaries, at an obvious memory cost. */
1519 static int dwarf_max_cache_age = 5;
1520 static void
1521 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1522 struct cmd_list_element *c, const char *value)
1523 {
1524 fprintf_filtered (file, _("The upper bound on the age of cached "
1525 "DWARF compilation units is %s.\n"),
1526 value);
1527 }
1528 \f
1529 /* local function prototypes */
1530
1531 static const char *get_section_name (const struct dwarf2_section_info *);
1532
1533 static const char *get_section_file_name (const struct dwarf2_section_info *);
1534
1535 static void dwarf2_find_base_address (struct die_info *die,
1536 struct dwarf2_cu *cu);
1537
1538 static struct partial_symtab *create_partial_symtab
1539 (struct dwarf2_per_cu_data *per_cu, const char *name);
1540
1541 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1542 const gdb_byte *info_ptr,
1543 struct die_info *type_unit_die,
1544 int has_children, void *data);
1545
1546 static void dwarf2_build_psymtabs_hard (struct objfile *);
1547
1548 static void scan_partial_symbols (struct partial_die_info *,
1549 CORE_ADDR *, CORE_ADDR *,
1550 int, struct dwarf2_cu *);
1551
1552 static void add_partial_symbol (struct partial_die_info *,
1553 struct dwarf2_cu *);
1554
1555 static void add_partial_namespace (struct partial_die_info *pdi,
1556 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1557 int set_addrmap, struct dwarf2_cu *cu);
1558
1559 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1560 CORE_ADDR *highpc, int set_addrmap,
1561 struct dwarf2_cu *cu);
1562
1563 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1564 struct dwarf2_cu *cu);
1565
1566 static void add_partial_subprogram (struct partial_die_info *pdi,
1567 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1568 int need_pc, struct dwarf2_cu *cu);
1569
1570 static void dwarf2_read_symtab (struct partial_symtab *,
1571 struct objfile *);
1572
1573 static void psymtab_to_symtab_1 (struct partial_symtab *);
1574
1575 static struct abbrev_info *abbrev_table_lookup_abbrev
1576 (const struct abbrev_table *, unsigned int);
1577
1578 static struct abbrev_table *abbrev_table_read_table
1579 (struct dwarf2_section_info *, sect_offset);
1580
1581 static void abbrev_table_free (struct abbrev_table *);
1582
1583 static void abbrev_table_free_cleanup (void *);
1584
1585 static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1586 struct dwarf2_section_info *);
1587
1588 static void dwarf2_free_abbrev_table (void *);
1589
1590 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1591
1592 static struct partial_die_info *load_partial_dies
1593 (const struct die_reader_specs *, const gdb_byte *, int);
1594
1595 static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1596 struct partial_die_info *,
1597 struct abbrev_info *,
1598 unsigned int,
1599 const gdb_byte *);
1600
1601 static struct partial_die_info *find_partial_die (sect_offset, int,
1602 struct dwarf2_cu *);
1603
1604 static void fixup_partial_die (struct partial_die_info *,
1605 struct dwarf2_cu *);
1606
1607 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1608 struct attribute *, struct attr_abbrev *,
1609 const gdb_byte *);
1610
1611 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1612
1613 static int read_1_signed_byte (bfd *, const gdb_byte *);
1614
1615 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1616
1617 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1618
1619 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1620
1621 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1622 unsigned int *);
1623
1624 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1625
1626 static LONGEST read_checked_initial_length_and_offset
1627 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1628 unsigned int *, unsigned int *);
1629
1630 static LONGEST read_offset (bfd *, const gdb_byte *,
1631 const struct comp_unit_head *,
1632 unsigned int *);
1633
1634 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1635
1636 static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1637 sect_offset);
1638
1639 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1640
1641 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1642
1643 static const char *read_indirect_string (bfd *, const gdb_byte *,
1644 const struct comp_unit_head *,
1645 unsigned int *);
1646
1647 static const char *read_indirect_line_string (bfd *, const gdb_byte *,
1648 const struct comp_unit_head *,
1649 unsigned int *);
1650
1651 static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1652
1653 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1654
1655 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1656 const gdb_byte *,
1657 unsigned int *);
1658
1659 static const char *read_str_index (const struct die_reader_specs *reader,
1660 ULONGEST str_index);
1661
1662 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1663
1664 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1665 struct dwarf2_cu *);
1666
1667 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1668 unsigned int);
1669
1670 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1671 struct dwarf2_cu *cu);
1672
1673 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1674 struct dwarf2_cu *cu);
1675
1676 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1677
1678 static struct die_info *die_specification (struct die_info *die,
1679 struct dwarf2_cu **);
1680
1681 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1682 struct dwarf2_cu *cu);
1683
1684 static void dwarf_decode_lines (struct line_header *, const char *,
1685 struct dwarf2_cu *, struct partial_symtab *,
1686 CORE_ADDR, int decode_mapping);
1687
1688 static void dwarf2_start_subfile (const char *, const char *);
1689
1690 static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1691 const char *, const char *,
1692 CORE_ADDR);
1693
1694 static struct symbol *new_symbol (struct die_info *, struct type *,
1695 struct dwarf2_cu *);
1696
1697 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1698 struct dwarf2_cu *, struct symbol *);
1699
1700 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1701 struct dwarf2_cu *);
1702
1703 static void dwarf2_const_value_attr (const struct attribute *attr,
1704 struct type *type,
1705 const char *name,
1706 struct obstack *obstack,
1707 struct dwarf2_cu *cu, LONGEST *value,
1708 const gdb_byte **bytes,
1709 struct dwarf2_locexpr_baton **baton);
1710
1711 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1712
1713 static int need_gnat_info (struct dwarf2_cu *);
1714
1715 static struct type *die_descriptive_type (struct die_info *,
1716 struct dwarf2_cu *);
1717
1718 static void set_descriptive_type (struct type *, struct die_info *,
1719 struct dwarf2_cu *);
1720
1721 static struct type *die_containing_type (struct die_info *,
1722 struct dwarf2_cu *);
1723
1724 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1725 struct dwarf2_cu *);
1726
1727 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1728
1729 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1730
1731 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1732
1733 static char *typename_concat (struct obstack *obs, const char *prefix,
1734 const char *suffix, int physname,
1735 struct dwarf2_cu *cu);
1736
1737 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1738
1739 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1740
1741 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1742
1743 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1744
1745 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1746
1747 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1748 struct dwarf2_cu *, struct partial_symtab *);
1749
1750 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1751 values. Keep the items ordered with increasing constraints compliance. */
1752 enum pc_bounds_kind
1753 {
1754 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1755 PC_BOUNDS_NOT_PRESENT,
1756
1757 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1758 were present but they do not form a valid range of PC addresses. */
1759 PC_BOUNDS_INVALID,
1760
1761 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1762 PC_BOUNDS_RANGES,
1763
1764 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1765 PC_BOUNDS_HIGH_LOW,
1766 };
1767
1768 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1769 CORE_ADDR *, CORE_ADDR *,
1770 struct dwarf2_cu *,
1771 struct partial_symtab *);
1772
1773 static void get_scope_pc_bounds (struct die_info *,
1774 CORE_ADDR *, CORE_ADDR *,
1775 struct dwarf2_cu *);
1776
1777 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1778 CORE_ADDR, struct dwarf2_cu *);
1779
1780 static void dwarf2_add_field (struct field_info *, struct die_info *,
1781 struct dwarf2_cu *);
1782
1783 static void dwarf2_attach_fields_to_type (struct field_info *,
1784 struct type *, struct dwarf2_cu *);
1785
1786 static void dwarf2_add_member_fn (struct field_info *,
1787 struct die_info *, struct type *,
1788 struct dwarf2_cu *);
1789
1790 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1791 struct type *,
1792 struct dwarf2_cu *);
1793
1794 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1795
1796 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1797
1798 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1799
1800 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1801
1802 static struct using_direct **using_directives (enum language);
1803
1804 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1805
1806 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1807
1808 static struct type *read_module_type (struct die_info *die,
1809 struct dwarf2_cu *cu);
1810
1811 static const char *namespace_name (struct die_info *die,
1812 int *is_anonymous, struct dwarf2_cu *);
1813
1814 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1815
1816 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1817
1818 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1819 struct dwarf2_cu *);
1820
1821 static struct die_info *read_die_and_siblings_1
1822 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1823 struct die_info *);
1824
1825 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1826 const gdb_byte *info_ptr,
1827 const gdb_byte **new_info_ptr,
1828 struct die_info *parent);
1829
1830 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1831 struct die_info **, const gdb_byte *,
1832 int *, int);
1833
1834 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1835 struct die_info **, const gdb_byte *,
1836 int *);
1837
1838 static void process_die (struct die_info *, struct dwarf2_cu *);
1839
1840 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1841 struct obstack *);
1842
1843 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1844
1845 static const char *dwarf2_full_name (const char *name,
1846 struct die_info *die,
1847 struct dwarf2_cu *cu);
1848
1849 static const char *dwarf2_physname (const char *name, struct die_info *die,
1850 struct dwarf2_cu *cu);
1851
1852 static struct die_info *dwarf2_extension (struct die_info *die,
1853 struct dwarf2_cu **);
1854
1855 static const char *dwarf_tag_name (unsigned int);
1856
1857 static const char *dwarf_attr_name (unsigned int);
1858
1859 static const char *dwarf_form_name (unsigned int);
1860
1861 static const char *dwarf_bool_name (unsigned int);
1862
1863 static const char *dwarf_type_encoding_name (unsigned int);
1864
1865 static struct die_info *sibling_die (struct die_info *);
1866
1867 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1868
1869 static void dump_die_for_error (struct die_info *);
1870
1871 static void dump_die_1 (struct ui_file *, int level, int max_level,
1872 struct die_info *);
1873
1874 /*static*/ void dump_die (struct die_info *, int max_level);
1875
1876 static void store_in_ref_table (struct die_info *,
1877 struct dwarf2_cu *);
1878
1879 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1880
1881 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1882
1883 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1884 const struct attribute *,
1885 struct dwarf2_cu **);
1886
1887 static struct die_info *follow_die_ref (struct die_info *,
1888 const struct attribute *,
1889 struct dwarf2_cu **);
1890
1891 static struct die_info *follow_die_sig (struct die_info *,
1892 const struct attribute *,
1893 struct dwarf2_cu **);
1894
1895 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1896 struct dwarf2_cu *);
1897
1898 static struct type *get_DW_AT_signature_type (struct die_info *,
1899 const struct attribute *,
1900 struct dwarf2_cu *);
1901
1902 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1903
1904 static void read_signatured_type (struct signatured_type *);
1905
1906 static int attr_to_dynamic_prop (const struct attribute *attr,
1907 struct die_info *die, struct dwarf2_cu *cu,
1908 struct dynamic_prop *prop);
1909
1910 /* memory allocation interface */
1911
1912 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1913
1914 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1915
1916 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1917
1918 static int attr_form_is_block (const struct attribute *);
1919
1920 static int attr_form_is_section_offset (const struct attribute *);
1921
1922 static int attr_form_is_constant (const struct attribute *);
1923
1924 static int attr_form_is_ref (const struct attribute *);
1925
1926 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1927 struct dwarf2_loclist_baton *baton,
1928 const struct attribute *attr);
1929
1930 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1931 struct symbol *sym,
1932 struct dwarf2_cu *cu,
1933 int is_block);
1934
1935 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1936 const gdb_byte *info_ptr,
1937 struct abbrev_info *abbrev);
1938
1939 static void free_stack_comp_unit (void *);
1940
1941 static hashval_t partial_die_hash (const void *item);
1942
1943 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1944
1945 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1946 (sect_offset sect_off, unsigned int offset_in_dwz, struct objfile *objfile);
1947
1948 static void init_one_comp_unit (struct dwarf2_cu *cu,
1949 struct dwarf2_per_cu_data *per_cu);
1950
1951 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1952 struct die_info *comp_unit_die,
1953 enum language pretend_language);
1954
1955 static void free_heap_comp_unit (void *);
1956
1957 static void free_cached_comp_units (void *);
1958
1959 static void age_cached_comp_units (void);
1960
1961 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1962
1963 static struct type *set_die_type (struct die_info *, struct type *,
1964 struct dwarf2_cu *);
1965
1966 static void create_all_comp_units (struct objfile *);
1967
1968 static int create_all_type_units (struct objfile *);
1969
1970 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1971 enum language);
1972
1973 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1974 enum language);
1975
1976 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1977 enum language);
1978
1979 static void dwarf2_add_dependence (struct dwarf2_cu *,
1980 struct dwarf2_per_cu_data *);
1981
1982 static void dwarf2_mark (struct dwarf2_cu *);
1983
1984 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1985
1986 static struct type *get_die_type_at_offset (sect_offset,
1987 struct dwarf2_per_cu_data *);
1988
1989 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1990
1991 static void dwarf2_release_queue (void *dummy);
1992
1993 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1994 enum language pretend_language);
1995
1996 static void process_queue (void);
1997
1998 /* The return type of find_file_and_directory. Note, the enclosed
1999 string pointers are only valid while this object is valid. */
2000
2001 struct file_and_directory
2002 {
2003 /* The filename. This is never NULL. */
2004 const char *name;
2005
2006 /* The compilation directory. NULL if not known. If we needed to
2007 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
2008 points directly to the DW_AT_comp_dir string attribute owned by
2009 the obstack that owns the DIE. */
2010 const char *comp_dir;
2011
2012 /* If we needed to build a new string for comp_dir, this is what
2013 owns the storage. */
2014 std::string comp_dir_storage;
2015 };
2016
2017 static file_and_directory find_file_and_directory (struct die_info *die,
2018 struct dwarf2_cu *cu);
2019
2020 static char *file_full_name (int file, struct line_header *lh,
2021 const char *comp_dir);
2022
2023 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
2024 enum class rcuh_kind { COMPILE, TYPE };
2025
2026 static const gdb_byte *read_and_check_comp_unit_head
2027 (struct comp_unit_head *header,
2028 struct dwarf2_section_info *section,
2029 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
2030 rcuh_kind section_kind);
2031
2032 static void init_cutu_and_read_dies
2033 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
2034 int use_existing_cu, int keep,
2035 die_reader_func_ftype *die_reader_func, void *data);
2036
2037 static void init_cutu_and_read_dies_simple
2038 (struct dwarf2_per_cu_data *this_cu,
2039 die_reader_func_ftype *die_reader_func, void *data);
2040
2041 static htab_t allocate_signatured_type_table (struct objfile *objfile);
2042
2043 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2044
2045 static struct dwo_unit *lookup_dwo_unit_in_dwp
2046 (struct dwp_file *dwp_file, const char *comp_dir,
2047 ULONGEST signature, int is_debug_types);
2048
2049 static struct dwp_file *get_dwp_file (void);
2050
2051 static struct dwo_unit *lookup_dwo_comp_unit
2052 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
2053
2054 static struct dwo_unit *lookup_dwo_type_unit
2055 (struct signatured_type *, const char *, const char *);
2056
2057 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2058
2059 static void free_dwo_file_cleanup (void *);
2060
2061 static void process_cu_includes (void);
2062
2063 static void check_producer (struct dwarf2_cu *cu);
2064
2065 static void free_line_header_voidp (void *arg);
2066 \f
2067 /* Various complaints about symbol reading that don't abort the process. */
2068
2069 static void
2070 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2071 {
2072 complaint (&symfile_complaints,
2073 _("statement list doesn't fit in .debug_line section"));
2074 }
2075
2076 static void
2077 dwarf2_debug_line_missing_file_complaint (void)
2078 {
2079 complaint (&symfile_complaints,
2080 _(".debug_line section has line data without a file"));
2081 }
2082
2083 static void
2084 dwarf2_debug_line_missing_end_sequence_complaint (void)
2085 {
2086 complaint (&symfile_complaints,
2087 _(".debug_line section has line "
2088 "program sequence without an end"));
2089 }
2090
2091 static void
2092 dwarf2_complex_location_expr_complaint (void)
2093 {
2094 complaint (&symfile_complaints, _("location expression too complex"));
2095 }
2096
2097 static void
2098 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2099 int arg3)
2100 {
2101 complaint (&symfile_complaints,
2102 _("const value length mismatch for '%s', got %d, expected %d"),
2103 arg1, arg2, arg3);
2104 }
2105
2106 static void
2107 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2108 {
2109 complaint (&symfile_complaints,
2110 _("debug info runs off end of %s section"
2111 " [in module %s]"),
2112 get_section_name (section),
2113 get_section_file_name (section));
2114 }
2115
2116 static void
2117 dwarf2_macro_malformed_definition_complaint (const char *arg1)
2118 {
2119 complaint (&symfile_complaints,
2120 _("macro debug info contains a "
2121 "malformed macro definition:\n`%s'"),
2122 arg1);
2123 }
2124
2125 static void
2126 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2127 {
2128 complaint (&symfile_complaints,
2129 _("invalid attribute class or form for '%s' in '%s'"),
2130 arg1, arg2);
2131 }
2132
2133 /* Hash function for line_header_hash. */
2134
2135 static hashval_t
2136 line_header_hash (const struct line_header *ofs)
2137 {
2138 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
2139 }
2140
2141 /* Hash function for htab_create_alloc_ex for line_header_hash. */
2142
2143 static hashval_t
2144 line_header_hash_voidp (const void *item)
2145 {
2146 const struct line_header *ofs = (const struct line_header *) item;
2147
2148 return line_header_hash (ofs);
2149 }
2150
2151 /* Equality function for line_header_hash. */
2152
2153 static int
2154 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2155 {
2156 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2157 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
2158
2159 return (ofs_lhs->sect_off == ofs_rhs->sect_off
2160 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2161 }
2162
2163 \f
2164 #if WORDS_BIGENDIAN
2165
2166 /* Convert VALUE between big- and little-endian. */
2167 static offset_type
2168 byte_swap (offset_type value)
2169 {
2170 offset_type result;
2171
2172 result = (value & 0xff) << 24;
2173 result |= (value & 0xff00) << 8;
2174 result |= (value & 0xff0000) >> 8;
2175 result |= (value & 0xff000000) >> 24;
2176 return result;
2177 }
2178
2179 #define MAYBE_SWAP(V) byte_swap (V)
2180
2181 #else
2182 #define MAYBE_SWAP(V) static_cast<offset_type> (V)
2183 #endif /* WORDS_BIGENDIAN */
2184
2185 /* Read the given attribute value as an address, taking the attribute's
2186 form into account. */
2187
2188 static CORE_ADDR
2189 attr_value_as_address (struct attribute *attr)
2190 {
2191 CORE_ADDR addr;
2192
2193 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2194 {
2195 /* Aside from a few clearly defined exceptions, attributes that
2196 contain an address must always be in DW_FORM_addr form.
2197 Unfortunately, some compilers happen to be violating this
2198 requirement by encoding addresses using other forms, such
2199 as DW_FORM_data4 for example. For those broken compilers,
2200 we try to do our best, without any guarantee of success,
2201 to interpret the address correctly. It would also be nice
2202 to generate a complaint, but that would require us to maintain
2203 a list of legitimate cases where a non-address form is allowed,
2204 as well as update callers to pass in at least the CU's DWARF
2205 version. This is more overhead than what we're willing to
2206 expand for a pretty rare case. */
2207 addr = DW_UNSND (attr);
2208 }
2209 else
2210 addr = DW_ADDR (attr);
2211
2212 return addr;
2213 }
2214
2215 /* The suffix for an index file. */
2216 #define INDEX_SUFFIX ".gdb-index"
2217
2218 /* See declaration. */
2219
2220 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2221 const dwarf2_debug_sections *names)
2222 : objfile (objfile_)
2223 {
2224 if (names == NULL)
2225 names = &dwarf2_elf_names;
2226
2227 bfd *obfd = objfile->obfd;
2228
2229 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2230 locate_sections (obfd, sec, *names);
2231 }
2232
2233 dwarf2_per_objfile::~dwarf2_per_objfile ()
2234 {
2235 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2236 free_cached_comp_units ();
2237
2238 if (quick_file_names_table)
2239 htab_delete (quick_file_names_table);
2240
2241 if (line_header_hash)
2242 htab_delete (line_header_hash);
2243
2244 /* Everything else should be on the objfile obstack. */
2245 }
2246
2247 /* See declaration. */
2248
2249 void
2250 dwarf2_per_objfile::free_cached_comp_units ()
2251 {
2252 dwarf2_per_cu_data *per_cu = read_in_chain;
2253 dwarf2_per_cu_data **last_chain = &read_in_chain;
2254 while (per_cu != NULL)
2255 {
2256 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2257
2258 free_heap_comp_unit (per_cu->cu);
2259 *last_chain = next_cu;
2260 per_cu = next_cu;
2261 }
2262 }
2263
2264 /* Try to locate the sections we need for DWARF 2 debugging
2265 information and return true if we have enough to do something.
2266 NAMES points to the dwarf2 section names, or is NULL if the standard
2267 ELF names are used. */
2268
2269 int
2270 dwarf2_has_info (struct objfile *objfile,
2271 const struct dwarf2_debug_sections *names)
2272 {
2273 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
2274 objfile_data (objfile, dwarf2_objfile_data_key));
2275 if (!dwarf2_per_objfile)
2276 {
2277 /* Initialize per-objfile state. */
2278 struct dwarf2_per_objfile *data
2279 = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_objfile);
2280
2281 dwarf2_per_objfile = new (data) struct dwarf2_per_objfile (objfile, names);
2282 set_objfile_data (objfile, dwarf2_objfile_data_key, dwarf2_per_objfile);
2283 }
2284 return (!dwarf2_per_objfile->info.is_virtual
2285 && dwarf2_per_objfile->info.s.section != NULL
2286 && !dwarf2_per_objfile->abbrev.is_virtual
2287 && dwarf2_per_objfile->abbrev.s.section != NULL);
2288 }
2289
2290 /* Return the containing section of virtual section SECTION. */
2291
2292 static struct dwarf2_section_info *
2293 get_containing_section (const struct dwarf2_section_info *section)
2294 {
2295 gdb_assert (section->is_virtual);
2296 return section->s.containing_section;
2297 }
2298
2299 /* Return the bfd owner of SECTION. */
2300
2301 static struct bfd *
2302 get_section_bfd_owner (const struct dwarf2_section_info *section)
2303 {
2304 if (section->is_virtual)
2305 {
2306 section = get_containing_section (section);
2307 gdb_assert (!section->is_virtual);
2308 }
2309 return section->s.section->owner;
2310 }
2311
2312 /* Return the bfd section of SECTION.
2313 Returns NULL if the section is not present. */
2314
2315 static asection *
2316 get_section_bfd_section (const struct dwarf2_section_info *section)
2317 {
2318 if (section->is_virtual)
2319 {
2320 section = get_containing_section (section);
2321 gdb_assert (!section->is_virtual);
2322 }
2323 return section->s.section;
2324 }
2325
2326 /* Return the name of SECTION. */
2327
2328 static const char *
2329 get_section_name (const struct dwarf2_section_info *section)
2330 {
2331 asection *sectp = get_section_bfd_section (section);
2332
2333 gdb_assert (sectp != NULL);
2334 return bfd_section_name (get_section_bfd_owner (section), sectp);
2335 }
2336
2337 /* Return the name of the file SECTION is in. */
2338
2339 static const char *
2340 get_section_file_name (const struct dwarf2_section_info *section)
2341 {
2342 bfd *abfd = get_section_bfd_owner (section);
2343
2344 return bfd_get_filename (abfd);
2345 }
2346
2347 /* Return the id of SECTION.
2348 Returns 0 if SECTION doesn't exist. */
2349
2350 static int
2351 get_section_id (const struct dwarf2_section_info *section)
2352 {
2353 asection *sectp = get_section_bfd_section (section);
2354
2355 if (sectp == NULL)
2356 return 0;
2357 return sectp->id;
2358 }
2359
2360 /* Return the flags of SECTION.
2361 SECTION (or containing section if this is a virtual section) must exist. */
2362
2363 static int
2364 get_section_flags (const struct dwarf2_section_info *section)
2365 {
2366 asection *sectp = get_section_bfd_section (section);
2367
2368 gdb_assert (sectp != NULL);
2369 return bfd_get_section_flags (sectp->owner, sectp);
2370 }
2371
2372 /* When loading sections, we look either for uncompressed section or for
2373 compressed section names. */
2374
2375 static int
2376 section_is_p (const char *section_name,
2377 const struct dwarf2_section_names *names)
2378 {
2379 if (names->normal != NULL
2380 && strcmp (section_name, names->normal) == 0)
2381 return 1;
2382 if (names->compressed != NULL
2383 && strcmp (section_name, names->compressed) == 0)
2384 return 1;
2385 return 0;
2386 }
2387
2388 /* See declaration. */
2389
2390 void
2391 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2392 const dwarf2_debug_sections &names)
2393 {
2394 flagword aflag = bfd_get_section_flags (abfd, sectp);
2395
2396 if ((aflag & SEC_HAS_CONTENTS) == 0)
2397 {
2398 }
2399 else if (section_is_p (sectp->name, &names.info))
2400 {
2401 this->info.s.section = sectp;
2402 this->info.size = bfd_get_section_size (sectp);
2403 }
2404 else if (section_is_p (sectp->name, &names.abbrev))
2405 {
2406 this->abbrev.s.section = sectp;
2407 this->abbrev.size = bfd_get_section_size (sectp);
2408 }
2409 else if (section_is_p (sectp->name, &names.line))
2410 {
2411 this->line.s.section = sectp;
2412 this->line.size = bfd_get_section_size (sectp);
2413 }
2414 else if (section_is_p (sectp->name, &names.loc))
2415 {
2416 this->loc.s.section = sectp;
2417 this->loc.size = bfd_get_section_size (sectp);
2418 }
2419 else if (section_is_p (sectp->name, &names.loclists))
2420 {
2421 this->loclists.s.section = sectp;
2422 this->loclists.size = bfd_get_section_size (sectp);
2423 }
2424 else if (section_is_p (sectp->name, &names.macinfo))
2425 {
2426 this->macinfo.s.section = sectp;
2427 this->macinfo.size = bfd_get_section_size (sectp);
2428 }
2429 else if (section_is_p (sectp->name, &names.macro))
2430 {
2431 this->macro.s.section = sectp;
2432 this->macro.size = bfd_get_section_size (sectp);
2433 }
2434 else if (section_is_p (sectp->name, &names.str))
2435 {
2436 this->str.s.section = sectp;
2437 this->str.size = bfd_get_section_size (sectp);
2438 }
2439 else if (section_is_p (sectp->name, &names.line_str))
2440 {
2441 this->line_str.s.section = sectp;
2442 this->line_str.size = bfd_get_section_size (sectp);
2443 }
2444 else if (section_is_p (sectp->name, &names.addr))
2445 {
2446 this->addr.s.section = sectp;
2447 this->addr.size = bfd_get_section_size (sectp);
2448 }
2449 else if (section_is_p (sectp->name, &names.frame))
2450 {
2451 this->frame.s.section = sectp;
2452 this->frame.size = bfd_get_section_size (sectp);
2453 }
2454 else if (section_is_p (sectp->name, &names.eh_frame))
2455 {
2456 this->eh_frame.s.section = sectp;
2457 this->eh_frame.size = bfd_get_section_size (sectp);
2458 }
2459 else if (section_is_p (sectp->name, &names.ranges))
2460 {
2461 this->ranges.s.section = sectp;
2462 this->ranges.size = bfd_get_section_size (sectp);
2463 }
2464 else if (section_is_p (sectp->name, &names.rnglists))
2465 {
2466 this->rnglists.s.section = sectp;
2467 this->rnglists.size = bfd_get_section_size (sectp);
2468 }
2469 else if (section_is_p (sectp->name, &names.types))
2470 {
2471 struct dwarf2_section_info type_section;
2472
2473 memset (&type_section, 0, sizeof (type_section));
2474 type_section.s.section = sectp;
2475 type_section.size = bfd_get_section_size (sectp);
2476
2477 VEC_safe_push (dwarf2_section_info_def, this->types,
2478 &type_section);
2479 }
2480 else if (section_is_p (sectp->name, &names.gdb_index))
2481 {
2482 this->gdb_index.s.section = sectp;
2483 this->gdb_index.size = bfd_get_section_size (sectp);
2484 }
2485
2486 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
2487 && bfd_section_vma (abfd, sectp) == 0)
2488 this->has_section_at_zero = true;
2489 }
2490
2491 /* A helper function that decides whether a section is empty,
2492 or not present. */
2493
2494 static int
2495 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2496 {
2497 if (section->is_virtual)
2498 return section->size == 0;
2499 return section->s.section == NULL || section->size == 0;
2500 }
2501
2502 /* Read the contents of the section INFO.
2503 OBJFILE is the main object file, but not necessarily the file where
2504 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2505 of the DWO file.
2506 If the section is compressed, uncompress it before returning. */
2507
2508 static void
2509 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
2510 {
2511 asection *sectp;
2512 bfd *abfd;
2513 gdb_byte *buf, *retbuf;
2514
2515 if (info->readin)
2516 return;
2517 info->buffer = NULL;
2518 info->readin = 1;
2519
2520 if (dwarf2_section_empty_p (info))
2521 return;
2522
2523 sectp = get_section_bfd_section (info);
2524
2525 /* If this is a virtual section we need to read in the real one first. */
2526 if (info->is_virtual)
2527 {
2528 struct dwarf2_section_info *containing_section =
2529 get_containing_section (info);
2530
2531 gdb_assert (sectp != NULL);
2532 if ((sectp->flags & SEC_RELOC) != 0)
2533 {
2534 error (_("Dwarf Error: DWP format V2 with relocations is not"
2535 " supported in section %s [in module %s]"),
2536 get_section_name (info), get_section_file_name (info));
2537 }
2538 dwarf2_read_section (objfile, containing_section);
2539 /* Other code should have already caught virtual sections that don't
2540 fit. */
2541 gdb_assert (info->virtual_offset + info->size
2542 <= containing_section->size);
2543 /* If the real section is empty or there was a problem reading the
2544 section we shouldn't get here. */
2545 gdb_assert (containing_section->buffer != NULL);
2546 info->buffer = containing_section->buffer + info->virtual_offset;
2547 return;
2548 }
2549
2550 /* If the section has relocations, we must read it ourselves.
2551 Otherwise we attach it to the BFD. */
2552 if ((sectp->flags & SEC_RELOC) == 0)
2553 {
2554 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2555 return;
2556 }
2557
2558 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
2559 info->buffer = buf;
2560
2561 /* When debugging .o files, we may need to apply relocations; see
2562 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2563 We never compress sections in .o files, so we only need to
2564 try this when the section is not compressed. */
2565 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2566 if (retbuf != NULL)
2567 {
2568 info->buffer = retbuf;
2569 return;
2570 }
2571
2572 abfd = get_section_bfd_owner (info);
2573 gdb_assert (abfd != NULL);
2574
2575 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2576 || bfd_bread (buf, info->size, abfd) != info->size)
2577 {
2578 error (_("Dwarf Error: Can't read DWARF data"
2579 " in section %s [in module %s]"),
2580 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2581 }
2582 }
2583
2584 /* A helper function that returns the size of a section in a safe way.
2585 If you are positive that the section has been read before using the
2586 size, then it is safe to refer to the dwarf2_section_info object's
2587 "size" field directly. In other cases, you must call this
2588 function, because for compressed sections the size field is not set
2589 correctly until the section has been read. */
2590
2591 static bfd_size_type
2592 dwarf2_section_size (struct objfile *objfile,
2593 struct dwarf2_section_info *info)
2594 {
2595 if (!info->readin)
2596 dwarf2_read_section (objfile, info);
2597 return info->size;
2598 }
2599
2600 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2601 SECTION_NAME. */
2602
2603 void
2604 dwarf2_get_section_info (struct objfile *objfile,
2605 enum dwarf2_section_enum sect,
2606 asection **sectp, const gdb_byte **bufp,
2607 bfd_size_type *sizep)
2608 {
2609 struct dwarf2_per_objfile *data
2610 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2611 dwarf2_objfile_data_key);
2612 struct dwarf2_section_info *info;
2613
2614 /* We may see an objfile without any DWARF, in which case we just
2615 return nothing. */
2616 if (data == NULL)
2617 {
2618 *sectp = NULL;
2619 *bufp = NULL;
2620 *sizep = 0;
2621 return;
2622 }
2623 switch (sect)
2624 {
2625 case DWARF2_DEBUG_FRAME:
2626 info = &data->frame;
2627 break;
2628 case DWARF2_EH_FRAME:
2629 info = &data->eh_frame;
2630 break;
2631 default:
2632 gdb_assert_not_reached ("unexpected section");
2633 }
2634
2635 dwarf2_read_section (objfile, info);
2636
2637 *sectp = get_section_bfd_section (info);
2638 *bufp = info->buffer;
2639 *sizep = info->size;
2640 }
2641
2642 /* A helper function to find the sections for a .dwz file. */
2643
2644 static void
2645 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2646 {
2647 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2648
2649 /* Note that we only support the standard ELF names, because .dwz
2650 is ELF-only (at the time of writing). */
2651 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2652 {
2653 dwz_file->abbrev.s.section = sectp;
2654 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2655 }
2656 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2657 {
2658 dwz_file->info.s.section = sectp;
2659 dwz_file->info.size = bfd_get_section_size (sectp);
2660 }
2661 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2662 {
2663 dwz_file->str.s.section = sectp;
2664 dwz_file->str.size = bfd_get_section_size (sectp);
2665 }
2666 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2667 {
2668 dwz_file->line.s.section = sectp;
2669 dwz_file->line.size = bfd_get_section_size (sectp);
2670 }
2671 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2672 {
2673 dwz_file->macro.s.section = sectp;
2674 dwz_file->macro.size = bfd_get_section_size (sectp);
2675 }
2676 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2677 {
2678 dwz_file->gdb_index.s.section = sectp;
2679 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2680 }
2681 }
2682
2683 /* Open the separate '.dwz' debug file, if needed. Return NULL if
2684 there is no .gnu_debugaltlink section in the file. Error if there
2685 is such a section but the file cannot be found. */
2686
2687 static struct dwz_file *
2688 dwarf2_get_dwz_file (void)
2689 {
2690 char *data;
2691 struct cleanup *cleanup;
2692 const char *filename;
2693 struct dwz_file *result;
2694 bfd_size_type buildid_len_arg;
2695 size_t buildid_len;
2696 bfd_byte *buildid;
2697
2698 if (dwarf2_per_objfile->dwz_file != NULL)
2699 return dwarf2_per_objfile->dwz_file;
2700
2701 bfd_set_error (bfd_error_no_error);
2702 data = bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2703 &buildid_len_arg, &buildid);
2704 if (data == NULL)
2705 {
2706 if (bfd_get_error () == bfd_error_no_error)
2707 return NULL;
2708 error (_("could not read '.gnu_debugaltlink' section: %s"),
2709 bfd_errmsg (bfd_get_error ()));
2710 }
2711 cleanup = make_cleanup (xfree, data);
2712 make_cleanup (xfree, buildid);
2713
2714 buildid_len = (size_t) buildid_len_arg;
2715
2716 filename = (const char *) data;
2717
2718 std::string abs_storage;
2719 if (!IS_ABSOLUTE_PATH (filename))
2720 {
2721 gdb::unique_xmalloc_ptr<char> abs
2722 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2723
2724 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2725 filename = abs_storage.c_str ();
2726 }
2727
2728 /* First try the file name given in the section. If that doesn't
2729 work, try to use the build-id instead. */
2730 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2731 if (dwz_bfd != NULL)
2732 {
2733 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2734 dwz_bfd.release ();
2735 }
2736
2737 if (dwz_bfd == NULL)
2738 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2739
2740 if (dwz_bfd == NULL)
2741 error (_("could not find '.gnu_debugaltlink' file for %s"),
2742 objfile_name (dwarf2_per_objfile->objfile));
2743
2744 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2745 struct dwz_file);
2746 result->dwz_bfd = dwz_bfd.release ();
2747
2748 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
2749
2750 do_cleanups (cleanup);
2751
2752 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
2753 dwarf2_per_objfile->dwz_file = result;
2754 return result;
2755 }
2756 \f
2757 /* DWARF quick_symbols_functions support. */
2758
2759 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2760 unique line tables, so we maintain a separate table of all .debug_line
2761 derived entries to support the sharing.
2762 All the quick functions need is the list of file names. We discard the
2763 line_header when we're done and don't need to record it here. */
2764 struct quick_file_names
2765 {
2766 /* The data used to construct the hash key. */
2767 struct stmt_list_hash hash;
2768
2769 /* The number of entries in file_names, real_names. */
2770 unsigned int num_file_names;
2771
2772 /* The file names from the line table, after being run through
2773 file_full_name. */
2774 const char **file_names;
2775
2776 /* The file names from the line table after being run through
2777 gdb_realpath. These are computed lazily. */
2778 const char **real_names;
2779 };
2780
2781 /* When using the index (and thus not using psymtabs), each CU has an
2782 object of this type. This is used to hold information needed by
2783 the various "quick" methods. */
2784 struct dwarf2_per_cu_quick_data
2785 {
2786 /* The file table. This can be NULL if there was no file table
2787 or it's currently not read in.
2788 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2789 struct quick_file_names *file_names;
2790
2791 /* The corresponding symbol table. This is NULL if symbols for this
2792 CU have not yet been read. */
2793 struct compunit_symtab *compunit_symtab;
2794
2795 /* A temporary mark bit used when iterating over all CUs in
2796 expand_symtabs_matching. */
2797 unsigned int mark : 1;
2798
2799 /* True if we've tried to read the file table and found there isn't one.
2800 There will be no point in trying to read it again next time. */
2801 unsigned int no_file_data : 1;
2802 };
2803
2804 /* Utility hash function for a stmt_list_hash. */
2805
2806 static hashval_t
2807 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2808 {
2809 hashval_t v = 0;
2810
2811 if (stmt_list_hash->dwo_unit != NULL)
2812 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2813 v += to_underlying (stmt_list_hash->line_sect_off);
2814 return v;
2815 }
2816
2817 /* Utility equality function for a stmt_list_hash. */
2818
2819 static int
2820 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2821 const struct stmt_list_hash *rhs)
2822 {
2823 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2824 return 0;
2825 if (lhs->dwo_unit != NULL
2826 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2827 return 0;
2828
2829 return lhs->line_sect_off == rhs->line_sect_off;
2830 }
2831
2832 /* Hash function for a quick_file_names. */
2833
2834 static hashval_t
2835 hash_file_name_entry (const void *e)
2836 {
2837 const struct quick_file_names *file_data
2838 = (const struct quick_file_names *) e;
2839
2840 return hash_stmt_list_entry (&file_data->hash);
2841 }
2842
2843 /* Equality function for a quick_file_names. */
2844
2845 static int
2846 eq_file_name_entry (const void *a, const void *b)
2847 {
2848 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2849 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2850
2851 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2852 }
2853
2854 /* Delete function for a quick_file_names. */
2855
2856 static void
2857 delete_file_name_entry (void *e)
2858 {
2859 struct quick_file_names *file_data = (struct quick_file_names *) e;
2860 int i;
2861
2862 for (i = 0; i < file_data->num_file_names; ++i)
2863 {
2864 xfree ((void*) file_data->file_names[i]);
2865 if (file_data->real_names)
2866 xfree ((void*) file_data->real_names[i]);
2867 }
2868
2869 /* The space for the struct itself lives on objfile_obstack,
2870 so we don't free it here. */
2871 }
2872
2873 /* Create a quick_file_names hash table. */
2874
2875 static htab_t
2876 create_quick_file_names_table (unsigned int nr_initial_entries)
2877 {
2878 return htab_create_alloc (nr_initial_entries,
2879 hash_file_name_entry, eq_file_name_entry,
2880 delete_file_name_entry, xcalloc, xfree);
2881 }
2882
2883 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2884 have to be created afterwards. You should call age_cached_comp_units after
2885 processing PER_CU->CU. dw2_setup must have been already called. */
2886
2887 static void
2888 load_cu (struct dwarf2_per_cu_data *per_cu)
2889 {
2890 if (per_cu->is_debug_types)
2891 load_full_type_unit (per_cu);
2892 else
2893 load_full_comp_unit (per_cu, language_minimal);
2894
2895 if (per_cu->cu == NULL)
2896 return; /* Dummy CU. */
2897
2898 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2899 }
2900
2901 /* Read in the symbols for PER_CU. */
2902
2903 static void
2904 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2905 {
2906 struct cleanup *back_to;
2907
2908 /* Skip type_unit_groups, reading the type units they contain
2909 is handled elsewhere. */
2910 if (IS_TYPE_UNIT_GROUP (per_cu))
2911 return;
2912
2913 back_to = make_cleanup (dwarf2_release_queue, NULL);
2914
2915 if (dwarf2_per_objfile->using_index
2916 ? per_cu->v.quick->compunit_symtab == NULL
2917 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2918 {
2919 queue_comp_unit (per_cu, language_minimal);
2920 load_cu (per_cu);
2921
2922 /* If we just loaded a CU from a DWO, and we're working with an index
2923 that may badly handle TUs, load all the TUs in that DWO as well.
2924 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2925 if (!per_cu->is_debug_types
2926 && per_cu->cu != NULL
2927 && per_cu->cu->dwo_unit != NULL
2928 && dwarf2_per_objfile->index_table != NULL
2929 && dwarf2_per_objfile->index_table->version <= 7
2930 /* DWP files aren't supported yet. */
2931 && get_dwp_file () == NULL)
2932 queue_and_load_all_dwo_tus (per_cu);
2933 }
2934
2935 process_queue ();
2936
2937 /* Age the cache, releasing compilation units that have not
2938 been used recently. */
2939 age_cached_comp_units ();
2940
2941 do_cleanups (back_to);
2942 }
2943
2944 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2945 the objfile from which this CU came. Returns the resulting symbol
2946 table. */
2947
2948 static struct compunit_symtab *
2949 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2950 {
2951 gdb_assert (dwarf2_per_objfile->using_index);
2952 if (!per_cu->v.quick->compunit_symtab)
2953 {
2954 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2955 scoped_restore decrementer = increment_reading_symtab ();
2956 dw2_do_instantiate_symtab (per_cu);
2957 process_cu_includes ();
2958 do_cleanups (back_to);
2959 }
2960
2961 return per_cu->v.quick->compunit_symtab;
2962 }
2963
2964 /* Return the CU/TU given its index.
2965
2966 This is intended for loops like:
2967
2968 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2969 + dwarf2_per_objfile->n_type_units); ++i)
2970 {
2971 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
2972
2973 ...;
2974 }
2975 */
2976
2977 static struct dwarf2_per_cu_data *
2978 dw2_get_cutu (int index)
2979 {
2980 if (index >= dwarf2_per_objfile->n_comp_units)
2981 {
2982 index -= dwarf2_per_objfile->n_comp_units;
2983 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2984 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
2985 }
2986
2987 return dwarf2_per_objfile->all_comp_units[index];
2988 }
2989
2990 /* Return the CU given its index.
2991 This differs from dw2_get_cutu in that it's for when you know INDEX
2992 refers to a CU. */
2993
2994 static struct dwarf2_per_cu_data *
2995 dw2_get_cu (int index)
2996 {
2997 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
2998
2999 return dwarf2_per_objfile->all_comp_units[index];
3000 }
3001
3002 /* A helper for create_cus_from_index that handles a given list of
3003 CUs. */
3004
3005 static void
3006 create_cus_from_index_list (struct objfile *objfile,
3007 const gdb_byte *cu_list, offset_type n_elements,
3008 struct dwarf2_section_info *section,
3009 int is_dwz,
3010 int base_offset)
3011 {
3012 offset_type i;
3013
3014 for (i = 0; i < n_elements; i += 2)
3015 {
3016 gdb_static_assert (sizeof (ULONGEST) >= 8);
3017
3018 sect_offset sect_off
3019 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3020 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
3021 cu_list += 2 * 8;
3022
3023 dwarf2_per_cu_data *the_cu
3024 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3025 struct dwarf2_per_cu_data);
3026 the_cu->sect_off = sect_off;
3027 the_cu->length = length;
3028 the_cu->objfile = objfile;
3029 the_cu->section = section;
3030 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3031 struct dwarf2_per_cu_quick_data);
3032 the_cu->is_dwz = is_dwz;
3033 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
3034 }
3035 }
3036
3037 /* Read the CU list from the mapped index, and use it to create all
3038 the CU objects for this objfile. */
3039
3040 static void
3041 create_cus_from_index (struct objfile *objfile,
3042 const gdb_byte *cu_list, offset_type cu_list_elements,
3043 const gdb_byte *dwz_list, offset_type dwz_elements)
3044 {
3045 struct dwz_file *dwz;
3046
3047 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
3048 dwarf2_per_objfile->all_comp_units =
3049 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
3050 dwarf2_per_objfile->n_comp_units);
3051
3052 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
3053 &dwarf2_per_objfile->info, 0, 0);
3054
3055 if (dwz_elements == 0)
3056 return;
3057
3058 dwz = dwarf2_get_dwz_file ();
3059 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
3060 cu_list_elements / 2);
3061 }
3062
3063 /* Create the signatured type hash table from the index. */
3064
3065 static void
3066 create_signatured_type_table_from_index (struct objfile *objfile,
3067 struct dwarf2_section_info *section,
3068 const gdb_byte *bytes,
3069 offset_type elements)
3070 {
3071 offset_type i;
3072 htab_t sig_types_hash;
3073
3074 dwarf2_per_objfile->n_type_units
3075 = dwarf2_per_objfile->n_allocated_type_units
3076 = elements / 3;
3077 dwarf2_per_objfile->all_type_units =
3078 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3079
3080 sig_types_hash = allocate_signatured_type_table (objfile);
3081
3082 for (i = 0; i < elements; i += 3)
3083 {
3084 struct signatured_type *sig_type;
3085 ULONGEST signature;
3086 void **slot;
3087 cu_offset type_offset_in_tu;
3088
3089 gdb_static_assert (sizeof (ULONGEST) >= 8);
3090 sect_offset sect_off
3091 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3092 type_offset_in_tu
3093 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3094 BFD_ENDIAN_LITTLE);
3095 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3096 bytes += 3 * 8;
3097
3098 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3099 struct signatured_type);
3100 sig_type->signature = signature;
3101 sig_type->type_offset_in_tu = type_offset_in_tu;
3102 sig_type->per_cu.is_debug_types = 1;
3103 sig_type->per_cu.section = section;
3104 sig_type->per_cu.sect_off = sect_off;
3105 sig_type->per_cu.objfile = objfile;
3106 sig_type->per_cu.v.quick
3107 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3108 struct dwarf2_per_cu_quick_data);
3109
3110 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3111 *slot = sig_type;
3112
3113 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
3114 }
3115
3116 dwarf2_per_objfile->signatured_types = sig_types_hash;
3117 }
3118
3119 /* Read the address map data from the mapped index, and use it to
3120 populate the objfile's psymtabs_addrmap. */
3121
3122 static void
3123 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
3124 {
3125 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3126 const gdb_byte *iter, *end;
3127 struct addrmap *mutable_map;
3128 CORE_ADDR baseaddr;
3129
3130 auto_obstack temp_obstack;
3131
3132 mutable_map = addrmap_create_mutable (&temp_obstack);
3133
3134 iter = index->address_table;
3135 end = iter + index->address_table_size;
3136
3137 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3138
3139 while (iter < end)
3140 {
3141 ULONGEST hi, lo, cu_index;
3142 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3143 iter += 8;
3144 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3145 iter += 8;
3146 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3147 iter += 4;
3148
3149 if (lo > hi)
3150 {
3151 complaint (&symfile_complaints,
3152 _(".gdb_index address table has invalid range (%s - %s)"),
3153 hex_string (lo), hex_string (hi));
3154 continue;
3155 }
3156
3157 if (cu_index >= dwarf2_per_objfile->n_comp_units)
3158 {
3159 complaint (&symfile_complaints,
3160 _(".gdb_index address table has invalid CU number %u"),
3161 (unsigned) cu_index);
3162 continue;
3163 }
3164
3165 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3166 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
3167 addrmap_set_empty (mutable_map, lo, hi - 1, dw2_get_cutu (cu_index));
3168 }
3169
3170 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3171 &objfile->objfile_obstack);
3172 }
3173
3174 /* The hash function for strings in the mapped index. This is the same as
3175 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
3176 implementation. This is necessary because the hash function is tied to the
3177 format of the mapped index file. The hash values do not have to match with
3178 SYMBOL_HASH_NEXT.
3179
3180 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
3181
3182 static hashval_t
3183 mapped_index_string_hash (int index_version, const void *p)
3184 {
3185 const unsigned char *str = (const unsigned char *) p;
3186 hashval_t r = 0;
3187 unsigned char c;
3188
3189 while ((c = *str++) != 0)
3190 {
3191 if (index_version >= 5)
3192 c = tolower (c);
3193 r = r * 67 + c - 113;
3194 }
3195
3196 return r;
3197 }
3198
3199 /* Find a slot in the mapped index INDEX for the object named NAME.
3200 If NAME is found, set *VEC_OUT to point to the CU vector in the
3201 constant pool and return 1. If NAME cannot be found, return 0. */
3202
3203 static int
3204 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3205 offset_type **vec_out)
3206 {
3207 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
3208 offset_type hash;
3209 offset_type slot, step;
3210 int (*cmp) (const char *, const char *);
3211
3212 if (current_language->la_language == language_cplus
3213 || current_language->la_language == language_fortran
3214 || current_language->la_language == language_d)
3215 {
3216 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3217 not contain any. */
3218
3219 if (strchr (name, '(') != NULL)
3220 {
3221 char *without_params = cp_remove_params (name);
3222
3223 if (without_params != NULL)
3224 {
3225 make_cleanup (xfree, without_params);
3226 name = without_params;
3227 }
3228 }
3229 }
3230
3231 /* Index version 4 did not support case insensitive searches. But the
3232 indices for case insensitive languages are built in lowercase, therefore
3233 simulate our NAME being searched is also lowercased. */
3234 hash = mapped_index_string_hash ((index->version == 4
3235 && case_sensitivity == case_sensitive_off
3236 ? 5 : index->version),
3237 name);
3238
3239 slot = hash & (index->symbol_table_slots - 1);
3240 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
3241 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3242
3243 for (;;)
3244 {
3245 /* Convert a slot number to an offset into the table. */
3246 offset_type i = 2 * slot;
3247 const char *str;
3248 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
3249 {
3250 do_cleanups (back_to);
3251 return 0;
3252 }
3253
3254 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
3255 if (!cmp (name, str))
3256 {
3257 *vec_out = (offset_type *) (index->constant_pool
3258 + MAYBE_SWAP (index->symbol_table[i + 1]));
3259 do_cleanups (back_to);
3260 return 1;
3261 }
3262
3263 slot = (slot + step) & (index->symbol_table_slots - 1);
3264 }
3265 }
3266
3267 /* A helper function that reads the .gdb_index from SECTION and fills
3268 in MAP. FILENAME is the name of the file containing the section;
3269 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3270 ok to use deprecated sections.
3271
3272 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3273 out parameters that are filled in with information about the CU and
3274 TU lists in the section.
3275
3276 Returns 1 if all went well, 0 otherwise. */
3277
3278 static int
3279 read_index_from_section (struct objfile *objfile,
3280 const char *filename,
3281 int deprecated_ok,
3282 struct dwarf2_section_info *section,
3283 struct mapped_index *map,
3284 const gdb_byte **cu_list,
3285 offset_type *cu_list_elements,
3286 const gdb_byte **types_list,
3287 offset_type *types_list_elements)
3288 {
3289 const gdb_byte *addr;
3290 offset_type version;
3291 offset_type *metadata;
3292 int i;
3293
3294 if (dwarf2_section_empty_p (section))
3295 return 0;
3296
3297 /* Older elfutils strip versions could keep the section in the main
3298 executable while splitting it for the separate debug info file. */
3299 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
3300 return 0;
3301
3302 dwarf2_read_section (objfile, section);
3303
3304 addr = section->buffer;
3305 /* Version check. */
3306 version = MAYBE_SWAP (*(offset_type *) addr);
3307 /* Versions earlier than 3 emitted every copy of a psymbol. This
3308 causes the index to behave very poorly for certain requests. Version 3
3309 contained incomplete addrmap. So, it seems better to just ignore such
3310 indices. */
3311 if (version < 4)
3312 {
3313 static int warning_printed = 0;
3314 if (!warning_printed)
3315 {
3316 warning (_("Skipping obsolete .gdb_index section in %s."),
3317 filename);
3318 warning_printed = 1;
3319 }
3320 return 0;
3321 }
3322 /* Index version 4 uses a different hash function than index version
3323 5 and later.
3324
3325 Versions earlier than 6 did not emit psymbols for inlined
3326 functions. Using these files will cause GDB not to be able to
3327 set breakpoints on inlined functions by name, so we ignore these
3328 indices unless the user has done
3329 "set use-deprecated-index-sections on". */
3330 if (version < 6 && !deprecated_ok)
3331 {
3332 static int warning_printed = 0;
3333 if (!warning_printed)
3334 {
3335 warning (_("\
3336 Skipping deprecated .gdb_index section in %s.\n\
3337 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3338 to use the section anyway."),
3339 filename);
3340 warning_printed = 1;
3341 }
3342 return 0;
3343 }
3344 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3345 of the TU (for symbols coming from TUs),
3346 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3347 Plus gold-generated indices can have duplicate entries for global symbols,
3348 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3349 These are just performance bugs, and we can't distinguish gdb-generated
3350 indices from gold-generated ones, so issue no warning here. */
3351
3352 /* Indexes with higher version than the one supported by GDB may be no
3353 longer backward compatible. */
3354 if (version > 8)
3355 return 0;
3356
3357 map->version = version;
3358 map->total_size = section->size;
3359
3360 metadata = (offset_type *) (addr + sizeof (offset_type));
3361
3362 i = 0;
3363 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3364 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3365 / 8);
3366 ++i;
3367
3368 *types_list = addr + MAYBE_SWAP (metadata[i]);
3369 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3370 - MAYBE_SWAP (metadata[i]))
3371 / 8);
3372 ++i;
3373
3374 map->address_table = addr + MAYBE_SWAP (metadata[i]);
3375 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
3376 - MAYBE_SWAP (metadata[i]));
3377 ++i;
3378
3379 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
3380 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
3381 - MAYBE_SWAP (metadata[i]))
3382 / (2 * sizeof (offset_type)));
3383 ++i;
3384
3385 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3386
3387 return 1;
3388 }
3389
3390
3391 /* Read the index file. If everything went ok, initialize the "quick"
3392 elements of all the CUs and return 1. Otherwise, return 0. */
3393
3394 static int
3395 dwarf2_read_index (struct objfile *objfile)
3396 {
3397 struct mapped_index local_map, *map;
3398 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3399 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3400 struct dwz_file *dwz;
3401
3402 if (!read_index_from_section (objfile, objfile_name (objfile),
3403 use_deprecated_index_sections,
3404 &dwarf2_per_objfile->gdb_index, &local_map,
3405 &cu_list, &cu_list_elements,
3406 &types_list, &types_list_elements))
3407 return 0;
3408
3409 /* Don't use the index if it's empty. */
3410 if (local_map.symbol_table_slots == 0)
3411 return 0;
3412
3413 /* If there is a .dwz file, read it so we can get its CU list as
3414 well. */
3415 dwz = dwarf2_get_dwz_file ();
3416 if (dwz != NULL)
3417 {
3418 struct mapped_index dwz_map;
3419 const gdb_byte *dwz_types_ignore;
3420 offset_type dwz_types_elements_ignore;
3421
3422 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3423 1,
3424 &dwz->gdb_index, &dwz_map,
3425 &dwz_list, &dwz_list_elements,
3426 &dwz_types_ignore,
3427 &dwz_types_elements_ignore))
3428 {
3429 warning (_("could not read '.gdb_index' section from %s; skipping"),
3430 bfd_get_filename (dwz->dwz_bfd));
3431 return 0;
3432 }
3433 }
3434
3435 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3436 dwz_list_elements);
3437
3438 if (types_list_elements)
3439 {
3440 struct dwarf2_section_info *section;
3441
3442 /* We can only handle a single .debug_types when we have an
3443 index. */
3444 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3445 return 0;
3446
3447 section = VEC_index (dwarf2_section_info_def,
3448 dwarf2_per_objfile->types, 0);
3449
3450 create_signatured_type_table_from_index (objfile, section, types_list,
3451 types_list_elements);
3452 }
3453
3454 create_addrmap_from_index (objfile, &local_map);
3455
3456 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
3457 *map = local_map;
3458
3459 dwarf2_per_objfile->index_table = map;
3460 dwarf2_per_objfile->using_index = 1;
3461 dwarf2_per_objfile->quick_file_names_table =
3462 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3463
3464 return 1;
3465 }
3466
3467 /* A helper for the "quick" functions which sets the global
3468 dwarf2_per_objfile according to OBJFILE. */
3469
3470 static void
3471 dw2_setup (struct objfile *objfile)
3472 {
3473 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
3474 objfile_data (objfile, dwarf2_objfile_data_key));
3475 gdb_assert (dwarf2_per_objfile);
3476 }
3477
3478 /* die_reader_func for dw2_get_file_names. */
3479
3480 static void
3481 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3482 const gdb_byte *info_ptr,
3483 struct die_info *comp_unit_die,
3484 int has_children,
3485 void *data)
3486 {
3487 struct dwarf2_cu *cu = reader->cu;
3488 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3489 struct objfile *objfile = dwarf2_per_objfile->objfile;
3490 struct dwarf2_per_cu_data *lh_cu;
3491 struct attribute *attr;
3492 int i;
3493 void **slot;
3494 struct quick_file_names *qfn;
3495
3496 gdb_assert (! this_cu->is_debug_types);
3497
3498 /* Our callers never want to match partial units -- instead they
3499 will match the enclosing full CU. */
3500 if (comp_unit_die->tag == DW_TAG_partial_unit)
3501 {
3502 this_cu->v.quick->no_file_data = 1;
3503 return;
3504 }
3505
3506 lh_cu = this_cu;
3507 slot = NULL;
3508
3509 line_header_up lh;
3510 sect_offset line_offset {};
3511
3512 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3513 if (attr)
3514 {
3515 struct quick_file_names find_entry;
3516
3517 line_offset = (sect_offset) DW_UNSND (attr);
3518
3519 /* We may have already read in this line header (TU line header sharing).
3520 If we have we're done. */
3521 find_entry.hash.dwo_unit = cu->dwo_unit;
3522 find_entry.hash.line_sect_off = line_offset;
3523 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3524 &find_entry, INSERT);
3525 if (*slot != NULL)
3526 {
3527 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3528 return;
3529 }
3530
3531 lh = dwarf_decode_line_header (line_offset, cu);
3532 }
3533 if (lh == NULL)
3534 {
3535 lh_cu->v.quick->no_file_data = 1;
3536 return;
3537 }
3538
3539 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3540 qfn->hash.dwo_unit = cu->dwo_unit;
3541 qfn->hash.line_sect_off = line_offset;
3542 gdb_assert (slot != NULL);
3543 *slot = qfn;
3544
3545 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3546
3547 qfn->num_file_names = lh->file_names.size ();
3548 qfn->file_names =
3549 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3550 for (i = 0; i < lh->file_names.size (); ++i)
3551 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
3552 qfn->real_names = NULL;
3553
3554 lh_cu->v.quick->file_names = qfn;
3555 }
3556
3557 /* A helper for the "quick" functions which attempts to read the line
3558 table for THIS_CU. */
3559
3560 static struct quick_file_names *
3561 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3562 {
3563 /* This should never be called for TUs. */
3564 gdb_assert (! this_cu->is_debug_types);
3565 /* Nor type unit groups. */
3566 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3567
3568 if (this_cu->v.quick->file_names != NULL)
3569 return this_cu->v.quick->file_names;
3570 /* If we know there is no line data, no point in looking again. */
3571 if (this_cu->v.quick->no_file_data)
3572 return NULL;
3573
3574 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
3575
3576 if (this_cu->v.quick->no_file_data)
3577 return NULL;
3578 return this_cu->v.quick->file_names;
3579 }
3580
3581 /* A helper for the "quick" functions which computes and caches the
3582 real path for a given file name from the line table. */
3583
3584 static const char *
3585 dw2_get_real_path (struct objfile *objfile,
3586 struct quick_file_names *qfn, int index)
3587 {
3588 if (qfn->real_names == NULL)
3589 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3590 qfn->num_file_names, const char *);
3591
3592 if (qfn->real_names[index] == NULL)
3593 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3594
3595 return qfn->real_names[index];
3596 }
3597
3598 static struct symtab *
3599 dw2_find_last_source_symtab (struct objfile *objfile)
3600 {
3601 struct compunit_symtab *cust;
3602 int index;
3603
3604 dw2_setup (objfile);
3605 index = dwarf2_per_objfile->n_comp_units - 1;
3606 cust = dw2_instantiate_symtab (dw2_get_cutu (index));
3607 if (cust == NULL)
3608 return NULL;
3609 return compunit_primary_filetab (cust);
3610 }
3611
3612 /* Traversal function for dw2_forget_cached_source_info. */
3613
3614 static int
3615 dw2_free_cached_file_names (void **slot, void *info)
3616 {
3617 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3618
3619 if (file_data->real_names)
3620 {
3621 int i;
3622
3623 for (i = 0; i < file_data->num_file_names; ++i)
3624 {
3625 xfree ((void*) file_data->real_names[i]);
3626 file_data->real_names[i] = NULL;
3627 }
3628 }
3629
3630 return 1;
3631 }
3632
3633 static void
3634 dw2_forget_cached_source_info (struct objfile *objfile)
3635 {
3636 dw2_setup (objfile);
3637
3638 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3639 dw2_free_cached_file_names, NULL);
3640 }
3641
3642 /* Helper function for dw2_map_symtabs_matching_filename that expands
3643 the symtabs and calls the iterator. */
3644
3645 static int
3646 dw2_map_expand_apply (struct objfile *objfile,
3647 struct dwarf2_per_cu_data *per_cu,
3648 const char *name, const char *real_path,
3649 gdb::function_view<bool (symtab *)> callback)
3650 {
3651 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3652
3653 /* Don't visit already-expanded CUs. */
3654 if (per_cu->v.quick->compunit_symtab)
3655 return 0;
3656
3657 /* This may expand more than one symtab, and we want to iterate over
3658 all of them. */
3659 dw2_instantiate_symtab (per_cu);
3660
3661 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3662 last_made, callback);
3663 }
3664
3665 /* Implementation of the map_symtabs_matching_filename method. */
3666
3667 static bool
3668 dw2_map_symtabs_matching_filename
3669 (struct objfile *objfile, const char *name, const char *real_path,
3670 gdb::function_view<bool (symtab *)> callback)
3671 {
3672 int i;
3673 const char *name_basename = lbasename (name);
3674
3675 dw2_setup (objfile);
3676
3677 /* The rule is CUs specify all the files, including those used by
3678 any TU, so there's no need to scan TUs here. */
3679
3680 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3681 {
3682 int j;
3683 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3684 struct quick_file_names *file_data;
3685
3686 /* We only need to look at symtabs not already expanded. */
3687 if (per_cu->v.quick->compunit_symtab)
3688 continue;
3689
3690 file_data = dw2_get_file_names (per_cu);
3691 if (file_data == NULL)
3692 continue;
3693
3694 for (j = 0; j < file_data->num_file_names; ++j)
3695 {
3696 const char *this_name = file_data->file_names[j];
3697 const char *this_real_name;
3698
3699 if (compare_filenames_for_search (this_name, name))
3700 {
3701 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3702 callback))
3703 return true;
3704 continue;
3705 }
3706
3707 /* Before we invoke realpath, which can get expensive when many
3708 files are involved, do a quick comparison of the basenames. */
3709 if (! basenames_may_differ
3710 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3711 continue;
3712
3713 this_real_name = dw2_get_real_path (objfile, file_data, j);
3714 if (compare_filenames_for_search (this_real_name, name))
3715 {
3716 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3717 callback))
3718 return true;
3719 continue;
3720 }
3721
3722 if (real_path != NULL)
3723 {
3724 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3725 gdb_assert (IS_ABSOLUTE_PATH (name));
3726 if (this_real_name != NULL
3727 && FILENAME_CMP (real_path, this_real_name) == 0)
3728 {
3729 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3730 callback))
3731 return true;
3732 continue;
3733 }
3734 }
3735 }
3736 }
3737
3738 return false;
3739 }
3740
3741 /* Struct used to manage iterating over all CUs looking for a symbol. */
3742
3743 struct dw2_symtab_iterator
3744 {
3745 /* The internalized form of .gdb_index. */
3746 struct mapped_index *index;
3747 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3748 int want_specific_block;
3749 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3750 Unused if !WANT_SPECIFIC_BLOCK. */
3751 int block_index;
3752 /* The kind of symbol we're looking for. */
3753 domain_enum domain;
3754 /* The list of CUs from the index entry of the symbol,
3755 or NULL if not found. */
3756 offset_type *vec;
3757 /* The next element in VEC to look at. */
3758 int next;
3759 /* The number of elements in VEC, or zero if there is no match. */
3760 int length;
3761 /* Have we seen a global version of the symbol?
3762 If so we can ignore all further global instances.
3763 This is to work around gold/15646, inefficient gold-generated
3764 indices. */
3765 int global_seen;
3766 };
3767
3768 /* Initialize the index symtab iterator ITER.
3769 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3770 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
3771
3772 static void
3773 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3774 struct mapped_index *index,
3775 int want_specific_block,
3776 int block_index,
3777 domain_enum domain,
3778 const char *name)
3779 {
3780 iter->index = index;
3781 iter->want_specific_block = want_specific_block;
3782 iter->block_index = block_index;
3783 iter->domain = domain;
3784 iter->next = 0;
3785 iter->global_seen = 0;
3786
3787 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3788 iter->length = MAYBE_SWAP (*iter->vec);
3789 else
3790 {
3791 iter->vec = NULL;
3792 iter->length = 0;
3793 }
3794 }
3795
3796 /* Return the next matching CU or NULL if there are no more. */
3797
3798 static struct dwarf2_per_cu_data *
3799 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3800 {
3801 for ( ; iter->next < iter->length; ++iter->next)
3802 {
3803 offset_type cu_index_and_attrs =
3804 MAYBE_SWAP (iter->vec[iter->next + 1]);
3805 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3806 struct dwarf2_per_cu_data *per_cu;
3807 int want_static = iter->block_index != GLOBAL_BLOCK;
3808 /* This value is only valid for index versions >= 7. */
3809 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3810 gdb_index_symbol_kind symbol_kind =
3811 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3812 /* Only check the symbol attributes if they're present.
3813 Indices prior to version 7 don't record them,
3814 and indices >= 7 may elide them for certain symbols
3815 (gold does this). */
3816 int attrs_valid =
3817 (iter->index->version >= 7
3818 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3819
3820 /* Don't crash on bad data. */
3821 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3822 + dwarf2_per_objfile->n_type_units))
3823 {
3824 complaint (&symfile_complaints,
3825 _(".gdb_index entry has bad CU index"
3826 " [in module %s]"),
3827 objfile_name (dwarf2_per_objfile->objfile));
3828 continue;
3829 }
3830
3831 per_cu = dw2_get_cutu (cu_index);
3832
3833 /* Skip if already read in. */
3834 if (per_cu->v.quick->compunit_symtab)
3835 continue;
3836
3837 /* Check static vs global. */
3838 if (attrs_valid)
3839 {
3840 if (iter->want_specific_block
3841 && want_static != is_static)
3842 continue;
3843 /* Work around gold/15646. */
3844 if (!is_static && iter->global_seen)
3845 continue;
3846 if (!is_static)
3847 iter->global_seen = 1;
3848 }
3849
3850 /* Only check the symbol's kind if it has one. */
3851 if (attrs_valid)
3852 {
3853 switch (iter->domain)
3854 {
3855 case VAR_DOMAIN:
3856 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3857 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3858 /* Some types are also in VAR_DOMAIN. */
3859 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3860 continue;
3861 break;
3862 case STRUCT_DOMAIN:
3863 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3864 continue;
3865 break;
3866 case LABEL_DOMAIN:
3867 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3868 continue;
3869 break;
3870 default:
3871 break;
3872 }
3873 }
3874
3875 ++iter->next;
3876 return per_cu;
3877 }
3878
3879 return NULL;
3880 }
3881
3882 static struct compunit_symtab *
3883 dw2_lookup_symbol (struct objfile *objfile, int block_index,
3884 const char *name, domain_enum domain)
3885 {
3886 struct compunit_symtab *stab_best = NULL;
3887 struct mapped_index *index;
3888
3889 dw2_setup (objfile);
3890
3891 index = dwarf2_per_objfile->index_table;
3892
3893 /* index is NULL if OBJF_READNOW. */
3894 if (index)
3895 {
3896 struct dw2_symtab_iterator iter;
3897 struct dwarf2_per_cu_data *per_cu;
3898
3899 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
3900
3901 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3902 {
3903 struct symbol *sym, *with_opaque = NULL;
3904 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
3905 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3906 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3907
3908 sym = block_find_symbol (block, name, domain,
3909 block_find_non_opaque_type_preferred,
3910 &with_opaque);
3911
3912 /* Some caution must be observed with overloaded functions
3913 and methods, since the index will not contain any overload
3914 information (but NAME might contain it). */
3915
3916 if (sym != NULL
3917 && SYMBOL_MATCHES_SEARCH_NAME (sym, name))
3918 return stab;
3919 if (with_opaque != NULL
3920 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, name))
3921 stab_best = stab;
3922
3923 /* Keep looking through other CUs. */
3924 }
3925 }
3926
3927 return stab_best;
3928 }
3929
3930 static void
3931 dw2_print_stats (struct objfile *objfile)
3932 {
3933 int i, total, count;
3934
3935 dw2_setup (objfile);
3936 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
3937 count = 0;
3938 for (i = 0; i < total; ++i)
3939 {
3940 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
3941
3942 if (!per_cu->v.quick->compunit_symtab)
3943 ++count;
3944 }
3945 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3946 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3947 }
3948
3949 /* This dumps minimal information about the index.
3950 It is called via "mt print objfiles".
3951 One use is to verify .gdb_index has been loaded by the
3952 gdb.dwarf2/gdb-index.exp testcase. */
3953
3954 static void
3955 dw2_dump (struct objfile *objfile)
3956 {
3957 dw2_setup (objfile);
3958 gdb_assert (dwarf2_per_objfile->using_index);
3959 printf_filtered (".gdb_index:");
3960 if (dwarf2_per_objfile->index_table != NULL)
3961 {
3962 printf_filtered (" version %d\n",
3963 dwarf2_per_objfile->index_table->version);
3964 }
3965 else
3966 printf_filtered (" faked for \"readnow\"\n");
3967 printf_filtered ("\n");
3968 }
3969
3970 static void
3971 dw2_relocate (struct objfile *objfile,
3972 const struct section_offsets *new_offsets,
3973 const struct section_offsets *delta)
3974 {
3975 /* There's nothing to relocate here. */
3976 }
3977
3978 static void
3979 dw2_expand_symtabs_for_function (struct objfile *objfile,
3980 const char *func_name)
3981 {
3982 struct mapped_index *index;
3983
3984 dw2_setup (objfile);
3985
3986 index = dwarf2_per_objfile->index_table;
3987
3988 /* index is NULL if OBJF_READNOW. */
3989 if (index)
3990 {
3991 struct dw2_symtab_iterator iter;
3992 struct dwarf2_per_cu_data *per_cu;
3993
3994 /* Note: It doesn't matter what we pass for block_index here. */
3995 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3996 func_name);
3997
3998 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3999 dw2_instantiate_symtab (per_cu);
4000 }
4001 }
4002
4003 static void
4004 dw2_expand_all_symtabs (struct objfile *objfile)
4005 {
4006 int i;
4007
4008 dw2_setup (objfile);
4009
4010 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
4011 + dwarf2_per_objfile->n_type_units); ++i)
4012 {
4013 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
4014
4015 dw2_instantiate_symtab (per_cu);
4016 }
4017 }
4018
4019 static void
4020 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4021 const char *fullname)
4022 {
4023 int i;
4024
4025 dw2_setup (objfile);
4026
4027 /* We don't need to consider type units here.
4028 This is only called for examining code, e.g. expand_line_sal.
4029 There can be an order of magnitude (or more) more type units
4030 than comp units, and we avoid them if we can. */
4031
4032 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4033 {
4034 int j;
4035 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
4036 struct quick_file_names *file_data;
4037
4038 /* We only need to look at symtabs not already expanded. */
4039 if (per_cu->v.quick->compunit_symtab)
4040 continue;
4041
4042 file_data = dw2_get_file_names (per_cu);
4043 if (file_data == NULL)
4044 continue;
4045
4046 for (j = 0; j < file_data->num_file_names; ++j)
4047 {
4048 const char *this_fullname = file_data->file_names[j];
4049
4050 if (filename_cmp (this_fullname, fullname) == 0)
4051 {
4052 dw2_instantiate_symtab (per_cu);
4053 break;
4054 }
4055 }
4056 }
4057 }
4058
4059 static void
4060 dw2_map_matching_symbols (struct objfile *objfile,
4061 const char * name, domain_enum domain,
4062 int global,
4063 int (*callback) (struct block *,
4064 struct symbol *, void *),
4065 void *data, symbol_compare_ftype *match,
4066 symbol_compare_ftype *ordered_compare)
4067 {
4068 /* Currently unimplemented; used for Ada. The function can be called if the
4069 current language is Ada for a non-Ada objfile using GNU index. As Ada
4070 does not look for non-Ada symbols this function should just return. */
4071 }
4072
4073 static void
4074 dw2_expand_symtabs_matching
4075 (struct objfile *objfile,
4076 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4077 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4078 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4079 enum search_domain kind)
4080 {
4081 int i;
4082 offset_type iter;
4083 struct mapped_index *index;
4084
4085 dw2_setup (objfile);
4086
4087 /* index_table is NULL if OBJF_READNOW. */
4088 if (!dwarf2_per_objfile->index_table)
4089 return;
4090 index = dwarf2_per_objfile->index_table;
4091
4092 if (file_matcher != NULL)
4093 {
4094 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4095 htab_eq_pointer,
4096 NULL, xcalloc, xfree));
4097 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4098 htab_eq_pointer,
4099 NULL, xcalloc, xfree));
4100
4101 /* The rule is CUs specify all the files, including those used by
4102 any TU, so there's no need to scan TUs here. */
4103
4104 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4105 {
4106 int j;
4107 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4108 struct quick_file_names *file_data;
4109 void **slot;
4110
4111 QUIT;
4112
4113 per_cu->v.quick->mark = 0;
4114
4115 /* We only need to look at symtabs not already expanded. */
4116 if (per_cu->v.quick->compunit_symtab)
4117 continue;
4118
4119 file_data = dw2_get_file_names (per_cu);
4120 if (file_data == NULL)
4121 continue;
4122
4123 if (htab_find (visited_not_found.get (), file_data) != NULL)
4124 continue;
4125 else if (htab_find (visited_found.get (), file_data) != NULL)
4126 {
4127 per_cu->v.quick->mark = 1;
4128 continue;
4129 }
4130
4131 for (j = 0; j < file_data->num_file_names; ++j)
4132 {
4133 const char *this_real_name;
4134
4135 if (file_matcher (file_data->file_names[j], false))
4136 {
4137 per_cu->v.quick->mark = 1;
4138 break;
4139 }
4140
4141 /* Before we invoke realpath, which can get expensive when many
4142 files are involved, do a quick comparison of the basenames. */
4143 if (!basenames_may_differ
4144 && !file_matcher (lbasename (file_data->file_names[j]),
4145 true))
4146 continue;
4147
4148 this_real_name = dw2_get_real_path (objfile, file_data, j);
4149 if (file_matcher (this_real_name, false))
4150 {
4151 per_cu->v.quick->mark = 1;
4152 break;
4153 }
4154 }
4155
4156 slot = htab_find_slot (per_cu->v.quick->mark
4157 ? visited_found.get ()
4158 : visited_not_found.get (),
4159 file_data, INSERT);
4160 *slot = file_data;
4161 }
4162 }
4163
4164 for (iter = 0; iter < index->symbol_table_slots; ++iter)
4165 {
4166 offset_type idx = 2 * iter;
4167 const char *name;
4168 offset_type *vec, vec_len, vec_idx;
4169 int global_seen = 0;
4170
4171 QUIT;
4172
4173 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
4174 continue;
4175
4176 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
4177
4178 if (!symbol_matcher (name))
4179 continue;
4180
4181 /* The name was matched, now expand corresponding CUs that were
4182 marked. */
4183 vec = (offset_type *) (index->constant_pool
4184 + MAYBE_SWAP (index->symbol_table[idx + 1]));
4185 vec_len = MAYBE_SWAP (vec[0]);
4186 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4187 {
4188 struct dwarf2_per_cu_data *per_cu;
4189 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4190 /* This value is only valid for index versions >= 7. */
4191 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4192 gdb_index_symbol_kind symbol_kind =
4193 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4194 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4195 /* Only check the symbol attributes if they're present.
4196 Indices prior to version 7 don't record them,
4197 and indices >= 7 may elide them for certain symbols
4198 (gold does this). */
4199 int attrs_valid =
4200 (index->version >= 7
4201 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4202
4203 /* Work around gold/15646. */
4204 if (attrs_valid)
4205 {
4206 if (!is_static && global_seen)
4207 continue;
4208 if (!is_static)
4209 global_seen = 1;
4210 }
4211
4212 /* Only check the symbol's kind if it has one. */
4213 if (attrs_valid)
4214 {
4215 switch (kind)
4216 {
4217 case VARIABLES_DOMAIN:
4218 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4219 continue;
4220 break;
4221 case FUNCTIONS_DOMAIN:
4222 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4223 continue;
4224 break;
4225 case TYPES_DOMAIN:
4226 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4227 continue;
4228 break;
4229 default:
4230 break;
4231 }
4232 }
4233
4234 /* Don't crash on bad data. */
4235 if (cu_index >= (dwarf2_per_objfile->n_comp_units
4236 + dwarf2_per_objfile->n_type_units))
4237 {
4238 complaint (&symfile_complaints,
4239 _(".gdb_index entry has bad CU index"
4240 " [in module %s]"), objfile_name (objfile));
4241 continue;
4242 }
4243
4244 per_cu = dw2_get_cutu (cu_index);
4245 if (file_matcher == NULL || per_cu->v.quick->mark)
4246 {
4247 int symtab_was_null =
4248 (per_cu->v.quick->compunit_symtab == NULL);
4249
4250 dw2_instantiate_symtab (per_cu);
4251
4252 if (expansion_notify != NULL
4253 && symtab_was_null
4254 && per_cu->v.quick->compunit_symtab != NULL)
4255 {
4256 expansion_notify (per_cu->v.quick->compunit_symtab);
4257 }
4258 }
4259 }
4260 }
4261 }
4262
4263 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4264 symtab. */
4265
4266 static struct compunit_symtab *
4267 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4268 CORE_ADDR pc)
4269 {
4270 int i;
4271
4272 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4273 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4274 return cust;
4275
4276 if (cust->includes == NULL)
4277 return NULL;
4278
4279 for (i = 0; cust->includes[i]; ++i)
4280 {
4281 struct compunit_symtab *s = cust->includes[i];
4282
4283 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4284 if (s != NULL)
4285 return s;
4286 }
4287
4288 return NULL;
4289 }
4290
4291 static struct compunit_symtab *
4292 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4293 struct bound_minimal_symbol msymbol,
4294 CORE_ADDR pc,
4295 struct obj_section *section,
4296 int warn_if_readin)
4297 {
4298 struct dwarf2_per_cu_data *data;
4299 struct compunit_symtab *result;
4300
4301 dw2_setup (objfile);
4302
4303 if (!objfile->psymtabs_addrmap)
4304 return NULL;
4305
4306 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
4307 pc);
4308 if (!data)
4309 return NULL;
4310
4311 if (warn_if_readin && data->v.quick->compunit_symtab)
4312 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4313 paddress (get_objfile_arch (objfile), pc));
4314
4315 result
4316 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
4317 pc);
4318 gdb_assert (result != NULL);
4319 return result;
4320 }
4321
4322 static void
4323 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4324 void *data, int need_fullname)
4325 {
4326 dw2_setup (objfile);
4327
4328 if (!dwarf2_per_objfile->filenames_cache)
4329 {
4330 dwarf2_per_objfile->filenames_cache.emplace ();
4331
4332 htab_up visited (htab_create_alloc (10,
4333 htab_hash_pointer, htab_eq_pointer,
4334 NULL, xcalloc, xfree));
4335
4336 /* The rule is CUs specify all the files, including those used
4337 by any TU, so there's no need to scan TUs here. We can
4338 ignore file names coming from already-expanded CUs. */
4339
4340 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4341 {
4342 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
4343
4344 if (per_cu->v.quick->compunit_symtab)
4345 {
4346 void **slot = htab_find_slot (visited.get (),
4347 per_cu->v.quick->file_names,
4348 INSERT);
4349
4350 *slot = per_cu->v.quick->file_names;
4351 }
4352 }
4353
4354 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4355 {
4356 int j;
4357 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4358 struct quick_file_names *file_data;
4359 void **slot;
4360
4361 /* We only need to look at symtabs not already expanded. */
4362 if (per_cu->v.quick->compunit_symtab)
4363 continue;
4364
4365 file_data = dw2_get_file_names (per_cu);
4366 if (file_data == NULL)
4367 continue;
4368
4369 slot = htab_find_slot (visited.get (), file_data, INSERT);
4370 if (*slot)
4371 {
4372 /* Already visited. */
4373 continue;
4374 }
4375 *slot = file_data;
4376
4377 for (int j = 0; j < file_data->num_file_names; ++j)
4378 {
4379 const char *filename = file_data->file_names[j];
4380 dwarf2_per_objfile->filenames_cache->seen (filename);
4381 }
4382 }
4383 }
4384
4385 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
4386 {
4387 gdb::unique_xmalloc_ptr<char> this_real_name;
4388
4389 if (need_fullname)
4390 this_real_name = gdb_realpath (filename);
4391 (*fun) (filename, this_real_name.get (), data);
4392 });
4393 }
4394
4395 static int
4396 dw2_has_symbols (struct objfile *objfile)
4397 {
4398 return 1;
4399 }
4400
4401 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4402 {
4403 dw2_has_symbols,
4404 dw2_find_last_source_symtab,
4405 dw2_forget_cached_source_info,
4406 dw2_map_symtabs_matching_filename,
4407 dw2_lookup_symbol,
4408 dw2_print_stats,
4409 dw2_dump,
4410 dw2_relocate,
4411 dw2_expand_symtabs_for_function,
4412 dw2_expand_all_symtabs,
4413 dw2_expand_symtabs_with_fullname,
4414 dw2_map_matching_symbols,
4415 dw2_expand_symtabs_matching,
4416 dw2_find_pc_sect_compunit_symtab,
4417 dw2_map_symbol_filenames
4418 };
4419
4420 /* Initialize for reading DWARF for this objfile. Return 0 if this
4421 file will use psymtabs, or 1 if using the GNU index. */
4422
4423 int
4424 dwarf2_initialize_objfile (struct objfile *objfile)
4425 {
4426 /* If we're about to read full symbols, don't bother with the
4427 indices. In this case we also don't care if some other debug
4428 format is making psymtabs, because they are all about to be
4429 expanded anyway. */
4430 if ((objfile->flags & OBJF_READNOW))
4431 {
4432 int i;
4433
4434 dwarf2_per_objfile->using_index = 1;
4435 create_all_comp_units (objfile);
4436 create_all_type_units (objfile);
4437 dwarf2_per_objfile->quick_file_names_table =
4438 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
4439
4440 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
4441 + dwarf2_per_objfile->n_type_units); ++i)
4442 {
4443 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
4444
4445 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4446 struct dwarf2_per_cu_quick_data);
4447 }
4448
4449 /* Return 1 so that gdb sees the "quick" functions. However,
4450 these functions will be no-ops because we will have expanded
4451 all symtabs. */
4452 return 1;
4453 }
4454
4455 if (dwarf2_read_index (objfile))
4456 return 1;
4457
4458 return 0;
4459 }
4460
4461 \f
4462
4463 /* Build a partial symbol table. */
4464
4465 void
4466 dwarf2_build_psymtabs (struct objfile *objfile)
4467 {
4468
4469 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
4470 {
4471 init_psymbol_list (objfile, 1024);
4472 }
4473
4474 TRY
4475 {
4476 /* This isn't really ideal: all the data we allocate on the
4477 objfile's obstack is still uselessly kept around. However,
4478 freeing it seems unsafe. */
4479 psymtab_discarder psymtabs (objfile);
4480 dwarf2_build_psymtabs_hard (objfile);
4481 psymtabs.keep ();
4482 }
4483 CATCH (except, RETURN_MASK_ERROR)
4484 {
4485 exception_print (gdb_stderr, except);
4486 }
4487 END_CATCH
4488 }
4489
4490 /* Return the total length of the CU described by HEADER. */
4491
4492 static unsigned int
4493 get_cu_length (const struct comp_unit_head *header)
4494 {
4495 return header->initial_length_size + header->length;
4496 }
4497
4498 /* Return TRUE if SECT_OFF is within CU_HEADER. */
4499
4500 static inline bool
4501 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
4502 {
4503 sect_offset bottom = cu_header->sect_off;
4504 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
4505
4506 return sect_off >= bottom && sect_off < top;
4507 }
4508
4509 /* Find the base address of the compilation unit for range lists and
4510 location lists. It will normally be specified by DW_AT_low_pc.
4511 In DWARF-3 draft 4, the base address could be overridden by
4512 DW_AT_entry_pc. It's been removed, but GCC still uses this for
4513 compilation units with discontinuous ranges. */
4514
4515 static void
4516 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
4517 {
4518 struct attribute *attr;
4519
4520 cu->base_known = 0;
4521 cu->base_address = 0;
4522
4523 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
4524 if (attr)
4525 {
4526 cu->base_address = attr_value_as_address (attr);
4527 cu->base_known = 1;
4528 }
4529 else
4530 {
4531 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
4532 if (attr)
4533 {
4534 cu->base_address = attr_value_as_address (attr);
4535 cu->base_known = 1;
4536 }
4537 }
4538 }
4539
4540 /* Read in the comp unit header information from the debug_info at info_ptr.
4541 Use rcuh_kind::COMPILE as the default type if not known by the caller.
4542 NOTE: This leaves members offset, first_die_offset to be filled in
4543 by the caller. */
4544
4545 static const gdb_byte *
4546 read_comp_unit_head (struct comp_unit_head *cu_header,
4547 const gdb_byte *info_ptr,
4548 struct dwarf2_section_info *section,
4549 rcuh_kind section_kind)
4550 {
4551 int signed_addr;
4552 unsigned int bytes_read;
4553 const char *filename = get_section_file_name (section);
4554 bfd *abfd = get_section_bfd_owner (section);
4555
4556 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
4557 cu_header->initial_length_size = bytes_read;
4558 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
4559 info_ptr += bytes_read;
4560 cu_header->version = read_2_bytes (abfd, info_ptr);
4561 info_ptr += 2;
4562 if (cu_header->version < 5)
4563 switch (section_kind)
4564 {
4565 case rcuh_kind::COMPILE:
4566 cu_header->unit_type = DW_UT_compile;
4567 break;
4568 case rcuh_kind::TYPE:
4569 cu_header->unit_type = DW_UT_type;
4570 break;
4571 default:
4572 internal_error (__FILE__, __LINE__,
4573 _("read_comp_unit_head: invalid section_kind"));
4574 }
4575 else
4576 {
4577 cu_header->unit_type = static_cast<enum dwarf_unit_type>
4578 (read_1_byte (abfd, info_ptr));
4579 info_ptr += 1;
4580 switch (cu_header->unit_type)
4581 {
4582 case DW_UT_compile:
4583 if (section_kind != rcuh_kind::COMPILE)
4584 error (_("Dwarf Error: wrong unit_type in compilation unit header "
4585 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
4586 filename);
4587 break;
4588 case DW_UT_type:
4589 section_kind = rcuh_kind::TYPE;
4590 break;
4591 default:
4592 error (_("Dwarf Error: wrong unit_type in compilation unit header "
4593 "(is %d, should be %d or %d) [in module %s]"),
4594 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
4595 }
4596
4597 cu_header->addr_size = read_1_byte (abfd, info_ptr);
4598 info_ptr += 1;
4599 }
4600 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
4601 cu_header,
4602 &bytes_read);
4603 info_ptr += bytes_read;
4604 if (cu_header->version < 5)
4605 {
4606 cu_header->addr_size = read_1_byte (abfd, info_ptr);
4607 info_ptr += 1;
4608 }
4609 signed_addr = bfd_get_sign_extend_vma (abfd);
4610 if (signed_addr < 0)
4611 internal_error (__FILE__, __LINE__,
4612 _("read_comp_unit_head: dwarf from non elf file"));
4613 cu_header->signed_addr_p = signed_addr;
4614
4615 if (section_kind == rcuh_kind::TYPE)
4616 {
4617 LONGEST type_offset;
4618
4619 cu_header->signature = read_8_bytes (abfd, info_ptr);
4620 info_ptr += 8;
4621
4622 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
4623 info_ptr += bytes_read;
4624 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
4625 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
4626 error (_("Dwarf Error: Too big type_offset in compilation unit "
4627 "header (is %s) [in module %s]"), plongest (type_offset),
4628 filename);
4629 }
4630
4631 return info_ptr;
4632 }
4633
4634 /* Helper function that returns the proper abbrev section for
4635 THIS_CU. */
4636
4637 static struct dwarf2_section_info *
4638 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
4639 {
4640 struct dwarf2_section_info *abbrev;
4641
4642 if (this_cu->is_dwz)
4643 abbrev = &dwarf2_get_dwz_file ()->abbrev;
4644 else
4645 abbrev = &dwarf2_per_objfile->abbrev;
4646
4647 return abbrev;
4648 }
4649
4650 /* Subroutine of read_and_check_comp_unit_head and
4651 read_and_check_type_unit_head to simplify them.
4652 Perform various error checking on the header. */
4653
4654 static void
4655 error_check_comp_unit_head (struct comp_unit_head *header,
4656 struct dwarf2_section_info *section,
4657 struct dwarf2_section_info *abbrev_section)
4658 {
4659 const char *filename = get_section_file_name (section);
4660
4661 if (header->version < 2 || header->version > 5)
4662 error (_("Dwarf Error: wrong version in compilation unit header "
4663 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
4664 filename);
4665
4666 if (to_underlying (header->abbrev_sect_off)
4667 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
4668 error (_("Dwarf Error: bad offset (0x%x) in compilation unit header "
4669 "(offset 0x%x + 6) [in module %s]"),
4670 to_underlying (header->abbrev_sect_off),
4671 to_underlying (header->sect_off),
4672 filename);
4673
4674 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
4675 avoid potential 32-bit overflow. */
4676 if (((ULONGEST) header->sect_off + get_cu_length (header))
4677 > section->size)
4678 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
4679 "(offset 0x%x + 0) [in module %s]"),
4680 header->length, to_underlying (header->sect_off),
4681 filename);
4682 }
4683
4684 /* Read in a CU/TU header and perform some basic error checking.
4685 The contents of the header are stored in HEADER.
4686 The result is a pointer to the start of the first DIE. */
4687
4688 static const gdb_byte *
4689 read_and_check_comp_unit_head (struct comp_unit_head *header,
4690 struct dwarf2_section_info *section,
4691 struct dwarf2_section_info *abbrev_section,
4692 const gdb_byte *info_ptr,
4693 rcuh_kind section_kind)
4694 {
4695 const gdb_byte *beg_of_comp_unit = info_ptr;
4696 bfd *abfd = get_section_bfd_owner (section);
4697
4698 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
4699
4700 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
4701
4702 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
4703
4704 error_check_comp_unit_head (header, section, abbrev_section);
4705
4706 return info_ptr;
4707 }
4708
4709 /* Fetch the abbreviation table offset from a comp or type unit header. */
4710
4711 static sect_offset
4712 read_abbrev_offset (struct dwarf2_section_info *section,
4713 sect_offset sect_off)
4714 {
4715 bfd *abfd = get_section_bfd_owner (section);
4716 const gdb_byte *info_ptr;
4717 unsigned int initial_length_size, offset_size;
4718 uint16_t version;
4719
4720 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4721 info_ptr = section->buffer + to_underlying (sect_off);
4722 read_initial_length (abfd, info_ptr, &initial_length_size);
4723 offset_size = initial_length_size == 4 ? 4 : 8;
4724 info_ptr += initial_length_size;
4725
4726 version = read_2_bytes (abfd, info_ptr);
4727 info_ptr += 2;
4728 if (version >= 5)
4729 {
4730 /* Skip unit type and address size. */
4731 info_ptr += 2;
4732 }
4733
4734 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
4735 }
4736
4737 /* Allocate a new partial symtab for file named NAME and mark this new
4738 partial symtab as being an include of PST. */
4739
4740 static void
4741 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
4742 struct objfile *objfile)
4743 {
4744 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4745
4746 if (!IS_ABSOLUTE_PATH (subpst->filename))
4747 {
4748 /* It shares objfile->objfile_obstack. */
4749 subpst->dirname = pst->dirname;
4750 }
4751
4752 subpst->textlow = 0;
4753 subpst->texthigh = 0;
4754
4755 subpst->dependencies
4756 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
4757 subpst->dependencies[0] = pst;
4758 subpst->number_of_dependencies = 1;
4759
4760 subpst->globals_offset = 0;
4761 subpst->n_global_syms = 0;
4762 subpst->statics_offset = 0;
4763 subpst->n_static_syms = 0;
4764 subpst->compunit_symtab = NULL;
4765 subpst->read_symtab = pst->read_symtab;
4766 subpst->readin = 0;
4767
4768 /* No private part is necessary for include psymtabs. This property
4769 can be used to differentiate between such include psymtabs and
4770 the regular ones. */
4771 subpst->read_symtab_private = NULL;
4772 }
4773
4774 /* Read the Line Number Program data and extract the list of files
4775 included by the source file represented by PST. Build an include
4776 partial symtab for each of these included files. */
4777
4778 static void
4779 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
4780 struct die_info *die,
4781 struct partial_symtab *pst)
4782 {
4783 line_header_up lh;
4784 struct attribute *attr;
4785
4786 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4787 if (attr)
4788 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
4789 if (lh == NULL)
4790 return; /* No linetable, so no includes. */
4791
4792 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
4793 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
4794 }
4795
4796 static hashval_t
4797 hash_signatured_type (const void *item)
4798 {
4799 const struct signatured_type *sig_type
4800 = (const struct signatured_type *) item;
4801
4802 /* This drops the top 32 bits of the signature, but is ok for a hash. */
4803 return sig_type->signature;
4804 }
4805
4806 static int
4807 eq_signatured_type (const void *item_lhs, const void *item_rhs)
4808 {
4809 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
4810 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
4811
4812 return lhs->signature == rhs->signature;
4813 }
4814
4815 /* Allocate a hash table for signatured types. */
4816
4817 static htab_t
4818 allocate_signatured_type_table (struct objfile *objfile)
4819 {
4820 return htab_create_alloc_ex (41,
4821 hash_signatured_type,
4822 eq_signatured_type,
4823 NULL,
4824 &objfile->objfile_obstack,
4825 hashtab_obstack_allocate,
4826 dummy_obstack_deallocate);
4827 }
4828
4829 /* A helper function to add a signatured type CU to a table. */
4830
4831 static int
4832 add_signatured_type_cu_to_table (void **slot, void *datum)
4833 {
4834 struct signatured_type *sigt = (struct signatured_type *) *slot;
4835 struct signatured_type ***datap = (struct signatured_type ***) datum;
4836
4837 **datap = sigt;
4838 ++*datap;
4839
4840 return 1;
4841 }
4842
4843 /* A helper for create_debug_types_hash_table. Read types from SECTION
4844 and fill them into TYPES_HTAB. It will process only type units,
4845 therefore DW_UT_type. */
4846
4847 static void
4848 create_debug_type_hash_table (struct dwo_file *dwo_file,
4849 dwarf2_section_info *section, htab_t &types_htab,
4850 rcuh_kind section_kind)
4851 {
4852 struct objfile *objfile = dwarf2_per_objfile->objfile;
4853 struct dwarf2_section_info *abbrev_section;
4854 bfd *abfd;
4855 const gdb_byte *info_ptr, *end_ptr;
4856
4857 abbrev_section = (dwo_file != NULL
4858 ? &dwo_file->sections.abbrev
4859 : &dwarf2_per_objfile->abbrev);
4860
4861 if (dwarf_read_debug)
4862 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
4863 get_section_name (section),
4864 get_section_file_name (abbrev_section));
4865
4866 dwarf2_read_section (objfile, section);
4867 info_ptr = section->buffer;
4868
4869 if (info_ptr == NULL)
4870 return;
4871
4872 /* We can't set abfd until now because the section may be empty or
4873 not present, in which case the bfd is unknown. */
4874 abfd = get_section_bfd_owner (section);
4875
4876 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4877 because we don't need to read any dies: the signature is in the
4878 header. */
4879
4880 end_ptr = info_ptr + section->size;
4881 while (info_ptr < end_ptr)
4882 {
4883 struct signatured_type *sig_type;
4884 struct dwo_unit *dwo_tu;
4885 void **slot;
4886 const gdb_byte *ptr = info_ptr;
4887 struct comp_unit_head header;
4888 unsigned int length;
4889
4890 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
4891
4892 /* Initialize it due to a false compiler warning. */
4893 header.signature = -1;
4894 header.type_cu_offset_in_tu = (cu_offset) -1;
4895
4896 /* We need to read the type's signature in order to build the hash
4897 table, but we don't need anything else just yet. */
4898
4899 ptr = read_and_check_comp_unit_head (&header, section,
4900 abbrev_section, ptr, section_kind);
4901
4902 length = get_cu_length (&header);
4903
4904 /* Skip dummy type units. */
4905 if (ptr >= info_ptr + length
4906 || peek_abbrev_code (abfd, ptr) == 0
4907 || header.unit_type != DW_UT_type)
4908 {
4909 info_ptr += length;
4910 continue;
4911 }
4912
4913 if (types_htab == NULL)
4914 {
4915 if (dwo_file)
4916 types_htab = allocate_dwo_unit_table (objfile);
4917 else
4918 types_htab = allocate_signatured_type_table (objfile);
4919 }
4920
4921 if (dwo_file)
4922 {
4923 sig_type = NULL;
4924 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4925 struct dwo_unit);
4926 dwo_tu->dwo_file = dwo_file;
4927 dwo_tu->signature = header.signature;
4928 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
4929 dwo_tu->section = section;
4930 dwo_tu->sect_off = sect_off;
4931 dwo_tu->length = length;
4932 }
4933 else
4934 {
4935 /* N.B.: type_offset is not usable if this type uses a DWO file.
4936 The real type_offset is in the DWO file. */
4937 dwo_tu = NULL;
4938 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4939 struct signatured_type);
4940 sig_type->signature = header.signature;
4941 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
4942 sig_type->per_cu.objfile = objfile;
4943 sig_type->per_cu.is_debug_types = 1;
4944 sig_type->per_cu.section = section;
4945 sig_type->per_cu.sect_off = sect_off;
4946 sig_type->per_cu.length = length;
4947 }
4948
4949 slot = htab_find_slot (types_htab,
4950 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4951 INSERT);
4952 gdb_assert (slot != NULL);
4953 if (*slot != NULL)
4954 {
4955 sect_offset dup_sect_off;
4956
4957 if (dwo_file)
4958 {
4959 const struct dwo_unit *dup_tu
4960 = (const struct dwo_unit *) *slot;
4961
4962 dup_sect_off = dup_tu->sect_off;
4963 }
4964 else
4965 {
4966 const struct signatured_type *dup_tu
4967 = (const struct signatured_type *) *slot;
4968
4969 dup_sect_off = dup_tu->per_cu.sect_off;
4970 }
4971
4972 complaint (&symfile_complaints,
4973 _("debug type entry at offset 0x%x is duplicate to"
4974 " the entry at offset 0x%x, signature %s"),
4975 to_underlying (sect_off), to_underlying (dup_sect_off),
4976 hex_string (header.signature));
4977 }
4978 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
4979
4980 if (dwarf_read_debug > 1)
4981 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
4982 to_underlying (sect_off),
4983 hex_string (header.signature));
4984
4985 info_ptr += length;
4986 }
4987 }
4988
4989 /* Create the hash table of all entries in the .debug_types
4990 (or .debug_types.dwo) section(s).
4991 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
4992 otherwise it is NULL.
4993
4994 The result is a pointer to the hash table or NULL if there are no types.
4995
4996 Note: This function processes DWO files only, not DWP files. */
4997
4998 static void
4999 create_debug_types_hash_table (struct dwo_file *dwo_file,
5000 VEC (dwarf2_section_info_def) *types,
5001 htab_t &types_htab)
5002 {
5003 int ix;
5004 struct dwarf2_section_info *section;
5005
5006 if (VEC_empty (dwarf2_section_info_def, types))
5007 return;
5008
5009 for (ix = 0;
5010 VEC_iterate (dwarf2_section_info_def, types, ix, section);
5011 ++ix)
5012 create_debug_type_hash_table (dwo_file, section, types_htab,
5013 rcuh_kind::TYPE);
5014 }
5015
5016 /* Create the hash table of all entries in the .debug_types section,
5017 and initialize all_type_units.
5018 The result is zero if there is an error (e.g. missing .debug_types section),
5019 otherwise non-zero. */
5020
5021 static int
5022 create_all_type_units (struct objfile *objfile)
5023 {
5024 htab_t types_htab = NULL;
5025 struct signatured_type **iter;
5026
5027 create_debug_type_hash_table (NULL, &dwarf2_per_objfile->info, types_htab,
5028 rcuh_kind::COMPILE);
5029 create_debug_types_hash_table (NULL, dwarf2_per_objfile->types, types_htab);
5030 if (types_htab == NULL)
5031 {
5032 dwarf2_per_objfile->signatured_types = NULL;
5033 return 0;
5034 }
5035
5036 dwarf2_per_objfile->signatured_types = types_htab;
5037
5038 dwarf2_per_objfile->n_type_units
5039 = dwarf2_per_objfile->n_allocated_type_units
5040 = htab_elements (types_htab);
5041 dwarf2_per_objfile->all_type_units =
5042 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
5043 iter = &dwarf2_per_objfile->all_type_units[0];
5044 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
5045 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
5046 == dwarf2_per_objfile->n_type_units);
5047
5048 return 1;
5049 }
5050
5051 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
5052 If SLOT is non-NULL, it is the entry to use in the hash table.
5053 Otherwise we find one. */
5054
5055 static struct signatured_type *
5056 add_type_unit (ULONGEST sig, void **slot)
5057 {
5058 struct objfile *objfile = dwarf2_per_objfile->objfile;
5059 int n_type_units = dwarf2_per_objfile->n_type_units;
5060 struct signatured_type *sig_type;
5061
5062 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
5063 ++n_type_units;
5064 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
5065 {
5066 if (dwarf2_per_objfile->n_allocated_type_units == 0)
5067 dwarf2_per_objfile->n_allocated_type_units = 1;
5068 dwarf2_per_objfile->n_allocated_type_units *= 2;
5069 dwarf2_per_objfile->all_type_units
5070 = XRESIZEVEC (struct signatured_type *,
5071 dwarf2_per_objfile->all_type_units,
5072 dwarf2_per_objfile->n_allocated_type_units);
5073 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
5074 }
5075 dwarf2_per_objfile->n_type_units = n_type_units;
5076
5077 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5078 struct signatured_type);
5079 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
5080 sig_type->signature = sig;
5081 sig_type->per_cu.is_debug_types = 1;
5082 if (dwarf2_per_objfile->using_index)
5083 {
5084 sig_type->per_cu.v.quick =
5085 OBSTACK_ZALLOC (&objfile->objfile_obstack,
5086 struct dwarf2_per_cu_quick_data);
5087 }
5088
5089 if (slot == NULL)
5090 {
5091 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5092 sig_type, INSERT);
5093 }
5094 gdb_assert (*slot == NULL);
5095 *slot = sig_type;
5096 /* The rest of sig_type must be filled in by the caller. */
5097 return sig_type;
5098 }
5099
5100 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
5101 Fill in SIG_ENTRY with DWO_ENTRY. */
5102
5103 static void
5104 fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
5105 struct signatured_type *sig_entry,
5106 struct dwo_unit *dwo_entry)
5107 {
5108 /* Make sure we're not clobbering something we don't expect to. */
5109 gdb_assert (! sig_entry->per_cu.queued);
5110 gdb_assert (sig_entry->per_cu.cu == NULL);
5111 if (dwarf2_per_objfile->using_index)
5112 {
5113 gdb_assert (sig_entry->per_cu.v.quick != NULL);
5114 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
5115 }
5116 else
5117 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
5118 gdb_assert (sig_entry->signature == dwo_entry->signature);
5119 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
5120 gdb_assert (sig_entry->type_unit_group == NULL);
5121 gdb_assert (sig_entry->dwo_unit == NULL);
5122
5123 sig_entry->per_cu.section = dwo_entry->section;
5124 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
5125 sig_entry->per_cu.length = dwo_entry->length;
5126 sig_entry->per_cu.reading_dwo_directly = 1;
5127 sig_entry->per_cu.objfile = objfile;
5128 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
5129 sig_entry->dwo_unit = dwo_entry;
5130 }
5131
5132 /* Subroutine of lookup_signatured_type.
5133 If we haven't read the TU yet, create the signatured_type data structure
5134 for a TU to be read in directly from a DWO file, bypassing the stub.
5135 This is the "Stay in DWO Optimization": When there is no DWP file and we're
5136 using .gdb_index, then when reading a CU we want to stay in the DWO file
5137 containing that CU. Otherwise we could end up reading several other DWO
5138 files (due to comdat folding) to process the transitive closure of all the
5139 mentioned TUs, and that can be slow. The current DWO file will have every
5140 type signature that it needs.
5141 We only do this for .gdb_index because in the psymtab case we already have
5142 to read all the DWOs to build the type unit groups. */
5143
5144 static struct signatured_type *
5145 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5146 {
5147 struct objfile *objfile = dwarf2_per_objfile->objfile;
5148 struct dwo_file *dwo_file;
5149 struct dwo_unit find_dwo_entry, *dwo_entry;
5150 struct signatured_type find_sig_entry, *sig_entry;
5151 void **slot;
5152
5153 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
5154
5155 /* If TU skeletons have been removed then we may not have read in any
5156 TUs yet. */
5157 if (dwarf2_per_objfile->signatured_types == NULL)
5158 {
5159 dwarf2_per_objfile->signatured_types
5160 = allocate_signatured_type_table (objfile);
5161 }
5162
5163 /* We only ever need to read in one copy of a signatured type.
5164 Use the global signatured_types array to do our own comdat-folding
5165 of types. If this is the first time we're reading this TU, and
5166 the TU has an entry in .gdb_index, replace the recorded data from
5167 .gdb_index with this TU. */
5168
5169 find_sig_entry.signature = sig;
5170 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5171 &find_sig_entry, INSERT);
5172 sig_entry = (struct signatured_type *) *slot;
5173
5174 /* We can get here with the TU already read, *or* in the process of being
5175 read. Don't reassign the global entry to point to this DWO if that's
5176 the case. Also note that if the TU is already being read, it may not
5177 have come from a DWO, the program may be a mix of Fission-compiled
5178 code and non-Fission-compiled code. */
5179
5180 /* Have we already tried to read this TU?
5181 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5182 needn't exist in the global table yet). */
5183 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
5184 return sig_entry;
5185
5186 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
5187 dwo_unit of the TU itself. */
5188 dwo_file = cu->dwo_unit->dwo_file;
5189
5190 /* Ok, this is the first time we're reading this TU. */
5191 if (dwo_file->tus == NULL)
5192 return NULL;
5193 find_dwo_entry.signature = sig;
5194 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
5195 if (dwo_entry == NULL)
5196 return NULL;
5197
5198 /* If the global table doesn't have an entry for this TU, add one. */
5199 if (sig_entry == NULL)
5200 sig_entry = add_type_unit (sig, slot);
5201
5202 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
5203 sig_entry->per_cu.tu_read = 1;
5204 return sig_entry;
5205 }
5206
5207 /* Subroutine of lookup_signatured_type.
5208 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
5209 then try the DWP file. If the TU stub (skeleton) has been removed then
5210 it won't be in .gdb_index. */
5211
5212 static struct signatured_type *
5213 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5214 {
5215 struct objfile *objfile = dwarf2_per_objfile->objfile;
5216 struct dwp_file *dwp_file = get_dwp_file ();
5217 struct dwo_unit *dwo_entry;
5218 struct signatured_type find_sig_entry, *sig_entry;
5219 void **slot;
5220
5221 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
5222 gdb_assert (dwp_file != NULL);
5223
5224 /* If TU skeletons have been removed then we may not have read in any
5225 TUs yet. */
5226 if (dwarf2_per_objfile->signatured_types == NULL)
5227 {
5228 dwarf2_per_objfile->signatured_types
5229 = allocate_signatured_type_table (objfile);
5230 }
5231
5232 find_sig_entry.signature = sig;
5233 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5234 &find_sig_entry, INSERT);
5235 sig_entry = (struct signatured_type *) *slot;
5236
5237 /* Have we already tried to read this TU?
5238 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5239 needn't exist in the global table yet). */
5240 if (sig_entry != NULL)
5241 return sig_entry;
5242
5243 if (dwp_file->tus == NULL)
5244 return NULL;
5245 dwo_entry = lookup_dwo_unit_in_dwp (dwp_file, NULL,
5246 sig, 1 /* is_debug_types */);
5247 if (dwo_entry == NULL)
5248 return NULL;
5249
5250 sig_entry = add_type_unit (sig, slot);
5251 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
5252
5253 return sig_entry;
5254 }
5255
5256 /* Lookup a signature based type for DW_FORM_ref_sig8.
5257 Returns NULL if signature SIG is not present in the table.
5258 It is up to the caller to complain about this. */
5259
5260 static struct signatured_type *
5261 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5262 {
5263 if (cu->dwo_unit
5264 && dwarf2_per_objfile->using_index)
5265 {
5266 /* We're in a DWO/DWP file, and we're using .gdb_index.
5267 These cases require special processing. */
5268 if (get_dwp_file () == NULL)
5269 return lookup_dwo_signatured_type (cu, sig);
5270 else
5271 return lookup_dwp_signatured_type (cu, sig);
5272 }
5273 else
5274 {
5275 struct signatured_type find_entry, *entry;
5276
5277 if (dwarf2_per_objfile->signatured_types == NULL)
5278 return NULL;
5279 find_entry.signature = sig;
5280 entry = ((struct signatured_type *)
5281 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
5282 return entry;
5283 }
5284 }
5285 \f
5286 /* Low level DIE reading support. */
5287
5288 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
5289
5290 static void
5291 init_cu_die_reader (struct die_reader_specs *reader,
5292 struct dwarf2_cu *cu,
5293 struct dwarf2_section_info *section,
5294 struct dwo_file *dwo_file)
5295 {
5296 gdb_assert (section->readin && section->buffer != NULL);
5297 reader->abfd = get_section_bfd_owner (section);
5298 reader->cu = cu;
5299 reader->dwo_file = dwo_file;
5300 reader->die_section = section;
5301 reader->buffer = section->buffer;
5302 reader->buffer_end = section->buffer + section->size;
5303 reader->comp_dir = NULL;
5304 }
5305
5306 /* Subroutine of init_cutu_and_read_dies to simplify it.
5307 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
5308 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
5309 already.
5310
5311 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
5312 from it to the DIE in the DWO. If NULL we are skipping the stub.
5313 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
5314 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
5315 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
5316 STUB_COMP_DIR may be non-NULL.
5317 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
5318 are filled in with the info of the DIE from the DWO file.
5319 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
5320 provided an abbrev table to use.
5321 The result is non-zero if a valid (non-dummy) DIE was found. */
5322
5323 static int
5324 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
5325 struct dwo_unit *dwo_unit,
5326 int abbrev_table_provided,
5327 struct die_info *stub_comp_unit_die,
5328 const char *stub_comp_dir,
5329 struct die_reader_specs *result_reader,
5330 const gdb_byte **result_info_ptr,
5331 struct die_info **result_comp_unit_die,
5332 int *result_has_children)
5333 {
5334 struct objfile *objfile = dwarf2_per_objfile->objfile;
5335 struct dwarf2_cu *cu = this_cu->cu;
5336 struct dwarf2_section_info *section;
5337 bfd *abfd;
5338 const gdb_byte *begin_info_ptr, *info_ptr;
5339 ULONGEST signature; /* Or dwo_id. */
5340 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
5341 int i,num_extra_attrs;
5342 struct dwarf2_section_info *dwo_abbrev_section;
5343 struct attribute *attr;
5344 struct die_info *comp_unit_die;
5345
5346 /* At most one of these may be provided. */
5347 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
5348
5349 /* These attributes aren't processed until later:
5350 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
5351 DW_AT_comp_dir is used now, to find the DWO file, but it is also
5352 referenced later. However, these attributes are found in the stub
5353 which we won't have later. In order to not impose this complication
5354 on the rest of the code, we read them here and copy them to the
5355 DWO CU/TU die. */
5356
5357 stmt_list = NULL;
5358 low_pc = NULL;
5359 high_pc = NULL;
5360 ranges = NULL;
5361 comp_dir = NULL;
5362
5363 if (stub_comp_unit_die != NULL)
5364 {
5365 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
5366 DWO file. */
5367 if (! this_cu->is_debug_types)
5368 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
5369 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
5370 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
5371 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
5372 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
5373
5374 /* There should be a DW_AT_addr_base attribute here (if needed).
5375 We need the value before we can process DW_FORM_GNU_addr_index. */
5376 cu->addr_base = 0;
5377 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
5378 if (attr)
5379 cu->addr_base = DW_UNSND (attr);
5380
5381 /* There should be a DW_AT_ranges_base attribute here (if needed).
5382 We need the value before we can process DW_AT_ranges. */
5383 cu->ranges_base = 0;
5384 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
5385 if (attr)
5386 cu->ranges_base = DW_UNSND (attr);
5387 }
5388 else if (stub_comp_dir != NULL)
5389 {
5390 /* Reconstruct the comp_dir attribute to simplify the code below. */
5391 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
5392 comp_dir->name = DW_AT_comp_dir;
5393 comp_dir->form = DW_FORM_string;
5394 DW_STRING_IS_CANONICAL (comp_dir) = 0;
5395 DW_STRING (comp_dir) = stub_comp_dir;
5396 }
5397
5398 /* Set up for reading the DWO CU/TU. */
5399 cu->dwo_unit = dwo_unit;
5400 section = dwo_unit->section;
5401 dwarf2_read_section (objfile, section);
5402 abfd = get_section_bfd_owner (section);
5403 begin_info_ptr = info_ptr = (section->buffer
5404 + to_underlying (dwo_unit->sect_off));
5405 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
5406 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
5407
5408 if (this_cu->is_debug_types)
5409 {
5410 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
5411
5412 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5413 dwo_abbrev_section,
5414 info_ptr, rcuh_kind::TYPE);
5415 /* This is not an assert because it can be caused by bad debug info. */
5416 if (sig_type->signature != cu->header.signature)
5417 {
5418 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
5419 " TU at offset 0x%x [in module %s]"),
5420 hex_string (sig_type->signature),
5421 hex_string (cu->header.signature),
5422 to_underlying (dwo_unit->sect_off),
5423 bfd_get_filename (abfd));
5424 }
5425 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
5426 /* For DWOs coming from DWP files, we don't know the CU length
5427 nor the type's offset in the TU until now. */
5428 dwo_unit->length = get_cu_length (&cu->header);
5429 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
5430
5431 /* Establish the type offset that can be used to lookup the type.
5432 For DWO files, we don't know it until now. */
5433 sig_type->type_offset_in_section
5434 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
5435 }
5436 else
5437 {
5438 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5439 dwo_abbrev_section,
5440 info_ptr, rcuh_kind::COMPILE);
5441 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
5442 /* For DWOs coming from DWP files, we don't know the CU length
5443 until now. */
5444 dwo_unit->length = get_cu_length (&cu->header);
5445 }
5446
5447 /* Replace the CU's original abbrev table with the DWO's.
5448 Reminder: We can't read the abbrev table until we've read the header. */
5449 if (abbrev_table_provided)
5450 {
5451 /* Don't free the provided abbrev table, the caller of
5452 init_cutu_and_read_dies owns it. */
5453 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
5454 /* Ensure the DWO abbrev table gets freed. */
5455 make_cleanup (dwarf2_free_abbrev_table, cu);
5456 }
5457 else
5458 {
5459 dwarf2_free_abbrev_table (cu);
5460 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
5461 /* Leave any existing abbrev table cleanup as is. */
5462 }
5463
5464 /* Read in the die, but leave space to copy over the attributes
5465 from the stub. This has the benefit of simplifying the rest of
5466 the code - all the work to maintain the illusion of a single
5467 DW_TAG_{compile,type}_unit DIE is done here. */
5468 num_extra_attrs = ((stmt_list != NULL)
5469 + (low_pc != NULL)
5470 + (high_pc != NULL)
5471 + (ranges != NULL)
5472 + (comp_dir != NULL));
5473 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
5474 result_has_children, num_extra_attrs);
5475
5476 /* Copy over the attributes from the stub to the DIE we just read in. */
5477 comp_unit_die = *result_comp_unit_die;
5478 i = comp_unit_die->num_attrs;
5479 if (stmt_list != NULL)
5480 comp_unit_die->attrs[i++] = *stmt_list;
5481 if (low_pc != NULL)
5482 comp_unit_die->attrs[i++] = *low_pc;
5483 if (high_pc != NULL)
5484 comp_unit_die->attrs[i++] = *high_pc;
5485 if (ranges != NULL)
5486 comp_unit_die->attrs[i++] = *ranges;
5487 if (comp_dir != NULL)
5488 comp_unit_die->attrs[i++] = *comp_dir;
5489 comp_unit_die->num_attrs += num_extra_attrs;
5490
5491 if (dwarf_die_debug)
5492 {
5493 fprintf_unfiltered (gdb_stdlog,
5494 "Read die from %s@0x%x of %s:\n",
5495 get_section_name (section),
5496 (unsigned) (begin_info_ptr - section->buffer),
5497 bfd_get_filename (abfd));
5498 dump_die (comp_unit_die, dwarf_die_debug);
5499 }
5500
5501 /* Save the comp_dir attribute. If there is no DWP file then we'll read
5502 TUs by skipping the stub and going directly to the entry in the DWO file.
5503 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
5504 to get it via circuitous means. Blech. */
5505 if (comp_dir != NULL)
5506 result_reader->comp_dir = DW_STRING (comp_dir);
5507
5508 /* Skip dummy compilation units. */
5509 if (info_ptr >= begin_info_ptr + dwo_unit->length
5510 || peek_abbrev_code (abfd, info_ptr) == 0)
5511 return 0;
5512
5513 *result_info_ptr = info_ptr;
5514 return 1;
5515 }
5516
5517 /* Subroutine of init_cutu_and_read_dies to simplify it.
5518 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
5519 Returns NULL if the specified DWO unit cannot be found. */
5520
5521 static struct dwo_unit *
5522 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
5523 struct die_info *comp_unit_die)
5524 {
5525 struct dwarf2_cu *cu = this_cu->cu;
5526 struct attribute *attr;
5527 ULONGEST signature;
5528 struct dwo_unit *dwo_unit;
5529 const char *comp_dir, *dwo_name;
5530
5531 gdb_assert (cu != NULL);
5532
5533 /* Yeah, we look dwo_name up again, but it simplifies the code. */
5534 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5535 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
5536
5537 if (this_cu->is_debug_types)
5538 {
5539 struct signatured_type *sig_type;
5540
5541 /* Since this_cu is the first member of struct signatured_type,
5542 we can go from a pointer to one to a pointer to the other. */
5543 sig_type = (struct signatured_type *) this_cu;
5544 signature = sig_type->signature;
5545 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
5546 }
5547 else
5548 {
5549 struct attribute *attr;
5550
5551 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
5552 if (! attr)
5553 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
5554 " [in module %s]"),
5555 dwo_name, objfile_name (this_cu->objfile));
5556 signature = DW_UNSND (attr);
5557 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
5558 signature);
5559 }
5560
5561 return dwo_unit;
5562 }
5563
5564 /* Subroutine of init_cutu_and_read_dies to simplify it.
5565 See it for a description of the parameters.
5566 Read a TU directly from a DWO file, bypassing the stub.
5567
5568 Note: This function could be a little bit simpler if we shared cleanups
5569 with our caller, init_cutu_and_read_dies. That's generally a fragile thing
5570 to do, so we keep this function self-contained. Or we could move this
5571 into our caller, but it's complex enough already. */
5572
5573 static void
5574 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
5575 int use_existing_cu, int keep,
5576 die_reader_func_ftype *die_reader_func,
5577 void *data)
5578 {
5579 struct dwarf2_cu *cu;
5580 struct signatured_type *sig_type;
5581 struct cleanup *cleanups, *free_cu_cleanup = NULL;
5582 struct die_reader_specs reader;
5583 const gdb_byte *info_ptr;
5584 struct die_info *comp_unit_die;
5585 int has_children;
5586
5587 /* Verify we can do the following downcast, and that we have the
5588 data we need. */
5589 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
5590 sig_type = (struct signatured_type *) this_cu;
5591 gdb_assert (sig_type->dwo_unit != NULL);
5592
5593 cleanups = make_cleanup (null_cleanup, NULL);
5594
5595 if (use_existing_cu && this_cu->cu != NULL)
5596 {
5597 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
5598 cu = this_cu->cu;
5599 /* There's no need to do the rereading_dwo_cu handling that
5600 init_cutu_and_read_dies does since we don't read the stub. */
5601 }
5602 else
5603 {
5604 /* If !use_existing_cu, this_cu->cu must be NULL. */
5605 gdb_assert (this_cu->cu == NULL);
5606 cu = XNEW (struct dwarf2_cu);
5607 init_one_comp_unit (cu, this_cu);
5608 /* If an error occurs while loading, release our storage. */
5609 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
5610 }
5611
5612 /* A future optimization, if needed, would be to use an existing
5613 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
5614 could share abbrev tables. */
5615
5616 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
5617 0 /* abbrev_table_provided */,
5618 NULL /* stub_comp_unit_die */,
5619 sig_type->dwo_unit->dwo_file->comp_dir,
5620 &reader, &info_ptr,
5621 &comp_unit_die, &has_children) == 0)
5622 {
5623 /* Dummy die. */
5624 do_cleanups (cleanups);
5625 return;
5626 }
5627
5628 /* All the "real" work is done here. */
5629 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5630
5631 /* This duplicates the code in init_cutu_and_read_dies,
5632 but the alternative is making the latter more complex.
5633 This function is only for the special case of using DWO files directly:
5634 no point in overly complicating the general case just to handle this. */
5635 if (free_cu_cleanup != NULL)
5636 {
5637 if (keep)
5638 {
5639 /* We've successfully allocated this compilation unit. Let our
5640 caller clean it up when finished with it. */
5641 discard_cleanups (free_cu_cleanup);
5642
5643 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5644 So we have to manually free the abbrev table. */
5645 dwarf2_free_abbrev_table (cu);
5646
5647 /* Link this CU into read_in_chain. */
5648 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5649 dwarf2_per_objfile->read_in_chain = this_cu;
5650 }
5651 else
5652 do_cleanups (free_cu_cleanup);
5653 }
5654
5655 do_cleanups (cleanups);
5656 }
5657
5658 /* Initialize a CU (or TU) and read its DIEs.
5659 If the CU defers to a DWO file, read the DWO file as well.
5660
5661 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
5662 Otherwise the table specified in the comp unit header is read in and used.
5663 This is an optimization for when we already have the abbrev table.
5664
5665 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
5666 Otherwise, a new CU is allocated with xmalloc.
5667
5668 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
5669 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
5670
5671 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
5672 linker) then DIE_READER_FUNC will not get called. */
5673
5674 static void
5675 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
5676 struct abbrev_table *abbrev_table,
5677 int use_existing_cu, int keep,
5678 die_reader_func_ftype *die_reader_func,
5679 void *data)
5680 {
5681 struct objfile *objfile = dwarf2_per_objfile->objfile;
5682 struct dwarf2_section_info *section = this_cu->section;
5683 bfd *abfd = get_section_bfd_owner (section);
5684 struct dwarf2_cu *cu;
5685 const gdb_byte *begin_info_ptr, *info_ptr;
5686 struct die_reader_specs reader;
5687 struct die_info *comp_unit_die;
5688 int has_children;
5689 struct attribute *attr;
5690 struct cleanup *cleanups, *free_cu_cleanup = NULL;
5691 struct signatured_type *sig_type = NULL;
5692 struct dwarf2_section_info *abbrev_section;
5693 /* Non-zero if CU currently points to a DWO file and we need to
5694 reread it. When this happens we need to reread the skeleton die
5695 before we can reread the DWO file (this only applies to CUs, not TUs). */
5696 int rereading_dwo_cu = 0;
5697
5698 if (dwarf_die_debug)
5699 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5700 this_cu->is_debug_types ? "type" : "comp",
5701 to_underlying (this_cu->sect_off));
5702
5703 if (use_existing_cu)
5704 gdb_assert (keep);
5705
5706 /* If we're reading a TU directly from a DWO file, including a virtual DWO
5707 file (instead of going through the stub), short-circuit all of this. */
5708 if (this_cu->reading_dwo_directly)
5709 {
5710 /* Narrow down the scope of possibilities to have to understand. */
5711 gdb_assert (this_cu->is_debug_types);
5712 gdb_assert (abbrev_table == NULL);
5713 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
5714 die_reader_func, data);
5715 return;
5716 }
5717
5718 cleanups = make_cleanup (null_cleanup, NULL);
5719
5720 /* This is cheap if the section is already read in. */
5721 dwarf2_read_section (objfile, section);
5722
5723 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
5724
5725 abbrev_section = get_abbrev_section_for_cu (this_cu);
5726
5727 if (use_existing_cu && this_cu->cu != NULL)
5728 {
5729 cu = this_cu->cu;
5730 /* If this CU is from a DWO file we need to start over, we need to
5731 refetch the attributes from the skeleton CU.
5732 This could be optimized by retrieving those attributes from when we
5733 were here the first time: the previous comp_unit_die was stored in
5734 comp_unit_obstack. But there's no data yet that we need this
5735 optimization. */
5736 if (cu->dwo_unit != NULL)
5737 rereading_dwo_cu = 1;
5738 }
5739 else
5740 {
5741 /* If !use_existing_cu, this_cu->cu must be NULL. */
5742 gdb_assert (this_cu->cu == NULL);
5743 cu = XNEW (struct dwarf2_cu);
5744 init_one_comp_unit (cu, this_cu);
5745 /* If an error occurs while loading, release our storage. */
5746 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
5747 }
5748
5749 /* Get the header. */
5750 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
5751 {
5752 /* We already have the header, there's no need to read it in again. */
5753 info_ptr += to_underlying (cu->header.first_die_cu_offset);
5754 }
5755 else
5756 {
5757 if (this_cu->is_debug_types)
5758 {
5759 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5760 abbrev_section, info_ptr,
5761 rcuh_kind::TYPE);
5762
5763 /* Since per_cu is the first member of struct signatured_type,
5764 we can go from a pointer to one to a pointer to the other. */
5765 sig_type = (struct signatured_type *) this_cu;
5766 gdb_assert (sig_type->signature == cu->header.signature);
5767 gdb_assert (sig_type->type_offset_in_tu
5768 == cu->header.type_cu_offset_in_tu);
5769 gdb_assert (this_cu->sect_off == cu->header.sect_off);
5770
5771 /* LENGTH has not been set yet for type units if we're
5772 using .gdb_index. */
5773 this_cu->length = get_cu_length (&cu->header);
5774
5775 /* Establish the type offset that can be used to lookup the type. */
5776 sig_type->type_offset_in_section =
5777 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
5778
5779 this_cu->dwarf_version = cu->header.version;
5780 }
5781 else
5782 {
5783 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5784 abbrev_section,
5785 info_ptr,
5786 rcuh_kind::COMPILE);
5787
5788 gdb_assert (this_cu->sect_off == cu->header.sect_off);
5789 gdb_assert (this_cu->length == get_cu_length (&cu->header));
5790 this_cu->dwarf_version = cu->header.version;
5791 }
5792 }
5793
5794 /* Skip dummy compilation units. */
5795 if (info_ptr >= begin_info_ptr + this_cu->length
5796 || peek_abbrev_code (abfd, info_ptr) == 0)
5797 {
5798 do_cleanups (cleanups);
5799 return;
5800 }
5801
5802 /* If we don't have them yet, read the abbrevs for this compilation unit.
5803 And if we need to read them now, make sure they're freed when we're
5804 done. Note that it's important that if the CU had an abbrev table
5805 on entry we don't free it when we're done: Somewhere up the call stack
5806 it may be in use. */
5807 if (abbrev_table != NULL)
5808 {
5809 gdb_assert (cu->abbrev_table == NULL);
5810 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
5811 cu->abbrev_table = abbrev_table;
5812 }
5813 else if (cu->abbrev_table == NULL)
5814 {
5815 dwarf2_read_abbrevs (cu, abbrev_section);
5816 make_cleanup (dwarf2_free_abbrev_table, cu);
5817 }
5818 else if (rereading_dwo_cu)
5819 {
5820 dwarf2_free_abbrev_table (cu);
5821 dwarf2_read_abbrevs (cu, abbrev_section);
5822 }
5823
5824 /* Read the top level CU/TU die. */
5825 init_cu_die_reader (&reader, cu, section, NULL);
5826 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5827
5828 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
5829 from the DWO file.
5830 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
5831 DWO CU, that this test will fail (the attribute will not be present). */
5832 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5833 if (attr)
5834 {
5835 struct dwo_unit *dwo_unit;
5836 struct die_info *dwo_comp_unit_die;
5837
5838 if (has_children)
5839 {
5840 complaint (&symfile_complaints,
5841 _("compilation unit with DW_AT_GNU_dwo_name"
5842 " has children (offset 0x%x) [in module %s]"),
5843 to_underlying (this_cu->sect_off), bfd_get_filename (abfd));
5844 }
5845 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
5846 if (dwo_unit != NULL)
5847 {
5848 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
5849 abbrev_table != NULL,
5850 comp_unit_die, NULL,
5851 &reader, &info_ptr,
5852 &dwo_comp_unit_die, &has_children) == 0)
5853 {
5854 /* Dummy die. */
5855 do_cleanups (cleanups);
5856 return;
5857 }
5858 comp_unit_die = dwo_comp_unit_die;
5859 }
5860 else
5861 {
5862 /* Yikes, we couldn't find the rest of the DIE, we only have
5863 the stub. A complaint has already been logged. There's
5864 not much more we can do except pass on the stub DIE to
5865 die_reader_func. We don't want to throw an error on bad
5866 debug info. */
5867 }
5868 }
5869
5870 /* All of the above is setup for this call. Yikes. */
5871 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5872
5873 /* Done, clean up. */
5874 if (free_cu_cleanup != NULL)
5875 {
5876 if (keep)
5877 {
5878 /* We've successfully allocated this compilation unit. Let our
5879 caller clean it up when finished with it. */
5880 discard_cleanups (free_cu_cleanup);
5881
5882 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5883 So we have to manually free the abbrev table. */
5884 dwarf2_free_abbrev_table (cu);
5885
5886 /* Link this CU into read_in_chain. */
5887 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5888 dwarf2_per_objfile->read_in_chain = this_cu;
5889 }
5890 else
5891 do_cleanups (free_cu_cleanup);
5892 }
5893
5894 do_cleanups (cleanups);
5895 }
5896
5897 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
5898 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
5899 to have already done the lookup to find the DWO file).
5900
5901 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
5902 THIS_CU->is_debug_types, but nothing else.
5903
5904 We fill in THIS_CU->length.
5905
5906 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
5907 linker) then DIE_READER_FUNC will not get called.
5908
5909 THIS_CU->cu is always freed when done.
5910 This is done in order to not leave THIS_CU->cu in a state where we have
5911 to care whether it refers to the "main" CU or the DWO CU. */
5912
5913 static void
5914 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
5915 struct dwo_file *dwo_file,
5916 die_reader_func_ftype *die_reader_func,
5917 void *data)
5918 {
5919 struct objfile *objfile = dwarf2_per_objfile->objfile;
5920 struct dwarf2_section_info *section = this_cu->section;
5921 bfd *abfd = get_section_bfd_owner (section);
5922 struct dwarf2_section_info *abbrev_section;
5923 struct dwarf2_cu cu;
5924 const gdb_byte *begin_info_ptr, *info_ptr;
5925 struct die_reader_specs reader;
5926 struct cleanup *cleanups;
5927 struct die_info *comp_unit_die;
5928 int has_children;
5929
5930 if (dwarf_die_debug)
5931 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5932 this_cu->is_debug_types ? "type" : "comp",
5933 to_underlying (this_cu->sect_off));
5934
5935 gdb_assert (this_cu->cu == NULL);
5936
5937 abbrev_section = (dwo_file != NULL
5938 ? &dwo_file->sections.abbrev
5939 : get_abbrev_section_for_cu (this_cu));
5940
5941 /* This is cheap if the section is already read in. */
5942 dwarf2_read_section (objfile, section);
5943
5944 init_one_comp_unit (&cu, this_cu);
5945
5946 cleanups = make_cleanup (free_stack_comp_unit, &cu);
5947
5948 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
5949 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
5950 abbrev_section, info_ptr,
5951 (this_cu->is_debug_types
5952 ? rcuh_kind::TYPE
5953 : rcuh_kind::COMPILE));
5954
5955 this_cu->length = get_cu_length (&cu.header);
5956
5957 /* Skip dummy compilation units. */
5958 if (info_ptr >= begin_info_ptr + this_cu->length
5959 || peek_abbrev_code (abfd, info_ptr) == 0)
5960 {
5961 do_cleanups (cleanups);
5962 return;
5963 }
5964
5965 dwarf2_read_abbrevs (&cu, abbrev_section);
5966 make_cleanup (dwarf2_free_abbrev_table, &cu);
5967
5968 init_cu_die_reader (&reader, &cu, section, dwo_file);
5969 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5970
5971 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5972
5973 do_cleanups (cleanups);
5974 }
5975
5976 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
5977 does not lookup the specified DWO file.
5978 This cannot be used to read DWO files.
5979
5980 THIS_CU->cu is always freed when done.
5981 This is done in order to not leave THIS_CU->cu in a state where we have
5982 to care whether it refers to the "main" CU or the DWO CU.
5983 We can revisit this if the data shows there's a performance issue. */
5984
5985 static void
5986 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
5987 die_reader_func_ftype *die_reader_func,
5988 void *data)
5989 {
5990 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
5991 }
5992 \f
5993 /* Type Unit Groups.
5994
5995 Type Unit Groups are a way to collapse the set of all TUs (type units) into
5996 a more manageable set. The grouping is done by DW_AT_stmt_list entry
5997 so that all types coming from the same compilation (.o file) are grouped
5998 together. A future step could be to put the types in the same symtab as
5999 the CU the types ultimately came from. */
6000
6001 static hashval_t
6002 hash_type_unit_group (const void *item)
6003 {
6004 const struct type_unit_group *tu_group
6005 = (const struct type_unit_group *) item;
6006
6007 return hash_stmt_list_entry (&tu_group->hash);
6008 }
6009
6010 static int
6011 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
6012 {
6013 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
6014 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
6015
6016 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
6017 }
6018
6019 /* Allocate a hash table for type unit groups. */
6020
6021 static htab_t
6022 allocate_type_unit_groups_table (void)
6023 {
6024 return htab_create_alloc_ex (3,
6025 hash_type_unit_group,
6026 eq_type_unit_group,
6027 NULL,
6028 &dwarf2_per_objfile->objfile->objfile_obstack,
6029 hashtab_obstack_allocate,
6030 dummy_obstack_deallocate);
6031 }
6032
6033 /* Type units that don't have DW_AT_stmt_list are grouped into their own
6034 partial symtabs. We combine several TUs per psymtab to not let the size
6035 of any one psymtab grow too big. */
6036 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
6037 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
6038
6039 /* Helper routine for get_type_unit_group.
6040 Create the type_unit_group object used to hold one or more TUs. */
6041
6042 static struct type_unit_group *
6043 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
6044 {
6045 struct objfile *objfile = dwarf2_per_objfile->objfile;
6046 struct dwarf2_per_cu_data *per_cu;
6047 struct type_unit_group *tu_group;
6048
6049 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6050 struct type_unit_group);
6051 per_cu = &tu_group->per_cu;
6052 per_cu->objfile = objfile;
6053
6054 if (dwarf2_per_objfile->using_index)
6055 {
6056 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6057 struct dwarf2_per_cu_quick_data);
6058 }
6059 else
6060 {
6061 unsigned int line_offset = to_underlying (line_offset_struct);
6062 struct partial_symtab *pst;
6063 char *name;
6064
6065 /* Give the symtab a useful name for debug purposes. */
6066 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
6067 name = xstrprintf ("<type_units_%d>",
6068 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
6069 else
6070 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
6071
6072 pst = create_partial_symtab (per_cu, name);
6073 pst->anonymous = 1;
6074
6075 xfree (name);
6076 }
6077
6078 tu_group->hash.dwo_unit = cu->dwo_unit;
6079 tu_group->hash.line_sect_off = line_offset_struct;
6080
6081 return tu_group;
6082 }
6083
6084 /* Look up the type_unit_group for type unit CU, and create it if necessary.
6085 STMT_LIST is a DW_AT_stmt_list attribute. */
6086
6087 static struct type_unit_group *
6088 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
6089 {
6090 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6091 struct type_unit_group *tu_group;
6092 void **slot;
6093 unsigned int line_offset;
6094 struct type_unit_group type_unit_group_for_lookup;
6095
6096 if (dwarf2_per_objfile->type_unit_groups == NULL)
6097 {
6098 dwarf2_per_objfile->type_unit_groups =
6099 allocate_type_unit_groups_table ();
6100 }
6101
6102 /* Do we need to create a new group, or can we use an existing one? */
6103
6104 if (stmt_list)
6105 {
6106 line_offset = DW_UNSND (stmt_list);
6107 ++tu_stats->nr_symtab_sharers;
6108 }
6109 else
6110 {
6111 /* Ugh, no stmt_list. Rare, but we have to handle it.
6112 We can do various things here like create one group per TU or
6113 spread them over multiple groups to split up the expansion work.
6114 To avoid worst case scenarios (too many groups or too large groups)
6115 we, umm, group them in bunches. */
6116 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
6117 | (tu_stats->nr_stmt_less_type_units
6118 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
6119 ++tu_stats->nr_stmt_less_type_units;
6120 }
6121
6122 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
6123 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
6124 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
6125 &type_unit_group_for_lookup, INSERT);
6126 if (*slot != NULL)
6127 {
6128 tu_group = (struct type_unit_group *) *slot;
6129 gdb_assert (tu_group != NULL);
6130 }
6131 else
6132 {
6133 sect_offset line_offset_struct = (sect_offset) line_offset;
6134 tu_group = create_type_unit_group (cu, line_offset_struct);
6135 *slot = tu_group;
6136 ++tu_stats->nr_symtabs;
6137 }
6138
6139 return tu_group;
6140 }
6141 \f
6142 /* Partial symbol tables. */
6143
6144 /* Create a psymtab named NAME and assign it to PER_CU.
6145
6146 The caller must fill in the following details:
6147 dirname, textlow, texthigh. */
6148
6149 static struct partial_symtab *
6150 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
6151 {
6152 struct objfile *objfile = per_cu->objfile;
6153 struct partial_symtab *pst;
6154
6155 pst = start_psymtab_common (objfile, name, 0,
6156 objfile->global_psymbols.next,
6157 objfile->static_psymbols.next);
6158
6159 pst->psymtabs_addrmap_supported = 1;
6160
6161 /* This is the glue that links PST into GDB's symbol API. */
6162 pst->read_symtab_private = per_cu;
6163 pst->read_symtab = dwarf2_read_symtab;
6164 per_cu->v.psymtab = pst;
6165
6166 return pst;
6167 }
6168
6169 /* The DATA object passed to process_psymtab_comp_unit_reader has this
6170 type. */
6171
6172 struct process_psymtab_comp_unit_data
6173 {
6174 /* True if we are reading a DW_TAG_partial_unit. */
6175
6176 int want_partial_unit;
6177
6178 /* The "pretend" language that is used if the CU doesn't declare a
6179 language. */
6180
6181 enum language pretend_language;
6182 };
6183
6184 /* die_reader_func for process_psymtab_comp_unit. */
6185
6186 static void
6187 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
6188 const gdb_byte *info_ptr,
6189 struct die_info *comp_unit_die,
6190 int has_children,
6191 void *data)
6192 {
6193 struct dwarf2_cu *cu = reader->cu;
6194 struct objfile *objfile = cu->objfile;
6195 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6196 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
6197 CORE_ADDR baseaddr;
6198 CORE_ADDR best_lowpc = 0, best_highpc = 0;
6199 struct partial_symtab *pst;
6200 enum pc_bounds_kind cu_bounds_kind;
6201 const char *filename;
6202 struct process_psymtab_comp_unit_data *info
6203 = (struct process_psymtab_comp_unit_data *) data;
6204
6205 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
6206 return;
6207
6208 gdb_assert (! per_cu->is_debug_types);
6209
6210 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
6211
6212 cu->list_in_scope = &file_symbols;
6213
6214 /* Allocate a new partial symbol table structure. */
6215 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
6216 if (filename == NULL)
6217 filename = "";
6218
6219 pst = create_partial_symtab (per_cu, filename);
6220
6221 /* This must be done before calling dwarf2_build_include_psymtabs. */
6222 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6223
6224 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6225
6226 dwarf2_find_base_address (comp_unit_die, cu);
6227
6228 /* Possibly set the default values of LOWPC and HIGHPC from
6229 `DW_AT_ranges'. */
6230 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
6231 &best_highpc, cu, pst);
6232 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
6233 /* Store the contiguous range if it is not empty; it can be empty for
6234 CUs with no code. */
6235 addrmap_set_empty (objfile->psymtabs_addrmap,
6236 gdbarch_adjust_dwarf2_addr (gdbarch,
6237 best_lowpc + baseaddr),
6238 gdbarch_adjust_dwarf2_addr (gdbarch,
6239 best_highpc + baseaddr) - 1,
6240 pst);
6241
6242 /* Check if comp unit has_children.
6243 If so, read the rest of the partial symbols from this comp unit.
6244 If not, there's no more debug_info for this comp unit. */
6245 if (has_children)
6246 {
6247 struct partial_die_info *first_die;
6248 CORE_ADDR lowpc, highpc;
6249
6250 lowpc = ((CORE_ADDR) -1);
6251 highpc = ((CORE_ADDR) 0);
6252
6253 first_die = load_partial_dies (reader, info_ptr, 1);
6254
6255 scan_partial_symbols (first_die, &lowpc, &highpc,
6256 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
6257
6258 /* If we didn't find a lowpc, set it to highpc to avoid
6259 complaints from `maint check'. */
6260 if (lowpc == ((CORE_ADDR) -1))
6261 lowpc = highpc;
6262
6263 /* If the compilation unit didn't have an explicit address range,
6264 then use the information extracted from its child dies. */
6265 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
6266 {
6267 best_lowpc = lowpc;
6268 best_highpc = highpc;
6269 }
6270 }
6271 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
6272 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
6273
6274 end_psymtab_common (objfile, pst);
6275
6276 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
6277 {
6278 int i;
6279 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6280 struct dwarf2_per_cu_data *iter;
6281
6282 /* Fill in 'dependencies' here; we fill in 'users' in a
6283 post-pass. */
6284 pst->number_of_dependencies = len;
6285 pst->dependencies =
6286 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
6287 for (i = 0;
6288 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
6289 i, iter);
6290 ++i)
6291 pst->dependencies[i] = iter->v.psymtab;
6292
6293 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6294 }
6295
6296 /* Get the list of files included in the current compilation unit,
6297 and build a psymtab for each of them. */
6298 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
6299
6300 if (dwarf_read_debug)
6301 {
6302 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6303
6304 fprintf_unfiltered (gdb_stdlog,
6305 "Psymtab for %s unit @0x%x: %s - %s"
6306 ", %d global, %d static syms\n",
6307 per_cu->is_debug_types ? "type" : "comp",
6308 to_underlying (per_cu->sect_off),
6309 paddress (gdbarch, pst->textlow),
6310 paddress (gdbarch, pst->texthigh),
6311 pst->n_global_syms, pst->n_static_syms);
6312 }
6313 }
6314
6315 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6316 Process compilation unit THIS_CU for a psymtab. */
6317
6318 static void
6319 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
6320 int want_partial_unit,
6321 enum language pretend_language)
6322 {
6323 /* If this compilation unit was already read in, free the
6324 cached copy in order to read it in again. This is
6325 necessary because we skipped some symbols when we first
6326 read in the compilation unit (see load_partial_dies).
6327 This problem could be avoided, but the benefit is unclear. */
6328 if (this_cu->cu != NULL)
6329 free_one_cached_comp_unit (this_cu);
6330
6331 if (this_cu->is_debug_types)
6332 init_cutu_and_read_dies (this_cu, NULL, 0, 0, build_type_psymtabs_reader,
6333 NULL);
6334 else
6335 {
6336 process_psymtab_comp_unit_data info;
6337 info.want_partial_unit = want_partial_unit;
6338 info.pretend_language = pretend_language;
6339 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
6340 process_psymtab_comp_unit_reader, &info);
6341 }
6342
6343 /* Age out any secondary CUs. */
6344 age_cached_comp_units ();
6345 }
6346
6347 /* Reader function for build_type_psymtabs. */
6348
6349 static void
6350 build_type_psymtabs_reader (const struct die_reader_specs *reader,
6351 const gdb_byte *info_ptr,
6352 struct die_info *type_unit_die,
6353 int has_children,
6354 void *data)
6355 {
6356 struct objfile *objfile = dwarf2_per_objfile->objfile;
6357 struct dwarf2_cu *cu = reader->cu;
6358 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
6359 struct signatured_type *sig_type;
6360 struct type_unit_group *tu_group;
6361 struct attribute *attr;
6362 struct partial_die_info *first_die;
6363 CORE_ADDR lowpc, highpc;
6364 struct partial_symtab *pst;
6365
6366 gdb_assert (data == NULL);
6367 gdb_assert (per_cu->is_debug_types);
6368 sig_type = (struct signatured_type *) per_cu;
6369
6370 if (! has_children)
6371 return;
6372
6373 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
6374 tu_group = get_type_unit_group (cu, attr);
6375
6376 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
6377
6378 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
6379 cu->list_in_scope = &file_symbols;
6380 pst = create_partial_symtab (per_cu, "");
6381 pst->anonymous = 1;
6382
6383 first_die = load_partial_dies (reader, info_ptr, 1);
6384
6385 lowpc = (CORE_ADDR) -1;
6386 highpc = (CORE_ADDR) 0;
6387 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
6388
6389 end_psymtab_common (objfile, pst);
6390 }
6391
6392 /* Struct used to sort TUs by their abbreviation table offset. */
6393
6394 struct tu_abbrev_offset
6395 {
6396 struct signatured_type *sig_type;
6397 sect_offset abbrev_offset;
6398 };
6399
6400 /* Helper routine for build_type_psymtabs_1, passed to qsort. */
6401
6402 static int
6403 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
6404 {
6405 const struct tu_abbrev_offset * const *a
6406 = (const struct tu_abbrev_offset * const*) ap;
6407 const struct tu_abbrev_offset * const *b
6408 = (const struct tu_abbrev_offset * const*) bp;
6409 sect_offset aoff = (*a)->abbrev_offset;
6410 sect_offset boff = (*b)->abbrev_offset;
6411
6412 return (aoff > boff) - (aoff < boff);
6413 }
6414
6415 /* Efficiently read all the type units.
6416 This does the bulk of the work for build_type_psymtabs.
6417
6418 The efficiency is because we sort TUs by the abbrev table they use and
6419 only read each abbrev table once. In one program there are 200K TUs
6420 sharing 8K abbrev tables.
6421
6422 The main purpose of this function is to support building the
6423 dwarf2_per_objfile->type_unit_groups table.
6424 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
6425 can collapse the search space by grouping them by stmt_list.
6426 The savings can be significant, in the same program from above the 200K TUs
6427 share 8K stmt_list tables.
6428
6429 FUNC is expected to call get_type_unit_group, which will create the
6430 struct type_unit_group if necessary and add it to
6431 dwarf2_per_objfile->type_unit_groups. */
6432
6433 static void
6434 build_type_psymtabs_1 (void)
6435 {
6436 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6437 struct cleanup *cleanups;
6438 struct abbrev_table *abbrev_table;
6439 sect_offset abbrev_offset;
6440 struct tu_abbrev_offset *sorted_by_abbrev;
6441 int i;
6442
6443 /* It's up to the caller to not call us multiple times. */
6444 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
6445
6446 if (dwarf2_per_objfile->n_type_units == 0)
6447 return;
6448
6449 /* TUs typically share abbrev tables, and there can be way more TUs than
6450 abbrev tables. Sort by abbrev table to reduce the number of times we
6451 read each abbrev table in.
6452 Alternatives are to punt or to maintain a cache of abbrev tables.
6453 This is simpler and efficient enough for now.
6454
6455 Later we group TUs by their DW_AT_stmt_list value (as this defines the
6456 symtab to use). Typically TUs with the same abbrev offset have the same
6457 stmt_list value too so in practice this should work well.
6458
6459 The basic algorithm here is:
6460
6461 sort TUs by abbrev table
6462 for each TU with same abbrev table:
6463 read abbrev table if first user
6464 read TU top level DIE
6465 [IWBN if DWO skeletons had DW_AT_stmt_list]
6466 call FUNC */
6467
6468 if (dwarf_read_debug)
6469 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
6470
6471 /* Sort in a separate table to maintain the order of all_type_units
6472 for .gdb_index: TU indices directly index all_type_units. */
6473 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
6474 dwarf2_per_objfile->n_type_units);
6475 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
6476 {
6477 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
6478
6479 sorted_by_abbrev[i].sig_type = sig_type;
6480 sorted_by_abbrev[i].abbrev_offset =
6481 read_abbrev_offset (sig_type->per_cu.section,
6482 sig_type->per_cu.sect_off);
6483 }
6484 cleanups = make_cleanup (xfree, sorted_by_abbrev);
6485 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
6486 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
6487
6488 abbrev_offset = (sect_offset) ~(unsigned) 0;
6489 abbrev_table = NULL;
6490 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
6491
6492 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
6493 {
6494 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
6495
6496 /* Switch to the next abbrev table if necessary. */
6497 if (abbrev_table == NULL
6498 || tu->abbrev_offset != abbrev_offset)
6499 {
6500 if (abbrev_table != NULL)
6501 {
6502 abbrev_table_free (abbrev_table);
6503 /* Reset to NULL in case abbrev_table_read_table throws
6504 an error: abbrev_table_free_cleanup will get called. */
6505 abbrev_table = NULL;
6506 }
6507 abbrev_offset = tu->abbrev_offset;
6508 abbrev_table =
6509 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
6510 abbrev_offset);
6511 ++tu_stats->nr_uniq_abbrev_tables;
6512 }
6513
6514 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
6515 build_type_psymtabs_reader, NULL);
6516 }
6517
6518 do_cleanups (cleanups);
6519 }
6520
6521 /* Print collected type unit statistics. */
6522
6523 static void
6524 print_tu_stats (void)
6525 {
6526 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6527
6528 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
6529 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
6530 dwarf2_per_objfile->n_type_units);
6531 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
6532 tu_stats->nr_uniq_abbrev_tables);
6533 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
6534 tu_stats->nr_symtabs);
6535 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
6536 tu_stats->nr_symtab_sharers);
6537 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
6538 tu_stats->nr_stmt_less_type_units);
6539 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
6540 tu_stats->nr_all_type_units_reallocs);
6541 }
6542
6543 /* Traversal function for build_type_psymtabs. */
6544
6545 static int
6546 build_type_psymtab_dependencies (void **slot, void *info)
6547 {
6548 struct objfile *objfile = dwarf2_per_objfile->objfile;
6549 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
6550 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
6551 struct partial_symtab *pst = per_cu->v.psymtab;
6552 int len = VEC_length (sig_type_ptr, tu_group->tus);
6553 struct signatured_type *iter;
6554 int i;
6555
6556 gdb_assert (len > 0);
6557 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
6558
6559 pst->number_of_dependencies = len;
6560 pst->dependencies =
6561 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
6562 for (i = 0;
6563 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
6564 ++i)
6565 {
6566 gdb_assert (iter->per_cu.is_debug_types);
6567 pst->dependencies[i] = iter->per_cu.v.psymtab;
6568 iter->type_unit_group = tu_group;
6569 }
6570
6571 VEC_free (sig_type_ptr, tu_group->tus);
6572
6573 return 1;
6574 }
6575
6576 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6577 Build partial symbol tables for the .debug_types comp-units. */
6578
6579 static void
6580 build_type_psymtabs (struct objfile *objfile)
6581 {
6582 if (! create_all_type_units (objfile))
6583 return;
6584
6585 build_type_psymtabs_1 ();
6586 }
6587
6588 /* Traversal function for process_skeletonless_type_unit.
6589 Read a TU in a DWO file and build partial symbols for it. */
6590
6591 static int
6592 process_skeletonless_type_unit (void **slot, void *info)
6593 {
6594 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
6595 struct objfile *objfile = (struct objfile *) info;
6596 struct signatured_type find_entry, *entry;
6597
6598 /* If this TU doesn't exist in the global table, add it and read it in. */
6599
6600 if (dwarf2_per_objfile->signatured_types == NULL)
6601 {
6602 dwarf2_per_objfile->signatured_types
6603 = allocate_signatured_type_table (objfile);
6604 }
6605
6606 find_entry.signature = dwo_unit->signature;
6607 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
6608 INSERT);
6609 /* If we've already seen this type there's nothing to do. What's happening
6610 is we're doing our own version of comdat-folding here. */
6611 if (*slot != NULL)
6612 return 1;
6613
6614 /* This does the job that create_all_type_units would have done for
6615 this TU. */
6616 entry = add_type_unit (dwo_unit->signature, slot);
6617 fill_in_sig_entry_from_dwo_entry (objfile, entry, dwo_unit);
6618 *slot = entry;
6619
6620 /* This does the job that build_type_psymtabs_1 would have done. */
6621 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
6622 build_type_psymtabs_reader, NULL);
6623
6624 return 1;
6625 }
6626
6627 /* Traversal function for process_skeletonless_type_units. */
6628
6629 static int
6630 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
6631 {
6632 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
6633
6634 if (dwo_file->tus != NULL)
6635 {
6636 htab_traverse_noresize (dwo_file->tus,
6637 process_skeletonless_type_unit, info);
6638 }
6639
6640 return 1;
6641 }
6642
6643 /* Scan all TUs of DWO files, verifying we've processed them.
6644 This is needed in case a TU was emitted without its skeleton.
6645 Note: This can't be done until we know what all the DWO files are. */
6646
6647 static void
6648 process_skeletonless_type_units (struct objfile *objfile)
6649 {
6650 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
6651 if (get_dwp_file () == NULL
6652 && dwarf2_per_objfile->dwo_files != NULL)
6653 {
6654 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
6655 process_dwo_file_for_skeletonless_type_units,
6656 objfile);
6657 }
6658 }
6659
6660 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
6661
6662 static void
6663 psymtabs_addrmap_cleanup (void *o)
6664 {
6665 struct objfile *objfile = (struct objfile *) o;
6666
6667 objfile->psymtabs_addrmap = NULL;
6668 }
6669
6670 /* Compute the 'user' field for each psymtab in OBJFILE. */
6671
6672 static void
6673 set_partial_user (struct objfile *objfile)
6674 {
6675 int i;
6676
6677 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
6678 {
6679 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
6680 struct partial_symtab *pst = per_cu->v.psymtab;
6681 int j;
6682
6683 if (pst == NULL)
6684 continue;
6685
6686 for (j = 0; j < pst->number_of_dependencies; ++j)
6687 {
6688 /* Set the 'user' field only if it is not already set. */
6689 if (pst->dependencies[j]->user == NULL)
6690 pst->dependencies[j]->user = pst;
6691 }
6692 }
6693 }
6694
6695 /* Build the partial symbol table by doing a quick pass through the
6696 .debug_info and .debug_abbrev sections. */
6697
6698 static void
6699 dwarf2_build_psymtabs_hard (struct objfile *objfile)
6700 {
6701 struct cleanup *back_to, *addrmap_cleanup;
6702 int i;
6703
6704 if (dwarf_read_debug)
6705 {
6706 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
6707 objfile_name (objfile));
6708 }
6709
6710 dwarf2_per_objfile->reading_partial_symbols = 1;
6711
6712 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
6713
6714 /* Any cached compilation units will be linked by the per-objfile
6715 read_in_chain. Make sure to free them when we're done. */
6716 back_to = make_cleanup (free_cached_comp_units, NULL);
6717
6718 build_type_psymtabs (objfile);
6719
6720 create_all_comp_units (objfile);
6721
6722 /* Create a temporary address map on a temporary obstack. We later
6723 copy this to the final obstack. */
6724 auto_obstack temp_obstack;
6725 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
6726 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
6727
6728 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
6729 {
6730 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
6731
6732 process_psymtab_comp_unit (per_cu, 0, language_minimal);
6733 }
6734
6735 /* This has to wait until we read the CUs, we need the list of DWOs. */
6736 process_skeletonless_type_units (objfile);
6737
6738 /* Now that all TUs have been processed we can fill in the dependencies. */
6739 if (dwarf2_per_objfile->type_unit_groups != NULL)
6740 {
6741 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
6742 build_type_psymtab_dependencies, NULL);
6743 }
6744
6745 if (dwarf_read_debug)
6746 print_tu_stats ();
6747
6748 set_partial_user (objfile);
6749
6750 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
6751 &objfile->objfile_obstack);
6752 discard_cleanups (addrmap_cleanup);
6753
6754 do_cleanups (back_to);
6755
6756 if (dwarf_read_debug)
6757 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
6758 objfile_name (objfile));
6759 }
6760
6761 /* die_reader_func for load_partial_comp_unit. */
6762
6763 static void
6764 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
6765 const gdb_byte *info_ptr,
6766 struct die_info *comp_unit_die,
6767 int has_children,
6768 void *data)
6769 {
6770 struct dwarf2_cu *cu = reader->cu;
6771
6772 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
6773
6774 /* Check if comp unit has_children.
6775 If so, read the rest of the partial symbols from this comp unit.
6776 If not, there's no more debug_info for this comp unit. */
6777 if (has_children)
6778 load_partial_dies (reader, info_ptr, 0);
6779 }
6780
6781 /* Load the partial DIEs for a secondary CU into memory.
6782 This is also used when rereading a primary CU with load_all_dies. */
6783
6784 static void
6785 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
6786 {
6787 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6788 load_partial_comp_unit_reader, NULL);
6789 }
6790
6791 static void
6792 read_comp_units_from_section (struct objfile *objfile,
6793 struct dwarf2_section_info *section,
6794 struct dwarf2_section_info *abbrev_section,
6795 unsigned int is_dwz,
6796 int *n_allocated,
6797 int *n_comp_units,
6798 struct dwarf2_per_cu_data ***all_comp_units)
6799 {
6800 const gdb_byte *info_ptr;
6801 bfd *abfd = get_section_bfd_owner (section);
6802
6803 if (dwarf_read_debug)
6804 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
6805 get_section_name (section),
6806 get_section_file_name (section));
6807
6808 dwarf2_read_section (objfile, section);
6809
6810 info_ptr = section->buffer;
6811
6812 while (info_ptr < section->buffer + section->size)
6813 {
6814 struct dwarf2_per_cu_data *this_cu;
6815
6816 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
6817
6818 comp_unit_head cu_header;
6819 read_and_check_comp_unit_head (&cu_header, section, abbrev_section,
6820 info_ptr, rcuh_kind::COMPILE);
6821
6822 /* Save the compilation unit for later lookup. */
6823 if (cu_header.unit_type != DW_UT_type)
6824 {
6825 this_cu = XOBNEW (&objfile->objfile_obstack,
6826 struct dwarf2_per_cu_data);
6827 memset (this_cu, 0, sizeof (*this_cu));
6828 }
6829 else
6830 {
6831 auto sig_type = XOBNEW (&objfile->objfile_obstack,
6832 struct signatured_type);
6833 memset (sig_type, 0, sizeof (*sig_type));
6834 sig_type->signature = cu_header.signature;
6835 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
6836 this_cu = &sig_type->per_cu;
6837 }
6838 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
6839 this_cu->sect_off = sect_off;
6840 this_cu->length = cu_header.length + cu_header.initial_length_size;
6841 this_cu->is_dwz = is_dwz;
6842 this_cu->objfile = objfile;
6843 this_cu->section = section;
6844
6845 if (*n_comp_units == *n_allocated)
6846 {
6847 *n_allocated *= 2;
6848 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
6849 *all_comp_units, *n_allocated);
6850 }
6851 (*all_comp_units)[*n_comp_units] = this_cu;
6852 ++*n_comp_units;
6853
6854 info_ptr = info_ptr + this_cu->length;
6855 }
6856 }
6857
6858 /* Create a list of all compilation units in OBJFILE.
6859 This is only done for -readnow and building partial symtabs. */
6860
6861 static void
6862 create_all_comp_units (struct objfile *objfile)
6863 {
6864 int n_allocated;
6865 int n_comp_units;
6866 struct dwarf2_per_cu_data **all_comp_units;
6867 struct dwz_file *dwz;
6868
6869 n_comp_units = 0;
6870 n_allocated = 10;
6871 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
6872
6873 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info,
6874 &dwarf2_per_objfile->abbrev, 0,
6875 &n_allocated, &n_comp_units, &all_comp_units);
6876
6877 dwz = dwarf2_get_dwz_file ();
6878 if (dwz != NULL)
6879 read_comp_units_from_section (objfile, &dwz->info, &dwz->abbrev, 1,
6880 &n_allocated, &n_comp_units,
6881 &all_comp_units);
6882
6883 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
6884 struct dwarf2_per_cu_data *,
6885 n_comp_units);
6886 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
6887 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6888 xfree (all_comp_units);
6889 dwarf2_per_objfile->n_comp_units = n_comp_units;
6890 }
6891
6892 /* Process all loaded DIEs for compilation unit CU, starting at
6893 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
6894 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
6895 DW_AT_ranges). See the comments of add_partial_subprogram on how
6896 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
6897
6898 static void
6899 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
6900 CORE_ADDR *highpc, int set_addrmap,
6901 struct dwarf2_cu *cu)
6902 {
6903 struct partial_die_info *pdi;
6904
6905 /* Now, march along the PDI's, descending into ones which have
6906 interesting children but skipping the children of the other ones,
6907 until we reach the end of the compilation unit. */
6908
6909 pdi = first_die;
6910
6911 while (pdi != NULL)
6912 {
6913 fixup_partial_die (pdi, cu);
6914
6915 /* Anonymous namespaces or modules have no name but have interesting
6916 children, so we need to look at them. Ditto for anonymous
6917 enums. */
6918
6919 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
6920 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
6921 || pdi->tag == DW_TAG_imported_unit)
6922 {
6923 switch (pdi->tag)
6924 {
6925 case DW_TAG_subprogram:
6926 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
6927 break;
6928 case DW_TAG_constant:
6929 case DW_TAG_variable:
6930 case DW_TAG_typedef:
6931 case DW_TAG_union_type:
6932 if (!pdi->is_declaration)
6933 {
6934 add_partial_symbol (pdi, cu);
6935 }
6936 break;
6937 case DW_TAG_class_type:
6938 case DW_TAG_interface_type:
6939 case DW_TAG_structure_type:
6940 if (!pdi->is_declaration)
6941 {
6942 add_partial_symbol (pdi, cu);
6943 }
6944 if (cu->language == language_rust && pdi->has_children)
6945 scan_partial_symbols (pdi->die_child, lowpc, highpc,
6946 set_addrmap, cu);
6947 break;
6948 case DW_TAG_enumeration_type:
6949 if (!pdi->is_declaration)
6950 add_partial_enumeration (pdi, cu);
6951 break;
6952 case DW_TAG_base_type:
6953 case DW_TAG_subrange_type:
6954 /* File scope base type definitions are added to the partial
6955 symbol table. */
6956 add_partial_symbol (pdi, cu);
6957 break;
6958 case DW_TAG_namespace:
6959 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
6960 break;
6961 case DW_TAG_module:
6962 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
6963 break;
6964 case DW_TAG_imported_unit:
6965 {
6966 struct dwarf2_per_cu_data *per_cu;
6967
6968 /* For now we don't handle imported units in type units. */
6969 if (cu->per_cu->is_debug_types)
6970 {
6971 error (_("Dwarf Error: DW_TAG_imported_unit is not"
6972 " supported in type units [in module %s]"),
6973 objfile_name (cu->objfile));
6974 }
6975
6976 per_cu = dwarf2_find_containing_comp_unit (pdi->d.sect_off,
6977 pdi->is_dwz,
6978 cu->objfile);
6979
6980 /* Go read the partial unit, if needed. */
6981 if (per_cu->v.psymtab == NULL)
6982 process_psymtab_comp_unit (per_cu, 1, cu->language);
6983
6984 VEC_safe_push (dwarf2_per_cu_ptr,
6985 cu->per_cu->imported_symtabs, per_cu);
6986 }
6987 break;
6988 case DW_TAG_imported_declaration:
6989 add_partial_symbol (pdi, cu);
6990 break;
6991 default:
6992 break;
6993 }
6994 }
6995
6996 /* If the die has a sibling, skip to the sibling. */
6997
6998 pdi = pdi->die_sibling;
6999 }
7000 }
7001
7002 /* Functions used to compute the fully scoped name of a partial DIE.
7003
7004 Normally, this is simple. For C++, the parent DIE's fully scoped
7005 name is concatenated with "::" and the partial DIE's name.
7006 Enumerators are an exception; they use the scope of their parent
7007 enumeration type, i.e. the name of the enumeration type is not
7008 prepended to the enumerator.
7009
7010 There are two complexities. One is DW_AT_specification; in this
7011 case "parent" means the parent of the target of the specification,
7012 instead of the direct parent of the DIE. The other is compilers
7013 which do not emit DW_TAG_namespace; in this case we try to guess
7014 the fully qualified name of structure types from their members'
7015 linkage names. This must be done using the DIE's children rather
7016 than the children of any DW_AT_specification target. We only need
7017 to do this for structures at the top level, i.e. if the target of
7018 any DW_AT_specification (if any; otherwise the DIE itself) does not
7019 have a parent. */
7020
7021 /* Compute the scope prefix associated with PDI's parent, in
7022 compilation unit CU. The result will be allocated on CU's
7023 comp_unit_obstack, or a copy of the already allocated PDI->NAME
7024 field. NULL is returned if no prefix is necessary. */
7025 static const char *
7026 partial_die_parent_scope (struct partial_die_info *pdi,
7027 struct dwarf2_cu *cu)
7028 {
7029 const char *grandparent_scope;
7030 struct partial_die_info *parent, *real_pdi;
7031
7032 /* We need to look at our parent DIE; if we have a DW_AT_specification,
7033 then this means the parent of the specification DIE. */
7034
7035 real_pdi = pdi;
7036 while (real_pdi->has_specification)
7037 real_pdi = find_partial_die (real_pdi->spec_offset,
7038 real_pdi->spec_is_dwz, cu);
7039
7040 parent = real_pdi->die_parent;
7041 if (parent == NULL)
7042 return NULL;
7043
7044 if (parent->scope_set)
7045 return parent->scope;
7046
7047 fixup_partial_die (parent, cu);
7048
7049 grandparent_scope = partial_die_parent_scope (parent, cu);
7050
7051 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
7052 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
7053 Work around this problem here. */
7054 if (cu->language == language_cplus
7055 && parent->tag == DW_TAG_namespace
7056 && strcmp (parent->name, "::") == 0
7057 && grandparent_scope == NULL)
7058 {
7059 parent->scope = NULL;
7060 parent->scope_set = 1;
7061 return NULL;
7062 }
7063
7064 if (pdi->tag == DW_TAG_enumerator)
7065 /* Enumerators should not get the name of the enumeration as a prefix. */
7066 parent->scope = grandparent_scope;
7067 else if (parent->tag == DW_TAG_namespace
7068 || parent->tag == DW_TAG_module
7069 || parent->tag == DW_TAG_structure_type
7070 || parent->tag == DW_TAG_class_type
7071 || parent->tag == DW_TAG_interface_type
7072 || parent->tag == DW_TAG_union_type
7073 || parent->tag == DW_TAG_enumeration_type)
7074 {
7075 if (grandparent_scope == NULL)
7076 parent->scope = parent->name;
7077 else
7078 parent->scope = typename_concat (&cu->comp_unit_obstack,
7079 grandparent_scope,
7080 parent->name, 0, cu);
7081 }
7082 else
7083 {
7084 /* FIXME drow/2004-04-01: What should we be doing with
7085 function-local names? For partial symbols, we should probably be
7086 ignoring them. */
7087 complaint (&symfile_complaints,
7088 _("unhandled containing DIE tag %d for DIE at %d"),
7089 parent->tag, to_underlying (pdi->sect_off));
7090 parent->scope = grandparent_scope;
7091 }
7092
7093 parent->scope_set = 1;
7094 return parent->scope;
7095 }
7096
7097 /* Return the fully scoped name associated with PDI, from compilation unit
7098 CU. The result will be allocated with malloc. */
7099
7100 static char *
7101 partial_die_full_name (struct partial_die_info *pdi,
7102 struct dwarf2_cu *cu)
7103 {
7104 const char *parent_scope;
7105
7106 /* If this is a template instantiation, we can not work out the
7107 template arguments from partial DIEs. So, unfortunately, we have
7108 to go through the full DIEs. At least any work we do building
7109 types here will be reused if full symbols are loaded later. */
7110 if (pdi->has_template_arguments)
7111 {
7112 fixup_partial_die (pdi, cu);
7113
7114 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
7115 {
7116 struct die_info *die;
7117 struct attribute attr;
7118 struct dwarf2_cu *ref_cu = cu;
7119
7120 /* DW_FORM_ref_addr is using section offset. */
7121 attr.name = (enum dwarf_attribute) 0;
7122 attr.form = DW_FORM_ref_addr;
7123 attr.u.unsnd = to_underlying (pdi->sect_off);
7124 die = follow_die_ref (NULL, &attr, &ref_cu);
7125
7126 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
7127 }
7128 }
7129
7130 parent_scope = partial_die_parent_scope (pdi, cu);
7131 if (parent_scope == NULL)
7132 return NULL;
7133 else
7134 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
7135 }
7136
7137 static void
7138 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
7139 {
7140 struct objfile *objfile = cu->objfile;
7141 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7142 CORE_ADDR addr = 0;
7143 const char *actual_name = NULL;
7144 CORE_ADDR baseaddr;
7145 char *built_actual_name;
7146
7147 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7148
7149 built_actual_name = partial_die_full_name (pdi, cu);
7150 if (built_actual_name != NULL)
7151 actual_name = built_actual_name;
7152
7153 if (actual_name == NULL)
7154 actual_name = pdi->name;
7155
7156 switch (pdi->tag)
7157 {
7158 case DW_TAG_subprogram:
7159 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
7160 if (pdi->is_external || cu->language == language_ada)
7161 {
7162 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
7163 of the global scope. But in Ada, we want to be able to access
7164 nested procedures globally. So all Ada subprograms are stored
7165 in the global scope. */
7166 add_psymbol_to_list (actual_name, strlen (actual_name),
7167 built_actual_name != NULL,
7168 VAR_DOMAIN, LOC_BLOCK,
7169 &objfile->global_psymbols,
7170 addr, cu->language, objfile);
7171 }
7172 else
7173 {
7174 add_psymbol_to_list (actual_name, strlen (actual_name),
7175 built_actual_name != NULL,
7176 VAR_DOMAIN, LOC_BLOCK,
7177 &objfile->static_psymbols,
7178 addr, cu->language, objfile);
7179 }
7180
7181 if (pdi->main_subprogram && actual_name != NULL)
7182 set_objfile_main_name (objfile, actual_name, cu->language);
7183 break;
7184 case DW_TAG_constant:
7185 {
7186 struct psymbol_allocation_list *list;
7187
7188 if (pdi->is_external)
7189 list = &objfile->global_psymbols;
7190 else
7191 list = &objfile->static_psymbols;
7192 add_psymbol_to_list (actual_name, strlen (actual_name),
7193 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
7194 list, 0, cu->language, objfile);
7195 }
7196 break;
7197 case DW_TAG_variable:
7198 if (pdi->d.locdesc)
7199 addr = decode_locdesc (pdi->d.locdesc, cu);
7200
7201 if (pdi->d.locdesc
7202 && addr == 0
7203 && !dwarf2_per_objfile->has_section_at_zero)
7204 {
7205 /* A global or static variable may also have been stripped
7206 out by the linker if unused, in which case its address
7207 will be nullified; do not add such variables into partial
7208 symbol table then. */
7209 }
7210 else if (pdi->is_external)
7211 {
7212 /* Global Variable.
7213 Don't enter into the minimal symbol tables as there is
7214 a minimal symbol table entry from the ELF symbols already.
7215 Enter into partial symbol table if it has a location
7216 descriptor or a type.
7217 If the location descriptor is missing, new_symbol will create
7218 a LOC_UNRESOLVED symbol, the address of the variable will then
7219 be determined from the minimal symbol table whenever the variable
7220 is referenced.
7221 The address for the partial symbol table entry is not
7222 used by GDB, but it comes in handy for debugging partial symbol
7223 table building. */
7224
7225 if (pdi->d.locdesc || pdi->has_type)
7226 add_psymbol_to_list (actual_name, strlen (actual_name),
7227 built_actual_name != NULL,
7228 VAR_DOMAIN, LOC_STATIC,
7229 &objfile->global_psymbols,
7230 addr + baseaddr,
7231 cu->language, objfile);
7232 }
7233 else
7234 {
7235 int has_loc = pdi->d.locdesc != NULL;
7236
7237 /* Static Variable. Skip symbols whose value we cannot know (those
7238 without location descriptors or constant values). */
7239 if (!has_loc && !pdi->has_const_value)
7240 {
7241 xfree (built_actual_name);
7242 return;
7243 }
7244
7245 add_psymbol_to_list (actual_name, strlen (actual_name),
7246 built_actual_name != NULL,
7247 VAR_DOMAIN, LOC_STATIC,
7248 &objfile->static_psymbols,
7249 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
7250 cu->language, objfile);
7251 }
7252 break;
7253 case DW_TAG_typedef:
7254 case DW_TAG_base_type:
7255 case DW_TAG_subrange_type:
7256 add_psymbol_to_list (actual_name, strlen (actual_name),
7257 built_actual_name != NULL,
7258 VAR_DOMAIN, LOC_TYPEDEF,
7259 &objfile->static_psymbols,
7260 0, cu->language, objfile);
7261 break;
7262 case DW_TAG_imported_declaration:
7263 case DW_TAG_namespace:
7264 add_psymbol_to_list (actual_name, strlen (actual_name),
7265 built_actual_name != NULL,
7266 VAR_DOMAIN, LOC_TYPEDEF,
7267 &objfile->global_psymbols,
7268 0, cu->language, objfile);
7269 break;
7270 case DW_TAG_module:
7271 add_psymbol_to_list (actual_name, strlen (actual_name),
7272 built_actual_name != NULL,
7273 MODULE_DOMAIN, LOC_TYPEDEF,
7274 &objfile->global_psymbols,
7275 0, cu->language, objfile);
7276 break;
7277 case DW_TAG_class_type:
7278 case DW_TAG_interface_type:
7279 case DW_TAG_structure_type:
7280 case DW_TAG_union_type:
7281 case DW_TAG_enumeration_type:
7282 /* Skip external references. The DWARF standard says in the section
7283 about "Structure, Union, and Class Type Entries": "An incomplete
7284 structure, union or class type is represented by a structure,
7285 union or class entry that does not have a byte size attribute
7286 and that has a DW_AT_declaration attribute." */
7287 if (!pdi->has_byte_size && pdi->is_declaration)
7288 {
7289 xfree (built_actual_name);
7290 return;
7291 }
7292
7293 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
7294 static vs. global. */
7295 add_psymbol_to_list (actual_name, strlen (actual_name),
7296 built_actual_name != NULL,
7297 STRUCT_DOMAIN, LOC_TYPEDEF,
7298 cu->language == language_cplus
7299 ? &objfile->global_psymbols
7300 : &objfile->static_psymbols,
7301 0, cu->language, objfile);
7302
7303 break;
7304 case DW_TAG_enumerator:
7305 add_psymbol_to_list (actual_name, strlen (actual_name),
7306 built_actual_name != NULL,
7307 VAR_DOMAIN, LOC_CONST,
7308 cu->language == language_cplus
7309 ? &objfile->global_psymbols
7310 : &objfile->static_psymbols,
7311 0, cu->language, objfile);
7312 break;
7313 default:
7314 break;
7315 }
7316
7317 xfree (built_actual_name);
7318 }
7319
7320 /* Read a partial die corresponding to a namespace; also, add a symbol
7321 corresponding to that namespace to the symbol table. NAMESPACE is
7322 the name of the enclosing namespace. */
7323
7324 static void
7325 add_partial_namespace (struct partial_die_info *pdi,
7326 CORE_ADDR *lowpc, CORE_ADDR *highpc,
7327 int set_addrmap, struct dwarf2_cu *cu)
7328 {
7329 /* Add a symbol for the namespace. */
7330
7331 add_partial_symbol (pdi, cu);
7332
7333 /* Now scan partial symbols in that namespace. */
7334
7335 if (pdi->has_children)
7336 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
7337 }
7338
7339 /* Read a partial die corresponding to a Fortran module. */
7340
7341 static void
7342 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
7343 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
7344 {
7345 /* Add a symbol for the namespace. */
7346
7347 add_partial_symbol (pdi, cu);
7348
7349 /* Now scan partial symbols in that module. */
7350
7351 if (pdi->has_children)
7352 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
7353 }
7354
7355 /* Read a partial die corresponding to a subprogram and create a partial
7356 symbol for that subprogram. When the CU language allows it, this
7357 routine also defines a partial symbol for each nested subprogram
7358 that this subprogram contains. If SET_ADDRMAP is true, record the
7359 covered ranges in the addrmap. Set *LOWPC and *HIGHPC to the lowest
7360 and highest PC values found in PDI.
7361
7362 PDI may also be a lexical block, in which case we simply search
7363 recursively for subprograms defined inside that lexical block.
7364 Again, this is only performed when the CU language allows this
7365 type of definitions. */
7366
7367 static void
7368 add_partial_subprogram (struct partial_die_info *pdi,
7369 CORE_ADDR *lowpc, CORE_ADDR *highpc,
7370 int set_addrmap, struct dwarf2_cu *cu)
7371 {
7372 if (pdi->tag == DW_TAG_subprogram)
7373 {
7374 if (pdi->has_pc_info)
7375 {
7376 if (pdi->lowpc < *lowpc)
7377 *lowpc = pdi->lowpc;
7378 if (pdi->highpc > *highpc)
7379 *highpc = pdi->highpc;
7380 if (set_addrmap)
7381 {
7382 struct objfile *objfile = cu->objfile;
7383 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7384 CORE_ADDR baseaddr;
7385 CORE_ADDR highpc;
7386 CORE_ADDR lowpc;
7387
7388 baseaddr = ANOFFSET (objfile->section_offsets,
7389 SECT_OFF_TEXT (objfile));
7390 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
7391 pdi->lowpc + baseaddr);
7392 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
7393 pdi->highpc + baseaddr);
7394 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
7395 cu->per_cu->v.psymtab);
7396 }
7397 }
7398
7399 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
7400 {
7401 if (!pdi->is_declaration)
7402 /* Ignore subprogram DIEs that do not have a name, they are
7403 illegal. Do not emit a complaint at this point, we will
7404 do so when we convert this psymtab into a symtab. */
7405 if (pdi->name)
7406 add_partial_symbol (pdi, cu);
7407 }
7408 }
7409
7410 if (! pdi->has_children)
7411 return;
7412
7413 if (cu->language == language_ada)
7414 {
7415 pdi = pdi->die_child;
7416 while (pdi != NULL)
7417 {
7418 fixup_partial_die (pdi, cu);
7419 if (pdi->tag == DW_TAG_subprogram
7420 || pdi->tag == DW_TAG_lexical_block)
7421 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7422 pdi = pdi->die_sibling;
7423 }
7424 }
7425 }
7426
7427 /* Read a partial die corresponding to an enumeration type. */
7428
7429 static void
7430 add_partial_enumeration (struct partial_die_info *enum_pdi,
7431 struct dwarf2_cu *cu)
7432 {
7433 struct partial_die_info *pdi;
7434
7435 if (enum_pdi->name != NULL)
7436 add_partial_symbol (enum_pdi, cu);
7437
7438 pdi = enum_pdi->die_child;
7439 while (pdi)
7440 {
7441 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
7442 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
7443 else
7444 add_partial_symbol (pdi, cu);
7445 pdi = pdi->die_sibling;
7446 }
7447 }
7448
7449 /* Return the initial uleb128 in the die at INFO_PTR. */
7450
7451 static unsigned int
7452 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
7453 {
7454 unsigned int bytes_read;
7455
7456 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7457 }
7458
7459 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
7460 Return the corresponding abbrev, or NULL if the number is zero (indicating
7461 an empty DIE). In either case *BYTES_READ will be set to the length of
7462 the initial number. */
7463
7464 static struct abbrev_info *
7465 peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
7466 struct dwarf2_cu *cu)
7467 {
7468 bfd *abfd = cu->objfile->obfd;
7469 unsigned int abbrev_number;
7470 struct abbrev_info *abbrev;
7471
7472 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
7473
7474 if (abbrev_number == 0)
7475 return NULL;
7476
7477 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
7478 if (!abbrev)
7479 {
7480 error (_("Dwarf Error: Could not find abbrev number %d in %s"
7481 " at offset 0x%x [in module %s]"),
7482 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
7483 to_underlying (cu->header.sect_off), bfd_get_filename (abfd));
7484 }
7485
7486 return abbrev;
7487 }
7488
7489 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7490 Returns a pointer to the end of a series of DIEs, terminated by an empty
7491 DIE. Any children of the skipped DIEs will also be skipped. */
7492
7493 static const gdb_byte *
7494 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
7495 {
7496 struct dwarf2_cu *cu = reader->cu;
7497 struct abbrev_info *abbrev;
7498 unsigned int bytes_read;
7499
7500 while (1)
7501 {
7502 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
7503 if (abbrev == NULL)
7504 return info_ptr + bytes_read;
7505 else
7506 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
7507 }
7508 }
7509
7510 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7511 INFO_PTR should point just after the initial uleb128 of a DIE, and the
7512 abbrev corresponding to that skipped uleb128 should be passed in
7513 ABBREV. Returns a pointer to this DIE's sibling, skipping any
7514 children. */
7515
7516 static const gdb_byte *
7517 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
7518 struct abbrev_info *abbrev)
7519 {
7520 unsigned int bytes_read;
7521 struct attribute attr;
7522 bfd *abfd = reader->abfd;
7523 struct dwarf2_cu *cu = reader->cu;
7524 const gdb_byte *buffer = reader->buffer;
7525 const gdb_byte *buffer_end = reader->buffer_end;
7526 unsigned int form, i;
7527
7528 for (i = 0; i < abbrev->num_attrs; i++)
7529 {
7530 /* The only abbrev we care about is DW_AT_sibling. */
7531 if (abbrev->attrs[i].name == DW_AT_sibling)
7532 {
7533 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
7534 if (attr.form == DW_FORM_ref_addr)
7535 complaint (&symfile_complaints,
7536 _("ignoring absolute DW_AT_sibling"));
7537 else
7538 {
7539 sect_offset off = dwarf2_get_ref_die_offset (&attr);
7540 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
7541
7542 if (sibling_ptr < info_ptr)
7543 complaint (&symfile_complaints,
7544 _("DW_AT_sibling points backwards"));
7545 else if (sibling_ptr > reader->buffer_end)
7546 dwarf2_section_buffer_overflow_complaint (reader->die_section);
7547 else
7548 return sibling_ptr;
7549 }
7550 }
7551
7552 /* If it isn't DW_AT_sibling, skip this attribute. */
7553 form = abbrev->attrs[i].form;
7554 skip_attribute:
7555 switch (form)
7556 {
7557 case DW_FORM_ref_addr:
7558 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
7559 and later it is offset sized. */
7560 if (cu->header.version == 2)
7561 info_ptr += cu->header.addr_size;
7562 else
7563 info_ptr += cu->header.offset_size;
7564 break;
7565 case DW_FORM_GNU_ref_alt:
7566 info_ptr += cu->header.offset_size;
7567 break;
7568 case DW_FORM_addr:
7569 info_ptr += cu->header.addr_size;
7570 break;
7571 case DW_FORM_data1:
7572 case DW_FORM_ref1:
7573 case DW_FORM_flag:
7574 info_ptr += 1;
7575 break;
7576 case DW_FORM_flag_present:
7577 case DW_FORM_implicit_const:
7578 break;
7579 case DW_FORM_data2:
7580 case DW_FORM_ref2:
7581 info_ptr += 2;
7582 break;
7583 case DW_FORM_data4:
7584 case DW_FORM_ref4:
7585 info_ptr += 4;
7586 break;
7587 case DW_FORM_data8:
7588 case DW_FORM_ref8:
7589 case DW_FORM_ref_sig8:
7590 info_ptr += 8;
7591 break;
7592 case DW_FORM_data16:
7593 info_ptr += 16;
7594 break;
7595 case DW_FORM_string:
7596 read_direct_string (abfd, info_ptr, &bytes_read);
7597 info_ptr += bytes_read;
7598 break;
7599 case DW_FORM_sec_offset:
7600 case DW_FORM_strp:
7601 case DW_FORM_GNU_strp_alt:
7602 info_ptr += cu->header.offset_size;
7603 break;
7604 case DW_FORM_exprloc:
7605 case DW_FORM_block:
7606 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7607 info_ptr += bytes_read;
7608 break;
7609 case DW_FORM_block1:
7610 info_ptr += 1 + read_1_byte (abfd, info_ptr);
7611 break;
7612 case DW_FORM_block2:
7613 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
7614 break;
7615 case DW_FORM_block4:
7616 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
7617 break;
7618 case DW_FORM_sdata:
7619 case DW_FORM_udata:
7620 case DW_FORM_ref_udata:
7621 case DW_FORM_GNU_addr_index:
7622 case DW_FORM_GNU_str_index:
7623 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
7624 break;
7625 case DW_FORM_indirect:
7626 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7627 info_ptr += bytes_read;
7628 /* We need to continue parsing from here, so just go back to
7629 the top. */
7630 goto skip_attribute;
7631
7632 default:
7633 error (_("Dwarf Error: Cannot handle %s "
7634 "in DWARF reader [in module %s]"),
7635 dwarf_form_name (form),
7636 bfd_get_filename (abfd));
7637 }
7638 }
7639
7640 if (abbrev->has_children)
7641 return skip_children (reader, info_ptr);
7642 else
7643 return info_ptr;
7644 }
7645
7646 /* Locate ORIG_PDI's sibling.
7647 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
7648
7649 static const gdb_byte *
7650 locate_pdi_sibling (const struct die_reader_specs *reader,
7651 struct partial_die_info *orig_pdi,
7652 const gdb_byte *info_ptr)
7653 {
7654 /* Do we know the sibling already? */
7655
7656 if (orig_pdi->sibling)
7657 return orig_pdi->sibling;
7658
7659 /* Are there any children to deal with? */
7660
7661 if (!orig_pdi->has_children)
7662 return info_ptr;
7663
7664 /* Skip the children the long way. */
7665
7666 return skip_children (reader, info_ptr);
7667 }
7668
7669 /* Expand this partial symbol table into a full symbol table. SELF is
7670 not NULL. */
7671
7672 static void
7673 dwarf2_read_symtab (struct partial_symtab *self,
7674 struct objfile *objfile)
7675 {
7676 if (self->readin)
7677 {
7678 warning (_("bug: psymtab for %s is already read in."),
7679 self->filename);
7680 }
7681 else
7682 {
7683 if (info_verbose)
7684 {
7685 printf_filtered (_("Reading in symbols for %s..."),
7686 self->filename);
7687 gdb_flush (gdb_stdout);
7688 }
7689
7690 /* Restore our global data. */
7691 dwarf2_per_objfile
7692 = (struct dwarf2_per_objfile *) objfile_data (objfile,
7693 dwarf2_objfile_data_key);
7694
7695 /* If this psymtab is constructed from a debug-only objfile, the
7696 has_section_at_zero flag will not necessarily be correct. We
7697 can get the correct value for this flag by looking at the data
7698 associated with the (presumably stripped) associated objfile. */
7699 if (objfile->separate_debug_objfile_backlink)
7700 {
7701 struct dwarf2_per_objfile *dpo_backlink
7702 = ((struct dwarf2_per_objfile *)
7703 objfile_data (objfile->separate_debug_objfile_backlink,
7704 dwarf2_objfile_data_key));
7705
7706 dwarf2_per_objfile->has_section_at_zero
7707 = dpo_backlink->has_section_at_zero;
7708 }
7709
7710 dwarf2_per_objfile->reading_partial_symbols = 0;
7711
7712 psymtab_to_symtab_1 (self);
7713
7714 /* Finish up the debug error message. */
7715 if (info_verbose)
7716 printf_filtered (_("done.\n"));
7717 }
7718
7719 process_cu_includes ();
7720 }
7721 \f
7722 /* Reading in full CUs. */
7723
7724 /* Add PER_CU to the queue. */
7725
7726 static void
7727 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
7728 enum language pretend_language)
7729 {
7730 struct dwarf2_queue_item *item;
7731
7732 per_cu->queued = 1;
7733 item = XNEW (struct dwarf2_queue_item);
7734 item->per_cu = per_cu;
7735 item->pretend_language = pretend_language;
7736 item->next = NULL;
7737
7738 if (dwarf2_queue == NULL)
7739 dwarf2_queue = item;
7740 else
7741 dwarf2_queue_tail->next = item;
7742
7743 dwarf2_queue_tail = item;
7744 }
7745
7746 /* If PER_CU is not yet queued, add it to the queue.
7747 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
7748 dependency.
7749 The result is non-zero if PER_CU was queued, otherwise the result is zero
7750 meaning either PER_CU is already queued or it is already loaded.
7751
7752 N.B. There is an invariant here that if a CU is queued then it is loaded.
7753 The caller is required to load PER_CU if we return non-zero. */
7754
7755 static int
7756 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
7757 struct dwarf2_per_cu_data *per_cu,
7758 enum language pretend_language)
7759 {
7760 /* We may arrive here during partial symbol reading, if we need full
7761 DIEs to process an unusual case (e.g. template arguments). Do
7762 not queue PER_CU, just tell our caller to load its DIEs. */
7763 if (dwarf2_per_objfile->reading_partial_symbols)
7764 {
7765 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
7766 return 1;
7767 return 0;
7768 }
7769
7770 /* Mark the dependence relation so that we don't flush PER_CU
7771 too early. */
7772 if (dependent_cu != NULL)
7773 dwarf2_add_dependence (dependent_cu, per_cu);
7774
7775 /* If it's already on the queue, we have nothing to do. */
7776 if (per_cu->queued)
7777 return 0;
7778
7779 /* If the compilation unit is already loaded, just mark it as
7780 used. */
7781 if (per_cu->cu != NULL)
7782 {
7783 per_cu->cu->last_used = 0;
7784 return 0;
7785 }
7786
7787 /* Add it to the queue. */
7788 queue_comp_unit (per_cu, pretend_language);
7789
7790 return 1;
7791 }
7792
7793 /* Process the queue. */
7794
7795 static void
7796 process_queue (void)
7797 {
7798 struct dwarf2_queue_item *item, *next_item;
7799
7800 if (dwarf_read_debug)
7801 {
7802 fprintf_unfiltered (gdb_stdlog,
7803 "Expanding one or more symtabs of objfile %s ...\n",
7804 objfile_name (dwarf2_per_objfile->objfile));
7805 }
7806
7807 /* The queue starts out with one item, but following a DIE reference
7808 may load a new CU, adding it to the end of the queue. */
7809 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
7810 {
7811 if ((dwarf2_per_objfile->using_index
7812 ? !item->per_cu->v.quick->compunit_symtab
7813 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
7814 /* Skip dummy CUs. */
7815 && item->per_cu->cu != NULL)
7816 {
7817 struct dwarf2_per_cu_data *per_cu = item->per_cu;
7818 unsigned int debug_print_threshold;
7819 char buf[100];
7820
7821 if (per_cu->is_debug_types)
7822 {
7823 struct signatured_type *sig_type =
7824 (struct signatured_type *) per_cu;
7825
7826 sprintf (buf, "TU %s at offset 0x%x",
7827 hex_string (sig_type->signature),
7828 to_underlying (per_cu->sect_off));
7829 /* There can be 100s of TUs.
7830 Only print them in verbose mode. */
7831 debug_print_threshold = 2;
7832 }
7833 else
7834 {
7835 sprintf (buf, "CU at offset 0x%x",
7836 to_underlying (per_cu->sect_off));
7837 debug_print_threshold = 1;
7838 }
7839
7840 if (dwarf_read_debug >= debug_print_threshold)
7841 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
7842
7843 if (per_cu->is_debug_types)
7844 process_full_type_unit (per_cu, item->pretend_language);
7845 else
7846 process_full_comp_unit (per_cu, item->pretend_language);
7847
7848 if (dwarf_read_debug >= debug_print_threshold)
7849 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
7850 }
7851
7852 item->per_cu->queued = 0;
7853 next_item = item->next;
7854 xfree (item);
7855 }
7856
7857 dwarf2_queue_tail = NULL;
7858
7859 if (dwarf_read_debug)
7860 {
7861 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
7862 objfile_name (dwarf2_per_objfile->objfile));
7863 }
7864 }
7865
7866 /* Free all allocated queue entries. This function only releases anything if
7867 an error was thrown; if the queue was processed then it would have been
7868 freed as we went along. */
7869
7870 static void
7871 dwarf2_release_queue (void *dummy)
7872 {
7873 struct dwarf2_queue_item *item, *last;
7874
7875 item = dwarf2_queue;
7876 while (item)
7877 {
7878 /* Anything still marked queued is likely to be in an
7879 inconsistent state, so discard it. */
7880 if (item->per_cu->queued)
7881 {
7882 if (item->per_cu->cu != NULL)
7883 free_one_cached_comp_unit (item->per_cu);
7884 item->per_cu->queued = 0;
7885 }
7886
7887 last = item;
7888 item = item->next;
7889 xfree (last);
7890 }
7891
7892 dwarf2_queue = dwarf2_queue_tail = NULL;
7893 }
7894
7895 /* Read in full symbols for PST, and anything it depends on. */
7896
7897 static void
7898 psymtab_to_symtab_1 (struct partial_symtab *pst)
7899 {
7900 struct dwarf2_per_cu_data *per_cu;
7901 int i;
7902
7903 if (pst->readin)
7904 return;
7905
7906 for (i = 0; i < pst->number_of_dependencies; i++)
7907 if (!pst->dependencies[i]->readin
7908 && pst->dependencies[i]->user == NULL)
7909 {
7910 /* Inform about additional files that need to be read in. */
7911 if (info_verbose)
7912 {
7913 /* FIXME: i18n: Need to make this a single string. */
7914 fputs_filtered (" ", gdb_stdout);
7915 wrap_here ("");
7916 fputs_filtered ("and ", gdb_stdout);
7917 wrap_here ("");
7918 printf_filtered ("%s...", pst->dependencies[i]->filename);
7919 wrap_here (""); /* Flush output. */
7920 gdb_flush (gdb_stdout);
7921 }
7922 psymtab_to_symtab_1 (pst->dependencies[i]);
7923 }
7924
7925 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
7926
7927 if (per_cu == NULL)
7928 {
7929 /* It's an include file, no symbols to read for it.
7930 Everything is in the parent symtab. */
7931 pst->readin = 1;
7932 return;
7933 }
7934
7935 dw2_do_instantiate_symtab (per_cu);
7936 }
7937
7938 /* Trivial hash function for die_info: the hash value of a DIE
7939 is its offset in .debug_info for this objfile. */
7940
7941 static hashval_t
7942 die_hash (const void *item)
7943 {
7944 const struct die_info *die = (const struct die_info *) item;
7945
7946 return to_underlying (die->sect_off);
7947 }
7948
7949 /* Trivial comparison function for die_info structures: two DIEs
7950 are equal if they have the same offset. */
7951
7952 static int
7953 die_eq (const void *item_lhs, const void *item_rhs)
7954 {
7955 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
7956 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
7957
7958 return die_lhs->sect_off == die_rhs->sect_off;
7959 }
7960
7961 /* die_reader_func for load_full_comp_unit.
7962 This is identical to read_signatured_type_reader,
7963 but is kept separate for now. */
7964
7965 static void
7966 load_full_comp_unit_reader (const struct die_reader_specs *reader,
7967 const gdb_byte *info_ptr,
7968 struct die_info *comp_unit_die,
7969 int has_children,
7970 void *data)
7971 {
7972 struct dwarf2_cu *cu = reader->cu;
7973 enum language *language_ptr = (enum language *) data;
7974
7975 gdb_assert (cu->die_hash == NULL);
7976 cu->die_hash =
7977 htab_create_alloc_ex (cu->header.length / 12,
7978 die_hash,
7979 die_eq,
7980 NULL,
7981 &cu->comp_unit_obstack,
7982 hashtab_obstack_allocate,
7983 dummy_obstack_deallocate);
7984
7985 if (has_children)
7986 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
7987 &info_ptr, comp_unit_die);
7988 cu->dies = comp_unit_die;
7989 /* comp_unit_die is not stored in die_hash, no need. */
7990
7991 /* We try not to read any attributes in this function, because not
7992 all CUs needed for references have been loaded yet, and symbol
7993 table processing isn't initialized. But we have to set the CU language,
7994 or we won't be able to build types correctly.
7995 Similarly, if we do not read the producer, we can not apply
7996 producer-specific interpretation. */
7997 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
7998 }
7999
8000 /* Load the DIEs associated with PER_CU into memory. */
8001
8002 static void
8003 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8004 enum language pretend_language)
8005 {
8006 gdb_assert (! this_cu->is_debug_types);
8007
8008 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
8009 load_full_comp_unit_reader, &pretend_language);
8010 }
8011
8012 /* Add a DIE to the delayed physname list. */
8013
8014 static void
8015 add_to_method_list (struct type *type, int fnfield_index, int index,
8016 const char *name, struct die_info *die,
8017 struct dwarf2_cu *cu)
8018 {
8019 struct delayed_method_info mi;
8020 mi.type = type;
8021 mi.fnfield_index = fnfield_index;
8022 mi.index = index;
8023 mi.name = name;
8024 mi.die = die;
8025 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
8026 }
8027
8028 /* A cleanup for freeing the delayed method list. */
8029
8030 static void
8031 free_delayed_list (void *ptr)
8032 {
8033 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
8034 if (cu->method_list != NULL)
8035 {
8036 VEC_free (delayed_method_info, cu->method_list);
8037 cu->method_list = NULL;
8038 }
8039 }
8040
8041 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8042 "const" / "volatile". If so, decrements LEN by the length of the
8043 modifier and return true. Otherwise return false. */
8044
8045 template<size_t N>
8046 static bool
8047 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8048 {
8049 size_t mod_len = sizeof (mod) - 1;
8050 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8051 {
8052 len -= mod_len;
8053 return true;
8054 }
8055 return false;
8056 }
8057
8058 /* Compute the physnames of any methods on the CU's method list.
8059
8060 The computation of method physnames is delayed in order to avoid the
8061 (bad) condition that one of the method's formal parameters is of an as yet
8062 incomplete type. */
8063
8064 static void
8065 compute_delayed_physnames (struct dwarf2_cu *cu)
8066 {
8067 int i;
8068 struct delayed_method_info *mi;
8069
8070 /* Only C++ delays computing physnames. */
8071 if (VEC_empty (delayed_method_info, cu->method_list))
8072 return;
8073 gdb_assert (cu->language == language_cplus);
8074
8075 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
8076 {
8077 const char *physname;
8078 struct fn_fieldlist *fn_flp
8079 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
8080 physname = dwarf2_physname (mi->name, mi->die, cu);
8081 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi->index)
8082 = physname ? physname : "";
8083
8084 /* Since there's no tag to indicate whether a method is a
8085 const/volatile overload, extract that information out of the
8086 demangled name. */
8087 if (physname != NULL)
8088 {
8089 size_t len = strlen (physname);
8090
8091 while (1)
8092 {
8093 if (physname[len] == ')') /* shortcut */
8094 break;
8095 else if (check_modifier (physname, len, " const"))
8096 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi->index) = 1;
8097 else if (check_modifier (physname, len, " volatile"))
8098 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi->index) = 1;
8099 else
8100 break;
8101 }
8102 }
8103 }
8104 }
8105
8106 /* Go objects should be embedded in a DW_TAG_module DIE,
8107 and it's not clear if/how imported objects will appear.
8108 To keep Go support simple until that's worked out,
8109 go back through what we've read and create something usable.
8110 We could do this while processing each DIE, and feels kinda cleaner,
8111 but that way is more invasive.
8112 This is to, for example, allow the user to type "p var" or "b main"
8113 without having to specify the package name, and allow lookups
8114 of module.object to work in contexts that use the expression
8115 parser. */
8116
8117 static void
8118 fixup_go_packaging (struct dwarf2_cu *cu)
8119 {
8120 char *package_name = NULL;
8121 struct pending *list;
8122 int i;
8123
8124 for (list = global_symbols; list != NULL; list = list->next)
8125 {
8126 for (i = 0; i < list->nsyms; ++i)
8127 {
8128 struct symbol *sym = list->symbol[i];
8129
8130 if (SYMBOL_LANGUAGE (sym) == language_go
8131 && SYMBOL_CLASS (sym) == LOC_BLOCK)
8132 {
8133 char *this_package_name = go_symbol_package_name (sym);
8134
8135 if (this_package_name == NULL)
8136 continue;
8137 if (package_name == NULL)
8138 package_name = this_package_name;
8139 else
8140 {
8141 if (strcmp (package_name, this_package_name) != 0)
8142 complaint (&symfile_complaints,
8143 _("Symtab %s has objects from two different Go packages: %s and %s"),
8144 (symbol_symtab (sym) != NULL
8145 ? symtab_to_filename_for_display
8146 (symbol_symtab (sym))
8147 : objfile_name (cu->objfile)),
8148 this_package_name, package_name);
8149 xfree (this_package_name);
8150 }
8151 }
8152 }
8153 }
8154
8155 if (package_name != NULL)
8156 {
8157 struct objfile *objfile = cu->objfile;
8158 const char *saved_package_name
8159 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
8160 package_name,
8161 strlen (package_name));
8162 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
8163 saved_package_name);
8164 struct symbol *sym;
8165
8166 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8167
8168 sym = allocate_symbol (objfile);
8169 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
8170 SYMBOL_SET_NAMES (sym, saved_package_name,
8171 strlen (saved_package_name), 0, objfile);
8172 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
8173 e.g., "main" finds the "main" module and not C's main(). */
8174 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
8175 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
8176 SYMBOL_TYPE (sym) = type;
8177
8178 add_symbol_to_list (sym, &global_symbols);
8179
8180 xfree (package_name);
8181 }
8182 }
8183
8184 /* Return the symtab for PER_CU. This works properly regardless of
8185 whether we're using the index or psymtabs. */
8186
8187 static struct compunit_symtab *
8188 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
8189 {
8190 return (dwarf2_per_objfile->using_index
8191 ? per_cu->v.quick->compunit_symtab
8192 : per_cu->v.psymtab->compunit_symtab);
8193 }
8194
8195 /* A helper function for computing the list of all symbol tables
8196 included by PER_CU. */
8197
8198 static void
8199 recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
8200 htab_t all_children, htab_t all_type_symtabs,
8201 struct dwarf2_per_cu_data *per_cu,
8202 struct compunit_symtab *immediate_parent)
8203 {
8204 void **slot;
8205 int ix;
8206 struct compunit_symtab *cust;
8207 struct dwarf2_per_cu_data *iter;
8208
8209 slot = htab_find_slot (all_children, per_cu, INSERT);
8210 if (*slot != NULL)
8211 {
8212 /* This inclusion and its children have been processed. */
8213 return;
8214 }
8215
8216 *slot = per_cu;
8217 /* Only add a CU if it has a symbol table. */
8218 cust = get_compunit_symtab (per_cu);
8219 if (cust != NULL)
8220 {
8221 /* If this is a type unit only add its symbol table if we haven't
8222 seen it yet (type unit per_cu's can share symtabs). */
8223 if (per_cu->is_debug_types)
8224 {
8225 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
8226 if (*slot == NULL)
8227 {
8228 *slot = cust;
8229 VEC_safe_push (compunit_symtab_ptr, *result, cust);
8230 if (cust->user == NULL)
8231 cust->user = immediate_parent;
8232 }
8233 }
8234 else
8235 {
8236 VEC_safe_push (compunit_symtab_ptr, *result, cust);
8237 if (cust->user == NULL)
8238 cust->user = immediate_parent;
8239 }
8240 }
8241
8242 for (ix = 0;
8243 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
8244 ++ix)
8245 {
8246 recursively_compute_inclusions (result, all_children,
8247 all_type_symtabs, iter, cust);
8248 }
8249 }
8250
8251 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
8252 PER_CU. */
8253
8254 static void
8255 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
8256 {
8257 gdb_assert (! per_cu->is_debug_types);
8258
8259 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
8260 {
8261 int ix, len;
8262 struct dwarf2_per_cu_data *per_cu_iter;
8263 struct compunit_symtab *compunit_symtab_iter;
8264 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
8265 htab_t all_children, all_type_symtabs;
8266 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
8267
8268 /* If we don't have a symtab, we can just skip this case. */
8269 if (cust == NULL)
8270 return;
8271
8272 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
8273 NULL, xcalloc, xfree);
8274 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
8275 NULL, xcalloc, xfree);
8276
8277 for (ix = 0;
8278 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
8279 ix, per_cu_iter);
8280 ++ix)
8281 {
8282 recursively_compute_inclusions (&result_symtabs, all_children,
8283 all_type_symtabs, per_cu_iter,
8284 cust);
8285 }
8286
8287 /* Now we have a transitive closure of all the included symtabs. */
8288 len = VEC_length (compunit_symtab_ptr, result_symtabs);
8289 cust->includes
8290 = XOBNEWVEC (&dwarf2_per_objfile->objfile->objfile_obstack,
8291 struct compunit_symtab *, len + 1);
8292 for (ix = 0;
8293 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
8294 compunit_symtab_iter);
8295 ++ix)
8296 cust->includes[ix] = compunit_symtab_iter;
8297 cust->includes[len] = NULL;
8298
8299 VEC_free (compunit_symtab_ptr, result_symtabs);
8300 htab_delete (all_children);
8301 htab_delete (all_type_symtabs);
8302 }
8303 }
8304
8305 /* Compute the 'includes' field for the symtabs of all the CUs we just
8306 read. */
8307
8308 static void
8309 process_cu_includes (void)
8310 {
8311 int ix;
8312 struct dwarf2_per_cu_data *iter;
8313
8314 for (ix = 0;
8315 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
8316 ix, iter);
8317 ++ix)
8318 {
8319 if (! iter->is_debug_types)
8320 compute_compunit_symtab_includes (iter);
8321 }
8322
8323 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
8324 }
8325
8326 /* Generate full symbol information for PER_CU, whose DIEs have
8327 already been loaded into memory. */
8328
8329 static void
8330 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
8331 enum language pretend_language)
8332 {
8333 struct dwarf2_cu *cu = per_cu->cu;
8334 struct objfile *objfile = per_cu->objfile;
8335 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8336 CORE_ADDR lowpc, highpc;
8337 struct compunit_symtab *cust;
8338 struct cleanup *back_to, *delayed_list_cleanup;
8339 CORE_ADDR baseaddr;
8340 struct block *static_block;
8341 CORE_ADDR addr;
8342
8343 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8344
8345 buildsym_init ();
8346 back_to = make_cleanup (really_free_pendings, NULL);
8347 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
8348
8349 cu->list_in_scope = &file_symbols;
8350
8351 cu->language = pretend_language;
8352 cu->language_defn = language_def (cu->language);
8353
8354 /* Do line number decoding in read_file_scope () */
8355 process_die (cu->dies, cu);
8356
8357 /* For now fudge the Go package. */
8358 if (cu->language == language_go)
8359 fixup_go_packaging (cu);
8360
8361 /* Now that we have processed all the DIEs in the CU, all the types
8362 should be complete, and it should now be safe to compute all of the
8363 physnames. */
8364 compute_delayed_physnames (cu);
8365 do_cleanups (delayed_list_cleanup);
8366
8367 /* Some compilers don't define a DW_AT_high_pc attribute for the
8368 compilation unit. If the DW_AT_high_pc is missing, synthesize
8369 it, by scanning the DIE's below the compilation unit. */
8370 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
8371
8372 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
8373 static_block = end_symtab_get_static_block (addr, 0, 1);
8374
8375 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
8376 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
8377 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
8378 addrmap to help ensure it has an accurate map of pc values belonging to
8379 this comp unit. */
8380 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
8381
8382 cust = end_symtab_from_static_block (static_block,
8383 SECT_OFF_TEXT (objfile), 0);
8384
8385 if (cust != NULL)
8386 {
8387 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
8388
8389 /* Set symtab language to language from DW_AT_language. If the
8390 compilation is from a C file generated by language preprocessors, do
8391 not set the language if it was already deduced by start_subfile. */
8392 if (!(cu->language == language_c
8393 && COMPUNIT_FILETABS (cust)->language != language_unknown))
8394 COMPUNIT_FILETABS (cust)->language = cu->language;
8395
8396 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
8397 produce DW_AT_location with location lists but it can be possibly
8398 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
8399 there were bugs in prologue debug info, fixed later in GCC-4.5
8400 by "unwind info for epilogues" patch (which is not directly related).
8401
8402 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
8403 needed, it would be wrong due to missing DW_AT_producer there.
8404
8405 Still one can confuse GDB by using non-standard GCC compilation
8406 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
8407 */
8408 if (cu->has_loclist && gcc_4_minor >= 5)
8409 cust->locations_valid = 1;
8410
8411 if (gcc_4_minor >= 5)
8412 cust->epilogue_unwind_valid = 1;
8413
8414 cust->call_site_htab = cu->call_site_htab;
8415 }
8416
8417 if (dwarf2_per_objfile->using_index)
8418 per_cu->v.quick->compunit_symtab = cust;
8419 else
8420 {
8421 struct partial_symtab *pst = per_cu->v.psymtab;
8422 pst->compunit_symtab = cust;
8423 pst->readin = 1;
8424 }
8425
8426 /* Push it for inclusion processing later. */
8427 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
8428
8429 do_cleanups (back_to);
8430 }
8431
8432 /* Generate full symbol information for type unit PER_CU, whose DIEs have
8433 already been loaded into memory. */
8434
8435 static void
8436 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
8437 enum language pretend_language)
8438 {
8439 struct dwarf2_cu *cu = per_cu->cu;
8440 struct objfile *objfile = per_cu->objfile;
8441 struct compunit_symtab *cust;
8442 struct cleanup *back_to, *delayed_list_cleanup;
8443 struct signatured_type *sig_type;
8444
8445 gdb_assert (per_cu->is_debug_types);
8446 sig_type = (struct signatured_type *) per_cu;
8447
8448 buildsym_init ();
8449 back_to = make_cleanup (really_free_pendings, NULL);
8450 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
8451
8452 cu->list_in_scope = &file_symbols;
8453
8454 cu->language = pretend_language;
8455 cu->language_defn = language_def (cu->language);
8456
8457 /* The symbol tables are set up in read_type_unit_scope. */
8458 process_die (cu->dies, cu);
8459
8460 /* For now fudge the Go package. */
8461 if (cu->language == language_go)
8462 fixup_go_packaging (cu);
8463
8464 /* Now that we have processed all the DIEs in the CU, all the types
8465 should be complete, and it should now be safe to compute all of the
8466 physnames. */
8467 compute_delayed_physnames (cu);
8468 do_cleanups (delayed_list_cleanup);
8469
8470 /* TUs share symbol tables.
8471 If this is the first TU to use this symtab, complete the construction
8472 of it with end_expandable_symtab. Otherwise, complete the addition of
8473 this TU's symbols to the existing symtab. */
8474 if (sig_type->type_unit_group->compunit_symtab == NULL)
8475 {
8476 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8477 sig_type->type_unit_group->compunit_symtab = cust;
8478
8479 if (cust != NULL)
8480 {
8481 /* Set symtab language to language from DW_AT_language. If the
8482 compilation is from a C file generated by language preprocessors,
8483 do not set the language if it was already deduced by
8484 start_subfile. */
8485 if (!(cu->language == language_c
8486 && COMPUNIT_FILETABS (cust)->language != language_c))
8487 COMPUNIT_FILETABS (cust)->language = cu->language;
8488 }
8489 }
8490 else
8491 {
8492 augment_type_symtab ();
8493 cust = sig_type->type_unit_group->compunit_symtab;
8494 }
8495
8496 if (dwarf2_per_objfile->using_index)
8497 per_cu->v.quick->compunit_symtab = cust;
8498 else
8499 {
8500 struct partial_symtab *pst = per_cu->v.psymtab;
8501 pst->compunit_symtab = cust;
8502 pst->readin = 1;
8503 }
8504
8505 do_cleanups (back_to);
8506 }
8507
8508 /* Process an imported unit DIE. */
8509
8510 static void
8511 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
8512 {
8513 struct attribute *attr;
8514
8515 /* For now we don't handle imported units in type units. */
8516 if (cu->per_cu->is_debug_types)
8517 {
8518 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8519 " supported in type units [in module %s]"),
8520 objfile_name (cu->objfile));
8521 }
8522
8523 attr = dwarf2_attr (die, DW_AT_import, cu);
8524 if (attr != NULL)
8525 {
8526 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
8527 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
8528 dwarf2_per_cu_data *per_cu
8529 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, cu->objfile);
8530
8531 /* If necessary, add it to the queue and load its DIEs. */
8532 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
8533 load_full_comp_unit (per_cu, cu->language);
8534
8535 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8536 per_cu);
8537 }
8538 }
8539
8540 /* RAII object that represents a process_die scope: i.e.,
8541 starts/finishes processing a DIE. */
8542 class process_die_scope
8543 {
8544 public:
8545 process_die_scope (die_info *die, dwarf2_cu *cu)
8546 : m_die (die), m_cu (cu)
8547 {
8548 /* We should only be processing DIEs not already in process. */
8549 gdb_assert (!m_die->in_process);
8550 m_die->in_process = true;
8551 }
8552
8553 ~process_die_scope ()
8554 {
8555 m_die->in_process = false;
8556
8557 /* If we're done processing the DIE for the CU that owns the line
8558 header, we don't need the line header anymore. */
8559 if (m_cu->line_header_die_owner == m_die)
8560 {
8561 delete m_cu->line_header;
8562 m_cu->line_header = NULL;
8563 m_cu->line_header_die_owner = NULL;
8564 }
8565 }
8566
8567 private:
8568 die_info *m_die;
8569 dwarf2_cu *m_cu;
8570 };
8571
8572 /* Process a die and its children. */
8573
8574 static void
8575 process_die (struct die_info *die, struct dwarf2_cu *cu)
8576 {
8577 process_die_scope scope (die, cu);
8578
8579 switch (die->tag)
8580 {
8581 case DW_TAG_padding:
8582 break;
8583 case DW_TAG_compile_unit:
8584 case DW_TAG_partial_unit:
8585 read_file_scope (die, cu);
8586 break;
8587 case DW_TAG_type_unit:
8588 read_type_unit_scope (die, cu);
8589 break;
8590 case DW_TAG_subprogram:
8591 case DW_TAG_inlined_subroutine:
8592 read_func_scope (die, cu);
8593 break;
8594 case DW_TAG_lexical_block:
8595 case DW_TAG_try_block:
8596 case DW_TAG_catch_block:
8597 read_lexical_block_scope (die, cu);
8598 break;
8599 case DW_TAG_call_site:
8600 case DW_TAG_GNU_call_site:
8601 read_call_site_scope (die, cu);
8602 break;
8603 case DW_TAG_class_type:
8604 case DW_TAG_interface_type:
8605 case DW_TAG_structure_type:
8606 case DW_TAG_union_type:
8607 process_structure_scope (die, cu);
8608 break;
8609 case DW_TAG_enumeration_type:
8610 process_enumeration_scope (die, cu);
8611 break;
8612
8613 /* These dies have a type, but processing them does not create
8614 a symbol or recurse to process the children. Therefore we can
8615 read them on-demand through read_type_die. */
8616 case DW_TAG_subroutine_type:
8617 case DW_TAG_set_type:
8618 case DW_TAG_array_type:
8619 case DW_TAG_pointer_type:
8620 case DW_TAG_ptr_to_member_type:
8621 case DW_TAG_reference_type:
8622 case DW_TAG_rvalue_reference_type:
8623 case DW_TAG_string_type:
8624 break;
8625
8626 case DW_TAG_base_type:
8627 case DW_TAG_subrange_type:
8628 case DW_TAG_typedef:
8629 /* Add a typedef symbol for the type definition, if it has a
8630 DW_AT_name. */
8631 new_symbol (die, read_type_die (die, cu), cu);
8632 break;
8633 case DW_TAG_common_block:
8634 read_common_block (die, cu);
8635 break;
8636 case DW_TAG_common_inclusion:
8637 break;
8638 case DW_TAG_namespace:
8639 cu->processing_has_namespace_info = 1;
8640 read_namespace (die, cu);
8641 break;
8642 case DW_TAG_module:
8643 cu->processing_has_namespace_info = 1;
8644 read_module (die, cu);
8645 break;
8646 case DW_TAG_imported_declaration:
8647 cu->processing_has_namespace_info = 1;
8648 if (read_namespace_alias (die, cu))
8649 break;
8650 /* The declaration is not a global namespace alias: fall through. */
8651 case DW_TAG_imported_module:
8652 cu->processing_has_namespace_info = 1;
8653 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
8654 || cu->language != language_fortran))
8655 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
8656 dwarf_tag_name (die->tag));
8657 read_import_statement (die, cu);
8658 break;
8659
8660 case DW_TAG_imported_unit:
8661 process_imported_unit_die (die, cu);
8662 break;
8663
8664 default:
8665 new_symbol (die, NULL, cu);
8666 break;
8667 }
8668 }
8669 \f
8670 /* DWARF name computation. */
8671
8672 /* A helper function for dwarf2_compute_name which determines whether DIE
8673 needs to have the name of the scope prepended to the name listed in the
8674 die. */
8675
8676 static int
8677 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
8678 {
8679 struct attribute *attr;
8680
8681 switch (die->tag)
8682 {
8683 case DW_TAG_namespace:
8684 case DW_TAG_typedef:
8685 case DW_TAG_class_type:
8686 case DW_TAG_interface_type:
8687 case DW_TAG_structure_type:
8688 case DW_TAG_union_type:
8689 case DW_TAG_enumeration_type:
8690 case DW_TAG_enumerator:
8691 case DW_TAG_subprogram:
8692 case DW_TAG_inlined_subroutine:
8693 case DW_TAG_member:
8694 case DW_TAG_imported_declaration:
8695 return 1;
8696
8697 case DW_TAG_variable:
8698 case DW_TAG_constant:
8699 /* We only need to prefix "globally" visible variables. These include
8700 any variable marked with DW_AT_external or any variable that
8701 lives in a namespace. [Variables in anonymous namespaces
8702 require prefixing, but they are not DW_AT_external.] */
8703
8704 if (dwarf2_attr (die, DW_AT_specification, cu))
8705 {
8706 struct dwarf2_cu *spec_cu = cu;
8707
8708 return die_needs_namespace (die_specification (die, &spec_cu),
8709 spec_cu);
8710 }
8711
8712 attr = dwarf2_attr (die, DW_AT_external, cu);
8713 if (attr == NULL && die->parent->tag != DW_TAG_namespace
8714 && die->parent->tag != DW_TAG_module)
8715 return 0;
8716 /* A variable in a lexical block of some kind does not need a
8717 namespace, even though in C++ such variables may be external
8718 and have a mangled name. */
8719 if (die->parent->tag == DW_TAG_lexical_block
8720 || die->parent->tag == DW_TAG_try_block
8721 || die->parent->tag == DW_TAG_catch_block
8722 || die->parent->tag == DW_TAG_subprogram)
8723 return 0;
8724 return 1;
8725
8726 default:
8727 return 0;
8728 }
8729 }
8730
8731 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
8732 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
8733 defined for the given DIE. */
8734
8735 static struct attribute *
8736 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
8737 {
8738 struct attribute *attr;
8739
8740 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
8741 if (attr == NULL)
8742 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
8743
8744 return attr;
8745 }
8746
8747 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
8748 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
8749 defined for the given DIE. */
8750
8751 static const char *
8752 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
8753 {
8754 const char *linkage_name;
8755
8756 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
8757 if (linkage_name == NULL)
8758 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
8759
8760 return linkage_name;
8761 }
8762
8763 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
8764 compute the physname for the object, which include a method's:
8765 - formal parameters (C++),
8766 - receiver type (Go),
8767
8768 The term "physname" is a bit confusing.
8769 For C++, for example, it is the demangled name.
8770 For Go, for example, it's the mangled name.
8771
8772 For Ada, return the DIE's linkage name rather than the fully qualified
8773 name. PHYSNAME is ignored..
8774
8775 The result is allocated on the objfile_obstack and canonicalized. */
8776
8777 static const char *
8778 dwarf2_compute_name (const char *name,
8779 struct die_info *die, struct dwarf2_cu *cu,
8780 int physname)
8781 {
8782 struct objfile *objfile = cu->objfile;
8783
8784 if (name == NULL)
8785 name = dwarf2_name (die, cu);
8786
8787 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
8788 but otherwise compute it by typename_concat inside GDB.
8789 FIXME: Actually this is not really true, or at least not always true.
8790 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
8791 Fortran names because there is no mangling standard. So new_symbol_full
8792 will set the demangled name to the result of dwarf2_full_name, and it is
8793 the demangled name that GDB uses if it exists. */
8794 if (cu->language == language_ada
8795 || (cu->language == language_fortran && physname))
8796 {
8797 /* For Ada unit, we prefer the linkage name over the name, as
8798 the former contains the exported name, which the user expects
8799 to be able to reference. Ideally, we want the user to be able
8800 to reference this entity using either natural or linkage name,
8801 but we haven't started looking at this enhancement yet. */
8802 const char *linkage_name = dw2_linkage_name (die, cu);
8803
8804 if (linkage_name != NULL)
8805 return linkage_name;
8806 }
8807
8808 /* These are the only languages we know how to qualify names in. */
8809 if (name != NULL
8810 && (cu->language == language_cplus
8811 || cu->language == language_fortran || cu->language == language_d
8812 || cu->language == language_rust))
8813 {
8814 if (die_needs_namespace (die, cu))
8815 {
8816 long length;
8817 const char *prefix;
8818 const char *canonical_name = NULL;
8819
8820 string_file buf;
8821
8822 prefix = determine_prefix (die, cu);
8823 if (*prefix != '\0')
8824 {
8825 char *prefixed_name = typename_concat (NULL, prefix, name,
8826 physname, cu);
8827
8828 buf.puts (prefixed_name);
8829 xfree (prefixed_name);
8830 }
8831 else
8832 buf.puts (name);
8833
8834 /* Template parameters may be specified in the DIE's DW_AT_name, or
8835 as children with DW_TAG_template_type_param or
8836 DW_TAG_value_type_param. If the latter, add them to the name
8837 here. If the name already has template parameters, then
8838 skip this step; some versions of GCC emit both, and
8839 it is more efficient to use the pre-computed name.
8840
8841 Something to keep in mind about this process: it is very
8842 unlikely, or in some cases downright impossible, to produce
8843 something that will match the mangled name of a function.
8844 If the definition of the function has the same debug info,
8845 we should be able to match up with it anyway. But fallbacks
8846 using the minimal symbol, for instance to find a method
8847 implemented in a stripped copy of libstdc++, will not work.
8848 If we do not have debug info for the definition, we will have to
8849 match them up some other way.
8850
8851 When we do name matching there is a related problem with function
8852 templates; two instantiated function templates are allowed to
8853 differ only by their return types, which we do not add here. */
8854
8855 if (cu->language == language_cplus && strchr (name, '<') == NULL)
8856 {
8857 struct attribute *attr;
8858 struct die_info *child;
8859 int first = 1;
8860
8861 die->building_fullname = 1;
8862
8863 for (child = die->child; child != NULL; child = child->sibling)
8864 {
8865 struct type *type;
8866 LONGEST value;
8867 const gdb_byte *bytes;
8868 struct dwarf2_locexpr_baton *baton;
8869 struct value *v;
8870
8871 if (child->tag != DW_TAG_template_type_param
8872 && child->tag != DW_TAG_template_value_param)
8873 continue;
8874
8875 if (first)
8876 {
8877 buf.puts ("<");
8878 first = 0;
8879 }
8880 else
8881 buf.puts (", ");
8882
8883 attr = dwarf2_attr (child, DW_AT_type, cu);
8884 if (attr == NULL)
8885 {
8886 complaint (&symfile_complaints,
8887 _("template parameter missing DW_AT_type"));
8888 buf.puts ("UNKNOWN_TYPE");
8889 continue;
8890 }
8891 type = die_type (child, cu);
8892
8893 if (child->tag == DW_TAG_template_type_param)
8894 {
8895 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
8896 continue;
8897 }
8898
8899 attr = dwarf2_attr (child, DW_AT_const_value, cu);
8900 if (attr == NULL)
8901 {
8902 complaint (&symfile_complaints,
8903 _("template parameter missing "
8904 "DW_AT_const_value"));
8905 buf.puts ("UNKNOWN_VALUE");
8906 continue;
8907 }
8908
8909 dwarf2_const_value_attr (attr, type, name,
8910 &cu->comp_unit_obstack, cu,
8911 &value, &bytes, &baton);
8912
8913 if (TYPE_NOSIGN (type))
8914 /* GDB prints characters as NUMBER 'CHAR'. If that's
8915 changed, this can use value_print instead. */
8916 c_printchar (value, type, &buf);
8917 else
8918 {
8919 struct value_print_options opts;
8920
8921 if (baton != NULL)
8922 v = dwarf2_evaluate_loc_desc (type, NULL,
8923 baton->data,
8924 baton->size,
8925 baton->per_cu);
8926 else if (bytes != NULL)
8927 {
8928 v = allocate_value (type);
8929 memcpy (value_contents_writeable (v), bytes,
8930 TYPE_LENGTH (type));
8931 }
8932 else
8933 v = value_from_longest (type, value);
8934
8935 /* Specify decimal so that we do not depend on
8936 the radix. */
8937 get_formatted_print_options (&opts, 'd');
8938 opts.raw = 1;
8939 value_print (v, &buf, &opts);
8940 release_value (v);
8941 value_free (v);
8942 }
8943 }
8944
8945 die->building_fullname = 0;
8946
8947 if (!first)
8948 {
8949 /* Close the argument list, with a space if necessary
8950 (nested templates). */
8951 if (!buf.empty () && buf.string ().back () == '>')
8952 buf.puts (" >");
8953 else
8954 buf.puts (">");
8955 }
8956 }
8957
8958 /* For C++ methods, append formal parameter type
8959 information, if PHYSNAME. */
8960
8961 if (physname && die->tag == DW_TAG_subprogram
8962 && cu->language == language_cplus)
8963 {
8964 struct type *type = read_type_die (die, cu);
8965
8966 c_type_print_args (type, &buf, 1, cu->language,
8967 &type_print_raw_options);
8968
8969 if (cu->language == language_cplus)
8970 {
8971 /* Assume that an artificial first parameter is
8972 "this", but do not crash if it is not. RealView
8973 marks unnamed (and thus unused) parameters as
8974 artificial; there is no way to differentiate
8975 the two cases. */
8976 if (TYPE_NFIELDS (type) > 0
8977 && TYPE_FIELD_ARTIFICIAL (type, 0)
8978 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
8979 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
8980 0))))
8981 buf.puts (" const");
8982 }
8983 }
8984
8985 const std::string &intermediate_name = buf.string ();
8986
8987 if (cu->language == language_cplus)
8988 canonical_name
8989 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
8990 &objfile->per_bfd->storage_obstack);
8991
8992 /* If we only computed INTERMEDIATE_NAME, or if
8993 INTERMEDIATE_NAME is already canonical, then we need to
8994 copy it to the appropriate obstack. */
8995 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
8996 name = ((const char *)
8997 obstack_copy0 (&objfile->per_bfd->storage_obstack,
8998 intermediate_name.c_str (),
8999 intermediate_name.length ()));
9000 else
9001 name = canonical_name;
9002 }
9003 }
9004
9005 return name;
9006 }
9007
9008 /* Return the fully qualified name of DIE, based on its DW_AT_name.
9009 If scope qualifiers are appropriate they will be added. The result
9010 will be allocated on the storage_obstack, or NULL if the DIE does
9011 not have a name. NAME may either be from a previous call to
9012 dwarf2_name or NULL.
9013
9014 The output string will be canonicalized (if C++). */
9015
9016 static const char *
9017 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
9018 {
9019 return dwarf2_compute_name (name, die, cu, 0);
9020 }
9021
9022 /* Construct a physname for the given DIE in CU. NAME may either be
9023 from a previous call to dwarf2_name or NULL. The result will be
9024 allocated on the objfile_objstack or NULL if the DIE does not have a
9025 name.
9026
9027 The output string will be canonicalized (if C++). */
9028
9029 static const char *
9030 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
9031 {
9032 struct objfile *objfile = cu->objfile;
9033 const char *retval, *mangled = NULL, *canon = NULL;
9034 struct cleanup *back_to;
9035 int need_copy = 1;
9036
9037 /* In this case dwarf2_compute_name is just a shortcut not building anything
9038 on its own. */
9039 if (!die_needs_namespace (die, cu))
9040 return dwarf2_compute_name (name, die, cu, 1);
9041
9042 back_to = make_cleanup (null_cleanup, NULL);
9043
9044 mangled = dw2_linkage_name (die, cu);
9045
9046 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
9047 See https://github.com/rust-lang/rust/issues/32925. */
9048 if (cu->language == language_rust && mangled != NULL
9049 && strchr (mangled, '{') != NULL)
9050 mangled = NULL;
9051
9052 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
9053 has computed. */
9054 if (mangled != NULL)
9055 {
9056 char *demangled;
9057
9058 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
9059 type. It is easier for GDB users to search for such functions as
9060 `name(params)' than `long name(params)'. In such case the minimal
9061 symbol names do not match the full symbol names but for template
9062 functions there is never a need to look up their definition from their
9063 declaration so the only disadvantage remains the minimal symbol
9064 variant `long name(params)' does not have the proper inferior type.
9065 */
9066
9067 if (cu->language == language_go)
9068 {
9069 /* This is a lie, but we already lie to the caller new_symbol_full.
9070 new_symbol_full assumes we return the mangled name.
9071 This just undoes that lie until things are cleaned up. */
9072 demangled = NULL;
9073 }
9074 else
9075 {
9076 demangled = gdb_demangle (mangled,
9077 (DMGL_PARAMS | DMGL_ANSI | DMGL_RET_DROP));
9078 }
9079 if (demangled)
9080 {
9081 make_cleanup (xfree, demangled);
9082 canon = demangled;
9083 }
9084 else
9085 {
9086 canon = mangled;
9087 need_copy = 0;
9088 }
9089 }
9090
9091 if (canon == NULL || check_physname)
9092 {
9093 const char *physname = dwarf2_compute_name (name, die, cu, 1);
9094
9095 if (canon != NULL && strcmp (physname, canon) != 0)
9096 {
9097 /* It may not mean a bug in GDB. The compiler could also
9098 compute DW_AT_linkage_name incorrectly. But in such case
9099 GDB would need to be bug-to-bug compatible. */
9100
9101 complaint (&symfile_complaints,
9102 _("Computed physname <%s> does not match demangled <%s> "
9103 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
9104 physname, canon, mangled, to_underlying (die->sect_off),
9105 objfile_name (objfile));
9106
9107 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
9108 is available here - over computed PHYSNAME. It is safer
9109 against both buggy GDB and buggy compilers. */
9110
9111 retval = canon;
9112 }
9113 else
9114 {
9115 retval = physname;
9116 need_copy = 0;
9117 }
9118 }
9119 else
9120 retval = canon;
9121
9122 if (need_copy)
9123 retval = ((const char *)
9124 obstack_copy0 (&objfile->per_bfd->storage_obstack,
9125 retval, strlen (retval)));
9126
9127 do_cleanups (back_to);
9128 return retval;
9129 }
9130
9131 /* Inspect DIE in CU for a namespace alias. If one exists, record
9132 a new symbol for it.
9133
9134 Returns 1 if a namespace alias was recorded, 0 otherwise. */
9135
9136 static int
9137 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
9138 {
9139 struct attribute *attr;
9140
9141 /* If the die does not have a name, this is not a namespace
9142 alias. */
9143 attr = dwarf2_attr (die, DW_AT_name, cu);
9144 if (attr != NULL)
9145 {
9146 int num;
9147 struct die_info *d = die;
9148 struct dwarf2_cu *imported_cu = cu;
9149
9150 /* If the compiler has nested DW_AT_imported_declaration DIEs,
9151 keep inspecting DIEs until we hit the underlying import. */
9152 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
9153 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
9154 {
9155 attr = dwarf2_attr (d, DW_AT_import, cu);
9156 if (attr == NULL)
9157 break;
9158
9159 d = follow_die_ref (d, attr, &imported_cu);
9160 if (d->tag != DW_TAG_imported_declaration)
9161 break;
9162 }
9163
9164 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
9165 {
9166 complaint (&symfile_complaints,
9167 _("DIE at 0x%x has too many recursively imported "
9168 "declarations"), to_underlying (d->sect_off));
9169 return 0;
9170 }
9171
9172 if (attr != NULL)
9173 {
9174 struct type *type;
9175 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
9176
9177 type = get_die_type_at_offset (sect_off, cu->per_cu);
9178 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
9179 {
9180 /* This declaration is a global namespace alias. Add
9181 a symbol for it whose type is the aliased namespace. */
9182 new_symbol (die, type, cu);
9183 return 1;
9184 }
9185 }
9186 }
9187
9188 return 0;
9189 }
9190
9191 /* Return the using directives repository (global or local?) to use in the
9192 current context for LANGUAGE.
9193
9194 For Ada, imported declarations can materialize renamings, which *may* be
9195 global. However it is impossible (for now?) in DWARF to distinguish
9196 "external" imported declarations and "static" ones. As all imported
9197 declarations seem to be static in all other languages, make them all CU-wide
9198 global only in Ada. */
9199
9200 static struct using_direct **
9201 using_directives (enum language language)
9202 {
9203 if (language == language_ada && context_stack_depth == 0)
9204 return &global_using_directives;
9205 else
9206 return &local_using_directives;
9207 }
9208
9209 /* Read the import statement specified by the given die and record it. */
9210
9211 static void
9212 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
9213 {
9214 struct objfile *objfile = cu->objfile;
9215 struct attribute *import_attr;
9216 struct die_info *imported_die, *child_die;
9217 struct dwarf2_cu *imported_cu;
9218 const char *imported_name;
9219 const char *imported_name_prefix;
9220 const char *canonical_name;
9221 const char *import_alias;
9222 const char *imported_declaration = NULL;
9223 const char *import_prefix;
9224 std::vector<const char *> excludes;
9225
9226 import_attr = dwarf2_attr (die, DW_AT_import, cu);
9227 if (import_attr == NULL)
9228 {
9229 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
9230 dwarf_tag_name (die->tag));
9231 return;
9232 }
9233
9234 imported_cu = cu;
9235 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
9236 imported_name = dwarf2_name (imported_die, imported_cu);
9237 if (imported_name == NULL)
9238 {
9239 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
9240
9241 The import in the following code:
9242 namespace A
9243 {
9244 typedef int B;
9245 }
9246
9247 int main ()
9248 {
9249 using A::B;
9250 B b;
9251 return b;
9252 }
9253
9254 ...
9255 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
9256 <52> DW_AT_decl_file : 1
9257 <53> DW_AT_decl_line : 6
9258 <54> DW_AT_import : <0x75>
9259 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
9260 <59> DW_AT_name : B
9261 <5b> DW_AT_decl_file : 1
9262 <5c> DW_AT_decl_line : 2
9263 <5d> DW_AT_type : <0x6e>
9264 ...
9265 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
9266 <76> DW_AT_byte_size : 4
9267 <77> DW_AT_encoding : 5 (signed)
9268
9269 imports the wrong die ( 0x75 instead of 0x58 ).
9270 This case will be ignored until the gcc bug is fixed. */
9271 return;
9272 }
9273
9274 /* Figure out the local name after import. */
9275 import_alias = dwarf2_name (die, cu);
9276
9277 /* Figure out where the statement is being imported to. */
9278 import_prefix = determine_prefix (die, cu);
9279
9280 /* Figure out what the scope of the imported die is and prepend it
9281 to the name of the imported die. */
9282 imported_name_prefix = determine_prefix (imported_die, imported_cu);
9283
9284 if (imported_die->tag != DW_TAG_namespace
9285 && imported_die->tag != DW_TAG_module)
9286 {
9287 imported_declaration = imported_name;
9288 canonical_name = imported_name_prefix;
9289 }
9290 else if (strlen (imported_name_prefix) > 0)
9291 canonical_name = obconcat (&objfile->objfile_obstack,
9292 imported_name_prefix,
9293 (cu->language == language_d ? "." : "::"),
9294 imported_name, (char *) NULL);
9295 else
9296 canonical_name = imported_name;
9297
9298 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
9299 for (child_die = die->child; child_die && child_die->tag;
9300 child_die = sibling_die (child_die))
9301 {
9302 /* DWARF-4: A Fortran use statement with a “rename list” may be
9303 represented by an imported module entry with an import attribute
9304 referring to the module and owned entries corresponding to those
9305 entities that are renamed as part of being imported. */
9306
9307 if (child_die->tag != DW_TAG_imported_declaration)
9308 {
9309 complaint (&symfile_complaints,
9310 _("child DW_TAG_imported_declaration expected "
9311 "- DIE at 0x%x [in module %s]"),
9312 to_underlying (child_die->sect_off), objfile_name (objfile));
9313 continue;
9314 }
9315
9316 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
9317 if (import_attr == NULL)
9318 {
9319 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
9320 dwarf_tag_name (child_die->tag));
9321 continue;
9322 }
9323
9324 imported_cu = cu;
9325 imported_die = follow_die_ref_or_sig (child_die, import_attr,
9326 &imported_cu);
9327 imported_name = dwarf2_name (imported_die, imported_cu);
9328 if (imported_name == NULL)
9329 {
9330 complaint (&symfile_complaints,
9331 _("child DW_TAG_imported_declaration has unknown "
9332 "imported name - DIE at 0x%x [in module %s]"),
9333 to_underlying (child_die->sect_off), objfile_name (objfile));
9334 continue;
9335 }
9336
9337 excludes.push_back (imported_name);
9338
9339 process_die (child_die, cu);
9340 }
9341
9342 add_using_directive (using_directives (cu->language),
9343 import_prefix,
9344 canonical_name,
9345 import_alias,
9346 imported_declaration,
9347 excludes,
9348 0,
9349 &objfile->objfile_obstack);
9350 }
9351
9352 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
9353 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
9354 this, it was first present in GCC release 4.3.0. */
9355
9356 static int
9357 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
9358 {
9359 if (!cu->checked_producer)
9360 check_producer (cu);
9361
9362 return cu->producer_is_gcc_lt_4_3;
9363 }
9364
9365 static file_and_directory
9366 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9367 {
9368 file_and_directory res;
9369
9370 /* Find the filename. Do not use dwarf2_name here, since the filename
9371 is not a source language identifier. */
9372 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
9373 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9374
9375 if (res.comp_dir == NULL
9376 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
9377 && IS_ABSOLUTE_PATH (res.name))
9378 {
9379 res.comp_dir_storage = ldirname (res.name);
9380 if (!res.comp_dir_storage.empty ())
9381 res.comp_dir = res.comp_dir_storage.c_str ();
9382 }
9383 if (res.comp_dir != NULL)
9384 {
9385 /* Irix 6.2 native cc prepends <machine>.: to the compilation
9386 directory, get rid of it. */
9387 const char *cp = strchr (res.comp_dir, ':');
9388
9389 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
9390 res.comp_dir = cp + 1;
9391 }
9392
9393 if (res.name == NULL)
9394 res.name = "<unknown>";
9395
9396 return res;
9397 }
9398
9399 /* Handle DW_AT_stmt_list for a compilation unit.
9400 DIE is the DW_TAG_compile_unit die for CU.
9401 COMP_DIR is the compilation directory. LOWPC is passed to
9402 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
9403
9404 static void
9405 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
9406 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
9407 {
9408 struct objfile *objfile = dwarf2_per_objfile->objfile;
9409 struct attribute *attr;
9410 struct line_header line_header_local;
9411 hashval_t line_header_local_hash;
9412 unsigned u;
9413 void **slot;
9414 int decode_mapping;
9415
9416 gdb_assert (! cu->per_cu->is_debug_types);
9417
9418 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
9419 if (attr == NULL)
9420 return;
9421
9422 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
9423
9424 /* The line header hash table is only created if needed (it exists to
9425 prevent redundant reading of the line table for partial_units).
9426 If we're given a partial_unit, we'll need it. If we're given a
9427 compile_unit, then use the line header hash table if it's already
9428 created, but don't create one just yet. */
9429
9430 if (dwarf2_per_objfile->line_header_hash == NULL
9431 && die->tag == DW_TAG_partial_unit)
9432 {
9433 dwarf2_per_objfile->line_header_hash
9434 = htab_create_alloc_ex (127, line_header_hash_voidp,
9435 line_header_eq_voidp,
9436 free_line_header_voidp,
9437 &objfile->objfile_obstack,
9438 hashtab_obstack_allocate,
9439 dummy_obstack_deallocate);
9440 }
9441
9442 line_header_local.sect_off = line_offset;
9443 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
9444 line_header_local_hash = line_header_hash (&line_header_local);
9445 if (dwarf2_per_objfile->line_header_hash != NULL)
9446 {
9447 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
9448 &line_header_local,
9449 line_header_local_hash, NO_INSERT);
9450
9451 /* For DW_TAG_compile_unit we need info like symtab::linetable which
9452 is not present in *SLOT (since if there is something in *SLOT then
9453 it will be for a partial_unit). */
9454 if (die->tag == DW_TAG_partial_unit && slot != NULL)
9455 {
9456 gdb_assert (*slot != NULL);
9457 cu->line_header = (struct line_header *) *slot;
9458 return;
9459 }
9460 }
9461
9462 /* dwarf_decode_line_header does not yet provide sufficient information.
9463 We always have to call also dwarf_decode_lines for it. */
9464 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
9465 if (lh == NULL)
9466 return;
9467
9468 cu->line_header = lh.release ();
9469 cu->line_header_die_owner = die;
9470
9471 if (dwarf2_per_objfile->line_header_hash == NULL)
9472 slot = NULL;
9473 else
9474 {
9475 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
9476 &line_header_local,
9477 line_header_local_hash, INSERT);
9478 gdb_assert (slot != NULL);
9479 }
9480 if (slot != NULL && *slot == NULL)
9481 {
9482 /* This newly decoded line number information unit will be owned
9483 by line_header_hash hash table. */
9484 *slot = cu->line_header;
9485 cu->line_header_die_owner = NULL;
9486 }
9487 else
9488 {
9489 /* We cannot free any current entry in (*slot) as that struct line_header
9490 may be already used by multiple CUs. Create only temporary decoded
9491 line_header for this CU - it may happen at most once for each line
9492 number information unit. And if we're not using line_header_hash
9493 then this is what we want as well. */
9494 gdb_assert (die->tag != DW_TAG_partial_unit);
9495 }
9496 decode_mapping = (die->tag != DW_TAG_partial_unit);
9497 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
9498 decode_mapping);
9499
9500 }
9501
9502 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
9503
9504 static void
9505 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
9506 {
9507 struct objfile *objfile = dwarf2_per_objfile->objfile;
9508 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9509 CORE_ADDR lowpc = ((CORE_ADDR) -1);
9510 CORE_ADDR highpc = ((CORE_ADDR) 0);
9511 struct attribute *attr;
9512 struct die_info *child_die;
9513 CORE_ADDR baseaddr;
9514
9515 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9516
9517 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
9518
9519 /* If we didn't find a lowpc, set it to highpc to avoid complaints
9520 from finish_block. */
9521 if (lowpc == ((CORE_ADDR) -1))
9522 lowpc = highpc;
9523 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
9524
9525 file_and_directory fnd = find_file_and_directory (die, cu);
9526
9527 prepare_one_comp_unit (cu, die, cu->language);
9528
9529 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
9530 standardised yet. As a workaround for the language detection we fall
9531 back to the DW_AT_producer string. */
9532 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
9533 cu->language = language_opencl;
9534
9535 /* Similar hack for Go. */
9536 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
9537 set_cu_language (DW_LANG_Go, cu);
9538
9539 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
9540
9541 /* Decode line number information if present. We do this before
9542 processing child DIEs, so that the line header table is available
9543 for DW_AT_decl_file. */
9544 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
9545
9546 /* Process all dies in compilation unit. */
9547 if (die->child != NULL)
9548 {
9549 child_die = die->child;
9550 while (child_die && child_die->tag)
9551 {
9552 process_die (child_die, cu);
9553 child_die = sibling_die (child_die);
9554 }
9555 }
9556
9557 /* Decode macro information, if present. Dwarf 2 macro information
9558 refers to information in the line number info statement program
9559 header, so we can only read it if we've read the header
9560 successfully. */
9561 attr = dwarf2_attr (die, DW_AT_macros, cu);
9562 if (attr == NULL)
9563 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
9564 if (attr && cu->line_header)
9565 {
9566 if (dwarf2_attr (die, DW_AT_macro_info, cu))
9567 complaint (&symfile_complaints,
9568 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
9569
9570 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
9571 }
9572 else
9573 {
9574 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
9575 if (attr && cu->line_header)
9576 {
9577 unsigned int macro_offset = DW_UNSND (attr);
9578
9579 dwarf_decode_macros (cu, macro_offset, 0);
9580 }
9581 }
9582 }
9583
9584 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
9585 Create the set of symtabs used by this TU, or if this TU is sharing
9586 symtabs with another TU and the symtabs have already been created
9587 then restore those symtabs in the line header.
9588 We don't need the pc/line-number mapping for type units. */
9589
9590 static void
9591 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
9592 {
9593 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
9594 struct type_unit_group *tu_group;
9595 int first_time;
9596 struct attribute *attr;
9597 unsigned int i;
9598 struct signatured_type *sig_type;
9599
9600 gdb_assert (per_cu->is_debug_types);
9601 sig_type = (struct signatured_type *) per_cu;
9602
9603 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
9604
9605 /* If we're using .gdb_index (includes -readnow) then
9606 per_cu->type_unit_group may not have been set up yet. */
9607 if (sig_type->type_unit_group == NULL)
9608 sig_type->type_unit_group = get_type_unit_group (cu, attr);
9609 tu_group = sig_type->type_unit_group;
9610
9611 /* If we've already processed this stmt_list there's no real need to
9612 do it again, we could fake it and just recreate the part we need
9613 (file name,index -> symtab mapping). If data shows this optimization
9614 is useful we can do it then. */
9615 first_time = tu_group->compunit_symtab == NULL;
9616
9617 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
9618 debug info. */
9619 line_header_up lh;
9620 if (attr != NULL)
9621 {
9622 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
9623 lh = dwarf_decode_line_header (line_offset, cu);
9624 }
9625 if (lh == NULL)
9626 {
9627 if (first_time)
9628 dwarf2_start_symtab (cu, "", NULL, 0);
9629 else
9630 {
9631 gdb_assert (tu_group->symtabs == NULL);
9632 restart_symtab (tu_group->compunit_symtab, "", 0);
9633 }
9634 return;
9635 }
9636
9637 cu->line_header = lh.release ();
9638 cu->line_header_die_owner = die;
9639
9640 if (first_time)
9641 {
9642 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
9643
9644 /* Note: We don't assign tu_group->compunit_symtab yet because we're
9645 still initializing it, and our caller (a few levels up)
9646 process_full_type_unit still needs to know if this is the first
9647 time. */
9648
9649 tu_group->num_symtabs = cu->line_header->file_names.size ();
9650 tu_group->symtabs = XNEWVEC (struct symtab *,
9651 cu->line_header->file_names.size ());
9652
9653 for (i = 0; i < cu->line_header->file_names.size (); ++i)
9654 {
9655 file_entry &fe = cu->line_header->file_names[i];
9656
9657 dwarf2_start_subfile (fe.name, fe.include_dir (cu->line_header));
9658
9659 if (current_subfile->symtab == NULL)
9660 {
9661 /* NOTE: start_subfile will recognize when it's been
9662 passed a file it has already seen. So we can't
9663 assume there's a simple mapping from
9664 cu->line_header->file_names to subfiles, plus
9665 cu->line_header->file_names may contain dups. */
9666 current_subfile->symtab
9667 = allocate_symtab (cust, current_subfile->name);
9668 }
9669
9670 fe.symtab = current_subfile->symtab;
9671 tu_group->symtabs[i] = fe.symtab;
9672 }
9673 }
9674 else
9675 {
9676 restart_symtab (tu_group->compunit_symtab, "", 0);
9677
9678 for (i = 0; i < cu->line_header->file_names.size (); ++i)
9679 {
9680 file_entry &fe = cu->line_header->file_names[i];
9681
9682 fe.symtab = tu_group->symtabs[i];
9683 }
9684 }
9685
9686 /* The main symtab is allocated last. Type units don't have DW_AT_name
9687 so they don't have a "real" (so to speak) symtab anyway.
9688 There is later code that will assign the main symtab to all symbols
9689 that don't have one. We need to handle the case of a symbol with a
9690 missing symtab (DW_AT_decl_file) anyway. */
9691 }
9692
9693 /* Process DW_TAG_type_unit.
9694 For TUs we want to skip the first top level sibling if it's not the
9695 actual type being defined by this TU. In this case the first top
9696 level sibling is there to provide context only. */
9697
9698 static void
9699 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
9700 {
9701 struct die_info *child_die;
9702
9703 prepare_one_comp_unit (cu, die, language_minimal);
9704
9705 /* Initialize (or reinitialize) the machinery for building symtabs.
9706 We do this before processing child DIEs, so that the line header table
9707 is available for DW_AT_decl_file. */
9708 setup_type_unit_groups (die, cu);
9709
9710 if (die->child != NULL)
9711 {
9712 child_die = die->child;
9713 while (child_die && child_die->tag)
9714 {
9715 process_die (child_die, cu);
9716 child_die = sibling_die (child_die);
9717 }
9718 }
9719 }
9720 \f
9721 /* DWO/DWP files.
9722
9723 http://gcc.gnu.org/wiki/DebugFission
9724 http://gcc.gnu.org/wiki/DebugFissionDWP
9725
9726 To simplify handling of both DWO files ("object" files with the DWARF info)
9727 and DWP files (a file with the DWOs packaged up into one file), we treat
9728 DWP files as having a collection of virtual DWO files. */
9729
9730 static hashval_t
9731 hash_dwo_file (const void *item)
9732 {
9733 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
9734 hashval_t hash;
9735
9736 hash = htab_hash_string (dwo_file->dwo_name);
9737 if (dwo_file->comp_dir != NULL)
9738 hash += htab_hash_string (dwo_file->comp_dir);
9739 return hash;
9740 }
9741
9742 static int
9743 eq_dwo_file (const void *item_lhs, const void *item_rhs)
9744 {
9745 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
9746 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
9747
9748 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
9749 return 0;
9750 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
9751 return lhs->comp_dir == rhs->comp_dir;
9752 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
9753 }
9754
9755 /* Allocate a hash table for DWO files. */
9756
9757 static htab_t
9758 allocate_dwo_file_hash_table (void)
9759 {
9760 struct objfile *objfile = dwarf2_per_objfile->objfile;
9761
9762 return htab_create_alloc_ex (41,
9763 hash_dwo_file,
9764 eq_dwo_file,
9765 NULL,
9766 &objfile->objfile_obstack,
9767 hashtab_obstack_allocate,
9768 dummy_obstack_deallocate);
9769 }
9770
9771 /* Lookup DWO file DWO_NAME. */
9772
9773 static void **
9774 lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
9775 {
9776 struct dwo_file find_entry;
9777 void **slot;
9778
9779 if (dwarf2_per_objfile->dwo_files == NULL)
9780 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
9781
9782 memset (&find_entry, 0, sizeof (find_entry));
9783 find_entry.dwo_name = dwo_name;
9784 find_entry.comp_dir = comp_dir;
9785 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
9786
9787 return slot;
9788 }
9789
9790 static hashval_t
9791 hash_dwo_unit (const void *item)
9792 {
9793 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
9794
9795 /* This drops the top 32 bits of the id, but is ok for a hash. */
9796 return dwo_unit->signature;
9797 }
9798
9799 static int
9800 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
9801 {
9802 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
9803 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
9804
9805 /* The signature is assumed to be unique within the DWO file.
9806 So while object file CU dwo_id's always have the value zero,
9807 that's OK, assuming each object file DWO file has only one CU,
9808 and that's the rule for now. */
9809 return lhs->signature == rhs->signature;
9810 }
9811
9812 /* Allocate a hash table for DWO CUs,TUs.
9813 There is one of these tables for each of CUs,TUs for each DWO file. */
9814
9815 static htab_t
9816 allocate_dwo_unit_table (struct objfile *objfile)
9817 {
9818 /* Start out with a pretty small number.
9819 Generally DWO files contain only one CU and maybe some TUs. */
9820 return htab_create_alloc_ex (3,
9821 hash_dwo_unit,
9822 eq_dwo_unit,
9823 NULL,
9824 &objfile->objfile_obstack,
9825 hashtab_obstack_allocate,
9826 dummy_obstack_deallocate);
9827 }
9828
9829 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
9830
9831 struct create_dwo_cu_data
9832 {
9833 struct dwo_file *dwo_file;
9834 struct dwo_unit dwo_unit;
9835 };
9836
9837 /* die_reader_func for create_dwo_cu. */
9838
9839 static void
9840 create_dwo_cu_reader (const struct die_reader_specs *reader,
9841 const gdb_byte *info_ptr,
9842 struct die_info *comp_unit_die,
9843 int has_children,
9844 void *datap)
9845 {
9846 struct dwarf2_cu *cu = reader->cu;
9847 sect_offset sect_off = cu->per_cu->sect_off;
9848 struct dwarf2_section_info *section = cu->per_cu->section;
9849 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
9850 struct dwo_file *dwo_file = data->dwo_file;
9851 struct dwo_unit *dwo_unit = &data->dwo_unit;
9852 struct attribute *attr;
9853
9854 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
9855 if (attr == NULL)
9856 {
9857 complaint (&symfile_complaints,
9858 _("Dwarf Error: debug entry at offset 0x%x is missing"
9859 " its dwo_id [in module %s]"),
9860 to_underlying (sect_off), dwo_file->dwo_name);
9861 return;
9862 }
9863
9864 dwo_unit->dwo_file = dwo_file;
9865 dwo_unit->signature = DW_UNSND (attr);
9866 dwo_unit->section = section;
9867 dwo_unit->sect_off = sect_off;
9868 dwo_unit->length = cu->per_cu->length;
9869
9870 if (dwarf_read_debug)
9871 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
9872 to_underlying (sect_off),
9873 hex_string (dwo_unit->signature));
9874 }
9875
9876 /* Create the dwo_units for the CUs in a DWO_FILE.
9877 Note: This function processes DWO files only, not DWP files. */
9878
9879 static void
9880 create_cus_hash_table (struct dwo_file &dwo_file, dwarf2_section_info &section,
9881 htab_t &cus_htab)
9882 {
9883 struct objfile *objfile = dwarf2_per_objfile->objfile;
9884 const struct dwarf2_section_info *abbrev_section = &dwo_file.sections.abbrev;
9885 const gdb_byte *info_ptr, *end_ptr;
9886
9887 dwarf2_read_section (objfile, &section);
9888 info_ptr = section.buffer;
9889
9890 if (info_ptr == NULL)
9891 return;
9892
9893 if (dwarf_read_debug)
9894 {
9895 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
9896 get_section_name (&section),
9897 get_section_file_name (&section));
9898 }
9899
9900 end_ptr = info_ptr + section.size;
9901 while (info_ptr < end_ptr)
9902 {
9903 struct dwarf2_per_cu_data per_cu;
9904 struct create_dwo_cu_data create_dwo_cu_data;
9905 struct dwo_unit *dwo_unit;
9906 void **slot;
9907 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
9908
9909 memset (&create_dwo_cu_data.dwo_unit, 0,
9910 sizeof (create_dwo_cu_data.dwo_unit));
9911 memset (&per_cu, 0, sizeof (per_cu));
9912 per_cu.objfile = objfile;
9913 per_cu.is_debug_types = 0;
9914 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
9915 per_cu.section = &section;
9916 create_dwo_cu_data.dwo_file = &dwo_file;
9917
9918 init_cutu_and_read_dies_no_follow (
9919 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
9920 info_ptr += per_cu.length;
9921
9922 // If the unit could not be parsed, skip it.
9923 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
9924 continue;
9925
9926 if (cus_htab == NULL)
9927 cus_htab = allocate_dwo_unit_table (objfile);
9928
9929 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
9930 *dwo_unit = create_dwo_cu_data.dwo_unit;
9931 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
9932 gdb_assert (slot != NULL);
9933 if (*slot != NULL)
9934 {
9935 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
9936 sect_offset dup_sect_off = dup_cu->sect_off;
9937
9938 complaint (&symfile_complaints,
9939 _("debug cu entry at offset 0x%x is duplicate to"
9940 " the entry at offset 0x%x, signature %s"),
9941 to_underlying (sect_off), to_underlying (dup_sect_off),
9942 hex_string (dwo_unit->signature));
9943 }
9944 *slot = (void *)dwo_unit;
9945 }
9946 }
9947
9948 /* DWP file .debug_{cu,tu}_index section format:
9949 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
9950
9951 DWP Version 1:
9952
9953 Both index sections have the same format, and serve to map a 64-bit
9954 signature to a set of section numbers. Each section begins with a header,
9955 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
9956 indexes, and a pool of 32-bit section numbers. The index sections will be
9957 aligned at 8-byte boundaries in the file.
9958
9959 The index section header consists of:
9960
9961 V, 32 bit version number
9962 -, 32 bits unused
9963 N, 32 bit number of compilation units or type units in the index
9964 M, 32 bit number of slots in the hash table
9965
9966 Numbers are recorded using the byte order of the application binary.
9967
9968 The hash table begins at offset 16 in the section, and consists of an array
9969 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
9970 order of the application binary). Unused slots in the hash table are 0.
9971 (We rely on the extreme unlikeliness of a signature being exactly 0.)
9972
9973 The parallel table begins immediately after the hash table
9974 (at offset 16 + 8 * M from the beginning of the section), and consists of an
9975 array of 32-bit indexes (using the byte order of the application binary),
9976 corresponding 1-1 with slots in the hash table. Each entry in the parallel
9977 table contains a 32-bit index into the pool of section numbers. For unused
9978 hash table slots, the corresponding entry in the parallel table will be 0.
9979
9980 The pool of section numbers begins immediately following the hash table
9981 (at offset 16 + 12 * M from the beginning of the section). The pool of
9982 section numbers consists of an array of 32-bit words (using the byte order
9983 of the application binary). Each item in the array is indexed starting
9984 from 0. The hash table entry provides the index of the first section
9985 number in the set. Additional section numbers in the set follow, and the
9986 set is terminated by a 0 entry (section number 0 is not used in ELF).
9987
9988 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
9989 section must be the first entry in the set, and the .debug_abbrev.dwo must
9990 be the second entry. Other members of the set may follow in any order.
9991
9992 ---
9993
9994 DWP Version 2:
9995
9996 DWP Version 2 combines all the .debug_info, etc. sections into one,
9997 and the entries in the index tables are now offsets into these sections.
9998 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
9999 section.
10000
10001 Index Section Contents:
10002 Header
10003 Hash Table of Signatures dwp_hash_table.hash_table
10004 Parallel Table of Indices dwp_hash_table.unit_table
10005 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
10006 Table of Section Sizes dwp_hash_table.v2.sizes
10007
10008 The index section header consists of:
10009
10010 V, 32 bit version number
10011 L, 32 bit number of columns in the table of section offsets
10012 N, 32 bit number of compilation units or type units in the index
10013 M, 32 bit number of slots in the hash table
10014
10015 Numbers are recorded using the byte order of the application binary.
10016
10017 The hash table has the same format as version 1.
10018 The parallel table of indices has the same format as version 1,
10019 except that the entries are origin-1 indices into the table of sections
10020 offsets and the table of section sizes.
10021
10022 The table of offsets begins immediately following the parallel table
10023 (at offset 16 + 12 * M from the beginning of the section). The table is
10024 a two-dimensional array of 32-bit words (using the byte order of the
10025 application binary), with L columns and N+1 rows, in row-major order.
10026 Each row in the array is indexed starting from 0. The first row provides
10027 a key to the remaining rows: each column in this row provides an identifier
10028 for a debug section, and the offsets in the same column of subsequent rows
10029 refer to that section. The section identifiers are:
10030
10031 DW_SECT_INFO 1 .debug_info.dwo
10032 DW_SECT_TYPES 2 .debug_types.dwo
10033 DW_SECT_ABBREV 3 .debug_abbrev.dwo
10034 DW_SECT_LINE 4 .debug_line.dwo
10035 DW_SECT_LOC 5 .debug_loc.dwo
10036 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
10037 DW_SECT_MACINFO 7 .debug_macinfo.dwo
10038 DW_SECT_MACRO 8 .debug_macro.dwo
10039
10040 The offsets provided by the CU and TU index sections are the base offsets
10041 for the contributions made by each CU or TU to the corresponding section
10042 in the package file. Each CU and TU header contains an abbrev_offset
10043 field, used to find the abbreviations table for that CU or TU within the
10044 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
10045 be interpreted as relative to the base offset given in the index section.
10046 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
10047 should be interpreted as relative to the base offset for .debug_line.dwo,
10048 and offsets into other debug sections obtained from DWARF attributes should
10049 also be interpreted as relative to the corresponding base offset.
10050
10051 The table of sizes begins immediately following the table of offsets.
10052 Like the table of offsets, it is a two-dimensional array of 32-bit words,
10053 with L columns and N rows, in row-major order. Each row in the array is
10054 indexed starting from 1 (row 0 is shared by the two tables).
10055
10056 ---
10057
10058 Hash table lookup is handled the same in version 1 and 2:
10059
10060 We assume that N and M will not exceed 2^32 - 1.
10061 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
10062
10063 Given a 64-bit compilation unit signature or a type signature S, an entry
10064 in the hash table is located as follows:
10065
10066 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
10067 the low-order k bits all set to 1.
10068
10069 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
10070
10071 3) If the hash table entry at index H matches the signature, use that
10072 entry. If the hash table entry at index H is unused (all zeroes),
10073 terminate the search: the signature is not present in the table.
10074
10075 4) Let H = (H + H') modulo M. Repeat at Step 3.
10076
10077 Because M > N and H' and M are relatively prime, the search is guaranteed
10078 to stop at an unused slot or find the match. */
10079
10080 /* Create a hash table to map DWO IDs to their CU/TU entry in
10081 .debug_{info,types}.dwo in DWP_FILE.
10082 Returns NULL if there isn't one.
10083 Note: This function processes DWP files only, not DWO files. */
10084
10085 static struct dwp_hash_table *
10086 create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
10087 {
10088 struct objfile *objfile = dwarf2_per_objfile->objfile;
10089 bfd *dbfd = dwp_file->dbfd;
10090 const gdb_byte *index_ptr, *index_end;
10091 struct dwarf2_section_info *index;
10092 uint32_t version, nr_columns, nr_units, nr_slots;
10093 struct dwp_hash_table *htab;
10094
10095 if (is_debug_types)
10096 index = &dwp_file->sections.tu_index;
10097 else
10098 index = &dwp_file->sections.cu_index;
10099
10100 if (dwarf2_section_empty_p (index))
10101 return NULL;
10102 dwarf2_read_section (objfile, index);
10103
10104 index_ptr = index->buffer;
10105 index_end = index_ptr + index->size;
10106
10107 version = read_4_bytes (dbfd, index_ptr);
10108 index_ptr += 4;
10109 if (version == 2)
10110 nr_columns = read_4_bytes (dbfd, index_ptr);
10111 else
10112 nr_columns = 0;
10113 index_ptr += 4;
10114 nr_units = read_4_bytes (dbfd, index_ptr);
10115 index_ptr += 4;
10116 nr_slots = read_4_bytes (dbfd, index_ptr);
10117 index_ptr += 4;
10118
10119 if (version != 1 && version != 2)
10120 {
10121 error (_("Dwarf Error: unsupported DWP file version (%s)"
10122 " [in module %s]"),
10123 pulongest (version), dwp_file->name);
10124 }
10125 if (nr_slots != (nr_slots & -nr_slots))
10126 {
10127 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
10128 " is not power of 2 [in module %s]"),
10129 pulongest (nr_slots), dwp_file->name);
10130 }
10131
10132 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
10133 htab->version = version;
10134 htab->nr_columns = nr_columns;
10135 htab->nr_units = nr_units;
10136 htab->nr_slots = nr_slots;
10137 htab->hash_table = index_ptr;
10138 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
10139
10140 /* Exit early if the table is empty. */
10141 if (nr_slots == 0 || nr_units == 0
10142 || (version == 2 && nr_columns == 0))
10143 {
10144 /* All must be zero. */
10145 if (nr_slots != 0 || nr_units != 0
10146 || (version == 2 && nr_columns != 0))
10147 {
10148 complaint (&symfile_complaints,
10149 _("Empty DWP but nr_slots,nr_units,nr_columns not"
10150 " all zero [in modules %s]"),
10151 dwp_file->name);
10152 }
10153 return htab;
10154 }
10155
10156 if (version == 1)
10157 {
10158 htab->section_pool.v1.indices =
10159 htab->unit_table + sizeof (uint32_t) * nr_slots;
10160 /* It's harder to decide whether the section is too small in v1.
10161 V1 is deprecated anyway so we punt. */
10162 }
10163 else
10164 {
10165 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
10166 int *ids = htab->section_pool.v2.section_ids;
10167 /* Reverse map for error checking. */
10168 int ids_seen[DW_SECT_MAX + 1];
10169 int i;
10170
10171 if (nr_columns < 2)
10172 {
10173 error (_("Dwarf Error: bad DWP hash table, too few columns"
10174 " in section table [in module %s]"),
10175 dwp_file->name);
10176 }
10177 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
10178 {
10179 error (_("Dwarf Error: bad DWP hash table, too many columns"
10180 " in section table [in module %s]"),
10181 dwp_file->name);
10182 }
10183 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
10184 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
10185 for (i = 0; i < nr_columns; ++i)
10186 {
10187 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
10188
10189 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
10190 {
10191 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
10192 " in section table [in module %s]"),
10193 id, dwp_file->name);
10194 }
10195 if (ids_seen[id] != -1)
10196 {
10197 error (_("Dwarf Error: bad DWP hash table, duplicate section"
10198 " id %d in section table [in module %s]"),
10199 id, dwp_file->name);
10200 }
10201 ids_seen[id] = i;
10202 ids[i] = id;
10203 }
10204 /* Must have exactly one info or types section. */
10205 if (((ids_seen[DW_SECT_INFO] != -1)
10206 + (ids_seen[DW_SECT_TYPES] != -1))
10207 != 1)
10208 {
10209 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
10210 " DWO info/types section [in module %s]"),
10211 dwp_file->name);
10212 }
10213 /* Must have an abbrev section. */
10214 if (ids_seen[DW_SECT_ABBREV] == -1)
10215 {
10216 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
10217 " section [in module %s]"),
10218 dwp_file->name);
10219 }
10220 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
10221 htab->section_pool.v2.sizes =
10222 htab->section_pool.v2.offsets + (sizeof (uint32_t)
10223 * nr_units * nr_columns);
10224 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
10225 * nr_units * nr_columns))
10226 > index_end)
10227 {
10228 error (_("Dwarf Error: DWP index section is corrupt (too small)"
10229 " [in module %s]"),
10230 dwp_file->name);
10231 }
10232 }
10233
10234 return htab;
10235 }
10236
10237 /* Update SECTIONS with the data from SECTP.
10238
10239 This function is like the other "locate" section routines that are
10240 passed to bfd_map_over_sections, but in this context the sections to
10241 read comes from the DWP V1 hash table, not the full ELF section table.
10242
10243 The result is non-zero for success, or zero if an error was found. */
10244
10245 static int
10246 locate_v1_virtual_dwo_sections (asection *sectp,
10247 struct virtual_v1_dwo_sections *sections)
10248 {
10249 const struct dwop_section_names *names = &dwop_section_names;
10250
10251 if (section_is_p (sectp->name, &names->abbrev_dwo))
10252 {
10253 /* There can be only one. */
10254 if (sections->abbrev.s.section != NULL)
10255 return 0;
10256 sections->abbrev.s.section = sectp;
10257 sections->abbrev.size = bfd_get_section_size (sectp);
10258 }
10259 else if (section_is_p (sectp->name, &names->info_dwo)
10260 || section_is_p (sectp->name, &names->types_dwo))
10261 {
10262 /* There can be only one. */
10263 if (sections->info_or_types.s.section != NULL)
10264 return 0;
10265 sections->info_or_types.s.section = sectp;
10266 sections->info_or_types.size = bfd_get_section_size (sectp);
10267 }
10268 else if (section_is_p (sectp->name, &names->line_dwo))
10269 {
10270 /* There can be only one. */
10271 if (sections->line.s.section != NULL)
10272 return 0;
10273 sections->line.s.section = sectp;
10274 sections->line.size = bfd_get_section_size (sectp);
10275 }
10276 else if (section_is_p (sectp->name, &names->loc_dwo))
10277 {
10278 /* There can be only one. */
10279 if (sections->loc.s.section != NULL)
10280 return 0;
10281 sections->loc.s.section = sectp;
10282 sections->loc.size = bfd_get_section_size (sectp);
10283 }
10284 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10285 {
10286 /* There can be only one. */
10287 if (sections->macinfo.s.section != NULL)
10288 return 0;
10289 sections->macinfo.s.section = sectp;
10290 sections->macinfo.size = bfd_get_section_size (sectp);
10291 }
10292 else if (section_is_p (sectp->name, &names->macro_dwo))
10293 {
10294 /* There can be only one. */
10295 if (sections->macro.s.section != NULL)
10296 return 0;
10297 sections->macro.s.section = sectp;
10298 sections->macro.size = bfd_get_section_size (sectp);
10299 }
10300 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10301 {
10302 /* There can be only one. */
10303 if (sections->str_offsets.s.section != NULL)
10304 return 0;
10305 sections->str_offsets.s.section = sectp;
10306 sections->str_offsets.size = bfd_get_section_size (sectp);
10307 }
10308 else
10309 {
10310 /* No other kind of section is valid. */
10311 return 0;
10312 }
10313
10314 return 1;
10315 }
10316
10317 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10318 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10319 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10320 This is for DWP version 1 files. */
10321
10322 static struct dwo_unit *
10323 create_dwo_unit_in_dwp_v1 (struct dwp_file *dwp_file,
10324 uint32_t unit_index,
10325 const char *comp_dir,
10326 ULONGEST signature, int is_debug_types)
10327 {
10328 struct objfile *objfile = dwarf2_per_objfile->objfile;
10329 const struct dwp_hash_table *dwp_htab =
10330 is_debug_types ? dwp_file->tus : dwp_file->cus;
10331 bfd *dbfd = dwp_file->dbfd;
10332 const char *kind = is_debug_types ? "TU" : "CU";
10333 struct dwo_file *dwo_file;
10334 struct dwo_unit *dwo_unit;
10335 struct virtual_v1_dwo_sections sections;
10336 void **dwo_file_slot;
10337 char *virtual_dwo_name;
10338 struct cleanup *cleanups;
10339 int i;
10340
10341 gdb_assert (dwp_file->version == 1);
10342
10343 if (dwarf_read_debug)
10344 {
10345 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
10346 kind,
10347 pulongest (unit_index), hex_string (signature),
10348 dwp_file->name);
10349 }
10350
10351 /* Fetch the sections of this DWO unit.
10352 Put a limit on the number of sections we look for so that bad data
10353 doesn't cause us to loop forever. */
10354
10355 #define MAX_NR_V1_DWO_SECTIONS \
10356 (1 /* .debug_info or .debug_types */ \
10357 + 1 /* .debug_abbrev */ \
10358 + 1 /* .debug_line */ \
10359 + 1 /* .debug_loc */ \
10360 + 1 /* .debug_str_offsets */ \
10361 + 1 /* .debug_macro or .debug_macinfo */ \
10362 + 1 /* trailing zero */)
10363
10364 memset (&sections, 0, sizeof (sections));
10365 cleanups = make_cleanup (null_cleanup, 0);
10366
10367 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
10368 {
10369 asection *sectp;
10370 uint32_t section_nr =
10371 read_4_bytes (dbfd,
10372 dwp_htab->section_pool.v1.indices
10373 + (unit_index + i) * sizeof (uint32_t));
10374
10375 if (section_nr == 0)
10376 break;
10377 if (section_nr >= dwp_file->num_sections)
10378 {
10379 error (_("Dwarf Error: bad DWP hash table, section number too large"
10380 " [in module %s]"),
10381 dwp_file->name);
10382 }
10383
10384 sectp = dwp_file->elf_sections[section_nr];
10385 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
10386 {
10387 error (_("Dwarf Error: bad DWP hash table, invalid section found"
10388 " [in module %s]"),
10389 dwp_file->name);
10390 }
10391 }
10392
10393 if (i < 2
10394 || dwarf2_section_empty_p (&sections.info_or_types)
10395 || dwarf2_section_empty_p (&sections.abbrev))
10396 {
10397 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
10398 " [in module %s]"),
10399 dwp_file->name);
10400 }
10401 if (i == MAX_NR_V1_DWO_SECTIONS)
10402 {
10403 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
10404 " [in module %s]"),
10405 dwp_file->name);
10406 }
10407
10408 /* It's easier for the rest of the code if we fake a struct dwo_file and
10409 have dwo_unit "live" in that. At least for now.
10410
10411 The DWP file can be made up of a random collection of CUs and TUs.
10412 However, for each CU + set of TUs that came from the same original DWO
10413 file, we can combine them back into a virtual DWO file to save space
10414 (fewer struct dwo_file objects to allocate). Remember that for really
10415 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10416
10417 virtual_dwo_name =
10418 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
10419 get_section_id (&sections.abbrev),
10420 get_section_id (&sections.line),
10421 get_section_id (&sections.loc),
10422 get_section_id (&sections.str_offsets));
10423 make_cleanup (xfree, virtual_dwo_name);
10424 /* Can we use an existing virtual DWO file? */
10425 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
10426 /* Create one if necessary. */
10427 if (*dwo_file_slot == NULL)
10428 {
10429 if (dwarf_read_debug)
10430 {
10431 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
10432 virtual_dwo_name);
10433 }
10434 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
10435 dwo_file->dwo_name
10436 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
10437 virtual_dwo_name,
10438 strlen (virtual_dwo_name));
10439 dwo_file->comp_dir = comp_dir;
10440 dwo_file->sections.abbrev = sections.abbrev;
10441 dwo_file->sections.line = sections.line;
10442 dwo_file->sections.loc = sections.loc;
10443 dwo_file->sections.macinfo = sections.macinfo;
10444 dwo_file->sections.macro = sections.macro;
10445 dwo_file->sections.str_offsets = sections.str_offsets;
10446 /* The "str" section is global to the entire DWP file. */
10447 dwo_file->sections.str = dwp_file->sections.str;
10448 /* The info or types section is assigned below to dwo_unit,
10449 there's no need to record it in dwo_file.
10450 Also, we can't simply record type sections in dwo_file because
10451 we record a pointer into the vector in dwo_unit. As we collect more
10452 types we'll grow the vector and eventually have to reallocate space
10453 for it, invalidating all copies of pointers into the previous
10454 contents. */
10455 *dwo_file_slot = dwo_file;
10456 }
10457 else
10458 {
10459 if (dwarf_read_debug)
10460 {
10461 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
10462 virtual_dwo_name);
10463 }
10464 dwo_file = (struct dwo_file *) *dwo_file_slot;
10465 }
10466 do_cleanups (cleanups);
10467
10468 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10469 dwo_unit->dwo_file = dwo_file;
10470 dwo_unit->signature = signature;
10471 dwo_unit->section =
10472 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
10473 *dwo_unit->section = sections.info_or_types;
10474 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10475
10476 return dwo_unit;
10477 }
10478
10479 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
10480 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
10481 piece within that section used by a TU/CU, return a virtual section
10482 of just that piece. */
10483
10484 static struct dwarf2_section_info
10485 create_dwp_v2_section (struct dwarf2_section_info *section,
10486 bfd_size_type offset, bfd_size_type size)
10487 {
10488 struct dwarf2_section_info result;
10489 asection *sectp;
10490
10491 gdb_assert (section != NULL);
10492 gdb_assert (!section->is_virtual);
10493
10494 memset (&result, 0, sizeof (result));
10495 result.s.containing_section = section;
10496 result.is_virtual = 1;
10497
10498 if (size == 0)
10499 return result;
10500
10501 sectp = get_section_bfd_section (section);
10502
10503 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
10504 bounds of the real section. This is a pretty-rare event, so just
10505 flag an error (easier) instead of a warning and trying to cope. */
10506 if (sectp == NULL
10507 || offset + size > bfd_get_section_size (sectp))
10508 {
10509 bfd *abfd = sectp->owner;
10510
10511 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
10512 " in section %s [in module %s]"),
10513 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
10514 objfile_name (dwarf2_per_objfile->objfile));
10515 }
10516
10517 result.virtual_offset = offset;
10518 result.size = size;
10519 return result;
10520 }
10521
10522 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10523 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10524 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10525 This is for DWP version 2 files. */
10526
10527 static struct dwo_unit *
10528 create_dwo_unit_in_dwp_v2 (struct dwp_file *dwp_file,
10529 uint32_t unit_index,
10530 const char *comp_dir,
10531 ULONGEST signature, int is_debug_types)
10532 {
10533 struct objfile *objfile = dwarf2_per_objfile->objfile;
10534 const struct dwp_hash_table *dwp_htab =
10535 is_debug_types ? dwp_file->tus : dwp_file->cus;
10536 bfd *dbfd = dwp_file->dbfd;
10537 const char *kind = is_debug_types ? "TU" : "CU";
10538 struct dwo_file *dwo_file;
10539 struct dwo_unit *dwo_unit;
10540 struct virtual_v2_dwo_sections sections;
10541 void **dwo_file_slot;
10542 char *virtual_dwo_name;
10543 struct cleanup *cleanups;
10544 int i;
10545
10546 gdb_assert (dwp_file->version == 2);
10547
10548 if (dwarf_read_debug)
10549 {
10550 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
10551 kind,
10552 pulongest (unit_index), hex_string (signature),
10553 dwp_file->name);
10554 }
10555
10556 /* Fetch the section offsets of this DWO unit. */
10557
10558 memset (&sections, 0, sizeof (sections));
10559 cleanups = make_cleanup (null_cleanup, 0);
10560
10561 for (i = 0; i < dwp_htab->nr_columns; ++i)
10562 {
10563 uint32_t offset = read_4_bytes (dbfd,
10564 dwp_htab->section_pool.v2.offsets
10565 + (((unit_index - 1) * dwp_htab->nr_columns
10566 + i)
10567 * sizeof (uint32_t)));
10568 uint32_t size = read_4_bytes (dbfd,
10569 dwp_htab->section_pool.v2.sizes
10570 + (((unit_index - 1) * dwp_htab->nr_columns
10571 + i)
10572 * sizeof (uint32_t)));
10573
10574 switch (dwp_htab->section_pool.v2.section_ids[i])
10575 {
10576 case DW_SECT_INFO:
10577 case DW_SECT_TYPES:
10578 sections.info_or_types_offset = offset;
10579 sections.info_or_types_size = size;
10580 break;
10581 case DW_SECT_ABBREV:
10582 sections.abbrev_offset = offset;
10583 sections.abbrev_size = size;
10584 break;
10585 case DW_SECT_LINE:
10586 sections.line_offset = offset;
10587 sections.line_size = size;
10588 break;
10589 case DW_SECT_LOC:
10590 sections.loc_offset = offset;
10591 sections.loc_size = size;
10592 break;
10593 case DW_SECT_STR_OFFSETS:
10594 sections.str_offsets_offset = offset;
10595 sections.str_offsets_size = size;
10596 break;
10597 case DW_SECT_MACINFO:
10598 sections.macinfo_offset = offset;
10599 sections.macinfo_size = size;
10600 break;
10601 case DW_SECT_MACRO:
10602 sections.macro_offset = offset;
10603 sections.macro_size = size;
10604 break;
10605 }
10606 }
10607
10608 /* It's easier for the rest of the code if we fake a struct dwo_file and
10609 have dwo_unit "live" in that. At least for now.
10610
10611 The DWP file can be made up of a random collection of CUs and TUs.
10612 However, for each CU + set of TUs that came from the same original DWO
10613 file, we can combine them back into a virtual DWO file to save space
10614 (fewer struct dwo_file objects to allocate). Remember that for really
10615 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10616
10617 virtual_dwo_name =
10618 xstrprintf ("virtual-dwo/%ld-%ld-%ld-%ld",
10619 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
10620 (long) (sections.line_size ? sections.line_offset : 0),
10621 (long) (sections.loc_size ? sections.loc_offset : 0),
10622 (long) (sections.str_offsets_size
10623 ? sections.str_offsets_offset : 0));
10624 make_cleanup (xfree, virtual_dwo_name);
10625 /* Can we use an existing virtual DWO file? */
10626 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
10627 /* Create one if necessary. */
10628 if (*dwo_file_slot == NULL)
10629 {
10630 if (dwarf_read_debug)
10631 {
10632 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
10633 virtual_dwo_name);
10634 }
10635 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
10636 dwo_file->dwo_name
10637 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
10638 virtual_dwo_name,
10639 strlen (virtual_dwo_name));
10640 dwo_file->comp_dir = comp_dir;
10641 dwo_file->sections.abbrev =
10642 create_dwp_v2_section (&dwp_file->sections.abbrev,
10643 sections.abbrev_offset, sections.abbrev_size);
10644 dwo_file->sections.line =
10645 create_dwp_v2_section (&dwp_file->sections.line,
10646 sections.line_offset, sections.line_size);
10647 dwo_file->sections.loc =
10648 create_dwp_v2_section (&dwp_file->sections.loc,
10649 sections.loc_offset, sections.loc_size);
10650 dwo_file->sections.macinfo =
10651 create_dwp_v2_section (&dwp_file->sections.macinfo,
10652 sections.macinfo_offset, sections.macinfo_size);
10653 dwo_file->sections.macro =
10654 create_dwp_v2_section (&dwp_file->sections.macro,
10655 sections.macro_offset, sections.macro_size);
10656 dwo_file->sections.str_offsets =
10657 create_dwp_v2_section (&dwp_file->sections.str_offsets,
10658 sections.str_offsets_offset,
10659 sections.str_offsets_size);
10660 /* The "str" section is global to the entire DWP file. */
10661 dwo_file->sections.str = dwp_file->sections.str;
10662 /* The info or types section is assigned below to dwo_unit,
10663 there's no need to record it in dwo_file.
10664 Also, we can't simply record type sections in dwo_file because
10665 we record a pointer into the vector in dwo_unit. As we collect more
10666 types we'll grow the vector and eventually have to reallocate space
10667 for it, invalidating all copies of pointers into the previous
10668 contents. */
10669 *dwo_file_slot = dwo_file;
10670 }
10671 else
10672 {
10673 if (dwarf_read_debug)
10674 {
10675 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
10676 virtual_dwo_name);
10677 }
10678 dwo_file = (struct dwo_file *) *dwo_file_slot;
10679 }
10680 do_cleanups (cleanups);
10681
10682 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10683 dwo_unit->dwo_file = dwo_file;
10684 dwo_unit->signature = signature;
10685 dwo_unit->section =
10686 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
10687 *dwo_unit->section = create_dwp_v2_section (is_debug_types
10688 ? &dwp_file->sections.types
10689 : &dwp_file->sections.info,
10690 sections.info_or_types_offset,
10691 sections.info_or_types_size);
10692 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10693
10694 return dwo_unit;
10695 }
10696
10697 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
10698 Returns NULL if the signature isn't found. */
10699
10700 static struct dwo_unit *
10701 lookup_dwo_unit_in_dwp (struct dwp_file *dwp_file, const char *comp_dir,
10702 ULONGEST signature, int is_debug_types)
10703 {
10704 const struct dwp_hash_table *dwp_htab =
10705 is_debug_types ? dwp_file->tus : dwp_file->cus;
10706 bfd *dbfd = dwp_file->dbfd;
10707 uint32_t mask = dwp_htab->nr_slots - 1;
10708 uint32_t hash = signature & mask;
10709 uint32_t hash2 = ((signature >> 32) & mask) | 1;
10710 unsigned int i;
10711 void **slot;
10712 struct dwo_unit find_dwo_cu;
10713
10714 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
10715 find_dwo_cu.signature = signature;
10716 slot = htab_find_slot (is_debug_types
10717 ? dwp_file->loaded_tus
10718 : dwp_file->loaded_cus,
10719 &find_dwo_cu, INSERT);
10720
10721 if (*slot != NULL)
10722 return (struct dwo_unit *) *slot;
10723
10724 /* Use a for loop so that we don't loop forever on bad debug info. */
10725 for (i = 0; i < dwp_htab->nr_slots; ++i)
10726 {
10727 ULONGEST signature_in_table;
10728
10729 signature_in_table =
10730 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
10731 if (signature_in_table == signature)
10732 {
10733 uint32_t unit_index =
10734 read_4_bytes (dbfd,
10735 dwp_htab->unit_table + hash * sizeof (uint32_t));
10736
10737 if (dwp_file->version == 1)
10738 {
10739 *slot = create_dwo_unit_in_dwp_v1 (dwp_file, unit_index,
10740 comp_dir, signature,
10741 is_debug_types);
10742 }
10743 else
10744 {
10745 *slot = create_dwo_unit_in_dwp_v2 (dwp_file, unit_index,
10746 comp_dir, signature,
10747 is_debug_types);
10748 }
10749 return (struct dwo_unit *) *slot;
10750 }
10751 if (signature_in_table == 0)
10752 return NULL;
10753 hash = (hash + hash2) & mask;
10754 }
10755
10756 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
10757 " [in module %s]"),
10758 dwp_file->name);
10759 }
10760
10761 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
10762 Open the file specified by FILE_NAME and hand it off to BFD for
10763 preliminary analysis. Return a newly initialized bfd *, which
10764 includes a canonicalized copy of FILE_NAME.
10765 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
10766 SEARCH_CWD is true if the current directory is to be searched.
10767 It will be searched before debug-file-directory.
10768 If successful, the file is added to the bfd include table of the
10769 objfile's bfd (see gdb_bfd_record_inclusion).
10770 If unable to find/open the file, return NULL.
10771 NOTE: This function is derived from symfile_bfd_open. */
10772
10773 static gdb_bfd_ref_ptr
10774 try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
10775 {
10776 int desc, flags;
10777 char *absolute_name;
10778 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
10779 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
10780 to debug_file_directory. */
10781 char *search_path;
10782 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
10783
10784 if (search_cwd)
10785 {
10786 if (*debug_file_directory != '\0')
10787 search_path = concat (".", dirname_separator_string,
10788 debug_file_directory, (char *) NULL);
10789 else
10790 search_path = xstrdup (".");
10791 }
10792 else
10793 search_path = xstrdup (debug_file_directory);
10794
10795 flags = OPF_RETURN_REALPATH;
10796 if (is_dwp)
10797 flags |= OPF_SEARCH_IN_PATH;
10798 desc = openp (search_path, flags, file_name,
10799 O_RDONLY | O_BINARY, &absolute_name);
10800 xfree (search_path);
10801 if (desc < 0)
10802 return NULL;
10803
10804 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name, gnutarget, desc));
10805 xfree (absolute_name);
10806 if (sym_bfd == NULL)
10807 return NULL;
10808 bfd_set_cacheable (sym_bfd.get (), 1);
10809
10810 if (!bfd_check_format (sym_bfd.get (), bfd_object))
10811 return NULL;
10812
10813 /* Success. Record the bfd as having been included by the objfile's bfd.
10814 This is important because things like demangled_names_hash lives in the
10815 objfile's per_bfd space and may have references to things like symbol
10816 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
10817 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
10818
10819 return sym_bfd;
10820 }
10821
10822 /* Try to open DWO file FILE_NAME.
10823 COMP_DIR is the DW_AT_comp_dir attribute.
10824 The result is the bfd handle of the file.
10825 If there is a problem finding or opening the file, return NULL.
10826 Upon success, the canonicalized path of the file is stored in the bfd,
10827 same as symfile_bfd_open. */
10828
10829 static gdb_bfd_ref_ptr
10830 open_dwo_file (const char *file_name, const char *comp_dir)
10831 {
10832 if (IS_ABSOLUTE_PATH (file_name))
10833 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
10834
10835 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
10836
10837 if (comp_dir != NULL)
10838 {
10839 char *path_to_try = concat (comp_dir, SLASH_STRING,
10840 file_name, (char *) NULL);
10841
10842 /* NOTE: If comp_dir is a relative path, this will also try the
10843 search path, which seems useful. */
10844 gdb_bfd_ref_ptr abfd (try_open_dwop_file (path_to_try, 0 /*is_dwp*/,
10845 1 /*search_cwd*/));
10846 xfree (path_to_try);
10847 if (abfd != NULL)
10848 return abfd;
10849 }
10850
10851 /* That didn't work, try debug-file-directory, which, despite its name,
10852 is a list of paths. */
10853
10854 if (*debug_file_directory == '\0')
10855 return NULL;
10856
10857 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
10858 }
10859
10860 /* This function is mapped across the sections and remembers the offset and
10861 size of each of the DWO debugging sections we are interested in. */
10862
10863 static void
10864 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
10865 {
10866 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
10867 const struct dwop_section_names *names = &dwop_section_names;
10868
10869 if (section_is_p (sectp->name, &names->abbrev_dwo))
10870 {
10871 dwo_sections->abbrev.s.section = sectp;
10872 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
10873 }
10874 else if (section_is_p (sectp->name, &names->info_dwo))
10875 {
10876 dwo_sections->info.s.section = sectp;
10877 dwo_sections->info.size = bfd_get_section_size (sectp);
10878 }
10879 else if (section_is_p (sectp->name, &names->line_dwo))
10880 {
10881 dwo_sections->line.s.section = sectp;
10882 dwo_sections->line.size = bfd_get_section_size (sectp);
10883 }
10884 else if (section_is_p (sectp->name, &names->loc_dwo))
10885 {
10886 dwo_sections->loc.s.section = sectp;
10887 dwo_sections->loc.size = bfd_get_section_size (sectp);
10888 }
10889 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10890 {
10891 dwo_sections->macinfo.s.section = sectp;
10892 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
10893 }
10894 else if (section_is_p (sectp->name, &names->macro_dwo))
10895 {
10896 dwo_sections->macro.s.section = sectp;
10897 dwo_sections->macro.size = bfd_get_section_size (sectp);
10898 }
10899 else if (section_is_p (sectp->name, &names->str_dwo))
10900 {
10901 dwo_sections->str.s.section = sectp;
10902 dwo_sections->str.size = bfd_get_section_size (sectp);
10903 }
10904 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10905 {
10906 dwo_sections->str_offsets.s.section = sectp;
10907 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
10908 }
10909 else if (section_is_p (sectp->name, &names->types_dwo))
10910 {
10911 struct dwarf2_section_info type_section;
10912
10913 memset (&type_section, 0, sizeof (type_section));
10914 type_section.s.section = sectp;
10915 type_section.size = bfd_get_section_size (sectp);
10916 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
10917 &type_section);
10918 }
10919 }
10920
10921 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
10922 by PER_CU. This is for the non-DWP case.
10923 The result is NULL if DWO_NAME can't be found. */
10924
10925 static struct dwo_file *
10926 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
10927 const char *dwo_name, const char *comp_dir)
10928 {
10929 struct objfile *objfile = dwarf2_per_objfile->objfile;
10930 struct dwo_file *dwo_file;
10931 struct cleanup *cleanups;
10932
10933 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwo_name, comp_dir));
10934 if (dbfd == NULL)
10935 {
10936 if (dwarf_read_debug)
10937 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
10938 return NULL;
10939 }
10940 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
10941 dwo_file->dwo_name = dwo_name;
10942 dwo_file->comp_dir = comp_dir;
10943 dwo_file->dbfd = dbfd.release ();
10944
10945 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
10946
10947 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
10948 &dwo_file->sections);
10949
10950 create_cus_hash_table (*dwo_file, dwo_file->sections.info, dwo_file->cus);
10951
10952 create_debug_types_hash_table (dwo_file, dwo_file->sections.types,
10953 dwo_file->tus);
10954
10955 discard_cleanups (cleanups);
10956
10957 if (dwarf_read_debug)
10958 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
10959
10960 return dwo_file;
10961 }
10962
10963 /* This function is mapped across the sections and remembers the offset and
10964 size of each of the DWP debugging sections common to version 1 and 2 that
10965 we are interested in. */
10966
10967 static void
10968 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
10969 void *dwp_file_ptr)
10970 {
10971 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
10972 const struct dwop_section_names *names = &dwop_section_names;
10973 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
10974
10975 /* Record the ELF section number for later lookup: this is what the
10976 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
10977 gdb_assert (elf_section_nr < dwp_file->num_sections);
10978 dwp_file->elf_sections[elf_section_nr] = sectp;
10979
10980 /* Look for specific sections that we need. */
10981 if (section_is_p (sectp->name, &names->str_dwo))
10982 {
10983 dwp_file->sections.str.s.section = sectp;
10984 dwp_file->sections.str.size = bfd_get_section_size (sectp);
10985 }
10986 else if (section_is_p (sectp->name, &names->cu_index))
10987 {
10988 dwp_file->sections.cu_index.s.section = sectp;
10989 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
10990 }
10991 else if (section_is_p (sectp->name, &names->tu_index))
10992 {
10993 dwp_file->sections.tu_index.s.section = sectp;
10994 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
10995 }
10996 }
10997
10998 /* This function is mapped across the sections and remembers the offset and
10999 size of each of the DWP version 2 debugging sections that we are interested
11000 in. This is split into a separate function because we don't know if we
11001 have version 1 or 2 until we parse the cu_index/tu_index sections. */
11002
11003 static void
11004 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
11005 {
11006 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
11007 const struct dwop_section_names *names = &dwop_section_names;
11008 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
11009
11010 /* Record the ELF section number for later lookup: this is what the
11011 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
11012 gdb_assert (elf_section_nr < dwp_file->num_sections);
11013 dwp_file->elf_sections[elf_section_nr] = sectp;
11014
11015 /* Look for specific sections that we need. */
11016 if (section_is_p (sectp->name, &names->abbrev_dwo))
11017 {
11018 dwp_file->sections.abbrev.s.section = sectp;
11019 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
11020 }
11021 else if (section_is_p (sectp->name, &names->info_dwo))
11022 {
11023 dwp_file->sections.info.s.section = sectp;
11024 dwp_file->sections.info.size = bfd_get_section_size (sectp);
11025 }
11026 else if (section_is_p (sectp->name, &names->line_dwo))
11027 {
11028 dwp_file->sections.line.s.section = sectp;
11029 dwp_file->sections.line.size = bfd_get_section_size (sectp);
11030 }
11031 else if (section_is_p (sectp->name, &names->loc_dwo))
11032 {
11033 dwp_file->sections.loc.s.section = sectp;
11034 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
11035 }
11036 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11037 {
11038 dwp_file->sections.macinfo.s.section = sectp;
11039 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
11040 }
11041 else if (section_is_p (sectp->name, &names->macro_dwo))
11042 {
11043 dwp_file->sections.macro.s.section = sectp;
11044 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
11045 }
11046 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11047 {
11048 dwp_file->sections.str_offsets.s.section = sectp;
11049 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
11050 }
11051 else if (section_is_p (sectp->name, &names->types_dwo))
11052 {
11053 dwp_file->sections.types.s.section = sectp;
11054 dwp_file->sections.types.size = bfd_get_section_size (sectp);
11055 }
11056 }
11057
11058 /* Hash function for dwp_file loaded CUs/TUs. */
11059
11060 static hashval_t
11061 hash_dwp_loaded_cutus (const void *item)
11062 {
11063 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11064
11065 /* This drops the top 32 bits of the signature, but is ok for a hash. */
11066 return dwo_unit->signature;
11067 }
11068
11069 /* Equality function for dwp_file loaded CUs/TUs. */
11070
11071 static int
11072 eq_dwp_loaded_cutus (const void *a, const void *b)
11073 {
11074 const struct dwo_unit *dua = (const struct dwo_unit *) a;
11075 const struct dwo_unit *dub = (const struct dwo_unit *) b;
11076
11077 return dua->signature == dub->signature;
11078 }
11079
11080 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
11081
11082 static htab_t
11083 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
11084 {
11085 return htab_create_alloc_ex (3,
11086 hash_dwp_loaded_cutus,
11087 eq_dwp_loaded_cutus,
11088 NULL,
11089 &objfile->objfile_obstack,
11090 hashtab_obstack_allocate,
11091 dummy_obstack_deallocate);
11092 }
11093
11094 /* Try to open DWP file FILE_NAME.
11095 The result is the bfd handle of the file.
11096 If there is a problem finding or opening the file, return NULL.
11097 Upon success, the canonicalized path of the file is stored in the bfd,
11098 same as symfile_bfd_open. */
11099
11100 static gdb_bfd_ref_ptr
11101 open_dwp_file (const char *file_name)
11102 {
11103 gdb_bfd_ref_ptr abfd (try_open_dwop_file (file_name, 1 /*is_dwp*/,
11104 1 /*search_cwd*/));
11105 if (abfd != NULL)
11106 return abfd;
11107
11108 /* Work around upstream bug 15652.
11109 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
11110 [Whether that's a "bug" is debatable, but it is getting in our way.]
11111 We have no real idea where the dwp file is, because gdb's realpath-ing
11112 of the executable's path may have discarded the needed info.
11113 [IWBN if the dwp file name was recorded in the executable, akin to
11114 .gnu_debuglink, but that doesn't exist yet.]
11115 Strip the directory from FILE_NAME and search again. */
11116 if (*debug_file_directory != '\0')
11117 {
11118 /* Don't implicitly search the current directory here.
11119 If the user wants to search "." to handle this case,
11120 it must be added to debug-file-directory. */
11121 return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
11122 0 /*search_cwd*/);
11123 }
11124
11125 return NULL;
11126 }
11127
11128 /* Initialize the use of the DWP file for the current objfile.
11129 By convention the name of the DWP file is ${objfile}.dwp.
11130 The result is NULL if it can't be found. */
11131
11132 static struct dwp_file *
11133 open_and_init_dwp_file (void)
11134 {
11135 struct objfile *objfile = dwarf2_per_objfile->objfile;
11136 struct dwp_file *dwp_file;
11137
11138 /* Try to find first .dwp for the binary file before any symbolic links
11139 resolving. */
11140
11141 /* If the objfile is a debug file, find the name of the real binary
11142 file and get the name of dwp file from there. */
11143 std::string dwp_name;
11144 if (objfile->separate_debug_objfile_backlink != NULL)
11145 {
11146 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
11147 const char *backlink_basename = lbasename (backlink->original_name);
11148
11149 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
11150 }
11151 else
11152 dwp_name = objfile->original_name;
11153
11154 dwp_name += ".dwp";
11155
11156 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwp_name.c_str ()));
11157 if (dbfd == NULL
11158 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
11159 {
11160 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
11161 dwp_name = objfile_name (objfile);
11162 dwp_name += ".dwp";
11163 dbfd = open_dwp_file (dwp_name.c_str ());
11164 }
11165
11166 if (dbfd == NULL)
11167 {
11168 if (dwarf_read_debug)
11169 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
11170 return NULL;
11171 }
11172 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
11173 dwp_file->name = bfd_get_filename (dbfd.get ());
11174 dwp_file->dbfd = dbfd.release ();
11175
11176 /* +1: section 0 is unused */
11177 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
11178 dwp_file->elf_sections =
11179 OBSTACK_CALLOC (&objfile->objfile_obstack,
11180 dwp_file->num_sections, asection *);
11181
11182 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
11183 dwp_file);
11184
11185 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
11186
11187 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
11188
11189 /* The DWP file version is stored in the hash table. Oh well. */
11190 if (dwp_file->cus->version != dwp_file->tus->version)
11191 {
11192 /* Technically speaking, we should try to limp along, but this is
11193 pretty bizarre. We use pulongest here because that's the established
11194 portability solution (e.g, we cannot use %u for uint32_t). */
11195 error (_("Dwarf Error: DWP file CU version %s doesn't match"
11196 " TU version %s [in DWP file %s]"),
11197 pulongest (dwp_file->cus->version),
11198 pulongest (dwp_file->tus->version), dwp_name.c_str ());
11199 }
11200 dwp_file->version = dwp_file->cus->version;
11201
11202 if (dwp_file->version == 2)
11203 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
11204 dwp_file);
11205
11206 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
11207 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
11208
11209 if (dwarf_read_debug)
11210 {
11211 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
11212 fprintf_unfiltered (gdb_stdlog,
11213 " %s CUs, %s TUs\n",
11214 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
11215 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
11216 }
11217
11218 return dwp_file;
11219 }
11220
11221 /* Wrapper around open_and_init_dwp_file, only open it once. */
11222
11223 static struct dwp_file *
11224 get_dwp_file (void)
11225 {
11226 if (! dwarf2_per_objfile->dwp_checked)
11227 {
11228 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
11229 dwarf2_per_objfile->dwp_checked = 1;
11230 }
11231 return dwarf2_per_objfile->dwp_file;
11232 }
11233
11234 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
11235 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
11236 or in the DWP file for the objfile, referenced by THIS_UNIT.
11237 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
11238 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
11239
11240 This is called, for example, when wanting to read a variable with a
11241 complex location. Therefore we don't want to do file i/o for every call.
11242 Therefore we don't want to look for a DWO file on every call.
11243 Therefore we first see if we've already seen SIGNATURE in a DWP file,
11244 then we check if we've already seen DWO_NAME, and only THEN do we check
11245 for a DWO file.
11246
11247 The result is a pointer to the dwo_unit object or NULL if we didn't find it
11248 (dwo_id mismatch or couldn't find the DWO/DWP file). */
11249
11250 static struct dwo_unit *
11251 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
11252 const char *dwo_name, const char *comp_dir,
11253 ULONGEST signature, int is_debug_types)
11254 {
11255 struct objfile *objfile = dwarf2_per_objfile->objfile;
11256 const char *kind = is_debug_types ? "TU" : "CU";
11257 void **dwo_file_slot;
11258 struct dwo_file *dwo_file;
11259 struct dwp_file *dwp_file;
11260
11261 /* First see if there's a DWP file.
11262 If we have a DWP file but didn't find the DWO inside it, don't
11263 look for the original DWO file. It makes gdb behave differently
11264 depending on whether one is debugging in the build tree. */
11265
11266 dwp_file = get_dwp_file ();
11267 if (dwp_file != NULL)
11268 {
11269 const struct dwp_hash_table *dwp_htab =
11270 is_debug_types ? dwp_file->tus : dwp_file->cus;
11271
11272 if (dwp_htab != NULL)
11273 {
11274 struct dwo_unit *dwo_cutu =
11275 lookup_dwo_unit_in_dwp (dwp_file, comp_dir,
11276 signature, is_debug_types);
11277
11278 if (dwo_cutu != NULL)
11279 {
11280 if (dwarf_read_debug)
11281 {
11282 fprintf_unfiltered (gdb_stdlog,
11283 "Virtual DWO %s %s found: @%s\n",
11284 kind, hex_string (signature),
11285 host_address_to_string (dwo_cutu));
11286 }
11287 return dwo_cutu;
11288 }
11289 }
11290 }
11291 else
11292 {
11293 /* No DWP file, look for the DWO file. */
11294
11295 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
11296 if (*dwo_file_slot == NULL)
11297 {
11298 /* Read in the file and build a table of the CUs/TUs it contains. */
11299 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
11300 }
11301 /* NOTE: This will be NULL if unable to open the file. */
11302 dwo_file = (struct dwo_file *) *dwo_file_slot;
11303
11304 if (dwo_file != NULL)
11305 {
11306 struct dwo_unit *dwo_cutu = NULL;
11307
11308 if (is_debug_types && dwo_file->tus)
11309 {
11310 struct dwo_unit find_dwo_cutu;
11311
11312 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
11313 find_dwo_cutu.signature = signature;
11314 dwo_cutu
11315 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
11316 }
11317 else if (!is_debug_types && dwo_file->cus)
11318 {
11319 struct dwo_unit find_dwo_cutu;
11320
11321 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
11322 find_dwo_cutu.signature = signature;
11323 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
11324 &find_dwo_cutu);
11325 }
11326
11327 if (dwo_cutu != NULL)
11328 {
11329 if (dwarf_read_debug)
11330 {
11331 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
11332 kind, dwo_name, hex_string (signature),
11333 host_address_to_string (dwo_cutu));
11334 }
11335 return dwo_cutu;
11336 }
11337 }
11338 }
11339
11340 /* We didn't find it. This could mean a dwo_id mismatch, or
11341 someone deleted the DWO/DWP file, or the search path isn't set up
11342 correctly to find the file. */
11343
11344 if (dwarf_read_debug)
11345 {
11346 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
11347 kind, dwo_name, hex_string (signature));
11348 }
11349
11350 /* This is a warning and not a complaint because it can be caused by
11351 pilot error (e.g., user accidentally deleting the DWO). */
11352 {
11353 /* Print the name of the DWP file if we looked there, helps the user
11354 better diagnose the problem. */
11355 char *dwp_text = NULL;
11356 struct cleanup *cleanups;
11357
11358 if (dwp_file != NULL)
11359 dwp_text = xstrprintf (" [in DWP file %s]", lbasename (dwp_file->name));
11360 cleanups = make_cleanup (xfree, dwp_text);
11361
11362 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset 0x%x"
11363 " [in module %s]"),
11364 kind, dwo_name, hex_string (signature),
11365 dwp_text != NULL ? dwp_text : "",
11366 this_unit->is_debug_types ? "TU" : "CU",
11367 to_underlying (this_unit->sect_off), objfile_name (objfile));
11368
11369 do_cleanups (cleanups);
11370 }
11371 return NULL;
11372 }
11373
11374 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
11375 See lookup_dwo_cutu_unit for details. */
11376
11377 static struct dwo_unit *
11378 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
11379 const char *dwo_name, const char *comp_dir,
11380 ULONGEST signature)
11381 {
11382 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
11383 }
11384
11385 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
11386 See lookup_dwo_cutu_unit for details. */
11387
11388 static struct dwo_unit *
11389 lookup_dwo_type_unit (struct signatured_type *this_tu,
11390 const char *dwo_name, const char *comp_dir)
11391 {
11392 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
11393 }
11394
11395 /* Traversal function for queue_and_load_all_dwo_tus. */
11396
11397 static int
11398 queue_and_load_dwo_tu (void **slot, void *info)
11399 {
11400 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
11401 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
11402 ULONGEST signature = dwo_unit->signature;
11403 struct signatured_type *sig_type =
11404 lookup_dwo_signatured_type (per_cu->cu, signature);
11405
11406 if (sig_type != NULL)
11407 {
11408 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
11409
11410 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
11411 a real dependency of PER_CU on SIG_TYPE. That is detected later
11412 while processing PER_CU. */
11413 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
11414 load_full_type_unit (sig_cu);
11415 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
11416 }
11417
11418 return 1;
11419 }
11420
11421 /* Queue all TUs contained in the DWO of PER_CU to be read in.
11422 The DWO may have the only definition of the type, though it may not be
11423 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
11424 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
11425
11426 static void
11427 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
11428 {
11429 struct dwo_unit *dwo_unit;
11430 struct dwo_file *dwo_file;
11431
11432 gdb_assert (!per_cu->is_debug_types);
11433 gdb_assert (get_dwp_file () == NULL);
11434 gdb_assert (per_cu->cu != NULL);
11435
11436 dwo_unit = per_cu->cu->dwo_unit;
11437 gdb_assert (dwo_unit != NULL);
11438
11439 dwo_file = dwo_unit->dwo_file;
11440 if (dwo_file->tus != NULL)
11441 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
11442 }
11443
11444 /* Free all resources associated with DWO_FILE.
11445 Close the DWO file and munmap the sections.
11446 All memory should be on the objfile obstack. */
11447
11448 static void
11449 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
11450 {
11451
11452 /* Note: dbfd is NULL for virtual DWO files. */
11453 gdb_bfd_unref (dwo_file->dbfd);
11454
11455 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
11456 }
11457
11458 /* Wrapper for free_dwo_file for use in cleanups. */
11459
11460 static void
11461 free_dwo_file_cleanup (void *arg)
11462 {
11463 struct dwo_file *dwo_file = (struct dwo_file *) arg;
11464 struct objfile *objfile = dwarf2_per_objfile->objfile;
11465
11466 free_dwo_file (dwo_file, objfile);
11467 }
11468
11469 /* Traversal function for free_dwo_files. */
11470
11471 static int
11472 free_dwo_file_from_slot (void **slot, void *info)
11473 {
11474 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
11475 struct objfile *objfile = (struct objfile *) info;
11476
11477 free_dwo_file (dwo_file, objfile);
11478
11479 return 1;
11480 }
11481
11482 /* Free all resources associated with DWO_FILES. */
11483
11484 static void
11485 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
11486 {
11487 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
11488 }
11489 \f
11490 /* Read in various DIEs. */
11491
11492 /* qsort helper for inherit_abstract_dies. */
11493
11494 static int
11495 unsigned_int_compar (const void *ap, const void *bp)
11496 {
11497 unsigned int a = *(unsigned int *) ap;
11498 unsigned int b = *(unsigned int *) bp;
11499
11500 return (a > b) - (b > a);
11501 }
11502
11503 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
11504 Inherit only the children of the DW_AT_abstract_origin DIE not being
11505 already referenced by DW_AT_abstract_origin from the children of the
11506 current DIE. */
11507
11508 static void
11509 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
11510 {
11511 struct die_info *child_die;
11512 unsigned die_children_count;
11513 /* CU offsets which were referenced by children of the current DIE. */
11514 sect_offset *offsets;
11515 sect_offset *offsets_end, *offsetp;
11516 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
11517 struct die_info *origin_die;
11518 /* Iterator of the ORIGIN_DIE children. */
11519 struct die_info *origin_child_die;
11520 struct cleanup *cleanups;
11521 struct attribute *attr;
11522 struct dwarf2_cu *origin_cu;
11523 struct pending **origin_previous_list_in_scope;
11524
11525 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
11526 if (!attr)
11527 return;
11528
11529 /* Note that following die references may follow to a die in a
11530 different cu. */
11531
11532 origin_cu = cu;
11533 origin_die = follow_die_ref (die, attr, &origin_cu);
11534
11535 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
11536 symbols in. */
11537 origin_previous_list_in_scope = origin_cu->list_in_scope;
11538 origin_cu->list_in_scope = cu->list_in_scope;
11539
11540 if (die->tag != origin_die->tag
11541 && !(die->tag == DW_TAG_inlined_subroutine
11542 && origin_die->tag == DW_TAG_subprogram))
11543 complaint (&symfile_complaints,
11544 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
11545 to_underlying (die->sect_off),
11546 to_underlying (origin_die->sect_off));
11547
11548 child_die = die->child;
11549 die_children_count = 0;
11550 while (child_die && child_die->tag)
11551 {
11552 child_die = sibling_die (child_die);
11553 die_children_count++;
11554 }
11555 offsets = XNEWVEC (sect_offset, die_children_count);
11556 cleanups = make_cleanup (xfree, offsets);
11557
11558 offsets_end = offsets;
11559 for (child_die = die->child;
11560 child_die && child_die->tag;
11561 child_die = sibling_die (child_die))
11562 {
11563 struct die_info *child_origin_die;
11564 struct dwarf2_cu *child_origin_cu;
11565
11566 /* We are trying to process concrete instance entries:
11567 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
11568 it's not relevant to our analysis here. i.e. detecting DIEs that are
11569 present in the abstract instance but not referenced in the concrete
11570 one. */
11571 if (child_die->tag == DW_TAG_call_site
11572 || child_die->tag == DW_TAG_GNU_call_site)
11573 continue;
11574
11575 /* For each CHILD_DIE, find the corresponding child of
11576 ORIGIN_DIE. If there is more than one layer of
11577 DW_AT_abstract_origin, follow them all; there shouldn't be,
11578 but GCC versions at least through 4.4 generate this (GCC PR
11579 40573). */
11580 child_origin_die = child_die;
11581 child_origin_cu = cu;
11582 while (1)
11583 {
11584 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
11585 child_origin_cu);
11586 if (attr == NULL)
11587 break;
11588 child_origin_die = follow_die_ref (child_origin_die, attr,
11589 &child_origin_cu);
11590 }
11591
11592 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
11593 counterpart may exist. */
11594 if (child_origin_die != child_die)
11595 {
11596 if (child_die->tag != child_origin_die->tag
11597 && !(child_die->tag == DW_TAG_inlined_subroutine
11598 && child_origin_die->tag == DW_TAG_subprogram))
11599 complaint (&symfile_complaints,
11600 _("Child DIE 0x%x and its abstract origin 0x%x have "
11601 "different tags"),
11602 to_underlying (child_die->sect_off),
11603 to_underlying (child_origin_die->sect_off));
11604 if (child_origin_die->parent != origin_die)
11605 complaint (&symfile_complaints,
11606 _("Child DIE 0x%x and its abstract origin 0x%x have "
11607 "different parents"),
11608 to_underlying (child_die->sect_off),
11609 to_underlying (child_origin_die->sect_off));
11610 else
11611 *offsets_end++ = child_origin_die->sect_off;
11612 }
11613 }
11614 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
11615 unsigned_int_compar);
11616 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
11617 if (offsetp[-1] == *offsetp)
11618 complaint (&symfile_complaints,
11619 _("Multiple children of DIE 0x%x refer "
11620 "to DIE 0x%x as their abstract origin"),
11621 to_underlying (die->sect_off), to_underlying (*offsetp));
11622
11623 offsetp = offsets;
11624 origin_child_die = origin_die->child;
11625 while (origin_child_die && origin_child_die->tag)
11626 {
11627 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
11628 while (offsetp < offsets_end
11629 && *offsetp < origin_child_die->sect_off)
11630 offsetp++;
11631 if (offsetp >= offsets_end
11632 || *offsetp > origin_child_die->sect_off)
11633 {
11634 /* Found that ORIGIN_CHILD_DIE is really not referenced.
11635 Check whether we're already processing ORIGIN_CHILD_DIE.
11636 This can happen with mutually referenced abstract_origins.
11637 PR 16581. */
11638 if (!origin_child_die->in_process)
11639 process_die (origin_child_die, origin_cu);
11640 }
11641 origin_child_die = sibling_die (origin_child_die);
11642 }
11643 origin_cu->list_in_scope = origin_previous_list_in_scope;
11644
11645 do_cleanups (cleanups);
11646 }
11647
11648 static void
11649 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
11650 {
11651 struct objfile *objfile = cu->objfile;
11652 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11653 struct context_stack *newobj;
11654 CORE_ADDR lowpc;
11655 CORE_ADDR highpc;
11656 struct die_info *child_die;
11657 struct attribute *attr, *call_line, *call_file;
11658 const char *name;
11659 CORE_ADDR baseaddr;
11660 struct block *block;
11661 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
11662 VEC (symbolp) *template_args = NULL;
11663 struct template_symbol *templ_func = NULL;
11664
11665 if (inlined_func)
11666 {
11667 /* If we do not have call site information, we can't show the
11668 caller of this inlined function. That's too confusing, so
11669 only use the scope for local variables. */
11670 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
11671 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
11672 if (call_line == NULL || call_file == NULL)
11673 {
11674 read_lexical_block_scope (die, cu);
11675 return;
11676 }
11677 }
11678
11679 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11680
11681 name = dwarf2_name (die, cu);
11682
11683 /* Ignore functions with missing or empty names. These are actually
11684 illegal according to the DWARF standard. */
11685 if (name == NULL)
11686 {
11687 complaint (&symfile_complaints,
11688 _("missing name for subprogram DIE at %d"),
11689 to_underlying (die->sect_off));
11690 return;
11691 }
11692
11693 /* Ignore functions with missing or invalid low and high pc attributes. */
11694 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
11695 <= PC_BOUNDS_INVALID)
11696 {
11697 attr = dwarf2_attr (die, DW_AT_external, cu);
11698 if (!attr || !DW_UNSND (attr))
11699 complaint (&symfile_complaints,
11700 _("cannot get low and high bounds "
11701 "for subprogram DIE at %d"),
11702 to_underlying (die->sect_off));
11703 return;
11704 }
11705
11706 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11707 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
11708
11709 /* If we have any template arguments, then we must allocate a
11710 different sort of symbol. */
11711 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
11712 {
11713 if (child_die->tag == DW_TAG_template_type_param
11714 || child_die->tag == DW_TAG_template_value_param)
11715 {
11716 templ_func = allocate_template_symbol (objfile);
11717 templ_func->base.is_cplus_template_function = 1;
11718 break;
11719 }
11720 }
11721
11722 newobj = push_context (0, lowpc);
11723 newobj->name = new_symbol_full (die, read_type_die (die, cu), cu,
11724 (struct symbol *) templ_func);
11725
11726 /* If there is a location expression for DW_AT_frame_base, record
11727 it. */
11728 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
11729 if (attr)
11730 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
11731
11732 /* If there is a location for the static link, record it. */
11733 newobj->static_link = NULL;
11734 attr = dwarf2_attr (die, DW_AT_static_link, cu);
11735 if (attr)
11736 {
11737 newobj->static_link
11738 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
11739 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
11740 }
11741
11742 cu->list_in_scope = &local_symbols;
11743
11744 if (die->child != NULL)
11745 {
11746 child_die = die->child;
11747 while (child_die && child_die->tag)
11748 {
11749 if (child_die->tag == DW_TAG_template_type_param
11750 || child_die->tag == DW_TAG_template_value_param)
11751 {
11752 struct symbol *arg = new_symbol (child_die, NULL, cu);
11753
11754 if (arg != NULL)
11755 VEC_safe_push (symbolp, template_args, arg);
11756 }
11757 else
11758 process_die (child_die, cu);
11759 child_die = sibling_die (child_die);
11760 }
11761 }
11762
11763 inherit_abstract_dies (die, cu);
11764
11765 /* If we have a DW_AT_specification, we might need to import using
11766 directives from the context of the specification DIE. See the
11767 comment in determine_prefix. */
11768 if (cu->language == language_cplus
11769 && dwarf2_attr (die, DW_AT_specification, cu))
11770 {
11771 struct dwarf2_cu *spec_cu = cu;
11772 struct die_info *spec_die = die_specification (die, &spec_cu);
11773
11774 while (spec_die)
11775 {
11776 child_die = spec_die->child;
11777 while (child_die && child_die->tag)
11778 {
11779 if (child_die->tag == DW_TAG_imported_module)
11780 process_die (child_die, spec_cu);
11781 child_die = sibling_die (child_die);
11782 }
11783
11784 /* In some cases, GCC generates specification DIEs that
11785 themselves contain DW_AT_specification attributes. */
11786 spec_die = die_specification (spec_die, &spec_cu);
11787 }
11788 }
11789
11790 newobj = pop_context ();
11791 /* Make a block for the local symbols within. */
11792 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
11793 newobj->static_link, lowpc, highpc);
11794
11795 /* For C++, set the block's scope. */
11796 if ((cu->language == language_cplus
11797 || cu->language == language_fortran
11798 || cu->language == language_d
11799 || cu->language == language_rust)
11800 && cu->processing_has_namespace_info)
11801 block_set_scope (block, determine_prefix (die, cu),
11802 &objfile->objfile_obstack);
11803
11804 /* If we have address ranges, record them. */
11805 dwarf2_record_block_ranges (die, block, baseaddr, cu);
11806
11807 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
11808
11809 /* Attach template arguments to function. */
11810 if (! VEC_empty (symbolp, template_args))
11811 {
11812 gdb_assert (templ_func != NULL);
11813
11814 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
11815 templ_func->template_arguments
11816 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
11817 templ_func->n_template_arguments);
11818 memcpy (templ_func->template_arguments,
11819 VEC_address (symbolp, template_args),
11820 (templ_func->n_template_arguments * sizeof (struct symbol *)));
11821 VEC_free (symbolp, template_args);
11822 }
11823
11824 /* In C++, we can have functions nested inside functions (e.g., when
11825 a function declares a class that has methods). This means that
11826 when we finish processing a function scope, we may need to go
11827 back to building a containing block's symbol lists. */
11828 local_symbols = newobj->locals;
11829 local_using_directives = newobj->local_using_directives;
11830
11831 /* If we've finished processing a top-level function, subsequent
11832 symbols go in the file symbol list. */
11833 if (outermost_context_p ())
11834 cu->list_in_scope = &file_symbols;
11835 }
11836
11837 /* Process all the DIES contained within a lexical block scope. Start
11838 a new scope, process the dies, and then close the scope. */
11839
11840 static void
11841 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
11842 {
11843 struct objfile *objfile = cu->objfile;
11844 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11845 struct context_stack *newobj;
11846 CORE_ADDR lowpc, highpc;
11847 struct die_info *child_die;
11848 CORE_ADDR baseaddr;
11849
11850 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11851
11852 /* Ignore blocks with missing or invalid low and high pc attributes. */
11853 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
11854 as multiple lexical blocks? Handling children in a sane way would
11855 be nasty. Might be easier to properly extend generic blocks to
11856 describe ranges. */
11857 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
11858 {
11859 case PC_BOUNDS_NOT_PRESENT:
11860 /* DW_TAG_lexical_block has no attributes, process its children as if
11861 there was no wrapping by that DW_TAG_lexical_block.
11862 GCC does no longer produces such DWARF since GCC r224161. */
11863 for (child_die = die->child;
11864 child_die != NULL && child_die->tag;
11865 child_die = sibling_die (child_die))
11866 process_die (child_die, cu);
11867 return;
11868 case PC_BOUNDS_INVALID:
11869 return;
11870 }
11871 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11872 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
11873
11874 push_context (0, lowpc);
11875 if (die->child != NULL)
11876 {
11877 child_die = die->child;
11878 while (child_die && child_die->tag)
11879 {
11880 process_die (child_die, cu);
11881 child_die = sibling_die (child_die);
11882 }
11883 }
11884 inherit_abstract_dies (die, cu);
11885 newobj = pop_context ();
11886
11887 if (local_symbols != NULL || local_using_directives != NULL)
11888 {
11889 struct block *block
11890 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
11891 newobj->start_addr, highpc);
11892
11893 /* Note that recording ranges after traversing children, as we
11894 do here, means that recording a parent's ranges entails
11895 walking across all its children's ranges as they appear in
11896 the address map, which is quadratic behavior.
11897
11898 It would be nicer to record the parent's ranges before
11899 traversing its children, simply overriding whatever you find
11900 there. But since we don't even decide whether to create a
11901 block until after we've traversed its children, that's hard
11902 to do. */
11903 dwarf2_record_block_ranges (die, block, baseaddr, cu);
11904 }
11905 local_symbols = newobj->locals;
11906 local_using_directives = newobj->local_using_directives;
11907 }
11908
11909 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
11910
11911 static void
11912 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
11913 {
11914 struct objfile *objfile = cu->objfile;
11915 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11916 CORE_ADDR pc, baseaddr;
11917 struct attribute *attr;
11918 struct call_site *call_site, call_site_local;
11919 void **slot;
11920 int nparams;
11921 struct die_info *child_die;
11922
11923 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11924
11925 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
11926 if (attr == NULL)
11927 {
11928 /* This was a pre-DWARF-5 GNU extension alias
11929 for DW_AT_call_return_pc. */
11930 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11931 }
11932 if (!attr)
11933 {
11934 complaint (&symfile_complaints,
11935 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
11936 "DIE 0x%x [in module %s]"),
11937 to_underlying (die->sect_off), objfile_name (objfile));
11938 return;
11939 }
11940 pc = attr_value_as_address (attr) + baseaddr;
11941 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
11942
11943 if (cu->call_site_htab == NULL)
11944 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
11945 NULL, &objfile->objfile_obstack,
11946 hashtab_obstack_allocate, NULL);
11947 call_site_local.pc = pc;
11948 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
11949 if (*slot != NULL)
11950 {
11951 complaint (&symfile_complaints,
11952 _("Duplicate PC %s for DW_TAG_call_site "
11953 "DIE 0x%x [in module %s]"),
11954 paddress (gdbarch, pc), to_underlying (die->sect_off),
11955 objfile_name (objfile));
11956 return;
11957 }
11958
11959 /* Count parameters at the caller. */
11960
11961 nparams = 0;
11962 for (child_die = die->child; child_die && child_die->tag;
11963 child_die = sibling_die (child_die))
11964 {
11965 if (child_die->tag != DW_TAG_call_site_parameter
11966 && child_die->tag != DW_TAG_GNU_call_site_parameter)
11967 {
11968 complaint (&symfile_complaints,
11969 _("Tag %d is not DW_TAG_call_site_parameter in "
11970 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
11971 child_die->tag, to_underlying (child_die->sect_off),
11972 objfile_name (objfile));
11973 continue;
11974 }
11975
11976 nparams++;
11977 }
11978
11979 call_site
11980 = ((struct call_site *)
11981 obstack_alloc (&objfile->objfile_obstack,
11982 sizeof (*call_site)
11983 + (sizeof (*call_site->parameter) * (nparams - 1))));
11984 *slot = call_site;
11985 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
11986 call_site->pc = pc;
11987
11988 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
11989 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
11990 {
11991 struct die_info *func_die;
11992
11993 /* Skip also over DW_TAG_inlined_subroutine. */
11994 for (func_die = die->parent;
11995 func_die && func_die->tag != DW_TAG_subprogram
11996 && func_die->tag != DW_TAG_subroutine_type;
11997 func_die = func_die->parent);
11998
11999 /* DW_AT_call_all_calls is a superset
12000 of DW_AT_call_all_tail_calls. */
12001 if (func_die
12002 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
12003 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
12004 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
12005 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
12006 {
12007 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
12008 not complete. But keep CALL_SITE for look ups via call_site_htab,
12009 both the initial caller containing the real return address PC and
12010 the final callee containing the current PC of a chain of tail
12011 calls do not need to have the tail call list complete. But any
12012 function candidate for a virtual tail call frame searched via
12013 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
12014 determined unambiguously. */
12015 }
12016 else
12017 {
12018 struct type *func_type = NULL;
12019
12020 if (func_die)
12021 func_type = get_die_type (func_die, cu);
12022 if (func_type != NULL)
12023 {
12024 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
12025
12026 /* Enlist this call site to the function. */
12027 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
12028 TYPE_TAIL_CALL_LIST (func_type) = call_site;
12029 }
12030 else
12031 complaint (&symfile_complaints,
12032 _("Cannot find function owning DW_TAG_call_site "
12033 "DIE 0x%x [in module %s]"),
12034 to_underlying (die->sect_off), objfile_name (objfile));
12035 }
12036 }
12037
12038 attr = dwarf2_attr (die, DW_AT_call_target, cu);
12039 if (attr == NULL)
12040 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
12041 if (attr == NULL)
12042 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
12043 if (attr == NULL)
12044 {
12045 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
12046 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12047 }
12048 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
12049 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
12050 /* Keep NULL DWARF_BLOCK. */;
12051 else if (attr_form_is_block (attr))
12052 {
12053 struct dwarf2_locexpr_baton *dlbaton;
12054
12055 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
12056 dlbaton->data = DW_BLOCK (attr)->data;
12057 dlbaton->size = DW_BLOCK (attr)->size;
12058 dlbaton->per_cu = cu->per_cu;
12059
12060 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
12061 }
12062 else if (attr_form_is_ref (attr))
12063 {
12064 struct dwarf2_cu *target_cu = cu;
12065 struct die_info *target_die;
12066
12067 target_die = follow_die_ref (die, attr, &target_cu);
12068 gdb_assert (target_cu->objfile == objfile);
12069 if (die_is_declaration (target_die, target_cu))
12070 {
12071 const char *target_physname;
12072
12073 /* Prefer the mangled name; otherwise compute the demangled one. */
12074 target_physname = dw2_linkage_name (target_die, target_cu);
12075 if (target_physname == NULL)
12076 target_physname = dwarf2_physname (NULL, target_die, target_cu);
12077 if (target_physname == NULL)
12078 complaint (&symfile_complaints,
12079 _("DW_AT_call_target target DIE has invalid "
12080 "physname, for referencing DIE 0x%x [in module %s]"),
12081 to_underlying (die->sect_off), objfile_name (objfile));
12082 else
12083 SET_FIELD_PHYSNAME (call_site->target, target_physname);
12084 }
12085 else
12086 {
12087 CORE_ADDR lowpc;
12088
12089 /* DW_AT_entry_pc should be preferred. */
12090 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
12091 <= PC_BOUNDS_INVALID)
12092 complaint (&symfile_complaints,
12093 _("DW_AT_call_target target DIE has invalid "
12094 "low pc, for referencing DIE 0x%x [in module %s]"),
12095 to_underlying (die->sect_off), objfile_name (objfile));
12096 else
12097 {
12098 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12099 SET_FIELD_PHYSADDR (call_site->target, lowpc);
12100 }
12101 }
12102 }
12103 else
12104 complaint (&symfile_complaints,
12105 _("DW_TAG_call_site DW_AT_call_target is neither "
12106 "block nor reference, for DIE 0x%x [in module %s]"),
12107 to_underlying (die->sect_off), objfile_name (objfile));
12108
12109 call_site->per_cu = cu->per_cu;
12110
12111 for (child_die = die->child;
12112 child_die && child_die->tag;
12113 child_die = sibling_die (child_die))
12114 {
12115 struct call_site_parameter *parameter;
12116 struct attribute *loc, *origin;
12117
12118 if (child_die->tag != DW_TAG_call_site_parameter
12119 && child_die->tag != DW_TAG_GNU_call_site_parameter)
12120 {
12121 /* Already printed the complaint above. */
12122 continue;
12123 }
12124
12125 gdb_assert (call_site->parameter_count < nparams);
12126 parameter = &call_site->parameter[call_site->parameter_count];
12127
12128 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
12129 specifies DW_TAG_formal_parameter. Value of the data assumed for the
12130 register is contained in DW_AT_call_value. */
12131
12132 loc = dwarf2_attr (child_die, DW_AT_location, cu);
12133 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
12134 if (origin == NULL)
12135 {
12136 /* This was a pre-DWARF-5 GNU extension alias
12137 for DW_AT_call_parameter. */
12138 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
12139 }
12140 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
12141 {
12142 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
12143
12144 sect_offset sect_off
12145 = (sect_offset) dwarf2_get_ref_die_offset (origin);
12146 if (!offset_in_cu_p (&cu->header, sect_off))
12147 {
12148 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
12149 binding can be done only inside one CU. Such referenced DIE
12150 therefore cannot be even moved to DW_TAG_partial_unit. */
12151 complaint (&symfile_complaints,
12152 _("DW_AT_call_parameter offset is not in CU for "
12153 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
12154 to_underlying (child_die->sect_off),
12155 objfile_name (objfile));
12156 continue;
12157 }
12158 parameter->u.param_cu_off
12159 = (cu_offset) (sect_off - cu->header.sect_off);
12160 }
12161 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
12162 {
12163 complaint (&symfile_complaints,
12164 _("No DW_FORM_block* DW_AT_location for "
12165 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
12166 to_underlying (child_die->sect_off), objfile_name (objfile));
12167 continue;
12168 }
12169 else
12170 {
12171 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
12172 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
12173 if (parameter->u.dwarf_reg != -1)
12174 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
12175 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
12176 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
12177 &parameter->u.fb_offset))
12178 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
12179 else
12180 {
12181 complaint (&symfile_complaints,
12182 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
12183 "for DW_FORM_block* DW_AT_location is supported for "
12184 "DW_TAG_call_site child DIE 0x%x "
12185 "[in module %s]"),
12186 to_underlying (child_die->sect_off),
12187 objfile_name (objfile));
12188 continue;
12189 }
12190 }
12191
12192 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
12193 if (attr == NULL)
12194 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
12195 if (!attr_form_is_block (attr))
12196 {
12197 complaint (&symfile_complaints,
12198 _("No DW_FORM_block* DW_AT_call_value for "
12199 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
12200 to_underlying (child_die->sect_off),
12201 objfile_name (objfile));
12202 continue;
12203 }
12204 parameter->value = DW_BLOCK (attr)->data;
12205 parameter->value_size = DW_BLOCK (attr)->size;
12206
12207 /* Parameters are not pre-cleared by memset above. */
12208 parameter->data_value = NULL;
12209 parameter->data_value_size = 0;
12210 call_site->parameter_count++;
12211
12212 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
12213 if (attr == NULL)
12214 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
12215 if (attr)
12216 {
12217 if (!attr_form_is_block (attr))
12218 complaint (&symfile_complaints,
12219 _("No DW_FORM_block* DW_AT_call_data_value for "
12220 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
12221 to_underlying (child_die->sect_off),
12222 objfile_name (objfile));
12223 else
12224 {
12225 parameter->data_value = DW_BLOCK (attr)->data;
12226 parameter->data_value_size = DW_BLOCK (attr)->size;
12227 }
12228 }
12229 }
12230 }
12231
12232 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
12233 reading .debug_rnglists.
12234 Callback's type should be:
12235 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
12236 Return true if the attributes are present and valid, otherwise,
12237 return false. */
12238
12239 template <typename Callback>
12240 static bool
12241 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
12242 Callback &&callback)
12243 {
12244 struct objfile *objfile = cu->objfile;
12245 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12246 struct comp_unit_head *cu_header = &cu->header;
12247 bfd *obfd = objfile->obfd;
12248 unsigned int addr_size = cu_header->addr_size;
12249 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12250 /* Base address selection entry. */
12251 CORE_ADDR base;
12252 int found_base;
12253 unsigned int dummy;
12254 const gdb_byte *buffer;
12255 CORE_ADDR low = 0;
12256 CORE_ADDR high = 0;
12257 CORE_ADDR baseaddr;
12258 bool overflow = false;
12259
12260 found_base = cu->base_known;
12261 base = cu->base_address;
12262
12263 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
12264 if (offset >= dwarf2_per_objfile->rnglists.size)
12265 {
12266 complaint (&symfile_complaints,
12267 _("Offset %d out of bounds for DW_AT_ranges attribute"),
12268 offset);
12269 return false;
12270 }
12271 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
12272
12273 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12274
12275 while (1)
12276 {
12277 /* Initialize it due to a false compiler warning. */
12278 CORE_ADDR range_beginning = 0, range_end = 0;
12279 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
12280 + dwarf2_per_objfile->rnglists.size);
12281 unsigned int bytes_read;
12282
12283 if (buffer == buf_end)
12284 {
12285 overflow = true;
12286 break;
12287 }
12288 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
12289 switch (rlet)
12290 {
12291 case DW_RLE_end_of_list:
12292 break;
12293 case DW_RLE_base_address:
12294 if (buffer + cu->header.addr_size > buf_end)
12295 {
12296 overflow = true;
12297 break;
12298 }
12299 base = read_address (obfd, buffer, cu, &bytes_read);
12300 found_base = 1;
12301 buffer += bytes_read;
12302 break;
12303 case DW_RLE_start_length:
12304 if (buffer + cu->header.addr_size > buf_end)
12305 {
12306 overflow = true;
12307 break;
12308 }
12309 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
12310 buffer += bytes_read;
12311 range_end = (range_beginning
12312 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
12313 buffer += bytes_read;
12314 if (buffer > buf_end)
12315 {
12316 overflow = true;
12317 break;
12318 }
12319 break;
12320 case DW_RLE_offset_pair:
12321 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12322 buffer += bytes_read;
12323 if (buffer > buf_end)
12324 {
12325 overflow = true;
12326 break;
12327 }
12328 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12329 buffer += bytes_read;
12330 if (buffer > buf_end)
12331 {
12332 overflow = true;
12333 break;
12334 }
12335 break;
12336 case DW_RLE_start_end:
12337 if (buffer + 2 * cu->header.addr_size > buf_end)
12338 {
12339 overflow = true;
12340 break;
12341 }
12342 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
12343 buffer += bytes_read;
12344 range_end = read_address (obfd, buffer, cu, &bytes_read);
12345 buffer += bytes_read;
12346 break;
12347 default:
12348 complaint (&symfile_complaints,
12349 _("Invalid .debug_rnglists data (no base address)"));
12350 return false;
12351 }
12352 if (rlet == DW_RLE_end_of_list || overflow)
12353 break;
12354 if (rlet == DW_RLE_base_address)
12355 continue;
12356
12357 if (!found_base)
12358 {
12359 /* We have no valid base address for the ranges
12360 data. */
12361 complaint (&symfile_complaints,
12362 _("Invalid .debug_rnglists data (no base address)"));
12363 return false;
12364 }
12365
12366 if (range_beginning > range_end)
12367 {
12368 /* Inverted range entries are invalid. */
12369 complaint (&symfile_complaints,
12370 _("Invalid .debug_rnglists data (inverted range)"));
12371 return false;
12372 }
12373
12374 /* Empty range entries have no effect. */
12375 if (range_beginning == range_end)
12376 continue;
12377
12378 range_beginning += base;
12379 range_end += base;
12380
12381 /* A not-uncommon case of bad debug info.
12382 Don't pollute the addrmap with bad data. */
12383 if (range_beginning + baseaddr == 0
12384 && !dwarf2_per_objfile->has_section_at_zero)
12385 {
12386 complaint (&symfile_complaints,
12387 _(".debug_rnglists entry has start address of zero"
12388 " [in module %s]"), objfile_name (objfile));
12389 continue;
12390 }
12391
12392 callback (range_beginning, range_end);
12393 }
12394
12395 if (overflow)
12396 {
12397 complaint (&symfile_complaints,
12398 _("Offset %d is not terminated "
12399 "for DW_AT_ranges attribute"),
12400 offset);
12401 return false;
12402 }
12403
12404 return true;
12405 }
12406
12407 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
12408 Callback's type should be:
12409 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
12410 Return 1 if the attributes are present and valid, otherwise, return 0. */
12411
12412 template <typename Callback>
12413 static int
12414 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
12415 Callback &&callback)
12416 {
12417 struct objfile *objfile = cu->objfile;
12418 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12419 struct comp_unit_head *cu_header = &cu->header;
12420 bfd *obfd = objfile->obfd;
12421 unsigned int addr_size = cu_header->addr_size;
12422 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12423 /* Base address selection entry. */
12424 CORE_ADDR base;
12425 int found_base;
12426 unsigned int dummy;
12427 const gdb_byte *buffer;
12428 CORE_ADDR baseaddr;
12429
12430 if (cu_header->version >= 5)
12431 return dwarf2_rnglists_process (offset, cu, callback);
12432
12433 found_base = cu->base_known;
12434 base = cu->base_address;
12435
12436 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
12437 if (offset >= dwarf2_per_objfile->ranges.size)
12438 {
12439 complaint (&symfile_complaints,
12440 _("Offset %d out of bounds for DW_AT_ranges attribute"),
12441 offset);
12442 return 0;
12443 }
12444 buffer = dwarf2_per_objfile->ranges.buffer + offset;
12445
12446 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12447
12448 while (1)
12449 {
12450 CORE_ADDR range_beginning, range_end;
12451
12452 range_beginning = read_address (obfd, buffer, cu, &dummy);
12453 buffer += addr_size;
12454 range_end = read_address (obfd, buffer, cu, &dummy);
12455 buffer += addr_size;
12456 offset += 2 * addr_size;
12457
12458 /* An end of list marker is a pair of zero addresses. */
12459 if (range_beginning == 0 && range_end == 0)
12460 /* Found the end of list entry. */
12461 break;
12462
12463 /* Each base address selection entry is a pair of 2 values.
12464 The first is the largest possible address, the second is
12465 the base address. Check for a base address here. */
12466 if ((range_beginning & mask) == mask)
12467 {
12468 /* If we found the largest possible address, then we already
12469 have the base address in range_end. */
12470 base = range_end;
12471 found_base = 1;
12472 continue;
12473 }
12474
12475 if (!found_base)
12476 {
12477 /* We have no valid base address for the ranges
12478 data. */
12479 complaint (&symfile_complaints,
12480 _("Invalid .debug_ranges data (no base address)"));
12481 return 0;
12482 }
12483
12484 if (range_beginning > range_end)
12485 {
12486 /* Inverted range entries are invalid. */
12487 complaint (&symfile_complaints,
12488 _("Invalid .debug_ranges data (inverted range)"));
12489 return 0;
12490 }
12491
12492 /* Empty range entries have no effect. */
12493 if (range_beginning == range_end)
12494 continue;
12495
12496 range_beginning += base;
12497 range_end += base;
12498
12499 /* A not-uncommon case of bad debug info.
12500 Don't pollute the addrmap with bad data. */
12501 if (range_beginning + baseaddr == 0
12502 && !dwarf2_per_objfile->has_section_at_zero)
12503 {
12504 complaint (&symfile_complaints,
12505 _(".debug_ranges entry has start address of zero"
12506 " [in module %s]"), objfile_name (objfile));
12507 continue;
12508 }
12509
12510 callback (range_beginning, range_end);
12511 }
12512
12513 return 1;
12514 }
12515
12516 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
12517 Return 1 if the attributes are present and valid, otherwise, return 0.
12518 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
12519
12520 static int
12521 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
12522 CORE_ADDR *high_return, struct dwarf2_cu *cu,
12523 struct partial_symtab *ranges_pst)
12524 {
12525 struct objfile *objfile = cu->objfile;
12526 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12527 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
12528 SECT_OFF_TEXT (objfile));
12529 int low_set = 0;
12530 CORE_ADDR low = 0;
12531 CORE_ADDR high = 0;
12532 int retval;
12533
12534 retval = dwarf2_ranges_process (offset, cu,
12535 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
12536 {
12537 if (ranges_pst != NULL)
12538 {
12539 CORE_ADDR lowpc;
12540 CORE_ADDR highpc;
12541
12542 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
12543 range_beginning + baseaddr);
12544 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
12545 range_end + baseaddr);
12546 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
12547 ranges_pst);
12548 }
12549
12550 /* FIXME: This is recording everything as a low-high
12551 segment of consecutive addresses. We should have a
12552 data structure for discontiguous block ranges
12553 instead. */
12554 if (! low_set)
12555 {
12556 low = range_beginning;
12557 high = range_end;
12558 low_set = 1;
12559 }
12560 else
12561 {
12562 if (range_beginning < low)
12563 low = range_beginning;
12564 if (range_end > high)
12565 high = range_end;
12566 }
12567 });
12568 if (!retval)
12569 return 0;
12570
12571 if (! low_set)
12572 /* If the first entry is an end-of-list marker, the range
12573 describes an empty scope, i.e. no instructions. */
12574 return 0;
12575
12576 if (low_return)
12577 *low_return = low;
12578 if (high_return)
12579 *high_return = high;
12580 return 1;
12581 }
12582
12583 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
12584 definition for the return value. *LOWPC and *HIGHPC are set iff
12585 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
12586
12587 static enum pc_bounds_kind
12588 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
12589 CORE_ADDR *highpc, struct dwarf2_cu *cu,
12590 struct partial_symtab *pst)
12591 {
12592 struct attribute *attr;
12593 struct attribute *attr_high;
12594 CORE_ADDR low = 0;
12595 CORE_ADDR high = 0;
12596 enum pc_bounds_kind ret;
12597
12598 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
12599 if (attr_high)
12600 {
12601 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12602 if (attr)
12603 {
12604 low = attr_value_as_address (attr);
12605 high = attr_value_as_address (attr_high);
12606 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
12607 high += low;
12608 }
12609 else
12610 /* Found high w/o low attribute. */
12611 return PC_BOUNDS_INVALID;
12612
12613 /* Found consecutive range of addresses. */
12614 ret = PC_BOUNDS_HIGH_LOW;
12615 }
12616 else
12617 {
12618 attr = dwarf2_attr (die, DW_AT_ranges, cu);
12619 if (attr != NULL)
12620 {
12621 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
12622 We take advantage of the fact that DW_AT_ranges does not appear
12623 in DW_TAG_compile_unit of DWO files. */
12624 int need_ranges_base = die->tag != DW_TAG_compile_unit;
12625 unsigned int ranges_offset = (DW_UNSND (attr)
12626 + (need_ranges_base
12627 ? cu->ranges_base
12628 : 0));
12629
12630 /* Value of the DW_AT_ranges attribute is the offset in the
12631 .debug_ranges section. */
12632 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
12633 return PC_BOUNDS_INVALID;
12634 /* Found discontinuous range of addresses. */
12635 ret = PC_BOUNDS_RANGES;
12636 }
12637 else
12638 return PC_BOUNDS_NOT_PRESENT;
12639 }
12640
12641 /* read_partial_die has also the strict LOW < HIGH requirement. */
12642 if (high <= low)
12643 return PC_BOUNDS_INVALID;
12644
12645 /* When using the GNU linker, .gnu.linkonce. sections are used to
12646 eliminate duplicate copies of functions and vtables and such.
12647 The linker will arbitrarily choose one and discard the others.
12648 The AT_*_pc values for such functions refer to local labels in
12649 these sections. If the section from that file was discarded, the
12650 labels are not in the output, so the relocs get a value of 0.
12651 If this is a discarded function, mark the pc bounds as invalid,
12652 so that GDB will ignore it. */
12653 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
12654 return PC_BOUNDS_INVALID;
12655
12656 *lowpc = low;
12657 if (highpc)
12658 *highpc = high;
12659 return ret;
12660 }
12661
12662 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
12663 its low and high PC addresses. Do nothing if these addresses could not
12664 be determined. Otherwise, set LOWPC to the low address if it is smaller,
12665 and HIGHPC to the high address if greater than HIGHPC. */
12666
12667 static void
12668 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
12669 CORE_ADDR *lowpc, CORE_ADDR *highpc,
12670 struct dwarf2_cu *cu)
12671 {
12672 CORE_ADDR low, high;
12673 struct die_info *child = die->child;
12674
12675 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
12676 {
12677 *lowpc = std::min (*lowpc, low);
12678 *highpc = std::max (*highpc, high);
12679 }
12680
12681 /* If the language does not allow nested subprograms (either inside
12682 subprograms or lexical blocks), we're done. */
12683 if (cu->language != language_ada)
12684 return;
12685
12686 /* Check all the children of the given DIE. If it contains nested
12687 subprograms, then check their pc bounds. Likewise, we need to
12688 check lexical blocks as well, as they may also contain subprogram
12689 definitions. */
12690 while (child && child->tag)
12691 {
12692 if (child->tag == DW_TAG_subprogram
12693 || child->tag == DW_TAG_lexical_block)
12694 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
12695 child = sibling_die (child);
12696 }
12697 }
12698
12699 /* Get the low and high pc's represented by the scope DIE, and store
12700 them in *LOWPC and *HIGHPC. If the correct values can't be
12701 determined, set *LOWPC to -1 and *HIGHPC to 0. */
12702
12703 static void
12704 get_scope_pc_bounds (struct die_info *die,
12705 CORE_ADDR *lowpc, CORE_ADDR *highpc,
12706 struct dwarf2_cu *cu)
12707 {
12708 CORE_ADDR best_low = (CORE_ADDR) -1;
12709 CORE_ADDR best_high = (CORE_ADDR) 0;
12710 CORE_ADDR current_low, current_high;
12711
12712 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
12713 >= PC_BOUNDS_RANGES)
12714 {
12715 best_low = current_low;
12716 best_high = current_high;
12717 }
12718 else
12719 {
12720 struct die_info *child = die->child;
12721
12722 while (child && child->tag)
12723 {
12724 switch (child->tag) {
12725 case DW_TAG_subprogram:
12726 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
12727 break;
12728 case DW_TAG_namespace:
12729 case DW_TAG_module:
12730 /* FIXME: carlton/2004-01-16: Should we do this for
12731 DW_TAG_class_type/DW_TAG_structure_type, too? I think
12732 that current GCC's always emit the DIEs corresponding
12733 to definitions of methods of classes as children of a
12734 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
12735 the DIEs giving the declarations, which could be
12736 anywhere). But I don't see any reason why the
12737 standards says that they have to be there. */
12738 get_scope_pc_bounds (child, &current_low, &current_high, cu);
12739
12740 if (current_low != ((CORE_ADDR) -1))
12741 {
12742 best_low = std::min (best_low, current_low);
12743 best_high = std::max (best_high, current_high);
12744 }
12745 break;
12746 default:
12747 /* Ignore. */
12748 break;
12749 }
12750
12751 child = sibling_die (child);
12752 }
12753 }
12754
12755 *lowpc = best_low;
12756 *highpc = best_high;
12757 }
12758
12759 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
12760 in DIE. */
12761
12762 static void
12763 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
12764 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
12765 {
12766 struct objfile *objfile = cu->objfile;
12767 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12768 struct attribute *attr;
12769 struct attribute *attr_high;
12770
12771 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
12772 if (attr_high)
12773 {
12774 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12775 if (attr)
12776 {
12777 CORE_ADDR low = attr_value_as_address (attr);
12778 CORE_ADDR high = attr_value_as_address (attr_high);
12779
12780 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
12781 high += low;
12782
12783 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
12784 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
12785 record_block_range (block, low, high - 1);
12786 }
12787 }
12788
12789 attr = dwarf2_attr (die, DW_AT_ranges, cu);
12790 if (attr)
12791 {
12792 bfd *obfd = objfile->obfd;
12793 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
12794 We take advantage of the fact that DW_AT_ranges does not appear
12795 in DW_TAG_compile_unit of DWO files. */
12796 int need_ranges_base = die->tag != DW_TAG_compile_unit;
12797
12798 /* The value of the DW_AT_ranges attribute is the offset of the
12799 address range list in the .debug_ranges section. */
12800 unsigned long offset = (DW_UNSND (attr)
12801 + (need_ranges_base ? cu->ranges_base : 0));
12802 const gdb_byte *buffer;
12803
12804 /* For some target architectures, but not others, the
12805 read_address function sign-extends the addresses it returns.
12806 To recognize base address selection entries, we need a
12807 mask. */
12808 unsigned int addr_size = cu->header.addr_size;
12809 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12810
12811 /* The base address, to which the next pair is relative. Note
12812 that this 'base' is a DWARF concept: most entries in a range
12813 list are relative, to reduce the number of relocs against the
12814 debugging information. This is separate from this function's
12815 'baseaddr' argument, which GDB uses to relocate debugging
12816 information from a shared library based on the address at
12817 which the library was loaded. */
12818 CORE_ADDR base = cu->base_address;
12819 int base_known = cu->base_known;
12820
12821 dwarf2_ranges_process (offset, cu,
12822 [&] (CORE_ADDR start, CORE_ADDR end)
12823 {
12824 start += baseaddr;
12825 end += baseaddr;
12826 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
12827 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
12828 record_block_range (block, start, end - 1);
12829 });
12830 }
12831 }
12832
12833 /* Check whether the producer field indicates either of GCC < 4.6, or the
12834 Intel C/C++ compiler, and cache the result in CU. */
12835
12836 static void
12837 check_producer (struct dwarf2_cu *cu)
12838 {
12839 int major, minor;
12840
12841 if (cu->producer == NULL)
12842 {
12843 /* For unknown compilers expect their behavior is DWARF version
12844 compliant.
12845
12846 GCC started to support .debug_types sections by -gdwarf-4 since
12847 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
12848 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
12849 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
12850 interpreted incorrectly by GDB now - GCC PR debug/48229. */
12851 }
12852 else if (producer_is_gcc (cu->producer, &major, &minor))
12853 {
12854 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
12855 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
12856 }
12857 else if (startswith (cu->producer, "Intel(R) C"))
12858 cu->producer_is_icc = 1;
12859 else
12860 {
12861 /* For other non-GCC compilers, expect their behavior is DWARF version
12862 compliant. */
12863 }
12864
12865 cu->checked_producer = 1;
12866 }
12867
12868 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
12869 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
12870 during 4.6.0 experimental. */
12871
12872 static int
12873 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
12874 {
12875 if (!cu->checked_producer)
12876 check_producer (cu);
12877
12878 return cu->producer_is_gxx_lt_4_6;
12879 }
12880
12881 /* Return the default accessibility type if it is not overriden by
12882 DW_AT_accessibility. */
12883
12884 static enum dwarf_access_attribute
12885 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
12886 {
12887 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
12888 {
12889 /* The default DWARF 2 accessibility for members is public, the default
12890 accessibility for inheritance is private. */
12891
12892 if (die->tag != DW_TAG_inheritance)
12893 return DW_ACCESS_public;
12894 else
12895 return DW_ACCESS_private;
12896 }
12897 else
12898 {
12899 /* DWARF 3+ defines the default accessibility a different way. The same
12900 rules apply now for DW_TAG_inheritance as for the members and it only
12901 depends on the container kind. */
12902
12903 if (die->parent->tag == DW_TAG_class_type)
12904 return DW_ACCESS_private;
12905 else
12906 return DW_ACCESS_public;
12907 }
12908 }
12909
12910 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
12911 offset. If the attribute was not found return 0, otherwise return
12912 1. If it was found but could not properly be handled, set *OFFSET
12913 to 0. */
12914
12915 static int
12916 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
12917 LONGEST *offset)
12918 {
12919 struct attribute *attr;
12920
12921 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
12922 if (attr != NULL)
12923 {
12924 *offset = 0;
12925
12926 /* Note that we do not check for a section offset first here.
12927 This is because DW_AT_data_member_location is new in DWARF 4,
12928 so if we see it, we can assume that a constant form is really
12929 a constant and not a section offset. */
12930 if (attr_form_is_constant (attr))
12931 *offset = dwarf2_get_attr_constant_value (attr, 0);
12932 else if (attr_form_is_section_offset (attr))
12933 dwarf2_complex_location_expr_complaint ();
12934 else if (attr_form_is_block (attr))
12935 *offset = decode_locdesc (DW_BLOCK (attr), cu);
12936 else
12937 dwarf2_complex_location_expr_complaint ();
12938
12939 return 1;
12940 }
12941
12942 return 0;
12943 }
12944
12945 /* Add an aggregate field to the field list. */
12946
12947 static void
12948 dwarf2_add_field (struct field_info *fip, struct die_info *die,
12949 struct dwarf2_cu *cu)
12950 {
12951 struct objfile *objfile = cu->objfile;
12952 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12953 struct nextfield *new_field;
12954 struct attribute *attr;
12955 struct field *fp;
12956 const char *fieldname = "";
12957
12958 /* Allocate a new field list entry and link it in. */
12959 new_field = XNEW (struct nextfield);
12960 make_cleanup (xfree, new_field);
12961 memset (new_field, 0, sizeof (struct nextfield));
12962
12963 if (die->tag == DW_TAG_inheritance)
12964 {
12965 new_field->next = fip->baseclasses;
12966 fip->baseclasses = new_field;
12967 }
12968 else
12969 {
12970 new_field->next = fip->fields;
12971 fip->fields = new_field;
12972 }
12973 fip->nfields++;
12974
12975 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
12976 if (attr)
12977 new_field->accessibility = DW_UNSND (attr);
12978 else
12979 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
12980 if (new_field->accessibility != DW_ACCESS_public)
12981 fip->non_public_fields = 1;
12982
12983 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
12984 if (attr)
12985 new_field->virtuality = DW_UNSND (attr);
12986 else
12987 new_field->virtuality = DW_VIRTUALITY_none;
12988
12989 fp = &new_field->field;
12990
12991 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
12992 {
12993 LONGEST offset;
12994
12995 /* Data member other than a C++ static data member. */
12996
12997 /* Get type of field. */
12998 fp->type = die_type (die, cu);
12999
13000 SET_FIELD_BITPOS (*fp, 0);
13001
13002 /* Get bit size of field (zero if none). */
13003 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
13004 if (attr)
13005 {
13006 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
13007 }
13008 else
13009 {
13010 FIELD_BITSIZE (*fp) = 0;
13011 }
13012
13013 /* Get bit offset of field. */
13014 if (handle_data_member_location (die, cu, &offset))
13015 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
13016 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
13017 if (attr)
13018 {
13019 if (gdbarch_bits_big_endian (gdbarch))
13020 {
13021 /* For big endian bits, the DW_AT_bit_offset gives the
13022 additional bit offset from the MSB of the containing
13023 anonymous object to the MSB of the field. We don't
13024 have to do anything special since we don't need to
13025 know the size of the anonymous object. */
13026 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
13027 }
13028 else
13029 {
13030 /* For little endian bits, compute the bit offset to the
13031 MSB of the anonymous object, subtract off the number of
13032 bits from the MSB of the field to the MSB of the
13033 object, and then subtract off the number of bits of
13034 the field itself. The result is the bit offset of
13035 the LSB of the field. */
13036 int anonymous_size;
13037 int bit_offset = DW_UNSND (attr);
13038
13039 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13040 if (attr)
13041 {
13042 /* The size of the anonymous object containing
13043 the bit field is explicit, so use the
13044 indicated size (in bytes). */
13045 anonymous_size = DW_UNSND (attr);
13046 }
13047 else
13048 {
13049 /* The size of the anonymous object containing
13050 the bit field must be inferred from the type
13051 attribute of the data member containing the
13052 bit field. */
13053 anonymous_size = TYPE_LENGTH (fp->type);
13054 }
13055 SET_FIELD_BITPOS (*fp,
13056 (FIELD_BITPOS (*fp)
13057 + anonymous_size * bits_per_byte
13058 - bit_offset - FIELD_BITSIZE (*fp)));
13059 }
13060 }
13061 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
13062 if (attr != NULL)
13063 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
13064 + dwarf2_get_attr_constant_value (attr, 0)));
13065
13066 /* Get name of field. */
13067 fieldname = dwarf2_name (die, cu);
13068 if (fieldname == NULL)
13069 fieldname = "";
13070
13071 /* The name is already allocated along with this objfile, so we don't
13072 need to duplicate it for the type. */
13073 fp->name = fieldname;
13074
13075 /* Change accessibility for artificial fields (e.g. virtual table
13076 pointer or virtual base class pointer) to private. */
13077 if (dwarf2_attr (die, DW_AT_artificial, cu))
13078 {
13079 FIELD_ARTIFICIAL (*fp) = 1;
13080 new_field->accessibility = DW_ACCESS_private;
13081 fip->non_public_fields = 1;
13082 }
13083 }
13084 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
13085 {
13086 /* C++ static member. */
13087
13088 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
13089 is a declaration, but all versions of G++ as of this writing
13090 (so through at least 3.2.1) incorrectly generate
13091 DW_TAG_variable tags. */
13092
13093 const char *physname;
13094
13095 /* Get name of field. */
13096 fieldname = dwarf2_name (die, cu);
13097 if (fieldname == NULL)
13098 return;
13099
13100 attr = dwarf2_attr (die, DW_AT_const_value, cu);
13101 if (attr
13102 /* Only create a symbol if this is an external value.
13103 new_symbol checks this and puts the value in the global symbol
13104 table, which we want. If it is not external, new_symbol
13105 will try to put the value in cu->list_in_scope which is wrong. */
13106 && dwarf2_flag_true_p (die, DW_AT_external, cu))
13107 {
13108 /* A static const member, not much different than an enum as far as
13109 we're concerned, except that we can support more types. */
13110 new_symbol (die, NULL, cu);
13111 }
13112
13113 /* Get physical name. */
13114 physname = dwarf2_physname (fieldname, die, cu);
13115
13116 /* The name is already allocated along with this objfile, so we don't
13117 need to duplicate it for the type. */
13118 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
13119 FIELD_TYPE (*fp) = die_type (die, cu);
13120 FIELD_NAME (*fp) = fieldname;
13121 }
13122 else if (die->tag == DW_TAG_inheritance)
13123 {
13124 LONGEST offset;
13125
13126 /* C++ base class field. */
13127 if (handle_data_member_location (die, cu, &offset))
13128 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
13129 FIELD_BITSIZE (*fp) = 0;
13130 FIELD_TYPE (*fp) = die_type (die, cu);
13131 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
13132 fip->nbaseclasses++;
13133 }
13134 }
13135
13136 /* Add a typedef defined in the scope of the FIP's class. */
13137
13138 static void
13139 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
13140 struct dwarf2_cu *cu)
13141 {
13142 struct typedef_field_list *new_field;
13143 struct typedef_field *fp;
13144
13145 /* Allocate a new field list entry and link it in. */
13146 new_field = XCNEW (struct typedef_field_list);
13147 make_cleanup (xfree, new_field);
13148
13149 gdb_assert (die->tag == DW_TAG_typedef);
13150
13151 fp = &new_field->field;
13152
13153 /* Get name of field. */
13154 fp->name = dwarf2_name (die, cu);
13155 if (fp->name == NULL)
13156 return;
13157
13158 fp->type = read_type_die (die, cu);
13159
13160 new_field->next = fip->typedef_field_list;
13161 fip->typedef_field_list = new_field;
13162 fip->typedef_field_list_count++;
13163 }
13164
13165 /* Create the vector of fields, and attach it to the type. */
13166
13167 static void
13168 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
13169 struct dwarf2_cu *cu)
13170 {
13171 int nfields = fip->nfields;
13172
13173 /* Record the field count, allocate space for the array of fields,
13174 and create blank accessibility bitfields if necessary. */
13175 TYPE_NFIELDS (type) = nfields;
13176 TYPE_FIELDS (type) = (struct field *)
13177 TYPE_ALLOC (type, sizeof (struct field) * nfields);
13178 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
13179
13180 if (fip->non_public_fields && cu->language != language_ada)
13181 {
13182 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13183
13184 TYPE_FIELD_PRIVATE_BITS (type) =
13185 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13186 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
13187
13188 TYPE_FIELD_PROTECTED_BITS (type) =
13189 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13190 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
13191
13192 TYPE_FIELD_IGNORE_BITS (type) =
13193 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13194 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
13195 }
13196
13197 /* If the type has baseclasses, allocate and clear a bit vector for
13198 TYPE_FIELD_VIRTUAL_BITS. */
13199 if (fip->nbaseclasses && cu->language != language_ada)
13200 {
13201 int num_bytes = B_BYTES (fip->nbaseclasses);
13202 unsigned char *pointer;
13203
13204 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13205 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
13206 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
13207 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
13208 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
13209 }
13210
13211 /* Copy the saved-up fields into the field vector. Start from the head of
13212 the list, adding to the tail of the field array, so that they end up in
13213 the same order in the array in which they were added to the list. */
13214 while (nfields-- > 0)
13215 {
13216 struct nextfield *fieldp;
13217
13218 if (fip->fields)
13219 {
13220 fieldp = fip->fields;
13221 fip->fields = fieldp->next;
13222 }
13223 else
13224 {
13225 fieldp = fip->baseclasses;
13226 fip->baseclasses = fieldp->next;
13227 }
13228
13229 TYPE_FIELD (type, nfields) = fieldp->field;
13230 switch (fieldp->accessibility)
13231 {
13232 case DW_ACCESS_private:
13233 if (cu->language != language_ada)
13234 SET_TYPE_FIELD_PRIVATE (type, nfields);
13235 break;
13236
13237 case DW_ACCESS_protected:
13238 if (cu->language != language_ada)
13239 SET_TYPE_FIELD_PROTECTED (type, nfields);
13240 break;
13241
13242 case DW_ACCESS_public:
13243 break;
13244
13245 default:
13246 /* Unknown accessibility. Complain and treat it as public. */
13247 {
13248 complaint (&symfile_complaints, _("unsupported accessibility %d"),
13249 fieldp->accessibility);
13250 }
13251 break;
13252 }
13253 if (nfields < fip->nbaseclasses)
13254 {
13255 switch (fieldp->virtuality)
13256 {
13257 case DW_VIRTUALITY_virtual:
13258 case DW_VIRTUALITY_pure_virtual:
13259 if (cu->language == language_ada)
13260 error (_("unexpected virtuality in component of Ada type"));
13261 SET_TYPE_FIELD_VIRTUAL (type, nfields);
13262 break;
13263 }
13264 }
13265 }
13266 }
13267
13268 /* Return true if this member function is a constructor, false
13269 otherwise. */
13270
13271 static int
13272 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
13273 {
13274 const char *fieldname;
13275 const char *type_name;
13276 int len;
13277
13278 if (die->parent == NULL)
13279 return 0;
13280
13281 if (die->parent->tag != DW_TAG_structure_type
13282 && die->parent->tag != DW_TAG_union_type
13283 && die->parent->tag != DW_TAG_class_type)
13284 return 0;
13285
13286 fieldname = dwarf2_name (die, cu);
13287 type_name = dwarf2_name (die->parent, cu);
13288 if (fieldname == NULL || type_name == NULL)
13289 return 0;
13290
13291 len = strlen (fieldname);
13292 return (strncmp (fieldname, type_name, len) == 0
13293 && (type_name[len] == '\0' || type_name[len] == '<'));
13294 }
13295
13296 /* Add a member function to the proper fieldlist. */
13297
13298 static void
13299 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
13300 struct type *type, struct dwarf2_cu *cu)
13301 {
13302 struct objfile *objfile = cu->objfile;
13303 struct attribute *attr;
13304 struct fnfieldlist *flp;
13305 int i;
13306 struct fn_field *fnp;
13307 const char *fieldname;
13308 struct nextfnfield *new_fnfield;
13309 struct type *this_type;
13310 enum dwarf_access_attribute accessibility;
13311
13312 if (cu->language == language_ada)
13313 error (_("unexpected member function in Ada type"));
13314
13315 /* Get name of member function. */
13316 fieldname = dwarf2_name (die, cu);
13317 if (fieldname == NULL)
13318 return;
13319
13320 /* Look up member function name in fieldlist. */
13321 for (i = 0; i < fip->nfnfields; i++)
13322 {
13323 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
13324 break;
13325 }
13326
13327 /* Create new list element if necessary. */
13328 if (i < fip->nfnfields)
13329 flp = &fip->fnfieldlists[i];
13330 else
13331 {
13332 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
13333 {
13334 fip->fnfieldlists = (struct fnfieldlist *)
13335 xrealloc (fip->fnfieldlists,
13336 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
13337 * sizeof (struct fnfieldlist));
13338 if (fip->nfnfields == 0)
13339 make_cleanup (free_current_contents, &fip->fnfieldlists);
13340 }
13341 flp = &fip->fnfieldlists[fip->nfnfields];
13342 flp->name = fieldname;
13343 flp->length = 0;
13344 flp->head = NULL;
13345 i = fip->nfnfields++;
13346 }
13347
13348 /* Create a new member function field and chain it to the field list
13349 entry. */
13350 new_fnfield = XNEW (struct nextfnfield);
13351 make_cleanup (xfree, new_fnfield);
13352 memset (new_fnfield, 0, sizeof (struct nextfnfield));
13353 new_fnfield->next = flp->head;
13354 flp->head = new_fnfield;
13355 flp->length++;
13356
13357 /* Fill in the member function field info. */
13358 fnp = &new_fnfield->fnfield;
13359
13360 /* Delay processing of the physname until later. */
13361 if (cu->language == language_cplus)
13362 {
13363 add_to_method_list (type, i, flp->length - 1, fieldname,
13364 die, cu);
13365 }
13366 else
13367 {
13368 const char *physname = dwarf2_physname (fieldname, die, cu);
13369 fnp->physname = physname ? physname : "";
13370 }
13371
13372 fnp->type = alloc_type (objfile);
13373 this_type = read_type_die (die, cu);
13374 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
13375 {
13376 int nparams = TYPE_NFIELDS (this_type);
13377
13378 /* TYPE is the domain of this method, and THIS_TYPE is the type
13379 of the method itself (TYPE_CODE_METHOD). */
13380 smash_to_method_type (fnp->type, type,
13381 TYPE_TARGET_TYPE (this_type),
13382 TYPE_FIELDS (this_type),
13383 TYPE_NFIELDS (this_type),
13384 TYPE_VARARGS (this_type));
13385
13386 /* Handle static member functions.
13387 Dwarf2 has no clean way to discern C++ static and non-static
13388 member functions. G++ helps GDB by marking the first
13389 parameter for non-static member functions (which is the this
13390 pointer) as artificial. We obtain this information from
13391 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
13392 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
13393 fnp->voffset = VOFFSET_STATIC;
13394 }
13395 else
13396 complaint (&symfile_complaints, _("member function type missing for '%s'"),
13397 dwarf2_full_name (fieldname, die, cu));
13398
13399 /* Get fcontext from DW_AT_containing_type if present. */
13400 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
13401 fnp->fcontext = die_containing_type (die, cu);
13402
13403 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
13404 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
13405
13406 /* Get accessibility. */
13407 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
13408 if (attr)
13409 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
13410 else
13411 accessibility = dwarf2_default_access_attribute (die, cu);
13412 switch (accessibility)
13413 {
13414 case DW_ACCESS_private:
13415 fnp->is_private = 1;
13416 break;
13417 case DW_ACCESS_protected:
13418 fnp->is_protected = 1;
13419 break;
13420 }
13421
13422 /* Check for artificial methods. */
13423 attr = dwarf2_attr (die, DW_AT_artificial, cu);
13424 if (attr && DW_UNSND (attr) != 0)
13425 fnp->is_artificial = 1;
13426
13427 fnp->is_constructor = dwarf2_is_constructor (die, cu);
13428
13429 /* Get index in virtual function table if it is a virtual member
13430 function. For older versions of GCC, this is an offset in the
13431 appropriate virtual table, as specified by DW_AT_containing_type.
13432 For everyone else, it is an expression to be evaluated relative
13433 to the object address. */
13434
13435 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
13436 if (attr)
13437 {
13438 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
13439 {
13440 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
13441 {
13442 /* Old-style GCC. */
13443 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
13444 }
13445 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
13446 || (DW_BLOCK (attr)->size > 1
13447 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
13448 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
13449 {
13450 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
13451 if ((fnp->voffset % cu->header.addr_size) != 0)
13452 dwarf2_complex_location_expr_complaint ();
13453 else
13454 fnp->voffset /= cu->header.addr_size;
13455 fnp->voffset += 2;
13456 }
13457 else
13458 dwarf2_complex_location_expr_complaint ();
13459
13460 if (!fnp->fcontext)
13461 {
13462 /* If there is no `this' field and no DW_AT_containing_type,
13463 we cannot actually find a base class context for the
13464 vtable! */
13465 if (TYPE_NFIELDS (this_type) == 0
13466 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
13467 {
13468 complaint (&symfile_complaints,
13469 _("cannot determine context for virtual member "
13470 "function \"%s\" (offset %d)"),
13471 fieldname, to_underlying (die->sect_off));
13472 }
13473 else
13474 {
13475 fnp->fcontext
13476 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
13477 }
13478 }
13479 }
13480 else if (attr_form_is_section_offset (attr))
13481 {
13482 dwarf2_complex_location_expr_complaint ();
13483 }
13484 else
13485 {
13486 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
13487 fieldname);
13488 }
13489 }
13490 else
13491 {
13492 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
13493 if (attr && DW_UNSND (attr))
13494 {
13495 /* GCC does this, as of 2008-08-25; PR debug/37237. */
13496 complaint (&symfile_complaints,
13497 _("Member function \"%s\" (offset %d) is virtual "
13498 "but the vtable offset is not specified"),
13499 fieldname, to_underlying (die->sect_off));
13500 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13501 TYPE_CPLUS_DYNAMIC (type) = 1;
13502 }
13503 }
13504 }
13505
13506 /* Create the vector of member function fields, and attach it to the type. */
13507
13508 static void
13509 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
13510 struct dwarf2_cu *cu)
13511 {
13512 struct fnfieldlist *flp;
13513 int i;
13514
13515 if (cu->language == language_ada)
13516 error (_("unexpected member functions in Ada type"));
13517
13518 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13519 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
13520 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
13521
13522 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
13523 {
13524 struct nextfnfield *nfp = flp->head;
13525 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
13526 int k;
13527
13528 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
13529 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
13530 fn_flp->fn_fields = (struct fn_field *)
13531 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
13532 for (k = flp->length; (k--, nfp); nfp = nfp->next)
13533 fn_flp->fn_fields[k] = nfp->fnfield;
13534 }
13535
13536 TYPE_NFN_FIELDS (type) = fip->nfnfields;
13537 }
13538
13539 /* Returns non-zero if NAME is the name of a vtable member in CU's
13540 language, zero otherwise. */
13541 static int
13542 is_vtable_name (const char *name, struct dwarf2_cu *cu)
13543 {
13544 static const char vptr[] = "_vptr";
13545 static const char vtable[] = "vtable";
13546
13547 /* Look for the C++ form of the vtable. */
13548 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
13549 return 1;
13550
13551 return 0;
13552 }
13553
13554 /* GCC outputs unnamed structures that are really pointers to member
13555 functions, with the ABI-specified layout. If TYPE describes
13556 such a structure, smash it into a member function type.
13557
13558 GCC shouldn't do this; it should just output pointer to member DIEs.
13559 This is GCC PR debug/28767. */
13560
13561 static void
13562 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
13563 {
13564 struct type *pfn_type, *self_type, *new_type;
13565
13566 /* Check for a structure with no name and two children. */
13567 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
13568 return;
13569
13570 /* Check for __pfn and __delta members. */
13571 if (TYPE_FIELD_NAME (type, 0) == NULL
13572 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
13573 || TYPE_FIELD_NAME (type, 1) == NULL
13574 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
13575 return;
13576
13577 /* Find the type of the method. */
13578 pfn_type = TYPE_FIELD_TYPE (type, 0);
13579 if (pfn_type == NULL
13580 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
13581 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
13582 return;
13583
13584 /* Look for the "this" argument. */
13585 pfn_type = TYPE_TARGET_TYPE (pfn_type);
13586 if (TYPE_NFIELDS (pfn_type) == 0
13587 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
13588 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
13589 return;
13590
13591 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
13592 new_type = alloc_type (objfile);
13593 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
13594 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
13595 TYPE_VARARGS (pfn_type));
13596 smash_to_methodptr_type (type, new_type);
13597 }
13598
13599 /* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
13600 (icc). */
13601
13602 static int
13603 producer_is_icc (struct dwarf2_cu *cu)
13604 {
13605 if (!cu->checked_producer)
13606 check_producer (cu);
13607
13608 return cu->producer_is_icc;
13609 }
13610
13611 /* Called when we find the DIE that starts a structure or union scope
13612 (definition) to create a type for the structure or union. Fill in
13613 the type's name and general properties; the members will not be
13614 processed until process_structure_scope. A symbol table entry for
13615 the type will also not be done until process_structure_scope (assuming
13616 the type has a name).
13617
13618 NOTE: we need to call these functions regardless of whether or not the
13619 DIE has a DW_AT_name attribute, since it might be an anonymous
13620 structure or union. This gets the type entered into our set of
13621 user defined types. */
13622
13623 static struct type *
13624 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
13625 {
13626 struct objfile *objfile = cu->objfile;
13627 struct type *type;
13628 struct attribute *attr;
13629 const char *name;
13630
13631 /* If the definition of this type lives in .debug_types, read that type.
13632 Don't follow DW_AT_specification though, that will take us back up
13633 the chain and we want to go down. */
13634 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
13635 if (attr)
13636 {
13637 type = get_DW_AT_signature_type (die, attr, cu);
13638
13639 /* The type's CU may not be the same as CU.
13640 Ensure TYPE is recorded with CU in die_type_hash. */
13641 return set_die_type (die, type, cu);
13642 }
13643
13644 type = alloc_type (objfile);
13645 INIT_CPLUS_SPECIFIC (type);
13646
13647 name = dwarf2_name (die, cu);
13648 if (name != NULL)
13649 {
13650 if (cu->language == language_cplus
13651 || cu->language == language_d
13652 || cu->language == language_rust)
13653 {
13654 const char *full_name = dwarf2_full_name (name, die, cu);
13655
13656 /* dwarf2_full_name might have already finished building the DIE's
13657 type. If so, there is no need to continue. */
13658 if (get_die_type (die, cu) != NULL)
13659 return get_die_type (die, cu);
13660
13661 TYPE_TAG_NAME (type) = full_name;
13662 if (die->tag == DW_TAG_structure_type
13663 || die->tag == DW_TAG_class_type)
13664 TYPE_NAME (type) = TYPE_TAG_NAME (type);
13665 }
13666 else
13667 {
13668 /* The name is already allocated along with this objfile, so
13669 we don't need to duplicate it for the type. */
13670 TYPE_TAG_NAME (type) = name;
13671 if (die->tag == DW_TAG_class_type)
13672 TYPE_NAME (type) = TYPE_TAG_NAME (type);
13673 }
13674 }
13675
13676 if (die->tag == DW_TAG_structure_type)
13677 {
13678 TYPE_CODE (type) = TYPE_CODE_STRUCT;
13679 }
13680 else if (die->tag == DW_TAG_union_type)
13681 {
13682 TYPE_CODE (type) = TYPE_CODE_UNION;
13683 }
13684 else
13685 {
13686 TYPE_CODE (type) = TYPE_CODE_STRUCT;
13687 }
13688
13689 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
13690 TYPE_DECLARED_CLASS (type) = 1;
13691
13692 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13693 if (attr)
13694 {
13695 if (attr_form_is_constant (attr))
13696 TYPE_LENGTH (type) = DW_UNSND (attr);
13697 else
13698 {
13699 /* For the moment, dynamic type sizes are not supported
13700 by GDB's struct type. The actual size is determined
13701 on-demand when resolving the type of a given object,
13702 so set the type's length to zero for now. Otherwise,
13703 we record an expression as the length, and that expression
13704 could lead to a very large value, which could eventually
13705 lead to us trying to allocate that much memory when creating
13706 a value of that type. */
13707 TYPE_LENGTH (type) = 0;
13708 }
13709 }
13710 else
13711 {
13712 TYPE_LENGTH (type) = 0;
13713 }
13714
13715 if (producer_is_icc (cu) && (TYPE_LENGTH (type) == 0))
13716 {
13717 /* ICC does not output the required DW_AT_declaration
13718 on incomplete types, but gives them a size of zero. */
13719 TYPE_STUB (type) = 1;
13720 }
13721 else
13722 TYPE_STUB_SUPPORTED (type) = 1;
13723
13724 if (die_is_declaration (die, cu))
13725 TYPE_STUB (type) = 1;
13726 else if (attr == NULL && die->child == NULL
13727 && producer_is_realview (cu->producer))
13728 /* RealView does not output the required DW_AT_declaration
13729 on incomplete types. */
13730 TYPE_STUB (type) = 1;
13731
13732 /* We need to add the type field to the die immediately so we don't
13733 infinitely recurse when dealing with pointers to the structure
13734 type within the structure itself. */
13735 set_die_type (die, type, cu);
13736
13737 /* set_die_type should be already done. */
13738 set_descriptive_type (type, die, cu);
13739
13740 return type;
13741 }
13742
13743 /* Finish creating a structure or union type, including filling in
13744 its members and creating a symbol for it. */
13745
13746 static void
13747 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
13748 {
13749 struct objfile *objfile = cu->objfile;
13750 struct die_info *child_die;
13751 struct type *type;
13752
13753 type = get_die_type (die, cu);
13754 if (type == NULL)
13755 type = read_structure_type (die, cu);
13756
13757 if (die->child != NULL && ! die_is_declaration (die, cu))
13758 {
13759 struct field_info fi;
13760 VEC (symbolp) *template_args = NULL;
13761 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
13762
13763 memset (&fi, 0, sizeof (struct field_info));
13764
13765 child_die = die->child;
13766
13767 while (child_die && child_die->tag)
13768 {
13769 if (child_die->tag == DW_TAG_member
13770 || child_die->tag == DW_TAG_variable)
13771 {
13772 /* NOTE: carlton/2002-11-05: A C++ static data member
13773 should be a DW_TAG_member that is a declaration, but
13774 all versions of G++ as of this writing (so through at
13775 least 3.2.1) incorrectly generate DW_TAG_variable
13776 tags for them instead. */
13777 dwarf2_add_field (&fi, child_die, cu);
13778 }
13779 else if (child_die->tag == DW_TAG_subprogram)
13780 {
13781 /* Rust doesn't have member functions in the C++ sense.
13782 However, it does emit ordinary functions as children
13783 of a struct DIE. */
13784 if (cu->language == language_rust)
13785 read_func_scope (child_die, cu);
13786 else
13787 {
13788 /* C++ member function. */
13789 dwarf2_add_member_fn (&fi, child_die, type, cu);
13790 }
13791 }
13792 else if (child_die->tag == DW_TAG_inheritance)
13793 {
13794 /* C++ base class field. */
13795 dwarf2_add_field (&fi, child_die, cu);
13796 }
13797 else if (child_die->tag == DW_TAG_typedef)
13798 dwarf2_add_typedef (&fi, child_die, cu);
13799 else if (child_die->tag == DW_TAG_template_type_param
13800 || child_die->tag == DW_TAG_template_value_param)
13801 {
13802 struct symbol *arg = new_symbol (child_die, NULL, cu);
13803
13804 if (arg != NULL)
13805 VEC_safe_push (symbolp, template_args, arg);
13806 }
13807
13808 child_die = sibling_die (child_die);
13809 }
13810
13811 /* Attach template arguments to type. */
13812 if (! VEC_empty (symbolp, template_args))
13813 {
13814 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13815 TYPE_N_TEMPLATE_ARGUMENTS (type)
13816 = VEC_length (symbolp, template_args);
13817 TYPE_TEMPLATE_ARGUMENTS (type)
13818 = XOBNEWVEC (&objfile->objfile_obstack,
13819 struct symbol *,
13820 TYPE_N_TEMPLATE_ARGUMENTS (type));
13821 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
13822 VEC_address (symbolp, template_args),
13823 (TYPE_N_TEMPLATE_ARGUMENTS (type)
13824 * sizeof (struct symbol *)));
13825 VEC_free (symbolp, template_args);
13826 }
13827
13828 /* Attach fields and member functions to the type. */
13829 if (fi.nfields)
13830 dwarf2_attach_fields_to_type (&fi, type, cu);
13831 if (fi.nfnfields)
13832 {
13833 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
13834
13835 /* Get the type which refers to the base class (possibly this
13836 class itself) which contains the vtable pointer for the current
13837 class from the DW_AT_containing_type attribute. This use of
13838 DW_AT_containing_type is a GNU extension. */
13839
13840 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
13841 {
13842 struct type *t = die_containing_type (die, cu);
13843
13844 set_type_vptr_basetype (type, t);
13845 if (type == t)
13846 {
13847 int i;
13848
13849 /* Our own class provides vtbl ptr. */
13850 for (i = TYPE_NFIELDS (t) - 1;
13851 i >= TYPE_N_BASECLASSES (t);
13852 --i)
13853 {
13854 const char *fieldname = TYPE_FIELD_NAME (t, i);
13855
13856 if (is_vtable_name (fieldname, cu))
13857 {
13858 set_type_vptr_fieldno (type, i);
13859 break;
13860 }
13861 }
13862
13863 /* Complain if virtual function table field not found. */
13864 if (i < TYPE_N_BASECLASSES (t))
13865 complaint (&symfile_complaints,
13866 _("virtual function table pointer "
13867 "not found when defining class '%s'"),
13868 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
13869 "");
13870 }
13871 else
13872 {
13873 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
13874 }
13875 }
13876 else if (cu->producer
13877 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
13878 {
13879 /* The IBM XLC compiler does not provide direct indication
13880 of the containing type, but the vtable pointer is
13881 always named __vfp. */
13882
13883 int i;
13884
13885 for (i = TYPE_NFIELDS (type) - 1;
13886 i >= TYPE_N_BASECLASSES (type);
13887 --i)
13888 {
13889 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
13890 {
13891 set_type_vptr_fieldno (type, i);
13892 set_type_vptr_basetype (type, type);
13893 break;
13894 }
13895 }
13896 }
13897 }
13898
13899 /* Copy fi.typedef_field_list linked list elements content into the
13900 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
13901 if (fi.typedef_field_list)
13902 {
13903 int i = fi.typedef_field_list_count;
13904
13905 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13906 TYPE_TYPEDEF_FIELD_ARRAY (type)
13907 = ((struct typedef_field *)
13908 TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i));
13909 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
13910
13911 /* Reverse the list order to keep the debug info elements order. */
13912 while (--i >= 0)
13913 {
13914 struct typedef_field *dest, *src;
13915
13916 dest = &TYPE_TYPEDEF_FIELD (type, i);
13917 src = &fi.typedef_field_list->field;
13918 fi.typedef_field_list = fi.typedef_field_list->next;
13919 *dest = *src;
13920 }
13921 }
13922
13923 do_cleanups (back_to);
13924 }
13925
13926 quirk_gcc_member_function_pointer (type, objfile);
13927
13928 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
13929 snapshots) has been known to create a die giving a declaration
13930 for a class that has, as a child, a die giving a definition for a
13931 nested class. So we have to process our children even if the
13932 current die is a declaration. Normally, of course, a declaration
13933 won't have any children at all. */
13934
13935 child_die = die->child;
13936
13937 while (child_die != NULL && child_die->tag)
13938 {
13939 if (child_die->tag == DW_TAG_member
13940 || child_die->tag == DW_TAG_variable
13941 || child_die->tag == DW_TAG_inheritance
13942 || child_die->tag == DW_TAG_template_value_param
13943 || child_die->tag == DW_TAG_template_type_param)
13944 {
13945 /* Do nothing. */
13946 }
13947 else
13948 process_die (child_die, cu);
13949
13950 child_die = sibling_die (child_die);
13951 }
13952
13953 /* Do not consider external references. According to the DWARF standard,
13954 these DIEs are identified by the fact that they have no byte_size
13955 attribute, and a declaration attribute. */
13956 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
13957 || !die_is_declaration (die, cu))
13958 new_symbol (die, type, cu);
13959 }
13960
13961 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
13962 update TYPE using some information only available in DIE's children. */
13963
13964 static void
13965 update_enumeration_type_from_children (struct die_info *die,
13966 struct type *type,
13967 struct dwarf2_cu *cu)
13968 {
13969 struct die_info *child_die;
13970 int unsigned_enum = 1;
13971 int flag_enum = 1;
13972 ULONGEST mask = 0;
13973
13974 auto_obstack obstack;
13975
13976 for (child_die = die->child;
13977 child_die != NULL && child_die->tag;
13978 child_die = sibling_die (child_die))
13979 {
13980 struct attribute *attr;
13981 LONGEST value;
13982 const gdb_byte *bytes;
13983 struct dwarf2_locexpr_baton *baton;
13984 const char *name;
13985
13986 if (child_die->tag != DW_TAG_enumerator)
13987 continue;
13988
13989 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
13990 if (attr == NULL)
13991 continue;
13992
13993 name = dwarf2_name (child_die, cu);
13994 if (name == NULL)
13995 name = "<anonymous enumerator>";
13996
13997 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
13998 &value, &bytes, &baton);
13999 if (value < 0)
14000 {
14001 unsigned_enum = 0;
14002 flag_enum = 0;
14003 }
14004 else if ((mask & value) != 0)
14005 flag_enum = 0;
14006 else
14007 mask |= value;
14008
14009 /* If we already know that the enum type is neither unsigned, nor
14010 a flag type, no need to look at the rest of the enumerates. */
14011 if (!unsigned_enum && !flag_enum)
14012 break;
14013 }
14014
14015 if (unsigned_enum)
14016 TYPE_UNSIGNED (type) = 1;
14017 if (flag_enum)
14018 TYPE_FLAG_ENUM (type) = 1;
14019 }
14020
14021 /* Given a DW_AT_enumeration_type die, set its type. We do not
14022 complete the type's fields yet, or create any symbols. */
14023
14024 static struct type *
14025 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
14026 {
14027 struct objfile *objfile = cu->objfile;
14028 struct type *type;
14029 struct attribute *attr;
14030 const char *name;
14031
14032 /* If the definition of this type lives in .debug_types, read that type.
14033 Don't follow DW_AT_specification though, that will take us back up
14034 the chain and we want to go down. */
14035 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
14036 if (attr)
14037 {
14038 type = get_DW_AT_signature_type (die, attr, cu);
14039
14040 /* The type's CU may not be the same as CU.
14041 Ensure TYPE is recorded with CU in die_type_hash. */
14042 return set_die_type (die, type, cu);
14043 }
14044
14045 type = alloc_type (objfile);
14046
14047 TYPE_CODE (type) = TYPE_CODE_ENUM;
14048 name = dwarf2_full_name (NULL, die, cu);
14049 if (name != NULL)
14050 TYPE_TAG_NAME (type) = name;
14051
14052 attr = dwarf2_attr (die, DW_AT_type, cu);
14053 if (attr != NULL)
14054 {
14055 struct type *underlying_type = die_type (die, cu);
14056
14057 TYPE_TARGET_TYPE (type) = underlying_type;
14058 }
14059
14060 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14061 if (attr)
14062 {
14063 TYPE_LENGTH (type) = DW_UNSND (attr);
14064 }
14065 else
14066 {
14067 TYPE_LENGTH (type) = 0;
14068 }
14069
14070 /* The enumeration DIE can be incomplete. In Ada, any type can be
14071 declared as private in the package spec, and then defined only
14072 inside the package body. Such types are known as Taft Amendment
14073 Types. When another package uses such a type, an incomplete DIE
14074 may be generated by the compiler. */
14075 if (die_is_declaration (die, cu))
14076 TYPE_STUB (type) = 1;
14077
14078 /* Finish the creation of this type by using the enum's children.
14079 We must call this even when the underlying type has been provided
14080 so that we can determine if we're looking at a "flag" enum. */
14081 update_enumeration_type_from_children (die, type, cu);
14082
14083 /* If this type has an underlying type that is not a stub, then we
14084 may use its attributes. We always use the "unsigned" attribute
14085 in this situation, because ordinarily we guess whether the type
14086 is unsigned -- but the guess can be wrong and the underlying type
14087 can tell us the reality. However, we defer to a local size
14088 attribute if one exists, because this lets the compiler override
14089 the underlying type if needed. */
14090 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
14091 {
14092 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
14093 if (TYPE_LENGTH (type) == 0)
14094 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
14095 }
14096
14097 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
14098
14099 return set_die_type (die, type, cu);
14100 }
14101
14102 /* Given a pointer to a die which begins an enumeration, process all
14103 the dies that define the members of the enumeration, and create the
14104 symbol for the enumeration type.
14105
14106 NOTE: We reverse the order of the element list. */
14107
14108 static void
14109 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
14110 {
14111 struct type *this_type;
14112
14113 this_type = get_die_type (die, cu);
14114 if (this_type == NULL)
14115 this_type = read_enumeration_type (die, cu);
14116
14117 if (die->child != NULL)
14118 {
14119 struct die_info *child_die;
14120 struct symbol *sym;
14121 struct field *fields = NULL;
14122 int num_fields = 0;
14123 const char *name;
14124
14125 child_die = die->child;
14126 while (child_die && child_die->tag)
14127 {
14128 if (child_die->tag != DW_TAG_enumerator)
14129 {
14130 process_die (child_die, cu);
14131 }
14132 else
14133 {
14134 name = dwarf2_name (child_die, cu);
14135 if (name)
14136 {
14137 sym = new_symbol (child_die, this_type, cu);
14138
14139 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
14140 {
14141 fields = (struct field *)
14142 xrealloc (fields,
14143 (num_fields + DW_FIELD_ALLOC_CHUNK)
14144 * sizeof (struct field));
14145 }
14146
14147 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
14148 FIELD_TYPE (fields[num_fields]) = NULL;
14149 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
14150 FIELD_BITSIZE (fields[num_fields]) = 0;
14151
14152 num_fields++;
14153 }
14154 }
14155
14156 child_die = sibling_die (child_die);
14157 }
14158
14159 if (num_fields)
14160 {
14161 TYPE_NFIELDS (this_type) = num_fields;
14162 TYPE_FIELDS (this_type) = (struct field *)
14163 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
14164 memcpy (TYPE_FIELDS (this_type), fields,
14165 sizeof (struct field) * num_fields);
14166 xfree (fields);
14167 }
14168 }
14169
14170 /* If we are reading an enum from a .debug_types unit, and the enum
14171 is a declaration, and the enum is not the signatured type in the
14172 unit, then we do not want to add a symbol for it. Adding a
14173 symbol would in some cases obscure the true definition of the
14174 enum, giving users an incomplete type when the definition is
14175 actually available. Note that we do not want to do this for all
14176 enums which are just declarations, because C++0x allows forward
14177 enum declarations. */
14178 if (cu->per_cu->is_debug_types
14179 && die_is_declaration (die, cu))
14180 {
14181 struct signatured_type *sig_type;
14182
14183 sig_type = (struct signatured_type *) cu->per_cu;
14184 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
14185 if (sig_type->type_offset_in_section != die->sect_off)
14186 return;
14187 }
14188
14189 new_symbol (die, this_type, cu);
14190 }
14191
14192 /* Extract all information from a DW_TAG_array_type DIE and put it in
14193 the DIE's type field. For now, this only handles one dimensional
14194 arrays. */
14195
14196 static struct type *
14197 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
14198 {
14199 struct objfile *objfile = cu->objfile;
14200 struct die_info *child_die;
14201 struct type *type;
14202 struct type *element_type, *range_type, *index_type;
14203 struct type **range_types = NULL;
14204 struct attribute *attr;
14205 int ndim = 0;
14206 struct cleanup *back_to;
14207 const char *name;
14208 unsigned int bit_stride = 0;
14209
14210 element_type = die_type (die, cu);
14211
14212 /* The die_type call above may have already set the type for this DIE. */
14213 type = get_die_type (die, cu);
14214 if (type)
14215 return type;
14216
14217 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
14218 if (attr != NULL)
14219 bit_stride = DW_UNSND (attr) * 8;
14220
14221 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
14222 if (attr != NULL)
14223 bit_stride = DW_UNSND (attr);
14224
14225 /* Irix 6.2 native cc creates array types without children for
14226 arrays with unspecified length. */
14227 if (die->child == NULL)
14228 {
14229 index_type = objfile_type (objfile)->builtin_int;
14230 range_type = create_static_range_type (NULL, index_type, 0, -1);
14231 type = create_array_type_with_stride (NULL, element_type, range_type,
14232 bit_stride);
14233 return set_die_type (die, type, cu);
14234 }
14235
14236 back_to = make_cleanup (null_cleanup, NULL);
14237 child_die = die->child;
14238 while (child_die && child_die->tag)
14239 {
14240 if (child_die->tag == DW_TAG_subrange_type)
14241 {
14242 struct type *child_type = read_type_die (child_die, cu);
14243
14244 if (child_type != NULL)
14245 {
14246 /* The range type was succesfully read. Save it for the
14247 array type creation. */
14248 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
14249 {
14250 range_types = (struct type **)
14251 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
14252 * sizeof (struct type *));
14253 if (ndim == 0)
14254 make_cleanup (free_current_contents, &range_types);
14255 }
14256 range_types[ndim++] = child_type;
14257 }
14258 }
14259 child_die = sibling_die (child_die);
14260 }
14261
14262 /* Dwarf2 dimensions are output from left to right, create the
14263 necessary array types in backwards order. */
14264
14265 type = element_type;
14266
14267 if (read_array_order (die, cu) == DW_ORD_col_major)
14268 {
14269 int i = 0;
14270
14271 while (i < ndim)
14272 type = create_array_type_with_stride (NULL, type, range_types[i++],
14273 bit_stride);
14274 }
14275 else
14276 {
14277 while (ndim-- > 0)
14278 type = create_array_type_with_stride (NULL, type, range_types[ndim],
14279 bit_stride);
14280 }
14281
14282 /* Understand Dwarf2 support for vector types (like they occur on
14283 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
14284 array type. This is not part of the Dwarf2/3 standard yet, but a
14285 custom vendor extension. The main difference between a regular
14286 array and the vector variant is that vectors are passed by value
14287 to functions. */
14288 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
14289 if (attr)
14290 make_vector_type (type);
14291
14292 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
14293 implementation may choose to implement triple vectors using this
14294 attribute. */
14295 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14296 if (attr)
14297 {
14298 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
14299 TYPE_LENGTH (type) = DW_UNSND (attr);
14300 else
14301 complaint (&symfile_complaints,
14302 _("DW_AT_byte_size for array type smaller "
14303 "than the total size of elements"));
14304 }
14305
14306 name = dwarf2_name (die, cu);
14307 if (name)
14308 TYPE_NAME (type) = name;
14309
14310 /* Install the type in the die. */
14311 set_die_type (die, type, cu);
14312
14313 /* set_die_type should be already done. */
14314 set_descriptive_type (type, die, cu);
14315
14316 do_cleanups (back_to);
14317
14318 return type;
14319 }
14320
14321 static enum dwarf_array_dim_ordering
14322 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
14323 {
14324 struct attribute *attr;
14325
14326 attr = dwarf2_attr (die, DW_AT_ordering, cu);
14327
14328 if (attr)
14329 return (enum dwarf_array_dim_ordering) DW_SND (attr);
14330
14331 /* GNU F77 is a special case, as at 08/2004 array type info is the
14332 opposite order to the dwarf2 specification, but data is still
14333 laid out as per normal fortran.
14334
14335 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
14336 version checking. */
14337
14338 if (cu->language == language_fortran
14339 && cu->producer && strstr (cu->producer, "GNU F77"))
14340 {
14341 return DW_ORD_row_major;
14342 }
14343
14344 switch (cu->language_defn->la_array_ordering)
14345 {
14346 case array_column_major:
14347 return DW_ORD_col_major;
14348 case array_row_major:
14349 default:
14350 return DW_ORD_row_major;
14351 };
14352 }
14353
14354 /* Extract all information from a DW_TAG_set_type DIE and put it in
14355 the DIE's type field. */
14356
14357 static struct type *
14358 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
14359 {
14360 struct type *domain_type, *set_type;
14361 struct attribute *attr;
14362
14363 domain_type = die_type (die, cu);
14364
14365 /* The die_type call above may have already set the type for this DIE. */
14366 set_type = get_die_type (die, cu);
14367 if (set_type)
14368 return set_type;
14369
14370 set_type = create_set_type (NULL, domain_type);
14371
14372 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14373 if (attr)
14374 TYPE_LENGTH (set_type) = DW_UNSND (attr);
14375
14376 return set_die_type (die, set_type, cu);
14377 }
14378
14379 /* A helper for read_common_block that creates a locexpr baton.
14380 SYM is the symbol which we are marking as computed.
14381 COMMON_DIE is the DIE for the common block.
14382 COMMON_LOC is the location expression attribute for the common
14383 block itself.
14384 MEMBER_LOC is the location expression attribute for the particular
14385 member of the common block that we are processing.
14386 CU is the CU from which the above come. */
14387
14388 static void
14389 mark_common_block_symbol_computed (struct symbol *sym,
14390 struct die_info *common_die,
14391 struct attribute *common_loc,
14392 struct attribute *member_loc,
14393 struct dwarf2_cu *cu)
14394 {
14395 struct objfile *objfile = dwarf2_per_objfile->objfile;
14396 struct dwarf2_locexpr_baton *baton;
14397 gdb_byte *ptr;
14398 unsigned int cu_off;
14399 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
14400 LONGEST offset = 0;
14401
14402 gdb_assert (common_loc && member_loc);
14403 gdb_assert (attr_form_is_block (common_loc));
14404 gdb_assert (attr_form_is_block (member_loc)
14405 || attr_form_is_constant (member_loc));
14406
14407 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14408 baton->per_cu = cu->per_cu;
14409 gdb_assert (baton->per_cu);
14410
14411 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
14412
14413 if (attr_form_is_constant (member_loc))
14414 {
14415 offset = dwarf2_get_attr_constant_value (member_loc, 0);
14416 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
14417 }
14418 else
14419 baton->size += DW_BLOCK (member_loc)->size;
14420
14421 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
14422 baton->data = ptr;
14423
14424 *ptr++ = DW_OP_call4;
14425 cu_off = common_die->sect_off - cu->per_cu->sect_off;
14426 store_unsigned_integer (ptr, 4, byte_order, cu_off);
14427 ptr += 4;
14428
14429 if (attr_form_is_constant (member_loc))
14430 {
14431 *ptr++ = DW_OP_addr;
14432 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
14433 ptr += cu->header.addr_size;
14434 }
14435 else
14436 {
14437 /* We have to copy the data here, because DW_OP_call4 will only
14438 use a DW_AT_location attribute. */
14439 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
14440 ptr += DW_BLOCK (member_loc)->size;
14441 }
14442
14443 *ptr++ = DW_OP_plus;
14444 gdb_assert (ptr - baton->data == baton->size);
14445
14446 SYMBOL_LOCATION_BATON (sym) = baton;
14447 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
14448 }
14449
14450 /* Create appropriate locally-scoped variables for all the
14451 DW_TAG_common_block entries. Also create a struct common_block
14452 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
14453 is used to sepate the common blocks name namespace from regular
14454 variable names. */
14455
14456 static void
14457 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
14458 {
14459 struct attribute *attr;
14460
14461 attr = dwarf2_attr (die, DW_AT_location, cu);
14462 if (attr)
14463 {
14464 /* Support the .debug_loc offsets. */
14465 if (attr_form_is_block (attr))
14466 {
14467 /* Ok. */
14468 }
14469 else if (attr_form_is_section_offset (attr))
14470 {
14471 dwarf2_complex_location_expr_complaint ();
14472 attr = NULL;
14473 }
14474 else
14475 {
14476 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
14477 "common block member");
14478 attr = NULL;
14479 }
14480 }
14481
14482 if (die->child != NULL)
14483 {
14484 struct objfile *objfile = cu->objfile;
14485 struct die_info *child_die;
14486 size_t n_entries = 0, size;
14487 struct common_block *common_block;
14488 struct symbol *sym;
14489
14490 for (child_die = die->child;
14491 child_die && child_die->tag;
14492 child_die = sibling_die (child_die))
14493 ++n_entries;
14494
14495 size = (sizeof (struct common_block)
14496 + (n_entries - 1) * sizeof (struct symbol *));
14497 common_block
14498 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
14499 size);
14500 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
14501 common_block->n_entries = 0;
14502
14503 for (child_die = die->child;
14504 child_die && child_die->tag;
14505 child_die = sibling_die (child_die))
14506 {
14507 /* Create the symbol in the DW_TAG_common_block block in the current
14508 symbol scope. */
14509 sym = new_symbol (child_die, NULL, cu);
14510 if (sym != NULL)
14511 {
14512 struct attribute *member_loc;
14513
14514 common_block->contents[common_block->n_entries++] = sym;
14515
14516 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
14517 cu);
14518 if (member_loc)
14519 {
14520 /* GDB has handled this for a long time, but it is
14521 not specified by DWARF. It seems to have been
14522 emitted by gfortran at least as recently as:
14523 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
14524 complaint (&symfile_complaints,
14525 _("Variable in common block has "
14526 "DW_AT_data_member_location "
14527 "- DIE at 0x%x [in module %s]"),
14528 to_underlying (child_die->sect_off),
14529 objfile_name (cu->objfile));
14530
14531 if (attr_form_is_section_offset (member_loc))
14532 dwarf2_complex_location_expr_complaint ();
14533 else if (attr_form_is_constant (member_loc)
14534 || attr_form_is_block (member_loc))
14535 {
14536 if (attr)
14537 mark_common_block_symbol_computed (sym, die, attr,
14538 member_loc, cu);
14539 }
14540 else
14541 dwarf2_complex_location_expr_complaint ();
14542 }
14543 }
14544 }
14545
14546 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
14547 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
14548 }
14549 }
14550
14551 /* Create a type for a C++ namespace. */
14552
14553 static struct type *
14554 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
14555 {
14556 struct objfile *objfile = cu->objfile;
14557 const char *previous_prefix, *name;
14558 int is_anonymous;
14559 struct type *type;
14560
14561 /* For extensions, reuse the type of the original namespace. */
14562 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
14563 {
14564 struct die_info *ext_die;
14565 struct dwarf2_cu *ext_cu = cu;
14566
14567 ext_die = dwarf2_extension (die, &ext_cu);
14568 type = read_type_die (ext_die, ext_cu);
14569
14570 /* EXT_CU may not be the same as CU.
14571 Ensure TYPE is recorded with CU in die_type_hash. */
14572 return set_die_type (die, type, cu);
14573 }
14574
14575 name = namespace_name (die, &is_anonymous, cu);
14576
14577 /* Now build the name of the current namespace. */
14578
14579 previous_prefix = determine_prefix (die, cu);
14580 if (previous_prefix[0] != '\0')
14581 name = typename_concat (&objfile->objfile_obstack,
14582 previous_prefix, name, 0, cu);
14583
14584 /* Create the type. */
14585 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
14586 TYPE_TAG_NAME (type) = TYPE_NAME (type);
14587
14588 return set_die_type (die, type, cu);
14589 }
14590
14591 /* Read a namespace scope. */
14592
14593 static void
14594 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
14595 {
14596 struct objfile *objfile = cu->objfile;
14597 int is_anonymous;
14598
14599 /* Add a symbol associated to this if we haven't seen the namespace
14600 before. Also, add a using directive if it's an anonymous
14601 namespace. */
14602
14603 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
14604 {
14605 struct type *type;
14606
14607 type = read_type_die (die, cu);
14608 new_symbol (die, type, cu);
14609
14610 namespace_name (die, &is_anonymous, cu);
14611 if (is_anonymous)
14612 {
14613 const char *previous_prefix = determine_prefix (die, cu);
14614
14615 std::vector<const char *> excludes;
14616 add_using_directive (using_directives (cu->language),
14617 previous_prefix, TYPE_NAME (type), NULL,
14618 NULL, excludes, 0, &objfile->objfile_obstack);
14619 }
14620 }
14621
14622 if (die->child != NULL)
14623 {
14624 struct die_info *child_die = die->child;
14625
14626 while (child_die && child_die->tag)
14627 {
14628 process_die (child_die, cu);
14629 child_die = sibling_die (child_die);
14630 }
14631 }
14632 }
14633
14634 /* Read a Fortran module as type. This DIE can be only a declaration used for
14635 imported module. Still we need that type as local Fortran "use ... only"
14636 declaration imports depend on the created type in determine_prefix. */
14637
14638 static struct type *
14639 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
14640 {
14641 struct objfile *objfile = cu->objfile;
14642 const char *module_name;
14643 struct type *type;
14644
14645 module_name = dwarf2_name (die, cu);
14646 if (!module_name)
14647 complaint (&symfile_complaints,
14648 _("DW_TAG_module has no name, offset 0x%x"),
14649 to_underlying (die->sect_off));
14650 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
14651
14652 /* determine_prefix uses TYPE_TAG_NAME. */
14653 TYPE_TAG_NAME (type) = TYPE_NAME (type);
14654
14655 return set_die_type (die, type, cu);
14656 }
14657
14658 /* Read a Fortran module. */
14659
14660 static void
14661 read_module (struct die_info *die, struct dwarf2_cu *cu)
14662 {
14663 struct die_info *child_die = die->child;
14664 struct type *type;
14665
14666 type = read_type_die (die, cu);
14667 new_symbol (die, type, cu);
14668
14669 while (child_die && child_die->tag)
14670 {
14671 process_die (child_die, cu);
14672 child_die = sibling_die (child_die);
14673 }
14674 }
14675
14676 /* Return the name of the namespace represented by DIE. Set
14677 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
14678 namespace. */
14679
14680 static const char *
14681 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
14682 {
14683 struct die_info *current_die;
14684 const char *name = NULL;
14685
14686 /* Loop through the extensions until we find a name. */
14687
14688 for (current_die = die;
14689 current_die != NULL;
14690 current_die = dwarf2_extension (die, &cu))
14691 {
14692 /* We don't use dwarf2_name here so that we can detect the absence
14693 of a name -> anonymous namespace. */
14694 name = dwarf2_string_attr (die, DW_AT_name, cu);
14695
14696 if (name != NULL)
14697 break;
14698 }
14699
14700 /* Is it an anonymous namespace? */
14701
14702 *is_anonymous = (name == NULL);
14703 if (*is_anonymous)
14704 name = CP_ANONYMOUS_NAMESPACE_STR;
14705
14706 return name;
14707 }
14708
14709 /* Extract all information from a DW_TAG_pointer_type DIE and add to
14710 the user defined type vector. */
14711
14712 static struct type *
14713 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
14714 {
14715 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
14716 struct comp_unit_head *cu_header = &cu->header;
14717 struct type *type;
14718 struct attribute *attr_byte_size;
14719 struct attribute *attr_address_class;
14720 int byte_size, addr_class;
14721 struct type *target_type;
14722
14723 target_type = die_type (die, cu);
14724
14725 /* The die_type call above may have already set the type for this DIE. */
14726 type = get_die_type (die, cu);
14727 if (type)
14728 return type;
14729
14730 type = lookup_pointer_type (target_type);
14731
14732 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
14733 if (attr_byte_size)
14734 byte_size = DW_UNSND (attr_byte_size);
14735 else
14736 byte_size = cu_header->addr_size;
14737
14738 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
14739 if (attr_address_class)
14740 addr_class = DW_UNSND (attr_address_class);
14741 else
14742 addr_class = DW_ADDR_none;
14743
14744 /* If the pointer size or address class is different than the
14745 default, create a type variant marked as such and set the
14746 length accordingly. */
14747 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
14748 {
14749 if (gdbarch_address_class_type_flags_p (gdbarch))
14750 {
14751 int type_flags;
14752
14753 type_flags = gdbarch_address_class_type_flags
14754 (gdbarch, byte_size, addr_class);
14755 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
14756 == 0);
14757 type = make_type_with_address_space (type, type_flags);
14758 }
14759 else if (TYPE_LENGTH (type) != byte_size)
14760 {
14761 complaint (&symfile_complaints,
14762 _("invalid pointer size %d"), byte_size);
14763 }
14764 else
14765 {
14766 /* Should we also complain about unhandled address classes? */
14767 }
14768 }
14769
14770 TYPE_LENGTH (type) = byte_size;
14771 return set_die_type (die, type, cu);
14772 }
14773
14774 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
14775 the user defined type vector. */
14776
14777 static struct type *
14778 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
14779 {
14780 struct type *type;
14781 struct type *to_type;
14782 struct type *domain;
14783
14784 to_type = die_type (die, cu);
14785 domain = die_containing_type (die, cu);
14786
14787 /* The calls above may have already set the type for this DIE. */
14788 type = get_die_type (die, cu);
14789 if (type)
14790 return type;
14791
14792 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
14793 type = lookup_methodptr_type (to_type);
14794 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
14795 {
14796 struct type *new_type = alloc_type (cu->objfile);
14797
14798 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
14799 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
14800 TYPE_VARARGS (to_type));
14801 type = lookup_methodptr_type (new_type);
14802 }
14803 else
14804 type = lookup_memberptr_type (to_type, domain);
14805
14806 return set_die_type (die, type, cu);
14807 }
14808
14809 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
14810 the user defined type vector. */
14811
14812 static struct type *
14813 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
14814 enum type_code refcode)
14815 {
14816 struct comp_unit_head *cu_header = &cu->header;
14817 struct type *type, *target_type;
14818 struct attribute *attr;
14819
14820 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
14821
14822 target_type = die_type (die, cu);
14823
14824 /* The die_type call above may have already set the type for this DIE. */
14825 type = get_die_type (die, cu);
14826 if (type)
14827 return type;
14828
14829 type = lookup_reference_type (target_type, refcode);
14830 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14831 if (attr)
14832 {
14833 TYPE_LENGTH (type) = DW_UNSND (attr);
14834 }
14835 else
14836 {
14837 TYPE_LENGTH (type) = cu_header->addr_size;
14838 }
14839 return set_die_type (die, type, cu);
14840 }
14841
14842 /* Add the given cv-qualifiers to the element type of the array. GCC
14843 outputs DWARF type qualifiers that apply to an array, not the
14844 element type. But GDB relies on the array element type to carry
14845 the cv-qualifiers. This mimics section 6.7.3 of the C99
14846 specification. */
14847
14848 static struct type *
14849 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
14850 struct type *base_type, int cnst, int voltl)
14851 {
14852 struct type *el_type, *inner_array;
14853
14854 base_type = copy_type (base_type);
14855 inner_array = base_type;
14856
14857 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
14858 {
14859 TYPE_TARGET_TYPE (inner_array) =
14860 copy_type (TYPE_TARGET_TYPE (inner_array));
14861 inner_array = TYPE_TARGET_TYPE (inner_array);
14862 }
14863
14864 el_type = TYPE_TARGET_TYPE (inner_array);
14865 cnst |= TYPE_CONST (el_type);
14866 voltl |= TYPE_VOLATILE (el_type);
14867 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
14868
14869 return set_die_type (die, base_type, cu);
14870 }
14871
14872 static struct type *
14873 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
14874 {
14875 struct type *base_type, *cv_type;
14876
14877 base_type = die_type (die, cu);
14878
14879 /* The die_type call above may have already set the type for this DIE. */
14880 cv_type = get_die_type (die, cu);
14881 if (cv_type)
14882 return cv_type;
14883
14884 /* In case the const qualifier is applied to an array type, the element type
14885 is so qualified, not the array type (section 6.7.3 of C99). */
14886 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
14887 return add_array_cv_type (die, cu, base_type, 1, 0);
14888
14889 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
14890 return set_die_type (die, cv_type, cu);
14891 }
14892
14893 static struct type *
14894 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
14895 {
14896 struct type *base_type, *cv_type;
14897
14898 base_type = die_type (die, cu);
14899
14900 /* The die_type call above may have already set the type for this DIE. */
14901 cv_type = get_die_type (die, cu);
14902 if (cv_type)
14903 return cv_type;
14904
14905 /* In case the volatile qualifier is applied to an array type, the
14906 element type is so qualified, not the array type (section 6.7.3
14907 of C99). */
14908 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
14909 return add_array_cv_type (die, cu, base_type, 0, 1);
14910
14911 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
14912 return set_die_type (die, cv_type, cu);
14913 }
14914
14915 /* Handle DW_TAG_restrict_type. */
14916
14917 static struct type *
14918 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
14919 {
14920 struct type *base_type, *cv_type;
14921
14922 base_type = die_type (die, cu);
14923
14924 /* The die_type call above may have already set the type for this DIE. */
14925 cv_type = get_die_type (die, cu);
14926 if (cv_type)
14927 return cv_type;
14928
14929 cv_type = make_restrict_type (base_type);
14930 return set_die_type (die, cv_type, cu);
14931 }
14932
14933 /* Handle DW_TAG_atomic_type. */
14934
14935 static struct type *
14936 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
14937 {
14938 struct type *base_type, *cv_type;
14939
14940 base_type = die_type (die, cu);
14941
14942 /* The die_type call above may have already set the type for this DIE. */
14943 cv_type = get_die_type (die, cu);
14944 if (cv_type)
14945 return cv_type;
14946
14947 cv_type = make_atomic_type (base_type);
14948 return set_die_type (die, cv_type, cu);
14949 }
14950
14951 /* Extract all information from a DW_TAG_string_type DIE and add to
14952 the user defined type vector. It isn't really a user defined type,
14953 but it behaves like one, with other DIE's using an AT_user_def_type
14954 attribute to reference it. */
14955
14956 static struct type *
14957 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
14958 {
14959 struct objfile *objfile = cu->objfile;
14960 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14961 struct type *type, *range_type, *index_type, *char_type;
14962 struct attribute *attr;
14963 unsigned int length;
14964
14965 attr = dwarf2_attr (die, DW_AT_string_length, cu);
14966 if (attr)
14967 {
14968 length = DW_UNSND (attr);
14969 }
14970 else
14971 {
14972 /* Check for the DW_AT_byte_size attribute. */
14973 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14974 if (attr)
14975 {
14976 length = DW_UNSND (attr);
14977 }
14978 else
14979 {
14980 length = 1;
14981 }
14982 }
14983
14984 index_type = objfile_type (objfile)->builtin_int;
14985 range_type = create_static_range_type (NULL, index_type, 1, length);
14986 char_type = language_string_char_type (cu->language_defn, gdbarch);
14987 type = create_string_type (NULL, char_type, range_type);
14988
14989 return set_die_type (die, type, cu);
14990 }
14991
14992 /* Assuming that DIE corresponds to a function, returns nonzero
14993 if the function is prototyped. */
14994
14995 static int
14996 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
14997 {
14998 struct attribute *attr;
14999
15000 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
15001 if (attr && (DW_UNSND (attr) != 0))
15002 return 1;
15003
15004 /* The DWARF standard implies that the DW_AT_prototyped attribute
15005 is only meaninful for C, but the concept also extends to other
15006 languages that allow unprototyped functions (Eg: Objective C).
15007 For all other languages, assume that functions are always
15008 prototyped. */
15009 if (cu->language != language_c
15010 && cu->language != language_objc
15011 && cu->language != language_opencl)
15012 return 1;
15013
15014 /* RealView does not emit DW_AT_prototyped. We can not distinguish
15015 prototyped and unprototyped functions; default to prototyped,
15016 since that is more common in modern code (and RealView warns
15017 about unprototyped functions). */
15018 if (producer_is_realview (cu->producer))
15019 return 1;
15020
15021 return 0;
15022 }
15023
15024 /* Handle DIES due to C code like:
15025
15026 struct foo
15027 {
15028 int (*funcp)(int a, long l);
15029 int b;
15030 };
15031
15032 ('funcp' generates a DW_TAG_subroutine_type DIE). */
15033
15034 static struct type *
15035 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
15036 {
15037 struct objfile *objfile = cu->objfile;
15038 struct type *type; /* Type that this function returns. */
15039 struct type *ftype; /* Function that returns above type. */
15040 struct attribute *attr;
15041
15042 type = die_type (die, cu);
15043
15044 /* The die_type call above may have already set the type for this DIE. */
15045 ftype = get_die_type (die, cu);
15046 if (ftype)
15047 return ftype;
15048
15049 ftype = lookup_function_type (type);
15050
15051 if (prototyped_function_p (die, cu))
15052 TYPE_PROTOTYPED (ftype) = 1;
15053
15054 /* Store the calling convention in the type if it's available in
15055 the subroutine die. Otherwise set the calling convention to
15056 the default value DW_CC_normal. */
15057 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15058 if (attr)
15059 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
15060 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
15061 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
15062 else
15063 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
15064
15065 /* Record whether the function returns normally to its caller or not
15066 if the DWARF producer set that information. */
15067 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
15068 if (attr && (DW_UNSND (attr) != 0))
15069 TYPE_NO_RETURN (ftype) = 1;
15070
15071 /* We need to add the subroutine type to the die immediately so
15072 we don't infinitely recurse when dealing with parameters
15073 declared as the same subroutine type. */
15074 set_die_type (die, ftype, cu);
15075
15076 if (die->child != NULL)
15077 {
15078 struct type *void_type = objfile_type (objfile)->builtin_void;
15079 struct die_info *child_die;
15080 int nparams, iparams;
15081
15082 /* Count the number of parameters.
15083 FIXME: GDB currently ignores vararg functions, but knows about
15084 vararg member functions. */
15085 nparams = 0;
15086 child_die = die->child;
15087 while (child_die && child_die->tag)
15088 {
15089 if (child_die->tag == DW_TAG_formal_parameter)
15090 nparams++;
15091 else if (child_die->tag == DW_TAG_unspecified_parameters)
15092 TYPE_VARARGS (ftype) = 1;
15093 child_die = sibling_die (child_die);
15094 }
15095
15096 /* Allocate storage for parameters and fill them in. */
15097 TYPE_NFIELDS (ftype) = nparams;
15098 TYPE_FIELDS (ftype) = (struct field *)
15099 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
15100
15101 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
15102 even if we error out during the parameters reading below. */
15103 for (iparams = 0; iparams < nparams; iparams++)
15104 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
15105
15106 iparams = 0;
15107 child_die = die->child;
15108 while (child_die && child_die->tag)
15109 {
15110 if (child_die->tag == DW_TAG_formal_parameter)
15111 {
15112 struct type *arg_type;
15113
15114 /* DWARF version 2 has no clean way to discern C++
15115 static and non-static member functions. G++ helps
15116 GDB by marking the first parameter for non-static
15117 member functions (which is the this pointer) as
15118 artificial. We pass this information to
15119 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
15120
15121 DWARF version 3 added DW_AT_object_pointer, which GCC
15122 4.5 does not yet generate. */
15123 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
15124 if (attr)
15125 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
15126 else
15127 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
15128 arg_type = die_type (child_die, cu);
15129
15130 /* RealView does not mark THIS as const, which the testsuite
15131 expects. GCC marks THIS as const in method definitions,
15132 but not in the class specifications (GCC PR 43053). */
15133 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
15134 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
15135 {
15136 int is_this = 0;
15137 struct dwarf2_cu *arg_cu = cu;
15138 const char *name = dwarf2_name (child_die, cu);
15139
15140 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
15141 if (attr)
15142 {
15143 /* If the compiler emits this, use it. */
15144 if (follow_die_ref (die, attr, &arg_cu) == child_die)
15145 is_this = 1;
15146 }
15147 else if (name && strcmp (name, "this") == 0)
15148 /* Function definitions will have the argument names. */
15149 is_this = 1;
15150 else if (name == NULL && iparams == 0)
15151 /* Declarations may not have the names, so like
15152 elsewhere in GDB, assume an artificial first
15153 argument is "this". */
15154 is_this = 1;
15155
15156 if (is_this)
15157 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
15158 arg_type, 0);
15159 }
15160
15161 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
15162 iparams++;
15163 }
15164 child_die = sibling_die (child_die);
15165 }
15166 }
15167
15168 return ftype;
15169 }
15170
15171 static struct type *
15172 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
15173 {
15174 struct objfile *objfile = cu->objfile;
15175 const char *name = NULL;
15176 struct type *this_type, *target_type;
15177
15178 name = dwarf2_full_name (NULL, die, cu);
15179 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
15180 TYPE_TARGET_STUB (this_type) = 1;
15181 set_die_type (die, this_type, cu);
15182 target_type = die_type (die, cu);
15183 if (target_type != this_type)
15184 TYPE_TARGET_TYPE (this_type) = target_type;
15185 else
15186 {
15187 /* Self-referential typedefs are, it seems, not allowed by the DWARF
15188 spec and cause infinite loops in GDB. */
15189 complaint (&symfile_complaints,
15190 _("Self-referential DW_TAG_typedef "
15191 "- DIE at 0x%x [in module %s]"),
15192 to_underlying (die->sect_off), objfile_name (objfile));
15193 TYPE_TARGET_TYPE (this_type) = NULL;
15194 }
15195 return this_type;
15196 }
15197
15198 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
15199 (which may be different from NAME) to the architecture back-end to allow
15200 it to guess the correct format if necessary. */
15201
15202 static struct type *
15203 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
15204 const char *name_hint)
15205 {
15206 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15207 const struct floatformat **format;
15208 struct type *type;
15209
15210 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
15211 if (format)
15212 type = init_float_type (objfile, bits, name, format);
15213 else
15214 type = init_type (objfile, TYPE_CODE_ERROR, bits / TARGET_CHAR_BIT, name);
15215
15216 return type;
15217 }
15218
15219 /* Find a representation of a given base type and install
15220 it in the TYPE field of the die. */
15221
15222 static struct type *
15223 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
15224 {
15225 struct objfile *objfile = cu->objfile;
15226 struct type *type;
15227 struct attribute *attr;
15228 int encoding = 0, bits = 0;
15229 const char *name;
15230
15231 attr = dwarf2_attr (die, DW_AT_encoding, cu);
15232 if (attr)
15233 {
15234 encoding = DW_UNSND (attr);
15235 }
15236 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15237 if (attr)
15238 {
15239 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
15240 }
15241 name = dwarf2_name (die, cu);
15242 if (!name)
15243 {
15244 complaint (&symfile_complaints,
15245 _("DW_AT_name missing from DW_TAG_base_type"));
15246 }
15247
15248 switch (encoding)
15249 {
15250 case DW_ATE_address:
15251 /* Turn DW_ATE_address into a void * pointer. */
15252 type = init_type (objfile, TYPE_CODE_VOID, 1, NULL);
15253 type = init_pointer_type (objfile, bits, name, type);
15254 break;
15255 case DW_ATE_boolean:
15256 type = init_boolean_type (objfile, bits, 1, name);
15257 break;
15258 case DW_ATE_complex_float:
15259 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
15260 type = init_complex_type (objfile, name, type);
15261 break;
15262 case DW_ATE_decimal_float:
15263 type = init_decfloat_type (objfile, bits, name);
15264 break;
15265 case DW_ATE_float:
15266 type = dwarf2_init_float_type (objfile, bits, name, name);
15267 break;
15268 case DW_ATE_signed:
15269 type = init_integer_type (objfile, bits, 0, name);
15270 break;
15271 case DW_ATE_unsigned:
15272 if (cu->language == language_fortran
15273 && name
15274 && startswith (name, "character("))
15275 type = init_character_type (objfile, bits, 1, name);
15276 else
15277 type = init_integer_type (objfile, bits, 1, name);
15278 break;
15279 case DW_ATE_signed_char:
15280 if (cu->language == language_ada || cu->language == language_m2
15281 || cu->language == language_pascal
15282 || cu->language == language_fortran)
15283 type = init_character_type (objfile, bits, 0, name);
15284 else
15285 type = init_integer_type (objfile, bits, 0, name);
15286 break;
15287 case DW_ATE_unsigned_char:
15288 if (cu->language == language_ada || cu->language == language_m2
15289 || cu->language == language_pascal
15290 || cu->language == language_fortran
15291 || cu->language == language_rust)
15292 type = init_character_type (objfile, bits, 1, name);
15293 else
15294 type = init_integer_type (objfile, bits, 1, name);
15295 break;
15296 case DW_ATE_UTF:
15297 {
15298 gdbarch *arch = get_objfile_arch (objfile);
15299
15300 if (bits == 16)
15301 type = builtin_type (arch)->builtin_char16;
15302 else if (bits == 32)
15303 type = builtin_type (arch)->builtin_char32;
15304 else
15305 {
15306 complaint (&symfile_complaints,
15307 _("unsupported DW_ATE_UTF bit size: '%d'"),
15308 bits);
15309 type = init_integer_type (objfile, bits, 1, name);
15310 }
15311 return set_die_type (die, type, cu);
15312 }
15313 break;
15314
15315 default:
15316 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
15317 dwarf_type_encoding_name (encoding));
15318 type = init_type (objfile, TYPE_CODE_ERROR,
15319 bits / TARGET_CHAR_BIT, name);
15320 break;
15321 }
15322
15323 if (name && strcmp (name, "char") == 0)
15324 TYPE_NOSIGN (type) = 1;
15325
15326 return set_die_type (die, type, cu);
15327 }
15328
15329 /* Parse dwarf attribute if it's a block, reference or constant and put the
15330 resulting value of the attribute into struct bound_prop.
15331 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
15332
15333 static int
15334 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
15335 struct dwarf2_cu *cu, struct dynamic_prop *prop)
15336 {
15337 struct dwarf2_property_baton *baton;
15338 struct obstack *obstack = &cu->objfile->objfile_obstack;
15339
15340 if (attr == NULL || prop == NULL)
15341 return 0;
15342
15343 if (attr_form_is_block (attr))
15344 {
15345 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15346 baton->referenced_type = NULL;
15347 baton->locexpr.per_cu = cu->per_cu;
15348 baton->locexpr.size = DW_BLOCK (attr)->size;
15349 baton->locexpr.data = DW_BLOCK (attr)->data;
15350 prop->data.baton = baton;
15351 prop->kind = PROP_LOCEXPR;
15352 gdb_assert (prop->data.baton != NULL);
15353 }
15354 else if (attr_form_is_ref (attr))
15355 {
15356 struct dwarf2_cu *target_cu = cu;
15357 struct die_info *target_die;
15358 struct attribute *target_attr;
15359
15360 target_die = follow_die_ref (die, attr, &target_cu);
15361 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
15362 if (target_attr == NULL)
15363 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
15364 target_cu);
15365 if (target_attr == NULL)
15366 return 0;
15367
15368 switch (target_attr->name)
15369 {
15370 case DW_AT_location:
15371 if (attr_form_is_section_offset (target_attr))
15372 {
15373 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15374 baton->referenced_type = die_type (target_die, target_cu);
15375 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
15376 prop->data.baton = baton;
15377 prop->kind = PROP_LOCLIST;
15378 gdb_assert (prop->data.baton != NULL);
15379 }
15380 else if (attr_form_is_block (target_attr))
15381 {
15382 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15383 baton->referenced_type = die_type (target_die, target_cu);
15384 baton->locexpr.per_cu = cu->per_cu;
15385 baton->locexpr.size = DW_BLOCK (target_attr)->size;
15386 baton->locexpr.data = DW_BLOCK (target_attr)->data;
15387 prop->data.baton = baton;
15388 prop->kind = PROP_LOCEXPR;
15389 gdb_assert (prop->data.baton != NULL);
15390 }
15391 else
15392 {
15393 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15394 "dynamic property");
15395 return 0;
15396 }
15397 break;
15398 case DW_AT_data_member_location:
15399 {
15400 LONGEST offset;
15401
15402 if (!handle_data_member_location (target_die, target_cu,
15403 &offset))
15404 return 0;
15405
15406 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15407 baton->referenced_type = read_type_die (target_die->parent,
15408 target_cu);
15409 baton->offset_info.offset = offset;
15410 baton->offset_info.type = die_type (target_die, target_cu);
15411 prop->data.baton = baton;
15412 prop->kind = PROP_ADDR_OFFSET;
15413 break;
15414 }
15415 }
15416 }
15417 else if (attr_form_is_constant (attr))
15418 {
15419 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
15420 prop->kind = PROP_CONST;
15421 }
15422 else
15423 {
15424 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
15425 dwarf2_name (die, cu));
15426 return 0;
15427 }
15428
15429 return 1;
15430 }
15431
15432 /* Read the given DW_AT_subrange DIE. */
15433
15434 static struct type *
15435 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
15436 {
15437 struct type *base_type, *orig_base_type;
15438 struct type *range_type;
15439 struct attribute *attr;
15440 struct dynamic_prop low, high;
15441 int low_default_is_valid;
15442 int high_bound_is_count = 0;
15443 const char *name;
15444 LONGEST negative_mask;
15445
15446 orig_base_type = die_type (die, cu);
15447 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
15448 whereas the real type might be. So, we use ORIG_BASE_TYPE when
15449 creating the range type, but we use the result of check_typedef
15450 when examining properties of the type. */
15451 base_type = check_typedef (orig_base_type);
15452
15453 /* The die_type call above may have already set the type for this DIE. */
15454 range_type = get_die_type (die, cu);
15455 if (range_type)
15456 return range_type;
15457
15458 low.kind = PROP_CONST;
15459 high.kind = PROP_CONST;
15460 high.data.const_val = 0;
15461
15462 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
15463 omitting DW_AT_lower_bound. */
15464 switch (cu->language)
15465 {
15466 case language_c:
15467 case language_cplus:
15468 low.data.const_val = 0;
15469 low_default_is_valid = 1;
15470 break;
15471 case language_fortran:
15472 low.data.const_val = 1;
15473 low_default_is_valid = 1;
15474 break;
15475 case language_d:
15476 case language_objc:
15477 case language_rust:
15478 low.data.const_val = 0;
15479 low_default_is_valid = (cu->header.version >= 4);
15480 break;
15481 case language_ada:
15482 case language_m2:
15483 case language_pascal:
15484 low.data.const_val = 1;
15485 low_default_is_valid = (cu->header.version >= 4);
15486 break;
15487 default:
15488 low.data.const_val = 0;
15489 low_default_is_valid = 0;
15490 break;
15491 }
15492
15493 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
15494 if (attr)
15495 attr_to_dynamic_prop (attr, die, cu, &low);
15496 else if (!low_default_is_valid)
15497 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
15498 "- DIE at 0x%x [in module %s]"),
15499 to_underlying (die->sect_off), objfile_name (cu->objfile));
15500
15501 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
15502 if (!attr_to_dynamic_prop (attr, die, cu, &high))
15503 {
15504 attr = dwarf2_attr (die, DW_AT_count, cu);
15505 if (attr_to_dynamic_prop (attr, die, cu, &high))
15506 {
15507 /* If bounds are constant do the final calculation here. */
15508 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
15509 high.data.const_val = low.data.const_val + high.data.const_val - 1;
15510 else
15511 high_bound_is_count = 1;
15512 }
15513 }
15514
15515 /* Dwarf-2 specifications explicitly allows to create subrange types
15516 without specifying a base type.
15517 In that case, the base type must be set to the type of
15518 the lower bound, upper bound or count, in that order, if any of these
15519 three attributes references an object that has a type.
15520 If no base type is found, the Dwarf-2 specifications say that
15521 a signed integer type of size equal to the size of an address should
15522 be used.
15523 For the following C code: `extern char gdb_int [];'
15524 GCC produces an empty range DIE.
15525 FIXME: muller/2010-05-28: Possible references to object for low bound,
15526 high bound or count are not yet handled by this code. */
15527 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
15528 {
15529 struct objfile *objfile = cu->objfile;
15530 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15531 int addr_size = gdbarch_addr_bit (gdbarch) /8;
15532 struct type *int_type = objfile_type (objfile)->builtin_int;
15533
15534 /* Test "int", "long int", and "long long int" objfile types,
15535 and select the first one having a size above or equal to the
15536 architecture address size. */
15537 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15538 base_type = int_type;
15539 else
15540 {
15541 int_type = objfile_type (objfile)->builtin_long;
15542 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15543 base_type = int_type;
15544 else
15545 {
15546 int_type = objfile_type (objfile)->builtin_long_long;
15547 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15548 base_type = int_type;
15549 }
15550 }
15551 }
15552
15553 /* Normally, the DWARF producers are expected to use a signed
15554 constant form (Eg. DW_FORM_sdata) to express negative bounds.
15555 But this is unfortunately not always the case, as witnessed
15556 with GCC, for instance, where the ambiguous DW_FORM_dataN form
15557 is used instead. To work around that ambiguity, we treat
15558 the bounds as signed, and thus sign-extend their values, when
15559 the base type is signed. */
15560 negative_mask =
15561 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
15562 if (low.kind == PROP_CONST
15563 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
15564 low.data.const_val |= negative_mask;
15565 if (high.kind == PROP_CONST
15566 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
15567 high.data.const_val |= negative_mask;
15568
15569 range_type = create_range_type (NULL, orig_base_type, &low, &high);
15570
15571 if (high_bound_is_count)
15572 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
15573
15574 /* Ada expects an empty array on no boundary attributes. */
15575 if (attr == NULL && cu->language != language_ada)
15576 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
15577
15578 name = dwarf2_name (die, cu);
15579 if (name)
15580 TYPE_NAME (range_type) = name;
15581
15582 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15583 if (attr)
15584 TYPE_LENGTH (range_type) = DW_UNSND (attr);
15585
15586 set_die_type (die, range_type, cu);
15587
15588 /* set_die_type should be already done. */
15589 set_descriptive_type (range_type, die, cu);
15590
15591 return range_type;
15592 }
15593
15594 static struct type *
15595 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
15596 {
15597 struct type *type;
15598
15599 /* For now, we only support the C meaning of an unspecified type: void. */
15600
15601 type = init_type (cu->objfile, TYPE_CODE_VOID, 0, NULL);
15602 TYPE_NAME (type) = dwarf2_name (die, cu);
15603
15604 return set_die_type (die, type, cu);
15605 }
15606
15607 /* Read a single die and all its descendents. Set the die's sibling
15608 field to NULL; set other fields in the die correctly, and set all
15609 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
15610 location of the info_ptr after reading all of those dies. PARENT
15611 is the parent of the die in question. */
15612
15613 static struct die_info *
15614 read_die_and_children (const struct die_reader_specs *reader,
15615 const gdb_byte *info_ptr,
15616 const gdb_byte **new_info_ptr,
15617 struct die_info *parent)
15618 {
15619 struct die_info *die;
15620 const gdb_byte *cur_ptr;
15621 int has_children;
15622
15623 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
15624 if (die == NULL)
15625 {
15626 *new_info_ptr = cur_ptr;
15627 return NULL;
15628 }
15629 store_in_ref_table (die, reader->cu);
15630
15631 if (has_children)
15632 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
15633 else
15634 {
15635 die->child = NULL;
15636 *new_info_ptr = cur_ptr;
15637 }
15638
15639 die->sibling = NULL;
15640 die->parent = parent;
15641 return die;
15642 }
15643
15644 /* Read a die, all of its descendents, and all of its siblings; set
15645 all of the fields of all of the dies correctly. Arguments are as
15646 in read_die_and_children. */
15647
15648 static struct die_info *
15649 read_die_and_siblings_1 (const struct die_reader_specs *reader,
15650 const gdb_byte *info_ptr,
15651 const gdb_byte **new_info_ptr,
15652 struct die_info *parent)
15653 {
15654 struct die_info *first_die, *last_sibling;
15655 const gdb_byte *cur_ptr;
15656
15657 cur_ptr = info_ptr;
15658 first_die = last_sibling = NULL;
15659
15660 while (1)
15661 {
15662 struct die_info *die
15663 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
15664
15665 if (die == NULL)
15666 {
15667 *new_info_ptr = cur_ptr;
15668 return first_die;
15669 }
15670
15671 if (!first_die)
15672 first_die = die;
15673 else
15674 last_sibling->sibling = die;
15675
15676 last_sibling = die;
15677 }
15678 }
15679
15680 /* Read a die, all of its descendents, and all of its siblings; set
15681 all of the fields of all of the dies correctly. Arguments are as
15682 in read_die_and_children.
15683 This the main entry point for reading a DIE and all its children. */
15684
15685 static struct die_info *
15686 read_die_and_siblings (const struct die_reader_specs *reader,
15687 const gdb_byte *info_ptr,
15688 const gdb_byte **new_info_ptr,
15689 struct die_info *parent)
15690 {
15691 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
15692 new_info_ptr, parent);
15693
15694 if (dwarf_die_debug)
15695 {
15696 fprintf_unfiltered (gdb_stdlog,
15697 "Read die from %s@0x%x of %s:\n",
15698 get_section_name (reader->die_section),
15699 (unsigned) (info_ptr - reader->die_section->buffer),
15700 bfd_get_filename (reader->abfd));
15701 dump_die (die, dwarf_die_debug);
15702 }
15703
15704 return die;
15705 }
15706
15707 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
15708 attributes.
15709 The caller is responsible for filling in the extra attributes
15710 and updating (*DIEP)->num_attrs.
15711 Set DIEP to point to a newly allocated die with its information,
15712 except for its child, sibling, and parent fields.
15713 Set HAS_CHILDREN to tell whether the die has children or not. */
15714
15715 static const gdb_byte *
15716 read_full_die_1 (const struct die_reader_specs *reader,
15717 struct die_info **diep, const gdb_byte *info_ptr,
15718 int *has_children, int num_extra_attrs)
15719 {
15720 unsigned int abbrev_number, bytes_read, i;
15721 struct abbrev_info *abbrev;
15722 struct die_info *die;
15723 struct dwarf2_cu *cu = reader->cu;
15724 bfd *abfd = reader->abfd;
15725
15726 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
15727 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
15728 info_ptr += bytes_read;
15729 if (!abbrev_number)
15730 {
15731 *diep = NULL;
15732 *has_children = 0;
15733 return info_ptr;
15734 }
15735
15736 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
15737 if (!abbrev)
15738 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
15739 abbrev_number,
15740 bfd_get_filename (abfd));
15741
15742 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
15743 die->sect_off = sect_off;
15744 die->tag = abbrev->tag;
15745 die->abbrev = abbrev_number;
15746
15747 /* Make the result usable.
15748 The caller needs to update num_attrs after adding the extra
15749 attributes. */
15750 die->num_attrs = abbrev->num_attrs;
15751
15752 for (i = 0; i < abbrev->num_attrs; ++i)
15753 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
15754 info_ptr);
15755
15756 *diep = die;
15757 *has_children = abbrev->has_children;
15758 return info_ptr;
15759 }
15760
15761 /* Read a die and all its attributes.
15762 Set DIEP to point to a newly allocated die with its information,
15763 except for its child, sibling, and parent fields.
15764 Set HAS_CHILDREN to tell whether the die has children or not. */
15765
15766 static const gdb_byte *
15767 read_full_die (const struct die_reader_specs *reader,
15768 struct die_info **diep, const gdb_byte *info_ptr,
15769 int *has_children)
15770 {
15771 const gdb_byte *result;
15772
15773 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
15774
15775 if (dwarf_die_debug)
15776 {
15777 fprintf_unfiltered (gdb_stdlog,
15778 "Read die from %s@0x%x of %s:\n",
15779 get_section_name (reader->die_section),
15780 (unsigned) (info_ptr - reader->die_section->buffer),
15781 bfd_get_filename (reader->abfd));
15782 dump_die (*diep, dwarf_die_debug);
15783 }
15784
15785 return result;
15786 }
15787 \f
15788 /* Abbreviation tables.
15789
15790 In DWARF version 2, the description of the debugging information is
15791 stored in a separate .debug_abbrev section. Before we read any
15792 dies from a section we read in all abbreviations and install them
15793 in a hash table. */
15794
15795 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
15796
15797 static struct abbrev_info *
15798 abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
15799 {
15800 struct abbrev_info *abbrev;
15801
15802 abbrev = XOBNEW (&abbrev_table->abbrev_obstack, struct abbrev_info);
15803 memset (abbrev, 0, sizeof (struct abbrev_info));
15804
15805 return abbrev;
15806 }
15807
15808 /* Add an abbreviation to the table. */
15809
15810 static void
15811 abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
15812 unsigned int abbrev_number,
15813 struct abbrev_info *abbrev)
15814 {
15815 unsigned int hash_number;
15816
15817 hash_number = abbrev_number % ABBREV_HASH_SIZE;
15818 abbrev->next = abbrev_table->abbrevs[hash_number];
15819 abbrev_table->abbrevs[hash_number] = abbrev;
15820 }
15821
15822 /* Look up an abbrev in the table.
15823 Returns NULL if the abbrev is not found. */
15824
15825 static struct abbrev_info *
15826 abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
15827 unsigned int abbrev_number)
15828 {
15829 unsigned int hash_number;
15830 struct abbrev_info *abbrev;
15831
15832 hash_number = abbrev_number % ABBREV_HASH_SIZE;
15833 abbrev = abbrev_table->abbrevs[hash_number];
15834
15835 while (abbrev)
15836 {
15837 if (abbrev->number == abbrev_number)
15838 return abbrev;
15839 abbrev = abbrev->next;
15840 }
15841 return NULL;
15842 }
15843
15844 /* Read in an abbrev table. */
15845
15846 static struct abbrev_table *
15847 abbrev_table_read_table (struct dwarf2_section_info *section,
15848 sect_offset sect_off)
15849 {
15850 struct objfile *objfile = dwarf2_per_objfile->objfile;
15851 bfd *abfd = get_section_bfd_owner (section);
15852 struct abbrev_table *abbrev_table;
15853 const gdb_byte *abbrev_ptr;
15854 struct abbrev_info *cur_abbrev;
15855 unsigned int abbrev_number, bytes_read, abbrev_name;
15856 unsigned int abbrev_form;
15857 struct attr_abbrev *cur_attrs;
15858 unsigned int allocated_attrs;
15859
15860 abbrev_table = XNEW (struct abbrev_table);
15861 abbrev_table->sect_off = sect_off;
15862 obstack_init (&abbrev_table->abbrev_obstack);
15863 abbrev_table->abbrevs =
15864 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct abbrev_info *,
15865 ABBREV_HASH_SIZE);
15866 memset (abbrev_table->abbrevs, 0,
15867 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
15868
15869 dwarf2_read_section (objfile, section);
15870 abbrev_ptr = section->buffer + to_underlying (sect_off);
15871 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15872 abbrev_ptr += bytes_read;
15873
15874 allocated_attrs = ATTR_ALLOC_CHUNK;
15875 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
15876
15877 /* Loop until we reach an abbrev number of 0. */
15878 while (abbrev_number)
15879 {
15880 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
15881
15882 /* read in abbrev header */
15883 cur_abbrev->number = abbrev_number;
15884 cur_abbrev->tag
15885 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15886 abbrev_ptr += bytes_read;
15887 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
15888 abbrev_ptr += 1;
15889
15890 /* now read in declarations */
15891 for (;;)
15892 {
15893 LONGEST implicit_const;
15894
15895 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15896 abbrev_ptr += bytes_read;
15897 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15898 abbrev_ptr += bytes_read;
15899 if (abbrev_form == DW_FORM_implicit_const)
15900 {
15901 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
15902 &bytes_read);
15903 abbrev_ptr += bytes_read;
15904 }
15905 else
15906 {
15907 /* Initialize it due to a false compiler warning. */
15908 implicit_const = -1;
15909 }
15910
15911 if (abbrev_name == 0)
15912 break;
15913
15914 if (cur_abbrev->num_attrs == allocated_attrs)
15915 {
15916 allocated_attrs += ATTR_ALLOC_CHUNK;
15917 cur_attrs
15918 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
15919 }
15920
15921 cur_attrs[cur_abbrev->num_attrs].name
15922 = (enum dwarf_attribute) abbrev_name;
15923 cur_attrs[cur_abbrev->num_attrs].form
15924 = (enum dwarf_form) abbrev_form;
15925 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
15926 ++cur_abbrev->num_attrs;
15927 }
15928
15929 cur_abbrev->attrs =
15930 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
15931 cur_abbrev->num_attrs);
15932 memcpy (cur_abbrev->attrs, cur_attrs,
15933 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
15934
15935 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
15936
15937 /* Get next abbreviation.
15938 Under Irix6 the abbreviations for a compilation unit are not
15939 always properly terminated with an abbrev number of 0.
15940 Exit loop if we encounter an abbreviation which we have
15941 already read (which means we are about to read the abbreviations
15942 for the next compile unit) or if the end of the abbreviation
15943 table is reached. */
15944 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
15945 break;
15946 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15947 abbrev_ptr += bytes_read;
15948 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
15949 break;
15950 }
15951
15952 xfree (cur_attrs);
15953 return abbrev_table;
15954 }
15955
15956 /* Free the resources held by ABBREV_TABLE. */
15957
15958 static void
15959 abbrev_table_free (struct abbrev_table *abbrev_table)
15960 {
15961 obstack_free (&abbrev_table->abbrev_obstack, NULL);
15962 xfree (abbrev_table);
15963 }
15964
15965 /* Same as abbrev_table_free but as a cleanup.
15966 We pass in a pointer to the pointer to the table so that we can
15967 set the pointer to NULL when we're done. It also simplifies
15968 build_type_psymtabs_1. */
15969
15970 static void
15971 abbrev_table_free_cleanup (void *table_ptr)
15972 {
15973 struct abbrev_table **abbrev_table_ptr = (struct abbrev_table **) table_ptr;
15974
15975 if (*abbrev_table_ptr != NULL)
15976 abbrev_table_free (*abbrev_table_ptr);
15977 *abbrev_table_ptr = NULL;
15978 }
15979
15980 /* Read the abbrev table for CU from ABBREV_SECTION. */
15981
15982 static void
15983 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
15984 struct dwarf2_section_info *abbrev_section)
15985 {
15986 cu->abbrev_table =
15987 abbrev_table_read_table (abbrev_section, cu->header.abbrev_sect_off);
15988 }
15989
15990 /* Release the memory used by the abbrev table for a compilation unit. */
15991
15992 static void
15993 dwarf2_free_abbrev_table (void *ptr_to_cu)
15994 {
15995 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr_to_cu;
15996
15997 if (cu->abbrev_table != NULL)
15998 abbrev_table_free (cu->abbrev_table);
15999 /* Set this to NULL so that we SEGV if we try to read it later,
16000 and also because free_comp_unit verifies this is NULL. */
16001 cu->abbrev_table = NULL;
16002 }
16003 \f
16004 /* Returns nonzero if TAG represents a type that we might generate a partial
16005 symbol for. */
16006
16007 static int
16008 is_type_tag_for_partial (int tag)
16009 {
16010 switch (tag)
16011 {
16012 #if 0
16013 /* Some types that would be reasonable to generate partial symbols for,
16014 that we don't at present. */
16015 case DW_TAG_array_type:
16016 case DW_TAG_file_type:
16017 case DW_TAG_ptr_to_member_type:
16018 case DW_TAG_set_type:
16019 case DW_TAG_string_type:
16020 case DW_TAG_subroutine_type:
16021 #endif
16022 case DW_TAG_base_type:
16023 case DW_TAG_class_type:
16024 case DW_TAG_interface_type:
16025 case DW_TAG_enumeration_type:
16026 case DW_TAG_structure_type:
16027 case DW_TAG_subrange_type:
16028 case DW_TAG_typedef:
16029 case DW_TAG_union_type:
16030 return 1;
16031 default:
16032 return 0;
16033 }
16034 }
16035
16036 /* Load all DIEs that are interesting for partial symbols into memory. */
16037
16038 static struct partial_die_info *
16039 load_partial_dies (const struct die_reader_specs *reader,
16040 const gdb_byte *info_ptr, int building_psymtab)
16041 {
16042 struct dwarf2_cu *cu = reader->cu;
16043 struct objfile *objfile = cu->objfile;
16044 struct partial_die_info *part_die;
16045 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
16046 struct abbrev_info *abbrev;
16047 unsigned int bytes_read;
16048 unsigned int load_all = 0;
16049 int nesting_level = 1;
16050
16051 parent_die = NULL;
16052 last_die = NULL;
16053
16054 gdb_assert (cu->per_cu != NULL);
16055 if (cu->per_cu->load_all_dies)
16056 load_all = 1;
16057
16058 cu->partial_dies
16059 = htab_create_alloc_ex (cu->header.length / 12,
16060 partial_die_hash,
16061 partial_die_eq,
16062 NULL,
16063 &cu->comp_unit_obstack,
16064 hashtab_obstack_allocate,
16065 dummy_obstack_deallocate);
16066
16067 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
16068
16069 while (1)
16070 {
16071 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
16072
16073 /* A NULL abbrev means the end of a series of children. */
16074 if (abbrev == NULL)
16075 {
16076 if (--nesting_level == 0)
16077 {
16078 /* PART_DIE was probably the last thing allocated on the
16079 comp_unit_obstack, so we could call obstack_free
16080 here. We don't do that because the waste is small,
16081 and will be cleaned up when we're done with this
16082 compilation unit. This way, we're also more robust
16083 against other users of the comp_unit_obstack. */
16084 return first_die;
16085 }
16086 info_ptr += bytes_read;
16087 last_die = parent_die;
16088 parent_die = parent_die->die_parent;
16089 continue;
16090 }
16091
16092 /* Check for template arguments. We never save these; if
16093 they're seen, we just mark the parent, and go on our way. */
16094 if (parent_die != NULL
16095 && cu->language == language_cplus
16096 && (abbrev->tag == DW_TAG_template_type_param
16097 || abbrev->tag == DW_TAG_template_value_param))
16098 {
16099 parent_die->has_template_arguments = 1;
16100
16101 if (!load_all)
16102 {
16103 /* We don't need a partial DIE for the template argument. */
16104 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
16105 continue;
16106 }
16107 }
16108
16109 /* We only recurse into c++ subprograms looking for template arguments.
16110 Skip their other children. */
16111 if (!load_all
16112 && cu->language == language_cplus
16113 && parent_die != NULL
16114 && parent_die->tag == DW_TAG_subprogram)
16115 {
16116 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
16117 continue;
16118 }
16119
16120 /* Check whether this DIE is interesting enough to save. Normally
16121 we would not be interested in members here, but there may be
16122 later variables referencing them via DW_AT_specification (for
16123 static members). */
16124 if (!load_all
16125 && !is_type_tag_for_partial (abbrev->tag)
16126 && abbrev->tag != DW_TAG_constant
16127 && abbrev->tag != DW_TAG_enumerator
16128 && abbrev->tag != DW_TAG_subprogram
16129 && abbrev->tag != DW_TAG_lexical_block
16130 && abbrev->tag != DW_TAG_variable
16131 && abbrev->tag != DW_TAG_namespace
16132 && abbrev->tag != DW_TAG_module
16133 && abbrev->tag != DW_TAG_member
16134 && abbrev->tag != DW_TAG_imported_unit
16135 && abbrev->tag != DW_TAG_imported_declaration)
16136 {
16137 /* Otherwise we skip to the next sibling, if any. */
16138 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
16139 continue;
16140 }
16141
16142 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
16143 info_ptr);
16144
16145 /* This two-pass algorithm for processing partial symbols has a
16146 high cost in cache pressure. Thus, handle some simple cases
16147 here which cover the majority of C partial symbols. DIEs
16148 which neither have specification tags in them, nor could have
16149 specification tags elsewhere pointing at them, can simply be
16150 processed and discarded.
16151
16152 This segment is also optional; scan_partial_symbols and
16153 add_partial_symbol will handle these DIEs if we chain
16154 them in normally. When compilers which do not emit large
16155 quantities of duplicate debug information are more common,
16156 this code can probably be removed. */
16157
16158 /* Any complete simple types at the top level (pretty much all
16159 of them, for a language without namespaces), can be processed
16160 directly. */
16161 if (parent_die == NULL
16162 && part_die->has_specification == 0
16163 && part_die->is_declaration == 0
16164 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
16165 || part_die->tag == DW_TAG_base_type
16166 || part_die->tag == DW_TAG_subrange_type))
16167 {
16168 if (building_psymtab && part_die->name != NULL)
16169 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
16170 VAR_DOMAIN, LOC_TYPEDEF,
16171 &objfile->static_psymbols,
16172 0, cu->language, objfile);
16173 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
16174 continue;
16175 }
16176
16177 /* The exception for DW_TAG_typedef with has_children above is
16178 a workaround of GCC PR debug/47510. In the case of this complaint
16179 type_name_no_tag_or_error will error on such types later.
16180
16181 GDB skipped children of DW_TAG_typedef by the shortcut above and then
16182 it could not find the child DIEs referenced later, this is checked
16183 above. In correct DWARF DW_TAG_typedef should have no children. */
16184
16185 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
16186 complaint (&symfile_complaints,
16187 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
16188 "- DIE at 0x%x [in module %s]"),
16189 to_underlying (part_die->sect_off), objfile_name (objfile));
16190
16191 /* If we're at the second level, and we're an enumerator, and
16192 our parent has no specification (meaning possibly lives in a
16193 namespace elsewhere), then we can add the partial symbol now
16194 instead of queueing it. */
16195 if (part_die->tag == DW_TAG_enumerator
16196 && parent_die != NULL
16197 && parent_die->die_parent == NULL
16198 && parent_die->tag == DW_TAG_enumeration_type
16199 && parent_die->has_specification == 0)
16200 {
16201 if (part_die->name == NULL)
16202 complaint (&symfile_complaints,
16203 _("malformed enumerator DIE ignored"));
16204 else if (building_psymtab)
16205 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
16206 VAR_DOMAIN, LOC_CONST,
16207 cu->language == language_cplus
16208 ? &objfile->global_psymbols
16209 : &objfile->static_psymbols,
16210 0, cu->language, objfile);
16211
16212 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
16213 continue;
16214 }
16215
16216 /* We'll save this DIE so link it in. */
16217 part_die->die_parent = parent_die;
16218 part_die->die_sibling = NULL;
16219 part_die->die_child = NULL;
16220
16221 if (last_die && last_die == parent_die)
16222 last_die->die_child = part_die;
16223 else if (last_die)
16224 last_die->die_sibling = part_die;
16225
16226 last_die = part_die;
16227
16228 if (first_die == NULL)
16229 first_die = part_die;
16230
16231 /* Maybe add the DIE to the hash table. Not all DIEs that we
16232 find interesting need to be in the hash table, because we
16233 also have the parent/sibling/child chains; only those that we
16234 might refer to by offset later during partial symbol reading.
16235
16236 For now this means things that might have be the target of a
16237 DW_AT_specification, DW_AT_abstract_origin, or
16238 DW_AT_extension. DW_AT_extension will refer only to
16239 namespaces; DW_AT_abstract_origin refers to functions (and
16240 many things under the function DIE, but we do not recurse
16241 into function DIEs during partial symbol reading) and
16242 possibly variables as well; DW_AT_specification refers to
16243 declarations. Declarations ought to have the DW_AT_declaration
16244 flag. It happens that GCC forgets to put it in sometimes, but
16245 only for functions, not for types.
16246
16247 Adding more things than necessary to the hash table is harmless
16248 except for the performance cost. Adding too few will result in
16249 wasted time in find_partial_die, when we reread the compilation
16250 unit with load_all_dies set. */
16251
16252 if (load_all
16253 || abbrev->tag == DW_TAG_constant
16254 || abbrev->tag == DW_TAG_subprogram
16255 || abbrev->tag == DW_TAG_variable
16256 || abbrev->tag == DW_TAG_namespace
16257 || part_die->is_declaration)
16258 {
16259 void **slot;
16260
16261 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
16262 to_underlying (part_die->sect_off),
16263 INSERT);
16264 *slot = part_die;
16265 }
16266
16267 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
16268
16269 /* For some DIEs we want to follow their children (if any). For C
16270 we have no reason to follow the children of structures; for other
16271 languages we have to, so that we can get at method physnames
16272 to infer fully qualified class names, for DW_AT_specification,
16273 and for C++ template arguments. For C++, we also look one level
16274 inside functions to find template arguments (if the name of the
16275 function does not already contain the template arguments).
16276
16277 For Ada, we need to scan the children of subprograms and lexical
16278 blocks as well because Ada allows the definition of nested
16279 entities that could be interesting for the debugger, such as
16280 nested subprograms for instance. */
16281 if (last_die->has_children
16282 && (load_all
16283 || last_die->tag == DW_TAG_namespace
16284 || last_die->tag == DW_TAG_module
16285 || last_die->tag == DW_TAG_enumeration_type
16286 || (cu->language == language_cplus
16287 && last_die->tag == DW_TAG_subprogram
16288 && (last_die->name == NULL
16289 || strchr (last_die->name, '<') == NULL))
16290 || (cu->language != language_c
16291 && (last_die->tag == DW_TAG_class_type
16292 || last_die->tag == DW_TAG_interface_type
16293 || last_die->tag == DW_TAG_structure_type
16294 || last_die->tag == DW_TAG_union_type))
16295 || (cu->language == language_ada
16296 && (last_die->tag == DW_TAG_subprogram
16297 || last_die->tag == DW_TAG_lexical_block))))
16298 {
16299 nesting_level++;
16300 parent_die = last_die;
16301 continue;
16302 }
16303
16304 /* Otherwise we skip to the next sibling, if any. */
16305 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
16306
16307 /* Back to the top, do it again. */
16308 }
16309 }
16310
16311 /* Read a minimal amount of information into the minimal die structure. */
16312
16313 static const gdb_byte *
16314 read_partial_die (const struct die_reader_specs *reader,
16315 struct partial_die_info *part_die,
16316 struct abbrev_info *abbrev, unsigned int abbrev_len,
16317 const gdb_byte *info_ptr)
16318 {
16319 struct dwarf2_cu *cu = reader->cu;
16320 struct objfile *objfile = cu->objfile;
16321 const gdb_byte *buffer = reader->buffer;
16322 unsigned int i;
16323 struct attribute attr;
16324 int has_low_pc_attr = 0;
16325 int has_high_pc_attr = 0;
16326 int high_pc_relative = 0;
16327
16328 memset (part_die, 0, sizeof (struct partial_die_info));
16329
16330 part_die->sect_off = (sect_offset) (info_ptr - buffer);
16331
16332 info_ptr += abbrev_len;
16333
16334 if (abbrev == NULL)
16335 return info_ptr;
16336
16337 part_die->tag = abbrev->tag;
16338 part_die->has_children = abbrev->has_children;
16339
16340 for (i = 0; i < abbrev->num_attrs; ++i)
16341 {
16342 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
16343
16344 /* Store the data if it is of an attribute we want to keep in a
16345 partial symbol table. */
16346 switch (attr.name)
16347 {
16348 case DW_AT_name:
16349 switch (part_die->tag)
16350 {
16351 case DW_TAG_compile_unit:
16352 case DW_TAG_partial_unit:
16353 case DW_TAG_type_unit:
16354 /* Compilation units have a DW_AT_name that is a filename, not
16355 a source language identifier. */
16356 case DW_TAG_enumeration_type:
16357 case DW_TAG_enumerator:
16358 /* These tags always have simple identifiers already; no need
16359 to canonicalize them. */
16360 part_die->name = DW_STRING (&attr);
16361 break;
16362 default:
16363 part_die->name
16364 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
16365 &objfile->per_bfd->storage_obstack);
16366 break;
16367 }
16368 break;
16369 case DW_AT_linkage_name:
16370 case DW_AT_MIPS_linkage_name:
16371 /* Note that both forms of linkage name might appear. We
16372 assume they will be the same, and we only store the last
16373 one we see. */
16374 if (cu->language == language_ada)
16375 part_die->name = DW_STRING (&attr);
16376 part_die->linkage_name = DW_STRING (&attr);
16377 break;
16378 case DW_AT_low_pc:
16379 has_low_pc_attr = 1;
16380 part_die->lowpc = attr_value_as_address (&attr);
16381 break;
16382 case DW_AT_high_pc:
16383 has_high_pc_attr = 1;
16384 part_die->highpc = attr_value_as_address (&attr);
16385 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
16386 high_pc_relative = 1;
16387 break;
16388 case DW_AT_location:
16389 /* Support the .debug_loc offsets. */
16390 if (attr_form_is_block (&attr))
16391 {
16392 part_die->d.locdesc = DW_BLOCK (&attr);
16393 }
16394 else if (attr_form_is_section_offset (&attr))
16395 {
16396 dwarf2_complex_location_expr_complaint ();
16397 }
16398 else
16399 {
16400 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16401 "partial symbol information");
16402 }
16403 break;
16404 case DW_AT_external:
16405 part_die->is_external = DW_UNSND (&attr);
16406 break;
16407 case DW_AT_declaration:
16408 part_die->is_declaration = DW_UNSND (&attr);
16409 break;
16410 case DW_AT_type:
16411 part_die->has_type = 1;
16412 break;
16413 case DW_AT_abstract_origin:
16414 case DW_AT_specification:
16415 case DW_AT_extension:
16416 part_die->has_specification = 1;
16417 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
16418 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
16419 || cu->per_cu->is_dwz);
16420 break;
16421 case DW_AT_sibling:
16422 /* Ignore absolute siblings, they might point outside of
16423 the current compile unit. */
16424 if (attr.form == DW_FORM_ref_addr)
16425 complaint (&symfile_complaints,
16426 _("ignoring absolute DW_AT_sibling"));
16427 else
16428 {
16429 sect_offset off = dwarf2_get_ref_die_offset (&attr);
16430 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
16431
16432 if (sibling_ptr < info_ptr)
16433 complaint (&symfile_complaints,
16434 _("DW_AT_sibling points backwards"));
16435 else if (sibling_ptr > reader->buffer_end)
16436 dwarf2_section_buffer_overflow_complaint (reader->die_section);
16437 else
16438 part_die->sibling = sibling_ptr;
16439 }
16440 break;
16441 case DW_AT_byte_size:
16442 part_die->has_byte_size = 1;
16443 break;
16444 case DW_AT_const_value:
16445 part_die->has_const_value = 1;
16446 break;
16447 case DW_AT_calling_convention:
16448 /* DWARF doesn't provide a way to identify a program's source-level
16449 entry point. DW_AT_calling_convention attributes are only meant
16450 to describe functions' calling conventions.
16451
16452 However, because it's a necessary piece of information in
16453 Fortran, and before DWARF 4 DW_CC_program was the only
16454 piece of debugging information whose definition refers to
16455 a 'main program' at all, several compilers marked Fortran
16456 main programs with DW_CC_program --- even when those
16457 functions use the standard calling conventions.
16458
16459 Although DWARF now specifies a way to provide this
16460 information, we support this practice for backward
16461 compatibility. */
16462 if (DW_UNSND (&attr) == DW_CC_program
16463 && cu->language == language_fortran)
16464 part_die->main_subprogram = 1;
16465 break;
16466 case DW_AT_inline:
16467 if (DW_UNSND (&attr) == DW_INL_inlined
16468 || DW_UNSND (&attr) == DW_INL_declared_inlined)
16469 part_die->may_be_inlined = 1;
16470 break;
16471
16472 case DW_AT_import:
16473 if (part_die->tag == DW_TAG_imported_unit)
16474 {
16475 part_die->d.sect_off = dwarf2_get_ref_die_offset (&attr);
16476 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
16477 || cu->per_cu->is_dwz);
16478 }
16479 break;
16480
16481 case DW_AT_main_subprogram:
16482 part_die->main_subprogram = DW_UNSND (&attr);
16483 break;
16484
16485 default:
16486 break;
16487 }
16488 }
16489
16490 if (high_pc_relative)
16491 part_die->highpc += part_die->lowpc;
16492
16493 if (has_low_pc_attr && has_high_pc_attr)
16494 {
16495 /* When using the GNU linker, .gnu.linkonce. sections are used to
16496 eliminate duplicate copies of functions and vtables and such.
16497 The linker will arbitrarily choose one and discard the others.
16498 The AT_*_pc values for such functions refer to local labels in
16499 these sections. If the section from that file was discarded, the
16500 labels are not in the output, so the relocs get a value of 0.
16501 If this is a discarded function, mark the pc bounds as invalid,
16502 so that GDB will ignore it. */
16503 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
16504 {
16505 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16506
16507 complaint (&symfile_complaints,
16508 _("DW_AT_low_pc %s is zero "
16509 "for DIE at 0x%x [in module %s]"),
16510 paddress (gdbarch, part_die->lowpc),
16511 to_underlying (part_die->sect_off), objfile_name (objfile));
16512 }
16513 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
16514 else if (part_die->lowpc >= part_die->highpc)
16515 {
16516 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16517
16518 complaint (&symfile_complaints,
16519 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
16520 "for DIE at 0x%x [in module %s]"),
16521 paddress (gdbarch, part_die->lowpc),
16522 paddress (gdbarch, part_die->highpc),
16523 to_underlying (part_die->sect_off),
16524 objfile_name (objfile));
16525 }
16526 else
16527 part_die->has_pc_info = 1;
16528 }
16529
16530 return info_ptr;
16531 }
16532
16533 /* Find a cached partial DIE at OFFSET in CU. */
16534
16535 static struct partial_die_info *
16536 find_partial_die_in_comp_unit (sect_offset sect_off, struct dwarf2_cu *cu)
16537 {
16538 struct partial_die_info *lookup_die = NULL;
16539 struct partial_die_info part_die;
16540
16541 part_die.sect_off = sect_off;
16542 lookup_die = ((struct partial_die_info *)
16543 htab_find_with_hash (cu->partial_dies, &part_die,
16544 to_underlying (sect_off)));
16545
16546 return lookup_die;
16547 }
16548
16549 /* Find a partial DIE at OFFSET, which may or may not be in CU,
16550 except in the case of .debug_types DIEs which do not reference
16551 outside their CU (they do however referencing other types via
16552 DW_FORM_ref_sig8). */
16553
16554 static struct partial_die_info *
16555 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
16556 {
16557 struct objfile *objfile = cu->objfile;
16558 struct dwarf2_per_cu_data *per_cu = NULL;
16559 struct partial_die_info *pd = NULL;
16560
16561 if (offset_in_dwz == cu->per_cu->is_dwz
16562 && offset_in_cu_p (&cu->header, sect_off))
16563 {
16564 pd = find_partial_die_in_comp_unit (sect_off, cu);
16565 if (pd != NULL)
16566 return pd;
16567 /* We missed recording what we needed.
16568 Load all dies and try again. */
16569 per_cu = cu->per_cu;
16570 }
16571 else
16572 {
16573 /* TUs don't reference other CUs/TUs (except via type signatures). */
16574 if (cu->per_cu->is_debug_types)
16575 {
16576 error (_("Dwarf Error: Type Unit at offset 0x%x contains"
16577 " external reference to offset 0x%x [in module %s].\n"),
16578 to_underlying (cu->header.sect_off), to_underlying (sect_off),
16579 bfd_get_filename (objfile->obfd));
16580 }
16581 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
16582 objfile);
16583
16584 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
16585 load_partial_comp_unit (per_cu);
16586
16587 per_cu->cu->last_used = 0;
16588 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
16589 }
16590
16591 /* If we didn't find it, and not all dies have been loaded,
16592 load them all and try again. */
16593
16594 if (pd == NULL && per_cu->load_all_dies == 0)
16595 {
16596 per_cu->load_all_dies = 1;
16597
16598 /* This is nasty. When we reread the DIEs, somewhere up the call chain
16599 THIS_CU->cu may already be in use. So we can't just free it and
16600 replace its DIEs with the ones we read in. Instead, we leave those
16601 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
16602 and clobber THIS_CU->cu->partial_dies with the hash table for the new
16603 set. */
16604 load_partial_comp_unit (per_cu);
16605
16606 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
16607 }
16608
16609 if (pd == NULL)
16610 internal_error (__FILE__, __LINE__,
16611 _("could not find partial DIE 0x%x "
16612 "in cache [from module %s]\n"),
16613 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
16614 return pd;
16615 }
16616
16617 /* See if we can figure out if the class lives in a namespace. We do
16618 this by looking for a member function; its demangled name will
16619 contain namespace info, if there is any. */
16620
16621 static void
16622 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
16623 struct dwarf2_cu *cu)
16624 {
16625 /* NOTE: carlton/2003-10-07: Getting the info this way changes
16626 what template types look like, because the demangler
16627 frequently doesn't give the same name as the debug info. We
16628 could fix this by only using the demangled name to get the
16629 prefix (but see comment in read_structure_type). */
16630
16631 struct partial_die_info *real_pdi;
16632 struct partial_die_info *child_pdi;
16633
16634 /* If this DIE (this DIE's specification, if any) has a parent, then
16635 we should not do this. We'll prepend the parent's fully qualified
16636 name when we create the partial symbol. */
16637
16638 real_pdi = struct_pdi;
16639 while (real_pdi->has_specification)
16640 real_pdi = find_partial_die (real_pdi->spec_offset,
16641 real_pdi->spec_is_dwz, cu);
16642
16643 if (real_pdi->die_parent != NULL)
16644 return;
16645
16646 for (child_pdi = struct_pdi->die_child;
16647 child_pdi != NULL;
16648 child_pdi = child_pdi->die_sibling)
16649 {
16650 if (child_pdi->tag == DW_TAG_subprogram
16651 && child_pdi->linkage_name != NULL)
16652 {
16653 char *actual_class_name
16654 = language_class_name_from_physname (cu->language_defn,
16655 child_pdi->linkage_name);
16656 if (actual_class_name != NULL)
16657 {
16658 struct_pdi->name
16659 = ((const char *)
16660 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
16661 actual_class_name,
16662 strlen (actual_class_name)));
16663 xfree (actual_class_name);
16664 }
16665 break;
16666 }
16667 }
16668 }
16669
16670 /* Adjust PART_DIE before generating a symbol for it. This function
16671 may set the is_external flag or change the DIE's name. */
16672
16673 static void
16674 fixup_partial_die (struct partial_die_info *part_die,
16675 struct dwarf2_cu *cu)
16676 {
16677 /* Once we've fixed up a die, there's no point in doing so again.
16678 This also avoids a memory leak if we were to call
16679 guess_partial_die_structure_name multiple times. */
16680 if (part_die->fixup_called)
16681 return;
16682
16683 /* If we found a reference attribute and the DIE has no name, try
16684 to find a name in the referred to DIE. */
16685
16686 if (part_die->name == NULL && part_die->has_specification)
16687 {
16688 struct partial_die_info *spec_die;
16689
16690 spec_die = find_partial_die (part_die->spec_offset,
16691 part_die->spec_is_dwz, cu);
16692
16693 fixup_partial_die (spec_die, cu);
16694
16695 if (spec_die->name)
16696 {
16697 part_die->name = spec_die->name;
16698
16699 /* Copy DW_AT_external attribute if it is set. */
16700 if (spec_die->is_external)
16701 part_die->is_external = spec_die->is_external;
16702 }
16703 }
16704
16705 /* Set default names for some unnamed DIEs. */
16706
16707 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
16708 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
16709
16710 /* If there is no parent die to provide a namespace, and there are
16711 children, see if we can determine the namespace from their linkage
16712 name. */
16713 if (cu->language == language_cplus
16714 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
16715 && part_die->die_parent == NULL
16716 && part_die->has_children
16717 && (part_die->tag == DW_TAG_class_type
16718 || part_die->tag == DW_TAG_structure_type
16719 || part_die->tag == DW_TAG_union_type))
16720 guess_partial_die_structure_name (part_die, cu);
16721
16722 /* GCC might emit a nameless struct or union that has a linkage
16723 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16724 if (part_die->name == NULL
16725 && (part_die->tag == DW_TAG_class_type
16726 || part_die->tag == DW_TAG_interface_type
16727 || part_die->tag == DW_TAG_structure_type
16728 || part_die->tag == DW_TAG_union_type)
16729 && part_die->linkage_name != NULL)
16730 {
16731 char *demangled;
16732
16733 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
16734 if (demangled)
16735 {
16736 const char *base;
16737
16738 /* Strip any leading namespaces/classes, keep only the base name.
16739 DW_AT_name for named DIEs does not contain the prefixes. */
16740 base = strrchr (demangled, ':');
16741 if (base && base > demangled && base[-1] == ':')
16742 base++;
16743 else
16744 base = demangled;
16745
16746 part_die->name
16747 = ((const char *)
16748 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
16749 base, strlen (base)));
16750 xfree (demangled);
16751 }
16752 }
16753
16754 part_die->fixup_called = 1;
16755 }
16756
16757 /* Read an attribute value described by an attribute form. */
16758
16759 static const gdb_byte *
16760 read_attribute_value (const struct die_reader_specs *reader,
16761 struct attribute *attr, unsigned form,
16762 LONGEST implicit_const, const gdb_byte *info_ptr)
16763 {
16764 struct dwarf2_cu *cu = reader->cu;
16765 struct objfile *objfile = cu->objfile;
16766 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16767 bfd *abfd = reader->abfd;
16768 struct comp_unit_head *cu_header = &cu->header;
16769 unsigned int bytes_read;
16770 struct dwarf_block *blk;
16771
16772 attr->form = (enum dwarf_form) form;
16773 switch (form)
16774 {
16775 case DW_FORM_ref_addr:
16776 if (cu->header.version == 2)
16777 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
16778 else
16779 DW_UNSND (attr) = read_offset (abfd, info_ptr,
16780 &cu->header, &bytes_read);
16781 info_ptr += bytes_read;
16782 break;
16783 case DW_FORM_GNU_ref_alt:
16784 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
16785 info_ptr += bytes_read;
16786 break;
16787 case DW_FORM_addr:
16788 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
16789 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
16790 info_ptr += bytes_read;
16791 break;
16792 case DW_FORM_block2:
16793 blk = dwarf_alloc_block (cu);
16794 blk->size = read_2_bytes (abfd, info_ptr);
16795 info_ptr += 2;
16796 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16797 info_ptr += blk->size;
16798 DW_BLOCK (attr) = blk;
16799 break;
16800 case DW_FORM_block4:
16801 blk = dwarf_alloc_block (cu);
16802 blk->size = read_4_bytes (abfd, info_ptr);
16803 info_ptr += 4;
16804 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16805 info_ptr += blk->size;
16806 DW_BLOCK (attr) = blk;
16807 break;
16808 case DW_FORM_data2:
16809 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
16810 info_ptr += 2;
16811 break;
16812 case DW_FORM_data4:
16813 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
16814 info_ptr += 4;
16815 break;
16816 case DW_FORM_data8:
16817 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
16818 info_ptr += 8;
16819 break;
16820 case DW_FORM_data16:
16821 blk = dwarf_alloc_block (cu);
16822 blk->size = 16;
16823 blk->data = read_n_bytes (abfd, info_ptr, 16);
16824 info_ptr += 16;
16825 DW_BLOCK (attr) = blk;
16826 break;
16827 case DW_FORM_sec_offset:
16828 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
16829 info_ptr += bytes_read;
16830 break;
16831 case DW_FORM_string:
16832 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
16833 DW_STRING_IS_CANONICAL (attr) = 0;
16834 info_ptr += bytes_read;
16835 break;
16836 case DW_FORM_strp:
16837 if (!cu->per_cu->is_dwz)
16838 {
16839 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
16840 &bytes_read);
16841 DW_STRING_IS_CANONICAL (attr) = 0;
16842 info_ptr += bytes_read;
16843 break;
16844 }
16845 /* FALLTHROUGH */
16846 case DW_FORM_line_strp:
16847 if (!cu->per_cu->is_dwz)
16848 {
16849 DW_STRING (attr) = read_indirect_line_string (abfd, info_ptr,
16850 cu_header, &bytes_read);
16851 DW_STRING_IS_CANONICAL (attr) = 0;
16852 info_ptr += bytes_read;
16853 break;
16854 }
16855 /* FALLTHROUGH */
16856 case DW_FORM_GNU_strp_alt:
16857 {
16858 struct dwz_file *dwz = dwarf2_get_dwz_file ();
16859 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
16860 &bytes_read);
16861
16862 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
16863 DW_STRING_IS_CANONICAL (attr) = 0;
16864 info_ptr += bytes_read;
16865 }
16866 break;
16867 case DW_FORM_exprloc:
16868 case DW_FORM_block:
16869 blk = dwarf_alloc_block (cu);
16870 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16871 info_ptr += bytes_read;
16872 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16873 info_ptr += blk->size;
16874 DW_BLOCK (attr) = blk;
16875 break;
16876 case DW_FORM_block1:
16877 blk = dwarf_alloc_block (cu);
16878 blk->size = read_1_byte (abfd, info_ptr);
16879 info_ptr += 1;
16880 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16881 info_ptr += blk->size;
16882 DW_BLOCK (attr) = blk;
16883 break;
16884 case DW_FORM_data1:
16885 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
16886 info_ptr += 1;
16887 break;
16888 case DW_FORM_flag:
16889 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
16890 info_ptr += 1;
16891 break;
16892 case DW_FORM_flag_present:
16893 DW_UNSND (attr) = 1;
16894 break;
16895 case DW_FORM_sdata:
16896 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
16897 info_ptr += bytes_read;
16898 break;
16899 case DW_FORM_udata:
16900 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16901 info_ptr += bytes_read;
16902 break;
16903 case DW_FORM_ref1:
16904 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
16905 + read_1_byte (abfd, info_ptr));
16906 info_ptr += 1;
16907 break;
16908 case DW_FORM_ref2:
16909 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
16910 + read_2_bytes (abfd, info_ptr));
16911 info_ptr += 2;
16912 break;
16913 case DW_FORM_ref4:
16914 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
16915 + read_4_bytes (abfd, info_ptr));
16916 info_ptr += 4;
16917 break;
16918 case DW_FORM_ref8:
16919 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
16920 + read_8_bytes (abfd, info_ptr));
16921 info_ptr += 8;
16922 break;
16923 case DW_FORM_ref_sig8:
16924 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
16925 info_ptr += 8;
16926 break;
16927 case DW_FORM_ref_udata:
16928 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
16929 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
16930 info_ptr += bytes_read;
16931 break;
16932 case DW_FORM_indirect:
16933 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16934 info_ptr += bytes_read;
16935 if (form == DW_FORM_implicit_const)
16936 {
16937 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
16938 info_ptr += bytes_read;
16939 }
16940 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
16941 info_ptr);
16942 break;
16943 case DW_FORM_implicit_const:
16944 DW_SND (attr) = implicit_const;
16945 break;
16946 case DW_FORM_GNU_addr_index:
16947 if (reader->dwo_file == NULL)
16948 {
16949 /* For now flag a hard error.
16950 Later we can turn this into a complaint. */
16951 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
16952 dwarf_form_name (form),
16953 bfd_get_filename (abfd));
16954 }
16955 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
16956 info_ptr += bytes_read;
16957 break;
16958 case DW_FORM_GNU_str_index:
16959 if (reader->dwo_file == NULL)
16960 {
16961 /* For now flag a hard error.
16962 Later we can turn this into a complaint if warranted. */
16963 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
16964 dwarf_form_name (form),
16965 bfd_get_filename (abfd));
16966 }
16967 {
16968 ULONGEST str_index =
16969 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16970
16971 DW_STRING (attr) = read_str_index (reader, str_index);
16972 DW_STRING_IS_CANONICAL (attr) = 0;
16973 info_ptr += bytes_read;
16974 }
16975 break;
16976 default:
16977 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
16978 dwarf_form_name (form),
16979 bfd_get_filename (abfd));
16980 }
16981
16982 /* Super hack. */
16983 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
16984 attr->form = DW_FORM_GNU_ref_alt;
16985
16986 /* We have seen instances where the compiler tried to emit a byte
16987 size attribute of -1 which ended up being encoded as an unsigned
16988 0xffffffff. Although 0xffffffff is technically a valid size value,
16989 an object of this size seems pretty unlikely so we can relatively
16990 safely treat these cases as if the size attribute was invalid and
16991 treat them as zero by default. */
16992 if (attr->name == DW_AT_byte_size
16993 && form == DW_FORM_data4
16994 && DW_UNSND (attr) >= 0xffffffff)
16995 {
16996 complaint
16997 (&symfile_complaints,
16998 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
16999 hex_string (DW_UNSND (attr)));
17000 DW_UNSND (attr) = 0;
17001 }
17002
17003 return info_ptr;
17004 }
17005
17006 /* Read an attribute described by an abbreviated attribute. */
17007
17008 static const gdb_byte *
17009 read_attribute (const struct die_reader_specs *reader,
17010 struct attribute *attr, struct attr_abbrev *abbrev,
17011 const gdb_byte *info_ptr)
17012 {
17013 attr->name = abbrev->name;
17014 return read_attribute_value (reader, attr, abbrev->form,
17015 abbrev->implicit_const, info_ptr);
17016 }
17017
17018 /* Read dwarf information from a buffer. */
17019
17020 static unsigned int
17021 read_1_byte (bfd *abfd, const gdb_byte *buf)
17022 {
17023 return bfd_get_8 (abfd, buf);
17024 }
17025
17026 static int
17027 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
17028 {
17029 return bfd_get_signed_8 (abfd, buf);
17030 }
17031
17032 static unsigned int
17033 read_2_bytes (bfd *abfd, const gdb_byte *buf)
17034 {
17035 return bfd_get_16 (abfd, buf);
17036 }
17037
17038 static int
17039 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
17040 {
17041 return bfd_get_signed_16 (abfd, buf);
17042 }
17043
17044 static unsigned int
17045 read_4_bytes (bfd *abfd, const gdb_byte *buf)
17046 {
17047 return bfd_get_32 (abfd, buf);
17048 }
17049
17050 static int
17051 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
17052 {
17053 return bfd_get_signed_32 (abfd, buf);
17054 }
17055
17056 static ULONGEST
17057 read_8_bytes (bfd *abfd, const gdb_byte *buf)
17058 {
17059 return bfd_get_64 (abfd, buf);
17060 }
17061
17062 static CORE_ADDR
17063 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
17064 unsigned int *bytes_read)
17065 {
17066 struct comp_unit_head *cu_header = &cu->header;
17067 CORE_ADDR retval = 0;
17068
17069 if (cu_header->signed_addr_p)
17070 {
17071 switch (cu_header->addr_size)
17072 {
17073 case 2:
17074 retval = bfd_get_signed_16 (abfd, buf);
17075 break;
17076 case 4:
17077 retval = bfd_get_signed_32 (abfd, buf);
17078 break;
17079 case 8:
17080 retval = bfd_get_signed_64 (abfd, buf);
17081 break;
17082 default:
17083 internal_error (__FILE__, __LINE__,
17084 _("read_address: bad switch, signed [in module %s]"),
17085 bfd_get_filename (abfd));
17086 }
17087 }
17088 else
17089 {
17090 switch (cu_header->addr_size)
17091 {
17092 case 2:
17093 retval = bfd_get_16 (abfd, buf);
17094 break;
17095 case 4:
17096 retval = bfd_get_32 (abfd, buf);
17097 break;
17098 case 8:
17099 retval = bfd_get_64 (abfd, buf);
17100 break;
17101 default:
17102 internal_error (__FILE__, __LINE__,
17103 _("read_address: bad switch, "
17104 "unsigned [in module %s]"),
17105 bfd_get_filename (abfd));
17106 }
17107 }
17108
17109 *bytes_read = cu_header->addr_size;
17110 return retval;
17111 }
17112
17113 /* Read the initial length from a section. The (draft) DWARF 3
17114 specification allows the initial length to take up either 4 bytes
17115 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
17116 bytes describe the length and all offsets will be 8 bytes in length
17117 instead of 4.
17118
17119 An older, non-standard 64-bit format is also handled by this
17120 function. The older format in question stores the initial length
17121 as an 8-byte quantity without an escape value. Lengths greater
17122 than 2^32 aren't very common which means that the initial 4 bytes
17123 is almost always zero. Since a length value of zero doesn't make
17124 sense for the 32-bit format, this initial zero can be considered to
17125 be an escape value which indicates the presence of the older 64-bit
17126 format. As written, the code can't detect (old format) lengths
17127 greater than 4GB. If it becomes necessary to handle lengths
17128 somewhat larger than 4GB, we could allow other small values (such
17129 as the non-sensical values of 1, 2, and 3) to also be used as
17130 escape values indicating the presence of the old format.
17131
17132 The value returned via bytes_read should be used to increment the
17133 relevant pointer after calling read_initial_length().
17134
17135 [ Note: read_initial_length() and read_offset() are based on the
17136 document entitled "DWARF Debugging Information Format", revision
17137 3, draft 8, dated November 19, 2001. This document was obtained
17138 from:
17139
17140 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
17141
17142 This document is only a draft and is subject to change. (So beware.)
17143
17144 Details regarding the older, non-standard 64-bit format were
17145 determined empirically by examining 64-bit ELF files produced by
17146 the SGI toolchain on an IRIX 6.5 machine.
17147
17148 - Kevin, July 16, 2002
17149 ] */
17150
17151 static LONGEST
17152 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
17153 {
17154 LONGEST length = bfd_get_32 (abfd, buf);
17155
17156 if (length == 0xffffffff)
17157 {
17158 length = bfd_get_64 (abfd, buf + 4);
17159 *bytes_read = 12;
17160 }
17161 else if (length == 0)
17162 {
17163 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
17164 length = bfd_get_64 (abfd, buf);
17165 *bytes_read = 8;
17166 }
17167 else
17168 {
17169 *bytes_read = 4;
17170 }
17171
17172 return length;
17173 }
17174
17175 /* Cover function for read_initial_length.
17176 Returns the length of the object at BUF, and stores the size of the
17177 initial length in *BYTES_READ and stores the size that offsets will be in
17178 *OFFSET_SIZE.
17179 If the initial length size is not equivalent to that specified in
17180 CU_HEADER then issue a complaint.
17181 This is useful when reading non-comp-unit headers. */
17182
17183 static LONGEST
17184 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
17185 const struct comp_unit_head *cu_header,
17186 unsigned int *bytes_read,
17187 unsigned int *offset_size)
17188 {
17189 LONGEST length = read_initial_length (abfd, buf, bytes_read);
17190
17191 gdb_assert (cu_header->initial_length_size == 4
17192 || cu_header->initial_length_size == 8
17193 || cu_header->initial_length_size == 12);
17194
17195 if (cu_header->initial_length_size != *bytes_read)
17196 complaint (&symfile_complaints,
17197 _("intermixed 32-bit and 64-bit DWARF sections"));
17198
17199 *offset_size = (*bytes_read == 4) ? 4 : 8;
17200 return length;
17201 }
17202
17203 /* Read an offset from the data stream. The size of the offset is
17204 given by cu_header->offset_size. */
17205
17206 static LONGEST
17207 read_offset (bfd *abfd, const gdb_byte *buf,
17208 const struct comp_unit_head *cu_header,
17209 unsigned int *bytes_read)
17210 {
17211 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
17212
17213 *bytes_read = cu_header->offset_size;
17214 return offset;
17215 }
17216
17217 /* Read an offset from the data stream. */
17218
17219 static LONGEST
17220 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
17221 {
17222 LONGEST retval = 0;
17223
17224 switch (offset_size)
17225 {
17226 case 4:
17227 retval = bfd_get_32 (abfd, buf);
17228 break;
17229 case 8:
17230 retval = bfd_get_64 (abfd, buf);
17231 break;
17232 default:
17233 internal_error (__FILE__, __LINE__,
17234 _("read_offset_1: bad switch [in module %s]"),
17235 bfd_get_filename (abfd));
17236 }
17237
17238 return retval;
17239 }
17240
17241 static const gdb_byte *
17242 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
17243 {
17244 /* If the size of a host char is 8 bits, we can return a pointer
17245 to the buffer, otherwise we have to copy the data to a buffer
17246 allocated on the temporary obstack. */
17247 gdb_assert (HOST_CHAR_BIT == 8);
17248 return buf;
17249 }
17250
17251 static const char *
17252 read_direct_string (bfd *abfd, const gdb_byte *buf,
17253 unsigned int *bytes_read_ptr)
17254 {
17255 /* If the size of a host char is 8 bits, we can return a pointer
17256 to the string, otherwise we have to copy the string to a buffer
17257 allocated on the temporary obstack. */
17258 gdb_assert (HOST_CHAR_BIT == 8);
17259 if (*buf == '\0')
17260 {
17261 *bytes_read_ptr = 1;
17262 return NULL;
17263 }
17264 *bytes_read_ptr = strlen ((const char *) buf) + 1;
17265 return (const char *) buf;
17266 }
17267
17268 /* Return pointer to string at section SECT offset STR_OFFSET with error
17269 reporting strings FORM_NAME and SECT_NAME. */
17270
17271 static const char *
17272 read_indirect_string_at_offset_from (bfd *abfd, LONGEST str_offset,
17273 struct dwarf2_section_info *sect,
17274 const char *form_name,
17275 const char *sect_name)
17276 {
17277 dwarf2_read_section (dwarf2_per_objfile->objfile, sect);
17278 if (sect->buffer == NULL)
17279 error (_("%s used without %s section [in module %s]"),
17280 form_name, sect_name, bfd_get_filename (abfd));
17281 if (str_offset >= sect->size)
17282 error (_("%s pointing outside of %s section [in module %s]"),
17283 form_name, sect_name, bfd_get_filename (abfd));
17284 gdb_assert (HOST_CHAR_BIT == 8);
17285 if (sect->buffer[str_offset] == '\0')
17286 return NULL;
17287 return (const char *) (sect->buffer + str_offset);
17288 }
17289
17290 /* Return pointer to string at .debug_str offset STR_OFFSET. */
17291
17292 static const char *
17293 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
17294 {
17295 return read_indirect_string_at_offset_from (abfd, str_offset,
17296 &dwarf2_per_objfile->str,
17297 "DW_FORM_strp", ".debug_str");
17298 }
17299
17300 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
17301
17302 static const char *
17303 read_indirect_line_string_at_offset (bfd *abfd, LONGEST str_offset)
17304 {
17305 return read_indirect_string_at_offset_from (abfd, str_offset,
17306 &dwarf2_per_objfile->line_str,
17307 "DW_FORM_line_strp",
17308 ".debug_line_str");
17309 }
17310
17311 /* Read a string at offset STR_OFFSET in the .debug_str section from
17312 the .dwz file DWZ. Throw an error if the offset is too large. If
17313 the string consists of a single NUL byte, return NULL; otherwise
17314 return a pointer to the string. */
17315
17316 static const char *
17317 read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
17318 {
17319 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
17320
17321 if (dwz->str.buffer == NULL)
17322 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
17323 "section [in module %s]"),
17324 bfd_get_filename (dwz->dwz_bfd));
17325 if (str_offset >= dwz->str.size)
17326 error (_("DW_FORM_GNU_strp_alt pointing outside of "
17327 ".debug_str section [in module %s]"),
17328 bfd_get_filename (dwz->dwz_bfd));
17329 gdb_assert (HOST_CHAR_BIT == 8);
17330 if (dwz->str.buffer[str_offset] == '\0')
17331 return NULL;
17332 return (const char *) (dwz->str.buffer + str_offset);
17333 }
17334
17335 /* Return pointer to string at .debug_str offset as read from BUF.
17336 BUF is assumed to be in a compilation unit described by CU_HEADER.
17337 Return *BYTES_READ_PTR count of bytes read from BUF. */
17338
17339 static const char *
17340 read_indirect_string (bfd *abfd, const gdb_byte *buf,
17341 const struct comp_unit_head *cu_header,
17342 unsigned int *bytes_read_ptr)
17343 {
17344 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
17345
17346 return read_indirect_string_at_offset (abfd, str_offset);
17347 }
17348
17349 /* Return pointer to string at .debug_line_str offset as read from BUF.
17350 BUF is assumed to be in a compilation unit described by CU_HEADER.
17351 Return *BYTES_READ_PTR count of bytes read from BUF. */
17352
17353 static const char *
17354 read_indirect_line_string (bfd *abfd, const gdb_byte *buf,
17355 const struct comp_unit_head *cu_header,
17356 unsigned int *bytes_read_ptr)
17357 {
17358 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
17359
17360 return read_indirect_line_string_at_offset (abfd, str_offset);
17361 }
17362
17363 ULONGEST
17364 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
17365 unsigned int *bytes_read_ptr)
17366 {
17367 ULONGEST result;
17368 unsigned int num_read;
17369 int shift;
17370 unsigned char byte;
17371
17372 result = 0;
17373 shift = 0;
17374 num_read = 0;
17375 while (1)
17376 {
17377 byte = bfd_get_8 (abfd, buf);
17378 buf++;
17379 num_read++;
17380 result |= ((ULONGEST) (byte & 127) << shift);
17381 if ((byte & 128) == 0)
17382 {
17383 break;
17384 }
17385 shift += 7;
17386 }
17387 *bytes_read_ptr = num_read;
17388 return result;
17389 }
17390
17391 static LONGEST
17392 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
17393 unsigned int *bytes_read_ptr)
17394 {
17395 LONGEST result;
17396 int shift, num_read;
17397 unsigned char byte;
17398
17399 result = 0;
17400 shift = 0;
17401 num_read = 0;
17402 while (1)
17403 {
17404 byte = bfd_get_8 (abfd, buf);
17405 buf++;
17406 num_read++;
17407 result |= ((LONGEST) (byte & 127) << shift);
17408 shift += 7;
17409 if ((byte & 128) == 0)
17410 {
17411 break;
17412 }
17413 }
17414 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
17415 result |= -(((LONGEST) 1) << shift);
17416 *bytes_read_ptr = num_read;
17417 return result;
17418 }
17419
17420 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
17421 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
17422 ADDR_SIZE is the size of addresses from the CU header. */
17423
17424 static CORE_ADDR
17425 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
17426 {
17427 struct objfile *objfile = dwarf2_per_objfile->objfile;
17428 bfd *abfd = objfile->obfd;
17429 const gdb_byte *info_ptr;
17430
17431 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
17432 if (dwarf2_per_objfile->addr.buffer == NULL)
17433 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
17434 objfile_name (objfile));
17435 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
17436 error (_("DW_FORM_addr_index pointing outside of "
17437 ".debug_addr section [in module %s]"),
17438 objfile_name (objfile));
17439 info_ptr = (dwarf2_per_objfile->addr.buffer
17440 + addr_base + addr_index * addr_size);
17441 if (addr_size == 4)
17442 return bfd_get_32 (abfd, info_ptr);
17443 else
17444 return bfd_get_64 (abfd, info_ptr);
17445 }
17446
17447 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
17448
17449 static CORE_ADDR
17450 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
17451 {
17452 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
17453 }
17454
17455 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
17456
17457 static CORE_ADDR
17458 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
17459 unsigned int *bytes_read)
17460 {
17461 bfd *abfd = cu->objfile->obfd;
17462 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
17463
17464 return read_addr_index (cu, addr_index);
17465 }
17466
17467 /* Data structure to pass results from dwarf2_read_addr_index_reader
17468 back to dwarf2_read_addr_index. */
17469
17470 struct dwarf2_read_addr_index_data
17471 {
17472 ULONGEST addr_base;
17473 int addr_size;
17474 };
17475
17476 /* die_reader_func for dwarf2_read_addr_index. */
17477
17478 static void
17479 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
17480 const gdb_byte *info_ptr,
17481 struct die_info *comp_unit_die,
17482 int has_children,
17483 void *data)
17484 {
17485 struct dwarf2_cu *cu = reader->cu;
17486 struct dwarf2_read_addr_index_data *aidata =
17487 (struct dwarf2_read_addr_index_data *) data;
17488
17489 aidata->addr_base = cu->addr_base;
17490 aidata->addr_size = cu->header.addr_size;
17491 }
17492
17493 /* Given an index in .debug_addr, fetch the value.
17494 NOTE: This can be called during dwarf expression evaluation,
17495 long after the debug information has been read, and thus per_cu->cu
17496 may no longer exist. */
17497
17498 CORE_ADDR
17499 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
17500 unsigned int addr_index)
17501 {
17502 struct objfile *objfile = per_cu->objfile;
17503 struct dwarf2_cu *cu = per_cu->cu;
17504 ULONGEST addr_base;
17505 int addr_size;
17506
17507 /* This is intended to be called from outside this file. */
17508 dw2_setup (objfile);
17509
17510 /* We need addr_base and addr_size.
17511 If we don't have PER_CU->cu, we have to get it.
17512 Nasty, but the alternative is storing the needed info in PER_CU,
17513 which at this point doesn't seem justified: it's not clear how frequently
17514 it would get used and it would increase the size of every PER_CU.
17515 Entry points like dwarf2_per_cu_addr_size do a similar thing
17516 so we're not in uncharted territory here.
17517 Alas we need to be a bit more complicated as addr_base is contained
17518 in the DIE.
17519
17520 We don't need to read the entire CU(/TU).
17521 We just need the header and top level die.
17522
17523 IWBN to use the aging mechanism to let us lazily later discard the CU.
17524 For now we skip this optimization. */
17525
17526 if (cu != NULL)
17527 {
17528 addr_base = cu->addr_base;
17529 addr_size = cu->header.addr_size;
17530 }
17531 else
17532 {
17533 struct dwarf2_read_addr_index_data aidata;
17534
17535 /* Note: We can't use init_cutu_and_read_dies_simple here,
17536 we need addr_base. */
17537 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
17538 dwarf2_read_addr_index_reader, &aidata);
17539 addr_base = aidata.addr_base;
17540 addr_size = aidata.addr_size;
17541 }
17542
17543 return read_addr_index_1 (addr_index, addr_base, addr_size);
17544 }
17545
17546 /* Given a DW_FORM_GNU_str_index, fetch the string.
17547 This is only used by the Fission support. */
17548
17549 static const char *
17550 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
17551 {
17552 struct objfile *objfile = dwarf2_per_objfile->objfile;
17553 const char *objf_name = objfile_name (objfile);
17554 bfd *abfd = objfile->obfd;
17555 struct dwarf2_cu *cu = reader->cu;
17556 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
17557 struct dwarf2_section_info *str_offsets_section =
17558 &reader->dwo_file->sections.str_offsets;
17559 const gdb_byte *info_ptr;
17560 ULONGEST str_offset;
17561 static const char form_name[] = "DW_FORM_GNU_str_index";
17562
17563 dwarf2_read_section (objfile, str_section);
17564 dwarf2_read_section (objfile, str_offsets_section);
17565 if (str_section->buffer == NULL)
17566 error (_("%s used without .debug_str.dwo section"
17567 " in CU at offset 0x%x [in module %s]"),
17568 form_name, to_underlying (cu->header.sect_off), objf_name);
17569 if (str_offsets_section->buffer == NULL)
17570 error (_("%s used without .debug_str_offsets.dwo section"
17571 " in CU at offset 0x%x [in module %s]"),
17572 form_name, to_underlying (cu->header.sect_off), objf_name);
17573 if (str_index * cu->header.offset_size >= str_offsets_section->size)
17574 error (_("%s pointing outside of .debug_str_offsets.dwo"
17575 " section in CU at offset 0x%x [in module %s]"),
17576 form_name, to_underlying (cu->header.sect_off), objf_name);
17577 info_ptr = (str_offsets_section->buffer
17578 + str_index * cu->header.offset_size);
17579 if (cu->header.offset_size == 4)
17580 str_offset = bfd_get_32 (abfd, info_ptr);
17581 else
17582 str_offset = bfd_get_64 (abfd, info_ptr);
17583 if (str_offset >= str_section->size)
17584 error (_("Offset from %s pointing outside of"
17585 " .debug_str.dwo section in CU at offset 0x%x [in module %s]"),
17586 form_name, to_underlying (cu->header.sect_off), objf_name);
17587 return (const char *) (str_section->buffer + str_offset);
17588 }
17589
17590 /* Return the length of an LEB128 number in BUF. */
17591
17592 static int
17593 leb128_size (const gdb_byte *buf)
17594 {
17595 const gdb_byte *begin = buf;
17596 gdb_byte byte;
17597
17598 while (1)
17599 {
17600 byte = *buf++;
17601 if ((byte & 128) == 0)
17602 return buf - begin;
17603 }
17604 }
17605
17606 static void
17607 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
17608 {
17609 switch (lang)
17610 {
17611 case DW_LANG_C89:
17612 case DW_LANG_C99:
17613 case DW_LANG_C11:
17614 case DW_LANG_C:
17615 case DW_LANG_UPC:
17616 cu->language = language_c;
17617 break;
17618 case DW_LANG_Java:
17619 case DW_LANG_C_plus_plus:
17620 case DW_LANG_C_plus_plus_11:
17621 case DW_LANG_C_plus_plus_14:
17622 cu->language = language_cplus;
17623 break;
17624 case DW_LANG_D:
17625 cu->language = language_d;
17626 break;
17627 case DW_LANG_Fortran77:
17628 case DW_LANG_Fortran90:
17629 case DW_LANG_Fortran95:
17630 case DW_LANG_Fortran03:
17631 case DW_LANG_Fortran08:
17632 cu->language = language_fortran;
17633 break;
17634 case DW_LANG_Go:
17635 cu->language = language_go;
17636 break;
17637 case DW_LANG_Mips_Assembler:
17638 cu->language = language_asm;
17639 break;
17640 case DW_LANG_Ada83:
17641 case DW_LANG_Ada95:
17642 cu->language = language_ada;
17643 break;
17644 case DW_LANG_Modula2:
17645 cu->language = language_m2;
17646 break;
17647 case DW_LANG_Pascal83:
17648 cu->language = language_pascal;
17649 break;
17650 case DW_LANG_ObjC:
17651 cu->language = language_objc;
17652 break;
17653 case DW_LANG_Rust:
17654 case DW_LANG_Rust_old:
17655 cu->language = language_rust;
17656 break;
17657 case DW_LANG_Cobol74:
17658 case DW_LANG_Cobol85:
17659 default:
17660 cu->language = language_minimal;
17661 break;
17662 }
17663 cu->language_defn = language_def (cu->language);
17664 }
17665
17666 /* Return the named attribute or NULL if not there. */
17667
17668 static struct attribute *
17669 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
17670 {
17671 for (;;)
17672 {
17673 unsigned int i;
17674 struct attribute *spec = NULL;
17675
17676 for (i = 0; i < die->num_attrs; ++i)
17677 {
17678 if (die->attrs[i].name == name)
17679 return &die->attrs[i];
17680 if (die->attrs[i].name == DW_AT_specification
17681 || die->attrs[i].name == DW_AT_abstract_origin)
17682 spec = &die->attrs[i];
17683 }
17684
17685 if (!spec)
17686 break;
17687
17688 die = follow_die_ref (die, spec, &cu);
17689 }
17690
17691 return NULL;
17692 }
17693
17694 /* Return the named attribute or NULL if not there,
17695 but do not follow DW_AT_specification, etc.
17696 This is for use in contexts where we're reading .debug_types dies.
17697 Following DW_AT_specification, DW_AT_abstract_origin will take us
17698 back up the chain, and we want to go down. */
17699
17700 static struct attribute *
17701 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
17702 {
17703 unsigned int i;
17704
17705 for (i = 0; i < die->num_attrs; ++i)
17706 if (die->attrs[i].name == name)
17707 return &die->attrs[i];
17708
17709 return NULL;
17710 }
17711
17712 /* Return the string associated with a string-typed attribute, or NULL if it
17713 is either not found or is of an incorrect type. */
17714
17715 static const char *
17716 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
17717 {
17718 struct attribute *attr;
17719 const char *str = NULL;
17720
17721 attr = dwarf2_attr (die, name, cu);
17722
17723 if (attr != NULL)
17724 {
17725 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
17726 || attr->form == DW_FORM_string
17727 || attr->form == DW_FORM_GNU_str_index
17728 || attr->form == DW_FORM_GNU_strp_alt)
17729 str = DW_STRING (attr);
17730 else
17731 complaint (&symfile_complaints,
17732 _("string type expected for attribute %s for "
17733 "DIE at 0x%x in module %s"),
17734 dwarf_attr_name (name), to_underlying (die->sect_off),
17735 objfile_name (cu->objfile));
17736 }
17737
17738 return str;
17739 }
17740
17741 /* Return non-zero iff the attribute NAME is defined for the given DIE,
17742 and holds a non-zero value. This function should only be used for
17743 DW_FORM_flag or DW_FORM_flag_present attributes. */
17744
17745 static int
17746 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
17747 {
17748 struct attribute *attr = dwarf2_attr (die, name, cu);
17749
17750 return (attr && DW_UNSND (attr));
17751 }
17752
17753 static int
17754 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
17755 {
17756 /* A DIE is a declaration if it has a DW_AT_declaration attribute
17757 which value is non-zero. However, we have to be careful with
17758 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
17759 (via dwarf2_flag_true_p) follows this attribute. So we may
17760 end up accidently finding a declaration attribute that belongs
17761 to a different DIE referenced by the specification attribute,
17762 even though the given DIE does not have a declaration attribute. */
17763 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
17764 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
17765 }
17766
17767 /* Return the die giving the specification for DIE, if there is
17768 one. *SPEC_CU is the CU containing DIE on input, and the CU
17769 containing the return value on output. If there is no
17770 specification, but there is an abstract origin, that is
17771 returned. */
17772
17773 static struct die_info *
17774 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
17775 {
17776 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
17777 *spec_cu);
17778
17779 if (spec_attr == NULL)
17780 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
17781
17782 if (spec_attr == NULL)
17783 return NULL;
17784 else
17785 return follow_die_ref (die, spec_attr, spec_cu);
17786 }
17787
17788 /* Stub for free_line_header to match void * callback types. */
17789
17790 static void
17791 free_line_header_voidp (void *arg)
17792 {
17793 struct line_header *lh = (struct line_header *) arg;
17794
17795 delete lh;
17796 }
17797
17798 void
17799 line_header::add_include_dir (const char *include_dir)
17800 {
17801 if (dwarf_line_debug >= 2)
17802 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
17803 include_dirs.size () + 1, include_dir);
17804
17805 include_dirs.push_back (include_dir);
17806 }
17807
17808 void
17809 line_header::add_file_name (const char *name,
17810 dir_index d_index,
17811 unsigned int mod_time,
17812 unsigned int length)
17813 {
17814 if (dwarf_line_debug >= 2)
17815 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
17816 (unsigned) file_names.size () + 1, name);
17817
17818 file_names.emplace_back (name, d_index, mod_time, length);
17819 }
17820
17821 /* A convenience function to find the proper .debug_line section for a CU. */
17822
17823 static struct dwarf2_section_info *
17824 get_debug_line_section (struct dwarf2_cu *cu)
17825 {
17826 struct dwarf2_section_info *section;
17827
17828 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
17829 DWO file. */
17830 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17831 section = &cu->dwo_unit->dwo_file->sections.line;
17832 else if (cu->per_cu->is_dwz)
17833 {
17834 struct dwz_file *dwz = dwarf2_get_dwz_file ();
17835
17836 section = &dwz->line;
17837 }
17838 else
17839 section = &dwarf2_per_objfile->line;
17840
17841 return section;
17842 }
17843
17844 /* Read directory or file name entry format, starting with byte of
17845 format count entries, ULEB128 pairs of entry formats, ULEB128 of
17846 entries count and the entries themselves in the described entry
17847 format. */
17848
17849 static void
17850 read_formatted_entries (bfd *abfd, const gdb_byte **bufp,
17851 struct line_header *lh,
17852 const struct comp_unit_head *cu_header,
17853 void (*callback) (struct line_header *lh,
17854 const char *name,
17855 dir_index d_index,
17856 unsigned int mod_time,
17857 unsigned int length))
17858 {
17859 gdb_byte format_count, formati;
17860 ULONGEST data_count, datai;
17861 const gdb_byte *buf = *bufp;
17862 const gdb_byte *format_header_data;
17863 int i;
17864 unsigned int bytes_read;
17865
17866 format_count = read_1_byte (abfd, buf);
17867 buf += 1;
17868 format_header_data = buf;
17869 for (formati = 0; formati < format_count; formati++)
17870 {
17871 read_unsigned_leb128 (abfd, buf, &bytes_read);
17872 buf += bytes_read;
17873 read_unsigned_leb128 (abfd, buf, &bytes_read);
17874 buf += bytes_read;
17875 }
17876
17877 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
17878 buf += bytes_read;
17879 for (datai = 0; datai < data_count; datai++)
17880 {
17881 const gdb_byte *format = format_header_data;
17882 struct file_entry fe;
17883
17884 for (formati = 0; formati < format_count; formati++)
17885 {
17886 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
17887 format += bytes_read;
17888
17889 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
17890 format += bytes_read;
17891
17892 gdb::optional<const char *> string;
17893 gdb::optional<unsigned int> uint;
17894
17895 switch (form)
17896 {
17897 case DW_FORM_string:
17898 string.emplace (read_direct_string (abfd, buf, &bytes_read));
17899 buf += bytes_read;
17900 break;
17901
17902 case DW_FORM_line_strp:
17903 string.emplace (read_indirect_line_string (abfd, buf,
17904 cu_header,
17905 &bytes_read));
17906 buf += bytes_read;
17907 break;
17908
17909 case DW_FORM_data1:
17910 uint.emplace (read_1_byte (abfd, buf));
17911 buf += 1;
17912 break;
17913
17914 case DW_FORM_data2:
17915 uint.emplace (read_2_bytes (abfd, buf));
17916 buf += 2;
17917 break;
17918
17919 case DW_FORM_data4:
17920 uint.emplace (read_4_bytes (abfd, buf));
17921 buf += 4;
17922 break;
17923
17924 case DW_FORM_data8:
17925 uint.emplace (read_8_bytes (abfd, buf));
17926 buf += 8;
17927 break;
17928
17929 case DW_FORM_udata:
17930 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
17931 buf += bytes_read;
17932 break;
17933
17934 case DW_FORM_block:
17935 /* It is valid only for DW_LNCT_timestamp which is ignored by
17936 current GDB. */
17937 break;
17938 }
17939
17940 switch (content_type)
17941 {
17942 case DW_LNCT_path:
17943 if (string.has_value ())
17944 fe.name = *string;
17945 break;
17946 case DW_LNCT_directory_index:
17947 if (uint.has_value ())
17948 fe.d_index = (dir_index) *uint;
17949 break;
17950 case DW_LNCT_timestamp:
17951 if (uint.has_value ())
17952 fe.mod_time = *uint;
17953 break;
17954 case DW_LNCT_size:
17955 if (uint.has_value ())
17956 fe.length = *uint;
17957 break;
17958 case DW_LNCT_MD5:
17959 break;
17960 default:
17961 complaint (&symfile_complaints,
17962 _("Unknown format content type %s"),
17963 pulongest (content_type));
17964 }
17965 }
17966
17967 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
17968 }
17969
17970 *bufp = buf;
17971 }
17972
17973 /* Read the statement program header starting at OFFSET in
17974 .debug_line, or .debug_line.dwo. Return a pointer
17975 to a struct line_header, allocated using xmalloc.
17976 Returns NULL if there is a problem reading the header, e.g., if it
17977 has a version we don't understand.
17978
17979 NOTE: the strings in the include directory and file name tables of
17980 the returned object point into the dwarf line section buffer,
17981 and must not be freed. */
17982
17983 static line_header_up
17984 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
17985 {
17986 const gdb_byte *line_ptr;
17987 unsigned int bytes_read, offset_size;
17988 int i;
17989 const char *cur_dir, *cur_file;
17990 struct dwarf2_section_info *section;
17991 bfd *abfd;
17992
17993 section = get_debug_line_section (cu);
17994 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
17995 if (section->buffer == NULL)
17996 {
17997 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17998 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
17999 else
18000 complaint (&symfile_complaints, _("missing .debug_line section"));
18001 return 0;
18002 }
18003
18004 /* We can't do this until we know the section is non-empty.
18005 Only then do we know we have such a section. */
18006 abfd = get_section_bfd_owner (section);
18007
18008 /* Make sure that at least there's room for the total_length field.
18009 That could be 12 bytes long, but we're just going to fudge that. */
18010 if (to_underlying (sect_off) + 4 >= section->size)
18011 {
18012 dwarf2_statement_list_fits_in_line_number_section_complaint ();
18013 return 0;
18014 }
18015
18016 line_header_up lh (new line_header ());
18017
18018 lh->sect_off = sect_off;
18019 lh->offset_in_dwz = cu->per_cu->is_dwz;
18020
18021 line_ptr = section->buffer + to_underlying (sect_off);
18022
18023 /* Read in the header. */
18024 lh->total_length =
18025 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
18026 &bytes_read, &offset_size);
18027 line_ptr += bytes_read;
18028 if (line_ptr + lh->total_length > (section->buffer + section->size))
18029 {
18030 dwarf2_statement_list_fits_in_line_number_section_complaint ();
18031 return 0;
18032 }
18033 lh->statement_program_end = line_ptr + lh->total_length;
18034 lh->version = read_2_bytes (abfd, line_ptr);
18035 line_ptr += 2;
18036 if (lh->version > 5)
18037 {
18038 /* This is a version we don't understand. The format could have
18039 changed in ways we don't handle properly so just punt. */
18040 complaint (&symfile_complaints,
18041 _("unsupported version in .debug_line section"));
18042 return NULL;
18043 }
18044 if (lh->version >= 5)
18045 {
18046 gdb_byte segment_selector_size;
18047
18048 /* Skip address size. */
18049 read_1_byte (abfd, line_ptr);
18050 line_ptr += 1;
18051
18052 segment_selector_size = read_1_byte (abfd, line_ptr);
18053 line_ptr += 1;
18054 if (segment_selector_size != 0)
18055 {
18056 complaint (&symfile_complaints,
18057 _("unsupported segment selector size %u "
18058 "in .debug_line section"),
18059 segment_selector_size);
18060 return NULL;
18061 }
18062 }
18063 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
18064 line_ptr += offset_size;
18065 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
18066 line_ptr += 1;
18067 if (lh->version >= 4)
18068 {
18069 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
18070 line_ptr += 1;
18071 }
18072 else
18073 lh->maximum_ops_per_instruction = 1;
18074
18075 if (lh->maximum_ops_per_instruction == 0)
18076 {
18077 lh->maximum_ops_per_instruction = 1;
18078 complaint (&symfile_complaints,
18079 _("invalid maximum_ops_per_instruction "
18080 "in `.debug_line' section"));
18081 }
18082
18083 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
18084 line_ptr += 1;
18085 lh->line_base = read_1_signed_byte (abfd, line_ptr);
18086 line_ptr += 1;
18087 lh->line_range = read_1_byte (abfd, line_ptr);
18088 line_ptr += 1;
18089 lh->opcode_base = read_1_byte (abfd, line_ptr);
18090 line_ptr += 1;
18091 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
18092
18093 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
18094 for (i = 1; i < lh->opcode_base; ++i)
18095 {
18096 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
18097 line_ptr += 1;
18098 }
18099
18100 if (lh->version >= 5)
18101 {
18102 /* Read directory table. */
18103 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
18104 [] (struct line_header *lh, const char *name,
18105 dir_index d_index, unsigned int mod_time,
18106 unsigned int length)
18107 {
18108 lh->add_include_dir (name);
18109 });
18110
18111 /* Read file name table. */
18112 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
18113 [] (struct line_header *lh, const char *name,
18114 dir_index d_index, unsigned int mod_time,
18115 unsigned int length)
18116 {
18117 lh->add_file_name (name, d_index, mod_time, length);
18118 });
18119 }
18120 else
18121 {
18122 /* Read directory table. */
18123 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
18124 {
18125 line_ptr += bytes_read;
18126 lh->add_include_dir (cur_dir);
18127 }
18128 line_ptr += bytes_read;
18129
18130 /* Read file name table. */
18131 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
18132 {
18133 unsigned int mod_time, length;
18134 dir_index d_index;
18135
18136 line_ptr += bytes_read;
18137 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18138 line_ptr += bytes_read;
18139 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18140 line_ptr += bytes_read;
18141 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18142 line_ptr += bytes_read;
18143
18144 lh->add_file_name (cur_file, d_index, mod_time, length);
18145 }
18146 line_ptr += bytes_read;
18147 }
18148 lh->statement_program_start = line_ptr;
18149
18150 if (line_ptr > (section->buffer + section->size))
18151 complaint (&symfile_complaints,
18152 _("line number info header doesn't "
18153 "fit in `.debug_line' section"));
18154
18155 return lh;
18156 }
18157
18158 /* Subroutine of dwarf_decode_lines to simplify it.
18159 Return the file name of the psymtab for included file FILE_INDEX
18160 in line header LH of PST.
18161 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
18162 If space for the result is malloc'd, it will be freed by a cleanup.
18163 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
18164
18165 The function creates dangling cleanup registration. */
18166
18167 static const char *
18168 psymtab_include_file_name (const struct line_header *lh, int file_index,
18169 const struct partial_symtab *pst,
18170 const char *comp_dir)
18171 {
18172 const file_entry &fe = lh->file_names[file_index];
18173 const char *include_name = fe.name;
18174 const char *include_name_to_compare = include_name;
18175 const char *pst_filename;
18176 char *copied_name = NULL;
18177 int file_is_pst;
18178
18179 const char *dir_name = fe.include_dir (lh);
18180
18181 if (!IS_ABSOLUTE_PATH (include_name)
18182 && (dir_name != NULL || comp_dir != NULL))
18183 {
18184 /* Avoid creating a duplicate psymtab for PST.
18185 We do this by comparing INCLUDE_NAME and PST_FILENAME.
18186 Before we do the comparison, however, we need to account
18187 for DIR_NAME and COMP_DIR.
18188 First prepend dir_name (if non-NULL). If we still don't
18189 have an absolute path prepend comp_dir (if non-NULL).
18190 However, the directory we record in the include-file's
18191 psymtab does not contain COMP_DIR (to match the
18192 corresponding symtab(s)).
18193
18194 Example:
18195
18196 bash$ cd /tmp
18197 bash$ gcc -g ./hello.c
18198 include_name = "hello.c"
18199 dir_name = "."
18200 DW_AT_comp_dir = comp_dir = "/tmp"
18201 DW_AT_name = "./hello.c"
18202
18203 */
18204
18205 if (dir_name != NULL)
18206 {
18207 char *tem = concat (dir_name, SLASH_STRING,
18208 include_name, (char *)NULL);
18209
18210 make_cleanup (xfree, tem);
18211 include_name = tem;
18212 include_name_to_compare = include_name;
18213 }
18214 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
18215 {
18216 char *tem = concat (comp_dir, SLASH_STRING,
18217 include_name, (char *)NULL);
18218
18219 make_cleanup (xfree, tem);
18220 include_name_to_compare = tem;
18221 }
18222 }
18223
18224 pst_filename = pst->filename;
18225 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
18226 {
18227 copied_name = concat (pst->dirname, SLASH_STRING,
18228 pst_filename, (char *)NULL);
18229 pst_filename = copied_name;
18230 }
18231
18232 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
18233
18234 if (copied_name != NULL)
18235 xfree (copied_name);
18236
18237 if (file_is_pst)
18238 return NULL;
18239 return include_name;
18240 }
18241
18242 /* State machine to track the state of the line number program. */
18243
18244 class lnp_state_machine
18245 {
18246 public:
18247 /* Initialize a machine state for the start of a line number
18248 program. */
18249 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
18250
18251 file_entry *current_file ()
18252 {
18253 /* lh->file_names is 0-based, but the file name numbers in the
18254 statement program are 1-based. */
18255 return m_line_header->file_name_at (m_file);
18256 }
18257
18258 /* Record the line in the state machine. END_SEQUENCE is true if
18259 we're processing the end of a sequence. */
18260 void record_line (bool end_sequence);
18261
18262 /* Check address and if invalid nop-out the rest of the lines in this
18263 sequence. */
18264 void check_line_address (struct dwarf2_cu *cu,
18265 const gdb_byte *line_ptr,
18266 CORE_ADDR lowpc, CORE_ADDR address);
18267
18268 void handle_set_discriminator (unsigned int discriminator)
18269 {
18270 m_discriminator = discriminator;
18271 m_line_has_non_zero_discriminator |= discriminator != 0;
18272 }
18273
18274 /* Handle DW_LNE_set_address. */
18275 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
18276 {
18277 m_op_index = 0;
18278 address += baseaddr;
18279 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
18280 }
18281
18282 /* Handle DW_LNS_advance_pc. */
18283 void handle_advance_pc (CORE_ADDR adjust);
18284
18285 /* Handle a special opcode. */
18286 void handle_special_opcode (unsigned char op_code);
18287
18288 /* Handle DW_LNS_advance_line. */
18289 void handle_advance_line (int line_delta)
18290 {
18291 advance_line (line_delta);
18292 }
18293
18294 /* Handle DW_LNS_set_file. */
18295 void handle_set_file (file_name_index file);
18296
18297 /* Handle DW_LNS_negate_stmt. */
18298 void handle_negate_stmt ()
18299 {
18300 m_is_stmt = !m_is_stmt;
18301 }
18302
18303 /* Handle DW_LNS_const_add_pc. */
18304 void handle_const_add_pc ();
18305
18306 /* Handle DW_LNS_fixed_advance_pc. */
18307 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
18308 {
18309 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18310 m_op_index = 0;
18311 }
18312
18313 /* Handle DW_LNS_copy. */
18314 void handle_copy ()
18315 {
18316 record_line (false);
18317 m_discriminator = 0;
18318 }
18319
18320 /* Handle DW_LNE_end_sequence. */
18321 void handle_end_sequence ()
18322 {
18323 m_record_line_callback = ::record_line;
18324 }
18325
18326 private:
18327 /* Advance the line by LINE_DELTA. */
18328 void advance_line (int line_delta)
18329 {
18330 m_line += line_delta;
18331
18332 if (line_delta != 0)
18333 m_line_has_non_zero_discriminator = m_discriminator != 0;
18334 }
18335
18336 gdbarch *m_gdbarch;
18337
18338 /* True if we're recording lines.
18339 Otherwise we're building partial symtabs and are just interested in
18340 finding include files mentioned by the line number program. */
18341 bool m_record_lines_p;
18342
18343 /* The line number header. */
18344 line_header *m_line_header;
18345
18346 /* These are part of the standard DWARF line number state machine,
18347 and initialized according to the DWARF spec. */
18348
18349 unsigned char m_op_index = 0;
18350 /* The line table index (1-based) of the current file. */
18351 file_name_index m_file = (file_name_index) 1;
18352 unsigned int m_line = 1;
18353
18354 /* These are initialized in the constructor. */
18355
18356 CORE_ADDR m_address;
18357 bool m_is_stmt;
18358 unsigned int m_discriminator;
18359
18360 /* Additional bits of state we need to track. */
18361
18362 /* The last file that we called dwarf2_start_subfile for.
18363 This is only used for TLLs. */
18364 unsigned int m_last_file = 0;
18365 /* The last file a line number was recorded for. */
18366 struct subfile *m_last_subfile = NULL;
18367
18368 /* The function to call to record a line. */
18369 record_line_ftype *m_record_line_callback = NULL;
18370
18371 /* The last line number that was recorded, used to coalesce
18372 consecutive entries for the same line. This can happen, for
18373 example, when discriminators are present. PR 17276. */
18374 unsigned int m_last_line = 0;
18375 bool m_line_has_non_zero_discriminator = false;
18376 };
18377
18378 void
18379 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
18380 {
18381 CORE_ADDR addr_adj = (((m_op_index + adjust)
18382 / m_line_header->maximum_ops_per_instruction)
18383 * m_line_header->minimum_instruction_length);
18384 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18385 m_op_index = ((m_op_index + adjust)
18386 % m_line_header->maximum_ops_per_instruction);
18387 }
18388
18389 void
18390 lnp_state_machine::handle_special_opcode (unsigned char op_code)
18391 {
18392 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
18393 CORE_ADDR addr_adj = (((m_op_index
18394 + (adj_opcode / m_line_header->line_range))
18395 / m_line_header->maximum_ops_per_instruction)
18396 * m_line_header->minimum_instruction_length);
18397 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18398 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
18399 % m_line_header->maximum_ops_per_instruction);
18400
18401 int line_delta = (m_line_header->line_base
18402 + (adj_opcode % m_line_header->line_range));
18403 advance_line (line_delta);
18404 record_line (false);
18405 m_discriminator = 0;
18406 }
18407
18408 void
18409 lnp_state_machine::handle_set_file (file_name_index file)
18410 {
18411 m_file = file;
18412
18413 const file_entry *fe = current_file ();
18414 if (fe == NULL)
18415 dwarf2_debug_line_missing_file_complaint ();
18416 else if (m_record_lines_p)
18417 {
18418 const char *dir = fe->include_dir (m_line_header);
18419
18420 m_last_subfile = current_subfile;
18421 m_line_has_non_zero_discriminator = m_discriminator != 0;
18422 dwarf2_start_subfile (fe->name, dir);
18423 }
18424 }
18425
18426 void
18427 lnp_state_machine::handle_const_add_pc ()
18428 {
18429 CORE_ADDR adjust
18430 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
18431
18432 CORE_ADDR addr_adj
18433 = (((m_op_index + adjust)
18434 / m_line_header->maximum_ops_per_instruction)
18435 * m_line_header->minimum_instruction_length);
18436
18437 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18438 m_op_index = ((m_op_index + adjust)
18439 % m_line_header->maximum_ops_per_instruction);
18440 }
18441
18442 /* Ignore this record_line request. */
18443
18444 static void
18445 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
18446 {
18447 return;
18448 }
18449
18450 /* Return non-zero if we should add LINE to the line number table.
18451 LINE is the line to add, LAST_LINE is the last line that was added,
18452 LAST_SUBFILE is the subfile for LAST_LINE.
18453 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
18454 had a non-zero discriminator.
18455
18456 We have to be careful in the presence of discriminators.
18457 E.g., for this line:
18458
18459 for (i = 0; i < 100000; i++);
18460
18461 clang can emit four line number entries for that one line,
18462 each with a different discriminator.
18463 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
18464
18465 However, we want gdb to coalesce all four entries into one.
18466 Otherwise the user could stepi into the middle of the line and
18467 gdb would get confused about whether the pc really was in the
18468 middle of the line.
18469
18470 Things are further complicated by the fact that two consecutive
18471 line number entries for the same line is a heuristic used by gcc
18472 to denote the end of the prologue. So we can't just discard duplicate
18473 entries, we have to be selective about it. The heuristic we use is
18474 that we only collapse consecutive entries for the same line if at least
18475 one of those entries has a non-zero discriminator. PR 17276.
18476
18477 Note: Addresses in the line number state machine can never go backwards
18478 within one sequence, thus this coalescing is ok. */
18479
18480 static int
18481 dwarf_record_line_p (unsigned int line, unsigned int last_line,
18482 int line_has_non_zero_discriminator,
18483 struct subfile *last_subfile)
18484 {
18485 if (current_subfile != last_subfile)
18486 return 1;
18487 if (line != last_line)
18488 return 1;
18489 /* Same line for the same file that we've seen already.
18490 As a last check, for pr 17276, only record the line if the line
18491 has never had a non-zero discriminator. */
18492 if (!line_has_non_zero_discriminator)
18493 return 1;
18494 return 0;
18495 }
18496
18497 /* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
18498 in the line table of subfile SUBFILE. */
18499
18500 static void
18501 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
18502 unsigned int line, CORE_ADDR address,
18503 record_line_ftype p_record_line)
18504 {
18505 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
18506
18507 if (dwarf_line_debug)
18508 {
18509 fprintf_unfiltered (gdb_stdlog,
18510 "Recording line %u, file %s, address %s\n",
18511 line, lbasename (subfile->name),
18512 paddress (gdbarch, address));
18513 }
18514
18515 (*p_record_line) (subfile, line, addr);
18516 }
18517
18518 /* Subroutine of dwarf_decode_lines_1 to simplify it.
18519 Mark the end of a set of line number records.
18520 The arguments are the same as for dwarf_record_line_1.
18521 If SUBFILE is NULL the request is ignored. */
18522
18523 static void
18524 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
18525 CORE_ADDR address, record_line_ftype p_record_line)
18526 {
18527 if (subfile == NULL)
18528 return;
18529
18530 if (dwarf_line_debug)
18531 {
18532 fprintf_unfiltered (gdb_stdlog,
18533 "Finishing current line, file %s, address %s\n",
18534 lbasename (subfile->name),
18535 paddress (gdbarch, address));
18536 }
18537
18538 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
18539 }
18540
18541 void
18542 lnp_state_machine::record_line (bool end_sequence)
18543 {
18544 if (dwarf_line_debug)
18545 {
18546 fprintf_unfiltered (gdb_stdlog,
18547 "Processing actual line %u: file %u,"
18548 " address %s, is_stmt %u, discrim %u\n",
18549 m_line, to_underlying (m_file),
18550 paddress (m_gdbarch, m_address),
18551 m_is_stmt, m_discriminator);
18552 }
18553
18554 file_entry *fe = current_file ();
18555
18556 if (fe == NULL)
18557 dwarf2_debug_line_missing_file_complaint ();
18558 /* For now we ignore lines not starting on an instruction boundary.
18559 But not when processing end_sequence for compatibility with the
18560 previous version of the code. */
18561 else if (m_op_index == 0 || end_sequence)
18562 {
18563 fe->included_p = 1;
18564 if (m_record_lines_p && m_is_stmt)
18565 {
18566 if (m_last_subfile != current_subfile || end_sequence)
18567 {
18568 dwarf_finish_line (m_gdbarch, m_last_subfile,
18569 m_address, m_record_line_callback);
18570 }
18571
18572 if (!end_sequence)
18573 {
18574 if (dwarf_record_line_p (m_line, m_last_line,
18575 m_line_has_non_zero_discriminator,
18576 m_last_subfile))
18577 {
18578 dwarf_record_line_1 (m_gdbarch, current_subfile,
18579 m_line, m_address,
18580 m_record_line_callback);
18581 }
18582 m_last_subfile = current_subfile;
18583 m_last_line = m_line;
18584 }
18585 }
18586 }
18587 }
18588
18589 lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
18590 bool record_lines_p)
18591 {
18592 m_gdbarch = arch;
18593 m_record_lines_p = record_lines_p;
18594 m_line_header = lh;
18595
18596 m_record_line_callback = ::record_line;
18597
18598 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
18599 was a line entry for it so that the backend has a chance to adjust it
18600 and also record it in case it needs it. This is currently used by MIPS
18601 code, cf. `mips_adjust_dwarf2_line'. */
18602 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
18603 m_is_stmt = lh->default_is_stmt;
18604 m_discriminator = 0;
18605 }
18606
18607 void
18608 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
18609 const gdb_byte *line_ptr,
18610 CORE_ADDR lowpc, CORE_ADDR address)
18611 {
18612 /* If address < lowpc then it's not a usable value, it's outside the
18613 pc range of the CU. However, we restrict the test to only address
18614 values of zero to preserve GDB's previous behaviour which is to
18615 handle the specific case of a function being GC'd by the linker. */
18616
18617 if (address == 0 && address < lowpc)
18618 {
18619 /* This line table is for a function which has been
18620 GCd by the linker. Ignore it. PR gdb/12528 */
18621
18622 struct objfile *objfile = cu->objfile;
18623 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
18624
18625 complaint (&symfile_complaints,
18626 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
18627 line_offset, objfile_name (objfile));
18628 m_record_line_callback = noop_record_line;
18629 /* Note: record_line_callback is left as noop_record_line until
18630 we see DW_LNE_end_sequence. */
18631 }
18632 }
18633
18634 /* Subroutine of dwarf_decode_lines to simplify it.
18635 Process the line number information in LH.
18636 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
18637 program in order to set included_p for every referenced header. */
18638
18639 static void
18640 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
18641 const int decode_for_pst_p, CORE_ADDR lowpc)
18642 {
18643 const gdb_byte *line_ptr, *extended_end;
18644 const gdb_byte *line_end;
18645 unsigned int bytes_read, extended_len;
18646 unsigned char op_code, extended_op;
18647 CORE_ADDR baseaddr;
18648 struct objfile *objfile = cu->objfile;
18649 bfd *abfd = objfile->obfd;
18650 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18651 /* True if we're recording line info (as opposed to building partial
18652 symtabs and just interested in finding include files mentioned by
18653 the line number program). */
18654 bool record_lines_p = !decode_for_pst_p;
18655
18656 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
18657
18658 line_ptr = lh->statement_program_start;
18659 line_end = lh->statement_program_end;
18660
18661 /* Read the statement sequences until there's nothing left. */
18662 while (line_ptr < line_end)
18663 {
18664 /* The DWARF line number program state machine. Reset the state
18665 machine at the start of each sequence. */
18666 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
18667 bool end_sequence = false;
18668
18669 if (record_lines_p)
18670 {
18671 /* Start a subfile for the current file of the state
18672 machine. */
18673 const file_entry *fe = state_machine.current_file ();
18674
18675 if (fe != NULL)
18676 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
18677 }
18678
18679 /* Decode the table. */
18680 while (line_ptr < line_end && !end_sequence)
18681 {
18682 op_code = read_1_byte (abfd, line_ptr);
18683 line_ptr += 1;
18684
18685 if (op_code >= lh->opcode_base)
18686 {
18687 /* Special opcode. */
18688 state_machine.handle_special_opcode (op_code);
18689 }
18690 else switch (op_code)
18691 {
18692 case DW_LNS_extended_op:
18693 extended_len = read_unsigned_leb128 (abfd, line_ptr,
18694 &bytes_read);
18695 line_ptr += bytes_read;
18696 extended_end = line_ptr + extended_len;
18697 extended_op = read_1_byte (abfd, line_ptr);
18698 line_ptr += 1;
18699 switch (extended_op)
18700 {
18701 case DW_LNE_end_sequence:
18702 state_machine.handle_end_sequence ();
18703 end_sequence = true;
18704 break;
18705 case DW_LNE_set_address:
18706 {
18707 CORE_ADDR address
18708 = read_address (abfd, line_ptr, cu, &bytes_read);
18709 line_ptr += bytes_read;
18710
18711 state_machine.check_line_address (cu, line_ptr,
18712 lowpc, address);
18713 state_machine.handle_set_address (baseaddr, address);
18714 }
18715 break;
18716 case DW_LNE_define_file:
18717 {
18718 const char *cur_file;
18719 unsigned int mod_time, length;
18720 dir_index dindex;
18721
18722 cur_file = read_direct_string (abfd, line_ptr,
18723 &bytes_read);
18724 line_ptr += bytes_read;
18725 dindex = (dir_index)
18726 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18727 line_ptr += bytes_read;
18728 mod_time =
18729 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18730 line_ptr += bytes_read;
18731 length =
18732 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18733 line_ptr += bytes_read;
18734 lh->add_file_name (cur_file, dindex, mod_time, length);
18735 }
18736 break;
18737 case DW_LNE_set_discriminator:
18738 {
18739 /* The discriminator is not interesting to the
18740 debugger; just ignore it. We still need to
18741 check its value though:
18742 if there are consecutive entries for the same
18743 (non-prologue) line we want to coalesce them.
18744 PR 17276. */
18745 unsigned int discr
18746 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18747 line_ptr += bytes_read;
18748
18749 state_machine.handle_set_discriminator (discr);
18750 }
18751 break;
18752 default:
18753 complaint (&symfile_complaints,
18754 _("mangled .debug_line section"));
18755 return;
18756 }
18757 /* Make sure that we parsed the extended op correctly. If e.g.
18758 we expected a different address size than the producer used,
18759 we may have read the wrong number of bytes. */
18760 if (line_ptr != extended_end)
18761 {
18762 complaint (&symfile_complaints,
18763 _("mangled .debug_line section"));
18764 return;
18765 }
18766 break;
18767 case DW_LNS_copy:
18768 state_machine.handle_copy ();
18769 break;
18770 case DW_LNS_advance_pc:
18771 {
18772 CORE_ADDR adjust
18773 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18774 line_ptr += bytes_read;
18775
18776 state_machine.handle_advance_pc (adjust);
18777 }
18778 break;
18779 case DW_LNS_advance_line:
18780 {
18781 int line_delta
18782 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
18783 line_ptr += bytes_read;
18784
18785 state_machine.handle_advance_line (line_delta);
18786 }
18787 break;
18788 case DW_LNS_set_file:
18789 {
18790 file_name_index file
18791 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
18792 &bytes_read);
18793 line_ptr += bytes_read;
18794
18795 state_machine.handle_set_file (file);
18796 }
18797 break;
18798 case DW_LNS_set_column:
18799 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18800 line_ptr += bytes_read;
18801 break;
18802 case DW_LNS_negate_stmt:
18803 state_machine.handle_negate_stmt ();
18804 break;
18805 case DW_LNS_set_basic_block:
18806 break;
18807 /* Add to the address register of the state machine the
18808 address increment value corresponding to special opcode
18809 255. I.e., this value is scaled by the minimum
18810 instruction length since special opcode 255 would have
18811 scaled the increment. */
18812 case DW_LNS_const_add_pc:
18813 state_machine.handle_const_add_pc ();
18814 break;
18815 case DW_LNS_fixed_advance_pc:
18816 {
18817 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
18818 line_ptr += 2;
18819
18820 state_machine.handle_fixed_advance_pc (addr_adj);
18821 }
18822 break;
18823 default:
18824 {
18825 /* Unknown standard opcode, ignore it. */
18826 int i;
18827
18828 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
18829 {
18830 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18831 line_ptr += bytes_read;
18832 }
18833 }
18834 }
18835 }
18836
18837 if (!end_sequence)
18838 dwarf2_debug_line_missing_end_sequence_complaint ();
18839
18840 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
18841 in which case we still finish recording the last line). */
18842 state_machine.record_line (true);
18843 }
18844 }
18845
18846 /* Decode the Line Number Program (LNP) for the given line_header
18847 structure and CU. The actual information extracted and the type
18848 of structures created from the LNP depends on the value of PST.
18849
18850 1. If PST is NULL, then this procedure uses the data from the program
18851 to create all necessary symbol tables, and their linetables.
18852
18853 2. If PST is not NULL, this procedure reads the program to determine
18854 the list of files included by the unit represented by PST, and
18855 builds all the associated partial symbol tables.
18856
18857 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
18858 It is used for relative paths in the line table.
18859 NOTE: When processing partial symtabs (pst != NULL),
18860 comp_dir == pst->dirname.
18861
18862 NOTE: It is important that psymtabs have the same file name (via strcmp)
18863 as the corresponding symtab. Since COMP_DIR is not used in the name of the
18864 symtab we don't use it in the name of the psymtabs we create.
18865 E.g. expand_line_sal requires this when finding psymtabs to expand.
18866 A good testcase for this is mb-inline.exp.
18867
18868 LOWPC is the lowest address in CU (or 0 if not known).
18869
18870 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
18871 for its PC<->lines mapping information. Otherwise only the filename
18872 table is read in. */
18873
18874 static void
18875 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
18876 struct dwarf2_cu *cu, struct partial_symtab *pst,
18877 CORE_ADDR lowpc, int decode_mapping)
18878 {
18879 struct objfile *objfile = cu->objfile;
18880 const int decode_for_pst_p = (pst != NULL);
18881
18882 if (decode_mapping)
18883 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
18884
18885 if (decode_for_pst_p)
18886 {
18887 int file_index;
18888
18889 /* Now that we're done scanning the Line Header Program, we can
18890 create the psymtab of each included file. */
18891 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
18892 if (lh->file_names[file_index].included_p == 1)
18893 {
18894 const char *include_name =
18895 psymtab_include_file_name (lh, file_index, pst, comp_dir);
18896 if (include_name != NULL)
18897 dwarf2_create_include_psymtab (include_name, pst, objfile);
18898 }
18899 }
18900 else
18901 {
18902 /* Make sure a symtab is created for every file, even files
18903 which contain only variables (i.e. no code with associated
18904 line numbers). */
18905 struct compunit_symtab *cust = buildsym_compunit_symtab ();
18906 int i;
18907
18908 for (i = 0; i < lh->file_names.size (); i++)
18909 {
18910 file_entry &fe = lh->file_names[i];
18911
18912 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
18913
18914 if (current_subfile->symtab == NULL)
18915 {
18916 current_subfile->symtab
18917 = allocate_symtab (cust, current_subfile->name);
18918 }
18919 fe.symtab = current_subfile->symtab;
18920 }
18921 }
18922 }
18923
18924 /* Start a subfile for DWARF. FILENAME is the name of the file and
18925 DIRNAME the name of the source directory which contains FILENAME
18926 or NULL if not known.
18927 This routine tries to keep line numbers from identical absolute and
18928 relative file names in a common subfile.
18929
18930 Using the `list' example from the GDB testsuite, which resides in
18931 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
18932 of /srcdir/list0.c yields the following debugging information for list0.c:
18933
18934 DW_AT_name: /srcdir/list0.c
18935 DW_AT_comp_dir: /compdir
18936 files.files[0].name: list0.h
18937 files.files[0].dir: /srcdir
18938 files.files[1].name: list0.c
18939 files.files[1].dir: /srcdir
18940
18941 The line number information for list0.c has to end up in a single
18942 subfile, so that `break /srcdir/list0.c:1' works as expected.
18943 start_subfile will ensure that this happens provided that we pass the
18944 concatenation of files.files[1].dir and files.files[1].name as the
18945 subfile's name. */
18946
18947 static void
18948 dwarf2_start_subfile (const char *filename, const char *dirname)
18949 {
18950 char *copy = NULL;
18951
18952 /* In order not to lose the line information directory,
18953 we concatenate it to the filename when it makes sense.
18954 Note that the Dwarf3 standard says (speaking of filenames in line
18955 information): ``The directory index is ignored for file names
18956 that represent full path names''. Thus ignoring dirname in the
18957 `else' branch below isn't an issue. */
18958
18959 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
18960 {
18961 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
18962 filename = copy;
18963 }
18964
18965 start_subfile (filename);
18966
18967 if (copy != NULL)
18968 xfree (copy);
18969 }
18970
18971 /* Start a symtab for DWARF.
18972 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
18973
18974 static struct compunit_symtab *
18975 dwarf2_start_symtab (struct dwarf2_cu *cu,
18976 const char *name, const char *comp_dir, CORE_ADDR low_pc)
18977 {
18978 struct compunit_symtab *cust
18979 = start_symtab (cu->objfile, name, comp_dir, low_pc);
18980
18981 record_debugformat ("DWARF 2");
18982 record_producer (cu->producer);
18983
18984 /* We assume that we're processing GCC output. */
18985 processing_gcc_compilation = 2;
18986
18987 cu->processing_has_namespace_info = 0;
18988
18989 return cust;
18990 }
18991
18992 static void
18993 var_decode_location (struct attribute *attr, struct symbol *sym,
18994 struct dwarf2_cu *cu)
18995 {
18996 struct objfile *objfile = cu->objfile;
18997 struct comp_unit_head *cu_header = &cu->header;
18998
18999 /* NOTE drow/2003-01-30: There used to be a comment and some special
19000 code here to turn a symbol with DW_AT_external and a
19001 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
19002 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
19003 with some versions of binutils) where shared libraries could have
19004 relocations against symbols in their debug information - the
19005 minimal symbol would have the right address, but the debug info
19006 would not. It's no longer necessary, because we will explicitly
19007 apply relocations when we read in the debug information now. */
19008
19009 /* A DW_AT_location attribute with no contents indicates that a
19010 variable has been optimized away. */
19011 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
19012 {
19013 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
19014 return;
19015 }
19016
19017 /* Handle one degenerate form of location expression specially, to
19018 preserve GDB's previous behavior when section offsets are
19019 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
19020 then mark this symbol as LOC_STATIC. */
19021
19022 if (attr_form_is_block (attr)
19023 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
19024 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
19025 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
19026 && (DW_BLOCK (attr)->size
19027 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
19028 {
19029 unsigned int dummy;
19030
19031 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
19032 SYMBOL_VALUE_ADDRESS (sym) =
19033 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
19034 else
19035 SYMBOL_VALUE_ADDRESS (sym) =
19036 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
19037 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
19038 fixup_symbol_section (sym, objfile);
19039 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
19040 SYMBOL_SECTION (sym));
19041 return;
19042 }
19043
19044 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
19045 expression evaluator, and use LOC_COMPUTED only when necessary
19046 (i.e. when the value of a register or memory location is
19047 referenced, or a thread-local block, etc.). Then again, it might
19048 not be worthwhile. I'm assuming that it isn't unless performance
19049 or memory numbers show me otherwise. */
19050
19051 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
19052
19053 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
19054 cu->has_loclist = 1;
19055 }
19056
19057 /* Given a pointer to a DWARF information entry, figure out if we need
19058 to make a symbol table entry for it, and if so, create a new entry
19059 and return a pointer to it.
19060 If TYPE is NULL, determine symbol type from the die, otherwise
19061 used the passed type.
19062 If SPACE is not NULL, use it to hold the new symbol. If it is
19063 NULL, allocate a new symbol on the objfile's obstack. */
19064
19065 static struct symbol *
19066 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
19067 struct symbol *space)
19068 {
19069 struct objfile *objfile = cu->objfile;
19070 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19071 struct symbol *sym = NULL;
19072 const char *name;
19073 struct attribute *attr = NULL;
19074 struct attribute *attr2 = NULL;
19075 CORE_ADDR baseaddr;
19076 struct pending **list_to_add = NULL;
19077
19078 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
19079
19080 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19081
19082 name = dwarf2_name (die, cu);
19083 if (name)
19084 {
19085 const char *linkagename;
19086 int suppress_add = 0;
19087
19088 if (space)
19089 sym = space;
19090 else
19091 sym = allocate_symbol (objfile);
19092 OBJSTAT (objfile, n_syms++);
19093
19094 /* Cache this symbol's name and the name's demangled form (if any). */
19095 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
19096 linkagename = dwarf2_physname (name, die, cu);
19097 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
19098
19099 /* Fortran does not have mangling standard and the mangling does differ
19100 between gfortran, iFort etc. */
19101 if (cu->language == language_fortran
19102 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
19103 symbol_set_demangled_name (&(sym->ginfo),
19104 dwarf2_full_name (name, die, cu),
19105 NULL);
19106
19107 /* Default assumptions.
19108 Use the passed type or decode it from the die. */
19109 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
19110 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
19111 if (type != NULL)
19112 SYMBOL_TYPE (sym) = type;
19113 else
19114 SYMBOL_TYPE (sym) = die_type (die, cu);
19115 attr = dwarf2_attr (die,
19116 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
19117 cu);
19118 if (attr)
19119 {
19120 SYMBOL_LINE (sym) = DW_UNSND (attr);
19121 }
19122
19123 attr = dwarf2_attr (die,
19124 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
19125 cu);
19126 if (attr)
19127 {
19128 file_name_index file_index = (file_name_index) DW_UNSND (attr);
19129 struct file_entry *fe;
19130
19131 if (cu->line_header != NULL)
19132 fe = cu->line_header->file_name_at (file_index);
19133 else
19134 fe = NULL;
19135
19136 if (fe == NULL)
19137 complaint (&symfile_complaints,
19138 _("file index out of range"));
19139 else
19140 symbol_set_symtab (sym, fe->symtab);
19141 }
19142
19143 switch (die->tag)
19144 {
19145 case DW_TAG_label:
19146 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
19147 if (attr)
19148 {
19149 CORE_ADDR addr;
19150
19151 addr = attr_value_as_address (attr);
19152 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
19153 SYMBOL_VALUE_ADDRESS (sym) = addr;
19154 }
19155 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
19156 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
19157 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
19158 add_symbol_to_list (sym, cu->list_in_scope);
19159 break;
19160 case DW_TAG_subprogram:
19161 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
19162 finish_block. */
19163 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
19164 attr2 = dwarf2_attr (die, DW_AT_external, cu);
19165 if ((attr2 && (DW_UNSND (attr2) != 0))
19166 || cu->language == language_ada)
19167 {
19168 /* Subprograms marked external are stored as a global symbol.
19169 Ada subprograms, whether marked external or not, are always
19170 stored as a global symbol, because we want to be able to
19171 access them globally. For instance, we want to be able
19172 to break on a nested subprogram without having to
19173 specify the context. */
19174 list_to_add = &global_symbols;
19175 }
19176 else
19177 {
19178 list_to_add = cu->list_in_scope;
19179 }
19180 break;
19181 case DW_TAG_inlined_subroutine:
19182 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
19183 finish_block. */
19184 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
19185 SYMBOL_INLINED (sym) = 1;
19186 list_to_add = cu->list_in_scope;
19187 break;
19188 case DW_TAG_template_value_param:
19189 suppress_add = 1;
19190 /* Fall through. */
19191 case DW_TAG_constant:
19192 case DW_TAG_variable:
19193 case DW_TAG_member:
19194 /* Compilation with minimal debug info may result in
19195 variables with missing type entries. Change the
19196 misleading `void' type to something sensible. */
19197 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
19198 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
19199
19200 attr = dwarf2_attr (die, DW_AT_const_value, cu);
19201 /* In the case of DW_TAG_member, we should only be called for
19202 static const members. */
19203 if (die->tag == DW_TAG_member)
19204 {
19205 /* dwarf2_add_field uses die_is_declaration,
19206 so we do the same. */
19207 gdb_assert (die_is_declaration (die, cu));
19208 gdb_assert (attr);
19209 }
19210 if (attr)
19211 {
19212 dwarf2_const_value (attr, sym, cu);
19213 attr2 = dwarf2_attr (die, DW_AT_external, cu);
19214 if (!suppress_add)
19215 {
19216 if (attr2 && (DW_UNSND (attr2) != 0))
19217 list_to_add = &global_symbols;
19218 else
19219 list_to_add = cu->list_in_scope;
19220 }
19221 break;
19222 }
19223 attr = dwarf2_attr (die, DW_AT_location, cu);
19224 if (attr)
19225 {
19226 var_decode_location (attr, sym, cu);
19227 attr2 = dwarf2_attr (die, DW_AT_external, cu);
19228
19229 /* Fortran explicitly imports any global symbols to the local
19230 scope by DW_TAG_common_block. */
19231 if (cu->language == language_fortran && die->parent
19232 && die->parent->tag == DW_TAG_common_block)
19233 attr2 = NULL;
19234
19235 if (SYMBOL_CLASS (sym) == LOC_STATIC
19236 && SYMBOL_VALUE_ADDRESS (sym) == 0
19237 && !dwarf2_per_objfile->has_section_at_zero)
19238 {
19239 /* When a static variable is eliminated by the linker,
19240 the corresponding debug information is not stripped
19241 out, but the variable address is set to null;
19242 do not add such variables into symbol table. */
19243 }
19244 else if (attr2 && (DW_UNSND (attr2) != 0))
19245 {
19246 /* Workaround gfortran PR debug/40040 - it uses
19247 DW_AT_location for variables in -fPIC libraries which may
19248 get overriden by other libraries/executable and get
19249 a different address. Resolve it by the minimal symbol
19250 which may come from inferior's executable using copy
19251 relocation. Make this workaround only for gfortran as for
19252 other compilers GDB cannot guess the minimal symbol
19253 Fortran mangling kind. */
19254 if (cu->language == language_fortran && die->parent
19255 && die->parent->tag == DW_TAG_module
19256 && cu->producer
19257 && startswith (cu->producer, "GNU Fortran"))
19258 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
19259
19260 /* A variable with DW_AT_external is never static,
19261 but it may be block-scoped. */
19262 list_to_add = (cu->list_in_scope == &file_symbols
19263 ? &global_symbols : cu->list_in_scope);
19264 }
19265 else
19266 list_to_add = cu->list_in_scope;
19267 }
19268 else
19269 {
19270 /* We do not know the address of this symbol.
19271 If it is an external symbol and we have type information
19272 for it, enter the symbol as a LOC_UNRESOLVED symbol.
19273 The address of the variable will then be determined from
19274 the minimal symbol table whenever the variable is
19275 referenced. */
19276 attr2 = dwarf2_attr (die, DW_AT_external, cu);
19277
19278 /* Fortran explicitly imports any global symbols to the local
19279 scope by DW_TAG_common_block. */
19280 if (cu->language == language_fortran && die->parent
19281 && die->parent->tag == DW_TAG_common_block)
19282 {
19283 /* SYMBOL_CLASS doesn't matter here because
19284 read_common_block is going to reset it. */
19285 if (!suppress_add)
19286 list_to_add = cu->list_in_scope;
19287 }
19288 else if (attr2 && (DW_UNSND (attr2) != 0)
19289 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
19290 {
19291 /* A variable with DW_AT_external is never static, but it
19292 may be block-scoped. */
19293 list_to_add = (cu->list_in_scope == &file_symbols
19294 ? &global_symbols : cu->list_in_scope);
19295
19296 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
19297 }
19298 else if (!die_is_declaration (die, cu))
19299 {
19300 /* Use the default LOC_OPTIMIZED_OUT class. */
19301 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
19302 if (!suppress_add)
19303 list_to_add = cu->list_in_scope;
19304 }
19305 }
19306 break;
19307 case DW_TAG_formal_parameter:
19308 /* If we are inside a function, mark this as an argument. If
19309 not, we might be looking at an argument to an inlined function
19310 when we do not have enough information to show inlined frames;
19311 pretend it's a local variable in that case so that the user can
19312 still see it. */
19313 if (context_stack_depth > 0
19314 && context_stack[context_stack_depth - 1].name != NULL)
19315 SYMBOL_IS_ARGUMENT (sym) = 1;
19316 attr = dwarf2_attr (die, DW_AT_location, cu);
19317 if (attr)
19318 {
19319 var_decode_location (attr, sym, cu);
19320 }
19321 attr = dwarf2_attr (die, DW_AT_const_value, cu);
19322 if (attr)
19323 {
19324 dwarf2_const_value (attr, sym, cu);
19325 }
19326
19327 list_to_add = cu->list_in_scope;
19328 break;
19329 case DW_TAG_unspecified_parameters:
19330 /* From varargs functions; gdb doesn't seem to have any
19331 interest in this information, so just ignore it for now.
19332 (FIXME?) */
19333 break;
19334 case DW_TAG_template_type_param:
19335 suppress_add = 1;
19336 /* Fall through. */
19337 case DW_TAG_class_type:
19338 case DW_TAG_interface_type:
19339 case DW_TAG_structure_type:
19340 case DW_TAG_union_type:
19341 case DW_TAG_set_type:
19342 case DW_TAG_enumeration_type:
19343 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19344 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
19345
19346 {
19347 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
19348 really ever be static objects: otherwise, if you try
19349 to, say, break of a class's method and you're in a file
19350 which doesn't mention that class, it won't work unless
19351 the check for all static symbols in lookup_symbol_aux
19352 saves you. See the OtherFileClass tests in
19353 gdb.c++/namespace.exp. */
19354
19355 if (!suppress_add)
19356 {
19357 list_to_add = (cu->list_in_scope == &file_symbols
19358 && cu->language == language_cplus
19359 ? &global_symbols : cu->list_in_scope);
19360
19361 /* The semantics of C++ state that "struct foo {
19362 ... }" also defines a typedef for "foo". */
19363 if (cu->language == language_cplus
19364 || cu->language == language_ada
19365 || cu->language == language_d
19366 || cu->language == language_rust)
19367 {
19368 /* The symbol's name is already allocated along
19369 with this objfile, so we don't need to
19370 duplicate it for the type. */
19371 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
19372 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
19373 }
19374 }
19375 }
19376 break;
19377 case DW_TAG_typedef:
19378 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19379 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
19380 list_to_add = cu->list_in_scope;
19381 break;
19382 case DW_TAG_base_type:
19383 case DW_TAG_subrange_type:
19384 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19385 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
19386 list_to_add = cu->list_in_scope;
19387 break;
19388 case DW_TAG_enumerator:
19389 attr = dwarf2_attr (die, DW_AT_const_value, cu);
19390 if (attr)
19391 {
19392 dwarf2_const_value (attr, sym, cu);
19393 }
19394 {
19395 /* NOTE: carlton/2003-11-10: See comment above in the
19396 DW_TAG_class_type, etc. block. */
19397
19398 list_to_add = (cu->list_in_scope == &file_symbols
19399 && cu->language == language_cplus
19400 ? &global_symbols : cu->list_in_scope);
19401 }
19402 break;
19403 case DW_TAG_imported_declaration:
19404 case DW_TAG_namespace:
19405 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19406 list_to_add = &global_symbols;
19407 break;
19408 case DW_TAG_module:
19409 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19410 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
19411 list_to_add = &global_symbols;
19412 break;
19413 case DW_TAG_common_block:
19414 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
19415 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
19416 add_symbol_to_list (sym, cu->list_in_scope);
19417 break;
19418 default:
19419 /* Not a tag we recognize. Hopefully we aren't processing
19420 trash data, but since we must specifically ignore things
19421 we don't recognize, there is nothing else we should do at
19422 this point. */
19423 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
19424 dwarf_tag_name (die->tag));
19425 break;
19426 }
19427
19428 if (suppress_add)
19429 {
19430 sym->hash_next = objfile->template_symbols;
19431 objfile->template_symbols = sym;
19432 list_to_add = NULL;
19433 }
19434
19435 if (list_to_add != NULL)
19436 add_symbol_to_list (sym, list_to_add);
19437
19438 /* For the benefit of old versions of GCC, check for anonymous
19439 namespaces based on the demangled name. */
19440 if (!cu->processing_has_namespace_info
19441 && cu->language == language_cplus)
19442 cp_scan_for_anonymous_namespaces (sym, objfile);
19443 }
19444 return (sym);
19445 }
19446
19447 /* A wrapper for new_symbol_full that always allocates a new symbol. */
19448
19449 static struct symbol *
19450 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19451 {
19452 return new_symbol_full (die, type, cu, NULL);
19453 }
19454
19455 /* Given an attr with a DW_FORM_dataN value in host byte order,
19456 zero-extend it as appropriate for the symbol's type. The DWARF
19457 standard (v4) is not entirely clear about the meaning of using
19458 DW_FORM_dataN for a constant with a signed type, where the type is
19459 wider than the data. The conclusion of a discussion on the DWARF
19460 list was that this is unspecified. We choose to always zero-extend
19461 because that is the interpretation long in use by GCC. */
19462
19463 static gdb_byte *
19464 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
19465 struct dwarf2_cu *cu, LONGEST *value, int bits)
19466 {
19467 struct objfile *objfile = cu->objfile;
19468 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
19469 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
19470 LONGEST l = DW_UNSND (attr);
19471
19472 if (bits < sizeof (*value) * 8)
19473 {
19474 l &= ((LONGEST) 1 << bits) - 1;
19475 *value = l;
19476 }
19477 else if (bits == sizeof (*value) * 8)
19478 *value = l;
19479 else
19480 {
19481 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
19482 store_unsigned_integer (bytes, bits / 8, byte_order, l);
19483 return bytes;
19484 }
19485
19486 return NULL;
19487 }
19488
19489 /* Read a constant value from an attribute. Either set *VALUE, or if
19490 the value does not fit in *VALUE, set *BYTES - either already
19491 allocated on the objfile obstack, or newly allocated on OBSTACK,
19492 or, set *BATON, if we translated the constant to a location
19493 expression. */
19494
19495 static void
19496 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
19497 const char *name, struct obstack *obstack,
19498 struct dwarf2_cu *cu,
19499 LONGEST *value, const gdb_byte **bytes,
19500 struct dwarf2_locexpr_baton **baton)
19501 {
19502 struct objfile *objfile = cu->objfile;
19503 struct comp_unit_head *cu_header = &cu->header;
19504 struct dwarf_block *blk;
19505 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
19506 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
19507
19508 *value = 0;
19509 *bytes = NULL;
19510 *baton = NULL;
19511
19512 switch (attr->form)
19513 {
19514 case DW_FORM_addr:
19515 case DW_FORM_GNU_addr_index:
19516 {
19517 gdb_byte *data;
19518
19519 if (TYPE_LENGTH (type) != cu_header->addr_size)
19520 dwarf2_const_value_length_mismatch_complaint (name,
19521 cu_header->addr_size,
19522 TYPE_LENGTH (type));
19523 /* Symbols of this form are reasonably rare, so we just
19524 piggyback on the existing location code rather than writing
19525 a new implementation of symbol_computed_ops. */
19526 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
19527 (*baton)->per_cu = cu->per_cu;
19528 gdb_assert ((*baton)->per_cu);
19529
19530 (*baton)->size = 2 + cu_header->addr_size;
19531 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
19532 (*baton)->data = data;
19533
19534 data[0] = DW_OP_addr;
19535 store_unsigned_integer (&data[1], cu_header->addr_size,
19536 byte_order, DW_ADDR (attr));
19537 data[cu_header->addr_size + 1] = DW_OP_stack_value;
19538 }
19539 break;
19540 case DW_FORM_string:
19541 case DW_FORM_strp:
19542 case DW_FORM_GNU_str_index:
19543 case DW_FORM_GNU_strp_alt:
19544 /* DW_STRING is already allocated on the objfile obstack, point
19545 directly to it. */
19546 *bytes = (const gdb_byte *) DW_STRING (attr);
19547 break;
19548 case DW_FORM_block1:
19549 case DW_FORM_block2:
19550 case DW_FORM_block4:
19551 case DW_FORM_block:
19552 case DW_FORM_exprloc:
19553 case DW_FORM_data16:
19554 blk = DW_BLOCK (attr);
19555 if (TYPE_LENGTH (type) != blk->size)
19556 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
19557 TYPE_LENGTH (type));
19558 *bytes = blk->data;
19559 break;
19560
19561 /* The DW_AT_const_value attributes are supposed to carry the
19562 symbol's value "represented as it would be on the target
19563 architecture." By the time we get here, it's already been
19564 converted to host endianness, so we just need to sign- or
19565 zero-extend it as appropriate. */
19566 case DW_FORM_data1:
19567 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
19568 break;
19569 case DW_FORM_data2:
19570 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
19571 break;
19572 case DW_FORM_data4:
19573 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
19574 break;
19575 case DW_FORM_data8:
19576 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
19577 break;
19578
19579 case DW_FORM_sdata:
19580 case DW_FORM_implicit_const:
19581 *value = DW_SND (attr);
19582 break;
19583
19584 case DW_FORM_udata:
19585 *value = DW_UNSND (attr);
19586 break;
19587
19588 default:
19589 complaint (&symfile_complaints,
19590 _("unsupported const value attribute form: '%s'"),
19591 dwarf_form_name (attr->form));
19592 *value = 0;
19593 break;
19594 }
19595 }
19596
19597
19598 /* Copy constant value from an attribute to a symbol. */
19599
19600 static void
19601 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
19602 struct dwarf2_cu *cu)
19603 {
19604 struct objfile *objfile = cu->objfile;
19605 LONGEST value;
19606 const gdb_byte *bytes;
19607 struct dwarf2_locexpr_baton *baton;
19608
19609 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
19610 SYMBOL_PRINT_NAME (sym),
19611 &objfile->objfile_obstack, cu,
19612 &value, &bytes, &baton);
19613
19614 if (baton != NULL)
19615 {
19616 SYMBOL_LOCATION_BATON (sym) = baton;
19617 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
19618 }
19619 else if (bytes != NULL)
19620 {
19621 SYMBOL_VALUE_BYTES (sym) = bytes;
19622 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
19623 }
19624 else
19625 {
19626 SYMBOL_VALUE (sym) = value;
19627 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
19628 }
19629 }
19630
19631 /* Return the type of the die in question using its DW_AT_type attribute. */
19632
19633 static struct type *
19634 die_type (struct die_info *die, struct dwarf2_cu *cu)
19635 {
19636 struct attribute *type_attr;
19637
19638 type_attr = dwarf2_attr (die, DW_AT_type, cu);
19639 if (!type_attr)
19640 {
19641 /* A missing DW_AT_type represents a void type. */
19642 return objfile_type (cu->objfile)->builtin_void;
19643 }
19644
19645 return lookup_die_type (die, type_attr, cu);
19646 }
19647
19648 /* True iff CU's producer generates GNAT Ada auxiliary information
19649 that allows to find parallel types through that information instead
19650 of having to do expensive parallel lookups by type name. */
19651
19652 static int
19653 need_gnat_info (struct dwarf2_cu *cu)
19654 {
19655 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
19656 of GNAT produces this auxiliary information, without any indication
19657 that it is produced. Part of enhancing the FSF version of GNAT
19658 to produce that information will be to put in place an indicator
19659 that we can use in order to determine whether the descriptive type
19660 info is available or not. One suggestion that has been made is
19661 to use a new attribute, attached to the CU die. For now, assume
19662 that the descriptive type info is not available. */
19663 return 0;
19664 }
19665
19666 /* Return the auxiliary type of the die in question using its
19667 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
19668 attribute is not present. */
19669
19670 static struct type *
19671 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
19672 {
19673 struct attribute *type_attr;
19674
19675 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
19676 if (!type_attr)
19677 return NULL;
19678
19679 return lookup_die_type (die, type_attr, cu);
19680 }
19681
19682 /* If DIE has a descriptive_type attribute, then set the TYPE's
19683 descriptive type accordingly. */
19684
19685 static void
19686 set_descriptive_type (struct type *type, struct die_info *die,
19687 struct dwarf2_cu *cu)
19688 {
19689 struct type *descriptive_type = die_descriptive_type (die, cu);
19690
19691 if (descriptive_type)
19692 {
19693 ALLOCATE_GNAT_AUX_TYPE (type);
19694 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
19695 }
19696 }
19697
19698 /* Return the containing type of the die in question using its
19699 DW_AT_containing_type attribute. */
19700
19701 static struct type *
19702 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
19703 {
19704 struct attribute *type_attr;
19705
19706 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
19707 if (!type_attr)
19708 error (_("Dwarf Error: Problem turning containing type into gdb type "
19709 "[in module %s]"), objfile_name (cu->objfile));
19710
19711 return lookup_die_type (die, type_attr, cu);
19712 }
19713
19714 /* Return an error marker type to use for the ill formed type in DIE/CU. */
19715
19716 static struct type *
19717 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
19718 {
19719 struct objfile *objfile = dwarf2_per_objfile->objfile;
19720 char *message, *saved;
19721
19722 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
19723 objfile_name (objfile),
19724 to_underlying (cu->header.sect_off),
19725 to_underlying (die->sect_off));
19726 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
19727 message, strlen (message));
19728 xfree (message);
19729
19730 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
19731 }
19732
19733 /* Look up the type of DIE in CU using its type attribute ATTR.
19734 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
19735 DW_AT_containing_type.
19736 If there is no type substitute an error marker. */
19737
19738 static struct type *
19739 lookup_die_type (struct die_info *die, const struct attribute *attr,
19740 struct dwarf2_cu *cu)
19741 {
19742 struct objfile *objfile = cu->objfile;
19743 struct type *this_type;
19744
19745 gdb_assert (attr->name == DW_AT_type
19746 || attr->name == DW_AT_GNAT_descriptive_type
19747 || attr->name == DW_AT_containing_type);
19748
19749 /* First see if we have it cached. */
19750
19751 if (attr->form == DW_FORM_GNU_ref_alt)
19752 {
19753 struct dwarf2_per_cu_data *per_cu;
19754 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
19755
19756 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, cu->objfile);
19757 this_type = get_die_type_at_offset (sect_off, per_cu);
19758 }
19759 else if (attr_form_is_ref (attr))
19760 {
19761 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
19762
19763 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
19764 }
19765 else if (attr->form == DW_FORM_ref_sig8)
19766 {
19767 ULONGEST signature = DW_SIGNATURE (attr);
19768
19769 return get_signatured_type (die, signature, cu);
19770 }
19771 else
19772 {
19773 complaint (&symfile_complaints,
19774 _("Dwarf Error: Bad type attribute %s in DIE"
19775 " at 0x%x [in module %s]"),
19776 dwarf_attr_name (attr->name), to_underlying (die->sect_off),
19777 objfile_name (objfile));
19778 return build_error_marker_type (cu, die);
19779 }
19780
19781 /* If not cached we need to read it in. */
19782
19783 if (this_type == NULL)
19784 {
19785 struct die_info *type_die = NULL;
19786 struct dwarf2_cu *type_cu = cu;
19787
19788 if (attr_form_is_ref (attr))
19789 type_die = follow_die_ref (die, attr, &type_cu);
19790 if (type_die == NULL)
19791 return build_error_marker_type (cu, die);
19792 /* If we find the type now, it's probably because the type came
19793 from an inter-CU reference and the type's CU got expanded before
19794 ours. */
19795 this_type = read_type_die (type_die, type_cu);
19796 }
19797
19798 /* If we still don't have a type use an error marker. */
19799
19800 if (this_type == NULL)
19801 return build_error_marker_type (cu, die);
19802
19803 return this_type;
19804 }
19805
19806 /* Return the type in DIE, CU.
19807 Returns NULL for invalid types.
19808
19809 This first does a lookup in die_type_hash,
19810 and only reads the die in if necessary.
19811
19812 NOTE: This can be called when reading in partial or full symbols. */
19813
19814 static struct type *
19815 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
19816 {
19817 struct type *this_type;
19818
19819 this_type = get_die_type (die, cu);
19820 if (this_type)
19821 return this_type;
19822
19823 return read_type_die_1 (die, cu);
19824 }
19825
19826 /* Read the type in DIE, CU.
19827 Returns NULL for invalid types. */
19828
19829 static struct type *
19830 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
19831 {
19832 struct type *this_type = NULL;
19833
19834 switch (die->tag)
19835 {
19836 case DW_TAG_class_type:
19837 case DW_TAG_interface_type:
19838 case DW_TAG_structure_type:
19839 case DW_TAG_union_type:
19840 this_type = read_structure_type (die, cu);
19841 break;
19842 case DW_TAG_enumeration_type:
19843 this_type = read_enumeration_type (die, cu);
19844 break;
19845 case DW_TAG_subprogram:
19846 case DW_TAG_subroutine_type:
19847 case DW_TAG_inlined_subroutine:
19848 this_type = read_subroutine_type (die, cu);
19849 break;
19850 case DW_TAG_array_type:
19851 this_type = read_array_type (die, cu);
19852 break;
19853 case DW_TAG_set_type:
19854 this_type = read_set_type (die, cu);
19855 break;
19856 case DW_TAG_pointer_type:
19857 this_type = read_tag_pointer_type (die, cu);
19858 break;
19859 case DW_TAG_ptr_to_member_type:
19860 this_type = read_tag_ptr_to_member_type (die, cu);
19861 break;
19862 case DW_TAG_reference_type:
19863 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
19864 break;
19865 case DW_TAG_rvalue_reference_type:
19866 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
19867 break;
19868 case DW_TAG_const_type:
19869 this_type = read_tag_const_type (die, cu);
19870 break;
19871 case DW_TAG_volatile_type:
19872 this_type = read_tag_volatile_type (die, cu);
19873 break;
19874 case DW_TAG_restrict_type:
19875 this_type = read_tag_restrict_type (die, cu);
19876 break;
19877 case DW_TAG_string_type:
19878 this_type = read_tag_string_type (die, cu);
19879 break;
19880 case DW_TAG_typedef:
19881 this_type = read_typedef (die, cu);
19882 break;
19883 case DW_TAG_subrange_type:
19884 this_type = read_subrange_type (die, cu);
19885 break;
19886 case DW_TAG_base_type:
19887 this_type = read_base_type (die, cu);
19888 break;
19889 case DW_TAG_unspecified_type:
19890 this_type = read_unspecified_type (die, cu);
19891 break;
19892 case DW_TAG_namespace:
19893 this_type = read_namespace_type (die, cu);
19894 break;
19895 case DW_TAG_module:
19896 this_type = read_module_type (die, cu);
19897 break;
19898 case DW_TAG_atomic_type:
19899 this_type = read_tag_atomic_type (die, cu);
19900 break;
19901 default:
19902 complaint (&symfile_complaints,
19903 _("unexpected tag in read_type_die: '%s'"),
19904 dwarf_tag_name (die->tag));
19905 break;
19906 }
19907
19908 return this_type;
19909 }
19910
19911 /* See if we can figure out if the class lives in a namespace. We do
19912 this by looking for a member function; its demangled name will
19913 contain namespace info, if there is any.
19914 Return the computed name or NULL.
19915 Space for the result is allocated on the objfile's obstack.
19916 This is the full-die version of guess_partial_die_structure_name.
19917 In this case we know DIE has no useful parent. */
19918
19919 static char *
19920 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
19921 {
19922 struct die_info *spec_die;
19923 struct dwarf2_cu *spec_cu;
19924 struct die_info *child;
19925
19926 spec_cu = cu;
19927 spec_die = die_specification (die, &spec_cu);
19928 if (spec_die != NULL)
19929 {
19930 die = spec_die;
19931 cu = spec_cu;
19932 }
19933
19934 for (child = die->child;
19935 child != NULL;
19936 child = child->sibling)
19937 {
19938 if (child->tag == DW_TAG_subprogram)
19939 {
19940 const char *linkage_name = dw2_linkage_name (child, cu);
19941
19942 if (linkage_name != NULL)
19943 {
19944 char *actual_name
19945 = language_class_name_from_physname (cu->language_defn,
19946 linkage_name);
19947 char *name = NULL;
19948
19949 if (actual_name != NULL)
19950 {
19951 const char *die_name = dwarf2_name (die, cu);
19952
19953 if (die_name != NULL
19954 && strcmp (die_name, actual_name) != 0)
19955 {
19956 /* Strip off the class name from the full name.
19957 We want the prefix. */
19958 int die_name_len = strlen (die_name);
19959 int actual_name_len = strlen (actual_name);
19960
19961 /* Test for '::' as a sanity check. */
19962 if (actual_name_len > die_name_len + 2
19963 && actual_name[actual_name_len
19964 - die_name_len - 1] == ':')
19965 name = (char *) obstack_copy0 (
19966 &cu->objfile->per_bfd->storage_obstack,
19967 actual_name, actual_name_len - die_name_len - 2);
19968 }
19969 }
19970 xfree (actual_name);
19971 return name;
19972 }
19973 }
19974 }
19975
19976 return NULL;
19977 }
19978
19979 /* GCC might emit a nameless typedef that has a linkage name. Determine the
19980 prefix part in such case. See
19981 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19982
19983 static const char *
19984 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
19985 {
19986 struct attribute *attr;
19987 const char *base;
19988
19989 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
19990 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
19991 return NULL;
19992
19993 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
19994 return NULL;
19995
19996 attr = dw2_linkage_name_attr (die, cu);
19997 if (attr == NULL || DW_STRING (attr) == NULL)
19998 return NULL;
19999
20000 /* dwarf2_name had to be already called. */
20001 gdb_assert (DW_STRING_IS_CANONICAL (attr));
20002
20003 /* Strip the base name, keep any leading namespaces/classes. */
20004 base = strrchr (DW_STRING (attr), ':');
20005 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
20006 return "";
20007
20008 return (char *) obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
20009 DW_STRING (attr),
20010 &base[-1] - DW_STRING (attr));
20011 }
20012
20013 /* Return the name of the namespace/class that DIE is defined within,
20014 or "" if we can't tell. The caller should not xfree the result.
20015
20016 For example, if we're within the method foo() in the following
20017 code:
20018
20019 namespace N {
20020 class C {
20021 void foo () {
20022 }
20023 };
20024 }
20025
20026 then determine_prefix on foo's die will return "N::C". */
20027
20028 static const char *
20029 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
20030 {
20031 struct die_info *parent, *spec_die;
20032 struct dwarf2_cu *spec_cu;
20033 struct type *parent_type;
20034 const char *retval;
20035
20036 if (cu->language != language_cplus
20037 && cu->language != language_fortran && cu->language != language_d
20038 && cu->language != language_rust)
20039 return "";
20040
20041 retval = anonymous_struct_prefix (die, cu);
20042 if (retval)
20043 return retval;
20044
20045 /* We have to be careful in the presence of DW_AT_specification.
20046 For example, with GCC 3.4, given the code
20047
20048 namespace N {
20049 void foo() {
20050 // Definition of N::foo.
20051 }
20052 }
20053
20054 then we'll have a tree of DIEs like this:
20055
20056 1: DW_TAG_compile_unit
20057 2: DW_TAG_namespace // N
20058 3: DW_TAG_subprogram // declaration of N::foo
20059 4: DW_TAG_subprogram // definition of N::foo
20060 DW_AT_specification // refers to die #3
20061
20062 Thus, when processing die #4, we have to pretend that we're in
20063 the context of its DW_AT_specification, namely the contex of die
20064 #3. */
20065 spec_cu = cu;
20066 spec_die = die_specification (die, &spec_cu);
20067 if (spec_die == NULL)
20068 parent = die->parent;
20069 else
20070 {
20071 parent = spec_die->parent;
20072 cu = spec_cu;
20073 }
20074
20075 if (parent == NULL)
20076 return "";
20077 else if (parent->building_fullname)
20078 {
20079 const char *name;
20080 const char *parent_name;
20081
20082 /* It has been seen on RealView 2.2 built binaries,
20083 DW_TAG_template_type_param types actually _defined_ as
20084 children of the parent class:
20085
20086 enum E {};
20087 template class <class Enum> Class{};
20088 Class<enum E> class_e;
20089
20090 1: DW_TAG_class_type (Class)
20091 2: DW_TAG_enumeration_type (E)
20092 3: DW_TAG_enumerator (enum1:0)
20093 3: DW_TAG_enumerator (enum2:1)
20094 ...
20095 2: DW_TAG_template_type_param
20096 DW_AT_type DW_FORM_ref_udata (E)
20097
20098 Besides being broken debug info, it can put GDB into an
20099 infinite loop. Consider:
20100
20101 When we're building the full name for Class<E>, we'll start
20102 at Class, and go look over its template type parameters,
20103 finding E. We'll then try to build the full name of E, and
20104 reach here. We're now trying to build the full name of E,
20105 and look over the parent DIE for containing scope. In the
20106 broken case, if we followed the parent DIE of E, we'd again
20107 find Class, and once again go look at its template type
20108 arguments, etc., etc. Simply don't consider such parent die
20109 as source-level parent of this die (it can't be, the language
20110 doesn't allow it), and break the loop here. */
20111 name = dwarf2_name (die, cu);
20112 parent_name = dwarf2_name (parent, cu);
20113 complaint (&symfile_complaints,
20114 _("template param type '%s' defined within parent '%s'"),
20115 name ? name : "<unknown>",
20116 parent_name ? parent_name : "<unknown>");
20117 return "";
20118 }
20119 else
20120 switch (parent->tag)
20121 {
20122 case DW_TAG_namespace:
20123 parent_type = read_type_die (parent, cu);
20124 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
20125 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
20126 Work around this problem here. */
20127 if (cu->language == language_cplus
20128 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
20129 return "";
20130 /* We give a name to even anonymous namespaces. */
20131 return TYPE_TAG_NAME (parent_type);
20132 case DW_TAG_class_type:
20133 case DW_TAG_interface_type:
20134 case DW_TAG_structure_type:
20135 case DW_TAG_union_type:
20136 case DW_TAG_module:
20137 parent_type = read_type_die (parent, cu);
20138 if (TYPE_TAG_NAME (parent_type) != NULL)
20139 return TYPE_TAG_NAME (parent_type);
20140 else
20141 /* An anonymous structure is only allowed non-static data
20142 members; no typedefs, no member functions, et cetera.
20143 So it does not need a prefix. */
20144 return "";
20145 case DW_TAG_compile_unit:
20146 case DW_TAG_partial_unit:
20147 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
20148 if (cu->language == language_cplus
20149 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
20150 && die->child != NULL
20151 && (die->tag == DW_TAG_class_type
20152 || die->tag == DW_TAG_structure_type
20153 || die->tag == DW_TAG_union_type))
20154 {
20155 char *name = guess_full_die_structure_name (die, cu);
20156 if (name != NULL)
20157 return name;
20158 }
20159 return "";
20160 case DW_TAG_enumeration_type:
20161 parent_type = read_type_die (parent, cu);
20162 if (TYPE_DECLARED_CLASS (parent_type))
20163 {
20164 if (TYPE_TAG_NAME (parent_type) != NULL)
20165 return TYPE_TAG_NAME (parent_type);
20166 return "";
20167 }
20168 /* Fall through. */
20169 default:
20170 return determine_prefix (parent, cu);
20171 }
20172 }
20173
20174 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
20175 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
20176 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
20177 an obconcat, otherwise allocate storage for the result. The CU argument is
20178 used to determine the language and hence, the appropriate separator. */
20179
20180 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
20181
20182 static char *
20183 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
20184 int physname, struct dwarf2_cu *cu)
20185 {
20186 const char *lead = "";
20187 const char *sep;
20188
20189 if (suffix == NULL || suffix[0] == '\0'
20190 || prefix == NULL || prefix[0] == '\0')
20191 sep = "";
20192 else if (cu->language == language_d)
20193 {
20194 /* For D, the 'main' function could be defined in any module, but it
20195 should never be prefixed. */
20196 if (strcmp (suffix, "D main") == 0)
20197 {
20198 prefix = "";
20199 sep = "";
20200 }
20201 else
20202 sep = ".";
20203 }
20204 else if (cu->language == language_fortran && physname)
20205 {
20206 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
20207 DW_AT_MIPS_linkage_name is preferred and used instead. */
20208
20209 lead = "__";
20210 sep = "_MOD_";
20211 }
20212 else
20213 sep = "::";
20214
20215 if (prefix == NULL)
20216 prefix = "";
20217 if (suffix == NULL)
20218 suffix = "";
20219
20220 if (obs == NULL)
20221 {
20222 char *retval
20223 = ((char *)
20224 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
20225
20226 strcpy (retval, lead);
20227 strcat (retval, prefix);
20228 strcat (retval, sep);
20229 strcat (retval, suffix);
20230 return retval;
20231 }
20232 else
20233 {
20234 /* We have an obstack. */
20235 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
20236 }
20237 }
20238
20239 /* Return sibling of die, NULL if no sibling. */
20240
20241 static struct die_info *
20242 sibling_die (struct die_info *die)
20243 {
20244 return die->sibling;
20245 }
20246
20247 /* Get name of a die, return NULL if not found. */
20248
20249 static const char *
20250 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
20251 struct obstack *obstack)
20252 {
20253 if (name && cu->language == language_cplus)
20254 {
20255 std::string canon_name = cp_canonicalize_string (name);
20256
20257 if (!canon_name.empty ())
20258 {
20259 if (canon_name != name)
20260 name = (const char *) obstack_copy0 (obstack,
20261 canon_name.c_str (),
20262 canon_name.length ());
20263 }
20264 }
20265
20266 return name;
20267 }
20268
20269 /* Get name of a die, return NULL if not found.
20270 Anonymous namespaces are converted to their magic string. */
20271
20272 static const char *
20273 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
20274 {
20275 struct attribute *attr;
20276
20277 attr = dwarf2_attr (die, DW_AT_name, cu);
20278 if ((!attr || !DW_STRING (attr))
20279 && die->tag != DW_TAG_namespace
20280 && die->tag != DW_TAG_class_type
20281 && die->tag != DW_TAG_interface_type
20282 && die->tag != DW_TAG_structure_type
20283 && die->tag != DW_TAG_union_type)
20284 return NULL;
20285
20286 switch (die->tag)
20287 {
20288 case DW_TAG_compile_unit:
20289 case DW_TAG_partial_unit:
20290 /* Compilation units have a DW_AT_name that is a filename, not
20291 a source language identifier. */
20292 case DW_TAG_enumeration_type:
20293 case DW_TAG_enumerator:
20294 /* These tags always have simple identifiers already; no need
20295 to canonicalize them. */
20296 return DW_STRING (attr);
20297
20298 case DW_TAG_namespace:
20299 if (attr != NULL && DW_STRING (attr) != NULL)
20300 return DW_STRING (attr);
20301 return CP_ANONYMOUS_NAMESPACE_STR;
20302
20303 case DW_TAG_class_type:
20304 case DW_TAG_interface_type:
20305 case DW_TAG_structure_type:
20306 case DW_TAG_union_type:
20307 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
20308 structures or unions. These were of the form "._%d" in GCC 4.1,
20309 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
20310 and GCC 4.4. We work around this problem by ignoring these. */
20311 if (attr && DW_STRING (attr)
20312 && (startswith (DW_STRING (attr), "._")
20313 || startswith (DW_STRING (attr), "<anonymous")))
20314 return NULL;
20315
20316 /* GCC might emit a nameless typedef that has a linkage name. See
20317 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20318 if (!attr || DW_STRING (attr) == NULL)
20319 {
20320 char *demangled = NULL;
20321
20322 attr = dw2_linkage_name_attr (die, cu);
20323 if (attr == NULL || DW_STRING (attr) == NULL)
20324 return NULL;
20325
20326 /* Avoid demangling DW_STRING (attr) the second time on a second
20327 call for the same DIE. */
20328 if (!DW_STRING_IS_CANONICAL (attr))
20329 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
20330
20331 if (demangled)
20332 {
20333 const char *base;
20334
20335 /* FIXME: we already did this for the partial symbol... */
20336 DW_STRING (attr)
20337 = ((const char *)
20338 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
20339 demangled, strlen (demangled)));
20340 DW_STRING_IS_CANONICAL (attr) = 1;
20341 xfree (demangled);
20342
20343 /* Strip any leading namespaces/classes, keep only the base name.
20344 DW_AT_name for named DIEs does not contain the prefixes. */
20345 base = strrchr (DW_STRING (attr), ':');
20346 if (base && base > DW_STRING (attr) && base[-1] == ':')
20347 return &base[1];
20348 else
20349 return DW_STRING (attr);
20350 }
20351 }
20352 break;
20353
20354 default:
20355 break;
20356 }
20357
20358 if (!DW_STRING_IS_CANONICAL (attr))
20359 {
20360 DW_STRING (attr)
20361 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
20362 &cu->objfile->per_bfd->storage_obstack);
20363 DW_STRING_IS_CANONICAL (attr) = 1;
20364 }
20365 return DW_STRING (attr);
20366 }
20367
20368 /* Return the die that this die in an extension of, or NULL if there
20369 is none. *EXT_CU is the CU containing DIE on input, and the CU
20370 containing the return value on output. */
20371
20372 static struct die_info *
20373 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
20374 {
20375 struct attribute *attr;
20376
20377 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
20378 if (attr == NULL)
20379 return NULL;
20380
20381 return follow_die_ref (die, attr, ext_cu);
20382 }
20383
20384 /* Convert a DIE tag into its string name. */
20385
20386 static const char *
20387 dwarf_tag_name (unsigned tag)
20388 {
20389 const char *name = get_DW_TAG_name (tag);
20390
20391 if (name == NULL)
20392 return "DW_TAG_<unknown>";
20393
20394 return name;
20395 }
20396
20397 /* Convert a DWARF attribute code into its string name. */
20398
20399 static const char *
20400 dwarf_attr_name (unsigned attr)
20401 {
20402 const char *name;
20403
20404 #ifdef MIPS /* collides with DW_AT_HP_block_index */
20405 if (attr == DW_AT_MIPS_fde)
20406 return "DW_AT_MIPS_fde";
20407 #else
20408 if (attr == DW_AT_HP_block_index)
20409 return "DW_AT_HP_block_index";
20410 #endif
20411
20412 name = get_DW_AT_name (attr);
20413
20414 if (name == NULL)
20415 return "DW_AT_<unknown>";
20416
20417 return name;
20418 }
20419
20420 /* Convert a DWARF value form code into its string name. */
20421
20422 static const char *
20423 dwarf_form_name (unsigned form)
20424 {
20425 const char *name = get_DW_FORM_name (form);
20426
20427 if (name == NULL)
20428 return "DW_FORM_<unknown>";
20429
20430 return name;
20431 }
20432
20433 static const char *
20434 dwarf_bool_name (unsigned mybool)
20435 {
20436 if (mybool)
20437 return "TRUE";
20438 else
20439 return "FALSE";
20440 }
20441
20442 /* Convert a DWARF type code into its string name. */
20443
20444 static const char *
20445 dwarf_type_encoding_name (unsigned enc)
20446 {
20447 const char *name = get_DW_ATE_name (enc);
20448
20449 if (name == NULL)
20450 return "DW_ATE_<unknown>";
20451
20452 return name;
20453 }
20454
20455 static void
20456 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
20457 {
20458 unsigned int i;
20459
20460 print_spaces (indent, f);
20461 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
20462 dwarf_tag_name (die->tag), die->abbrev,
20463 to_underlying (die->sect_off));
20464
20465 if (die->parent != NULL)
20466 {
20467 print_spaces (indent, f);
20468 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
20469 to_underlying (die->parent->sect_off));
20470 }
20471
20472 print_spaces (indent, f);
20473 fprintf_unfiltered (f, " has children: %s\n",
20474 dwarf_bool_name (die->child != NULL));
20475
20476 print_spaces (indent, f);
20477 fprintf_unfiltered (f, " attributes:\n");
20478
20479 for (i = 0; i < die->num_attrs; ++i)
20480 {
20481 print_spaces (indent, f);
20482 fprintf_unfiltered (f, " %s (%s) ",
20483 dwarf_attr_name (die->attrs[i].name),
20484 dwarf_form_name (die->attrs[i].form));
20485
20486 switch (die->attrs[i].form)
20487 {
20488 case DW_FORM_addr:
20489 case DW_FORM_GNU_addr_index:
20490 fprintf_unfiltered (f, "address: ");
20491 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
20492 break;
20493 case DW_FORM_block2:
20494 case DW_FORM_block4:
20495 case DW_FORM_block:
20496 case DW_FORM_block1:
20497 fprintf_unfiltered (f, "block: size %s",
20498 pulongest (DW_BLOCK (&die->attrs[i])->size));
20499 break;
20500 case DW_FORM_exprloc:
20501 fprintf_unfiltered (f, "expression: size %s",
20502 pulongest (DW_BLOCK (&die->attrs[i])->size));
20503 break;
20504 case DW_FORM_data16:
20505 fprintf_unfiltered (f, "constant of 16 bytes");
20506 break;
20507 case DW_FORM_ref_addr:
20508 fprintf_unfiltered (f, "ref address: ");
20509 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
20510 break;
20511 case DW_FORM_GNU_ref_alt:
20512 fprintf_unfiltered (f, "alt ref address: ");
20513 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
20514 break;
20515 case DW_FORM_ref1:
20516 case DW_FORM_ref2:
20517 case DW_FORM_ref4:
20518 case DW_FORM_ref8:
20519 case DW_FORM_ref_udata:
20520 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
20521 (long) (DW_UNSND (&die->attrs[i])));
20522 break;
20523 case DW_FORM_data1:
20524 case DW_FORM_data2:
20525 case DW_FORM_data4:
20526 case DW_FORM_data8:
20527 case DW_FORM_udata:
20528 case DW_FORM_sdata:
20529 fprintf_unfiltered (f, "constant: %s",
20530 pulongest (DW_UNSND (&die->attrs[i])));
20531 break;
20532 case DW_FORM_sec_offset:
20533 fprintf_unfiltered (f, "section offset: %s",
20534 pulongest (DW_UNSND (&die->attrs[i])));
20535 break;
20536 case DW_FORM_ref_sig8:
20537 fprintf_unfiltered (f, "signature: %s",
20538 hex_string (DW_SIGNATURE (&die->attrs[i])));
20539 break;
20540 case DW_FORM_string:
20541 case DW_FORM_strp:
20542 case DW_FORM_line_strp:
20543 case DW_FORM_GNU_str_index:
20544 case DW_FORM_GNU_strp_alt:
20545 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
20546 DW_STRING (&die->attrs[i])
20547 ? DW_STRING (&die->attrs[i]) : "",
20548 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
20549 break;
20550 case DW_FORM_flag:
20551 if (DW_UNSND (&die->attrs[i]))
20552 fprintf_unfiltered (f, "flag: TRUE");
20553 else
20554 fprintf_unfiltered (f, "flag: FALSE");
20555 break;
20556 case DW_FORM_flag_present:
20557 fprintf_unfiltered (f, "flag: TRUE");
20558 break;
20559 case DW_FORM_indirect:
20560 /* The reader will have reduced the indirect form to
20561 the "base form" so this form should not occur. */
20562 fprintf_unfiltered (f,
20563 "unexpected attribute form: DW_FORM_indirect");
20564 break;
20565 case DW_FORM_implicit_const:
20566 fprintf_unfiltered (f, "constant: %s",
20567 plongest (DW_SND (&die->attrs[i])));
20568 break;
20569 default:
20570 fprintf_unfiltered (f, "unsupported attribute form: %d.",
20571 die->attrs[i].form);
20572 break;
20573 }
20574 fprintf_unfiltered (f, "\n");
20575 }
20576 }
20577
20578 static void
20579 dump_die_for_error (struct die_info *die)
20580 {
20581 dump_die_shallow (gdb_stderr, 0, die);
20582 }
20583
20584 static void
20585 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
20586 {
20587 int indent = level * 4;
20588
20589 gdb_assert (die != NULL);
20590
20591 if (level >= max_level)
20592 return;
20593
20594 dump_die_shallow (f, indent, die);
20595
20596 if (die->child != NULL)
20597 {
20598 print_spaces (indent, f);
20599 fprintf_unfiltered (f, " Children:");
20600 if (level + 1 < max_level)
20601 {
20602 fprintf_unfiltered (f, "\n");
20603 dump_die_1 (f, level + 1, max_level, die->child);
20604 }
20605 else
20606 {
20607 fprintf_unfiltered (f,
20608 " [not printed, max nesting level reached]\n");
20609 }
20610 }
20611
20612 if (die->sibling != NULL && level > 0)
20613 {
20614 dump_die_1 (f, level, max_level, die->sibling);
20615 }
20616 }
20617
20618 /* This is called from the pdie macro in gdbinit.in.
20619 It's not static so gcc will keep a copy callable from gdb. */
20620
20621 void
20622 dump_die (struct die_info *die, int max_level)
20623 {
20624 dump_die_1 (gdb_stdlog, 0, max_level, die);
20625 }
20626
20627 static void
20628 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
20629 {
20630 void **slot;
20631
20632 slot = htab_find_slot_with_hash (cu->die_hash, die,
20633 to_underlying (die->sect_off),
20634 INSERT);
20635
20636 *slot = die;
20637 }
20638
20639 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
20640 required kind. */
20641
20642 static sect_offset
20643 dwarf2_get_ref_die_offset (const struct attribute *attr)
20644 {
20645 if (attr_form_is_ref (attr))
20646 return (sect_offset) DW_UNSND (attr);
20647
20648 complaint (&symfile_complaints,
20649 _("unsupported die ref attribute form: '%s'"),
20650 dwarf_form_name (attr->form));
20651 return {};
20652 }
20653
20654 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
20655 * the value held by the attribute is not constant. */
20656
20657 static LONGEST
20658 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
20659 {
20660 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
20661 return DW_SND (attr);
20662 else if (attr->form == DW_FORM_udata
20663 || attr->form == DW_FORM_data1
20664 || attr->form == DW_FORM_data2
20665 || attr->form == DW_FORM_data4
20666 || attr->form == DW_FORM_data8)
20667 return DW_UNSND (attr);
20668 else
20669 {
20670 /* For DW_FORM_data16 see attr_form_is_constant. */
20671 complaint (&symfile_complaints,
20672 _("Attribute value is not a constant (%s)"),
20673 dwarf_form_name (attr->form));
20674 return default_value;
20675 }
20676 }
20677
20678 /* Follow reference or signature attribute ATTR of SRC_DIE.
20679 On entry *REF_CU is the CU of SRC_DIE.
20680 On exit *REF_CU is the CU of the result. */
20681
20682 static struct die_info *
20683 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
20684 struct dwarf2_cu **ref_cu)
20685 {
20686 struct die_info *die;
20687
20688 if (attr_form_is_ref (attr))
20689 die = follow_die_ref (src_die, attr, ref_cu);
20690 else if (attr->form == DW_FORM_ref_sig8)
20691 die = follow_die_sig (src_die, attr, ref_cu);
20692 else
20693 {
20694 dump_die_for_error (src_die);
20695 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
20696 objfile_name ((*ref_cu)->objfile));
20697 }
20698
20699 return die;
20700 }
20701
20702 /* Follow reference OFFSET.
20703 On entry *REF_CU is the CU of the source die referencing OFFSET.
20704 On exit *REF_CU is the CU of the result.
20705 Returns NULL if OFFSET is invalid. */
20706
20707 static struct die_info *
20708 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
20709 struct dwarf2_cu **ref_cu)
20710 {
20711 struct die_info temp_die;
20712 struct dwarf2_cu *target_cu, *cu = *ref_cu;
20713
20714 gdb_assert (cu->per_cu != NULL);
20715
20716 target_cu = cu;
20717
20718 if (cu->per_cu->is_debug_types)
20719 {
20720 /* .debug_types CUs cannot reference anything outside their CU.
20721 If they need to, they have to reference a signatured type via
20722 DW_FORM_ref_sig8. */
20723 if (!offset_in_cu_p (&cu->header, sect_off))
20724 return NULL;
20725 }
20726 else if (offset_in_dwz != cu->per_cu->is_dwz
20727 || !offset_in_cu_p (&cu->header, sect_off))
20728 {
20729 struct dwarf2_per_cu_data *per_cu;
20730
20731 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
20732 cu->objfile);
20733
20734 /* If necessary, add it to the queue and load its DIEs. */
20735 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
20736 load_full_comp_unit (per_cu, cu->language);
20737
20738 target_cu = per_cu->cu;
20739 }
20740 else if (cu->dies == NULL)
20741 {
20742 /* We're loading full DIEs during partial symbol reading. */
20743 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
20744 load_full_comp_unit (cu->per_cu, language_minimal);
20745 }
20746
20747 *ref_cu = target_cu;
20748 temp_die.sect_off = sect_off;
20749 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
20750 &temp_die,
20751 to_underlying (sect_off));
20752 }
20753
20754 /* Follow reference attribute ATTR of SRC_DIE.
20755 On entry *REF_CU is the CU of SRC_DIE.
20756 On exit *REF_CU is the CU of the result. */
20757
20758 static struct die_info *
20759 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
20760 struct dwarf2_cu **ref_cu)
20761 {
20762 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
20763 struct dwarf2_cu *cu = *ref_cu;
20764 struct die_info *die;
20765
20766 die = follow_die_offset (sect_off,
20767 (attr->form == DW_FORM_GNU_ref_alt
20768 || cu->per_cu->is_dwz),
20769 ref_cu);
20770 if (!die)
20771 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
20772 "at 0x%x [in module %s]"),
20773 to_underlying (sect_off), to_underlying (src_die->sect_off),
20774 objfile_name (cu->objfile));
20775
20776 return die;
20777 }
20778
20779 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
20780 Returned value is intended for DW_OP_call*. Returned
20781 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
20782
20783 struct dwarf2_locexpr_baton
20784 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
20785 struct dwarf2_per_cu_data *per_cu,
20786 CORE_ADDR (*get_frame_pc) (void *baton),
20787 void *baton)
20788 {
20789 struct dwarf2_cu *cu;
20790 struct die_info *die;
20791 struct attribute *attr;
20792 struct dwarf2_locexpr_baton retval;
20793
20794 dw2_setup (per_cu->objfile);
20795
20796 if (per_cu->cu == NULL)
20797 load_cu (per_cu);
20798 cu = per_cu->cu;
20799 if (cu == NULL)
20800 {
20801 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20802 Instead just throw an error, not much else we can do. */
20803 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
20804 to_underlying (sect_off), objfile_name (per_cu->objfile));
20805 }
20806
20807 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
20808 if (!die)
20809 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
20810 to_underlying (sect_off), objfile_name (per_cu->objfile));
20811
20812 attr = dwarf2_attr (die, DW_AT_location, cu);
20813 if (!attr)
20814 {
20815 /* DWARF: "If there is no such attribute, then there is no effect.".
20816 DATA is ignored if SIZE is 0. */
20817
20818 retval.data = NULL;
20819 retval.size = 0;
20820 }
20821 else if (attr_form_is_section_offset (attr))
20822 {
20823 struct dwarf2_loclist_baton loclist_baton;
20824 CORE_ADDR pc = (*get_frame_pc) (baton);
20825 size_t size;
20826
20827 fill_in_loclist_baton (cu, &loclist_baton, attr);
20828
20829 retval.data = dwarf2_find_location_expression (&loclist_baton,
20830 &size, pc);
20831 retval.size = size;
20832 }
20833 else
20834 {
20835 if (!attr_form_is_block (attr))
20836 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
20837 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
20838 to_underlying (sect_off), objfile_name (per_cu->objfile));
20839
20840 retval.data = DW_BLOCK (attr)->data;
20841 retval.size = DW_BLOCK (attr)->size;
20842 }
20843 retval.per_cu = cu->per_cu;
20844
20845 age_cached_comp_units ();
20846
20847 return retval;
20848 }
20849
20850 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
20851 offset. */
20852
20853 struct dwarf2_locexpr_baton
20854 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
20855 struct dwarf2_per_cu_data *per_cu,
20856 CORE_ADDR (*get_frame_pc) (void *baton),
20857 void *baton)
20858 {
20859 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
20860
20861 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
20862 }
20863
20864 /* Write a constant of a given type as target-ordered bytes into
20865 OBSTACK. */
20866
20867 static const gdb_byte *
20868 write_constant_as_bytes (struct obstack *obstack,
20869 enum bfd_endian byte_order,
20870 struct type *type,
20871 ULONGEST value,
20872 LONGEST *len)
20873 {
20874 gdb_byte *result;
20875
20876 *len = TYPE_LENGTH (type);
20877 result = (gdb_byte *) obstack_alloc (obstack, *len);
20878 store_unsigned_integer (result, *len, byte_order, value);
20879
20880 return result;
20881 }
20882
20883 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
20884 pointer to the constant bytes and set LEN to the length of the
20885 data. If memory is needed, allocate it on OBSTACK. If the DIE
20886 does not have a DW_AT_const_value, return NULL. */
20887
20888 const gdb_byte *
20889 dwarf2_fetch_constant_bytes (sect_offset sect_off,
20890 struct dwarf2_per_cu_data *per_cu,
20891 struct obstack *obstack,
20892 LONGEST *len)
20893 {
20894 struct dwarf2_cu *cu;
20895 struct die_info *die;
20896 struct attribute *attr;
20897 const gdb_byte *result = NULL;
20898 struct type *type;
20899 LONGEST value;
20900 enum bfd_endian byte_order;
20901
20902 dw2_setup (per_cu->objfile);
20903
20904 if (per_cu->cu == NULL)
20905 load_cu (per_cu);
20906 cu = per_cu->cu;
20907 if (cu == NULL)
20908 {
20909 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20910 Instead just throw an error, not much else we can do. */
20911 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
20912 to_underlying (sect_off), objfile_name (per_cu->objfile));
20913 }
20914
20915 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
20916 if (!die)
20917 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
20918 to_underlying (sect_off), objfile_name (per_cu->objfile));
20919
20920
20921 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20922 if (attr == NULL)
20923 return NULL;
20924
20925 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
20926 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20927
20928 switch (attr->form)
20929 {
20930 case DW_FORM_addr:
20931 case DW_FORM_GNU_addr_index:
20932 {
20933 gdb_byte *tem;
20934
20935 *len = cu->header.addr_size;
20936 tem = (gdb_byte *) obstack_alloc (obstack, *len);
20937 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
20938 result = tem;
20939 }
20940 break;
20941 case DW_FORM_string:
20942 case DW_FORM_strp:
20943 case DW_FORM_GNU_str_index:
20944 case DW_FORM_GNU_strp_alt:
20945 /* DW_STRING is already allocated on the objfile obstack, point
20946 directly to it. */
20947 result = (const gdb_byte *) DW_STRING (attr);
20948 *len = strlen (DW_STRING (attr));
20949 break;
20950 case DW_FORM_block1:
20951 case DW_FORM_block2:
20952 case DW_FORM_block4:
20953 case DW_FORM_block:
20954 case DW_FORM_exprloc:
20955 case DW_FORM_data16:
20956 result = DW_BLOCK (attr)->data;
20957 *len = DW_BLOCK (attr)->size;
20958 break;
20959
20960 /* The DW_AT_const_value attributes are supposed to carry the
20961 symbol's value "represented as it would be on the target
20962 architecture." By the time we get here, it's already been
20963 converted to host endianness, so we just need to sign- or
20964 zero-extend it as appropriate. */
20965 case DW_FORM_data1:
20966 type = die_type (die, cu);
20967 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
20968 if (result == NULL)
20969 result = write_constant_as_bytes (obstack, byte_order,
20970 type, value, len);
20971 break;
20972 case DW_FORM_data2:
20973 type = die_type (die, cu);
20974 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
20975 if (result == NULL)
20976 result = write_constant_as_bytes (obstack, byte_order,
20977 type, value, len);
20978 break;
20979 case DW_FORM_data4:
20980 type = die_type (die, cu);
20981 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
20982 if (result == NULL)
20983 result = write_constant_as_bytes (obstack, byte_order,
20984 type, value, len);
20985 break;
20986 case DW_FORM_data8:
20987 type = die_type (die, cu);
20988 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
20989 if (result == NULL)
20990 result = write_constant_as_bytes (obstack, byte_order,
20991 type, value, len);
20992 break;
20993
20994 case DW_FORM_sdata:
20995 case DW_FORM_implicit_const:
20996 type = die_type (die, cu);
20997 result = write_constant_as_bytes (obstack, byte_order,
20998 type, DW_SND (attr), len);
20999 break;
21000
21001 case DW_FORM_udata:
21002 type = die_type (die, cu);
21003 result = write_constant_as_bytes (obstack, byte_order,
21004 type, DW_UNSND (attr), len);
21005 break;
21006
21007 default:
21008 complaint (&symfile_complaints,
21009 _("unsupported const value attribute form: '%s'"),
21010 dwarf_form_name (attr->form));
21011 break;
21012 }
21013
21014 return result;
21015 }
21016
21017 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
21018 valid type for this die is found. */
21019
21020 struct type *
21021 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
21022 struct dwarf2_per_cu_data *per_cu)
21023 {
21024 struct dwarf2_cu *cu;
21025 struct die_info *die;
21026
21027 dw2_setup (per_cu->objfile);
21028
21029 if (per_cu->cu == NULL)
21030 load_cu (per_cu);
21031 cu = per_cu->cu;
21032 if (!cu)
21033 return NULL;
21034
21035 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
21036 if (!die)
21037 return NULL;
21038
21039 return die_type (die, cu);
21040 }
21041
21042 /* Return the type of the DIE at DIE_OFFSET in the CU named by
21043 PER_CU. */
21044
21045 struct type *
21046 dwarf2_get_die_type (cu_offset die_offset,
21047 struct dwarf2_per_cu_data *per_cu)
21048 {
21049 dw2_setup (per_cu->objfile);
21050
21051 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
21052 return get_die_type_at_offset (die_offset_sect, per_cu);
21053 }
21054
21055 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
21056 On entry *REF_CU is the CU of SRC_DIE.
21057 On exit *REF_CU is the CU of the result.
21058 Returns NULL if the referenced DIE isn't found. */
21059
21060 static struct die_info *
21061 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
21062 struct dwarf2_cu **ref_cu)
21063 {
21064 struct die_info temp_die;
21065 struct dwarf2_cu *sig_cu;
21066 struct die_info *die;
21067
21068 /* While it might be nice to assert sig_type->type == NULL here,
21069 we can get here for DW_AT_imported_declaration where we need
21070 the DIE not the type. */
21071
21072 /* If necessary, add it to the queue and load its DIEs. */
21073
21074 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
21075 read_signatured_type (sig_type);
21076
21077 sig_cu = sig_type->per_cu.cu;
21078 gdb_assert (sig_cu != NULL);
21079 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
21080 temp_die.sect_off = sig_type->type_offset_in_section;
21081 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
21082 to_underlying (temp_die.sect_off));
21083 if (die)
21084 {
21085 /* For .gdb_index version 7 keep track of included TUs.
21086 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
21087 if (dwarf2_per_objfile->index_table != NULL
21088 && dwarf2_per_objfile->index_table->version <= 7)
21089 {
21090 VEC_safe_push (dwarf2_per_cu_ptr,
21091 (*ref_cu)->per_cu->imported_symtabs,
21092 sig_cu->per_cu);
21093 }
21094
21095 *ref_cu = sig_cu;
21096 return die;
21097 }
21098
21099 return NULL;
21100 }
21101
21102 /* Follow signatured type referenced by ATTR in SRC_DIE.
21103 On entry *REF_CU is the CU of SRC_DIE.
21104 On exit *REF_CU is the CU of the result.
21105 The result is the DIE of the type.
21106 If the referenced type cannot be found an error is thrown. */
21107
21108 static struct die_info *
21109 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
21110 struct dwarf2_cu **ref_cu)
21111 {
21112 ULONGEST signature = DW_SIGNATURE (attr);
21113 struct signatured_type *sig_type;
21114 struct die_info *die;
21115
21116 gdb_assert (attr->form == DW_FORM_ref_sig8);
21117
21118 sig_type = lookup_signatured_type (*ref_cu, signature);
21119 /* sig_type will be NULL if the signatured type is missing from
21120 the debug info. */
21121 if (sig_type == NULL)
21122 {
21123 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
21124 " from DIE at 0x%x [in module %s]"),
21125 hex_string (signature), to_underlying (src_die->sect_off),
21126 objfile_name ((*ref_cu)->objfile));
21127 }
21128
21129 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
21130 if (die == NULL)
21131 {
21132 dump_die_for_error (src_die);
21133 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
21134 " from DIE at 0x%x [in module %s]"),
21135 hex_string (signature), to_underlying (src_die->sect_off),
21136 objfile_name ((*ref_cu)->objfile));
21137 }
21138
21139 return die;
21140 }
21141
21142 /* Get the type specified by SIGNATURE referenced in DIE/CU,
21143 reading in and processing the type unit if necessary. */
21144
21145 static struct type *
21146 get_signatured_type (struct die_info *die, ULONGEST signature,
21147 struct dwarf2_cu *cu)
21148 {
21149 struct signatured_type *sig_type;
21150 struct dwarf2_cu *type_cu;
21151 struct die_info *type_die;
21152 struct type *type;
21153
21154 sig_type = lookup_signatured_type (cu, signature);
21155 /* sig_type will be NULL if the signatured type is missing from
21156 the debug info. */
21157 if (sig_type == NULL)
21158 {
21159 complaint (&symfile_complaints,
21160 _("Dwarf Error: Cannot find signatured DIE %s referenced"
21161 " from DIE at 0x%x [in module %s]"),
21162 hex_string (signature), to_underlying (die->sect_off),
21163 objfile_name (dwarf2_per_objfile->objfile));
21164 return build_error_marker_type (cu, die);
21165 }
21166
21167 /* If we already know the type we're done. */
21168 if (sig_type->type != NULL)
21169 return sig_type->type;
21170
21171 type_cu = cu;
21172 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
21173 if (type_die != NULL)
21174 {
21175 /* N.B. We need to call get_die_type to ensure only one type for this DIE
21176 is created. This is important, for example, because for c++ classes
21177 we need TYPE_NAME set which is only done by new_symbol. Blech. */
21178 type = read_type_die (type_die, type_cu);
21179 if (type == NULL)
21180 {
21181 complaint (&symfile_complaints,
21182 _("Dwarf Error: Cannot build signatured type %s"
21183 " referenced from DIE at 0x%x [in module %s]"),
21184 hex_string (signature), to_underlying (die->sect_off),
21185 objfile_name (dwarf2_per_objfile->objfile));
21186 type = build_error_marker_type (cu, die);
21187 }
21188 }
21189 else
21190 {
21191 complaint (&symfile_complaints,
21192 _("Dwarf Error: Problem reading signatured DIE %s referenced"
21193 " from DIE at 0x%x [in module %s]"),
21194 hex_string (signature), to_underlying (die->sect_off),
21195 objfile_name (dwarf2_per_objfile->objfile));
21196 type = build_error_marker_type (cu, die);
21197 }
21198 sig_type->type = type;
21199
21200 return type;
21201 }
21202
21203 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
21204 reading in and processing the type unit if necessary. */
21205
21206 static struct type *
21207 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
21208 struct dwarf2_cu *cu) /* ARI: editCase function */
21209 {
21210 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
21211 if (attr_form_is_ref (attr))
21212 {
21213 struct dwarf2_cu *type_cu = cu;
21214 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
21215
21216 return read_type_die (type_die, type_cu);
21217 }
21218 else if (attr->form == DW_FORM_ref_sig8)
21219 {
21220 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
21221 }
21222 else
21223 {
21224 complaint (&symfile_complaints,
21225 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
21226 " at 0x%x [in module %s]"),
21227 dwarf_form_name (attr->form), to_underlying (die->sect_off),
21228 objfile_name (dwarf2_per_objfile->objfile));
21229 return build_error_marker_type (cu, die);
21230 }
21231 }
21232
21233 /* Load the DIEs associated with type unit PER_CU into memory. */
21234
21235 static void
21236 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
21237 {
21238 struct signatured_type *sig_type;
21239
21240 /* Caller is responsible for ensuring type_unit_groups don't get here. */
21241 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
21242
21243 /* We have the per_cu, but we need the signatured_type.
21244 Fortunately this is an easy translation. */
21245 gdb_assert (per_cu->is_debug_types);
21246 sig_type = (struct signatured_type *) per_cu;
21247
21248 gdb_assert (per_cu->cu == NULL);
21249
21250 read_signatured_type (sig_type);
21251
21252 gdb_assert (per_cu->cu != NULL);
21253 }
21254
21255 /* die_reader_func for read_signatured_type.
21256 This is identical to load_full_comp_unit_reader,
21257 but is kept separate for now. */
21258
21259 static void
21260 read_signatured_type_reader (const struct die_reader_specs *reader,
21261 const gdb_byte *info_ptr,
21262 struct die_info *comp_unit_die,
21263 int has_children,
21264 void *data)
21265 {
21266 struct dwarf2_cu *cu = reader->cu;
21267
21268 gdb_assert (cu->die_hash == NULL);
21269 cu->die_hash =
21270 htab_create_alloc_ex (cu->header.length / 12,
21271 die_hash,
21272 die_eq,
21273 NULL,
21274 &cu->comp_unit_obstack,
21275 hashtab_obstack_allocate,
21276 dummy_obstack_deallocate);
21277
21278 if (has_children)
21279 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
21280 &info_ptr, comp_unit_die);
21281 cu->dies = comp_unit_die;
21282 /* comp_unit_die is not stored in die_hash, no need. */
21283
21284 /* We try not to read any attributes in this function, because not
21285 all CUs needed for references have been loaded yet, and symbol
21286 table processing isn't initialized. But we have to set the CU language,
21287 or we won't be able to build types correctly.
21288 Similarly, if we do not read the producer, we can not apply
21289 producer-specific interpretation. */
21290 prepare_one_comp_unit (cu, cu->dies, language_minimal);
21291 }
21292
21293 /* Read in a signatured type and build its CU and DIEs.
21294 If the type is a stub for the real type in a DWO file,
21295 read in the real type from the DWO file as well. */
21296
21297 static void
21298 read_signatured_type (struct signatured_type *sig_type)
21299 {
21300 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
21301
21302 gdb_assert (per_cu->is_debug_types);
21303 gdb_assert (per_cu->cu == NULL);
21304
21305 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
21306 read_signatured_type_reader, NULL);
21307 sig_type->per_cu.tu_read = 1;
21308 }
21309
21310 /* Decode simple location descriptions.
21311 Given a pointer to a dwarf block that defines a location, compute
21312 the location and return the value.
21313
21314 NOTE drow/2003-11-18: This function is called in two situations
21315 now: for the address of static or global variables (partial symbols
21316 only) and for offsets into structures which are expected to be
21317 (more or less) constant. The partial symbol case should go away,
21318 and only the constant case should remain. That will let this
21319 function complain more accurately. A few special modes are allowed
21320 without complaint for global variables (for instance, global
21321 register values and thread-local values).
21322
21323 A location description containing no operations indicates that the
21324 object is optimized out. The return value is 0 for that case.
21325 FIXME drow/2003-11-16: No callers check for this case any more; soon all
21326 callers will only want a very basic result and this can become a
21327 complaint.
21328
21329 Note that stack[0] is unused except as a default error return. */
21330
21331 static CORE_ADDR
21332 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
21333 {
21334 struct objfile *objfile = cu->objfile;
21335 size_t i;
21336 size_t size = blk->size;
21337 const gdb_byte *data = blk->data;
21338 CORE_ADDR stack[64];
21339 int stacki;
21340 unsigned int bytes_read, unsnd;
21341 gdb_byte op;
21342
21343 i = 0;
21344 stacki = 0;
21345 stack[stacki] = 0;
21346 stack[++stacki] = 0;
21347
21348 while (i < size)
21349 {
21350 op = data[i++];
21351 switch (op)
21352 {
21353 case DW_OP_lit0:
21354 case DW_OP_lit1:
21355 case DW_OP_lit2:
21356 case DW_OP_lit3:
21357 case DW_OP_lit4:
21358 case DW_OP_lit5:
21359 case DW_OP_lit6:
21360 case DW_OP_lit7:
21361 case DW_OP_lit8:
21362 case DW_OP_lit9:
21363 case DW_OP_lit10:
21364 case DW_OP_lit11:
21365 case DW_OP_lit12:
21366 case DW_OP_lit13:
21367 case DW_OP_lit14:
21368 case DW_OP_lit15:
21369 case DW_OP_lit16:
21370 case DW_OP_lit17:
21371 case DW_OP_lit18:
21372 case DW_OP_lit19:
21373 case DW_OP_lit20:
21374 case DW_OP_lit21:
21375 case DW_OP_lit22:
21376 case DW_OP_lit23:
21377 case DW_OP_lit24:
21378 case DW_OP_lit25:
21379 case DW_OP_lit26:
21380 case DW_OP_lit27:
21381 case DW_OP_lit28:
21382 case DW_OP_lit29:
21383 case DW_OP_lit30:
21384 case DW_OP_lit31:
21385 stack[++stacki] = op - DW_OP_lit0;
21386 break;
21387
21388 case DW_OP_reg0:
21389 case DW_OP_reg1:
21390 case DW_OP_reg2:
21391 case DW_OP_reg3:
21392 case DW_OP_reg4:
21393 case DW_OP_reg5:
21394 case DW_OP_reg6:
21395 case DW_OP_reg7:
21396 case DW_OP_reg8:
21397 case DW_OP_reg9:
21398 case DW_OP_reg10:
21399 case DW_OP_reg11:
21400 case DW_OP_reg12:
21401 case DW_OP_reg13:
21402 case DW_OP_reg14:
21403 case DW_OP_reg15:
21404 case DW_OP_reg16:
21405 case DW_OP_reg17:
21406 case DW_OP_reg18:
21407 case DW_OP_reg19:
21408 case DW_OP_reg20:
21409 case DW_OP_reg21:
21410 case DW_OP_reg22:
21411 case DW_OP_reg23:
21412 case DW_OP_reg24:
21413 case DW_OP_reg25:
21414 case DW_OP_reg26:
21415 case DW_OP_reg27:
21416 case DW_OP_reg28:
21417 case DW_OP_reg29:
21418 case DW_OP_reg30:
21419 case DW_OP_reg31:
21420 stack[++stacki] = op - DW_OP_reg0;
21421 if (i < size)
21422 dwarf2_complex_location_expr_complaint ();
21423 break;
21424
21425 case DW_OP_regx:
21426 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
21427 i += bytes_read;
21428 stack[++stacki] = unsnd;
21429 if (i < size)
21430 dwarf2_complex_location_expr_complaint ();
21431 break;
21432
21433 case DW_OP_addr:
21434 stack[++stacki] = read_address (objfile->obfd, &data[i],
21435 cu, &bytes_read);
21436 i += bytes_read;
21437 break;
21438
21439 case DW_OP_const1u:
21440 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
21441 i += 1;
21442 break;
21443
21444 case DW_OP_const1s:
21445 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
21446 i += 1;
21447 break;
21448
21449 case DW_OP_const2u:
21450 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
21451 i += 2;
21452 break;
21453
21454 case DW_OP_const2s:
21455 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
21456 i += 2;
21457 break;
21458
21459 case DW_OP_const4u:
21460 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
21461 i += 4;
21462 break;
21463
21464 case DW_OP_const4s:
21465 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
21466 i += 4;
21467 break;
21468
21469 case DW_OP_const8u:
21470 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
21471 i += 8;
21472 break;
21473
21474 case DW_OP_constu:
21475 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
21476 &bytes_read);
21477 i += bytes_read;
21478 break;
21479
21480 case DW_OP_consts:
21481 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
21482 i += bytes_read;
21483 break;
21484
21485 case DW_OP_dup:
21486 stack[stacki + 1] = stack[stacki];
21487 stacki++;
21488 break;
21489
21490 case DW_OP_plus:
21491 stack[stacki - 1] += stack[stacki];
21492 stacki--;
21493 break;
21494
21495 case DW_OP_plus_uconst:
21496 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
21497 &bytes_read);
21498 i += bytes_read;
21499 break;
21500
21501 case DW_OP_minus:
21502 stack[stacki - 1] -= stack[stacki];
21503 stacki--;
21504 break;
21505
21506 case DW_OP_deref:
21507 /* If we're not the last op, then we definitely can't encode
21508 this using GDB's address_class enum. This is valid for partial
21509 global symbols, although the variable's address will be bogus
21510 in the psymtab. */
21511 if (i < size)
21512 dwarf2_complex_location_expr_complaint ();
21513 break;
21514
21515 case DW_OP_GNU_push_tls_address:
21516 case DW_OP_form_tls_address:
21517 /* The top of the stack has the offset from the beginning
21518 of the thread control block at which the variable is located. */
21519 /* Nothing should follow this operator, so the top of stack would
21520 be returned. */
21521 /* This is valid for partial global symbols, but the variable's
21522 address will be bogus in the psymtab. Make it always at least
21523 non-zero to not look as a variable garbage collected by linker
21524 which have DW_OP_addr 0. */
21525 if (i < size)
21526 dwarf2_complex_location_expr_complaint ();
21527 stack[stacki]++;
21528 break;
21529
21530 case DW_OP_GNU_uninit:
21531 break;
21532
21533 case DW_OP_GNU_addr_index:
21534 case DW_OP_GNU_const_index:
21535 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
21536 &bytes_read);
21537 i += bytes_read;
21538 break;
21539
21540 default:
21541 {
21542 const char *name = get_DW_OP_name (op);
21543
21544 if (name)
21545 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
21546 name);
21547 else
21548 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
21549 op);
21550 }
21551
21552 return (stack[stacki]);
21553 }
21554
21555 /* Enforce maximum stack depth of SIZE-1 to avoid writing
21556 outside of the allocated space. Also enforce minimum>0. */
21557 if (stacki >= ARRAY_SIZE (stack) - 1)
21558 {
21559 complaint (&symfile_complaints,
21560 _("location description stack overflow"));
21561 return 0;
21562 }
21563
21564 if (stacki <= 0)
21565 {
21566 complaint (&symfile_complaints,
21567 _("location description stack underflow"));
21568 return 0;
21569 }
21570 }
21571 return (stack[stacki]);
21572 }
21573
21574 /* memory allocation interface */
21575
21576 static struct dwarf_block *
21577 dwarf_alloc_block (struct dwarf2_cu *cu)
21578 {
21579 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
21580 }
21581
21582 static struct die_info *
21583 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
21584 {
21585 struct die_info *die;
21586 size_t size = sizeof (struct die_info);
21587
21588 if (num_attrs > 1)
21589 size += (num_attrs - 1) * sizeof (struct attribute);
21590
21591 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
21592 memset (die, 0, sizeof (struct die_info));
21593 return (die);
21594 }
21595
21596 \f
21597 /* Macro support. */
21598
21599 /* Return file name relative to the compilation directory of file number I in
21600 *LH's file name table. The result is allocated using xmalloc; the caller is
21601 responsible for freeing it. */
21602
21603 static char *
21604 file_file_name (int file, struct line_header *lh)
21605 {
21606 /* Is the file number a valid index into the line header's file name
21607 table? Remember that file numbers start with one, not zero. */
21608 if (1 <= file && file <= lh->file_names.size ())
21609 {
21610 const file_entry &fe = lh->file_names[file - 1];
21611
21612 if (!IS_ABSOLUTE_PATH (fe.name))
21613 {
21614 const char *dir = fe.include_dir (lh);
21615 if (dir != NULL)
21616 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
21617 }
21618 return xstrdup (fe.name);
21619 }
21620 else
21621 {
21622 /* The compiler produced a bogus file number. We can at least
21623 record the macro definitions made in the file, even if we
21624 won't be able to find the file by name. */
21625 char fake_name[80];
21626
21627 xsnprintf (fake_name, sizeof (fake_name),
21628 "<bad macro file number %d>", file);
21629
21630 complaint (&symfile_complaints,
21631 _("bad file number in macro information (%d)"),
21632 file);
21633
21634 return xstrdup (fake_name);
21635 }
21636 }
21637
21638 /* Return the full name of file number I in *LH's file name table.
21639 Use COMP_DIR as the name of the current directory of the
21640 compilation. The result is allocated using xmalloc; the caller is
21641 responsible for freeing it. */
21642 static char *
21643 file_full_name (int file, struct line_header *lh, const char *comp_dir)
21644 {
21645 /* Is the file number a valid index into the line header's file name
21646 table? Remember that file numbers start with one, not zero. */
21647 if (1 <= file && file <= lh->file_names.size ())
21648 {
21649 char *relative = file_file_name (file, lh);
21650
21651 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
21652 return relative;
21653 return reconcat (relative, comp_dir, SLASH_STRING,
21654 relative, (char *) NULL);
21655 }
21656 else
21657 return file_file_name (file, lh);
21658 }
21659
21660
21661 static struct macro_source_file *
21662 macro_start_file (int file, int line,
21663 struct macro_source_file *current_file,
21664 struct line_header *lh)
21665 {
21666 /* File name relative to the compilation directory of this source file. */
21667 char *file_name = file_file_name (file, lh);
21668
21669 if (! current_file)
21670 {
21671 /* Note: We don't create a macro table for this compilation unit
21672 at all until we actually get a filename. */
21673 struct macro_table *macro_table = get_macro_table ();
21674
21675 /* If we have no current file, then this must be the start_file
21676 directive for the compilation unit's main source file. */
21677 current_file = macro_set_main (macro_table, file_name);
21678 macro_define_special (macro_table);
21679 }
21680 else
21681 current_file = macro_include (current_file, line, file_name);
21682
21683 xfree (file_name);
21684
21685 return current_file;
21686 }
21687
21688 static const char *
21689 consume_improper_spaces (const char *p, const char *body)
21690 {
21691 if (*p == ' ')
21692 {
21693 complaint (&symfile_complaints,
21694 _("macro definition contains spaces "
21695 "in formal argument list:\n`%s'"),
21696 body);
21697
21698 while (*p == ' ')
21699 p++;
21700 }
21701
21702 return p;
21703 }
21704
21705
21706 static void
21707 parse_macro_definition (struct macro_source_file *file, int line,
21708 const char *body)
21709 {
21710 const char *p;
21711
21712 /* The body string takes one of two forms. For object-like macro
21713 definitions, it should be:
21714
21715 <macro name> " " <definition>
21716
21717 For function-like macro definitions, it should be:
21718
21719 <macro name> "() " <definition>
21720 or
21721 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
21722
21723 Spaces may appear only where explicitly indicated, and in the
21724 <definition>.
21725
21726 The Dwarf 2 spec says that an object-like macro's name is always
21727 followed by a space, but versions of GCC around March 2002 omit
21728 the space when the macro's definition is the empty string.
21729
21730 The Dwarf 2 spec says that there should be no spaces between the
21731 formal arguments in a function-like macro's formal argument list,
21732 but versions of GCC around March 2002 include spaces after the
21733 commas. */
21734
21735
21736 /* Find the extent of the macro name. The macro name is terminated
21737 by either a space or null character (for an object-like macro) or
21738 an opening paren (for a function-like macro). */
21739 for (p = body; *p; p++)
21740 if (*p == ' ' || *p == '(')
21741 break;
21742
21743 if (*p == ' ' || *p == '\0')
21744 {
21745 /* It's an object-like macro. */
21746 int name_len = p - body;
21747 char *name = savestring (body, name_len);
21748 const char *replacement;
21749
21750 if (*p == ' ')
21751 replacement = body + name_len + 1;
21752 else
21753 {
21754 dwarf2_macro_malformed_definition_complaint (body);
21755 replacement = body + name_len;
21756 }
21757
21758 macro_define_object (file, line, name, replacement);
21759
21760 xfree (name);
21761 }
21762 else if (*p == '(')
21763 {
21764 /* It's a function-like macro. */
21765 char *name = savestring (body, p - body);
21766 int argc = 0;
21767 int argv_size = 1;
21768 char **argv = XNEWVEC (char *, argv_size);
21769
21770 p++;
21771
21772 p = consume_improper_spaces (p, body);
21773
21774 /* Parse the formal argument list. */
21775 while (*p && *p != ')')
21776 {
21777 /* Find the extent of the current argument name. */
21778 const char *arg_start = p;
21779
21780 while (*p && *p != ',' && *p != ')' && *p != ' ')
21781 p++;
21782
21783 if (! *p || p == arg_start)
21784 dwarf2_macro_malformed_definition_complaint (body);
21785 else
21786 {
21787 /* Make sure argv has room for the new argument. */
21788 if (argc >= argv_size)
21789 {
21790 argv_size *= 2;
21791 argv = XRESIZEVEC (char *, argv, argv_size);
21792 }
21793
21794 argv[argc++] = savestring (arg_start, p - arg_start);
21795 }
21796
21797 p = consume_improper_spaces (p, body);
21798
21799 /* Consume the comma, if present. */
21800 if (*p == ',')
21801 {
21802 p++;
21803
21804 p = consume_improper_spaces (p, body);
21805 }
21806 }
21807
21808 if (*p == ')')
21809 {
21810 p++;
21811
21812 if (*p == ' ')
21813 /* Perfectly formed definition, no complaints. */
21814 macro_define_function (file, line, name,
21815 argc, (const char **) argv,
21816 p + 1);
21817 else if (*p == '\0')
21818 {
21819 /* Complain, but do define it. */
21820 dwarf2_macro_malformed_definition_complaint (body);
21821 macro_define_function (file, line, name,
21822 argc, (const char **) argv,
21823 p);
21824 }
21825 else
21826 /* Just complain. */
21827 dwarf2_macro_malformed_definition_complaint (body);
21828 }
21829 else
21830 /* Just complain. */
21831 dwarf2_macro_malformed_definition_complaint (body);
21832
21833 xfree (name);
21834 {
21835 int i;
21836
21837 for (i = 0; i < argc; i++)
21838 xfree (argv[i]);
21839 }
21840 xfree (argv);
21841 }
21842 else
21843 dwarf2_macro_malformed_definition_complaint (body);
21844 }
21845
21846 /* Skip some bytes from BYTES according to the form given in FORM.
21847 Returns the new pointer. */
21848
21849 static const gdb_byte *
21850 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
21851 enum dwarf_form form,
21852 unsigned int offset_size,
21853 struct dwarf2_section_info *section)
21854 {
21855 unsigned int bytes_read;
21856
21857 switch (form)
21858 {
21859 case DW_FORM_data1:
21860 case DW_FORM_flag:
21861 ++bytes;
21862 break;
21863
21864 case DW_FORM_data2:
21865 bytes += 2;
21866 break;
21867
21868 case DW_FORM_data4:
21869 bytes += 4;
21870 break;
21871
21872 case DW_FORM_data8:
21873 bytes += 8;
21874 break;
21875
21876 case DW_FORM_data16:
21877 bytes += 16;
21878 break;
21879
21880 case DW_FORM_string:
21881 read_direct_string (abfd, bytes, &bytes_read);
21882 bytes += bytes_read;
21883 break;
21884
21885 case DW_FORM_sec_offset:
21886 case DW_FORM_strp:
21887 case DW_FORM_GNU_strp_alt:
21888 bytes += offset_size;
21889 break;
21890
21891 case DW_FORM_block:
21892 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
21893 bytes += bytes_read;
21894 break;
21895
21896 case DW_FORM_block1:
21897 bytes += 1 + read_1_byte (abfd, bytes);
21898 break;
21899 case DW_FORM_block2:
21900 bytes += 2 + read_2_bytes (abfd, bytes);
21901 break;
21902 case DW_FORM_block4:
21903 bytes += 4 + read_4_bytes (abfd, bytes);
21904 break;
21905
21906 case DW_FORM_sdata:
21907 case DW_FORM_udata:
21908 case DW_FORM_GNU_addr_index:
21909 case DW_FORM_GNU_str_index:
21910 bytes = gdb_skip_leb128 (bytes, buffer_end);
21911 if (bytes == NULL)
21912 {
21913 dwarf2_section_buffer_overflow_complaint (section);
21914 return NULL;
21915 }
21916 break;
21917
21918 case DW_FORM_implicit_const:
21919 break;
21920
21921 default:
21922 {
21923 complain:
21924 complaint (&symfile_complaints,
21925 _("invalid form 0x%x in `%s'"),
21926 form, get_section_name (section));
21927 return NULL;
21928 }
21929 }
21930
21931 return bytes;
21932 }
21933
21934 /* A helper for dwarf_decode_macros that handles skipping an unknown
21935 opcode. Returns an updated pointer to the macro data buffer; or,
21936 on error, issues a complaint and returns NULL. */
21937
21938 static const gdb_byte *
21939 skip_unknown_opcode (unsigned int opcode,
21940 const gdb_byte **opcode_definitions,
21941 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
21942 bfd *abfd,
21943 unsigned int offset_size,
21944 struct dwarf2_section_info *section)
21945 {
21946 unsigned int bytes_read, i;
21947 unsigned long arg;
21948 const gdb_byte *defn;
21949
21950 if (opcode_definitions[opcode] == NULL)
21951 {
21952 complaint (&symfile_complaints,
21953 _("unrecognized DW_MACFINO opcode 0x%x"),
21954 opcode);
21955 return NULL;
21956 }
21957
21958 defn = opcode_definitions[opcode];
21959 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
21960 defn += bytes_read;
21961
21962 for (i = 0; i < arg; ++i)
21963 {
21964 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
21965 (enum dwarf_form) defn[i], offset_size,
21966 section);
21967 if (mac_ptr == NULL)
21968 {
21969 /* skip_form_bytes already issued the complaint. */
21970 return NULL;
21971 }
21972 }
21973
21974 return mac_ptr;
21975 }
21976
21977 /* A helper function which parses the header of a macro section.
21978 If the macro section is the extended (for now called "GNU") type,
21979 then this updates *OFFSET_SIZE. Returns a pointer to just after
21980 the header, or issues a complaint and returns NULL on error. */
21981
21982 static const gdb_byte *
21983 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
21984 bfd *abfd,
21985 const gdb_byte *mac_ptr,
21986 unsigned int *offset_size,
21987 int section_is_gnu)
21988 {
21989 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
21990
21991 if (section_is_gnu)
21992 {
21993 unsigned int version, flags;
21994
21995 version = read_2_bytes (abfd, mac_ptr);
21996 if (version != 4 && version != 5)
21997 {
21998 complaint (&symfile_complaints,
21999 _("unrecognized version `%d' in .debug_macro section"),
22000 version);
22001 return NULL;
22002 }
22003 mac_ptr += 2;
22004
22005 flags = read_1_byte (abfd, mac_ptr);
22006 ++mac_ptr;
22007 *offset_size = (flags & 1) ? 8 : 4;
22008
22009 if ((flags & 2) != 0)
22010 /* We don't need the line table offset. */
22011 mac_ptr += *offset_size;
22012
22013 /* Vendor opcode descriptions. */
22014 if ((flags & 4) != 0)
22015 {
22016 unsigned int i, count;
22017
22018 count = read_1_byte (abfd, mac_ptr);
22019 ++mac_ptr;
22020 for (i = 0; i < count; ++i)
22021 {
22022 unsigned int opcode, bytes_read;
22023 unsigned long arg;
22024
22025 opcode = read_1_byte (abfd, mac_ptr);
22026 ++mac_ptr;
22027 opcode_definitions[opcode] = mac_ptr;
22028 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22029 mac_ptr += bytes_read;
22030 mac_ptr += arg;
22031 }
22032 }
22033 }
22034
22035 return mac_ptr;
22036 }
22037
22038 /* A helper for dwarf_decode_macros that handles the GNU extensions,
22039 including DW_MACRO_import. */
22040
22041 static void
22042 dwarf_decode_macro_bytes (bfd *abfd,
22043 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
22044 struct macro_source_file *current_file,
22045 struct line_header *lh,
22046 struct dwarf2_section_info *section,
22047 int section_is_gnu, int section_is_dwz,
22048 unsigned int offset_size,
22049 htab_t include_hash)
22050 {
22051 struct objfile *objfile = dwarf2_per_objfile->objfile;
22052 enum dwarf_macro_record_type macinfo_type;
22053 int at_commandline;
22054 const gdb_byte *opcode_definitions[256];
22055
22056 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
22057 &offset_size, section_is_gnu);
22058 if (mac_ptr == NULL)
22059 {
22060 /* We already issued a complaint. */
22061 return;
22062 }
22063
22064 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
22065 GDB is still reading the definitions from command line. First
22066 DW_MACINFO_start_file will need to be ignored as it was already executed
22067 to create CURRENT_FILE for the main source holding also the command line
22068 definitions. On first met DW_MACINFO_start_file this flag is reset to
22069 normally execute all the remaining DW_MACINFO_start_file macinfos. */
22070
22071 at_commandline = 1;
22072
22073 do
22074 {
22075 /* Do we at least have room for a macinfo type byte? */
22076 if (mac_ptr >= mac_end)
22077 {
22078 dwarf2_section_buffer_overflow_complaint (section);
22079 break;
22080 }
22081
22082 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
22083 mac_ptr++;
22084
22085 /* Note that we rely on the fact that the corresponding GNU and
22086 DWARF constants are the same. */
22087 switch (macinfo_type)
22088 {
22089 /* A zero macinfo type indicates the end of the macro
22090 information. */
22091 case 0:
22092 break;
22093
22094 case DW_MACRO_define:
22095 case DW_MACRO_undef:
22096 case DW_MACRO_define_strp:
22097 case DW_MACRO_undef_strp:
22098 case DW_MACRO_define_sup:
22099 case DW_MACRO_undef_sup:
22100 {
22101 unsigned int bytes_read;
22102 int line;
22103 const char *body;
22104 int is_define;
22105
22106 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22107 mac_ptr += bytes_read;
22108
22109 if (macinfo_type == DW_MACRO_define
22110 || macinfo_type == DW_MACRO_undef)
22111 {
22112 body = read_direct_string (abfd, mac_ptr, &bytes_read);
22113 mac_ptr += bytes_read;
22114 }
22115 else
22116 {
22117 LONGEST str_offset;
22118
22119 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
22120 mac_ptr += offset_size;
22121
22122 if (macinfo_type == DW_MACRO_define_sup
22123 || macinfo_type == DW_MACRO_undef_sup
22124 || section_is_dwz)
22125 {
22126 struct dwz_file *dwz = dwarf2_get_dwz_file ();
22127
22128 body = read_indirect_string_from_dwz (dwz, str_offset);
22129 }
22130 else
22131 body = read_indirect_string_at_offset (abfd, str_offset);
22132 }
22133
22134 is_define = (macinfo_type == DW_MACRO_define
22135 || macinfo_type == DW_MACRO_define_strp
22136 || macinfo_type == DW_MACRO_define_sup);
22137 if (! current_file)
22138 {
22139 /* DWARF violation as no main source is present. */
22140 complaint (&symfile_complaints,
22141 _("debug info with no main source gives macro %s "
22142 "on line %d: %s"),
22143 is_define ? _("definition") : _("undefinition"),
22144 line, body);
22145 break;
22146 }
22147 if ((line == 0 && !at_commandline)
22148 || (line != 0 && at_commandline))
22149 complaint (&symfile_complaints,
22150 _("debug info gives %s macro %s with %s line %d: %s"),
22151 at_commandline ? _("command-line") : _("in-file"),
22152 is_define ? _("definition") : _("undefinition"),
22153 line == 0 ? _("zero") : _("non-zero"), line, body);
22154
22155 if (is_define)
22156 parse_macro_definition (current_file, line, body);
22157 else
22158 {
22159 gdb_assert (macinfo_type == DW_MACRO_undef
22160 || macinfo_type == DW_MACRO_undef_strp
22161 || macinfo_type == DW_MACRO_undef_sup);
22162 macro_undef (current_file, line, body);
22163 }
22164 }
22165 break;
22166
22167 case DW_MACRO_start_file:
22168 {
22169 unsigned int bytes_read;
22170 int line, file;
22171
22172 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22173 mac_ptr += bytes_read;
22174 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22175 mac_ptr += bytes_read;
22176
22177 if ((line == 0 && !at_commandline)
22178 || (line != 0 && at_commandline))
22179 complaint (&symfile_complaints,
22180 _("debug info gives source %d included "
22181 "from %s at %s line %d"),
22182 file, at_commandline ? _("command-line") : _("file"),
22183 line == 0 ? _("zero") : _("non-zero"), line);
22184
22185 if (at_commandline)
22186 {
22187 /* This DW_MACRO_start_file was executed in the
22188 pass one. */
22189 at_commandline = 0;
22190 }
22191 else
22192 current_file = macro_start_file (file, line, current_file, lh);
22193 }
22194 break;
22195
22196 case DW_MACRO_end_file:
22197 if (! current_file)
22198 complaint (&symfile_complaints,
22199 _("macro debug info has an unmatched "
22200 "`close_file' directive"));
22201 else
22202 {
22203 current_file = current_file->included_by;
22204 if (! current_file)
22205 {
22206 enum dwarf_macro_record_type next_type;
22207
22208 /* GCC circa March 2002 doesn't produce the zero
22209 type byte marking the end of the compilation
22210 unit. Complain if it's not there, but exit no
22211 matter what. */
22212
22213 /* Do we at least have room for a macinfo type byte? */
22214 if (mac_ptr >= mac_end)
22215 {
22216 dwarf2_section_buffer_overflow_complaint (section);
22217 return;
22218 }
22219
22220 /* We don't increment mac_ptr here, so this is just
22221 a look-ahead. */
22222 next_type
22223 = (enum dwarf_macro_record_type) read_1_byte (abfd,
22224 mac_ptr);
22225 if (next_type != 0)
22226 complaint (&symfile_complaints,
22227 _("no terminating 0-type entry for "
22228 "macros in `.debug_macinfo' section"));
22229
22230 return;
22231 }
22232 }
22233 break;
22234
22235 case DW_MACRO_import:
22236 case DW_MACRO_import_sup:
22237 {
22238 LONGEST offset;
22239 void **slot;
22240 bfd *include_bfd = abfd;
22241 struct dwarf2_section_info *include_section = section;
22242 const gdb_byte *include_mac_end = mac_end;
22243 int is_dwz = section_is_dwz;
22244 const gdb_byte *new_mac_ptr;
22245
22246 offset = read_offset_1 (abfd, mac_ptr, offset_size);
22247 mac_ptr += offset_size;
22248
22249 if (macinfo_type == DW_MACRO_import_sup)
22250 {
22251 struct dwz_file *dwz = dwarf2_get_dwz_file ();
22252
22253 dwarf2_read_section (objfile, &dwz->macro);
22254
22255 include_section = &dwz->macro;
22256 include_bfd = get_section_bfd_owner (include_section);
22257 include_mac_end = dwz->macro.buffer + dwz->macro.size;
22258 is_dwz = 1;
22259 }
22260
22261 new_mac_ptr = include_section->buffer + offset;
22262 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
22263
22264 if (*slot != NULL)
22265 {
22266 /* This has actually happened; see
22267 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
22268 complaint (&symfile_complaints,
22269 _("recursive DW_MACRO_import in "
22270 ".debug_macro section"));
22271 }
22272 else
22273 {
22274 *slot = (void *) new_mac_ptr;
22275
22276 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
22277 include_mac_end, current_file, lh,
22278 section, section_is_gnu, is_dwz,
22279 offset_size, include_hash);
22280
22281 htab_remove_elt (include_hash, (void *) new_mac_ptr);
22282 }
22283 }
22284 break;
22285
22286 case DW_MACINFO_vendor_ext:
22287 if (!section_is_gnu)
22288 {
22289 unsigned int bytes_read;
22290
22291 /* This reads the constant, but since we don't recognize
22292 any vendor extensions, we ignore it. */
22293 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22294 mac_ptr += bytes_read;
22295 read_direct_string (abfd, mac_ptr, &bytes_read);
22296 mac_ptr += bytes_read;
22297
22298 /* We don't recognize any vendor extensions. */
22299 break;
22300 }
22301 /* FALLTHROUGH */
22302
22303 default:
22304 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
22305 mac_ptr, mac_end, abfd, offset_size,
22306 section);
22307 if (mac_ptr == NULL)
22308 return;
22309 break;
22310 }
22311 } while (macinfo_type != 0);
22312 }
22313
22314 static void
22315 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
22316 int section_is_gnu)
22317 {
22318 struct objfile *objfile = dwarf2_per_objfile->objfile;
22319 struct line_header *lh = cu->line_header;
22320 bfd *abfd;
22321 const gdb_byte *mac_ptr, *mac_end;
22322 struct macro_source_file *current_file = 0;
22323 enum dwarf_macro_record_type macinfo_type;
22324 unsigned int offset_size = cu->header.offset_size;
22325 const gdb_byte *opcode_definitions[256];
22326 struct cleanup *cleanup;
22327 void **slot;
22328 struct dwarf2_section_info *section;
22329 const char *section_name;
22330
22331 if (cu->dwo_unit != NULL)
22332 {
22333 if (section_is_gnu)
22334 {
22335 section = &cu->dwo_unit->dwo_file->sections.macro;
22336 section_name = ".debug_macro.dwo";
22337 }
22338 else
22339 {
22340 section = &cu->dwo_unit->dwo_file->sections.macinfo;
22341 section_name = ".debug_macinfo.dwo";
22342 }
22343 }
22344 else
22345 {
22346 if (section_is_gnu)
22347 {
22348 section = &dwarf2_per_objfile->macro;
22349 section_name = ".debug_macro";
22350 }
22351 else
22352 {
22353 section = &dwarf2_per_objfile->macinfo;
22354 section_name = ".debug_macinfo";
22355 }
22356 }
22357
22358 dwarf2_read_section (objfile, section);
22359 if (section->buffer == NULL)
22360 {
22361 complaint (&symfile_complaints, _("missing %s section"), section_name);
22362 return;
22363 }
22364 abfd = get_section_bfd_owner (section);
22365
22366 /* First pass: Find the name of the base filename.
22367 This filename is needed in order to process all macros whose definition
22368 (or undefinition) comes from the command line. These macros are defined
22369 before the first DW_MACINFO_start_file entry, and yet still need to be
22370 associated to the base file.
22371
22372 To determine the base file name, we scan the macro definitions until we
22373 reach the first DW_MACINFO_start_file entry. We then initialize
22374 CURRENT_FILE accordingly so that any macro definition found before the
22375 first DW_MACINFO_start_file can still be associated to the base file. */
22376
22377 mac_ptr = section->buffer + offset;
22378 mac_end = section->buffer + section->size;
22379
22380 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
22381 &offset_size, section_is_gnu);
22382 if (mac_ptr == NULL)
22383 {
22384 /* We already issued a complaint. */
22385 return;
22386 }
22387
22388 do
22389 {
22390 /* Do we at least have room for a macinfo type byte? */
22391 if (mac_ptr >= mac_end)
22392 {
22393 /* Complaint is printed during the second pass as GDB will probably
22394 stop the first pass earlier upon finding
22395 DW_MACINFO_start_file. */
22396 break;
22397 }
22398
22399 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
22400 mac_ptr++;
22401
22402 /* Note that we rely on the fact that the corresponding GNU and
22403 DWARF constants are the same. */
22404 switch (macinfo_type)
22405 {
22406 /* A zero macinfo type indicates the end of the macro
22407 information. */
22408 case 0:
22409 break;
22410
22411 case DW_MACRO_define:
22412 case DW_MACRO_undef:
22413 /* Only skip the data by MAC_PTR. */
22414 {
22415 unsigned int bytes_read;
22416
22417 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22418 mac_ptr += bytes_read;
22419 read_direct_string (abfd, mac_ptr, &bytes_read);
22420 mac_ptr += bytes_read;
22421 }
22422 break;
22423
22424 case DW_MACRO_start_file:
22425 {
22426 unsigned int bytes_read;
22427 int line, file;
22428
22429 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22430 mac_ptr += bytes_read;
22431 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22432 mac_ptr += bytes_read;
22433
22434 current_file = macro_start_file (file, line, current_file, lh);
22435 }
22436 break;
22437
22438 case DW_MACRO_end_file:
22439 /* No data to skip by MAC_PTR. */
22440 break;
22441
22442 case DW_MACRO_define_strp:
22443 case DW_MACRO_undef_strp:
22444 case DW_MACRO_define_sup:
22445 case DW_MACRO_undef_sup:
22446 {
22447 unsigned int bytes_read;
22448
22449 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22450 mac_ptr += bytes_read;
22451 mac_ptr += offset_size;
22452 }
22453 break;
22454
22455 case DW_MACRO_import:
22456 case DW_MACRO_import_sup:
22457 /* Note that, according to the spec, a transparent include
22458 chain cannot call DW_MACRO_start_file. So, we can just
22459 skip this opcode. */
22460 mac_ptr += offset_size;
22461 break;
22462
22463 case DW_MACINFO_vendor_ext:
22464 /* Only skip the data by MAC_PTR. */
22465 if (!section_is_gnu)
22466 {
22467 unsigned int bytes_read;
22468
22469 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22470 mac_ptr += bytes_read;
22471 read_direct_string (abfd, mac_ptr, &bytes_read);
22472 mac_ptr += bytes_read;
22473 }
22474 /* FALLTHROUGH */
22475
22476 default:
22477 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
22478 mac_ptr, mac_end, abfd, offset_size,
22479 section);
22480 if (mac_ptr == NULL)
22481 return;
22482 break;
22483 }
22484 } while (macinfo_type != 0 && current_file == NULL);
22485
22486 /* Second pass: Process all entries.
22487
22488 Use the AT_COMMAND_LINE flag to determine whether we are still processing
22489 command-line macro definitions/undefinitions. This flag is unset when we
22490 reach the first DW_MACINFO_start_file entry. */
22491
22492 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
22493 htab_eq_pointer,
22494 NULL, xcalloc, xfree));
22495 mac_ptr = section->buffer + offset;
22496 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
22497 *slot = (void *) mac_ptr;
22498 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
22499 current_file, lh, section,
22500 section_is_gnu, 0, offset_size,
22501 include_hash.get ());
22502 }
22503
22504 /* Check if the attribute's form is a DW_FORM_block*
22505 if so return true else false. */
22506
22507 static int
22508 attr_form_is_block (const struct attribute *attr)
22509 {
22510 return (attr == NULL ? 0 :
22511 attr->form == DW_FORM_block1
22512 || attr->form == DW_FORM_block2
22513 || attr->form == DW_FORM_block4
22514 || attr->form == DW_FORM_block
22515 || attr->form == DW_FORM_exprloc);
22516 }
22517
22518 /* Return non-zero if ATTR's value is a section offset --- classes
22519 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
22520 You may use DW_UNSND (attr) to retrieve such offsets.
22521
22522 Section 7.5.4, "Attribute Encodings", explains that no attribute
22523 may have a value that belongs to more than one of these classes; it
22524 would be ambiguous if we did, because we use the same forms for all
22525 of them. */
22526
22527 static int
22528 attr_form_is_section_offset (const struct attribute *attr)
22529 {
22530 return (attr->form == DW_FORM_data4
22531 || attr->form == DW_FORM_data8
22532 || attr->form == DW_FORM_sec_offset);
22533 }
22534
22535 /* Return non-zero if ATTR's value falls in the 'constant' class, or
22536 zero otherwise. When this function returns true, you can apply
22537 dwarf2_get_attr_constant_value to it.
22538
22539 However, note that for some attributes you must check
22540 attr_form_is_section_offset before using this test. DW_FORM_data4
22541 and DW_FORM_data8 are members of both the constant class, and of
22542 the classes that contain offsets into other debug sections
22543 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
22544 that, if an attribute's can be either a constant or one of the
22545 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
22546 taken as section offsets, not constants.
22547
22548 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
22549 cannot handle that. */
22550
22551 static int
22552 attr_form_is_constant (const struct attribute *attr)
22553 {
22554 switch (attr->form)
22555 {
22556 case DW_FORM_sdata:
22557 case DW_FORM_udata:
22558 case DW_FORM_data1:
22559 case DW_FORM_data2:
22560 case DW_FORM_data4:
22561 case DW_FORM_data8:
22562 case DW_FORM_implicit_const:
22563 return 1;
22564 default:
22565 return 0;
22566 }
22567 }
22568
22569
22570 /* DW_ADDR is always stored already as sect_offset; despite for the forms
22571 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
22572
22573 static int
22574 attr_form_is_ref (const struct attribute *attr)
22575 {
22576 switch (attr->form)
22577 {
22578 case DW_FORM_ref_addr:
22579 case DW_FORM_ref1:
22580 case DW_FORM_ref2:
22581 case DW_FORM_ref4:
22582 case DW_FORM_ref8:
22583 case DW_FORM_ref_udata:
22584 case DW_FORM_GNU_ref_alt:
22585 return 1;
22586 default:
22587 return 0;
22588 }
22589 }
22590
22591 /* Return the .debug_loc section to use for CU.
22592 For DWO files use .debug_loc.dwo. */
22593
22594 static struct dwarf2_section_info *
22595 cu_debug_loc_section (struct dwarf2_cu *cu)
22596 {
22597 if (cu->dwo_unit)
22598 {
22599 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
22600
22601 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
22602 }
22603 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
22604 : &dwarf2_per_objfile->loc);
22605 }
22606
22607 /* A helper function that fills in a dwarf2_loclist_baton. */
22608
22609 static void
22610 fill_in_loclist_baton (struct dwarf2_cu *cu,
22611 struct dwarf2_loclist_baton *baton,
22612 const struct attribute *attr)
22613 {
22614 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22615
22616 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
22617
22618 baton->per_cu = cu->per_cu;
22619 gdb_assert (baton->per_cu);
22620 /* We don't know how long the location list is, but make sure we
22621 don't run off the edge of the section. */
22622 baton->size = section->size - DW_UNSND (attr);
22623 baton->data = section->buffer + DW_UNSND (attr);
22624 baton->base_address = cu->base_address;
22625 baton->from_dwo = cu->dwo_unit != NULL;
22626 }
22627
22628 static void
22629 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
22630 struct dwarf2_cu *cu, int is_block)
22631 {
22632 struct objfile *objfile = dwarf2_per_objfile->objfile;
22633 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22634
22635 if (attr_form_is_section_offset (attr)
22636 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
22637 the section. If so, fall through to the complaint in the
22638 other branch. */
22639 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
22640 {
22641 struct dwarf2_loclist_baton *baton;
22642
22643 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
22644
22645 fill_in_loclist_baton (cu, baton, attr);
22646
22647 if (cu->base_known == 0)
22648 complaint (&symfile_complaints,
22649 _("Location list used without "
22650 "specifying the CU base address."));
22651
22652 SYMBOL_ACLASS_INDEX (sym) = (is_block
22653 ? dwarf2_loclist_block_index
22654 : dwarf2_loclist_index);
22655 SYMBOL_LOCATION_BATON (sym) = baton;
22656 }
22657 else
22658 {
22659 struct dwarf2_locexpr_baton *baton;
22660
22661 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
22662 baton->per_cu = cu->per_cu;
22663 gdb_assert (baton->per_cu);
22664
22665 if (attr_form_is_block (attr))
22666 {
22667 /* Note that we're just copying the block's data pointer
22668 here, not the actual data. We're still pointing into the
22669 info_buffer for SYM's objfile; right now we never release
22670 that buffer, but when we do clean up properly this may
22671 need to change. */
22672 baton->size = DW_BLOCK (attr)->size;
22673 baton->data = DW_BLOCK (attr)->data;
22674 }
22675 else
22676 {
22677 dwarf2_invalid_attrib_class_complaint ("location description",
22678 SYMBOL_NATURAL_NAME (sym));
22679 baton->size = 0;
22680 }
22681
22682 SYMBOL_ACLASS_INDEX (sym) = (is_block
22683 ? dwarf2_locexpr_block_index
22684 : dwarf2_locexpr_index);
22685 SYMBOL_LOCATION_BATON (sym) = baton;
22686 }
22687 }
22688
22689 /* Return the OBJFILE associated with the compilation unit CU. If CU
22690 came from a separate debuginfo file, then the master objfile is
22691 returned. */
22692
22693 struct objfile *
22694 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
22695 {
22696 struct objfile *objfile = per_cu->objfile;
22697
22698 /* Return the master objfile, so that we can report and look up the
22699 correct file containing this variable. */
22700 if (objfile->separate_debug_objfile_backlink)
22701 objfile = objfile->separate_debug_objfile_backlink;
22702
22703 return objfile;
22704 }
22705
22706 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
22707 (CU_HEADERP is unused in such case) or prepare a temporary copy at
22708 CU_HEADERP first. */
22709
22710 static const struct comp_unit_head *
22711 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
22712 struct dwarf2_per_cu_data *per_cu)
22713 {
22714 const gdb_byte *info_ptr;
22715
22716 if (per_cu->cu)
22717 return &per_cu->cu->header;
22718
22719 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
22720
22721 memset (cu_headerp, 0, sizeof (*cu_headerp));
22722 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
22723 rcuh_kind::COMPILE);
22724
22725 return cu_headerp;
22726 }
22727
22728 /* Return the address size given in the compilation unit header for CU. */
22729
22730 int
22731 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
22732 {
22733 struct comp_unit_head cu_header_local;
22734 const struct comp_unit_head *cu_headerp;
22735
22736 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22737
22738 return cu_headerp->addr_size;
22739 }
22740
22741 /* Return the offset size given in the compilation unit header for CU. */
22742
22743 int
22744 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
22745 {
22746 struct comp_unit_head cu_header_local;
22747 const struct comp_unit_head *cu_headerp;
22748
22749 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22750
22751 return cu_headerp->offset_size;
22752 }
22753
22754 /* See its dwarf2loc.h declaration. */
22755
22756 int
22757 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
22758 {
22759 struct comp_unit_head cu_header_local;
22760 const struct comp_unit_head *cu_headerp;
22761
22762 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22763
22764 if (cu_headerp->version == 2)
22765 return cu_headerp->addr_size;
22766 else
22767 return cu_headerp->offset_size;
22768 }
22769
22770 /* Return the text offset of the CU. The returned offset comes from
22771 this CU's objfile. If this objfile came from a separate debuginfo
22772 file, then the offset may be different from the corresponding
22773 offset in the parent objfile. */
22774
22775 CORE_ADDR
22776 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
22777 {
22778 struct objfile *objfile = per_cu->objfile;
22779
22780 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
22781 }
22782
22783 /* Return DWARF version number of PER_CU. */
22784
22785 short
22786 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
22787 {
22788 return per_cu->dwarf_version;
22789 }
22790
22791 /* Locate the .debug_info compilation unit from CU's objfile which contains
22792 the DIE at OFFSET. Raises an error on failure. */
22793
22794 static struct dwarf2_per_cu_data *
22795 dwarf2_find_containing_comp_unit (sect_offset sect_off,
22796 unsigned int offset_in_dwz,
22797 struct objfile *objfile)
22798 {
22799 struct dwarf2_per_cu_data *this_cu;
22800 int low, high;
22801 const sect_offset *cu_off;
22802
22803 low = 0;
22804 high = dwarf2_per_objfile->n_comp_units - 1;
22805 while (high > low)
22806 {
22807 struct dwarf2_per_cu_data *mid_cu;
22808 int mid = low + (high - low) / 2;
22809
22810 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
22811 cu_off = &mid_cu->sect_off;
22812 if (mid_cu->is_dwz > offset_in_dwz
22813 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
22814 high = mid;
22815 else
22816 low = mid + 1;
22817 }
22818 gdb_assert (low == high);
22819 this_cu = dwarf2_per_objfile->all_comp_units[low];
22820 cu_off = &this_cu->sect_off;
22821 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
22822 {
22823 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
22824 error (_("Dwarf Error: could not find partial DIE containing "
22825 "offset 0x%x [in module %s]"),
22826 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
22827
22828 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
22829 <= sect_off);
22830 return dwarf2_per_objfile->all_comp_units[low-1];
22831 }
22832 else
22833 {
22834 this_cu = dwarf2_per_objfile->all_comp_units[low];
22835 if (low == dwarf2_per_objfile->n_comp_units - 1
22836 && sect_off >= this_cu->sect_off + this_cu->length)
22837 error (_("invalid dwarf2 offset %u"), to_underlying (sect_off));
22838 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
22839 return this_cu;
22840 }
22841 }
22842
22843 /* Initialize dwarf2_cu CU, owned by PER_CU. */
22844
22845 static void
22846 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
22847 {
22848 memset (cu, 0, sizeof (*cu));
22849 per_cu->cu = cu;
22850 cu->per_cu = per_cu;
22851 cu->objfile = per_cu->objfile;
22852 obstack_init (&cu->comp_unit_obstack);
22853 }
22854
22855 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
22856
22857 static void
22858 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
22859 enum language pretend_language)
22860 {
22861 struct attribute *attr;
22862
22863 /* Set the language we're debugging. */
22864 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
22865 if (attr)
22866 set_cu_language (DW_UNSND (attr), cu);
22867 else
22868 {
22869 cu->language = pretend_language;
22870 cu->language_defn = language_def (cu->language);
22871 }
22872
22873 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
22874 }
22875
22876 /* Release one cached compilation unit, CU. We unlink it from the tree
22877 of compilation units, but we don't remove it from the read_in_chain;
22878 the caller is responsible for that.
22879 NOTE: DATA is a void * because this function is also used as a
22880 cleanup routine. */
22881
22882 static void
22883 free_heap_comp_unit (void *data)
22884 {
22885 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
22886
22887 gdb_assert (cu->per_cu != NULL);
22888 cu->per_cu->cu = NULL;
22889 cu->per_cu = NULL;
22890
22891 obstack_free (&cu->comp_unit_obstack, NULL);
22892
22893 xfree (cu);
22894 }
22895
22896 /* This cleanup function is passed the address of a dwarf2_cu on the stack
22897 when we're finished with it. We can't free the pointer itself, but be
22898 sure to unlink it from the cache. Also release any associated storage. */
22899
22900 static void
22901 free_stack_comp_unit (void *data)
22902 {
22903 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
22904
22905 gdb_assert (cu->per_cu != NULL);
22906 cu->per_cu->cu = NULL;
22907 cu->per_cu = NULL;
22908
22909 obstack_free (&cu->comp_unit_obstack, NULL);
22910 cu->partial_dies = NULL;
22911 }
22912
22913 /* Free all cached compilation units. */
22914
22915 static void
22916 free_cached_comp_units (void *data)
22917 {
22918 dwarf2_per_objfile->free_cached_comp_units ();
22919 }
22920
22921 /* Increase the age counter on each cached compilation unit, and free
22922 any that are too old. */
22923
22924 static void
22925 age_cached_comp_units (void)
22926 {
22927 struct dwarf2_per_cu_data *per_cu, **last_chain;
22928
22929 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
22930 per_cu = dwarf2_per_objfile->read_in_chain;
22931 while (per_cu != NULL)
22932 {
22933 per_cu->cu->last_used ++;
22934 if (per_cu->cu->last_used <= dwarf_max_cache_age)
22935 dwarf2_mark (per_cu->cu);
22936 per_cu = per_cu->cu->read_in_chain;
22937 }
22938
22939 per_cu = dwarf2_per_objfile->read_in_chain;
22940 last_chain = &dwarf2_per_objfile->read_in_chain;
22941 while (per_cu != NULL)
22942 {
22943 struct dwarf2_per_cu_data *next_cu;
22944
22945 next_cu = per_cu->cu->read_in_chain;
22946
22947 if (!per_cu->cu->mark)
22948 {
22949 free_heap_comp_unit (per_cu->cu);
22950 *last_chain = next_cu;
22951 }
22952 else
22953 last_chain = &per_cu->cu->read_in_chain;
22954
22955 per_cu = next_cu;
22956 }
22957 }
22958
22959 /* Remove a single compilation unit from the cache. */
22960
22961 static void
22962 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
22963 {
22964 struct dwarf2_per_cu_data *per_cu, **last_chain;
22965
22966 per_cu = dwarf2_per_objfile->read_in_chain;
22967 last_chain = &dwarf2_per_objfile->read_in_chain;
22968 while (per_cu != NULL)
22969 {
22970 struct dwarf2_per_cu_data *next_cu;
22971
22972 next_cu = per_cu->cu->read_in_chain;
22973
22974 if (per_cu == target_per_cu)
22975 {
22976 free_heap_comp_unit (per_cu->cu);
22977 per_cu->cu = NULL;
22978 *last_chain = next_cu;
22979 break;
22980 }
22981 else
22982 last_chain = &per_cu->cu->read_in_chain;
22983
22984 per_cu = next_cu;
22985 }
22986 }
22987
22988 /* Release all extra memory associated with OBJFILE. */
22989
22990 void
22991 dwarf2_free_objfile (struct objfile *objfile)
22992 {
22993 dwarf2_per_objfile
22994 = (struct dwarf2_per_objfile *) objfile_data (objfile,
22995 dwarf2_objfile_data_key);
22996
22997 if (dwarf2_per_objfile == NULL)
22998 return;
22999
23000 dwarf2_per_objfile->~dwarf2_per_objfile ();
23001 }
23002
23003 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23004 We store these in a hash table separate from the DIEs, and preserve them
23005 when the DIEs are flushed out of cache.
23006
23007 The CU "per_cu" pointer is needed because offset alone is not enough to
23008 uniquely identify the type. A file may have multiple .debug_types sections,
23009 or the type may come from a DWO file. Furthermore, while it's more logical
23010 to use per_cu->section+offset, with Fission the section with the data is in
23011 the DWO file but we don't know that section at the point we need it.
23012 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23013 because we can enter the lookup routine, get_die_type_at_offset, from
23014 outside this file, and thus won't necessarily have PER_CU->cu.
23015 Fortunately, PER_CU is stable for the life of the objfile. */
23016
23017 struct dwarf2_per_cu_offset_and_type
23018 {
23019 const struct dwarf2_per_cu_data *per_cu;
23020 sect_offset sect_off;
23021 struct type *type;
23022 };
23023
23024 /* Hash function for a dwarf2_per_cu_offset_and_type. */
23025
23026 static hashval_t
23027 per_cu_offset_and_type_hash (const void *item)
23028 {
23029 const struct dwarf2_per_cu_offset_and_type *ofs
23030 = (const struct dwarf2_per_cu_offset_and_type *) item;
23031
23032 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
23033 }
23034
23035 /* Equality function for a dwarf2_per_cu_offset_and_type. */
23036
23037 static int
23038 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
23039 {
23040 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23041 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23042 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23043 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
23044
23045 return (ofs_lhs->per_cu == ofs_rhs->per_cu
23046 && ofs_lhs->sect_off == ofs_rhs->sect_off);
23047 }
23048
23049 /* Set the type associated with DIE to TYPE. Save it in CU's hash
23050 table if necessary. For convenience, return TYPE.
23051
23052 The DIEs reading must have careful ordering to:
23053 * Not cause infite loops trying to read in DIEs as a prerequisite for
23054 reading current DIE.
23055 * Not trying to dereference contents of still incompletely read in types
23056 while reading in other DIEs.
23057 * Enable referencing still incompletely read in types just by a pointer to
23058 the type without accessing its fields.
23059
23060 Therefore caller should follow these rules:
23061 * Try to fetch any prerequisite types we may need to build this DIE type
23062 before building the type and calling set_die_type.
23063 * After building type call set_die_type for current DIE as soon as
23064 possible before fetching more types to complete the current type.
23065 * Make the type as complete as possible before fetching more types. */
23066
23067 static struct type *
23068 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23069 {
23070 struct dwarf2_per_cu_offset_and_type **slot, ofs;
23071 struct objfile *objfile = cu->objfile;
23072 struct attribute *attr;
23073 struct dynamic_prop prop;
23074
23075 /* For Ada types, make sure that the gnat-specific data is always
23076 initialized (if not already set). There are a few types where
23077 we should not be doing so, because the type-specific area is
23078 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23079 where the type-specific area is used to store the floatformat).
23080 But this is not a problem, because the gnat-specific information
23081 is actually not needed for these types. */
23082 if (need_gnat_info (cu)
23083 && TYPE_CODE (type) != TYPE_CODE_FUNC
23084 && TYPE_CODE (type) != TYPE_CODE_FLT
23085 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
23086 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
23087 && TYPE_CODE (type) != TYPE_CODE_METHOD
23088 && !HAVE_GNAT_AUX_INFO (type))
23089 INIT_GNAT_SPECIFIC (type);
23090
23091 /* Read DW_AT_allocated and set in type. */
23092 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23093 if (attr_form_is_block (attr))
23094 {
23095 if (attr_to_dynamic_prop (attr, die, cu, &prop))
23096 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type, objfile);
23097 }
23098 else if (attr != NULL)
23099 {
23100 complaint (&symfile_complaints,
23101 _("DW_AT_allocated has the wrong form (%s) at DIE 0x%x"),
23102 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23103 to_underlying (die->sect_off));
23104 }
23105
23106 /* Read DW_AT_associated and set in type. */
23107 attr = dwarf2_attr (die, DW_AT_associated, cu);
23108 if (attr_form_is_block (attr))
23109 {
23110 if (attr_to_dynamic_prop (attr, die, cu, &prop))
23111 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type, objfile);
23112 }
23113 else if (attr != NULL)
23114 {
23115 complaint (&symfile_complaints,
23116 _("DW_AT_associated has the wrong form (%s) at DIE 0x%x"),
23117 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23118 to_underlying (die->sect_off));
23119 }
23120
23121 /* Read DW_AT_data_location and set in type. */
23122 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23123 if (attr_to_dynamic_prop (attr, die, cu, &prop))
23124 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type, objfile);
23125
23126 if (dwarf2_per_objfile->die_type_hash == NULL)
23127 {
23128 dwarf2_per_objfile->die_type_hash =
23129 htab_create_alloc_ex (127,
23130 per_cu_offset_and_type_hash,
23131 per_cu_offset_and_type_eq,
23132 NULL,
23133 &objfile->objfile_obstack,
23134 hashtab_obstack_allocate,
23135 dummy_obstack_deallocate);
23136 }
23137
23138 ofs.per_cu = cu->per_cu;
23139 ofs.sect_off = die->sect_off;
23140 ofs.type = type;
23141 slot = (struct dwarf2_per_cu_offset_and_type **)
23142 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
23143 if (*slot)
23144 complaint (&symfile_complaints,
23145 _("A problem internal to GDB: DIE 0x%x has type already set"),
23146 to_underlying (die->sect_off));
23147 *slot = XOBNEW (&objfile->objfile_obstack,
23148 struct dwarf2_per_cu_offset_and_type);
23149 **slot = ofs;
23150 return type;
23151 }
23152
23153 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23154 or return NULL if the die does not have a saved type. */
23155
23156 static struct type *
23157 get_die_type_at_offset (sect_offset sect_off,
23158 struct dwarf2_per_cu_data *per_cu)
23159 {
23160 struct dwarf2_per_cu_offset_and_type *slot, ofs;
23161
23162 if (dwarf2_per_objfile->die_type_hash == NULL)
23163 return NULL;
23164
23165 ofs.per_cu = per_cu;
23166 ofs.sect_off = sect_off;
23167 slot = ((struct dwarf2_per_cu_offset_and_type *)
23168 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
23169 if (slot)
23170 return slot->type;
23171 else
23172 return NULL;
23173 }
23174
23175 /* Look up the type for DIE in CU in die_type_hash,
23176 or return NULL if DIE does not have a saved type. */
23177
23178 static struct type *
23179 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23180 {
23181 return get_die_type_at_offset (die->sect_off, cu->per_cu);
23182 }
23183
23184 /* Add a dependence relationship from CU to REF_PER_CU. */
23185
23186 static void
23187 dwarf2_add_dependence (struct dwarf2_cu *cu,
23188 struct dwarf2_per_cu_data *ref_per_cu)
23189 {
23190 void **slot;
23191
23192 if (cu->dependencies == NULL)
23193 cu->dependencies
23194 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23195 NULL, &cu->comp_unit_obstack,
23196 hashtab_obstack_allocate,
23197 dummy_obstack_deallocate);
23198
23199 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23200 if (*slot == NULL)
23201 *slot = ref_per_cu;
23202 }
23203
23204 /* Subroutine of dwarf2_mark to pass to htab_traverse.
23205 Set the mark field in every compilation unit in the
23206 cache that we must keep because we are keeping CU. */
23207
23208 static int
23209 dwarf2_mark_helper (void **slot, void *data)
23210 {
23211 struct dwarf2_per_cu_data *per_cu;
23212
23213 per_cu = (struct dwarf2_per_cu_data *) *slot;
23214
23215 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23216 reading of the chain. As such dependencies remain valid it is not much
23217 useful to track and undo them during QUIT cleanups. */
23218 if (per_cu->cu == NULL)
23219 return 1;
23220
23221 if (per_cu->cu->mark)
23222 return 1;
23223 per_cu->cu->mark = 1;
23224
23225 if (per_cu->cu->dependencies != NULL)
23226 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23227
23228 return 1;
23229 }
23230
23231 /* Set the mark field in CU and in every other compilation unit in the
23232 cache that we must keep because we are keeping CU. */
23233
23234 static void
23235 dwarf2_mark (struct dwarf2_cu *cu)
23236 {
23237 if (cu->mark)
23238 return;
23239 cu->mark = 1;
23240 if (cu->dependencies != NULL)
23241 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
23242 }
23243
23244 static void
23245 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23246 {
23247 while (per_cu)
23248 {
23249 per_cu->cu->mark = 0;
23250 per_cu = per_cu->cu->read_in_chain;
23251 }
23252 }
23253
23254 /* Trivial hash function for partial_die_info: the hash value of a DIE
23255 is its offset in .debug_info for this objfile. */
23256
23257 static hashval_t
23258 partial_die_hash (const void *item)
23259 {
23260 const struct partial_die_info *part_die
23261 = (const struct partial_die_info *) item;
23262
23263 return to_underlying (part_die->sect_off);
23264 }
23265
23266 /* Trivial comparison function for partial_die_info structures: two DIEs
23267 are equal if they have the same offset. */
23268
23269 static int
23270 partial_die_eq (const void *item_lhs, const void *item_rhs)
23271 {
23272 const struct partial_die_info *part_die_lhs
23273 = (const struct partial_die_info *) item_lhs;
23274 const struct partial_die_info *part_die_rhs
23275 = (const struct partial_die_info *) item_rhs;
23276
23277 return part_die_lhs->sect_off == part_die_rhs->sect_off;
23278 }
23279
23280 static struct cmd_list_element *set_dwarf_cmdlist;
23281 static struct cmd_list_element *show_dwarf_cmdlist;
23282
23283 static void
23284 set_dwarf_cmd (char *args, int from_tty)
23285 {
23286 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
23287 gdb_stdout);
23288 }
23289
23290 static void
23291 show_dwarf_cmd (char *args, int from_tty)
23292 {
23293 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
23294 }
23295
23296 /* Free data associated with OBJFILE, if necessary. */
23297
23298 static void
23299 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
23300 {
23301 struct dwarf2_per_objfile *data = (struct dwarf2_per_objfile *) d;
23302 int ix;
23303
23304 /* Make sure we don't accidentally use dwarf2_per_objfile while
23305 cleaning up. */
23306 dwarf2_per_objfile = NULL;
23307
23308 for (ix = 0; ix < data->n_comp_units; ++ix)
23309 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
23310
23311 for (ix = 0; ix < data->n_type_units; ++ix)
23312 VEC_free (dwarf2_per_cu_ptr,
23313 data->all_type_units[ix]->per_cu.imported_symtabs);
23314 xfree (data->all_type_units);
23315
23316 VEC_free (dwarf2_section_info_def, data->types);
23317
23318 if (data->dwo_files)
23319 free_dwo_files (data->dwo_files, objfile);
23320 if (data->dwp_file)
23321 gdb_bfd_unref (data->dwp_file->dbfd);
23322
23323 if (data->dwz_file && data->dwz_file->dwz_bfd)
23324 gdb_bfd_unref (data->dwz_file->dwz_bfd);
23325 }
23326
23327 \f
23328 /* The "save gdb-index" command. */
23329
23330 /* In-memory buffer to prepare data to be written later to a file. */
23331 class data_buf
23332 {
23333 public:
23334 /* Copy DATA to the end of the buffer. */
23335 template<typename T>
23336 void append_data (const T &data)
23337 {
23338 std::copy (reinterpret_cast<const gdb_byte *> (&data),
23339 reinterpret_cast<const gdb_byte *> (&data + 1),
23340 grow (sizeof (data)));
23341 }
23342
23343 /* Copy CSTR (a zero-terminated string) to the end of buffer. The
23344 terminating zero is appended too. */
23345 void append_cstr0 (const char *cstr)
23346 {
23347 const size_t size = strlen (cstr) + 1;
23348 std::copy (cstr, cstr + size, grow (size));
23349 }
23350
23351 /* Accept a host-format integer in VAL and append it to the buffer
23352 as a target-format integer which is LEN bytes long. */
23353 void append_uint (size_t len, bfd_endian byte_order, ULONGEST val)
23354 {
23355 ::store_unsigned_integer (grow (len), len, byte_order, val);
23356 }
23357
23358 /* Return the size of the buffer. */
23359 size_t size () const
23360 {
23361 return m_vec.size ();
23362 }
23363
23364 /* Write the buffer to FILE. */
23365 void file_write (FILE *file) const
23366 {
23367 if (::fwrite (m_vec.data (), 1, m_vec.size (), file) != m_vec.size ())
23368 error (_("couldn't write data to file"));
23369 }
23370
23371 private:
23372 /* Grow SIZE bytes at the end of the buffer. Returns a pointer to
23373 the start of the new block. */
23374 gdb_byte *grow (size_t size)
23375 {
23376 m_vec.resize (m_vec.size () + size);
23377 return &*m_vec.end () - size;
23378 }
23379
23380 gdb::byte_vector m_vec;
23381 };
23382
23383 /* An entry in the symbol table. */
23384 struct symtab_index_entry
23385 {
23386 /* The name of the symbol. */
23387 const char *name;
23388 /* The offset of the name in the constant pool. */
23389 offset_type index_offset;
23390 /* A sorted vector of the indices of all the CUs that hold an object
23391 of this name. */
23392 std::vector<offset_type> cu_indices;
23393 };
23394
23395 /* The symbol table. This is a power-of-2-sized hash table. */
23396 struct mapped_symtab
23397 {
23398 mapped_symtab ()
23399 {
23400 data.resize (1024);
23401 }
23402
23403 offset_type n_elements = 0;
23404 std::vector<symtab_index_entry> data;
23405 };
23406
23407 /* Find a slot in SYMTAB for the symbol NAME. Returns a reference to
23408 the slot.
23409
23410 Function is used only during write_hash_table so no index format backward
23411 compatibility is needed. */
23412
23413 static symtab_index_entry &
23414 find_slot (struct mapped_symtab *symtab, const char *name)
23415 {
23416 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
23417
23418 index = hash & (symtab->data.size () - 1);
23419 step = ((hash * 17) & (symtab->data.size () - 1)) | 1;
23420
23421 for (;;)
23422 {
23423 if (symtab->data[index].name == NULL
23424 || strcmp (name, symtab->data[index].name) == 0)
23425 return symtab->data[index];
23426 index = (index + step) & (symtab->data.size () - 1);
23427 }
23428 }
23429
23430 /* Expand SYMTAB's hash table. */
23431
23432 static void
23433 hash_expand (struct mapped_symtab *symtab)
23434 {
23435 auto old_entries = std::move (symtab->data);
23436
23437 symtab->data.clear ();
23438 symtab->data.resize (old_entries.size () * 2);
23439
23440 for (auto &it : old_entries)
23441 if (it.name != NULL)
23442 {
23443 auto &ref = find_slot (symtab, it.name);
23444 ref = std::move (it);
23445 }
23446 }
23447
23448 /* Add an entry to SYMTAB. NAME is the name of the symbol.
23449 CU_INDEX is the index of the CU in which the symbol appears.
23450 IS_STATIC is one if the symbol is static, otherwise zero (global). */
23451
23452 static void
23453 add_index_entry (struct mapped_symtab *symtab, const char *name,
23454 int is_static, gdb_index_symbol_kind kind,
23455 offset_type cu_index)
23456 {
23457 offset_type cu_index_and_attrs;
23458
23459 ++symtab->n_elements;
23460 if (4 * symtab->n_elements / 3 >= symtab->data.size ())
23461 hash_expand (symtab);
23462
23463 symtab_index_entry &slot = find_slot (symtab, name);
23464 if (slot.name == NULL)
23465 {
23466 slot.name = name;
23467 /* index_offset is set later. */
23468 }
23469
23470 cu_index_and_attrs = 0;
23471 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
23472 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
23473 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
23474
23475 /* We don't want to record an index value twice as we want to avoid the
23476 duplication.
23477 We process all global symbols and then all static symbols
23478 (which would allow us to avoid the duplication by only having to check
23479 the last entry pushed), but a symbol could have multiple kinds in one CU.
23480 To keep things simple we don't worry about the duplication here and
23481 sort and uniqufy the list after we've processed all symbols. */
23482 slot.cu_indices.push_back (cu_index_and_attrs);
23483 }
23484
23485 /* Sort and remove duplicates of all symbols' cu_indices lists. */
23486
23487 static void
23488 uniquify_cu_indices (struct mapped_symtab *symtab)
23489 {
23490 for (auto &entry : symtab->data)
23491 {
23492 if (entry.name != NULL && !entry.cu_indices.empty ())
23493 {
23494 auto &cu_indices = entry.cu_indices;
23495 std::sort (cu_indices.begin (), cu_indices.end ());
23496 auto from = std::unique (cu_indices.begin (), cu_indices.end ());
23497 cu_indices.erase (from, cu_indices.end ());
23498 }
23499 }
23500 }
23501
23502 /* A form of 'const char *' suitable for container keys. Only the
23503 pointer is stored. The strings themselves are compared, not the
23504 pointers. */
23505 class c_str_view
23506 {
23507 public:
23508 c_str_view (const char *cstr)
23509 : m_cstr (cstr)
23510 {}
23511
23512 bool operator== (const c_str_view &other) const
23513 {
23514 return strcmp (m_cstr, other.m_cstr) == 0;
23515 }
23516
23517 private:
23518 friend class c_str_view_hasher;
23519 const char *const m_cstr;
23520 };
23521
23522 /* A std::unordered_map::hasher for c_str_view that uses the right
23523 hash function for strings in a mapped index. */
23524 class c_str_view_hasher
23525 {
23526 public:
23527 size_t operator () (const c_str_view &x) const
23528 {
23529 return mapped_index_string_hash (INT_MAX, x.m_cstr);
23530 }
23531 };
23532
23533 /* A std::unordered_map::hasher for std::vector<>. */
23534 template<typename T>
23535 class vector_hasher
23536 {
23537 public:
23538 size_t operator () (const std::vector<T> &key) const
23539 {
23540 return iterative_hash (key.data (),
23541 sizeof (key.front ()) * key.size (), 0);
23542 }
23543 };
23544
23545 /* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with
23546 constant pool entries going into the data buffer CPOOL. */
23547
23548 static void
23549 write_hash_table (mapped_symtab *symtab, data_buf &output, data_buf &cpool)
23550 {
23551 {
23552 /* Elements are sorted vectors of the indices of all the CUs that
23553 hold an object of this name. */
23554 std::unordered_map<std::vector<offset_type>, offset_type,
23555 vector_hasher<offset_type>>
23556 symbol_hash_table;
23557
23558 /* We add all the index vectors to the constant pool first, to
23559 ensure alignment is ok. */
23560 for (symtab_index_entry &entry : symtab->data)
23561 {
23562 if (entry.name == NULL)
23563 continue;
23564 gdb_assert (entry.index_offset == 0);
23565
23566 /* Finding before inserting is faster than always trying to
23567 insert, because inserting always allocates a node, does the
23568 lookup, and then destroys the new node if another node
23569 already had the same key. C++17 try_emplace will avoid
23570 this. */
23571 const auto found
23572 = symbol_hash_table.find (entry.cu_indices);
23573 if (found != symbol_hash_table.end ())
23574 {
23575 entry.index_offset = found->second;
23576 continue;
23577 }
23578
23579 symbol_hash_table.emplace (entry.cu_indices, cpool.size ());
23580 entry.index_offset = cpool.size ();
23581 cpool.append_data (MAYBE_SWAP (entry.cu_indices.size ()));
23582 for (const auto index : entry.cu_indices)
23583 cpool.append_data (MAYBE_SWAP (index));
23584 }
23585 }
23586
23587 /* Now write out the hash table. */
23588 std::unordered_map<c_str_view, offset_type, c_str_view_hasher> str_table;
23589 for (const auto &entry : symtab->data)
23590 {
23591 offset_type str_off, vec_off;
23592
23593 if (entry.name != NULL)
23594 {
23595 const auto insertpair = str_table.emplace (entry.name, cpool.size ());
23596 if (insertpair.second)
23597 cpool.append_cstr0 (entry.name);
23598 str_off = insertpair.first->second;
23599 vec_off = entry.index_offset;
23600 }
23601 else
23602 {
23603 /* While 0 is a valid constant pool index, it is not valid
23604 to have 0 for both offsets. */
23605 str_off = 0;
23606 vec_off = 0;
23607 }
23608
23609 output.append_data (MAYBE_SWAP (str_off));
23610 output.append_data (MAYBE_SWAP (vec_off));
23611 }
23612 }
23613
23614 typedef std::unordered_map<partial_symtab *, unsigned int> psym_index_map;
23615
23616 /* Helper struct for building the address table. */
23617 struct addrmap_index_data
23618 {
23619 addrmap_index_data (data_buf &addr_vec_, psym_index_map &cu_index_htab_)
23620 : addr_vec (addr_vec_), cu_index_htab (cu_index_htab_)
23621 {}
23622
23623 struct objfile *objfile;
23624 data_buf &addr_vec;
23625 psym_index_map &cu_index_htab;
23626
23627 /* Non-zero if the previous_* fields are valid.
23628 We can't write an entry until we see the next entry (since it is only then
23629 that we know the end of the entry). */
23630 int previous_valid;
23631 /* Index of the CU in the table of all CUs in the index file. */
23632 unsigned int previous_cu_index;
23633 /* Start address of the CU. */
23634 CORE_ADDR previous_cu_start;
23635 };
23636
23637 /* Write an address entry to ADDR_VEC. */
23638
23639 static void
23640 add_address_entry (struct objfile *objfile, data_buf &addr_vec,
23641 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
23642 {
23643 CORE_ADDR baseaddr;
23644
23645 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23646
23647 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, start - baseaddr);
23648 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, end - baseaddr);
23649 addr_vec.append_data (MAYBE_SWAP (cu_index));
23650 }
23651
23652 /* Worker function for traversing an addrmap to build the address table. */
23653
23654 static int
23655 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
23656 {
23657 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
23658 struct partial_symtab *pst = (struct partial_symtab *) obj;
23659
23660 if (data->previous_valid)
23661 add_address_entry (data->objfile, data->addr_vec,
23662 data->previous_cu_start, start_addr,
23663 data->previous_cu_index);
23664
23665 data->previous_cu_start = start_addr;
23666 if (pst != NULL)
23667 {
23668 const auto it = data->cu_index_htab.find (pst);
23669 gdb_assert (it != data->cu_index_htab.cend ());
23670 data->previous_cu_index = it->second;
23671 data->previous_valid = 1;
23672 }
23673 else
23674 data->previous_valid = 0;
23675
23676 return 0;
23677 }
23678
23679 /* Write OBJFILE's address map to ADDR_VEC.
23680 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
23681 in the index file. */
23682
23683 static void
23684 write_address_map (struct objfile *objfile, data_buf &addr_vec,
23685 psym_index_map &cu_index_htab)
23686 {
23687 struct addrmap_index_data addrmap_index_data (addr_vec, cu_index_htab);
23688
23689 /* When writing the address table, we have to cope with the fact that
23690 the addrmap iterator only provides the start of a region; we have to
23691 wait until the next invocation to get the start of the next region. */
23692
23693 addrmap_index_data.objfile = objfile;
23694 addrmap_index_data.previous_valid = 0;
23695
23696 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
23697 &addrmap_index_data);
23698
23699 /* It's highly unlikely the last entry (end address = 0xff...ff)
23700 is valid, but we should still handle it.
23701 The end address is recorded as the start of the next region, but that
23702 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
23703 anyway. */
23704 if (addrmap_index_data.previous_valid)
23705 add_address_entry (objfile, addr_vec,
23706 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
23707 addrmap_index_data.previous_cu_index);
23708 }
23709
23710 /* Return the symbol kind of PSYM. */
23711
23712 static gdb_index_symbol_kind
23713 symbol_kind (struct partial_symbol *psym)
23714 {
23715 domain_enum domain = PSYMBOL_DOMAIN (psym);
23716 enum address_class aclass = PSYMBOL_CLASS (psym);
23717
23718 switch (domain)
23719 {
23720 case VAR_DOMAIN:
23721 switch (aclass)
23722 {
23723 case LOC_BLOCK:
23724 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
23725 case LOC_TYPEDEF:
23726 return GDB_INDEX_SYMBOL_KIND_TYPE;
23727 case LOC_COMPUTED:
23728 case LOC_CONST_BYTES:
23729 case LOC_OPTIMIZED_OUT:
23730 case LOC_STATIC:
23731 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
23732 case LOC_CONST:
23733 /* Note: It's currently impossible to recognize psyms as enum values
23734 short of reading the type info. For now punt. */
23735 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
23736 default:
23737 /* There are other LOC_FOO values that one might want to classify
23738 as variables, but dwarf2read.c doesn't currently use them. */
23739 return GDB_INDEX_SYMBOL_KIND_OTHER;
23740 }
23741 case STRUCT_DOMAIN:
23742 return GDB_INDEX_SYMBOL_KIND_TYPE;
23743 default:
23744 return GDB_INDEX_SYMBOL_KIND_OTHER;
23745 }
23746 }
23747
23748 /* Add a list of partial symbols to SYMTAB. */
23749
23750 static void
23751 write_psymbols (struct mapped_symtab *symtab,
23752 std::unordered_set<partial_symbol *> &psyms_seen,
23753 struct partial_symbol **psymp,
23754 int count,
23755 offset_type cu_index,
23756 int is_static)
23757 {
23758 for (; count-- > 0; ++psymp)
23759 {
23760 struct partial_symbol *psym = *psymp;
23761
23762 if (SYMBOL_LANGUAGE (psym) == language_ada)
23763 error (_("Ada is not currently supported by the index"));
23764
23765 /* Only add a given psymbol once. */
23766 if (psyms_seen.insert (psym).second)
23767 {
23768 gdb_index_symbol_kind kind = symbol_kind (psym);
23769
23770 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
23771 is_static, kind, cu_index);
23772 }
23773 }
23774 }
23775
23776 /* A helper struct used when iterating over debug_types. */
23777 struct signatured_type_index_data
23778 {
23779 signatured_type_index_data (data_buf &types_list_,
23780 std::unordered_set<partial_symbol *> &psyms_seen_)
23781 : types_list (types_list_), psyms_seen (psyms_seen_)
23782 {}
23783
23784 struct objfile *objfile;
23785 struct mapped_symtab *symtab;
23786 data_buf &types_list;
23787 std::unordered_set<partial_symbol *> &psyms_seen;
23788 int cu_index;
23789 };
23790
23791 /* A helper function that writes a single signatured_type to an
23792 obstack. */
23793
23794 static int
23795 write_one_signatured_type (void **slot, void *d)
23796 {
23797 struct signatured_type_index_data *info
23798 = (struct signatured_type_index_data *) d;
23799 struct signatured_type *entry = (struct signatured_type *) *slot;
23800 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
23801
23802 write_psymbols (info->symtab,
23803 info->psyms_seen,
23804 info->objfile->global_psymbols.list
23805 + psymtab->globals_offset,
23806 psymtab->n_global_syms, info->cu_index,
23807 0);
23808 write_psymbols (info->symtab,
23809 info->psyms_seen,
23810 info->objfile->static_psymbols.list
23811 + psymtab->statics_offset,
23812 psymtab->n_static_syms, info->cu_index,
23813 1);
23814
23815 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
23816 to_underlying (entry->per_cu.sect_off));
23817 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
23818 to_underlying (entry->type_offset_in_tu));
23819 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE, entry->signature);
23820
23821 ++info->cu_index;
23822
23823 return 1;
23824 }
23825
23826 /* Recurse into all "included" dependencies and count their symbols as
23827 if they appeared in this psymtab. */
23828
23829 static void
23830 recursively_count_psymbols (struct partial_symtab *psymtab,
23831 size_t &psyms_seen)
23832 {
23833 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
23834 if (psymtab->dependencies[i]->user != NULL)
23835 recursively_count_psymbols (psymtab->dependencies[i],
23836 psyms_seen);
23837
23838 psyms_seen += psymtab->n_global_syms;
23839 psyms_seen += psymtab->n_static_syms;
23840 }
23841
23842 /* Recurse into all "included" dependencies and write their symbols as
23843 if they appeared in this psymtab. */
23844
23845 static void
23846 recursively_write_psymbols (struct objfile *objfile,
23847 struct partial_symtab *psymtab,
23848 struct mapped_symtab *symtab,
23849 std::unordered_set<partial_symbol *> &psyms_seen,
23850 offset_type cu_index)
23851 {
23852 int i;
23853
23854 for (i = 0; i < psymtab->number_of_dependencies; ++i)
23855 if (psymtab->dependencies[i]->user != NULL)
23856 recursively_write_psymbols (objfile, psymtab->dependencies[i],
23857 symtab, psyms_seen, cu_index);
23858
23859 write_psymbols (symtab,
23860 psyms_seen,
23861 objfile->global_psymbols.list + psymtab->globals_offset,
23862 psymtab->n_global_syms, cu_index,
23863 0);
23864 write_psymbols (symtab,
23865 psyms_seen,
23866 objfile->static_psymbols.list + psymtab->statics_offset,
23867 psymtab->n_static_syms, cu_index,
23868 1);
23869 }
23870
23871 /* Create an index file for OBJFILE in the directory DIR. */
23872
23873 static void
23874 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
23875 {
23876 if (dwarf2_per_objfile->using_index)
23877 error (_("Cannot use an index to create the index"));
23878
23879 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
23880 error (_("Cannot make an index when the file has multiple .debug_types sections"));
23881
23882 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
23883 return;
23884
23885 struct stat st;
23886 if (stat (objfile_name (objfile), &st) < 0)
23887 perror_with_name (objfile_name (objfile));
23888
23889 std::string filename (std::string (dir) + SLASH_STRING
23890 + lbasename (objfile_name (objfile)) + INDEX_SUFFIX);
23891
23892 FILE *out_file = gdb_fopen_cloexec (filename.c_str (), "wb").release ();
23893 if (!out_file)
23894 error (_("Can't open `%s' for writing"), filename.c_str ());
23895
23896 /* Order matters here; we want FILE to be closed before FILENAME is
23897 unlinked, because on MS-Windows one cannot delete a file that is
23898 still open. (Don't call anything here that might throw until
23899 file_closer is created.) */
23900 gdb::unlinker unlink_file (filename.c_str ());
23901 gdb_file_up close_out_file (out_file);
23902
23903 mapped_symtab symtab;
23904 data_buf cu_list;
23905
23906 /* While we're scanning CU's create a table that maps a psymtab pointer
23907 (which is what addrmap records) to its index (which is what is recorded
23908 in the index file). This will later be needed to write the address
23909 table. */
23910 psym_index_map cu_index_htab;
23911 cu_index_htab.reserve (dwarf2_per_objfile->n_comp_units);
23912
23913 /* The CU list is already sorted, so we don't need to do additional
23914 work here. Also, the debug_types entries do not appear in
23915 all_comp_units, but only in their own hash table. */
23916
23917 /* The psyms_seen set is potentially going to be largish (~40k
23918 elements when indexing a -g3 build of GDB itself). Estimate the
23919 number of elements in order to avoid too many rehashes, which
23920 require rebuilding buckets and thus many trips to
23921 malloc/free. */
23922 size_t psyms_count = 0;
23923 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
23924 {
23925 struct dwarf2_per_cu_data *per_cu
23926 = dwarf2_per_objfile->all_comp_units[i];
23927 struct partial_symtab *psymtab = per_cu->v.psymtab;
23928
23929 if (psymtab != NULL && psymtab->user == NULL)
23930 recursively_count_psymbols (psymtab, psyms_count);
23931 }
23932 /* Generating an index for gdb itself shows a ratio of
23933 TOTAL_SEEN_SYMS/UNIQUE_SYMS or ~5. 4 seems like a good bet. */
23934 std::unordered_set<partial_symbol *> psyms_seen (psyms_count / 4);
23935 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
23936 {
23937 struct dwarf2_per_cu_data *per_cu
23938 = dwarf2_per_objfile->all_comp_units[i];
23939 struct partial_symtab *psymtab = per_cu->v.psymtab;
23940
23941 /* CU of a shared file from 'dwz -m' may be unused by this main file.
23942 It may be referenced from a local scope but in such case it does not
23943 need to be present in .gdb_index. */
23944 if (psymtab == NULL)
23945 continue;
23946
23947 if (psymtab->user == NULL)
23948 recursively_write_psymbols (objfile, psymtab, &symtab,
23949 psyms_seen, i);
23950
23951 const auto insertpair = cu_index_htab.emplace (psymtab, i);
23952 gdb_assert (insertpair.second);
23953
23954 cu_list.append_uint (8, BFD_ENDIAN_LITTLE,
23955 to_underlying (per_cu->sect_off));
23956 cu_list.append_uint (8, BFD_ENDIAN_LITTLE, per_cu->length);
23957 }
23958
23959 /* Dump the address map. */
23960 data_buf addr_vec;
23961 write_address_map (objfile, addr_vec, cu_index_htab);
23962
23963 /* Write out the .debug_type entries, if any. */
23964 data_buf types_cu_list;
23965 if (dwarf2_per_objfile->signatured_types)
23966 {
23967 signatured_type_index_data sig_data (types_cu_list,
23968 psyms_seen);
23969
23970 sig_data.objfile = objfile;
23971 sig_data.symtab = &symtab;
23972 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
23973 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
23974 write_one_signatured_type, &sig_data);
23975 }
23976
23977 /* Now that we've processed all symbols we can shrink their cu_indices
23978 lists. */
23979 uniquify_cu_indices (&symtab);
23980
23981 data_buf symtab_vec, constant_pool;
23982 write_hash_table (&symtab, symtab_vec, constant_pool);
23983
23984 data_buf contents;
23985 const offset_type size_of_contents = 6 * sizeof (offset_type);
23986 offset_type total_len = size_of_contents;
23987
23988 /* The version number. */
23989 contents.append_data (MAYBE_SWAP (8));
23990
23991 /* The offset of the CU list from the start of the file. */
23992 contents.append_data (MAYBE_SWAP (total_len));
23993 total_len += cu_list.size ();
23994
23995 /* The offset of the types CU list from the start of the file. */
23996 contents.append_data (MAYBE_SWAP (total_len));
23997 total_len += types_cu_list.size ();
23998
23999 /* The offset of the address table from the start of the file. */
24000 contents.append_data (MAYBE_SWAP (total_len));
24001 total_len += addr_vec.size ();
24002
24003 /* The offset of the symbol table from the start of the file. */
24004 contents.append_data (MAYBE_SWAP (total_len));
24005 total_len += symtab_vec.size ();
24006
24007 /* The offset of the constant pool from the start of the file. */
24008 contents.append_data (MAYBE_SWAP (total_len));
24009 total_len += constant_pool.size ();
24010
24011 gdb_assert (contents.size () == size_of_contents);
24012
24013 contents.file_write (out_file);
24014 cu_list.file_write (out_file);
24015 types_cu_list.file_write (out_file);
24016 addr_vec.file_write (out_file);
24017 symtab_vec.file_write (out_file);
24018 constant_pool.file_write (out_file);
24019
24020 /* We want to keep the file. */
24021 unlink_file.keep ();
24022 }
24023
24024 /* Implementation of the `save gdb-index' command.
24025
24026 Note that the file format used by this command is documented in the
24027 GDB manual. Any changes here must be documented there. */
24028
24029 static void
24030 save_gdb_index_command (char *arg, int from_tty)
24031 {
24032 struct objfile *objfile;
24033
24034 if (!arg || !*arg)
24035 error (_("usage: save gdb-index DIRECTORY"));
24036
24037 ALL_OBJFILES (objfile)
24038 {
24039 struct stat st;
24040
24041 /* If the objfile does not correspond to an actual file, skip it. */
24042 if (stat (objfile_name (objfile), &st) < 0)
24043 continue;
24044
24045 dwarf2_per_objfile
24046 = (struct dwarf2_per_objfile *) objfile_data (objfile,
24047 dwarf2_objfile_data_key);
24048 if (dwarf2_per_objfile)
24049 {
24050
24051 TRY
24052 {
24053 write_psymtabs_to_index (objfile, arg);
24054 }
24055 CATCH (except, RETURN_MASK_ERROR)
24056 {
24057 exception_fprintf (gdb_stderr, except,
24058 _("Error while writing index for `%s': "),
24059 objfile_name (objfile));
24060 }
24061 END_CATCH
24062 }
24063 }
24064 }
24065
24066 \f
24067
24068 int dwarf_always_disassemble;
24069
24070 static void
24071 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
24072 struct cmd_list_element *c, const char *value)
24073 {
24074 fprintf_filtered (file,
24075 _("Whether to always disassemble "
24076 "DWARF expressions is %s.\n"),
24077 value);
24078 }
24079
24080 static void
24081 show_check_physname (struct ui_file *file, int from_tty,
24082 struct cmd_list_element *c, const char *value)
24083 {
24084 fprintf_filtered (file,
24085 _("Whether to check \"physname\" is %s.\n"),
24086 value);
24087 }
24088
24089 void
24090 _initialize_dwarf2_read (void)
24091 {
24092 struct cmd_list_element *c;
24093
24094 dwarf2_objfile_data_key
24095 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
24096
24097 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
24098 Set DWARF specific variables.\n\
24099 Configure DWARF variables such as the cache size"),
24100 &set_dwarf_cmdlist, "maintenance set dwarf ",
24101 0/*allow-unknown*/, &maintenance_set_cmdlist);
24102
24103 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
24104 Show DWARF specific variables\n\
24105 Show DWARF variables such as the cache size"),
24106 &show_dwarf_cmdlist, "maintenance show dwarf ",
24107 0/*allow-unknown*/, &maintenance_show_cmdlist);
24108
24109 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24110 &dwarf_max_cache_age, _("\
24111 Set the upper bound on the age of cached DWARF compilation units."), _("\
24112 Show the upper bound on the age of cached DWARF compilation units."), _("\
24113 A higher limit means that cached compilation units will be stored\n\
24114 in memory longer, and more total memory will be used. Zero disables\n\
24115 caching, which can slow down startup."),
24116 NULL,
24117 show_dwarf_max_cache_age,
24118 &set_dwarf_cmdlist,
24119 &show_dwarf_cmdlist);
24120
24121 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
24122 &dwarf_always_disassemble, _("\
24123 Set whether `info address' always disassembles DWARF expressions."), _("\
24124 Show whether `info address' always disassembles DWARF expressions."), _("\
24125 When enabled, DWARF expressions are always printed in an assembly-like\n\
24126 syntax. When disabled, expressions will be printed in a more\n\
24127 conversational style, when possible."),
24128 NULL,
24129 show_dwarf_always_disassemble,
24130 &set_dwarf_cmdlist,
24131 &show_dwarf_cmdlist);
24132
24133 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24134 Set debugging of the DWARF reader."), _("\
24135 Show debugging of the DWARF reader."), _("\
24136 When enabled (non-zero), debugging messages are printed during DWARF\n\
24137 reading and symtab expansion. A value of 1 (one) provides basic\n\
24138 information. A value greater than 1 provides more verbose information."),
24139 NULL,
24140 NULL,
24141 &setdebuglist, &showdebuglist);
24142
24143 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24144 Set debugging of the DWARF DIE reader."), _("\
24145 Show debugging of the DWARF DIE reader."), _("\
24146 When enabled (non-zero), DIEs are dumped after they are read in.\n\
24147 The value is the maximum depth to print."),
24148 NULL,
24149 NULL,
24150 &setdebuglist, &showdebuglist);
24151
24152 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24153 Set debugging of the dwarf line reader."), _("\
24154 Show debugging of the dwarf line reader."), _("\
24155 When enabled (non-zero), line number entries are dumped as they are read in.\n\
24156 A value of 1 (one) provides basic information.\n\
24157 A value greater than 1 provides more verbose information."),
24158 NULL,
24159 NULL,
24160 &setdebuglist, &showdebuglist);
24161
24162 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24163 Set cross-checking of \"physname\" code against demangler."), _("\
24164 Show cross-checking of \"physname\" code against demangler."), _("\
24165 When enabled, GDB's internal \"physname\" code is checked against\n\
24166 the demangler."),
24167 NULL, show_check_physname,
24168 &setdebuglist, &showdebuglist);
24169
24170 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24171 no_class, &use_deprecated_index_sections, _("\
24172 Set whether to use deprecated gdb_index sections."), _("\
24173 Show whether to use deprecated gdb_index sections."), _("\
24174 When enabled, deprecated .gdb_index sections are used anyway.\n\
24175 Normally they are ignored either because of a missing feature or\n\
24176 performance issue.\n\
24177 Warning: This option must be enabled before gdb reads the file."),
24178 NULL,
24179 NULL,
24180 &setlist, &showlist);
24181
24182 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
24183 _("\
24184 Save a gdb-index file.\n\
24185 Usage: save gdb-index DIRECTORY"),
24186 &save_cmdlist);
24187 set_cmd_completer (c, filename_completer);
24188
24189 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24190 &dwarf2_locexpr_funcs);
24191 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24192 &dwarf2_loclist_funcs);
24193
24194 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24195 &dwarf2_block_frame_base_locexpr_funcs);
24196 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24197 &dwarf2_block_frame_base_loclist_funcs);
24198 }
This page took 0.738421 seconds and 4 git commands to generate.