Allocate dwp_file with new
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2018 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 "dwarf2read.h"
33 #include "dwarf-index-common.h"
34 #include "bfd.h"
35 #include "elf-bfd.h"
36 #include "symtab.h"
37 #include "gdbtypes.h"
38 #include "objfiles.h"
39 #include "dwarf2.h"
40 #include "buildsym.h"
41 #include "demangle.h"
42 #include "gdb-demangle.h"
43 #include "expression.h"
44 #include "filenames.h" /* for DOSish file names */
45 #include "macrotab.h"
46 #include "language.h"
47 #include "complaints.h"
48 #include "bcache.h"
49 #include "dwarf2expr.h"
50 #include "dwarf2loc.h"
51 #include "cp-support.h"
52 #include "hashtab.h"
53 #include "command.h"
54 #include "gdbcmd.h"
55 #include "block.h"
56 #include "addrmap.h"
57 #include "typeprint.h"
58 #include "psympriv.h"
59 #include <sys/stat.h>
60 #include "completer.h"
61 #include "vec.h"
62 #include "c-lang.h"
63 #include "go-lang.h"
64 #include "valprint.h"
65 #include "gdbcore.h" /* for gnutarget */
66 #include "gdb/gdb-index.h"
67 #include <ctype.h>
68 #include "gdb_bfd.h"
69 #include "f-lang.h"
70 #include "source.h"
71 #include "filestuff.h"
72 #include "build-id.h"
73 #include "namespace.h"
74 #include "common/gdb_unlinker.h"
75 #include "common/function-view.h"
76 #include "common/gdb_optional.h"
77 #include "common/underlying.h"
78 #include "common/byte-vector.h"
79 #include "common/hash_enum.h"
80 #include "filename-seen-cache.h"
81 #include "producer.h"
82 #include <fcntl.h>
83 #include <sys/types.h>
84 #include <algorithm>
85 #include <unordered_set>
86 #include <unordered_map>
87 #include "selftest.h"
88 #include <cmath>
89 #include <set>
90 #include <forward_list>
91 #include "rust-lang.h"
92 #include "common/pathstuff.h"
93
94 /* When == 1, print basic high level tracing messages.
95 When > 1, be more verbose.
96 This is in contrast to the low level DIE reading of dwarf_die_debug. */
97 static unsigned int dwarf_read_debug = 0;
98
99 /* When non-zero, dump DIEs after they are read in. */
100 static unsigned int dwarf_die_debug = 0;
101
102 /* When non-zero, dump line number entries as they are read in. */
103 static unsigned int dwarf_line_debug = 0;
104
105 /* When non-zero, cross-check physname against demangler. */
106 static int check_physname = 0;
107
108 /* When non-zero, do not reject deprecated .gdb_index sections. */
109 static int use_deprecated_index_sections = 0;
110
111 static const struct objfile_data *dwarf2_objfile_data_key;
112
113 /* The "aclass" indices for various kinds of computed DWARF symbols. */
114
115 static int dwarf2_locexpr_index;
116 static int dwarf2_loclist_index;
117 static int dwarf2_locexpr_block_index;
118 static int dwarf2_loclist_block_index;
119
120 /* An index into a (C++) symbol name component in a symbol name as
121 recorded in the mapped_index's symbol table. For each C++ symbol
122 in the symbol table, we record one entry for the start of each
123 component in the symbol in a table of name components, and then
124 sort the table, in order to be able to binary search symbol names,
125 ignoring leading namespaces, both completion and regular look up.
126 For example, for symbol "A::B::C", we'll have an entry that points
127 to "A::B::C", another that points to "B::C", and another for "C".
128 Note that function symbols in GDB index have no parameter
129 information, just the function/method names. You can convert a
130 name_component to a "const char *" using the
131 'mapped_index::symbol_name_at(offset_type)' method. */
132
133 struct name_component
134 {
135 /* Offset in the symbol name where the component starts. Stored as
136 a (32-bit) offset instead of a pointer to save memory and improve
137 locality on 64-bit architectures. */
138 offset_type name_offset;
139
140 /* The symbol's index in the symbol and constant pool tables of a
141 mapped_index. */
142 offset_type idx;
143 };
144
145 /* Base class containing bits shared by both .gdb_index and
146 .debug_name indexes. */
147
148 struct mapped_index_base
149 {
150 /* The name_component table (a sorted vector). See name_component's
151 description above. */
152 std::vector<name_component> name_components;
153
154 /* How NAME_COMPONENTS is sorted. */
155 enum case_sensitivity name_components_casing;
156
157 /* Return the number of names in the symbol table. */
158 virtual size_t symbol_name_count () const = 0;
159
160 /* Get the name of the symbol at IDX in the symbol table. */
161 virtual const char *symbol_name_at (offset_type idx) const = 0;
162
163 /* Return whether the name at IDX in the symbol table should be
164 ignored. */
165 virtual bool symbol_name_slot_invalid (offset_type idx) const
166 {
167 return false;
168 }
169
170 /* Build the symbol name component sorted vector, if we haven't
171 yet. */
172 void build_name_components ();
173
174 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
175 possible matches for LN_NO_PARAMS in the name component
176 vector. */
177 std::pair<std::vector<name_component>::const_iterator,
178 std::vector<name_component>::const_iterator>
179 find_name_components_bounds (const lookup_name_info &ln_no_params) const;
180
181 /* Prevent deleting/destroying via a base class pointer. */
182 protected:
183 ~mapped_index_base() = default;
184 };
185
186 /* A description of the mapped index. The file format is described in
187 a comment by the code that writes the index. */
188 struct mapped_index final : public mapped_index_base
189 {
190 /* A slot/bucket in the symbol table hash. */
191 struct symbol_table_slot
192 {
193 const offset_type name;
194 const offset_type vec;
195 };
196
197 /* Index data format version. */
198 int version = 0;
199
200 /* The address table data. */
201 gdb::array_view<const gdb_byte> address_table;
202
203 /* The symbol table, implemented as a hash table. */
204 gdb::array_view<symbol_table_slot> symbol_table;
205
206 /* A pointer to the constant pool. */
207 const char *constant_pool = nullptr;
208
209 bool symbol_name_slot_invalid (offset_type idx) const override
210 {
211 const auto &bucket = this->symbol_table[idx];
212 return bucket.name == 0 && bucket.vec;
213 }
214
215 /* Convenience method to get at the name of the symbol at IDX in the
216 symbol table. */
217 const char *symbol_name_at (offset_type idx) const override
218 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
219
220 size_t symbol_name_count () const override
221 { return this->symbol_table.size (); }
222 };
223
224 /* A description of the mapped .debug_names.
225 Uninitialized map has CU_COUNT 0. */
226 struct mapped_debug_names final : public mapped_index_base
227 {
228 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
229 : dwarf2_per_objfile (dwarf2_per_objfile_)
230 {}
231
232 struct dwarf2_per_objfile *dwarf2_per_objfile;
233 bfd_endian dwarf5_byte_order;
234 bool dwarf5_is_dwarf64;
235 bool augmentation_is_gdb;
236 uint8_t offset_size;
237 uint32_t cu_count = 0;
238 uint32_t tu_count, bucket_count, name_count;
239 const gdb_byte *cu_table_reordered, *tu_table_reordered;
240 const uint32_t *bucket_table_reordered, *hash_table_reordered;
241 const gdb_byte *name_table_string_offs_reordered;
242 const gdb_byte *name_table_entry_offs_reordered;
243 const gdb_byte *entry_pool;
244
245 struct index_val
246 {
247 ULONGEST dwarf_tag;
248 struct attr
249 {
250 /* Attribute name DW_IDX_*. */
251 ULONGEST dw_idx;
252
253 /* Attribute form DW_FORM_*. */
254 ULONGEST form;
255
256 /* Value if FORM is DW_FORM_implicit_const. */
257 LONGEST implicit_const;
258 };
259 std::vector<attr> attr_vec;
260 };
261
262 std::unordered_map<ULONGEST, index_val> abbrev_map;
263
264 const char *namei_to_name (uint32_t namei) const;
265
266 /* Implementation of the mapped_index_base virtual interface, for
267 the name_components cache. */
268
269 const char *symbol_name_at (offset_type idx) const override
270 { return namei_to_name (idx); }
271
272 size_t symbol_name_count () const override
273 { return this->name_count; }
274 };
275
276 /* See dwarf2read.h. */
277
278 dwarf2_per_objfile *
279 get_dwarf2_per_objfile (struct objfile *objfile)
280 {
281 return ((struct dwarf2_per_objfile *)
282 objfile_data (objfile, dwarf2_objfile_data_key));
283 }
284
285 /* Set the dwarf2_per_objfile associated to OBJFILE. */
286
287 void
288 set_dwarf2_per_objfile (struct objfile *objfile,
289 struct dwarf2_per_objfile *dwarf2_per_objfile)
290 {
291 gdb_assert (get_dwarf2_per_objfile (objfile) == NULL);
292 set_objfile_data (objfile, dwarf2_objfile_data_key, dwarf2_per_objfile);
293 }
294
295 /* Default names of the debugging sections. */
296
297 /* Note that if the debugging section has been compressed, it might
298 have a name like .zdebug_info. */
299
300 static const struct dwarf2_debug_sections dwarf2_elf_names =
301 {
302 { ".debug_info", ".zdebug_info" },
303 { ".debug_abbrev", ".zdebug_abbrev" },
304 { ".debug_line", ".zdebug_line" },
305 { ".debug_loc", ".zdebug_loc" },
306 { ".debug_loclists", ".zdebug_loclists" },
307 { ".debug_macinfo", ".zdebug_macinfo" },
308 { ".debug_macro", ".zdebug_macro" },
309 { ".debug_str", ".zdebug_str" },
310 { ".debug_line_str", ".zdebug_line_str" },
311 { ".debug_ranges", ".zdebug_ranges" },
312 { ".debug_rnglists", ".zdebug_rnglists" },
313 { ".debug_types", ".zdebug_types" },
314 { ".debug_addr", ".zdebug_addr" },
315 { ".debug_frame", ".zdebug_frame" },
316 { ".eh_frame", NULL },
317 { ".gdb_index", ".zgdb_index" },
318 { ".debug_names", ".zdebug_names" },
319 { ".debug_aranges", ".zdebug_aranges" },
320 23
321 };
322
323 /* List of DWO/DWP sections. */
324
325 static const struct dwop_section_names
326 {
327 struct dwarf2_section_names abbrev_dwo;
328 struct dwarf2_section_names info_dwo;
329 struct dwarf2_section_names line_dwo;
330 struct dwarf2_section_names loc_dwo;
331 struct dwarf2_section_names loclists_dwo;
332 struct dwarf2_section_names macinfo_dwo;
333 struct dwarf2_section_names macro_dwo;
334 struct dwarf2_section_names str_dwo;
335 struct dwarf2_section_names str_offsets_dwo;
336 struct dwarf2_section_names types_dwo;
337 struct dwarf2_section_names cu_index;
338 struct dwarf2_section_names tu_index;
339 }
340 dwop_section_names =
341 {
342 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
343 { ".debug_info.dwo", ".zdebug_info.dwo" },
344 { ".debug_line.dwo", ".zdebug_line.dwo" },
345 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
346 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
347 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
348 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
349 { ".debug_str.dwo", ".zdebug_str.dwo" },
350 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
351 { ".debug_types.dwo", ".zdebug_types.dwo" },
352 { ".debug_cu_index", ".zdebug_cu_index" },
353 { ".debug_tu_index", ".zdebug_tu_index" },
354 };
355
356 /* local data types */
357
358 /* The data in a compilation unit header, after target2host
359 translation, looks like this. */
360 struct comp_unit_head
361 {
362 unsigned int length;
363 short version;
364 unsigned char addr_size;
365 unsigned char signed_addr_p;
366 sect_offset abbrev_sect_off;
367
368 /* Size of file offsets; either 4 or 8. */
369 unsigned int offset_size;
370
371 /* Size of the length field; either 4 or 12. */
372 unsigned int initial_length_size;
373
374 enum dwarf_unit_type unit_type;
375
376 /* Offset to the first byte of this compilation unit header in the
377 .debug_info section, for resolving relative reference dies. */
378 sect_offset sect_off;
379
380 /* Offset to first die in this cu from the start of the cu.
381 This will be the first byte following the compilation unit header. */
382 cu_offset first_die_cu_offset;
383
384 /* 64-bit signature of this type unit - it is valid only for
385 UNIT_TYPE DW_UT_type. */
386 ULONGEST signature;
387
388 /* For types, offset in the type's DIE of the type defined by this TU. */
389 cu_offset type_cu_offset_in_tu;
390 };
391
392 /* Type used for delaying computation of method physnames.
393 See comments for compute_delayed_physnames. */
394 struct delayed_method_info
395 {
396 /* The type to which the method is attached, i.e., its parent class. */
397 struct type *type;
398
399 /* The index of the method in the type's function fieldlists. */
400 int fnfield_index;
401
402 /* The index of the method in the fieldlist. */
403 int index;
404
405 /* The name of the DIE. */
406 const char *name;
407
408 /* The DIE associated with this method. */
409 struct die_info *die;
410 };
411
412 /* Internal state when decoding a particular compilation unit. */
413 struct dwarf2_cu
414 {
415 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
416 ~dwarf2_cu ();
417
418 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
419
420 /* The header of the compilation unit. */
421 struct comp_unit_head header {};
422
423 /* Base address of this compilation unit. */
424 CORE_ADDR base_address = 0;
425
426 /* Non-zero if base_address has been set. */
427 int base_known = 0;
428
429 /* The language we are debugging. */
430 enum language language = language_unknown;
431 const struct language_defn *language_defn = nullptr;
432
433 const char *producer = nullptr;
434
435 /* The generic symbol table building routines have separate lists for
436 file scope symbols and all all other scopes (local scopes). So
437 we need to select the right one to pass to add_symbol_to_list().
438 We do it by keeping a pointer to the correct list in list_in_scope.
439
440 FIXME: The original dwarf code just treated the file scope as the
441 first local scope, and all other local scopes as nested local
442 scopes, and worked fine. Check to see if we really need to
443 distinguish these in buildsym.c. */
444 struct pending **list_in_scope = nullptr;
445
446 /* Hash table holding all the loaded partial DIEs
447 with partial_die->offset.SECT_OFF as hash. */
448 htab_t partial_dies = nullptr;
449
450 /* Storage for things with the same lifetime as this read-in compilation
451 unit, including partial DIEs. */
452 auto_obstack comp_unit_obstack;
453
454 /* When multiple dwarf2_cu structures are living in memory, this field
455 chains them all together, so that they can be released efficiently.
456 We will probably also want a generation counter so that most-recently-used
457 compilation units are cached... */
458 struct dwarf2_per_cu_data *read_in_chain = nullptr;
459
460 /* Backlink to our per_cu entry. */
461 struct dwarf2_per_cu_data *per_cu;
462
463 /* How many compilation units ago was this CU last referenced? */
464 int last_used = 0;
465
466 /* A hash table of DIE cu_offset for following references with
467 die_info->offset.sect_off as hash. */
468 htab_t die_hash = nullptr;
469
470 /* Full DIEs if read in. */
471 struct die_info *dies = nullptr;
472
473 /* A set of pointers to dwarf2_per_cu_data objects for compilation
474 units referenced by this one. Only set during full symbol processing;
475 partial symbol tables do not have dependencies. */
476 htab_t dependencies = nullptr;
477
478 /* Header data from the line table, during full symbol processing. */
479 struct line_header *line_header = nullptr;
480 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
481 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
482 this is the DW_TAG_compile_unit die for this CU. We'll hold on
483 to the line header as long as this DIE is being processed. See
484 process_die_scope. */
485 die_info *line_header_die_owner = nullptr;
486
487 /* A list of methods which need to have physnames computed
488 after all type information has been read. */
489 std::vector<delayed_method_info> method_list;
490
491 /* To be copied to symtab->call_site_htab. */
492 htab_t call_site_htab = nullptr;
493
494 /* Non-NULL if this CU came from a DWO file.
495 There is an invariant here that is important to remember:
496 Except for attributes copied from the top level DIE in the "main"
497 (or "stub") file in preparation for reading the DWO file
498 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
499 Either there isn't a DWO file (in which case this is NULL and the point
500 is moot), or there is and either we're not going to read it (in which
501 case this is NULL) or there is and we are reading it (in which case this
502 is non-NULL). */
503 struct dwo_unit *dwo_unit = nullptr;
504
505 /* The DW_AT_addr_base attribute if present, zero otherwise
506 (zero is a valid value though).
507 Note this value comes from the Fission stub CU/TU's DIE. */
508 ULONGEST addr_base = 0;
509
510 /* The DW_AT_ranges_base attribute if present, zero otherwise
511 (zero is a valid value though).
512 Note this value comes from the Fission stub CU/TU's DIE.
513 Also note that the value is zero in the non-DWO case so this value can
514 be used without needing to know whether DWO files are in use or not.
515 N.B. This does not apply to DW_AT_ranges appearing in
516 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
517 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
518 DW_AT_ranges_base *would* have to be applied, and we'd have to care
519 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
520 ULONGEST ranges_base = 0;
521
522 /* When reading debug info generated by older versions of rustc, we
523 have to rewrite some union types to be struct types with a
524 variant part. This rewriting must be done after the CU is fully
525 read in, because otherwise at the point of rewriting some struct
526 type might not have been fully processed. So, we keep a list of
527 all such types here and process them after expansion. */
528 std::vector<struct type *> rust_unions;
529
530 /* Mark used when releasing cached dies. */
531 unsigned int mark : 1;
532
533 /* This CU references .debug_loc. See the symtab->locations_valid field.
534 This test is imperfect as there may exist optimized debug code not using
535 any location list and still facing inlining issues if handled as
536 unoptimized code. For a future better test see GCC PR other/32998. */
537 unsigned int has_loclist : 1;
538
539 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
540 if all the producer_is_* fields are valid. This information is cached
541 because profiling CU expansion showed excessive time spent in
542 producer_is_gxx_lt_4_6. */
543 unsigned int checked_producer : 1;
544 unsigned int producer_is_gxx_lt_4_6 : 1;
545 unsigned int producer_is_gcc_lt_4_3 : 1;
546 unsigned int producer_is_icc_lt_14 : 1;
547
548 /* When set, the file that we're processing is known to have
549 debugging info for C++ namespaces. GCC 3.3.x did not produce
550 this information, but later versions do. */
551
552 unsigned int processing_has_namespace_info : 1;
553
554 struct partial_die_info *find_partial_die (sect_offset sect_off);
555 };
556
557 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
558 This includes type_unit_group and quick_file_names. */
559
560 struct stmt_list_hash
561 {
562 /* The DWO unit this table is from or NULL if there is none. */
563 struct dwo_unit *dwo_unit;
564
565 /* Offset in .debug_line or .debug_line.dwo. */
566 sect_offset line_sect_off;
567 };
568
569 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
570 an object of this type. */
571
572 struct type_unit_group
573 {
574 /* dwarf2read.c's main "handle" on a TU symtab.
575 To simplify things we create an artificial CU that "includes" all the
576 type units using this stmt_list so that the rest of the code still has
577 a "per_cu" handle on the symtab.
578 This PER_CU is recognized by having no section. */
579 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
580 struct dwarf2_per_cu_data per_cu;
581
582 /* The TUs that share this DW_AT_stmt_list entry.
583 This is added to while parsing type units to build partial symtabs,
584 and is deleted afterwards and not used again. */
585 VEC (sig_type_ptr) *tus;
586
587 /* The compunit symtab.
588 Type units in a group needn't all be defined in the same source file,
589 so we create an essentially anonymous symtab as the compunit symtab. */
590 struct compunit_symtab *compunit_symtab;
591
592 /* The data used to construct the hash key. */
593 struct stmt_list_hash hash;
594
595 /* The number of symtabs from the line header.
596 The value here must match line_header.num_file_names. */
597 unsigned int num_symtabs;
598
599 /* The symbol tables for this TU (obtained from the files listed in
600 DW_AT_stmt_list).
601 WARNING: The order of entries here must match the order of entries
602 in the line header. After the first TU using this type_unit_group, the
603 line header for the subsequent TUs is recreated from this. This is done
604 because we need to use the same symtabs for each TU using the same
605 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
606 there's no guarantee the line header doesn't have duplicate entries. */
607 struct symtab **symtabs;
608 };
609
610 /* These sections are what may appear in a (real or virtual) DWO file. */
611
612 struct dwo_sections
613 {
614 struct dwarf2_section_info abbrev;
615 struct dwarf2_section_info line;
616 struct dwarf2_section_info loc;
617 struct dwarf2_section_info loclists;
618 struct dwarf2_section_info macinfo;
619 struct dwarf2_section_info macro;
620 struct dwarf2_section_info str;
621 struct dwarf2_section_info str_offsets;
622 /* In the case of a virtual DWO file, these two are unused. */
623 struct dwarf2_section_info info;
624 VEC (dwarf2_section_info_def) *types;
625 };
626
627 /* CUs/TUs in DWP/DWO files. */
628
629 struct dwo_unit
630 {
631 /* Backlink to the containing struct dwo_file. */
632 struct dwo_file *dwo_file;
633
634 /* The "id" that distinguishes this CU/TU.
635 .debug_info calls this "dwo_id", .debug_types calls this "signature".
636 Since signatures came first, we stick with it for consistency. */
637 ULONGEST signature;
638
639 /* The section this CU/TU lives in, in the DWO file. */
640 struct dwarf2_section_info *section;
641
642 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
643 sect_offset sect_off;
644 unsigned int length;
645
646 /* For types, offset in the type's DIE of the type defined by this TU. */
647 cu_offset type_offset_in_tu;
648 };
649
650 /* include/dwarf2.h defines the DWP section codes.
651 It defines a max value but it doesn't define a min value, which we
652 use for error checking, so provide one. */
653
654 enum dwp_v2_section_ids
655 {
656 DW_SECT_MIN = 1
657 };
658
659 /* Data for one DWO file.
660
661 This includes virtual DWO files (a virtual DWO file is a DWO file as it
662 appears in a DWP file). DWP files don't really have DWO files per se -
663 comdat folding of types "loses" the DWO file they came from, and from
664 a high level view DWP files appear to contain a mass of random types.
665 However, to maintain consistency with the non-DWP case we pretend DWP
666 files contain virtual DWO files, and we assign each TU with one virtual
667 DWO file (generally based on the line and abbrev section offsets -
668 a heuristic that seems to work in practice). */
669
670 struct dwo_file
671 {
672 /* The DW_AT_GNU_dwo_name attribute.
673 For virtual DWO files the name is constructed from the section offsets
674 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
675 from related CU+TUs. */
676 const char *dwo_name;
677
678 /* The DW_AT_comp_dir attribute. */
679 const char *comp_dir;
680
681 /* The bfd, when the file is open. Otherwise this is NULL.
682 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
683 bfd *dbfd;
684
685 /* The sections that make up this DWO file.
686 Remember that for virtual DWO files in DWP V2, these are virtual
687 sections (for lack of a better name). */
688 struct dwo_sections sections;
689
690 /* The CUs in the file.
691 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
692 an extension to handle LLVM's Link Time Optimization output (where
693 multiple source files may be compiled into a single object/dwo pair). */
694 htab_t cus;
695
696 /* Table of TUs in the file.
697 Each element is a struct dwo_unit. */
698 htab_t tus;
699 };
700
701 /* These sections are what may appear in a DWP file. */
702
703 struct dwp_sections
704 {
705 /* These are used by both DWP version 1 and 2. */
706 struct dwarf2_section_info str;
707 struct dwarf2_section_info cu_index;
708 struct dwarf2_section_info tu_index;
709
710 /* These are only used by DWP version 2 files.
711 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
712 sections are referenced by section number, and are not recorded here.
713 In DWP version 2 there is at most one copy of all these sections, each
714 section being (effectively) comprised of the concatenation of all of the
715 individual sections that exist in the version 1 format.
716 To keep the code simple we treat each of these concatenated pieces as a
717 section itself (a virtual section?). */
718 struct dwarf2_section_info abbrev;
719 struct dwarf2_section_info info;
720 struct dwarf2_section_info line;
721 struct dwarf2_section_info loc;
722 struct dwarf2_section_info macinfo;
723 struct dwarf2_section_info macro;
724 struct dwarf2_section_info str_offsets;
725 struct dwarf2_section_info types;
726 };
727
728 /* These sections are what may appear in a virtual DWO file in DWP version 1.
729 A virtual DWO file is a DWO file as it appears in a DWP file. */
730
731 struct virtual_v1_dwo_sections
732 {
733 struct dwarf2_section_info abbrev;
734 struct dwarf2_section_info line;
735 struct dwarf2_section_info loc;
736 struct dwarf2_section_info macinfo;
737 struct dwarf2_section_info macro;
738 struct dwarf2_section_info str_offsets;
739 /* Each DWP hash table entry records one CU or one TU.
740 That is recorded here, and copied to dwo_unit.section. */
741 struct dwarf2_section_info info_or_types;
742 };
743
744 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
745 In version 2, the sections of the DWO files are concatenated together
746 and stored in one section of that name. Thus each ELF section contains
747 several "virtual" sections. */
748
749 struct virtual_v2_dwo_sections
750 {
751 bfd_size_type abbrev_offset;
752 bfd_size_type abbrev_size;
753
754 bfd_size_type line_offset;
755 bfd_size_type line_size;
756
757 bfd_size_type loc_offset;
758 bfd_size_type loc_size;
759
760 bfd_size_type macinfo_offset;
761 bfd_size_type macinfo_size;
762
763 bfd_size_type macro_offset;
764 bfd_size_type macro_size;
765
766 bfd_size_type str_offsets_offset;
767 bfd_size_type str_offsets_size;
768
769 /* Each DWP hash table entry records one CU or one TU.
770 That is recorded here, and copied to dwo_unit.section. */
771 bfd_size_type info_or_types_offset;
772 bfd_size_type info_or_types_size;
773 };
774
775 /* Contents of DWP hash tables. */
776
777 struct dwp_hash_table
778 {
779 uint32_t version, nr_columns;
780 uint32_t nr_units, nr_slots;
781 const gdb_byte *hash_table, *unit_table;
782 union
783 {
784 struct
785 {
786 const gdb_byte *indices;
787 } v1;
788 struct
789 {
790 /* This is indexed by column number and gives the id of the section
791 in that column. */
792 #define MAX_NR_V2_DWO_SECTIONS \
793 (1 /* .debug_info or .debug_types */ \
794 + 1 /* .debug_abbrev */ \
795 + 1 /* .debug_line */ \
796 + 1 /* .debug_loc */ \
797 + 1 /* .debug_str_offsets */ \
798 + 1 /* .debug_macro or .debug_macinfo */)
799 int section_ids[MAX_NR_V2_DWO_SECTIONS];
800 const gdb_byte *offsets;
801 const gdb_byte *sizes;
802 } v2;
803 } section_pool;
804 };
805
806 /* Data for one DWP file. */
807
808 struct dwp_file
809 {
810 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
811 : name (name_),
812 dbfd (std::move (abfd))
813 {
814 }
815
816 /* Name of the file. */
817 const char *name;
818
819 /* File format version. */
820 int version = 0;
821
822 /* The bfd. */
823 gdb_bfd_ref_ptr dbfd;
824
825 /* Section info for this file. */
826 struct dwp_sections sections {};
827
828 /* Table of CUs in the file. */
829 const struct dwp_hash_table *cus = nullptr;
830
831 /* Table of TUs in the file. */
832 const struct dwp_hash_table *tus = nullptr;
833
834 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
835 htab_t loaded_cus {};
836 htab_t loaded_tus {};
837
838 /* Table to map ELF section numbers to their sections.
839 This is only needed for the DWP V1 file format. */
840 unsigned int num_sections = 0;
841 asection **elf_sections = nullptr;
842 };
843
844 /* This represents a '.dwz' file. */
845
846 struct dwz_file
847 {
848 /* A dwz file can only contain a few sections. */
849 struct dwarf2_section_info abbrev;
850 struct dwarf2_section_info info;
851 struct dwarf2_section_info str;
852 struct dwarf2_section_info line;
853 struct dwarf2_section_info macro;
854 struct dwarf2_section_info gdb_index;
855 struct dwarf2_section_info debug_names;
856
857 /* The dwz's BFD. */
858 bfd *dwz_bfd;
859 };
860
861 /* Struct used to pass misc. parameters to read_die_and_children, et
862 al. which are used for both .debug_info and .debug_types dies.
863 All parameters here are unchanging for the life of the call. This
864 struct exists to abstract away the constant parameters of die reading. */
865
866 struct die_reader_specs
867 {
868 /* The bfd of die_section. */
869 bfd* abfd;
870
871 /* The CU of the DIE we are parsing. */
872 struct dwarf2_cu *cu;
873
874 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
875 struct dwo_file *dwo_file;
876
877 /* The section the die comes from.
878 This is either .debug_info or .debug_types, or the .dwo variants. */
879 struct dwarf2_section_info *die_section;
880
881 /* die_section->buffer. */
882 const gdb_byte *buffer;
883
884 /* The end of the buffer. */
885 const gdb_byte *buffer_end;
886
887 /* The value of the DW_AT_comp_dir attribute. */
888 const char *comp_dir;
889
890 /* The abbreviation table to use when reading the DIEs. */
891 struct abbrev_table *abbrev_table;
892 };
893
894 /* Type of function passed to init_cutu_and_read_dies, et.al. */
895 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
896 const gdb_byte *info_ptr,
897 struct die_info *comp_unit_die,
898 int has_children,
899 void *data);
900
901 /* A 1-based directory index. This is a strong typedef to prevent
902 accidentally using a directory index as a 0-based index into an
903 array/vector. */
904 enum class dir_index : unsigned int {};
905
906 /* Likewise, a 1-based file name index. */
907 enum class file_name_index : unsigned int {};
908
909 struct file_entry
910 {
911 file_entry () = default;
912
913 file_entry (const char *name_, dir_index d_index_,
914 unsigned int mod_time_, unsigned int length_)
915 : name (name_),
916 d_index (d_index_),
917 mod_time (mod_time_),
918 length (length_)
919 {}
920
921 /* Return the include directory at D_INDEX stored in LH. Returns
922 NULL if D_INDEX is out of bounds. */
923 const char *include_dir (const line_header *lh) const;
924
925 /* The file name. Note this is an observing pointer. The memory is
926 owned by debug_line_buffer. */
927 const char *name {};
928
929 /* The directory index (1-based). */
930 dir_index d_index {};
931
932 unsigned int mod_time {};
933
934 unsigned int length {};
935
936 /* True if referenced by the Line Number Program. */
937 bool included_p {};
938
939 /* The associated symbol table, if any. */
940 struct symtab *symtab {};
941 };
942
943 /* The line number information for a compilation unit (found in the
944 .debug_line section) begins with a "statement program header",
945 which contains the following information. */
946 struct line_header
947 {
948 line_header ()
949 : offset_in_dwz {}
950 {}
951
952 /* Add an entry to the include directory table. */
953 void add_include_dir (const char *include_dir);
954
955 /* Add an entry to the file name table. */
956 void add_file_name (const char *name, dir_index d_index,
957 unsigned int mod_time, unsigned int length);
958
959 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
960 is out of bounds. */
961 const char *include_dir_at (dir_index index) const
962 {
963 /* Convert directory index number (1-based) to vector index
964 (0-based). */
965 size_t vec_index = to_underlying (index) - 1;
966
967 if (vec_index >= include_dirs.size ())
968 return NULL;
969 return include_dirs[vec_index];
970 }
971
972 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
973 is out of bounds. */
974 file_entry *file_name_at (file_name_index index)
975 {
976 /* Convert file name index number (1-based) to vector index
977 (0-based). */
978 size_t vec_index = to_underlying (index) - 1;
979
980 if (vec_index >= file_names.size ())
981 return NULL;
982 return &file_names[vec_index];
983 }
984
985 /* Const version of the above. */
986 const file_entry *file_name_at (unsigned int index) const
987 {
988 if (index >= file_names.size ())
989 return NULL;
990 return &file_names[index];
991 }
992
993 /* Offset of line number information in .debug_line section. */
994 sect_offset sect_off {};
995
996 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
997 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
998
999 unsigned int total_length {};
1000 unsigned short version {};
1001 unsigned int header_length {};
1002 unsigned char minimum_instruction_length {};
1003 unsigned char maximum_ops_per_instruction {};
1004 unsigned char default_is_stmt {};
1005 int line_base {};
1006 unsigned char line_range {};
1007 unsigned char opcode_base {};
1008
1009 /* standard_opcode_lengths[i] is the number of operands for the
1010 standard opcode whose value is i. This means that
1011 standard_opcode_lengths[0] is unused, and the last meaningful
1012 element is standard_opcode_lengths[opcode_base - 1]. */
1013 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
1014
1015 /* The include_directories table. Note these are observing
1016 pointers. The memory is owned by debug_line_buffer. */
1017 std::vector<const char *> include_dirs;
1018
1019 /* The file_names table. */
1020 std::vector<file_entry> file_names;
1021
1022 /* The start and end of the statement program following this
1023 header. These point into dwarf2_per_objfile->line_buffer. */
1024 const gdb_byte *statement_program_start {}, *statement_program_end {};
1025 };
1026
1027 typedef std::unique_ptr<line_header> line_header_up;
1028
1029 const char *
1030 file_entry::include_dir (const line_header *lh) const
1031 {
1032 return lh->include_dir_at (d_index);
1033 }
1034
1035 /* When we construct a partial symbol table entry we only
1036 need this much information. */
1037 struct partial_die_info : public allocate_on_obstack
1038 {
1039 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1040
1041 /* Disable assign but still keep copy ctor, which is needed
1042 load_partial_dies. */
1043 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1044
1045 /* Adjust the partial die before generating a symbol for it. This
1046 function may set the is_external flag or change the DIE's
1047 name. */
1048 void fixup (struct dwarf2_cu *cu);
1049
1050 /* Read a minimal amount of information into the minimal die
1051 structure. */
1052 const gdb_byte *read (const struct die_reader_specs *reader,
1053 const struct abbrev_info &abbrev,
1054 const gdb_byte *info_ptr);
1055
1056 /* Offset of this DIE. */
1057 const sect_offset sect_off;
1058
1059 /* DWARF-2 tag for this DIE. */
1060 const ENUM_BITFIELD(dwarf_tag) tag : 16;
1061
1062 /* Assorted flags describing the data found in this DIE. */
1063 const unsigned int has_children : 1;
1064
1065 unsigned int is_external : 1;
1066 unsigned int is_declaration : 1;
1067 unsigned int has_type : 1;
1068 unsigned int has_specification : 1;
1069 unsigned int has_pc_info : 1;
1070 unsigned int may_be_inlined : 1;
1071
1072 /* This DIE has been marked DW_AT_main_subprogram. */
1073 unsigned int main_subprogram : 1;
1074
1075 /* Flag set if the SCOPE field of this structure has been
1076 computed. */
1077 unsigned int scope_set : 1;
1078
1079 /* Flag set if the DIE has a byte_size attribute. */
1080 unsigned int has_byte_size : 1;
1081
1082 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1083 unsigned int has_const_value : 1;
1084
1085 /* Flag set if any of the DIE's children are template arguments. */
1086 unsigned int has_template_arguments : 1;
1087
1088 /* Flag set if fixup has been called on this die. */
1089 unsigned int fixup_called : 1;
1090
1091 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1092 unsigned int is_dwz : 1;
1093
1094 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1095 unsigned int spec_is_dwz : 1;
1096
1097 /* The name of this DIE. Normally the value of DW_AT_name, but
1098 sometimes a default name for unnamed DIEs. */
1099 const char *name = nullptr;
1100
1101 /* The linkage name, if present. */
1102 const char *linkage_name = nullptr;
1103
1104 /* The scope to prepend to our children. This is generally
1105 allocated on the comp_unit_obstack, so will disappear
1106 when this compilation unit leaves the cache. */
1107 const char *scope = nullptr;
1108
1109 /* Some data associated with the partial DIE. The tag determines
1110 which field is live. */
1111 union
1112 {
1113 /* The location description associated with this DIE, if any. */
1114 struct dwarf_block *locdesc;
1115 /* The offset of an import, for DW_TAG_imported_unit. */
1116 sect_offset sect_off;
1117 } d {};
1118
1119 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1120 CORE_ADDR lowpc = 0;
1121 CORE_ADDR highpc = 0;
1122
1123 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1124 DW_AT_sibling, if any. */
1125 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1126 could return DW_AT_sibling values to its caller load_partial_dies. */
1127 const gdb_byte *sibling = nullptr;
1128
1129 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1130 DW_AT_specification (or DW_AT_abstract_origin or
1131 DW_AT_extension). */
1132 sect_offset spec_offset {};
1133
1134 /* Pointers to this DIE's parent, first child, and next sibling,
1135 if any. */
1136 struct partial_die_info *die_parent = nullptr;
1137 struct partial_die_info *die_child = nullptr;
1138 struct partial_die_info *die_sibling = nullptr;
1139
1140 friend struct partial_die_info *
1141 dwarf2_cu::find_partial_die (sect_offset sect_off);
1142
1143 private:
1144 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1145 partial_die_info (sect_offset sect_off)
1146 : partial_die_info (sect_off, DW_TAG_padding, 0)
1147 {
1148 }
1149
1150 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1151 int has_children_)
1152 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1153 {
1154 is_external = 0;
1155 is_declaration = 0;
1156 has_type = 0;
1157 has_specification = 0;
1158 has_pc_info = 0;
1159 may_be_inlined = 0;
1160 main_subprogram = 0;
1161 scope_set = 0;
1162 has_byte_size = 0;
1163 has_const_value = 0;
1164 has_template_arguments = 0;
1165 fixup_called = 0;
1166 is_dwz = 0;
1167 spec_is_dwz = 0;
1168 }
1169 };
1170
1171 /* This data structure holds the information of an abbrev. */
1172 struct abbrev_info
1173 {
1174 unsigned int number; /* number identifying abbrev */
1175 enum dwarf_tag tag; /* dwarf tag */
1176 unsigned short has_children; /* boolean */
1177 unsigned short num_attrs; /* number of attributes */
1178 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1179 struct abbrev_info *next; /* next in chain */
1180 };
1181
1182 struct attr_abbrev
1183 {
1184 ENUM_BITFIELD(dwarf_attribute) name : 16;
1185 ENUM_BITFIELD(dwarf_form) form : 16;
1186
1187 /* It is valid only if FORM is DW_FORM_implicit_const. */
1188 LONGEST implicit_const;
1189 };
1190
1191 /* Size of abbrev_table.abbrev_hash_table. */
1192 #define ABBREV_HASH_SIZE 121
1193
1194 /* Top level data structure to contain an abbreviation table. */
1195
1196 struct abbrev_table
1197 {
1198 explicit abbrev_table (sect_offset off)
1199 : sect_off (off)
1200 {
1201 m_abbrevs =
1202 XOBNEWVEC (&abbrev_obstack, struct abbrev_info *, ABBREV_HASH_SIZE);
1203 memset (m_abbrevs, 0, ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
1204 }
1205
1206 DISABLE_COPY_AND_ASSIGN (abbrev_table);
1207
1208 /* Allocate space for a struct abbrev_info object in
1209 ABBREV_TABLE. */
1210 struct abbrev_info *alloc_abbrev ();
1211
1212 /* Add an abbreviation to the table. */
1213 void add_abbrev (unsigned int abbrev_number, struct abbrev_info *abbrev);
1214
1215 /* Look up an abbrev in the table.
1216 Returns NULL if the abbrev is not found. */
1217
1218 struct abbrev_info *lookup_abbrev (unsigned int abbrev_number);
1219
1220
1221 /* Where the abbrev table came from.
1222 This is used as a sanity check when the table is used. */
1223 const sect_offset sect_off;
1224
1225 /* Storage for the abbrev table. */
1226 auto_obstack abbrev_obstack;
1227
1228 private:
1229
1230 /* Hash table of abbrevs.
1231 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1232 It could be statically allocated, but the previous code didn't so we
1233 don't either. */
1234 struct abbrev_info **m_abbrevs;
1235 };
1236
1237 typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
1238
1239 /* Attributes have a name and a value. */
1240 struct attribute
1241 {
1242 ENUM_BITFIELD(dwarf_attribute) name : 16;
1243 ENUM_BITFIELD(dwarf_form) form : 15;
1244
1245 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1246 field should be in u.str (existing only for DW_STRING) but it is kept
1247 here for better struct attribute alignment. */
1248 unsigned int string_is_canonical : 1;
1249
1250 union
1251 {
1252 const char *str;
1253 struct dwarf_block *blk;
1254 ULONGEST unsnd;
1255 LONGEST snd;
1256 CORE_ADDR addr;
1257 ULONGEST signature;
1258 }
1259 u;
1260 };
1261
1262 /* This data structure holds a complete die structure. */
1263 struct die_info
1264 {
1265 /* DWARF-2 tag for this DIE. */
1266 ENUM_BITFIELD(dwarf_tag) tag : 16;
1267
1268 /* Number of attributes */
1269 unsigned char num_attrs;
1270
1271 /* True if we're presently building the full type name for the
1272 type derived from this DIE. */
1273 unsigned char building_fullname : 1;
1274
1275 /* True if this die is in process. PR 16581. */
1276 unsigned char in_process : 1;
1277
1278 /* Abbrev number */
1279 unsigned int abbrev;
1280
1281 /* Offset in .debug_info or .debug_types section. */
1282 sect_offset sect_off;
1283
1284 /* The dies in a compilation unit form an n-ary tree. PARENT
1285 points to this die's parent; CHILD points to the first child of
1286 this node; and all the children of a given node are chained
1287 together via their SIBLING fields. */
1288 struct die_info *child; /* Its first child, if any. */
1289 struct die_info *sibling; /* Its next sibling, if any. */
1290 struct die_info *parent; /* Its parent, if any. */
1291
1292 /* An array of attributes, with NUM_ATTRS elements. There may be
1293 zero, but it's not common and zero-sized arrays are not
1294 sufficiently portable C. */
1295 struct attribute attrs[1];
1296 };
1297
1298 /* Get at parts of an attribute structure. */
1299
1300 #define DW_STRING(attr) ((attr)->u.str)
1301 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1302 #define DW_UNSND(attr) ((attr)->u.unsnd)
1303 #define DW_BLOCK(attr) ((attr)->u.blk)
1304 #define DW_SND(attr) ((attr)->u.snd)
1305 #define DW_ADDR(attr) ((attr)->u.addr)
1306 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1307
1308 /* Blocks are a bunch of untyped bytes. */
1309 struct dwarf_block
1310 {
1311 size_t size;
1312
1313 /* Valid only if SIZE is not zero. */
1314 const gdb_byte *data;
1315 };
1316
1317 #ifndef ATTR_ALLOC_CHUNK
1318 #define ATTR_ALLOC_CHUNK 4
1319 #endif
1320
1321 /* Allocate fields for structs, unions and enums in this size. */
1322 #ifndef DW_FIELD_ALLOC_CHUNK
1323 #define DW_FIELD_ALLOC_CHUNK 4
1324 #endif
1325
1326 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1327 but this would require a corresponding change in unpack_field_as_long
1328 and friends. */
1329 static int bits_per_byte = 8;
1330
1331 /* When reading a variant or variant part, we track a bit more
1332 information about the field, and store it in an object of this
1333 type. */
1334
1335 struct variant_field
1336 {
1337 /* If we see a DW_TAG_variant, then this will be the discriminant
1338 value. */
1339 ULONGEST discriminant_value;
1340 /* If we see a DW_TAG_variant, then this will be set if this is the
1341 default branch. */
1342 bool default_branch;
1343 /* While reading a DW_TAG_variant_part, this will be set if this
1344 field is the discriminant. */
1345 bool is_discriminant;
1346 };
1347
1348 struct nextfield
1349 {
1350 int accessibility = 0;
1351 int virtuality = 0;
1352 /* Extra information to describe a variant or variant part. */
1353 struct variant_field variant {};
1354 struct field field {};
1355 };
1356
1357 struct fnfieldlist
1358 {
1359 const char *name = nullptr;
1360 std::vector<struct fn_field> fnfields;
1361 };
1362
1363 /* The routines that read and process dies for a C struct or C++ class
1364 pass lists of data member fields and lists of member function fields
1365 in an instance of a field_info structure, as defined below. */
1366 struct field_info
1367 {
1368 /* List of data member and baseclasses fields. */
1369 std::vector<struct nextfield> fields;
1370 std::vector<struct nextfield> baseclasses;
1371
1372 /* Number of fields (including baseclasses). */
1373 int nfields = 0;
1374
1375 /* Set if the accesibility of one of the fields is not public. */
1376 int non_public_fields = 0;
1377
1378 /* Member function fieldlist array, contains name of possibly overloaded
1379 member function, number of overloaded member functions and a pointer
1380 to the head of the member function field chain. */
1381 std::vector<struct fnfieldlist> fnfieldlists;
1382
1383 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1384 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1385 std::vector<struct decl_field> typedef_field_list;
1386
1387 /* Nested types defined by this class and the number of elements in this
1388 list. */
1389 std::vector<struct decl_field> nested_types_list;
1390 };
1391
1392 /* One item on the queue of compilation units to read in full symbols
1393 for. */
1394 struct dwarf2_queue_item
1395 {
1396 struct dwarf2_per_cu_data *per_cu;
1397 enum language pretend_language;
1398 struct dwarf2_queue_item *next;
1399 };
1400
1401 /* The current queue. */
1402 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1403
1404 /* Loaded secondary compilation units are kept in memory until they
1405 have not been referenced for the processing of this many
1406 compilation units. Set this to zero to disable caching. Cache
1407 sizes of up to at least twenty will improve startup time for
1408 typical inter-CU-reference binaries, at an obvious memory cost. */
1409 static int dwarf_max_cache_age = 5;
1410 static void
1411 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1412 struct cmd_list_element *c, const char *value)
1413 {
1414 fprintf_filtered (file, _("The upper bound on the age of cached "
1415 "DWARF compilation units is %s.\n"),
1416 value);
1417 }
1418 \f
1419 /* local function prototypes */
1420
1421 static const char *get_section_name (const struct dwarf2_section_info *);
1422
1423 static const char *get_section_file_name (const struct dwarf2_section_info *);
1424
1425 static void dwarf2_find_base_address (struct die_info *die,
1426 struct dwarf2_cu *cu);
1427
1428 static struct partial_symtab *create_partial_symtab
1429 (struct dwarf2_per_cu_data *per_cu, const char *name);
1430
1431 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1432 const gdb_byte *info_ptr,
1433 struct die_info *type_unit_die,
1434 int has_children, void *data);
1435
1436 static void dwarf2_build_psymtabs_hard
1437 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1438
1439 static void scan_partial_symbols (struct partial_die_info *,
1440 CORE_ADDR *, CORE_ADDR *,
1441 int, struct dwarf2_cu *);
1442
1443 static void add_partial_symbol (struct partial_die_info *,
1444 struct dwarf2_cu *);
1445
1446 static void add_partial_namespace (struct partial_die_info *pdi,
1447 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1448 int set_addrmap, struct dwarf2_cu *cu);
1449
1450 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1451 CORE_ADDR *highpc, int set_addrmap,
1452 struct dwarf2_cu *cu);
1453
1454 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1455 struct dwarf2_cu *cu);
1456
1457 static void add_partial_subprogram (struct partial_die_info *pdi,
1458 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1459 int need_pc, struct dwarf2_cu *cu);
1460
1461 static void dwarf2_read_symtab (struct partial_symtab *,
1462 struct objfile *);
1463
1464 static void psymtab_to_symtab_1 (struct partial_symtab *);
1465
1466 static abbrev_table_up abbrev_table_read_table
1467 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1468 sect_offset);
1469
1470 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1471
1472 static struct partial_die_info *load_partial_dies
1473 (const struct die_reader_specs *, const gdb_byte *, int);
1474
1475 static struct partial_die_info *find_partial_die (sect_offset, int,
1476 struct dwarf2_cu *);
1477
1478 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1479 struct attribute *, struct attr_abbrev *,
1480 const gdb_byte *);
1481
1482 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1483
1484 static int read_1_signed_byte (bfd *, const gdb_byte *);
1485
1486 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1487
1488 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1489
1490 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1491
1492 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1493 unsigned int *);
1494
1495 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1496
1497 static LONGEST read_checked_initial_length_and_offset
1498 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1499 unsigned int *, unsigned int *);
1500
1501 static LONGEST read_offset (bfd *, const gdb_byte *,
1502 const struct comp_unit_head *,
1503 unsigned int *);
1504
1505 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1506
1507 static sect_offset read_abbrev_offset
1508 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1509 struct dwarf2_section_info *, sect_offset);
1510
1511 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1512
1513 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1514
1515 static const char *read_indirect_string
1516 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1517 const struct comp_unit_head *, unsigned int *);
1518
1519 static const char *read_indirect_line_string
1520 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1521 const struct comp_unit_head *, unsigned int *);
1522
1523 static const char *read_indirect_string_at_offset
1524 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1525 LONGEST str_offset);
1526
1527 static const char *read_indirect_string_from_dwz
1528 (struct objfile *objfile, struct dwz_file *, LONGEST);
1529
1530 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1531
1532 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1533 const gdb_byte *,
1534 unsigned int *);
1535
1536 static const char *read_str_index (const struct die_reader_specs *reader,
1537 ULONGEST str_index);
1538
1539 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1540
1541 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1542 struct dwarf2_cu *);
1543
1544 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1545 unsigned int);
1546
1547 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1548 struct dwarf2_cu *cu);
1549
1550 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1551 struct dwarf2_cu *cu);
1552
1553 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1554
1555 static struct die_info *die_specification (struct die_info *die,
1556 struct dwarf2_cu **);
1557
1558 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1559 struct dwarf2_cu *cu);
1560
1561 static void dwarf_decode_lines (struct line_header *, const char *,
1562 struct dwarf2_cu *, struct partial_symtab *,
1563 CORE_ADDR, int decode_mapping);
1564
1565 static void dwarf2_start_subfile (const char *, const char *);
1566
1567 static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1568 const char *, const char *,
1569 CORE_ADDR);
1570
1571 static struct symbol *new_symbol (struct die_info *, struct type *,
1572 struct dwarf2_cu *, struct symbol * = NULL);
1573
1574 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1575 struct dwarf2_cu *);
1576
1577 static void dwarf2_const_value_attr (const struct attribute *attr,
1578 struct type *type,
1579 const char *name,
1580 struct obstack *obstack,
1581 struct dwarf2_cu *cu, LONGEST *value,
1582 const gdb_byte **bytes,
1583 struct dwarf2_locexpr_baton **baton);
1584
1585 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1586
1587 static int need_gnat_info (struct dwarf2_cu *);
1588
1589 static struct type *die_descriptive_type (struct die_info *,
1590 struct dwarf2_cu *);
1591
1592 static void set_descriptive_type (struct type *, struct die_info *,
1593 struct dwarf2_cu *);
1594
1595 static struct type *die_containing_type (struct die_info *,
1596 struct dwarf2_cu *);
1597
1598 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1599 struct dwarf2_cu *);
1600
1601 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1602
1603 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1604
1605 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1606
1607 static char *typename_concat (struct obstack *obs, const char *prefix,
1608 const char *suffix, int physname,
1609 struct dwarf2_cu *cu);
1610
1611 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1612
1613 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1614
1615 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1616
1617 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1618
1619 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1620
1621 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1622
1623 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1624 struct dwarf2_cu *, struct partial_symtab *);
1625
1626 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1627 values. Keep the items ordered with increasing constraints compliance. */
1628 enum pc_bounds_kind
1629 {
1630 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1631 PC_BOUNDS_NOT_PRESENT,
1632
1633 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1634 were present but they do not form a valid range of PC addresses. */
1635 PC_BOUNDS_INVALID,
1636
1637 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1638 PC_BOUNDS_RANGES,
1639
1640 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1641 PC_BOUNDS_HIGH_LOW,
1642 };
1643
1644 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1645 CORE_ADDR *, CORE_ADDR *,
1646 struct dwarf2_cu *,
1647 struct partial_symtab *);
1648
1649 static void get_scope_pc_bounds (struct die_info *,
1650 CORE_ADDR *, CORE_ADDR *,
1651 struct dwarf2_cu *);
1652
1653 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1654 CORE_ADDR, struct dwarf2_cu *);
1655
1656 static void dwarf2_add_field (struct field_info *, struct die_info *,
1657 struct dwarf2_cu *);
1658
1659 static void dwarf2_attach_fields_to_type (struct field_info *,
1660 struct type *, struct dwarf2_cu *);
1661
1662 static void dwarf2_add_member_fn (struct field_info *,
1663 struct die_info *, struct type *,
1664 struct dwarf2_cu *);
1665
1666 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1667 struct type *,
1668 struct dwarf2_cu *);
1669
1670 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1671
1672 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1673
1674 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1675
1676 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1677
1678 static struct using_direct **using_directives (enum language);
1679
1680 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1681
1682 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1683
1684 static struct type *read_module_type (struct die_info *die,
1685 struct dwarf2_cu *cu);
1686
1687 static const char *namespace_name (struct die_info *die,
1688 int *is_anonymous, struct dwarf2_cu *);
1689
1690 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1691
1692 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1693
1694 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1695 struct dwarf2_cu *);
1696
1697 static struct die_info *read_die_and_siblings_1
1698 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1699 struct die_info *);
1700
1701 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1702 const gdb_byte *info_ptr,
1703 const gdb_byte **new_info_ptr,
1704 struct die_info *parent);
1705
1706 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1707 struct die_info **, const gdb_byte *,
1708 int *, int);
1709
1710 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1711 struct die_info **, const gdb_byte *,
1712 int *);
1713
1714 static void process_die (struct die_info *, struct dwarf2_cu *);
1715
1716 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1717 struct obstack *);
1718
1719 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1720
1721 static const char *dwarf2_full_name (const char *name,
1722 struct die_info *die,
1723 struct dwarf2_cu *cu);
1724
1725 static const char *dwarf2_physname (const char *name, struct die_info *die,
1726 struct dwarf2_cu *cu);
1727
1728 static struct die_info *dwarf2_extension (struct die_info *die,
1729 struct dwarf2_cu **);
1730
1731 static const char *dwarf_tag_name (unsigned int);
1732
1733 static const char *dwarf_attr_name (unsigned int);
1734
1735 static const char *dwarf_form_name (unsigned int);
1736
1737 static const char *dwarf_bool_name (unsigned int);
1738
1739 static const char *dwarf_type_encoding_name (unsigned int);
1740
1741 static struct die_info *sibling_die (struct die_info *);
1742
1743 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1744
1745 static void dump_die_for_error (struct die_info *);
1746
1747 static void dump_die_1 (struct ui_file *, int level, int max_level,
1748 struct die_info *);
1749
1750 /*static*/ void dump_die (struct die_info *, int max_level);
1751
1752 static void store_in_ref_table (struct die_info *,
1753 struct dwarf2_cu *);
1754
1755 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1756
1757 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1758
1759 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1760 const struct attribute *,
1761 struct dwarf2_cu **);
1762
1763 static struct die_info *follow_die_ref (struct die_info *,
1764 const struct attribute *,
1765 struct dwarf2_cu **);
1766
1767 static struct die_info *follow_die_sig (struct die_info *,
1768 const struct attribute *,
1769 struct dwarf2_cu **);
1770
1771 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1772 struct dwarf2_cu *);
1773
1774 static struct type *get_DW_AT_signature_type (struct die_info *,
1775 const struct attribute *,
1776 struct dwarf2_cu *);
1777
1778 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1779
1780 static void read_signatured_type (struct signatured_type *);
1781
1782 static int attr_to_dynamic_prop (const struct attribute *attr,
1783 struct die_info *die, struct dwarf2_cu *cu,
1784 struct dynamic_prop *prop);
1785
1786 /* memory allocation interface */
1787
1788 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1789
1790 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1791
1792 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1793
1794 static int attr_form_is_block (const struct attribute *);
1795
1796 static int attr_form_is_section_offset (const struct attribute *);
1797
1798 static int attr_form_is_constant (const struct attribute *);
1799
1800 static int attr_form_is_ref (const struct attribute *);
1801
1802 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1803 struct dwarf2_loclist_baton *baton,
1804 const struct attribute *attr);
1805
1806 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1807 struct symbol *sym,
1808 struct dwarf2_cu *cu,
1809 int is_block);
1810
1811 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1812 const gdb_byte *info_ptr,
1813 struct abbrev_info *abbrev);
1814
1815 static hashval_t partial_die_hash (const void *item);
1816
1817 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1818
1819 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1820 (sect_offset sect_off, unsigned int offset_in_dwz,
1821 struct dwarf2_per_objfile *dwarf2_per_objfile);
1822
1823 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1824 struct die_info *comp_unit_die,
1825 enum language pretend_language);
1826
1827 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1828
1829 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1830
1831 static struct type *set_die_type (struct die_info *, struct type *,
1832 struct dwarf2_cu *);
1833
1834 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1835
1836 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1837
1838 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1839 enum language);
1840
1841 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1842 enum language);
1843
1844 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1845 enum language);
1846
1847 static void dwarf2_add_dependence (struct dwarf2_cu *,
1848 struct dwarf2_per_cu_data *);
1849
1850 static void dwarf2_mark (struct dwarf2_cu *);
1851
1852 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1853
1854 static struct type *get_die_type_at_offset (sect_offset,
1855 struct dwarf2_per_cu_data *);
1856
1857 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1858
1859 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1860 enum language pretend_language);
1861
1862 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1863
1864 /* Class, the destructor of which frees all allocated queue entries. This
1865 will only have work to do if an error was thrown while processing the
1866 dwarf. If no error was thrown then the queue entries should have all
1867 been processed, and freed, as we went along. */
1868
1869 class dwarf2_queue_guard
1870 {
1871 public:
1872 dwarf2_queue_guard () = default;
1873
1874 /* Free any entries remaining on the queue. There should only be
1875 entries left if we hit an error while processing the dwarf. */
1876 ~dwarf2_queue_guard ()
1877 {
1878 struct dwarf2_queue_item *item, *last;
1879
1880 item = dwarf2_queue;
1881 while (item)
1882 {
1883 /* Anything still marked queued is likely to be in an
1884 inconsistent state, so discard it. */
1885 if (item->per_cu->queued)
1886 {
1887 if (item->per_cu->cu != NULL)
1888 free_one_cached_comp_unit (item->per_cu);
1889 item->per_cu->queued = 0;
1890 }
1891
1892 last = item;
1893 item = item->next;
1894 xfree (last);
1895 }
1896
1897 dwarf2_queue = dwarf2_queue_tail = NULL;
1898 }
1899 };
1900
1901 /* The return type of find_file_and_directory. Note, the enclosed
1902 string pointers are only valid while this object is valid. */
1903
1904 struct file_and_directory
1905 {
1906 /* The filename. This is never NULL. */
1907 const char *name;
1908
1909 /* The compilation directory. NULL if not known. If we needed to
1910 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1911 points directly to the DW_AT_comp_dir string attribute owned by
1912 the obstack that owns the DIE. */
1913 const char *comp_dir;
1914
1915 /* If we needed to build a new string for comp_dir, this is what
1916 owns the storage. */
1917 std::string comp_dir_storage;
1918 };
1919
1920 static file_and_directory find_file_and_directory (struct die_info *die,
1921 struct dwarf2_cu *cu);
1922
1923 static char *file_full_name (int file, struct line_header *lh,
1924 const char *comp_dir);
1925
1926 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
1927 enum class rcuh_kind { COMPILE, TYPE };
1928
1929 static const gdb_byte *read_and_check_comp_unit_head
1930 (struct dwarf2_per_objfile* dwarf2_per_objfile,
1931 struct comp_unit_head *header,
1932 struct dwarf2_section_info *section,
1933 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
1934 rcuh_kind section_kind);
1935
1936 static void init_cutu_and_read_dies
1937 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1938 int use_existing_cu, int keep, bool skip_partial,
1939 die_reader_func_ftype *die_reader_func, void *data);
1940
1941 static void init_cutu_and_read_dies_simple
1942 (struct dwarf2_per_cu_data *this_cu,
1943 die_reader_func_ftype *die_reader_func, void *data);
1944
1945 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1946
1947 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1948
1949 static struct dwo_unit *lookup_dwo_unit_in_dwp
1950 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1951 struct dwp_file *dwp_file, const char *comp_dir,
1952 ULONGEST signature, int is_debug_types);
1953
1954 static struct dwp_file *get_dwp_file
1955 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1956
1957 static struct dwo_unit *lookup_dwo_comp_unit
1958 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1959
1960 static struct dwo_unit *lookup_dwo_type_unit
1961 (struct signatured_type *, const char *, const char *);
1962
1963 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1964
1965 static void free_dwo_file (struct dwo_file *);
1966
1967 /* A unique_ptr helper to free a dwo_file. */
1968
1969 struct dwo_file_deleter
1970 {
1971 void operator() (struct dwo_file *df) const
1972 {
1973 free_dwo_file (df);
1974 }
1975 };
1976
1977 /* A unique pointer to a dwo_file. */
1978
1979 typedef std::unique_ptr<struct dwo_file, dwo_file_deleter> dwo_file_up;
1980
1981 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
1982
1983 static void check_producer (struct dwarf2_cu *cu);
1984
1985 static void free_line_header_voidp (void *arg);
1986 \f
1987 /* Various complaints about symbol reading that don't abort the process. */
1988
1989 static void
1990 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1991 {
1992 complaint (&symfile_complaints,
1993 _("statement list doesn't fit in .debug_line section"));
1994 }
1995
1996 static void
1997 dwarf2_debug_line_missing_file_complaint (void)
1998 {
1999 complaint (&symfile_complaints,
2000 _(".debug_line section has line data without a file"));
2001 }
2002
2003 static void
2004 dwarf2_debug_line_missing_end_sequence_complaint (void)
2005 {
2006 complaint (&symfile_complaints,
2007 _(".debug_line section has line "
2008 "program sequence without an end"));
2009 }
2010
2011 static void
2012 dwarf2_complex_location_expr_complaint (void)
2013 {
2014 complaint (&symfile_complaints, _("location expression too complex"));
2015 }
2016
2017 static void
2018 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2019 int arg3)
2020 {
2021 complaint (&symfile_complaints,
2022 _("const value length mismatch for '%s', got %d, expected %d"),
2023 arg1, arg2, arg3);
2024 }
2025
2026 static void
2027 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2028 {
2029 complaint (&symfile_complaints,
2030 _("debug info runs off end of %s section"
2031 " [in module %s]"),
2032 get_section_name (section),
2033 get_section_file_name (section));
2034 }
2035
2036 static void
2037 dwarf2_macro_malformed_definition_complaint (const char *arg1)
2038 {
2039 complaint (&symfile_complaints,
2040 _("macro debug info contains a "
2041 "malformed macro definition:\n`%s'"),
2042 arg1);
2043 }
2044
2045 static void
2046 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2047 {
2048 complaint (&symfile_complaints,
2049 _("invalid attribute class or form for '%s' in '%s'"),
2050 arg1, arg2);
2051 }
2052
2053 /* Hash function for line_header_hash. */
2054
2055 static hashval_t
2056 line_header_hash (const struct line_header *ofs)
2057 {
2058 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
2059 }
2060
2061 /* Hash function for htab_create_alloc_ex for line_header_hash. */
2062
2063 static hashval_t
2064 line_header_hash_voidp (const void *item)
2065 {
2066 const struct line_header *ofs = (const struct line_header *) item;
2067
2068 return line_header_hash (ofs);
2069 }
2070
2071 /* Equality function for line_header_hash. */
2072
2073 static int
2074 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2075 {
2076 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2077 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
2078
2079 return (ofs_lhs->sect_off == ofs_rhs->sect_off
2080 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2081 }
2082
2083 \f
2084
2085 /* Read the given attribute value as an address, taking the attribute's
2086 form into account. */
2087
2088 static CORE_ADDR
2089 attr_value_as_address (struct attribute *attr)
2090 {
2091 CORE_ADDR addr;
2092
2093 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2094 {
2095 /* Aside from a few clearly defined exceptions, attributes that
2096 contain an address must always be in DW_FORM_addr form.
2097 Unfortunately, some compilers happen to be violating this
2098 requirement by encoding addresses using other forms, such
2099 as DW_FORM_data4 for example. For those broken compilers,
2100 we try to do our best, without any guarantee of success,
2101 to interpret the address correctly. It would also be nice
2102 to generate a complaint, but that would require us to maintain
2103 a list of legitimate cases where a non-address form is allowed,
2104 as well as update callers to pass in at least the CU's DWARF
2105 version. This is more overhead than what we're willing to
2106 expand for a pretty rare case. */
2107 addr = DW_UNSND (attr);
2108 }
2109 else
2110 addr = DW_ADDR (attr);
2111
2112 return addr;
2113 }
2114
2115 /* See declaration. */
2116
2117 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2118 const dwarf2_debug_sections *names)
2119 : objfile (objfile_)
2120 {
2121 if (names == NULL)
2122 names = &dwarf2_elf_names;
2123
2124 bfd *obfd = objfile->obfd;
2125
2126 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2127 locate_sections (obfd, sec, *names);
2128 }
2129
2130 static void free_dwo_files (htab_t dwo_files, struct objfile *objfile);
2131
2132 dwarf2_per_objfile::~dwarf2_per_objfile ()
2133 {
2134 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2135 free_cached_comp_units ();
2136
2137 if (quick_file_names_table)
2138 htab_delete (quick_file_names_table);
2139
2140 if (line_header_hash)
2141 htab_delete (line_header_hash);
2142
2143 for (dwarf2_per_cu_data *per_cu : all_comp_units)
2144 VEC_free (dwarf2_per_cu_ptr, per_cu->imported_symtabs);
2145
2146 for (signatured_type *sig_type : all_type_units)
2147 VEC_free (dwarf2_per_cu_ptr, sig_type->per_cu.imported_symtabs);
2148
2149 VEC_free (dwarf2_section_info_def, types);
2150
2151 if (dwo_files != NULL)
2152 free_dwo_files (dwo_files, objfile);
2153
2154 if (dwz_file != NULL && dwz_file->dwz_bfd)
2155 gdb_bfd_unref (dwz_file->dwz_bfd);
2156
2157 /* Everything else should be on the objfile obstack. */
2158 }
2159
2160 /* See declaration. */
2161
2162 void
2163 dwarf2_per_objfile::free_cached_comp_units ()
2164 {
2165 dwarf2_per_cu_data *per_cu = read_in_chain;
2166 dwarf2_per_cu_data **last_chain = &read_in_chain;
2167 while (per_cu != NULL)
2168 {
2169 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2170
2171 delete per_cu->cu;
2172 *last_chain = next_cu;
2173 per_cu = next_cu;
2174 }
2175 }
2176
2177 /* A helper class that calls free_cached_comp_units on
2178 destruction. */
2179
2180 class free_cached_comp_units
2181 {
2182 public:
2183
2184 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2185 : m_per_objfile (per_objfile)
2186 {
2187 }
2188
2189 ~free_cached_comp_units ()
2190 {
2191 m_per_objfile->free_cached_comp_units ();
2192 }
2193
2194 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2195
2196 private:
2197
2198 dwarf2_per_objfile *m_per_objfile;
2199 };
2200
2201 /* Try to locate the sections we need for DWARF 2 debugging
2202 information and return true if we have enough to do something.
2203 NAMES points to the dwarf2 section names, or is NULL if the standard
2204 ELF names are used. */
2205
2206 int
2207 dwarf2_has_info (struct objfile *objfile,
2208 const struct dwarf2_debug_sections *names)
2209 {
2210 if (objfile->flags & OBJF_READNEVER)
2211 return 0;
2212
2213 struct dwarf2_per_objfile *dwarf2_per_objfile
2214 = get_dwarf2_per_objfile (objfile);
2215
2216 if (dwarf2_per_objfile == NULL)
2217 {
2218 /* Initialize per-objfile state. */
2219 dwarf2_per_objfile
2220 = new (&objfile->objfile_obstack) struct dwarf2_per_objfile (objfile,
2221 names);
2222 set_dwarf2_per_objfile (objfile, dwarf2_per_objfile);
2223 }
2224 return (!dwarf2_per_objfile->info.is_virtual
2225 && dwarf2_per_objfile->info.s.section != NULL
2226 && !dwarf2_per_objfile->abbrev.is_virtual
2227 && dwarf2_per_objfile->abbrev.s.section != NULL);
2228 }
2229
2230 /* Return the containing section of virtual section SECTION. */
2231
2232 static struct dwarf2_section_info *
2233 get_containing_section (const struct dwarf2_section_info *section)
2234 {
2235 gdb_assert (section->is_virtual);
2236 return section->s.containing_section;
2237 }
2238
2239 /* Return the bfd owner of SECTION. */
2240
2241 static struct bfd *
2242 get_section_bfd_owner (const struct dwarf2_section_info *section)
2243 {
2244 if (section->is_virtual)
2245 {
2246 section = get_containing_section (section);
2247 gdb_assert (!section->is_virtual);
2248 }
2249 return section->s.section->owner;
2250 }
2251
2252 /* Return the bfd section of SECTION.
2253 Returns NULL if the section is not present. */
2254
2255 static asection *
2256 get_section_bfd_section (const struct dwarf2_section_info *section)
2257 {
2258 if (section->is_virtual)
2259 {
2260 section = get_containing_section (section);
2261 gdb_assert (!section->is_virtual);
2262 }
2263 return section->s.section;
2264 }
2265
2266 /* Return the name of SECTION. */
2267
2268 static const char *
2269 get_section_name (const struct dwarf2_section_info *section)
2270 {
2271 asection *sectp = get_section_bfd_section (section);
2272
2273 gdb_assert (sectp != NULL);
2274 return bfd_section_name (get_section_bfd_owner (section), sectp);
2275 }
2276
2277 /* Return the name of the file SECTION is in. */
2278
2279 static const char *
2280 get_section_file_name (const struct dwarf2_section_info *section)
2281 {
2282 bfd *abfd = get_section_bfd_owner (section);
2283
2284 return bfd_get_filename (abfd);
2285 }
2286
2287 /* Return the id of SECTION.
2288 Returns 0 if SECTION doesn't exist. */
2289
2290 static int
2291 get_section_id (const struct dwarf2_section_info *section)
2292 {
2293 asection *sectp = get_section_bfd_section (section);
2294
2295 if (sectp == NULL)
2296 return 0;
2297 return sectp->id;
2298 }
2299
2300 /* Return the flags of SECTION.
2301 SECTION (or containing section if this is a virtual section) must exist. */
2302
2303 static int
2304 get_section_flags (const struct dwarf2_section_info *section)
2305 {
2306 asection *sectp = get_section_bfd_section (section);
2307
2308 gdb_assert (sectp != NULL);
2309 return bfd_get_section_flags (sectp->owner, sectp);
2310 }
2311
2312 /* When loading sections, we look either for uncompressed section or for
2313 compressed section names. */
2314
2315 static int
2316 section_is_p (const char *section_name,
2317 const struct dwarf2_section_names *names)
2318 {
2319 if (names->normal != NULL
2320 && strcmp (section_name, names->normal) == 0)
2321 return 1;
2322 if (names->compressed != NULL
2323 && strcmp (section_name, names->compressed) == 0)
2324 return 1;
2325 return 0;
2326 }
2327
2328 /* See declaration. */
2329
2330 void
2331 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2332 const dwarf2_debug_sections &names)
2333 {
2334 flagword aflag = bfd_get_section_flags (abfd, sectp);
2335
2336 if ((aflag & SEC_HAS_CONTENTS) == 0)
2337 {
2338 }
2339 else if (section_is_p (sectp->name, &names.info))
2340 {
2341 this->info.s.section = sectp;
2342 this->info.size = bfd_get_section_size (sectp);
2343 }
2344 else if (section_is_p (sectp->name, &names.abbrev))
2345 {
2346 this->abbrev.s.section = sectp;
2347 this->abbrev.size = bfd_get_section_size (sectp);
2348 }
2349 else if (section_is_p (sectp->name, &names.line))
2350 {
2351 this->line.s.section = sectp;
2352 this->line.size = bfd_get_section_size (sectp);
2353 }
2354 else if (section_is_p (sectp->name, &names.loc))
2355 {
2356 this->loc.s.section = sectp;
2357 this->loc.size = bfd_get_section_size (sectp);
2358 }
2359 else if (section_is_p (sectp->name, &names.loclists))
2360 {
2361 this->loclists.s.section = sectp;
2362 this->loclists.size = bfd_get_section_size (sectp);
2363 }
2364 else if (section_is_p (sectp->name, &names.macinfo))
2365 {
2366 this->macinfo.s.section = sectp;
2367 this->macinfo.size = bfd_get_section_size (sectp);
2368 }
2369 else if (section_is_p (sectp->name, &names.macro))
2370 {
2371 this->macro.s.section = sectp;
2372 this->macro.size = bfd_get_section_size (sectp);
2373 }
2374 else if (section_is_p (sectp->name, &names.str))
2375 {
2376 this->str.s.section = sectp;
2377 this->str.size = bfd_get_section_size (sectp);
2378 }
2379 else if (section_is_p (sectp->name, &names.line_str))
2380 {
2381 this->line_str.s.section = sectp;
2382 this->line_str.size = bfd_get_section_size (sectp);
2383 }
2384 else if (section_is_p (sectp->name, &names.addr))
2385 {
2386 this->addr.s.section = sectp;
2387 this->addr.size = bfd_get_section_size (sectp);
2388 }
2389 else if (section_is_p (sectp->name, &names.frame))
2390 {
2391 this->frame.s.section = sectp;
2392 this->frame.size = bfd_get_section_size (sectp);
2393 }
2394 else if (section_is_p (sectp->name, &names.eh_frame))
2395 {
2396 this->eh_frame.s.section = sectp;
2397 this->eh_frame.size = bfd_get_section_size (sectp);
2398 }
2399 else if (section_is_p (sectp->name, &names.ranges))
2400 {
2401 this->ranges.s.section = sectp;
2402 this->ranges.size = bfd_get_section_size (sectp);
2403 }
2404 else if (section_is_p (sectp->name, &names.rnglists))
2405 {
2406 this->rnglists.s.section = sectp;
2407 this->rnglists.size = bfd_get_section_size (sectp);
2408 }
2409 else if (section_is_p (sectp->name, &names.types))
2410 {
2411 struct dwarf2_section_info type_section;
2412
2413 memset (&type_section, 0, sizeof (type_section));
2414 type_section.s.section = sectp;
2415 type_section.size = bfd_get_section_size (sectp);
2416
2417 VEC_safe_push (dwarf2_section_info_def, this->types,
2418 &type_section);
2419 }
2420 else if (section_is_p (sectp->name, &names.gdb_index))
2421 {
2422 this->gdb_index.s.section = sectp;
2423 this->gdb_index.size = bfd_get_section_size (sectp);
2424 }
2425 else if (section_is_p (sectp->name, &names.debug_names))
2426 {
2427 this->debug_names.s.section = sectp;
2428 this->debug_names.size = bfd_get_section_size (sectp);
2429 }
2430 else if (section_is_p (sectp->name, &names.debug_aranges))
2431 {
2432 this->debug_aranges.s.section = sectp;
2433 this->debug_aranges.size = bfd_get_section_size (sectp);
2434 }
2435
2436 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
2437 && bfd_section_vma (abfd, sectp) == 0)
2438 this->has_section_at_zero = true;
2439 }
2440
2441 /* A helper function that decides whether a section is empty,
2442 or not present. */
2443
2444 static int
2445 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2446 {
2447 if (section->is_virtual)
2448 return section->size == 0;
2449 return section->s.section == NULL || section->size == 0;
2450 }
2451
2452 /* See dwarf2read.h. */
2453
2454 void
2455 dwarf2_read_section (struct objfile *objfile, dwarf2_section_info *info)
2456 {
2457 asection *sectp;
2458 bfd *abfd;
2459 gdb_byte *buf, *retbuf;
2460
2461 if (info->readin)
2462 return;
2463 info->buffer = NULL;
2464 info->readin = 1;
2465
2466 if (dwarf2_section_empty_p (info))
2467 return;
2468
2469 sectp = get_section_bfd_section (info);
2470
2471 /* If this is a virtual section we need to read in the real one first. */
2472 if (info->is_virtual)
2473 {
2474 struct dwarf2_section_info *containing_section =
2475 get_containing_section (info);
2476
2477 gdb_assert (sectp != NULL);
2478 if ((sectp->flags & SEC_RELOC) != 0)
2479 {
2480 error (_("Dwarf Error: DWP format V2 with relocations is not"
2481 " supported in section %s [in module %s]"),
2482 get_section_name (info), get_section_file_name (info));
2483 }
2484 dwarf2_read_section (objfile, containing_section);
2485 /* Other code should have already caught virtual sections that don't
2486 fit. */
2487 gdb_assert (info->virtual_offset + info->size
2488 <= containing_section->size);
2489 /* If the real section is empty or there was a problem reading the
2490 section we shouldn't get here. */
2491 gdb_assert (containing_section->buffer != NULL);
2492 info->buffer = containing_section->buffer + info->virtual_offset;
2493 return;
2494 }
2495
2496 /* If the section has relocations, we must read it ourselves.
2497 Otherwise we attach it to the BFD. */
2498 if ((sectp->flags & SEC_RELOC) == 0)
2499 {
2500 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2501 return;
2502 }
2503
2504 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
2505 info->buffer = buf;
2506
2507 /* When debugging .o files, we may need to apply relocations; see
2508 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2509 We never compress sections in .o files, so we only need to
2510 try this when the section is not compressed. */
2511 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2512 if (retbuf != NULL)
2513 {
2514 info->buffer = retbuf;
2515 return;
2516 }
2517
2518 abfd = get_section_bfd_owner (info);
2519 gdb_assert (abfd != NULL);
2520
2521 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2522 || bfd_bread (buf, info->size, abfd) != info->size)
2523 {
2524 error (_("Dwarf Error: Can't read DWARF data"
2525 " in section %s [in module %s]"),
2526 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2527 }
2528 }
2529
2530 /* A helper function that returns the size of a section in a safe way.
2531 If you are positive that the section has been read before using the
2532 size, then it is safe to refer to the dwarf2_section_info object's
2533 "size" field directly. In other cases, you must call this
2534 function, because for compressed sections the size field is not set
2535 correctly until the section has been read. */
2536
2537 static bfd_size_type
2538 dwarf2_section_size (struct objfile *objfile,
2539 struct dwarf2_section_info *info)
2540 {
2541 if (!info->readin)
2542 dwarf2_read_section (objfile, info);
2543 return info->size;
2544 }
2545
2546 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2547 SECTION_NAME. */
2548
2549 void
2550 dwarf2_get_section_info (struct objfile *objfile,
2551 enum dwarf2_section_enum sect,
2552 asection **sectp, const gdb_byte **bufp,
2553 bfd_size_type *sizep)
2554 {
2555 struct dwarf2_per_objfile *data
2556 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2557 dwarf2_objfile_data_key);
2558 struct dwarf2_section_info *info;
2559
2560 /* We may see an objfile without any DWARF, in which case we just
2561 return nothing. */
2562 if (data == NULL)
2563 {
2564 *sectp = NULL;
2565 *bufp = NULL;
2566 *sizep = 0;
2567 return;
2568 }
2569 switch (sect)
2570 {
2571 case DWARF2_DEBUG_FRAME:
2572 info = &data->frame;
2573 break;
2574 case DWARF2_EH_FRAME:
2575 info = &data->eh_frame;
2576 break;
2577 default:
2578 gdb_assert_not_reached ("unexpected section");
2579 }
2580
2581 dwarf2_read_section (objfile, info);
2582
2583 *sectp = get_section_bfd_section (info);
2584 *bufp = info->buffer;
2585 *sizep = info->size;
2586 }
2587
2588 /* A helper function to find the sections for a .dwz file. */
2589
2590 static void
2591 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2592 {
2593 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2594
2595 /* Note that we only support the standard ELF names, because .dwz
2596 is ELF-only (at the time of writing). */
2597 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2598 {
2599 dwz_file->abbrev.s.section = sectp;
2600 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2601 }
2602 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2603 {
2604 dwz_file->info.s.section = sectp;
2605 dwz_file->info.size = bfd_get_section_size (sectp);
2606 }
2607 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2608 {
2609 dwz_file->str.s.section = sectp;
2610 dwz_file->str.size = bfd_get_section_size (sectp);
2611 }
2612 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2613 {
2614 dwz_file->line.s.section = sectp;
2615 dwz_file->line.size = bfd_get_section_size (sectp);
2616 }
2617 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2618 {
2619 dwz_file->macro.s.section = sectp;
2620 dwz_file->macro.size = bfd_get_section_size (sectp);
2621 }
2622 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2623 {
2624 dwz_file->gdb_index.s.section = sectp;
2625 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2626 }
2627 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2628 {
2629 dwz_file->debug_names.s.section = sectp;
2630 dwz_file->debug_names.size = bfd_get_section_size (sectp);
2631 }
2632 }
2633
2634 /* Open the separate '.dwz' debug file, if needed. Return NULL if
2635 there is no .gnu_debugaltlink section in the file. Error if there
2636 is such a section but the file cannot be found. */
2637
2638 static struct dwz_file *
2639 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2640 {
2641 const char *filename;
2642 struct dwz_file *result;
2643 bfd_size_type buildid_len_arg;
2644 size_t buildid_len;
2645 bfd_byte *buildid;
2646
2647 if (dwarf2_per_objfile->dwz_file != NULL)
2648 return dwarf2_per_objfile->dwz_file;
2649
2650 bfd_set_error (bfd_error_no_error);
2651 gdb::unique_xmalloc_ptr<char> data
2652 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2653 &buildid_len_arg, &buildid));
2654 if (data == NULL)
2655 {
2656 if (bfd_get_error () == bfd_error_no_error)
2657 return NULL;
2658 error (_("could not read '.gnu_debugaltlink' section: %s"),
2659 bfd_errmsg (bfd_get_error ()));
2660 }
2661
2662 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2663
2664 buildid_len = (size_t) buildid_len_arg;
2665
2666 filename = data.get ();
2667
2668 std::string abs_storage;
2669 if (!IS_ABSOLUTE_PATH (filename))
2670 {
2671 gdb::unique_xmalloc_ptr<char> abs
2672 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2673
2674 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2675 filename = abs_storage.c_str ();
2676 }
2677
2678 /* First try the file name given in the section. If that doesn't
2679 work, try to use the build-id instead. */
2680 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2681 if (dwz_bfd != NULL)
2682 {
2683 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2684 dwz_bfd.release ();
2685 }
2686
2687 if (dwz_bfd == NULL)
2688 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2689
2690 if (dwz_bfd == NULL)
2691 error (_("could not find '.gnu_debugaltlink' file for %s"),
2692 objfile_name (dwarf2_per_objfile->objfile));
2693
2694 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2695 struct dwz_file);
2696 result->dwz_bfd = dwz_bfd.release ();
2697
2698 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
2699
2700 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
2701 dwarf2_per_objfile->dwz_file = result;
2702 return result;
2703 }
2704 \f
2705 /* DWARF quick_symbols_functions support. */
2706
2707 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2708 unique line tables, so we maintain a separate table of all .debug_line
2709 derived entries to support the sharing.
2710 All the quick functions need is the list of file names. We discard the
2711 line_header when we're done and don't need to record it here. */
2712 struct quick_file_names
2713 {
2714 /* The data used to construct the hash key. */
2715 struct stmt_list_hash hash;
2716
2717 /* The number of entries in file_names, real_names. */
2718 unsigned int num_file_names;
2719
2720 /* The file names from the line table, after being run through
2721 file_full_name. */
2722 const char **file_names;
2723
2724 /* The file names from the line table after being run through
2725 gdb_realpath. These are computed lazily. */
2726 const char **real_names;
2727 };
2728
2729 /* When using the index (and thus not using psymtabs), each CU has an
2730 object of this type. This is used to hold information needed by
2731 the various "quick" methods. */
2732 struct dwarf2_per_cu_quick_data
2733 {
2734 /* The file table. This can be NULL if there was no file table
2735 or it's currently not read in.
2736 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2737 struct quick_file_names *file_names;
2738
2739 /* The corresponding symbol table. This is NULL if symbols for this
2740 CU have not yet been read. */
2741 struct compunit_symtab *compunit_symtab;
2742
2743 /* A temporary mark bit used when iterating over all CUs in
2744 expand_symtabs_matching. */
2745 unsigned int mark : 1;
2746
2747 /* True if we've tried to read the file table and found there isn't one.
2748 There will be no point in trying to read it again next time. */
2749 unsigned int no_file_data : 1;
2750 };
2751
2752 /* Utility hash function for a stmt_list_hash. */
2753
2754 static hashval_t
2755 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2756 {
2757 hashval_t v = 0;
2758
2759 if (stmt_list_hash->dwo_unit != NULL)
2760 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2761 v += to_underlying (stmt_list_hash->line_sect_off);
2762 return v;
2763 }
2764
2765 /* Utility equality function for a stmt_list_hash. */
2766
2767 static int
2768 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2769 const struct stmt_list_hash *rhs)
2770 {
2771 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2772 return 0;
2773 if (lhs->dwo_unit != NULL
2774 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2775 return 0;
2776
2777 return lhs->line_sect_off == rhs->line_sect_off;
2778 }
2779
2780 /* Hash function for a quick_file_names. */
2781
2782 static hashval_t
2783 hash_file_name_entry (const void *e)
2784 {
2785 const struct quick_file_names *file_data
2786 = (const struct quick_file_names *) e;
2787
2788 return hash_stmt_list_entry (&file_data->hash);
2789 }
2790
2791 /* Equality function for a quick_file_names. */
2792
2793 static int
2794 eq_file_name_entry (const void *a, const void *b)
2795 {
2796 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2797 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2798
2799 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2800 }
2801
2802 /* Delete function for a quick_file_names. */
2803
2804 static void
2805 delete_file_name_entry (void *e)
2806 {
2807 struct quick_file_names *file_data = (struct quick_file_names *) e;
2808 int i;
2809
2810 for (i = 0; i < file_data->num_file_names; ++i)
2811 {
2812 xfree ((void*) file_data->file_names[i]);
2813 if (file_data->real_names)
2814 xfree ((void*) file_data->real_names[i]);
2815 }
2816
2817 /* The space for the struct itself lives on objfile_obstack,
2818 so we don't free it here. */
2819 }
2820
2821 /* Create a quick_file_names hash table. */
2822
2823 static htab_t
2824 create_quick_file_names_table (unsigned int nr_initial_entries)
2825 {
2826 return htab_create_alloc (nr_initial_entries,
2827 hash_file_name_entry, eq_file_name_entry,
2828 delete_file_name_entry, xcalloc, xfree);
2829 }
2830
2831 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2832 have to be created afterwards. You should call age_cached_comp_units after
2833 processing PER_CU->CU. dw2_setup must have been already called. */
2834
2835 static void
2836 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2837 {
2838 if (per_cu->is_debug_types)
2839 load_full_type_unit (per_cu);
2840 else
2841 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2842
2843 if (per_cu->cu == NULL)
2844 return; /* Dummy CU. */
2845
2846 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2847 }
2848
2849 /* Read in the symbols for PER_CU. */
2850
2851 static void
2852 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2853 {
2854 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2855
2856 /* Skip type_unit_groups, reading the type units they contain
2857 is handled elsewhere. */
2858 if (IS_TYPE_UNIT_GROUP (per_cu))
2859 return;
2860
2861 /* The destructor of dwarf2_queue_guard frees any entries left on
2862 the queue. After this point we're guaranteed to leave this function
2863 with the dwarf queue empty. */
2864 dwarf2_queue_guard q_guard;
2865
2866 if (dwarf2_per_objfile->using_index
2867 ? per_cu->v.quick->compunit_symtab == NULL
2868 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2869 {
2870 queue_comp_unit (per_cu, language_minimal);
2871 load_cu (per_cu, skip_partial);
2872
2873 /* If we just loaded a CU from a DWO, and we're working with an index
2874 that may badly handle TUs, load all the TUs in that DWO as well.
2875 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2876 if (!per_cu->is_debug_types
2877 && per_cu->cu != NULL
2878 && per_cu->cu->dwo_unit != NULL
2879 && dwarf2_per_objfile->index_table != NULL
2880 && dwarf2_per_objfile->index_table->version <= 7
2881 /* DWP files aren't supported yet. */
2882 && get_dwp_file (dwarf2_per_objfile) == NULL)
2883 queue_and_load_all_dwo_tus (per_cu);
2884 }
2885
2886 process_queue (dwarf2_per_objfile);
2887
2888 /* Age the cache, releasing compilation units that have not
2889 been used recently. */
2890 age_cached_comp_units (dwarf2_per_objfile);
2891 }
2892
2893 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2894 the objfile from which this CU came. Returns the resulting symbol
2895 table. */
2896
2897 static struct compunit_symtab *
2898 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2899 {
2900 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2901
2902 gdb_assert (dwarf2_per_objfile->using_index);
2903 if (!per_cu->v.quick->compunit_symtab)
2904 {
2905 free_cached_comp_units freer (dwarf2_per_objfile);
2906 scoped_restore decrementer = increment_reading_symtab ();
2907 dw2_do_instantiate_symtab (per_cu, skip_partial);
2908 process_cu_includes (dwarf2_per_objfile);
2909 }
2910
2911 return per_cu->v.quick->compunit_symtab;
2912 }
2913
2914 /* See declaration. */
2915
2916 dwarf2_per_cu_data *
2917 dwarf2_per_objfile::get_cutu (int index)
2918 {
2919 if (index >= this->all_comp_units.size ())
2920 {
2921 index -= this->all_comp_units.size ();
2922 gdb_assert (index < this->all_type_units.size ());
2923 return &this->all_type_units[index]->per_cu;
2924 }
2925
2926 return this->all_comp_units[index];
2927 }
2928
2929 /* See declaration. */
2930
2931 dwarf2_per_cu_data *
2932 dwarf2_per_objfile::get_cu (int index)
2933 {
2934 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2935
2936 return this->all_comp_units[index];
2937 }
2938
2939 /* See declaration. */
2940
2941 signatured_type *
2942 dwarf2_per_objfile::get_tu (int index)
2943 {
2944 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2945
2946 return this->all_type_units[index];
2947 }
2948
2949 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2950 objfile_obstack, and constructed with the specified field
2951 values. */
2952
2953 static dwarf2_per_cu_data *
2954 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2955 struct dwarf2_section_info *section,
2956 int is_dwz,
2957 sect_offset sect_off, ULONGEST length)
2958 {
2959 struct objfile *objfile = dwarf2_per_objfile->objfile;
2960 dwarf2_per_cu_data *the_cu
2961 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2962 struct dwarf2_per_cu_data);
2963 the_cu->sect_off = sect_off;
2964 the_cu->length = length;
2965 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2966 the_cu->section = section;
2967 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2968 struct dwarf2_per_cu_quick_data);
2969 the_cu->is_dwz = is_dwz;
2970 return the_cu;
2971 }
2972
2973 /* A helper for create_cus_from_index that handles a given list of
2974 CUs. */
2975
2976 static void
2977 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2978 const gdb_byte *cu_list, offset_type n_elements,
2979 struct dwarf2_section_info *section,
2980 int is_dwz)
2981 {
2982 for (offset_type i = 0; i < n_elements; i += 2)
2983 {
2984 gdb_static_assert (sizeof (ULONGEST) >= 8);
2985
2986 sect_offset sect_off
2987 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2988 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2989 cu_list += 2 * 8;
2990
2991 dwarf2_per_cu_data *per_cu
2992 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2993 sect_off, length);
2994 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
2995 }
2996 }
2997
2998 /* Read the CU list from the mapped index, and use it to create all
2999 the CU objects for this objfile. */
3000
3001 static void
3002 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3003 const gdb_byte *cu_list, offset_type cu_list_elements,
3004 const gdb_byte *dwz_list, offset_type dwz_elements)
3005 {
3006 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
3007 dwarf2_per_objfile->all_comp_units.reserve
3008 ((cu_list_elements + dwz_elements) / 2);
3009
3010 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
3011 &dwarf2_per_objfile->info, 0);
3012
3013 if (dwz_elements == 0)
3014 return;
3015
3016 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3017 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
3018 &dwz->info, 1);
3019 }
3020
3021 /* Create the signatured type hash table from the index. */
3022
3023 static void
3024 create_signatured_type_table_from_index
3025 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3026 struct dwarf2_section_info *section,
3027 const gdb_byte *bytes,
3028 offset_type elements)
3029 {
3030 struct objfile *objfile = dwarf2_per_objfile->objfile;
3031
3032 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3033 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
3034
3035 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3036
3037 for (offset_type i = 0; i < elements; i += 3)
3038 {
3039 struct signatured_type *sig_type;
3040 ULONGEST signature;
3041 void **slot;
3042 cu_offset type_offset_in_tu;
3043
3044 gdb_static_assert (sizeof (ULONGEST) >= 8);
3045 sect_offset sect_off
3046 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3047 type_offset_in_tu
3048 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3049 BFD_ENDIAN_LITTLE);
3050 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3051 bytes += 3 * 8;
3052
3053 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3054 struct signatured_type);
3055 sig_type->signature = signature;
3056 sig_type->type_offset_in_tu = type_offset_in_tu;
3057 sig_type->per_cu.is_debug_types = 1;
3058 sig_type->per_cu.section = section;
3059 sig_type->per_cu.sect_off = sect_off;
3060 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3061 sig_type->per_cu.v.quick
3062 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3063 struct dwarf2_per_cu_quick_data);
3064
3065 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3066 *slot = sig_type;
3067
3068 dwarf2_per_objfile->all_type_units.push_back (sig_type);
3069 }
3070
3071 dwarf2_per_objfile->signatured_types = sig_types_hash;
3072 }
3073
3074 /* Create the signatured type hash table from .debug_names. */
3075
3076 static void
3077 create_signatured_type_table_from_debug_names
3078 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3079 const mapped_debug_names &map,
3080 struct dwarf2_section_info *section,
3081 struct dwarf2_section_info *abbrev_section)
3082 {
3083 struct objfile *objfile = dwarf2_per_objfile->objfile;
3084
3085 dwarf2_read_section (objfile, section);
3086 dwarf2_read_section (objfile, abbrev_section);
3087
3088 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3089 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
3090
3091 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3092
3093 for (uint32_t i = 0; i < map.tu_count; ++i)
3094 {
3095 struct signatured_type *sig_type;
3096 void **slot;
3097
3098 sect_offset sect_off
3099 = (sect_offset) (extract_unsigned_integer
3100 (map.tu_table_reordered + i * map.offset_size,
3101 map.offset_size,
3102 map.dwarf5_byte_order));
3103
3104 comp_unit_head cu_header;
3105 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3106 abbrev_section,
3107 section->buffer + to_underlying (sect_off),
3108 rcuh_kind::TYPE);
3109
3110 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3111 struct signatured_type);
3112 sig_type->signature = cu_header.signature;
3113 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3114 sig_type->per_cu.is_debug_types = 1;
3115 sig_type->per_cu.section = section;
3116 sig_type->per_cu.sect_off = sect_off;
3117 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3118 sig_type->per_cu.v.quick
3119 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3120 struct dwarf2_per_cu_quick_data);
3121
3122 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3123 *slot = sig_type;
3124
3125 dwarf2_per_objfile->all_type_units.push_back (sig_type);
3126 }
3127
3128 dwarf2_per_objfile->signatured_types = sig_types_hash;
3129 }
3130
3131 /* Read the address map data from the mapped index, and use it to
3132 populate the objfile's psymtabs_addrmap. */
3133
3134 static void
3135 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3136 struct mapped_index *index)
3137 {
3138 struct objfile *objfile = dwarf2_per_objfile->objfile;
3139 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3140 const gdb_byte *iter, *end;
3141 struct addrmap *mutable_map;
3142 CORE_ADDR baseaddr;
3143
3144 auto_obstack temp_obstack;
3145
3146 mutable_map = addrmap_create_mutable (&temp_obstack);
3147
3148 iter = index->address_table.data ();
3149 end = iter + index->address_table.size ();
3150
3151 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3152
3153 while (iter < end)
3154 {
3155 ULONGEST hi, lo, cu_index;
3156 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3157 iter += 8;
3158 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3159 iter += 8;
3160 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3161 iter += 4;
3162
3163 if (lo > hi)
3164 {
3165 complaint (&symfile_complaints,
3166 _(".gdb_index address table has invalid range (%s - %s)"),
3167 hex_string (lo), hex_string (hi));
3168 continue;
3169 }
3170
3171 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
3172 {
3173 complaint (&symfile_complaints,
3174 _(".gdb_index address table has invalid CU number %u"),
3175 (unsigned) cu_index);
3176 continue;
3177 }
3178
3179 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3180 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
3181 addrmap_set_empty (mutable_map, lo, hi - 1,
3182 dwarf2_per_objfile->get_cu (cu_index));
3183 }
3184
3185 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3186 &objfile->objfile_obstack);
3187 }
3188
3189 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
3190 populate the objfile's psymtabs_addrmap. */
3191
3192 static void
3193 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
3194 struct dwarf2_section_info *section)
3195 {
3196 struct objfile *objfile = dwarf2_per_objfile->objfile;
3197 bfd *abfd = objfile->obfd;
3198 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3199 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3200 SECT_OFF_TEXT (objfile));
3201
3202 auto_obstack temp_obstack;
3203 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3204
3205 std::unordered_map<sect_offset,
3206 dwarf2_per_cu_data *,
3207 gdb::hash_enum<sect_offset>>
3208 debug_info_offset_to_per_cu;
3209 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3210 {
3211 const auto insertpair
3212 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3213 if (!insertpair.second)
3214 {
3215 warning (_("Section .debug_aranges in %s has duplicate "
3216 "debug_info_offset %s, ignoring .debug_aranges."),
3217 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
3218 return;
3219 }
3220 }
3221
3222 dwarf2_read_section (objfile, section);
3223
3224 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3225
3226 const gdb_byte *addr = section->buffer;
3227
3228 while (addr < section->buffer + section->size)
3229 {
3230 const gdb_byte *const entry_addr = addr;
3231 unsigned int bytes_read;
3232
3233 const LONGEST entry_length = read_initial_length (abfd, addr,
3234 &bytes_read);
3235 addr += bytes_read;
3236
3237 const gdb_byte *const entry_end = addr + entry_length;
3238 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3239 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3240 if (addr + entry_length > section->buffer + section->size)
3241 {
3242 warning (_("Section .debug_aranges in %s entry at offset %zu "
3243 "length %s exceeds section length %s, "
3244 "ignoring .debug_aranges."),
3245 objfile_name (objfile), entry_addr - section->buffer,
3246 plongest (bytes_read + entry_length),
3247 pulongest (section->size));
3248 return;
3249 }
3250
3251 /* The version number. */
3252 const uint16_t version = read_2_bytes (abfd, addr);
3253 addr += 2;
3254 if (version != 2)
3255 {
3256 warning (_("Section .debug_aranges in %s entry at offset %zu "
3257 "has unsupported version %d, ignoring .debug_aranges."),
3258 objfile_name (objfile), entry_addr - section->buffer,
3259 version);
3260 return;
3261 }
3262
3263 const uint64_t debug_info_offset
3264 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3265 addr += offset_size;
3266 const auto per_cu_it
3267 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3268 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3269 {
3270 warning (_("Section .debug_aranges in %s entry at offset %zu "
3271 "debug_info_offset %s does not exists, "
3272 "ignoring .debug_aranges."),
3273 objfile_name (objfile), entry_addr - section->buffer,
3274 pulongest (debug_info_offset));
3275 return;
3276 }
3277 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3278
3279 const uint8_t address_size = *addr++;
3280 if (address_size < 1 || address_size > 8)
3281 {
3282 warning (_("Section .debug_aranges in %s entry at offset %zu "
3283 "address_size %u is invalid, ignoring .debug_aranges."),
3284 objfile_name (objfile), entry_addr - section->buffer,
3285 address_size);
3286 return;
3287 }
3288
3289 const uint8_t segment_selector_size = *addr++;
3290 if (segment_selector_size != 0)
3291 {
3292 warning (_("Section .debug_aranges in %s entry at offset %zu "
3293 "segment_selector_size %u is not supported, "
3294 "ignoring .debug_aranges."),
3295 objfile_name (objfile), entry_addr - section->buffer,
3296 segment_selector_size);
3297 return;
3298 }
3299
3300 /* Must pad to an alignment boundary that is twice the address
3301 size. It is undocumented by the DWARF standard but GCC does
3302 use it. */
3303 for (size_t padding = ((-(addr - section->buffer))
3304 & (2 * address_size - 1));
3305 padding > 0; padding--)
3306 if (*addr++ != 0)
3307 {
3308 warning (_("Section .debug_aranges in %s entry at offset %zu "
3309 "padding is not zero, ignoring .debug_aranges."),
3310 objfile_name (objfile), entry_addr - section->buffer);
3311 return;
3312 }
3313
3314 for (;;)
3315 {
3316 if (addr + 2 * address_size > entry_end)
3317 {
3318 warning (_("Section .debug_aranges in %s entry at offset %zu "
3319 "address list is not properly terminated, "
3320 "ignoring .debug_aranges."),
3321 objfile_name (objfile), entry_addr - section->buffer);
3322 return;
3323 }
3324 ULONGEST start = extract_unsigned_integer (addr, address_size,
3325 dwarf5_byte_order);
3326 addr += address_size;
3327 ULONGEST length = extract_unsigned_integer (addr, address_size,
3328 dwarf5_byte_order);
3329 addr += address_size;
3330 if (start == 0 && length == 0)
3331 break;
3332 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3333 {
3334 /* Symbol was eliminated due to a COMDAT group. */
3335 continue;
3336 }
3337 ULONGEST end = start + length;
3338 start = gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr);
3339 end = gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr);
3340 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3341 }
3342 }
3343
3344 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3345 &objfile->objfile_obstack);
3346 }
3347
3348 /* Find a slot in the mapped index INDEX for the object named NAME.
3349 If NAME is found, set *VEC_OUT to point to the CU vector in the
3350 constant pool and return true. If NAME cannot be found, return
3351 false. */
3352
3353 static bool
3354 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3355 offset_type **vec_out)
3356 {
3357 offset_type hash;
3358 offset_type slot, step;
3359 int (*cmp) (const char *, const char *);
3360
3361 gdb::unique_xmalloc_ptr<char> without_params;
3362 if (current_language->la_language == language_cplus
3363 || current_language->la_language == language_fortran
3364 || current_language->la_language == language_d)
3365 {
3366 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3367 not contain any. */
3368
3369 if (strchr (name, '(') != NULL)
3370 {
3371 without_params = cp_remove_params (name);
3372
3373 if (without_params != NULL)
3374 name = without_params.get ();
3375 }
3376 }
3377
3378 /* Index version 4 did not support case insensitive searches. But the
3379 indices for case insensitive languages are built in lowercase, therefore
3380 simulate our NAME being searched is also lowercased. */
3381 hash = mapped_index_string_hash ((index->version == 4
3382 && case_sensitivity == case_sensitive_off
3383 ? 5 : index->version),
3384 name);
3385
3386 slot = hash & (index->symbol_table.size () - 1);
3387 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
3388 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3389
3390 for (;;)
3391 {
3392 const char *str;
3393
3394 const auto &bucket = index->symbol_table[slot];
3395 if (bucket.name == 0 && bucket.vec == 0)
3396 return false;
3397
3398 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3399 if (!cmp (name, str))
3400 {
3401 *vec_out = (offset_type *) (index->constant_pool
3402 + MAYBE_SWAP (bucket.vec));
3403 return true;
3404 }
3405
3406 slot = (slot + step) & (index->symbol_table.size () - 1);
3407 }
3408 }
3409
3410 /* A helper function that reads the .gdb_index from SECTION and fills
3411 in MAP. FILENAME is the name of the file containing the section;
3412 it is used for error reporting. DEPRECATED_OK is true if it is
3413 ok to use deprecated sections.
3414
3415 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3416 out parameters that are filled in with information about the CU and
3417 TU lists in the section.
3418
3419 Returns 1 if all went well, 0 otherwise. */
3420
3421 static bool
3422 read_index_from_section (struct objfile *objfile,
3423 const char *filename,
3424 bool deprecated_ok,
3425 struct dwarf2_section_info *section,
3426 struct mapped_index *map,
3427 const gdb_byte **cu_list,
3428 offset_type *cu_list_elements,
3429 const gdb_byte **types_list,
3430 offset_type *types_list_elements)
3431 {
3432 const gdb_byte *addr;
3433 offset_type version;
3434 offset_type *metadata;
3435 int i;
3436
3437 if (dwarf2_section_empty_p (section))
3438 return 0;
3439
3440 /* Older elfutils strip versions could keep the section in the main
3441 executable while splitting it for the separate debug info file. */
3442 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
3443 return 0;
3444
3445 dwarf2_read_section (objfile, section);
3446
3447 addr = section->buffer;
3448 /* Version check. */
3449 version = MAYBE_SWAP (*(offset_type *) addr);
3450 /* Versions earlier than 3 emitted every copy of a psymbol. This
3451 causes the index to behave very poorly for certain requests. Version 3
3452 contained incomplete addrmap. So, it seems better to just ignore such
3453 indices. */
3454 if (version < 4)
3455 {
3456 static int warning_printed = 0;
3457 if (!warning_printed)
3458 {
3459 warning (_("Skipping obsolete .gdb_index section in %s."),
3460 filename);
3461 warning_printed = 1;
3462 }
3463 return 0;
3464 }
3465 /* Index version 4 uses a different hash function than index version
3466 5 and later.
3467
3468 Versions earlier than 6 did not emit psymbols for inlined
3469 functions. Using these files will cause GDB not to be able to
3470 set breakpoints on inlined functions by name, so we ignore these
3471 indices unless the user has done
3472 "set use-deprecated-index-sections on". */
3473 if (version < 6 && !deprecated_ok)
3474 {
3475 static int warning_printed = 0;
3476 if (!warning_printed)
3477 {
3478 warning (_("\
3479 Skipping deprecated .gdb_index section in %s.\n\
3480 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3481 to use the section anyway."),
3482 filename);
3483 warning_printed = 1;
3484 }
3485 return 0;
3486 }
3487 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3488 of the TU (for symbols coming from TUs),
3489 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3490 Plus gold-generated indices can have duplicate entries for global symbols,
3491 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3492 These are just performance bugs, and we can't distinguish gdb-generated
3493 indices from gold-generated ones, so issue no warning here. */
3494
3495 /* Indexes with higher version than the one supported by GDB may be no
3496 longer backward compatible. */
3497 if (version > 8)
3498 return 0;
3499
3500 map->version = version;
3501
3502 metadata = (offset_type *) (addr + sizeof (offset_type));
3503
3504 i = 0;
3505 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3506 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3507 / 8);
3508 ++i;
3509
3510 *types_list = addr + MAYBE_SWAP (metadata[i]);
3511 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3512 - MAYBE_SWAP (metadata[i]))
3513 / 8);
3514 ++i;
3515
3516 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3517 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3518 map->address_table
3519 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3520 ++i;
3521
3522 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3523 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3524 map->symbol_table
3525 = gdb::array_view<mapped_index::symbol_table_slot>
3526 ((mapped_index::symbol_table_slot *) symbol_table,
3527 (mapped_index::symbol_table_slot *) symbol_table_end);
3528
3529 ++i;
3530 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3531
3532 return 1;
3533 }
3534
3535 /* Read .gdb_index. If everything went ok, initialize the "quick"
3536 elements of all the CUs and return 1. Otherwise, return 0. */
3537
3538 static int
3539 dwarf2_read_index (struct dwarf2_per_objfile *dwarf2_per_objfile)
3540 {
3541 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3542 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3543 struct dwz_file *dwz;
3544 struct objfile *objfile = dwarf2_per_objfile->objfile;
3545
3546 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3547 if (!read_index_from_section (objfile, objfile_name (objfile),
3548 use_deprecated_index_sections,
3549 &dwarf2_per_objfile->gdb_index, map.get (),
3550 &cu_list, &cu_list_elements,
3551 &types_list, &types_list_elements))
3552 return 0;
3553
3554 /* Don't use the index if it's empty. */
3555 if (map->symbol_table.empty ())
3556 return 0;
3557
3558 /* If there is a .dwz file, read it so we can get its CU list as
3559 well. */
3560 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3561 if (dwz != NULL)
3562 {
3563 struct mapped_index dwz_map;
3564 const gdb_byte *dwz_types_ignore;
3565 offset_type dwz_types_elements_ignore;
3566
3567 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3568 1,
3569 &dwz->gdb_index, &dwz_map,
3570 &dwz_list, &dwz_list_elements,
3571 &dwz_types_ignore,
3572 &dwz_types_elements_ignore))
3573 {
3574 warning (_("could not read '.gdb_index' section from %s; skipping"),
3575 bfd_get_filename (dwz->dwz_bfd));
3576 return 0;
3577 }
3578 }
3579
3580 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3581 dwz_list, dwz_list_elements);
3582
3583 if (types_list_elements)
3584 {
3585 struct dwarf2_section_info *section;
3586
3587 /* We can only handle a single .debug_types when we have an
3588 index. */
3589 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3590 return 0;
3591
3592 section = VEC_index (dwarf2_section_info_def,
3593 dwarf2_per_objfile->types, 0);
3594
3595 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3596 types_list, types_list_elements);
3597 }
3598
3599 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3600
3601 dwarf2_per_objfile->index_table = std::move (map);
3602 dwarf2_per_objfile->using_index = 1;
3603 dwarf2_per_objfile->quick_file_names_table =
3604 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
3605
3606 return 1;
3607 }
3608
3609 /* die_reader_func for dw2_get_file_names. */
3610
3611 static void
3612 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3613 const gdb_byte *info_ptr,
3614 struct die_info *comp_unit_die,
3615 int has_children,
3616 void *data)
3617 {
3618 struct dwarf2_cu *cu = reader->cu;
3619 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3620 struct dwarf2_per_objfile *dwarf2_per_objfile
3621 = cu->per_cu->dwarf2_per_objfile;
3622 struct objfile *objfile = dwarf2_per_objfile->objfile;
3623 struct dwarf2_per_cu_data *lh_cu;
3624 struct attribute *attr;
3625 int i;
3626 void **slot;
3627 struct quick_file_names *qfn;
3628
3629 gdb_assert (! this_cu->is_debug_types);
3630
3631 /* Our callers never want to match partial units -- instead they
3632 will match the enclosing full CU. */
3633 if (comp_unit_die->tag == DW_TAG_partial_unit)
3634 {
3635 this_cu->v.quick->no_file_data = 1;
3636 return;
3637 }
3638
3639 lh_cu = this_cu;
3640 slot = NULL;
3641
3642 line_header_up lh;
3643 sect_offset line_offset {};
3644
3645 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3646 if (attr)
3647 {
3648 struct quick_file_names find_entry;
3649
3650 line_offset = (sect_offset) DW_UNSND (attr);
3651
3652 /* We may have already read in this line header (TU line header sharing).
3653 If we have we're done. */
3654 find_entry.hash.dwo_unit = cu->dwo_unit;
3655 find_entry.hash.line_sect_off = line_offset;
3656 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3657 &find_entry, INSERT);
3658 if (*slot != NULL)
3659 {
3660 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3661 return;
3662 }
3663
3664 lh = dwarf_decode_line_header (line_offset, cu);
3665 }
3666 if (lh == NULL)
3667 {
3668 lh_cu->v.quick->no_file_data = 1;
3669 return;
3670 }
3671
3672 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3673 qfn->hash.dwo_unit = cu->dwo_unit;
3674 qfn->hash.line_sect_off = line_offset;
3675 gdb_assert (slot != NULL);
3676 *slot = qfn;
3677
3678 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3679
3680 qfn->num_file_names = lh->file_names.size ();
3681 qfn->file_names =
3682 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3683 for (i = 0; i < lh->file_names.size (); ++i)
3684 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
3685 qfn->real_names = NULL;
3686
3687 lh_cu->v.quick->file_names = qfn;
3688 }
3689
3690 /* A helper for the "quick" functions which attempts to read the line
3691 table for THIS_CU. */
3692
3693 static struct quick_file_names *
3694 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3695 {
3696 /* This should never be called for TUs. */
3697 gdb_assert (! this_cu->is_debug_types);
3698 /* Nor type unit groups. */
3699 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3700
3701 if (this_cu->v.quick->file_names != NULL)
3702 return this_cu->v.quick->file_names;
3703 /* If we know there is no line data, no point in looking again. */
3704 if (this_cu->v.quick->no_file_data)
3705 return NULL;
3706
3707 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
3708
3709 if (this_cu->v.quick->no_file_data)
3710 return NULL;
3711 return this_cu->v.quick->file_names;
3712 }
3713
3714 /* A helper for the "quick" functions which computes and caches the
3715 real path for a given file name from the line table. */
3716
3717 static const char *
3718 dw2_get_real_path (struct objfile *objfile,
3719 struct quick_file_names *qfn, int index)
3720 {
3721 if (qfn->real_names == NULL)
3722 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3723 qfn->num_file_names, const char *);
3724
3725 if (qfn->real_names[index] == NULL)
3726 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3727
3728 return qfn->real_names[index];
3729 }
3730
3731 static struct symtab *
3732 dw2_find_last_source_symtab (struct objfile *objfile)
3733 {
3734 struct dwarf2_per_objfile *dwarf2_per_objfile
3735 = get_dwarf2_per_objfile (objfile);
3736 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
3737 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3738
3739 if (cust == NULL)
3740 return NULL;
3741
3742 return compunit_primary_filetab (cust);
3743 }
3744
3745 /* Traversal function for dw2_forget_cached_source_info. */
3746
3747 static int
3748 dw2_free_cached_file_names (void **slot, void *info)
3749 {
3750 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3751
3752 if (file_data->real_names)
3753 {
3754 int i;
3755
3756 for (i = 0; i < file_data->num_file_names; ++i)
3757 {
3758 xfree ((void*) file_data->real_names[i]);
3759 file_data->real_names[i] = NULL;
3760 }
3761 }
3762
3763 return 1;
3764 }
3765
3766 static void
3767 dw2_forget_cached_source_info (struct objfile *objfile)
3768 {
3769 struct dwarf2_per_objfile *dwarf2_per_objfile
3770 = get_dwarf2_per_objfile (objfile);
3771
3772 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3773 dw2_free_cached_file_names, NULL);
3774 }
3775
3776 /* Helper function for dw2_map_symtabs_matching_filename that expands
3777 the symtabs and calls the iterator. */
3778
3779 static int
3780 dw2_map_expand_apply (struct objfile *objfile,
3781 struct dwarf2_per_cu_data *per_cu,
3782 const char *name, const char *real_path,
3783 gdb::function_view<bool (symtab *)> callback)
3784 {
3785 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3786
3787 /* Don't visit already-expanded CUs. */
3788 if (per_cu->v.quick->compunit_symtab)
3789 return 0;
3790
3791 /* This may expand more than one symtab, and we want to iterate over
3792 all of them. */
3793 dw2_instantiate_symtab (per_cu, false);
3794
3795 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3796 last_made, callback);
3797 }
3798
3799 /* Implementation of the map_symtabs_matching_filename method. */
3800
3801 static bool
3802 dw2_map_symtabs_matching_filename
3803 (struct objfile *objfile, const char *name, const char *real_path,
3804 gdb::function_view<bool (symtab *)> callback)
3805 {
3806 const char *name_basename = lbasename (name);
3807 struct dwarf2_per_objfile *dwarf2_per_objfile
3808 = get_dwarf2_per_objfile (objfile);
3809
3810 /* The rule is CUs specify all the files, including those used by
3811 any TU, so there's no need to scan TUs here. */
3812
3813 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3814 {
3815 /* We only need to look at symtabs not already expanded. */
3816 if (per_cu->v.quick->compunit_symtab)
3817 continue;
3818
3819 quick_file_names *file_data = dw2_get_file_names (per_cu);
3820 if (file_data == NULL)
3821 continue;
3822
3823 for (int j = 0; j < file_data->num_file_names; ++j)
3824 {
3825 const char *this_name = file_data->file_names[j];
3826 const char *this_real_name;
3827
3828 if (compare_filenames_for_search (this_name, name))
3829 {
3830 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3831 callback))
3832 return true;
3833 continue;
3834 }
3835
3836 /* Before we invoke realpath, which can get expensive when many
3837 files are involved, do a quick comparison of the basenames. */
3838 if (! basenames_may_differ
3839 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3840 continue;
3841
3842 this_real_name = dw2_get_real_path (objfile, file_data, j);
3843 if (compare_filenames_for_search (this_real_name, name))
3844 {
3845 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3846 callback))
3847 return true;
3848 continue;
3849 }
3850
3851 if (real_path != NULL)
3852 {
3853 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3854 gdb_assert (IS_ABSOLUTE_PATH (name));
3855 if (this_real_name != NULL
3856 && FILENAME_CMP (real_path, this_real_name) == 0)
3857 {
3858 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3859 callback))
3860 return true;
3861 continue;
3862 }
3863 }
3864 }
3865 }
3866
3867 return false;
3868 }
3869
3870 /* Struct used to manage iterating over all CUs looking for a symbol. */
3871
3872 struct dw2_symtab_iterator
3873 {
3874 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3875 struct dwarf2_per_objfile *dwarf2_per_objfile;
3876 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3877 int want_specific_block;
3878 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3879 Unused if !WANT_SPECIFIC_BLOCK. */
3880 int block_index;
3881 /* The kind of symbol we're looking for. */
3882 domain_enum domain;
3883 /* The list of CUs from the index entry of the symbol,
3884 or NULL if not found. */
3885 offset_type *vec;
3886 /* The next element in VEC to look at. */
3887 int next;
3888 /* The number of elements in VEC, or zero if there is no match. */
3889 int length;
3890 /* Have we seen a global version of the symbol?
3891 If so we can ignore all further global instances.
3892 This is to work around gold/15646, inefficient gold-generated
3893 indices. */
3894 int global_seen;
3895 };
3896
3897 /* Initialize the index symtab iterator ITER.
3898 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3899 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
3900
3901 static void
3902 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3903 struct dwarf2_per_objfile *dwarf2_per_objfile,
3904 int want_specific_block,
3905 int block_index,
3906 domain_enum domain,
3907 const char *name)
3908 {
3909 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3910 iter->want_specific_block = want_specific_block;
3911 iter->block_index = block_index;
3912 iter->domain = domain;
3913 iter->next = 0;
3914 iter->global_seen = 0;
3915
3916 mapped_index *index = dwarf2_per_objfile->index_table.get ();
3917
3918 /* index is NULL if OBJF_READNOW. */
3919 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3920 iter->length = MAYBE_SWAP (*iter->vec);
3921 else
3922 {
3923 iter->vec = NULL;
3924 iter->length = 0;
3925 }
3926 }
3927
3928 /* Return the next matching CU or NULL if there are no more. */
3929
3930 static struct dwarf2_per_cu_data *
3931 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3932 {
3933 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3934
3935 for ( ; iter->next < iter->length; ++iter->next)
3936 {
3937 offset_type cu_index_and_attrs =
3938 MAYBE_SWAP (iter->vec[iter->next + 1]);
3939 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3940 int want_static = iter->block_index != GLOBAL_BLOCK;
3941 /* This value is only valid for index versions >= 7. */
3942 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3943 gdb_index_symbol_kind symbol_kind =
3944 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3945 /* Only check the symbol attributes if they're present.
3946 Indices prior to version 7 don't record them,
3947 and indices >= 7 may elide them for certain symbols
3948 (gold does this). */
3949 int attrs_valid =
3950 (dwarf2_per_objfile->index_table->version >= 7
3951 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3952
3953 /* Don't crash on bad data. */
3954 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
3955 + dwarf2_per_objfile->all_type_units.size ()))
3956 {
3957 complaint (&symfile_complaints,
3958 _(".gdb_index entry has bad CU index"
3959 " [in module %s]"),
3960 objfile_name (dwarf2_per_objfile->objfile));
3961 continue;
3962 }
3963
3964 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3965
3966 /* Skip if already read in. */
3967 if (per_cu->v.quick->compunit_symtab)
3968 continue;
3969
3970 /* Check static vs global. */
3971 if (attrs_valid)
3972 {
3973 if (iter->want_specific_block
3974 && want_static != is_static)
3975 continue;
3976 /* Work around gold/15646. */
3977 if (!is_static && iter->global_seen)
3978 continue;
3979 if (!is_static)
3980 iter->global_seen = 1;
3981 }
3982
3983 /* Only check the symbol's kind if it has one. */
3984 if (attrs_valid)
3985 {
3986 switch (iter->domain)
3987 {
3988 case VAR_DOMAIN:
3989 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3990 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3991 /* Some types are also in VAR_DOMAIN. */
3992 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3993 continue;
3994 break;
3995 case STRUCT_DOMAIN:
3996 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3997 continue;
3998 break;
3999 case LABEL_DOMAIN:
4000 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4001 continue;
4002 break;
4003 default:
4004 break;
4005 }
4006 }
4007
4008 ++iter->next;
4009 return per_cu;
4010 }
4011
4012 return NULL;
4013 }
4014
4015 static struct compunit_symtab *
4016 dw2_lookup_symbol (struct objfile *objfile, int block_index,
4017 const char *name, domain_enum domain)
4018 {
4019 struct compunit_symtab *stab_best = NULL;
4020 struct dwarf2_per_objfile *dwarf2_per_objfile
4021 = get_dwarf2_per_objfile (objfile);
4022
4023 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4024
4025 struct dw2_symtab_iterator iter;
4026 struct dwarf2_per_cu_data *per_cu;
4027
4028 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 1, block_index, domain, name);
4029
4030 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4031 {
4032 struct symbol *sym, *with_opaque = NULL;
4033 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
4034 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4035 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
4036
4037 sym = block_find_symbol (block, name, domain,
4038 block_find_non_opaque_type_preferred,
4039 &with_opaque);
4040
4041 /* Some caution must be observed with overloaded functions
4042 and methods, since the index will not contain any overload
4043 information (but NAME might contain it). */
4044
4045 if (sym != NULL
4046 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4047 return stab;
4048 if (with_opaque != NULL
4049 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4050 stab_best = stab;
4051
4052 /* Keep looking through other CUs. */
4053 }
4054
4055 return stab_best;
4056 }
4057
4058 static void
4059 dw2_print_stats (struct objfile *objfile)
4060 {
4061 struct dwarf2_per_objfile *dwarf2_per_objfile
4062 = get_dwarf2_per_objfile (objfile);
4063 int total = (dwarf2_per_objfile->all_comp_units.size ()
4064 + dwarf2_per_objfile->all_type_units.size ());
4065 int count = 0;
4066
4067 for (int i = 0; i < total; ++i)
4068 {
4069 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
4070
4071 if (!per_cu->v.quick->compunit_symtab)
4072 ++count;
4073 }
4074 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
4075 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4076 }
4077
4078 /* This dumps minimal information about the index.
4079 It is called via "mt print objfiles".
4080 One use is to verify .gdb_index has been loaded by the
4081 gdb.dwarf2/gdb-index.exp testcase. */
4082
4083 static void
4084 dw2_dump (struct objfile *objfile)
4085 {
4086 struct dwarf2_per_objfile *dwarf2_per_objfile
4087 = get_dwarf2_per_objfile (objfile);
4088
4089 gdb_assert (dwarf2_per_objfile->using_index);
4090 printf_filtered (".gdb_index:");
4091 if (dwarf2_per_objfile->index_table != NULL)
4092 {
4093 printf_filtered (" version %d\n",
4094 dwarf2_per_objfile->index_table->version);
4095 }
4096 else
4097 printf_filtered (" faked for \"readnow\"\n");
4098 printf_filtered ("\n");
4099 }
4100
4101 static void
4102 dw2_relocate (struct objfile *objfile,
4103 const struct section_offsets *new_offsets,
4104 const struct section_offsets *delta)
4105 {
4106 /* There's nothing to relocate here. */
4107 }
4108
4109 static void
4110 dw2_expand_symtabs_for_function (struct objfile *objfile,
4111 const char *func_name)
4112 {
4113 struct dwarf2_per_objfile *dwarf2_per_objfile
4114 = get_dwarf2_per_objfile (objfile);
4115
4116 struct dw2_symtab_iterator iter;
4117 struct dwarf2_per_cu_data *per_cu;
4118
4119 /* Note: It doesn't matter what we pass for block_index here. */
4120 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 0, GLOBAL_BLOCK, VAR_DOMAIN,
4121 func_name);
4122
4123 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4124 dw2_instantiate_symtab (per_cu, false);
4125
4126 }
4127
4128 static void
4129 dw2_expand_all_symtabs (struct objfile *objfile)
4130 {
4131 struct dwarf2_per_objfile *dwarf2_per_objfile
4132 = get_dwarf2_per_objfile (objfile);
4133 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
4134 + dwarf2_per_objfile->all_type_units.size ());
4135
4136 for (int i = 0; i < total_units; ++i)
4137 {
4138 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
4139
4140 /* We don't want to directly expand a partial CU, because if we
4141 read it with the wrong language, then assertion failures can
4142 be triggered later on. See PR symtab/23010. So, tell
4143 dw2_instantiate_symtab to skip partial CUs -- any important
4144 partial CU will be read via DW_TAG_imported_unit anyway. */
4145 dw2_instantiate_symtab (per_cu, true);
4146 }
4147 }
4148
4149 static void
4150 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4151 const char *fullname)
4152 {
4153 struct dwarf2_per_objfile *dwarf2_per_objfile
4154 = get_dwarf2_per_objfile (objfile);
4155
4156 /* We don't need to consider type units here.
4157 This is only called for examining code, e.g. expand_line_sal.
4158 There can be an order of magnitude (or more) more type units
4159 than comp units, and we avoid them if we can. */
4160
4161 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4162 {
4163 /* We only need to look at symtabs not already expanded. */
4164 if (per_cu->v.quick->compunit_symtab)
4165 continue;
4166
4167 quick_file_names *file_data = dw2_get_file_names (per_cu);
4168 if (file_data == NULL)
4169 continue;
4170
4171 for (int j = 0; j < file_data->num_file_names; ++j)
4172 {
4173 const char *this_fullname = file_data->file_names[j];
4174
4175 if (filename_cmp (this_fullname, fullname) == 0)
4176 {
4177 dw2_instantiate_symtab (per_cu, false);
4178 break;
4179 }
4180 }
4181 }
4182 }
4183
4184 static void
4185 dw2_map_matching_symbols (struct objfile *objfile,
4186 const char * name, domain_enum domain,
4187 int global,
4188 int (*callback) (struct block *,
4189 struct symbol *, void *),
4190 void *data, symbol_name_match_type match,
4191 symbol_compare_ftype *ordered_compare)
4192 {
4193 /* Currently unimplemented; used for Ada. The function can be called if the
4194 current language is Ada for a non-Ada objfile using GNU index. As Ada
4195 does not look for non-Ada symbols this function should just return. */
4196 }
4197
4198 /* Symbol name matcher for .gdb_index names.
4199
4200 Symbol names in .gdb_index have a few particularities:
4201
4202 - There's no indication of which is the language of each symbol.
4203
4204 Since each language has its own symbol name matching algorithm,
4205 and we don't know which language is the right one, we must match
4206 each symbol against all languages. This would be a potential
4207 performance problem if it were not mitigated by the
4208 mapped_index::name_components lookup table, which significantly
4209 reduces the number of times we need to call into this matcher,
4210 making it a non-issue.
4211
4212 - Symbol names in the index have no overload (parameter)
4213 information. I.e., in C++, "foo(int)" and "foo(long)" both
4214 appear as "foo" in the index, for example.
4215
4216 This means that the lookup names passed to the symbol name
4217 matcher functions must have no parameter information either
4218 because (e.g.) symbol search name "foo" does not match
4219 lookup-name "foo(int)" [while swapping search name for lookup
4220 name would match].
4221 */
4222 class gdb_index_symbol_name_matcher
4223 {
4224 public:
4225 /* Prepares the vector of comparison functions for LOOKUP_NAME. */
4226 gdb_index_symbol_name_matcher (const lookup_name_info &lookup_name);
4227
4228 /* Walk all the matcher routines and match SYMBOL_NAME against them.
4229 Returns true if any matcher matches. */
4230 bool matches (const char *symbol_name);
4231
4232 private:
4233 /* A reference to the lookup name we're matching against. */
4234 const lookup_name_info &m_lookup_name;
4235
4236 /* A vector holding all the different symbol name matchers, for all
4237 languages. */
4238 std::vector<symbol_name_matcher_ftype *> m_symbol_name_matcher_funcs;
4239 };
4240
4241 gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
4242 (const lookup_name_info &lookup_name)
4243 : m_lookup_name (lookup_name)
4244 {
4245 /* Prepare the vector of comparison functions upfront, to avoid
4246 doing the same work for each symbol. Care is taken to avoid
4247 matching with the same matcher more than once if/when multiple
4248 languages use the same matcher function. */
4249 auto &matchers = m_symbol_name_matcher_funcs;
4250 matchers.reserve (nr_languages);
4251
4252 matchers.push_back (default_symbol_name_matcher);
4253
4254 for (int i = 0; i < nr_languages; i++)
4255 {
4256 const language_defn *lang = language_def ((enum language) i);
4257 symbol_name_matcher_ftype *name_matcher
4258 = get_symbol_name_matcher (lang, m_lookup_name);
4259
4260 /* Don't insert the same comparison routine more than once.
4261 Note that we do this linear walk instead of a seemingly
4262 cheaper sorted insert, or use a std::set or something like
4263 that, because relative order of function addresses is not
4264 stable. This is not a problem in practice because the number
4265 of supported languages is low, and the cost here is tiny
4266 compared to the number of searches we'll do afterwards using
4267 this object. */
4268 if (name_matcher != default_symbol_name_matcher
4269 && (std::find (matchers.begin (), matchers.end (), name_matcher)
4270 == matchers.end ()))
4271 matchers.push_back (name_matcher);
4272 }
4273 }
4274
4275 bool
4276 gdb_index_symbol_name_matcher::matches (const char *symbol_name)
4277 {
4278 for (auto matches_name : m_symbol_name_matcher_funcs)
4279 if (matches_name (symbol_name, m_lookup_name, NULL))
4280 return true;
4281
4282 return false;
4283 }
4284
4285 /* Starting from a search name, return the string that finds the upper
4286 bound of all strings that start with SEARCH_NAME in a sorted name
4287 list. Returns the empty string to indicate that the upper bound is
4288 the end of the list. */
4289
4290 static std::string
4291 make_sort_after_prefix_name (const char *search_name)
4292 {
4293 /* When looking to complete "func", we find the upper bound of all
4294 symbols that start with "func" by looking for where we'd insert
4295 the closest string that would follow "func" in lexicographical
4296 order. Usually, that's "func"-with-last-character-incremented,
4297 i.e. "fund". Mind non-ASCII characters, though. Usually those
4298 will be UTF-8 multi-byte sequences, but we can't be certain.
4299 Especially mind the 0xff character, which is a valid character in
4300 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4301 rule out compilers allowing it in identifiers. Note that
4302 conveniently, strcmp/strcasecmp are specified to compare
4303 characters interpreted as unsigned char. So what we do is treat
4304 the whole string as a base 256 number composed of a sequence of
4305 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4306 to 0, and carries 1 to the following more-significant position.
4307 If the very first character in SEARCH_NAME ends up incremented
4308 and carries/overflows, then the upper bound is the end of the
4309 list. The string after the empty string is also the empty
4310 string.
4311
4312 Some examples of this operation:
4313
4314 SEARCH_NAME => "+1" RESULT
4315
4316 "abc" => "abd"
4317 "ab\xff" => "ac"
4318 "\xff" "a" "\xff" => "\xff" "b"
4319 "\xff" => ""
4320 "\xff\xff" => ""
4321 "" => ""
4322
4323 Then, with these symbols for example:
4324
4325 func
4326 func1
4327 fund
4328
4329 completing "func" looks for symbols between "func" and
4330 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4331 which finds "func" and "func1", but not "fund".
4332
4333 And with:
4334
4335 funcÿ (Latin1 'ÿ' [0xff])
4336 funcÿ1
4337 fund
4338
4339 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4340 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4341
4342 And with:
4343
4344 ÿÿ (Latin1 'ÿ' [0xff])
4345 ÿÿ1
4346
4347 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4348 the end of the list.
4349 */
4350 std::string after = search_name;
4351 while (!after.empty () && (unsigned char) after.back () == 0xff)
4352 after.pop_back ();
4353 if (!after.empty ())
4354 after.back () = (unsigned char) after.back () + 1;
4355 return after;
4356 }
4357
4358 /* See declaration. */
4359
4360 std::pair<std::vector<name_component>::const_iterator,
4361 std::vector<name_component>::const_iterator>
4362 mapped_index_base::find_name_components_bounds
4363 (const lookup_name_info &lookup_name_without_params) const
4364 {
4365 auto *name_cmp
4366 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4367
4368 const char *cplus
4369 = lookup_name_without_params.cplus ().lookup_name ().c_str ();
4370
4371 /* Comparison function object for lower_bound that matches against a
4372 given symbol name. */
4373 auto lookup_compare_lower = [&] (const name_component &elem,
4374 const char *name)
4375 {
4376 const char *elem_qualified = this->symbol_name_at (elem.idx);
4377 const char *elem_name = elem_qualified + elem.name_offset;
4378 return name_cmp (elem_name, name) < 0;
4379 };
4380
4381 /* Comparison function object for upper_bound that matches against a
4382 given symbol name. */
4383 auto lookup_compare_upper = [&] (const char *name,
4384 const name_component &elem)
4385 {
4386 const char *elem_qualified = this->symbol_name_at (elem.idx);
4387 const char *elem_name = elem_qualified + elem.name_offset;
4388 return name_cmp (name, elem_name) < 0;
4389 };
4390
4391 auto begin = this->name_components.begin ();
4392 auto end = this->name_components.end ();
4393
4394 /* Find the lower bound. */
4395 auto lower = [&] ()
4396 {
4397 if (lookup_name_without_params.completion_mode () && cplus[0] == '\0')
4398 return begin;
4399 else
4400 return std::lower_bound (begin, end, cplus, lookup_compare_lower);
4401 } ();
4402
4403 /* Find the upper bound. */
4404 auto upper = [&] ()
4405 {
4406 if (lookup_name_without_params.completion_mode ())
4407 {
4408 /* In completion mode, we want UPPER to point past all
4409 symbols names that have the same prefix. I.e., with
4410 these symbols, and completing "func":
4411
4412 function << lower bound
4413 function1
4414 other_function << upper bound
4415
4416 We find the upper bound by looking for the insertion
4417 point of "func"-with-last-character-incremented,
4418 i.e. "fund". */
4419 std::string after = make_sort_after_prefix_name (cplus);
4420 if (after.empty ())
4421 return end;
4422 return std::lower_bound (lower, end, after.c_str (),
4423 lookup_compare_lower);
4424 }
4425 else
4426 return std::upper_bound (lower, end, cplus, lookup_compare_upper);
4427 } ();
4428
4429 return {lower, upper};
4430 }
4431
4432 /* See declaration. */
4433
4434 void
4435 mapped_index_base::build_name_components ()
4436 {
4437 if (!this->name_components.empty ())
4438 return;
4439
4440 this->name_components_casing = case_sensitivity;
4441 auto *name_cmp
4442 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4443
4444 /* The code below only knows how to break apart components of C++
4445 symbol names (and other languages that use '::' as
4446 namespace/module separator). If we add support for wild matching
4447 to some language that uses some other operator (E.g., Ada, Go and
4448 D use '.'), then we'll need to try splitting the symbol name
4449 according to that language too. Note that Ada does support wild
4450 matching, but doesn't currently support .gdb_index. */
4451 auto count = this->symbol_name_count ();
4452 for (offset_type idx = 0; idx < count; idx++)
4453 {
4454 if (this->symbol_name_slot_invalid (idx))
4455 continue;
4456
4457 const char *name = this->symbol_name_at (idx);
4458
4459 /* Add each name component to the name component table. */
4460 unsigned int previous_len = 0;
4461 for (unsigned int current_len = cp_find_first_component (name);
4462 name[current_len] != '\0';
4463 current_len += cp_find_first_component (name + current_len))
4464 {
4465 gdb_assert (name[current_len] == ':');
4466 this->name_components.push_back ({previous_len, idx});
4467 /* Skip the '::'. */
4468 current_len += 2;
4469 previous_len = current_len;
4470 }
4471 this->name_components.push_back ({previous_len, idx});
4472 }
4473
4474 /* Sort name_components elements by name. */
4475 auto name_comp_compare = [&] (const name_component &left,
4476 const name_component &right)
4477 {
4478 const char *left_qualified = this->symbol_name_at (left.idx);
4479 const char *right_qualified = this->symbol_name_at (right.idx);
4480
4481 const char *left_name = left_qualified + left.name_offset;
4482 const char *right_name = right_qualified + right.name_offset;
4483
4484 return name_cmp (left_name, right_name) < 0;
4485 };
4486
4487 std::sort (this->name_components.begin (),
4488 this->name_components.end (),
4489 name_comp_compare);
4490 }
4491
4492 /* Helper for dw2_expand_symtabs_matching that works with a
4493 mapped_index_base instead of the containing objfile. This is split
4494 to a separate function in order to be able to unit test the
4495 name_components matching using a mock mapped_index_base. For each
4496 symbol name that matches, calls MATCH_CALLBACK, passing it the
4497 symbol's index in the mapped_index_base symbol table. */
4498
4499 static void
4500 dw2_expand_symtabs_matching_symbol
4501 (mapped_index_base &index,
4502 const lookup_name_info &lookup_name_in,
4503 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4504 enum search_domain kind,
4505 gdb::function_view<void (offset_type)> match_callback)
4506 {
4507 lookup_name_info lookup_name_without_params
4508 = lookup_name_in.make_ignore_params ();
4509 gdb_index_symbol_name_matcher lookup_name_matcher
4510 (lookup_name_without_params);
4511
4512 /* Build the symbol name component sorted vector, if we haven't
4513 yet. */
4514 index.build_name_components ();
4515
4516 auto bounds = index.find_name_components_bounds (lookup_name_without_params);
4517
4518 /* Now for each symbol name in range, check to see if we have a name
4519 match, and if so, call the MATCH_CALLBACK callback. */
4520
4521 /* The same symbol may appear more than once in the range though.
4522 E.g., if we're looking for symbols that complete "w", and we have
4523 a symbol named "w1::w2", we'll find the two name components for
4524 that same symbol in the range. To be sure we only call the
4525 callback once per symbol, we first collect the symbol name
4526 indexes that matched in a temporary vector and ignore
4527 duplicates. */
4528 std::vector<offset_type> matches;
4529 matches.reserve (std::distance (bounds.first, bounds.second));
4530
4531 for (; bounds.first != bounds.second; ++bounds.first)
4532 {
4533 const char *qualified = index.symbol_name_at (bounds.first->idx);
4534
4535 if (!lookup_name_matcher.matches (qualified)
4536 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4537 continue;
4538
4539 matches.push_back (bounds.first->idx);
4540 }
4541
4542 std::sort (matches.begin (), matches.end ());
4543
4544 /* Finally call the callback, once per match. */
4545 ULONGEST prev = -1;
4546 for (offset_type idx : matches)
4547 {
4548 if (prev != idx)
4549 {
4550 match_callback (idx);
4551 prev = idx;
4552 }
4553 }
4554
4555 /* Above we use a type wider than idx's for 'prev', since 0 and
4556 (offset_type)-1 are both possible values. */
4557 static_assert (sizeof (prev) > sizeof (offset_type), "");
4558 }
4559
4560 #if GDB_SELF_TEST
4561
4562 namespace selftests { namespace dw2_expand_symtabs_matching {
4563
4564 /* A mock .gdb_index/.debug_names-like name index table, enough to
4565 exercise dw2_expand_symtabs_matching_symbol, which works with the
4566 mapped_index_base interface. Builds an index from the symbol list
4567 passed as parameter to the constructor. */
4568 class mock_mapped_index : public mapped_index_base
4569 {
4570 public:
4571 mock_mapped_index (gdb::array_view<const char *> symbols)
4572 : m_symbol_table (symbols)
4573 {}
4574
4575 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4576
4577 /* Return the number of names in the symbol table. */
4578 size_t symbol_name_count () const override
4579 {
4580 return m_symbol_table.size ();
4581 }
4582
4583 /* Get the name of the symbol at IDX in the symbol table. */
4584 const char *symbol_name_at (offset_type idx) const override
4585 {
4586 return m_symbol_table[idx];
4587 }
4588
4589 private:
4590 gdb::array_view<const char *> m_symbol_table;
4591 };
4592
4593 /* Convenience function that converts a NULL pointer to a "<null>"
4594 string, to pass to print routines. */
4595
4596 static const char *
4597 string_or_null (const char *str)
4598 {
4599 return str != NULL ? str : "<null>";
4600 }
4601
4602 /* Check if a lookup_name_info built from
4603 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4604 index. EXPECTED_LIST is the list of expected matches, in expected
4605 matching order. If no match expected, then an empty list is
4606 specified. Returns true on success. On failure prints a warning
4607 indicating the file:line that failed, and returns false. */
4608
4609 static bool
4610 check_match (const char *file, int line,
4611 mock_mapped_index &mock_index,
4612 const char *name, symbol_name_match_type match_type,
4613 bool completion_mode,
4614 std::initializer_list<const char *> expected_list)
4615 {
4616 lookup_name_info lookup_name (name, match_type, completion_mode);
4617
4618 bool matched = true;
4619
4620 auto mismatch = [&] (const char *expected_str,
4621 const char *got)
4622 {
4623 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4624 "expected=\"%s\", got=\"%s\"\n"),
4625 file, line,
4626 (match_type == symbol_name_match_type::FULL
4627 ? "FULL" : "WILD"),
4628 name, string_or_null (expected_str), string_or_null (got));
4629 matched = false;
4630 };
4631
4632 auto expected_it = expected_list.begin ();
4633 auto expected_end = expected_list.end ();
4634
4635 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4636 NULL, ALL_DOMAIN,
4637 [&] (offset_type idx)
4638 {
4639 const char *matched_name = mock_index.symbol_name_at (idx);
4640 const char *expected_str
4641 = expected_it == expected_end ? NULL : *expected_it++;
4642
4643 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4644 mismatch (expected_str, matched_name);
4645 });
4646
4647 const char *expected_str
4648 = expected_it == expected_end ? NULL : *expected_it++;
4649 if (expected_str != NULL)
4650 mismatch (expected_str, NULL);
4651
4652 return matched;
4653 }
4654
4655 /* The symbols added to the mock mapped_index for testing (in
4656 canonical form). */
4657 static const char *test_symbols[] = {
4658 "function",
4659 "std::bar",
4660 "std::zfunction",
4661 "std::zfunction2",
4662 "w1::w2",
4663 "ns::foo<char*>",
4664 "ns::foo<int>",
4665 "ns::foo<long>",
4666 "ns2::tmpl<int>::foo2",
4667 "(anonymous namespace)::A::B::C",
4668
4669 /* These are used to check that the increment-last-char in the
4670 matching algorithm for completion doesn't match "t1_fund" when
4671 completing "t1_func". */
4672 "t1_func",
4673 "t1_func1",
4674 "t1_fund",
4675 "t1_fund1",
4676
4677 /* A UTF-8 name with multi-byte sequences to make sure that
4678 cp-name-parser understands this as a single identifier ("função"
4679 is "function" in PT). */
4680 u8"u8função",
4681
4682 /* \377 (0xff) is Latin1 'ÿ'. */
4683 "yfunc\377",
4684
4685 /* \377 (0xff) is Latin1 'ÿ'. */
4686 "\377",
4687 "\377\377123",
4688
4689 /* A name with all sorts of complications. Starts with "z" to make
4690 it easier for the completion tests below. */
4691 #define Z_SYM_NAME \
4692 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4693 "::tuple<(anonymous namespace)::ui*, " \
4694 "std::default_delete<(anonymous namespace)::ui>, void>"
4695
4696 Z_SYM_NAME
4697 };
4698
4699 /* Returns true if the mapped_index_base::find_name_component_bounds
4700 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4701 in completion mode. */
4702
4703 static bool
4704 check_find_bounds_finds (mapped_index_base &index,
4705 const char *search_name,
4706 gdb::array_view<const char *> expected_syms)
4707 {
4708 lookup_name_info lookup_name (search_name,
4709 symbol_name_match_type::FULL, true);
4710
4711 auto bounds = index.find_name_components_bounds (lookup_name);
4712
4713 size_t distance = std::distance (bounds.first, bounds.second);
4714 if (distance != expected_syms.size ())
4715 return false;
4716
4717 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4718 {
4719 auto nc_elem = bounds.first + exp_elem;
4720 const char *qualified = index.symbol_name_at (nc_elem->idx);
4721 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4722 return false;
4723 }
4724
4725 return true;
4726 }
4727
4728 /* Test the lower-level mapped_index::find_name_component_bounds
4729 method. */
4730
4731 static void
4732 test_mapped_index_find_name_component_bounds ()
4733 {
4734 mock_mapped_index mock_index (test_symbols);
4735
4736 mock_index.build_name_components ();
4737
4738 /* Test the lower-level mapped_index::find_name_component_bounds
4739 method in completion mode. */
4740 {
4741 static const char *expected_syms[] = {
4742 "t1_func",
4743 "t1_func1",
4744 };
4745
4746 SELF_CHECK (check_find_bounds_finds (mock_index,
4747 "t1_func", expected_syms));
4748 }
4749
4750 /* Check that the increment-last-char in the name matching algorithm
4751 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4752 {
4753 static const char *expected_syms1[] = {
4754 "\377",
4755 "\377\377123",
4756 };
4757 SELF_CHECK (check_find_bounds_finds (mock_index,
4758 "\377", expected_syms1));
4759
4760 static const char *expected_syms2[] = {
4761 "\377\377123",
4762 };
4763 SELF_CHECK (check_find_bounds_finds (mock_index,
4764 "\377\377", expected_syms2));
4765 }
4766 }
4767
4768 /* Test dw2_expand_symtabs_matching_symbol. */
4769
4770 static void
4771 test_dw2_expand_symtabs_matching_symbol ()
4772 {
4773 mock_mapped_index mock_index (test_symbols);
4774
4775 /* We let all tests run until the end even if some fails, for debug
4776 convenience. */
4777 bool any_mismatch = false;
4778
4779 /* Create the expected symbols list (an initializer_list). Needed
4780 because lists have commas, and we need to pass them to CHECK,
4781 which is a macro. */
4782 #define EXPECT(...) { __VA_ARGS__ }
4783
4784 /* Wrapper for check_match that passes down the current
4785 __FILE__/__LINE__. */
4786 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4787 any_mismatch |= !check_match (__FILE__, __LINE__, \
4788 mock_index, \
4789 NAME, MATCH_TYPE, COMPLETION_MODE, \
4790 EXPECTED_LIST)
4791
4792 /* Identity checks. */
4793 for (const char *sym : test_symbols)
4794 {
4795 /* Should be able to match all existing symbols. */
4796 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4797 EXPECT (sym));
4798
4799 /* Should be able to match all existing symbols with
4800 parameters. */
4801 std::string with_params = std::string (sym) + "(int)";
4802 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4803 EXPECT (sym));
4804
4805 /* Should be able to match all existing symbols with
4806 parameters and qualifiers. */
4807 with_params = std::string (sym) + " ( int ) const";
4808 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4809 EXPECT (sym));
4810
4811 /* This should really find sym, but cp-name-parser.y doesn't
4812 know about lvalue/rvalue qualifiers yet. */
4813 with_params = std::string (sym) + " ( int ) &&";
4814 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4815 {});
4816 }
4817
4818 /* Check that the name matching algorithm for completion doesn't get
4819 confused with Latin1 'ÿ' / 0xff. */
4820 {
4821 static const char str[] = "\377";
4822 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4823 EXPECT ("\377", "\377\377123"));
4824 }
4825
4826 /* Check that the increment-last-char in the matching algorithm for
4827 completion doesn't match "t1_fund" when completing "t1_func". */
4828 {
4829 static const char str[] = "t1_func";
4830 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4831 EXPECT ("t1_func", "t1_func1"));
4832 }
4833
4834 /* Check that completion mode works at each prefix of the expected
4835 symbol name. */
4836 {
4837 static const char str[] = "function(int)";
4838 size_t len = strlen (str);
4839 std::string lookup;
4840
4841 for (size_t i = 1; i < len; i++)
4842 {
4843 lookup.assign (str, i);
4844 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4845 EXPECT ("function"));
4846 }
4847 }
4848
4849 /* While "w" is a prefix of both components, the match function
4850 should still only be called once. */
4851 {
4852 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4853 EXPECT ("w1::w2"));
4854 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4855 EXPECT ("w1::w2"));
4856 }
4857
4858 /* Same, with a "complicated" symbol. */
4859 {
4860 static const char str[] = Z_SYM_NAME;
4861 size_t len = strlen (str);
4862 std::string lookup;
4863
4864 for (size_t i = 1; i < len; i++)
4865 {
4866 lookup.assign (str, i);
4867 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4868 EXPECT (Z_SYM_NAME));
4869 }
4870 }
4871
4872 /* In FULL mode, an incomplete symbol doesn't match. */
4873 {
4874 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4875 {});
4876 }
4877
4878 /* A complete symbol with parameters matches any overload, since the
4879 index has no overload info. */
4880 {
4881 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4882 EXPECT ("std::zfunction", "std::zfunction2"));
4883 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4884 EXPECT ("std::zfunction", "std::zfunction2"));
4885 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4886 EXPECT ("std::zfunction", "std::zfunction2"));
4887 }
4888
4889 /* Check that whitespace is ignored appropriately. A symbol with a
4890 template argument list. */
4891 {
4892 static const char expected[] = "ns::foo<int>";
4893 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4894 EXPECT (expected));
4895 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4896 EXPECT (expected));
4897 }
4898
4899 /* Check that whitespace is ignored appropriately. A symbol with a
4900 template argument list that includes a pointer. */
4901 {
4902 static const char expected[] = "ns::foo<char*>";
4903 /* Try both completion and non-completion modes. */
4904 static const bool completion_mode[2] = {false, true};
4905 for (size_t i = 0; i < 2; i++)
4906 {
4907 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4908 completion_mode[i], EXPECT (expected));
4909 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4910 completion_mode[i], EXPECT (expected));
4911
4912 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4913 completion_mode[i], EXPECT (expected));
4914 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4915 completion_mode[i], EXPECT (expected));
4916 }
4917 }
4918
4919 {
4920 /* Check method qualifiers are ignored. */
4921 static const char expected[] = "ns::foo<char*>";
4922 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4923 symbol_name_match_type::FULL, true, EXPECT (expected));
4924 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4925 symbol_name_match_type::FULL, true, EXPECT (expected));
4926 CHECK_MATCH ("foo < char * > ( int ) const",
4927 symbol_name_match_type::WILD, true, EXPECT (expected));
4928 CHECK_MATCH ("foo < char * > ( int ) &&",
4929 symbol_name_match_type::WILD, true, EXPECT (expected));
4930 }
4931
4932 /* Test lookup names that don't match anything. */
4933 {
4934 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4935 {});
4936
4937 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4938 {});
4939 }
4940
4941 /* Some wild matching tests, exercising "(anonymous namespace)",
4942 which should not be confused with a parameter list. */
4943 {
4944 static const char *syms[] = {
4945 "A::B::C",
4946 "B::C",
4947 "C",
4948 "A :: B :: C ( int )",
4949 "B :: C ( int )",
4950 "C ( int )",
4951 };
4952
4953 for (const char *s : syms)
4954 {
4955 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4956 EXPECT ("(anonymous namespace)::A::B::C"));
4957 }
4958 }
4959
4960 {
4961 static const char expected[] = "ns2::tmpl<int>::foo2";
4962 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4963 EXPECT (expected));
4964 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4965 EXPECT (expected));
4966 }
4967
4968 SELF_CHECK (!any_mismatch);
4969
4970 #undef EXPECT
4971 #undef CHECK_MATCH
4972 }
4973
4974 static void
4975 run_test ()
4976 {
4977 test_mapped_index_find_name_component_bounds ();
4978 test_dw2_expand_symtabs_matching_symbol ();
4979 }
4980
4981 }} // namespace selftests::dw2_expand_symtabs_matching
4982
4983 #endif /* GDB_SELF_TEST */
4984
4985 /* If FILE_MATCHER is NULL or if PER_CU has
4986 dwarf2_per_cu_quick_data::MARK set (see
4987 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4988 EXPANSION_NOTIFY on it. */
4989
4990 static void
4991 dw2_expand_symtabs_matching_one
4992 (struct dwarf2_per_cu_data *per_cu,
4993 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4994 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4995 {
4996 if (file_matcher == NULL || per_cu->v.quick->mark)
4997 {
4998 bool symtab_was_null
4999 = (per_cu->v.quick->compunit_symtab == NULL);
5000
5001 dw2_instantiate_symtab (per_cu, false);
5002
5003 if (expansion_notify != NULL
5004 && symtab_was_null
5005 && per_cu->v.quick->compunit_symtab != NULL)
5006 expansion_notify (per_cu->v.quick->compunit_symtab);
5007 }
5008 }
5009
5010 /* Helper for dw2_expand_matching symtabs. Called on each symbol
5011 matched, to expand corresponding CUs that were marked. IDX is the
5012 index of the symbol name that matched. */
5013
5014 static void
5015 dw2_expand_marked_cus
5016 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
5017 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5018 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5019 search_domain kind)
5020 {
5021 offset_type *vec, vec_len, vec_idx;
5022 bool global_seen = false;
5023 mapped_index &index = *dwarf2_per_objfile->index_table;
5024
5025 vec = (offset_type *) (index.constant_pool
5026 + MAYBE_SWAP (index.symbol_table[idx].vec));
5027 vec_len = MAYBE_SWAP (vec[0]);
5028 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5029 {
5030 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5031 /* This value is only valid for index versions >= 7. */
5032 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5033 gdb_index_symbol_kind symbol_kind =
5034 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5035 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5036 /* Only check the symbol attributes if they're present.
5037 Indices prior to version 7 don't record them,
5038 and indices >= 7 may elide them for certain symbols
5039 (gold does this). */
5040 int attrs_valid =
5041 (index.version >= 7
5042 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5043
5044 /* Work around gold/15646. */
5045 if (attrs_valid)
5046 {
5047 if (!is_static && global_seen)
5048 continue;
5049 if (!is_static)
5050 global_seen = true;
5051 }
5052
5053 /* Only check the symbol's kind if it has one. */
5054 if (attrs_valid)
5055 {
5056 switch (kind)
5057 {
5058 case VARIABLES_DOMAIN:
5059 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5060 continue;
5061 break;
5062 case FUNCTIONS_DOMAIN:
5063 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
5064 continue;
5065 break;
5066 case TYPES_DOMAIN:
5067 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5068 continue;
5069 break;
5070 default:
5071 break;
5072 }
5073 }
5074
5075 /* Don't crash on bad data. */
5076 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
5077 + dwarf2_per_objfile->all_type_units.size ()))
5078 {
5079 complaint (&symfile_complaints,
5080 _(".gdb_index entry has bad CU index"
5081 " [in module %s]"),
5082 objfile_name (dwarf2_per_objfile->objfile));
5083 continue;
5084 }
5085
5086 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
5087 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5088 expansion_notify);
5089 }
5090 }
5091
5092 /* If FILE_MATCHER is non-NULL, set all the
5093 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5094 that match FILE_MATCHER. */
5095
5096 static void
5097 dw_expand_symtabs_matching_file_matcher
5098 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5099 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
5100 {
5101 if (file_matcher == NULL)
5102 return;
5103
5104 objfile *const objfile = dwarf2_per_objfile->objfile;
5105
5106 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5107 htab_eq_pointer,
5108 NULL, xcalloc, xfree));
5109 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
5110 htab_eq_pointer,
5111 NULL, xcalloc, xfree));
5112
5113 /* The rule is CUs specify all the files, including those used by
5114 any TU, so there's no need to scan TUs here. */
5115
5116 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5117 {
5118 QUIT;
5119
5120 per_cu->v.quick->mark = 0;
5121
5122 /* We only need to look at symtabs not already expanded. */
5123 if (per_cu->v.quick->compunit_symtab)
5124 continue;
5125
5126 quick_file_names *file_data = dw2_get_file_names (per_cu);
5127 if (file_data == NULL)
5128 continue;
5129
5130 if (htab_find (visited_not_found.get (), file_data) != NULL)
5131 continue;
5132 else if (htab_find (visited_found.get (), file_data) != NULL)
5133 {
5134 per_cu->v.quick->mark = 1;
5135 continue;
5136 }
5137
5138 for (int j = 0; j < file_data->num_file_names; ++j)
5139 {
5140 const char *this_real_name;
5141
5142 if (file_matcher (file_data->file_names[j], false))
5143 {
5144 per_cu->v.quick->mark = 1;
5145 break;
5146 }
5147
5148 /* Before we invoke realpath, which can get expensive when many
5149 files are involved, do a quick comparison of the basenames. */
5150 if (!basenames_may_differ
5151 && !file_matcher (lbasename (file_data->file_names[j]),
5152 true))
5153 continue;
5154
5155 this_real_name = dw2_get_real_path (objfile, file_data, j);
5156 if (file_matcher (this_real_name, false))
5157 {
5158 per_cu->v.quick->mark = 1;
5159 break;
5160 }
5161 }
5162
5163 void **slot = htab_find_slot (per_cu->v.quick->mark
5164 ? visited_found.get ()
5165 : visited_not_found.get (),
5166 file_data, INSERT);
5167 *slot = file_data;
5168 }
5169 }
5170
5171 static void
5172 dw2_expand_symtabs_matching
5173 (struct objfile *objfile,
5174 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5175 const lookup_name_info &lookup_name,
5176 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5177 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5178 enum search_domain kind)
5179 {
5180 struct dwarf2_per_objfile *dwarf2_per_objfile
5181 = get_dwarf2_per_objfile (objfile);
5182
5183 /* index_table is NULL if OBJF_READNOW. */
5184 if (!dwarf2_per_objfile->index_table)
5185 return;
5186
5187 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5188
5189 mapped_index &index = *dwarf2_per_objfile->index_table;
5190
5191 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5192 symbol_matcher,
5193 kind, [&] (offset_type idx)
5194 {
5195 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
5196 expansion_notify, kind);
5197 });
5198 }
5199
5200 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5201 symtab. */
5202
5203 static struct compunit_symtab *
5204 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5205 CORE_ADDR pc)
5206 {
5207 int i;
5208
5209 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5210 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5211 return cust;
5212
5213 if (cust->includes == NULL)
5214 return NULL;
5215
5216 for (i = 0; cust->includes[i]; ++i)
5217 {
5218 struct compunit_symtab *s = cust->includes[i];
5219
5220 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5221 if (s != NULL)
5222 return s;
5223 }
5224
5225 return NULL;
5226 }
5227
5228 static struct compunit_symtab *
5229 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5230 struct bound_minimal_symbol msymbol,
5231 CORE_ADDR pc,
5232 struct obj_section *section,
5233 int warn_if_readin)
5234 {
5235 struct dwarf2_per_cu_data *data;
5236 struct compunit_symtab *result;
5237
5238 if (!objfile->psymtabs_addrmap)
5239 return NULL;
5240
5241 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
5242 pc);
5243 if (!data)
5244 return NULL;
5245
5246 if (warn_if_readin && data->v.quick->compunit_symtab)
5247 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5248 paddress (get_objfile_arch (objfile), pc));
5249
5250 result
5251 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
5252 false),
5253 pc);
5254 gdb_assert (result != NULL);
5255 return result;
5256 }
5257
5258 static void
5259 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5260 void *data, int need_fullname)
5261 {
5262 struct dwarf2_per_objfile *dwarf2_per_objfile
5263 = get_dwarf2_per_objfile (objfile);
5264
5265 if (!dwarf2_per_objfile->filenames_cache)
5266 {
5267 dwarf2_per_objfile->filenames_cache.emplace ();
5268
5269 htab_up visited (htab_create_alloc (10,
5270 htab_hash_pointer, htab_eq_pointer,
5271 NULL, xcalloc, xfree));
5272
5273 /* The rule is CUs specify all the files, including those used
5274 by any TU, so there's no need to scan TUs here. We can
5275 ignore file names coming from already-expanded CUs. */
5276
5277 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5278 {
5279 if (per_cu->v.quick->compunit_symtab)
5280 {
5281 void **slot = htab_find_slot (visited.get (),
5282 per_cu->v.quick->file_names,
5283 INSERT);
5284
5285 *slot = per_cu->v.quick->file_names;
5286 }
5287 }
5288
5289 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5290 {
5291 /* We only need to look at symtabs not already expanded. */
5292 if (per_cu->v.quick->compunit_symtab)
5293 continue;
5294
5295 quick_file_names *file_data = dw2_get_file_names (per_cu);
5296 if (file_data == NULL)
5297 continue;
5298
5299 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
5300 if (*slot)
5301 {
5302 /* Already visited. */
5303 continue;
5304 }
5305 *slot = file_data;
5306
5307 for (int j = 0; j < file_data->num_file_names; ++j)
5308 {
5309 const char *filename = file_data->file_names[j];
5310 dwarf2_per_objfile->filenames_cache->seen (filename);
5311 }
5312 }
5313 }
5314
5315 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5316 {
5317 gdb::unique_xmalloc_ptr<char> this_real_name;
5318
5319 if (need_fullname)
5320 this_real_name = gdb_realpath (filename);
5321 (*fun) (filename, this_real_name.get (), data);
5322 });
5323 }
5324
5325 static int
5326 dw2_has_symbols (struct objfile *objfile)
5327 {
5328 return 1;
5329 }
5330
5331 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5332 {
5333 dw2_has_symbols,
5334 dw2_find_last_source_symtab,
5335 dw2_forget_cached_source_info,
5336 dw2_map_symtabs_matching_filename,
5337 dw2_lookup_symbol,
5338 dw2_print_stats,
5339 dw2_dump,
5340 dw2_relocate,
5341 dw2_expand_symtabs_for_function,
5342 dw2_expand_all_symtabs,
5343 dw2_expand_symtabs_with_fullname,
5344 dw2_map_matching_symbols,
5345 dw2_expand_symtabs_matching,
5346 dw2_find_pc_sect_compunit_symtab,
5347 NULL,
5348 dw2_map_symbol_filenames
5349 };
5350
5351 /* DWARF-5 debug_names reader. */
5352
5353 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5354 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5355
5356 /* A helper function that reads the .debug_names section in SECTION
5357 and fills in MAP. FILENAME is the name of the file containing the
5358 section; it is used for error reporting.
5359
5360 Returns true if all went well, false otherwise. */
5361
5362 static bool
5363 read_debug_names_from_section (struct objfile *objfile,
5364 const char *filename,
5365 struct dwarf2_section_info *section,
5366 mapped_debug_names &map)
5367 {
5368 if (dwarf2_section_empty_p (section))
5369 return false;
5370
5371 /* Older elfutils strip versions could keep the section in the main
5372 executable while splitting it for the separate debug info file. */
5373 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5374 return false;
5375
5376 dwarf2_read_section (objfile, section);
5377
5378 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5379
5380 const gdb_byte *addr = section->buffer;
5381
5382 bfd *const abfd = get_section_bfd_owner (section);
5383
5384 unsigned int bytes_read;
5385 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5386 addr += bytes_read;
5387
5388 map.dwarf5_is_dwarf64 = bytes_read != 4;
5389 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5390 if (bytes_read + length != section->size)
5391 {
5392 /* There may be multiple per-CU indices. */
5393 warning (_("Section .debug_names in %s length %s does not match "
5394 "section length %s, ignoring .debug_names."),
5395 filename, plongest (bytes_read + length),
5396 pulongest (section->size));
5397 return false;
5398 }
5399
5400 /* The version number. */
5401 uint16_t version = read_2_bytes (abfd, addr);
5402 addr += 2;
5403 if (version != 5)
5404 {
5405 warning (_("Section .debug_names in %s has unsupported version %d, "
5406 "ignoring .debug_names."),
5407 filename, version);
5408 return false;
5409 }
5410
5411 /* Padding. */
5412 uint16_t padding = read_2_bytes (abfd, addr);
5413 addr += 2;
5414 if (padding != 0)
5415 {
5416 warning (_("Section .debug_names in %s has unsupported padding %d, "
5417 "ignoring .debug_names."),
5418 filename, padding);
5419 return false;
5420 }
5421
5422 /* comp_unit_count - The number of CUs in the CU list. */
5423 map.cu_count = read_4_bytes (abfd, addr);
5424 addr += 4;
5425
5426 /* local_type_unit_count - The number of TUs in the local TU
5427 list. */
5428 map.tu_count = read_4_bytes (abfd, addr);
5429 addr += 4;
5430
5431 /* foreign_type_unit_count - The number of TUs in the foreign TU
5432 list. */
5433 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5434 addr += 4;
5435 if (foreign_tu_count != 0)
5436 {
5437 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5438 "ignoring .debug_names."),
5439 filename, static_cast<unsigned long> (foreign_tu_count));
5440 return false;
5441 }
5442
5443 /* bucket_count - The number of hash buckets in the hash lookup
5444 table. */
5445 map.bucket_count = read_4_bytes (abfd, addr);
5446 addr += 4;
5447
5448 /* name_count - The number of unique names in the index. */
5449 map.name_count = read_4_bytes (abfd, addr);
5450 addr += 4;
5451
5452 /* abbrev_table_size - The size in bytes of the abbreviations
5453 table. */
5454 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5455 addr += 4;
5456
5457 /* augmentation_string_size - The size in bytes of the augmentation
5458 string. This value is rounded up to a multiple of 4. */
5459 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5460 addr += 4;
5461 map.augmentation_is_gdb = ((augmentation_string_size
5462 == sizeof (dwarf5_augmentation))
5463 && memcmp (addr, dwarf5_augmentation,
5464 sizeof (dwarf5_augmentation)) == 0);
5465 augmentation_string_size += (-augmentation_string_size) & 3;
5466 addr += augmentation_string_size;
5467
5468 /* List of CUs */
5469 map.cu_table_reordered = addr;
5470 addr += map.cu_count * map.offset_size;
5471
5472 /* List of Local TUs */
5473 map.tu_table_reordered = addr;
5474 addr += map.tu_count * map.offset_size;
5475
5476 /* Hash Lookup Table */
5477 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5478 addr += map.bucket_count * 4;
5479 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5480 addr += map.name_count * 4;
5481
5482 /* Name Table */
5483 map.name_table_string_offs_reordered = addr;
5484 addr += map.name_count * map.offset_size;
5485 map.name_table_entry_offs_reordered = addr;
5486 addr += map.name_count * map.offset_size;
5487
5488 const gdb_byte *abbrev_table_start = addr;
5489 for (;;)
5490 {
5491 unsigned int bytes_read;
5492 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5493 addr += bytes_read;
5494 if (index_num == 0)
5495 break;
5496
5497 const auto insertpair
5498 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5499 if (!insertpair.second)
5500 {
5501 warning (_("Section .debug_names in %s has duplicate index %s, "
5502 "ignoring .debug_names."),
5503 filename, pulongest (index_num));
5504 return false;
5505 }
5506 mapped_debug_names::index_val &indexval = insertpair.first->second;
5507 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5508 addr += bytes_read;
5509
5510 for (;;)
5511 {
5512 mapped_debug_names::index_val::attr attr;
5513 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5514 addr += bytes_read;
5515 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5516 addr += bytes_read;
5517 if (attr.form == DW_FORM_implicit_const)
5518 {
5519 attr.implicit_const = read_signed_leb128 (abfd, addr,
5520 &bytes_read);
5521 addr += bytes_read;
5522 }
5523 if (attr.dw_idx == 0 && attr.form == 0)
5524 break;
5525 indexval.attr_vec.push_back (std::move (attr));
5526 }
5527 }
5528 if (addr != abbrev_table_start + abbrev_table_size)
5529 {
5530 warning (_("Section .debug_names in %s has abbreviation_table "
5531 "of size %zu vs. written as %u, ignoring .debug_names."),
5532 filename, addr - abbrev_table_start, abbrev_table_size);
5533 return false;
5534 }
5535 map.entry_pool = addr;
5536
5537 return true;
5538 }
5539
5540 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5541 list. */
5542
5543 static void
5544 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5545 const mapped_debug_names &map,
5546 dwarf2_section_info &section,
5547 bool is_dwz)
5548 {
5549 sect_offset sect_off_prev;
5550 for (uint32_t i = 0; i <= map.cu_count; ++i)
5551 {
5552 sect_offset sect_off_next;
5553 if (i < map.cu_count)
5554 {
5555 sect_off_next
5556 = (sect_offset) (extract_unsigned_integer
5557 (map.cu_table_reordered + i * map.offset_size,
5558 map.offset_size,
5559 map.dwarf5_byte_order));
5560 }
5561 else
5562 sect_off_next = (sect_offset) section.size;
5563 if (i >= 1)
5564 {
5565 const ULONGEST length = sect_off_next - sect_off_prev;
5566 dwarf2_per_cu_data *per_cu
5567 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5568 sect_off_prev, length);
5569 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
5570 }
5571 sect_off_prev = sect_off_next;
5572 }
5573 }
5574
5575 /* Read the CU list from the mapped index, and use it to create all
5576 the CU objects for this dwarf2_per_objfile. */
5577
5578 static void
5579 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5580 const mapped_debug_names &map,
5581 const mapped_debug_names &dwz_map)
5582 {
5583 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5584 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5585
5586 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5587 dwarf2_per_objfile->info,
5588 false /* is_dwz */);
5589
5590 if (dwz_map.cu_count == 0)
5591 return;
5592
5593 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5594 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5595 true /* is_dwz */);
5596 }
5597
5598 /* Read .debug_names. If everything went ok, initialize the "quick"
5599 elements of all the CUs and return true. Otherwise, return false. */
5600
5601 static bool
5602 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5603 {
5604 mapped_debug_names local_map (dwarf2_per_objfile);
5605 mapped_debug_names dwz_map (dwarf2_per_objfile);
5606 struct objfile *objfile = dwarf2_per_objfile->objfile;
5607
5608 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5609 &dwarf2_per_objfile->debug_names,
5610 local_map))
5611 return false;
5612
5613 /* Don't use the index if it's empty. */
5614 if (local_map.name_count == 0)
5615 return false;
5616
5617 /* If there is a .dwz file, read it so we can get its CU list as
5618 well. */
5619 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5620 if (dwz != NULL)
5621 {
5622 if (!read_debug_names_from_section (objfile,
5623 bfd_get_filename (dwz->dwz_bfd),
5624 &dwz->debug_names, dwz_map))
5625 {
5626 warning (_("could not read '.debug_names' section from %s; skipping"),
5627 bfd_get_filename (dwz->dwz_bfd));
5628 return false;
5629 }
5630 }
5631
5632 create_cus_from_debug_names (dwarf2_per_objfile, local_map, dwz_map);
5633
5634 if (local_map.tu_count != 0)
5635 {
5636 /* We can only handle a single .debug_types when we have an
5637 index. */
5638 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
5639 return false;
5640
5641 dwarf2_section_info *section = VEC_index (dwarf2_section_info_def,
5642 dwarf2_per_objfile->types, 0);
5643
5644 create_signatured_type_table_from_debug_names
5645 (dwarf2_per_objfile, local_map, section, &dwarf2_per_objfile->abbrev);
5646 }
5647
5648 create_addrmap_from_aranges (dwarf2_per_objfile,
5649 &dwarf2_per_objfile->debug_aranges);
5650
5651 dwarf2_per_objfile->debug_names_table.reset
5652 (new mapped_debug_names (dwarf2_per_objfile));
5653 *dwarf2_per_objfile->debug_names_table = std::move (local_map);
5654 dwarf2_per_objfile->using_index = 1;
5655 dwarf2_per_objfile->quick_file_names_table =
5656 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5657
5658 return true;
5659 }
5660
5661 /* Type used to manage iterating over all CUs looking for a symbol for
5662 .debug_names. */
5663
5664 class dw2_debug_names_iterator
5665 {
5666 public:
5667 /* If WANT_SPECIFIC_BLOCK is true, only look for symbols in block
5668 BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
5669 dw2_debug_names_iterator (const mapped_debug_names &map,
5670 bool want_specific_block,
5671 block_enum block_index, domain_enum domain,
5672 const char *name)
5673 : m_map (map), m_want_specific_block (want_specific_block),
5674 m_block_index (block_index), m_domain (domain),
5675 m_addr (find_vec_in_debug_names (map, name))
5676 {}
5677
5678 dw2_debug_names_iterator (const mapped_debug_names &map,
5679 search_domain search, uint32_t namei)
5680 : m_map (map),
5681 m_search (search),
5682 m_addr (find_vec_in_debug_names (map, namei))
5683 {}
5684
5685 /* Return the next matching CU or NULL if there are no more. */
5686 dwarf2_per_cu_data *next ();
5687
5688 private:
5689 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5690 const char *name);
5691 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5692 uint32_t namei);
5693
5694 /* The internalized form of .debug_names. */
5695 const mapped_debug_names &m_map;
5696
5697 /* If true, only look for symbols that match BLOCK_INDEX. */
5698 const bool m_want_specific_block = false;
5699
5700 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
5701 Unused if !WANT_SPECIFIC_BLOCK - FIRST_LOCAL_BLOCK is an invalid
5702 value. */
5703 const block_enum m_block_index = FIRST_LOCAL_BLOCK;
5704
5705 /* The kind of symbol we're looking for. */
5706 const domain_enum m_domain = UNDEF_DOMAIN;
5707 const search_domain m_search = ALL_DOMAIN;
5708
5709 /* The list of CUs from the index entry of the symbol, or NULL if
5710 not found. */
5711 const gdb_byte *m_addr;
5712 };
5713
5714 const char *
5715 mapped_debug_names::namei_to_name (uint32_t namei) const
5716 {
5717 const ULONGEST namei_string_offs
5718 = extract_unsigned_integer ((name_table_string_offs_reordered
5719 + namei * offset_size),
5720 offset_size,
5721 dwarf5_byte_order);
5722 return read_indirect_string_at_offset
5723 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
5724 }
5725
5726 /* Find a slot in .debug_names for the object named NAME. If NAME is
5727 found, return pointer to its pool data. If NAME cannot be found,
5728 return NULL. */
5729
5730 const gdb_byte *
5731 dw2_debug_names_iterator::find_vec_in_debug_names
5732 (const mapped_debug_names &map, const char *name)
5733 {
5734 int (*cmp) (const char *, const char *);
5735
5736 if (current_language->la_language == language_cplus
5737 || current_language->la_language == language_fortran
5738 || current_language->la_language == language_d)
5739 {
5740 /* NAME is already canonical. Drop any qualifiers as
5741 .debug_names does not contain any. */
5742
5743 if (strchr (name, '(') != NULL)
5744 {
5745 gdb::unique_xmalloc_ptr<char> without_params
5746 = cp_remove_params (name);
5747
5748 if (without_params != NULL)
5749 {
5750 name = without_params.get();
5751 }
5752 }
5753 }
5754
5755 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5756
5757 const uint32_t full_hash = dwarf5_djb_hash (name);
5758 uint32_t namei
5759 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5760 (map.bucket_table_reordered
5761 + (full_hash % map.bucket_count)), 4,
5762 map.dwarf5_byte_order);
5763 if (namei == 0)
5764 return NULL;
5765 --namei;
5766 if (namei >= map.name_count)
5767 {
5768 complaint (&symfile_complaints,
5769 _("Wrong .debug_names with name index %u but name_count=%u "
5770 "[in module %s]"),
5771 namei, map.name_count,
5772 objfile_name (map.dwarf2_per_objfile->objfile));
5773 return NULL;
5774 }
5775
5776 for (;;)
5777 {
5778 const uint32_t namei_full_hash
5779 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5780 (map.hash_table_reordered + namei), 4,
5781 map.dwarf5_byte_order);
5782 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5783 return NULL;
5784
5785 if (full_hash == namei_full_hash)
5786 {
5787 const char *const namei_string = map.namei_to_name (namei);
5788
5789 #if 0 /* An expensive sanity check. */
5790 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5791 {
5792 complaint (&symfile_complaints,
5793 _("Wrong .debug_names hash for string at index %u "
5794 "[in module %s]"),
5795 namei, objfile_name (dwarf2_per_objfile->objfile));
5796 return NULL;
5797 }
5798 #endif
5799
5800 if (cmp (namei_string, name) == 0)
5801 {
5802 const ULONGEST namei_entry_offs
5803 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5804 + namei * map.offset_size),
5805 map.offset_size, map.dwarf5_byte_order);
5806 return map.entry_pool + namei_entry_offs;
5807 }
5808 }
5809
5810 ++namei;
5811 if (namei >= map.name_count)
5812 return NULL;
5813 }
5814 }
5815
5816 const gdb_byte *
5817 dw2_debug_names_iterator::find_vec_in_debug_names
5818 (const mapped_debug_names &map, uint32_t namei)
5819 {
5820 if (namei >= map.name_count)
5821 {
5822 complaint (&symfile_complaints,
5823 _("Wrong .debug_names with name index %u but name_count=%u "
5824 "[in module %s]"),
5825 namei, map.name_count,
5826 objfile_name (map.dwarf2_per_objfile->objfile));
5827 return NULL;
5828 }
5829
5830 const ULONGEST namei_entry_offs
5831 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5832 + namei * map.offset_size),
5833 map.offset_size, map.dwarf5_byte_order);
5834 return map.entry_pool + namei_entry_offs;
5835 }
5836
5837 /* See dw2_debug_names_iterator. */
5838
5839 dwarf2_per_cu_data *
5840 dw2_debug_names_iterator::next ()
5841 {
5842 if (m_addr == NULL)
5843 return NULL;
5844
5845 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5846 struct objfile *objfile = dwarf2_per_objfile->objfile;
5847 bfd *const abfd = objfile->obfd;
5848
5849 again:
5850
5851 unsigned int bytes_read;
5852 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5853 m_addr += bytes_read;
5854 if (abbrev == 0)
5855 return NULL;
5856
5857 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5858 if (indexval_it == m_map.abbrev_map.cend ())
5859 {
5860 complaint (&symfile_complaints,
5861 _("Wrong .debug_names undefined abbrev code %s "
5862 "[in module %s]"),
5863 pulongest (abbrev), objfile_name (objfile));
5864 return NULL;
5865 }
5866 const mapped_debug_names::index_val &indexval = indexval_it->second;
5867 bool have_is_static = false;
5868 bool is_static;
5869 dwarf2_per_cu_data *per_cu = NULL;
5870 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5871 {
5872 ULONGEST ull;
5873 switch (attr.form)
5874 {
5875 case DW_FORM_implicit_const:
5876 ull = attr.implicit_const;
5877 break;
5878 case DW_FORM_flag_present:
5879 ull = 1;
5880 break;
5881 case DW_FORM_udata:
5882 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5883 m_addr += bytes_read;
5884 break;
5885 default:
5886 complaint (&symfile_complaints,
5887 _("Unsupported .debug_names form %s [in module %s]"),
5888 dwarf_form_name (attr.form),
5889 objfile_name (objfile));
5890 return NULL;
5891 }
5892 switch (attr.dw_idx)
5893 {
5894 case DW_IDX_compile_unit:
5895 /* Don't crash on bad data. */
5896 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5897 {
5898 complaint (&symfile_complaints,
5899 _(".debug_names entry has bad CU index %s"
5900 " [in module %s]"),
5901 pulongest (ull),
5902 objfile_name (dwarf2_per_objfile->objfile));
5903 continue;
5904 }
5905 per_cu = dwarf2_per_objfile->get_cutu (ull);
5906 break;
5907 case DW_IDX_type_unit:
5908 /* Don't crash on bad data. */
5909 if (ull >= dwarf2_per_objfile->all_type_units.size ())
5910 {
5911 complaint (&symfile_complaints,
5912 _(".debug_names entry has bad TU index %s"
5913 " [in module %s]"),
5914 pulongest (ull),
5915 objfile_name (dwarf2_per_objfile->objfile));
5916 continue;
5917 }
5918 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5919 break;
5920 case DW_IDX_GNU_internal:
5921 if (!m_map.augmentation_is_gdb)
5922 break;
5923 have_is_static = true;
5924 is_static = true;
5925 break;
5926 case DW_IDX_GNU_external:
5927 if (!m_map.augmentation_is_gdb)
5928 break;
5929 have_is_static = true;
5930 is_static = false;
5931 break;
5932 }
5933 }
5934
5935 /* Skip if already read in. */
5936 if (per_cu->v.quick->compunit_symtab)
5937 goto again;
5938
5939 /* Check static vs global. */
5940 if (have_is_static)
5941 {
5942 const bool want_static = m_block_index != GLOBAL_BLOCK;
5943 if (m_want_specific_block && want_static != is_static)
5944 goto again;
5945 }
5946
5947 /* Match dw2_symtab_iter_next, symbol_kind
5948 and debug_names::psymbol_tag. */
5949 switch (m_domain)
5950 {
5951 case VAR_DOMAIN:
5952 switch (indexval.dwarf_tag)
5953 {
5954 case DW_TAG_variable:
5955 case DW_TAG_subprogram:
5956 /* Some types are also in VAR_DOMAIN. */
5957 case DW_TAG_typedef:
5958 case DW_TAG_structure_type:
5959 break;
5960 default:
5961 goto again;
5962 }
5963 break;
5964 case STRUCT_DOMAIN:
5965 switch (indexval.dwarf_tag)
5966 {
5967 case DW_TAG_typedef:
5968 case DW_TAG_structure_type:
5969 break;
5970 default:
5971 goto again;
5972 }
5973 break;
5974 case LABEL_DOMAIN:
5975 switch (indexval.dwarf_tag)
5976 {
5977 case 0:
5978 case DW_TAG_variable:
5979 break;
5980 default:
5981 goto again;
5982 }
5983 break;
5984 default:
5985 break;
5986 }
5987
5988 /* Match dw2_expand_symtabs_matching, symbol_kind and
5989 debug_names::psymbol_tag. */
5990 switch (m_search)
5991 {
5992 case VARIABLES_DOMAIN:
5993 switch (indexval.dwarf_tag)
5994 {
5995 case DW_TAG_variable:
5996 break;
5997 default:
5998 goto again;
5999 }
6000 break;
6001 case FUNCTIONS_DOMAIN:
6002 switch (indexval.dwarf_tag)
6003 {
6004 case DW_TAG_subprogram:
6005 break;
6006 default:
6007 goto again;
6008 }
6009 break;
6010 case TYPES_DOMAIN:
6011 switch (indexval.dwarf_tag)
6012 {
6013 case DW_TAG_typedef:
6014 case DW_TAG_structure_type:
6015 break;
6016 default:
6017 goto again;
6018 }
6019 break;
6020 default:
6021 break;
6022 }
6023
6024 return per_cu;
6025 }
6026
6027 static struct compunit_symtab *
6028 dw2_debug_names_lookup_symbol (struct objfile *objfile, int block_index_int,
6029 const char *name, domain_enum domain)
6030 {
6031 const block_enum block_index = static_cast<block_enum> (block_index_int);
6032 struct dwarf2_per_objfile *dwarf2_per_objfile
6033 = get_dwarf2_per_objfile (objfile);
6034
6035 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6036 if (!mapp)
6037 {
6038 /* index is NULL if OBJF_READNOW. */
6039 return NULL;
6040 }
6041 const auto &map = *mapp;
6042
6043 dw2_debug_names_iterator iter (map, true /* want_specific_block */,
6044 block_index, domain, name);
6045
6046 struct compunit_symtab *stab_best = NULL;
6047 struct dwarf2_per_cu_data *per_cu;
6048 while ((per_cu = iter.next ()) != NULL)
6049 {
6050 struct symbol *sym, *with_opaque = NULL;
6051 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
6052 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6053 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
6054
6055 sym = block_find_symbol (block, name, domain,
6056 block_find_non_opaque_type_preferred,
6057 &with_opaque);
6058
6059 /* Some caution must be observed with overloaded functions and
6060 methods, since the index will not contain any overload
6061 information (but NAME might contain it). */
6062
6063 if (sym != NULL
6064 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
6065 return stab;
6066 if (with_opaque != NULL
6067 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
6068 stab_best = stab;
6069
6070 /* Keep looking through other CUs. */
6071 }
6072
6073 return stab_best;
6074 }
6075
6076 /* This dumps minimal information about .debug_names. It is called
6077 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6078 uses this to verify that .debug_names has been loaded. */
6079
6080 static void
6081 dw2_debug_names_dump (struct objfile *objfile)
6082 {
6083 struct dwarf2_per_objfile *dwarf2_per_objfile
6084 = get_dwarf2_per_objfile (objfile);
6085
6086 gdb_assert (dwarf2_per_objfile->using_index);
6087 printf_filtered (".debug_names:");
6088 if (dwarf2_per_objfile->debug_names_table)
6089 printf_filtered (" exists\n");
6090 else
6091 printf_filtered (" faked for \"readnow\"\n");
6092 printf_filtered ("\n");
6093 }
6094
6095 static void
6096 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6097 const char *func_name)
6098 {
6099 struct dwarf2_per_objfile *dwarf2_per_objfile
6100 = get_dwarf2_per_objfile (objfile);
6101
6102 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6103 if (dwarf2_per_objfile->debug_names_table)
6104 {
6105 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6106
6107 /* Note: It doesn't matter what we pass for block_index here. */
6108 dw2_debug_names_iterator iter (map, false /* want_specific_block */,
6109 GLOBAL_BLOCK, VAR_DOMAIN, func_name);
6110
6111 struct dwarf2_per_cu_data *per_cu;
6112 while ((per_cu = iter.next ()) != NULL)
6113 dw2_instantiate_symtab (per_cu, false);
6114 }
6115 }
6116
6117 static void
6118 dw2_debug_names_expand_symtabs_matching
6119 (struct objfile *objfile,
6120 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6121 const lookup_name_info &lookup_name,
6122 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6123 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6124 enum search_domain kind)
6125 {
6126 struct dwarf2_per_objfile *dwarf2_per_objfile
6127 = get_dwarf2_per_objfile (objfile);
6128
6129 /* debug_names_table is NULL if OBJF_READNOW. */
6130 if (!dwarf2_per_objfile->debug_names_table)
6131 return;
6132
6133 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
6134
6135 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6136
6137 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6138 symbol_matcher,
6139 kind, [&] (offset_type namei)
6140 {
6141 /* The name was matched, now expand corresponding CUs that were
6142 marked. */
6143 dw2_debug_names_iterator iter (map, kind, namei);
6144
6145 struct dwarf2_per_cu_data *per_cu;
6146 while ((per_cu = iter.next ()) != NULL)
6147 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6148 expansion_notify);
6149 });
6150 }
6151
6152 const struct quick_symbol_functions dwarf2_debug_names_functions =
6153 {
6154 dw2_has_symbols,
6155 dw2_find_last_source_symtab,
6156 dw2_forget_cached_source_info,
6157 dw2_map_symtabs_matching_filename,
6158 dw2_debug_names_lookup_symbol,
6159 dw2_print_stats,
6160 dw2_debug_names_dump,
6161 dw2_relocate,
6162 dw2_debug_names_expand_symtabs_for_function,
6163 dw2_expand_all_symtabs,
6164 dw2_expand_symtabs_with_fullname,
6165 dw2_map_matching_symbols,
6166 dw2_debug_names_expand_symtabs_matching,
6167 dw2_find_pc_sect_compunit_symtab,
6168 NULL,
6169 dw2_map_symbol_filenames
6170 };
6171
6172 /* See symfile.h. */
6173
6174 bool
6175 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
6176 {
6177 struct dwarf2_per_objfile *dwarf2_per_objfile
6178 = get_dwarf2_per_objfile (objfile);
6179
6180 /* If we're about to read full symbols, don't bother with the
6181 indices. In this case we also don't care if some other debug
6182 format is making psymtabs, because they are all about to be
6183 expanded anyway. */
6184 if ((objfile->flags & OBJF_READNOW))
6185 {
6186 dwarf2_per_objfile->using_index = 1;
6187 create_all_comp_units (dwarf2_per_objfile);
6188 create_all_type_units (dwarf2_per_objfile);
6189 dwarf2_per_objfile->quick_file_names_table
6190 = create_quick_file_names_table
6191 (dwarf2_per_objfile->all_comp_units.size ());
6192
6193 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
6194 + dwarf2_per_objfile->all_type_units.size ()); ++i)
6195 {
6196 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
6197
6198 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6199 struct dwarf2_per_cu_quick_data);
6200 }
6201
6202 /* Return 1 so that gdb sees the "quick" functions. However,
6203 these functions will be no-ops because we will have expanded
6204 all symtabs. */
6205 *index_kind = dw_index_kind::GDB_INDEX;
6206 return true;
6207 }
6208
6209 if (dwarf2_read_debug_names (dwarf2_per_objfile))
6210 {
6211 *index_kind = dw_index_kind::DEBUG_NAMES;
6212 return true;
6213 }
6214
6215 if (dwarf2_read_index (dwarf2_per_objfile))
6216 {
6217 *index_kind = dw_index_kind::GDB_INDEX;
6218 return true;
6219 }
6220
6221 return false;
6222 }
6223
6224 \f
6225
6226 /* Build a partial symbol table. */
6227
6228 void
6229 dwarf2_build_psymtabs (struct objfile *objfile)
6230 {
6231 struct dwarf2_per_objfile *dwarf2_per_objfile
6232 = get_dwarf2_per_objfile (objfile);
6233
6234 if (objfile->global_psymbols.capacity () == 0
6235 && objfile->static_psymbols.capacity () == 0)
6236 init_psymbol_list (objfile, 1024);
6237
6238 TRY
6239 {
6240 /* This isn't really ideal: all the data we allocate on the
6241 objfile's obstack is still uselessly kept around. However,
6242 freeing it seems unsafe. */
6243 psymtab_discarder psymtabs (objfile);
6244 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6245 psymtabs.keep ();
6246 }
6247 CATCH (except, RETURN_MASK_ERROR)
6248 {
6249 exception_print (gdb_stderr, except);
6250 }
6251 END_CATCH
6252 }
6253
6254 /* Return the total length of the CU described by HEADER. */
6255
6256 static unsigned int
6257 get_cu_length (const struct comp_unit_head *header)
6258 {
6259 return header->initial_length_size + header->length;
6260 }
6261
6262 /* Return TRUE if SECT_OFF is within CU_HEADER. */
6263
6264 static inline bool
6265 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
6266 {
6267 sect_offset bottom = cu_header->sect_off;
6268 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
6269
6270 return sect_off >= bottom && sect_off < top;
6271 }
6272
6273 /* Find the base address of the compilation unit for range lists and
6274 location lists. It will normally be specified by DW_AT_low_pc.
6275 In DWARF-3 draft 4, the base address could be overridden by
6276 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6277 compilation units with discontinuous ranges. */
6278
6279 static void
6280 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6281 {
6282 struct attribute *attr;
6283
6284 cu->base_known = 0;
6285 cu->base_address = 0;
6286
6287 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6288 if (attr)
6289 {
6290 cu->base_address = attr_value_as_address (attr);
6291 cu->base_known = 1;
6292 }
6293 else
6294 {
6295 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6296 if (attr)
6297 {
6298 cu->base_address = attr_value_as_address (attr);
6299 cu->base_known = 1;
6300 }
6301 }
6302 }
6303
6304 /* Read in the comp unit header information from the debug_info at info_ptr.
6305 Use rcuh_kind::COMPILE as the default type if not known by the caller.
6306 NOTE: This leaves members offset, first_die_offset to be filled in
6307 by the caller. */
6308
6309 static const gdb_byte *
6310 read_comp_unit_head (struct comp_unit_head *cu_header,
6311 const gdb_byte *info_ptr,
6312 struct dwarf2_section_info *section,
6313 rcuh_kind section_kind)
6314 {
6315 int signed_addr;
6316 unsigned int bytes_read;
6317 const char *filename = get_section_file_name (section);
6318 bfd *abfd = get_section_bfd_owner (section);
6319
6320 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6321 cu_header->initial_length_size = bytes_read;
6322 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
6323 info_ptr += bytes_read;
6324 cu_header->version = read_2_bytes (abfd, info_ptr);
6325 info_ptr += 2;
6326 if (cu_header->version < 5)
6327 switch (section_kind)
6328 {
6329 case rcuh_kind::COMPILE:
6330 cu_header->unit_type = DW_UT_compile;
6331 break;
6332 case rcuh_kind::TYPE:
6333 cu_header->unit_type = DW_UT_type;
6334 break;
6335 default:
6336 internal_error (__FILE__, __LINE__,
6337 _("read_comp_unit_head: invalid section_kind"));
6338 }
6339 else
6340 {
6341 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6342 (read_1_byte (abfd, info_ptr));
6343 info_ptr += 1;
6344 switch (cu_header->unit_type)
6345 {
6346 case DW_UT_compile:
6347 if (section_kind != rcuh_kind::COMPILE)
6348 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6349 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
6350 filename);
6351 break;
6352 case DW_UT_type:
6353 section_kind = rcuh_kind::TYPE;
6354 break;
6355 default:
6356 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6357 "(is %d, should be %d or %d) [in module %s]"),
6358 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
6359 }
6360
6361 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6362 info_ptr += 1;
6363 }
6364 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6365 cu_header,
6366 &bytes_read);
6367 info_ptr += bytes_read;
6368 if (cu_header->version < 5)
6369 {
6370 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6371 info_ptr += 1;
6372 }
6373 signed_addr = bfd_get_sign_extend_vma (abfd);
6374 if (signed_addr < 0)
6375 internal_error (__FILE__, __LINE__,
6376 _("read_comp_unit_head: dwarf from non elf file"));
6377 cu_header->signed_addr_p = signed_addr;
6378
6379 if (section_kind == rcuh_kind::TYPE)
6380 {
6381 LONGEST type_offset;
6382
6383 cu_header->signature = read_8_bytes (abfd, info_ptr);
6384 info_ptr += 8;
6385
6386 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6387 info_ptr += bytes_read;
6388 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6389 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
6390 error (_("Dwarf Error: Too big type_offset in compilation unit "
6391 "header (is %s) [in module %s]"), plongest (type_offset),
6392 filename);
6393 }
6394
6395 return info_ptr;
6396 }
6397
6398 /* Helper function that returns the proper abbrev section for
6399 THIS_CU. */
6400
6401 static struct dwarf2_section_info *
6402 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6403 {
6404 struct dwarf2_section_info *abbrev;
6405 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6406
6407 if (this_cu->is_dwz)
6408 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
6409 else
6410 abbrev = &dwarf2_per_objfile->abbrev;
6411
6412 return abbrev;
6413 }
6414
6415 /* Subroutine of read_and_check_comp_unit_head and
6416 read_and_check_type_unit_head to simplify them.
6417 Perform various error checking on the header. */
6418
6419 static void
6420 error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6421 struct comp_unit_head *header,
6422 struct dwarf2_section_info *section,
6423 struct dwarf2_section_info *abbrev_section)
6424 {
6425 const char *filename = get_section_file_name (section);
6426
6427 if (header->version < 2 || header->version > 5)
6428 error (_("Dwarf Error: wrong version in compilation unit header "
6429 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
6430 filename);
6431
6432 if (to_underlying (header->abbrev_sect_off)
6433 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
6434 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6435 "(offset %s + 6) [in module %s]"),
6436 sect_offset_str (header->abbrev_sect_off),
6437 sect_offset_str (header->sect_off),
6438 filename);
6439
6440 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
6441 avoid potential 32-bit overflow. */
6442 if (((ULONGEST) header->sect_off + get_cu_length (header))
6443 > section->size)
6444 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
6445 "(offset %s + 0) [in module %s]"),
6446 header->length, sect_offset_str (header->sect_off),
6447 filename);
6448 }
6449
6450 /* Read in a CU/TU header and perform some basic error checking.
6451 The contents of the header are stored in HEADER.
6452 The result is a pointer to the start of the first DIE. */
6453
6454 static const gdb_byte *
6455 read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6456 struct comp_unit_head *header,
6457 struct dwarf2_section_info *section,
6458 struct dwarf2_section_info *abbrev_section,
6459 const gdb_byte *info_ptr,
6460 rcuh_kind section_kind)
6461 {
6462 const gdb_byte *beg_of_comp_unit = info_ptr;
6463
6464 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
6465
6466 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
6467
6468 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
6469
6470 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6471 abbrev_section);
6472
6473 return info_ptr;
6474 }
6475
6476 /* Fetch the abbreviation table offset from a comp or type unit header. */
6477
6478 static sect_offset
6479 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6480 struct dwarf2_section_info *section,
6481 sect_offset sect_off)
6482 {
6483 bfd *abfd = get_section_bfd_owner (section);
6484 const gdb_byte *info_ptr;
6485 unsigned int initial_length_size, offset_size;
6486 uint16_t version;
6487
6488 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
6489 info_ptr = section->buffer + to_underlying (sect_off);
6490 read_initial_length (abfd, info_ptr, &initial_length_size);
6491 offset_size = initial_length_size == 4 ? 4 : 8;
6492 info_ptr += initial_length_size;
6493
6494 version = read_2_bytes (abfd, info_ptr);
6495 info_ptr += 2;
6496 if (version >= 5)
6497 {
6498 /* Skip unit type and address size. */
6499 info_ptr += 2;
6500 }
6501
6502 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
6503 }
6504
6505 /* Allocate a new partial symtab for file named NAME and mark this new
6506 partial symtab as being an include of PST. */
6507
6508 static void
6509 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
6510 struct objfile *objfile)
6511 {
6512 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6513
6514 if (!IS_ABSOLUTE_PATH (subpst->filename))
6515 {
6516 /* It shares objfile->objfile_obstack. */
6517 subpst->dirname = pst->dirname;
6518 }
6519
6520 subpst->textlow = 0;
6521 subpst->texthigh = 0;
6522
6523 subpst->dependencies
6524 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
6525 subpst->dependencies[0] = pst;
6526 subpst->number_of_dependencies = 1;
6527
6528 subpst->globals_offset = 0;
6529 subpst->n_global_syms = 0;
6530 subpst->statics_offset = 0;
6531 subpst->n_static_syms = 0;
6532 subpst->compunit_symtab = NULL;
6533 subpst->read_symtab = pst->read_symtab;
6534 subpst->readin = 0;
6535
6536 /* No private part is necessary for include psymtabs. This property
6537 can be used to differentiate between such include psymtabs and
6538 the regular ones. */
6539 subpst->read_symtab_private = NULL;
6540 }
6541
6542 /* Read the Line Number Program data and extract the list of files
6543 included by the source file represented by PST. Build an include
6544 partial symtab for each of these included files. */
6545
6546 static void
6547 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6548 struct die_info *die,
6549 struct partial_symtab *pst)
6550 {
6551 line_header_up lh;
6552 struct attribute *attr;
6553
6554 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6555 if (attr)
6556 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6557 if (lh == NULL)
6558 return; /* No linetable, so no includes. */
6559
6560 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
6561 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
6562 }
6563
6564 static hashval_t
6565 hash_signatured_type (const void *item)
6566 {
6567 const struct signatured_type *sig_type
6568 = (const struct signatured_type *) item;
6569
6570 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6571 return sig_type->signature;
6572 }
6573
6574 static int
6575 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6576 {
6577 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6578 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6579
6580 return lhs->signature == rhs->signature;
6581 }
6582
6583 /* Allocate a hash table for signatured types. */
6584
6585 static htab_t
6586 allocate_signatured_type_table (struct objfile *objfile)
6587 {
6588 return htab_create_alloc_ex (41,
6589 hash_signatured_type,
6590 eq_signatured_type,
6591 NULL,
6592 &objfile->objfile_obstack,
6593 hashtab_obstack_allocate,
6594 dummy_obstack_deallocate);
6595 }
6596
6597 /* A helper function to add a signatured type CU to a table. */
6598
6599 static int
6600 add_signatured_type_cu_to_table (void **slot, void *datum)
6601 {
6602 struct signatured_type *sigt = (struct signatured_type *) *slot;
6603 std::vector<signatured_type *> *all_type_units
6604 = (std::vector<signatured_type *> *) datum;
6605
6606 all_type_units->push_back (sigt);
6607
6608 return 1;
6609 }
6610
6611 /* A helper for create_debug_types_hash_table. Read types from SECTION
6612 and fill them into TYPES_HTAB. It will process only type units,
6613 therefore DW_UT_type. */
6614
6615 static void
6616 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6617 struct dwo_file *dwo_file,
6618 dwarf2_section_info *section, htab_t &types_htab,
6619 rcuh_kind section_kind)
6620 {
6621 struct objfile *objfile = dwarf2_per_objfile->objfile;
6622 struct dwarf2_section_info *abbrev_section;
6623 bfd *abfd;
6624 const gdb_byte *info_ptr, *end_ptr;
6625
6626 abbrev_section = (dwo_file != NULL
6627 ? &dwo_file->sections.abbrev
6628 : &dwarf2_per_objfile->abbrev);
6629
6630 if (dwarf_read_debug)
6631 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6632 get_section_name (section),
6633 get_section_file_name (abbrev_section));
6634
6635 dwarf2_read_section (objfile, section);
6636 info_ptr = section->buffer;
6637
6638 if (info_ptr == NULL)
6639 return;
6640
6641 /* We can't set abfd until now because the section may be empty or
6642 not present, in which case the bfd is unknown. */
6643 abfd = get_section_bfd_owner (section);
6644
6645 /* We don't use init_cutu_and_read_dies_simple, or some such, here
6646 because we don't need to read any dies: the signature is in the
6647 header. */
6648
6649 end_ptr = info_ptr + section->size;
6650 while (info_ptr < end_ptr)
6651 {
6652 struct signatured_type *sig_type;
6653 struct dwo_unit *dwo_tu;
6654 void **slot;
6655 const gdb_byte *ptr = info_ptr;
6656 struct comp_unit_head header;
6657 unsigned int length;
6658
6659 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6660
6661 /* Initialize it due to a false compiler warning. */
6662 header.signature = -1;
6663 header.type_cu_offset_in_tu = (cu_offset) -1;
6664
6665 /* We need to read the type's signature in order to build the hash
6666 table, but we don't need anything else just yet. */
6667
6668 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6669 abbrev_section, ptr, section_kind);
6670
6671 length = get_cu_length (&header);
6672
6673 /* Skip dummy type units. */
6674 if (ptr >= info_ptr + length
6675 || peek_abbrev_code (abfd, ptr) == 0
6676 || header.unit_type != DW_UT_type)
6677 {
6678 info_ptr += length;
6679 continue;
6680 }
6681
6682 if (types_htab == NULL)
6683 {
6684 if (dwo_file)
6685 types_htab = allocate_dwo_unit_table (objfile);
6686 else
6687 types_htab = allocate_signatured_type_table (objfile);
6688 }
6689
6690 if (dwo_file)
6691 {
6692 sig_type = NULL;
6693 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6694 struct dwo_unit);
6695 dwo_tu->dwo_file = dwo_file;
6696 dwo_tu->signature = header.signature;
6697 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6698 dwo_tu->section = section;
6699 dwo_tu->sect_off = sect_off;
6700 dwo_tu->length = length;
6701 }
6702 else
6703 {
6704 /* N.B.: type_offset is not usable if this type uses a DWO file.
6705 The real type_offset is in the DWO file. */
6706 dwo_tu = NULL;
6707 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6708 struct signatured_type);
6709 sig_type->signature = header.signature;
6710 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6711 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6712 sig_type->per_cu.is_debug_types = 1;
6713 sig_type->per_cu.section = section;
6714 sig_type->per_cu.sect_off = sect_off;
6715 sig_type->per_cu.length = length;
6716 }
6717
6718 slot = htab_find_slot (types_htab,
6719 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6720 INSERT);
6721 gdb_assert (slot != NULL);
6722 if (*slot != NULL)
6723 {
6724 sect_offset dup_sect_off;
6725
6726 if (dwo_file)
6727 {
6728 const struct dwo_unit *dup_tu
6729 = (const struct dwo_unit *) *slot;
6730
6731 dup_sect_off = dup_tu->sect_off;
6732 }
6733 else
6734 {
6735 const struct signatured_type *dup_tu
6736 = (const struct signatured_type *) *slot;
6737
6738 dup_sect_off = dup_tu->per_cu.sect_off;
6739 }
6740
6741 complaint (&symfile_complaints,
6742 _("debug type entry at offset %s is duplicate to"
6743 " the entry at offset %s, signature %s"),
6744 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6745 hex_string (header.signature));
6746 }
6747 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6748
6749 if (dwarf_read_debug > 1)
6750 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6751 sect_offset_str (sect_off),
6752 hex_string (header.signature));
6753
6754 info_ptr += length;
6755 }
6756 }
6757
6758 /* Create the hash table of all entries in the .debug_types
6759 (or .debug_types.dwo) section(s).
6760 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6761 otherwise it is NULL.
6762
6763 The result is a pointer to the hash table or NULL if there are no types.
6764
6765 Note: This function processes DWO files only, not DWP files. */
6766
6767 static void
6768 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6769 struct dwo_file *dwo_file,
6770 VEC (dwarf2_section_info_def) *types,
6771 htab_t &types_htab)
6772 {
6773 int ix;
6774 struct dwarf2_section_info *section;
6775
6776 if (VEC_empty (dwarf2_section_info_def, types))
6777 return;
6778
6779 for (ix = 0;
6780 VEC_iterate (dwarf2_section_info_def, types, ix, section);
6781 ++ix)
6782 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, section,
6783 types_htab, rcuh_kind::TYPE);
6784 }
6785
6786 /* Create the hash table of all entries in the .debug_types section,
6787 and initialize all_type_units.
6788 The result is zero if there is an error (e.g. missing .debug_types section),
6789 otherwise non-zero. */
6790
6791 static int
6792 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6793 {
6794 htab_t types_htab = NULL;
6795
6796 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6797 &dwarf2_per_objfile->info, types_htab,
6798 rcuh_kind::COMPILE);
6799 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6800 dwarf2_per_objfile->types, types_htab);
6801 if (types_htab == NULL)
6802 {
6803 dwarf2_per_objfile->signatured_types = NULL;
6804 return 0;
6805 }
6806
6807 dwarf2_per_objfile->signatured_types = types_htab;
6808
6809 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6810 dwarf2_per_objfile->all_type_units.reserve (htab_elements (types_htab));
6811
6812 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table,
6813 &dwarf2_per_objfile->all_type_units);
6814
6815 return 1;
6816 }
6817
6818 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6819 If SLOT is non-NULL, it is the entry to use in the hash table.
6820 Otherwise we find one. */
6821
6822 static struct signatured_type *
6823 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6824 void **slot)
6825 {
6826 struct objfile *objfile = dwarf2_per_objfile->objfile;
6827
6828 if (dwarf2_per_objfile->all_type_units.size ()
6829 == dwarf2_per_objfile->all_type_units.capacity ())
6830 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6831
6832 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6833 struct signatured_type);
6834
6835 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6836 sig_type->signature = sig;
6837 sig_type->per_cu.is_debug_types = 1;
6838 if (dwarf2_per_objfile->using_index)
6839 {
6840 sig_type->per_cu.v.quick =
6841 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6842 struct dwarf2_per_cu_quick_data);
6843 }
6844
6845 if (slot == NULL)
6846 {
6847 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6848 sig_type, INSERT);
6849 }
6850 gdb_assert (*slot == NULL);
6851 *slot = sig_type;
6852 /* The rest of sig_type must be filled in by the caller. */
6853 return sig_type;
6854 }
6855
6856 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6857 Fill in SIG_ENTRY with DWO_ENTRY. */
6858
6859 static void
6860 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6861 struct signatured_type *sig_entry,
6862 struct dwo_unit *dwo_entry)
6863 {
6864 /* Make sure we're not clobbering something we don't expect to. */
6865 gdb_assert (! sig_entry->per_cu.queued);
6866 gdb_assert (sig_entry->per_cu.cu == NULL);
6867 if (dwarf2_per_objfile->using_index)
6868 {
6869 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6870 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6871 }
6872 else
6873 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6874 gdb_assert (sig_entry->signature == dwo_entry->signature);
6875 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6876 gdb_assert (sig_entry->type_unit_group == NULL);
6877 gdb_assert (sig_entry->dwo_unit == NULL);
6878
6879 sig_entry->per_cu.section = dwo_entry->section;
6880 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6881 sig_entry->per_cu.length = dwo_entry->length;
6882 sig_entry->per_cu.reading_dwo_directly = 1;
6883 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6884 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6885 sig_entry->dwo_unit = dwo_entry;
6886 }
6887
6888 /* Subroutine of lookup_signatured_type.
6889 If we haven't read the TU yet, create the signatured_type data structure
6890 for a TU to be read in directly from a DWO file, bypassing the stub.
6891 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6892 using .gdb_index, then when reading a CU we want to stay in the DWO file
6893 containing that CU. Otherwise we could end up reading several other DWO
6894 files (due to comdat folding) to process the transitive closure of all the
6895 mentioned TUs, and that can be slow. The current DWO file will have every
6896 type signature that it needs.
6897 We only do this for .gdb_index because in the psymtab case we already have
6898 to read all the DWOs to build the type unit groups. */
6899
6900 static struct signatured_type *
6901 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6902 {
6903 struct dwarf2_per_objfile *dwarf2_per_objfile
6904 = cu->per_cu->dwarf2_per_objfile;
6905 struct objfile *objfile = dwarf2_per_objfile->objfile;
6906 struct dwo_file *dwo_file;
6907 struct dwo_unit find_dwo_entry, *dwo_entry;
6908 struct signatured_type find_sig_entry, *sig_entry;
6909 void **slot;
6910
6911 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6912
6913 /* If TU skeletons have been removed then we may not have read in any
6914 TUs yet. */
6915 if (dwarf2_per_objfile->signatured_types == NULL)
6916 {
6917 dwarf2_per_objfile->signatured_types
6918 = allocate_signatured_type_table (objfile);
6919 }
6920
6921 /* We only ever need to read in one copy of a signatured type.
6922 Use the global signatured_types array to do our own comdat-folding
6923 of types. If this is the first time we're reading this TU, and
6924 the TU has an entry in .gdb_index, replace the recorded data from
6925 .gdb_index with this TU. */
6926
6927 find_sig_entry.signature = sig;
6928 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6929 &find_sig_entry, INSERT);
6930 sig_entry = (struct signatured_type *) *slot;
6931
6932 /* We can get here with the TU already read, *or* in the process of being
6933 read. Don't reassign the global entry to point to this DWO if that's
6934 the case. Also note that if the TU is already being read, it may not
6935 have come from a DWO, the program may be a mix of Fission-compiled
6936 code and non-Fission-compiled code. */
6937
6938 /* Have we already tried to read this TU?
6939 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6940 needn't exist in the global table yet). */
6941 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6942 return sig_entry;
6943
6944 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6945 dwo_unit of the TU itself. */
6946 dwo_file = cu->dwo_unit->dwo_file;
6947
6948 /* Ok, this is the first time we're reading this TU. */
6949 if (dwo_file->tus == NULL)
6950 return NULL;
6951 find_dwo_entry.signature = sig;
6952 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
6953 if (dwo_entry == NULL)
6954 return NULL;
6955
6956 /* If the global table doesn't have an entry for this TU, add one. */
6957 if (sig_entry == NULL)
6958 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6959
6960 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6961 sig_entry->per_cu.tu_read = 1;
6962 return sig_entry;
6963 }
6964
6965 /* Subroutine of lookup_signatured_type.
6966 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6967 then try the DWP file. If the TU stub (skeleton) has been removed then
6968 it won't be in .gdb_index. */
6969
6970 static struct signatured_type *
6971 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6972 {
6973 struct dwarf2_per_objfile *dwarf2_per_objfile
6974 = cu->per_cu->dwarf2_per_objfile;
6975 struct objfile *objfile = dwarf2_per_objfile->objfile;
6976 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
6977 struct dwo_unit *dwo_entry;
6978 struct signatured_type find_sig_entry, *sig_entry;
6979 void **slot;
6980
6981 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6982 gdb_assert (dwp_file != NULL);
6983
6984 /* If TU skeletons have been removed then we may not have read in any
6985 TUs yet. */
6986 if (dwarf2_per_objfile->signatured_types == NULL)
6987 {
6988 dwarf2_per_objfile->signatured_types
6989 = allocate_signatured_type_table (objfile);
6990 }
6991
6992 find_sig_entry.signature = sig;
6993 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6994 &find_sig_entry, INSERT);
6995 sig_entry = (struct signatured_type *) *slot;
6996
6997 /* Have we already tried to read this TU?
6998 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6999 needn't exist in the global table yet). */
7000 if (sig_entry != NULL)
7001 return sig_entry;
7002
7003 if (dwp_file->tus == NULL)
7004 return NULL;
7005 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
7006 sig, 1 /* is_debug_types */);
7007 if (dwo_entry == NULL)
7008 return NULL;
7009
7010 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7011 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7012
7013 return sig_entry;
7014 }
7015
7016 /* Lookup a signature based type for DW_FORM_ref_sig8.
7017 Returns NULL if signature SIG is not present in the table.
7018 It is up to the caller to complain about this. */
7019
7020 static struct signatured_type *
7021 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7022 {
7023 struct dwarf2_per_objfile *dwarf2_per_objfile
7024 = cu->per_cu->dwarf2_per_objfile;
7025
7026 if (cu->dwo_unit
7027 && dwarf2_per_objfile->using_index)
7028 {
7029 /* We're in a DWO/DWP file, and we're using .gdb_index.
7030 These cases require special processing. */
7031 if (get_dwp_file (dwarf2_per_objfile) == NULL)
7032 return lookup_dwo_signatured_type (cu, sig);
7033 else
7034 return lookup_dwp_signatured_type (cu, sig);
7035 }
7036 else
7037 {
7038 struct signatured_type find_entry, *entry;
7039
7040 if (dwarf2_per_objfile->signatured_types == NULL)
7041 return NULL;
7042 find_entry.signature = sig;
7043 entry = ((struct signatured_type *)
7044 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
7045 return entry;
7046 }
7047 }
7048 \f
7049 /* Low level DIE reading support. */
7050
7051 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7052
7053 static void
7054 init_cu_die_reader (struct die_reader_specs *reader,
7055 struct dwarf2_cu *cu,
7056 struct dwarf2_section_info *section,
7057 struct dwo_file *dwo_file,
7058 struct abbrev_table *abbrev_table)
7059 {
7060 gdb_assert (section->readin && section->buffer != NULL);
7061 reader->abfd = get_section_bfd_owner (section);
7062 reader->cu = cu;
7063 reader->dwo_file = dwo_file;
7064 reader->die_section = section;
7065 reader->buffer = section->buffer;
7066 reader->buffer_end = section->buffer + section->size;
7067 reader->comp_dir = NULL;
7068 reader->abbrev_table = abbrev_table;
7069 }
7070
7071 /* Subroutine of init_cutu_and_read_dies to simplify it.
7072 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7073 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7074 already.
7075
7076 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7077 from it to the DIE in the DWO. If NULL we are skipping the stub.
7078 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7079 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
7080 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7081 STUB_COMP_DIR may be non-NULL.
7082 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7083 are filled in with the info of the DIE from the DWO file.
7084 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7085 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7086 kept around for at least as long as *RESULT_READER.
7087
7088 The result is non-zero if a valid (non-dummy) DIE was found. */
7089
7090 static int
7091 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7092 struct dwo_unit *dwo_unit,
7093 struct die_info *stub_comp_unit_die,
7094 const char *stub_comp_dir,
7095 struct die_reader_specs *result_reader,
7096 const gdb_byte **result_info_ptr,
7097 struct die_info **result_comp_unit_die,
7098 int *result_has_children,
7099 abbrev_table_up *result_dwo_abbrev_table)
7100 {
7101 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7102 struct objfile *objfile = dwarf2_per_objfile->objfile;
7103 struct dwarf2_cu *cu = this_cu->cu;
7104 bfd *abfd;
7105 const gdb_byte *begin_info_ptr, *info_ptr;
7106 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7107 int i,num_extra_attrs;
7108 struct dwarf2_section_info *dwo_abbrev_section;
7109 struct attribute *attr;
7110 struct die_info *comp_unit_die;
7111
7112 /* At most one of these may be provided. */
7113 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
7114
7115 /* These attributes aren't processed until later:
7116 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
7117 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7118 referenced later. However, these attributes are found in the stub
7119 which we won't have later. In order to not impose this complication
7120 on the rest of the code, we read them here and copy them to the
7121 DWO CU/TU die. */
7122
7123 stmt_list = NULL;
7124 low_pc = NULL;
7125 high_pc = NULL;
7126 ranges = NULL;
7127 comp_dir = NULL;
7128
7129 if (stub_comp_unit_die != NULL)
7130 {
7131 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7132 DWO file. */
7133 if (! this_cu->is_debug_types)
7134 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7135 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7136 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7137 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7138 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7139
7140 /* There should be a DW_AT_addr_base attribute here (if needed).
7141 We need the value before we can process DW_FORM_GNU_addr_index. */
7142 cu->addr_base = 0;
7143 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7144 if (attr)
7145 cu->addr_base = DW_UNSND (attr);
7146
7147 /* There should be a DW_AT_ranges_base attribute here (if needed).
7148 We need the value before we can process DW_AT_ranges. */
7149 cu->ranges_base = 0;
7150 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7151 if (attr)
7152 cu->ranges_base = DW_UNSND (attr);
7153 }
7154 else if (stub_comp_dir != NULL)
7155 {
7156 /* Reconstruct the comp_dir attribute to simplify the code below. */
7157 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
7158 comp_dir->name = DW_AT_comp_dir;
7159 comp_dir->form = DW_FORM_string;
7160 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7161 DW_STRING (comp_dir) = stub_comp_dir;
7162 }
7163
7164 /* Set up for reading the DWO CU/TU. */
7165 cu->dwo_unit = dwo_unit;
7166 dwarf2_section_info *section = dwo_unit->section;
7167 dwarf2_read_section (objfile, section);
7168 abfd = get_section_bfd_owner (section);
7169 begin_info_ptr = info_ptr = (section->buffer
7170 + to_underlying (dwo_unit->sect_off));
7171 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7172
7173 if (this_cu->is_debug_types)
7174 {
7175 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7176
7177 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7178 &cu->header, section,
7179 dwo_abbrev_section,
7180 info_ptr, rcuh_kind::TYPE);
7181 /* This is not an assert because it can be caused by bad debug info. */
7182 if (sig_type->signature != cu->header.signature)
7183 {
7184 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7185 " TU at offset %s [in module %s]"),
7186 hex_string (sig_type->signature),
7187 hex_string (cu->header.signature),
7188 sect_offset_str (dwo_unit->sect_off),
7189 bfd_get_filename (abfd));
7190 }
7191 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7192 /* For DWOs coming from DWP files, we don't know the CU length
7193 nor the type's offset in the TU until now. */
7194 dwo_unit->length = get_cu_length (&cu->header);
7195 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7196
7197 /* Establish the type offset that can be used to lookup the type.
7198 For DWO files, we don't know it until now. */
7199 sig_type->type_offset_in_section
7200 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7201 }
7202 else
7203 {
7204 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7205 &cu->header, section,
7206 dwo_abbrev_section,
7207 info_ptr, rcuh_kind::COMPILE);
7208 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7209 /* For DWOs coming from DWP files, we don't know the CU length
7210 until now. */
7211 dwo_unit->length = get_cu_length (&cu->header);
7212 }
7213
7214 *result_dwo_abbrev_table
7215 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7216 cu->header.abbrev_sect_off);
7217 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7218 result_dwo_abbrev_table->get ());
7219
7220 /* Read in the die, but leave space to copy over the attributes
7221 from the stub. This has the benefit of simplifying the rest of
7222 the code - all the work to maintain the illusion of a single
7223 DW_TAG_{compile,type}_unit DIE is done here. */
7224 num_extra_attrs = ((stmt_list != NULL)
7225 + (low_pc != NULL)
7226 + (high_pc != NULL)
7227 + (ranges != NULL)
7228 + (comp_dir != NULL));
7229 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7230 result_has_children, num_extra_attrs);
7231
7232 /* Copy over the attributes from the stub to the DIE we just read in. */
7233 comp_unit_die = *result_comp_unit_die;
7234 i = comp_unit_die->num_attrs;
7235 if (stmt_list != NULL)
7236 comp_unit_die->attrs[i++] = *stmt_list;
7237 if (low_pc != NULL)
7238 comp_unit_die->attrs[i++] = *low_pc;
7239 if (high_pc != NULL)
7240 comp_unit_die->attrs[i++] = *high_pc;
7241 if (ranges != NULL)
7242 comp_unit_die->attrs[i++] = *ranges;
7243 if (comp_dir != NULL)
7244 comp_unit_die->attrs[i++] = *comp_dir;
7245 comp_unit_die->num_attrs += num_extra_attrs;
7246
7247 if (dwarf_die_debug)
7248 {
7249 fprintf_unfiltered (gdb_stdlog,
7250 "Read die from %s@0x%x of %s:\n",
7251 get_section_name (section),
7252 (unsigned) (begin_info_ptr - section->buffer),
7253 bfd_get_filename (abfd));
7254 dump_die (comp_unit_die, dwarf_die_debug);
7255 }
7256
7257 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7258 TUs by skipping the stub and going directly to the entry in the DWO file.
7259 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7260 to get it via circuitous means. Blech. */
7261 if (comp_dir != NULL)
7262 result_reader->comp_dir = DW_STRING (comp_dir);
7263
7264 /* Skip dummy compilation units. */
7265 if (info_ptr >= begin_info_ptr + dwo_unit->length
7266 || peek_abbrev_code (abfd, info_ptr) == 0)
7267 return 0;
7268
7269 *result_info_ptr = info_ptr;
7270 return 1;
7271 }
7272
7273 /* Subroutine of init_cutu_and_read_dies to simplify it.
7274 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7275 Returns NULL if the specified DWO unit cannot be found. */
7276
7277 static struct dwo_unit *
7278 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7279 struct die_info *comp_unit_die)
7280 {
7281 struct dwarf2_cu *cu = this_cu->cu;
7282 ULONGEST signature;
7283 struct dwo_unit *dwo_unit;
7284 const char *comp_dir, *dwo_name;
7285
7286 gdb_assert (cu != NULL);
7287
7288 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7289 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7290 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7291
7292 if (this_cu->is_debug_types)
7293 {
7294 struct signatured_type *sig_type;
7295
7296 /* Since this_cu is the first member of struct signatured_type,
7297 we can go from a pointer to one to a pointer to the other. */
7298 sig_type = (struct signatured_type *) this_cu;
7299 signature = sig_type->signature;
7300 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7301 }
7302 else
7303 {
7304 struct attribute *attr;
7305
7306 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7307 if (! attr)
7308 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7309 " [in module %s]"),
7310 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
7311 signature = DW_UNSND (attr);
7312 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7313 signature);
7314 }
7315
7316 return dwo_unit;
7317 }
7318
7319 /* Subroutine of init_cutu_and_read_dies to simplify it.
7320 See it for a description of the parameters.
7321 Read a TU directly from a DWO file, bypassing the stub. */
7322
7323 static void
7324 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7325 int use_existing_cu, int keep,
7326 die_reader_func_ftype *die_reader_func,
7327 void *data)
7328 {
7329 std::unique_ptr<dwarf2_cu> new_cu;
7330 struct signatured_type *sig_type;
7331 struct die_reader_specs reader;
7332 const gdb_byte *info_ptr;
7333 struct die_info *comp_unit_die;
7334 int has_children;
7335 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7336
7337 /* Verify we can do the following downcast, and that we have the
7338 data we need. */
7339 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7340 sig_type = (struct signatured_type *) this_cu;
7341 gdb_assert (sig_type->dwo_unit != NULL);
7342
7343 if (use_existing_cu && this_cu->cu != NULL)
7344 {
7345 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
7346 /* There's no need to do the rereading_dwo_cu handling that
7347 init_cutu_and_read_dies does since we don't read the stub. */
7348 }
7349 else
7350 {
7351 /* If !use_existing_cu, this_cu->cu must be NULL. */
7352 gdb_assert (this_cu->cu == NULL);
7353 new_cu.reset (new dwarf2_cu (this_cu));
7354 }
7355
7356 /* A future optimization, if needed, would be to use an existing
7357 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7358 could share abbrev tables. */
7359
7360 /* The abbreviation table used by READER, this must live at least as long as
7361 READER. */
7362 abbrev_table_up dwo_abbrev_table;
7363
7364 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
7365 NULL /* stub_comp_unit_die */,
7366 sig_type->dwo_unit->dwo_file->comp_dir,
7367 &reader, &info_ptr,
7368 &comp_unit_die, &has_children,
7369 &dwo_abbrev_table) == 0)
7370 {
7371 /* Dummy die. */
7372 return;
7373 }
7374
7375 /* All the "real" work is done here. */
7376 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7377
7378 /* This duplicates the code in init_cutu_and_read_dies,
7379 but the alternative is making the latter more complex.
7380 This function is only for the special case of using DWO files directly:
7381 no point in overly complicating the general case just to handle this. */
7382 if (new_cu != NULL && keep)
7383 {
7384 /* Link this CU into read_in_chain. */
7385 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7386 dwarf2_per_objfile->read_in_chain = this_cu;
7387 /* The chain owns it now. */
7388 new_cu.release ();
7389 }
7390 }
7391
7392 /* Initialize a CU (or TU) and read its DIEs.
7393 If the CU defers to a DWO file, read the DWO file as well.
7394
7395 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7396 Otherwise the table specified in the comp unit header is read in and used.
7397 This is an optimization for when we already have the abbrev table.
7398
7399 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7400 Otherwise, a new CU is allocated with xmalloc.
7401
7402 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7403 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7404
7405 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7406 linker) then DIE_READER_FUNC will not get called. */
7407
7408 static void
7409 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
7410 struct abbrev_table *abbrev_table,
7411 int use_existing_cu, int keep,
7412 bool skip_partial,
7413 die_reader_func_ftype *die_reader_func,
7414 void *data)
7415 {
7416 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7417 struct objfile *objfile = dwarf2_per_objfile->objfile;
7418 struct dwarf2_section_info *section = this_cu->section;
7419 bfd *abfd = get_section_bfd_owner (section);
7420 struct dwarf2_cu *cu;
7421 const gdb_byte *begin_info_ptr, *info_ptr;
7422 struct die_reader_specs reader;
7423 struct die_info *comp_unit_die;
7424 int has_children;
7425 struct attribute *attr;
7426 struct signatured_type *sig_type = NULL;
7427 struct dwarf2_section_info *abbrev_section;
7428 /* Non-zero if CU currently points to a DWO file and we need to
7429 reread it. When this happens we need to reread the skeleton die
7430 before we can reread the DWO file (this only applies to CUs, not TUs). */
7431 int rereading_dwo_cu = 0;
7432
7433 if (dwarf_die_debug)
7434 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7435 this_cu->is_debug_types ? "type" : "comp",
7436 sect_offset_str (this_cu->sect_off));
7437
7438 if (use_existing_cu)
7439 gdb_assert (keep);
7440
7441 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7442 file (instead of going through the stub), short-circuit all of this. */
7443 if (this_cu->reading_dwo_directly)
7444 {
7445 /* Narrow down the scope of possibilities to have to understand. */
7446 gdb_assert (this_cu->is_debug_types);
7447 gdb_assert (abbrev_table == NULL);
7448 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7449 die_reader_func, data);
7450 return;
7451 }
7452
7453 /* This is cheap if the section is already read in. */
7454 dwarf2_read_section (objfile, section);
7455
7456 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7457
7458 abbrev_section = get_abbrev_section_for_cu (this_cu);
7459
7460 std::unique_ptr<dwarf2_cu> new_cu;
7461 if (use_existing_cu && this_cu->cu != NULL)
7462 {
7463 cu = this_cu->cu;
7464 /* If this CU is from a DWO file we need to start over, we need to
7465 refetch the attributes from the skeleton CU.
7466 This could be optimized by retrieving those attributes from when we
7467 were here the first time: the previous comp_unit_die was stored in
7468 comp_unit_obstack. But there's no data yet that we need this
7469 optimization. */
7470 if (cu->dwo_unit != NULL)
7471 rereading_dwo_cu = 1;
7472 }
7473 else
7474 {
7475 /* If !use_existing_cu, this_cu->cu must be NULL. */
7476 gdb_assert (this_cu->cu == NULL);
7477 new_cu.reset (new dwarf2_cu (this_cu));
7478 cu = new_cu.get ();
7479 }
7480
7481 /* Get the header. */
7482 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7483 {
7484 /* We already have the header, there's no need to read it in again. */
7485 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7486 }
7487 else
7488 {
7489 if (this_cu->is_debug_types)
7490 {
7491 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7492 &cu->header, section,
7493 abbrev_section, info_ptr,
7494 rcuh_kind::TYPE);
7495
7496 /* Since per_cu is the first member of struct signatured_type,
7497 we can go from a pointer to one to a pointer to the other. */
7498 sig_type = (struct signatured_type *) this_cu;
7499 gdb_assert (sig_type->signature == cu->header.signature);
7500 gdb_assert (sig_type->type_offset_in_tu
7501 == cu->header.type_cu_offset_in_tu);
7502 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7503
7504 /* LENGTH has not been set yet for type units if we're
7505 using .gdb_index. */
7506 this_cu->length = get_cu_length (&cu->header);
7507
7508 /* Establish the type offset that can be used to lookup the type. */
7509 sig_type->type_offset_in_section =
7510 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7511
7512 this_cu->dwarf_version = cu->header.version;
7513 }
7514 else
7515 {
7516 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7517 &cu->header, section,
7518 abbrev_section,
7519 info_ptr,
7520 rcuh_kind::COMPILE);
7521
7522 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7523 gdb_assert (this_cu->length == get_cu_length (&cu->header));
7524 this_cu->dwarf_version = cu->header.version;
7525 }
7526 }
7527
7528 /* Skip dummy compilation units. */
7529 if (info_ptr >= begin_info_ptr + this_cu->length
7530 || peek_abbrev_code (abfd, info_ptr) == 0)
7531 return;
7532
7533 /* If we don't have them yet, read the abbrevs for this compilation unit.
7534 And if we need to read them now, make sure they're freed when we're
7535 done (own the table through ABBREV_TABLE_HOLDER). */
7536 abbrev_table_up abbrev_table_holder;
7537 if (abbrev_table != NULL)
7538 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7539 else
7540 {
7541 abbrev_table_holder
7542 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7543 cu->header.abbrev_sect_off);
7544 abbrev_table = abbrev_table_holder.get ();
7545 }
7546
7547 /* Read the top level CU/TU die. */
7548 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
7549 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7550
7551 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7552 return;
7553
7554 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7555 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7556 table from the DWO file and pass the ownership over to us. It will be
7557 referenced from READER, so we must make sure to free it after we're done
7558 with READER.
7559
7560 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7561 DWO CU, that this test will fail (the attribute will not be present). */
7562 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7563 abbrev_table_up dwo_abbrev_table;
7564 if (attr)
7565 {
7566 struct dwo_unit *dwo_unit;
7567 struct die_info *dwo_comp_unit_die;
7568
7569 if (has_children)
7570 {
7571 complaint (&symfile_complaints,
7572 _("compilation unit with DW_AT_GNU_dwo_name"
7573 " has children (offset %s) [in module %s]"),
7574 sect_offset_str (this_cu->sect_off),
7575 bfd_get_filename (abfd));
7576 }
7577 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
7578 if (dwo_unit != NULL)
7579 {
7580 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
7581 comp_unit_die, NULL,
7582 &reader, &info_ptr,
7583 &dwo_comp_unit_die, &has_children,
7584 &dwo_abbrev_table) == 0)
7585 {
7586 /* Dummy die. */
7587 return;
7588 }
7589 comp_unit_die = dwo_comp_unit_die;
7590 }
7591 else
7592 {
7593 /* Yikes, we couldn't find the rest of the DIE, we only have
7594 the stub. A complaint has already been logged. There's
7595 not much more we can do except pass on the stub DIE to
7596 die_reader_func. We don't want to throw an error on bad
7597 debug info. */
7598 }
7599 }
7600
7601 /* All of the above is setup for this call. Yikes. */
7602 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7603
7604 /* Done, clean up. */
7605 if (new_cu != NULL && keep)
7606 {
7607 /* Link this CU into read_in_chain. */
7608 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7609 dwarf2_per_objfile->read_in_chain = this_cu;
7610 /* The chain owns it now. */
7611 new_cu.release ();
7612 }
7613 }
7614
7615 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
7616 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
7617 to have already done the lookup to find the DWO file).
7618
7619 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7620 THIS_CU->is_debug_types, but nothing else.
7621
7622 We fill in THIS_CU->length.
7623
7624 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7625 linker) then DIE_READER_FUNC will not get called.
7626
7627 THIS_CU->cu is always freed when done.
7628 This is done in order to not leave THIS_CU->cu in a state where we have
7629 to care whether it refers to the "main" CU or the DWO CU. */
7630
7631 static void
7632 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
7633 struct dwo_file *dwo_file,
7634 die_reader_func_ftype *die_reader_func,
7635 void *data)
7636 {
7637 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7638 struct objfile *objfile = dwarf2_per_objfile->objfile;
7639 struct dwarf2_section_info *section = this_cu->section;
7640 bfd *abfd = get_section_bfd_owner (section);
7641 struct dwarf2_section_info *abbrev_section;
7642 const gdb_byte *begin_info_ptr, *info_ptr;
7643 struct die_reader_specs reader;
7644 struct die_info *comp_unit_die;
7645 int has_children;
7646
7647 if (dwarf_die_debug)
7648 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7649 this_cu->is_debug_types ? "type" : "comp",
7650 sect_offset_str (this_cu->sect_off));
7651
7652 gdb_assert (this_cu->cu == NULL);
7653
7654 abbrev_section = (dwo_file != NULL
7655 ? &dwo_file->sections.abbrev
7656 : get_abbrev_section_for_cu (this_cu));
7657
7658 /* This is cheap if the section is already read in. */
7659 dwarf2_read_section (objfile, section);
7660
7661 struct dwarf2_cu cu (this_cu);
7662
7663 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7664 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7665 &cu.header, section,
7666 abbrev_section, info_ptr,
7667 (this_cu->is_debug_types
7668 ? rcuh_kind::TYPE
7669 : rcuh_kind::COMPILE));
7670
7671 this_cu->length = get_cu_length (&cu.header);
7672
7673 /* Skip dummy compilation units. */
7674 if (info_ptr >= begin_info_ptr + this_cu->length
7675 || peek_abbrev_code (abfd, info_ptr) == 0)
7676 return;
7677
7678 abbrev_table_up abbrev_table
7679 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7680 cu.header.abbrev_sect_off);
7681
7682 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
7683 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7684
7685 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7686 }
7687
7688 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
7689 does not lookup the specified DWO file.
7690 This cannot be used to read DWO files.
7691
7692 THIS_CU->cu is always freed when done.
7693 This is done in order to not leave THIS_CU->cu in a state where we have
7694 to care whether it refers to the "main" CU or the DWO CU.
7695 We can revisit this if the data shows there's a performance issue. */
7696
7697 static void
7698 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
7699 die_reader_func_ftype *die_reader_func,
7700 void *data)
7701 {
7702 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
7703 }
7704 \f
7705 /* Type Unit Groups.
7706
7707 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7708 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7709 so that all types coming from the same compilation (.o file) are grouped
7710 together. A future step could be to put the types in the same symtab as
7711 the CU the types ultimately came from. */
7712
7713 static hashval_t
7714 hash_type_unit_group (const void *item)
7715 {
7716 const struct type_unit_group *tu_group
7717 = (const struct type_unit_group *) item;
7718
7719 return hash_stmt_list_entry (&tu_group->hash);
7720 }
7721
7722 static int
7723 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7724 {
7725 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7726 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7727
7728 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7729 }
7730
7731 /* Allocate a hash table for type unit groups. */
7732
7733 static htab_t
7734 allocate_type_unit_groups_table (struct objfile *objfile)
7735 {
7736 return htab_create_alloc_ex (3,
7737 hash_type_unit_group,
7738 eq_type_unit_group,
7739 NULL,
7740 &objfile->objfile_obstack,
7741 hashtab_obstack_allocate,
7742 dummy_obstack_deallocate);
7743 }
7744
7745 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7746 partial symtabs. We combine several TUs per psymtab to not let the size
7747 of any one psymtab grow too big. */
7748 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7749 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7750
7751 /* Helper routine for get_type_unit_group.
7752 Create the type_unit_group object used to hold one or more TUs. */
7753
7754 static struct type_unit_group *
7755 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7756 {
7757 struct dwarf2_per_objfile *dwarf2_per_objfile
7758 = cu->per_cu->dwarf2_per_objfile;
7759 struct objfile *objfile = dwarf2_per_objfile->objfile;
7760 struct dwarf2_per_cu_data *per_cu;
7761 struct type_unit_group *tu_group;
7762
7763 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7764 struct type_unit_group);
7765 per_cu = &tu_group->per_cu;
7766 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7767
7768 if (dwarf2_per_objfile->using_index)
7769 {
7770 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7771 struct dwarf2_per_cu_quick_data);
7772 }
7773 else
7774 {
7775 unsigned int line_offset = to_underlying (line_offset_struct);
7776 struct partial_symtab *pst;
7777 char *name;
7778
7779 /* Give the symtab a useful name for debug purposes. */
7780 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7781 name = xstrprintf ("<type_units_%d>",
7782 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7783 else
7784 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
7785
7786 pst = create_partial_symtab (per_cu, name);
7787 pst->anonymous = 1;
7788
7789 xfree (name);
7790 }
7791
7792 tu_group->hash.dwo_unit = cu->dwo_unit;
7793 tu_group->hash.line_sect_off = line_offset_struct;
7794
7795 return tu_group;
7796 }
7797
7798 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7799 STMT_LIST is a DW_AT_stmt_list attribute. */
7800
7801 static struct type_unit_group *
7802 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7803 {
7804 struct dwarf2_per_objfile *dwarf2_per_objfile
7805 = cu->per_cu->dwarf2_per_objfile;
7806 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7807 struct type_unit_group *tu_group;
7808 void **slot;
7809 unsigned int line_offset;
7810 struct type_unit_group type_unit_group_for_lookup;
7811
7812 if (dwarf2_per_objfile->type_unit_groups == NULL)
7813 {
7814 dwarf2_per_objfile->type_unit_groups =
7815 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
7816 }
7817
7818 /* Do we need to create a new group, or can we use an existing one? */
7819
7820 if (stmt_list)
7821 {
7822 line_offset = DW_UNSND (stmt_list);
7823 ++tu_stats->nr_symtab_sharers;
7824 }
7825 else
7826 {
7827 /* Ugh, no stmt_list. Rare, but we have to handle it.
7828 We can do various things here like create one group per TU or
7829 spread them over multiple groups to split up the expansion work.
7830 To avoid worst case scenarios (too many groups or too large groups)
7831 we, umm, group them in bunches. */
7832 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7833 | (tu_stats->nr_stmt_less_type_units
7834 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7835 ++tu_stats->nr_stmt_less_type_units;
7836 }
7837
7838 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7839 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7840 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7841 &type_unit_group_for_lookup, INSERT);
7842 if (*slot != NULL)
7843 {
7844 tu_group = (struct type_unit_group *) *slot;
7845 gdb_assert (tu_group != NULL);
7846 }
7847 else
7848 {
7849 sect_offset line_offset_struct = (sect_offset) line_offset;
7850 tu_group = create_type_unit_group (cu, line_offset_struct);
7851 *slot = tu_group;
7852 ++tu_stats->nr_symtabs;
7853 }
7854
7855 return tu_group;
7856 }
7857 \f
7858 /* Partial symbol tables. */
7859
7860 /* Create a psymtab named NAME and assign it to PER_CU.
7861
7862 The caller must fill in the following details:
7863 dirname, textlow, texthigh. */
7864
7865 static struct partial_symtab *
7866 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7867 {
7868 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
7869 struct partial_symtab *pst;
7870
7871 pst = start_psymtab_common (objfile, name, 0,
7872 objfile->global_psymbols,
7873 objfile->static_psymbols);
7874
7875 pst->psymtabs_addrmap_supported = 1;
7876
7877 /* This is the glue that links PST into GDB's symbol API. */
7878 pst->read_symtab_private = per_cu;
7879 pst->read_symtab = dwarf2_read_symtab;
7880 per_cu->v.psymtab = pst;
7881
7882 return pst;
7883 }
7884
7885 /* The DATA object passed to process_psymtab_comp_unit_reader has this
7886 type. */
7887
7888 struct process_psymtab_comp_unit_data
7889 {
7890 /* True if we are reading a DW_TAG_partial_unit. */
7891
7892 int want_partial_unit;
7893
7894 /* The "pretend" language that is used if the CU doesn't declare a
7895 language. */
7896
7897 enum language pretend_language;
7898 };
7899
7900 /* die_reader_func for process_psymtab_comp_unit. */
7901
7902 static void
7903 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7904 const gdb_byte *info_ptr,
7905 struct die_info *comp_unit_die,
7906 int has_children,
7907 void *data)
7908 {
7909 struct dwarf2_cu *cu = reader->cu;
7910 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
7911 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7912 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7913 CORE_ADDR baseaddr;
7914 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7915 struct partial_symtab *pst;
7916 enum pc_bounds_kind cu_bounds_kind;
7917 const char *filename;
7918 struct process_psymtab_comp_unit_data *info
7919 = (struct process_psymtab_comp_unit_data *) data;
7920
7921 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
7922 return;
7923
7924 gdb_assert (! per_cu->is_debug_types);
7925
7926 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
7927
7928 cu->list_in_scope = &file_symbols;
7929
7930 /* Allocate a new partial symbol table structure. */
7931 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7932 if (filename == NULL)
7933 filename = "";
7934
7935 pst = create_partial_symtab (per_cu, filename);
7936
7937 /* This must be done before calling dwarf2_build_include_psymtabs. */
7938 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7939
7940 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7941
7942 dwarf2_find_base_address (comp_unit_die, cu);
7943
7944 /* Possibly set the default values of LOWPC and HIGHPC from
7945 `DW_AT_ranges'. */
7946 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7947 &best_highpc, cu, pst);
7948 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7949 /* Store the contiguous range if it is not empty; it can be empty for
7950 CUs with no code. */
7951 addrmap_set_empty (objfile->psymtabs_addrmap,
7952 gdbarch_adjust_dwarf2_addr (gdbarch,
7953 best_lowpc + baseaddr),
7954 gdbarch_adjust_dwarf2_addr (gdbarch,
7955 best_highpc + baseaddr) - 1,
7956 pst);
7957
7958 /* Check if comp unit has_children.
7959 If so, read the rest of the partial symbols from this comp unit.
7960 If not, there's no more debug_info for this comp unit. */
7961 if (has_children)
7962 {
7963 struct partial_die_info *first_die;
7964 CORE_ADDR lowpc, highpc;
7965
7966 lowpc = ((CORE_ADDR) -1);
7967 highpc = ((CORE_ADDR) 0);
7968
7969 first_die = load_partial_dies (reader, info_ptr, 1);
7970
7971 scan_partial_symbols (first_die, &lowpc, &highpc,
7972 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7973
7974 /* If we didn't find a lowpc, set it to highpc to avoid
7975 complaints from `maint check'. */
7976 if (lowpc == ((CORE_ADDR) -1))
7977 lowpc = highpc;
7978
7979 /* If the compilation unit didn't have an explicit address range,
7980 then use the information extracted from its child dies. */
7981 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7982 {
7983 best_lowpc = lowpc;
7984 best_highpc = highpc;
7985 }
7986 }
7987 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
7988 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
7989
7990 end_psymtab_common (objfile, pst);
7991
7992 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
7993 {
7994 int i;
7995 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
7996 struct dwarf2_per_cu_data *iter;
7997
7998 /* Fill in 'dependencies' here; we fill in 'users' in a
7999 post-pass. */
8000 pst->number_of_dependencies = len;
8001 pst->dependencies =
8002 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
8003 for (i = 0;
8004 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8005 i, iter);
8006 ++i)
8007 pst->dependencies[i] = iter->v.psymtab;
8008
8009 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8010 }
8011
8012 /* Get the list of files included in the current compilation unit,
8013 and build a psymtab for each of them. */
8014 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8015
8016 if (dwarf_read_debug)
8017 {
8018 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8019
8020 fprintf_unfiltered (gdb_stdlog,
8021 "Psymtab for %s unit @%s: %s - %s"
8022 ", %d global, %d static syms\n",
8023 per_cu->is_debug_types ? "type" : "comp",
8024 sect_offset_str (per_cu->sect_off),
8025 paddress (gdbarch, pst->textlow),
8026 paddress (gdbarch, pst->texthigh),
8027 pst->n_global_syms, pst->n_static_syms);
8028 }
8029 }
8030
8031 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8032 Process compilation unit THIS_CU for a psymtab. */
8033
8034 static void
8035 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
8036 int want_partial_unit,
8037 enum language pretend_language)
8038 {
8039 /* If this compilation unit was already read in, free the
8040 cached copy in order to read it in again. This is
8041 necessary because we skipped some symbols when we first
8042 read in the compilation unit (see load_partial_dies).
8043 This problem could be avoided, but the benefit is unclear. */
8044 if (this_cu->cu != NULL)
8045 free_one_cached_comp_unit (this_cu);
8046
8047 if (this_cu->is_debug_types)
8048 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8049 build_type_psymtabs_reader, NULL);
8050 else
8051 {
8052 process_psymtab_comp_unit_data info;
8053 info.want_partial_unit = want_partial_unit;
8054 info.pretend_language = pretend_language;
8055 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8056 process_psymtab_comp_unit_reader, &info);
8057 }
8058
8059 /* Age out any secondary CUs. */
8060 age_cached_comp_units (this_cu->dwarf2_per_objfile);
8061 }
8062
8063 /* Reader function for build_type_psymtabs. */
8064
8065 static void
8066 build_type_psymtabs_reader (const struct die_reader_specs *reader,
8067 const gdb_byte *info_ptr,
8068 struct die_info *type_unit_die,
8069 int has_children,
8070 void *data)
8071 {
8072 struct dwarf2_per_objfile *dwarf2_per_objfile
8073 = reader->cu->per_cu->dwarf2_per_objfile;
8074 struct objfile *objfile = dwarf2_per_objfile->objfile;
8075 struct dwarf2_cu *cu = reader->cu;
8076 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8077 struct signatured_type *sig_type;
8078 struct type_unit_group *tu_group;
8079 struct attribute *attr;
8080 struct partial_die_info *first_die;
8081 CORE_ADDR lowpc, highpc;
8082 struct partial_symtab *pst;
8083
8084 gdb_assert (data == NULL);
8085 gdb_assert (per_cu->is_debug_types);
8086 sig_type = (struct signatured_type *) per_cu;
8087
8088 if (! has_children)
8089 return;
8090
8091 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
8092 tu_group = get_type_unit_group (cu, attr);
8093
8094 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
8095
8096 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8097 cu->list_in_scope = &file_symbols;
8098 pst = create_partial_symtab (per_cu, "");
8099 pst->anonymous = 1;
8100
8101 first_die = load_partial_dies (reader, info_ptr, 1);
8102
8103 lowpc = (CORE_ADDR) -1;
8104 highpc = (CORE_ADDR) 0;
8105 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8106
8107 end_psymtab_common (objfile, pst);
8108 }
8109
8110 /* Struct used to sort TUs by their abbreviation table offset. */
8111
8112 struct tu_abbrev_offset
8113 {
8114 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
8115 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
8116 {}
8117
8118 signatured_type *sig_type;
8119 sect_offset abbrev_offset;
8120 };
8121
8122 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
8123
8124 static bool
8125 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8126 const struct tu_abbrev_offset &b)
8127 {
8128 return a.abbrev_offset < b.abbrev_offset;
8129 }
8130
8131 /* Efficiently read all the type units.
8132 This does the bulk of the work for build_type_psymtabs.
8133
8134 The efficiency is because we sort TUs by the abbrev table they use and
8135 only read each abbrev table once. In one program there are 200K TUs
8136 sharing 8K abbrev tables.
8137
8138 The main purpose of this function is to support building the
8139 dwarf2_per_objfile->type_unit_groups table.
8140 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8141 can collapse the search space by grouping them by stmt_list.
8142 The savings can be significant, in the same program from above the 200K TUs
8143 share 8K stmt_list tables.
8144
8145 FUNC is expected to call get_type_unit_group, which will create the
8146 struct type_unit_group if necessary and add it to
8147 dwarf2_per_objfile->type_unit_groups. */
8148
8149 static void
8150 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
8151 {
8152 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8153 abbrev_table_up abbrev_table;
8154 sect_offset abbrev_offset;
8155
8156 /* It's up to the caller to not call us multiple times. */
8157 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8158
8159 if (dwarf2_per_objfile->all_type_units.empty ())
8160 return;
8161
8162 /* TUs typically share abbrev tables, and there can be way more TUs than
8163 abbrev tables. Sort by abbrev table to reduce the number of times we
8164 read each abbrev table in.
8165 Alternatives are to punt or to maintain a cache of abbrev tables.
8166 This is simpler and efficient enough for now.
8167
8168 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8169 symtab to use). Typically TUs with the same abbrev offset have the same
8170 stmt_list value too so in practice this should work well.
8171
8172 The basic algorithm here is:
8173
8174 sort TUs by abbrev table
8175 for each TU with same abbrev table:
8176 read abbrev table if first user
8177 read TU top level DIE
8178 [IWBN if DWO skeletons had DW_AT_stmt_list]
8179 call FUNC */
8180
8181 if (dwarf_read_debug)
8182 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8183
8184 /* Sort in a separate table to maintain the order of all_type_units
8185 for .gdb_index: TU indices directly index all_type_units. */
8186 std::vector<tu_abbrev_offset> sorted_by_abbrev;
8187 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
8188
8189 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
8190 sorted_by_abbrev.emplace_back
8191 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
8192 sig_type->per_cu.section,
8193 sig_type->per_cu.sect_off));
8194
8195 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8196 sort_tu_by_abbrev_offset);
8197
8198 abbrev_offset = (sect_offset) ~(unsigned) 0;
8199
8200 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
8201 {
8202 /* Switch to the next abbrev table if necessary. */
8203 if (abbrev_table == NULL
8204 || tu.abbrev_offset != abbrev_offset)
8205 {
8206 abbrev_offset = tu.abbrev_offset;
8207 abbrev_table =
8208 abbrev_table_read_table (dwarf2_per_objfile,
8209 &dwarf2_per_objfile->abbrev,
8210 abbrev_offset);
8211 ++tu_stats->nr_uniq_abbrev_tables;
8212 }
8213
8214 init_cutu_and_read_dies (&tu.sig_type->per_cu, abbrev_table.get (),
8215 0, 0, false, build_type_psymtabs_reader, NULL);
8216 }
8217 }
8218
8219 /* Print collected type unit statistics. */
8220
8221 static void
8222 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
8223 {
8224 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8225
8226 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8227 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
8228 dwarf2_per_objfile->all_type_units.size ());
8229 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8230 tu_stats->nr_uniq_abbrev_tables);
8231 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8232 tu_stats->nr_symtabs);
8233 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8234 tu_stats->nr_symtab_sharers);
8235 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8236 tu_stats->nr_stmt_less_type_units);
8237 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8238 tu_stats->nr_all_type_units_reallocs);
8239 }
8240
8241 /* Traversal function for build_type_psymtabs. */
8242
8243 static int
8244 build_type_psymtab_dependencies (void **slot, void *info)
8245 {
8246 struct dwarf2_per_objfile *dwarf2_per_objfile
8247 = (struct dwarf2_per_objfile *) info;
8248 struct objfile *objfile = dwarf2_per_objfile->objfile;
8249 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8250 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8251 struct partial_symtab *pst = per_cu->v.psymtab;
8252 int len = VEC_length (sig_type_ptr, tu_group->tus);
8253 struct signatured_type *iter;
8254 int i;
8255
8256 gdb_assert (len > 0);
8257 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
8258
8259 pst->number_of_dependencies = len;
8260 pst->dependencies =
8261 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
8262 for (i = 0;
8263 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
8264 ++i)
8265 {
8266 gdb_assert (iter->per_cu.is_debug_types);
8267 pst->dependencies[i] = iter->per_cu.v.psymtab;
8268 iter->type_unit_group = tu_group;
8269 }
8270
8271 VEC_free (sig_type_ptr, tu_group->tus);
8272
8273 return 1;
8274 }
8275
8276 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8277 Build partial symbol tables for the .debug_types comp-units. */
8278
8279 static void
8280 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
8281 {
8282 if (! create_all_type_units (dwarf2_per_objfile))
8283 return;
8284
8285 build_type_psymtabs_1 (dwarf2_per_objfile);
8286 }
8287
8288 /* Traversal function for process_skeletonless_type_unit.
8289 Read a TU in a DWO file and build partial symbols for it. */
8290
8291 static int
8292 process_skeletonless_type_unit (void **slot, void *info)
8293 {
8294 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8295 struct dwarf2_per_objfile *dwarf2_per_objfile
8296 = (struct dwarf2_per_objfile *) info;
8297 struct signatured_type find_entry, *entry;
8298
8299 /* If this TU doesn't exist in the global table, add it and read it in. */
8300
8301 if (dwarf2_per_objfile->signatured_types == NULL)
8302 {
8303 dwarf2_per_objfile->signatured_types
8304 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
8305 }
8306
8307 find_entry.signature = dwo_unit->signature;
8308 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8309 INSERT);
8310 /* If we've already seen this type there's nothing to do. What's happening
8311 is we're doing our own version of comdat-folding here. */
8312 if (*slot != NULL)
8313 return 1;
8314
8315 /* This does the job that create_all_type_units would have done for
8316 this TU. */
8317 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8318 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
8319 *slot = entry;
8320
8321 /* This does the job that build_type_psymtabs_1 would have done. */
8322 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0, false,
8323 build_type_psymtabs_reader, NULL);
8324
8325 return 1;
8326 }
8327
8328 /* Traversal function for process_skeletonless_type_units. */
8329
8330 static int
8331 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8332 {
8333 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8334
8335 if (dwo_file->tus != NULL)
8336 {
8337 htab_traverse_noresize (dwo_file->tus,
8338 process_skeletonless_type_unit, info);
8339 }
8340
8341 return 1;
8342 }
8343
8344 /* Scan all TUs of DWO files, verifying we've processed them.
8345 This is needed in case a TU was emitted without its skeleton.
8346 Note: This can't be done until we know what all the DWO files are. */
8347
8348 static void
8349 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8350 {
8351 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8352 if (get_dwp_file (dwarf2_per_objfile) == NULL
8353 && dwarf2_per_objfile->dwo_files != NULL)
8354 {
8355 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
8356 process_dwo_file_for_skeletonless_type_units,
8357 dwarf2_per_objfile);
8358 }
8359 }
8360
8361 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8362
8363 static void
8364 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
8365 {
8366 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8367 {
8368 struct partial_symtab *pst = per_cu->v.psymtab;
8369
8370 if (pst == NULL)
8371 continue;
8372
8373 for (int j = 0; j < pst->number_of_dependencies; ++j)
8374 {
8375 /* Set the 'user' field only if it is not already set. */
8376 if (pst->dependencies[j]->user == NULL)
8377 pst->dependencies[j]->user = pst;
8378 }
8379 }
8380 }
8381
8382 /* Build the partial symbol table by doing a quick pass through the
8383 .debug_info and .debug_abbrev sections. */
8384
8385 static void
8386 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
8387 {
8388 struct objfile *objfile = dwarf2_per_objfile->objfile;
8389
8390 if (dwarf_read_debug)
8391 {
8392 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8393 objfile_name (objfile));
8394 }
8395
8396 dwarf2_per_objfile->reading_partial_symbols = 1;
8397
8398 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
8399
8400 /* Any cached compilation units will be linked by the per-objfile
8401 read_in_chain. Make sure to free them when we're done. */
8402 free_cached_comp_units freer (dwarf2_per_objfile);
8403
8404 build_type_psymtabs (dwarf2_per_objfile);
8405
8406 create_all_comp_units (dwarf2_per_objfile);
8407
8408 /* Create a temporary address map on a temporary obstack. We later
8409 copy this to the final obstack. */
8410 auto_obstack temp_obstack;
8411
8412 scoped_restore save_psymtabs_addrmap
8413 = make_scoped_restore (&objfile->psymtabs_addrmap,
8414 addrmap_create_mutable (&temp_obstack));
8415
8416 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8417 process_psymtab_comp_unit (per_cu, 0, language_minimal);
8418
8419 /* This has to wait until we read the CUs, we need the list of DWOs. */
8420 process_skeletonless_type_units (dwarf2_per_objfile);
8421
8422 /* Now that all TUs have been processed we can fill in the dependencies. */
8423 if (dwarf2_per_objfile->type_unit_groups != NULL)
8424 {
8425 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
8426 build_type_psymtab_dependencies, dwarf2_per_objfile);
8427 }
8428
8429 if (dwarf_read_debug)
8430 print_tu_stats (dwarf2_per_objfile);
8431
8432 set_partial_user (dwarf2_per_objfile);
8433
8434 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
8435 &objfile->objfile_obstack);
8436 /* At this point we want to keep the address map. */
8437 save_psymtabs_addrmap.release ();
8438
8439 if (dwarf_read_debug)
8440 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8441 objfile_name (objfile));
8442 }
8443
8444 /* die_reader_func for load_partial_comp_unit. */
8445
8446 static void
8447 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
8448 const gdb_byte *info_ptr,
8449 struct die_info *comp_unit_die,
8450 int has_children,
8451 void *data)
8452 {
8453 struct dwarf2_cu *cu = reader->cu;
8454
8455 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
8456
8457 /* Check if comp unit has_children.
8458 If so, read the rest of the partial symbols from this comp unit.
8459 If not, there's no more debug_info for this comp unit. */
8460 if (has_children)
8461 load_partial_dies (reader, info_ptr, 0);
8462 }
8463
8464 /* Load the partial DIEs for a secondary CU into memory.
8465 This is also used when rereading a primary CU with load_all_dies. */
8466
8467 static void
8468 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8469 {
8470 init_cutu_and_read_dies (this_cu, NULL, 1, 1, false,
8471 load_partial_comp_unit_reader, NULL);
8472 }
8473
8474 static void
8475 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8476 struct dwarf2_section_info *section,
8477 struct dwarf2_section_info *abbrev_section,
8478 unsigned int is_dwz)
8479 {
8480 const gdb_byte *info_ptr;
8481 struct objfile *objfile = dwarf2_per_objfile->objfile;
8482
8483 if (dwarf_read_debug)
8484 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8485 get_section_name (section),
8486 get_section_file_name (section));
8487
8488 dwarf2_read_section (objfile, section);
8489
8490 info_ptr = section->buffer;
8491
8492 while (info_ptr < section->buffer + section->size)
8493 {
8494 struct dwarf2_per_cu_data *this_cu;
8495
8496 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8497
8498 comp_unit_head cu_header;
8499 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8500 abbrev_section, info_ptr,
8501 rcuh_kind::COMPILE);
8502
8503 /* Save the compilation unit for later lookup. */
8504 if (cu_header.unit_type != DW_UT_type)
8505 {
8506 this_cu = XOBNEW (&objfile->objfile_obstack,
8507 struct dwarf2_per_cu_data);
8508 memset (this_cu, 0, sizeof (*this_cu));
8509 }
8510 else
8511 {
8512 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8513 struct signatured_type);
8514 memset (sig_type, 0, sizeof (*sig_type));
8515 sig_type->signature = cu_header.signature;
8516 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8517 this_cu = &sig_type->per_cu;
8518 }
8519 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8520 this_cu->sect_off = sect_off;
8521 this_cu->length = cu_header.length + cu_header.initial_length_size;
8522 this_cu->is_dwz = is_dwz;
8523 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8524 this_cu->section = section;
8525
8526 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
8527
8528 info_ptr = info_ptr + this_cu->length;
8529 }
8530 }
8531
8532 /* Create a list of all compilation units in OBJFILE.
8533 This is only done for -readnow and building partial symtabs. */
8534
8535 static void
8536 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8537 {
8538 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
8539 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
8540 &dwarf2_per_objfile->abbrev, 0);
8541
8542 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
8543 if (dwz != NULL)
8544 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
8545 1);
8546 }
8547
8548 /* Process all loaded DIEs for compilation unit CU, starting at
8549 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8550 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8551 DW_AT_ranges). See the comments of add_partial_subprogram on how
8552 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8553
8554 static void
8555 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8556 CORE_ADDR *highpc, int set_addrmap,
8557 struct dwarf2_cu *cu)
8558 {
8559 struct partial_die_info *pdi;
8560
8561 /* Now, march along the PDI's, descending into ones which have
8562 interesting children but skipping the children of the other ones,
8563 until we reach the end of the compilation unit. */
8564
8565 pdi = first_die;
8566
8567 while (pdi != NULL)
8568 {
8569 pdi->fixup (cu);
8570
8571 /* Anonymous namespaces or modules have no name but have interesting
8572 children, so we need to look at them. Ditto for anonymous
8573 enums. */
8574
8575 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
8576 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8577 || pdi->tag == DW_TAG_imported_unit
8578 || pdi->tag == DW_TAG_inlined_subroutine)
8579 {
8580 switch (pdi->tag)
8581 {
8582 case DW_TAG_subprogram:
8583 case DW_TAG_inlined_subroutine:
8584 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8585 break;
8586 case DW_TAG_constant:
8587 case DW_TAG_variable:
8588 case DW_TAG_typedef:
8589 case DW_TAG_union_type:
8590 if (!pdi->is_declaration)
8591 {
8592 add_partial_symbol (pdi, cu);
8593 }
8594 break;
8595 case DW_TAG_class_type:
8596 case DW_TAG_interface_type:
8597 case DW_TAG_structure_type:
8598 if (!pdi->is_declaration)
8599 {
8600 add_partial_symbol (pdi, cu);
8601 }
8602 if ((cu->language == language_rust
8603 || cu->language == language_cplus) && pdi->has_children)
8604 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8605 set_addrmap, cu);
8606 break;
8607 case DW_TAG_enumeration_type:
8608 if (!pdi->is_declaration)
8609 add_partial_enumeration (pdi, cu);
8610 break;
8611 case DW_TAG_base_type:
8612 case DW_TAG_subrange_type:
8613 /* File scope base type definitions are added to the partial
8614 symbol table. */
8615 add_partial_symbol (pdi, cu);
8616 break;
8617 case DW_TAG_namespace:
8618 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8619 break;
8620 case DW_TAG_module:
8621 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8622 break;
8623 case DW_TAG_imported_unit:
8624 {
8625 struct dwarf2_per_cu_data *per_cu;
8626
8627 /* For now we don't handle imported units in type units. */
8628 if (cu->per_cu->is_debug_types)
8629 {
8630 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8631 " supported in type units [in module %s]"),
8632 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
8633 }
8634
8635 per_cu = dwarf2_find_containing_comp_unit
8636 (pdi->d.sect_off, pdi->is_dwz,
8637 cu->per_cu->dwarf2_per_objfile);
8638
8639 /* Go read the partial unit, if needed. */
8640 if (per_cu->v.psymtab == NULL)
8641 process_psymtab_comp_unit (per_cu, 1, cu->language);
8642
8643 VEC_safe_push (dwarf2_per_cu_ptr,
8644 cu->per_cu->imported_symtabs, per_cu);
8645 }
8646 break;
8647 case DW_TAG_imported_declaration:
8648 add_partial_symbol (pdi, cu);
8649 break;
8650 default:
8651 break;
8652 }
8653 }
8654
8655 /* If the die has a sibling, skip to the sibling. */
8656
8657 pdi = pdi->die_sibling;
8658 }
8659 }
8660
8661 /* Functions used to compute the fully scoped name of a partial DIE.
8662
8663 Normally, this is simple. For C++, the parent DIE's fully scoped
8664 name is concatenated with "::" and the partial DIE's name.
8665 Enumerators are an exception; they use the scope of their parent
8666 enumeration type, i.e. the name of the enumeration type is not
8667 prepended to the enumerator.
8668
8669 There are two complexities. One is DW_AT_specification; in this
8670 case "parent" means the parent of the target of the specification,
8671 instead of the direct parent of the DIE. The other is compilers
8672 which do not emit DW_TAG_namespace; in this case we try to guess
8673 the fully qualified name of structure types from their members'
8674 linkage names. This must be done using the DIE's children rather
8675 than the children of any DW_AT_specification target. We only need
8676 to do this for structures at the top level, i.e. if the target of
8677 any DW_AT_specification (if any; otherwise the DIE itself) does not
8678 have a parent. */
8679
8680 /* Compute the scope prefix associated with PDI's parent, in
8681 compilation unit CU. The result will be allocated on CU's
8682 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8683 field. NULL is returned if no prefix is necessary. */
8684 static const char *
8685 partial_die_parent_scope (struct partial_die_info *pdi,
8686 struct dwarf2_cu *cu)
8687 {
8688 const char *grandparent_scope;
8689 struct partial_die_info *parent, *real_pdi;
8690
8691 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8692 then this means the parent of the specification DIE. */
8693
8694 real_pdi = pdi;
8695 while (real_pdi->has_specification)
8696 real_pdi = find_partial_die (real_pdi->spec_offset,
8697 real_pdi->spec_is_dwz, cu);
8698
8699 parent = real_pdi->die_parent;
8700 if (parent == NULL)
8701 return NULL;
8702
8703 if (parent->scope_set)
8704 return parent->scope;
8705
8706 parent->fixup (cu);
8707
8708 grandparent_scope = partial_die_parent_scope (parent, cu);
8709
8710 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8711 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8712 Work around this problem here. */
8713 if (cu->language == language_cplus
8714 && parent->tag == DW_TAG_namespace
8715 && strcmp (parent->name, "::") == 0
8716 && grandparent_scope == NULL)
8717 {
8718 parent->scope = NULL;
8719 parent->scope_set = 1;
8720 return NULL;
8721 }
8722
8723 if (pdi->tag == DW_TAG_enumerator)
8724 /* Enumerators should not get the name of the enumeration as a prefix. */
8725 parent->scope = grandparent_scope;
8726 else if (parent->tag == DW_TAG_namespace
8727 || parent->tag == DW_TAG_module
8728 || parent->tag == DW_TAG_structure_type
8729 || parent->tag == DW_TAG_class_type
8730 || parent->tag == DW_TAG_interface_type
8731 || parent->tag == DW_TAG_union_type
8732 || parent->tag == DW_TAG_enumeration_type)
8733 {
8734 if (grandparent_scope == NULL)
8735 parent->scope = parent->name;
8736 else
8737 parent->scope = typename_concat (&cu->comp_unit_obstack,
8738 grandparent_scope,
8739 parent->name, 0, cu);
8740 }
8741 else
8742 {
8743 /* FIXME drow/2004-04-01: What should we be doing with
8744 function-local names? For partial symbols, we should probably be
8745 ignoring them. */
8746 complaint (&symfile_complaints,
8747 _("unhandled containing DIE tag %d for DIE at %s"),
8748 parent->tag, sect_offset_str (pdi->sect_off));
8749 parent->scope = grandparent_scope;
8750 }
8751
8752 parent->scope_set = 1;
8753 return parent->scope;
8754 }
8755
8756 /* Return the fully scoped name associated with PDI, from compilation unit
8757 CU. The result will be allocated with malloc. */
8758
8759 static char *
8760 partial_die_full_name (struct partial_die_info *pdi,
8761 struct dwarf2_cu *cu)
8762 {
8763 const char *parent_scope;
8764
8765 /* If this is a template instantiation, we can not work out the
8766 template arguments from partial DIEs. So, unfortunately, we have
8767 to go through the full DIEs. At least any work we do building
8768 types here will be reused if full symbols are loaded later. */
8769 if (pdi->has_template_arguments)
8770 {
8771 pdi->fixup (cu);
8772
8773 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8774 {
8775 struct die_info *die;
8776 struct attribute attr;
8777 struct dwarf2_cu *ref_cu = cu;
8778
8779 /* DW_FORM_ref_addr is using section offset. */
8780 attr.name = (enum dwarf_attribute) 0;
8781 attr.form = DW_FORM_ref_addr;
8782 attr.u.unsnd = to_underlying (pdi->sect_off);
8783 die = follow_die_ref (NULL, &attr, &ref_cu);
8784
8785 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8786 }
8787 }
8788
8789 parent_scope = partial_die_parent_scope (pdi, cu);
8790 if (parent_scope == NULL)
8791 return NULL;
8792 else
8793 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
8794 }
8795
8796 static void
8797 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8798 {
8799 struct dwarf2_per_objfile *dwarf2_per_objfile
8800 = cu->per_cu->dwarf2_per_objfile;
8801 struct objfile *objfile = dwarf2_per_objfile->objfile;
8802 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8803 CORE_ADDR addr = 0;
8804 const char *actual_name = NULL;
8805 CORE_ADDR baseaddr;
8806 char *built_actual_name;
8807
8808 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8809
8810 built_actual_name = partial_die_full_name (pdi, cu);
8811 if (built_actual_name != NULL)
8812 actual_name = built_actual_name;
8813
8814 if (actual_name == NULL)
8815 actual_name = pdi->name;
8816
8817 switch (pdi->tag)
8818 {
8819 case DW_TAG_inlined_subroutine:
8820 case DW_TAG_subprogram:
8821 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
8822 if (pdi->is_external || cu->language == language_ada)
8823 {
8824 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
8825 of the global scope. But in Ada, we want to be able to access
8826 nested procedures globally. So all Ada subprograms are stored
8827 in the global scope. */
8828 add_psymbol_to_list (actual_name, strlen (actual_name),
8829 built_actual_name != NULL,
8830 VAR_DOMAIN, LOC_BLOCK,
8831 &objfile->global_psymbols,
8832 addr, cu->language, objfile);
8833 }
8834 else
8835 {
8836 add_psymbol_to_list (actual_name, strlen (actual_name),
8837 built_actual_name != NULL,
8838 VAR_DOMAIN, LOC_BLOCK,
8839 &objfile->static_psymbols,
8840 addr, cu->language, objfile);
8841 }
8842
8843 if (pdi->main_subprogram && actual_name != NULL)
8844 set_objfile_main_name (objfile, actual_name, cu->language);
8845 break;
8846 case DW_TAG_constant:
8847 {
8848 std::vector<partial_symbol *> *list;
8849
8850 if (pdi->is_external)
8851 list = &objfile->global_psymbols;
8852 else
8853 list = &objfile->static_psymbols;
8854 add_psymbol_to_list (actual_name, strlen (actual_name),
8855 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8856 list, 0, cu->language, objfile);
8857 }
8858 break;
8859 case DW_TAG_variable:
8860 if (pdi->d.locdesc)
8861 addr = decode_locdesc (pdi->d.locdesc, cu);
8862
8863 if (pdi->d.locdesc
8864 && addr == 0
8865 && !dwarf2_per_objfile->has_section_at_zero)
8866 {
8867 /* A global or static variable may also have been stripped
8868 out by the linker if unused, in which case its address
8869 will be nullified; do not add such variables into partial
8870 symbol table then. */
8871 }
8872 else if (pdi->is_external)
8873 {
8874 /* Global Variable.
8875 Don't enter into the minimal symbol tables as there is
8876 a minimal symbol table entry from the ELF symbols already.
8877 Enter into partial symbol table if it has a location
8878 descriptor or a type.
8879 If the location descriptor is missing, new_symbol will create
8880 a LOC_UNRESOLVED symbol, the address of the variable will then
8881 be determined from the minimal symbol table whenever the variable
8882 is referenced.
8883 The address for the partial symbol table entry is not
8884 used by GDB, but it comes in handy for debugging partial symbol
8885 table building. */
8886
8887 if (pdi->d.locdesc || pdi->has_type)
8888 add_psymbol_to_list (actual_name, strlen (actual_name),
8889 built_actual_name != NULL,
8890 VAR_DOMAIN, LOC_STATIC,
8891 &objfile->global_psymbols,
8892 addr + baseaddr,
8893 cu->language, objfile);
8894 }
8895 else
8896 {
8897 int has_loc = pdi->d.locdesc != NULL;
8898
8899 /* Static Variable. Skip symbols whose value we cannot know (those
8900 without location descriptors or constant values). */
8901 if (!has_loc && !pdi->has_const_value)
8902 {
8903 xfree (built_actual_name);
8904 return;
8905 }
8906
8907 add_psymbol_to_list (actual_name, strlen (actual_name),
8908 built_actual_name != NULL,
8909 VAR_DOMAIN, LOC_STATIC,
8910 &objfile->static_psymbols,
8911 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
8912 cu->language, objfile);
8913 }
8914 break;
8915 case DW_TAG_typedef:
8916 case DW_TAG_base_type:
8917 case DW_TAG_subrange_type:
8918 add_psymbol_to_list (actual_name, strlen (actual_name),
8919 built_actual_name != NULL,
8920 VAR_DOMAIN, LOC_TYPEDEF,
8921 &objfile->static_psymbols,
8922 0, cu->language, objfile);
8923 break;
8924 case DW_TAG_imported_declaration:
8925 case DW_TAG_namespace:
8926 add_psymbol_to_list (actual_name, strlen (actual_name),
8927 built_actual_name != NULL,
8928 VAR_DOMAIN, LOC_TYPEDEF,
8929 &objfile->global_psymbols,
8930 0, cu->language, objfile);
8931 break;
8932 case DW_TAG_module:
8933 add_psymbol_to_list (actual_name, strlen (actual_name),
8934 built_actual_name != NULL,
8935 MODULE_DOMAIN, LOC_TYPEDEF,
8936 &objfile->global_psymbols,
8937 0, cu->language, objfile);
8938 break;
8939 case DW_TAG_class_type:
8940 case DW_TAG_interface_type:
8941 case DW_TAG_structure_type:
8942 case DW_TAG_union_type:
8943 case DW_TAG_enumeration_type:
8944 /* Skip external references. The DWARF standard says in the section
8945 about "Structure, Union, and Class Type Entries": "An incomplete
8946 structure, union or class type is represented by a structure,
8947 union or class entry that does not have a byte size attribute
8948 and that has a DW_AT_declaration attribute." */
8949 if (!pdi->has_byte_size && pdi->is_declaration)
8950 {
8951 xfree (built_actual_name);
8952 return;
8953 }
8954
8955 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8956 static vs. global. */
8957 add_psymbol_to_list (actual_name, strlen (actual_name),
8958 built_actual_name != NULL,
8959 STRUCT_DOMAIN, LOC_TYPEDEF,
8960 cu->language == language_cplus
8961 ? &objfile->global_psymbols
8962 : &objfile->static_psymbols,
8963 0, cu->language, objfile);
8964
8965 break;
8966 case DW_TAG_enumerator:
8967 add_psymbol_to_list (actual_name, strlen (actual_name),
8968 built_actual_name != NULL,
8969 VAR_DOMAIN, LOC_CONST,
8970 cu->language == language_cplus
8971 ? &objfile->global_psymbols
8972 : &objfile->static_psymbols,
8973 0, cu->language, objfile);
8974 break;
8975 default:
8976 break;
8977 }
8978
8979 xfree (built_actual_name);
8980 }
8981
8982 /* Read a partial die corresponding to a namespace; also, add a symbol
8983 corresponding to that namespace to the symbol table. NAMESPACE is
8984 the name of the enclosing namespace. */
8985
8986 static void
8987 add_partial_namespace (struct partial_die_info *pdi,
8988 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8989 int set_addrmap, struct dwarf2_cu *cu)
8990 {
8991 /* Add a symbol for the namespace. */
8992
8993 add_partial_symbol (pdi, cu);
8994
8995 /* Now scan partial symbols in that namespace. */
8996
8997 if (pdi->has_children)
8998 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8999 }
9000
9001 /* Read a partial die corresponding to a Fortran module. */
9002
9003 static void
9004 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
9005 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
9006 {
9007 /* Add a symbol for the namespace. */
9008
9009 add_partial_symbol (pdi, cu);
9010
9011 /* Now scan partial symbols in that module. */
9012
9013 if (pdi->has_children)
9014 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9015 }
9016
9017 /* Read a partial die corresponding to a subprogram or an inlined
9018 subprogram and create a partial symbol for that subprogram.
9019 When the CU language allows it, this routine also defines a partial
9020 symbol for each nested subprogram that this subprogram contains.
9021 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9022 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
9023
9024 PDI may also be a lexical block, in which case we simply search
9025 recursively for subprograms defined inside that lexical block.
9026 Again, this is only performed when the CU language allows this
9027 type of definitions. */
9028
9029 static void
9030 add_partial_subprogram (struct partial_die_info *pdi,
9031 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9032 int set_addrmap, struct dwarf2_cu *cu)
9033 {
9034 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
9035 {
9036 if (pdi->has_pc_info)
9037 {
9038 if (pdi->lowpc < *lowpc)
9039 *lowpc = pdi->lowpc;
9040 if (pdi->highpc > *highpc)
9041 *highpc = pdi->highpc;
9042 if (set_addrmap)
9043 {
9044 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9045 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9046 CORE_ADDR baseaddr;
9047 CORE_ADDR highpc;
9048 CORE_ADDR lowpc;
9049
9050 baseaddr = ANOFFSET (objfile->section_offsets,
9051 SECT_OFF_TEXT (objfile));
9052 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9053 pdi->lowpc + baseaddr);
9054 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9055 pdi->highpc + baseaddr);
9056 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9057 cu->per_cu->v.psymtab);
9058 }
9059 }
9060
9061 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9062 {
9063 if (!pdi->is_declaration)
9064 /* Ignore subprogram DIEs that do not have a name, they are
9065 illegal. Do not emit a complaint at this point, we will
9066 do so when we convert this psymtab into a symtab. */
9067 if (pdi->name)
9068 add_partial_symbol (pdi, cu);
9069 }
9070 }
9071
9072 if (! pdi->has_children)
9073 return;
9074
9075 if (cu->language == language_ada)
9076 {
9077 pdi = pdi->die_child;
9078 while (pdi != NULL)
9079 {
9080 pdi->fixup (cu);
9081 if (pdi->tag == DW_TAG_subprogram
9082 || pdi->tag == DW_TAG_inlined_subroutine
9083 || pdi->tag == DW_TAG_lexical_block)
9084 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9085 pdi = pdi->die_sibling;
9086 }
9087 }
9088 }
9089
9090 /* Read a partial die corresponding to an enumeration type. */
9091
9092 static void
9093 add_partial_enumeration (struct partial_die_info *enum_pdi,
9094 struct dwarf2_cu *cu)
9095 {
9096 struct partial_die_info *pdi;
9097
9098 if (enum_pdi->name != NULL)
9099 add_partial_symbol (enum_pdi, cu);
9100
9101 pdi = enum_pdi->die_child;
9102 while (pdi)
9103 {
9104 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
9105 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
9106 else
9107 add_partial_symbol (pdi, cu);
9108 pdi = pdi->die_sibling;
9109 }
9110 }
9111
9112 /* Return the initial uleb128 in the die at INFO_PTR. */
9113
9114 static unsigned int
9115 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
9116 {
9117 unsigned int bytes_read;
9118
9119 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9120 }
9121
9122 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9123 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9124
9125 Return the corresponding abbrev, or NULL if the number is zero (indicating
9126 an empty DIE). In either case *BYTES_READ will be set to the length of
9127 the initial number. */
9128
9129 static struct abbrev_info *
9130 peek_die_abbrev (const die_reader_specs &reader,
9131 const gdb_byte *info_ptr, unsigned int *bytes_read)
9132 {
9133 dwarf2_cu *cu = reader.cu;
9134 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
9135 unsigned int abbrev_number
9136 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
9137
9138 if (abbrev_number == 0)
9139 return NULL;
9140
9141 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
9142 if (!abbrev)
9143 {
9144 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9145 " at offset %s [in module %s]"),
9146 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9147 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
9148 }
9149
9150 return abbrev;
9151 }
9152
9153 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9154 Returns a pointer to the end of a series of DIEs, terminated by an empty
9155 DIE. Any children of the skipped DIEs will also be skipped. */
9156
9157 static const gdb_byte *
9158 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
9159 {
9160 while (1)
9161 {
9162 unsigned int bytes_read;
9163 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9164
9165 if (abbrev == NULL)
9166 return info_ptr + bytes_read;
9167 else
9168 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
9169 }
9170 }
9171
9172 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9173 INFO_PTR should point just after the initial uleb128 of a DIE, and the
9174 abbrev corresponding to that skipped uleb128 should be passed in
9175 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9176 children. */
9177
9178 static const gdb_byte *
9179 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
9180 struct abbrev_info *abbrev)
9181 {
9182 unsigned int bytes_read;
9183 struct attribute attr;
9184 bfd *abfd = reader->abfd;
9185 struct dwarf2_cu *cu = reader->cu;
9186 const gdb_byte *buffer = reader->buffer;
9187 const gdb_byte *buffer_end = reader->buffer_end;
9188 unsigned int form, i;
9189
9190 for (i = 0; i < abbrev->num_attrs; i++)
9191 {
9192 /* The only abbrev we care about is DW_AT_sibling. */
9193 if (abbrev->attrs[i].name == DW_AT_sibling)
9194 {
9195 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
9196 if (attr.form == DW_FORM_ref_addr)
9197 complaint (&symfile_complaints,
9198 _("ignoring absolute DW_AT_sibling"));
9199 else
9200 {
9201 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9202 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
9203
9204 if (sibling_ptr < info_ptr)
9205 complaint (&symfile_complaints,
9206 _("DW_AT_sibling points backwards"));
9207 else if (sibling_ptr > reader->buffer_end)
9208 dwarf2_section_buffer_overflow_complaint (reader->die_section);
9209 else
9210 return sibling_ptr;
9211 }
9212 }
9213
9214 /* If it isn't DW_AT_sibling, skip this attribute. */
9215 form = abbrev->attrs[i].form;
9216 skip_attribute:
9217 switch (form)
9218 {
9219 case DW_FORM_ref_addr:
9220 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9221 and later it is offset sized. */
9222 if (cu->header.version == 2)
9223 info_ptr += cu->header.addr_size;
9224 else
9225 info_ptr += cu->header.offset_size;
9226 break;
9227 case DW_FORM_GNU_ref_alt:
9228 info_ptr += cu->header.offset_size;
9229 break;
9230 case DW_FORM_addr:
9231 info_ptr += cu->header.addr_size;
9232 break;
9233 case DW_FORM_data1:
9234 case DW_FORM_ref1:
9235 case DW_FORM_flag:
9236 info_ptr += 1;
9237 break;
9238 case DW_FORM_flag_present:
9239 case DW_FORM_implicit_const:
9240 break;
9241 case DW_FORM_data2:
9242 case DW_FORM_ref2:
9243 info_ptr += 2;
9244 break;
9245 case DW_FORM_data4:
9246 case DW_FORM_ref4:
9247 info_ptr += 4;
9248 break;
9249 case DW_FORM_data8:
9250 case DW_FORM_ref8:
9251 case DW_FORM_ref_sig8:
9252 info_ptr += 8;
9253 break;
9254 case DW_FORM_data16:
9255 info_ptr += 16;
9256 break;
9257 case DW_FORM_string:
9258 read_direct_string (abfd, info_ptr, &bytes_read);
9259 info_ptr += bytes_read;
9260 break;
9261 case DW_FORM_sec_offset:
9262 case DW_FORM_strp:
9263 case DW_FORM_GNU_strp_alt:
9264 info_ptr += cu->header.offset_size;
9265 break;
9266 case DW_FORM_exprloc:
9267 case DW_FORM_block:
9268 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9269 info_ptr += bytes_read;
9270 break;
9271 case DW_FORM_block1:
9272 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9273 break;
9274 case DW_FORM_block2:
9275 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9276 break;
9277 case DW_FORM_block4:
9278 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9279 break;
9280 case DW_FORM_sdata:
9281 case DW_FORM_udata:
9282 case DW_FORM_ref_udata:
9283 case DW_FORM_GNU_addr_index:
9284 case DW_FORM_GNU_str_index:
9285 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9286 break;
9287 case DW_FORM_indirect:
9288 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9289 info_ptr += bytes_read;
9290 /* We need to continue parsing from here, so just go back to
9291 the top. */
9292 goto skip_attribute;
9293
9294 default:
9295 error (_("Dwarf Error: Cannot handle %s "
9296 "in DWARF reader [in module %s]"),
9297 dwarf_form_name (form),
9298 bfd_get_filename (abfd));
9299 }
9300 }
9301
9302 if (abbrev->has_children)
9303 return skip_children (reader, info_ptr);
9304 else
9305 return info_ptr;
9306 }
9307
9308 /* Locate ORIG_PDI's sibling.
9309 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9310
9311 static const gdb_byte *
9312 locate_pdi_sibling (const struct die_reader_specs *reader,
9313 struct partial_die_info *orig_pdi,
9314 const gdb_byte *info_ptr)
9315 {
9316 /* Do we know the sibling already? */
9317
9318 if (orig_pdi->sibling)
9319 return orig_pdi->sibling;
9320
9321 /* Are there any children to deal with? */
9322
9323 if (!orig_pdi->has_children)
9324 return info_ptr;
9325
9326 /* Skip the children the long way. */
9327
9328 return skip_children (reader, info_ptr);
9329 }
9330
9331 /* Expand this partial symbol table into a full symbol table. SELF is
9332 not NULL. */
9333
9334 static void
9335 dwarf2_read_symtab (struct partial_symtab *self,
9336 struct objfile *objfile)
9337 {
9338 struct dwarf2_per_objfile *dwarf2_per_objfile
9339 = get_dwarf2_per_objfile (objfile);
9340
9341 if (self->readin)
9342 {
9343 warning (_("bug: psymtab for %s is already read in."),
9344 self->filename);
9345 }
9346 else
9347 {
9348 if (info_verbose)
9349 {
9350 printf_filtered (_("Reading in symbols for %s..."),
9351 self->filename);
9352 gdb_flush (gdb_stdout);
9353 }
9354
9355 /* If this psymtab is constructed from a debug-only objfile, the
9356 has_section_at_zero flag will not necessarily be correct. We
9357 can get the correct value for this flag by looking at the data
9358 associated with the (presumably stripped) associated objfile. */
9359 if (objfile->separate_debug_objfile_backlink)
9360 {
9361 struct dwarf2_per_objfile *dpo_backlink
9362 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9363
9364 dwarf2_per_objfile->has_section_at_zero
9365 = dpo_backlink->has_section_at_zero;
9366 }
9367
9368 dwarf2_per_objfile->reading_partial_symbols = 0;
9369
9370 psymtab_to_symtab_1 (self);
9371
9372 /* Finish up the debug error message. */
9373 if (info_verbose)
9374 printf_filtered (_("done.\n"));
9375 }
9376
9377 process_cu_includes (dwarf2_per_objfile);
9378 }
9379 \f
9380 /* Reading in full CUs. */
9381
9382 /* Add PER_CU to the queue. */
9383
9384 static void
9385 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9386 enum language pretend_language)
9387 {
9388 struct dwarf2_queue_item *item;
9389
9390 per_cu->queued = 1;
9391 item = XNEW (struct dwarf2_queue_item);
9392 item->per_cu = per_cu;
9393 item->pretend_language = pretend_language;
9394 item->next = NULL;
9395
9396 if (dwarf2_queue == NULL)
9397 dwarf2_queue = item;
9398 else
9399 dwarf2_queue_tail->next = item;
9400
9401 dwarf2_queue_tail = item;
9402 }
9403
9404 /* If PER_CU is not yet queued, add it to the queue.
9405 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9406 dependency.
9407 The result is non-zero if PER_CU was queued, otherwise the result is zero
9408 meaning either PER_CU is already queued or it is already loaded.
9409
9410 N.B. There is an invariant here that if a CU is queued then it is loaded.
9411 The caller is required to load PER_CU if we return non-zero. */
9412
9413 static int
9414 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9415 struct dwarf2_per_cu_data *per_cu,
9416 enum language pretend_language)
9417 {
9418 /* We may arrive here during partial symbol reading, if we need full
9419 DIEs to process an unusual case (e.g. template arguments). Do
9420 not queue PER_CU, just tell our caller to load its DIEs. */
9421 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
9422 {
9423 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9424 return 1;
9425 return 0;
9426 }
9427
9428 /* Mark the dependence relation so that we don't flush PER_CU
9429 too early. */
9430 if (dependent_cu != NULL)
9431 dwarf2_add_dependence (dependent_cu, per_cu);
9432
9433 /* If it's already on the queue, we have nothing to do. */
9434 if (per_cu->queued)
9435 return 0;
9436
9437 /* If the compilation unit is already loaded, just mark it as
9438 used. */
9439 if (per_cu->cu != NULL)
9440 {
9441 per_cu->cu->last_used = 0;
9442 return 0;
9443 }
9444
9445 /* Add it to the queue. */
9446 queue_comp_unit (per_cu, pretend_language);
9447
9448 return 1;
9449 }
9450
9451 /* Process the queue. */
9452
9453 static void
9454 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
9455 {
9456 struct dwarf2_queue_item *item, *next_item;
9457
9458 if (dwarf_read_debug)
9459 {
9460 fprintf_unfiltered (gdb_stdlog,
9461 "Expanding one or more symtabs of objfile %s ...\n",
9462 objfile_name (dwarf2_per_objfile->objfile));
9463 }
9464
9465 /* The queue starts out with one item, but following a DIE reference
9466 may load a new CU, adding it to the end of the queue. */
9467 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9468 {
9469 if ((dwarf2_per_objfile->using_index
9470 ? !item->per_cu->v.quick->compunit_symtab
9471 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9472 /* Skip dummy CUs. */
9473 && item->per_cu->cu != NULL)
9474 {
9475 struct dwarf2_per_cu_data *per_cu = item->per_cu;
9476 unsigned int debug_print_threshold;
9477 char buf[100];
9478
9479 if (per_cu->is_debug_types)
9480 {
9481 struct signatured_type *sig_type =
9482 (struct signatured_type *) per_cu;
9483
9484 sprintf (buf, "TU %s at offset %s",
9485 hex_string (sig_type->signature),
9486 sect_offset_str (per_cu->sect_off));
9487 /* There can be 100s of TUs.
9488 Only print them in verbose mode. */
9489 debug_print_threshold = 2;
9490 }
9491 else
9492 {
9493 sprintf (buf, "CU at offset %s",
9494 sect_offset_str (per_cu->sect_off));
9495 debug_print_threshold = 1;
9496 }
9497
9498 if (dwarf_read_debug >= debug_print_threshold)
9499 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9500
9501 if (per_cu->is_debug_types)
9502 process_full_type_unit (per_cu, item->pretend_language);
9503 else
9504 process_full_comp_unit (per_cu, item->pretend_language);
9505
9506 if (dwarf_read_debug >= debug_print_threshold)
9507 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9508 }
9509
9510 item->per_cu->queued = 0;
9511 next_item = item->next;
9512 xfree (item);
9513 }
9514
9515 dwarf2_queue_tail = NULL;
9516
9517 if (dwarf_read_debug)
9518 {
9519 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9520 objfile_name (dwarf2_per_objfile->objfile));
9521 }
9522 }
9523
9524 /* Read in full symbols for PST, and anything it depends on. */
9525
9526 static void
9527 psymtab_to_symtab_1 (struct partial_symtab *pst)
9528 {
9529 struct dwarf2_per_cu_data *per_cu;
9530 int i;
9531
9532 if (pst->readin)
9533 return;
9534
9535 for (i = 0; i < pst->number_of_dependencies; i++)
9536 if (!pst->dependencies[i]->readin
9537 && pst->dependencies[i]->user == NULL)
9538 {
9539 /* Inform about additional files that need to be read in. */
9540 if (info_verbose)
9541 {
9542 /* FIXME: i18n: Need to make this a single string. */
9543 fputs_filtered (" ", gdb_stdout);
9544 wrap_here ("");
9545 fputs_filtered ("and ", gdb_stdout);
9546 wrap_here ("");
9547 printf_filtered ("%s...", pst->dependencies[i]->filename);
9548 wrap_here (""); /* Flush output. */
9549 gdb_flush (gdb_stdout);
9550 }
9551 psymtab_to_symtab_1 (pst->dependencies[i]);
9552 }
9553
9554 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
9555
9556 if (per_cu == NULL)
9557 {
9558 /* It's an include file, no symbols to read for it.
9559 Everything is in the parent symtab. */
9560 pst->readin = 1;
9561 return;
9562 }
9563
9564 dw2_do_instantiate_symtab (per_cu, false);
9565 }
9566
9567 /* Trivial hash function for die_info: the hash value of a DIE
9568 is its offset in .debug_info for this objfile. */
9569
9570 static hashval_t
9571 die_hash (const void *item)
9572 {
9573 const struct die_info *die = (const struct die_info *) item;
9574
9575 return to_underlying (die->sect_off);
9576 }
9577
9578 /* Trivial comparison function for die_info structures: two DIEs
9579 are equal if they have the same offset. */
9580
9581 static int
9582 die_eq (const void *item_lhs, const void *item_rhs)
9583 {
9584 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9585 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9586
9587 return die_lhs->sect_off == die_rhs->sect_off;
9588 }
9589
9590 /* die_reader_func for load_full_comp_unit.
9591 This is identical to read_signatured_type_reader,
9592 but is kept separate for now. */
9593
9594 static void
9595 load_full_comp_unit_reader (const struct die_reader_specs *reader,
9596 const gdb_byte *info_ptr,
9597 struct die_info *comp_unit_die,
9598 int has_children,
9599 void *data)
9600 {
9601 struct dwarf2_cu *cu = reader->cu;
9602 enum language *language_ptr = (enum language *) data;
9603
9604 gdb_assert (cu->die_hash == NULL);
9605 cu->die_hash =
9606 htab_create_alloc_ex (cu->header.length / 12,
9607 die_hash,
9608 die_eq,
9609 NULL,
9610 &cu->comp_unit_obstack,
9611 hashtab_obstack_allocate,
9612 dummy_obstack_deallocate);
9613
9614 if (has_children)
9615 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
9616 &info_ptr, comp_unit_die);
9617 cu->dies = comp_unit_die;
9618 /* comp_unit_die is not stored in die_hash, no need. */
9619
9620 /* We try not to read any attributes in this function, because not
9621 all CUs needed for references have been loaded yet, and symbol
9622 table processing isn't initialized. But we have to set the CU language,
9623 or we won't be able to build types correctly.
9624 Similarly, if we do not read the producer, we can not apply
9625 producer-specific interpretation. */
9626 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
9627 }
9628
9629 /* Load the DIEs associated with PER_CU into memory. */
9630
9631 static void
9632 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
9633 bool skip_partial,
9634 enum language pretend_language)
9635 {
9636 gdb_assert (! this_cu->is_debug_types);
9637
9638 init_cutu_and_read_dies (this_cu, NULL, 1, 1, skip_partial,
9639 load_full_comp_unit_reader, &pretend_language);
9640 }
9641
9642 /* Add a DIE to the delayed physname list. */
9643
9644 static void
9645 add_to_method_list (struct type *type, int fnfield_index, int index,
9646 const char *name, struct die_info *die,
9647 struct dwarf2_cu *cu)
9648 {
9649 struct delayed_method_info mi;
9650 mi.type = type;
9651 mi.fnfield_index = fnfield_index;
9652 mi.index = index;
9653 mi.name = name;
9654 mi.die = die;
9655 cu->method_list.push_back (mi);
9656 }
9657
9658 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9659 "const" / "volatile". If so, decrements LEN by the length of the
9660 modifier and return true. Otherwise return false. */
9661
9662 template<size_t N>
9663 static bool
9664 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9665 {
9666 size_t mod_len = sizeof (mod) - 1;
9667 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9668 {
9669 len -= mod_len;
9670 return true;
9671 }
9672 return false;
9673 }
9674
9675 /* Compute the physnames of any methods on the CU's method list.
9676
9677 The computation of method physnames is delayed in order to avoid the
9678 (bad) condition that one of the method's formal parameters is of an as yet
9679 incomplete type. */
9680
9681 static void
9682 compute_delayed_physnames (struct dwarf2_cu *cu)
9683 {
9684 /* Only C++ delays computing physnames. */
9685 if (cu->method_list.empty ())
9686 return;
9687 gdb_assert (cu->language == language_cplus);
9688
9689 for (struct delayed_method_info &mi : cu->method_list)
9690 {
9691 const char *physname;
9692 struct fn_fieldlist *fn_flp
9693 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9694 physname = dwarf2_physname (mi.name, mi.die, cu);
9695 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9696 = physname ? physname : "";
9697
9698 /* Since there's no tag to indicate whether a method is a
9699 const/volatile overload, extract that information out of the
9700 demangled name. */
9701 if (physname != NULL)
9702 {
9703 size_t len = strlen (physname);
9704
9705 while (1)
9706 {
9707 if (physname[len] == ')') /* shortcut */
9708 break;
9709 else if (check_modifier (physname, len, " const"))
9710 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9711 else if (check_modifier (physname, len, " volatile"))
9712 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9713 else
9714 break;
9715 }
9716 }
9717 }
9718
9719 /* The list is no longer needed. */
9720 cu->method_list.clear ();
9721 }
9722
9723 /* Go objects should be embedded in a DW_TAG_module DIE,
9724 and it's not clear if/how imported objects will appear.
9725 To keep Go support simple until that's worked out,
9726 go back through what we've read and create something usable.
9727 We could do this while processing each DIE, and feels kinda cleaner,
9728 but that way is more invasive.
9729 This is to, for example, allow the user to type "p var" or "b main"
9730 without having to specify the package name, and allow lookups
9731 of module.object to work in contexts that use the expression
9732 parser. */
9733
9734 static void
9735 fixup_go_packaging (struct dwarf2_cu *cu)
9736 {
9737 char *package_name = NULL;
9738 struct pending *list;
9739 int i;
9740
9741 for (list = global_symbols; list != NULL; list = list->next)
9742 {
9743 for (i = 0; i < list->nsyms; ++i)
9744 {
9745 struct symbol *sym = list->symbol[i];
9746
9747 if (SYMBOL_LANGUAGE (sym) == language_go
9748 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9749 {
9750 char *this_package_name = go_symbol_package_name (sym);
9751
9752 if (this_package_name == NULL)
9753 continue;
9754 if (package_name == NULL)
9755 package_name = this_package_name;
9756 else
9757 {
9758 struct objfile *objfile
9759 = cu->per_cu->dwarf2_per_objfile->objfile;
9760 if (strcmp (package_name, this_package_name) != 0)
9761 complaint (&symfile_complaints,
9762 _("Symtab %s has objects from two different Go packages: %s and %s"),
9763 (symbol_symtab (sym) != NULL
9764 ? symtab_to_filename_for_display
9765 (symbol_symtab (sym))
9766 : objfile_name (objfile)),
9767 this_package_name, package_name);
9768 xfree (this_package_name);
9769 }
9770 }
9771 }
9772 }
9773
9774 if (package_name != NULL)
9775 {
9776 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9777 const char *saved_package_name
9778 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
9779 package_name,
9780 strlen (package_name));
9781 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9782 saved_package_name);
9783 struct symbol *sym;
9784
9785 TYPE_TAG_NAME (type) = TYPE_NAME (type);
9786
9787 sym = allocate_symbol (objfile);
9788 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
9789 SYMBOL_SET_NAMES (sym, saved_package_name,
9790 strlen (saved_package_name), 0, objfile);
9791 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9792 e.g., "main" finds the "main" module and not C's main(). */
9793 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9794 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9795 SYMBOL_TYPE (sym) = type;
9796
9797 add_symbol_to_list (sym, &global_symbols);
9798
9799 xfree (package_name);
9800 }
9801 }
9802
9803 /* Allocate a fully-qualified name consisting of the two parts on the
9804 obstack. */
9805
9806 static const char *
9807 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9808 {
9809 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9810 }
9811
9812 /* A helper that allocates a struct discriminant_info to attach to a
9813 union type. */
9814
9815 static struct discriminant_info *
9816 alloc_discriminant_info (struct type *type, int discriminant_index,
9817 int default_index)
9818 {
9819 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9820 gdb_assert (discriminant_index == -1
9821 || (discriminant_index >= 0
9822 && discriminant_index < TYPE_NFIELDS (type)));
9823 gdb_assert (default_index == -1
9824 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9825
9826 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9827
9828 struct discriminant_info *disc
9829 = ((struct discriminant_info *)
9830 TYPE_ZALLOC (type,
9831 offsetof (struct discriminant_info, discriminants)
9832 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9833 disc->default_index = default_index;
9834 disc->discriminant_index = discriminant_index;
9835
9836 struct dynamic_prop prop;
9837 prop.kind = PROP_UNDEFINED;
9838 prop.data.baton = disc;
9839
9840 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9841
9842 return disc;
9843 }
9844
9845 /* Some versions of rustc emitted enums in an unusual way.
9846
9847 Ordinary enums were emitted as unions. The first element of each
9848 structure in the union was named "RUST$ENUM$DISR". This element
9849 held the discriminant.
9850
9851 These versions of Rust also implemented the "non-zero"
9852 optimization. When the enum had two values, and one is empty and
9853 the other holds a pointer that cannot be zero, the pointer is used
9854 as the discriminant, with a zero value meaning the empty variant.
9855 Here, the union's first member is of the form
9856 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9857 where the fieldnos are the indices of the fields that should be
9858 traversed in order to find the field (which may be several fields deep)
9859 and the variantname is the name of the variant of the case when the
9860 field is zero.
9861
9862 This function recognizes whether TYPE is of one of these forms,
9863 and, if so, smashes it to be a variant type. */
9864
9865 static void
9866 quirk_rust_enum (struct type *type, struct objfile *objfile)
9867 {
9868 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9869
9870 /* We don't need to deal with empty enums. */
9871 if (TYPE_NFIELDS (type) == 0)
9872 return;
9873
9874 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9875 if (TYPE_NFIELDS (type) == 1
9876 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9877 {
9878 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9879
9880 /* Decode the field name to find the offset of the
9881 discriminant. */
9882 ULONGEST bit_offset = 0;
9883 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9884 while (name[0] >= '0' && name[0] <= '9')
9885 {
9886 char *tail;
9887 unsigned long index = strtoul (name, &tail, 10);
9888 name = tail;
9889 if (*name != '$'
9890 || index >= TYPE_NFIELDS (field_type)
9891 || (TYPE_FIELD_LOC_KIND (field_type, index)
9892 != FIELD_LOC_KIND_BITPOS))
9893 {
9894 complaint (&symfile_complaints,
9895 _("Could not parse Rust enum encoding string \"%s\""
9896 "[in module %s]"),
9897 TYPE_FIELD_NAME (type, 0),
9898 objfile_name (objfile));
9899 return;
9900 }
9901 ++name;
9902
9903 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9904 field_type = TYPE_FIELD_TYPE (field_type, index);
9905 }
9906
9907 /* Make a union to hold the variants. */
9908 struct type *union_type = alloc_type (objfile);
9909 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9910 TYPE_NFIELDS (union_type) = 3;
9911 TYPE_FIELDS (union_type)
9912 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9913 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9914 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9915
9916 /* Put the discriminant must at index 0. */
9917 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9918 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9919 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9920 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9921
9922 /* The order of fields doesn't really matter, so put the real
9923 field at index 1 and the data-less field at index 2. */
9924 struct discriminant_info *disc
9925 = alloc_discriminant_info (union_type, 0, 1);
9926 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9927 TYPE_FIELD_NAME (union_type, 1)
9928 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9929 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9930 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9931 TYPE_FIELD_NAME (union_type, 1));
9932
9933 const char *dataless_name
9934 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9935 name);
9936 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9937 dataless_name);
9938 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9939 /* NAME points into the original discriminant name, which
9940 already has the correct lifetime. */
9941 TYPE_FIELD_NAME (union_type, 2) = name;
9942 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
9943 disc->discriminants[2] = 0;
9944
9945 /* Smash this type to be a structure type. We have to do this
9946 because the type has already been recorded. */
9947 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9948 TYPE_NFIELDS (type) = 1;
9949 TYPE_FIELDS (type)
9950 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
9951
9952 /* Install the variant part. */
9953 TYPE_FIELD_TYPE (type, 0) = union_type;
9954 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9955 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9956 }
9957 else if (TYPE_NFIELDS (type) == 1)
9958 {
9959 /* We assume that a union with a single field is a univariant
9960 enum. */
9961 /* Smash this type to be a structure type. We have to do this
9962 because the type has already been recorded. */
9963 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9964
9965 /* Make a union to hold the variants. */
9966 struct type *union_type = alloc_type (objfile);
9967 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9968 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
9969 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9970 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9971 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
9972
9973 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
9974 const char *variant_name
9975 = rust_last_path_segment (TYPE_NAME (field_type));
9976 TYPE_FIELD_NAME (union_type, 0) = variant_name;
9977 TYPE_NAME (field_type)
9978 = rust_fully_qualify (&objfile->objfile_obstack,
9979 TYPE_NAME (type), variant_name);
9980
9981 /* Install the union in the outer struct type. */
9982 TYPE_NFIELDS (type) = 1;
9983 TYPE_FIELDS (type)
9984 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
9985 TYPE_FIELD_TYPE (type, 0) = union_type;
9986 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9987 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9988
9989 alloc_discriminant_info (union_type, -1, 0);
9990 }
9991 else
9992 {
9993 struct type *disr_type = nullptr;
9994 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9995 {
9996 disr_type = TYPE_FIELD_TYPE (type, i);
9997
9998 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
9999 {
10000 /* All fields of a true enum will be structs. */
10001 return;
10002 }
10003 else if (TYPE_NFIELDS (disr_type) == 0)
10004 {
10005 /* Could be data-less variant, so keep going. */
10006 disr_type = nullptr;
10007 }
10008 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10009 "RUST$ENUM$DISR") != 0)
10010 {
10011 /* Not a Rust enum. */
10012 return;
10013 }
10014 else
10015 {
10016 /* Found one. */
10017 break;
10018 }
10019 }
10020
10021 /* If we got here without a discriminant, then it's probably
10022 just a union. */
10023 if (disr_type == nullptr)
10024 return;
10025
10026 /* Smash this type to be a structure type. We have to do this
10027 because the type has already been recorded. */
10028 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10029
10030 /* Make a union to hold the variants. */
10031 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10032 struct type *union_type = alloc_type (objfile);
10033 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10034 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10035 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10036 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10037 TYPE_FIELDS (union_type)
10038 = (struct field *) TYPE_ZALLOC (union_type,
10039 (TYPE_NFIELDS (union_type)
10040 * sizeof (struct field)));
10041
10042 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10043 TYPE_NFIELDS (type) * sizeof (struct field));
10044
10045 /* Install the discriminant at index 0 in the union. */
10046 TYPE_FIELD (union_type, 0) = *disr_field;
10047 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10048 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10049
10050 /* Install the union in the outer struct type. */
10051 TYPE_FIELD_TYPE (type, 0) = union_type;
10052 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10053 TYPE_NFIELDS (type) = 1;
10054
10055 /* Set the size and offset of the union type. */
10056 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10057
10058 /* We need a way to find the correct discriminant given a
10059 variant name. For convenience we build a map here. */
10060 struct type *enum_type = FIELD_TYPE (*disr_field);
10061 std::unordered_map<std::string, ULONGEST> discriminant_map;
10062 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10063 {
10064 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10065 {
10066 const char *name
10067 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10068 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10069 }
10070 }
10071
10072 int n_fields = TYPE_NFIELDS (union_type);
10073 struct discriminant_info *disc
10074 = alloc_discriminant_info (union_type, 0, -1);
10075 /* Skip the discriminant here. */
10076 for (int i = 1; i < n_fields; ++i)
10077 {
10078 /* Find the final word in the name of this variant's type.
10079 That name can be used to look up the correct
10080 discriminant. */
10081 const char *variant_name
10082 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10083 i)));
10084
10085 auto iter = discriminant_map.find (variant_name);
10086 if (iter != discriminant_map.end ())
10087 disc->discriminants[i] = iter->second;
10088
10089 /* Remove the discriminant field, if it exists. */
10090 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
10091 if (TYPE_NFIELDS (sub_type) > 0)
10092 {
10093 --TYPE_NFIELDS (sub_type);
10094 ++TYPE_FIELDS (sub_type);
10095 }
10096 TYPE_FIELD_NAME (union_type, i) = variant_name;
10097 TYPE_NAME (sub_type)
10098 = rust_fully_qualify (&objfile->objfile_obstack,
10099 TYPE_NAME (type), variant_name);
10100 }
10101 }
10102 }
10103
10104 /* Rewrite some Rust unions to be structures with variants parts. */
10105
10106 static void
10107 rust_union_quirks (struct dwarf2_cu *cu)
10108 {
10109 gdb_assert (cu->language == language_rust);
10110 for (struct type *type : cu->rust_unions)
10111 quirk_rust_enum (type, cu->per_cu->dwarf2_per_objfile->objfile);
10112 /* We don't need this any more. */
10113 cu->rust_unions.clear ();
10114 }
10115
10116 /* Return the symtab for PER_CU. This works properly regardless of
10117 whether we're using the index or psymtabs. */
10118
10119 static struct compunit_symtab *
10120 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
10121 {
10122 return (per_cu->dwarf2_per_objfile->using_index
10123 ? per_cu->v.quick->compunit_symtab
10124 : per_cu->v.psymtab->compunit_symtab);
10125 }
10126
10127 /* A helper function for computing the list of all symbol tables
10128 included by PER_CU. */
10129
10130 static void
10131 recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
10132 htab_t all_children, htab_t all_type_symtabs,
10133 struct dwarf2_per_cu_data *per_cu,
10134 struct compunit_symtab *immediate_parent)
10135 {
10136 void **slot;
10137 int ix;
10138 struct compunit_symtab *cust;
10139 struct dwarf2_per_cu_data *iter;
10140
10141 slot = htab_find_slot (all_children, per_cu, INSERT);
10142 if (*slot != NULL)
10143 {
10144 /* This inclusion and its children have been processed. */
10145 return;
10146 }
10147
10148 *slot = per_cu;
10149 /* Only add a CU if it has a symbol table. */
10150 cust = get_compunit_symtab (per_cu);
10151 if (cust != NULL)
10152 {
10153 /* If this is a type unit only add its symbol table if we haven't
10154 seen it yet (type unit per_cu's can share symtabs). */
10155 if (per_cu->is_debug_types)
10156 {
10157 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
10158 if (*slot == NULL)
10159 {
10160 *slot = cust;
10161 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10162 if (cust->user == NULL)
10163 cust->user = immediate_parent;
10164 }
10165 }
10166 else
10167 {
10168 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10169 if (cust->user == NULL)
10170 cust->user = immediate_parent;
10171 }
10172 }
10173
10174 for (ix = 0;
10175 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
10176 ++ix)
10177 {
10178 recursively_compute_inclusions (result, all_children,
10179 all_type_symtabs, iter, cust);
10180 }
10181 }
10182
10183 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
10184 PER_CU. */
10185
10186 static void
10187 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
10188 {
10189 gdb_assert (! per_cu->is_debug_types);
10190
10191 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
10192 {
10193 int ix, len;
10194 struct dwarf2_per_cu_data *per_cu_iter;
10195 struct compunit_symtab *compunit_symtab_iter;
10196 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
10197 htab_t all_children, all_type_symtabs;
10198 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
10199
10200 /* If we don't have a symtab, we can just skip this case. */
10201 if (cust == NULL)
10202 return;
10203
10204 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10205 NULL, xcalloc, xfree);
10206 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10207 NULL, xcalloc, xfree);
10208
10209 for (ix = 0;
10210 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
10211 ix, per_cu_iter);
10212 ++ix)
10213 {
10214 recursively_compute_inclusions (&result_symtabs, all_children,
10215 all_type_symtabs, per_cu_iter,
10216 cust);
10217 }
10218
10219 /* Now we have a transitive closure of all the included symtabs. */
10220 len = VEC_length (compunit_symtab_ptr, result_symtabs);
10221 cust->includes
10222 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
10223 struct compunit_symtab *, len + 1);
10224 for (ix = 0;
10225 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
10226 compunit_symtab_iter);
10227 ++ix)
10228 cust->includes[ix] = compunit_symtab_iter;
10229 cust->includes[len] = NULL;
10230
10231 VEC_free (compunit_symtab_ptr, result_symtabs);
10232 htab_delete (all_children);
10233 htab_delete (all_type_symtabs);
10234 }
10235 }
10236
10237 /* Compute the 'includes' field for the symtabs of all the CUs we just
10238 read. */
10239
10240 static void
10241 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
10242 {
10243 int ix;
10244 struct dwarf2_per_cu_data *iter;
10245
10246 for (ix = 0;
10247 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
10248 ix, iter);
10249 ++ix)
10250 {
10251 if (! iter->is_debug_types)
10252 compute_compunit_symtab_includes (iter);
10253 }
10254
10255 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
10256 }
10257
10258 /* Generate full symbol information for PER_CU, whose DIEs have
10259 already been loaded into memory. */
10260
10261 static void
10262 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10263 enum language pretend_language)
10264 {
10265 struct dwarf2_cu *cu = per_cu->cu;
10266 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10267 struct objfile *objfile = dwarf2_per_objfile->objfile;
10268 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10269 CORE_ADDR lowpc, highpc;
10270 struct compunit_symtab *cust;
10271 CORE_ADDR baseaddr;
10272 struct block *static_block;
10273 CORE_ADDR addr;
10274
10275 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10276
10277 buildsym_init ();
10278 scoped_free_pendings free_pending;
10279
10280 /* Clear the list here in case something was left over. */
10281 cu->method_list.clear ();
10282
10283 cu->list_in_scope = &file_symbols;
10284
10285 cu->language = pretend_language;
10286 cu->language_defn = language_def (cu->language);
10287
10288 /* Do line number decoding in read_file_scope () */
10289 process_die (cu->dies, cu);
10290
10291 /* For now fudge the Go package. */
10292 if (cu->language == language_go)
10293 fixup_go_packaging (cu);
10294
10295 /* Now that we have processed all the DIEs in the CU, all the types
10296 should be complete, and it should now be safe to compute all of the
10297 physnames. */
10298 compute_delayed_physnames (cu);
10299
10300 if (cu->language == language_rust)
10301 rust_union_quirks (cu);
10302
10303 /* Some compilers don't define a DW_AT_high_pc attribute for the
10304 compilation unit. If the DW_AT_high_pc is missing, synthesize
10305 it, by scanning the DIE's below the compilation unit. */
10306 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10307
10308 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10309 static_block = end_symtab_get_static_block (addr, 0, 1);
10310
10311 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10312 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10313 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10314 addrmap to help ensure it has an accurate map of pc values belonging to
10315 this comp unit. */
10316 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10317
10318 cust = end_symtab_from_static_block (static_block,
10319 SECT_OFF_TEXT (objfile), 0);
10320
10321 if (cust != NULL)
10322 {
10323 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10324
10325 /* Set symtab language to language from DW_AT_language. If the
10326 compilation is from a C file generated by language preprocessors, do
10327 not set the language if it was already deduced by start_subfile. */
10328 if (!(cu->language == language_c
10329 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10330 COMPUNIT_FILETABS (cust)->language = cu->language;
10331
10332 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10333 produce DW_AT_location with location lists but it can be possibly
10334 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10335 there were bugs in prologue debug info, fixed later in GCC-4.5
10336 by "unwind info for epilogues" patch (which is not directly related).
10337
10338 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10339 needed, it would be wrong due to missing DW_AT_producer there.
10340
10341 Still one can confuse GDB by using non-standard GCC compilation
10342 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10343 */
10344 if (cu->has_loclist && gcc_4_minor >= 5)
10345 cust->locations_valid = 1;
10346
10347 if (gcc_4_minor >= 5)
10348 cust->epilogue_unwind_valid = 1;
10349
10350 cust->call_site_htab = cu->call_site_htab;
10351 }
10352
10353 if (dwarf2_per_objfile->using_index)
10354 per_cu->v.quick->compunit_symtab = cust;
10355 else
10356 {
10357 struct partial_symtab *pst = per_cu->v.psymtab;
10358 pst->compunit_symtab = cust;
10359 pst->readin = 1;
10360 }
10361
10362 /* Push it for inclusion processing later. */
10363 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
10364 }
10365
10366 /* Generate full symbol information for type unit PER_CU, whose DIEs have
10367 already been loaded into memory. */
10368
10369 static void
10370 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10371 enum language pretend_language)
10372 {
10373 struct dwarf2_cu *cu = per_cu->cu;
10374 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10375 struct objfile *objfile = dwarf2_per_objfile->objfile;
10376 struct compunit_symtab *cust;
10377 struct signatured_type *sig_type;
10378
10379 gdb_assert (per_cu->is_debug_types);
10380 sig_type = (struct signatured_type *) per_cu;
10381
10382 buildsym_init ();
10383 scoped_free_pendings free_pending;
10384
10385 /* Clear the list here in case something was left over. */
10386 cu->method_list.clear ();
10387
10388 cu->list_in_scope = &file_symbols;
10389
10390 cu->language = pretend_language;
10391 cu->language_defn = language_def (cu->language);
10392
10393 /* The symbol tables are set up in read_type_unit_scope. */
10394 process_die (cu->dies, cu);
10395
10396 /* For now fudge the Go package. */
10397 if (cu->language == language_go)
10398 fixup_go_packaging (cu);
10399
10400 /* Now that we have processed all the DIEs in the CU, all the types
10401 should be complete, and it should now be safe to compute all of the
10402 physnames. */
10403 compute_delayed_physnames (cu);
10404
10405 if (cu->language == language_rust)
10406 rust_union_quirks (cu);
10407
10408 /* TUs share symbol tables.
10409 If this is the first TU to use this symtab, complete the construction
10410 of it with end_expandable_symtab. Otherwise, complete the addition of
10411 this TU's symbols to the existing symtab. */
10412 if (sig_type->type_unit_group->compunit_symtab == NULL)
10413 {
10414 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10415 sig_type->type_unit_group->compunit_symtab = cust;
10416
10417 if (cust != NULL)
10418 {
10419 /* Set symtab language to language from DW_AT_language. If the
10420 compilation is from a C file generated by language preprocessors,
10421 do not set the language if it was already deduced by
10422 start_subfile. */
10423 if (!(cu->language == language_c
10424 && COMPUNIT_FILETABS (cust)->language != language_c))
10425 COMPUNIT_FILETABS (cust)->language = cu->language;
10426 }
10427 }
10428 else
10429 {
10430 augment_type_symtab ();
10431 cust = sig_type->type_unit_group->compunit_symtab;
10432 }
10433
10434 if (dwarf2_per_objfile->using_index)
10435 per_cu->v.quick->compunit_symtab = cust;
10436 else
10437 {
10438 struct partial_symtab *pst = per_cu->v.psymtab;
10439 pst->compunit_symtab = cust;
10440 pst->readin = 1;
10441 }
10442 }
10443
10444 /* Process an imported unit DIE. */
10445
10446 static void
10447 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10448 {
10449 struct attribute *attr;
10450
10451 /* For now we don't handle imported units in type units. */
10452 if (cu->per_cu->is_debug_types)
10453 {
10454 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10455 " supported in type units [in module %s]"),
10456 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
10457 }
10458
10459 attr = dwarf2_attr (die, DW_AT_import, cu);
10460 if (attr != NULL)
10461 {
10462 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10463 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10464 dwarf2_per_cu_data *per_cu
10465 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
10466 cu->per_cu->dwarf2_per_objfile);
10467
10468 /* If necessary, add it to the queue and load its DIEs. */
10469 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10470 load_full_comp_unit (per_cu, false, cu->language);
10471
10472 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
10473 per_cu);
10474 }
10475 }
10476
10477 /* RAII object that represents a process_die scope: i.e.,
10478 starts/finishes processing a DIE. */
10479 class process_die_scope
10480 {
10481 public:
10482 process_die_scope (die_info *die, dwarf2_cu *cu)
10483 : m_die (die), m_cu (cu)
10484 {
10485 /* We should only be processing DIEs not already in process. */
10486 gdb_assert (!m_die->in_process);
10487 m_die->in_process = true;
10488 }
10489
10490 ~process_die_scope ()
10491 {
10492 m_die->in_process = false;
10493
10494 /* If we're done processing the DIE for the CU that owns the line
10495 header, we don't need the line header anymore. */
10496 if (m_cu->line_header_die_owner == m_die)
10497 {
10498 delete m_cu->line_header;
10499 m_cu->line_header = NULL;
10500 m_cu->line_header_die_owner = NULL;
10501 }
10502 }
10503
10504 private:
10505 die_info *m_die;
10506 dwarf2_cu *m_cu;
10507 };
10508
10509 /* Process a die and its children. */
10510
10511 static void
10512 process_die (struct die_info *die, struct dwarf2_cu *cu)
10513 {
10514 process_die_scope scope (die, cu);
10515
10516 switch (die->tag)
10517 {
10518 case DW_TAG_padding:
10519 break;
10520 case DW_TAG_compile_unit:
10521 case DW_TAG_partial_unit:
10522 read_file_scope (die, cu);
10523 break;
10524 case DW_TAG_type_unit:
10525 read_type_unit_scope (die, cu);
10526 break;
10527 case DW_TAG_subprogram:
10528 case DW_TAG_inlined_subroutine:
10529 read_func_scope (die, cu);
10530 break;
10531 case DW_TAG_lexical_block:
10532 case DW_TAG_try_block:
10533 case DW_TAG_catch_block:
10534 read_lexical_block_scope (die, cu);
10535 break;
10536 case DW_TAG_call_site:
10537 case DW_TAG_GNU_call_site:
10538 read_call_site_scope (die, cu);
10539 break;
10540 case DW_TAG_class_type:
10541 case DW_TAG_interface_type:
10542 case DW_TAG_structure_type:
10543 case DW_TAG_union_type:
10544 process_structure_scope (die, cu);
10545 break;
10546 case DW_TAG_enumeration_type:
10547 process_enumeration_scope (die, cu);
10548 break;
10549
10550 /* These dies have a type, but processing them does not create
10551 a symbol or recurse to process the children. Therefore we can
10552 read them on-demand through read_type_die. */
10553 case DW_TAG_subroutine_type:
10554 case DW_TAG_set_type:
10555 case DW_TAG_array_type:
10556 case DW_TAG_pointer_type:
10557 case DW_TAG_ptr_to_member_type:
10558 case DW_TAG_reference_type:
10559 case DW_TAG_rvalue_reference_type:
10560 case DW_TAG_string_type:
10561 break;
10562
10563 case DW_TAG_base_type:
10564 case DW_TAG_subrange_type:
10565 case DW_TAG_typedef:
10566 /* Add a typedef symbol for the type definition, if it has a
10567 DW_AT_name. */
10568 new_symbol (die, read_type_die (die, cu), cu);
10569 break;
10570 case DW_TAG_common_block:
10571 read_common_block (die, cu);
10572 break;
10573 case DW_TAG_common_inclusion:
10574 break;
10575 case DW_TAG_namespace:
10576 cu->processing_has_namespace_info = 1;
10577 read_namespace (die, cu);
10578 break;
10579 case DW_TAG_module:
10580 cu->processing_has_namespace_info = 1;
10581 read_module (die, cu);
10582 break;
10583 case DW_TAG_imported_declaration:
10584 cu->processing_has_namespace_info = 1;
10585 if (read_namespace_alias (die, cu))
10586 break;
10587 /* The declaration is not a global namespace alias. */
10588 /* Fall through. */
10589 case DW_TAG_imported_module:
10590 cu->processing_has_namespace_info = 1;
10591 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10592 || cu->language != language_fortran))
10593 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
10594 dwarf_tag_name (die->tag));
10595 read_import_statement (die, cu);
10596 break;
10597
10598 case DW_TAG_imported_unit:
10599 process_imported_unit_die (die, cu);
10600 break;
10601
10602 case DW_TAG_variable:
10603 read_variable (die, cu);
10604 break;
10605
10606 default:
10607 new_symbol (die, NULL, cu);
10608 break;
10609 }
10610 }
10611 \f
10612 /* DWARF name computation. */
10613
10614 /* A helper function for dwarf2_compute_name which determines whether DIE
10615 needs to have the name of the scope prepended to the name listed in the
10616 die. */
10617
10618 static int
10619 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10620 {
10621 struct attribute *attr;
10622
10623 switch (die->tag)
10624 {
10625 case DW_TAG_namespace:
10626 case DW_TAG_typedef:
10627 case DW_TAG_class_type:
10628 case DW_TAG_interface_type:
10629 case DW_TAG_structure_type:
10630 case DW_TAG_union_type:
10631 case DW_TAG_enumeration_type:
10632 case DW_TAG_enumerator:
10633 case DW_TAG_subprogram:
10634 case DW_TAG_inlined_subroutine:
10635 case DW_TAG_member:
10636 case DW_TAG_imported_declaration:
10637 return 1;
10638
10639 case DW_TAG_variable:
10640 case DW_TAG_constant:
10641 /* We only need to prefix "globally" visible variables. These include
10642 any variable marked with DW_AT_external or any variable that
10643 lives in a namespace. [Variables in anonymous namespaces
10644 require prefixing, but they are not DW_AT_external.] */
10645
10646 if (dwarf2_attr (die, DW_AT_specification, cu))
10647 {
10648 struct dwarf2_cu *spec_cu = cu;
10649
10650 return die_needs_namespace (die_specification (die, &spec_cu),
10651 spec_cu);
10652 }
10653
10654 attr = dwarf2_attr (die, DW_AT_external, cu);
10655 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10656 && die->parent->tag != DW_TAG_module)
10657 return 0;
10658 /* A variable in a lexical block of some kind does not need a
10659 namespace, even though in C++ such variables may be external
10660 and have a mangled name. */
10661 if (die->parent->tag == DW_TAG_lexical_block
10662 || die->parent->tag == DW_TAG_try_block
10663 || die->parent->tag == DW_TAG_catch_block
10664 || die->parent->tag == DW_TAG_subprogram)
10665 return 0;
10666 return 1;
10667
10668 default:
10669 return 0;
10670 }
10671 }
10672
10673 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10674 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10675 defined for the given DIE. */
10676
10677 static struct attribute *
10678 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10679 {
10680 struct attribute *attr;
10681
10682 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10683 if (attr == NULL)
10684 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10685
10686 return attr;
10687 }
10688
10689 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10690 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10691 defined for the given DIE. */
10692
10693 static const char *
10694 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10695 {
10696 const char *linkage_name;
10697
10698 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10699 if (linkage_name == NULL)
10700 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10701
10702 return linkage_name;
10703 }
10704
10705 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10706 compute the physname for the object, which include a method's:
10707 - formal parameters (C++),
10708 - receiver type (Go),
10709
10710 The term "physname" is a bit confusing.
10711 For C++, for example, it is the demangled name.
10712 For Go, for example, it's the mangled name.
10713
10714 For Ada, return the DIE's linkage name rather than the fully qualified
10715 name. PHYSNAME is ignored..
10716
10717 The result is allocated on the objfile_obstack and canonicalized. */
10718
10719 static const char *
10720 dwarf2_compute_name (const char *name,
10721 struct die_info *die, struct dwarf2_cu *cu,
10722 int physname)
10723 {
10724 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10725
10726 if (name == NULL)
10727 name = dwarf2_name (die, cu);
10728
10729 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10730 but otherwise compute it by typename_concat inside GDB.
10731 FIXME: Actually this is not really true, or at least not always true.
10732 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
10733 Fortran names because there is no mangling standard. So new_symbol
10734 will set the demangled name to the result of dwarf2_full_name, and it is
10735 the demangled name that GDB uses if it exists. */
10736 if (cu->language == language_ada
10737 || (cu->language == language_fortran && physname))
10738 {
10739 /* For Ada unit, we prefer the linkage name over the name, as
10740 the former contains the exported name, which the user expects
10741 to be able to reference. Ideally, we want the user to be able
10742 to reference this entity using either natural or linkage name,
10743 but we haven't started looking at this enhancement yet. */
10744 const char *linkage_name = dw2_linkage_name (die, cu);
10745
10746 if (linkage_name != NULL)
10747 return linkage_name;
10748 }
10749
10750 /* These are the only languages we know how to qualify names in. */
10751 if (name != NULL
10752 && (cu->language == language_cplus
10753 || cu->language == language_fortran || cu->language == language_d
10754 || cu->language == language_rust))
10755 {
10756 if (die_needs_namespace (die, cu))
10757 {
10758 const char *prefix;
10759 const char *canonical_name = NULL;
10760
10761 string_file buf;
10762
10763 prefix = determine_prefix (die, cu);
10764 if (*prefix != '\0')
10765 {
10766 char *prefixed_name = typename_concat (NULL, prefix, name,
10767 physname, cu);
10768
10769 buf.puts (prefixed_name);
10770 xfree (prefixed_name);
10771 }
10772 else
10773 buf.puts (name);
10774
10775 /* Template parameters may be specified in the DIE's DW_AT_name, or
10776 as children with DW_TAG_template_type_param or
10777 DW_TAG_value_type_param. If the latter, add them to the name
10778 here. If the name already has template parameters, then
10779 skip this step; some versions of GCC emit both, and
10780 it is more efficient to use the pre-computed name.
10781
10782 Something to keep in mind about this process: it is very
10783 unlikely, or in some cases downright impossible, to produce
10784 something that will match the mangled name of a function.
10785 If the definition of the function has the same debug info,
10786 we should be able to match up with it anyway. But fallbacks
10787 using the minimal symbol, for instance to find a method
10788 implemented in a stripped copy of libstdc++, will not work.
10789 If we do not have debug info for the definition, we will have to
10790 match them up some other way.
10791
10792 When we do name matching there is a related problem with function
10793 templates; two instantiated function templates are allowed to
10794 differ only by their return types, which we do not add here. */
10795
10796 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10797 {
10798 struct attribute *attr;
10799 struct die_info *child;
10800 int first = 1;
10801
10802 die->building_fullname = 1;
10803
10804 for (child = die->child; child != NULL; child = child->sibling)
10805 {
10806 struct type *type;
10807 LONGEST value;
10808 const gdb_byte *bytes;
10809 struct dwarf2_locexpr_baton *baton;
10810 struct value *v;
10811
10812 if (child->tag != DW_TAG_template_type_param
10813 && child->tag != DW_TAG_template_value_param)
10814 continue;
10815
10816 if (first)
10817 {
10818 buf.puts ("<");
10819 first = 0;
10820 }
10821 else
10822 buf.puts (", ");
10823
10824 attr = dwarf2_attr (child, DW_AT_type, cu);
10825 if (attr == NULL)
10826 {
10827 complaint (&symfile_complaints,
10828 _("template parameter missing DW_AT_type"));
10829 buf.puts ("UNKNOWN_TYPE");
10830 continue;
10831 }
10832 type = die_type (child, cu);
10833
10834 if (child->tag == DW_TAG_template_type_param)
10835 {
10836 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
10837 continue;
10838 }
10839
10840 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10841 if (attr == NULL)
10842 {
10843 complaint (&symfile_complaints,
10844 _("template parameter missing "
10845 "DW_AT_const_value"));
10846 buf.puts ("UNKNOWN_VALUE");
10847 continue;
10848 }
10849
10850 dwarf2_const_value_attr (attr, type, name,
10851 &cu->comp_unit_obstack, cu,
10852 &value, &bytes, &baton);
10853
10854 if (TYPE_NOSIGN (type))
10855 /* GDB prints characters as NUMBER 'CHAR'. If that's
10856 changed, this can use value_print instead. */
10857 c_printchar (value, type, &buf);
10858 else
10859 {
10860 struct value_print_options opts;
10861
10862 if (baton != NULL)
10863 v = dwarf2_evaluate_loc_desc (type, NULL,
10864 baton->data,
10865 baton->size,
10866 baton->per_cu);
10867 else if (bytes != NULL)
10868 {
10869 v = allocate_value (type);
10870 memcpy (value_contents_writeable (v), bytes,
10871 TYPE_LENGTH (type));
10872 }
10873 else
10874 v = value_from_longest (type, value);
10875
10876 /* Specify decimal so that we do not depend on
10877 the radix. */
10878 get_formatted_print_options (&opts, 'd');
10879 opts.raw = 1;
10880 value_print (v, &buf, &opts);
10881 release_value (v);
10882 }
10883 }
10884
10885 die->building_fullname = 0;
10886
10887 if (!first)
10888 {
10889 /* Close the argument list, with a space if necessary
10890 (nested templates). */
10891 if (!buf.empty () && buf.string ().back () == '>')
10892 buf.puts (" >");
10893 else
10894 buf.puts (">");
10895 }
10896 }
10897
10898 /* For C++ methods, append formal parameter type
10899 information, if PHYSNAME. */
10900
10901 if (physname && die->tag == DW_TAG_subprogram
10902 && cu->language == language_cplus)
10903 {
10904 struct type *type = read_type_die (die, cu);
10905
10906 c_type_print_args (type, &buf, 1, cu->language,
10907 &type_print_raw_options);
10908
10909 if (cu->language == language_cplus)
10910 {
10911 /* Assume that an artificial first parameter is
10912 "this", but do not crash if it is not. RealView
10913 marks unnamed (and thus unused) parameters as
10914 artificial; there is no way to differentiate
10915 the two cases. */
10916 if (TYPE_NFIELDS (type) > 0
10917 && TYPE_FIELD_ARTIFICIAL (type, 0)
10918 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
10919 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10920 0))))
10921 buf.puts (" const");
10922 }
10923 }
10924
10925 const std::string &intermediate_name = buf.string ();
10926
10927 if (cu->language == language_cplus)
10928 canonical_name
10929 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10930 &objfile->per_bfd->storage_obstack);
10931
10932 /* If we only computed INTERMEDIATE_NAME, or if
10933 INTERMEDIATE_NAME is already canonical, then we need to
10934 copy it to the appropriate obstack. */
10935 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10936 name = ((const char *)
10937 obstack_copy0 (&objfile->per_bfd->storage_obstack,
10938 intermediate_name.c_str (),
10939 intermediate_name.length ()));
10940 else
10941 name = canonical_name;
10942 }
10943 }
10944
10945 return name;
10946 }
10947
10948 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10949 If scope qualifiers are appropriate they will be added. The result
10950 will be allocated on the storage_obstack, or NULL if the DIE does
10951 not have a name. NAME may either be from a previous call to
10952 dwarf2_name or NULL.
10953
10954 The output string will be canonicalized (if C++). */
10955
10956 static const char *
10957 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10958 {
10959 return dwarf2_compute_name (name, die, cu, 0);
10960 }
10961
10962 /* Construct a physname for the given DIE in CU. NAME may either be
10963 from a previous call to dwarf2_name or NULL. The result will be
10964 allocated on the objfile_objstack or NULL if the DIE does not have a
10965 name.
10966
10967 The output string will be canonicalized (if C++). */
10968
10969 static const char *
10970 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10971 {
10972 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10973 const char *retval, *mangled = NULL, *canon = NULL;
10974 int need_copy = 1;
10975
10976 /* In this case dwarf2_compute_name is just a shortcut not building anything
10977 on its own. */
10978 if (!die_needs_namespace (die, cu))
10979 return dwarf2_compute_name (name, die, cu, 1);
10980
10981 mangled = dw2_linkage_name (die, cu);
10982
10983 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10984 See https://github.com/rust-lang/rust/issues/32925. */
10985 if (cu->language == language_rust && mangled != NULL
10986 && strchr (mangled, '{') != NULL)
10987 mangled = NULL;
10988
10989 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10990 has computed. */
10991 gdb::unique_xmalloc_ptr<char> demangled;
10992 if (mangled != NULL)
10993 {
10994
10995 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10996 {
10997 /* Do nothing (do not demangle the symbol name). */
10998 }
10999 else if (cu->language == language_go)
11000 {
11001 /* This is a lie, but we already lie to the caller new_symbol.
11002 new_symbol assumes we return the mangled name.
11003 This just undoes that lie until things are cleaned up. */
11004 }
11005 else
11006 {
11007 /* Use DMGL_RET_DROP for C++ template functions to suppress
11008 their return type. It is easier for GDB users to search
11009 for such functions as `name(params)' than `long name(params)'.
11010 In such case the minimal symbol names do not match the full
11011 symbol names but for template functions there is never a need
11012 to look up their definition from their declaration so
11013 the only disadvantage remains the minimal symbol variant
11014 `long name(params)' does not have the proper inferior type. */
11015 demangled.reset (gdb_demangle (mangled,
11016 (DMGL_PARAMS | DMGL_ANSI
11017 | DMGL_RET_DROP)));
11018 }
11019 if (demangled)
11020 canon = demangled.get ();
11021 else
11022 {
11023 canon = mangled;
11024 need_copy = 0;
11025 }
11026 }
11027
11028 if (canon == NULL || check_physname)
11029 {
11030 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11031
11032 if (canon != NULL && strcmp (physname, canon) != 0)
11033 {
11034 /* It may not mean a bug in GDB. The compiler could also
11035 compute DW_AT_linkage_name incorrectly. But in such case
11036 GDB would need to be bug-to-bug compatible. */
11037
11038 complaint (&symfile_complaints,
11039 _("Computed physname <%s> does not match demangled <%s> "
11040 "(from linkage <%s>) - DIE at %s [in module %s]"),
11041 physname, canon, mangled, sect_offset_str (die->sect_off),
11042 objfile_name (objfile));
11043
11044 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11045 is available here - over computed PHYSNAME. It is safer
11046 against both buggy GDB and buggy compilers. */
11047
11048 retval = canon;
11049 }
11050 else
11051 {
11052 retval = physname;
11053 need_copy = 0;
11054 }
11055 }
11056 else
11057 retval = canon;
11058
11059 if (need_copy)
11060 retval = ((const char *)
11061 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11062 retval, strlen (retval)));
11063
11064 return retval;
11065 }
11066
11067 /* Inspect DIE in CU for a namespace alias. If one exists, record
11068 a new symbol for it.
11069
11070 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11071
11072 static int
11073 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11074 {
11075 struct attribute *attr;
11076
11077 /* If the die does not have a name, this is not a namespace
11078 alias. */
11079 attr = dwarf2_attr (die, DW_AT_name, cu);
11080 if (attr != NULL)
11081 {
11082 int num;
11083 struct die_info *d = die;
11084 struct dwarf2_cu *imported_cu = cu;
11085
11086 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11087 keep inspecting DIEs until we hit the underlying import. */
11088 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
11089 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11090 {
11091 attr = dwarf2_attr (d, DW_AT_import, cu);
11092 if (attr == NULL)
11093 break;
11094
11095 d = follow_die_ref (d, attr, &imported_cu);
11096 if (d->tag != DW_TAG_imported_declaration)
11097 break;
11098 }
11099
11100 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11101 {
11102 complaint (&symfile_complaints,
11103 _("DIE at %s has too many recursively imported "
11104 "declarations"), sect_offset_str (d->sect_off));
11105 return 0;
11106 }
11107
11108 if (attr != NULL)
11109 {
11110 struct type *type;
11111 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
11112
11113 type = get_die_type_at_offset (sect_off, cu->per_cu);
11114 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11115 {
11116 /* This declaration is a global namespace alias. Add
11117 a symbol for it whose type is the aliased namespace. */
11118 new_symbol (die, type, cu);
11119 return 1;
11120 }
11121 }
11122 }
11123
11124 return 0;
11125 }
11126
11127 /* Return the using directives repository (global or local?) to use in the
11128 current context for LANGUAGE.
11129
11130 For Ada, imported declarations can materialize renamings, which *may* be
11131 global. However it is impossible (for now?) in DWARF to distinguish
11132 "external" imported declarations and "static" ones. As all imported
11133 declarations seem to be static in all other languages, make them all CU-wide
11134 global only in Ada. */
11135
11136 static struct using_direct **
11137 using_directives (enum language language)
11138 {
11139 if (language == language_ada && context_stack_depth == 0)
11140 return &global_using_directives;
11141 else
11142 return &local_using_directives;
11143 }
11144
11145 /* Read the import statement specified by the given die and record it. */
11146
11147 static void
11148 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11149 {
11150 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11151 struct attribute *import_attr;
11152 struct die_info *imported_die, *child_die;
11153 struct dwarf2_cu *imported_cu;
11154 const char *imported_name;
11155 const char *imported_name_prefix;
11156 const char *canonical_name;
11157 const char *import_alias;
11158 const char *imported_declaration = NULL;
11159 const char *import_prefix;
11160 std::vector<const char *> excludes;
11161
11162 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11163 if (import_attr == NULL)
11164 {
11165 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11166 dwarf_tag_name (die->tag));
11167 return;
11168 }
11169
11170 imported_cu = cu;
11171 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11172 imported_name = dwarf2_name (imported_die, imported_cu);
11173 if (imported_name == NULL)
11174 {
11175 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11176
11177 The import in the following code:
11178 namespace A
11179 {
11180 typedef int B;
11181 }
11182
11183 int main ()
11184 {
11185 using A::B;
11186 B b;
11187 return b;
11188 }
11189
11190 ...
11191 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11192 <52> DW_AT_decl_file : 1
11193 <53> DW_AT_decl_line : 6
11194 <54> DW_AT_import : <0x75>
11195 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11196 <59> DW_AT_name : B
11197 <5b> DW_AT_decl_file : 1
11198 <5c> DW_AT_decl_line : 2
11199 <5d> DW_AT_type : <0x6e>
11200 ...
11201 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11202 <76> DW_AT_byte_size : 4
11203 <77> DW_AT_encoding : 5 (signed)
11204
11205 imports the wrong die ( 0x75 instead of 0x58 ).
11206 This case will be ignored until the gcc bug is fixed. */
11207 return;
11208 }
11209
11210 /* Figure out the local name after import. */
11211 import_alias = dwarf2_name (die, cu);
11212
11213 /* Figure out where the statement is being imported to. */
11214 import_prefix = determine_prefix (die, cu);
11215
11216 /* Figure out what the scope of the imported die is and prepend it
11217 to the name of the imported die. */
11218 imported_name_prefix = determine_prefix (imported_die, imported_cu);
11219
11220 if (imported_die->tag != DW_TAG_namespace
11221 && imported_die->tag != DW_TAG_module)
11222 {
11223 imported_declaration = imported_name;
11224 canonical_name = imported_name_prefix;
11225 }
11226 else if (strlen (imported_name_prefix) > 0)
11227 canonical_name = obconcat (&objfile->objfile_obstack,
11228 imported_name_prefix,
11229 (cu->language == language_d ? "." : "::"),
11230 imported_name, (char *) NULL);
11231 else
11232 canonical_name = imported_name;
11233
11234 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11235 for (child_die = die->child; child_die && child_die->tag;
11236 child_die = sibling_die (child_die))
11237 {
11238 /* DWARF-4: A Fortran use statement with a “rename list” may be
11239 represented by an imported module entry with an import attribute
11240 referring to the module and owned entries corresponding to those
11241 entities that are renamed as part of being imported. */
11242
11243 if (child_die->tag != DW_TAG_imported_declaration)
11244 {
11245 complaint (&symfile_complaints,
11246 _("child DW_TAG_imported_declaration expected "
11247 "- DIE at %s [in module %s]"),
11248 sect_offset_str (child_die->sect_off),
11249 objfile_name (objfile));
11250 continue;
11251 }
11252
11253 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11254 if (import_attr == NULL)
11255 {
11256 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11257 dwarf_tag_name (child_die->tag));
11258 continue;
11259 }
11260
11261 imported_cu = cu;
11262 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11263 &imported_cu);
11264 imported_name = dwarf2_name (imported_die, imported_cu);
11265 if (imported_name == NULL)
11266 {
11267 complaint (&symfile_complaints,
11268 _("child DW_TAG_imported_declaration has unknown "
11269 "imported name - DIE at %s [in module %s]"),
11270 sect_offset_str (child_die->sect_off),
11271 objfile_name (objfile));
11272 continue;
11273 }
11274
11275 excludes.push_back (imported_name);
11276
11277 process_die (child_die, cu);
11278 }
11279
11280 add_using_directive (using_directives (cu->language),
11281 import_prefix,
11282 canonical_name,
11283 import_alias,
11284 imported_declaration,
11285 excludes,
11286 0,
11287 &objfile->objfile_obstack);
11288 }
11289
11290 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11291 types, but gives them a size of zero. Starting with version 14,
11292 ICC is compatible with GCC. */
11293
11294 static int
11295 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11296 {
11297 if (!cu->checked_producer)
11298 check_producer (cu);
11299
11300 return cu->producer_is_icc_lt_14;
11301 }
11302
11303 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11304 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11305 this, it was first present in GCC release 4.3.0. */
11306
11307 static int
11308 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11309 {
11310 if (!cu->checked_producer)
11311 check_producer (cu);
11312
11313 return cu->producer_is_gcc_lt_4_3;
11314 }
11315
11316 static file_and_directory
11317 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11318 {
11319 file_and_directory res;
11320
11321 /* Find the filename. Do not use dwarf2_name here, since the filename
11322 is not a source language identifier. */
11323 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11324 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11325
11326 if (res.comp_dir == NULL
11327 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11328 && IS_ABSOLUTE_PATH (res.name))
11329 {
11330 res.comp_dir_storage = ldirname (res.name);
11331 if (!res.comp_dir_storage.empty ())
11332 res.comp_dir = res.comp_dir_storage.c_str ();
11333 }
11334 if (res.comp_dir != NULL)
11335 {
11336 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11337 directory, get rid of it. */
11338 const char *cp = strchr (res.comp_dir, ':');
11339
11340 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11341 res.comp_dir = cp + 1;
11342 }
11343
11344 if (res.name == NULL)
11345 res.name = "<unknown>";
11346
11347 return res;
11348 }
11349
11350 /* Handle DW_AT_stmt_list for a compilation unit.
11351 DIE is the DW_TAG_compile_unit die for CU.
11352 COMP_DIR is the compilation directory. LOWPC is passed to
11353 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11354
11355 static void
11356 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11357 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11358 {
11359 struct dwarf2_per_objfile *dwarf2_per_objfile
11360 = cu->per_cu->dwarf2_per_objfile;
11361 struct objfile *objfile = dwarf2_per_objfile->objfile;
11362 struct attribute *attr;
11363 struct line_header line_header_local;
11364 hashval_t line_header_local_hash;
11365 void **slot;
11366 int decode_mapping;
11367
11368 gdb_assert (! cu->per_cu->is_debug_types);
11369
11370 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11371 if (attr == NULL)
11372 return;
11373
11374 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11375
11376 /* The line header hash table is only created if needed (it exists to
11377 prevent redundant reading of the line table for partial_units).
11378 If we're given a partial_unit, we'll need it. If we're given a
11379 compile_unit, then use the line header hash table if it's already
11380 created, but don't create one just yet. */
11381
11382 if (dwarf2_per_objfile->line_header_hash == NULL
11383 && die->tag == DW_TAG_partial_unit)
11384 {
11385 dwarf2_per_objfile->line_header_hash
11386 = htab_create_alloc_ex (127, line_header_hash_voidp,
11387 line_header_eq_voidp,
11388 free_line_header_voidp,
11389 &objfile->objfile_obstack,
11390 hashtab_obstack_allocate,
11391 dummy_obstack_deallocate);
11392 }
11393
11394 line_header_local.sect_off = line_offset;
11395 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11396 line_header_local_hash = line_header_hash (&line_header_local);
11397 if (dwarf2_per_objfile->line_header_hash != NULL)
11398 {
11399 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11400 &line_header_local,
11401 line_header_local_hash, NO_INSERT);
11402
11403 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11404 is not present in *SLOT (since if there is something in *SLOT then
11405 it will be for a partial_unit). */
11406 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11407 {
11408 gdb_assert (*slot != NULL);
11409 cu->line_header = (struct line_header *) *slot;
11410 return;
11411 }
11412 }
11413
11414 /* dwarf_decode_line_header does not yet provide sufficient information.
11415 We always have to call also dwarf_decode_lines for it. */
11416 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11417 if (lh == NULL)
11418 return;
11419
11420 cu->line_header = lh.release ();
11421 cu->line_header_die_owner = die;
11422
11423 if (dwarf2_per_objfile->line_header_hash == NULL)
11424 slot = NULL;
11425 else
11426 {
11427 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11428 &line_header_local,
11429 line_header_local_hash, INSERT);
11430 gdb_assert (slot != NULL);
11431 }
11432 if (slot != NULL && *slot == NULL)
11433 {
11434 /* This newly decoded line number information unit will be owned
11435 by line_header_hash hash table. */
11436 *slot = cu->line_header;
11437 cu->line_header_die_owner = NULL;
11438 }
11439 else
11440 {
11441 /* We cannot free any current entry in (*slot) as that struct line_header
11442 may be already used by multiple CUs. Create only temporary decoded
11443 line_header for this CU - it may happen at most once for each line
11444 number information unit. And if we're not using line_header_hash
11445 then this is what we want as well. */
11446 gdb_assert (die->tag != DW_TAG_partial_unit);
11447 }
11448 decode_mapping = (die->tag != DW_TAG_partial_unit);
11449 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11450 decode_mapping);
11451
11452 }
11453
11454 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11455
11456 static void
11457 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11458 {
11459 struct dwarf2_per_objfile *dwarf2_per_objfile
11460 = cu->per_cu->dwarf2_per_objfile;
11461 struct objfile *objfile = dwarf2_per_objfile->objfile;
11462 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11463 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11464 CORE_ADDR highpc = ((CORE_ADDR) 0);
11465 struct attribute *attr;
11466 struct die_info *child_die;
11467 CORE_ADDR baseaddr;
11468
11469 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11470
11471 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11472
11473 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11474 from finish_block. */
11475 if (lowpc == ((CORE_ADDR) -1))
11476 lowpc = highpc;
11477 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11478
11479 file_and_directory fnd = find_file_and_directory (die, cu);
11480
11481 prepare_one_comp_unit (cu, die, cu->language);
11482
11483 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11484 standardised yet. As a workaround for the language detection we fall
11485 back to the DW_AT_producer string. */
11486 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11487 cu->language = language_opencl;
11488
11489 /* Similar hack for Go. */
11490 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11491 set_cu_language (DW_LANG_Go, cu);
11492
11493 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
11494
11495 /* Decode line number information if present. We do this before
11496 processing child DIEs, so that the line header table is available
11497 for DW_AT_decl_file. */
11498 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11499
11500 /* Process all dies in compilation unit. */
11501 if (die->child != NULL)
11502 {
11503 child_die = die->child;
11504 while (child_die && child_die->tag)
11505 {
11506 process_die (child_die, cu);
11507 child_die = sibling_die (child_die);
11508 }
11509 }
11510
11511 /* Decode macro information, if present. Dwarf 2 macro information
11512 refers to information in the line number info statement program
11513 header, so we can only read it if we've read the header
11514 successfully. */
11515 attr = dwarf2_attr (die, DW_AT_macros, cu);
11516 if (attr == NULL)
11517 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11518 if (attr && cu->line_header)
11519 {
11520 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11521 complaint (&symfile_complaints,
11522 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11523
11524 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11525 }
11526 else
11527 {
11528 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11529 if (attr && cu->line_header)
11530 {
11531 unsigned int macro_offset = DW_UNSND (attr);
11532
11533 dwarf_decode_macros (cu, macro_offset, 0);
11534 }
11535 }
11536 }
11537
11538 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
11539 Create the set of symtabs used by this TU, or if this TU is sharing
11540 symtabs with another TU and the symtabs have already been created
11541 then restore those symtabs in the line header.
11542 We don't need the pc/line-number mapping for type units. */
11543
11544 static void
11545 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
11546 {
11547 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
11548 struct type_unit_group *tu_group;
11549 int first_time;
11550 struct attribute *attr;
11551 unsigned int i;
11552 struct signatured_type *sig_type;
11553
11554 gdb_assert (per_cu->is_debug_types);
11555 sig_type = (struct signatured_type *) per_cu;
11556
11557 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11558
11559 /* If we're using .gdb_index (includes -readnow) then
11560 per_cu->type_unit_group may not have been set up yet. */
11561 if (sig_type->type_unit_group == NULL)
11562 sig_type->type_unit_group = get_type_unit_group (cu, attr);
11563 tu_group = sig_type->type_unit_group;
11564
11565 /* If we've already processed this stmt_list there's no real need to
11566 do it again, we could fake it and just recreate the part we need
11567 (file name,index -> symtab mapping). If data shows this optimization
11568 is useful we can do it then. */
11569 first_time = tu_group->compunit_symtab == NULL;
11570
11571 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11572 debug info. */
11573 line_header_up lh;
11574 if (attr != NULL)
11575 {
11576 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11577 lh = dwarf_decode_line_header (line_offset, cu);
11578 }
11579 if (lh == NULL)
11580 {
11581 if (first_time)
11582 dwarf2_start_symtab (cu, "", NULL, 0);
11583 else
11584 {
11585 gdb_assert (tu_group->symtabs == NULL);
11586 restart_symtab (tu_group->compunit_symtab, "", 0);
11587 }
11588 return;
11589 }
11590
11591 cu->line_header = lh.release ();
11592 cu->line_header_die_owner = die;
11593
11594 if (first_time)
11595 {
11596 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
11597
11598 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11599 still initializing it, and our caller (a few levels up)
11600 process_full_type_unit still needs to know if this is the first
11601 time. */
11602
11603 tu_group->num_symtabs = cu->line_header->file_names.size ();
11604 tu_group->symtabs = XNEWVEC (struct symtab *,
11605 cu->line_header->file_names.size ());
11606
11607 for (i = 0; i < cu->line_header->file_names.size (); ++i)
11608 {
11609 file_entry &fe = cu->line_header->file_names[i];
11610
11611 dwarf2_start_subfile (fe.name, fe.include_dir (cu->line_header));
11612
11613 if (current_subfile->symtab == NULL)
11614 {
11615 /* NOTE: start_subfile will recognize when it's been
11616 passed a file it has already seen. So we can't
11617 assume there's a simple mapping from
11618 cu->line_header->file_names to subfiles, plus
11619 cu->line_header->file_names may contain dups. */
11620 current_subfile->symtab
11621 = allocate_symtab (cust, current_subfile->name);
11622 }
11623
11624 fe.symtab = current_subfile->symtab;
11625 tu_group->symtabs[i] = fe.symtab;
11626 }
11627 }
11628 else
11629 {
11630 restart_symtab (tu_group->compunit_symtab, "", 0);
11631
11632 for (i = 0; i < cu->line_header->file_names.size (); ++i)
11633 {
11634 file_entry &fe = cu->line_header->file_names[i];
11635
11636 fe.symtab = tu_group->symtabs[i];
11637 }
11638 }
11639
11640 /* The main symtab is allocated last. Type units don't have DW_AT_name
11641 so they don't have a "real" (so to speak) symtab anyway.
11642 There is later code that will assign the main symtab to all symbols
11643 that don't have one. We need to handle the case of a symbol with a
11644 missing symtab (DW_AT_decl_file) anyway. */
11645 }
11646
11647 /* Process DW_TAG_type_unit.
11648 For TUs we want to skip the first top level sibling if it's not the
11649 actual type being defined by this TU. In this case the first top
11650 level sibling is there to provide context only. */
11651
11652 static void
11653 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11654 {
11655 struct die_info *child_die;
11656
11657 prepare_one_comp_unit (cu, die, language_minimal);
11658
11659 /* Initialize (or reinitialize) the machinery for building symtabs.
11660 We do this before processing child DIEs, so that the line header table
11661 is available for DW_AT_decl_file. */
11662 setup_type_unit_groups (die, cu);
11663
11664 if (die->child != NULL)
11665 {
11666 child_die = die->child;
11667 while (child_die && child_die->tag)
11668 {
11669 process_die (child_die, cu);
11670 child_die = sibling_die (child_die);
11671 }
11672 }
11673 }
11674 \f
11675 /* DWO/DWP files.
11676
11677 http://gcc.gnu.org/wiki/DebugFission
11678 http://gcc.gnu.org/wiki/DebugFissionDWP
11679
11680 To simplify handling of both DWO files ("object" files with the DWARF info)
11681 and DWP files (a file with the DWOs packaged up into one file), we treat
11682 DWP files as having a collection of virtual DWO files. */
11683
11684 static hashval_t
11685 hash_dwo_file (const void *item)
11686 {
11687 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11688 hashval_t hash;
11689
11690 hash = htab_hash_string (dwo_file->dwo_name);
11691 if (dwo_file->comp_dir != NULL)
11692 hash += htab_hash_string (dwo_file->comp_dir);
11693 return hash;
11694 }
11695
11696 static int
11697 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11698 {
11699 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11700 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11701
11702 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11703 return 0;
11704 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11705 return lhs->comp_dir == rhs->comp_dir;
11706 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11707 }
11708
11709 /* Allocate a hash table for DWO files. */
11710
11711 static htab_t
11712 allocate_dwo_file_hash_table (struct objfile *objfile)
11713 {
11714 return htab_create_alloc_ex (41,
11715 hash_dwo_file,
11716 eq_dwo_file,
11717 NULL,
11718 &objfile->objfile_obstack,
11719 hashtab_obstack_allocate,
11720 dummy_obstack_deallocate);
11721 }
11722
11723 /* Lookup DWO file DWO_NAME. */
11724
11725 static void **
11726 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11727 const char *dwo_name,
11728 const char *comp_dir)
11729 {
11730 struct dwo_file find_entry;
11731 void **slot;
11732
11733 if (dwarf2_per_objfile->dwo_files == NULL)
11734 dwarf2_per_objfile->dwo_files
11735 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
11736
11737 memset (&find_entry, 0, sizeof (find_entry));
11738 find_entry.dwo_name = dwo_name;
11739 find_entry.comp_dir = comp_dir;
11740 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
11741
11742 return slot;
11743 }
11744
11745 static hashval_t
11746 hash_dwo_unit (const void *item)
11747 {
11748 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11749
11750 /* This drops the top 32 bits of the id, but is ok for a hash. */
11751 return dwo_unit->signature;
11752 }
11753
11754 static int
11755 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11756 {
11757 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11758 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11759
11760 /* The signature is assumed to be unique within the DWO file.
11761 So while object file CU dwo_id's always have the value zero,
11762 that's OK, assuming each object file DWO file has only one CU,
11763 and that's the rule for now. */
11764 return lhs->signature == rhs->signature;
11765 }
11766
11767 /* Allocate a hash table for DWO CUs,TUs.
11768 There is one of these tables for each of CUs,TUs for each DWO file. */
11769
11770 static htab_t
11771 allocate_dwo_unit_table (struct objfile *objfile)
11772 {
11773 /* Start out with a pretty small number.
11774 Generally DWO files contain only one CU and maybe some TUs. */
11775 return htab_create_alloc_ex (3,
11776 hash_dwo_unit,
11777 eq_dwo_unit,
11778 NULL,
11779 &objfile->objfile_obstack,
11780 hashtab_obstack_allocate,
11781 dummy_obstack_deallocate);
11782 }
11783
11784 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
11785
11786 struct create_dwo_cu_data
11787 {
11788 struct dwo_file *dwo_file;
11789 struct dwo_unit dwo_unit;
11790 };
11791
11792 /* die_reader_func for create_dwo_cu. */
11793
11794 static void
11795 create_dwo_cu_reader (const struct die_reader_specs *reader,
11796 const gdb_byte *info_ptr,
11797 struct die_info *comp_unit_die,
11798 int has_children,
11799 void *datap)
11800 {
11801 struct dwarf2_cu *cu = reader->cu;
11802 sect_offset sect_off = cu->per_cu->sect_off;
11803 struct dwarf2_section_info *section = cu->per_cu->section;
11804 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
11805 struct dwo_file *dwo_file = data->dwo_file;
11806 struct dwo_unit *dwo_unit = &data->dwo_unit;
11807 struct attribute *attr;
11808
11809 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
11810 if (attr == NULL)
11811 {
11812 complaint (&symfile_complaints,
11813 _("Dwarf Error: debug entry at offset %s is missing"
11814 " its dwo_id [in module %s]"),
11815 sect_offset_str (sect_off), dwo_file->dwo_name);
11816 return;
11817 }
11818
11819 dwo_unit->dwo_file = dwo_file;
11820 dwo_unit->signature = DW_UNSND (attr);
11821 dwo_unit->section = section;
11822 dwo_unit->sect_off = sect_off;
11823 dwo_unit->length = cu->per_cu->length;
11824
11825 if (dwarf_read_debug)
11826 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11827 sect_offset_str (sect_off),
11828 hex_string (dwo_unit->signature));
11829 }
11830
11831 /* Create the dwo_units for the CUs in a DWO_FILE.
11832 Note: This function processes DWO files only, not DWP files. */
11833
11834 static void
11835 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11836 struct dwo_file &dwo_file, dwarf2_section_info &section,
11837 htab_t &cus_htab)
11838 {
11839 struct objfile *objfile = dwarf2_per_objfile->objfile;
11840 const gdb_byte *info_ptr, *end_ptr;
11841
11842 dwarf2_read_section (objfile, &section);
11843 info_ptr = section.buffer;
11844
11845 if (info_ptr == NULL)
11846 return;
11847
11848 if (dwarf_read_debug)
11849 {
11850 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11851 get_section_name (&section),
11852 get_section_file_name (&section));
11853 }
11854
11855 end_ptr = info_ptr + section.size;
11856 while (info_ptr < end_ptr)
11857 {
11858 struct dwarf2_per_cu_data per_cu;
11859 struct create_dwo_cu_data create_dwo_cu_data;
11860 struct dwo_unit *dwo_unit;
11861 void **slot;
11862 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11863
11864 memset (&create_dwo_cu_data.dwo_unit, 0,
11865 sizeof (create_dwo_cu_data.dwo_unit));
11866 memset (&per_cu, 0, sizeof (per_cu));
11867 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11868 per_cu.is_debug_types = 0;
11869 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11870 per_cu.section = &section;
11871 create_dwo_cu_data.dwo_file = &dwo_file;
11872
11873 init_cutu_and_read_dies_no_follow (
11874 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
11875 info_ptr += per_cu.length;
11876
11877 // If the unit could not be parsed, skip it.
11878 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
11879 continue;
11880
11881 if (cus_htab == NULL)
11882 cus_htab = allocate_dwo_unit_table (objfile);
11883
11884 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11885 *dwo_unit = create_dwo_cu_data.dwo_unit;
11886 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
11887 gdb_assert (slot != NULL);
11888 if (*slot != NULL)
11889 {
11890 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11891 sect_offset dup_sect_off = dup_cu->sect_off;
11892
11893 complaint (&symfile_complaints,
11894 _("debug cu entry at offset %s is duplicate to"
11895 " the entry at offset %s, signature %s"),
11896 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11897 hex_string (dwo_unit->signature));
11898 }
11899 *slot = (void *)dwo_unit;
11900 }
11901 }
11902
11903 /* DWP file .debug_{cu,tu}_index section format:
11904 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11905
11906 DWP Version 1:
11907
11908 Both index sections have the same format, and serve to map a 64-bit
11909 signature to a set of section numbers. Each section begins with a header,
11910 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11911 indexes, and a pool of 32-bit section numbers. The index sections will be
11912 aligned at 8-byte boundaries in the file.
11913
11914 The index section header consists of:
11915
11916 V, 32 bit version number
11917 -, 32 bits unused
11918 N, 32 bit number of compilation units or type units in the index
11919 M, 32 bit number of slots in the hash table
11920
11921 Numbers are recorded using the byte order of the application binary.
11922
11923 The hash table begins at offset 16 in the section, and consists of an array
11924 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11925 order of the application binary). Unused slots in the hash table are 0.
11926 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11927
11928 The parallel table begins immediately after the hash table
11929 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11930 array of 32-bit indexes (using the byte order of the application binary),
11931 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11932 table contains a 32-bit index into the pool of section numbers. For unused
11933 hash table slots, the corresponding entry in the parallel table will be 0.
11934
11935 The pool of section numbers begins immediately following the hash table
11936 (at offset 16 + 12 * M from the beginning of the section). The pool of
11937 section numbers consists of an array of 32-bit words (using the byte order
11938 of the application binary). Each item in the array is indexed starting
11939 from 0. The hash table entry provides the index of the first section
11940 number in the set. Additional section numbers in the set follow, and the
11941 set is terminated by a 0 entry (section number 0 is not used in ELF).
11942
11943 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11944 section must be the first entry in the set, and the .debug_abbrev.dwo must
11945 be the second entry. Other members of the set may follow in any order.
11946
11947 ---
11948
11949 DWP Version 2:
11950
11951 DWP Version 2 combines all the .debug_info, etc. sections into one,
11952 and the entries in the index tables are now offsets into these sections.
11953 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11954 section.
11955
11956 Index Section Contents:
11957 Header
11958 Hash Table of Signatures dwp_hash_table.hash_table
11959 Parallel Table of Indices dwp_hash_table.unit_table
11960 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11961 Table of Section Sizes dwp_hash_table.v2.sizes
11962
11963 The index section header consists of:
11964
11965 V, 32 bit version number
11966 L, 32 bit number of columns in the table of section offsets
11967 N, 32 bit number of compilation units or type units in the index
11968 M, 32 bit number of slots in the hash table
11969
11970 Numbers are recorded using the byte order of the application binary.
11971
11972 The hash table has the same format as version 1.
11973 The parallel table of indices has the same format as version 1,
11974 except that the entries are origin-1 indices into the table of sections
11975 offsets and the table of section sizes.
11976
11977 The table of offsets begins immediately following the parallel table
11978 (at offset 16 + 12 * M from the beginning of the section). The table is
11979 a two-dimensional array of 32-bit words (using the byte order of the
11980 application binary), with L columns and N+1 rows, in row-major order.
11981 Each row in the array is indexed starting from 0. The first row provides
11982 a key to the remaining rows: each column in this row provides an identifier
11983 for a debug section, and the offsets in the same column of subsequent rows
11984 refer to that section. The section identifiers are:
11985
11986 DW_SECT_INFO 1 .debug_info.dwo
11987 DW_SECT_TYPES 2 .debug_types.dwo
11988 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11989 DW_SECT_LINE 4 .debug_line.dwo
11990 DW_SECT_LOC 5 .debug_loc.dwo
11991 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11992 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11993 DW_SECT_MACRO 8 .debug_macro.dwo
11994
11995 The offsets provided by the CU and TU index sections are the base offsets
11996 for the contributions made by each CU or TU to the corresponding section
11997 in the package file. Each CU and TU header contains an abbrev_offset
11998 field, used to find the abbreviations table for that CU or TU within the
11999 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12000 be interpreted as relative to the base offset given in the index section.
12001 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12002 should be interpreted as relative to the base offset for .debug_line.dwo,
12003 and offsets into other debug sections obtained from DWARF attributes should
12004 also be interpreted as relative to the corresponding base offset.
12005
12006 The table of sizes begins immediately following the table of offsets.
12007 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12008 with L columns and N rows, in row-major order. Each row in the array is
12009 indexed starting from 1 (row 0 is shared by the two tables).
12010
12011 ---
12012
12013 Hash table lookup is handled the same in version 1 and 2:
12014
12015 We assume that N and M will not exceed 2^32 - 1.
12016 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12017
12018 Given a 64-bit compilation unit signature or a type signature S, an entry
12019 in the hash table is located as follows:
12020
12021 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12022 the low-order k bits all set to 1.
12023
12024 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
12025
12026 3) If the hash table entry at index H matches the signature, use that
12027 entry. If the hash table entry at index H is unused (all zeroes),
12028 terminate the search: the signature is not present in the table.
12029
12030 4) Let H = (H + H') modulo M. Repeat at Step 3.
12031
12032 Because M > N and H' and M are relatively prime, the search is guaranteed
12033 to stop at an unused slot or find the match. */
12034
12035 /* Create a hash table to map DWO IDs to their CU/TU entry in
12036 .debug_{info,types}.dwo in DWP_FILE.
12037 Returns NULL if there isn't one.
12038 Note: This function processes DWP files only, not DWO files. */
12039
12040 static struct dwp_hash_table *
12041 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12042 struct dwp_file *dwp_file, int is_debug_types)
12043 {
12044 struct objfile *objfile = dwarf2_per_objfile->objfile;
12045 bfd *dbfd = dwp_file->dbfd.get ();
12046 const gdb_byte *index_ptr, *index_end;
12047 struct dwarf2_section_info *index;
12048 uint32_t version, nr_columns, nr_units, nr_slots;
12049 struct dwp_hash_table *htab;
12050
12051 if (is_debug_types)
12052 index = &dwp_file->sections.tu_index;
12053 else
12054 index = &dwp_file->sections.cu_index;
12055
12056 if (dwarf2_section_empty_p (index))
12057 return NULL;
12058 dwarf2_read_section (objfile, index);
12059
12060 index_ptr = index->buffer;
12061 index_end = index_ptr + index->size;
12062
12063 version = read_4_bytes (dbfd, index_ptr);
12064 index_ptr += 4;
12065 if (version == 2)
12066 nr_columns = read_4_bytes (dbfd, index_ptr);
12067 else
12068 nr_columns = 0;
12069 index_ptr += 4;
12070 nr_units = read_4_bytes (dbfd, index_ptr);
12071 index_ptr += 4;
12072 nr_slots = read_4_bytes (dbfd, index_ptr);
12073 index_ptr += 4;
12074
12075 if (version != 1 && version != 2)
12076 {
12077 error (_("Dwarf Error: unsupported DWP file version (%s)"
12078 " [in module %s]"),
12079 pulongest (version), dwp_file->name);
12080 }
12081 if (nr_slots != (nr_slots & -nr_slots))
12082 {
12083 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
12084 " is not power of 2 [in module %s]"),
12085 pulongest (nr_slots), dwp_file->name);
12086 }
12087
12088 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
12089 htab->version = version;
12090 htab->nr_columns = nr_columns;
12091 htab->nr_units = nr_units;
12092 htab->nr_slots = nr_slots;
12093 htab->hash_table = index_ptr;
12094 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
12095
12096 /* Exit early if the table is empty. */
12097 if (nr_slots == 0 || nr_units == 0
12098 || (version == 2 && nr_columns == 0))
12099 {
12100 /* All must be zero. */
12101 if (nr_slots != 0 || nr_units != 0
12102 || (version == 2 && nr_columns != 0))
12103 {
12104 complaint (&symfile_complaints,
12105 _("Empty DWP but nr_slots,nr_units,nr_columns not"
12106 " all zero [in modules %s]"),
12107 dwp_file->name);
12108 }
12109 return htab;
12110 }
12111
12112 if (version == 1)
12113 {
12114 htab->section_pool.v1.indices =
12115 htab->unit_table + sizeof (uint32_t) * nr_slots;
12116 /* It's harder to decide whether the section is too small in v1.
12117 V1 is deprecated anyway so we punt. */
12118 }
12119 else
12120 {
12121 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12122 int *ids = htab->section_pool.v2.section_ids;
12123 /* Reverse map for error checking. */
12124 int ids_seen[DW_SECT_MAX + 1];
12125 int i;
12126
12127 if (nr_columns < 2)
12128 {
12129 error (_("Dwarf Error: bad DWP hash table, too few columns"
12130 " in section table [in module %s]"),
12131 dwp_file->name);
12132 }
12133 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12134 {
12135 error (_("Dwarf Error: bad DWP hash table, too many columns"
12136 " in section table [in module %s]"),
12137 dwp_file->name);
12138 }
12139 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12140 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12141 for (i = 0; i < nr_columns; ++i)
12142 {
12143 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12144
12145 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12146 {
12147 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12148 " in section table [in module %s]"),
12149 id, dwp_file->name);
12150 }
12151 if (ids_seen[id] != -1)
12152 {
12153 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12154 " id %d in section table [in module %s]"),
12155 id, dwp_file->name);
12156 }
12157 ids_seen[id] = i;
12158 ids[i] = id;
12159 }
12160 /* Must have exactly one info or types section. */
12161 if (((ids_seen[DW_SECT_INFO] != -1)
12162 + (ids_seen[DW_SECT_TYPES] != -1))
12163 != 1)
12164 {
12165 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12166 " DWO info/types section [in module %s]"),
12167 dwp_file->name);
12168 }
12169 /* Must have an abbrev section. */
12170 if (ids_seen[DW_SECT_ABBREV] == -1)
12171 {
12172 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12173 " section [in module %s]"),
12174 dwp_file->name);
12175 }
12176 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12177 htab->section_pool.v2.sizes =
12178 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12179 * nr_units * nr_columns);
12180 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12181 * nr_units * nr_columns))
12182 > index_end)
12183 {
12184 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12185 " [in module %s]"),
12186 dwp_file->name);
12187 }
12188 }
12189
12190 return htab;
12191 }
12192
12193 /* Update SECTIONS with the data from SECTP.
12194
12195 This function is like the other "locate" section routines that are
12196 passed to bfd_map_over_sections, but in this context the sections to
12197 read comes from the DWP V1 hash table, not the full ELF section table.
12198
12199 The result is non-zero for success, or zero if an error was found. */
12200
12201 static int
12202 locate_v1_virtual_dwo_sections (asection *sectp,
12203 struct virtual_v1_dwo_sections *sections)
12204 {
12205 const struct dwop_section_names *names = &dwop_section_names;
12206
12207 if (section_is_p (sectp->name, &names->abbrev_dwo))
12208 {
12209 /* There can be only one. */
12210 if (sections->abbrev.s.section != NULL)
12211 return 0;
12212 sections->abbrev.s.section = sectp;
12213 sections->abbrev.size = bfd_get_section_size (sectp);
12214 }
12215 else if (section_is_p (sectp->name, &names->info_dwo)
12216 || section_is_p (sectp->name, &names->types_dwo))
12217 {
12218 /* There can be only one. */
12219 if (sections->info_or_types.s.section != NULL)
12220 return 0;
12221 sections->info_or_types.s.section = sectp;
12222 sections->info_or_types.size = bfd_get_section_size (sectp);
12223 }
12224 else if (section_is_p (sectp->name, &names->line_dwo))
12225 {
12226 /* There can be only one. */
12227 if (sections->line.s.section != NULL)
12228 return 0;
12229 sections->line.s.section = sectp;
12230 sections->line.size = bfd_get_section_size (sectp);
12231 }
12232 else if (section_is_p (sectp->name, &names->loc_dwo))
12233 {
12234 /* There can be only one. */
12235 if (sections->loc.s.section != NULL)
12236 return 0;
12237 sections->loc.s.section = sectp;
12238 sections->loc.size = bfd_get_section_size (sectp);
12239 }
12240 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12241 {
12242 /* There can be only one. */
12243 if (sections->macinfo.s.section != NULL)
12244 return 0;
12245 sections->macinfo.s.section = sectp;
12246 sections->macinfo.size = bfd_get_section_size (sectp);
12247 }
12248 else if (section_is_p (sectp->name, &names->macro_dwo))
12249 {
12250 /* There can be only one. */
12251 if (sections->macro.s.section != NULL)
12252 return 0;
12253 sections->macro.s.section = sectp;
12254 sections->macro.size = bfd_get_section_size (sectp);
12255 }
12256 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12257 {
12258 /* There can be only one. */
12259 if (sections->str_offsets.s.section != NULL)
12260 return 0;
12261 sections->str_offsets.s.section = sectp;
12262 sections->str_offsets.size = bfd_get_section_size (sectp);
12263 }
12264 else
12265 {
12266 /* No other kind of section is valid. */
12267 return 0;
12268 }
12269
12270 return 1;
12271 }
12272
12273 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12274 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12275 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12276 This is for DWP version 1 files. */
12277
12278 static struct dwo_unit *
12279 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12280 struct dwp_file *dwp_file,
12281 uint32_t unit_index,
12282 const char *comp_dir,
12283 ULONGEST signature, int is_debug_types)
12284 {
12285 struct objfile *objfile = dwarf2_per_objfile->objfile;
12286 const struct dwp_hash_table *dwp_htab =
12287 is_debug_types ? dwp_file->tus : dwp_file->cus;
12288 bfd *dbfd = dwp_file->dbfd.get ();
12289 const char *kind = is_debug_types ? "TU" : "CU";
12290 struct dwo_file *dwo_file;
12291 struct dwo_unit *dwo_unit;
12292 struct virtual_v1_dwo_sections sections;
12293 void **dwo_file_slot;
12294 int i;
12295
12296 gdb_assert (dwp_file->version == 1);
12297
12298 if (dwarf_read_debug)
12299 {
12300 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
12301 kind,
12302 pulongest (unit_index), hex_string (signature),
12303 dwp_file->name);
12304 }
12305
12306 /* Fetch the sections of this DWO unit.
12307 Put a limit on the number of sections we look for so that bad data
12308 doesn't cause us to loop forever. */
12309
12310 #define MAX_NR_V1_DWO_SECTIONS \
12311 (1 /* .debug_info or .debug_types */ \
12312 + 1 /* .debug_abbrev */ \
12313 + 1 /* .debug_line */ \
12314 + 1 /* .debug_loc */ \
12315 + 1 /* .debug_str_offsets */ \
12316 + 1 /* .debug_macro or .debug_macinfo */ \
12317 + 1 /* trailing zero */)
12318
12319 memset (&sections, 0, sizeof (sections));
12320
12321 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12322 {
12323 asection *sectp;
12324 uint32_t section_nr =
12325 read_4_bytes (dbfd,
12326 dwp_htab->section_pool.v1.indices
12327 + (unit_index + i) * sizeof (uint32_t));
12328
12329 if (section_nr == 0)
12330 break;
12331 if (section_nr >= dwp_file->num_sections)
12332 {
12333 error (_("Dwarf Error: bad DWP hash table, section number too large"
12334 " [in module %s]"),
12335 dwp_file->name);
12336 }
12337
12338 sectp = dwp_file->elf_sections[section_nr];
12339 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12340 {
12341 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12342 " [in module %s]"),
12343 dwp_file->name);
12344 }
12345 }
12346
12347 if (i < 2
12348 || dwarf2_section_empty_p (&sections.info_or_types)
12349 || dwarf2_section_empty_p (&sections.abbrev))
12350 {
12351 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12352 " [in module %s]"),
12353 dwp_file->name);
12354 }
12355 if (i == MAX_NR_V1_DWO_SECTIONS)
12356 {
12357 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12358 " [in module %s]"),
12359 dwp_file->name);
12360 }
12361
12362 /* It's easier for the rest of the code if we fake a struct dwo_file and
12363 have dwo_unit "live" in that. At least for now.
12364
12365 The DWP file can be made up of a random collection of CUs and TUs.
12366 However, for each CU + set of TUs that came from the same original DWO
12367 file, we can combine them back into a virtual DWO file to save space
12368 (fewer struct dwo_file objects to allocate). Remember that for really
12369 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12370
12371 std::string virtual_dwo_name =
12372 string_printf ("virtual-dwo/%d-%d-%d-%d",
12373 get_section_id (&sections.abbrev),
12374 get_section_id (&sections.line),
12375 get_section_id (&sections.loc),
12376 get_section_id (&sections.str_offsets));
12377 /* Can we use an existing virtual DWO file? */
12378 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12379 virtual_dwo_name.c_str (),
12380 comp_dir);
12381 /* Create one if necessary. */
12382 if (*dwo_file_slot == NULL)
12383 {
12384 if (dwarf_read_debug)
12385 {
12386 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12387 virtual_dwo_name.c_str ());
12388 }
12389 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
12390 dwo_file->dwo_name
12391 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
12392 virtual_dwo_name.c_str (),
12393 virtual_dwo_name.size ());
12394 dwo_file->comp_dir = comp_dir;
12395 dwo_file->sections.abbrev = sections.abbrev;
12396 dwo_file->sections.line = sections.line;
12397 dwo_file->sections.loc = sections.loc;
12398 dwo_file->sections.macinfo = sections.macinfo;
12399 dwo_file->sections.macro = sections.macro;
12400 dwo_file->sections.str_offsets = sections.str_offsets;
12401 /* The "str" section is global to the entire DWP file. */
12402 dwo_file->sections.str = dwp_file->sections.str;
12403 /* The info or types section is assigned below to dwo_unit,
12404 there's no need to record it in dwo_file.
12405 Also, we can't simply record type sections in dwo_file because
12406 we record a pointer into the vector in dwo_unit. As we collect more
12407 types we'll grow the vector and eventually have to reallocate space
12408 for it, invalidating all copies of pointers into the previous
12409 contents. */
12410 *dwo_file_slot = dwo_file;
12411 }
12412 else
12413 {
12414 if (dwarf_read_debug)
12415 {
12416 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12417 virtual_dwo_name.c_str ());
12418 }
12419 dwo_file = (struct dwo_file *) *dwo_file_slot;
12420 }
12421
12422 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12423 dwo_unit->dwo_file = dwo_file;
12424 dwo_unit->signature = signature;
12425 dwo_unit->section =
12426 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12427 *dwo_unit->section = sections.info_or_types;
12428 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12429
12430 return dwo_unit;
12431 }
12432
12433 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12434 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12435 piece within that section used by a TU/CU, return a virtual section
12436 of just that piece. */
12437
12438 static struct dwarf2_section_info
12439 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12440 struct dwarf2_section_info *section,
12441 bfd_size_type offset, bfd_size_type size)
12442 {
12443 struct dwarf2_section_info result;
12444 asection *sectp;
12445
12446 gdb_assert (section != NULL);
12447 gdb_assert (!section->is_virtual);
12448
12449 memset (&result, 0, sizeof (result));
12450 result.s.containing_section = section;
12451 result.is_virtual = 1;
12452
12453 if (size == 0)
12454 return result;
12455
12456 sectp = get_section_bfd_section (section);
12457
12458 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12459 bounds of the real section. This is a pretty-rare event, so just
12460 flag an error (easier) instead of a warning and trying to cope. */
12461 if (sectp == NULL
12462 || offset + size > bfd_get_section_size (sectp))
12463 {
12464 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12465 " in section %s [in module %s]"),
12466 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
12467 objfile_name (dwarf2_per_objfile->objfile));
12468 }
12469
12470 result.virtual_offset = offset;
12471 result.size = size;
12472 return result;
12473 }
12474
12475 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12476 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12477 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12478 This is for DWP version 2 files. */
12479
12480 static struct dwo_unit *
12481 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12482 struct dwp_file *dwp_file,
12483 uint32_t unit_index,
12484 const char *comp_dir,
12485 ULONGEST signature, int is_debug_types)
12486 {
12487 struct objfile *objfile = dwarf2_per_objfile->objfile;
12488 const struct dwp_hash_table *dwp_htab =
12489 is_debug_types ? dwp_file->tus : dwp_file->cus;
12490 bfd *dbfd = dwp_file->dbfd.get ();
12491 const char *kind = is_debug_types ? "TU" : "CU";
12492 struct dwo_file *dwo_file;
12493 struct dwo_unit *dwo_unit;
12494 struct virtual_v2_dwo_sections sections;
12495 void **dwo_file_slot;
12496 int i;
12497
12498 gdb_assert (dwp_file->version == 2);
12499
12500 if (dwarf_read_debug)
12501 {
12502 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12503 kind,
12504 pulongest (unit_index), hex_string (signature),
12505 dwp_file->name);
12506 }
12507
12508 /* Fetch the section offsets of this DWO unit. */
12509
12510 memset (&sections, 0, sizeof (sections));
12511
12512 for (i = 0; i < dwp_htab->nr_columns; ++i)
12513 {
12514 uint32_t offset = read_4_bytes (dbfd,
12515 dwp_htab->section_pool.v2.offsets
12516 + (((unit_index - 1) * dwp_htab->nr_columns
12517 + i)
12518 * sizeof (uint32_t)));
12519 uint32_t size = read_4_bytes (dbfd,
12520 dwp_htab->section_pool.v2.sizes
12521 + (((unit_index - 1) * dwp_htab->nr_columns
12522 + i)
12523 * sizeof (uint32_t)));
12524
12525 switch (dwp_htab->section_pool.v2.section_ids[i])
12526 {
12527 case DW_SECT_INFO:
12528 case DW_SECT_TYPES:
12529 sections.info_or_types_offset = offset;
12530 sections.info_or_types_size = size;
12531 break;
12532 case DW_SECT_ABBREV:
12533 sections.abbrev_offset = offset;
12534 sections.abbrev_size = size;
12535 break;
12536 case DW_SECT_LINE:
12537 sections.line_offset = offset;
12538 sections.line_size = size;
12539 break;
12540 case DW_SECT_LOC:
12541 sections.loc_offset = offset;
12542 sections.loc_size = size;
12543 break;
12544 case DW_SECT_STR_OFFSETS:
12545 sections.str_offsets_offset = offset;
12546 sections.str_offsets_size = size;
12547 break;
12548 case DW_SECT_MACINFO:
12549 sections.macinfo_offset = offset;
12550 sections.macinfo_size = size;
12551 break;
12552 case DW_SECT_MACRO:
12553 sections.macro_offset = offset;
12554 sections.macro_size = size;
12555 break;
12556 }
12557 }
12558
12559 /* It's easier for the rest of the code if we fake a struct dwo_file and
12560 have dwo_unit "live" in that. At least for now.
12561
12562 The DWP file can be made up of a random collection of CUs and TUs.
12563 However, for each CU + set of TUs that came from the same original DWO
12564 file, we can combine them back into a virtual DWO file to save space
12565 (fewer struct dwo_file objects to allocate). Remember that for really
12566 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12567
12568 std::string virtual_dwo_name =
12569 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12570 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12571 (long) (sections.line_size ? sections.line_offset : 0),
12572 (long) (sections.loc_size ? sections.loc_offset : 0),
12573 (long) (sections.str_offsets_size
12574 ? sections.str_offsets_offset : 0));
12575 /* Can we use an existing virtual DWO file? */
12576 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12577 virtual_dwo_name.c_str (),
12578 comp_dir);
12579 /* Create one if necessary. */
12580 if (*dwo_file_slot == NULL)
12581 {
12582 if (dwarf_read_debug)
12583 {
12584 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12585 virtual_dwo_name.c_str ());
12586 }
12587 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
12588 dwo_file->dwo_name
12589 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
12590 virtual_dwo_name.c_str (),
12591 virtual_dwo_name.size ());
12592 dwo_file->comp_dir = comp_dir;
12593 dwo_file->sections.abbrev =
12594 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12595 sections.abbrev_offset, sections.abbrev_size);
12596 dwo_file->sections.line =
12597 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12598 sections.line_offset, sections.line_size);
12599 dwo_file->sections.loc =
12600 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12601 sections.loc_offset, sections.loc_size);
12602 dwo_file->sections.macinfo =
12603 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12604 sections.macinfo_offset, sections.macinfo_size);
12605 dwo_file->sections.macro =
12606 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12607 sections.macro_offset, sections.macro_size);
12608 dwo_file->sections.str_offsets =
12609 create_dwp_v2_section (dwarf2_per_objfile,
12610 &dwp_file->sections.str_offsets,
12611 sections.str_offsets_offset,
12612 sections.str_offsets_size);
12613 /* The "str" section is global to the entire DWP file. */
12614 dwo_file->sections.str = dwp_file->sections.str;
12615 /* The info or types section is assigned below to dwo_unit,
12616 there's no need to record it in dwo_file.
12617 Also, we can't simply record type sections in dwo_file because
12618 we record a pointer into the vector in dwo_unit. As we collect more
12619 types we'll grow the vector and eventually have to reallocate space
12620 for it, invalidating all copies of pointers into the previous
12621 contents. */
12622 *dwo_file_slot = dwo_file;
12623 }
12624 else
12625 {
12626 if (dwarf_read_debug)
12627 {
12628 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12629 virtual_dwo_name.c_str ());
12630 }
12631 dwo_file = (struct dwo_file *) *dwo_file_slot;
12632 }
12633
12634 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12635 dwo_unit->dwo_file = dwo_file;
12636 dwo_unit->signature = signature;
12637 dwo_unit->section =
12638 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12639 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12640 is_debug_types
12641 ? &dwp_file->sections.types
12642 : &dwp_file->sections.info,
12643 sections.info_or_types_offset,
12644 sections.info_or_types_size);
12645 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12646
12647 return dwo_unit;
12648 }
12649
12650 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12651 Returns NULL if the signature isn't found. */
12652
12653 static struct dwo_unit *
12654 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12655 struct dwp_file *dwp_file, const char *comp_dir,
12656 ULONGEST signature, int is_debug_types)
12657 {
12658 const struct dwp_hash_table *dwp_htab =
12659 is_debug_types ? dwp_file->tus : dwp_file->cus;
12660 bfd *dbfd = dwp_file->dbfd.get ();
12661 uint32_t mask = dwp_htab->nr_slots - 1;
12662 uint32_t hash = signature & mask;
12663 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12664 unsigned int i;
12665 void **slot;
12666 struct dwo_unit find_dwo_cu;
12667
12668 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12669 find_dwo_cu.signature = signature;
12670 slot = htab_find_slot (is_debug_types
12671 ? dwp_file->loaded_tus
12672 : dwp_file->loaded_cus,
12673 &find_dwo_cu, INSERT);
12674
12675 if (*slot != NULL)
12676 return (struct dwo_unit *) *slot;
12677
12678 /* Use a for loop so that we don't loop forever on bad debug info. */
12679 for (i = 0; i < dwp_htab->nr_slots; ++i)
12680 {
12681 ULONGEST signature_in_table;
12682
12683 signature_in_table =
12684 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12685 if (signature_in_table == signature)
12686 {
12687 uint32_t unit_index =
12688 read_4_bytes (dbfd,
12689 dwp_htab->unit_table + hash * sizeof (uint32_t));
12690
12691 if (dwp_file->version == 1)
12692 {
12693 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12694 dwp_file, unit_index,
12695 comp_dir, signature,
12696 is_debug_types);
12697 }
12698 else
12699 {
12700 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12701 dwp_file, unit_index,
12702 comp_dir, signature,
12703 is_debug_types);
12704 }
12705 return (struct dwo_unit *) *slot;
12706 }
12707 if (signature_in_table == 0)
12708 return NULL;
12709 hash = (hash + hash2) & mask;
12710 }
12711
12712 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12713 " [in module %s]"),
12714 dwp_file->name);
12715 }
12716
12717 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12718 Open the file specified by FILE_NAME and hand it off to BFD for
12719 preliminary analysis. Return a newly initialized bfd *, which
12720 includes a canonicalized copy of FILE_NAME.
12721 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12722 SEARCH_CWD is true if the current directory is to be searched.
12723 It will be searched before debug-file-directory.
12724 If successful, the file is added to the bfd include table of the
12725 objfile's bfd (see gdb_bfd_record_inclusion).
12726 If unable to find/open the file, return NULL.
12727 NOTE: This function is derived from symfile_bfd_open. */
12728
12729 static gdb_bfd_ref_ptr
12730 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12731 const char *file_name, int is_dwp, int search_cwd)
12732 {
12733 int desc;
12734 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12735 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12736 to debug_file_directory. */
12737 const char *search_path;
12738 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12739
12740 gdb::unique_xmalloc_ptr<char> search_path_holder;
12741 if (search_cwd)
12742 {
12743 if (*debug_file_directory != '\0')
12744 {
12745 search_path_holder.reset (concat (".", dirname_separator_string,
12746 debug_file_directory,
12747 (char *) NULL));
12748 search_path = search_path_holder.get ();
12749 }
12750 else
12751 search_path = ".";
12752 }
12753 else
12754 search_path = debug_file_directory;
12755
12756 openp_flags flags = OPF_RETURN_REALPATH;
12757 if (is_dwp)
12758 flags |= OPF_SEARCH_IN_PATH;
12759
12760 gdb::unique_xmalloc_ptr<char> absolute_name;
12761 desc = openp (search_path, flags, file_name,
12762 O_RDONLY | O_BINARY, &absolute_name);
12763 if (desc < 0)
12764 return NULL;
12765
12766 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12767 gnutarget, desc));
12768 if (sym_bfd == NULL)
12769 return NULL;
12770 bfd_set_cacheable (sym_bfd.get (), 1);
12771
12772 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12773 return NULL;
12774
12775 /* Success. Record the bfd as having been included by the objfile's bfd.
12776 This is important because things like demangled_names_hash lives in the
12777 objfile's per_bfd space and may have references to things like symbol
12778 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12779 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12780
12781 return sym_bfd;
12782 }
12783
12784 /* Try to open DWO file FILE_NAME.
12785 COMP_DIR is the DW_AT_comp_dir attribute.
12786 The result is the bfd handle of the file.
12787 If there is a problem finding or opening the file, return NULL.
12788 Upon success, the canonicalized path of the file is stored in the bfd,
12789 same as symfile_bfd_open. */
12790
12791 static gdb_bfd_ref_ptr
12792 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12793 const char *file_name, const char *comp_dir)
12794 {
12795 if (IS_ABSOLUTE_PATH (file_name))
12796 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12797 0 /*is_dwp*/, 0 /*search_cwd*/);
12798
12799 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12800
12801 if (comp_dir != NULL)
12802 {
12803 char *path_to_try = concat (comp_dir, SLASH_STRING,
12804 file_name, (char *) NULL);
12805
12806 /* NOTE: If comp_dir is a relative path, this will also try the
12807 search path, which seems useful. */
12808 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12809 path_to_try,
12810 0 /*is_dwp*/,
12811 1 /*search_cwd*/));
12812 xfree (path_to_try);
12813 if (abfd != NULL)
12814 return abfd;
12815 }
12816
12817 /* That didn't work, try debug-file-directory, which, despite its name,
12818 is a list of paths. */
12819
12820 if (*debug_file_directory == '\0')
12821 return NULL;
12822
12823 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12824 0 /*is_dwp*/, 1 /*search_cwd*/);
12825 }
12826
12827 /* This function is mapped across the sections and remembers the offset and
12828 size of each of the DWO debugging sections we are interested in. */
12829
12830 static void
12831 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12832 {
12833 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12834 const struct dwop_section_names *names = &dwop_section_names;
12835
12836 if (section_is_p (sectp->name, &names->abbrev_dwo))
12837 {
12838 dwo_sections->abbrev.s.section = sectp;
12839 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
12840 }
12841 else if (section_is_p (sectp->name, &names->info_dwo))
12842 {
12843 dwo_sections->info.s.section = sectp;
12844 dwo_sections->info.size = bfd_get_section_size (sectp);
12845 }
12846 else if (section_is_p (sectp->name, &names->line_dwo))
12847 {
12848 dwo_sections->line.s.section = sectp;
12849 dwo_sections->line.size = bfd_get_section_size (sectp);
12850 }
12851 else if (section_is_p (sectp->name, &names->loc_dwo))
12852 {
12853 dwo_sections->loc.s.section = sectp;
12854 dwo_sections->loc.size = bfd_get_section_size (sectp);
12855 }
12856 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12857 {
12858 dwo_sections->macinfo.s.section = sectp;
12859 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
12860 }
12861 else if (section_is_p (sectp->name, &names->macro_dwo))
12862 {
12863 dwo_sections->macro.s.section = sectp;
12864 dwo_sections->macro.size = bfd_get_section_size (sectp);
12865 }
12866 else if (section_is_p (sectp->name, &names->str_dwo))
12867 {
12868 dwo_sections->str.s.section = sectp;
12869 dwo_sections->str.size = bfd_get_section_size (sectp);
12870 }
12871 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12872 {
12873 dwo_sections->str_offsets.s.section = sectp;
12874 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
12875 }
12876 else if (section_is_p (sectp->name, &names->types_dwo))
12877 {
12878 struct dwarf2_section_info type_section;
12879
12880 memset (&type_section, 0, sizeof (type_section));
12881 type_section.s.section = sectp;
12882 type_section.size = bfd_get_section_size (sectp);
12883 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
12884 &type_section);
12885 }
12886 }
12887
12888 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12889 by PER_CU. This is for the non-DWP case.
12890 The result is NULL if DWO_NAME can't be found. */
12891
12892 static struct dwo_file *
12893 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12894 const char *dwo_name, const char *comp_dir)
12895 {
12896 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12897 struct objfile *objfile = dwarf2_per_objfile->objfile;
12898
12899 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir));
12900 if (dbfd == NULL)
12901 {
12902 if (dwarf_read_debug)
12903 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12904 return NULL;
12905 }
12906
12907 /* We use a unique pointer here, despite the obstack allocation,
12908 because a dwo_file needs some cleanup if it is abandoned. */
12909 dwo_file_up dwo_file (OBSTACK_ZALLOC (&objfile->objfile_obstack,
12910 struct dwo_file));
12911 dwo_file->dwo_name = dwo_name;
12912 dwo_file->comp_dir = comp_dir;
12913 dwo_file->dbfd = dbfd.release ();
12914
12915 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
12916 &dwo_file->sections);
12917
12918 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
12919 dwo_file->cus);
12920
12921 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12922 dwo_file->sections.types, dwo_file->tus);
12923
12924 if (dwarf_read_debug)
12925 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12926
12927 return dwo_file.release ();
12928 }
12929
12930 /* This function is mapped across the sections and remembers the offset and
12931 size of each of the DWP debugging sections common to version 1 and 2 that
12932 we are interested in. */
12933
12934 static void
12935 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12936 void *dwp_file_ptr)
12937 {
12938 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12939 const struct dwop_section_names *names = &dwop_section_names;
12940 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12941
12942 /* Record the ELF section number for later lookup: this is what the
12943 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12944 gdb_assert (elf_section_nr < dwp_file->num_sections);
12945 dwp_file->elf_sections[elf_section_nr] = sectp;
12946
12947 /* Look for specific sections that we need. */
12948 if (section_is_p (sectp->name, &names->str_dwo))
12949 {
12950 dwp_file->sections.str.s.section = sectp;
12951 dwp_file->sections.str.size = bfd_get_section_size (sectp);
12952 }
12953 else if (section_is_p (sectp->name, &names->cu_index))
12954 {
12955 dwp_file->sections.cu_index.s.section = sectp;
12956 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
12957 }
12958 else if (section_is_p (sectp->name, &names->tu_index))
12959 {
12960 dwp_file->sections.tu_index.s.section = sectp;
12961 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
12962 }
12963 }
12964
12965 /* This function is mapped across the sections and remembers the offset and
12966 size of each of the DWP version 2 debugging sections that we are interested
12967 in. This is split into a separate function because we don't know if we
12968 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12969
12970 static void
12971 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12972 {
12973 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12974 const struct dwop_section_names *names = &dwop_section_names;
12975 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12976
12977 /* Record the ELF section number for later lookup: this is what the
12978 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12979 gdb_assert (elf_section_nr < dwp_file->num_sections);
12980 dwp_file->elf_sections[elf_section_nr] = sectp;
12981
12982 /* Look for specific sections that we need. */
12983 if (section_is_p (sectp->name, &names->abbrev_dwo))
12984 {
12985 dwp_file->sections.abbrev.s.section = sectp;
12986 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
12987 }
12988 else if (section_is_p (sectp->name, &names->info_dwo))
12989 {
12990 dwp_file->sections.info.s.section = sectp;
12991 dwp_file->sections.info.size = bfd_get_section_size (sectp);
12992 }
12993 else if (section_is_p (sectp->name, &names->line_dwo))
12994 {
12995 dwp_file->sections.line.s.section = sectp;
12996 dwp_file->sections.line.size = bfd_get_section_size (sectp);
12997 }
12998 else if (section_is_p (sectp->name, &names->loc_dwo))
12999 {
13000 dwp_file->sections.loc.s.section = sectp;
13001 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
13002 }
13003 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13004 {
13005 dwp_file->sections.macinfo.s.section = sectp;
13006 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
13007 }
13008 else if (section_is_p (sectp->name, &names->macro_dwo))
13009 {
13010 dwp_file->sections.macro.s.section = sectp;
13011 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
13012 }
13013 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13014 {
13015 dwp_file->sections.str_offsets.s.section = sectp;
13016 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
13017 }
13018 else if (section_is_p (sectp->name, &names->types_dwo))
13019 {
13020 dwp_file->sections.types.s.section = sectp;
13021 dwp_file->sections.types.size = bfd_get_section_size (sectp);
13022 }
13023 }
13024
13025 /* Hash function for dwp_file loaded CUs/TUs. */
13026
13027 static hashval_t
13028 hash_dwp_loaded_cutus (const void *item)
13029 {
13030 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13031
13032 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13033 return dwo_unit->signature;
13034 }
13035
13036 /* Equality function for dwp_file loaded CUs/TUs. */
13037
13038 static int
13039 eq_dwp_loaded_cutus (const void *a, const void *b)
13040 {
13041 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13042 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13043
13044 return dua->signature == dub->signature;
13045 }
13046
13047 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13048
13049 static htab_t
13050 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13051 {
13052 return htab_create_alloc_ex (3,
13053 hash_dwp_loaded_cutus,
13054 eq_dwp_loaded_cutus,
13055 NULL,
13056 &objfile->objfile_obstack,
13057 hashtab_obstack_allocate,
13058 dummy_obstack_deallocate);
13059 }
13060
13061 /* Try to open DWP file FILE_NAME.
13062 The result is the bfd handle of the file.
13063 If there is a problem finding or opening the file, return NULL.
13064 Upon success, the canonicalized path of the file is stored in the bfd,
13065 same as symfile_bfd_open. */
13066
13067 static gdb_bfd_ref_ptr
13068 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13069 const char *file_name)
13070 {
13071 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13072 1 /*is_dwp*/,
13073 1 /*search_cwd*/));
13074 if (abfd != NULL)
13075 return abfd;
13076
13077 /* Work around upstream bug 15652.
13078 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13079 [Whether that's a "bug" is debatable, but it is getting in our way.]
13080 We have no real idea where the dwp file is, because gdb's realpath-ing
13081 of the executable's path may have discarded the needed info.
13082 [IWBN if the dwp file name was recorded in the executable, akin to
13083 .gnu_debuglink, but that doesn't exist yet.]
13084 Strip the directory from FILE_NAME and search again. */
13085 if (*debug_file_directory != '\0')
13086 {
13087 /* Don't implicitly search the current directory here.
13088 If the user wants to search "." to handle this case,
13089 it must be added to debug-file-directory. */
13090 return try_open_dwop_file (dwarf2_per_objfile,
13091 lbasename (file_name), 1 /*is_dwp*/,
13092 0 /*search_cwd*/);
13093 }
13094
13095 return NULL;
13096 }
13097
13098 /* Initialize the use of the DWP file for the current objfile.
13099 By convention the name of the DWP file is ${objfile}.dwp.
13100 The result is NULL if it can't be found. */
13101
13102 static std::unique_ptr<struct dwp_file>
13103 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13104 {
13105 struct objfile *objfile = dwarf2_per_objfile->objfile;
13106
13107 /* Try to find first .dwp for the binary file before any symbolic links
13108 resolving. */
13109
13110 /* If the objfile is a debug file, find the name of the real binary
13111 file and get the name of dwp file from there. */
13112 std::string dwp_name;
13113 if (objfile->separate_debug_objfile_backlink != NULL)
13114 {
13115 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13116 const char *backlink_basename = lbasename (backlink->original_name);
13117
13118 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13119 }
13120 else
13121 dwp_name = objfile->original_name;
13122
13123 dwp_name += ".dwp";
13124
13125 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
13126 if (dbfd == NULL
13127 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13128 {
13129 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13130 dwp_name = objfile_name (objfile);
13131 dwp_name += ".dwp";
13132 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
13133 }
13134
13135 if (dbfd == NULL)
13136 {
13137 if (dwarf_read_debug)
13138 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
13139 return std::unique_ptr<dwp_file> ();
13140 }
13141
13142 const char *name = bfd_get_filename (dbfd.get ());
13143 std::unique_ptr<struct dwp_file> dwp_file
13144 (new struct dwp_file (name, std::move (dbfd)));
13145
13146 /* +1: section 0 is unused */
13147 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
13148 dwp_file->elf_sections =
13149 OBSTACK_CALLOC (&objfile->objfile_obstack,
13150 dwp_file->num_sections, asection *);
13151
13152 bfd_map_over_sections (dwp_file->dbfd.get (),
13153 dwarf2_locate_common_dwp_sections,
13154 dwp_file.get ());
13155
13156 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13157 0);
13158
13159 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13160 1);
13161
13162 /* The DWP file version is stored in the hash table. Oh well. */
13163 if (dwp_file->cus && dwp_file->tus
13164 && dwp_file->cus->version != dwp_file->tus->version)
13165 {
13166 /* Technically speaking, we should try to limp along, but this is
13167 pretty bizarre. We use pulongest here because that's the established
13168 portability solution (e.g, we cannot use %u for uint32_t). */
13169 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13170 " TU version %s [in DWP file %s]"),
13171 pulongest (dwp_file->cus->version),
13172 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13173 }
13174
13175 if (dwp_file->cus)
13176 dwp_file->version = dwp_file->cus->version;
13177 else if (dwp_file->tus)
13178 dwp_file->version = dwp_file->tus->version;
13179 else
13180 dwp_file->version = 2;
13181
13182 if (dwp_file->version == 2)
13183 bfd_map_over_sections (dwp_file->dbfd.get (),
13184 dwarf2_locate_v2_dwp_sections,
13185 dwp_file.get ());
13186
13187 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13188 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
13189
13190 if (dwarf_read_debug)
13191 {
13192 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13193 fprintf_unfiltered (gdb_stdlog,
13194 " %s CUs, %s TUs\n",
13195 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13196 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13197 }
13198
13199 return dwp_file;
13200 }
13201
13202 /* Wrapper around open_and_init_dwp_file, only open it once. */
13203
13204 static struct dwp_file *
13205 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13206 {
13207 if (! dwarf2_per_objfile->dwp_checked)
13208 {
13209 dwarf2_per_objfile->dwp_file
13210 = open_and_init_dwp_file (dwarf2_per_objfile);
13211 dwarf2_per_objfile->dwp_checked = 1;
13212 }
13213 return dwarf2_per_objfile->dwp_file.get ();
13214 }
13215
13216 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13217 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13218 or in the DWP file for the objfile, referenced by THIS_UNIT.
13219 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13220 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13221
13222 This is called, for example, when wanting to read a variable with a
13223 complex location. Therefore we don't want to do file i/o for every call.
13224 Therefore we don't want to look for a DWO file on every call.
13225 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13226 then we check if we've already seen DWO_NAME, and only THEN do we check
13227 for a DWO file.
13228
13229 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13230 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13231
13232 static struct dwo_unit *
13233 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13234 const char *dwo_name, const char *comp_dir,
13235 ULONGEST signature, int is_debug_types)
13236 {
13237 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
13238 struct objfile *objfile = dwarf2_per_objfile->objfile;
13239 const char *kind = is_debug_types ? "TU" : "CU";
13240 void **dwo_file_slot;
13241 struct dwo_file *dwo_file;
13242 struct dwp_file *dwp_file;
13243
13244 /* First see if there's a DWP file.
13245 If we have a DWP file but didn't find the DWO inside it, don't
13246 look for the original DWO file. It makes gdb behave differently
13247 depending on whether one is debugging in the build tree. */
13248
13249 dwp_file = get_dwp_file (dwarf2_per_objfile);
13250 if (dwp_file != NULL)
13251 {
13252 const struct dwp_hash_table *dwp_htab =
13253 is_debug_types ? dwp_file->tus : dwp_file->cus;
13254
13255 if (dwp_htab != NULL)
13256 {
13257 struct dwo_unit *dwo_cutu =
13258 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
13259 signature, is_debug_types);
13260
13261 if (dwo_cutu != NULL)
13262 {
13263 if (dwarf_read_debug)
13264 {
13265 fprintf_unfiltered (gdb_stdlog,
13266 "Virtual DWO %s %s found: @%s\n",
13267 kind, hex_string (signature),
13268 host_address_to_string (dwo_cutu));
13269 }
13270 return dwo_cutu;
13271 }
13272 }
13273 }
13274 else
13275 {
13276 /* No DWP file, look for the DWO file. */
13277
13278 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13279 dwo_name, comp_dir);
13280 if (*dwo_file_slot == NULL)
13281 {
13282 /* Read in the file and build a table of the CUs/TUs it contains. */
13283 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
13284 }
13285 /* NOTE: This will be NULL if unable to open the file. */
13286 dwo_file = (struct dwo_file *) *dwo_file_slot;
13287
13288 if (dwo_file != NULL)
13289 {
13290 struct dwo_unit *dwo_cutu = NULL;
13291
13292 if (is_debug_types && dwo_file->tus)
13293 {
13294 struct dwo_unit find_dwo_cutu;
13295
13296 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13297 find_dwo_cutu.signature = signature;
13298 dwo_cutu
13299 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
13300 }
13301 else if (!is_debug_types && dwo_file->cus)
13302 {
13303 struct dwo_unit find_dwo_cutu;
13304
13305 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13306 find_dwo_cutu.signature = signature;
13307 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13308 &find_dwo_cutu);
13309 }
13310
13311 if (dwo_cutu != NULL)
13312 {
13313 if (dwarf_read_debug)
13314 {
13315 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13316 kind, dwo_name, hex_string (signature),
13317 host_address_to_string (dwo_cutu));
13318 }
13319 return dwo_cutu;
13320 }
13321 }
13322 }
13323
13324 /* We didn't find it. This could mean a dwo_id mismatch, or
13325 someone deleted the DWO/DWP file, or the search path isn't set up
13326 correctly to find the file. */
13327
13328 if (dwarf_read_debug)
13329 {
13330 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13331 kind, dwo_name, hex_string (signature));
13332 }
13333
13334 /* This is a warning and not a complaint because it can be caused by
13335 pilot error (e.g., user accidentally deleting the DWO). */
13336 {
13337 /* Print the name of the DWP file if we looked there, helps the user
13338 better diagnose the problem. */
13339 std::string dwp_text;
13340
13341 if (dwp_file != NULL)
13342 dwp_text = string_printf (" [in DWP file %s]",
13343 lbasename (dwp_file->name));
13344
13345 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13346 " [in module %s]"),
13347 kind, dwo_name, hex_string (signature),
13348 dwp_text.c_str (),
13349 this_unit->is_debug_types ? "TU" : "CU",
13350 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
13351 }
13352 return NULL;
13353 }
13354
13355 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13356 See lookup_dwo_cutu_unit for details. */
13357
13358 static struct dwo_unit *
13359 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13360 const char *dwo_name, const char *comp_dir,
13361 ULONGEST signature)
13362 {
13363 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13364 }
13365
13366 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13367 See lookup_dwo_cutu_unit for details. */
13368
13369 static struct dwo_unit *
13370 lookup_dwo_type_unit (struct signatured_type *this_tu,
13371 const char *dwo_name, const char *comp_dir)
13372 {
13373 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13374 }
13375
13376 /* Traversal function for queue_and_load_all_dwo_tus. */
13377
13378 static int
13379 queue_and_load_dwo_tu (void **slot, void *info)
13380 {
13381 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13382 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13383 ULONGEST signature = dwo_unit->signature;
13384 struct signatured_type *sig_type =
13385 lookup_dwo_signatured_type (per_cu->cu, signature);
13386
13387 if (sig_type != NULL)
13388 {
13389 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13390
13391 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13392 a real dependency of PER_CU on SIG_TYPE. That is detected later
13393 while processing PER_CU. */
13394 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13395 load_full_type_unit (sig_cu);
13396 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
13397 }
13398
13399 return 1;
13400 }
13401
13402 /* Queue all TUs contained in the DWO of PER_CU to be read in.
13403 The DWO may have the only definition of the type, though it may not be
13404 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13405 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13406
13407 static void
13408 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13409 {
13410 struct dwo_unit *dwo_unit;
13411 struct dwo_file *dwo_file;
13412
13413 gdb_assert (!per_cu->is_debug_types);
13414 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
13415 gdb_assert (per_cu->cu != NULL);
13416
13417 dwo_unit = per_cu->cu->dwo_unit;
13418 gdb_assert (dwo_unit != NULL);
13419
13420 dwo_file = dwo_unit->dwo_file;
13421 if (dwo_file->tus != NULL)
13422 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13423 }
13424
13425 /* Free all resources associated with DWO_FILE.
13426 Close the DWO file and munmap the sections. */
13427
13428 static void
13429 free_dwo_file (struct dwo_file *dwo_file)
13430 {
13431 /* Note: dbfd is NULL for virtual DWO files. */
13432 gdb_bfd_unref (dwo_file->dbfd);
13433
13434 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
13435 }
13436
13437 /* Traversal function for free_dwo_files. */
13438
13439 static int
13440 free_dwo_file_from_slot (void **slot, void *info)
13441 {
13442 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
13443
13444 free_dwo_file (dwo_file);
13445
13446 return 1;
13447 }
13448
13449 /* Free all resources associated with DWO_FILES. */
13450
13451 static void
13452 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
13453 {
13454 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
13455 }
13456 \f
13457 /* Read in various DIEs. */
13458
13459 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13460 Inherit only the children of the DW_AT_abstract_origin DIE not being
13461 already referenced by DW_AT_abstract_origin from the children of the
13462 current DIE. */
13463
13464 static void
13465 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13466 {
13467 struct die_info *child_die;
13468 sect_offset *offsetp;
13469 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13470 struct die_info *origin_die;
13471 /* Iterator of the ORIGIN_DIE children. */
13472 struct die_info *origin_child_die;
13473 struct attribute *attr;
13474 struct dwarf2_cu *origin_cu;
13475 struct pending **origin_previous_list_in_scope;
13476
13477 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13478 if (!attr)
13479 return;
13480
13481 /* Note that following die references may follow to a die in a
13482 different cu. */
13483
13484 origin_cu = cu;
13485 origin_die = follow_die_ref (die, attr, &origin_cu);
13486
13487 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13488 symbols in. */
13489 origin_previous_list_in_scope = origin_cu->list_in_scope;
13490 origin_cu->list_in_scope = cu->list_in_scope;
13491
13492 if (die->tag != origin_die->tag
13493 && !(die->tag == DW_TAG_inlined_subroutine
13494 && origin_die->tag == DW_TAG_subprogram))
13495 complaint (&symfile_complaints,
13496 _("DIE %s and its abstract origin %s have different tags"),
13497 sect_offset_str (die->sect_off),
13498 sect_offset_str (origin_die->sect_off));
13499
13500 std::vector<sect_offset> offsets;
13501
13502 for (child_die = die->child;
13503 child_die && child_die->tag;
13504 child_die = sibling_die (child_die))
13505 {
13506 struct die_info *child_origin_die;
13507 struct dwarf2_cu *child_origin_cu;
13508
13509 /* We are trying to process concrete instance entries:
13510 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13511 it's not relevant to our analysis here. i.e. detecting DIEs that are
13512 present in the abstract instance but not referenced in the concrete
13513 one. */
13514 if (child_die->tag == DW_TAG_call_site
13515 || child_die->tag == DW_TAG_GNU_call_site)
13516 continue;
13517
13518 /* For each CHILD_DIE, find the corresponding child of
13519 ORIGIN_DIE. If there is more than one layer of
13520 DW_AT_abstract_origin, follow them all; there shouldn't be,
13521 but GCC versions at least through 4.4 generate this (GCC PR
13522 40573). */
13523 child_origin_die = child_die;
13524 child_origin_cu = cu;
13525 while (1)
13526 {
13527 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13528 child_origin_cu);
13529 if (attr == NULL)
13530 break;
13531 child_origin_die = follow_die_ref (child_origin_die, attr,
13532 &child_origin_cu);
13533 }
13534
13535 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13536 counterpart may exist. */
13537 if (child_origin_die != child_die)
13538 {
13539 if (child_die->tag != child_origin_die->tag
13540 && !(child_die->tag == DW_TAG_inlined_subroutine
13541 && child_origin_die->tag == DW_TAG_subprogram))
13542 complaint (&symfile_complaints,
13543 _("Child DIE %s and its abstract origin %s have "
13544 "different tags"),
13545 sect_offset_str (child_die->sect_off),
13546 sect_offset_str (child_origin_die->sect_off));
13547 if (child_origin_die->parent != origin_die)
13548 complaint (&symfile_complaints,
13549 _("Child DIE %s and its abstract origin %s have "
13550 "different parents"),
13551 sect_offset_str (child_die->sect_off),
13552 sect_offset_str (child_origin_die->sect_off));
13553 else
13554 offsets.push_back (child_origin_die->sect_off);
13555 }
13556 }
13557 std::sort (offsets.begin (), offsets.end ());
13558 sect_offset *offsets_end = offsets.data () + offsets.size ();
13559 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13560 if (offsetp[-1] == *offsetp)
13561 complaint (&symfile_complaints,
13562 _("Multiple children of DIE %s refer "
13563 "to DIE %s as their abstract origin"),
13564 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13565
13566 offsetp = offsets.data ();
13567 origin_child_die = origin_die->child;
13568 while (origin_child_die && origin_child_die->tag)
13569 {
13570 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13571 while (offsetp < offsets_end
13572 && *offsetp < origin_child_die->sect_off)
13573 offsetp++;
13574 if (offsetp >= offsets_end
13575 || *offsetp > origin_child_die->sect_off)
13576 {
13577 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13578 Check whether we're already processing ORIGIN_CHILD_DIE.
13579 This can happen with mutually referenced abstract_origins.
13580 PR 16581. */
13581 if (!origin_child_die->in_process)
13582 process_die (origin_child_die, origin_cu);
13583 }
13584 origin_child_die = sibling_die (origin_child_die);
13585 }
13586 origin_cu->list_in_scope = origin_previous_list_in_scope;
13587 }
13588
13589 static void
13590 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13591 {
13592 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13593 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13594 struct context_stack *newobj;
13595 CORE_ADDR lowpc;
13596 CORE_ADDR highpc;
13597 struct die_info *child_die;
13598 struct attribute *attr, *call_line, *call_file;
13599 const char *name;
13600 CORE_ADDR baseaddr;
13601 struct block *block;
13602 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13603 std::vector<struct symbol *> template_args;
13604 struct template_symbol *templ_func = NULL;
13605
13606 if (inlined_func)
13607 {
13608 /* If we do not have call site information, we can't show the
13609 caller of this inlined function. That's too confusing, so
13610 only use the scope for local variables. */
13611 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13612 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13613 if (call_line == NULL || call_file == NULL)
13614 {
13615 read_lexical_block_scope (die, cu);
13616 return;
13617 }
13618 }
13619
13620 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13621
13622 name = dwarf2_name (die, cu);
13623
13624 /* Ignore functions with missing or empty names. These are actually
13625 illegal according to the DWARF standard. */
13626 if (name == NULL)
13627 {
13628 complaint (&symfile_complaints,
13629 _("missing name for subprogram DIE at %s"),
13630 sect_offset_str (die->sect_off));
13631 return;
13632 }
13633
13634 /* Ignore functions with missing or invalid low and high pc attributes. */
13635 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13636 <= PC_BOUNDS_INVALID)
13637 {
13638 attr = dwarf2_attr (die, DW_AT_external, cu);
13639 if (!attr || !DW_UNSND (attr))
13640 complaint (&symfile_complaints,
13641 _("cannot get low and high bounds "
13642 "for subprogram DIE at %s"),
13643 sect_offset_str (die->sect_off));
13644 return;
13645 }
13646
13647 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13648 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13649
13650 /* If we have any template arguments, then we must allocate a
13651 different sort of symbol. */
13652 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13653 {
13654 if (child_die->tag == DW_TAG_template_type_param
13655 || child_die->tag == DW_TAG_template_value_param)
13656 {
13657 templ_func = allocate_template_symbol (objfile);
13658 templ_func->subclass = SYMBOL_TEMPLATE;
13659 break;
13660 }
13661 }
13662
13663 newobj = push_context (0, lowpc);
13664 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13665 (struct symbol *) templ_func);
13666
13667 /* If there is a location expression for DW_AT_frame_base, record
13668 it. */
13669 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13670 if (attr)
13671 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13672
13673 /* If there is a location for the static link, record it. */
13674 newobj->static_link = NULL;
13675 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13676 if (attr)
13677 {
13678 newobj->static_link
13679 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13680 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
13681 }
13682
13683 cu->list_in_scope = &local_symbols;
13684
13685 if (die->child != NULL)
13686 {
13687 child_die = die->child;
13688 while (child_die && child_die->tag)
13689 {
13690 if (child_die->tag == DW_TAG_template_type_param
13691 || child_die->tag == DW_TAG_template_value_param)
13692 {
13693 struct symbol *arg = new_symbol (child_die, NULL, cu);
13694
13695 if (arg != NULL)
13696 template_args.push_back (arg);
13697 }
13698 else
13699 process_die (child_die, cu);
13700 child_die = sibling_die (child_die);
13701 }
13702 }
13703
13704 inherit_abstract_dies (die, cu);
13705
13706 /* If we have a DW_AT_specification, we might need to import using
13707 directives from the context of the specification DIE. See the
13708 comment in determine_prefix. */
13709 if (cu->language == language_cplus
13710 && dwarf2_attr (die, DW_AT_specification, cu))
13711 {
13712 struct dwarf2_cu *spec_cu = cu;
13713 struct die_info *spec_die = die_specification (die, &spec_cu);
13714
13715 while (spec_die)
13716 {
13717 child_die = spec_die->child;
13718 while (child_die && child_die->tag)
13719 {
13720 if (child_die->tag == DW_TAG_imported_module)
13721 process_die (child_die, spec_cu);
13722 child_die = sibling_die (child_die);
13723 }
13724
13725 /* In some cases, GCC generates specification DIEs that
13726 themselves contain DW_AT_specification attributes. */
13727 spec_die = die_specification (spec_die, &spec_cu);
13728 }
13729 }
13730
13731 newobj = pop_context ();
13732 /* Make a block for the local symbols within. */
13733 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
13734 newobj->static_link, lowpc, highpc);
13735
13736 /* For C++, set the block's scope. */
13737 if ((cu->language == language_cplus
13738 || cu->language == language_fortran
13739 || cu->language == language_d
13740 || cu->language == language_rust)
13741 && cu->processing_has_namespace_info)
13742 block_set_scope (block, determine_prefix (die, cu),
13743 &objfile->objfile_obstack);
13744
13745 /* If we have address ranges, record them. */
13746 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13747
13748 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
13749
13750 /* Attach template arguments to function. */
13751 if (!template_args.empty ())
13752 {
13753 gdb_assert (templ_func != NULL);
13754
13755 templ_func->n_template_arguments = template_args.size ();
13756 templ_func->template_arguments
13757 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13758 templ_func->n_template_arguments);
13759 memcpy (templ_func->template_arguments,
13760 template_args.data (),
13761 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13762 }
13763
13764 /* In C++, we can have functions nested inside functions (e.g., when
13765 a function declares a class that has methods). This means that
13766 when we finish processing a function scope, we may need to go
13767 back to building a containing block's symbol lists. */
13768 local_symbols = newobj->locals;
13769 local_using_directives = newobj->local_using_directives;
13770
13771 /* If we've finished processing a top-level function, subsequent
13772 symbols go in the file symbol list. */
13773 if (outermost_context_p ())
13774 cu->list_in_scope = &file_symbols;
13775 }
13776
13777 /* Process all the DIES contained within a lexical block scope. Start
13778 a new scope, process the dies, and then close the scope. */
13779
13780 static void
13781 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13782 {
13783 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13784 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13785 struct context_stack *newobj;
13786 CORE_ADDR lowpc, highpc;
13787 struct die_info *child_die;
13788 CORE_ADDR baseaddr;
13789
13790 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13791
13792 /* Ignore blocks with missing or invalid low and high pc attributes. */
13793 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13794 as multiple lexical blocks? Handling children in a sane way would
13795 be nasty. Might be easier to properly extend generic blocks to
13796 describe ranges. */
13797 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13798 {
13799 case PC_BOUNDS_NOT_PRESENT:
13800 /* DW_TAG_lexical_block has no attributes, process its children as if
13801 there was no wrapping by that DW_TAG_lexical_block.
13802 GCC does no longer produces such DWARF since GCC r224161. */
13803 for (child_die = die->child;
13804 child_die != NULL && child_die->tag;
13805 child_die = sibling_die (child_die))
13806 process_die (child_die, cu);
13807 return;
13808 case PC_BOUNDS_INVALID:
13809 return;
13810 }
13811 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13812 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13813
13814 push_context (0, lowpc);
13815 if (die->child != NULL)
13816 {
13817 child_die = die->child;
13818 while (child_die && child_die->tag)
13819 {
13820 process_die (child_die, cu);
13821 child_die = sibling_die (child_die);
13822 }
13823 }
13824 inherit_abstract_dies (die, cu);
13825 newobj = pop_context ();
13826
13827 if (local_symbols != NULL || local_using_directives != NULL)
13828 {
13829 struct block *block
13830 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
13831 newobj->start_addr, highpc);
13832
13833 /* Note that recording ranges after traversing children, as we
13834 do here, means that recording a parent's ranges entails
13835 walking across all its children's ranges as they appear in
13836 the address map, which is quadratic behavior.
13837
13838 It would be nicer to record the parent's ranges before
13839 traversing its children, simply overriding whatever you find
13840 there. But since we don't even decide whether to create a
13841 block until after we've traversed its children, that's hard
13842 to do. */
13843 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13844 }
13845 local_symbols = newobj->locals;
13846 local_using_directives = newobj->local_using_directives;
13847 }
13848
13849 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13850
13851 static void
13852 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13853 {
13854 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13855 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13856 CORE_ADDR pc, baseaddr;
13857 struct attribute *attr;
13858 struct call_site *call_site, call_site_local;
13859 void **slot;
13860 int nparams;
13861 struct die_info *child_die;
13862
13863 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13864
13865 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13866 if (attr == NULL)
13867 {
13868 /* This was a pre-DWARF-5 GNU extension alias
13869 for DW_AT_call_return_pc. */
13870 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13871 }
13872 if (!attr)
13873 {
13874 complaint (&symfile_complaints,
13875 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
13876 "DIE %s [in module %s]"),
13877 sect_offset_str (die->sect_off), objfile_name (objfile));
13878 return;
13879 }
13880 pc = attr_value_as_address (attr) + baseaddr;
13881 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13882
13883 if (cu->call_site_htab == NULL)
13884 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13885 NULL, &objfile->objfile_obstack,
13886 hashtab_obstack_allocate, NULL);
13887 call_site_local.pc = pc;
13888 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13889 if (*slot != NULL)
13890 {
13891 complaint (&symfile_complaints,
13892 _("Duplicate PC %s for DW_TAG_call_site "
13893 "DIE %s [in module %s]"),
13894 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13895 objfile_name (objfile));
13896 return;
13897 }
13898
13899 /* Count parameters at the caller. */
13900
13901 nparams = 0;
13902 for (child_die = die->child; child_die && child_die->tag;
13903 child_die = sibling_die (child_die))
13904 {
13905 if (child_die->tag != DW_TAG_call_site_parameter
13906 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13907 {
13908 complaint (&symfile_complaints,
13909 _("Tag %d is not DW_TAG_call_site_parameter in "
13910 "DW_TAG_call_site child DIE %s [in module %s]"),
13911 child_die->tag, sect_offset_str (child_die->sect_off),
13912 objfile_name (objfile));
13913 continue;
13914 }
13915
13916 nparams++;
13917 }
13918
13919 call_site
13920 = ((struct call_site *)
13921 obstack_alloc (&objfile->objfile_obstack,
13922 sizeof (*call_site)
13923 + (sizeof (*call_site->parameter) * (nparams - 1))));
13924 *slot = call_site;
13925 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13926 call_site->pc = pc;
13927
13928 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13929 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13930 {
13931 struct die_info *func_die;
13932
13933 /* Skip also over DW_TAG_inlined_subroutine. */
13934 for (func_die = die->parent;
13935 func_die && func_die->tag != DW_TAG_subprogram
13936 && func_die->tag != DW_TAG_subroutine_type;
13937 func_die = func_die->parent);
13938
13939 /* DW_AT_call_all_calls is a superset
13940 of DW_AT_call_all_tail_calls. */
13941 if (func_die
13942 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13943 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13944 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13945 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13946 {
13947 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13948 not complete. But keep CALL_SITE for look ups via call_site_htab,
13949 both the initial caller containing the real return address PC and
13950 the final callee containing the current PC of a chain of tail
13951 calls do not need to have the tail call list complete. But any
13952 function candidate for a virtual tail call frame searched via
13953 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13954 determined unambiguously. */
13955 }
13956 else
13957 {
13958 struct type *func_type = NULL;
13959
13960 if (func_die)
13961 func_type = get_die_type (func_die, cu);
13962 if (func_type != NULL)
13963 {
13964 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13965
13966 /* Enlist this call site to the function. */
13967 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13968 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13969 }
13970 else
13971 complaint (&symfile_complaints,
13972 _("Cannot find function owning DW_TAG_call_site "
13973 "DIE %s [in module %s]"),
13974 sect_offset_str (die->sect_off), objfile_name (objfile));
13975 }
13976 }
13977
13978 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13979 if (attr == NULL)
13980 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13981 if (attr == NULL)
13982 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13983 if (attr == NULL)
13984 {
13985 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13986 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13987 }
13988 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13989 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
13990 /* Keep NULL DWARF_BLOCK. */;
13991 else if (attr_form_is_block (attr))
13992 {
13993 struct dwarf2_locexpr_baton *dlbaton;
13994
13995 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13996 dlbaton->data = DW_BLOCK (attr)->data;
13997 dlbaton->size = DW_BLOCK (attr)->size;
13998 dlbaton->per_cu = cu->per_cu;
13999
14000 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14001 }
14002 else if (attr_form_is_ref (attr))
14003 {
14004 struct dwarf2_cu *target_cu = cu;
14005 struct die_info *target_die;
14006
14007 target_die = follow_die_ref (die, attr, &target_cu);
14008 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
14009 if (die_is_declaration (target_die, target_cu))
14010 {
14011 const char *target_physname;
14012
14013 /* Prefer the mangled name; otherwise compute the demangled one. */
14014 target_physname = dw2_linkage_name (target_die, target_cu);
14015 if (target_physname == NULL)
14016 target_physname = dwarf2_physname (NULL, target_die, target_cu);
14017 if (target_physname == NULL)
14018 complaint (&symfile_complaints,
14019 _("DW_AT_call_target target DIE has invalid "
14020 "physname, for referencing DIE %s [in module %s]"),
14021 sect_offset_str (die->sect_off), objfile_name (objfile));
14022 else
14023 SET_FIELD_PHYSNAME (call_site->target, target_physname);
14024 }
14025 else
14026 {
14027 CORE_ADDR lowpc;
14028
14029 /* DW_AT_entry_pc should be preferred. */
14030 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14031 <= PC_BOUNDS_INVALID)
14032 complaint (&symfile_complaints,
14033 _("DW_AT_call_target target DIE has invalid "
14034 "low pc, for referencing DIE %s [in module %s]"),
14035 sect_offset_str (die->sect_off), objfile_name (objfile));
14036 else
14037 {
14038 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14039 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14040 }
14041 }
14042 }
14043 else
14044 complaint (&symfile_complaints,
14045 _("DW_TAG_call_site DW_AT_call_target is neither "
14046 "block nor reference, for DIE %s [in module %s]"),
14047 sect_offset_str (die->sect_off), objfile_name (objfile));
14048
14049 call_site->per_cu = cu->per_cu;
14050
14051 for (child_die = die->child;
14052 child_die && child_die->tag;
14053 child_die = sibling_die (child_die))
14054 {
14055 struct call_site_parameter *parameter;
14056 struct attribute *loc, *origin;
14057
14058 if (child_die->tag != DW_TAG_call_site_parameter
14059 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14060 {
14061 /* Already printed the complaint above. */
14062 continue;
14063 }
14064
14065 gdb_assert (call_site->parameter_count < nparams);
14066 parameter = &call_site->parameter[call_site->parameter_count];
14067
14068 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14069 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14070 register is contained in DW_AT_call_value. */
14071
14072 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14073 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14074 if (origin == NULL)
14075 {
14076 /* This was a pre-DWARF-5 GNU extension alias
14077 for DW_AT_call_parameter. */
14078 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14079 }
14080 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
14081 {
14082 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14083
14084 sect_offset sect_off
14085 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14086 if (!offset_in_cu_p (&cu->header, sect_off))
14087 {
14088 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14089 binding can be done only inside one CU. Such referenced DIE
14090 therefore cannot be even moved to DW_TAG_partial_unit. */
14091 complaint (&symfile_complaints,
14092 _("DW_AT_call_parameter offset is not in CU for "
14093 "DW_TAG_call_site child DIE %s [in module %s]"),
14094 sect_offset_str (child_die->sect_off),
14095 objfile_name (objfile));
14096 continue;
14097 }
14098 parameter->u.param_cu_off
14099 = (cu_offset) (sect_off - cu->header.sect_off);
14100 }
14101 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
14102 {
14103 complaint (&symfile_complaints,
14104 _("No DW_FORM_block* DW_AT_location for "
14105 "DW_TAG_call_site child DIE %s [in module %s]"),
14106 sect_offset_str (child_die->sect_off), objfile_name (objfile));
14107 continue;
14108 }
14109 else
14110 {
14111 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14112 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14113 if (parameter->u.dwarf_reg != -1)
14114 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14115 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14116 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14117 &parameter->u.fb_offset))
14118 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14119 else
14120 {
14121 complaint (&symfile_complaints,
14122 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
14123 "for DW_FORM_block* DW_AT_location is supported for "
14124 "DW_TAG_call_site child DIE %s "
14125 "[in module %s]"),
14126 sect_offset_str (child_die->sect_off),
14127 objfile_name (objfile));
14128 continue;
14129 }
14130 }
14131
14132 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14133 if (attr == NULL)
14134 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14135 if (!attr_form_is_block (attr))
14136 {
14137 complaint (&symfile_complaints,
14138 _("No DW_FORM_block* DW_AT_call_value for "
14139 "DW_TAG_call_site child DIE %s [in module %s]"),
14140 sect_offset_str (child_die->sect_off),
14141 objfile_name (objfile));
14142 continue;
14143 }
14144 parameter->value = DW_BLOCK (attr)->data;
14145 parameter->value_size = DW_BLOCK (attr)->size;
14146
14147 /* Parameters are not pre-cleared by memset above. */
14148 parameter->data_value = NULL;
14149 parameter->data_value_size = 0;
14150 call_site->parameter_count++;
14151
14152 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14153 if (attr == NULL)
14154 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14155 if (attr)
14156 {
14157 if (!attr_form_is_block (attr))
14158 complaint (&symfile_complaints,
14159 _("No DW_FORM_block* DW_AT_call_data_value for "
14160 "DW_TAG_call_site child DIE %s [in module %s]"),
14161 sect_offset_str (child_die->sect_off),
14162 objfile_name (objfile));
14163 else
14164 {
14165 parameter->data_value = DW_BLOCK (attr)->data;
14166 parameter->data_value_size = DW_BLOCK (attr)->size;
14167 }
14168 }
14169 }
14170 }
14171
14172 /* Helper function for read_variable. If DIE represents a virtual
14173 table, then return the type of the concrete object that is
14174 associated with the virtual table. Otherwise, return NULL. */
14175
14176 static struct type *
14177 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14178 {
14179 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14180 if (attr == NULL)
14181 return NULL;
14182
14183 /* Find the type DIE. */
14184 struct die_info *type_die = NULL;
14185 struct dwarf2_cu *type_cu = cu;
14186
14187 if (attr_form_is_ref (attr))
14188 type_die = follow_die_ref (die, attr, &type_cu);
14189 if (type_die == NULL)
14190 return NULL;
14191
14192 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14193 return NULL;
14194 return die_containing_type (type_die, type_cu);
14195 }
14196
14197 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14198
14199 static void
14200 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14201 {
14202 struct rust_vtable_symbol *storage = NULL;
14203
14204 if (cu->language == language_rust)
14205 {
14206 struct type *containing_type = rust_containing_type (die, cu);
14207
14208 if (containing_type != NULL)
14209 {
14210 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14211
14212 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14213 struct rust_vtable_symbol);
14214 initialize_objfile_symbol (storage);
14215 storage->concrete_type = containing_type;
14216 storage->subclass = SYMBOL_RUST_VTABLE;
14217 }
14218 }
14219
14220 new_symbol (die, NULL, cu, storage);
14221 }
14222
14223 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14224 reading .debug_rnglists.
14225 Callback's type should be:
14226 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14227 Return true if the attributes are present and valid, otherwise,
14228 return false. */
14229
14230 template <typename Callback>
14231 static bool
14232 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14233 Callback &&callback)
14234 {
14235 struct dwarf2_per_objfile *dwarf2_per_objfile
14236 = cu->per_cu->dwarf2_per_objfile;
14237 struct objfile *objfile = dwarf2_per_objfile->objfile;
14238 bfd *obfd = objfile->obfd;
14239 /* Base address selection entry. */
14240 CORE_ADDR base;
14241 int found_base;
14242 const gdb_byte *buffer;
14243 CORE_ADDR baseaddr;
14244 bool overflow = false;
14245
14246 found_base = cu->base_known;
14247 base = cu->base_address;
14248
14249 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14250 if (offset >= dwarf2_per_objfile->rnglists.size)
14251 {
14252 complaint (&symfile_complaints,
14253 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14254 offset);
14255 return false;
14256 }
14257 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14258
14259 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14260
14261 while (1)
14262 {
14263 /* Initialize it due to a false compiler warning. */
14264 CORE_ADDR range_beginning = 0, range_end = 0;
14265 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14266 + dwarf2_per_objfile->rnglists.size);
14267 unsigned int bytes_read;
14268
14269 if (buffer == buf_end)
14270 {
14271 overflow = true;
14272 break;
14273 }
14274 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14275 switch (rlet)
14276 {
14277 case DW_RLE_end_of_list:
14278 break;
14279 case DW_RLE_base_address:
14280 if (buffer + cu->header.addr_size > buf_end)
14281 {
14282 overflow = true;
14283 break;
14284 }
14285 base = read_address (obfd, buffer, cu, &bytes_read);
14286 found_base = 1;
14287 buffer += bytes_read;
14288 break;
14289 case DW_RLE_start_length:
14290 if (buffer + cu->header.addr_size > buf_end)
14291 {
14292 overflow = true;
14293 break;
14294 }
14295 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14296 buffer += bytes_read;
14297 range_end = (range_beginning
14298 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14299 buffer += bytes_read;
14300 if (buffer > buf_end)
14301 {
14302 overflow = true;
14303 break;
14304 }
14305 break;
14306 case DW_RLE_offset_pair:
14307 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14308 buffer += bytes_read;
14309 if (buffer > buf_end)
14310 {
14311 overflow = true;
14312 break;
14313 }
14314 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14315 buffer += bytes_read;
14316 if (buffer > buf_end)
14317 {
14318 overflow = true;
14319 break;
14320 }
14321 break;
14322 case DW_RLE_start_end:
14323 if (buffer + 2 * cu->header.addr_size > buf_end)
14324 {
14325 overflow = true;
14326 break;
14327 }
14328 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14329 buffer += bytes_read;
14330 range_end = read_address (obfd, buffer, cu, &bytes_read);
14331 buffer += bytes_read;
14332 break;
14333 default:
14334 complaint (&symfile_complaints,
14335 _("Invalid .debug_rnglists data (no base address)"));
14336 return false;
14337 }
14338 if (rlet == DW_RLE_end_of_list || overflow)
14339 break;
14340 if (rlet == DW_RLE_base_address)
14341 continue;
14342
14343 if (!found_base)
14344 {
14345 /* We have no valid base address for the ranges
14346 data. */
14347 complaint (&symfile_complaints,
14348 _("Invalid .debug_rnglists data (no base address)"));
14349 return false;
14350 }
14351
14352 if (range_beginning > range_end)
14353 {
14354 /* Inverted range entries are invalid. */
14355 complaint (&symfile_complaints,
14356 _("Invalid .debug_rnglists data (inverted range)"));
14357 return false;
14358 }
14359
14360 /* Empty range entries have no effect. */
14361 if (range_beginning == range_end)
14362 continue;
14363
14364 range_beginning += base;
14365 range_end += base;
14366
14367 /* A not-uncommon case of bad debug info.
14368 Don't pollute the addrmap with bad data. */
14369 if (range_beginning + baseaddr == 0
14370 && !dwarf2_per_objfile->has_section_at_zero)
14371 {
14372 complaint (&symfile_complaints,
14373 _(".debug_rnglists entry has start address of zero"
14374 " [in module %s]"), objfile_name (objfile));
14375 continue;
14376 }
14377
14378 callback (range_beginning, range_end);
14379 }
14380
14381 if (overflow)
14382 {
14383 complaint (&symfile_complaints,
14384 _("Offset %d is not terminated "
14385 "for DW_AT_ranges attribute"),
14386 offset);
14387 return false;
14388 }
14389
14390 return true;
14391 }
14392
14393 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14394 Callback's type should be:
14395 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14396 Return 1 if the attributes are present and valid, otherwise, return 0. */
14397
14398 template <typename Callback>
14399 static int
14400 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
14401 Callback &&callback)
14402 {
14403 struct dwarf2_per_objfile *dwarf2_per_objfile
14404 = cu->per_cu->dwarf2_per_objfile;
14405 struct objfile *objfile = dwarf2_per_objfile->objfile;
14406 struct comp_unit_head *cu_header = &cu->header;
14407 bfd *obfd = objfile->obfd;
14408 unsigned int addr_size = cu_header->addr_size;
14409 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14410 /* Base address selection entry. */
14411 CORE_ADDR base;
14412 int found_base;
14413 unsigned int dummy;
14414 const gdb_byte *buffer;
14415 CORE_ADDR baseaddr;
14416
14417 if (cu_header->version >= 5)
14418 return dwarf2_rnglists_process (offset, cu, callback);
14419
14420 found_base = cu->base_known;
14421 base = cu->base_address;
14422
14423 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
14424 if (offset >= dwarf2_per_objfile->ranges.size)
14425 {
14426 complaint (&symfile_complaints,
14427 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14428 offset);
14429 return 0;
14430 }
14431 buffer = dwarf2_per_objfile->ranges.buffer + offset;
14432
14433 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14434
14435 while (1)
14436 {
14437 CORE_ADDR range_beginning, range_end;
14438
14439 range_beginning = read_address (obfd, buffer, cu, &dummy);
14440 buffer += addr_size;
14441 range_end = read_address (obfd, buffer, cu, &dummy);
14442 buffer += addr_size;
14443 offset += 2 * addr_size;
14444
14445 /* An end of list marker is a pair of zero addresses. */
14446 if (range_beginning == 0 && range_end == 0)
14447 /* Found the end of list entry. */
14448 break;
14449
14450 /* Each base address selection entry is a pair of 2 values.
14451 The first is the largest possible address, the second is
14452 the base address. Check for a base address here. */
14453 if ((range_beginning & mask) == mask)
14454 {
14455 /* If we found the largest possible address, then we already
14456 have the base address in range_end. */
14457 base = range_end;
14458 found_base = 1;
14459 continue;
14460 }
14461
14462 if (!found_base)
14463 {
14464 /* We have no valid base address for the ranges
14465 data. */
14466 complaint (&symfile_complaints,
14467 _("Invalid .debug_ranges data (no base address)"));
14468 return 0;
14469 }
14470
14471 if (range_beginning > range_end)
14472 {
14473 /* Inverted range entries are invalid. */
14474 complaint (&symfile_complaints,
14475 _("Invalid .debug_ranges data (inverted range)"));
14476 return 0;
14477 }
14478
14479 /* Empty range entries have no effect. */
14480 if (range_beginning == range_end)
14481 continue;
14482
14483 range_beginning += base;
14484 range_end += base;
14485
14486 /* A not-uncommon case of bad debug info.
14487 Don't pollute the addrmap with bad data. */
14488 if (range_beginning + baseaddr == 0
14489 && !dwarf2_per_objfile->has_section_at_zero)
14490 {
14491 complaint (&symfile_complaints,
14492 _(".debug_ranges entry has start address of zero"
14493 " [in module %s]"), objfile_name (objfile));
14494 continue;
14495 }
14496
14497 callback (range_beginning, range_end);
14498 }
14499
14500 return 1;
14501 }
14502
14503 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14504 Return 1 if the attributes are present and valid, otherwise, return 0.
14505 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14506
14507 static int
14508 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14509 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14510 struct partial_symtab *ranges_pst)
14511 {
14512 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14513 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14514 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14515 SECT_OFF_TEXT (objfile));
14516 int low_set = 0;
14517 CORE_ADDR low = 0;
14518 CORE_ADDR high = 0;
14519 int retval;
14520
14521 retval = dwarf2_ranges_process (offset, cu,
14522 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14523 {
14524 if (ranges_pst != NULL)
14525 {
14526 CORE_ADDR lowpc;
14527 CORE_ADDR highpc;
14528
14529 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14530 range_beginning + baseaddr);
14531 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14532 range_end + baseaddr);
14533 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
14534 ranges_pst);
14535 }
14536
14537 /* FIXME: This is recording everything as a low-high
14538 segment of consecutive addresses. We should have a
14539 data structure for discontiguous block ranges
14540 instead. */
14541 if (! low_set)
14542 {
14543 low = range_beginning;
14544 high = range_end;
14545 low_set = 1;
14546 }
14547 else
14548 {
14549 if (range_beginning < low)
14550 low = range_beginning;
14551 if (range_end > high)
14552 high = range_end;
14553 }
14554 });
14555 if (!retval)
14556 return 0;
14557
14558 if (! low_set)
14559 /* If the first entry is an end-of-list marker, the range
14560 describes an empty scope, i.e. no instructions. */
14561 return 0;
14562
14563 if (low_return)
14564 *low_return = low;
14565 if (high_return)
14566 *high_return = high;
14567 return 1;
14568 }
14569
14570 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14571 definition for the return value. *LOWPC and *HIGHPC are set iff
14572 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14573
14574 static enum pc_bounds_kind
14575 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14576 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14577 struct partial_symtab *pst)
14578 {
14579 struct dwarf2_per_objfile *dwarf2_per_objfile
14580 = cu->per_cu->dwarf2_per_objfile;
14581 struct attribute *attr;
14582 struct attribute *attr_high;
14583 CORE_ADDR low = 0;
14584 CORE_ADDR high = 0;
14585 enum pc_bounds_kind ret;
14586
14587 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14588 if (attr_high)
14589 {
14590 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14591 if (attr)
14592 {
14593 low = attr_value_as_address (attr);
14594 high = attr_value_as_address (attr_high);
14595 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14596 high += low;
14597 }
14598 else
14599 /* Found high w/o low attribute. */
14600 return PC_BOUNDS_INVALID;
14601
14602 /* Found consecutive range of addresses. */
14603 ret = PC_BOUNDS_HIGH_LOW;
14604 }
14605 else
14606 {
14607 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14608 if (attr != NULL)
14609 {
14610 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14611 We take advantage of the fact that DW_AT_ranges does not appear
14612 in DW_TAG_compile_unit of DWO files. */
14613 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14614 unsigned int ranges_offset = (DW_UNSND (attr)
14615 + (need_ranges_base
14616 ? cu->ranges_base
14617 : 0));
14618
14619 /* Value of the DW_AT_ranges attribute is the offset in the
14620 .debug_ranges section. */
14621 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14622 return PC_BOUNDS_INVALID;
14623 /* Found discontinuous range of addresses. */
14624 ret = PC_BOUNDS_RANGES;
14625 }
14626 else
14627 return PC_BOUNDS_NOT_PRESENT;
14628 }
14629
14630 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14631 if (high <= low)
14632 return PC_BOUNDS_INVALID;
14633
14634 /* When using the GNU linker, .gnu.linkonce. sections are used to
14635 eliminate duplicate copies of functions and vtables and such.
14636 The linker will arbitrarily choose one and discard the others.
14637 The AT_*_pc values for such functions refer to local labels in
14638 these sections. If the section from that file was discarded, the
14639 labels are not in the output, so the relocs get a value of 0.
14640 If this is a discarded function, mark the pc bounds as invalid,
14641 so that GDB will ignore it. */
14642 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
14643 return PC_BOUNDS_INVALID;
14644
14645 *lowpc = low;
14646 if (highpc)
14647 *highpc = high;
14648 return ret;
14649 }
14650
14651 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14652 its low and high PC addresses. Do nothing if these addresses could not
14653 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14654 and HIGHPC to the high address if greater than HIGHPC. */
14655
14656 static void
14657 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14658 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14659 struct dwarf2_cu *cu)
14660 {
14661 CORE_ADDR low, high;
14662 struct die_info *child = die->child;
14663
14664 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14665 {
14666 *lowpc = std::min (*lowpc, low);
14667 *highpc = std::max (*highpc, high);
14668 }
14669
14670 /* If the language does not allow nested subprograms (either inside
14671 subprograms or lexical blocks), we're done. */
14672 if (cu->language != language_ada)
14673 return;
14674
14675 /* Check all the children of the given DIE. If it contains nested
14676 subprograms, then check their pc bounds. Likewise, we need to
14677 check lexical blocks as well, as they may also contain subprogram
14678 definitions. */
14679 while (child && child->tag)
14680 {
14681 if (child->tag == DW_TAG_subprogram
14682 || child->tag == DW_TAG_lexical_block)
14683 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14684 child = sibling_die (child);
14685 }
14686 }
14687
14688 /* Get the low and high pc's represented by the scope DIE, and store
14689 them in *LOWPC and *HIGHPC. If the correct values can't be
14690 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14691
14692 static void
14693 get_scope_pc_bounds (struct die_info *die,
14694 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14695 struct dwarf2_cu *cu)
14696 {
14697 CORE_ADDR best_low = (CORE_ADDR) -1;
14698 CORE_ADDR best_high = (CORE_ADDR) 0;
14699 CORE_ADDR current_low, current_high;
14700
14701 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14702 >= PC_BOUNDS_RANGES)
14703 {
14704 best_low = current_low;
14705 best_high = current_high;
14706 }
14707 else
14708 {
14709 struct die_info *child = die->child;
14710
14711 while (child && child->tag)
14712 {
14713 switch (child->tag) {
14714 case DW_TAG_subprogram:
14715 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14716 break;
14717 case DW_TAG_namespace:
14718 case DW_TAG_module:
14719 /* FIXME: carlton/2004-01-16: Should we do this for
14720 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14721 that current GCC's always emit the DIEs corresponding
14722 to definitions of methods of classes as children of a
14723 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14724 the DIEs giving the declarations, which could be
14725 anywhere). But I don't see any reason why the
14726 standards says that they have to be there. */
14727 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14728
14729 if (current_low != ((CORE_ADDR) -1))
14730 {
14731 best_low = std::min (best_low, current_low);
14732 best_high = std::max (best_high, current_high);
14733 }
14734 break;
14735 default:
14736 /* Ignore. */
14737 break;
14738 }
14739
14740 child = sibling_die (child);
14741 }
14742 }
14743
14744 *lowpc = best_low;
14745 *highpc = best_high;
14746 }
14747
14748 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14749 in DIE. */
14750
14751 static void
14752 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14753 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14754 {
14755 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14756 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14757 struct attribute *attr;
14758 struct attribute *attr_high;
14759
14760 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14761 if (attr_high)
14762 {
14763 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14764 if (attr)
14765 {
14766 CORE_ADDR low = attr_value_as_address (attr);
14767 CORE_ADDR high = attr_value_as_address (attr_high);
14768
14769 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14770 high += low;
14771
14772 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14773 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14774 record_block_range (block, low, high - 1);
14775 }
14776 }
14777
14778 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14779 if (attr)
14780 {
14781 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14782 We take advantage of the fact that DW_AT_ranges does not appear
14783 in DW_TAG_compile_unit of DWO files. */
14784 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14785
14786 /* The value of the DW_AT_ranges attribute is the offset of the
14787 address range list in the .debug_ranges section. */
14788 unsigned long offset = (DW_UNSND (attr)
14789 + (need_ranges_base ? cu->ranges_base : 0));
14790
14791 dwarf2_ranges_process (offset, cu,
14792 [&] (CORE_ADDR start, CORE_ADDR end)
14793 {
14794 start += baseaddr;
14795 end += baseaddr;
14796 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14797 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14798 record_block_range (block, start, end - 1);
14799 });
14800 }
14801 }
14802
14803 /* Check whether the producer field indicates either of GCC < 4.6, or the
14804 Intel C/C++ compiler, and cache the result in CU. */
14805
14806 static void
14807 check_producer (struct dwarf2_cu *cu)
14808 {
14809 int major, minor;
14810
14811 if (cu->producer == NULL)
14812 {
14813 /* For unknown compilers expect their behavior is DWARF version
14814 compliant.
14815
14816 GCC started to support .debug_types sections by -gdwarf-4 since
14817 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14818 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14819 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14820 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14821 }
14822 else if (producer_is_gcc (cu->producer, &major, &minor))
14823 {
14824 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14825 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14826 }
14827 else if (producer_is_icc (cu->producer, &major, &minor))
14828 cu->producer_is_icc_lt_14 = major < 14;
14829 else
14830 {
14831 /* For other non-GCC compilers, expect their behavior is DWARF version
14832 compliant. */
14833 }
14834
14835 cu->checked_producer = 1;
14836 }
14837
14838 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14839 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14840 during 4.6.0 experimental. */
14841
14842 static int
14843 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14844 {
14845 if (!cu->checked_producer)
14846 check_producer (cu);
14847
14848 return cu->producer_is_gxx_lt_4_6;
14849 }
14850
14851 /* Return the default accessibility type if it is not overriden by
14852 DW_AT_accessibility. */
14853
14854 static enum dwarf_access_attribute
14855 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14856 {
14857 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14858 {
14859 /* The default DWARF 2 accessibility for members is public, the default
14860 accessibility for inheritance is private. */
14861
14862 if (die->tag != DW_TAG_inheritance)
14863 return DW_ACCESS_public;
14864 else
14865 return DW_ACCESS_private;
14866 }
14867 else
14868 {
14869 /* DWARF 3+ defines the default accessibility a different way. The same
14870 rules apply now for DW_TAG_inheritance as for the members and it only
14871 depends on the container kind. */
14872
14873 if (die->parent->tag == DW_TAG_class_type)
14874 return DW_ACCESS_private;
14875 else
14876 return DW_ACCESS_public;
14877 }
14878 }
14879
14880 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14881 offset. If the attribute was not found return 0, otherwise return
14882 1. If it was found but could not properly be handled, set *OFFSET
14883 to 0. */
14884
14885 static int
14886 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14887 LONGEST *offset)
14888 {
14889 struct attribute *attr;
14890
14891 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14892 if (attr != NULL)
14893 {
14894 *offset = 0;
14895
14896 /* Note that we do not check for a section offset first here.
14897 This is because DW_AT_data_member_location is new in DWARF 4,
14898 so if we see it, we can assume that a constant form is really
14899 a constant and not a section offset. */
14900 if (attr_form_is_constant (attr))
14901 *offset = dwarf2_get_attr_constant_value (attr, 0);
14902 else if (attr_form_is_section_offset (attr))
14903 dwarf2_complex_location_expr_complaint ();
14904 else if (attr_form_is_block (attr))
14905 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14906 else
14907 dwarf2_complex_location_expr_complaint ();
14908
14909 return 1;
14910 }
14911
14912 return 0;
14913 }
14914
14915 /* Add an aggregate field to the field list. */
14916
14917 static void
14918 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14919 struct dwarf2_cu *cu)
14920 {
14921 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14922 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14923 struct nextfield *new_field;
14924 struct attribute *attr;
14925 struct field *fp;
14926 const char *fieldname = "";
14927
14928 if (die->tag == DW_TAG_inheritance)
14929 {
14930 fip->baseclasses.emplace_back ();
14931 new_field = &fip->baseclasses.back ();
14932 }
14933 else
14934 {
14935 fip->fields.emplace_back ();
14936 new_field = &fip->fields.back ();
14937 }
14938
14939 fip->nfields++;
14940
14941 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14942 if (attr)
14943 new_field->accessibility = DW_UNSND (attr);
14944 else
14945 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14946 if (new_field->accessibility != DW_ACCESS_public)
14947 fip->non_public_fields = 1;
14948
14949 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14950 if (attr)
14951 new_field->virtuality = DW_UNSND (attr);
14952 else
14953 new_field->virtuality = DW_VIRTUALITY_none;
14954
14955 fp = &new_field->field;
14956
14957 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14958 {
14959 LONGEST offset;
14960
14961 /* Data member other than a C++ static data member. */
14962
14963 /* Get type of field. */
14964 fp->type = die_type (die, cu);
14965
14966 SET_FIELD_BITPOS (*fp, 0);
14967
14968 /* Get bit size of field (zero if none). */
14969 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14970 if (attr)
14971 {
14972 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14973 }
14974 else
14975 {
14976 FIELD_BITSIZE (*fp) = 0;
14977 }
14978
14979 /* Get bit offset of field. */
14980 if (handle_data_member_location (die, cu, &offset))
14981 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14982 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14983 if (attr)
14984 {
14985 if (gdbarch_bits_big_endian (gdbarch))
14986 {
14987 /* For big endian bits, the DW_AT_bit_offset gives the
14988 additional bit offset from the MSB of the containing
14989 anonymous object to the MSB of the field. We don't
14990 have to do anything special since we don't need to
14991 know the size of the anonymous object. */
14992 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14993 }
14994 else
14995 {
14996 /* For little endian bits, compute the bit offset to the
14997 MSB of the anonymous object, subtract off the number of
14998 bits from the MSB of the field to the MSB of the
14999 object, and then subtract off the number of bits of
15000 the field itself. The result is the bit offset of
15001 the LSB of the field. */
15002 int anonymous_size;
15003 int bit_offset = DW_UNSND (attr);
15004
15005 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15006 if (attr)
15007 {
15008 /* The size of the anonymous object containing
15009 the bit field is explicit, so use the
15010 indicated size (in bytes). */
15011 anonymous_size = DW_UNSND (attr);
15012 }
15013 else
15014 {
15015 /* The size of the anonymous object containing
15016 the bit field must be inferred from the type
15017 attribute of the data member containing the
15018 bit field. */
15019 anonymous_size = TYPE_LENGTH (fp->type);
15020 }
15021 SET_FIELD_BITPOS (*fp,
15022 (FIELD_BITPOS (*fp)
15023 + anonymous_size * bits_per_byte
15024 - bit_offset - FIELD_BITSIZE (*fp)));
15025 }
15026 }
15027 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15028 if (attr != NULL)
15029 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15030 + dwarf2_get_attr_constant_value (attr, 0)));
15031
15032 /* Get name of field. */
15033 fieldname = dwarf2_name (die, cu);
15034 if (fieldname == NULL)
15035 fieldname = "";
15036
15037 /* The name is already allocated along with this objfile, so we don't
15038 need to duplicate it for the type. */
15039 fp->name = fieldname;
15040
15041 /* Change accessibility for artificial fields (e.g. virtual table
15042 pointer or virtual base class pointer) to private. */
15043 if (dwarf2_attr (die, DW_AT_artificial, cu))
15044 {
15045 FIELD_ARTIFICIAL (*fp) = 1;
15046 new_field->accessibility = DW_ACCESS_private;
15047 fip->non_public_fields = 1;
15048 }
15049 }
15050 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15051 {
15052 /* C++ static member. */
15053
15054 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15055 is a declaration, but all versions of G++ as of this writing
15056 (so through at least 3.2.1) incorrectly generate
15057 DW_TAG_variable tags. */
15058
15059 const char *physname;
15060
15061 /* Get name of field. */
15062 fieldname = dwarf2_name (die, cu);
15063 if (fieldname == NULL)
15064 return;
15065
15066 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15067 if (attr
15068 /* Only create a symbol if this is an external value.
15069 new_symbol checks this and puts the value in the global symbol
15070 table, which we want. If it is not external, new_symbol
15071 will try to put the value in cu->list_in_scope which is wrong. */
15072 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15073 {
15074 /* A static const member, not much different than an enum as far as
15075 we're concerned, except that we can support more types. */
15076 new_symbol (die, NULL, cu);
15077 }
15078
15079 /* Get physical name. */
15080 physname = dwarf2_physname (fieldname, die, cu);
15081
15082 /* The name is already allocated along with this objfile, so we don't
15083 need to duplicate it for the type. */
15084 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15085 FIELD_TYPE (*fp) = die_type (die, cu);
15086 FIELD_NAME (*fp) = fieldname;
15087 }
15088 else if (die->tag == DW_TAG_inheritance)
15089 {
15090 LONGEST offset;
15091
15092 /* C++ base class field. */
15093 if (handle_data_member_location (die, cu, &offset))
15094 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15095 FIELD_BITSIZE (*fp) = 0;
15096 FIELD_TYPE (*fp) = die_type (die, cu);
15097 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
15098 }
15099 else if (die->tag == DW_TAG_variant_part)
15100 {
15101 /* process_structure_scope will treat this DIE as a union. */
15102 process_structure_scope (die, cu);
15103
15104 /* The variant part is relative to the start of the enclosing
15105 structure. */
15106 SET_FIELD_BITPOS (*fp, 0);
15107 fp->type = get_die_type (die, cu);
15108 fp->artificial = 1;
15109 fp->name = "<<variant>>";
15110 }
15111 else
15112 gdb_assert_not_reached ("missing case in dwarf2_add_field");
15113 }
15114
15115 /* Can the type given by DIE define another type? */
15116
15117 static bool
15118 type_can_define_types (const struct die_info *die)
15119 {
15120 switch (die->tag)
15121 {
15122 case DW_TAG_typedef:
15123 case DW_TAG_class_type:
15124 case DW_TAG_structure_type:
15125 case DW_TAG_union_type:
15126 case DW_TAG_enumeration_type:
15127 return true;
15128
15129 default:
15130 return false;
15131 }
15132 }
15133
15134 /* Add a type definition defined in the scope of the FIP's class. */
15135
15136 static void
15137 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15138 struct dwarf2_cu *cu)
15139 {
15140 struct decl_field fp;
15141 memset (&fp, 0, sizeof (fp));
15142
15143 gdb_assert (type_can_define_types (die));
15144
15145 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15146 fp.name = dwarf2_name (die, cu);
15147 fp.type = read_type_die (die, cu);
15148
15149 /* Save accessibility. */
15150 enum dwarf_access_attribute accessibility;
15151 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15152 if (attr != NULL)
15153 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15154 else
15155 accessibility = dwarf2_default_access_attribute (die, cu);
15156 switch (accessibility)
15157 {
15158 case DW_ACCESS_public:
15159 /* The assumed value if neither private nor protected. */
15160 break;
15161 case DW_ACCESS_private:
15162 fp.is_private = 1;
15163 break;
15164 case DW_ACCESS_protected:
15165 fp.is_protected = 1;
15166 break;
15167 default:
15168 complaint (&symfile_complaints,
15169 _("Unhandled DW_AT_accessibility value (%x)"), accessibility);
15170 }
15171
15172 if (die->tag == DW_TAG_typedef)
15173 fip->typedef_field_list.push_back (fp);
15174 else
15175 fip->nested_types_list.push_back (fp);
15176 }
15177
15178 /* Create the vector of fields, and attach it to the type. */
15179
15180 static void
15181 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15182 struct dwarf2_cu *cu)
15183 {
15184 int nfields = fip->nfields;
15185
15186 /* Record the field count, allocate space for the array of fields,
15187 and create blank accessibility bitfields if necessary. */
15188 TYPE_NFIELDS (type) = nfields;
15189 TYPE_FIELDS (type) = (struct field *)
15190 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
15191
15192 if (fip->non_public_fields && cu->language != language_ada)
15193 {
15194 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15195
15196 TYPE_FIELD_PRIVATE_BITS (type) =
15197 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15198 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15199
15200 TYPE_FIELD_PROTECTED_BITS (type) =
15201 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15202 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15203
15204 TYPE_FIELD_IGNORE_BITS (type) =
15205 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15206 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15207 }
15208
15209 /* If the type has baseclasses, allocate and clear a bit vector for
15210 TYPE_FIELD_VIRTUAL_BITS. */
15211 if (!fip->baseclasses.empty () && cu->language != language_ada)
15212 {
15213 int num_bytes = B_BYTES (fip->baseclasses.size ());
15214 unsigned char *pointer;
15215
15216 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15217 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15218 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15219 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15220 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15221 }
15222
15223 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15224 {
15225 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15226
15227 for (int index = 0; index < nfields; ++index)
15228 {
15229 struct nextfield &field = fip->fields[index];
15230
15231 if (field.variant.is_discriminant)
15232 di->discriminant_index = index;
15233 else if (field.variant.default_branch)
15234 di->default_index = index;
15235 else
15236 di->discriminants[index] = field.variant.discriminant_value;
15237 }
15238 }
15239
15240 /* Copy the saved-up fields into the field vector. */
15241 for (int i = 0; i < nfields; ++i)
15242 {
15243 struct nextfield &field
15244 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15245 : fip->fields[i - fip->baseclasses.size ()]);
15246
15247 TYPE_FIELD (type, i) = field.field;
15248 switch (field.accessibility)
15249 {
15250 case DW_ACCESS_private:
15251 if (cu->language != language_ada)
15252 SET_TYPE_FIELD_PRIVATE (type, i);
15253 break;
15254
15255 case DW_ACCESS_protected:
15256 if (cu->language != language_ada)
15257 SET_TYPE_FIELD_PROTECTED (type, i);
15258 break;
15259
15260 case DW_ACCESS_public:
15261 break;
15262
15263 default:
15264 /* Unknown accessibility. Complain and treat it as public. */
15265 {
15266 complaint (&symfile_complaints, _("unsupported accessibility %d"),
15267 field.accessibility);
15268 }
15269 break;
15270 }
15271 if (i < fip->baseclasses.size ())
15272 {
15273 switch (field.virtuality)
15274 {
15275 case DW_VIRTUALITY_virtual:
15276 case DW_VIRTUALITY_pure_virtual:
15277 if (cu->language == language_ada)
15278 error (_("unexpected virtuality in component of Ada type"));
15279 SET_TYPE_FIELD_VIRTUAL (type, i);
15280 break;
15281 }
15282 }
15283 }
15284 }
15285
15286 /* Return true if this member function is a constructor, false
15287 otherwise. */
15288
15289 static int
15290 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15291 {
15292 const char *fieldname;
15293 const char *type_name;
15294 int len;
15295
15296 if (die->parent == NULL)
15297 return 0;
15298
15299 if (die->parent->tag != DW_TAG_structure_type
15300 && die->parent->tag != DW_TAG_union_type
15301 && die->parent->tag != DW_TAG_class_type)
15302 return 0;
15303
15304 fieldname = dwarf2_name (die, cu);
15305 type_name = dwarf2_name (die->parent, cu);
15306 if (fieldname == NULL || type_name == NULL)
15307 return 0;
15308
15309 len = strlen (fieldname);
15310 return (strncmp (fieldname, type_name, len) == 0
15311 && (type_name[len] == '\0' || type_name[len] == '<'));
15312 }
15313
15314 /* Add a member function to the proper fieldlist. */
15315
15316 static void
15317 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15318 struct type *type, struct dwarf2_cu *cu)
15319 {
15320 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15321 struct attribute *attr;
15322 int i;
15323 struct fnfieldlist *flp = nullptr;
15324 struct fn_field *fnp;
15325 const char *fieldname;
15326 struct type *this_type;
15327 enum dwarf_access_attribute accessibility;
15328
15329 if (cu->language == language_ada)
15330 error (_("unexpected member function in Ada type"));
15331
15332 /* Get name of member function. */
15333 fieldname = dwarf2_name (die, cu);
15334 if (fieldname == NULL)
15335 return;
15336
15337 /* Look up member function name in fieldlist. */
15338 for (i = 0; i < fip->fnfieldlists.size (); i++)
15339 {
15340 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15341 {
15342 flp = &fip->fnfieldlists[i];
15343 break;
15344 }
15345 }
15346
15347 /* Create a new fnfieldlist if necessary. */
15348 if (flp == nullptr)
15349 {
15350 fip->fnfieldlists.emplace_back ();
15351 flp = &fip->fnfieldlists.back ();
15352 flp->name = fieldname;
15353 i = fip->fnfieldlists.size () - 1;
15354 }
15355
15356 /* Create a new member function field and add it to the vector of
15357 fnfieldlists. */
15358 flp->fnfields.emplace_back ();
15359 fnp = &flp->fnfields.back ();
15360
15361 /* Delay processing of the physname until later. */
15362 if (cu->language == language_cplus)
15363 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15364 die, cu);
15365 else
15366 {
15367 const char *physname = dwarf2_physname (fieldname, die, cu);
15368 fnp->physname = physname ? physname : "";
15369 }
15370
15371 fnp->type = alloc_type (objfile);
15372 this_type = read_type_die (die, cu);
15373 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
15374 {
15375 int nparams = TYPE_NFIELDS (this_type);
15376
15377 /* TYPE is the domain of this method, and THIS_TYPE is the type
15378 of the method itself (TYPE_CODE_METHOD). */
15379 smash_to_method_type (fnp->type, type,
15380 TYPE_TARGET_TYPE (this_type),
15381 TYPE_FIELDS (this_type),
15382 TYPE_NFIELDS (this_type),
15383 TYPE_VARARGS (this_type));
15384
15385 /* Handle static member functions.
15386 Dwarf2 has no clean way to discern C++ static and non-static
15387 member functions. G++ helps GDB by marking the first
15388 parameter for non-static member functions (which is the this
15389 pointer) as artificial. We obtain this information from
15390 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15391 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15392 fnp->voffset = VOFFSET_STATIC;
15393 }
15394 else
15395 complaint (&symfile_complaints, _("member function type missing for '%s'"),
15396 dwarf2_full_name (fieldname, die, cu));
15397
15398 /* Get fcontext from DW_AT_containing_type if present. */
15399 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15400 fnp->fcontext = die_containing_type (die, cu);
15401
15402 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15403 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15404
15405 /* Get accessibility. */
15406 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15407 if (attr)
15408 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15409 else
15410 accessibility = dwarf2_default_access_attribute (die, cu);
15411 switch (accessibility)
15412 {
15413 case DW_ACCESS_private:
15414 fnp->is_private = 1;
15415 break;
15416 case DW_ACCESS_protected:
15417 fnp->is_protected = 1;
15418 break;
15419 }
15420
15421 /* Check for artificial methods. */
15422 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15423 if (attr && DW_UNSND (attr) != 0)
15424 fnp->is_artificial = 1;
15425
15426 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15427
15428 /* Get index in virtual function table if it is a virtual member
15429 function. For older versions of GCC, this is an offset in the
15430 appropriate virtual table, as specified by DW_AT_containing_type.
15431 For everyone else, it is an expression to be evaluated relative
15432 to the object address. */
15433
15434 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15435 if (attr)
15436 {
15437 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
15438 {
15439 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15440 {
15441 /* Old-style GCC. */
15442 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15443 }
15444 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15445 || (DW_BLOCK (attr)->size > 1
15446 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15447 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15448 {
15449 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15450 if ((fnp->voffset % cu->header.addr_size) != 0)
15451 dwarf2_complex_location_expr_complaint ();
15452 else
15453 fnp->voffset /= cu->header.addr_size;
15454 fnp->voffset += 2;
15455 }
15456 else
15457 dwarf2_complex_location_expr_complaint ();
15458
15459 if (!fnp->fcontext)
15460 {
15461 /* If there is no `this' field and no DW_AT_containing_type,
15462 we cannot actually find a base class context for the
15463 vtable! */
15464 if (TYPE_NFIELDS (this_type) == 0
15465 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15466 {
15467 complaint (&symfile_complaints,
15468 _("cannot determine context for virtual member "
15469 "function \"%s\" (offset %s)"),
15470 fieldname, sect_offset_str (die->sect_off));
15471 }
15472 else
15473 {
15474 fnp->fcontext
15475 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15476 }
15477 }
15478 }
15479 else if (attr_form_is_section_offset (attr))
15480 {
15481 dwarf2_complex_location_expr_complaint ();
15482 }
15483 else
15484 {
15485 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15486 fieldname);
15487 }
15488 }
15489 else
15490 {
15491 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15492 if (attr && DW_UNSND (attr))
15493 {
15494 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15495 complaint (&symfile_complaints,
15496 _("Member function \"%s\" (offset %s) is virtual "
15497 "but the vtable offset is not specified"),
15498 fieldname, sect_offset_str (die->sect_off));
15499 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15500 TYPE_CPLUS_DYNAMIC (type) = 1;
15501 }
15502 }
15503 }
15504
15505 /* Create the vector of member function fields, and attach it to the type. */
15506
15507 static void
15508 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15509 struct dwarf2_cu *cu)
15510 {
15511 if (cu->language == language_ada)
15512 error (_("unexpected member functions in Ada type"));
15513
15514 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15515 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15516 TYPE_ALLOC (type,
15517 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15518
15519 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15520 {
15521 struct fnfieldlist &nf = fip->fnfieldlists[i];
15522 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15523
15524 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15525 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15526 fn_flp->fn_fields = (struct fn_field *)
15527 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15528
15529 for (int k = 0; k < nf.fnfields.size (); ++k)
15530 fn_flp->fn_fields[k] = nf.fnfields[k];
15531 }
15532
15533 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15534 }
15535
15536 /* Returns non-zero if NAME is the name of a vtable member in CU's
15537 language, zero otherwise. */
15538 static int
15539 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15540 {
15541 static const char vptr[] = "_vptr";
15542
15543 /* Look for the C++ form of the vtable. */
15544 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15545 return 1;
15546
15547 return 0;
15548 }
15549
15550 /* GCC outputs unnamed structures that are really pointers to member
15551 functions, with the ABI-specified layout. If TYPE describes
15552 such a structure, smash it into a member function type.
15553
15554 GCC shouldn't do this; it should just output pointer to member DIEs.
15555 This is GCC PR debug/28767. */
15556
15557 static void
15558 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15559 {
15560 struct type *pfn_type, *self_type, *new_type;
15561
15562 /* Check for a structure with no name and two children. */
15563 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15564 return;
15565
15566 /* Check for __pfn and __delta members. */
15567 if (TYPE_FIELD_NAME (type, 0) == NULL
15568 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15569 || TYPE_FIELD_NAME (type, 1) == NULL
15570 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15571 return;
15572
15573 /* Find the type of the method. */
15574 pfn_type = TYPE_FIELD_TYPE (type, 0);
15575 if (pfn_type == NULL
15576 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15577 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
15578 return;
15579
15580 /* Look for the "this" argument. */
15581 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15582 if (TYPE_NFIELDS (pfn_type) == 0
15583 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15584 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
15585 return;
15586
15587 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15588 new_type = alloc_type (objfile);
15589 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15590 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15591 TYPE_VARARGS (pfn_type));
15592 smash_to_methodptr_type (type, new_type);
15593 }
15594
15595 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15596 appropriate error checking and issuing complaints if there is a
15597 problem. */
15598
15599 static ULONGEST
15600 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15601 {
15602 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15603
15604 if (attr == nullptr)
15605 return 0;
15606
15607 if (!attr_form_is_constant (attr))
15608 {
15609 complaint (&symfile_complaints,
15610 _("DW_AT_alignment must have constant form"
15611 " - DIE at %s [in module %s]"),
15612 sect_offset_str (die->sect_off),
15613 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15614 return 0;
15615 }
15616
15617 ULONGEST align;
15618 if (attr->form == DW_FORM_sdata)
15619 {
15620 LONGEST val = DW_SND (attr);
15621 if (val < 0)
15622 {
15623 complaint (&symfile_complaints,
15624 _("DW_AT_alignment value must not be negative"
15625 " - DIE at %s [in module %s]"),
15626 sect_offset_str (die->sect_off),
15627 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15628 return 0;
15629 }
15630 align = val;
15631 }
15632 else
15633 align = DW_UNSND (attr);
15634
15635 if (align == 0)
15636 {
15637 complaint (&symfile_complaints,
15638 _("DW_AT_alignment value must not be zero"
15639 " - DIE at %s [in module %s]"),
15640 sect_offset_str (die->sect_off),
15641 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15642 return 0;
15643 }
15644 if ((align & (align - 1)) != 0)
15645 {
15646 complaint (&symfile_complaints,
15647 _("DW_AT_alignment value must be a power of 2"
15648 " - DIE at %s [in module %s]"),
15649 sect_offset_str (die->sect_off),
15650 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15651 return 0;
15652 }
15653
15654 return align;
15655 }
15656
15657 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15658 the alignment for TYPE. */
15659
15660 static void
15661 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15662 struct type *type)
15663 {
15664 if (!set_type_align (type, get_alignment (cu, die)))
15665 complaint (&symfile_complaints,
15666 _("DW_AT_alignment value too large"
15667 " - DIE at %s [in module %s]"),
15668 sect_offset_str (die->sect_off),
15669 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15670 }
15671
15672 /* Called when we find the DIE that starts a structure or union scope
15673 (definition) to create a type for the structure or union. Fill in
15674 the type's name and general properties; the members will not be
15675 processed until process_structure_scope. A symbol table entry for
15676 the type will also not be done until process_structure_scope (assuming
15677 the type has a name).
15678
15679 NOTE: we need to call these functions regardless of whether or not the
15680 DIE has a DW_AT_name attribute, since it might be an anonymous
15681 structure or union. This gets the type entered into our set of
15682 user defined types. */
15683
15684 static struct type *
15685 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15686 {
15687 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15688 struct type *type;
15689 struct attribute *attr;
15690 const char *name;
15691
15692 /* If the definition of this type lives in .debug_types, read that type.
15693 Don't follow DW_AT_specification though, that will take us back up
15694 the chain and we want to go down. */
15695 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15696 if (attr)
15697 {
15698 type = get_DW_AT_signature_type (die, attr, cu);
15699
15700 /* The type's CU may not be the same as CU.
15701 Ensure TYPE is recorded with CU in die_type_hash. */
15702 return set_die_type (die, type, cu);
15703 }
15704
15705 type = alloc_type (objfile);
15706 INIT_CPLUS_SPECIFIC (type);
15707
15708 name = dwarf2_name (die, cu);
15709 if (name != NULL)
15710 {
15711 if (cu->language == language_cplus
15712 || cu->language == language_d
15713 || cu->language == language_rust)
15714 {
15715 const char *full_name = dwarf2_full_name (name, die, cu);
15716
15717 /* dwarf2_full_name might have already finished building the DIE's
15718 type. If so, there is no need to continue. */
15719 if (get_die_type (die, cu) != NULL)
15720 return get_die_type (die, cu);
15721
15722 TYPE_TAG_NAME (type) = full_name;
15723 if (die->tag == DW_TAG_structure_type
15724 || die->tag == DW_TAG_class_type)
15725 TYPE_NAME (type) = TYPE_TAG_NAME (type);
15726 }
15727 else
15728 {
15729 /* The name is already allocated along with this objfile, so
15730 we don't need to duplicate it for the type. */
15731 TYPE_TAG_NAME (type) = name;
15732 if (die->tag == DW_TAG_class_type)
15733 TYPE_NAME (type) = TYPE_TAG_NAME (type);
15734 }
15735 }
15736
15737 if (die->tag == DW_TAG_structure_type)
15738 {
15739 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15740 }
15741 else if (die->tag == DW_TAG_union_type)
15742 {
15743 TYPE_CODE (type) = TYPE_CODE_UNION;
15744 }
15745 else if (die->tag == DW_TAG_variant_part)
15746 {
15747 TYPE_CODE (type) = TYPE_CODE_UNION;
15748 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15749 }
15750 else
15751 {
15752 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15753 }
15754
15755 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15756 TYPE_DECLARED_CLASS (type) = 1;
15757
15758 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15759 if (attr)
15760 {
15761 if (attr_form_is_constant (attr))
15762 TYPE_LENGTH (type) = DW_UNSND (attr);
15763 else
15764 {
15765 /* For the moment, dynamic type sizes are not supported
15766 by GDB's struct type. The actual size is determined
15767 on-demand when resolving the type of a given object,
15768 so set the type's length to zero for now. Otherwise,
15769 we record an expression as the length, and that expression
15770 could lead to a very large value, which could eventually
15771 lead to us trying to allocate that much memory when creating
15772 a value of that type. */
15773 TYPE_LENGTH (type) = 0;
15774 }
15775 }
15776 else
15777 {
15778 TYPE_LENGTH (type) = 0;
15779 }
15780
15781 maybe_set_alignment (cu, die, type);
15782
15783 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15784 {
15785 /* ICC<14 does not output the required DW_AT_declaration on
15786 incomplete types, but gives them a size of zero. */
15787 TYPE_STUB (type) = 1;
15788 }
15789 else
15790 TYPE_STUB_SUPPORTED (type) = 1;
15791
15792 if (die_is_declaration (die, cu))
15793 TYPE_STUB (type) = 1;
15794 else if (attr == NULL && die->child == NULL
15795 && producer_is_realview (cu->producer))
15796 /* RealView does not output the required DW_AT_declaration
15797 on incomplete types. */
15798 TYPE_STUB (type) = 1;
15799
15800 /* We need to add the type field to the die immediately so we don't
15801 infinitely recurse when dealing with pointers to the structure
15802 type within the structure itself. */
15803 set_die_type (die, type, cu);
15804
15805 /* set_die_type should be already done. */
15806 set_descriptive_type (type, die, cu);
15807
15808 return type;
15809 }
15810
15811 /* A helper for process_structure_scope that handles a single member
15812 DIE. */
15813
15814 static void
15815 handle_struct_member_die (struct die_info *child_die, struct type *type,
15816 struct field_info *fi,
15817 std::vector<struct symbol *> *template_args,
15818 struct dwarf2_cu *cu)
15819 {
15820 if (child_die->tag == DW_TAG_member
15821 || child_die->tag == DW_TAG_variable
15822 || child_die->tag == DW_TAG_variant_part)
15823 {
15824 /* NOTE: carlton/2002-11-05: A C++ static data member
15825 should be a DW_TAG_member that is a declaration, but
15826 all versions of G++ as of this writing (so through at
15827 least 3.2.1) incorrectly generate DW_TAG_variable
15828 tags for them instead. */
15829 dwarf2_add_field (fi, child_die, cu);
15830 }
15831 else if (child_die->tag == DW_TAG_subprogram)
15832 {
15833 /* Rust doesn't have member functions in the C++ sense.
15834 However, it does emit ordinary functions as children
15835 of a struct DIE. */
15836 if (cu->language == language_rust)
15837 read_func_scope (child_die, cu);
15838 else
15839 {
15840 /* C++ member function. */
15841 dwarf2_add_member_fn (fi, child_die, type, cu);
15842 }
15843 }
15844 else if (child_die->tag == DW_TAG_inheritance)
15845 {
15846 /* C++ base class field. */
15847 dwarf2_add_field (fi, child_die, cu);
15848 }
15849 else if (type_can_define_types (child_die))
15850 dwarf2_add_type_defn (fi, child_die, cu);
15851 else if (child_die->tag == DW_TAG_template_type_param
15852 || child_die->tag == DW_TAG_template_value_param)
15853 {
15854 struct symbol *arg = new_symbol (child_die, NULL, cu);
15855
15856 if (arg != NULL)
15857 template_args->push_back (arg);
15858 }
15859 else if (child_die->tag == DW_TAG_variant)
15860 {
15861 /* In a variant we want to get the discriminant and also add a
15862 field for our sole member child. */
15863 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15864
15865 for (struct die_info *variant_child = child_die->child;
15866 variant_child != NULL;
15867 variant_child = sibling_die (variant_child))
15868 {
15869 if (variant_child->tag == DW_TAG_member)
15870 {
15871 handle_struct_member_die (variant_child, type, fi,
15872 template_args, cu);
15873 /* Only handle the one. */
15874 break;
15875 }
15876 }
15877
15878 /* We don't handle this but we might as well report it if we see
15879 it. */
15880 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
15881 complaint (&symfile_complaints,
15882 _("DW_AT_discr_list is not supported yet"
15883 " - DIE at %s [in module %s]"),
15884 sect_offset_str (child_die->sect_off),
15885 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15886
15887 /* The first field was just added, so we can stash the
15888 discriminant there. */
15889 gdb_assert (!fi->fields.empty ());
15890 if (discr == NULL)
15891 fi->fields.back ().variant.default_branch = true;
15892 else
15893 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
15894 }
15895 }
15896
15897 /* Finish creating a structure or union type, including filling in
15898 its members and creating a symbol for it. */
15899
15900 static void
15901 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15902 {
15903 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15904 struct die_info *child_die;
15905 struct type *type;
15906
15907 type = get_die_type (die, cu);
15908 if (type == NULL)
15909 type = read_structure_type (die, cu);
15910
15911 /* When reading a DW_TAG_variant_part, we need to notice when we
15912 read the discriminant member, so we can record it later in the
15913 discriminant_info. */
15914 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
15915 sect_offset discr_offset;
15916
15917 if (is_variant_part)
15918 {
15919 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15920 if (discr == NULL)
15921 {
15922 /* Maybe it's a univariant form, an extension we support.
15923 In this case arrange not to check the offset. */
15924 is_variant_part = false;
15925 }
15926 else if (attr_form_is_ref (discr))
15927 {
15928 struct dwarf2_cu *target_cu = cu;
15929 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15930
15931 discr_offset = target_die->sect_off;
15932 }
15933 else
15934 {
15935 complaint (&symfile_complaints,
15936 _("DW_AT_discr does not have DIE reference form"
15937 " - DIE at %s [in module %s]"),
15938 sect_offset_str (die->sect_off),
15939 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15940 is_variant_part = false;
15941 }
15942 }
15943
15944 if (die->child != NULL && ! die_is_declaration (die, cu))
15945 {
15946 struct field_info fi;
15947 std::vector<struct symbol *> template_args;
15948
15949 child_die = die->child;
15950
15951 while (child_die && child_die->tag)
15952 {
15953 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15954
15955 if (is_variant_part && discr_offset == child_die->sect_off)
15956 fi.fields.back ().variant.is_discriminant = true;
15957
15958 child_die = sibling_die (child_die);
15959 }
15960
15961 /* Attach template arguments to type. */
15962 if (!template_args.empty ())
15963 {
15964 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15965 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15966 TYPE_TEMPLATE_ARGUMENTS (type)
15967 = XOBNEWVEC (&objfile->objfile_obstack,
15968 struct symbol *,
15969 TYPE_N_TEMPLATE_ARGUMENTS (type));
15970 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15971 template_args.data (),
15972 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15973 * sizeof (struct symbol *)));
15974 }
15975
15976 /* Attach fields and member functions to the type. */
15977 if (fi.nfields)
15978 dwarf2_attach_fields_to_type (&fi, type, cu);
15979 if (!fi.fnfieldlists.empty ())
15980 {
15981 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15982
15983 /* Get the type which refers to the base class (possibly this
15984 class itself) which contains the vtable pointer for the current
15985 class from the DW_AT_containing_type attribute. This use of
15986 DW_AT_containing_type is a GNU extension. */
15987
15988 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15989 {
15990 struct type *t = die_containing_type (die, cu);
15991
15992 set_type_vptr_basetype (type, t);
15993 if (type == t)
15994 {
15995 int i;
15996
15997 /* Our own class provides vtbl ptr. */
15998 for (i = TYPE_NFIELDS (t) - 1;
15999 i >= TYPE_N_BASECLASSES (t);
16000 --i)
16001 {
16002 const char *fieldname = TYPE_FIELD_NAME (t, i);
16003
16004 if (is_vtable_name (fieldname, cu))
16005 {
16006 set_type_vptr_fieldno (type, i);
16007 break;
16008 }
16009 }
16010
16011 /* Complain if virtual function table field not found. */
16012 if (i < TYPE_N_BASECLASSES (t))
16013 complaint (&symfile_complaints,
16014 _("virtual function table pointer "
16015 "not found when defining class '%s'"),
16016 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
16017 "");
16018 }
16019 else
16020 {
16021 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16022 }
16023 }
16024 else if (cu->producer
16025 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16026 {
16027 /* The IBM XLC compiler does not provide direct indication
16028 of the containing type, but the vtable pointer is
16029 always named __vfp. */
16030
16031 int i;
16032
16033 for (i = TYPE_NFIELDS (type) - 1;
16034 i >= TYPE_N_BASECLASSES (type);
16035 --i)
16036 {
16037 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16038 {
16039 set_type_vptr_fieldno (type, i);
16040 set_type_vptr_basetype (type, type);
16041 break;
16042 }
16043 }
16044 }
16045 }
16046
16047 /* Copy fi.typedef_field_list linked list elements content into the
16048 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16049 if (!fi.typedef_field_list.empty ())
16050 {
16051 int count = fi.typedef_field_list.size ();
16052
16053 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16054 TYPE_TYPEDEF_FIELD_ARRAY (type)
16055 = ((struct decl_field *)
16056 TYPE_ALLOC (type,
16057 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16058 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16059
16060 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16061 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16062 }
16063
16064 /* Copy fi.nested_types_list linked list elements content into the
16065 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16066 if (!fi.nested_types_list.empty () && cu->language != language_ada)
16067 {
16068 int count = fi.nested_types_list.size ();
16069
16070 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16071 TYPE_NESTED_TYPES_ARRAY (type)
16072 = ((struct decl_field *)
16073 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16074 TYPE_NESTED_TYPES_COUNT (type) = count;
16075
16076 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16077 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16078 }
16079 }
16080
16081 quirk_gcc_member_function_pointer (type, objfile);
16082 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16083 cu->rust_unions.push_back (type);
16084
16085 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16086 snapshots) has been known to create a die giving a declaration
16087 for a class that has, as a child, a die giving a definition for a
16088 nested class. So we have to process our children even if the
16089 current die is a declaration. Normally, of course, a declaration
16090 won't have any children at all. */
16091
16092 child_die = die->child;
16093
16094 while (child_die != NULL && child_die->tag)
16095 {
16096 if (child_die->tag == DW_TAG_member
16097 || child_die->tag == DW_TAG_variable
16098 || child_die->tag == DW_TAG_inheritance
16099 || child_die->tag == DW_TAG_template_value_param
16100 || child_die->tag == DW_TAG_template_type_param)
16101 {
16102 /* Do nothing. */
16103 }
16104 else
16105 process_die (child_die, cu);
16106
16107 child_die = sibling_die (child_die);
16108 }
16109
16110 /* Do not consider external references. According to the DWARF standard,
16111 these DIEs are identified by the fact that they have no byte_size
16112 attribute, and a declaration attribute. */
16113 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16114 || !die_is_declaration (die, cu))
16115 new_symbol (die, type, cu);
16116 }
16117
16118 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
16119 update TYPE using some information only available in DIE's children. */
16120
16121 static void
16122 update_enumeration_type_from_children (struct die_info *die,
16123 struct type *type,
16124 struct dwarf2_cu *cu)
16125 {
16126 struct die_info *child_die;
16127 int unsigned_enum = 1;
16128 int flag_enum = 1;
16129 ULONGEST mask = 0;
16130
16131 auto_obstack obstack;
16132
16133 for (child_die = die->child;
16134 child_die != NULL && child_die->tag;
16135 child_die = sibling_die (child_die))
16136 {
16137 struct attribute *attr;
16138 LONGEST value;
16139 const gdb_byte *bytes;
16140 struct dwarf2_locexpr_baton *baton;
16141 const char *name;
16142
16143 if (child_die->tag != DW_TAG_enumerator)
16144 continue;
16145
16146 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16147 if (attr == NULL)
16148 continue;
16149
16150 name = dwarf2_name (child_die, cu);
16151 if (name == NULL)
16152 name = "<anonymous enumerator>";
16153
16154 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16155 &value, &bytes, &baton);
16156 if (value < 0)
16157 {
16158 unsigned_enum = 0;
16159 flag_enum = 0;
16160 }
16161 else if ((mask & value) != 0)
16162 flag_enum = 0;
16163 else
16164 mask |= value;
16165
16166 /* If we already know that the enum type is neither unsigned, nor
16167 a flag type, no need to look at the rest of the enumerates. */
16168 if (!unsigned_enum && !flag_enum)
16169 break;
16170 }
16171
16172 if (unsigned_enum)
16173 TYPE_UNSIGNED (type) = 1;
16174 if (flag_enum)
16175 TYPE_FLAG_ENUM (type) = 1;
16176 }
16177
16178 /* Given a DW_AT_enumeration_type die, set its type. We do not
16179 complete the type's fields yet, or create any symbols. */
16180
16181 static struct type *
16182 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16183 {
16184 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16185 struct type *type;
16186 struct attribute *attr;
16187 const char *name;
16188
16189 /* If the definition of this type lives in .debug_types, read that type.
16190 Don't follow DW_AT_specification though, that will take us back up
16191 the chain and we want to go down. */
16192 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
16193 if (attr)
16194 {
16195 type = get_DW_AT_signature_type (die, attr, cu);
16196
16197 /* The type's CU may not be the same as CU.
16198 Ensure TYPE is recorded with CU in die_type_hash. */
16199 return set_die_type (die, type, cu);
16200 }
16201
16202 type = alloc_type (objfile);
16203
16204 TYPE_CODE (type) = TYPE_CODE_ENUM;
16205 name = dwarf2_full_name (NULL, die, cu);
16206 if (name != NULL)
16207 TYPE_TAG_NAME (type) = name;
16208
16209 attr = dwarf2_attr (die, DW_AT_type, cu);
16210 if (attr != NULL)
16211 {
16212 struct type *underlying_type = die_type (die, cu);
16213
16214 TYPE_TARGET_TYPE (type) = underlying_type;
16215 }
16216
16217 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16218 if (attr)
16219 {
16220 TYPE_LENGTH (type) = DW_UNSND (attr);
16221 }
16222 else
16223 {
16224 TYPE_LENGTH (type) = 0;
16225 }
16226
16227 maybe_set_alignment (cu, die, type);
16228
16229 /* The enumeration DIE can be incomplete. In Ada, any type can be
16230 declared as private in the package spec, and then defined only
16231 inside the package body. Such types are known as Taft Amendment
16232 Types. When another package uses such a type, an incomplete DIE
16233 may be generated by the compiler. */
16234 if (die_is_declaration (die, cu))
16235 TYPE_STUB (type) = 1;
16236
16237 /* Finish the creation of this type by using the enum's children.
16238 We must call this even when the underlying type has been provided
16239 so that we can determine if we're looking at a "flag" enum. */
16240 update_enumeration_type_from_children (die, type, cu);
16241
16242 /* If this type has an underlying type that is not a stub, then we
16243 may use its attributes. We always use the "unsigned" attribute
16244 in this situation, because ordinarily we guess whether the type
16245 is unsigned -- but the guess can be wrong and the underlying type
16246 can tell us the reality. However, we defer to a local size
16247 attribute if one exists, because this lets the compiler override
16248 the underlying type if needed. */
16249 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16250 {
16251 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16252 if (TYPE_LENGTH (type) == 0)
16253 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16254 if (TYPE_RAW_ALIGN (type) == 0
16255 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
16256 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
16257 }
16258
16259 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16260
16261 return set_die_type (die, type, cu);
16262 }
16263
16264 /* Given a pointer to a die which begins an enumeration, process all
16265 the dies that define the members of the enumeration, and create the
16266 symbol for the enumeration type.
16267
16268 NOTE: We reverse the order of the element list. */
16269
16270 static void
16271 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16272 {
16273 struct type *this_type;
16274
16275 this_type = get_die_type (die, cu);
16276 if (this_type == NULL)
16277 this_type = read_enumeration_type (die, cu);
16278
16279 if (die->child != NULL)
16280 {
16281 struct die_info *child_die;
16282 struct symbol *sym;
16283 struct field *fields = NULL;
16284 int num_fields = 0;
16285 const char *name;
16286
16287 child_die = die->child;
16288 while (child_die && child_die->tag)
16289 {
16290 if (child_die->tag != DW_TAG_enumerator)
16291 {
16292 process_die (child_die, cu);
16293 }
16294 else
16295 {
16296 name = dwarf2_name (child_die, cu);
16297 if (name)
16298 {
16299 sym = new_symbol (child_die, this_type, cu);
16300
16301 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16302 {
16303 fields = (struct field *)
16304 xrealloc (fields,
16305 (num_fields + DW_FIELD_ALLOC_CHUNK)
16306 * sizeof (struct field));
16307 }
16308
16309 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
16310 FIELD_TYPE (fields[num_fields]) = NULL;
16311 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
16312 FIELD_BITSIZE (fields[num_fields]) = 0;
16313
16314 num_fields++;
16315 }
16316 }
16317
16318 child_die = sibling_die (child_die);
16319 }
16320
16321 if (num_fields)
16322 {
16323 TYPE_NFIELDS (this_type) = num_fields;
16324 TYPE_FIELDS (this_type) = (struct field *)
16325 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16326 memcpy (TYPE_FIELDS (this_type), fields,
16327 sizeof (struct field) * num_fields);
16328 xfree (fields);
16329 }
16330 }
16331
16332 /* If we are reading an enum from a .debug_types unit, and the enum
16333 is a declaration, and the enum is not the signatured type in the
16334 unit, then we do not want to add a symbol for it. Adding a
16335 symbol would in some cases obscure the true definition of the
16336 enum, giving users an incomplete type when the definition is
16337 actually available. Note that we do not want to do this for all
16338 enums which are just declarations, because C++0x allows forward
16339 enum declarations. */
16340 if (cu->per_cu->is_debug_types
16341 && die_is_declaration (die, cu))
16342 {
16343 struct signatured_type *sig_type;
16344
16345 sig_type = (struct signatured_type *) cu->per_cu;
16346 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16347 if (sig_type->type_offset_in_section != die->sect_off)
16348 return;
16349 }
16350
16351 new_symbol (die, this_type, cu);
16352 }
16353
16354 /* Extract all information from a DW_TAG_array_type DIE and put it in
16355 the DIE's type field. For now, this only handles one dimensional
16356 arrays. */
16357
16358 static struct type *
16359 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16360 {
16361 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16362 struct die_info *child_die;
16363 struct type *type;
16364 struct type *element_type, *range_type, *index_type;
16365 struct attribute *attr;
16366 const char *name;
16367 struct dynamic_prop *byte_stride_prop = NULL;
16368 unsigned int bit_stride = 0;
16369
16370 element_type = die_type (die, cu);
16371
16372 /* The die_type call above may have already set the type for this DIE. */
16373 type = get_die_type (die, cu);
16374 if (type)
16375 return type;
16376
16377 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16378 if (attr != NULL)
16379 {
16380 int stride_ok;
16381
16382 byte_stride_prop
16383 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16384 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop);
16385 if (!stride_ok)
16386 {
16387 complaint (&symfile_complaints,
16388 _("unable to read array DW_AT_byte_stride "
16389 " - DIE at %s [in module %s]"),
16390 sect_offset_str (die->sect_off),
16391 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16392 /* Ignore this attribute. We will likely not be able to print
16393 arrays of this type correctly, but there is little we can do
16394 to help if we cannot read the attribute's value. */
16395 byte_stride_prop = NULL;
16396 }
16397 }
16398
16399 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16400 if (attr != NULL)
16401 bit_stride = DW_UNSND (attr);
16402
16403 /* Irix 6.2 native cc creates array types without children for
16404 arrays with unspecified length. */
16405 if (die->child == NULL)
16406 {
16407 index_type = objfile_type (objfile)->builtin_int;
16408 range_type = create_static_range_type (NULL, index_type, 0, -1);
16409 type = create_array_type_with_stride (NULL, element_type, range_type,
16410 byte_stride_prop, bit_stride);
16411 return set_die_type (die, type, cu);
16412 }
16413
16414 std::vector<struct type *> range_types;
16415 child_die = die->child;
16416 while (child_die && child_die->tag)
16417 {
16418 if (child_die->tag == DW_TAG_subrange_type)
16419 {
16420 struct type *child_type = read_type_die (child_die, cu);
16421
16422 if (child_type != NULL)
16423 {
16424 /* The range type was succesfully read. Save it for the
16425 array type creation. */
16426 range_types.push_back (child_type);
16427 }
16428 }
16429 child_die = sibling_die (child_die);
16430 }
16431
16432 /* Dwarf2 dimensions are output from left to right, create the
16433 necessary array types in backwards order. */
16434
16435 type = element_type;
16436
16437 if (read_array_order (die, cu) == DW_ORD_col_major)
16438 {
16439 int i = 0;
16440
16441 while (i < range_types.size ())
16442 type = create_array_type_with_stride (NULL, type, range_types[i++],
16443 byte_stride_prop, bit_stride);
16444 }
16445 else
16446 {
16447 size_t ndim = range_types.size ();
16448 while (ndim-- > 0)
16449 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16450 byte_stride_prop, bit_stride);
16451 }
16452
16453 /* Understand Dwarf2 support for vector types (like they occur on
16454 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16455 array type. This is not part of the Dwarf2/3 standard yet, but a
16456 custom vendor extension. The main difference between a regular
16457 array and the vector variant is that vectors are passed by value
16458 to functions. */
16459 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16460 if (attr)
16461 make_vector_type (type);
16462
16463 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16464 implementation may choose to implement triple vectors using this
16465 attribute. */
16466 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16467 if (attr)
16468 {
16469 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16470 TYPE_LENGTH (type) = DW_UNSND (attr);
16471 else
16472 complaint (&symfile_complaints,
16473 _("DW_AT_byte_size for array type smaller "
16474 "than the total size of elements"));
16475 }
16476
16477 name = dwarf2_name (die, cu);
16478 if (name)
16479 TYPE_NAME (type) = name;
16480
16481 maybe_set_alignment (cu, die, type);
16482
16483 /* Install the type in the die. */
16484 set_die_type (die, type, cu);
16485
16486 /* set_die_type should be already done. */
16487 set_descriptive_type (type, die, cu);
16488
16489 return type;
16490 }
16491
16492 static enum dwarf_array_dim_ordering
16493 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16494 {
16495 struct attribute *attr;
16496
16497 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16498
16499 if (attr)
16500 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16501
16502 /* GNU F77 is a special case, as at 08/2004 array type info is the
16503 opposite order to the dwarf2 specification, but data is still
16504 laid out as per normal fortran.
16505
16506 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16507 version checking. */
16508
16509 if (cu->language == language_fortran
16510 && cu->producer && strstr (cu->producer, "GNU F77"))
16511 {
16512 return DW_ORD_row_major;
16513 }
16514
16515 switch (cu->language_defn->la_array_ordering)
16516 {
16517 case array_column_major:
16518 return DW_ORD_col_major;
16519 case array_row_major:
16520 default:
16521 return DW_ORD_row_major;
16522 };
16523 }
16524
16525 /* Extract all information from a DW_TAG_set_type DIE and put it in
16526 the DIE's type field. */
16527
16528 static struct type *
16529 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16530 {
16531 struct type *domain_type, *set_type;
16532 struct attribute *attr;
16533
16534 domain_type = die_type (die, cu);
16535
16536 /* The die_type call above may have already set the type for this DIE. */
16537 set_type = get_die_type (die, cu);
16538 if (set_type)
16539 return set_type;
16540
16541 set_type = create_set_type (NULL, domain_type);
16542
16543 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16544 if (attr)
16545 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16546
16547 maybe_set_alignment (cu, die, set_type);
16548
16549 return set_die_type (die, set_type, cu);
16550 }
16551
16552 /* A helper for read_common_block that creates a locexpr baton.
16553 SYM is the symbol which we are marking as computed.
16554 COMMON_DIE is the DIE for the common block.
16555 COMMON_LOC is the location expression attribute for the common
16556 block itself.
16557 MEMBER_LOC is the location expression attribute for the particular
16558 member of the common block that we are processing.
16559 CU is the CU from which the above come. */
16560
16561 static void
16562 mark_common_block_symbol_computed (struct symbol *sym,
16563 struct die_info *common_die,
16564 struct attribute *common_loc,
16565 struct attribute *member_loc,
16566 struct dwarf2_cu *cu)
16567 {
16568 struct dwarf2_per_objfile *dwarf2_per_objfile
16569 = cu->per_cu->dwarf2_per_objfile;
16570 struct objfile *objfile = dwarf2_per_objfile->objfile;
16571 struct dwarf2_locexpr_baton *baton;
16572 gdb_byte *ptr;
16573 unsigned int cu_off;
16574 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16575 LONGEST offset = 0;
16576
16577 gdb_assert (common_loc && member_loc);
16578 gdb_assert (attr_form_is_block (common_loc));
16579 gdb_assert (attr_form_is_block (member_loc)
16580 || attr_form_is_constant (member_loc));
16581
16582 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16583 baton->per_cu = cu->per_cu;
16584 gdb_assert (baton->per_cu);
16585
16586 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16587
16588 if (attr_form_is_constant (member_loc))
16589 {
16590 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16591 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16592 }
16593 else
16594 baton->size += DW_BLOCK (member_loc)->size;
16595
16596 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16597 baton->data = ptr;
16598
16599 *ptr++ = DW_OP_call4;
16600 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16601 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16602 ptr += 4;
16603
16604 if (attr_form_is_constant (member_loc))
16605 {
16606 *ptr++ = DW_OP_addr;
16607 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16608 ptr += cu->header.addr_size;
16609 }
16610 else
16611 {
16612 /* We have to copy the data here, because DW_OP_call4 will only
16613 use a DW_AT_location attribute. */
16614 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16615 ptr += DW_BLOCK (member_loc)->size;
16616 }
16617
16618 *ptr++ = DW_OP_plus;
16619 gdb_assert (ptr - baton->data == baton->size);
16620
16621 SYMBOL_LOCATION_BATON (sym) = baton;
16622 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16623 }
16624
16625 /* Create appropriate locally-scoped variables for all the
16626 DW_TAG_common_block entries. Also create a struct common_block
16627 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16628 is used to sepate the common blocks name namespace from regular
16629 variable names. */
16630
16631 static void
16632 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16633 {
16634 struct attribute *attr;
16635
16636 attr = dwarf2_attr (die, DW_AT_location, cu);
16637 if (attr)
16638 {
16639 /* Support the .debug_loc offsets. */
16640 if (attr_form_is_block (attr))
16641 {
16642 /* Ok. */
16643 }
16644 else if (attr_form_is_section_offset (attr))
16645 {
16646 dwarf2_complex_location_expr_complaint ();
16647 attr = NULL;
16648 }
16649 else
16650 {
16651 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16652 "common block member");
16653 attr = NULL;
16654 }
16655 }
16656
16657 if (die->child != NULL)
16658 {
16659 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16660 struct die_info *child_die;
16661 size_t n_entries = 0, size;
16662 struct common_block *common_block;
16663 struct symbol *sym;
16664
16665 for (child_die = die->child;
16666 child_die && child_die->tag;
16667 child_die = sibling_die (child_die))
16668 ++n_entries;
16669
16670 size = (sizeof (struct common_block)
16671 + (n_entries - 1) * sizeof (struct symbol *));
16672 common_block
16673 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16674 size);
16675 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16676 common_block->n_entries = 0;
16677
16678 for (child_die = die->child;
16679 child_die && child_die->tag;
16680 child_die = sibling_die (child_die))
16681 {
16682 /* Create the symbol in the DW_TAG_common_block block in the current
16683 symbol scope. */
16684 sym = new_symbol (child_die, NULL, cu);
16685 if (sym != NULL)
16686 {
16687 struct attribute *member_loc;
16688
16689 common_block->contents[common_block->n_entries++] = sym;
16690
16691 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16692 cu);
16693 if (member_loc)
16694 {
16695 /* GDB has handled this for a long time, but it is
16696 not specified by DWARF. It seems to have been
16697 emitted by gfortran at least as recently as:
16698 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16699 complaint (&symfile_complaints,
16700 _("Variable in common block has "
16701 "DW_AT_data_member_location "
16702 "- DIE at %s [in module %s]"),
16703 sect_offset_str (child_die->sect_off),
16704 objfile_name (objfile));
16705
16706 if (attr_form_is_section_offset (member_loc))
16707 dwarf2_complex_location_expr_complaint ();
16708 else if (attr_form_is_constant (member_loc)
16709 || attr_form_is_block (member_loc))
16710 {
16711 if (attr)
16712 mark_common_block_symbol_computed (sym, die, attr,
16713 member_loc, cu);
16714 }
16715 else
16716 dwarf2_complex_location_expr_complaint ();
16717 }
16718 }
16719 }
16720
16721 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16722 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16723 }
16724 }
16725
16726 /* Create a type for a C++ namespace. */
16727
16728 static struct type *
16729 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16730 {
16731 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16732 const char *previous_prefix, *name;
16733 int is_anonymous;
16734 struct type *type;
16735
16736 /* For extensions, reuse the type of the original namespace. */
16737 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16738 {
16739 struct die_info *ext_die;
16740 struct dwarf2_cu *ext_cu = cu;
16741
16742 ext_die = dwarf2_extension (die, &ext_cu);
16743 type = read_type_die (ext_die, ext_cu);
16744
16745 /* EXT_CU may not be the same as CU.
16746 Ensure TYPE is recorded with CU in die_type_hash. */
16747 return set_die_type (die, type, cu);
16748 }
16749
16750 name = namespace_name (die, &is_anonymous, cu);
16751
16752 /* Now build the name of the current namespace. */
16753
16754 previous_prefix = determine_prefix (die, cu);
16755 if (previous_prefix[0] != '\0')
16756 name = typename_concat (&objfile->objfile_obstack,
16757 previous_prefix, name, 0, cu);
16758
16759 /* Create the type. */
16760 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16761 TYPE_TAG_NAME (type) = TYPE_NAME (type);
16762
16763 return set_die_type (die, type, cu);
16764 }
16765
16766 /* Read a namespace scope. */
16767
16768 static void
16769 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16770 {
16771 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16772 int is_anonymous;
16773
16774 /* Add a symbol associated to this if we haven't seen the namespace
16775 before. Also, add a using directive if it's an anonymous
16776 namespace. */
16777
16778 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16779 {
16780 struct type *type;
16781
16782 type = read_type_die (die, cu);
16783 new_symbol (die, type, cu);
16784
16785 namespace_name (die, &is_anonymous, cu);
16786 if (is_anonymous)
16787 {
16788 const char *previous_prefix = determine_prefix (die, cu);
16789
16790 std::vector<const char *> excludes;
16791 add_using_directive (using_directives (cu->language),
16792 previous_prefix, TYPE_NAME (type), NULL,
16793 NULL, excludes, 0, &objfile->objfile_obstack);
16794 }
16795 }
16796
16797 if (die->child != NULL)
16798 {
16799 struct die_info *child_die = die->child;
16800
16801 while (child_die && child_die->tag)
16802 {
16803 process_die (child_die, cu);
16804 child_die = sibling_die (child_die);
16805 }
16806 }
16807 }
16808
16809 /* Read a Fortran module as type. This DIE can be only a declaration used for
16810 imported module. Still we need that type as local Fortran "use ... only"
16811 declaration imports depend on the created type in determine_prefix. */
16812
16813 static struct type *
16814 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16815 {
16816 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16817 const char *module_name;
16818 struct type *type;
16819
16820 module_name = dwarf2_name (die, cu);
16821 if (!module_name)
16822 complaint (&symfile_complaints,
16823 _("DW_TAG_module has no name, offset %s"),
16824 sect_offset_str (die->sect_off));
16825 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16826
16827 /* determine_prefix uses TYPE_TAG_NAME. */
16828 TYPE_TAG_NAME (type) = TYPE_NAME (type);
16829
16830 return set_die_type (die, type, cu);
16831 }
16832
16833 /* Read a Fortran module. */
16834
16835 static void
16836 read_module (struct die_info *die, struct dwarf2_cu *cu)
16837 {
16838 struct die_info *child_die = die->child;
16839 struct type *type;
16840
16841 type = read_type_die (die, cu);
16842 new_symbol (die, type, cu);
16843
16844 while (child_die && child_die->tag)
16845 {
16846 process_die (child_die, cu);
16847 child_die = sibling_die (child_die);
16848 }
16849 }
16850
16851 /* Return the name of the namespace represented by DIE. Set
16852 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16853 namespace. */
16854
16855 static const char *
16856 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16857 {
16858 struct die_info *current_die;
16859 const char *name = NULL;
16860
16861 /* Loop through the extensions until we find a name. */
16862
16863 for (current_die = die;
16864 current_die != NULL;
16865 current_die = dwarf2_extension (die, &cu))
16866 {
16867 /* We don't use dwarf2_name here so that we can detect the absence
16868 of a name -> anonymous namespace. */
16869 name = dwarf2_string_attr (die, DW_AT_name, cu);
16870
16871 if (name != NULL)
16872 break;
16873 }
16874
16875 /* Is it an anonymous namespace? */
16876
16877 *is_anonymous = (name == NULL);
16878 if (*is_anonymous)
16879 name = CP_ANONYMOUS_NAMESPACE_STR;
16880
16881 return name;
16882 }
16883
16884 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16885 the user defined type vector. */
16886
16887 static struct type *
16888 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16889 {
16890 struct gdbarch *gdbarch
16891 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
16892 struct comp_unit_head *cu_header = &cu->header;
16893 struct type *type;
16894 struct attribute *attr_byte_size;
16895 struct attribute *attr_address_class;
16896 int byte_size, addr_class;
16897 struct type *target_type;
16898
16899 target_type = die_type (die, cu);
16900
16901 /* The die_type call above may have already set the type for this DIE. */
16902 type = get_die_type (die, cu);
16903 if (type)
16904 return type;
16905
16906 type = lookup_pointer_type (target_type);
16907
16908 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16909 if (attr_byte_size)
16910 byte_size = DW_UNSND (attr_byte_size);
16911 else
16912 byte_size = cu_header->addr_size;
16913
16914 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16915 if (attr_address_class)
16916 addr_class = DW_UNSND (attr_address_class);
16917 else
16918 addr_class = DW_ADDR_none;
16919
16920 ULONGEST alignment = get_alignment (cu, die);
16921
16922 /* If the pointer size, alignment, or address class is different
16923 than the default, create a type variant marked as such and set
16924 the length accordingly. */
16925 if (TYPE_LENGTH (type) != byte_size
16926 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16927 && alignment != TYPE_RAW_ALIGN (type))
16928 || addr_class != DW_ADDR_none)
16929 {
16930 if (gdbarch_address_class_type_flags_p (gdbarch))
16931 {
16932 int type_flags;
16933
16934 type_flags = gdbarch_address_class_type_flags
16935 (gdbarch, byte_size, addr_class);
16936 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16937 == 0);
16938 type = make_type_with_address_space (type, type_flags);
16939 }
16940 else if (TYPE_LENGTH (type) != byte_size)
16941 {
16942 complaint (&symfile_complaints,
16943 _("invalid pointer size %d"), byte_size);
16944 }
16945 else if (TYPE_RAW_ALIGN (type) != alignment)
16946 {
16947 complaint (&symfile_complaints,
16948 _("Invalid DW_AT_alignment"
16949 " - DIE at %s [in module %s]"),
16950 sect_offset_str (die->sect_off),
16951 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16952 }
16953 else
16954 {
16955 /* Should we also complain about unhandled address classes? */
16956 }
16957 }
16958
16959 TYPE_LENGTH (type) = byte_size;
16960 set_type_align (type, alignment);
16961 return set_die_type (die, type, cu);
16962 }
16963
16964 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16965 the user defined type vector. */
16966
16967 static struct type *
16968 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16969 {
16970 struct type *type;
16971 struct type *to_type;
16972 struct type *domain;
16973
16974 to_type = die_type (die, cu);
16975 domain = die_containing_type (die, cu);
16976
16977 /* The calls above may have already set the type for this DIE. */
16978 type = get_die_type (die, cu);
16979 if (type)
16980 return type;
16981
16982 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16983 type = lookup_methodptr_type (to_type);
16984 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16985 {
16986 struct type *new_type
16987 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
16988
16989 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16990 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16991 TYPE_VARARGS (to_type));
16992 type = lookup_methodptr_type (new_type);
16993 }
16994 else
16995 type = lookup_memberptr_type (to_type, domain);
16996
16997 return set_die_type (die, type, cu);
16998 }
16999
17000 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17001 the user defined type vector. */
17002
17003 static struct type *
17004 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17005 enum type_code refcode)
17006 {
17007 struct comp_unit_head *cu_header = &cu->header;
17008 struct type *type, *target_type;
17009 struct attribute *attr;
17010
17011 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17012
17013 target_type = die_type (die, cu);
17014
17015 /* The die_type call above may have already set the type for this DIE. */
17016 type = get_die_type (die, cu);
17017 if (type)
17018 return type;
17019
17020 type = lookup_reference_type (target_type, refcode);
17021 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17022 if (attr)
17023 {
17024 TYPE_LENGTH (type) = DW_UNSND (attr);
17025 }
17026 else
17027 {
17028 TYPE_LENGTH (type) = cu_header->addr_size;
17029 }
17030 maybe_set_alignment (cu, die, type);
17031 return set_die_type (die, type, cu);
17032 }
17033
17034 /* Add the given cv-qualifiers to the element type of the array. GCC
17035 outputs DWARF type qualifiers that apply to an array, not the
17036 element type. But GDB relies on the array element type to carry
17037 the cv-qualifiers. This mimics section 6.7.3 of the C99
17038 specification. */
17039
17040 static struct type *
17041 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17042 struct type *base_type, int cnst, int voltl)
17043 {
17044 struct type *el_type, *inner_array;
17045
17046 base_type = copy_type (base_type);
17047 inner_array = base_type;
17048
17049 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17050 {
17051 TYPE_TARGET_TYPE (inner_array) =
17052 copy_type (TYPE_TARGET_TYPE (inner_array));
17053 inner_array = TYPE_TARGET_TYPE (inner_array);
17054 }
17055
17056 el_type = TYPE_TARGET_TYPE (inner_array);
17057 cnst |= TYPE_CONST (el_type);
17058 voltl |= TYPE_VOLATILE (el_type);
17059 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17060
17061 return set_die_type (die, base_type, cu);
17062 }
17063
17064 static struct type *
17065 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17066 {
17067 struct type *base_type, *cv_type;
17068
17069 base_type = die_type (die, cu);
17070
17071 /* The die_type call above may have already set the type for this DIE. */
17072 cv_type = get_die_type (die, cu);
17073 if (cv_type)
17074 return cv_type;
17075
17076 /* In case the const qualifier is applied to an array type, the element type
17077 is so qualified, not the array type (section 6.7.3 of C99). */
17078 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17079 return add_array_cv_type (die, cu, base_type, 1, 0);
17080
17081 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17082 return set_die_type (die, cv_type, cu);
17083 }
17084
17085 static struct type *
17086 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17087 {
17088 struct type *base_type, *cv_type;
17089
17090 base_type = die_type (die, cu);
17091
17092 /* The die_type call above may have already set the type for this DIE. */
17093 cv_type = get_die_type (die, cu);
17094 if (cv_type)
17095 return cv_type;
17096
17097 /* In case the volatile qualifier is applied to an array type, the
17098 element type is so qualified, not the array type (section 6.7.3
17099 of C99). */
17100 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17101 return add_array_cv_type (die, cu, base_type, 0, 1);
17102
17103 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17104 return set_die_type (die, cv_type, cu);
17105 }
17106
17107 /* Handle DW_TAG_restrict_type. */
17108
17109 static struct type *
17110 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17111 {
17112 struct type *base_type, *cv_type;
17113
17114 base_type = die_type (die, cu);
17115
17116 /* The die_type call above may have already set the type for this DIE. */
17117 cv_type = get_die_type (die, cu);
17118 if (cv_type)
17119 return cv_type;
17120
17121 cv_type = make_restrict_type (base_type);
17122 return set_die_type (die, cv_type, cu);
17123 }
17124
17125 /* Handle DW_TAG_atomic_type. */
17126
17127 static struct type *
17128 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17129 {
17130 struct type *base_type, *cv_type;
17131
17132 base_type = die_type (die, cu);
17133
17134 /* The die_type call above may have already set the type for this DIE. */
17135 cv_type = get_die_type (die, cu);
17136 if (cv_type)
17137 return cv_type;
17138
17139 cv_type = make_atomic_type (base_type);
17140 return set_die_type (die, cv_type, cu);
17141 }
17142
17143 /* Extract all information from a DW_TAG_string_type DIE and add to
17144 the user defined type vector. It isn't really a user defined type,
17145 but it behaves like one, with other DIE's using an AT_user_def_type
17146 attribute to reference it. */
17147
17148 static struct type *
17149 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17150 {
17151 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17152 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17153 struct type *type, *range_type, *index_type, *char_type;
17154 struct attribute *attr;
17155 unsigned int length;
17156
17157 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17158 if (attr)
17159 {
17160 length = DW_UNSND (attr);
17161 }
17162 else
17163 {
17164 /* Check for the DW_AT_byte_size attribute. */
17165 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17166 if (attr)
17167 {
17168 length = DW_UNSND (attr);
17169 }
17170 else
17171 {
17172 length = 1;
17173 }
17174 }
17175
17176 index_type = objfile_type (objfile)->builtin_int;
17177 range_type = create_static_range_type (NULL, index_type, 1, length);
17178 char_type = language_string_char_type (cu->language_defn, gdbarch);
17179 type = create_string_type (NULL, char_type, range_type);
17180
17181 return set_die_type (die, type, cu);
17182 }
17183
17184 /* Assuming that DIE corresponds to a function, returns nonzero
17185 if the function is prototyped. */
17186
17187 static int
17188 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17189 {
17190 struct attribute *attr;
17191
17192 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17193 if (attr && (DW_UNSND (attr) != 0))
17194 return 1;
17195
17196 /* The DWARF standard implies that the DW_AT_prototyped attribute
17197 is only meaninful for C, but the concept also extends to other
17198 languages that allow unprototyped functions (Eg: Objective C).
17199 For all other languages, assume that functions are always
17200 prototyped. */
17201 if (cu->language != language_c
17202 && cu->language != language_objc
17203 && cu->language != language_opencl)
17204 return 1;
17205
17206 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17207 prototyped and unprototyped functions; default to prototyped,
17208 since that is more common in modern code (and RealView warns
17209 about unprototyped functions). */
17210 if (producer_is_realview (cu->producer))
17211 return 1;
17212
17213 return 0;
17214 }
17215
17216 /* Handle DIES due to C code like:
17217
17218 struct foo
17219 {
17220 int (*funcp)(int a, long l);
17221 int b;
17222 };
17223
17224 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17225
17226 static struct type *
17227 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17228 {
17229 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17230 struct type *type; /* Type that this function returns. */
17231 struct type *ftype; /* Function that returns above type. */
17232 struct attribute *attr;
17233
17234 type = die_type (die, cu);
17235
17236 /* The die_type call above may have already set the type for this DIE. */
17237 ftype = get_die_type (die, cu);
17238 if (ftype)
17239 return ftype;
17240
17241 ftype = lookup_function_type (type);
17242
17243 if (prototyped_function_p (die, cu))
17244 TYPE_PROTOTYPED (ftype) = 1;
17245
17246 /* Store the calling convention in the type if it's available in
17247 the subroutine die. Otherwise set the calling convention to
17248 the default value DW_CC_normal. */
17249 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17250 if (attr)
17251 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17252 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17253 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17254 else
17255 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17256
17257 /* Record whether the function returns normally to its caller or not
17258 if the DWARF producer set that information. */
17259 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17260 if (attr && (DW_UNSND (attr) != 0))
17261 TYPE_NO_RETURN (ftype) = 1;
17262
17263 /* We need to add the subroutine type to the die immediately so
17264 we don't infinitely recurse when dealing with parameters
17265 declared as the same subroutine type. */
17266 set_die_type (die, ftype, cu);
17267
17268 if (die->child != NULL)
17269 {
17270 struct type *void_type = objfile_type (objfile)->builtin_void;
17271 struct die_info *child_die;
17272 int nparams, iparams;
17273
17274 /* Count the number of parameters.
17275 FIXME: GDB currently ignores vararg functions, but knows about
17276 vararg member functions. */
17277 nparams = 0;
17278 child_die = die->child;
17279 while (child_die && child_die->tag)
17280 {
17281 if (child_die->tag == DW_TAG_formal_parameter)
17282 nparams++;
17283 else if (child_die->tag == DW_TAG_unspecified_parameters)
17284 TYPE_VARARGS (ftype) = 1;
17285 child_die = sibling_die (child_die);
17286 }
17287
17288 /* Allocate storage for parameters and fill them in. */
17289 TYPE_NFIELDS (ftype) = nparams;
17290 TYPE_FIELDS (ftype) = (struct field *)
17291 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
17292
17293 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17294 even if we error out during the parameters reading below. */
17295 for (iparams = 0; iparams < nparams; iparams++)
17296 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17297
17298 iparams = 0;
17299 child_die = die->child;
17300 while (child_die && child_die->tag)
17301 {
17302 if (child_die->tag == DW_TAG_formal_parameter)
17303 {
17304 struct type *arg_type;
17305
17306 /* DWARF version 2 has no clean way to discern C++
17307 static and non-static member functions. G++ helps
17308 GDB by marking the first parameter for non-static
17309 member functions (which is the this pointer) as
17310 artificial. We pass this information to
17311 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17312
17313 DWARF version 3 added DW_AT_object_pointer, which GCC
17314 4.5 does not yet generate. */
17315 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17316 if (attr)
17317 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17318 else
17319 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17320 arg_type = die_type (child_die, cu);
17321
17322 /* RealView does not mark THIS as const, which the testsuite
17323 expects. GCC marks THIS as const in method definitions,
17324 but not in the class specifications (GCC PR 43053). */
17325 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17326 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17327 {
17328 int is_this = 0;
17329 struct dwarf2_cu *arg_cu = cu;
17330 const char *name = dwarf2_name (child_die, cu);
17331
17332 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17333 if (attr)
17334 {
17335 /* If the compiler emits this, use it. */
17336 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17337 is_this = 1;
17338 }
17339 else if (name && strcmp (name, "this") == 0)
17340 /* Function definitions will have the argument names. */
17341 is_this = 1;
17342 else if (name == NULL && iparams == 0)
17343 /* Declarations may not have the names, so like
17344 elsewhere in GDB, assume an artificial first
17345 argument is "this". */
17346 is_this = 1;
17347
17348 if (is_this)
17349 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17350 arg_type, 0);
17351 }
17352
17353 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17354 iparams++;
17355 }
17356 child_die = sibling_die (child_die);
17357 }
17358 }
17359
17360 return ftype;
17361 }
17362
17363 static struct type *
17364 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17365 {
17366 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17367 const char *name = NULL;
17368 struct type *this_type, *target_type;
17369
17370 name = dwarf2_full_name (NULL, die, cu);
17371 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17372 TYPE_TARGET_STUB (this_type) = 1;
17373 set_die_type (die, this_type, cu);
17374 target_type = die_type (die, cu);
17375 if (target_type != this_type)
17376 TYPE_TARGET_TYPE (this_type) = target_type;
17377 else
17378 {
17379 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17380 spec and cause infinite loops in GDB. */
17381 complaint (&symfile_complaints,
17382 _("Self-referential DW_TAG_typedef "
17383 "- DIE at %s [in module %s]"),
17384 sect_offset_str (die->sect_off), objfile_name (objfile));
17385 TYPE_TARGET_TYPE (this_type) = NULL;
17386 }
17387 return this_type;
17388 }
17389
17390 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17391 (which may be different from NAME) to the architecture back-end to allow
17392 it to guess the correct format if necessary. */
17393
17394 static struct type *
17395 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17396 const char *name_hint)
17397 {
17398 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17399 const struct floatformat **format;
17400 struct type *type;
17401
17402 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17403 if (format)
17404 type = init_float_type (objfile, bits, name, format);
17405 else
17406 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17407
17408 return type;
17409 }
17410
17411 /* Find a representation of a given base type and install
17412 it in the TYPE field of the die. */
17413
17414 static struct type *
17415 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17416 {
17417 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17418 struct type *type;
17419 struct attribute *attr;
17420 int encoding = 0, bits = 0;
17421 const char *name;
17422
17423 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17424 if (attr)
17425 {
17426 encoding = DW_UNSND (attr);
17427 }
17428 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17429 if (attr)
17430 {
17431 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17432 }
17433 name = dwarf2_name (die, cu);
17434 if (!name)
17435 {
17436 complaint (&symfile_complaints,
17437 _("DW_AT_name missing from DW_TAG_base_type"));
17438 }
17439
17440 switch (encoding)
17441 {
17442 case DW_ATE_address:
17443 /* Turn DW_ATE_address into a void * pointer. */
17444 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17445 type = init_pointer_type (objfile, bits, name, type);
17446 break;
17447 case DW_ATE_boolean:
17448 type = init_boolean_type (objfile, bits, 1, name);
17449 break;
17450 case DW_ATE_complex_float:
17451 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
17452 type = init_complex_type (objfile, name, type);
17453 break;
17454 case DW_ATE_decimal_float:
17455 type = init_decfloat_type (objfile, bits, name);
17456 break;
17457 case DW_ATE_float:
17458 type = dwarf2_init_float_type (objfile, bits, name, name);
17459 break;
17460 case DW_ATE_signed:
17461 type = init_integer_type (objfile, bits, 0, name);
17462 break;
17463 case DW_ATE_unsigned:
17464 if (cu->language == language_fortran
17465 && name
17466 && startswith (name, "character("))
17467 type = init_character_type (objfile, bits, 1, name);
17468 else
17469 type = init_integer_type (objfile, bits, 1, name);
17470 break;
17471 case DW_ATE_signed_char:
17472 if (cu->language == language_ada || cu->language == language_m2
17473 || cu->language == language_pascal
17474 || cu->language == language_fortran)
17475 type = init_character_type (objfile, bits, 0, name);
17476 else
17477 type = init_integer_type (objfile, bits, 0, name);
17478 break;
17479 case DW_ATE_unsigned_char:
17480 if (cu->language == language_ada || cu->language == language_m2
17481 || cu->language == language_pascal
17482 || cu->language == language_fortran
17483 || cu->language == language_rust)
17484 type = init_character_type (objfile, bits, 1, name);
17485 else
17486 type = init_integer_type (objfile, bits, 1, name);
17487 break;
17488 case DW_ATE_UTF:
17489 {
17490 gdbarch *arch = get_objfile_arch (objfile);
17491
17492 if (bits == 16)
17493 type = builtin_type (arch)->builtin_char16;
17494 else if (bits == 32)
17495 type = builtin_type (arch)->builtin_char32;
17496 else
17497 {
17498 complaint (&symfile_complaints,
17499 _("unsupported DW_ATE_UTF bit size: '%d'"),
17500 bits);
17501 type = init_integer_type (objfile, bits, 1, name);
17502 }
17503 return set_die_type (die, type, cu);
17504 }
17505 break;
17506
17507 default:
17508 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
17509 dwarf_type_encoding_name (encoding));
17510 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17511 break;
17512 }
17513
17514 if (name && strcmp (name, "char") == 0)
17515 TYPE_NOSIGN (type) = 1;
17516
17517 maybe_set_alignment (cu, die, type);
17518
17519 return set_die_type (die, type, cu);
17520 }
17521
17522 /* Parse dwarf attribute if it's a block, reference or constant and put the
17523 resulting value of the attribute into struct bound_prop.
17524 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17525
17526 static int
17527 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17528 struct dwarf2_cu *cu, struct dynamic_prop *prop)
17529 {
17530 struct dwarf2_property_baton *baton;
17531 struct obstack *obstack
17532 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17533
17534 if (attr == NULL || prop == NULL)
17535 return 0;
17536
17537 if (attr_form_is_block (attr))
17538 {
17539 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17540 baton->referenced_type = NULL;
17541 baton->locexpr.per_cu = cu->per_cu;
17542 baton->locexpr.size = DW_BLOCK (attr)->size;
17543 baton->locexpr.data = DW_BLOCK (attr)->data;
17544 prop->data.baton = baton;
17545 prop->kind = PROP_LOCEXPR;
17546 gdb_assert (prop->data.baton != NULL);
17547 }
17548 else if (attr_form_is_ref (attr))
17549 {
17550 struct dwarf2_cu *target_cu = cu;
17551 struct die_info *target_die;
17552 struct attribute *target_attr;
17553
17554 target_die = follow_die_ref (die, attr, &target_cu);
17555 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17556 if (target_attr == NULL)
17557 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17558 target_cu);
17559 if (target_attr == NULL)
17560 return 0;
17561
17562 switch (target_attr->name)
17563 {
17564 case DW_AT_location:
17565 if (attr_form_is_section_offset (target_attr))
17566 {
17567 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17568 baton->referenced_type = die_type (target_die, target_cu);
17569 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17570 prop->data.baton = baton;
17571 prop->kind = PROP_LOCLIST;
17572 gdb_assert (prop->data.baton != NULL);
17573 }
17574 else if (attr_form_is_block (target_attr))
17575 {
17576 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17577 baton->referenced_type = die_type (target_die, target_cu);
17578 baton->locexpr.per_cu = cu->per_cu;
17579 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17580 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17581 prop->data.baton = baton;
17582 prop->kind = PROP_LOCEXPR;
17583 gdb_assert (prop->data.baton != NULL);
17584 }
17585 else
17586 {
17587 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17588 "dynamic property");
17589 return 0;
17590 }
17591 break;
17592 case DW_AT_data_member_location:
17593 {
17594 LONGEST offset;
17595
17596 if (!handle_data_member_location (target_die, target_cu,
17597 &offset))
17598 return 0;
17599
17600 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17601 baton->referenced_type = read_type_die (target_die->parent,
17602 target_cu);
17603 baton->offset_info.offset = offset;
17604 baton->offset_info.type = die_type (target_die, target_cu);
17605 prop->data.baton = baton;
17606 prop->kind = PROP_ADDR_OFFSET;
17607 break;
17608 }
17609 }
17610 }
17611 else if (attr_form_is_constant (attr))
17612 {
17613 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17614 prop->kind = PROP_CONST;
17615 }
17616 else
17617 {
17618 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17619 dwarf2_name (die, cu));
17620 return 0;
17621 }
17622
17623 return 1;
17624 }
17625
17626 /* Read the given DW_AT_subrange DIE. */
17627
17628 static struct type *
17629 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17630 {
17631 struct type *base_type, *orig_base_type;
17632 struct type *range_type;
17633 struct attribute *attr;
17634 struct dynamic_prop low, high;
17635 int low_default_is_valid;
17636 int high_bound_is_count = 0;
17637 const char *name;
17638 LONGEST negative_mask;
17639
17640 orig_base_type = die_type (die, cu);
17641 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17642 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17643 creating the range type, but we use the result of check_typedef
17644 when examining properties of the type. */
17645 base_type = check_typedef (orig_base_type);
17646
17647 /* The die_type call above may have already set the type for this DIE. */
17648 range_type = get_die_type (die, cu);
17649 if (range_type)
17650 return range_type;
17651
17652 low.kind = PROP_CONST;
17653 high.kind = PROP_CONST;
17654 high.data.const_val = 0;
17655
17656 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17657 omitting DW_AT_lower_bound. */
17658 switch (cu->language)
17659 {
17660 case language_c:
17661 case language_cplus:
17662 low.data.const_val = 0;
17663 low_default_is_valid = 1;
17664 break;
17665 case language_fortran:
17666 low.data.const_val = 1;
17667 low_default_is_valid = 1;
17668 break;
17669 case language_d:
17670 case language_objc:
17671 case language_rust:
17672 low.data.const_val = 0;
17673 low_default_is_valid = (cu->header.version >= 4);
17674 break;
17675 case language_ada:
17676 case language_m2:
17677 case language_pascal:
17678 low.data.const_val = 1;
17679 low_default_is_valid = (cu->header.version >= 4);
17680 break;
17681 default:
17682 low.data.const_val = 0;
17683 low_default_is_valid = 0;
17684 break;
17685 }
17686
17687 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17688 if (attr)
17689 attr_to_dynamic_prop (attr, die, cu, &low);
17690 else if (!low_default_is_valid)
17691 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
17692 "- DIE at %s [in module %s]"),
17693 sect_offset_str (die->sect_off),
17694 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17695
17696 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
17697 if (!attr_to_dynamic_prop (attr, die, cu, &high))
17698 {
17699 attr = dwarf2_attr (die, DW_AT_count, cu);
17700 if (attr_to_dynamic_prop (attr, die, cu, &high))
17701 {
17702 /* If bounds are constant do the final calculation here. */
17703 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17704 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17705 else
17706 high_bound_is_count = 1;
17707 }
17708 }
17709
17710 /* Dwarf-2 specifications explicitly allows to create subrange types
17711 without specifying a base type.
17712 In that case, the base type must be set to the type of
17713 the lower bound, upper bound or count, in that order, if any of these
17714 three attributes references an object that has a type.
17715 If no base type is found, the Dwarf-2 specifications say that
17716 a signed integer type of size equal to the size of an address should
17717 be used.
17718 For the following C code: `extern char gdb_int [];'
17719 GCC produces an empty range DIE.
17720 FIXME: muller/2010-05-28: Possible references to object for low bound,
17721 high bound or count are not yet handled by this code. */
17722 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
17723 {
17724 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17725 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17726 int addr_size = gdbarch_addr_bit (gdbarch) /8;
17727 struct type *int_type = objfile_type (objfile)->builtin_int;
17728
17729 /* Test "int", "long int", and "long long int" objfile types,
17730 and select the first one having a size above or equal to the
17731 architecture address size. */
17732 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17733 base_type = int_type;
17734 else
17735 {
17736 int_type = objfile_type (objfile)->builtin_long;
17737 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17738 base_type = int_type;
17739 else
17740 {
17741 int_type = objfile_type (objfile)->builtin_long_long;
17742 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17743 base_type = int_type;
17744 }
17745 }
17746 }
17747
17748 /* Normally, the DWARF producers are expected to use a signed
17749 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17750 But this is unfortunately not always the case, as witnessed
17751 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17752 is used instead. To work around that ambiguity, we treat
17753 the bounds as signed, and thus sign-extend their values, when
17754 the base type is signed. */
17755 negative_mask =
17756 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17757 if (low.kind == PROP_CONST
17758 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17759 low.data.const_val |= negative_mask;
17760 if (high.kind == PROP_CONST
17761 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17762 high.data.const_val |= negative_mask;
17763
17764 range_type = create_range_type (NULL, orig_base_type, &low, &high);
17765
17766 if (high_bound_is_count)
17767 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17768
17769 /* Ada expects an empty array on no boundary attributes. */
17770 if (attr == NULL && cu->language != language_ada)
17771 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17772
17773 name = dwarf2_name (die, cu);
17774 if (name)
17775 TYPE_NAME (range_type) = name;
17776
17777 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17778 if (attr)
17779 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17780
17781 maybe_set_alignment (cu, die, range_type);
17782
17783 set_die_type (die, range_type, cu);
17784
17785 /* set_die_type should be already done. */
17786 set_descriptive_type (range_type, die, cu);
17787
17788 return range_type;
17789 }
17790
17791 static struct type *
17792 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17793 {
17794 struct type *type;
17795
17796 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17797 NULL);
17798 TYPE_NAME (type) = dwarf2_name (die, cu);
17799
17800 /* In Ada, an unspecified type is typically used when the description
17801 of the type is defered to a different unit. When encountering
17802 such a type, we treat it as a stub, and try to resolve it later on,
17803 when needed. */
17804 if (cu->language == language_ada)
17805 TYPE_STUB (type) = 1;
17806
17807 return set_die_type (die, type, cu);
17808 }
17809
17810 /* Read a single die and all its descendents. Set the die's sibling
17811 field to NULL; set other fields in the die correctly, and set all
17812 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17813 location of the info_ptr after reading all of those dies. PARENT
17814 is the parent of the die in question. */
17815
17816 static struct die_info *
17817 read_die_and_children (const struct die_reader_specs *reader,
17818 const gdb_byte *info_ptr,
17819 const gdb_byte **new_info_ptr,
17820 struct die_info *parent)
17821 {
17822 struct die_info *die;
17823 const gdb_byte *cur_ptr;
17824 int has_children;
17825
17826 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
17827 if (die == NULL)
17828 {
17829 *new_info_ptr = cur_ptr;
17830 return NULL;
17831 }
17832 store_in_ref_table (die, reader->cu);
17833
17834 if (has_children)
17835 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17836 else
17837 {
17838 die->child = NULL;
17839 *new_info_ptr = cur_ptr;
17840 }
17841
17842 die->sibling = NULL;
17843 die->parent = parent;
17844 return die;
17845 }
17846
17847 /* Read a die, all of its descendents, and all of its siblings; set
17848 all of the fields of all of the dies correctly. Arguments are as
17849 in read_die_and_children. */
17850
17851 static struct die_info *
17852 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17853 const gdb_byte *info_ptr,
17854 const gdb_byte **new_info_ptr,
17855 struct die_info *parent)
17856 {
17857 struct die_info *first_die, *last_sibling;
17858 const gdb_byte *cur_ptr;
17859
17860 cur_ptr = info_ptr;
17861 first_die = last_sibling = NULL;
17862
17863 while (1)
17864 {
17865 struct die_info *die
17866 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17867
17868 if (die == NULL)
17869 {
17870 *new_info_ptr = cur_ptr;
17871 return first_die;
17872 }
17873
17874 if (!first_die)
17875 first_die = die;
17876 else
17877 last_sibling->sibling = die;
17878
17879 last_sibling = die;
17880 }
17881 }
17882
17883 /* Read a die, all of its descendents, and all of its siblings; set
17884 all of the fields of all of the dies correctly. Arguments are as
17885 in read_die_and_children.
17886 This the main entry point for reading a DIE and all its children. */
17887
17888 static struct die_info *
17889 read_die_and_siblings (const struct die_reader_specs *reader,
17890 const gdb_byte *info_ptr,
17891 const gdb_byte **new_info_ptr,
17892 struct die_info *parent)
17893 {
17894 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17895 new_info_ptr, parent);
17896
17897 if (dwarf_die_debug)
17898 {
17899 fprintf_unfiltered (gdb_stdlog,
17900 "Read die from %s@0x%x of %s:\n",
17901 get_section_name (reader->die_section),
17902 (unsigned) (info_ptr - reader->die_section->buffer),
17903 bfd_get_filename (reader->abfd));
17904 dump_die (die, dwarf_die_debug);
17905 }
17906
17907 return die;
17908 }
17909
17910 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17911 attributes.
17912 The caller is responsible for filling in the extra attributes
17913 and updating (*DIEP)->num_attrs.
17914 Set DIEP to point to a newly allocated die with its information,
17915 except for its child, sibling, and parent fields.
17916 Set HAS_CHILDREN to tell whether the die has children or not. */
17917
17918 static const gdb_byte *
17919 read_full_die_1 (const struct die_reader_specs *reader,
17920 struct die_info **diep, const gdb_byte *info_ptr,
17921 int *has_children, int num_extra_attrs)
17922 {
17923 unsigned int abbrev_number, bytes_read, i;
17924 struct abbrev_info *abbrev;
17925 struct die_info *die;
17926 struct dwarf2_cu *cu = reader->cu;
17927 bfd *abfd = reader->abfd;
17928
17929 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17930 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17931 info_ptr += bytes_read;
17932 if (!abbrev_number)
17933 {
17934 *diep = NULL;
17935 *has_children = 0;
17936 return info_ptr;
17937 }
17938
17939 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
17940 if (!abbrev)
17941 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17942 abbrev_number,
17943 bfd_get_filename (abfd));
17944
17945 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
17946 die->sect_off = sect_off;
17947 die->tag = abbrev->tag;
17948 die->abbrev = abbrev_number;
17949
17950 /* Make the result usable.
17951 The caller needs to update num_attrs after adding the extra
17952 attributes. */
17953 die->num_attrs = abbrev->num_attrs;
17954
17955 for (i = 0; i < abbrev->num_attrs; ++i)
17956 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17957 info_ptr);
17958
17959 *diep = die;
17960 *has_children = abbrev->has_children;
17961 return info_ptr;
17962 }
17963
17964 /* Read a die and all its attributes.
17965 Set DIEP to point to a newly allocated die with its information,
17966 except for its child, sibling, and parent fields.
17967 Set HAS_CHILDREN to tell whether the die has children or not. */
17968
17969 static const gdb_byte *
17970 read_full_die (const struct die_reader_specs *reader,
17971 struct die_info **diep, const gdb_byte *info_ptr,
17972 int *has_children)
17973 {
17974 const gdb_byte *result;
17975
17976 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
17977
17978 if (dwarf_die_debug)
17979 {
17980 fprintf_unfiltered (gdb_stdlog,
17981 "Read die from %s@0x%x of %s:\n",
17982 get_section_name (reader->die_section),
17983 (unsigned) (info_ptr - reader->die_section->buffer),
17984 bfd_get_filename (reader->abfd));
17985 dump_die (*diep, dwarf_die_debug);
17986 }
17987
17988 return result;
17989 }
17990 \f
17991 /* Abbreviation tables.
17992
17993 In DWARF version 2, the description of the debugging information is
17994 stored in a separate .debug_abbrev section. Before we read any
17995 dies from a section we read in all abbreviations and install them
17996 in a hash table. */
17997
17998 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
17999
18000 struct abbrev_info *
18001 abbrev_table::alloc_abbrev ()
18002 {
18003 struct abbrev_info *abbrev;
18004
18005 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
18006 memset (abbrev, 0, sizeof (struct abbrev_info));
18007
18008 return abbrev;
18009 }
18010
18011 /* Add an abbreviation to the table. */
18012
18013 void
18014 abbrev_table::add_abbrev (unsigned int abbrev_number,
18015 struct abbrev_info *abbrev)
18016 {
18017 unsigned int hash_number;
18018
18019 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18020 abbrev->next = m_abbrevs[hash_number];
18021 m_abbrevs[hash_number] = abbrev;
18022 }
18023
18024 /* Look up an abbrev in the table.
18025 Returns NULL if the abbrev is not found. */
18026
18027 struct abbrev_info *
18028 abbrev_table::lookup_abbrev (unsigned int abbrev_number)
18029 {
18030 unsigned int hash_number;
18031 struct abbrev_info *abbrev;
18032
18033 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18034 abbrev = m_abbrevs[hash_number];
18035
18036 while (abbrev)
18037 {
18038 if (abbrev->number == abbrev_number)
18039 return abbrev;
18040 abbrev = abbrev->next;
18041 }
18042 return NULL;
18043 }
18044
18045 /* Read in an abbrev table. */
18046
18047 static abbrev_table_up
18048 abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18049 struct dwarf2_section_info *section,
18050 sect_offset sect_off)
18051 {
18052 struct objfile *objfile = dwarf2_per_objfile->objfile;
18053 bfd *abfd = get_section_bfd_owner (section);
18054 const gdb_byte *abbrev_ptr;
18055 struct abbrev_info *cur_abbrev;
18056 unsigned int abbrev_number, bytes_read, abbrev_name;
18057 unsigned int abbrev_form;
18058 struct attr_abbrev *cur_attrs;
18059 unsigned int allocated_attrs;
18060
18061 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
18062
18063 dwarf2_read_section (objfile, section);
18064 abbrev_ptr = section->buffer + to_underlying (sect_off);
18065 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18066 abbrev_ptr += bytes_read;
18067
18068 allocated_attrs = ATTR_ALLOC_CHUNK;
18069 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
18070
18071 /* Loop until we reach an abbrev number of 0. */
18072 while (abbrev_number)
18073 {
18074 cur_abbrev = abbrev_table->alloc_abbrev ();
18075
18076 /* read in abbrev header */
18077 cur_abbrev->number = abbrev_number;
18078 cur_abbrev->tag
18079 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18080 abbrev_ptr += bytes_read;
18081 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18082 abbrev_ptr += 1;
18083
18084 /* now read in declarations */
18085 for (;;)
18086 {
18087 LONGEST implicit_const;
18088
18089 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18090 abbrev_ptr += bytes_read;
18091 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18092 abbrev_ptr += bytes_read;
18093 if (abbrev_form == DW_FORM_implicit_const)
18094 {
18095 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18096 &bytes_read);
18097 abbrev_ptr += bytes_read;
18098 }
18099 else
18100 {
18101 /* Initialize it due to a false compiler warning. */
18102 implicit_const = -1;
18103 }
18104
18105 if (abbrev_name == 0)
18106 break;
18107
18108 if (cur_abbrev->num_attrs == allocated_attrs)
18109 {
18110 allocated_attrs += ATTR_ALLOC_CHUNK;
18111 cur_attrs
18112 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
18113 }
18114
18115 cur_attrs[cur_abbrev->num_attrs].name
18116 = (enum dwarf_attribute) abbrev_name;
18117 cur_attrs[cur_abbrev->num_attrs].form
18118 = (enum dwarf_form) abbrev_form;
18119 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
18120 ++cur_abbrev->num_attrs;
18121 }
18122
18123 cur_abbrev->attrs =
18124 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18125 cur_abbrev->num_attrs);
18126 memcpy (cur_abbrev->attrs, cur_attrs,
18127 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18128
18129 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
18130
18131 /* Get next abbreviation.
18132 Under Irix6 the abbreviations for a compilation unit are not
18133 always properly terminated with an abbrev number of 0.
18134 Exit loop if we encounter an abbreviation which we have
18135 already read (which means we are about to read the abbreviations
18136 for the next compile unit) or if the end of the abbreviation
18137 table is reached. */
18138 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
18139 break;
18140 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18141 abbrev_ptr += bytes_read;
18142 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
18143 break;
18144 }
18145
18146 xfree (cur_attrs);
18147 return abbrev_table;
18148 }
18149
18150 /* Returns nonzero if TAG represents a type that we might generate a partial
18151 symbol for. */
18152
18153 static int
18154 is_type_tag_for_partial (int tag)
18155 {
18156 switch (tag)
18157 {
18158 #if 0
18159 /* Some types that would be reasonable to generate partial symbols for,
18160 that we don't at present. */
18161 case DW_TAG_array_type:
18162 case DW_TAG_file_type:
18163 case DW_TAG_ptr_to_member_type:
18164 case DW_TAG_set_type:
18165 case DW_TAG_string_type:
18166 case DW_TAG_subroutine_type:
18167 #endif
18168 case DW_TAG_base_type:
18169 case DW_TAG_class_type:
18170 case DW_TAG_interface_type:
18171 case DW_TAG_enumeration_type:
18172 case DW_TAG_structure_type:
18173 case DW_TAG_subrange_type:
18174 case DW_TAG_typedef:
18175 case DW_TAG_union_type:
18176 return 1;
18177 default:
18178 return 0;
18179 }
18180 }
18181
18182 /* Load all DIEs that are interesting for partial symbols into memory. */
18183
18184 static struct partial_die_info *
18185 load_partial_dies (const struct die_reader_specs *reader,
18186 const gdb_byte *info_ptr, int building_psymtab)
18187 {
18188 struct dwarf2_cu *cu = reader->cu;
18189 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18190 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18191 unsigned int bytes_read;
18192 unsigned int load_all = 0;
18193 int nesting_level = 1;
18194
18195 parent_die = NULL;
18196 last_die = NULL;
18197
18198 gdb_assert (cu->per_cu != NULL);
18199 if (cu->per_cu->load_all_dies)
18200 load_all = 1;
18201
18202 cu->partial_dies
18203 = htab_create_alloc_ex (cu->header.length / 12,
18204 partial_die_hash,
18205 partial_die_eq,
18206 NULL,
18207 &cu->comp_unit_obstack,
18208 hashtab_obstack_allocate,
18209 dummy_obstack_deallocate);
18210
18211 while (1)
18212 {
18213 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18214
18215 /* A NULL abbrev means the end of a series of children. */
18216 if (abbrev == NULL)
18217 {
18218 if (--nesting_level == 0)
18219 return first_die;
18220
18221 info_ptr += bytes_read;
18222 last_die = parent_die;
18223 parent_die = parent_die->die_parent;
18224 continue;
18225 }
18226
18227 /* Check for template arguments. We never save these; if
18228 they're seen, we just mark the parent, and go on our way. */
18229 if (parent_die != NULL
18230 && cu->language == language_cplus
18231 && (abbrev->tag == DW_TAG_template_type_param
18232 || abbrev->tag == DW_TAG_template_value_param))
18233 {
18234 parent_die->has_template_arguments = 1;
18235
18236 if (!load_all)
18237 {
18238 /* We don't need a partial DIE for the template argument. */
18239 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18240 continue;
18241 }
18242 }
18243
18244 /* We only recurse into c++ subprograms looking for template arguments.
18245 Skip their other children. */
18246 if (!load_all
18247 && cu->language == language_cplus
18248 && parent_die != NULL
18249 && parent_die->tag == DW_TAG_subprogram)
18250 {
18251 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18252 continue;
18253 }
18254
18255 /* Check whether this DIE is interesting enough to save. Normally
18256 we would not be interested in members here, but there may be
18257 later variables referencing them via DW_AT_specification (for
18258 static members). */
18259 if (!load_all
18260 && !is_type_tag_for_partial (abbrev->tag)
18261 && abbrev->tag != DW_TAG_constant
18262 && abbrev->tag != DW_TAG_enumerator
18263 && abbrev->tag != DW_TAG_subprogram
18264 && abbrev->tag != DW_TAG_inlined_subroutine
18265 && abbrev->tag != DW_TAG_lexical_block
18266 && abbrev->tag != DW_TAG_variable
18267 && abbrev->tag != DW_TAG_namespace
18268 && abbrev->tag != DW_TAG_module
18269 && abbrev->tag != DW_TAG_member
18270 && abbrev->tag != DW_TAG_imported_unit
18271 && abbrev->tag != DW_TAG_imported_declaration)
18272 {
18273 /* Otherwise we skip to the next sibling, if any. */
18274 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18275 continue;
18276 }
18277
18278 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18279 abbrev);
18280
18281 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18282
18283 /* This two-pass algorithm for processing partial symbols has a
18284 high cost in cache pressure. Thus, handle some simple cases
18285 here which cover the majority of C partial symbols. DIEs
18286 which neither have specification tags in them, nor could have
18287 specification tags elsewhere pointing at them, can simply be
18288 processed and discarded.
18289
18290 This segment is also optional; scan_partial_symbols and
18291 add_partial_symbol will handle these DIEs if we chain
18292 them in normally. When compilers which do not emit large
18293 quantities of duplicate debug information are more common,
18294 this code can probably be removed. */
18295
18296 /* Any complete simple types at the top level (pretty much all
18297 of them, for a language without namespaces), can be processed
18298 directly. */
18299 if (parent_die == NULL
18300 && pdi.has_specification == 0
18301 && pdi.is_declaration == 0
18302 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18303 || pdi.tag == DW_TAG_base_type
18304 || pdi.tag == DW_TAG_subrange_type))
18305 {
18306 if (building_psymtab && pdi.name != NULL)
18307 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
18308 VAR_DOMAIN, LOC_TYPEDEF,
18309 &objfile->static_psymbols,
18310 0, cu->language, objfile);
18311 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18312 continue;
18313 }
18314
18315 /* The exception for DW_TAG_typedef with has_children above is
18316 a workaround of GCC PR debug/47510. In the case of this complaint
18317 type_name_no_tag_or_error will error on such types later.
18318
18319 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18320 it could not find the child DIEs referenced later, this is checked
18321 above. In correct DWARF DW_TAG_typedef should have no children. */
18322
18323 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18324 complaint (&symfile_complaints,
18325 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18326 "- DIE at %s [in module %s]"),
18327 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18328
18329 /* If we're at the second level, and we're an enumerator, and
18330 our parent has no specification (meaning possibly lives in a
18331 namespace elsewhere), then we can add the partial symbol now
18332 instead of queueing it. */
18333 if (pdi.tag == DW_TAG_enumerator
18334 && parent_die != NULL
18335 && parent_die->die_parent == NULL
18336 && parent_die->tag == DW_TAG_enumeration_type
18337 && parent_die->has_specification == 0)
18338 {
18339 if (pdi.name == NULL)
18340 complaint (&symfile_complaints,
18341 _("malformed enumerator DIE ignored"));
18342 else if (building_psymtab)
18343 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
18344 VAR_DOMAIN, LOC_CONST,
18345 cu->language == language_cplus
18346 ? &objfile->global_psymbols
18347 : &objfile->static_psymbols,
18348 0, cu->language, objfile);
18349
18350 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18351 continue;
18352 }
18353
18354 struct partial_die_info *part_die
18355 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18356
18357 /* We'll save this DIE so link it in. */
18358 part_die->die_parent = parent_die;
18359 part_die->die_sibling = NULL;
18360 part_die->die_child = NULL;
18361
18362 if (last_die && last_die == parent_die)
18363 last_die->die_child = part_die;
18364 else if (last_die)
18365 last_die->die_sibling = part_die;
18366
18367 last_die = part_die;
18368
18369 if (first_die == NULL)
18370 first_die = part_die;
18371
18372 /* Maybe add the DIE to the hash table. Not all DIEs that we
18373 find interesting need to be in the hash table, because we
18374 also have the parent/sibling/child chains; only those that we
18375 might refer to by offset later during partial symbol reading.
18376
18377 For now this means things that might have be the target of a
18378 DW_AT_specification, DW_AT_abstract_origin, or
18379 DW_AT_extension. DW_AT_extension will refer only to
18380 namespaces; DW_AT_abstract_origin refers to functions (and
18381 many things under the function DIE, but we do not recurse
18382 into function DIEs during partial symbol reading) and
18383 possibly variables as well; DW_AT_specification refers to
18384 declarations. Declarations ought to have the DW_AT_declaration
18385 flag. It happens that GCC forgets to put it in sometimes, but
18386 only for functions, not for types.
18387
18388 Adding more things than necessary to the hash table is harmless
18389 except for the performance cost. Adding too few will result in
18390 wasted time in find_partial_die, when we reread the compilation
18391 unit with load_all_dies set. */
18392
18393 if (load_all
18394 || abbrev->tag == DW_TAG_constant
18395 || abbrev->tag == DW_TAG_subprogram
18396 || abbrev->tag == DW_TAG_variable
18397 || abbrev->tag == DW_TAG_namespace
18398 || part_die->is_declaration)
18399 {
18400 void **slot;
18401
18402 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18403 to_underlying (part_die->sect_off),
18404 INSERT);
18405 *slot = part_die;
18406 }
18407
18408 /* For some DIEs we want to follow their children (if any). For C
18409 we have no reason to follow the children of structures; for other
18410 languages we have to, so that we can get at method physnames
18411 to infer fully qualified class names, for DW_AT_specification,
18412 and for C++ template arguments. For C++, we also look one level
18413 inside functions to find template arguments (if the name of the
18414 function does not already contain the template arguments).
18415
18416 For Ada, we need to scan the children of subprograms and lexical
18417 blocks as well because Ada allows the definition of nested
18418 entities that could be interesting for the debugger, such as
18419 nested subprograms for instance. */
18420 if (last_die->has_children
18421 && (load_all
18422 || last_die->tag == DW_TAG_namespace
18423 || last_die->tag == DW_TAG_module
18424 || last_die->tag == DW_TAG_enumeration_type
18425 || (cu->language == language_cplus
18426 && last_die->tag == DW_TAG_subprogram
18427 && (last_die->name == NULL
18428 || strchr (last_die->name, '<') == NULL))
18429 || (cu->language != language_c
18430 && (last_die->tag == DW_TAG_class_type
18431 || last_die->tag == DW_TAG_interface_type
18432 || last_die->tag == DW_TAG_structure_type
18433 || last_die->tag == DW_TAG_union_type))
18434 || (cu->language == language_ada
18435 && (last_die->tag == DW_TAG_subprogram
18436 || last_die->tag == DW_TAG_lexical_block))))
18437 {
18438 nesting_level++;
18439 parent_die = last_die;
18440 continue;
18441 }
18442
18443 /* Otherwise we skip to the next sibling, if any. */
18444 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18445
18446 /* Back to the top, do it again. */
18447 }
18448 }
18449
18450 partial_die_info::partial_die_info (sect_offset sect_off_,
18451 struct abbrev_info *abbrev)
18452 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18453 {
18454 }
18455
18456 /* Read a minimal amount of information into the minimal die structure.
18457 INFO_PTR should point just after the initial uleb128 of a DIE. */
18458
18459 const gdb_byte *
18460 partial_die_info::read (const struct die_reader_specs *reader,
18461 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18462 {
18463 struct dwarf2_cu *cu = reader->cu;
18464 struct dwarf2_per_objfile *dwarf2_per_objfile
18465 = cu->per_cu->dwarf2_per_objfile;
18466 unsigned int i;
18467 int has_low_pc_attr = 0;
18468 int has_high_pc_attr = 0;
18469 int high_pc_relative = 0;
18470
18471 for (i = 0; i < abbrev.num_attrs; ++i)
18472 {
18473 struct attribute attr;
18474
18475 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
18476
18477 /* Store the data if it is of an attribute we want to keep in a
18478 partial symbol table. */
18479 switch (attr.name)
18480 {
18481 case DW_AT_name:
18482 switch (tag)
18483 {
18484 case DW_TAG_compile_unit:
18485 case DW_TAG_partial_unit:
18486 case DW_TAG_type_unit:
18487 /* Compilation units have a DW_AT_name that is a filename, not
18488 a source language identifier. */
18489 case DW_TAG_enumeration_type:
18490 case DW_TAG_enumerator:
18491 /* These tags always have simple identifiers already; no need
18492 to canonicalize them. */
18493 name = DW_STRING (&attr);
18494 break;
18495 default:
18496 {
18497 struct objfile *objfile = dwarf2_per_objfile->objfile;
18498
18499 name
18500 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18501 &objfile->per_bfd->storage_obstack);
18502 }
18503 break;
18504 }
18505 break;
18506 case DW_AT_linkage_name:
18507 case DW_AT_MIPS_linkage_name:
18508 /* Note that both forms of linkage name might appear. We
18509 assume they will be the same, and we only store the last
18510 one we see. */
18511 if (cu->language == language_ada)
18512 name = DW_STRING (&attr);
18513 linkage_name = DW_STRING (&attr);
18514 break;
18515 case DW_AT_low_pc:
18516 has_low_pc_attr = 1;
18517 lowpc = attr_value_as_address (&attr);
18518 break;
18519 case DW_AT_high_pc:
18520 has_high_pc_attr = 1;
18521 highpc = attr_value_as_address (&attr);
18522 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18523 high_pc_relative = 1;
18524 break;
18525 case DW_AT_location:
18526 /* Support the .debug_loc offsets. */
18527 if (attr_form_is_block (&attr))
18528 {
18529 d.locdesc = DW_BLOCK (&attr);
18530 }
18531 else if (attr_form_is_section_offset (&attr))
18532 {
18533 dwarf2_complex_location_expr_complaint ();
18534 }
18535 else
18536 {
18537 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18538 "partial symbol information");
18539 }
18540 break;
18541 case DW_AT_external:
18542 is_external = DW_UNSND (&attr);
18543 break;
18544 case DW_AT_declaration:
18545 is_declaration = DW_UNSND (&attr);
18546 break;
18547 case DW_AT_type:
18548 has_type = 1;
18549 break;
18550 case DW_AT_abstract_origin:
18551 case DW_AT_specification:
18552 case DW_AT_extension:
18553 has_specification = 1;
18554 spec_offset = dwarf2_get_ref_die_offset (&attr);
18555 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18556 || cu->per_cu->is_dwz);
18557 break;
18558 case DW_AT_sibling:
18559 /* Ignore absolute siblings, they might point outside of
18560 the current compile unit. */
18561 if (attr.form == DW_FORM_ref_addr)
18562 complaint (&symfile_complaints,
18563 _("ignoring absolute DW_AT_sibling"));
18564 else
18565 {
18566 const gdb_byte *buffer = reader->buffer;
18567 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18568 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18569
18570 if (sibling_ptr < info_ptr)
18571 complaint (&symfile_complaints,
18572 _("DW_AT_sibling points backwards"));
18573 else if (sibling_ptr > reader->buffer_end)
18574 dwarf2_section_buffer_overflow_complaint (reader->die_section);
18575 else
18576 sibling = sibling_ptr;
18577 }
18578 break;
18579 case DW_AT_byte_size:
18580 has_byte_size = 1;
18581 break;
18582 case DW_AT_const_value:
18583 has_const_value = 1;
18584 break;
18585 case DW_AT_calling_convention:
18586 /* DWARF doesn't provide a way to identify a program's source-level
18587 entry point. DW_AT_calling_convention attributes are only meant
18588 to describe functions' calling conventions.
18589
18590 However, because it's a necessary piece of information in
18591 Fortran, and before DWARF 4 DW_CC_program was the only
18592 piece of debugging information whose definition refers to
18593 a 'main program' at all, several compilers marked Fortran
18594 main programs with DW_CC_program --- even when those
18595 functions use the standard calling conventions.
18596
18597 Although DWARF now specifies a way to provide this
18598 information, we support this practice for backward
18599 compatibility. */
18600 if (DW_UNSND (&attr) == DW_CC_program
18601 && cu->language == language_fortran)
18602 main_subprogram = 1;
18603 break;
18604 case DW_AT_inline:
18605 if (DW_UNSND (&attr) == DW_INL_inlined
18606 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18607 may_be_inlined = 1;
18608 break;
18609
18610 case DW_AT_import:
18611 if (tag == DW_TAG_imported_unit)
18612 {
18613 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18614 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18615 || cu->per_cu->is_dwz);
18616 }
18617 break;
18618
18619 case DW_AT_main_subprogram:
18620 main_subprogram = DW_UNSND (&attr);
18621 break;
18622
18623 default:
18624 break;
18625 }
18626 }
18627
18628 if (high_pc_relative)
18629 highpc += lowpc;
18630
18631 if (has_low_pc_attr && has_high_pc_attr)
18632 {
18633 /* When using the GNU linker, .gnu.linkonce. sections are used to
18634 eliminate duplicate copies of functions and vtables and such.
18635 The linker will arbitrarily choose one and discard the others.
18636 The AT_*_pc values for such functions refer to local labels in
18637 these sections. If the section from that file was discarded, the
18638 labels are not in the output, so the relocs get a value of 0.
18639 If this is a discarded function, mark the pc bounds as invalid,
18640 so that GDB will ignore it. */
18641 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18642 {
18643 struct objfile *objfile = dwarf2_per_objfile->objfile;
18644 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18645
18646 complaint (&symfile_complaints,
18647 _("DW_AT_low_pc %s is zero "
18648 "for DIE at %s [in module %s]"),
18649 paddress (gdbarch, lowpc),
18650 sect_offset_str (sect_off),
18651 objfile_name (objfile));
18652 }
18653 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18654 else if (lowpc >= highpc)
18655 {
18656 struct objfile *objfile = dwarf2_per_objfile->objfile;
18657 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18658
18659 complaint (&symfile_complaints,
18660 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18661 "for DIE at %s [in module %s]"),
18662 paddress (gdbarch, lowpc),
18663 paddress (gdbarch, highpc),
18664 sect_offset_str (sect_off),
18665 objfile_name (objfile));
18666 }
18667 else
18668 has_pc_info = 1;
18669 }
18670
18671 return info_ptr;
18672 }
18673
18674 /* Find a cached partial DIE at OFFSET in CU. */
18675
18676 struct partial_die_info *
18677 dwarf2_cu::find_partial_die (sect_offset sect_off)
18678 {
18679 struct partial_die_info *lookup_die = NULL;
18680 struct partial_die_info part_die (sect_off);
18681
18682 lookup_die = ((struct partial_die_info *)
18683 htab_find_with_hash (partial_dies, &part_die,
18684 to_underlying (sect_off)));
18685
18686 return lookup_die;
18687 }
18688
18689 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18690 except in the case of .debug_types DIEs which do not reference
18691 outside their CU (they do however referencing other types via
18692 DW_FORM_ref_sig8). */
18693
18694 static struct partial_die_info *
18695 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18696 {
18697 struct dwarf2_per_objfile *dwarf2_per_objfile
18698 = cu->per_cu->dwarf2_per_objfile;
18699 struct objfile *objfile = dwarf2_per_objfile->objfile;
18700 struct dwarf2_per_cu_data *per_cu = NULL;
18701 struct partial_die_info *pd = NULL;
18702
18703 if (offset_in_dwz == cu->per_cu->is_dwz
18704 && offset_in_cu_p (&cu->header, sect_off))
18705 {
18706 pd = cu->find_partial_die (sect_off);
18707 if (pd != NULL)
18708 return pd;
18709 /* We missed recording what we needed.
18710 Load all dies and try again. */
18711 per_cu = cu->per_cu;
18712 }
18713 else
18714 {
18715 /* TUs don't reference other CUs/TUs (except via type signatures). */
18716 if (cu->per_cu->is_debug_types)
18717 {
18718 error (_("Dwarf Error: Type Unit at offset %s contains"
18719 " external reference to offset %s [in module %s].\n"),
18720 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18721 bfd_get_filename (objfile->obfd));
18722 }
18723 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18724 dwarf2_per_objfile);
18725
18726 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18727 load_partial_comp_unit (per_cu);
18728
18729 per_cu->cu->last_used = 0;
18730 pd = per_cu->cu->find_partial_die (sect_off);
18731 }
18732
18733 /* If we didn't find it, and not all dies have been loaded,
18734 load them all and try again. */
18735
18736 if (pd == NULL && per_cu->load_all_dies == 0)
18737 {
18738 per_cu->load_all_dies = 1;
18739
18740 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18741 THIS_CU->cu may already be in use. So we can't just free it and
18742 replace its DIEs with the ones we read in. Instead, we leave those
18743 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18744 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18745 set. */
18746 load_partial_comp_unit (per_cu);
18747
18748 pd = per_cu->cu->find_partial_die (sect_off);
18749 }
18750
18751 if (pd == NULL)
18752 internal_error (__FILE__, __LINE__,
18753 _("could not find partial DIE %s "
18754 "in cache [from module %s]\n"),
18755 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18756 return pd;
18757 }
18758
18759 /* See if we can figure out if the class lives in a namespace. We do
18760 this by looking for a member function; its demangled name will
18761 contain namespace info, if there is any. */
18762
18763 static void
18764 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18765 struct dwarf2_cu *cu)
18766 {
18767 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18768 what template types look like, because the demangler
18769 frequently doesn't give the same name as the debug info. We
18770 could fix this by only using the demangled name to get the
18771 prefix (but see comment in read_structure_type). */
18772
18773 struct partial_die_info *real_pdi;
18774 struct partial_die_info *child_pdi;
18775
18776 /* If this DIE (this DIE's specification, if any) has a parent, then
18777 we should not do this. We'll prepend the parent's fully qualified
18778 name when we create the partial symbol. */
18779
18780 real_pdi = struct_pdi;
18781 while (real_pdi->has_specification)
18782 real_pdi = find_partial_die (real_pdi->spec_offset,
18783 real_pdi->spec_is_dwz, cu);
18784
18785 if (real_pdi->die_parent != NULL)
18786 return;
18787
18788 for (child_pdi = struct_pdi->die_child;
18789 child_pdi != NULL;
18790 child_pdi = child_pdi->die_sibling)
18791 {
18792 if (child_pdi->tag == DW_TAG_subprogram
18793 && child_pdi->linkage_name != NULL)
18794 {
18795 char *actual_class_name
18796 = language_class_name_from_physname (cu->language_defn,
18797 child_pdi->linkage_name);
18798 if (actual_class_name != NULL)
18799 {
18800 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18801 struct_pdi->name
18802 = ((const char *)
18803 obstack_copy0 (&objfile->per_bfd->storage_obstack,
18804 actual_class_name,
18805 strlen (actual_class_name)));
18806 xfree (actual_class_name);
18807 }
18808 break;
18809 }
18810 }
18811 }
18812
18813 void
18814 partial_die_info::fixup (struct dwarf2_cu *cu)
18815 {
18816 /* Once we've fixed up a die, there's no point in doing so again.
18817 This also avoids a memory leak if we were to call
18818 guess_partial_die_structure_name multiple times. */
18819 if (fixup_called)
18820 return;
18821
18822 /* If we found a reference attribute and the DIE has no name, try
18823 to find a name in the referred to DIE. */
18824
18825 if (name == NULL && has_specification)
18826 {
18827 struct partial_die_info *spec_die;
18828
18829 spec_die = find_partial_die (spec_offset, spec_is_dwz, cu);
18830
18831 spec_die->fixup (cu);
18832
18833 if (spec_die->name)
18834 {
18835 name = spec_die->name;
18836
18837 /* Copy DW_AT_external attribute if it is set. */
18838 if (spec_die->is_external)
18839 is_external = spec_die->is_external;
18840 }
18841 }
18842
18843 /* Set default names for some unnamed DIEs. */
18844
18845 if (name == NULL && tag == DW_TAG_namespace)
18846 name = CP_ANONYMOUS_NAMESPACE_STR;
18847
18848 /* If there is no parent die to provide a namespace, and there are
18849 children, see if we can determine the namespace from their linkage
18850 name. */
18851 if (cu->language == language_cplus
18852 && !VEC_empty (dwarf2_section_info_def,
18853 cu->per_cu->dwarf2_per_objfile->types)
18854 && die_parent == NULL
18855 && has_children
18856 && (tag == DW_TAG_class_type
18857 || tag == DW_TAG_structure_type
18858 || tag == DW_TAG_union_type))
18859 guess_partial_die_structure_name (this, cu);
18860
18861 /* GCC might emit a nameless struct or union that has a linkage
18862 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18863 if (name == NULL
18864 && (tag == DW_TAG_class_type
18865 || tag == DW_TAG_interface_type
18866 || tag == DW_TAG_structure_type
18867 || tag == DW_TAG_union_type)
18868 && linkage_name != NULL)
18869 {
18870 char *demangled;
18871
18872 demangled = gdb_demangle (linkage_name, DMGL_TYPES);
18873 if (demangled)
18874 {
18875 const char *base;
18876
18877 /* Strip any leading namespaces/classes, keep only the base name.
18878 DW_AT_name for named DIEs does not contain the prefixes. */
18879 base = strrchr (demangled, ':');
18880 if (base && base > demangled && base[-1] == ':')
18881 base++;
18882 else
18883 base = demangled;
18884
18885 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18886 name
18887 = ((const char *)
18888 obstack_copy0 (&objfile->per_bfd->storage_obstack,
18889 base, strlen (base)));
18890 xfree (demangled);
18891 }
18892 }
18893
18894 fixup_called = 1;
18895 }
18896
18897 /* Read an attribute value described by an attribute form. */
18898
18899 static const gdb_byte *
18900 read_attribute_value (const struct die_reader_specs *reader,
18901 struct attribute *attr, unsigned form,
18902 LONGEST implicit_const, const gdb_byte *info_ptr)
18903 {
18904 struct dwarf2_cu *cu = reader->cu;
18905 struct dwarf2_per_objfile *dwarf2_per_objfile
18906 = cu->per_cu->dwarf2_per_objfile;
18907 struct objfile *objfile = dwarf2_per_objfile->objfile;
18908 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18909 bfd *abfd = reader->abfd;
18910 struct comp_unit_head *cu_header = &cu->header;
18911 unsigned int bytes_read;
18912 struct dwarf_block *blk;
18913
18914 attr->form = (enum dwarf_form) form;
18915 switch (form)
18916 {
18917 case DW_FORM_ref_addr:
18918 if (cu->header.version == 2)
18919 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
18920 else
18921 DW_UNSND (attr) = read_offset (abfd, info_ptr,
18922 &cu->header, &bytes_read);
18923 info_ptr += bytes_read;
18924 break;
18925 case DW_FORM_GNU_ref_alt:
18926 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18927 info_ptr += bytes_read;
18928 break;
18929 case DW_FORM_addr:
18930 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
18931 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
18932 info_ptr += bytes_read;
18933 break;
18934 case DW_FORM_block2:
18935 blk = dwarf_alloc_block (cu);
18936 blk->size = read_2_bytes (abfd, info_ptr);
18937 info_ptr += 2;
18938 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18939 info_ptr += blk->size;
18940 DW_BLOCK (attr) = blk;
18941 break;
18942 case DW_FORM_block4:
18943 blk = dwarf_alloc_block (cu);
18944 blk->size = read_4_bytes (abfd, info_ptr);
18945 info_ptr += 4;
18946 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18947 info_ptr += blk->size;
18948 DW_BLOCK (attr) = blk;
18949 break;
18950 case DW_FORM_data2:
18951 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18952 info_ptr += 2;
18953 break;
18954 case DW_FORM_data4:
18955 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18956 info_ptr += 4;
18957 break;
18958 case DW_FORM_data8:
18959 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18960 info_ptr += 8;
18961 break;
18962 case DW_FORM_data16:
18963 blk = dwarf_alloc_block (cu);
18964 blk->size = 16;
18965 blk->data = read_n_bytes (abfd, info_ptr, 16);
18966 info_ptr += 16;
18967 DW_BLOCK (attr) = blk;
18968 break;
18969 case DW_FORM_sec_offset:
18970 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18971 info_ptr += bytes_read;
18972 break;
18973 case DW_FORM_string:
18974 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
18975 DW_STRING_IS_CANONICAL (attr) = 0;
18976 info_ptr += bytes_read;
18977 break;
18978 case DW_FORM_strp:
18979 if (!cu->per_cu->is_dwz)
18980 {
18981 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18982 abfd, info_ptr, cu_header,
18983 &bytes_read);
18984 DW_STRING_IS_CANONICAL (attr) = 0;
18985 info_ptr += bytes_read;
18986 break;
18987 }
18988 /* FALLTHROUGH */
18989 case DW_FORM_line_strp:
18990 if (!cu->per_cu->is_dwz)
18991 {
18992 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
18993 abfd, info_ptr,
18994 cu_header, &bytes_read);
18995 DW_STRING_IS_CANONICAL (attr) = 0;
18996 info_ptr += bytes_read;
18997 break;
18998 }
18999 /* FALLTHROUGH */
19000 case DW_FORM_GNU_strp_alt:
19001 {
19002 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19003 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19004 &bytes_read);
19005
19006 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19007 dwz, str_offset);
19008 DW_STRING_IS_CANONICAL (attr) = 0;
19009 info_ptr += bytes_read;
19010 }
19011 break;
19012 case DW_FORM_exprloc:
19013 case DW_FORM_block:
19014 blk = dwarf_alloc_block (cu);
19015 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19016 info_ptr += bytes_read;
19017 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19018 info_ptr += blk->size;
19019 DW_BLOCK (attr) = blk;
19020 break;
19021 case DW_FORM_block1:
19022 blk = dwarf_alloc_block (cu);
19023 blk->size = read_1_byte (abfd, info_ptr);
19024 info_ptr += 1;
19025 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19026 info_ptr += blk->size;
19027 DW_BLOCK (attr) = blk;
19028 break;
19029 case DW_FORM_data1:
19030 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19031 info_ptr += 1;
19032 break;
19033 case DW_FORM_flag:
19034 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19035 info_ptr += 1;
19036 break;
19037 case DW_FORM_flag_present:
19038 DW_UNSND (attr) = 1;
19039 break;
19040 case DW_FORM_sdata:
19041 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19042 info_ptr += bytes_read;
19043 break;
19044 case DW_FORM_udata:
19045 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19046 info_ptr += bytes_read;
19047 break;
19048 case DW_FORM_ref1:
19049 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19050 + read_1_byte (abfd, info_ptr));
19051 info_ptr += 1;
19052 break;
19053 case DW_FORM_ref2:
19054 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19055 + read_2_bytes (abfd, info_ptr));
19056 info_ptr += 2;
19057 break;
19058 case DW_FORM_ref4:
19059 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19060 + read_4_bytes (abfd, info_ptr));
19061 info_ptr += 4;
19062 break;
19063 case DW_FORM_ref8:
19064 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19065 + read_8_bytes (abfd, info_ptr));
19066 info_ptr += 8;
19067 break;
19068 case DW_FORM_ref_sig8:
19069 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19070 info_ptr += 8;
19071 break;
19072 case DW_FORM_ref_udata:
19073 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19074 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19075 info_ptr += bytes_read;
19076 break;
19077 case DW_FORM_indirect:
19078 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19079 info_ptr += bytes_read;
19080 if (form == DW_FORM_implicit_const)
19081 {
19082 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19083 info_ptr += bytes_read;
19084 }
19085 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19086 info_ptr);
19087 break;
19088 case DW_FORM_implicit_const:
19089 DW_SND (attr) = implicit_const;
19090 break;
19091 case DW_FORM_GNU_addr_index:
19092 if (reader->dwo_file == NULL)
19093 {
19094 /* For now flag a hard error.
19095 Later we can turn this into a complaint. */
19096 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19097 dwarf_form_name (form),
19098 bfd_get_filename (abfd));
19099 }
19100 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19101 info_ptr += bytes_read;
19102 break;
19103 case DW_FORM_GNU_str_index:
19104 if (reader->dwo_file == NULL)
19105 {
19106 /* For now flag a hard error.
19107 Later we can turn this into a complaint if warranted. */
19108 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19109 dwarf_form_name (form),
19110 bfd_get_filename (abfd));
19111 }
19112 {
19113 ULONGEST str_index =
19114 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19115
19116 DW_STRING (attr) = read_str_index (reader, str_index);
19117 DW_STRING_IS_CANONICAL (attr) = 0;
19118 info_ptr += bytes_read;
19119 }
19120 break;
19121 default:
19122 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19123 dwarf_form_name (form),
19124 bfd_get_filename (abfd));
19125 }
19126
19127 /* Super hack. */
19128 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
19129 attr->form = DW_FORM_GNU_ref_alt;
19130
19131 /* We have seen instances where the compiler tried to emit a byte
19132 size attribute of -1 which ended up being encoded as an unsigned
19133 0xffffffff. Although 0xffffffff is technically a valid size value,
19134 an object of this size seems pretty unlikely so we can relatively
19135 safely treat these cases as if the size attribute was invalid and
19136 treat them as zero by default. */
19137 if (attr->name == DW_AT_byte_size
19138 && form == DW_FORM_data4
19139 && DW_UNSND (attr) >= 0xffffffff)
19140 {
19141 complaint
19142 (&symfile_complaints,
19143 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19144 hex_string (DW_UNSND (attr)));
19145 DW_UNSND (attr) = 0;
19146 }
19147
19148 return info_ptr;
19149 }
19150
19151 /* Read an attribute described by an abbreviated attribute. */
19152
19153 static const gdb_byte *
19154 read_attribute (const struct die_reader_specs *reader,
19155 struct attribute *attr, struct attr_abbrev *abbrev,
19156 const gdb_byte *info_ptr)
19157 {
19158 attr->name = abbrev->name;
19159 return read_attribute_value (reader, attr, abbrev->form,
19160 abbrev->implicit_const, info_ptr);
19161 }
19162
19163 /* Read dwarf information from a buffer. */
19164
19165 static unsigned int
19166 read_1_byte (bfd *abfd, const gdb_byte *buf)
19167 {
19168 return bfd_get_8 (abfd, buf);
19169 }
19170
19171 static int
19172 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
19173 {
19174 return bfd_get_signed_8 (abfd, buf);
19175 }
19176
19177 static unsigned int
19178 read_2_bytes (bfd *abfd, const gdb_byte *buf)
19179 {
19180 return bfd_get_16 (abfd, buf);
19181 }
19182
19183 static int
19184 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
19185 {
19186 return bfd_get_signed_16 (abfd, buf);
19187 }
19188
19189 static unsigned int
19190 read_4_bytes (bfd *abfd, const gdb_byte *buf)
19191 {
19192 return bfd_get_32 (abfd, buf);
19193 }
19194
19195 static int
19196 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
19197 {
19198 return bfd_get_signed_32 (abfd, buf);
19199 }
19200
19201 static ULONGEST
19202 read_8_bytes (bfd *abfd, const gdb_byte *buf)
19203 {
19204 return bfd_get_64 (abfd, buf);
19205 }
19206
19207 static CORE_ADDR
19208 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
19209 unsigned int *bytes_read)
19210 {
19211 struct comp_unit_head *cu_header = &cu->header;
19212 CORE_ADDR retval = 0;
19213
19214 if (cu_header->signed_addr_p)
19215 {
19216 switch (cu_header->addr_size)
19217 {
19218 case 2:
19219 retval = bfd_get_signed_16 (abfd, buf);
19220 break;
19221 case 4:
19222 retval = bfd_get_signed_32 (abfd, buf);
19223 break;
19224 case 8:
19225 retval = bfd_get_signed_64 (abfd, buf);
19226 break;
19227 default:
19228 internal_error (__FILE__, __LINE__,
19229 _("read_address: bad switch, signed [in module %s]"),
19230 bfd_get_filename (abfd));
19231 }
19232 }
19233 else
19234 {
19235 switch (cu_header->addr_size)
19236 {
19237 case 2:
19238 retval = bfd_get_16 (abfd, buf);
19239 break;
19240 case 4:
19241 retval = bfd_get_32 (abfd, buf);
19242 break;
19243 case 8:
19244 retval = bfd_get_64 (abfd, buf);
19245 break;
19246 default:
19247 internal_error (__FILE__, __LINE__,
19248 _("read_address: bad switch, "
19249 "unsigned [in module %s]"),
19250 bfd_get_filename (abfd));
19251 }
19252 }
19253
19254 *bytes_read = cu_header->addr_size;
19255 return retval;
19256 }
19257
19258 /* Read the initial length from a section. The (draft) DWARF 3
19259 specification allows the initial length to take up either 4 bytes
19260 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19261 bytes describe the length and all offsets will be 8 bytes in length
19262 instead of 4.
19263
19264 An older, non-standard 64-bit format is also handled by this
19265 function. The older format in question stores the initial length
19266 as an 8-byte quantity without an escape value. Lengths greater
19267 than 2^32 aren't very common which means that the initial 4 bytes
19268 is almost always zero. Since a length value of zero doesn't make
19269 sense for the 32-bit format, this initial zero can be considered to
19270 be an escape value which indicates the presence of the older 64-bit
19271 format. As written, the code can't detect (old format) lengths
19272 greater than 4GB. If it becomes necessary to handle lengths
19273 somewhat larger than 4GB, we could allow other small values (such
19274 as the non-sensical values of 1, 2, and 3) to also be used as
19275 escape values indicating the presence of the old format.
19276
19277 The value returned via bytes_read should be used to increment the
19278 relevant pointer after calling read_initial_length().
19279
19280 [ Note: read_initial_length() and read_offset() are based on the
19281 document entitled "DWARF Debugging Information Format", revision
19282 3, draft 8, dated November 19, 2001. This document was obtained
19283 from:
19284
19285 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
19286
19287 This document is only a draft and is subject to change. (So beware.)
19288
19289 Details regarding the older, non-standard 64-bit format were
19290 determined empirically by examining 64-bit ELF files produced by
19291 the SGI toolchain on an IRIX 6.5 machine.
19292
19293 - Kevin, July 16, 2002
19294 ] */
19295
19296 static LONGEST
19297 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
19298 {
19299 LONGEST length = bfd_get_32 (abfd, buf);
19300
19301 if (length == 0xffffffff)
19302 {
19303 length = bfd_get_64 (abfd, buf + 4);
19304 *bytes_read = 12;
19305 }
19306 else if (length == 0)
19307 {
19308 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
19309 length = bfd_get_64 (abfd, buf);
19310 *bytes_read = 8;
19311 }
19312 else
19313 {
19314 *bytes_read = 4;
19315 }
19316
19317 return length;
19318 }
19319
19320 /* Cover function for read_initial_length.
19321 Returns the length of the object at BUF, and stores the size of the
19322 initial length in *BYTES_READ and stores the size that offsets will be in
19323 *OFFSET_SIZE.
19324 If the initial length size is not equivalent to that specified in
19325 CU_HEADER then issue a complaint.
19326 This is useful when reading non-comp-unit headers. */
19327
19328 static LONGEST
19329 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
19330 const struct comp_unit_head *cu_header,
19331 unsigned int *bytes_read,
19332 unsigned int *offset_size)
19333 {
19334 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19335
19336 gdb_assert (cu_header->initial_length_size == 4
19337 || cu_header->initial_length_size == 8
19338 || cu_header->initial_length_size == 12);
19339
19340 if (cu_header->initial_length_size != *bytes_read)
19341 complaint (&symfile_complaints,
19342 _("intermixed 32-bit and 64-bit DWARF sections"));
19343
19344 *offset_size = (*bytes_read == 4) ? 4 : 8;
19345 return length;
19346 }
19347
19348 /* Read an offset from the data stream. The size of the offset is
19349 given by cu_header->offset_size. */
19350
19351 static LONGEST
19352 read_offset (bfd *abfd, const gdb_byte *buf,
19353 const struct comp_unit_head *cu_header,
19354 unsigned int *bytes_read)
19355 {
19356 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
19357
19358 *bytes_read = cu_header->offset_size;
19359 return offset;
19360 }
19361
19362 /* Read an offset from the data stream. */
19363
19364 static LONGEST
19365 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
19366 {
19367 LONGEST retval = 0;
19368
19369 switch (offset_size)
19370 {
19371 case 4:
19372 retval = bfd_get_32 (abfd, buf);
19373 break;
19374 case 8:
19375 retval = bfd_get_64 (abfd, buf);
19376 break;
19377 default:
19378 internal_error (__FILE__, __LINE__,
19379 _("read_offset_1: bad switch [in module %s]"),
19380 bfd_get_filename (abfd));
19381 }
19382
19383 return retval;
19384 }
19385
19386 static const gdb_byte *
19387 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
19388 {
19389 /* If the size of a host char is 8 bits, we can return a pointer
19390 to the buffer, otherwise we have to copy the data to a buffer
19391 allocated on the temporary obstack. */
19392 gdb_assert (HOST_CHAR_BIT == 8);
19393 return buf;
19394 }
19395
19396 static const char *
19397 read_direct_string (bfd *abfd, const gdb_byte *buf,
19398 unsigned int *bytes_read_ptr)
19399 {
19400 /* If the size of a host char is 8 bits, we can return a pointer
19401 to the string, otherwise we have to copy the string to a buffer
19402 allocated on the temporary obstack. */
19403 gdb_assert (HOST_CHAR_BIT == 8);
19404 if (*buf == '\0')
19405 {
19406 *bytes_read_ptr = 1;
19407 return NULL;
19408 }
19409 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19410 return (const char *) buf;
19411 }
19412
19413 /* Return pointer to string at section SECT offset STR_OFFSET with error
19414 reporting strings FORM_NAME and SECT_NAME. */
19415
19416 static const char *
19417 read_indirect_string_at_offset_from (struct objfile *objfile,
19418 bfd *abfd, LONGEST str_offset,
19419 struct dwarf2_section_info *sect,
19420 const char *form_name,
19421 const char *sect_name)
19422 {
19423 dwarf2_read_section (objfile, sect);
19424 if (sect->buffer == NULL)
19425 error (_("%s used without %s section [in module %s]"),
19426 form_name, sect_name, bfd_get_filename (abfd));
19427 if (str_offset >= sect->size)
19428 error (_("%s pointing outside of %s section [in module %s]"),
19429 form_name, sect_name, bfd_get_filename (abfd));
19430 gdb_assert (HOST_CHAR_BIT == 8);
19431 if (sect->buffer[str_offset] == '\0')
19432 return NULL;
19433 return (const char *) (sect->buffer + str_offset);
19434 }
19435
19436 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19437
19438 static const char *
19439 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19440 bfd *abfd, LONGEST str_offset)
19441 {
19442 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19443 abfd, str_offset,
19444 &dwarf2_per_objfile->str,
19445 "DW_FORM_strp", ".debug_str");
19446 }
19447
19448 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19449
19450 static const char *
19451 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19452 bfd *abfd, LONGEST str_offset)
19453 {
19454 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19455 abfd, str_offset,
19456 &dwarf2_per_objfile->line_str,
19457 "DW_FORM_line_strp",
19458 ".debug_line_str");
19459 }
19460
19461 /* Read a string at offset STR_OFFSET in the .debug_str section from
19462 the .dwz file DWZ. Throw an error if the offset is too large. If
19463 the string consists of a single NUL byte, return NULL; otherwise
19464 return a pointer to the string. */
19465
19466 static const char *
19467 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19468 LONGEST str_offset)
19469 {
19470 dwarf2_read_section (objfile, &dwz->str);
19471
19472 if (dwz->str.buffer == NULL)
19473 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19474 "section [in module %s]"),
19475 bfd_get_filename (dwz->dwz_bfd));
19476 if (str_offset >= dwz->str.size)
19477 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19478 ".debug_str section [in module %s]"),
19479 bfd_get_filename (dwz->dwz_bfd));
19480 gdb_assert (HOST_CHAR_BIT == 8);
19481 if (dwz->str.buffer[str_offset] == '\0')
19482 return NULL;
19483 return (const char *) (dwz->str.buffer + str_offset);
19484 }
19485
19486 /* Return pointer to string at .debug_str offset as read from BUF.
19487 BUF is assumed to be in a compilation unit described by CU_HEADER.
19488 Return *BYTES_READ_PTR count of bytes read from BUF. */
19489
19490 static const char *
19491 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19492 const gdb_byte *buf,
19493 const struct comp_unit_head *cu_header,
19494 unsigned int *bytes_read_ptr)
19495 {
19496 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19497
19498 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
19499 }
19500
19501 /* Return pointer to string at .debug_line_str offset as read from BUF.
19502 BUF is assumed to be in a compilation unit described by CU_HEADER.
19503 Return *BYTES_READ_PTR count of bytes read from BUF. */
19504
19505 static const char *
19506 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19507 bfd *abfd, const gdb_byte *buf,
19508 const struct comp_unit_head *cu_header,
19509 unsigned int *bytes_read_ptr)
19510 {
19511 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19512
19513 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19514 str_offset);
19515 }
19516
19517 ULONGEST
19518 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
19519 unsigned int *bytes_read_ptr)
19520 {
19521 ULONGEST result;
19522 unsigned int num_read;
19523 int shift;
19524 unsigned char byte;
19525
19526 result = 0;
19527 shift = 0;
19528 num_read = 0;
19529 while (1)
19530 {
19531 byte = bfd_get_8 (abfd, buf);
19532 buf++;
19533 num_read++;
19534 result |= ((ULONGEST) (byte & 127) << shift);
19535 if ((byte & 128) == 0)
19536 {
19537 break;
19538 }
19539 shift += 7;
19540 }
19541 *bytes_read_ptr = num_read;
19542 return result;
19543 }
19544
19545 static LONGEST
19546 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
19547 unsigned int *bytes_read_ptr)
19548 {
19549 LONGEST result;
19550 int shift, num_read;
19551 unsigned char byte;
19552
19553 result = 0;
19554 shift = 0;
19555 num_read = 0;
19556 while (1)
19557 {
19558 byte = bfd_get_8 (abfd, buf);
19559 buf++;
19560 num_read++;
19561 result |= ((LONGEST) (byte & 127) << shift);
19562 shift += 7;
19563 if ((byte & 128) == 0)
19564 {
19565 break;
19566 }
19567 }
19568 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
19569 result |= -(((LONGEST) 1) << shift);
19570 *bytes_read_ptr = num_read;
19571 return result;
19572 }
19573
19574 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19575 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
19576 ADDR_SIZE is the size of addresses from the CU header. */
19577
19578 static CORE_ADDR
19579 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19580 unsigned int addr_index, ULONGEST addr_base, int addr_size)
19581 {
19582 struct objfile *objfile = dwarf2_per_objfile->objfile;
19583 bfd *abfd = objfile->obfd;
19584 const gdb_byte *info_ptr;
19585
19586 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
19587 if (dwarf2_per_objfile->addr.buffer == NULL)
19588 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19589 objfile_name (objfile));
19590 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
19591 error (_("DW_FORM_addr_index pointing outside of "
19592 ".debug_addr section [in module %s]"),
19593 objfile_name (objfile));
19594 info_ptr = (dwarf2_per_objfile->addr.buffer
19595 + addr_base + addr_index * addr_size);
19596 if (addr_size == 4)
19597 return bfd_get_32 (abfd, info_ptr);
19598 else
19599 return bfd_get_64 (abfd, info_ptr);
19600 }
19601
19602 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19603
19604 static CORE_ADDR
19605 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19606 {
19607 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19608 cu->addr_base, cu->header.addr_size);
19609 }
19610
19611 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19612
19613 static CORE_ADDR
19614 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19615 unsigned int *bytes_read)
19616 {
19617 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
19618 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19619
19620 return read_addr_index (cu, addr_index);
19621 }
19622
19623 /* Data structure to pass results from dwarf2_read_addr_index_reader
19624 back to dwarf2_read_addr_index. */
19625
19626 struct dwarf2_read_addr_index_data
19627 {
19628 ULONGEST addr_base;
19629 int addr_size;
19630 };
19631
19632 /* die_reader_func for dwarf2_read_addr_index. */
19633
19634 static void
19635 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
19636 const gdb_byte *info_ptr,
19637 struct die_info *comp_unit_die,
19638 int has_children,
19639 void *data)
19640 {
19641 struct dwarf2_cu *cu = reader->cu;
19642 struct dwarf2_read_addr_index_data *aidata =
19643 (struct dwarf2_read_addr_index_data *) data;
19644
19645 aidata->addr_base = cu->addr_base;
19646 aidata->addr_size = cu->header.addr_size;
19647 }
19648
19649 /* Given an index in .debug_addr, fetch the value.
19650 NOTE: This can be called during dwarf expression evaluation,
19651 long after the debug information has been read, and thus per_cu->cu
19652 may no longer exist. */
19653
19654 CORE_ADDR
19655 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19656 unsigned int addr_index)
19657 {
19658 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
19659 struct dwarf2_cu *cu = per_cu->cu;
19660 ULONGEST addr_base;
19661 int addr_size;
19662
19663 /* We need addr_base and addr_size.
19664 If we don't have PER_CU->cu, we have to get it.
19665 Nasty, but the alternative is storing the needed info in PER_CU,
19666 which at this point doesn't seem justified: it's not clear how frequently
19667 it would get used and it would increase the size of every PER_CU.
19668 Entry points like dwarf2_per_cu_addr_size do a similar thing
19669 so we're not in uncharted territory here.
19670 Alas we need to be a bit more complicated as addr_base is contained
19671 in the DIE.
19672
19673 We don't need to read the entire CU(/TU).
19674 We just need the header and top level die.
19675
19676 IWBN to use the aging mechanism to let us lazily later discard the CU.
19677 For now we skip this optimization. */
19678
19679 if (cu != NULL)
19680 {
19681 addr_base = cu->addr_base;
19682 addr_size = cu->header.addr_size;
19683 }
19684 else
19685 {
19686 struct dwarf2_read_addr_index_data aidata;
19687
19688 /* Note: We can't use init_cutu_and_read_dies_simple here,
19689 we need addr_base. */
19690 init_cutu_and_read_dies (per_cu, NULL, 0, 0, false,
19691 dwarf2_read_addr_index_reader, &aidata);
19692 addr_base = aidata.addr_base;
19693 addr_size = aidata.addr_size;
19694 }
19695
19696 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19697 addr_size);
19698 }
19699
19700 /* Given a DW_FORM_GNU_str_index, fetch the string.
19701 This is only used by the Fission support. */
19702
19703 static const char *
19704 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19705 {
19706 struct dwarf2_cu *cu = reader->cu;
19707 struct dwarf2_per_objfile *dwarf2_per_objfile
19708 = cu->per_cu->dwarf2_per_objfile;
19709 struct objfile *objfile = dwarf2_per_objfile->objfile;
19710 const char *objf_name = objfile_name (objfile);
19711 bfd *abfd = objfile->obfd;
19712 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
19713 struct dwarf2_section_info *str_offsets_section =
19714 &reader->dwo_file->sections.str_offsets;
19715 const gdb_byte *info_ptr;
19716 ULONGEST str_offset;
19717 static const char form_name[] = "DW_FORM_GNU_str_index";
19718
19719 dwarf2_read_section (objfile, str_section);
19720 dwarf2_read_section (objfile, str_offsets_section);
19721 if (str_section->buffer == NULL)
19722 error (_("%s used without .debug_str.dwo section"
19723 " in CU at offset %s [in module %s]"),
19724 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19725 if (str_offsets_section->buffer == NULL)
19726 error (_("%s used without .debug_str_offsets.dwo section"
19727 " in CU at offset %s [in module %s]"),
19728 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19729 if (str_index * cu->header.offset_size >= str_offsets_section->size)
19730 error (_("%s pointing outside of .debug_str_offsets.dwo"
19731 " section in CU at offset %s [in module %s]"),
19732 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19733 info_ptr = (str_offsets_section->buffer
19734 + str_index * cu->header.offset_size);
19735 if (cu->header.offset_size == 4)
19736 str_offset = bfd_get_32 (abfd, info_ptr);
19737 else
19738 str_offset = bfd_get_64 (abfd, info_ptr);
19739 if (str_offset >= str_section->size)
19740 error (_("Offset from %s pointing outside of"
19741 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19742 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19743 return (const char *) (str_section->buffer + str_offset);
19744 }
19745
19746 /* Return the length of an LEB128 number in BUF. */
19747
19748 static int
19749 leb128_size (const gdb_byte *buf)
19750 {
19751 const gdb_byte *begin = buf;
19752 gdb_byte byte;
19753
19754 while (1)
19755 {
19756 byte = *buf++;
19757 if ((byte & 128) == 0)
19758 return buf - begin;
19759 }
19760 }
19761
19762 static void
19763 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19764 {
19765 switch (lang)
19766 {
19767 case DW_LANG_C89:
19768 case DW_LANG_C99:
19769 case DW_LANG_C11:
19770 case DW_LANG_C:
19771 case DW_LANG_UPC:
19772 cu->language = language_c;
19773 break;
19774 case DW_LANG_Java:
19775 case DW_LANG_C_plus_plus:
19776 case DW_LANG_C_plus_plus_11:
19777 case DW_LANG_C_plus_plus_14:
19778 cu->language = language_cplus;
19779 break;
19780 case DW_LANG_D:
19781 cu->language = language_d;
19782 break;
19783 case DW_LANG_Fortran77:
19784 case DW_LANG_Fortran90:
19785 case DW_LANG_Fortran95:
19786 case DW_LANG_Fortran03:
19787 case DW_LANG_Fortran08:
19788 cu->language = language_fortran;
19789 break;
19790 case DW_LANG_Go:
19791 cu->language = language_go;
19792 break;
19793 case DW_LANG_Mips_Assembler:
19794 cu->language = language_asm;
19795 break;
19796 case DW_LANG_Ada83:
19797 case DW_LANG_Ada95:
19798 cu->language = language_ada;
19799 break;
19800 case DW_LANG_Modula2:
19801 cu->language = language_m2;
19802 break;
19803 case DW_LANG_Pascal83:
19804 cu->language = language_pascal;
19805 break;
19806 case DW_LANG_ObjC:
19807 cu->language = language_objc;
19808 break;
19809 case DW_LANG_Rust:
19810 case DW_LANG_Rust_old:
19811 cu->language = language_rust;
19812 break;
19813 case DW_LANG_Cobol74:
19814 case DW_LANG_Cobol85:
19815 default:
19816 cu->language = language_minimal;
19817 break;
19818 }
19819 cu->language_defn = language_def (cu->language);
19820 }
19821
19822 /* Return the named attribute or NULL if not there. */
19823
19824 static struct attribute *
19825 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19826 {
19827 for (;;)
19828 {
19829 unsigned int i;
19830 struct attribute *spec = NULL;
19831
19832 for (i = 0; i < die->num_attrs; ++i)
19833 {
19834 if (die->attrs[i].name == name)
19835 return &die->attrs[i];
19836 if (die->attrs[i].name == DW_AT_specification
19837 || die->attrs[i].name == DW_AT_abstract_origin)
19838 spec = &die->attrs[i];
19839 }
19840
19841 if (!spec)
19842 break;
19843
19844 die = follow_die_ref (die, spec, &cu);
19845 }
19846
19847 return NULL;
19848 }
19849
19850 /* Return the named attribute or NULL if not there,
19851 but do not follow DW_AT_specification, etc.
19852 This is for use in contexts where we're reading .debug_types dies.
19853 Following DW_AT_specification, DW_AT_abstract_origin will take us
19854 back up the chain, and we want to go down. */
19855
19856 static struct attribute *
19857 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
19858 {
19859 unsigned int i;
19860
19861 for (i = 0; i < die->num_attrs; ++i)
19862 if (die->attrs[i].name == name)
19863 return &die->attrs[i];
19864
19865 return NULL;
19866 }
19867
19868 /* Return the string associated with a string-typed attribute, or NULL if it
19869 is either not found or is of an incorrect type. */
19870
19871 static const char *
19872 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19873 {
19874 struct attribute *attr;
19875 const char *str = NULL;
19876
19877 attr = dwarf2_attr (die, name, cu);
19878
19879 if (attr != NULL)
19880 {
19881 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
19882 || attr->form == DW_FORM_string
19883 || attr->form == DW_FORM_GNU_str_index
19884 || attr->form == DW_FORM_GNU_strp_alt)
19885 str = DW_STRING (attr);
19886 else
19887 complaint (&symfile_complaints,
19888 _("string type expected for attribute %s for "
19889 "DIE at %s in module %s"),
19890 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19891 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
19892 }
19893
19894 return str;
19895 }
19896
19897 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19898 and holds a non-zero value. This function should only be used for
19899 DW_FORM_flag or DW_FORM_flag_present attributes. */
19900
19901 static int
19902 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19903 {
19904 struct attribute *attr = dwarf2_attr (die, name, cu);
19905
19906 return (attr && DW_UNSND (attr));
19907 }
19908
19909 static int
19910 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19911 {
19912 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19913 which value is non-zero. However, we have to be careful with
19914 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19915 (via dwarf2_flag_true_p) follows this attribute. So we may
19916 end up accidently finding a declaration attribute that belongs
19917 to a different DIE referenced by the specification attribute,
19918 even though the given DIE does not have a declaration attribute. */
19919 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19920 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19921 }
19922
19923 /* Return the die giving the specification for DIE, if there is
19924 one. *SPEC_CU is the CU containing DIE on input, and the CU
19925 containing the return value on output. If there is no
19926 specification, but there is an abstract origin, that is
19927 returned. */
19928
19929 static struct die_info *
19930 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19931 {
19932 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19933 *spec_cu);
19934
19935 if (spec_attr == NULL)
19936 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19937
19938 if (spec_attr == NULL)
19939 return NULL;
19940 else
19941 return follow_die_ref (die, spec_attr, spec_cu);
19942 }
19943
19944 /* Stub for free_line_header to match void * callback types. */
19945
19946 static void
19947 free_line_header_voidp (void *arg)
19948 {
19949 struct line_header *lh = (struct line_header *) arg;
19950
19951 delete lh;
19952 }
19953
19954 void
19955 line_header::add_include_dir (const char *include_dir)
19956 {
19957 if (dwarf_line_debug >= 2)
19958 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
19959 include_dirs.size () + 1, include_dir);
19960
19961 include_dirs.push_back (include_dir);
19962 }
19963
19964 void
19965 line_header::add_file_name (const char *name,
19966 dir_index d_index,
19967 unsigned int mod_time,
19968 unsigned int length)
19969 {
19970 if (dwarf_line_debug >= 2)
19971 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
19972 (unsigned) file_names.size () + 1, name);
19973
19974 file_names.emplace_back (name, d_index, mod_time, length);
19975 }
19976
19977 /* A convenience function to find the proper .debug_line section for a CU. */
19978
19979 static struct dwarf2_section_info *
19980 get_debug_line_section (struct dwarf2_cu *cu)
19981 {
19982 struct dwarf2_section_info *section;
19983 struct dwarf2_per_objfile *dwarf2_per_objfile
19984 = cu->per_cu->dwarf2_per_objfile;
19985
19986 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19987 DWO file. */
19988 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19989 section = &cu->dwo_unit->dwo_file->sections.line;
19990 else if (cu->per_cu->is_dwz)
19991 {
19992 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19993
19994 section = &dwz->line;
19995 }
19996 else
19997 section = &dwarf2_per_objfile->line;
19998
19999 return section;
20000 }
20001
20002 /* Read directory or file name entry format, starting with byte of
20003 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20004 entries count and the entries themselves in the described entry
20005 format. */
20006
20007 static void
20008 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20009 bfd *abfd, const gdb_byte **bufp,
20010 struct line_header *lh,
20011 const struct comp_unit_head *cu_header,
20012 void (*callback) (struct line_header *lh,
20013 const char *name,
20014 dir_index d_index,
20015 unsigned int mod_time,
20016 unsigned int length))
20017 {
20018 gdb_byte format_count, formati;
20019 ULONGEST data_count, datai;
20020 const gdb_byte *buf = *bufp;
20021 const gdb_byte *format_header_data;
20022 unsigned int bytes_read;
20023
20024 format_count = read_1_byte (abfd, buf);
20025 buf += 1;
20026 format_header_data = buf;
20027 for (formati = 0; formati < format_count; formati++)
20028 {
20029 read_unsigned_leb128 (abfd, buf, &bytes_read);
20030 buf += bytes_read;
20031 read_unsigned_leb128 (abfd, buf, &bytes_read);
20032 buf += bytes_read;
20033 }
20034
20035 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20036 buf += bytes_read;
20037 for (datai = 0; datai < data_count; datai++)
20038 {
20039 const gdb_byte *format = format_header_data;
20040 struct file_entry fe;
20041
20042 for (formati = 0; formati < format_count; formati++)
20043 {
20044 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
20045 format += bytes_read;
20046
20047 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
20048 format += bytes_read;
20049
20050 gdb::optional<const char *> string;
20051 gdb::optional<unsigned int> uint;
20052
20053 switch (form)
20054 {
20055 case DW_FORM_string:
20056 string.emplace (read_direct_string (abfd, buf, &bytes_read));
20057 buf += bytes_read;
20058 break;
20059
20060 case DW_FORM_line_strp:
20061 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20062 abfd, buf,
20063 cu_header,
20064 &bytes_read));
20065 buf += bytes_read;
20066 break;
20067
20068 case DW_FORM_data1:
20069 uint.emplace (read_1_byte (abfd, buf));
20070 buf += 1;
20071 break;
20072
20073 case DW_FORM_data2:
20074 uint.emplace (read_2_bytes (abfd, buf));
20075 buf += 2;
20076 break;
20077
20078 case DW_FORM_data4:
20079 uint.emplace (read_4_bytes (abfd, buf));
20080 buf += 4;
20081 break;
20082
20083 case DW_FORM_data8:
20084 uint.emplace (read_8_bytes (abfd, buf));
20085 buf += 8;
20086 break;
20087
20088 case DW_FORM_udata:
20089 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
20090 buf += bytes_read;
20091 break;
20092
20093 case DW_FORM_block:
20094 /* It is valid only for DW_LNCT_timestamp which is ignored by
20095 current GDB. */
20096 break;
20097 }
20098
20099 switch (content_type)
20100 {
20101 case DW_LNCT_path:
20102 if (string.has_value ())
20103 fe.name = *string;
20104 break;
20105 case DW_LNCT_directory_index:
20106 if (uint.has_value ())
20107 fe.d_index = (dir_index) *uint;
20108 break;
20109 case DW_LNCT_timestamp:
20110 if (uint.has_value ())
20111 fe.mod_time = *uint;
20112 break;
20113 case DW_LNCT_size:
20114 if (uint.has_value ())
20115 fe.length = *uint;
20116 break;
20117 case DW_LNCT_MD5:
20118 break;
20119 default:
20120 complaint (&symfile_complaints,
20121 _("Unknown format content type %s"),
20122 pulongest (content_type));
20123 }
20124 }
20125
20126 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
20127 }
20128
20129 *bufp = buf;
20130 }
20131
20132 /* Read the statement program header starting at OFFSET in
20133 .debug_line, or .debug_line.dwo. Return a pointer
20134 to a struct line_header, allocated using xmalloc.
20135 Returns NULL if there is a problem reading the header, e.g., if it
20136 has a version we don't understand.
20137
20138 NOTE: the strings in the include directory and file name tables of
20139 the returned object point into the dwarf line section buffer,
20140 and must not be freed. */
20141
20142 static line_header_up
20143 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20144 {
20145 const gdb_byte *line_ptr;
20146 unsigned int bytes_read, offset_size;
20147 int i;
20148 const char *cur_dir, *cur_file;
20149 struct dwarf2_section_info *section;
20150 bfd *abfd;
20151 struct dwarf2_per_objfile *dwarf2_per_objfile
20152 = cu->per_cu->dwarf2_per_objfile;
20153
20154 section = get_debug_line_section (cu);
20155 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20156 if (section->buffer == NULL)
20157 {
20158 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20159 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
20160 else
20161 complaint (&symfile_complaints, _("missing .debug_line section"));
20162 return 0;
20163 }
20164
20165 /* We can't do this until we know the section is non-empty.
20166 Only then do we know we have such a section. */
20167 abfd = get_section_bfd_owner (section);
20168
20169 /* Make sure that at least there's room for the total_length field.
20170 That could be 12 bytes long, but we're just going to fudge that. */
20171 if (to_underlying (sect_off) + 4 >= section->size)
20172 {
20173 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20174 return 0;
20175 }
20176
20177 line_header_up lh (new line_header ());
20178
20179 lh->sect_off = sect_off;
20180 lh->offset_in_dwz = cu->per_cu->is_dwz;
20181
20182 line_ptr = section->buffer + to_underlying (sect_off);
20183
20184 /* Read in the header. */
20185 lh->total_length =
20186 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20187 &bytes_read, &offset_size);
20188 line_ptr += bytes_read;
20189 if (line_ptr + lh->total_length > (section->buffer + section->size))
20190 {
20191 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20192 return 0;
20193 }
20194 lh->statement_program_end = line_ptr + lh->total_length;
20195 lh->version = read_2_bytes (abfd, line_ptr);
20196 line_ptr += 2;
20197 if (lh->version > 5)
20198 {
20199 /* This is a version we don't understand. The format could have
20200 changed in ways we don't handle properly so just punt. */
20201 complaint (&symfile_complaints,
20202 _("unsupported version in .debug_line section"));
20203 return NULL;
20204 }
20205 if (lh->version >= 5)
20206 {
20207 gdb_byte segment_selector_size;
20208
20209 /* Skip address size. */
20210 read_1_byte (abfd, line_ptr);
20211 line_ptr += 1;
20212
20213 segment_selector_size = read_1_byte (abfd, line_ptr);
20214 line_ptr += 1;
20215 if (segment_selector_size != 0)
20216 {
20217 complaint (&symfile_complaints,
20218 _("unsupported segment selector size %u "
20219 "in .debug_line section"),
20220 segment_selector_size);
20221 return NULL;
20222 }
20223 }
20224 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20225 line_ptr += offset_size;
20226 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20227 line_ptr += 1;
20228 if (lh->version >= 4)
20229 {
20230 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20231 line_ptr += 1;
20232 }
20233 else
20234 lh->maximum_ops_per_instruction = 1;
20235
20236 if (lh->maximum_ops_per_instruction == 0)
20237 {
20238 lh->maximum_ops_per_instruction = 1;
20239 complaint (&symfile_complaints,
20240 _("invalid maximum_ops_per_instruction "
20241 "in `.debug_line' section"));
20242 }
20243
20244 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20245 line_ptr += 1;
20246 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20247 line_ptr += 1;
20248 lh->line_range = read_1_byte (abfd, line_ptr);
20249 line_ptr += 1;
20250 lh->opcode_base = read_1_byte (abfd, line_ptr);
20251 line_ptr += 1;
20252 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
20253
20254 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20255 for (i = 1; i < lh->opcode_base; ++i)
20256 {
20257 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20258 line_ptr += 1;
20259 }
20260
20261 if (lh->version >= 5)
20262 {
20263 /* Read directory table. */
20264 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20265 &cu->header,
20266 [] (struct line_header *lh, const char *name,
20267 dir_index d_index, unsigned int mod_time,
20268 unsigned int length)
20269 {
20270 lh->add_include_dir (name);
20271 });
20272
20273 /* Read file name table. */
20274 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20275 &cu->header,
20276 [] (struct line_header *lh, const char *name,
20277 dir_index d_index, unsigned int mod_time,
20278 unsigned int length)
20279 {
20280 lh->add_file_name (name, d_index, mod_time, length);
20281 });
20282 }
20283 else
20284 {
20285 /* Read directory table. */
20286 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20287 {
20288 line_ptr += bytes_read;
20289 lh->add_include_dir (cur_dir);
20290 }
20291 line_ptr += bytes_read;
20292
20293 /* Read file name table. */
20294 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20295 {
20296 unsigned int mod_time, length;
20297 dir_index d_index;
20298
20299 line_ptr += bytes_read;
20300 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20301 line_ptr += bytes_read;
20302 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20303 line_ptr += bytes_read;
20304 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20305 line_ptr += bytes_read;
20306
20307 lh->add_file_name (cur_file, d_index, mod_time, length);
20308 }
20309 line_ptr += bytes_read;
20310 }
20311 lh->statement_program_start = line_ptr;
20312
20313 if (line_ptr > (section->buffer + section->size))
20314 complaint (&symfile_complaints,
20315 _("line number info header doesn't "
20316 "fit in `.debug_line' section"));
20317
20318 return lh;
20319 }
20320
20321 /* Subroutine of dwarf_decode_lines to simplify it.
20322 Return the file name of the psymtab for included file FILE_INDEX
20323 in line header LH of PST.
20324 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20325 If space for the result is malloc'd, *NAME_HOLDER will be set.
20326 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20327
20328 static const char *
20329 psymtab_include_file_name (const struct line_header *lh, int file_index,
20330 const struct partial_symtab *pst,
20331 const char *comp_dir,
20332 gdb::unique_xmalloc_ptr<char> *name_holder)
20333 {
20334 const file_entry &fe = lh->file_names[file_index];
20335 const char *include_name = fe.name;
20336 const char *include_name_to_compare = include_name;
20337 const char *pst_filename;
20338 int file_is_pst;
20339
20340 const char *dir_name = fe.include_dir (lh);
20341
20342 gdb::unique_xmalloc_ptr<char> hold_compare;
20343 if (!IS_ABSOLUTE_PATH (include_name)
20344 && (dir_name != NULL || comp_dir != NULL))
20345 {
20346 /* Avoid creating a duplicate psymtab for PST.
20347 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20348 Before we do the comparison, however, we need to account
20349 for DIR_NAME and COMP_DIR.
20350 First prepend dir_name (if non-NULL). If we still don't
20351 have an absolute path prepend comp_dir (if non-NULL).
20352 However, the directory we record in the include-file's
20353 psymtab does not contain COMP_DIR (to match the
20354 corresponding symtab(s)).
20355
20356 Example:
20357
20358 bash$ cd /tmp
20359 bash$ gcc -g ./hello.c
20360 include_name = "hello.c"
20361 dir_name = "."
20362 DW_AT_comp_dir = comp_dir = "/tmp"
20363 DW_AT_name = "./hello.c"
20364
20365 */
20366
20367 if (dir_name != NULL)
20368 {
20369 name_holder->reset (concat (dir_name, SLASH_STRING,
20370 include_name, (char *) NULL));
20371 include_name = name_holder->get ();
20372 include_name_to_compare = include_name;
20373 }
20374 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20375 {
20376 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20377 include_name, (char *) NULL));
20378 include_name_to_compare = hold_compare.get ();
20379 }
20380 }
20381
20382 pst_filename = pst->filename;
20383 gdb::unique_xmalloc_ptr<char> copied_name;
20384 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20385 {
20386 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20387 pst_filename, (char *) NULL));
20388 pst_filename = copied_name.get ();
20389 }
20390
20391 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20392
20393 if (file_is_pst)
20394 return NULL;
20395 return include_name;
20396 }
20397
20398 /* State machine to track the state of the line number program. */
20399
20400 class lnp_state_machine
20401 {
20402 public:
20403 /* Initialize a machine state for the start of a line number
20404 program. */
20405 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
20406
20407 file_entry *current_file ()
20408 {
20409 /* lh->file_names is 0-based, but the file name numbers in the
20410 statement program are 1-based. */
20411 return m_line_header->file_name_at (m_file);
20412 }
20413
20414 /* Record the line in the state machine. END_SEQUENCE is true if
20415 we're processing the end of a sequence. */
20416 void record_line (bool end_sequence);
20417
20418 /* Check address and if invalid nop-out the rest of the lines in this
20419 sequence. */
20420 void check_line_address (struct dwarf2_cu *cu,
20421 const gdb_byte *line_ptr,
20422 CORE_ADDR lowpc, CORE_ADDR address);
20423
20424 void handle_set_discriminator (unsigned int discriminator)
20425 {
20426 m_discriminator = discriminator;
20427 m_line_has_non_zero_discriminator |= discriminator != 0;
20428 }
20429
20430 /* Handle DW_LNE_set_address. */
20431 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20432 {
20433 m_op_index = 0;
20434 address += baseaddr;
20435 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20436 }
20437
20438 /* Handle DW_LNS_advance_pc. */
20439 void handle_advance_pc (CORE_ADDR adjust);
20440
20441 /* Handle a special opcode. */
20442 void handle_special_opcode (unsigned char op_code);
20443
20444 /* Handle DW_LNS_advance_line. */
20445 void handle_advance_line (int line_delta)
20446 {
20447 advance_line (line_delta);
20448 }
20449
20450 /* Handle DW_LNS_set_file. */
20451 void handle_set_file (file_name_index file);
20452
20453 /* Handle DW_LNS_negate_stmt. */
20454 void handle_negate_stmt ()
20455 {
20456 m_is_stmt = !m_is_stmt;
20457 }
20458
20459 /* Handle DW_LNS_const_add_pc. */
20460 void handle_const_add_pc ();
20461
20462 /* Handle DW_LNS_fixed_advance_pc. */
20463 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20464 {
20465 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20466 m_op_index = 0;
20467 }
20468
20469 /* Handle DW_LNS_copy. */
20470 void handle_copy ()
20471 {
20472 record_line (false);
20473 m_discriminator = 0;
20474 }
20475
20476 /* Handle DW_LNE_end_sequence. */
20477 void handle_end_sequence ()
20478 {
20479 m_record_line_callback = ::record_line;
20480 }
20481
20482 private:
20483 /* Advance the line by LINE_DELTA. */
20484 void advance_line (int line_delta)
20485 {
20486 m_line += line_delta;
20487
20488 if (line_delta != 0)
20489 m_line_has_non_zero_discriminator = m_discriminator != 0;
20490 }
20491
20492 gdbarch *m_gdbarch;
20493
20494 /* True if we're recording lines.
20495 Otherwise we're building partial symtabs and are just interested in
20496 finding include files mentioned by the line number program. */
20497 bool m_record_lines_p;
20498
20499 /* The line number header. */
20500 line_header *m_line_header;
20501
20502 /* These are part of the standard DWARF line number state machine,
20503 and initialized according to the DWARF spec. */
20504
20505 unsigned char m_op_index = 0;
20506 /* The line table index (1-based) of the current file. */
20507 file_name_index m_file = (file_name_index) 1;
20508 unsigned int m_line = 1;
20509
20510 /* These are initialized in the constructor. */
20511
20512 CORE_ADDR m_address;
20513 bool m_is_stmt;
20514 unsigned int m_discriminator;
20515
20516 /* Additional bits of state we need to track. */
20517
20518 /* The last file that we called dwarf2_start_subfile for.
20519 This is only used for TLLs. */
20520 unsigned int m_last_file = 0;
20521 /* The last file a line number was recorded for. */
20522 struct subfile *m_last_subfile = NULL;
20523
20524 /* The function to call to record a line. */
20525 record_line_ftype *m_record_line_callback = NULL;
20526
20527 /* The last line number that was recorded, used to coalesce
20528 consecutive entries for the same line. This can happen, for
20529 example, when discriminators are present. PR 17276. */
20530 unsigned int m_last_line = 0;
20531 bool m_line_has_non_zero_discriminator = false;
20532 };
20533
20534 void
20535 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20536 {
20537 CORE_ADDR addr_adj = (((m_op_index + adjust)
20538 / m_line_header->maximum_ops_per_instruction)
20539 * m_line_header->minimum_instruction_length);
20540 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20541 m_op_index = ((m_op_index + adjust)
20542 % m_line_header->maximum_ops_per_instruction);
20543 }
20544
20545 void
20546 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20547 {
20548 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20549 CORE_ADDR addr_adj = (((m_op_index
20550 + (adj_opcode / m_line_header->line_range))
20551 / m_line_header->maximum_ops_per_instruction)
20552 * m_line_header->minimum_instruction_length);
20553 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20554 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20555 % m_line_header->maximum_ops_per_instruction);
20556
20557 int line_delta = (m_line_header->line_base
20558 + (adj_opcode % m_line_header->line_range));
20559 advance_line (line_delta);
20560 record_line (false);
20561 m_discriminator = 0;
20562 }
20563
20564 void
20565 lnp_state_machine::handle_set_file (file_name_index file)
20566 {
20567 m_file = file;
20568
20569 const file_entry *fe = current_file ();
20570 if (fe == NULL)
20571 dwarf2_debug_line_missing_file_complaint ();
20572 else if (m_record_lines_p)
20573 {
20574 const char *dir = fe->include_dir (m_line_header);
20575
20576 m_last_subfile = current_subfile;
20577 m_line_has_non_zero_discriminator = m_discriminator != 0;
20578 dwarf2_start_subfile (fe->name, dir);
20579 }
20580 }
20581
20582 void
20583 lnp_state_machine::handle_const_add_pc ()
20584 {
20585 CORE_ADDR adjust
20586 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20587
20588 CORE_ADDR addr_adj
20589 = (((m_op_index + adjust)
20590 / m_line_header->maximum_ops_per_instruction)
20591 * m_line_header->minimum_instruction_length);
20592
20593 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20594 m_op_index = ((m_op_index + adjust)
20595 % m_line_header->maximum_ops_per_instruction);
20596 }
20597
20598 /* Ignore this record_line request. */
20599
20600 static void
20601 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
20602 {
20603 return;
20604 }
20605
20606 /* Return non-zero if we should add LINE to the line number table.
20607 LINE is the line to add, LAST_LINE is the last line that was added,
20608 LAST_SUBFILE is the subfile for LAST_LINE.
20609 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20610 had a non-zero discriminator.
20611
20612 We have to be careful in the presence of discriminators.
20613 E.g., for this line:
20614
20615 for (i = 0; i < 100000; i++);
20616
20617 clang can emit four line number entries for that one line,
20618 each with a different discriminator.
20619 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20620
20621 However, we want gdb to coalesce all four entries into one.
20622 Otherwise the user could stepi into the middle of the line and
20623 gdb would get confused about whether the pc really was in the
20624 middle of the line.
20625
20626 Things are further complicated by the fact that two consecutive
20627 line number entries for the same line is a heuristic used by gcc
20628 to denote the end of the prologue. So we can't just discard duplicate
20629 entries, we have to be selective about it. The heuristic we use is
20630 that we only collapse consecutive entries for the same line if at least
20631 one of those entries has a non-zero discriminator. PR 17276.
20632
20633 Note: Addresses in the line number state machine can never go backwards
20634 within one sequence, thus this coalescing is ok. */
20635
20636 static int
20637 dwarf_record_line_p (unsigned int line, unsigned int last_line,
20638 int line_has_non_zero_discriminator,
20639 struct subfile *last_subfile)
20640 {
20641 if (current_subfile != last_subfile)
20642 return 1;
20643 if (line != last_line)
20644 return 1;
20645 /* Same line for the same file that we've seen already.
20646 As a last check, for pr 17276, only record the line if the line
20647 has never had a non-zero discriminator. */
20648 if (!line_has_non_zero_discriminator)
20649 return 1;
20650 return 0;
20651 }
20652
20653 /* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
20654 in the line table of subfile SUBFILE. */
20655
20656 static void
20657 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20658 unsigned int line, CORE_ADDR address,
20659 record_line_ftype p_record_line)
20660 {
20661 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20662
20663 if (dwarf_line_debug)
20664 {
20665 fprintf_unfiltered (gdb_stdlog,
20666 "Recording line %u, file %s, address %s\n",
20667 line, lbasename (subfile->name),
20668 paddress (gdbarch, address));
20669 }
20670
20671 (*p_record_line) (subfile, line, addr);
20672 }
20673
20674 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20675 Mark the end of a set of line number records.
20676 The arguments are the same as for dwarf_record_line_1.
20677 If SUBFILE is NULL the request is ignored. */
20678
20679 static void
20680 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20681 CORE_ADDR address, record_line_ftype p_record_line)
20682 {
20683 if (subfile == NULL)
20684 return;
20685
20686 if (dwarf_line_debug)
20687 {
20688 fprintf_unfiltered (gdb_stdlog,
20689 "Finishing current line, file %s, address %s\n",
20690 lbasename (subfile->name),
20691 paddress (gdbarch, address));
20692 }
20693
20694 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
20695 }
20696
20697 void
20698 lnp_state_machine::record_line (bool end_sequence)
20699 {
20700 if (dwarf_line_debug)
20701 {
20702 fprintf_unfiltered (gdb_stdlog,
20703 "Processing actual line %u: file %u,"
20704 " address %s, is_stmt %u, discrim %u\n",
20705 m_line, to_underlying (m_file),
20706 paddress (m_gdbarch, m_address),
20707 m_is_stmt, m_discriminator);
20708 }
20709
20710 file_entry *fe = current_file ();
20711
20712 if (fe == NULL)
20713 dwarf2_debug_line_missing_file_complaint ();
20714 /* For now we ignore lines not starting on an instruction boundary.
20715 But not when processing end_sequence for compatibility with the
20716 previous version of the code. */
20717 else if (m_op_index == 0 || end_sequence)
20718 {
20719 fe->included_p = 1;
20720 if (m_record_lines_p && m_is_stmt)
20721 {
20722 if (m_last_subfile != current_subfile || end_sequence)
20723 {
20724 dwarf_finish_line (m_gdbarch, m_last_subfile,
20725 m_address, m_record_line_callback);
20726 }
20727
20728 if (!end_sequence)
20729 {
20730 if (dwarf_record_line_p (m_line, m_last_line,
20731 m_line_has_non_zero_discriminator,
20732 m_last_subfile))
20733 {
20734 dwarf_record_line_1 (m_gdbarch, current_subfile,
20735 m_line, m_address,
20736 m_record_line_callback);
20737 }
20738 m_last_subfile = current_subfile;
20739 m_last_line = m_line;
20740 }
20741 }
20742 }
20743 }
20744
20745 lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
20746 bool record_lines_p)
20747 {
20748 m_gdbarch = arch;
20749 m_record_lines_p = record_lines_p;
20750 m_line_header = lh;
20751
20752 m_record_line_callback = ::record_line;
20753
20754 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20755 was a line entry for it so that the backend has a chance to adjust it
20756 and also record it in case it needs it. This is currently used by MIPS
20757 code, cf. `mips_adjust_dwarf2_line'. */
20758 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20759 m_is_stmt = lh->default_is_stmt;
20760 m_discriminator = 0;
20761 }
20762
20763 void
20764 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20765 const gdb_byte *line_ptr,
20766 CORE_ADDR lowpc, CORE_ADDR address)
20767 {
20768 /* If address < lowpc then it's not a usable value, it's outside the
20769 pc range of the CU. However, we restrict the test to only address
20770 values of zero to preserve GDB's previous behaviour which is to
20771 handle the specific case of a function being GC'd by the linker. */
20772
20773 if (address == 0 && address < lowpc)
20774 {
20775 /* This line table is for a function which has been
20776 GCd by the linker. Ignore it. PR gdb/12528 */
20777
20778 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20779 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20780
20781 complaint (&symfile_complaints,
20782 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20783 line_offset, objfile_name (objfile));
20784 m_record_line_callback = noop_record_line;
20785 /* Note: record_line_callback is left as noop_record_line until
20786 we see DW_LNE_end_sequence. */
20787 }
20788 }
20789
20790 /* Subroutine of dwarf_decode_lines to simplify it.
20791 Process the line number information in LH.
20792 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20793 program in order to set included_p for every referenced header. */
20794
20795 static void
20796 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20797 const int decode_for_pst_p, CORE_ADDR lowpc)
20798 {
20799 const gdb_byte *line_ptr, *extended_end;
20800 const gdb_byte *line_end;
20801 unsigned int bytes_read, extended_len;
20802 unsigned char op_code, extended_op;
20803 CORE_ADDR baseaddr;
20804 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20805 bfd *abfd = objfile->obfd;
20806 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20807 /* True if we're recording line info (as opposed to building partial
20808 symtabs and just interested in finding include files mentioned by
20809 the line number program). */
20810 bool record_lines_p = !decode_for_pst_p;
20811
20812 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20813
20814 line_ptr = lh->statement_program_start;
20815 line_end = lh->statement_program_end;
20816
20817 /* Read the statement sequences until there's nothing left. */
20818 while (line_ptr < line_end)
20819 {
20820 /* The DWARF line number program state machine. Reset the state
20821 machine at the start of each sequence. */
20822 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
20823 bool end_sequence = false;
20824
20825 if (record_lines_p)
20826 {
20827 /* Start a subfile for the current file of the state
20828 machine. */
20829 const file_entry *fe = state_machine.current_file ();
20830
20831 if (fe != NULL)
20832 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
20833 }
20834
20835 /* Decode the table. */
20836 while (line_ptr < line_end && !end_sequence)
20837 {
20838 op_code = read_1_byte (abfd, line_ptr);
20839 line_ptr += 1;
20840
20841 if (op_code >= lh->opcode_base)
20842 {
20843 /* Special opcode. */
20844 state_machine.handle_special_opcode (op_code);
20845 }
20846 else switch (op_code)
20847 {
20848 case DW_LNS_extended_op:
20849 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20850 &bytes_read);
20851 line_ptr += bytes_read;
20852 extended_end = line_ptr + extended_len;
20853 extended_op = read_1_byte (abfd, line_ptr);
20854 line_ptr += 1;
20855 switch (extended_op)
20856 {
20857 case DW_LNE_end_sequence:
20858 state_machine.handle_end_sequence ();
20859 end_sequence = true;
20860 break;
20861 case DW_LNE_set_address:
20862 {
20863 CORE_ADDR address
20864 = read_address (abfd, line_ptr, cu, &bytes_read);
20865 line_ptr += bytes_read;
20866
20867 state_machine.check_line_address (cu, line_ptr,
20868 lowpc, address);
20869 state_machine.handle_set_address (baseaddr, address);
20870 }
20871 break;
20872 case DW_LNE_define_file:
20873 {
20874 const char *cur_file;
20875 unsigned int mod_time, length;
20876 dir_index dindex;
20877
20878 cur_file = read_direct_string (abfd, line_ptr,
20879 &bytes_read);
20880 line_ptr += bytes_read;
20881 dindex = (dir_index)
20882 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20883 line_ptr += bytes_read;
20884 mod_time =
20885 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20886 line_ptr += bytes_read;
20887 length =
20888 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20889 line_ptr += bytes_read;
20890 lh->add_file_name (cur_file, dindex, mod_time, length);
20891 }
20892 break;
20893 case DW_LNE_set_discriminator:
20894 {
20895 /* The discriminator is not interesting to the
20896 debugger; just ignore it. We still need to
20897 check its value though:
20898 if there are consecutive entries for the same
20899 (non-prologue) line we want to coalesce them.
20900 PR 17276. */
20901 unsigned int discr
20902 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20903 line_ptr += bytes_read;
20904
20905 state_machine.handle_set_discriminator (discr);
20906 }
20907 break;
20908 default:
20909 complaint (&symfile_complaints,
20910 _("mangled .debug_line section"));
20911 return;
20912 }
20913 /* Make sure that we parsed the extended op correctly. If e.g.
20914 we expected a different address size than the producer used,
20915 we may have read the wrong number of bytes. */
20916 if (line_ptr != extended_end)
20917 {
20918 complaint (&symfile_complaints,
20919 _("mangled .debug_line section"));
20920 return;
20921 }
20922 break;
20923 case DW_LNS_copy:
20924 state_machine.handle_copy ();
20925 break;
20926 case DW_LNS_advance_pc:
20927 {
20928 CORE_ADDR adjust
20929 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20930 line_ptr += bytes_read;
20931
20932 state_machine.handle_advance_pc (adjust);
20933 }
20934 break;
20935 case DW_LNS_advance_line:
20936 {
20937 int line_delta
20938 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20939 line_ptr += bytes_read;
20940
20941 state_machine.handle_advance_line (line_delta);
20942 }
20943 break;
20944 case DW_LNS_set_file:
20945 {
20946 file_name_index file
20947 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20948 &bytes_read);
20949 line_ptr += bytes_read;
20950
20951 state_machine.handle_set_file (file);
20952 }
20953 break;
20954 case DW_LNS_set_column:
20955 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20956 line_ptr += bytes_read;
20957 break;
20958 case DW_LNS_negate_stmt:
20959 state_machine.handle_negate_stmt ();
20960 break;
20961 case DW_LNS_set_basic_block:
20962 break;
20963 /* Add to the address register of the state machine the
20964 address increment value corresponding to special opcode
20965 255. I.e., this value is scaled by the minimum
20966 instruction length since special opcode 255 would have
20967 scaled the increment. */
20968 case DW_LNS_const_add_pc:
20969 state_machine.handle_const_add_pc ();
20970 break;
20971 case DW_LNS_fixed_advance_pc:
20972 {
20973 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20974 line_ptr += 2;
20975
20976 state_machine.handle_fixed_advance_pc (addr_adj);
20977 }
20978 break;
20979 default:
20980 {
20981 /* Unknown standard opcode, ignore it. */
20982 int i;
20983
20984 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20985 {
20986 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20987 line_ptr += bytes_read;
20988 }
20989 }
20990 }
20991 }
20992
20993 if (!end_sequence)
20994 dwarf2_debug_line_missing_end_sequence_complaint ();
20995
20996 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20997 in which case we still finish recording the last line). */
20998 state_machine.record_line (true);
20999 }
21000 }
21001
21002 /* Decode the Line Number Program (LNP) for the given line_header
21003 structure and CU. The actual information extracted and the type
21004 of structures created from the LNP depends on the value of PST.
21005
21006 1. If PST is NULL, then this procedure uses the data from the program
21007 to create all necessary symbol tables, and their linetables.
21008
21009 2. If PST is not NULL, this procedure reads the program to determine
21010 the list of files included by the unit represented by PST, and
21011 builds all the associated partial symbol tables.
21012
21013 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21014 It is used for relative paths in the line table.
21015 NOTE: When processing partial symtabs (pst != NULL),
21016 comp_dir == pst->dirname.
21017
21018 NOTE: It is important that psymtabs have the same file name (via strcmp)
21019 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21020 symtab we don't use it in the name of the psymtabs we create.
21021 E.g. expand_line_sal requires this when finding psymtabs to expand.
21022 A good testcase for this is mb-inline.exp.
21023
21024 LOWPC is the lowest address in CU (or 0 if not known).
21025
21026 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21027 for its PC<->lines mapping information. Otherwise only the filename
21028 table is read in. */
21029
21030 static void
21031 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21032 struct dwarf2_cu *cu, struct partial_symtab *pst,
21033 CORE_ADDR lowpc, int decode_mapping)
21034 {
21035 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21036 const int decode_for_pst_p = (pst != NULL);
21037
21038 if (decode_mapping)
21039 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21040
21041 if (decode_for_pst_p)
21042 {
21043 int file_index;
21044
21045 /* Now that we're done scanning the Line Header Program, we can
21046 create the psymtab of each included file. */
21047 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
21048 if (lh->file_names[file_index].included_p == 1)
21049 {
21050 gdb::unique_xmalloc_ptr<char> name_holder;
21051 const char *include_name =
21052 psymtab_include_file_name (lh, file_index, pst, comp_dir,
21053 &name_holder);
21054 if (include_name != NULL)
21055 dwarf2_create_include_psymtab (include_name, pst, objfile);
21056 }
21057 }
21058 else
21059 {
21060 /* Make sure a symtab is created for every file, even files
21061 which contain only variables (i.e. no code with associated
21062 line numbers). */
21063 struct compunit_symtab *cust = buildsym_compunit_symtab ();
21064 int i;
21065
21066 for (i = 0; i < lh->file_names.size (); i++)
21067 {
21068 file_entry &fe = lh->file_names[i];
21069
21070 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
21071
21072 if (current_subfile->symtab == NULL)
21073 {
21074 current_subfile->symtab
21075 = allocate_symtab (cust, current_subfile->name);
21076 }
21077 fe.symtab = current_subfile->symtab;
21078 }
21079 }
21080 }
21081
21082 /* Start a subfile for DWARF. FILENAME is the name of the file and
21083 DIRNAME the name of the source directory which contains FILENAME
21084 or NULL if not known.
21085 This routine tries to keep line numbers from identical absolute and
21086 relative file names in a common subfile.
21087
21088 Using the `list' example from the GDB testsuite, which resides in
21089 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21090 of /srcdir/list0.c yields the following debugging information for list0.c:
21091
21092 DW_AT_name: /srcdir/list0.c
21093 DW_AT_comp_dir: /compdir
21094 files.files[0].name: list0.h
21095 files.files[0].dir: /srcdir
21096 files.files[1].name: list0.c
21097 files.files[1].dir: /srcdir
21098
21099 The line number information for list0.c has to end up in a single
21100 subfile, so that `break /srcdir/list0.c:1' works as expected.
21101 start_subfile will ensure that this happens provided that we pass the
21102 concatenation of files.files[1].dir and files.files[1].name as the
21103 subfile's name. */
21104
21105 static void
21106 dwarf2_start_subfile (const char *filename, const char *dirname)
21107 {
21108 char *copy = NULL;
21109
21110 /* In order not to lose the line information directory,
21111 we concatenate it to the filename when it makes sense.
21112 Note that the Dwarf3 standard says (speaking of filenames in line
21113 information): ``The directory index is ignored for file names
21114 that represent full path names''. Thus ignoring dirname in the
21115 `else' branch below isn't an issue. */
21116
21117 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21118 {
21119 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21120 filename = copy;
21121 }
21122
21123 start_subfile (filename);
21124
21125 if (copy != NULL)
21126 xfree (copy);
21127 }
21128
21129 /* Start a symtab for DWARF.
21130 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
21131
21132 static struct compunit_symtab *
21133 dwarf2_start_symtab (struct dwarf2_cu *cu,
21134 const char *name, const char *comp_dir, CORE_ADDR low_pc)
21135 {
21136 struct compunit_symtab *cust
21137 = start_symtab (cu->per_cu->dwarf2_per_objfile->objfile, name, comp_dir,
21138 low_pc, cu->language);
21139
21140 record_debugformat ("DWARF 2");
21141 record_producer (cu->producer);
21142
21143 /* We assume that we're processing GCC output. */
21144 processing_gcc_compilation = 2;
21145
21146 cu->processing_has_namespace_info = 0;
21147
21148 return cust;
21149 }
21150
21151 static void
21152 var_decode_location (struct attribute *attr, struct symbol *sym,
21153 struct dwarf2_cu *cu)
21154 {
21155 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21156 struct comp_unit_head *cu_header = &cu->header;
21157
21158 /* NOTE drow/2003-01-30: There used to be a comment and some special
21159 code here to turn a symbol with DW_AT_external and a
21160 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21161 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21162 with some versions of binutils) where shared libraries could have
21163 relocations against symbols in their debug information - the
21164 minimal symbol would have the right address, but the debug info
21165 would not. It's no longer necessary, because we will explicitly
21166 apply relocations when we read in the debug information now. */
21167
21168 /* A DW_AT_location attribute with no contents indicates that a
21169 variable has been optimized away. */
21170 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21171 {
21172 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21173 return;
21174 }
21175
21176 /* Handle one degenerate form of location expression specially, to
21177 preserve GDB's previous behavior when section offsets are
21178 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
21179 then mark this symbol as LOC_STATIC. */
21180
21181 if (attr_form_is_block (attr)
21182 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21183 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21184 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21185 && (DW_BLOCK (attr)->size
21186 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
21187 {
21188 unsigned int dummy;
21189
21190 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21191 SYMBOL_VALUE_ADDRESS (sym) =
21192 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
21193 else
21194 SYMBOL_VALUE_ADDRESS (sym) =
21195 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
21196 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21197 fixup_symbol_section (sym, objfile);
21198 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
21199 SYMBOL_SECTION (sym));
21200 return;
21201 }
21202
21203 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21204 expression evaluator, and use LOC_COMPUTED only when necessary
21205 (i.e. when the value of a register or memory location is
21206 referenced, or a thread-local block, etc.). Then again, it might
21207 not be worthwhile. I'm assuming that it isn't unless performance
21208 or memory numbers show me otherwise. */
21209
21210 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21211
21212 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21213 cu->has_loclist = 1;
21214 }
21215
21216 /* Given a pointer to a DWARF information entry, figure out if we need
21217 to make a symbol table entry for it, and if so, create a new entry
21218 and return a pointer to it.
21219 If TYPE is NULL, determine symbol type from the die, otherwise
21220 used the passed type.
21221 If SPACE is not NULL, use it to hold the new symbol. If it is
21222 NULL, allocate a new symbol on the objfile's obstack. */
21223
21224 static struct symbol *
21225 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21226 struct symbol *space)
21227 {
21228 struct dwarf2_per_objfile *dwarf2_per_objfile
21229 = cu->per_cu->dwarf2_per_objfile;
21230 struct objfile *objfile = dwarf2_per_objfile->objfile;
21231 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21232 struct symbol *sym = NULL;
21233 const char *name;
21234 struct attribute *attr = NULL;
21235 struct attribute *attr2 = NULL;
21236 CORE_ADDR baseaddr;
21237 struct pending **list_to_add = NULL;
21238
21239 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21240
21241 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21242
21243 name = dwarf2_name (die, cu);
21244 if (name)
21245 {
21246 const char *linkagename;
21247 int suppress_add = 0;
21248
21249 if (space)
21250 sym = space;
21251 else
21252 sym = allocate_symbol (objfile);
21253 OBJSTAT (objfile, n_syms++);
21254
21255 /* Cache this symbol's name and the name's demangled form (if any). */
21256 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
21257 linkagename = dwarf2_physname (name, die, cu);
21258 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
21259
21260 /* Fortran does not have mangling standard and the mangling does differ
21261 between gfortran, iFort etc. */
21262 if (cu->language == language_fortran
21263 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
21264 symbol_set_demangled_name (&(sym->ginfo),
21265 dwarf2_full_name (name, die, cu),
21266 NULL);
21267
21268 /* Default assumptions.
21269 Use the passed type or decode it from the die. */
21270 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21271 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21272 if (type != NULL)
21273 SYMBOL_TYPE (sym) = type;
21274 else
21275 SYMBOL_TYPE (sym) = die_type (die, cu);
21276 attr = dwarf2_attr (die,
21277 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21278 cu);
21279 if (attr)
21280 {
21281 SYMBOL_LINE (sym) = DW_UNSND (attr);
21282 }
21283
21284 attr = dwarf2_attr (die,
21285 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21286 cu);
21287 if (attr)
21288 {
21289 file_name_index file_index = (file_name_index) DW_UNSND (attr);
21290 struct file_entry *fe;
21291
21292 if (cu->line_header != NULL)
21293 fe = cu->line_header->file_name_at (file_index);
21294 else
21295 fe = NULL;
21296
21297 if (fe == NULL)
21298 complaint (&symfile_complaints,
21299 _("file index out of range"));
21300 else
21301 symbol_set_symtab (sym, fe->symtab);
21302 }
21303
21304 switch (die->tag)
21305 {
21306 case DW_TAG_label:
21307 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21308 if (attr)
21309 {
21310 CORE_ADDR addr;
21311
21312 addr = attr_value_as_address (attr);
21313 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21314 SYMBOL_VALUE_ADDRESS (sym) = addr;
21315 }
21316 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21317 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21318 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21319 add_symbol_to_list (sym, cu->list_in_scope);
21320 break;
21321 case DW_TAG_subprogram:
21322 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21323 finish_block. */
21324 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21325 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21326 if ((attr2 && (DW_UNSND (attr2) != 0))
21327 || cu->language == language_ada)
21328 {
21329 /* Subprograms marked external are stored as a global symbol.
21330 Ada subprograms, whether marked external or not, are always
21331 stored as a global symbol, because we want to be able to
21332 access them globally. For instance, we want to be able
21333 to break on a nested subprogram without having to
21334 specify the context. */
21335 list_to_add = &global_symbols;
21336 }
21337 else
21338 {
21339 list_to_add = cu->list_in_scope;
21340 }
21341 break;
21342 case DW_TAG_inlined_subroutine:
21343 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21344 finish_block. */
21345 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21346 SYMBOL_INLINED (sym) = 1;
21347 list_to_add = cu->list_in_scope;
21348 break;
21349 case DW_TAG_template_value_param:
21350 suppress_add = 1;
21351 /* Fall through. */
21352 case DW_TAG_constant:
21353 case DW_TAG_variable:
21354 case DW_TAG_member:
21355 /* Compilation with minimal debug info may result in
21356 variables with missing type entries. Change the
21357 misleading `void' type to something sensible. */
21358 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
21359 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21360
21361 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21362 /* In the case of DW_TAG_member, we should only be called for
21363 static const members. */
21364 if (die->tag == DW_TAG_member)
21365 {
21366 /* dwarf2_add_field uses die_is_declaration,
21367 so we do the same. */
21368 gdb_assert (die_is_declaration (die, cu));
21369 gdb_assert (attr);
21370 }
21371 if (attr)
21372 {
21373 dwarf2_const_value (attr, sym, cu);
21374 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21375 if (!suppress_add)
21376 {
21377 if (attr2 && (DW_UNSND (attr2) != 0))
21378 list_to_add = &global_symbols;
21379 else
21380 list_to_add = cu->list_in_scope;
21381 }
21382 break;
21383 }
21384 attr = dwarf2_attr (die, DW_AT_location, cu);
21385 if (attr)
21386 {
21387 var_decode_location (attr, sym, cu);
21388 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21389
21390 /* Fortran explicitly imports any global symbols to the local
21391 scope by DW_TAG_common_block. */
21392 if (cu->language == language_fortran && die->parent
21393 && die->parent->tag == DW_TAG_common_block)
21394 attr2 = NULL;
21395
21396 if (SYMBOL_CLASS (sym) == LOC_STATIC
21397 && SYMBOL_VALUE_ADDRESS (sym) == 0
21398 && !dwarf2_per_objfile->has_section_at_zero)
21399 {
21400 /* When a static variable is eliminated by the linker,
21401 the corresponding debug information is not stripped
21402 out, but the variable address is set to null;
21403 do not add such variables into symbol table. */
21404 }
21405 else if (attr2 && (DW_UNSND (attr2) != 0))
21406 {
21407 /* Workaround gfortran PR debug/40040 - it uses
21408 DW_AT_location for variables in -fPIC libraries which may
21409 get overriden by other libraries/executable and get
21410 a different address. Resolve it by the minimal symbol
21411 which may come from inferior's executable using copy
21412 relocation. Make this workaround only for gfortran as for
21413 other compilers GDB cannot guess the minimal symbol
21414 Fortran mangling kind. */
21415 if (cu->language == language_fortran && die->parent
21416 && die->parent->tag == DW_TAG_module
21417 && cu->producer
21418 && startswith (cu->producer, "GNU Fortran"))
21419 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21420
21421 /* A variable with DW_AT_external is never static,
21422 but it may be block-scoped. */
21423 list_to_add = (cu->list_in_scope == &file_symbols
21424 ? &global_symbols : cu->list_in_scope);
21425 }
21426 else
21427 list_to_add = cu->list_in_scope;
21428 }
21429 else
21430 {
21431 /* We do not know the address of this symbol.
21432 If it is an external symbol and we have type information
21433 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21434 The address of the variable will then be determined from
21435 the minimal symbol table whenever the variable is
21436 referenced. */
21437 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21438
21439 /* Fortran explicitly imports any global symbols to the local
21440 scope by DW_TAG_common_block. */
21441 if (cu->language == language_fortran && die->parent
21442 && die->parent->tag == DW_TAG_common_block)
21443 {
21444 /* SYMBOL_CLASS doesn't matter here because
21445 read_common_block is going to reset it. */
21446 if (!suppress_add)
21447 list_to_add = cu->list_in_scope;
21448 }
21449 else if (attr2 && (DW_UNSND (attr2) != 0)
21450 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21451 {
21452 /* A variable with DW_AT_external is never static, but it
21453 may be block-scoped. */
21454 list_to_add = (cu->list_in_scope == &file_symbols
21455 ? &global_symbols : cu->list_in_scope);
21456
21457 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21458 }
21459 else if (!die_is_declaration (die, cu))
21460 {
21461 /* Use the default LOC_OPTIMIZED_OUT class. */
21462 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21463 if (!suppress_add)
21464 list_to_add = cu->list_in_scope;
21465 }
21466 }
21467 break;
21468 case DW_TAG_formal_parameter:
21469 /* If we are inside a function, mark this as an argument. If
21470 not, we might be looking at an argument to an inlined function
21471 when we do not have enough information to show inlined frames;
21472 pretend it's a local variable in that case so that the user can
21473 still see it. */
21474 if (context_stack_depth > 0
21475 && context_stack[context_stack_depth - 1].name != NULL)
21476 SYMBOL_IS_ARGUMENT (sym) = 1;
21477 attr = dwarf2_attr (die, DW_AT_location, cu);
21478 if (attr)
21479 {
21480 var_decode_location (attr, sym, cu);
21481 }
21482 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21483 if (attr)
21484 {
21485 dwarf2_const_value (attr, sym, cu);
21486 }
21487
21488 list_to_add = cu->list_in_scope;
21489 break;
21490 case DW_TAG_unspecified_parameters:
21491 /* From varargs functions; gdb doesn't seem to have any
21492 interest in this information, so just ignore it for now.
21493 (FIXME?) */
21494 break;
21495 case DW_TAG_template_type_param:
21496 suppress_add = 1;
21497 /* Fall through. */
21498 case DW_TAG_class_type:
21499 case DW_TAG_interface_type:
21500 case DW_TAG_structure_type:
21501 case DW_TAG_union_type:
21502 case DW_TAG_set_type:
21503 case DW_TAG_enumeration_type:
21504 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21505 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21506
21507 {
21508 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21509 really ever be static objects: otherwise, if you try
21510 to, say, break of a class's method and you're in a file
21511 which doesn't mention that class, it won't work unless
21512 the check for all static symbols in lookup_symbol_aux
21513 saves you. See the OtherFileClass tests in
21514 gdb.c++/namespace.exp. */
21515
21516 if (!suppress_add)
21517 {
21518 list_to_add = (cu->list_in_scope == &file_symbols
21519 && cu->language == language_cplus
21520 ? &global_symbols : cu->list_in_scope);
21521
21522 /* The semantics of C++ state that "struct foo {
21523 ... }" also defines a typedef for "foo". */
21524 if (cu->language == language_cplus
21525 || cu->language == language_ada
21526 || cu->language == language_d
21527 || cu->language == language_rust)
21528 {
21529 /* The symbol's name is already allocated along
21530 with this objfile, so we don't need to
21531 duplicate it for the type. */
21532 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21533 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
21534 }
21535 }
21536 }
21537 break;
21538 case DW_TAG_typedef:
21539 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21540 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21541 list_to_add = cu->list_in_scope;
21542 break;
21543 case DW_TAG_base_type:
21544 case DW_TAG_subrange_type:
21545 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21546 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21547 list_to_add = cu->list_in_scope;
21548 break;
21549 case DW_TAG_enumerator:
21550 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21551 if (attr)
21552 {
21553 dwarf2_const_value (attr, sym, cu);
21554 }
21555 {
21556 /* NOTE: carlton/2003-11-10: See comment above in the
21557 DW_TAG_class_type, etc. block. */
21558
21559 list_to_add = (cu->list_in_scope == &file_symbols
21560 && cu->language == language_cplus
21561 ? &global_symbols : cu->list_in_scope);
21562 }
21563 break;
21564 case DW_TAG_imported_declaration:
21565 case DW_TAG_namespace:
21566 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21567 list_to_add = &global_symbols;
21568 break;
21569 case DW_TAG_module:
21570 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21571 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21572 list_to_add = &global_symbols;
21573 break;
21574 case DW_TAG_common_block:
21575 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21576 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21577 add_symbol_to_list (sym, cu->list_in_scope);
21578 break;
21579 default:
21580 /* Not a tag we recognize. Hopefully we aren't processing
21581 trash data, but since we must specifically ignore things
21582 we don't recognize, there is nothing else we should do at
21583 this point. */
21584 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
21585 dwarf_tag_name (die->tag));
21586 break;
21587 }
21588
21589 if (suppress_add)
21590 {
21591 sym->hash_next = objfile->template_symbols;
21592 objfile->template_symbols = sym;
21593 list_to_add = NULL;
21594 }
21595
21596 if (list_to_add != NULL)
21597 add_symbol_to_list (sym, list_to_add);
21598
21599 /* For the benefit of old versions of GCC, check for anonymous
21600 namespaces based on the demangled name. */
21601 if (!cu->processing_has_namespace_info
21602 && cu->language == language_cplus)
21603 cp_scan_for_anonymous_namespaces (sym, objfile);
21604 }
21605 return (sym);
21606 }
21607
21608 /* Given an attr with a DW_FORM_dataN value in host byte order,
21609 zero-extend it as appropriate for the symbol's type. The DWARF
21610 standard (v4) is not entirely clear about the meaning of using
21611 DW_FORM_dataN for a constant with a signed type, where the type is
21612 wider than the data. The conclusion of a discussion on the DWARF
21613 list was that this is unspecified. We choose to always zero-extend
21614 because that is the interpretation long in use by GCC. */
21615
21616 static gdb_byte *
21617 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21618 struct dwarf2_cu *cu, LONGEST *value, int bits)
21619 {
21620 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21621 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21622 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21623 LONGEST l = DW_UNSND (attr);
21624
21625 if (bits < sizeof (*value) * 8)
21626 {
21627 l &= ((LONGEST) 1 << bits) - 1;
21628 *value = l;
21629 }
21630 else if (bits == sizeof (*value) * 8)
21631 *value = l;
21632 else
21633 {
21634 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21635 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21636 return bytes;
21637 }
21638
21639 return NULL;
21640 }
21641
21642 /* Read a constant value from an attribute. Either set *VALUE, or if
21643 the value does not fit in *VALUE, set *BYTES - either already
21644 allocated on the objfile obstack, or newly allocated on OBSTACK,
21645 or, set *BATON, if we translated the constant to a location
21646 expression. */
21647
21648 static void
21649 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21650 const char *name, struct obstack *obstack,
21651 struct dwarf2_cu *cu,
21652 LONGEST *value, const gdb_byte **bytes,
21653 struct dwarf2_locexpr_baton **baton)
21654 {
21655 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21656 struct comp_unit_head *cu_header = &cu->header;
21657 struct dwarf_block *blk;
21658 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21659 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21660
21661 *value = 0;
21662 *bytes = NULL;
21663 *baton = NULL;
21664
21665 switch (attr->form)
21666 {
21667 case DW_FORM_addr:
21668 case DW_FORM_GNU_addr_index:
21669 {
21670 gdb_byte *data;
21671
21672 if (TYPE_LENGTH (type) != cu_header->addr_size)
21673 dwarf2_const_value_length_mismatch_complaint (name,
21674 cu_header->addr_size,
21675 TYPE_LENGTH (type));
21676 /* Symbols of this form are reasonably rare, so we just
21677 piggyback on the existing location code rather than writing
21678 a new implementation of symbol_computed_ops. */
21679 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21680 (*baton)->per_cu = cu->per_cu;
21681 gdb_assert ((*baton)->per_cu);
21682
21683 (*baton)->size = 2 + cu_header->addr_size;
21684 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21685 (*baton)->data = data;
21686
21687 data[0] = DW_OP_addr;
21688 store_unsigned_integer (&data[1], cu_header->addr_size,
21689 byte_order, DW_ADDR (attr));
21690 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21691 }
21692 break;
21693 case DW_FORM_string:
21694 case DW_FORM_strp:
21695 case DW_FORM_GNU_str_index:
21696 case DW_FORM_GNU_strp_alt:
21697 /* DW_STRING is already allocated on the objfile obstack, point
21698 directly to it. */
21699 *bytes = (const gdb_byte *) DW_STRING (attr);
21700 break;
21701 case DW_FORM_block1:
21702 case DW_FORM_block2:
21703 case DW_FORM_block4:
21704 case DW_FORM_block:
21705 case DW_FORM_exprloc:
21706 case DW_FORM_data16:
21707 blk = DW_BLOCK (attr);
21708 if (TYPE_LENGTH (type) != blk->size)
21709 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21710 TYPE_LENGTH (type));
21711 *bytes = blk->data;
21712 break;
21713
21714 /* The DW_AT_const_value attributes are supposed to carry the
21715 symbol's value "represented as it would be on the target
21716 architecture." By the time we get here, it's already been
21717 converted to host endianness, so we just need to sign- or
21718 zero-extend it as appropriate. */
21719 case DW_FORM_data1:
21720 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21721 break;
21722 case DW_FORM_data2:
21723 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21724 break;
21725 case DW_FORM_data4:
21726 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21727 break;
21728 case DW_FORM_data8:
21729 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21730 break;
21731
21732 case DW_FORM_sdata:
21733 case DW_FORM_implicit_const:
21734 *value = DW_SND (attr);
21735 break;
21736
21737 case DW_FORM_udata:
21738 *value = DW_UNSND (attr);
21739 break;
21740
21741 default:
21742 complaint (&symfile_complaints,
21743 _("unsupported const value attribute form: '%s'"),
21744 dwarf_form_name (attr->form));
21745 *value = 0;
21746 break;
21747 }
21748 }
21749
21750
21751 /* Copy constant value from an attribute to a symbol. */
21752
21753 static void
21754 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21755 struct dwarf2_cu *cu)
21756 {
21757 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21758 LONGEST value;
21759 const gdb_byte *bytes;
21760 struct dwarf2_locexpr_baton *baton;
21761
21762 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21763 SYMBOL_PRINT_NAME (sym),
21764 &objfile->objfile_obstack, cu,
21765 &value, &bytes, &baton);
21766
21767 if (baton != NULL)
21768 {
21769 SYMBOL_LOCATION_BATON (sym) = baton;
21770 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21771 }
21772 else if (bytes != NULL)
21773 {
21774 SYMBOL_VALUE_BYTES (sym) = bytes;
21775 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21776 }
21777 else
21778 {
21779 SYMBOL_VALUE (sym) = value;
21780 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21781 }
21782 }
21783
21784 /* Return the type of the die in question using its DW_AT_type attribute. */
21785
21786 static struct type *
21787 die_type (struct die_info *die, struct dwarf2_cu *cu)
21788 {
21789 struct attribute *type_attr;
21790
21791 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21792 if (!type_attr)
21793 {
21794 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21795 /* A missing DW_AT_type represents a void type. */
21796 return objfile_type (objfile)->builtin_void;
21797 }
21798
21799 return lookup_die_type (die, type_attr, cu);
21800 }
21801
21802 /* True iff CU's producer generates GNAT Ada auxiliary information
21803 that allows to find parallel types through that information instead
21804 of having to do expensive parallel lookups by type name. */
21805
21806 static int
21807 need_gnat_info (struct dwarf2_cu *cu)
21808 {
21809 /* Assume that the Ada compiler was GNAT, which always produces
21810 the auxiliary information. */
21811 return (cu->language == language_ada);
21812 }
21813
21814 /* Return the auxiliary type of the die in question using its
21815 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21816 attribute is not present. */
21817
21818 static struct type *
21819 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21820 {
21821 struct attribute *type_attr;
21822
21823 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21824 if (!type_attr)
21825 return NULL;
21826
21827 return lookup_die_type (die, type_attr, cu);
21828 }
21829
21830 /* If DIE has a descriptive_type attribute, then set the TYPE's
21831 descriptive type accordingly. */
21832
21833 static void
21834 set_descriptive_type (struct type *type, struct die_info *die,
21835 struct dwarf2_cu *cu)
21836 {
21837 struct type *descriptive_type = die_descriptive_type (die, cu);
21838
21839 if (descriptive_type)
21840 {
21841 ALLOCATE_GNAT_AUX_TYPE (type);
21842 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21843 }
21844 }
21845
21846 /* Return the containing type of the die in question using its
21847 DW_AT_containing_type attribute. */
21848
21849 static struct type *
21850 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21851 {
21852 struct attribute *type_attr;
21853 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21854
21855 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21856 if (!type_attr)
21857 error (_("Dwarf Error: Problem turning containing type into gdb type "
21858 "[in module %s]"), objfile_name (objfile));
21859
21860 return lookup_die_type (die, type_attr, cu);
21861 }
21862
21863 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21864
21865 static struct type *
21866 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21867 {
21868 struct dwarf2_per_objfile *dwarf2_per_objfile
21869 = cu->per_cu->dwarf2_per_objfile;
21870 struct objfile *objfile = dwarf2_per_objfile->objfile;
21871 char *message, *saved;
21872
21873 message = xstrprintf (_("<unknown type in %s, CU %s, DIE %s>"),
21874 objfile_name (objfile),
21875 sect_offset_str (cu->header.sect_off),
21876 sect_offset_str (die->sect_off));
21877 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
21878 message, strlen (message));
21879 xfree (message);
21880
21881 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21882 }
21883
21884 /* Look up the type of DIE in CU using its type attribute ATTR.
21885 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21886 DW_AT_containing_type.
21887 If there is no type substitute an error marker. */
21888
21889 static struct type *
21890 lookup_die_type (struct die_info *die, const struct attribute *attr,
21891 struct dwarf2_cu *cu)
21892 {
21893 struct dwarf2_per_objfile *dwarf2_per_objfile
21894 = cu->per_cu->dwarf2_per_objfile;
21895 struct objfile *objfile = dwarf2_per_objfile->objfile;
21896 struct type *this_type;
21897
21898 gdb_assert (attr->name == DW_AT_type
21899 || attr->name == DW_AT_GNAT_descriptive_type
21900 || attr->name == DW_AT_containing_type);
21901
21902 /* First see if we have it cached. */
21903
21904 if (attr->form == DW_FORM_GNU_ref_alt)
21905 {
21906 struct dwarf2_per_cu_data *per_cu;
21907 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21908
21909 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21910 dwarf2_per_objfile);
21911 this_type = get_die_type_at_offset (sect_off, per_cu);
21912 }
21913 else if (attr_form_is_ref (attr))
21914 {
21915 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21916
21917 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
21918 }
21919 else if (attr->form == DW_FORM_ref_sig8)
21920 {
21921 ULONGEST signature = DW_SIGNATURE (attr);
21922
21923 return get_signatured_type (die, signature, cu);
21924 }
21925 else
21926 {
21927 complaint (&symfile_complaints,
21928 _("Dwarf Error: Bad type attribute %s in DIE"
21929 " at %s [in module %s]"),
21930 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21931 objfile_name (objfile));
21932 return build_error_marker_type (cu, die);
21933 }
21934
21935 /* If not cached we need to read it in. */
21936
21937 if (this_type == NULL)
21938 {
21939 struct die_info *type_die = NULL;
21940 struct dwarf2_cu *type_cu = cu;
21941
21942 if (attr_form_is_ref (attr))
21943 type_die = follow_die_ref (die, attr, &type_cu);
21944 if (type_die == NULL)
21945 return build_error_marker_type (cu, die);
21946 /* If we find the type now, it's probably because the type came
21947 from an inter-CU reference and the type's CU got expanded before
21948 ours. */
21949 this_type = read_type_die (type_die, type_cu);
21950 }
21951
21952 /* If we still don't have a type use an error marker. */
21953
21954 if (this_type == NULL)
21955 return build_error_marker_type (cu, die);
21956
21957 return this_type;
21958 }
21959
21960 /* Return the type in DIE, CU.
21961 Returns NULL for invalid types.
21962
21963 This first does a lookup in die_type_hash,
21964 and only reads the die in if necessary.
21965
21966 NOTE: This can be called when reading in partial or full symbols. */
21967
21968 static struct type *
21969 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21970 {
21971 struct type *this_type;
21972
21973 this_type = get_die_type (die, cu);
21974 if (this_type)
21975 return this_type;
21976
21977 return read_type_die_1 (die, cu);
21978 }
21979
21980 /* Read the type in DIE, CU.
21981 Returns NULL for invalid types. */
21982
21983 static struct type *
21984 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21985 {
21986 struct type *this_type = NULL;
21987
21988 switch (die->tag)
21989 {
21990 case DW_TAG_class_type:
21991 case DW_TAG_interface_type:
21992 case DW_TAG_structure_type:
21993 case DW_TAG_union_type:
21994 this_type = read_structure_type (die, cu);
21995 break;
21996 case DW_TAG_enumeration_type:
21997 this_type = read_enumeration_type (die, cu);
21998 break;
21999 case DW_TAG_subprogram:
22000 case DW_TAG_subroutine_type:
22001 case DW_TAG_inlined_subroutine:
22002 this_type = read_subroutine_type (die, cu);
22003 break;
22004 case DW_TAG_array_type:
22005 this_type = read_array_type (die, cu);
22006 break;
22007 case DW_TAG_set_type:
22008 this_type = read_set_type (die, cu);
22009 break;
22010 case DW_TAG_pointer_type:
22011 this_type = read_tag_pointer_type (die, cu);
22012 break;
22013 case DW_TAG_ptr_to_member_type:
22014 this_type = read_tag_ptr_to_member_type (die, cu);
22015 break;
22016 case DW_TAG_reference_type:
22017 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22018 break;
22019 case DW_TAG_rvalue_reference_type:
22020 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22021 break;
22022 case DW_TAG_const_type:
22023 this_type = read_tag_const_type (die, cu);
22024 break;
22025 case DW_TAG_volatile_type:
22026 this_type = read_tag_volatile_type (die, cu);
22027 break;
22028 case DW_TAG_restrict_type:
22029 this_type = read_tag_restrict_type (die, cu);
22030 break;
22031 case DW_TAG_string_type:
22032 this_type = read_tag_string_type (die, cu);
22033 break;
22034 case DW_TAG_typedef:
22035 this_type = read_typedef (die, cu);
22036 break;
22037 case DW_TAG_subrange_type:
22038 this_type = read_subrange_type (die, cu);
22039 break;
22040 case DW_TAG_base_type:
22041 this_type = read_base_type (die, cu);
22042 break;
22043 case DW_TAG_unspecified_type:
22044 this_type = read_unspecified_type (die, cu);
22045 break;
22046 case DW_TAG_namespace:
22047 this_type = read_namespace_type (die, cu);
22048 break;
22049 case DW_TAG_module:
22050 this_type = read_module_type (die, cu);
22051 break;
22052 case DW_TAG_atomic_type:
22053 this_type = read_tag_atomic_type (die, cu);
22054 break;
22055 default:
22056 complaint (&symfile_complaints,
22057 _("unexpected tag in read_type_die: '%s'"),
22058 dwarf_tag_name (die->tag));
22059 break;
22060 }
22061
22062 return this_type;
22063 }
22064
22065 /* See if we can figure out if the class lives in a namespace. We do
22066 this by looking for a member function; its demangled name will
22067 contain namespace info, if there is any.
22068 Return the computed name or NULL.
22069 Space for the result is allocated on the objfile's obstack.
22070 This is the full-die version of guess_partial_die_structure_name.
22071 In this case we know DIE has no useful parent. */
22072
22073 static char *
22074 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22075 {
22076 struct die_info *spec_die;
22077 struct dwarf2_cu *spec_cu;
22078 struct die_info *child;
22079 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22080
22081 spec_cu = cu;
22082 spec_die = die_specification (die, &spec_cu);
22083 if (spec_die != NULL)
22084 {
22085 die = spec_die;
22086 cu = spec_cu;
22087 }
22088
22089 for (child = die->child;
22090 child != NULL;
22091 child = child->sibling)
22092 {
22093 if (child->tag == DW_TAG_subprogram)
22094 {
22095 const char *linkage_name = dw2_linkage_name (child, cu);
22096
22097 if (linkage_name != NULL)
22098 {
22099 char *actual_name
22100 = language_class_name_from_physname (cu->language_defn,
22101 linkage_name);
22102 char *name = NULL;
22103
22104 if (actual_name != NULL)
22105 {
22106 const char *die_name = dwarf2_name (die, cu);
22107
22108 if (die_name != NULL
22109 && strcmp (die_name, actual_name) != 0)
22110 {
22111 /* Strip off the class name from the full name.
22112 We want the prefix. */
22113 int die_name_len = strlen (die_name);
22114 int actual_name_len = strlen (actual_name);
22115
22116 /* Test for '::' as a sanity check. */
22117 if (actual_name_len > die_name_len + 2
22118 && actual_name[actual_name_len
22119 - die_name_len - 1] == ':')
22120 name = (char *) obstack_copy0 (
22121 &objfile->per_bfd->storage_obstack,
22122 actual_name, actual_name_len - die_name_len - 2);
22123 }
22124 }
22125 xfree (actual_name);
22126 return name;
22127 }
22128 }
22129 }
22130
22131 return NULL;
22132 }
22133
22134 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22135 prefix part in such case. See
22136 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22137
22138 static const char *
22139 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22140 {
22141 struct attribute *attr;
22142 const char *base;
22143
22144 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22145 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22146 return NULL;
22147
22148 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22149 return NULL;
22150
22151 attr = dw2_linkage_name_attr (die, cu);
22152 if (attr == NULL || DW_STRING (attr) == NULL)
22153 return NULL;
22154
22155 /* dwarf2_name had to be already called. */
22156 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22157
22158 /* Strip the base name, keep any leading namespaces/classes. */
22159 base = strrchr (DW_STRING (attr), ':');
22160 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22161 return "";
22162
22163 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22164 return (char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
22165 DW_STRING (attr),
22166 &base[-1] - DW_STRING (attr));
22167 }
22168
22169 /* Return the name of the namespace/class that DIE is defined within,
22170 or "" if we can't tell. The caller should not xfree the result.
22171
22172 For example, if we're within the method foo() in the following
22173 code:
22174
22175 namespace N {
22176 class C {
22177 void foo () {
22178 }
22179 };
22180 }
22181
22182 then determine_prefix on foo's die will return "N::C". */
22183
22184 static const char *
22185 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22186 {
22187 struct dwarf2_per_objfile *dwarf2_per_objfile
22188 = cu->per_cu->dwarf2_per_objfile;
22189 struct die_info *parent, *spec_die;
22190 struct dwarf2_cu *spec_cu;
22191 struct type *parent_type;
22192 const char *retval;
22193
22194 if (cu->language != language_cplus
22195 && cu->language != language_fortran && cu->language != language_d
22196 && cu->language != language_rust)
22197 return "";
22198
22199 retval = anonymous_struct_prefix (die, cu);
22200 if (retval)
22201 return retval;
22202
22203 /* We have to be careful in the presence of DW_AT_specification.
22204 For example, with GCC 3.4, given the code
22205
22206 namespace N {
22207 void foo() {
22208 // Definition of N::foo.
22209 }
22210 }
22211
22212 then we'll have a tree of DIEs like this:
22213
22214 1: DW_TAG_compile_unit
22215 2: DW_TAG_namespace // N
22216 3: DW_TAG_subprogram // declaration of N::foo
22217 4: DW_TAG_subprogram // definition of N::foo
22218 DW_AT_specification // refers to die #3
22219
22220 Thus, when processing die #4, we have to pretend that we're in
22221 the context of its DW_AT_specification, namely the contex of die
22222 #3. */
22223 spec_cu = cu;
22224 spec_die = die_specification (die, &spec_cu);
22225 if (spec_die == NULL)
22226 parent = die->parent;
22227 else
22228 {
22229 parent = spec_die->parent;
22230 cu = spec_cu;
22231 }
22232
22233 if (parent == NULL)
22234 return "";
22235 else if (parent->building_fullname)
22236 {
22237 const char *name;
22238 const char *parent_name;
22239
22240 /* It has been seen on RealView 2.2 built binaries,
22241 DW_TAG_template_type_param types actually _defined_ as
22242 children of the parent class:
22243
22244 enum E {};
22245 template class <class Enum> Class{};
22246 Class<enum E> class_e;
22247
22248 1: DW_TAG_class_type (Class)
22249 2: DW_TAG_enumeration_type (E)
22250 3: DW_TAG_enumerator (enum1:0)
22251 3: DW_TAG_enumerator (enum2:1)
22252 ...
22253 2: DW_TAG_template_type_param
22254 DW_AT_type DW_FORM_ref_udata (E)
22255
22256 Besides being broken debug info, it can put GDB into an
22257 infinite loop. Consider:
22258
22259 When we're building the full name for Class<E>, we'll start
22260 at Class, and go look over its template type parameters,
22261 finding E. We'll then try to build the full name of E, and
22262 reach here. We're now trying to build the full name of E,
22263 and look over the parent DIE for containing scope. In the
22264 broken case, if we followed the parent DIE of E, we'd again
22265 find Class, and once again go look at its template type
22266 arguments, etc., etc. Simply don't consider such parent die
22267 as source-level parent of this die (it can't be, the language
22268 doesn't allow it), and break the loop here. */
22269 name = dwarf2_name (die, cu);
22270 parent_name = dwarf2_name (parent, cu);
22271 complaint (&symfile_complaints,
22272 _("template param type '%s' defined within parent '%s'"),
22273 name ? name : "<unknown>",
22274 parent_name ? parent_name : "<unknown>");
22275 return "";
22276 }
22277 else
22278 switch (parent->tag)
22279 {
22280 case DW_TAG_namespace:
22281 parent_type = read_type_die (parent, cu);
22282 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22283 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22284 Work around this problem here. */
22285 if (cu->language == language_cplus
22286 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
22287 return "";
22288 /* We give a name to even anonymous namespaces. */
22289 return TYPE_TAG_NAME (parent_type);
22290 case DW_TAG_class_type:
22291 case DW_TAG_interface_type:
22292 case DW_TAG_structure_type:
22293 case DW_TAG_union_type:
22294 case DW_TAG_module:
22295 parent_type = read_type_die (parent, cu);
22296 if (TYPE_TAG_NAME (parent_type) != NULL)
22297 return TYPE_TAG_NAME (parent_type);
22298 else
22299 /* An anonymous structure is only allowed non-static data
22300 members; no typedefs, no member functions, et cetera.
22301 So it does not need a prefix. */
22302 return "";
22303 case DW_TAG_compile_unit:
22304 case DW_TAG_partial_unit:
22305 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22306 if (cu->language == language_cplus
22307 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
22308 && die->child != NULL
22309 && (die->tag == DW_TAG_class_type
22310 || die->tag == DW_TAG_structure_type
22311 || die->tag == DW_TAG_union_type))
22312 {
22313 char *name = guess_full_die_structure_name (die, cu);
22314 if (name != NULL)
22315 return name;
22316 }
22317 return "";
22318 case DW_TAG_enumeration_type:
22319 parent_type = read_type_die (parent, cu);
22320 if (TYPE_DECLARED_CLASS (parent_type))
22321 {
22322 if (TYPE_TAG_NAME (parent_type) != NULL)
22323 return TYPE_TAG_NAME (parent_type);
22324 return "";
22325 }
22326 /* Fall through. */
22327 default:
22328 return determine_prefix (parent, cu);
22329 }
22330 }
22331
22332 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22333 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22334 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22335 an obconcat, otherwise allocate storage for the result. The CU argument is
22336 used to determine the language and hence, the appropriate separator. */
22337
22338 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22339
22340 static char *
22341 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22342 int physname, struct dwarf2_cu *cu)
22343 {
22344 const char *lead = "";
22345 const char *sep;
22346
22347 if (suffix == NULL || suffix[0] == '\0'
22348 || prefix == NULL || prefix[0] == '\0')
22349 sep = "";
22350 else if (cu->language == language_d)
22351 {
22352 /* For D, the 'main' function could be defined in any module, but it
22353 should never be prefixed. */
22354 if (strcmp (suffix, "D main") == 0)
22355 {
22356 prefix = "";
22357 sep = "";
22358 }
22359 else
22360 sep = ".";
22361 }
22362 else if (cu->language == language_fortran && physname)
22363 {
22364 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22365 DW_AT_MIPS_linkage_name is preferred and used instead. */
22366
22367 lead = "__";
22368 sep = "_MOD_";
22369 }
22370 else
22371 sep = "::";
22372
22373 if (prefix == NULL)
22374 prefix = "";
22375 if (suffix == NULL)
22376 suffix = "";
22377
22378 if (obs == NULL)
22379 {
22380 char *retval
22381 = ((char *)
22382 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22383
22384 strcpy (retval, lead);
22385 strcat (retval, prefix);
22386 strcat (retval, sep);
22387 strcat (retval, suffix);
22388 return retval;
22389 }
22390 else
22391 {
22392 /* We have an obstack. */
22393 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22394 }
22395 }
22396
22397 /* Return sibling of die, NULL if no sibling. */
22398
22399 static struct die_info *
22400 sibling_die (struct die_info *die)
22401 {
22402 return die->sibling;
22403 }
22404
22405 /* Get name of a die, return NULL if not found. */
22406
22407 static const char *
22408 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22409 struct obstack *obstack)
22410 {
22411 if (name && cu->language == language_cplus)
22412 {
22413 std::string canon_name = cp_canonicalize_string (name);
22414
22415 if (!canon_name.empty ())
22416 {
22417 if (canon_name != name)
22418 name = (const char *) obstack_copy0 (obstack,
22419 canon_name.c_str (),
22420 canon_name.length ());
22421 }
22422 }
22423
22424 return name;
22425 }
22426
22427 /* Get name of a die, return NULL if not found.
22428 Anonymous namespaces are converted to their magic string. */
22429
22430 static const char *
22431 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22432 {
22433 struct attribute *attr;
22434 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22435
22436 attr = dwarf2_attr (die, DW_AT_name, cu);
22437 if ((!attr || !DW_STRING (attr))
22438 && die->tag != DW_TAG_namespace
22439 && die->tag != DW_TAG_class_type
22440 && die->tag != DW_TAG_interface_type
22441 && die->tag != DW_TAG_structure_type
22442 && die->tag != DW_TAG_union_type)
22443 return NULL;
22444
22445 switch (die->tag)
22446 {
22447 case DW_TAG_compile_unit:
22448 case DW_TAG_partial_unit:
22449 /* Compilation units have a DW_AT_name that is a filename, not
22450 a source language identifier. */
22451 case DW_TAG_enumeration_type:
22452 case DW_TAG_enumerator:
22453 /* These tags always have simple identifiers already; no need
22454 to canonicalize them. */
22455 return DW_STRING (attr);
22456
22457 case DW_TAG_namespace:
22458 if (attr != NULL && DW_STRING (attr) != NULL)
22459 return DW_STRING (attr);
22460 return CP_ANONYMOUS_NAMESPACE_STR;
22461
22462 case DW_TAG_class_type:
22463 case DW_TAG_interface_type:
22464 case DW_TAG_structure_type:
22465 case DW_TAG_union_type:
22466 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22467 structures or unions. These were of the form "._%d" in GCC 4.1,
22468 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22469 and GCC 4.4. We work around this problem by ignoring these. */
22470 if (attr && DW_STRING (attr)
22471 && (startswith (DW_STRING (attr), "._")
22472 || startswith (DW_STRING (attr), "<anonymous")))
22473 return NULL;
22474
22475 /* GCC might emit a nameless typedef that has a linkage name. See
22476 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22477 if (!attr || DW_STRING (attr) == NULL)
22478 {
22479 char *demangled = NULL;
22480
22481 attr = dw2_linkage_name_attr (die, cu);
22482 if (attr == NULL || DW_STRING (attr) == NULL)
22483 return NULL;
22484
22485 /* Avoid demangling DW_STRING (attr) the second time on a second
22486 call for the same DIE. */
22487 if (!DW_STRING_IS_CANONICAL (attr))
22488 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
22489
22490 if (demangled)
22491 {
22492 const char *base;
22493
22494 /* FIXME: we already did this for the partial symbol... */
22495 DW_STRING (attr)
22496 = ((const char *)
22497 obstack_copy0 (&objfile->per_bfd->storage_obstack,
22498 demangled, strlen (demangled)));
22499 DW_STRING_IS_CANONICAL (attr) = 1;
22500 xfree (demangled);
22501
22502 /* Strip any leading namespaces/classes, keep only the base name.
22503 DW_AT_name for named DIEs does not contain the prefixes. */
22504 base = strrchr (DW_STRING (attr), ':');
22505 if (base && base > DW_STRING (attr) && base[-1] == ':')
22506 return &base[1];
22507 else
22508 return DW_STRING (attr);
22509 }
22510 }
22511 break;
22512
22513 default:
22514 break;
22515 }
22516
22517 if (!DW_STRING_IS_CANONICAL (attr))
22518 {
22519 DW_STRING (attr)
22520 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22521 &objfile->per_bfd->storage_obstack);
22522 DW_STRING_IS_CANONICAL (attr) = 1;
22523 }
22524 return DW_STRING (attr);
22525 }
22526
22527 /* Return the die that this die in an extension of, or NULL if there
22528 is none. *EXT_CU is the CU containing DIE on input, and the CU
22529 containing the return value on output. */
22530
22531 static struct die_info *
22532 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22533 {
22534 struct attribute *attr;
22535
22536 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22537 if (attr == NULL)
22538 return NULL;
22539
22540 return follow_die_ref (die, attr, ext_cu);
22541 }
22542
22543 /* Convert a DIE tag into its string name. */
22544
22545 static const char *
22546 dwarf_tag_name (unsigned tag)
22547 {
22548 const char *name = get_DW_TAG_name (tag);
22549
22550 if (name == NULL)
22551 return "DW_TAG_<unknown>";
22552
22553 return name;
22554 }
22555
22556 /* Convert a DWARF attribute code into its string name. */
22557
22558 static const char *
22559 dwarf_attr_name (unsigned attr)
22560 {
22561 const char *name;
22562
22563 #ifdef MIPS /* collides with DW_AT_HP_block_index */
22564 if (attr == DW_AT_MIPS_fde)
22565 return "DW_AT_MIPS_fde";
22566 #else
22567 if (attr == DW_AT_HP_block_index)
22568 return "DW_AT_HP_block_index";
22569 #endif
22570
22571 name = get_DW_AT_name (attr);
22572
22573 if (name == NULL)
22574 return "DW_AT_<unknown>";
22575
22576 return name;
22577 }
22578
22579 /* Convert a DWARF value form code into its string name. */
22580
22581 static const char *
22582 dwarf_form_name (unsigned form)
22583 {
22584 const char *name = get_DW_FORM_name (form);
22585
22586 if (name == NULL)
22587 return "DW_FORM_<unknown>";
22588
22589 return name;
22590 }
22591
22592 static const char *
22593 dwarf_bool_name (unsigned mybool)
22594 {
22595 if (mybool)
22596 return "TRUE";
22597 else
22598 return "FALSE";
22599 }
22600
22601 /* Convert a DWARF type code into its string name. */
22602
22603 static const char *
22604 dwarf_type_encoding_name (unsigned enc)
22605 {
22606 const char *name = get_DW_ATE_name (enc);
22607
22608 if (name == NULL)
22609 return "DW_ATE_<unknown>";
22610
22611 return name;
22612 }
22613
22614 static void
22615 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22616 {
22617 unsigned int i;
22618
22619 print_spaces (indent, f);
22620 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
22621 dwarf_tag_name (die->tag), die->abbrev,
22622 sect_offset_str (die->sect_off));
22623
22624 if (die->parent != NULL)
22625 {
22626 print_spaces (indent, f);
22627 fprintf_unfiltered (f, " parent at offset: %s\n",
22628 sect_offset_str (die->parent->sect_off));
22629 }
22630
22631 print_spaces (indent, f);
22632 fprintf_unfiltered (f, " has children: %s\n",
22633 dwarf_bool_name (die->child != NULL));
22634
22635 print_spaces (indent, f);
22636 fprintf_unfiltered (f, " attributes:\n");
22637
22638 for (i = 0; i < die->num_attrs; ++i)
22639 {
22640 print_spaces (indent, f);
22641 fprintf_unfiltered (f, " %s (%s) ",
22642 dwarf_attr_name (die->attrs[i].name),
22643 dwarf_form_name (die->attrs[i].form));
22644
22645 switch (die->attrs[i].form)
22646 {
22647 case DW_FORM_addr:
22648 case DW_FORM_GNU_addr_index:
22649 fprintf_unfiltered (f, "address: ");
22650 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
22651 break;
22652 case DW_FORM_block2:
22653 case DW_FORM_block4:
22654 case DW_FORM_block:
22655 case DW_FORM_block1:
22656 fprintf_unfiltered (f, "block: size %s",
22657 pulongest (DW_BLOCK (&die->attrs[i])->size));
22658 break;
22659 case DW_FORM_exprloc:
22660 fprintf_unfiltered (f, "expression: size %s",
22661 pulongest (DW_BLOCK (&die->attrs[i])->size));
22662 break;
22663 case DW_FORM_data16:
22664 fprintf_unfiltered (f, "constant of 16 bytes");
22665 break;
22666 case DW_FORM_ref_addr:
22667 fprintf_unfiltered (f, "ref address: ");
22668 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22669 break;
22670 case DW_FORM_GNU_ref_alt:
22671 fprintf_unfiltered (f, "alt ref address: ");
22672 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22673 break;
22674 case DW_FORM_ref1:
22675 case DW_FORM_ref2:
22676 case DW_FORM_ref4:
22677 case DW_FORM_ref8:
22678 case DW_FORM_ref_udata:
22679 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22680 (long) (DW_UNSND (&die->attrs[i])));
22681 break;
22682 case DW_FORM_data1:
22683 case DW_FORM_data2:
22684 case DW_FORM_data4:
22685 case DW_FORM_data8:
22686 case DW_FORM_udata:
22687 case DW_FORM_sdata:
22688 fprintf_unfiltered (f, "constant: %s",
22689 pulongest (DW_UNSND (&die->attrs[i])));
22690 break;
22691 case DW_FORM_sec_offset:
22692 fprintf_unfiltered (f, "section offset: %s",
22693 pulongest (DW_UNSND (&die->attrs[i])));
22694 break;
22695 case DW_FORM_ref_sig8:
22696 fprintf_unfiltered (f, "signature: %s",
22697 hex_string (DW_SIGNATURE (&die->attrs[i])));
22698 break;
22699 case DW_FORM_string:
22700 case DW_FORM_strp:
22701 case DW_FORM_line_strp:
22702 case DW_FORM_GNU_str_index:
22703 case DW_FORM_GNU_strp_alt:
22704 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22705 DW_STRING (&die->attrs[i])
22706 ? DW_STRING (&die->attrs[i]) : "",
22707 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22708 break;
22709 case DW_FORM_flag:
22710 if (DW_UNSND (&die->attrs[i]))
22711 fprintf_unfiltered (f, "flag: TRUE");
22712 else
22713 fprintf_unfiltered (f, "flag: FALSE");
22714 break;
22715 case DW_FORM_flag_present:
22716 fprintf_unfiltered (f, "flag: TRUE");
22717 break;
22718 case DW_FORM_indirect:
22719 /* The reader will have reduced the indirect form to
22720 the "base form" so this form should not occur. */
22721 fprintf_unfiltered (f,
22722 "unexpected attribute form: DW_FORM_indirect");
22723 break;
22724 case DW_FORM_implicit_const:
22725 fprintf_unfiltered (f, "constant: %s",
22726 plongest (DW_SND (&die->attrs[i])));
22727 break;
22728 default:
22729 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22730 die->attrs[i].form);
22731 break;
22732 }
22733 fprintf_unfiltered (f, "\n");
22734 }
22735 }
22736
22737 static void
22738 dump_die_for_error (struct die_info *die)
22739 {
22740 dump_die_shallow (gdb_stderr, 0, die);
22741 }
22742
22743 static void
22744 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22745 {
22746 int indent = level * 4;
22747
22748 gdb_assert (die != NULL);
22749
22750 if (level >= max_level)
22751 return;
22752
22753 dump_die_shallow (f, indent, die);
22754
22755 if (die->child != NULL)
22756 {
22757 print_spaces (indent, f);
22758 fprintf_unfiltered (f, " Children:");
22759 if (level + 1 < max_level)
22760 {
22761 fprintf_unfiltered (f, "\n");
22762 dump_die_1 (f, level + 1, max_level, die->child);
22763 }
22764 else
22765 {
22766 fprintf_unfiltered (f,
22767 " [not printed, max nesting level reached]\n");
22768 }
22769 }
22770
22771 if (die->sibling != NULL && level > 0)
22772 {
22773 dump_die_1 (f, level, max_level, die->sibling);
22774 }
22775 }
22776
22777 /* This is called from the pdie macro in gdbinit.in.
22778 It's not static so gcc will keep a copy callable from gdb. */
22779
22780 void
22781 dump_die (struct die_info *die, int max_level)
22782 {
22783 dump_die_1 (gdb_stdlog, 0, max_level, die);
22784 }
22785
22786 static void
22787 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22788 {
22789 void **slot;
22790
22791 slot = htab_find_slot_with_hash (cu->die_hash, die,
22792 to_underlying (die->sect_off),
22793 INSERT);
22794
22795 *slot = die;
22796 }
22797
22798 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22799 required kind. */
22800
22801 static sect_offset
22802 dwarf2_get_ref_die_offset (const struct attribute *attr)
22803 {
22804 if (attr_form_is_ref (attr))
22805 return (sect_offset) DW_UNSND (attr);
22806
22807 complaint (&symfile_complaints,
22808 _("unsupported die ref attribute form: '%s'"),
22809 dwarf_form_name (attr->form));
22810 return {};
22811 }
22812
22813 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22814 * the value held by the attribute is not constant. */
22815
22816 static LONGEST
22817 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
22818 {
22819 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
22820 return DW_SND (attr);
22821 else if (attr->form == DW_FORM_udata
22822 || attr->form == DW_FORM_data1
22823 || attr->form == DW_FORM_data2
22824 || attr->form == DW_FORM_data4
22825 || attr->form == DW_FORM_data8)
22826 return DW_UNSND (attr);
22827 else
22828 {
22829 /* For DW_FORM_data16 see attr_form_is_constant. */
22830 complaint (&symfile_complaints,
22831 _("Attribute value is not a constant (%s)"),
22832 dwarf_form_name (attr->form));
22833 return default_value;
22834 }
22835 }
22836
22837 /* Follow reference or signature attribute ATTR of SRC_DIE.
22838 On entry *REF_CU is the CU of SRC_DIE.
22839 On exit *REF_CU is the CU of the result. */
22840
22841 static struct die_info *
22842 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22843 struct dwarf2_cu **ref_cu)
22844 {
22845 struct die_info *die;
22846
22847 if (attr_form_is_ref (attr))
22848 die = follow_die_ref (src_die, attr, ref_cu);
22849 else if (attr->form == DW_FORM_ref_sig8)
22850 die = follow_die_sig (src_die, attr, ref_cu);
22851 else
22852 {
22853 dump_die_for_error (src_die);
22854 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22855 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22856 }
22857
22858 return die;
22859 }
22860
22861 /* Follow reference OFFSET.
22862 On entry *REF_CU is the CU of the source die referencing OFFSET.
22863 On exit *REF_CU is the CU of the result.
22864 Returns NULL if OFFSET is invalid. */
22865
22866 static struct die_info *
22867 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22868 struct dwarf2_cu **ref_cu)
22869 {
22870 struct die_info temp_die;
22871 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22872 struct dwarf2_per_objfile *dwarf2_per_objfile
22873 = cu->per_cu->dwarf2_per_objfile;
22874
22875 gdb_assert (cu->per_cu != NULL);
22876
22877 target_cu = cu;
22878
22879 if (cu->per_cu->is_debug_types)
22880 {
22881 /* .debug_types CUs cannot reference anything outside their CU.
22882 If they need to, they have to reference a signatured type via
22883 DW_FORM_ref_sig8. */
22884 if (!offset_in_cu_p (&cu->header, sect_off))
22885 return NULL;
22886 }
22887 else if (offset_in_dwz != cu->per_cu->is_dwz
22888 || !offset_in_cu_p (&cu->header, sect_off))
22889 {
22890 struct dwarf2_per_cu_data *per_cu;
22891
22892 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22893 dwarf2_per_objfile);
22894
22895 /* If necessary, add it to the queue and load its DIEs. */
22896 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
22897 load_full_comp_unit (per_cu, false, cu->language);
22898
22899 target_cu = per_cu->cu;
22900 }
22901 else if (cu->dies == NULL)
22902 {
22903 /* We're loading full DIEs during partial symbol reading. */
22904 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
22905 load_full_comp_unit (cu->per_cu, false, language_minimal);
22906 }
22907
22908 *ref_cu = target_cu;
22909 temp_die.sect_off = sect_off;
22910 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22911 &temp_die,
22912 to_underlying (sect_off));
22913 }
22914
22915 /* Follow reference attribute ATTR of SRC_DIE.
22916 On entry *REF_CU is the CU of SRC_DIE.
22917 On exit *REF_CU is the CU of the result. */
22918
22919 static struct die_info *
22920 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22921 struct dwarf2_cu **ref_cu)
22922 {
22923 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22924 struct dwarf2_cu *cu = *ref_cu;
22925 struct die_info *die;
22926
22927 die = follow_die_offset (sect_off,
22928 (attr->form == DW_FORM_GNU_ref_alt
22929 || cu->per_cu->is_dwz),
22930 ref_cu);
22931 if (!die)
22932 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22933 "at %s [in module %s]"),
22934 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22935 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
22936
22937 return die;
22938 }
22939
22940 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
22941 Returned value is intended for DW_OP_call*. Returned
22942 dwarf2_locexpr_baton->data has lifetime of
22943 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
22944
22945 struct dwarf2_locexpr_baton
22946 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22947 struct dwarf2_per_cu_data *per_cu,
22948 CORE_ADDR (*get_frame_pc) (void *baton),
22949 void *baton)
22950 {
22951 struct dwarf2_cu *cu;
22952 struct die_info *die;
22953 struct attribute *attr;
22954 struct dwarf2_locexpr_baton retval;
22955 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22956 struct objfile *objfile = dwarf2_per_objfile->objfile;
22957
22958 if (per_cu->cu == NULL)
22959 load_cu (per_cu, false);
22960 cu = per_cu->cu;
22961 if (cu == NULL)
22962 {
22963 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22964 Instead just throw an error, not much else we can do. */
22965 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22966 sect_offset_str (sect_off), objfile_name (objfile));
22967 }
22968
22969 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22970 if (!die)
22971 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22972 sect_offset_str (sect_off), objfile_name (objfile));
22973
22974 attr = dwarf2_attr (die, DW_AT_location, cu);
22975 if (!attr)
22976 {
22977 /* DWARF: "If there is no such attribute, then there is no effect.".
22978 DATA is ignored if SIZE is 0. */
22979
22980 retval.data = NULL;
22981 retval.size = 0;
22982 }
22983 else if (attr_form_is_section_offset (attr))
22984 {
22985 struct dwarf2_loclist_baton loclist_baton;
22986 CORE_ADDR pc = (*get_frame_pc) (baton);
22987 size_t size;
22988
22989 fill_in_loclist_baton (cu, &loclist_baton, attr);
22990
22991 retval.data = dwarf2_find_location_expression (&loclist_baton,
22992 &size, pc);
22993 retval.size = size;
22994 }
22995 else
22996 {
22997 if (!attr_form_is_block (attr))
22998 error (_("Dwarf Error: DIE at %s referenced in module %s "
22999 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23000 sect_offset_str (sect_off), objfile_name (objfile));
23001
23002 retval.data = DW_BLOCK (attr)->data;
23003 retval.size = DW_BLOCK (attr)->size;
23004 }
23005 retval.per_cu = cu->per_cu;
23006
23007 age_cached_comp_units (dwarf2_per_objfile);
23008
23009 return retval;
23010 }
23011
23012 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23013 offset. */
23014
23015 struct dwarf2_locexpr_baton
23016 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23017 struct dwarf2_per_cu_data *per_cu,
23018 CORE_ADDR (*get_frame_pc) (void *baton),
23019 void *baton)
23020 {
23021 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23022
23023 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
23024 }
23025
23026 /* Write a constant of a given type as target-ordered bytes into
23027 OBSTACK. */
23028
23029 static const gdb_byte *
23030 write_constant_as_bytes (struct obstack *obstack,
23031 enum bfd_endian byte_order,
23032 struct type *type,
23033 ULONGEST value,
23034 LONGEST *len)
23035 {
23036 gdb_byte *result;
23037
23038 *len = TYPE_LENGTH (type);
23039 result = (gdb_byte *) obstack_alloc (obstack, *len);
23040 store_unsigned_integer (result, *len, byte_order, value);
23041
23042 return result;
23043 }
23044
23045 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23046 pointer to the constant bytes and set LEN to the length of the
23047 data. If memory is needed, allocate it on OBSTACK. If the DIE
23048 does not have a DW_AT_const_value, return NULL. */
23049
23050 const gdb_byte *
23051 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23052 struct dwarf2_per_cu_data *per_cu,
23053 struct obstack *obstack,
23054 LONGEST *len)
23055 {
23056 struct dwarf2_cu *cu;
23057 struct die_info *die;
23058 struct attribute *attr;
23059 const gdb_byte *result = NULL;
23060 struct type *type;
23061 LONGEST value;
23062 enum bfd_endian byte_order;
23063 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23064
23065 if (per_cu->cu == NULL)
23066 load_cu (per_cu, false);
23067 cu = per_cu->cu;
23068 if (cu == NULL)
23069 {
23070 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23071 Instead just throw an error, not much else we can do. */
23072 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23073 sect_offset_str (sect_off), objfile_name (objfile));
23074 }
23075
23076 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23077 if (!die)
23078 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23079 sect_offset_str (sect_off), objfile_name (objfile));
23080
23081 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23082 if (attr == NULL)
23083 return NULL;
23084
23085 byte_order = (bfd_big_endian (objfile->obfd)
23086 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23087
23088 switch (attr->form)
23089 {
23090 case DW_FORM_addr:
23091 case DW_FORM_GNU_addr_index:
23092 {
23093 gdb_byte *tem;
23094
23095 *len = cu->header.addr_size;
23096 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23097 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23098 result = tem;
23099 }
23100 break;
23101 case DW_FORM_string:
23102 case DW_FORM_strp:
23103 case DW_FORM_GNU_str_index:
23104 case DW_FORM_GNU_strp_alt:
23105 /* DW_STRING is already allocated on the objfile obstack, point
23106 directly to it. */
23107 result = (const gdb_byte *) DW_STRING (attr);
23108 *len = strlen (DW_STRING (attr));
23109 break;
23110 case DW_FORM_block1:
23111 case DW_FORM_block2:
23112 case DW_FORM_block4:
23113 case DW_FORM_block:
23114 case DW_FORM_exprloc:
23115 case DW_FORM_data16:
23116 result = DW_BLOCK (attr)->data;
23117 *len = DW_BLOCK (attr)->size;
23118 break;
23119
23120 /* The DW_AT_const_value attributes are supposed to carry the
23121 symbol's value "represented as it would be on the target
23122 architecture." By the time we get here, it's already been
23123 converted to host endianness, so we just need to sign- or
23124 zero-extend it as appropriate. */
23125 case DW_FORM_data1:
23126 type = die_type (die, cu);
23127 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23128 if (result == NULL)
23129 result = write_constant_as_bytes (obstack, byte_order,
23130 type, value, len);
23131 break;
23132 case DW_FORM_data2:
23133 type = die_type (die, cu);
23134 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23135 if (result == NULL)
23136 result = write_constant_as_bytes (obstack, byte_order,
23137 type, value, len);
23138 break;
23139 case DW_FORM_data4:
23140 type = die_type (die, cu);
23141 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23142 if (result == NULL)
23143 result = write_constant_as_bytes (obstack, byte_order,
23144 type, value, len);
23145 break;
23146 case DW_FORM_data8:
23147 type = die_type (die, cu);
23148 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23149 if (result == NULL)
23150 result = write_constant_as_bytes (obstack, byte_order,
23151 type, value, len);
23152 break;
23153
23154 case DW_FORM_sdata:
23155 case DW_FORM_implicit_const:
23156 type = die_type (die, cu);
23157 result = write_constant_as_bytes (obstack, byte_order,
23158 type, DW_SND (attr), len);
23159 break;
23160
23161 case DW_FORM_udata:
23162 type = die_type (die, cu);
23163 result = write_constant_as_bytes (obstack, byte_order,
23164 type, DW_UNSND (attr), len);
23165 break;
23166
23167 default:
23168 complaint (&symfile_complaints,
23169 _("unsupported const value attribute form: '%s'"),
23170 dwarf_form_name (attr->form));
23171 break;
23172 }
23173
23174 return result;
23175 }
23176
23177 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23178 valid type for this die is found. */
23179
23180 struct type *
23181 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23182 struct dwarf2_per_cu_data *per_cu)
23183 {
23184 struct dwarf2_cu *cu;
23185 struct die_info *die;
23186
23187 if (per_cu->cu == NULL)
23188 load_cu (per_cu, false);
23189 cu = per_cu->cu;
23190 if (!cu)
23191 return NULL;
23192
23193 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23194 if (!die)
23195 return NULL;
23196
23197 return die_type (die, cu);
23198 }
23199
23200 /* Return the type of the DIE at DIE_OFFSET in the CU named by
23201 PER_CU. */
23202
23203 struct type *
23204 dwarf2_get_die_type (cu_offset die_offset,
23205 struct dwarf2_per_cu_data *per_cu)
23206 {
23207 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23208 return get_die_type_at_offset (die_offset_sect, per_cu);
23209 }
23210
23211 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23212 On entry *REF_CU is the CU of SRC_DIE.
23213 On exit *REF_CU is the CU of the result.
23214 Returns NULL if the referenced DIE isn't found. */
23215
23216 static struct die_info *
23217 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23218 struct dwarf2_cu **ref_cu)
23219 {
23220 struct die_info temp_die;
23221 struct dwarf2_cu *sig_cu;
23222 struct die_info *die;
23223
23224 /* While it might be nice to assert sig_type->type == NULL here,
23225 we can get here for DW_AT_imported_declaration where we need
23226 the DIE not the type. */
23227
23228 /* If necessary, add it to the queue and load its DIEs. */
23229
23230 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
23231 read_signatured_type (sig_type);
23232
23233 sig_cu = sig_type->per_cu.cu;
23234 gdb_assert (sig_cu != NULL);
23235 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23236 temp_die.sect_off = sig_type->type_offset_in_section;
23237 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23238 to_underlying (temp_die.sect_off));
23239 if (die)
23240 {
23241 struct dwarf2_per_objfile *dwarf2_per_objfile
23242 = (*ref_cu)->per_cu->dwarf2_per_objfile;
23243
23244 /* For .gdb_index version 7 keep track of included TUs.
23245 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23246 if (dwarf2_per_objfile->index_table != NULL
23247 && dwarf2_per_objfile->index_table->version <= 7)
23248 {
23249 VEC_safe_push (dwarf2_per_cu_ptr,
23250 (*ref_cu)->per_cu->imported_symtabs,
23251 sig_cu->per_cu);
23252 }
23253
23254 *ref_cu = sig_cu;
23255 return die;
23256 }
23257
23258 return NULL;
23259 }
23260
23261 /* Follow signatured type referenced by ATTR in SRC_DIE.
23262 On entry *REF_CU is the CU of SRC_DIE.
23263 On exit *REF_CU is the CU of the result.
23264 The result is the DIE of the type.
23265 If the referenced type cannot be found an error is thrown. */
23266
23267 static struct die_info *
23268 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23269 struct dwarf2_cu **ref_cu)
23270 {
23271 ULONGEST signature = DW_SIGNATURE (attr);
23272 struct signatured_type *sig_type;
23273 struct die_info *die;
23274
23275 gdb_assert (attr->form == DW_FORM_ref_sig8);
23276
23277 sig_type = lookup_signatured_type (*ref_cu, signature);
23278 /* sig_type will be NULL if the signatured type is missing from
23279 the debug info. */
23280 if (sig_type == NULL)
23281 {
23282 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23283 " from DIE at %s [in module %s]"),
23284 hex_string (signature), sect_offset_str (src_die->sect_off),
23285 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23286 }
23287
23288 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23289 if (die == NULL)
23290 {
23291 dump_die_for_error (src_die);
23292 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23293 " from DIE at %s [in module %s]"),
23294 hex_string (signature), sect_offset_str (src_die->sect_off),
23295 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23296 }
23297
23298 return die;
23299 }
23300
23301 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23302 reading in and processing the type unit if necessary. */
23303
23304 static struct type *
23305 get_signatured_type (struct die_info *die, ULONGEST signature,
23306 struct dwarf2_cu *cu)
23307 {
23308 struct dwarf2_per_objfile *dwarf2_per_objfile
23309 = cu->per_cu->dwarf2_per_objfile;
23310 struct signatured_type *sig_type;
23311 struct dwarf2_cu *type_cu;
23312 struct die_info *type_die;
23313 struct type *type;
23314
23315 sig_type = lookup_signatured_type (cu, signature);
23316 /* sig_type will be NULL if the signatured type is missing from
23317 the debug info. */
23318 if (sig_type == NULL)
23319 {
23320 complaint (&symfile_complaints,
23321 _("Dwarf Error: Cannot find signatured DIE %s referenced"
23322 " from DIE at %s [in module %s]"),
23323 hex_string (signature), sect_offset_str (die->sect_off),
23324 objfile_name (dwarf2_per_objfile->objfile));
23325 return build_error_marker_type (cu, die);
23326 }
23327
23328 /* If we already know the type we're done. */
23329 if (sig_type->type != NULL)
23330 return sig_type->type;
23331
23332 type_cu = cu;
23333 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23334 if (type_die != NULL)
23335 {
23336 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23337 is created. This is important, for example, because for c++ classes
23338 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23339 type = read_type_die (type_die, type_cu);
23340 if (type == NULL)
23341 {
23342 complaint (&symfile_complaints,
23343 _("Dwarf Error: Cannot build signatured type %s"
23344 " referenced from DIE at %s [in module %s]"),
23345 hex_string (signature), sect_offset_str (die->sect_off),
23346 objfile_name (dwarf2_per_objfile->objfile));
23347 type = build_error_marker_type (cu, die);
23348 }
23349 }
23350 else
23351 {
23352 complaint (&symfile_complaints,
23353 _("Dwarf Error: Problem reading signatured DIE %s referenced"
23354 " from DIE at %s [in module %s]"),
23355 hex_string (signature), sect_offset_str (die->sect_off),
23356 objfile_name (dwarf2_per_objfile->objfile));
23357 type = build_error_marker_type (cu, die);
23358 }
23359 sig_type->type = type;
23360
23361 return type;
23362 }
23363
23364 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23365 reading in and processing the type unit if necessary. */
23366
23367 static struct type *
23368 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23369 struct dwarf2_cu *cu) /* ARI: editCase function */
23370 {
23371 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23372 if (attr_form_is_ref (attr))
23373 {
23374 struct dwarf2_cu *type_cu = cu;
23375 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23376
23377 return read_type_die (type_die, type_cu);
23378 }
23379 else if (attr->form == DW_FORM_ref_sig8)
23380 {
23381 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23382 }
23383 else
23384 {
23385 struct dwarf2_per_objfile *dwarf2_per_objfile
23386 = cu->per_cu->dwarf2_per_objfile;
23387
23388 complaint (&symfile_complaints,
23389 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23390 " at %s [in module %s]"),
23391 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
23392 objfile_name (dwarf2_per_objfile->objfile));
23393 return build_error_marker_type (cu, die);
23394 }
23395 }
23396
23397 /* Load the DIEs associated with type unit PER_CU into memory. */
23398
23399 static void
23400 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
23401 {
23402 struct signatured_type *sig_type;
23403
23404 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23405 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23406
23407 /* We have the per_cu, but we need the signatured_type.
23408 Fortunately this is an easy translation. */
23409 gdb_assert (per_cu->is_debug_types);
23410 sig_type = (struct signatured_type *) per_cu;
23411
23412 gdb_assert (per_cu->cu == NULL);
23413
23414 read_signatured_type (sig_type);
23415
23416 gdb_assert (per_cu->cu != NULL);
23417 }
23418
23419 /* die_reader_func for read_signatured_type.
23420 This is identical to load_full_comp_unit_reader,
23421 but is kept separate for now. */
23422
23423 static void
23424 read_signatured_type_reader (const struct die_reader_specs *reader,
23425 const gdb_byte *info_ptr,
23426 struct die_info *comp_unit_die,
23427 int has_children,
23428 void *data)
23429 {
23430 struct dwarf2_cu *cu = reader->cu;
23431
23432 gdb_assert (cu->die_hash == NULL);
23433 cu->die_hash =
23434 htab_create_alloc_ex (cu->header.length / 12,
23435 die_hash,
23436 die_eq,
23437 NULL,
23438 &cu->comp_unit_obstack,
23439 hashtab_obstack_allocate,
23440 dummy_obstack_deallocate);
23441
23442 if (has_children)
23443 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23444 &info_ptr, comp_unit_die);
23445 cu->dies = comp_unit_die;
23446 /* comp_unit_die is not stored in die_hash, no need. */
23447
23448 /* We try not to read any attributes in this function, because not
23449 all CUs needed for references have been loaded yet, and symbol
23450 table processing isn't initialized. But we have to set the CU language,
23451 or we won't be able to build types correctly.
23452 Similarly, if we do not read the producer, we can not apply
23453 producer-specific interpretation. */
23454 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23455 }
23456
23457 /* Read in a signatured type and build its CU and DIEs.
23458 If the type is a stub for the real type in a DWO file,
23459 read in the real type from the DWO file as well. */
23460
23461 static void
23462 read_signatured_type (struct signatured_type *sig_type)
23463 {
23464 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
23465
23466 gdb_assert (per_cu->is_debug_types);
23467 gdb_assert (per_cu->cu == NULL);
23468
23469 init_cutu_and_read_dies (per_cu, NULL, 0, 1, false,
23470 read_signatured_type_reader, NULL);
23471 sig_type->per_cu.tu_read = 1;
23472 }
23473
23474 /* Decode simple location descriptions.
23475 Given a pointer to a dwarf block that defines a location, compute
23476 the location and return the value.
23477
23478 NOTE drow/2003-11-18: This function is called in two situations
23479 now: for the address of static or global variables (partial symbols
23480 only) and for offsets into structures which are expected to be
23481 (more or less) constant. The partial symbol case should go away,
23482 and only the constant case should remain. That will let this
23483 function complain more accurately. A few special modes are allowed
23484 without complaint for global variables (for instance, global
23485 register values and thread-local values).
23486
23487 A location description containing no operations indicates that the
23488 object is optimized out. The return value is 0 for that case.
23489 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23490 callers will only want a very basic result and this can become a
23491 complaint.
23492
23493 Note that stack[0] is unused except as a default error return. */
23494
23495 static CORE_ADDR
23496 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
23497 {
23498 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
23499 size_t i;
23500 size_t size = blk->size;
23501 const gdb_byte *data = blk->data;
23502 CORE_ADDR stack[64];
23503 int stacki;
23504 unsigned int bytes_read, unsnd;
23505 gdb_byte op;
23506
23507 i = 0;
23508 stacki = 0;
23509 stack[stacki] = 0;
23510 stack[++stacki] = 0;
23511
23512 while (i < size)
23513 {
23514 op = data[i++];
23515 switch (op)
23516 {
23517 case DW_OP_lit0:
23518 case DW_OP_lit1:
23519 case DW_OP_lit2:
23520 case DW_OP_lit3:
23521 case DW_OP_lit4:
23522 case DW_OP_lit5:
23523 case DW_OP_lit6:
23524 case DW_OP_lit7:
23525 case DW_OP_lit8:
23526 case DW_OP_lit9:
23527 case DW_OP_lit10:
23528 case DW_OP_lit11:
23529 case DW_OP_lit12:
23530 case DW_OP_lit13:
23531 case DW_OP_lit14:
23532 case DW_OP_lit15:
23533 case DW_OP_lit16:
23534 case DW_OP_lit17:
23535 case DW_OP_lit18:
23536 case DW_OP_lit19:
23537 case DW_OP_lit20:
23538 case DW_OP_lit21:
23539 case DW_OP_lit22:
23540 case DW_OP_lit23:
23541 case DW_OP_lit24:
23542 case DW_OP_lit25:
23543 case DW_OP_lit26:
23544 case DW_OP_lit27:
23545 case DW_OP_lit28:
23546 case DW_OP_lit29:
23547 case DW_OP_lit30:
23548 case DW_OP_lit31:
23549 stack[++stacki] = op - DW_OP_lit0;
23550 break;
23551
23552 case DW_OP_reg0:
23553 case DW_OP_reg1:
23554 case DW_OP_reg2:
23555 case DW_OP_reg3:
23556 case DW_OP_reg4:
23557 case DW_OP_reg5:
23558 case DW_OP_reg6:
23559 case DW_OP_reg7:
23560 case DW_OP_reg8:
23561 case DW_OP_reg9:
23562 case DW_OP_reg10:
23563 case DW_OP_reg11:
23564 case DW_OP_reg12:
23565 case DW_OP_reg13:
23566 case DW_OP_reg14:
23567 case DW_OP_reg15:
23568 case DW_OP_reg16:
23569 case DW_OP_reg17:
23570 case DW_OP_reg18:
23571 case DW_OP_reg19:
23572 case DW_OP_reg20:
23573 case DW_OP_reg21:
23574 case DW_OP_reg22:
23575 case DW_OP_reg23:
23576 case DW_OP_reg24:
23577 case DW_OP_reg25:
23578 case DW_OP_reg26:
23579 case DW_OP_reg27:
23580 case DW_OP_reg28:
23581 case DW_OP_reg29:
23582 case DW_OP_reg30:
23583 case DW_OP_reg31:
23584 stack[++stacki] = op - DW_OP_reg0;
23585 if (i < size)
23586 dwarf2_complex_location_expr_complaint ();
23587 break;
23588
23589 case DW_OP_regx:
23590 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23591 i += bytes_read;
23592 stack[++stacki] = unsnd;
23593 if (i < size)
23594 dwarf2_complex_location_expr_complaint ();
23595 break;
23596
23597 case DW_OP_addr:
23598 stack[++stacki] = read_address (objfile->obfd, &data[i],
23599 cu, &bytes_read);
23600 i += bytes_read;
23601 break;
23602
23603 case DW_OP_const1u:
23604 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23605 i += 1;
23606 break;
23607
23608 case DW_OP_const1s:
23609 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23610 i += 1;
23611 break;
23612
23613 case DW_OP_const2u:
23614 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23615 i += 2;
23616 break;
23617
23618 case DW_OP_const2s:
23619 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23620 i += 2;
23621 break;
23622
23623 case DW_OP_const4u:
23624 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23625 i += 4;
23626 break;
23627
23628 case DW_OP_const4s:
23629 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23630 i += 4;
23631 break;
23632
23633 case DW_OP_const8u:
23634 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23635 i += 8;
23636 break;
23637
23638 case DW_OP_constu:
23639 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23640 &bytes_read);
23641 i += bytes_read;
23642 break;
23643
23644 case DW_OP_consts:
23645 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23646 i += bytes_read;
23647 break;
23648
23649 case DW_OP_dup:
23650 stack[stacki + 1] = stack[stacki];
23651 stacki++;
23652 break;
23653
23654 case DW_OP_plus:
23655 stack[stacki - 1] += stack[stacki];
23656 stacki--;
23657 break;
23658
23659 case DW_OP_plus_uconst:
23660 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23661 &bytes_read);
23662 i += bytes_read;
23663 break;
23664
23665 case DW_OP_minus:
23666 stack[stacki - 1] -= stack[stacki];
23667 stacki--;
23668 break;
23669
23670 case DW_OP_deref:
23671 /* If we're not the last op, then we definitely can't encode
23672 this using GDB's address_class enum. This is valid for partial
23673 global symbols, although the variable's address will be bogus
23674 in the psymtab. */
23675 if (i < size)
23676 dwarf2_complex_location_expr_complaint ();
23677 break;
23678
23679 case DW_OP_GNU_push_tls_address:
23680 case DW_OP_form_tls_address:
23681 /* The top of the stack has the offset from the beginning
23682 of the thread control block at which the variable is located. */
23683 /* Nothing should follow this operator, so the top of stack would
23684 be returned. */
23685 /* This is valid for partial global symbols, but the variable's
23686 address will be bogus in the psymtab. Make it always at least
23687 non-zero to not look as a variable garbage collected by linker
23688 which have DW_OP_addr 0. */
23689 if (i < size)
23690 dwarf2_complex_location_expr_complaint ();
23691 stack[stacki]++;
23692 break;
23693
23694 case DW_OP_GNU_uninit:
23695 break;
23696
23697 case DW_OP_GNU_addr_index:
23698 case DW_OP_GNU_const_index:
23699 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23700 &bytes_read);
23701 i += bytes_read;
23702 break;
23703
23704 default:
23705 {
23706 const char *name = get_DW_OP_name (op);
23707
23708 if (name)
23709 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
23710 name);
23711 else
23712 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
23713 op);
23714 }
23715
23716 return (stack[stacki]);
23717 }
23718
23719 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23720 outside of the allocated space. Also enforce minimum>0. */
23721 if (stacki >= ARRAY_SIZE (stack) - 1)
23722 {
23723 complaint (&symfile_complaints,
23724 _("location description stack overflow"));
23725 return 0;
23726 }
23727
23728 if (stacki <= 0)
23729 {
23730 complaint (&symfile_complaints,
23731 _("location description stack underflow"));
23732 return 0;
23733 }
23734 }
23735 return (stack[stacki]);
23736 }
23737
23738 /* memory allocation interface */
23739
23740 static struct dwarf_block *
23741 dwarf_alloc_block (struct dwarf2_cu *cu)
23742 {
23743 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23744 }
23745
23746 static struct die_info *
23747 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23748 {
23749 struct die_info *die;
23750 size_t size = sizeof (struct die_info);
23751
23752 if (num_attrs > 1)
23753 size += (num_attrs - 1) * sizeof (struct attribute);
23754
23755 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23756 memset (die, 0, sizeof (struct die_info));
23757 return (die);
23758 }
23759
23760 \f
23761 /* Macro support. */
23762
23763 /* Return file name relative to the compilation directory of file number I in
23764 *LH's file name table. The result is allocated using xmalloc; the caller is
23765 responsible for freeing it. */
23766
23767 static char *
23768 file_file_name (int file, struct line_header *lh)
23769 {
23770 /* Is the file number a valid index into the line header's file name
23771 table? Remember that file numbers start with one, not zero. */
23772 if (1 <= file && file <= lh->file_names.size ())
23773 {
23774 const file_entry &fe = lh->file_names[file - 1];
23775
23776 if (!IS_ABSOLUTE_PATH (fe.name))
23777 {
23778 const char *dir = fe.include_dir (lh);
23779 if (dir != NULL)
23780 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
23781 }
23782 return xstrdup (fe.name);
23783 }
23784 else
23785 {
23786 /* The compiler produced a bogus file number. We can at least
23787 record the macro definitions made in the file, even if we
23788 won't be able to find the file by name. */
23789 char fake_name[80];
23790
23791 xsnprintf (fake_name, sizeof (fake_name),
23792 "<bad macro file number %d>", file);
23793
23794 complaint (&symfile_complaints,
23795 _("bad file number in macro information (%d)"),
23796 file);
23797
23798 return xstrdup (fake_name);
23799 }
23800 }
23801
23802 /* Return the full name of file number I in *LH's file name table.
23803 Use COMP_DIR as the name of the current directory of the
23804 compilation. The result is allocated using xmalloc; the caller is
23805 responsible for freeing it. */
23806 static char *
23807 file_full_name (int file, struct line_header *lh, const char *comp_dir)
23808 {
23809 /* Is the file number a valid index into the line header's file name
23810 table? Remember that file numbers start with one, not zero. */
23811 if (1 <= file && file <= lh->file_names.size ())
23812 {
23813 char *relative = file_file_name (file, lh);
23814
23815 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
23816 return relative;
23817 return reconcat (relative, comp_dir, SLASH_STRING,
23818 relative, (char *) NULL);
23819 }
23820 else
23821 return file_file_name (file, lh);
23822 }
23823
23824
23825 static struct macro_source_file *
23826 macro_start_file (int file, int line,
23827 struct macro_source_file *current_file,
23828 struct line_header *lh)
23829 {
23830 /* File name relative to the compilation directory of this source file. */
23831 char *file_name = file_file_name (file, lh);
23832
23833 if (! current_file)
23834 {
23835 /* Note: We don't create a macro table for this compilation unit
23836 at all until we actually get a filename. */
23837 struct macro_table *macro_table = get_macro_table ();
23838
23839 /* If we have no current file, then this must be the start_file
23840 directive for the compilation unit's main source file. */
23841 current_file = macro_set_main (macro_table, file_name);
23842 macro_define_special (macro_table);
23843 }
23844 else
23845 current_file = macro_include (current_file, line, file_name);
23846
23847 xfree (file_name);
23848
23849 return current_file;
23850 }
23851
23852 static const char *
23853 consume_improper_spaces (const char *p, const char *body)
23854 {
23855 if (*p == ' ')
23856 {
23857 complaint (&symfile_complaints,
23858 _("macro definition contains spaces "
23859 "in formal argument list:\n`%s'"),
23860 body);
23861
23862 while (*p == ' ')
23863 p++;
23864 }
23865
23866 return p;
23867 }
23868
23869
23870 static void
23871 parse_macro_definition (struct macro_source_file *file, int line,
23872 const char *body)
23873 {
23874 const char *p;
23875
23876 /* The body string takes one of two forms. For object-like macro
23877 definitions, it should be:
23878
23879 <macro name> " " <definition>
23880
23881 For function-like macro definitions, it should be:
23882
23883 <macro name> "() " <definition>
23884 or
23885 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
23886
23887 Spaces may appear only where explicitly indicated, and in the
23888 <definition>.
23889
23890 The Dwarf 2 spec says that an object-like macro's name is always
23891 followed by a space, but versions of GCC around March 2002 omit
23892 the space when the macro's definition is the empty string.
23893
23894 The Dwarf 2 spec says that there should be no spaces between the
23895 formal arguments in a function-like macro's formal argument list,
23896 but versions of GCC around March 2002 include spaces after the
23897 commas. */
23898
23899
23900 /* Find the extent of the macro name. The macro name is terminated
23901 by either a space or null character (for an object-like macro) or
23902 an opening paren (for a function-like macro). */
23903 for (p = body; *p; p++)
23904 if (*p == ' ' || *p == '(')
23905 break;
23906
23907 if (*p == ' ' || *p == '\0')
23908 {
23909 /* It's an object-like macro. */
23910 int name_len = p - body;
23911 char *name = savestring (body, name_len);
23912 const char *replacement;
23913
23914 if (*p == ' ')
23915 replacement = body + name_len + 1;
23916 else
23917 {
23918 dwarf2_macro_malformed_definition_complaint (body);
23919 replacement = body + name_len;
23920 }
23921
23922 macro_define_object (file, line, name, replacement);
23923
23924 xfree (name);
23925 }
23926 else if (*p == '(')
23927 {
23928 /* It's a function-like macro. */
23929 char *name = savestring (body, p - body);
23930 int argc = 0;
23931 int argv_size = 1;
23932 char **argv = XNEWVEC (char *, argv_size);
23933
23934 p++;
23935
23936 p = consume_improper_spaces (p, body);
23937
23938 /* Parse the formal argument list. */
23939 while (*p && *p != ')')
23940 {
23941 /* Find the extent of the current argument name. */
23942 const char *arg_start = p;
23943
23944 while (*p && *p != ',' && *p != ')' && *p != ' ')
23945 p++;
23946
23947 if (! *p || p == arg_start)
23948 dwarf2_macro_malformed_definition_complaint (body);
23949 else
23950 {
23951 /* Make sure argv has room for the new argument. */
23952 if (argc >= argv_size)
23953 {
23954 argv_size *= 2;
23955 argv = XRESIZEVEC (char *, argv, argv_size);
23956 }
23957
23958 argv[argc++] = savestring (arg_start, p - arg_start);
23959 }
23960
23961 p = consume_improper_spaces (p, body);
23962
23963 /* Consume the comma, if present. */
23964 if (*p == ',')
23965 {
23966 p++;
23967
23968 p = consume_improper_spaces (p, body);
23969 }
23970 }
23971
23972 if (*p == ')')
23973 {
23974 p++;
23975
23976 if (*p == ' ')
23977 /* Perfectly formed definition, no complaints. */
23978 macro_define_function (file, line, name,
23979 argc, (const char **) argv,
23980 p + 1);
23981 else if (*p == '\0')
23982 {
23983 /* Complain, but do define it. */
23984 dwarf2_macro_malformed_definition_complaint (body);
23985 macro_define_function (file, line, name,
23986 argc, (const char **) argv,
23987 p);
23988 }
23989 else
23990 /* Just complain. */
23991 dwarf2_macro_malformed_definition_complaint (body);
23992 }
23993 else
23994 /* Just complain. */
23995 dwarf2_macro_malformed_definition_complaint (body);
23996
23997 xfree (name);
23998 {
23999 int i;
24000
24001 for (i = 0; i < argc; i++)
24002 xfree (argv[i]);
24003 }
24004 xfree (argv);
24005 }
24006 else
24007 dwarf2_macro_malformed_definition_complaint (body);
24008 }
24009
24010 /* Skip some bytes from BYTES according to the form given in FORM.
24011 Returns the new pointer. */
24012
24013 static const gdb_byte *
24014 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
24015 enum dwarf_form form,
24016 unsigned int offset_size,
24017 struct dwarf2_section_info *section)
24018 {
24019 unsigned int bytes_read;
24020
24021 switch (form)
24022 {
24023 case DW_FORM_data1:
24024 case DW_FORM_flag:
24025 ++bytes;
24026 break;
24027
24028 case DW_FORM_data2:
24029 bytes += 2;
24030 break;
24031
24032 case DW_FORM_data4:
24033 bytes += 4;
24034 break;
24035
24036 case DW_FORM_data8:
24037 bytes += 8;
24038 break;
24039
24040 case DW_FORM_data16:
24041 bytes += 16;
24042 break;
24043
24044 case DW_FORM_string:
24045 read_direct_string (abfd, bytes, &bytes_read);
24046 bytes += bytes_read;
24047 break;
24048
24049 case DW_FORM_sec_offset:
24050 case DW_FORM_strp:
24051 case DW_FORM_GNU_strp_alt:
24052 bytes += offset_size;
24053 break;
24054
24055 case DW_FORM_block:
24056 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24057 bytes += bytes_read;
24058 break;
24059
24060 case DW_FORM_block1:
24061 bytes += 1 + read_1_byte (abfd, bytes);
24062 break;
24063 case DW_FORM_block2:
24064 bytes += 2 + read_2_bytes (abfd, bytes);
24065 break;
24066 case DW_FORM_block4:
24067 bytes += 4 + read_4_bytes (abfd, bytes);
24068 break;
24069
24070 case DW_FORM_sdata:
24071 case DW_FORM_udata:
24072 case DW_FORM_GNU_addr_index:
24073 case DW_FORM_GNU_str_index:
24074 bytes = gdb_skip_leb128 (bytes, buffer_end);
24075 if (bytes == NULL)
24076 {
24077 dwarf2_section_buffer_overflow_complaint (section);
24078 return NULL;
24079 }
24080 break;
24081
24082 case DW_FORM_implicit_const:
24083 break;
24084
24085 default:
24086 {
24087 complaint (&symfile_complaints,
24088 _("invalid form 0x%x in `%s'"),
24089 form, get_section_name (section));
24090 return NULL;
24091 }
24092 }
24093
24094 return bytes;
24095 }
24096
24097 /* A helper for dwarf_decode_macros that handles skipping an unknown
24098 opcode. Returns an updated pointer to the macro data buffer; or,
24099 on error, issues a complaint and returns NULL. */
24100
24101 static const gdb_byte *
24102 skip_unknown_opcode (unsigned int opcode,
24103 const gdb_byte **opcode_definitions,
24104 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24105 bfd *abfd,
24106 unsigned int offset_size,
24107 struct dwarf2_section_info *section)
24108 {
24109 unsigned int bytes_read, i;
24110 unsigned long arg;
24111 const gdb_byte *defn;
24112
24113 if (opcode_definitions[opcode] == NULL)
24114 {
24115 complaint (&symfile_complaints,
24116 _("unrecognized DW_MACFINO opcode 0x%x"),
24117 opcode);
24118 return NULL;
24119 }
24120
24121 defn = opcode_definitions[opcode];
24122 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24123 defn += bytes_read;
24124
24125 for (i = 0; i < arg; ++i)
24126 {
24127 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24128 (enum dwarf_form) defn[i], offset_size,
24129 section);
24130 if (mac_ptr == NULL)
24131 {
24132 /* skip_form_bytes already issued the complaint. */
24133 return NULL;
24134 }
24135 }
24136
24137 return mac_ptr;
24138 }
24139
24140 /* A helper function which parses the header of a macro section.
24141 If the macro section is the extended (for now called "GNU") type,
24142 then this updates *OFFSET_SIZE. Returns a pointer to just after
24143 the header, or issues a complaint and returns NULL on error. */
24144
24145 static const gdb_byte *
24146 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
24147 bfd *abfd,
24148 const gdb_byte *mac_ptr,
24149 unsigned int *offset_size,
24150 int section_is_gnu)
24151 {
24152 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
24153
24154 if (section_is_gnu)
24155 {
24156 unsigned int version, flags;
24157
24158 version = read_2_bytes (abfd, mac_ptr);
24159 if (version != 4 && version != 5)
24160 {
24161 complaint (&symfile_complaints,
24162 _("unrecognized version `%d' in .debug_macro section"),
24163 version);
24164 return NULL;
24165 }
24166 mac_ptr += 2;
24167
24168 flags = read_1_byte (abfd, mac_ptr);
24169 ++mac_ptr;
24170 *offset_size = (flags & 1) ? 8 : 4;
24171
24172 if ((flags & 2) != 0)
24173 /* We don't need the line table offset. */
24174 mac_ptr += *offset_size;
24175
24176 /* Vendor opcode descriptions. */
24177 if ((flags & 4) != 0)
24178 {
24179 unsigned int i, count;
24180
24181 count = read_1_byte (abfd, mac_ptr);
24182 ++mac_ptr;
24183 for (i = 0; i < count; ++i)
24184 {
24185 unsigned int opcode, bytes_read;
24186 unsigned long arg;
24187
24188 opcode = read_1_byte (abfd, mac_ptr);
24189 ++mac_ptr;
24190 opcode_definitions[opcode] = mac_ptr;
24191 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24192 mac_ptr += bytes_read;
24193 mac_ptr += arg;
24194 }
24195 }
24196 }
24197
24198 return mac_ptr;
24199 }
24200
24201 /* A helper for dwarf_decode_macros that handles the GNU extensions,
24202 including DW_MACRO_import. */
24203
24204 static void
24205 dwarf_decode_macro_bytes (struct dwarf2_per_objfile *dwarf2_per_objfile,
24206 bfd *abfd,
24207 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24208 struct macro_source_file *current_file,
24209 struct line_header *lh,
24210 struct dwarf2_section_info *section,
24211 int section_is_gnu, int section_is_dwz,
24212 unsigned int offset_size,
24213 htab_t include_hash)
24214 {
24215 struct objfile *objfile = dwarf2_per_objfile->objfile;
24216 enum dwarf_macro_record_type macinfo_type;
24217 int at_commandline;
24218 const gdb_byte *opcode_definitions[256];
24219
24220 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24221 &offset_size, section_is_gnu);
24222 if (mac_ptr == NULL)
24223 {
24224 /* We already issued a complaint. */
24225 return;
24226 }
24227
24228 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24229 GDB is still reading the definitions from command line. First
24230 DW_MACINFO_start_file will need to be ignored as it was already executed
24231 to create CURRENT_FILE for the main source holding also the command line
24232 definitions. On first met DW_MACINFO_start_file this flag is reset to
24233 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24234
24235 at_commandline = 1;
24236
24237 do
24238 {
24239 /* Do we at least have room for a macinfo type byte? */
24240 if (mac_ptr >= mac_end)
24241 {
24242 dwarf2_section_buffer_overflow_complaint (section);
24243 break;
24244 }
24245
24246 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24247 mac_ptr++;
24248
24249 /* Note that we rely on the fact that the corresponding GNU and
24250 DWARF constants are the same. */
24251 DIAGNOSTIC_PUSH
24252 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24253 switch (macinfo_type)
24254 {
24255 /* A zero macinfo type indicates the end of the macro
24256 information. */
24257 case 0:
24258 break;
24259
24260 case DW_MACRO_define:
24261 case DW_MACRO_undef:
24262 case DW_MACRO_define_strp:
24263 case DW_MACRO_undef_strp:
24264 case DW_MACRO_define_sup:
24265 case DW_MACRO_undef_sup:
24266 {
24267 unsigned int bytes_read;
24268 int line;
24269 const char *body;
24270 int is_define;
24271
24272 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24273 mac_ptr += bytes_read;
24274
24275 if (macinfo_type == DW_MACRO_define
24276 || macinfo_type == DW_MACRO_undef)
24277 {
24278 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24279 mac_ptr += bytes_read;
24280 }
24281 else
24282 {
24283 LONGEST str_offset;
24284
24285 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24286 mac_ptr += offset_size;
24287
24288 if (macinfo_type == DW_MACRO_define_sup
24289 || macinfo_type == DW_MACRO_undef_sup
24290 || section_is_dwz)
24291 {
24292 struct dwz_file *dwz
24293 = dwarf2_get_dwz_file (dwarf2_per_objfile);
24294
24295 body = read_indirect_string_from_dwz (objfile,
24296 dwz, str_offset);
24297 }
24298 else
24299 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24300 abfd, str_offset);
24301 }
24302
24303 is_define = (macinfo_type == DW_MACRO_define
24304 || macinfo_type == DW_MACRO_define_strp
24305 || macinfo_type == DW_MACRO_define_sup);
24306 if (! current_file)
24307 {
24308 /* DWARF violation as no main source is present. */
24309 complaint (&symfile_complaints,
24310 _("debug info with no main source gives macro %s "
24311 "on line %d: %s"),
24312 is_define ? _("definition") : _("undefinition"),
24313 line, body);
24314 break;
24315 }
24316 if ((line == 0 && !at_commandline)
24317 || (line != 0 && at_commandline))
24318 complaint (&symfile_complaints,
24319 _("debug info gives %s macro %s with %s line %d: %s"),
24320 at_commandline ? _("command-line") : _("in-file"),
24321 is_define ? _("definition") : _("undefinition"),
24322 line == 0 ? _("zero") : _("non-zero"), line, body);
24323
24324 if (is_define)
24325 parse_macro_definition (current_file, line, body);
24326 else
24327 {
24328 gdb_assert (macinfo_type == DW_MACRO_undef
24329 || macinfo_type == DW_MACRO_undef_strp
24330 || macinfo_type == DW_MACRO_undef_sup);
24331 macro_undef (current_file, line, body);
24332 }
24333 }
24334 break;
24335
24336 case DW_MACRO_start_file:
24337 {
24338 unsigned int bytes_read;
24339 int line, file;
24340
24341 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24342 mac_ptr += bytes_read;
24343 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24344 mac_ptr += bytes_read;
24345
24346 if ((line == 0 && !at_commandline)
24347 || (line != 0 && at_commandline))
24348 complaint (&symfile_complaints,
24349 _("debug info gives source %d included "
24350 "from %s at %s line %d"),
24351 file, at_commandline ? _("command-line") : _("file"),
24352 line == 0 ? _("zero") : _("non-zero"), line);
24353
24354 if (at_commandline)
24355 {
24356 /* This DW_MACRO_start_file was executed in the
24357 pass one. */
24358 at_commandline = 0;
24359 }
24360 else
24361 current_file = macro_start_file (file, line, current_file, lh);
24362 }
24363 break;
24364
24365 case DW_MACRO_end_file:
24366 if (! current_file)
24367 complaint (&symfile_complaints,
24368 _("macro debug info has an unmatched "
24369 "`close_file' directive"));
24370 else
24371 {
24372 current_file = current_file->included_by;
24373 if (! current_file)
24374 {
24375 enum dwarf_macro_record_type next_type;
24376
24377 /* GCC circa March 2002 doesn't produce the zero
24378 type byte marking the end of the compilation
24379 unit. Complain if it's not there, but exit no
24380 matter what. */
24381
24382 /* Do we at least have room for a macinfo type byte? */
24383 if (mac_ptr >= mac_end)
24384 {
24385 dwarf2_section_buffer_overflow_complaint (section);
24386 return;
24387 }
24388
24389 /* We don't increment mac_ptr here, so this is just
24390 a look-ahead. */
24391 next_type
24392 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24393 mac_ptr);
24394 if (next_type != 0)
24395 complaint (&symfile_complaints,
24396 _("no terminating 0-type entry for "
24397 "macros in `.debug_macinfo' section"));
24398
24399 return;
24400 }
24401 }
24402 break;
24403
24404 case DW_MACRO_import:
24405 case DW_MACRO_import_sup:
24406 {
24407 LONGEST offset;
24408 void **slot;
24409 bfd *include_bfd = abfd;
24410 struct dwarf2_section_info *include_section = section;
24411 const gdb_byte *include_mac_end = mac_end;
24412 int is_dwz = section_is_dwz;
24413 const gdb_byte *new_mac_ptr;
24414
24415 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24416 mac_ptr += offset_size;
24417
24418 if (macinfo_type == DW_MACRO_import_sup)
24419 {
24420 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
24421
24422 dwarf2_read_section (objfile, &dwz->macro);
24423
24424 include_section = &dwz->macro;
24425 include_bfd = get_section_bfd_owner (include_section);
24426 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24427 is_dwz = 1;
24428 }
24429
24430 new_mac_ptr = include_section->buffer + offset;
24431 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24432
24433 if (*slot != NULL)
24434 {
24435 /* This has actually happened; see
24436 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
24437 complaint (&symfile_complaints,
24438 _("recursive DW_MACRO_import in "
24439 ".debug_macro section"));
24440 }
24441 else
24442 {
24443 *slot = (void *) new_mac_ptr;
24444
24445 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24446 include_bfd, new_mac_ptr,
24447 include_mac_end, current_file, lh,
24448 section, section_is_gnu, is_dwz,
24449 offset_size, include_hash);
24450
24451 htab_remove_elt (include_hash, (void *) new_mac_ptr);
24452 }
24453 }
24454 break;
24455
24456 case DW_MACINFO_vendor_ext:
24457 if (!section_is_gnu)
24458 {
24459 unsigned int bytes_read;
24460
24461 /* This reads the constant, but since we don't recognize
24462 any vendor extensions, we ignore it. */
24463 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24464 mac_ptr += bytes_read;
24465 read_direct_string (abfd, mac_ptr, &bytes_read);
24466 mac_ptr += bytes_read;
24467
24468 /* We don't recognize any vendor extensions. */
24469 break;
24470 }
24471 /* FALLTHROUGH */
24472
24473 default:
24474 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24475 mac_ptr, mac_end, abfd, offset_size,
24476 section);
24477 if (mac_ptr == NULL)
24478 return;
24479 break;
24480 }
24481 DIAGNOSTIC_POP
24482 } while (macinfo_type != 0);
24483 }
24484
24485 static void
24486 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24487 int section_is_gnu)
24488 {
24489 struct dwarf2_per_objfile *dwarf2_per_objfile
24490 = cu->per_cu->dwarf2_per_objfile;
24491 struct objfile *objfile = dwarf2_per_objfile->objfile;
24492 struct line_header *lh = cu->line_header;
24493 bfd *abfd;
24494 const gdb_byte *mac_ptr, *mac_end;
24495 struct macro_source_file *current_file = 0;
24496 enum dwarf_macro_record_type macinfo_type;
24497 unsigned int offset_size = cu->header.offset_size;
24498 const gdb_byte *opcode_definitions[256];
24499 void **slot;
24500 struct dwarf2_section_info *section;
24501 const char *section_name;
24502
24503 if (cu->dwo_unit != NULL)
24504 {
24505 if (section_is_gnu)
24506 {
24507 section = &cu->dwo_unit->dwo_file->sections.macro;
24508 section_name = ".debug_macro.dwo";
24509 }
24510 else
24511 {
24512 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24513 section_name = ".debug_macinfo.dwo";
24514 }
24515 }
24516 else
24517 {
24518 if (section_is_gnu)
24519 {
24520 section = &dwarf2_per_objfile->macro;
24521 section_name = ".debug_macro";
24522 }
24523 else
24524 {
24525 section = &dwarf2_per_objfile->macinfo;
24526 section_name = ".debug_macinfo";
24527 }
24528 }
24529
24530 dwarf2_read_section (objfile, section);
24531 if (section->buffer == NULL)
24532 {
24533 complaint (&symfile_complaints, _("missing %s section"), section_name);
24534 return;
24535 }
24536 abfd = get_section_bfd_owner (section);
24537
24538 /* First pass: Find the name of the base filename.
24539 This filename is needed in order to process all macros whose definition
24540 (or undefinition) comes from the command line. These macros are defined
24541 before the first DW_MACINFO_start_file entry, and yet still need to be
24542 associated to the base file.
24543
24544 To determine the base file name, we scan the macro definitions until we
24545 reach the first DW_MACINFO_start_file entry. We then initialize
24546 CURRENT_FILE accordingly so that any macro definition found before the
24547 first DW_MACINFO_start_file can still be associated to the base file. */
24548
24549 mac_ptr = section->buffer + offset;
24550 mac_end = section->buffer + section->size;
24551
24552 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24553 &offset_size, section_is_gnu);
24554 if (mac_ptr == NULL)
24555 {
24556 /* We already issued a complaint. */
24557 return;
24558 }
24559
24560 do
24561 {
24562 /* Do we at least have room for a macinfo type byte? */
24563 if (mac_ptr >= mac_end)
24564 {
24565 /* Complaint is printed during the second pass as GDB will probably
24566 stop the first pass earlier upon finding
24567 DW_MACINFO_start_file. */
24568 break;
24569 }
24570
24571 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24572 mac_ptr++;
24573
24574 /* Note that we rely on the fact that the corresponding GNU and
24575 DWARF constants are the same. */
24576 DIAGNOSTIC_PUSH
24577 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24578 switch (macinfo_type)
24579 {
24580 /* A zero macinfo type indicates the end of the macro
24581 information. */
24582 case 0:
24583 break;
24584
24585 case DW_MACRO_define:
24586 case DW_MACRO_undef:
24587 /* Only skip the data by MAC_PTR. */
24588 {
24589 unsigned int bytes_read;
24590
24591 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24592 mac_ptr += bytes_read;
24593 read_direct_string (abfd, mac_ptr, &bytes_read);
24594 mac_ptr += bytes_read;
24595 }
24596 break;
24597
24598 case DW_MACRO_start_file:
24599 {
24600 unsigned int bytes_read;
24601 int line, file;
24602
24603 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24604 mac_ptr += bytes_read;
24605 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24606 mac_ptr += bytes_read;
24607
24608 current_file = macro_start_file (file, line, current_file, lh);
24609 }
24610 break;
24611
24612 case DW_MACRO_end_file:
24613 /* No data to skip by MAC_PTR. */
24614 break;
24615
24616 case DW_MACRO_define_strp:
24617 case DW_MACRO_undef_strp:
24618 case DW_MACRO_define_sup:
24619 case DW_MACRO_undef_sup:
24620 {
24621 unsigned int bytes_read;
24622
24623 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24624 mac_ptr += bytes_read;
24625 mac_ptr += offset_size;
24626 }
24627 break;
24628
24629 case DW_MACRO_import:
24630 case DW_MACRO_import_sup:
24631 /* Note that, according to the spec, a transparent include
24632 chain cannot call DW_MACRO_start_file. So, we can just
24633 skip this opcode. */
24634 mac_ptr += offset_size;
24635 break;
24636
24637 case DW_MACINFO_vendor_ext:
24638 /* Only skip the data by MAC_PTR. */
24639 if (!section_is_gnu)
24640 {
24641 unsigned int bytes_read;
24642
24643 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24644 mac_ptr += bytes_read;
24645 read_direct_string (abfd, mac_ptr, &bytes_read);
24646 mac_ptr += bytes_read;
24647 }
24648 /* FALLTHROUGH */
24649
24650 default:
24651 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24652 mac_ptr, mac_end, abfd, offset_size,
24653 section);
24654 if (mac_ptr == NULL)
24655 return;
24656 break;
24657 }
24658 DIAGNOSTIC_POP
24659 } while (macinfo_type != 0 && current_file == NULL);
24660
24661 /* Second pass: Process all entries.
24662
24663 Use the AT_COMMAND_LINE flag to determine whether we are still processing
24664 command-line macro definitions/undefinitions. This flag is unset when we
24665 reach the first DW_MACINFO_start_file entry. */
24666
24667 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
24668 htab_eq_pointer,
24669 NULL, xcalloc, xfree));
24670 mac_ptr = section->buffer + offset;
24671 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
24672 *slot = (void *) mac_ptr;
24673 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24674 abfd, mac_ptr, mac_end,
24675 current_file, lh, section,
24676 section_is_gnu, 0, offset_size,
24677 include_hash.get ());
24678 }
24679
24680 /* Check if the attribute's form is a DW_FORM_block*
24681 if so return true else false. */
24682
24683 static int
24684 attr_form_is_block (const struct attribute *attr)
24685 {
24686 return (attr == NULL ? 0 :
24687 attr->form == DW_FORM_block1
24688 || attr->form == DW_FORM_block2
24689 || attr->form == DW_FORM_block4
24690 || attr->form == DW_FORM_block
24691 || attr->form == DW_FORM_exprloc);
24692 }
24693
24694 /* Return non-zero if ATTR's value is a section offset --- classes
24695 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
24696 You may use DW_UNSND (attr) to retrieve such offsets.
24697
24698 Section 7.5.4, "Attribute Encodings", explains that no attribute
24699 may have a value that belongs to more than one of these classes; it
24700 would be ambiguous if we did, because we use the same forms for all
24701 of them. */
24702
24703 static int
24704 attr_form_is_section_offset (const struct attribute *attr)
24705 {
24706 return (attr->form == DW_FORM_data4
24707 || attr->form == DW_FORM_data8
24708 || attr->form == DW_FORM_sec_offset);
24709 }
24710
24711 /* Return non-zero if ATTR's value falls in the 'constant' class, or
24712 zero otherwise. When this function returns true, you can apply
24713 dwarf2_get_attr_constant_value to it.
24714
24715 However, note that for some attributes you must check
24716 attr_form_is_section_offset before using this test. DW_FORM_data4
24717 and DW_FORM_data8 are members of both the constant class, and of
24718 the classes that contain offsets into other debug sections
24719 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
24720 that, if an attribute's can be either a constant or one of the
24721 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
24722 taken as section offsets, not constants.
24723
24724 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
24725 cannot handle that. */
24726
24727 static int
24728 attr_form_is_constant (const struct attribute *attr)
24729 {
24730 switch (attr->form)
24731 {
24732 case DW_FORM_sdata:
24733 case DW_FORM_udata:
24734 case DW_FORM_data1:
24735 case DW_FORM_data2:
24736 case DW_FORM_data4:
24737 case DW_FORM_data8:
24738 case DW_FORM_implicit_const:
24739 return 1;
24740 default:
24741 return 0;
24742 }
24743 }
24744
24745
24746 /* DW_ADDR is always stored already as sect_offset; despite for the forms
24747 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
24748
24749 static int
24750 attr_form_is_ref (const struct attribute *attr)
24751 {
24752 switch (attr->form)
24753 {
24754 case DW_FORM_ref_addr:
24755 case DW_FORM_ref1:
24756 case DW_FORM_ref2:
24757 case DW_FORM_ref4:
24758 case DW_FORM_ref8:
24759 case DW_FORM_ref_udata:
24760 case DW_FORM_GNU_ref_alt:
24761 return 1;
24762 default:
24763 return 0;
24764 }
24765 }
24766
24767 /* Return the .debug_loc section to use for CU.
24768 For DWO files use .debug_loc.dwo. */
24769
24770 static struct dwarf2_section_info *
24771 cu_debug_loc_section (struct dwarf2_cu *cu)
24772 {
24773 struct dwarf2_per_objfile *dwarf2_per_objfile
24774 = cu->per_cu->dwarf2_per_objfile;
24775
24776 if (cu->dwo_unit)
24777 {
24778 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24779
24780 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24781 }
24782 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
24783 : &dwarf2_per_objfile->loc);
24784 }
24785
24786 /* A helper function that fills in a dwarf2_loclist_baton. */
24787
24788 static void
24789 fill_in_loclist_baton (struct dwarf2_cu *cu,
24790 struct dwarf2_loclist_baton *baton,
24791 const struct attribute *attr)
24792 {
24793 struct dwarf2_per_objfile *dwarf2_per_objfile
24794 = cu->per_cu->dwarf2_per_objfile;
24795 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24796
24797 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
24798
24799 baton->per_cu = cu->per_cu;
24800 gdb_assert (baton->per_cu);
24801 /* We don't know how long the location list is, but make sure we
24802 don't run off the edge of the section. */
24803 baton->size = section->size - DW_UNSND (attr);
24804 baton->data = section->buffer + DW_UNSND (attr);
24805 baton->base_address = cu->base_address;
24806 baton->from_dwo = cu->dwo_unit != NULL;
24807 }
24808
24809 static void
24810 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24811 struct dwarf2_cu *cu, int is_block)
24812 {
24813 struct dwarf2_per_objfile *dwarf2_per_objfile
24814 = cu->per_cu->dwarf2_per_objfile;
24815 struct objfile *objfile = dwarf2_per_objfile->objfile;
24816 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24817
24818 if (attr_form_is_section_offset (attr)
24819 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24820 the section. If so, fall through to the complaint in the
24821 other branch. */
24822 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
24823 {
24824 struct dwarf2_loclist_baton *baton;
24825
24826 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24827
24828 fill_in_loclist_baton (cu, baton, attr);
24829
24830 if (cu->base_known == 0)
24831 complaint (&symfile_complaints,
24832 _("Location list used without "
24833 "specifying the CU base address."));
24834
24835 SYMBOL_ACLASS_INDEX (sym) = (is_block
24836 ? dwarf2_loclist_block_index
24837 : dwarf2_loclist_index);
24838 SYMBOL_LOCATION_BATON (sym) = baton;
24839 }
24840 else
24841 {
24842 struct dwarf2_locexpr_baton *baton;
24843
24844 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24845 baton->per_cu = cu->per_cu;
24846 gdb_assert (baton->per_cu);
24847
24848 if (attr_form_is_block (attr))
24849 {
24850 /* Note that we're just copying the block's data pointer
24851 here, not the actual data. We're still pointing into the
24852 info_buffer for SYM's objfile; right now we never release
24853 that buffer, but when we do clean up properly this may
24854 need to change. */
24855 baton->size = DW_BLOCK (attr)->size;
24856 baton->data = DW_BLOCK (attr)->data;
24857 }
24858 else
24859 {
24860 dwarf2_invalid_attrib_class_complaint ("location description",
24861 SYMBOL_NATURAL_NAME (sym));
24862 baton->size = 0;
24863 }
24864
24865 SYMBOL_ACLASS_INDEX (sym) = (is_block
24866 ? dwarf2_locexpr_block_index
24867 : dwarf2_locexpr_index);
24868 SYMBOL_LOCATION_BATON (sym) = baton;
24869 }
24870 }
24871
24872 /* Return the OBJFILE associated with the compilation unit CU. If CU
24873 came from a separate debuginfo file, then the master objfile is
24874 returned. */
24875
24876 struct objfile *
24877 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
24878 {
24879 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
24880
24881 /* Return the master objfile, so that we can report and look up the
24882 correct file containing this variable. */
24883 if (objfile->separate_debug_objfile_backlink)
24884 objfile = objfile->separate_debug_objfile_backlink;
24885
24886 return objfile;
24887 }
24888
24889 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
24890 (CU_HEADERP is unused in such case) or prepare a temporary copy at
24891 CU_HEADERP first. */
24892
24893 static const struct comp_unit_head *
24894 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
24895 struct dwarf2_per_cu_data *per_cu)
24896 {
24897 const gdb_byte *info_ptr;
24898
24899 if (per_cu->cu)
24900 return &per_cu->cu->header;
24901
24902 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
24903
24904 memset (cu_headerp, 0, sizeof (*cu_headerp));
24905 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
24906 rcuh_kind::COMPILE);
24907
24908 return cu_headerp;
24909 }
24910
24911 /* Return the address size given in the compilation unit header for CU. */
24912
24913 int
24914 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
24915 {
24916 struct comp_unit_head cu_header_local;
24917 const struct comp_unit_head *cu_headerp;
24918
24919 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24920
24921 return cu_headerp->addr_size;
24922 }
24923
24924 /* Return the offset size given in the compilation unit header for CU. */
24925
24926 int
24927 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
24928 {
24929 struct comp_unit_head cu_header_local;
24930 const struct comp_unit_head *cu_headerp;
24931
24932 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24933
24934 return cu_headerp->offset_size;
24935 }
24936
24937 /* See its dwarf2loc.h declaration. */
24938
24939 int
24940 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
24941 {
24942 struct comp_unit_head cu_header_local;
24943 const struct comp_unit_head *cu_headerp;
24944
24945 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24946
24947 if (cu_headerp->version == 2)
24948 return cu_headerp->addr_size;
24949 else
24950 return cu_headerp->offset_size;
24951 }
24952
24953 /* Return the text offset of the CU. The returned offset comes from
24954 this CU's objfile. If this objfile came from a separate debuginfo
24955 file, then the offset may be different from the corresponding
24956 offset in the parent objfile. */
24957
24958 CORE_ADDR
24959 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
24960 {
24961 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
24962
24963 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
24964 }
24965
24966 /* Return DWARF version number of PER_CU. */
24967
24968 short
24969 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
24970 {
24971 return per_cu->dwarf_version;
24972 }
24973
24974 /* Locate the .debug_info compilation unit from CU's objfile which contains
24975 the DIE at OFFSET. Raises an error on failure. */
24976
24977 static struct dwarf2_per_cu_data *
24978 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24979 unsigned int offset_in_dwz,
24980 struct dwarf2_per_objfile *dwarf2_per_objfile)
24981 {
24982 struct dwarf2_per_cu_data *this_cu;
24983 int low, high;
24984 const sect_offset *cu_off;
24985
24986 low = 0;
24987 high = dwarf2_per_objfile->all_comp_units.size () - 1;
24988 while (high > low)
24989 {
24990 struct dwarf2_per_cu_data *mid_cu;
24991 int mid = low + (high - low) / 2;
24992
24993 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
24994 cu_off = &mid_cu->sect_off;
24995 if (mid_cu->is_dwz > offset_in_dwz
24996 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
24997 high = mid;
24998 else
24999 low = mid + 1;
25000 }
25001 gdb_assert (low == high);
25002 this_cu = dwarf2_per_objfile->all_comp_units[low];
25003 cu_off = &this_cu->sect_off;
25004 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
25005 {
25006 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
25007 error (_("Dwarf Error: could not find partial DIE containing "
25008 "offset %s [in module %s]"),
25009 sect_offset_str (sect_off),
25010 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
25011
25012 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25013 <= sect_off);
25014 return dwarf2_per_objfile->all_comp_units[low-1];
25015 }
25016 else
25017 {
25018 this_cu = dwarf2_per_objfile->all_comp_units[low];
25019 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
25020 && sect_off >= this_cu->sect_off + this_cu->length)
25021 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
25022 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
25023 return this_cu;
25024 }
25025 }
25026
25027 /* Initialize dwarf2_cu CU, owned by PER_CU. */
25028
25029 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25030 : per_cu (per_cu_),
25031 mark (0),
25032 has_loclist (0),
25033 checked_producer (0),
25034 producer_is_gxx_lt_4_6 (0),
25035 producer_is_gcc_lt_4_3 (0),
25036 producer_is_icc_lt_14 (0),
25037 processing_has_namespace_info (0)
25038 {
25039 per_cu->cu = this;
25040 }
25041
25042 /* Destroy a dwarf2_cu. */
25043
25044 dwarf2_cu::~dwarf2_cu ()
25045 {
25046 per_cu->cu = NULL;
25047 }
25048
25049 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25050
25051 static void
25052 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25053 enum language pretend_language)
25054 {
25055 struct attribute *attr;
25056
25057 /* Set the language we're debugging. */
25058 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25059 if (attr)
25060 set_cu_language (DW_UNSND (attr), cu);
25061 else
25062 {
25063 cu->language = pretend_language;
25064 cu->language_defn = language_def (cu->language);
25065 }
25066
25067 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25068 }
25069
25070 /* Increase the age counter on each cached compilation unit, and free
25071 any that are too old. */
25072
25073 static void
25074 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
25075 {
25076 struct dwarf2_per_cu_data *per_cu, **last_chain;
25077
25078 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25079 per_cu = dwarf2_per_objfile->read_in_chain;
25080 while (per_cu != NULL)
25081 {
25082 per_cu->cu->last_used ++;
25083 if (per_cu->cu->last_used <= dwarf_max_cache_age)
25084 dwarf2_mark (per_cu->cu);
25085 per_cu = per_cu->cu->read_in_chain;
25086 }
25087
25088 per_cu = dwarf2_per_objfile->read_in_chain;
25089 last_chain = &dwarf2_per_objfile->read_in_chain;
25090 while (per_cu != NULL)
25091 {
25092 struct dwarf2_per_cu_data *next_cu;
25093
25094 next_cu = per_cu->cu->read_in_chain;
25095
25096 if (!per_cu->cu->mark)
25097 {
25098 delete per_cu->cu;
25099 *last_chain = next_cu;
25100 }
25101 else
25102 last_chain = &per_cu->cu->read_in_chain;
25103
25104 per_cu = next_cu;
25105 }
25106 }
25107
25108 /* Remove a single compilation unit from the cache. */
25109
25110 static void
25111 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
25112 {
25113 struct dwarf2_per_cu_data *per_cu, **last_chain;
25114 struct dwarf2_per_objfile *dwarf2_per_objfile
25115 = target_per_cu->dwarf2_per_objfile;
25116
25117 per_cu = dwarf2_per_objfile->read_in_chain;
25118 last_chain = &dwarf2_per_objfile->read_in_chain;
25119 while (per_cu != NULL)
25120 {
25121 struct dwarf2_per_cu_data *next_cu;
25122
25123 next_cu = per_cu->cu->read_in_chain;
25124
25125 if (per_cu == target_per_cu)
25126 {
25127 delete per_cu->cu;
25128 per_cu->cu = NULL;
25129 *last_chain = next_cu;
25130 break;
25131 }
25132 else
25133 last_chain = &per_cu->cu->read_in_chain;
25134
25135 per_cu = next_cu;
25136 }
25137 }
25138
25139 /* Release all extra memory associated with OBJFILE. */
25140
25141 void
25142 dwarf2_free_objfile (struct objfile *objfile)
25143 {
25144 struct dwarf2_per_objfile *dwarf2_per_objfile
25145 = get_dwarf2_per_objfile (objfile);
25146
25147 delete dwarf2_per_objfile;
25148 }
25149
25150 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25151 We store these in a hash table separate from the DIEs, and preserve them
25152 when the DIEs are flushed out of cache.
25153
25154 The CU "per_cu" pointer is needed because offset alone is not enough to
25155 uniquely identify the type. A file may have multiple .debug_types sections,
25156 or the type may come from a DWO file. Furthermore, while it's more logical
25157 to use per_cu->section+offset, with Fission the section with the data is in
25158 the DWO file but we don't know that section at the point we need it.
25159 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25160 because we can enter the lookup routine, get_die_type_at_offset, from
25161 outside this file, and thus won't necessarily have PER_CU->cu.
25162 Fortunately, PER_CU is stable for the life of the objfile. */
25163
25164 struct dwarf2_per_cu_offset_and_type
25165 {
25166 const struct dwarf2_per_cu_data *per_cu;
25167 sect_offset sect_off;
25168 struct type *type;
25169 };
25170
25171 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25172
25173 static hashval_t
25174 per_cu_offset_and_type_hash (const void *item)
25175 {
25176 const struct dwarf2_per_cu_offset_and_type *ofs
25177 = (const struct dwarf2_per_cu_offset_and_type *) item;
25178
25179 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25180 }
25181
25182 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25183
25184 static int
25185 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25186 {
25187 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25188 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25189 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25190 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25191
25192 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25193 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25194 }
25195
25196 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25197 table if necessary. For convenience, return TYPE.
25198
25199 The DIEs reading must have careful ordering to:
25200 * Not cause infite loops trying to read in DIEs as a prerequisite for
25201 reading current DIE.
25202 * Not trying to dereference contents of still incompletely read in types
25203 while reading in other DIEs.
25204 * Enable referencing still incompletely read in types just by a pointer to
25205 the type without accessing its fields.
25206
25207 Therefore caller should follow these rules:
25208 * Try to fetch any prerequisite types we may need to build this DIE type
25209 before building the type and calling set_die_type.
25210 * After building type call set_die_type for current DIE as soon as
25211 possible before fetching more types to complete the current type.
25212 * Make the type as complete as possible before fetching more types. */
25213
25214 static struct type *
25215 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25216 {
25217 struct dwarf2_per_objfile *dwarf2_per_objfile
25218 = cu->per_cu->dwarf2_per_objfile;
25219 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25220 struct objfile *objfile = dwarf2_per_objfile->objfile;
25221 struct attribute *attr;
25222 struct dynamic_prop prop;
25223
25224 /* For Ada types, make sure that the gnat-specific data is always
25225 initialized (if not already set). There are a few types where
25226 we should not be doing so, because the type-specific area is
25227 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25228 where the type-specific area is used to store the floatformat).
25229 But this is not a problem, because the gnat-specific information
25230 is actually not needed for these types. */
25231 if (need_gnat_info (cu)
25232 && TYPE_CODE (type) != TYPE_CODE_FUNC
25233 && TYPE_CODE (type) != TYPE_CODE_FLT
25234 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25235 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25236 && TYPE_CODE (type) != TYPE_CODE_METHOD
25237 && !HAVE_GNAT_AUX_INFO (type))
25238 INIT_GNAT_SPECIFIC (type);
25239
25240 /* Read DW_AT_allocated and set in type. */
25241 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25242 if (attr_form_is_block (attr))
25243 {
25244 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25245 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
25246 }
25247 else if (attr != NULL)
25248 {
25249 complaint (&symfile_complaints,
25250 _("DW_AT_allocated has the wrong form (%s) at DIE %s"),
25251 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25252 sect_offset_str (die->sect_off));
25253 }
25254
25255 /* Read DW_AT_associated and set in type. */
25256 attr = dwarf2_attr (die, DW_AT_associated, cu);
25257 if (attr_form_is_block (attr))
25258 {
25259 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25260 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
25261 }
25262 else if (attr != NULL)
25263 {
25264 complaint (&symfile_complaints,
25265 _("DW_AT_associated has the wrong form (%s) at DIE %s"),
25266 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25267 sect_offset_str (die->sect_off));
25268 }
25269
25270 /* Read DW_AT_data_location and set in type. */
25271 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25272 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25273 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
25274
25275 if (dwarf2_per_objfile->die_type_hash == NULL)
25276 {
25277 dwarf2_per_objfile->die_type_hash =
25278 htab_create_alloc_ex (127,
25279 per_cu_offset_and_type_hash,
25280 per_cu_offset_and_type_eq,
25281 NULL,
25282 &objfile->objfile_obstack,
25283 hashtab_obstack_allocate,
25284 dummy_obstack_deallocate);
25285 }
25286
25287 ofs.per_cu = cu->per_cu;
25288 ofs.sect_off = die->sect_off;
25289 ofs.type = type;
25290 slot = (struct dwarf2_per_cu_offset_and_type **)
25291 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
25292 if (*slot)
25293 complaint (&symfile_complaints,
25294 _("A problem internal to GDB: DIE %s has type already set"),
25295 sect_offset_str (die->sect_off));
25296 *slot = XOBNEW (&objfile->objfile_obstack,
25297 struct dwarf2_per_cu_offset_and_type);
25298 **slot = ofs;
25299 return type;
25300 }
25301
25302 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25303 or return NULL if the die does not have a saved type. */
25304
25305 static struct type *
25306 get_die_type_at_offset (sect_offset sect_off,
25307 struct dwarf2_per_cu_data *per_cu)
25308 {
25309 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25310 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
25311
25312 if (dwarf2_per_objfile->die_type_hash == NULL)
25313 return NULL;
25314
25315 ofs.per_cu = per_cu;
25316 ofs.sect_off = sect_off;
25317 slot = ((struct dwarf2_per_cu_offset_and_type *)
25318 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
25319 if (slot)
25320 return slot->type;
25321 else
25322 return NULL;
25323 }
25324
25325 /* Look up the type for DIE in CU in die_type_hash,
25326 or return NULL if DIE does not have a saved type. */
25327
25328 static struct type *
25329 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25330 {
25331 return get_die_type_at_offset (die->sect_off, cu->per_cu);
25332 }
25333
25334 /* Add a dependence relationship from CU to REF_PER_CU. */
25335
25336 static void
25337 dwarf2_add_dependence (struct dwarf2_cu *cu,
25338 struct dwarf2_per_cu_data *ref_per_cu)
25339 {
25340 void **slot;
25341
25342 if (cu->dependencies == NULL)
25343 cu->dependencies
25344 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25345 NULL, &cu->comp_unit_obstack,
25346 hashtab_obstack_allocate,
25347 dummy_obstack_deallocate);
25348
25349 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25350 if (*slot == NULL)
25351 *slot = ref_per_cu;
25352 }
25353
25354 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25355 Set the mark field in every compilation unit in the
25356 cache that we must keep because we are keeping CU. */
25357
25358 static int
25359 dwarf2_mark_helper (void **slot, void *data)
25360 {
25361 struct dwarf2_per_cu_data *per_cu;
25362
25363 per_cu = (struct dwarf2_per_cu_data *) *slot;
25364
25365 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25366 reading of the chain. As such dependencies remain valid it is not much
25367 useful to track and undo them during QUIT cleanups. */
25368 if (per_cu->cu == NULL)
25369 return 1;
25370
25371 if (per_cu->cu->mark)
25372 return 1;
25373 per_cu->cu->mark = 1;
25374
25375 if (per_cu->cu->dependencies != NULL)
25376 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25377
25378 return 1;
25379 }
25380
25381 /* Set the mark field in CU and in every other compilation unit in the
25382 cache that we must keep because we are keeping CU. */
25383
25384 static void
25385 dwarf2_mark (struct dwarf2_cu *cu)
25386 {
25387 if (cu->mark)
25388 return;
25389 cu->mark = 1;
25390 if (cu->dependencies != NULL)
25391 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
25392 }
25393
25394 static void
25395 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25396 {
25397 while (per_cu)
25398 {
25399 per_cu->cu->mark = 0;
25400 per_cu = per_cu->cu->read_in_chain;
25401 }
25402 }
25403
25404 /* Trivial hash function for partial_die_info: the hash value of a DIE
25405 is its offset in .debug_info for this objfile. */
25406
25407 static hashval_t
25408 partial_die_hash (const void *item)
25409 {
25410 const struct partial_die_info *part_die
25411 = (const struct partial_die_info *) item;
25412
25413 return to_underlying (part_die->sect_off);
25414 }
25415
25416 /* Trivial comparison function for partial_die_info structures: two DIEs
25417 are equal if they have the same offset. */
25418
25419 static int
25420 partial_die_eq (const void *item_lhs, const void *item_rhs)
25421 {
25422 const struct partial_die_info *part_die_lhs
25423 = (const struct partial_die_info *) item_lhs;
25424 const struct partial_die_info *part_die_rhs
25425 = (const struct partial_die_info *) item_rhs;
25426
25427 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25428 }
25429
25430 static struct cmd_list_element *set_dwarf_cmdlist;
25431 static struct cmd_list_element *show_dwarf_cmdlist;
25432
25433 static void
25434 set_dwarf_cmd (const char *args, int from_tty)
25435 {
25436 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
25437 gdb_stdout);
25438 }
25439
25440 static void
25441 show_dwarf_cmd (const char *args, int from_tty)
25442 {
25443 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
25444 }
25445
25446 int dwarf_always_disassemble;
25447
25448 static void
25449 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
25450 struct cmd_list_element *c, const char *value)
25451 {
25452 fprintf_filtered (file,
25453 _("Whether to always disassemble "
25454 "DWARF expressions is %s.\n"),
25455 value);
25456 }
25457
25458 static void
25459 show_check_physname (struct ui_file *file, int from_tty,
25460 struct cmd_list_element *c, const char *value)
25461 {
25462 fprintf_filtered (file,
25463 _("Whether to check \"physname\" is %s.\n"),
25464 value);
25465 }
25466
25467 void
25468 _initialize_dwarf2_read (void)
25469 {
25470
25471 dwarf2_objfile_data_key = register_objfile_data ();
25472
25473 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
25474 Set DWARF specific variables.\n\
25475 Configure DWARF variables such as the cache size"),
25476 &set_dwarf_cmdlist, "maintenance set dwarf ",
25477 0/*allow-unknown*/, &maintenance_set_cmdlist);
25478
25479 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
25480 Show DWARF specific variables\n\
25481 Show DWARF variables such as the cache size"),
25482 &show_dwarf_cmdlist, "maintenance show dwarf ",
25483 0/*allow-unknown*/, &maintenance_show_cmdlist);
25484
25485 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25486 &dwarf_max_cache_age, _("\
25487 Set the upper bound on the age of cached DWARF compilation units."), _("\
25488 Show the upper bound on the age of cached DWARF compilation units."), _("\
25489 A higher limit means that cached compilation units will be stored\n\
25490 in memory longer, and more total memory will be used. Zero disables\n\
25491 caching, which can slow down startup."),
25492 NULL,
25493 show_dwarf_max_cache_age,
25494 &set_dwarf_cmdlist,
25495 &show_dwarf_cmdlist);
25496
25497 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
25498 &dwarf_always_disassemble, _("\
25499 Set whether `info address' always disassembles DWARF expressions."), _("\
25500 Show whether `info address' always disassembles DWARF expressions."), _("\
25501 When enabled, DWARF expressions are always printed in an assembly-like\n\
25502 syntax. When disabled, expressions will be printed in a more\n\
25503 conversational style, when possible."),
25504 NULL,
25505 show_dwarf_always_disassemble,
25506 &set_dwarf_cmdlist,
25507 &show_dwarf_cmdlist);
25508
25509 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25510 Set debugging of the DWARF reader."), _("\
25511 Show debugging of the DWARF reader."), _("\
25512 When enabled (non-zero), debugging messages are printed during DWARF\n\
25513 reading and symtab expansion. A value of 1 (one) provides basic\n\
25514 information. A value greater than 1 provides more verbose information."),
25515 NULL,
25516 NULL,
25517 &setdebuglist, &showdebuglist);
25518
25519 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25520 Set debugging of the DWARF DIE reader."), _("\
25521 Show debugging of the DWARF DIE reader."), _("\
25522 When enabled (non-zero), DIEs are dumped after they are read in.\n\
25523 The value is the maximum depth to print."),
25524 NULL,
25525 NULL,
25526 &setdebuglist, &showdebuglist);
25527
25528 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25529 Set debugging of the dwarf line reader."), _("\
25530 Show debugging of the dwarf line reader."), _("\
25531 When enabled (non-zero), line number entries are dumped as they are read in.\n\
25532 A value of 1 (one) provides basic information.\n\
25533 A value greater than 1 provides more verbose information."),
25534 NULL,
25535 NULL,
25536 &setdebuglist, &showdebuglist);
25537
25538 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25539 Set cross-checking of \"physname\" code against demangler."), _("\
25540 Show cross-checking of \"physname\" code against demangler."), _("\
25541 When enabled, GDB's internal \"physname\" code is checked against\n\
25542 the demangler."),
25543 NULL, show_check_physname,
25544 &setdebuglist, &showdebuglist);
25545
25546 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25547 no_class, &use_deprecated_index_sections, _("\
25548 Set whether to use deprecated gdb_index sections."), _("\
25549 Show whether to use deprecated gdb_index sections."), _("\
25550 When enabled, deprecated .gdb_index sections are used anyway.\n\
25551 Normally they are ignored either because of a missing feature or\n\
25552 performance issue.\n\
25553 Warning: This option must be enabled before gdb reads the file."),
25554 NULL,
25555 NULL,
25556 &setlist, &showlist);
25557
25558 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25559 &dwarf2_locexpr_funcs);
25560 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25561 &dwarf2_loclist_funcs);
25562
25563 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25564 &dwarf2_block_frame_base_locexpr_funcs);
25565 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25566 &dwarf2_block_frame_base_loclist_funcs);
25567
25568 #if GDB_SELF_TEST
25569 selftests::register_test ("dw2_expand_symtabs_matching",
25570 selftests::dw2_expand_symtabs_matching::run_test);
25571 #endif
25572 }
This page took 0.529313 seconds and 5 git commands to generate.