Remove mapped_index::total_size
[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;
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;
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 /* Name of the file. */
811 const char *name;
812
813 /* File format version. */
814 int version;
815
816 /* The bfd. */
817 bfd *dbfd;
818
819 /* Section info for this file. */
820 struct dwp_sections sections;
821
822 /* Table of CUs in the file. */
823 const struct dwp_hash_table *cus;
824
825 /* Table of TUs in the file. */
826 const struct dwp_hash_table *tus;
827
828 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
829 htab_t loaded_cus;
830 htab_t loaded_tus;
831
832 /* Table to map ELF section numbers to their sections.
833 This is only needed for the DWP V1 file format. */
834 unsigned int num_sections;
835 asection **elf_sections;
836 };
837
838 /* This represents a '.dwz' file. */
839
840 struct dwz_file
841 {
842 /* A dwz file can only contain a few sections. */
843 struct dwarf2_section_info abbrev;
844 struct dwarf2_section_info info;
845 struct dwarf2_section_info str;
846 struct dwarf2_section_info line;
847 struct dwarf2_section_info macro;
848 struct dwarf2_section_info gdb_index;
849 struct dwarf2_section_info debug_names;
850
851 /* The dwz's BFD. */
852 bfd *dwz_bfd;
853 };
854
855 /* Struct used to pass misc. parameters to read_die_and_children, et
856 al. which are used for both .debug_info and .debug_types dies.
857 All parameters here are unchanging for the life of the call. This
858 struct exists to abstract away the constant parameters of die reading. */
859
860 struct die_reader_specs
861 {
862 /* The bfd of die_section. */
863 bfd* abfd;
864
865 /* The CU of the DIE we are parsing. */
866 struct dwarf2_cu *cu;
867
868 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
869 struct dwo_file *dwo_file;
870
871 /* The section the die comes from.
872 This is either .debug_info or .debug_types, or the .dwo variants. */
873 struct dwarf2_section_info *die_section;
874
875 /* die_section->buffer. */
876 const gdb_byte *buffer;
877
878 /* The end of the buffer. */
879 const gdb_byte *buffer_end;
880
881 /* The value of the DW_AT_comp_dir attribute. */
882 const char *comp_dir;
883
884 /* The abbreviation table to use when reading the DIEs. */
885 struct abbrev_table *abbrev_table;
886 };
887
888 /* Type of function passed to init_cutu_and_read_dies, et.al. */
889 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
890 const gdb_byte *info_ptr,
891 struct die_info *comp_unit_die,
892 int has_children,
893 void *data);
894
895 /* A 1-based directory index. This is a strong typedef to prevent
896 accidentally using a directory index as a 0-based index into an
897 array/vector. */
898 enum class dir_index : unsigned int {};
899
900 /* Likewise, a 1-based file name index. */
901 enum class file_name_index : unsigned int {};
902
903 struct file_entry
904 {
905 file_entry () = default;
906
907 file_entry (const char *name_, dir_index d_index_,
908 unsigned int mod_time_, unsigned int length_)
909 : name (name_),
910 d_index (d_index_),
911 mod_time (mod_time_),
912 length (length_)
913 {}
914
915 /* Return the include directory at D_INDEX stored in LH. Returns
916 NULL if D_INDEX is out of bounds. */
917 const char *include_dir (const line_header *lh) const;
918
919 /* The file name. Note this is an observing pointer. The memory is
920 owned by debug_line_buffer. */
921 const char *name {};
922
923 /* The directory index (1-based). */
924 dir_index d_index {};
925
926 unsigned int mod_time {};
927
928 unsigned int length {};
929
930 /* True if referenced by the Line Number Program. */
931 bool included_p {};
932
933 /* The associated symbol table, if any. */
934 struct symtab *symtab {};
935 };
936
937 /* The line number information for a compilation unit (found in the
938 .debug_line section) begins with a "statement program header",
939 which contains the following information. */
940 struct line_header
941 {
942 line_header ()
943 : offset_in_dwz {}
944 {}
945
946 /* Add an entry to the include directory table. */
947 void add_include_dir (const char *include_dir);
948
949 /* Add an entry to the file name table. */
950 void add_file_name (const char *name, dir_index d_index,
951 unsigned int mod_time, unsigned int length);
952
953 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
954 is out of bounds. */
955 const char *include_dir_at (dir_index index) const
956 {
957 /* Convert directory index number (1-based) to vector index
958 (0-based). */
959 size_t vec_index = to_underlying (index) - 1;
960
961 if (vec_index >= include_dirs.size ())
962 return NULL;
963 return include_dirs[vec_index];
964 }
965
966 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
967 is out of bounds. */
968 file_entry *file_name_at (file_name_index index)
969 {
970 /* Convert file name index number (1-based) to vector index
971 (0-based). */
972 size_t vec_index = to_underlying (index) - 1;
973
974 if (vec_index >= file_names.size ())
975 return NULL;
976 return &file_names[vec_index];
977 }
978
979 /* Const version of the above. */
980 const file_entry *file_name_at (unsigned int index) const
981 {
982 if (index >= file_names.size ())
983 return NULL;
984 return &file_names[index];
985 }
986
987 /* Offset of line number information in .debug_line section. */
988 sect_offset sect_off {};
989
990 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
991 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
992
993 unsigned int total_length {};
994 unsigned short version {};
995 unsigned int header_length {};
996 unsigned char minimum_instruction_length {};
997 unsigned char maximum_ops_per_instruction {};
998 unsigned char default_is_stmt {};
999 int line_base {};
1000 unsigned char line_range {};
1001 unsigned char opcode_base {};
1002
1003 /* standard_opcode_lengths[i] is the number of operands for the
1004 standard opcode whose value is i. This means that
1005 standard_opcode_lengths[0] is unused, and the last meaningful
1006 element is standard_opcode_lengths[opcode_base - 1]. */
1007 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
1008
1009 /* The include_directories table. Note these are observing
1010 pointers. The memory is owned by debug_line_buffer. */
1011 std::vector<const char *> include_dirs;
1012
1013 /* The file_names table. */
1014 std::vector<file_entry> file_names;
1015
1016 /* The start and end of the statement program following this
1017 header. These point into dwarf2_per_objfile->line_buffer. */
1018 const gdb_byte *statement_program_start {}, *statement_program_end {};
1019 };
1020
1021 typedef std::unique_ptr<line_header> line_header_up;
1022
1023 const char *
1024 file_entry::include_dir (const line_header *lh) const
1025 {
1026 return lh->include_dir_at (d_index);
1027 }
1028
1029 /* When we construct a partial symbol table entry we only
1030 need this much information. */
1031 struct partial_die_info : public allocate_on_obstack
1032 {
1033 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1034
1035 /* Disable assign but still keep copy ctor, which is needed
1036 load_partial_dies. */
1037 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1038
1039 /* Adjust the partial die before generating a symbol for it. This
1040 function may set the is_external flag or change the DIE's
1041 name. */
1042 void fixup (struct dwarf2_cu *cu);
1043
1044 /* Read a minimal amount of information into the minimal die
1045 structure. */
1046 const gdb_byte *read (const struct die_reader_specs *reader,
1047 const struct abbrev_info &abbrev,
1048 const gdb_byte *info_ptr);
1049
1050 /* Offset of this DIE. */
1051 const sect_offset sect_off;
1052
1053 /* DWARF-2 tag for this DIE. */
1054 const ENUM_BITFIELD(dwarf_tag) tag : 16;
1055
1056 /* Assorted flags describing the data found in this DIE. */
1057 const unsigned int has_children : 1;
1058
1059 unsigned int is_external : 1;
1060 unsigned int is_declaration : 1;
1061 unsigned int has_type : 1;
1062 unsigned int has_specification : 1;
1063 unsigned int has_pc_info : 1;
1064 unsigned int may_be_inlined : 1;
1065
1066 /* This DIE has been marked DW_AT_main_subprogram. */
1067 unsigned int main_subprogram : 1;
1068
1069 /* Flag set if the SCOPE field of this structure has been
1070 computed. */
1071 unsigned int scope_set : 1;
1072
1073 /* Flag set if the DIE has a byte_size attribute. */
1074 unsigned int has_byte_size : 1;
1075
1076 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1077 unsigned int has_const_value : 1;
1078
1079 /* Flag set if any of the DIE's children are template arguments. */
1080 unsigned int has_template_arguments : 1;
1081
1082 /* Flag set if fixup has been called on this die. */
1083 unsigned int fixup_called : 1;
1084
1085 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1086 unsigned int is_dwz : 1;
1087
1088 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1089 unsigned int spec_is_dwz : 1;
1090
1091 /* The name of this DIE. Normally the value of DW_AT_name, but
1092 sometimes a default name for unnamed DIEs. */
1093 const char *name = nullptr;
1094
1095 /* The linkage name, if present. */
1096 const char *linkage_name = nullptr;
1097
1098 /* The scope to prepend to our children. This is generally
1099 allocated on the comp_unit_obstack, so will disappear
1100 when this compilation unit leaves the cache. */
1101 const char *scope = nullptr;
1102
1103 /* Some data associated with the partial DIE. The tag determines
1104 which field is live. */
1105 union
1106 {
1107 /* The location description associated with this DIE, if any. */
1108 struct dwarf_block *locdesc;
1109 /* The offset of an import, for DW_TAG_imported_unit. */
1110 sect_offset sect_off;
1111 } d {};
1112
1113 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1114 CORE_ADDR lowpc = 0;
1115 CORE_ADDR highpc = 0;
1116
1117 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1118 DW_AT_sibling, if any. */
1119 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1120 could return DW_AT_sibling values to its caller load_partial_dies. */
1121 const gdb_byte *sibling = nullptr;
1122
1123 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1124 DW_AT_specification (or DW_AT_abstract_origin or
1125 DW_AT_extension). */
1126 sect_offset spec_offset {};
1127
1128 /* Pointers to this DIE's parent, first child, and next sibling,
1129 if any. */
1130 struct partial_die_info *die_parent = nullptr;
1131 struct partial_die_info *die_child = nullptr;
1132 struct partial_die_info *die_sibling = nullptr;
1133
1134 friend struct partial_die_info *
1135 dwarf2_cu::find_partial_die (sect_offset sect_off);
1136
1137 private:
1138 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1139 partial_die_info (sect_offset sect_off)
1140 : partial_die_info (sect_off, DW_TAG_padding, 0)
1141 {
1142 }
1143
1144 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1145 int has_children_)
1146 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1147 {
1148 is_external = 0;
1149 is_declaration = 0;
1150 has_type = 0;
1151 has_specification = 0;
1152 has_pc_info = 0;
1153 may_be_inlined = 0;
1154 main_subprogram = 0;
1155 scope_set = 0;
1156 has_byte_size = 0;
1157 has_const_value = 0;
1158 has_template_arguments = 0;
1159 fixup_called = 0;
1160 is_dwz = 0;
1161 spec_is_dwz = 0;
1162 }
1163 };
1164
1165 /* This data structure holds the information of an abbrev. */
1166 struct abbrev_info
1167 {
1168 unsigned int number; /* number identifying abbrev */
1169 enum dwarf_tag tag; /* dwarf tag */
1170 unsigned short has_children; /* boolean */
1171 unsigned short num_attrs; /* number of attributes */
1172 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1173 struct abbrev_info *next; /* next in chain */
1174 };
1175
1176 struct attr_abbrev
1177 {
1178 ENUM_BITFIELD(dwarf_attribute) name : 16;
1179 ENUM_BITFIELD(dwarf_form) form : 16;
1180
1181 /* It is valid only if FORM is DW_FORM_implicit_const. */
1182 LONGEST implicit_const;
1183 };
1184
1185 /* Size of abbrev_table.abbrev_hash_table. */
1186 #define ABBREV_HASH_SIZE 121
1187
1188 /* Top level data structure to contain an abbreviation table. */
1189
1190 struct abbrev_table
1191 {
1192 explicit abbrev_table (sect_offset off)
1193 : sect_off (off)
1194 {
1195 m_abbrevs =
1196 XOBNEWVEC (&abbrev_obstack, struct abbrev_info *, ABBREV_HASH_SIZE);
1197 memset (m_abbrevs, 0, ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
1198 }
1199
1200 DISABLE_COPY_AND_ASSIGN (abbrev_table);
1201
1202 /* Allocate space for a struct abbrev_info object in
1203 ABBREV_TABLE. */
1204 struct abbrev_info *alloc_abbrev ();
1205
1206 /* Add an abbreviation to the table. */
1207 void add_abbrev (unsigned int abbrev_number, struct abbrev_info *abbrev);
1208
1209 /* Look up an abbrev in the table.
1210 Returns NULL if the abbrev is not found. */
1211
1212 struct abbrev_info *lookup_abbrev (unsigned int abbrev_number);
1213
1214
1215 /* Where the abbrev table came from.
1216 This is used as a sanity check when the table is used. */
1217 const sect_offset sect_off;
1218
1219 /* Storage for the abbrev table. */
1220 auto_obstack abbrev_obstack;
1221
1222 private:
1223
1224 /* Hash table of abbrevs.
1225 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1226 It could be statically allocated, but the previous code didn't so we
1227 don't either. */
1228 struct abbrev_info **m_abbrevs;
1229 };
1230
1231 typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
1232
1233 /* Attributes have a name and a value. */
1234 struct attribute
1235 {
1236 ENUM_BITFIELD(dwarf_attribute) name : 16;
1237 ENUM_BITFIELD(dwarf_form) form : 15;
1238
1239 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1240 field should be in u.str (existing only for DW_STRING) but it is kept
1241 here for better struct attribute alignment. */
1242 unsigned int string_is_canonical : 1;
1243
1244 union
1245 {
1246 const char *str;
1247 struct dwarf_block *blk;
1248 ULONGEST unsnd;
1249 LONGEST snd;
1250 CORE_ADDR addr;
1251 ULONGEST signature;
1252 }
1253 u;
1254 };
1255
1256 /* This data structure holds a complete die structure. */
1257 struct die_info
1258 {
1259 /* DWARF-2 tag for this DIE. */
1260 ENUM_BITFIELD(dwarf_tag) tag : 16;
1261
1262 /* Number of attributes */
1263 unsigned char num_attrs;
1264
1265 /* True if we're presently building the full type name for the
1266 type derived from this DIE. */
1267 unsigned char building_fullname : 1;
1268
1269 /* True if this die is in process. PR 16581. */
1270 unsigned char in_process : 1;
1271
1272 /* Abbrev number */
1273 unsigned int abbrev;
1274
1275 /* Offset in .debug_info or .debug_types section. */
1276 sect_offset sect_off;
1277
1278 /* The dies in a compilation unit form an n-ary tree. PARENT
1279 points to this die's parent; CHILD points to the first child of
1280 this node; and all the children of a given node are chained
1281 together via their SIBLING fields. */
1282 struct die_info *child; /* Its first child, if any. */
1283 struct die_info *sibling; /* Its next sibling, if any. */
1284 struct die_info *parent; /* Its parent, if any. */
1285
1286 /* An array of attributes, with NUM_ATTRS elements. There may be
1287 zero, but it's not common and zero-sized arrays are not
1288 sufficiently portable C. */
1289 struct attribute attrs[1];
1290 };
1291
1292 /* Get at parts of an attribute structure. */
1293
1294 #define DW_STRING(attr) ((attr)->u.str)
1295 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1296 #define DW_UNSND(attr) ((attr)->u.unsnd)
1297 #define DW_BLOCK(attr) ((attr)->u.blk)
1298 #define DW_SND(attr) ((attr)->u.snd)
1299 #define DW_ADDR(attr) ((attr)->u.addr)
1300 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1301
1302 /* Blocks are a bunch of untyped bytes. */
1303 struct dwarf_block
1304 {
1305 size_t size;
1306
1307 /* Valid only if SIZE is not zero. */
1308 const gdb_byte *data;
1309 };
1310
1311 #ifndef ATTR_ALLOC_CHUNK
1312 #define ATTR_ALLOC_CHUNK 4
1313 #endif
1314
1315 /* Allocate fields for structs, unions and enums in this size. */
1316 #ifndef DW_FIELD_ALLOC_CHUNK
1317 #define DW_FIELD_ALLOC_CHUNK 4
1318 #endif
1319
1320 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1321 but this would require a corresponding change in unpack_field_as_long
1322 and friends. */
1323 static int bits_per_byte = 8;
1324
1325 /* When reading a variant or variant part, we track a bit more
1326 information about the field, and store it in an object of this
1327 type. */
1328
1329 struct variant_field
1330 {
1331 /* If we see a DW_TAG_variant, then this will be the discriminant
1332 value. */
1333 ULONGEST discriminant_value;
1334 /* If we see a DW_TAG_variant, then this will be set if this is the
1335 default branch. */
1336 bool default_branch;
1337 /* While reading a DW_TAG_variant_part, this will be set if this
1338 field is the discriminant. */
1339 bool is_discriminant;
1340 };
1341
1342 struct nextfield
1343 {
1344 int accessibility = 0;
1345 int virtuality = 0;
1346 /* Extra information to describe a variant or variant part. */
1347 struct variant_field variant {};
1348 struct field field {};
1349 };
1350
1351 struct fnfieldlist
1352 {
1353 const char *name = nullptr;
1354 std::vector<struct fn_field> fnfields;
1355 };
1356
1357 /* The routines that read and process dies for a C struct or C++ class
1358 pass lists of data member fields and lists of member function fields
1359 in an instance of a field_info structure, as defined below. */
1360 struct field_info
1361 {
1362 /* List of data member and baseclasses fields. */
1363 std::vector<struct nextfield> fields;
1364 std::vector<struct nextfield> baseclasses;
1365
1366 /* Number of fields (including baseclasses). */
1367 int nfields = 0;
1368
1369 /* Set if the accesibility of one of the fields is not public. */
1370 int non_public_fields = 0;
1371
1372 /* Member function fieldlist array, contains name of possibly overloaded
1373 member function, number of overloaded member functions and a pointer
1374 to the head of the member function field chain. */
1375 std::vector<struct fnfieldlist> fnfieldlists;
1376
1377 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1378 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1379 std::vector<struct decl_field> typedef_field_list;
1380
1381 /* Nested types defined by this class and the number of elements in this
1382 list. */
1383 std::vector<struct decl_field> nested_types_list;
1384 };
1385
1386 /* One item on the queue of compilation units to read in full symbols
1387 for. */
1388 struct dwarf2_queue_item
1389 {
1390 struct dwarf2_per_cu_data *per_cu;
1391 enum language pretend_language;
1392 struct dwarf2_queue_item *next;
1393 };
1394
1395 /* The current queue. */
1396 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1397
1398 /* Loaded secondary compilation units are kept in memory until they
1399 have not been referenced for the processing of this many
1400 compilation units. Set this to zero to disable caching. Cache
1401 sizes of up to at least twenty will improve startup time for
1402 typical inter-CU-reference binaries, at an obvious memory cost. */
1403 static int dwarf_max_cache_age = 5;
1404 static void
1405 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1406 struct cmd_list_element *c, const char *value)
1407 {
1408 fprintf_filtered (file, _("The upper bound on the age of cached "
1409 "DWARF compilation units is %s.\n"),
1410 value);
1411 }
1412 \f
1413 /* local function prototypes */
1414
1415 static const char *get_section_name (const struct dwarf2_section_info *);
1416
1417 static const char *get_section_file_name (const struct dwarf2_section_info *);
1418
1419 static void dwarf2_find_base_address (struct die_info *die,
1420 struct dwarf2_cu *cu);
1421
1422 static struct partial_symtab *create_partial_symtab
1423 (struct dwarf2_per_cu_data *per_cu, const char *name);
1424
1425 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1426 const gdb_byte *info_ptr,
1427 struct die_info *type_unit_die,
1428 int has_children, void *data);
1429
1430 static void dwarf2_build_psymtabs_hard
1431 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1432
1433 static void scan_partial_symbols (struct partial_die_info *,
1434 CORE_ADDR *, CORE_ADDR *,
1435 int, struct dwarf2_cu *);
1436
1437 static void add_partial_symbol (struct partial_die_info *,
1438 struct dwarf2_cu *);
1439
1440 static void add_partial_namespace (struct partial_die_info *pdi,
1441 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1442 int set_addrmap, struct dwarf2_cu *cu);
1443
1444 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1445 CORE_ADDR *highpc, int set_addrmap,
1446 struct dwarf2_cu *cu);
1447
1448 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1449 struct dwarf2_cu *cu);
1450
1451 static void add_partial_subprogram (struct partial_die_info *pdi,
1452 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1453 int need_pc, struct dwarf2_cu *cu);
1454
1455 static void dwarf2_read_symtab (struct partial_symtab *,
1456 struct objfile *);
1457
1458 static void psymtab_to_symtab_1 (struct partial_symtab *);
1459
1460 static abbrev_table_up abbrev_table_read_table
1461 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1462 sect_offset);
1463
1464 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1465
1466 static struct partial_die_info *load_partial_dies
1467 (const struct die_reader_specs *, const gdb_byte *, int);
1468
1469 static struct partial_die_info *find_partial_die (sect_offset, int,
1470 struct dwarf2_cu *);
1471
1472 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1473 struct attribute *, struct attr_abbrev *,
1474 const gdb_byte *);
1475
1476 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1477
1478 static int read_1_signed_byte (bfd *, const gdb_byte *);
1479
1480 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1481
1482 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1483
1484 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1485
1486 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1487 unsigned int *);
1488
1489 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1490
1491 static LONGEST read_checked_initial_length_and_offset
1492 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1493 unsigned int *, unsigned int *);
1494
1495 static LONGEST read_offset (bfd *, const gdb_byte *,
1496 const struct comp_unit_head *,
1497 unsigned int *);
1498
1499 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1500
1501 static sect_offset read_abbrev_offset
1502 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1503 struct dwarf2_section_info *, sect_offset);
1504
1505 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1506
1507 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1508
1509 static const char *read_indirect_string
1510 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1511 const struct comp_unit_head *, unsigned int *);
1512
1513 static const char *read_indirect_line_string
1514 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1515 const struct comp_unit_head *, unsigned int *);
1516
1517 static const char *read_indirect_string_at_offset
1518 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1519 LONGEST str_offset);
1520
1521 static const char *read_indirect_string_from_dwz
1522 (struct objfile *objfile, struct dwz_file *, LONGEST);
1523
1524 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1525
1526 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1527 const gdb_byte *,
1528 unsigned int *);
1529
1530 static const char *read_str_index (const struct die_reader_specs *reader,
1531 ULONGEST str_index);
1532
1533 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1534
1535 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1536 struct dwarf2_cu *);
1537
1538 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1539 unsigned int);
1540
1541 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1542 struct dwarf2_cu *cu);
1543
1544 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1545 struct dwarf2_cu *cu);
1546
1547 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1548
1549 static struct die_info *die_specification (struct die_info *die,
1550 struct dwarf2_cu **);
1551
1552 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1553 struct dwarf2_cu *cu);
1554
1555 static void dwarf_decode_lines (struct line_header *, const char *,
1556 struct dwarf2_cu *, struct partial_symtab *,
1557 CORE_ADDR, int decode_mapping);
1558
1559 static void dwarf2_start_subfile (const char *, const char *);
1560
1561 static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1562 const char *, const char *,
1563 CORE_ADDR);
1564
1565 static struct symbol *new_symbol (struct die_info *, struct type *,
1566 struct dwarf2_cu *, struct symbol * = NULL);
1567
1568 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1569 struct dwarf2_cu *);
1570
1571 static void dwarf2_const_value_attr (const struct attribute *attr,
1572 struct type *type,
1573 const char *name,
1574 struct obstack *obstack,
1575 struct dwarf2_cu *cu, LONGEST *value,
1576 const gdb_byte **bytes,
1577 struct dwarf2_locexpr_baton **baton);
1578
1579 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1580
1581 static int need_gnat_info (struct dwarf2_cu *);
1582
1583 static struct type *die_descriptive_type (struct die_info *,
1584 struct dwarf2_cu *);
1585
1586 static void set_descriptive_type (struct type *, struct die_info *,
1587 struct dwarf2_cu *);
1588
1589 static struct type *die_containing_type (struct die_info *,
1590 struct dwarf2_cu *);
1591
1592 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1593 struct dwarf2_cu *);
1594
1595 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1596
1597 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1598
1599 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1600
1601 static char *typename_concat (struct obstack *obs, const char *prefix,
1602 const char *suffix, int physname,
1603 struct dwarf2_cu *cu);
1604
1605 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1606
1607 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1608
1609 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1610
1611 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1612
1613 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1614
1615 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1616
1617 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1618 struct dwarf2_cu *, struct partial_symtab *);
1619
1620 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1621 values. Keep the items ordered with increasing constraints compliance. */
1622 enum pc_bounds_kind
1623 {
1624 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1625 PC_BOUNDS_NOT_PRESENT,
1626
1627 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1628 were present but they do not form a valid range of PC addresses. */
1629 PC_BOUNDS_INVALID,
1630
1631 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1632 PC_BOUNDS_RANGES,
1633
1634 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1635 PC_BOUNDS_HIGH_LOW,
1636 };
1637
1638 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1639 CORE_ADDR *, CORE_ADDR *,
1640 struct dwarf2_cu *,
1641 struct partial_symtab *);
1642
1643 static void get_scope_pc_bounds (struct die_info *,
1644 CORE_ADDR *, CORE_ADDR *,
1645 struct dwarf2_cu *);
1646
1647 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1648 CORE_ADDR, struct dwarf2_cu *);
1649
1650 static void dwarf2_add_field (struct field_info *, struct die_info *,
1651 struct dwarf2_cu *);
1652
1653 static void dwarf2_attach_fields_to_type (struct field_info *,
1654 struct type *, struct dwarf2_cu *);
1655
1656 static void dwarf2_add_member_fn (struct field_info *,
1657 struct die_info *, struct type *,
1658 struct dwarf2_cu *);
1659
1660 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1661 struct type *,
1662 struct dwarf2_cu *);
1663
1664 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1665
1666 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1667
1668 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1669
1670 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1671
1672 static struct using_direct **using_directives (enum language);
1673
1674 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1675
1676 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1677
1678 static struct type *read_module_type (struct die_info *die,
1679 struct dwarf2_cu *cu);
1680
1681 static const char *namespace_name (struct die_info *die,
1682 int *is_anonymous, struct dwarf2_cu *);
1683
1684 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1685
1686 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1687
1688 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1689 struct dwarf2_cu *);
1690
1691 static struct die_info *read_die_and_siblings_1
1692 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1693 struct die_info *);
1694
1695 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1696 const gdb_byte *info_ptr,
1697 const gdb_byte **new_info_ptr,
1698 struct die_info *parent);
1699
1700 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1701 struct die_info **, const gdb_byte *,
1702 int *, int);
1703
1704 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1705 struct die_info **, const gdb_byte *,
1706 int *);
1707
1708 static void process_die (struct die_info *, struct dwarf2_cu *);
1709
1710 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1711 struct obstack *);
1712
1713 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1714
1715 static const char *dwarf2_full_name (const char *name,
1716 struct die_info *die,
1717 struct dwarf2_cu *cu);
1718
1719 static const char *dwarf2_physname (const char *name, struct die_info *die,
1720 struct dwarf2_cu *cu);
1721
1722 static struct die_info *dwarf2_extension (struct die_info *die,
1723 struct dwarf2_cu **);
1724
1725 static const char *dwarf_tag_name (unsigned int);
1726
1727 static const char *dwarf_attr_name (unsigned int);
1728
1729 static const char *dwarf_form_name (unsigned int);
1730
1731 static const char *dwarf_bool_name (unsigned int);
1732
1733 static const char *dwarf_type_encoding_name (unsigned int);
1734
1735 static struct die_info *sibling_die (struct die_info *);
1736
1737 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1738
1739 static void dump_die_for_error (struct die_info *);
1740
1741 static void dump_die_1 (struct ui_file *, int level, int max_level,
1742 struct die_info *);
1743
1744 /*static*/ void dump_die (struct die_info *, int max_level);
1745
1746 static void store_in_ref_table (struct die_info *,
1747 struct dwarf2_cu *);
1748
1749 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1750
1751 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1752
1753 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1754 const struct attribute *,
1755 struct dwarf2_cu **);
1756
1757 static struct die_info *follow_die_ref (struct die_info *,
1758 const struct attribute *,
1759 struct dwarf2_cu **);
1760
1761 static struct die_info *follow_die_sig (struct die_info *,
1762 const struct attribute *,
1763 struct dwarf2_cu **);
1764
1765 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1766 struct dwarf2_cu *);
1767
1768 static struct type *get_DW_AT_signature_type (struct die_info *,
1769 const struct attribute *,
1770 struct dwarf2_cu *);
1771
1772 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1773
1774 static void read_signatured_type (struct signatured_type *);
1775
1776 static int attr_to_dynamic_prop (const struct attribute *attr,
1777 struct die_info *die, struct dwarf2_cu *cu,
1778 struct dynamic_prop *prop);
1779
1780 /* memory allocation interface */
1781
1782 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1783
1784 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1785
1786 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1787
1788 static int attr_form_is_block (const struct attribute *);
1789
1790 static int attr_form_is_section_offset (const struct attribute *);
1791
1792 static int attr_form_is_constant (const struct attribute *);
1793
1794 static int attr_form_is_ref (const struct attribute *);
1795
1796 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1797 struct dwarf2_loclist_baton *baton,
1798 const struct attribute *attr);
1799
1800 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1801 struct symbol *sym,
1802 struct dwarf2_cu *cu,
1803 int is_block);
1804
1805 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1806 const gdb_byte *info_ptr,
1807 struct abbrev_info *abbrev);
1808
1809 static hashval_t partial_die_hash (const void *item);
1810
1811 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1812
1813 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1814 (sect_offset sect_off, unsigned int offset_in_dwz,
1815 struct dwarf2_per_objfile *dwarf2_per_objfile);
1816
1817 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1818 struct die_info *comp_unit_die,
1819 enum language pretend_language);
1820
1821 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1822
1823 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1824
1825 static struct type *set_die_type (struct die_info *, struct type *,
1826 struct dwarf2_cu *);
1827
1828 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1829
1830 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1831
1832 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1833 enum language);
1834
1835 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1836 enum language);
1837
1838 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1839 enum language);
1840
1841 static void dwarf2_add_dependence (struct dwarf2_cu *,
1842 struct dwarf2_per_cu_data *);
1843
1844 static void dwarf2_mark (struct dwarf2_cu *);
1845
1846 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1847
1848 static struct type *get_die_type_at_offset (sect_offset,
1849 struct dwarf2_per_cu_data *);
1850
1851 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1852
1853 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1854 enum language pretend_language);
1855
1856 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1857
1858 /* Class, the destructor of which frees all allocated queue entries. This
1859 will only have work to do if an error was thrown while processing the
1860 dwarf. If no error was thrown then the queue entries should have all
1861 been processed, and freed, as we went along. */
1862
1863 class dwarf2_queue_guard
1864 {
1865 public:
1866 dwarf2_queue_guard () = default;
1867
1868 /* Free any entries remaining on the queue. There should only be
1869 entries left if we hit an error while processing the dwarf. */
1870 ~dwarf2_queue_guard ()
1871 {
1872 struct dwarf2_queue_item *item, *last;
1873
1874 item = dwarf2_queue;
1875 while (item)
1876 {
1877 /* Anything still marked queued is likely to be in an
1878 inconsistent state, so discard it. */
1879 if (item->per_cu->queued)
1880 {
1881 if (item->per_cu->cu != NULL)
1882 free_one_cached_comp_unit (item->per_cu);
1883 item->per_cu->queued = 0;
1884 }
1885
1886 last = item;
1887 item = item->next;
1888 xfree (last);
1889 }
1890
1891 dwarf2_queue = dwarf2_queue_tail = NULL;
1892 }
1893 };
1894
1895 /* The return type of find_file_and_directory. Note, the enclosed
1896 string pointers are only valid while this object is valid. */
1897
1898 struct file_and_directory
1899 {
1900 /* The filename. This is never NULL. */
1901 const char *name;
1902
1903 /* The compilation directory. NULL if not known. If we needed to
1904 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1905 points directly to the DW_AT_comp_dir string attribute owned by
1906 the obstack that owns the DIE. */
1907 const char *comp_dir;
1908
1909 /* If we needed to build a new string for comp_dir, this is what
1910 owns the storage. */
1911 std::string comp_dir_storage;
1912 };
1913
1914 static file_and_directory find_file_and_directory (struct die_info *die,
1915 struct dwarf2_cu *cu);
1916
1917 static char *file_full_name (int file, struct line_header *lh,
1918 const char *comp_dir);
1919
1920 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
1921 enum class rcuh_kind { COMPILE, TYPE };
1922
1923 static const gdb_byte *read_and_check_comp_unit_head
1924 (struct dwarf2_per_objfile* dwarf2_per_objfile,
1925 struct comp_unit_head *header,
1926 struct dwarf2_section_info *section,
1927 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
1928 rcuh_kind section_kind);
1929
1930 static void init_cutu_and_read_dies
1931 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1932 int use_existing_cu, int keep, bool skip_partial,
1933 die_reader_func_ftype *die_reader_func, void *data);
1934
1935 static void init_cutu_and_read_dies_simple
1936 (struct dwarf2_per_cu_data *this_cu,
1937 die_reader_func_ftype *die_reader_func, void *data);
1938
1939 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1940
1941 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1942
1943 static struct dwo_unit *lookup_dwo_unit_in_dwp
1944 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1945 struct dwp_file *dwp_file, const char *comp_dir,
1946 ULONGEST signature, int is_debug_types);
1947
1948 static struct dwp_file *get_dwp_file
1949 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1950
1951 static struct dwo_unit *lookup_dwo_comp_unit
1952 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1953
1954 static struct dwo_unit *lookup_dwo_type_unit
1955 (struct signatured_type *, const char *, const char *);
1956
1957 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1958
1959 static void free_dwo_file (struct dwo_file *);
1960
1961 /* A unique_ptr helper to free a dwo_file. */
1962
1963 struct dwo_file_deleter
1964 {
1965 void operator() (struct dwo_file *df) const
1966 {
1967 free_dwo_file (df);
1968 }
1969 };
1970
1971 /* A unique pointer to a dwo_file. */
1972
1973 typedef std::unique_ptr<struct dwo_file, dwo_file_deleter> dwo_file_up;
1974
1975 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
1976
1977 static void check_producer (struct dwarf2_cu *cu);
1978
1979 static void free_line_header_voidp (void *arg);
1980 \f
1981 /* Various complaints about symbol reading that don't abort the process. */
1982
1983 static void
1984 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1985 {
1986 complaint (&symfile_complaints,
1987 _("statement list doesn't fit in .debug_line section"));
1988 }
1989
1990 static void
1991 dwarf2_debug_line_missing_file_complaint (void)
1992 {
1993 complaint (&symfile_complaints,
1994 _(".debug_line section has line data without a file"));
1995 }
1996
1997 static void
1998 dwarf2_debug_line_missing_end_sequence_complaint (void)
1999 {
2000 complaint (&symfile_complaints,
2001 _(".debug_line section has line "
2002 "program sequence without an end"));
2003 }
2004
2005 static void
2006 dwarf2_complex_location_expr_complaint (void)
2007 {
2008 complaint (&symfile_complaints, _("location expression too complex"));
2009 }
2010
2011 static void
2012 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2013 int arg3)
2014 {
2015 complaint (&symfile_complaints,
2016 _("const value length mismatch for '%s', got %d, expected %d"),
2017 arg1, arg2, arg3);
2018 }
2019
2020 static void
2021 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2022 {
2023 complaint (&symfile_complaints,
2024 _("debug info runs off end of %s section"
2025 " [in module %s]"),
2026 get_section_name (section),
2027 get_section_file_name (section));
2028 }
2029
2030 static void
2031 dwarf2_macro_malformed_definition_complaint (const char *arg1)
2032 {
2033 complaint (&symfile_complaints,
2034 _("macro debug info contains a "
2035 "malformed macro definition:\n`%s'"),
2036 arg1);
2037 }
2038
2039 static void
2040 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2041 {
2042 complaint (&symfile_complaints,
2043 _("invalid attribute class or form for '%s' in '%s'"),
2044 arg1, arg2);
2045 }
2046
2047 /* Hash function for line_header_hash. */
2048
2049 static hashval_t
2050 line_header_hash (const struct line_header *ofs)
2051 {
2052 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
2053 }
2054
2055 /* Hash function for htab_create_alloc_ex for line_header_hash. */
2056
2057 static hashval_t
2058 line_header_hash_voidp (const void *item)
2059 {
2060 const struct line_header *ofs = (const struct line_header *) item;
2061
2062 return line_header_hash (ofs);
2063 }
2064
2065 /* Equality function for line_header_hash. */
2066
2067 static int
2068 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2069 {
2070 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2071 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
2072
2073 return (ofs_lhs->sect_off == ofs_rhs->sect_off
2074 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2075 }
2076
2077 \f
2078
2079 /* Read the given attribute value as an address, taking the attribute's
2080 form into account. */
2081
2082 static CORE_ADDR
2083 attr_value_as_address (struct attribute *attr)
2084 {
2085 CORE_ADDR addr;
2086
2087 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2088 {
2089 /* Aside from a few clearly defined exceptions, attributes that
2090 contain an address must always be in DW_FORM_addr form.
2091 Unfortunately, some compilers happen to be violating this
2092 requirement by encoding addresses using other forms, such
2093 as DW_FORM_data4 for example. For those broken compilers,
2094 we try to do our best, without any guarantee of success,
2095 to interpret the address correctly. It would also be nice
2096 to generate a complaint, but that would require us to maintain
2097 a list of legitimate cases where a non-address form is allowed,
2098 as well as update callers to pass in at least the CU's DWARF
2099 version. This is more overhead than what we're willing to
2100 expand for a pretty rare case. */
2101 addr = DW_UNSND (attr);
2102 }
2103 else
2104 addr = DW_ADDR (attr);
2105
2106 return addr;
2107 }
2108
2109 /* See declaration. */
2110
2111 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2112 const dwarf2_debug_sections *names)
2113 : objfile (objfile_)
2114 {
2115 if (names == NULL)
2116 names = &dwarf2_elf_names;
2117
2118 bfd *obfd = objfile->obfd;
2119
2120 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2121 locate_sections (obfd, sec, *names);
2122 }
2123
2124 static void free_dwo_files (htab_t dwo_files, struct objfile *objfile);
2125
2126 dwarf2_per_objfile::~dwarf2_per_objfile ()
2127 {
2128 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2129 free_cached_comp_units ();
2130
2131 if (quick_file_names_table)
2132 htab_delete (quick_file_names_table);
2133
2134 if (line_header_hash)
2135 htab_delete (line_header_hash);
2136
2137 for (dwarf2_per_cu_data *per_cu : all_comp_units)
2138 VEC_free (dwarf2_per_cu_ptr, per_cu->imported_symtabs);
2139
2140 for (signatured_type *sig_type : all_type_units)
2141 VEC_free (dwarf2_per_cu_ptr, sig_type->per_cu.imported_symtabs);
2142
2143 VEC_free (dwarf2_section_info_def, types);
2144
2145 if (dwo_files != NULL)
2146 free_dwo_files (dwo_files, objfile);
2147 if (dwp_file != NULL)
2148 gdb_bfd_unref (dwp_file->dbfd);
2149
2150 if (dwz_file != NULL && dwz_file->dwz_bfd)
2151 gdb_bfd_unref (dwz_file->dwz_bfd);
2152
2153 if (index_table != NULL)
2154 index_table->~mapped_index ();
2155
2156 /* Everything else should be on the objfile obstack. */
2157 }
2158
2159 /* See declaration. */
2160
2161 void
2162 dwarf2_per_objfile::free_cached_comp_units ()
2163 {
2164 dwarf2_per_cu_data *per_cu = read_in_chain;
2165 dwarf2_per_cu_data **last_chain = &read_in_chain;
2166 while (per_cu != NULL)
2167 {
2168 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2169
2170 delete per_cu->cu;
2171 *last_chain = next_cu;
2172 per_cu = next_cu;
2173 }
2174 }
2175
2176 /* A helper class that calls free_cached_comp_units on
2177 destruction. */
2178
2179 class free_cached_comp_units
2180 {
2181 public:
2182
2183 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2184 : m_per_objfile (per_objfile)
2185 {
2186 }
2187
2188 ~free_cached_comp_units ()
2189 {
2190 m_per_objfile->free_cached_comp_units ();
2191 }
2192
2193 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2194
2195 private:
2196
2197 dwarf2_per_objfile *m_per_objfile;
2198 };
2199
2200 /* Try to locate the sections we need for DWARF 2 debugging
2201 information and return true if we have enough to do something.
2202 NAMES points to the dwarf2 section names, or is NULL if the standard
2203 ELF names are used. */
2204
2205 int
2206 dwarf2_has_info (struct objfile *objfile,
2207 const struct dwarf2_debug_sections *names)
2208 {
2209 if (objfile->flags & OBJF_READNEVER)
2210 return 0;
2211
2212 struct dwarf2_per_objfile *dwarf2_per_objfile
2213 = get_dwarf2_per_objfile (objfile);
2214
2215 if (dwarf2_per_objfile == NULL)
2216 {
2217 /* Initialize per-objfile state. */
2218 dwarf2_per_objfile
2219 = new (&objfile->objfile_obstack) struct dwarf2_per_objfile (objfile,
2220 names);
2221 set_dwarf2_per_objfile (objfile, dwarf2_per_objfile);
2222 }
2223 return (!dwarf2_per_objfile->info.is_virtual
2224 && dwarf2_per_objfile->info.s.section != NULL
2225 && !dwarf2_per_objfile->abbrev.is_virtual
2226 && dwarf2_per_objfile->abbrev.s.section != NULL);
2227 }
2228
2229 /* Return the containing section of virtual section SECTION. */
2230
2231 static struct dwarf2_section_info *
2232 get_containing_section (const struct dwarf2_section_info *section)
2233 {
2234 gdb_assert (section->is_virtual);
2235 return section->s.containing_section;
2236 }
2237
2238 /* Return the bfd owner of SECTION. */
2239
2240 static struct bfd *
2241 get_section_bfd_owner (const struct dwarf2_section_info *section)
2242 {
2243 if (section->is_virtual)
2244 {
2245 section = get_containing_section (section);
2246 gdb_assert (!section->is_virtual);
2247 }
2248 return section->s.section->owner;
2249 }
2250
2251 /* Return the bfd section of SECTION.
2252 Returns NULL if the section is not present. */
2253
2254 static asection *
2255 get_section_bfd_section (const struct dwarf2_section_info *section)
2256 {
2257 if (section->is_virtual)
2258 {
2259 section = get_containing_section (section);
2260 gdb_assert (!section->is_virtual);
2261 }
2262 return section->s.section;
2263 }
2264
2265 /* Return the name of SECTION. */
2266
2267 static const char *
2268 get_section_name (const struct dwarf2_section_info *section)
2269 {
2270 asection *sectp = get_section_bfd_section (section);
2271
2272 gdb_assert (sectp != NULL);
2273 return bfd_section_name (get_section_bfd_owner (section), sectp);
2274 }
2275
2276 /* Return the name of the file SECTION is in. */
2277
2278 static const char *
2279 get_section_file_name (const struct dwarf2_section_info *section)
2280 {
2281 bfd *abfd = get_section_bfd_owner (section);
2282
2283 return bfd_get_filename (abfd);
2284 }
2285
2286 /* Return the id of SECTION.
2287 Returns 0 if SECTION doesn't exist. */
2288
2289 static int
2290 get_section_id (const struct dwarf2_section_info *section)
2291 {
2292 asection *sectp = get_section_bfd_section (section);
2293
2294 if (sectp == NULL)
2295 return 0;
2296 return sectp->id;
2297 }
2298
2299 /* Return the flags of SECTION.
2300 SECTION (or containing section if this is a virtual section) must exist. */
2301
2302 static int
2303 get_section_flags (const struct dwarf2_section_info *section)
2304 {
2305 asection *sectp = get_section_bfd_section (section);
2306
2307 gdb_assert (sectp != NULL);
2308 return bfd_get_section_flags (sectp->owner, sectp);
2309 }
2310
2311 /* When loading sections, we look either for uncompressed section or for
2312 compressed section names. */
2313
2314 static int
2315 section_is_p (const char *section_name,
2316 const struct dwarf2_section_names *names)
2317 {
2318 if (names->normal != NULL
2319 && strcmp (section_name, names->normal) == 0)
2320 return 1;
2321 if (names->compressed != NULL
2322 && strcmp (section_name, names->compressed) == 0)
2323 return 1;
2324 return 0;
2325 }
2326
2327 /* See declaration. */
2328
2329 void
2330 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2331 const dwarf2_debug_sections &names)
2332 {
2333 flagword aflag = bfd_get_section_flags (abfd, sectp);
2334
2335 if ((aflag & SEC_HAS_CONTENTS) == 0)
2336 {
2337 }
2338 else if (section_is_p (sectp->name, &names.info))
2339 {
2340 this->info.s.section = sectp;
2341 this->info.size = bfd_get_section_size (sectp);
2342 }
2343 else if (section_is_p (sectp->name, &names.abbrev))
2344 {
2345 this->abbrev.s.section = sectp;
2346 this->abbrev.size = bfd_get_section_size (sectp);
2347 }
2348 else if (section_is_p (sectp->name, &names.line))
2349 {
2350 this->line.s.section = sectp;
2351 this->line.size = bfd_get_section_size (sectp);
2352 }
2353 else if (section_is_p (sectp->name, &names.loc))
2354 {
2355 this->loc.s.section = sectp;
2356 this->loc.size = bfd_get_section_size (sectp);
2357 }
2358 else if (section_is_p (sectp->name, &names.loclists))
2359 {
2360 this->loclists.s.section = sectp;
2361 this->loclists.size = bfd_get_section_size (sectp);
2362 }
2363 else if (section_is_p (sectp->name, &names.macinfo))
2364 {
2365 this->macinfo.s.section = sectp;
2366 this->macinfo.size = bfd_get_section_size (sectp);
2367 }
2368 else if (section_is_p (sectp->name, &names.macro))
2369 {
2370 this->macro.s.section = sectp;
2371 this->macro.size = bfd_get_section_size (sectp);
2372 }
2373 else if (section_is_p (sectp->name, &names.str))
2374 {
2375 this->str.s.section = sectp;
2376 this->str.size = bfd_get_section_size (sectp);
2377 }
2378 else if (section_is_p (sectp->name, &names.line_str))
2379 {
2380 this->line_str.s.section = sectp;
2381 this->line_str.size = bfd_get_section_size (sectp);
2382 }
2383 else if (section_is_p (sectp->name, &names.addr))
2384 {
2385 this->addr.s.section = sectp;
2386 this->addr.size = bfd_get_section_size (sectp);
2387 }
2388 else if (section_is_p (sectp->name, &names.frame))
2389 {
2390 this->frame.s.section = sectp;
2391 this->frame.size = bfd_get_section_size (sectp);
2392 }
2393 else if (section_is_p (sectp->name, &names.eh_frame))
2394 {
2395 this->eh_frame.s.section = sectp;
2396 this->eh_frame.size = bfd_get_section_size (sectp);
2397 }
2398 else if (section_is_p (sectp->name, &names.ranges))
2399 {
2400 this->ranges.s.section = sectp;
2401 this->ranges.size = bfd_get_section_size (sectp);
2402 }
2403 else if (section_is_p (sectp->name, &names.rnglists))
2404 {
2405 this->rnglists.s.section = sectp;
2406 this->rnglists.size = bfd_get_section_size (sectp);
2407 }
2408 else if (section_is_p (sectp->name, &names.types))
2409 {
2410 struct dwarf2_section_info type_section;
2411
2412 memset (&type_section, 0, sizeof (type_section));
2413 type_section.s.section = sectp;
2414 type_section.size = bfd_get_section_size (sectp);
2415
2416 VEC_safe_push (dwarf2_section_info_def, this->types,
2417 &type_section);
2418 }
2419 else if (section_is_p (sectp->name, &names.gdb_index))
2420 {
2421 this->gdb_index.s.section = sectp;
2422 this->gdb_index.size = bfd_get_section_size (sectp);
2423 }
2424 else if (section_is_p (sectp->name, &names.debug_names))
2425 {
2426 this->debug_names.s.section = sectp;
2427 this->debug_names.size = bfd_get_section_size (sectp);
2428 }
2429 else if (section_is_p (sectp->name, &names.debug_aranges))
2430 {
2431 this->debug_aranges.s.section = sectp;
2432 this->debug_aranges.size = bfd_get_section_size (sectp);
2433 }
2434
2435 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
2436 && bfd_section_vma (abfd, sectp) == 0)
2437 this->has_section_at_zero = true;
2438 }
2439
2440 /* A helper function that decides whether a section is empty,
2441 or not present. */
2442
2443 static int
2444 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2445 {
2446 if (section->is_virtual)
2447 return section->size == 0;
2448 return section->s.section == NULL || section->size == 0;
2449 }
2450
2451 /* See dwarf2read.h. */
2452
2453 void
2454 dwarf2_read_section (struct objfile *objfile, dwarf2_section_info *info)
2455 {
2456 asection *sectp;
2457 bfd *abfd;
2458 gdb_byte *buf, *retbuf;
2459
2460 if (info->readin)
2461 return;
2462 info->buffer = NULL;
2463 info->readin = 1;
2464
2465 if (dwarf2_section_empty_p (info))
2466 return;
2467
2468 sectp = get_section_bfd_section (info);
2469
2470 /* If this is a virtual section we need to read in the real one first. */
2471 if (info->is_virtual)
2472 {
2473 struct dwarf2_section_info *containing_section =
2474 get_containing_section (info);
2475
2476 gdb_assert (sectp != NULL);
2477 if ((sectp->flags & SEC_RELOC) != 0)
2478 {
2479 error (_("Dwarf Error: DWP format V2 with relocations is not"
2480 " supported in section %s [in module %s]"),
2481 get_section_name (info), get_section_file_name (info));
2482 }
2483 dwarf2_read_section (objfile, containing_section);
2484 /* Other code should have already caught virtual sections that don't
2485 fit. */
2486 gdb_assert (info->virtual_offset + info->size
2487 <= containing_section->size);
2488 /* If the real section is empty or there was a problem reading the
2489 section we shouldn't get here. */
2490 gdb_assert (containing_section->buffer != NULL);
2491 info->buffer = containing_section->buffer + info->virtual_offset;
2492 return;
2493 }
2494
2495 /* If the section has relocations, we must read it ourselves.
2496 Otherwise we attach it to the BFD. */
2497 if ((sectp->flags & SEC_RELOC) == 0)
2498 {
2499 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2500 return;
2501 }
2502
2503 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
2504 info->buffer = buf;
2505
2506 /* When debugging .o files, we may need to apply relocations; see
2507 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2508 We never compress sections in .o files, so we only need to
2509 try this when the section is not compressed. */
2510 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2511 if (retbuf != NULL)
2512 {
2513 info->buffer = retbuf;
2514 return;
2515 }
2516
2517 abfd = get_section_bfd_owner (info);
2518 gdb_assert (abfd != NULL);
2519
2520 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2521 || bfd_bread (buf, info->size, abfd) != info->size)
2522 {
2523 error (_("Dwarf Error: Can't read DWARF data"
2524 " in section %s [in module %s]"),
2525 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2526 }
2527 }
2528
2529 /* A helper function that returns the size of a section in a safe way.
2530 If you are positive that the section has been read before using the
2531 size, then it is safe to refer to the dwarf2_section_info object's
2532 "size" field directly. In other cases, you must call this
2533 function, because for compressed sections the size field is not set
2534 correctly until the section has been read. */
2535
2536 static bfd_size_type
2537 dwarf2_section_size (struct objfile *objfile,
2538 struct dwarf2_section_info *info)
2539 {
2540 if (!info->readin)
2541 dwarf2_read_section (objfile, info);
2542 return info->size;
2543 }
2544
2545 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2546 SECTION_NAME. */
2547
2548 void
2549 dwarf2_get_section_info (struct objfile *objfile,
2550 enum dwarf2_section_enum sect,
2551 asection **sectp, const gdb_byte **bufp,
2552 bfd_size_type *sizep)
2553 {
2554 struct dwarf2_per_objfile *data
2555 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2556 dwarf2_objfile_data_key);
2557 struct dwarf2_section_info *info;
2558
2559 /* We may see an objfile without any DWARF, in which case we just
2560 return nothing. */
2561 if (data == NULL)
2562 {
2563 *sectp = NULL;
2564 *bufp = NULL;
2565 *sizep = 0;
2566 return;
2567 }
2568 switch (sect)
2569 {
2570 case DWARF2_DEBUG_FRAME:
2571 info = &data->frame;
2572 break;
2573 case DWARF2_EH_FRAME:
2574 info = &data->eh_frame;
2575 break;
2576 default:
2577 gdb_assert_not_reached ("unexpected section");
2578 }
2579
2580 dwarf2_read_section (objfile, info);
2581
2582 *sectp = get_section_bfd_section (info);
2583 *bufp = info->buffer;
2584 *sizep = info->size;
2585 }
2586
2587 /* A helper function to find the sections for a .dwz file. */
2588
2589 static void
2590 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2591 {
2592 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2593
2594 /* Note that we only support the standard ELF names, because .dwz
2595 is ELF-only (at the time of writing). */
2596 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2597 {
2598 dwz_file->abbrev.s.section = sectp;
2599 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2600 }
2601 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2602 {
2603 dwz_file->info.s.section = sectp;
2604 dwz_file->info.size = bfd_get_section_size (sectp);
2605 }
2606 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2607 {
2608 dwz_file->str.s.section = sectp;
2609 dwz_file->str.size = bfd_get_section_size (sectp);
2610 }
2611 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2612 {
2613 dwz_file->line.s.section = sectp;
2614 dwz_file->line.size = bfd_get_section_size (sectp);
2615 }
2616 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2617 {
2618 dwz_file->macro.s.section = sectp;
2619 dwz_file->macro.size = bfd_get_section_size (sectp);
2620 }
2621 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2622 {
2623 dwz_file->gdb_index.s.section = sectp;
2624 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2625 }
2626 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2627 {
2628 dwz_file->debug_names.s.section = sectp;
2629 dwz_file->debug_names.size = bfd_get_section_size (sectp);
2630 }
2631 }
2632
2633 /* Open the separate '.dwz' debug file, if needed. Return NULL if
2634 there is no .gnu_debugaltlink section in the file. Error if there
2635 is such a section but the file cannot be found. */
2636
2637 static struct dwz_file *
2638 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2639 {
2640 const char *filename;
2641 struct dwz_file *result;
2642 bfd_size_type buildid_len_arg;
2643 size_t buildid_len;
2644 bfd_byte *buildid;
2645
2646 if (dwarf2_per_objfile->dwz_file != NULL)
2647 return dwarf2_per_objfile->dwz_file;
2648
2649 bfd_set_error (bfd_error_no_error);
2650 gdb::unique_xmalloc_ptr<char> data
2651 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2652 &buildid_len_arg, &buildid));
2653 if (data == NULL)
2654 {
2655 if (bfd_get_error () == bfd_error_no_error)
2656 return NULL;
2657 error (_("could not read '.gnu_debugaltlink' section: %s"),
2658 bfd_errmsg (bfd_get_error ()));
2659 }
2660
2661 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2662
2663 buildid_len = (size_t) buildid_len_arg;
2664
2665 filename = data.get ();
2666
2667 std::string abs_storage;
2668 if (!IS_ABSOLUTE_PATH (filename))
2669 {
2670 gdb::unique_xmalloc_ptr<char> abs
2671 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2672
2673 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2674 filename = abs_storage.c_str ();
2675 }
2676
2677 /* First try the file name given in the section. If that doesn't
2678 work, try to use the build-id instead. */
2679 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2680 if (dwz_bfd != NULL)
2681 {
2682 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2683 dwz_bfd.release ();
2684 }
2685
2686 if (dwz_bfd == NULL)
2687 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2688
2689 if (dwz_bfd == NULL)
2690 error (_("could not find '.gnu_debugaltlink' file for %s"),
2691 objfile_name (dwarf2_per_objfile->objfile));
2692
2693 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2694 struct dwz_file);
2695 result->dwz_bfd = dwz_bfd.release ();
2696
2697 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
2698
2699 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
2700 dwarf2_per_objfile->dwz_file = result;
2701 return result;
2702 }
2703 \f
2704 /* DWARF quick_symbols_functions support. */
2705
2706 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2707 unique line tables, so we maintain a separate table of all .debug_line
2708 derived entries to support the sharing.
2709 All the quick functions need is the list of file names. We discard the
2710 line_header when we're done and don't need to record it here. */
2711 struct quick_file_names
2712 {
2713 /* The data used to construct the hash key. */
2714 struct stmt_list_hash hash;
2715
2716 /* The number of entries in file_names, real_names. */
2717 unsigned int num_file_names;
2718
2719 /* The file names from the line table, after being run through
2720 file_full_name. */
2721 const char **file_names;
2722
2723 /* The file names from the line table after being run through
2724 gdb_realpath. These are computed lazily. */
2725 const char **real_names;
2726 };
2727
2728 /* When using the index (and thus not using psymtabs), each CU has an
2729 object of this type. This is used to hold information needed by
2730 the various "quick" methods. */
2731 struct dwarf2_per_cu_quick_data
2732 {
2733 /* The file table. This can be NULL if there was no file table
2734 or it's currently not read in.
2735 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2736 struct quick_file_names *file_names;
2737
2738 /* The corresponding symbol table. This is NULL if symbols for this
2739 CU have not yet been read. */
2740 struct compunit_symtab *compunit_symtab;
2741
2742 /* A temporary mark bit used when iterating over all CUs in
2743 expand_symtabs_matching. */
2744 unsigned int mark : 1;
2745
2746 /* True if we've tried to read the file table and found there isn't one.
2747 There will be no point in trying to read it again next time. */
2748 unsigned int no_file_data : 1;
2749 };
2750
2751 /* Utility hash function for a stmt_list_hash. */
2752
2753 static hashval_t
2754 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2755 {
2756 hashval_t v = 0;
2757
2758 if (stmt_list_hash->dwo_unit != NULL)
2759 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2760 v += to_underlying (stmt_list_hash->line_sect_off);
2761 return v;
2762 }
2763
2764 /* Utility equality function for a stmt_list_hash. */
2765
2766 static int
2767 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2768 const struct stmt_list_hash *rhs)
2769 {
2770 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2771 return 0;
2772 if (lhs->dwo_unit != NULL
2773 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2774 return 0;
2775
2776 return lhs->line_sect_off == rhs->line_sect_off;
2777 }
2778
2779 /* Hash function for a quick_file_names. */
2780
2781 static hashval_t
2782 hash_file_name_entry (const void *e)
2783 {
2784 const struct quick_file_names *file_data
2785 = (const struct quick_file_names *) e;
2786
2787 return hash_stmt_list_entry (&file_data->hash);
2788 }
2789
2790 /* Equality function for a quick_file_names. */
2791
2792 static int
2793 eq_file_name_entry (const void *a, const void *b)
2794 {
2795 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2796 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2797
2798 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2799 }
2800
2801 /* Delete function for a quick_file_names. */
2802
2803 static void
2804 delete_file_name_entry (void *e)
2805 {
2806 struct quick_file_names *file_data = (struct quick_file_names *) e;
2807 int i;
2808
2809 for (i = 0; i < file_data->num_file_names; ++i)
2810 {
2811 xfree ((void*) file_data->file_names[i]);
2812 if (file_data->real_names)
2813 xfree ((void*) file_data->real_names[i]);
2814 }
2815
2816 /* The space for the struct itself lives on objfile_obstack,
2817 so we don't free it here. */
2818 }
2819
2820 /* Create a quick_file_names hash table. */
2821
2822 static htab_t
2823 create_quick_file_names_table (unsigned int nr_initial_entries)
2824 {
2825 return htab_create_alloc (nr_initial_entries,
2826 hash_file_name_entry, eq_file_name_entry,
2827 delete_file_name_entry, xcalloc, xfree);
2828 }
2829
2830 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2831 have to be created afterwards. You should call age_cached_comp_units after
2832 processing PER_CU->CU. dw2_setup must have been already called. */
2833
2834 static void
2835 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2836 {
2837 if (per_cu->is_debug_types)
2838 load_full_type_unit (per_cu);
2839 else
2840 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2841
2842 if (per_cu->cu == NULL)
2843 return; /* Dummy CU. */
2844
2845 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2846 }
2847
2848 /* Read in the symbols for PER_CU. */
2849
2850 static void
2851 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2852 {
2853 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2854
2855 /* Skip type_unit_groups, reading the type units they contain
2856 is handled elsewhere. */
2857 if (IS_TYPE_UNIT_GROUP (per_cu))
2858 return;
2859
2860 /* The destructor of dwarf2_queue_guard frees any entries left on
2861 the queue. After this point we're guaranteed to leave this function
2862 with the dwarf queue empty. */
2863 dwarf2_queue_guard q_guard;
2864
2865 if (dwarf2_per_objfile->using_index
2866 ? per_cu->v.quick->compunit_symtab == NULL
2867 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2868 {
2869 queue_comp_unit (per_cu, language_minimal);
2870 load_cu (per_cu, skip_partial);
2871
2872 /* If we just loaded a CU from a DWO, and we're working with an index
2873 that may badly handle TUs, load all the TUs in that DWO as well.
2874 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2875 if (!per_cu->is_debug_types
2876 && per_cu->cu != NULL
2877 && per_cu->cu->dwo_unit != NULL
2878 && dwarf2_per_objfile->index_table != NULL
2879 && dwarf2_per_objfile->index_table->version <= 7
2880 /* DWP files aren't supported yet. */
2881 && get_dwp_file (dwarf2_per_objfile) == NULL)
2882 queue_and_load_all_dwo_tus (per_cu);
2883 }
2884
2885 process_queue (dwarf2_per_objfile);
2886
2887 /* Age the cache, releasing compilation units that have not
2888 been used recently. */
2889 age_cached_comp_units (dwarf2_per_objfile);
2890 }
2891
2892 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2893 the objfile from which this CU came. Returns the resulting symbol
2894 table. */
2895
2896 static struct compunit_symtab *
2897 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2898 {
2899 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2900
2901 gdb_assert (dwarf2_per_objfile->using_index);
2902 if (!per_cu->v.quick->compunit_symtab)
2903 {
2904 free_cached_comp_units freer (dwarf2_per_objfile);
2905 scoped_restore decrementer = increment_reading_symtab ();
2906 dw2_do_instantiate_symtab (per_cu, skip_partial);
2907 process_cu_includes (dwarf2_per_objfile);
2908 }
2909
2910 return per_cu->v.quick->compunit_symtab;
2911 }
2912
2913 /* See declaration. */
2914
2915 dwarf2_per_cu_data *
2916 dwarf2_per_objfile::get_cutu (int index)
2917 {
2918 if (index >= this->all_comp_units.size ())
2919 {
2920 index -= this->all_comp_units.size ();
2921 gdb_assert (index < this->all_type_units.size ());
2922 return &this->all_type_units[index]->per_cu;
2923 }
2924
2925 return this->all_comp_units[index];
2926 }
2927
2928 /* See declaration. */
2929
2930 dwarf2_per_cu_data *
2931 dwarf2_per_objfile::get_cu (int index)
2932 {
2933 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2934
2935 return this->all_comp_units[index];
2936 }
2937
2938 /* See declaration. */
2939
2940 signatured_type *
2941 dwarf2_per_objfile::get_tu (int index)
2942 {
2943 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2944
2945 return this->all_type_units[index];
2946 }
2947
2948 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2949 objfile_obstack, and constructed with the specified field
2950 values. */
2951
2952 static dwarf2_per_cu_data *
2953 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2954 struct dwarf2_section_info *section,
2955 int is_dwz,
2956 sect_offset sect_off, ULONGEST length)
2957 {
2958 struct objfile *objfile = dwarf2_per_objfile->objfile;
2959 dwarf2_per_cu_data *the_cu
2960 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2961 struct dwarf2_per_cu_data);
2962 the_cu->sect_off = sect_off;
2963 the_cu->length = length;
2964 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2965 the_cu->section = section;
2966 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2967 struct dwarf2_per_cu_quick_data);
2968 the_cu->is_dwz = is_dwz;
2969 return the_cu;
2970 }
2971
2972 /* A helper for create_cus_from_index that handles a given list of
2973 CUs. */
2974
2975 static void
2976 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2977 const gdb_byte *cu_list, offset_type n_elements,
2978 struct dwarf2_section_info *section,
2979 int is_dwz)
2980 {
2981 for (offset_type i = 0; i < n_elements; i += 2)
2982 {
2983 gdb_static_assert (sizeof (ULONGEST) >= 8);
2984
2985 sect_offset sect_off
2986 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2987 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2988 cu_list += 2 * 8;
2989
2990 dwarf2_per_cu_data *per_cu
2991 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2992 sect_off, length);
2993 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
2994 }
2995 }
2996
2997 /* Read the CU list from the mapped index, and use it to create all
2998 the CU objects for this objfile. */
2999
3000 static void
3001 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3002 const gdb_byte *cu_list, offset_type cu_list_elements,
3003 const gdb_byte *dwz_list, offset_type dwz_elements)
3004 {
3005 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
3006 dwarf2_per_objfile->all_comp_units.reserve
3007 ((cu_list_elements + dwz_elements) / 2);
3008
3009 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
3010 &dwarf2_per_objfile->info, 0);
3011
3012 if (dwz_elements == 0)
3013 return;
3014
3015 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3016 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
3017 &dwz->info, 1);
3018 }
3019
3020 /* Create the signatured type hash table from the index. */
3021
3022 static void
3023 create_signatured_type_table_from_index
3024 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3025 struct dwarf2_section_info *section,
3026 const gdb_byte *bytes,
3027 offset_type elements)
3028 {
3029 struct objfile *objfile = dwarf2_per_objfile->objfile;
3030
3031 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3032 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
3033
3034 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3035
3036 for (offset_type i = 0; i < elements; i += 3)
3037 {
3038 struct signatured_type *sig_type;
3039 ULONGEST signature;
3040 void **slot;
3041 cu_offset type_offset_in_tu;
3042
3043 gdb_static_assert (sizeof (ULONGEST) >= 8);
3044 sect_offset sect_off
3045 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3046 type_offset_in_tu
3047 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3048 BFD_ENDIAN_LITTLE);
3049 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3050 bytes += 3 * 8;
3051
3052 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3053 struct signatured_type);
3054 sig_type->signature = signature;
3055 sig_type->type_offset_in_tu = type_offset_in_tu;
3056 sig_type->per_cu.is_debug_types = 1;
3057 sig_type->per_cu.section = section;
3058 sig_type->per_cu.sect_off = sect_off;
3059 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3060 sig_type->per_cu.v.quick
3061 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3062 struct dwarf2_per_cu_quick_data);
3063
3064 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3065 *slot = sig_type;
3066
3067 dwarf2_per_objfile->all_type_units.push_back (sig_type);
3068 }
3069
3070 dwarf2_per_objfile->signatured_types = sig_types_hash;
3071 }
3072
3073 /* Create the signatured type hash table from .debug_names. */
3074
3075 static void
3076 create_signatured_type_table_from_debug_names
3077 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3078 const mapped_debug_names &map,
3079 struct dwarf2_section_info *section,
3080 struct dwarf2_section_info *abbrev_section)
3081 {
3082 struct objfile *objfile = dwarf2_per_objfile->objfile;
3083
3084 dwarf2_read_section (objfile, section);
3085 dwarf2_read_section (objfile, abbrev_section);
3086
3087 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3088 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
3089
3090 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3091
3092 for (uint32_t i = 0; i < map.tu_count; ++i)
3093 {
3094 struct signatured_type *sig_type;
3095 void **slot;
3096
3097 sect_offset sect_off
3098 = (sect_offset) (extract_unsigned_integer
3099 (map.tu_table_reordered + i * map.offset_size,
3100 map.offset_size,
3101 map.dwarf5_byte_order));
3102
3103 comp_unit_head cu_header;
3104 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3105 abbrev_section,
3106 section->buffer + to_underlying (sect_off),
3107 rcuh_kind::TYPE);
3108
3109 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3110 struct signatured_type);
3111 sig_type->signature = cu_header.signature;
3112 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3113 sig_type->per_cu.is_debug_types = 1;
3114 sig_type->per_cu.section = section;
3115 sig_type->per_cu.sect_off = sect_off;
3116 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3117 sig_type->per_cu.v.quick
3118 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3119 struct dwarf2_per_cu_quick_data);
3120
3121 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3122 *slot = sig_type;
3123
3124 dwarf2_per_objfile->all_type_units.push_back (sig_type);
3125 }
3126
3127 dwarf2_per_objfile->signatured_types = sig_types_hash;
3128 }
3129
3130 /* Read the address map data from the mapped index, and use it to
3131 populate the objfile's psymtabs_addrmap. */
3132
3133 static void
3134 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3135 struct mapped_index *index)
3136 {
3137 struct objfile *objfile = dwarf2_per_objfile->objfile;
3138 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3139 const gdb_byte *iter, *end;
3140 struct addrmap *mutable_map;
3141 CORE_ADDR baseaddr;
3142
3143 auto_obstack temp_obstack;
3144
3145 mutable_map = addrmap_create_mutable (&temp_obstack);
3146
3147 iter = index->address_table.data ();
3148 end = iter + index->address_table.size ();
3149
3150 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3151
3152 while (iter < end)
3153 {
3154 ULONGEST hi, lo, cu_index;
3155 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3156 iter += 8;
3157 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3158 iter += 8;
3159 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3160 iter += 4;
3161
3162 if (lo > hi)
3163 {
3164 complaint (&symfile_complaints,
3165 _(".gdb_index address table has invalid range (%s - %s)"),
3166 hex_string (lo), hex_string (hi));
3167 continue;
3168 }
3169
3170 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
3171 {
3172 complaint (&symfile_complaints,
3173 _(".gdb_index address table has invalid CU number %u"),
3174 (unsigned) cu_index);
3175 continue;
3176 }
3177
3178 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3179 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
3180 addrmap_set_empty (mutable_map, lo, hi - 1,
3181 dwarf2_per_objfile->get_cu (cu_index));
3182 }
3183
3184 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3185 &objfile->objfile_obstack);
3186 }
3187
3188 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
3189 populate the objfile's psymtabs_addrmap. */
3190
3191 static void
3192 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
3193 struct dwarf2_section_info *section)
3194 {
3195 struct objfile *objfile = dwarf2_per_objfile->objfile;
3196 bfd *abfd = objfile->obfd;
3197 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3198 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3199 SECT_OFF_TEXT (objfile));
3200
3201 auto_obstack temp_obstack;
3202 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3203
3204 std::unordered_map<sect_offset,
3205 dwarf2_per_cu_data *,
3206 gdb::hash_enum<sect_offset>>
3207 debug_info_offset_to_per_cu;
3208 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3209 {
3210 const auto insertpair
3211 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3212 if (!insertpair.second)
3213 {
3214 warning (_("Section .debug_aranges in %s has duplicate "
3215 "debug_info_offset %s, ignoring .debug_aranges."),
3216 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
3217 return;
3218 }
3219 }
3220
3221 dwarf2_read_section (objfile, section);
3222
3223 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3224
3225 const gdb_byte *addr = section->buffer;
3226
3227 while (addr < section->buffer + section->size)
3228 {
3229 const gdb_byte *const entry_addr = addr;
3230 unsigned int bytes_read;
3231
3232 const LONGEST entry_length = read_initial_length (abfd, addr,
3233 &bytes_read);
3234 addr += bytes_read;
3235
3236 const gdb_byte *const entry_end = addr + entry_length;
3237 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3238 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3239 if (addr + entry_length > section->buffer + section->size)
3240 {
3241 warning (_("Section .debug_aranges in %s entry at offset %zu "
3242 "length %s exceeds section length %s, "
3243 "ignoring .debug_aranges."),
3244 objfile_name (objfile), entry_addr - section->buffer,
3245 plongest (bytes_read + entry_length),
3246 pulongest (section->size));
3247 return;
3248 }
3249
3250 /* The version number. */
3251 const uint16_t version = read_2_bytes (abfd, addr);
3252 addr += 2;
3253 if (version != 2)
3254 {
3255 warning (_("Section .debug_aranges in %s entry at offset %zu "
3256 "has unsupported version %d, ignoring .debug_aranges."),
3257 objfile_name (objfile), entry_addr - section->buffer,
3258 version);
3259 return;
3260 }
3261
3262 const uint64_t debug_info_offset
3263 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3264 addr += offset_size;
3265 const auto per_cu_it
3266 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3267 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3268 {
3269 warning (_("Section .debug_aranges in %s entry at offset %zu "
3270 "debug_info_offset %s does not exists, "
3271 "ignoring .debug_aranges."),
3272 objfile_name (objfile), entry_addr - section->buffer,
3273 pulongest (debug_info_offset));
3274 return;
3275 }
3276 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3277
3278 const uint8_t address_size = *addr++;
3279 if (address_size < 1 || address_size > 8)
3280 {
3281 warning (_("Section .debug_aranges in %s entry at offset %zu "
3282 "address_size %u is invalid, ignoring .debug_aranges."),
3283 objfile_name (objfile), entry_addr - section->buffer,
3284 address_size);
3285 return;
3286 }
3287
3288 const uint8_t segment_selector_size = *addr++;
3289 if (segment_selector_size != 0)
3290 {
3291 warning (_("Section .debug_aranges in %s entry at offset %zu "
3292 "segment_selector_size %u is not supported, "
3293 "ignoring .debug_aranges."),
3294 objfile_name (objfile), entry_addr - section->buffer,
3295 segment_selector_size);
3296 return;
3297 }
3298
3299 /* Must pad to an alignment boundary that is twice the address
3300 size. It is undocumented by the DWARF standard but GCC does
3301 use it. */
3302 for (size_t padding = ((-(addr - section->buffer))
3303 & (2 * address_size - 1));
3304 padding > 0; padding--)
3305 if (*addr++ != 0)
3306 {
3307 warning (_("Section .debug_aranges in %s entry at offset %zu "
3308 "padding is not zero, ignoring .debug_aranges."),
3309 objfile_name (objfile), entry_addr - section->buffer);
3310 return;
3311 }
3312
3313 for (;;)
3314 {
3315 if (addr + 2 * address_size > entry_end)
3316 {
3317 warning (_("Section .debug_aranges in %s entry at offset %zu "
3318 "address list is not properly terminated, "
3319 "ignoring .debug_aranges."),
3320 objfile_name (objfile), entry_addr - section->buffer);
3321 return;
3322 }
3323 ULONGEST start = extract_unsigned_integer (addr, address_size,
3324 dwarf5_byte_order);
3325 addr += address_size;
3326 ULONGEST length = extract_unsigned_integer (addr, address_size,
3327 dwarf5_byte_order);
3328 addr += address_size;
3329 if (start == 0 && length == 0)
3330 break;
3331 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3332 {
3333 /* Symbol was eliminated due to a COMDAT group. */
3334 continue;
3335 }
3336 ULONGEST end = start + length;
3337 start = gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr);
3338 end = gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr);
3339 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3340 }
3341 }
3342
3343 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3344 &objfile->objfile_obstack);
3345 }
3346
3347 /* Find a slot in the mapped index INDEX for the object named NAME.
3348 If NAME is found, set *VEC_OUT to point to the CU vector in the
3349 constant pool and return true. If NAME cannot be found, return
3350 false. */
3351
3352 static bool
3353 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3354 offset_type **vec_out)
3355 {
3356 offset_type hash;
3357 offset_type slot, step;
3358 int (*cmp) (const char *, const char *);
3359
3360 gdb::unique_xmalloc_ptr<char> without_params;
3361 if (current_language->la_language == language_cplus
3362 || current_language->la_language == language_fortran
3363 || current_language->la_language == language_d)
3364 {
3365 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3366 not contain any. */
3367
3368 if (strchr (name, '(') != NULL)
3369 {
3370 without_params = cp_remove_params (name);
3371
3372 if (without_params != NULL)
3373 name = without_params.get ();
3374 }
3375 }
3376
3377 /* Index version 4 did not support case insensitive searches. But the
3378 indices for case insensitive languages are built in lowercase, therefore
3379 simulate our NAME being searched is also lowercased. */
3380 hash = mapped_index_string_hash ((index->version == 4
3381 && case_sensitivity == case_sensitive_off
3382 ? 5 : index->version),
3383 name);
3384
3385 slot = hash & (index->symbol_table.size () - 1);
3386 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
3387 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3388
3389 for (;;)
3390 {
3391 const char *str;
3392
3393 const auto &bucket = index->symbol_table[slot];
3394 if (bucket.name == 0 && bucket.vec == 0)
3395 return false;
3396
3397 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3398 if (!cmp (name, str))
3399 {
3400 *vec_out = (offset_type *) (index->constant_pool
3401 + MAYBE_SWAP (bucket.vec));
3402 return true;
3403 }
3404
3405 slot = (slot + step) & (index->symbol_table.size () - 1);
3406 }
3407 }
3408
3409 /* A helper function that reads the .gdb_index from SECTION and fills
3410 in MAP. FILENAME is the name of the file containing the section;
3411 it is used for error reporting. DEPRECATED_OK is true if it is
3412 ok to use deprecated sections.
3413
3414 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3415 out parameters that are filled in with information about the CU and
3416 TU lists in the section.
3417
3418 Returns 1 if all went well, 0 otherwise. */
3419
3420 static bool
3421 read_index_from_section (struct objfile *objfile,
3422 const char *filename,
3423 bool deprecated_ok,
3424 struct dwarf2_section_info *section,
3425 struct mapped_index *map,
3426 const gdb_byte **cu_list,
3427 offset_type *cu_list_elements,
3428 const gdb_byte **types_list,
3429 offset_type *types_list_elements)
3430 {
3431 const gdb_byte *addr;
3432 offset_type version;
3433 offset_type *metadata;
3434 int i;
3435
3436 if (dwarf2_section_empty_p (section))
3437 return 0;
3438
3439 /* Older elfutils strip versions could keep the section in the main
3440 executable while splitting it for the separate debug info file. */
3441 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
3442 return 0;
3443
3444 dwarf2_read_section (objfile, section);
3445
3446 addr = section->buffer;
3447 /* Version check. */
3448 version = MAYBE_SWAP (*(offset_type *) addr);
3449 /* Versions earlier than 3 emitted every copy of a psymbol. This
3450 causes the index to behave very poorly for certain requests. Version 3
3451 contained incomplete addrmap. So, it seems better to just ignore such
3452 indices. */
3453 if (version < 4)
3454 {
3455 static int warning_printed = 0;
3456 if (!warning_printed)
3457 {
3458 warning (_("Skipping obsolete .gdb_index section in %s."),
3459 filename);
3460 warning_printed = 1;
3461 }
3462 return 0;
3463 }
3464 /* Index version 4 uses a different hash function than index version
3465 5 and later.
3466
3467 Versions earlier than 6 did not emit psymbols for inlined
3468 functions. Using these files will cause GDB not to be able to
3469 set breakpoints on inlined functions by name, so we ignore these
3470 indices unless the user has done
3471 "set use-deprecated-index-sections on". */
3472 if (version < 6 && !deprecated_ok)
3473 {
3474 static int warning_printed = 0;
3475 if (!warning_printed)
3476 {
3477 warning (_("\
3478 Skipping deprecated .gdb_index section in %s.\n\
3479 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3480 to use the section anyway."),
3481 filename);
3482 warning_printed = 1;
3483 }
3484 return 0;
3485 }
3486 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3487 of the TU (for symbols coming from TUs),
3488 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3489 Plus gold-generated indices can have duplicate entries for global symbols,
3490 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3491 These are just performance bugs, and we can't distinguish gdb-generated
3492 indices from gold-generated ones, so issue no warning here. */
3493
3494 /* Indexes with higher version than the one supported by GDB may be no
3495 longer backward compatible. */
3496 if (version > 8)
3497 return 0;
3498
3499 map->version = version;
3500
3501 metadata = (offset_type *) (addr + sizeof (offset_type));
3502
3503 i = 0;
3504 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3505 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3506 / 8);
3507 ++i;
3508
3509 *types_list = addr + MAYBE_SWAP (metadata[i]);
3510 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3511 - MAYBE_SWAP (metadata[i]))
3512 / 8);
3513 ++i;
3514
3515 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3516 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3517 map->address_table
3518 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3519 ++i;
3520
3521 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3522 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3523 map->symbol_table
3524 = gdb::array_view<mapped_index::symbol_table_slot>
3525 ((mapped_index::symbol_table_slot *) symbol_table,
3526 (mapped_index::symbol_table_slot *) symbol_table_end);
3527
3528 ++i;
3529 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3530
3531 return 1;
3532 }
3533
3534 /* Read .gdb_index. If everything went ok, initialize the "quick"
3535 elements of all the CUs and return 1. Otherwise, return 0. */
3536
3537 static int
3538 dwarf2_read_index (struct dwarf2_per_objfile *dwarf2_per_objfile)
3539 {
3540 struct mapped_index local_map, *map;
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 if (!read_index_from_section (objfile, objfile_name (objfile),
3547 use_deprecated_index_sections,
3548 &dwarf2_per_objfile->gdb_index, &local_map,
3549 &cu_list, &cu_list_elements,
3550 &types_list, &types_list_elements))
3551 return 0;
3552
3553 /* Don't use the index if it's empty. */
3554 if (local_map.symbol_table.empty ())
3555 return 0;
3556
3557 /* If there is a .dwz file, read it so we can get its CU list as
3558 well. */
3559 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3560 if (dwz != NULL)
3561 {
3562 struct mapped_index dwz_map;
3563 const gdb_byte *dwz_types_ignore;
3564 offset_type dwz_types_elements_ignore;
3565
3566 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3567 1,
3568 &dwz->gdb_index, &dwz_map,
3569 &dwz_list, &dwz_list_elements,
3570 &dwz_types_ignore,
3571 &dwz_types_elements_ignore))
3572 {
3573 warning (_("could not read '.gdb_index' section from %s; skipping"),
3574 bfd_get_filename (dwz->dwz_bfd));
3575 return 0;
3576 }
3577 }
3578
3579 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3580 dwz_list, dwz_list_elements);
3581
3582 if (types_list_elements)
3583 {
3584 struct dwarf2_section_info *section;
3585
3586 /* We can only handle a single .debug_types when we have an
3587 index. */
3588 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3589 return 0;
3590
3591 section = VEC_index (dwarf2_section_info_def,
3592 dwarf2_per_objfile->types, 0);
3593
3594 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3595 types_list, types_list_elements);
3596 }
3597
3598 create_addrmap_from_index (dwarf2_per_objfile, &local_map);
3599
3600 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
3601 map = new (map) mapped_index ();
3602 *map = local_map;
3603
3604 dwarf2_per_objfile->index_table = map;
3605 dwarf2_per_objfile->using_index = 1;
3606 dwarf2_per_objfile->quick_file_names_table =
3607 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
3608
3609 return 1;
3610 }
3611
3612 /* die_reader_func for dw2_get_file_names. */
3613
3614 static void
3615 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3616 const gdb_byte *info_ptr,
3617 struct die_info *comp_unit_die,
3618 int has_children,
3619 void *data)
3620 {
3621 struct dwarf2_cu *cu = reader->cu;
3622 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3623 struct dwarf2_per_objfile *dwarf2_per_objfile
3624 = cu->per_cu->dwarf2_per_objfile;
3625 struct objfile *objfile = dwarf2_per_objfile->objfile;
3626 struct dwarf2_per_cu_data *lh_cu;
3627 struct attribute *attr;
3628 int i;
3629 void **slot;
3630 struct quick_file_names *qfn;
3631
3632 gdb_assert (! this_cu->is_debug_types);
3633
3634 /* Our callers never want to match partial units -- instead they
3635 will match the enclosing full CU. */
3636 if (comp_unit_die->tag == DW_TAG_partial_unit)
3637 {
3638 this_cu->v.quick->no_file_data = 1;
3639 return;
3640 }
3641
3642 lh_cu = this_cu;
3643 slot = NULL;
3644
3645 line_header_up lh;
3646 sect_offset line_offset {};
3647
3648 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3649 if (attr)
3650 {
3651 struct quick_file_names find_entry;
3652
3653 line_offset = (sect_offset) DW_UNSND (attr);
3654
3655 /* We may have already read in this line header (TU line header sharing).
3656 If we have we're done. */
3657 find_entry.hash.dwo_unit = cu->dwo_unit;
3658 find_entry.hash.line_sect_off = line_offset;
3659 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3660 &find_entry, INSERT);
3661 if (*slot != NULL)
3662 {
3663 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3664 return;
3665 }
3666
3667 lh = dwarf_decode_line_header (line_offset, cu);
3668 }
3669 if (lh == NULL)
3670 {
3671 lh_cu->v.quick->no_file_data = 1;
3672 return;
3673 }
3674
3675 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3676 qfn->hash.dwo_unit = cu->dwo_unit;
3677 qfn->hash.line_sect_off = line_offset;
3678 gdb_assert (slot != NULL);
3679 *slot = qfn;
3680
3681 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3682
3683 qfn->num_file_names = lh->file_names.size ();
3684 qfn->file_names =
3685 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3686 for (i = 0; i < lh->file_names.size (); ++i)
3687 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
3688 qfn->real_names = NULL;
3689
3690 lh_cu->v.quick->file_names = qfn;
3691 }
3692
3693 /* A helper for the "quick" functions which attempts to read the line
3694 table for THIS_CU. */
3695
3696 static struct quick_file_names *
3697 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3698 {
3699 /* This should never be called for TUs. */
3700 gdb_assert (! this_cu->is_debug_types);
3701 /* Nor type unit groups. */
3702 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3703
3704 if (this_cu->v.quick->file_names != NULL)
3705 return this_cu->v.quick->file_names;
3706 /* If we know there is no line data, no point in looking again. */
3707 if (this_cu->v.quick->no_file_data)
3708 return NULL;
3709
3710 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
3711
3712 if (this_cu->v.quick->no_file_data)
3713 return NULL;
3714 return this_cu->v.quick->file_names;
3715 }
3716
3717 /* A helper for the "quick" functions which computes and caches the
3718 real path for a given file name from the line table. */
3719
3720 static const char *
3721 dw2_get_real_path (struct objfile *objfile,
3722 struct quick_file_names *qfn, int index)
3723 {
3724 if (qfn->real_names == NULL)
3725 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3726 qfn->num_file_names, const char *);
3727
3728 if (qfn->real_names[index] == NULL)
3729 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3730
3731 return qfn->real_names[index];
3732 }
3733
3734 static struct symtab *
3735 dw2_find_last_source_symtab (struct objfile *objfile)
3736 {
3737 struct dwarf2_per_objfile *dwarf2_per_objfile
3738 = get_dwarf2_per_objfile (objfile);
3739 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
3740 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3741
3742 if (cust == NULL)
3743 return NULL;
3744
3745 return compunit_primary_filetab (cust);
3746 }
3747
3748 /* Traversal function for dw2_forget_cached_source_info. */
3749
3750 static int
3751 dw2_free_cached_file_names (void **slot, void *info)
3752 {
3753 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3754
3755 if (file_data->real_names)
3756 {
3757 int i;
3758
3759 for (i = 0; i < file_data->num_file_names; ++i)
3760 {
3761 xfree ((void*) file_data->real_names[i]);
3762 file_data->real_names[i] = NULL;
3763 }
3764 }
3765
3766 return 1;
3767 }
3768
3769 static void
3770 dw2_forget_cached_source_info (struct objfile *objfile)
3771 {
3772 struct dwarf2_per_objfile *dwarf2_per_objfile
3773 = get_dwarf2_per_objfile (objfile);
3774
3775 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3776 dw2_free_cached_file_names, NULL);
3777 }
3778
3779 /* Helper function for dw2_map_symtabs_matching_filename that expands
3780 the symtabs and calls the iterator. */
3781
3782 static int
3783 dw2_map_expand_apply (struct objfile *objfile,
3784 struct dwarf2_per_cu_data *per_cu,
3785 const char *name, const char *real_path,
3786 gdb::function_view<bool (symtab *)> callback)
3787 {
3788 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3789
3790 /* Don't visit already-expanded CUs. */
3791 if (per_cu->v.quick->compunit_symtab)
3792 return 0;
3793
3794 /* This may expand more than one symtab, and we want to iterate over
3795 all of them. */
3796 dw2_instantiate_symtab (per_cu, false);
3797
3798 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3799 last_made, callback);
3800 }
3801
3802 /* Implementation of the map_symtabs_matching_filename method. */
3803
3804 static bool
3805 dw2_map_symtabs_matching_filename
3806 (struct objfile *objfile, const char *name, const char *real_path,
3807 gdb::function_view<bool (symtab *)> callback)
3808 {
3809 const char *name_basename = lbasename (name);
3810 struct dwarf2_per_objfile *dwarf2_per_objfile
3811 = get_dwarf2_per_objfile (objfile);
3812
3813 /* The rule is CUs specify all the files, including those used by
3814 any TU, so there's no need to scan TUs here. */
3815
3816 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3817 {
3818 /* We only need to look at symtabs not already expanded. */
3819 if (per_cu->v.quick->compunit_symtab)
3820 continue;
3821
3822 quick_file_names *file_data = dw2_get_file_names (per_cu);
3823 if (file_data == NULL)
3824 continue;
3825
3826 for (int j = 0; j < file_data->num_file_names; ++j)
3827 {
3828 const char *this_name = file_data->file_names[j];
3829 const char *this_real_name;
3830
3831 if (compare_filenames_for_search (this_name, name))
3832 {
3833 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3834 callback))
3835 return true;
3836 continue;
3837 }
3838
3839 /* Before we invoke realpath, which can get expensive when many
3840 files are involved, do a quick comparison of the basenames. */
3841 if (! basenames_may_differ
3842 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3843 continue;
3844
3845 this_real_name = dw2_get_real_path (objfile, file_data, j);
3846 if (compare_filenames_for_search (this_real_name, name))
3847 {
3848 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3849 callback))
3850 return true;
3851 continue;
3852 }
3853
3854 if (real_path != NULL)
3855 {
3856 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3857 gdb_assert (IS_ABSOLUTE_PATH (name));
3858 if (this_real_name != NULL
3859 && FILENAME_CMP (real_path, this_real_name) == 0)
3860 {
3861 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3862 callback))
3863 return true;
3864 continue;
3865 }
3866 }
3867 }
3868 }
3869
3870 return false;
3871 }
3872
3873 /* Struct used to manage iterating over all CUs looking for a symbol. */
3874
3875 struct dw2_symtab_iterator
3876 {
3877 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3878 struct dwarf2_per_objfile *dwarf2_per_objfile;
3879 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3880 int want_specific_block;
3881 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3882 Unused if !WANT_SPECIFIC_BLOCK. */
3883 int block_index;
3884 /* The kind of symbol we're looking for. */
3885 domain_enum domain;
3886 /* The list of CUs from the index entry of the symbol,
3887 or NULL if not found. */
3888 offset_type *vec;
3889 /* The next element in VEC to look at. */
3890 int next;
3891 /* The number of elements in VEC, or zero if there is no match. */
3892 int length;
3893 /* Have we seen a global version of the symbol?
3894 If so we can ignore all further global instances.
3895 This is to work around gold/15646, inefficient gold-generated
3896 indices. */
3897 int global_seen;
3898 };
3899
3900 /* Initialize the index symtab iterator ITER.
3901 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3902 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
3903
3904 static void
3905 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3906 struct dwarf2_per_objfile *dwarf2_per_objfile,
3907 int want_specific_block,
3908 int block_index,
3909 domain_enum domain,
3910 const char *name)
3911 {
3912 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3913 iter->want_specific_block = want_specific_block;
3914 iter->block_index = block_index;
3915 iter->domain = domain;
3916 iter->next = 0;
3917 iter->global_seen = 0;
3918
3919 mapped_index *index = dwarf2_per_objfile->index_table;
3920
3921 /* index is NULL if OBJF_READNOW. */
3922 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3923 iter->length = MAYBE_SWAP (*iter->vec);
3924 else
3925 {
3926 iter->vec = NULL;
3927 iter->length = 0;
3928 }
3929 }
3930
3931 /* Return the next matching CU or NULL if there are no more. */
3932
3933 static struct dwarf2_per_cu_data *
3934 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3935 {
3936 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3937
3938 for ( ; iter->next < iter->length; ++iter->next)
3939 {
3940 offset_type cu_index_and_attrs =
3941 MAYBE_SWAP (iter->vec[iter->next + 1]);
3942 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3943 int want_static = iter->block_index != GLOBAL_BLOCK;
3944 /* This value is only valid for index versions >= 7. */
3945 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3946 gdb_index_symbol_kind symbol_kind =
3947 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3948 /* Only check the symbol attributes if they're present.
3949 Indices prior to version 7 don't record them,
3950 and indices >= 7 may elide them for certain symbols
3951 (gold does this). */
3952 int attrs_valid =
3953 (dwarf2_per_objfile->index_table->version >= 7
3954 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3955
3956 /* Don't crash on bad data. */
3957 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
3958 + dwarf2_per_objfile->all_type_units.size ()))
3959 {
3960 complaint (&symfile_complaints,
3961 _(".gdb_index entry has bad CU index"
3962 " [in module %s]"),
3963 objfile_name (dwarf2_per_objfile->objfile));
3964 continue;
3965 }
3966
3967 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3968
3969 /* Skip if already read in. */
3970 if (per_cu->v.quick->compunit_symtab)
3971 continue;
3972
3973 /* Check static vs global. */
3974 if (attrs_valid)
3975 {
3976 if (iter->want_specific_block
3977 && want_static != is_static)
3978 continue;
3979 /* Work around gold/15646. */
3980 if (!is_static && iter->global_seen)
3981 continue;
3982 if (!is_static)
3983 iter->global_seen = 1;
3984 }
3985
3986 /* Only check the symbol's kind if it has one. */
3987 if (attrs_valid)
3988 {
3989 switch (iter->domain)
3990 {
3991 case VAR_DOMAIN:
3992 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3993 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3994 /* Some types are also in VAR_DOMAIN. */
3995 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3996 continue;
3997 break;
3998 case STRUCT_DOMAIN:
3999 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4000 continue;
4001 break;
4002 case LABEL_DOMAIN:
4003 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4004 continue;
4005 break;
4006 default:
4007 break;
4008 }
4009 }
4010
4011 ++iter->next;
4012 return per_cu;
4013 }
4014
4015 return NULL;
4016 }
4017
4018 static struct compunit_symtab *
4019 dw2_lookup_symbol (struct objfile *objfile, int block_index,
4020 const char *name, domain_enum domain)
4021 {
4022 struct compunit_symtab *stab_best = NULL;
4023 struct dwarf2_per_objfile *dwarf2_per_objfile
4024 = get_dwarf2_per_objfile (objfile);
4025
4026 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4027
4028 struct dw2_symtab_iterator iter;
4029 struct dwarf2_per_cu_data *per_cu;
4030
4031 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 1, block_index, domain, name);
4032
4033 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4034 {
4035 struct symbol *sym, *with_opaque = NULL;
4036 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
4037 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4038 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
4039
4040 sym = block_find_symbol (block, name, domain,
4041 block_find_non_opaque_type_preferred,
4042 &with_opaque);
4043
4044 /* Some caution must be observed with overloaded functions
4045 and methods, since the index will not contain any overload
4046 information (but NAME might contain it). */
4047
4048 if (sym != NULL
4049 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4050 return stab;
4051 if (with_opaque != NULL
4052 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4053 stab_best = stab;
4054
4055 /* Keep looking through other CUs. */
4056 }
4057
4058 return stab_best;
4059 }
4060
4061 static void
4062 dw2_print_stats (struct objfile *objfile)
4063 {
4064 struct dwarf2_per_objfile *dwarf2_per_objfile
4065 = get_dwarf2_per_objfile (objfile);
4066 int total = (dwarf2_per_objfile->all_comp_units.size ()
4067 + dwarf2_per_objfile->all_type_units.size ());
4068 int count = 0;
4069
4070 for (int i = 0; i < total; ++i)
4071 {
4072 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
4073
4074 if (!per_cu->v.quick->compunit_symtab)
4075 ++count;
4076 }
4077 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
4078 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4079 }
4080
4081 /* This dumps minimal information about the index.
4082 It is called via "mt print objfiles".
4083 One use is to verify .gdb_index has been loaded by the
4084 gdb.dwarf2/gdb-index.exp testcase. */
4085
4086 static void
4087 dw2_dump (struct objfile *objfile)
4088 {
4089 struct dwarf2_per_objfile *dwarf2_per_objfile
4090 = get_dwarf2_per_objfile (objfile);
4091
4092 gdb_assert (dwarf2_per_objfile->using_index);
4093 printf_filtered (".gdb_index:");
4094 if (dwarf2_per_objfile->index_table != NULL)
4095 {
4096 printf_filtered (" version %d\n",
4097 dwarf2_per_objfile->index_table->version);
4098 }
4099 else
4100 printf_filtered (" faked for \"readnow\"\n");
4101 printf_filtered ("\n");
4102 }
4103
4104 static void
4105 dw2_relocate (struct objfile *objfile,
4106 const struct section_offsets *new_offsets,
4107 const struct section_offsets *delta)
4108 {
4109 /* There's nothing to relocate here. */
4110 }
4111
4112 static void
4113 dw2_expand_symtabs_for_function (struct objfile *objfile,
4114 const char *func_name)
4115 {
4116 struct dwarf2_per_objfile *dwarf2_per_objfile
4117 = get_dwarf2_per_objfile (objfile);
4118
4119 struct dw2_symtab_iterator iter;
4120 struct dwarf2_per_cu_data *per_cu;
4121
4122 /* Note: It doesn't matter what we pass for block_index here. */
4123 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 0, GLOBAL_BLOCK, VAR_DOMAIN,
4124 func_name);
4125
4126 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4127 dw2_instantiate_symtab (per_cu, false);
4128
4129 }
4130
4131 static void
4132 dw2_expand_all_symtabs (struct objfile *objfile)
4133 {
4134 struct dwarf2_per_objfile *dwarf2_per_objfile
4135 = get_dwarf2_per_objfile (objfile);
4136 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
4137 + dwarf2_per_objfile->all_type_units.size ());
4138
4139 for (int i = 0; i < total_units; ++i)
4140 {
4141 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
4142
4143 /* We don't want to directly expand a partial CU, because if we
4144 read it with the wrong language, then assertion failures can
4145 be triggered later on. See PR symtab/23010. So, tell
4146 dw2_instantiate_symtab to skip partial CUs -- any important
4147 partial CU will be read via DW_TAG_imported_unit anyway. */
4148 dw2_instantiate_symtab (per_cu, true);
4149 }
4150 }
4151
4152 static void
4153 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4154 const char *fullname)
4155 {
4156 struct dwarf2_per_objfile *dwarf2_per_objfile
4157 = get_dwarf2_per_objfile (objfile);
4158
4159 /* We don't need to consider type units here.
4160 This is only called for examining code, e.g. expand_line_sal.
4161 There can be an order of magnitude (or more) more type units
4162 than comp units, and we avoid them if we can. */
4163
4164 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4165 {
4166 /* We only need to look at symtabs not already expanded. */
4167 if (per_cu->v.quick->compunit_symtab)
4168 continue;
4169
4170 quick_file_names *file_data = dw2_get_file_names (per_cu);
4171 if (file_data == NULL)
4172 continue;
4173
4174 for (int j = 0; j < file_data->num_file_names; ++j)
4175 {
4176 const char *this_fullname = file_data->file_names[j];
4177
4178 if (filename_cmp (this_fullname, fullname) == 0)
4179 {
4180 dw2_instantiate_symtab (per_cu, false);
4181 break;
4182 }
4183 }
4184 }
4185 }
4186
4187 static void
4188 dw2_map_matching_symbols (struct objfile *objfile,
4189 const char * name, domain_enum domain,
4190 int global,
4191 int (*callback) (struct block *,
4192 struct symbol *, void *),
4193 void *data, symbol_name_match_type match,
4194 symbol_compare_ftype *ordered_compare)
4195 {
4196 /* Currently unimplemented; used for Ada. The function can be called if the
4197 current language is Ada for a non-Ada objfile using GNU index. As Ada
4198 does not look for non-Ada symbols this function should just return. */
4199 }
4200
4201 /* Symbol name matcher for .gdb_index names.
4202
4203 Symbol names in .gdb_index have a few particularities:
4204
4205 - There's no indication of which is the language of each symbol.
4206
4207 Since each language has its own symbol name matching algorithm,
4208 and we don't know which language is the right one, we must match
4209 each symbol against all languages. This would be a potential
4210 performance problem if it were not mitigated by the
4211 mapped_index::name_components lookup table, which significantly
4212 reduces the number of times we need to call into this matcher,
4213 making it a non-issue.
4214
4215 - Symbol names in the index have no overload (parameter)
4216 information. I.e., in C++, "foo(int)" and "foo(long)" both
4217 appear as "foo" in the index, for example.
4218
4219 This means that the lookup names passed to the symbol name
4220 matcher functions must have no parameter information either
4221 because (e.g.) symbol search name "foo" does not match
4222 lookup-name "foo(int)" [while swapping search name for lookup
4223 name would match].
4224 */
4225 class gdb_index_symbol_name_matcher
4226 {
4227 public:
4228 /* Prepares the vector of comparison functions for LOOKUP_NAME. */
4229 gdb_index_symbol_name_matcher (const lookup_name_info &lookup_name);
4230
4231 /* Walk all the matcher routines and match SYMBOL_NAME against them.
4232 Returns true if any matcher matches. */
4233 bool matches (const char *symbol_name);
4234
4235 private:
4236 /* A reference to the lookup name we're matching against. */
4237 const lookup_name_info &m_lookup_name;
4238
4239 /* A vector holding all the different symbol name matchers, for all
4240 languages. */
4241 std::vector<symbol_name_matcher_ftype *> m_symbol_name_matcher_funcs;
4242 };
4243
4244 gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
4245 (const lookup_name_info &lookup_name)
4246 : m_lookup_name (lookup_name)
4247 {
4248 /* Prepare the vector of comparison functions upfront, to avoid
4249 doing the same work for each symbol. Care is taken to avoid
4250 matching with the same matcher more than once if/when multiple
4251 languages use the same matcher function. */
4252 auto &matchers = m_symbol_name_matcher_funcs;
4253 matchers.reserve (nr_languages);
4254
4255 matchers.push_back (default_symbol_name_matcher);
4256
4257 for (int i = 0; i < nr_languages; i++)
4258 {
4259 const language_defn *lang = language_def ((enum language) i);
4260 symbol_name_matcher_ftype *name_matcher
4261 = get_symbol_name_matcher (lang, m_lookup_name);
4262
4263 /* Don't insert the same comparison routine more than once.
4264 Note that we do this linear walk instead of a seemingly
4265 cheaper sorted insert, or use a std::set or something like
4266 that, because relative order of function addresses is not
4267 stable. This is not a problem in practice because the number
4268 of supported languages is low, and the cost here is tiny
4269 compared to the number of searches we'll do afterwards using
4270 this object. */
4271 if (name_matcher != default_symbol_name_matcher
4272 && (std::find (matchers.begin (), matchers.end (), name_matcher)
4273 == matchers.end ()))
4274 matchers.push_back (name_matcher);
4275 }
4276 }
4277
4278 bool
4279 gdb_index_symbol_name_matcher::matches (const char *symbol_name)
4280 {
4281 for (auto matches_name : m_symbol_name_matcher_funcs)
4282 if (matches_name (symbol_name, m_lookup_name, NULL))
4283 return true;
4284
4285 return false;
4286 }
4287
4288 /* Starting from a search name, return the string that finds the upper
4289 bound of all strings that start with SEARCH_NAME in a sorted name
4290 list. Returns the empty string to indicate that the upper bound is
4291 the end of the list. */
4292
4293 static std::string
4294 make_sort_after_prefix_name (const char *search_name)
4295 {
4296 /* When looking to complete "func", we find the upper bound of all
4297 symbols that start with "func" by looking for where we'd insert
4298 the closest string that would follow "func" in lexicographical
4299 order. Usually, that's "func"-with-last-character-incremented,
4300 i.e. "fund". Mind non-ASCII characters, though. Usually those
4301 will be UTF-8 multi-byte sequences, but we can't be certain.
4302 Especially mind the 0xff character, which is a valid character in
4303 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4304 rule out compilers allowing it in identifiers. Note that
4305 conveniently, strcmp/strcasecmp are specified to compare
4306 characters interpreted as unsigned char. So what we do is treat
4307 the whole string as a base 256 number composed of a sequence of
4308 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4309 to 0, and carries 1 to the following more-significant position.
4310 If the very first character in SEARCH_NAME ends up incremented
4311 and carries/overflows, then the upper bound is the end of the
4312 list. The string after the empty string is also the empty
4313 string.
4314
4315 Some examples of this operation:
4316
4317 SEARCH_NAME => "+1" RESULT
4318
4319 "abc" => "abd"
4320 "ab\xff" => "ac"
4321 "\xff" "a" "\xff" => "\xff" "b"
4322 "\xff" => ""
4323 "\xff\xff" => ""
4324 "" => ""
4325
4326 Then, with these symbols for example:
4327
4328 func
4329 func1
4330 fund
4331
4332 completing "func" looks for symbols between "func" and
4333 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4334 which finds "func" and "func1", but not "fund".
4335
4336 And with:
4337
4338 funcÿ (Latin1 'ÿ' [0xff])
4339 funcÿ1
4340 fund
4341
4342 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4343 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4344
4345 And with:
4346
4347 ÿÿ (Latin1 'ÿ' [0xff])
4348 ÿÿ1
4349
4350 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4351 the end of the list.
4352 */
4353 std::string after = search_name;
4354 while (!after.empty () && (unsigned char) after.back () == 0xff)
4355 after.pop_back ();
4356 if (!after.empty ())
4357 after.back () = (unsigned char) after.back () + 1;
4358 return after;
4359 }
4360
4361 /* See declaration. */
4362
4363 std::pair<std::vector<name_component>::const_iterator,
4364 std::vector<name_component>::const_iterator>
4365 mapped_index_base::find_name_components_bounds
4366 (const lookup_name_info &lookup_name_without_params) const
4367 {
4368 auto *name_cmp
4369 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4370
4371 const char *cplus
4372 = lookup_name_without_params.cplus ().lookup_name ().c_str ();
4373
4374 /* Comparison function object for lower_bound that matches against a
4375 given symbol name. */
4376 auto lookup_compare_lower = [&] (const name_component &elem,
4377 const char *name)
4378 {
4379 const char *elem_qualified = this->symbol_name_at (elem.idx);
4380 const char *elem_name = elem_qualified + elem.name_offset;
4381 return name_cmp (elem_name, name) < 0;
4382 };
4383
4384 /* Comparison function object for upper_bound that matches against a
4385 given symbol name. */
4386 auto lookup_compare_upper = [&] (const char *name,
4387 const name_component &elem)
4388 {
4389 const char *elem_qualified = this->symbol_name_at (elem.idx);
4390 const char *elem_name = elem_qualified + elem.name_offset;
4391 return name_cmp (name, elem_name) < 0;
4392 };
4393
4394 auto begin = this->name_components.begin ();
4395 auto end = this->name_components.end ();
4396
4397 /* Find the lower bound. */
4398 auto lower = [&] ()
4399 {
4400 if (lookup_name_without_params.completion_mode () && cplus[0] == '\0')
4401 return begin;
4402 else
4403 return std::lower_bound (begin, end, cplus, lookup_compare_lower);
4404 } ();
4405
4406 /* Find the upper bound. */
4407 auto upper = [&] ()
4408 {
4409 if (lookup_name_without_params.completion_mode ())
4410 {
4411 /* In completion mode, we want UPPER to point past all
4412 symbols names that have the same prefix. I.e., with
4413 these symbols, and completing "func":
4414
4415 function << lower bound
4416 function1
4417 other_function << upper bound
4418
4419 We find the upper bound by looking for the insertion
4420 point of "func"-with-last-character-incremented,
4421 i.e. "fund". */
4422 std::string after = make_sort_after_prefix_name (cplus);
4423 if (after.empty ())
4424 return end;
4425 return std::lower_bound (lower, end, after.c_str (),
4426 lookup_compare_lower);
4427 }
4428 else
4429 return std::upper_bound (lower, end, cplus, lookup_compare_upper);
4430 } ();
4431
4432 return {lower, upper};
4433 }
4434
4435 /* See declaration. */
4436
4437 void
4438 mapped_index_base::build_name_components ()
4439 {
4440 if (!this->name_components.empty ())
4441 return;
4442
4443 this->name_components_casing = case_sensitivity;
4444 auto *name_cmp
4445 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4446
4447 /* The code below only knows how to break apart components of C++
4448 symbol names (and other languages that use '::' as
4449 namespace/module separator). If we add support for wild matching
4450 to some language that uses some other operator (E.g., Ada, Go and
4451 D use '.'), then we'll need to try splitting the symbol name
4452 according to that language too. Note that Ada does support wild
4453 matching, but doesn't currently support .gdb_index. */
4454 auto count = this->symbol_name_count ();
4455 for (offset_type idx = 0; idx < count; idx++)
4456 {
4457 if (this->symbol_name_slot_invalid (idx))
4458 continue;
4459
4460 const char *name = this->symbol_name_at (idx);
4461
4462 /* Add each name component to the name component table. */
4463 unsigned int previous_len = 0;
4464 for (unsigned int current_len = cp_find_first_component (name);
4465 name[current_len] != '\0';
4466 current_len += cp_find_first_component (name + current_len))
4467 {
4468 gdb_assert (name[current_len] == ':');
4469 this->name_components.push_back ({previous_len, idx});
4470 /* Skip the '::'. */
4471 current_len += 2;
4472 previous_len = current_len;
4473 }
4474 this->name_components.push_back ({previous_len, idx});
4475 }
4476
4477 /* Sort name_components elements by name. */
4478 auto name_comp_compare = [&] (const name_component &left,
4479 const name_component &right)
4480 {
4481 const char *left_qualified = this->symbol_name_at (left.idx);
4482 const char *right_qualified = this->symbol_name_at (right.idx);
4483
4484 const char *left_name = left_qualified + left.name_offset;
4485 const char *right_name = right_qualified + right.name_offset;
4486
4487 return name_cmp (left_name, right_name) < 0;
4488 };
4489
4490 std::sort (this->name_components.begin (),
4491 this->name_components.end (),
4492 name_comp_compare);
4493 }
4494
4495 /* Helper for dw2_expand_symtabs_matching that works with a
4496 mapped_index_base instead of the containing objfile. This is split
4497 to a separate function in order to be able to unit test the
4498 name_components matching using a mock mapped_index_base. For each
4499 symbol name that matches, calls MATCH_CALLBACK, passing it the
4500 symbol's index in the mapped_index_base symbol table. */
4501
4502 static void
4503 dw2_expand_symtabs_matching_symbol
4504 (mapped_index_base &index,
4505 const lookup_name_info &lookup_name_in,
4506 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4507 enum search_domain kind,
4508 gdb::function_view<void (offset_type)> match_callback)
4509 {
4510 lookup_name_info lookup_name_without_params
4511 = lookup_name_in.make_ignore_params ();
4512 gdb_index_symbol_name_matcher lookup_name_matcher
4513 (lookup_name_without_params);
4514
4515 /* Build the symbol name component sorted vector, if we haven't
4516 yet. */
4517 index.build_name_components ();
4518
4519 auto bounds = index.find_name_components_bounds (lookup_name_without_params);
4520
4521 /* Now for each symbol name in range, check to see if we have a name
4522 match, and if so, call the MATCH_CALLBACK callback. */
4523
4524 /* The same symbol may appear more than once in the range though.
4525 E.g., if we're looking for symbols that complete "w", and we have
4526 a symbol named "w1::w2", we'll find the two name components for
4527 that same symbol in the range. To be sure we only call the
4528 callback once per symbol, we first collect the symbol name
4529 indexes that matched in a temporary vector and ignore
4530 duplicates. */
4531 std::vector<offset_type> matches;
4532 matches.reserve (std::distance (bounds.first, bounds.second));
4533
4534 for (; bounds.first != bounds.second; ++bounds.first)
4535 {
4536 const char *qualified = index.symbol_name_at (bounds.first->idx);
4537
4538 if (!lookup_name_matcher.matches (qualified)
4539 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4540 continue;
4541
4542 matches.push_back (bounds.first->idx);
4543 }
4544
4545 std::sort (matches.begin (), matches.end ());
4546
4547 /* Finally call the callback, once per match. */
4548 ULONGEST prev = -1;
4549 for (offset_type idx : matches)
4550 {
4551 if (prev != idx)
4552 {
4553 match_callback (idx);
4554 prev = idx;
4555 }
4556 }
4557
4558 /* Above we use a type wider than idx's for 'prev', since 0 and
4559 (offset_type)-1 are both possible values. */
4560 static_assert (sizeof (prev) > sizeof (offset_type), "");
4561 }
4562
4563 #if GDB_SELF_TEST
4564
4565 namespace selftests { namespace dw2_expand_symtabs_matching {
4566
4567 /* A mock .gdb_index/.debug_names-like name index table, enough to
4568 exercise dw2_expand_symtabs_matching_symbol, which works with the
4569 mapped_index_base interface. Builds an index from the symbol list
4570 passed as parameter to the constructor. */
4571 class mock_mapped_index : public mapped_index_base
4572 {
4573 public:
4574 mock_mapped_index (gdb::array_view<const char *> symbols)
4575 : m_symbol_table (symbols)
4576 {}
4577
4578 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4579
4580 /* Return the number of names in the symbol table. */
4581 size_t symbol_name_count () const override
4582 {
4583 return m_symbol_table.size ();
4584 }
4585
4586 /* Get the name of the symbol at IDX in the symbol table. */
4587 const char *symbol_name_at (offset_type idx) const override
4588 {
4589 return m_symbol_table[idx];
4590 }
4591
4592 private:
4593 gdb::array_view<const char *> m_symbol_table;
4594 };
4595
4596 /* Convenience function that converts a NULL pointer to a "<null>"
4597 string, to pass to print routines. */
4598
4599 static const char *
4600 string_or_null (const char *str)
4601 {
4602 return str != NULL ? str : "<null>";
4603 }
4604
4605 /* Check if a lookup_name_info built from
4606 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4607 index. EXPECTED_LIST is the list of expected matches, in expected
4608 matching order. If no match expected, then an empty list is
4609 specified. Returns true on success. On failure prints a warning
4610 indicating the file:line that failed, and returns false. */
4611
4612 static bool
4613 check_match (const char *file, int line,
4614 mock_mapped_index &mock_index,
4615 const char *name, symbol_name_match_type match_type,
4616 bool completion_mode,
4617 std::initializer_list<const char *> expected_list)
4618 {
4619 lookup_name_info lookup_name (name, match_type, completion_mode);
4620
4621 bool matched = true;
4622
4623 auto mismatch = [&] (const char *expected_str,
4624 const char *got)
4625 {
4626 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4627 "expected=\"%s\", got=\"%s\"\n"),
4628 file, line,
4629 (match_type == symbol_name_match_type::FULL
4630 ? "FULL" : "WILD"),
4631 name, string_or_null (expected_str), string_or_null (got));
4632 matched = false;
4633 };
4634
4635 auto expected_it = expected_list.begin ();
4636 auto expected_end = expected_list.end ();
4637
4638 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4639 NULL, ALL_DOMAIN,
4640 [&] (offset_type idx)
4641 {
4642 const char *matched_name = mock_index.symbol_name_at (idx);
4643 const char *expected_str
4644 = expected_it == expected_end ? NULL : *expected_it++;
4645
4646 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4647 mismatch (expected_str, matched_name);
4648 });
4649
4650 const char *expected_str
4651 = expected_it == expected_end ? NULL : *expected_it++;
4652 if (expected_str != NULL)
4653 mismatch (expected_str, NULL);
4654
4655 return matched;
4656 }
4657
4658 /* The symbols added to the mock mapped_index for testing (in
4659 canonical form). */
4660 static const char *test_symbols[] = {
4661 "function",
4662 "std::bar",
4663 "std::zfunction",
4664 "std::zfunction2",
4665 "w1::w2",
4666 "ns::foo<char*>",
4667 "ns::foo<int>",
4668 "ns::foo<long>",
4669 "ns2::tmpl<int>::foo2",
4670 "(anonymous namespace)::A::B::C",
4671
4672 /* These are used to check that the increment-last-char in the
4673 matching algorithm for completion doesn't match "t1_fund" when
4674 completing "t1_func". */
4675 "t1_func",
4676 "t1_func1",
4677 "t1_fund",
4678 "t1_fund1",
4679
4680 /* A UTF-8 name with multi-byte sequences to make sure that
4681 cp-name-parser understands this as a single identifier ("função"
4682 is "function" in PT). */
4683 u8"u8função",
4684
4685 /* \377 (0xff) is Latin1 'ÿ'. */
4686 "yfunc\377",
4687
4688 /* \377 (0xff) is Latin1 'ÿ'. */
4689 "\377",
4690 "\377\377123",
4691
4692 /* A name with all sorts of complications. Starts with "z" to make
4693 it easier for the completion tests below. */
4694 #define Z_SYM_NAME \
4695 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4696 "::tuple<(anonymous namespace)::ui*, " \
4697 "std::default_delete<(anonymous namespace)::ui>, void>"
4698
4699 Z_SYM_NAME
4700 };
4701
4702 /* Returns true if the mapped_index_base::find_name_component_bounds
4703 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4704 in completion mode. */
4705
4706 static bool
4707 check_find_bounds_finds (mapped_index_base &index,
4708 const char *search_name,
4709 gdb::array_view<const char *> expected_syms)
4710 {
4711 lookup_name_info lookup_name (search_name,
4712 symbol_name_match_type::FULL, true);
4713
4714 auto bounds = index.find_name_components_bounds (lookup_name);
4715
4716 size_t distance = std::distance (bounds.first, bounds.second);
4717 if (distance != expected_syms.size ())
4718 return false;
4719
4720 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4721 {
4722 auto nc_elem = bounds.first + exp_elem;
4723 const char *qualified = index.symbol_name_at (nc_elem->idx);
4724 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4725 return false;
4726 }
4727
4728 return true;
4729 }
4730
4731 /* Test the lower-level mapped_index::find_name_component_bounds
4732 method. */
4733
4734 static void
4735 test_mapped_index_find_name_component_bounds ()
4736 {
4737 mock_mapped_index mock_index (test_symbols);
4738
4739 mock_index.build_name_components ();
4740
4741 /* Test the lower-level mapped_index::find_name_component_bounds
4742 method in completion mode. */
4743 {
4744 static const char *expected_syms[] = {
4745 "t1_func",
4746 "t1_func1",
4747 };
4748
4749 SELF_CHECK (check_find_bounds_finds (mock_index,
4750 "t1_func", expected_syms));
4751 }
4752
4753 /* Check that the increment-last-char in the name matching algorithm
4754 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4755 {
4756 static const char *expected_syms1[] = {
4757 "\377",
4758 "\377\377123",
4759 };
4760 SELF_CHECK (check_find_bounds_finds (mock_index,
4761 "\377", expected_syms1));
4762
4763 static const char *expected_syms2[] = {
4764 "\377\377123",
4765 };
4766 SELF_CHECK (check_find_bounds_finds (mock_index,
4767 "\377\377", expected_syms2));
4768 }
4769 }
4770
4771 /* Test dw2_expand_symtabs_matching_symbol. */
4772
4773 static void
4774 test_dw2_expand_symtabs_matching_symbol ()
4775 {
4776 mock_mapped_index mock_index (test_symbols);
4777
4778 /* We let all tests run until the end even if some fails, for debug
4779 convenience. */
4780 bool any_mismatch = false;
4781
4782 /* Create the expected symbols list (an initializer_list). Needed
4783 because lists have commas, and we need to pass them to CHECK,
4784 which is a macro. */
4785 #define EXPECT(...) { __VA_ARGS__ }
4786
4787 /* Wrapper for check_match that passes down the current
4788 __FILE__/__LINE__. */
4789 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4790 any_mismatch |= !check_match (__FILE__, __LINE__, \
4791 mock_index, \
4792 NAME, MATCH_TYPE, COMPLETION_MODE, \
4793 EXPECTED_LIST)
4794
4795 /* Identity checks. */
4796 for (const char *sym : test_symbols)
4797 {
4798 /* Should be able to match all existing symbols. */
4799 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4800 EXPECT (sym));
4801
4802 /* Should be able to match all existing symbols with
4803 parameters. */
4804 std::string with_params = std::string (sym) + "(int)";
4805 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4806 EXPECT (sym));
4807
4808 /* Should be able to match all existing symbols with
4809 parameters and qualifiers. */
4810 with_params = std::string (sym) + " ( int ) const";
4811 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4812 EXPECT (sym));
4813
4814 /* This should really find sym, but cp-name-parser.y doesn't
4815 know about lvalue/rvalue qualifiers yet. */
4816 with_params = std::string (sym) + " ( int ) &&";
4817 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4818 {});
4819 }
4820
4821 /* Check that the name matching algorithm for completion doesn't get
4822 confused with Latin1 'ÿ' / 0xff. */
4823 {
4824 static const char str[] = "\377";
4825 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4826 EXPECT ("\377", "\377\377123"));
4827 }
4828
4829 /* Check that the increment-last-char in the matching algorithm for
4830 completion doesn't match "t1_fund" when completing "t1_func". */
4831 {
4832 static const char str[] = "t1_func";
4833 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4834 EXPECT ("t1_func", "t1_func1"));
4835 }
4836
4837 /* Check that completion mode works at each prefix of the expected
4838 symbol name. */
4839 {
4840 static const char str[] = "function(int)";
4841 size_t len = strlen (str);
4842 std::string lookup;
4843
4844 for (size_t i = 1; i < len; i++)
4845 {
4846 lookup.assign (str, i);
4847 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4848 EXPECT ("function"));
4849 }
4850 }
4851
4852 /* While "w" is a prefix of both components, the match function
4853 should still only be called once. */
4854 {
4855 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4856 EXPECT ("w1::w2"));
4857 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4858 EXPECT ("w1::w2"));
4859 }
4860
4861 /* Same, with a "complicated" symbol. */
4862 {
4863 static const char str[] = Z_SYM_NAME;
4864 size_t len = strlen (str);
4865 std::string lookup;
4866
4867 for (size_t i = 1; i < len; i++)
4868 {
4869 lookup.assign (str, i);
4870 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4871 EXPECT (Z_SYM_NAME));
4872 }
4873 }
4874
4875 /* In FULL mode, an incomplete symbol doesn't match. */
4876 {
4877 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4878 {});
4879 }
4880
4881 /* A complete symbol with parameters matches any overload, since the
4882 index has no overload info. */
4883 {
4884 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4885 EXPECT ("std::zfunction", "std::zfunction2"));
4886 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4887 EXPECT ("std::zfunction", "std::zfunction2"));
4888 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4889 EXPECT ("std::zfunction", "std::zfunction2"));
4890 }
4891
4892 /* Check that whitespace is ignored appropriately. A symbol with a
4893 template argument list. */
4894 {
4895 static const char expected[] = "ns::foo<int>";
4896 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4897 EXPECT (expected));
4898 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4899 EXPECT (expected));
4900 }
4901
4902 /* Check that whitespace is ignored appropriately. A symbol with a
4903 template argument list that includes a pointer. */
4904 {
4905 static const char expected[] = "ns::foo<char*>";
4906 /* Try both completion and non-completion modes. */
4907 static const bool completion_mode[2] = {false, true};
4908 for (size_t i = 0; i < 2; i++)
4909 {
4910 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4911 completion_mode[i], EXPECT (expected));
4912 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4913 completion_mode[i], EXPECT (expected));
4914
4915 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4916 completion_mode[i], EXPECT (expected));
4917 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4918 completion_mode[i], EXPECT (expected));
4919 }
4920 }
4921
4922 {
4923 /* Check method qualifiers are ignored. */
4924 static const char expected[] = "ns::foo<char*>";
4925 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4926 symbol_name_match_type::FULL, true, EXPECT (expected));
4927 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4928 symbol_name_match_type::FULL, true, EXPECT (expected));
4929 CHECK_MATCH ("foo < char * > ( int ) const",
4930 symbol_name_match_type::WILD, true, EXPECT (expected));
4931 CHECK_MATCH ("foo < char * > ( int ) &&",
4932 symbol_name_match_type::WILD, true, EXPECT (expected));
4933 }
4934
4935 /* Test lookup names that don't match anything. */
4936 {
4937 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4938 {});
4939
4940 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4941 {});
4942 }
4943
4944 /* Some wild matching tests, exercising "(anonymous namespace)",
4945 which should not be confused with a parameter list. */
4946 {
4947 static const char *syms[] = {
4948 "A::B::C",
4949 "B::C",
4950 "C",
4951 "A :: B :: C ( int )",
4952 "B :: C ( int )",
4953 "C ( int )",
4954 };
4955
4956 for (const char *s : syms)
4957 {
4958 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4959 EXPECT ("(anonymous namespace)::A::B::C"));
4960 }
4961 }
4962
4963 {
4964 static const char expected[] = "ns2::tmpl<int>::foo2";
4965 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4966 EXPECT (expected));
4967 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4968 EXPECT (expected));
4969 }
4970
4971 SELF_CHECK (!any_mismatch);
4972
4973 #undef EXPECT
4974 #undef CHECK_MATCH
4975 }
4976
4977 static void
4978 run_test ()
4979 {
4980 test_mapped_index_find_name_component_bounds ();
4981 test_dw2_expand_symtabs_matching_symbol ();
4982 }
4983
4984 }} // namespace selftests::dw2_expand_symtabs_matching
4985
4986 #endif /* GDB_SELF_TEST */
4987
4988 /* If FILE_MATCHER is NULL or if PER_CU has
4989 dwarf2_per_cu_quick_data::MARK set (see
4990 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4991 EXPANSION_NOTIFY on it. */
4992
4993 static void
4994 dw2_expand_symtabs_matching_one
4995 (struct dwarf2_per_cu_data *per_cu,
4996 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4997 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4998 {
4999 if (file_matcher == NULL || per_cu->v.quick->mark)
5000 {
5001 bool symtab_was_null
5002 = (per_cu->v.quick->compunit_symtab == NULL);
5003
5004 dw2_instantiate_symtab (per_cu, false);
5005
5006 if (expansion_notify != NULL
5007 && symtab_was_null
5008 && per_cu->v.quick->compunit_symtab != NULL)
5009 expansion_notify (per_cu->v.quick->compunit_symtab);
5010 }
5011 }
5012
5013 /* Helper for dw2_expand_matching symtabs. Called on each symbol
5014 matched, to expand corresponding CUs that were marked. IDX is the
5015 index of the symbol name that matched. */
5016
5017 static void
5018 dw2_expand_marked_cus
5019 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
5020 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5021 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5022 search_domain kind)
5023 {
5024 offset_type *vec, vec_len, vec_idx;
5025 bool global_seen = false;
5026 mapped_index &index = *dwarf2_per_objfile->index_table;
5027
5028 vec = (offset_type *) (index.constant_pool
5029 + MAYBE_SWAP (index.symbol_table[idx].vec));
5030 vec_len = MAYBE_SWAP (vec[0]);
5031 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5032 {
5033 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5034 /* This value is only valid for index versions >= 7. */
5035 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5036 gdb_index_symbol_kind symbol_kind =
5037 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5038 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5039 /* Only check the symbol attributes if they're present.
5040 Indices prior to version 7 don't record them,
5041 and indices >= 7 may elide them for certain symbols
5042 (gold does this). */
5043 int attrs_valid =
5044 (index.version >= 7
5045 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5046
5047 /* Work around gold/15646. */
5048 if (attrs_valid)
5049 {
5050 if (!is_static && global_seen)
5051 continue;
5052 if (!is_static)
5053 global_seen = true;
5054 }
5055
5056 /* Only check the symbol's kind if it has one. */
5057 if (attrs_valid)
5058 {
5059 switch (kind)
5060 {
5061 case VARIABLES_DOMAIN:
5062 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5063 continue;
5064 break;
5065 case FUNCTIONS_DOMAIN:
5066 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
5067 continue;
5068 break;
5069 case TYPES_DOMAIN:
5070 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5071 continue;
5072 break;
5073 default:
5074 break;
5075 }
5076 }
5077
5078 /* Don't crash on bad data. */
5079 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
5080 + dwarf2_per_objfile->all_type_units.size ()))
5081 {
5082 complaint (&symfile_complaints,
5083 _(".gdb_index entry has bad CU index"
5084 " [in module %s]"),
5085 objfile_name (dwarf2_per_objfile->objfile));
5086 continue;
5087 }
5088
5089 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
5090 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5091 expansion_notify);
5092 }
5093 }
5094
5095 /* If FILE_MATCHER is non-NULL, set all the
5096 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5097 that match FILE_MATCHER. */
5098
5099 static void
5100 dw_expand_symtabs_matching_file_matcher
5101 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5102 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
5103 {
5104 if (file_matcher == NULL)
5105 return;
5106
5107 objfile *const objfile = dwarf2_per_objfile->objfile;
5108
5109 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5110 htab_eq_pointer,
5111 NULL, xcalloc, xfree));
5112 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
5113 htab_eq_pointer,
5114 NULL, xcalloc, xfree));
5115
5116 /* The rule is CUs specify all the files, including those used by
5117 any TU, so there's no need to scan TUs here. */
5118
5119 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5120 {
5121 QUIT;
5122
5123 per_cu->v.quick->mark = 0;
5124
5125 /* We only need to look at symtabs not already expanded. */
5126 if (per_cu->v.quick->compunit_symtab)
5127 continue;
5128
5129 quick_file_names *file_data = dw2_get_file_names (per_cu);
5130 if (file_data == NULL)
5131 continue;
5132
5133 if (htab_find (visited_not_found.get (), file_data) != NULL)
5134 continue;
5135 else if (htab_find (visited_found.get (), file_data) != NULL)
5136 {
5137 per_cu->v.quick->mark = 1;
5138 continue;
5139 }
5140
5141 for (int j = 0; j < file_data->num_file_names; ++j)
5142 {
5143 const char *this_real_name;
5144
5145 if (file_matcher (file_data->file_names[j], false))
5146 {
5147 per_cu->v.quick->mark = 1;
5148 break;
5149 }
5150
5151 /* Before we invoke realpath, which can get expensive when many
5152 files are involved, do a quick comparison of the basenames. */
5153 if (!basenames_may_differ
5154 && !file_matcher (lbasename (file_data->file_names[j]),
5155 true))
5156 continue;
5157
5158 this_real_name = dw2_get_real_path (objfile, file_data, j);
5159 if (file_matcher (this_real_name, false))
5160 {
5161 per_cu->v.quick->mark = 1;
5162 break;
5163 }
5164 }
5165
5166 void **slot = htab_find_slot (per_cu->v.quick->mark
5167 ? visited_found.get ()
5168 : visited_not_found.get (),
5169 file_data, INSERT);
5170 *slot = file_data;
5171 }
5172 }
5173
5174 static void
5175 dw2_expand_symtabs_matching
5176 (struct objfile *objfile,
5177 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5178 const lookup_name_info &lookup_name,
5179 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5180 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5181 enum search_domain kind)
5182 {
5183 struct dwarf2_per_objfile *dwarf2_per_objfile
5184 = get_dwarf2_per_objfile (objfile);
5185
5186 /* index_table is NULL if OBJF_READNOW. */
5187 if (!dwarf2_per_objfile->index_table)
5188 return;
5189
5190 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5191
5192 mapped_index &index = *dwarf2_per_objfile->index_table;
5193
5194 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5195 symbol_matcher,
5196 kind, [&] (offset_type idx)
5197 {
5198 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
5199 expansion_notify, kind);
5200 });
5201 }
5202
5203 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5204 symtab. */
5205
5206 static struct compunit_symtab *
5207 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5208 CORE_ADDR pc)
5209 {
5210 int i;
5211
5212 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5213 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5214 return cust;
5215
5216 if (cust->includes == NULL)
5217 return NULL;
5218
5219 for (i = 0; cust->includes[i]; ++i)
5220 {
5221 struct compunit_symtab *s = cust->includes[i];
5222
5223 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5224 if (s != NULL)
5225 return s;
5226 }
5227
5228 return NULL;
5229 }
5230
5231 static struct compunit_symtab *
5232 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5233 struct bound_minimal_symbol msymbol,
5234 CORE_ADDR pc,
5235 struct obj_section *section,
5236 int warn_if_readin)
5237 {
5238 struct dwarf2_per_cu_data *data;
5239 struct compunit_symtab *result;
5240
5241 if (!objfile->psymtabs_addrmap)
5242 return NULL;
5243
5244 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
5245 pc);
5246 if (!data)
5247 return NULL;
5248
5249 if (warn_if_readin && data->v.quick->compunit_symtab)
5250 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5251 paddress (get_objfile_arch (objfile), pc));
5252
5253 result
5254 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
5255 false),
5256 pc);
5257 gdb_assert (result != NULL);
5258 return result;
5259 }
5260
5261 static void
5262 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5263 void *data, int need_fullname)
5264 {
5265 struct dwarf2_per_objfile *dwarf2_per_objfile
5266 = get_dwarf2_per_objfile (objfile);
5267
5268 if (!dwarf2_per_objfile->filenames_cache)
5269 {
5270 dwarf2_per_objfile->filenames_cache.emplace ();
5271
5272 htab_up visited (htab_create_alloc (10,
5273 htab_hash_pointer, htab_eq_pointer,
5274 NULL, xcalloc, xfree));
5275
5276 /* The rule is CUs specify all the files, including those used
5277 by any TU, so there's no need to scan TUs here. We can
5278 ignore file names coming from already-expanded CUs. */
5279
5280 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5281 {
5282 if (per_cu->v.quick->compunit_symtab)
5283 {
5284 void **slot = htab_find_slot (visited.get (),
5285 per_cu->v.quick->file_names,
5286 INSERT);
5287
5288 *slot = per_cu->v.quick->file_names;
5289 }
5290 }
5291
5292 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5293 {
5294 /* We only need to look at symtabs not already expanded. */
5295 if (per_cu->v.quick->compunit_symtab)
5296 continue;
5297
5298 quick_file_names *file_data = dw2_get_file_names (per_cu);
5299 if (file_data == NULL)
5300 continue;
5301
5302 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
5303 if (*slot)
5304 {
5305 /* Already visited. */
5306 continue;
5307 }
5308 *slot = file_data;
5309
5310 for (int j = 0; j < file_data->num_file_names; ++j)
5311 {
5312 const char *filename = file_data->file_names[j];
5313 dwarf2_per_objfile->filenames_cache->seen (filename);
5314 }
5315 }
5316 }
5317
5318 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5319 {
5320 gdb::unique_xmalloc_ptr<char> this_real_name;
5321
5322 if (need_fullname)
5323 this_real_name = gdb_realpath (filename);
5324 (*fun) (filename, this_real_name.get (), data);
5325 });
5326 }
5327
5328 static int
5329 dw2_has_symbols (struct objfile *objfile)
5330 {
5331 return 1;
5332 }
5333
5334 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5335 {
5336 dw2_has_symbols,
5337 dw2_find_last_source_symtab,
5338 dw2_forget_cached_source_info,
5339 dw2_map_symtabs_matching_filename,
5340 dw2_lookup_symbol,
5341 dw2_print_stats,
5342 dw2_dump,
5343 dw2_relocate,
5344 dw2_expand_symtabs_for_function,
5345 dw2_expand_all_symtabs,
5346 dw2_expand_symtabs_with_fullname,
5347 dw2_map_matching_symbols,
5348 dw2_expand_symtabs_matching,
5349 dw2_find_pc_sect_compunit_symtab,
5350 NULL,
5351 dw2_map_symbol_filenames
5352 };
5353
5354 /* DWARF-5 debug_names reader. */
5355
5356 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5357 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5358
5359 /* A helper function that reads the .debug_names section in SECTION
5360 and fills in MAP. FILENAME is the name of the file containing the
5361 section; it is used for error reporting.
5362
5363 Returns true if all went well, false otherwise. */
5364
5365 static bool
5366 read_debug_names_from_section (struct objfile *objfile,
5367 const char *filename,
5368 struct dwarf2_section_info *section,
5369 mapped_debug_names &map)
5370 {
5371 if (dwarf2_section_empty_p (section))
5372 return false;
5373
5374 /* Older elfutils strip versions could keep the section in the main
5375 executable while splitting it for the separate debug info file. */
5376 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5377 return false;
5378
5379 dwarf2_read_section (objfile, section);
5380
5381 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5382
5383 const gdb_byte *addr = section->buffer;
5384
5385 bfd *const abfd = get_section_bfd_owner (section);
5386
5387 unsigned int bytes_read;
5388 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5389 addr += bytes_read;
5390
5391 map.dwarf5_is_dwarf64 = bytes_read != 4;
5392 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5393 if (bytes_read + length != section->size)
5394 {
5395 /* There may be multiple per-CU indices. */
5396 warning (_("Section .debug_names in %s length %s does not match "
5397 "section length %s, ignoring .debug_names."),
5398 filename, plongest (bytes_read + length),
5399 pulongest (section->size));
5400 return false;
5401 }
5402
5403 /* The version number. */
5404 uint16_t version = read_2_bytes (abfd, addr);
5405 addr += 2;
5406 if (version != 5)
5407 {
5408 warning (_("Section .debug_names in %s has unsupported version %d, "
5409 "ignoring .debug_names."),
5410 filename, version);
5411 return false;
5412 }
5413
5414 /* Padding. */
5415 uint16_t padding = read_2_bytes (abfd, addr);
5416 addr += 2;
5417 if (padding != 0)
5418 {
5419 warning (_("Section .debug_names in %s has unsupported padding %d, "
5420 "ignoring .debug_names."),
5421 filename, padding);
5422 return false;
5423 }
5424
5425 /* comp_unit_count - The number of CUs in the CU list. */
5426 map.cu_count = read_4_bytes (abfd, addr);
5427 addr += 4;
5428
5429 /* local_type_unit_count - The number of TUs in the local TU
5430 list. */
5431 map.tu_count = read_4_bytes (abfd, addr);
5432 addr += 4;
5433
5434 /* foreign_type_unit_count - The number of TUs in the foreign TU
5435 list. */
5436 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5437 addr += 4;
5438 if (foreign_tu_count != 0)
5439 {
5440 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5441 "ignoring .debug_names."),
5442 filename, static_cast<unsigned long> (foreign_tu_count));
5443 return false;
5444 }
5445
5446 /* bucket_count - The number of hash buckets in the hash lookup
5447 table. */
5448 map.bucket_count = read_4_bytes (abfd, addr);
5449 addr += 4;
5450
5451 /* name_count - The number of unique names in the index. */
5452 map.name_count = read_4_bytes (abfd, addr);
5453 addr += 4;
5454
5455 /* abbrev_table_size - The size in bytes of the abbreviations
5456 table. */
5457 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5458 addr += 4;
5459
5460 /* augmentation_string_size - The size in bytes of the augmentation
5461 string. This value is rounded up to a multiple of 4. */
5462 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5463 addr += 4;
5464 map.augmentation_is_gdb = ((augmentation_string_size
5465 == sizeof (dwarf5_augmentation))
5466 && memcmp (addr, dwarf5_augmentation,
5467 sizeof (dwarf5_augmentation)) == 0);
5468 augmentation_string_size += (-augmentation_string_size) & 3;
5469 addr += augmentation_string_size;
5470
5471 /* List of CUs */
5472 map.cu_table_reordered = addr;
5473 addr += map.cu_count * map.offset_size;
5474
5475 /* List of Local TUs */
5476 map.tu_table_reordered = addr;
5477 addr += map.tu_count * map.offset_size;
5478
5479 /* Hash Lookup Table */
5480 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5481 addr += map.bucket_count * 4;
5482 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5483 addr += map.name_count * 4;
5484
5485 /* Name Table */
5486 map.name_table_string_offs_reordered = addr;
5487 addr += map.name_count * map.offset_size;
5488 map.name_table_entry_offs_reordered = addr;
5489 addr += map.name_count * map.offset_size;
5490
5491 const gdb_byte *abbrev_table_start = addr;
5492 for (;;)
5493 {
5494 unsigned int bytes_read;
5495 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5496 addr += bytes_read;
5497 if (index_num == 0)
5498 break;
5499
5500 const auto insertpair
5501 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5502 if (!insertpair.second)
5503 {
5504 warning (_("Section .debug_names in %s has duplicate index %s, "
5505 "ignoring .debug_names."),
5506 filename, pulongest (index_num));
5507 return false;
5508 }
5509 mapped_debug_names::index_val &indexval = insertpair.first->second;
5510 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5511 addr += bytes_read;
5512
5513 for (;;)
5514 {
5515 mapped_debug_names::index_val::attr attr;
5516 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5517 addr += bytes_read;
5518 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5519 addr += bytes_read;
5520 if (attr.form == DW_FORM_implicit_const)
5521 {
5522 attr.implicit_const = read_signed_leb128 (abfd, addr,
5523 &bytes_read);
5524 addr += bytes_read;
5525 }
5526 if (attr.dw_idx == 0 && attr.form == 0)
5527 break;
5528 indexval.attr_vec.push_back (std::move (attr));
5529 }
5530 }
5531 if (addr != abbrev_table_start + abbrev_table_size)
5532 {
5533 warning (_("Section .debug_names in %s has abbreviation_table "
5534 "of size %zu vs. written as %u, ignoring .debug_names."),
5535 filename, addr - abbrev_table_start, abbrev_table_size);
5536 return false;
5537 }
5538 map.entry_pool = addr;
5539
5540 return true;
5541 }
5542
5543 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5544 list. */
5545
5546 static void
5547 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5548 const mapped_debug_names &map,
5549 dwarf2_section_info &section,
5550 bool is_dwz)
5551 {
5552 sect_offset sect_off_prev;
5553 for (uint32_t i = 0; i <= map.cu_count; ++i)
5554 {
5555 sect_offset sect_off_next;
5556 if (i < map.cu_count)
5557 {
5558 sect_off_next
5559 = (sect_offset) (extract_unsigned_integer
5560 (map.cu_table_reordered + i * map.offset_size,
5561 map.offset_size,
5562 map.dwarf5_byte_order));
5563 }
5564 else
5565 sect_off_next = (sect_offset) section.size;
5566 if (i >= 1)
5567 {
5568 const ULONGEST length = sect_off_next - sect_off_prev;
5569 dwarf2_per_cu_data *per_cu
5570 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5571 sect_off_prev, length);
5572 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
5573 }
5574 sect_off_prev = sect_off_next;
5575 }
5576 }
5577
5578 /* Read the CU list from the mapped index, and use it to create all
5579 the CU objects for this dwarf2_per_objfile. */
5580
5581 static void
5582 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5583 const mapped_debug_names &map,
5584 const mapped_debug_names &dwz_map)
5585 {
5586 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5587 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5588
5589 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5590 dwarf2_per_objfile->info,
5591 false /* is_dwz */);
5592
5593 if (dwz_map.cu_count == 0)
5594 return;
5595
5596 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5597 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5598 true /* is_dwz */);
5599 }
5600
5601 /* Read .debug_names. If everything went ok, initialize the "quick"
5602 elements of all the CUs and return true. Otherwise, return false. */
5603
5604 static bool
5605 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5606 {
5607 mapped_debug_names local_map (dwarf2_per_objfile);
5608 mapped_debug_names dwz_map (dwarf2_per_objfile);
5609 struct objfile *objfile = dwarf2_per_objfile->objfile;
5610
5611 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5612 &dwarf2_per_objfile->debug_names,
5613 local_map))
5614 return false;
5615
5616 /* Don't use the index if it's empty. */
5617 if (local_map.name_count == 0)
5618 return false;
5619
5620 /* If there is a .dwz file, read it so we can get its CU list as
5621 well. */
5622 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5623 if (dwz != NULL)
5624 {
5625 if (!read_debug_names_from_section (objfile,
5626 bfd_get_filename (dwz->dwz_bfd),
5627 &dwz->debug_names, dwz_map))
5628 {
5629 warning (_("could not read '.debug_names' section from %s; skipping"),
5630 bfd_get_filename (dwz->dwz_bfd));
5631 return false;
5632 }
5633 }
5634
5635 create_cus_from_debug_names (dwarf2_per_objfile, local_map, dwz_map);
5636
5637 if (local_map.tu_count != 0)
5638 {
5639 /* We can only handle a single .debug_types when we have an
5640 index. */
5641 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
5642 return false;
5643
5644 dwarf2_section_info *section = VEC_index (dwarf2_section_info_def,
5645 dwarf2_per_objfile->types, 0);
5646
5647 create_signatured_type_table_from_debug_names
5648 (dwarf2_per_objfile, local_map, section, &dwarf2_per_objfile->abbrev);
5649 }
5650
5651 create_addrmap_from_aranges (dwarf2_per_objfile,
5652 &dwarf2_per_objfile->debug_aranges);
5653
5654 dwarf2_per_objfile->debug_names_table.reset
5655 (new mapped_debug_names (dwarf2_per_objfile));
5656 *dwarf2_per_objfile->debug_names_table = std::move (local_map);
5657 dwarf2_per_objfile->using_index = 1;
5658 dwarf2_per_objfile->quick_file_names_table =
5659 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5660
5661 return true;
5662 }
5663
5664 /* Type used to manage iterating over all CUs looking for a symbol for
5665 .debug_names. */
5666
5667 class dw2_debug_names_iterator
5668 {
5669 public:
5670 /* If WANT_SPECIFIC_BLOCK is true, only look for symbols in block
5671 BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
5672 dw2_debug_names_iterator (const mapped_debug_names &map,
5673 bool want_specific_block,
5674 block_enum block_index, domain_enum domain,
5675 const char *name)
5676 : m_map (map), m_want_specific_block (want_specific_block),
5677 m_block_index (block_index), m_domain (domain),
5678 m_addr (find_vec_in_debug_names (map, name))
5679 {}
5680
5681 dw2_debug_names_iterator (const mapped_debug_names &map,
5682 search_domain search, uint32_t namei)
5683 : m_map (map),
5684 m_search (search),
5685 m_addr (find_vec_in_debug_names (map, namei))
5686 {}
5687
5688 /* Return the next matching CU or NULL if there are no more. */
5689 dwarf2_per_cu_data *next ();
5690
5691 private:
5692 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5693 const char *name);
5694 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5695 uint32_t namei);
5696
5697 /* The internalized form of .debug_names. */
5698 const mapped_debug_names &m_map;
5699
5700 /* If true, only look for symbols that match BLOCK_INDEX. */
5701 const bool m_want_specific_block = false;
5702
5703 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
5704 Unused if !WANT_SPECIFIC_BLOCK - FIRST_LOCAL_BLOCK is an invalid
5705 value. */
5706 const block_enum m_block_index = FIRST_LOCAL_BLOCK;
5707
5708 /* The kind of symbol we're looking for. */
5709 const domain_enum m_domain = UNDEF_DOMAIN;
5710 const search_domain m_search = ALL_DOMAIN;
5711
5712 /* The list of CUs from the index entry of the symbol, or NULL if
5713 not found. */
5714 const gdb_byte *m_addr;
5715 };
5716
5717 const char *
5718 mapped_debug_names::namei_to_name (uint32_t namei) const
5719 {
5720 const ULONGEST namei_string_offs
5721 = extract_unsigned_integer ((name_table_string_offs_reordered
5722 + namei * offset_size),
5723 offset_size,
5724 dwarf5_byte_order);
5725 return read_indirect_string_at_offset
5726 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
5727 }
5728
5729 /* Find a slot in .debug_names for the object named NAME. If NAME is
5730 found, return pointer to its pool data. If NAME cannot be found,
5731 return NULL. */
5732
5733 const gdb_byte *
5734 dw2_debug_names_iterator::find_vec_in_debug_names
5735 (const mapped_debug_names &map, const char *name)
5736 {
5737 int (*cmp) (const char *, const char *);
5738
5739 if (current_language->la_language == language_cplus
5740 || current_language->la_language == language_fortran
5741 || current_language->la_language == language_d)
5742 {
5743 /* NAME is already canonical. Drop any qualifiers as
5744 .debug_names does not contain any. */
5745
5746 if (strchr (name, '(') != NULL)
5747 {
5748 gdb::unique_xmalloc_ptr<char> without_params
5749 = cp_remove_params (name);
5750
5751 if (without_params != NULL)
5752 {
5753 name = without_params.get();
5754 }
5755 }
5756 }
5757
5758 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5759
5760 const uint32_t full_hash = dwarf5_djb_hash (name);
5761 uint32_t namei
5762 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5763 (map.bucket_table_reordered
5764 + (full_hash % map.bucket_count)), 4,
5765 map.dwarf5_byte_order);
5766 if (namei == 0)
5767 return NULL;
5768 --namei;
5769 if (namei >= map.name_count)
5770 {
5771 complaint (&symfile_complaints,
5772 _("Wrong .debug_names with name index %u but name_count=%u "
5773 "[in module %s]"),
5774 namei, map.name_count,
5775 objfile_name (map.dwarf2_per_objfile->objfile));
5776 return NULL;
5777 }
5778
5779 for (;;)
5780 {
5781 const uint32_t namei_full_hash
5782 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5783 (map.hash_table_reordered + namei), 4,
5784 map.dwarf5_byte_order);
5785 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5786 return NULL;
5787
5788 if (full_hash == namei_full_hash)
5789 {
5790 const char *const namei_string = map.namei_to_name (namei);
5791
5792 #if 0 /* An expensive sanity check. */
5793 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5794 {
5795 complaint (&symfile_complaints,
5796 _("Wrong .debug_names hash for string at index %u "
5797 "[in module %s]"),
5798 namei, objfile_name (dwarf2_per_objfile->objfile));
5799 return NULL;
5800 }
5801 #endif
5802
5803 if (cmp (namei_string, name) == 0)
5804 {
5805 const ULONGEST namei_entry_offs
5806 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5807 + namei * map.offset_size),
5808 map.offset_size, map.dwarf5_byte_order);
5809 return map.entry_pool + namei_entry_offs;
5810 }
5811 }
5812
5813 ++namei;
5814 if (namei >= map.name_count)
5815 return NULL;
5816 }
5817 }
5818
5819 const gdb_byte *
5820 dw2_debug_names_iterator::find_vec_in_debug_names
5821 (const mapped_debug_names &map, uint32_t namei)
5822 {
5823 if (namei >= map.name_count)
5824 {
5825 complaint (&symfile_complaints,
5826 _("Wrong .debug_names with name index %u but name_count=%u "
5827 "[in module %s]"),
5828 namei, map.name_count,
5829 objfile_name (map.dwarf2_per_objfile->objfile));
5830 return NULL;
5831 }
5832
5833 const ULONGEST namei_entry_offs
5834 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5835 + namei * map.offset_size),
5836 map.offset_size, map.dwarf5_byte_order);
5837 return map.entry_pool + namei_entry_offs;
5838 }
5839
5840 /* See dw2_debug_names_iterator. */
5841
5842 dwarf2_per_cu_data *
5843 dw2_debug_names_iterator::next ()
5844 {
5845 if (m_addr == NULL)
5846 return NULL;
5847
5848 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5849 struct objfile *objfile = dwarf2_per_objfile->objfile;
5850 bfd *const abfd = objfile->obfd;
5851
5852 again:
5853
5854 unsigned int bytes_read;
5855 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5856 m_addr += bytes_read;
5857 if (abbrev == 0)
5858 return NULL;
5859
5860 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5861 if (indexval_it == m_map.abbrev_map.cend ())
5862 {
5863 complaint (&symfile_complaints,
5864 _("Wrong .debug_names undefined abbrev code %s "
5865 "[in module %s]"),
5866 pulongest (abbrev), objfile_name (objfile));
5867 return NULL;
5868 }
5869 const mapped_debug_names::index_val &indexval = indexval_it->second;
5870 bool have_is_static = false;
5871 bool is_static;
5872 dwarf2_per_cu_data *per_cu = NULL;
5873 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5874 {
5875 ULONGEST ull;
5876 switch (attr.form)
5877 {
5878 case DW_FORM_implicit_const:
5879 ull = attr.implicit_const;
5880 break;
5881 case DW_FORM_flag_present:
5882 ull = 1;
5883 break;
5884 case DW_FORM_udata:
5885 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5886 m_addr += bytes_read;
5887 break;
5888 default:
5889 complaint (&symfile_complaints,
5890 _("Unsupported .debug_names form %s [in module %s]"),
5891 dwarf_form_name (attr.form),
5892 objfile_name (objfile));
5893 return NULL;
5894 }
5895 switch (attr.dw_idx)
5896 {
5897 case DW_IDX_compile_unit:
5898 /* Don't crash on bad data. */
5899 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5900 {
5901 complaint (&symfile_complaints,
5902 _(".debug_names entry has bad CU index %s"
5903 " [in module %s]"),
5904 pulongest (ull),
5905 objfile_name (dwarf2_per_objfile->objfile));
5906 continue;
5907 }
5908 per_cu = dwarf2_per_objfile->get_cutu (ull);
5909 break;
5910 case DW_IDX_type_unit:
5911 /* Don't crash on bad data. */
5912 if (ull >= dwarf2_per_objfile->all_type_units.size ())
5913 {
5914 complaint (&symfile_complaints,
5915 _(".debug_names entry has bad TU index %s"
5916 " [in module %s]"),
5917 pulongest (ull),
5918 objfile_name (dwarf2_per_objfile->objfile));
5919 continue;
5920 }
5921 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5922 break;
5923 case DW_IDX_GNU_internal:
5924 if (!m_map.augmentation_is_gdb)
5925 break;
5926 have_is_static = true;
5927 is_static = true;
5928 break;
5929 case DW_IDX_GNU_external:
5930 if (!m_map.augmentation_is_gdb)
5931 break;
5932 have_is_static = true;
5933 is_static = false;
5934 break;
5935 }
5936 }
5937
5938 /* Skip if already read in. */
5939 if (per_cu->v.quick->compunit_symtab)
5940 goto again;
5941
5942 /* Check static vs global. */
5943 if (have_is_static)
5944 {
5945 const bool want_static = m_block_index != GLOBAL_BLOCK;
5946 if (m_want_specific_block && want_static != is_static)
5947 goto again;
5948 }
5949
5950 /* Match dw2_symtab_iter_next, symbol_kind
5951 and debug_names::psymbol_tag. */
5952 switch (m_domain)
5953 {
5954 case VAR_DOMAIN:
5955 switch (indexval.dwarf_tag)
5956 {
5957 case DW_TAG_variable:
5958 case DW_TAG_subprogram:
5959 /* Some types are also in VAR_DOMAIN. */
5960 case DW_TAG_typedef:
5961 case DW_TAG_structure_type:
5962 break;
5963 default:
5964 goto again;
5965 }
5966 break;
5967 case STRUCT_DOMAIN:
5968 switch (indexval.dwarf_tag)
5969 {
5970 case DW_TAG_typedef:
5971 case DW_TAG_structure_type:
5972 break;
5973 default:
5974 goto again;
5975 }
5976 break;
5977 case LABEL_DOMAIN:
5978 switch (indexval.dwarf_tag)
5979 {
5980 case 0:
5981 case DW_TAG_variable:
5982 break;
5983 default:
5984 goto again;
5985 }
5986 break;
5987 default:
5988 break;
5989 }
5990
5991 /* Match dw2_expand_symtabs_matching, symbol_kind and
5992 debug_names::psymbol_tag. */
5993 switch (m_search)
5994 {
5995 case VARIABLES_DOMAIN:
5996 switch (indexval.dwarf_tag)
5997 {
5998 case DW_TAG_variable:
5999 break;
6000 default:
6001 goto again;
6002 }
6003 break;
6004 case FUNCTIONS_DOMAIN:
6005 switch (indexval.dwarf_tag)
6006 {
6007 case DW_TAG_subprogram:
6008 break;
6009 default:
6010 goto again;
6011 }
6012 break;
6013 case TYPES_DOMAIN:
6014 switch (indexval.dwarf_tag)
6015 {
6016 case DW_TAG_typedef:
6017 case DW_TAG_structure_type:
6018 break;
6019 default:
6020 goto again;
6021 }
6022 break;
6023 default:
6024 break;
6025 }
6026
6027 return per_cu;
6028 }
6029
6030 static struct compunit_symtab *
6031 dw2_debug_names_lookup_symbol (struct objfile *objfile, int block_index_int,
6032 const char *name, domain_enum domain)
6033 {
6034 const block_enum block_index = static_cast<block_enum> (block_index_int);
6035 struct dwarf2_per_objfile *dwarf2_per_objfile
6036 = get_dwarf2_per_objfile (objfile);
6037
6038 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6039 if (!mapp)
6040 {
6041 /* index is NULL if OBJF_READNOW. */
6042 return NULL;
6043 }
6044 const auto &map = *mapp;
6045
6046 dw2_debug_names_iterator iter (map, true /* want_specific_block */,
6047 block_index, domain, name);
6048
6049 struct compunit_symtab *stab_best = NULL;
6050 struct dwarf2_per_cu_data *per_cu;
6051 while ((per_cu = iter.next ()) != NULL)
6052 {
6053 struct symbol *sym, *with_opaque = NULL;
6054 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
6055 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6056 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
6057
6058 sym = block_find_symbol (block, name, domain,
6059 block_find_non_opaque_type_preferred,
6060 &with_opaque);
6061
6062 /* Some caution must be observed with overloaded functions and
6063 methods, since the index will not contain any overload
6064 information (but NAME might contain it). */
6065
6066 if (sym != NULL
6067 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
6068 return stab;
6069 if (with_opaque != NULL
6070 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
6071 stab_best = stab;
6072
6073 /* Keep looking through other CUs. */
6074 }
6075
6076 return stab_best;
6077 }
6078
6079 /* This dumps minimal information about .debug_names. It is called
6080 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6081 uses this to verify that .debug_names has been loaded. */
6082
6083 static void
6084 dw2_debug_names_dump (struct objfile *objfile)
6085 {
6086 struct dwarf2_per_objfile *dwarf2_per_objfile
6087 = get_dwarf2_per_objfile (objfile);
6088
6089 gdb_assert (dwarf2_per_objfile->using_index);
6090 printf_filtered (".debug_names:");
6091 if (dwarf2_per_objfile->debug_names_table)
6092 printf_filtered (" exists\n");
6093 else
6094 printf_filtered (" faked for \"readnow\"\n");
6095 printf_filtered ("\n");
6096 }
6097
6098 static void
6099 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6100 const char *func_name)
6101 {
6102 struct dwarf2_per_objfile *dwarf2_per_objfile
6103 = get_dwarf2_per_objfile (objfile);
6104
6105 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6106 if (dwarf2_per_objfile->debug_names_table)
6107 {
6108 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6109
6110 /* Note: It doesn't matter what we pass for block_index here. */
6111 dw2_debug_names_iterator iter (map, false /* want_specific_block */,
6112 GLOBAL_BLOCK, VAR_DOMAIN, func_name);
6113
6114 struct dwarf2_per_cu_data *per_cu;
6115 while ((per_cu = iter.next ()) != NULL)
6116 dw2_instantiate_symtab (per_cu, false);
6117 }
6118 }
6119
6120 static void
6121 dw2_debug_names_expand_symtabs_matching
6122 (struct objfile *objfile,
6123 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6124 const lookup_name_info &lookup_name,
6125 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6126 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6127 enum search_domain kind)
6128 {
6129 struct dwarf2_per_objfile *dwarf2_per_objfile
6130 = get_dwarf2_per_objfile (objfile);
6131
6132 /* debug_names_table is NULL if OBJF_READNOW. */
6133 if (!dwarf2_per_objfile->debug_names_table)
6134 return;
6135
6136 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
6137
6138 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6139
6140 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6141 symbol_matcher,
6142 kind, [&] (offset_type namei)
6143 {
6144 /* The name was matched, now expand corresponding CUs that were
6145 marked. */
6146 dw2_debug_names_iterator iter (map, kind, namei);
6147
6148 struct dwarf2_per_cu_data *per_cu;
6149 while ((per_cu = iter.next ()) != NULL)
6150 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6151 expansion_notify);
6152 });
6153 }
6154
6155 const struct quick_symbol_functions dwarf2_debug_names_functions =
6156 {
6157 dw2_has_symbols,
6158 dw2_find_last_source_symtab,
6159 dw2_forget_cached_source_info,
6160 dw2_map_symtabs_matching_filename,
6161 dw2_debug_names_lookup_symbol,
6162 dw2_print_stats,
6163 dw2_debug_names_dump,
6164 dw2_relocate,
6165 dw2_debug_names_expand_symtabs_for_function,
6166 dw2_expand_all_symtabs,
6167 dw2_expand_symtabs_with_fullname,
6168 dw2_map_matching_symbols,
6169 dw2_debug_names_expand_symtabs_matching,
6170 dw2_find_pc_sect_compunit_symtab,
6171 NULL,
6172 dw2_map_symbol_filenames
6173 };
6174
6175 /* See symfile.h. */
6176
6177 bool
6178 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
6179 {
6180 struct dwarf2_per_objfile *dwarf2_per_objfile
6181 = get_dwarf2_per_objfile (objfile);
6182
6183 /* If we're about to read full symbols, don't bother with the
6184 indices. In this case we also don't care if some other debug
6185 format is making psymtabs, because they are all about to be
6186 expanded anyway. */
6187 if ((objfile->flags & OBJF_READNOW))
6188 {
6189 dwarf2_per_objfile->using_index = 1;
6190 create_all_comp_units (dwarf2_per_objfile);
6191 create_all_type_units (dwarf2_per_objfile);
6192 dwarf2_per_objfile->quick_file_names_table
6193 = create_quick_file_names_table
6194 (dwarf2_per_objfile->all_comp_units.size ());
6195
6196 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
6197 + dwarf2_per_objfile->all_type_units.size ()); ++i)
6198 {
6199 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
6200
6201 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6202 struct dwarf2_per_cu_quick_data);
6203 }
6204
6205 /* Return 1 so that gdb sees the "quick" functions. However,
6206 these functions will be no-ops because we will have expanded
6207 all symtabs. */
6208 *index_kind = dw_index_kind::GDB_INDEX;
6209 return true;
6210 }
6211
6212 if (dwarf2_read_debug_names (dwarf2_per_objfile))
6213 {
6214 *index_kind = dw_index_kind::DEBUG_NAMES;
6215 return true;
6216 }
6217
6218 if (dwarf2_read_index (dwarf2_per_objfile))
6219 {
6220 *index_kind = dw_index_kind::GDB_INDEX;
6221 return true;
6222 }
6223
6224 return false;
6225 }
6226
6227 \f
6228
6229 /* Build a partial symbol table. */
6230
6231 void
6232 dwarf2_build_psymtabs (struct objfile *objfile)
6233 {
6234 struct dwarf2_per_objfile *dwarf2_per_objfile
6235 = get_dwarf2_per_objfile (objfile);
6236
6237 if (objfile->global_psymbols.capacity () == 0
6238 && objfile->static_psymbols.capacity () == 0)
6239 init_psymbol_list (objfile, 1024);
6240
6241 TRY
6242 {
6243 /* This isn't really ideal: all the data we allocate on the
6244 objfile's obstack is still uselessly kept around. However,
6245 freeing it seems unsafe. */
6246 psymtab_discarder psymtabs (objfile);
6247 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6248 psymtabs.keep ();
6249 }
6250 CATCH (except, RETURN_MASK_ERROR)
6251 {
6252 exception_print (gdb_stderr, except);
6253 }
6254 END_CATCH
6255 }
6256
6257 /* Return the total length of the CU described by HEADER. */
6258
6259 static unsigned int
6260 get_cu_length (const struct comp_unit_head *header)
6261 {
6262 return header->initial_length_size + header->length;
6263 }
6264
6265 /* Return TRUE if SECT_OFF is within CU_HEADER. */
6266
6267 static inline bool
6268 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
6269 {
6270 sect_offset bottom = cu_header->sect_off;
6271 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
6272
6273 return sect_off >= bottom && sect_off < top;
6274 }
6275
6276 /* Find the base address of the compilation unit for range lists and
6277 location lists. It will normally be specified by DW_AT_low_pc.
6278 In DWARF-3 draft 4, the base address could be overridden by
6279 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6280 compilation units with discontinuous ranges. */
6281
6282 static void
6283 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6284 {
6285 struct attribute *attr;
6286
6287 cu->base_known = 0;
6288 cu->base_address = 0;
6289
6290 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6291 if (attr)
6292 {
6293 cu->base_address = attr_value_as_address (attr);
6294 cu->base_known = 1;
6295 }
6296 else
6297 {
6298 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6299 if (attr)
6300 {
6301 cu->base_address = attr_value_as_address (attr);
6302 cu->base_known = 1;
6303 }
6304 }
6305 }
6306
6307 /* Read in the comp unit header information from the debug_info at info_ptr.
6308 Use rcuh_kind::COMPILE as the default type if not known by the caller.
6309 NOTE: This leaves members offset, first_die_offset to be filled in
6310 by the caller. */
6311
6312 static const gdb_byte *
6313 read_comp_unit_head (struct comp_unit_head *cu_header,
6314 const gdb_byte *info_ptr,
6315 struct dwarf2_section_info *section,
6316 rcuh_kind section_kind)
6317 {
6318 int signed_addr;
6319 unsigned int bytes_read;
6320 const char *filename = get_section_file_name (section);
6321 bfd *abfd = get_section_bfd_owner (section);
6322
6323 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6324 cu_header->initial_length_size = bytes_read;
6325 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
6326 info_ptr += bytes_read;
6327 cu_header->version = read_2_bytes (abfd, info_ptr);
6328 info_ptr += 2;
6329 if (cu_header->version < 5)
6330 switch (section_kind)
6331 {
6332 case rcuh_kind::COMPILE:
6333 cu_header->unit_type = DW_UT_compile;
6334 break;
6335 case rcuh_kind::TYPE:
6336 cu_header->unit_type = DW_UT_type;
6337 break;
6338 default:
6339 internal_error (__FILE__, __LINE__,
6340 _("read_comp_unit_head: invalid section_kind"));
6341 }
6342 else
6343 {
6344 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6345 (read_1_byte (abfd, info_ptr));
6346 info_ptr += 1;
6347 switch (cu_header->unit_type)
6348 {
6349 case DW_UT_compile:
6350 if (section_kind != rcuh_kind::COMPILE)
6351 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6352 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
6353 filename);
6354 break;
6355 case DW_UT_type:
6356 section_kind = rcuh_kind::TYPE;
6357 break;
6358 default:
6359 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6360 "(is %d, should be %d or %d) [in module %s]"),
6361 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
6362 }
6363
6364 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6365 info_ptr += 1;
6366 }
6367 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6368 cu_header,
6369 &bytes_read);
6370 info_ptr += bytes_read;
6371 if (cu_header->version < 5)
6372 {
6373 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6374 info_ptr += 1;
6375 }
6376 signed_addr = bfd_get_sign_extend_vma (abfd);
6377 if (signed_addr < 0)
6378 internal_error (__FILE__, __LINE__,
6379 _("read_comp_unit_head: dwarf from non elf file"));
6380 cu_header->signed_addr_p = signed_addr;
6381
6382 if (section_kind == rcuh_kind::TYPE)
6383 {
6384 LONGEST type_offset;
6385
6386 cu_header->signature = read_8_bytes (abfd, info_ptr);
6387 info_ptr += 8;
6388
6389 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6390 info_ptr += bytes_read;
6391 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6392 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
6393 error (_("Dwarf Error: Too big type_offset in compilation unit "
6394 "header (is %s) [in module %s]"), plongest (type_offset),
6395 filename);
6396 }
6397
6398 return info_ptr;
6399 }
6400
6401 /* Helper function that returns the proper abbrev section for
6402 THIS_CU. */
6403
6404 static struct dwarf2_section_info *
6405 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6406 {
6407 struct dwarf2_section_info *abbrev;
6408 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6409
6410 if (this_cu->is_dwz)
6411 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
6412 else
6413 abbrev = &dwarf2_per_objfile->abbrev;
6414
6415 return abbrev;
6416 }
6417
6418 /* Subroutine of read_and_check_comp_unit_head and
6419 read_and_check_type_unit_head to simplify them.
6420 Perform various error checking on the header. */
6421
6422 static void
6423 error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6424 struct comp_unit_head *header,
6425 struct dwarf2_section_info *section,
6426 struct dwarf2_section_info *abbrev_section)
6427 {
6428 const char *filename = get_section_file_name (section);
6429
6430 if (header->version < 2 || header->version > 5)
6431 error (_("Dwarf Error: wrong version in compilation unit header "
6432 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
6433 filename);
6434
6435 if (to_underlying (header->abbrev_sect_off)
6436 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
6437 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6438 "(offset %s + 6) [in module %s]"),
6439 sect_offset_str (header->abbrev_sect_off),
6440 sect_offset_str (header->sect_off),
6441 filename);
6442
6443 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
6444 avoid potential 32-bit overflow. */
6445 if (((ULONGEST) header->sect_off + get_cu_length (header))
6446 > section->size)
6447 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
6448 "(offset %s + 0) [in module %s]"),
6449 header->length, sect_offset_str (header->sect_off),
6450 filename);
6451 }
6452
6453 /* Read in a CU/TU header and perform some basic error checking.
6454 The contents of the header are stored in HEADER.
6455 The result is a pointer to the start of the first DIE. */
6456
6457 static const gdb_byte *
6458 read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6459 struct comp_unit_head *header,
6460 struct dwarf2_section_info *section,
6461 struct dwarf2_section_info *abbrev_section,
6462 const gdb_byte *info_ptr,
6463 rcuh_kind section_kind)
6464 {
6465 const gdb_byte *beg_of_comp_unit = info_ptr;
6466
6467 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
6468
6469 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
6470
6471 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
6472
6473 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6474 abbrev_section);
6475
6476 return info_ptr;
6477 }
6478
6479 /* Fetch the abbreviation table offset from a comp or type unit header. */
6480
6481 static sect_offset
6482 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6483 struct dwarf2_section_info *section,
6484 sect_offset sect_off)
6485 {
6486 bfd *abfd = get_section_bfd_owner (section);
6487 const gdb_byte *info_ptr;
6488 unsigned int initial_length_size, offset_size;
6489 uint16_t version;
6490
6491 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
6492 info_ptr = section->buffer + to_underlying (sect_off);
6493 read_initial_length (abfd, info_ptr, &initial_length_size);
6494 offset_size = initial_length_size == 4 ? 4 : 8;
6495 info_ptr += initial_length_size;
6496
6497 version = read_2_bytes (abfd, info_ptr);
6498 info_ptr += 2;
6499 if (version >= 5)
6500 {
6501 /* Skip unit type and address size. */
6502 info_ptr += 2;
6503 }
6504
6505 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
6506 }
6507
6508 /* Allocate a new partial symtab for file named NAME and mark this new
6509 partial symtab as being an include of PST. */
6510
6511 static void
6512 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
6513 struct objfile *objfile)
6514 {
6515 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6516
6517 if (!IS_ABSOLUTE_PATH (subpst->filename))
6518 {
6519 /* It shares objfile->objfile_obstack. */
6520 subpst->dirname = pst->dirname;
6521 }
6522
6523 subpst->textlow = 0;
6524 subpst->texthigh = 0;
6525
6526 subpst->dependencies
6527 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
6528 subpst->dependencies[0] = pst;
6529 subpst->number_of_dependencies = 1;
6530
6531 subpst->globals_offset = 0;
6532 subpst->n_global_syms = 0;
6533 subpst->statics_offset = 0;
6534 subpst->n_static_syms = 0;
6535 subpst->compunit_symtab = NULL;
6536 subpst->read_symtab = pst->read_symtab;
6537 subpst->readin = 0;
6538
6539 /* No private part is necessary for include psymtabs. This property
6540 can be used to differentiate between such include psymtabs and
6541 the regular ones. */
6542 subpst->read_symtab_private = NULL;
6543 }
6544
6545 /* Read the Line Number Program data and extract the list of files
6546 included by the source file represented by PST. Build an include
6547 partial symtab for each of these included files. */
6548
6549 static void
6550 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6551 struct die_info *die,
6552 struct partial_symtab *pst)
6553 {
6554 line_header_up lh;
6555 struct attribute *attr;
6556
6557 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6558 if (attr)
6559 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6560 if (lh == NULL)
6561 return; /* No linetable, so no includes. */
6562
6563 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
6564 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
6565 }
6566
6567 static hashval_t
6568 hash_signatured_type (const void *item)
6569 {
6570 const struct signatured_type *sig_type
6571 = (const struct signatured_type *) item;
6572
6573 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6574 return sig_type->signature;
6575 }
6576
6577 static int
6578 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6579 {
6580 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6581 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6582
6583 return lhs->signature == rhs->signature;
6584 }
6585
6586 /* Allocate a hash table for signatured types. */
6587
6588 static htab_t
6589 allocate_signatured_type_table (struct objfile *objfile)
6590 {
6591 return htab_create_alloc_ex (41,
6592 hash_signatured_type,
6593 eq_signatured_type,
6594 NULL,
6595 &objfile->objfile_obstack,
6596 hashtab_obstack_allocate,
6597 dummy_obstack_deallocate);
6598 }
6599
6600 /* A helper function to add a signatured type CU to a table. */
6601
6602 static int
6603 add_signatured_type_cu_to_table (void **slot, void *datum)
6604 {
6605 struct signatured_type *sigt = (struct signatured_type *) *slot;
6606 std::vector<signatured_type *> *all_type_units
6607 = (std::vector<signatured_type *> *) datum;
6608
6609 all_type_units->push_back (sigt);
6610
6611 return 1;
6612 }
6613
6614 /* A helper for create_debug_types_hash_table. Read types from SECTION
6615 and fill them into TYPES_HTAB. It will process only type units,
6616 therefore DW_UT_type. */
6617
6618 static void
6619 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6620 struct dwo_file *dwo_file,
6621 dwarf2_section_info *section, htab_t &types_htab,
6622 rcuh_kind section_kind)
6623 {
6624 struct objfile *objfile = dwarf2_per_objfile->objfile;
6625 struct dwarf2_section_info *abbrev_section;
6626 bfd *abfd;
6627 const gdb_byte *info_ptr, *end_ptr;
6628
6629 abbrev_section = (dwo_file != NULL
6630 ? &dwo_file->sections.abbrev
6631 : &dwarf2_per_objfile->abbrev);
6632
6633 if (dwarf_read_debug)
6634 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6635 get_section_name (section),
6636 get_section_file_name (abbrev_section));
6637
6638 dwarf2_read_section (objfile, section);
6639 info_ptr = section->buffer;
6640
6641 if (info_ptr == NULL)
6642 return;
6643
6644 /* We can't set abfd until now because the section may be empty or
6645 not present, in which case the bfd is unknown. */
6646 abfd = get_section_bfd_owner (section);
6647
6648 /* We don't use init_cutu_and_read_dies_simple, or some such, here
6649 because we don't need to read any dies: the signature is in the
6650 header. */
6651
6652 end_ptr = info_ptr + section->size;
6653 while (info_ptr < end_ptr)
6654 {
6655 struct signatured_type *sig_type;
6656 struct dwo_unit *dwo_tu;
6657 void **slot;
6658 const gdb_byte *ptr = info_ptr;
6659 struct comp_unit_head header;
6660 unsigned int length;
6661
6662 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6663
6664 /* Initialize it due to a false compiler warning. */
6665 header.signature = -1;
6666 header.type_cu_offset_in_tu = (cu_offset) -1;
6667
6668 /* We need to read the type's signature in order to build the hash
6669 table, but we don't need anything else just yet. */
6670
6671 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6672 abbrev_section, ptr, section_kind);
6673
6674 length = get_cu_length (&header);
6675
6676 /* Skip dummy type units. */
6677 if (ptr >= info_ptr + length
6678 || peek_abbrev_code (abfd, ptr) == 0
6679 || header.unit_type != DW_UT_type)
6680 {
6681 info_ptr += length;
6682 continue;
6683 }
6684
6685 if (types_htab == NULL)
6686 {
6687 if (dwo_file)
6688 types_htab = allocate_dwo_unit_table (objfile);
6689 else
6690 types_htab = allocate_signatured_type_table (objfile);
6691 }
6692
6693 if (dwo_file)
6694 {
6695 sig_type = NULL;
6696 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6697 struct dwo_unit);
6698 dwo_tu->dwo_file = dwo_file;
6699 dwo_tu->signature = header.signature;
6700 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6701 dwo_tu->section = section;
6702 dwo_tu->sect_off = sect_off;
6703 dwo_tu->length = length;
6704 }
6705 else
6706 {
6707 /* N.B.: type_offset is not usable if this type uses a DWO file.
6708 The real type_offset is in the DWO file. */
6709 dwo_tu = NULL;
6710 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6711 struct signatured_type);
6712 sig_type->signature = header.signature;
6713 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6714 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6715 sig_type->per_cu.is_debug_types = 1;
6716 sig_type->per_cu.section = section;
6717 sig_type->per_cu.sect_off = sect_off;
6718 sig_type->per_cu.length = length;
6719 }
6720
6721 slot = htab_find_slot (types_htab,
6722 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6723 INSERT);
6724 gdb_assert (slot != NULL);
6725 if (*slot != NULL)
6726 {
6727 sect_offset dup_sect_off;
6728
6729 if (dwo_file)
6730 {
6731 const struct dwo_unit *dup_tu
6732 = (const struct dwo_unit *) *slot;
6733
6734 dup_sect_off = dup_tu->sect_off;
6735 }
6736 else
6737 {
6738 const struct signatured_type *dup_tu
6739 = (const struct signatured_type *) *slot;
6740
6741 dup_sect_off = dup_tu->per_cu.sect_off;
6742 }
6743
6744 complaint (&symfile_complaints,
6745 _("debug type entry at offset %s is duplicate to"
6746 " the entry at offset %s, signature %s"),
6747 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6748 hex_string (header.signature));
6749 }
6750 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6751
6752 if (dwarf_read_debug > 1)
6753 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6754 sect_offset_str (sect_off),
6755 hex_string (header.signature));
6756
6757 info_ptr += length;
6758 }
6759 }
6760
6761 /* Create the hash table of all entries in the .debug_types
6762 (or .debug_types.dwo) section(s).
6763 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6764 otherwise it is NULL.
6765
6766 The result is a pointer to the hash table or NULL if there are no types.
6767
6768 Note: This function processes DWO files only, not DWP files. */
6769
6770 static void
6771 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6772 struct dwo_file *dwo_file,
6773 VEC (dwarf2_section_info_def) *types,
6774 htab_t &types_htab)
6775 {
6776 int ix;
6777 struct dwarf2_section_info *section;
6778
6779 if (VEC_empty (dwarf2_section_info_def, types))
6780 return;
6781
6782 for (ix = 0;
6783 VEC_iterate (dwarf2_section_info_def, types, ix, section);
6784 ++ix)
6785 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, section,
6786 types_htab, rcuh_kind::TYPE);
6787 }
6788
6789 /* Create the hash table of all entries in the .debug_types section,
6790 and initialize all_type_units.
6791 The result is zero if there is an error (e.g. missing .debug_types section),
6792 otherwise non-zero. */
6793
6794 static int
6795 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6796 {
6797 htab_t types_htab = NULL;
6798
6799 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6800 &dwarf2_per_objfile->info, types_htab,
6801 rcuh_kind::COMPILE);
6802 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6803 dwarf2_per_objfile->types, types_htab);
6804 if (types_htab == NULL)
6805 {
6806 dwarf2_per_objfile->signatured_types = NULL;
6807 return 0;
6808 }
6809
6810 dwarf2_per_objfile->signatured_types = types_htab;
6811
6812 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6813 dwarf2_per_objfile->all_type_units.reserve (htab_elements (types_htab));
6814
6815 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table,
6816 &dwarf2_per_objfile->all_type_units);
6817
6818 return 1;
6819 }
6820
6821 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6822 If SLOT is non-NULL, it is the entry to use in the hash table.
6823 Otherwise we find one. */
6824
6825 static struct signatured_type *
6826 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6827 void **slot)
6828 {
6829 struct objfile *objfile = dwarf2_per_objfile->objfile;
6830
6831 if (dwarf2_per_objfile->all_type_units.size ()
6832 == dwarf2_per_objfile->all_type_units.capacity ())
6833 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6834
6835 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6836 struct signatured_type);
6837
6838 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6839 sig_type->signature = sig;
6840 sig_type->per_cu.is_debug_types = 1;
6841 if (dwarf2_per_objfile->using_index)
6842 {
6843 sig_type->per_cu.v.quick =
6844 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6845 struct dwarf2_per_cu_quick_data);
6846 }
6847
6848 if (slot == NULL)
6849 {
6850 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6851 sig_type, INSERT);
6852 }
6853 gdb_assert (*slot == NULL);
6854 *slot = sig_type;
6855 /* The rest of sig_type must be filled in by the caller. */
6856 return sig_type;
6857 }
6858
6859 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6860 Fill in SIG_ENTRY with DWO_ENTRY. */
6861
6862 static void
6863 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6864 struct signatured_type *sig_entry,
6865 struct dwo_unit *dwo_entry)
6866 {
6867 /* Make sure we're not clobbering something we don't expect to. */
6868 gdb_assert (! sig_entry->per_cu.queued);
6869 gdb_assert (sig_entry->per_cu.cu == NULL);
6870 if (dwarf2_per_objfile->using_index)
6871 {
6872 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6873 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6874 }
6875 else
6876 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6877 gdb_assert (sig_entry->signature == dwo_entry->signature);
6878 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6879 gdb_assert (sig_entry->type_unit_group == NULL);
6880 gdb_assert (sig_entry->dwo_unit == NULL);
6881
6882 sig_entry->per_cu.section = dwo_entry->section;
6883 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6884 sig_entry->per_cu.length = dwo_entry->length;
6885 sig_entry->per_cu.reading_dwo_directly = 1;
6886 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6887 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6888 sig_entry->dwo_unit = dwo_entry;
6889 }
6890
6891 /* Subroutine of lookup_signatured_type.
6892 If we haven't read the TU yet, create the signatured_type data structure
6893 for a TU to be read in directly from a DWO file, bypassing the stub.
6894 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6895 using .gdb_index, then when reading a CU we want to stay in the DWO file
6896 containing that CU. Otherwise we could end up reading several other DWO
6897 files (due to comdat folding) to process the transitive closure of all the
6898 mentioned TUs, and that can be slow. The current DWO file will have every
6899 type signature that it needs.
6900 We only do this for .gdb_index because in the psymtab case we already have
6901 to read all the DWOs to build the type unit groups. */
6902
6903 static struct signatured_type *
6904 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6905 {
6906 struct dwarf2_per_objfile *dwarf2_per_objfile
6907 = cu->per_cu->dwarf2_per_objfile;
6908 struct objfile *objfile = dwarf2_per_objfile->objfile;
6909 struct dwo_file *dwo_file;
6910 struct dwo_unit find_dwo_entry, *dwo_entry;
6911 struct signatured_type find_sig_entry, *sig_entry;
6912 void **slot;
6913
6914 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6915
6916 /* If TU skeletons have been removed then we may not have read in any
6917 TUs yet. */
6918 if (dwarf2_per_objfile->signatured_types == NULL)
6919 {
6920 dwarf2_per_objfile->signatured_types
6921 = allocate_signatured_type_table (objfile);
6922 }
6923
6924 /* We only ever need to read in one copy of a signatured type.
6925 Use the global signatured_types array to do our own comdat-folding
6926 of types. If this is the first time we're reading this TU, and
6927 the TU has an entry in .gdb_index, replace the recorded data from
6928 .gdb_index with this TU. */
6929
6930 find_sig_entry.signature = sig;
6931 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6932 &find_sig_entry, INSERT);
6933 sig_entry = (struct signatured_type *) *slot;
6934
6935 /* We can get here with the TU already read, *or* in the process of being
6936 read. Don't reassign the global entry to point to this DWO if that's
6937 the case. Also note that if the TU is already being read, it may not
6938 have come from a DWO, the program may be a mix of Fission-compiled
6939 code and non-Fission-compiled code. */
6940
6941 /* Have we already tried to read this TU?
6942 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6943 needn't exist in the global table yet). */
6944 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6945 return sig_entry;
6946
6947 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6948 dwo_unit of the TU itself. */
6949 dwo_file = cu->dwo_unit->dwo_file;
6950
6951 /* Ok, this is the first time we're reading this TU. */
6952 if (dwo_file->tus == NULL)
6953 return NULL;
6954 find_dwo_entry.signature = sig;
6955 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
6956 if (dwo_entry == NULL)
6957 return NULL;
6958
6959 /* If the global table doesn't have an entry for this TU, add one. */
6960 if (sig_entry == NULL)
6961 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6962
6963 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6964 sig_entry->per_cu.tu_read = 1;
6965 return sig_entry;
6966 }
6967
6968 /* Subroutine of lookup_signatured_type.
6969 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6970 then try the DWP file. If the TU stub (skeleton) has been removed then
6971 it won't be in .gdb_index. */
6972
6973 static struct signatured_type *
6974 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6975 {
6976 struct dwarf2_per_objfile *dwarf2_per_objfile
6977 = cu->per_cu->dwarf2_per_objfile;
6978 struct objfile *objfile = dwarf2_per_objfile->objfile;
6979 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
6980 struct dwo_unit *dwo_entry;
6981 struct signatured_type find_sig_entry, *sig_entry;
6982 void **slot;
6983
6984 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6985 gdb_assert (dwp_file != NULL);
6986
6987 /* If TU skeletons have been removed then we may not have read in any
6988 TUs yet. */
6989 if (dwarf2_per_objfile->signatured_types == NULL)
6990 {
6991 dwarf2_per_objfile->signatured_types
6992 = allocate_signatured_type_table (objfile);
6993 }
6994
6995 find_sig_entry.signature = sig;
6996 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6997 &find_sig_entry, INSERT);
6998 sig_entry = (struct signatured_type *) *slot;
6999
7000 /* Have we already tried to read this TU?
7001 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7002 needn't exist in the global table yet). */
7003 if (sig_entry != NULL)
7004 return sig_entry;
7005
7006 if (dwp_file->tus == NULL)
7007 return NULL;
7008 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
7009 sig, 1 /* is_debug_types */);
7010 if (dwo_entry == NULL)
7011 return NULL;
7012
7013 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7014 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7015
7016 return sig_entry;
7017 }
7018
7019 /* Lookup a signature based type for DW_FORM_ref_sig8.
7020 Returns NULL if signature SIG is not present in the table.
7021 It is up to the caller to complain about this. */
7022
7023 static struct signatured_type *
7024 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7025 {
7026 struct dwarf2_per_objfile *dwarf2_per_objfile
7027 = cu->per_cu->dwarf2_per_objfile;
7028
7029 if (cu->dwo_unit
7030 && dwarf2_per_objfile->using_index)
7031 {
7032 /* We're in a DWO/DWP file, and we're using .gdb_index.
7033 These cases require special processing. */
7034 if (get_dwp_file (dwarf2_per_objfile) == NULL)
7035 return lookup_dwo_signatured_type (cu, sig);
7036 else
7037 return lookup_dwp_signatured_type (cu, sig);
7038 }
7039 else
7040 {
7041 struct signatured_type find_entry, *entry;
7042
7043 if (dwarf2_per_objfile->signatured_types == NULL)
7044 return NULL;
7045 find_entry.signature = sig;
7046 entry = ((struct signatured_type *)
7047 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
7048 return entry;
7049 }
7050 }
7051 \f
7052 /* Low level DIE reading support. */
7053
7054 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7055
7056 static void
7057 init_cu_die_reader (struct die_reader_specs *reader,
7058 struct dwarf2_cu *cu,
7059 struct dwarf2_section_info *section,
7060 struct dwo_file *dwo_file,
7061 struct abbrev_table *abbrev_table)
7062 {
7063 gdb_assert (section->readin && section->buffer != NULL);
7064 reader->abfd = get_section_bfd_owner (section);
7065 reader->cu = cu;
7066 reader->dwo_file = dwo_file;
7067 reader->die_section = section;
7068 reader->buffer = section->buffer;
7069 reader->buffer_end = section->buffer + section->size;
7070 reader->comp_dir = NULL;
7071 reader->abbrev_table = abbrev_table;
7072 }
7073
7074 /* Subroutine of init_cutu_and_read_dies to simplify it.
7075 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7076 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7077 already.
7078
7079 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7080 from it to the DIE in the DWO. If NULL we are skipping the stub.
7081 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7082 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
7083 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7084 STUB_COMP_DIR may be non-NULL.
7085 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7086 are filled in with the info of the DIE from the DWO file.
7087 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7088 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7089 kept around for at least as long as *RESULT_READER.
7090
7091 The result is non-zero if a valid (non-dummy) DIE was found. */
7092
7093 static int
7094 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7095 struct dwo_unit *dwo_unit,
7096 struct die_info *stub_comp_unit_die,
7097 const char *stub_comp_dir,
7098 struct die_reader_specs *result_reader,
7099 const gdb_byte **result_info_ptr,
7100 struct die_info **result_comp_unit_die,
7101 int *result_has_children,
7102 abbrev_table_up *result_dwo_abbrev_table)
7103 {
7104 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7105 struct objfile *objfile = dwarf2_per_objfile->objfile;
7106 struct dwarf2_cu *cu = this_cu->cu;
7107 bfd *abfd;
7108 const gdb_byte *begin_info_ptr, *info_ptr;
7109 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7110 int i,num_extra_attrs;
7111 struct dwarf2_section_info *dwo_abbrev_section;
7112 struct attribute *attr;
7113 struct die_info *comp_unit_die;
7114
7115 /* At most one of these may be provided. */
7116 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
7117
7118 /* These attributes aren't processed until later:
7119 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
7120 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7121 referenced later. However, these attributes are found in the stub
7122 which we won't have later. In order to not impose this complication
7123 on the rest of the code, we read them here and copy them to the
7124 DWO CU/TU die. */
7125
7126 stmt_list = NULL;
7127 low_pc = NULL;
7128 high_pc = NULL;
7129 ranges = NULL;
7130 comp_dir = NULL;
7131
7132 if (stub_comp_unit_die != NULL)
7133 {
7134 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7135 DWO file. */
7136 if (! this_cu->is_debug_types)
7137 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7138 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7139 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7140 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7141 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7142
7143 /* There should be a DW_AT_addr_base attribute here (if needed).
7144 We need the value before we can process DW_FORM_GNU_addr_index. */
7145 cu->addr_base = 0;
7146 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7147 if (attr)
7148 cu->addr_base = DW_UNSND (attr);
7149
7150 /* There should be a DW_AT_ranges_base attribute here (if needed).
7151 We need the value before we can process DW_AT_ranges. */
7152 cu->ranges_base = 0;
7153 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7154 if (attr)
7155 cu->ranges_base = DW_UNSND (attr);
7156 }
7157 else if (stub_comp_dir != NULL)
7158 {
7159 /* Reconstruct the comp_dir attribute to simplify the code below. */
7160 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
7161 comp_dir->name = DW_AT_comp_dir;
7162 comp_dir->form = DW_FORM_string;
7163 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7164 DW_STRING (comp_dir) = stub_comp_dir;
7165 }
7166
7167 /* Set up for reading the DWO CU/TU. */
7168 cu->dwo_unit = dwo_unit;
7169 dwarf2_section_info *section = dwo_unit->section;
7170 dwarf2_read_section (objfile, section);
7171 abfd = get_section_bfd_owner (section);
7172 begin_info_ptr = info_ptr = (section->buffer
7173 + to_underlying (dwo_unit->sect_off));
7174 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7175
7176 if (this_cu->is_debug_types)
7177 {
7178 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7179
7180 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7181 &cu->header, section,
7182 dwo_abbrev_section,
7183 info_ptr, rcuh_kind::TYPE);
7184 /* This is not an assert because it can be caused by bad debug info. */
7185 if (sig_type->signature != cu->header.signature)
7186 {
7187 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7188 " TU at offset %s [in module %s]"),
7189 hex_string (sig_type->signature),
7190 hex_string (cu->header.signature),
7191 sect_offset_str (dwo_unit->sect_off),
7192 bfd_get_filename (abfd));
7193 }
7194 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7195 /* For DWOs coming from DWP files, we don't know the CU length
7196 nor the type's offset in the TU until now. */
7197 dwo_unit->length = get_cu_length (&cu->header);
7198 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7199
7200 /* Establish the type offset that can be used to lookup the type.
7201 For DWO files, we don't know it until now. */
7202 sig_type->type_offset_in_section
7203 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7204 }
7205 else
7206 {
7207 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7208 &cu->header, section,
7209 dwo_abbrev_section,
7210 info_ptr, rcuh_kind::COMPILE);
7211 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7212 /* For DWOs coming from DWP files, we don't know the CU length
7213 until now. */
7214 dwo_unit->length = get_cu_length (&cu->header);
7215 }
7216
7217 *result_dwo_abbrev_table
7218 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7219 cu->header.abbrev_sect_off);
7220 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7221 result_dwo_abbrev_table->get ());
7222
7223 /* Read in the die, but leave space to copy over the attributes
7224 from the stub. This has the benefit of simplifying the rest of
7225 the code - all the work to maintain the illusion of a single
7226 DW_TAG_{compile,type}_unit DIE is done here. */
7227 num_extra_attrs = ((stmt_list != NULL)
7228 + (low_pc != NULL)
7229 + (high_pc != NULL)
7230 + (ranges != NULL)
7231 + (comp_dir != NULL));
7232 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7233 result_has_children, num_extra_attrs);
7234
7235 /* Copy over the attributes from the stub to the DIE we just read in. */
7236 comp_unit_die = *result_comp_unit_die;
7237 i = comp_unit_die->num_attrs;
7238 if (stmt_list != NULL)
7239 comp_unit_die->attrs[i++] = *stmt_list;
7240 if (low_pc != NULL)
7241 comp_unit_die->attrs[i++] = *low_pc;
7242 if (high_pc != NULL)
7243 comp_unit_die->attrs[i++] = *high_pc;
7244 if (ranges != NULL)
7245 comp_unit_die->attrs[i++] = *ranges;
7246 if (comp_dir != NULL)
7247 comp_unit_die->attrs[i++] = *comp_dir;
7248 comp_unit_die->num_attrs += num_extra_attrs;
7249
7250 if (dwarf_die_debug)
7251 {
7252 fprintf_unfiltered (gdb_stdlog,
7253 "Read die from %s@0x%x of %s:\n",
7254 get_section_name (section),
7255 (unsigned) (begin_info_ptr - section->buffer),
7256 bfd_get_filename (abfd));
7257 dump_die (comp_unit_die, dwarf_die_debug);
7258 }
7259
7260 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7261 TUs by skipping the stub and going directly to the entry in the DWO file.
7262 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7263 to get it via circuitous means. Blech. */
7264 if (comp_dir != NULL)
7265 result_reader->comp_dir = DW_STRING (comp_dir);
7266
7267 /* Skip dummy compilation units. */
7268 if (info_ptr >= begin_info_ptr + dwo_unit->length
7269 || peek_abbrev_code (abfd, info_ptr) == 0)
7270 return 0;
7271
7272 *result_info_ptr = info_ptr;
7273 return 1;
7274 }
7275
7276 /* Subroutine of init_cutu_and_read_dies to simplify it.
7277 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7278 Returns NULL if the specified DWO unit cannot be found. */
7279
7280 static struct dwo_unit *
7281 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7282 struct die_info *comp_unit_die)
7283 {
7284 struct dwarf2_cu *cu = this_cu->cu;
7285 ULONGEST signature;
7286 struct dwo_unit *dwo_unit;
7287 const char *comp_dir, *dwo_name;
7288
7289 gdb_assert (cu != NULL);
7290
7291 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7292 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7293 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7294
7295 if (this_cu->is_debug_types)
7296 {
7297 struct signatured_type *sig_type;
7298
7299 /* Since this_cu is the first member of struct signatured_type,
7300 we can go from a pointer to one to a pointer to the other. */
7301 sig_type = (struct signatured_type *) this_cu;
7302 signature = sig_type->signature;
7303 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7304 }
7305 else
7306 {
7307 struct attribute *attr;
7308
7309 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7310 if (! attr)
7311 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7312 " [in module %s]"),
7313 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
7314 signature = DW_UNSND (attr);
7315 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7316 signature);
7317 }
7318
7319 return dwo_unit;
7320 }
7321
7322 /* Subroutine of init_cutu_and_read_dies to simplify it.
7323 See it for a description of the parameters.
7324 Read a TU directly from a DWO file, bypassing the stub. */
7325
7326 static void
7327 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7328 int use_existing_cu, int keep,
7329 die_reader_func_ftype *die_reader_func,
7330 void *data)
7331 {
7332 std::unique_ptr<dwarf2_cu> new_cu;
7333 struct signatured_type *sig_type;
7334 struct die_reader_specs reader;
7335 const gdb_byte *info_ptr;
7336 struct die_info *comp_unit_die;
7337 int has_children;
7338 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7339
7340 /* Verify we can do the following downcast, and that we have the
7341 data we need. */
7342 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7343 sig_type = (struct signatured_type *) this_cu;
7344 gdb_assert (sig_type->dwo_unit != NULL);
7345
7346 if (use_existing_cu && this_cu->cu != NULL)
7347 {
7348 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
7349 /* There's no need to do the rereading_dwo_cu handling that
7350 init_cutu_and_read_dies does since we don't read the stub. */
7351 }
7352 else
7353 {
7354 /* If !use_existing_cu, this_cu->cu must be NULL. */
7355 gdb_assert (this_cu->cu == NULL);
7356 new_cu.reset (new dwarf2_cu (this_cu));
7357 }
7358
7359 /* A future optimization, if needed, would be to use an existing
7360 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7361 could share abbrev tables. */
7362
7363 /* The abbreviation table used by READER, this must live at least as long as
7364 READER. */
7365 abbrev_table_up dwo_abbrev_table;
7366
7367 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
7368 NULL /* stub_comp_unit_die */,
7369 sig_type->dwo_unit->dwo_file->comp_dir,
7370 &reader, &info_ptr,
7371 &comp_unit_die, &has_children,
7372 &dwo_abbrev_table) == 0)
7373 {
7374 /* Dummy die. */
7375 return;
7376 }
7377
7378 /* All the "real" work is done here. */
7379 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7380
7381 /* This duplicates the code in init_cutu_and_read_dies,
7382 but the alternative is making the latter more complex.
7383 This function is only for the special case of using DWO files directly:
7384 no point in overly complicating the general case just to handle this. */
7385 if (new_cu != NULL && keep)
7386 {
7387 /* Link this CU into read_in_chain. */
7388 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7389 dwarf2_per_objfile->read_in_chain = this_cu;
7390 /* The chain owns it now. */
7391 new_cu.release ();
7392 }
7393 }
7394
7395 /* Initialize a CU (or TU) and read its DIEs.
7396 If the CU defers to a DWO file, read the DWO file as well.
7397
7398 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7399 Otherwise the table specified in the comp unit header is read in and used.
7400 This is an optimization for when we already have the abbrev table.
7401
7402 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7403 Otherwise, a new CU is allocated with xmalloc.
7404
7405 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7406 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7407
7408 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7409 linker) then DIE_READER_FUNC will not get called. */
7410
7411 static void
7412 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
7413 struct abbrev_table *abbrev_table,
7414 int use_existing_cu, int keep,
7415 bool skip_partial,
7416 die_reader_func_ftype *die_reader_func,
7417 void *data)
7418 {
7419 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7420 struct objfile *objfile = dwarf2_per_objfile->objfile;
7421 struct dwarf2_section_info *section = this_cu->section;
7422 bfd *abfd = get_section_bfd_owner (section);
7423 struct dwarf2_cu *cu;
7424 const gdb_byte *begin_info_ptr, *info_ptr;
7425 struct die_reader_specs reader;
7426 struct die_info *comp_unit_die;
7427 int has_children;
7428 struct attribute *attr;
7429 struct signatured_type *sig_type = NULL;
7430 struct dwarf2_section_info *abbrev_section;
7431 /* Non-zero if CU currently points to a DWO file and we need to
7432 reread it. When this happens we need to reread the skeleton die
7433 before we can reread the DWO file (this only applies to CUs, not TUs). */
7434 int rereading_dwo_cu = 0;
7435
7436 if (dwarf_die_debug)
7437 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7438 this_cu->is_debug_types ? "type" : "comp",
7439 sect_offset_str (this_cu->sect_off));
7440
7441 if (use_existing_cu)
7442 gdb_assert (keep);
7443
7444 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7445 file (instead of going through the stub), short-circuit all of this. */
7446 if (this_cu->reading_dwo_directly)
7447 {
7448 /* Narrow down the scope of possibilities to have to understand. */
7449 gdb_assert (this_cu->is_debug_types);
7450 gdb_assert (abbrev_table == NULL);
7451 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7452 die_reader_func, data);
7453 return;
7454 }
7455
7456 /* This is cheap if the section is already read in. */
7457 dwarf2_read_section (objfile, section);
7458
7459 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7460
7461 abbrev_section = get_abbrev_section_for_cu (this_cu);
7462
7463 std::unique_ptr<dwarf2_cu> new_cu;
7464 if (use_existing_cu && this_cu->cu != NULL)
7465 {
7466 cu = this_cu->cu;
7467 /* If this CU is from a DWO file we need to start over, we need to
7468 refetch the attributes from the skeleton CU.
7469 This could be optimized by retrieving those attributes from when we
7470 were here the first time: the previous comp_unit_die was stored in
7471 comp_unit_obstack. But there's no data yet that we need this
7472 optimization. */
7473 if (cu->dwo_unit != NULL)
7474 rereading_dwo_cu = 1;
7475 }
7476 else
7477 {
7478 /* If !use_existing_cu, this_cu->cu must be NULL. */
7479 gdb_assert (this_cu->cu == NULL);
7480 new_cu.reset (new dwarf2_cu (this_cu));
7481 cu = new_cu.get ();
7482 }
7483
7484 /* Get the header. */
7485 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7486 {
7487 /* We already have the header, there's no need to read it in again. */
7488 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7489 }
7490 else
7491 {
7492 if (this_cu->is_debug_types)
7493 {
7494 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7495 &cu->header, section,
7496 abbrev_section, info_ptr,
7497 rcuh_kind::TYPE);
7498
7499 /* Since per_cu is the first member of struct signatured_type,
7500 we can go from a pointer to one to a pointer to the other. */
7501 sig_type = (struct signatured_type *) this_cu;
7502 gdb_assert (sig_type->signature == cu->header.signature);
7503 gdb_assert (sig_type->type_offset_in_tu
7504 == cu->header.type_cu_offset_in_tu);
7505 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7506
7507 /* LENGTH has not been set yet for type units if we're
7508 using .gdb_index. */
7509 this_cu->length = get_cu_length (&cu->header);
7510
7511 /* Establish the type offset that can be used to lookup the type. */
7512 sig_type->type_offset_in_section =
7513 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7514
7515 this_cu->dwarf_version = cu->header.version;
7516 }
7517 else
7518 {
7519 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7520 &cu->header, section,
7521 abbrev_section,
7522 info_ptr,
7523 rcuh_kind::COMPILE);
7524
7525 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7526 gdb_assert (this_cu->length == get_cu_length (&cu->header));
7527 this_cu->dwarf_version = cu->header.version;
7528 }
7529 }
7530
7531 /* Skip dummy compilation units. */
7532 if (info_ptr >= begin_info_ptr + this_cu->length
7533 || peek_abbrev_code (abfd, info_ptr) == 0)
7534 return;
7535
7536 /* If we don't have them yet, read the abbrevs for this compilation unit.
7537 And if we need to read them now, make sure they're freed when we're
7538 done (own the table through ABBREV_TABLE_HOLDER). */
7539 abbrev_table_up abbrev_table_holder;
7540 if (abbrev_table != NULL)
7541 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7542 else
7543 {
7544 abbrev_table_holder
7545 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7546 cu->header.abbrev_sect_off);
7547 abbrev_table = abbrev_table_holder.get ();
7548 }
7549
7550 /* Read the top level CU/TU die. */
7551 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
7552 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7553
7554 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7555 return;
7556
7557 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7558 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7559 table from the DWO file and pass the ownership over to us. It will be
7560 referenced from READER, so we must make sure to free it after we're done
7561 with READER.
7562
7563 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7564 DWO CU, that this test will fail (the attribute will not be present). */
7565 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7566 abbrev_table_up dwo_abbrev_table;
7567 if (attr)
7568 {
7569 struct dwo_unit *dwo_unit;
7570 struct die_info *dwo_comp_unit_die;
7571
7572 if (has_children)
7573 {
7574 complaint (&symfile_complaints,
7575 _("compilation unit with DW_AT_GNU_dwo_name"
7576 " has children (offset %s) [in module %s]"),
7577 sect_offset_str (this_cu->sect_off),
7578 bfd_get_filename (abfd));
7579 }
7580 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
7581 if (dwo_unit != NULL)
7582 {
7583 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
7584 comp_unit_die, NULL,
7585 &reader, &info_ptr,
7586 &dwo_comp_unit_die, &has_children,
7587 &dwo_abbrev_table) == 0)
7588 {
7589 /* Dummy die. */
7590 return;
7591 }
7592 comp_unit_die = dwo_comp_unit_die;
7593 }
7594 else
7595 {
7596 /* Yikes, we couldn't find the rest of the DIE, we only have
7597 the stub. A complaint has already been logged. There's
7598 not much more we can do except pass on the stub DIE to
7599 die_reader_func. We don't want to throw an error on bad
7600 debug info. */
7601 }
7602 }
7603
7604 /* All of the above is setup for this call. Yikes. */
7605 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7606
7607 /* Done, clean up. */
7608 if (new_cu != NULL && keep)
7609 {
7610 /* Link this CU into read_in_chain. */
7611 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7612 dwarf2_per_objfile->read_in_chain = this_cu;
7613 /* The chain owns it now. */
7614 new_cu.release ();
7615 }
7616 }
7617
7618 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
7619 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
7620 to have already done the lookup to find the DWO file).
7621
7622 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7623 THIS_CU->is_debug_types, but nothing else.
7624
7625 We fill in THIS_CU->length.
7626
7627 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7628 linker) then DIE_READER_FUNC will not get called.
7629
7630 THIS_CU->cu is always freed when done.
7631 This is done in order to not leave THIS_CU->cu in a state where we have
7632 to care whether it refers to the "main" CU or the DWO CU. */
7633
7634 static void
7635 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
7636 struct dwo_file *dwo_file,
7637 die_reader_func_ftype *die_reader_func,
7638 void *data)
7639 {
7640 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7641 struct objfile *objfile = dwarf2_per_objfile->objfile;
7642 struct dwarf2_section_info *section = this_cu->section;
7643 bfd *abfd = get_section_bfd_owner (section);
7644 struct dwarf2_section_info *abbrev_section;
7645 const gdb_byte *begin_info_ptr, *info_ptr;
7646 struct die_reader_specs reader;
7647 struct die_info *comp_unit_die;
7648 int has_children;
7649
7650 if (dwarf_die_debug)
7651 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7652 this_cu->is_debug_types ? "type" : "comp",
7653 sect_offset_str (this_cu->sect_off));
7654
7655 gdb_assert (this_cu->cu == NULL);
7656
7657 abbrev_section = (dwo_file != NULL
7658 ? &dwo_file->sections.abbrev
7659 : get_abbrev_section_for_cu (this_cu));
7660
7661 /* This is cheap if the section is already read in. */
7662 dwarf2_read_section (objfile, section);
7663
7664 struct dwarf2_cu cu (this_cu);
7665
7666 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7667 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7668 &cu.header, section,
7669 abbrev_section, info_ptr,
7670 (this_cu->is_debug_types
7671 ? rcuh_kind::TYPE
7672 : rcuh_kind::COMPILE));
7673
7674 this_cu->length = get_cu_length (&cu.header);
7675
7676 /* Skip dummy compilation units. */
7677 if (info_ptr >= begin_info_ptr + this_cu->length
7678 || peek_abbrev_code (abfd, info_ptr) == 0)
7679 return;
7680
7681 abbrev_table_up abbrev_table
7682 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7683 cu.header.abbrev_sect_off);
7684
7685 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
7686 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7687
7688 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7689 }
7690
7691 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
7692 does not lookup the specified DWO file.
7693 This cannot be used to read DWO files.
7694
7695 THIS_CU->cu is always freed when done.
7696 This is done in order to not leave THIS_CU->cu in a state where we have
7697 to care whether it refers to the "main" CU or the DWO CU.
7698 We can revisit this if the data shows there's a performance issue. */
7699
7700 static void
7701 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
7702 die_reader_func_ftype *die_reader_func,
7703 void *data)
7704 {
7705 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
7706 }
7707 \f
7708 /* Type Unit Groups.
7709
7710 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7711 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7712 so that all types coming from the same compilation (.o file) are grouped
7713 together. A future step could be to put the types in the same symtab as
7714 the CU the types ultimately came from. */
7715
7716 static hashval_t
7717 hash_type_unit_group (const void *item)
7718 {
7719 const struct type_unit_group *tu_group
7720 = (const struct type_unit_group *) item;
7721
7722 return hash_stmt_list_entry (&tu_group->hash);
7723 }
7724
7725 static int
7726 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7727 {
7728 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7729 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7730
7731 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7732 }
7733
7734 /* Allocate a hash table for type unit groups. */
7735
7736 static htab_t
7737 allocate_type_unit_groups_table (struct objfile *objfile)
7738 {
7739 return htab_create_alloc_ex (3,
7740 hash_type_unit_group,
7741 eq_type_unit_group,
7742 NULL,
7743 &objfile->objfile_obstack,
7744 hashtab_obstack_allocate,
7745 dummy_obstack_deallocate);
7746 }
7747
7748 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7749 partial symtabs. We combine several TUs per psymtab to not let the size
7750 of any one psymtab grow too big. */
7751 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7752 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7753
7754 /* Helper routine for get_type_unit_group.
7755 Create the type_unit_group object used to hold one or more TUs. */
7756
7757 static struct type_unit_group *
7758 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7759 {
7760 struct dwarf2_per_objfile *dwarf2_per_objfile
7761 = cu->per_cu->dwarf2_per_objfile;
7762 struct objfile *objfile = dwarf2_per_objfile->objfile;
7763 struct dwarf2_per_cu_data *per_cu;
7764 struct type_unit_group *tu_group;
7765
7766 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7767 struct type_unit_group);
7768 per_cu = &tu_group->per_cu;
7769 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7770
7771 if (dwarf2_per_objfile->using_index)
7772 {
7773 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7774 struct dwarf2_per_cu_quick_data);
7775 }
7776 else
7777 {
7778 unsigned int line_offset = to_underlying (line_offset_struct);
7779 struct partial_symtab *pst;
7780 char *name;
7781
7782 /* Give the symtab a useful name for debug purposes. */
7783 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7784 name = xstrprintf ("<type_units_%d>",
7785 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7786 else
7787 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
7788
7789 pst = create_partial_symtab (per_cu, name);
7790 pst->anonymous = 1;
7791
7792 xfree (name);
7793 }
7794
7795 tu_group->hash.dwo_unit = cu->dwo_unit;
7796 tu_group->hash.line_sect_off = line_offset_struct;
7797
7798 return tu_group;
7799 }
7800
7801 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7802 STMT_LIST is a DW_AT_stmt_list attribute. */
7803
7804 static struct type_unit_group *
7805 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7806 {
7807 struct dwarf2_per_objfile *dwarf2_per_objfile
7808 = cu->per_cu->dwarf2_per_objfile;
7809 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7810 struct type_unit_group *tu_group;
7811 void **slot;
7812 unsigned int line_offset;
7813 struct type_unit_group type_unit_group_for_lookup;
7814
7815 if (dwarf2_per_objfile->type_unit_groups == NULL)
7816 {
7817 dwarf2_per_objfile->type_unit_groups =
7818 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
7819 }
7820
7821 /* Do we need to create a new group, or can we use an existing one? */
7822
7823 if (stmt_list)
7824 {
7825 line_offset = DW_UNSND (stmt_list);
7826 ++tu_stats->nr_symtab_sharers;
7827 }
7828 else
7829 {
7830 /* Ugh, no stmt_list. Rare, but we have to handle it.
7831 We can do various things here like create one group per TU or
7832 spread them over multiple groups to split up the expansion work.
7833 To avoid worst case scenarios (too many groups or too large groups)
7834 we, umm, group them in bunches. */
7835 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7836 | (tu_stats->nr_stmt_less_type_units
7837 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7838 ++tu_stats->nr_stmt_less_type_units;
7839 }
7840
7841 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7842 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7843 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7844 &type_unit_group_for_lookup, INSERT);
7845 if (*slot != NULL)
7846 {
7847 tu_group = (struct type_unit_group *) *slot;
7848 gdb_assert (tu_group != NULL);
7849 }
7850 else
7851 {
7852 sect_offset line_offset_struct = (sect_offset) line_offset;
7853 tu_group = create_type_unit_group (cu, line_offset_struct);
7854 *slot = tu_group;
7855 ++tu_stats->nr_symtabs;
7856 }
7857
7858 return tu_group;
7859 }
7860 \f
7861 /* Partial symbol tables. */
7862
7863 /* Create a psymtab named NAME and assign it to PER_CU.
7864
7865 The caller must fill in the following details:
7866 dirname, textlow, texthigh. */
7867
7868 static struct partial_symtab *
7869 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7870 {
7871 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
7872 struct partial_symtab *pst;
7873
7874 pst = start_psymtab_common (objfile, name, 0,
7875 objfile->global_psymbols,
7876 objfile->static_psymbols);
7877
7878 pst->psymtabs_addrmap_supported = 1;
7879
7880 /* This is the glue that links PST into GDB's symbol API. */
7881 pst->read_symtab_private = per_cu;
7882 pst->read_symtab = dwarf2_read_symtab;
7883 per_cu->v.psymtab = pst;
7884
7885 return pst;
7886 }
7887
7888 /* The DATA object passed to process_psymtab_comp_unit_reader has this
7889 type. */
7890
7891 struct process_psymtab_comp_unit_data
7892 {
7893 /* True if we are reading a DW_TAG_partial_unit. */
7894
7895 int want_partial_unit;
7896
7897 /* The "pretend" language that is used if the CU doesn't declare a
7898 language. */
7899
7900 enum language pretend_language;
7901 };
7902
7903 /* die_reader_func for process_psymtab_comp_unit. */
7904
7905 static void
7906 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7907 const gdb_byte *info_ptr,
7908 struct die_info *comp_unit_die,
7909 int has_children,
7910 void *data)
7911 {
7912 struct dwarf2_cu *cu = reader->cu;
7913 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
7914 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7915 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7916 CORE_ADDR baseaddr;
7917 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7918 struct partial_symtab *pst;
7919 enum pc_bounds_kind cu_bounds_kind;
7920 const char *filename;
7921 struct process_psymtab_comp_unit_data *info
7922 = (struct process_psymtab_comp_unit_data *) data;
7923
7924 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
7925 return;
7926
7927 gdb_assert (! per_cu->is_debug_types);
7928
7929 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
7930
7931 cu->list_in_scope = &file_symbols;
7932
7933 /* Allocate a new partial symbol table structure. */
7934 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7935 if (filename == NULL)
7936 filename = "";
7937
7938 pst = create_partial_symtab (per_cu, filename);
7939
7940 /* This must be done before calling dwarf2_build_include_psymtabs. */
7941 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7942
7943 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7944
7945 dwarf2_find_base_address (comp_unit_die, cu);
7946
7947 /* Possibly set the default values of LOWPC and HIGHPC from
7948 `DW_AT_ranges'. */
7949 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7950 &best_highpc, cu, pst);
7951 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7952 /* Store the contiguous range if it is not empty; it can be empty for
7953 CUs with no code. */
7954 addrmap_set_empty (objfile->psymtabs_addrmap,
7955 gdbarch_adjust_dwarf2_addr (gdbarch,
7956 best_lowpc + baseaddr),
7957 gdbarch_adjust_dwarf2_addr (gdbarch,
7958 best_highpc + baseaddr) - 1,
7959 pst);
7960
7961 /* Check if comp unit has_children.
7962 If so, read the rest of the partial symbols from this comp unit.
7963 If not, there's no more debug_info for this comp unit. */
7964 if (has_children)
7965 {
7966 struct partial_die_info *first_die;
7967 CORE_ADDR lowpc, highpc;
7968
7969 lowpc = ((CORE_ADDR) -1);
7970 highpc = ((CORE_ADDR) 0);
7971
7972 first_die = load_partial_dies (reader, info_ptr, 1);
7973
7974 scan_partial_symbols (first_die, &lowpc, &highpc,
7975 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7976
7977 /* If we didn't find a lowpc, set it to highpc to avoid
7978 complaints from `maint check'. */
7979 if (lowpc == ((CORE_ADDR) -1))
7980 lowpc = highpc;
7981
7982 /* If the compilation unit didn't have an explicit address range,
7983 then use the information extracted from its child dies. */
7984 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7985 {
7986 best_lowpc = lowpc;
7987 best_highpc = highpc;
7988 }
7989 }
7990 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
7991 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
7992
7993 end_psymtab_common (objfile, pst);
7994
7995 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
7996 {
7997 int i;
7998 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
7999 struct dwarf2_per_cu_data *iter;
8000
8001 /* Fill in 'dependencies' here; we fill in 'users' in a
8002 post-pass. */
8003 pst->number_of_dependencies = len;
8004 pst->dependencies =
8005 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
8006 for (i = 0;
8007 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8008 i, iter);
8009 ++i)
8010 pst->dependencies[i] = iter->v.psymtab;
8011
8012 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8013 }
8014
8015 /* Get the list of files included in the current compilation unit,
8016 and build a psymtab for each of them. */
8017 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8018
8019 if (dwarf_read_debug)
8020 {
8021 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8022
8023 fprintf_unfiltered (gdb_stdlog,
8024 "Psymtab for %s unit @%s: %s - %s"
8025 ", %d global, %d static syms\n",
8026 per_cu->is_debug_types ? "type" : "comp",
8027 sect_offset_str (per_cu->sect_off),
8028 paddress (gdbarch, pst->textlow),
8029 paddress (gdbarch, pst->texthigh),
8030 pst->n_global_syms, pst->n_static_syms);
8031 }
8032 }
8033
8034 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8035 Process compilation unit THIS_CU for a psymtab. */
8036
8037 static void
8038 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
8039 int want_partial_unit,
8040 enum language pretend_language)
8041 {
8042 /* If this compilation unit was already read in, free the
8043 cached copy in order to read it in again. This is
8044 necessary because we skipped some symbols when we first
8045 read in the compilation unit (see load_partial_dies).
8046 This problem could be avoided, but the benefit is unclear. */
8047 if (this_cu->cu != NULL)
8048 free_one_cached_comp_unit (this_cu);
8049
8050 if (this_cu->is_debug_types)
8051 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8052 build_type_psymtabs_reader, NULL);
8053 else
8054 {
8055 process_psymtab_comp_unit_data info;
8056 info.want_partial_unit = want_partial_unit;
8057 info.pretend_language = pretend_language;
8058 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8059 process_psymtab_comp_unit_reader, &info);
8060 }
8061
8062 /* Age out any secondary CUs. */
8063 age_cached_comp_units (this_cu->dwarf2_per_objfile);
8064 }
8065
8066 /* Reader function for build_type_psymtabs. */
8067
8068 static void
8069 build_type_psymtabs_reader (const struct die_reader_specs *reader,
8070 const gdb_byte *info_ptr,
8071 struct die_info *type_unit_die,
8072 int has_children,
8073 void *data)
8074 {
8075 struct dwarf2_per_objfile *dwarf2_per_objfile
8076 = reader->cu->per_cu->dwarf2_per_objfile;
8077 struct objfile *objfile = dwarf2_per_objfile->objfile;
8078 struct dwarf2_cu *cu = reader->cu;
8079 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8080 struct signatured_type *sig_type;
8081 struct type_unit_group *tu_group;
8082 struct attribute *attr;
8083 struct partial_die_info *first_die;
8084 CORE_ADDR lowpc, highpc;
8085 struct partial_symtab *pst;
8086
8087 gdb_assert (data == NULL);
8088 gdb_assert (per_cu->is_debug_types);
8089 sig_type = (struct signatured_type *) per_cu;
8090
8091 if (! has_children)
8092 return;
8093
8094 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
8095 tu_group = get_type_unit_group (cu, attr);
8096
8097 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
8098
8099 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8100 cu->list_in_scope = &file_symbols;
8101 pst = create_partial_symtab (per_cu, "");
8102 pst->anonymous = 1;
8103
8104 first_die = load_partial_dies (reader, info_ptr, 1);
8105
8106 lowpc = (CORE_ADDR) -1;
8107 highpc = (CORE_ADDR) 0;
8108 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8109
8110 end_psymtab_common (objfile, pst);
8111 }
8112
8113 /* Struct used to sort TUs by their abbreviation table offset. */
8114
8115 struct tu_abbrev_offset
8116 {
8117 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
8118 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
8119 {}
8120
8121 signatured_type *sig_type;
8122 sect_offset abbrev_offset;
8123 };
8124
8125 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
8126
8127 static bool
8128 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8129 const struct tu_abbrev_offset &b)
8130 {
8131 return a.abbrev_offset < b.abbrev_offset;
8132 }
8133
8134 /* Efficiently read all the type units.
8135 This does the bulk of the work for build_type_psymtabs.
8136
8137 The efficiency is because we sort TUs by the abbrev table they use and
8138 only read each abbrev table once. In one program there are 200K TUs
8139 sharing 8K abbrev tables.
8140
8141 The main purpose of this function is to support building the
8142 dwarf2_per_objfile->type_unit_groups table.
8143 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8144 can collapse the search space by grouping them by stmt_list.
8145 The savings can be significant, in the same program from above the 200K TUs
8146 share 8K stmt_list tables.
8147
8148 FUNC is expected to call get_type_unit_group, which will create the
8149 struct type_unit_group if necessary and add it to
8150 dwarf2_per_objfile->type_unit_groups. */
8151
8152 static void
8153 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
8154 {
8155 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8156 abbrev_table_up abbrev_table;
8157 sect_offset abbrev_offset;
8158
8159 /* It's up to the caller to not call us multiple times. */
8160 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8161
8162 if (dwarf2_per_objfile->all_type_units.empty ())
8163 return;
8164
8165 /* TUs typically share abbrev tables, and there can be way more TUs than
8166 abbrev tables. Sort by abbrev table to reduce the number of times we
8167 read each abbrev table in.
8168 Alternatives are to punt or to maintain a cache of abbrev tables.
8169 This is simpler and efficient enough for now.
8170
8171 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8172 symtab to use). Typically TUs with the same abbrev offset have the same
8173 stmt_list value too so in practice this should work well.
8174
8175 The basic algorithm here is:
8176
8177 sort TUs by abbrev table
8178 for each TU with same abbrev table:
8179 read abbrev table if first user
8180 read TU top level DIE
8181 [IWBN if DWO skeletons had DW_AT_stmt_list]
8182 call FUNC */
8183
8184 if (dwarf_read_debug)
8185 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8186
8187 /* Sort in a separate table to maintain the order of all_type_units
8188 for .gdb_index: TU indices directly index all_type_units. */
8189 std::vector<tu_abbrev_offset> sorted_by_abbrev;
8190 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
8191
8192 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
8193 sorted_by_abbrev.emplace_back
8194 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
8195 sig_type->per_cu.section,
8196 sig_type->per_cu.sect_off));
8197
8198 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8199 sort_tu_by_abbrev_offset);
8200
8201 abbrev_offset = (sect_offset) ~(unsigned) 0;
8202
8203 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
8204 {
8205 /* Switch to the next abbrev table if necessary. */
8206 if (abbrev_table == NULL
8207 || tu.abbrev_offset != abbrev_offset)
8208 {
8209 abbrev_offset = tu.abbrev_offset;
8210 abbrev_table =
8211 abbrev_table_read_table (dwarf2_per_objfile,
8212 &dwarf2_per_objfile->abbrev,
8213 abbrev_offset);
8214 ++tu_stats->nr_uniq_abbrev_tables;
8215 }
8216
8217 init_cutu_and_read_dies (&tu.sig_type->per_cu, abbrev_table.get (),
8218 0, 0, false, build_type_psymtabs_reader, NULL);
8219 }
8220 }
8221
8222 /* Print collected type unit statistics. */
8223
8224 static void
8225 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
8226 {
8227 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8228
8229 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8230 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
8231 dwarf2_per_objfile->all_type_units.size ());
8232 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8233 tu_stats->nr_uniq_abbrev_tables);
8234 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8235 tu_stats->nr_symtabs);
8236 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8237 tu_stats->nr_symtab_sharers);
8238 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8239 tu_stats->nr_stmt_less_type_units);
8240 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8241 tu_stats->nr_all_type_units_reallocs);
8242 }
8243
8244 /* Traversal function for build_type_psymtabs. */
8245
8246 static int
8247 build_type_psymtab_dependencies (void **slot, void *info)
8248 {
8249 struct dwarf2_per_objfile *dwarf2_per_objfile
8250 = (struct dwarf2_per_objfile *) info;
8251 struct objfile *objfile = dwarf2_per_objfile->objfile;
8252 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8253 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8254 struct partial_symtab *pst = per_cu->v.psymtab;
8255 int len = VEC_length (sig_type_ptr, tu_group->tus);
8256 struct signatured_type *iter;
8257 int i;
8258
8259 gdb_assert (len > 0);
8260 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
8261
8262 pst->number_of_dependencies = len;
8263 pst->dependencies =
8264 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
8265 for (i = 0;
8266 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
8267 ++i)
8268 {
8269 gdb_assert (iter->per_cu.is_debug_types);
8270 pst->dependencies[i] = iter->per_cu.v.psymtab;
8271 iter->type_unit_group = tu_group;
8272 }
8273
8274 VEC_free (sig_type_ptr, tu_group->tus);
8275
8276 return 1;
8277 }
8278
8279 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8280 Build partial symbol tables for the .debug_types comp-units. */
8281
8282 static void
8283 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
8284 {
8285 if (! create_all_type_units (dwarf2_per_objfile))
8286 return;
8287
8288 build_type_psymtabs_1 (dwarf2_per_objfile);
8289 }
8290
8291 /* Traversal function for process_skeletonless_type_unit.
8292 Read a TU in a DWO file and build partial symbols for it. */
8293
8294 static int
8295 process_skeletonless_type_unit (void **slot, void *info)
8296 {
8297 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8298 struct dwarf2_per_objfile *dwarf2_per_objfile
8299 = (struct dwarf2_per_objfile *) info;
8300 struct signatured_type find_entry, *entry;
8301
8302 /* If this TU doesn't exist in the global table, add it and read it in. */
8303
8304 if (dwarf2_per_objfile->signatured_types == NULL)
8305 {
8306 dwarf2_per_objfile->signatured_types
8307 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
8308 }
8309
8310 find_entry.signature = dwo_unit->signature;
8311 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8312 INSERT);
8313 /* If we've already seen this type there's nothing to do. What's happening
8314 is we're doing our own version of comdat-folding here. */
8315 if (*slot != NULL)
8316 return 1;
8317
8318 /* This does the job that create_all_type_units would have done for
8319 this TU. */
8320 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8321 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
8322 *slot = entry;
8323
8324 /* This does the job that build_type_psymtabs_1 would have done. */
8325 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0, false,
8326 build_type_psymtabs_reader, NULL);
8327
8328 return 1;
8329 }
8330
8331 /* Traversal function for process_skeletonless_type_units. */
8332
8333 static int
8334 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8335 {
8336 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8337
8338 if (dwo_file->tus != NULL)
8339 {
8340 htab_traverse_noresize (dwo_file->tus,
8341 process_skeletonless_type_unit, info);
8342 }
8343
8344 return 1;
8345 }
8346
8347 /* Scan all TUs of DWO files, verifying we've processed them.
8348 This is needed in case a TU was emitted without its skeleton.
8349 Note: This can't be done until we know what all the DWO files are. */
8350
8351 static void
8352 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8353 {
8354 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8355 if (get_dwp_file (dwarf2_per_objfile) == NULL
8356 && dwarf2_per_objfile->dwo_files != NULL)
8357 {
8358 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
8359 process_dwo_file_for_skeletonless_type_units,
8360 dwarf2_per_objfile);
8361 }
8362 }
8363
8364 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8365
8366 static void
8367 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
8368 {
8369 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8370 {
8371 struct partial_symtab *pst = per_cu->v.psymtab;
8372
8373 if (pst == NULL)
8374 continue;
8375
8376 for (int j = 0; j < pst->number_of_dependencies; ++j)
8377 {
8378 /* Set the 'user' field only if it is not already set. */
8379 if (pst->dependencies[j]->user == NULL)
8380 pst->dependencies[j]->user = pst;
8381 }
8382 }
8383 }
8384
8385 /* Build the partial symbol table by doing a quick pass through the
8386 .debug_info and .debug_abbrev sections. */
8387
8388 static void
8389 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
8390 {
8391 struct objfile *objfile = dwarf2_per_objfile->objfile;
8392
8393 if (dwarf_read_debug)
8394 {
8395 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8396 objfile_name (objfile));
8397 }
8398
8399 dwarf2_per_objfile->reading_partial_symbols = 1;
8400
8401 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
8402
8403 /* Any cached compilation units will be linked by the per-objfile
8404 read_in_chain. Make sure to free them when we're done. */
8405 free_cached_comp_units freer (dwarf2_per_objfile);
8406
8407 build_type_psymtabs (dwarf2_per_objfile);
8408
8409 create_all_comp_units (dwarf2_per_objfile);
8410
8411 /* Create a temporary address map on a temporary obstack. We later
8412 copy this to the final obstack. */
8413 auto_obstack temp_obstack;
8414
8415 scoped_restore save_psymtabs_addrmap
8416 = make_scoped_restore (&objfile->psymtabs_addrmap,
8417 addrmap_create_mutable (&temp_obstack));
8418
8419 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8420 process_psymtab_comp_unit (per_cu, 0, language_minimal);
8421
8422 /* This has to wait until we read the CUs, we need the list of DWOs. */
8423 process_skeletonless_type_units (dwarf2_per_objfile);
8424
8425 /* Now that all TUs have been processed we can fill in the dependencies. */
8426 if (dwarf2_per_objfile->type_unit_groups != NULL)
8427 {
8428 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
8429 build_type_psymtab_dependencies, dwarf2_per_objfile);
8430 }
8431
8432 if (dwarf_read_debug)
8433 print_tu_stats (dwarf2_per_objfile);
8434
8435 set_partial_user (dwarf2_per_objfile);
8436
8437 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
8438 &objfile->objfile_obstack);
8439 /* At this point we want to keep the address map. */
8440 save_psymtabs_addrmap.release ();
8441
8442 if (dwarf_read_debug)
8443 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8444 objfile_name (objfile));
8445 }
8446
8447 /* die_reader_func for load_partial_comp_unit. */
8448
8449 static void
8450 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
8451 const gdb_byte *info_ptr,
8452 struct die_info *comp_unit_die,
8453 int has_children,
8454 void *data)
8455 {
8456 struct dwarf2_cu *cu = reader->cu;
8457
8458 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
8459
8460 /* Check if comp unit has_children.
8461 If so, read the rest of the partial symbols from this comp unit.
8462 If not, there's no more debug_info for this comp unit. */
8463 if (has_children)
8464 load_partial_dies (reader, info_ptr, 0);
8465 }
8466
8467 /* Load the partial DIEs for a secondary CU into memory.
8468 This is also used when rereading a primary CU with load_all_dies. */
8469
8470 static void
8471 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8472 {
8473 init_cutu_and_read_dies (this_cu, NULL, 1, 1, false,
8474 load_partial_comp_unit_reader, NULL);
8475 }
8476
8477 static void
8478 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8479 struct dwarf2_section_info *section,
8480 struct dwarf2_section_info *abbrev_section,
8481 unsigned int is_dwz)
8482 {
8483 const gdb_byte *info_ptr;
8484 struct objfile *objfile = dwarf2_per_objfile->objfile;
8485
8486 if (dwarf_read_debug)
8487 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8488 get_section_name (section),
8489 get_section_file_name (section));
8490
8491 dwarf2_read_section (objfile, section);
8492
8493 info_ptr = section->buffer;
8494
8495 while (info_ptr < section->buffer + section->size)
8496 {
8497 struct dwarf2_per_cu_data *this_cu;
8498
8499 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8500
8501 comp_unit_head cu_header;
8502 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8503 abbrev_section, info_ptr,
8504 rcuh_kind::COMPILE);
8505
8506 /* Save the compilation unit for later lookup. */
8507 if (cu_header.unit_type != DW_UT_type)
8508 {
8509 this_cu = XOBNEW (&objfile->objfile_obstack,
8510 struct dwarf2_per_cu_data);
8511 memset (this_cu, 0, sizeof (*this_cu));
8512 }
8513 else
8514 {
8515 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8516 struct signatured_type);
8517 memset (sig_type, 0, sizeof (*sig_type));
8518 sig_type->signature = cu_header.signature;
8519 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8520 this_cu = &sig_type->per_cu;
8521 }
8522 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8523 this_cu->sect_off = sect_off;
8524 this_cu->length = cu_header.length + cu_header.initial_length_size;
8525 this_cu->is_dwz = is_dwz;
8526 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8527 this_cu->section = section;
8528
8529 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
8530
8531 info_ptr = info_ptr + this_cu->length;
8532 }
8533 }
8534
8535 /* Create a list of all compilation units in OBJFILE.
8536 This is only done for -readnow and building partial symtabs. */
8537
8538 static void
8539 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8540 {
8541 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
8542 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
8543 &dwarf2_per_objfile->abbrev, 0);
8544
8545 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
8546 if (dwz != NULL)
8547 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
8548 1);
8549 }
8550
8551 /* Process all loaded DIEs for compilation unit CU, starting at
8552 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8553 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8554 DW_AT_ranges). See the comments of add_partial_subprogram on how
8555 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8556
8557 static void
8558 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8559 CORE_ADDR *highpc, int set_addrmap,
8560 struct dwarf2_cu *cu)
8561 {
8562 struct partial_die_info *pdi;
8563
8564 /* Now, march along the PDI's, descending into ones which have
8565 interesting children but skipping the children of the other ones,
8566 until we reach the end of the compilation unit. */
8567
8568 pdi = first_die;
8569
8570 while (pdi != NULL)
8571 {
8572 pdi->fixup (cu);
8573
8574 /* Anonymous namespaces or modules have no name but have interesting
8575 children, so we need to look at them. Ditto for anonymous
8576 enums. */
8577
8578 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
8579 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8580 || pdi->tag == DW_TAG_imported_unit
8581 || pdi->tag == DW_TAG_inlined_subroutine)
8582 {
8583 switch (pdi->tag)
8584 {
8585 case DW_TAG_subprogram:
8586 case DW_TAG_inlined_subroutine:
8587 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8588 break;
8589 case DW_TAG_constant:
8590 case DW_TAG_variable:
8591 case DW_TAG_typedef:
8592 case DW_TAG_union_type:
8593 if (!pdi->is_declaration)
8594 {
8595 add_partial_symbol (pdi, cu);
8596 }
8597 break;
8598 case DW_TAG_class_type:
8599 case DW_TAG_interface_type:
8600 case DW_TAG_structure_type:
8601 if (!pdi->is_declaration)
8602 {
8603 add_partial_symbol (pdi, cu);
8604 }
8605 if ((cu->language == language_rust
8606 || cu->language == language_cplus) && pdi->has_children)
8607 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8608 set_addrmap, cu);
8609 break;
8610 case DW_TAG_enumeration_type:
8611 if (!pdi->is_declaration)
8612 add_partial_enumeration (pdi, cu);
8613 break;
8614 case DW_TAG_base_type:
8615 case DW_TAG_subrange_type:
8616 /* File scope base type definitions are added to the partial
8617 symbol table. */
8618 add_partial_symbol (pdi, cu);
8619 break;
8620 case DW_TAG_namespace:
8621 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8622 break;
8623 case DW_TAG_module:
8624 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8625 break;
8626 case DW_TAG_imported_unit:
8627 {
8628 struct dwarf2_per_cu_data *per_cu;
8629
8630 /* For now we don't handle imported units in type units. */
8631 if (cu->per_cu->is_debug_types)
8632 {
8633 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8634 " supported in type units [in module %s]"),
8635 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
8636 }
8637
8638 per_cu = dwarf2_find_containing_comp_unit
8639 (pdi->d.sect_off, pdi->is_dwz,
8640 cu->per_cu->dwarf2_per_objfile);
8641
8642 /* Go read the partial unit, if needed. */
8643 if (per_cu->v.psymtab == NULL)
8644 process_psymtab_comp_unit (per_cu, 1, cu->language);
8645
8646 VEC_safe_push (dwarf2_per_cu_ptr,
8647 cu->per_cu->imported_symtabs, per_cu);
8648 }
8649 break;
8650 case DW_TAG_imported_declaration:
8651 add_partial_symbol (pdi, cu);
8652 break;
8653 default:
8654 break;
8655 }
8656 }
8657
8658 /* If the die has a sibling, skip to the sibling. */
8659
8660 pdi = pdi->die_sibling;
8661 }
8662 }
8663
8664 /* Functions used to compute the fully scoped name of a partial DIE.
8665
8666 Normally, this is simple. For C++, the parent DIE's fully scoped
8667 name is concatenated with "::" and the partial DIE's name.
8668 Enumerators are an exception; they use the scope of their parent
8669 enumeration type, i.e. the name of the enumeration type is not
8670 prepended to the enumerator.
8671
8672 There are two complexities. One is DW_AT_specification; in this
8673 case "parent" means the parent of the target of the specification,
8674 instead of the direct parent of the DIE. The other is compilers
8675 which do not emit DW_TAG_namespace; in this case we try to guess
8676 the fully qualified name of structure types from their members'
8677 linkage names. This must be done using the DIE's children rather
8678 than the children of any DW_AT_specification target. We only need
8679 to do this for structures at the top level, i.e. if the target of
8680 any DW_AT_specification (if any; otherwise the DIE itself) does not
8681 have a parent. */
8682
8683 /* Compute the scope prefix associated with PDI's parent, in
8684 compilation unit CU. The result will be allocated on CU's
8685 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8686 field. NULL is returned if no prefix is necessary. */
8687 static const char *
8688 partial_die_parent_scope (struct partial_die_info *pdi,
8689 struct dwarf2_cu *cu)
8690 {
8691 const char *grandparent_scope;
8692 struct partial_die_info *parent, *real_pdi;
8693
8694 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8695 then this means the parent of the specification DIE. */
8696
8697 real_pdi = pdi;
8698 while (real_pdi->has_specification)
8699 real_pdi = find_partial_die (real_pdi->spec_offset,
8700 real_pdi->spec_is_dwz, cu);
8701
8702 parent = real_pdi->die_parent;
8703 if (parent == NULL)
8704 return NULL;
8705
8706 if (parent->scope_set)
8707 return parent->scope;
8708
8709 parent->fixup (cu);
8710
8711 grandparent_scope = partial_die_parent_scope (parent, cu);
8712
8713 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8714 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8715 Work around this problem here. */
8716 if (cu->language == language_cplus
8717 && parent->tag == DW_TAG_namespace
8718 && strcmp (parent->name, "::") == 0
8719 && grandparent_scope == NULL)
8720 {
8721 parent->scope = NULL;
8722 parent->scope_set = 1;
8723 return NULL;
8724 }
8725
8726 if (pdi->tag == DW_TAG_enumerator)
8727 /* Enumerators should not get the name of the enumeration as a prefix. */
8728 parent->scope = grandparent_scope;
8729 else if (parent->tag == DW_TAG_namespace
8730 || parent->tag == DW_TAG_module
8731 || parent->tag == DW_TAG_structure_type
8732 || parent->tag == DW_TAG_class_type
8733 || parent->tag == DW_TAG_interface_type
8734 || parent->tag == DW_TAG_union_type
8735 || parent->tag == DW_TAG_enumeration_type)
8736 {
8737 if (grandparent_scope == NULL)
8738 parent->scope = parent->name;
8739 else
8740 parent->scope = typename_concat (&cu->comp_unit_obstack,
8741 grandparent_scope,
8742 parent->name, 0, cu);
8743 }
8744 else
8745 {
8746 /* FIXME drow/2004-04-01: What should we be doing with
8747 function-local names? For partial symbols, we should probably be
8748 ignoring them. */
8749 complaint (&symfile_complaints,
8750 _("unhandled containing DIE tag %d for DIE at %s"),
8751 parent->tag, sect_offset_str (pdi->sect_off));
8752 parent->scope = grandparent_scope;
8753 }
8754
8755 parent->scope_set = 1;
8756 return parent->scope;
8757 }
8758
8759 /* Return the fully scoped name associated with PDI, from compilation unit
8760 CU. The result will be allocated with malloc. */
8761
8762 static char *
8763 partial_die_full_name (struct partial_die_info *pdi,
8764 struct dwarf2_cu *cu)
8765 {
8766 const char *parent_scope;
8767
8768 /* If this is a template instantiation, we can not work out the
8769 template arguments from partial DIEs. So, unfortunately, we have
8770 to go through the full DIEs. At least any work we do building
8771 types here will be reused if full symbols are loaded later. */
8772 if (pdi->has_template_arguments)
8773 {
8774 pdi->fixup (cu);
8775
8776 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8777 {
8778 struct die_info *die;
8779 struct attribute attr;
8780 struct dwarf2_cu *ref_cu = cu;
8781
8782 /* DW_FORM_ref_addr is using section offset. */
8783 attr.name = (enum dwarf_attribute) 0;
8784 attr.form = DW_FORM_ref_addr;
8785 attr.u.unsnd = to_underlying (pdi->sect_off);
8786 die = follow_die_ref (NULL, &attr, &ref_cu);
8787
8788 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8789 }
8790 }
8791
8792 parent_scope = partial_die_parent_scope (pdi, cu);
8793 if (parent_scope == NULL)
8794 return NULL;
8795 else
8796 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
8797 }
8798
8799 static void
8800 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8801 {
8802 struct dwarf2_per_objfile *dwarf2_per_objfile
8803 = cu->per_cu->dwarf2_per_objfile;
8804 struct objfile *objfile = dwarf2_per_objfile->objfile;
8805 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8806 CORE_ADDR addr = 0;
8807 const char *actual_name = NULL;
8808 CORE_ADDR baseaddr;
8809 char *built_actual_name;
8810
8811 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8812
8813 built_actual_name = partial_die_full_name (pdi, cu);
8814 if (built_actual_name != NULL)
8815 actual_name = built_actual_name;
8816
8817 if (actual_name == NULL)
8818 actual_name = pdi->name;
8819
8820 switch (pdi->tag)
8821 {
8822 case DW_TAG_inlined_subroutine:
8823 case DW_TAG_subprogram:
8824 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
8825 if (pdi->is_external || cu->language == language_ada)
8826 {
8827 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
8828 of the global scope. But in Ada, we want to be able to access
8829 nested procedures globally. So all Ada subprograms are stored
8830 in the global scope. */
8831 add_psymbol_to_list (actual_name, strlen (actual_name),
8832 built_actual_name != NULL,
8833 VAR_DOMAIN, LOC_BLOCK,
8834 &objfile->global_psymbols,
8835 addr, cu->language, objfile);
8836 }
8837 else
8838 {
8839 add_psymbol_to_list (actual_name, strlen (actual_name),
8840 built_actual_name != NULL,
8841 VAR_DOMAIN, LOC_BLOCK,
8842 &objfile->static_psymbols,
8843 addr, cu->language, objfile);
8844 }
8845
8846 if (pdi->main_subprogram && actual_name != NULL)
8847 set_objfile_main_name (objfile, actual_name, cu->language);
8848 break;
8849 case DW_TAG_constant:
8850 {
8851 std::vector<partial_symbol *> *list;
8852
8853 if (pdi->is_external)
8854 list = &objfile->global_psymbols;
8855 else
8856 list = &objfile->static_psymbols;
8857 add_psymbol_to_list (actual_name, strlen (actual_name),
8858 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8859 list, 0, cu->language, objfile);
8860 }
8861 break;
8862 case DW_TAG_variable:
8863 if (pdi->d.locdesc)
8864 addr = decode_locdesc (pdi->d.locdesc, cu);
8865
8866 if (pdi->d.locdesc
8867 && addr == 0
8868 && !dwarf2_per_objfile->has_section_at_zero)
8869 {
8870 /* A global or static variable may also have been stripped
8871 out by the linker if unused, in which case its address
8872 will be nullified; do not add such variables into partial
8873 symbol table then. */
8874 }
8875 else if (pdi->is_external)
8876 {
8877 /* Global Variable.
8878 Don't enter into the minimal symbol tables as there is
8879 a minimal symbol table entry from the ELF symbols already.
8880 Enter into partial symbol table if it has a location
8881 descriptor or a type.
8882 If the location descriptor is missing, new_symbol will create
8883 a LOC_UNRESOLVED symbol, the address of the variable will then
8884 be determined from the minimal symbol table whenever the variable
8885 is referenced.
8886 The address for the partial symbol table entry is not
8887 used by GDB, but it comes in handy for debugging partial symbol
8888 table building. */
8889
8890 if (pdi->d.locdesc || pdi->has_type)
8891 add_psymbol_to_list (actual_name, strlen (actual_name),
8892 built_actual_name != NULL,
8893 VAR_DOMAIN, LOC_STATIC,
8894 &objfile->global_psymbols,
8895 addr + baseaddr,
8896 cu->language, objfile);
8897 }
8898 else
8899 {
8900 int has_loc = pdi->d.locdesc != NULL;
8901
8902 /* Static Variable. Skip symbols whose value we cannot know (those
8903 without location descriptors or constant values). */
8904 if (!has_loc && !pdi->has_const_value)
8905 {
8906 xfree (built_actual_name);
8907 return;
8908 }
8909
8910 add_psymbol_to_list (actual_name, strlen (actual_name),
8911 built_actual_name != NULL,
8912 VAR_DOMAIN, LOC_STATIC,
8913 &objfile->static_psymbols,
8914 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
8915 cu->language, objfile);
8916 }
8917 break;
8918 case DW_TAG_typedef:
8919 case DW_TAG_base_type:
8920 case DW_TAG_subrange_type:
8921 add_psymbol_to_list (actual_name, strlen (actual_name),
8922 built_actual_name != NULL,
8923 VAR_DOMAIN, LOC_TYPEDEF,
8924 &objfile->static_psymbols,
8925 0, cu->language, objfile);
8926 break;
8927 case DW_TAG_imported_declaration:
8928 case DW_TAG_namespace:
8929 add_psymbol_to_list (actual_name, strlen (actual_name),
8930 built_actual_name != NULL,
8931 VAR_DOMAIN, LOC_TYPEDEF,
8932 &objfile->global_psymbols,
8933 0, cu->language, objfile);
8934 break;
8935 case DW_TAG_module:
8936 add_psymbol_to_list (actual_name, strlen (actual_name),
8937 built_actual_name != NULL,
8938 MODULE_DOMAIN, LOC_TYPEDEF,
8939 &objfile->global_psymbols,
8940 0, cu->language, objfile);
8941 break;
8942 case DW_TAG_class_type:
8943 case DW_TAG_interface_type:
8944 case DW_TAG_structure_type:
8945 case DW_TAG_union_type:
8946 case DW_TAG_enumeration_type:
8947 /* Skip external references. The DWARF standard says in the section
8948 about "Structure, Union, and Class Type Entries": "An incomplete
8949 structure, union or class type is represented by a structure,
8950 union or class entry that does not have a byte size attribute
8951 and that has a DW_AT_declaration attribute." */
8952 if (!pdi->has_byte_size && pdi->is_declaration)
8953 {
8954 xfree (built_actual_name);
8955 return;
8956 }
8957
8958 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8959 static vs. global. */
8960 add_psymbol_to_list (actual_name, strlen (actual_name),
8961 built_actual_name != NULL,
8962 STRUCT_DOMAIN, LOC_TYPEDEF,
8963 cu->language == language_cplus
8964 ? &objfile->global_psymbols
8965 : &objfile->static_psymbols,
8966 0, cu->language, objfile);
8967
8968 break;
8969 case DW_TAG_enumerator:
8970 add_psymbol_to_list (actual_name, strlen (actual_name),
8971 built_actual_name != NULL,
8972 VAR_DOMAIN, LOC_CONST,
8973 cu->language == language_cplus
8974 ? &objfile->global_psymbols
8975 : &objfile->static_psymbols,
8976 0, cu->language, objfile);
8977 break;
8978 default:
8979 break;
8980 }
8981
8982 xfree (built_actual_name);
8983 }
8984
8985 /* Read a partial die corresponding to a namespace; also, add a symbol
8986 corresponding to that namespace to the symbol table. NAMESPACE is
8987 the name of the enclosing namespace. */
8988
8989 static void
8990 add_partial_namespace (struct partial_die_info *pdi,
8991 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8992 int set_addrmap, struct dwarf2_cu *cu)
8993 {
8994 /* Add a symbol for the namespace. */
8995
8996 add_partial_symbol (pdi, cu);
8997
8998 /* Now scan partial symbols in that namespace. */
8999
9000 if (pdi->has_children)
9001 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9002 }
9003
9004 /* Read a partial die corresponding to a Fortran module. */
9005
9006 static void
9007 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
9008 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
9009 {
9010 /* Add a symbol for the namespace. */
9011
9012 add_partial_symbol (pdi, cu);
9013
9014 /* Now scan partial symbols in that module. */
9015
9016 if (pdi->has_children)
9017 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9018 }
9019
9020 /* Read a partial die corresponding to a subprogram or an inlined
9021 subprogram and create a partial symbol for that subprogram.
9022 When the CU language allows it, this routine also defines a partial
9023 symbol for each nested subprogram that this subprogram contains.
9024 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9025 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
9026
9027 PDI may also be a lexical block, in which case we simply search
9028 recursively for subprograms defined inside that lexical block.
9029 Again, this is only performed when the CU language allows this
9030 type of definitions. */
9031
9032 static void
9033 add_partial_subprogram (struct partial_die_info *pdi,
9034 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9035 int set_addrmap, struct dwarf2_cu *cu)
9036 {
9037 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
9038 {
9039 if (pdi->has_pc_info)
9040 {
9041 if (pdi->lowpc < *lowpc)
9042 *lowpc = pdi->lowpc;
9043 if (pdi->highpc > *highpc)
9044 *highpc = pdi->highpc;
9045 if (set_addrmap)
9046 {
9047 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9048 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9049 CORE_ADDR baseaddr;
9050 CORE_ADDR highpc;
9051 CORE_ADDR lowpc;
9052
9053 baseaddr = ANOFFSET (objfile->section_offsets,
9054 SECT_OFF_TEXT (objfile));
9055 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9056 pdi->lowpc + baseaddr);
9057 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9058 pdi->highpc + baseaddr);
9059 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9060 cu->per_cu->v.psymtab);
9061 }
9062 }
9063
9064 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9065 {
9066 if (!pdi->is_declaration)
9067 /* Ignore subprogram DIEs that do not have a name, they are
9068 illegal. Do not emit a complaint at this point, we will
9069 do so when we convert this psymtab into a symtab. */
9070 if (pdi->name)
9071 add_partial_symbol (pdi, cu);
9072 }
9073 }
9074
9075 if (! pdi->has_children)
9076 return;
9077
9078 if (cu->language == language_ada)
9079 {
9080 pdi = pdi->die_child;
9081 while (pdi != NULL)
9082 {
9083 pdi->fixup (cu);
9084 if (pdi->tag == DW_TAG_subprogram
9085 || pdi->tag == DW_TAG_inlined_subroutine
9086 || pdi->tag == DW_TAG_lexical_block)
9087 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9088 pdi = pdi->die_sibling;
9089 }
9090 }
9091 }
9092
9093 /* Read a partial die corresponding to an enumeration type. */
9094
9095 static void
9096 add_partial_enumeration (struct partial_die_info *enum_pdi,
9097 struct dwarf2_cu *cu)
9098 {
9099 struct partial_die_info *pdi;
9100
9101 if (enum_pdi->name != NULL)
9102 add_partial_symbol (enum_pdi, cu);
9103
9104 pdi = enum_pdi->die_child;
9105 while (pdi)
9106 {
9107 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
9108 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
9109 else
9110 add_partial_symbol (pdi, cu);
9111 pdi = pdi->die_sibling;
9112 }
9113 }
9114
9115 /* Return the initial uleb128 in the die at INFO_PTR. */
9116
9117 static unsigned int
9118 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
9119 {
9120 unsigned int bytes_read;
9121
9122 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9123 }
9124
9125 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9126 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9127
9128 Return the corresponding abbrev, or NULL if the number is zero (indicating
9129 an empty DIE). In either case *BYTES_READ will be set to the length of
9130 the initial number. */
9131
9132 static struct abbrev_info *
9133 peek_die_abbrev (const die_reader_specs &reader,
9134 const gdb_byte *info_ptr, unsigned int *bytes_read)
9135 {
9136 dwarf2_cu *cu = reader.cu;
9137 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
9138 unsigned int abbrev_number
9139 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
9140
9141 if (abbrev_number == 0)
9142 return NULL;
9143
9144 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
9145 if (!abbrev)
9146 {
9147 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9148 " at offset %s [in module %s]"),
9149 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9150 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
9151 }
9152
9153 return abbrev;
9154 }
9155
9156 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9157 Returns a pointer to the end of a series of DIEs, terminated by an empty
9158 DIE. Any children of the skipped DIEs will also be skipped. */
9159
9160 static const gdb_byte *
9161 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
9162 {
9163 while (1)
9164 {
9165 unsigned int bytes_read;
9166 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9167
9168 if (abbrev == NULL)
9169 return info_ptr + bytes_read;
9170 else
9171 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
9172 }
9173 }
9174
9175 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9176 INFO_PTR should point just after the initial uleb128 of a DIE, and the
9177 abbrev corresponding to that skipped uleb128 should be passed in
9178 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9179 children. */
9180
9181 static const gdb_byte *
9182 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
9183 struct abbrev_info *abbrev)
9184 {
9185 unsigned int bytes_read;
9186 struct attribute attr;
9187 bfd *abfd = reader->abfd;
9188 struct dwarf2_cu *cu = reader->cu;
9189 const gdb_byte *buffer = reader->buffer;
9190 const gdb_byte *buffer_end = reader->buffer_end;
9191 unsigned int form, i;
9192
9193 for (i = 0; i < abbrev->num_attrs; i++)
9194 {
9195 /* The only abbrev we care about is DW_AT_sibling. */
9196 if (abbrev->attrs[i].name == DW_AT_sibling)
9197 {
9198 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
9199 if (attr.form == DW_FORM_ref_addr)
9200 complaint (&symfile_complaints,
9201 _("ignoring absolute DW_AT_sibling"));
9202 else
9203 {
9204 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9205 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
9206
9207 if (sibling_ptr < info_ptr)
9208 complaint (&symfile_complaints,
9209 _("DW_AT_sibling points backwards"));
9210 else if (sibling_ptr > reader->buffer_end)
9211 dwarf2_section_buffer_overflow_complaint (reader->die_section);
9212 else
9213 return sibling_ptr;
9214 }
9215 }
9216
9217 /* If it isn't DW_AT_sibling, skip this attribute. */
9218 form = abbrev->attrs[i].form;
9219 skip_attribute:
9220 switch (form)
9221 {
9222 case DW_FORM_ref_addr:
9223 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9224 and later it is offset sized. */
9225 if (cu->header.version == 2)
9226 info_ptr += cu->header.addr_size;
9227 else
9228 info_ptr += cu->header.offset_size;
9229 break;
9230 case DW_FORM_GNU_ref_alt:
9231 info_ptr += cu->header.offset_size;
9232 break;
9233 case DW_FORM_addr:
9234 info_ptr += cu->header.addr_size;
9235 break;
9236 case DW_FORM_data1:
9237 case DW_FORM_ref1:
9238 case DW_FORM_flag:
9239 info_ptr += 1;
9240 break;
9241 case DW_FORM_flag_present:
9242 case DW_FORM_implicit_const:
9243 break;
9244 case DW_FORM_data2:
9245 case DW_FORM_ref2:
9246 info_ptr += 2;
9247 break;
9248 case DW_FORM_data4:
9249 case DW_FORM_ref4:
9250 info_ptr += 4;
9251 break;
9252 case DW_FORM_data8:
9253 case DW_FORM_ref8:
9254 case DW_FORM_ref_sig8:
9255 info_ptr += 8;
9256 break;
9257 case DW_FORM_data16:
9258 info_ptr += 16;
9259 break;
9260 case DW_FORM_string:
9261 read_direct_string (abfd, info_ptr, &bytes_read);
9262 info_ptr += bytes_read;
9263 break;
9264 case DW_FORM_sec_offset:
9265 case DW_FORM_strp:
9266 case DW_FORM_GNU_strp_alt:
9267 info_ptr += cu->header.offset_size;
9268 break;
9269 case DW_FORM_exprloc:
9270 case DW_FORM_block:
9271 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9272 info_ptr += bytes_read;
9273 break;
9274 case DW_FORM_block1:
9275 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9276 break;
9277 case DW_FORM_block2:
9278 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9279 break;
9280 case DW_FORM_block4:
9281 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9282 break;
9283 case DW_FORM_sdata:
9284 case DW_FORM_udata:
9285 case DW_FORM_ref_udata:
9286 case DW_FORM_GNU_addr_index:
9287 case DW_FORM_GNU_str_index:
9288 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9289 break;
9290 case DW_FORM_indirect:
9291 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9292 info_ptr += bytes_read;
9293 /* We need to continue parsing from here, so just go back to
9294 the top. */
9295 goto skip_attribute;
9296
9297 default:
9298 error (_("Dwarf Error: Cannot handle %s "
9299 "in DWARF reader [in module %s]"),
9300 dwarf_form_name (form),
9301 bfd_get_filename (abfd));
9302 }
9303 }
9304
9305 if (abbrev->has_children)
9306 return skip_children (reader, info_ptr);
9307 else
9308 return info_ptr;
9309 }
9310
9311 /* Locate ORIG_PDI's sibling.
9312 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9313
9314 static const gdb_byte *
9315 locate_pdi_sibling (const struct die_reader_specs *reader,
9316 struct partial_die_info *orig_pdi,
9317 const gdb_byte *info_ptr)
9318 {
9319 /* Do we know the sibling already? */
9320
9321 if (orig_pdi->sibling)
9322 return orig_pdi->sibling;
9323
9324 /* Are there any children to deal with? */
9325
9326 if (!orig_pdi->has_children)
9327 return info_ptr;
9328
9329 /* Skip the children the long way. */
9330
9331 return skip_children (reader, info_ptr);
9332 }
9333
9334 /* Expand this partial symbol table into a full symbol table. SELF is
9335 not NULL. */
9336
9337 static void
9338 dwarf2_read_symtab (struct partial_symtab *self,
9339 struct objfile *objfile)
9340 {
9341 struct dwarf2_per_objfile *dwarf2_per_objfile
9342 = get_dwarf2_per_objfile (objfile);
9343
9344 if (self->readin)
9345 {
9346 warning (_("bug: psymtab for %s is already read in."),
9347 self->filename);
9348 }
9349 else
9350 {
9351 if (info_verbose)
9352 {
9353 printf_filtered (_("Reading in symbols for %s..."),
9354 self->filename);
9355 gdb_flush (gdb_stdout);
9356 }
9357
9358 /* If this psymtab is constructed from a debug-only objfile, the
9359 has_section_at_zero flag will not necessarily be correct. We
9360 can get the correct value for this flag by looking at the data
9361 associated with the (presumably stripped) associated objfile. */
9362 if (objfile->separate_debug_objfile_backlink)
9363 {
9364 struct dwarf2_per_objfile *dpo_backlink
9365 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9366
9367 dwarf2_per_objfile->has_section_at_zero
9368 = dpo_backlink->has_section_at_zero;
9369 }
9370
9371 dwarf2_per_objfile->reading_partial_symbols = 0;
9372
9373 psymtab_to_symtab_1 (self);
9374
9375 /* Finish up the debug error message. */
9376 if (info_verbose)
9377 printf_filtered (_("done.\n"));
9378 }
9379
9380 process_cu_includes (dwarf2_per_objfile);
9381 }
9382 \f
9383 /* Reading in full CUs. */
9384
9385 /* Add PER_CU to the queue. */
9386
9387 static void
9388 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9389 enum language pretend_language)
9390 {
9391 struct dwarf2_queue_item *item;
9392
9393 per_cu->queued = 1;
9394 item = XNEW (struct dwarf2_queue_item);
9395 item->per_cu = per_cu;
9396 item->pretend_language = pretend_language;
9397 item->next = NULL;
9398
9399 if (dwarf2_queue == NULL)
9400 dwarf2_queue = item;
9401 else
9402 dwarf2_queue_tail->next = item;
9403
9404 dwarf2_queue_tail = item;
9405 }
9406
9407 /* If PER_CU is not yet queued, add it to the queue.
9408 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9409 dependency.
9410 The result is non-zero if PER_CU was queued, otherwise the result is zero
9411 meaning either PER_CU is already queued or it is already loaded.
9412
9413 N.B. There is an invariant here that if a CU is queued then it is loaded.
9414 The caller is required to load PER_CU if we return non-zero. */
9415
9416 static int
9417 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9418 struct dwarf2_per_cu_data *per_cu,
9419 enum language pretend_language)
9420 {
9421 /* We may arrive here during partial symbol reading, if we need full
9422 DIEs to process an unusual case (e.g. template arguments). Do
9423 not queue PER_CU, just tell our caller to load its DIEs. */
9424 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
9425 {
9426 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9427 return 1;
9428 return 0;
9429 }
9430
9431 /* Mark the dependence relation so that we don't flush PER_CU
9432 too early. */
9433 if (dependent_cu != NULL)
9434 dwarf2_add_dependence (dependent_cu, per_cu);
9435
9436 /* If it's already on the queue, we have nothing to do. */
9437 if (per_cu->queued)
9438 return 0;
9439
9440 /* If the compilation unit is already loaded, just mark it as
9441 used. */
9442 if (per_cu->cu != NULL)
9443 {
9444 per_cu->cu->last_used = 0;
9445 return 0;
9446 }
9447
9448 /* Add it to the queue. */
9449 queue_comp_unit (per_cu, pretend_language);
9450
9451 return 1;
9452 }
9453
9454 /* Process the queue. */
9455
9456 static void
9457 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
9458 {
9459 struct dwarf2_queue_item *item, *next_item;
9460
9461 if (dwarf_read_debug)
9462 {
9463 fprintf_unfiltered (gdb_stdlog,
9464 "Expanding one or more symtabs of objfile %s ...\n",
9465 objfile_name (dwarf2_per_objfile->objfile));
9466 }
9467
9468 /* The queue starts out with one item, but following a DIE reference
9469 may load a new CU, adding it to the end of the queue. */
9470 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9471 {
9472 if ((dwarf2_per_objfile->using_index
9473 ? !item->per_cu->v.quick->compunit_symtab
9474 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9475 /* Skip dummy CUs. */
9476 && item->per_cu->cu != NULL)
9477 {
9478 struct dwarf2_per_cu_data *per_cu = item->per_cu;
9479 unsigned int debug_print_threshold;
9480 char buf[100];
9481
9482 if (per_cu->is_debug_types)
9483 {
9484 struct signatured_type *sig_type =
9485 (struct signatured_type *) per_cu;
9486
9487 sprintf (buf, "TU %s at offset %s",
9488 hex_string (sig_type->signature),
9489 sect_offset_str (per_cu->sect_off));
9490 /* There can be 100s of TUs.
9491 Only print them in verbose mode. */
9492 debug_print_threshold = 2;
9493 }
9494 else
9495 {
9496 sprintf (buf, "CU at offset %s",
9497 sect_offset_str (per_cu->sect_off));
9498 debug_print_threshold = 1;
9499 }
9500
9501 if (dwarf_read_debug >= debug_print_threshold)
9502 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9503
9504 if (per_cu->is_debug_types)
9505 process_full_type_unit (per_cu, item->pretend_language);
9506 else
9507 process_full_comp_unit (per_cu, item->pretend_language);
9508
9509 if (dwarf_read_debug >= debug_print_threshold)
9510 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9511 }
9512
9513 item->per_cu->queued = 0;
9514 next_item = item->next;
9515 xfree (item);
9516 }
9517
9518 dwarf2_queue_tail = NULL;
9519
9520 if (dwarf_read_debug)
9521 {
9522 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9523 objfile_name (dwarf2_per_objfile->objfile));
9524 }
9525 }
9526
9527 /* Read in full symbols for PST, and anything it depends on. */
9528
9529 static void
9530 psymtab_to_symtab_1 (struct partial_symtab *pst)
9531 {
9532 struct dwarf2_per_cu_data *per_cu;
9533 int i;
9534
9535 if (pst->readin)
9536 return;
9537
9538 for (i = 0; i < pst->number_of_dependencies; i++)
9539 if (!pst->dependencies[i]->readin
9540 && pst->dependencies[i]->user == NULL)
9541 {
9542 /* Inform about additional files that need to be read in. */
9543 if (info_verbose)
9544 {
9545 /* FIXME: i18n: Need to make this a single string. */
9546 fputs_filtered (" ", gdb_stdout);
9547 wrap_here ("");
9548 fputs_filtered ("and ", gdb_stdout);
9549 wrap_here ("");
9550 printf_filtered ("%s...", pst->dependencies[i]->filename);
9551 wrap_here (""); /* Flush output. */
9552 gdb_flush (gdb_stdout);
9553 }
9554 psymtab_to_symtab_1 (pst->dependencies[i]);
9555 }
9556
9557 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
9558
9559 if (per_cu == NULL)
9560 {
9561 /* It's an include file, no symbols to read for it.
9562 Everything is in the parent symtab. */
9563 pst->readin = 1;
9564 return;
9565 }
9566
9567 dw2_do_instantiate_symtab (per_cu, false);
9568 }
9569
9570 /* Trivial hash function for die_info: the hash value of a DIE
9571 is its offset in .debug_info for this objfile. */
9572
9573 static hashval_t
9574 die_hash (const void *item)
9575 {
9576 const struct die_info *die = (const struct die_info *) item;
9577
9578 return to_underlying (die->sect_off);
9579 }
9580
9581 /* Trivial comparison function for die_info structures: two DIEs
9582 are equal if they have the same offset. */
9583
9584 static int
9585 die_eq (const void *item_lhs, const void *item_rhs)
9586 {
9587 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9588 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9589
9590 return die_lhs->sect_off == die_rhs->sect_off;
9591 }
9592
9593 /* die_reader_func for load_full_comp_unit.
9594 This is identical to read_signatured_type_reader,
9595 but is kept separate for now. */
9596
9597 static void
9598 load_full_comp_unit_reader (const struct die_reader_specs *reader,
9599 const gdb_byte *info_ptr,
9600 struct die_info *comp_unit_die,
9601 int has_children,
9602 void *data)
9603 {
9604 struct dwarf2_cu *cu = reader->cu;
9605 enum language *language_ptr = (enum language *) data;
9606
9607 gdb_assert (cu->die_hash == NULL);
9608 cu->die_hash =
9609 htab_create_alloc_ex (cu->header.length / 12,
9610 die_hash,
9611 die_eq,
9612 NULL,
9613 &cu->comp_unit_obstack,
9614 hashtab_obstack_allocate,
9615 dummy_obstack_deallocate);
9616
9617 if (has_children)
9618 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
9619 &info_ptr, comp_unit_die);
9620 cu->dies = comp_unit_die;
9621 /* comp_unit_die is not stored in die_hash, no need. */
9622
9623 /* We try not to read any attributes in this function, because not
9624 all CUs needed for references have been loaded yet, and symbol
9625 table processing isn't initialized. But we have to set the CU language,
9626 or we won't be able to build types correctly.
9627 Similarly, if we do not read the producer, we can not apply
9628 producer-specific interpretation. */
9629 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
9630 }
9631
9632 /* Load the DIEs associated with PER_CU into memory. */
9633
9634 static void
9635 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
9636 bool skip_partial,
9637 enum language pretend_language)
9638 {
9639 gdb_assert (! this_cu->is_debug_types);
9640
9641 init_cutu_and_read_dies (this_cu, NULL, 1, 1, skip_partial,
9642 load_full_comp_unit_reader, &pretend_language);
9643 }
9644
9645 /* Add a DIE to the delayed physname list. */
9646
9647 static void
9648 add_to_method_list (struct type *type, int fnfield_index, int index,
9649 const char *name, struct die_info *die,
9650 struct dwarf2_cu *cu)
9651 {
9652 struct delayed_method_info mi;
9653 mi.type = type;
9654 mi.fnfield_index = fnfield_index;
9655 mi.index = index;
9656 mi.name = name;
9657 mi.die = die;
9658 cu->method_list.push_back (mi);
9659 }
9660
9661 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9662 "const" / "volatile". If so, decrements LEN by the length of the
9663 modifier and return true. Otherwise return false. */
9664
9665 template<size_t N>
9666 static bool
9667 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9668 {
9669 size_t mod_len = sizeof (mod) - 1;
9670 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9671 {
9672 len -= mod_len;
9673 return true;
9674 }
9675 return false;
9676 }
9677
9678 /* Compute the physnames of any methods on the CU's method list.
9679
9680 The computation of method physnames is delayed in order to avoid the
9681 (bad) condition that one of the method's formal parameters is of an as yet
9682 incomplete type. */
9683
9684 static void
9685 compute_delayed_physnames (struct dwarf2_cu *cu)
9686 {
9687 /* Only C++ delays computing physnames. */
9688 if (cu->method_list.empty ())
9689 return;
9690 gdb_assert (cu->language == language_cplus);
9691
9692 for (struct delayed_method_info &mi : cu->method_list)
9693 {
9694 const char *physname;
9695 struct fn_fieldlist *fn_flp
9696 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9697 physname = dwarf2_physname (mi.name, mi.die, cu);
9698 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9699 = physname ? physname : "";
9700
9701 /* Since there's no tag to indicate whether a method is a
9702 const/volatile overload, extract that information out of the
9703 demangled name. */
9704 if (physname != NULL)
9705 {
9706 size_t len = strlen (physname);
9707
9708 while (1)
9709 {
9710 if (physname[len] == ')') /* shortcut */
9711 break;
9712 else if (check_modifier (physname, len, " const"))
9713 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9714 else if (check_modifier (physname, len, " volatile"))
9715 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9716 else
9717 break;
9718 }
9719 }
9720 }
9721
9722 /* The list is no longer needed. */
9723 cu->method_list.clear ();
9724 }
9725
9726 /* Go objects should be embedded in a DW_TAG_module DIE,
9727 and it's not clear if/how imported objects will appear.
9728 To keep Go support simple until that's worked out,
9729 go back through what we've read and create something usable.
9730 We could do this while processing each DIE, and feels kinda cleaner,
9731 but that way is more invasive.
9732 This is to, for example, allow the user to type "p var" or "b main"
9733 without having to specify the package name, and allow lookups
9734 of module.object to work in contexts that use the expression
9735 parser. */
9736
9737 static void
9738 fixup_go_packaging (struct dwarf2_cu *cu)
9739 {
9740 char *package_name = NULL;
9741 struct pending *list;
9742 int i;
9743
9744 for (list = global_symbols; list != NULL; list = list->next)
9745 {
9746 for (i = 0; i < list->nsyms; ++i)
9747 {
9748 struct symbol *sym = list->symbol[i];
9749
9750 if (SYMBOL_LANGUAGE (sym) == language_go
9751 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9752 {
9753 char *this_package_name = go_symbol_package_name (sym);
9754
9755 if (this_package_name == NULL)
9756 continue;
9757 if (package_name == NULL)
9758 package_name = this_package_name;
9759 else
9760 {
9761 struct objfile *objfile
9762 = cu->per_cu->dwarf2_per_objfile->objfile;
9763 if (strcmp (package_name, this_package_name) != 0)
9764 complaint (&symfile_complaints,
9765 _("Symtab %s has objects from two different Go packages: %s and %s"),
9766 (symbol_symtab (sym) != NULL
9767 ? symtab_to_filename_for_display
9768 (symbol_symtab (sym))
9769 : objfile_name (objfile)),
9770 this_package_name, package_name);
9771 xfree (this_package_name);
9772 }
9773 }
9774 }
9775 }
9776
9777 if (package_name != NULL)
9778 {
9779 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9780 const char *saved_package_name
9781 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
9782 package_name,
9783 strlen (package_name));
9784 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9785 saved_package_name);
9786 struct symbol *sym;
9787
9788 TYPE_TAG_NAME (type) = TYPE_NAME (type);
9789
9790 sym = allocate_symbol (objfile);
9791 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
9792 SYMBOL_SET_NAMES (sym, saved_package_name,
9793 strlen (saved_package_name), 0, objfile);
9794 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9795 e.g., "main" finds the "main" module and not C's main(). */
9796 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9797 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9798 SYMBOL_TYPE (sym) = type;
9799
9800 add_symbol_to_list (sym, &global_symbols);
9801
9802 xfree (package_name);
9803 }
9804 }
9805
9806 /* Allocate a fully-qualified name consisting of the two parts on the
9807 obstack. */
9808
9809 static const char *
9810 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9811 {
9812 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9813 }
9814
9815 /* A helper that allocates a struct discriminant_info to attach to a
9816 union type. */
9817
9818 static struct discriminant_info *
9819 alloc_discriminant_info (struct type *type, int discriminant_index,
9820 int default_index)
9821 {
9822 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9823 gdb_assert (discriminant_index == -1
9824 || (discriminant_index >= 0
9825 && discriminant_index < TYPE_NFIELDS (type)));
9826 gdb_assert (default_index == -1
9827 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9828
9829 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9830
9831 struct discriminant_info *disc
9832 = ((struct discriminant_info *)
9833 TYPE_ZALLOC (type,
9834 offsetof (struct discriminant_info, discriminants)
9835 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9836 disc->default_index = default_index;
9837 disc->discriminant_index = discriminant_index;
9838
9839 struct dynamic_prop prop;
9840 prop.kind = PROP_UNDEFINED;
9841 prop.data.baton = disc;
9842
9843 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9844
9845 return disc;
9846 }
9847
9848 /* Some versions of rustc emitted enums in an unusual way.
9849
9850 Ordinary enums were emitted as unions. The first element of each
9851 structure in the union was named "RUST$ENUM$DISR". This element
9852 held the discriminant.
9853
9854 These versions of Rust also implemented the "non-zero"
9855 optimization. When the enum had two values, and one is empty and
9856 the other holds a pointer that cannot be zero, the pointer is used
9857 as the discriminant, with a zero value meaning the empty variant.
9858 Here, the union's first member is of the form
9859 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9860 where the fieldnos are the indices of the fields that should be
9861 traversed in order to find the field (which may be several fields deep)
9862 and the variantname is the name of the variant of the case when the
9863 field is zero.
9864
9865 This function recognizes whether TYPE is of one of these forms,
9866 and, if so, smashes it to be a variant type. */
9867
9868 static void
9869 quirk_rust_enum (struct type *type, struct objfile *objfile)
9870 {
9871 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9872
9873 /* We don't need to deal with empty enums. */
9874 if (TYPE_NFIELDS (type) == 0)
9875 return;
9876
9877 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9878 if (TYPE_NFIELDS (type) == 1
9879 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9880 {
9881 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9882
9883 /* Decode the field name to find the offset of the
9884 discriminant. */
9885 ULONGEST bit_offset = 0;
9886 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9887 while (name[0] >= '0' && name[0] <= '9')
9888 {
9889 char *tail;
9890 unsigned long index = strtoul (name, &tail, 10);
9891 name = tail;
9892 if (*name != '$'
9893 || index >= TYPE_NFIELDS (field_type)
9894 || (TYPE_FIELD_LOC_KIND (field_type, index)
9895 != FIELD_LOC_KIND_BITPOS))
9896 {
9897 complaint (&symfile_complaints,
9898 _("Could not parse Rust enum encoding string \"%s\""
9899 "[in module %s]"),
9900 TYPE_FIELD_NAME (type, 0),
9901 objfile_name (objfile));
9902 return;
9903 }
9904 ++name;
9905
9906 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9907 field_type = TYPE_FIELD_TYPE (field_type, index);
9908 }
9909
9910 /* Make a union to hold the variants. */
9911 struct type *union_type = alloc_type (objfile);
9912 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9913 TYPE_NFIELDS (union_type) = 3;
9914 TYPE_FIELDS (union_type)
9915 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9916 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9917 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9918
9919 /* Put the discriminant must at index 0. */
9920 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9921 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9922 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9923 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9924
9925 /* The order of fields doesn't really matter, so put the real
9926 field at index 1 and the data-less field at index 2. */
9927 struct discriminant_info *disc
9928 = alloc_discriminant_info (union_type, 0, 1);
9929 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9930 TYPE_FIELD_NAME (union_type, 1)
9931 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9932 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9933 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9934 TYPE_FIELD_NAME (union_type, 1));
9935
9936 const char *dataless_name
9937 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9938 name);
9939 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9940 dataless_name);
9941 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9942 /* NAME points into the original discriminant name, which
9943 already has the correct lifetime. */
9944 TYPE_FIELD_NAME (union_type, 2) = name;
9945 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
9946 disc->discriminants[2] = 0;
9947
9948 /* Smash this type to be a structure type. We have to do this
9949 because the type has already been recorded. */
9950 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9951 TYPE_NFIELDS (type) = 1;
9952 TYPE_FIELDS (type)
9953 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
9954
9955 /* Install the variant part. */
9956 TYPE_FIELD_TYPE (type, 0) = union_type;
9957 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9958 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9959 }
9960 else if (TYPE_NFIELDS (type) == 1)
9961 {
9962 /* We assume that a union with a single field is a univariant
9963 enum. */
9964 /* Smash this type to be a structure type. We have to do this
9965 because the type has already been recorded. */
9966 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9967
9968 /* Make a union to hold the variants. */
9969 struct type *union_type = alloc_type (objfile);
9970 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9971 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
9972 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9973 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9974 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
9975
9976 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
9977 const char *variant_name
9978 = rust_last_path_segment (TYPE_NAME (field_type));
9979 TYPE_FIELD_NAME (union_type, 0) = variant_name;
9980 TYPE_NAME (field_type)
9981 = rust_fully_qualify (&objfile->objfile_obstack,
9982 TYPE_NAME (type), variant_name);
9983
9984 /* Install the union in the outer struct type. */
9985 TYPE_NFIELDS (type) = 1;
9986 TYPE_FIELDS (type)
9987 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
9988 TYPE_FIELD_TYPE (type, 0) = union_type;
9989 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9990 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9991
9992 alloc_discriminant_info (union_type, -1, 0);
9993 }
9994 else
9995 {
9996 struct type *disr_type = nullptr;
9997 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9998 {
9999 disr_type = TYPE_FIELD_TYPE (type, i);
10000
10001 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
10002 {
10003 /* All fields of a true enum will be structs. */
10004 return;
10005 }
10006 else if (TYPE_NFIELDS (disr_type) == 0)
10007 {
10008 /* Could be data-less variant, so keep going. */
10009 disr_type = nullptr;
10010 }
10011 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10012 "RUST$ENUM$DISR") != 0)
10013 {
10014 /* Not a Rust enum. */
10015 return;
10016 }
10017 else
10018 {
10019 /* Found one. */
10020 break;
10021 }
10022 }
10023
10024 /* If we got here without a discriminant, then it's probably
10025 just a union. */
10026 if (disr_type == nullptr)
10027 return;
10028
10029 /* Smash this type to be a structure type. We have to do this
10030 because the type has already been recorded. */
10031 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10032
10033 /* Make a union to hold the variants. */
10034 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10035 struct type *union_type = alloc_type (objfile);
10036 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10037 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10038 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10039 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10040 TYPE_FIELDS (union_type)
10041 = (struct field *) TYPE_ZALLOC (union_type,
10042 (TYPE_NFIELDS (union_type)
10043 * sizeof (struct field)));
10044
10045 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10046 TYPE_NFIELDS (type) * sizeof (struct field));
10047
10048 /* Install the discriminant at index 0 in the union. */
10049 TYPE_FIELD (union_type, 0) = *disr_field;
10050 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10051 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10052
10053 /* Install the union in the outer struct type. */
10054 TYPE_FIELD_TYPE (type, 0) = union_type;
10055 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10056 TYPE_NFIELDS (type) = 1;
10057
10058 /* Set the size and offset of the union type. */
10059 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10060
10061 /* We need a way to find the correct discriminant given a
10062 variant name. For convenience we build a map here. */
10063 struct type *enum_type = FIELD_TYPE (*disr_field);
10064 std::unordered_map<std::string, ULONGEST> discriminant_map;
10065 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10066 {
10067 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10068 {
10069 const char *name
10070 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10071 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10072 }
10073 }
10074
10075 int n_fields = TYPE_NFIELDS (union_type);
10076 struct discriminant_info *disc
10077 = alloc_discriminant_info (union_type, 0, -1);
10078 /* Skip the discriminant here. */
10079 for (int i = 1; i < n_fields; ++i)
10080 {
10081 /* Find the final word in the name of this variant's type.
10082 That name can be used to look up the correct
10083 discriminant. */
10084 const char *variant_name
10085 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10086 i)));
10087
10088 auto iter = discriminant_map.find (variant_name);
10089 if (iter != discriminant_map.end ())
10090 disc->discriminants[i] = iter->second;
10091
10092 /* Remove the discriminant field, if it exists. */
10093 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
10094 if (TYPE_NFIELDS (sub_type) > 0)
10095 {
10096 --TYPE_NFIELDS (sub_type);
10097 ++TYPE_FIELDS (sub_type);
10098 }
10099 TYPE_FIELD_NAME (union_type, i) = variant_name;
10100 TYPE_NAME (sub_type)
10101 = rust_fully_qualify (&objfile->objfile_obstack,
10102 TYPE_NAME (type), variant_name);
10103 }
10104 }
10105 }
10106
10107 /* Rewrite some Rust unions to be structures with variants parts. */
10108
10109 static void
10110 rust_union_quirks (struct dwarf2_cu *cu)
10111 {
10112 gdb_assert (cu->language == language_rust);
10113 for (struct type *type : cu->rust_unions)
10114 quirk_rust_enum (type, cu->per_cu->dwarf2_per_objfile->objfile);
10115 /* We don't need this any more. */
10116 cu->rust_unions.clear ();
10117 }
10118
10119 /* Return the symtab for PER_CU. This works properly regardless of
10120 whether we're using the index or psymtabs. */
10121
10122 static struct compunit_symtab *
10123 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
10124 {
10125 return (per_cu->dwarf2_per_objfile->using_index
10126 ? per_cu->v.quick->compunit_symtab
10127 : per_cu->v.psymtab->compunit_symtab);
10128 }
10129
10130 /* A helper function for computing the list of all symbol tables
10131 included by PER_CU. */
10132
10133 static void
10134 recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
10135 htab_t all_children, htab_t all_type_symtabs,
10136 struct dwarf2_per_cu_data *per_cu,
10137 struct compunit_symtab *immediate_parent)
10138 {
10139 void **slot;
10140 int ix;
10141 struct compunit_symtab *cust;
10142 struct dwarf2_per_cu_data *iter;
10143
10144 slot = htab_find_slot (all_children, per_cu, INSERT);
10145 if (*slot != NULL)
10146 {
10147 /* This inclusion and its children have been processed. */
10148 return;
10149 }
10150
10151 *slot = per_cu;
10152 /* Only add a CU if it has a symbol table. */
10153 cust = get_compunit_symtab (per_cu);
10154 if (cust != NULL)
10155 {
10156 /* If this is a type unit only add its symbol table if we haven't
10157 seen it yet (type unit per_cu's can share symtabs). */
10158 if (per_cu->is_debug_types)
10159 {
10160 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
10161 if (*slot == NULL)
10162 {
10163 *slot = cust;
10164 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10165 if (cust->user == NULL)
10166 cust->user = immediate_parent;
10167 }
10168 }
10169 else
10170 {
10171 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10172 if (cust->user == NULL)
10173 cust->user = immediate_parent;
10174 }
10175 }
10176
10177 for (ix = 0;
10178 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
10179 ++ix)
10180 {
10181 recursively_compute_inclusions (result, all_children,
10182 all_type_symtabs, iter, cust);
10183 }
10184 }
10185
10186 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
10187 PER_CU. */
10188
10189 static void
10190 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
10191 {
10192 gdb_assert (! per_cu->is_debug_types);
10193
10194 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
10195 {
10196 int ix, len;
10197 struct dwarf2_per_cu_data *per_cu_iter;
10198 struct compunit_symtab *compunit_symtab_iter;
10199 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
10200 htab_t all_children, all_type_symtabs;
10201 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
10202
10203 /* If we don't have a symtab, we can just skip this case. */
10204 if (cust == NULL)
10205 return;
10206
10207 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10208 NULL, xcalloc, xfree);
10209 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10210 NULL, xcalloc, xfree);
10211
10212 for (ix = 0;
10213 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
10214 ix, per_cu_iter);
10215 ++ix)
10216 {
10217 recursively_compute_inclusions (&result_symtabs, all_children,
10218 all_type_symtabs, per_cu_iter,
10219 cust);
10220 }
10221
10222 /* Now we have a transitive closure of all the included symtabs. */
10223 len = VEC_length (compunit_symtab_ptr, result_symtabs);
10224 cust->includes
10225 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
10226 struct compunit_symtab *, len + 1);
10227 for (ix = 0;
10228 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
10229 compunit_symtab_iter);
10230 ++ix)
10231 cust->includes[ix] = compunit_symtab_iter;
10232 cust->includes[len] = NULL;
10233
10234 VEC_free (compunit_symtab_ptr, result_symtabs);
10235 htab_delete (all_children);
10236 htab_delete (all_type_symtabs);
10237 }
10238 }
10239
10240 /* Compute the 'includes' field for the symtabs of all the CUs we just
10241 read. */
10242
10243 static void
10244 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
10245 {
10246 int ix;
10247 struct dwarf2_per_cu_data *iter;
10248
10249 for (ix = 0;
10250 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
10251 ix, iter);
10252 ++ix)
10253 {
10254 if (! iter->is_debug_types)
10255 compute_compunit_symtab_includes (iter);
10256 }
10257
10258 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
10259 }
10260
10261 /* Generate full symbol information for PER_CU, whose DIEs have
10262 already been loaded into memory. */
10263
10264 static void
10265 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10266 enum language pretend_language)
10267 {
10268 struct dwarf2_cu *cu = per_cu->cu;
10269 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10270 struct objfile *objfile = dwarf2_per_objfile->objfile;
10271 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10272 CORE_ADDR lowpc, highpc;
10273 struct compunit_symtab *cust;
10274 CORE_ADDR baseaddr;
10275 struct block *static_block;
10276 CORE_ADDR addr;
10277
10278 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10279
10280 buildsym_init ();
10281 scoped_free_pendings free_pending;
10282
10283 /* Clear the list here in case something was left over. */
10284 cu->method_list.clear ();
10285
10286 cu->list_in_scope = &file_symbols;
10287
10288 cu->language = pretend_language;
10289 cu->language_defn = language_def (cu->language);
10290
10291 /* Do line number decoding in read_file_scope () */
10292 process_die (cu->dies, cu);
10293
10294 /* For now fudge the Go package. */
10295 if (cu->language == language_go)
10296 fixup_go_packaging (cu);
10297
10298 /* Now that we have processed all the DIEs in the CU, all the types
10299 should be complete, and it should now be safe to compute all of the
10300 physnames. */
10301 compute_delayed_physnames (cu);
10302
10303 if (cu->language == language_rust)
10304 rust_union_quirks (cu);
10305
10306 /* Some compilers don't define a DW_AT_high_pc attribute for the
10307 compilation unit. If the DW_AT_high_pc is missing, synthesize
10308 it, by scanning the DIE's below the compilation unit. */
10309 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10310
10311 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10312 static_block = end_symtab_get_static_block (addr, 0, 1);
10313
10314 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10315 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10316 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10317 addrmap to help ensure it has an accurate map of pc values belonging to
10318 this comp unit. */
10319 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10320
10321 cust = end_symtab_from_static_block (static_block,
10322 SECT_OFF_TEXT (objfile), 0);
10323
10324 if (cust != NULL)
10325 {
10326 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10327
10328 /* Set symtab language to language from DW_AT_language. If the
10329 compilation is from a C file generated by language preprocessors, do
10330 not set the language if it was already deduced by start_subfile. */
10331 if (!(cu->language == language_c
10332 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10333 COMPUNIT_FILETABS (cust)->language = cu->language;
10334
10335 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10336 produce DW_AT_location with location lists but it can be possibly
10337 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10338 there were bugs in prologue debug info, fixed later in GCC-4.5
10339 by "unwind info for epilogues" patch (which is not directly related).
10340
10341 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10342 needed, it would be wrong due to missing DW_AT_producer there.
10343
10344 Still one can confuse GDB by using non-standard GCC compilation
10345 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10346 */
10347 if (cu->has_loclist && gcc_4_minor >= 5)
10348 cust->locations_valid = 1;
10349
10350 if (gcc_4_minor >= 5)
10351 cust->epilogue_unwind_valid = 1;
10352
10353 cust->call_site_htab = cu->call_site_htab;
10354 }
10355
10356 if (dwarf2_per_objfile->using_index)
10357 per_cu->v.quick->compunit_symtab = cust;
10358 else
10359 {
10360 struct partial_symtab *pst = per_cu->v.psymtab;
10361 pst->compunit_symtab = cust;
10362 pst->readin = 1;
10363 }
10364
10365 /* Push it for inclusion processing later. */
10366 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
10367 }
10368
10369 /* Generate full symbol information for type unit PER_CU, whose DIEs have
10370 already been loaded into memory. */
10371
10372 static void
10373 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10374 enum language pretend_language)
10375 {
10376 struct dwarf2_cu *cu = per_cu->cu;
10377 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10378 struct objfile *objfile = dwarf2_per_objfile->objfile;
10379 struct compunit_symtab *cust;
10380 struct signatured_type *sig_type;
10381
10382 gdb_assert (per_cu->is_debug_types);
10383 sig_type = (struct signatured_type *) per_cu;
10384
10385 buildsym_init ();
10386 scoped_free_pendings free_pending;
10387
10388 /* Clear the list here in case something was left over. */
10389 cu->method_list.clear ();
10390
10391 cu->list_in_scope = &file_symbols;
10392
10393 cu->language = pretend_language;
10394 cu->language_defn = language_def (cu->language);
10395
10396 /* The symbol tables are set up in read_type_unit_scope. */
10397 process_die (cu->dies, cu);
10398
10399 /* For now fudge the Go package. */
10400 if (cu->language == language_go)
10401 fixup_go_packaging (cu);
10402
10403 /* Now that we have processed all the DIEs in the CU, all the types
10404 should be complete, and it should now be safe to compute all of the
10405 physnames. */
10406 compute_delayed_physnames (cu);
10407
10408 if (cu->language == language_rust)
10409 rust_union_quirks (cu);
10410
10411 /* TUs share symbol tables.
10412 If this is the first TU to use this symtab, complete the construction
10413 of it with end_expandable_symtab. Otherwise, complete the addition of
10414 this TU's symbols to the existing symtab. */
10415 if (sig_type->type_unit_group->compunit_symtab == NULL)
10416 {
10417 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10418 sig_type->type_unit_group->compunit_symtab = cust;
10419
10420 if (cust != NULL)
10421 {
10422 /* Set symtab language to language from DW_AT_language. If the
10423 compilation is from a C file generated by language preprocessors,
10424 do not set the language if it was already deduced by
10425 start_subfile. */
10426 if (!(cu->language == language_c
10427 && COMPUNIT_FILETABS (cust)->language != language_c))
10428 COMPUNIT_FILETABS (cust)->language = cu->language;
10429 }
10430 }
10431 else
10432 {
10433 augment_type_symtab ();
10434 cust = sig_type->type_unit_group->compunit_symtab;
10435 }
10436
10437 if (dwarf2_per_objfile->using_index)
10438 per_cu->v.quick->compunit_symtab = cust;
10439 else
10440 {
10441 struct partial_symtab *pst = per_cu->v.psymtab;
10442 pst->compunit_symtab = cust;
10443 pst->readin = 1;
10444 }
10445 }
10446
10447 /* Process an imported unit DIE. */
10448
10449 static void
10450 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10451 {
10452 struct attribute *attr;
10453
10454 /* For now we don't handle imported units in type units. */
10455 if (cu->per_cu->is_debug_types)
10456 {
10457 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10458 " supported in type units [in module %s]"),
10459 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
10460 }
10461
10462 attr = dwarf2_attr (die, DW_AT_import, cu);
10463 if (attr != NULL)
10464 {
10465 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10466 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10467 dwarf2_per_cu_data *per_cu
10468 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
10469 cu->per_cu->dwarf2_per_objfile);
10470
10471 /* If necessary, add it to the queue and load its DIEs. */
10472 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10473 load_full_comp_unit (per_cu, false, cu->language);
10474
10475 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
10476 per_cu);
10477 }
10478 }
10479
10480 /* RAII object that represents a process_die scope: i.e.,
10481 starts/finishes processing a DIE. */
10482 class process_die_scope
10483 {
10484 public:
10485 process_die_scope (die_info *die, dwarf2_cu *cu)
10486 : m_die (die), m_cu (cu)
10487 {
10488 /* We should only be processing DIEs not already in process. */
10489 gdb_assert (!m_die->in_process);
10490 m_die->in_process = true;
10491 }
10492
10493 ~process_die_scope ()
10494 {
10495 m_die->in_process = false;
10496
10497 /* If we're done processing the DIE for the CU that owns the line
10498 header, we don't need the line header anymore. */
10499 if (m_cu->line_header_die_owner == m_die)
10500 {
10501 delete m_cu->line_header;
10502 m_cu->line_header = NULL;
10503 m_cu->line_header_die_owner = NULL;
10504 }
10505 }
10506
10507 private:
10508 die_info *m_die;
10509 dwarf2_cu *m_cu;
10510 };
10511
10512 /* Process a die and its children. */
10513
10514 static void
10515 process_die (struct die_info *die, struct dwarf2_cu *cu)
10516 {
10517 process_die_scope scope (die, cu);
10518
10519 switch (die->tag)
10520 {
10521 case DW_TAG_padding:
10522 break;
10523 case DW_TAG_compile_unit:
10524 case DW_TAG_partial_unit:
10525 read_file_scope (die, cu);
10526 break;
10527 case DW_TAG_type_unit:
10528 read_type_unit_scope (die, cu);
10529 break;
10530 case DW_TAG_subprogram:
10531 case DW_TAG_inlined_subroutine:
10532 read_func_scope (die, cu);
10533 break;
10534 case DW_TAG_lexical_block:
10535 case DW_TAG_try_block:
10536 case DW_TAG_catch_block:
10537 read_lexical_block_scope (die, cu);
10538 break;
10539 case DW_TAG_call_site:
10540 case DW_TAG_GNU_call_site:
10541 read_call_site_scope (die, cu);
10542 break;
10543 case DW_TAG_class_type:
10544 case DW_TAG_interface_type:
10545 case DW_TAG_structure_type:
10546 case DW_TAG_union_type:
10547 process_structure_scope (die, cu);
10548 break;
10549 case DW_TAG_enumeration_type:
10550 process_enumeration_scope (die, cu);
10551 break;
10552
10553 /* These dies have a type, but processing them does not create
10554 a symbol or recurse to process the children. Therefore we can
10555 read them on-demand through read_type_die. */
10556 case DW_TAG_subroutine_type:
10557 case DW_TAG_set_type:
10558 case DW_TAG_array_type:
10559 case DW_TAG_pointer_type:
10560 case DW_TAG_ptr_to_member_type:
10561 case DW_TAG_reference_type:
10562 case DW_TAG_rvalue_reference_type:
10563 case DW_TAG_string_type:
10564 break;
10565
10566 case DW_TAG_base_type:
10567 case DW_TAG_subrange_type:
10568 case DW_TAG_typedef:
10569 /* Add a typedef symbol for the type definition, if it has a
10570 DW_AT_name. */
10571 new_symbol (die, read_type_die (die, cu), cu);
10572 break;
10573 case DW_TAG_common_block:
10574 read_common_block (die, cu);
10575 break;
10576 case DW_TAG_common_inclusion:
10577 break;
10578 case DW_TAG_namespace:
10579 cu->processing_has_namespace_info = 1;
10580 read_namespace (die, cu);
10581 break;
10582 case DW_TAG_module:
10583 cu->processing_has_namespace_info = 1;
10584 read_module (die, cu);
10585 break;
10586 case DW_TAG_imported_declaration:
10587 cu->processing_has_namespace_info = 1;
10588 if (read_namespace_alias (die, cu))
10589 break;
10590 /* The declaration is not a global namespace alias. */
10591 /* Fall through. */
10592 case DW_TAG_imported_module:
10593 cu->processing_has_namespace_info = 1;
10594 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10595 || cu->language != language_fortran))
10596 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
10597 dwarf_tag_name (die->tag));
10598 read_import_statement (die, cu);
10599 break;
10600
10601 case DW_TAG_imported_unit:
10602 process_imported_unit_die (die, cu);
10603 break;
10604
10605 case DW_TAG_variable:
10606 read_variable (die, cu);
10607 break;
10608
10609 default:
10610 new_symbol (die, NULL, cu);
10611 break;
10612 }
10613 }
10614 \f
10615 /* DWARF name computation. */
10616
10617 /* A helper function for dwarf2_compute_name which determines whether DIE
10618 needs to have the name of the scope prepended to the name listed in the
10619 die. */
10620
10621 static int
10622 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10623 {
10624 struct attribute *attr;
10625
10626 switch (die->tag)
10627 {
10628 case DW_TAG_namespace:
10629 case DW_TAG_typedef:
10630 case DW_TAG_class_type:
10631 case DW_TAG_interface_type:
10632 case DW_TAG_structure_type:
10633 case DW_TAG_union_type:
10634 case DW_TAG_enumeration_type:
10635 case DW_TAG_enumerator:
10636 case DW_TAG_subprogram:
10637 case DW_TAG_inlined_subroutine:
10638 case DW_TAG_member:
10639 case DW_TAG_imported_declaration:
10640 return 1;
10641
10642 case DW_TAG_variable:
10643 case DW_TAG_constant:
10644 /* We only need to prefix "globally" visible variables. These include
10645 any variable marked with DW_AT_external or any variable that
10646 lives in a namespace. [Variables in anonymous namespaces
10647 require prefixing, but they are not DW_AT_external.] */
10648
10649 if (dwarf2_attr (die, DW_AT_specification, cu))
10650 {
10651 struct dwarf2_cu *spec_cu = cu;
10652
10653 return die_needs_namespace (die_specification (die, &spec_cu),
10654 spec_cu);
10655 }
10656
10657 attr = dwarf2_attr (die, DW_AT_external, cu);
10658 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10659 && die->parent->tag != DW_TAG_module)
10660 return 0;
10661 /* A variable in a lexical block of some kind does not need a
10662 namespace, even though in C++ such variables may be external
10663 and have a mangled name. */
10664 if (die->parent->tag == DW_TAG_lexical_block
10665 || die->parent->tag == DW_TAG_try_block
10666 || die->parent->tag == DW_TAG_catch_block
10667 || die->parent->tag == DW_TAG_subprogram)
10668 return 0;
10669 return 1;
10670
10671 default:
10672 return 0;
10673 }
10674 }
10675
10676 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10677 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10678 defined for the given DIE. */
10679
10680 static struct attribute *
10681 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10682 {
10683 struct attribute *attr;
10684
10685 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10686 if (attr == NULL)
10687 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10688
10689 return attr;
10690 }
10691
10692 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10693 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10694 defined for the given DIE. */
10695
10696 static const char *
10697 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10698 {
10699 const char *linkage_name;
10700
10701 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10702 if (linkage_name == NULL)
10703 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10704
10705 return linkage_name;
10706 }
10707
10708 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10709 compute the physname for the object, which include a method's:
10710 - formal parameters (C++),
10711 - receiver type (Go),
10712
10713 The term "physname" is a bit confusing.
10714 For C++, for example, it is the demangled name.
10715 For Go, for example, it's the mangled name.
10716
10717 For Ada, return the DIE's linkage name rather than the fully qualified
10718 name. PHYSNAME is ignored..
10719
10720 The result is allocated on the objfile_obstack and canonicalized. */
10721
10722 static const char *
10723 dwarf2_compute_name (const char *name,
10724 struct die_info *die, struct dwarf2_cu *cu,
10725 int physname)
10726 {
10727 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10728
10729 if (name == NULL)
10730 name = dwarf2_name (die, cu);
10731
10732 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10733 but otherwise compute it by typename_concat inside GDB.
10734 FIXME: Actually this is not really true, or at least not always true.
10735 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
10736 Fortran names because there is no mangling standard. So new_symbol
10737 will set the demangled name to the result of dwarf2_full_name, and it is
10738 the demangled name that GDB uses if it exists. */
10739 if (cu->language == language_ada
10740 || (cu->language == language_fortran && physname))
10741 {
10742 /* For Ada unit, we prefer the linkage name over the name, as
10743 the former contains the exported name, which the user expects
10744 to be able to reference. Ideally, we want the user to be able
10745 to reference this entity using either natural or linkage name,
10746 but we haven't started looking at this enhancement yet. */
10747 const char *linkage_name = dw2_linkage_name (die, cu);
10748
10749 if (linkage_name != NULL)
10750 return linkage_name;
10751 }
10752
10753 /* These are the only languages we know how to qualify names in. */
10754 if (name != NULL
10755 && (cu->language == language_cplus
10756 || cu->language == language_fortran || cu->language == language_d
10757 || cu->language == language_rust))
10758 {
10759 if (die_needs_namespace (die, cu))
10760 {
10761 const char *prefix;
10762 const char *canonical_name = NULL;
10763
10764 string_file buf;
10765
10766 prefix = determine_prefix (die, cu);
10767 if (*prefix != '\0')
10768 {
10769 char *prefixed_name = typename_concat (NULL, prefix, name,
10770 physname, cu);
10771
10772 buf.puts (prefixed_name);
10773 xfree (prefixed_name);
10774 }
10775 else
10776 buf.puts (name);
10777
10778 /* Template parameters may be specified in the DIE's DW_AT_name, or
10779 as children with DW_TAG_template_type_param or
10780 DW_TAG_value_type_param. If the latter, add them to the name
10781 here. If the name already has template parameters, then
10782 skip this step; some versions of GCC emit both, and
10783 it is more efficient to use the pre-computed name.
10784
10785 Something to keep in mind about this process: it is very
10786 unlikely, or in some cases downright impossible, to produce
10787 something that will match the mangled name of a function.
10788 If the definition of the function has the same debug info,
10789 we should be able to match up with it anyway. But fallbacks
10790 using the minimal symbol, for instance to find a method
10791 implemented in a stripped copy of libstdc++, will not work.
10792 If we do not have debug info for the definition, we will have to
10793 match them up some other way.
10794
10795 When we do name matching there is a related problem with function
10796 templates; two instantiated function templates are allowed to
10797 differ only by their return types, which we do not add here. */
10798
10799 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10800 {
10801 struct attribute *attr;
10802 struct die_info *child;
10803 int first = 1;
10804
10805 die->building_fullname = 1;
10806
10807 for (child = die->child; child != NULL; child = child->sibling)
10808 {
10809 struct type *type;
10810 LONGEST value;
10811 const gdb_byte *bytes;
10812 struct dwarf2_locexpr_baton *baton;
10813 struct value *v;
10814
10815 if (child->tag != DW_TAG_template_type_param
10816 && child->tag != DW_TAG_template_value_param)
10817 continue;
10818
10819 if (first)
10820 {
10821 buf.puts ("<");
10822 first = 0;
10823 }
10824 else
10825 buf.puts (", ");
10826
10827 attr = dwarf2_attr (child, DW_AT_type, cu);
10828 if (attr == NULL)
10829 {
10830 complaint (&symfile_complaints,
10831 _("template parameter missing DW_AT_type"));
10832 buf.puts ("UNKNOWN_TYPE");
10833 continue;
10834 }
10835 type = die_type (child, cu);
10836
10837 if (child->tag == DW_TAG_template_type_param)
10838 {
10839 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
10840 continue;
10841 }
10842
10843 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10844 if (attr == NULL)
10845 {
10846 complaint (&symfile_complaints,
10847 _("template parameter missing "
10848 "DW_AT_const_value"));
10849 buf.puts ("UNKNOWN_VALUE");
10850 continue;
10851 }
10852
10853 dwarf2_const_value_attr (attr, type, name,
10854 &cu->comp_unit_obstack, cu,
10855 &value, &bytes, &baton);
10856
10857 if (TYPE_NOSIGN (type))
10858 /* GDB prints characters as NUMBER 'CHAR'. If that's
10859 changed, this can use value_print instead. */
10860 c_printchar (value, type, &buf);
10861 else
10862 {
10863 struct value_print_options opts;
10864
10865 if (baton != NULL)
10866 v = dwarf2_evaluate_loc_desc (type, NULL,
10867 baton->data,
10868 baton->size,
10869 baton->per_cu);
10870 else if (bytes != NULL)
10871 {
10872 v = allocate_value (type);
10873 memcpy (value_contents_writeable (v), bytes,
10874 TYPE_LENGTH (type));
10875 }
10876 else
10877 v = value_from_longest (type, value);
10878
10879 /* Specify decimal so that we do not depend on
10880 the radix. */
10881 get_formatted_print_options (&opts, 'd');
10882 opts.raw = 1;
10883 value_print (v, &buf, &opts);
10884 release_value (v);
10885 }
10886 }
10887
10888 die->building_fullname = 0;
10889
10890 if (!first)
10891 {
10892 /* Close the argument list, with a space if necessary
10893 (nested templates). */
10894 if (!buf.empty () && buf.string ().back () == '>')
10895 buf.puts (" >");
10896 else
10897 buf.puts (">");
10898 }
10899 }
10900
10901 /* For C++ methods, append formal parameter type
10902 information, if PHYSNAME. */
10903
10904 if (physname && die->tag == DW_TAG_subprogram
10905 && cu->language == language_cplus)
10906 {
10907 struct type *type = read_type_die (die, cu);
10908
10909 c_type_print_args (type, &buf, 1, cu->language,
10910 &type_print_raw_options);
10911
10912 if (cu->language == language_cplus)
10913 {
10914 /* Assume that an artificial first parameter is
10915 "this", but do not crash if it is not. RealView
10916 marks unnamed (and thus unused) parameters as
10917 artificial; there is no way to differentiate
10918 the two cases. */
10919 if (TYPE_NFIELDS (type) > 0
10920 && TYPE_FIELD_ARTIFICIAL (type, 0)
10921 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
10922 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10923 0))))
10924 buf.puts (" const");
10925 }
10926 }
10927
10928 const std::string &intermediate_name = buf.string ();
10929
10930 if (cu->language == language_cplus)
10931 canonical_name
10932 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10933 &objfile->per_bfd->storage_obstack);
10934
10935 /* If we only computed INTERMEDIATE_NAME, or if
10936 INTERMEDIATE_NAME is already canonical, then we need to
10937 copy it to the appropriate obstack. */
10938 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10939 name = ((const char *)
10940 obstack_copy0 (&objfile->per_bfd->storage_obstack,
10941 intermediate_name.c_str (),
10942 intermediate_name.length ()));
10943 else
10944 name = canonical_name;
10945 }
10946 }
10947
10948 return name;
10949 }
10950
10951 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10952 If scope qualifiers are appropriate they will be added. The result
10953 will be allocated on the storage_obstack, or NULL if the DIE does
10954 not have a name. NAME may either be from a previous call to
10955 dwarf2_name or NULL.
10956
10957 The output string will be canonicalized (if C++). */
10958
10959 static const char *
10960 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10961 {
10962 return dwarf2_compute_name (name, die, cu, 0);
10963 }
10964
10965 /* Construct a physname for the given DIE in CU. NAME may either be
10966 from a previous call to dwarf2_name or NULL. The result will be
10967 allocated on the objfile_objstack or NULL if the DIE does not have a
10968 name.
10969
10970 The output string will be canonicalized (if C++). */
10971
10972 static const char *
10973 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10974 {
10975 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10976 const char *retval, *mangled = NULL, *canon = NULL;
10977 int need_copy = 1;
10978
10979 /* In this case dwarf2_compute_name is just a shortcut not building anything
10980 on its own. */
10981 if (!die_needs_namespace (die, cu))
10982 return dwarf2_compute_name (name, die, cu, 1);
10983
10984 mangled = dw2_linkage_name (die, cu);
10985
10986 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10987 See https://github.com/rust-lang/rust/issues/32925. */
10988 if (cu->language == language_rust && mangled != NULL
10989 && strchr (mangled, '{') != NULL)
10990 mangled = NULL;
10991
10992 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10993 has computed. */
10994 gdb::unique_xmalloc_ptr<char> demangled;
10995 if (mangled != NULL)
10996 {
10997
10998 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10999 {
11000 /* Do nothing (do not demangle the symbol name). */
11001 }
11002 else if (cu->language == language_go)
11003 {
11004 /* This is a lie, but we already lie to the caller new_symbol.
11005 new_symbol assumes we return the mangled name.
11006 This just undoes that lie until things are cleaned up. */
11007 }
11008 else
11009 {
11010 /* Use DMGL_RET_DROP for C++ template functions to suppress
11011 their return type. It is easier for GDB users to search
11012 for such functions as `name(params)' than `long name(params)'.
11013 In such case the minimal symbol names do not match the full
11014 symbol names but for template functions there is never a need
11015 to look up their definition from their declaration so
11016 the only disadvantage remains the minimal symbol variant
11017 `long name(params)' does not have the proper inferior type. */
11018 demangled.reset (gdb_demangle (mangled,
11019 (DMGL_PARAMS | DMGL_ANSI
11020 | DMGL_RET_DROP)));
11021 }
11022 if (demangled)
11023 canon = demangled.get ();
11024 else
11025 {
11026 canon = mangled;
11027 need_copy = 0;
11028 }
11029 }
11030
11031 if (canon == NULL || check_physname)
11032 {
11033 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11034
11035 if (canon != NULL && strcmp (physname, canon) != 0)
11036 {
11037 /* It may not mean a bug in GDB. The compiler could also
11038 compute DW_AT_linkage_name incorrectly. But in such case
11039 GDB would need to be bug-to-bug compatible. */
11040
11041 complaint (&symfile_complaints,
11042 _("Computed physname <%s> does not match demangled <%s> "
11043 "(from linkage <%s>) - DIE at %s [in module %s]"),
11044 physname, canon, mangled, sect_offset_str (die->sect_off),
11045 objfile_name (objfile));
11046
11047 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11048 is available here - over computed PHYSNAME. It is safer
11049 against both buggy GDB and buggy compilers. */
11050
11051 retval = canon;
11052 }
11053 else
11054 {
11055 retval = physname;
11056 need_copy = 0;
11057 }
11058 }
11059 else
11060 retval = canon;
11061
11062 if (need_copy)
11063 retval = ((const char *)
11064 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11065 retval, strlen (retval)));
11066
11067 return retval;
11068 }
11069
11070 /* Inspect DIE in CU for a namespace alias. If one exists, record
11071 a new symbol for it.
11072
11073 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11074
11075 static int
11076 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11077 {
11078 struct attribute *attr;
11079
11080 /* If the die does not have a name, this is not a namespace
11081 alias. */
11082 attr = dwarf2_attr (die, DW_AT_name, cu);
11083 if (attr != NULL)
11084 {
11085 int num;
11086 struct die_info *d = die;
11087 struct dwarf2_cu *imported_cu = cu;
11088
11089 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11090 keep inspecting DIEs until we hit the underlying import. */
11091 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
11092 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11093 {
11094 attr = dwarf2_attr (d, DW_AT_import, cu);
11095 if (attr == NULL)
11096 break;
11097
11098 d = follow_die_ref (d, attr, &imported_cu);
11099 if (d->tag != DW_TAG_imported_declaration)
11100 break;
11101 }
11102
11103 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11104 {
11105 complaint (&symfile_complaints,
11106 _("DIE at %s has too many recursively imported "
11107 "declarations"), sect_offset_str (d->sect_off));
11108 return 0;
11109 }
11110
11111 if (attr != NULL)
11112 {
11113 struct type *type;
11114 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
11115
11116 type = get_die_type_at_offset (sect_off, cu->per_cu);
11117 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11118 {
11119 /* This declaration is a global namespace alias. Add
11120 a symbol for it whose type is the aliased namespace. */
11121 new_symbol (die, type, cu);
11122 return 1;
11123 }
11124 }
11125 }
11126
11127 return 0;
11128 }
11129
11130 /* Return the using directives repository (global or local?) to use in the
11131 current context for LANGUAGE.
11132
11133 For Ada, imported declarations can materialize renamings, which *may* be
11134 global. However it is impossible (for now?) in DWARF to distinguish
11135 "external" imported declarations and "static" ones. As all imported
11136 declarations seem to be static in all other languages, make them all CU-wide
11137 global only in Ada. */
11138
11139 static struct using_direct **
11140 using_directives (enum language language)
11141 {
11142 if (language == language_ada && context_stack_depth == 0)
11143 return &global_using_directives;
11144 else
11145 return &local_using_directives;
11146 }
11147
11148 /* Read the import statement specified by the given die and record it. */
11149
11150 static void
11151 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11152 {
11153 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11154 struct attribute *import_attr;
11155 struct die_info *imported_die, *child_die;
11156 struct dwarf2_cu *imported_cu;
11157 const char *imported_name;
11158 const char *imported_name_prefix;
11159 const char *canonical_name;
11160 const char *import_alias;
11161 const char *imported_declaration = NULL;
11162 const char *import_prefix;
11163 std::vector<const char *> excludes;
11164
11165 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11166 if (import_attr == NULL)
11167 {
11168 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11169 dwarf_tag_name (die->tag));
11170 return;
11171 }
11172
11173 imported_cu = cu;
11174 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11175 imported_name = dwarf2_name (imported_die, imported_cu);
11176 if (imported_name == NULL)
11177 {
11178 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11179
11180 The import in the following code:
11181 namespace A
11182 {
11183 typedef int B;
11184 }
11185
11186 int main ()
11187 {
11188 using A::B;
11189 B b;
11190 return b;
11191 }
11192
11193 ...
11194 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11195 <52> DW_AT_decl_file : 1
11196 <53> DW_AT_decl_line : 6
11197 <54> DW_AT_import : <0x75>
11198 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11199 <59> DW_AT_name : B
11200 <5b> DW_AT_decl_file : 1
11201 <5c> DW_AT_decl_line : 2
11202 <5d> DW_AT_type : <0x6e>
11203 ...
11204 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11205 <76> DW_AT_byte_size : 4
11206 <77> DW_AT_encoding : 5 (signed)
11207
11208 imports the wrong die ( 0x75 instead of 0x58 ).
11209 This case will be ignored until the gcc bug is fixed. */
11210 return;
11211 }
11212
11213 /* Figure out the local name after import. */
11214 import_alias = dwarf2_name (die, cu);
11215
11216 /* Figure out where the statement is being imported to. */
11217 import_prefix = determine_prefix (die, cu);
11218
11219 /* Figure out what the scope of the imported die is and prepend it
11220 to the name of the imported die. */
11221 imported_name_prefix = determine_prefix (imported_die, imported_cu);
11222
11223 if (imported_die->tag != DW_TAG_namespace
11224 && imported_die->tag != DW_TAG_module)
11225 {
11226 imported_declaration = imported_name;
11227 canonical_name = imported_name_prefix;
11228 }
11229 else if (strlen (imported_name_prefix) > 0)
11230 canonical_name = obconcat (&objfile->objfile_obstack,
11231 imported_name_prefix,
11232 (cu->language == language_d ? "." : "::"),
11233 imported_name, (char *) NULL);
11234 else
11235 canonical_name = imported_name;
11236
11237 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11238 for (child_die = die->child; child_die && child_die->tag;
11239 child_die = sibling_die (child_die))
11240 {
11241 /* DWARF-4: A Fortran use statement with a “rename list” may be
11242 represented by an imported module entry with an import attribute
11243 referring to the module and owned entries corresponding to those
11244 entities that are renamed as part of being imported. */
11245
11246 if (child_die->tag != DW_TAG_imported_declaration)
11247 {
11248 complaint (&symfile_complaints,
11249 _("child DW_TAG_imported_declaration expected "
11250 "- DIE at %s [in module %s]"),
11251 sect_offset_str (child_die->sect_off),
11252 objfile_name (objfile));
11253 continue;
11254 }
11255
11256 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11257 if (import_attr == NULL)
11258 {
11259 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11260 dwarf_tag_name (child_die->tag));
11261 continue;
11262 }
11263
11264 imported_cu = cu;
11265 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11266 &imported_cu);
11267 imported_name = dwarf2_name (imported_die, imported_cu);
11268 if (imported_name == NULL)
11269 {
11270 complaint (&symfile_complaints,
11271 _("child DW_TAG_imported_declaration has unknown "
11272 "imported name - DIE at %s [in module %s]"),
11273 sect_offset_str (child_die->sect_off),
11274 objfile_name (objfile));
11275 continue;
11276 }
11277
11278 excludes.push_back (imported_name);
11279
11280 process_die (child_die, cu);
11281 }
11282
11283 add_using_directive (using_directives (cu->language),
11284 import_prefix,
11285 canonical_name,
11286 import_alias,
11287 imported_declaration,
11288 excludes,
11289 0,
11290 &objfile->objfile_obstack);
11291 }
11292
11293 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11294 types, but gives them a size of zero. Starting with version 14,
11295 ICC is compatible with GCC. */
11296
11297 static int
11298 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11299 {
11300 if (!cu->checked_producer)
11301 check_producer (cu);
11302
11303 return cu->producer_is_icc_lt_14;
11304 }
11305
11306 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11307 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11308 this, it was first present in GCC release 4.3.0. */
11309
11310 static int
11311 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11312 {
11313 if (!cu->checked_producer)
11314 check_producer (cu);
11315
11316 return cu->producer_is_gcc_lt_4_3;
11317 }
11318
11319 static file_and_directory
11320 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11321 {
11322 file_and_directory res;
11323
11324 /* Find the filename. Do not use dwarf2_name here, since the filename
11325 is not a source language identifier. */
11326 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11327 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11328
11329 if (res.comp_dir == NULL
11330 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11331 && IS_ABSOLUTE_PATH (res.name))
11332 {
11333 res.comp_dir_storage = ldirname (res.name);
11334 if (!res.comp_dir_storage.empty ())
11335 res.comp_dir = res.comp_dir_storage.c_str ();
11336 }
11337 if (res.comp_dir != NULL)
11338 {
11339 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11340 directory, get rid of it. */
11341 const char *cp = strchr (res.comp_dir, ':');
11342
11343 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11344 res.comp_dir = cp + 1;
11345 }
11346
11347 if (res.name == NULL)
11348 res.name = "<unknown>";
11349
11350 return res;
11351 }
11352
11353 /* Handle DW_AT_stmt_list for a compilation unit.
11354 DIE is the DW_TAG_compile_unit die for CU.
11355 COMP_DIR is the compilation directory. LOWPC is passed to
11356 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11357
11358 static void
11359 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11360 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11361 {
11362 struct dwarf2_per_objfile *dwarf2_per_objfile
11363 = cu->per_cu->dwarf2_per_objfile;
11364 struct objfile *objfile = dwarf2_per_objfile->objfile;
11365 struct attribute *attr;
11366 struct line_header line_header_local;
11367 hashval_t line_header_local_hash;
11368 void **slot;
11369 int decode_mapping;
11370
11371 gdb_assert (! cu->per_cu->is_debug_types);
11372
11373 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11374 if (attr == NULL)
11375 return;
11376
11377 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11378
11379 /* The line header hash table is only created if needed (it exists to
11380 prevent redundant reading of the line table for partial_units).
11381 If we're given a partial_unit, we'll need it. If we're given a
11382 compile_unit, then use the line header hash table if it's already
11383 created, but don't create one just yet. */
11384
11385 if (dwarf2_per_objfile->line_header_hash == NULL
11386 && die->tag == DW_TAG_partial_unit)
11387 {
11388 dwarf2_per_objfile->line_header_hash
11389 = htab_create_alloc_ex (127, line_header_hash_voidp,
11390 line_header_eq_voidp,
11391 free_line_header_voidp,
11392 &objfile->objfile_obstack,
11393 hashtab_obstack_allocate,
11394 dummy_obstack_deallocate);
11395 }
11396
11397 line_header_local.sect_off = line_offset;
11398 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11399 line_header_local_hash = line_header_hash (&line_header_local);
11400 if (dwarf2_per_objfile->line_header_hash != NULL)
11401 {
11402 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11403 &line_header_local,
11404 line_header_local_hash, NO_INSERT);
11405
11406 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11407 is not present in *SLOT (since if there is something in *SLOT then
11408 it will be for a partial_unit). */
11409 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11410 {
11411 gdb_assert (*slot != NULL);
11412 cu->line_header = (struct line_header *) *slot;
11413 return;
11414 }
11415 }
11416
11417 /* dwarf_decode_line_header does not yet provide sufficient information.
11418 We always have to call also dwarf_decode_lines for it. */
11419 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11420 if (lh == NULL)
11421 return;
11422
11423 cu->line_header = lh.release ();
11424 cu->line_header_die_owner = die;
11425
11426 if (dwarf2_per_objfile->line_header_hash == NULL)
11427 slot = NULL;
11428 else
11429 {
11430 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11431 &line_header_local,
11432 line_header_local_hash, INSERT);
11433 gdb_assert (slot != NULL);
11434 }
11435 if (slot != NULL && *slot == NULL)
11436 {
11437 /* This newly decoded line number information unit will be owned
11438 by line_header_hash hash table. */
11439 *slot = cu->line_header;
11440 cu->line_header_die_owner = NULL;
11441 }
11442 else
11443 {
11444 /* We cannot free any current entry in (*slot) as that struct line_header
11445 may be already used by multiple CUs. Create only temporary decoded
11446 line_header for this CU - it may happen at most once for each line
11447 number information unit. And if we're not using line_header_hash
11448 then this is what we want as well. */
11449 gdb_assert (die->tag != DW_TAG_partial_unit);
11450 }
11451 decode_mapping = (die->tag != DW_TAG_partial_unit);
11452 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11453 decode_mapping);
11454
11455 }
11456
11457 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11458
11459 static void
11460 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11461 {
11462 struct dwarf2_per_objfile *dwarf2_per_objfile
11463 = cu->per_cu->dwarf2_per_objfile;
11464 struct objfile *objfile = dwarf2_per_objfile->objfile;
11465 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11466 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11467 CORE_ADDR highpc = ((CORE_ADDR) 0);
11468 struct attribute *attr;
11469 struct die_info *child_die;
11470 CORE_ADDR baseaddr;
11471
11472 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11473
11474 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11475
11476 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11477 from finish_block. */
11478 if (lowpc == ((CORE_ADDR) -1))
11479 lowpc = highpc;
11480 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11481
11482 file_and_directory fnd = find_file_and_directory (die, cu);
11483
11484 prepare_one_comp_unit (cu, die, cu->language);
11485
11486 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11487 standardised yet. As a workaround for the language detection we fall
11488 back to the DW_AT_producer string. */
11489 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11490 cu->language = language_opencl;
11491
11492 /* Similar hack for Go. */
11493 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11494 set_cu_language (DW_LANG_Go, cu);
11495
11496 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
11497
11498 /* Decode line number information if present. We do this before
11499 processing child DIEs, so that the line header table is available
11500 for DW_AT_decl_file. */
11501 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11502
11503 /* Process all dies in compilation unit. */
11504 if (die->child != NULL)
11505 {
11506 child_die = die->child;
11507 while (child_die && child_die->tag)
11508 {
11509 process_die (child_die, cu);
11510 child_die = sibling_die (child_die);
11511 }
11512 }
11513
11514 /* Decode macro information, if present. Dwarf 2 macro information
11515 refers to information in the line number info statement program
11516 header, so we can only read it if we've read the header
11517 successfully. */
11518 attr = dwarf2_attr (die, DW_AT_macros, cu);
11519 if (attr == NULL)
11520 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11521 if (attr && cu->line_header)
11522 {
11523 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11524 complaint (&symfile_complaints,
11525 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11526
11527 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11528 }
11529 else
11530 {
11531 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11532 if (attr && cu->line_header)
11533 {
11534 unsigned int macro_offset = DW_UNSND (attr);
11535
11536 dwarf_decode_macros (cu, macro_offset, 0);
11537 }
11538 }
11539 }
11540
11541 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
11542 Create the set of symtabs used by this TU, or if this TU is sharing
11543 symtabs with another TU and the symtabs have already been created
11544 then restore those symtabs in the line header.
11545 We don't need the pc/line-number mapping for type units. */
11546
11547 static void
11548 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
11549 {
11550 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
11551 struct type_unit_group *tu_group;
11552 int first_time;
11553 struct attribute *attr;
11554 unsigned int i;
11555 struct signatured_type *sig_type;
11556
11557 gdb_assert (per_cu->is_debug_types);
11558 sig_type = (struct signatured_type *) per_cu;
11559
11560 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11561
11562 /* If we're using .gdb_index (includes -readnow) then
11563 per_cu->type_unit_group may not have been set up yet. */
11564 if (sig_type->type_unit_group == NULL)
11565 sig_type->type_unit_group = get_type_unit_group (cu, attr);
11566 tu_group = sig_type->type_unit_group;
11567
11568 /* If we've already processed this stmt_list there's no real need to
11569 do it again, we could fake it and just recreate the part we need
11570 (file name,index -> symtab mapping). If data shows this optimization
11571 is useful we can do it then. */
11572 first_time = tu_group->compunit_symtab == NULL;
11573
11574 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11575 debug info. */
11576 line_header_up lh;
11577 if (attr != NULL)
11578 {
11579 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11580 lh = dwarf_decode_line_header (line_offset, cu);
11581 }
11582 if (lh == NULL)
11583 {
11584 if (first_time)
11585 dwarf2_start_symtab (cu, "", NULL, 0);
11586 else
11587 {
11588 gdb_assert (tu_group->symtabs == NULL);
11589 restart_symtab (tu_group->compunit_symtab, "", 0);
11590 }
11591 return;
11592 }
11593
11594 cu->line_header = lh.release ();
11595 cu->line_header_die_owner = die;
11596
11597 if (first_time)
11598 {
11599 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
11600
11601 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11602 still initializing it, and our caller (a few levels up)
11603 process_full_type_unit still needs to know if this is the first
11604 time. */
11605
11606 tu_group->num_symtabs = cu->line_header->file_names.size ();
11607 tu_group->symtabs = XNEWVEC (struct symtab *,
11608 cu->line_header->file_names.size ());
11609
11610 for (i = 0; i < cu->line_header->file_names.size (); ++i)
11611 {
11612 file_entry &fe = cu->line_header->file_names[i];
11613
11614 dwarf2_start_subfile (fe.name, fe.include_dir (cu->line_header));
11615
11616 if (current_subfile->symtab == NULL)
11617 {
11618 /* NOTE: start_subfile will recognize when it's been
11619 passed a file it has already seen. So we can't
11620 assume there's a simple mapping from
11621 cu->line_header->file_names to subfiles, plus
11622 cu->line_header->file_names may contain dups. */
11623 current_subfile->symtab
11624 = allocate_symtab (cust, current_subfile->name);
11625 }
11626
11627 fe.symtab = current_subfile->symtab;
11628 tu_group->symtabs[i] = fe.symtab;
11629 }
11630 }
11631 else
11632 {
11633 restart_symtab (tu_group->compunit_symtab, "", 0);
11634
11635 for (i = 0; i < cu->line_header->file_names.size (); ++i)
11636 {
11637 file_entry &fe = cu->line_header->file_names[i];
11638
11639 fe.symtab = tu_group->symtabs[i];
11640 }
11641 }
11642
11643 /* The main symtab is allocated last. Type units don't have DW_AT_name
11644 so they don't have a "real" (so to speak) symtab anyway.
11645 There is later code that will assign the main symtab to all symbols
11646 that don't have one. We need to handle the case of a symbol with a
11647 missing symtab (DW_AT_decl_file) anyway. */
11648 }
11649
11650 /* Process DW_TAG_type_unit.
11651 For TUs we want to skip the first top level sibling if it's not the
11652 actual type being defined by this TU. In this case the first top
11653 level sibling is there to provide context only. */
11654
11655 static void
11656 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11657 {
11658 struct die_info *child_die;
11659
11660 prepare_one_comp_unit (cu, die, language_minimal);
11661
11662 /* Initialize (or reinitialize) the machinery for building symtabs.
11663 We do this before processing child DIEs, so that the line header table
11664 is available for DW_AT_decl_file. */
11665 setup_type_unit_groups (die, cu);
11666
11667 if (die->child != NULL)
11668 {
11669 child_die = die->child;
11670 while (child_die && child_die->tag)
11671 {
11672 process_die (child_die, cu);
11673 child_die = sibling_die (child_die);
11674 }
11675 }
11676 }
11677 \f
11678 /* DWO/DWP files.
11679
11680 http://gcc.gnu.org/wiki/DebugFission
11681 http://gcc.gnu.org/wiki/DebugFissionDWP
11682
11683 To simplify handling of both DWO files ("object" files with the DWARF info)
11684 and DWP files (a file with the DWOs packaged up into one file), we treat
11685 DWP files as having a collection of virtual DWO files. */
11686
11687 static hashval_t
11688 hash_dwo_file (const void *item)
11689 {
11690 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11691 hashval_t hash;
11692
11693 hash = htab_hash_string (dwo_file->dwo_name);
11694 if (dwo_file->comp_dir != NULL)
11695 hash += htab_hash_string (dwo_file->comp_dir);
11696 return hash;
11697 }
11698
11699 static int
11700 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11701 {
11702 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11703 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11704
11705 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11706 return 0;
11707 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11708 return lhs->comp_dir == rhs->comp_dir;
11709 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11710 }
11711
11712 /* Allocate a hash table for DWO files. */
11713
11714 static htab_t
11715 allocate_dwo_file_hash_table (struct objfile *objfile)
11716 {
11717 return htab_create_alloc_ex (41,
11718 hash_dwo_file,
11719 eq_dwo_file,
11720 NULL,
11721 &objfile->objfile_obstack,
11722 hashtab_obstack_allocate,
11723 dummy_obstack_deallocate);
11724 }
11725
11726 /* Lookup DWO file DWO_NAME. */
11727
11728 static void **
11729 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11730 const char *dwo_name,
11731 const char *comp_dir)
11732 {
11733 struct dwo_file find_entry;
11734 void **slot;
11735
11736 if (dwarf2_per_objfile->dwo_files == NULL)
11737 dwarf2_per_objfile->dwo_files
11738 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
11739
11740 memset (&find_entry, 0, sizeof (find_entry));
11741 find_entry.dwo_name = dwo_name;
11742 find_entry.comp_dir = comp_dir;
11743 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
11744
11745 return slot;
11746 }
11747
11748 static hashval_t
11749 hash_dwo_unit (const void *item)
11750 {
11751 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11752
11753 /* This drops the top 32 bits of the id, but is ok for a hash. */
11754 return dwo_unit->signature;
11755 }
11756
11757 static int
11758 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11759 {
11760 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11761 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11762
11763 /* The signature is assumed to be unique within the DWO file.
11764 So while object file CU dwo_id's always have the value zero,
11765 that's OK, assuming each object file DWO file has only one CU,
11766 and that's the rule for now. */
11767 return lhs->signature == rhs->signature;
11768 }
11769
11770 /* Allocate a hash table for DWO CUs,TUs.
11771 There is one of these tables for each of CUs,TUs for each DWO file. */
11772
11773 static htab_t
11774 allocate_dwo_unit_table (struct objfile *objfile)
11775 {
11776 /* Start out with a pretty small number.
11777 Generally DWO files contain only one CU and maybe some TUs. */
11778 return htab_create_alloc_ex (3,
11779 hash_dwo_unit,
11780 eq_dwo_unit,
11781 NULL,
11782 &objfile->objfile_obstack,
11783 hashtab_obstack_allocate,
11784 dummy_obstack_deallocate);
11785 }
11786
11787 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
11788
11789 struct create_dwo_cu_data
11790 {
11791 struct dwo_file *dwo_file;
11792 struct dwo_unit dwo_unit;
11793 };
11794
11795 /* die_reader_func for create_dwo_cu. */
11796
11797 static void
11798 create_dwo_cu_reader (const struct die_reader_specs *reader,
11799 const gdb_byte *info_ptr,
11800 struct die_info *comp_unit_die,
11801 int has_children,
11802 void *datap)
11803 {
11804 struct dwarf2_cu *cu = reader->cu;
11805 sect_offset sect_off = cu->per_cu->sect_off;
11806 struct dwarf2_section_info *section = cu->per_cu->section;
11807 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
11808 struct dwo_file *dwo_file = data->dwo_file;
11809 struct dwo_unit *dwo_unit = &data->dwo_unit;
11810 struct attribute *attr;
11811
11812 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
11813 if (attr == NULL)
11814 {
11815 complaint (&symfile_complaints,
11816 _("Dwarf Error: debug entry at offset %s is missing"
11817 " its dwo_id [in module %s]"),
11818 sect_offset_str (sect_off), dwo_file->dwo_name);
11819 return;
11820 }
11821
11822 dwo_unit->dwo_file = dwo_file;
11823 dwo_unit->signature = DW_UNSND (attr);
11824 dwo_unit->section = section;
11825 dwo_unit->sect_off = sect_off;
11826 dwo_unit->length = cu->per_cu->length;
11827
11828 if (dwarf_read_debug)
11829 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11830 sect_offset_str (sect_off),
11831 hex_string (dwo_unit->signature));
11832 }
11833
11834 /* Create the dwo_units for the CUs in a DWO_FILE.
11835 Note: This function processes DWO files only, not DWP files. */
11836
11837 static void
11838 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11839 struct dwo_file &dwo_file, dwarf2_section_info &section,
11840 htab_t &cus_htab)
11841 {
11842 struct objfile *objfile = dwarf2_per_objfile->objfile;
11843 const gdb_byte *info_ptr, *end_ptr;
11844
11845 dwarf2_read_section (objfile, &section);
11846 info_ptr = section.buffer;
11847
11848 if (info_ptr == NULL)
11849 return;
11850
11851 if (dwarf_read_debug)
11852 {
11853 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11854 get_section_name (&section),
11855 get_section_file_name (&section));
11856 }
11857
11858 end_ptr = info_ptr + section.size;
11859 while (info_ptr < end_ptr)
11860 {
11861 struct dwarf2_per_cu_data per_cu;
11862 struct create_dwo_cu_data create_dwo_cu_data;
11863 struct dwo_unit *dwo_unit;
11864 void **slot;
11865 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11866
11867 memset (&create_dwo_cu_data.dwo_unit, 0,
11868 sizeof (create_dwo_cu_data.dwo_unit));
11869 memset (&per_cu, 0, sizeof (per_cu));
11870 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11871 per_cu.is_debug_types = 0;
11872 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11873 per_cu.section = &section;
11874 create_dwo_cu_data.dwo_file = &dwo_file;
11875
11876 init_cutu_and_read_dies_no_follow (
11877 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
11878 info_ptr += per_cu.length;
11879
11880 // If the unit could not be parsed, skip it.
11881 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
11882 continue;
11883
11884 if (cus_htab == NULL)
11885 cus_htab = allocate_dwo_unit_table (objfile);
11886
11887 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11888 *dwo_unit = create_dwo_cu_data.dwo_unit;
11889 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
11890 gdb_assert (slot != NULL);
11891 if (*slot != NULL)
11892 {
11893 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11894 sect_offset dup_sect_off = dup_cu->sect_off;
11895
11896 complaint (&symfile_complaints,
11897 _("debug cu entry at offset %s is duplicate to"
11898 " the entry at offset %s, signature %s"),
11899 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11900 hex_string (dwo_unit->signature));
11901 }
11902 *slot = (void *)dwo_unit;
11903 }
11904 }
11905
11906 /* DWP file .debug_{cu,tu}_index section format:
11907 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11908
11909 DWP Version 1:
11910
11911 Both index sections have the same format, and serve to map a 64-bit
11912 signature to a set of section numbers. Each section begins with a header,
11913 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11914 indexes, and a pool of 32-bit section numbers. The index sections will be
11915 aligned at 8-byte boundaries in the file.
11916
11917 The index section header consists of:
11918
11919 V, 32 bit version number
11920 -, 32 bits unused
11921 N, 32 bit number of compilation units or type units in the index
11922 M, 32 bit number of slots in the hash table
11923
11924 Numbers are recorded using the byte order of the application binary.
11925
11926 The hash table begins at offset 16 in the section, and consists of an array
11927 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11928 order of the application binary). Unused slots in the hash table are 0.
11929 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11930
11931 The parallel table begins immediately after the hash table
11932 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11933 array of 32-bit indexes (using the byte order of the application binary),
11934 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11935 table contains a 32-bit index into the pool of section numbers. For unused
11936 hash table slots, the corresponding entry in the parallel table will be 0.
11937
11938 The pool of section numbers begins immediately following the hash table
11939 (at offset 16 + 12 * M from the beginning of the section). The pool of
11940 section numbers consists of an array of 32-bit words (using the byte order
11941 of the application binary). Each item in the array is indexed starting
11942 from 0. The hash table entry provides the index of the first section
11943 number in the set. Additional section numbers in the set follow, and the
11944 set is terminated by a 0 entry (section number 0 is not used in ELF).
11945
11946 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11947 section must be the first entry in the set, and the .debug_abbrev.dwo must
11948 be the second entry. Other members of the set may follow in any order.
11949
11950 ---
11951
11952 DWP Version 2:
11953
11954 DWP Version 2 combines all the .debug_info, etc. sections into one,
11955 and the entries in the index tables are now offsets into these sections.
11956 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11957 section.
11958
11959 Index Section Contents:
11960 Header
11961 Hash Table of Signatures dwp_hash_table.hash_table
11962 Parallel Table of Indices dwp_hash_table.unit_table
11963 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11964 Table of Section Sizes dwp_hash_table.v2.sizes
11965
11966 The index section header consists of:
11967
11968 V, 32 bit version number
11969 L, 32 bit number of columns in the table of section offsets
11970 N, 32 bit number of compilation units or type units in the index
11971 M, 32 bit number of slots in the hash table
11972
11973 Numbers are recorded using the byte order of the application binary.
11974
11975 The hash table has the same format as version 1.
11976 The parallel table of indices has the same format as version 1,
11977 except that the entries are origin-1 indices into the table of sections
11978 offsets and the table of section sizes.
11979
11980 The table of offsets begins immediately following the parallel table
11981 (at offset 16 + 12 * M from the beginning of the section). The table is
11982 a two-dimensional array of 32-bit words (using the byte order of the
11983 application binary), with L columns and N+1 rows, in row-major order.
11984 Each row in the array is indexed starting from 0. The first row provides
11985 a key to the remaining rows: each column in this row provides an identifier
11986 for a debug section, and the offsets in the same column of subsequent rows
11987 refer to that section. The section identifiers are:
11988
11989 DW_SECT_INFO 1 .debug_info.dwo
11990 DW_SECT_TYPES 2 .debug_types.dwo
11991 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11992 DW_SECT_LINE 4 .debug_line.dwo
11993 DW_SECT_LOC 5 .debug_loc.dwo
11994 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11995 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11996 DW_SECT_MACRO 8 .debug_macro.dwo
11997
11998 The offsets provided by the CU and TU index sections are the base offsets
11999 for the contributions made by each CU or TU to the corresponding section
12000 in the package file. Each CU and TU header contains an abbrev_offset
12001 field, used to find the abbreviations table for that CU or TU within the
12002 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12003 be interpreted as relative to the base offset given in the index section.
12004 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12005 should be interpreted as relative to the base offset for .debug_line.dwo,
12006 and offsets into other debug sections obtained from DWARF attributes should
12007 also be interpreted as relative to the corresponding base offset.
12008
12009 The table of sizes begins immediately following the table of offsets.
12010 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12011 with L columns and N rows, in row-major order. Each row in the array is
12012 indexed starting from 1 (row 0 is shared by the two tables).
12013
12014 ---
12015
12016 Hash table lookup is handled the same in version 1 and 2:
12017
12018 We assume that N and M will not exceed 2^32 - 1.
12019 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12020
12021 Given a 64-bit compilation unit signature or a type signature S, an entry
12022 in the hash table is located as follows:
12023
12024 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12025 the low-order k bits all set to 1.
12026
12027 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
12028
12029 3) If the hash table entry at index H matches the signature, use that
12030 entry. If the hash table entry at index H is unused (all zeroes),
12031 terminate the search: the signature is not present in the table.
12032
12033 4) Let H = (H + H') modulo M. Repeat at Step 3.
12034
12035 Because M > N and H' and M are relatively prime, the search is guaranteed
12036 to stop at an unused slot or find the match. */
12037
12038 /* Create a hash table to map DWO IDs to their CU/TU entry in
12039 .debug_{info,types}.dwo in DWP_FILE.
12040 Returns NULL if there isn't one.
12041 Note: This function processes DWP files only, not DWO files. */
12042
12043 static struct dwp_hash_table *
12044 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12045 struct dwp_file *dwp_file, int is_debug_types)
12046 {
12047 struct objfile *objfile = dwarf2_per_objfile->objfile;
12048 bfd *dbfd = dwp_file->dbfd;
12049 const gdb_byte *index_ptr, *index_end;
12050 struct dwarf2_section_info *index;
12051 uint32_t version, nr_columns, nr_units, nr_slots;
12052 struct dwp_hash_table *htab;
12053
12054 if (is_debug_types)
12055 index = &dwp_file->sections.tu_index;
12056 else
12057 index = &dwp_file->sections.cu_index;
12058
12059 if (dwarf2_section_empty_p (index))
12060 return NULL;
12061 dwarf2_read_section (objfile, index);
12062
12063 index_ptr = index->buffer;
12064 index_end = index_ptr + index->size;
12065
12066 version = read_4_bytes (dbfd, index_ptr);
12067 index_ptr += 4;
12068 if (version == 2)
12069 nr_columns = read_4_bytes (dbfd, index_ptr);
12070 else
12071 nr_columns = 0;
12072 index_ptr += 4;
12073 nr_units = read_4_bytes (dbfd, index_ptr);
12074 index_ptr += 4;
12075 nr_slots = read_4_bytes (dbfd, index_ptr);
12076 index_ptr += 4;
12077
12078 if (version != 1 && version != 2)
12079 {
12080 error (_("Dwarf Error: unsupported DWP file version (%s)"
12081 " [in module %s]"),
12082 pulongest (version), dwp_file->name);
12083 }
12084 if (nr_slots != (nr_slots & -nr_slots))
12085 {
12086 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
12087 " is not power of 2 [in module %s]"),
12088 pulongest (nr_slots), dwp_file->name);
12089 }
12090
12091 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
12092 htab->version = version;
12093 htab->nr_columns = nr_columns;
12094 htab->nr_units = nr_units;
12095 htab->nr_slots = nr_slots;
12096 htab->hash_table = index_ptr;
12097 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
12098
12099 /* Exit early if the table is empty. */
12100 if (nr_slots == 0 || nr_units == 0
12101 || (version == 2 && nr_columns == 0))
12102 {
12103 /* All must be zero. */
12104 if (nr_slots != 0 || nr_units != 0
12105 || (version == 2 && nr_columns != 0))
12106 {
12107 complaint (&symfile_complaints,
12108 _("Empty DWP but nr_slots,nr_units,nr_columns not"
12109 " all zero [in modules %s]"),
12110 dwp_file->name);
12111 }
12112 return htab;
12113 }
12114
12115 if (version == 1)
12116 {
12117 htab->section_pool.v1.indices =
12118 htab->unit_table + sizeof (uint32_t) * nr_slots;
12119 /* It's harder to decide whether the section is too small in v1.
12120 V1 is deprecated anyway so we punt. */
12121 }
12122 else
12123 {
12124 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12125 int *ids = htab->section_pool.v2.section_ids;
12126 /* Reverse map for error checking. */
12127 int ids_seen[DW_SECT_MAX + 1];
12128 int i;
12129
12130 if (nr_columns < 2)
12131 {
12132 error (_("Dwarf Error: bad DWP hash table, too few columns"
12133 " in section table [in module %s]"),
12134 dwp_file->name);
12135 }
12136 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12137 {
12138 error (_("Dwarf Error: bad DWP hash table, too many columns"
12139 " in section table [in module %s]"),
12140 dwp_file->name);
12141 }
12142 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12143 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12144 for (i = 0; i < nr_columns; ++i)
12145 {
12146 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12147
12148 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12149 {
12150 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12151 " in section table [in module %s]"),
12152 id, dwp_file->name);
12153 }
12154 if (ids_seen[id] != -1)
12155 {
12156 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12157 " id %d in section table [in module %s]"),
12158 id, dwp_file->name);
12159 }
12160 ids_seen[id] = i;
12161 ids[i] = id;
12162 }
12163 /* Must have exactly one info or types section. */
12164 if (((ids_seen[DW_SECT_INFO] != -1)
12165 + (ids_seen[DW_SECT_TYPES] != -1))
12166 != 1)
12167 {
12168 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12169 " DWO info/types section [in module %s]"),
12170 dwp_file->name);
12171 }
12172 /* Must have an abbrev section. */
12173 if (ids_seen[DW_SECT_ABBREV] == -1)
12174 {
12175 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12176 " section [in module %s]"),
12177 dwp_file->name);
12178 }
12179 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12180 htab->section_pool.v2.sizes =
12181 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12182 * nr_units * nr_columns);
12183 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12184 * nr_units * nr_columns))
12185 > index_end)
12186 {
12187 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12188 " [in module %s]"),
12189 dwp_file->name);
12190 }
12191 }
12192
12193 return htab;
12194 }
12195
12196 /* Update SECTIONS with the data from SECTP.
12197
12198 This function is like the other "locate" section routines that are
12199 passed to bfd_map_over_sections, but in this context the sections to
12200 read comes from the DWP V1 hash table, not the full ELF section table.
12201
12202 The result is non-zero for success, or zero if an error was found. */
12203
12204 static int
12205 locate_v1_virtual_dwo_sections (asection *sectp,
12206 struct virtual_v1_dwo_sections *sections)
12207 {
12208 const struct dwop_section_names *names = &dwop_section_names;
12209
12210 if (section_is_p (sectp->name, &names->abbrev_dwo))
12211 {
12212 /* There can be only one. */
12213 if (sections->abbrev.s.section != NULL)
12214 return 0;
12215 sections->abbrev.s.section = sectp;
12216 sections->abbrev.size = bfd_get_section_size (sectp);
12217 }
12218 else if (section_is_p (sectp->name, &names->info_dwo)
12219 || section_is_p (sectp->name, &names->types_dwo))
12220 {
12221 /* There can be only one. */
12222 if (sections->info_or_types.s.section != NULL)
12223 return 0;
12224 sections->info_or_types.s.section = sectp;
12225 sections->info_or_types.size = bfd_get_section_size (sectp);
12226 }
12227 else if (section_is_p (sectp->name, &names->line_dwo))
12228 {
12229 /* There can be only one. */
12230 if (sections->line.s.section != NULL)
12231 return 0;
12232 sections->line.s.section = sectp;
12233 sections->line.size = bfd_get_section_size (sectp);
12234 }
12235 else if (section_is_p (sectp->name, &names->loc_dwo))
12236 {
12237 /* There can be only one. */
12238 if (sections->loc.s.section != NULL)
12239 return 0;
12240 sections->loc.s.section = sectp;
12241 sections->loc.size = bfd_get_section_size (sectp);
12242 }
12243 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12244 {
12245 /* There can be only one. */
12246 if (sections->macinfo.s.section != NULL)
12247 return 0;
12248 sections->macinfo.s.section = sectp;
12249 sections->macinfo.size = bfd_get_section_size (sectp);
12250 }
12251 else if (section_is_p (sectp->name, &names->macro_dwo))
12252 {
12253 /* There can be only one. */
12254 if (sections->macro.s.section != NULL)
12255 return 0;
12256 sections->macro.s.section = sectp;
12257 sections->macro.size = bfd_get_section_size (sectp);
12258 }
12259 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12260 {
12261 /* There can be only one. */
12262 if (sections->str_offsets.s.section != NULL)
12263 return 0;
12264 sections->str_offsets.s.section = sectp;
12265 sections->str_offsets.size = bfd_get_section_size (sectp);
12266 }
12267 else
12268 {
12269 /* No other kind of section is valid. */
12270 return 0;
12271 }
12272
12273 return 1;
12274 }
12275
12276 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12277 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12278 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12279 This is for DWP version 1 files. */
12280
12281 static struct dwo_unit *
12282 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12283 struct dwp_file *dwp_file,
12284 uint32_t unit_index,
12285 const char *comp_dir,
12286 ULONGEST signature, int is_debug_types)
12287 {
12288 struct objfile *objfile = dwarf2_per_objfile->objfile;
12289 const struct dwp_hash_table *dwp_htab =
12290 is_debug_types ? dwp_file->tus : dwp_file->cus;
12291 bfd *dbfd = dwp_file->dbfd;
12292 const char *kind = is_debug_types ? "TU" : "CU";
12293 struct dwo_file *dwo_file;
12294 struct dwo_unit *dwo_unit;
12295 struct virtual_v1_dwo_sections sections;
12296 void **dwo_file_slot;
12297 int i;
12298
12299 gdb_assert (dwp_file->version == 1);
12300
12301 if (dwarf_read_debug)
12302 {
12303 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
12304 kind,
12305 pulongest (unit_index), hex_string (signature),
12306 dwp_file->name);
12307 }
12308
12309 /* Fetch the sections of this DWO unit.
12310 Put a limit on the number of sections we look for so that bad data
12311 doesn't cause us to loop forever. */
12312
12313 #define MAX_NR_V1_DWO_SECTIONS \
12314 (1 /* .debug_info or .debug_types */ \
12315 + 1 /* .debug_abbrev */ \
12316 + 1 /* .debug_line */ \
12317 + 1 /* .debug_loc */ \
12318 + 1 /* .debug_str_offsets */ \
12319 + 1 /* .debug_macro or .debug_macinfo */ \
12320 + 1 /* trailing zero */)
12321
12322 memset (&sections, 0, sizeof (sections));
12323
12324 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12325 {
12326 asection *sectp;
12327 uint32_t section_nr =
12328 read_4_bytes (dbfd,
12329 dwp_htab->section_pool.v1.indices
12330 + (unit_index + i) * sizeof (uint32_t));
12331
12332 if (section_nr == 0)
12333 break;
12334 if (section_nr >= dwp_file->num_sections)
12335 {
12336 error (_("Dwarf Error: bad DWP hash table, section number too large"
12337 " [in module %s]"),
12338 dwp_file->name);
12339 }
12340
12341 sectp = dwp_file->elf_sections[section_nr];
12342 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12343 {
12344 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12345 " [in module %s]"),
12346 dwp_file->name);
12347 }
12348 }
12349
12350 if (i < 2
12351 || dwarf2_section_empty_p (&sections.info_or_types)
12352 || dwarf2_section_empty_p (&sections.abbrev))
12353 {
12354 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12355 " [in module %s]"),
12356 dwp_file->name);
12357 }
12358 if (i == MAX_NR_V1_DWO_SECTIONS)
12359 {
12360 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12361 " [in module %s]"),
12362 dwp_file->name);
12363 }
12364
12365 /* It's easier for the rest of the code if we fake a struct dwo_file and
12366 have dwo_unit "live" in that. At least for now.
12367
12368 The DWP file can be made up of a random collection of CUs and TUs.
12369 However, for each CU + set of TUs that came from the same original DWO
12370 file, we can combine them back into a virtual DWO file to save space
12371 (fewer struct dwo_file objects to allocate). Remember that for really
12372 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12373
12374 std::string virtual_dwo_name =
12375 string_printf ("virtual-dwo/%d-%d-%d-%d",
12376 get_section_id (&sections.abbrev),
12377 get_section_id (&sections.line),
12378 get_section_id (&sections.loc),
12379 get_section_id (&sections.str_offsets));
12380 /* Can we use an existing virtual DWO file? */
12381 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12382 virtual_dwo_name.c_str (),
12383 comp_dir);
12384 /* Create one if necessary. */
12385 if (*dwo_file_slot == NULL)
12386 {
12387 if (dwarf_read_debug)
12388 {
12389 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12390 virtual_dwo_name.c_str ());
12391 }
12392 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
12393 dwo_file->dwo_name
12394 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
12395 virtual_dwo_name.c_str (),
12396 virtual_dwo_name.size ());
12397 dwo_file->comp_dir = comp_dir;
12398 dwo_file->sections.abbrev = sections.abbrev;
12399 dwo_file->sections.line = sections.line;
12400 dwo_file->sections.loc = sections.loc;
12401 dwo_file->sections.macinfo = sections.macinfo;
12402 dwo_file->sections.macro = sections.macro;
12403 dwo_file->sections.str_offsets = sections.str_offsets;
12404 /* The "str" section is global to the entire DWP file. */
12405 dwo_file->sections.str = dwp_file->sections.str;
12406 /* The info or types section is assigned below to dwo_unit,
12407 there's no need to record it in dwo_file.
12408 Also, we can't simply record type sections in dwo_file because
12409 we record a pointer into the vector in dwo_unit. As we collect more
12410 types we'll grow the vector and eventually have to reallocate space
12411 for it, invalidating all copies of pointers into the previous
12412 contents. */
12413 *dwo_file_slot = dwo_file;
12414 }
12415 else
12416 {
12417 if (dwarf_read_debug)
12418 {
12419 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12420 virtual_dwo_name.c_str ());
12421 }
12422 dwo_file = (struct dwo_file *) *dwo_file_slot;
12423 }
12424
12425 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12426 dwo_unit->dwo_file = dwo_file;
12427 dwo_unit->signature = signature;
12428 dwo_unit->section =
12429 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12430 *dwo_unit->section = sections.info_or_types;
12431 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12432
12433 return dwo_unit;
12434 }
12435
12436 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12437 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12438 piece within that section used by a TU/CU, return a virtual section
12439 of just that piece. */
12440
12441 static struct dwarf2_section_info
12442 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12443 struct dwarf2_section_info *section,
12444 bfd_size_type offset, bfd_size_type size)
12445 {
12446 struct dwarf2_section_info result;
12447 asection *sectp;
12448
12449 gdb_assert (section != NULL);
12450 gdb_assert (!section->is_virtual);
12451
12452 memset (&result, 0, sizeof (result));
12453 result.s.containing_section = section;
12454 result.is_virtual = 1;
12455
12456 if (size == 0)
12457 return result;
12458
12459 sectp = get_section_bfd_section (section);
12460
12461 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12462 bounds of the real section. This is a pretty-rare event, so just
12463 flag an error (easier) instead of a warning and trying to cope. */
12464 if (sectp == NULL
12465 || offset + size > bfd_get_section_size (sectp))
12466 {
12467 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12468 " in section %s [in module %s]"),
12469 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
12470 objfile_name (dwarf2_per_objfile->objfile));
12471 }
12472
12473 result.virtual_offset = offset;
12474 result.size = size;
12475 return result;
12476 }
12477
12478 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12479 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12480 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12481 This is for DWP version 2 files. */
12482
12483 static struct dwo_unit *
12484 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12485 struct dwp_file *dwp_file,
12486 uint32_t unit_index,
12487 const char *comp_dir,
12488 ULONGEST signature, int is_debug_types)
12489 {
12490 struct objfile *objfile = dwarf2_per_objfile->objfile;
12491 const struct dwp_hash_table *dwp_htab =
12492 is_debug_types ? dwp_file->tus : dwp_file->cus;
12493 bfd *dbfd = dwp_file->dbfd;
12494 const char *kind = is_debug_types ? "TU" : "CU";
12495 struct dwo_file *dwo_file;
12496 struct dwo_unit *dwo_unit;
12497 struct virtual_v2_dwo_sections sections;
12498 void **dwo_file_slot;
12499 int i;
12500
12501 gdb_assert (dwp_file->version == 2);
12502
12503 if (dwarf_read_debug)
12504 {
12505 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12506 kind,
12507 pulongest (unit_index), hex_string (signature),
12508 dwp_file->name);
12509 }
12510
12511 /* Fetch the section offsets of this DWO unit. */
12512
12513 memset (&sections, 0, sizeof (sections));
12514
12515 for (i = 0; i < dwp_htab->nr_columns; ++i)
12516 {
12517 uint32_t offset = read_4_bytes (dbfd,
12518 dwp_htab->section_pool.v2.offsets
12519 + (((unit_index - 1) * dwp_htab->nr_columns
12520 + i)
12521 * sizeof (uint32_t)));
12522 uint32_t size = read_4_bytes (dbfd,
12523 dwp_htab->section_pool.v2.sizes
12524 + (((unit_index - 1) * dwp_htab->nr_columns
12525 + i)
12526 * sizeof (uint32_t)));
12527
12528 switch (dwp_htab->section_pool.v2.section_ids[i])
12529 {
12530 case DW_SECT_INFO:
12531 case DW_SECT_TYPES:
12532 sections.info_or_types_offset = offset;
12533 sections.info_or_types_size = size;
12534 break;
12535 case DW_SECT_ABBREV:
12536 sections.abbrev_offset = offset;
12537 sections.abbrev_size = size;
12538 break;
12539 case DW_SECT_LINE:
12540 sections.line_offset = offset;
12541 sections.line_size = size;
12542 break;
12543 case DW_SECT_LOC:
12544 sections.loc_offset = offset;
12545 sections.loc_size = size;
12546 break;
12547 case DW_SECT_STR_OFFSETS:
12548 sections.str_offsets_offset = offset;
12549 sections.str_offsets_size = size;
12550 break;
12551 case DW_SECT_MACINFO:
12552 sections.macinfo_offset = offset;
12553 sections.macinfo_size = size;
12554 break;
12555 case DW_SECT_MACRO:
12556 sections.macro_offset = offset;
12557 sections.macro_size = size;
12558 break;
12559 }
12560 }
12561
12562 /* It's easier for the rest of the code if we fake a struct dwo_file and
12563 have dwo_unit "live" in that. At least for now.
12564
12565 The DWP file can be made up of a random collection of CUs and TUs.
12566 However, for each CU + set of TUs that came from the same original DWO
12567 file, we can combine them back into a virtual DWO file to save space
12568 (fewer struct dwo_file objects to allocate). Remember that for really
12569 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12570
12571 std::string virtual_dwo_name =
12572 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12573 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12574 (long) (sections.line_size ? sections.line_offset : 0),
12575 (long) (sections.loc_size ? sections.loc_offset : 0),
12576 (long) (sections.str_offsets_size
12577 ? sections.str_offsets_offset : 0));
12578 /* Can we use an existing virtual DWO file? */
12579 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12580 virtual_dwo_name.c_str (),
12581 comp_dir);
12582 /* Create one if necessary. */
12583 if (*dwo_file_slot == NULL)
12584 {
12585 if (dwarf_read_debug)
12586 {
12587 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12588 virtual_dwo_name.c_str ());
12589 }
12590 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
12591 dwo_file->dwo_name
12592 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
12593 virtual_dwo_name.c_str (),
12594 virtual_dwo_name.size ());
12595 dwo_file->comp_dir = comp_dir;
12596 dwo_file->sections.abbrev =
12597 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12598 sections.abbrev_offset, sections.abbrev_size);
12599 dwo_file->sections.line =
12600 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12601 sections.line_offset, sections.line_size);
12602 dwo_file->sections.loc =
12603 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12604 sections.loc_offset, sections.loc_size);
12605 dwo_file->sections.macinfo =
12606 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12607 sections.macinfo_offset, sections.macinfo_size);
12608 dwo_file->sections.macro =
12609 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12610 sections.macro_offset, sections.macro_size);
12611 dwo_file->sections.str_offsets =
12612 create_dwp_v2_section (dwarf2_per_objfile,
12613 &dwp_file->sections.str_offsets,
12614 sections.str_offsets_offset,
12615 sections.str_offsets_size);
12616 /* The "str" section is global to the entire DWP file. */
12617 dwo_file->sections.str = dwp_file->sections.str;
12618 /* The info or types section is assigned below to dwo_unit,
12619 there's no need to record it in dwo_file.
12620 Also, we can't simply record type sections in dwo_file because
12621 we record a pointer into the vector in dwo_unit. As we collect more
12622 types we'll grow the vector and eventually have to reallocate space
12623 for it, invalidating all copies of pointers into the previous
12624 contents. */
12625 *dwo_file_slot = dwo_file;
12626 }
12627 else
12628 {
12629 if (dwarf_read_debug)
12630 {
12631 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12632 virtual_dwo_name.c_str ());
12633 }
12634 dwo_file = (struct dwo_file *) *dwo_file_slot;
12635 }
12636
12637 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12638 dwo_unit->dwo_file = dwo_file;
12639 dwo_unit->signature = signature;
12640 dwo_unit->section =
12641 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12642 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12643 is_debug_types
12644 ? &dwp_file->sections.types
12645 : &dwp_file->sections.info,
12646 sections.info_or_types_offset,
12647 sections.info_or_types_size);
12648 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12649
12650 return dwo_unit;
12651 }
12652
12653 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12654 Returns NULL if the signature isn't found. */
12655
12656 static struct dwo_unit *
12657 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12658 struct dwp_file *dwp_file, const char *comp_dir,
12659 ULONGEST signature, int is_debug_types)
12660 {
12661 const struct dwp_hash_table *dwp_htab =
12662 is_debug_types ? dwp_file->tus : dwp_file->cus;
12663 bfd *dbfd = dwp_file->dbfd;
12664 uint32_t mask = dwp_htab->nr_slots - 1;
12665 uint32_t hash = signature & mask;
12666 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12667 unsigned int i;
12668 void **slot;
12669 struct dwo_unit find_dwo_cu;
12670
12671 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12672 find_dwo_cu.signature = signature;
12673 slot = htab_find_slot (is_debug_types
12674 ? dwp_file->loaded_tus
12675 : dwp_file->loaded_cus,
12676 &find_dwo_cu, INSERT);
12677
12678 if (*slot != NULL)
12679 return (struct dwo_unit *) *slot;
12680
12681 /* Use a for loop so that we don't loop forever on bad debug info. */
12682 for (i = 0; i < dwp_htab->nr_slots; ++i)
12683 {
12684 ULONGEST signature_in_table;
12685
12686 signature_in_table =
12687 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12688 if (signature_in_table == signature)
12689 {
12690 uint32_t unit_index =
12691 read_4_bytes (dbfd,
12692 dwp_htab->unit_table + hash * sizeof (uint32_t));
12693
12694 if (dwp_file->version == 1)
12695 {
12696 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12697 dwp_file, unit_index,
12698 comp_dir, signature,
12699 is_debug_types);
12700 }
12701 else
12702 {
12703 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12704 dwp_file, unit_index,
12705 comp_dir, signature,
12706 is_debug_types);
12707 }
12708 return (struct dwo_unit *) *slot;
12709 }
12710 if (signature_in_table == 0)
12711 return NULL;
12712 hash = (hash + hash2) & mask;
12713 }
12714
12715 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12716 " [in module %s]"),
12717 dwp_file->name);
12718 }
12719
12720 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12721 Open the file specified by FILE_NAME and hand it off to BFD for
12722 preliminary analysis. Return a newly initialized bfd *, which
12723 includes a canonicalized copy of FILE_NAME.
12724 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12725 SEARCH_CWD is true if the current directory is to be searched.
12726 It will be searched before debug-file-directory.
12727 If successful, the file is added to the bfd include table of the
12728 objfile's bfd (see gdb_bfd_record_inclusion).
12729 If unable to find/open the file, return NULL.
12730 NOTE: This function is derived from symfile_bfd_open. */
12731
12732 static gdb_bfd_ref_ptr
12733 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12734 const char *file_name, int is_dwp, int search_cwd)
12735 {
12736 int desc;
12737 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12738 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12739 to debug_file_directory. */
12740 const char *search_path;
12741 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12742
12743 gdb::unique_xmalloc_ptr<char> search_path_holder;
12744 if (search_cwd)
12745 {
12746 if (*debug_file_directory != '\0')
12747 {
12748 search_path_holder.reset (concat (".", dirname_separator_string,
12749 debug_file_directory,
12750 (char *) NULL));
12751 search_path = search_path_holder.get ();
12752 }
12753 else
12754 search_path = ".";
12755 }
12756 else
12757 search_path = debug_file_directory;
12758
12759 openp_flags flags = OPF_RETURN_REALPATH;
12760 if (is_dwp)
12761 flags |= OPF_SEARCH_IN_PATH;
12762
12763 gdb::unique_xmalloc_ptr<char> absolute_name;
12764 desc = openp (search_path, flags, file_name,
12765 O_RDONLY | O_BINARY, &absolute_name);
12766 if (desc < 0)
12767 return NULL;
12768
12769 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12770 gnutarget, desc));
12771 if (sym_bfd == NULL)
12772 return NULL;
12773 bfd_set_cacheable (sym_bfd.get (), 1);
12774
12775 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12776 return NULL;
12777
12778 /* Success. Record the bfd as having been included by the objfile's bfd.
12779 This is important because things like demangled_names_hash lives in the
12780 objfile's per_bfd space and may have references to things like symbol
12781 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12782 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12783
12784 return sym_bfd;
12785 }
12786
12787 /* Try to open DWO file FILE_NAME.
12788 COMP_DIR is the DW_AT_comp_dir attribute.
12789 The result is the bfd handle of the file.
12790 If there is a problem finding or opening the file, return NULL.
12791 Upon success, the canonicalized path of the file is stored in the bfd,
12792 same as symfile_bfd_open. */
12793
12794 static gdb_bfd_ref_ptr
12795 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12796 const char *file_name, const char *comp_dir)
12797 {
12798 if (IS_ABSOLUTE_PATH (file_name))
12799 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12800 0 /*is_dwp*/, 0 /*search_cwd*/);
12801
12802 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12803
12804 if (comp_dir != NULL)
12805 {
12806 char *path_to_try = concat (comp_dir, SLASH_STRING,
12807 file_name, (char *) NULL);
12808
12809 /* NOTE: If comp_dir is a relative path, this will also try the
12810 search path, which seems useful. */
12811 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12812 path_to_try,
12813 0 /*is_dwp*/,
12814 1 /*search_cwd*/));
12815 xfree (path_to_try);
12816 if (abfd != NULL)
12817 return abfd;
12818 }
12819
12820 /* That didn't work, try debug-file-directory, which, despite its name,
12821 is a list of paths. */
12822
12823 if (*debug_file_directory == '\0')
12824 return NULL;
12825
12826 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12827 0 /*is_dwp*/, 1 /*search_cwd*/);
12828 }
12829
12830 /* This function is mapped across the sections and remembers the offset and
12831 size of each of the DWO debugging sections we are interested in. */
12832
12833 static void
12834 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12835 {
12836 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12837 const struct dwop_section_names *names = &dwop_section_names;
12838
12839 if (section_is_p (sectp->name, &names->abbrev_dwo))
12840 {
12841 dwo_sections->abbrev.s.section = sectp;
12842 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
12843 }
12844 else if (section_is_p (sectp->name, &names->info_dwo))
12845 {
12846 dwo_sections->info.s.section = sectp;
12847 dwo_sections->info.size = bfd_get_section_size (sectp);
12848 }
12849 else if (section_is_p (sectp->name, &names->line_dwo))
12850 {
12851 dwo_sections->line.s.section = sectp;
12852 dwo_sections->line.size = bfd_get_section_size (sectp);
12853 }
12854 else if (section_is_p (sectp->name, &names->loc_dwo))
12855 {
12856 dwo_sections->loc.s.section = sectp;
12857 dwo_sections->loc.size = bfd_get_section_size (sectp);
12858 }
12859 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12860 {
12861 dwo_sections->macinfo.s.section = sectp;
12862 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
12863 }
12864 else if (section_is_p (sectp->name, &names->macro_dwo))
12865 {
12866 dwo_sections->macro.s.section = sectp;
12867 dwo_sections->macro.size = bfd_get_section_size (sectp);
12868 }
12869 else if (section_is_p (sectp->name, &names->str_dwo))
12870 {
12871 dwo_sections->str.s.section = sectp;
12872 dwo_sections->str.size = bfd_get_section_size (sectp);
12873 }
12874 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12875 {
12876 dwo_sections->str_offsets.s.section = sectp;
12877 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
12878 }
12879 else if (section_is_p (sectp->name, &names->types_dwo))
12880 {
12881 struct dwarf2_section_info type_section;
12882
12883 memset (&type_section, 0, sizeof (type_section));
12884 type_section.s.section = sectp;
12885 type_section.size = bfd_get_section_size (sectp);
12886 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
12887 &type_section);
12888 }
12889 }
12890
12891 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12892 by PER_CU. This is for the non-DWP case.
12893 The result is NULL if DWO_NAME can't be found. */
12894
12895 static struct dwo_file *
12896 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12897 const char *dwo_name, const char *comp_dir)
12898 {
12899 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12900 struct objfile *objfile = dwarf2_per_objfile->objfile;
12901
12902 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir));
12903 if (dbfd == NULL)
12904 {
12905 if (dwarf_read_debug)
12906 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12907 return NULL;
12908 }
12909
12910 /* We use a unique pointer here, despite the obstack allocation,
12911 because a dwo_file needs some cleanup if it is abandoned. */
12912 dwo_file_up dwo_file (OBSTACK_ZALLOC (&objfile->objfile_obstack,
12913 struct dwo_file));
12914 dwo_file->dwo_name = dwo_name;
12915 dwo_file->comp_dir = comp_dir;
12916 dwo_file->dbfd = dbfd.release ();
12917
12918 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
12919 &dwo_file->sections);
12920
12921 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
12922 dwo_file->cus);
12923
12924 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12925 dwo_file->sections.types, dwo_file->tus);
12926
12927 if (dwarf_read_debug)
12928 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12929
12930 return dwo_file.release ();
12931 }
12932
12933 /* This function is mapped across the sections and remembers the offset and
12934 size of each of the DWP debugging sections common to version 1 and 2 that
12935 we are interested in. */
12936
12937 static void
12938 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12939 void *dwp_file_ptr)
12940 {
12941 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12942 const struct dwop_section_names *names = &dwop_section_names;
12943 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12944
12945 /* Record the ELF section number for later lookup: this is what the
12946 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12947 gdb_assert (elf_section_nr < dwp_file->num_sections);
12948 dwp_file->elf_sections[elf_section_nr] = sectp;
12949
12950 /* Look for specific sections that we need. */
12951 if (section_is_p (sectp->name, &names->str_dwo))
12952 {
12953 dwp_file->sections.str.s.section = sectp;
12954 dwp_file->sections.str.size = bfd_get_section_size (sectp);
12955 }
12956 else if (section_is_p (sectp->name, &names->cu_index))
12957 {
12958 dwp_file->sections.cu_index.s.section = sectp;
12959 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
12960 }
12961 else if (section_is_p (sectp->name, &names->tu_index))
12962 {
12963 dwp_file->sections.tu_index.s.section = sectp;
12964 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
12965 }
12966 }
12967
12968 /* This function is mapped across the sections and remembers the offset and
12969 size of each of the DWP version 2 debugging sections that we are interested
12970 in. This is split into a separate function because we don't know if we
12971 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12972
12973 static void
12974 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12975 {
12976 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12977 const struct dwop_section_names *names = &dwop_section_names;
12978 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12979
12980 /* Record the ELF section number for later lookup: this is what the
12981 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12982 gdb_assert (elf_section_nr < dwp_file->num_sections);
12983 dwp_file->elf_sections[elf_section_nr] = sectp;
12984
12985 /* Look for specific sections that we need. */
12986 if (section_is_p (sectp->name, &names->abbrev_dwo))
12987 {
12988 dwp_file->sections.abbrev.s.section = sectp;
12989 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
12990 }
12991 else if (section_is_p (sectp->name, &names->info_dwo))
12992 {
12993 dwp_file->sections.info.s.section = sectp;
12994 dwp_file->sections.info.size = bfd_get_section_size (sectp);
12995 }
12996 else if (section_is_p (sectp->name, &names->line_dwo))
12997 {
12998 dwp_file->sections.line.s.section = sectp;
12999 dwp_file->sections.line.size = bfd_get_section_size (sectp);
13000 }
13001 else if (section_is_p (sectp->name, &names->loc_dwo))
13002 {
13003 dwp_file->sections.loc.s.section = sectp;
13004 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
13005 }
13006 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13007 {
13008 dwp_file->sections.macinfo.s.section = sectp;
13009 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
13010 }
13011 else if (section_is_p (sectp->name, &names->macro_dwo))
13012 {
13013 dwp_file->sections.macro.s.section = sectp;
13014 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
13015 }
13016 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13017 {
13018 dwp_file->sections.str_offsets.s.section = sectp;
13019 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
13020 }
13021 else if (section_is_p (sectp->name, &names->types_dwo))
13022 {
13023 dwp_file->sections.types.s.section = sectp;
13024 dwp_file->sections.types.size = bfd_get_section_size (sectp);
13025 }
13026 }
13027
13028 /* Hash function for dwp_file loaded CUs/TUs. */
13029
13030 static hashval_t
13031 hash_dwp_loaded_cutus (const void *item)
13032 {
13033 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13034
13035 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13036 return dwo_unit->signature;
13037 }
13038
13039 /* Equality function for dwp_file loaded CUs/TUs. */
13040
13041 static int
13042 eq_dwp_loaded_cutus (const void *a, const void *b)
13043 {
13044 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13045 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13046
13047 return dua->signature == dub->signature;
13048 }
13049
13050 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13051
13052 static htab_t
13053 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13054 {
13055 return htab_create_alloc_ex (3,
13056 hash_dwp_loaded_cutus,
13057 eq_dwp_loaded_cutus,
13058 NULL,
13059 &objfile->objfile_obstack,
13060 hashtab_obstack_allocate,
13061 dummy_obstack_deallocate);
13062 }
13063
13064 /* Try to open DWP file FILE_NAME.
13065 The result is the bfd handle of the file.
13066 If there is a problem finding or opening the file, return NULL.
13067 Upon success, the canonicalized path of the file is stored in the bfd,
13068 same as symfile_bfd_open. */
13069
13070 static gdb_bfd_ref_ptr
13071 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13072 const char *file_name)
13073 {
13074 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13075 1 /*is_dwp*/,
13076 1 /*search_cwd*/));
13077 if (abfd != NULL)
13078 return abfd;
13079
13080 /* Work around upstream bug 15652.
13081 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13082 [Whether that's a "bug" is debatable, but it is getting in our way.]
13083 We have no real idea where the dwp file is, because gdb's realpath-ing
13084 of the executable's path may have discarded the needed info.
13085 [IWBN if the dwp file name was recorded in the executable, akin to
13086 .gnu_debuglink, but that doesn't exist yet.]
13087 Strip the directory from FILE_NAME and search again. */
13088 if (*debug_file_directory != '\0')
13089 {
13090 /* Don't implicitly search the current directory here.
13091 If the user wants to search "." to handle this case,
13092 it must be added to debug-file-directory. */
13093 return try_open_dwop_file (dwarf2_per_objfile,
13094 lbasename (file_name), 1 /*is_dwp*/,
13095 0 /*search_cwd*/);
13096 }
13097
13098 return NULL;
13099 }
13100
13101 /* Initialize the use of the DWP file for the current objfile.
13102 By convention the name of the DWP file is ${objfile}.dwp.
13103 The result is NULL if it can't be found. */
13104
13105 static struct dwp_file *
13106 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13107 {
13108 struct objfile *objfile = dwarf2_per_objfile->objfile;
13109 struct dwp_file *dwp_file;
13110
13111 /* Try to find first .dwp for the binary file before any symbolic links
13112 resolving. */
13113
13114 /* If the objfile is a debug file, find the name of the real binary
13115 file and get the name of dwp file from there. */
13116 std::string dwp_name;
13117 if (objfile->separate_debug_objfile_backlink != NULL)
13118 {
13119 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13120 const char *backlink_basename = lbasename (backlink->original_name);
13121
13122 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13123 }
13124 else
13125 dwp_name = objfile->original_name;
13126
13127 dwp_name += ".dwp";
13128
13129 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
13130 if (dbfd == NULL
13131 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13132 {
13133 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13134 dwp_name = objfile_name (objfile);
13135 dwp_name += ".dwp";
13136 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
13137 }
13138
13139 if (dbfd == NULL)
13140 {
13141 if (dwarf_read_debug)
13142 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
13143 return NULL;
13144 }
13145 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
13146 dwp_file->name = bfd_get_filename (dbfd.get ());
13147 dwp_file->dbfd = dbfd.release ();
13148
13149 /* +1: section 0 is unused */
13150 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
13151 dwp_file->elf_sections =
13152 OBSTACK_CALLOC (&objfile->objfile_obstack,
13153 dwp_file->num_sections, asection *);
13154
13155 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
13156 dwp_file);
13157
13158 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file, 0);
13159
13160 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file, 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, dwarf2_locate_v2_dwp_sections,
13184 dwp_file);
13185
13186 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13187 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
13188
13189 if (dwarf_read_debug)
13190 {
13191 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13192 fprintf_unfiltered (gdb_stdlog,
13193 " %s CUs, %s TUs\n",
13194 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13195 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13196 }
13197
13198 return dwp_file;
13199 }
13200
13201 /* Wrapper around open_and_init_dwp_file, only open it once. */
13202
13203 static struct dwp_file *
13204 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13205 {
13206 if (! dwarf2_per_objfile->dwp_checked)
13207 {
13208 dwarf2_per_objfile->dwp_file
13209 = open_and_init_dwp_file (dwarf2_per_objfile);
13210 dwarf2_per_objfile->dwp_checked = 1;
13211 }
13212 return dwarf2_per_objfile->dwp_file;
13213 }
13214
13215 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13216 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13217 or in the DWP file for the objfile, referenced by THIS_UNIT.
13218 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13219 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13220
13221 This is called, for example, when wanting to read a variable with a
13222 complex location. Therefore we don't want to do file i/o for every call.
13223 Therefore we don't want to look for a DWO file on every call.
13224 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13225 then we check if we've already seen DWO_NAME, and only THEN do we check
13226 for a DWO file.
13227
13228 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13229 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13230
13231 static struct dwo_unit *
13232 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13233 const char *dwo_name, const char *comp_dir,
13234 ULONGEST signature, int is_debug_types)
13235 {
13236 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
13237 struct objfile *objfile = dwarf2_per_objfile->objfile;
13238 const char *kind = is_debug_types ? "TU" : "CU";
13239 void **dwo_file_slot;
13240 struct dwo_file *dwo_file;
13241 struct dwp_file *dwp_file;
13242
13243 /* First see if there's a DWP file.
13244 If we have a DWP file but didn't find the DWO inside it, don't
13245 look for the original DWO file. It makes gdb behave differently
13246 depending on whether one is debugging in the build tree. */
13247
13248 dwp_file = get_dwp_file (dwarf2_per_objfile);
13249 if (dwp_file != NULL)
13250 {
13251 const struct dwp_hash_table *dwp_htab =
13252 is_debug_types ? dwp_file->tus : dwp_file->cus;
13253
13254 if (dwp_htab != NULL)
13255 {
13256 struct dwo_unit *dwo_cutu =
13257 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
13258 signature, is_debug_types);
13259
13260 if (dwo_cutu != NULL)
13261 {
13262 if (dwarf_read_debug)
13263 {
13264 fprintf_unfiltered (gdb_stdlog,
13265 "Virtual DWO %s %s found: @%s\n",
13266 kind, hex_string (signature),
13267 host_address_to_string (dwo_cutu));
13268 }
13269 return dwo_cutu;
13270 }
13271 }
13272 }
13273 else
13274 {
13275 /* No DWP file, look for the DWO file. */
13276
13277 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13278 dwo_name, comp_dir);
13279 if (*dwo_file_slot == NULL)
13280 {
13281 /* Read in the file and build a table of the CUs/TUs it contains. */
13282 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
13283 }
13284 /* NOTE: This will be NULL if unable to open the file. */
13285 dwo_file = (struct dwo_file *) *dwo_file_slot;
13286
13287 if (dwo_file != NULL)
13288 {
13289 struct dwo_unit *dwo_cutu = NULL;
13290
13291 if (is_debug_types && dwo_file->tus)
13292 {
13293 struct dwo_unit find_dwo_cutu;
13294
13295 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13296 find_dwo_cutu.signature = signature;
13297 dwo_cutu
13298 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
13299 }
13300 else if (!is_debug_types && dwo_file->cus)
13301 {
13302 struct dwo_unit find_dwo_cutu;
13303
13304 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13305 find_dwo_cutu.signature = signature;
13306 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13307 &find_dwo_cutu);
13308 }
13309
13310 if (dwo_cutu != NULL)
13311 {
13312 if (dwarf_read_debug)
13313 {
13314 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13315 kind, dwo_name, hex_string (signature),
13316 host_address_to_string (dwo_cutu));
13317 }
13318 return dwo_cutu;
13319 }
13320 }
13321 }
13322
13323 /* We didn't find it. This could mean a dwo_id mismatch, or
13324 someone deleted the DWO/DWP file, or the search path isn't set up
13325 correctly to find the file. */
13326
13327 if (dwarf_read_debug)
13328 {
13329 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13330 kind, dwo_name, hex_string (signature));
13331 }
13332
13333 /* This is a warning and not a complaint because it can be caused by
13334 pilot error (e.g., user accidentally deleting the DWO). */
13335 {
13336 /* Print the name of the DWP file if we looked there, helps the user
13337 better diagnose the problem. */
13338 std::string dwp_text;
13339
13340 if (dwp_file != NULL)
13341 dwp_text = string_printf (" [in DWP file %s]",
13342 lbasename (dwp_file->name));
13343
13344 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13345 " [in module %s]"),
13346 kind, dwo_name, hex_string (signature),
13347 dwp_text.c_str (),
13348 this_unit->is_debug_types ? "TU" : "CU",
13349 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
13350 }
13351 return NULL;
13352 }
13353
13354 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13355 See lookup_dwo_cutu_unit for details. */
13356
13357 static struct dwo_unit *
13358 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13359 const char *dwo_name, const char *comp_dir,
13360 ULONGEST signature)
13361 {
13362 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13363 }
13364
13365 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13366 See lookup_dwo_cutu_unit for details. */
13367
13368 static struct dwo_unit *
13369 lookup_dwo_type_unit (struct signatured_type *this_tu,
13370 const char *dwo_name, const char *comp_dir)
13371 {
13372 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13373 }
13374
13375 /* Traversal function for queue_and_load_all_dwo_tus. */
13376
13377 static int
13378 queue_and_load_dwo_tu (void **slot, void *info)
13379 {
13380 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13381 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13382 ULONGEST signature = dwo_unit->signature;
13383 struct signatured_type *sig_type =
13384 lookup_dwo_signatured_type (per_cu->cu, signature);
13385
13386 if (sig_type != NULL)
13387 {
13388 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13389
13390 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13391 a real dependency of PER_CU on SIG_TYPE. That is detected later
13392 while processing PER_CU. */
13393 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13394 load_full_type_unit (sig_cu);
13395 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
13396 }
13397
13398 return 1;
13399 }
13400
13401 /* Queue all TUs contained in the DWO of PER_CU to be read in.
13402 The DWO may have the only definition of the type, though it may not be
13403 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13404 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13405
13406 static void
13407 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13408 {
13409 struct dwo_unit *dwo_unit;
13410 struct dwo_file *dwo_file;
13411
13412 gdb_assert (!per_cu->is_debug_types);
13413 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
13414 gdb_assert (per_cu->cu != NULL);
13415
13416 dwo_unit = per_cu->cu->dwo_unit;
13417 gdb_assert (dwo_unit != NULL);
13418
13419 dwo_file = dwo_unit->dwo_file;
13420 if (dwo_file->tus != NULL)
13421 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13422 }
13423
13424 /* Free all resources associated with DWO_FILE.
13425 Close the DWO file and munmap the sections. */
13426
13427 static void
13428 free_dwo_file (struct dwo_file *dwo_file)
13429 {
13430 /* Note: dbfd is NULL for virtual DWO files. */
13431 gdb_bfd_unref (dwo_file->dbfd);
13432
13433 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
13434 }
13435
13436 /* Traversal function for free_dwo_files. */
13437
13438 static int
13439 free_dwo_file_from_slot (void **slot, void *info)
13440 {
13441 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
13442
13443 free_dwo_file (dwo_file);
13444
13445 return 1;
13446 }
13447
13448 /* Free all resources associated with DWO_FILES. */
13449
13450 static void
13451 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
13452 {
13453 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
13454 }
13455 \f
13456 /* Read in various DIEs. */
13457
13458 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13459 Inherit only the children of the DW_AT_abstract_origin DIE not being
13460 already referenced by DW_AT_abstract_origin from the children of the
13461 current DIE. */
13462
13463 static void
13464 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13465 {
13466 struct die_info *child_die;
13467 sect_offset *offsetp;
13468 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13469 struct die_info *origin_die;
13470 /* Iterator of the ORIGIN_DIE children. */
13471 struct die_info *origin_child_die;
13472 struct attribute *attr;
13473 struct dwarf2_cu *origin_cu;
13474 struct pending **origin_previous_list_in_scope;
13475
13476 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13477 if (!attr)
13478 return;
13479
13480 /* Note that following die references may follow to a die in a
13481 different cu. */
13482
13483 origin_cu = cu;
13484 origin_die = follow_die_ref (die, attr, &origin_cu);
13485
13486 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13487 symbols in. */
13488 origin_previous_list_in_scope = origin_cu->list_in_scope;
13489 origin_cu->list_in_scope = cu->list_in_scope;
13490
13491 if (die->tag != origin_die->tag
13492 && !(die->tag == DW_TAG_inlined_subroutine
13493 && origin_die->tag == DW_TAG_subprogram))
13494 complaint (&symfile_complaints,
13495 _("DIE %s and its abstract origin %s have different tags"),
13496 sect_offset_str (die->sect_off),
13497 sect_offset_str (origin_die->sect_off));
13498
13499 std::vector<sect_offset> offsets;
13500
13501 for (child_die = die->child;
13502 child_die && child_die->tag;
13503 child_die = sibling_die (child_die))
13504 {
13505 struct die_info *child_origin_die;
13506 struct dwarf2_cu *child_origin_cu;
13507
13508 /* We are trying to process concrete instance entries:
13509 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13510 it's not relevant to our analysis here. i.e. detecting DIEs that are
13511 present in the abstract instance but not referenced in the concrete
13512 one. */
13513 if (child_die->tag == DW_TAG_call_site
13514 || child_die->tag == DW_TAG_GNU_call_site)
13515 continue;
13516
13517 /* For each CHILD_DIE, find the corresponding child of
13518 ORIGIN_DIE. If there is more than one layer of
13519 DW_AT_abstract_origin, follow them all; there shouldn't be,
13520 but GCC versions at least through 4.4 generate this (GCC PR
13521 40573). */
13522 child_origin_die = child_die;
13523 child_origin_cu = cu;
13524 while (1)
13525 {
13526 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13527 child_origin_cu);
13528 if (attr == NULL)
13529 break;
13530 child_origin_die = follow_die_ref (child_origin_die, attr,
13531 &child_origin_cu);
13532 }
13533
13534 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13535 counterpart may exist. */
13536 if (child_origin_die != child_die)
13537 {
13538 if (child_die->tag != child_origin_die->tag
13539 && !(child_die->tag == DW_TAG_inlined_subroutine
13540 && child_origin_die->tag == DW_TAG_subprogram))
13541 complaint (&symfile_complaints,
13542 _("Child DIE %s and its abstract origin %s have "
13543 "different tags"),
13544 sect_offset_str (child_die->sect_off),
13545 sect_offset_str (child_origin_die->sect_off));
13546 if (child_origin_die->parent != origin_die)
13547 complaint (&symfile_complaints,
13548 _("Child DIE %s and its abstract origin %s have "
13549 "different parents"),
13550 sect_offset_str (child_die->sect_off),
13551 sect_offset_str (child_origin_die->sect_off));
13552 else
13553 offsets.push_back (child_origin_die->sect_off);
13554 }
13555 }
13556 std::sort (offsets.begin (), offsets.end ());
13557 sect_offset *offsets_end = offsets.data () + offsets.size ();
13558 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13559 if (offsetp[-1] == *offsetp)
13560 complaint (&symfile_complaints,
13561 _("Multiple children of DIE %s refer "
13562 "to DIE %s as their abstract origin"),
13563 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13564
13565 offsetp = offsets.data ();
13566 origin_child_die = origin_die->child;
13567 while (origin_child_die && origin_child_die->tag)
13568 {
13569 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13570 while (offsetp < offsets_end
13571 && *offsetp < origin_child_die->sect_off)
13572 offsetp++;
13573 if (offsetp >= offsets_end
13574 || *offsetp > origin_child_die->sect_off)
13575 {
13576 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13577 Check whether we're already processing ORIGIN_CHILD_DIE.
13578 This can happen with mutually referenced abstract_origins.
13579 PR 16581. */
13580 if (!origin_child_die->in_process)
13581 process_die (origin_child_die, origin_cu);
13582 }
13583 origin_child_die = sibling_die (origin_child_die);
13584 }
13585 origin_cu->list_in_scope = origin_previous_list_in_scope;
13586 }
13587
13588 static void
13589 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13590 {
13591 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13592 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13593 struct context_stack *newobj;
13594 CORE_ADDR lowpc;
13595 CORE_ADDR highpc;
13596 struct die_info *child_die;
13597 struct attribute *attr, *call_line, *call_file;
13598 const char *name;
13599 CORE_ADDR baseaddr;
13600 struct block *block;
13601 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13602 std::vector<struct symbol *> template_args;
13603 struct template_symbol *templ_func = NULL;
13604
13605 if (inlined_func)
13606 {
13607 /* If we do not have call site information, we can't show the
13608 caller of this inlined function. That's too confusing, so
13609 only use the scope for local variables. */
13610 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13611 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13612 if (call_line == NULL || call_file == NULL)
13613 {
13614 read_lexical_block_scope (die, cu);
13615 return;
13616 }
13617 }
13618
13619 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13620
13621 name = dwarf2_name (die, cu);
13622
13623 /* Ignore functions with missing or empty names. These are actually
13624 illegal according to the DWARF standard. */
13625 if (name == NULL)
13626 {
13627 complaint (&symfile_complaints,
13628 _("missing name for subprogram DIE at %s"),
13629 sect_offset_str (die->sect_off));
13630 return;
13631 }
13632
13633 /* Ignore functions with missing or invalid low and high pc attributes. */
13634 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13635 <= PC_BOUNDS_INVALID)
13636 {
13637 attr = dwarf2_attr (die, DW_AT_external, cu);
13638 if (!attr || !DW_UNSND (attr))
13639 complaint (&symfile_complaints,
13640 _("cannot get low and high bounds "
13641 "for subprogram DIE at %s"),
13642 sect_offset_str (die->sect_off));
13643 return;
13644 }
13645
13646 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13647 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13648
13649 /* If we have any template arguments, then we must allocate a
13650 different sort of symbol. */
13651 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13652 {
13653 if (child_die->tag == DW_TAG_template_type_param
13654 || child_die->tag == DW_TAG_template_value_param)
13655 {
13656 templ_func = allocate_template_symbol (objfile);
13657 templ_func->subclass = SYMBOL_TEMPLATE;
13658 break;
13659 }
13660 }
13661
13662 newobj = push_context (0, lowpc);
13663 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13664 (struct symbol *) templ_func);
13665
13666 /* If there is a location expression for DW_AT_frame_base, record
13667 it. */
13668 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13669 if (attr)
13670 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13671
13672 /* If there is a location for the static link, record it. */
13673 newobj->static_link = NULL;
13674 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13675 if (attr)
13676 {
13677 newobj->static_link
13678 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13679 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
13680 }
13681
13682 cu->list_in_scope = &local_symbols;
13683
13684 if (die->child != NULL)
13685 {
13686 child_die = die->child;
13687 while (child_die && child_die->tag)
13688 {
13689 if (child_die->tag == DW_TAG_template_type_param
13690 || child_die->tag == DW_TAG_template_value_param)
13691 {
13692 struct symbol *arg = new_symbol (child_die, NULL, cu);
13693
13694 if (arg != NULL)
13695 template_args.push_back (arg);
13696 }
13697 else
13698 process_die (child_die, cu);
13699 child_die = sibling_die (child_die);
13700 }
13701 }
13702
13703 inherit_abstract_dies (die, cu);
13704
13705 /* If we have a DW_AT_specification, we might need to import using
13706 directives from the context of the specification DIE. See the
13707 comment in determine_prefix. */
13708 if (cu->language == language_cplus
13709 && dwarf2_attr (die, DW_AT_specification, cu))
13710 {
13711 struct dwarf2_cu *spec_cu = cu;
13712 struct die_info *spec_die = die_specification (die, &spec_cu);
13713
13714 while (spec_die)
13715 {
13716 child_die = spec_die->child;
13717 while (child_die && child_die->tag)
13718 {
13719 if (child_die->tag == DW_TAG_imported_module)
13720 process_die (child_die, spec_cu);
13721 child_die = sibling_die (child_die);
13722 }
13723
13724 /* In some cases, GCC generates specification DIEs that
13725 themselves contain DW_AT_specification attributes. */
13726 spec_die = die_specification (spec_die, &spec_cu);
13727 }
13728 }
13729
13730 newobj = pop_context ();
13731 /* Make a block for the local symbols within. */
13732 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
13733 newobj->static_link, lowpc, highpc);
13734
13735 /* For C++, set the block's scope. */
13736 if ((cu->language == language_cplus
13737 || cu->language == language_fortran
13738 || cu->language == language_d
13739 || cu->language == language_rust)
13740 && cu->processing_has_namespace_info)
13741 block_set_scope (block, determine_prefix (die, cu),
13742 &objfile->objfile_obstack);
13743
13744 /* If we have address ranges, record them. */
13745 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13746
13747 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
13748
13749 /* Attach template arguments to function. */
13750 if (!template_args.empty ())
13751 {
13752 gdb_assert (templ_func != NULL);
13753
13754 templ_func->n_template_arguments = template_args.size ();
13755 templ_func->template_arguments
13756 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13757 templ_func->n_template_arguments);
13758 memcpy (templ_func->template_arguments,
13759 template_args.data (),
13760 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13761 }
13762
13763 /* In C++, we can have functions nested inside functions (e.g., when
13764 a function declares a class that has methods). This means that
13765 when we finish processing a function scope, we may need to go
13766 back to building a containing block's symbol lists. */
13767 local_symbols = newobj->locals;
13768 local_using_directives = newobj->local_using_directives;
13769
13770 /* If we've finished processing a top-level function, subsequent
13771 symbols go in the file symbol list. */
13772 if (outermost_context_p ())
13773 cu->list_in_scope = &file_symbols;
13774 }
13775
13776 /* Process all the DIES contained within a lexical block scope. Start
13777 a new scope, process the dies, and then close the scope. */
13778
13779 static void
13780 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13781 {
13782 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13783 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13784 struct context_stack *newobj;
13785 CORE_ADDR lowpc, highpc;
13786 struct die_info *child_die;
13787 CORE_ADDR baseaddr;
13788
13789 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13790
13791 /* Ignore blocks with missing or invalid low and high pc attributes. */
13792 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13793 as multiple lexical blocks? Handling children in a sane way would
13794 be nasty. Might be easier to properly extend generic blocks to
13795 describe ranges. */
13796 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13797 {
13798 case PC_BOUNDS_NOT_PRESENT:
13799 /* DW_TAG_lexical_block has no attributes, process its children as if
13800 there was no wrapping by that DW_TAG_lexical_block.
13801 GCC does no longer produces such DWARF since GCC r224161. */
13802 for (child_die = die->child;
13803 child_die != NULL && child_die->tag;
13804 child_die = sibling_die (child_die))
13805 process_die (child_die, cu);
13806 return;
13807 case PC_BOUNDS_INVALID:
13808 return;
13809 }
13810 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13811 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13812
13813 push_context (0, lowpc);
13814 if (die->child != NULL)
13815 {
13816 child_die = die->child;
13817 while (child_die && child_die->tag)
13818 {
13819 process_die (child_die, cu);
13820 child_die = sibling_die (child_die);
13821 }
13822 }
13823 inherit_abstract_dies (die, cu);
13824 newobj = pop_context ();
13825
13826 if (local_symbols != NULL || local_using_directives != NULL)
13827 {
13828 struct block *block
13829 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
13830 newobj->start_addr, highpc);
13831
13832 /* Note that recording ranges after traversing children, as we
13833 do here, means that recording a parent's ranges entails
13834 walking across all its children's ranges as they appear in
13835 the address map, which is quadratic behavior.
13836
13837 It would be nicer to record the parent's ranges before
13838 traversing its children, simply overriding whatever you find
13839 there. But since we don't even decide whether to create a
13840 block until after we've traversed its children, that's hard
13841 to do. */
13842 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13843 }
13844 local_symbols = newobj->locals;
13845 local_using_directives = newobj->local_using_directives;
13846 }
13847
13848 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13849
13850 static void
13851 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13852 {
13853 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13854 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13855 CORE_ADDR pc, baseaddr;
13856 struct attribute *attr;
13857 struct call_site *call_site, call_site_local;
13858 void **slot;
13859 int nparams;
13860 struct die_info *child_die;
13861
13862 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13863
13864 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13865 if (attr == NULL)
13866 {
13867 /* This was a pre-DWARF-5 GNU extension alias
13868 for DW_AT_call_return_pc. */
13869 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13870 }
13871 if (!attr)
13872 {
13873 complaint (&symfile_complaints,
13874 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
13875 "DIE %s [in module %s]"),
13876 sect_offset_str (die->sect_off), objfile_name (objfile));
13877 return;
13878 }
13879 pc = attr_value_as_address (attr) + baseaddr;
13880 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13881
13882 if (cu->call_site_htab == NULL)
13883 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13884 NULL, &objfile->objfile_obstack,
13885 hashtab_obstack_allocate, NULL);
13886 call_site_local.pc = pc;
13887 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13888 if (*slot != NULL)
13889 {
13890 complaint (&symfile_complaints,
13891 _("Duplicate PC %s for DW_TAG_call_site "
13892 "DIE %s [in module %s]"),
13893 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13894 objfile_name (objfile));
13895 return;
13896 }
13897
13898 /* Count parameters at the caller. */
13899
13900 nparams = 0;
13901 for (child_die = die->child; child_die && child_die->tag;
13902 child_die = sibling_die (child_die))
13903 {
13904 if (child_die->tag != DW_TAG_call_site_parameter
13905 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13906 {
13907 complaint (&symfile_complaints,
13908 _("Tag %d is not DW_TAG_call_site_parameter in "
13909 "DW_TAG_call_site child DIE %s [in module %s]"),
13910 child_die->tag, sect_offset_str (child_die->sect_off),
13911 objfile_name (objfile));
13912 continue;
13913 }
13914
13915 nparams++;
13916 }
13917
13918 call_site
13919 = ((struct call_site *)
13920 obstack_alloc (&objfile->objfile_obstack,
13921 sizeof (*call_site)
13922 + (sizeof (*call_site->parameter) * (nparams - 1))));
13923 *slot = call_site;
13924 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13925 call_site->pc = pc;
13926
13927 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13928 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13929 {
13930 struct die_info *func_die;
13931
13932 /* Skip also over DW_TAG_inlined_subroutine. */
13933 for (func_die = die->parent;
13934 func_die && func_die->tag != DW_TAG_subprogram
13935 && func_die->tag != DW_TAG_subroutine_type;
13936 func_die = func_die->parent);
13937
13938 /* DW_AT_call_all_calls is a superset
13939 of DW_AT_call_all_tail_calls. */
13940 if (func_die
13941 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13942 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13943 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13944 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13945 {
13946 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13947 not complete. But keep CALL_SITE for look ups via call_site_htab,
13948 both the initial caller containing the real return address PC and
13949 the final callee containing the current PC of a chain of tail
13950 calls do not need to have the tail call list complete. But any
13951 function candidate for a virtual tail call frame searched via
13952 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13953 determined unambiguously. */
13954 }
13955 else
13956 {
13957 struct type *func_type = NULL;
13958
13959 if (func_die)
13960 func_type = get_die_type (func_die, cu);
13961 if (func_type != NULL)
13962 {
13963 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13964
13965 /* Enlist this call site to the function. */
13966 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13967 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13968 }
13969 else
13970 complaint (&symfile_complaints,
13971 _("Cannot find function owning DW_TAG_call_site "
13972 "DIE %s [in module %s]"),
13973 sect_offset_str (die->sect_off), objfile_name (objfile));
13974 }
13975 }
13976
13977 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13978 if (attr == NULL)
13979 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13980 if (attr == NULL)
13981 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13982 if (attr == NULL)
13983 {
13984 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13985 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13986 }
13987 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13988 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
13989 /* Keep NULL DWARF_BLOCK. */;
13990 else if (attr_form_is_block (attr))
13991 {
13992 struct dwarf2_locexpr_baton *dlbaton;
13993
13994 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13995 dlbaton->data = DW_BLOCK (attr)->data;
13996 dlbaton->size = DW_BLOCK (attr)->size;
13997 dlbaton->per_cu = cu->per_cu;
13998
13999 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14000 }
14001 else if (attr_form_is_ref (attr))
14002 {
14003 struct dwarf2_cu *target_cu = cu;
14004 struct die_info *target_die;
14005
14006 target_die = follow_die_ref (die, attr, &target_cu);
14007 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
14008 if (die_is_declaration (target_die, target_cu))
14009 {
14010 const char *target_physname;
14011
14012 /* Prefer the mangled name; otherwise compute the demangled one. */
14013 target_physname = dw2_linkage_name (target_die, target_cu);
14014 if (target_physname == NULL)
14015 target_physname = dwarf2_physname (NULL, target_die, target_cu);
14016 if (target_physname == NULL)
14017 complaint (&symfile_complaints,
14018 _("DW_AT_call_target target DIE has invalid "
14019 "physname, for referencing DIE %s [in module %s]"),
14020 sect_offset_str (die->sect_off), objfile_name (objfile));
14021 else
14022 SET_FIELD_PHYSNAME (call_site->target, target_physname);
14023 }
14024 else
14025 {
14026 CORE_ADDR lowpc;
14027
14028 /* DW_AT_entry_pc should be preferred. */
14029 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14030 <= PC_BOUNDS_INVALID)
14031 complaint (&symfile_complaints,
14032 _("DW_AT_call_target target DIE has invalid "
14033 "low pc, for referencing DIE %s [in module %s]"),
14034 sect_offset_str (die->sect_off), objfile_name (objfile));
14035 else
14036 {
14037 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14038 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14039 }
14040 }
14041 }
14042 else
14043 complaint (&symfile_complaints,
14044 _("DW_TAG_call_site DW_AT_call_target is neither "
14045 "block nor reference, for DIE %s [in module %s]"),
14046 sect_offset_str (die->sect_off), objfile_name (objfile));
14047
14048 call_site->per_cu = cu->per_cu;
14049
14050 for (child_die = die->child;
14051 child_die && child_die->tag;
14052 child_die = sibling_die (child_die))
14053 {
14054 struct call_site_parameter *parameter;
14055 struct attribute *loc, *origin;
14056
14057 if (child_die->tag != DW_TAG_call_site_parameter
14058 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14059 {
14060 /* Already printed the complaint above. */
14061 continue;
14062 }
14063
14064 gdb_assert (call_site->parameter_count < nparams);
14065 parameter = &call_site->parameter[call_site->parameter_count];
14066
14067 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14068 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14069 register is contained in DW_AT_call_value. */
14070
14071 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14072 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14073 if (origin == NULL)
14074 {
14075 /* This was a pre-DWARF-5 GNU extension alias
14076 for DW_AT_call_parameter. */
14077 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14078 }
14079 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
14080 {
14081 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14082
14083 sect_offset sect_off
14084 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14085 if (!offset_in_cu_p (&cu->header, sect_off))
14086 {
14087 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14088 binding can be done only inside one CU. Such referenced DIE
14089 therefore cannot be even moved to DW_TAG_partial_unit. */
14090 complaint (&symfile_complaints,
14091 _("DW_AT_call_parameter offset is not in CU for "
14092 "DW_TAG_call_site child DIE %s [in module %s]"),
14093 sect_offset_str (child_die->sect_off),
14094 objfile_name (objfile));
14095 continue;
14096 }
14097 parameter->u.param_cu_off
14098 = (cu_offset) (sect_off - cu->header.sect_off);
14099 }
14100 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
14101 {
14102 complaint (&symfile_complaints,
14103 _("No DW_FORM_block* DW_AT_location for "
14104 "DW_TAG_call_site child DIE %s [in module %s]"),
14105 sect_offset_str (child_die->sect_off), objfile_name (objfile));
14106 continue;
14107 }
14108 else
14109 {
14110 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14111 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14112 if (parameter->u.dwarf_reg != -1)
14113 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14114 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14115 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14116 &parameter->u.fb_offset))
14117 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14118 else
14119 {
14120 complaint (&symfile_complaints,
14121 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
14122 "for DW_FORM_block* DW_AT_location is supported for "
14123 "DW_TAG_call_site child DIE %s "
14124 "[in module %s]"),
14125 sect_offset_str (child_die->sect_off),
14126 objfile_name (objfile));
14127 continue;
14128 }
14129 }
14130
14131 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14132 if (attr == NULL)
14133 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14134 if (!attr_form_is_block (attr))
14135 {
14136 complaint (&symfile_complaints,
14137 _("No DW_FORM_block* DW_AT_call_value for "
14138 "DW_TAG_call_site child DIE %s [in module %s]"),
14139 sect_offset_str (child_die->sect_off),
14140 objfile_name (objfile));
14141 continue;
14142 }
14143 parameter->value = DW_BLOCK (attr)->data;
14144 parameter->value_size = DW_BLOCK (attr)->size;
14145
14146 /* Parameters are not pre-cleared by memset above. */
14147 parameter->data_value = NULL;
14148 parameter->data_value_size = 0;
14149 call_site->parameter_count++;
14150
14151 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14152 if (attr == NULL)
14153 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14154 if (attr)
14155 {
14156 if (!attr_form_is_block (attr))
14157 complaint (&symfile_complaints,
14158 _("No DW_FORM_block* DW_AT_call_data_value for "
14159 "DW_TAG_call_site child DIE %s [in module %s]"),
14160 sect_offset_str (child_die->sect_off),
14161 objfile_name (objfile));
14162 else
14163 {
14164 parameter->data_value = DW_BLOCK (attr)->data;
14165 parameter->data_value_size = DW_BLOCK (attr)->size;
14166 }
14167 }
14168 }
14169 }
14170
14171 /* Helper function for read_variable. If DIE represents a virtual
14172 table, then return the type of the concrete object that is
14173 associated with the virtual table. Otherwise, return NULL. */
14174
14175 static struct type *
14176 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14177 {
14178 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14179 if (attr == NULL)
14180 return NULL;
14181
14182 /* Find the type DIE. */
14183 struct die_info *type_die = NULL;
14184 struct dwarf2_cu *type_cu = cu;
14185
14186 if (attr_form_is_ref (attr))
14187 type_die = follow_die_ref (die, attr, &type_cu);
14188 if (type_die == NULL)
14189 return NULL;
14190
14191 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14192 return NULL;
14193 return die_containing_type (type_die, type_cu);
14194 }
14195
14196 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14197
14198 static void
14199 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14200 {
14201 struct rust_vtable_symbol *storage = NULL;
14202
14203 if (cu->language == language_rust)
14204 {
14205 struct type *containing_type = rust_containing_type (die, cu);
14206
14207 if (containing_type != NULL)
14208 {
14209 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14210
14211 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14212 struct rust_vtable_symbol);
14213 initialize_objfile_symbol (storage);
14214 storage->concrete_type = containing_type;
14215 storage->subclass = SYMBOL_RUST_VTABLE;
14216 }
14217 }
14218
14219 new_symbol (die, NULL, cu, storage);
14220 }
14221
14222 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14223 reading .debug_rnglists.
14224 Callback's type should be:
14225 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14226 Return true if the attributes are present and valid, otherwise,
14227 return false. */
14228
14229 template <typename Callback>
14230 static bool
14231 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14232 Callback &&callback)
14233 {
14234 struct dwarf2_per_objfile *dwarf2_per_objfile
14235 = cu->per_cu->dwarf2_per_objfile;
14236 struct objfile *objfile = dwarf2_per_objfile->objfile;
14237 bfd *obfd = objfile->obfd;
14238 /* Base address selection entry. */
14239 CORE_ADDR base;
14240 int found_base;
14241 const gdb_byte *buffer;
14242 CORE_ADDR baseaddr;
14243 bool overflow = false;
14244
14245 found_base = cu->base_known;
14246 base = cu->base_address;
14247
14248 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14249 if (offset >= dwarf2_per_objfile->rnglists.size)
14250 {
14251 complaint (&symfile_complaints,
14252 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14253 offset);
14254 return false;
14255 }
14256 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14257
14258 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14259
14260 while (1)
14261 {
14262 /* Initialize it due to a false compiler warning. */
14263 CORE_ADDR range_beginning = 0, range_end = 0;
14264 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14265 + dwarf2_per_objfile->rnglists.size);
14266 unsigned int bytes_read;
14267
14268 if (buffer == buf_end)
14269 {
14270 overflow = true;
14271 break;
14272 }
14273 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14274 switch (rlet)
14275 {
14276 case DW_RLE_end_of_list:
14277 break;
14278 case DW_RLE_base_address:
14279 if (buffer + cu->header.addr_size > buf_end)
14280 {
14281 overflow = true;
14282 break;
14283 }
14284 base = read_address (obfd, buffer, cu, &bytes_read);
14285 found_base = 1;
14286 buffer += bytes_read;
14287 break;
14288 case DW_RLE_start_length:
14289 if (buffer + cu->header.addr_size > buf_end)
14290 {
14291 overflow = true;
14292 break;
14293 }
14294 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14295 buffer += bytes_read;
14296 range_end = (range_beginning
14297 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14298 buffer += bytes_read;
14299 if (buffer > buf_end)
14300 {
14301 overflow = true;
14302 break;
14303 }
14304 break;
14305 case DW_RLE_offset_pair:
14306 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14307 buffer += bytes_read;
14308 if (buffer > buf_end)
14309 {
14310 overflow = true;
14311 break;
14312 }
14313 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14314 buffer += bytes_read;
14315 if (buffer > buf_end)
14316 {
14317 overflow = true;
14318 break;
14319 }
14320 break;
14321 case DW_RLE_start_end:
14322 if (buffer + 2 * cu->header.addr_size > buf_end)
14323 {
14324 overflow = true;
14325 break;
14326 }
14327 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14328 buffer += bytes_read;
14329 range_end = read_address (obfd, buffer, cu, &bytes_read);
14330 buffer += bytes_read;
14331 break;
14332 default:
14333 complaint (&symfile_complaints,
14334 _("Invalid .debug_rnglists data (no base address)"));
14335 return false;
14336 }
14337 if (rlet == DW_RLE_end_of_list || overflow)
14338 break;
14339 if (rlet == DW_RLE_base_address)
14340 continue;
14341
14342 if (!found_base)
14343 {
14344 /* We have no valid base address for the ranges
14345 data. */
14346 complaint (&symfile_complaints,
14347 _("Invalid .debug_rnglists data (no base address)"));
14348 return false;
14349 }
14350
14351 if (range_beginning > range_end)
14352 {
14353 /* Inverted range entries are invalid. */
14354 complaint (&symfile_complaints,
14355 _("Invalid .debug_rnglists data (inverted range)"));
14356 return false;
14357 }
14358
14359 /* Empty range entries have no effect. */
14360 if (range_beginning == range_end)
14361 continue;
14362
14363 range_beginning += base;
14364 range_end += base;
14365
14366 /* A not-uncommon case of bad debug info.
14367 Don't pollute the addrmap with bad data. */
14368 if (range_beginning + baseaddr == 0
14369 && !dwarf2_per_objfile->has_section_at_zero)
14370 {
14371 complaint (&symfile_complaints,
14372 _(".debug_rnglists entry has start address of zero"
14373 " [in module %s]"), objfile_name (objfile));
14374 continue;
14375 }
14376
14377 callback (range_beginning, range_end);
14378 }
14379
14380 if (overflow)
14381 {
14382 complaint (&symfile_complaints,
14383 _("Offset %d is not terminated "
14384 "for DW_AT_ranges attribute"),
14385 offset);
14386 return false;
14387 }
14388
14389 return true;
14390 }
14391
14392 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14393 Callback's type should be:
14394 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14395 Return 1 if the attributes are present and valid, otherwise, return 0. */
14396
14397 template <typename Callback>
14398 static int
14399 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
14400 Callback &&callback)
14401 {
14402 struct dwarf2_per_objfile *dwarf2_per_objfile
14403 = cu->per_cu->dwarf2_per_objfile;
14404 struct objfile *objfile = dwarf2_per_objfile->objfile;
14405 struct comp_unit_head *cu_header = &cu->header;
14406 bfd *obfd = objfile->obfd;
14407 unsigned int addr_size = cu_header->addr_size;
14408 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14409 /* Base address selection entry. */
14410 CORE_ADDR base;
14411 int found_base;
14412 unsigned int dummy;
14413 const gdb_byte *buffer;
14414 CORE_ADDR baseaddr;
14415
14416 if (cu_header->version >= 5)
14417 return dwarf2_rnglists_process (offset, cu, callback);
14418
14419 found_base = cu->base_known;
14420 base = cu->base_address;
14421
14422 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
14423 if (offset >= dwarf2_per_objfile->ranges.size)
14424 {
14425 complaint (&symfile_complaints,
14426 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14427 offset);
14428 return 0;
14429 }
14430 buffer = dwarf2_per_objfile->ranges.buffer + offset;
14431
14432 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14433
14434 while (1)
14435 {
14436 CORE_ADDR range_beginning, range_end;
14437
14438 range_beginning = read_address (obfd, buffer, cu, &dummy);
14439 buffer += addr_size;
14440 range_end = read_address (obfd, buffer, cu, &dummy);
14441 buffer += addr_size;
14442 offset += 2 * addr_size;
14443
14444 /* An end of list marker is a pair of zero addresses. */
14445 if (range_beginning == 0 && range_end == 0)
14446 /* Found the end of list entry. */
14447 break;
14448
14449 /* Each base address selection entry is a pair of 2 values.
14450 The first is the largest possible address, the second is
14451 the base address. Check for a base address here. */
14452 if ((range_beginning & mask) == mask)
14453 {
14454 /* If we found the largest possible address, then we already
14455 have the base address in range_end. */
14456 base = range_end;
14457 found_base = 1;
14458 continue;
14459 }
14460
14461 if (!found_base)
14462 {
14463 /* We have no valid base address for the ranges
14464 data. */
14465 complaint (&symfile_complaints,
14466 _("Invalid .debug_ranges data (no base address)"));
14467 return 0;
14468 }
14469
14470 if (range_beginning > range_end)
14471 {
14472 /* Inverted range entries are invalid. */
14473 complaint (&symfile_complaints,
14474 _("Invalid .debug_ranges data (inverted range)"));
14475 return 0;
14476 }
14477
14478 /* Empty range entries have no effect. */
14479 if (range_beginning == range_end)
14480 continue;
14481
14482 range_beginning += base;
14483 range_end += base;
14484
14485 /* A not-uncommon case of bad debug info.
14486 Don't pollute the addrmap with bad data. */
14487 if (range_beginning + baseaddr == 0
14488 && !dwarf2_per_objfile->has_section_at_zero)
14489 {
14490 complaint (&symfile_complaints,
14491 _(".debug_ranges entry has start address of zero"
14492 " [in module %s]"), objfile_name (objfile));
14493 continue;
14494 }
14495
14496 callback (range_beginning, range_end);
14497 }
14498
14499 return 1;
14500 }
14501
14502 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14503 Return 1 if the attributes are present and valid, otherwise, return 0.
14504 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14505
14506 static int
14507 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14508 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14509 struct partial_symtab *ranges_pst)
14510 {
14511 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14512 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14513 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14514 SECT_OFF_TEXT (objfile));
14515 int low_set = 0;
14516 CORE_ADDR low = 0;
14517 CORE_ADDR high = 0;
14518 int retval;
14519
14520 retval = dwarf2_ranges_process (offset, cu,
14521 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14522 {
14523 if (ranges_pst != NULL)
14524 {
14525 CORE_ADDR lowpc;
14526 CORE_ADDR highpc;
14527
14528 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14529 range_beginning + baseaddr);
14530 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14531 range_end + baseaddr);
14532 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
14533 ranges_pst);
14534 }
14535
14536 /* FIXME: This is recording everything as a low-high
14537 segment of consecutive addresses. We should have a
14538 data structure for discontiguous block ranges
14539 instead. */
14540 if (! low_set)
14541 {
14542 low = range_beginning;
14543 high = range_end;
14544 low_set = 1;
14545 }
14546 else
14547 {
14548 if (range_beginning < low)
14549 low = range_beginning;
14550 if (range_end > high)
14551 high = range_end;
14552 }
14553 });
14554 if (!retval)
14555 return 0;
14556
14557 if (! low_set)
14558 /* If the first entry is an end-of-list marker, the range
14559 describes an empty scope, i.e. no instructions. */
14560 return 0;
14561
14562 if (low_return)
14563 *low_return = low;
14564 if (high_return)
14565 *high_return = high;
14566 return 1;
14567 }
14568
14569 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14570 definition for the return value. *LOWPC and *HIGHPC are set iff
14571 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14572
14573 static enum pc_bounds_kind
14574 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14575 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14576 struct partial_symtab *pst)
14577 {
14578 struct dwarf2_per_objfile *dwarf2_per_objfile
14579 = cu->per_cu->dwarf2_per_objfile;
14580 struct attribute *attr;
14581 struct attribute *attr_high;
14582 CORE_ADDR low = 0;
14583 CORE_ADDR high = 0;
14584 enum pc_bounds_kind ret;
14585
14586 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14587 if (attr_high)
14588 {
14589 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14590 if (attr)
14591 {
14592 low = attr_value_as_address (attr);
14593 high = attr_value_as_address (attr_high);
14594 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14595 high += low;
14596 }
14597 else
14598 /* Found high w/o low attribute. */
14599 return PC_BOUNDS_INVALID;
14600
14601 /* Found consecutive range of addresses. */
14602 ret = PC_BOUNDS_HIGH_LOW;
14603 }
14604 else
14605 {
14606 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14607 if (attr != NULL)
14608 {
14609 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14610 We take advantage of the fact that DW_AT_ranges does not appear
14611 in DW_TAG_compile_unit of DWO files. */
14612 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14613 unsigned int ranges_offset = (DW_UNSND (attr)
14614 + (need_ranges_base
14615 ? cu->ranges_base
14616 : 0));
14617
14618 /* Value of the DW_AT_ranges attribute is the offset in the
14619 .debug_ranges section. */
14620 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14621 return PC_BOUNDS_INVALID;
14622 /* Found discontinuous range of addresses. */
14623 ret = PC_BOUNDS_RANGES;
14624 }
14625 else
14626 return PC_BOUNDS_NOT_PRESENT;
14627 }
14628
14629 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14630 if (high <= low)
14631 return PC_BOUNDS_INVALID;
14632
14633 /* When using the GNU linker, .gnu.linkonce. sections are used to
14634 eliminate duplicate copies of functions and vtables and such.
14635 The linker will arbitrarily choose one and discard the others.
14636 The AT_*_pc values for such functions refer to local labels in
14637 these sections. If the section from that file was discarded, the
14638 labels are not in the output, so the relocs get a value of 0.
14639 If this is a discarded function, mark the pc bounds as invalid,
14640 so that GDB will ignore it. */
14641 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
14642 return PC_BOUNDS_INVALID;
14643
14644 *lowpc = low;
14645 if (highpc)
14646 *highpc = high;
14647 return ret;
14648 }
14649
14650 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14651 its low and high PC addresses. Do nothing if these addresses could not
14652 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14653 and HIGHPC to the high address if greater than HIGHPC. */
14654
14655 static void
14656 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14657 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14658 struct dwarf2_cu *cu)
14659 {
14660 CORE_ADDR low, high;
14661 struct die_info *child = die->child;
14662
14663 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14664 {
14665 *lowpc = std::min (*lowpc, low);
14666 *highpc = std::max (*highpc, high);
14667 }
14668
14669 /* If the language does not allow nested subprograms (either inside
14670 subprograms or lexical blocks), we're done. */
14671 if (cu->language != language_ada)
14672 return;
14673
14674 /* Check all the children of the given DIE. If it contains nested
14675 subprograms, then check their pc bounds. Likewise, we need to
14676 check lexical blocks as well, as they may also contain subprogram
14677 definitions. */
14678 while (child && child->tag)
14679 {
14680 if (child->tag == DW_TAG_subprogram
14681 || child->tag == DW_TAG_lexical_block)
14682 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14683 child = sibling_die (child);
14684 }
14685 }
14686
14687 /* Get the low and high pc's represented by the scope DIE, and store
14688 them in *LOWPC and *HIGHPC. If the correct values can't be
14689 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14690
14691 static void
14692 get_scope_pc_bounds (struct die_info *die,
14693 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14694 struct dwarf2_cu *cu)
14695 {
14696 CORE_ADDR best_low = (CORE_ADDR) -1;
14697 CORE_ADDR best_high = (CORE_ADDR) 0;
14698 CORE_ADDR current_low, current_high;
14699
14700 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14701 >= PC_BOUNDS_RANGES)
14702 {
14703 best_low = current_low;
14704 best_high = current_high;
14705 }
14706 else
14707 {
14708 struct die_info *child = die->child;
14709
14710 while (child && child->tag)
14711 {
14712 switch (child->tag) {
14713 case DW_TAG_subprogram:
14714 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14715 break;
14716 case DW_TAG_namespace:
14717 case DW_TAG_module:
14718 /* FIXME: carlton/2004-01-16: Should we do this for
14719 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14720 that current GCC's always emit the DIEs corresponding
14721 to definitions of methods of classes as children of a
14722 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14723 the DIEs giving the declarations, which could be
14724 anywhere). But I don't see any reason why the
14725 standards says that they have to be there. */
14726 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14727
14728 if (current_low != ((CORE_ADDR) -1))
14729 {
14730 best_low = std::min (best_low, current_low);
14731 best_high = std::max (best_high, current_high);
14732 }
14733 break;
14734 default:
14735 /* Ignore. */
14736 break;
14737 }
14738
14739 child = sibling_die (child);
14740 }
14741 }
14742
14743 *lowpc = best_low;
14744 *highpc = best_high;
14745 }
14746
14747 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14748 in DIE. */
14749
14750 static void
14751 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14752 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14753 {
14754 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14755 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14756 struct attribute *attr;
14757 struct attribute *attr_high;
14758
14759 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14760 if (attr_high)
14761 {
14762 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14763 if (attr)
14764 {
14765 CORE_ADDR low = attr_value_as_address (attr);
14766 CORE_ADDR high = attr_value_as_address (attr_high);
14767
14768 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14769 high += low;
14770
14771 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14772 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14773 record_block_range (block, low, high - 1);
14774 }
14775 }
14776
14777 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14778 if (attr)
14779 {
14780 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14781 We take advantage of the fact that DW_AT_ranges does not appear
14782 in DW_TAG_compile_unit of DWO files. */
14783 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14784
14785 /* The value of the DW_AT_ranges attribute is the offset of the
14786 address range list in the .debug_ranges section. */
14787 unsigned long offset = (DW_UNSND (attr)
14788 + (need_ranges_base ? cu->ranges_base : 0));
14789
14790 dwarf2_ranges_process (offset, cu,
14791 [&] (CORE_ADDR start, CORE_ADDR end)
14792 {
14793 start += baseaddr;
14794 end += baseaddr;
14795 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14796 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14797 record_block_range (block, start, end - 1);
14798 });
14799 }
14800 }
14801
14802 /* Check whether the producer field indicates either of GCC < 4.6, or the
14803 Intel C/C++ compiler, and cache the result in CU. */
14804
14805 static void
14806 check_producer (struct dwarf2_cu *cu)
14807 {
14808 int major, minor;
14809
14810 if (cu->producer == NULL)
14811 {
14812 /* For unknown compilers expect their behavior is DWARF version
14813 compliant.
14814
14815 GCC started to support .debug_types sections by -gdwarf-4 since
14816 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14817 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14818 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14819 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14820 }
14821 else if (producer_is_gcc (cu->producer, &major, &minor))
14822 {
14823 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14824 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14825 }
14826 else if (producer_is_icc (cu->producer, &major, &minor))
14827 cu->producer_is_icc_lt_14 = major < 14;
14828 else
14829 {
14830 /* For other non-GCC compilers, expect their behavior is DWARF version
14831 compliant. */
14832 }
14833
14834 cu->checked_producer = 1;
14835 }
14836
14837 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14838 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14839 during 4.6.0 experimental. */
14840
14841 static int
14842 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14843 {
14844 if (!cu->checked_producer)
14845 check_producer (cu);
14846
14847 return cu->producer_is_gxx_lt_4_6;
14848 }
14849
14850 /* Return the default accessibility type if it is not overriden by
14851 DW_AT_accessibility. */
14852
14853 static enum dwarf_access_attribute
14854 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14855 {
14856 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14857 {
14858 /* The default DWARF 2 accessibility for members is public, the default
14859 accessibility for inheritance is private. */
14860
14861 if (die->tag != DW_TAG_inheritance)
14862 return DW_ACCESS_public;
14863 else
14864 return DW_ACCESS_private;
14865 }
14866 else
14867 {
14868 /* DWARF 3+ defines the default accessibility a different way. The same
14869 rules apply now for DW_TAG_inheritance as for the members and it only
14870 depends on the container kind. */
14871
14872 if (die->parent->tag == DW_TAG_class_type)
14873 return DW_ACCESS_private;
14874 else
14875 return DW_ACCESS_public;
14876 }
14877 }
14878
14879 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14880 offset. If the attribute was not found return 0, otherwise return
14881 1. If it was found but could not properly be handled, set *OFFSET
14882 to 0. */
14883
14884 static int
14885 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14886 LONGEST *offset)
14887 {
14888 struct attribute *attr;
14889
14890 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14891 if (attr != NULL)
14892 {
14893 *offset = 0;
14894
14895 /* Note that we do not check for a section offset first here.
14896 This is because DW_AT_data_member_location is new in DWARF 4,
14897 so if we see it, we can assume that a constant form is really
14898 a constant and not a section offset. */
14899 if (attr_form_is_constant (attr))
14900 *offset = dwarf2_get_attr_constant_value (attr, 0);
14901 else if (attr_form_is_section_offset (attr))
14902 dwarf2_complex_location_expr_complaint ();
14903 else if (attr_form_is_block (attr))
14904 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14905 else
14906 dwarf2_complex_location_expr_complaint ();
14907
14908 return 1;
14909 }
14910
14911 return 0;
14912 }
14913
14914 /* Add an aggregate field to the field list. */
14915
14916 static void
14917 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14918 struct dwarf2_cu *cu)
14919 {
14920 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14921 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14922 struct nextfield *new_field;
14923 struct attribute *attr;
14924 struct field *fp;
14925 const char *fieldname = "";
14926
14927 if (die->tag == DW_TAG_inheritance)
14928 {
14929 fip->baseclasses.emplace_back ();
14930 new_field = &fip->baseclasses.back ();
14931 }
14932 else
14933 {
14934 fip->fields.emplace_back ();
14935 new_field = &fip->fields.back ();
14936 }
14937
14938 fip->nfields++;
14939
14940 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14941 if (attr)
14942 new_field->accessibility = DW_UNSND (attr);
14943 else
14944 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14945 if (new_field->accessibility != DW_ACCESS_public)
14946 fip->non_public_fields = 1;
14947
14948 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14949 if (attr)
14950 new_field->virtuality = DW_UNSND (attr);
14951 else
14952 new_field->virtuality = DW_VIRTUALITY_none;
14953
14954 fp = &new_field->field;
14955
14956 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14957 {
14958 LONGEST offset;
14959
14960 /* Data member other than a C++ static data member. */
14961
14962 /* Get type of field. */
14963 fp->type = die_type (die, cu);
14964
14965 SET_FIELD_BITPOS (*fp, 0);
14966
14967 /* Get bit size of field (zero if none). */
14968 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14969 if (attr)
14970 {
14971 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14972 }
14973 else
14974 {
14975 FIELD_BITSIZE (*fp) = 0;
14976 }
14977
14978 /* Get bit offset of field. */
14979 if (handle_data_member_location (die, cu, &offset))
14980 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14981 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14982 if (attr)
14983 {
14984 if (gdbarch_bits_big_endian (gdbarch))
14985 {
14986 /* For big endian bits, the DW_AT_bit_offset gives the
14987 additional bit offset from the MSB of the containing
14988 anonymous object to the MSB of the field. We don't
14989 have to do anything special since we don't need to
14990 know the size of the anonymous object. */
14991 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14992 }
14993 else
14994 {
14995 /* For little endian bits, compute the bit offset to the
14996 MSB of the anonymous object, subtract off the number of
14997 bits from the MSB of the field to the MSB of the
14998 object, and then subtract off the number of bits of
14999 the field itself. The result is the bit offset of
15000 the LSB of the field. */
15001 int anonymous_size;
15002 int bit_offset = DW_UNSND (attr);
15003
15004 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15005 if (attr)
15006 {
15007 /* The size of the anonymous object containing
15008 the bit field is explicit, so use the
15009 indicated size (in bytes). */
15010 anonymous_size = DW_UNSND (attr);
15011 }
15012 else
15013 {
15014 /* The size of the anonymous object containing
15015 the bit field must be inferred from the type
15016 attribute of the data member containing the
15017 bit field. */
15018 anonymous_size = TYPE_LENGTH (fp->type);
15019 }
15020 SET_FIELD_BITPOS (*fp,
15021 (FIELD_BITPOS (*fp)
15022 + anonymous_size * bits_per_byte
15023 - bit_offset - FIELD_BITSIZE (*fp)));
15024 }
15025 }
15026 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15027 if (attr != NULL)
15028 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15029 + dwarf2_get_attr_constant_value (attr, 0)));
15030
15031 /* Get name of field. */
15032 fieldname = dwarf2_name (die, cu);
15033 if (fieldname == NULL)
15034 fieldname = "";
15035
15036 /* The name is already allocated along with this objfile, so we don't
15037 need to duplicate it for the type. */
15038 fp->name = fieldname;
15039
15040 /* Change accessibility for artificial fields (e.g. virtual table
15041 pointer or virtual base class pointer) to private. */
15042 if (dwarf2_attr (die, DW_AT_artificial, cu))
15043 {
15044 FIELD_ARTIFICIAL (*fp) = 1;
15045 new_field->accessibility = DW_ACCESS_private;
15046 fip->non_public_fields = 1;
15047 }
15048 }
15049 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15050 {
15051 /* C++ static member. */
15052
15053 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15054 is a declaration, but all versions of G++ as of this writing
15055 (so through at least 3.2.1) incorrectly generate
15056 DW_TAG_variable tags. */
15057
15058 const char *physname;
15059
15060 /* Get name of field. */
15061 fieldname = dwarf2_name (die, cu);
15062 if (fieldname == NULL)
15063 return;
15064
15065 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15066 if (attr
15067 /* Only create a symbol if this is an external value.
15068 new_symbol checks this and puts the value in the global symbol
15069 table, which we want. If it is not external, new_symbol
15070 will try to put the value in cu->list_in_scope which is wrong. */
15071 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15072 {
15073 /* A static const member, not much different than an enum as far as
15074 we're concerned, except that we can support more types. */
15075 new_symbol (die, NULL, cu);
15076 }
15077
15078 /* Get physical name. */
15079 physname = dwarf2_physname (fieldname, die, cu);
15080
15081 /* The name is already allocated along with this objfile, so we don't
15082 need to duplicate it for the type. */
15083 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15084 FIELD_TYPE (*fp) = die_type (die, cu);
15085 FIELD_NAME (*fp) = fieldname;
15086 }
15087 else if (die->tag == DW_TAG_inheritance)
15088 {
15089 LONGEST offset;
15090
15091 /* C++ base class field. */
15092 if (handle_data_member_location (die, cu, &offset))
15093 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15094 FIELD_BITSIZE (*fp) = 0;
15095 FIELD_TYPE (*fp) = die_type (die, cu);
15096 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
15097 }
15098 else if (die->tag == DW_TAG_variant_part)
15099 {
15100 /* process_structure_scope will treat this DIE as a union. */
15101 process_structure_scope (die, cu);
15102
15103 /* The variant part is relative to the start of the enclosing
15104 structure. */
15105 SET_FIELD_BITPOS (*fp, 0);
15106 fp->type = get_die_type (die, cu);
15107 fp->artificial = 1;
15108 fp->name = "<<variant>>";
15109 }
15110 else
15111 gdb_assert_not_reached ("missing case in dwarf2_add_field");
15112 }
15113
15114 /* Can the type given by DIE define another type? */
15115
15116 static bool
15117 type_can_define_types (const struct die_info *die)
15118 {
15119 switch (die->tag)
15120 {
15121 case DW_TAG_typedef:
15122 case DW_TAG_class_type:
15123 case DW_TAG_structure_type:
15124 case DW_TAG_union_type:
15125 case DW_TAG_enumeration_type:
15126 return true;
15127
15128 default:
15129 return false;
15130 }
15131 }
15132
15133 /* Add a type definition defined in the scope of the FIP's class. */
15134
15135 static void
15136 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15137 struct dwarf2_cu *cu)
15138 {
15139 struct decl_field fp;
15140 memset (&fp, 0, sizeof (fp));
15141
15142 gdb_assert (type_can_define_types (die));
15143
15144 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15145 fp.name = dwarf2_name (die, cu);
15146 fp.type = read_type_die (die, cu);
15147
15148 /* Save accessibility. */
15149 enum dwarf_access_attribute accessibility;
15150 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15151 if (attr != NULL)
15152 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15153 else
15154 accessibility = dwarf2_default_access_attribute (die, cu);
15155 switch (accessibility)
15156 {
15157 case DW_ACCESS_public:
15158 /* The assumed value if neither private nor protected. */
15159 break;
15160 case DW_ACCESS_private:
15161 fp.is_private = 1;
15162 break;
15163 case DW_ACCESS_protected:
15164 fp.is_protected = 1;
15165 break;
15166 default:
15167 complaint (&symfile_complaints,
15168 _("Unhandled DW_AT_accessibility value (%x)"), accessibility);
15169 }
15170
15171 if (die->tag == DW_TAG_typedef)
15172 fip->typedef_field_list.push_back (fp);
15173 else
15174 fip->nested_types_list.push_back (fp);
15175 }
15176
15177 /* Create the vector of fields, and attach it to the type. */
15178
15179 static void
15180 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15181 struct dwarf2_cu *cu)
15182 {
15183 int nfields = fip->nfields;
15184
15185 /* Record the field count, allocate space for the array of fields,
15186 and create blank accessibility bitfields if necessary. */
15187 TYPE_NFIELDS (type) = nfields;
15188 TYPE_FIELDS (type) = (struct field *)
15189 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
15190
15191 if (fip->non_public_fields && cu->language != language_ada)
15192 {
15193 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15194
15195 TYPE_FIELD_PRIVATE_BITS (type) =
15196 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15197 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15198
15199 TYPE_FIELD_PROTECTED_BITS (type) =
15200 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15201 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15202
15203 TYPE_FIELD_IGNORE_BITS (type) =
15204 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15205 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15206 }
15207
15208 /* If the type has baseclasses, allocate and clear a bit vector for
15209 TYPE_FIELD_VIRTUAL_BITS. */
15210 if (!fip->baseclasses.empty () && cu->language != language_ada)
15211 {
15212 int num_bytes = B_BYTES (fip->baseclasses.size ());
15213 unsigned char *pointer;
15214
15215 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15216 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15217 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15218 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15219 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15220 }
15221
15222 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15223 {
15224 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15225
15226 for (int index = 0; index < nfields; ++index)
15227 {
15228 struct nextfield &field = fip->fields[index];
15229
15230 if (field.variant.is_discriminant)
15231 di->discriminant_index = index;
15232 else if (field.variant.default_branch)
15233 di->default_index = index;
15234 else
15235 di->discriminants[index] = field.variant.discriminant_value;
15236 }
15237 }
15238
15239 /* Copy the saved-up fields into the field vector. */
15240 for (int i = 0; i < nfields; ++i)
15241 {
15242 struct nextfield &field
15243 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15244 : fip->fields[i - fip->baseclasses.size ()]);
15245
15246 TYPE_FIELD (type, i) = field.field;
15247 switch (field.accessibility)
15248 {
15249 case DW_ACCESS_private:
15250 if (cu->language != language_ada)
15251 SET_TYPE_FIELD_PRIVATE (type, i);
15252 break;
15253
15254 case DW_ACCESS_protected:
15255 if (cu->language != language_ada)
15256 SET_TYPE_FIELD_PROTECTED (type, i);
15257 break;
15258
15259 case DW_ACCESS_public:
15260 break;
15261
15262 default:
15263 /* Unknown accessibility. Complain and treat it as public. */
15264 {
15265 complaint (&symfile_complaints, _("unsupported accessibility %d"),
15266 field.accessibility);
15267 }
15268 break;
15269 }
15270 if (i < fip->baseclasses.size ())
15271 {
15272 switch (field.virtuality)
15273 {
15274 case DW_VIRTUALITY_virtual:
15275 case DW_VIRTUALITY_pure_virtual:
15276 if (cu->language == language_ada)
15277 error (_("unexpected virtuality in component of Ada type"));
15278 SET_TYPE_FIELD_VIRTUAL (type, i);
15279 break;
15280 }
15281 }
15282 }
15283 }
15284
15285 /* Return true if this member function is a constructor, false
15286 otherwise. */
15287
15288 static int
15289 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15290 {
15291 const char *fieldname;
15292 const char *type_name;
15293 int len;
15294
15295 if (die->parent == NULL)
15296 return 0;
15297
15298 if (die->parent->tag != DW_TAG_structure_type
15299 && die->parent->tag != DW_TAG_union_type
15300 && die->parent->tag != DW_TAG_class_type)
15301 return 0;
15302
15303 fieldname = dwarf2_name (die, cu);
15304 type_name = dwarf2_name (die->parent, cu);
15305 if (fieldname == NULL || type_name == NULL)
15306 return 0;
15307
15308 len = strlen (fieldname);
15309 return (strncmp (fieldname, type_name, len) == 0
15310 && (type_name[len] == '\0' || type_name[len] == '<'));
15311 }
15312
15313 /* Add a member function to the proper fieldlist. */
15314
15315 static void
15316 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15317 struct type *type, struct dwarf2_cu *cu)
15318 {
15319 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15320 struct attribute *attr;
15321 int i;
15322 struct fnfieldlist *flp = nullptr;
15323 struct fn_field *fnp;
15324 const char *fieldname;
15325 struct type *this_type;
15326 enum dwarf_access_attribute accessibility;
15327
15328 if (cu->language == language_ada)
15329 error (_("unexpected member function in Ada type"));
15330
15331 /* Get name of member function. */
15332 fieldname = dwarf2_name (die, cu);
15333 if (fieldname == NULL)
15334 return;
15335
15336 /* Look up member function name in fieldlist. */
15337 for (i = 0; i < fip->fnfieldlists.size (); i++)
15338 {
15339 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15340 {
15341 flp = &fip->fnfieldlists[i];
15342 break;
15343 }
15344 }
15345
15346 /* Create a new fnfieldlist if necessary. */
15347 if (flp == nullptr)
15348 {
15349 fip->fnfieldlists.emplace_back ();
15350 flp = &fip->fnfieldlists.back ();
15351 flp->name = fieldname;
15352 i = fip->fnfieldlists.size () - 1;
15353 }
15354
15355 /* Create a new member function field and add it to the vector of
15356 fnfieldlists. */
15357 flp->fnfields.emplace_back ();
15358 fnp = &flp->fnfields.back ();
15359
15360 /* Delay processing of the physname until later. */
15361 if (cu->language == language_cplus)
15362 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15363 die, cu);
15364 else
15365 {
15366 const char *physname = dwarf2_physname (fieldname, die, cu);
15367 fnp->physname = physname ? physname : "";
15368 }
15369
15370 fnp->type = alloc_type (objfile);
15371 this_type = read_type_die (die, cu);
15372 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
15373 {
15374 int nparams = TYPE_NFIELDS (this_type);
15375
15376 /* TYPE is the domain of this method, and THIS_TYPE is the type
15377 of the method itself (TYPE_CODE_METHOD). */
15378 smash_to_method_type (fnp->type, type,
15379 TYPE_TARGET_TYPE (this_type),
15380 TYPE_FIELDS (this_type),
15381 TYPE_NFIELDS (this_type),
15382 TYPE_VARARGS (this_type));
15383
15384 /* Handle static member functions.
15385 Dwarf2 has no clean way to discern C++ static and non-static
15386 member functions. G++ helps GDB by marking the first
15387 parameter for non-static member functions (which is the this
15388 pointer) as artificial. We obtain this information from
15389 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15390 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15391 fnp->voffset = VOFFSET_STATIC;
15392 }
15393 else
15394 complaint (&symfile_complaints, _("member function type missing for '%s'"),
15395 dwarf2_full_name (fieldname, die, cu));
15396
15397 /* Get fcontext from DW_AT_containing_type if present. */
15398 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15399 fnp->fcontext = die_containing_type (die, cu);
15400
15401 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15402 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15403
15404 /* Get accessibility. */
15405 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15406 if (attr)
15407 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15408 else
15409 accessibility = dwarf2_default_access_attribute (die, cu);
15410 switch (accessibility)
15411 {
15412 case DW_ACCESS_private:
15413 fnp->is_private = 1;
15414 break;
15415 case DW_ACCESS_protected:
15416 fnp->is_protected = 1;
15417 break;
15418 }
15419
15420 /* Check for artificial methods. */
15421 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15422 if (attr && DW_UNSND (attr) != 0)
15423 fnp->is_artificial = 1;
15424
15425 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15426
15427 /* Get index in virtual function table if it is a virtual member
15428 function. For older versions of GCC, this is an offset in the
15429 appropriate virtual table, as specified by DW_AT_containing_type.
15430 For everyone else, it is an expression to be evaluated relative
15431 to the object address. */
15432
15433 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15434 if (attr)
15435 {
15436 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
15437 {
15438 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15439 {
15440 /* Old-style GCC. */
15441 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15442 }
15443 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15444 || (DW_BLOCK (attr)->size > 1
15445 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15446 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15447 {
15448 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15449 if ((fnp->voffset % cu->header.addr_size) != 0)
15450 dwarf2_complex_location_expr_complaint ();
15451 else
15452 fnp->voffset /= cu->header.addr_size;
15453 fnp->voffset += 2;
15454 }
15455 else
15456 dwarf2_complex_location_expr_complaint ();
15457
15458 if (!fnp->fcontext)
15459 {
15460 /* If there is no `this' field and no DW_AT_containing_type,
15461 we cannot actually find a base class context for the
15462 vtable! */
15463 if (TYPE_NFIELDS (this_type) == 0
15464 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15465 {
15466 complaint (&symfile_complaints,
15467 _("cannot determine context for virtual member "
15468 "function \"%s\" (offset %s)"),
15469 fieldname, sect_offset_str (die->sect_off));
15470 }
15471 else
15472 {
15473 fnp->fcontext
15474 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15475 }
15476 }
15477 }
15478 else if (attr_form_is_section_offset (attr))
15479 {
15480 dwarf2_complex_location_expr_complaint ();
15481 }
15482 else
15483 {
15484 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15485 fieldname);
15486 }
15487 }
15488 else
15489 {
15490 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15491 if (attr && DW_UNSND (attr))
15492 {
15493 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15494 complaint (&symfile_complaints,
15495 _("Member function \"%s\" (offset %s) is virtual "
15496 "but the vtable offset is not specified"),
15497 fieldname, sect_offset_str (die->sect_off));
15498 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15499 TYPE_CPLUS_DYNAMIC (type) = 1;
15500 }
15501 }
15502 }
15503
15504 /* Create the vector of member function fields, and attach it to the type. */
15505
15506 static void
15507 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15508 struct dwarf2_cu *cu)
15509 {
15510 if (cu->language == language_ada)
15511 error (_("unexpected member functions in Ada type"));
15512
15513 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15514 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15515 TYPE_ALLOC (type,
15516 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15517
15518 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15519 {
15520 struct fnfieldlist &nf = fip->fnfieldlists[i];
15521 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15522
15523 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15524 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15525 fn_flp->fn_fields = (struct fn_field *)
15526 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15527
15528 for (int k = 0; k < nf.fnfields.size (); ++k)
15529 fn_flp->fn_fields[k] = nf.fnfields[k];
15530 }
15531
15532 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15533 }
15534
15535 /* Returns non-zero if NAME is the name of a vtable member in CU's
15536 language, zero otherwise. */
15537 static int
15538 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15539 {
15540 static const char vptr[] = "_vptr";
15541
15542 /* Look for the C++ form of the vtable. */
15543 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15544 return 1;
15545
15546 return 0;
15547 }
15548
15549 /* GCC outputs unnamed structures that are really pointers to member
15550 functions, with the ABI-specified layout. If TYPE describes
15551 such a structure, smash it into a member function type.
15552
15553 GCC shouldn't do this; it should just output pointer to member DIEs.
15554 This is GCC PR debug/28767. */
15555
15556 static void
15557 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15558 {
15559 struct type *pfn_type, *self_type, *new_type;
15560
15561 /* Check for a structure with no name and two children. */
15562 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15563 return;
15564
15565 /* Check for __pfn and __delta members. */
15566 if (TYPE_FIELD_NAME (type, 0) == NULL
15567 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15568 || TYPE_FIELD_NAME (type, 1) == NULL
15569 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15570 return;
15571
15572 /* Find the type of the method. */
15573 pfn_type = TYPE_FIELD_TYPE (type, 0);
15574 if (pfn_type == NULL
15575 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15576 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
15577 return;
15578
15579 /* Look for the "this" argument. */
15580 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15581 if (TYPE_NFIELDS (pfn_type) == 0
15582 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15583 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
15584 return;
15585
15586 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15587 new_type = alloc_type (objfile);
15588 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15589 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15590 TYPE_VARARGS (pfn_type));
15591 smash_to_methodptr_type (type, new_type);
15592 }
15593
15594 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15595 appropriate error checking and issuing complaints if there is a
15596 problem. */
15597
15598 static ULONGEST
15599 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15600 {
15601 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15602
15603 if (attr == nullptr)
15604 return 0;
15605
15606 if (!attr_form_is_constant (attr))
15607 {
15608 complaint (&symfile_complaints,
15609 _("DW_AT_alignment must have constant form"
15610 " - DIE at %s [in module %s]"),
15611 sect_offset_str (die->sect_off),
15612 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15613 return 0;
15614 }
15615
15616 ULONGEST align;
15617 if (attr->form == DW_FORM_sdata)
15618 {
15619 LONGEST val = DW_SND (attr);
15620 if (val < 0)
15621 {
15622 complaint (&symfile_complaints,
15623 _("DW_AT_alignment value must not be negative"
15624 " - DIE at %s [in module %s]"),
15625 sect_offset_str (die->sect_off),
15626 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15627 return 0;
15628 }
15629 align = val;
15630 }
15631 else
15632 align = DW_UNSND (attr);
15633
15634 if (align == 0)
15635 {
15636 complaint (&symfile_complaints,
15637 _("DW_AT_alignment value must not be zero"
15638 " - DIE at %s [in module %s]"),
15639 sect_offset_str (die->sect_off),
15640 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15641 return 0;
15642 }
15643 if ((align & (align - 1)) != 0)
15644 {
15645 complaint (&symfile_complaints,
15646 _("DW_AT_alignment value must be a power of 2"
15647 " - DIE at %s [in module %s]"),
15648 sect_offset_str (die->sect_off),
15649 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15650 return 0;
15651 }
15652
15653 return align;
15654 }
15655
15656 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15657 the alignment for TYPE. */
15658
15659 static void
15660 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15661 struct type *type)
15662 {
15663 if (!set_type_align (type, get_alignment (cu, die)))
15664 complaint (&symfile_complaints,
15665 _("DW_AT_alignment value too large"
15666 " - DIE at %s [in module %s]"),
15667 sect_offset_str (die->sect_off),
15668 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15669 }
15670
15671 /* Called when we find the DIE that starts a structure or union scope
15672 (definition) to create a type for the structure or union. Fill in
15673 the type's name and general properties; the members will not be
15674 processed until process_structure_scope. A symbol table entry for
15675 the type will also not be done until process_structure_scope (assuming
15676 the type has a name).
15677
15678 NOTE: we need to call these functions regardless of whether or not the
15679 DIE has a DW_AT_name attribute, since it might be an anonymous
15680 structure or union. This gets the type entered into our set of
15681 user defined types. */
15682
15683 static struct type *
15684 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15685 {
15686 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15687 struct type *type;
15688 struct attribute *attr;
15689 const char *name;
15690
15691 /* If the definition of this type lives in .debug_types, read that type.
15692 Don't follow DW_AT_specification though, that will take us back up
15693 the chain and we want to go down. */
15694 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15695 if (attr)
15696 {
15697 type = get_DW_AT_signature_type (die, attr, cu);
15698
15699 /* The type's CU may not be the same as CU.
15700 Ensure TYPE is recorded with CU in die_type_hash. */
15701 return set_die_type (die, type, cu);
15702 }
15703
15704 type = alloc_type (objfile);
15705 INIT_CPLUS_SPECIFIC (type);
15706
15707 name = dwarf2_name (die, cu);
15708 if (name != NULL)
15709 {
15710 if (cu->language == language_cplus
15711 || cu->language == language_d
15712 || cu->language == language_rust)
15713 {
15714 const char *full_name = dwarf2_full_name (name, die, cu);
15715
15716 /* dwarf2_full_name might have already finished building the DIE's
15717 type. If so, there is no need to continue. */
15718 if (get_die_type (die, cu) != NULL)
15719 return get_die_type (die, cu);
15720
15721 TYPE_TAG_NAME (type) = full_name;
15722 if (die->tag == DW_TAG_structure_type
15723 || die->tag == DW_TAG_class_type)
15724 TYPE_NAME (type) = TYPE_TAG_NAME (type);
15725 }
15726 else
15727 {
15728 /* The name is already allocated along with this objfile, so
15729 we don't need to duplicate it for the type. */
15730 TYPE_TAG_NAME (type) = name;
15731 if (die->tag == DW_TAG_class_type)
15732 TYPE_NAME (type) = TYPE_TAG_NAME (type);
15733 }
15734 }
15735
15736 if (die->tag == DW_TAG_structure_type)
15737 {
15738 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15739 }
15740 else if (die->tag == DW_TAG_union_type)
15741 {
15742 TYPE_CODE (type) = TYPE_CODE_UNION;
15743 }
15744 else if (die->tag == DW_TAG_variant_part)
15745 {
15746 TYPE_CODE (type) = TYPE_CODE_UNION;
15747 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15748 }
15749 else
15750 {
15751 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15752 }
15753
15754 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15755 TYPE_DECLARED_CLASS (type) = 1;
15756
15757 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15758 if (attr)
15759 {
15760 if (attr_form_is_constant (attr))
15761 TYPE_LENGTH (type) = DW_UNSND (attr);
15762 else
15763 {
15764 /* For the moment, dynamic type sizes are not supported
15765 by GDB's struct type. The actual size is determined
15766 on-demand when resolving the type of a given object,
15767 so set the type's length to zero for now. Otherwise,
15768 we record an expression as the length, and that expression
15769 could lead to a very large value, which could eventually
15770 lead to us trying to allocate that much memory when creating
15771 a value of that type. */
15772 TYPE_LENGTH (type) = 0;
15773 }
15774 }
15775 else
15776 {
15777 TYPE_LENGTH (type) = 0;
15778 }
15779
15780 maybe_set_alignment (cu, die, type);
15781
15782 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15783 {
15784 /* ICC<14 does not output the required DW_AT_declaration on
15785 incomplete types, but gives them a size of zero. */
15786 TYPE_STUB (type) = 1;
15787 }
15788 else
15789 TYPE_STUB_SUPPORTED (type) = 1;
15790
15791 if (die_is_declaration (die, cu))
15792 TYPE_STUB (type) = 1;
15793 else if (attr == NULL && die->child == NULL
15794 && producer_is_realview (cu->producer))
15795 /* RealView does not output the required DW_AT_declaration
15796 on incomplete types. */
15797 TYPE_STUB (type) = 1;
15798
15799 /* We need to add the type field to the die immediately so we don't
15800 infinitely recurse when dealing with pointers to the structure
15801 type within the structure itself. */
15802 set_die_type (die, type, cu);
15803
15804 /* set_die_type should be already done. */
15805 set_descriptive_type (type, die, cu);
15806
15807 return type;
15808 }
15809
15810 /* A helper for process_structure_scope that handles a single member
15811 DIE. */
15812
15813 static void
15814 handle_struct_member_die (struct die_info *child_die, struct type *type,
15815 struct field_info *fi,
15816 std::vector<struct symbol *> *template_args,
15817 struct dwarf2_cu *cu)
15818 {
15819 if (child_die->tag == DW_TAG_member
15820 || child_die->tag == DW_TAG_variable
15821 || child_die->tag == DW_TAG_variant_part)
15822 {
15823 /* NOTE: carlton/2002-11-05: A C++ static data member
15824 should be a DW_TAG_member that is a declaration, but
15825 all versions of G++ as of this writing (so through at
15826 least 3.2.1) incorrectly generate DW_TAG_variable
15827 tags for them instead. */
15828 dwarf2_add_field (fi, child_die, cu);
15829 }
15830 else if (child_die->tag == DW_TAG_subprogram)
15831 {
15832 /* Rust doesn't have member functions in the C++ sense.
15833 However, it does emit ordinary functions as children
15834 of a struct DIE. */
15835 if (cu->language == language_rust)
15836 read_func_scope (child_die, cu);
15837 else
15838 {
15839 /* C++ member function. */
15840 dwarf2_add_member_fn (fi, child_die, type, cu);
15841 }
15842 }
15843 else if (child_die->tag == DW_TAG_inheritance)
15844 {
15845 /* C++ base class field. */
15846 dwarf2_add_field (fi, child_die, cu);
15847 }
15848 else if (type_can_define_types (child_die))
15849 dwarf2_add_type_defn (fi, child_die, cu);
15850 else if (child_die->tag == DW_TAG_template_type_param
15851 || child_die->tag == DW_TAG_template_value_param)
15852 {
15853 struct symbol *arg = new_symbol (child_die, NULL, cu);
15854
15855 if (arg != NULL)
15856 template_args->push_back (arg);
15857 }
15858 else if (child_die->tag == DW_TAG_variant)
15859 {
15860 /* In a variant we want to get the discriminant and also add a
15861 field for our sole member child. */
15862 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15863
15864 for (struct die_info *variant_child = child_die->child;
15865 variant_child != NULL;
15866 variant_child = sibling_die (variant_child))
15867 {
15868 if (variant_child->tag == DW_TAG_member)
15869 {
15870 handle_struct_member_die (variant_child, type, fi,
15871 template_args, cu);
15872 /* Only handle the one. */
15873 break;
15874 }
15875 }
15876
15877 /* We don't handle this but we might as well report it if we see
15878 it. */
15879 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
15880 complaint (&symfile_complaints,
15881 _("DW_AT_discr_list is not supported yet"
15882 " - DIE at %s [in module %s]"),
15883 sect_offset_str (child_die->sect_off),
15884 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15885
15886 /* The first field was just added, so we can stash the
15887 discriminant there. */
15888 gdb_assert (!fi->fields.empty ());
15889 if (discr == NULL)
15890 fi->fields.back ().variant.default_branch = true;
15891 else
15892 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
15893 }
15894 }
15895
15896 /* Finish creating a structure or union type, including filling in
15897 its members and creating a symbol for it. */
15898
15899 static void
15900 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15901 {
15902 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15903 struct die_info *child_die;
15904 struct type *type;
15905
15906 type = get_die_type (die, cu);
15907 if (type == NULL)
15908 type = read_structure_type (die, cu);
15909
15910 /* When reading a DW_TAG_variant_part, we need to notice when we
15911 read the discriminant member, so we can record it later in the
15912 discriminant_info. */
15913 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
15914 sect_offset discr_offset;
15915
15916 if (is_variant_part)
15917 {
15918 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15919 if (discr == NULL)
15920 {
15921 /* Maybe it's a univariant form, an extension we support.
15922 In this case arrange not to check the offset. */
15923 is_variant_part = false;
15924 }
15925 else if (attr_form_is_ref (discr))
15926 {
15927 struct dwarf2_cu *target_cu = cu;
15928 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15929
15930 discr_offset = target_die->sect_off;
15931 }
15932 else
15933 {
15934 complaint (&symfile_complaints,
15935 _("DW_AT_discr does not have DIE reference form"
15936 " - DIE at %s [in module %s]"),
15937 sect_offset_str (die->sect_off),
15938 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15939 is_variant_part = false;
15940 }
15941 }
15942
15943 if (die->child != NULL && ! die_is_declaration (die, cu))
15944 {
15945 struct field_info fi;
15946 std::vector<struct symbol *> template_args;
15947
15948 child_die = die->child;
15949
15950 while (child_die && child_die->tag)
15951 {
15952 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15953
15954 if (is_variant_part && discr_offset == child_die->sect_off)
15955 fi.fields.back ().variant.is_discriminant = true;
15956
15957 child_die = sibling_die (child_die);
15958 }
15959
15960 /* Attach template arguments to type. */
15961 if (!template_args.empty ())
15962 {
15963 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15964 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15965 TYPE_TEMPLATE_ARGUMENTS (type)
15966 = XOBNEWVEC (&objfile->objfile_obstack,
15967 struct symbol *,
15968 TYPE_N_TEMPLATE_ARGUMENTS (type));
15969 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15970 template_args.data (),
15971 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15972 * sizeof (struct symbol *)));
15973 }
15974
15975 /* Attach fields and member functions to the type. */
15976 if (fi.nfields)
15977 dwarf2_attach_fields_to_type (&fi, type, cu);
15978 if (!fi.fnfieldlists.empty ())
15979 {
15980 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15981
15982 /* Get the type which refers to the base class (possibly this
15983 class itself) which contains the vtable pointer for the current
15984 class from the DW_AT_containing_type attribute. This use of
15985 DW_AT_containing_type is a GNU extension. */
15986
15987 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15988 {
15989 struct type *t = die_containing_type (die, cu);
15990
15991 set_type_vptr_basetype (type, t);
15992 if (type == t)
15993 {
15994 int i;
15995
15996 /* Our own class provides vtbl ptr. */
15997 for (i = TYPE_NFIELDS (t) - 1;
15998 i >= TYPE_N_BASECLASSES (t);
15999 --i)
16000 {
16001 const char *fieldname = TYPE_FIELD_NAME (t, i);
16002
16003 if (is_vtable_name (fieldname, cu))
16004 {
16005 set_type_vptr_fieldno (type, i);
16006 break;
16007 }
16008 }
16009
16010 /* Complain if virtual function table field not found. */
16011 if (i < TYPE_N_BASECLASSES (t))
16012 complaint (&symfile_complaints,
16013 _("virtual function table pointer "
16014 "not found when defining class '%s'"),
16015 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
16016 "");
16017 }
16018 else
16019 {
16020 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16021 }
16022 }
16023 else if (cu->producer
16024 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16025 {
16026 /* The IBM XLC compiler does not provide direct indication
16027 of the containing type, but the vtable pointer is
16028 always named __vfp. */
16029
16030 int i;
16031
16032 for (i = TYPE_NFIELDS (type) - 1;
16033 i >= TYPE_N_BASECLASSES (type);
16034 --i)
16035 {
16036 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16037 {
16038 set_type_vptr_fieldno (type, i);
16039 set_type_vptr_basetype (type, type);
16040 break;
16041 }
16042 }
16043 }
16044 }
16045
16046 /* Copy fi.typedef_field_list linked list elements content into the
16047 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16048 if (!fi.typedef_field_list.empty ())
16049 {
16050 int count = fi.typedef_field_list.size ();
16051
16052 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16053 TYPE_TYPEDEF_FIELD_ARRAY (type)
16054 = ((struct decl_field *)
16055 TYPE_ALLOC (type,
16056 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16057 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16058
16059 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16060 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16061 }
16062
16063 /* Copy fi.nested_types_list linked list elements content into the
16064 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16065 if (!fi.nested_types_list.empty () && cu->language != language_ada)
16066 {
16067 int count = fi.nested_types_list.size ();
16068
16069 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16070 TYPE_NESTED_TYPES_ARRAY (type)
16071 = ((struct decl_field *)
16072 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16073 TYPE_NESTED_TYPES_COUNT (type) = count;
16074
16075 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16076 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16077 }
16078 }
16079
16080 quirk_gcc_member_function_pointer (type, objfile);
16081 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16082 cu->rust_unions.push_back (type);
16083
16084 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16085 snapshots) has been known to create a die giving a declaration
16086 for a class that has, as a child, a die giving a definition for a
16087 nested class. So we have to process our children even if the
16088 current die is a declaration. Normally, of course, a declaration
16089 won't have any children at all. */
16090
16091 child_die = die->child;
16092
16093 while (child_die != NULL && child_die->tag)
16094 {
16095 if (child_die->tag == DW_TAG_member
16096 || child_die->tag == DW_TAG_variable
16097 || child_die->tag == DW_TAG_inheritance
16098 || child_die->tag == DW_TAG_template_value_param
16099 || child_die->tag == DW_TAG_template_type_param)
16100 {
16101 /* Do nothing. */
16102 }
16103 else
16104 process_die (child_die, cu);
16105
16106 child_die = sibling_die (child_die);
16107 }
16108
16109 /* Do not consider external references. According to the DWARF standard,
16110 these DIEs are identified by the fact that they have no byte_size
16111 attribute, and a declaration attribute. */
16112 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16113 || !die_is_declaration (die, cu))
16114 new_symbol (die, type, cu);
16115 }
16116
16117 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
16118 update TYPE using some information only available in DIE's children. */
16119
16120 static void
16121 update_enumeration_type_from_children (struct die_info *die,
16122 struct type *type,
16123 struct dwarf2_cu *cu)
16124 {
16125 struct die_info *child_die;
16126 int unsigned_enum = 1;
16127 int flag_enum = 1;
16128 ULONGEST mask = 0;
16129
16130 auto_obstack obstack;
16131
16132 for (child_die = die->child;
16133 child_die != NULL && child_die->tag;
16134 child_die = sibling_die (child_die))
16135 {
16136 struct attribute *attr;
16137 LONGEST value;
16138 const gdb_byte *bytes;
16139 struct dwarf2_locexpr_baton *baton;
16140 const char *name;
16141
16142 if (child_die->tag != DW_TAG_enumerator)
16143 continue;
16144
16145 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16146 if (attr == NULL)
16147 continue;
16148
16149 name = dwarf2_name (child_die, cu);
16150 if (name == NULL)
16151 name = "<anonymous enumerator>";
16152
16153 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16154 &value, &bytes, &baton);
16155 if (value < 0)
16156 {
16157 unsigned_enum = 0;
16158 flag_enum = 0;
16159 }
16160 else if ((mask & value) != 0)
16161 flag_enum = 0;
16162 else
16163 mask |= value;
16164
16165 /* If we already know that the enum type is neither unsigned, nor
16166 a flag type, no need to look at the rest of the enumerates. */
16167 if (!unsigned_enum && !flag_enum)
16168 break;
16169 }
16170
16171 if (unsigned_enum)
16172 TYPE_UNSIGNED (type) = 1;
16173 if (flag_enum)
16174 TYPE_FLAG_ENUM (type) = 1;
16175 }
16176
16177 /* Given a DW_AT_enumeration_type die, set its type. We do not
16178 complete the type's fields yet, or create any symbols. */
16179
16180 static struct type *
16181 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16182 {
16183 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16184 struct type *type;
16185 struct attribute *attr;
16186 const char *name;
16187
16188 /* If the definition of this type lives in .debug_types, read that type.
16189 Don't follow DW_AT_specification though, that will take us back up
16190 the chain and we want to go down. */
16191 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
16192 if (attr)
16193 {
16194 type = get_DW_AT_signature_type (die, attr, cu);
16195
16196 /* The type's CU may not be the same as CU.
16197 Ensure TYPE is recorded with CU in die_type_hash. */
16198 return set_die_type (die, type, cu);
16199 }
16200
16201 type = alloc_type (objfile);
16202
16203 TYPE_CODE (type) = TYPE_CODE_ENUM;
16204 name = dwarf2_full_name (NULL, die, cu);
16205 if (name != NULL)
16206 TYPE_TAG_NAME (type) = name;
16207
16208 attr = dwarf2_attr (die, DW_AT_type, cu);
16209 if (attr != NULL)
16210 {
16211 struct type *underlying_type = die_type (die, cu);
16212
16213 TYPE_TARGET_TYPE (type) = underlying_type;
16214 }
16215
16216 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16217 if (attr)
16218 {
16219 TYPE_LENGTH (type) = DW_UNSND (attr);
16220 }
16221 else
16222 {
16223 TYPE_LENGTH (type) = 0;
16224 }
16225
16226 maybe_set_alignment (cu, die, type);
16227
16228 /* The enumeration DIE can be incomplete. In Ada, any type can be
16229 declared as private in the package spec, and then defined only
16230 inside the package body. Such types are known as Taft Amendment
16231 Types. When another package uses such a type, an incomplete DIE
16232 may be generated by the compiler. */
16233 if (die_is_declaration (die, cu))
16234 TYPE_STUB (type) = 1;
16235
16236 /* Finish the creation of this type by using the enum's children.
16237 We must call this even when the underlying type has been provided
16238 so that we can determine if we're looking at a "flag" enum. */
16239 update_enumeration_type_from_children (die, type, cu);
16240
16241 /* If this type has an underlying type that is not a stub, then we
16242 may use its attributes. We always use the "unsigned" attribute
16243 in this situation, because ordinarily we guess whether the type
16244 is unsigned -- but the guess can be wrong and the underlying type
16245 can tell us the reality. However, we defer to a local size
16246 attribute if one exists, because this lets the compiler override
16247 the underlying type if needed. */
16248 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16249 {
16250 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16251 if (TYPE_LENGTH (type) == 0)
16252 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16253 if (TYPE_RAW_ALIGN (type) == 0
16254 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
16255 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
16256 }
16257
16258 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16259
16260 return set_die_type (die, type, cu);
16261 }
16262
16263 /* Given a pointer to a die which begins an enumeration, process all
16264 the dies that define the members of the enumeration, and create the
16265 symbol for the enumeration type.
16266
16267 NOTE: We reverse the order of the element list. */
16268
16269 static void
16270 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16271 {
16272 struct type *this_type;
16273
16274 this_type = get_die_type (die, cu);
16275 if (this_type == NULL)
16276 this_type = read_enumeration_type (die, cu);
16277
16278 if (die->child != NULL)
16279 {
16280 struct die_info *child_die;
16281 struct symbol *sym;
16282 struct field *fields = NULL;
16283 int num_fields = 0;
16284 const char *name;
16285
16286 child_die = die->child;
16287 while (child_die && child_die->tag)
16288 {
16289 if (child_die->tag != DW_TAG_enumerator)
16290 {
16291 process_die (child_die, cu);
16292 }
16293 else
16294 {
16295 name = dwarf2_name (child_die, cu);
16296 if (name)
16297 {
16298 sym = new_symbol (child_die, this_type, cu);
16299
16300 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16301 {
16302 fields = (struct field *)
16303 xrealloc (fields,
16304 (num_fields + DW_FIELD_ALLOC_CHUNK)
16305 * sizeof (struct field));
16306 }
16307
16308 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
16309 FIELD_TYPE (fields[num_fields]) = NULL;
16310 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
16311 FIELD_BITSIZE (fields[num_fields]) = 0;
16312
16313 num_fields++;
16314 }
16315 }
16316
16317 child_die = sibling_die (child_die);
16318 }
16319
16320 if (num_fields)
16321 {
16322 TYPE_NFIELDS (this_type) = num_fields;
16323 TYPE_FIELDS (this_type) = (struct field *)
16324 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16325 memcpy (TYPE_FIELDS (this_type), fields,
16326 sizeof (struct field) * num_fields);
16327 xfree (fields);
16328 }
16329 }
16330
16331 /* If we are reading an enum from a .debug_types unit, and the enum
16332 is a declaration, and the enum is not the signatured type in the
16333 unit, then we do not want to add a symbol for it. Adding a
16334 symbol would in some cases obscure the true definition of the
16335 enum, giving users an incomplete type when the definition is
16336 actually available. Note that we do not want to do this for all
16337 enums which are just declarations, because C++0x allows forward
16338 enum declarations. */
16339 if (cu->per_cu->is_debug_types
16340 && die_is_declaration (die, cu))
16341 {
16342 struct signatured_type *sig_type;
16343
16344 sig_type = (struct signatured_type *) cu->per_cu;
16345 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16346 if (sig_type->type_offset_in_section != die->sect_off)
16347 return;
16348 }
16349
16350 new_symbol (die, this_type, cu);
16351 }
16352
16353 /* Extract all information from a DW_TAG_array_type DIE and put it in
16354 the DIE's type field. For now, this only handles one dimensional
16355 arrays. */
16356
16357 static struct type *
16358 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16359 {
16360 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16361 struct die_info *child_die;
16362 struct type *type;
16363 struct type *element_type, *range_type, *index_type;
16364 struct attribute *attr;
16365 const char *name;
16366 struct dynamic_prop *byte_stride_prop = NULL;
16367 unsigned int bit_stride = 0;
16368
16369 element_type = die_type (die, cu);
16370
16371 /* The die_type call above may have already set the type for this DIE. */
16372 type = get_die_type (die, cu);
16373 if (type)
16374 return type;
16375
16376 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16377 if (attr != NULL)
16378 {
16379 int stride_ok;
16380
16381 byte_stride_prop
16382 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16383 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop);
16384 if (!stride_ok)
16385 {
16386 complaint (&symfile_complaints,
16387 _("unable to read array DW_AT_byte_stride "
16388 " - DIE at %s [in module %s]"),
16389 sect_offset_str (die->sect_off),
16390 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16391 /* Ignore this attribute. We will likely not be able to print
16392 arrays of this type correctly, but there is little we can do
16393 to help if we cannot read the attribute's value. */
16394 byte_stride_prop = NULL;
16395 }
16396 }
16397
16398 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16399 if (attr != NULL)
16400 bit_stride = DW_UNSND (attr);
16401
16402 /* Irix 6.2 native cc creates array types without children for
16403 arrays with unspecified length. */
16404 if (die->child == NULL)
16405 {
16406 index_type = objfile_type (objfile)->builtin_int;
16407 range_type = create_static_range_type (NULL, index_type, 0, -1);
16408 type = create_array_type_with_stride (NULL, element_type, range_type,
16409 byte_stride_prop, bit_stride);
16410 return set_die_type (die, type, cu);
16411 }
16412
16413 std::vector<struct type *> range_types;
16414 child_die = die->child;
16415 while (child_die && child_die->tag)
16416 {
16417 if (child_die->tag == DW_TAG_subrange_type)
16418 {
16419 struct type *child_type = read_type_die (child_die, cu);
16420
16421 if (child_type != NULL)
16422 {
16423 /* The range type was succesfully read. Save it for the
16424 array type creation. */
16425 range_types.push_back (child_type);
16426 }
16427 }
16428 child_die = sibling_die (child_die);
16429 }
16430
16431 /* Dwarf2 dimensions are output from left to right, create the
16432 necessary array types in backwards order. */
16433
16434 type = element_type;
16435
16436 if (read_array_order (die, cu) == DW_ORD_col_major)
16437 {
16438 int i = 0;
16439
16440 while (i < range_types.size ())
16441 type = create_array_type_with_stride (NULL, type, range_types[i++],
16442 byte_stride_prop, bit_stride);
16443 }
16444 else
16445 {
16446 size_t ndim = range_types.size ();
16447 while (ndim-- > 0)
16448 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16449 byte_stride_prop, bit_stride);
16450 }
16451
16452 /* Understand Dwarf2 support for vector types (like they occur on
16453 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16454 array type. This is not part of the Dwarf2/3 standard yet, but a
16455 custom vendor extension. The main difference between a regular
16456 array and the vector variant is that vectors are passed by value
16457 to functions. */
16458 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16459 if (attr)
16460 make_vector_type (type);
16461
16462 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16463 implementation may choose to implement triple vectors using this
16464 attribute. */
16465 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16466 if (attr)
16467 {
16468 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16469 TYPE_LENGTH (type) = DW_UNSND (attr);
16470 else
16471 complaint (&symfile_complaints,
16472 _("DW_AT_byte_size for array type smaller "
16473 "than the total size of elements"));
16474 }
16475
16476 name = dwarf2_name (die, cu);
16477 if (name)
16478 TYPE_NAME (type) = name;
16479
16480 maybe_set_alignment (cu, die, type);
16481
16482 /* Install the type in the die. */
16483 set_die_type (die, type, cu);
16484
16485 /* set_die_type should be already done. */
16486 set_descriptive_type (type, die, cu);
16487
16488 return type;
16489 }
16490
16491 static enum dwarf_array_dim_ordering
16492 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16493 {
16494 struct attribute *attr;
16495
16496 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16497
16498 if (attr)
16499 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16500
16501 /* GNU F77 is a special case, as at 08/2004 array type info is the
16502 opposite order to the dwarf2 specification, but data is still
16503 laid out as per normal fortran.
16504
16505 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16506 version checking. */
16507
16508 if (cu->language == language_fortran
16509 && cu->producer && strstr (cu->producer, "GNU F77"))
16510 {
16511 return DW_ORD_row_major;
16512 }
16513
16514 switch (cu->language_defn->la_array_ordering)
16515 {
16516 case array_column_major:
16517 return DW_ORD_col_major;
16518 case array_row_major:
16519 default:
16520 return DW_ORD_row_major;
16521 };
16522 }
16523
16524 /* Extract all information from a DW_TAG_set_type DIE and put it in
16525 the DIE's type field. */
16526
16527 static struct type *
16528 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16529 {
16530 struct type *domain_type, *set_type;
16531 struct attribute *attr;
16532
16533 domain_type = die_type (die, cu);
16534
16535 /* The die_type call above may have already set the type for this DIE. */
16536 set_type = get_die_type (die, cu);
16537 if (set_type)
16538 return set_type;
16539
16540 set_type = create_set_type (NULL, domain_type);
16541
16542 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16543 if (attr)
16544 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16545
16546 maybe_set_alignment (cu, die, set_type);
16547
16548 return set_die_type (die, set_type, cu);
16549 }
16550
16551 /* A helper for read_common_block that creates a locexpr baton.
16552 SYM is the symbol which we are marking as computed.
16553 COMMON_DIE is the DIE for the common block.
16554 COMMON_LOC is the location expression attribute for the common
16555 block itself.
16556 MEMBER_LOC is the location expression attribute for the particular
16557 member of the common block that we are processing.
16558 CU is the CU from which the above come. */
16559
16560 static void
16561 mark_common_block_symbol_computed (struct symbol *sym,
16562 struct die_info *common_die,
16563 struct attribute *common_loc,
16564 struct attribute *member_loc,
16565 struct dwarf2_cu *cu)
16566 {
16567 struct dwarf2_per_objfile *dwarf2_per_objfile
16568 = cu->per_cu->dwarf2_per_objfile;
16569 struct objfile *objfile = dwarf2_per_objfile->objfile;
16570 struct dwarf2_locexpr_baton *baton;
16571 gdb_byte *ptr;
16572 unsigned int cu_off;
16573 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16574 LONGEST offset = 0;
16575
16576 gdb_assert (common_loc && member_loc);
16577 gdb_assert (attr_form_is_block (common_loc));
16578 gdb_assert (attr_form_is_block (member_loc)
16579 || attr_form_is_constant (member_loc));
16580
16581 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16582 baton->per_cu = cu->per_cu;
16583 gdb_assert (baton->per_cu);
16584
16585 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16586
16587 if (attr_form_is_constant (member_loc))
16588 {
16589 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16590 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16591 }
16592 else
16593 baton->size += DW_BLOCK (member_loc)->size;
16594
16595 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16596 baton->data = ptr;
16597
16598 *ptr++ = DW_OP_call4;
16599 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16600 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16601 ptr += 4;
16602
16603 if (attr_form_is_constant (member_loc))
16604 {
16605 *ptr++ = DW_OP_addr;
16606 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16607 ptr += cu->header.addr_size;
16608 }
16609 else
16610 {
16611 /* We have to copy the data here, because DW_OP_call4 will only
16612 use a DW_AT_location attribute. */
16613 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16614 ptr += DW_BLOCK (member_loc)->size;
16615 }
16616
16617 *ptr++ = DW_OP_plus;
16618 gdb_assert (ptr - baton->data == baton->size);
16619
16620 SYMBOL_LOCATION_BATON (sym) = baton;
16621 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16622 }
16623
16624 /* Create appropriate locally-scoped variables for all the
16625 DW_TAG_common_block entries. Also create a struct common_block
16626 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16627 is used to sepate the common blocks name namespace from regular
16628 variable names. */
16629
16630 static void
16631 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16632 {
16633 struct attribute *attr;
16634
16635 attr = dwarf2_attr (die, DW_AT_location, cu);
16636 if (attr)
16637 {
16638 /* Support the .debug_loc offsets. */
16639 if (attr_form_is_block (attr))
16640 {
16641 /* Ok. */
16642 }
16643 else if (attr_form_is_section_offset (attr))
16644 {
16645 dwarf2_complex_location_expr_complaint ();
16646 attr = NULL;
16647 }
16648 else
16649 {
16650 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16651 "common block member");
16652 attr = NULL;
16653 }
16654 }
16655
16656 if (die->child != NULL)
16657 {
16658 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16659 struct die_info *child_die;
16660 size_t n_entries = 0, size;
16661 struct common_block *common_block;
16662 struct symbol *sym;
16663
16664 for (child_die = die->child;
16665 child_die && child_die->tag;
16666 child_die = sibling_die (child_die))
16667 ++n_entries;
16668
16669 size = (sizeof (struct common_block)
16670 + (n_entries - 1) * sizeof (struct symbol *));
16671 common_block
16672 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16673 size);
16674 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16675 common_block->n_entries = 0;
16676
16677 for (child_die = die->child;
16678 child_die && child_die->tag;
16679 child_die = sibling_die (child_die))
16680 {
16681 /* Create the symbol in the DW_TAG_common_block block in the current
16682 symbol scope. */
16683 sym = new_symbol (child_die, NULL, cu);
16684 if (sym != NULL)
16685 {
16686 struct attribute *member_loc;
16687
16688 common_block->contents[common_block->n_entries++] = sym;
16689
16690 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16691 cu);
16692 if (member_loc)
16693 {
16694 /* GDB has handled this for a long time, but it is
16695 not specified by DWARF. It seems to have been
16696 emitted by gfortran at least as recently as:
16697 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16698 complaint (&symfile_complaints,
16699 _("Variable in common block has "
16700 "DW_AT_data_member_location "
16701 "- DIE at %s [in module %s]"),
16702 sect_offset_str (child_die->sect_off),
16703 objfile_name (objfile));
16704
16705 if (attr_form_is_section_offset (member_loc))
16706 dwarf2_complex_location_expr_complaint ();
16707 else if (attr_form_is_constant (member_loc)
16708 || attr_form_is_block (member_loc))
16709 {
16710 if (attr)
16711 mark_common_block_symbol_computed (sym, die, attr,
16712 member_loc, cu);
16713 }
16714 else
16715 dwarf2_complex_location_expr_complaint ();
16716 }
16717 }
16718 }
16719
16720 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16721 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16722 }
16723 }
16724
16725 /* Create a type for a C++ namespace. */
16726
16727 static struct type *
16728 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16729 {
16730 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16731 const char *previous_prefix, *name;
16732 int is_anonymous;
16733 struct type *type;
16734
16735 /* For extensions, reuse the type of the original namespace. */
16736 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16737 {
16738 struct die_info *ext_die;
16739 struct dwarf2_cu *ext_cu = cu;
16740
16741 ext_die = dwarf2_extension (die, &ext_cu);
16742 type = read_type_die (ext_die, ext_cu);
16743
16744 /* EXT_CU may not be the same as CU.
16745 Ensure TYPE is recorded with CU in die_type_hash. */
16746 return set_die_type (die, type, cu);
16747 }
16748
16749 name = namespace_name (die, &is_anonymous, cu);
16750
16751 /* Now build the name of the current namespace. */
16752
16753 previous_prefix = determine_prefix (die, cu);
16754 if (previous_prefix[0] != '\0')
16755 name = typename_concat (&objfile->objfile_obstack,
16756 previous_prefix, name, 0, cu);
16757
16758 /* Create the type. */
16759 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16760 TYPE_TAG_NAME (type) = TYPE_NAME (type);
16761
16762 return set_die_type (die, type, cu);
16763 }
16764
16765 /* Read a namespace scope. */
16766
16767 static void
16768 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16769 {
16770 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16771 int is_anonymous;
16772
16773 /* Add a symbol associated to this if we haven't seen the namespace
16774 before. Also, add a using directive if it's an anonymous
16775 namespace. */
16776
16777 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16778 {
16779 struct type *type;
16780
16781 type = read_type_die (die, cu);
16782 new_symbol (die, type, cu);
16783
16784 namespace_name (die, &is_anonymous, cu);
16785 if (is_anonymous)
16786 {
16787 const char *previous_prefix = determine_prefix (die, cu);
16788
16789 std::vector<const char *> excludes;
16790 add_using_directive (using_directives (cu->language),
16791 previous_prefix, TYPE_NAME (type), NULL,
16792 NULL, excludes, 0, &objfile->objfile_obstack);
16793 }
16794 }
16795
16796 if (die->child != NULL)
16797 {
16798 struct die_info *child_die = die->child;
16799
16800 while (child_die && child_die->tag)
16801 {
16802 process_die (child_die, cu);
16803 child_die = sibling_die (child_die);
16804 }
16805 }
16806 }
16807
16808 /* Read a Fortran module as type. This DIE can be only a declaration used for
16809 imported module. Still we need that type as local Fortran "use ... only"
16810 declaration imports depend on the created type in determine_prefix. */
16811
16812 static struct type *
16813 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16814 {
16815 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16816 const char *module_name;
16817 struct type *type;
16818
16819 module_name = dwarf2_name (die, cu);
16820 if (!module_name)
16821 complaint (&symfile_complaints,
16822 _("DW_TAG_module has no name, offset %s"),
16823 sect_offset_str (die->sect_off));
16824 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16825
16826 /* determine_prefix uses TYPE_TAG_NAME. */
16827 TYPE_TAG_NAME (type) = TYPE_NAME (type);
16828
16829 return set_die_type (die, type, cu);
16830 }
16831
16832 /* Read a Fortran module. */
16833
16834 static void
16835 read_module (struct die_info *die, struct dwarf2_cu *cu)
16836 {
16837 struct die_info *child_die = die->child;
16838 struct type *type;
16839
16840 type = read_type_die (die, cu);
16841 new_symbol (die, type, cu);
16842
16843 while (child_die && child_die->tag)
16844 {
16845 process_die (child_die, cu);
16846 child_die = sibling_die (child_die);
16847 }
16848 }
16849
16850 /* Return the name of the namespace represented by DIE. Set
16851 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16852 namespace. */
16853
16854 static const char *
16855 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16856 {
16857 struct die_info *current_die;
16858 const char *name = NULL;
16859
16860 /* Loop through the extensions until we find a name. */
16861
16862 for (current_die = die;
16863 current_die != NULL;
16864 current_die = dwarf2_extension (die, &cu))
16865 {
16866 /* We don't use dwarf2_name here so that we can detect the absence
16867 of a name -> anonymous namespace. */
16868 name = dwarf2_string_attr (die, DW_AT_name, cu);
16869
16870 if (name != NULL)
16871 break;
16872 }
16873
16874 /* Is it an anonymous namespace? */
16875
16876 *is_anonymous = (name == NULL);
16877 if (*is_anonymous)
16878 name = CP_ANONYMOUS_NAMESPACE_STR;
16879
16880 return name;
16881 }
16882
16883 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16884 the user defined type vector. */
16885
16886 static struct type *
16887 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16888 {
16889 struct gdbarch *gdbarch
16890 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
16891 struct comp_unit_head *cu_header = &cu->header;
16892 struct type *type;
16893 struct attribute *attr_byte_size;
16894 struct attribute *attr_address_class;
16895 int byte_size, addr_class;
16896 struct type *target_type;
16897
16898 target_type = die_type (die, cu);
16899
16900 /* The die_type call above may have already set the type for this DIE. */
16901 type = get_die_type (die, cu);
16902 if (type)
16903 return type;
16904
16905 type = lookup_pointer_type (target_type);
16906
16907 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16908 if (attr_byte_size)
16909 byte_size = DW_UNSND (attr_byte_size);
16910 else
16911 byte_size = cu_header->addr_size;
16912
16913 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16914 if (attr_address_class)
16915 addr_class = DW_UNSND (attr_address_class);
16916 else
16917 addr_class = DW_ADDR_none;
16918
16919 ULONGEST alignment = get_alignment (cu, die);
16920
16921 /* If the pointer size, alignment, or address class is different
16922 than the default, create a type variant marked as such and set
16923 the length accordingly. */
16924 if (TYPE_LENGTH (type) != byte_size
16925 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16926 && alignment != TYPE_RAW_ALIGN (type))
16927 || addr_class != DW_ADDR_none)
16928 {
16929 if (gdbarch_address_class_type_flags_p (gdbarch))
16930 {
16931 int type_flags;
16932
16933 type_flags = gdbarch_address_class_type_flags
16934 (gdbarch, byte_size, addr_class);
16935 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16936 == 0);
16937 type = make_type_with_address_space (type, type_flags);
16938 }
16939 else if (TYPE_LENGTH (type) != byte_size)
16940 {
16941 complaint (&symfile_complaints,
16942 _("invalid pointer size %d"), byte_size);
16943 }
16944 else if (TYPE_RAW_ALIGN (type) != alignment)
16945 {
16946 complaint (&symfile_complaints,
16947 _("Invalid DW_AT_alignment"
16948 " - DIE at %s [in module %s]"),
16949 sect_offset_str (die->sect_off),
16950 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16951 }
16952 else
16953 {
16954 /* Should we also complain about unhandled address classes? */
16955 }
16956 }
16957
16958 TYPE_LENGTH (type) = byte_size;
16959 set_type_align (type, alignment);
16960 return set_die_type (die, type, cu);
16961 }
16962
16963 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16964 the user defined type vector. */
16965
16966 static struct type *
16967 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16968 {
16969 struct type *type;
16970 struct type *to_type;
16971 struct type *domain;
16972
16973 to_type = die_type (die, cu);
16974 domain = die_containing_type (die, cu);
16975
16976 /* The calls above may have already set the type for this DIE. */
16977 type = get_die_type (die, cu);
16978 if (type)
16979 return type;
16980
16981 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16982 type = lookup_methodptr_type (to_type);
16983 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16984 {
16985 struct type *new_type
16986 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
16987
16988 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16989 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16990 TYPE_VARARGS (to_type));
16991 type = lookup_methodptr_type (new_type);
16992 }
16993 else
16994 type = lookup_memberptr_type (to_type, domain);
16995
16996 return set_die_type (die, type, cu);
16997 }
16998
16999 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17000 the user defined type vector. */
17001
17002 static struct type *
17003 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17004 enum type_code refcode)
17005 {
17006 struct comp_unit_head *cu_header = &cu->header;
17007 struct type *type, *target_type;
17008 struct attribute *attr;
17009
17010 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17011
17012 target_type = die_type (die, cu);
17013
17014 /* The die_type call above may have already set the type for this DIE. */
17015 type = get_die_type (die, cu);
17016 if (type)
17017 return type;
17018
17019 type = lookup_reference_type (target_type, refcode);
17020 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17021 if (attr)
17022 {
17023 TYPE_LENGTH (type) = DW_UNSND (attr);
17024 }
17025 else
17026 {
17027 TYPE_LENGTH (type) = cu_header->addr_size;
17028 }
17029 maybe_set_alignment (cu, die, type);
17030 return set_die_type (die, type, cu);
17031 }
17032
17033 /* Add the given cv-qualifiers to the element type of the array. GCC
17034 outputs DWARF type qualifiers that apply to an array, not the
17035 element type. But GDB relies on the array element type to carry
17036 the cv-qualifiers. This mimics section 6.7.3 of the C99
17037 specification. */
17038
17039 static struct type *
17040 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17041 struct type *base_type, int cnst, int voltl)
17042 {
17043 struct type *el_type, *inner_array;
17044
17045 base_type = copy_type (base_type);
17046 inner_array = base_type;
17047
17048 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17049 {
17050 TYPE_TARGET_TYPE (inner_array) =
17051 copy_type (TYPE_TARGET_TYPE (inner_array));
17052 inner_array = TYPE_TARGET_TYPE (inner_array);
17053 }
17054
17055 el_type = TYPE_TARGET_TYPE (inner_array);
17056 cnst |= TYPE_CONST (el_type);
17057 voltl |= TYPE_VOLATILE (el_type);
17058 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17059
17060 return set_die_type (die, base_type, cu);
17061 }
17062
17063 static struct type *
17064 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17065 {
17066 struct type *base_type, *cv_type;
17067
17068 base_type = die_type (die, cu);
17069
17070 /* The die_type call above may have already set the type for this DIE. */
17071 cv_type = get_die_type (die, cu);
17072 if (cv_type)
17073 return cv_type;
17074
17075 /* In case the const qualifier is applied to an array type, the element type
17076 is so qualified, not the array type (section 6.7.3 of C99). */
17077 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17078 return add_array_cv_type (die, cu, base_type, 1, 0);
17079
17080 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17081 return set_die_type (die, cv_type, cu);
17082 }
17083
17084 static struct type *
17085 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17086 {
17087 struct type *base_type, *cv_type;
17088
17089 base_type = die_type (die, cu);
17090
17091 /* The die_type call above may have already set the type for this DIE. */
17092 cv_type = get_die_type (die, cu);
17093 if (cv_type)
17094 return cv_type;
17095
17096 /* In case the volatile qualifier is applied to an array type, the
17097 element type is so qualified, not the array type (section 6.7.3
17098 of C99). */
17099 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17100 return add_array_cv_type (die, cu, base_type, 0, 1);
17101
17102 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17103 return set_die_type (die, cv_type, cu);
17104 }
17105
17106 /* Handle DW_TAG_restrict_type. */
17107
17108 static struct type *
17109 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17110 {
17111 struct type *base_type, *cv_type;
17112
17113 base_type = die_type (die, cu);
17114
17115 /* The die_type call above may have already set the type for this DIE. */
17116 cv_type = get_die_type (die, cu);
17117 if (cv_type)
17118 return cv_type;
17119
17120 cv_type = make_restrict_type (base_type);
17121 return set_die_type (die, cv_type, cu);
17122 }
17123
17124 /* Handle DW_TAG_atomic_type. */
17125
17126 static struct type *
17127 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17128 {
17129 struct type *base_type, *cv_type;
17130
17131 base_type = die_type (die, cu);
17132
17133 /* The die_type call above may have already set the type for this DIE. */
17134 cv_type = get_die_type (die, cu);
17135 if (cv_type)
17136 return cv_type;
17137
17138 cv_type = make_atomic_type (base_type);
17139 return set_die_type (die, cv_type, cu);
17140 }
17141
17142 /* Extract all information from a DW_TAG_string_type DIE and add to
17143 the user defined type vector. It isn't really a user defined type,
17144 but it behaves like one, with other DIE's using an AT_user_def_type
17145 attribute to reference it. */
17146
17147 static struct type *
17148 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17149 {
17150 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17151 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17152 struct type *type, *range_type, *index_type, *char_type;
17153 struct attribute *attr;
17154 unsigned int length;
17155
17156 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17157 if (attr)
17158 {
17159 length = DW_UNSND (attr);
17160 }
17161 else
17162 {
17163 /* Check for the DW_AT_byte_size attribute. */
17164 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17165 if (attr)
17166 {
17167 length = DW_UNSND (attr);
17168 }
17169 else
17170 {
17171 length = 1;
17172 }
17173 }
17174
17175 index_type = objfile_type (objfile)->builtin_int;
17176 range_type = create_static_range_type (NULL, index_type, 1, length);
17177 char_type = language_string_char_type (cu->language_defn, gdbarch);
17178 type = create_string_type (NULL, char_type, range_type);
17179
17180 return set_die_type (die, type, cu);
17181 }
17182
17183 /* Assuming that DIE corresponds to a function, returns nonzero
17184 if the function is prototyped. */
17185
17186 static int
17187 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17188 {
17189 struct attribute *attr;
17190
17191 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17192 if (attr && (DW_UNSND (attr) != 0))
17193 return 1;
17194
17195 /* The DWARF standard implies that the DW_AT_prototyped attribute
17196 is only meaninful for C, but the concept also extends to other
17197 languages that allow unprototyped functions (Eg: Objective C).
17198 For all other languages, assume that functions are always
17199 prototyped. */
17200 if (cu->language != language_c
17201 && cu->language != language_objc
17202 && cu->language != language_opencl)
17203 return 1;
17204
17205 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17206 prototyped and unprototyped functions; default to prototyped,
17207 since that is more common in modern code (and RealView warns
17208 about unprototyped functions). */
17209 if (producer_is_realview (cu->producer))
17210 return 1;
17211
17212 return 0;
17213 }
17214
17215 /* Handle DIES due to C code like:
17216
17217 struct foo
17218 {
17219 int (*funcp)(int a, long l);
17220 int b;
17221 };
17222
17223 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17224
17225 static struct type *
17226 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17227 {
17228 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17229 struct type *type; /* Type that this function returns. */
17230 struct type *ftype; /* Function that returns above type. */
17231 struct attribute *attr;
17232
17233 type = die_type (die, cu);
17234
17235 /* The die_type call above may have already set the type for this DIE. */
17236 ftype = get_die_type (die, cu);
17237 if (ftype)
17238 return ftype;
17239
17240 ftype = lookup_function_type (type);
17241
17242 if (prototyped_function_p (die, cu))
17243 TYPE_PROTOTYPED (ftype) = 1;
17244
17245 /* Store the calling convention in the type if it's available in
17246 the subroutine die. Otherwise set the calling convention to
17247 the default value DW_CC_normal. */
17248 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17249 if (attr)
17250 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17251 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17252 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17253 else
17254 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17255
17256 /* Record whether the function returns normally to its caller or not
17257 if the DWARF producer set that information. */
17258 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17259 if (attr && (DW_UNSND (attr) != 0))
17260 TYPE_NO_RETURN (ftype) = 1;
17261
17262 /* We need to add the subroutine type to the die immediately so
17263 we don't infinitely recurse when dealing with parameters
17264 declared as the same subroutine type. */
17265 set_die_type (die, ftype, cu);
17266
17267 if (die->child != NULL)
17268 {
17269 struct type *void_type = objfile_type (objfile)->builtin_void;
17270 struct die_info *child_die;
17271 int nparams, iparams;
17272
17273 /* Count the number of parameters.
17274 FIXME: GDB currently ignores vararg functions, but knows about
17275 vararg member functions. */
17276 nparams = 0;
17277 child_die = die->child;
17278 while (child_die && child_die->tag)
17279 {
17280 if (child_die->tag == DW_TAG_formal_parameter)
17281 nparams++;
17282 else if (child_die->tag == DW_TAG_unspecified_parameters)
17283 TYPE_VARARGS (ftype) = 1;
17284 child_die = sibling_die (child_die);
17285 }
17286
17287 /* Allocate storage for parameters and fill them in. */
17288 TYPE_NFIELDS (ftype) = nparams;
17289 TYPE_FIELDS (ftype) = (struct field *)
17290 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
17291
17292 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17293 even if we error out during the parameters reading below. */
17294 for (iparams = 0; iparams < nparams; iparams++)
17295 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17296
17297 iparams = 0;
17298 child_die = die->child;
17299 while (child_die && child_die->tag)
17300 {
17301 if (child_die->tag == DW_TAG_formal_parameter)
17302 {
17303 struct type *arg_type;
17304
17305 /* DWARF version 2 has no clean way to discern C++
17306 static and non-static member functions. G++ helps
17307 GDB by marking the first parameter for non-static
17308 member functions (which is the this pointer) as
17309 artificial. We pass this information to
17310 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17311
17312 DWARF version 3 added DW_AT_object_pointer, which GCC
17313 4.5 does not yet generate. */
17314 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17315 if (attr)
17316 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17317 else
17318 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17319 arg_type = die_type (child_die, cu);
17320
17321 /* RealView does not mark THIS as const, which the testsuite
17322 expects. GCC marks THIS as const in method definitions,
17323 but not in the class specifications (GCC PR 43053). */
17324 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17325 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17326 {
17327 int is_this = 0;
17328 struct dwarf2_cu *arg_cu = cu;
17329 const char *name = dwarf2_name (child_die, cu);
17330
17331 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17332 if (attr)
17333 {
17334 /* If the compiler emits this, use it. */
17335 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17336 is_this = 1;
17337 }
17338 else if (name && strcmp (name, "this") == 0)
17339 /* Function definitions will have the argument names. */
17340 is_this = 1;
17341 else if (name == NULL && iparams == 0)
17342 /* Declarations may not have the names, so like
17343 elsewhere in GDB, assume an artificial first
17344 argument is "this". */
17345 is_this = 1;
17346
17347 if (is_this)
17348 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17349 arg_type, 0);
17350 }
17351
17352 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17353 iparams++;
17354 }
17355 child_die = sibling_die (child_die);
17356 }
17357 }
17358
17359 return ftype;
17360 }
17361
17362 static struct type *
17363 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17364 {
17365 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17366 const char *name = NULL;
17367 struct type *this_type, *target_type;
17368
17369 name = dwarf2_full_name (NULL, die, cu);
17370 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17371 TYPE_TARGET_STUB (this_type) = 1;
17372 set_die_type (die, this_type, cu);
17373 target_type = die_type (die, cu);
17374 if (target_type != this_type)
17375 TYPE_TARGET_TYPE (this_type) = target_type;
17376 else
17377 {
17378 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17379 spec and cause infinite loops in GDB. */
17380 complaint (&symfile_complaints,
17381 _("Self-referential DW_TAG_typedef "
17382 "- DIE at %s [in module %s]"),
17383 sect_offset_str (die->sect_off), objfile_name (objfile));
17384 TYPE_TARGET_TYPE (this_type) = NULL;
17385 }
17386 return this_type;
17387 }
17388
17389 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17390 (which may be different from NAME) to the architecture back-end to allow
17391 it to guess the correct format if necessary. */
17392
17393 static struct type *
17394 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17395 const char *name_hint)
17396 {
17397 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17398 const struct floatformat **format;
17399 struct type *type;
17400
17401 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17402 if (format)
17403 type = init_float_type (objfile, bits, name, format);
17404 else
17405 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17406
17407 return type;
17408 }
17409
17410 /* Find a representation of a given base type and install
17411 it in the TYPE field of the die. */
17412
17413 static struct type *
17414 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17415 {
17416 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17417 struct type *type;
17418 struct attribute *attr;
17419 int encoding = 0, bits = 0;
17420 const char *name;
17421
17422 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17423 if (attr)
17424 {
17425 encoding = DW_UNSND (attr);
17426 }
17427 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17428 if (attr)
17429 {
17430 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17431 }
17432 name = dwarf2_name (die, cu);
17433 if (!name)
17434 {
17435 complaint (&symfile_complaints,
17436 _("DW_AT_name missing from DW_TAG_base_type"));
17437 }
17438
17439 switch (encoding)
17440 {
17441 case DW_ATE_address:
17442 /* Turn DW_ATE_address into a void * pointer. */
17443 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17444 type = init_pointer_type (objfile, bits, name, type);
17445 break;
17446 case DW_ATE_boolean:
17447 type = init_boolean_type (objfile, bits, 1, name);
17448 break;
17449 case DW_ATE_complex_float:
17450 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
17451 type = init_complex_type (objfile, name, type);
17452 break;
17453 case DW_ATE_decimal_float:
17454 type = init_decfloat_type (objfile, bits, name);
17455 break;
17456 case DW_ATE_float:
17457 type = dwarf2_init_float_type (objfile, bits, name, name);
17458 break;
17459 case DW_ATE_signed:
17460 type = init_integer_type (objfile, bits, 0, name);
17461 break;
17462 case DW_ATE_unsigned:
17463 if (cu->language == language_fortran
17464 && name
17465 && startswith (name, "character("))
17466 type = init_character_type (objfile, bits, 1, name);
17467 else
17468 type = init_integer_type (objfile, bits, 1, name);
17469 break;
17470 case DW_ATE_signed_char:
17471 if (cu->language == language_ada || cu->language == language_m2
17472 || cu->language == language_pascal
17473 || cu->language == language_fortran)
17474 type = init_character_type (objfile, bits, 0, name);
17475 else
17476 type = init_integer_type (objfile, bits, 0, name);
17477 break;
17478 case DW_ATE_unsigned_char:
17479 if (cu->language == language_ada || cu->language == language_m2
17480 || cu->language == language_pascal
17481 || cu->language == language_fortran
17482 || cu->language == language_rust)
17483 type = init_character_type (objfile, bits, 1, name);
17484 else
17485 type = init_integer_type (objfile, bits, 1, name);
17486 break;
17487 case DW_ATE_UTF:
17488 {
17489 gdbarch *arch = get_objfile_arch (objfile);
17490
17491 if (bits == 16)
17492 type = builtin_type (arch)->builtin_char16;
17493 else if (bits == 32)
17494 type = builtin_type (arch)->builtin_char32;
17495 else
17496 {
17497 complaint (&symfile_complaints,
17498 _("unsupported DW_ATE_UTF bit size: '%d'"),
17499 bits);
17500 type = init_integer_type (objfile, bits, 1, name);
17501 }
17502 return set_die_type (die, type, cu);
17503 }
17504 break;
17505
17506 default:
17507 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
17508 dwarf_type_encoding_name (encoding));
17509 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17510 break;
17511 }
17512
17513 if (name && strcmp (name, "char") == 0)
17514 TYPE_NOSIGN (type) = 1;
17515
17516 maybe_set_alignment (cu, die, type);
17517
17518 return set_die_type (die, type, cu);
17519 }
17520
17521 /* Parse dwarf attribute if it's a block, reference or constant and put the
17522 resulting value of the attribute into struct bound_prop.
17523 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17524
17525 static int
17526 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17527 struct dwarf2_cu *cu, struct dynamic_prop *prop)
17528 {
17529 struct dwarf2_property_baton *baton;
17530 struct obstack *obstack
17531 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17532
17533 if (attr == NULL || prop == NULL)
17534 return 0;
17535
17536 if (attr_form_is_block (attr))
17537 {
17538 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17539 baton->referenced_type = NULL;
17540 baton->locexpr.per_cu = cu->per_cu;
17541 baton->locexpr.size = DW_BLOCK (attr)->size;
17542 baton->locexpr.data = DW_BLOCK (attr)->data;
17543 prop->data.baton = baton;
17544 prop->kind = PROP_LOCEXPR;
17545 gdb_assert (prop->data.baton != NULL);
17546 }
17547 else if (attr_form_is_ref (attr))
17548 {
17549 struct dwarf2_cu *target_cu = cu;
17550 struct die_info *target_die;
17551 struct attribute *target_attr;
17552
17553 target_die = follow_die_ref (die, attr, &target_cu);
17554 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17555 if (target_attr == NULL)
17556 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17557 target_cu);
17558 if (target_attr == NULL)
17559 return 0;
17560
17561 switch (target_attr->name)
17562 {
17563 case DW_AT_location:
17564 if (attr_form_is_section_offset (target_attr))
17565 {
17566 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17567 baton->referenced_type = die_type (target_die, target_cu);
17568 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17569 prop->data.baton = baton;
17570 prop->kind = PROP_LOCLIST;
17571 gdb_assert (prop->data.baton != NULL);
17572 }
17573 else if (attr_form_is_block (target_attr))
17574 {
17575 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17576 baton->referenced_type = die_type (target_die, target_cu);
17577 baton->locexpr.per_cu = cu->per_cu;
17578 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17579 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17580 prop->data.baton = baton;
17581 prop->kind = PROP_LOCEXPR;
17582 gdb_assert (prop->data.baton != NULL);
17583 }
17584 else
17585 {
17586 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17587 "dynamic property");
17588 return 0;
17589 }
17590 break;
17591 case DW_AT_data_member_location:
17592 {
17593 LONGEST offset;
17594
17595 if (!handle_data_member_location (target_die, target_cu,
17596 &offset))
17597 return 0;
17598
17599 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17600 baton->referenced_type = read_type_die (target_die->parent,
17601 target_cu);
17602 baton->offset_info.offset = offset;
17603 baton->offset_info.type = die_type (target_die, target_cu);
17604 prop->data.baton = baton;
17605 prop->kind = PROP_ADDR_OFFSET;
17606 break;
17607 }
17608 }
17609 }
17610 else if (attr_form_is_constant (attr))
17611 {
17612 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17613 prop->kind = PROP_CONST;
17614 }
17615 else
17616 {
17617 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17618 dwarf2_name (die, cu));
17619 return 0;
17620 }
17621
17622 return 1;
17623 }
17624
17625 /* Read the given DW_AT_subrange DIE. */
17626
17627 static struct type *
17628 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17629 {
17630 struct type *base_type, *orig_base_type;
17631 struct type *range_type;
17632 struct attribute *attr;
17633 struct dynamic_prop low, high;
17634 int low_default_is_valid;
17635 int high_bound_is_count = 0;
17636 const char *name;
17637 LONGEST negative_mask;
17638
17639 orig_base_type = die_type (die, cu);
17640 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17641 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17642 creating the range type, but we use the result of check_typedef
17643 when examining properties of the type. */
17644 base_type = check_typedef (orig_base_type);
17645
17646 /* The die_type call above may have already set the type for this DIE. */
17647 range_type = get_die_type (die, cu);
17648 if (range_type)
17649 return range_type;
17650
17651 low.kind = PROP_CONST;
17652 high.kind = PROP_CONST;
17653 high.data.const_val = 0;
17654
17655 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17656 omitting DW_AT_lower_bound. */
17657 switch (cu->language)
17658 {
17659 case language_c:
17660 case language_cplus:
17661 low.data.const_val = 0;
17662 low_default_is_valid = 1;
17663 break;
17664 case language_fortran:
17665 low.data.const_val = 1;
17666 low_default_is_valid = 1;
17667 break;
17668 case language_d:
17669 case language_objc:
17670 case language_rust:
17671 low.data.const_val = 0;
17672 low_default_is_valid = (cu->header.version >= 4);
17673 break;
17674 case language_ada:
17675 case language_m2:
17676 case language_pascal:
17677 low.data.const_val = 1;
17678 low_default_is_valid = (cu->header.version >= 4);
17679 break;
17680 default:
17681 low.data.const_val = 0;
17682 low_default_is_valid = 0;
17683 break;
17684 }
17685
17686 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17687 if (attr)
17688 attr_to_dynamic_prop (attr, die, cu, &low);
17689 else if (!low_default_is_valid)
17690 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
17691 "- DIE at %s [in module %s]"),
17692 sect_offset_str (die->sect_off),
17693 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17694
17695 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
17696 if (!attr_to_dynamic_prop (attr, die, cu, &high))
17697 {
17698 attr = dwarf2_attr (die, DW_AT_count, cu);
17699 if (attr_to_dynamic_prop (attr, die, cu, &high))
17700 {
17701 /* If bounds are constant do the final calculation here. */
17702 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17703 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17704 else
17705 high_bound_is_count = 1;
17706 }
17707 }
17708
17709 /* Dwarf-2 specifications explicitly allows to create subrange types
17710 without specifying a base type.
17711 In that case, the base type must be set to the type of
17712 the lower bound, upper bound or count, in that order, if any of these
17713 three attributes references an object that has a type.
17714 If no base type is found, the Dwarf-2 specifications say that
17715 a signed integer type of size equal to the size of an address should
17716 be used.
17717 For the following C code: `extern char gdb_int [];'
17718 GCC produces an empty range DIE.
17719 FIXME: muller/2010-05-28: Possible references to object for low bound,
17720 high bound or count are not yet handled by this code. */
17721 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
17722 {
17723 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17724 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17725 int addr_size = gdbarch_addr_bit (gdbarch) /8;
17726 struct type *int_type = objfile_type (objfile)->builtin_int;
17727
17728 /* Test "int", "long int", and "long long int" objfile types,
17729 and select the first one having a size above or equal to the
17730 architecture address size. */
17731 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17732 base_type = int_type;
17733 else
17734 {
17735 int_type = objfile_type (objfile)->builtin_long;
17736 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17737 base_type = int_type;
17738 else
17739 {
17740 int_type = objfile_type (objfile)->builtin_long_long;
17741 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17742 base_type = int_type;
17743 }
17744 }
17745 }
17746
17747 /* Normally, the DWARF producers are expected to use a signed
17748 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17749 But this is unfortunately not always the case, as witnessed
17750 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17751 is used instead. To work around that ambiguity, we treat
17752 the bounds as signed, and thus sign-extend their values, when
17753 the base type is signed. */
17754 negative_mask =
17755 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17756 if (low.kind == PROP_CONST
17757 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17758 low.data.const_val |= negative_mask;
17759 if (high.kind == PROP_CONST
17760 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17761 high.data.const_val |= negative_mask;
17762
17763 range_type = create_range_type (NULL, orig_base_type, &low, &high);
17764
17765 if (high_bound_is_count)
17766 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17767
17768 /* Ada expects an empty array on no boundary attributes. */
17769 if (attr == NULL && cu->language != language_ada)
17770 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17771
17772 name = dwarf2_name (die, cu);
17773 if (name)
17774 TYPE_NAME (range_type) = name;
17775
17776 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17777 if (attr)
17778 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17779
17780 maybe_set_alignment (cu, die, range_type);
17781
17782 set_die_type (die, range_type, cu);
17783
17784 /* set_die_type should be already done. */
17785 set_descriptive_type (range_type, die, cu);
17786
17787 return range_type;
17788 }
17789
17790 static struct type *
17791 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17792 {
17793 struct type *type;
17794
17795 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17796 NULL);
17797 TYPE_NAME (type) = dwarf2_name (die, cu);
17798
17799 /* In Ada, an unspecified type is typically used when the description
17800 of the type is defered to a different unit. When encountering
17801 such a type, we treat it as a stub, and try to resolve it later on,
17802 when needed. */
17803 if (cu->language == language_ada)
17804 TYPE_STUB (type) = 1;
17805
17806 return set_die_type (die, type, cu);
17807 }
17808
17809 /* Read a single die and all its descendents. Set the die's sibling
17810 field to NULL; set other fields in the die correctly, and set all
17811 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17812 location of the info_ptr after reading all of those dies. PARENT
17813 is the parent of the die in question. */
17814
17815 static struct die_info *
17816 read_die_and_children (const struct die_reader_specs *reader,
17817 const gdb_byte *info_ptr,
17818 const gdb_byte **new_info_ptr,
17819 struct die_info *parent)
17820 {
17821 struct die_info *die;
17822 const gdb_byte *cur_ptr;
17823 int has_children;
17824
17825 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
17826 if (die == NULL)
17827 {
17828 *new_info_ptr = cur_ptr;
17829 return NULL;
17830 }
17831 store_in_ref_table (die, reader->cu);
17832
17833 if (has_children)
17834 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17835 else
17836 {
17837 die->child = NULL;
17838 *new_info_ptr = cur_ptr;
17839 }
17840
17841 die->sibling = NULL;
17842 die->parent = parent;
17843 return die;
17844 }
17845
17846 /* Read a die, all of its descendents, and all of its siblings; set
17847 all of the fields of all of the dies correctly. Arguments are as
17848 in read_die_and_children. */
17849
17850 static struct die_info *
17851 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17852 const gdb_byte *info_ptr,
17853 const gdb_byte **new_info_ptr,
17854 struct die_info *parent)
17855 {
17856 struct die_info *first_die, *last_sibling;
17857 const gdb_byte *cur_ptr;
17858
17859 cur_ptr = info_ptr;
17860 first_die = last_sibling = NULL;
17861
17862 while (1)
17863 {
17864 struct die_info *die
17865 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17866
17867 if (die == NULL)
17868 {
17869 *new_info_ptr = cur_ptr;
17870 return first_die;
17871 }
17872
17873 if (!first_die)
17874 first_die = die;
17875 else
17876 last_sibling->sibling = die;
17877
17878 last_sibling = die;
17879 }
17880 }
17881
17882 /* Read a die, all of its descendents, and all of its siblings; set
17883 all of the fields of all of the dies correctly. Arguments are as
17884 in read_die_and_children.
17885 This the main entry point for reading a DIE and all its children. */
17886
17887 static struct die_info *
17888 read_die_and_siblings (const struct die_reader_specs *reader,
17889 const gdb_byte *info_ptr,
17890 const gdb_byte **new_info_ptr,
17891 struct die_info *parent)
17892 {
17893 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17894 new_info_ptr, parent);
17895
17896 if (dwarf_die_debug)
17897 {
17898 fprintf_unfiltered (gdb_stdlog,
17899 "Read die from %s@0x%x of %s:\n",
17900 get_section_name (reader->die_section),
17901 (unsigned) (info_ptr - reader->die_section->buffer),
17902 bfd_get_filename (reader->abfd));
17903 dump_die (die, dwarf_die_debug);
17904 }
17905
17906 return die;
17907 }
17908
17909 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17910 attributes.
17911 The caller is responsible for filling in the extra attributes
17912 and updating (*DIEP)->num_attrs.
17913 Set DIEP to point to a newly allocated die with its information,
17914 except for its child, sibling, and parent fields.
17915 Set HAS_CHILDREN to tell whether the die has children or not. */
17916
17917 static const gdb_byte *
17918 read_full_die_1 (const struct die_reader_specs *reader,
17919 struct die_info **diep, const gdb_byte *info_ptr,
17920 int *has_children, int num_extra_attrs)
17921 {
17922 unsigned int abbrev_number, bytes_read, i;
17923 struct abbrev_info *abbrev;
17924 struct die_info *die;
17925 struct dwarf2_cu *cu = reader->cu;
17926 bfd *abfd = reader->abfd;
17927
17928 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17929 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17930 info_ptr += bytes_read;
17931 if (!abbrev_number)
17932 {
17933 *diep = NULL;
17934 *has_children = 0;
17935 return info_ptr;
17936 }
17937
17938 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
17939 if (!abbrev)
17940 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17941 abbrev_number,
17942 bfd_get_filename (abfd));
17943
17944 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
17945 die->sect_off = sect_off;
17946 die->tag = abbrev->tag;
17947 die->abbrev = abbrev_number;
17948
17949 /* Make the result usable.
17950 The caller needs to update num_attrs after adding the extra
17951 attributes. */
17952 die->num_attrs = abbrev->num_attrs;
17953
17954 for (i = 0; i < abbrev->num_attrs; ++i)
17955 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17956 info_ptr);
17957
17958 *diep = die;
17959 *has_children = abbrev->has_children;
17960 return info_ptr;
17961 }
17962
17963 /* Read a die and all its attributes.
17964 Set DIEP to point to a newly allocated die with its information,
17965 except for its child, sibling, and parent fields.
17966 Set HAS_CHILDREN to tell whether the die has children or not. */
17967
17968 static const gdb_byte *
17969 read_full_die (const struct die_reader_specs *reader,
17970 struct die_info **diep, const gdb_byte *info_ptr,
17971 int *has_children)
17972 {
17973 const gdb_byte *result;
17974
17975 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
17976
17977 if (dwarf_die_debug)
17978 {
17979 fprintf_unfiltered (gdb_stdlog,
17980 "Read die from %s@0x%x of %s:\n",
17981 get_section_name (reader->die_section),
17982 (unsigned) (info_ptr - reader->die_section->buffer),
17983 bfd_get_filename (reader->abfd));
17984 dump_die (*diep, dwarf_die_debug);
17985 }
17986
17987 return result;
17988 }
17989 \f
17990 /* Abbreviation tables.
17991
17992 In DWARF version 2, the description of the debugging information is
17993 stored in a separate .debug_abbrev section. Before we read any
17994 dies from a section we read in all abbreviations and install them
17995 in a hash table. */
17996
17997 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
17998
17999 struct abbrev_info *
18000 abbrev_table::alloc_abbrev ()
18001 {
18002 struct abbrev_info *abbrev;
18003
18004 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
18005 memset (abbrev, 0, sizeof (struct abbrev_info));
18006
18007 return abbrev;
18008 }
18009
18010 /* Add an abbreviation to the table. */
18011
18012 void
18013 abbrev_table::add_abbrev (unsigned int abbrev_number,
18014 struct abbrev_info *abbrev)
18015 {
18016 unsigned int hash_number;
18017
18018 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18019 abbrev->next = m_abbrevs[hash_number];
18020 m_abbrevs[hash_number] = abbrev;
18021 }
18022
18023 /* Look up an abbrev in the table.
18024 Returns NULL if the abbrev is not found. */
18025
18026 struct abbrev_info *
18027 abbrev_table::lookup_abbrev (unsigned int abbrev_number)
18028 {
18029 unsigned int hash_number;
18030 struct abbrev_info *abbrev;
18031
18032 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18033 abbrev = m_abbrevs[hash_number];
18034
18035 while (abbrev)
18036 {
18037 if (abbrev->number == abbrev_number)
18038 return abbrev;
18039 abbrev = abbrev->next;
18040 }
18041 return NULL;
18042 }
18043
18044 /* Read in an abbrev table. */
18045
18046 static abbrev_table_up
18047 abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18048 struct dwarf2_section_info *section,
18049 sect_offset sect_off)
18050 {
18051 struct objfile *objfile = dwarf2_per_objfile->objfile;
18052 bfd *abfd = get_section_bfd_owner (section);
18053 const gdb_byte *abbrev_ptr;
18054 struct abbrev_info *cur_abbrev;
18055 unsigned int abbrev_number, bytes_read, abbrev_name;
18056 unsigned int abbrev_form;
18057 struct attr_abbrev *cur_attrs;
18058 unsigned int allocated_attrs;
18059
18060 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
18061
18062 dwarf2_read_section (objfile, section);
18063 abbrev_ptr = section->buffer + to_underlying (sect_off);
18064 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18065 abbrev_ptr += bytes_read;
18066
18067 allocated_attrs = ATTR_ALLOC_CHUNK;
18068 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
18069
18070 /* Loop until we reach an abbrev number of 0. */
18071 while (abbrev_number)
18072 {
18073 cur_abbrev = abbrev_table->alloc_abbrev ();
18074
18075 /* read in abbrev header */
18076 cur_abbrev->number = abbrev_number;
18077 cur_abbrev->tag
18078 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18079 abbrev_ptr += bytes_read;
18080 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18081 abbrev_ptr += 1;
18082
18083 /* now read in declarations */
18084 for (;;)
18085 {
18086 LONGEST implicit_const;
18087
18088 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18089 abbrev_ptr += bytes_read;
18090 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18091 abbrev_ptr += bytes_read;
18092 if (abbrev_form == DW_FORM_implicit_const)
18093 {
18094 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18095 &bytes_read);
18096 abbrev_ptr += bytes_read;
18097 }
18098 else
18099 {
18100 /* Initialize it due to a false compiler warning. */
18101 implicit_const = -1;
18102 }
18103
18104 if (abbrev_name == 0)
18105 break;
18106
18107 if (cur_abbrev->num_attrs == allocated_attrs)
18108 {
18109 allocated_attrs += ATTR_ALLOC_CHUNK;
18110 cur_attrs
18111 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
18112 }
18113
18114 cur_attrs[cur_abbrev->num_attrs].name
18115 = (enum dwarf_attribute) abbrev_name;
18116 cur_attrs[cur_abbrev->num_attrs].form
18117 = (enum dwarf_form) abbrev_form;
18118 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
18119 ++cur_abbrev->num_attrs;
18120 }
18121
18122 cur_abbrev->attrs =
18123 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18124 cur_abbrev->num_attrs);
18125 memcpy (cur_abbrev->attrs, cur_attrs,
18126 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18127
18128 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
18129
18130 /* Get next abbreviation.
18131 Under Irix6 the abbreviations for a compilation unit are not
18132 always properly terminated with an abbrev number of 0.
18133 Exit loop if we encounter an abbreviation which we have
18134 already read (which means we are about to read the abbreviations
18135 for the next compile unit) or if the end of the abbreviation
18136 table is reached. */
18137 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
18138 break;
18139 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18140 abbrev_ptr += bytes_read;
18141 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
18142 break;
18143 }
18144
18145 xfree (cur_attrs);
18146 return abbrev_table;
18147 }
18148
18149 /* Returns nonzero if TAG represents a type that we might generate a partial
18150 symbol for. */
18151
18152 static int
18153 is_type_tag_for_partial (int tag)
18154 {
18155 switch (tag)
18156 {
18157 #if 0
18158 /* Some types that would be reasonable to generate partial symbols for,
18159 that we don't at present. */
18160 case DW_TAG_array_type:
18161 case DW_TAG_file_type:
18162 case DW_TAG_ptr_to_member_type:
18163 case DW_TAG_set_type:
18164 case DW_TAG_string_type:
18165 case DW_TAG_subroutine_type:
18166 #endif
18167 case DW_TAG_base_type:
18168 case DW_TAG_class_type:
18169 case DW_TAG_interface_type:
18170 case DW_TAG_enumeration_type:
18171 case DW_TAG_structure_type:
18172 case DW_TAG_subrange_type:
18173 case DW_TAG_typedef:
18174 case DW_TAG_union_type:
18175 return 1;
18176 default:
18177 return 0;
18178 }
18179 }
18180
18181 /* Load all DIEs that are interesting for partial symbols into memory. */
18182
18183 static struct partial_die_info *
18184 load_partial_dies (const struct die_reader_specs *reader,
18185 const gdb_byte *info_ptr, int building_psymtab)
18186 {
18187 struct dwarf2_cu *cu = reader->cu;
18188 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18189 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18190 unsigned int bytes_read;
18191 unsigned int load_all = 0;
18192 int nesting_level = 1;
18193
18194 parent_die = NULL;
18195 last_die = NULL;
18196
18197 gdb_assert (cu->per_cu != NULL);
18198 if (cu->per_cu->load_all_dies)
18199 load_all = 1;
18200
18201 cu->partial_dies
18202 = htab_create_alloc_ex (cu->header.length / 12,
18203 partial_die_hash,
18204 partial_die_eq,
18205 NULL,
18206 &cu->comp_unit_obstack,
18207 hashtab_obstack_allocate,
18208 dummy_obstack_deallocate);
18209
18210 while (1)
18211 {
18212 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18213
18214 /* A NULL abbrev means the end of a series of children. */
18215 if (abbrev == NULL)
18216 {
18217 if (--nesting_level == 0)
18218 return first_die;
18219
18220 info_ptr += bytes_read;
18221 last_die = parent_die;
18222 parent_die = parent_die->die_parent;
18223 continue;
18224 }
18225
18226 /* Check for template arguments. We never save these; if
18227 they're seen, we just mark the parent, and go on our way. */
18228 if (parent_die != NULL
18229 && cu->language == language_cplus
18230 && (abbrev->tag == DW_TAG_template_type_param
18231 || abbrev->tag == DW_TAG_template_value_param))
18232 {
18233 parent_die->has_template_arguments = 1;
18234
18235 if (!load_all)
18236 {
18237 /* We don't need a partial DIE for the template argument. */
18238 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18239 continue;
18240 }
18241 }
18242
18243 /* We only recurse into c++ subprograms looking for template arguments.
18244 Skip their other children. */
18245 if (!load_all
18246 && cu->language == language_cplus
18247 && parent_die != NULL
18248 && parent_die->tag == DW_TAG_subprogram)
18249 {
18250 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18251 continue;
18252 }
18253
18254 /* Check whether this DIE is interesting enough to save. Normally
18255 we would not be interested in members here, but there may be
18256 later variables referencing them via DW_AT_specification (for
18257 static members). */
18258 if (!load_all
18259 && !is_type_tag_for_partial (abbrev->tag)
18260 && abbrev->tag != DW_TAG_constant
18261 && abbrev->tag != DW_TAG_enumerator
18262 && abbrev->tag != DW_TAG_subprogram
18263 && abbrev->tag != DW_TAG_inlined_subroutine
18264 && abbrev->tag != DW_TAG_lexical_block
18265 && abbrev->tag != DW_TAG_variable
18266 && abbrev->tag != DW_TAG_namespace
18267 && abbrev->tag != DW_TAG_module
18268 && abbrev->tag != DW_TAG_member
18269 && abbrev->tag != DW_TAG_imported_unit
18270 && abbrev->tag != DW_TAG_imported_declaration)
18271 {
18272 /* Otherwise we skip to the next sibling, if any. */
18273 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18274 continue;
18275 }
18276
18277 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18278 abbrev);
18279
18280 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18281
18282 /* This two-pass algorithm for processing partial symbols has a
18283 high cost in cache pressure. Thus, handle some simple cases
18284 here which cover the majority of C partial symbols. DIEs
18285 which neither have specification tags in them, nor could have
18286 specification tags elsewhere pointing at them, can simply be
18287 processed and discarded.
18288
18289 This segment is also optional; scan_partial_symbols and
18290 add_partial_symbol will handle these DIEs if we chain
18291 them in normally. When compilers which do not emit large
18292 quantities of duplicate debug information are more common,
18293 this code can probably be removed. */
18294
18295 /* Any complete simple types at the top level (pretty much all
18296 of them, for a language without namespaces), can be processed
18297 directly. */
18298 if (parent_die == NULL
18299 && pdi.has_specification == 0
18300 && pdi.is_declaration == 0
18301 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18302 || pdi.tag == DW_TAG_base_type
18303 || pdi.tag == DW_TAG_subrange_type))
18304 {
18305 if (building_psymtab && pdi.name != NULL)
18306 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
18307 VAR_DOMAIN, LOC_TYPEDEF,
18308 &objfile->static_psymbols,
18309 0, cu->language, objfile);
18310 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18311 continue;
18312 }
18313
18314 /* The exception for DW_TAG_typedef with has_children above is
18315 a workaround of GCC PR debug/47510. In the case of this complaint
18316 type_name_no_tag_or_error will error on such types later.
18317
18318 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18319 it could not find the child DIEs referenced later, this is checked
18320 above. In correct DWARF DW_TAG_typedef should have no children. */
18321
18322 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18323 complaint (&symfile_complaints,
18324 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18325 "- DIE at %s [in module %s]"),
18326 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18327
18328 /* If we're at the second level, and we're an enumerator, and
18329 our parent has no specification (meaning possibly lives in a
18330 namespace elsewhere), then we can add the partial symbol now
18331 instead of queueing it. */
18332 if (pdi.tag == DW_TAG_enumerator
18333 && parent_die != NULL
18334 && parent_die->die_parent == NULL
18335 && parent_die->tag == DW_TAG_enumeration_type
18336 && parent_die->has_specification == 0)
18337 {
18338 if (pdi.name == NULL)
18339 complaint (&symfile_complaints,
18340 _("malformed enumerator DIE ignored"));
18341 else if (building_psymtab)
18342 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
18343 VAR_DOMAIN, LOC_CONST,
18344 cu->language == language_cplus
18345 ? &objfile->global_psymbols
18346 : &objfile->static_psymbols,
18347 0, cu->language, objfile);
18348
18349 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18350 continue;
18351 }
18352
18353 struct partial_die_info *part_die
18354 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18355
18356 /* We'll save this DIE so link it in. */
18357 part_die->die_parent = parent_die;
18358 part_die->die_sibling = NULL;
18359 part_die->die_child = NULL;
18360
18361 if (last_die && last_die == parent_die)
18362 last_die->die_child = part_die;
18363 else if (last_die)
18364 last_die->die_sibling = part_die;
18365
18366 last_die = part_die;
18367
18368 if (first_die == NULL)
18369 first_die = part_die;
18370
18371 /* Maybe add the DIE to the hash table. Not all DIEs that we
18372 find interesting need to be in the hash table, because we
18373 also have the parent/sibling/child chains; only those that we
18374 might refer to by offset later during partial symbol reading.
18375
18376 For now this means things that might have be the target of a
18377 DW_AT_specification, DW_AT_abstract_origin, or
18378 DW_AT_extension. DW_AT_extension will refer only to
18379 namespaces; DW_AT_abstract_origin refers to functions (and
18380 many things under the function DIE, but we do not recurse
18381 into function DIEs during partial symbol reading) and
18382 possibly variables as well; DW_AT_specification refers to
18383 declarations. Declarations ought to have the DW_AT_declaration
18384 flag. It happens that GCC forgets to put it in sometimes, but
18385 only for functions, not for types.
18386
18387 Adding more things than necessary to the hash table is harmless
18388 except for the performance cost. Adding too few will result in
18389 wasted time in find_partial_die, when we reread the compilation
18390 unit with load_all_dies set. */
18391
18392 if (load_all
18393 || abbrev->tag == DW_TAG_constant
18394 || abbrev->tag == DW_TAG_subprogram
18395 || abbrev->tag == DW_TAG_variable
18396 || abbrev->tag == DW_TAG_namespace
18397 || part_die->is_declaration)
18398 {
18399 void **slot;
18400
18401 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18402 to_underlying (part_die->sect_off),
18403 INSERT);
18404 *slot = part_die;
18405 }
18406
18407 /* For some DIEs we want to follow their children (if any). For C
18408 we have no reason to follow the children of structures; for other
18409 languages we have to, so that we can get at method physnames
18410 to infer fully qualified class names, for DW_AT_specification,
18411 and for C++ template arguments. For C++, we also look one level
18412 inside functions to find template arguments (if the name of the
18413 function does not already contain the template arguments).
18414
18415 For Ada, we need to scan the children of subprograms and lexical
18416 blocks as well because Ada allows the definition of nested
18417 entities that could be interesting for the debugger, such as
18418 nested subprograms for instance. */
18419 if (last_die->has_children
18420 && (load_all
18421 || last_die->tag == DW_TAG_namespace
18422 || last_die->tag == DW_TAG_module
18423 || last_die->tag == DW_TAG_enumeration_type
18424 || (cu->language == language_cplus
18425 && last_die->tag == DW_TAG_subprogram
18426 && (last_die->name == NULL
18427 || strchr (last_die->name, '<') == NULL))
18428 || (cu->language != language_c
18429 && (last_die->tag == DW_TAG_class_type
18430 || last_die->tag == DW_TAG_interface_type
18431 || last_die->tag == DW_TAG_structure_type
18432 || last_die->tag == DW_TAG_union_type))
18433 || (cu->language == language_ada
18434 && (last_die->tag == DW_TAG_subprogram
18435 || last_die->tag == DW_TAG_lexical_block))))
18436 {
18437 nesting_level++;
18438 parent_die = last_die;
18439 continue;
18440 }
18441
18442 /* Otherwise we skip to the next sibling, if any. */
18443 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18444
18445 /* Back to the top, do it again. */
18446 }
18447 }
18448
18449 partial_die_info::partial_die_info (sect_offset sect_off_,
18450 struct abbrev_info *abbrev)
18451 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18452 {
18453 }
18454
18455 /* Read a minimal amount of information into the minimal die structure.
18456 INFO_PTR should point just after the initial uleb128 of a DIE. */
18457
18458 const gdb_byte *
18459 partial_die_info::read (const struct die_reader_specs *reader,
18460 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18461 {
18462 struct dwarf2_cu *cu = reader->cu;
18463 struct dwarf2_per_objfile *dwarf2_per_objfile
18464 = cu->per_cu->dwarf2_per_objfile;
18465 unsigned int i;
18466 int has_low_pc_attr = 0;
18467 int has_high_pc_attr = 0;
18468 int high_pc_relative = 0;
18469
18470 for (i = 0; i < abbrev.num_attrs; ++i)
18471 {
18472 struct attribute attr;
18473
18474 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
18475
18476 /* Store the data if it is of an attribute we want to keep in a
18477 partial symbol table. */
18478 switch (attr.name)
18479 {
18480 case DW_AT_name:
18481 switch (tag)
18482 {
18483 case DW_TAG_compile_unit:
18484 case DW_TAG_partial_unit:
18485 case DW_TAG_type_unit:
18486 /* Compilation units have a DW_AT_name that is a filename, not
18487 a source language identifier. */
18488 case DW_TAG_enumeration_type:
18489 case DW_TAG_enumerator:
18490 /* These tags always have simple identifiers already; no need
18491 to canonicalize them. */
18492 name = DW_STRING (&attr);
18493 break;
18494 default:
18495 {
18496 struct objfile *objfile = dwarf2_per_objfile->objfile;
18497
18498 name
18499 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18500 &objfile->per_bfd->storage_obstack);
18501 }
18502 break;
18503 }
18504 break;
18505 case DW_AT_linkage_name:
18506 case DW_AT_MIPS_linkage_name:
18507 /* Note that both forms of linkage name might appear. We
18508 assume they will be the same, and we only store the last
18509 one we see. */
18510 if (cu->language == language_ada)
18511 name = DW_STRING (&attr);
18512 linkage_name = DW_STRING (&attr);
18513 break;
18514 case DW_AT_low_pc:
18515 has_low_pc_attr = 1;
18516 lowpc = attr_value_as_address (&attr);
18517 break;
18518 case DW_AT_high_pc:
18519 has_high_pc_attr = 1;
18520 highpc = attr_value_as_address (&attr);
18521 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18522 high_pc_relative = 1;
18523 break;
18524 case DW_AT_location:
18525 /* Support the .debug_loc offsets. */
18526 if (attr_form_is_block (&attr))
18527 {
18528 d.locdesc = DW_BLOCK (&attr);
18529 }
18530 else if (attr_form_is_section_offset (&attr))
18531 {
18532 dwarf2_complex_location_expr_complaint ();
18533 }
18534 else
18535 {
18536 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18537 "partial symbol information");
18538 }
18539 break;
18540 case DW_AT_external:
18541 is_external = DW_UNSND (&attr);
18542 break;
18543 case DW_AT_declaration:
18544 is_declaration = DW_UNSND (&attr);
18545 break;
18546 case DW_AT_type:
18547 has_type = 1;
18548 break;
18549 case DW_AT_abstract_origin:
18550 case DW_AT_specification:
18551 case DW_AT_extension:
18552 has_specification = 1;
18553 spec_offset = dwarf2_get_ref_die_offset (&attr);
18554 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18555 || cu->per_cu->is_dwz);
18556 break;
18557 case DW_AT_sibling:
18558 /* Ignore absolute siblings, they might point outside of
18559 the current compile unit. */
18560 if (attr.form == DW_FORM_ref_addr)
18561 complaint (&symfile_complaints,
18562 _("ignoring absolute DW_AT_sibling"));
18563 else
18564 {
18565 const gdb_byte *buffer = reader->buffer;
18566 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18567 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18568
18569 if (sibling_ptr < info_ptr)
18570 complaint (&symfile_complaints,
18571 _("DW_AT_sibling points backwards"));
18572 else if (sibling_ptr > reader->buffer_end)
18573 dwarf2_section_buffer_overflow_complaint (reader->die_section);
18574 else
18575 sibling = sibling_ptr;
18576 }
18577 break;
18578 case DW_AT_byte_size:
18579 has_byte_size = 1;
18580 break;
18581 case DW_AT_const_value:
18582 has_const_value = 1;
18583 break;
18584 case DW_AT_calling_convention:
18585 /* DWARF doesn't provide a way to identify a program's source-level
18586 entry point. DW_AT_calling_convention attributes are only meant
18587 to describe functions' calling conventions.
18588
18589 However, because it's a necessary piece of information in
18590 Fortran, and before DWARF 4 DW_CC_program was the only
18591 piece of debugging information whose definition refers to
18592 a 'main program' at all, several compilers marked Fortran
18593 main programs with DW_CC_program --- even when those
18594 functions use the standard calling conventions.
18595
18596 Although DWARF now specifies a way to provide this
18597 information, we support this practice for backward
18598 compatibility. */
18599 if (DW_UNSND (&attr) == DW_CC_program
18600 && cu->language == language_fortran)
18601 main_subprogram = 1;
18602 break;
18603 case DW_AT_inline:
18604 if (DW_UNSND (&attr) == DW_INL_inlined
18605 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18606 may_be_inlined = 1;
18607 break;
18608
18609 case DW_AT_import:
18610 if (tag == DW_TAG_imported_unit)
18611 {
18612 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18613 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18614 || cu->per_cu->is_dwz);
18615 }
18616 break;
18617
18618 case DW_AT_main_subprogram:
18619 main_subprogram = DW_UNSND (&attr);
18620 break;
18621
18622 default:
18623 break;
18624 }
18625 }
18626
18627 if (high_pc_relative)
18628 highpc += lowpc;
18629
18630 if (has_low_pc_attr && has_high_pc_attr)
18631 {
18632 /* When using the GNU linker, .gnu.linkonce. sections are used to
18633 eliminate duplicate copies of functions and vtables and such.
18634 The linker will arbitrarily choose one and discard the others.
18635 The AT_*_pc values for such functions refer to local labels in
18636 these sections. If the section from that file was discarded, the
18637 labels are not in the output, so the relocs get a value of 0.
18638 If this is a discarded function, mark the pc bounds as invalid,
18639 so that GDB will ignore it. */
18640 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18641 {
18642 struct objfile *objfile = dwarf2_per_objfile->objfile;
18643 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18644
18645 complaint (&symfile_complaints,
18646 _("DW_AT_low_pc %s is zero "
18647 "for DIE at %s [in module %s]"),
18648 paddress (gdbarch, lowpc),
18649 sect_offset_str (sect_off),
18650 objfile_name (objfile));
18651 }
18652 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18653 else if (lowpc >= highpc)
18654 {
18655 struct objfile *objfile = dwarf2_per_objfile->objfile;
18656 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18657
18658 complaint (&symfile_complaints,
18659 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18660 "for DIE at %s [in module %s]"),
18661 paddress (gdbarch, lowpc),
18662 paddress (gdbarch, highpc),
18663 sect_offset_str (sect_off),
18664 objfile_name (objfile));
18665 }
18666 else
18667 has_pc_info = 1;
18668 }
18669
18670 return info_ptr;
18671 }
18672
18673 /* Find a cached partial DIE at OFFSET in CU. */
18674
18675 struct partial_die_info *
18676 dwarf2_cu::find_partial_die (sect_offset sect_off)
18677 {
18678 struct partial_die_info *lookup_die = NULL;
18679 struct partial_die_info part_die (sect_off);
18680
18681 lookup_die = ((struct partial_die_info *)
18682 htab_find_with_hash (partial_dies, &part_die,
18683 to_underlying (sect_off)));
18684
18685 return lookup_die;
18686 }
18687
18688 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18689 except in the case of .debug_types DIEs which do not reference
18690 outside their CU (they do however referencing other types via
18691 DW_FORM_ref_sig8). */
18692
18693 static struct partial_die_info *
18694 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18695 {
18696 struct dwarf2_per_objfile *dwarf2_per_objfile
18697 = cu->per_cu->dwarf2_per_objfile;
18698 struct objfile *objfile = dwarf2_per_objfile->objfile;
18699 struct dwarf2_per_cu_data *per_cu = NULL;
18700 struct partial_die_info *pd = NULL;
18701
18702 if (offset_in_dwz == cu->per_cu->is_dwz
18703 && offset_in_cu_p (&cu->header, sect_off))
18704 {
18705 pd = cu->find_partial_die (sect_off);
18706 if (pd != NULL)
18707 return pd;
18708 /* We missed recording what we needed.
18709 Load all dies and try again. */
18710 per_cu = cu->per_cu;
18711 }
18712 else
18713 {
18714 /* TUs don't reference other CUs/TUs (except via type signatures). */
18715 if (cu->per_cu->is_debug_types)
18716 {
18717 error (_("Dwarf Error: Type Unit at offset %s contains"
18718 " external reference to offset %s [in module %s].\n"),
18719 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18720 bfd_get_filename (objfile->obfd));
18721 }
18722 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18723 dwarf2_per_objfile);
18724
18725 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18726 load_partial_comp_unit (per_cu);
18727
18728 per_cu->cu->last_used = 0;
18729 pd = per_cu->cu->find_partial_die (sect_off);
18730 }
18731
18732 /* If we didn't find it, and not all dies have been loaded,
18733 load them all and try again. */
18734
18735 if (pd == NULL && per_cu->load_all_dies == 0)
18736 {
18737 per_cu->load_all_dies = 1;
18738
18739 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18740 THIS_CU->cu may already be in use. So we can't just free it and
18741 replace its DIEs with the ones we read in. Instead, we leave those
18742 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18743 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18744 set. */
18745 load_partial_comp_unit (per_cu);
18746
18747 pd = per_cu->cu->find_partial_die (sect_off);
18748 }
18749
18750 if (pd == NULL)
18751 internal_error (__FILE__, __LINE__,
18752 _("could not find partial DIE %s "
18753 "in cache [from module %s]\n"),
18754 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18755 return pd;
18756 }
18757
18758 /* See if we can figure out if the class lives in a namespace. We do
18759 this by looking for a member function; its demangled name will
18760 contain namespace info, if there is any. */
18761
18762 static void
18763 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18764 struct dwarf2_cu *cu)
18765 {
18766 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18767 what template types look like, because the demangler
18768 frequently doesn't give the same name as the debug info. We
18769 could fix this by only using the demangled name to get the
18770 prefix (but see comment in read_structure_type). */
18771
18772 struct partial_die_info *real_pdi;
18773 struct partial_die_info *child_pdi;
18774
18775 /* If this DIE (this DIE's specification, if any) has a parent, then
18776 we should not do this. We'll prepend the parent's fully qualified
18777 name when we create the partial symbol. */
18778
18779 real_pdi = struct_pdi;
18780 while (real_pdi->has_specification)
18781 real_pdi = find_partial_die (real_pdi->spec_offset,
18782 real_pdi->spec_is_dwz, cu);
18783
18784 if (real_pdi->die_parent != NULL)
18785 return;
18786
18787 for (child_pdi = struct_pdi->die_child;
18788 child_pdi != NULL;
18789 child_pdi = child_pdi->die_sibling)
18790 {
18791 if (child_pdi->tag == DW_TAG_subprogram
18792 && child_pdi->linkage_name != NULL)
18793 {
18794 char *actual_class_name
18795 = language_class_name_from_physname (cu->language_defn,
18796 child_pdi->linkage_name);
18797 if (actual_class_name != NULL)
18798 {
18799 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18800 struct_pdi->name
18801 = ((const char *)
18802 obstack_copy0 (&objfile->per_bfd->storage_obstack,
18803 actual_class_name,
18804 strlen (actual_class_name)));
18805 xfree (actual_class_name);
18806 }
18807 break;
18808 }
18809 }
18810 }
18811
18812 void
18813 partial_die_info::fixup (struct dwarf2_cu *cu)
18814 {
18815 /* Once we've fixed up a die, there's no point in doing so again.
18816 This also avoids a memory leak if we were to call
18817 guess_partial_die_structure_name multiple times. */
18818 if (fixup_called)
18819 return;
18820
18821 /* If we found a reference attribute and the DIE has no name, try
18822 to find a name in the referred to DIE. */
18823
18824 if (name == NULL && has_specification)
18825 {
18826 struct partial_die_info *spec_die;
18827
18828 spec_die = find_partial_die (spec_offset, spec_is_dwz, cu);
18829
18830 spec_die->fixup (cu);
18831
18832 if (spec_die->name)
18833 {
18834 name = spec_die->name;
18835
18836 /* Copy DW_AT_external attribute if it is set. */
18837 if (spec_die->is_external)
18838 is_external = spec_die->is_external;
18839 }
18840 }
18841
18842 /* Set default names for some unnamed DIEs. */
18843
18844 if (name == NULL && tag == DW_TAG_namespace)
18845 name = CP_ANONYMOUS_NAMESPACE_STR;
18846
18847 /* If there is no parent die to provide a namespace, and there are
18848 children, see if we can determine the namespace from their linkage
18849 name. */
18850 if (cu->language == language_cplus
18851 && !VEC_empty (dwarf2_section_info_def,
18852 cu->per_cu->dwarf2_per_objfile->types)
18853 && die_parent == NULL
18854 && has_children
18855 && (tag == DW_TAG_class_type
18856 || tag == DW_TAG_structure_type
18857 || tag == DW_TAG_union_type))
18858 guess_partial_die_structure_name (this, cu);
18859
18860 /* GCC might emit a nameless struct or union that has a linkage
18861 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18862 if (name == NULL
18863 && (tag == DW_TAG_class_type
18864 || tag == DW_TAG_interface_type
18865 || tag == DW_TAG_structure_type
18866 || tag == DW_TAG_union_type)
18867 && linkage_name != NULL)
18868 {
18869 char *demangled;
18870
18871 demangled = gdb_demangle (linkage_name, DMGL_TYPES);
18872 if (demangled)
18873 {
18874 const char *base;
18875
18876 /* Strip any leading namespaces/classes, keep only the base name.
18877 DW_AT_name for named DIEs does not contain the prefixes. */
18878 base = strrchr (demangled, ':');
18879 if (base && base > demangled && base[-1] == ':')
18880 base++;
18881 else
18882 base = demangled;
18883
18884 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18885 name
18886 = ((const char *)
18887 obstack_copy0 (&objfile->per_bfd->storage_obstack,
18888 base, strlen (base)));
18889 xfree (demangled);
18890 }
18891 }
18892
18893 fixup_called = 1;
18894 }
18895
18896 /* Read an attribute value described by an attribute form. */
18897
18898 static const gdb_byte *
18899 read_attribute_value (const struct die_reader_specs *reader,
18900 struct attribute *attr, unsigned form,
18901 LONGEST implicit_const, const gdb_byte *info_ptr)
18902 {
18903 struct dwarf2_cu *cu = reader->cu;
18904 struct dwarf2_per_objfile *dwarf2_per_objfile
18905 = cu->per_cu->dwarf2_per_objfile;
18906 struct objfile *objfile = dwarf2_per_objfile->objfile;
18907 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18908 bfd *abfd = reader->abfd;
18909 struct comp_unit_head *cu_header = &cu->header;
18910 unsigned int bytes_read;
18911 struct dwarf_block *blk;
18912
18913 attr->form = (enum dwarf_form) form;
18914 switch (form)
18915 {
18916 case DW_FORM_ref_addr:
18917 if (cu->header.version == 2)
18918 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
18919 else
18920 DW_UNSND (attr) = read_offset (abfd, info_ptr,
18921 &cu->header, &bytes_read);
18922 info_ptr += bytes_read;
18923 break;
18924 case DW_FORM_GNU_ref_alt:
18925 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18926 info_ptr += bytes_read;
18927 break;
18928 case DW_FORM_addr:
18929 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
18930 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
18931 info_ptr += bytes_read;
18932 break;
18933 case DW_FORM_block2:
18934 blk = dwarf_alloc_block (cu);
18935 blk->size = read_2_bytes (abfd, info_ptr);
18936 info_ptr += 2;
18937 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18938 info_ptr += blk->size;
18939 DW_BLOCK (attr) = blk;
18940 break;
18941 case DW_FORM_block4:
18942 blk = dwarf_alloc_block (cu);
18943 blk->size = read_4_bytes (abfd, info_ptr);
18944 info_ptr += 4;
18945 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18946 info_ptr += blk->size;
18947 DW_BLOCK (attr) = blk;
18948 break;
18949 case DW_FORM_data2:
18950 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18951 info_ptr += 2;
18952 break;
18953 case DW_FORM_data4:
18954 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18955 info_ptr += 4;
18956 break;
18957 case DW_FORM_data8:
18958 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18959 info_ptr += 8;
18960 break;
18961 case DW_FORM_data16:
18962 blk = dwarf_alloc_block (cu);
18963 blk->size = 16;
18964 blk->data = read_n_bytes (abfd, info_ptr, 16);
18965 info_ptr += 16;
18966 DW_BLOCK (attr) = blk;
18967 break;
18968 case DW_FORM_sec_offset:
18969 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18970 info_ptr += bytes_read;
18971 break;
18972 case DW_FORM_string:
18973 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
18974 DW_STRING_IS_CANONICAL (attr) = 0;
18975 info_ptr += bytes_read;
18976 break;
18977 case DW_FORM_strp:
18978 if (!cu->per_cu->is_dwz)
18979 {
18980 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18981 abfd, info_ptr, cu_header,
18982 &bytes_read);
18983 DW_STRING_IS_CANONICAL (attr) = 0;
18984 info_ptr += bytes_read;
18985 break;
18986 }
18987 /* FALLTHROUGH */
18988 case DW_FORM_line_strp:
18989 if (!cu->per_cu->is_dwz)
18990 {
18991 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
18992 abfd, info_ptr,
18993 cu_header, &bytes_read);
18994 DW_STRING_IS_CANONICAL (attr) = 0;
18995 info_ptr += bytes_read;
18996 break;
18997 }
18998 /* FALLTHROUGH */
18999 case DW_FORM_GNU_strp_alt:
19000 {
19001 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19002 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19003 &bytes_read);
19004
19005 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19006 dwz, str_offset);
19007 DW_STRING_IS_CANONICAL (attr) = 0;
19008 info_ptr += bytes_read;
19009 }
19010 break;
19011 case DW_FORM_exprloc:
19012 case DW_FORM_block:
19013 blk = dwarf_alloc_block (cu);
19014 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19015 info_ptr += bytes_read;
19016 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19017 info_ptr += blk->size;
19018 DW_BLOCK (attr) = blk;
19019 break;
19020 case DW_FORM_block1:
19021 blk = dwarf_alloc_block (cu);
19022 blk->size = read_1_byte (abfd, info_ptr);
19023 info_ptr += 1;
19024 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19025 info_ptr += blk->size;
19026 DW_BLOCK (attr) = blk;
19027 break;
19028 case DW_FORM_data1:
19029 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19030 info_ptr += 1;
19031 break;
19032 case DW_FORM_flag:
19033 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19034 info_ptr += 1;
19035 break;
19036 case DW_FORM_flag_present:
19037 DW_UNSND (attr) = 1;
19038 break;
19039 case DW_FORM_sdata:
19040 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19041 info_ptr += bytes_read;
19042 break;
19043 case DW_FORM_udata:
19044 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19045 info_ptr += bytes_read;
19046 break;
19047 case DW_FORM_ref1:
19048 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19049 + read_1_byte (abfd, info_ptr));
19050 info_ptr += 1;
19051 break;
19052 case DW_FORM_ref2:
19053 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19054 + read_2_bytes (abfd, info_ptr));
19055 info_ptr += 2;
19056 break;
19057 case DW_FORM_ref4:
19058 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19059 + read_4_bytes (abfd, info_ptr));
19060 info_ptr += 4;
19061 break;
19062 case DW_FORM_ref8:
19063 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19064 + read_8_bytes (abfd, info_ptr));
19065 info_ptr += 8;
19066 break;
19067 case DW_FORM_ref_sig8:
19068 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19069 info_ptr += 8;
19070 break;
19071 case DW_FORM_ref_udata:
19072 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19073 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19074 info_ptr += bytes_read;
19075 break;
19076 case DW_FORM_indirect:
19077 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19078 info_ptr += bytes_read;
19079 if (form == DW_FORM_implicit_const)
19080 {
19081 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19082 info_ptr += bytes_read;
19083 }
19084 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19085 info_ptr);
19086 break;
19087 case DW_FORM_implicit_const:
19088 DW_SND (attr) = implicit_const;
19089 break;
19090 case DW_FORM_GNU_addr_index:
19091 if (reader->dwo_file == NULL)
19092 {
19093 /* For now flag a hard error.
19094 Later we can turn this into a complaint. */
19095 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19096 dwarf_form_name (form),
19097 bfd_get_filename (abfd));
19098 }
19099 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19100 info_ptr += bytes_read;
19101 break;
19102 case DW_FORM_GNU_str_index:
19103 if (reader->dwo_file == NULL)
19104 {
19105 /* For now flag a hard error.
19106 Later we can turn this into a complaint if warranted. */
19107 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19108 dwarf_form_name (form),
19109 bfd_get_filename (abfd));
19110 }
19111 {
19112 ULONGEST str_index =
19113 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19114
19115 DW_STRING (attr) = read_str_index (reader, str_index);
19116 DW_STRING_IS_CANONICAL (attr) = 0;
19117 info_ptr += bytes_read;
19118 }
19119 break;
19120 default:
19121 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19122 dwarf_form_name (form),
19123 bfd_get_filename (abfd));
19124 }
19125
19126 /* Super hack. */
19127 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
19128 attr->form = DW_FORM_GNU_ref_alt;
19129
19130 /* We have seen instances where the compiler tried to emit a byte
19131 size attribute of -1 which ended up being encoded as an unsigned
19132 0xffffffff. Although 0xffffffff is technically a valid size value,
19133 an object of this size seems pretty unlikely so we can relatively
19134 safely treat these cases as if the size attribute was invalid and
19135 treat them as zero by default. */
19136 if (attr->name == DW_AT_byte_size
19137 && form == DW_FORM_data4
19138 && DW_UNSND (attr) >= 0xffffffff)
19139 {
19140 complaint
19141 (&symfile_complaints,
19142 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19143 hex_string (DW_UNSND (attr)));
19144 DW_UNSND (attr) = 0;
19145 }
19146
19147 return info_ptr;
19148 }
19149
19150 /* Read an attribute described by an abbreviated attribute. */
19151
19152 static const gdb_byte *
19153 read_attribute (const struct die_reader_specs *reader,
19154 struct attribute *attr, struct attr_abbrev *abbrev,
19155 const gdb_byte *info_ptr)
19156 {
19157 attr->name = abbrev->name;
19158 return read_attribute_value (reader, attr, abbrev->form,
19159 abbrev->implicit_const, info_ptr);
19160 }
19161
19162 /* Read dwarf information from a buffer. */
19163
19164 static unsigned int
19165 read_1_byte (bfd *abfd, const gdb_byte *buf)
19166 {
19167 return bfd_get_8 (abfd, buf);
19168 }
19169
19170 static int
19171 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
19172 {
19173 return bfd_get_signed_8 (abfd, buf);
19174 }
19175
19176 static unsigned int
19177 read_2_bytes (bfd *abfd, const gdb_byte *buf)
19178 {
19179 return bfd_get_16 (abfd, buf);
19180 }
19181
19182 static int
19183 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
19184 {
19185 return bfd_get_signed_16 (abfd, buf);
19186 }
19187
19188 static unsigned int
19189 read_4_bytes (bfd *abfd, const gdb_byte *buf)
19190 {
19191 return bfd_get_32 (abfd, buf);
19192 }
19193
19194 static int
19195 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
19196 {
19197 return bfd_get_signed_32 (abfd, buf);
19198 }
19199
19200 static ULONGEST
19201 read_8_bytes (bfd *abfd, const gdb_byte *buf)
19202 {
19203 return bfd_get_64 (abfd, buf);
19204 }
19205
19206 static CORE_ADDR
19207 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
19208 unsigned int *bytes_read)
19209 {
19210 struct comp_unit_head *cu_header = &cu->header;
19211 CORE_ADDR retval = 0;
19212
19213 if (cu_header->signed_addr_p)
19214 {
19215 switch (cu_header->addr_size)
19216 {
19217 case 2:
19218 retval = bfd_get_signed_16 (abfd, buf);
19219 break;
19220 case 4:
19221 retval = bfd_get_signed_32 (abfd, buf);
19222 break;
19223 case 8:
19224 retval = bfd_get_signed_64 (abfd, buf);
19225 break;
19226 default:
19227 internal_error (__FILE__, __LINE__,
19228 _("read_address: bad switch, signed [in module %s]"),
19229 bfd_get_filename (abfd));
19230 }
19231 }
19232 else
19233 {
19234 switch (cu_header->addr_size)
19235 {
19236 case 2:
19237 retval = bfd_get_16 (abfd, buf);
19238 break;
19239 case 4:
19240 retval = bfd_get_32 (abfd, buf);
19241 break;
19242 case 8:
19243 retval = bfd_get_64 (abfd, buf);
19244 break;
19245 default:
19246 internal_error (__FILE__, __LINE__,
19247 _("read_address: bad switch, "
19248 "unsigned [in module %s]"),
19249 bfd_get_filename (abfd));
19250 }
19251 }
19252
19253 *bytes_read = cu_header->addr_size;
19254 return retval;
19255 }
19256
19257 /* Read the initial length from a section. The (draft) DWARF 3
19258 specification allows the initial length to take up either 4 bytes
19259 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19260 bytes describe the length and all offsets will be 8 bytes in length
19261 instead of 4.
19262
19263 An older, non-standard 64-bit format is also handled by this
19264 function. The older format in question stores the initial length
19265 as an 8-byte quantity without an escape value. Lengths greater
19266 than 2^32 aren't very common which means that the initial 4 bytes
19267 is almost always zero. Since a length value of zero doesn't make
19268 sense for the 32-bit format, this initial zero can be considered to
19269 be an escape value which indicates the presence of the older 64-bit
19270 format. As written, the code can't detect (old format) lengths
19271 greater than 4GB. If it becomes necessary to handle lengths
19272 somewhat larger than 4GB, we could allow other small values (such
19273 as the non-sensical values of 1, 2, and 3) to also be used as
19274 escape values indicating the presence of the old format.
19275
19276 The value returned via bytes_read should be used to increment the
19277 relevant pointer after calling read_initial_length().
19278
19279 [ Note: read_initial_length() and read_offset() are based on the
19280 document entitled "DWARF Debugging Information Format", revision
19281 3, draft 8, dated November 19, 2001. This document was obtained
19282 from:
19283
19284 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
19285
19286 This document is only a draft and is subject to change. (So beware.)
19287
19288 Details regarding the older, non-standard 64-bit format were
19289 determined empirically by examining 64-bit ELF files produced by
19290 the SGI toolchain on an IRIX 6.5 machine.
19291
19292 - Kevin, July 16, 2002
19293 ] */
19294
19295 static LONGEST
19296 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
19297 {
19298 LONGEST length = bfd_get_32 (abfd, buf);
19299
19300 if (length == 0xffffffff)
19301 {
19302 length = bfd_get_64 (abfd, buf + 4);
19303 *bytes_read = 12;
19304 }
19305 else if (length == 0)
19306 {
19307 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
19308 length = bfd_get_64 (abfd, buf);
19309 *bytes_read = 8;
19310 }
19311 else
19312 {
19313 *bytes_read = 4;
19314 }
19315
19316 return length;
19317 }
19318
19319 /* Cover function for read_initial_length.
19320 Returns the length of the object at BUF, and stores the size of the
19321 initial length in *BYTES_READ and stores the size that offsets will be in
19322 *OFFSET_SIZE.
19323 If the initial length size is not equivalent to that specified in
19324 CU_HEADER then issue a complaint.
19325 This is useful when reading non-comp-unit headers. */
19326
19327 static LONGEST
19328 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
19329 const struct comp_unit_head *cu_header,
19330 unsigned int *bytes_read,
19331 unsigned int *offset_size)
19332 {
19333 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19334
19335 gdb_assert (cu_header->initial_length_size == 4
19336 || cu_header->initial_length_size == 8
19337 || cu_header->initial_length_size == 12);
19338
19339 if (cu_header->initial_length_size != *bytes_read)
19340 complaint (&symfile_complaints,
19341 _("intermixed 32-bit and 64-bit DWARF sections"));
19342
19343 *offset_size = (*bytes_read == 4) ? 4 : 8;
19344 return length;
19345 }
19346
19347 /* Read an offset from the data stream. The size of the offset is
19348 given by cu_header->offset_size. */
19349
19350 static LONGEST
19351 read_offset (bfd *abfd, const gdb_byte *buf,
19352 const struct comp_unit_head *cu_header,
19353 unsigned int *bytes_read)
19354 {
19355 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
19356
19357 *bytes_read = cu_header->offset_size;
19358 return offset;
19359 }
19360
19361 /* Read an offset from the data stream. */
19362
19363 static LONGEST
19364 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
19365 {
19366 LONGEST retval = 0;
19367
19368 switch (offset_size)
19369 {
19370 case 4:
19371 retval = bfd_get_32 (abfd, buf);
19372 break;
19373 case 8:
19374 retval = bfd_get_64 (abfd, buf);
19375 break;
19376 default:
19377 internal_error (__FILE__, __LINE__,
19378 _("read_offset_1: bad switch [in module %s]"),
19379 bfd_get_filename (abfd));
19380 }
19381
19382 return retval;
19383 }
19384
19385 static const gdb_byte *
19386 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
19387 {
19388 /* If the size of a host char is 8 bits, we can return a pointer
19389 to the buffer, otherwise we have to copy the data to a buffer
19390 allocated on the temporary obstack. */
19391 gdb_assert (HOST_CHAR_BIT == 8);
19392 return buf;
19393 }
19394
19395 static const char *
19396 read_direct_string (bfd *abfd, const gdb_byte *buf,
19397 unsigned int *bytes_read_ptr)
19398 {
19399 /* If the size of a host char is 8 bits, we can return a pointer
19400 to the string, otherwise we have to copy the string to a buffer
19401 allocated on the temporary obstack. */
19402 gdb_assert (HOST_CHAR_BIT == 8);
19403 if (*buf == '\0')
19404 {
19405 *bytes_read_ptr = 1;
19406 return NULL;
19407 }
19408 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19409 return (const char *) buf;
19410 }
19411
19412 /* Return pointer to string at section SECT offset STR_OFFSET with error
19413 reporting strings FORM_NAME and SECT_NAME. */
19414
19415 static const char *
19416 read_indirect_string_at_offset_from (struct objfile *objfile,
19417 bfd *abfd, LONGEST str_offset,
19418 struct dwarf2_section_info *sect,
19419 const char *form_name,
19420 const char *sect_name)
19421 {
19422 dwarf2_read_section (objfile, sect);
19423 if (sect->buffer == NULL)
19424 error (_("%s used without %s section [in module %s]"),
19425 form_name, sect_name, bfd_get_filename (abfd));
19426 if (str_offset >= sect->size)
19427 error (_("%s pointing outside of %s section [in module %s]"),
19428 form_name, sect_name, bfd_get_filename (abfd));
19429 gdb_assert (HOST_CHAR_BIT == 8);
19430 if (sect->buffer[str_offset] == '\0')
19431 return NULL;
19432 return (const char *) (sect->buffer + str_offset);
19433 }
19434
19435 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19436
19437 static const char *
19438 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19439 bfd *abfd, LONGEST str_offset)
19440 {
19441 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19442 abfd, str_offset,
19443 &dwarf2_per_objfile->str,
19444 "DW_FORM_strp", ".debug_str");
19445 }
19446
19447 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19448
19449 static const char *
19450 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19451 bfd *abfd, LONGEST str_offset)
19452 {
19453 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19454 abfd, str_offset,
19455 &dwarf2_per_objfile->line_str,
19456 "DW_FORM_line_strp",
19457 ".debug_line_str");
19458 }
19459
19460 /* Read a string at offset STR_OFFSET in the .debug_str section from
19461 the .dwz file DWZ. Throw an error if the offset is too large. If
19462 the string consists of a single NUL byte, return NULL; otherwise
19463 return a pointer to the string. */
19464
19465 static const char *
19466 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19467 LONGEST str_offset)
19468 {
19469 dwarf2_read_section (objfile, &dwz->str);
19470
19471 if (dwz->str.buffer == NULL)
19472 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19473 "section [in module %s]"),
19474 bfd_get_filename (dwz->dwz_bfd));
19475 if (str_offset >= dwz->str.size)
19476 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19477 ".debug_str section [in module %s]"),
19478 bfd_get_filename (dwz->dwz_bfd));
19479 gdb_assert (HOST_CHAR_BIT == 8);
19480 if (dwz->str.buffer[str_offset] == '\0')
19481 return NULL;
19482 return (const char *) (dwz->str.buffer + str_offset);
19483 }
19484
19485 /* Return pointer to string at .debug_str offset as read from BUF.
19486 BUF is assumed to be in a compilation unit described by CU_HEADER.
19487 Return *BYTES_READ_PTR count of bytes read from BUF. */
19488
19489 static const char *
19490 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19491 const gdb_byte *buf,
19492 const struct comp_unit_head *cu_header,
19493 unsigned int *bytes_read_ptr)
19494 {
19495 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19496
19497 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
19498 }
19499
19500 /* Return pointer to string at .debug_line_str offset as read from BUF.
19501 BUF is assumed to be in a compilation unit described by CU_HEADER.
19502 Return *BYTES_READ_PTR count of bytes read from BUF. */
19503
19504 static const char *
19505 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19506 bfd *abfd, const gdb_byte *buf,
19507 const struct comp_unit_head *cu_header,
19508 unsigned int *bytes_read_ptr)
19509 {
19510 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19511
19512 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19513 str_offset);
19514 }
19515
19516 ULONGEST
19517 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
19518 unsigned int *bytes_read_ptr)
19519 {
19520 ULONGEST result;
19521 unsigned int num_read;
19522 int shift;
19523 unsigned char byte;
19524
19525 result = 0;
19526 shift = 0;
19527 num_read = 0;
19528 while (1)
19529 {
19530 byte = bfd_get_8 (abfd, buf);
19531 buf++;
19532 num_read++;
19533 result |= ((ULONGEST) (byte & 127) << shift);
19534 if ((byte & 128) == 0)
19535 {
19536 break;
19537 }
19538 shift += 7;
19539 }
19540 *bytes_read_ptr = num_read;
19541 return result;
19542 }
19543
19544 static LONGEST
19545 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
19546 unsigned int *bytes_read_ptr)
19547 {
19548 LONGEST result;
19549 int shift, num_read;
19550 unsigned char byte;
19551
19552 result = 0;
19553 shift = 0;
19554 num_read = 0;
19555 while (1)
19556 {
19557 byte = bfd_get_8 (abfd, buf);
19558 buf++;
19559 num_read++;
19560 result |= ((LONGEST) (byte & 127) << shift);
19561 shift += 7;
19562 if ((byte & 128) == 0)
19563 {
19564 break;
19565 }
19566 }
19567 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
19568 result |= -(((LONGEST) 1) << shift);
19569 *bytes_read_ptr = num_read;
19570 return result;
19571 }
19572
19573 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19574 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
19575 ADDR_SIZE is the size of addresses from the CU header. */
19576
19577 static CORE_ADDR
19578 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19579 unsigned int addr_index, ULONGEST addr_base, int addr_size)
19580 {
19581 struct objfile *objfile = dwarf2_per_objfile->objfile;
19582 bfd *abfd = objfile->obfd;
19583 const gdb_byte *info_ptr;
19584
19585 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
19586 if (dwarf2_per_objfile->addr.buffer == NULL)
19587 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19588 objfile_name (objfile));
19589 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
19590 error (_("DW_FORM_addr_index pointing outside of "
19591 ".debug_addr section [in module %s]"),
19592 objfile_name (objfile));
19593 info_ptr = (dwarf2_per_objfile->addr.buffer
19594 + addr_base + addr_index * addr_size);
19595 if (addr_size == 4)
19596 return bfd_get_32 (abfd, info_ptr);
19597 else
19598 return bfd_get_64 (abfd, info_ptr);
19599 }
19600
19601 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19602
19603 static CORE_ADDR
19604 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19605 {
19606 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19607 cu->addr_base, cu->header.addr_size);
19608 }
19609
19610 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19611
19612 static CORE_ADDR
19613 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19614 unsigned int *bytes_read)
19615 {
19616 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
19617 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19618
19619 return read_addr_index (cu, addr_index);
19620 }
19621
19622 /* Data structure to pass results from dwarf2_read_addr_index_reader
19623 back to dwarf2_read_addr_index. */
19624
19625 struct dwarf2_read_addr_index_data
19626 {
19627 ULONGEST addr_base;
19628 int addr_size;
19629 };
19630
19631 /* die_reader_func for dwarf2_read_addr_index. */
19632
19633 static void
19634 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
19635 const gdb_byte *info_ptr,
19636 struct die_info *comp_unit_die,
19637 int has_children,
19638 void *data)
19639 {
19640 struct dwarf2_cu *cu = reader->cu;
19641 struct dwarf2_read_addr_index_data *aidata =
19642 (struct dwarf2_read_addr_index_data *) data;
19643
19644 aidata->addr_base = cu->addr_base;
19645 aidata->addr_size = cu->header.addr_size;
19646 }
19647
19648 /* Given an index in .debug_addr, fetch the value.
19649 NOTE: This can be called during dwarf expression evaluation,
19650 long after the debug information has been read, and thus per_cu->cu
19651 may no longer exist. */
19652
19653 CORE_ADDR
19654 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19655 unsigned int addr_index)
19656 {
19657 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
19658 struct dwarf2_cu *cu = per_cu->cu;
19659 ULONGEST addr_base;
19660 int addr_size;
19661
19662 /* We need addr_base and addr_size.
19663 If we don't have PER_CU->cu, we have to get it.
19664 Nasty, but the alternative is storing the needed info in PER_CU,
19665 which at this point doesn't seem justified: it's not clear how frequently
19666 it would get used and it would increase the size of every PER_CU.
19667 Entry points like dwarf2_per_cu_addr_size do a similar thing
19668 so we're not in uncharted territory here.
19669 Alas we need to be a bit more complicated as addr_base is contained
19670 in the DIE.
19671
19672 We don't need to read the entire CU(/TU).
19673 We just need the header and top level die.
19674
19675 IWBN to use the aging mechanism to let us lazily later discard the CU.
19676 For now we skip this optimization. */
19677
19678 if (cu != NULL)
19679 {
19680 addr_base = cu->addr_base;
19681 addr_size = cu->header.addr_size;
19682 }
19683 else
19684 {
19685 struct dwarf2_read_addr_index_data aidata;
19686
19687 /* Note: We can't use init_cutu_and_read_dies_simple here,
19688 we need addr_base. */
19689 init_cutu_and_read_dies (per_cu, NULL, 0, 0, false,
19690 dwarf2_read_addr_index_reader, &aidata);
19691 addr_base = aidata.addr_base;
19692 addr_size = aidata.addr_size;
19693 }
19694
19695 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19696 addr_size);
19697 }
19698
19699 /* Given a DW_FORM_GNU_str_index, fetch the string.
19700 This is only used by the Fission support. */
19701
19702 static const char *
19703 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19704 {
19705 struct dwarf2_cu *cu = reader->cu;
19706 struct dwarf2_per_objfile *dwarf2_per_objfile
19707 = cu->per_cu->dwarf2_per_objfile;
19708 struct objfile *objfile = dwarf2_per_objfile->objfile;
19709 const char *objf_name = objfile_name (objfile);
19710 bfd *abfd = objfile->obfd;
19711 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
19712 struct dwarf2_section_info *str_offsets_section =
19713 &reader->dwo_file->sections.str_offsets;
19714 const gdb_byte *info_ptr;
19715 ULONGEST str_offset;
19716 static const char form_name[] = "DW_FORM_GNU_str_index";
19717
19718 dwarf2_read_section (objfile, str_section);
19719 dwarf2_read_section (objfile, str_offsets_section);
19720 if (str_section->buffer == NULL)
19721 error (_("%s used without .debug_str.dwo section"
19722 " in CU at offset %s [in module %s]"),
19723 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19724 if (str_offsets_section->buffer == NULL)
19725 error (_("%s used without .debug_str_offsets.dwo section"
19726 " in CU at offset %s [in module %s]"),
19727 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19728 if (str_index * cu->header.offset_size >= str_offsets_section->size)
19729 error (_("%s pointing outside of .debug_str_offsets.dwo"
19730 " section in CU at offset %s [in module %s]"),
19731 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19732 info_ptr = (str_offsets_section->buffer
19733 + str_index * cu->header.offset_size);
19734 if (cu->header.offset_size == 4)
19735 str_offset = bfd_get_32 (abfd, info_ptr);
19736 else
19737 str_offset = bfd_get_64 (abfd, info_ptr);
19738 if (str_offset >= str_section->size)
19739 error (_("Offset from %s pointing outside of"
19740 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19741 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19742 return (const char *) (str_section->buffer + str_offset);
19743 }
19744
19745 /* Return the length of an LEB128 number in BUF. */
19746
19747 static int
19748 leb128_size (const gdb_byte *buf)
19749 {
19750 const gdb_byte *begin = buf;
19751 gdb_byte byte;
19752
19753 while (1)
19754 {
19755 byte = *buf++;
19756 if ((byte & 128) == 0)
19757 return buf - begin;
19758 }
19759 }
19760
19761 static void
19762 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19763 {
19764 switch (lang)
19765 {
19766 case DW_LANG_C89:
19767 case DW_LANG_C99:
19768 case DW_LANG_C11:
19769 case DW_LANG_C:
19770 case DW_LANG_UPC:
19771 cu->language = language_c;
19772 break;
19773 case DW_LANG_Java:
19774 case DW_LANG_C_plus_plus:
19775 case DW_LANG_C_plus_plus_11:
19776 case DW_LANG_C_plus_plus_14:
19777 cu->language = language_cplus;
19778 break;
19779 case DW_LANG_D:
19780 cu->language = language_d;
19781 break;
19782 case DW_LANG_Fortran77:
19783 case DW_LANG_Fortran90:
19784 case DW_LANG_Fortran95:
19785 case DW_LANG_Fortran03:
19786 case DW_LANG_Fortran08:
19787 cu->language = language_fortran;
19788 break;
19789 case DW_LANG_Go:
19790 cu->language = language_go;
19791 break;
19792 case DW_LANG_Mips_Assembler:
19793 cu->language = language_asm;
19794 break;
19795 case DW_LANG_Ada83:
19796 case DW_LANG_Ada95:
19797 cu->language = language_ada;
19798 break;
19799 case DW_LANG_Modula2:
19800 cu->language = language_m2;
19801 break;
19802 case DW_LANG_Pascal83:
19803 cu->language = language_pascal;
19804 break;
19805 case DW_LANG_ObjC:
19806 cu->language = language_objc;
19807 break;
19808 case DW_LANG_Rust:
19809 case DW_LANG_Rust_old:
19810 cu->language = language_rust;
19811 break;
19812 case DW_LANG_Cobol74:
19813 case DW_LANG_Cobol85:
19814 default:
19815 cu->language = language_minimal;
19816 break;
19817 }
19818 cu->language_defn = language_def (cu->language);
19819 }
19820
19821 /* Return the named attribute or NULL if not there. */
19822
19823 static struct attribute *
19824 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19825 {
19826 for (;;)
19827 {
19828 unsigned int i;
19829 struct attribute *spec = NULL;
19830
19831 for (i = 0; i < die->num_attrs; ++i)
19832 {
19833 if (die->attrs[i].name == name)
19834 return &die->attrs[i];
19835 if (die->attrs[i].name == DW_AT_specification
19836 || die->attrs[i].name == DW_AT_abstract_origin)
19837 spec = &die->attrs[i];
19838 }
19839
19840 if (!spec)
19841 break;
19842
19843 die = follow_die_ref (die, spec, &cu);
19844 }
19845
19846 return NULL;
19847 }
19848
19849 /* Return the named attribute or NULL if not there,
19850 but do not follow DW_AT_specification, etc.
19851 This is for use in contexts where we're reading .debug_types dies.
19852 Following DW_AT_specification, DW_AT_abstract_origin will take us
19853 back up the chain, and we want to go down. */
19854
19855 static struct attribute *
19856 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
19857 {
19858 unsigned int i;
19859
19860 for (i = 0; i < die->num_attrs; ++i)
19861 if (die->attrs[i].name == name)
19862 return &die->attrs[i];
19863
19864 return NULL;
19865 }
19866
19867 /* Return the string associated with a string-typed attribute, or NULL if it
19868 is either not found or is of an incorrect type. */
19869
19870 static const char *
19871 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19872 {
19873 struct attribute *attr;
19874 const char *str = NULL;
19875
19876 attr = dwarf2_attr (die, name, cu);
19877
19878 if (attr != NULL)
19879 {
19880 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
19881 || attr->form == DW_FORM_string
19882 || attr->form == DW_FORM_GNU_str_index
19883 || attr->form == DW_FORM_GNU_strp_alt)
19884 str = DW_STRING (attr);
19885 else
19886 complaint (&symfile_complaints,
19887 _("string type expected for attribute %s for "
19888 "DIE at %s in module %s"),
19889 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19890 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
19891 }
19892
19893 return str;
19894 }
19895
19896 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19897 and holds a non-zero value. This function should only be used for
19898 DW_FORM_flag or DW_FORM_flag_present attributes. */
19899
19900 static int
19901 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19902 {
19903 struct attribute *attr = dwarf2_attr (die, name, cu);
19904
19905 return (attr && DW_UNSND (attr));
19906 }
19907
19908 static int
19909 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19910 {
19911 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19912 which value is non-zero. However, we have to be careful with
19913 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19914 (via dwarf2_flag_true_p) follows this attribute. So we may
19915 end up accidently finding a declaration attribute that belongs
19916 to a different DIE referenced by the specification attribute,
19917 even though the given DIE does not have a declaration attribute. */
19918 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19919 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19920 }
19921
19922 /* Return the die giving the specification for DIE, if there is
19923 one. *SPEC_CU is the CU containing DIE on input, and the CU
19924 containing the return value on output. If there is no
19925 specification, but there is an abstract origin, that is
19926 returned. */
19927
19928 static struct die_info *
19929 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19930 {
19931 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19932 *spec_cu);
19933
19934 if (spec_attr == NULL)
19935 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19936
19937 if (spec_attr == NULL)
19938 return NULL;
19939 else
19940 return follow_die_ref (die, spec_attr, spec_cu);
19941 }
19942
19943 /* Stub for free_line_header to match void * callback types. */
19944
19945 static void
19946 free_line_header_voidp (void *arg)
19947 {
19948 struct line_header *lh = (struct line_header *) arg;
19949
19950 delete lh;
19951 }
19952
19953 void
19954 line_header::add_include_dir (const char *include_dir)
19955 {
19956 if (dwarf_line_debug >= 2)
19957 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
19958 include_dirs.size () + 1, include_dir);
19959
19960 include_dirs.push_back (include_dir);
19961 }
19962
19963 void
19964 line_header::add_file_name (const char *name,
19965 dir_index d_index,
19966 unsigned int mod_time,
19967 unsigned int length)
19968 {
19969 if (dwarf_line_debug >= 2)
19970 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
19971 (unsigned) file_names.size () + 1, name);
19972
19973 file_names.emplace_back (name, d_index, mod_time, length);
19974 }
19975
19976 /* A convenience function to find the proper .debug_line section for a CU. */
19977
19978 static struct dwarf2_section_info *
19979 get_debug_line_section (struct dwarf2_cu *cu)
19980 {
19981 struct dwarf2_section_info *section;
19982 struct dwarf2_per_objfile *dwarf2_per_objfile
19983 = cu->per_cu->dwarf2_per_objfile;
19984
19985 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19986 DWO file. */
19987 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19988 section = &cu->dwo_unit->dwo_file->sections.line;
19989 else if (cu->per_cu->is_dwz)
19990 {
19991 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19992
19993 section = &dwz->line;
19994 }
19995 else
19996 section = &dwarf2_per_objfile->line;
19997
19998 return section;
19999 }
20000
20001 /* Read directory or file name entry format, starting with byte of
20002 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20003 entries count and the entries themselves in the described entry
20004 format. */
20005
20006 static void
20007 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20008 bfd *abfd, const gdb_byte **bufp,
20009 struct line_header *lh,
20010 const struct comp_unit_head *cu_header,
20011 void (*callback) (struct line_header *lh,
20012 const char *name,
20013 dir_index d_index,
20014 unsigned int mod_time,
20015 unsigned int length))
20016 {
20017 gdb_byte format_count, formati;
20018 ULONGEST data_count, datai;
20019 const gdb_byte *buf = *bufp;
20020 const gdb_byte *format_header_data;
20021 unsigned int bytes_read;
20022
20023 format_count = read_1_byte (abfd, buf);
20024 buf += 1;
20025 format_header_data = buf;
20026 for (formati = 0; formati < format_count; formati++)
20027 {
20028 read_unsigned_leb128 (abfd, buf, &bytes_read);
20029 buf += bytes_read;
20030 read_unsigned_leb128 (abfd, buf, &bytes_read);
20031 buf += bytes_read;
20032 }
20033
20034 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20035 buf += bytes_read;
20036 for (datai = 0; datai < data_count; datai++)
20037 {
20038 const gdb_byte *format = format_header_data;
20039 struct file_entry fe;
20040
20041 for (formati = 0; formati < format_count; formati++)
20042 {
20043 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
20044 format += bytes_read;
20045
20046 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
20047 format += bytes_read;
20048
20049 gdb::optional<const char *> string;
20050 gdb::optional<unsigned int> uint;
20051
20052 switch (form)
20053 {
20054 case DW_FORM_string:
20055 string.emplace (read_direct_string (abfd, buf, &bytes_read));
20056 buf += bytes_read;
20057 break;
20058
20059 case DW_FORM_line_strp:
20060 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20061 abfd, buf,
20062 cu_header,
20063 &bytes_read));
20064 buf += bytes_read;
20065 break;
20066
20067 case DW_FORM_data1:
20068 uint.emplace (read_1_byte (abfd, buf));
20069 buf += 1;
20070 break;
20071
20072 case DW_FORM_data2:
20073 uint.emplace (read_2_bytes (abfd, buf));
20074 buf += 2;
20075 break;
20076
20077 case DW_FORM_data4:
20078 uint.emplace (read_4_bytes (abfd, buf));
20079 buf += 4;
20080 break;
20081
20082 case DW_FORM_data8:
20083 uint.emplace (read_8_bytes (abfd, buf));
20084 buf += 8;
20085 break;
20086
20087 case DW_FORM_udata:
20088 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
20089 buf += bytes_read;
20090 break;
20091
20092 case DW_FORM_block:
20093 /* It is valid only for DW_LNCT_timestamp which is ignored by
20094 current GDB. */
20095 break;
20096 }
20097
20098 switch (content_type)
20099 {
20100 case DW_LNCT_path:
20101 if (string.has_value ())
20102 fe.name = *string;
20103 break;
20104 case DW_LNCT_directory_index:
20105 if (uint.has_value ())
20106 fe.d_index = (dir_index) *uint;
20107 break;
20108 case DW_LNCT_timestamp:
20109 if (uint.has_value ())
20110 fe.mod_time = *uint;
20111 break;
20112 case DW_LNCT_size:
20113 if (uint.has_value ())
20114 fe.length = *uint;
20115 break;
20116 case DW_LNCT_MD5:
20117 break;
20118 default:
20119 complaint (&symfile_complaints,
20120 _("Unknown format content type %s"),
20121 pulongest (content_type));
20122 }
20123 }
20124
20125 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
20126 }
20127
20128 *bufp = buf;
20129 }
20130
20131 /* Read the statement program header starting at OFFSET in
20132 .debug_line, or .debug_line.dwo. Return a pointer
20133 to a struct line_header, allocated using xmalloc.
20134 Returns NULL if there is a problem reading the header, e.g., if it
20135 has a version we don't understand.
20136
20137 NOTE: the strings in the include directory and file name tables of
20138 the returned object point into the dwarf line section buffer,
20139 and must not be freed. */
20140
20141 static line_header_up
20142 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20143 {
20144 const gdb_byte *line_ptr;
20145 unsigned int bytes_read, offset_size;
20146 int i;
20147 const char *cur_dir, *cur_file;
20148 struct dwarf2_section_info *section;
20149 bfd *abfd;
20150 struct dwarf2_per_objfile *dwarf2_per_objfile
20151 = cu->per_cu->dwarf2_per_objfile;
20152
20153 section = get_debug_line_section (cu);
20154 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20155 if (section->buffer == NULL)
20156 {
20157 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20158 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
20159 else
20160 complaint (&symfile_complaints, _("missing .debug_line section"));
20161 return 0;
20162 }
20163
20164 /* We can't do this until we know the section is non-empty.
20165 Only then do we know we have such a section. */
20166 abfd = get_section_bfd_owner (section);
20167
20168 /* Make sure that at least there's room for the total_length field.
20169 That could be 12 bytes long, but we're just going to fudge that. */
20170 if (to_underlying (sect_off) + 4 >= section->size)
20171 {
20172 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20173 return 0;
20174 }
20175
20176 line_header_up lh (new line_header ());
20177
20178 lh->sect_off = sect_off;
20179 lh->offset_in_dwz = cu->per_cu->is_dwz;
20180
20181 line_ptr = section->buffer + to_underlying (sect_off);
20182
20183 /* Read in the header. */
20184 lh->total_length =
20185 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20186 &bytes_read, &offset_size);
20187 line_ptr += bytes_read;
20188 if (line_ptr + lh->total_length > (section->buffer + section->size))
20189 {
20190 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20191 return 0;
20192 }
20193 lh->statement_program_end = line_ptr + lh->total_length;
20194 lh->version = read_2_bytes (abfd, line_ptr);
20195 line_ptr += 2;
20196 if (lh->version > 5)
20197 {
20198 /* This is a version we don't understand. The format could have
20199 changed in ways we don't handle properly so just punt. */
20200 complaint (&symfile_complaints,
20201 _("unsupported version in .debug_line section"));
20202 return NULL;
20203 }
20204 if (lh->version >= 5)
20205 {
20206 gdb_byte segment_selector_size;
20207
20208 /* Skip address size. */
20209 read_1_byte (abfd, line_ptr);
20210 line_ptr += 1;
20211
20212 segment_selector_size = read_1_byte (abfd, line_ptr);
20213 line_ptr += 1;
20214 if (segment_selector_size != 0)
20215 {
20216 complaint (&symfile_complaints,
20217 _("unsupported segment selector size %u "
20218 "in .debug_line section"),
20219 segment_selector_size);
20220 return NULL;
20221 }
20222 }
20223 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20224 line_ptr += offset_size;
20225 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20226 line_ptr += 1;
20227 if (lh->version >= 4)
20228 {
20229 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20230 line_ptr += 1;
20231 }
20232 else
20233 lh->maximum_ops_per_instruction = 1;
20234
20235 if (lh->maximum_ops_per_instruction == 0)
20236 {
20237 lh->maximum_ops_per_instruction = 1;
20238 complaint (&symfile_complaints,
20239 _("invalid maximum_ops_per_instruction "
20240 "in `.debug_line' section"));
20241 }
20242
20243 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20244 line_ptr += 1;
20245 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20246 line_ptr += 1;
20247 lh->line_range = read_1_byte (abfd, line_ptr);
20248 line_ptr += 1;
20249 lh->opcode_base = read_1_byte (abfd, line_ptr);
20250 line_ptr += 1;
20251 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
20252
20253 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20254 for (i = 1; i < lh->opcode_base; ++i)
20255 {
20256 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20257 line_ptr += 1;
20258 }
20259
20260 if (lh->version >= 5)
20261 {
20262 /* Read directory table. */
20263 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20264 &cu->header,
20265 [] (struct line_header *lh, const char *name,
20266 dir_index d_index, unsigned int mod_time,
20267 unsigned int length)
20268 {
20269 lh->add_include_dir (name);
20270 });
20271
20272 /* Read file name table. */
20273 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20274 &cu->header,
20275 [] (struct line_header *lh, const char *name,
20276 dir_index d_index, unsigned int mod_time,
20277 unsigned int length)
20278 {
20279 lh->add_file_name (name, d_index, mod_time, length);
20280 });
20281 }
20282 else
20283 {
20284 /* Read directory table. */
20285 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20286 {
20287 line_ptr += bytes_read;
20288 lh->add_include_dir (cur_dir);
20289 }
20290 line_ptr += bytes_read;
20291
20292 /* Read file name table. */
20293 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20294 {
20295 unsigned int mod_time, length;
20296 dir_index d_index;
20297
20298 line_ptr += bytes_read;
20299 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20300 line_ptr += bytes_read;
20301 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20302 line_ptr += bytes_read;
20303 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20304 line_ptr += bytes_read;
20305
20306 lh->add_file_name (cur_file, d_index, mod_time, length);
20307 }
20308 line_ptr += bytes_read;
20309 }
20310 lh->statement_program_start = line_ptr;
20311
20312 if (line_ptr > (section->buffer + section->size))
20313 complaint (&symfile_complaints,
20314 _("line number info header doesn't "
20315 "fit in `.debug_line' section"));
20316
20317 return lh;
20318 }
20319
20320 /* Subroutine of dwarf_decode_lines to simplify it.
20321 Return the file name of the psymtab for included file FILE_INDEX
20322 in line header LH of PST.
20323 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20324 If space for the result is malloc'd, *NAME_HOLDER will be set.
20325 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20326
20327 static const char *
20328 psymtab_include_file_name (const struct line_header *lh, int file_index,
20329 const struct partial_symtab *pst,
20330 const char *comp_dir,
20331 gdb::unique_xmalloc_ptr<char> *name_holder)
20332 {
20333 const file_entry &fe = lh->file_names[file_index];
20334 const char *include_name = fe.name;
20335 const char *include_name_to_compare = include_name;
20336 const char *pst_filename;
20337 int file_is_pst;
20338
20339 const char *dir_name = fe.include_dir (lh);
20340
20341 gdb::unique_xmalloc_ptr<char> hold_compare;
20342 if (!IS_ABSOLUTE_PATH (include_name)
20343 && (dir_name != NULL || comp_dir != NULL))
20344 {
20345 /* Avoid creating a duplicate psymtab for PST.
20346 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20347 Before we do the comparison, however, we need to account
20348 for DIR_NAME and COMP_DIR.
20349 First prepend dir_name (if non-NULL). If we still don't
20350 have an absolute path prepend comp_dir (if non-NULL).
20351 However, the directory we record in the include-file's
20352 psymtab does not contain COMP_DIR (to match the
20353 corresponding symtab(s)).
20354
20355 Example:
20356
20357 bash$ cd /tmp
20358 bash$ gcc -g ./hello.c
20359 include_name = "hello.c"
20360 dir_name = "."
20361 DW_AT_comp_dir = comp_dir = "/tmp"
20362 DW_AT_name = "./hello.c"
20363
20364 */
20365
20366 if (dir_name != NULL)
20367 {
20368 name_holder->reset (concat (dir_name, SLASH_STRING,
20369 include_name, (char *) NULL));
20370 include_name = name_holder->get ();
20371 include_name_to_compare = include_name;
20372 }
20373 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20374 {
20375 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20376 include_name, (char *) NULL));
20377 include_name_to_compare = hold_compare.get ();
20378 }
20379 }
20380
20381 pst_filename = pst->filename;
20382 gdb::unique_xmalloc_ptr<char> copied_name;
20383 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20384 {
20385 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20386 pst_filename, (char *) NULL));
20387 pst_filename = copied_name.get ();
20388 }
20389
20390 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20391
20392 if (file_is_pst)
20393 return NULL;
20394 return include_name;
20395 }
20396
20397 /* State machine to track the state of the line number program. */
20398
20399 class lnp_state_machine
20400 {
20401 public:
20402 /* Initialize a machine state for the start of a line number
20403 program. */
20404 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
20405
20406 file_entry *current_file ()
20407 {
20408 /* lh->file_names is 0-based, but the file name numbers in the
20409 statement program are 1-based. */
20410 return m_line_header->file_name_at (m_file);
20411 }
20412
20413 /* Record the line in the state machine. END_SEQUENCE is true if
20414 we're processing the end of a sequence. */
20415 void record_line (bool end_sequence);
20416
20417 /* Check address and if invalid nop-out the rest of the lines in this
20418 sequence. */
20419 void check_line_address (struct dwarf2_cu *cu,
20420 const gdb_byte *line_ptr,
20421 CORE_ADDR lowpc, CORE_ADDR address);
20422
20423 void handle_set_discriminator (unsigned int discriminator)
20424 {
20425 m_discriminator = discriminator;
20426 m_line_has_non_zero_discriminator |= discriminator != 0;
20427 }
20428
20429 /* Handle DW_LNE_set_address. */
20430 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20431 {
20432 m_op_index = 0;
20433 address += baseaddr;
20434 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20435 }
20436
20437 /* Handle DW_LNS_advance_pc. */
20438 void handle_advance_pc (CORE_ADDR adjust);
20439
20440 /* Handle a special opcode. */
20441 void handle_special_opcode (unsigned char op_code);
20442
20443 /* Handle DW_LNS_advance_line. */
20444 void handle_advance_line (int line_delta)
20445 {
20446 advance_line (line_delta);
20447 }
20448
20449 /* Handle DW_LNS_set_file. */
20450 void handle_set_file (file_name_index file);
20451
20452 /* Handle DW_LNS_negate_stmt. */
20453 void handle_negate_stmt ()
20454 {
20455 m_is_stmt = !m_is_stmt;
20456 }
20457
20458 /* Handle DW_LNS_const_add_pc. */
20459 void handle_const_add_pc ();
20460
20461 /* Handle DW_LNS_fixed_advance_pc. */
20462 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20463 {
20464 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20465 m_op_index = 0;
20466 }
20467
20468 /* Handle DW_LNS_copy. */
20469 void handle_copy ()
20470 {
20471 record_line (false);
20472 m_discriminator = 0;
20473 }
20474
20475 /* Handle DW_LNE_end_sequence. */
20476 void handle_end_sequence ()
20477 {
20478 m_record_line_callback = ::record_line;
20479 }
20480
20481 private:
20482 /* Advance the line by LINE_DELTA. */
20483 void advance_line (int line_delta)
20484 {
20485 m_line += line_delta;
20486
20487 if (line_delta != 0)
20488 m_line_has_non_zero_discriminator = m_discriminator != 0;
20489 }
20490
20491 gdbarch *m_gdbarch;
20492
20493 /* True if we're recording lines.
20494 Otherwise we're building partial symtabs and are just interested in
20495 finding include files mentioned by the line number program. */
20496 bool m_record_lines_p;
20497
20498 /* The line number header. */
20499 line_header *m_line_header;
20500
20501 /* These are part of the standard DWARF line number state machine,
20502 and initialized according to the DWARF spec. */
20503
20504 unsigned char m_op_index = 0;
20505 /* The line table index (1-based) of the current file. */
20506 file_name_index m_file = (file_name_index) 1;
20507 unsigned int m_line = 1;
20508
20509 /* These are initialized in the constructor. */
20510
20511 CORE_ADDR m_address;
20512 bool m_is_stmt;
20513 unsigned int m_discriminator;
20514
20515 /* Additional bits of state we need to track. */
20516
20517 /* The last file that we called dwarf2_start_subfile for.
20518 This is only used for TLLs. */
20519 unsigned int m_last_file = 0;
20520 /* The last file a line number was recorded for. */
20521 struct subfile *m_last_subfile = NULL;
20522
20523 /* The function to call to record a line. */
20524 record_line_ftype *m_record_line_callback = NULL;
20525
20526 /* The last line number that was recorded, used to coalesce
20527 consecutive entries for the same line. This can happen, for
20528 example, when discriminators are present. PR 17276. */
20529 unsigned int m_last_line = 0;
20530 bool m_line_has_non_zero_discriminator = false;
20531 };
20532
20533 void
20534 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20535 {
20536 CORE_ADDR addr_adj = (((m_op_index + adjust)
20537 / m_line_header->maximum_ops_per_instruction)
20538 * m_line_header->minimum_instruction_length);
20539 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20540 m_op_index = ((m_op_index + adjust)
20541 % m_line_header->maximum_ops_per_instruction);
20542 }
20543
20544 void
20545 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20546 {
20547 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20548 CORE_ADDR addr_adj = (((m_op_index
20549 + (adj_opcode / m_line_header->line_range))
20550 / m_line_header->maximum_ops_per_instruction)
20551 * m_line_header->minimum_instruction_length);
20552 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20553 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20554 % m_line_header->maximum_ops_per_instruction);
20555
20556 int line_delta = (m_line_header->line_base
20557 + (adj_opcode % m_line_header->line_range));
20558 advance_line (line_delta);
20559 record_line (false);
20560 m_discriminator = 0;
20561 }
20562
20563 void
20564 lnp_state_machine::handle_set_file (file_name_index file)
20565 {
20566 m_file = file;
20567
20568 const file_entry *fe = current_file ();
20569 if (fe == NULL)
20570 dwarf2_debug_line_missing_file_complaint ();
20571 else if (m_record_lines_p)
20572 {
20573 const char *dir = fe->include_dir (m_line_header);
20574
20575 m_last_subfile = current_subfile;
20576 m_line_has_non_zero_discriminator = m_discriminator != 0;
20577 dwarf2_start_subfile (fe->name, dir);
20578 }
20579 }
20580
20581 void
20582 lnp_state_machine::handle_const_add_pc ()
20583 {
20584 CORE_ADDR adjust
20585 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20586
20587 CORE_ADDR addr_adj
20588 = (((m_op_index + adjust)
20589 / m_line_header->maximum_ops_per_instruction)
20590 * m_line_header->minimum_instruction_length);
20591
20592 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20593 m_op_index = ((m_op_index + adjust)
20594 % m_line_header->maximum_ops_per_instruction);
20595 }
20596
20597 /* Ignore this record_line request. */
20598
20599 static void
20600 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
20601 {
20602 return;
20603 }
20604
20605 /* Return non-zero if we should add LINE to the line number table.
20606 LINE is the line to add, LAST_LINE is the last line that was added,
20607 LAST_SUBFILE is the subfile for LAST_LINE.
20608 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20609 had a non-zero discriminator.
20610
20611 We have to be careful in the presence of discriminators.
20612 E.g., for this line:
20613
20614 for (i = 0; i < 100000; i++);
20615
20616 clang can emit four line number entries for that one line,
20617 each with a different discriminator.
20618 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20619
20620 However, we want gdb to coalesce all four entries into one.
20621 Otherwise the user could stepi into the middle of the line and
20622 gdb would get confused about whether the pc really was in the
20623 middle of the line.
20624
20625 Things are further complicated by the fact that two consecutive
20626 line number entries for the same line is a heuristic used by gcc
20627 to denote the end of the prologue. So we can't just discard duplicate
20628 entries, we have to be selective about it. The heuristic we use is
20629 that we only collapse consecutive entries for the same line if at least
20630 one of those entries has a non-zero discriminator. PR 17276.
20631
20632 Note: Addresses in the line number state machine can never go backwards
20633 within one sequence, thus this coalescing is ok. */
20634
20635 static int
20636 dwarf_record_line_p (unsigned int line, unsigned int last_line,
20637 int line_has_non_zero_discriminator,
20638 struct subfile *last_subfile)
20639 {
20640 if (current_subfile != last_subfile)
20641 return 1;
20642 if (line != last_line)
20643 return 1;
20644 /* Same line for the same file that we've seen already.
20645 As a last check, for pr 17276, only record the line if the line
20646 has never had a non-zero discriminator. */
20647 if (!line_has_non_zero_discriminator)
20648 return 1;
20649 return 0;
20650 }
20651
20652 /* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
20653 in the line table of subfile SUBFILE. */
20654
20655 static void
20656 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20657 unsigned int line, CORE_ADDR address,
20658 record_line_ftype p_record_line)
20659 {
20660 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20661
20662 if (dwarf_line_debug)
20663 {
20664 fprintf_unfiltered (gdb_stdlog,
20665 "Recording line %u, file %s, address %s\n",
20666 line, lbasename (subfile->name),
20667 paddress (gdbarch, address));
20668 }
20669
20670 (*p_record_line) (subfile, line, addr);
20671 }
20672
20673 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20674 Mark the end of a set of line number records.
20675 The arguments are the same as for dwarf_record_line_1.
20676 If SUBFILE is NULL the request is ignored. */
20677
20678 static void
20679 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20680 CORE_ADDR address, record_line_ftype p_record_line)
20681 {
20682 if (subfile == NULL)
20683 return;
20684
20685 if (dwarf_line_debug)
20686 {
20687 fprintf_unfiltered (gdb_stdlog,
20688 "Finishing current line, file %s, address %s\n",
20689 lbasename (subfile->name),
20690 paddress (gdbarch, address));
20691 }
20692
20693 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
20694 }
20695
20696 void
20697 lnp_state_machine::record_line (bool end_sequence)
20698 {
20699 if (dwarf_line_debug)
20700 {
20701 fprintf_unfiltered (gdb_stdlog,
20702 "Processing actual line %u: file %u,"
20703 " address %s, is_stmt %u, discrim %u\n",
20704 m_line, to_underlying (m_file),
20705 paddress (m_gdbarch, m_address),
20706 m_is_stmt, m_discriminator);
20707 }
20708
20709 file_entry *fe = current_file ();
20710
20711 if (fe == NULL)
20712 dwarf2_debug_line_missing_file_complaint ();
20713 /* For now we ignore lines not starting on an instruction boundary.
20714 But not when processing end_sequence for compatibility with the
20715 previous version of the code. */
20716 else if (m_op_index == 0 || end_sequence)
20717 {
20718 fe->included_p = 1;
20719 if (m_record_lines_p && m_is_stmt)
20720 {
20721 if (m_last_subfile != current_subfile || end_sequence)
20722 {
20723 dwarf_finish_line (m_gdbarch, m_last_subfile,
20724 m_address, m_record_line_callback);
20725 }
20726
20727 if (!end_sequence)
20728 {
20729 if (dwarf_record_line_p (m_line, m_last_line,
20730 m_line_has_non_zero_discriminator,
20731 m_last_subfile))
20732 {
20733 dwarf_record_line_1 (m_gdbarch, current_subfile,
20734 m_line, m_address,
20735 m_record_line_callback);
20736 }
20737 m_last_subfile = current_subfile;
20738 m_last_line = m_line;
20739 }
20740 }
20741 }
20742 }
20743
20744 lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
20745 bool record_lines_p)
20746 {
20747 m_gdbarch = arch;
20748 m_record_lines_p = record_lines_p;
20749 m_line_header = lh;
20750
20751 m_record_line_callback = ::record_line;
20752
20753 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20754 was a line entry for it so that the backend has a chance to adjust it
20755 and also record it in case it needs it. This is currently used by MIPS
20756 code, cf. `mips_adjust_dwarf2_line'. */
20757 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20758 m_is_stmt = lh->default_is_stmt;
20759 m_discriminator = 0;
20760 }
20761
20762 void
20763 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20764 const gdb_byte *line_ptr,
20765 CORE_ADDR lowpc, CORE_ADDR address)
20766 {
20767 /* If address < lowpc then it's not a usable value, it's outside the
20768 pc range of the CU. However, we restrict the test to only address
20769 values of zero to preserve GDB's previous behaviour which is to
20770 handle the specific case of a function being GC'd by the linker. */
20771
20772 if (address == 0 && address < lowpc)
20773 {
20774 /* This line table is for a function which has been
20775 GCd by the linker. Ignore it. PR gdb/12528 */
20776
20777 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20778 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20779
20780 complaint (&symfile_complaints,
20781 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20782 line_offset, objfile_name (objfile));
20783 m_record_line_callback = noop_record_line;
20784 /* Note: record_line_callback is left as noop_record_line until
20785 we see DW_LNE_end_sequence. */
20786 }
20787 }
20788
20789 /* Subroutine of dwarf_decode_lines to simplify it.
20790 Process the line number information in LH.
20791 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20792 program in order to set included_p for every referenced header. */
20793
20794 static void
20795 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20796 const int decode_for_pst_p, CORE_ADDR lowpc)
20797 {
20798 const gdb_byte *line_ptr, *extended_end;
20799 const gdb_byte *line_end;
20800 unsigned int bytes_read, extended_len;
20801 unsigned char op_code, extended_op;
20802 CORE_ADDR baseaddr;
20803 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20804 bfd *abfd = objfile->obfd;
20805 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20806 /* True if we're recording line info (as opposed to building partial
20807 symtabs and just interested in finding include files mentioned by
20808 the line number program). */
20809 bool record_lines_p = !decode_for_pst_p;
20810
20811 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20812
20813 line_ptr = lh->statement_program_start;
20814 line_end = lh->statement_program_end;
20815
20816 /* Read the statement sequences until there's nothing left. */
20817 while (line_ptr < line_end)
20818 {
20819 /* The DWARF line number program state machine. Reset the state
20820 machine at the start of each sequence. */
20821 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
20822 bool end_sequence = false;
20823
20824 if (record_lines_p)
20825 {
20826 /* Start a subfile for the current file of the state
20827 machine. */
20828 const file_entry *fe = state_machine.current_file ();
20829
20830 if (fe != NULL)
20831 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
20832 }
20833
20834 /* Decode the table. */
20835 while (line_ptr < line_end && !end_sequence)
20836 {
20837 op_code = read_1_byte (abfd, line_ptr);
20838 line_ptr += 1;
20839
20840 if (op_code >= lh->opcode_base)
20841 {
20842 /* Special opcode. */
20843 state_machine.handle_special_opcode (op_code);
20844 }
20845 else switch (op_code)
20846 {
20847 case DW_LNS_extended_op:
20848 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20849 &bytes_read);
20850 line_ptr += bytes_read;
20851 extended_end = line_ptr + extended_len;
20852 extended_op = read_1_byte (abfd, line_ptr);
20853 line_ptr += 1;
20854 switch (extended_op)
20855 {
20856 case DW_LNE_end_sequence:
20857 state_machine.handle_end_sequence ();
20858 end_sequence = true;
20859 break;
20860 case DW_LNE_set_address:
20861 {
20862 CORE_ADDR address
20863 = read_address (abfd, line_ptr, cu, &bytes_read);
20864 line_ptr += bytes_read;
20865
20866 state_machine.check_line_address (cu, line_ptr,
20867 lowpc, address);
20868 state_machine.handle_set_address (baseaddr, address);
20869 }
20870 break;
20871 case DW_LNE_define_file:
20872 {
20873 const char *cur_file;
20874 unsigned int mod_time, length;
20875 dir_index dindex;
20876
20877 cur_file = read_direct_string (abfd, line_ptr,
20878 &bytes_read);
20879 line_ptr += bytes_read;
20880 dindex = (dir_index)
20881 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20882 line_ptr += bytes_read;
20883 mod_time =
20884 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20885 line_ptr += bytes_read;
20886 length =
20887 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20888 line_ptr += bytes_read;
20889 lh->add_file_name (cur_file, dindex, mod_time, length);
20890 }
20891 break;
20892 case DW_LNE_set_discriminator:
20893 {
20894 /* The discriminator is not interesting to the
20895 debugger; just ignore it. We still need to
20896 check its value though:
20897 if there are consecutive entries for the same
20898 (non-prologue) line we want to coalesce them.
20899 PR 17276. */
20900 unsigned int discr
20901 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20902 line_ptr += bytes_read;
20903
20904 state_machine.handle_set_discriminator (discr);
20905 }
20906 break;
20907 default:
20908 complaint (&symfile_complaints,
20909 _("mangled .debug_line section"));
20910 return;
20911 }
20912 /* Make sure that we parsed the extended op correctly. If e.g.
20913 we expected a different address size than the producer used,
20914 we may have read the wrong number of bytes. */
20915 if (line_ptr != extended_end)
20916 {
20917 complaint (&symfile_complaints,
20918 _("mangled .debug_line section"));
20919 return;
20920 }
20921 break;
20922 case DW_LNS_copy:
20923 state_machine.handle_copy ();
20924 break;
20925 case DW_LNS_advance_pc:
20926 {
20927 CORE_ADDR adjust
20928 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20929 line_ptr += bytes_read;
20930
20931 state_machine.handle_advance_pc (adjust);
20932 }
20933 break;
20934 case DW_LNS_advance_line:
20935 {
20936 int line_delta
20937 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20938 line_ptr += bytes_read;
20939
20940 state_machine.handle_advance_line (line_delta);
20941 }
20942 break;
20943 case DW_LNS_set_file:
20944 {
20945 file_name_index file
20946 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20947 &bytes_read);
20948 line_ptr += bytes_read;
20949
20950 state_machine.handle_set_file (file);
20951 }
20952 break;
20953 case DW_LNS_set_column:
20954 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20955 line_ptr += bytes_read;
20956 break;
20957 case DW_LNS_negate_stmt:
20958 state_machine.handle_negate_stmt ();
20959 break;
20960 case DW_LNS_set_basic_block:
20961 break;
20962 /* Add to the address register of the state machine the
20963 address increment value corresponding to special opcode
20964 255. I.e., this value is scaled by the minimum
20965 instruction length since special opcode 255 would have
20966 scaled the increment. */
20967 case DW_LNS_const_add_pc:
20968 state_machine.handle_const_add_pc ();
20969 break;
20970 case DW_LNS_fixed_advance_pc:
20971 {
20972 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20973 line_ptr += 2;
20974
20975 state_machine.handle_fixed_advance_pc (addr_adj);
20976 }
20977 break;
20978 default:
20979 {
20980 /* Unknown standard opcode, ignore it. */
20981 int i;
20982
20983 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20984 {
20985 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20986 line_ptr += bytes_read;
20987 }
20988 }
20989 }
20990 }
20991
20992 if (!end_sequence)
20993 dwarf2_debug_line_missing_end_sequence_complaint ();
20994
20995 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20996 in which case we still finish recording the last line). */
20997 state_machine.record_line (true);
20998 }
20999 }
21000
21001 /* Decode the Line Number Program (LNP) for the given line_header
21002 structure and CU. The actual information extracted and the type
21003 of structures created from the LNP depends on the value of PST.
21004
21005 1. If PST is NULL, then this procedure uses the data from the program
21006 to create all necessary symbol tables, and their linetables.
21007
21008 2. If PST is not NULL, this procedure reads the program to determine
21009 the list of files included by the unit represented by PST, and
21010 builds all the associated partial symbol tables.
21011
21012 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21013 It is used for relative paths in the line table.
21014 NOTE: When processing partial symtabs (pst != NULL),
21015 comp_dir == pst->dirname.
21016
21017 NOTE: It is important that psymtabs have the same file name (via strcmp)
21018 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21019 symtab we don't use it in the name of the psymtabs we create.
21020 E.g. expand_line_sal requires this when finding psymtabs to expand.
21021 A good testcase for this is mb-inline.exp.
21022
21023 LOWPC is the lowest address in CU (or 0 if not known).
21024
21025 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21026 for its PC<->lines mapping information. Otherwise only the filename
21027 table is read in. */
21028
21029 static void
21030 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21031 struct dwarf2_cu *cu, struct partial_symtab *pst,
21032 CORE_ADDR lowpc, int decode_mapping)
21033 {
21034 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21035 const int decode_for_pst_p = (pst != NULL);
21036
21037 if (decode_mapping)
21038 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21039
21040 if (decode_for_pst_p)
21041 {
21042 int file_index;
21043
21044 /* Now that we're done scanning the Line Header Program, we can
21045 create the psymtab of each included file. */
21046 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
21047 if (lh->file_names[file_index].included_p == 1)
21048 {
21049 gdb::unique_xmalloc_ptr<char> name_holder;
21050 const char *include_name =
21051 psymtab_include_file_name (lh, file_index, pst, comp_dir,
21052 &name_holder);
21053 if (include_name != NULL)
21054 dwarf2_create_include_psymtab (include_name, pst, objfile);
21055 }
21056 }
21057 else
21058 {
21059 /* Make sure a symtab is created for every file, even files
21060 which contain only variables (i.e. no code with associated
21061 line numbers). */
21062 struct compunit_symtab *cust = buildsym_compunit_symtab ();
21063 int i;
21064
21065 for (i = 0; i < lh->file_names.size (); i++)
21066 {
21067 file_entry &fe = lh->file_names[i];
21068
21069 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
21070
21071 if (current_subfile->symtab == NULL)
21072 {
21073 current_subfile->symtab
21074 = allocate_symtab (cust, current_subfile->name);
21075 }
21076 fe.symtab = current_subfile->symtab;
21077 }
21078 }
21079 }
21080
21081 /* Start a subfile for DWARF. FILENAME is the name of the file and
21082 DIRNAME the name of the source directory which contains FILENAME
21083 or NULL if not known.
21084 This routine tries to keep line numbers from identical absolute and
21085 relative file names in a common subfile.
21086
21087 Using the `list' example from the GDB testsuite, which resides in
21088 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21089 of /srcdir/list0.c yields the following debugging information for list0.c:
21090
21091 DW_AT_name: /srcdir/list0.c
21092 DW_AT_comp_dir: /compdir
21093 files.files[0].name: list0.h
21094 files.files[0].dir: /srcdir
21095 files.files[1].name: list0.c
21096 files.files[1].dir: /srcdir
21097
21098 The line number information for list0.c has to end up in a single
21099 subfile, so that `break /srcdir/list0.c:1' works as expected.
21100 start_subfile will ensure that this happens provided that we pass the
21101 concatenation of files.files[1].dir and files.files[1].name as the
21102 subfile's name. */
21103
21104 static void
21105 dwarf2_start_subfile (const char *filename, const char *dirname)
21106 {
21107 char *copy = NULL;
21108
21109 /* In order not to lose the line information directory,
21110 we concatenate it to the filename when it makes sense.
21111 Note that the Dwarf3 standard says (speaking of filenames in line
21112 information): ``The directory index is ignored for file names
21113 that represent full path names''. Thus ignoring dirname in the
21114 `else' branch below isn't an issue. */
21115
21116 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21117 {
21118 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21119 filename = copy;
21120 }
21121
21122 start_subfile (filename);
21123
21124 if (copy != NULL)
21125 xfree (copy);
21126 }
21127
21128 /* Start a symtab for DWARF.
21129 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
21130
21131 static struct compunit_symtab *
21132 dwarf2_start_symtab (struct dwarf2_cu *cu,
21133 const char *name, const char *comp_dir, CORE_ADDR low_pc)
21134 {
21135 struct compunit_symtab *cust
21136 = start_symtab (cu->per_cu->dwarf2_per_objfile->objfile, name, comp_dir,
21137 low_pc, cu->language);
21138
21139 record_debugformat ("DWARF 2");
21140 record_producer (cu->producer);
21141
21142 /* We assume that we're processing GCC output. */
21143 processing_gcc_compilation = 2;
21144
21145 cu->processing_has_namespace_info = 0;
21146
21147 return cust;
21148 }
21149
21150 static void
21151 var_decode_location (struct attribute *attr, struct symbol *sym,
21152 struct dwarf2_cu *cu)
21153 {
21154 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21155 struct comp_unit_head *cu_header = &cu->header;
21156
21157 /* NOTE drow/2003-01-30: There used to be a comment and some special
21158 code here to turn a symbol with DW_AT_external and a
21159 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21160 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21161 with some versions of binutils) where shared libraries could have
21162 relocations against symbols in their debug information - the
21163 minimal symbol would have the right address, but the debug info
21164 would not. It's no longer necessary, because we will explicitly
21165 apply relocations when we read in the debug information now. */
21166
21167 /* A DW_AT_location attribute with no contents indicates that a
21168 variable has been optimized away. */
21169 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21170 {
21171 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21172 return;
21173 }
21174
21175 /* Handle one degenerate form of location expression specially, to
21176 preserve GDB's previous behavior when section offsets are
21177 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
21178 then mark this symbol as LOC_STATIC. */
21179
21180 if (attr_form_is_block (attr)
21181 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21182 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21183 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21184 && (DW_BLOCK (attr)->size
21185 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
21186 {
21187 unsigned int dummy;
21188
21189 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21190 SYMBOL_VALUE_ADDRESS (sym) =
21191 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
21192 else
21193 SYMBOL_VALUE_ADDRESS (sym) =
21194 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
21195 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21196 fixup_symbol_section (sym, objfile);
21197 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
21198 SYMBOL_SECTION (sym));
21199 return;
21200 }
21201
21202 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21203 expression evaluator, and use LOC_COMPUTED only when necessary
21204 (i.e. when the value of a register or memory location is
21205 referenced, or a thread-local block, etc.). Then again, it might
21206 not be worthwhile. I'm assuming that it isn't unless performance
21207 or memory numbers show me otherwise. */
21208
21209 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21210
21211 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21212 cu->has_loclist = 1;
21213 }
21214
21215 /* Given a pointer to a DWARF information entry, figure out if we need
21216 to make a symbol table entry for it, and if so, create a new entry
21217 and return a pointer to it.
21218 If TYPE is NULL, determine symbol type from the die, otherwise
21219 used the passed type.
21220 If SPACE is not NULL, use it to hold the new symbol. If it is
21221 NULL, allocate a new symbol on the objfile's obstack. */
21222
21223 static struct symbol *
21224 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21225 struct symbol *space)
21226 {
21227 struct dwarf2_per_objfile *dwarf2_per_objfile
21228 = cu->per_cu->dwarf2_per_objfile;
21229 struct objfile *objfile = dwarf2_per_objfile->objfile;
21230 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21231 struct symbol *sym = NULL;
21232 const char *name;
21233 struct attribute *attr = NULL;
21234 struct attribute *attr2 = NULL;
21235 CORE_ADDR baseaddr;
21236 struct pending **list_to_add = NULL;
21237
21238 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21239
21240 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21241
21242 name = dwarf2_name (die, cu);
21243 if (name)
21244 {
21245 const char *linkagename;
21246 int suppress_add = 0;
21247
21248 if (space)
21249 sym = space;
21250 else
21251 sym = allocate_symbol (objfile);
21252 OBJSTAT (objfile, n_syms++);
21253
21254 /* Cache this symbol's name and the name's demangled form (if any). */
21255 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
21256 linkagename = dwarf2_physname (name, die, cu);
21257 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
21258
21259 /* Fortran does not have mangling standard and the mangling does differ
21260 between gfortran, iFort etc. */
21261 if (cu->language == language_fortran
21262 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
21263 symbol_set_demangled_name (&(sym->ginfo),
21264 dwarf2_full_name (name, die, cu),
21265 NULL);
21266
21267 /* Default assumptions.
21268 Use the passed type or decode it from the die. */
21269 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21270 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21271 if (type != NULL)
21272 SYMBOL_TYPE (sym) = type;
21273 else
21274 SYMBOL_TYPE (sym) = die_type (die, cu);
21275 attr = dwarf2_attr (die,
21276 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21277 cu);
21278 if (attr)
21279 {
21280 SYMBOL_LINE (sym) = DW_UNSND (attr);
21281 }
21282
21283 attr = dwarf2_attr (die,
21284 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21285 cu);
21286 if (attr)
21287 {
21288 file_name_index file_index = (file_name_index) DW_UNSND (attr);
21289 struct file_entry *fe;
21290
21291 if (cu->line_header != NULL)
21292 fe = cu->line_header->file_name_at (file_index);
21293 else
21294 fe = NULL;
21295
21296 if (fe == NULL)
21297 complaint (&symfile_complaints,
21298 _("file index out of range"));
21299 else
21300 symbol_set_symtab (sym, fe->symtab);
21301 }
21302
21303 switch (die->tag)
21304 {
21305 case DW_TAG_label:
21306 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21307 if (attr)
21308 {
21309 CORE_ADDR addr;
21310
21311 addr = attr_value_as_address (attr);
21312 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21313 SYMBOL_VALUE_ADDRESS (sym) = addr;
21314 }
21315 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21316 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21317 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21318 add_symbol_to_list (sym, cu->list_in_scope);
21319 break;
21320 case DW_TAG_subprogram:
21321 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21322 finish_block. */
21323 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21324 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21325 if ((attr2 && (DW_UNSND (attr2) != 0))
21326 || cu->language == language_ada)
21327 {
21328 /* Subprograms marked external are stored as a global symbol.
21329 Ada subprograms, whether marked external or not, are always
21330 stored as a global symbol, because we want to be able to
21331 access them globally. For instance, we want to be able
21332 to break on a nested subprogram without having to
21333 specify the context. */
21334 list_to_add = &global_symbols;
21335 }
21336 else
21337 {
21338 list_to_add = cu->list_in_scope;
21339 }
21340 break;
21341 case DW_TAG_inlined_subroutine:
21342 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21343 finish_block. */
21344 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21345 SYMBOL_INLINED (sym) = 1;
21346 list_to_add = cu->list_in_scope;
21347 break;
21348 case DW_TAG_template_value_param:
21349 suppress_add = 1;
21350 /* Fall through. */
21351 case DW_TAG_constant:
21352 case DW_TAG_variable:
21353 case DW_TAG_member:
21354 /* Compilation with minimal debug info may result in
21355 variables with missing type entries. Change the
21356 misleading `void' type to something sensible. */
21357 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
21358 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21359
21360 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21361 /* In the case of DW_TAG_member, we should only be called for
21362 static const members. */
21363 if (die->tag == DW_TAG_member)
21364 {
21365 /* dwarf2_add_field uses die_is_declaration,
21366 so we do the same. */
21367 gdb_assert (die_is_declaration (die, cu));
21368 gdb_assert (attr);
21369 }
21370 if (attr)
21371 {
21372 dwarf2_const_value (attr, sym, cu);
21373 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21374 if (!suppress_add)
21375 {
21376 if (attr2 && (DW_UNSND (attr2) != 0))
21377 list_to_add = &global_symbols;
21378 else
21379 list_to_add = cu->list_in_scope;
21380 }
21381 break;
21382 }
21383 attr = dwarf2_attr (die, DW_AT_location, cu);
21384 if (attr)
21385 {
21386 var_decode_location (attr, sym, cu);
21387 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21388
21389 /* Fortran explicitly imports any global symbols to the local
21390 scope by DW_TAG_common_block. */
21391 if (cu->language == language_fortran && die->parent
21392 && die->parent->tag == DW_TAG_common_block)
21393 attr2 = NULL;
21394
21395 if (SYMBOL_CLASS (sym) == LOC_STATIC
21396 && SYMBOL_VALUE_ADDRESS (sym) == 0
21397 && !dwarf2_per_objfile->has_section_at_zero)
21398 {
21399 /* When a static variable is eliminated by the linker,
21400 the corresponding debug information is not stripped
21401 out, but the variable address is set to null;
21402 do not add such variables into symbol table. */
21403 }
21404 else if (attr2 && (DW_UNSND (attr2) != 0))
21405 {
21406 /* Workaround gfortran PR debug/40040 - it uses
21407 DW_AT_location for variables in -fPIC libraries which may
21408 get overriden by other libraries/executable and get
21409 a different address. Resolve it by the minimal symbol
21410 which may come from inferior's executable using copy
21411 relocation. Make this workaround only for gfortran as for
21412 other compilers GDB cannot guess the minimal symbol
21413 Fortran mangling kind. */
21414 if (cu->language == language_fortran && die->parent
21415 && die->parent->tag == DW_TAG_module
21416 && cu->producer
21417 && startswith (cu->producer, "GNU Fortran"))
21418 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21419
21420 /* A variable with DW_AT_external is never static,
21421 but it may be block-scoped. */
21422 list_to_add = (cu->list_in_scope == &file_symbols
21423 ? &global_symbols : cu->list_in_scope);
21424 }
21425 else
21426 list_to_add = cu->list_in_scope;
21427 }
21428 else
21429 {
21430 /* We do not know the address of this symbol.
21431 If it is an external symbol and we have type information
21432 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21433 The address of the variable will then be determined from
21434 the minimal symbol table whenever the variable is
21435 referenced. */
21436 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21437
21438 /* Fortran explicitly imports any global symbols to the local
21439 scope by DW_TAG_common_block. */
21440 if (cu->language == language_fortran && die->parent
21441 && die->parent->tag == DW_TAG_common_block)
21442 {
21443 /* SYMBOL_CLASS doesn't matter here because
21444 read_common_block is going to reset it. */
21445 if (!suppress_add)
21446 list_to_add = cu->list_in_scope;
21447 }
21448 else if (attr2 && (DW_UNSND (attr2) != 0)
21449 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21450 {
21451 /* A variable with DW_AT_external is never static, but it
21452 may be block-scoped. */
21453 list_to_add = (cu->list_in_scope == &file_symbols
21454 ? &global_symbols : cu->list_in_scope);
21455
21456 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21457 }
21458 else if (!die_is_declaration (die, cu))
21459 {
21460 /* Use the default LOC_OPTIMIZED_OUT class. */
21461 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21462 if (!suppress_add)
21463 list_to_add = cu->list_in_scope;
21464 }
21465 }
21466 break;
21467 case DW_TAG_formal_parameter:
21468 /* If we are inside a function, mark this as an argument. If
21469 not, we might be looking at an argument to an inlined function
21470 when we do not have enough information to show inlined frames;
21471 pretend it's a local variable in that case so that the user can
21472 still see it. */
21473 if (context_stack_depth > 0
21474 && context_stack[context_stack_depth - 1].name != NULL)
21475 SYMBOL_IS_ARGUMENT (sym) = 1;
21476 attr = dwarf2_attr (die, DW_AT_location, cu);
21477 if (attr)
21478 {
21479 var_decode_location (attr, sym, cu);
21480 }
21481 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21482 if (attr)
21483 {
21484 dwarf2_const_value (attr, sym, cu);
21485 }
21486
21487 list_to_add = cu->list_in_scope;
21488 break;
21489 case DW_TAG_unspecified_parameters:
21490 /* From varargs functions; gdb doesn't seem to have any
21491 interest in this information, so just ignore it for now.
21492 (FIXME?) */
21493 break;
21494 case DW_TAG_template_type_param:
21495 suppress_add = 1;
21496 /* Fall through. */
21497 case DW_TAG_class_type:
21498 case DW_TAG_interface_type:
21499 case DW_TAG_structure_type:
21500 case DW_TAG_union_type:
21501 case DW_TAG_set_type:
21502 case DW_TAG_enumeration_type:
21503 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21504 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21505
21506 {
21507 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21508 really ever be static objects: otherwise, if you try
21509 to, say, break of a class's method and you're in a file
21510 which doesn't mention that class, it won't work unless
21511 the check for all static symbols in lookup_symbol_aux
21512 saves you. See the OtherFileClass tests in
21513 gdb.c++/namespace.exp. */
21514
21515 if (!suppress_add)
21516 {
21517 list_to_add = (cu->list_in_scope == &file_symbols
21518 && cu->language == language_cplus
21519 ? &global_symbols : cu->list_in_scope);
21520
21521 /* The semantics of C++ state that "struct foo {
21522 ... }" also defines a typedef for "foo". */
21523 if (cu->language == language_cplus
21524 || cu->language == language_ada
21525 || cu->language == language_d
21526 || cu->language == language_rust)
21527 {
21528 /* The symbol's name is already allocated along
21529 with this objfile, so we don't need to
21530 duplicate it for the type. */
21531 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21532 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
21533 }
21534 }
21535 }
21536 break;
21537 case DW_TAG_typedef:
21538 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21539 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21540 list_to_add = cu->list_in_scope;
21541 break;
21542 case DW_TAG_base_type:
21543 case DW_TAG_subrange_type:
21544 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21545 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21546 list_to_add = cu->list_in_scope;
21547 break;
21548 case DW_TAG_enumerator:
21549 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21550 if (attr)
21551 {
21552 dwarf2_const_value (attr, sym, cu);
21553 }
21554 {
21555 /* NOTE: carlton/2003-11-10: See comment above in the
21556 DW_TAG_class_type, etc. block. */
21557
21558 list_to_add = (cu->list_in_scope == &file_symbols
21559 && cu->language == language_cplus
21560 ? &global_symbols : cu->list_in_scope);
21561 }
21562 break;
21563 case DW_TAG_imported_declaration:
21564 case DW_TAG_namespace:
21565 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21566 list_to_add = &global_symbols;
21567 break;
21568 case DW_TAG_module:
21569 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21570 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21571 list_to_add = &global_symbols;
21572 break;
21573 case DW_TAG_common_block:
21574 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21575 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21576 add_symbol_to_list (sym, cu->list_in_scope);
21577 break;
21578 default:
21579 /* Not a tag we recognize. Hopefully we aren't processing
21580 trash data, but since we must specifically ignore things
21581 we don't recognize, there is nothing else we should do at
21582 this point. */
21583 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
21584 dwarf_tag_name (die->tag));
21585 break;
21586 }
21587
21588 if (suppress_add)
21589 {
21590 sym->hash_next = objfile->template_symbols;
21591 objfile->template_symbols = sym;
21592 list_to_add = NULL;
21593 }
21594
21595 if (list_to_add != NULL)
21596 add_symbol_to_list (sym, list_to_add);
21597
21598 /* For the benefit of old versions of GCC, check for anonymous
21599 namespaces based on the demangled name. */
21600 if (!cu->processing_has_namespace_info
21601 && cu->language == language_cplus)
21602 cp_scan_for_anonymous_namespaces (sym, objfile);
21603 }
21604 return (sym);
21605 }
21606
21607 /* Given an attr with a DW_FORM_dataN value in host byte order,
21608 zero-extend it as appropriate for the symbol's type. The DWARF
21609 standard (v4) is not entirely clear about the meaning of using
21610 DW_FORM_dataN for a constant with a signed type, where the type is
21611 wider than the data. The conclusion of a discussion on the DWARF
21612 list was that this is unspecified. We choose to always zero-extend
21613 because that is the interpretation long in use by GCC. */
21614
21615 static gdb_byte *
21616 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21617 struct dwarf2_cu *cu, LONGEST *value, int bits)
21618 {
21619 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21620 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21621 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21622 LONGEST l = DW_UNSND (attr);
21623
21624 if (bits < sizeof (*value) * 8)
21625 {
21626 l &= ((LONGEST) 1 << bits) - 1;
21627 *value = l;
21628 }
21629 else if (bits == sizeof (*value) * 8)
21630 *value = l;
21631 else
21632 {
21633 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21634 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21635 return bytes;
21636 }
21637
21638 return NULL;
21639 }
21640
21641 /* Read a constant value from an attribute. Either set *VALUE, or if
21642 the value does not fit in *VALUE, set *BYTES - either already
21643 allocated on the objfile obstack, or newly allocated on OBSTACK,
21644 or, set *BATON, if we translated the constant to a location
21645 expression. */
21646
21647 static void
21648 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21649 const char *name, struct obstack *obstack,
21650 struct dwarf2_cu *cu,
21651 LONGEST *value, const gdb_byte **bytes,
21652 struct dwarf2_locexpr_baton **baton)
21653 {
21654 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21655 struct comp_unit_head *cu_header = &cu->header;
21656 struct dwarf_block *blk;
21657 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21658 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21659
21660 *value = 0;
21661 *bytes = NULL;
21662 *baton = NULL;
21663
21664 switch (attr->form)
21665 {
21666 case DW_FORM_addr:
21667 case DW_FORM_GNU_addr_index:
21668 {
21669 gdb_byte *data;
21670
21671 if (TYPE_LENGTH (type) != cu_header->addr_size)
21672 dwarf2_const_value_length_mismatch_complaint (name,
21673 cu_header->addr_size,
21674 TYPE_LENGTH (type));
21675 /* Symbols of this form are reasonably rare, so we just
21676 piggyback on the existing location code rather than writing
21677 a new implementation of symbol_computed_ops. */
21678 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21679 (*baton)->per_cu = cu->per_cu;
21680 gdb_assert ((*baton)->per_cu);
21681
21682 (*baton)->size = 2 + cu_header->addr_size;
21683 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21684 (*baton)->data = data;
21685
21686 data[0] = DW_OP_addr;
21687 store_unsigned_integer (&data[1], cu_header->addr_size,
21688 byte_order, DW_ADDR (attr));
21689 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21690 }
21691 break;
21692 case DW_FORM_string:
21693 case DW_FORM_strp:
21694 case DW_FORM_GNU_str_index:
21695 case DW_FORM_GNU_strp_alt:
21696 /* DW_STRING is already allocated on the objfile obstack, point
21697 directly to it. */
21698 *bytes = (const gdb_byte *) DW_STRING (attr);
21699 break;
21700 case DW_FORM_block1:
21701 case DW_FORM_block2:
21702 case DW_FORM_block4:
21703 case DW_FORM_block:
21704 case DW_FORM_exprloc:
21705 case DW_FORM_data16:
21706 blk = DW_BLOCK (attr);
21707 if (TYPE_LENGTH (type) != blk->size)
21708 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21709 TYPE_LENGTH (type));
21710 *bytes = blk->data;
21711 break;
21712
21713 /* The DW_AT_const_value attributes are supposed to carry the
21714 symbol's value "represented as it would be on the target
21715 architecture." By the time we get here, it's already been
21716 converted to host endianness, so we just need to sign- or
21717 zero-extend it as appropriate. */
21718 case DW_FORM_data1:
21719 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21720 break;
21721 case DW_FORM_data2:
21722 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21723 break;
21724 case DW_FORM_data4:
21725 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21726 break;
21727 case DW_FORM_data8:
21728 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21729 break;
21730
21731 case DW_FORM_sdata:
21732 case DW_FORM_implicit_const:
21733 *value = DW_SND (attr);
21734 break;
21735
21736 case DW_FORM_udata:
21737 *value = DW_UNSND (attr);
21738 break;
21739
21740 default:
21741 complaint (&symfile_complaints,
21742 _("unsupported const value attribute form: '%s'"),
21743 dwarf_form_name (attr->form));
21744 *value = 0;
21745 break;
21746 }
21747 }
21748
21749
21750 /* Copy constant value from an attribute to a symbol. */
21751
21752 static void
21753 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21754 struct dwarf2_cu *cu)
21755 {
21756 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21757 LONGEST value;
21758 const gdb_byte *bytes;
21759 struct dwarf2_locexpr_baton *baton;
21760
21761 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21762 SYMBOL_PRINT_NAME (sym),
21763 &objfile->objfile_obstack, cu,
21764 &value, &bytes, &baton);
21765
21766 if (baton != NULL)
21767 {
21768 SYMBOL_LOCATION_BATON (sym) = baton;
21769 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21770 }
21771 else if (bytes != NULL)
21772 {
21773 SYMBOL_VALUE_BYTES (sym) = bytes;
21774 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21775 }
21776 else
21777 {
21778 SYMBOL_VALUE (sym) = value;
21779 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21780 }
21781 }
21782
21783 /* Return the type of the die in question using its DW_AT_type attribute. */
21784
21785 static struct type *
21786 die_type (struct die_info *die, struct dwarf2_cu *cu)
21787 {
21788 struct attribute *type_attr;
21789
21790 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21791 if (!type_attr)
21792 {
21793 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21794 /* A missing DW_AT_type represents a void type. */
21795 return objfile_type (objfile)->builtin_void;
21796 }
21797
21798 return lookup_die_type (die, type_attr, cu);
21799 }
21800
21801 /* True iff CU's producer generates GNAT Ada auxiliary information
21802 that allows to find parallel types through that information instead
21803 of having to do expensive parallel lookups by type name. */
21804
21805 static int
21806 need_gnat_info (struct dwarf2_cu *cu)
21807 {
21808 /* Assume that the Ada compiler was GNAT, which always produces
21809 the auxiliary information. */
21810 return (cu->language == language_ada);
21811 }
21812
21813 /* Return the auxiliary type of the die in question using its
21814 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21815 attribute is not present. */
21816
21817 static struct type *
21818 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21819 {
21820 struct attribute *type_attr;
21821
21822 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21823 if (!type_attr)
21824 return NULL;
21825
21826 return lookup_die_type (die, type_attr, cu);
21827 }
21828
21829 /* If DIE has a descriptive_type attribute, then set the TYPE's
21830 descriptive type accordingly. */
21831
21832 static void
21833 set_descriptive_type (struct type *type, struct die_info *die,
21834 struct dwarf2_cu *cu)
21835 {
21836 struct type *descriptive_type = die_descriptive_type (die, cu);
21837
21838 if (descriptive_type)
21839 {
21840 ALLOCATE_GNAT_AUX_TYPE (type);
21841 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21842 }
21843 }
21844
21845 /* Return the containing type of the die in question using its
21846 DW_AT_containing_type attribute. */
21847
21848 static struct type *
21849 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21850 {
21851 struct attribute *type_attr;
21852 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21853
21854 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21855 if (!type_attr)
21856 error (_("Dwarf Error: Problem turning containing type into gdb type "
21857 "[in module %s]"), objfile_name (objfile));
21858
21859 return lookup_die_type (die, type_attr, cu);
21860 }
21861
21862 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21863
21864 static struct type *
21865 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21866 {
21867 struct dwarf2_per_objfile *dwarf2_per_objfile
21868 = cu->per_cu->dwarf2_per_objfile;
21869 struct objfile *objfile = dwarf2_per_objfile->objfile;
21870 char *message, *saved;
21871
21872 message = xstrprintf (_("<unknown type in %s, CU %s, DIE %s>"),
21873 objfile_name (objfile),
21874 sect_offset_str (cu->header.sect_off),
21875 sect_offset_str (die->sect_off));
21876 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
21877 message, strlen (message));
21878 xfree (message);
21879
21880 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21881 }
21882
21883 /* Look up the type of DIE in CU using its type attribute ATTR.
21884 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21885 DW_AT_containing_type.
21886 If there is no type substitute an error marker. */
21887
21888 static struct type *
21889 lookup_die_type (struct die_info *die, const struct attribute *attr,
21890 struct dwarf2_cu *cu)
21891 {
21892 struct dwarf2_per_objfile *dwarf2_per_objfile
21893 = cu->per_cu->dwarf2_per_objfile;
21894 struct objfile *objfile = dwarf2_per_objfile->objfile;
21895 struct type *this_type;
21896
21897 gdb_assert (attr->name == DW_AT_type
21898 || attr->name == DW_AT_GNAT_descriptive_type
21899 || attr->name == DW_AT_containing_type);
21900
21901 /* First see if we have it cached. */
21902
21903 if (attr->form == DW_FORM_GNU_ref_alt)
21904 {
21905 struct dwarf2_per_cu_data *per_cu;
21906 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21907
21908 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21909 dwarf2_per_objfile);
21910 this_type = get_die_type_at_offset (sect_off, per_cu);
21911 }
21912 else if (attr_form_is_ref (attr))
21913 {
21914 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21915
21916 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
21917 }
21918 else if (attr->form == DW_FORM_ref_sig8)
21919 {
21920 ULONGEST signature = DW_SIGNATURE (attr);
21921
21922 return get_signatured_type (die, signature, cu);
21923 }
21924 else
21925 {
21926 complaint (&symfile_complaints,
21927 _("Dwarf Error: Bad type attribute %s in DIE"
21928 " at %s [in module %s]"),
21929 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21930 objfile_name (objfile));
21931 return build_error_marker_type (cu, die);
21932 }
21933
21934 /* If not cached we need to read it in. */
21935
21936 if (this_type == NULL)
21937 {
21938 struct die_info *type_die = NULL;
21939 struct dwarf2_cu *type_cu = cu;
21940
21941 if (attr_form_is_ref (attr))
21942 type_die = follow_die_ref (die, attr, &type_cu);
21943 if (type_die == NULL)
21944 return build_error_marker_type (cu, die);
21945 /* If we find the type now, it's probably because the type came
21946 from an inter-CU reference and the type's CU got expanded before
21947 ours. */
21948 this_type = read_type_die (type_die, type_cu);
21949 }
21950
21951 /* If we still don't have a type use an error marker. */
21952
21953 if (this_type == NULL)
21954 return build_error_marker_type (cu, die);
21955
21956 return this_type;
21957 }
21958
21959 /* Return the type in DIE, CU.
21960 Returns NULL for invalid types.
21961
21962 This first does a lookup in die_type_hash,
21963 and only reads the die in if necessary.
21964
21965 NOTE: This can be called when reading in partial or full symbols. */
21966
21967 static struct type *
21968 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21969 {
21970 struct type *this_type;
21971
21972 this_type = get_die_type (die, cu);
21973 if (this_type)
21974 return this_type;
21975
21976 return read_type_die_1 (die, cu);
21977 }
21978
21979 /* Read the type in DIE, CU.
21980 Returns NULL for invalid types. */
21981
21982 static struct type *
21983 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21984 {
21985 struct type *this_type = NULL;
21986
21987 switch (die->tag)
21988 {
21989 case DW_TAG_class_type:
21990 case DW_TAG_interface_type:
21991 case DW_TAG_structure_type:
21992 case DW_TAG_union_type:
21993 this_type = read_structure_type (die, cu);
21994 break;
21995 case DW_TAG_enumeration_type:
21996 this_type = read_enumeration_type (die, cu);
21997 break;
21998 case DW_TAG_subprogram:
21999 case DW_TAG_subroutine_type:
22000 case DW_TAG_inlined_subroutine:
22001 this_type = read_subroutine_type (die, cu);
22002 break;
22003 case DW_TAG_array_type:
22004 this_type = read_array_type (die, cu);
22005 break;
22006 case DW_TAG_set_type:
22007 this_type = read_set_type (die, cu);
22008 break;
22009 case DW_TAG_pointer_type:
22010 this_type = read_tag_pointer_type (die, cu);
22011 break;
22012 case DW_TAG_ptr_to_member_type:
22013 this_type = read_tag_ptr_to_member_type (die, cu);
22014 break;
22015 case DW_TAG_reference_type:
22016 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22017 break;
22018 case DW_TAG_rvalue_reference_type:
22019 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22020 break;
22021 case DW_TAG_const_type:
22022 this_type = read_tag_const_type (die, cu);
22023 break;
22024 case DW_TAG_volatile_type:
22025 this_type = read_tag_volatile_type (die, cu);
22026 break;
22027 case DW_TAG_restrict_type:
22028 this_type = read_tag_restrict_type (die, cu);
22029 break;
22030 case DW_TAG_string_type:
22031 this_type = read_tag_string_type (die, cu);
22032 break;
22033 case DW_TAG_typedef:
22034 this_type = read_typedef (die, cu);
22035 break;
22036 case DW_TAG_subrange_type:
22037 this_type = read_subrange_type (die, cu);
22038 break;
22039 case DW_TAG_base_type:
22040 this_type = read_base_type (die, cu);
22041 break;
22042 case DW_TAG_unspecified_type:
22043 this_type = read_unspecified_type (die, cu);
22044 break;
22045 case DW_TAG_namespace:
22046 this_type = read_namespace_type (die, cu);
22047 break;
22048 case DW_TAG_module:
22049 this_type = read_module_type (die, cu);
22050 break;
22051 case DW_TAG_atomic_type:
22052 this_type = read_tag_atomic_type (die, cu);
22053 break;
22054 default:
22055 complaint (&symfile_complaints,
22056 _("unexpected tag in read_type_die: '%s'"),
22057 dwarf_tag_name (die->tag));
22058 break;
22059 }
22060
22061 return this_type;
22062 }
22063
22064 /* See if we can figure out if the class lives in a namespace. We do
22065 this by looking for a member function; its demangled name will
22066 contain namespace info, if there is any.
22067 Return the computed name or NULL.
22068 Space for the result is allocated on the objfile's obstack.
22069 This is the full-die version of guess_partial_die_structure_name.
22070 In this case we know DIE has no useful parent. */
22071
22072 static char *
22073 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22074 {
22075 struct die_info *spec_die;
22076 struct dwarf2_cu *spec_cu;
22077 struct die_info *child;
22078 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22079
22080 spec_cu = cu;
22081 spec_die = die_specification (die, &spec_cu);
22082 if (spec_die != NULL)
22083 {
22084 die = spec_die;
22085 cu = spec_cu;
22086 }
22087
22088 for (child = die->child;
22089 child != NULL;
22090 child = child->sibling)
22091 {
22092 if (child->tag == DW_TAG_subprogram)
22093 {
22094 const char *linkage_name = dw2_linkage_name (child, cu);
22095
22096 if (linkage_name != NULL)
22097 {
22098 char *actual_name
22099 = language_class_name_from_physname (cu->language_defn,
22100 linkage_name);
22101 char *name = NULL;
22102
22103 if (actual_name != NULL)
22104 {
22105 const char *die_name = dwarf2_name (die, cu);
22106
22107 if (die_name != NULL
22108 && strcmp (die_name, actual_name) != 0)
22109 {
22110 /* Strip off the class name from the full name.
22111 We want the prefix. */
22112 int die_name_len = strlen (die_name);
22113 int actual_name_len = strlen (actual_name);
22114
22115 /* Test for '::' as a sanity check. */
22116 if (actual_name_len > die_name_len + 2
22117 && actual_name[actual_name_len
22118 - die_name_len - 1] == ':')
22119 name = (char *) obstack_copy0 (
22120 &objfile->per_bfd->storage_obstack,
22121 actual_name, actual_name_len - die_name_len - 2);
22122 }
22123 }
22124 xfree (actual_name);
22125 return name;
22126 }
22127 }
22128 }
22129
22130 return NULL;
22131 }
22132
22133 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22134 prefix part in such case. See
22135 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22136
22137 static const char *
22138 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22139 {
22140 struct attribute *attr;
22141 const char *base;
22142
22143 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22144 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22145 return NULL;
22146
22147 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22148 return NULL;
22149
22150 attr = dw2_linkage_name_attr (die, cu);
22151 if (attr == NULL || DW_STRING (attr) == NULL)
22152 return NULL;
22153
22154 /* dwarf2_name had to be already called. */
22155 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22156
22157 /* Strip the base name, keep any leading namespaces/classes. */
22158 base = strrchr (DW_STRING (attr), ':');
22159 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22160 return "";
22161
22162 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22163 return (char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
22164 DW_STRING (attr),
22165 &base[-1] - DW_STRING (attr));
22166 }
22167
22168 /* Return the name of the namespace/class that DIE is defined within,
22169 or "" if we can't tell. The caller should not xfree the result.
22170
22171 For example, if we're within the method foo() in the following
22172 code:
22173
22174 namespace N {
22175 class C {
22176 void foo () {
22177 }
22178 };
22179 }
22180
22181 then determine_prefix on foo's die will return "N::C". */
22182
22183 static const char *
22184 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22185 {
22186 struct dwarf2_per_objfile *dwarf2_per_objfile
22187 = cu->per_cu->dwarf2_per_objfile;
22188 struct die_info *parent, *spec_die;
22189 struct dwarf2_cu *spec_cu;
22190 struct type *parent_type;
22191 const char *retval;
22192
22193 if (cu->language != language_cplus
22194 && cu->language != language_fortran && cu->language != language_d
22195 && cu->language != language_rust)
22196 return "";
22197
22198 retval = anonymous_struct_prefix (die, cu);
22199 if (retval)
22200 return retval;
22201
22202 /* We have to be careful in the presence of DW_AT_specification.
22203 For example, with GCC 3.4, given the code
22204
22205 namespace N {
22206 void foo() {
22207 // Definition of N::foo.
22208 }
22209 }
22210
22211 then we'll have a tree of DIEs like this:
22212
22213 1: DW_TAG_compile_unit
22214 2: DW_TAG_namespace // N
22215 3: DW_TAG_subprogram // declaration of N::foo
22216 4: DW_TAG_subprogram // definition of N::foo
22217 DW_AT_specification // refers to die #3
22218
22219 Thus, when processing die #4, we have to pretend that we're in
22220 the context of its DW_AT_specification, namely the contex of die
22221 #3. */
22222 spec_cu = cu;
22223 spec_die = die_specification (die, &spec_cu);
22224 if (spec_die == NULL)
22225 parent = die->parent;
22226 else
22227 {
22228 parent = spec_die->parent;
22229 cu = spec_cu;
22230 }
22231
22232 if (parent == NULL)
22233 return "";
22234 else if (parent->building_fullname)
22235 {
22236 const char *name;
22237 const char *parent_name;
22238
22239 /* It has been seen on RealView 2.2 built binaries,
22240 DW_TAG_template_type_param types actually _defined_ as
22241 children of the parent class:
22242
22243 enum E {};
22244 template class <class Enum> Class{};
22245 Class<enum E> class_e;
22246
22247 1: DW_TAG_class_type (Class)
22248 2: DW_TAG_enumeration_type (E)
22249 3: DW_TAG_enumerator (enum1:0)
22250 3: DW_TAG_enumerator (enum2:1)
22251 ...
22252 2: DW_TAG_template_type_param
22253 DW_AT_type DW_FORM_ref_udata (E)
22254
22255 Besides being broken debug info, it can put GDB into an
22256 infinite loop. Consider:
22257
22258 When we're building the full name for Class<E>, we'll start
22259 at Class, and go look over its template type parameters,
22260 finding E. We'll then try to build the full name of E, and
22261 reach here. We're now trying to build the full name of E,
22262 and look over the parent DIE for containing scope. In the
22263 broken case, if we followed the parent DIE of E, we'd again
22264 find Class, and once again go look at its template type
22265 arguments, etc., etc. Simply don't consider such parent die
22266 as source-level parent of this die (it can't be, the language
22267 doesn't allow it), and break the loop here. */
22268 name = dwarf2_name (die, cu);
22269 parent_name = dwarf2_name (parent, cu);
22270 complaint (&symfile_complaints,
22271 _("template param type '%s' defined within parent '%s'"),
22272 name ? name : "<unknown>",
22273 parent_name ? parent_name : "<unknown>");
22274 return "";
22275 }
22276 else
22277 switch (parent->tag)
22278 {
22279 case DW_TAG_namespace:
22280 parent_type = read_type_die (parent, cu);
22281 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22282 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22283 Work around this problem here. */
22284 if (cu->language == language_cplus
22285 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
22286 return "";
22287 /* We give a name to even anonymous namespaces. */
22288 return TYPE_TAG_NAME (parent_type);
22289 case DW_TAG_class_type:
22290 case DW_TAG_interface_type:
22291 case DW_TAG_structure_type:
22292 case DW_TAG_union_type:
22293 case DW_TAG_module:
22294 parent_type = read_type_die (parent, cu);
22295 if (TYPE_TAG_NAME (parent_type) != NULL)
22296 return TYPE_TAG_NAME (parent_type);
22297 else
22298 /* An anonymous structure is only allowed non-static data
22299 members; no typedefs, no member functions, et cetera.
22300 So it does not need a prefix. */
22301 return "";
22302 case DW_TAG_compile_unit:
22303 case DW_TAG_partial_unit:
22304 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22305 if (cu->language == language_cplus
22306 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
22307 && die->child != NULL
22308 && (die->tag == DW_TAG_class_type
22309 || die->tag == DW_TAG_structure_type
22310 || die->tag == DW_TAG_union_type))
22311 {
22312 char *name = guess_full_die_structure_name (die, cu);
22313 if (name != NULL)
22314 return name;
22315 }
22316 return "";
22317 case DW_TAG_enumeration_type:
22318 parent_type = read_type_die (parent, cu);
22319 if (TYPE_DECLARED_CLASS (parent_type))
22320 {
22321 if (TYPE_TAG_NAME (parent_type) != NULL)
22322 return TYPE_TAG_NAME (parent_type);
22323 return "";
22324 }
22325 /* Fall through. */
22326 default:
22327 return determine_prefix (parent, cu);
22328 }
22329 }
22330
22331 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22332 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22333 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22334 an obconcat, otherwise allocate storage for the result. The CU argument is
22335 used to determine the language and hence, the appropriate separator. */
22336
22337 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22338
22339 static char *
22340 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22341 int physname, struct dwarf2_cu *cu)
22342 {
22343 const char *lead = "";
22344 const char *sep;
22345
22346 if (suffix == NULL || suffix[0] == '\0'
22347 || prefix == NULL || prefix[0] == '\0')
22348 sep = "";
22349 else if (cu->language == language_d)
22350 {
22351 /* For D, the 'main' function could be defined in any module, but it
22352 should never be prefixed. */
22353 if (strcmp (suffix, "D main") == 0)
22354 {
22355 prefix = "";
22356 sep = "";
22357 }
22358 else
22359 sep = ".";
22360 }
22361 else if (cu->language == language_fortran && physname)
22362 {
22363 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22364 DW_AT_MIPS_linkage_name is preferred and used instead. */
22365
22366 lead = "__";
22367 sep = "_MOD_";
22368 }
22369 else
22370 sep = "::";
22371
22372 if (prefix == NULL)
22373 prefix = "";
22374 if (suffix == NULL)
22375 suffix = "";
22376
22377 if (obs == NULL)
22378 {
22379 char *retval
22380 = ((char *)
22381 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22382
22383 strcpy (retval, lead);
22384 strcat (retval, prefix);
22385 strcat (retval, sep);
22386 strcat (retval, suffix);
22387 return retval;
22388 }
22389 else
22390 {
22391 /* We have an obstack. */
22392 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22393 }
22394 }
22395
22396 /* Return sibling of die, NULL if no sibling. */
22397
22398 static struct die_info *
22399 sibling_die (struct die_info *die)
22400 {
22401 return die->sibling;
22402 }
22403
22404 /* Get name of a die, return NULL if not found. */
22405
22406 static const char *
22407 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22408 struct obstack *obstack)
22409 {
22410 if (name && cu->language == language_cplus)
22411 {
22412 std::string canon_name = cp_canonicalize_string (name);
22413
22414 if (!canon_name.empty ())
22415 {
22416 if (canon_name != name)
22417 name = (const char *) obstack_copy0 (obstack,
22418 canon_name.c_str (),
22419 canon_name.length ());
22420 }
22421 }
22422
22423 return name;
22424 }
22425
22426 /* Get name of a die, return NULL if not found.
22427 Anonymous namespaces are converted to their magic string. */
22428
22429 static const char *
22430 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22431 {
22432 struct attribute *attr;
22433 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22434
22435 attr = dwarf2_attr (die, DW_AT_name, cu);
22436 if ((!attr || !DW_STRING (attr))
22437 && die->tag != DW_TAG_namespace
22438 && die->tag != DW_TAG_class_type
22439 && die->tag != DW_TAG_interface_type
22440 && die->tag != DW_TAG_structure_type
22441 && die->tag != DW_TAG_union_type)
22442 return NULL;
22443
22444 switch (die->tag)
22445 {
22446 case DW_TAG_compile_unit:
22447 case DW_TAG_partial_unit:
22448 /* Compilation units have a DW_AT_name that is a filename, not
22449 a source language identifier. */
22450 case DW_TAG_enumeration_type:
22451 case DW_TAG_enumerator:
22452 /* These tags always have simple identifiers already; no need
22453 to canonicalize them. */
22454 return DW_STRING (attr);
22455
22456 case DW_TAG_namespace:
22457 if (attr != NULL && DW_STRING (attr) != NULL)
22458 return DW_STRING (attr);
22459 return CP_ANONYMOUS_NAMESPACE_STR;
22460
22461 case DW_TAG_class_type:
22462 case DW_TAG_interface_type:
22463 case DW_TAG_structure_type:
22464 case DW_TAG_union_type:
22465 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22466 structures or unions. These were of the form "._%d" in GCC 4.1,
22467 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22468 and GCC 4.4. We work around this problem by ignoring these. */
22469 if (attr && DW_STRING (attr)
22470 && (startswith (DW_STRING (attr), "._")
22471 || startswith (DW_STRING (attr), "<anonymous")))
22472 return NULL;
22473
22474 /* GCC might emit a nameless typedef that has a linkage name. See
22475 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22476 if (!attr || DW_STRING (attr) == NULL)
22477 {
22478 char *demangled = NULL;
22479
22480 attr = dw2_linkage_name_attr (die, cu);
22481 if (attr == NULL || DW_STRING (attr) == NULL)
22482 return NULL;
22483
22484 /* Avoid demangling DW_STRING (attr) the second time on a second
22485 call for the same DIE. */
22486 if (!DW_STRING_IS_CANONICAL (attr))
22487 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
22488
22489 if (demangled)
22490 {
22491 const char *base;
22492
22493 /* FIXME: we already did this for the partial symbol... */
22494 DW_STRING (attr)
22495 = ((const char *)
22496 obstack_copy0 (&objfile->per_bfd->storage_obstack,
22497 demangled, strlen (demangled)));
22498 DW_STRING_IS_CANONICAL (attr) = 1;
22499 xfree (demangled);
22500
22501 /* Strip any leading namespaces/classes, keep only the base name.
22502 DW_AT_name for named DIEs does not contain the prefixes. */
22503 base = strrchr (DW_STRING (attr), ':');
22504 if (base && base > DW_STRING (attr) && base[-1] == ':')
22505 return &base[1];
22506 else
22507 return DW_STRING (attr);
22508 }
22509 }
22510 break;
22511
22512 default:
22513 break;
22514 }
22515
22516 if (!DW_STRING_IS_CANONICAL (attr))
22517 {
22518 DW_STRING (attr)
22519 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22520 &objfile->per_bfd->storage_obstack);
22521 DW_STRING_IS_CANONICAL (attr) = 1;
22522 }
22523 return DW_STRING (attr);
22524 }
22525
22526 /* Return the die that this die in an extension of, or NULL if there
22527 is none. *EXT_CU is the CU containing DIE on input, and the CU
22528 containing the return value on output. */
22529
22530 static struct die_info *
22531 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22532 {
22533 struct attribute *attr;
22534
22535 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22536 if (attr == NULL)
22537 return NULL;
22538
22539 return follow_die_ref (die, attr, ext_cu);
22540 }
22541
22542 /* Convert a DIE tag into its string name. */
22543
22544 static const char *
22545 dwarf_tag_name (unsigned tag)
22546 {
22547 const char *name = get_DW_TAG_name (tag);
22548
22549 if (name == NULL)
22550 return "DW_TAG_<unknown>";
22551
22552 return name;
22553 }
22554
22555 /* Convert a DWARF attribute code into its string name. */
22556
22557 static const char *
22558 dwarf_attr_name (unsigned attr)
22559 {
22560 const char *name;
22561
22562 #ifdef MIPS /* collides with DW_AT_HP_block_index */
22563 if (attr == DW_AT_MIPS_fde)
22564 return "DW_AT_MIPS_fde";
22565 #else
22566 if (attr == DW_AT_HP_block_index)
22567 return "DW_AT_HP_block_index";
22568 #endif
22569
22570 name = get_DW_AT_name (attr);
22571
22572 if (name == NULL)
22573 return "DW_AT_<unknown>";
22574
22575 return name;
22576 }
22577
22578 /* Convert a DWARF value form code into its string name. */
22579
22580 static const char *
22581 dwarf_form_name (unsigned form)
22582 {
22583 const char *name = get_DW_FORM_name (form);
22584
22585 if (name == NULL)
22586 return "DW_FORM_<unknown>";
22587
22588 return name;
22589 }
22590
22591 static const char *
22592 dwarf_bool_name (unsigned mybool)
22593 {
22594 if (mybool)
22595 return "TRUE";
22596 else
22597 return "FALSE";
22598 }
22599
22600 /* Convert a DWARF type code into its string name. */
22601
22602 static const char *
22603 dwarf_type_encoding_name (unsigned enc)
22604 {
22605 const char *name = get_DW_ATE_name (enc);
22606
22607 if (name == NULL)
22608 return "DW_ATE_<unknown>";
22609
22610 return name;
22611 }
22612
22613 static void
22614 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22615 {
22616 unsigned int i;
22617
22618 print_spaces (indent, f);
22619 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
22620 dwarf_tag_name (die->tag), die->abbrev,
22621 sect_offset_str (die->sect_off));
22622
22623 if (die->parent != NULL)
22624 {
22625 print_spaces (indent, f);
22626 fprintf_unfiltered (f, " parent at offset: %s\n",
22627 sect_offset_str (die->parent->sect_off));
22628 }
22629
22630 print_spaces (indent, f);
22631 fprintf_unfiltered (f, " has children: %s\n",
22632 dwarf_bool_name (die->child != NULL));
22633
22634 print_spaces (indent, f);
22635 fprintf_unfiltered (f, " attributes:\n");
22636
22637 for (i = 0; i < die->num_attrs; ++i)
22638 {
22639 print_spaces (indent, f);
22640 fprintf_unfiltered (f, " %s (%s) ",
22641 dwarf_attr_name (die->attrs[i].name),
22642 dwarf_form_name (die->attrs[i].form));
22643
22644 switch (die->attrs[i].form)
22645 {
22646 case DW_FORM_addr:
22647 case DW_FORM_GNU_addr_index:
22648 fprintf_unfiltered (f, "address: ");
22649 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
22650 break;
22651 case DW_FORM_block2:
22652 case DW_FORM_block4:
22653 case DW_FORM_block:
22654 case DW_FORM_block1:
22655 fprintf_unfiltered (f, "block: size %s",
22656 pulongest (DW_BLOCK (&die->attrs[i])->size));
22657 break;
22658 case DW_FORM_exprloc:
22659 fprintf_unfiltered (f, "expression: size %s",
22660 pulongest (DW_BLOCK (&die->attrs[i])->size));
22661 break;
22662 case DW_FORM_data16:
22663 fprintf_unfiltered (f, "constant of 16 bytes");
22664 break;
22665 case DW_FORM_ref_addr:
22666 fprintf_unfiltered (f, "ref address: ");
22667 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22668 break;
22669 case DW_FORM_GNU_ref_alt:
22670 fprintf_unfiltered (f, "alt ref address: ");
22671 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22672 break;
22673 case DW_FORM_ref1:
22674 case DW_FORM_ref2:
22675 case DW_FORM_ref4:
22676 case DW_FORM_ref8:
22677 case DW_FORM_ref_udata:
22678 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22679 (long) (DW_UNSND (&die->attrs[i])));
22680 break;
22681 case DW_FORM_data1:
22682 case DW_FORM_data2:
22683 case DW_FORM_data4:
22684 case DW_FORM_data8:
22685 case DW_FORM_udata:
22686 case DW_FORM_sdata:
22687 fprintf_unfiltered (f, "constant: %s",
22688 pulongest (DW_UNSND (&die->attrs[i])));
22689 break;
22690 case DW_FORM_sec_offset:
22691 fprintf_unfiltered (f, "section offset: %s",
22692 pulongest (DW_UNSND (&die->attrs[i])));
22693 break;
22694 case DW_FORM_ref_sig8:
22695 fprintf_unfiltered (f, "signature: %s",
22696 hex_string (DW_SIGNATURE (&die->attrs[i])));
22697 break;
22698 case DW_FORM_string:
22699 case DW_FORM_strp:
22700 case DW_FORM_line_strp:
22701 case DW_FORM_GNU_str_index:
22702 case DW_FORM_GNU_strp_alt:
22703 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22704 DW_STRING (&die->attrs[i])
22705 ? DW_STRING (&die->attrs[i]) : "",
22706 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22707 break;
22708 case DW_FORM_flag:
22709 if (DW_UNSND (&die->attrs[i]))
22710 fprintf_unfiltered (f, "flag: TRUE");
22711 else
22712 fprintf_unfiltered (f, "flag: FALSE");
22713 break;
22714 case DW_FORM_flag_present:
22715 fprintf_unfiltered (f, "flag: TRUE");
22716 break;
22717 case DW_FORM_indirect:
22718 /* The reader will have reduced the indirect form to
22719 the "base form" so this form should not occur. */
22720 fprintf_unfiltered (f,
22721 "unexpected attribute form: DW_FORM_indirect");
22722 break;
22723 case DW_FORM_implicit_const:
22724 fprintf_unfiltered (f, "constant: %s",
22725 plongest (DW_SND (&die->attrs[i])));
22726 break;
22727 default:
22728 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22729 die->attrs[i].form);
22730 break;
22731 }
22732 fprintf_unfiltered (f, "\n");
22733 }
22734 }
22735
22736 static void
22737 dump_die_for_error (struct die_info *die)
22738 {
22739 dump_die_shallow (gdb_stderr, 0, die);
22740 }
22741
22742 static void
22743 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22744 {
22745 int indent = level * 4;
22746
22747 gdb_assert (die != NULL);
22748
22749 if (level >= max_level)
22750 return;
22751
22752 dump_die_shallow (f, indent, die);
22753
22754 if (die->child != NULL)
22755 {
22756 print_spaces (indent, f);
22757 fprintf_unfiltered (f, " Children:");
22758 if (level + 1 < max_level)
22759 {
22760 fprintf_unfiltered (f, "\n");
22761 dump_die_1 (f, level + 1, max_level, die->child);
22762 }
22763 else
22764 {
22765 fprintf_unfiltered (f,
22766 " [not printed, max nesting level reached]\n");
22767 }
22768 }
22769
22770 if (die->sibling != NULL && level > 0)
22771 {
22772 dump_die_1 (f, level, max_level, die->sibling);
22773 }
22774 }
22775
22776 /* This is called from the pdie macro in gdbinit.in.
22777 It's not static so gcc will keep a copy callable from gdb. */
22778
22779 void
22780 dump_die (struct die_info *die, int max_level)
22781 {
22782 dump_die_1 (gdb_stdlog, 0, max_level, die);
22783 }
22784
22785 static void
22786 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22787 {
22788 void **slot;
22789
22790 slot = htab_find_slot_with_hash (cu->die_hash, die,
22791 to_underlying (die->sect_off),
22792 INSERT);
22793
22794 *slot = die;
22795 }
22796
22797 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22798 required kind. */
22799
22800 static sect_offset
22801 dwarf2_get_ref_die_offset (const struct attribute *attr)
22802 {
22803 if (attr_form_is_ref (attr))
22804 return (sect_offset) DW_UNSND (attr);
22805
22806 complaint (&symfile_complaints,
22807 _("unsupported die ref attribute form: '%s'"),
22808 dwarf_form_name (attr->form));
22809 return {};
22810 }
22811
22812 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22813 * the value held by the attribute is not constant. */
22814
22815 static LONGEST
22816 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
22817 {
22818 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
22819 return DW_SND (attr);
22820 else if (attr->form == DW_FORM_udata
22821 || attr->form == DW_FORM_data1
22822 || attr->form == DW_FORM_data2
22823 || attr->form == DW_FORM_data4
22824 || attr->form == DW_FORM_data8)
22825 return DW_UNSND (attr);
22826 else
22827 {
22828 /* For DW_FORM_data16 see attr_form_is_constant. */
22829 complaint (&symfile_complaints,
22830 _("Attribute value is not a constant (%s)"),
22831 dwarf_form_name (attr->form));
22832 return default_value;
22833 }
22834 }
22835
22836 /* Follow reference or signature attribute ATTR of SRC_DIE.
22837 On entry *REF_CU is the CU of SRC_DIE.
22838 On exit *REF_CU is the CU of the result. */
22839
22840 static struct die_info *
22841 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22842 struct dwarf2_cu **ref_cu)
22843 {
22844 struct die_info *die;
22845
22846 if (attr_form_is_ref (attr))
22847 die = follow_die_ref (src_die, attr, ref_cu);
22848 else if (attr->form == DW_FORM_ref_sig8)
22849 die = follow_die_sig (src_die, attr, ref_cu);
22850 else
22851 {
22852 dump_die_for_error (src_die);
22853 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22854 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22855 }
22856
22857 return die;
22858 }
22859
22860 /* Follow reference OFFSET.
22861 On entry *REF_CU is the CU of the source die referencing OFFSET.
22862 On exit *REF_CU is the CU of the result.
22863 Returns NULL if OFFSET is invalid. */
22864
22865 static struct die_info *
22866 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22867 struct dwarf2_cu **ref_cu)
22868 {
22869 struct die_info temp_die;
22870 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22871 struct dwarf2_per_objfile *dwarf2_per_objfile
22872 = cu->per_cu->dwarf2_per_objfile;
22873
22874 gdb_assert (cu->per_cu != NULL);
22875
22876 target_cu = cu;
22877
22878 if (cu->per_cu->is_debug_types)
22879 {
22880 /* .debug_types CUs cannot reference anything outside their CU.
22881 If they need to, they have to reference a signatured type via
22882 DW_FORM_ref_sig8. */
22883 if (!offset_in_cu_p (&cu->header, sect_off))
22884 return NULL;
22885 }
22886 else if (offset_in_dwz != cu->per_cu->is_dwz
22887 || !offset_in_cu_p (&cu->header, sect_off))
22888 {
22889 struct dwarf2_per_cu_data *per_cu;
22890
22891 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22892 dwarf2_per_objfile);
22893
22894 /* If necessary, add it to the queue and load its DIEs. */
22895 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
22896 load_full_comp_unit (per_cu, false, cu->language);
22897
22898 target_cu = per_cu->cu;
22899 }
22900 else if (cu->dies == NULL)
22901 {
22902 /* We're loading full DIEs during partial symbol reading. */
22903 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
22904 load_full_comp_unit (cu->per_cu, false, language_minimal);
22905 }
22906
22907 *ref_cu = target_cu;
22908 temp_die.sect_off = sect_off;
22909 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22910 &temp_die,
22911 to_underlying (sect_off));
22912 }
22913
22914 /* Follow reference attribute ATTR of SRC_DIE.
22915 On entry *REF_CU is the CU of SRC_DIE.
22916 On exit *REF_CU is the CU of the result. */
22917
22918 static struct die_info *
22919 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22920 struct dwarf2_cu **ref_cu)
22921 {
22922 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22923 struct dwarf2_cu *cu = *ref_cu;
22924 struct die_info *die;
22925
22926 die = follow_die_offset (sect_off,
22927 (attr->form == DW_FORM_GNU_ref_alt
22928 || cu->per_cu->is_dwz),
22929 ref_cu);
22930 if (!die)
22931 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22932 "at %s [in module %s]"),
22933 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22934 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
22935
22936 return die;
22937 }
22938
22939 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
22940 Returned value is intended for DW_OP_call*. Returned
22941 dwarf2_locexpr_baton->data has lifetime of
22942 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
22943
22944 struct dwarf2_locexpr_baton
22945 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22946 struct dwarf2_per_cu_data *per_cu,
22947 CORE_ADDR (*get_frame_pc) (void *baton),
22948 void *baton)
22949 {
22950 struct dwarf2_cu *cu;
22951 struct die_info *die;
22952 struct attribute *attr;
22953 struct dwarf2_locexpr_baton retval;
22954 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22955 struct objfile *objfile = dwarf2_per_objfile->objfile;
22956
22957 if (per_cu->cu == NULL)
22958 load_cu (per_cu, false);
22959 cu = per_cu->cu;
22960 if (cu == NULL)
22961 {
22962 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22963 Instead just throw an error, not much else we can do. */
22964 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22965 sect_offset_str (sect_off), objfile_name (objfile));
22966 }
22967
22968 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22969 if (!die)
22970 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22971 sect_offset_str (sect_off), objfile_name (objfile));
22972
22973 attr = dwarf2_attr (die, DW_AT_location, cu);
22974 if (!attr)
22975 {
22976 /* DWARF: "If there is no such attribute, then there is no effect.".
22977 DATA is ignored if SIZE is 0. */
22978
22979 retval.data = NULL;
22980 retval.size = 0;
22981 }
22982 else if (attr_form_is_section_offset (attr))
22983 {
22984 struct dwarf2_loclist_baton loclist_baton;
22985 CORE_ADDR pc = (*get_frame_pc) (baton);
22986 size_t size;
22987
22988 fill_in_loclist_baton (cu, &loclist_baton, attr);
22989
22990 retval.data = dwarf2_find_location_expression (&loclist_baton,
22991 &size, pc);
22992 retval.size = size;
22993 }
22994 else
22995 {
22996 if (!attr_form_is_block (attr))
22997 error (_("Dwarf Error: DIE at %s referenced in module %s "
22998 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22999 sect_offset_str (sect_off), objfile_name (objfile));
23000
23001 retval.data = DW_BLOCK (attr)->data;
23002 retval.size = DW_BLOCK (attr)->size;
23003 }
23004 retval.per_cu = cu->per_cu;
23005
23006 age_cached_comp_units (dwarf2_per_objfile);
23007
23008 return retval;
23009 }
23010
23011 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23012 offset. */
23013
23014 struct dwarf2_locexpr_baton
23015 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23016 struct dwarf2_per_cu_data *per_cu,
23017 CORE_ADDR (*get_frame_pc) (void *baton),
23018 void *baton)
23019 {
23020 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23021
23022 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
23023 }
23024
23025 /* Write a constant of a given type as target-ordered bytes into
23026 OBSTACK. */
23027
23028 static const gdb_byte *
23029 write_constant_as_bytes (struct obstack *obstack,
23030 enum bfd_endian byte_order,
23031 struct type *type,
23032 ULONGEST value,
23033 LONGEST *len)
23034 {
23035 gdb_byte *result;
23036
23037 *len = TYPE_LENGTH (type);
23038 result = (gdb_byte *) obstack_alloc (obstack, *len);
23039 store_unsigned_integer (result, *len, byte_order, value);
23040
23041 return result;
23042 }
23043
23044 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23045 pointer to the constant bytes and set LEN to the length of the
23046 data. If memory is needed, allocate it on OBSTACK. If the DIE
23047 does not have a DW_AT_const_value, return NULL. */
23048
23049 const gdb_byte *
23050 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23051 struct dwarf2_per_cu_data *per_cu,
23052 struct obstack *obstack,
23053 LONGEST *len)
23054 {
23055 struct dwarf2_cu *cu;
23056 struct die_info *die;
23057 struct attribute *attr;
23058 const gdb_byte *result = NULL;
23059 struct type *type;
23060 LONGEST value;
23061 enum bfd_endian byte_order;
23062 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23063
23064 if (per_cu->cu == NULL)
23065 load_cu (per_cu, false);
23066 cu = per_cu->cu;
23067 if (cu == NULL)
23068 {
23069 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23070 Instead just throw an error, not much else we can do. */
23071 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23072 sect_offset_str (sect_off), objfile_name (objfile));
23073 }
23074
23075 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23076 if (!die)
23077 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23078 sect_offset_str (sect_off), objfile_name (objfile));
23079
23080 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23081 if (attr == NULL)
23082 return NULL;
23083
23084 byte_order = (bfd_big_endian (objfile->obfd)
23085 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23086
23087 switch (attr->form)
23088 {
23089 case DW_FORM_addr:
23090 case DW_FORM_GNU_addr_index:
23091 {
23092 gdb_byte *tem;
23093
23094 *len = cu->header.addr_size;
23095 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23096 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23097 result = tem;
23098 }
23099 break;
23100 case DW_FORM_string:
23101 case DW_FORM_strp:
23102 case DW_FORM_GNU_str_index:
23103 case DW_FORM_GNU_strp_alt:
23104 /* DW_STRING is already allocated on the objfile obstack, point
23105 directly to it. */
23106 result = (const gdb_byte *) DW_STRING (attr);
23107 *len = strlen (DW_STRING (attr));
23108 break;
23109 case DW_FORM_block1:
23110 case DW_FORM_block2:
23111 case DW_FORM_block4:
23112 case DW_FORM_block:
23113 case DW_FORM_exprloc:
23114 case DW_FORM_data16:
23115 result = DW_BLOCK (attr)->data;
23116 *len = DW_BLOCK (attr)->size;
23117 break;
23118
23119 /* The DW_AT_const_value attributes are supposed to carry the
23120 symbol's value "represented as it would be on the target
23121 architecture." By the time we get here, it's already been
23122 converted to host endianness, so we just need to sign- or
23123 zero-extend it as appropriate. */
23124 case DW_FORM_data1:
23125 type = die_type (die, cu);
23126 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23127 if (result == NULL)
23128 result = write_constant_as_bytes (obstack, byte_order,
23129 type, value, len);
23130 break;
23131 case DW_FORM_data2:
23132 type = die_type (die, cu);
23133 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23134 if (result == NULL)
23135 result = write_constant_as_bytes (obstack, byte_order,
23136 type, value, len);
23137 break;
23138 case DW_FORM_data4:
23139 type = die_type (die, cu);
23140 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23141 if (result == NULL)
23142 result = write_constant_as_bytes (obstack, byte_order,
23143 type, value, len);
23144 break;
23145 case DW_FORM_data8:
23146 type = die_type (die, cu);
23147 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23148 if (result == NULL)
23149 result = write_constant_as_bytes (obstack, byte_order,
23150 type, value, len);
23151 break;
23152
23153 case DW_FORM_sdata:
23154 case DW_FORM_implicit_const:
23155 type = die_type (die, cu);
23156 result = write_constant_as_bytes (obstack, byte_order,
23157 type, DW_SND (attr), len);
23158 break;
23159
23160 case DW_FORM_udata:
23161 type = die_type (die, cu);
23162 result = write_constant_as_bytes (obstack, byte_order,
23163 type, DW_UNSND (attr), len);
23164 break;
23165
23166 default:
23167 complaint (&symfile_complaints,
23168 _("unsupported const value attribute form: '%s'"),
23169 dwarf_form_name (attr->form));
23170 break;
23171 }
23172
23173 return result;
23174 }
23175
23176 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23177 valid type for this die is found. */
23178
23179 struct type *
23180 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23181 struct dwarf2_per_cu_data *per_cu)
23182 {
23183 struct dwarf2_cu *cu;
23184 struct die_info *die;
23185
23186 if (per_cu->cu == NULL)
23187 load_cu (per_cu, false);
23188 cu = per_cu->cu;
23189 if (!cu)
23190 return NULL;
23191
23192 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23193 if (!die)
23194 return NULL;
23195
23196 return die_type (die, cu);
23197 }
23198
23199 /* Return the type of the DIE at DIE_OFFSET in the CU named by
23200 PER_CU. */
23201
23202 struct type *
23203 dwarf2_get_die_type (cu_offset die_offset,
23204 struct dwarf2_per_cu_data *per_cu)
23205 {
23206 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23207 return get_die_type_at_offset (die_offset_sect, per_cu);
23208 }
23209
23210 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23211 On entry *REF_CU is the CU of SRC_DIE.
23212 On exit *REF_CU is the CU of the result.
23213 Returns NULL if the referenced DIE isn't found. */
23214
23215 static struct die_info *
23216 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23217 struct dwarf2_cu **ref_cu)
23218 {
23219 struct die_info temp_die;
23220 struct dwarf2_cu *sig_cu;
23221 struct die_info *die;
23222
23223 /* While it might be nice to assert sig_type->type == NULL here,
23224 we can get here for DW_AT_imported_declaration where we need
23225 the DIE not the type. */
23226
23227 /* If necessary, add it to the queue and load its DIEs. */
23228
23229 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
23230 read_signatured_type (sig_type);
23231
23232 sig_cu = sig_type->per_cu.cu;
23233 gdb_assert (sig_cu != NULL);
23234 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23235 temp_die.sect_off = sig_type->type_offset_in_section;
23236 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23237 to_underlying (temp_die.sect_off));
23238 if (die)
23239 {
23240 struct dwarf2_per_objfile *dwarf2_per_objfile
23241 = (*ref_cu)->per_cu->dwarf2_per_objfile;
23242
23243 /* For .gdb_index version 7 keep track of included TUs.
23244 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23245 if (dwarf2_per_objfile->index_table != NULL
23246 && dwarf2_per_objfile->index_table->version <= 7)
23247 {
23248 VEC_safe_push (dwarf2_per_cu_ptr,
23249 (*ref_cu)->per_cu->imported_symtabs,
23250 sig_cu->per_cu);
23251 }
23252
23253 *ref_cu = sig_cu;
23254 return die;
23255 }
23256
23257 return NULL;
23258 }
23259
23260 /* Follow signatured type referenced by ATTR in SRC_DIE.
23261 On entry *REF_CU is the CU of SRC_DIE.
23262 On exit *REF_CU is the CU of the result.
23263 The result is the DIE of the type.
23264 If the referenced type cannot be found an error is thrown. */
23265
23266 static struct die_info *
23267 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23268 struct dwarf2_cu **ref_cu)
23269 {
23270 ULONGEST signature = DW_SIGNATURE (attr);
23271 struct signatured_type *sig_type;
23272 struct die_info *die;
23273
23274 gdb_assert (attr->form == DW_FORM_ref_sig8);
23275
23276 sig_type = lookup_signatured_type (*ref_cu, signature);
23277 /* sig_type will be NULL if the signatured type is missing from
23278 the debug info. */
23279 if (sig_type == NULL)
23280 {
23281 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23282 " from DIE at %s [in module %s]"),
23283 hex_string (signature), sect_offset_str (src_die->sect_off),
23284 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23285 }
23286
23287 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23288 if (die == NULL)
23289 {
23290 dump_die_for_error (src_die);
23291 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23292 " from DIE at %s [in module %s]"),
23293 hex_string (signature), sect_offset_str (src_die->sect_off),
23294 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23295 }
23296
23297 return die;
23298 }
23299
23300 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23301 reading in and processing the type unit if necessary. */
23302
23303 static struct type *
23304 get_signatured_type (struct die_info *die, ULONGEST signature,
23305 struct dwarf2_cu *cu)
23306 {
23307 struct dwarf2_per_objfile *dwarf2_per_objfile
23308 = cu->per_cu->dwarf2_per_objfile;
23309 struct signatured_type *sig_type;
23310 struct dwarf2_cu *type_cu;
23311 struct die_info *type_die;
23312 struct type *type;
23313
23314 sig_type = lookup_signatured_type (cu, signature);
23315 /* sig_type will be NULL if the signatured type is missing from
23316 the debug info. */
23317 if (sig_type == NULL)
23318 {
23319 complaint (&symfile_complaints,
23320 _("Dwarf Error: Cannot find signatured DIE %s referenced"
23321 " from DIE at %s [in module %s]"),
23322 hex_string (signature), sect_offset_str (die->sect_off),
23323 objfile_name (dwarf2_per_objfile->objfile));
23324 return build_error_marker_type (cu, die);
23325 }
23326
23327 /* If we already know the type we're done. */
23328 if (sig_type->type != NULL)
23329 return sig_type->type;
23330
23331 type_cu = cu;
23332 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23333 if (type_die != NULL)
23334 {
23335 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23336 is created. This is important, for example, because for c++ classes
23337 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23338 type = read_type_die (type_die, type_cu);
23339 if (type == NULL)
23340 {
23341 complaint (&symfile_complaints,
23342 _("Dwarf Error: Cannot build signatured type %s"
23343 " referenced from DIE at %s [in module %s]"),
23344 hex_string (signature), sect_offset_str (die->sect_off),
23345 objfile_name (dwarf2_per_objfile->objfile));
23346 type = build_error_marker_type (cu, die);
23347 }
23348 }
23349 else
23350 {
23351 complaint (&symfile_complaints,
23352 _("Dwarf Error: Problem reading signatured DIE %s referenced"
23353 " from DIE at %s [in module %s]"),
23354 hex_string (signature), sect_offset_str (die->sect_off),
23355 objfile_name (dwarf2_per_objfile->objfile));
23356 type = build_error_marker_type (cu, die);
23357 }
23358 sig_type->type = type;
23359
23360 return type;
23361 }
23362
23363 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23364 reading in and processing the type unit if necessary. */
23365
23366 static struct type *
23367 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23368 struct dwarf2_cu *cu) /* ARI: editCase function */
23369 {
23370 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23371 if (attr_form_is_ref (attr))
23372 {
23373 struct dwarf2_cu *type_cu = cu;
23374 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23375
23376 return read_type_die (type_die, type_cu);
23377 }
23378 else if (attr->form == DW_FORM_ref_sig8)
23379 {
23380 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23381 }
23382 else
23383 {
23384 struct dwarf2_per_objfile *dwarf2_per_objfile
23385 = cu->per_cu->dwarf2_per_objfile;
23386
23387 complaint (&symfile_complaints,
23388 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23389 " at %s [in module %s]"),
23390 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
23391 objfile_name (dwarf2_per_objfile->objfile));
23392 return build_error_marker_type (cu, die);
23393 }
23394 }
23395
23396 /* Load the DIEs associated with type unit PER_CU into memory. */
23397
23398 static void
23399 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
23400 {
23401 struct signatured_type *sig_type;
23402
23403 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23404 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23405
23406 /* We have the per_cu, but we need the signatured_type.
23407 Fortunately this is an easy translation. */
23408 gdb_assert (per_cu->is_debug_types);
23409 sig_type = (struct signatured_type *) per_cu;
23410
23411 gdb_assert (per_cu->cu == NULL);
23412
23413 read_signatured_type (sig_type);
23414
23415 gdb_assert (per_cu->cu != NULL);
23416 }
23417
23418 /* die_reader_func for read_signatured_type.
23419 This is identical to load_full_comp_unit_reader,
23420 but is kept separate for now. */
23421
23422 static void
23423 read_signatured_type_reader (const struct die_reader_specs *reader,
23424 const gdb_byte *info_ptr,
23425 struct die_info *comp_unit_die,
23426 int has_children,
23427 void *data)
23428 {
23429 struct dwarf2_cu *cu = reader->cu;
23430
23431 gdb_assert (cu->die_hash == NULL);
23432 cu->die_hash =
23433 htab_create_alloc_ex (cu->header.length / 12,
23434 die_hash,
23435 die_eq,
23436 NULL,
23437 &cu->comp_unit_obstack,
23438 hashtab_obstack_allocate,
23439 dummy_obstack_deallocate);
23440
23441 if (has_children)
23442 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23443 &info_ptr, comp_unit_die);
23444 cu->dies = comp_unit_die;
23445 /* comp_unit_die is not stored in die_hash, no need. */
23446
23447 /* We try not to read any attributes in this function, because not
23448 all CUs needed for references have been loaded yet, and symbol
23449 table processing isn't initialized. But we have to set the CU language,
23450 or we won't be able to build types correctly.
23451 Similarly, if we do not read the producer, we can not apply
23452 producer-specific interpretation. */
23453 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23454 }
23455
23456 /* Read in a signatured type and build its CU and DIEs.
23457 If the type is a stub for the real type in a DWO file,
23458 read in the real type from the DWO file as well. */
23459
23460 static void
23461 read_signatured_type (struct signatured_type *sig_type)
23462 {
23463 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
23464
23465 gdb_assert (per_cu->is_debug_types);
23466 gdb_assert (per_cu->cu == NULL);
23467
23468 init_cutu_and_read_dies (per_cu, NULL, 0, 1, false,
23469 read_signatured_type_reader, NULL);
23470 sig_type->per_cu.tu_read = 1;
23471 }
23472
23473 /* Decode simple location descriptions.
23474 Given a pointer to a dwarf block that defines a location, compute
23475 the location and return the value.
23476
23477 NOTE drow/2003-11-18: This function is called in two situations
23478 now: for the address of static or global variables (partial symbols
23479 only) and for offsets into structures which are expected to be
23480 (more or less) constant. The partial symbol case should go away,
23481 and only the constant case should remain. That will let this
23482 function complain more accurately. A few special modes are allowed
23483 without complaint for global variables (for instance, global
23484 register values and thread-local values).
23485
23486 A location description containing no operations indicates that the
23487 object is optimized out. The return value is 0 for that case.
23488 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23489 callers will only want a very basic result and this can become a
23490 complaint.
23491
23492 Note that stack[0] is unused except as a default error return. */
23493
23494 static CORE_ADDR
23495 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
23496 {
23497 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
23498 size_t i;
23499 size_t size = blk->size;
23500 const gdb_byte *data = blk->data;
23501 CORE_ADDR stack[64];
23502 int stacki;
23503 unsigned int bytes_read, unsnd;
23504 gdb_byte op;
23505
23506 i = 0;
23507 stacki = 0;
23508 stack[stacki] = 0;
23509 stack[++stacki] = 0;
23510
23511 while (i < size)
23512 {
23513 op = data[i++];
23514 switch (op)
23515 {
23516 case DW_OP_lit0:
23517 case DW_OP_lit1:
23518 case DW_OP_lit2:
23519 case DW_OP_lit3:
23520 case DW_OP_lit4:
23521 case DW_OP_lit5:
23522 case DW_OP_lit6:
23523 case DW_OP_lit7:
23524 case DW_OP_lit8:
23525 case DW_OP_lit9:
23526 case DW_OP_lit10:
23527 case DW_OP_lit11:
23528 case DW_OP_lit12:
23529 case DW_OP_lit13:
23530 case DW_OP_lit14:
23531 case DW_OP_lit15:
23532 case DW_OP_lit16:
23533 case DW_OP_lit17:
23534 case DW_OP_lit18:
23535 case DW_OP_lit19:
23536 case DW_OP_lit20:
23537 case DW_OP_lit21:
23538 case DW_OP_lit22:
23539 case DW_OP_lit23:
23540 case DW_OP_lit24:
23541 case DW_OP_lit25:
23542 case DW_OP_lit26:
23543 case DW_OP_lit27:
23544 case DW_OP_lit28:
23545 case DW_OP_lit29:
23546 case DW_OP_lit30:
23547 case DW_OP_lit31:
23548 stack[++stacki] = op - DW_OP_lit0;
23549 break;
23550
23551 case DW_OP_reg0:
23552 case DW_OP_reg1:
23553 case DW_OP_reg2:
23554 case DW_OP_reg3:
23555 case DW_OP_reg4:
23556 case DW_OP_reg5:
23557 case DW_OP_reg6:
23558 case DW_OP_reg7:
23559 case DW_OP_reg8:
23560 case DW_OP_reg9:
23561 case DW_OP_reg10:
23562 case DW_OP_reg11:
23563 case DW_OP_reg12:
23564 case DW_OP_reg13:
23565 case DW_OP_reg14:
23566 case DW_OP_reg15:
23567 case DW_OP_reg16:
23568 case DW_OP_reg17:
23569 case DW_OP_reg18:
23570 case DW_OP_reg19:
23571 case DW_OP_reg20:
23572 case DW_OP_reg21:
23573 case DW_OP_reg22:
23574 case DW_OP_reg23:
23575 case DW_OP_reg24:
23576 case DW_OP_reg25:
23577 case DW_OP_reg26:
23578 case DW_OP_reg27:
23579 case DW_OP_reg28:
23580 case DW_OP_reg29:
23581 case DW_OP_reg30:
23582 case DW_OP_reg31:
23583 stack[++stacki] = op - DW_OP_reg0;
23584 if (i < size)
23585 dwarf2_complex_location_expr_complaint ();
23586 break;
23587
23588 case DW_OP_regx:
23589 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23590 i += bytes_read;
23591 stack[++stacki] = unsnd;
23592 if (i < size)
23593 dwarf2_complex_location_expr_complaint ();
23594 break;
23595
23596 case DW_OP_addr:
23597 stack[++stacki] = read_address (objfile->obfd, &data[i],
23598 cu, &bytes_read);
23599 i += bytes_read;
23600 break;
23601
23602 case DW_OP_const1u:
23603 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23604 i += 1;
23605 break;
23606
23607 case DW_OP_const1s:
23608 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23609 i += 1;
23610 break;
23611
23612 case DW_OP_const2u:
23613 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23614 i += 2;
23615 break;
23616
23617 case DW_OP_const2s:
23618 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23619 i += 2;
23620 break;
23621
23622 case DW_OP_const4u:
23623 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23624 i += 4;
23625 break;
23626
23627 case DW_OP_const4s:
23628 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23629 i += 4;
23630 break;
23631
23632 case DW_OP_const8u:
23633 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23634 i += 8;
23635 break;
23636
23637 case DW_OP_constu:
23638 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23639 &bytes_read);
23640 i += bytes_read;
23641 break;
23642
23643 case DW_OP_consts:
23644 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23645 i += bytes_read;
23646 break;
23647
23648 case DW_OP_dup:
23649 stack[stacki + 1] = stack[stacki];
23650 stacki++;
23651 break;
23652
23653 case DW_OP_plus:
23654 stack[stacki - 1] += stack[stacki];
23655 stacki--;
23656 break;
23657
23658 case DW_OP_plus_uconst:
23659 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23660 &bytes_read);
23661 i += bytes_read;
23662 break;
23663
23664 case DW_OP_minus:
23665 stack[stacki - 1] -= stack[stacki];
23666 stacki--;
23667 break;
23668
23669 case DW_OP_deref:
23670 /* If we're not the last op, then we definitely can't encode
23671 this using GDB's address_class enum. This is valid for partial
23672 global symbols, although the variable's address will be bogus
23673 in the psymtab. */
23674 if (i < size)
23675 dwarf2_complex_location_expr_complaint ();
23676 break;
23677
23678 case DW_OP_GNU_push_tls_address:
23679 case DW_OP_form_tls_address:
23680 /* The top of the stack has the offset from the beginning
23681 of the thread control block at which the variable is located. */
23682 /* Nothing should follow this operator, so the top of stack would
23683 be returned. */
23684 /* This is valid for partial global symbols, but the variable's
23685 address will be bogus in the psymtab. Make it always at least
23686 non-zero to not look as a variable garbage collected by linker
23687 which have DW_OP_addr 0. */
23688 if (i < size)
23689 dwarf2_complex_location_expr_complaint ();
23690 stack[stacki]++;
23691 break;
23692
23693 case DW_OP_GNU_uninit:
23694 break;
23695
23696 case DW_OP_GNU_addr_index:
23697 case DW_OP_GNU_const_index:
23698 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23699 &bytes_read);
23700 i += bytes_read;
23701 break;
23702
23703 default:
23704 {
23705 const char *name = get_DW_OP_name (op);
23706
23707 if (name)
23708 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
23709 name);
23710 else
23711 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
23712 op);
23713 }
23714
23715 return (stack[stacki]);
23716 }
23717
23718 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23719 outside of the allocated space. Also enforce minimum>0. */
23720 if (stacki >= ARRAY_SIZE (stack) - 1)
23721 {
23722 complaint (&symfile_complaints,
23723 _("location description stack overflow"));
23724 return 0;
23725 }
23726
23727 if (stacki <= 0)
23728 {
23729 complaint (&symfile_complaints,
23730 _("location description stack underflow"));
23731 return 0;
23732 }
23733 }
23734 return (stack[stacki]);
23735 }
23736
23737 /* memory allocation interface */
23738
23739 static struct dwarf_block *
23740 dwarf_alloc_block (struct dwarf2_cu *cu)
23741 {
23742 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23743 }
23744
23745 static struct die_info *
23746 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23747 {
23748 struct die_info *die;
23749 size_t size = sizeof (struct die_info);
23750
23751 if (num_attrs > 1)
23752 size += (num_attrs - 1) * sizeof (struct attribute);
23753
23754 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23755 memset (die, 0, sizeof (struct die_info));
23756 return (die);
23757 }
23758
23759 \f
23760 /* Macro support. */
23761
23762 /* Return file name relative to the compilation directory of file number I in
23763 *LH's file name table. The result is allocated using xmalloc; the caller is
23764 responsible for freeing it. */
23765
23766 static char *
23767 file_file_name (int file, struct line_header *lh)
23768 {
23769 /* Is the file number a valid index into the line header's file name
23770 table? Remember that file numbers start with one, not zero. */
23771 if (1 <= file && file <= lh->file_names.size ())
23772 {
23773 const file_entry &fe = lh->file_names[file - 1];
23774
23775 if (!IS_ABSOLUTE_PATH (fe.name))
23776 {
23777 const char *dir = fe.include_dir (lh);
23778 if (dir != NULL)
23779 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
23780 }
23781 return xstrdup (fe.name);
23782 }
23783 else
23784 {
23785 /* The compiler produced a bogus file number. We can at least
23786 record the macro definitions made in the file, even if we
23787 won't be able to find the file by name. */
23788 char fake_name[80];
23789
23790 xsnprintf (fake_name, sizeof (fake_name),
23791 "<bad macro file number %d>", file);
23792
23793 complaint (&symfile_complaints,
23794 _("bad file number in macro information (%d)"),
23795 file);
23796
23797 return xstrdup (fake_name);
23798 }
23799 }
23800
23801 /* Return the full name of file number I in *LH's file name table.
23802 Use COMP_DIR as the name of the current directory of the
23803 compilation. The result is allocated using xmalloc; the caller is
23804 responsible for freeing it. */
23805 static char *
23806 file_full_name (int file, struct line_header *lh, const char *comp_dir)
23807 {
23808 /* Is the file number a valid index into the line header's file name
23809 table? Remember that file numbers start with one, not zero. */
23810 if (1 <= file && file <= lh->file_names.size ())
23811 {
23812 char *relative = file_file_name (file, lh);
23813
23814 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
23815 return relative;
23816 return reconcat (relative, comp_dir, SLASH_STRING,
23817 relative, (char *) NULL);
23818 }
23819 else
23820 return file_file_name (file, lh);
23821 }
23822
23823
23824 static struct macro_source_file *
23825 macro_start_file (int file, int line,
23826 struct macro_source_file *current_file,
23827 struct line_header *lh)
23828 {
23829 /* File name relative to the compilation directory of this source file. */
23830 char *file_name = file_file_name (file, lh);
23831
23832 if (! current_file)
23833 {
23834 /* Note: We don't create a macro table for this compilation unit
23835 at all until we actually get a filename. */
23836 struct macro_table *macro_table = get_macro_table ();
23837
23838 /* If we have no current file, then this must be the start_file
23839 directive for the compilation unit's main source file. */
23840 current_file = macro_set_main (macro_table, file_name);
23841 macro_define_special (macro_table);
23842 }
23843 else
23844 current_file = macro_include (current_file, line, file_name);
23845
23846 xfree (file_name);
23847
23848 return current_file;
23849 }
23850
23851 static const char *
23852 consume_improper_spaces (const char *p, const char *body)
23853 {
23854 if (*p == ' ')
23855 {
23856 complaint (&symfile_complaints,
23857 _("macro definition contains spaces "
23858 "in formal argument list:\n`%s'"),
23859 body);
23860
23861 while (*p == ' ')
23862 p++;
23863 }
23864
23865 return p;
23866 }
23867
23868
23869 static void
23870 parse_macro_definition (struct macro_source_file *file, int line,
23871 const char *body)
23872 {
23873 const char *p;
23874
23875 /* The body string takes one of two forms. For object-like macro
23876 definitions, it should be:
23877
23878 <macro name> " " <definition>
23879
23880 For function-like macro definitions, it should be:
23881
23882 <macro name> "() " <definition>
23883 or
23884 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
23885
23886 Spaces may appear only where explicitly indicated, and in the
23887 <definition>.
23888
23889 The Dwarf 2 spec says that an object-like macro's name is always
23890 followed by a space, but versions of GCC around March 2002 omit
23891 the space when the macro's definition is the empty string.
23892
23893 The Dwarf 2 spec says that there should be no spaces between the
23894 formal arguments in a function-like macro's formal argument list,
23895 but versions of GCC around March 2002 include spaces after the
23896 commas. */
23897
23898
23899 /* Find the extent of the macro name. The macro name is terminated
23900 by either a space or null character (for an object-like macro) or
23901 an opening paren (for a function-like macro). */
23902 for (p = body; *p; p++)
23903 if (*p == ' ' || *p == '(')
23904 break;
23905
23906 if (*p == ' ' || *p == '\0')
23907 {
23908 /* It's an object-like macro. */
23909 int name_len = p - body;
23910 char *name = savestring (body, name_len);
23911 const char *replacement;
23912
23913 if (*p == ' ')
23914 replacement = body + name_len + 1;
23915 else
23916 {
23917 dwarf2_macro_malformed_definition_complaint (body);
23918 replacement = body + name_len;
23919 }
23920
23921 macro_define_object (file, line, name, replacement);
23922
23923 xfree (name);
23924 }
23925 else if (*p == '(')
23926 {
23927 /* It's a function-like macro. */
23928 char *name = savestring (body, p - body);
23929 int argc = 0;
23930 int argv_size = 1;
23931 char **argv = XNEWVEC (char *, argv_size);
23932
23933 p++;
23934
23935 p = consume_improper_spaces (p, body);
23936
23937 /* Parse the formal argument list. */
23938 while (*p && *p != ')')
23939 {
23940 /* Find the extent of the current argument name. */
23941 const char *arg_start = p;
23942
23943 while (*p && *p != ',' && *p != ')' && *p != ' ')
23944 p++;
23945
23946 if (! *p || p == arg_start)
23947 dwarf2_macro_malformed_definition_complaint (body);
23948 else
23949 {
23950 /* Make sure argv has room for the new argument. */
23951 if (argc >= argv_size)
23952 {
23953 argv_size *= 2;
23954 argv = XRESIZEVEC (char *, argv, argv_size);
23955 }
23956
23957 argv[argc++] = savestring (arg_start, p - arg_start);
23958 }
23959
23960 p = consume_improper_spaces (p, body);
23961
23962 /* Consume the comma, if present. */
23963 if (*p == ',')
23964 {
23965 p++;
23966
23967 p = consume_improper_spaces (p, body);
23968 }
23969 }
23970
23971 if (*p == ')')
23972 {
23973 p++;
23974
23975 if (*p == ' ')
23976 /* Perfectly formed definition, no complaints. */
23977 macro_define_function (file, line, name,
23978 argc, (const char **) argv,
23979 p + 1);
23980 else if (*p == '\0')
23981 {
23982 /* Complain, but do define it. */
23983 dwarf2_macro_malformed_definition_complaint (body);
23984 macro_define_function (file, line, name,
23985 argc, (const char **) argv,
23986 p);
23987 }
23988 else
23989 /* Just complain. */
23990 dwarf2_macro_malformed_definition_complaint (body);
23991 }
23992 else
23993 /* Just complain. */
23994 dwarf2_macro_malformed_definition_complaint (body);
23995
23996 xfree (name);
23997 {
23998 int i;
23999
24000 for (i = 0; i < argc; i++)
24001 xfree (argv[i]);
24002 }
24003 xfree (argv);
24004 }
24005 else
24006 dwarf2_macro_malformed_definition_complaint (body);
24007 }
24008
24009 /* Skip some bytes from BYTES according to the form given in FORM.
24010 Returns the new pointer. */
24011
24012 static const gdb_byte *
24013 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
24014 enum dwarf_form form,
24015 unsigned int offset_size,
24016 struct dwarf2_section_info *section)
24017 {
24018 unsigned int bytes_read;
24019
24020 switch (form)
24021 {
24022 case DW_FORM_data1:
24023 case DW_FORM_flag:
24024 ++bytes;
24025 break;
24026
24027 case DW_FORM_data2:
24028 bytes += 2;
24029 break;
24030
24031 case DW_FORM_data4:
24032 bytes += 4;
24033 break;
24034
24035 case DW_FORM_data8:
24036 bytes += 8;
24037 break;
24038
24039 case DW_FORM_data16:
24040 bytes += 16;
24041 break;
24042
24043 case DW_FORM_string:
24044 read_direct_string (abfd, bytes, &bytes_read);
24045 bytes += bytes_read;
24046 break;
24047
24048 case DW_FORM_sec_offset:
24049 case DW_FORM_strp:
24050 case DW_FORM_GNU_strp_alt:
24051 bytes += offset_size;
24052 break;
24053
24054 case DW_FORM_block:
24055 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24056 bytes += bytes_read;
24057 break;
24058
24059 case DW_FORM_block1:
24060 bytes += 1 + read_1_byte (abfd, bytes);
24061 break;
24062 case DW_FORM_block2:
24063 bytes += 2 + read_2_bytes (abfd, bytes);
24064 break;
24065 case DW_FORM_block4:
24066 bytes += 4 + read_4_bytes (abfd, bytes);
24067 break;
24068
24069 case DW_FORM_sdata:
24070 case DW_FORM_udata:
24071 case DW_FORM_GNU_addr_index:
24072 case DW_FORM_GNU_str_index:
24073 bytes = gdb_skip_leb128 (bytes, buffer_end);
24074 if (bytes == NULL)
24075 {
24076 dwarf2_section_buffer_overflow_complaint (section);
24077 return NULL;
24078 }
24079 break;
24080
24081 case DW_FORM_implicit_const:
24082 break;
24083
24084 default:
24085 {
24086 complaint (&symfile_complaints,
24087 _("invalid form 0x%x in `%s'"),
24088 form, get_section_name (section));
24089 return NULL;
24090 }
24091 }
24092
24093 return bytes;
24094 }
24095
24096 /* A helper for dwarf_decode_macros that handles skipping an unknown
24097 opcode. Returns an updated pointer to the macro data buffer; or,
24098 on error, issues a complaint and returns NULL. */
24099
24100 static const gdb_byte *
24101 skip_unknown_opcode (unsigned int opcode,
24102 const gdb_byte **opcode_definitions,
24103 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24104 bfd *abfd,
24105 unsigned int offset_size,
24106 struct dwarf2_section_info *section)
24107 {
24108 unsigned int bytes_read, i;
24109 unsigned long arg;
24110 const gdb_byte *defn;
24111
24112 if (opcode_definitions[opcode] == NULL)
24113 {
24114 complaint (&symfile_complaints,
24115 _("unrecognized DW_MACFINO opcode 0x%x"),
24116 opcode);
24117 return NULL;
24118 }
24119
24120 defn = opcode_definitions[opcode];
24121 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24122 defn += bytes_read;
24123
24124 for (i = 0; i < arg; ++i)
24125 {
24126 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24127 (enum dwarf_form) defn[i], offset_size,
24128 section);
24129 if (mac_ptr == NULL)
24130 {
24131 /* skip_form_bytes already issued the complaint. */
24132 return NULL;
24133 }
24134 }
24135
24136 return mac_ptr;
24137 }
24138
24139 /* A helper function which parses the header of a macro section.
24140 If the macro section is the extended (for now called "GNU") type,
24141 then this updates *OFFSET_SIZE. Returns a pointer to just after
24142 the header, or issues a complaint and returns NULL on error. */
24143
24144 static const gdb_byte *
24145 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
24146 bfd *abfd,
24147 const gdb_byte *mac_ptr,
24148 unsigned int *offset_size,
24149 int section_is_gnu)
24150 {
24151 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
24152
24153 if (section_is_gnu)
24154 {
24155 unsigned int version, flags;
24156
24157 version = read_2_bytes (abfd, mac_ptr);
24158 if (version != 4 && version != 5)
24159 {
24160 complaint (&symfile_complaints,
24161 _("unrecognized version `%d' in .debug_macro section"),
24162 version);
24163 return NULL;
24164 }
24165 mac_ptr += 2;
24166
24167 flags = read_1_byte (abfd, mac_ptr);
24168 ++mac_ptr;
24169 *offset_size = (flags & 1) ? 8 : 4;
24170
24171 if ((flags & 2) != 0)
24172 /* We don't need the line table offset. */
24173 mac_ptr += *offset_size;
24174
24175 /* Vendor opcode descriptions. */
24176 if ((flags & 4) != 0)
24177 {
24178 unsigned int i, count;
24179
24180 count = read_1_byte (abfd, mac_ptr);
24181 ++mac_ptr;
24182 for (i = 0; i < count; ++i)
24183 {
24184 unsigned int opcode, bytes_read;
24185 unsigned long arg;
24186
24187 opcode = read_1_byte (abfd, mac_ptr);
24188 ++mac_ptr;
24189 opcode_definitions[opcode] = mac_ptr;
24190 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24191 mac_ptr += bytes_read;
24192 mac_ptr += arg;
24193 }
24194 }
24195 }
24196
24197 return mac_ptr;
24198 }
24199
24200 /* A helper for dwarf_decode_macros that handles the GNU extensions,
24201 including DW_MACRO_import. */
24202
24203 static void
24204 dwarf_decode_macro_bytes (struct dwarf2_per_objfile *dwarf2_per_objfile,
24205 bfd *abfd,
24206 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24207 struct macro_source_file *current_file,
24208 struct line_header *lh,
24209 struct dwarf2_section_info *section,
24210 int section_is_gnu, int section_is_dwz,
24211 unsigned int offset_size,
24212 htab_t include_hash)
24213 {
24214 struct objfile *objfile = dwarf2_per_objfile->objfile;
24215 enum dwarf_macro_record_type macinfo_type;
24216 int at_commandline;
24217 const gdb_byte *opcode_definitions[256];
24218
24219 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24220 &offset_size, section_is_gnu);
24221 if (mac_ptr == NULL)
24222 {
24223 /* We already issued a complaint. */
24224 return;
24225 }
24226
24227 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24228 GDB is still reading the definitions from command line. First
24229 DW_MACINFO_start_file will need to be ignored as it was already executed
24230 to create CURRENT_FILE for the main source holding also the command line
24231 definitions. On first met DW_MACINFO_start_file this flag is reset to
24232 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24233
24234 at_commandline = 1;
24235
24236 do
24237 {
24238 /* Do we at least have room for a macinfo type byte? */
24239 if (mac_ptr >= mac_end)
24240 {
24241 dwarf2_section_buffer_overflow_complaint (section);
24242 break;
24243 }
24244
24245 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24246 mac_ptr++;
24247
24248 /* Note that we rely on the fact that the corresponding GNU and
24249 DWARF constants are the same. */
24250 DIAGNOSTIC_PUSH
24251 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24252 switch (macinfo_type)
24253 {
24254 /* A zero macinfo type indicates the end of the macro
24255 information. */
24256 case 0:
24257 break;
24258
24259 case DW_MACRO_define:
24260 case DW_MACRO_undef:
24261 case DW_MACRO_define_strp:
24262 case DW_MACRO_undef_strp:
24263 case DW_MACRO_define_sup:
24264 case DW_MACRO_undef_sup:
24265 {
24266 unsigned int bytes_read;
24267 int line;
24268 const char *body;
24269 int is_define;
24270
24271 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24272 mac_ptr += bytes_read;
24273
24274 if (macinfo_type == DW_MACRO_define
24275 || macinfo_type == DW_MACRO_undef)
24276 {
24277 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24278 mac_ptr += bytes_read;
24279 }
24280 else
24281 {
24282 LONGEST str_offset;
24283
24284 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24285 mac_ptr += offset_size;
24286
24287 if (macinfo_type == DW_MACRO_define_sup
24288 || macinfo_type == DW_MACRO_undef_sup
24289 || section_is_dwz)
24290 {
24291 struct dwz_file *dwz
24292 = dwarf2_get_dwz_file (dwarf2_per_objfile);
24293
24294 body = read_indirect_string_from_dwz (objfile,
24295 dwz, str_offset);
24296 }
24297 else
24298 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24299 abfd, str_offset);
24300 }
24301
24302 is_define = (macinfo_type == DW_MACRO_define
24303 || macinfo_type == DW_MACRO_define_strp
24304 || macinfo_type == DW_MACRO_define_sup);
24305 if (! current_file)
24306 {
24307 /* DWARF violation as no main source is present. */
24308 complaint (&symfile_complaints,
24309 _("debug info with no main source gives macro %s "
24310 "on line %d: %s"),
24311 is_define ? _("definition") : _("undefinition"),
24312 line, body);
24313 break;
24314 }
24315 if ((line == 0 && !at_commandline)
24316 || (line != 0 && at_commandline))
24317 complaint (&symfile_complaints,
24318 _("debug info gives %s macro %s with %s line %d: %s"),
24319 at_commandline ? _("command-line") : _("in-file"),
24320 is_define ? _("definition") : _("undefinition"),
24321 line == 0 ? _("zero") : _("non-zero"), line, body);
24322
24323 if (is_define)
24324 parse_macro_definition (current_file, line, body);
24325 else
24326 {
24327 gdb_assert (macinfo_type == DW_MACRO_undef
24328 || macinfo_type == DW_MACRO_undef_strp
24329 || macinfo_type == DW_MACRO_undef_sup);
24330 macro_undef (current_file, line, body);
24331 }
24332 }
24333 break;
24334
24335 case DW_MACRO_start_file:
24336 {
24337 unsigned int bytes_read;
24338 int line, file;
24339
24340 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24341 mac_ptr += bytes_read;
24342 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24343 mac_ptr += bytes_read;
24344
24345 if ((line == 0 && !at_commandline)
24346 || (line != 0 && at_commandline))
24347 complaint (&symfile_complaints,
24348 _("debug info gives source %d included "
24349 "from %s at %s line %d"),
24350 file, at_commandline ? _("command-line") : _("file"),
24351 line == 0 ? _("zero") : _("non-zero"), line);
24352
24353 if (at_commandline)
24354 {
24355 /* This DW_MACRO_start_file was executed in the
24356 pass one. */
24357 at_commandline = 0;
24358 }
24359 else
24360 current_file = macro_start_file (file, line, current_file, lh);
24361 }
24362 break;
24363
24364 case DW_MACRO_end_file:
24365 if (! current_file)
24366 complaint (&symfile_complaints,
24367 _("macro debug info has an unmatched "
24368 "`close_file' directive"));
24369 else
24370 {
24371 current_file = current_file->included_by;
24372 if (! current_file)
24373 {
24374 enum dwarf_macro_record_type next_type;
24375
24376 /* GCC circa March 2002 doesn't produce the zero
24377 type byte marking the end of the compilation
24378 unit. Complain if it's not there, but exit no
24379 matter what. */
24380
24381 /* Do we at least have room for a macinfo type byte? */
24382 if (mac_ptr >= mac_end)
24383 {
24384 dwarf2_section_buffer_overflow_complaint (section);
24385 return;
24386 }
24387
24388 /* We don't increment mac_ptr here, so this is just
24389 a look-ahead. */
24390 next_type
24391 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24392 mac_ptr);
24393 if (next_type != 0)
24394 complaint (&symfile_complaints,
24395 _("no terminating 0-type entry for "
24396 "macros in `.debug_macinfo' section"));
24397
24398 return;
24399 }
24400 }
24401 break;
24402
24403 case DW_MACRO_import:
24404 case DW_MACRO_import_sup:
24405 {
24406 LONGEST offset;
24407 void **slot;
24408 bfd *include_bfd = abfd;
24409 struct dwarf2_section_info *include_section = section;
24410 const gdb_byte *include_mac_end = mac_end;
24411 int is_dwz = section_is_dwz;
24412 const gdb_byte *new_mac_ptr;
24413
24414 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24415 mac_ptr += offset_size;
24416
24417 if (macinfo_type == DW_MACRO_import_sup)
24418 {
24419 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
24420
24421 dwarf2_read_section (objfile, &dwz->macro);
24422
24423 include_section = &dwz->macro;
24424 include_bfd = get_section_bfd_owner (include_section);
24425 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24426 is_dwz = 1;
24427 }
24428
24429 new_mac_ptr = include_section->buffer + offset;
24430 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24431
24432 if (*slot != NULL)
24433 {
24434 /* This has actually happened; see
24435 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
24436 complaint (&symfile_complaints,
24437 _("recursive DW_MACRO_import in "
24438 ".debug_macro section"));
24439 }
24440 else
24441 {
24442 *slot = (void *) new_mac_ptr;
24443
24444 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24445 include_bfd, new_mac_ptr,
24446 include_mac_end, current_file, lh,
24447 section, section_is_gnu, is_dwz,
24448 offset_size, include_hash);
24449
24450 htab_remove_elt (include_hash, (void *) new_mac_ptr);
24451 }
24452 }
24453 break;
24454
24455 case DW_MACINFO_vendor_ext:
24456 if (!section_is_gnu)
24457 {
24458 unsigned int bytes_read;
24459
24460 /* This reads the constant, but since we don't recognize
24461 any vendor extensions, we ignore it. */
24462 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24463 mac_ptr += bytes_read;
24464 read_direct_string (abfd, mac_ptr, &bytes_read);
24465 mac_ptr += bytes_read;
24466
24467 /* We don't recognize any vendor extensions. */
24468 break;
24469 }
24470 /* FALLTHROUGH */
24471
24472 default:
24473 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24474 mac_ptr, mac_end, abfd, offset_size,
24475 section);
24476 if (mac_ptr == NULL)
24477 return;
24478 break;
24479 }
24480 DIAGNOSTIC_POP
24481 } while (macinfo_type != 0);
24482 }
24483
24484 static void
24485 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24486 int section_is_gnu)
24487 {
24488 struct dwarf2_per_objfile *dwarf2_per_objfile
24489 = cu->per_cu->dwarf2_per_objfile;
24490 struct objfile *objfile = dwarf2_per_objfile->objfile;
24491 struct line_header *lh = cu->line_header;
24492 bfd *abfd;
24493 const gdb_byte *mac_ptr, *mac_end;
24494 struct macro_source_file *current_file = 0;
24495 enum dwarf_macro_record_type macinfo_type;
24496 unsigned int offset_size = cu->header.offset_size;
24497 const gdb_byte *opcode_definitions[256];
24498 void **slot;
24499 struct dwarf2_section_info *section;
24500 const char *section_name;
24501
24502 if (cu->dwo_unit != NULL)
24503 {
24504 if (section_is_gnu)
24505 {
24506 section = &cu->dwo_unit->dwo_file->sections.macro;
24507 section_name = ".debug_macro.dwo";
24508 }
24509 else
24510 {
24511 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24512 section_name = ".debug_macinfo.dwo";
24513 }
24514 }
24515 else
24516 {
24517 if (section_is_gnu)
24518 {
24519 section = &dwarf2_per_objfile->macro;
24520 section_name = ".debug_macro";
24521 }
24522 else
24523 {
24524 section = &dwarf2_per_objfile->macinfo;
24525 section_name = ".debug_macinfo";
24526 }
24527 }
24528
24529 dwarf2_read_section (objfile, section);
24530 if (section->buffer == NULL)
24531 {
24532 complaint (&symfile_complaints, _("missing %s section"), section_name);
24533 return;
24534 }
24535 abfd = get_section_bfd_owner (section);
24536
24537 /* First pass: Find the name of the base filename.
24538 This filename is needed in order to process all macros whose definition
24539 (or undefinition) comes from the command line. These macros are defined
24540 before the first DW_MACINFO_start_file entry, and yet still need to be
24541 associated to the base file.
24542
24543 To determine the base file name, we scan the macro definitions until we
24544 reach the first DW_MACINFO_start_file entry. We then initialize
24545 CURRENT_FILE accordingly so that any macro definition found before the
24546 first DW_MACINFO_start_file can still be associated to the base file. */
24547
24548 mac_ptr = section->buffer + offset;
24549 mac_end = section->buffer + section->size;
24550
24551 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24552 &offset_size, section_is_gnu);
24553 if (mac_ptr == NULL)
24554 {
24555 /* We already issued a complaint. */
24556 return;
24557 }
24558
24559 do
24560 {
24561 /* Do we at least have room for a macinfo type byte? */
24562 if (mac_ptr >= mac_end)
24563 {
24564 /* Complaint is printed during the second pass as GDB will probably
24565 stop the first pass earlier upon finding
24566 DW_MACINFO_start_file. */
24567 break;
24568 }
24569
24570 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24571 mac_ptr++;
24572
24573 /* Note that we rely on the fact that the corresponding GNU and
24574 DWARF constants are the same. */
24575 DIAGNOSTIC_PUSH
24576 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24577 switch (macinfo_type)
24578 {
24579 /* A zero macinfo type indicates the end of the macro
24580 information. */
24581 case 0:
24582 break;
24583
24584 case DW_MACRO_define:
24585 case DW_MACRO_undef:
24586 /* Only skip the data by MAC_PTR. */
24587 {
24588 unsigned int bytes_read;
24589
24590 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24591 mac_ptr += bytes_read;
24592 read_direct_string (abfd, mac_ptr, &bytes_read);
24593 mac_ptr += bytes_read;
24594 }
24595 break;
24596
24597 case DW_MACRO_start_file:
24598 {
24599 unsigned int bytes_read;
24600 int line, file;
24601
24602 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24603 mac_ptr += bytes_read;
24604 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24605 mac_ptr += bytes_read;
24606
24607 current_file = macro_start_file (file, line, current_file, lh);
24608 }
24609 break;
24610
24611 case DW_MACRO_end_file:
24612 /* No data to skip by MAC_PTR. */
24613 break;
24614
24615 case DW_MACRO_define_strp:
24616 case DW_MACRO_undef_strp:
24617 case DW_MACRO_define_sup:
24618 case DW_MACRO_undef_sup:
24619 {
24620 unsigned int bytes_read;
24621
24622 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24623 mac_ptr += bytes_read;
24624 mac_ptr += offset_size;
24625 }
24626 break;
24627
24628 case DW_MACRO_import:
24629 case DW_MACRO_import_sup:
24630 /* Note that, according to the spec, a transparent include
24631 chain cannot call DW_MACRO_start_file. So, we can just
24632 skip this opcode. */
24633 mac_ptr += offset_size;
24634 break;
24635
24636 case DW_MACINFO_vendor_ext:
24637 /* Only skip the data by MAC_PTR. */
24638 if (!section_is_gnu)
24639 {
24640 unsigned int bytes_read;
24641
24642 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24643 mac_ptr += bytes_read;
24644 read_direct_string (abfd, mac_ptr, &bytes_read);
24645 mac_ptr += bytes_read;
24646 }
24647 /* FALLTHROUGH */
24648
24649 default:
24650 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24651 mac_ptr, mac_end, abfd, offset_size,
24652 section);
24653 if (mac_ptr == NULL)
24654 return;
24655 break;
24656 }
24657 DIAGNOSTIC_POP
24658 } while (macinfo_type != 0 && current_file == NULL);
24659
24660 /* Second pass: Process all entries.
24661
24662 Use the AT_COMMAND_LINE flag to determine whether we are still processing
24663 command-line macro definitions/undefinitions. This flag is unset when we
24664 reach the first DW_MACINFO_start_file entry. */
24665
24666 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
24667 htab_eq_pointer,
24668 NULL, xcalloc, xfree));
24669 mac_ptr = section->buffer + offset;
24670 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
24671 *slot = (void *) mac_ptr;
24672 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24673 abfd, mac_ptr, mac_end,
24674 current_file, lh, section,
24675 section_is_gnu, 0, offset_size,
24676 include_hash.get ());
24677 }
24678
24679 /* Check if the attribute's form is a DW_FORM_block*
24680 if so return true else false. */
24681
24682 static int
24683 attr_form_is_block (const struct attribute *attr)
24684 {
24685 return (attr == NULL ? 0 :
24686 attr->form == DW_FORM_block1
24687 || attr->form == DW_FORM_block2
24688 || attr->form == DW_FORM_block4
24689 || attr->form == DW_FORM_block
24690 || attr->form == DW_FORM_exprloc);
24691 }
24692
24693 /* Return non-zero if ATTR's value is a section offset --- classes
24694 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
24695 You may use DW_UNSND (attr) to retrieve such offsets.
24696
24697 Section 7.5.4, "Attribute Encodings", explains that no attribute
24698 may have a value that belongs to more than one of these classes; it
24699 would be ambiguous if we did, because we use the same forms for all
24700 of them. */
24701
24702 static int
24703 attr_form_is_section_offset (const struct attribute *attr)
24704 {
24705 return (attr->form == DW_FORM_data4
24706 || attr->form == DW_FORM_data8
24707 || attr->form == DW_FORM_sec_offset);
24708 }
24709
24710 /* Return non-zero if ATTR's value falls in the 'constant' class, or
24711 zero otherwise. When this function returns true, you can apply
24712 dwarf2_get_attr_constant_value to it.
24713
24714 However, note that for some attributes you must check
24715 attr_form_is_section_offset before using this test. DW_FORM_data4
24716 and DW_FORM_data8 are members of both the constant class, and of
24717 the classes that contain offsets into other debug sections
24718 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
24719 that, if an attribute's can be either a constant or one of the
24720 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
24721 taken as section offsets, not constants.
24722
24723 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
24724 cannot handle that. */
24725
24726 static int
24727 attr_form_is_constant (const struct attribute *attr)
24728 {
24729 switch (attr->form)
24730 {
24731 case DW_FORM_sdata:
24732 case DW_FORM_udata:
24733 case DW_FORM_data1:
24734 case DW_FORM_data2:
24735 case DW_FORM_data4:
24736 case DW_FORM_data8:
24737 case DW_FORM_implicit_const:
24738 return 1;
24739 default:
24740 return 0;
24741 }
24742 }
24743
24744
24745 /* DW_ADDR is always stored already as sect_offset; despite for the forms
24746 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
24747
24748 static int
24749 attr_form_is_ref (const struct attribute *attr)
24750 {
24751 switch (attr->form)
24752 {
24753 case DW_FORM_ref_addr:
24754 case DW_FORM_ref1:
24755 case DW_FORM_ref2:
24756 case DW_FORM_ref4:
24757 case DW_FORM_ref8:
24758 case DW_FORM_ref_udata:
24759 case DW_FORM_GNU_ref_alt:
24760 return 1;
24761 default:
24762 return 0;
24763 }
24764 }
24765
24766 /* Return the .debug_loc section to use for CU.
24767 For DWO files use .debug_loc.dwo. */
24768
24769 static struct dwarf2_section_info *
24770 cu_debug_loc_section (struct dwarf2_cu *cu)
24771 {
24772 struct dwarf2_per_objfile *dwarf2_per_objfile
24773 = cu->per_cu->dwarf2_per_objfile;
24774
24775 if (cu->dwo_unit)
24776 {
24777 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24778
24779 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24780 }
24781 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
24782 : &dwarf2_per_objfile->loc);
24783 }
24784
24785 /* A helper function that fills in a dwarf2_loclist_baton. */
24786
24787 static void
24788 fill_in_loclist_baton (struct dwarf2_cu *cu,
24789 struct dwarf2_loclist_baton *baton,
24790 const struct attribute *attr)
24791 {
24792 struct dwarf2_per_objfile *dwarf2_per_objfile
24793 = cu->per_cu->dwarf2_per_objfile;
24794 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24795
24796 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
24797
24798 baton->per_cu = cu->per_cu;
24799 gdb_assert (baton->per_cu);
24800 /* We don't know how long the location list is, but make sure we
24801 don't run off the edge of the section. */
24802 baton->size = section->size - DW_UNSND (attr);
24803 baton->data = section->buffer + DW_UNSND (attr);
24804 baton->base_address = cu->base_address;
24805 baton->from_dwo = cu->dwo_unit != NULL;
24806 }
24807
24808 static void
24809 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24810 struct dwarf2_cu *cu, int is_block)
24811 {
24812 struct dwarf2_per_objfile *dwarf2_per_objfile
24813 = cu->per_cu->dwarf2_per_objfile;
24814 struct objfile *objfile = dwarf2_per_objfile->objfile;
24815 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24816
24817 if (attr_form_is_section_offset (attr)
24818 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24819 the section. If so, fall through to the complaint in the
24820 other branch. */
24821 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
24822 {
24823 struct dwarf2_loclist_baton *baton;
24824
24825 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24826
24827 fill_in_loclist_baton (cu, baton, attr);
24828
24829 if (cu->base_known == 0)
24830 complaint (&symfile_complaints,
24831 _("Location list used without "
24832 "specifying the CU base address."));
24833
24834 SYMBOL_ACLASS_INDEX (sym) = (is_block
24835 ? dwarf2_loclist_block_index
24836 : dwarf2_loclist_index);
24837 SYMBOL_LOCATION_BATON (sym) = baton;
24838 }
24839 else
24840 {
24841 struct dwarf2_locexpr_baton *baton;
24842
24843 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24844 baton->per_cu = cu->per_cu;
24845 gdb_assert (baton->per_cu);
24846
24847 if (attr_form_is_block (attr))
24848 {
24849 /* Note that we're just copying the block's data pointer
24850 here, not the actual data. We're still pointing into the
24851 info_buffer for SYM's objfile; right now we never release
24852 that buffer, but when we do clean up properly this may
24853 need to change. */
24854 baton->size = DW_BLOCK (attr)->size;
24855 baton->data = DW_BLOCK (attr)->data;
24856 }
24857 else
24858 {
24859 dwarf2_invalid_attrib_class_complaint ("location description",
24860 SYMBOL_NATURAL_NAME (sym));
24861 baton->size = 0;
24862 }
24863
24864 SYMBOL_ACLASS_INDEX (sym) = (is_block
24865 ? dwarf2_locexpr_block_index
24866 : dwarf2_locexpr_index);
24867 SYMBOL_LOCATION_BATON (sym) = baton;
24868 }
24869 }
24870
24871 /* Return the OBJFILE associated with the compilation unit CU. If CU
24872 came from a separate debuginfo file, then the master objfile is
24873 returned. */
24874
24875 struct objfile *
24876 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
24877 {
24878 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
24879
24880 /* Return the master objfile, so that we can report and look up the
24881 correct file containing this variable. */
24882 if (objfile->separate_debug_objfile_backlink)
24883 objfile = objfile->separate_debug_objfile_backlink;
24884
24885 return objfile;
24886 }
24887
24888 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
24889 (CU_HEADERP is unused in such case) or prepare a temporary copy at
24890 CU_HEADERP first. */
24891
24892 static const struct comp_unit_head *
24893 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
24894 struct dwarf2_per_cu_data *per_cu)
24895 {
24896 const gdb_byte *info_ptr;
24897
24898 if (per_cu->cu)
24899 return &per_cu->cu->header;
24900
24901 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
24902
24903 memset (cu_headerp, 0, sizeof (*cu_headerp));
24904 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
24905 rcuh_kind::COMPILE);
24906
24907 return cu_headerp;
24908 }
24909
24910 /* Return the address size given in the compilation unit header for CU. */
24911
24912 int
24913 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
24914 {
24915 struct comp_unit_head cu_header_local;
24916 const struct comp_unit_head *cu_headerp;
24917
24918 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24919
24920 return cu_headerp->addr_size;
24921 }
24922
24923 /* Return the offset size given in the compilation unit header for CU. */
24924
24925 int
24926 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
24927 {
24928 struct comp_unit_head cu_header_local;
24929 const struct comp_unit_head *cu_headerp;
24930
24931 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24932
24933 return cu_headerp->offset_size;
24934 }
24935
24936 /* See its dwarf2loc.h declaration. */
24937
24938 int
24939 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
24940 {
24941 struct comp_unit_head cu_header_local;
24942 const struct comp_unit_head *cu_headerp;
24943
24944 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24945
24946 if (cu_headerp->version == 2)
24947 return cu_headerp->addr_size;
24948 else
24949 return cu_headerp->offset_size;
24950 }
24951
24952 /* Return the text offset of the CU. The returned offset comes from
24953 this CU's objfile. If this objfile came from a separate debuginfo
24954 file, then the offset may be different from the corresponding
24955 offset in the parent objfile. */
24956
24957 CORE_ADDR
24958 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
24959 {
24960 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
24961
24962 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
24963 }
24964
24965 /* Return DWARF version number of PER_CU. */
24966
24967 short
24968 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
24969 {
24970 return per_cu->dwarf_version;
24971 }
24972
24973 /* Locate the .debug_info compilation unit from CU's objfile which contains
24974 the DIE at OFFSET. Raises an error on failure. */
24975
24976 static struct dwarf2_per_cu_data *
24977 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24978 unsigned int offset_in_dwz,
24979 struct dwarf2_per_objfile *dwarf2_per_objfile)
24980 {
24981 struct dwarf2_per_cu_data *this_cu;
24982 int low, high;
24983 const sect_offset *cu_off;
24984
24985 low = 0;
24986 high = dwarf2_per_objfile->all_comp_units.size () - 1;
24987 while (high > low)
24988 {
24989 struct dwarf2_per_cu_data *mid_cu;
24990 int mid = low + (high - low) / 2;
24991
24992 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
24993 cu_off = &mid_cu->sect_off;
24994 if (mid_cu->is_dwz > offset_in_dwz
24995 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
24996 high = mid;
24997 else
24998 low = mid + 1;
24999 }
25000 gdb_assert (low == high);
25001 this_cu = dwarf2_per_objfile->all_comp_units[low];
25002 cu_off = &this_cu->sect_off;
25003 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
25004 {
25005 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
25006 error (_("Dwarf Error: could not find partial DIE containing "
25007 "offset %s [in module %s]"),
25008 sect_offset_str (sect_off),
25009 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
25010
25011 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25012 <= sect_off);
25013 return dwarf2_per_objfile->all_comp_units[low-1];
25014 }
25015 else
25016 {
25017 this_cu = dwarf2_per_objfile->all_comp_units[low];
25018 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
25019 && sect_off >= this_cu->sect_off + this_cu->length)
25020 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
25021 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
25022 return this_cu;
25023 }
25024 }
25025
25026 /* Initialize dwarf2_cu CU, owned by PER_CU. */
25027
25028 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25029 : per_cu (per_cu_),
25030 mark (0),
25031 has_loclist (0),
25032 checked_producer (0),
25033 producer_is_gxx_lt_4_6 (0),
25034 producer_is_gcc_lt_4_3 (0),
25035 producer_is_icc_lt_14 (0),
25036 processing_has_namespace_info (0)
25037 {
25038 per_cu->cu = this;
25039 }
25040
25041 /* Destroy a dwarf2_cu. */
25042
25043 dwarf2_cu::~dwarf2_cu ()
25044 {
25045 per_cu->cu = NULL;
25046 }
25047
25048 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25049
25050 static void
25051 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25052 enum language pretend_language)
25053 {
25054 struct attribute *attr;
25055
25056 /* Set the language we're debugging. */
25057 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25058 if (attr)
25059 set_cu_language (DW_UNSND (attr), cu);
25060 else
25061 {
25062 cu->language = pretend_language;
25063 cu->language_defn = language_def (cu->language);
25064 }
25065
25066 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25067 }
25068
25069 /* Increase the age counter on each cached compilation unit, and free
25070 any that are too old. */
25071
25072 static void
25073 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
25074 {
25075 struct dwarf2_per_cu_data *per_cu, **last_chain;
25076
25077 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25078 per_cu = dwarf2_per_objfile->read_in_chain;
25079 while (per_cu != NULL)
25080 {
25081 per_cu->cu->last_used ++;
25082 if (per_cu->cu->last_used <= dwarf_max_cache_age)
25083 dwarf2_mark (per_cu->cu);
25084 per_cu = per_cu->cu->read_in_chain;
25085 }
25086
25087 per_cu = dwarf2_per_objfile->read_in_chain;
25088 last_chain = &dwarf2_per_objfile->read_in_chain;
25089 while (per_cu != NULL)
25090 {
25091 struct dwarf2_per_cu_data *next_cu;
25092
25093 next_cu = per_cu->cu->read_in_chain;
25094
25095 if (!per_cu->cu->mark)
25096 {
25097 delete per_cu->cu;
25098 *last_chain = next_cu;
25099 }
25100 else
25101 last_chain = &per_cu->cu->read_in_chain;
25102
25103 per_cu = next_cu;
25104 }
25105 }
25106
25107 /* Remove a single compilation unit from the cache. */
25108
25109 static void
25110 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
25111 {
25112 struct dwarf2_per_cu_data *per_cu, **last_chain;
25113 struct dwarf2_per_objfile *dwarf2_per_objfile
25114 = target_per_cu->dwarf2_per_objfile;
25115
25116 per_cu = dwarf2_per_objfile->read_in_chain;
25117 last_chain = &dwarf2_per_objfile->read_in_chain;
25118 while (per_cu != NULL)
25119 {
25120 struct dwarf2_per_cu_data *next_cu;
25121
25122 next_cu = per_cu->cu->read_in_chain;
25123
25124 if (per_cu == target_per_cu)
25125 {
25126 delete per_cu->cu;
25127 per_cu->cu = NULL;
25128 *last_chain = next_cu;
25129 break;
25130 }
25131 else
25132 last_chain = &per_cu->cu->read_in_chain;
25133
25134 per_cu = next_cu;
25135 }
25136 }
25137
25138 /* Release all extra memory associated with OBJFILE. */
25139
25140 void
25141 dwarf2_free_objfile (struct objfile *objfile)
25142 {
25143 struct dwarf2_per_objfile *dwarf2_per_objfile
25144 = get_dwarf2_per_objfile (objfile);
25145
25146 delete dwarf2_per_objfile;
25147 }
25148
25149 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25150 We store these in a hash table separate from the DIEs, and preserve them
25151 when the DIEs are flushed out of cache.
25152
25153 The CU "per_cu" pointer is needed because offset alone is not enough to
25154 uniquely identify the type. A file may have multiple .debug_types sections,
25155 or the type may come from a DWO file. Furthermore, while it's more logical
25156 to use per_cu->section+offset, with Fission the section with the data is in
25157 the DWO file but we don't know that section at the point we need it.
25158 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25159 because we can enter the lookup routine, get_die_type_at_offset, from
25160 outside this file, and thus won't necessarily have PER_CU->cu.
25161 Fortunately, PER_CU is stable for the life of the objfile. */
25162
25163 struct dwarf2_per_cu_offset_and_type
25164 {
25165 const struct dwarf2_per_cu_data *per_cu;
25166 sect_offset sect_off;
25167 struct type *type;
25168 };
25169
25170 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25171
25172 static hashval_t
25173 per_cu_offset_and_type_hash (const void *item)
25174 {
25175 const struct dwarf2_per_cu_offset_and_type *ofs
25176 = (const struct dwarf2_per_cu_offset_and_type *) item;
25177
25178 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25179 }
25180
25181 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25182
25183 static int
25184 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25185 {
25186 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25187 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25188 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25189 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25190
25191 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25192 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25193 }
25194
25195 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25196 table if necessary. For convenience, return TYPE.
25197
25198 The DIEs reading must have careful ordering to:
25199 * Not cause infite loops trying to read in DIEs as a prerequisite for
25200 reading current DIE.
25201 * Not trying to dereference contents of still incompletely read in types
25202 while reading in other DIEs.
25203 * Enable referencing still incompletely read in types just by a pointer to
25204 the type without accessing its fields.
25205
25206 Therefore caller should follow these rules:
25207 * Try to fetch any prerequisite types we may need to build this DIE type
25208 before building the type and calling set_die_type.
25209 * After building type call set_die_type for current DIE as soon as
25210 possible before fetching more types to complete the current type.
25211 * Make the type as complete as possible before fetching more types. */
25212
25213 static struct type *
25214 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25215 {
25216 struct dwarf2_per_objfile *dwarf2_per_objfile
25217 = cu->per_cu->dwarf2_per_objfile;
25218 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25219 struct objfile *objfile = dwarf2_per_objfile->objfile;
25220 struct attribute *attr;
25221 struct dynamic_prop prop;
25222
25223 /* For Ada types, make sure that the gnat-specific data is always
25224 initialized (if not already set). There are a few types where
25225 we should not be doing so, because the type-specific area is
25226 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25227 where the type-specific area is used to store the floatformat).
25228 But this is not a problem, because the gnat-specific information
25229 is actually not needed for these types. */
25230 if (need_gnat_info (cu)
25231 && TYPE_CODE (type) != TYPE_CODE_FUNC
25232 && TYPE_CODE (type) != TYPE_CODE_FLT
25233 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25234 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25235 && TYPE_CODE (type) != TYPE_CODE_METHOD
25236 && !HAVE_GNAT_AUX_INFO (type))
25237 INIT_GNAT_SPECIFIC (type);
25238
25239 /* Read DW_AT_allocated and set in type. */
25240 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25241 if (attr_form_is_block (attr))
25242 {
25243 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25244 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
25245 }
25246 else if (attr != NULL)
25247 {
25248 complaint (&symfile_complaints,
25249 _("DW_AT_allocated has the wrong form (%s) at DIE %s"),
25250 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25251 sect_offset_str (die->sect_off));
25252 }
25253
25254 /* Read DW_AT_associated and set in type. */
25255 attr = dwarf2_attr (die, DW_AT_associated, cu);
25256 if (attr_form_is_block (attr))
25257 {
25258 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25259 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
25260 }
25261 else if (attr != NULL)
25262 {
25263 complaint (&symfile_complaints,
25264 _("DW_AT_associated has the wrong form (%s) at DIE %s"),
25265 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25266 sect_offset_str (die->sect_off));
25267 }
25268
25269 /* Read DW_AT_data_location and set in type. */
25270 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25271 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25272 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
25273
25274 if (dwarf2_per_objfile->die_type_hash == NULL)
25275 {
25276 dwarf2_per_objfile->die_type_hash =
25277 htab_create_alloc_ex (127,
25278 per_cu_offset_and_type_hash,
25279 per_cu_offset_and_type_eq,
25280 NULL,
25281 &objfile->objfile_obstack,
25282 hashtab_obstack_allocate,
25283 dummy_obstack_deallocate);
25284 }
25285
25286 ofs.per_cu = cu->per_cu;
25287 ofs.sect_off = die->sect_off;
25288 ofs.type = type;
25289 slot = (struct dwarf2_per_cu_offset_and_type **)
25290 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
25291 if (*slot)
25292 complaint (&symfile_complaints,
25293 _("A problem internal to GDB: DIE %s has type already set"),
25294 sect_offset_str (die->sect_off));
25295 *slot = XOBNEW (&objfile->objfile_obstack,
25296 struct dwarf2_per_cu_offset_and_type);
25297 **slot = ofs;
25298 return type;
25299 }
25300
25301 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25302 or return NULL if the die does not have a saved type. */
25303
25304 static struct type *
25305 get_die_type_at_offset (sect_offset sect_off,
25306 struct dwarf2_per_cu_data *per_cu)
25307 {
25308 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25309 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
25310
25311 if (dwarf2_per_objfile->die_type_hash == NULL)
25312 return NULL;
25313
25314 ofs.per_cu = per_cu;
25315 ofs.sect_off = sect_off;
25316 slot = ((struct dwarf2_per_cu_offset_and_type *)
25317 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
25318 if (slot)
25319 return slot->type;
25320 else
25321 return NULL;
25322 }
25323
25324 /* Look up the type for DIE in CU in die_type_hash,
25325 or return NULL if DIE does not have a saved type. */
25326
25327 static struct type *
25328 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25329 {
25330 return get_die_type_at_offset (die->sect_off, cu->per_cu);
25331 }
25332
25333 /* Add a dependence relationship from CU to REF_PER_CU. */
25334
25335 static void
25336 dwarf2_add_dependence (struct dwarf2_cu *cu,
25337 struct dwarf2_per_cu_data *ref_per_cu)
25338 {
25339 void **slot;
25340
25341 if (cu->dependencies == NULL)
25342 cu->dependencies
25343 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25344 NULL, &cu->comp_unit_obstack,
25345 hashtab_obstack_allocate,
25346 dummy_obstack_deallocate);
25347
25348 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25349 if (*slot == NULL)
25350 *slot = ref_per_cu;
25351 }
25352
25353 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25354 Set the mark field in every compilation unit in the
25355 cache that we must keep because we are keeping CU. */
25356
25357 static int
25358 dwarf2_mark_helper (void **slot, void *data)
25359 {
25360 struct dwarf2_per_cu_data *per_cu;
25361
25362 per_cu = (struct dwarf2_per_cu_data *) *slot;
25363
25364 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25365 reading of the chain. As such dependencies remain valid it is not much
25366 useful to track and undo them during QUIT cleanups. */
25367 if (per_cu->cu == NULL)
25368 return 1;
25369
25370 if (per_cu->cu->mark)
25371 return 1;
25372 per_cu->cu->mark = 1;
25373
25374 if (per_cu->cu->dependencies != NULL)
25375 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25376
25377 return 1;
25378 }
25379
25380 /* Set the mark field in CU and in every other compilation unit in the
25381 cache that we must keep because we are keeping CU. */
25382
25383 static void
25384 dwarf2_mark (struct dwarf2_cu *cu)
25385 {
25386 if (cu->mark)
25387 return;
25388 cu->mark = 1;
25389 if (cu->dependencies != NULL)
25390 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
25391 }
25392
25393 static void
25394 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25395 {
25396 while (per_cu)
25397 {
25398 per_cu->cu->mark = 0;
25399 per_cu = per_cu->cu->read_in_chain;
25400 }
25401 }
25402
25403 /* Trivial hash function for partial_die_info: the hash value of a DIE
25404 is its offset in .debug_info for this objfile. */
25405
25406 static hashval_t
25407 partial_die_hash (const void *item)
25408 {
25409 const struct partial_die_info *part_die
25410 = (const struct partial_die_info *) item;
25411
25412 return to_underlying (part_die->sect_off);
25413 }
25414
25415 /* Trivial comparison function for partial_die_info structures: two DIEs
25416 are equal if they have the same offset. */
25417
25418 static int
25419 partial_die_eq (const void *item_lhs, const void *item_rhs)
25420 {
25421 const struct partial_die_info *part_die_lhs
25422 = (const struct partial_die_info *) item_lhs;
25423 const struct partial_die_info *part_die_rhs
25424 = (const struct partial_die_info *) item_rhs;
25425
25426 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25427 }
25428
25429 static struct cmd_list_element *set_dwarf_cmdlist;
25430 static struct cmd_list_element *show_dwarf_cmdlist;
25431
25432 static void
25433 set_dwarf_cmd (const char *args, int from_tty)
25434 {
25435 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
25436 gdb_stdout);
25437 }
25438
25439 static void
25440 show_dwarf_cmd (const char *args, int from_tty)
25441 {
25442 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
25443 }
25444
25445 int dwarf_always_disassemble;
25446
25447 static void
25448 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
25449 struct cmd_list_element *c, const char *value)
25450 {
25451 fprintf_filtered (file,
25452 _("Whether to always disassemble "
25453 "DWARF expressions is %s.\n"),
25454 value);
25455 }
25456
25457 static void
25458 show_check_physname (struct ui_file *file, int from_tty,
25459 struct cmd_list_element *c, const char *value)
25460 {
25461 fprintf_filtered (file,
25462 _("Whether to check \"physname\" is %s.\n"),
25463 value);
25464 }
25465
25466 void
25467 _initialize_dwarf2_read (void)
25468 {
25469
25470 dwarf2_objfile_data_key = register_objfile_data ();
25471
25472 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
25473 Set DWARF specific variables.\n\
25474 Configure DWARF variables such as the cache size"),
25475 &set_dwarf_cmdlist, "maintenance set dwarf ",
25476 0/*allow-unknown*/, &maintenance_set_cmdlist);
25477
25478 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
25479 Show DWARF specific variables\n\
25480 Show DWARF variables such as the cache size"),
25481 &show_dwarf_cmdlist, "maintenance show dwarf ",
25482 0/*allow-unknown*/, &maintenance_show_cmdlist);
25483
25484 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25485 &dwarf_max_cache_age, _("\
25486 Set the upper bound on the age of cached DWARF compilation units."), _("\
25487 Show the upper bound on the age of cached DWARF compilation units."), _("\
25488 A higher limit means that cached compilation units will be stored\n\
25489 in memory longer, and more total memory will be used. Zero disables\n\
25490 caching, which can slow down startup."),
25491 NULL,
25492 show_dwarf_max_cache_age,
25493 &set_dwarf_cmdlist,
25494 &show_dwarf_cmdlist);
25495
25496 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
25497 &dwarf_always_disassemble, _("\
25498 Set whether `info address' always disassembles DWARF expressions."), _("\
25499 Show whether `info address' always disassembles DWARF expressions."), _("\
25500 When enabled, DWARF expressions are always printed in an assembly-like\n\
25501 syntax. When disabled, expressions will be printed in a more\n\
25502 conversational style, when possible."),
25503 NULL,
25504 show_dwarf_always_disassemble,
25505 &set_dwarf_cmdlist,
25506 &show_dwarf_cmdlist);
25507
25508 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25509 Set debugging of the DWARF reader."), _("\
25510 Show debugging of the DWARF reader."), _("\
25511 When enabled (non-zero), debugging messages are printed during DWARF\n\
25512 reading and symtab expansion. A value of 1 (one) provides basic\n\
25513 information. A value greater than 1 provides more verbose information."),
25514 NULL,
25515 NULL,
25516 &setdebuglist, &showdebuglist);
25517
25518 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25519 Set debugging of the DWARF DIE reader."), _("\
25520 Show debugging of the DWARF DIE reader."), _("\
25521 When enabled (non-zero), DIEs are dumped after they are read in.\n\
25522 The value is the maximum depth to print."),
25523 NULL,
25524 NULL,
25525 &setdebuglist, &showdebuglist);
25526
25527 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25528 Set debugging of the dwarf line reader."), _("\
25529 Show debugging of the dwarf line reader."), _("\
25530 When enabled (non-zero), line number entries are dumped as they are read in.\n\
25531 A value of 1 (one) provides basic information.\n\
25532 A value greater than 1 provides more verbose information."),
25533 NULL,
25534 NULL,
25535 &setdebuglist, &showdebuglist);
25536
25537 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25538 Set cross-checking of \"physname\" code against demangler."), _("\
25539 Show cross-checking of \"physname\" code against demangler."), _("\
25540 When enabled, GDB's internal \"physname\" code is checked against\n\
25541 the demangler."),
25542 NULL, show_check_physname,
25543 &setdebuglist, &showdebuglist);
25544
25545 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25546 no_class, &use_deprecated_index_sections, _("\
25547 Set whether to use deprecated gdb_index sections."), _("\
25548 Show whether to use deprecated gdb_index sections."), _("\
25549 When enabled, deprecated .gdb_index sections are used anyway.\n\
25550 Normally they are ignored either because of a missing feature or\n\
25551 performance issue.\n\
25552 Warning: This option must be enabled before gdb reads the file."),
25553 NULL,
25554 NULL,
25555 &setlist, &showlist);
25556
25557 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25558 &dwarf2_locexpr_funcs);
25559 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25560 &dwarf2_loclist_funcs);
25561
25562 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25563 &dwarf2_block_frame_base_locexpr_funcs);
25564 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25565 &dwarf2_block_frame_base_loclist_funcs);
25566
25567 #if GDB_SELF_TEST
25568 selftests::register_test ("dw2_expand_symtabs_matching",
25569 selftests::dw2_expand_symtabs_matching::run_test);
25570 #endif
25571 }
This page took 0.607716 seconds and 5 git commands to generate.