Use a const line_header in macro reader
[deliverable/binutils-gdb.git] / gdb / dwarf2 / read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2020 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 "dwarf2/read.h"
33 #include "dwarf2/abbrev.h"
34 #include "dwarf2/attribute.h"
35 #include "dwarf2/comp-unit.h"
36 #include "dwarf2/index-cache.h"
37 #include "dwarf2/index-common.h"
38 #include "dwarf2/leb.h"
39 #include "dwarf2/line-header.h"
40 #include "dwarf2/dwz.h"
41 #include "dwarf2/macro.h"
42 #include "bfd.h"
43 #include "elf-bfd.h"
44 #include "symtab.h"
45 #include "gdbtypes.h"
46 #include "objfiles.h"
47 #include "dwarf2.h"
48 #include "buildsym.h"
49 #include "demangle.h"
50 #include "gdb-demangle.h"
51 #include "filenames.h" /* for DOSish file names */
52 #include "language.h"
53 #include "complaints.h"
54 #include "dwarf2/expr.h"
55 #include "dwarf2/loc.h"
56 #include "cp-support.h"
57 #include "hashtab.h"
58 #include "command.h"
59 #include "gdbcmd.h"
60 #include "block.h"
61 #include "addrmap.h"
62 #include "typeprint.h"
63 #include "psympriv.h"
64 #include "c-lang.h"
65 #include "go-lang.h"
66 #include "valprint.h"
67 #include "gdbcore.h" /* for gnutarget */
68 #include "gdb/gdb-index.h"
69 #include "gdb_bfd.h"
70 #include "f-lang.h"
71 #include "source.h"
72 #include "build-id.h"
73 #include "namespace.h"
74 #include "gdbsupport/function-view.h"
75 #include "gdbsupport/gdb_optional.h"
76 #include "gdbsupport/underlying.h"
77 #include "gdbsupport/hash_enum.h"
78 #include "filename-seen-cache.h"
79 #include "producer.h"
80 #include <fcntl.h>
81 #include <algorithm>
82 #include <unordered_map>
83 #include "gdbsupport/selftest.h"
84 #include "rust-lang.h"
85 #include "gdbsupport/pathstuff.h"
86 #include "count-one-bits.h"
87 #include "debuginfod-support.h"
88
89 /* When == 1, print basic high level tracing messages.
90 When > 1, be more verbose.
91 This is in contrast to the low level DIE reading of dwarf_die_debug. */
92 static unsigned int dwarf_read_debug = 0;
93
94 /* When non-zero, dump DIEs after they are read in. */
95 static unsigned int dwarf_die_debug = 0;
96
97 /* When non-zero, dump line number entries as they are read in. */
98 unsigned int dwarf_line_debug = 0;
99
100 /* When true, cross-check physname against demangler. */
101 static bool check_physname = false;
102
103 /* When true, do not reject deprecated .gdb_index sections. */
104 static bool use_deprecated_index_sections = false;
105
106 static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
107
108 /* The "aclass" indices for various kinds of computed DWARF symbols. */
109
110 static int dwarf2_locexpr_index;
111 static int dwarf2_loclist_index;
112 static int dwarf2_locexpr_block_index;
113 static int dwarf2_loclist_block_index;
114
115 /* An index into a (C++) symbol name component in a symbol name as
116 recorded in the mapped_index's symbol table. For each C++ symbol
117 in the symbol table, we record one entry for the start of each
118 component in the symbol in a table of name components, and then
119 sort the table, in order to be able to binary search symbol names,
120 ignoring leading namespaces, both completion and regular look up.
121 For example, for symbol "A::B::C", we'll have an entry that points
122 to "A::B::C", another that points to "B::C", and another for "C".
123 Note that function symbols in GDB index have no parameter
124 information, just the function/method names. You can convert a
125 name_component to a "const char *" using the
126 'mapped_index::symbol_name_at(offset_type)' method. */
127
128 struct name_component
129 {
130 /* Offset in the symbol name where the component starts. Stored as
131 a (32-bit) offset instead of a pointer to save memory and improve
132 locality on 64-bit architectures. */
133 offset_type name_offset;
134
135 /* The symbol's index in the symbol and constant pool tables of a
136 mapped_index. */
137 offset_type idx;
138 };
139
140 /* Base class containing bits shared by both .gdb_index and
141 .debug_name indexes. */
142
143 struct mapped_index_base
144 {
145 mapped_index_base () = default;
146 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
147
148 /* The name_component table (a sorted vector). See name_component's
149 description above. */
150 std::vector<name_component> name_components;
151
152 /* How NAME_COMPONENTS is sorted. */
153 enum case_sensitivity name_components_casing;
154
155 /* Return the number of names in the symbol table. */
156 virtual size_t symbol_name_count () const = 0;
157
158 /* Get the name of the symbol at IDX in the symbol table. */
159 virtual const char *symbol_name_at (offset_type idx) const = 0;
160
161 /* Return whether the name at IDX in the symbol table should be
162 ignored. */
163 virtual bool symbol_name_slot_invalid (offset_type idx) const
164 {
165 return false;
166 }
167
168 /* Build the symbol name component sorted vector, if we haven't
169 yet. */
170 void build_name_components ();
171
172 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
173 possible matches for LN_NO_PARAMS in the name component
174 vector. */
175 std::pair<std::vector<name_component>::const_iterator,
176 std::vector<name_component>::const_iterator>
177 find_name_components_bounds (const lookup_name_info &ln_no_params,
178 enum language lang) const;
179
180 /* Prevent deleting/destroying via a base class pointer. */
181 protected:
182 ~mapped_index_base() = default;
183 };
184
185 /* A description of the mapped index. The file format is described in
186 a comment by the code that writes the index. */
187 struct mapped_index final : public mapped_index_base
188 {
189 /* A slot/bucket in the symbol table hash. */
190 struct symbol_table_slot
191 {
192 const offset_type name;
193 const offset_type vec;
194 };
195
196 /* Index data format version. */
197 int version = 0;
198
199 /* The address table data. */
200 gdb::array_view<const gdb_byte> address_table;
201
202 /* The symbol table, implemented as a hash table. */
203 gdb::array_view<symbol_table_slot> symbol_table;
204
205 /* A pointer to the constant pool. */
206 const char *constant_pool = nullptr;
207
208 bool symbol_name_slot_invalid (offset_type idx) const override
209 {
210 const auto &bucket = this->symbol_table[idx];
211 return bucket.name == 0 && bucket.vec == 0;
212 }
213
214 /* Convenience method to get at the name of the symbol at IDX in the
215 symbol table. */
216 const char *symbol_name_at (offset_type idx) const override
217 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
218
219 size_t symbol_name_count () const override
220 { return this->symbol_table.size (); }
221 };
222
223 /* A description of the mapped .debug_names.
224 Uninitialized map has CU_COUNT 0. */
225 struct mapped_debug_names final : public mapped_index_base
226 {
227 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
228 : dwarf2_per_objfile (dwarf2_per_objfile_)
229 {}
230
231 struct dwarf2_per_objfile *dwarf2_per_objfile;
232 bfd_endian dwarf5_byte_order;
233 bool dwarf5_is_dwarf64;
234 bool augmentation_is_gdb;
235 uint8_t offset_size;
236 uint32_t cu_count = 0;
237 uint32_t tu_count, bucket_count, name_count;
238 const gdb_byte *cu_table_reordered, *tu_table_reordered;
239 const uint32_t *bucket_table_reordered, *hash_table_reordered;
240 const gdb_byte *name_table_string_offs_reordered;
241 const gdb_byte *name_table_entry_offs_reordered;
242 const gdb_byte *entry_pool;
243
244 struct index_val
245 {
246 ULONGEST dwarf_tag;
247 struct attr
248 {
249 /* Attribute name DW_IDX_*. */
250 ULONGEST dw_idx;
251
252 /* Attribute form DW_FORM_*. */
253 ULONGEST form;
254
255 /* Value if FORM is DW_FORM_implicit_const. */
256 LONGEST implicit_const;
257 };
258 std::vector<attr> attr_vec;
259 };
260
261 std::unordered_map<ULONGEST, index_val> abbrev_map;
262
263 const char *namei_to_name (uint32_t namei) const;
264
265 /* Implementation of the mapped_index_base virtual interface, for
266 the name_components cache. */
267
268 const char *symbol_name_at (offset_type idx) const override
269 { return namei_to_name (idx); }
270
271 size_t symbol_name_count () const override
272 { return this->name_count; }
273 };
274
275 /* See dwarf2read.h. */
276
277 dwarf2_per_objfile *
278 get_dwarf2_per_objfile (struct objfile *objfile)
279 {
280 return dwarf2_objfile_data_key.get (objfile);
281 }
282
283 /* Default names of the debugging sections. */
284
285 /* Note that if the debugging section has been compressed, it might
286 have a name like .zdebug_info. */
287
288 static const struct dwarf2_debug_sections dwarf2_elf_names =
289 {
290 { ".debug_info", ".zdebug_info" },
291 { ".debug_abbrev", ".zdebug_abbrev" },
292 { ".debug_line", ".zdebug_line" },
293 { ".debug_loc", ".zdebug_loc" },
294 { ".debug_loclists", ".zdebug_loclists" },
295 { ".debug_macinfo", ".zdebug_macinfo" },
296 { ".debug_macro", ".zdebug_macro" },
297 { ".debug_str", ".zdebug_str" },
298 { ".debug_str_offsets", ".zdebug_str_offsets" },
299 { ".debug_line_str", ".zdebug_line_str" },
300 { ".debug_ranges", ".zdebug_ranges" },
301 { ".debug_rnglists", ".zdebug_rnglists" },
302 { ".debug_types", ".zdebug_types" },
303 { ".debug_addr", ".zdebug_addr" },
304 { ".debug_frame", ".zdebug_frame" },
305 { ".eh_frame", NULL },
306 { ".gdb_index", ".zgdb_index" },
307 { ".debug_names", ".zdebug_names" },
308 { ".debug_aranges", ".zdebug_aranges" },
309 23
310 };
311
312 /* List of DWO/DWP sections. */
313
314 static const struct dwop_section_names
315 {
316 struct dwarf2_section_names abbrev_dwo;
317 struct dwarf2_section_names info_dwo;
318 struct dwarf2_section_names line_dwo;
319 struct dwarf2_section_names loc_dwo;
320 struct dwarf2_section_names loclists_dwo;
321 struct dwarf2_section_names macinfo_dwo;
322 struct dwarf2_section_names macro_dwo;
323 struct dwarf2_section_names str_dwo;
324 struct dwarf2_section_names str_offsets_dwo;
325 struct dwarf2_section_names types_dwo;
326 struct dwarf2_section_names cu_index;
327 struct dwarf2_section_names tu_index;
328 }
329 dwop_section_names =
330 {
331 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
332 { ".debug_info.dwo", ".zdebug_info.dwo" },
333 { ".debug_line.dwo", ".zdebug_line.dwo" },
334 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
335 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
336 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
337 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
338 { ".debug_str.dwo", ".zdebug_str.dwo" },
339 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
340 { ".debug_types.dwo", ".zdebug_types.dwo" },
341 { ".debug_cu_index", ".zdebug_cu_index" },
342 { ".debug_tu_index", ".zdebug_tu_index" },
343 };
344
345 /* local data types */
346
347 /* Type used for delaying computation of method physnames.
348 See comments for compute_delayed_physnames. */
349 struct delayed_method_info
350 {
351 /* The type to which the method is attached, i.e., its parent class. */
352 struct type *type;
353
354 /* The index of the method in the type's function fieldlists. */
355 int fnfield_index;
356
357 /* The index of the method in the fieldlist. */
358 int index;
359
360 /* The name of the DIE. */
361 const char *name;
362
363 /* The DIE associated with this method. */
364 struct die_info *die;
365 };
366
367 /* Internal state when decoding a particular compilation unit. */
368 struct dwarf2_cu
369 {
370 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
371 ~dwarf2_cu ();
372
373 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
374
375 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
376 Create the set of symtabs used by this TU, or if this TU is sharing
377 symtabs with another TU and the symtabs have already been created
378 then restore those symtabs in the line header.
379 We don't need the pc/line-number mapping for type units. */
380 void setup_type_unit_groups (struct die_info *die);
381
382 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
383 buildsym_compunit constructor. */
384 struct compunit_symtab *start_symtab (const char *name,
385 const char *comp_dir,
386 CORE_ADDR low_pc);
387
388 /* Reset the builder. */
389 void reset_builder () { m_builder.reset (); }
390
391 /* The header of the compilation unit. */
392 struct comp_unit_head header {};
393
394 /* Base address of this compilation unit. */
395 CORE_ADDR base_address = 0;
396
397 /* Non-zero if base_address has been set. */
398 int base_known = 0;
399
400 /* The language we are debugging. */
401 enum language language = language_unknown;
402 const struct language_defn *language_defn = nullptr;
403
404 const char *producer = nullptr;
405
406 private:
407 /* The symtab builder for this CU. This is only non-NULL when full
408 symbols are being read. */
409 std::unique_ptr<buildsym_compunit> m_builder;
410
411 public:
412 /* The generic symbol table building routines have separate lists for
413 file scope symbols and all all other scopes (local scopes). So
414 we need to select the right one to pass to add_symbol_to_list().
415 We do it by keeping a pointer to the correct list in list_in_scope.
416
417 FIXME: The original dwarf code just treated the file scope as the
418 first local scope, and all other local scopes as nested local
419 scopes, and worked fine. Check to see if we really need to
420 distinguish these in buildsym.c. */
421 struct pending **list_in_scope = nullptr;
422
423 /* Hash table holding all the loaded partial DIEs
424 with partial_die->offset.SECT_OFF as hash. */
425 htab_t partial_dies = nullptr;
426
427 /* Storage for things with the same lifetime as this read-in compilation
428 unit, including partial DIEs. */
429 auto_obstack comp_unit_obstack;
430
431 /* When multiple dwarf2_cu structures are living in memory, this field
432 chains them all together, so that they can be released efficiently.
433 We will probably also want a generation counter so that most-recently-used
434 compilation units are cached... */
435 struct dwarf2_per_cu_data *read_in_chain = nullptr;
436
437 /* Backlink to our per_cu entry. */
438 struct dwarf2_per_cu_data *per_cu;
439
440 /* How many compilation units ago was this CU last referenced? */
441 int last_used = 0;
442
443 /* A hash table of DIE cu_offset for following references with
444 die_info->offset.sect_off as hash. */
445 htab_t die_hash = nullptr;
446
447 /* Full DIEs if read in. */
448 struct die_info *dies = nullptr;
449
450 /* A set of pointers to dwarf2_per_cu_data objects for compilation
451 units referenced by this one. Only set during full symbol processing;
452 partial symbol tables do not have dependencies. */
453 htab_t dependencies = nullptr;
454
455 /* Header data from the line table, during full symbol processing. */
456 struct line_header *line_header = nullptr;
457 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
458 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
459 this is the DW_TAG_compile_unit die for this CU. We'll hold on
460 to the line header as long as this DIE is being processed. See
461 process_die_scope. */
462 die_info *line_header_die_owner = nullptr;
463
464 /* A list of methods which need to have physnames computed
465 after all type information has been read. */
466 std::vector<delayed_method_info> method_list;
467
468 /* To be copied to symtab->call_site_htab. */
469 htab_t call_site_htab = nullptr;
470
471 /* Non-NULL if this CU came from a DWO file.
472 There is an invariant here that is important to remember:
473 Except for attributes copied from the top level DIE in the "main"
474 (or "stub") file in preparation for reading the DWO file
475 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
476 Either there isn't a DWO file (in which case this is NULL and the point
477 is moot), or there is and either we're not going to read it (in which
478 case this is NULL) or there is and we are reading it (in which case this
479 is non-NULL). */
480 struct dwo_unit *dwo_unit = nullptr;
481
482 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
483 Note this value comes from the Fission stub CU/TU's DIE. */
484 gdb::optional<ULONGEST> addr_base;
485
486 /* The DW_AT_rnglists_base attribute if present.
487 Note this value comes from the Fission stub CU/TU's DIE.
488 Also note that the value is zero in the non-DWO case so this value can
489 be used without needing to know whether DWO files are in use or not.
490 N.B. This does not apply to DW_AT_ranges appearing in
491 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
492 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
493 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
494 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
495 ULONGEST ranges_base = 0;
496
497 /* When reading debug info generated by older versions of rustc, we
498 have to rewrite some union types to be struct types with a
499 variant part. This rewriting must be done after the CU is fully
500 read in, because otherwise at the point of rewriting some struct
501 type might not have been fully processed. So, we keep a list of
502 all such types here and process them after expansion. */
503 std::vector<struct type *> rust_unions;
504
505 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
506 files, the value is implicitly zero. For DWARF 5 version DWO files, the
507 value is often implicit and is the size of the header of
508 .debug_str_offsets section (8 or 4, depending on the address size). */
509 gdb::optional<ULONGEST> str_offsets_base;
510
511 /* Mark used when releasing cached dies. */
512 bool mark : 1;
513
514 /* This CU references .debug_loc. See the symtab->locations_valid field.
515 This test is imperfect as there may exist optimized debug code not using
516 any location list and still facing inlining issues if handled as
517 unoptimized code. For a future better test see GCC PR other/32998. */
518 bool has_loclist : 1;
519
520 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
521 if all the producer_is_* fields are valid. This information is cached
522 because profiling CU expansion showed excessive time spent in
523 producer_is_gxx_lt_4_6. */
524 bool checked_producer : 1;
525 bool producer_is_gxx_lt_4_6 : 1;
526 bool producer_is_gcc_lt_4_3 : 1;
527 bool producer_is_icc : 1;
528 bool producer_is_icc_lt_14 : 1;
529 bool producer_is_codewarrior : 1;
530
531 /* When true, the file that we're processing is known to have
532 debugging info for C++ namespaces. GCC 3.3.x did not produce
533 this information, but later versions do. */
534
535 bool processing_has_namespace_info : 1;
536
537 struct partial_die_info *find_partial_die (sect_offset sect_off);
538
539 /* If this CU was inherited by another CU (via specification,
540 abstract_origin, etc), this is the ancestor CU. */
541 dwarf2_cu *ancestor;
542
543 /* Get the buildsym_compunit for this CU. */
544 buildsym_compunit *get_builder ()
545 {
546 /* If this CU has a builder associated with it, use that. */
547 if (m_builder != nullptr)
548 return m_builder.get ();
549
550 /* Otherwise, search ancestors for a valid builder. */
551 if (ancestor != nullptr)
552 return ancestor->get_builder ();
553
554 return nullptr;
555 }
556 };
557
558 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
559 This includes type_unit_group and quick_file_names. */
560
561 struct stmt_list_hash
562 {
563 /* The DWO unit this table is from or NULL if there is none. */
564 struct dwo_unit *dwo_unit;
565
566 /* Offset in .debug_line or .debug_line.dwo. */
567 sect_offset line_sect_off;
568 };
569
570 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
571 an object of this type. */
572
573 struct type_unit_group
574 {
575 /* dwarf2read.c's main "handle" on a TU symtab.
576 To simplify things we create an artificial CU that "includes" all the
577 type units using this stmt_list so that the rest of the code still has
578 a "per_cu" handle on the symtab. */
579 struct dwarf2_per_cu_data per_cu;
580
581 /* The TUs that share this DW_AT_stmt_list entry.
582 This is added to while parsing type units to build partial symtabs,
583 and is deleted afterwards and not used again. */
584 std::vector<signatured_type *> *tus;
585
586 /* The compunit symtab.
587 Type units in a group needn't all be defined in the same source file,
588 so we create an essentially anonymous symtab as the compunit symtab. */
589 struct compunit_symtab *compunit_symtab;
590
591 /* The data used to construct the hash key. */
592 struct stmt_list_hash hash;
593
594 /* The symbol tables for this TU (obtained from the files listed in
595 DW_AT_stmt_list).
596 WARNING: The order of entries here must match the order of entries
597 in the line header. After the first TU using this type_unit_group, the
598 line header for the subsequent TUs is recreated from this. This is done
599 because we need to use the same symtabs for each TU using the same
600 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
601 there's no guarantee the line header doesn't have duplicate entries. */
602 struct symtab **symtabs;
603 };
604
605 /* These sections are what may appear in a (real or virtual) DWO file. */
606
607 struct dwo_sections
608 {
609 struct dwarf2_section_info abbrev;
610 struct dwarf2_section_info line;
611 struct dwarf2_section_info loc;
612 struct dwarf2_section_info loclists;
613 struct dwarf2_section_info macinfo;
614 struct dwarf2_section_info macro;
615 struct dwarf2_section_info str;
616 struct dwarf2_section_info str_offsets;
617 /* In the case of a virtual DWO file, these two are unused. */
618 struct dwarf2_section_info info;
619 std::vector<dwarf2_section_info> types;
620 };
621
622 /* CUs/TUs in DWP/DWO files. */
623
624 struct dwo_unit
625 {
626 /* Backlink to the containing struct dwo_file. */
627 struct dwo_file *dwo_file;
628
629 /* The "id" that distinguishes this CU/TU.
630 .debug_info calls this "dwo_id", .debug_types calls this "signature".
631 Since signatures came first, we stick with it for consistency. */
632 ULONGEST signature;
633
634 /* The section this CU/TU lives in, in the DWO file. */
635 struct dwarf2_section_info *section;
636
637 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
638 sect_offset sect_off;
639 unsigned int length;
640
641 /* For types, offset in the type's DIE of the type defined by this TU. */
642 cu_offset type_offset_in_tu;
643 };
644
645 /* include/dwarf2.h defines the DWP section codes.
646 It defines a max value but it doesn't define a min value, which we
647 use for error checking, so provide one. */
648
649 enum dwp_v2_section_ids
650 {
651 DW_SECT_MIN = 1
652 };
653
654 /* Data for one DWO file.
655
656 This includes virtual DWO files (a virtual DWO file is a DWO file as it
657 appears in a DWP file). DWP files don't really have DWO files per se -
658 comdat folding of types "loses" the DWO file they came from, and from
659 a high level view DWP files appear to contain a mass of random types.
660 However, to maintain consistency with the non-DWP case we pretend DWP
661 files contain virtual DWO files, and we assign each TU with one virtual
662 DWO file (generally based on the line and abbrev section offsets -
663 a heuristic that seems to work in practice). */
664
665 struct dwo_file
666 {
667 dwo_file () = default;
668 DISABLE_COPY_AND_ASSIGN (dwo_file);
669
670 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
671 For virtual DWO files the name is constructed from the section offsets
672 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
673 from related CU+TUs. */
674 const char *dwo_name = nullptr;
675
676 /* The DW_AT_comp_dir attribute. */
677 const char *comp_dir = nullptr;
678
679 /* The bfd, when the file is open. Otherwise this is NULL.
680 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
681 gdb_bfd_ref_ptr dbfd;
682
683 /* The sections that make up this DWO file.
684 Remember that for virtual DWO files in DWP V2, these are virtual
685 sections (for lack of a better name). */
686 struct dwo_sections sections {};
687
688 /* The CUs in the file.
689 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
690 an extension to handle LLVM's Link Time Optimization output (where
691 multiple source files may be compiled into a single object/dwo pair). */
692 htab_up cus;
693
694 /* Table of TUs in the file.
695 Each element is a struct dwo_unit. */
696 htab_up tus;
697 };
698
699 /* These sections are what may appear in a DWP file. */
700
701 struct dwp_sections
702 {
703 /* These are used by both DWP version 1 and 2. */
704 struct dwarf2_section_info str;
705 struct dwarf2_section_info cu_index;
706 struct dwarf2_section_info tu_index;
707
708 /* These are only used by DWP version 2 files.
709 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
710 sections are referenced by section number, and are not recorded here.
711 In DWP version 2 there is at most one copy of all these sections, each
712 section being (effectively) comprised of the concatenation of all of the
713 individual sections that exist in the version 1 format.
714 To keep the code simple we treat each of these concatenated pieces as a
715 section itself (a virtual section?). */
716 struct dwarf2_section_info abbrev;
717 struct dwarf2_section_info info;
718 struct dwarf2_section_info line;
719 struct dwarf2_section_info loc;
720 struct dwarf2_section_info macinfo;
721 struct dwarf2_section_info macro;
722 struct dwarf2_section_info str_offsets;
723 struct dwarf2_section_info types;
724 };
725
726 /* These sections are what may appear in a virtual DWO file in DWP version 1.
727 A virtual DWO file is a DWO file as it appears in a DWP file. */
728
729 struct virtual_v1_dwo_sections
730 {
731 struct dwarf2_section_info abbrev;
732 struct dwarf2_section_info line;
733 struct dwarf2_section_info loc;
734 struct dwarf2_section_info macinfo;
735 struct dwarf2_section_info macro;
736 struct dwarf2_section_info str_offsets;
737 /* Each DWP hash table entry records one CU or one TU.
738 That is recorded here, and copied to dwo_unit.section. */
739 struct dwarf2_section_info info_or_types;
740 };
741
742 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
743 In version 2, the sections of the DWO files are concatenated together
744 and stored in one section of that name. Thus each ELF section contains
745 several "virtual" sections. */
746
747 struct virtual_v2_dwo_sections
748 {
749 bfd_size_type abbrev_offset;
750 bfd_size_type abbrev_size;
751
752 bfd_size_type line_offset;
753 bfd_size_type line_size;
754
755 bfd_size_type loc_offset;
756 bfd_size_type loc_size;
757
758 bfd_size_type macinfo_offset;
759 bfd_size_type macinfo_size;
760
761 bfd_size_type macro_offset;
762 bfd_size_type macro_size;
763
764 bfd_size_type str_offsets_offset;
765 bfd_size_type str_offsets_size;
766
767 /* Each DWP hash table entry records one CU or one TU.
768 That is recorded here, and copied to dwo_unit.section. */
769 bfd_size_type info_or_types_offset;
770 bfd_size_type info_or_types_size;
771 };
772
773 /* Contents of DWP hash tables. */
774
775 struct dwp_hash_table
776 {
777 uint32_t version, nr_columns;
778 uint32_t nr_units, nr_slots;
779 const gdb_byte *hash_table, *unit_table;
780 union
781 {
782 struct
783 {
784 const gdb_byte *indices;
785 } v1;
786 struct
787 {
788 /* This is indexed by column number and gives the id of the section
789 in that column. */
790 #define MAX_NR_V2_DWO_SECTIONS \
791 (1 /* .debug_info or .debug_types */ \
792 + 1 /* .debug_abbrev */ \
793 + 1 /* .debug_line */ \
794 + 1 /* .debug_loc */ \
795 + 1 /* .debug_str_offsets */ \
796 + 1 /* .debug_macro or .debug_macinfo */)
797 int section_ids[MAX_NR_V2_DWO_SECTIONS];
798 const gdb_byte *offsets;
799 const gdb_byte *sizes;
800 } v2;
801 } section_pool;
802 };
803
804 /* Data for one DWP file. */
805
806 struct dwp_file
807 {
808 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
809 : name (name_),
810 dbfd (std::move (abfd))
811 {
812 }
813
814 /* Name of the file. */
815 const char *name;
816
817 /* File format version. */
818 int version = 0;
819
820 /* The bfd. */
821 gdb_bfd_ref_ptr dbfd;
822
823 /* Section info for this file. */
824 struct dwp_sections sections {};
825
826 /* Table of CUs in the file. */
827 const struct dwp_hash_table *cus = nullptr;
828
829 /* Table of TUs in the file. */
830 const struct dwp_hash_table *tus = nullptr;
831
832 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
833 htab_up loaded_cus;
834 htab_up loaded_tus;
835
836 /* Table to map ELF section numbers to their sections.
837 This is only needed for the DWP V1 file format. */
838 unsigned int num_sections = 0;
839 asection **elf_sections = nullptr;
840 };
841
842 /* Struct used to pass misc. parameters to read_die_and_children, et
843 al. which are used for both .debug_info and .debug_types dies.
844 All parameters here are unchanging for the life of the call. This
845 struct exists to abstract away the constant parameters of die reading. */
846
847 struct die_reader_specs
848 {
849 /* The bfd of die_section. */
850 bfd* abfd;
851
852 /* The CU of the DIE we are parsing. */
853 struct dwarf2_cu *cu;
854
855 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
856 struct dwo_file *dwo_file;
857
858 /* The section the die comes from.
859 This is either .debug_info or .debug_types, or the .dwo variants. */
860 struct dwarf2_section_info *die_section;
861
862 /* die_section->buffer. */
863 const gdb_byte *buffer;
864
865 /* The end of the buffer. */
866 const gdb_byte *buffer_end;
867
868 /* The abbreviation table to use when reading the DIEs. */
869 struct abbrev_table *abbrev_table;
870 };
871
872 /* A subclass of die_reader_specs that holds storage and has complex
873 constructor and destructor behavior. */
874
875 class cutu_reader : public die_reader_specs
876 {
877 public:
878
879 cutu_reader (struct dwarf2_per_cu_data *this_cu,
880 struct abbrev_table *abbrev_table,
881 int use_existing_cu,
882 bool skip_partial);
883
884 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
885 struct dwarf2_cu *parent_cu = nullptr,
886 struct dwo_file *dwo_file = nullptr);
887
888 DISABLE_COPY_AND_ASSIGN (cutu_reader);
889
890 const gdb_byte *info_ptr = nullptr;
891 struct die_info *comp_unit_die = nullptr;
892 bool dummy_p = false;
893
894 /* Release the new CU, putting it on the chain. This cannot be done
895 for dummy CUs. */
896 void keep ();
897
898 private:
899 void init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
900 int use_existing_cu);
901
902 struct dwarf2_per_cu_data *m_this_cu;
903 std::unique_ptr<dwarf2_cu> m_new_cu;
904
905 /* The ordinary abbreviation table. */
906 abbrev_table_up m_abbrev_table_holder;
907
908 /* The DWO abbreviation table. */
909 abbrev_table_up m_dwo_abbrev_table;
910 };
911
912 /* When we construct a partial symbol table entry we only
913 need this much information. */
914 struct partial_die_info : public allocate_on_obstack
915 {
916 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
917
918 /* Disable assign but still keep copy ctor, which is needed
919 load_partial_dies. */
920 partial_die_info& operator=(const partial_die_info& rhs) = delete;
921
922 /* Adjust the partial die before generating a symbol for it. This
923 function may set the is_external flag or change the DIE's
924 name. */
925 void fixup (struct dwarf2_cu *cu);
926
927 /* Read a minimal amount of information into the minimal die
928 structure. */
929 const gdb_byte *read (const struct die_reader_specs *reader,
930 const struct abbrev_info &abbrev,
931 const gdb_byte *info_ptr);
932
933 /* Offset of this DIE. */
934 const sect_offset sect_off;
935
936 /* DWARF-2 tag for this DIE. */
937 const ENUM_BITFIELD(dwarf_tag) tag : 16;
938
939 /* Assorted flags describing the data found in this DIE. */
940 const unsigned int has_children : 1;
941
942 unsigned int is_external : 1;
943 unsigned int is_declaration : 1;
944 unsigned int has_type : 1;
945 unsigned int has_specification : 1;
946 unsigned int has_pc_info : 1;
947 unsigned int may_be_inlined : 1;
948
949 /* This DIE has been marked DW_AT_main_subprogram. */
950 unsigned int main_subprogram : 1;
951
952 /* Flag set if the SCOPE field of this structure has been
953 computed. */
954 unsigned int scope_set : 1;
955
956 /* Flag set if the DIE has a byte_size attribute. */
957 unsigned int has_byte_size : 1;
958
959 /* Flag set if the DIE has a DW_AT_const_value attribute. */
960 unsigned int has_const_value : 1;
961
962 /* Flag set if any of the DIE's children are template arguments. */
963 unsigned int has_template_arguments : 1;
964
965 /* Flag set if fixup has been called on this die. */
966 unsigned int fixup_called : 1;
967
968 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
969 unsigned int is_dwz : 1;
970
971 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
972 unsigned int spec_is_dwz : 1;
973
974 /* The name of this DIE. Normally the value of DW_AT_name, but
975 sometimes a default name for unnamed DIEs. */
976 const char *name = nullptr;
977
978 /* The linkage name, if present. */
979 const char *linkage_name = nullptr;
980
981 /* The scope to prepend to our children. This is generally
982 allocated on the comp_unit_obstack, so will disappear
983 when this compilation unit leaves the cache. */
984 const char *scope = nullptr;
985
986 /* Some data associated with the partial DIE. The tag determines
987 which field is live. */
988 union
989 {
990 /* The location description associated with this DIE, if any. */
991 struct dwarf_block *locdesc;
992 /* The offset of an import, for DW_TAG_imported_unit. */
993 sect_offset sect_off;
994 } d {};
995
996 /* If HAS_PC_INFO, the PC range associated with this DIE. */
997 CORE_ADDR lowpc = 0;
998 CORE_ADDR highpc = 0;
999
1000 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1001 DW_AT_sibling, if any. */
1002 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1003 could return DW_AT_sibling values to its caller load_partial_dies. */
1004 const gdb_byte *sibling = nullptr;
1005
1006 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1007 DW_AT_specification (or DW_AT_abstract_origin or
1008 DW_AT_extension). */
1009 sect_offset spec_offset {};
1010
1011 /* Pointers to this DIE's parent, first child, and next sibling,
1012 if any. */
1013 struct partial_die_info *die_parent = nullptr;
1014 struct partial_die_info *die_child = nullptr;
1015 struct partial_die_info *die_sibling = nullptr;
1016
1017 friend struct partial_die_info *
1018 dwarf2_cu::find_partial_die (sect_offset sect_off);
1019
1020 private:
1021 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1022 partial_die_info (sect_offset sect_off)
1023 : partial_die_info (sect_off, DW_TAG_padding, 0)
1024 {
1025 }
1026
1027 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1028 int has_children_)
1029 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1030 {
1031 is_external = 0;
1032 is_declaration = 0;
1033 has_type = 0;
1034 has_specification = 0;
1035 has_pc_info = 0;
1036 may_be_inlined = 0;
1037 main_subprogram = 0;
1038 scope_set = 0;
1039 has_byte_size = 0;
1040 has_const_value = 0;
1041 has_template_arguments = 0;
1042 fixup_called = 0;
1043 is_dwz = 0;
1044 spec_is_dwz = 0;
1045 }
1046 };
1047
1048 /* This data structure holds a complete die structure. */
1049 struct die_info
1050 {
1051 /* DWARF-2 tag for this DIE. */
1052 ENUM_BITFIELD(dwarf_tag) tag : 16;
1053
1054 /* Number of attributes */
1055 unsigned char num_attrs;
1056
1057 /* True if we're presently building the full type name for the
1058 type derived from this DIE. */
1059 unsigned char building_fullname : 1;
1060
1061 /* True if this die is in process. PR 16581. */
1062 unsigned char in_process : 1;
1063
1064 /* True if this DIE has children. */
1065 unsigned char has_children : 1;
1066
1067 /* Abbrev number */
1068 unsigned int abbrev;
1069
1070 /* Offset in .debug_info or .debug_types section. */
1071 sect_offset sect_off;
1072
1073 /* The dies in a compilation unit form an n-ary tree. PARENT
1074 points to this die's parent; CHILD points to the first child of
1075 this node; and all the children of a given node are chained
1076 together via their SIBLING fields. */
1077 struct die_info *child; /* Its first child, if any. */
1078 struct die_info *sibling; /* Its next sibling, if any. */
1079 struct die_info *parent; /* Its parent, if any. */
1080
1081 /* An array of attributes, with NUM_ATTRS elements. There may be
1082 zero, but it's not common and zero-sized arrays are not
1083 sufficiently portable C. */
1084 struct attribute attrs[1];
1085 };
1086
1087 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1088 but this would require a corresponding change in unpack_field_as_long
1089 and friends. */
1090 static int bits_per_byte = 8;
1091
1092 /* When reading a variant or variant part, we track a bit more
1093 information about the field, and store it in an object of this
1094 type. */
1095
1096 struct variant_field
1097 {
1098 /* If we see a DW_TAG_variant, then this will be the discriminant
1099 value. */
1100 ULONGEST discriminant_value;
1101 /* If we see a DW_TAG_variant, then this will be set if this is the
1102 default branch. */
1103 bool default_branch;
1104 /* While reading a DW_TAG_variant_part, this will be set if this
1105 field is the discriminant. */
1106 bool is_discriminant;
1107 };
1108
1109 struct nextfield
1110 {
1111 int accessibility = 0;
1112 int virtuality = 0;
1113 /* Extra information to describe a variant or variant part. */
1114 struct variant_field variant {};
1115 struct field field {};
1116 };
1117
1118 struct fnfieldlist
1119 {
1120 const char *name = nullptr;
1121 std::vector<struct fn_field> fnfields;
1122 };
1123
1124 /* The routines that read and process dies for a C struct or C++ class
1125 pass lists of data member fields and lists of member function fields
1126 in an instance of a field_info structure, as defined below. */
1127 struct field_info
1128 {
1129 /* List of data member and baseclasses fields. */
1130 std::vector<struct nextfield> fields;
1131 std::vector<struct nextfield> baseclasses;
1132
1133 /* Set if the accessibility of one of the fields is not public. */
1134 int non_public_fields = 0;
1135
1136 /* Member function fieldlist array, contains name of possibly overloaded
1137 member function, number of overloaded member functions and a pointer
1138 to the head of the member function field chain. */
1139 std::vector<struct fnfieldlist> fnfieldlists;
1140
1141 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1142 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1143 std::vector<struct decl_field> typedef_field_list;
1144
1145 /* Nested types defined by this class and the number of elements in this
1146 list. */
1147 std::vector<struct decl_field> nested_types_list;
1148
1149 /* Return the total number of fields (including baseclasses). */
1150 int nfields () const
1151 {
1152 return fields.size () + baseclasses.size ();
1153 }
1154 };
1155
1156 /* Loaded secondary compilation units are kept in memory until they
1157 have not been referenced for the processing of this many
1158 compilation units. Set this to zero to disable caching. Cache
1159 sizes of up to at least twenty will improve startup time for
1160 typical inter-CU-reference binaries, at an obvious memory cost. */
1161 static int dwarf_max_cache_age = 5;
1162 static void
1163 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1164 struct cmd_list_element *c, const char *value)
1165 {
1166 fprintf_filtered (file, _("The upper bound on the age of cached "
1167 "DWARF compilation units is %s.\n"),
1168 value);
1169 }
1170 \f
1171 /* local function prototypes */
1172
1173 static void dwarf2_find_base_address (struct die_info *die,
1174 struct dwarf2_cu *cu);
1175
1176 static dwarf2_psymtab *create_partial_symtab
1177 (struct dwarf2_per_cu_data *per_cu, const char *name);
1178
1179 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1180 const gdb_byte *info_ptr,
1181 struct die_info *type_unit_die);
1182
1183 static void dwarf2_build_psymtabs_hard
1184 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1185
1186 static void scan_partial_symbols (struct partial_die_info *,
1187 CORE_ADDR *, CORE_ADDR *,
1188 int, struct dwarf2_cu *);
1189
1190 static void add_partial_symbol (struct partial_die_info *,
1191 struct dwarf2_cu *);
1192
1193 static void add_partial_namespace (struct partial_die_info *pdi,
1194 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1195 int set_addrmap, struct dwarf2_cu *cu);
1196
1197 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1198 CORE_ADDR *highpc, int set_addrmap,
1199 struct dwarf2_cu *cu);
1200
1201 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1202 struct dwarf2_cu *cu);
1203
1204 static void add_partial_subprogram (struct partial_die_info *pdi,
1205 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1206 int need_pc, struct dwarf2_cu *cu);
1207
1208 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1209
1210 static struct partial_die_info *load_partial_dies
1211 (const struct die_reader_specs *, const gdb_byte *, int);
1212
1213 /* A pair of partial_die_info and compilation unit. */
1214 struct cu_partial_die_info
1215 {
1216 /* The compilation unit of the partial_die_info. */
1217 struct dwarf2_cu *cu;
1218 /* A partial_die_info. */
1219 struct partial_die_info *pdi;
1220
1221 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1222 : cu (cu),
1223 pdi (pdi)
1224 { /* Nothing. */ }
1225
1226 private:
1227 cu_partial_die_info () = delete;
1228 };
1229
1230 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1231 struct dwarf2_cu *);
1232
1233 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1234 struct attribute *, struct attr_abbrev *,
1235 const gdb_byte *, bool *need_reprocess);
1236
1237 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1238 struct attribute *attr);
1239
1240 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1241
1242 static LONGEST read_checked_initial_length_and_offset
1243 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1244 unsigned int *, unsigned int *);
1245
1246 static sect_offset read_abbrev_offset
1247 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1248 struct dwarf2_section_info *, sect_offset);
1249
1250 static const char *read_indirect_string
1251 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1252 const struct comp_unit_head *, unsigned int *);
1253
1254 static const char *read_indirect_line_string
1255 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1256 const struct comp_unit_head *, unsigned int *);
1257
1258 static const char *read_indirect_string_at_offset
1259 (struct dwarf2_per_objfile *dwarf2_per_objfile, LONGEST str_offset);
1260
1261 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1262 const gdb_byte *,
1263 unsigned int *);
1264
1265 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1266 ULONGEST str_index);
1267
1268 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1269 ULONGEST str_index);
1270
1271 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1272
1273 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1274 struct dwarf2_cu *);
1275
1276 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1277 unsigned int);
1278
1279 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1280 struct dwarf2_cu *cu);
1281
1282 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1283
1284 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1285 struct dwarf2_cu *cu);
1286
1287 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1288
1289 static struct die_info *die_specification (struct die_info *die,
1290 struct dwarf2_cu **);
1291
1292 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1293 struct dwarf2_cu *cu);
1294
1295 static void dwarf_decode_lines (struct line_header *, const char *,
1296 struct dwarf2_cu *, dwarf2_psymtab *,
1297 CORE_ADDR, int decode_mapping);
1298
1299 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1300 const char *);
1301
1302 static struct symbol *new_symbol (struct die_info *, struct type *,
1303 struct dwarf2_cu *, struct symbol * = NULL);
1304
1305 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1306 struct dwarf2_cu *);
1307
1308 static void dwarf2_const_value_attr (const struct attribute *attr,
1309 struct type *type,
1310 const char *name,
1311 struct obstack *obstack,
1312 struct dwarf2_cu *cu, LONGEST *value,
1313 const gdb_byte **bytes,
1314 struct dwarf2_locexpr_baton **baton);
1315
1316 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1317
1318 static int need_gnat_info (struct dwarf2_cu *);
1319
1320 static struct type *die_descriptive_type (struct die_info *,
1321 struct dwarf2_cu *);
1322
1323 static void set_descriptive_type (struct type *, struct die_info *,
1324 struct dwarf2_cu *);
1325
1326 static struct type *die_containing_type (struct die_info *,
1327 struct dwarf2_cu *);
1328
1329 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1330 struct dwarf2_cu *);
1331
1332 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1333
1334 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1335
1336 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1337
1338 static char *typename_concat (struct obstack *obs, const char *prefix,
1339 const char *suffix, int physname,
1340 struct dwarf2_cu *cu);
1341
1342 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1343
1344 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1345
1346 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1347
1348 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1349
1350 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1351
1352 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1353
1354 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1355 struct dwarf2_cu *, dwarf2_psymtab *);
1356
1357 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1358 values. Keep the items ordered with increasing constraints compliance. */
1359 enum pc_bounds_kind
1360 {
1361 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1362 PC_BOUNDS_NOT_PRESENT,
1363
1364 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1365 were present but they do not form a valid range of PC addresses. */
1366 PC_BOUNDS_INVALID,
1367
1368 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1369 PC_BOUNDS_RANGES,
1370
1371 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1372 PC_BOUNDS_HIGH_LOW,
1373 };
1374
1375 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1376 CORE_ADDR *, CORE_ADDR *,
1377 struct dwarf2_cu *,
1378 dwarf2_psymtab *);
1379
1380 static void get_scope_pc_bounds (struct die_info *,
1381 CORE_ADDR *, CORE_ADDR *,
1382 struct dwarf2_cu *);
1383
1384 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1385 CORE_ADDR, struct dwarf2_cu *);
1386
1387 static void dwarf2_add_field (struct field_info *, struct die_info *,
1388 struct dwarf2_cu *);
1389
1390 static void dwarf2_attach_fields_to_type (struct field_info *,
1391 struct type *, struct dwarf2_cu *);
1392
1393 static void dwarf2_add_member_fn (struct field_info *,
1394 struct die_info *, struct type *,
1395 struct dwarf2_cu *);
1396
1397 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1398 struct type *,
1399 struct dwarf2_cu *);
1400
1401 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1402
1403 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1404
1405 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1406
1407 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1408
1409 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1410
1411 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1412
1413 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1414
1415 static struct type *read_module_type (struct die_info *die,
1416 struct dwarf2_cu *cu);
1417
1418 static const char *namespace_name (struct die_info *die,
1419 int *is_anonymous, struct dwarf2_cu *);
1420
1421 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1422
1423 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1424
1425 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1426 struct dwarf2_cu *);
1427
1428 static struct die_info *read_die_and_siblings_1
1429 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1430 struct die_info *);
1431
1432 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1433 const gdb_byte *info_ptr,
1434 const gdb_byte **new_info_ptr,
1435 struct die_info *parent);
1436
1437 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1438 struct die_info **, const gdb_byte *,
1439 int);
1440
1441 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1442 struct die_info **, const gdb_byte *);
1443
1444 static void process_die (struct die_info *, struct dwarf2_cu *);
1445
1446 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1447 struct objfile *);
1448
1449 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1450
1451 static const char *dwarf2_full_name (const char *name,
1452 struct die_info *die,
1453 struct dwarf2_cu *cu);
1454
1455 static const char *dwarf2_physname (const char *name, struct die_info *die,
1456 struct dwarf2_cu *cu);
1457
1458 static struct die_info *dwarf2_extension (struct die_info *die,
1459 struct dwarf2_cu **);
1460
1461 static const char *dwarf_tag_name (unsigned int);
1462
1463 static const char *dwarf_attr_name (unsigned int);
1464
1465 static const char *dwarf_form_name (unsigned int);
1466
1467 static const char *dwarf_bool_name (unsigned int);
1468
1469 static const char *dwarf_type_encoding_name (unsigned int);
1470
1471 static struct die_info *sibling_die (struct die_info *);
1472
1473 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1474
1475 static void dump_die_for_error (struct die_info *);
1476
1477 static void dump_die_1 (struct ui_file *, int level, int max_level,
1478 struct die_info *);
1479
1480 /*static*/ void dump_die (struct die_info *, int max_level);
1481
1482 static void store_in_ref_table (struct die_info *,
1483 struct dwarf2_cu *);
1484
1485 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1486
1487 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1488
1489 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1490 const struct attribute *,
1491 struct dwarf2_cu **);
1492
1493 static struct die_info *follow_die_ref (struct die_info *,
1494 const struct attribute *,
1495 struct dwarf2_cu **);
1496
1497 static struct die_info *follow_die_sig (struct die_info *,
1498 const struct attribute *,
1499 struct dwarf2_cu **);
1500
1501 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1502 struct dwarf2_cu *);
1503
1504 static struct type *get_DW_AT_signature_type (struct die_info *,
1505 const struct attribute *,
1506 struct dwarf2_cu *);
1507
1508 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1509
1510 static void read_signatured_type (struct signatured_type *);
1511
1512 static int attr_to_dynamic_prop (const struct attribute *attr,
1513 struct die_info *die, struct dwarf2_cu *cu,
1514 struct dynamic_prop *prop, struct type *type);
1515
1516 /* memory allocation interface */
1517
1518 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1519
1520 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1521
1522 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1523
1524 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1525 struct dwarf2_loclist_baton *baton,
1526 const struct attribute *attr);
1527
1528 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1529 struct symbol *sym,
1530 struct dwarf2_cu *cu,
1531 int is_block);
1532
1533 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1534 const gdb_byte *info_ptr,
1535 struct abbrev_info *abbrev);
1536
1537 static hashval_t partial_die_hash (const void *item);
1538
1539 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1540
1541 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1542 (sect_offset sect_off, unsigned int offset_in_dwz,
1543 struct dwarf2_per_objfile *dwarf2_per_objfile);
1544
1545 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1546 struct die_info *comp_unit_die,
1547 enum language pretend_language);
1548
1549 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1550
1551 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1552
1553 static struct type *set_die_type (struct die_info *, struct type *,
1554 struct dwarf2_cu *);
1555
1556 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1557
1558 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1559
1560 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1561 enum language);
1562
1563 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1564 enum language);
1565
1566 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1567 enum language);
1568
1569 static void dwarf2_add_dependence (struct dwarf2_cu *,
1570 struct dwarf2_per_cu_data *);
1571
1572 static void dwarf2_mark (struct dwarf2_cu *);
1573
1574 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1575
1576 static struct type *get_die_type_at_offset (sect_offset,
1577 struct dwarf2_per_cu_data *);
1578
1579 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1580
1581 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1582 enum language pretend_language);
1583
1584 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1585
1586 /* Class, the destructor of which frees all allocated queue entries. This
1587 will only have work to do if an error was thrown while processing the
1588 dwarf. If no error was thrown then the queue entries should have all
1589 been processed, and freed, as we went along. */
1590
1591 class dwarf2_queue_guard
1592 {
1593 public:
1594 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1595 : m_per_objfile (per_objfile)
1596 {
1597 }
1598
1599 /* Free any entries remaining on the queue. There should only be
1600 entries left if we hit an error while processing the dwarf. */
1601 ~dwarf2_queue_guard ()
1602 {
1603 /* Ensure that no memory is allocated by the queue. */
1604 std::queue<dwarf2_queue_item> empty;
1605 std::swap (m_per_objfile->queue, empty);
1606 }
1607
1608 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1609
1610 private:
1611 dwarf2_per_objfile *m_per_objfile;
1612 };
1613
1614 dwarf2_queue_item::~dwarf2_queue_item ()
1615 {
1616 /* Anything still marked queued is likely to be in an
1617 inconsistent state, so discard it. */
1618 if (per_cu->queued)
1619 {
1620 if (per_cu->cu != NULL)
1621 free_one_cached_comp_unit (per_cu);
1622 per_cu->queued = 0;
1623 }
1624 }
1625
1626 /* The return type of find_file_and_directory. Note, the enclosed
1627 string pointers are only valid while this object is valid. */
1628
1629 struct file_and_directory
1630 {
1631 /* The filename. This is never NULL. */
1632 const char *name;
1633
1634 /* The compilation directory. NULL if not known. If we needed to
1635 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1636 points directly to the DW_AT_comp_dir string attribute owned by
1637 the obstack that owns the DIE. */
1638 const char *comp_dir;
1639
1640 /* If we needed to build a new string for comp_dir, this is what
1641 owns the storage. */
1642 std::string comp_dir_storage;
1643 };
1644
1645 static file_and_directory find_file_and_directory (struct die_info *die,
1646 struct dwarf2_cu *cu);
1647
1648 static htab_up allocate_signatured_type_table ();
1649
1650 static htab_up allocate_dwo_unit_table ();
1651
1652 static struct dwo_unit *lookup_dwo_unit_in_dwp
1653 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1654 struct dwp_file *dwp_file, const char *comp_dir,
1655 ULONGEST signature, int is_debug_types);
1656
1657 static struct dwp_file *get_dwp_file
1658 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1659
1660 static struct dwo_unit *lookup_dwo_comp_unit
1661 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1662
1663 static struct dwo_unit *lookup_dwo_type_unit
1664 (struct signatured_type *, const char *, const char *);
1665
1666 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1667
1668 /* A unique pointer to a dwo_file. */
1669
1670 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1671
1672 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
1673
1674 static void check_producer (struct dwarf2_cu *cu);
1675
1676 static void free_line_header_voidp (void *arg);
1677 \f
1678 /* Various complaints about symbol reading that don't abort the process. */
1679
1680 static void
1681 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1682 {
1683 complaint (_("statement list doesn't fit in .debug_line section"));
1684 }
1685
1686 static void
1687 dwarf2_debug_line_missing_file_complaint (void)
1688 {
1689 complaint (_(".debug_line section has line data without a file"));
1690 }
1691
1692 static void
1693 dwarf2_debug_line_missing_end_sequence_complaint (void)
1694 {
1695 complaint (_(".debug_line section has line "
1696 "program sequence without an end"));
1697 }
1698
1699 static void
1700 dwarf2_complex_location_expr_complaint (void)
1701 {
1702 complaint (_("location expression too complex"));
1703 }
1704
1705 static void
1706 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1707 int arg3)
1708 {
1709 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1710 arg1, arg2, arg3);
1711 }
1712
1713 static void
1714 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1715 {
1716 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1717 arg1, arg2);
1718 }
1719
1720 /* Hash function for line_header_hash. */
1721
1722 static hashval_t
1723 line_header_hash (const struct line_header *ofs)
1724 {
1725 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1726 }
1727
1728 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1729
1730 static hashval_t
1731 line_header_hash_voidp (const void *item)
1732 {
1733 const struct line_header *ofs = (const struct line_header *) item;
1734
1735 return line_header_hash (ofs);
1736 }
1737
1738 /* Equality function for line_header_hash. */
1739
1740 static int
1741 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1742 {
1743 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1744 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1745
1746 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1747 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1748 }
1749
1750 \f
1751
1752 /* See declaration. */
1753
1754 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
1755 const dwarf2_debug_sections *names,
1756 bool can_copy_)
1757 : objfile (objfile_),
1758 can_copy (can_copy_)
1759 {
1760 if (names == NULL)
1761 names = &dwarf2_elf_names;
1762
1763 bfd *obfd = objfile->obfd;
1764
1765 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1766 locate_sections (obfd, sec, *names);
1767 }
1768
1769 dwarf2_per_objfile::~dwarf2_per_objfile ()
1770 {
1771 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1772 free_cached_comp_units ();
1773
1774 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1775 per_cu->imported_symtabs_free ();
1776
1777 for (signatured_type *sig_type : all_type_units)
1778 sig_type->per_cu.imported_symtabs_free ();
1779
1780 /* Everything else should be on the objfile obstack. */
1781 }
1782
1783 /* See declaration. */
1784
1785 void
1786 dwarf2_per_objfile::free_cached_comp_units ()
1787 {
1788 dwarf2_per_cu_data *per_cu = read_in_chain;
1789 dwarf2_per_cu_data **last_chain = &read_in_chain;
1790 while (per_cu != NULL)
1791 {
1792 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1793
1794 delete per_cu->cu;
1795 *last_chain = next_cu;
1796 per_cu = next_cu;
1797 }
1798 }
1799
1800 /* A helper class that calls free_cached_comp_units on
1801 destruction. */
1802
1803 class free_cached_comp_units
1804 {
1805 public:
1806
1807 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1808 : m_per_objfile (per_objfile)
1809 {
1810 }
1811
1812 ~free_cached_comp_units ()
1813 {
1814 m_per_objfile->free_cached_comp_units ();
1815 }
1816
1817 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1818
1819 private:
1820
1821 dwarf2_per_objfile *m_per_objfile;
1822 };
1823
1824 /* Try to locate the sections we need for DWARF 2 debugging
1825 information and return true if we have enough to do something.
1826 NAMES points to the dwarf2 section names, or is NULL if the standard
1827 ELF names are used. CAN_COPY is true for formats where symbol
1828 interposition is possible and so symbol values must follow copy
1829 relocation rules. */
1830
1831 int
1832 dwarf2_has_info (struct objfile *objfile,
1833 const struct dwarf2_debug_sections *names,
1834 bool can_copy)
1835 {
1836 if (objfile->flags & OBJF_READNEVER)
1837 return 0;
1838
1839 struct dwarf2_per_objfile *dwarf2_per_objfile
1840 = get_dwarf2_per_objfile (objfile);
1841
1842 if (dwarf2_per_objfile == NULL)
1843 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
1844 names,
1845 can_copy);
1846
1847 return (!dwarf2_per_objfile->info.is_virtual
1848 && dwarf2_per_objfile->info.s.section != NULL
1849 && !dwarf2_per_objfile->abbrev.is_virtual
1850 && dwarf2_per_objfile->abbrev.s.section != NULL);
1851 }
1852
1853 /* When loading sections, we look either for uncompressed section or for
1854 compressed section names. */
1855
1856 static int
1857 section_is_p (const char *section_name,
1858 const struct dwarf2_section_names *names)
1859 {
1860 if (names->normal != NULL
1861 && strcmp (section_name, names->normal) == 0)
1862 return 1;
1863 if (names->compressed != NULL
1864 && strcmp (section_name, names->compressed) == 0)
1865 return 1;
1866 return 0;
1867 }
1868
1869 /* See declaration. */
1870
1871 void
1872 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
1873 const dwarf2_debug_sections &names)
1874 {
1875 flagword aflag = bfd_section_flags (sectp);
1876
1877 if ((aflag & SEC_HAS_CONTENTS) == 0)
1878 {
1879 }
1880 else if (elf_section_data (sectp)->this_hdr.sh_size
1881 > bfd_get_file_size (abfd))
1882 {
1883 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1884 warning (_("Discarding section %s which has a section size (%s"
1885 ") larger than the file size [in module %s]"),
1886 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1887 bfd_get_filename (abfd));
1888 }
1889 else if (section_is_p (sectp->name, &names.info))
1890 {
1891 this->info.s.section = sectp;
1892 this->info.size = bfd_section_size (sectp);
1893 }
1894 else if (section_is_p (sectp->name, &names.abbrev))
1895 {
1896 this->abbrev.s.section = sectp;
1897 this->abbrev.size = bfd_section_size (sectp);
1898 }
1899 else if (section_is_p (sectp->name, &names.line))
1900 {
1901 this->line.s.section = sectp;
1902 this->line.size = bfd_section_size (sectp);
1903 }
1904 else if (section_is_p (sectp->name, &names.loc))
1905 {
1906 this->loc.s.section = sectp;
1907 this->loc.size = bfd_section_size (sectp);
1908 }
1909 else if (section_is_p (sectp->name, &names.loclists))
1910 {
1911 this->loclists.s.section = sectp;
1912 this->loclists.size = bfd_section_size (sectp);
1913 }
1914 else if (section_is_p (sectp->name, &names.macinfo))
1915 {
1916 this->macinfo.s.section = sectp;
1917 this->macinfo.size = bfd_section_size (sectp);
1918 }
1919 else if (section_is_p (sectp->name, &names.macro))
1920 {
1921 this->macro.s.section = sectp;
1922 this->macro.size = bfd_section_size (sectp);
1923 }
1924 else if (section_is_p (sectp->name, &names.str))
1925 {
1926 this->str.s.section = sectp;
1927 this->str.size = bfd_section_size (sectp);
1928 }
1929 else if (section_is_p (sectp->name, &names.str_offsets))
1930 {
1931 this->str_offsets.s.section = sectp;
1932 this->str_offsets.size = bfd_section_size (sectp);
1933 }
1934 else if (section_is_p (sectp->name, &names.line_str))
1935 {
1936 this->line_str.s.section = sectp;
1937 this->line_str.size = bfd_section_size (sectp);
1938 }
1939 else if (section_is_p (sectp->name, &names.addr))
1940 {
1941 this->addr.s.section = sectp;
1942 this->addr.size = bfd_section_size (sectp);
1943 }
1944 else if (section_is_p (sectp->name, &names.frame))
1945 {
1946 this->frame.s.section = sectp;
1947 this->frame.size = bfd_section_size (sectp);
1948 }
1949 else if (section_is_p (sectp->name, &names.eh_frame))
1950 {
1951 this->eh_frame.s.section = sectp;
1952 this->eh_frame.size = bfd_section_size (sectp);
1953 }
1954 else if (section_is_p (sectp->name, &names.ranges))
1955 {
1956 this->ranges.s.section = sectp;
1957 this->ranges.size = bfd_section_size (sectp);
1958 }
1959 else if (section_is_p (sectp->name, &names.rnglists))
1960 {
1961 this->rnglists.s.section = sectp;
1962 this->rnglists.size = bfd_section_size (sectp);
1963 }
1964 else if (section_is_p (sectp->name, &names.types))
1965 {
1966 struct dwarf2_section_info type_section;
1967
1968 memset (&type_section, 0, sizeof (type_section));
1969 type_section.s.section = sectp;
1970 type_section.size = bfd_section_size (sectp);
1971
1972 this->types.push_back (type_section);
1973 }
1974 else if (section_is_p (sectp->name, &names.gdb_index))
1975 {
1976 this->gdb_index.s.section = sectp;
1977 this->gdb_index.size = bfd_section_size (sectp);
1978 }
1979 else if (section_is_p (sectp->name, &names.debug_names))
1980 {
1981 this->debug_names.s.section = sectp;
1982 this->debug_names.size = bfd_section_size (sectp);
1983 }
1984 else if (section_is_p (sectp->name, &names.debug_aranges))
1985 {
1986 this->debug_aranges.s.section = sectp;
1987 this->debug_aranges.size = bfd_section_size (sectp);
1988 }
1989
1990 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
1991 && bfd_section_vma (sectp) == 0)
1992 this->has_section_at_zero = true;
1993 }
1994
1995 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1996 SECTION_NAME. */
1997
1998 void
1999 dwarf2_get_section_info (struct objfile *objfile,
2000 enum dwarf2_section_enum sect,
2001 asection **sectp, const gdb_byte **bufp,
2002 bfd_size_type *sizep)
2003 {
2004 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
2005 struct dwarf2_section_info *info;
2006
2007 /* We may see an objfile without any DWARF, in which case we just
2008 return nothing. */
2009 if (data == NULL)
2010 {
2011 *sectp = NULL;
2012 *bufp = NULL;
2013 *sizep = 0;
2014 return;
2015 }
2016 switch (sect)
2017 {
2018 case DWARF2_DEBUG_FRAME:
2019 info = &data->frame;
2020 break;
2021 case DWARF2_EH_FRAME:
2022 info = &data->eh_frame;
2023 break;
2024 default:
2025 gdb_assert_not_reached ("unexpected section");
2026 }
2027
2028 info->read (objfile);
2029
2030 *sectp = info->get_bfd_section ();
2031 *bufp = info->buffer;
2032 *sizep = info->size;
2033 }
2034
2035 /* A helper function to find the sections for a .dwz file. */
2036
2037 static void
2038 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2039 {
2040 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2041
2042 /* Note that we only support the standard ELF names, because .dwz
2043 is ELF-only (at the time of writing). */
2044 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2045 {
2046 dwz_file->abbrev.s.section = sectp;
2047 dwz_file->abbrev.size = bfd_section_size (sectp);
2048 }
2049 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2050 {
2051 dwz_file->info.s.section = sectp;
2052 dwz_file->info.size = bfd_section_size (sectp);
2053 }
2054 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2055 {
2056 dwz_file->str.s.section = sectp;
2057 dwz_file->str.size = bfd_section_size (sectp);
2058 }
2059 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2060 {
2061 dwz_file->line.s.section = sectp;
2062 dwz_file->line.size = bfd_section_size (sectp);
2063 }
2064 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2065 {
2066 dwz_file->macro.s.section = sectp;
2067 dwz_file->macro.size = bfd_section_size (sectp);
2068 }
2069 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2070 {
2071 dwz_file->gdb_index.s.section = sectp;
2072 dwz_file->gdb_index.size = bfd_section_size (sectp);
2073 }
2074 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2075 {
2076 dwz_file->debug_names.s.section = sectp;
2077 dwz_file->debug_names.size = bfd_section_size (sectp);
2078 }
2079 }
2080
2081 /* See dwarf2read.h. */
2082
2083 struct dwz_file *
2084 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2085 {
2086 const char *filename;
2087 bfd_size_type buildid_len_arg;
2088 size_t buildid_len;
2089 bfd_byte *buildid;
2090
2091 if (dwarf2_per_objfile->dwz_file != NULL)
2092 return dwarf2_per_objfile->dwz_file.get ();
2093
2094 bfd_set_error (bfd_error_no_error);
2095 gdb::unique_xmalloc_ptr<char> data
2096 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2097 &buildid_len_arg, &buildid));
2098 if (data == NULL)
2099 {
2100 if (bfd_get_error () == bfd_error_no_error)
2101 return NULL;
2102 error (_("could not read '.gnu_debugaltlink' section: %s"),
2103 bfd_errmsg (bfd_get_error ()));
2104 }
2105
2106 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2107
2108 buildid_len = (size_t) buildid_len_arg;
2109
2110 filename = data.get ();
2111
2112 std::string abs_storage;
2113 if (!IS_ABSOLUTE_PATH (filename))
2114 {
2115 gdb::unique_xmalloc_ptr<char> abs
2116 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2117
2118 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2119 filename = abs_storage.c_str ();
2120 }
2121
2122 /* First try the file name given in the section. If that doesn't
2123 work, try to use the build-id instead. */
2124 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2125 if (dwz_bfd != NULL)
2126 {
2127 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2128 dwz_bfd.reset (nullptr);
2129 }
2130
2131 if (dwz_bfd == NULL)
2132 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2133
2134 if (dwz_bfd == nullptr)
2135 {
2136 gdb::unique_xmalloc_ptr<char> alt_filename;
2137 const char *origname = dwarf2_per_objfile->objfile->original_name;
2138
2139 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2140 buildid_len,
2141 origname,
2142 &alt_filename));
2143
2144 if (fd.get () >= 0)
2145 {
2146 /* File successfully retrieved from server. */
2147 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget, -1);
2148
2149 if (dwz_bfd == nullptr)
2150 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2151 alt_filename.get ());
2152 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2153 dwz_bfd.reset (nullptr);
2154 }
2155 }
2156
2157 if (dwz_bfd == NULL)
2158 error (_("could not find '.gnu_debugaltlink' file for %s"),
2159 objfile_name (dwarf2_per_objfile->objfile));
2160
2161 std::unique_ptr<struct dwz_file> result
2162 (new struct dwz_file (std::move (dwz_bfd)));
2163
2164 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2165 result.get ());
2166
2167 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2168 result->dwz_bfd.get ());
2169 dwarf2_per_objfile->dwz_file = std::move (result);
2170 return dwarf2_per_objfile->dwz_file.get ();
2171 }
2172 \f
2173 /* DWARF quick_symbols_functions support. */
2174
2175 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2176 unique line tables, so we maintain a separate table of all .debug_line
2177 derived entries to support the sharing.
2178 All the quick functions need is the list of file names. We discard the
2179 line_header when we're done and don't need to record it here. */
2180 struct quick_file_names
2181 {
2182 /* The data used to construct the hash key. */
2183 struct stmt_list_hash hash;
2184
2185 /* The number of entries in file_names, real_names. */
2186 unsigned int num_file_names;
2187
2188 /* The file names from the line table, after being run through
2189 file_full_name. */
2190 const char **file_names;
2191
2192 /* The file names from the line table after being run through
2193 gdb_realpath. These are computed lazily. */
2194 const char **real_names;
2195 };
2196
2197 /* When using the index (and thus not using psymtabs), each CU has an
2198 object of this type. This is used to hold information needed by
2199 the various "quick" methods. */
2200 struct dwarf2_per_cu_quick_data
2201 {
2202 /* The file table. This can be NULL if there was no file table
2203 or it's currently not read in.
2204 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2205 struct quick_file_names *file_names;
2206
2207 /* The corresponding symbol table. This is NULL if symbols for this
2208 CU have not yet been read. */
2209 struct compunit_symtab *compunit_symtab;
2210
2211 /* A temporary mark bit used when iterating over all CUs in
2212 expand_symtabs_matching. */
2213 unsigned int mark : 1;
2214
2215 /* True if we've tried to read the file table and found there isn't one.
2216 There will be no point in trying to read it again next time. */
2217 unsigned int no_file_data : 1;
2218 };
2219
2220 /* Utility hash function for a stmt_list_hash. */
2221
2222 static hashval_t
2223 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2224 {
2225 hashval_t v = 0;
2226
2227 if (stmt_list_hash->dwo_unit != NULL)
2228 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2229 v += to_underlying (stmt_list_hash->line_sect_off);
2230 return v;
2231 }
2232
2233 /* Utility equality function for a stmt_list_hash. */
2234
2235 static int
2236 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2237 const struct stmt_list_hash *rhs)
2238 {
2239 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2240 return 0;
2241 if (lhs->dwo_unit != NULL
2242 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2243 return 0;
2244
2245 return lhs->line_sect_off == rhs->line_sect_off;
2246 }
2247
2248 /* Hash function for a quick_file_names. */
2249
2250 static hashval_t
2251 hash_file_name_entry (const void *e)
2252 {
2253 const struct quick_file_names *file_data
2254 = (const struct quick_file_names *) e;
2255
2256 return hash_stmt_list_entry (&file_data->hash);
2257 }
2258
2259 /* Equality function for a quick_file_names. */
2260
2261 static int
2262 eq_file_name_entry (const void *a, const void *b)
2263 {
2264 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2265 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2266
2267 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2268 }
2269
2270 /* Delete function for a quick_file_names. */
2271
2272 static void
2273 delete_file_name_entry (void *e)
2274 {
2275 struct quick_file_names *file_data = (struct quick_file_names *) e;
2276 int i;
2277
2278 for (i = 0; i < file_data->num_file_names; ++i)
2279 {
2280 xfree ((void*) file_data->file_names[i]);
2281 if (file_data->real_names)
2282 xfree ((void*) file_data->real_names[i]);
2283 }
2284
2285 /* The space for the struct itself lives on objfile_obstack,
2286 so we don't free it here. */
2287 }
2288
2289 /* Create a quick_file_names hash table. */
2290
2291 static htab_up
2292 create_quick_file_names_table (unsigned int nr_initial_entries)
2293 {
2294 return htab_up (htab_create_alloc (nr_initial_entries,
2295 hash_file_name_entry, eq_file_name_entry,
2296 delete_file_name_entry, xcalloc, xfree));
2297 }
2298
2299 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2300 have to be created afterwards. You should call age_cached_comp_units after
2301 processing PER_CU->CU. dw2_setup must have been already called. */
2302
2303 static void
2304 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2305 {
2306 if (per_cu->is_debug_types)
2307 load_full_type_unit (per_cu);
2308 else
2309 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2310
2311 if (per_cu->cu == NULL)
2312 return; /* Dummy CU. */
2313
2314 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2315 }
2316
2317 /* Read in the symbols for PER_CU. */
2318
2319 static void
2320 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2321 {
2322 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2323
2324 /* Skip type_unit_groups, reading the type units they contain
2325 is handled elsewhere. */
2326 if (per_cu->type_unit_group_p ())
2327 return;
2328
2329 /* The destructor of dwarf2_queue_guard frees any entries left on
2330 the queue. After this point we're guaranteed to leave this function
2331 with the dwarf queue empty. */
2332 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
2333
2334 if (dwarf2_per_objfile->using_index
2335 ? per_cu->v.quick->compunit_symtab == NULL
2336 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2337 {
2338 queue_comp_unit (per_cu, language_minimal);
2339 load_cu (per_cu, skip_partial);
2340
2341 /* If we just loaded a CU from a DWO, and we're working with an index
2342 that may badly handle TUs, load all the TUs in that DWO as well.
2343 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2344 if (!per_cu->is_debug_types
2345 && per_cu->cu != NULL
2346 && per_cu->cu->dwo_unit != NULL
2347 && dwarf2_per_objfile->index_table != NULL
2348 && dwarf2_per_objfile->index_table->version <= 7
2349 /* DWP files aren't supported yet. */
2350 && get_dwp_file (dwarf2_per_objfile) == NULL)
2351 queue_and_load_all_dwo_tus (per_cu);
2352 }
2353
2354 process_queue (dwarf2_per_objfile);
2355
2356 /* Age the cache, releasing compilation units that have not
2357 been used recently. */
2358 age_cached_comp_units (dwarf2_per_objfile);
2359 }
2360
2361 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2362 the objfile from which this CU came. Returns the resulting symbol
2363 table. */
2364
2365 static struct compunit_symtab *
2366 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2367 {
2368 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2369
2370 gdb_assert (dwarf2_per_objfile->using_index);
2371 if (!per_cu->v.quick->compunit_symtab)
2372 {
2373 free_cached_comp_units freer (dwarf2_per_objfile);
2374 scoped_restore decrementer = increment_reading_symtab ();
2375 dw2_do_instantiate_symtab (per_cu, skip_partial);
2376 process_cu_includes (dwarf2_per_objfile);
2377 }
2378
2379 return per_cu->v.quick->compunit_symtab;
2380 }
2381
2382 /* See declaration. */
2383
2384 dwarf2_per_cu_data *
2385 dwarf2_per_objfile::get_cutu (int index)
2386 {
2387 if (index >= this->all_comp_units.size ())
2388 {
2389 index -= this->all_comp_units.size ();
2390 gdb_assert (index < this->all_type_units.size ());
2391 return &this->all_type_units[index]->per_cu;
2392 }
2393
2394 return this->all_comp_units[index];
2395 }
2396
2397 /* See declaration. */
2398
2399 dwarf2_per_cu_data *
2400 dwarf2_per_objfile::get_cu (int index)
2401 {
2402 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2403
2404 return this->all_comp_units[index];
2405 }
2406
2407 /* See declaration. */
2408
2409 signatured_type *
2410 dwarf2_per_objfile::get_tu (int index)
2411 {
2412 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2413
2414 return this->all_type_units[index];
2415 }
2416
2417 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2418 objfile_obstack, and constructed with the specified field
2419 values. */
2420
2421 static dwarf2_per_cu_data *
2422 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2423 struct dwarf2_section_info *section,
2424 int is_dwz,
2425 sect_offset sect_off, ULONGEST length)
2426 {
2427 struct objfile *objfile = dwarf2_per_objfile->objfile;
2428 dwarf2_per_cu_data *the_cu
2429 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2430 struct dwarf2_per_cu_data);
2431 the_cu->sect_off = sect_off;
2432 the_cu->length = length;
2433 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2434 the_cu->section = section;
2435 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2436 struct dwarf2_per_cu_quick_data);
2437 the_cu->is_dwz = is_dwz;
2438 return the_cu;
2439 }
2440
2441 /* A helper for create_cus_from_index that handles a given list of
2442 CUs. */
2443
2444 static void
2445 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2446 const gdb_byte *cu_list, offset_type n_elements,
2447 struct dwarf2_section_info *section,
2448 int is_dwz)
2449 {
2450 for (offset_type i = 0; i < n_elements; i += 2)
2451 {
2452 gdb_static_assert (sizeof (ULONGEST) >= 8);
2453
2454 sect_offset sect_off
2455 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2456 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2457 cu_list += 2 * 8;
2458
2459 dwarf2_per_cu_data *per_cu
2460 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2461 sect_off, length);
2462 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
2463 }
2464 }
2465
2466 /* Read the CU list from the mapped index, and use it to create all
2467 the CU objects for this objfile. */
2468
2469 static void
2470 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2471 const gdb_byte *cu_list, offset_type cu_list_elements,
2472 const gdb_byte *dwz_list, offset_type dwz_elements)
2473 {
2474 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
2475 dwarf2_per_objfile->all_comp_units.reserve
2476 ((cu_list_elements + dwz_elements) / 2);
2477
2478 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
2479 &dwarf2_per_objfile->info, 0);
2480
2481 if (dwz_elements == 0)
2482 return;
2483
2484 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2485 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
2486 &dwz->info, 1);
2487 }
2488
2489 /* Create the signatured type hash table from the index. */
2490
2491 static void
2492 create_signatured_type_table_from_index
2493 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2494 struct dwarf2_section_info *section,
2495 const gdb_byte *bytes,
2496 offset_type elements)
2497 {
2498 struct objfile *objfile = dwarf2_per_objfile->objfile;
2499
2500 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2501 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
2502
2503 htab_up sig_types_hash = allocate_signatured_type_table ();
2504
2505 for (offset_type i = 0; i < elements; i += 3)
2506 {
2507 struct signatured_type *sig_type;
2508 ULONGEST signature;
2509 void **slot;
2510 cu_offset type_offset_in_tu;
2511
2512 gdb_static_assert (sizeof (ULONGEST) >= 8);
2513 sect_offset sect_off
2514 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2515 type_offset_in_tu
2516 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2517 BFD_ENDIAN_LITTLE);
2518 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2519 bytes += 3 * 8;
2520
2521 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2522 struct signatured_type);
2523 sig_type->signature = signature;
2524 sig_type->type_offset_in_tu = type_offset_in_tu;
2525 sig_type->per_cu.is_debug_types = 1;
2526 sig_type->per_cu.section = section;
2527 sig_type->per_cu.sect_off = sect_off;
2528 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2529 sig_type->per_cu.v.quick
2530 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2531 struct dwarf2_per_cu_quick_data);
2532
2533 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2534 *slot = sig_type;
2535
2536 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2537 }
2538
2539 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
2540 }
2541
2542 /* Create the signatured type hash table from .debug_names. */
2543
2544 static void
2545 create_signatured_type_table_from_debug_names
2546 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2547 const mapped_debug_names &map,
2548 struct dwarf2_section_info *section,
2549 struct dwarf2_section_info *abbrev_section)
2550 {
2551 struct objfile *objfile = dwarf2_per_objfile->objfile;
2552
2553 section->read (objfile);
2554 abbrev_section->read (objfile);
2555
2556 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2557 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
2558
2559 htab_up sig_types_hash = allocate_signatured_type_table ();
2560
2561 for (uint32_t i = 0; i < map.tu_count; ++i)
2562 {
2563 struct signatured_type *sig_type;
2564 void **slot;
2565
2566 sect_offset sect_off
2567 = (sect_offset) (extract_unsigned_integer
2568 (map.tu_table_reordered + i * map.offset_size,
2569 map.offset_size,
2570 map.dwarf5_byte_order));
2571
2572 comp_unit_head cu_header;
2573 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2574 abbrev_section,
2575 section->buffer + to_underlying (sect_off),
2576 rcuh_kind::TYPE);
2577
2578 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2579 struct signatured_type);
2580 sig_type->signature = cu_header.signature;
2581 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2582 sig_type->per_cu.is_debug_types = 1;
2583 sig_type->per_cu.section = section;
2584 sig_type->per_cu.sect_off = sect_off;
2585 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2586 sig_type->per_cu.v.quick
2587 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2588 struct dwarf2_per_cu_quick_data);
2589
2590 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2591 *slot = sig_type;
2592
2593 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2594 }
2595
2596 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
2597 }
2598
2599 /* Read the address map data from the mapped index, and use it to
2600 populate the objfile's psymtabs_addrmap. */
2601
2602 static void
2603 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2604 struct mapped_index *index)
2605 {
2606 struct objfile *objfile = dwarf2_per_objfile->objfile;
2607 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2608 const gdb_byte *iter, *end;
2609 struct addrmap *mutable_map;
2610 CORE_ADDR baseaddr;
2611
2612 auto_obstack temp_obstack;
2613
2614 mutable_map = addrmap_create_mutable (&temp_obstack);
2615
2616 iter = index->address_table.data ();
2617 end = iter + index->address_table.size ();
2618
2619 baseaddr = objfile->text_section_offset ();
2620
2621 while (iter < end)
2622 {
2623 ULONGEST hi, lo, cu_index;
2624 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2625 iter += 8;
2626 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2627 iter += 8;
2628 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2629 iter += 4;
2630
2631 if (lo > hi)
2632 {
2633 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2634 hex_string (lo), hex_string (hi));
2635 continue;
2636 }
2637
2638 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
2639 {
2640 complaint (_(".gdb_index address table has invalid CU number %u"),
2641 (unsigned) cu_index);
2642 continue;
2643 }
2644
2645 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2646 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2647 addrmap_set_empty (mutable_map, lo, hi - 1,
2648 dwarf2_per_objfile->get_cu (cu_index));
2649 }
2650
2651 objfile->partial_symtabs->psymtabs_addrmap
2652 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2653 }
2654
2655 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2656 populate the objfile's psymtabs_addrmap. */
2657
2658 static void
2659 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
2660 struct dwarf2_section_info *section)
2661 {
2662 struct objfile *objfile = dwarf2_per_objfile->objfile;
2663 bfd *abfd = objfile->obfd;
2664 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2665 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2666
2667 auto_obstack temp_obstack;
2668 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2669
2670 std::unordered_map<sect_offset,
2671 dwarf2_per_cu_data *,
2672 gdb::hash_enum<sect_offset>>
2673 debug_info_offset_to_per_cu;
2674 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
2675 {
2676 const auto insertpair
2677 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2678 if (!insertpair.second)
2679 {
2680 warning (_("Section .debug_aranges in %s has duplicate "
2681 "debug_info_offset %s, ignoring .debug_aranges."),
2682 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2683 return;
2684 }
2685 }
2686
2687 section->read (objfile);
2688
2689 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2690
2691 const gdb_byte *addr = section->buffer;
2692
2693 while (addr < section->buffer + section->size)
2694 {
2695 const gdb_byte *const entry_addr = addr;
2696 unsigned int bytes_read;
2697
2698 const LONGEST entry_length = read_initial_length (abfd, addr,
2699 &bytes_read);
2700 addr += bytes_read;
2701
2702 const gdb_byte *const entry_end = addr + entry_length;
2703 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2704 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2705 if (addr + entry_length > section->buffer + section->size)
2706 {
2707 warning (_("Section .debug_aranges in %s entry at offset %s "
2708 "length %s exceeds section length %s, "
2709 "ignoring .debug_aranges."),
2710 objfile_name (objfile),
2711 plongest (entry_addr - section->buffer),
2712 plongest (bytes_read + entry_length),
2713 pulongest (section->size));
2714 return;
2715 }
2716
2717 /* The version number. */
2718 const uint16_t version = read_2_bytes (abfd, addr);
2719 addr += 2;
2720 if (version != 2)
2721 {
2722 warning (_("Section .debug_aranges in %s entry at offset %s "
2723 "has unsupported version %d, ignoring .debug_aranges."),
2724 objfile_name (objfile),
2725 plongest (entry_addr - section->buffer), version);
2726 return;
2727 }
2728
2729 const uint64_t debug_info_offset
2730 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2731 addr += offset_size;
2732 const auto per_cu_it
2733 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2734 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2735 {
2736 warning (_("Section .debug_aranges in %s entry at offset %s "
2737 "debug_info_offset %s does not exists, "
2738 "ignoring .debug_aranges."),
2739 objfile_name (objfile),
2740 plongest (entry_addr - section->buffer),
2741 pulongest (debug_info_offset));
2742 return;
2743 }
2744 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2745
2746 const uint8_t address_size = *addr++;
2747 if (address_size < 1 || address_size > 8)
2748 {
2749 warning (_("Section .debug_aranges in %s entry at offset %s "
2750 "address_size %u is invalid, ignoring .debug_aranges."),
2751 objfile_name (objfile),
2752 plongest (entry_addr - section->buffer), address_size);
2753 return;
2754 }
2755
2756 const uint8_t segment_selector_size = *addr++;
2757 if (segment_selector_size != 0)
2758 {
2759 warning (_("Section .debug_aranges in %s entry at offset %s "
2760 "segment_selector_size %u is not supported, "
2761 "ignoring .debug_aranges."),
2762 objfile_name (objfile),
2763 plongest (entry_addr - section->buffer),
2764 segment_selector_size);
2765 return;
2766 }
2767
2768 /* Must pad to an alignment boundary that is twice the address
2769 size. It is undocumented by the DWARF standard but GCC does
2770 use it. */
2771 for (size_t padding = ((-(addr - section->buffer))
2772 & (2 * address_size - 1));
2773 padding > 0; padding--)
2774 if (*addr++ != 0)
2775 {
2776 warning (_("Section .debug_aranges in %s entry at offset %s "
2777 "padding is not zero, ignoring .debug_aranges."),
2778 objfile_name (objfile),
2779 plongest (entry_addr - section->buffer));
2780 return;
2781 }
2782
2783 for (;;)
2784 {
2785 if (addr + 2 * address_size > entry_end)
2786 {
2787 warning (_("Section .debug_aranges in %s entry at offset %s "
2788 "address list is not properly terminated, "
2789 "ignoring .debug_aranges."),
2790 objfile_name (objfile),
2791 plongest (entry_addr - section->buffer));
2792 return;
2793 }
2794 ULONGEST start = extract_unsigned_integer (addr, address_size,
2795 dwarf5_byte_order);
2796 addr += address_size;
2797 ULONGEST length = extract_unsigned_integer (addr, address_size,
2798 dwarf5_byte_order);
2799 addr += address_size;
2800 if (start == 0 && length == 0)
2801 break;
2802 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
2803 {
2804 /* Symbol was eliminated due to a COMDAT group. */
2805 continue;
2806 }
2807 ULONGEST end = start + length;
2808 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2809 - baseaddr);
2810 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2811 - baseaddr);
2812 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2813 }
2814 }
2815
2816 objfile->partial_symtabs->psymtabs_addrmap
2817 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2818 }
2819
2820 /* Find a slot in the mapped index INDEX for the object named NAME.
2821 If NAME is found, set *VEC_OUT to point to the CU vector in the
2822 constant pool and return true. If NAME cannot be found, return
2823 false. */
2824
2825 static bool
2826 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2827 offset_type **vec_out)
2828 {
2829 offset_type hash;
2830 offset_type slot, step;
2831 int (*cmp) (const char *, const char *);
2832
2833 gdb::unique_xmalloc_ptr<char> without_params;
2834 if (current_language->la_language == language_cplus
2835 || current_language->la_language == language_fortran
2836 || current_language->la_language == language_d)
2837 {
2838 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2839 not contain any. */
2840
2841 if (strchr (name, '(') != NULL)
2842 {
2843 without_params = cp_remove_params (name);
2844
2845 if (without_params != NULL)
2846 name = without_params.get ();
2847 }
2848 }
2849
2850 /* Index version 4 did not support case insensitive searches. But the
2851 indices for case insensitive languages are built in lowercase, therefore
2852 simulate our NAME being searched is also lowercased. */
2853 hash = mapped_index_string_hash ((index->version == 4
2854 && case_sensitivity == case_sensitive_off
2855 ? 5 : index->version),
2856 name);
2857
2858 slot = hash & (index->symbol_table.size () - 1);
2859 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2860 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2861
2862 for (;;)
2863 {
2864 const char *str;
2865
2866 const auto &bucket = index->symbol_table[slot];
2867 if (bucket.name == 0 && bucket.vec == 0)
2868 return false;
2869
2870 str = index->constant_pool + MAYBE_SWAP (bucket.name);
2871 if (!cmp (name, str))
2872 {
2873 *vec_out = (offset_type *) (index->constant_pool
2874 + MAYBE_SWAP (bucket.vec));
2875 return true;
2876 }
2877
2878 slot = (slot + step) & (index->symbol_table.size () - 1);
2879 }
2880 }
2881
2882 /* A helper function that reads the .gdb_index from BUFFER and fills
2883 in MAP. FILENAME is the name of the file containing the data;
2884 it is used for error reporting. DEPRECATED_OK is true if it is
2885 ok to use deprecated sections.
2886
2887 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2888 out parameters that are filled in with information about the CU and
2889 TU lists in the section.
2890
2891 Returns true if all went well, false otherwise. */
2892
2893 static bool
2894 read_gdb_index_from_buffer (struct objfile *objfile,
2895 const char *filename,
2896 bool deprecated_ok,
2897 gdb::array_view<const gdb_byte> buffer,
2898 struct mapped_index *map,
2899 const gdb_byte **cu_list,
2900 offset_type *cu_list_elements,
2901 const gdb_byte **types_list,
2902 offset_type *types_list_elements)
2903 {
2904 const gdb_byte *addr = &buffer[0];
2905
2906 /* Version check. */
2907 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
2908 /* Versions earlier than 3 emitted every copy of a psymbol. This
2909 causes the index to behave very poorly for certain requests. Version 3
2910 contained incomplete addrmap. So, it seems better to just ignore such
2911 indices. */
2912 if (version < 4)
2913 {
2914 static int warning_printed = 0;
2915 if (!warning_printed)
2916 {
2917 warning (_("Skipping obsolete .gdb_index section in %s."),
2918 filename);
2919 warning_printed = 1;
2920 }
2921 return 0;
2922 }
2923 /* Index version 4 uses a different hash function than index version
2924 5 and later.
2925
2926 Versions earlier than 6 did not emit psymbols for inlined
2927 functions. Using these files will cause GDB not to be able to
2928 set breakpoints on inlined functions by name, so we ignore these
2929 indices unless the user has done
2930 "set use-deprecated-index-sections on". */
2931 if (version < 6 && !deprecated_ok)
2932 {
2933 static int warning_printed = 0;
2934 if (!warning_printed)
2935 {
2936 warning (_("\
2937 Skipping deprecated .gdb_index section in %s.\n\
2938 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2939 to use the section anyway."),
2940 filename);
2941 warning_printed = 1;
2942 }
2943 return 0;
2944 }
2945 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2946 of the TU (for symbols coming from TUs),
2947 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2948 Plus gold-generated indices can have duplicate entries for global symbols,
2949 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2950 These are just performance bugs, and we can't distinguish gdb-generated
2951 indices from gold-generated ones, so issue no warning here. */
2952
2953 /* Indexes with higher version than the one supported by GDB may be no
2954 longer backward compatible. */
2955 if (version > 8)
2956 return 0;
2957
2958 map->version = version;
2959
2960 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
2961
2962 int i = 0;
2963 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2964 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2965 / 8);
2966 ++i;
2967
2968 *types_list = addr + MAYBE_SWAP (metadata[i]);
2969 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2970 - MAYBE_SWAP (metadata[i]))
2971 / 8);
2972 ++i;
2973
2974 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
2975 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2976 map->address_table
2977 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
2978 ++i;
2979
2980 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
2981 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2982 map->symbol_table
2983 = gdb::array_view<mapped_index::symbol_table_slot>
2984 ((mapped_index::symbol_table_slot *) symbol_table,
2985 (mapped_index::symbol_table_slot *) symbol_table_end);
2986
2987 ++i;
2988 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
2989
2990 return 1;
2991 }
2992
2993 /* Callback types for dwarf2_read_gdb_index. */
2994
2995 typedef gdb::function_view
2996 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
2997 get_gdb_index_contents_ftype;
2998 typedef gdb::function_view
2999 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3000 get_gdb_index_contents_dwz_ftype;
3001
3002 /* Read .gdb_index. If everything went ok, initialize the "quick"
3003 elements of all the CUs and return 1. Otherwise, return 0. */
3004
3005 static int
3006 dwarf2_read_gdb_index
3007 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3008 get_gdb_index_contents_ftype get_gdb_index_contents,
3009 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3010 {
3011 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3012 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3013 struct dwz_file *dwz;
3014 struct objfile *objfile = dwarf2_per_objfile->objfile;
3015
3016 gdb::array_view<const gdb_byte> main_index_contents
3017 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3018
3019 if (main_index_contents.empty ())
3020 return 0;
3021
3022 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3023 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3024 use_deprecated_index_sections,
3025 main_index_contents, map.get (), &cu_list,
3026 &cu_list_elements, &types_list,
3027 &types_list_elements))
3028 return 0;
3029
3030 /* Don't use the index if it's empty. */
3031 if (map->symbol_table.empty ())
3032 return 0;
3033
3034 /* If there is a .dwz file, read it so we can get its CU list as
3035 well. */
3036 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3037 if (dwz != NULL)
3038 {
3039 struct mapped_index dwz_map;
3040 const gdb_byte *dwz_types_ignore;
3041 offset_type dwz_types_elements_ignore;
3042
3043 gdb::array_view<const gdb_byte> dwz_index_content
3044 = get_gdb_index_contents_dwz (objfile, dwz);
3045
3046 if (dwz_index_content.empty ())
3047 return 0;
3048
3049 if (!read_gdb_index_from_buffer (objfile,
3050 bfd_get_filename (dwz->dwz_bfd.get ()),
3051 1, dwz_index_content, &dwz_map,
3052 &dwz_list, &dwz_list_elements,
3053 &dwz_types_ignore,
3054 &dwz_types_elements_ignore))
3055 {
3056 warning (_("could not read '.gdb_index' section from %s; skipping"),
3057 bfd_get_filename (dwz->dwz_bfd.get ()));
3058 return 0;
3059 }
3060 }
3061
3062 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3063 dwz_list, dwz_list_elements);
3064
3065 if (types_list_elements)
3066 {
3067 /* We can only handle a single .debug_types when we have an
3068 index. */
3069 if (dwarf2_per_objfile->types.size () != 1)
3070 return 0;
3071
3072 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
3073
3074 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3075 types_list, types_list_elements);
3076 }
3077
3078 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3079
3080 dwarf2_per_objfile->index_table = std::move (map);
3081 dwarf2_per_objfile->using_index = 1;
3082 dwarf2_per_objfile->quick_file_names_table =
3083 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
3084
3085 return 1;
3086 }
3087
3088 /* die_reader_func for dw2_get_file_names. */
3089
3090 static void
3091 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3092 const gdb_byte *info_ptr,
3093 struct die_info *comp_unit_die)
3094 {
3095 struct dwarf2_cu *cu = reader->cu;
3096 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3097 struct dwarf2_per_objfile *dwarf2_per_objfile
3098 = cu->per_cu->dwarf2_per_objfile;
3099 struct objfile *objfile = dwarf2_per_objfile->objfile;
3100 struct dwarf2_per_cu_data *lh_cu;
3101 struct attribute *attr;
3102 void **slot;
3103 struct quick_file_names *qfn;
3104
3105 gdb_assert (! this_cu->is_debug_types);
3106
3107 /* Our callers never want to match partial units -- instead they
3108 will match the enclosing full CU. */
3109 if (comp_unit_die->tag == DW_TAG_partial_unit)
3110 {
3111 this_cu->v.quick->no_file_data = 1;
3112 return;
3113 }
3114
3115 lh_cu = this_cu;
3116 slot = NULL;
3117
3118 line_header_up lh;
3119 sect_offset line_offset {};
3120
3121 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3122 if (attr != nullptr)
3123 {
3124 struct quick_file_names find_entry;
3125
3126 line_offset = (sect_offset) DW_UNSND (attr);
3127
3128 /* We may have already read in this line header (TU line header sharing).
3129 If we have we're done. */
3130 find_entry.hash.dwo_unit = cu->dwo_unit;
3131 find_entry.hash.line_sect_off = line_offset;
3132 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table.get (),
3133 &find_entry, INSERT);
3134 if (*slot != NULL)
3135 {
3136 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3137 return;
3138 }
3139
3140 lh = dwarf_decode_line_header (line_offset, cu);
3141 }
3142 if (lh == NULL)
3143 {
3144 lh_cu->v.quick->no_file_data = 1;
3145 return;
3146 }
3147
3148 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3149 qfn->hash.dwo_unit = cu->dwo_unit;
3150 qfn->hash.line_sect_off = line_offset;
3151 gdb_assert (slot != NULL);
3152 *slot = qfn;
3153
3154 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3155
3156 int offset = 0;
3157 if (strcmp (fnd.name, "<unknown>") != 0)
3158 ++offset;
3159
3160 qfn->num_file_names = offset + lh->file_names_size ();
3161 qfn->file_names =
3162 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3163 if (offset != 0)
3164 qfn->file_names[0] = xstrdup (fnd.name);
3165 for (int i = 0; i < lh->file_names_size (); ++i)
3166 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3167 fnd.comp_dir).release ();
3168 qfn->real_names = NULL;
3169
3170 lh_cu->v.quick->file_names = qfn;
3171 }
3172
3173 /* A helper for the "quick" functions which attempts to read the line
3174 table for THIS_CU. */
3175
3176 static struct quick_file_names *
3177 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3178 {
3179 /* This should never be called for TUs. */
3180 gdb_assert (! this_cu->is_debug_types);
3181 /* Nor type unit groups. */
3182 gdb_assert (! this_cu->type_unit_group_p ());
3183
3184 if (this_cu->v.quick->file_names != NULL)
3185 return this_cu->v.quick->file_names;
3186 /* If we know there is no line data, no point in looking again. */
3187 if (this_cu->v.quick->no_file_data)
3188 return NULL;
3189
3190 cutu_reader reader (this_cu);
3191 if (!reader.dummy_p)
3192 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
3193
3194 if (this_cu->v.quick->no_file_data)
3195 return NULL;
3196 return this_cu->v.quick->file_names;
3197 }
3198
3199 /* A helper for the "quick" functions which computes and caches the
3200 real path for a given file name from the line table. */
3201
3202 static const char *
3203 dw2_get_real_path (struct objfile *objfile,
3204 struct quick_file_names *qfn, int index)
3205 {
3206 if (qfn->real_names == NULL)
3207 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3208 qfn->num_file_names, const char *);
3209
3210 if (qfn->real_names[index] == NULL)
3211 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3212
3213 return qfn->real_names[index];
3214 }
3215
3216 static struct symtab *
3217 dw2_find_last_source_symtab (struct objfile *objfile)
3218 {
3219 struct dwarf2_per_objfile *dwarf2_per_objfile
3220 = get_dwarf2_per_objfile (objfile);
3221 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
3222 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3223
3224 if (cust == NULL)
3225 return NULL;
3226
3227 return compunit_primary_filetab (cust);
3228 }
3229
3230 /* Traversal function for dw2_forget_cached_source_info. */
3231
3232 static int
3233 dw2_free_cached_file_names (void **slot, void *info)
3234 {
3235 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3236
3237 if (file_data->real_names)
3238 {
3239 int i;
3240
3241 for (i = 0; i < file_data->num_file_names; ++i)
3242 {
3243 xfree ((void*) file_data->real_names[i]);
3244 file_data->real_names[i] = NULL;
3245 }
3246 }
3247
3248 return 1;
3249 }
3250
3251 static void
3252 dw2_forget_cached_source_info (struct objfile *objfile)
3253 {
3254 struct dwarf2_per_objfile *dwarf2_per_objfile
3255 = get_dwarf2_per_objfile (objfile);
3256
3257 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table.get (),
3258 dw2_free_cached_file_names, NULL);
3259 }
3260
3261 /* Helper function for dw2_map_symtabs_matching_filename that expands
3262 the symtabs and calls the iterator. */
3263
3264 static int
3265 dw2_map_expand_apply (struct objfile *objfile,
3266 struct dwarf2_per_cu_data *per_cu,
3267 const char *name, const char *real_path,
3268 gdb::function_view<bool (symtab *)> callback)
3269 {
3270 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3271
3272 /* Don't visit already-expanded CUs. */
3273 if (per_cu->v.quick->compunit_symtab)
3274 return 0;
3275
3276 /* This may expand more than one symtab, and we want to iterate over
3277 all of them. */
3278 dw2_instantiate_symtab (per_cu, false);
3279
3280 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3281 last_made, callback);
3282 }
3283
3284 /* Implementation of the map_symtabs_matching_filename method. */
3285
3286 static bool
3287 dw2_map_symtabs_matching_filename
3288 (struct objfile *objfile, const char *name, const char *real_path,
3289 gdb::function_view<bool (symtab *)> callback)
3290 {
3291 const char *name_basename = lbasename (name);
3292 struct dwarf2_per_objfile *dwarf2_per_objfile
3293 = get_dwarf2_per_objfile (objfile);
3294
3295 /* The rule is CUs specify all the files, including those used by
3296 any TU, so there's no need to scan TUs here. */
3297
3298 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3299 {
3300 /* We only need to look at symtabs not already expanded. */
3301 if (per_cu->v.quick->compunit_symtab)
3302 continue;
3303
3304 quick_file_names *file_data = dw2_get_file_names (per_cu);
3305 if (file_data == NULL)
3306 continue;
3307
3308 for (int j = 0; j < file_data->num_file_names; ++j)
3309 {
3310 const char *this_name = file_data->file_names[j];
3311 const char *this_real_name;
3312
3313 if (compare_filenames_for_search (this_name, name))
3314 {
3315 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3316 callback))
3317 return true;
3318 continue;
3319 }
3320
3321 /* Before we invoke realpath, which can get expensive when many
3322 files are involved, do a quick comparison of the basenames. */
3323 if (! basenames_may_differ
3324 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3325 continue;
3326
3327 this_real_name = dw2_get_real_path (objfile, file_data, j);
3328 if (compare_filenames_for_search (this_real_name, name))
3329 {
3330 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3331 callback))
3332 return true;
3333 continue;
3334 }
3335
3336 if (real_path != NULL)
3337 {
3338 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3339 gdb_assert (IS_ABSOLUTE_PATH (name));
3340 if (this_real_name != NULL
3341 && FILENAME_CMP (real_path, this_real_name) == 0)
3342 {
3343 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3344 callback))
3345 return true;
3346 continue;
3347 }
3348 }
3349 }
3350 }
3351
3352 return false;
3353 }
3354
3355 /* Struct used to manage iterating over all CUs looking for a symbol. */
3356
3357 struct dw2_symtab_iterator
3358 {
3359 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3360 struct dwarf2_per_objfile *dwarf2_per_objfile;
3361 /* If set, only look for symbols that match that block. Valid values are
3362 GLOBAL_BLOCK and STATIC_BLOCK. */
3363 gdb::optional<block_enum> block_index;
3364 /* The kind of symbol we're looking for. */
3365 domain_enum domain;
3366 /* The list of CUs from the index entry of the symbol,
3367 or NULL if not found. */
3368 offset_type *vec;
3369 /* The next element in VEC to look at. */
3370 int next;
3371 /* The number of elements in VEC, or zero if there is no match. */
3372 int length;
3373 /* Have we seen a global version of the symbol?
3374 If so we can ignore all further global instances.
3375 This is to work around gold/15646, inefficient gold-generated
3376 indices. */
3377 int global_seen;
3378 };
3379
3380 /* Initialize the index symtab iterator ITER. */
3381
3382 static void
3383 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3384 struct dwarf2_per_objfile *dwarf2_per_objfile,
3385 gdb::optional<block_enum> block_index,
3386 domain_enum domain,
3387 const char *name)
3388 {
3389 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3390 iter->block_index = block_index;
3391 iter->domain = domain;
3392 iter->next = 0;
3393 iter->global_seen = 0;
3394
3395 mapped_index *index = dwarf2_per_objfile->index_table.get ();
3396
3397 /* index is NULL if OBJF_READNOW. */
3398 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3399 iter->length = MAYBE_SWAP (*iter->vec);
3400 else
3401 {
3402 iter->vec = NULL;
3403 iter->length = 0;
3404 }
3405 }
3406
3407 /* Return the next matching CU or NULL if there are no more. */
3408
3409 static struct dwarf2_per_cu_data *
3410 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3411 {
3412 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3413
3414 for ( ; iter->next < iter->length; ++iter->next)
3415 {
3416 offset_type cu_index_and_attrs =
3417 MAYBE_SWAP (iter->vec[iter->next + 1]);
3418 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3419 gdb_index_symbol_kind symbol_kind =
3420 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3421 /* Only check the symbol attributes if they're present.
3422 Indices prior to version 7 don't record them,
3423 and indices >= 7 may elide them for certain symbols
3424 (gold does this). */
3425 int attrs_valid =
3426 (dwarf2_per_objfile->index_table->version >= 7
3427 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3428
3429 /* Don't crash on bad data. */
3430 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
3431 + dwarf2_per_objfile->all_type_units.size ()))
3432 {
3433 complaint (_(".gdb_index entry has bad CU index"
3434 " [in module %s]"),
3435 objfile_name (dwarf2_per_objfile->objfile));
3436 continue;
3437 }
3438
3439 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3440
3441 /* Skip if already read in. */
3442 if (per_cu->v.quick->compunit_symtab)
3443 continue;
3444
3445 /* Check static vs global. */
3446 if (attrs_valid)
3447 {
3448 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3449
3450 if (iter->block_index.has_value ())
3451 {
3452 bool want_static = *iter->block_index == STATIC_BLOCK;
3453
3454 if (is_static != want_static)
3455 continue;
3456 }
3457
3458 /* Work around gold/15646. */
3459 if (!is_static && iter->global_seen)
3460 continue;
3461 if (!is_static)
3462 iter->global_seen = 1;
3463 }
3464
3465 /* Only check the symbol's kind if it has one. */
3466 if (attrs_valid)
3467 {
3468 switch (iter->domain)
3469 {
3470 case VAR_DOMAIN:
3471 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3472 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3473 /* Some types are also in VAR_DOMAIN. */
3474 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3475 continue;
3476 break;
3477 case STRUCT_DOMAIN:
3478 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3479 continue;
3480 break;
3481 case LABEL_DOMAIN:
3482 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3483 continue;
3484 break;
3485 case MODULE_DOMAIN:
3486 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3487 continue;
3488 break;
3489 default:
3490 break;
3491 }
3492 }
3493
3494 ++iter->next;
3495 return per_cu;
3496 }
3497
3498 return NULL;
3499 }
3500
3501 static struct compunit_symtab *
3502 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3503 const char *name, domain_enum domain)
3504 {
3505 struct compunit_symtab *stab_best = NULL;
3506 struct dwarf2_per_objfile *dwarf2_per_objfile
3507 = get_dwarf2_per_objfile (objfile);
3508
3509 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3510
3511 struct dw2_symtab_iterator iter;
3512 struct dwarf2_per_cu_data *per_cu;
3513
3514 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
3515
3516 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3517 {
3518 struct symbol *sym, *with_opaque = NULL;
3519 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
3520 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3521 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3522
3523 sym = block_find_symbol (block, name, domain,
3524 block_find_non_opaque_type_preferred,
3525 &with_opaque);
3526
3527 /* Some caution must be observed with overloaded functions
3528 and methods, since the index will not contain any overload
3529 information (but NAME might contain it). */
3530
3531 if (sym != NULL
3532 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3533 return stab;
3534 if (with_opaque != NULL
3535 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3536 stab_best = stab;
3537
3538 /* Keep looking through other CUs. */
3539 }
3540
3541 return stab_best;
3542 }
3543
3544 static void
3545 dw2_print_stats (struct objfile *objfile)
3546 {
3547 struct dwarf2_per_objfile *dwarf2_per_objfile
3548 = get_dwarf2_per_objfile (objfile);
3549 int total = (dwarf2_per_objfile->all_comp_units.size ()
3550 + dwarf2_per_objfile->all_type_units.size ());
3551 int count = 0;
3552
3553 for (int i = 0; i < total; ++i)
3554 {
3555 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3556
3557 if (!per_cu->v.quick->compunit_symtab)
3558 ++count;
3559 }
3560 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3561 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3562 }
3563
3564 /* This dumps minimal information about the index.
3565 It is called via "mt print objfiles".
3566 One use is to verify .gdb_index has been loaded by the
3567 gdb.dwarf2/gdb-index.exp testcase. */
3568
3569 static void
3570 dw2_dump (struct objfile *objfile)
3571 {
3572 struct dwarf2_per_objfile *dwarf2_per_objfile
3573 = get_dwarf2_per_objfile (objfile);
3574
3575 gdb_assert (dwarf2_per_objfile->using_index);
3576 printf_filtered (".gdb_index:");
3577 if (dwarf2_per_objfile->index_table != NULL)
3578 {
3579 printf_filtered (" version %d\n",
3580 dwarf2_per_objfile->index_table->version);
3581 }
3582 else
3583 printf_filtered (" faked for \"readnow\"\n");
3584 printf_filtered ("\n");
3585 }
3586
3587 static void
3588 dw2_expand_symtabs_for_function (struct objfile *objfile,
3589 const char *func_name)
3590 {
3591 struct dwarf2_per_objfile *dwarf2_per_objfile
3592 = get_dwarf2_per_objfile (objfile);
3593
3594 struct dw2_symtab_iterator iter;
3595 struct dwarf2_per_cu_data *per_cu;
3596
3597 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
3598
3599 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3600 dw2_instantiate_symtab (per_cu, false);
3601
3602 }
3603
3604 static void
3605 dw2_expand_all_symtabs (struct objfile *objfile)
3606 {
3607 struct dwarf2_per_objfile *dwarf2_per_objfile
3608 = get_dwarf2_per_objfile (objfile);
3609 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
3610 + dwarf2_per_objfile->all_type_units.size ());
3611
3612 for (int i = 0; i < total_units; ++i)
3613 {
3614 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3615
3616 /* We don't want to directly expand a partial CU, because if we
3617 read it with the wrong language, then assertion failures can
3618 be triggered later on. See PR symtab/23010. So, tell
3619 dw2_instantiate_symtab to skip partial CUs -- any important
3620 partial CU will be read via DW_TAG_imported_unit anyway. */
3621 dw2_instantiate_symtab (per_cu, true);
3622 }
3623 }
3624
3625 static void
3626 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3627 const char *fullname)
3628 {
3629 struct dwarf2_per_objfile *dwarf2_per_objfile
3630 = get_dwarf2_per_objfile (objfile);
3631
3632 /* We don't need to consider type units here.
3633 This is only called for examining code, e.g. expand_line_sal.
3634 There can be an order of magnitude (or more) more type units
3635 than comp units, and we avoid them if we can. */
3636
3637 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3638 {
3639 /* We only need to look at symtabs not already expanded. */
3640 if (per_cu->v.quick->compunit_symtab)
3641 continue;
3642
3643 quick_file_names *file_data = dw2_get_file_names (per_cu);
3644 if (file_data == NULL)
3645 continue;
3646
3647 for (int j = 0; j < file_data->num_file_names; ++j)
3648 {
3649 const char *this_fullname = file_data->file_names[j];
3650
3651 if (filename_cmp (this_fullname, fullname) == 0)
3652 {
3653 dw2_instantiate_symtab (per_cu, false);
3654 break;
3655 }
3656 }
3657 }
3658 }
3659
3660 static void
3661 dw2_map_matching_symbols
3662 (struct objfile *objfile,
3663 const lookup_name_info &name, domain_enum domain,
3664 int global,
3665 gdb::function_view<symbol_found_callback_ftype> callback,
3666 symbol_compare_ftype *ordered_compare)
3667 {
3668 /* Currently unimplemented; used for Ada. The function can be called if the
3669 current language is Ada for a non-Ada objfile using GNU index. As Ada
3670 does not look for non-Ada symbols this function should just return. */
3671 }
3672
3673 /* Starting from a search name, return the string that finds the upper
3674 bound of all strings that start with SEARCH_NAME in a sorted name
3675 list. Returns the empty string to indicate that the upper bound is
3676 the end of the list. */
3677
3678 static std::string
3679 make_sort_after_prefix_name (const char *search_name)
3680 {
3681 /* When looking to complete "func", we find the upper bound of all
3682 symbols that start with "func" by looking for where we'd insert
3683 the closest string that would follow "func" in lexicographical
3684 order. Usually, that's "func"-with-last-character-incremented,
3685 i.e. "fund". Mind non-ASCII characters, though. Usually those
3686 will be UTF-8 multi-byte sequences, but we can't be certain.
3687 Especially mind the 0xff character, which is a valid character in
3688 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3689 rule out compilers allowing it in identifiers. Note that
3690 conveniently, strcmp/strcasecmp are specified to compare
3691 characters interpreted as unsigned char. So what we do is treat
3692 the whole string as a base 256 number composed of a sequence of
3693 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3694 to 0, and carries 1 to the following more-significant position.
3695 If the very first character in SEARCH_NAME ends up incremented
3696 and carries/overflows, then the upper bound is the end of the
3697 list. The string after the empty string is also the empty
3698 string.
3699
3700 Some examples of this operation:
3701
3702 SEARCH_NAME => "+1" RESULT
3703
3704 "abc" => "abd"
3705 "ab\xff" => "ac"
3706 "\xff" "a" "\xff" => "\xff" "b"
3707 "\xff" => ""
3708 "\xff\xff" => ""
3709 "" => ""
3710
3711 Then, with these symbols for example:
3712
3713 func
3714 func1
3715 fund
3716
3717 completing "func" looks for symbols between "func" and
3718 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3719 which finds "func" and "func1", but not "fund".
3720
3721 And with:
3722
3723 funcÿ (Latin1 'ÿ' [0xff])
3724 funcÿ1
3725 fund
3726
3727 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3728 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3729
3730 And with:
3731
3732 ÿÿ (Latin1 'ÿ' [0xff])
3733 ÿÿ1
3734
3735 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3736 the end of the list.
3737 */
3738 std::string after = search_name;
3739 while (!after.empty () && (unsigned char) after.back () == 0xff)
3740 after.pop_back ();
3741 if (!after.empty ())
3742 after.back () = (unsigned char) after.back () + 1;
3743 return after;
3744 }
3745
3746 /* See declaration. */
3747
3748 std::pair<std::vector<name_component>::const_iterator,
3749 std::vector<name_component>::const_iterator>
3750 mapped_index_base::find_name_components_bounds
3751 (const lookup_name_info &lookup_name_without_params, language lang) const
3752 {
3753 auto *name_cmp
3754 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3755
3756 const char *lang_name
3757 = lookup_name_without_params.language_lookup_name (lang).c_str ();
3758
3759 /* Comparison function object for lower_bound that matches against a
3760 given symbol name. */
3761 auto lookup_compare_lower = [&] (const name_component &elem,
3762 const char *name)
3763 {
3764 const char *elem_qualified = this->symbol_name_at (elem.idx);
3765 const char *elem_name = elem_qualified + elem.name_offset;
3766 return name_cmp (elem_name, name) < 0;
3767 };
3768
3769 /* Comparison function object for upper_bound that matches against a
3770 given symbol name. */
3771 auto lookup_compare_upper = [&] (const char *name,
3772 const name_component &elem)
3773 {
3774 const char *elem_qualified = this->symbol_name_at (elem.idx);
3775 const char *elem_name = elem_qualified + elem.name_offset;
3776 return name_cmp (name, elem_name) < 0;
3777 };
3778
3779 auto begin = this->name_components.begin ();
3780 auto end = this->name_components.end ();
3781
3782 /* Find the lower bound. */
3783 auto lower = [&] ()
3784 {
3785 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3786 return begin;
3787 else
3788 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3789 } ();
3790
3791 /* Find the upper bound. */
3792 auto upper = [&] ()
3793 {
3794 if (lookup_name_without_params.completion_mode ())
3795 {
3796 /* In completion mode, we want UPPER to point past all
3797 symbols names that have the same prefix. I.e., with
3798 these symbols, and completing "func":
3799
3800 function << lower bound
3801 function1
3802 other_function << upper bound
3803
3804 We find the upper bound by looking for the insertion
3805 point of "func"-with-last-character-incremented,
3806 i.e. "fund". */
3807 std::string after = make_sort_after_prefix_name (lang_name);
3808 if (after.empty ())
3809 return end;
3810 return std::lower_bound (lower, end, after.c_str (),
3811 lookup_compare_lower);
3812 }
3813 else
3814 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3815 } ();
3816
3817 return {lower, upper};
3818 }
3819
3820 /* See declaration. */
3821
3822 void
3823 mapped_index_base::build_name_components ()
3824 {
3825 if (!this->name_components.empty ())
3826 return;
3827
3828 this->name_components_casing = case_sensitivity;
3829 auto *name_cmp
3830 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3831
3832 /* The code below only knows how to break apart components of C++
3833 symbol names (and other languages that use '::' as
3834 namespace/module separator) and Ada symbol names. */
3835 auto count = this->symbol_name_count ();
3836 for (offset_type idx = 0; idx < count; idx++)
3837 {
3838 if (this->symbol_name_slot_invalid (idx))
3839 continue;
3840
3841 const char *name = this->symbol_name_at (idx);
3842
3843 /* Add each name component to the name component table. */
3844 unsigned int previous_len = 0;
3845
3846 if (strstr (name, "::") != nullptr)
3847 {
3848 for (unsigned int current_len = cp_find_first_component (name);
3849 name[current_len] != '\0';
3850 current_len += cp_find_first_component (name + current_len))
3851 {
3852 gdb_assert (name[current_len] == ':');
3853 this->name_components.push_back ({previous_len, idx});
3854 /* Skip the '::'. */
3855 current_len += 2;
3856 previous_len = current_len;
3857 }
3858 }
3859 else
3860 {
3861 /* Handle the Ada encoded (aka mangled) form here. */
3862 for (const char *iter = strstr (name, "__");
3863 iter != nullptr;
3864 iter = strstr (iter, "__"))
3865 {
3866 this->name_components.push_back ({previous_len, idx});
3867 iter += 2;
3868 previous_len = iter - name;
3869 }
3870 }
3871
3872 this->name_components.push_back ({previous_len, idx});
3873 }
3874
3875 /* Sort name_components elements by name. */
3876 auto name_comp_compare = [&] (const name_component &left,
3877 const name_component &right)
3878 {
3879 const char *left_qualified = this->symbol_name_at (left.idx);
3880 const char *right_qualified = this->symbol_name_at (right.idx);
3881
3882 const char *left_name = left_qualified + left.name_offset;
3883 const char *right_name = right_qualified + right.name_offset;
3884
3885 return name_cmp (left_name, right_name) < 0;
3886 };
3887
3888 std::sort (this->name_components.begin (),
3889 this->name_components.end (),
3890 name_comp_compare);
3891 }
3892
3893 /* Helper for dw2_expand_symtabs_matching that works with a
3894 mapped_index_base instead of the containing objfile. This is split
3895 to a separate function in order to be able to unit test the
3896 name_components matching using a mock mapped_index_base. For each
3897 symbol name that matches, calls MATCH_CALLBACK, passing it the
3898 symbol's index in the mapped_index_base symbol table. */
3899
3900 static void
3901 dw2_expand_symtabs_matching_symbol
3902 (mapped_index_base &index,
3903 const lookup_name_info &lookup_name_in,
3904 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3905 enum search_domain kind,
3906 gdb::function_view<bool (offset_type)> match_callback)
3907 {
3908 lookup_name_info lookup_name_without_params
3909 = lookup_name_in.make_ignore_params ();
3910
3911 /* Build the symbol name component sorted vector, if we haven't
3912 yet. */
3913 index.build_name_components ();
3914
3915 /* The same symbol may appear more than once in the range though.
3916 E.g., if we're looking for symbols that complete "w", and we have
3917 a symbol named "w1::w2", we'll find the two name components for
3918 that same symbol in the range. To be sure we only call the
3919 callback once per symbol, we first collect the symbol name
3920 indexes that matched in a temporary vector and ignore
3921 duplicates. */
3922 std::vector<offset_type> matches;
3923
3924 struct name_and_matcher
3925 {
3926 symbol_name_matcher_ftype *matcher;
3927 const std::string &name;
3928
3929 bool operator== (const name_and_matcher &other) const
3930 {
3931 return matcher == other.matcher && name == other.name;
3932 }
3933 };
3934
3935 /* A vector holding all the different symbol name matchers, for all
3936 languages. */
3937 std::vector<name_and_matcher> matchers;
3938
3939 for (int i = 0; i < nr_languages; i++)
3940 {
3941 enum language lang_e = (enum language) i;
3942
3943 const language_defn *lang = language_def (lang_e);
3944 symbol_name_matcher_ftype *name_matcher
3945 = get_symbol_name_matcher (lang, lookup_name_without_params);
3946
3947 name_and_matcher key {
3948 name_matcher,
3949 lookup_name_without_params.language_lookup_name (lang_e)
3950 };
3951
3952 /* Don't insert the same comparison routine more than once.
3953 Note that we do this linear walk. This is not a problem in
3954 practice because the number of supported languages is
3955 low. */
3956 if (std::find (matchers.begin (), matchers.end (), key)
3957 != matchers.end ())
3958 continue;
3959 matchers.push_back (std::move (key));
3960
3961 auto bounds
3962 = index.find_name_components_bounds (lookup_name_without_params,
3963 lang_e);
3964
3965 /* Now for each symbol name in range, check to see if we have a name
3966 match, and if so, call the MATCH_CALLBACK callback. */
3967
3968 for (; bounds.first != bounds.second; ++bounds.first)
3969 {
3970 const char *qualified = index.symbol_name_at (bounds.first->idx);
3971
3972 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3973 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3974 continue;
3975
3976 matches.push_back (bounds.first->idx);
3977 }
3978 }
3979
3980 std::sort (matches.begin (), matches.end ());
3981
3982 /* Finally call the callback, once per match. */
3983 ULONGEST prev = -1;
3984 for (offset_type idx : matches)
3985 {
3986 if (prev != idx)
3987 {
3988 if (!match_callback (idx))
3989 break;
3990 prev = idx;
3991 }
3992 }
3993
3994 /* Above we use a type wider than idx's for 'prev', since 0 and
3995 (offset_type)-1 are both possible values. */
3996 static_assert (sizeof (prev) > sizeof (offset_type), "");
3997 }
3998
3999 #if GDB_SELF_TEST
4000
4001 namespace selftests { namespace dw2_expand_symtabs_matching {
4002
4003 /* A mock .gdb_index/.debug_names-like name index table, enough to
4004 exercise dw2_expand_symtabs_matching_symbol, which works with the
4005 mapped_index_base interface. Builds an index from the symbol list
4006 passed as parameter to the constructor. */
4007 class mock_mapped_index : public mapped_index_base
4008 {
4009 public:
4010 mock_mapped_index (gdb::array_view<const char *> symbols)
4011 : m_symbol_table (symbols)
4012 {}
4013
4014 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4015
4016 /* Return the number of names in the symbol table. */
4017 size_t symbol_name_count () const override
4018 {
4019 return m_symbol_table.size ();
4020 }
4021
4022 /* Get the name of the symbol at IDX in the symbol table. */
4023 const char *symbol_name_at (offset_type idx) const override
4024 {
4025 return m_symbol_table[idx];
4026 }
4027
4028 private:
4029 gdb::array_view<const char *> m_symbol_table;
4030 };
4031
4032 /* Convenience function that converts a NULL pointer to a "<null>"
4033 string, to pass to print routines. */
4034
4035 static const char *
4036 string_or_null (const char *str)
4037 {
4038 return str != NULL ? str : "<null>";
4039 }
4040
4041 /* Check if a lookup_name_info built from
4042 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4043 index. EXPECTED_LIST is the list of expected matches, in expected
4044 matching order. If no match expected, then an empty list is
4045 specified. Returns true on success. On failure prints a warning
4046 indicating the file:line that failed, and returns false. */
4047
4048 static bool
4049 check_match (const char *file, int line,
4050 mock_mapped_index &mock_index,
4051 const char *name, symbol_name_match_type match_type,
4052 bool completion_mode,
4053 std::initializer_list<const char *> expected_list)
4054 {
4055 lookup_name_info lookup_name (name, match_type, completion_mode);
4056
4057 bool matched = true;
4058
4059 auto mismatch = [&] (const char *expected_str,
4060 const char *got)
4061 {
4062 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4063 "expected=\"%s\", got=\"%s\"\n"),
4064 file, line,
4065 (match_type == symbol_name_match_type::FULL
4066 ? "FULL" : "WILD"),
4067 name, string_or_null (expected_str), string_or_null (got));
4068 matched = false;
4069 };
4070
4071 auto expected_it = expected_list.begin ();
4072 auto expected_end = expected_list.end ();
4073
4074 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4075 NULL, ALL_DOMAIN,
4076 [&] (offset_type idx)
4077 {
4078 const char *matched_name = mock_index.symbol_name_at (idx);
4079 const char *expected_str
4080 = expected_it == expected_end ? NULL : *expected_it++;
4081
4082 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4083 mismatch (expected_str, matched_name);
4084 return true;
4085 });
4086
4087 const char *expected_str
4088 = expected_it == expected_end ? NULL : *expected_it++;
4089 if (expected_str != NULL)
4090 mismatch (expected_str, NULL);
4091
4092 return matched;
4093 }
4094
4095 /* The symbols added to the mock mapped_index for testing (in
4096 canonical form). */
4097 static const char *test_symbols[] = {
4098 "function",
4099 "std::bar",
4100 "std::zfunction",
4101 "std::zfunction2",
4102 "w1::w2",
4103 "ns::foo<char*>",
4104 "ns::foo<int>",
4105 "ns::foo<long>",
4106 "ns2::tmpl<int>::foo2",
4107 "(anonymous namespace)::A::B::C",
4108
4109 /* These are used to check that the increment-last-char in the
4110 matching algorithm for completion doesn't match "t1_fund" when
4111 completing "t1_func". */
4112 "t1_func",
4113 "t1_func1",
4114 "t1_fund",
4115 "t1_fund1",
4116
4117 /* A UTF-8 name with multi-byte sequences to make sure that
4118 cp-name-parser understands this as a single identifier ("função"
4119 is "function" in PT). */
4120 u8"u8função",
4121
4122 /* \377 (0xff) is Latin1 'ÿ'. */
4123 "yfunc\377",
4124
4125 /* \377 (0xff) is Latin1 'ÿ'. */
4126 "\377",
4127 "\377\377123",
4128
4129 /* A name with all sorts of complications. Starts with "z" to make
4130 it easier for the completion tests below. */
4131 #define Z_SYM_NAME \
4132 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4133 "::tuple<(anonymous namespace)::ui*, " \
4134 "std::default_delete<(anonymous namespace)::ui>, void>"
4135
4136 Z_SYM_NAME
4137 };
4138
4139 /* Returns true if the mapped_index_base::find_name_component_bounds
4140 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4141 in completion mode. */
4142
4143 static bool
4144 check_find_bounds_finds (mapped_index_base &index,
4145 const char *search_name,
4146 gdb::array_view<const char *> expected_syms)
4147 {
4148 lookup_name_info lookup_name (search_name,
4149 symbol_name_match_type::FULL, true);
4150
4151 auto bounds = index.find_name_components_bounds (lookup_name,
4152 language_cplus);
4153
4154 size_t distance = std::distance (bounds.first, bounds.second);
4155 if (distance != expected_syms.size ())
4156 return false;
4157
4158 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4159 {
4160 auto nc_elem = bounds.first + exp_elem;
4161 const char *qualified = index.symbol_name_at (nc_elem->idx);
4162 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4163 return false;
4164 }
4165
4166 return true;
4167 }
4168
4169 /* Test the lower-level mapped_index::find_name_component_bounds
4170 method. */
4171
4172 static void
4173 test_mapped_index_find_name_component_bounds ()
4174 {
4175 mock_mapped_index mock_index (test_symbols);
4176
4177 mock_index.build_name_components ();
4178
4179 /* Test the lower-level mapped_index::find_name_component_bounds
4180 method in completion mode. */
4181 {
4182 static const char *expected_syms[] = {
4183 "t1_func",
4184 "t1_func1",
4185 };
4186
4187 SELF_CHECK (check_find_bounds_finds (mock_index,
4188 "t1_func", expected_syms));
4189 }
4190
4191 /* Check that the increment-last-char in the name matching algorithm
4192 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4193 {
4194 static const char *expected_syms1[] = {
4195 "\377",
4196 "\377\377123",
4197 };
4198 SELF_CHECK (check_find_bounds_finds (mock_index,
4199 "\377", expected_syms1));
4200
4201 static const char *expected_syms2[] = {
4202 "\377\377123",
4203 };
4204 SELF_CHECK (check_find_bounds_finds (mock_index,
4205 "\377\377", expected_syms2));
4206 }
4207 }
4208
4209 /* Test dw2_expand_symtabs_matching_symbol. */
4210
4211 static void
4212 test_dw2_expand_symtabs_matching_symbol ()
4213 {
4214 mock_mapped_index mock_index (test_symbols);
4215
4216 /* We let all tests run until the end even if some fails, for debug
4217 convenience. */
4218 bool any_mismatch = false;
4219
4220 /* Create the expected symbols list (an initializer_list). Needed
4221 because lists have commas, and we need to pass them to CHECK,
4222 which is a macro. */
4223 #define EXPECT(...) { __VA_ARGS__ }
4224
4225 /* Wrapper for check_match that passes down the current
4226 __FILE__/__LINE__. */
4227 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4228 any_mismatch |= !check_match (__FILE__, __LINE__, \
4229 mock_index, \
4230 NAME, MATCH_TYPE, COMPLETION_MODE, \
4231 EXPECTED_LIST)
4232
4233 /* Identity checks. */
4234 for (const char *sym : test_symbols)
4235 {
4236 /* Should be able to match all existing symbols. */
4237 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4238 EXPECT (sym));
4239
4240 /* Should be able to match all existing symbols with
4241 parameters. */
4242 std::string with_params = std::string (sym) + "(int)";
4243 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4244 EXPECT (sym));
4245
4246 /* Should be able to match all existing symbols with
4247 parameters and qualifiers. */
4248 with_params = std::string (sym) + " ( int ) const";
4249 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4250 EXPECT (sym));
4251
4252 /* This should really find sym, but cp-name-parser.y doesn't
4253 know about lvalue/rvalue qualifiers yet. */
4254 with_params = std::string (sym) + " ( int ) &&";
4255 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4256 {});
4257 }
4258
4259 /* Check that the name matching algorithm for completion doesn't get
4260 confused with Latin1 'ÿ' / 0xff. */
4261 {
4262 static const char str[] = "\377";
4263 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4264 EXPECT ("\377", "\377\377123"));
4265 }
4266
4267 /* Check that the increment-last-char in the matching algorithm for
4268 completion doesn't match "t1_fund" when completing "t1_func". */
4269 {
4270 static const char str[] = "t1_func";
4271 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4272 EXPECT ("t1_func", "t1_func1"));
4273 }
4274
4275 /* Check that completion mode works at each prefix of the expected
4276 symbol name. */
4277 {
4278 static const char str[] = "function(int)";
4279 size_t len = strlen (str);
4280 std::string lookup;
4281
4282 for (size_t i = 1; i < len; i++)
4283 {
4284 lookup.assign (str, i);
4285 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4286 EXPECT ("function"));
4287 }
4288 }
4289
4290 /* While "w" is a prefix of both components, the match function
4291 should still only be called once. */
4292 {
4293 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4294 EXPECT ("w1::w2"));
4295 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4296 EXPECT ("w1::w2"));
4297 }
4298
4299 /* Same, with a "complicated" symbol. */
4300 {
4301 static const char str[] = Z_SYM_NAME;
4302 size_t len = strlen (str);
4303 std::string lookup;
4304
4305 for (size_t i = 1; i < len; i++)
4306 {
4307 lookup.assign (str, i);
4308 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4309 EXPECT (Z_SYM_NAME));
4310 }
4311 }
4312
4313 /* In FULL mode, an incomplete symbol doesn't match. */
4314 {
4315 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4316 {});
4317 }
4318
4319 /* A complete symbol with parameters matches any overload, since the
4320 index has no overload info. */
4321 {
4322 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4323 EXPECT ("std::zfunction", "std::zfunction2"));
4324 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4325 EXPECT ("std::zfunction", "std::zfunction2"));
4326 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4327 EXPECT ("std::zfunction", "std::zfunction2"));
4328 }
4329
4330 /* Check that whitespace is ignored appropriately. A symbol with a
4331 template argument list. */
4332 {
4333 static const char expected[] = "ns::foo<int>";
4334 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4335 EXPECT (expected));
4336 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4337 EXPECT (expected));
4338 }
4339
4340 /* Check that whitespace is ignored appropriately. A symbol with a
4341 template argument list that includes a pointer. */
4342 {
4343 static const char expected[] = "ns::foo<char*>";
4344 /* Try both completion and non-completion modes. */
4345 static const bool completion_mode[2] = {false, true};
4346 for (size_t i = 0; i < 2; i++)
4347 {
4348 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4349 completion_mode[i], EXPECT (expected));
4350 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4351 completion_mode[i], EXPECT (expected));
4352
4353 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4354 completion_mode[i], EXPECT (expected));
4355 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4356 completion_mode[i], EXPECT (expected));
4357 }
4358 }
4359
4360 {
4361 /* Check method qualifiers are ignored. */
4362 static const char expected[] = "ns::foo<char*>";
4363 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4364 symbol_name_match_type::FULL, true, EXPECT (expected));
4365 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4366 symbol_name_match_type::FULL, true, EXPECT (expected));
4367 CHECK_MATCH ("foo < char * > ( int ) const",
4368 symbol_name_match_type::WILD, true, EXPECT (expected));
4369 CHECK_MATCH ("foo < char * > ( int ) &&",
4370 symbol_name_match_type::WILD, true, EXPECT (expected));
4371 }
4372
4373 /* Test lookup names that don't match anything. */
4374 {
4375 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4376 {});
4377
4378 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4379 {});
4380 }
4381
4382 /* Some wild matching tests, exercising "(anonymous namespace)",
4383 which should not be confused with a parameter list. */
4384 {
4385 static const char *syms[] = {
4386 "A::B::C",
4387 "B::C",
4388 "C",
4389 "A :: B :: C ( int )",
4390 "B :: C ( int )",
4391 "C ( int )",
4392 };
4393
4394 for (const char *s : syms)
4395 {
4396 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4397 EXPECT ("(anonymous namespace)::A::B::C"));
4398 }
4399 }
4400
4401 {
4402 static const char expected[] = "ns2::tmpl<int>::foo2";
4403 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4404 EXPECT (expected));
4405 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4406 EXPECT (expected));
4407 }
4408
4409 SELF_CHECK (!any_mismatch);
4410
4411 #undef EXPECT
4412 #undef CHECK_MATCH
4413 }
4414
4415 static void
4416 run_test ()
4417 {
4418 test_mapped_index_find_name_component_bounds ();
4419 test_dw2_expand_symtabs_matching_symbol ();
4420 }
4421
4422 }} // namespace selftests::dw2_expand_symtabs_matching
4423
4424 #endif /* GDB_SELF_TEST */
4425
4426 /* If FILE_MATCHER is NULL or if PER_CU has
4427 dwarf2_per_cu_quick_data::MARK set (see
4428 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4429 EXPANSION_NOTIFY on it. */
4430
4431 static void
4432 dw2_expand_symtabs_matching_one
4433 (struct dwarf2_per_cu_data *per_cu,
4434 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4435 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4436 {
4437 if (file_matcher == NULL || per_cu->v.quick->mark)
4438 {
4439 bool symtab_was_null
4440 = (per_cu->v.quick->compunit_symtab == NULL);
4441
4442 dw2_instantiate_symtab (per_cu, false);
4443
4444 if (expansion_notify != NULL
4445 && symtab_was_null
4446 && per_cu->v.quick->compunit_symtab != NULL)
4447 expansion_notify (per_cu->v.quick->compunit_symtab);
4448 }
4449 }
4450
4451 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4452 matched, to expand corresponding CUs that were marked. IDX is the
4453 index of the symbol name that matched. */
4454
4455 static void
4456 dw2_expand_marked_cus
4457 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
4458 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4459 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4460 search_domain kind)
4461 {
4462 offset_type *vec, vec_len, vec_idx;
4463 bool global_seen = false;
4464 mapped_index &index = *dwarf2_per_objfile->index_table;
4465
4466 vec = (offset_type *) (index.constant_pool
4467 + MAYBE_SWAP (index.symbol_table[idx].vec));
4468 vec_len = MAYBE_SWAP (vec[0]);
4469 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4470 {
4471 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4472 /* This value is only valid for index versions >= 7. */
4473 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4474 gdb_index_symbol_kind symbol_kind =
4475 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4476 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4477 /* Only check the symbol attributes if they're present.
4478 Indices prior to version 7 don't record them,
4479 and indices >= 7 may elide them for certain symbols
4480 (gold does this). */
4481 int attrs_valid =
4482 (index.version >= 7
4483 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4484
4485 /* Work around gold/15646. */
4486 if (attrs_valid)
4487 {
4488 if (!is_static && global_seen)
4489 continue;
4490 if (!is_static)
4491 global_seen = true;
4492 }
4493
4494 /* Only check the symbol's kind if it has one. */
4495 if (attrs_valid)
4496 {
4497 switch (kind)
4498 {
4499 case VARIABLES_DOMAIN:
4500 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4501 continue;
4502 break;
4503 case FUNCTIONS_DOMAIN:
4504 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4505 continue;
4506 break;
4507 case TYPES_DOMAIN:
4508 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4509 continue;
4510 break;
4511 case MODULES_DOMAIN:
4512 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4513 continue;
4514 break;
4515 default:
4516 break;
4517 }
4518 }
4519
4520 /* Don't crash on bad data. */
4521 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
4522 + dwarf2_per_objfile->all_type_units.size ()))
4523 {
4524 complaint (_(".gdb_index entry has bad CU index"
4525 " [in module %s]"),
4526 objfile_name (dwarf2_per_objfile->objfile));
4527 continue;
4528 }
4529
4530 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4531 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4532 expansion_notify);
4533 }
4534 }
4535
4536 /* If FILE_MATCHER is non-NULL, set all the
4537 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4538 that match FILE_MATCHER. */
4539
4540 static void
4541 dw_expand_symtabs_matching_file_matcher
4542 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4543 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4544 {
4545 if (file_matcher == NULL)
4546 return;
4547
4548 objfile *const objfile = dwarf2_per_objfile->objfile;
4549
4550 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4551 htab_eq_pointer,
4552 NULL, xcalloc, xfree));
4553 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4554 htab_eq_pointer,
4555 NULL, xcalloc, xfree));
4556
4557 /* The rule is CUs specify all the files, including those used by
4558 any TU, so there's no need to scan TUs here. */
4559
4560 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4561 {
4562 QUIT;
4563
4564 per_cu->v.quick->mark = 0;
4565
4566 /* We only need to look at symtabs not already expanded. */
4567 if (per_cu->v.quick->compunit_symtab)
4568 continue;
4569
4570 quick_file_names *file_data = dw2_get_file_names (per_cu);
4571 if (file_data == NULL)
4572 continue;
4573
4574 if (htab_find (visited_not_found.get (), file_data) != NULL)
4575 continue;
4576 else if (htab_find (visited_found.get (), file_data) != NULL)
4577 {
4578 per_cu->v.quick->mark = 1;
4579 continue;
4580 }
4581
4582 for (int j = 0; j < file_data->num_file_names; ++j)
4583 {
4584 const char *this_real_name;
4585
4586 if (file_matcher (file_data->file_names[j], false))
4587 {
4588 per_cu->v.quick->mark = 1;
4589 break;
4590 }
4591
4592 /* Before we invoke realpath, which can get expensive when many
4593 files are involved, do a quick comparison of the basenames. */
4594 if (!basenames_may_differ
4595 && !file_matcher (lbasename (file_data->file_names[j]),
4596 true))
4597 continue;
4598
4599 this_real_name = dw2_get_real_path (objfile, file_data, j);
4600 if (file_matcher (this_real_name, false))
4601 {
4602 per_cu->v.quick->mark = 1;
4603 break;
4604 }
4605 }
4606
4607 void **slot = htab_find_slot (per_cu->v.quick->mark
4608 ? visited_found.get ()
4609 : visited_not_found.get (),
4610 file_data, INSERT);
4611 *slot = file_data;
4612 }
4613 }
4614
4615 static void
4616 dw2_expand_symtabs_matching
4617 (struct objfile *objfile,
4618 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4619 const lookup_name_info &lookup_name,
4620 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4621 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4622 enum search_domain kind)
4623 {
4624 struct dwarf2_per_objfile *dwarf2_per_objfile
4625 = get_dwarf2_per_objfile (objfile);
4626
4627 /* index_table is NULL if OBJF_READNOW. */
4628 if (!dwarf2_per_objfile->index_table)
4629 return;
4630
4631 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
4632
4633 mapped_index &index = *dwarf2_per_objfile->index_table;
4634
4635 dw2_expand_symtabs_matching_symbol (index, lookup_name,
4636 symbol_matcher,
4637 kind, [&] (offset_type idx)
4638 {
4639 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
4640 expansion_notify, kind);
4641 return true;
4642 });
4643 }
4644
4645 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4646 symtab. */
4647
4648 static struct compunit_symtab *
4649 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4650 CORE_ADDR pc)
4651 {
4652 int i;
4653
4654 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4655 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4656 return cust;
4657
4658 if (cust->includes == NULL)
4659 return NULL;
4660
4661 for (i = 0; cust->includes[i]; ++i)
4662 {
4663 struct compunit_symtab *s = cust->includes[i];
4664
4665 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4666 if (s != NULL)
4667 return s;
4668 }
4669
4670 return NULL;
4671 }
4672
4673 static struct compunit_symtab *
4674 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4675 struct bound_minimal_symbol msymbol,
4676 CORE_ADDR pc,
4677 struct obj_section *section,
4678 int warn_if_readin)
4679 {
4680 struct dwarf2_per_cu_data *data;
4681 struct compunit_symtab *result;
4682
4683 if (!objfile->partial_symtabs->psymtabs_addrmap)
4684 return NULL;
4685
4686 CORE_ADDR baseaddr = objfile->text_section_offset ();
4687 data = (struct dwarf2_per_cu_data *) addrmap_find
4688 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4689 if (!data)
4690 return NULL;
4691
4692 if (warn_if_readin && data->v.quick->compunit_symtab)
4693 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4694 paddress (get_objfile_arch (objfile), pc));
4695
4696 result
4697 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
4698 false),
4699 pc);
4700 gdb_assert (result != NULL);
4701 return result;
4702 }
4703
4704 static void
4705 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4706 void *data, int need_fullname)
4707 {
4708 struct dwarf2_per_objfile *dwarf2_per_objfile
4709 = get_dwarf2_per_objfile (objfile);
4710
4711 if (!dwarf2_per_objfile->filenames_cache)
4712 {
4713 dwarf2_per_objfile->filenames_cache.emplace ();
4714
4715 htab_up visited (htab_create_alloc (10,
4716 htab_hash_pointer, htab_eq_pointer,
4717 NULL, xcalloc, xfree));
4718
4719 /* The rule is CUs specify all the files, including those used
4720 by any TU, so there's no need to scan TUs here. We can
4721 ignore file names coming from already-expanded CUs. */
4722
4723 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4724 {
4725 if (per_cu->v.quick->compunit_symtab)
4726 {
4727 void **slot = htab_find_slot (visited.get (),
4728 per_cu->v.quick->file_names,
4729 INSERT);
4730
4731 *slot = per_cu->v.quick->file_names;
4732 }
4733 }
4734
4735 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4736 {
4737 /* We only need to look at symtabs not already expanded. */
4738 if (per_cu->v.quick->compunit_symtab)
4739 continue;
4740
4741 quick_file_names *file_data = dw2_get_file_names (per_cu);
4742 if (file_data == NULL)
4743 continue;
4744
4745 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4746 if (*slot)
4747 {
4748 /* Already visited. */
4749 continue;
4750 }
4751 *slot = file_data;
4752
4753 for (int j = 0; j < file_data->num_file_names; ++j)
4754 {
4755 const char *filename = file_data->file_names[j];
4756 dwarf2_per_objfile->filenames_cache->seen (filename);
4757 }
4758 }
4759 }
4760
4761 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
4762 {
4763 gdb::unique_xmalloc_ptr<char> this_real_name;
4764
4765 if (need_fullname)
4766 this_real_name = gdb_realpath (filename);
4767 (*fun) (filename, this_real_name.get (), data);
4768 });
4769 }
4770
4771 static int
4772 dw2_has_symbols (struct objfile *objfile)
4773 {
4774 return 1;
4775 }
4776
4777 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4778 {
4779 dw2_has_symbols,
4780 dw2_find_last_source_symtab,
4781 dw2_forget_cached_source_info,
4782 dw2_map_symtabs_matching_filename,
4783 dw2_lookup_symbol,
4784 dw2_print_stats,
4785 dw2_dump,
4786 dw2_expand_symtabs_for_function,
4787 dw2_expand_all_symtabs,
4788 dw2_expand_symtabs_with_fullname,
4789 dw2_map_matching_symbols,
4790 dw2_expand_symtabs_matching,
4791 dw2_find_pc_sect_compunit_symtab,
4792 NULL,
4793 dw2_map_symbol_filenames
4794 };
4795
4796 /* DWARF-5 debug_names reader. */
4797
4798 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4799 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4800
4801 /* A helper function that reads the .debug_names section in SECTION
4802 and fills in MAP. FILENAME is the name of the file containing the
4803 section; it is used for error reporting.
4804
4805 Returns true if all went well, false otherwise. */
4806
4807 static bool
4808 read_debug_names_from_section (struct objfile *objfile,
4809 const char *filename,
4810 struct dwarf2_section_info *section,
4811 mapped_debug_names &map)
4812 {
4813 if (section->empty ())
4814 return false;
4815
4816 /* Older elfutils strip versions could keep the section in the main
4817 executable while splitting it for the separate debug info file. */
4818 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4819 return false;
4820
4821 section->read (objfile);
4822
4823 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
4824
4825 const gdb_byte *addr = section->buffer;
4826
4827 bfd *const abfd = section->get_bfd_owner ();
4828
4829 unsigned int bytes_read;
4830 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4831 addr += bytes_read;
4832
4833 map.dwarf5_is_dwarf64 = bytes_read != 4;
4834 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4835 if (bytes_read + length != section->size)
4836 {
4837 /* There may be multiple per-CU indices. */
4838 warning (_("Section .debug_names in %s length %s does not match "
4839 "section length %s, ignoring .debug_names."),
4840 filename, plongest (bytes_read + length),
4841 pulongest (section->size));
4842 return false;
4843 }
4844
4845 /* The version number. */
4846 uint16_t version = read_2_bytes (abfd, addr);
4847 addr += 2;
4848 if (version != 5)
4849 {
4850 warning (_("Section .debug_names in %s has unsupported version %d, "
4851 "ignoring .debug_names."),
4852 filename, version);
4853 return false;
4854 }
4855
4856 /* Padding. */
4857 uint16_t padding = read_2_bytes (abfd, addr);
4858 addr += 2;
4859 if (padding != 0)
4860 {
4861 warning (_("Section .debug_names in %s has unsupported padding %d, "
4862 "ignoring .debug_names."),
4863 filename, padding);
4864 return false;
4865 }
4866
4867 /* comp_unit_count - The number of CUs in the CU list. */
4868 map.cu_count = read_4_bytes (abfd, addr);
4869 addr += 4;
4870
4871 /* local_type_unit_count - The number of TUs in the local TU
4872 list. */
4873 map.tu_count = read_4_bytes (abfd, addr);
4874 addr += 4;
4875
4876 /* foreign_type_unit_count - The number of TUs in the foreign TU
4877 list. */
4878 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4879 addr += 4;
4880 if (foreign_tu_count != 0)
4881 {
4882 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4883 "ignoring .debug_names."),
4884 filename, static_cast<unsigned long> (foreign_tu_count));
4885 return false;
4886 }
4887
4888 /* bucket_count - The number of hash buckets in the hash lookup
4889 table. */
4890 map.bucket_count = read_4_bytes (abfd, addr);
4891 addr += 4;
4892
4893 /* name_count - The number of unique names in the index. */
4894 map.name_count = read_4_bytes (abfd, addr);
4895 addr += 4;
4896
4897 /* abbrev_table_size - The size in bytes of the abbreviations
4898 table. */
4899 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4900 addr += 4;
4901
4902 /* augmentation_string_size - The size in bytes of the augmentation
4903 string. This value is rounded up to a multiple of 4. */
4904 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4905 addr += 4;
4906 map.augmentation_is_gdb = ((augmentation_string_size
4907 == sizeof (dwarf5_augmentation))
4908 && memcmp (addr, dwarf5_augmentation,
4909 sizeof (dwarf5_augmentation)) == 0);
4910 augmentation_string_size += (-augmentation_string_size) & 3;
4911 addr += augmentation_string_size;
4912
4913 /* List of CUs */
4914 map.cu_table_reordered = addr;
4915 addr += map.cu_count * map.offset_size;
4916
4917 /* List of Local TUs */
4918 map.tu_table_reordered = addr;
4919 addr += map.tu_count * map.offset_size;
4920
4921 /* Hash Lookup Table */
4922 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4923 addr += map.bucket_count * 4;
4924 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4925 addr += map.name_count * 4;
4926
4927 /* Name Table */
4928 map.name_table_string_offs_reordered = addr;
4929 addr += map.name_count * map.offset_size;
4930 map.name_table_entry_offs_reordered = addr;
4931 addr += map.name_count * map.offset_size;
4932
4933 const gdb_byte *abbrev_table_start = addr;
4934 for (;;)
4935 {
4936 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4937 addr += bytes_read;
4938 if (index_num == 0)
4939 break;
4940
4941 const auto insertpair
4942 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4943 if (!insertpair.second)
4944 {
4945 warning (_("Section .debug_names in %s has duplicate index %s, "
4946 "ignoring .debug_names."),
4947 filename, pulongest (index_num));
4948 return false;
4949 }
4950 mapped_debug_names::index_val &indexval = insertpair.first->second;
4951 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4952 addr += bytes_read;
4953
4954 for (;;)
4955 {
4956 mapped_debug_names::index_val::attr attr;
4957 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4958 addr += bytes_read;
4959 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4960 addr += bytes_read;
4961 if (attr.form == DW_FORM_implicit_const)
4962 {
4963 attr.implicit_const = read_signed_leb128 (abfd, addr,
4964 &bytes_read);
4965 addr += bytes_read;
4966 }
4967 if (attr.dw_idx == 0 && attr.form == 0)
4968 break;
4969 indexval.attr_vec.push_back (std::move (attr));
4970 }
4971 }
4972 if (addr != abbrev_table_start + abbrev_table_size)
4973 {
4974 warning (_("Section .debug_names in %s has abbreviation_table "
4975 "of size %s vs. written as %u, ignoring .debug_names."),
4976 filename, plongest (addr - abbrev_table_start),
4977 abbrev_table_size);
4978 return false;
4979 }
4980 map.entry_pool = addr;
4981
4982 return true;
4983 }
4984
4985 /* A helper for create_cus_from_debug_names that handles the MAP's CU
4986 list. */
4987
4988 static void
4989 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4990 const mapped_debug_names &map,
4991 dwarf2_section_info &section,
4992 bool is_dwz)
4993 {
4994 sect_offset sect_off_prev;
4995 for (uint32_t i = 0; i <= map.cu_count; ++i)
4996 {
4997 sect_offset sect_off_next;
4998 if (i < map.cu_count)
4999 {
5000 sect_off_next
5001 = (sect_offset) (extract_unsigned_integer
5002 (map.cu_table_reordered + i * map.offset_size,
5003 map.offset_size,
5004 map.dwarf5_byte_order));
5005 }
5006 else
5007 sect_off_next = (sect_offset) section.size;
5008 if (i >= 1)
5009 {
5010 const ULONGEST length = sect_off_next - sect_off_prev;
5011 dwarf2_per_cu_data *per_cu
5012 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5013 sect_off_prev, length);
5014 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
5015 }
5016 sect_off_prev = sect_off_next;
5017 }
5018 }
5019
5020 /* Read the CU list from the mapped index, and use it to create all
5021 the CU objects for this dwarf2_per_objfile. */
5022
5023 static void
5024 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5025 const mapped_debug_names &map,
5026 const mapped_debug_names &dwz_map)
5027 {
5028 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5029 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5030
5031 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5032 dwarf2_per_objfile->info,
5033 false /* is_dwz */);
5034
5035 if (dwz_map.cu_count == 0)
5036 return;
5037
5038 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5039 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5040 true /* is_dwz */);
5041 }
5042
5043 /* Read .debug_names. If everything went ok, initialize the "quick"
5044 elements of all the CUs and return true. Otherwise, return false. */
5045
5046 static bool
5047 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5048 {
5049 std::unique_ptr<mapped_debug_names> map
5050 (new mapped_debug_names (dwarf2_per_objfile));
5051 mapped_debug_names dwz_map (dwarf2_per_objfile);
5052 struct objfile *objfile = dwarf2_per_objfile->objfile;
5053
5054 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5055 &dwarf2_per_objfile->debug_names,
5056 *map))
5057 return false;
5058
5059 /* Don't use the index if it's empty. */
5060 if (map->name_count == 0)
5061 return false;
5062
5063 /* If there is a .dwz file, read it so we can get its CU list as
5064 well. */
5065 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5066 if (dwz != NULL)
5067 {
5068 if (!read_debug_names_from_section (objfile,
5069 bfd_get_filename (dwz->dwz_bfd.get ()),
5070 &dwz->debug_names, dwz_map))
5071 {
5072 warning (_("could not read '.debug_names' section from %s; skipping"),
5073 bfd_get_filename (dwz->dwz_bfd.get ()));
5074 return false;
5075 }
5076 }
5077
5078 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5079
5080 if (map->tu_count != 0)
5081 {
5082 /* We can only handle a single .debug_types when we have an
5083 index. */
5084 if (dwarf2_per_objfile->types.size () != 1)
5085 return false;
5086
5087 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
5088
5089 create_signatured_type_table_from_debug_names
5090 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
5091 }
5092
5093 create_addrmap_from_aranges (dwarf2_per_objfile,
5094 &dwarf2_per_objfile->debug_aranges);
5095
5096 dwarf2_per_objfile->debug_names_table = std::move (map);
5097 dwarf2_per_objfile->using_index = 1;
5098 dwarf2_per_objfile->quick_file_names_table =
5099 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5100
5101 return true;
5102 }
5103
5104 /* Type used to manage iterating over all CUs looking for a symbol for
5105 .debug_names. */
5106
5107 class dw2_debug_names_iterator
5108 {
5109 public:
5110 dw2_debug_names_iterator (const mapped_debug_names &map,
5111 gdb::optional<block_enum> block_index,
5112 domain_enum domain,
5113 const char *name)
5114 : m_map (map), m_block_index (block_index), m_domain (domain),
5115 m_addr (find_vec_in_debug_names (map, name))
5116 {}
5117
5118 dw2_debug_names_iterator (const mapped_debug_names &map,
5119 search_domain search, uint32_t namei)
5120 : m_map (map),
5121 m_search (search),
5122 m_addr (find_vec_in_debug_names (map, namei))
5123 {}
5124
5125 dw2_debug_names_iterator (const mapped_debug_names &map,
5126 block_enum block_index, domain_enum domain,
5127 uint32_t namei)
5128 : m_map (map), m_block_index (block_index), m_domain (domain),
5129 m_addr (find_vec_in_debug_names (map, namei))
5130 {}
5131
5132 /* Return the next matching CU or NULL if there are no more. */
5133 dwarf2_per_cu_data *next ();
5134
5135 private:
5136 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5137 const char *name);
5138 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5139 uint32_t namei);
5140
5141 /* The internalized form of .debug_names. */
5142 const mapped_debug_names &m_map;
5143
5144 /* If set, only look for symbols that match that block. Valid values are
5145 GLOBAL_BLOCK and STATIC_BLOCK. */
5146 const gdb::optional<block_enum> m_block_index;
5147
5148 /* The kind of symbol we're looking for. */
5149 const domain_enum m_domain = UNDEF_DOMAIN;
5150 const search_domain m_search = ALL_DOMAIN;
5151
5152 /* The list of CUs from the index entry of the symbol, or NULL if
5153 not found. */
5154 const gdb_byte *m_addr;
5155 };
5156
5157 const char *
5158 mapped_debug_names::namei_to_name (uint32_t namei) const
5159 {
5160 const ULONGEST namei_string_offs
5161 = extract_unsigned_integer ((name_table_string_offs_reordered
5162 + namei * offset_size),
5163 offset_size,
5164 dwarf5_byte_order);
5165 return read_indirect_string_at_offset (dwarf2_per_objfile,
5166 namei_string_offs);
5167 }
5168
5169 /* Find a slot in .debug_names for the object named NAME. If NAME is
5170 found, return pointer to its pool data. If NAME cannot be found,
5171 return NULL. */
5172
5173 const gdb_byte *
5174 dw2_debug_names_iterator::find_vec_in_debug_names
5175 (const mapped_debug_names &map, const char *name)
5176 {
5177 int (*cmp) (const char *, const char *);
5178
5179 gdb::unique_xmalloc_ptr<char> without_params;
5180 if (current_language->la_language == language_cplus
5181 || current_language->la_language == language_fortran
5182 || current_language->la_language == language_d)
5183 {
5184 /* NAME is already canonical. Drop any qualifiers as
5185 .debug_names does not contain any. */
5186
5187 if (strchr (name, '(') != NULL)
5188 {
5189 without_params = cp_remove_params (name);
5190 if (without_params != NULL)
5191 name = without_params.get ();
5192 }
5193 }
5194
5195 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5196
5197 const uint32_t full_hash = dwarf5_djb_hash (name);
5198 uint32_t namei
5199 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5200 (map.bucket_table_reordered
5201 + (full_hash % map.bucket_count)), 4,
5202 map.dwarf5_byte_order);
5203 if (namei == 0)
5204 return NULL;
5205 --namei;
5206 if (namei >= map.name_count)
5207 {
5208 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5209 "[in module %s]"),
5210 namei, map.name_count,
5211 objfile_name (map.dwarf2_per_objfile->objfile));
5212 return NULL;
5213 }
5214
5215 for (;;)
5216 {
5217 const uint32_t namei_full_hash
5218 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5219 (map.hash_table_reordered + namei), 4,
5220 map.dwarf5_byte_order);
5221 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5222 return NULL;
5223
5224 if (full_hash == namei_full_hash)
5225 {
5226 const char *const namei_string = map.namei_to_name (namei);
5227
5228 #if 0 /* An expensive sanity check. */
5229 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5230 {
5231 complaint (_("Wrong .debug_names hash for string at index %u "
5232 "[in module %s]"),
5233 namei, objfile_name (dwarf2_per_objfile->objfile));
5234 return NULL;
5235 }
5236 #endif
5237
5238 if (cmp (namei_string, name) == 0)
5239 {
5240 const ULONGEST namei_entry_offs
5241 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5242 + namei * map.offset_size),
5243 map.offset_size, map.dwarf5_byte_order);
5244 return map.entry_pool + namei_entry_offs;
5245 }
5246 }
5247
5248 ++namei;
5249 if (namei >= map.name_count)
5250 return NULL;
5251 }
5252 }
5253
5254 const gdb_byte *
5255 dw2_debug_names_iterator::find_vec_in_debug_names
5256 (const mapped_debug_names &map, uint32_t namei)
5257 {
5258 if (namei >= map.name_count)
5259 {
5260 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5261 "[in module %s]"),
5262 namei, map.name_count,
5263 objfile_name (map.dwarf2_per_objfile->objfile));
5264 return NULL;
5265 }
5266
5267 const ULONGEST namei_entry_offs
5268 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5269 + namei * map.offset_size),
5270 map.offset_size, map.dwarf5_byte_order);
5271 return map.entry_pool + namei_entry_offs;
5272 }
5273
5274 /* See dw2_debug_names_iterator. */
5275
5276 dwarf2_per_cu_data *
5277 dw2_debug_names_iterator::next ()
5278 {
5279 if (m_addr == NULL)
5280 return NULL;
5281
5282 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5283 struct objfile *objfile = dwarf2_per_objfile->objfile;
5284 bfd *const abfd = objfile->obfd;
5285
5286 again:
5287
5288 unsigned int bytes_read;
5289 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5290 m_addr += bytes_read;
5291 if (abbrev == 0)
5292 return NULL;
5293
5294 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5295 if (indexval_it == m_map.abbrev_map.cend ())
5296 {
5297 complaint (_("Wrong .debug_names undefined abbrev code %s "
5298 "[in module %s]"),
5299 pulongest (abbrev), objfile_name (objfile));
5300 return NULL;
5301 }
5302 const mapped_debug_names::index_val &indexval = indexval_it->second;
5303 enum class symbol_linkage {
5304 unknown,
5305 static_,
5306 extern_,
5307 } symbol_linkage_ = symbol_linkage::unknown;
5308 dwarf2_per_cu_data *per_cu = NULL;
5309 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5310 {
5311 ULONGEST ull;
5312 switch (attr.form)
5313 {
5314 case DW_FORM_implicit_const:
5315 ull = attr.implicit_const;
5316 break;
5317 case DW_FORM_flag_present:
5318 ull = 1;
5319 break;
5320 case DW_FORM_udata:
5321 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5322 m_addr += bytes_read;
5323 break;
5324 default:
5325 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5326 dwarf_form_name (attr.form),
5327 objfile_name (objfile));
5328 return NULL;
5329 }
5330 switch (attr.dw_idx)
5331 {
5332 case DW_IDX_compile_unit:
5333 /* Don't crash on bad data. */
5334 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5335 {
5336 complaint (_(".debug_names entry has bad CU index %s"
5337 " [in module %s]"),
5338 pulongest (ull),
5339 objfile_name (dwarf2_per_objfile->objfile));
5340 continue;
5341 }
5342 per_cu = dwarf2_per_objfile->get_cutu (ull);
5343 break;
5344 case DW_IDX_type_unit:
5345 /* Don't crash on bad data. */
5346 if (ull >= dwarf2_per_objfile->all_type_units.size ())
5347 {
5348 complaint (_(".debug_names entry has bad TU index %s"
5349 " [in module %s]"),
5350 pulongest (ull),
5351 objfile_name (dwarf2_per_objfile->objfile));
5352 continue;
5353 }
5354 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5355 break;
5356 case DW_IDX_GNU_internal:
5357 if (!m_map.augmentation_is_gdb)
5358 break;
5359 symbol_linkage_ = symbol_linkage::static_;
5360 break;
5361 case DW_IDX_GNU_external:
5362 if (!m_map.augmentation_is_gdb)
5363 break;
5364 symbol_linkage_ = symbol_linkage::extern_;
5365 break;
5366 }
5367 }
5368
5369 /* Skip if already read in. */
5370 if (per_cu->v.quick->compunit_symtab)
5371 goto again;
5372
5373 /* Check static vs global. */
5374 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5375 {
5376 const bool want_static = *m_block_index == STATIC_BLOCK;
5377 const bool symbol_is_static =
5378 symbol_linkage_ == symbol_linkage::static_;
5379 if (want_static != symbol_is_static)
5380 goto again;
5381 }
5382
5383 /* Match dw2_symtab_iter_next, symbol_kind
5384 and debug_names::psymbol_tag. */
5385 switch (m_domain)
5386 {
5387 case VAR_DOMAIN:
5388 switch (indexval.dwarf_tag)
5389 {
5390 case DW_TAG_variable:
5391 case DW_TAG_subprogram:
5392 /* Some types are also in VAR_DOMAIN. */
5393 case DW_TAG_typedef:
5394 case DW_TAG_structure_type:
5395 break;
5396 default:
5397 goto again;
5398 }
5399 break;
5400 case STRUCT_DOMAIN:
5401 switch (indexval.dwarf_tag)
5402 {
5403 case DW_TAG_typedef:
5404 case DW_TAG_structure_type:
5405 break;
5406 default:
5407 goto again;
5408 }
5409 break;
5410 case LABEL_DOMAIN:
5411 switch (indexval.dwarf_tag)
5412 {
5413 case 0:
5414 case DW_TAG_variable:
5415 break;
5416 default:
5417 goto again;
5418 }
5419 break;
5420 case MODULE_DOMAIN:
5421 switch (indexval.dwarf_tag)
5422 {
5423 case DW_TAG_module:
5424 break;
5425 default:
5426 goto again;
5427 }
5428 break;
5429 default:
5430 break;
5431 }
5432
5433 /* Match dw2_expand_symtabs_matching, symbol_kind and
5434 debug_names::psymbol_tag. */
5435 switch (m_search)
5436 {
5437 case VARIABLES_DOMAIN:
5438 switch (indexval.dwarf_tag)
5439 {
5440 case DW_TAG_variable:
5441 break;
5442 default:
5443 goto again;
5444 }
5445 break;
5446 case FUNCTIONS_DOMAIN:
5447 switch (indexval.dwarf_tag)
5448 {
5449 case DW_TAG_subprogram:
5450 break;
5451 default:
5452 goto again;
5453 }
5454 break;
5455 case TYPES_DOMAIN:
5456 switch (indexval.dwarf_tag)
5457 {
5458 case DW_TAG_typedef:
5459 case DW_TAG_structure_type:
5460 break;
5461 default:
5462 goto again;
5463 }
5464 break;
5465 case MODULES_DOMAIN:
5466 switch (indexval.dwarf_tag)
5467 {
5468 case DW_TAG_module:
5469 break;
5470 default:
5471 goto again;
5472 }
5473 default:
5474 break;
5475 }
5476
5477 return per_cu;
5478 }
5479
5480 static struct compunit_symtab *
5481 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5482 const char *name, domain_enum domain)
5483 {
5484 struct dwarf2_per_objfile *dwarf2_per_objfile
5485 = get_dwarf2_per_objfile (objfile);
5486
5487 const auto &mapp = dwarf2_per_objfile->debug_names_table;
5488 if (!mapp)
5489 {
5490 /* index is NULL if OBJF_READNOW. */
5491 return NULL;
5492 }
5493 const auto &map = *mapp;
5494
5495 dw2_debug_names_iterator iter (map, block_index, domain, name);
5496
5497 struct compunit_symtab *stab_best = NULL;
5498 struct dwarf2_per_cu_data *per_cu;
5499 while ((per_cu = iter.next ()) != NULL)
5500 {
5501 struct symbol *sym, *with_opaque = NULL;
5502 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
5503 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5504 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5505
5506 sym = block_find_symbol (block, name, domain,
5507 block_find_non_opaque_type_preferred,
5508 &with_opaque);
5509
5510 /* Some caution must be observed with overloaded functions and
5511 methods, since the index will not contain any overload
5512 information (but NAME might contain it). */
5513
5514 if (sym != NULL
5515 && strcmp_iw (sym->search_name (), name) == 0)
5516 return stab;
5517 if (with_opaque != NULL
5518 && strcmp_iw (with_opaque->search_name (), name) == 0)
5519 stab_best = stab;
5520
5521 /* Keep looking through other CUs. */
5522 }
5523
5524 return stab_best;
5525 }
5526
5527 /* This dumps minimal information about .debug_names. It is called
5528 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5529 uses this to verify that .debug_names has been loaded. */
5530
5531 static void
5532 dw2_debug_names_dump (struct objfile *objfile)
5533 {
5534 struct dwarf2_per_objfile *dwarf2_per_objfile
5535 = get_dwarf2_per_objfile (objfile);
5536
5537 gdb_assert (dwarf2_per_objfile->using_index);
5538 printf_filtered (".debug_names:");
5539 if (dwarf2_per_objfile->debug_names_table)
5540 printf_filtered (" exists\n");
5541 else
5542 printf_filtered (" faked for \"readnow\"\n");
5543 printf_filtered ("\n");
5544 }
5545
5546 static void
5547 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5548 const char *func_name)
5549 {
5550 struct dwarf2_per_objfile *dwarf2_per_objfile
5551 = get_dwarf2_per_objfile (objfile);
5552
5553 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
5554 if (dwarf2_per_objfile->debug_names_table)
5555 {
5556 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5557
5558 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
5559
5560 struct dwarf2_per_cu_data *per_cu;
5561 while ((per_cu = iter.next ()) != NULL)
5562 dw2_instantiate_symtab (per_cu, false);
5563 }
5564 }
5565
5566 static void
5567 dw2_debug_names_map_matching_symbols
5568 (struct objfile *objfile,
5569 const lookup_name_info &name, domain_enum domain,
5570 int global,
5571 gdb::function_view<symbol_found_callback_ftype> callback,
5572 symbol_compare_ftype *ordered_compare)
5573 {
5574 struct dwarf2_per_objfile *dwarf2_per_objfile
5575 = get_dwarf2_per_objfile (objfile);
5576
5577 /* debug_names_table is NULL if OBJF_READNOW. */
5578 if (!dwarf2_per_objfile->debug_names_table)
5579 return;
5580
5581 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5582 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5583
5584 const char *match_name = name.ada ().lookup_name ().c_str ();
5585 auto matcher = [&] (const char *symname)
5586 {
5587 if (ordered_compare == nullptr)
5588 return true;
5589 return ordered_compare (symname, match_name) == 0;
5590 };
5591
5592 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5593 [&] (offset_type namei)
5594 {
5595 /* The name was matched, now expand corresponding CUs that were
5596 marked. */
5597 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5598
5599 struct dwarf2_per_cu_data *per_cu;
5600 while ((per_cu = iter.next ()) != NULL)
5601 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
5602 return true;
5603 });
5604
5605 /* It's a shame we couldn't do this inside the
5606 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5607 that have already been expanded. Instead, this loop matches what
5608 the psymtab code does. */
5609 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5610 {
5611 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
5612 if (cust != nullptr)
5613 {
5614 const struct block *block
5615 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
5616 if (!iterate_over_symbols_terminated (block, name,
5617 domain, callback))
5618 break;
5619 }
5620 }
5621 }
5622
5623 static void
5624 dw2_debug_names_expand_symtabs_matching
5625 (struct objfile *objfile,
5626 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5627 const lookup_name_info &lookup_name,
5628 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5629 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5630 enum search_domain kind)
5631 {
5632 struct dwarf2_per_objfile *dwarf2_per_objfile
5633 = get_dwarf2_per_objfile (objfile);
5634
5635 /* debug_names_table is NULL if OBJF_READNOW. */
5636 if (!dwarf2_per_objfile->debug_names_table)
5637 return;
5638
5639 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5640
5641 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5642
5643 dw2_expand_symtabs_matching_symbol (map, lookup_name,
5644 symbol_matcher,
5645 kind, [&] (offset_type namei)
5646 {
5647 /* The name was matched, now expand corresponding CUs that were
5648 marked. */
5649 dw2_debug_names_iterator iter (map, kind, namei);
5650
5651 struct dwarf2_per_cu_data *per_cu;
5652 while ((per_cu = iter.next ()) != NULL)
5653 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5654 expansion_notify);
5655 return true;
5656 });
5657 }
5658
5659 const struct quick_symbol_functions dwarf2_debug_names_functions =
5660 {
5661 dw2_has_symbols,
5662 dw2_find_last_source_symtab,
5663 dw2_forget_cached_source_info,
5664 dw2_map_symtabs_matching_filename,
5665 dw2_debug_names_lookup_symbol,
5666 dw2_print_stats,
5667 dw2_debug_names_dump,
5668 dw2_debug_names_expand_symtabs_for_function,
5669 dw2_expand_all_symtabs,
5670 dw2_expand_symtabs_with_fullname,
5671 dw2_debug_names_map_matching_symbols,
5672 dw2_debug_names_expand_symtabs_matching,
5673 dw2_find_pc_sect_compunit_symtab,
5674 NULL,
5675 dw2_map_symbol_filenames
5676 };
5677
5678 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5679 to either a dwarf2_per_objfile or dwz_file object. */
5680
5681 template <typename T>
5682 static gdb::array_view<const gdb_byte>
5683 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5684 {
5685 dwarf2_section_info *section = &section_owner->gdb_index;
5686
5687 if (section->empty ())
5688 return {};
5689
5690 /* Older elfutils strip versions could keep the section in the main
5691 executable while splitting it for the separate debug info file. */
5692 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5693 return {};
5694
5695 section->read (obj);
5696
5697 /* dwarf2_section_info::size is a bfd_size_type, while
5698 gdb::array_view works with size_t. On 32-bit hosts, with
5699 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5700 is 32-bit. So we need an explicit narrowing conversion here.
5701 This is fine, because it's impossible to allocate or mmap an
5702 array/buffer larger than what size_t can represent. */
5703 return gdb::make_array_view (section->buffer, section->size);
5704 }
5705
5706 /* Lookup the index cache for the contents of the index associated to
5707 DWARF2_OBJ. */
5708
5709 static gdb::array_view<const gdb_byte>
5710 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
5711 {
5712 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5713 if (build_id == nullptr)
5714 return {};
5715
5716 return global_index_cache.lookup_gdb_index (build_id,
5717 &dwarf2_obj->index_cache_res);
5718 }
5719
5720 /* Same as the above, but for DWZ. */
5721
5722 static gdb::array_view<const gdb_byte>
5723 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5724 {
5725 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5726 if (build_id == nullptr)
5727 return {};
5728
5729 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5730 }
5731
5732 /* See symfile.h. */
5733
5734 bool
5735 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
5736 {
5737 struct dwarf2_per_objfile *dwarf2_per_objfile
5738 = get_dwarf2_per_objfile (objfile);
5739
5740 /* If we're about to read full symbols, don't bother with the
5741 indices. In this case we also don't care if some other debug
5742 format is making psymtabs, because they are all about to be
5743 expanded anyway. */
5744 if ((objfile->flags & OBJF_READNOW))
5745 {
5746 dwarf2_per_objfile->using_index = 1;
5747 create_all_comp_units (dwarf2_per_objfile);
5748 create_all_type_units (dwarf2_per_objfile);
5749 dwarf2_per_objfile->quick_file_names_table
5750 = create_quick_file_names_table
5751 (dwarf2_per_objfile->all_comp_units.size ());
5752
5753 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
5754 + dwarf2_per_objfile->all_type_units.size ()); ++i)
5755 {
5756 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
5757
5758 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5759 struct dwarf2_per_cu_quick_data);
5760 }
5761
5762 /* Return 1 so that gdb sees the "quick" functions. However,
5763 these functions will be no-ops because we will have expanded
5764 all symtabs. */
5765 *index_kind = dw_index_kind::GDB_INDEX;
5766 return true;
5767 }
5768
5769 if (dwarf2_read_debug_names (dwarf2_per_objfile))
5770 {
5771 *index_kind = dw_index_kind::DEBUG_NAMES;
5772 return true;
5773 }
5774
5775 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5776 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
5777 get_gdb_index_contents_from_section<dwz_file>))
5778 {
5779 *index_kind = dw_index_kind::GDB_INDEX;
5780 return true;
5781 }
5782
5783 /* ... otherwise, try to find the index in the index cache. */
5784 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5785 get_gdb_index_contents_from_cache,
5786 get_gdb_index_contents_from_cache_dwz))
5787 {
5788 global_index_cache.hit ();
5789 *index_kind = dw_index_kind::GDB_INDEX;
5790 return true;
5791 }
5792
5793 global_index_cache.miss ();
5794 return false;
5795 }
5796
5797 \f
5798
5799 /* Build a partial symbol table. */
5800
5801 void
5802 dwarf2_build_psymtabs (struct objfile *objfile)
5803 {
5804 struct dwarf2_per_objfile *dwarf2_per_objfile
5805 = get_dwarf2_per_objfile (objfile);
5806
5807 init_psymbol_list (objfile, 1024);
5808
5809 try
5810 {
5811 /* This isn't really ideal: all the data we allocate on the
5812 objfile's obstack is still uselessly kept around. However,
5813 freeing it seems unsafe. */
5814 psymtab_discarder psymtabs (objfile);
5815 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
5816 psymtabs.keep ();
5817
5818 /* (maybe) store an index in the cache. */
5819 global_index_cache.store (dwarf2_per_objfile);
5820 }
5821 catch (const gdb_exception_error &except)
5822 {
5823 exception_print (gdb_stderr, except);
5824 }
5825 }
5826
5827 /* Find the base address of the compilation unit for range lists and
5828 location lists. It will normally be specified by DW_AT_low_pc.
5829 In DWARF-3 draft 4, the base address could be overridden by
5830 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5831 compilation units with discontinuous ranges. */
5832
5833 static void
5834 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5835 {
5836 struct attribute *attr;
5837
5838 cu->base_known = 0;
5839 cu->base_address = 0;
5840
5841 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5842 if (attr != nullptr)
5843 {
5844 cu->base_address = attr->value_as_address ();
5845 cu->base_known = 1;
5846 }
5847 else
5848 {
5849 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5850 if (attr != nullptr)
5851 {
5852 cu->base_address = attr->value_as_address ();
5853 cu->base_known = 1;
5854 }
5855 }
5856 }
5857
5858 /* Helper function that returns the proper abbrev section for
5859 THIS_CU. */
5860
5861 static struct dwarf2_section_info *
5862 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5863 {
5864 struct dwarf2_section_info *abbrev;
5865 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
5866
5867 if (this_cu->is_dwz)
5868 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
5869 else
5870 abbrev = &dwarf2_per_objfile->abbrev;
5871
5872 return abbrev;
5873 }
5874
5875 /* Fetch the abbreviation table offset from a comp or type unit header. */
5876
5877 static sect_offset
5878 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
5879 struct dwarf2_section_info *section,
5880 sect_offset sect_off)
5881 {
5882 bfd *abfd = section->get_bfd_owner ();
5883 const gdb_byte *info_ptr;
5884 unsigned int initial_length_size, offset_size;
5885 uint16_t version;
5886
5887 section->read (dwarf2_per_objfile->objfile);
5888 info_ptr = section->buffer + to_underlying (sect_off);
5889 read_initial_length (abfd, info_ptr, &initial_length_size);
5890 offset_size = initial_length_size == 4 ? 4 : 8;
5891 info_ptr += initial_length_size;
5892
5893 version = read_2_bytes (abfd, info_ptr);
5894 info_ptr += 2;
5895 if (version >= 5)
5896 {
5897 /* Skip unit type and address size. */
5898 info_ptr += 2;
5899 }
5900
5901 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
5902 }
5903
5904 /* A partial symtab that is used only for include files. */
5905 struct dwarf2_include_psymtab : public partial_symtab
5906 {
5907 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
5908 : partial_symtab (filename, objfile)
5909 {
5910 }
5911
5912 void read_symtab (struct objfile *objfile) override
5913 {
5914 expand_psymtab (objfile);
5915 }
5916
5917 void expand_psymtab (struct objfile *objfile) override
5918 {
5919 if (m_readin)
5920 return;
5921 /* It's an include file, no symbols to read for it.
5922 Everything is in the parent symtab. */
5923 read_dependencies (objfile);
5924 m_readin = true;
5925 }
5926
5927 bool readin_p () const override
5928 {
5929 return m_readin;
5930 }
5931
5932 struct compunit_symtab *get_compunit_symtab () const override
5933 {
5934 return nullptr;
5935 }
5936
5937 private:
5938
5939 bool m_readin = false;
5940 };
5941
5942 /* Allocate a new partial symtab for file named NAME and mark this new
5943 partial symtab as being an include of PST. */
5944
5945 static void
5946 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
5947 struct objfile *objfile)
5948 {
5949 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
5950
5951 if (!IS_ABSOLUTE_PATH (subpst->filename))
5952 {
5953 /* It shares objfile->objfile_obstack. */
5954 subpst->dirname = pst->dirname;
5955 }
5956
5957 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
5958 subpst->dependencies[0] = pst;
5959 subpst->number_of_dependencies = 1;
5960 }
5961
5962 /* Read the Line Number Program data and extract the list of files
5963 included by the source file represented by PST. Build an include
5964 partial symtab for each of these included files. */
5965
5966 static void
5967 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
5968 struct die_info *die,
5969 dwarf2_psymtab *pst)
5970 {
5971 line_header_up lh;
5972 struct attribute *attr;
5973
5974 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5975 if (attr != nullptr)
5976 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
5977 if (lh == NULL)
5978 return; /* No linetable, so no includes. */
5979
5980 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
5981 that we pass in the raw text_low here; that is ok because we're
5982 only decoding the line table to make include partial symtabs, and
5983 so the addresses aren't really used. */
5984 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
5985 pst->raw_text_low (), 1);
5986 }
5987
5988 static hashval_t
5989 hash_signatured_type (const void *item)
5990 {
5991 const struct signatured_type *sig_type
5992 = (const struct signatured_type *) item;
5993
5994 /* This drops the top 32 bits of the signature, but is ok for a hash. */
5995 return sig_type->signature;
5996 }
5997
5998 static int
5999 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6000 {
6001 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6002 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6003
6004 return lhs->signature == rhs->signature;
6005 }
6006
6007 /* Allocate a hash table for signatured types. */
6008
6009 static htab_up
6010 allocate_signatured_type_table ()
6011 {
6012 return htab_up (htab_create_alloc (41,
6013 hash_signatured_type,
6014 eq_signatured_type,
6015 NULL, xcalloc, xfree));
6016 }
6017
6018 /* A helper function to add a signatured type CU to a table. */
6019
6020 static int
6021 add_signatured_type_cu_to_table (void **slot, void *datum)
6022 {
6023 struct signatured_type *sigt = (struct signatured_type *) *slot;
6024 std::vector<signatured_type *> *all_type_units
6025 = (std::vector<signatured_type *> *) datum;
6026
6027 all_type_units->push_back (sigt);
6028
6029 return 1;
6030 }
6031
6032 /* A helper for create_debug_types_hash_table. Read types from SECTION
6033 and fill them into TYPES_HTAB. It will process only type units,
6034 therefore DW_UT_type. */
6035
6036 static void
6037 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6038 struct dwo_file *dwo_file,
6039 dwarf2_section_info *section, htab_up &types_htab,
6040 rcuh_kind section_kind)
6041 {
6042 struct objfile *objfile = dwarf2_per_objfile->objfile;
6043 struct dwarf2_section_info *abbrev_section;
6044 bfd *abfd;
6045 const gdb_byte *info_ptr, *end_ptr;
6046
6047 abbrev_section = (dwo_file != NULL
6048 ? &dwo_file->sections.abbrev
6049 : &dwarf2_per_objfile->abbrev);
6050
6051 if (dwarf_read_debug)
6052 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6053 section->get_name (),
6054 abbrev_section->get_file_name ());
6055
6056 section->read (objfile);
6057 info_ptr = section->buffer;
6058
6059 if (info_ptr == NULL)
6060 return;
6061
6062 /* We can't set abfd until now because the section may be empty or
6063 not present, in which case the bfd is unknown. */
6064 abfd = section->get_bfd_owner ();
6065
6066 /* We don't use cutu_reader here because we don't need to read
6067 any dies: the signature is in the header. */
6068
6069 end_ptr = info_ptr + section->size;
6070 while (info_ptr < end_ptr)
6071 {
6072 struct signatured_type *sig_type;
6073 struct dwo_unit *dwo_tu;
6074 void **slot;
6075 const gdb_byte *ptr = info_ptr;
6076 struct comp_unit_head header;
6077 unsigned int length;
6078
6079 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6080
6081 /* Initialize it due to a false compiler warning. */
6082 header.signature = -1;
6083 header.type_cu_offset_in_tu = (cu_offset) -1;
6084
6085 /* We need to read the type's signature in order to build the hash
6086 table, but we don't need anything else just yet. */
6087
6088 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6089 abbrev_section, ptr, section_kind);
6090
6091 length = header.get_length ();
6092
6093 /* Skip dummy type units. */
6094 if (ptr >= info_ptr + length
6095 || peek_abbrev_code (abfd, ptr) == 0
6096 || header.unit_type != DW_UT_type)
6097 {
6098 info_ptr += length;
6099 continue;
6100 }
6101
6102 if (types_htab == NULL)
6103 {
6104 if (dwo_file)
6105 types_htab = allocate_dwo_unit_table ();
6106 else
6107 types_htab = allocate_signatured_type_table ();
6108 }
6109
6110 if (dwo_file)
6111 {
6112 sig_type = NULL;
6113 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6114 struct dwo_unit);
6115 dwo_tu->dwo_file = dwo_file;
6116 dwo_tu->signature = header.signature;
6117 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6118 dwo_tu->section = section;
6119 dwo_tu->sect_off = sect_off;
6120 dwo_tu->length = length;
6121 }
6122 else
6123 {
6124 /* N.B.: type_offset is not usable if this type uses a DWO file.
6125 The real type_offset is in the DWO file. */
6126 dwo_tu = NULL;
6127 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6128 struct signatured_type);
6129 sig_type->signature = header.signature;
6130 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6131 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6132 sig_type->per_cu.is_debug_types = 1;
6133 sig_type->per_cu.section = section;
6134 sig_type->per_cu.sect_off = sect_off;
6135 sig_type->per_cu.length = length;
6136 }
6137
6138 slot = htab_find_slot (types_htab.get (),
6139 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6140 INSERT);
6141 gdb_assert (slot != NULL);
6142 if (*slot != NULL)
6143 {
6144 sect_offset dup_sect_off;
6145
6146 if (dwo_file)
6147 {
6148 const struct dwo_unit *dup_tu
6149 = (const struct dwo_unit *) *slot;
6150
6151 dup_sect_off = dup_tu->sect_off;
6152 }
6153 else
6154 {
6155 const struct signatured_type *dup_tu
6156 = (const struct signatured_type *) *slot;
6157
6158 dup_sect_off = dup_tu->per_cu.sect_off;
6159 }
6160
6161 complaint (_("debug type entry at offset %s is duplicate to"
6162 " the entry at offset %s, signature %s"),
6163 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6164 hex_string (header.signature));
6165 }
6166 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6167
6168 if (dwarf_read_debug > 1)
6169 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6170 sect_offset_str (sect_off),
6171 hex_string (header.signature));
6172
6173 info_ptr += length;
6174 }
6175 }
6176
6177 /* Create the hash table of all entries in the .debug_types
6178 (or .debug_types.dwo) section(s).
6179 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6180 otherwise it is NULL.
6181
6182 The result is a pointer to the hash table or NULL if there are no types.
6183
6184 Note: This function processes DWO files only, not DWP files. */
6185
6186 static void
6187 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6188 struct dwo_file *dwo_file,
6189 gdb::array_view<dwarf2_section_info> type_sections,
6190 htab_up &types_htab)
6191 {
6192 for (dwarf2_section_info &section : type_sections)
6193 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6194 types_htab, rcuh_kind::TYPE);
6195 }
6196
6197 /* Create the hash table of all entries in the .debug_types section,
6198 and initialize all_type_units.
6199 The result is zero if there is an error (e.g. missing .debug_types section),
6200 otherwise non-zero. */
6201
6202 static int
6203 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6204 {
6205 htab_up types_htab;
6206
6207 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6208 &dwarf2_per_objfile->info, types_htab,
6209 rcuh_kind::COMPILE);
6210 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6211 dwarf2_per_objfile->types, types_htab);
6212 if (types_htab == NULL)
6213 {
6214 dwarf2_per_objfile->signatured_types = NULL;
6215 return 0;
6216 }
6217
6218 dwarf2_per_objfile->signatured_types = std::move (types_htab);
6219
6220 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6221 dwarf2_per_objfile->all_type_units.reserve
6222 (htab_elements (dwarf2_per_objfile->signatured_types.get ()));
6223
6224 htab_traverse_noresize (dwarf2_per_objfile->signatured_types.get (),
6225 add_signatured_type_cu_to_table,
6226 &dwarf2_per_objfile->all_type_units);
6227
6228 return 1;
6229 }
6230
6231 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6232 If SLOT is non-NULL, it is the entry to use in the hash table.
6233 Otherwise we find one. */
6234
6235 static struct signatured_type *
6236 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6237 void **slot)
6238 {
6239 struct objfile *objfile = dwarf2_per_objfile->objfile;
6240
6241 if (dwarf2_per_objfile->all_type_units.size ()
6242 == dwarf2_per_objfile->all_type_units.capacity ())
6243 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6244
6245 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6246 struct signatured_type);
6247
6248 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6249 sig_type->signature = sig;
6250 sig_type->per_cu.is_debug_types = 1;
6251 if (dwarf2_per_objfile->using_index)
6252 {
6253 sig_type->per_cu.v.quick =
6254 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6255 struct dwarf2_per_cu_quick_data);
6256 }
6257
6258 if (slot == NULL)
6259 {
6260 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6261 sig_type, INSERT);
6262 }
6263 gdb_assert (*slot == NULL);
6264 *slot = sig_type;
6265 /* The rest of sig_type must be filled in by the caller. */
6266 return sig_type;
6267 }
6268
6269 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6270 Fill in SIG_ENTRY with DWO_ENTRY. */
6271
6272 static void
6273 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6274 struct signatured_type *sig_entry,
6275 struct dwo_unit *dwo_entry)
6276 {
6277 /* Make sure we're not clobbering something we don't expect to. */
6278 gdb_assert (! sig_entry->per_cu.queued);
6279 gdb_assert (sig_entry->per_cu.cu == NULL);
6280 if (dwarf2_per_objfile->using_index)
6281 {
6282 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6283 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6284 }
6285 else
6286 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6287 gdb_assert (sig_entry->signature == dwo_entry->signature);
6288 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6289 gdb_assert (sig_entry->type_unit_group == NULL);
6290 gdb_assert (sig_entry->dwo_unit == NULL);
6291
6292 sig_entry->per_cu.section = dwo_entry->section;
6293 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6294 sig_entry->per_cu.length = dwo_entry->length;
6295 sig_entry->per_cu.reading_dwo_directly = 1;
6296 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6297 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6298 sig_entry->dwo_unit = dwo_entry;
6299 }
6300
6301 /* Subroutine of lookup_signatured_type.
6302 If we haven't read the TU yet, create the signatured_type data structure
6303 for a TU to be read in directly from a DWO file, bypassing the stub.
6304 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6305 using .gdb_index, then when reading a CU we want to stay in the DWO file
6306 containing that CU. Otherwise we could end up reading several other DWO
6307 files (due to comdat folding) to process the transitive closure of all the
6308 mentioned TUs, and that can be slow. The current DWO file will have every
6309 type signature that it needs.
6310 We only do this for .gdb_index because in the psymtab case we already have
6311 to read all the DWOs to build the type unit groups. */
6312
6313 static struct signatured_type *
6314 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6315 {
6316 struct dwarf2_per_objfile *dwarf2_per_objfile
6317 = cu->per_cu->dwarf2_per_objfile;
6318 struct dwo_file *dwo_file;
6319 struct dwo_unit find_dwo_entry, *dwo_entry;
6320 struct signatured_type find_sig_entry, *sig_entry;
6321 void **slot;
6322
6323 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6324
6325 /* If TU skeletons have been removed then we may not have read in any
6326 TUs yet. */
6327 if (dwarf2_per_objfile->signatured_types == NULL)
6328 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6329
6330 /* We only ever need to read in one copy of a signatured type.
6331 Use the global signatured_types array to do our own comdat-folding
6332 of types. If this is the first time we're reading this TU, and
6333 the TU has an entry in .gdb_index, replace the recorded data from
6334 .gdb_index with this TU. */
6335
6336 find_sig_entry.signature = sig;
6337 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6338 &find_sig_entry, INSERT);
6339 sig_entry = (struct signatured_type *) *slot;
6340
6341 /* We can get here with the TU already read, *or* in the process of being
6342 read. Don't reassign the global entry to point to this DWO if that's
6343 the case. Also note that if the TU is already being read, it may not
6344 have come from a DWO, the program may be a mix of Fission-compiled
6345 code and non-Fission-compiled code. */
6346
6347 /* Have we already tried to read this TU?
6348 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6349 needn't exist in the global table yet). */
6350 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6351 return sig_entry;
6352
6353 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6354 dwo_unit of the TU itself. */
6355 dwo_file = cu->dwo_unit->dwo_file;
6356
6357 /* Ok, this is the first time we're reading this TU. */
6358 if (dwo_file->tus == NULL)
6359 return NULL;
6360 find_dwo_entry.signature = sig;
6361 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6362 &find_dwo_entry);
6363 if (dwo_entry == NULL)
6364 return NULL;
6365
6366 /* If the global table doesn't have an entry for this TU, add one. */
6367 if (sig_entry == NULL)
6368 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6369
6370 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6371 sig_entry->per_cu.tu_read = 1;
6372 return sig_entry;
6373 }
6374
6375 /* Subroutine of lookup_signatured_type.
6376 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6377 then try the DWP file. If the TU stub (skeleton) has been removed then
6378 it won't be in .gdb_index. */
6379
6380 static struct signatured_type *
6381 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6382 {
6383 struct dwarf2_per_objfile *dwarf2_per_objfile
6384 = cu->per_cu->dwarf2_per_objfile;
6385 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
6386 struct dwo_unit *dwo_entry;
6387 struct signatured_type find_sig_entry, *sig_entry;
6388 void **slot;
6389
6390 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6391 gdb_assert (dwp_file != NULL);
6392
6393 /* If TU skeletons have been removed then we may not have read in any
6394 TUs yet. */
6395 if (dwarf2_per_objfile->signatured_types == NULL)
6396 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6397
6398 find_sig_entry.signature = sig;
6399 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6400 &find_sig_entry, INSERT);
6401 sig_entry = (struct signatured_type *) *slot;
6402
6403 /* Have we already tried to read this TU?
6404 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6405 needn't exist in the global table yet). */
6406 if (sig_entry != NULL)
6407 return sig_entry;
6408
6409 if (dwp_file->tus == NULL)
6410 return NULL;
6411 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
6412 sig, 1 /* is_debug_types */);
6413 if (dwo_entry == NULL)
6414 return NULL;
6415
6416 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6417 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6418
6419 return sig_entry;
6420 }
6421
6422 /* Lookup a signature based type for DW_FORM_ref_sig8.
6423 Returns NULL if signature SIG is not present in the table.
6424 It is up to the caller to complain about this. */
6425
6426 static struct signatured_type *
6427 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6428 {
6429 struct dwarf2_per_objfile *dwarf2_per_objfile
6430 = cu->per_cu->dwarf2_per_objfile;
6431
6432 if (cu->dwo_unit
6433 && dwarf2_per_objfile->using_index)
6434 {
6435 /* We're in a DWO/DWP file, and we're using .gdb_index.
6436 These cases require special processing. */
6437 if (get_dwp_file (dwarf2_per_objfile) == NULL)
6438 return lookup_dwo_signatured_type (cu, sig);
6439 else
6440 return lookup_dwp_signatured_type (cu, sig);
6441 }
6442 else
6443 {
6444 struct signatured_type find_entry, *entry;
6445
6446 if (dwarf2_per_objfile->signatured_types == NULL)
6447 return NULL;
6448 find_entry.signature = sig;
6449 entry = ((struct signatured_type *)
6450 htab_find (dwarf2_per_objfile->signatured_types.get (),
6451 &find_entry));
6452 return entry;
6453 }
6454 }
6455
6456 /* Return the address base of the compile unit, which, if exists, is stored
6457 either at the attribute DW_AT_GNU_addr_base, or DW_AT_addr_base. */
6458 static gdb::optional<ULONGEST>
6459 lookup_addr_base (struct die_info *comp_unit_die)
6460 {
6461 struct attribute *attr;
6462 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_addr_base);
6463 if (attr == nullptr)
6464 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_addr_base);
6465 if (attr == nullptr)
6466 return gdb::optional<ULONGEST> ();
6467 return DW_UNSND (attr);
6468 }
6469
6470 /* Return range lists base of the compile unit, which, if exists, is stored
6471 either at the attribute DW_AT_rnglists_base or DW_AT_GNU_ranges_base. */
6472 static ULONGEST
6473 lookup_ranges_base (struct die_info *comp_unit_die)
6474 {
6475 struct attribute *attr;
6476 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_rnglists_base);
6477 if (attr == nullptr)
6478 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_ranges_base);
6479 if (attr == nullptr)
6480 return 0;
6481 return DW_UNSND (attr);
6482 }
6483
6484 /* Low level DIE reading support. */
6485
6486 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6487
6488 static void
6489 init_cu_die_reader (struct die_reader_specs *reader,
6490 struct dwarf2_cu *cu,
6491 struct dwarf2_section_info *section,
6492 struct dwo_file *dwo_file,
6493 struct abbrev_table *abbrev_table)
6494 {
6495 gdb_assert (section->readin && section->buffer != NULL);
6496 reader->abfd = section->get_bfd_owner ();
6497 reader->cu = cu;
6498 reader->dwo_file = dwo_file;
6499 reader->die_section = section;
6500 reader->buffer = section->buffer;
6501 reader->buffer_end = section->buffer + section->size;
6502 reader->abbrev_table = abbrev_table;
6503 }
6504
6505 /* Subroutine of cutu_reader to simplify it.
6506 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6507 There's just a lot of work to do, and cutu_reader is big enough
6508 already.
6509
6510 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6511 from it to the DIE in the DWO. If NULL we are skipping the stub.
6512 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6513 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6514 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6515 STUB_COMP_DIR may be non-NULL.
6516 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6517 are filled in with the info of the DIE from the DWO file.
6518 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6519 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6520 kept around for at least as long as *RESULT_READER.
6521
6522 The result is non-zero if a valid (non-dummy) DIE was found. */
6523
6524 static int
6525 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6526 struct dwo_unit *dwo_unit,
6527 struct die_info *stub_comp_unit_die,
6528 const char *stub_comp_dir,
6529 struct die_reader_specs *result_reader,
6530 const gdb_byte **result_info_ptr,
6531 struct die_info **result_comp_unit_die,
6532 abbrev_table_up *result_dwo_abbrev_table)
6533 {
6534 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6535 struct objfile *objfile = dwarf2_per_objfile->objfile;
6536 struct dwarf2_cu *cu = this_cu->cu;
6537 bfd *abfd;
6538 const gdb_byte *begin_info_ptr, *info_ptr;
6539 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6540 int i,num_extra_attrs;
6541 struct dwarf2_section_info *dwo_abbrev_section;
6542 struct die_info *comp_unit_die;
6543
6544 /* At most one of these may be provided. */
6545 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6546
6547 /* These attributes aren't processed until later:
6548 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6549 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6550 referenced later. However, these attributes are found in the stub
6551 which we won't have later. In order to not impose this complication
6552 on the rest of the code, we read them here and copy them to the
6553 DWO CU/TU die. */
6554
6555 stmt_list = NULL;
6556 low_pc = NULL;
6557 high_pc = NULL;
6558 ranges = NULL;
6559 comp_dir = NULL;
6560
6561 if (stub_comp_unit_die != NULL)
6562 {
6563 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6564 DWO file. */
6565 if (! this_cu->is_debug_types)
6566 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6567 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6568 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6569 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6570 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6571
6572 cu->addr_base = lookup_addr_base (stub_comp_unit_die);
6573
6574 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6575 here (if needed). We need the value before we can process
6576 DW_AT_ranges. */
6577 cu->ranges_base = lookup_ranges_base (stub_comp_unit_die);
6578 }
6579 else if (stub_comp_dir != NULL)
6580 {
6581 /* Reconstruct the comp_dir attribute to simplify the code below. */
6582 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6583 comp_dir->name = DW_AT_comp_dir;
6584 comp_dir->form = DW_FORM_string;
6585 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6586 DW_STRING (comp_dir) = stub_comp_dir;
6587 }
6588
6589 /* Set up for reading the DWO CU/TU. */
6590 cu->dwo_unit = dwo_unit;
6591 dwarf2_section_info *section = dwo_unit->section;
6592 section->read (objfile);
6593 abfd = section->get_bfd_owner ();
6594 begin_info_ptr = info_ptr = (section->buffer
6595 + to_underlying (dwo_unit->sect_off));
6596 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6597
6598 if (this_cu->is_debug_types)
6599 {
6600 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6601
6602 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6603 &cu->header, section,
6604 dwo_abbrev_section,
6605 info_ptr, rcuh_kind::TYPE);
6606 /* This is not an assert because it can be caused by bad debug info. */
6607 if (sig_type->signature != cu->header.signature)
6608 {
6609 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6610 " TU at offset %s [in module %s]"),
6611 hex_string (sig_type->signature),
6612 hex_string (cu->header.signature),
6613 sect_offset_str (dwo_unit->sect_off),
6614 bfd_get_filename (abfd));
6615 }
6616 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6617 /* For DWOs coming from DWP files, we don't know the CU length
6618 nor the type's offset in the TU until now. */
6619 dwo_unit->length = cu->header.get_length ();
6620 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6621
6622 /* Establish the type offset that can be used to lookup the type.
6623 For DWO files, we don't know it until now. */
6624 sig_type->type_offset_in_section
6625 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6626 }
6627 else
6628 {
6629 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6630 &cu->header, section,
6631 dwo_abbrev_section,
6632 info_ptr, rcuh_kind::COMPILE);
6633 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6634 /* For DWOs coming from DWP files, we don't know the CU length
6635 until now. */
6636 dwo_unit->length = cu->header.get_length ();
6637 }
6638
6639 *result_dwo_abbrev_table
6640 = abbrev_table::read (objfile, dwo_abbrev_section,
6641 cu->header.abbrev_sect_off);
6642 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6643 result_dwo_abbrev_table->get ());
6644
6645 /* Read in the die, but leave space to copy over the attributes
6646 from the stub. This has the benefit of simplifying the rest of
6647 the code - all the work to maintain the illusion of a single
6648 DW_TAG_{compile,type}_unit DIE is done here. */
6649 num_extra_attrs = ((stmt_list != NULL)
6650 + (low_pc != NULL)
6651 + (high_pc != NULL)
6652 + (ranges != NULL)
6653 + (comp_dir != NULL));
6654 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6655 num_extra_attrs);
6656
6657 /* Copy over the attributes from the stub to the DIE we just read in. */
6658 comp_unit_die = *result_comp_unit_die;
6659 i = comp_unit_die->num_attrs;
6660 if (stmt_list != NULL)
6661 comp_unit_die->attrs[i++] = *stmt_list;
6662 if (low_pc != NULL)
6663 comp_unit_die->attrs[i++] = *low_pc;
6664 if (high_pc != NULL)
6665 comp_unit_die->attrs[i++] = *high_pc;
6666 if (ranges != NULL)
6667 comp_unit_die->attrs[i++] = *ranges;
6668 if (comp_dir != NULL)
6669 comp_unit_die->attrs[i++] = *comp_dir;
6670 comp_unit_die->num_attrs += num_extra_attrs;
6671
6672 if (dwarf_die_debug)
6673 {
6674 fprintf_unfiltered (gdb_stdlog,
6675 "Read die from %s@0x%x of %s:\n",
6676 section->get_name (),
6677 (unsigned) (begin_info_ptr - section->buffer),
6678 bfd_get_filename (abfd));
6679 dump_die (comp_unit_die, dwarf_die_debug);
6680 }
6681
6682 /* Skip dummy compilation units. */
6683 if (info_ptr >= begin_info_ptr + dwo_unit->length
6684 || peek_abbrev_code (abfd, info_ptr) == 0)
6685 return 0;
6686
6687 *result_info_ptr = info_ptr;
6688 return 1;
6689 }
6690
6691 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6692 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6693 signature is part of the header. */
6694 static gdb::optional<ULONGEST>
6695 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6696 {
6697 if (cu->header.version >= 5)
6698 return cu->header.signature;
6699 struct attribute *attr;
6700 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6701 if (attr == nullptr)
6702 return gdb::optional<ULONGEST> ();
6703 return DW_UNSND (attr);
6704 }
6705
6706 /* Subroutine of cutu_reader to simplify it.
6707 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6708 Returns NULL if the specified DWO unit cannot be found. */
6709
6710 static struct dwo_unit *
6711 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
6712 struct die_info *comp_unit_die,
6713 const char *dwo_name)
6714 {
6715 struct dwarf2_cu *cu = this_cu->cu;
6716 struct dwo_unit *dwo_unit;
6717 const char *comp_dir;
6718
6719 gdb_assert (cu != NULL);
6720
6721 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6722 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6723 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6724
6725 if (this_cu->is_debug_types)
6726 {
6727 struct signatured_type *sig_type;
6728
6729 /* Since this_cu is the first member of struct signatured_type,
6730 we can go from a pointer to one to a pointer to the other. */
6731 sig_type = (struct signatured_type *) this_cu;
6732 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6733 }
6734 else
6735 {
6736 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6737 if (!signature.has_value ())
6738 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6739 " [in module %s]"),
6740 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
6741 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
6742 *signature);
6743 }
6744
6745 return dwo_unit;
6746 }
6747
6748 /* Subroutine of cutu_reader to simplify it.
6749 See it for a description of the parameters.
6750 Read a TU directly from a DWO file, bypassing the stub. */
6751
6752 void
6753 cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6754 int use_existing_cu)
6755 {
6756 struct signatured_type *sig_type;
6757
6758 /* Verify we can do the following downcast, and that we have the
6759 data we need. */
6760 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6761 sig_type = (struct signatured_type *) this_cu;
6762 gdb_assert (sig_type->dwo_unit != NULL);
6763
6764 if (use_existing_cu && this_cu->cu != NULL)
6765 {
6766 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6767 /* There's no need to do the rereading_dwo_cu handling that
6768 cutu_reader does since we don't read the stub. */
6769 }
6770 else
6771 {
6772 /* If !use_existing_cu, this_cu->cu must be NULL. */
6773 gdb_assert (this_cu->cu == NULL);
6774 m_new_cu.reset (new dwarf2_cu (this_cu));
6775 }
6776
6777 /* A future optimization, if needed, would be to use an existing
6778 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6779 could share abbrev tables. */
6780
6781 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
6782 NULL /* stub_comp_unit_die */,
6783 sig_type->dwo_unit->dwo_file->comp_dir,
6784 this, &info_ptr,
6785 &comp_unit_die,
6786 &m_dwo_abbrev_table) == 0)
6787 {
6788 /* Dummy die. */
6789 dummy_p = true;
6790 }
6791 }
6792
6793 /* Initialize a CU (or TU) and read its DIEs.
6794 If the CU defers to a DWO file, read the DWO file as well.
6795
6796 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6797 Otherwise the table specified in the comp unit header is read in and used.
6798 This is an optimization for when we already have the abbrev table.
6799
6800 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6801 Otherwise, a new CU is allocated with xmalloc. */
6802
6803 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6804 struct abbrev_table *abbrev_table,
6805 int use_existing_cu,
6806 bool skip_partial)
6807 : die_reader_specs {},
6808 m_this_cu (this_cu)
6809 {
6810 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6811 struct objfile *objfile = dwarf2_per_objfile->objfile;
6812 struct dwarf2_section_info *section = this_cu->section;
6813 bfd *abfd = section->get_bfd_owner ();
6814 struct dwarf2_cu *cu;
6815 const gdb_byte *begin_info_ptr;
6816 struct signatured_type *sig_type = NULL;
6817 struct dwarf2_section_info *abbrev_section;
6818 /* Non-zero if CU currently points to a DWO file and we need to
6819 reread it. When this happens we need to reread the skeleton die
6820 before we can reread the DWO file (this only applies to CUs, not TUs). */
6821 int rereading_dwo_cu = 0;
6822
6823 if (dwarf_die_debug)
6824 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6825 this_cu->is_debug_types ? "type" : "comp",
6826 sect_offset_str (this_cu->sect_off));
6827
6828 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6829 file (instead of going through the stub), short-circuit all of this. */
6830 if (this_cu->reading_dwo_directly)
6831 {
6832 /* Narrow down the scope of possibilities to have to understand. */
6833 gdb_assert (this_cu->is_debug_types);
6834 gdb_assert (abbrev_table == NULL);
6835 init_tu_and_read_dwo_dies (this_cu, use_existing_cu);
6836 return;
6837 }
6838
6839 /* This is cheap if the section is already read in. */
6840 section->read (objfile);
6841
6842 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6843
6844 abbrev_section = get_abbrev_section_for_cu (this_cu);
6845
6846 if (use_existing_cu && this_cu->cu != NULL)
6847 {
6848 cu = this_cu->cu;
6849 /* If this CU is from a DWO file we need to start over, we need to
6850 refetch the attributes from the skeleton CU.
6851 This could be optimized by retrieving those attributes from when we
6852 were here the first time: the previous comp_unit_die was stored in
6853 comp_unit_obstack. But there's no data yet that we need this
6854 optimization. */
6855 if (cu->dwo_unit != NULL)
6856 rereading_dwo_cu = 1;
6857 }
6858 else
6859 {
6860 /* If !use_existing_cu, this_cu->cu must be NULL. */
6861 gdb_assert (this_cu->cu == NULL);
6862 m_new_cu.reset (new dwarf2_cu (this_cu));
6863 cu = m_new_cu.get ();
6864 }
6865
6866 /* Get the header. */
6867 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
6868 {
6869 /* We already have the header, there's no need to read it in again. */
6870 info_ptr += to_underlying (cu->header.first_die_cu_offset);
6871 }
6872 else
6873 {
6874 if (this_cu->is_debug_types)
6875 {
6876 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6877 &cu->header, section,
6878 abbrev_section, info_ptr,
6879 rcuh_kind::TYPE);
6880
6881 /* Since per_cu is the first member of struct signatured_type,
6882 we can go from a pointer to one to a pointer to the other. */
6883 sig_type = (struct signatured_type *) this_cu;
6884 gdb_assert (sig_type->signature == cu->header.signature);
6885 gdb_assert (sig_type->type_offset_in_tu
6886 == cu->header.type_cu_offset_in_tu);
6887 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6888
6889 /* LENGTH has not been set yet for type units if we're
6890 using .gdb_index. */
6891 this_cu->length = cu->header.get_length ();
6892
6893 /* Establish the type offset that can be used to lookup the type. */
6894 sig_type->type_offset_in_section =
6895 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
6896
6897 this_cu->dwarf_version = cu->header.version;
6898 }
6899 else
6900 {
6901 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6902 &cu->header, section,
6903 abbrev_section,
6904 info_ptr,
6905 rcuh_kind::COMPILE);
6906
6907 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6908 gdb_assert (this_cu->length == cu->header.get_length ());
6909 this_cu->dwarf_version = cu->header.version;
6910 }
6911 }
6912
6913 /* Skip dummy compilation units. */
6914 if (info_ptr >= begin_info_ptr + this_cu->length
6915 || peek_abbrev_code (abfd, info_ptr) == 0)
6916 {
6917 dummy_p = true;
6918 return;
6919 }
6920
6921 /* If we don't have them yet, read the abbrevs for this compilation unit.
6922 And if we need to read them now, make sure they're freed when we're
6923 done. */
6924 if (abbrev_table != NULL)
6925 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6926 else
6927 {
6928 m_abbrev_table_holder
6929 = abbrev_table::read (objfile, abbrev_section,
6930 cu->header.abbrev_sect_off);
6931 abbrev_table = m_abbrev_table_holder.get ();
6932 }
6933
6934 /* Read the top level CU/TU die. */
6935 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
6936 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6937
6938 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
6939 {
6940 dummy_p = true;
6941 return;
6942 }
6943
6944 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6945 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6946 table from the DWO file and pass the ownership over to us. It will be
6947 referenced from READER, so we must make sure to free it after we're done
6948 with READER.
6949
6950 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6951 DWO CU, that this test will fail (the attribute will not be present). */
6952 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6953 if (dwo_name != nullptr)
6954 {
6955 struct dwo_unit *dwo_unit;
6956 struct die_info *dwo_comp_unit_die;
6957
6958 if (comp_unit_die->has_children)
6959 {
6960 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
6961 " has children (offset %s) [in module %s]"),
6962 sect_offset_str (this_cu->sect_off),
6963 bfd_get_filename (abfd));
6964 }
6965 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
6966 if (dwo_unit != NULL)
6967 {
6968 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
6969 comp_unit_die, NULL,
6970 this, &info_ptr,
6971 &dwo_comp_unit_die,
6972 &m_dwo_abbrev_table) == 0)
6973 {
6974 /* Dummy die. */
6975 dummy_p = true;
6976 return;
6977 }
6978 comp_unit_die = dwo_comp_unit_die;
6979 }
6980 else
6981 {
6982 /* Yikes, we couldn't find the rest of the DIE, we only have
6983 the stub. A complaint has already been logged. There's
6984 not much more we can do except pass on the stub DIE to
6985 die_reader_func. We don't want to throw an error on bad
6986 debug info. */
6987 }
6988 }
6989 }
6990
6991 void
6992 cutu_reader::keep ()
6993 {
6994 /* Done, clean up. */
6995 gdb_assert (!dummy_p);
6996 if (m_new_cu != NULL)
6997 {
6998 struct dwarf2_per_objfile *dwarf2_per_objfile
6999 = m_this_cu->dwarf2_per_objfile;
7000 /* Link this CU into read_in_chain. */
7001 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7002 dwarf2_per_objfile->read_in_chain = m_this_cu;
7003 /* The chain owns it now. */
7004 m_new_cu.release ();
7005 }
7006 }
7007
7008 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7009 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7010 assumed to have already done the lookup to find the DWO file).
7011
7012 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7013 THIS_CU->is_debug_types, but nothing else.
7014
7015 We fill in THIS_CU->length.
7016
7017 THIS_CU->cu is always freed when done.
7018 This is done in order to not leave THIS_CU->cu in a state where we have
7019 to care whether it refers to the "main" CU or the DWO CU.
7020
7021 When parent_cu is passed, it is used to provide a default value for
7022 str_offsets_base and addr_base from the parent. */
7023
7024 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7025 struct dwarf2_cu *parent_cu,
7026 struct dwo_file *dwo_file)
7027 : die_reader_specs {},
7028 m_this_cu (this_cu)
7029 {
7030 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7031 struct objfile *objfile = dwarf2_per_objfile->objfile;
7032 struct dwarf2_section_info *section = this_cu->section;
7033 bfd *abfd = section->get_bfd_owner ();
7034 struct dwarf2_section_info *abbrev_section;
7035 const gdb_byte *begin_info_ptr, *info_ptr;
7036
7037 if (dwarf_die_debug)
7038 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7039 this_cu->is_debug_types ? "type" : "comp",
7040 sect_offset_str (this_cu->sect_off));
7041
7042 gdb_assert (this_cu->cu == NULL);
7043
7044 abbrev_section = (dwo_file != NULL
7045 ? &dwo_file->sections.abbrev
7046 : get_abbrev_section_for_cu (this_cu));
7047
7048 /* This is cheap if the section is already read in. */
7049 section->read (objfile);
7050
7051 m_new_cu.reset (new dwarf2_cu (this_cu));
7052
7053 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7054 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7055 &m_new_cu->header, section,
7056 abbrev_section, info_ptr,
7057 (this_cu->is_debug_types
7058 ? rcuh_kind::TYPE
7059 : rcuh_kind::COMPILE));
7060
7061 if (parent_cu != nullptr)
7062 {
7063 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7064 m_new_cu->addr_base = parent_cu->addr_base;
7065 }
7066 this_cu->length = m_new_cu->header.get_length ();
7067
7068 /* Skip dummy compilation units. */
7069 if (info_ptr >= begin_info_ptr + this_cu->length
7070 || peek_abbrev_code (abfd, info_ptr) == 0)
7071 {
7072 dummy_p = true;
7073 return;
7074 }
7075
7076 m_abbrev_table_holder
7077 = abbrev_table::read (objfile, abbrev_section,
7078 m_new_cu->header.abbrev_sect_off);
7079
7080 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7081 m_abbrev_table_holder.get ());
7082 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7083 }
7084
7085 \f
7086 /* Type Unit Groups.
7087
7088 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7089 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7090 so that all types coming from the same compilation (.o file) are grouped
7091 together. A future step could be to put the types in the same symtab as
7092 the CU the types ultimately came from. */
7093
7094 static hashval_t
7095 hash_type_unit_group (const void *item)
7096 {
7097 const struct type_unit_group *tu_group
7098 = (const struct type_unit_group *) item;
7099
7100 return hash_stmt_list_entry (&tu_group->hash);
7101 }
7102
7103 static int
7104 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7105 {
7106 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7107 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7108
7109 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7110 }
7111
7112 /* Allocate a hash table for type unit groups. */
7113
7114 static htab_up
7115 allocate_type_unit_groups_table ()
7116 {
7117 return htab_up (htab_create_alloc (3,
7118 hash_type_unit_group,
7119 eq_type_unit_group,
7120 NULL, xcalloc, xfree));
7121 }
7122
7123 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7124 partial symtabs. We combine several TUs per psymtab to not let the size
7125 of any one psymtab grow too big. */
7126 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7127 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7128
7129 /* Helper routine for get_type_unit_group.
7130 Create the type_unit_group object used to hold one or more TUs. */
7131
7132 static struct type_unit_group *
7133 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7134 {
7135 struct dwarf2_per_objfile *dwarf2_per_objfile
7136 = cu->per_cu->dwarf2_per_objfile;
7137 struct objfile *objfile = dwarf2_per_objfile->objfile;
7138 struct dwarf2_per_cu_data *per_cu;
7139 struct type_unit_group *tu_group;
7140
7141 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7142 struct type_unit_group);
7143 per_cu = &tu_group->per_cu;
7144 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7145
7146 if (dwarf2_per_objfile->using_index)
7147 {
7148 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7149 struct dwarf2_per_cu_quick_data);
7150 }
7151 else
7152 {
7153 unsigned int line_offset = to_underlying (line_offset_struct);
7154 dwarf2_psymtab *pst;
7155 std::string name;
7156
7157 /* Give the symtab a useful name for debug purposes. */
7158 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7159 name = string_printf ("<type_units_%d>",
7160 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7161 else
7162 name = string_printf ("<type_units_at_0x%x>", line_offset);
7163
7164 pst = create_partial_symtab (per_cu, name.c_str ());
7165 pst->anonymous = true;
7166 }
7167
7168 tu_group->hash.dwo_unit = cu->dwo_unit;
7169 tu_group->hash.line_sect_off = line_offset_struct;
7170
7171 return tu_group;
7172 }
7173
7174 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7175 STMT_LIST is a DW_AT_stmt_list attribute. */
7176
7177 static struct type_unit_group *
7178 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7179 {
7180 struct dwarf2_per_objfile *dwarf2_per_objfile
7181 = cu->per_cu->dwarf2_per_objfile;
7182 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7183 struct type_unit_group *tu_group;
7184 void **slot;
7185 unsigned int line_offset;
7186 struct type_unit_group type_unit_group_for_lookup;
7187
7188 if (dwarf2_per_objfile->type_unit_groups == NULL)
7189 dwarf2_per_objfile->type_unit_groups = allocate_type_unit_groups_table ();
7190
7191 /* Do we need to create a new group, or can we use an existing one? */
7192
7193 if (stmt_list)
7194 {
7195 line_offset = DW_UNSND (stmt_list);
7196 ++tu_stats->nr_symtab_sharers;
7197 }
7198 else
7199 {
7200 /* Ugh, no stmt_list. Rare, but we have to handle it.
7201 We can do various things here like create one group per TU or
7202 spread them over multiple groups to split up the expansion work.
7203 To avoid worst case scenarios (too many groups or too large groups)
7204 we, umm, group them in bunches. */
7205 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7206 | (tu_stats->nr_stmt_less_type_units
7207 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7208 ++tu_stats->nr_stmt_less_type_units;
7209 }
7210
7211 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7212 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7213 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups.get (),
7214 &type_unit_group_for_lookup, INSERT);
7215 if (*slot != NULL)
7216 {
7217 tu_group = (struct type_unit_group *) *slot;
7218 gdb_assert (tu_group != NULL);
7219 }
7220 else
7221 {
7222 sect_offset line_offset_struct = (sect_offset) line_offset;
7223 tu_group = create_type_unit_group (cu, line_offset_struct);
7224 *slot = tu_group;
7225 ++tu_stats->nr_symtabs;
7226 }
7227
7228 return tu_group;
7229 }
7230 \f
7231 /* Partial symbol tables. */
7232
7233 /* Create a psymtab named NAME and assign it to PER_CU.
7234
7235 The caller must fill in the following details:
7236 dirname, textlow, texthigh. */
7237
7238 static dwarf2_psymtab *
7239 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7240 {
7241 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
7242 dwarf2_psymtab *pst;
7243
7244 pst = new dwarf2_psymtab (name, objfile, 0);
7245
7246 pst->psymtabs_addrmap_supported = true;
7247
7248 /* This is the glue that links PST into GDB's symbol API. */
7249 pst->per_cu_data = per_cu;
7250 per_cu->v.psymtab = pst;
7251
7252 return pst;
7253 }
7254
7255 /* DIE reader function for process_psymtab_comp_unit. */
7256
7257 static void
7258 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7259 const gdb_byte *info_ptr,
7260 struct die_info *comp_unit_die,
7261 enum language pretend_language)
7262 {
7263 struct dwarf2_cu *cu = reader->cu;
7264 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
7265 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7266 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7267 CORE_ADDR baseaddr;
7268 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7269 dwarf2_psymtab *pst;
7270 enum pc_bounds_kind cu_bounds_kind;
7271 const char *filename;
7272
7273 gdb_assert (! per_cu->is_debug_types);
7274
7275 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7276
7277 /* Allocate a new partial symbol table structure. */
7278 gdb::unique_xmalloc_ptr<char> debug_filename;
7279 static const char artificial[] = "<artificial>";
7280 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7281 if (filename == NULL)
7282 filename = "";
7283 else if (strcmp (filename, artificial) == 0)
7284 {
7285 debug_filename.reset (concat (artificial, "@",
7286 sect_offset_str (per_cu->sect_off),
7287 (char *) NULL));
7288 filename = debug_filename.get ();
7289 }
7290
7291 pst = create_partial_symtab (per_cu, filename);
7292
7293 /* This must be done before calling dwarf2_build_include_psymtabs. */
7294 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7295
7296 baseaddr = objfile->text_section_offset ();
7297
7298 dwarf2_find_base_address (comp_unit_die, cu);
7299
7300 /* Possibly set the default values of LOWPC and HIGHPC from
7301 `DW_AT_ranges'. */
7302 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7303 &best_highpc, cu, pst);
7304 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7305 {
7306 CORE_ADDR low
7307 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7308 - baseaddr);
7309 CORE_ADDR high
7310 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7311 - baseaddr - 1);
7312 /* Store the contiguous range if it is not empty; it can be
7313 empty for CUs with no code. */
7314 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7315 low, high, pst);
7316 }
7317
7318 /* Check if comp unit has_children.
7319 If so, read the rest of the partial symbols from this comp unit.
7320 If not, there's no more debug_info for this comp unit. */
7321 if (comp_unit_die->has_children)
7322 {
7323 struct partial_die_info *first_die;
7324 CORE_ADDR lowpc, highpc;
7325
7326 lowpc = ((CORE_ADDR) -1);
7327 highpc = ((CORE_ADDR) 0);
7328
7329 first_die = load_partial_dies (reader, info_ptr, 1);
7330
7331 scan_partial_symbols (first_die, &lowpc, &highpc,
7332 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7333
7334 /* If we didn't find a lowpc, set it to highpc to avoid
7335 complaints from `maint check'. */
7336 if (lowpc == ((CORE_ADDR) -1))
7337 lowpc = highpc;
7338
7339 /* If the compilation unit didn't have an explicit address range,
7340 then use the information extracted from its child dies. */
7341 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7342 {
7343 best_lowpc = lowpc;
7344 best_highpc = highpc;
7345 }
7346 }
7347 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7348 best_lowpc + baseaddr)
7349 - baseaddr);
7350 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7351 best_highpc + baseaddr)
7352 - baseaddr);
7353
7354 end_psymtab_common (objfile, pst);
7355
7356 if (!cu->per_cu->imported_symtabs_empty ())
7357 {
7358 int i;
7359 int len = cu->per_cu->imported_symtabs_size ();
7360
7361 /* Fill in 'dependencies' here; we fill in 'users' in a
7362 post-pass. */
7363 pst->number_of_dependencies = len;
7364 pst->dependencies
7365 = objfile->partial_symtabs->allocate_dependencies (len);
7366 for (i = 0; i < len; ++i)
7367 {
7368 pst->dependencies[i]
7369 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7370 }
7371
7372 cu->per_cu->imported_symtabs_free ();
7373 }
7374
7375 /* Get the list of files included in the current compilation unit,
7376 and build a psymtab for each of them. */
7377 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7378
7379 if (dwarf_read_debug)
7380 fprintf_unfiltered (gdb_stdlog,
7381 "Psymtab for %s unit @%s: %s - %s"
7382 ", %d global, %d static syms\n",
7383 per_cu->is_debug_types ? "type" : "comp",
7384 sect_offset_str (per_cu->sect_off),
7385 paddress (gdbarch, pst->text_low (objfile)),
7386 paddress (gdbarch, pst->text_high (objfile)),
7387 pst->n_global_syms, pst->n_static_syms);
7388 }
7389
7390 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7391 Process compilation unit THIS_CU for a psymtab. */
7392
7393 static void
7394 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
7395 bool want_partial_unit,
7396 enum language pretend_language)
7397 {
7398 /* If this compilation unit was already read in, free the
7399 cached copy in order to read it in again. This is
7400 necessary because we skipped some symbols when we first
7401 read in the compilation unit (see load_partial_dies).
7402 This problem could be avoided, but the benefit is unclear. */
7403 if (this_cu->cu != NULL)
7404 free_one_cached_comp_unit (this_cu);
7405
7406 cutu_reader reader (this_cu, NULL, 0, false);
7407
7408 switch (reader.comp_unit_die->tag)
7409 {
7410 case DW_TAG_compile_unit:
7411 this_cu->unit_type = DW_UT_compile;
7412 break;
7413 case DW_TAG_partial_unit:
7414 this_cu->unit_type = DW_UT_partial;
7415 break;
7416 default:
7417 abort ();
7418 }
7419
7420 if (reader.dummy_p)
7421 {
7422 /* Nothing. */
7423 }
7424 else if (this_cu->is_debug_types)
7425 build_type_psymtabs_reader (&reader, reader.info_ptr,
7426 reader.comp_unit_die);
7427 else if (want_partial_unit
7428 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7429 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7430 reader.comp_unit_die,
7431 pretend_language);
7432
7433 this_cu->lang = this_cu->cu->language;
7434
7435 /* Age out any secondary CUs. */
7436 age_cached_comp_units (this_cu->dwarf2_per_objfile);
7437 }
7438
7439 /* Reader function for build_type_psymtabs. */
7440
7441 static void
7442 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7443 const gdb_byte *info_ptr,
7444 struct die_info *type_unit_die)
7445 {
7446 struct dwarf2_per_objfile *dwarf2_per_objfile
7447 = reader->cu->per_cu->dwarf2_per_objfile;
7448 struct objfile *objfile = dwarf2_per_objfile->objfile;
7449 struct dwarf2_cu *cu = reader->cu;
7450 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7451 struct signatured_type *sig_type;
7452 struct type_unit_group *tu_group;
7453 struct attribute *attr;
7454 struct partial_die_info *first_die;
7455 CORE_ADDR lowpc, highpc;
7456 dwarf2_psymtab *pst;
7457
7458 gdb_assert (per_cu->is_debug_types);
7459 sig_type = (struct signatured_type *) per_cu;
7460
7461 if (! type_unit_die->has_children)
7462 return;
7463
7464 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
7465 tu_group = get_type_unit_group (cu, attr);
7466
7467 if (tu_group->tus == nullptr)
7468 tu_group->tus = new std::vector<signatured_type *>;
7469 tu_group->tus->push_back (sig_type);
7470
7471 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7472 pst = create_partial_symtab (per_cu, "");
7473 pst->anonymous = true;
7474
7475 first_die = load_partial_dies (reader, info_ptr, 1);
7476
7477 lowpc = (CORE_ADDR) -1;
7478 highpc = (CORE_ADDR) 0;
7479 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7480
7481 end_psymtab_common (objfile, pst);
7482 }
7483
7484 /* Struct used to sort TUs by their abbreviation table offset. */
7485
7486 struct tu_abbrev_offset
7487 {
7488 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7489 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7490 {}
7491
7492 signatured_type *sig_type;
7493 sect_offset abbrev_offset;
7494 };
7495
7496 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7497
7498 static bool
7499 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7500 const struct tu_abbrev_offset &b)
7501 {
7502 return a.abbrev_offset < b.abbrev_offset;
7503 }
7504
7505 /* Efficiently read all the type units.
7506 This does the bulk of the work for build_type_psymtabs.
7507
7508 The efficiency is because we sort TUs by the abbrev table they use and
7509 only read each abbrev table once. In one program there are 200K TUs
7510 sharing 8K abbrev tables.
7511
7512 The main purpose of this function is to support building the
7513 dwarf2_per_objfile->type_unit_groups table.
7514 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7515 can collapse the search space by grouping them by stmt_list.
7516 The savings can be significant, in the same program from above the 200K TUs
7517 share 8K stmt_list tables.
7518
7519 FUNC is expected to call get_type_unit_group, which will create the
7520 struct type_unit_group if necessary and add it to
7521 dwarf2_per_objfile->type_unit_groups. */
7522
7523 static void
7524 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
7525 {
7526 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7527 abbrev_table_up abbrev_table;
7528 sect_offset abbrev_offset;
7529
7530 /* It's up to the caller to not call us multiple times. */
7531 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7532
7533 if (dwarf2_per_objfile->all_type_units.empty ())
7534 return;
7535
7536 /* TUs typically share abbrev tables, and there can be way more TUs than
7537 abbrev tables. Sort by abbrev table to reduce the number of times we
7538 read each abbrev table in.
7539 Alternatives are to punt or to maintain a cache of abbrev tables.
7540 This is simpler and efficient enough for now.
7541
7542 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7543 symtab to use). Typically TUs with the same abbrev offset have the same
7544 stmt_list value too so in practice this should work well.
7545
7546 The basic algorithm here is:
7547
7548 sort TUs by abbrev table
7549 for each TU with same abbrev table:
7550 read abbrev table if first user
7551 read TU top level DIE
7552 [IWBN if DWO skeletons had DW_AT_stmt_list]
7553 call FUNC */
7554
7555 if (dwarf_read_debug)
7556 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7557
7558 /* Sort in a separate table to maintain the order of all_type_units
7559 for .gdb_index: TU indices directly index all_type_units. */
7560 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7561 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
7562
7563 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
7564 sorted_by_abbrev.emplace_back
7565 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7566 sig_type->per_cu.section,
7567 sig_type->per_cu.sect_off));
7568
7569 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7570 sort_tu_by_abbrev_offset);
7571
7572 abbrev_offset = (sect_offset) ~(unsigned) 0;
7573
7574 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7575 {
7576 /* Switch to the next abbrev table if necessary. */
7577 if (abbrev_table == NULL
7578 || tu.abbrev_offset != abbrev_offset)
7579 {
7580 abbrev_offset = tu.abbrev_offset;
7581 abbrev_table =
7582 abbrev_table::read (dwarf2_per_objfile->objfile,
7583 &dwarf2_per_objfile->abbrev,
7584 abbrev_offset);
7585 ++tu_stats->nr_uniq_abbrev_tables;
7586 }
7587
7588 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
7589 0, false);
7590 if (!reader.dummy_p)
7591 build_type_psymtabs_reader (&reader, reader.info_ptr,
7592 reader.comp_unit_die);
7593 }
7594 }
7595
7596 /* Print collected type unit statistics. */
7597
7598 static void
7599 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
7600 {
7601 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7602
7603 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7604 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7605 dwarf2_per_objfile->all_type_units.size ());
7606 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7607 tu_stats->nr_uniq_abbrev_tables);
7608 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7609 tu_stats->nr_symtabs);
7610 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7611 tu_stats->nr_symtab_sharers);
7612 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7613 tu_stats->nr_stmt_less_type_units);
7614 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7615 tu_stats->nr_all_type_units_reallocs);
7616 }
7617
7618 /* Traversal function for build_type_psymtabs. */
7619
7620 static int
7621 build_type_psymtab_dependencies (void **slot, void *info)
7622 {
7623 struct dwarf2_per_objfile *dwarf2_per_objfile
7624 = (struct dwarf2_per_objfile *) info;
7625 struct objfile *objfile = dwarf2_per_objfile->objfile;
7626 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7627 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7628 dwarf2_psymtab *pst = per_cu->v.psymtab;
7629 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7630 int i;
7631
7632 gdb_assert (len > 0);
7633 gdb_assert (per_cu->type_unit_group_p ());
7634
7635 pst->number_of_dependencies = len;
7636 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
7637 for (i = 0; i < len; ++i)
7638 {
7639 struct signatured_type *iter = tu_group->tus->at (i);
7640 gdb_assert (iter->per_cu.is_debug_types);
7641 pst->dependencies[i] = iter->per_cu.v.psymtab;
7642 iter->type_unit_group = tu_group;
7643 }
7644
7645 delete tu_group->tus;
7646 tu_group->tus = nullptr;
7647
7648 return 1;
7649 }
7650
7651 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7652 Build partial symbol tables for the .debug_types comp-units. */
7653
7654 static void
7655 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
7656 {
7657 if (! create_all_type_units (dwarf2_per_objfile))
7658 return;
7659
7660 build_type_psymtabs_1 (dwarf2_per_objfile);
7661 }
7662
7663 /* Traversal function for process_skeletonless_type_unit.
7664 Read a TU in a DWO file and build partial symbols for it. */
7665
7666 static int
7667 process_skeletonless_type_unit (void **slot, void *info)
7668 {
7669 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7670 struct dwarf2_per_objfile *dwarf2_per_objfile
7671 = (struct dwarf2_per_objfile *) info;
7672 struct signatured_type find_entry, *entry;
7673
7674 /* If this TU doesn't exist in the global table, add it and read it in. */
7675
7676 if (dwarf2_per_objfile->signatured_types == NULL)
7677 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
7678
7679 find_entry.signature = dwo_unit->signature;
7680 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
7681 &find_entry, INSERT);
7682 /* If we've already seen this type there's nothing to do. What's happening
7683 is we're doing our own version of comdat-folding here. */
7684 if (*slot != NULL)
7685 return 1;
7686
7687 /* This does the job that create_all_type_units would have done for
7688 this TU. */
7689 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7690 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
7691 *slot = entry;
7692
7693 /* This does the job that build_type_psymtabs_1 would have done. */
7694 cutu_reader reader (&entry->per_cu, NULL, 0, false);
7695 if (!reader.dummy_p)
7696 build_type_psymtabs_reader (&reader, reader.info_ptr,
7697 reader.comp_unit_die);
7698
7699 return 1;
7700 }
7701
7702 /* Traversal function for process_skeletonless_type_units. */
7703
7704 static int
7705 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7706 {
7707 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7708
7709 if (dwo_file->tus != NULL)
7710 htab_traverse_noresize (dwo_file->tus.get (),
7711 process_skeletonless_type_unit, info);
7712
7713 return 1;
7714 }
7715
7716 /* Scan all TUs of DWO files, verifying we've processed them.
7717 This is needed in case a TU was emitted without its skeleton.
7718 Note: This can't be done until we know what all the DWO files are. */
7719
7720 static void
7721 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7722 {
7723 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7724 if (get_dwp_file (dwarf2_per_objfile) == NULL
7725 && dwarf2_per_objfile->dwo_files != NULL)
7726 {
7727 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
7728 process_dwo_file_for_skeletonless_type_units,
7729 dwarf2_per_objfile);
7730 }
7731 }
7732
7733 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7734
7735 static void
7736 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
7737 {
7738 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
7739 {
7740 dwarf2_psymtab *pst = per_cu->v.psymtab;
7741
7742 if (pst == NULL)
7743 continue;
7744
7745 for (int j = 0; j < pst->number_of_dependencies; ++j)
7746 {
7747 /* Set the 'user' field only if it is not already set. */
7748 if (pst->dependencies[j]->user == NULL)
7749 pst->dependencies[j]->user = pst;
7750 }
7751 }
7752 }
7753
7754 /* Build the partial symbol table by doing a quick pass through the
7755 .debug_info and .debug_abbrev sections. */
7756
7757 static void
7758 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
7759 {
7760 struct objfile *objfile = dwarf2_per_objfile->objfile;
7761
7762 if (dwarf_read_debug)
7763 {
7764 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
7765 objfile_name (objfile));
7766 }
7767
7768 scoped_restore restore_reading_psyms
7769 = make_scoped_restore (&dwarf2_per_objfile->reading_partial_symbols,
7770 true);
7771
7772 dwarf2_per_objfile->info.read (objfile);
7773
7774 /* Any cached compilation units will be linked by the per-objfile
7775 read_in_chain. Make sure to free them when we're done. */
7776 free_cached_comp_units freer (dwarf2_per_objfile);
7777
7778 build_type_psymtabs (dwarf2_per_objfile);
7779
7780 create_all_comp_units (dwarf2_per_objfile);
7781
7782 /* Create a temporary address map on a temporary obstack. We later
7783 copy this to the final obstack. */
7784 auto_obstack temp_obstack;
7785
7786 scoped_restore save_psymtabs_addrmap
7787 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
7788 addrmap_create_mutable (&temp_obstack));
7789
7790 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
7791 process_psymtab_comp_unit (per_cu, false, language_minimal);
7792
7793 /* This has to wait until we read the CUs, we need the list of DWOs. */
7794 process_skeletonless_type_units (dwarf2_per_objfile);
7795
7796 /* Now that all TUs have been processed we can fill in the dependencies. */
7797 if (dwarf2_per_objfile->type_unit_groups != NULL)
7798 {
7799 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups.get (),
7800 build_type_psymtab_dependencies, dwarf2_per_objfile);
7801 }
7802
7803 if (dwarf_read_debug)
7804 print_tu_stats (dwarf2_per_objfile);
7805
7806 set_partial_user (dwarf2_per_objfile);
7807
7808 objfile->partial_symtabs->psymtabs_addrmap
7809 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
7810 objfile->partial_symtabs->obstack ());
7811 /* At this point we want to keep the address map. */
7812 save_psymtabs_addrmap.release ();
7813
7814 if (dwarf_read_debug)
7815 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
7816 objfile_name (objfile));
7817 }
7818
7819 /* Load the partial DIEs for a secondary CU into memory.
7820 This is also used when rereading a primary CU with load_all_dies. */
7821
7822 static void
7823 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7824 {
7825 cutu_reader reader (this_cu, NULL, 1, false);
7826
7827 if (!reader.dummy_p)
7828 {
7829 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7830 language_minimal);
7831
7832 /* Check if comp unit has_children.
7833 If so, read the rest of the partial symbols from this comp unit.
7834 If not, there's no more debug_info for this comp unit. */
7835 if (reader.comp_unit_die->has_children)
7836 load_partial_dies (&reader, reader.info_ptr, 0);
7837
7838 reader.keep ();
7839 }
7840 }
7841
7842 static void
7843 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
7844 struct dwarf2_section_info *section,
7845 struct dwarf2_section_info *abbrev_section,
7846 unsigned int is_dwz)
7847 {
7848 const gdb_byte *info_ptr;
7849 struct objfile *objfile = dwarf2_per_objfile->objfile;
7850
7851 if (dwarf_read_debug)
7852 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
7853 section->get_name (),
7854 section->get_file_name ());
7855
7856 section->read (objfile);
7857
7858 info_ptr = section->buffer;
7859
7860 while (info_ptr < section->buffer + section->size)
7861 {
7862 struct dwarf2_per_cu_data *this_cu;
7863
7864 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
7865
7866 comp_unit_head cu_header;
7867 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
7868 abbrev_section, info_ptr,
7869 rcuh_kind::COMPILE);
7870
7871 /* Save the compilation unit for later lookup. */
7872 if (cu_header.unit_type != DW_UT_type)
7873 {
7874 this_cu = XOBNEW (&objfile->objfile_obstack,
7875 struct dwarf2_per_cu_data);
7876 memset (this_cu, 0, sizeof (*this_cu));
7877 }
7878 else
7879 {
7880 auto sig_type = XOBNEW (&objfile->objfile_obstack,
7881 struct signatured_type);
7882 memset (sig_type, 0, sizeof (*sig_type));
7883 sig_type->signature = cu_header.signature;
7884 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7885 this_cu = &sig_type->per_cu;
7886 }
7887 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
7888 this_cu->sect_off = sect_off;
7889 this_cu->length = cu_header.length + cu_header.initial_length_size;
7890 this_cu->is_dwz = is_dwz;
7891 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7892 this_cu->section = section;
7893
7894 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
7895
7896 info_ptr = info_ptr + this_cu->length;
7897 }
7898 }
7899
7900 /* Create a list of all compilation units in OBJFILE.
7901 This is only done for -readnow and building partial symtabs. */
7902
7903 static void
7904 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7905 {
7906 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
7907 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
7908 &dwarf2_per_objfile->abbrev, 0);
7909
7910 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
7911 if (dwz != NULL)
7912 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
7913 1);
7914 }
7915
7916 /* Process all loaded DIEs for compilation unit CU, starting at
7917 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
7918 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
7919 DW_AT_ranges). See the comments of add_partial_subprogram on how
7920 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
7921
7922 static void
7923 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
7924 CORE_ADDR *highpc, int set_addrmap,
7925 struct dwarf2_cu *cu)
7926 {
7927 struct partial_die_info *pdi;
7928
7929 /* Now, march along the PDI's, descending into ones which have
7930 interesting children but skipping the children of the other ones,
7931 until we reach the end of the compilation unit. */
7932
7933 pdi = first_die;
7934
7935 while (pdi != NULL)
7936 {
7937 pdi->fixup (cu);
7938
7939 /* Anonymous namespaces or modules have no name but have interesting
7940 children, so we need to look at them. Ditto for anonymous
7941 enums. */
7942
7943 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
7944 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
7945 || pdi->tag == DW_TAG_imported_unit
7946 || pdi->tag == DW_TAG_inlined_subroutine)
7947 {
7948 switch (pdi->tag)
7949 {
7950 case DW_TAG_subprogram:
7951 case DW_TAG_inlined_subroutine:
7952 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7953 break;
7954 case DW_TAG_constant:
7955 case DW_TAG_variable:
7956 case DW_TAG_typedef:
7957 case DW_TAG_union_type:
7958 if (!pdi->is_declaration)
7959 {
7960 add_partial_symbol (pdi, cu);
7961 }
7962 break;
7963 case DW_TAG_class_type:
7964 case DW_TAG_interface_type:
7965 case DW_TAG_structure_type:
7966 if (!pdi->is_declaration)
7967 {
7968 add_partial_symbol (pdi, cu);
7969 }
7970 if ((cu->language == language_rust
7971 || cu->language == language_cplus) && pdi->has_children)
7972 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7973 set_addrmap, cu);
7974 break;
7975 case DW_TAG_enumeration_type:
7976 if (!pdi->is_declaration)
7977 add_partial_enumeration (pdi, cu);
7978 break;
7979 case DW_TAG_base_type:
7980 case DW_TAG_subrange_type:
7981 /* File scope base type definitions are added to the partial
7982 symbol table. */
7983 add_partial_symbol (pdi, cu);
7984 break;
7985 case DW_TAG_namespace:
7986 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
7987 break;
7988 case DW_TAG_module:
7989 if (!pdi->is_declaration)
7990 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
7991 break;
7992 case DW_TAG_imported_unit:
7993 {
7994 struct dwarf2_per_cu_data *per_cu;
7995
7996 /* For now we don't handle imported units in type units. */
7997 if (cu->per_cu->is_debug_types)
7998 {
7999 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8000 " supported in type units [in module %s]"),
8001 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
8002 }
8003
8004 per_cu = dwarf2_find_containing_comp_unit
8005 (pdi->d.sect_off, pdi->is_dwz,
8006 cu->per_cu->dwarf2_per_objfile);
8007
8008 /* Go read the partial unit, if needed. */
8009 if (per_cu->v.psymtab == NULL)
8010 process_psymtab_comp_unit (per_cu, true, cu->language);
8011
8012 cu->per_cu->imported_symtabs_push (per_cu);
8013 }
8014 break;
8015 case DW_TAG_imported_declaration:
8016 add_partial_symbol (pdi, cu);
8017 break;
8018 default:
8019 break;
8020 }
8021 }
8022
8023 /* If the die has a sibling, skip to the sibling. */
8024
8025 pdi = pdi->die_sibling;
8026 }
8027 }
8028
8029 /* Functions used to compute the fully scoped name of a partial DIE.
8030
8031 Normally, this is simple. For C++, the parent DIE's fully scoped
8032 name is concatenated with "::" and the partial DIE's name.
8033 Enumerators are an exception; they use the scope of their parent
8034 enumeration type, i.e. the name of the enumeration type is not
8035 prepended to the enumerator.
8036
8037 There are two complexities. One is DW_AT_specification; in this
8038 case "parent" means the parent of the target of the specification,
8039 instead of the direct parent of the DIE. The other is compilers
8040 which do not emit DW_TAG_namespace; in this case we try to guess
8041 the fully qualified name of structure types from their members'
8042 linkage names. This must be done using the DIE's children rather
8043 than the children of any DW_AT_specification target. We only need
8044 to do this for structures at the top level, i.e. if the target of
8045 any DW_AT_specification (if any; otherwise the DIE itself) does not
8046 have a parent. */
8047
8048 /* Compute the scope prefix associated with PDI's parent, in
8049 compilation unit CU. The result will be allocated on CU's
8050 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8051 field. NULL is returned if no prefix is necessary. */
8052 static const char *
8053 partial_die_parent_scope (struct partial_die_info *pdi,
8054 struct dwarf2_cu *cu)
8055 {
8056 const char *grandparent_scope;
8057 struct partial_die_info *parent, *real_pdi;
8058
8059 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8060 then this means the parent of the specification DIE. */
8061
8062 real_pdi = pdi;
8063 while (real_pdi->has_specification)
8064 {
8065 auto res = find_partial_die (real_pdi->spec_offset,
8066 real_pdi->spec_is_dwz, cu);
8067 real_pdi = res.pdi;
8068 cu = res.cu;
8069 }
8070
8071 parent = real_pdi->die_parent;
8072 if (parent == NULL)
8073 return NULL;
8074
8075 if (parent->scope_set)
8076 return parent->scope;
8077
8078 parent->fixup (cu);
8079
8080 grandparent_scope = partial_die_parent_scope (parent, cu);
8081
8082 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8083 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8084 Work around this problem here. */
8085 if (cu->language == language_cplus
8086 && parent->tag == DW_TAG_namespace
8087 && strcmp (parent->name, "::") == 0
8088 && grandparent_scope == NULL)
8089 {
8090 parent->scope = NULL;
8091 parent->scope_set = 1;
8092 return NULL;
8093 }
8094
8095 /* Nested subroutines in Fortran get a prefix. */
8096 if (pdi->tag == DW_TAG_enumerator)
8097 /* Enumerators should not get the name of the enumeration as a prefix. */
8098 parent->scope = grandparent_scope;
8099 else if (parent->tag == DW_TAG_namespace
8100 || parent->tag == DW_TAG_module
8101 || parent->tag == DW_TAG_structure_type
8102 || parent->tag == DW_TAG_class_type
8103 || parent->tag == DW_TAG_interface_type
8104 || parent->tag == DW_TAG_union_type
8105 || parent->tag == DW_TAG_enumeration_type
8106 || (cu->language == language_fortran
8107 && parent->tag == DW_TAG_subprogram
8108 && pdi->tag == DW_TAG_subprogram))
8109 {
8110 if (grandparent_scope == NULL)
8111 parent->scope = parent->name;
8112 else
8113 parent->scope = typename_concat (&cu->comp_unit_obstack,
8114 grandparent_scope,
8115 parent->name, 0, cu);
8116 }
8117 else
8118 {
8119 /* FIXME drow/2004-04-01: What should we be doing with
8120 function-local names? For partial symbols, we should probably be
8121 ignoring them. */
8122 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8123 dwarf_tag_name (parent->tag),
8124 sect_offset_str (pdi->sect_off));
8125 parent->scope = grandparent_scope;
8126 }
8127
8128 parent->scope_set = 1;
8129 return parent->scope;
8130 }
8131
8132 /* Return the fully scoped name associated with PDI, from compilation unit
8133 CU. The result will be allocated with malloc. */
8134
8135 static gdb::unique_xmalloc_ptr<char>
8136 partial_die_full_name (struct partial_die_info *pdi,
8137 struct dwarf2_cu *cu)
8138 {
8139 const char *parent_scope;
8140
8141 /* If this is a template instantiation, we can not work out the
8142 template arguments from partial DIEs. So, unfortunately, we have
8143 to go through the full DIEs. At least any work we do building
8144 types here will be reused if full symbols are loaded later. */
8145 if (pdi->has_template_arguments)
8146 {
8147 pdi->fixup (cu);
8148
8149 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8150 {
8151 struct die_info *die;
8152 struct attribute attr;
8153 struct dwarf2_cu *ref_cu = cu;
8154
8155 /* DW_FORM_ref_addr is using section offset. */
8156 attr.name = (enum dwarf_attribute) 0;
8157 attr.form = DW_FORM_ref_addr;
8158 attr.u.unsnd = to_underlying (pdi->sect_off);
8159 die = follow_die_ref (NULL, &attr, &ref_cu);
8160
8161 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8162 }
8163 }
8164
8165 parent_scope = partial_die_parent_scope (pdi, cu);
8166 if (parent_scope == NULL)
8167 return NULL;
8168 else
8169 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8170 pdi->name, 0, cu));
8171 }
8172
8173 static void
8174 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8175 {
8176 struct dwarf2_per_objfile *dwarf2_per_objfile
8177 = cu->per_cu->dwarf2_per_objfile;
8178 struct objfile *objfile = dwarf2_per_objfile->objfile;
8179 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8180 CORE_ADDR addr = 0;
8181 const char *actual_name = NULL;
8182 CORE_ADDR baseaddr;
8183
8184 baseaddr = objfile->text_section_offset ();
8185
8186 gdb::unique_xmalloc_ptr<char> built_actual_name
8187 = partial_die_full_name (pdi, cu);
8188 if (built_actual_name != NULL)
8189 actual_name = built_actual_name.get ();
8190
8191 if (actual_name == NULL)
8192 actual_name = pdi->name;
8193
8194 switch (pdi->tag)
8195 {
8196 case DW_TAG_inlined_subroutine:
8197 case DW_TAG_subprogram:
8198 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8199 - baseaddr);
8200 if (pdi->is_external
8201 || cu->language == language_ada
8202 || (cu->language == language_fortran
8203 && pdi->die_parent != NULL
8204 && pdi->die_parent->tag == DW_TAG_subprogram))
8205 {
8206 /* Normally, only "external" DIEs are part of the global scope.
8207 But in Ada and Fortran, we want to be able to access nested
8208 procedures globally. So all Ada and Fortran subprograms are
8209 stored in the global scope. */
8210 add_psymbol_to_list (actual_name,
8211 built_actual_name != NULL,
8212 VAR_DOMAIN, LOC_BLOCK,
8213 SECT_OFF_TEXT (objfile),
8214 psymbol_placement::GLOBAL,
8215 addr,
8216 cu->language, objfile);
8217 }
8218 else
8219 {
8220 add_psymbol_to_list (actual_name,
8221 built_actual_name != NULL,
8222 VAR_DOMAIN, LOC_BLOCK,
8223 SECT_OFF_TEXT (objfile),
8224 psymbol_placement::STATIC,
8225 addr, cu->language, objfile);
8226 }
8227
8228 if (pdi->main_subprogram && actual_name != NULL)
8229 set_objfile_main_name (objfile, actual_name, cu->language);
8230 break;
8231 case DW_TAG_constant:
8232 add_psymbol_to_list (actual_name,
8233 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8234 -1, (pdi->is_external
8235 ? psymbol_placement::GLOBAL
8236 : psymbol_placement::STATIC),
8237 0, cu->language, objfile);
8238 break;
8239 case DW_TAG_variable:
8240 if (pdi->d.locdesc)
8241 addr = decode_locdesc (pdi->d.locdesc, cu);
8242
8243 if (pdi->d.locdesc
8244 && addr == 0
8245 && !dwarf2_per_objfile->has_section_at_zero)
8246 {
8247 /* A global or static variable may also have been stripped
8248 out by the linker if unused, in which case its address
8249 will be nullified; do not add such variables into partial
8250 symbol table then. */
8251 }
8252 else if (pdi->is_external)
8253 {
8254 /* Global Variable.
8255 Don't enter into the minimal symbol tables as there is
8256 a minimal symbol table entry from the ELF symbols already.
8257 Enter into partial symbol table if it has a location
8258 descriptor or a type.
8259 If the location descriptor is missing, new_symbol will create
8260 a LOC_UNRESOLVED symbol, the address of the variable will then
8261 be determined from the minimal symbol table whenever the variable
8262 is referenced.
8263 The address for the partial symbol table entry is not
8264 used by GDB, but it comes in handy for debugging partial symbol
8265 table building. */
8266
8267 if (pdi->d.locdesc || pdi->has_type)
8268 add_psymbol_to_list (actual_name,
8269 built_actual_name != NULL,
8270 VAR_DOMAIN, LOC_STATIC,
8271 SECT_OFF_TEXT (objfile),
8272 psymbol_placement::GLOBAL,
8273 addr, cu->language, objfile);
8274 }
8275 else
8276 {
8277 int has_loc = pdi->d.locdesc != NULL;
8278
8279 /* Static Variable. Skip symbols whose value we cannot know (those
8280 without location descriptors or constant values). */
8281 if (!has_loc && !pdi->has_const_value)
8282 return;
8283
8284 add_psymbol_to_list (actual_name,
8285 built_actual_name != NULL,
8286 VAR_DOMAIN, LOC_STATIC,
8287 SECT_OFF_TEXT (objfile),
8288 psymbol_placement::STATIC,
8289 has_loc ? addr : 0,
8290 cu->language, objfile);
8291 }
8292 break;
8293 case DW_TAG_typedef:
8294 case DW_TAG_base_type:
8295 case DW_TAG_subrange_type:
8296 add_psymbol_to_list (actual_name,
8297 built_actual_name != NULL,
8298 VAR_DOMAIN, LOC_TYPEDEF, -1,
8299 psymbol_placement::STATIC,
8300 0, cu->language, objfile);
8301 break;
8302 case DW_TAG_imported_declaration:
8303 case DW_TAG_namespace:
8304 add_psymbol_to_list (actual_name,
8305 built_actual_name != NULL,
8306 VAR_DOMAIN, LOC_TYPEDEF, -1,
8307 psymbol_placement::GLOBAL,
8308 0, cu->language, objfile);
8309 break;
8310 case DW_TAG_module:
8311 /* With Fortran 77 there might be a "BLOCK DATA" module
8312 available without any name. If so, we skip the module as it
8313 doesn't bring any value. */
8314 if (actual_name != nullptr)
8315 add_psymbol_to_list (actual_name,
8316 built_actual_name != NULL,
8317 MODULE_DOMAIN, LOC_TYPEDEF, -1,
8318 psymbol_placement::GLOBAL,
8319 0, cu->language, objfile);
8320 break;
8321 case DW_TAG_class_type:
8322 case DW_TAG_interface_type:
8323 case DW_TAG_structure_type:
8324 case DW_TAG_union_type:
8325 case DW_TAG_enumeration_type:
8326 /* Skip external references. The DWARF standard says in the section
8327 about "Structure, Union, and Class Type Entries": "An incomplete
8328 structure, union or class type is represented by a structure,
8329 union or class entry that does not have a byte size attribute
8330 and that has a DW_AT_declaration attribute." */
8331 if (!pdi->has_byte_size && pdi->is_declaration)
8332 return;
8333
8334 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8335 static vs. global. */
8336 add_psymbol_to_list (actual_name,
8337 built_actual_name != NULL,
8338 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
8339 cu->language == language_cplus
8340 ? psymbol_placement::GLOBAL
8341 : psymbol_placement::STATIC,
8342 0, cu->language, objfile);
8343
8344 break;
8345 case DW_TAG_enumerator:
8346 add_psymbol_to_list (actual_name,
8347 built_actual_name != NULL,
8348 VAR_DOMAIN, LOC_CONST, -1,
8349 cu->language == language_cplus
8350 ? psymbol_placement::GLOBAL
8351 : psymbol_placement::STATIC,
8352 0, cu->language, objfile);
8353 break;
8354 default:
8355 break;
8356 }
8357 }
8358
8359 /* Read a partial die corresponding to a namespace; also, add a symbol
8360 corresponding to that namespace to the symbol table. NAMESPACE is
8361 the name of the enclosing namespace. */
8362
8363 static void
8364 add_partial_namespace (struct partial_die_info *pdi,
8365 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8366 int set_addrmap, struct dwarf2_cu *cu)
8367 {
8368 /* Add a symbol for the namespace. */
8369
8370 add_partial_symbol (pdi, cu);
8371
8372 /* Now scan partial symbols in that namespace. */
8373
8374 if (pdi->has_children)
8375 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8376 }
8377
8378 /* Read a partial die corresponding to a Fortran module. */
8379
8380 static void
8381 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8382 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8383 {
8384 /* Add a symbol for the namespace. */
8385
8386 add_partial_symbol (pdi, cu);
8387
8388 /* Now scan partial symbols in that module. */
8389
8390 if (pdi->has_children)
8391 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8392 }
8393
8394 /* Read a partial die corresponding to a subprogram or an inlined
8395 subprogram and create a partial symbol for that subprogram.
8396 When the CU language allows it, this routine also defines a partial
8397 symbol for each nested subprogram that this subprogram contains.
8398 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8399 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8400
8401 PDI may also be a lexical block, in which case we simply search
8402 recursively for subprograms defined inside that lexical block.
8403 Again, this is only performed when the CU language allows this
8404 type of definitions. */
8405
8406 static void
8407 add_partial_subprogram (struct partial_die_info *pdi,
8408 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8409 int set_addrmap, struct dwarf2_cu *cu)
8410 {
8411 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8412 {
8413 if (pdi->has_pc_info)
8414 {
8415 if (pdi->lowpc < *lowpc)
8416 *lowpc = pdi->lowpc;
8417 if (pdi->highpc > *highpc)
8418 *highpc = pdi->highpc;
8419 if (set_addrmap)
8420 {
8421 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8422 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8423 CORE_ADDR baseaddr;
8424 CORE_ADDR this_highpc;
8425 CORE_ADDR this_lowpc;
8426
8427 baseaddr = objfile->text_section_offset ();
8428 this_lowpc
8429 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8430 pdi->lowpc + baseaddr)
8431 - baseaddr);
8432 this_highpc
8433 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8434 pdi->highpc + baseaddr)
8435 - baseaddr);
8436 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8437 this_lowpc, this_highpc - 1,
8438 cu->per_cu->v.psymtab);
8439 }
8440 }
8441
8442 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8443 {
8444 if (!pdi->is_declaration)
8445 /* Ignore subprogram DIEs that do not have a name, they are
8446 illegal. Do not emit a complaint at this point, we will
8447 do so when we convert this psymtab into a symtab. */
8448 if (pdi->name)
8449 add_partial_symbol (pdi, cu);
8450 }
8451 }
8452
8453 if (! pdi->has_children)
8454 return;
8455
8456 if (cu->language == language_ada || cu->language == language_fortran)
8457 {
8458 pdi = pdi->die_child;
8459 while (pdi != NULL)
8460 {
8461 pdi->fixup (cu);
8462 if (pdi->tag == DW_TAG_subprogram
8463 || pdi->tag == DW_TAG_inlined_subroutine
8464 || pdi->tag == DW_TAG_lexical_block)
8465 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8466 pdi = pdi->die_sibling;
8467 }
8468 }
8469 }
8470
8471 /* Read a partial die corresponding to an enumeration type. */
8472
8473 static void
8474 add_partial_enumeration (struct partial_die_info *enum_pdi,
8475 struct dwarf2_cu *cu)
8476 {
8477 struct partial_die_info *pdi;
8478
8479 if (enum_pdi->name != NULL)
8480 add_partial_symbol (enum_pdi, cu);
8481
8482 pdi = enum_pdi->die_child;
8483 while (pdi)
8484 {
8485 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
8486 complaint (_("malformed enumerator DIE ignored"));
8487 else
8488 add_partial_symbol (pdi, cu);
8489 pdi = pdi->die_sibling;
8490 }
8491 }
8492
8493 /* Return the initial uleb128 in the die at INFO_PTR. */
8494
8495 static unsigned int
8496 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8497 {
8498 unsigned int bytes_read;
8499
8500 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8501 }
8502
8503 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8504 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8505
8506 Return the corresponding abbrev, or NULL if the number is zero (indicating
8507 an empty DIE). In either case *BYTES_READ will be set to the length of
8508 the initial number. */
8509
8510 static struct abbrev_info *
8511 peek_die_abbrev (const die_reader_specs &reader,
8512 const gdb_byte *info_ptr, unsigned int *bytes_read)
8513 {
8514 dwarf2_cu *cu = reader.cu;
8515 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
8516 unsigned int abbrev_number
8517 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8518
8519 if (abbrev_number == 0)
8520 return NULL;
8521
8522 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8523 if (!abbrev)
8524 {
8525 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8526 " at offset %s [in module %s]"),
8527 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8528 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8529 }
8530
8531 return abbrev;
8532 }
8533
8534 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8535 Returns a pointer to the end of a series of DIEs, terminated by an empty
8536 DIE. Any children of the skipped DIEs will also be skipped. */
8537
8538 static const gdb_byte *
8539 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8540 {
8541 while (1)
8542 {
8543 unsigned int bytes_read;
8544 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8545
8546 if (abbrev == NULL)
8547 return info_ptr + bytes_read;
8548 else
8549 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8550 }
8551 }
8552
8553 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8554 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8555 abbrev corresponding to that skipped uleb128 should be passed in
8556 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8557 children. */
8558
8559 static const gdb_byte *
8560 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8561 struct abbrev_info *abbrev)
8562 {
8563 unsigned int bytes_read;
8564 struct attribute attr;
8565 bfd *abfd = reader->abfd;
8566 struct dwarf2_cu *cu = reader->cu;
8567 const gdb_byte *buffer = reader->buffer;
8568 const gdb_byte *buffer_end = reader->buffer_end;
8569 unsigned int form, i;
8570
8571 for (i = 0; i < abbrev->num_attrs; i++)
8572 {
8573 /* The only abbrev we care about is DW_AT_sibling. */
8574 if (abbrev->attrs[i].name == DW_AT_sibling)
8575 {
8576 bool ignored;
8577 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8578 &ignored);
8579 if (attr.form == DW_FORM_ref_addr)
8580 complaint (_("ignoring absolute DW_AT_sibling"));
8581 else
8582 {
8583 sect_offset off = dwarf2_get_ref_die_offset (&attr);
8584 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8585
8586 if (sibling_ptr < info_ptr)
8587 complaint (_("DW_AT_sibling points backwards"));
8588 else if (sibling_ptr > reader->buffer_end)
8589 reader->die_section->overflow_complaint ();
8590 else
8591 return sibling_ptr;
8592 }
8593 }
8594
8595 /* If it isn't DW_AT_sibling, skip this attribute. */
8596 form = abbrev->attrs[i].form;
8597 skip_attribute:
8598 switch (form)
8599 {
8600 case DW_FORM_ref_addr:
8601 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8602 and later it is offset sized. */
8603 if (cu->header.version == 2)
8604 info_ptr += cu->header.addr_size;
8605 else
8606 info_ptr += cu->header.offset_size;
8607 break;
8608 case DW_FORM_GNU_ref_alt:
8609 info_ptr += cu->header.offset_size;
8610 break;
8611 case DW_FORM_addr:
8612 info_ptr += cu->header.addr_size;
8613 break;
8614 case DW_FORM_data1:
8615 case DW_FORM_ref1:
8616 case DW_FORM_flag:
8617 case DW_FORM_strx1:
8618 info_ptr += 1;
8619 break;
8620 case DW_FORM_flag_present:
8621 case DW_FORM_implicit_const:
8622 break;
8623 case DW_FORM_data2:
8624 case DW_FORM_ref2:
8625 case DW_FORM_strx2:
8626 info_ptr += 2;
8627 break;
8628 case DW_FORM_strx3:
8629 info_ptr += 3;
8630 break;
8631 case DW_FORM_data4:
8632 case DW_FORM_ref4:
8633 case DW_FORM_strx4:
8634 info_ptr += 4;
8635 break;
8636 case DW_FORM_data8:
8637 case DW_FORM_ref8:
8638 case DW_FORM_ref_sig8:
8639 info_ptr += 8;
8640 break;
8641 case DW_FORM_data16:
8642 info_ptr += 16;
8643 break;
8644 case DW_FORM_string:
8645 read_direct_string (abfd, info_ptr, &bytes_read);
8646 info_ptr += bytes_read;
8647 break;
8648 case DW_FORM_sec_offset:
8649 case DW_FORM_strp:
8650 case DW_FORM_GNU_strp_alt:
8651 info_ptr += cu->header.offset_size;
8652 break;
8653 case DW_FORM_exprloc:
8654 case DW_FORM_block:
8655 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8656 info_ptr += bytes_read;
8657 break;
8658 case DW_FORM_block1:
8659 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8660 break;
8661 case DW_FORM_block2:
8662 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8663 break;
8664 case DW_FORM_block4:
8665 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8666 break;
8667 case DW_FORM_addrx:
8668 case DW_FORM_strx:
8669 case DW_FORM_sdata:
8670 case DW_FORM_udata:
8671 case DW_FORM_ref_udata:
8672 case DW_FORM_GNU_addr_index:
8673 case DW_FORM_GNU_str_index:
8674 case DW_FORM_rnglistx:
8675 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8676 break;
8677 case DW_FORM_indirect:
8678 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8679 info_ptr += bytes_read;
8680 /* We need to continue parsing from here, so just go back to
8681 the top. */
8682 goto skip_attribute;
8683
8684 default:
8685 error (_("Dwarf Error: Cannot handle %s "
8686 "in DWARF reader [in module %s]"),
8687 dwarf_form_name (form),
8688 bfd_get_filename (abfd));
8689 }
8690 }
8691
8692 if (abbrev->has_children)
8693 return skip_children (reader, info_ptr);
8694 else
8695 return info_ptr;
8696 }
8697
8698 /* Locate ORIG_PDI's sibling.
8699 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8700
8701 static const gdb_byte *
8702 locate_pdi_sibling (const struct die_reader_specs *reader,
8703 struct partial_die_info *orig_pdi,
8704 const gdb_byte *info_ptr)
8705 {
8706 /* Do we know the sibling already? */
8707
8708 if (orig_pdi->sibling)
8709 return orig_pdi->sibling;
8710
8711 /* Are there any children to deal with? */
8712
8713 if (!orig_pdi->has_children)
8714 return info_ptr;
8715
8716 /* Skip the children the long way. */
8717
8718 return skip_children (reader, info_ptr);
8719 }
8720
8721 /* Expand this partial symbol table into a full symbol table. SELF is
8722 not NULL. */
8723
8724 void
8725 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8726 {
8727 struct dwarf2_per_objfile *dwarf2_per_objfile
8728 = get_dwarf2_per_objfile (objfile);
8729
8730 gdb_assert (!readin);
8731 /* If this psymtab is constructed from a debug-only objfile, the
8732 has_section_at_zero flag will not necessarily be correct. We
8733 can get the correct value for this flag by looking at the data
8734 associated with the (presumably stripped) associated objfile. */
8735 if (objfile->separate_debug_objfile_backlink)
8736 {
8737 struct dwarf2_per_objfile *dpo_backlink
8738 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8739
8740 dwarf2_per_objfile->has_section_at_zero
8741 = dpo_backlink->has_section_at_zero;
8742 }
8743
8744 expand_psymtab (objfile);
8745
8746 process_cu_includes (dwarf2_per_objfile);
8747 }
8748 \f
8749 /* Reading in full CUs. */
8750
8751 /* Add PER_CU to the queue. */
8752
8753 static void
8754 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8755 enum language pretend_language)
8756 {
8757 per_cu->queued = 1;
8758 per_cu->dwarf2_per_objfile->queue.emplace (per_cu, pretend_language);
8759 }
8760
8761 /* If PER_CU is not yet queued, add it to the queue.
8762 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8763 dependency.
8764 The result is non-zero if PER_CU was queued, otherwise the result is zero
8765 meaning either PER_CU is already queued or it is already loaded.
8766
8767 N.B. There is an invariant here that if a CU is queued then it is loaded.
8768 The caller is required to load PER_CU if we return non-zero. */
8769
8770 static int
8771 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8772 struct dwarf2_per_cu_data *per_cu,
8773 enum language pretend_language)
8774 {
8775 /* We may arrive here during partial symbol reading, if we need full
8776 DIEs to process an unusual case (e.g. template arguments). Do
8777 not queue PER_CU, just tell our caller to load its DIEs. */
8778 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
8779 {
8780 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8781 return 1;
8782 return 0;
8783 }
8784
8785 /* Mark the dependence relation so that we don't flush PER_CU
8786 too early. */
8787 if (dependent_cu != NULL)
8788 dwarf2_add_dependence (dependent_cu, per_cu);
8789
8790 /* If it's already on the queue, we have nothing to do. */
8791 if (per_cu->queued)
8792 return 0;
8793
8794 /* If the compilation unit is already loaded, just mark it as
8795 used. */
8796 if (per_cu->cu != NULL)
8797 {
8798 per_cu->cu->last_used = 0;
8799 return 0;
8800 }
8801
8802 /* Add it to the queue. */
8803 queue_comp_unit (per_cu, pretend_language);
8804
8805 return 1;
8806 }
8807
8808 /* Process the queue. */
8809
8810 static void
8811 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
8812 {
8813 if (dwarf_read_debug)
8814 {
8815 fprintf_unfiltered (gdb_stdlog,
8816 "Expanding one or more symtabs of objfile %s ...\n",
8817 objfile_name (dwarf2_per_objfile->objfile));
8818 }
8819
8820 /* The queue starts out with one item, but following a DIE reference
8821 may load a new CU, adding it to the end of the queue. */
8822 while (!dwarf2_per_objfile->queue.empty ())
8823 {
8824 dwarf2_queue_item &item = dwarf2_per_objfile->queue.front ();
8825
8826 if ((dwarf2_per_objfile->using_index
8827 ? !item.per_cu->v.quick->compunit_symtab
8828 : (item.per_cu->v.psymtab && !item.per_cu->v.psymtab->readin))
8829 /* Skip dummy CUs. */
8830 && item.per_cu->cu != NULL)
8831 {
8832 struct dwarf2_per_cu_data *per_cu = item.per_cu;
8833 unsigned int debug_print_threshold;
8834 char buf[100];
8835
8836 if (per_cu->is_debug_types)
8837 {
8838 struct signatured_type *sig_type =
8839 (struct signatured_type *) per_cu;
8840
8841 sprintf (buf, "TU %s at offset %s",
8842 hex_string (sig_type->signature),
8843 sect_offset_str (per_cu->sect_off));
8844 /* There can be 100s of TUs.
8845 Only print them in verbose mode. */
8846 debug_print_threshold = 2;
8847 }
8848 else
8849 {
8850 sprintf (buf, "CU at offset %s",
8851 sect_offset_str (per_cu->sect_off));
8852 debug_print_threshold = 1;
8853 }
8854
8855 if (dwarf_read_debug >= debug_print_threshold)
8856 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
8857
8858 if (per_cu->is_debug_types)
8859 process_full_type_unit (per_cu, item.pretend_language);
8860 else
8861 process_full_comp_unit (per_cu, item.pretend_language);
8862
8863 if (dwarf_read_debug >= debug_print_threshold)
8864 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
8865 }
8866
8867 item.per_cu->queued = 0;
8868 dwarf2_per_objfile->queue.pop ();
8869 }
8870
8871 if (dwarf_read_debug)
8872 {
8873 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
8874 objfile_name (dwarf2_per_objfile->objfile));
8875 }
8876 }
8877
8878 /* Read in full symbols for PST, and anything it depends on. */
8879
8880 void
8881 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
8882 {
8883 if (readin)
8884 return;
8885
8886 read_dependencies (objfile);
8887
8888 dw2_do_instantiate_symtab (per_cu_data, false);
8889 gdb_assert (get_compunit_symtab () != nullptr);
8890 }
8891
8892 /* Trivial hash function for die_info: the hash value of a DIE
8893 is its offset in .debug_info for this objfile. */
8894
8895 static hashval_t
8896 die_hash (const void *item)
8897 {
8898 const struct die_info *die = (const struct die_info *) item;
8899
8900 return to_underlying (die->sect_off);
8901 }
8902
8903 /* Trivial comparison function for die_info structures: two DIEs
8904 are equal if they have the same offset. */
8905
8906 static int
8907 die_eq (const void *item_lhs, const void *item_rhs)
8908 {
8909 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8910 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
8911
8912 return die_lhs->sect_off == die_rhs->sect_off;
8913 }
8914
8915 /* Load the DIEs associated with PER_CU into memory. */
8916
8917 static void
8918 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8919 bool skip_partial,
8920 enum language pretend_language)
8921 {
8922 gdb_assert (! this_cu->is_debug_types);
8923
8924 cutu_reader reader (this_cu, NULL, 1, skip_partial);
8925 if (reader.dummy_p)
8926 return;
8927
8928 struct dwarf2_cu *cu = reader.cu;
8929 const gdb_byte *info_ptr = reader.info_ptr;
8930
8931 gdb_assert (cu->die_hash == NULL);
8932 cu->die_hash =
8933 htab_create_alloc_ex (cu->header.length / 12,
8934 die_hash,
8935 die_eq,
8936 NULL,
8937 &cu->comp_unit_obstack,
8938 hashtab_obstack_allocate,
8939 dummy_obstack_deallocate);
8940
8941 if (reader.comp_unit_die->has_children)
8942 reader.comp_unit_die->child
8943 = read_die_and_siblings (&reader, reader.info_ptr,
8944 &info_ptr, reader.comp_unit_die);
8945 cu->dies = reader.comp_unit_die;
8946 /* comp_unit_die is not stored in die_hash, no need. */
8947
8948 /* We try not to read any attributes in this function, because not
8949 all CUs needed for references have been loaded yet, and symbol
8950 table processing isn't initialized. But we have to set the CU language,
8951 or we won't be able to build types correctly.
8952 Similarly, if we do not read the producer, we can not apply
8953 producer-specific interpretation. */
8954 prepare_one_comp_unit (cu, cu->dies, pretend_language);
8955
8956 reader.keep ();
8957 }
8958
8959 /* Add a DIE to the delayed physname list. */
8960
8961 static void
8962 add_to_method_list (struct type *type, int fnfield_index, int index,
8963 const char *name, struct die_info *die,
8964 struct dwarf2_cu *cu)
8965 {
8966 struct delayed_method_info mi;
8967 mi.type = type;
8968 mi.fnfield_index = fnfield_index;
8969 mi.index = index;
8970 mi.name = name;
8971 mi.die = die;
8972 cu->method_list.push_back (mi);
8973 }
8974
8975 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8976 "const" / "volatile". If so, decrements LEN by the length of the
8977 modifier and return true. Otherwise return false. */
8978
8979 template<size_t N>
8980 static bool
8981 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8982 {
8983 size_t mod_len = sizeof (mod) - 1;
8984 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8985 {
8986 len -= mod_len;
8987 return true;
8988 }
8989 return false;
8990 }
8991
8992 /* Compute the physnames of any methods on the CU's method list.
8993
8994 The computation of method physnames is delayed in order to avoid the
8995 (bad) condition that one of the method's formal parameters is of an as yet
8996 incomplete type. */
8997
8998 static void
8999 compute_delayed_physnames (struct dwarf2_cu *cu)
9000 {
9001 /* Only C++ delays computing physnames. */
9002 if (cu->method_list.empty ())
9003 return;
9004 gdb_assert (cu->language == language_cplus);
9005
9006 for (const delayed_method_info &mi : cu->method_list)
9007 {
9008 const char *physname;
9009 struct fn_fieldlist *fn_flp
9010 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9011 physname = dwarf2_physname (mi.name, mi.die, cu);
9012 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9013 = physname ? physname : "";
9014
9015 /* Since there's no tag to indicate whether a method is a
9016 const/volatile overload, extract that information out of the
9017 demangled name. */
9018 if (physname != NULL)
9019 {
9020 size_t len = strlen (physname);
9021
9022 while (1)
9023 {
9024 if (physname[len] == ')') /* shortcut */
9025 break;
9026 else if (check_modifier (physname, len, " const"))
9027 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9028 else if (check_modifier (physname, len, " volatile"))
9029 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9030 else
9031 break;
9032 }
9033 }
9034 }
9035
9036 /* The list is no longer needed. */
9037 cu->method_list.clear ();
9038 }
9039
9040 /* Go objects should be embedded in a DW_TAG_module DIE,
9041 and it's not clear if/how imported objects will appear.
9042 To keep Go support simple until that's worked out,
9043 go back through what we've read and create something usable.
9044 We could do this while processing each DIE, and feels kinda cleaner,
9045 but that way is more invasive.
9046 This is to, for example, allow the user to type "p var" or "b main"
9047 without having to specify the package name, and allow lookups
9048 of module.object to work in contexts that use the expression
9049 parser. */
9050
9051 static void
9052 fixup_go_packaging (struct dwarf2_cu *cu)
9053 {
9054 gdb::unique_xmalloc_ptr<char> package_name;
9055 struct pending *list;
9056 int i;
9057
9058 for (list = *cu->get_builder ()->get_global_symbols ();
9059 list != NULL;
9060 list = list->next)
9061 {
9062 for (i = 0; i < list->nsyms; ++i)
9063 {
9064 struct symbol *sym = list->symbol[i];
9065
9066 if (sym->language () == language_go
9067 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9068 {
9069 gdb::unique_xmalloc_ptr<char> this_package_name
9070 (go_symbol_package_name (sym));
9071
9072 if (this_package_name == NULL)
9073 continue;
9074 if (package_name == NULL)
9075 package_name = std::move (this_package_name);
9076 else
9077 {
9078 struct objfile *objfile
9079 = cu->per_cu->dwarf2_per_objfile->objfile;
9080 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9081 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9082 (symbol_symtab (sym) != NULL
9083 ? symtab_to_filename_for_display
9084 (symbol_symtab (sym))
9085 : objfile_name (objfile)),
9086 this_package_name.get (), package_name.get ());
9087 }
9088 }
9089 }
9090 }
9091
9092 if (package_name != NULL)
9093 {
9094 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9095 const char *saved_package_name = objfile->intern (package_name.get ());
9096 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9097 saved_package_name);
9098 struct symbol *sym;
9099
9100 sym = allocate_symbol (objfile);
9101 sym->set_language (language_go, &objfile->objfile_obstack);
9102 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9103 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9104 e.g., "main" finds the "main" module and not C's main(). */
9105 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9106 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9107 SYMBOL_TYPE (sym) = type;
9108
9109 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9110 }
9111 }
9112
9113 /* Allocate a fully-qualified name consisting of the two parts on the
9114 obstack. */
9115
9116 static const char *
9117 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9118 {
9119 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9120 }
9121
9122 /* A helper that allocates a struct discriminant_info to attach to a
9123 union type. */
9124
9125 static struct discriminant_info *
9126 alloc_discriminant_info (struct type *type, int discriminant_index,
9127 int default_index)
9128 {
9129 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9130 gdb_assert (discriminant_index == -1
9131 || (discriminant_index >= 0
9132 && discriminant_index < TYPE_NFIELDS (type)));
9133 gdb_assert (default_index == -1
9134 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9135
9136 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9137
9138 struct discriminant_info *disc
9139 = ((struct discriminant_info *)
9140 TYPE_ZALLOC (type,
9141 offsetof (struct discriminant_info, discriminants)
9142 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9143 disc->default_index = default_index;
9144 disc->discriminant_index = discriminant_index;
9145
9146 struct dynamic_prop prop;
9147 prop.kind = PROP_UNDEFINED;
9148 prop.data.baton = disc;
9149
9150 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9151
9152 return disc;
9153 }
9154
9155 /* Some versions of rustc emitted enums in an unusual way.
9156
9157 Ordinary enums were emitted as unions. The first element of each
9158 structure in the union was named "RUST$ENUM$DISR". This element
9159 held the discriminant.
9160
9161 These versions of Rust also implemented the "non-zero"
9162 optimization. When the enum had two values, and one is empty and
9163 the other holds a pointer that cannot be zero, the pointer is used
9164 as the discriminant, with a zero value meaning the empty variant.
9165 Here, the union's first member is of the form
9166 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9167 where the fieldnos are the indices of the fields that should be
9168 traversed in order to find the field (which may be several fields deep)
9169 and the variantname is the name of the variant of the case when the
9170 field is zero.
9171
9172 This function recognizes whether TYPE is of one of these forms,
9173 and, if so, smashes it to be a variant type. */
9174
9175 static void
9176 quirk_rust_enum (struct type *type, struct objfile *objfile)
9177 {
9178 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9179
9180 /* We don't need to deal with empty enums. */
9181 if (TYPE_NFIELDS (type) == 0)
9182 return;
9183
9184 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9185 if (TYPE_NFIELDS (type) == 1
9186 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9187 {
9188 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9189
9190 /* Decode the field name to find the offset of the
9191 discriminant. */
9192 ULONGEST bit_offset = 0;
9193 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9194 while (name[0] >= '0' && name[0] <= '9')
9195 {
9196 char *tail;
9197 unsigned long index = strtoul (name, &tail, 10);
9198 name = tail;
9199 if (*name != '$'
9200 || index >= TYPE_NFIELDS (field_type)
9201 || (TYPE_FIELD_LOC_KIND (field_type, index)
9202 != FIELD_LOC_KIND_BITPOS))
9203 {
9204 complaint (_("Could not parse Rust enum encoding string \"%s\""
9205 "[in module %s]"),
9206 TYPE_FIELD_NAME (type, 0),
9207 objfile_name (objfile));
9208 return;
9209 }
9210 ++name;
9211
9212 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9213 field_type = TYPE_FIELD_TYPE (field_type, index);
9214 }
9215
9216 /* Make a union to hold the variants. */
9217 struct type *union_type = alloc_type (objfile);
9218 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9219 TYPE_NFIELDS (union_type) = 3;
9220 TYPE_FIELDS (union_type)
9221 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9222 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9223 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9224
9225 /* Put the discriminant must at index 0. */
9226 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9227 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9228 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9229 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9230
9231 /* The order of fields doesn't really matter, so put the real
9232 field at index 1 and the data-less field at index 2. */
9233 struct discriminant_info *disc
9234 = alloc_discriminant_info (union_type, 0, 1);
9235 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9236 TYPE_FIELD_NAME (union_type, 1)
9237 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9238 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9239 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9240 TYPE_FIELD_NAME (union_type, 1));
9241
9242 const char *dataless_name
9243 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9244 name);
9245 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9246 dataless_name);
9247 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9248 /* NAME points into the original discriminant name, which
9249 already has the correct lifetime. */
9250 TYPE_FIELD_NAME (union_type, 2) = name;
9251 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
9252 disc->discriminants[2] = 0;
9253
9254 /* Smash this type to be a structure type. We have to do this
9255 because the type has already been recorded. */
9256 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9257 TYPE_NFIELDS (type) = 1;
9258 TYPE_FIELDS (type)
9259 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
9260
9261 /* Install the variant part. */
9262 TYPE_FIELD_TYPE (type, 0) = union_type;
9263 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9264 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9265 }
9266 /* A union with a single anonymous field is probably an old-style
9267 univariant enum. */
9268 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9269 {
9270 /* Smash this type to be a structure type. We have to do this
9271 because the type has already been recorded. */
9272 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9273
9274 /* Make a union to hold the variants. */
9275 struct type *union_type = alloc_type (objfile);
9276 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9277 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
9278 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9279 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9280 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
9281
9282 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
9283 const char *variant_name
9284 = rust_last_path_segment (TYPE_NAME (field_type));
9285 TYPE_FIELD_NAME (union_type, 0) = variant_name;
9286 TYPE_NAME (field_type)
9287 = rust_fully_qualify (&objfile->objfile_obstack,
9288 TYPE_NAME (type), variant_name);
9289
9290 /* Install the union in the outer struct type. */
9291 TYPE_NFIELDS (type) = 1;
9292 TYPE_FIELDS (type)
9293 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
9294 TYPE_FIELD_TYPE (type, 0) = union_type;
9295 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9296 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9297
9298 alloc_discriminant_info (union_type, -1, 0);
9299 }
9300 else
9301 {
9302 struct type *disr_type = nullptr;
9303 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9304 {
9305 disr_type = TYPE_FIELD_TYPE (type, i);
9306
9307 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
9308 {
9309 /* All fields of a true enum will be structs. */
9310 return;
9311 }
9312 else if (TYPE_NFIELDS (disr_type) == 0)
9313 {
9314 /* Could be data-less variant, so keep going. */
9315 disr_type = nullptr;
9316 }
9317 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9318 "RUST$ENUM$DISR") != 0)
9319 {
9320 /* Not a Rust enum. */
9321 return;
9322 }
9323 else
9324 {
9325 /* Found one. */
9326 break;
9327 }
9328 }
9329
9330 /* If we got here without a discriminant, then it's probably
9331 just a union. */
9332 if (disr_type == nullptr)
9333 return;
9334
9335 /* Smash this type to be a structure type. We have to do this
9336 because the type has already been recorded. */
9337 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9338
9339 /* Make a union to hold the variants. */
9340 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
9341 struct type *union_type = alloc_type (objfile);
9342 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9343 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
9344 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9345 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9346 TYPE_FIELDS (union_type)
9347 = (struct field *) TYPE_ZALLOC (union_type,
9348 (TYPE_NFIELDS (union_type)
9349 * sizeof (struct field)));
9350
9351 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
9352 TYPE_NFIELDS (type) * sizeof (struct field));
9353
9354 /* Install the discriminant at index 0 in the union. */
9355 TYPE_FIELD (union_type, 0) = *disr_field;
9356 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9357 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9358
9359 /* Install the union in the outer struct type. */
9360 TYPE_FIELD_TYPE (type, 0) = union_type;
9361 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9362 TYPE_NFIELDS (type) = 1;
9363
9364 /* Set the size and offset of the union type. */
9365 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9366
9367 /* We need a way to find the correct discriminant given a
9368 variant name. For convenience we build a map here. */
9369 struct type *enum_type = FIELD_TYPE (*disr_field);
9370 std::unordered_map<std::string, ULONGEST> discriminant_map;
9371 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
9372 {
9373 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9374 {
9375 const char *name
9376 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9377 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9378 }
9379 }
9380
9381 int n_fields = TYPE_NFIELDS (union_type);
9382 struct discriminant_info *disc
9383 = alloc_discriminant_info (union_type, 0, -1);
9384 /* Skip the discriminant here. */
9385 for (int i = 1; i < n_fields; ++i)
9386 {
9387 /* Find the final word in the name of this variant's type.
9388 That name can be used to look up the correct
9389 discriminant. */
9390 const char *variant_name
9391 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
9392 i)));
9393
9394 auto iter = discriminant_map.find (variant_name);
9395 if (iter != discriminant_map.end ())
9396 disc->discriminants[i] = iter->second;
9397
9398 /* Remove the discriminant field, if it exists. */
9399 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
9400 if (TYPE_NFIELDS (sub_type) > 0)
9401 {
9402 --TYPE_NFIELDS (sub_type);
9403 ++TYPE_FIELDS (sub_type);
9404 }
9405 TYPE_FIELD_NAME (union_type, i) = variant_name;
9406 TYPE_NAME (sub_type)
9407 = rust_fully_qualify (&objfile->objfile_obstack,
9408 TYPE_NAME (type), variant_name);
9409 }
9410 }
9411 }
9412
9413 /* Rewrite some Rust unions to be structures with variants parts. */
9414
9415 static void
9416 rust_union_quirks (struct dwarf2_cu *cu)
9417 {
9418 gdb_assert (cu->language == language_rust);
9419 for (type *type_ : cu->rust_unions)
9420 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
9421 /* We don't need this any more. */
9422 cu->rust_unions.clear ();
9423 }
9424
9425 /* Return the symtab for PER_CU. This works properly regardless of
9426 whether we're using the index or psymtabs. */
9427
9428 static struct compunit_symtab *
9429 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
9430 {
9431 return (per_cu->dwarf2_per_objfile->using_index
9432 ? per_cu->v.quick->compunit_symtab
9433 : per_cu->v.psymtab->compunit_symtab);
9434 }
9435
9436 /* A helper function for computing the list of all symbol tables
9437 included by PER_CU. */
9438
9439 static void
9440 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9441 htab_t all_children, htab_t all_type_symtabs,
9442 struct dwarf2_per_cu_data *per_cu,
9443 struct compunit_symtab *immediate_parent)
9444 {
9445 void **slot;
9446 struct compunit_symtab *cust;
9447
9448 slot = htab_find_slot (all_children, per_cu, INSERT);
9449 if (*slot != NULL)
9450 {
9451 /* This inclusion and its children have been processed. */
9452 return;
9453 }
9454
9455 *slot = per_cu;
9456 /* Only add a CU if it has a symbol table. */
9457 cust = get_compunit_symtab (per_cu);
9458 if (cust != NULL)
9459 {
9460 /* If this is a type unit only add its symbol table if we haven't
9461 seen it yet (type unit per_cu's can share symtabs). */
9462 if (per_cu->is_debug_types)
9463 {
9464 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9465 if (*slot == NULL)
9466 {
9467 *slot = cust;
9468 result->push_back (cust);
9469 if (cust->user == NULL)
9470 cust->user = immediate_parent;
9471 }
9472 }
9473 else
9474 {
9475 result->push_back (cust);
9476 if (cust->user == NULL)
9477 cust->user = immediate_parent;
9478 }
9479 }
9480
9481 if (!per_cu->imported_symtabs_empty ())
9482 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9483 {
9484 recursively_compute_inclusions (result, all_children,
9485 all_type_symtabs, ptr, cust);
9486 }
9487 }
9488
9489 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9490 PER_CU. */
9491
9492 static void
9493 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
9494 {
9495 gdb_assert (! per_cu->is_debug_types);
9496
9497 if (!per_cu->imported_symtabs_empty ())
9498 {
9499 int len;
9500 std::vector<compunit_symtab *> result_symtabs;
9501 htab_t all_children, all_type_symtabs;
9502 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
9503
9504 /* If we don't have a symtab, we can just skip this case. */
9505 if (cust == NULL)
9506 return;
9507
9508 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9509 NULL, xcalloc, xfree);
9510 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9511 NULL, xcalloc, xfree);
9512
9513 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9514 {
9515 recursively_compute_inclusions (&result_symtabs, all_children,
9516 all_type_symtabs, ptr, cust);
9517 }
9518
9519 /* Now we have a transitive closure of all the included symtabs. */
9520 len = result_symtabs.size ();
9521 cust->includes
9522 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
9523 struct compunit_symtab *, len + 1);
9524 memcpy (cust->includes, result_symtabs.data (),
9525 len * sizeof (compunit_symtab *));
9526 cust->includes[len] = NULL;
9527
9528 htab_delete (all_children);
9529 htab_delete (all_type_symtabs);
9530 }
9531 }
9532
9533 /* Compute the 'includes' field for the symtabs of all the CUs we just
9534 read. */
9535
9536 static void
9537 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
9538 {
9539 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
9540 {
9541 if (! iter->is_debug_types)
9542 compute_compunit_symtab_includes (iter);
9543 }
9544
9545 dwarf2_per_objfile->just_read_cus.clear ();
9546 }
9547
9548 /* Generate full symbol information for PER_CU, whose DIEs have
9549 already been loaded into memory. */
9550
9551 static void
9552 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9553 enum language pretend_language)
9554 {
9555 struct dwarf2_cu *cu = per_cu->cu;
9556 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9557 struct objfile *objfile = dwarf2_per_objfile->objfile;
9558 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9559 CORE_ADDR lowpc, highpc;
9560 struct compunit_symtab *cust;
9561 CORE_ADDR baseaddr;
9562 struct block *static_block;
9563 CORE_ADDR addr;
9564
9565 baseaddr = objfile->text_section_offset ();
9566
9567 /* Clear the list here in case something was left over. */
9568 cu->method_list.clear ();
9569
9570 cu->language = pretend_language;
9571 cu->language_defn = language_def (cu->language);
9572
9573 /* Do line number decoding in read_file_scope () */
9574 process_die (cu->dies, cu);
9575
9576 /* For now fudge the Go package. */
9577 if (cu->language == language_go)
9578 fixup_go_packaging (cu);
9579
9580 /* Now that we have processed all the DIEs in the CU, all the types
9581 should be complete, and it should now be safe to compute all of the
9582 physnames. */
9583 compute_delayed_physnames (cu);
9584
9585 if (cu->language == language_rust)
9586 rust_union_quirks (cu);
9587
9588 /* Some compilers don't define a DW_AT_high_pc attribute for the
9589 compilation unit. If the DW_AT_high_pc is missing, synthesize
9590 it, by scanning the DIE's below the compilation unit. */
9591 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9592
9593 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9594 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9595
9596 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9597 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9598 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9599 addrmap to help ensure it has an accurate map of pc values belonging to
9600 this comp unit. */
9601 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9602
9603 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9604 SECT_OFF_TEXT (objfile),
9605 0);
9606
9607 if (cust != NULL)
9608 {
9609 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9610
9611 /* Set symtab language to language from DW_AT_language. If the
9612 compilation is from a C file generated by language preprocessors, do
9613 not set the language if it was already deduced by start_subfile. */
9614 if (!(cu->language == language_c
9615 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9616 COMPUNIT_FILETABS (cust)->language = cu->language;
9617
9618 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9619 produce DW_AT_location with location lists but it can be possibly
9620 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9621 there were bugs in prologue debug info, fixed later in GCC-4.5
9622 by "unwind info for epilogues" patch (which is not directly related).
9623
9624 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9625 needed, it would be wrong due to missing DW_AT_producer there.
9626
9627 Still one can confuse GDB by using non-standard GCC compilation
9628 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9629 */
9630 if (cu->has_loclist && gcc_4_minor >= 5)
9631 cust->locations_valid = 1;
9632
9633 if (gcc_4_minor >= 5)
9634 cust->epilogue_unwind_valid = 1;
9635
9636 cust->call_site_htab = cu->call_site_htab;
9637 }
9638
9639 if (dwarf2_per_objfile->using_index)
9640 per_cu->v.quick->compunit_symtab = cust;
9641 else
9642 {
9643 dwarf2_psymtab *pst = per_cu->v.psymtab;
9644 pst->compunit_symtab = cust;
9645 pst->readin = true;
9646 }
9647
9648 /* Push it for inclusion processing later. */
9649 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
9650
9651 /* Not needed any more. */
9652 cu->reset_builder ();
9653 }
9654
9655 /* Generate full symbol information for type unit PER_CU, whose DIEs have
9656 already been loaded into memory. */
9657
9658 static void
9659 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9660 enum language pretend_language)
9661 {
9662 struct dwarf2_cu *cu = per_cu->cu;
9663 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9664 struct objfile *objfile = dwarf2_per_objfile->objfile;
9665 struct compunit_symtab *cust;
9666 struct signatured_type *sig_type;
9667
9668 gdb_assert (per_cu->is_debug_types);
9669 sig_type = (struct signatured_type *) per_cu;
9670
9671 /* Clear the list here in case something was left over. */
9672 cu->method_list.clear ();
9673
9674 cu->language = pretend_language;
9675 cu->language_defn = language_def (cu->language);
9676
9677 /* The symbol tables are set up in read_type_unit_scope. */
9678 process_die (cu->dies, cu);
9679
9680 /* For now fudge the Go package. */
9681 if (cu->language == language_go)
9682 fixup_go_packaging (cu);
9683
9684 /* Now that we have processed all the DIEs in the CU, all the types
9685 should be complete, and it should now be safe to compute all of the
9686 physnames. */
9687 compute_delayed_physnames (cu);
9688
9689 if (cu->language == language_rust)
9690 rust_union_quirks (cu);
9691
9692 /* TUs share symbol tables.
9693 If this is the first TU to use this symtab, complete the construction
9694 of it with end_expandable_symtab. Otherwise, complete the addition of
9695 this TU's symbols to the existing symtab. */
9696 if (sig_type->type_unit_group->compunit_symtab == NULL)
9697 {
9698 buildsym_compunit *builder = cu->get_builder ();
9699 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9700 sig_type->type_unit_group->compunit_symtab = cust;
9701
9702 if (cust != NULL)
9703 {
9704 /* Set symtab language to language from DW_AT_language. If the
9705 compilation is from a C file generated by language preprocessors,
9706 do not set the language if it was already deduced by
9707 start_subfile. */
9708 if (!(cu->language == language_c
9709 && COMPUNIT_FILETABS (cust)->language != language_c))
9710 COMPUNIT_FILETABS (cust)->language = cu->language;
9711 }
9712 }
9713 else
9714 {
9715 cu->get_builder ()->augment_type_symtab ();
9716 cust = sig_type->type_unit_group->compunit_symtab;
9717 }
9718
9719 if (dwarf2_per_objfile->using_index)
9720 per_cu->v.quick->compunit_symtab = cust;
9721 else
9722 {
9723 dwarf2_psymtab *pst = per_cu->v.psymtab;
9724 pst->compunit_symtab = cust;
9725 pst->readin = true;
9726 }
9727
9728 /* Not needed any more. */
9729 cu->reset_builder ();
9730 }
9731
9732 /* Process an imported unit DIE. */
9733
9734 static void
9735 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9736 {
9737 struct attribute *attr;
9738
9739 /* For now we don't handle imported units in type units. */
9740 if (cu->per_cu->is_debug_types)
9741 {
9742 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9743 " supported in type units [in module %s]"),
9744 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
9745 }
9746
9747 attr = dwarf2_attr (die, DW_AT_import, cu);
9748 if (attr != NULL)
9749 {
9750 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
9751 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9752 dwarf2_per_cu_data *per_cu
9753 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
9754 cu->per_cu->dwarf2_per_objfile);
9755
9756 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9757 into another compilation unit, at root level. Regard this as a hint,
9758 and ignore it. */
9759 if (die->parent && die->parent->parent == NULL
9760 && per_cu->unit_type == DW_UT_compile
9761 && per_cu->lang == language_cplus)
9762 return;
9763
9764 /* If necessary, add it to the queue and load its DIEs. */
9765 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
9766 load_full_comp_unit (per_cu, false, cu->language);
9767
9768 cu->per_cu->imported_symtabs_push (per_cu);
9769 }
9770 }
9771
9772 /* RAII object that represents a process_die scope: i.e.,
9773 starts/finishes processing a DIE. */
9774 class process_die_scope
9775 {
9776 public:
9777 process_die_scope (die_info *die, dwarf2_cu *cu)
9778 : m_die (die), m_cu (cu)
9779 {
9780 /* We should only be processing DIEs not already in process. */
9781 gdb_assert (!m_die->in_process);
9782 m_die->in_process = true;
9783 }
9784
9785 ~process_die_scope ()
9786 {
9787 m_die->in_process = false;
9788
9789 /* If we're done processing the DIE for the CU that owns the line
9790 header, we don't need the line header anymore. */
9791 if (m_cu->line_header_die_owner == m_die)
9792 {
9793 delete m_cu->line_header;
9794 m_cu->line_header = NULL;
9795 m_cu->line_header_die_owner = NULL;
9796 }
9797 }
9798
9799 private:
9800 die_info *m_die;
9801 dwarf2_cu *m_cu;
9802 };
9803
9804 /* Process a die and its children. */
9805
9806 static void
9807 process_die (struct die_info *die, struct dwarf2_cu *cu)
9808 {
9809 process_die_scope scope (die, cu);
9810
9811 switch (die->tag)
9812 {
9813 case DW_TAG_padding:
9814 break;
9815 case DW_TAG_compile_unit:
9816 case DW_TAG_partial_unit:
9817 read_file_scope (die, cu);
9818 break;
9819 case DW_TAG_type_unit:
9820 read_type_unit_scope (die, cu);
9821 break;
9822 case DW_TAG_subprogram:
9823 /* Nested subprograms in Fortran get a prefix. */
9824 if (cu->language == language_fortran
9825 && die->parent != NULL
9826 && die->parent->tag == DW_TAG_subprogram)
9827 cu->processing_has_namespace_info = true;
9828 /* Fall through. */
9829 case DW_TAG_inlined_subroutine:
9830 read_func_scope (die, cu);
9831 break;
9832 case DW_TAG_lexical_block:
9833 case DW_TAG_try_block:
9834 case DW_TAG_catch_block:
9835 read_lexical_block_scope (die, cu);
9836 break;
9837 case DW_TAG_call_site:
9838 case DW_TAG_GNU_call_site:
9839 read_call_site_scope (die, cu);
9840 break;
9841 case DW_TAG_class_type:
9842 case DW_TAG_interface_type:
9843 case DW_TAG_structure_type:
9844 case DW_TAG_union_type:
9845 process_structure_scope (die, cu);
9846 break;
9847 case DW_TAG_enumeration_type:
9848 process_enumeration_scope (die, cu);
9849 break;
9850
9851 /* These dies have a type, but processing them does not create
9852 a symbol or recurse to process the children. Therefore we can
9853 read them on-demand through read_type_die. */
9854 case DW_TAG_subroutine_type:
9855 case DW_TAG_set_type:
9856 case DW_TAG_array_type:
9857 case DW_TAG_pointer_type:
9858 case DW_TAG_ptr_to_member_type:
9859 case DW_TAG_reference_type:
9860 case DW_TAG_rvalue_reference_type:
9861 case DW_TAG_string_type:
9862 break;
9863
9864 case DW_TAG_base_type:
9865 case DW_TAG_subrange_type:
9866 case DW_TAG_typedef:
9867 /* Add a typedef symbol for the type definition, if it has a
9868 DW_AT_name. */
9869 new_symbol (die, read_type_die (die, cu), cu);
9870 break;
9871 case DW_TAG_common_block:
9872 read_common_block (die, cu);
9873 break;
9874 case DW_TAG_common_inclusion:
9875 break;
9876 case DW_TAG_namespace:
9877 cu->processing_has_namespace_info = true;
9878 read_namespace (die, cu);
9879 break;
9880 case DW_TAG_module:
9881 cu->processing_has_namespace_info = true;
9882 read_module (die, cu);
9883 break;
9884 case DW_TAG_imported_declaration:
9885 cu->processing_has_namespace_info = true;
9886 if (read_namespace_alias (die, cu))
9887 break;
9888 /* The declaration is not a global namespace alias. */
9889 /* Fall through. */
9890 case DW_TAG_imported_module:
9891 cu->processing_has_namespace_info = true;
9892 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9893 || cu->language != language_fortran))
9894 complaint (_("Tag '%s' has unexpected children"),
9895 dwarf_tag_name (die->tag));
9896 read_import_statement (die, cu);
9897 break;
9898
9899 case DW_TAG_imported_unit:
9900 process_imported_unit_die (die, cu);
9901 break;
9902
9903 case DW_TAG_variable:
9904 read_variable (die, cu);
9905 break;
9906
9907 default:
9908 new_symbol (die, NULL, cu);
9909 break;
9910 }
9911 }
9912 \f
9913 /* DWARF name computation. */
9914
9915 /* A helper function for dwarf2_compute_name which determines whether DIE
9916 needs to have the name of the scope prepended to the name listed in the
9917 die. */
9918
9919 static int
9920 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9921 {
9922 struct attribute *attr;
9923
9924 switch (die->tag)
9925 {
9926 case DW_TAG_namespace:
9927 case DW_TAG_typedef:
9928 case DW_TAG_class_type:
9929 case DW_TAG_interface_type:
9930 case DW_TAG_structure_type:
9931 case DW_TAG_union_type:
9932 case DW_TAG_enumeration_type:
9933 case DW_TAG_enumerator:
9934 case DW_TAG_subprogram:
9935 case DW_TAG_inlined_subroutine:
9936 case DW_TAG_member:
9937 case DW_TAG_imported_declaration:
9938 return 1;
9939
9940 case DW_TAG_variable:
9941 case DW_TAG_constant:
9942 /* We only need to prefix "globally" visible variables. These include
9943 any variable marked with DW_AT_external or any variable that
9944 lives in a namespace. [Variables in anonymous namespaces
9945 require prefixing, but they are not DW_AT_external.] */
9946
9947 if (dwarf2_attr (die, DW_AT_specification, cu))
9948 {
9949 struct dwarf2_cu *spec_cu = cu;
9950
9951 return die_needs_namespace (die_specification (die, &spec_cu),
9952 spec_cu);
9953 }
9954
9955 attr = dwarf2_attr (die, DW_AT_external, cu);
9956 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9957 && die->parent->tag != DW_TAG_module)
9958 return 0;
9959 /* A variable in a lexical block of some kind does not need a
9960 namespace, even though in C++ such variables may be external
9961 and have a mangled name. */
9962 if (die->parent->tag == DW_TAG_lexical_block
9963 || die->parent->tag == DW_TAG_try_block
9964 || die->parent->tag == DW_TAG_catch_block
9965 || die->parent->tag == DW_TAG_subprogram)
9966 return 0;
9967 return 1;
9968
9969 default:
9970 return 0;
9971 }
9972 }
9973
9974 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9975 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9976 defined for the given DIE. */
9977
9978 static struct attribute *
9979 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9980 {
9981 struct attribute *attr;
9982
9983 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9984 if (attr == NULL)
9985 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9986
9987 return attr;
9988 }
9989
9990 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9991 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9992 defined for the given DIE. */
9993
9994 static const char *
9995 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9996 {
9997 const char *linkage_name;
9998
9999 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10000 if (linkage_name == NULL)
10001 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10002
10003 return linkage_name;
10004 }
10005
10006 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10007 compute the physname for the object, which include a method's:
10008 - formal parameters (C++),
10009 - receiver type (Go),
10010
10011 The term "physname" is a bit confusing.
10012 For C++, for example, it is the demangled name.
10013 For Go, for example, it's the mangled name.
10014
10015 For Ada, return the DIE's linkage name rather than the fully qualified
10016 name. PHYSNAME is ignored..
10017
10018 The result is allocated on the objfile_obstack and canonicalized. */
10019
10020 static const char *
10021 dwarf2_compute_name (const char *name,
10022 struct die_info *die, struct dwarf2_cu *cu,
10023 int physname)
10024 {
10025 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10026
10027 if (name == NULL)
10028 name = dwarf2_name (die, cu);
10029
10030 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10031 but otherwise compute it by typename_concat inside GDB.
10032 FIXME: Actually this is not really true, or at least not always true.
10033 It's all very confusing. compute_and_set_names doesn't try to demangle
10034 Fortran names because there is no mangling standard. So new_symbol
10035 will set the demangled name to the result of dwarf2_full_name, and it is
10036 the demangled name that GDB uses if it exists. */
10037 if (cu->language == language_ada
10038 || (cu->language == language_fortran && physname))
10039 {
10040 /* For Ada unit, we prefer the linkage name over the name, as
10041 the former contains the exported name, which the user expects
10042 to be able to reference. Ideally, we want the user to be able
10043 to reference this entity using either natural or linkage name,
10044 but we haven't started looking at this enhancement yet. */
10045 const char *linkage_name = dw2_linkage_name (die, cu);
10046
10047 if (linkage_name != NULL)
10048 return linkage_name;
10049 }
10050
10051 /* These are the only languages we know how to qualify names in. */
10052 if (name != NULL
10053 && (cu->language == language_cplus
10054 || cu->language == language_fortran || cu->language == language_d
10055 || cu->language == language_rust))
10056 {
10057 if (die_needs_namespace (die, cu))
10058 {
10059 const char *prefix;
10060 const char *canonical_name = NULL;
10061
10062 string_file buf;
10063
10064 prefix = determine_prefix (die, cu);
10065 if (*prefix != '\0')
10066 {
10067 gdb::unique_xmalloc_ptr<char> prefixed_name
10068 (typename_concat (NULL, prefix, name, physname, cu));
10069
10070 buf.puts (prefixed_name.get ());
10071 }
10072 else
10073 buf.puts (name);
10074
10075 /* Template parameters may be specified in the DIE's DW_AT_name, or
10076 as children with DW_TAG_template_type_param or
10077 DW_TAG_value_type_param. If the latter, add them to the name
10078 here. If the name already has template parameters, then
10079 skip this step; some versions of GCC emit both, and
10080 it is more efficient to use the pre-computed name.
10081
10082 Something to keep in mind about this process: it is very
10083 unlikely, or in some cases downright impossible, to produce
10084 something that will match the mangled name of a function.
10085 If the definition of the function has the same debug info,
10086 we should be able to match up with it anyway. But fallbacks
10087 using the minimal symbol, for instance to find a method
10088 implemented in a stripped copy of libstdc++, will not work.
10089 If we do not have debug info for the definition, we will have to
10090 match them up some other way.
10091
10092 When we do name matching there is a related problem with function
10093 templates; two instantiated function templates are allowed to
10094 differ only by their return types, which we do not add here. */
10095
10096 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10097 {
10098 struct attribute *attr;
10099 struct die_info *child;
10100 int first = 1;
10101
10102 die->building_fullname = 1;
10103
10104 for (child = die->child; child != NULL; child = child->sibling)
10105 {
10106 struct type *type;
10107 LONGEST value;
10108 const gdb_byte *bytes;
10109 struct dwarf2_locexpr_baton *baton;
10110 struct value *v;
10111
10112 if (child->tag != DW_TAG_template_type_param
10113 && child->tag != DW_TAG_template_value_param)
10114 continue;
10115
10116 if (first)
10117 {
10118 buf.puts ("<");
10119 first = 0;
10120 }
10121 else
10122 buf.puts (", ");
10123
10124 attr = dwarf2_attr (child, DW_AT_type, cu);
10125 if (attr == NULL)
10126 {
10127 complaint (_("template parameter missing DW_AT_type"));
10128 buf.puts ("UNKNOWN_TYPE");
10129 continue;
10130 }
10131 type = die_type (child, cu);
10132
10133 if (child->tag == DW_TAG_template_type_param)
10134 {
10135 c_print_type (type, "", &buf, -1, 0, cu->language,
10136 &type_print_raw_options);
10137 continue;
10138 }
10139
10140 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10141 if (attr == NULL)
10142 {
10143 complaint (_("template parameter missing "
10144 "DW_AT_const_value"));
10145 buf.puts ("UNKNOWN_VALUE");
10146 continue;
10147 }
10148
10149 dwarf2_const_value_attr (attr, type, name,
10150 &cu->comp_unit_obstack, cu,
10151 &value, &bytes, &baton);
10152
10153 if (TYPE_NOSIGN (type))
10154 /* GDB prints characters as NUMBER 'CHAR'. If that's
10155 changed, this can use value_print instead. */
10156 c_printchar (value, type, &buf);
10157 else
10158 {
10159 struct value_print_options opts;
10160
10161 if (baton != NULL)
10162 v = dwarf2_evaluate_loc_desc (type, NULL,
10163 baton->data,
10164 baton->size,
10165 baton->per_cu);
10166 else if (bytes != NULL)
10167 {
10168 v = allocate_value (type);
10169 memcpy (value_contents_writeable (v), bytes,
10170 TYPE_LENGTH (type));
10171 }
10172 else
10173 v = value_from_longest (type, value);
10174
10175 /* Specify decimal so that we do not depend on
10176 the radix. */
10177 get_formatted_print_options (&opts, 'd');
10178 opts.raw = 1;
10179 value_print (v, &buf, &opts);
10180 release_value (v);
10181 }
10182 }
10183
10184 die->building_fullname = 0;
10185
10186 if (!first)
10187 {
10188 /* Close the argument list, with a space if necessary
10189 (nested templates). */
10190 if (!buf.empty () && buf.string ().back () == '>')
10191 buf.puts (" >");
10192 else
10193 buf.puts (">");
10194 }
10195 }
10196
10197 /* For C++ methods, append formal parameter type
10198 information, if PHYSNAME. */
10199
10200 if (physname && die->tag == DW_TAG_subprogram
10201 && cu->language == language_cplus)
10202 {
10203 struct type *type = read_type_die (die, cu);
10204
10205 c_type_print_args (type, &buf, 1, cu->language,
10206 &type_print_raw_options);
10207
10208 if (cu->language == language_cplus)
10209 {
10210 /* Assume that an artificial first parameter is
10211 "this", but do not crash if it is not. RealView
10212 marks unnamed (and thus unused) parameters as
10213 artificial; there is no way to differentiate
10214 the two cases. */
10215 if (TYPE_NFIELDS (type) > 0
10216 && TYPE_FIELD_ARTIFICIAL (type, 0)
10217 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
10218 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10219 0))))
10220 buf.puts (" const");
10221 }
10222 }
10223
10224 const std::string &intermediate_name = buf.string ();
10225
10226 if (cu->language == language_cplus)
10227 canonical_name
10228 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10229 objfile);
10230
10231 /* If we only computed INTERMEDIATE_NAME, or if
10232 INTERMEDIATE_NAME is already canonical, then we need to
10233 intern it. */
10234 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10235 name = objfile->intern (intermediate_name);
10236 else
10237 name = canonical_name;
10238 }
10239 }
10240
10241 return name;
10242 }
10243
10244 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10245 If scope qualifiers are appropriate they will be added. The result
10246 will be allocated on the storage_obstack, or NULL if the DIE does
10247 not have a name. NAME may either be from a previous call to
10248 dwarf2_name or NULL.
10249
10250 The output string will be canonicalized (if C++). */
10251
10252 static const char *
10253 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10254 {
10255 return dwarf2_compute_name (name, die, cu, 0);
10256 }
10257
10258 /* Construct a physname for the given DIE in CU. NAME may either be
10259 from a previous call to dwarf2_name or NULL. The result will be
10260 allocated on the objfile_objstack or NULL if the DIE does not have a
10261 name.
10262
10263 The output string will be canonicalized (if C++). */
10264
10265 static const char *
10266 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10267 {
10268 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10269 const char *retval, *mangled = NULL, *canon = NULL;
10270 int need_copy = 1;
10271
10272 /* In this case dwarf2_compute_name is just a shortcut not building anything
10273 on its own. */
10274 if (!die_needs_namespace (die, cu))
10275 return dwarf2_compute_name (name, die, cu, 1);
10276
10277 mangled = dw2_linkage_name (die, cu);
10278
10279 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10280 See https://github.com/rust-lang/rust/issues/32925. */
10281 if (cu->language == language_rust && mangled != NULL
10282 && strchr (mangled, '{') != NULL)
10283 mangled = NULL;
10284
10285 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10286 has computed. */
10287 gdb::unique_xmalloc_ptr<char> demangled;
10288 if (mangled != NULL)
10289 {
10290
10291 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10292 {
10293 /* Do nothing (do not demangle the symbol name). */
10294 }
10295 else if (cu->language == language_go)
10296 {
10297 /* This is a lie, but we already lie to the caller new_symbol.
10298 new_symbol assumes we return the mangled name.
10299 This just undoes that lie until things are cleaned up. */
10300 }
10301 else
10302 {
10303 /* Use DMGL_RET_DROP for C++ template functions to suppress
10304 their return type. It is easier for GDB users to search
10305 for such functions as `name(params)' than `long name(params)'.
10306 In such case the minimal symbol names do not match the full
10307 symbol names but for template functions there is never a need
10308 to look up their definition from their declaration so
10309 the only disadvantage remains the minimal symbol variant
10310 `long name(params)' does not have the proper inferior type. */
10311 demangled.reset (gdb_demangle (mangled,
10312 (DMGL_PARAMS | DMGL_ANSI
10313 | DMGL_RET_DROP)));
10314 }
10315 if (demangled)
10316 canon = demangled.get ();
10317 else
10318 {
10319 canon = mangled;
10320 need_copy = 0;
10321 }
10322 }
10323
10324 if (canon == NULL || check_physname)
10325 {
10326 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10327
10328 if (canon != NULL && strcmp (physname, canon) != 0)
10329 {
10330 /* It may not mean a bug in GDB. The compiler could also
10331 compute DW_AT_linkage_name incorrectly. But in such case
10332 GDB would need to be bug-to-bug compatible. */
10333
10334 complaint (_("Computed physname <%s> does not match demangled <%s> "
10335 "(from linkage <%s>) - DIE at %s [in module %s]"),
10336 physname, canon, mangled, sect_offset_str (die->sect_off),
10337 objfile_name (objfile));
10338
10339 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10340 is available here - over computed PHYSNAME. It is safer
10341 against both buggy GDB and buggy compilers. */
10342
10343 retval = canon;
10344 }
10345 else
10346 {
10347 retval = physname;
10348 need_copy = 0;
10349 }
10350 }
10351 else
10352 retval = canon;
10353
10354 if (need_copy)
10355 retval = objfile->intern (retval);
10356
10357 return retval;
10358 }
10359
10360 /* Inspect DIE in CU for a namespace alias. If one exists, record
10361 a new symbol for it.
10362
10363 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10364
10365 static int
10366 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10367 {
10368 struct attribute *attr;
10369
10370 /* If the die does not have a name, this is not a namespace
10371 alias. */
10372 attr = dwarf2_attr (die, DW_AT_name, cu);
10373 if (attr != NULL)
10374 {
10375 int num;
10376 struct die_info *d = die;
10377 struct dwarf2_cu *imported_cu = cu;
10378
10379 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10380 keep inspecting DIEs until we hit the underlying import. */
10381 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10382 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10383 {
10384 attr = dwarf2_attr (d, DW_AT_import, cu);
10385 if (attr == NULL)
10386 break;
10387
10388 d = follow_die_ref (d, attr, &imported_cu);
10389 if (d->tag != DW_TAG_imported_declaration)
10390 break;
10391 }
10392
10393 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10394 {
10395 complaint (_("DIE at %s has too many recursively imported "
10396 "declarations"), sect_offset_str (d->sect_off));
10397 return 0;
10398 }
10399
10400 if (attr != NULL)
10401 {
10402 struct type *type;
10403 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10404
10405 type = get_die_type_at_offset (sect_off, cu->per_cu);
10406 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
10407 {
10408 /* This declaration is a global namespace alias. Add
10409 a symbol for it whose type is the aliased namespace. */
10410 new_symbol (die, type, cu);
10411 return 1;
10412 }
10413 }
10414 }
10415
10416 return 0;
10417 }
10418
10419 /* Return the using directives repository (global or local?) to use in the
10420 current context for CU.
10421
10422 For Ada, imported declarations can materialize renamings, which *may* be
10423 global. However it is impossible (for now?) in DWARF to distinguish
10424 "external" imported declarations and "static" ones. As all imported
10425 declarations seem to be static in all other languages, make them all CU-wide
10426 global only in Ada. */
10427
10428 static struct using_direct **
10429 using_directives (struct dwarf2_cu *cu)
10430 {
10431 if (cu->language == language_ada
10432 && cu->get_builder ()->outermost_context_p ())
10433 return cu->get_builder ()->get_global_using_directives ();
10434 else
10435 return cu->get_builder ()->get_local_using_directives ();
10436 }
10437
10438 /* Read the import statement specified by the given die and record it. */
10439
10440 static void
10441 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10442 {
10443 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10444 struct attribute *import_attr;
10445 struct die_info *imported_die, *child_die;
10446 struct dwarf2_cu *imported_cu;
10447 const char *imported_name;
10448 const char *imported_name_prefix;
10449 const char *canonical_name;
10450 const char *import_alias;
10451 const char *imported_declaration = NULL;
10452 const char *import_prefix;
10453 std::vector<const char *> excludes;
10454
10455 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10456 if (import_attr == NULL)
10457 {
10458 complaint (_("Tag '%s' has no DW_AT_import"),
10459 dwarf_tag_name (die->tag));
10460 return;
10461 }
10462
10463 imported_cu = cu;
10464 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10465 imported_name = dwarf2_name (imported_die, imported_cu);
10466 if (imported_name == NULL)
10467 {
10468 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10469
10470 The import in the following code:
10471 namespace A
10472 {
10473 typedef int B;
10474 }
10475
10476 int main ()
10477 {
10478 using A::B;
10479 B b;
10480 return b;
10481 }
10482
10483 ...
10484 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10485 <52> DW_AT_decl_file : 1
10486 <53> DW_AT_decl_line : 6
10487 <54> DW_AT_import : <0x75>
10488 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10489 <59> DW_AT_name : B
10490 <5b> DW_AT_decl_file : 1
10491 <5c> DW_AT_decl_line : 2
10492 <5d> DW_AT_type : <0x6e>
10493 ...
10494 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10495 <76> DW_AT_byte_size : 4
10496 <77> DW_AT_encoding : 5 (signed)
10497
10498 imports the wrong die ( 0x75 instead of 0x58 ).
10499 This case will be ignored until the gcc bug is fixed. */
10500 return;
10501 }
10502
10503 /* Figure out the local name after import. */
10504 import_alias = dwarf2_name (die, cu);
10505
10506 /* Figure out where the statement is being imported to. */
10507 import_prefix = determine_prefix (die, cu);
10508
10509 /* Figure out what the scope of the imported die is and prepend it
10510 to the name of the imported die. */
10511 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10512
10513 if (imported_die->tag != DW_TAG_namespace
10514 && imported_die->tag != DW_TAG_module)
10515 {
10516 imported_declaration = imported_name;
10517 canonical_name = imported_name_prefix;
10518 }
10519 else if (strlen (imported_name_prefix) > 0)
10520 canonical_name = obconcat (&objfile->objfile_obstack,
10521 imported_name_prefix,
10522 (cu->language == language_d ? "." : "::"),
10523 imported_name, (char *) NULL);
10524 else
10525 canonical_name = imported_name;
10526
10527 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10528 for (child_die = die->child; child_die && child_die->tag;
10529 child_die = sibling_die (child_die))
10530 {
10531 /* DWARF-4: A Fortran use statement with a “rename list” may be
10532 represented by an imported module entry with an import attribute
10533 referring to the module and owned entries corresponding to those
10534 entities that are renamed as part of being imported. */
10535
10536 if (child_die->tag != DW_TAG_imported_declaration)
10537 {
10538 complaint (_("child DW_TAG_imported_declaration expected "
10539 "- DIE at %s [in module %s]"),
10540 sect_offset_str (child_die->sect_off),
10541 objfile_name (objfile));
10542 continue;
10543 }
10544
10545 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10546 if (import_attr == NULL)
10547 {
10548 complaint (_("Tag '%s' has no DW_AT_import"),
10549 dwarf_tag_name (child_die->tag));
10550 continue;
10551 }
10552
10553 imported_cu = cu;
10554 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10555 &imported_cu);
10556 imported_name = dwarf2_name (imported_die, imported_cu);
10557 if (imported_name == NULL)
10558 {
10559 complaint (_("child DW_TAG_imported_declaration has unknown "
10560 "imported name - DIE at %s [in module %s]"),
10561 sect_offset_str (child_die->sect_off),
10562 objfile_name (objfile));
10563 continue;
10564 }
10565
10566 excludes.push_back (imported_name);
10567
10568 process_die (child_die, cu);
10569 }
10570
10571 add_using_directive (using_directives (cu),
10572 import_prefix,
10573 canonical_name,
10574 import_alias,
10575 imported_declaration,
10576 excludes,
10577 0,
10578 &objfile->objfile_obstack);
10579 }
10580
10581 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10582 types, but gives them a size of zero. Starting with version 14,
10583 ICC is compatible with GCC. */
10584
10585 static bool
10586 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10587 {
10588 if (!cu->checked_producer)
10589 check_producer (cu);
10590
10591 return cu->producer_is_icc_lt_14;
10592 }
10593
10594 /* ICC generates a DW_AT_type for C void functions. This was observed on
10595 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10596 which says that void functions should not have a DW_AT_type. */
10597
10598 static bool
10599 producer_is_icc (struct dwarf2_cu *cu)
10600 {
10601 if (!cu->checked_producer)
10602 check_producer (cu);
10603
10604 return cu->producer_is_icc;
10605 }
10606
10607 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10608 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10609 this, it was first present in GCC release 4.3.0. */
10610
10611 static bool
10612 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10613 {
10614 if (!cu->checked_producer)
10615 check_producer (cu);
10616
10617 return cu->producer_is_gcc_lt_4_3;
10618 }
10619
10620 static file_and_directory
10621 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10622 {
10623 file_and_directory res;
10624
10625 /* Find the filename. Do not use dwarf2_name here, since the filename
10626 is not a source language identifier. */
10627 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10628 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10629
10630 if (res.comp_dir == NULL
10631 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10632 && IS_ABSOLUTE_PATH (res.name))
10633 {
10634 res.comp_dir_storage = ldirname (res.name);
10635 if (!res.comp_dir_storage.empty ())
10636 res.comp_dir = res.comp_dir_storage.c_str ();
10637 }
10638 if (res.comp_dir != NULL)
10639 {
10640 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10641 directory, get rid of it. */
10642 const char *cp = strchr (res.comp_dir, ':');
10643
10644 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10645 res.comp_dir = cp + 1;
10646 }
10647
10648 if (res.name == NULL)
10649 res.name = "<unknown>";
10650
10651 return res;
10652 }
10653
10654 /* Handle DW_AT_stmt_list for a compilation unit.
10655 DIE is the DW_TAG_compile_unit die for CU.
10656 COMP_DIR is the compilation directory. LOWPC is passed to
10657 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10658
10659 static void
10660 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10661 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10662 {
10663 struct dwarf2_per_objfile *dwarf2_per_objfile
10664 = cu->per_cu->dwarf2_per_objfile;
10665 struct attribute *attr;
10666 struct line_header line_header_local;
10667 hashval_t line_header_local_hash;
10668 void **slot;
10669 int decode_mapping;
10670
10671 gdb_assert (! cu->per_cu->is_debug_types);
10672
10673 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10674 if (attr == NULL)
10675 return;
10676
10677 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10678
10679 /* The line header hash table is only created if needed (it exists to
10680 prevent redundant reading of the line table for partial_units).
10681 If we're given a partial_unit, we'll need it. If we're given a
10682 compile_unit, then use the line header hash table if it's already
10683 created, but don't create one just yet. */
10684
10685 if (dwarf2_per_objfile->line_header_hash == NULL
10686 && die->tag == DW_TAG_partial_unit)
10687 {
10688 dwarf2_per_objfile->line_header_hash
10689 .reset (htab_create_alloc (127, line_header_hash_voidp,
10690 line_header_eq_voidp,
10691 free_line_header_voidp,
10692 xcalloc, xfree));
10693 }
10694
10695 line_header_local.sect_off = line_offset;
10696 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10697 line_header_local_hash = line_header_hash (&line_header_local);
10698 if (dwarf2_per_objfile->line_header_hash != NULL)
10699 {
10700 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10701 &line_header_local,
10702 line_header_local_hash, NO_INSERT);
10703
10704 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10705 is not present in *SLOT (since if there is something in *SLOT then
10706 it will be for a partial_unit). */
10707 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10708 {
10709 gdb_assert (*slot != NULL);
10710 cu->line_header = (struct line_header *) *slot;
10711 return;
10712 }
10713 }
10714
10715 /* dwarf_decode_line_header does not yet provide sufficient information.
10716 We always have to call also dwarf_decode_lines for it. */
10717 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10718 if (lh == NULL)
10719 return;
10720
10721 cu->line_header = lh.release ();
10722 cu->line_header_die_owner = die;
10723
10724 if (dwarf2_per_objfile->line_header_hash == NULL)
10725 slot = NULL;
10726 else
10727 {
10728 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10729 &line_header_local,
10730 line_header_local_hash, INSERT);
10731 gdb_assert (slot != NULL);
10732 }
10733 if (slot != NULL && *slot == NULL)
10734 {
10735 /* This newly decoded line number information unit will be owned
10736 by line_header_hash hash table. */
10737 *slot = cu->line_header;
10738 cu->line_header_die_owner = NULL;
10739 }
10740 else
10741 {
10742 /* We cannot free any current entry in (*slot) as that struct line_header
10743 may be already used by multiple CUs. Create only temporary decoded
10744 line_header for this CU - it may happen at most once for each line
10745 number information unit. And if we're not using line_header_hash
10746 then this is what we want as well. */
10747 gdb_assert (die->tag != DW_TAG_partial_unit);
10748 }
10749 decode_mapping = (die->tag != DW_TAG_partial_unit);
10750 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10751 decode_mapping);
10752
10753 }
10754
10755 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10756
10757 static void
10758 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10759 {
10760 struct dwarf2_per_objfile *dwarf2_per_objfile
10761 = cu->per_cu->dwarf2_per_objfile;
10762 struct objfile *objfile = dwarf2_per_objfile->objfile;
10763 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10764 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10765 CORE_ADDR highpc = ((CORE_ADDR) 0);
10766 struct attribute *attr;
10767 struct die_info *child_die;
10768 CORE_ADDR baseaddr;
10769
10770 prepare_one_comp_unit (cu, die, cu->language);
10771 baseaddr = objfile->text_section_offset ();
10772
10773 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10774
10775 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10776 from finish_block. */
10777 if (lowpc == ((CORE_ADDR) -1))
10778 lowpc = highpc;
10779 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10780
10781 file_and_directory fnd = find_file_and_directory (die, cu);
10782
10783 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10784 standardised yet. As a workaround for the language detection we fall
10785 back to the DW_AT_producer string. */
10786 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10787 cu->language = language_opencl;
10788
10789 /* Similar hack for Go. */
10790 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10791 set_cu_language (DW_LANG_Go, cu);
10792
10793 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
10794
10795 /* Decode line number information if present. We do this before
10796 processing child DIEs, so that the line header table is available
10797 for DW_AT_decl_file. */
10798 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
10799
10800 /* Process all dies in compilation unit. */
10801 if (die->child != NULL)
10802 {
10803 child_die = die->child;
10804 while (child_die && child_die->tag)
10805 {
10806 process_die (child_die, cu);
10807 child_die = sibling_die (child_die);
10808 }
10809 }
10810
10811 /* Decode macro information, if present. Dwarf 2 macro information
10812 refers to information in the line number info statement program
10813 header, so we can only read it if we've read the header
10814 successfully. */
10815 attr = dwarf2_attr (die, DW_AT_macros, cu);
10816 if (attr == NULL)
10817 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
10818 if (attr && cu->line_header)
10819 {
10820 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10821 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
10822
10823 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
10824 }
10825 else
10826 {
10827 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10828 if (attr && cu->line_header)
10829 {
10830 unsigned int macro_offset = DW_UNSND (attr);
10831
10832 dwarf_decode_macros (cu, macro_offset, 0);
10833 }
10834 }
10835 }
10836
10837 void
10838 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
10839 {
10840 struct type_unit_group *tu_group;
10841 int first_time;
10842 struct attribute *attr;
10843 unsigned int i;
10844 struct signatured_type *sig_type;
10845
10846 gdb_assert (per_cu->is_debug_types);
10847 sig_type = (struct signatured_type *) per_cu;
10848
10849 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
10850
10851 /* If we're using .gdb_index (includes -readnow) then
10852 per_cu->type_unit_group may not have been set up yet. */
10853 if (sig_type->type_unit_group == NULL)
10854 sig_type->type_unit_group = get_type_unit_group (this, attr);
10855 tu_group = sig_type->type_unit_group;
10856
10857 /* If we've already processed this stmt_list there's no real need to
10858 do it again, we could fake it and just recreate the part we need
10859 (file name,index -> symtab mapping). If data shows this optimization
10860 is useful we can do it then. */
10861 first_time = tu_group->compunit_symtab == NULL;
10862
10863 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10864 debug info. */
10865 line_header_up lh;
10866 if (attr != NULL)
10867 {
10868 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10869 lh = dwarf_decode_line_header (line_offset, this);
10870 }
10871 if (lh == NULL)
10872 {
10873 if (first_time)
10874 start_symtab ("", NULL, 0);
10875 else
10876 {
10877 gdb_assert (tu_group->symtabs == NULL);
10878 gdb_assert (m_builder == nullptr);
10879 struct compunit_symtab *cust = tu_group->compunit_symtab;
10880 m_builder.reset (new struct buildsym_compunit
10881 (COMPUNIT_OBJFILE (cust), "",
10882 COMPUNIT_DIRNAME (cust),
10883 compunit_language (cust),
10884 0, cust));
10885 }
10886 return;
10887 }
10888
10889 line_header = lh.release ();
10890 line_header_die_owner = die;
10891
10892 if (first_time)
10893 {
10894 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
10895
10896 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10897 still initializing it, and our caller (a few levels up)
10898 process_full_type_unit still needs to know if this is the first
10899 time. */
10900
10901 tu_group->symtabs
10902 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
10903 struct symtab *, line_header->file_names_size ());
10904
10905 auto &file_names = line_header->file_names ();
10906 for (i = 0; i < file_names.size (); ++i)
10907 {
10908 file_entry &fe = file_names[i];
10909 dwarf2_start_subfile (this, fe.name,
10910 fe.include_dir (line_header));
10911 buildsym_compunit *b = get_builder ();
10912 if (b->get_current_subfile ()->symtab == NULL)
10913 {
10914 /* NOTE: start_subfile will recognize when it's been
10915 passed a file it has already seen. So we can't
10916 assume there's a simple mapping from
10917 cu->line_header->file_names to subfiles, plus
10918 cu->line_header->file_names may contain dups. */
10919 b->get_current_subfile ()->symtab
10920 = allocate_symtab (cust, b->get_current_subfile ()->name);
10921 }
10922
10923 fe.symtab = b->get_current_subfile ()->symtab;
10924 tu_group->symtabs[i] = fe.symtab;
10925 }
10926 }
10927 else
10928 {
10929 gdb_assert (m_builder == nullptr);
10930 struct compunit_symtab *cust = tu_group->compunit_symtab;
10931 m_builder.reset (new struct buildsym_compunit
10932 (COMPUNIT_OBJFILE (cust), "",
10933 COMPUNIT_DIRNAME (cust),
10934 compunit_language (cust),
10935 0, cust));
10936
10937 auto &file_names = line_header->file_names ();
10938 for (i = 0; i < file_names.size (); ++i)
10939 {
10940 file_entry &fe = file_names[i];
10941 fe.symtab = tu_group->symtabs[i];
10942 }
10943 }
10944
10945 /* The main symtab is allocated last. Type units don't have DW_AT_name
10946 so they don't have a "real" (so to speak) symtab anyway.
10947 There is later code that will assign the main symtab to all symbols
10948 that don't have one. We need to handle the case of a symbol with a
10949 missing symtab (DW_AT_decl_file) anyway. */
10950 }
10951
10952 /* Process DW_TAG_type_unit.
10953 For TUs we want to skip the first top level sibling if it's not the
10954 actual type being defined by this TU. In this case the first top
10955 level sibling is there to provide context only. */
10956
10957 static void
10958 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10959 {
10960 struct die_info *child_die;
10961
10962 prepare_one_comp_unit (cu, die, language_minimal);
10963
10964 /* Initialize (or reinitialize) the machinery for building symtabs.
10965 We do this before processing child DIEs, so that the line header table
10966 is available for DW_AT_decl_file. */
10967 cu->setup_type_unit_groups (die);
10968
10969 if (die->child != NULL)
10970 {
10971 child_die = die->child;
10972 while (child_die && child_die->tag)
10973 {
10974 process_die (child_die, cu);
10975 child_die = sibling_die (child_die);
10976 }
10977 }
10978 }
10979 \f
10980 /* DWO/DWP files.
10981
10982 http://gcc.gnu.org/wiki/DebugFission
10983 http://gcc.gnu.org/wiki/DebugFissionDWP
10984
10985 To simplify handling of both DWO files ("object" files with the DWARF info)
10986 and DWP files (a file with the DWOs packaged up into one file), we treat
10987 DWP files as having a collection of virtual DWO files. */
10988
10989 static hashval_t
10990 hash_dwo_file (const void *item)
10991 {
10992 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
10993 hashval_t hash;
10994
10995 hash = htab_hash_string (dwo_file->dwo_name);
10996 if (dwo_file->comp_dir != NULL)
10997 hash += htab_hash_string (dwo_file->comp_dir);
10998 return hash;
10999 }
11000
11001 static int
11002 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11003 {
11004 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11005 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11006
11007 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11008 return 0;
11009 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11010 return lhs->comp_dir == rhs->comp_dir;
11011 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11012 }
11013
11014 /* Allocate a hash table for DWO files. */
11015
11016 static htab_up
11017 allocate_dwo_file_hash_table ()
11018 {
11019 auto delete_dwo_file = [] (void *item)
11020 {
11021 struct dwo_file *dwo_file = (struct dwo_file *) item;
11022
11023 delete dwo_file;
11024 };
11025
11026 return htab_up (htab_create_alloc (41,
11027 hash_dwo_file,
11028 eq_dwo_file,
11029 delete_dwo_file,
11030 xcalloc, xfree));
11031 }
11032
11033 /* Lookup DWO file DWO_NAME. */
11034
11035 static void **
11036 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11037 const char *dwo_name,
11038 const char *comp_dir)
11039 {
11040 struct dwo_file find_entry;
11041 void **slot;
11042
11043 if (dwarf2_per_objfile->dwo_files == NULL)
11044 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
11045
11046 find_entry.dwo_name = dwo_name;
11047 find_entry.comp_dir = comp_dir;
11048 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11049 INSERT);
11050
11051 return slot;
11052 }
11053
11054 static hashval_t
11055 hash_dwo_unit (const void *item)
11056 {
11057 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11058
11059 /* This drops the top 32 bits of the id, but is ok for a hash. */
11060 return dwo_unit->signature;
11061 }
11062
11063 static int
11064 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11065 {
11066 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11067 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11068
11069 /* The signature is assumed to be unique within the DWO file.
11070 So while object file CU dwo_id's always have the value zero,
11071 that's OK, assuming each object file DWO file has only one CU,
11072 and that's the rule for now. */
11073 return lhs->signature == rhs->signature;
11074 }
11075
11076 /* Allocate a hash table for DWO CUs,TUs.
11077 There is one of these tables for each of CUs,TUs for each DWO file. */
11078
11079 static htab_up
11080 allocate_dwo_unit_table ()
11081 {
11082 /* Start out with a pretty small number.
11083 Generally DWO files contain only one CU and maybe some TUs. */
11084 return htab_up (htab_create_alloc (3,
11085 hash_dwo_unit,
11086 eq_dwo_unit,
11087 NULL, xcalloc, xfree));
11088 }
11089
11090 /* die_reader_func for create_dwo_cu. */
11091
11092 static void
11093 create_dwo_cu_reader (const struct die_reader_specs *reader,
11094 const gdb_byte *info_ptr,
11095 struct die_info *comp_unit_die,
11096 struct dwo_file *dwo_file,
11097 struct dwo_unit *dwo_unit)
11098 {
11099 struct dwarf2_cu *cu = reader->cu;
11100 sect_offset sect_off = cu->per_cu->sect_off;
11101 struct dwarf2_section_info *section = cu->per_cu->section;
11102
11103 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11104 if (!signature.has_value ())
11105 {
11106 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11107 " its dwo_id [in module %s]"),
11108 sect_offset_str (sect_off), dwo_file->dwo_name);
11109 return;
11110 }
11111
11112 dwo_unit->dwo_file = dwo_file;
11113 dwo_unit->signature = *signature;
11114 dwo_unit->section = section;
11115 dwo_unit->sect_off = sect_off;
11116 dwo_unit->length = cu->per_cu->length;
11117
11118 if (dwarf_read_debug)
11119 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11120 sect_offset_str (sect_off),
11121 hex_string (dwo_unit->signature));
11122 }
11123
11124 /* Create the dwo_units for the CUs in a DWO_FILE.
11125 Note: This function processes DWO files only, not DWP files. */
11126
11127 static void
11128 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11129 dwarf2_cu *cu, struct dwo_file &dwo_file,
11130 dwarf2_section_info &section, htab_up &cus_htab)
11131 {
11132 struct objfile *objfile = dwarf2_per_objfile->objfile;
11133 const gdb_byte *info_ptr, *end_ptr;
11134
11135 section.read (objfile);
11136 info_ptr = section.buffer;
11137
11138 if (info_ptr == NULL)
11139 return;
11140
11141 if (dwarf_read_debug)
11142 {
11143 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11144 section.get_name (),
11145 section.get_file_name ());
11146 }
11147
11148 end_ptr = info_ptr + section.size;
11149 while (info_ptr < end_ptr)
11150 {
11151 struct dwarf2_per_cu_data per_cu;
11152 struct dwo_unit read_unit {};
11153 struct dwo_unit *dwo_unit;
11154 void **slot;
11155 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11156
11157 memset (&per_cu, 0, sizeof (per_cu));
11158 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11159 per_cu.is_debug_types = 0;
11160 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11161 per_cu.section = &section;
11162
11163 cutu_reader reader (&per_cu, cu, &dwo_file);
11164 if (!reader.dummy_p)
11165 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11166 &dwo_file, &read_unit);
11167 info_ptr += per_cu.length;
11168
11169 // If the unit could not be parsed, skip it.
11170 if (read_unit.dwo_file == NULL)
11171 continue;
11172
11173 if (cus_htab == NULL)
11174 cus_htab = allocate_dwo_unit_table ();
11175
11176 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11177 *dwo_unit = read_unit;
11178 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11179 gdb_assert (slot != NULL);
11180 if (*slot != NULL)
11181 {
11182 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11183 sect_offset dup_sect_off = dup_cu->sect_off;
11184
11185 complaint (_("debug cu entry at offset %s is duplicate to"
11186 " the entry at offset %s, signature %s"),
11187 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11188 hex_string (dwo_unit->signature));
11189 }
11190 *slot = (void *)dwo_unit;
11191 }
11192 }
11193
11194 /* DWP file .debug_{cu,tu}_index section format:
11195 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11196
11197 DWP Version 1:
11198
11199 Both index sections have the same format, and serve to map a 64-bit
11200 signature to a set of section numbers. Each section begins with a header,
11201 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11202 indexes, and a pool of 32-bit section numbers. The index sections will be
11203 aligned at 8-byte boundaries in the file.
11204
11205 The index section header consists of:
11206
11207 V, 32 bit version number
11208 -, 32 bits unused
11209 N, 32 bit number of compilation units or type units in the index
11210 M, 32 bit number of slots in the hash table
11211
11212 Numbers are recorded using the byte order of the application binary.
11213
11214 The hash table begins at offset 16 in the section, and consists of an array
11215 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11216 order of the application binary). Unused slots in the hash table are 0.
11217 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11218
11219 The parallel table begins immediately after the hash table
11220 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11221 array of 32-bit indexes (using the byte order of the application binary),
11222 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11223 table contains a 32-bit index into the pool of section numbers. For unused
11224 hash table slots, the corresponding entry in the parallel table will be 0.
11225
11226 The pool of section numbers begins immediately following the hash table
11227 (at offset 16 + 12 * M from the beginning of the section). The pool of
11228 section numbers consists of an array of 32-bit words (using the byte order
11229 of the application binary). Each item in the array is indexed starting
11230 from 0. The hash table entry provides the index of the first section
11231 number in the set. Additional section numbers in the set follow, and the
11232 set is terminated by a 0 entry (section number 0 is not used in ELF).
11233
11234 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11235 section must be the first entry in the set, and the .debug_abbrev.dwo must
11236 be the second entry. Other members of the set may follow in any order.
11237
11238 ---
11239
11240 DWP Version 2:
11241
11242 DWP Version 2 combines all the .debug_info, etc. sections into one,
11243 and the entries in the index tables are now offsets into these sections.
11244 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11245 section.
11246
11247 Index Section Contents:
11248 Header
11249 Hash Table of Signatures dwp_hash_table.hash_table
11250 Parallel Table of Indices dwp_hash_table.unit_table
11251 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11252 Table of Section Sizes dwp_hash_table.v2.sizes
11253
11254 The index section header consists of:
11255
11256 V, 32 bit version number
11257 L, 32 bit number of columns in the table of section offsets
11258 N, 32 bit number of compilation units or type units in the index
11259 M, 32 bit number of slots in the hash table
11260
11261 Numbers are recorded using the byte order of the application binary.
11262
11263 The hash table has the same format as version 1.
11264 The parallel table of indices has the same format as version 1,
11265 except that the entries are origin-1 indices into the table of sections
11266 offsets and the table of section sizes.
11267
11268 The table of offsets begins immediately following the parallel table
11269 (at offset 16 + 12 * M from the beginning of the section). The table is
11270 a two-dimensional array of 32-bit words (using the byte order of the
11271 application binary), with L columns and N+1 rows, in row-major order.
11272 Each row in the array is indexed starting from 0. The first row provides
11273 a key to the remaining rows: each column in this row provides an identifier
11274 for a debug section, and the offsets in the same column of subsequent rows
11275 refer to that section. The section identifiers are:
11276
11277 DW_SECT_INFO 1 .debug_info.dwo
11278 DW_SECT_TYPES 2 .debug_types.dwo
11279 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11280 DW_SECT_LINE 4 .debug_line.dwo
11281 DW_SECT_LOC 5 .debug_loc.dwo
11282 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11283 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11284 DW_SECT_MACRO 8 .debug_macro.dwo
11285
11286 The offsets provided by the CU and TU index sections are the base offsets
11287 for the contributions made by each CU or TU to the corresponding section
11288 in the package file. Each CU and TU header contains an abbrev_offset
11289 field, used to find the abbreviations table for that CU or TU within the
11290 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11291 be interpreted as relative to the base offset given in the index section.
11292 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11293 should be interpreted as relative to the base offset for .debug_line.dwo,
11294 and offsets into other debug sections obtained from DWARF attributes should
11295 also be interpreted as relative to the corresponding base offset.
11296
11297 The table of sizes begins immediately following the table of offsets.
11298 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11299 with L columns and N rows, in row-major order. Each row in the array is
11300 indexed starting from 1 (row 0 is shared by the two tables).
11301
11302 ---
11303
11304 Hash table lookup is handled the same in version 1 and 2:
11305
11306 We assume that N and M will not exceed 2^32 - 1.
11307 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11308
11309 Given a 64-bit compilation unit signature or a type signature S, an entry
11310 in the hash table is located as follows:
11311
11312 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11313 the low-order k bits all set to 1.
11314
11315 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11316
11317 3) If the hash table entry at index H matches the signature, use that
11318 entry. If the hash table entry at index H is unused (all zeroes),
11319 terminate the search: the signature is not present in the table.
11320
11321 4) Let H = (H + H') modulo M. Repeat at Step 3.
11322
11323 Because M > N and H' and M are relatively prime, the search is guaranteed
11324 to stop at an unused slot or find the match. */
11325
11326 /* Create a hash table to map DWO IDs to their CU/TU entry in
11327 .debug_{info,types}.dwo in DWP_FILE.
11328 Returns NULL if there isn't one.
11329 Note: This function processes DWP files only, not DWO files. */
11330
11331 static struct dwp_hash_table *
11332 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11333 struct dwp_file *dwp_file, int is_debug_types)
11334 {
11335 struct objfile *objfile = dwarf2_per_objfile->objfile;
11336 bfd *dbfd = dwp_file->dbfd.get ();
11337 const gdb_byte *index_ptr, *index_end;
11338 struct dwarf2_section_info *index;
11339 uint32_t version, nr_columns, nr_units, nr_slots;
11340 struct dwp_hash_table *htab;
11341
11342 if (is_debug_types)
11343 index = &dwp_file->sections.tu_index;
11344 else
11345 index = &dwp_file->sections.cu_index;
11346
11347 if (index->empty ())
11348 return NULL;
11349 index->read (objfile);
11350
11351 index_ptr = index->buffer;
11352 index_end = index_ptr + index->size;
11353
11354 version = read_4_bytes (dbfd, index_ptr);
11355 index_ptr += 4;
11356 if (version == 2)
11357 nr_columns = read_4_bytes (dbfd, index_ptr);
11358 else
11359 nr_columns = 0;
11360 index_ptr += 4;
11361 nr_units = read_4_bytes (dbfd, index_ptr);
11362 index_ptr += 4;
11363 nr_slots = read_4_bytes (dbfd, index_ptr);
11364 index_ptr += 4;
11365
11366 if (version != 1 && version != 2)
11367 {
11368 error (_("Dwarf Error: unsupported DWP file version (%s)"
11369 " [in module %s]"),
11370 pulongest (version), dwp_file->name);
11371 }
11372 if (nr_slots != (nr_slots & -nr_slots))
11373 {
11374 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11375 " is not power of 2 [in module %s]"),
11376 pulongest (nr_slots), dwp_file->name);
11377 }
11378
11379 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
11380 htab->version = version;
11381 htab->nr_columns = nr_columns;
11382 htab->nr_units = nr_units;
11383 htab->nr_slots = nr_slots;
11384 htab->hash_table = index_ptr;
11385 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11386
11387 /* Exit early if the table is empty. */
11388 if (nr_slots == 0 || nr_units == 0
11389 || (version == 2 && nr_columns == 0))
11390 {
11391 /* All must be zero. */
11392 if (nr_slots != 0 || nr_units != 0
11393 || (version == 2 && nr_columns != 0))
11394 {
11395 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11396 " all zero [in modules %s]"),
11397 dwp_file->name);
11398 }
11399 return htab;
11400 }
11401
11402 if (version == 1)
11403 {
11404 htab->section_pool.v1.indices =
11405 htab->unit_table + sizeof (uint32_t) * nr_slots;
11406 /* It's harder to decide whether the section is too small in v1.
11407 V1 is deprecated anyway so we punt. */
11408 }
11409 else
11410 {
11411 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11412 int *ids = htab->section_pool.v2.section_ids;
11413 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11414 /* Reverse map for error checking. */
11415 int ids_seen[DW_SECT_MAX + 1];
11416 int i;
11417
11418 if (nr_columns < 2)
11419 {
11420 error (_("Dwarf Error: bad DWP hash table, too few columns"
11421 " in section table [in module %s]"),
11422 dwp_file->name);
11423 }
11424 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11425 {
11426 error (_("Dwarf Error: bad DWP hash table, too many columns"
11427 " in section table [in module %s]"),
11428 dwp_file->name);
11429 }
11430 memset (ids, 255, sizeof_ids);
11431 memset (ids_seen, 255, sizeof (ids_seen));
11432 for (i = 0; i < nr_columns; ++i)
11433 {
11434 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11435
11436 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11437 {
11438 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11439 " in section table [in module %s]"),
11440 id, dwp_file->name);
11441 }
11442 if (ids_seen[id] != -1)
11443 {
11444 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11445 " id %d in section table [in module %s]"),
11446 id, dwp_file->name);
11447 }
11448 ids_seen[id] = i;
11449 ids[i] = id;
11450 }
11451 /* Must have exactly one info or types section. */
11452 if (((ids_seen[DW_SECT_INFO] != -1)
11453 + (ids_seen[DW_SECT_TYPES] != -1))
11454 != 1)
11455 {
11456 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11457 " DWO info/types section [in module %s]"),
11458 dwp_file->name);
11459 }
11460 /* Must have an abbrev section. */
11461 if (ids_seen[DW_SECT_ABBREV] == -1)
11462 {
11463 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11464 " section [in module %s]"),
11465 dwp_file->name);
11466 }
11467 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11468 htab->section_pool.v2.sizes =
11469 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11470 * nr_units * nr_columns);
11471 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11472 * nr_units * nr_columns))
11473 > index_end)
11474 {
11475 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11476 " [in module %s]"),
11477 dwp_file->name);
11478 }
11479 }
11480
11481 return htab;
11482 }
11483
11484 /* Update SECTIONS with the data from SECTP.
11485
11486 This function is like the other "locate" section routines that are
11487 passed to bfd_map_over_sections, but in this context the sections to
11488 read comes from the DWP V1 hash table, not the full ELF section table.
11489
11490 The result is non-zero for success, or zero if an error was found. */
11491
11492 static int
11493 locate_v1_virtual_dwo_sections (asection *sectp,
11494 struct virtual_v1_dwo_sections *sections)
11495 {
11496 const struct dwop_section_names *names = &dwop_section_names;
11497
11498 if (section_is_p (sectp->name, &names->abbrev_dwo))
11499 {
11500 /* There can be only one. */
11501 if (sections->abbrev.s.section != NULL)
11502 return 0;
11503 sections->abbrev.s.section = sectp;
11504 sections->abbrev.size = bfd_section_size (sectp);
11505 }
11506 else if (section_is_p (sectp->name, &names->info_dwo)
11507 || section_is_p (sectp->name, &names->types_dwo))
11508 {
11509 /* There can be only one. */
11510 if (sections->info_or_types.s.section != NULL)
11511 return 0;
11512 sections->info_or_types.s.section = sectp;
11513 sections->info_or_types.size = bfd_section_size (sectp);
11514 }
11515 else if (section_is_p (sectp->name, &names->line_dwo))
11516 {
11517 /* There can be only one. */
11518 if (sections->line.s.section != NULL)
11519 return 0;
11520 sections->line.s.section = sectp;
11521 sections->line.size = bfd_section_size (sectp);
11522 }
11523 else if (section_is_p (sectp->name, &names->loc_dwo))
11524 {
11525 /* There can be only one. */
11526 if (sections->loc.s.section != NULL)
11527 return 0;
11528 sections->loc.s.section = sectp;
11529 sections->loc.size = bfd_section_size (sectp);
11530 }
11531 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11532 {
11533 /* There can be only one. */
11534 if (sections->macinfo.s.section != NULL)
11535 return 0;
11536 sections->macinfo.s.section = sectp;
11537 sections->macinfo.size = bfd_section_size (sectp);
11538 }
11539 else if (section_is_p (sectp->name, &names->macro_dwo))
11540 {
11541 /* There can be only one. */
11542 if (sections->macro.s.section != NULL)
11543 return 0;
11544 sections->macro.s.section = sectp;
11545 sections->macro.size = bfd_section_size (sectp);
11546 }
11547 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11548 {
11549 /* There can be only one. */
11550 if (sections->str_offsets.s.section != NULL)
11551 return 0;
11552 sections->str_offsets.s.section = sectp;
11553 sections->str_offsets.size = bfd_section_size (sectp);
11554 }
11555 else
11556 {
11557 /* No other kind of section is valid. */
11558 return 0;
11559 }
11560
11561 return 1;
11562 }
11563
11564 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11565 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11566 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11567 This is for DWP version 1 files. */
11568
11569 static struct dwo_unit *
11570 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11571 struct dwp_file *dwp_file,
11572 uint32_t unit_index,
11573 const char *comp_dir,
11574 ULONGEST signature, int is_debug_types)
11575 {
11576 struct objfile *objfile = dwarf2_per_objfile->objfile;
11577 const struct dwp_hash_table *dwp_htab =
11578 is_debug_types ? dwp_file->tus : dwp_file->cus;
11579 bfd *dbfd = dwp_file->dbfd.get ();
11580 const char *kind = is_debug_types ? "TU" : "CU";
11581 struct dwo_file *dwo_file;
11582 struct dwo_unit *dwo_unit;
11583 struct virtual_v1_dwo_sections sections;
11584 void **dwo_file_slot;
11585 int i;
11586
11587 gdb_assert (dwp_file->version == 1);
11588
11589 if (dwarf_read_debug)
11590 {
11591 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11592 kind,
11593 pulongest (unit_index), hex_string (signature),
11594 dwp_file->name);
11595 }
11596
11597 /* Fetch the sections of this DWO unit.
11598 Put a limit on the number of sections we look for so that bad data
11599 doesn't cause us to loop forever. */
11600
11601 #define MAX_NR_V1_DWO_SECTIONS \
11602 (1 /* .debug_info or .debug_types */ \
11603 + 1 /* .debug_abbrev */ \
11604 + 1 /* .debug_line */ \
11605 + 1 /* .debug_loc */ \
11606 + 1 /* .debug_str_offsets */ \
11607 + 1 /* .debug_macro or .debug_macinfo */ \
11608 + 1 /* trailing zero */)
11609
11610 memset (&sections, 0, sizeof (sections));
11611
11612 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11613 {
11614 asection *sectp;
11615 uint32_t section_nr =
11616 read_4_bytes (dbfd,
11617 dwp_htab->section_pool.v1.indices
11618 + (unit_index + i) * sizeof (uint32_t));
11619
11620 if (section_nr == 0)
11621 break;
11622 if (section_nr >= dwp_file->num_sections)
11623 {
11624 error (_("Dwarf Error: bad DWP hash table, section number too large"
11625 " [in module %s]"),
11626 dwp_file->name);
11627 }
11628
11629 sectp = dwp_file->elf_sections[section_nr];
11630 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11631 {
11632 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11633 " [in module %s]"),
11634 dwp_file->name);
11635 }
11636 }
11637
11638 if (i < 2
11639 || sections.info_or_types.empty ()
11640 || sections.abbrev.empty ())
11641 {
11642 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11643 " [in module %s]"),
11644 dwp_file->name);
11645 }
11646 if (i == MAX_NR_V1_DWO_SECTIONS)
11647 {
11648 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11649 " [in module %s]"),
11650 dwp_file->name);
11651 }
11652
11653 /* It's easier for the rest of the code if we fake a struct dwo_file and
11654 have dwo_unit "live" in that. At least for now.
11655
11656 The DWP file can be made up of a random collection of CUs and TUs.
11657 However, for each CU + set of TUs that came from the same original DWO
11658 file, we can combine them back into a virtual DWO file to save space
11659 (fewer struct dwo_file objects to allocate). Remember that for really
11660 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11661
11662 std::string virtual_dwo_name =
11663 string_printf ("virtual-dwo/%d-%d-%d-%d",
11664 sections.abbrev.get_id (),
11665 sections.line.get_id (),
11666 sections.loc.get_id (),
11667 sections.str_offsets.get_id ());
11668 /* Can we use an existing virtual DWO file? */
11669 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11670 virtual_dwo_name.c_str (),
11671 comp_dir);
11672 /* Create one if necessary. */
11673 if (*dwo_file_slot == NULL)
11674 {
11675 if (dwarf_read_debug)
11676 {
11677 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11678 virtual_dwo_name.c_str ());
11679 }
11680 dwo_file = new struct dwo_file;
11681 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
11682 dwo_file->comp_dir = comp_dir;
11683 dwo_file->sections.abbrev = sections.abbrev;
11684 dwo_file->sections.line = sections.line;
11685 dwo_file->sections.loc = sections.loc;
11686 dwo_file->sections.macinfo = sections.macinfo;
11687 dwo_file->sections.macro = sections.macro;
11688 dwo_file->sections.str_offsets = sections.str_offsets;
11689 /* The "str" section is global to the entire DWP file. */
11690 dwo_file->sections.str = dwp_file->sections.str;
11691 /* The info or types section is assigned below to dwo_unit,
11692 there's no need to record it in dwo_file.
11693 Also, we can't simply record type sections in dwo_file because
11694 we record a pointer into the vector in dwo_unit. As we collect more
11695 types we'll grow the vector and eventually have to reallocate space
11696 for it, invalidating all copies of pointers into the previous
11697 contents. */
11698 *dwo_file_slot = dwo_file;
11699 }
11700 else
11701 {
11702 if (dwarf_read_debug)
11703 {
11704 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11705 virtual_dwo_name.c_str ());
11706 }
11707 dwo_file = (struct dwo_file *) *dwo_file_slot;
11708 }
11709
11710 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11711 dwo_unit->dwo_file = dwo_file;
11712 dwo_unit->signature = signature;
11713 dwo_unit->section =
11714 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11715 *dwo_unit->section = sections.info_or_types;
11716 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11717
11718 return dwo_unit;
11719 }
11720
11721 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11722 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11723 piece within that section used by a TU/CU, return a virtual section
11724 of just that piece. */
11725
11726 static struct dwarf2_section_info
11727 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11728 struct dwarf2_section_info *section,
11729 bfd_size_type offset, bfd_size_type size)
11730 {
11731 struct dwarf2_section_info result;
11732 asection *sectp;
11733
11734 gdb_assert (section != NULL);
11735 gdb_assert (!section->is_virtual);
11736
11737 memset (&result, 0, sizeof (result));
11738 result.s.containing_section = section;
11739 result.is_virtual = true;
11740
11741 if (size == 0)
11742 return result;
11743
11744 sectp = section->get_bfd_section ();
11745
11746 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11747 bounds of the real section. This is a pretty-rare event, so just
11748 flag an error (easier) instead of a warning and trying to cope. */
11749 if (sectp == NULL
11750 || offset + size > bfd_section_size (sectp))
11751 {
11752 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11753 " in section %s [in module %s]"),
11754 sectp ? bfd_section_name (sectp) : "<unknown>",
11755 objfile_name (dwarf2_per_objfile->objfile));
11756 }
11757
11758 result.virtual_offset = offset;
11759 result.size = size;
11760 return result;
11761 }
11762
11763 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11764 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11765 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11766 This is for DWP version 2 files. */
11767
11768 static struct dwo_unit *
11769 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11770 struct dwp_file *dwp_file,
11771 uint32_t unit_index,
11772 const char *comp_dir,
11773 ULONGEST signature, int is_debug_types)
11774 {
11775 struct objfile *objfile = dwarf2_per_objfile->objfile;
11776 const struct dwp_hash_table *dwp_htab =
11777 is_debug_types ? dwp_file->tus : dwp_file->cus;
11778 bfd *dbfd = dwp_file->dbfd.get ();
11779 const char *kind = is_debug_types ? "TU" : "CU";
11780 struct dwo_file *dwo_file;
11781 struct dwo_unit *dwo_unit;
11782 struct virtual_v2_dwo_sections sections;
11783 void **dwo_file_slot;
11784 int i;
11785
11786 gdb_assert (dwp_file->version == 2);
11787
11788 if (dwarf_read_debug)
11789 {
11790 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11791 kind,
11792 pulongest (unit_index), hex_string (signature),
11793 dwp_file->name);
11794 }
11795
11796 /* Fetch the section offsets of this DWO unit. */
11797
11798 memset (&sections, 0, sizeof (sections));
11799
11800 for (i = 0; i < dwp_htab->nr_columns; ++i)
11801 {
11802 uint32_t offset = read_4_bytes (dbfd,
11803 dwp_htab->section_pool.v2.offsets
11804 + (((unit_index - 1) * dwp_htab->nr_columns
11805 + i)
11806 * sizeof (uint32_t)));
11807 uint32_t size = read_4_bytes (dbfd,
11808 dwp_htab->section_pool.v2.sizes
11809 + (((unit_index - 1) * dwp_htab->nr_columns
11810 + i)
11811 * sizeof (uint32_t)));
11812
11813 switch (dwp_htab->section_pool.v2.section_ids[i])
11814 {
11815 case DW_SECT_INFO:
11816 case DW_SECT_TYPES:
11817 sections.info_or_types_offset = offset;
11818 sections.info_or_types_size = size;
11819 break;
11820 case DW_SECT_ABBREV:
11821 sections.abbrev_offset = offset;
11822 sections.abbrev_size = size;
11823 break;
11824 case DW_SECT_LINE:
11825 sections.line_offset = offset;
11826 sections.line_size = size;
11827 break;
11828 case DW_SECT_LOC:
11829 sections.loc_offset = offset;
11830 sections.loc_size = size;
11831 break;
11832 case DW_SECT_STR_OFFSETS:
11833 sections.str_offsets_offset = offset;
11834 sections.str_offsets_size = size;
11835 break;
11836 case DW_SECT_MACINFO:
11837 sections.macinfo_offset = offset;
11838 sections.macinfo_size = size;
11839 break;
11840 case DW_SECT_MACRO:
11841 sections.macro_offset = offset;
11842 sections.macro_size = size;
11843 break;
11844 }
11845 }
11846
11847 /* It's easier for the rest of the code if we fake a struct dwo_file and
11848 have dwo_unit "live" in that. At least for now.
11849
11850 The DWP file can be made up of a random collection of CUs and TUs.
11851 However, for each CU + set of TUs that came from the same original DWO
11852 file, we can combine them back into a virtual DWO file to save space
11853 (fewer struct dwo_file objects to allocate). Remember that for really
11854 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11855
11856 std::string virtual_dwo_name =
11857 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11858 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11859 (long) (sections.line_size ? sections.line_offset : 0),
11860 (long) (sections.loc_size ? sections.loc_offset : 0),
11861 (long) (sections.str_offsets_size
11862 ? sections.str_offsets_offset : 0));
11863 /* Can we use an existing virtual DWO file? */
11864 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11865 virtual_dwo_name.c_str (),
11866 comp_dir);
11867 /* Create one if necessary. */
11868 if (*dwo_file_slot == NULL)
11869 {
11870 if (dwarf_read_debug)
11871 {
11872 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11873 virtual_dwo_name.c_str ());
11874 }
11875 dwo_file = new struct dwo_file;
11876 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
11877 dwo_file->comp_dir = comp_dir;
11878 dwo_file->sections.abbrev =
11879 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
11880 sections.abbrev_offset, sections.abbrev_size);
11881 dwo_file->sections.line =
11882 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
11883 sections.line_offset, sections.line_size);
11884 dwo_file->sections.loc =
11885 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
11886 sections.loc_offset, sections.loc_size);
11887 dwo_file->sections.macinfo =
11888 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
11889 sections.macinfo_offset, sections.macinfo_size);
11890 dwo_file->sections.macro =
11891 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
11892 sections.macro_offset, sections.macro_size);
11893 dwo_file->sections.str_offsets =
11894 create_dwp_v2_section (dwarf2_per_objfile,
11895 &dwp_file->sections.str_offsets,
11896 sections.str_offsets_offset,
11897 sections.str_offsets_size);
11898 /* The "str" section is global to the entire DWP file. */
11899 dwo_file->sections.str = dwp_file->sections.str;
11900 /* The info or types section is assigned below to dwo_unit,
11901 there's no need to record it in dwo_file.
11902 Also, we can't simply record type sections in dwo_file because
11903 we record a pointer into the vector in dwo_unit. As we collect more
11904 types we'll grow the vector and eventually have to reallocate space
11905 for it, invalidating all copies of pointers into the previous
11906 contents. */
11907 *dwo_file_slot = dwo_file;
11908 }
11909 else
11910 {
11911 if (dwarf_read_debug)
11912 {
11913 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11914 virtual_dwo_name.c_str ());
11915 }
11916 dwo_file = (struct dwo_file *) *dwo_file_slot;
11917 }
11918
11919 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11920 dwo_unit->dwo_file = dwo_file;
11921 dwo_unit->signature = signature;
11922 dwo_unit->section =
11923 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11924 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
11925 is_debug_types
11926 ? &dwp_file->sections.types
11927 : &dwp_file->sections.info,
11928 sections.info_or_types_offset,
11929 sections.info_or_types_size);
11930 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11931
11932 return dwo_unit;
11933 }
11934
11935 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11936 Returns NULL if the signature isn't found. */
11937
11938 static struct dwo_unit *
11939 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
11940 struct dwp_file *dwp_file, const char *comp_dir,
11941 ULONGEST signature, int is_debug_types)
11942 {
11943 const struct dwp_hash_table *dwp_htab =
11944 is_debug_types ? dwp_file->tus : dwp_file->cus;
11945 bfd *dbfd = dwp_file->dbfd.get ();
11946 uint32_t mask = dwp_htab->nr_slots - 1;
11947 uint32_t hash = signature & mask;
11948 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11949 unsigned int i;
11950 void **slot;
11951 struct dwo_unit find_dwo_cu;
11952
11953 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11954 find_dwo_cu.signature = signature;
11955 slot = htab_find_slot (is_debug_types
11956 ? dwp_file->loaded_tus.get ()
11957 : dwp_file->loaded_cus.get (),
11958 &find_dwo_cu, INSERT);
11959
11960 if (*slot != NULL)
11961 return (struct dwo_unit *) *slot;
11962
11963 /* Use a for loop so that we don't loop forever on bad debug info. */
11964 for (i = 0; i < dwp_htab->nr_slots; ++i)
11965 {
11966 ULONGEST signature_in_table;
11967
11968 signature_in_table =
11969 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
11970 if (signature_in_table == signature)
11971 {
11972 uint32_t unit_index =
11973 read_4_bytes (dbfd,
11974 dwp_htab->unit_table + hash * sizeof (uint32_t));
11975
11976 if (dwp_file->version == 1)
11977 {
11978 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
11979 dwp_file, unit_index,
11980 comp_dir, signature,
11981 is_debug_types);
11982 }
11983 else
11984 {
11985 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
11986 dwp_file, unit_index,
11987 comp_dir, signature,
11988 is_debug_types);
11989 }
11990 return (struct dwo_unit *) *slot;
11991 }
11992 if (signature_in_table == 0)
11993 return NULL;
11994 hash = (hash + hash2) & mask;
11995 }
11996
11997 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11998 " [in module %s]"),
11999 dwp_file->name);
12000 }
12001
12002 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12003 Open the file specified by FILE_NAME and hand it off to BFD for
12004 preliminary analysis. Return a newly initialized bfd *, which
12005 includes a canonicalized copy of FILE_NAME.
12006 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12007 SEARCH_CWD is true if the current directory is to be searched.
12008 It will be searched before debug-file-directory.
12009 If successful, the file is added to the bfd include table of the
12010 objfile's bfd (see gdb_bfd_record_inclusion).
12011 If unable to find/open the file, return NULL.
12012 NOTE: This function is derived from symfile_bfd_open. */
12013
12014 static gdb_bfd_ref_ptr
12015 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12016 const char *file_name, int is_dwp, int search_cwd)
12017 {
12018 int desc;
12019 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12020 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12021 to debug_file_directory. */
12022 const char *search_path;
12023 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12024
12025 gdb::unique_xmalloc_ptr<char> search_path_holder;
12026 if (search_cwd)
12027 {
12028 if (*debug_file_directory != '\0')
12029 {
12030 search_path_holder.reset (concat (".", dirname_separator_string,
12031 debug_file_directory,
12032 (char *) NULL));
12033 search_path = search_path_holder.get ();
12034 }
12035 else
12036 search_path = ".";
12037 }
12038 else
12039 search_path = debug_file_directory;
12040
12041 openp_flags flags = OPF_RETURN_REALPATH;
12042 if (is_dwp)
12043 flags |= OPF_SEARCH_IN_PATH;
12044
12045 gdb::unique_xmalloc_ptr<char> absolute_name;
12046 desc = openp (search_path, flags, file_name,
12047 O_RDONLY | O_BINARY, &absolute_name);
12048 if (desc < 0)
12049 return NULL;
12050
12051 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12052 gnutarget, desc));
12053 if (sym_bfd == NULL)
12054 return NULL;
12055 bfd_set_cacheable (sym_bfd.get (), 1);
12056
12057 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12058 return NULL;
12059
12060 /* Success. Record the bfd as having been included by the objfile's bfd.
12061 This is important because things like demangled_names_hash lives in the
12062 objfile's per_bfd space and may have references to things like symbol
12063 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12064 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12065
12066 return sym_bfd;
12067 }
12068
12069 /* Try to open DWO file FILE_NAME.
12070 COMP_DIR is the DW_AT_comp_dir attribute.
12071 The result is the bfd handle of the file.
12072 If there is a problem finding or opening the file, return NULL.
12073 Upon success, the canonicalized path of the file is stored in the bfd,
12074 same as symfile_bfd_open. */
12075
12076 static gdb_bfd_ref_ptr
12077 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12078 const char *file_name, const char *comp_dir)
12079 {
12080 if (IS_ABSOLUTE_PATH (file_name))
12081 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12082 0 /*is_dwp*/, 0 /*search_cwd*/);
12083
12084 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12085
12086 if (comp_dir != NULL)
12087 {
12088 gdb::unique_xmalloc_ptr<char> path_to_try
12089 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12090
12091 /* NOTE: If comp_dir is a relative path, this will also try the
12092 search path, which seems useful. */
12093 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12094 path_to_try.get (),
12095 0 /*is_dwp*/,
12096 1 /*search_cwd*/));
12097 if (abfd != NULL)
12098 return abfd;
12099 }
12100
12101 /* That didn't work, try debug-file-directory, which, despite its name,
12102 is a list of paths. */
12103
12104 if (*debug_file_directory == '\0')
12105 return NULL;
12106
12107 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12108 0 /*is_dwp*/, 1 /*search_cwd*/);
12109 }
12110
12111 /* This function is mapped across the sections and remembers the offset and
12112 size of each of the DWO debugging sections we are interested in. */
12113
12114 static void
12115 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12116 {
12117 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12118 const struct dwop_section_names *names = &dwop_section_names;
12119
12120 if (section_is_p (sectp->name, &names->abbrev_dwo))
12121 {
12122 dwo_sections->abbrev.s.section = sectp;
12123 dwo_sections->abbrev.size = bfd_section_size (sectp);
12124 }
12125 else if (section_is_p (sectp->name, &names->info_dwo))
12126 {
12127 dwo_sections->info.s.section = sectp;
12128 dwo_sections->info.size = bfd_section_size (sectp);
12129 }
12130 else if (section_is_p (sectp->name, &names->line_dwo))
12131 {
12132 dwo_sections->line.s.section = sectp;
12133 dwo_sections->line.size = bfd_section_size (sectp);
12134 }
12135 else if (section_is_p (sectp->name, &names->loc_dwo))
12136 {
12137 dwo_sections->loc.s.section = sectp;
12138 dwo_sections->loc.size = bfd_section_size (sectp);
12139 }
12140 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12141 {
12142 dwo_sections->macinfo.s.section = sectp;
12143 dwo_sections->macinfo.size = bfd_section_size (sectp);
12144 }
12145 else if (section_is_p (sectp->name, &names->macro_dwo))
12146 {
12147 dwo_sections->macro.s.section = sectp;
12148 dwo_sections->macro.size = bfd_section_size (sectp);
12149 }
12150 else if (section_is_p (sectp->name, &names->str_dwo))
12151 {
12152 dwo_sections->str.s.section = sectp;
12153 dwo_sections->str.size = bfd_section_size (sectp);
12154 }
12155 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12156 {
12157 dwo_sections->str_offsets.s.section = sectp;
12158 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12159 }
12160 else if (section_is_p (sectp->name, &names->types_dwo))
12161 {
12162 struct dwarf2_section_info type_section;
12163
12164 memset (&type_section, 0, sizeof (type_section));
12165 type_section.s.section = sectp;
12166 type_section.size = bfd_section_size (sectp);
12167 dwo_sections->types.push_back (type_section);
12168 }
12169 }
12170
12171 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12172 by PER_CU. This is for the non-DWP case.
12173 The result is NULL if DWO_NAME can't be found. */
12174
12175 static struct dwo_file *
12176 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12177 const char *dwo_name, const char *comp_dir)
12178 {
12179 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12180
12181 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12182 if (dbfd == NULL)
12183 {
12184 if (dwarf_read_debug)
12185 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12186 return NULL;
12187 }
12188
12189 dwo_file_up dwo_file (new struct dwo_file);
12190 dwo_file->dwo_name = dwo_name;
12191 dwo_file->comp_dir = comp_dir;
12192 dwo_file->dbfd = std::move (dbfd);
12193
12194 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12195 &dwo_file->sections);
12196
12197 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12198 dwo_file->sections.info, dwo_file->cus);
12199
12200 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12201 dwo_file->sections.types, dwo_file->tus);
12202
12203 if (dwarf_read_debug)
12204 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12205
12206 return dwo_file.release ();
12207 }
12208
12209 /* This function is mapped across the sections and remembers the offset and
12210 size of each of the DWP debugging sections common to version 1 and 2 that
12211 we are interested in. */
12212
12213 static void
12214 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12215 void *dwp_file_ptr)
12216 {
12217 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12218 const struct dwop_section_names *names = &dwop_section_names;
12219 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12220
12221 /* Record the ELF section number for later lookup: this is what the
12222 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12223 gdb_assert (elf_section_nr < dwp_file->num_sections);
12224 dwp_file->elf_sections[elf_section_nr] = sectp;
12225
12226 /* Look for specific sections that we need. */
12227 if (section_is_p (sectp->name, &names->str_dwo))
12228 {
12229 dwp_file->sections.str.s.section = sectp;
12230 dwp_file->sections.str.size = bfd_section_size (sectp);
12231 }
12232 else if (section_is_p (sectp->name, &names->cu_index))
12233 {
12234 dwp_file->sections.cu_index.s.section = sectp;
12235 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12236 }
12237 else if (section_is_p (sectp->name, &names->tu_index))
12238 {
12239 dwp_file->sections.tu_index.s.section = sectp;
12240 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12241 }
12242 }
12243
12244 /* This function is mapped across the sections and remembers the offset and
12245 size of each of the DWP version 2 debugging sections that we are interested
12246 in. This is split into a separate function because we don't know if we
12247 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12248
12249 static void
12250 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12251 {
12252 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12253 const struct dwop_section_names *names = &dwop_section_names;
12254 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12255
12256 /* Record the ELF section number for later lookup: this is what the
12257 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12258 gdb_assert (elf_section_nr < dwp_file->num_sections);
12259 dwp_file->elf_sections[elf_section_nr] = sectp;
12260
12261 /* Look for specific sections that we need. */
12262 if (section_is_p (sectp->name, &names->abbrev_dwo))
12263 {
12264 dwp_file->sections.abbrev.s.section = sectp;
12265 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12266 }
12267 else if (section_is_p (sectp->name, &names->info_dwo))
12268 {
12269 dwp_file->sections.info.s.section = sectp;
12270 dwp_file->sections.info.size = bfd_section_size (sectp);
12271 }
12272 else if (section_is_p (sectp->name, &names->line_dwo))
12273 {
12274 dwp_file->sections.line.s.section = sectp;
12275 dwp_file->sections.line.size = bfd_section_size (sectp);
12276 }
12277 else if (section_is_p (sectp->name, &names->loc_dwo))
12278 {
12279 dwp_file->sections.loc.s.section = sectp;
12280 dwp_file->sections.loc.size = bfd_section_size (sectp);
12281 }
12282 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12283 {
12284 dwp_file->sections.macinfo.s.section = sectp;
12285 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12286 }
12287 else if (section_is_p (sectp->name, &names->macro_dwo))
12288 {
12289 dwp_file->sections.macro.s.section = sectp;
12290 dwp_file->sections.macro.size = bfd_section_size (sectp);
12291 }
12292 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12293 {
12294 dwp_file->sections.str_offsets.s.section = sectp;
12295 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12296 }
12297 else if (section_is_p (sectp->name, &names->types_dwo))
12298 {
12299 dwp_file->sections.types.s.section = sectp;
12300 dwp_file->sections.types.size = bfd_section_size (sectp);
12301 }
12302 }
12303
12304 /* Hash function for dwp_file loaded CUs/TUs. */
12305
12306 static hashval_t
12307 hash_dwp_loaded_cutus (const void *item)
12308 {
12309 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12310
12311 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12312 return dwo_unit->signature;
12313 }
12314
12315 /* Equality function for dwp_file loaded CUs/TUs. */
12316
12317 static int
12318 eq_dwp_loaded_cutus (const void *a, const void *b)
12319 {
12320 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12321 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12322
12323 return dua->signature == dub->signature;
12324 }
12325
12326 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12327
12328 static htab_up
12329 allocate_dwp_loaded_cutus_table ()
12330 {
12331 return htab_up (htab_create_alloc (3,
12332 hash_dwp_loaded_cutus,
12333 eq_dwp_loaded_cutus,
12334 NULL, xcalloc, xfree));
12335 }
12336
12337 /* Try to open DWP file FILE_NAME.
12338 The result is the bfd handle of the file.
12339 If there is a problem finding or opening the file, return NULL.
12340 Upon success, the canonicalized path of the file is stored in the bfd,
12341 same as symfile_bfd_open. */
12342
12343 static gdb_bfd_ref_ptr
12344 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12345 const char *file_name)
12346 {
12347 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12348 1 /*is_dwp*/,
12349 1 /*search_cwd*/));
12350 if (abfd != NULL)
12351 return abfd;
12352
12353 /* Work around upstream bug 15652.
12354 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12355 [Whether that's a "bug" is debatable, but it is getting in our way.]
12356 We have no real idea where the dwp file is, because gdb's realpath-ing
12357 of the executable's path may have discarded the needed info.
12358 [IWBN if the dwp file name was recorded in the executable, akin to
12359 .gnu_debuglink, but that doesn't exist yet.]
12360 Strip the directory from FILE_NAME and search again. */
12361 if (*debug_file_directory != '\0')
12362 {
12363 /* Don't implicitly search the current directory here.
12364 If the user wants to search "." to handle this case,
12365 it must be added to debug-file-directory. */
12366 return try_open_dwop_file (dwarf2_per_objfile,
12367 lbasename (file_name), 1 /*is_dwp*/,
12368 0 /*search_cwd*/);
12369 }
12370
12371 return NULL;
12372 }
12373
12374 /* Initialize the use of the DWP file for the current objfile.
12375 By convention the name of the DWP file is ${objfile}.dwp.
12376 The result is NULL if it can't be found. */
12377
12378 static std::unique_ptr<struct dwp_file>
12379 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12380 {
12381 struct objfile *objfile = dwarf2_per_objfile->objfile;
12382
12383 /* Try to find first .dwp for the binary file before any symbolic links
12384 resolving. */
12385
12386 /* If the objfile is a debug file, find the name of the real binary
12387 file and get the name of dwp file from there. */
12388 std::string dwp_name;
12389 if (objfile->separate_debug_objfile_backlink != NULL)
12390 {
12391 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12392 const char *backlink_basename = lbasename (backlink->original_name);
12393
12394 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12395 }
12396 else
12397 dwp_name = objfile->original_name;
12398
12399 dwp_name += ".dwp";
12400
12401 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
12402 if (dbfd == NULL
12403 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12404 {
12405 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12406 dwp_name = objfile_name (objfile);
12407 dwp_name += ".dwp";
12408 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
12409 }
12410
12411 if (dbfd == NULL)
12412 {
12413 if (dwarf_read_debug)
12414 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12415 return std::unique_ptr<dwp_file> ();
12416 }
12417
12418 const char *name = bfd_get_filename (dbfd.get ());
12419 std::unique_ptr<struct dwp_file> dwp_file
12420 (new struct dwp_file (name, std::move (dbfd)));
12421
12422 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12423 dwp_file->elf_sections =
12424 OBSTACK_CALLOC (&objfile->objfile_obstack,
12425 dwp_file->num_sections, asection *);
12426
12427 bfd_map_over_sections (dwp_file->dbfd.get (),
12428 dwarf2_locate_common_dwp_sections,
12429 dwp_file.get ());
12430
12431 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12432 0);
12433
12434 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12435 1);
12436
12437 /* The DWP file version is stored in the hash table. Oh well. */
12438 if (dwp_file->cus && dwp_file->tus
12439 && dwp_file->cus->version != dwp_file->tus->version)
12440 {
12441 /* Technically speaking, we should try to limp along, but this is
12442 pretty bizarre. We use pulongest here because that's the established
12443 portability solution (e.g, we cannot use %u for uint32_t). */
12444 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12445 " TU version %s [in DWP file %s]"),
12446 pulongest (dwp_file->cus->version),
12447 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12448 }
12449
12450 if (dwp_file->cus)
12451 dwp_file->version = dwp_file->cus->version;
12452 else if (dwp_file->tus)
12453 dwp_file->version = dwp_file->tus->version;
12454 else
12455 dwp_file->version = 2;
12456
12457 if (dwp_file->version == 2)
12458 bfd_map_over_sections (dwp_file->dbfd.get (),
12459 dwarf2_locate_v2_dwp_sections,
12460 dwp_file.get ());
12461
12462 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12463 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12464
12465 if (dwarf_read_debug)
12466 {
12467 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12468 fprintf_unfiltered (gdb_stdlog,
12469 " %s CUs, %s TUs\n",
12470 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12471 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12472 }
12473
12474 return dwp_file;
12475 }
12476
12477 /* Wrapper around open_and_init_dwp_file, only open it once. */
12478
12479 static struct dwp_file *
12480 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12481 {
12482 if (! dwarf2_per_objfile->dwp_checked)
12483 {
12484 dwarf2_per_objfile->dwp_file
12485 = open_and_init_dwp_file (dwarf2_per_objfile);
12486 dwarf2_per_objfile->dwp_checked = 1;
12487 }
12488 return dwarf2_per_objfile->dwp_file.get ();
12489 }
12490
12491 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12492 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12493 or in the DWP file for the objfile, referenced by THIS_UNIT.
12494 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12495 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12496
12497 This is called, for example, when wanting to read a variable with a
12498 complex location. Therefore we don't want to do file i/o for every call.
12499 Therefore we don't want to look for a DWO file on every call.
12500 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12501 then we check if we've already seen DWO_NAME, and only THEN do we check
12502 for a DWO file.
12503
12504 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12505 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12506
12507 static struct dwo_unit *
12508 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12509 const char *dwo_name, const char *comp_dir,
12510 ULONGEST signature, int is_debug_types)
12511 {
12512 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
12513 struct objfile *objfile = dwarf2_per_objfile->objfile;
12514 const char *kind = is_debug_types ? "TU" : "CU";
12515 void **dwo_file_slot;
12516 struct dwo_file *dwo_file;
12517 struct dwp_file *dwp_file;
12518
12519 /* First see if there's a DWP file.
12520 If we have a DWP file but didn't find the DWO inside it, don't
12521 look for the original DWO file. It makes gdb behave differently
12522 depending on whether one is debugging in the build tree. */
12523
12524 dwp_file = get_dwp_file (dwarf2_per_objfile);
12525 if (dwp_file != NULL)
12526 {
12527 const struct dwp_hash_table *dwp_htab =
12528 is_debug_types ? dwp_file->tus : dwp_file->cus;
12529
12530 if (dwp_htab != NULL)
12531 {
12532 struct dwo_unit *dwo_cutu =
12533 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
12534 signature, is_debug_types);
12535
12536 if (dwo_cutu != NULL)
12537 {
12538 if (dwarf_read_debug)
12539 {
12540 fprintf_unfiltered (gdb_stdlog,
12541 "Virtual DWO %s %s found: @%s\n",
12542 kind, hex_string (signature),
12543 host_address_to_string (dwo_cutu));
12544 }
12545 return dwo_cutu;
12546 }
12547 }
12548 }
12549 else
12550 {
12551 /* No DWP file, look for the DWO file. */
12552
12553 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12554 dwo_name, comp_dir);
12555 if (*dwo_file_slot == NULL)
12556 {
12557 /* Read in the file and build a table of the CUs/TUs it contains. */
12558 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
12559 }
12560 /* NOTE: This will be NULL if unable to open the file. */
12561 dwo_file = (struct dwo_file *) *dwo_file_slot;
12562
12563 if (dwo_file != NULL)
12564 {
12565 struct dwo_unit *dwo_cutu = NULL;
12566
12567 if (is_debug_types && dwo_file->tus)
12568 {
12569 struct dwo_unit find_dwo_cutu;
12570
12571 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12572 find_dwo_cutu.signature = signature;
12573 dwo_cutu
12574 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12575 &find_dwo_cutu);
12576 }
12577 else if (!is_debug_types && dwo_file->cus)
12578 {
12579 struct dwo_unit find_dwo_cutu;
12580
12581 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12582 find_dwo_cutu.signature = signature;
12583 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12584 &find_dwo_cutu);
12585 }
12586
12587 if (dwo_cutu != NULL)
12588 {
12589 if (dwarf_read_debug)
12590 {
12591 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12592 kind, dwo_name, hex_string (signature),
12593 host_address_to_string (dwo_cutu));
12594 }
12595 return dwo_cutu;
12596 }
12597 }
12598 }
12599
12600 /* We didn't find it. This could mean a dwo_id mismatch, or
12601 someone deleted the DWO/DWP file, or the search path isn't set up
12602 correctly to find the file. */
12603
12604 if (dwarf_read_debug)
12605 {
12606 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12607 kind, dwo_name, hex_string (signature));
12608 }
12609
12610 /* This is a warning and not a complaint because it can be caused by
12611 pilot error (e.g., user accidentally deleting the DWO). */
12612 {
12613 /* Print the name of the DWP file if we looked there, helps the user
12614 better diagnose the problem. */
12615 std::string dwp_text;
12616
12617 if (dwp_file != NULL)
12618 dwp_text = string_printf (" [in DWP file %s]",
12619 lbasename (dwp_file->name));
12620
12621 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12622 " [in module %s]"),
12623 kind, dwo_name, hex_string (signature),
12624 dwp_text.c_str (),
12625 this_unit->is_debug_types ? "TU" : "CU",
12626 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
12627 }
12628 return NULL;
12629 }
12630
12631 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12632 See lookup_dwo_cutu_unit for details. */
12633
12634 static struct dwo_unit *
12635 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12636 const char *dwo_name, const char *comp_dir,
12637 ULONGEST signature)
12638 {
12639 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12640 }
12641
12642 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12643 See lookup_dwo_cutu_unit for details. */
12644
12645 static struct dwo_unit *
12646 lookup_dwo_type_unit (struct signatured_type *this_tu,
12647 const char *dwo_name, const char *comp_dir)
12648 {
12649 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12650 }
12651
12652 /* Traversal function for queue_and_load_all_dwo_tus. */
12653
12654 static int
12655 queue_and_load_dwo_tu (void **slot, void *info)
12656 {
12657 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12658 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12659 ULONGEST signature = dwo_unit->signature;
12660 struct signatured_type *sig_type =
12661 lookup_dwo_signatured_type (per_cu->cu, signature);
12662
12663 if (sig_type != NULL)
12664 {
12665 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12666
12667 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12668 a real dependency of PER_CU on SIG_TYPE. That is detected later
12669 while processing PER_CU. */
12670 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12671 load_full_type_unit (sig_cu);
12672 per_cu->imported_symtabs_push (sig_cu);
12673 }
12674
12675 return 1;
12676 }
12677
12678 /* Queue all TUs contained in the DWO of PER_CU to be read in.
12679 The DWO may have the only definition of the type, though it may not be
12680 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12681 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12682
12683 static void
12684 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12685 {
12686 struct dwo_unit *dwo_unit;
12687 struct dwo_file *dwo_file;
12688
12689 gdb_assert (!per_cu->is_debug_types);
12690 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
12691 gdb_assert (per_cu->cu != NULL);
12692
12693 dwo_unit = per_cu->cu->dwo_unit;
12694 gdb_assert (dwo_unit != NULL);
12695
12696 dwo_file = dwo_unit->dwo_file;
12697 if (dwo_file->tus != NULL)
12698 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12699 per_cu);
12700 }
12701
12702 /* Read in various DIEs. */
12703
12704 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12705 Inherit only the children of the DW_AT_abstract_origin DIE not being
12706 already referenced by DW_AT_abstract_origin from the children of the
12707 current DIE. */
12708
12709 static void
12710 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12711 {
12712 struct die_info *child_die;
12713 sect_offset *offsetp;
12714 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12715 struct die_info *origin_die;
12716 /* Iterator of the ORIGIN_DIE children. */
12717 struct die_info *origin_child_die;
12718 struct attribute *attr;
12719 struct dwarf2_cu *origin_cu;
12720 struct pending **origin_previous_list_in_scope;
12721
12722 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12723 if (!attr)
12724 return;
12725
12726 /* Note that following die references may follow to a die in a
12727 different cu. */
12728
12729 origin_cu = cu;
12730 origin_die = follow_die_ref (die, attr, &origin_cu);
12731
12732 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12733 symbols in. */
12734 origin_previous_list_in_scope = origin_cu->list_in_scope;
12735 origin_cu->list_in_scope = cu->list_in_scope;
12736
12737 if (die->tag != origin_die->tag
12738 && !(die->tag == DW_TAG_inlined_subroutine
12739 && origin_die->tag == DW_TAG_subprogram))
12740 complaint (_("DIE %s and its abstract origin %s have different tags"),
12741 sect_offset_str (die->sect_off),
12742 sect_offset_str (origin_die->sect_off));
12743
12744 std::vector<sect_offset> offsets;
12745
12746 for (child_die = die->child;
12747 child_die && child_die->tag;
12748 child_die = sibling_die (child_die))
12749 {
12750 struct die_info *child_origin_die;
12751 struct dwarf2_cu *child_origin_cu;
12752
12753 /* We are trying to process concrete instance entries:
12754 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12755 it's not relevant to our analysis here. i.e. detecting DIEs that are
12756 present in the abstract instance but not referenced in the concrete
12757 one. */
12758 if (child_die->tag == DW_TAG_call_site
12759 || child_die->tag == DW_TAG_GNU_call_site)
12760 continue;
12761
12762 /* For each CHILD_DIE, find the corresponding child of
12763 ORIGIN_DIE. If there is more than one layer of
12764 DW_AT_abstract_origin, follow them all; there shouldn't be,
12765 but GCC versions at least through 4.4 generate this (GCC PR
12766 40573). */
12767 child_origin_die = child_die;
12768 child_origin_cu = cu;
12769 while (1)
12770 {
12771 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12772 child_origin_cu);
12773 if (attr == NULL)
12774 break;
12775 child_origin_die = follow_die_ref (child_origin_die, attr,
12776 &child_origin_cu);
12777 }
12778
12779 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12780 counterpart may exist. */
12781 if (child_origin_die != child_die)
12782 {
12783 if (child_die->tag != child_origin_die->tag
12784 && !(child_die->tag == DW_TAG_inlined_subroutine
12785 && child_origin_die->tag == DW_TAG_subprogram))
12786 complaint (_("Child DIE %s and its abstract origin %s have "
12787 "different tags"),
12788 sect_offset_str (child_die->sect_off),
12789 sect_offset_str (child_origin_die->sect_off));
12790 if (child_origin_die->parent != origin_die)
12791 complaint (_("Child DIE %s and its abstract origin %s have "
12792 "different parents"),
12793 sect_offset_str (child_die->sect_off),
12794 sect_offset_str (child_origin_die->sect_off));
12795 else
12796 offsets.push_back (child_origin_die->sect_off);
12797 }
12798 }
12799 std::sort (offsets.begin (), offsets.end ());
12800 sect_offset *offsets_end = offsets.data () + offsets.size ();
12801 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
12802 if (offsetp[-1] == *offsetp)
12803 complaint (_("Multiple children of DIE %s refer "
12804 "to DIE %s as their abstract origin"),
12805 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
12806
12807 offsetp = offsets.data ();
12808 origin_child_die = origin_die->child;
12809 while (origin_child_die && origin_child_die->tag)
12810 {
12811 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
12812 while (offsetp < offsets_end
12813 && *offsetp < origin_child_die->sect_off)
12814 offsetp++;
12815 if (offsetp >= offsets_end
12816 || *offsetp > origin_child_die->sect_off)
12817 {
12818 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12819 Check whether we're already processing ORIGIN_CHILD_DIE.
12820 This can happen with mutually referenced abstract_origins.
12821 PR 16581. */
12822 if (!origin_child_die->in_process)
12823 process_die (origin_child_die, origin_cu);
12824 }
12825 origin_child_die = sibling_die (origin_child_die);
12826 }
12827 origin_cu->list_in_scope = origin_previous_list_in_scope;
12828
12829 if (cu != origin_cu)
12830 compute_delayed_physnames (origin_cu);
12831 }
12832
12833 static void
12834 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
12835 {
12836 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12837 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12838 struct context_stack *newobj;
12839 CORE_ADDR lowpc;
12840 CORE_ADDR highpc;
12841 struct die_info *child_die;
12842 struct attribute *attr, *call_line, *call_file;
12843 const char *name;
12844 CORE_ADDR baseaddr;
12845 struct block *block;
12846 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
12847 std::vector<struct symbol *> template_args;
12848 struct template_symbol *templ_func = NULL;
12849
12850 if (inlined_func)
12851 {
12852 /* If we do not have call site information, we can't show the
12853 caller of this inlined function. That's too confusing, so
12854 only use the scope for local variables. */
12855 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12856 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12857 if (call_line == NULL || call_file == NULL)
12858 {
12859 read_lexical_block_scope (die, cu);
12860 return;
12861 }
12862 }
12863
12864 baseaddr = objfile->text_section_offset ();
12865
12866 name = dwarf2_name (die, cu);
12867
12868 /* Ignore functions with missing or empty names. These are actually
12869 illegal according to the DWARF standard. */
12870 if (name == NULL)
12871 {
12872 complaint (_("missing name for subprogram DIE at %s"),
12873 sect_offset_str (die->sect_off));
12874 return;
12875 }
12876
12877 /* Ignore functions with missing or invalid low and high pc attributes. */
12878 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
12879 <= PC_BOUNDS_INVALID)
12880 {
12881 attr = dwarf2_attr (die, DW_AT_external, cu);
12882 if (!attr || !DW_UNSND (attr))
12883 complaint (_("cannot get low and high bounds "
12884 "for subprogram DIE at %s"),
12885 sect_offset_str (die->sect_off));
12886 return;
12887 }
12888
12889 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12890 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12891
12892 /* If we have any template arguments, then we must allocate a
12893 different sort of symbol. */
12894 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
12895 {
12896 if (child_die->tag == DW_TAG_template_type_param
12897 || child_die->tag == DW_TAG_template_value_param)
12898 {
12899 templ_func = allocate_template_symbol (objfile);
12900 templ_func->subclass = SYMBOL_TEMPLATE;
12901 break;
12902 }
12903 }
12904
12905 newobj = cu->get_builder ()->push_context (0, lowpc);
12906 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
12907 (struct symbol *) templ_func);
12908
12909 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
12910 set_objfile_main_name (objfile, newobj->name->linkage_name (),
12911 cu->language);
12912
12913 /* If there is a location expression for DW_AT_frame_base, record
12914 it. */
12915 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
12916 if (attr != nullptr)
12917 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
12918
12919 /* If there is a location for the static link, record it. */
12920 newobj->static_link = NULL;
12921 attr = dwarf2_attr (die, DW_AT_static_link, cu);
12922 if (attr != nullptr)
12923 {
12924 newobj->static_link
12925 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
12926 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
12927 cu->per_cu->addr_type ());
12928 }
12929
12930 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
12931
12932 if (die->child != NULL)
12933 {
12934 child_die = die->child;
12935 while (child_die && child_die->tag)
12936 {
12937 if (child_die->tag == DW_TAG_template_type_param
12938 || child_die->tag == DW_TAG_template_value_param)
12939 {
12940 struct symbol *arg = new_symbol (child_die, NULL, cu);
12941
12942 if (arg != NULL)
12943 template_args.push_back (arg);
12944 }
12945 else
12946 process_die (child_die, cu);
12947 child_die = sibling_die (child_die);
12948 }
12949 }
12950
12951 inherit_abstract_dies (die, cu);
12952
12953 /* If we have a DW_AT_specification, we might need to import using
12954 directives from the context of the specification DIE. See the
12955 comment in determine_prefix. */
12956 if (cu->language == language_cplus
12957 && dwarf2_attr (die, DW_AT_specification, cu))
12958 {
12959 struct dwarf2_cu *spec_cu = cu;
12960 struct die_info *spec_die = die_specification (die, &spec_cu);
12961
12962 while (spec_die)
12963 {
12964 child_die = spec_die->child;
12965 while (child_die && child_die->tag)
12966 {
12967 if (child_die->tag == DW_TAG_imported_module)
12968 process_die (child_die, spec_cu);
12969 child_die = sibling_die (child_die);
12970 }
12971
12972 /* In some cases, GCC generates specification DIEs that
12973 themselves contain DW_AT_specification attributes. */
12974 spec_die = die_specification (spec_die, &spec_cu);
12975 }
12976 }
12977
12978 struct context_stack cstk = cu->get_builder ()->pop_context ();
12979 /* Make a block for the local symbols within. */
12980 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
12981 cstk.static_link, lowpc, highpc);
12982
12983 /* For C++, set the block's scope. */
12984 if ((cu->language == language_cplus
12985 || cu->language == language_fortran
12986 || cu->language == language_d
12987 || cu->language == language_rust)
12988 && cu->processing_has_namespace_info)
12989 block_set_scope (block, determine_prefix (die, cu),
12990 &objfile->objfile_obstack);
12991
12992 /* If we have address ranges, record them. */
12993 dwarf2_record_block_ranges (die, block, baseaddr, cu);
12994
12995 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
12996
12997 /* Attach template arguments to function. */
12998 if (!template_args.empty ())
12999 {
13000 gdb_assert (templ_func != NULL);
13001
13002 templ_func->n_template_arguments = template_args.size ();
13003 templ_func->template_arguments
13004 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13005 templ_func->n_template_arguments);
13006 memcpy (templ_func->template_arguments,
13007 template_args.data (),
13008 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13009
13010 /* Make sure that the symtab is set on the new symbols. Even
13011 though they don't appear in this symtab directly, other parts
13012 of gdb assume that symbols do, and this is reasonably
13013 true. */
13014 for (symbol *sym : template_args)
13015 symbol_set_symtab (sym, symbol_symtab (templ_func));
13016 }
13017
13018 /* In C++, we can have functions nested inside functions (e.g., when
13019 a function declares a class that has methods). This means that
13020 when we finish processing a function scope, we may need to go
13021 back to building a containing block's symbol lists. */
13022 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13023 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13024
13025 /* If we've finished processing a top-level function, subsequent
13026 symbols go in the file symbol list. */
13027 if (cu->get_builder ()->outermost_context_p ())
13028 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13029 }
13030
13031 /* Process all the DIES contained within a lexical block scope. Start
13032 a new scope, process the dies, and then close the scope. */
13033
13034 static void
13035 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13036 {
13037 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13038 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13039 CORE_ADDR lowpc, highpc;
13040 struct die_info *child_die;
13041 CORE_ADDR baseaddr;
13042
13043 baseaddr = objfile->text_section_offset ();
13044
13045 /* Ignore blocks with missing or invalid low and high pc attributes. */
13046 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13047 as multiple lexical blocks? Handling children in a sane way would
13048 be nasty. Might be easier to properly extend generic blocks to
13049 describe ranges. */
13050 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13051 {
13052 case PC_BOUNDS_NOT_PRESENT:
13053 /* DW_TAG_lexical_block has no attributes, process its children as if
13054 there was no wrapping by that DW_TAG_lexical_block.
13055 GCC does no longer produces such DWARF since GCC r224161. */
13056 for (child_die = die->child;
13057 child_die != NULL && child_die->tag;
13058 child_die = sibling_die (child_die))
13059 process_die (child_die, cu);
13060 return;
13061 case PC_BOUNDS_INVALID:
13062 return;
13063 }
13064 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13065 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13066
13067 cu->get_builder ()->push_context (0, lowpc);
13068 if (die->child != NULL)
13069 {
13070 child_die = die->child;
13071 while (child_die && child_die->tag)
13072 {
13073 process_die (child_die, cu);
13074 child_die = sibling_die (child_die);
13075 }
13076 }
13077 inherit_abstract_dies (die, cu);
13078 struct context_stack cstk = cu->get_builder ()->pop_context ();
13079
13080 if (*cu->get_builder ()->get_local_symbols () != NULL
13081 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13082 {
13083 struct block *block
13084 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13085 cstk.start_addr, highpc);
13086
13087 /* Note that recording ranges after traversing children, as we
13088 do here, means that recording a parent's ranges entails
13089 walking across all its children's ranges as they appear in
13090 the address map, which is quadratic behavior.
13091
13092 It would be nicer to record the parent's ranges before
13093 traversing its children, simply overriding whatever you find
13094 there. But since we don't even decide whether to create a
13095 block until after we've traversed its children, that's hard
13096 to do. */
13097 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13098 }
13099 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13100 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13101 }
13102
13103 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13104
13105 static void
13106 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13107 {
13108 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13109 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13110 CORE_ADDR pc, baseaddr;
13111 struct attribute *attr;
13112 struct call_site *call_site, call_site_local;
13113 void **slot;
13114 int nparams;
13115 struct die_info *child_die;
13116
13117 baseaddr = objfile->text_section_offset ();
13118
13119 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13120 if (attr == NULL)
13121 {
13122 /* This was a pre-DWARF-5 GNU extension alias
13123 for DW_AT_call_return_pc. */
13124 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13125 }
13126 if (!attr)
13127 {
13128 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13129 "DIE %s [in module %s]"),
13130 sect_offset_str (die->sect_off), objfile_name (objfile));
13131 return;
13132 }
13133 pc = attr->value_as_address () + baseaddr;
13134 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13135
13136 if (cu->call_site_htab == NULL)
13137 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13138 NULL, &objfile->objfile_obstack,
13139 hashtab_obstack_allocate, NULL);
13140 call_site_local.pc = pc;
13141 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13142 if (*slot != NULL)
13143 {
13144 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13145 "DIE %s [in module %s]"),
13146 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13147 objfile_name (objfile));
13148 return;
13149 }
13150
13151 /* Count parameters at the caller. */
13152
13153 nparams = 0;
13154 for (child_die = die->child; child_die && child_die->tag;
13155 child_die = sibling_die (child_die))
13156 {
13157 if (child_die->tag != DW_TAG_call_site_parameter
13158 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13159 {
13160 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13161 "DW_TAG_call_site child DIE %s [in module %s]"),
13162 child_die->tag, sect_offset_str (child_die->sect_off),
13163 objfile_name (objfile));
13164 continue;
13165 }
13166
13167 nparams++;
13168 }
13169
13170 call_site
13171 = ((struct call_site *)
13172 obstack_alloc (&objfile->objfile_obstack,
13173 sizeof (*call_site)
13174 + (sizeof (*call_site->parameter) * (nparams - 1))));
13175 *slot = call_site;
13176 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13177 call_site->pc = pc;
13178
13179 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13180 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13181 {
13182 struct die_info *func_die;
13183
13184 /* Skip also over DW_TAG_inlined_subroutine. */
13185 for (func_die = die->parent;
13186 func_die && func_die->tag != DW_TAG_subprogram
13187 && func_die->tag != DW_TAG_subroutine_type;
13188 func_die = func_die->parent);
13189
13190 /* DW_AT_call_all_calls is a superset
13191 of DW_AT_call_all_tail_calls. */
13192 if (func_die
13193 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13194 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13195 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13196 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13197 {
13198 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13199 not complete. But keep CALL_SITE for look ups via call_site_htab,
13200 both the initial caller containing the real return address PC and
13201 the final callee containing the current PC of a chain of tail
13202 calls do not need to have the tail call list complete. But any
13203 function candidate for a virtual tail call frame searched via
13204 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13205 determined unambiguously. */
13206 }
13207 else
13208 {
13209 struct type *func_type = NULL;
13210
13211 if (func_die)
13212 func_type = get_die_type (func_die, cu);
13213 if (func_type != NULL)
13214 {
13215 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13216
13217 /* Enlist this call site to the function. */
13218 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13219 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13220 }
13221 else
13222 complaint (_("Cannot find function owning DW_TAG_call_site "
13223 "DIE %s [in module %s]"),
13224 sect_offset_str (die->sect_off), objfile_name (objfile));
13225 }
13226 }
13227
13228 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13229 if (attr == NULL)
13230 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13231 if (attr == NULL)
13232 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13233 if (attr == NULL)
13234 {
13235 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13236 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13237 }
13238 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13239 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13240 /* Keep NULL DWARF_BLOCK. */;
13241 else if (attr->form_is_block ())
13242 {
13243 struct dwarf2_locexpr_baton *dlbaton;
13244
13245 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13246 dlbaton->data = DW_BLOCK (attr)->data;
13247 dlbaton->size = DW_BLOCK (attr)->size;
13248 dlbaton->per_cu = cu->per_cu;
13249
13250 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13251 }
13252 else if (attr->form_is_ref ())
13253 {
13254 struct dwarf2_cu *target_cu = cu;
13255 struct die_info *target_die;
13256
13257 target_die = follow_die_ref (die, attr, &target_cu);
13258 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
13259 if (die_is_declaration (target_die, target_cu))
13260 {
13261 const char *target_physname;
13262
13263 /* Prefer the mangled name; otherwise compute the demangled one. */
13264 target_physname = dw2_linkage_name (target_die, target_cu);
13265 if (target_physname == NULL)
13266 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13267 if (target_physname == NULL)
13268 complaint (_("DW_AT_call_target target DIE has invalid "
13269 "physname, for referencing DIE %s [in module %s]"),
13270 sect_offset_str (die->sect_off), objfile_name (objfile));
13271 else
13272 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13273 }
13274 else
13275 {
13276 CORE_ADDR lowpc;
13277
13278 /* DW_AT_entry_pc should be preferred. */
13279 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13280 <= PC_BOUNDS_INVALID)
13281 complaint (_("DW_AT_call_target target DIE has invalid "
13282 "low pc, for referencing DIE %s [in module %s]"),
13283 sect_offset_str (die->sect_off), objfile_name (objfile));
13284 else
13285 {
13286 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13287 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13288 }
13289 }
13290 }
13291 else
13292 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13293 "block nor reference, for DIE %s [in module %s]"),
13294 sect_offset_str (die->sect_off), objfile_name (objfile));
13295
13296 call_site->per_cu = cu->per_cu;
13297
13298 for (child_die = die->child;
13299 child_die && child_die->tag;
13300 child_die = sibling_die (child_die))
13301 {
13302 struct call_site_parameter *parameter;
13303 struct attribute *loc, *origin;
13304
13305 if (child_die->tag != DW_TAG_call_site_parameter
13306 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13307 {
13308 /* Already printed the complaint above. */
13309 continue;
13310 }
13311
13312 gdb_assert (call_site->parameter_count < nparams);
13313 parameter = &call_site->parameter[call_site->parameter_count];
13314
13315 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13316 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13317 register is contained in DW_AT_call_value. */
13318
13319 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13320 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13321 if (origin == NULL)
13322 {
13323 /* This was a pre-DWARF-5 GNU extension alias
13324 for DW_AT_call_parameter. */
13325 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13326 }
13327 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13328 {
13329 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13330
13331 sect_offset sect_off
13332 = (sect_offset) dwarf2_get_ref_die_offset (origin);
13333 if (!cu->header.offset_in_cu_p (sect_off))
13334 {
13335 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13336 binding can be done only inside one CU. Such referenced DIE
13337 therefore cannot be even moved to DW_TAG_partial_unit. */
13338 complaint (_("DW_AT_call_parameter offset is not in CU for "
13339 "DW_TAG_call_site child DIE %s [in module %s]"),
13340 sect_offset_str (child_die->sect_off),
13341 objfile_name (objfile));
13342 continue;
13343 }
13344 parameter->u.param_cu_off
13345 = (cu_offset) (sect_off - cu->header.sect_off);
13346 }
13347 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13348 {
13349 complaint (_("No DW_FORM_block* DW_AT_location for "
13350 "DW_TAG_call_site child DIE %s [in module %s]"),
13351 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13352 continue;
13353 }
13354 else
13355 {
13356 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13357 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13358 if (parameter->u.dwarf_reg != -1)
13359 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13360 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13361 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13362 &parameter->u.fb_offset))
13363 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13364 else
13365 {
13366 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13367 "for DW_FORM_block* DW_AT_location is supported for "
13368 "DW_TAG_call_site child DIE %s "
13369 "[in module %s]"),
13370 sect_offset_str (child_die->sect_off),
13371 objfile_name (objfile));
13372 continue;
13373 }
13374 }
13375
13376 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13377 if (attr == NULL)
13378 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13379 if (attr == NULL || !attr->form_is_block ())
13380 {
13381 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13382 "DW_TAG_call_site child DIE %s [in module %s]"),
13383 sect_offset_str (child_die->sect_off),
13384 objfile_name (objfile));
13385 continue;
13386 }
13387 parameter->value = DW_BLOCK (attr)->data;
13388 parameter->value_size = DW_BLOCK (attr)->size;
13389
13390 /* Parameters are not pre-cleared by memset above. */
13391 parameter->data_value = NULL;
13392 parameter->data_value_size = 0;
13393 call_site->parameter_count++;
13394
13395 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13396 if (attr == NULL)
13397 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13398 if (attr != nullptr)
13399 {
13400 if (!attr->form_is_block ())
13401 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13402 "DW_TAG_call_site child DIE %s [in module %s]"),
13403 sect_offset_str (child_die->sect_off),
13404 objfile_name (objfile));
13405 else
13406 {
13407 parameter->data_value = DW_BLOCK (attr)->data;
13408 parameter->data_value_size = DW_BLOCK (attr)->size;
13409 }
13410 }
13411 }
13412 }
13413
13414 /* Helper function for read_variable. If DIE represents a virtual
13415 table, then return the type of the concrete object that is
13416 associated with the virtual table. Otherwise, return NULL. */
13417
13418 static struct type *
13419 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13420 {
13421 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13422 if (attr == NULL)
13423 return NULL;
13424
13425 /* Find the type DIE. */
13426 struct die_info *type_die = NULL;
13427 struct dwarf2_cu *type_cu = cu;
13428
13429 if (attr->form_is_ref ())
13430 type_die = follow_die_ref (die, attr, &type_cu);
13431 if (type_die == NULL)
13432 return NULL;
13433
13434 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13435 return NULL;
13436 return die_containing_type (type_die, type_cu);
13437 }
13438
13439 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13440
13441 static void
13442 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13443 {
13444 struct rust_vtable_symbol *storage = NULL;
13445
13446 if (cu->language == language_rust)
13447 {
13448 struct type *containing_type = rust_containing_type (die, cu);
13449
13450 if (containing_type != NULL)
13451 {
13452 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13453
13454 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
13455 initialize_objfile_symbol (storage);
13456 storage->concrete_type = containing_type;
13457 storage->subclass = SYMBOL_RUST_VTABLE;
13458 }
13459 }
13460
13461 struct symbol *res = new_symbol (die, NULL, cu, storage);
13462 struct attribute *abstract_origin
13463 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13464 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13465 if (res == NULL && loc && abstract_origin)
13466 {
13467 /* We have a variable without a name, but with a location and an abstract
13468 origin. This may be a concrete instance of an abstract variable
13469 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13470 later. */
13471 struct dwarf2_cu *origin_cu = cu;
13472 struct die_info *origin_die
13473 = follow_die_ref (die, abstract_origin, &origin_cu);
13474 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
13475 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
13476 }
13477 }
13478
13479 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13480 reading .debug_rnglists.
13481 Callback's type should be:
13482 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13483 Return true if the attributes are present and valid, otherwise,
13484 return false. */
13485
13486 template <typename Callback>
13487 static bool
13488 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13489 Callback &&callback)
13490 {
13491 struct dwarf2_per_objfile *dwarf2_per_objfile
13492 = cu->per_cu->dwarf2_per_objfile;
13493 struct objfile *objfile = dwarf2_per_objfile->objfile;
13494 bfd *obfd = objfile->obfd;
13495 /* Base address selection entry. */
13496 CORE_ADDR base;
13497 int found_base;
13498 const gdb_byte *buffer;
13499 CORE_ADDR baseaddr;
13500 bool overflow = false;
13501
13502 found_base = cu->base_known;
13503 base = cu->base_address;
13504
13505 dwarf2_per_objfile->rnglists.read (objfile);
13506 if (offset >= dwarf2_per_objfile->rnglists.size)
13507 {
13508 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13509 offset);
13510 return false;
13511 }
13512 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13513
13514 baseaddr = objfile->text_section_offset ();
13515
13516 while (1)
13517 {
13518 /* Initialize it due to a false compiler warning. */
13519 CORE_ADDR range_beginning = 0, range_end = 0;
13520 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13521 + dwarf2_per_objfile->rnglists.size);
13522 unsigned int bytes_read;
13523
13524 if (buffer == buf_end)
13525 {
13526 overflow = true;
13527 break;
13528 }
13529 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13530 switch (rlet)
13531 {
13532 case DW_RLE_end_of_list:
13533 break;
13534 case DW_RLE_base_address:
13535 if (buffer + cu->header.addr_size > buf_end)
13536 {
13537 overflow = true;
13538 break;
13539 }
13540 base = cu->header.read_address (obfd, buffer, &bytes_read);
13541 found_base = 1;
13542 buffer += bytes_read;
13543 break;
13544 case DW_RLE_start_length:
13545 if (buffer + cu->header.addr_size > buf_end)
13546 {
13547 overflow = true;
13548 break;
13549 }
13550 range_beginning = cu->header.read_address (obfd, buffer,
13551 &bytes_read);
13552 buffer += bytes_read;
13553 range_end = (range_beginning
13554 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13555 buffer += bytes_read;
13556 if (buffer > buf_end)
13557 {
13558 overflow = true;
13559 break;
13560 }
13561 break;
13562 case DW_RLE_offset_pair:
13563 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13564 buffer += bytes_read;
13565 if (buffer > buf_end)
13566 {
13567 overflow = true;
13568 break;
13569 }
13570 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13571 buffer += bytes_read;
13572 if (buffer > buf_end)
13573 {
13574 overflow = true;
13575 break;
13576 }
13577 break;
13578 case DW_RLE_start_end:
13579 if (buffer + 2 * cu->header.addr_size > buf_end)
13580 {
13581 overflow = true;
13582 break;
13583 }
13584 range_beginning = cu->header.read_address (obfd, buffer,
13585 &bytes_read);
13586 buffer += bytes_read;
13587 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13588 buffer += bytes_read;
13589 break;
13590 default:
13591 complaint (_("Invalid .debug_rnglists data (no base address)"));
13592 return false;
13593 }
13594 if (rlet == DW_RLE_end_of_list || overflow)
13595 break;
13596 if (rlet == DW_RLE_base_address)
13597 continue;
13598
13599 if (!found_base)
13600 {
13601 /* We have no valid base address for the ranges
13602 data. */
13603 complaint (_("Invalid .debug_rnglists data (no base address)"));
13604 return false;
13605 }
13606
13607 if (range_beginning > range_end)
13608 {
13609 /* Inverted range entries are invalid. */
13610 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13611 return false;
13612 }
13613
13614 /* Empty range entries have no effect. */
13615 if (range_beginning == range_end)
13616 continue;
13617
13618 range_beginning += base;
13619 range_end += base;
13620
13621 /* A not-uncommon case of bad debug info.
13622 Don't pollute the addrmap with bad data. */
13623 if (range_beginning + baseaddr == 0
13624 && !dwarf2_per_objfile->has_section_at_zero)
13625 {
13626 complaint (_(".debug_rnglists entry has start address of zero"
13627 " [in module %s]"), objfile_name (objfile));
13628 continue;
13629 }
13630
13631 callback (range_beginning, range_end);
13632 }
13633
13634 if (overflow)
13635 {
13636 complaint (_("Offset %d is not terminated "
13637 "for DW_AT_ranges attribute"),
13638 offset);
13639 return false;
13640 }
13641
13642 return true;
13643 }
13644
13645 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13646 Callback's type should be:
13647 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13648 Return 1 if the attributes are present and valid, otherwise, return 0. */
13649
13650 template <typename Callback>
13651 static int
13652 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
13653 Callback &&callback)
13654 {
13655 struct dwarf2_per_objfile *dwarf2_per_objfile
13656 = cu->per_cu->dwarf2_per_objfile;
13657 struct objfile *objfile = dwarf2_per_objfile->objfile;
13658 struct comp_unit_head *cu_header = &cu->header;
13659 bfd *obfd = objfile->obfd;
13660 unsigned int addr_size = cu_header->addr_size;
13661 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13662 /* Base address selection entry. */
13663 CORE_ADDR base;
13664 int found_base;
13665 unsigned int dummy;
13666 const gdb_byte *buffer;
13667 CORE_ADDR baseaddr;
13668
13669 if (cu_header->version >= 5)
13670 return dwarf2_rnglists_process (offset, cu, callback);
13671
13672 found_base = cu->base_known;
13673 base = cu->base_address;
13674
13675 dwarf2_per_objfile->ranges.read (objfile);
13676 if (offset >= dwarf2_per_objfile->ranges.size)
13677 {
13678 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13679 offset);
13680 return 0;
13681 }
13682 buffer = dwarf2_per_objfile->ranges.buffer + offset;
13683
13684 baseaddr = objfile->text_section_offset ();
13685
13686 while (1)
13687 {
13688 CORE_ADDR range_beginning, range_end;
13689
13690 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13691 buffer += addr_size;
13692 range_end = cu->header.read_address (obfd, buffer, &dummy);
13693 buffer += addr_size;
13694 offset += 2 * addr_size;
13695
13696 /* An end of list marker is a pair of zero addresses. */
13697 if (range_beginning == 0 && range_end == 0)
13698 /* Found the end of list entry. */
13699 break;
13700
13701 /* Each base address selection entry is a pair of 2 values.
13702 The first is the largest possible address, the second is
13703 the base address. Check for a base address here. */
13704 if ((range_beginning & mask) == mask)
13705 {
13706 /* If we found the largest possible address, then we already
13707 have the base address in range_end. */
13708 base = range_end;
13709 found_base = 1;
13710 continue;
13711 }
13712
13713 if (!found_base)
13714 {
13715 /* We have no valid base address for the ranges
13716 data. */
13717 complaint (_("Invalid .debug_ranges data (no base address)"));
13718 return 0;
13719 }
13720
13721 if (range_beginning > range_end)
13722 {
13723 /* Inverted range entries are invalid. */
13724 complaint (_("Invalid .debug_ranges data (inverted range)"));
13725 return 0;
13726 }
13727
13728 /* Empty range entries have no effect. */
13729 if (range_beginning == range_end)
13730 continue;
13731
13732 range_beginning += base;
13733 range_end += base;
13734
13735 /* A not-uncommon case of bad debug info.
13736 Don't pollute the addrmap with bad data. */
13737 if (range_beginning + baseaddr == 0
13738 && !dwarf2_per_objfile->has_section_at_zero)
13739 {
13740 complaint (_(".debug_ranges entry has start address of zero"
13741 " [in module %s]"), objfile_name (objfile));
13742 continue;
13743 }
13744
13745 callback (range_beginning, range_end);
13746 }
13747
13748 return 1;
13749 }
13750
13751 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13752 Return 1 if the attributes are present and valid, otherwise, return 0.
13753 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13754
13755 static int
13756 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13757 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13758 dwarf2_psymtab *ranges_pst)
13759 {
13760 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13761 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13762 const CORE_ADDR baseaddr = objfile->text_section_offset ();
13763 int low_set = 0;
13764 CORE_ADDR low = 0;
13765 CORE_ADDR high = 0;
13766 int retval;
13767
13768 retval = dwarf2_ranges_process (offset, cu,
13769 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13770 {
13771 if (ranges_pst != NULL)
13772 {
13773 CORE_ADDR lowpc;
13774 CORE_ADDR highpc;
13775
13776 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13777 range_beginning + baseaddr)
13778 - baseaddr);
13779 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13780 range_end + baseaddr)
13781 - baseaddr);
13782 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13783 lowpc, highpc - 1, ranges_pst);
13784 }
13785
13786 /* FIXME: This is recording everything as a low-high
13787 segment of consecutive addresses. We should have a
13788 data structure for discontiguous block ranges
13789 instead. */
13790 if (! low_set)
13791 {
13792 low = range_beginning;
13793 high = range_end;
13794 low_set = 1;
13795 }
13796 else
13797 {
13798 if (range_beginning < low)
13799 low = range_beginning;
13800 if (range_end > high)
13801 high = range_end;
13802 }
13803 });
13804 if (!retval)
13805 return 0;
13806
13807 if (! low_set)
13808 /* If the first entry is an end-of-list marker, the range
13809 describes an empty scope, i.e. no instructions. */
13810 return 0;
13811
13812 if (low_return)
13813 *low_return = low;
13814 if (high_return)
13815 *high_return = high;
13816 return 1;
13817 }
13818
13819 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
13820 definition for the return value. *LOWPC and *HIGHPC are set iff
13821 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
13822
13823 static enum pc_bounds_kind
13824 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
13825 CORE_ADDR *highpc, struct dwarf2_cu *cu,
13826 dwarf2_psymtab *pst)
13827 {
13828 struct dwarf2_per_objfile *dwarf2_per_objfile
13829 = cu->per_cu->dwarf2_per_objfile;
13830 struct attribute *attr;
13831 struct attribute *attr_high;
13832 CORE_ADDR low = 0;
13833 CORE_ADDR high = 0;
13834 enum pc_bounds_kind ret;
13835
13836 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13837 if (attr_high)
13838 {
13839 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13840 if (attr != nullptr)
13841 {
13842 low = attr->value_as_address ();
13843 high = attr_high->value_as_address ();
13844 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13845 high += low;
13846 }
13847 else
13848 /* Found high w/o low attribute. */
13849 return PC_BOUNDS_INVALID;
13850
13851 /* Found consecutive range of addresses. */
13852 ret = PC_BOUNDS_HIGH_LOW;
13853 }
13854 else
13855 {
13856 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13857 if (attr != NULL)
13858 {
13859 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
13860 We take advantage of the fact that DW_AT_ranges does not appear
13861 in DW_TAG_compile_unit of DWO files. */
13862 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13863 unsigned int ranges_offset = (DW_UNSND (attr)
13864 + (need_ranges_base
13865 ? cu->ranges_base
13866 : 0));
13867
13868 /* Value of the DW_AT_ranges attribute is the offset in the
13869 .debug_ranges section. */
13870 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
13871 return PC_BOUNDS_INVALID;
13872 /* Found discontinuous range of addresses. */
13873 ret = PC_BOUNDS_RANGES;
13874 }
13875 else
13876 return PC_BOUNDS_NOT_PRESENT;
13877 }
13878
13879 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
13880 if (high <= low)
13881 return PC_BOUNDS_INVALID;
13882
13883 /* When using the GNU linker, .gnu.linkonce. sections are used to
13884 eliminate duplicate copies of functions and vtables and such.
13885 The linker will arbitrarily choose one and discard the others.
13886 The AT_*_pc values for such functions refer to local labels in
13887 these sections. If the section from that file was discarded, the
13888 labels are not in the output, so the relocs get a value of 0.
13889 If this is a discarded function, mark the pc bounds as invalid,
13890 so that GDB will ignore it. */
13891 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
13892 return PC_BOUNDS_INVALID;
13893
13894 *lowpc = low;
13895 if (highpc)
13896 *highpc = high;
13897 return ret;
13898 }
13899
13900 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
13901 its low and high PC addresses. Do nothing if these addresses could not
13902 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13903 and HIGHPC to the high address if greater than HIGHPC. */
13904
13905 static void
13906 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13907 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13908 struct dwarf2_cu *cu)
13909 {
13910 CORE_ADDR low, high;
13911 struct die_info *child = die->child;
13912
13913 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
13914 {
13915 *lowpc = std::min (*lowpc, low);
13916 *highpc = std::max (*highpc, high);
13917 }
13918
13919 /* If the language does not allow nested subprograms (either inside
13920 subprograms or lexical blocks), we're done. */
13921 if (cu->language != language_ada)
13922 return;
13923
13924 /* Check all the children of the given DIE. If it contains nested
13925 subprograms, then check their pc bounds. Likewise, we need to
13926 check lexical blocks as well, as they may also contain subprogram
13927 definitions. */
13928 while (child && child->tag)
13929 {
13930 if (child->tag == DW_TAG_subprogram
13931 || child->tag == DW_TAG_lexical_block)
13932 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
13933 child = sibling_die (child);
13934 }
13935 }
13936
13937 /* Get the low and high pc's represented by the scope DIE, and store
13938 them in *LOWPC and *HIGHPC. If the correct values can't be
13939 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13940
13941 static void
13942 get_scope_pc_bounds (struct die_info *die,
13943 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13944 struct dwarf2_cu *cu)
13945 {
13946 CORE_ADDR best_low = (CORE_ADDR) -1;
13947 CORE_ADDR best_high = (CORE_ADDR) 0;
13948 CORE_ADDR current_low, current_high;
13949
13950 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
13951 >= PC_BOUNDS_RANGES)
13952 {
13953 best_low = current_low;
13954 best_high = current_high;
13955 }
13956 else
13957 {
13958 struct die_info *child = die->child;
13959
13960 while (child && child->tag)
13961 {
13962 switch (child->tag) {
13963 case DW_TAG_subprogram:
13964 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
13965 break;
13966 case DW_TAG_namespace:
13967 case DW_TAG_module:
13968 /* FIXME: carlton/2004-01-16: Should we do this for
13969 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13970 that current GCC's always emit the DIEs corresponding
13971 to definitions of methods of classes as children of a
13972 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13973 the DIEs giving the declarations, which could be
13974 anywhere). But I don't see any reason why the
13975 standards says that they have to be there. */
13976 get_scope_pc_bounds (child, &current_low, &current_high, cu);
13977
13978 if (current_low != ((CORE_ADDR) -1))
13979 {
13980 best_low = std::min (best_low, current_low);
13981 best_high = std::max (best_high, current_high);
13982 }
13983 break;
13984 default:
13985 /* Ignore. */
13986 break;
13987 }
13988
13989 child = sibling_die (child);
13990 }
13991 }
13992
13993 *lowpc = best_low;
13994 *highpc = best_high;
13995 }
13996
13997 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
13998 in DIE. */
13999
14000 static void
14001 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14002 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14003 {
14004 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14005 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14006 struct attribute *attr;
14007 struct attribute *attr_high;
14008
14009 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14010 if (attr_high)
14011 {
14012 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14013 if (attr != nullptr)
14014 {
14015 CORE_ADDR low = attr->value_as_address ();
14016 CORE_ADDR high = attr_high->value_as_address ();
14017
14018 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14019 high += low;
14020
14021 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14022 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14023 cu->get_builder ()->record_block_range (block, low, high - 1);
14024 }
14025 }
14026
14027 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14028 if (attr != nullptr)
14029 {
14030 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14031 We take advantage of the fact that DW_AT_ranges does not appear
14032 in DW_TAG_compile_unit of DWO files. */
14033 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14034
14035 /* The value of the DW_AT_ranges attribute is the offset of the
14036 address range list in the .debug_ranges section. */
14037 unsigned long offset = (DW_UNSND (attr)
14038 + (need_ranges_base ? cu->ranges_base : 0));
14039
14040 std::vector<blockrange> blockvec;
14041 dwarf2_ranges_process (offset, cu,
14042 [&] (CORE_ADDR start, CORE_ADDR end)
14043 {
14044 start += baseaddr;
14045 end += baseaddr;
14046 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14047 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14048 cu->get_builder ()->record_block_range (block, start, end - 1);
14049 blockvec.emplace_back (start, end);
14050 });
14051
14052 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14053 }
14054 }
14055
14056 /* Check whether the producer field indicates either of GCC < 4.6, or the
14057 Intel C/C++ compiler, and cache the result in CU. */
14058
14059 static void
14060 check_producer (struct dwarf2_cu *cu)
14061 {
14062 int major, minor;
14063
14064 if (cu->producer == NULL)
14065 {
14066 /* For unknown compilers expect their behavior is DWARF version
14067 compliant.
14068
14069 GCC started to support .debug_types sections by -gdwarf-4 since
14070 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14071 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14072 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14073 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14074 }
14075 else if (producer_is_gcc (cu->producer, &major, &minor))
14076 {
14077 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14078 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14079 }
14080 else if (producer_is_icc (cu->producer, &major, &minor))
14081 {
14082 cu->producer_is_icc = true;
14083 cu->producer_is_icc_lt_14 = major < 14;
14084 }
14085 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14086 cu->producer_is_codewarrior = true;
14087 else
14088 {
14089 /* For other non-GCC compilers, expect their behavior is DWARF version
14090 compliant. */
14091 }
14092
14093 cu->checked_producer = true;
14094 }
14095
14096 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14097 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14098 during 4.6.0 experimental. */
14099
14100 static bool
14101 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14102 {
14103 if (!cu->checked_producer)
14104 check_producer (cu);
14105
14106 return cu->producer_is_gxx_lt_4_6;
14107 }
14108
14109
14110 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14111 with incorrect is_stmt attributes. */
14112
14113 static bool
14114 producer_is_codewarrior (struct dwarf2_cu *cu)
14115 {
14116 if (!cu->checked_producer)
14117 check_producer (cu);
14118
14119 return cu->producer_is_codewarrior;
14120 }
14121
14122 /* Return the default accessibility type if it is not overridden by
14123 DW_AT_accessibility. */
14124
14125 static enum dwarf_access_attribute
14126 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14127 {
14128 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14129 {
14130 /* The default DWARF 2 accessibility for members is public, the default
14131 accessibility for inheritance is private. */
14132
14133 if (die->tag != DW_TAG_inheritance)
14134 return DW_ACCESS_public;
14135 else
14136 return DW_ACCESS_private;
14137 }
14138 else
14139 {
14140 /* DWARF 3+ defines the default accessibility a different way. The same
14141 rules apply now for DW_TAG_inheritance as for the members and it only
14142 depends on the container kind. */
14143
14144 if (die->parent->tag == DW_TAG_class_type)
14145 return DW_ACCESS_private;
14146 else
14147 return DW_ACCESS_public;
14148 }
14149 }
14150
14151 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14152 offset. If the attribute was not found return 0, otherwise return
14153 1. If it was found but could not properly be handled, set *OFFSET
14154 to 0. */
14155
14156 static int
14157 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14158 LONGEST *offset)
14159 {
14160 struct attribute *attr;
14161
14162 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14163 if (attr != NULL)
14164 {
14165 *offset = 0;
14166
14167 /* Note that we do not check for a section offset first here.
14168 This is because DW_AT_data_member_location is new in DWARF 4,
14169 so if we see it, we can assume that a constant form is really
14170 a constant and not a section offset. */
14171 if (attr->form_is_constant ())
14172 *offset = dwarf2_get_attr_constant_value (attr, 0);
14173 else if (attr->form_is_section_offset ())
14174 dwarf2_complex_location_expr_complaint ();
14175 else if (attr->form_is_block ())
14176 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14177 else
14178 dwarf2_complex_location_expr_complaint ();
14179
14180 return 1;
14181 }
14182
14183 return 0;
14184 }
14185
14186 /* Add an aggregate field to the field list. */
14187
14188 static void
14189 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14190 struct dwarf2_cu *cu)
14191 {
14192 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14193 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14194 struct nextfield *new_field;
14195 struct attribute *attr;
14196 struct field *fp;
14197 const char *fieldname = "";
14198
14199 if (die->tag == DW_TAG_inheritance)
14200 {
14201 fip->baseclasses.emplace_back ();
14202 new_field = &fip->baseclasses.back ();
14203 }
14204 else
14205 {
14206 fip->fields.emplace_back ();
14207 new_field = &fip->fields.back ();
14208 }
14209
14210 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14211 if (attr != nullptr)
14212 new_field->accessibility = DW_UNSND (attr);
14213 else
14214 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14215 if (new_field->accessibility != DW_ACCESS_public)
14216 fip->non_public_fields = 1;
14217
14218 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14219 if (attr != nullptr)
14220 new_field->virtuality = DW_UNSND (attr);
14221 else
14222 new_field->virtuality = DW_VIRTUALITY_none;
14223
14224 fp = &new_field->field;
14225
14226 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14227 {
14228 LONGEST offset;
14229
14230 /* Data member other than a C++ static data member. */
14231
14232 /* Get type of field. */
14233 fp->type = die_type (die, cu);
14234
14235 SET_FIELD_BITPOS (*fp, 0);
14236
14237 /* Get bit size of field (zero if none). */
14238 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14239 if (attr != nullptr)
14240 {
14241 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14242 }
14243 else
14244 {
14245 FIELD_BITSIZE (*fp) = 0;
14246 }
14247
14248 /* Get bit offset of field. */
14249 if (handle_data_member_location (die, cu, &offset))
14250 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14251 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14252 if (attr != nullptr)
14253 {
14254 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14255 {
14256 /* For big endian bits, the DW_AT_bit_offset gives the
14257 additional bit offset from the MSB of the containing
14258 anonymous object to the MSB of the field. We don't
14259 have to do anything special since we don't need to
14260 know the size of the anonymous object. */
14261 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14262 }
14263 else
14264 {
14265 /* For little endian bits, compute the bit offset to the
14266 MSB of the anonymous object, subtract off the number of
14267 bits from the MSB of the field to the MSB of the
14268 object, and then subtract off the number of bits of
14269 the field itself. The result is the bit offset of
14270 the LSB of the field. */
14271 int anonymous_size;
14272 int bit_offset = DW_UNSND (attr);
14273
14274 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14275 if (attr != nullptr)
14276 {
14277 /* The size of the anonymous object containing
14278 the bit field is explicit, so use the
14279 indicated size (in bytes). */
14280 anonymous_size = DW_UNSND (attr);
14281 }
14282 else
14283 {
14284 /* The size of the anonymous object containing
14285 the bit field must be inferred from the type
14286 attribute of the data member containing the
14287 bit field. */
14288 anonymous_size = TYPE_LENGTH (fp->type);
14289 }
14290 SET_FIELD_BITPOS (*fp,
14291 (FIELD_BITPOS (*fp)
14292 + anonymous_size * bits_per_byte
14293 - bit_offset - FIELD_BITSIZE (*fp)));
14294 }
14295 }
14296 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14297 if (attr != NULL)
14298 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14299 + dwarf2_get_attr_constant_value (attr, 0)));
14300
14301 /* Get name of field. */
14302 fieldname = dwarf2_name (die, cu);
14303 if (fieldname == NULL)
14304 fieldname = "";
14305
14306 /* The name is already allocated along with this objfile, so we don't
14307 need to duplicate it for the type. */
14308 fp->name = fieldname;
14309
14310 /* Change accessibility for artificial fields (e.g. virtual table
14311 pointer or virtual base class pointer) to private. */
14312 if (dwarf2_attr (die, DW_AT_artificial, cu))
14313 {
14314 FIELD_ARTIFICIAL (*fp) = 1;
14315 new_field->accessibility = DW_ACCESS_private;
14316 fip->non_public_fields = 1;
14317 }
14318 }
14319 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14320 {
14321 /* C++ static member. */
14322
14323 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14324 is a declaration, but all versions of G++ as of this writing
14325 (so through at least 3.2.1) incorrectly generate
14326 DW_TAG_variable tags. */
14327
14328 const char *physname;
14329
14330 /* Get name of field. */
14331 fieldname = dwarf2_name (die, cu);
14332 if (fieldname == NULL)
14333 return;
14334
14335 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14336 if (attr
14337 /* Only create a symbol if this is an external value.
14338 new_symbol checks this and puts the value in the global symbol
14339 table, which we want. If it is not external, new_symbol
14340 will try to put the value in cu->list_in_scope which is wrong. */
14341 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14342 {
14343 /* A static const member, not much different than an enum as far as
14344 we're concerned, except that we can support more types. */
14345 new_symbol (die, NULL, cu);
14346 }
14347
14348 /* Get physical name. */
14349 physname = dwarf2_physname (fieldname, die, cu);
14350
14351 /* The name is already allocated along with this objfile, so we don't
14352 need to duplicate it for the type. */
14353 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14354 FIELD_TYPE (*fp) = die_type (die, cu);
14355 FIELD_NAME (*fp) = fieldname;
14356 }
14357 else if (die->tag == DW_TAG_inheritance)
14358 {
14359 LONGEST offset;
14360
14361 /* C++ base class field. */
14362 if (handle_data_member_location (die, cu, &offset))
14363 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14364 FIELD_BITSIZE (*fp) = 0;
14365 FIELD_TYPE (*fp) = die_type (die, cu);
14366 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
14367 }
14368 else if (die->tag == DW_TAG_variant_part)
14369 {
14370 /* process_structure_scope will treat this DIE as a union. */
14371 process_structure_scope (die, cu);
14372
14373 /* The variant part is relative to the start of the enclosing
14374 structure. */
14375 SET_FIELD_BITPOS (*fp, 0);
14376 fp->type = get_die_type (die, cu);
14377 fp->artificial = 1;
14378 fp->name = "<<variant>>";
14379
14380 /* Normally a DW_TAG_variant_part won't have a size, but our
14381 representation requires one, so set it to the maximum of the
14382 child sizes, being sure to account for the offset at which
14383 each child is seen. */
14384 if (TYPE_LENGTH (fp->type) == 0)
14385 {
14386 unsigned max = 0;
14387 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
14388 {
14389 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
14390 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
14391 if (len > max)
14392 max = len;
14393 }
14394 TYPE_LENGTH (fp->type) = max;
14395 }
14396 }
14397 else
14398 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14399 }
14400
14401 /* Can the type given by DIE define another type? */
14402
14403 static bool
14404 type_can_define_types (const struct die_info *die)
14405 {
14406 switch (die->tag)
14407 {
14408 case DW_TAG_typedef:
14409 case DW_TAG_class_type:
14410 case DW_TAG_structure_type:
14411 case DW_TAG_union_type:
14412 case DW_TAG_enumeration_type:
14413 return true;
14414
14415 default:
14416 return false;
14417 }
14418 }
14419
14420 /* Add a type definition defined in the scope of the FIP's class. */
14421
14422 static void
14423 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14424 struct dwarf2_cu *cu)
14425 {
14426 struct decl_field fp;
14427 memset (&fp, 0, sizeof (fp));
14428
14429 gdb_assert (type_can_define_types (die));
14430
14431 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14432 fp.name = dwarf2_name (die, cu);
14433 fp.type = read_type_die (die, cu);
14434
14435 /* Save accessibility. */
14436 enum dwarf_access_attribute accessibility;
14437 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14438 if (attr != NULL)
14439 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14440 else
14441 accessibility = dwarf2_default_access_attribute (die, cu);
14442 switch (accessibility)
14443 {
14444 case DW_ACCESS_public:
14445 /* The assumed value if neither private nor protected. */
14446 break;
14447 case DW_ACCESS_private:
14448 fp.is_private = 1;
14449 break;
14450 case DW_ACCESS_protected:
14451 fp.is_protected = 1;
14452 break;
14453 default:
14454 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14455 }
14456
14457 if (die->tag == DW_TAG_typedef)
14458 fip->typedef_field_list.push_back (fp);
14459 else
14460 fip->nested_types_list.push_back (fp);
14461 }
14462
14463 /* Create the vector of fields, and attach it to the type. */
14464
14465 static void
14466 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14467 struct dwarf2_cu *cu)
14468 {
14469 int nfields = fip->nfields ();
14470
14471 /* Record the field count, allocate space for the array of fields,
14472 and create blank accessibility bitfields if necessary. */
14473 TYPE_NFIELDS (type) = nfields;
14474 TYPE_FIELDS (type) = (struct field *)
14475 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
14476
14477 if (fip->non_public_fields && cu->language != language_ada)
14478 {
14479 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14480
14481 TYPE_FIELD_PRIVATE_BITS (type) =
14482 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14483 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14484
14485 TYPE_FIELD_PROTECTED_BITS (type) =
14486 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14487 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14488
14489 TYPE_FIELD_IGNORE_BITS (type) =
14490 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14491 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14492 }
14493
14494 /* If the type has baseclasses, allocate and clear a bit vector for
14495 TYPE_FIELD_VIRTUAL_BITS. */
14496 if (!fip->baseclasses.empty () && cu->language != language_ada)
14497 {
14498 int num_bytes = B_BYTES (fip->baseclasses.size ());
14499 unsigned char *pointer;
14500
14501 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14502 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14503 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14504 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14505 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14506 }
14507
14508 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
14509 {
14510 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
14511
14512 for (int index = 0; index < nfields; ++index)
14513 {
14514 struct nextfield &field = fip->fields[index];
14515
14516 if (field.variant.is_discriminant)
14517 di->discriminant_index = index;
14518 else if (field.variant.default_branch)
14519 di->default_index = index;
14520 else
14521 di->discriminants[index] = field.variant.discriminant_value;
14522 }
14523 }
14524
14525 /* Copy the saved-up fields into the field vector. */
14526 for (int i = 0; i < nfields; ++i)
14527 {
14528 struct nextfield &field
14529 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14530 : fip->fields[i - fip->baseclasses.size ()]);
14531
14532 TYPE_FIELD (type, i) = field.field;
14533 switch (field.accessibility)
14534 {
14535 case DW_ACCESS_private:
14536 if (cu->language != language_ada)
14537 SET_TYPE_FIELD_PRIVATE (type, i);
14538 break;
14539
14540 case DW_ACCESS_protected:
14541 if (cu->language != language_ada)
14542 SET_TYPE_FIELD_PROTECTED (type, i);
14543 break;
14544
14545 case DW_ACCESS_public:
14546 break;
14547
14548 default:
14549 /* Unknown accessibility. Complain and treat it as public. */
14550 {
14551 complaint (_("unsupported accessibility %d"),
14552 field.accessibility);
14553 }
14554 break;
14555 }
14556 if (i < fip->baseclasses.size ())
14557 {
14558 switch (field.virtuality)
14559 {
14560 case DW_VIRTUALITY_virtual:
14561 case DW_VIRTUALITY_pure_virtual:
14562 if (cu->language == language_ada)
14563 error (_("unexpected virtuality in component of Ada type"));
14564 SET_TYPE_FIELD_VIRTUAL (type, i);
14565 break;
14566 }
14567 }
14568 }
14569 }
14570
14571 /* Return true if this member function is a constructor, false
14572 otherwise. */
14573
14574 static int
14575 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14576 {
14577 const char *fieldname;
14578 const char *type_name;
14579 int len;
14580
14581 if (die->parent == NULL)
14582 return 0;
14583
14584 if (die->parent->tag != DW_TAG_structure_type
14585 && die->parent->tag != DW_TAG_union_type
14586 && die->parent->tag != DW_TAG_class_type)
14587 return 0;
14588
14589 fieldname = dwarf2_name (die, cu);
14590 type_name = dwarf2_name (die->parent, cu);
14591 if (fieldname == NULL || type_name == NULL)
14592 return 0;
14593
14594 len = strlen (fieldname);
14595 return (strncmp (fieldname, type_name, len) == 0
14596 && (type_name[len] == '\0' || type_name[len] == '<'));
14597 }
14598
14599 /* Check if the given VALUE is a recognized enum
14600 dwarf_defaulted_attribute constant according to DWARF5 spec,
14601 Table 7.24. */
14602
14603 static bool
14604 is_valid_DW_AT_defaulted (ULONGEST value)
14605 {
14606 switch (value)
14607 {
14608 case DW_DEFAULTED_no:
14609 case DW_DEFAULTED_in_class:
14610 case DW_DEFAULTED_out_of_class:
14611 return true;
14612 }
14613
14614 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
14615 return false;
14616 }
14617
14618 /* Add a member function to the proper fieldlist. */
14619
14620 static void
14621 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
14622 struct type *type, struct dwarf2_cu *cu)
14623 {
14624 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14625 struct attribute *attr;
14626 int i;
14627 struct fnfieldlist *flp = nullptr;
14628 struct fn_field *fnp;
14629 const char *fieldname;
14630 struct type *this_type;
14631 enum dwarf_access_attribute accessibility;
14632
14633 if (cu->language == language_ada)
14634 error (_("unexpected member function in Ada type"));
14635
14636 /* Get name of member function. */
14637 fieldname = dwarf2_name (die, cu);
14638 if (fieldname == NULL)
14639 return;
14640
14641 /* Look up member function name in fieldlist. */
14642 for (i = 0; i < fip->fnfieldlists.size (); i++)
14643 {
14644 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
14645 {
14646 flp = &fip->fnfieldlists[i];
14647 break;
14648 }
14649 }
14650
14651 /* Create a new fnfieldlist if necessary. */
14652 if (flp == nullptr)
14653 {
14654 fip->fnfieldlists.emplace_back ();
14655 flp = &fip->fnfieldlists.back ();
14656 flp->name = fieldname;
14657 i = fip->fnfieldlists.size () - 1;
14658 }
14659
14660 /* Create a new member function field and add it to the vector of
14661 fnfieldlists. */
14662 flp->fnfields.emplace_back ();
14663 fnp = &flp->fnfields.back ();
14664
14665 /* Delay processing of the physname until later. */
14666 if (cu->language == language_cplus)
14667 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14668 die, cu);
14669 else
14670 {
14671 const char *physname = dwarf2_physname (fieldname, die, cu);
14672 fnp->physname = physname ? physname : "";
14673 }
14674
14675 fnp->type = alloc_type (objfile);
14676 this_type = read_type_die (die, cu);
14677 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
14678 {
14679 int nparams = TYPE_NFIELDS (this_type);
14680
14681 /* TYPE is the domain of this method, and THIS_TYPE is the type
14682 of the method itself (TYPE_CODE_METHOD). */
14683 smash_to_method_type (fnp->type, type,
14684 TYPE_TARGET_TYPE (this_type),
14685 TYPE_FIELDS (this_type),
14686 TYPE_NFIELDS (this_type),
14687 TYPE_VARARGS (this_type));
14688
14689 /* Handle static member functions.
14690 Dwarf2 has no clean way to discern C++ static and non-static
14691 member functions. G++ helps GDB by marking the first
14692 parameter for non-static member functions (which is the this
14693 pointer) as artificial. We obtain this information from
14694 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
14695 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
14696 fnp->voffset = VOFFSET_STATIC;
14697 }
14698 else
14699 complaint (_("member function type missing for '%s'"),
14700 dwarf2_full_name (fieldname, die, cu));
14701
14702 /* Get fcontext from DW_AT_containing_type if present. */
14703 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
14704 fnp->fcontext = die_containing_type (die, cu);
14705
14706 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14707 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
14708
14709 /* Get accessibility. */
14710 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14711 if (attr != nullptr)
14712 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14713 else
14714 accessibility = dwarf2_default_access_attribute (die, cu);
14715 switch (accessibility)
14716 {
14717 case DW_ACCESS_private:
14718 fnp->is_private = 1;
14719 break;
14720 case DW_ACCESS_protected:
14721 fnp->is_protected = 1;
14722 break;
14723 }
14724
14725 /* Check for artificial methods. */
14726 attr = dwarf2_attr (die, DW_AT_artificial, cu);
14727 if (attr && DW_UNSND (attr) != 0)
14728 fnp->is_artificial = 1;
14729
14730 /* Check for defaulted methods. */
14731 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
14732 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
14733 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
14734
14735 /* Check for deleted methods. */
14736 attr = dwarf2_attr (die, DW_AT_deleted, cu);
14737 if (attr != nullptr && DW_UNSND (attr) != 0)
14738 fnp->is_deleted = 1;
14739
14740 fnp->is_constructor = dwarf2_is_constructor (die, cu);
14741
14742 /* Get index in virtual function table if it is a virtual member
14743 function. For older versions of GCC, this is an offset in the
14744 appropriate virtual table, as specified by DW_AT_containing_type.
14745 For everyone else, it is an expression to be evaluated relative
14746 to the object address. */
14747
14748 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
14749 if (attr != nullptr)
14750 {
14751 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
14752 {
14753 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
14754 {
14755 /* Old-style GCC. */
14756 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
14757 }
14758 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
14759 || (DW_BLOCK (attr)->size > 1
14760 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
14761 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
14762 {
14763 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
14764 if ((fnp->voffset % cu->header.addr_size) != 0)
14765 dwarf2_complex_location_expr_complaint ();
14766 else
14767 fnp->voffset /= cu->header.addr_size;
14768 fnp->voffset += 2;
14769 }
14770 else
14771 dwarf2_complex_location_expr_complaint ();
14772
14773 if (!fnp->fcontext)
14774 {
14775 /* If there is no `this' field and no DW_AT_containing_type,
14776 we cannot actually find a base class context for the
14777 vtable! */
14778 if (TYPE_NFIELDS (this_type) == 0
14779 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
14780 {
14781 complaint (_("cannot determine context for virtual member "
14782 "function \"%s\" (offset %s)"),
14783 fieldname, sect_offset_str (die->sect_off));
14784 }
14785 else
14786 {
14787 fnp->fcontext
14788 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
14789 }
14790 }
14791 }
14792 else if (attr->form_is_section_offset ())
14793 {
14794 dwarf2_complex_location_expr_complaint ();
14795 }
14796 else
14797 {
14798 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14799 fieldname);
14800 }
14801 }
14802 else
14803 {
14804 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14805 if (attr && DW_UNSND (attr))
14806 {
14807 /* GCC does this, as of 2008-08-25; PR debug/37237. */
14808 complaint (_("Member function \"%s\" (offset %s) is virtual "
14809 "but the vtable offset is not specified"),
14810 fieldname, sect_offset_str (die->sect_off));
14811 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14812 TYPE_CPLUS_DYNAMIC (type) = 1;
14813 }
14814 }
14815 }
14816
14817 /* Create the vector of member function fields, and attach it to the type. */
14818
14819 static void
14820 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
14821 struct dwarf2_cu *cu)
14822 {
14823 if (cu->language == language_ada)
14824 error (_("unexpected member functions in Ada type"));
14825
14826 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14827 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
14828 TYPE_ALLOC (type,
14829 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
14830
14831 for (int i = 0; i < fip->fnfieldlists.size (); i++)
14832 {
14833 struct fnfieldlist &nf = fip->fnfieldlists[i];
14834 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
14835
14836 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
14837 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
14838 fn_flp->fn_fields = (struct fn_field *)
14839 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
14840
14841 for (int k = 0; k < nf.fnfields.size (); ++k)
14842 fn_flp->fn_fields[k] = nf.fnfields[k];
14843 }
14844
14845 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
14846 }
14847
14848 /* Returns non-zero if NAME is the name of a vtable member in CU's
14849 language, zero otherwise. */
14850 static int
14851 is_vtable_name (const char *name, struct dwarf2_cu *cu)
14852 {
14853 static const char vptr[] = "_vptr";
14854
14855 /* Look for the C++ form of the vtable. */
14856 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
14857 return 1;
14858
14859 return 0;
14860 }
14861
14862 /* GCC outputs unnamed structures that are really pointers to member
14863 functions, with the ABI-specified layout. If TYPE describes
14864 such a structure, smash it into a member function type.
14865
14866 GCC shouldn't do this; it should just output pointer to member DIEs.
14867 This is GCC PR debug/28767. */
14868
14869 static void
14870 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
14871 {
14872 struct type *pfn_type, *self_type, *new_type;
14873
14874 /* Check for a structure with no name and two children. */
14875 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
14876 return;
14877
14878 /* Check for __pfn and __delta members. */
14879 if (TYPE_FIELD_NAME (type, 0) == NULL
14880 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
14881 || TYPE_FIELD_NAME (type, 1) == NULL
14882 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
14883 return;
14884
14885 /* Find the type of the method. */
14886 pfn_type = TYPE_FIELD_TYPE (type, 0);
14887 if (pfn_type == NULL
14888 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
14889 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
14890 return;
14891
14892 /* Look for the "this" argument. */
14893 pfn_type = TYPE_TARGET_TYPE (pfn_type);
14894 if (TYPE_NFIELDS (pfn_type) == 0
14895 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
14896 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
14897 return;
14898
14899 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
14900 new_type = alloc_type (objfile);
14901 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
14902 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
14903 TYPE_VARARGS (pfn_type));
14904 smash_to_methodptr_type (type, new_type);
14905 }
14906
14907 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
14908 appropriate error checking and issuing complaints if there is a
14909 problem. */
14910
14911 static ULONGEST
14912 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
14913 {
14914 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
14915
14916 if (attr == nullptr)
14917 return 0;
14918
14919 if (!attr->form_is_constant ())
14920 {
14921 complaint (_("DW_AT_alignment must have constant form"
14922 " - DIE at %s [in module %s]"),
14923 sect_offset_str (die->sect_off),
14924 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14925 return 0;
14926 }
14927
14928 ULONGEST align;
14929 if (attr->form == DW_FORM_sdata)
14930 {
14931 LONGEST val = DW_SND (attr);
14932 if (val < 0)
14933 {
14934 complaint (_("DW_AT_alignment value must not be negative"
14935 " - DIE at %s [in module %s]"),
14936 sect_offset_str (die->sect_off),
14937 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14938 return 0;
14939 }
14940 align = val;
14941 }
14942 else
14943 align = DW_UNSND (attr);
14944
14945 if (align == 0)
14946 {
14947 complaint (_("DW_AT_alignment value must not be zero"
14948 " - DIE at %s [in module %s]"),
14949 sect_offset_str (die->sect_off),
14950 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14951 return 0;
14952 }
14953 if ((align & (align - 1)) != 0)
14954 {
14955 complaint (_("DW_AT_alignment value must be a power of 2"
14956 " - DIE at %s [in module %s]"),
14957 sect_offset_str (die->sect_off),
14958 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14959 return 0;
14960 }
14961
14962 return align;
14963 }
14964
14965 /* If the DIE has a DW_AT_alignment attribute, use its value to set
14966 the alignment for TYPE. */
14967
14968 static void
14969 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
14970 struct type *type)
14971 {
14972 if (!set_type_align (type, get_alignment (cu, die)))
14973 complaint (_("DW_AT_alignment value too large"
14974 " - DIE at %s [in module %s]"),
14975 sect_offset_str (die->sect_off),
14976 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14977 }
14978
14979 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14980 constant for a type, according to DWARF5 spec, Table 5.5. */
14981
14982 static bool
14983 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
14984 {
14985 switch (value)
14986 {
14987 case DW_CC_normal:
14988 case DW_CC_pass_by_reference:
14989 case DW_CC_pass_by_value:
14990 return true;
14991
14992 default:
14993 complaint (_("unrecognized DW_AT_calling_convention value "
14994 "(%s) for a type"), pulongest (value));
14995 return false;
14996 }
14997 }
14998
14999 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15000 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15001 also according to GNU-specific values (see include/dwarf2.h). */
15002
15003 static bool
15004 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15005 {
15006 switch (value)
15007 {
15008 case DW_CC_normal:
15009 case DW_CC_program:
15010 case DW_CC_nocall:
15011 return true;
15012
15013 case DW_CC_GNU_renesas_sh:
15014 case DW_CC_GNU_borland_fastcall_i386:
15015 case DW_CC_GDB_IBM_OpenCL:
15016 return true;
15017
15018 default:
15019 complaint (_("unrecognized DW_AT_calling_convention value "
15020 "(%s) for a subroutine"), pulongest (value));
15021 return false;
15022 }
15023 }
15024
15025 /* Called when we find the DIE that starts a structure or union scope
15026 (definition) to create a type for the structure or union. Fill in
15027 the type's name and general properties; the members will not be
15028 processed until process_structure_scope. A symbol table entry for
15029 the type will also not be done until process_structure_scope (assuming
15030 the type has a name).
15031
15032 NOTE: we need to call these functions regardless of whether or not the
15033 DIE has a DW_AT_name attribute, since it might be an anonymous
15034 structure or union. This gets the type entered into our set of
15035 user defined types. */
15036
15037 static struct type *
15038 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15039 {
15040 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15041 struct type *type;
15042 struct attribute *attr;
15043 const char *name;
15044
15045 /* If the definition of this type lives in .debug_types, read that type.
15046 Don't follow DW_AT_specification though, that will take us back up
15047 the chain and we want to go down. */
15048 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15049 if (attr != nullptr)
15050 {
15051 type = get_DW_AT_signature_type (die, attr, cu);
15052
15053 /* The type's CU may not be the same as CU.
15054 Ensure TYPE is recorded with CU in die_type_hash. */
15055 return set_die_type (die, type, cu);
15056 }
15057
15058 type = alloc_type (objfile);
15059 INIT_CPLUS_SPECIFIC (type);
15060
15061 name = dwarf2_name (die, cu);
15062 if (name != NULL)
15063 {
15064 if (cu->language == language_cplus
15065 || cu->language == language_d
15066 || cu->language == language_rust)
15067 {
15068 const char *full_name = dwarf2_full_name (name, die, cu);
15069
15070 /* dwarf2_full_name might have already finished building the DIE's
15071 type. If so, there is no need to continue. */
15072 if (get_die_type (die, cu) != NULL)
15073 return get_die_type (die, cu);
15074
15075 TYPE_NAME (type) = full_name;
15076 }
15077 else
15078 {
15079 /* The name is already allocated along with this objfile, so
15080 we don't need to duplicate it for the type. */
15081 TYPE_NAME (type) = name;
15082 }
15083 }
15084
15085 if (die->tag == DW_TAG_structure_type)
15086 {
15087 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15088 }
15089 else if (die->tag == DW_TAG_union_type)
15090 {
15091 TYPE_CODE (type) = TYPE_CODE_UNION;
15092 }
15093 else if (die->tag == DW_TAG_variant_part)
15094 {
15095 TYPE_CODE (type) = TYPE_CODE_UNION;
15096 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15097 }
15098 else
15099 {
15100 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15101 }
15102
15103 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15104 TYPE_DECLARED_CLASS (type) = 1;
15105
15106 /* Store the calling convention in the type if it's available in
15107 the die. Otherwise the calling convention remains set to
15108 the default value DW_CC_normal. */
15109 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15110 if (attr != nullptr
15111 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15112 {
15113 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15114 TYPE_CPLUS_CALLING_CONVENTION (type)
15115 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15116 }
15117
15118 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15119 if (attr != nullptr)
15120 {
15121 if (attr->form_is_constant ())
15122 TYPE_LENGTH (type) = DW_UNSND (attr);
15123 else
15124 {
15125 /* For the moment, dynamic type sizes are not supported
15126 by GDB's struct type. The actual size is determined
15127 on-demand when resolving the type of a given object,
15128 so set the type's length to zero for now. Otherwise,
15129 we record an expression as the length, and that expression
15130 could lead to a very large value, which could eventually
15131 lead to us trying to allocate that much memory when creating
15132 a value of that type. */
15133 TYPE_LENGTH (type) = 0;
15134 }
15135 }
15136 else
15137 {
15138 TYPE_LENGTH (type) = 0;
15139 }
15140
15141 maybe_set_alignment (cu, die, type);
15142
15143 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15144 {
15145 /* ICC<14 does not output the required DW_AT_declaration on
15146 incomplete types, but gives them a size of zero. */
15147 TYPE_STUB (type) = 1;
15148 }
15149 else
15150 TYPE_STUB_SUPPORTED (type) = 1;
15151
15152 if (die_is_declaration (die, cu))
15153 TYPE_STUB (type) = 1;
15154 else if (attr == NULL && die->child == NULL
15155 && producer_is_realview (cu->producer))
15156 /* RealView does not output the required DW_AT_declaration
15157 on incomplete types. */
15158 TYPE_STUB (type) = 1;
15159
15160 /* We need to add the type field to the die immediately so we don't
15161 infinitely recurse when dealing with pointers to the structure
15162 type within the structure itself. */
15163 set_die_type (die, type, cu);
15164
15165 /* set_die_type should be already done. */
15166 set_descriptive_type (type, die, cu);
15167
15168 return type;
15169 }
15170
15171 /* A helper for process_structure_scope that handles a single member
15172 DIE. */
15173
15174 static void
15175 handle_struct_member_die (struct die_info *child_die, struct type *type,
15176 struct field_info *fi,
15177 std::vector<struct symbol *> *template_args,
15178 struct dwarf2_cu *cu)
15179 {
15180 if (child_die->tag == DW_TAG_member
15181 || child_die->tag == DW_TAG_variable
15182 || child_die->tag == DW_TAG_variant_part)
15183 {
15184 /* NOTE: carlton/2002-11-05: A C++ static data member
15185 should be a DW_TAG_member that is a declaration, but
15186 all versions of G++ as of this writing (so through at
15187 least 3.2.1) incorrectly generate DW_TAG_variable
15188 tags for them instead. */
15189 dwarf2_add_field (fi, child_die, cu);
15190 }
15191 else if (child_die->tag == DW_TAG_subprogram)
15192 {
15193 /* Rust doesn't have member functions in the C++ sense.
15194 However, it does emit ordinary functions as children
15195 of a struct DIE. */
15196 if (cu->language == language_rust)
15197 read_func_scope (child_die, cu);
15198 else
15199 {
15200 /* C++ member function. */
15201 dwarf2_add_member_fn (fi, child_die, type, cu);
15202 }
15203 }
15204 else if (child_die->tag == DW_TAG_inheritance)
15205 {
15206 /* C++ base class field. */
15207 dwarf2_add_field (fi, child_die, cu);
15208 }
15209 else if (type_can_define_types (child_die))
15210 dwarf2_add_type_defn (fi, child_die, cu);
15211 else if (child_die->tag == DW_TAG_template_type_param
15212 || child_die->tag == DW_TAG_template_value_param)
15213 {
15214 struct symbol *arg = new_symbol (child_die, NULL, cu);
15215
15216 if (arg != NULL)
15217 template_args->push_back (arg);
15218 }
15219 else if (child_die->tag == DW_TAG_variant)
15220 {
15221 /* In a variant we want to get the discriminant and also add a
15222 field for our sole member child. */
15223 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15224
15225 for (die_info *variant_child = child_die->child;
15226 variant_child != NULL;
15227 variant_child = sibling_die (variant_child))
15228 {
15229 if (variant_child->tag == DW_TAG_member)
15230 {
15231 handle_struct_member_die (variant_child, type, fi,
15232 template_args, cu);
15233 /* Only handle the one. */
15234 break;
15235 }
15236 }
15237
15238 /* We don't handle this but we might as well report it if we see
15239 it. */
15240 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
15241 complaint (_("DW_AT_discr_list is not supported yet"
15242 " - DIE at %s [in module %s]"),
15243 sect_offset_str (child_die->sect_off),
15244 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15245
15246 /* The first field was just added, so we can stash the
15247 discriminant there. */
15248 gdb_assert (!fi->fields.empty ());
15249 if (discr == NULL)
15250 fi->fields.back ().variant.default_branch = true;
15251 else
15252 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
15253 }
15254 }
15255
15256 /* Finish creating a structure or union type, including filling in
15257 its members and creating a symbol for it. */
15258
15259 static void
15260 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15261 {
15262 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15263 struct die_info *child_die;
15264 struct type *type;
15265
15266 type = get_die_type (die, cu);
15267 if (type == NULL)
15268 type = read_structure_type (die, cu);
15269
15270 /* When reading a DW_TAG_variant_part, we need to notice when we
15271 read the discriminant member, so we can record it later in the
15272 discriminant_info. */
15273 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
15274 sect_offset discr_offset {};
15275 bool has_template_parameters = false;
15276
15277 if (is_variant_part)
15278 {
15279 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15280 if (discr == NULL)
15281 {
15282 /* Maybe it's a univariant form, an extension we support.
15283 In this case arrange not to check the offset. */
15284 is_variant_part = false;
15285 }
15286 else if (discr->form_is_ref ())
15287 {
15288 struct dwarf2_cu *target_cu = cu;
15289 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15290
15291 discr_offset = target_die->sect_off;
15292 }
15293 else
15294 {
15295 complaint (_("DW_AT_discr does not have DIE reference form"
15296 " - DIE at %s [in module %s]"),
15297 sect_offset_str (die->sect_off),
15298 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15299 is_variant_part = false;
15300 }
15301 }
15302
15303 if (die->child != NULL && ! die_is_declaration (die, cu))
15304 {
15305 struct field_info fi;
15306 std::vector<struct symbol *> template_args;
15307
15308 child_die = die->child;
15309
15310 while (child_die && child_die->tag)
15311 {
15312 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15313
15314 if (is_variant_part && discr_offset == child_die->sect_off)
15315 fi.fields.back ().variant.is_discriminant = true;
15316
15317 child_die = sibling_die (child_die);
15318 }
15319
15320 /* Attach template arguments to type. */
15321 if (!template_args.empty ())
15322 {
15323 has_template_parameters = true;
15324 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15325 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15326 TYPE_TEMPLATE_ARGUMENTS (type)
15327 = XOBNEWVEC (&objfile->objfile_obstack,
15328 struct symbol *,
15329 TYPE_N_TEMPLATE_ARGUMENTS (type));
15330 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15331 template_args.data (),
15332 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15333 * sizeof (struct symbol *)));
15334 }
15335
15336 /* Attach fields and member functions to the type. */
15337 if (fi.nfields () > 0)
15338 dwarf2_attach_fields_to_type (&fi, type, cu);
15339 if (!fi.fnfieldlists.empty ())
15340 {
15341 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15342
15343 /* Get the type which refers to the base class (possibly this
15344 class itself) which contains the vtable pointer for the current
15345 class from the DW_AT_containing_type attribute. This use of
15346 DW_AT_containing_type is a GNU extension. */
15347
15348 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15349 {
15350 struct type *t = die_containing_type (die, cu);
15351
15352 set_type_vptr_basetype (type, t);
15353 if (type == t)
15354 {
15355 int i;
15356
15357 /* Our own class provides vtbl ptr. */
15358 for (i = TYPE_NFIELDS (t) - 1;
15359 i >= TYPE_N_BASECLASSES (t);
15360 --i)
15361 {
15362 const char *fieldname = TYPE_FIELD_NAME (t, i);
15363
15364 if (is_vtable_name (fieldname, cu))
15365 {
15366 set_type_vptr_fieldno (type, i);
15367 break;
15368 }
15369 }
15370
15371 /* Complain if virtual function table field not found. */
15372 if (i < TYPE_N_BASECLASSES (t))
15373 complaint (_("virtual function table pointer "
15374 "not found when defining class '%s'"),
15375 TYPE_NAME (type) ? TYPE_NAME (type) : "");
15376 }
15377 else
15378 {
15379 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15380 }
15381 }
15382 else if (cu->producer
15383 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15384 {
15385 /* The IBM XLC compiler does not provide direct indication
15386 of the containing type, but the vtable pointer is
15387 always named __vfp. */
15388
15389 int i;
15390
15391 for (i = TYPE_NFIELDS (type) - 1;
15392 i >= TYPE_N_BASECLASSES (type);
15393 --i)
15394 {
15395 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15396 {
15397 set_type_vptr_fieldno (type, i);
15398 set_type_vptr_basetype (type, type);
15399 break;
15400 }
15401 }
15402 }
15403 }
15404
15405 /* Copy fi.typedef_field_list linked list elements content into the
15406 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15407 if (!fi.typedef_field_list.empty ())
15408 {
15409 int count = fi.typedef_field_list.size ();
15410
15411 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15412 TYPE_TYPEDEF_FIELD_ARRAY (type)
15413 = ((struct decl_field *)
15414 TYPE_ALLOC (type,
15415 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15416 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15417
15418 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15419 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15420 }
15421
15422 /* Copy fi.nested_types_list linked list elements content into the
15423 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15424 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15425 {
15426 int count = fi.nested_types_list.size ();
15427
15428 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15429 TYPE_NESTED_TYPES_ARRAY (type)
15430 = ((struct decl_field *)
15431 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15432 TYPE_NESTED_TYPES_COUNT (type) = count;
15433
15434 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15435 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15436 }
15437 }
15438
15439 quirk_gcc_member_function_pointer (type, objfile);
15440 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15441 cu->rust_unions.push_back (type);
15442
15443 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15444 snapshots) has been known to create a die giving a declaration
15445 for a class that has, as a child, a die giving a definition for a
15446 nested class. So we have to process our children even if the
15447 current die is a declaration. Normally, of course, a declaration
15448 won't have any children at all. */
15449
15450 child_die = die->child;
15451
15452 while (child_die != NULL && child_die->tag)
15453 {
15454 if (child_die->tag == DW_TAG_member
15455 || child_die->tag == DW_TAG_variable
15456 || child_die->tag == DW_TAG_inheritance
15457 || child_die->tag == DW_TAG_template_value_param
15458 || child_die->tag == DW_TAG_template_type_param)
15459 {
15460 /* Do nothing. */
15461 }
15462 else
15463 process_die (child_die, cu);
15464
15465 child_die = sibling_die (child_die);
15466 }
15467
15468 /* Do not consider external references. According to the DWARF standard,
15469 these DIEs are identified by the fact that they have no byte_size
15470 attribute, and a declaration attribute. */
15471 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15472 || !die_is_declaration (die, cu))
15473 {
15474 struct symbol *sym = new_symbol (die, type, cu);
15475
15476 if (has_template_parameters)
15477 {
15478 struct symtab *symtab;
15479 if (sym != nullptr)
15480 symtab = symbol_symtab (sym);
15481 else if (cu->line_header != nullptr)
15482 {
15483 /* Any related symtab will do. */
15484 symtab
15485 = cu->line_header->file_names ()[0].symtab;
15486 }
15487 else
15488 {
15489 symtab = nullptr;
15490 complaint (_("could not find suitable "
15491 "symtab for template parameter"
15492 " - DIE at %s [in module %s]"),
15493 sect_offset_str (die->sect_off),
15494 objfile_name (objfile));
15495 }
15496
15497 if (symtab != nullptr)
15498 {
15499 /* Make sure that the symtab is set on the new symbols.
15500 Even though they don't appear in this symtab directly,
15501 other parts of gdb assume that symbols do, and this is
15502 reasonably true. */
15503 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15504 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15505 }
15506 }
15507 }
15508 }
15509
15510 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
15511 update TYPE using some information only available in DIE's children. */
15512
15513 static void
15514 update_enumeration_type_from_children (struct die_info *die,
15515 struct type *type,
15516 struct dwarf2_cu *cu)
15517 {
15518 struct die_info *child_die;
15519 int unsigned_enum = 1;
15520 int flag_enum = 1;
15521
15522 auto_obstack obstack;
15523
15524 for (child_die = die->child;
15525 child_die != NULL && child_die->tag;
15526 child_die = sibling_die (child_die))
15527 {
15528 struct attribute *attr;
15529 LONGEST value;
15530 const gdb_byte *bytes;
15531 struct dwarf2_locexpr_baton *baton;
15532 const char *name;
15533
15534 if (child_die->tag != DW_TAG_enumerator)
15535 continue;
15536
15537 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15538 if (attr == NULL)
15539 continue;
15540
15541 name = dwarf2_name (child_die, cu);
15542 if (name == NULL)
15543 name = "<anonymous enumerator>";
15544
15545 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15546 &value, &bytes, &baton);
15547 if (value < 0)
15548 {
15549 unsigned_enum = 0;
15550 flag_enum = 0;
15551 }
15552 else
15553 {
15554 if (count_one_bits_ll (value) >= 2)
15555 flag_enum = 0;
15556 }
15557
15558 /* If we already know that the enum type is neither unsigned, nor
15559 a flag type, no need to look at the rest of the enumerates. */
15560 if (!unsigned_enum && !flag_enum)
15561 break;
15562 }
15563
15564 if (unsigned_enum)
15565 TYPE_UNSIGNED (type) = 1;
15566 if (flag_enum)
15567 TYPE_FLAG_ENUM (type) = 1;
15568 }
15569
15570 /* Given a DW_AT_enumeration_type die, set its type. We do not
15571 complete the type's fields yet, or create any symbols. */
15572
15573 static struct type *
15574 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
15575 {
15576 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15577 struct type *type;
15578 struct attribute *attr;
15579 const char *name;
15580
15581 /* If the definition of this type lives in .debug_types, read that type.
15582 Don't follow DW_AT_specification though, that will take us back up
15583 the chain and we want to go down. */
15584 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15585 if (attr != nullptr)
15586 {
15587 type = get_DW_AT_signature_type (die, attr, cu);
15588
15589 /* The type's CU may not be the same as CU.
15590 Ensure TYPE is recorded with CU in die_type_hash. */
15591 return set_die_type (die, type, cu);
15592 }
15593
15594 type = alloc_type (objfile);
15595
15596 TYPE_CODE (type) = TYPE_CODE_ENUM;
15597 name = dwarf2_full_name (NULL, die, cu);
15598 if (name != NULL)
15599 TYPE_NAME (type) = name;
15600
15601 attr = dwarf2_attr (die, DW_AT_type, cu);
15602 if (attr != NULL)
15603 {
15604 struct type *underlying_type = die_type (die, cu);
15605
15606 TYPE_TARGET_TYPE (type) = underlying_type;
15607 }
15608
15609 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15610 if (attr != nullptr)
15611 {
15612 TYPE_LENGTH (type) = DW_UNSND (attr);
15613 }
15614 else
15615 {
15616 TYPE_LENGTH (type) = 0;
15617 }
15618
15619 maybe_set_alignment (cu, die, type);
15620
15621 /* The enumeration DIE can be incomplete. In Ada, any type can be
15622 declared as private in the package spec, and then defined only
15623 inside the package body. Such types are known as Taft Amendment
15624 Types. When another package uses such a type, an incomplete DIE
15625 may be generated by the compiler. */
15626 if (die_is_declaration (die, cu))
15627 TYPE_STUB (type) = 1;
15628
15629 /* Finish the creation of this type by using the enum's children.
15630 We must call this even when the underlying type has been provided
15631 so that we can determine if we're looking at a "flag" enum. */
15632 update_enumeration_type_from_children (die, type, cu);
15633
15634 /* If this type has an underlying type that is not a stub, then we
15635 may use its attributes. We always use the "unsigned" attribute
15636 in this situation, because ordinarily we guess whether the type
15637 is unsigned -- but the guess can be wrong and the underlying type
15638 can tell us the reality. However, we defer to a local size
15639 attribute if one exists, because this lets the compiler override
15640 the underlying type if needed. */
15641 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
15642 {
15643 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
15644 if (TYPE_LENGTH (type) == 0)
15645 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
15646 if (TYPE_RAW_ALIGN (type) == 0
15647 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
15648 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
15649 }
15650
15651 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
15652
15653 return set_die_type (die, type, cu);
15654 }
15655
15656 /* Given a pointer to a die which begins an enumeration, process all
15657 the dies that define the members of the enumeration, and create the
15658 symbol for the enumeration type.
15659
15660 NOTE: We reverse the order of the element list. */
15661
15662 static void
15663 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
15664 {
15665 struct type *this_type;
15666
15667 this_type = get_die_type (die, cu);
15668 if (this_type == NULL)
15669 this_type = read_enumeration_type (die, cu);
15670
15671 if (die->child != NULL)
15672 {
15673 struct die_info *child_die;
15674 struct symbol *sym;
15675 std::vector<struct field> fields;
15676 const char *name;
15677
15678 child_die = die->child;
15679 while (child_die && child_die->tag)
15680 {
15681 if (child_die->tag != DW_TAG_enumerator)
15682 {
15683 process_die (child_die, cu);
15684 }
15685 else
15686 {
15687 name = dwarf2_name (child_die, cu);
15688 if (name)
15689 {
15690 sym = new_symbol (child_die, this_type, cu);
15691
15692 fields.emplace_back ();
15693 struct field &field = fields.back ();
15694
15695 FIELD_NAME (field) = sym->linkage_name ();
15696 FIELD_TYPE (field) = NULL;
15697 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
15698 FIELD_BITSIZE (field) = 0;
15699 }
15700 }
15701
15702 child_die = sibling_die (child_die);
15703 }
15704
15705 if (!fields.empty ())
15706 {
15707 TYPE_NFIELDS (this_type) = fields.size ();
15708 TYPE_FIELDS (this_type) = (struct field *)
15709 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
15710 memcpy (TYPE_FIELDS (this_type), fields.data (),
15711 sizeof (struct field) * fields.size ());
15712 }
15713 }
15714
15715 /* If we are reading an enum from a .debug_types unit, and the enum
15716 is a declaration, and the enum is not the signatured type in the
15717 unit, then we do not want to add a symbol for it. Adding a
15718 symbol would in some cases obscure the true definition of the
15719 enum, giving users an incomplete type when the definition is
15720 actually available. Note that we do not want to do this for all
15721 enums which are just declarations, because C++0x allows forward
15722 enum declarations. */
15723 if (cu->per_cu->is_debug_types
15724 && die_is_declaration (die, cu))
15725 {
15726 struct signatured_type *sig_type;
15727
15728 sig_type = (struct signatured_type *) cu->per_cu;
15729 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
15730 if (sig_type->type_offset_in_section != die->sect_off)
15731 return;
15732 }
15733
15734 new_symbol (die, this_type, cu);
15735 }
15736
15737 /* Extract all information from a DW_TAG_array_type DIE and put it in
15738 the DIE's type field. For now, this only handles one dimensional
15739 arrays. */
15740
15741 static struct type *
15742 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
15743 {
15744 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15745 struct die_info *child_die;
15746 struct type *type;
15747 struct type *element_type, *range_type, *index_type;
15748 struct attribute *attr;
15749 const char *name;
15750 struct dynamic_prop *byte_stride_prop = NULL;
15751 unsigned int bit_stride = 0;
15752
15753 element_type = die_type (die, cu);
15754
15755 /* The die_type call above may have already set the type for this DIE. */
15756 type = get_die_type (die, cu);
15757 if (type)
15758 return type;
15759
15760 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
15761 if (attr != NULL)
15762 {
15763 int stride_ok;
15764 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
15765
15766 byte_stride_prop
15767 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
15768 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
15769 prop_type);
15770 if (!stride_ok)
15771 {
15772 complaint (_("unable to read array DW_AT_byte_stride "
15773 " - DIE at %s [in module %s]"),
15774 sect_offset_str (die->sect_off),
15775 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15776 /* Ignore this attribute. We will likely not be able to print
15777 arrays of this type correctly, but there is little we can do
15778 to help if we cannot read the attribute's value. */
15779 byte_stride_prop = NULL;
15780 }
15781 }
15782
15783 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
15784 if (attr != NULL)
15785 bit_stride = DW_UNSND (attr);
15786
15787 /* Irix 6.2 native cc creates array types without children for
15788 arrays with unspecified length. */
15789 if (die->child == NULL)
15790 {
15791 index_type = objfile_type (objfile)->builtin_int;
15792 range_type = create_static_range_type (NULL, index_type, 0, -1);
15793 type = create_array_type_with_stride (NULL, element_type, range_type,
15794 byte_stride_prop, bit_stride);
15795 return set_die_type (die, type, cu);
15796 }
15797
15798 std::vector<struct type *> range_types;
15799 child_die = die->child;
15800 while (child_die && child_die->tag)
15801 {
15802 if (child_die->tag == DW_TAG_subrange_type)
15803 {
15804 struct type *child_type = read_type_die (child_die, cu);
15805
15806 if (child_type != NULL)
15807 {
15808 /* The range type was succesfully read. Save it for the
15809 array type creation. */
15810 range_types.push_back (child_type);
15811 }
15812 }
15813 child_die = sibling_die (child_die);
15814 }
15815
15816 /* Dwarf2 dimensions are output from left to right, create the
15817 necessary array types in backwards order. */
15818
15819 type = element_type;
15820
15821 if (read_array_order (die, cu) == DW_ORD_col_major)
15822 {
15823 int i = 0;
15824
15825 while (i < range_types.size ())
15826 type = create_array_type_with_stride (NULL, type, range_types[i++],
15827 byte_stride_prop, bit_stride);
15828 }
15829 else
15830 {
15831 size_t ndim = range_types.size ();
15832 while (ndim-- > 0)
15833 type = create_array_type_with_stride (NULL, type, range_types[ndim],
15834 byte_stride_prop, bit_stride);
15835 }
15836
15837 /* Understand Dwarf2 support for vector types (like they occur on
15838 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
15839 array type. This is not part of the Dwarf2/3 standard yet, but a
15840 custom vendor extension. The main difference between a regular
15841 array and the vector variant is that vectors are passed by value
15842 to functions. */
15843 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
15844 if (attr != nullptr)
15845 make_vector_type (type);
15846
15847 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
15848 implementation may choose to implement triple vectors using this
15849 attribute. */
15850 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15851 if (attr != nullptr)
15852 {
15853 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
15854 TYPE_LENGTH (type) = DW_UNSND (attr);
15855 else
15856 complaint (_("DW_AT_byte_size for array type smaller "
15857 "than the total size of elements"));
15858 }
15859
15860 name = dwarf2_name (die, cu);
15861 if (name)
15862 TYPE_NAME (type) = name;
15863
15864 maybe_set_alignment (cu, die, type);
15865
15866 /* Install the type in the die. */
15867 set_die_type (die, type, cu);
15868
15869 /* set_die_type should be already done. */
15870 set_descriptive_type (type, die, cu);
15871
15872 return type;
15873 }
15874
15875 static enum dwarf_array_dim_ordering
15876 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
15877 {
15878 struct attribute *attr;
15879
15880 attr = dwarf2_attr (die, DW_AT_ordering, cu);
15881
15882 if (attr != nullptr)
15883 return (enum dwarf_array_dim_ordering) DW_SND (attr);
15884
15885 /* GNU F77 is a special case, as at 08/2004 array type info is the
15886 opposite order to the dwarf2 specification, but data is still
15887 laid out as per normal fortran.
15888
15889 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
15890 version checking. */
15891
15892 if (cu->language == language_fortran
15893 && cu->producer && strstr (cu->producer, "GNU F77"))
15894 {
15895 return DW_ORD_row_major;
15896 }
15897
15898 switch (cu->language_defn->la_array_ordering)
15899 {
15900 case array_column_major:
15901 return DW_ORD_col_major;
15902 case array_row_major:
15903 default:
15904 return DW_ORD_row_major;
15905 };
15906 }
15907
15908 /* Extract all information from a DW_TAG_set_type DIE and put it in
15909 the DIE's type field. */
15910
15911 static struct type *
15912 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
15913 {
15914 struct type *domain_type, *set_type;
15915 struct attribute *attr;
15916
15917 domain_type = die_type (die, cu);
15918
15919 /* The die_type call above may have already set the type for this DIE. */
15920 set_type = get_die_type (die, cu);
15921 if (set_type)
15922 return set_type;
15923
15924 set_type = create_set_type (NULL, domain_type);
15925
15926 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15927 if (attr != nullptr)
15928 TYPE_LENGTH (set_type) = DW_UNSND (attr);
15929
15930 maybe_set_alignment (cu, die, set_type);
15931
15932 return set_die_type (die, set_type, cu);
15933 }
15934
15935 /* A helper for read_common_block that creates a locexpr baton.
15936 SYM is the symbol which we are marking as computed.
15937 COMMON_DIE is the DIE for the common block.
15938 COMMON_LOC is the location expression attribute for the common
15939 block itself.
15940 MEMBER_LOC is the location expression attribute for the particular
15941 member of the common block that we are processing.
15942 CU is the CU from which the above come. */
15943
15944 static void
15945 mark_common_block_symbol_computed (struct symbol *sym,
15946 struct die_info *common_die,
15947 struct attribute *common_loc,
15948 struct attribute *member_loc,
15949 struct dwarf2_cu *cu)
15950 {
15951 struct dwarf2_per_objfile *dwarf2_per_objfile
15952 = cu->per_cu->dwarf2_per_objfile;
15953 struct objfile *objfile = dwarf2_per_objfile->objfile;
15954 struct dwarf2_locexpr_baton *baton;
15955 gdb_byte *ptr;
15956 unsigned int cu_off;
15957 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
15958 LONGEST offset = 0;
15959
15960 gdb_assert (common_loc && member_loc);
15961 gdb_assert (common_loc->form_is_block ());
15962 gdb_assert (member_loc->form_is_block ()
15963 || member_loc->form_is_constant ());
15964
15965 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
15966 baton->per_cu = cu->per_cu;
15967 gdb_assert (baton->per_cu);
15968
15969 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15970
15971 if (member_loc->form_is_constant ())
15972 {
15973 offset = dwarf2_get_attr_constant_value (member_loc, 0);
15974 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
15975 }
15976 else
15977 baton->size += DW_BLOCK (member_loc)->size;
15978
15979 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
15980 baton->data = ptr;
15981
15982 *ptr++ = DW_OP_call4;
15983 cu_off = common_die->sect_off - cu->per_cu->sect_off;
15984 store_unsigned_integer (ptr, 4, byte_order, cu_off);
15985 ptr += 4;
15986
15987 if (member_loc->form_is_constant ())
15988 {
15989 *ptr++ = DW_OP_addr;
15990 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
15991 ptr += cu->header.addr_size;
15992 }
15993 else
15994 {
15995 /* We have to copy the data here, because DW_OP_call4 will only
15996 use a DW_AT_location attribute. */
15997 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
15998 ptr += DW_BLOCK (member_loc)->size;
15999 }
16000
16001 *ptr++ = DW_OP_plus;
16002 gdb_assert (ptr - baton->data == baton->size);
16003
16004 SYMBOL_LOCATION_BATON (sym) = baton;
16005 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16006 }
16007
16008 /* Create appropriate locally-scoped variables for all the
16009 DW_TAG_common_block entries. Also create a struct common_block
16010 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16011 is used to separate the common blocks name namespace from regular
16012 variable names. */
16013
16014 static void
16015 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16016 {
16017 struct attribute *attr;
16018
16019 attr = dwarf2_attr (die, DW_AT_location, cu);
16020 if (attr != nullptr)
16021 {
16022 /* Support the .debug_loc offsets. */
16023 if (attr->form_is_block ())
16024 {
16025 /* Ok. */
16026 }
16027 else if (attr->form_is_section_offset ())
16028 {
16029 dwarf2_complex_location_expr_complaint ();
16030 attr = NULL;
16031 }
16032 else
16033 {
16034 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16035 "common block member");
16036 attr = NULL;
16037 }
16038 }
16039
16040 if (die->child != NULL)
16041 {
16042 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16043 struct die_info *child_die;
16044 size_t n_entries = 0, size;
16045 struct common_block *common_block;
16046 struct symbol *sym;
16047
16048 for (child_die = die->child;
16049 child_die && child_die->tag;
16050 child_die = sibling_die (child_die))
16051 ++n_entries;
16052
16053 size = (sizeof (struct common_block)
16054 + (n_entries - 1) * sizeof (struct symbol *));
16055 common_block
16056 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16057 size);
16058 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16059 common_block->n_entries = 0;
16060
16061 for (child_die = die->child;
16062 child_die && child_die->tag;
16063 child_die = sibling_die (child_die))
16064 {
16065 /* Create the symbol in the DW_TAG_common_block block in the current
16066 symbol scope. */
16067 sym = new_symbol (child_die, NULL, cu);
16068 if (sym != NULL)
16069 {
16070 struct attribute *member_loc;
16071
16072 common_block->contents[common_block->n_entries++] = sym;
16073
16074 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16075 cu);
16076 if (member_loc)
16077 {
16078 /* GDB has handled this for a long time, but it is
16079 not specified by DWARF. It seems to have been
16080 emitted by gfortran at least as recently as:
16081 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16082 complaint (_("Variable in common block has "
16083 "DW_AT_data_member_location "
16084 "- DIE at %s [in module %s]"),
16085 sect_offset_str (child_die->sect_off),
16086 objfile_name (objfile));
16087
16088 if (member_loc->form_is_section_offset ())
16089 dwarf2_complex_location_expr_complaint ();
16090 else if (member_loc->form_is_constant ()
16091 || member_loc->form_is_block ())
16092 {
16093 if (attr != nullptr)
16094 mark_common_block_symbol_computed (sym, die, attr,
16095 member_loc, cu);
16096 }
16097 else
16098 dwarf2_complex_location_expr_complaint ();
16099 }
16100 }
16101 }
16102
16103 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16104 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16105 }
16106 }
16107
16108 /* Create a type for a C++ namespace. */
16109
16110 static struct type *
16111 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16112 {
16113 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16114 const char *previous_prefix, *name;
16115 int is_anonymous;
16116 struct type *type;
16117
16118 /* For extensions, reuse the type of the original namespace. */
16119 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16120 {
16121 struct die_info *ext_die;
16122 struct dwarf2_cu *ext_cu = cu;
16123
16124 ext_die = dwarf2_extension (die, &ext_cu);
16125 type = read_type_die (ext_die, ext_cu);
16126
16127 /* EXT_CU may not be the same as CU.
16128 Ensure TYPE is recorded with CU in die_type_hash. */
16129 return set_die_type (die, type, cu);
16130 }
16131
16132 name = namespace_name (die, &is_anonymous, cu);
16133
16134 /* Now build the name of the current namespace. */
16135
16136 previous_prefix = determine_prefix (die, cu);
16137 if (previous_prefix[0] != '\0')
16138 name = typename_concat (&objfile->objfile_obstack,
16139 previous_prefix, name, 0, cu);
16140
16141 /* Create the type. */
16142 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16143
16144 return set_die_type (die, type, cu);
16145 }
16146
16147 /* Read a namespace scope. */
16148
16149 static void
16150 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16151 {
16152 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16153 int is_anonymous;
16154
16155 /* Add a symbol associated to this if we haven't seen the namespace
16156 before. Also, add a using directive if it's an anonymous
16157 namespace. */
16158
16159 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16160 {
16161 struct type *type;
16162
16163 type = read_type_die (die, cu);
16164 new_symbol (die, type, cu);
16165
16166 namespace_name (die, &is_anonymous, cu);
16167 if (is_anonymous)
16168 {
16169 const char *previous_prefix = determine_prefix (die, cu);
16170
16171 std::vector<const char *> excludes;
16172 add_using_directive (using_directives (cu),
16173 previous_prefix, TYPE_NAME (type), NULL,
16174 NULL, excludes, 0, &objfile->objfile_obstack);
16175 }
16176 }
16177
16178 if (die->child != NULL)
16179 {
16180 struct die_info *child_die = die->child;
16181
16182 while (child_die && child_die->tag)
16183 {
16184 process_die (child_die, cu);
16185 child_die = sibling_die (child_die);
16186 }
16187 }
16188 }
16189
16190 /* Read a Fortran module as type. This DIE can be only a declaration used for
16191 imported module. Still we need that type as local Fortran "use ... only"
16192 declaration imports depend on the created type in determine_prefix. */
16193
16194 static struct type *
16195 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16196 {
16197 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16198 const char *module_name;
16199 struct type *type;
16200
16201 module_name = dwarf2_name (die, cu);
16202 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16203
16204 return set_die_type (die, type, cu);
16205 }
16206
16207 /* Read a Fortran module. */
16208
16209 static void
16210 read_module (struct die_info *die, struct dwarf2_cu *cu)
16211 {
16212 struct die_info *child_die = die->child;
16213 struct type *type;
16214
16215 type = read_type_die (die, cu);
16216 new_symbol (die, type, cu);
16217
16218 while (child_die && child_die->tag)
16219 {
16220 process_die (child_die, cu);
16221 child_die = sibling_die (child_die);
16222 }
16223 }
16224
16225 /* Return the name of the namespace represented by DIE. Set
16226 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16227 namespace. */
16228
16229 static const char *
16230 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16231 {
16232 struct die_info *current_die;
16233 const char *name = NULL;
16234
16235 /* Loop through the extensions until we find a name. */
16236
16237 for (current_die = die;
16238 current_die != NULL;
16239 current_die = dwarf2_extension (die, &cu))
16240 {
16241 /* We don't use dwarf2_name here so that we can detect the absence
16242 of a name -> anonymous namespace. */
16243 name = dwarf2_string_attr (die, DW_AT_name, cu);
16244
16245 if (name != NULL)
16246 break;
16247 }
16248
16249 /* Is it an anonymous namespace? */
16250
16251 *is_anonymous = (name == NULL);
16252 if (*is_anonymous)
16253 name = CP_ANONYMOUS_NAMESPACE_STR;
16254
16255 return name;
16256 }
16257
16258 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16259 the user defined type vector. */
16260
16261 static struct type *
16262 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16263 {
16264 struct gdbarch *gdbarch
16265 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
16266 struct comp_unit_head *cu_header = &cu->header;
16267 struct type *type;
16268 struct attribute *attr_byte_size;
16269 struct attribute *attr_address_class;
16270 int byte_size, addr_class;
16271 struct type *target_type;
16272
16273 target_type = die_type (die, cu);
16274
16275 /* The die_type call above may have already set the type for this DIE. */
16276 type = get_die_type (die, cu);
16277 if (type)
16278 return type;
16279
16280 type = lookup_pointer_type (target_type);
16281
16282 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16283 if (attr_byte_size)
16284 byte_size = DW_UNSND (attr_byte_size);
16285 else
16286 byte_size = cu_header->addr_size;
16287
16288 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16289 if (attr_address_class)
16290 addr_class = DW_UNSND (attr_address_class);
16291 else
16292 addr_class = DW_ADDR_none;
16293
16294 ULONGEST alignment = get_alignment (cu, die);
16295
16296 /* If the pointer size, alignment, or address class is different
16297 than the default, create a type variant marked as such and set
16298 the length accordingly. */
16299 if (TYPE_LENGTH (type) != byte_size
16300 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16301 && alignment != TYPE_RAW_ALIGN (type))
16302 || addr_class != DW_ADDR_none)
16303 {
16304 if (gdbarch_address_class_type_flags_p (gdbarch))
16305 {
16306 int type_flags;
16307
16308 type_flags = gdbarch_address_class_type_flags
16309 (gdbarch, byte_size, addr_class);
16310 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16311 == 0);
16312 type = make_type_with_address_space (type, type_flags);
16313 }
16314 else if (TYPE_LENGTH (type) != byte_size)
16315 {
16316 complaint (_("invalid pointer size %d"), byte_size);
16317 }
16318 else if (TYPE_RAW_ALIGN (type) != alignment)
16319 {
16320 complaint (_("Invalid DW_AT_alignment"
16321 " - DIE at %s [in module %s]"),
16322 sect_offset_str (die->sect_off),
16323 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16324 }
16325 else
16326 {
16327 /* Should we also complain about unhandled address classes? */
16328 }
16329 }
16330
16331 TYPE_LENGTH (type) = byte_size;
16332 set_type_align (type, alignment);
16333 return set_die_type (die, type, cu);
16334 }
16335
16336 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16337 the user defined type vector. */
16338
16339 static struct type *
16340 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16341 {
16342 struct type *type;
16343 struct type *to_type;
16344 struct type *domain;
16345
16346 to_type = die_type (die, cu);
16347 domain = die_containing_type (die, cu);
16348
16349 /* The calls above may have already set the type for this DIE. */
16350 type = get_die_type (die, cu);
16351 if (type)
16352 return type;
16353
16354 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16355 type = lookup_methodptr_type (to_type);
16356 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16357 {
16358 struct type *new_type
16359 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
16360
16361 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16362 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16363 TYPE_VARARGS (to_type));
16364 type = lookup_methodptr_type (new_type);
16365 }
16366 else
16367 type = lookup_memberptr_type (to_type, domain);
16368
16369 return set_die_type (die, type, cu);
16370 }
16371
16372 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16373 the user defined type vector. */
16374
16375 static struct type *
16376 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16377 enum type_code refcode)
16378 {
16379 struct comp_unit_head *cu_header = &cu->header;
16380 struct type *type, *target_type;
16381 struct attribute *attr;
16382
16383 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16384
16385 target_type = die_type (die, cu);
16386
16387 /* The die_type call above may have already set the type for this DIE. */
16388 type = get_die_type (die, cu);
16389 if (type)
16390 return type;
16391
16392 type = lookup_reference_type (target_type, refcode);
16393 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16394 if (attr != nullptr)
16395 {
16396 TYPE_LENGTH (type) = DW_UNSND (attr);
16397 }
16398 else
16399 {
16400 TYPE_LENGTH (type) = cu_header->addr_size;
16401 }
16402 maybe_set_alignment (cu, die, type);
16403 return set_die_type (die, type, cu);
16404 }
16405
16406 /* Add the given cv-qualifiers to the element type of the array. GCC
16407 outputs DWARF type qualifiers that apply to an array, not the
16408 element type. But GDB relies on the array element type to carry
16409 the cv-qualifiers. This mimics section 6.7.3 of the C99
16410 specification. */
16411
16412 static struct type *
16413 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16414 struct type *base_type, int cnst, int voltl)
16415 {
16416 struct type *el_type, *inner_array;
16417
16418 base_type = copy_type (base_type);
16419 inner_array = base_type;
16420
16421 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16422 {
16423 TYPE_TARGET_TYPE (inner_array) =
16424 copy_type (TYPE_TARGET_TYPE (inner_array));
16425 inner_array = TYPE_TARGET_TYPE (inner_array);
16426 }
16427
16428 el_type = TYPE_TARGET_TYPE (inner_array);
16429 cnst |= TYPE_CONST (el_type);
16430 voltl |= TYPE_VOLATILE (el_type);
16431 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16432
16433 return set_die_type (die, base_type, cu);
16434 }
16435
16436 static struct type *
16437 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16438 {
16439 struct type *base_type, *cv_type;
16440
16441 base_type = die_type (die, cu);
16442
16443 /* The die_type call above may have already set the type for this DIE. */
16444 cv_type = get_die_type (die, cu);
16445 if (cv_type)
16446 return cv_type;
16447
16448 /* In case the const qualifier is applied to an array type, the element type
16449 is so qualified, not the array type (section 6.7.3 of C99). */
16450 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16451 return add_array_cv_type (die, cu, base_type, 1, 0);
16452
16453 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16454 return set_die_type (die, cv_type, cu);
16455 }
16456
16457 static struct type *
16458 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16459 {
16460 struct type *base_type, *cv_type;
16461
16462 base_type = die_type (die, cu);
16463
16464 /* The die_type call above may have already set the type for this DIE. */
16465 cv_type = get_die_type (die, cu);
16466 if (cv_type)
16467 return cv_type;
16468
16469 /* In case the volatile qualifier is applied to an array type, the
16470 element type is so qualified, not the array type (section 6.7.3
16471 of C99). */
16472 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16473 return add_array_cv_type (die, cu, base_type, 0, 1);
16474
16475 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16476 return set_die_type (die, cv_type, cu);
16477 }
16478
16479 /* Handle DW_TAG_restrict_type. */
16480
16481 static struct type *
16482 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16483 {
16484 struct type *base_type, *cv_type;
16485
16486 base_type = die_type (die, cu);
16487
16488 /* The die_type call above may have already set the type for this DIE. */
16489 cv_type = get_die_type (die, cu);
16490 if (cv_type)
16491 return cv_type;
16492
16493 cv_type = make_restrict_type (base_type);
16494 return set_die_type (die, cv_type, cu);
16495 }
16496
16497 /* Handle DW_TAG_atomic_type. */
16498
16499 static struct type *
16500 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16501 {
16502 struct type *base_type, *cv_type;
16503
16504 base_type = die_type (die, cu);
16505
16506 /* The die_type call above may have already set the type for this DIE. */
16507 cv_type = get_die_type (die, cu);
16508 if (cv_type)
16509 return cv_type;
16510
16511 cv_type = make_atomic_type (base_type);
16512 return set_die_type (die, cv_type, cu);
16513 }
16514
16515 /* Extract all information from a DW_TAG_string_type DIE and add to
16516 the user defined type vector. It isn't really a user defined type,
16517 but it behaves like one, with other DIE's using an AT_user_def_type
16518 attribute to reference it. */
16519
16520 static struct type *
16521 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
16522 {
16523 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16524 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16525 struct type *type, *range_type, *index_type, *char_type;
16526 struct attribute *attr;
16527 struct dynamic_prop prop;
16528 bool length_is_constant = true;
16529 LONGEST length;
16530
16531 /* There are a couple of places where bit sizes might be made use of
16532 when parsing a DW_TAG_string_type, however, no producer that we know
16533 of make use of these. Handling bit sizes that are a multiple of the
16534 byte size is easy enough, but what about other bit sizes? Lets deal
16535 with that problem when we have to. Warn about these attributes being
16536 unsupported, then parse the type and ignore them like we always
16537 have. */
16538 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16539 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16540 {
16541 static bool warning_printed = false;
16542 if (!warning_printed)
16543 {
16544 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16545 "currently supported on DW_TAG_string_type."));
16546 warning_printed = true;
16547 }
16548 }
16549
16550 attr = dwarf2_attr (die, DW_AT_string_length, cu);
16551 if (attr != nullptr && !attr->form_is_constant ())
16552 {
16553 /* The string length describes the location at which the length of
16554 the string can be found. The size of the length field can be
16555 specified with one of the attributes below. */
16556 struct type *prop_type;
16557 struct attribute *len
16558 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16559 if (len == nullptr)
16560 len = dwarf2_attr (die, DW_AT_byte_size, cu);
16561 if (len != nullptr && len->form_is_constant ())
16562 {
16563 /* Pass 0 as the default as we know this attribute is constant
16564 and the default value will not be returned. */
16565 LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
16566 prop_type = cu->per_cu->int_type (sz, true);
16567 }
16568 else
16569 {
16570 /* If the size is not specified then we assume it is the size of
16571 an address on this target. */
16572 prop_type = cu->per_cu->addr_sized_int_type (true);
16573 }
16574
16575 /* Convert the attribute into a dynamic property. */
16576 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16577 length = 1;
16578 else
16579 length_is_constant = false;
16580 }
16581 else if (attr != nullptr)
16582 {
16583 /* This DW_AT_string_length just contains the length with no
16584 indirection. There's no need to create a dynamic property in this
16585 case. Pass 0 for the default value as we know it will not be
16586 returned in this case. */
16587 length = dwarf2_get_attr_constant_value (attr, 0);
16588 }
16589 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
16590 {
16591 /* We don't currently support non-constant byte sizes for strings. */
16592 length = dwarf2_get_attr_constant_value (attr, 1);
16593 }
16594 else
16595 {
16596 /* Use 1 as a fallback length if we have nothing else. */
16597 length = 1;
16598 }
16599
16600 index_type = objfile_type (objfile)->builtin_int;
16601 if (length_is_constant)
16602 range_type = create_static_range_type (NULL, index_type, 1, length);
16603 else
16604 {
16605 struct dynamic_prop low_bound;
16606
16607 low_bound.kind = PROP_CONST;
16608 low_bound.data.const_val = 1;
16609 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
16610 }
16611 char_type = language_string_char_type (cu->language_defn, gdbarch);
16612 type = create_string_type (NULL, char_type, range_type);
16613
16614 return set_die_type (die, type, cu);
16615 }
16616
16617 /* Assuming that DIE corresponds to a function, returns nonzero
16618 if the function is prototyped. */
16619
16620 static int
16621 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
16622 {
16623 struct attribute *attr;
16624
16625 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
16626 if (attr && (DW_UNSND (attr) != 0))
16627 return 1;
16628
16629 /* The DWARF standard implies that the DW_AT_prototyped attribute
16630 is only meaningful for C, but the concept also extends to other
16631 languages that allow unprototyped functions (Eg: Objective C).
16632 For all other languages, assume that functions are always
16633 prototyped. */
16634 if (cu->language != language_c
16635 && cu->language != language_objc
16636 && cu->language != language_opencl)
16637 return 1;
16638
16639 /* RealView does not emit DW_AT_prototyped. We can not distinguish
16640 prototyped and unprototyped functions; default to prototyped,
16641 since that is more common in modern code (and RealView warns
16642 about unprototyped functions). */
16643 if (producer_is_realview (cu->producer))
16644 return 1;
16645
16646 return 0;
16647 }
16648
16649 /* Handle DIES due to C code like:
16650
16651 struct foo
16652 {
16653 int (*funcp)(int a, long l);
16654 int b;
16655 };
16656
16657 ('funcp' generates a DW_TAG_subroutine_type DIE). */
16658
16659 static struct type *
16660 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
16661 {
16662 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16663 struct type *type; /* Type that this function returns. */
16664 struct type *ftype; /* Function that returns above type. */
16665 struct attribute *attr;
16666
16667 type = die_type (die, cu);
16668
16669 /* The die_type call above may have already set the type for this DIE. */
16670 ftype = get_die_type (die, cu);
16671 if (ftype)
16672 return ftype;
16673
16674 ftype = lookup_function_type (type);
16675
16676 if (prototyped_function_p (die, cu))
16677 TYPE_PROTOTYPED (ftype) = 1;
16678
16679 /* Store the calling convention in the type if it's available in
16680 the subroutine die. Otherwise set the calling convention to
16681 the default value DW_CC_normal. */
16682 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16683 if (attr != nullptr
16684 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
16685 TYPE_CALLING_CONVENTION (ftype)
16686 = (enum dwarf_calling_convention) (DW_UNSND (attr));
16687 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
16688 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
16689 else
16690 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
16691
16692 /* Record whether the function returns normally to its caller or not
16693 if the DWARF producer set that information. */
16694 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
16695 if (attr && (DW_UNSND (attr) != 0))
16696 TYPE_NO_RETURN (ftype) = 1;
16697
16698 /* We need to add the subroutine type to the die immediately so
16699 we don't infinitely recurse when dealing with parameters
16700 declared as the same subroutine type. */
16701 set_die_type (die, ftype, cu);
16702
16703 if (die->child != NULL)
16704 {
16705 struct type *void_type = objfile_type (objfile)->builtin_void;
16706 struct die_info *child_die;
16707 int nparams, iparams;
16708
16709 /* Count the number of parameters.
16710 FIXME: GDB currently ignores vararg functions, but knows about
16711 vararg member functions. */
16712 nparams = 0;
16713 child_die = die->child;
16714 while (child_die && child_die->tag)
16715 {
16716 if (child_die->tag == DW_TAG_formal_parameter)
16717 nparams++;
16718 else if (child_die->tag == DW_TAG_unspecified_parameters)
16719 TYPE_VARARGS (ftype) = 1;
16720 child_die = sibling_die (child_die);
16721 }
16722
16723 /* Allocate storage for parameters and fill them in. */
16724 TYPE_NFIELDS (ftype) = nparams;
16725 TYPE_FIELDS (ftype) = (struct field *)
16726 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
16727
16728 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
16729 even if we error out during the parameters reading below. */
16730 for (iparams = 0; iparams < nparams; iparams++)
16731 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
16732
16733 iparams = 0;
16734 child_die = die->child;
16735 while (child_die && child_die->tag)
16736 {
16737 if (child_die->tag == DW_TAG_formal_parameter)
16738 {
16739 struct type *arg_type;
16740
16741 /* DWARF version 2 has no clean way to discern C++
16742 static and non-static member functions. G++ helps
16743 GDB by marking the first parameter for non-static
16744 member functions (which is the this pointer) as
16745 artificial. We pass this information to
16746 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
16747
16748 DWARF version 3 added DW_AT_object_pointer, which GCC
16749 4.5 does not yet generate. */
16750 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
16751 if (attr != nullptr)
16752 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
16753 else
16754 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
16755 arg_type = die_type (child_die, cu);
16756
16757 /* RealView does not mark THIS as const, which the testsuite
16758 expects. GCC marks THIS as const in method definitions,
16759 but not in the class specifications (GCC PR 43053). */
16760 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
16761 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
16762 {
16763 int is_this = 0;
16764 struct dwarf2_cu *arg_cu = cu;
16765 const char *name = dwarf2_name (child_die, cu);
16766
16767 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
16768 if (attr != nullptr)
16769 {
16770 /* If the compiler emits this, use it. */
16771 if (follow_die_ref (die, attr, &arg_cu) == child_die)
16772 is_this = 1;
16773 }
16774 else if (name && strcmp (name, "this") == 0)
16775 /* Function definitions will have the argument names. */
16776 is_this = 1;
16777 else if (name == NULL && iparams == 0)
16778 /* Declarations may not have the names, so like
16779 elsewhere in GDB, assume an artificial first
16780 argument is "this". */
16781 is_this = 1;
16782
16783 if (is_this)
16784 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
16785 arg_type, 0);
16786 }
16787
16788 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
16789 iparams++;
16790 }
16791 child_die = sibling_die (child_die);
16792 }
16793 }
16794
16795 return ftype;
16796 }
16797
16798 static struct type *
16799 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
16800 {
16801 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16802 const char *name = NULL;
16803 struct type *this_type, *target_type;
16804
16805 name = dwarf2_full_name (NULL, die, cu);
16806 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
16807 TYPE_TARGET_STUB (this_type) = 1;
16808 set_die_type (die, this_type, cu);
16809 target_type = die_type (die, cu);
16810 if (target_type != this_type)
16811 TYPE_TARGET_TYPE (this_type) = target_type;
16812 else
16813 {
16814 /* Self-referential typedefs are, it seems, not allowed by the DWARF
16815 spec and cause infinite loops in GDB. */
16816 complaint (_("Self-referential DW_TAG_typedef "
16817 "- DIE at %s [in module %s]"),
16818 sect_offset_str (die->sect_off), objfile_name (objfile));
16819 TYPE_TARGET_TYPE (this_type) = NULL;
16820 }
16821 if (name == NULL)
16822 {
16823 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
16824 anonymous typedefs, which is, strictly speaking, invalid DWARF.
16825 Handle these by just returning the target type, rather than
16826 constructing an anonymous typedef type and trying to handle this
16827 elsewhere. */
16828 set_die_type (die, target_type, cu);
16829 return target_type;
16830 }
16831 return this_type;
16832 }
16833
16834 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
16835 (which may be different from NAME) to the architecture back-end to allow
16836 it to guess the correct format if necessary. */
16837
16838 static struct type *
16839 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
16840 const char *name_hint, enum bfd_endian byte_order)
16841 {
16842 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16843 const struct floatformat **format;
16844 struct type *type;
16845
16846 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
16847 if (format)
16848 type = init_float_type (objfile, bits, name, format, byte_order);
16849 else
16850 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
16851
16852 return type;
16853 }
16854
16855 /* Allocate an integer type of size BITS and name NAME. */
16856
16857 static struct type *
16858 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
16859 int bits, int unsigned_p, const char *name)
16860 {
16861 struct type *type;
16862
16863 /* Versions of Intel's C Compiler generate an integer type called "void"
16864 instead of using DW_TAG_unspecified_type. This has been seen on
16865 at least versions 14, 17, and 18. */
16866 if (bits == 0 && producer_is_icc (cu) && name != nullptr
16867 && strcmp (name, "void") == 0)
16868 type = objfile_type (objfile)->builtin_void;
16869 else
16870 type = init_integer_type (objfile, bits, unsigned_p, name);
16871
16872 return type;
16873 }
16874
16875 /* Initialise and return a floating point type of size BITS suitable for
16876 use as a component of a complex number. The NAME_HINT is passed through
16877 when initialising the floating point type and is the name of the complex
16878 type.
16879
16880 As DWARF doesn't currently provide an explicit name for the components
16881 of a complex number, but it can be helpful to have these components
16882 named, we try to select a suitable name based on the size of the
16883 component. */
16884 static struct type *
16885 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
16886 struct objfile *objfile,
16887 int bits, const char *name_hint,
16888 enum bfd_endian byte_order)
16889 {
16890 gdbarch *gdbarch = get_objfile_arch (objfile);
16891 struct type *tt = nullptr;
16892
16893 /* Try to find a suitable floating point builtin type of size BITS.
16894 We're going to use the name of this type as the name for the complex
16895 target type that we are about to create. */
16896 switch (cu->language)
16897 {
16898 case language_fortran:
16899 switch (bits)
16900 {
16901 case 32:
16902 tt = builtin_f_type (gdbarch)->builtin_real;
16903 break;
16904 case 64:
16905 tt = builtin_f_type (gdbarch)->builtin_real_s8;
16906 break;
16907 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16908 case 128:
16909 tt = builtin_f_type (gdbarch)->builtin_real_s16;
16910 break;
16911 }
16912 break;
16913 default:
16914 switch (bits)
16915 {
16916 case 32:
16917 tt = builtin_type (gdbarch)->builtin_float;
16918 break;
16919 case 64:
16920 tt = builtin_type (gdbarch)->builtin_double;
16921 break;
16922 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16923 case 128:
16924 tt = builtin_type (gdbarch)->builtin_long_double;
16925 break;
16926 }
16927 break;
16928 }
16929
16930 /* If the type we found doesn't match the size we were looking for, then
16931 pretend we didn't find a type at all, the complex target type we
16932 create will then be nameless. */
16933 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
16934 tt = nullptr;
16935
16936 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
16937 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
16938 }
16939
16940 /* Find a representation of a given base type and install
16941 it in the TYPE field of the die. */
16942
16943 static struct type *
16944 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
16945 {
16946 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16947 struct type *type;
16948 struct attribute *attr;
16949 int encoding = 0, bits = 0;
16950 const char *name;
16951 gdbarch *arch;
16952
16953 attr = dwarf2_attr (die, DW_AT_encoding, cu);
16954 if (attr != nullptr)
16955 encoding = DW_UNSND (attr);
16956 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16957 if (attr != nullptr)
16958 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
16959 name = dwarf2_name (die, cu);
16960 if (!name)
16961 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
16962
16963 arch = get_objfile_arch (objfile);
16964 enum bfd_endian byte_order = gdbarch_byte_order (arch);
16965
16966 attr = dwarf2_attr (die, DW_AT_endianity, cu);
16967 if (attr)
16968 {
16969 int endianity = DW_UNSND (attr);
16970
16971 switch (endianity)
16972 {
16973 case DW_END_big:
16974 byte_order = BFD_ENDIAN_BIG;
16975 break;
16976 case DW_END_little:
16977 byte_order = BFD_ENDIAN_LITTLE;
16978 break;
16979 default:
16980 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
16981 break;
16982 }
16983 }
16984
16985 switch (encoding)
16986 {
16987 case DW_ATE_address:
16988 /* Turn DW_ATE_address into a void * pointer. */
16989 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
16990 type = init_pointer_type (objfile, bits, name, type);
16991 break;
16992 case DW_ATE_boolean:
16993 type = init_boolean_type (objfile, bits, 1, name);
16994 break;
16995 case DW_ATE_complex_float:
16996 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
16997 byte_order);
16998 type = init_complex_type (objfile, name, type);
16999 break;
17000 case DW_ATE_decimal_float:
17001 type = init_decfloat_type (objfile, bits, name);
17002 break;
17003 case DW_ATE_float:
17004 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17005 break;
17006 case DW_ATE_signed:
17007 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17008 break;
17009 case DW_ATE_unsigned:
17010 if (cu->language == language_fortran
17011 && name
17012 && startswith (name, "character("))
17013 type = init_character_type (objfile, bits, 1, name);
17014 else
17015 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17016 break;
17017 case DW_ATE_signed_char:
17018 if (cu->language == language_ada || cu->language == language_m2
17019 || cu->language == language_pascal
17020 || cu->language == language_fortran)
17021 type = init_character_type (objfile, bits, 0, name);
17022 else
17023 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17024 break;
17025 case DW_ATE_unsigned_char:
17026 if (cu->language == language_ada || cu->language == language_m2
17027 || cu->language == language_pascal
17028 || cu->language == language_fortran
17029 || cu->language == language_rust)
17030 type = init_character_type (objfile, bits, 1, name);
17031 else
17032 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17033 break;
17034 case DW_ATE_UTF:
17035 {
17036 if (bits == 16)
17037 type = builtin_type (arch)->builtin_char16;
17038 else if (bits == 32)
17039 type = builtin_type (arch)->builtin_char32;
17040 else
17041 {
17042 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17043 bits);
17044 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17045 }
17046 return set_die_type (die, type, cu);
17047 }
17048 break;
17049
17050 default:
17051 complaint (_("unsupported DW_AT_encoding: '%s'"),
17052 dwarf_type_encoding_name (encoding));
17053 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17054 break;
17055 }
17056
17057 if (name && strcmp (name, "char") == 0)
17058 TYPE_NOSIGN (type) = 1;
17059
17060 maybe_set_alignment (cu, die, type);
17061
17062 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17063
17064 return set_die_type (die, type, cu);
17065 }
17066
17067 /* Parse dwarf attribute if it's a block, reference or constant and put the
17068 resulting value of the attribute into struct bound_prop.
17069 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17070
17071 static int
17072 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17073 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17074 struct type *default_type)
17075 {
17076 struct dwarf2_property_baton *baton;
17077 struct obstack *obstack
17078 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17079
17080 gdb_assert (default_type != NULL);
17081
17082 if (attr == NULL || prop == NULL)
17083 return 0;
17084
17085 if (attr->form_is_block ())
17086 {
17087 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17088 baton->property_type = default_type;
17089 baton->locexpr.per_cu = cu->per_cu;
17090 baton->locexpr.size = DW_BLOCK (attr)->size;
17091 baton->locexpr.data = DW_BLOCK (attr)->data;
17092 switch (attr->name)
17093 {
17094 case DW_AT_string_length:
17095 baton->locexpr.is_reference = true;
17096 break;
17097 default:
17098 baton->locexpr.is_reference = false;
17099 break;
17100 }
17101 prop->data.baton = baton;
17102 prop->kind = PROP_LOCEXPR;
17103 gdb_assert (prop->data.baton != NULL);
17104 }
17105 else if (attr->form_is_ref ())
17106 {
17107 struct dwarf2_cu *target_cu = cu;
17108 struct die_info *target_die;
17109 struct attribute *target_attr;
17110
17111 target_die = follow_die_ref (die, attr, &target_cu);
17112 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17113 if (target_attr == NULL)
17114 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17115 target_cu);
17116 if (target_attr == NULL)
17117 return 0;
17118
17119 switch (target_attr->name)
17120 {
17121 case DW_AT_location:
17122 if (target_attr->form_is_section_offset ())
17123 {
17124 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17125 baton->property_type = die_type (target_die, target_cu);
17126 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17127 prop->data.baton = baton;
17128 prop->kind = PROP_LOCLIST;
17129 gdb_assert (prop->data.baton != NULL);
17130 }
17131 else if (target_attr->form_is_block ())
17132 {
17133 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17134 baton->property_type = die_type (target_die, target_cu);
17135 baton->locexpr.per_cu = cu->per_cu;
17136 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17137 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17138 baton->locexpr.is_reference = true;
17139 prop->data.baton = baton;
17140 prop->kind = PROP_LOCEXPR;
17141 gdb_assert (prop->data.baton != NULL);
17142 }
17143 else
17144 {
17145 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17146 "dynamic property");
17147 return 0;
17148 }
17149 break;
17150 case DW_AT_data_member_location:
17151 {
17152 LONGEST offset;
17153
17154 if (!handle_data_member_location (target_die, target_cu,
17155 &offset))
17156 return 0;
17157
17158 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17159 baton->property_type = read_type_die (target_die->parent,
17160 target_cu);
17161 baton->offset_info.offset = offset;
17162 baton->offset_info.type = die_type (target_die, target_cu);
17163 prop->data.baton = baton;
17164 prop->kind = PROP_ADDR_OFFSET;
17165 break;
17166 }
17167 }
17168 }
17169 else if (attr->form_is_constant ())
17170 {
17171 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17172 prop->kind = PROP_CONST;
17173 }
17174 else
17175 {
17176 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17177 dwarf2_name (die, cu));
17178 return 0;
17179 }
17180
17181 return 1;
17182 }
17183
17184 /* See read.h. */
17185
17186 struct type *
17187 dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
17188 {
17189 struct objfile *objfile = dwarf2_per_objfile->objfile;
17190 struct type *int_type;
17191
17192 /* Helper macro to examine the various builtin types. */
17193 #define TRY_TYPE(F) \
17194 int_type = (unsigned_p \
17195 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17196 : objfile_type (objfile)->builtin_ ## F); \
17197 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17198 return int_type
17199
17200 TRY_TYPE (char);
17201 TRY_TYPE (short);
17202 TRY_TYPE (int);
17203 TRY_TYPE (long);
17204 TRY_TYPE (long_long);
17205
17206 #undef TRY_TYPE
17207
17208 gdb_assert_not_reached ("unable to find suitable integer type");
17209 }
17210
17211 /* See read.h. */
17212
17213 struct type *
17214 dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
17215 {
17216 int addr_size = this->addr_size ();
17217 return int_type (addr_size, unsigned_p);
17218 }
17219
17220 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17221 present (which is valid) then compute the default type based on the
17222 compilation units address size. */
17223
17224 static struct type *
17225 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17226 {
17227 struct type *index_type = die_type (die, cu);
17228
17229 /* Dwarf-2 specifications explicitly allows to create subrange types
17230 without specifying a base type.
17231 In that case, the base type must be set to the type of
17232 the lower bound, upper bound or count, in that order, if any of these
17233 three attributes references an object that has a type.
17234 If no base type is found, the Dwarf-2 specifications say that
17235 a signed integer type of size equal to the size of an address should
17236 be used.
17237 For the following C code: `extern char gdb_int [];'
17238 GCC produces an empty range DIE.
17239 FIXME: muller/2010-05-28: Possible references to object for low bound,
17240 high bound or count are not yet handled by this code. */
17241 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
17242 index_type = cu->per_cu->addr_sized_int_type (false);
17243
17244 return index_type;
17245 }
17246
17247 /* Read the given DW_AT_subrange DIE. */
17248
17249 static struct type *
17250 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17251 {
17252 struct type *base_type, *orig_base_type;
17253 struct type *range_type;
17254 struct attribute *attr;
17255 struct dynamic_prop low, high;
17256 int low_default_is_valid;
17257 int high_bound_is_count = 0;
17258 const char *name;
17259 ULONGEST negative_mask;
17260
17261 orig_base_type = read_subrange_index_type (die, cu);
17262
17263 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17264 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17265 creating the range type, but we use the result of check_typedef
17266 when examining properties of the type. */
17267 base_type = check_typedef (orig_base_type);
17268
17269 /* The die_type call above may have already set the type for this DIE. */
17270 range_type = get_die_type (die, cu);
17271 if (range_type)
17272 return range_type;
17273
17274 low.kind = PROP_CONST;
17275 high.kind = PROP_CONST;
17276 high.data.const_val = 0;
17277
17278 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17279 omitting DW_AT_lower_bound. */
17280 switch (cu->language)
17281 {
17282 case language_c:
17283 case language_cplus:
17284 low.data.const_val = 0;
17285 low_default_is_valid = 1;
17286 break;
17287 case language_fortran:
17288 low.data.const_val = 1;
17289 low_default_is_valid = 1;
17290 break;
17291 case language_d:
17292 case language_objc:
17293 case language_rust:
17294 low.data.const_val = 0;
17295 low_default_is_valid = (cu->header.version >= 4);
17296 break;
17297 case language_ada:
17298 case language_m2:
17299 case language_pascal:
17300 low.data.const_val = 1;
17301 low_default_is_valid = (cu->header.version >= 4);
17302 break;
17303 default:
17304 low.data.const_val = 0;
17305 low_default_is_valid = 0;
17306 break;
17307 }
17308
17309 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17310 if (attr != nullptr)
17311 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17312 else if (!low_default_is_valid)
17313 complaint (_("Missing DW_AT_lower_bound "
17314 "- DIE at %s [in module %s]"),
17315 sect_offset_str (die->sect_off),
17316 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17317
17318 struct attribute *attr_ub, *attr_count;
17319 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17320 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17321 {
17322 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17323 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17324 {
17325 /* If bounds are constant do the final calculation here. */
17326 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17327 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17328 else
17329 high_bound_is_count = 1;
17330 }
17331 else
17332 {
17333 if (attr_ub != NULL)
17334 complaint (_("Unresolved DW_AT_upper_bound "
17335 "- DIE at %s [in module %s]"),
17336 sect_offset_str (die->sect_off),
17337 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17338 if (attr_count != NULL)
17339 complaint (_("Unresolved DW_AT_count "
17340 "- DIE at %s [in module %s]"),
17341 sect_offset_str (die->sect_off),
17342 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17343 }
17344 }
17345
17346 LONGEST bias = 0;
17347 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17348 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17349 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
17350
17351 /* Normally, the DWARF producers are expected to use a signed
17352 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17353 But this is unfortunately not always the case, as witnessed
17354 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17355 is used instead. To work around that ambiguity, we treat
17356 the bounds as signed, and thus sign-extend their values, when
17357 the base type is signed. */
17358 negative_mask =
17359 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17360 if (low.kind == PROP_CONST
17361 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17362 low.data.const_val |= negative_mask;
17363 if (high.kind == PROP_CONST
17364 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17365 high.data.const_val |= negative_mask;
17366
17367 /* Check for bit and byte strides. */
17368 struct dynamic_prop byte_stride_prop;
17369 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17370 if (attr_byte_stride != nullptr)
17371 {
17372 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17373 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17374 prop_type);
17375 }
17376
17377 struct dynamic_prop bit_stride_prop;
17378 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17379 if (attr_bit_stride != nullptr)
17380 {
17381 /* It only makes sense to have either a bit or byte stride. */
17382 if (attr_byte_stride != nullptr)
17383 {
17384 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17385 "- DIE at %s [in module %s]"),
17386 sect_offset_str (die->sect_off),
17387 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17388 attr_bit_stride = nullptr;
17389 }
17390 else
17391 {
17392 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17393 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17394 prop_type);
17395 }
17396 }
17397
17398 if (attr_byte_stride != nullptr
17399 || attr_bit_stride != nullptr)
17400 {
17401 bool byte_stride_p = (attr_byte_stride != nullptr);
17402 struct dynamic_prop *stride
17403 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17404
17405 range_type
17406 = create_range_type_with_stride (NULL, orig_base_type, &low,
17407 &high, bias, stride, byte_stride_p);
17408 }
17409 else
17410 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17411
17412 if (high_bound_is_count)
17413 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17414
17415 /* Ada expects an empty array on no boundary attributes. */
17416 if (attr == NULL && cu->language != language_ada)
17417 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17418
17419 name = dwarf2_name (die, cu);
17420 if (name)
17421 TYPE_NAME (range_type) = name;
17422
17423 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17424 if (attr != nullptr)
17425 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17426
17427 maybe_set_alignment (cu, die, range_type);
17428
17429 set_die_type (die, range_type, cu);
17430
17431 /* set_die_type should be already done. */
17432 set_descriptive_type (range_type, die, cu);
17433
17434 return range_type;
17435 }
17436
17437 static struct type *
17438 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17439 {
17440 struct type *type;
17441
17442 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17443 NULL);
17444 TYPE_NAME (type) = dwarf2_name (die, cu);
17445
17446 /* In Ada, an unspecified type is typically used when the description
17447 of the type is deferred to a different unit. When encountering
17448 such a type, we treat it as a stub, and try to resolve it later on,
17449 when needed. */
17450 if (cu->language == language_ada)
17451 TYPE_STUB (type) = 1;
17452
17453 return set_die_type (die, type, cu);
17454 }
17455
17456 /* Read a single die and all its descendents. Set the die's sibling
17457 field to NULL; set other fields in the die correctly, and set all
17458 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17459 location of the info_ptr after reading all of those dies. PARENT
17460 is the parent of the die in question. */
17461
17462 static struct die_info *
17463 read_die_and_children (const struct die_reader_specs *reader,
17464 const gdb_byte *info_ptr,
17465 const gdb_byte **new_info_ptr,
17466 struct die_info *parent)
17467 {
17468 struct die_info *die;
17469 const gdb_byte *cur_ptr;
17470
17471 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
17472 if (die == NULL)
17473 {
17474 *new_info_ptr = cur_ptr;
17475 return NULL;
17476 }
17477 store_in_ref_table (die, reader->cu);
17478
17479 if (die->has_children)
17480 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17481 else
17482 {
17483 die->child = NULL;
17484 *new_info_ptr = cur_ptr;
17485 }
17486
17487 die->sibling = NULL;
17488 die->parent = parent;
17489 return die;
17490 }
17491
17492 /* Read a die, all of its descendents, and all of its siblings; set
17493 all of the fields of all of the dies correctly. Arguments are as
17494 in read_die_and_children. */
17495
17496 static struct die_info *
17497 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17498 const gdb_byte *info_ptr,
17499 const gdb_byte **new_info_ptr,
17500 struct die_info *parent)
17501 {
17502 struct die_info *first_die, *last_sibling;
17503 const gdb_byte *cur_ptr;
17504
17505 cur_ptr = info_ptr;
17506 first_die = last_sibling = NULL;
17507
17508 while (1)
17509 {
17510 struct die_info *die
17511 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17512
17513 if (die == NULL)
17514 {
17515 *new_info_ptr = cur_ptr;
17516 return first_die;
17517 }
17518
17519 if (!first_die)
17520 first_die = die;
17521 else
17522 last_sibling->sibling = die;
17523
17524 last_sibling = die;
17525 }
17526 }
17527
17528 /* Read a die, all of its descendents, and all of its siblings; set
17529 all of the fields of all of the dies correctly. Arguments are as
17530 in read_die_and_children.
17531 This the main entry point for reading a DIE and all its children. */
17532
17533 static struct die_info *
17534 read_die_and_siblings (const struct die_reader_specs *reader,
17535 const gdb_byte *info_ptr,
17536 const gdb_byte **new_info_ptr,
17537 struct die_info *parent)
17538 {
17539 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17540 new_info_ptr, parent);
17541
17542 if (dwarf_die_debug)
17543 {
17544 fprintf_unfiltered (gdb_stdlog,
17545 "Read die from %s@0x%x of %s:\n",
17546 reader->die_section->get_name (),
17547 (unsigned) (info_ptr - reader->die_section->buffer),
17548 bfd_get_filename (reader->abfd));
17549 dump_die (die, dwarf_die_debug);
17550 }
17551
17552 return die;
17553 }
17554
17555 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17556 attributes.
17557 The caller is responsible for filling in the extra attributes
17558 and updating (*DIEP)->num_attrs.
17559 Set DIEP to point to a newly allocated die with its information,
17560 except for its child, sibling, and parent fields. */
17561
17562 static const gdb_byte *
17563 read_full_die_1 (const struct die_reader_specs *reader,
17564 struct die_info **diep, const gdb_byte *info_ptr,
17565 int num_extra_attrs)
17566 {
17567 unsigned int abbrev_number, bytes_read, i;
17568 struct abbrev_info *abbrev;
17569 struct die_info *die;
17570 struct dwarf2_cu *cu = reader->cu;
17571 bfd *abfd = reader->abfd;
17572
17573 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17574 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17575 info_ptr += bytes_read;
17576 if (!abbrev_number)
17577 {
17578 *diep = NULL;
17579 return info_ptr;
17580 }
17581
17582 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
17583 if (!abbrev)
17584 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17585 abbrev_number,
17586 bfd_get_filename (abfd));
17587
17588 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
17589 die->sect_off = sect_off;
17590 die->tag = abbrev->tag;
17591 die->abbrev = abbrev_number;
17592 die->has_children = abbrev->has_children;
17593
17594 /* Make the result usable.
17595 The caller needs to update num_attrs after adding the extra
17596 attributes. */
17597 die->num_attrs = abbrev->num_attrs;
17598
17599 std::vector<int> indexes_that_need_reprocess;
17600 for (i = 0; i < abbrev->num_attrs; ++i)
17601 {
17602 bool need_reprocess;
17603 info_ptr =
17604 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17605 info_ptr, &need_reprocess);
17606 if (need_reprocess)
17607 indexes_that_need_reprocess.push_back (i);
17608 }
17609
17610 struct attribute *attr = dwarf2_attr_no_follow (die, DW_AT_str_offsets_base);
17611 if (attr != nullptr)
17612 cu->str_offsets_base = DW_UNSND (attr);
17613
17614 auto maybe_addr_base = lookup_addr_base(die);
17615 if (maybe_addr_base.has_value ())
17616 cu->addr_base = *maybe_addr_base;
17617 for (int index : indexes_that_need_reprocess)
17618 read_attribute_reprocess (reader, &die->attrs[index]);
17619 *diep = die;
17620 return info_ptr;
17621 }
17622
17623 /* Read a die and all its attributes.
17624 Set DIEP to point to a newly allocated die with its information,
17625 except for its child, sibling, and parent fields. */
17626
17627 static const gdb_byte *
17628 read_full_die (const struct die_reader_specs *reader,
17629 struct die_info **diep, const gdb_byte *info_ptr)
17630 {
17631 const gdb_byte *result;
17632
17633 result = read_full_die_1 (reader, diep, info_ptr, 0);
17634
17635 if (dwarf_die_debug)
17636 {
17637 fprintf_unfiltered (gdb_stdlog,
17638 "Read die from %s@0x%x of %s:\n",
17639 reader->die_section->get_name (),
17640 (unsigned) (info_ptr - reader->die_section->buffer),
17641 bfd_get_filename (reader->abfd));
17642 dump_die (*diep, dwarf_die_debug);
17643 }
17644
17645 return result;
17646 }
17647 \f
17648
17649 /* Returns nonzero if TAG represents a type that we might generate a partial
17650 symbol for. */
17651
17652 static int
17653 is_type_tag_for_partial (int tag)
17654 {
17655 switch (tag)
17656 {
17657 #if 0
17658 /* Some types that would be reasonable to generate partial symbols for,
17659 that we don't at present. */
17660 case DW_TAG_array_type:
17661 case DW_TAG_file_type:
17662 case DW_TAG_ptr_to_member_type:
17663 case DW_TAG_set_type:
17664 case DW_TAG_string_type:
17665 case DW_TAG_subroutine_type:
17666 #endif
17667 case DW_TAG_base_type:
17668 case DW_TAG_class_type:
17669 case DW_TAG_interface_type:
17670 case DW_TAG_enumeration_type:
17671 case DW_TAG_structure_type:
17672 case DW_TAG_subrange_type:
17673 case DW_TAG_typedef:
17674 case DW_TAG_union_type:
17675 return 1;
17676 default:
17677 return 0;
17678 }
17679 }
17680
17681 /* Load all DIEs that are interesting for partial symbols into memory. */
17682
17683 static struct partial_die_info *
17684 load_partial_dies (const struct die_reader_specs *reader,
17685 const gdb_byte *info_ptr, int building_psymtab)
17686 {
17687 struct dwarf2_cu *cu = reader->cu;
17688 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17689 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
17690 unsigned int bytes_read;
17691 unsigned int load_all = 0;
17692 int nesting_level = 1;
17693
17694 parent_die = NULL;
17695 last_die = NULL;
17696
17697 gdb_assert (cu->per_cu != NULL);
17698 if (cu->per_cu->load_all_dies)
17699 load_all = 1;
17700
17701 cu->partial_dies
17702 = htab_create_alloc_ex (cu->header.length / 12,
17703 partial_die_hash,
17704 partial_die_eq,
17705 NULL,
17706 &cu->comp_unit_obstack,
17707 hashtab_obstack_allocate,
17708 dummy_obstack_deallocate);
17709
17710 while (1)
17711 {
17712 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
17713
17714 /* A NULL abbrev means the end of a series of children. */
17715 if (abbrev == NULL)
17716 {
17717 if (--nesting_level == 0)
17718 return first_die;
17719
17720 info_ptr += bytes_read;
17721 last_die = parent_die;
17722 parent_die = parent_die->die_parent;
17723 continue;
17724 }
17725
17726 /* Check for template arguments. We never save these; if
17727 they're seen, we just mark the parent, and go on our way. */
17728 if (parent_die != NULL
17729 && cu->language == language_cplus
17730 && (abbrev->tag == DW_TAG_template_type_param
17731 || abbrev->tag == DW_TAG_template_value_param))
17732 {
17733 parent_die->has_template_arguments = 1;
17734
17735 if (!load_all)
17736 {
17737 /* We don't need a partial DIE for the template argument. */
17738 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17739 continue;
17740 }
17741 }
17742
17743 /* We only recurse into c++ subprograms looking for template arguments.
17744 Skip their other children. */
17745 if (!load_all
17746 && cu->language == language_cplus
17747 && parent_die != NULL
17748 && parent_die->tag == DW_TAG_subprogram)
17749 {
17750 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17751 continue;
17752 }
17753
17754 /* Check whether this DIE is interesting enough to save. Normally
17755 we would not be interested in members here, but there may be
17756 later variables referencing them via DW_AT_specification (for
17757 static members). */
17758 if (!load_all
17759 && !is_type_tag_for_partial (abbrev->tag)
17760 && abbrev->tag != DW_TAG_constant
17761 && abbrev->tag != DW_TAG_enumerator
17762 && abbrev->tag != DW_TAG_subprogram
17763 && abbrev->tag != DW_TAG_inlined_subroutine
17764 && abbrev->tag != DW_TAG_lexical_block
17765 && abbrev->tag != DW_TAG_variable
17766 && abbrev->tag != DW_TAG_namespace
17767 && abbrev->tag != DW_TAG_module
17768 && abbrev->tag != DW_TAG_member
17769 && abbrev->tag != DW_TAG_imported_unit
17770 && abbrev->tag != DW_TAG_imported_declaration)
17771 {
17772 /* Otherwise we skip to the next sibling, if any. */
17773 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17774 continue;
17775 }
17776
17777 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
17778 abbrev);
17779
17780 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
17781
17782 /* This two-pass algorithm for processing partial symbols has a
17783 high cost in cache pressure. Thus, handle some simple cases
17784 here which cover the majority of C partial symbols. DIEs
17785 which neither have specification tags in them, nor could have
17786 specification tags elsewhere pointing at them, can simply be
17787 processed and discarded.
17788
17789 This segment is also optional; scan_partial_symbols and
17790 add_partial_symbol will handle these DIEs if we chain
17791 them in normally. When compilers which do not emit large
17792 quantities of duplicate debug information are more common,
17793 this code can probably be removed. */
17794
17795 /* Any complete simple types at the top level (pretty much all
17796 of them, for a language without namespaces), can be processed
17797 directly. */
17798 if (parent_die == NULL
17799 && pdi.has_specification == 0
17800 && pdi.is_declaration == 0
17801 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
17802 || pdi.tag == DW_TAG_base_type
17803 || pdi.tag == DW_TAG_subrange_type))
17804 {
17805 if (building_psymtab && pdi.name != NULL)
17806 add_psymbol_to_list (pdi.name, false,
17807 VAR_DOMAIN, LOC_TYPEDEF, -1,
17808 psymbol_placement::STATIC,
17809 0, cu->language, objfile);
17810 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
17811 continue;
17812 }
17813
17814 /* The exception for DW_TAG_typedef with has_children above is
17815 a workaround of GCC PR debug/47510. In the case of this complaint
17816 type_name_or_error will error on such types later.
17817
17818 GDB skipped children of DW_TAG_typedef by the shortcut above and then
17819 it could not find the child DIEs referenced later, this is checked
17820 above. In correct DWARF DW_TAG_typedef should have no children. */
17821
17822 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
17823 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
17824 "- DIE at %s [in module %s]"),
17825 sect_offset_str (pdi.sect_off), objfile_name (objfile));
17826
17827 /* If we're at the second level, and we're an enumerator, and
17828 our parent has no specification (meaning possibly lives in a
17829 namespace elsewhere), then we can add the partial symbol now
17830 instead of queueing it. */
17831 if (pdi.tag == DW_TAG_enumerator
17832 && parent_die != NULL
17833 && parent_die->die_parent == NULL
17834 && parent_die->tag == DW_TAG_enumeration_type
17835 && parent_die->has_specification == 0)
17836 {
17837 if (pdi.name == NULL)
17838 complaint (_("malformed enumerator DIE ignored"));
17839 else if (building_psymtab)
17840 add_psymbol_to_list (pdi.name, false,
17841 VAR_DOMAIN, LOC_CONST, -1,
17842 cu->language == language_cplus
17843 ? psymbol_placement::GLOBAL
17844 : psymbol_placement::STATIC,
17845 0, cu->language, objfile);
17846
17847 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
17848 continue;
17849 }
17850
17851 struct partial_die_info *part_die
17852 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
17853
17854 /* We'll save this DIE so link it in. */
17855 part_die->die_parent = parent_die;
17856 part_die->die_sibling = NULL;
17857 part_die->die_child = NULL;
17858
17859 if (last_die && last_die == parent_die)
17860 last_die->die_child = part_die;
17861 else if (last_die)
17862 last_die->die_sibling = part_die;
17863
17864 last_die = part_die;
17865
17866 if (first_die == NULL)
17867 first_die = part_die;
17868
17869 /* Maybe add the DIE to the hash table. Not all DIEs that we
17870 find interesting need to be in the hash table, because we
17871 also have the parent/sibling/child chains; only those that we
17872 might refer to by offset later during partial symbol reading.
17873
17874 For now this means things that might have be the target of a
17875 DW_AT_specification, DW_AT_abstract_origin, or
17876 DW_AT_extension. DW_AT_extension will refer only to
17877 namespaces; DW_AT_abstract_origin refers to functions (and
17878 many things under the function DIE, but we do not recurse
17879 into function DIEs during partial symbol reading) and
17880 possibly variables as well; DW_AT_specification refers to
17881 declarations. Declarations ought to have the DW_AT_declaration
17882 flag. It happens that GCC forgets to put it in sometimes, but
17883 only for functions, not for types.
17884
17885 Adding more things than necessary to the hash table is harmless
17886 except for the performance cost. Adding too few will result in
17887 wasted time in find_partial_die, when we reread the compilation
17888 unit with load_all_dies set. */
17889
17890 if (load_all
17891 || abbrev->tag == DW_TAG_constant
17892 || abbrev->tag == DW_TAG_subprogram
17893 || abbrev->tag == DW_TAG_variable
17894 || abbrev->tag == DW_TAG_namespace
17895 || part_die->is_declaration)
17896 {
17897 void **slot;
17898
17899 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
17900 to_underlying (part_die->sect_off),
17901 INSERT);
17902 *slot = part_die;
17903 }
17904
17905 /* For some DIEs we want to follow their children (if any). For C
17906 we have no reason to follow the children of structures; for other
17907 languages we have to, so that we can get at method physnames
17908 to infer fully qualified class names, for DW_AT_specification,
17909 and for C++ template arguments. For C++, we also look one level
17910 inside functions to find template arguments (if the name of the
17911 function does not already contain the template arguments).
17912
17913 For Ada and Fortran, we need to scan the children of subprograms
17914 and lexical blocks as well because these languages allow the
17915 definition of nested entities that could be interesting for the
17916 debugger, such as nested subprograms for instance. */
17917 if (last_die->has_children
17918 && (load_all
17919 || last_die->tag == DW_TAG_namespace
17920 || last_die->tag == DW_TAG_module
17921 || last_die->tag == DW_TAG_enumeration_type
17922 || (cu->language == language_cplus
17923 && last_die->tag == DW_TAG_subprogram
17924 && (last_die->name == NULL
17925 || strchr (last_die->name, '<') == NULL))
17926 || (cu->language != language_c
17927 && (last_die->tag == DW_TAG_class_type
17928 || last_die->tag == DW_TAG_interface_type
17929 || last_die->tag == DW_TAG_structure_type
17930 || last_die->tag == DW_TAG_union_type))
17931 || ((cu->language == language_ada
17932 || cu->language == language_fortran)
17933 && (last_die->tag == DW_TAG_subprogram
17934 || last_die->tag == DW_TAG_lexical_block))))
17935 {
17936 nesting_level++;
17937 parent_die = last_die;
17938 continue;
17939 }
17940
17941 /* Otherwise we skip to the next sibling, if any. */
17942 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
17943
17944 /* Back to the top, do it again. */
17945 }
17946 }
17947
17948 partial_die_info::partial_die_info (sect_offset sect_off_,
17949 struct abbrev_info *abbrev)
17950 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
17951 {
17952 }
17953
17954 /* Read a minimal amount of information into the minimal die structure.
17955 INFO_PTR should point just after the initial uleb128 of a DIE. */
17956
17957 const gdb_byte *
17958 partial_die_info::read (const struct die_reader_specs *reader,
17959 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
17960 {
17961 struct dwarf2_cu *cu = reader->cu;
17962 struct dwarf2_per_objfile *dwarf2_per_objfile
17963 = cu->per_cu->dwarf2_per_objfile;
17964 unsigned int i;
17965 int has_low_pc_attr = 0;
17966 int has_high_pc_attr = 0;
17967 int high_pc_relative = 0;
17968
17969 std::vector<struct attribute> attr_vec (abbrev.num_attrs);
17970 for (i = 0; i < abbrev.num_attrs; ++i)
17971 {
17972 bool need_reprocess;
17973 info_ptr = read_attribute (reader, &attr_vec[i], &abbrev.attrs[i],
17974 info_ptr, &need_reprocess);
17975 /* String and address offsets that need to do the reprocessing have
17976 already been read at this point, so there is no need to wait until
17977 the loop terminates to do the reprocessing. */
17978 if (need_reprocess)
17979 read_attribute_reprocess (reader, &attr_vec[i]);
17980 attribute &attr = attr_vec[i];
17981 /* Store the data if it is of an attribute we want to keep in a
17982 partial symbol table. */
17983 switch (attr.name)
17984 {
17985 case DW_AT_name:
17986 switch (tag)
17987 {
17988 case DW_TAG_compile_unit:
17989 case DW_TAG_partial_unit:
17990 case DW_TAG_type_unit:
17991 /* Compilation units have a DW_AT_name that is a filename, not
17992 a source language identifier. */
17993 case DW_TAG_enumeration_type:
17994 case DW_TAG_enumerator:
17995 /* These tags always have simple identifiers already; no need
17996 to canonicalize them. */
17997 name = DW_STRING (&attr);
17998 break;
17999 default:
18000 {
18001 struct objfile *objfile = dwarf2_per_objfile->objfile;
18002
18003 name
18004 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
18005 }
18006 break;
18007 }
18008 break;
18009 case DW_AT_linkage_name:
18010 case DW_AT_MIPS_linkage_name:
18011 /* Note that both forms of linkage name might appear. We
18012 assume they will be the same, and we only store the last
18013 one we see. */
18014 linkage_name = DW_STRING (&attr);
18015 break;
18016 case DW_AT_low_pc:
18017 has_low_pc_attr = 1;
18018 lowpc = attr.value_as_address ();
18019 break;
18020 case DW_AT_high_pc:
18021 has_high_pc_attr = 1;
18022 highpc = attr.value_as_address ();
18023 if (cu->header.version >= 4 && attr.form_is_constant ())
18024 high_pc_relative = 1;
18025 break;
18026 case DW_AT_location:
18027 /* Support the .debug_loc offsets. */
18028 if (attr.form_is_block ())
18029 {
18030 d.locdesc = DW_BLOCK (&attr);
18031 }
18032 else if (attr.form_is_section_offset ())
18033 {
18034 dwarf2_complex_location_expr_complaint ();
18035 }
18036 else
18037 {
18038 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18039 "partial symbol information");
18040 }
18041 break;
18042 case DW_AT_external:
18043 is_external = DW_UNSND (&attr);
18044 break;
18045 case DW_AT_declaration:
18046 is_declaration = DW_UNSND (&attr);
18047 break;
18048 case DW_AT_type:
18049 has_type = 1;
18050 break;
18051 case DW_AT_abstract_origin:
18052 case DW_AT_specification:
18053 case DW_AT_extension:
18054 has_specification = 1;
18055 spec_offset = dwarf2_get_ref_die_offset (&attr);
18056 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18057 || cu->per_cu->is_dwz);
18058 break;
18059 case DW_AT_sibling:
18060 /* Ignore absolute siblings, they might point outside of
18061 the current compile unit. */
18062 if (attr.form == DW_FORM_ref_addr)
18063 complaint (_("ignoring absolute DW_AT_sibling"));
18064 else
18065 {
18066 const gdb_byte *buffer = reader->buffer;
18067 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18068 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18069
18070 if (sibling_ptr < info_ptr)
18071 complaint (_("DW_AT_sibling points backwards"));
18072 else if (sibling_ptr > reader->buffer_end)
18073 reader->die_section->overflow_complaint ();
18074 else
18075 sibling = sibling_ptr;
18076 }
18077 break;
18078 case DW_AT_byte_size:
18079 has_byte_size = 1;
18080 break;
18081 case DW_AT_const_value:
18082 has_const_value = 1;
18083 break;
18084 case DW_AT_calling_convention:
18085 /* DWARF doesn't provide a way to identify a program's source-level
18086 entry point. DW_AT_calling_convention attributes are only meant
18087 to describe functions' calling conventions.
18088
18089 However, because it's a necessary piece of information in
18090 Fortran, and before DWARF 4 DW_CC_program was the only
18091 piece of debugging information whose definition refers to
18092 a 'main program' at all, several compilers marked Fortran
18093 main programs with DW_CC_program --- even when those
18094 functions use the standard calling conventions.
18095
18096 Although DWARF now specifies a way to provide this
18097 information, we support this practice for backward
18098 compatibility. */
18099 if (DW_UNSND (&attr) == DW_CC_program
18100 && cu->language == language_fortran)
18101 main_subprogram = 1;
18102 break;
18103 case DW_AT_inline:
18104 if (DW_UNSND (&attr) == DW_INL_inlined
18105 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18106 may_be_inlined = 1;
18107 break;
18108
18109 case DW_AT_import:
18110 if (tag == DW_TAG_imported_unit)
18111 {
18112 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18113 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18114 || cu->per_cu->is_dwz);
18115 }
18116 break;
18117
18118 case DW_AT_main_subprogram:
18119 main_subprogram = DW_UNSND (&attr);
18120 break;
18121
18122 case DW_AT_ranges:
18123 {
18124 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18125 but that requires a full DIE, so instead we just
18126 reimplement it. */
18127 int need_ranges_base = tag != DW_TAG_compile_unit;
18128 unsigned int ranges_offset = (DW_UNSND (&attr)
18129 + (need_ranges_base
18130 ? cu->ranges_base
18131 : 0));
18132
18133 /* Value of the DW_AT_ranges attribute is the offset in the
18134 .debug_ranges section. */
18135 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18136 nullptr))
18137 has_pc_info = 1;
18138 }
18139 break;
18140
18141 default:
18142 break;
18143 }
18144 }
18145
18146 /* For Ada, if both the name and the linkage name appear, we prefer
18147 the latter. This lets "catch exception" work better, regardless
18148 of the order in which the name and linkage name were emitted.
18149 Really, though, this is just a workaround for the fact that gdb
18150 doesn't store both the name and the linkage name. */
18151 if (cu->language == language_ada && linkage_name != nullptr)
18152 name = linkage_name;
18153
18154 if (high_pc_relative)
18155 highpc += lowpc;
18156
18157 if (has_low_pc_attr && has_high_pc_attr)
18158 {
18159 /* When using the GNU linker, .gnu.linkonce. sections are used to
18160 eliminate duplicate copies of functions and vtables and such.
18161 The linker will arbitrarily choose one and discard the others.
18162 The AT_*_pc values for such functions refer to local labels in
18163 these sections. If the section from that file was discarded, the
18164 labels are not in the output, so the relocs get a value of 0.
18165 If this is a discarded function, mark the pc bounds as invalid,
18166 so that GDB will ignore it. */
18167 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18168 {
18169 struct objfile *objfile = dwarf2_per_objfile->objfile;
18170 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18171
18172 complaint (_("DW_AT_low_pc %s is zero "
18173 "for DIE at %s [in module %s]"),
18174 paddress (gdbarch, lowpc),
18175 sect_offset_str (sect_off),
18176 objfile_name (objfile));
18177 }
18178 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18179 else if (lowpc >= highpc)
18180 {
18181 struct objfile *objfile = dwarf2_per_objfile->objfile;
18182 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18183
18184 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18185 "for DIE at %s [in module %s]"),
18186 paddress (gdbarch, lowpc),
18187 paddress (gdbarch, highpc),
18188 sect_offset_str (sect_off),
18189 objfile_name (objfile));
18190 }
18191 else
18192 has_pc_info = 1;
18193 }
18194
18195 return info_ptr;
18196 }
18197
18198 /* Find a cached partial DIE at OFFSET in CU. */
18199
18200 struct partial_die_info *
18201 dwarf2_cu::find_partial_die (sect_offset sect_off)
18202 {
18203 struct partial_die_info *lookup_die = NULL;
18204 struct partial_die_info part_die (sect_off);
18205
18206 lookup_die = ((struct partial_die_info *)
18207 htab_find_with_hash (partial_dies, &part_die,
18208 to_underlying (sect_off)));
18209
18210 return lookup_die;
18211 }
18212
18213 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18214 except in the case of .debug_types DIEs which do not reference
18215 outside their CU (they do however referencing other types via
18216 DW_FORM_ref_sig8). */
18217
18218 static const struct cu_partial_die_info
18219 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18220 {
18221 struct dwarf2_per_objfile *dwarf2_per_objfile
18222 = cu->per_cu->dwarf2_per_objfile;
18223 struct objfile *objfile = dwarf2_per_objfile->objfile;
18224 struct dwarf2_per_cu_data *per_cu = NULL;
18225 struct partial_die_info *pd = NULL;
18226
18227 if (offset_in_dwz == cu->per_cu->is_dwz
18228 && cu->header.offset_in_cu_p (sect_off))
18229 {
18230 pd = cu->find_partial_die (sect_off);
18231 if (pd != NULL)
18232 return { cu, pd };
18233 /* We missed recording what we needed.
18234 Load all dies and try again. */
18235 per_cu = cu->per_cu;
18236 }
18237 else
18238 {
18239 /* TUs don't reference other CUs/TUs (except via type signatures). */
18240 if (cu->per_cu->is_debug_types)
18241 {
18242 error (_("Dwarf Error: Type Unit at offset %s contains"
18243 " external reference to offset %s [in module %s].\n"),
18244 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18245 bfd_get_filename (objfile->obfd));
18246 }
18247 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18248 dwarf2_per_objfile);
18249
18250 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18251 load_partial_comp_unit (per_cu);
18252
18253 per_cu->cu->last_used = 0;
18254 pd = per_cu->cu->find_partial_die (sect_off);
18255 }
18256
18257 /* If we didn't find it, and not all dies have been loaded,
18258 load them all and try again. */
18259
18260 if (pd == NULL && per_cu->load_all_dies == 0)
18261 {
18262 per_cu->load_all_dies = 1;
18263
18264 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18265 THIS_CU->cu may already be in use. So we can't just free it and
18266 replace its DIEs with the ones we read in. Instead, we leave those
18267 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18268 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18269 set. */
18270 load_partial_comp_unit (per_cu);
18271
18272 pd = per_cu->cu->find_partial_die (sect_off);
18273 }
18274
18275 if (pd == NULL)
18276 internal_error (__FILE__, __LINE__,
18277 _("could not find partial DIE %s "
18278 "in cache [from module %s]\n"),
18279 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18280 return { per_cu->cu, pd };
18281 }
18282
18283 /* See if we can figure out if the class lives in a namespace. We do
18284 this by looking for a member function; its demangled name will
18285 contain namespace info, if there is any. */
18286
18287 static void
18288 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18289 struct dwarf2_cu *cu)
18290 {
18291 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18292 what template types look like, because the demangler
18293 frequently doesn't give the same name as the debug info. We
18294 could fix this by only using the demangled name to get the
18295 prefix (but see comment in read_structure_type). */
18296
18297 struct partial_die_info *real_pdi;
18298 struct partial_die_info *child_pdi;
18299
18300 /* If this DIE (this DIE's specification, if any) has a parent, then
18301 we should not do this. We'll prepend the parent's fully qualified
18302 name when we create the partial symbol. */
18303
18304 real_pdi = struct_pdi;
18305 while (real_pdi->has_specification)
18306 {
18307 auto res = find_partial_die (real_pdi->spec_offset,
18308 real_pdi->spec_is_dwz, cu);
18309 real_pdi = res.pdi;
18310 cu = res.cu;
18311 }
18312
18313 if (real_pdi->die_parent != NULL)
18314 return;
18315
18316 for (child_pdi = struct_pdi->die_child;
18317 child_pdi != NULL;
18318 child_pdi = child_pdi->die_sibling)
18319 {
18320 if (child_pdi->tag == DW_TAG_subprogram
18321 && child_pdi->linkage_name != NULL)
18322 {
18323 gdb::unique_xmalloc_ptr<char> actual_class_name
18324 (language_class_name_from_physname (cu->language_defn,
18325 child_pdi->linkage_name));
18326 if (actual_class_name != NULL)
18327 {
18328 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18329 struct_pdi->name = objfile->intern (actual_class_name.get ());
18330 }
18331 break;
18332 }
18333 }
18334 }
18335
18336 void
18337 partial_die_info::fixup (struct dwarf2_cu *cu)
18338 {
18339 /* Once we've fixed up a die, there's no point in doing so again.
18340 This also avoids a memory leak if we were to call
18341 guess_partial_die_structure_name multiple times. */
18342 if (fixup_called)
18343 return;
18344
18345 /* If we found a reference attribute and the DIE has no name, try
18346 to find a name in the referred to DIE. */
18347
18348 if (name == NULL && has_specification)
18349 {
18350 struct partial_die_info *spec_die;
18351
18352 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18353 spec_die = res.pdi;
18354 cu = res.cu;
18355
18356 spec_die->fixup (cu);
18357
18358 if (spec_die->name)
18359 {
18360 name = spec_die->name;
18361
18362 /* Copy DW_AT_external attribute if it is set. */
18363 if (spec_die->is_external)
18364 is_external = spec_die->is_external;
18365 }
18366 }
18367
18368 /* Set default names for some unnamed DIEs. */
18369
18370 if (name == NULL && tag == DW_TAG_namespace)
18371 name = CP_ANONYMOUS_NAMESPACE_STR;
18372
18373 /* If there is no parent die to provide a namespace, and there are
18374 children, see if we can determine the namespace from their linkage
18375 name. */
18376 if (cu->language == language_cplus
18377 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
18378 && die_parent == NULL
18379 && has_children
18380 && (tag == DW_TAG_class_type
18381 || tag == DW_TAG_structure_type
18382 || tag == DW_TAG_union_type))
18383 guess_partial_die_structure_name (this, cu);
18384
18385 /* GCC might emit a nameless struct or union that has a linkage
18386 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18387 if (name == NULL
18388 && (tag == DW_TAG_class_type
18389 || tag == DW_TAG_interface_type
18390 || tag == DW_TAG_structure_type
18391 || tag == DW_TAG_union_type)
18392 && linkage_name != NULL)
18393 {
18394 gdb::unique_xmalloc_ptr<char> demangled
18395 (gdb_demangle (linkage_name, DMGL_TYPES));
18396 if (demangled != nullptr)
18397 {
18398 const char *base;
18399
18400 /* Strip any leading namespaces/classes, keep only the base name.
18401 DW_AT_name for named DIEs does not contain the prefixes. */
18402 base = strrchr (demangled.get (), ':');
18403 if (base && base > demangled.get () && base[-1] == ':')
18404 base++;
18405 else
18406 base = demangled.get ();
18407
18408 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18409 name = objfile->intern (base);
18410 }
18411 }
18412
18413 fixup_called = 1;
18414 }
18415
18416 /* Process the attributes that had to be skipped in the first round. These
18417 attributes are the ones that need str_offsets_base or addr_base attributes.
18418 They could not have been processed in the first round, because at the time
18419 the values of str_offsets_base or addr_base may not have been known. */
18420 void read_attribute_reprocess (const struct die_reader_specs *reader,
18421 struct attribute *attr)
18422 {
18423 struct dwarf2_cu *cu = reader->cu;
18424 switch (attr->form)
18425 {
18426 case DW_FORM_addrx:
18427 case DW_FORM_GNU_addr_index:
18428 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18429 break;
18430 case DW_FORM_strx:
18431 case DW_FORM_strx1:
18432 case DW_FORM_strx2:
18433 case DW_FORM_strx3:
18434 case DW_FORM_strx4:
18435 case DW_FORM_GNU_str_index:
18436 {
18437 unsigned int str_index = DW_UNSND (attr);
18438 if (reader->dwo_file != NULL)
18439 {
18440 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18441 DW_STRING_IS_CANONICAL (attr) = 0;
18442 }
18443 else
18444 {
18445 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18446 DW_STRING_IS_CANONICAL (attr) = 0;
18447 }
18448 break;
18449 }
18450 default:
18451 gdb_assert_not_reached (_("Unexpected DWARF form."));
18452 }
18453 }
18454
18455 /* Read an attribute value described by an attribute form. */
18456
18457 static const gdb_byte *
18458 read_attribute_value (const struct die_reader_specs *reader,
18459 struct attribute *attr, unsigned form,
18460 LONGEST implicit_const, const gdb_byte *info_ptr,
18461 bool *need_reprocess)
18462 {
18463 struct dwarf2_cu *cu = reader->cu;
18464 struct dwarf2_per_objfile *dwarf2_per_objfile
18465 = cu->per_cu->dwarf2_per_objfile;
18466 struct objfile *objfile = dwarf2_per_objfile->objfile;
18467 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18468 bfd *abfd = reader->abfd;
18469 struct comp_unit_head *cu_header = &cu->header;
18470 unsigned int bytes_read;
18471 struct dwarf_block *blk;
18472 *need_reprocess = false;
18473
18474 attr->form = (enum dwarf_form) form;
18475 switch (form)
18476 {
18477 case DW_FORM_ref_addr:
18478 if (cu->header.version == 2)
18479 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
18480 &bytes_read);
18481 else
18482 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
18483 &bytes_read);
18484 info_ptr += bytes_read;
18485 break;
18486 case DW_FORM_GNU_ref_alt:
18487 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18488 info_ptr += bytes_read;
18489 break;
18490 case DW_FORM_addr:
18491 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
18492 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
18493 info_ptr += bytes_read;
18494 break;
18495 case DW_FORM_block2:
18496 blk = dwarf_alloc_block (cu);
18497 blk->size = read_2_bytes (abfd, info_ptr);
18498 info_ptr += 2;
18499 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18500 info_ptr += blk->size;
18501 DW_BLOCK (attr) = blk;
18502 break;
18503 case DW_FORM_block4:
18504 blk = dwarf_alloc_block (cu);
18505 blk->size = read_4_bytes (abfd, info_ptr);
18506 info_ptr += 4;
18507 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18508 info_ptr += blk->size;
18509 DW_BLOCK (attr) = blk;
18510 break;
18511 case DW_FORM_data2:
18512 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18513 info_ptr += 2;
18514 break;
18515 case DW_FORM_data4:
18516 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18517 info_ptr += 4;
18518 break;
18519 case DW_FORM_data8:
18520 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18521 info_ptr += 8;
18522 break;
18523 case DW_FORM_data16:
18524 blk = dwarf_alloc_block (cu);
18525 blk->size = 16;
18526 blk->data = read_n_bytes (abfd, info_ptr, 16);
18527 info_ptr += 16;
18528 DW_BLOCK (attr) = blk;
18529 break;
18530 case DW_FORM_sec_offset:
18531 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18532 info_ptr += bytes_read;
18533 break;
18534 case DW_FORM_string:
18535 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
18536 DW_STRING_IS_CANONICAL (attr) = 0;
18537 info_ptr += bytes_read;
18538 break;
18539 case DW_FORM_strp:
18540 if (!cu->per_cu->is_dwz)
18541 {
18542 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18543 abfd, info_ptr, cu_header,
18544 &bytes_read);
18545 DW_STRING_IS_CANONICAL (attr) = 0;
18546 info_ptr += bytes_read;
18547 break;
18548 }
18549 /* FALLTHROUGH */
18550 case DW_FORM_line_strp:
18551 if (!cu->per_cu->is_dwz)
18552 {
18553 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
18554 abfd, info_ptr,
18555 cu_header, &bytes_read);
18556 DW_STRING_IS_CANONICAL (attr) = 0;
18557 info_ptr += bytes_read;
18558 break;
18559 }
18560 /* FALLTHROUGH */
18561 case DW_FORM_GNU_strp_alt:
18562 {
18563 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
18564 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
18565 &bytes_read);
18566
18567 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
18568 DW_STRING_IS_CANONICAL (attr) = 0;
18569 info_ptr += bytes_read;
18570 }
18571 break;
18572 case DW_FORM_exprloc:
18573 case DW_FORM_block:
18574 blk = dwarf_alloc_block (cu);
18575 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18576 info_ptr += bytes_read;
18577 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18578 info_ptr += blk->size;
18579 DW_BLOCK (attr) = blk;
18580 break;
18581 case DW_FORM_block1:
18582 blk = dwarf_alloc_block (cu);
18583 blk->size = read_1_byte (abfd, info_ptr);
18584 info_ptr += 1;
18585 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18586 info_ptr += blk->size;
18587 DW_BLOCK (attr) = blk;
18588 break;
18589 case DW_FORM_data1:
18590 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18591 info_ptr += 1;
18592 break;
18593 case DW_FORM_flag:
18594 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18595 info_ptr += 1;
18596 break;
18597 case DW_FORM_flag_present:
18598 DW_UNSND (attr) = 1;
18599 break;
18600 case DW_FORM_sdata:
18601 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18602 info_ptr += bytes_read;
18603 break;
18604 case DW_FORM_udata:
18605 case DW_FORM_rnglistx:
18606 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18607 info_ptr += bytes_read;
18608 break;
18609 case DW_FORM_ref1:
18610 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18611 + read_1_byte (abfd, info_ptr));
18612 info_ptr += 1;
18613 break;
18614 case DW_FORM_ref2:
18615 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18616 + read_2_bytes (abfd, info_ptr));
18617 info_ptr += 2;
18618 break;
18619 case DW_FORM_ref4:
18620 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18621 + read_4_bytes (abfd, info_ptr));
18622 info_ptr += 4;
18623 break;
18624 case DW_FORM_ref8:
18625 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18626 + read_8_bytes (abfd, info_ptr));
18627 info_ptr += 8;
18628 break;
18629 case DW_FORM_ref_sig8:
18630 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
18631 info_ptr += 8;
18632 break;
18633 case DW_FORM_ref_udata:
18634 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18635 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
18636 info_ptr += bytes_read;
18637 break;
18638 case DW_FORM_indirect:
18639 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18640 info_ptr += bytes_read;
18641 if (form == DW_FORM_implicit_const)
18642 {
18643 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18644 info_ptr += bytes_read;
18645 }
18646 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18647 info_ptr, need_reprocess);
18648 break;
18649 case DW_FORM_implicit_const:
18650 DW_SND (attr) = implicit_const;
18651 break;
18652 case DW_FORM_addrx:
18653 case DW_FORM_GNU_addr_index:
18654 *need_reprocess = true;
18655 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18656 info_ptr += bytes_read;
18657 break;
18658 case DW_FORM_strx:
18659 case DW_FORM_strx1:
18660 case DW_FORM_strx2:
18661 case DW_FORM_strx3:
18662 case DW_FORM_strx4:
18663 case DW_FORM_GNU_str_index:
18664 {
18665 ULONGEST str_index;
18666 if (form == DW_FORM_strx1)
18667 {
18668 str_index = read_1_byte (abfd, info_ptr);
18669 info_ptr += 1;
18670 }
18671 else if (form == DW_FORM_strx2)
18672 {
18673 str_index = read_2_bytes (abfd, info_ptr);
18674 info_ptr += 2;
18675 }
18676 else if (form == DW_FORM_strx3)
18677 {
18678 str_index = read_3_bytes (abfd, info_ptr);
18679 info_ptr += 3;
18680 }
18681 else if (form == DW_FORM_strx4)
18682 {
18683 str_index = read_4_bytes (abfd, info_ptr);
18684 info_ptr += 4;
18685 }
18686 else
18687 {
18688 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18689 info_ptr += bytes_read;
18690 }
18691 *need_reprocess = true;
18692 DW_UNSND (attr) = str_index;
18693 }
18694 break;
18695 default:
18696 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
18697 dwarf_form_name (form),
18698 bfd_get_filename (abfd));
18699 }
18700
18701 /* Super hack. */
18702 if (cu->per_cu->is_dwz && attr->form_is_ref ())
18703 attr->form = DW_FORM_GNU_ref_alt;
18704
18705 /* We have seen instances where the compiler tried to emit a byte
18706 size attribute of -1 which ended up being encoded as an unsigned
18707 0xffffffff. Although 0xffffffff is technically a valid size value,
18708 an object of this size seems pretty unlikely so we can relatively
18709 safely treat these cases as if the size attribute was invalid and
18710 treat them as zero by default. */
18711 if (attr->name == DW_AT_byte_size
18712 && form == DW_FORM_data4
18713 && DW_UNSND (attr) >= 0xffffffff)
18714 {
18715 complaint
18716 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
18717 hex_string (DW_UNSND (attr)));
18718 DW_UNSND (attr) = 0;
18719 }
18720
18721 return info_ptr;
18722 }
18723
18724 /* Read an attribute described by an abbreviated attribute. */
18725
18726 static const gdb_byte *
18727 read_attribute (const struct die_reader_specs *reader,
18728 struct attribute *attr, struct attr_abbrev *abbrev,
18729 const gdb_byte *info_ptr, bool *need_reprocess)
18730 {
18731 attr->name = abbrev->name;
18732 return read_attribute_value (reader, attr, abbrev->form,
18733 abbrev->implicit_const, info_ptr,
18734 need_reprocess);
18735 }
18736
18737 /* Cover function for read_initial_length.
18738 Returns the length of the object at BUF, and stores the size of the
18739 initial length in *BYTES_READ and stores the size that offsets will be in
18740 *OFFSET_SIZE.
18741 If the initial length size is not equivalent to that specified in
18742 CU_HEADER then issue a complaint.
18743 This is useful when reading non-comp-unit headers. */
18744
18745 static LONGEST
18746 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
18747 const struct comp_unit_head *cu_header,
18748 unsigned int *bytes_read,
18749 unsigned int *offset_size)
18750 {
18751 LONGEST length = read_initial_length (abfd, buf, bytes_read);
18752
18753 gdb_assert (cu_header->initial_length_size == 4
18754 || cu_header->initial_length_size == 8
18755 || cu_header->initial_length_size == 12);
18756
18757 if (cu_header->initial_length_size != *bytes_read)
18758 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
18759
18760 *offset_size = (*bytes_read == 4) ? 4 : 8;
18761 return length;
18762 }
18763
18764 /* Return pointer to string at .debug_str offset STR_OFFSET. */
18765
18766 static const char *
18767 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
18768 LONGEST str_offset)
18769 {
18770 return dwarf2_per_objfile->str.read_string (dwarf2_per_objfile->objfile,
18771 str_offset, "DW_FORM_strp");
18772 }
18773
18774 /* Return pointer to string at .debug_str offset as read from BUF.
18775 BUF is assumed to be in a compilation unit described by CU_HEADER.
18776 Return *BYTES_READ_PTR count of bytes read from BUF. */
18777
18778 static const char *
18779 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
18780 const gdb_byte *buf,
18781 const struct comp_unit_head *cu_header,
18782 unsigned int *bytes_read_ptr)
18783 {
18784 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
18785
18786 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
18787 }
18788
18789 /* Return pointer to string at .debug_line_str offset as read from BUF.
18790 BUF is assumed to be in a compilation unit described by CU_HEADER.
18791 Return *BYTES_READ_PTR count of bytes read from BUF. */
18792
18793 static const char *
18794 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
18795 bfd *abfd, const gdb_byte *buf,
18796 const struct comp_unit_head *cu_header,
18797 unsigned int *bytes_read_ptr)
18798 {
18799 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
18800
18801 return dwarf2_per_objfile->line_str.read_string (dwarf2_per_objfile->objfile,
18802 str_offset,
18803 "DW_FORM_line_strp");
18804 }
18805
18806 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
18807 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
18808 ADDR_SIZE is the size of addresses from the CU header. */
18809
18810 static CORE_ADDR
18811 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18812 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
18813 int addr_size)
18814 {
18815 struct objfile *objfile = dwarf2_per_objfile->objfile;
18816 bfd *abfd = objfile->obfd;
18817 const gdb_byte *info_ptr;
18818 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
18819
18820 dwarf2_per_objfile->addr.read (objfile);
18821 if (dwarf2_per_objfile->addr.buffer == NULL)
18822 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
18823 objfile_name (objfile));
18824 if (addr_base_or_zero + addr_index * addr_size
18825 >= dwarf2_per_objfile->addr.size)
18826 error (_("DW_FORM_addr_index pointing outside of "
18827 ".debug_addr section [in module %s]"),
18828 objfile_name (objfile));
18829 info_ptr = (dwarf2_per_objfile->addr.buffer
18830 + addr_base_or_zero + addr_index * addr_size);
18831 if (addr_size == 4)
18832 return bfd_get_32 (abfd, info_ptr);
18833 else
18834 return bfd_get_64 (abfd, info_ptr);
18835 }
18836
18837 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
18838
18839 static CORE_ADDR
18840 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
18841 {
18842 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
18843 cu->addr_base, cu->header.addr_size);
18844 }
18845
18846 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
18847
18848 static CORE_ADDR
18849 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
18850 unsigned int *bytes_read)
18851 {
18852 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
18853 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
18854
18855 return read_addr_index (cu, addr_index);
18856 }
18857
18858 /* See read.h. */
18859
18860 CORE_ADDR
18861 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu, unsigned int addr_index)
18862 {
18863 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
18864 struct dwarf2_cu *cu = per_cu->cu;
18865 gdb::optional<ULONGEST> addr_base;
18866 int addr_size;
18867
18868 /* We need addr_base and addr_size.
18869 If we don't have PER_CU->cu, we have to get it.
18870 Nasty, but the alternative is storing the needed info in PER_CU,
18871 which at this point doesn't seem justified: it's not clear how frequently
18872 it would get used and it would increase the size of every PER_CU.
18873 Entry points like dwarf2_per_cu_addr_size do a similar thing
18874 so we're not in uncharted territory here.
18875 Alas we need to be a bit more complicated as addr_base is contained
18876 in the DIE.
18877
18878 We don't need to read the entire CU(/TU).
18879 We just need the header and top level die.
18880
18881 IWBN to use the aging mechanism to let us lazily later discard the CU.
18882 For now we skip this optimization. */
18883
18884 if (cu != NULL)
18885 {
18886 addr_base = cu->addr_base;
18887 addr_size = cu->header.addr_size;
18888 }
18889 else
18890 {
18891 cutu_reader reader (per_cu, NULL, 0, false);
18892 addr_base = reader.cu->addr_base;
18893 addr_size = reader.cu->header.addr_size;
18894 }
18895
18896 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
18897 addr_size);
18898 }
18899
18900 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
18901 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
18902 DWO file. */
18903
18904 static const char *
18905 read_str_index (struct dwarf2_cu *cu,
18906 struct dwarf2_section_info *str_section,
18907 struct dwarf2_section_info *str_offsets_section,
18908 ULONGEST str_offsets_base, ULONGEST str_index)
18909 {
18910 struct dwarf2_per_objfile *dwarf2_per_objfile
18911 = cu->per_cu->dwarf2_per_objfile;
18912 struct objfile *objfile = dwarf2_per_objfile->objfile;
18913 const char *objf_name = objfile_name (objfile);
18914 bfd *abfd = objfile->obfd;
18915 const gdb_byte *info_ptr;
18916 ULONGEST str_offset;
18917 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
18918
18919 str_section->read (objfile);
18920 str_offsets_section->read (objfile);
18921 if (str_section->buffer == NULL)
18922 error (_("%s used without %s section"
18923 " in CU at offset %s [in module %s]"),
18924 form_name, str_section->get_name (),
18925 sect_offset_str (cu->header.sect_off), objf_name);
18926 if (str_offsets_section->buffer == NULL)
18927 error (_("%s used without %s section"
18928 " in CU at offset %s [in module %s]"),
18929 form_name, str_section->get_name (),
18930 sect_offset_str (cu->header.sect_off), objf_name);
18931 info_ptr = (str_offsets_section->buffer
18932 + str_offsets_base
18933 + str_index * cu->header.offset_size);
18934 if (cu->header.offset_size == 4)
18935 str_offset = bfd_get_32 (abfd, info_ptr);
18936 else
18937 str_offset = bfd_get_64 (abfd, info_ptr);
18938 if (str_offset >= str_section->size)
18939 error (_("Offset from %s pointing outside of"
18940 " .debug_str.dwo section in CU at offset %s [in module %s]"),
18941 form_name, sect_offset_str (cu->header.sect_off), objf_name);
18942 return (const char *) (str_section->buffer + str_offset);
18943 }
18944
18945 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
18946
18947 static const char *
18948 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
18949 {
18950 ULONGEST str_offsets_base = reader->cu->header.version >= 5
18951 ? reader->cu->header.addr_size : 0;
18952 return read_str_index (reader->cu,
18953 &reader->dwo_file->sections.str,
18954 &reader->dwo_file->sections.str_offsets,
18955 str_offsets_base, str_index);
18956 }
18957
18958 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
18959
18960 static const char *
18961 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
18962 {
18963 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18964 const char *objf_name = objfile_name (objfile);
18965 static const char form_name[] = "DW_FORM_GNU_str_index";
18966 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
18967
18968 if (!cu->str_offsets_base.has_value ())
18969 error (_("%s used in Fission stub without %s"
18970 " in CU at offset 0x%lx [in module %s]"),
18971 form_name, str_offsets_attr_name,
18972 (long) cu->header.offset_size, objf_name);
18973
18974 return read_str_index (cu,
18975 &cu->per_cu->dwarf2_per_objfile->str,
18976 &cu->per_cu->dwarf2_per_objfile->str_offsets,
18977 *cu->str_offsets_base, str_index);
18978 }
18979
18980 /* Return the length of an LEB128 number in BUF. */
18981
18982 static int
18983 leb128_size (const gdb_byte *buf)
18984 {
18985 const gdb_byte *begin = buf;
18986 gdb_byte byte;
18987
18988 while (1)
18989 {
18990 byte = *buf++;
18991 if ((byte & 128) == 0)
18992 return buf - begin;
18993 }
18994 }
18995
18996 static void
18997 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
18998 {
18999 switch (lang)
19000 {
19001 case DW_LANG_C89:
19002 case DW_LANG_C99:
19003 case DW_LANG_C11:
19004 case DW_LANG_C:
19005 case DW_LANG_UPC:
19006 cu->language = language_c;
19007 break;
19008 case DW_LANG_Java:
19009 case DW_LANG_C_plus_plus:
19010 case DW_LANG_C_plus_plus_11:
19011 case DW_LANG_C_plus_plus_14:
19012 cu->language = language_cplus;
19013 break;
19014 case DW_LANG_D:
19015 cu->language = language_d;
19016 break;
19017 case DW_LANG_Fortran77:
19018 case DW_LANG_Fortran90:
19019 case DW_LANG_Fortran95:
19020 case DW_LANG_Fortran03:
19021 case DW_LANG_Fortran08:
19022 cu->language = language_fortran;
19023 break;
19024 case DW_LANG_Go:
19025 cu->language = language_go;
19026 break;
19027 case DW_LANG_Mips_Assembler:
19028 cu->language = language_asm;
19029 break;
19030 case DW_LANG_Ada83:
19031 case DW_LANG_Ada95:
19032 cu->language = language_ada;
19033 break;
19034 case DW_LANG_Modula2:
19035 cu->language = language_m2;
19036 break;
19037 case DW_LANG_Pascal83:
19038 cu->language = language_pascal;
19039 break;
19040 case DW_LANG_ObjC:
19041 cu->language = language_objc;
19042 break;
19043 case DW_LANG_Rust:
19044 case DW_LANG_Rust_old:
19045 cu->language = language_rust;
19046 break;
19047 case DW_LANG_Cobol74:
19048 case DW_LANG_Cobol85:
19049 default:
19050 cu->language = language_minimal;
19051 break;
19052 }
19053 cu->language_defn = language_def (cu->language);
19054 }
19055
19056 /* Return the named attribute or NULL if not there. */
19057
19058 static struct attribute *
19059 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19060 {
19061 for (;;)
19062 {
19063 unsigned int i;
19064 struct attribute *spec = NULL;
19065
19066 for (i = 0; i < die->num_attrs; ++i)
19067 {
19068 if (die->attrs[i].name == name)
19069 return &die->attrs[i];
19070 if (die->attrs[i].name == DW_AT_specification
19071 || die->attrs[i].name == DW_AT_abstract_origin)
19072 spec = &die->attrs[i];
19073 }
19074
19075 if (!spec)
19076 break;
19077
19078 die = follow_die_ref (die, spec, &cu);
19079 }
19080
19081 return NULL;
19082 }
19083
19084 /* Return the named attribute or NULL if not there,
19085 but do not follow DW_AT_specification, etc.
19086 This is for use in contexts where we're reading .debug_types dies.
19087 Following DW_AT_specification, DW_AT_abstract_origin will take us
19088 back up the chain, and we want to go down. */
19089
19090 static struct attribute *
19091 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
19092 {
19093 unsigned int i;
19094
19095 for (i = 0; i < die->num_attrs; ++i)
19096 if (die->attrs[i].name == name)
19097 return &die->attrs[i];
19098
19099 return NULL;
19100 }
19101
19102 /* Return the string associated with a string-typed attribute, or NULL if it
19103 is either not found or is of an incorrect type. */
19104
19105 static const char *
19106 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19107 {
19108 struct attribute *attr;
19109 const char *str = NULL;
19110
19111 attr = dwarf2_attr (die, name, cu);
19112
19113 if (attr != NULL)
19114 {
19115 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
19116 || attr->form == DW_FORM_string
19117 || attr->form == DW_FORM_strx
19118 || attr->form == DW_FORM_strx1
19119 || attr->form == DW_FORM_strx2
19120 || attr->form == DW_FORM_strx3
19121 || attr->form == DW_FORM_strx4
19122 || attr->form == DW_FORM_GNU_str_index
19123 || attr->form == DW_FORM_GNU_strp_alt)
19124 str = DW_STRING (attr);
19125 else
19126 complaint (_("string type expected for attribute %s for "
19127 "DIE at %s in module %s"),
19128 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19129 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
19130 }
19131
19132 return str;
19133 }
19134
19135 /* Return the dwo name or NULL if not present. If present, it is in either
19136 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19137 static const char *
19138 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19139 {
19140 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19141 if (dwo_name == nullptr)
19142 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19143 return dwo_name;
19144 }
19145
19146 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19147 and holds a non-zero value. This function should only be used for
19148 DW_FORM_flag or DW_FORM_flag_present attributes. */
19149
19150 static int
19151 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19152 {
19153 struct attribute *attr = dwarf2_attr (die, name, cu);
19154
19155 return (attr && DW_UNSND (attr));
19156 }
19157
19158 static int
19159 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19160 {
19161 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19162 which value is non-zero. However, we have to be careful with
19163 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19164 (via dwarf2_flag_true_p) follows this attribute. So we may
19165 end up accidently finding a declaration attribute that belongs
19166 to a different DIE referenced by the specification attribute,
19167 even though the given DIE does not have a declaration attribute. */
19168 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19169 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19170 }
19171
19172 /* Return the die giving the specification for DIE, if there is
19173 one. *SPEC_CU is the CU containing DIE on input, and the CU
19174 containing the return value on output. If there is no
19175 specification, but there is an abstract origin, that is
19176 returned. */
19177
19178 static struct die_info *
19179 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19180 {
19181 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19182 *spec_cu);
19183
19184 if (spec_attr == NULL)
19185 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19186
19187 if (spec_attr == NULL)
19188 return NULL;
19189 else
19190 return follow_die_ref (die, spec_attr, spec_cu);
19191 }
19192
19193 /* Stub for free_line_header to match void * callback types. */
19194
19195 static void
19196 free_line_header_voidp (void *arg)
19197 {
19198 struct line_header *lh = (struct line_header *) arg;
19199
19200 delete lh;
19201 }
19202
19203 /* A convenience function to find the proper .debug_line section for a CU. */
19204
19205 static struct dwarf2_section_info *
19206 get_debug_line_section (struct dwarf2_cu *cu)
19207 {
19208 struct dwarf2_section_info *section;
19209 struct dwarf2_per_objfile *dwarf2_per_objfile
19210 = cu->per_cu->dwarf2_per_objfile;
19211
19212 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19213 DWO file. */
19214 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19215 section = &cu->dwo_unit->dwo_file->sections.line;
19216 else if (cu->per_cu->is_dwz)
19217 {
19218 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19219
19220 section = &dwz->line;
19221 }
19222 else
19223 section = &dwarf2_per_objfile->line;
19224
19225 return section;
19226 }
19227
19228 /* Read directory or file name entry format, starting with byte of
19229 format count entries, ULEB128 pairs of entry formats, ULEB128 of
19230 entries count and the entries themselves in the described entry
19231 format. */
19232
19233 static void
19234 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
19235 bfd *abfd, const gdb_byte **bufp,
19236 struct line_header *lh,
19237 const struct comp_unit_head *cu_header,
19238 void (*callback) (struct line_header *lh,
19239 const char *name,
19240 dir_index d_index,
19241 unsigned int mod_time,
19242 unsigned int length))
19243 {
19244 gdb_byte format_count, formati;
19245 ULONGEST data_count, datai;
19246 const gdb_byte *buf = *bufp;
19247 const gdb_byte *format_header_data;
19248 unsigned int bytes_read;
19249
19250 format_count = read_1_byte (abfd, buf);
19251 buf += 1;
19252 format_header_data = buf;
19253 for (formati = 0; formati < format_count; formati++)
19254 {
19255 read_unsigned_leb128 (abfd, buf, &bytes_read);
19256 buf += bytes_read;
19257 read_unsigned_leb128 (abfd, buf, &bytes_read);
19258 buf += bytes_read;
19259 }
19260
19261 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
19262 buf += bytes_read;
19263 for (datai = 0; datai < data_count; datai++)
19264 {
19265 const gdb_byte *format = format_header_data;
19266 struct file_entry fe;
19267
19268 for (formati = 0; formati < format_count; formati++)
19269 {
19270 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
19271 format += bytes_read;
19272
19273 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
19274 format += bytes_read;
19275
19276 gdb::optional<const char *> string;
19277 gdb::optional<unsigned int> uint;
19278
19279 switch (form)
19280 {
19281 case DW_FORM_string:
19282 string.emplace (read_direct_string (abfd, buf, &bytes_read));
19283 buf += bytes_read;
19284 break;
19285
19286 case DW_FORM_line_strp:
19287 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
19288 abfd, buf,
19289 cu_header,
19290 &bytes_read));
19291 buf += bytes_read;
19292 break;
19293
19294 case DW_FORM_data1:
19295 uint.emplace (read_1_byte (abfd, buf));
19296 buf += 1;
19297 break;
19298
19299 case DW_FORM_data2:
19300 uint.emplace (read_2_bytes (abfd, buf));
19301 buf += 2;
19302 break;
19303
19304 case DW_FORM_data4:
19305 uint.emplace (read_4_bytes (abfd, buf));
19306 buf += 4;
19307 break;
19308
19309 case DW_FORM_data8:
19310 uint.emplace (read_8_bytes (abfd, buf));
19311 buf += 8;
19312 break;
19313
19314 case DW_FORM_data16:
19315 /* This is used for MD5, but file_entry does not record MD5s. */
19316 buf += 16;
19317 break;
19318
19319 case DW_FORM_udata:
19320 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
19321 buf += bytes_read;
19322 break;
19323
19324 case DW_FORM_block:
19325 /* It is valid only for DW_LNCT_timestamp which is ignored by
19326 current GDB. */
19327 break;
19328 }
19329
19330 switch (content_type)
19331 {
19332 case DW_LNCT_path:
19333 if (string.has_value ())
19334 fe.name = *string;
19335 break;
19336 case DW_LNCT_directory_index:
19337 if (uint.has_value ())
19338 fe.d_index = (dir_index) *uint;
19339 break;
19340 case DW_LNCT_timestamp:
19341 if (uint.has_value ())
19342 fe.mod_time = *uint;
19343 break;
19344 case DW_LNCT_size:
19345 if (uint.has_value ())
19346 fe.length = *uint;
19347 break;
19348 case DW_LNCT_MD5:
19349 break;
19350 default:
19351 complaint (_("Unknown format content type %s"),
19352 pulongest (content_type));
19353 }
19354 }
19355
19356 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
19357 }
19358
19359 *bufp = buf;
19360 }
19361
19362 /* Read the statement program header starting at OFFSET in
19363 .debug_line, or .debug_line.dwo. Return a pointer
19364 to a struct line_header, allocated using xmalloc.
19365 Returns NULL if there is a problem reading the header, e.g., if it
19366 has a version we don't understand.
19367
19368 NOTE: the strings in the include directory and file name tables of
19369 the returned object point into the dwarf line section buffer,
19370 and must not be freed. */
19371
19372 static line_header_up
19373 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
19374 {
19375 const gdb_byte *line_ptr;
19376 unsigned int bytes_read, offset_size;
19377 int i;
19378 const char *cur_dir, *cur_file;
19379 struct dwarf2_section_info *section;
19380 bfd *abfd;
19381 struct dwarf2_per_objfile *dwarf2_per_objfile
19382 = cu->per_cu->dwarf2_per_objfile;
19383
19384 section = get_debug_line_section (cu);
19385 section->read (dwarf2_per_objfile->objfile);
19386 if (section->buffer == NULL)
19387 {
19388 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19389 complaint (_("missing .debug_line.dwo section"));
19390 else
19391 complaint (_("missing .debug_line section"));
19392 return 0;
19393 }
19394
19395 /* We can't do this until we know the section is non-empty.
19396 Only then do we know we have such a section. */
19397 abfd = section->get_bfd_owner ();
19398
19399 /* Make sure that at least there's room for the total_length field.
19400 That could be 12 bytes long, but we're just going to fudge that. */
19401 if (to_underlying (sect_off) + 4 >= section->size)
19402 {
19403 dwarf2_statement_list_fits_in_line_number_section_complaint ();
19404 return 0;
19405 }
19406
19407 line_header_up lh (new line_header ());
19408
19409 lh->sect_off = sect_off;
19410 lh->offset_in_dwz = cu->per_cu->is_dwz;
19411
19412 line_ptr = section->buffer + to_underlying (sect_off);
19413
19414 /* Read in the header. */
19415 lh->total_length =
19416 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
19417 &bytes_read, &offset_size);
19418 line_ptr += bytes_read;
19419
19420 const gdb_byte *start_here = line_ptr;
19421
19422 if (line_ptr + lh->total_length > (section->buffer + section->size))
19423 {
19424 dwarf2_statement_list_fits_in_line_number_section_complaint ();
19425 return 0;
19426 }
19427 lh->statement_program_end = start_here + lh->total_length;
19428 lh->version = read_2_bytes (abfd, line_ptr);
19429 line_ptr += 2;
19430 if (lh->version > 5)
19431 {
19432 /* This is a version we don't understand. The format could have
19433 changed in ways we don't handle properly so just punt. */
19434 complaint (_("unsupported version in .debug_line section"));
19435 return NULL;
19436 }
19437 if (lh->version >= 5)
19438 {
19439 gdb_byte segment_selector_size;
19440
19441 /* Skip address size. */
19442 read_1_byte (abfd, line_ptr);
19443 line_ptr += 1;
19444
19445 segment_selector_size = read_1_byte (abfd, line_ptr);
19446 line_ptr += 1;
19447 if (segment_selector_size != 0)
19448 {
19449 complaint (_("unsupported segment selector size %u "
19450 "in .debug_line section"),
19451 segment_selector_size);
19452 return NULL;
19453 }
19454 }
19455 lh->header_length = read_offset (abfd, line_ptr, offset_size);
19456 line_ptr += offset_size;
19457 lh->statement_program_start = line_ptr + lh->header_length;
19458 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
19459 line_ptr += 1;
19460 if (lh->version >= 4)
19461 {
19462 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
19463 line_ptr += 1;
19464 }
19465 else
19466 lh->maximum_ops_per_instruction = 1;
19467
19468 if (lh->maximum_ops_per_instruction == 0)
19469 {
19470 lh->maximum_ops_per_instruction = 1;
19471 complaint (_("invalid maximum_ops_per_instruction "
19472 "in `.debug_line' section"));
19473 }
19474
19475 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
19476 line_ptr += 1;
19477 lh->line_base = read_1_signed_byte (abfd, line_ptr);
19478 line_ptr += 1;
19479 lh->line_range = read_1_byte (abfd, line_ptr);
19480 line_ptr += 1;
19481 lh->opcode_base = read_1_byte (abfd, line_ptr);
19482 line_ptr += 1;
19483 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
19484
19485 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
19486 for (i = 1; i < lh->opcode_base; ++i)
19487 {
19488 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
19489 line_ptr += 1;
19490 }
19491
19492 if (lh->version >= 5)
19493 {
19494 /* Read directory table. */
19495 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
19496 &cu->header,
19497 [] (struct line_header *header, const char *name,
19498 dir_index d_index, unsigned int mod_time,
19499 unsigned int length)
19500 {
19501 header->add_include_dir (name);
19502 });
19503
19504 /* Read file name table. */
19505 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
19506 &cu->header,
19507 [] (struct line_header *header, const char *name,
19508 dir_index d_index, unsigned int mod_time,
19509 unsigned int length)
19510 {
19511 header->add_file_name (name, d_index, mod_time, length);
19512 });
19513 }
19514 else
19515 {
19516 /* Read directory table. */
19517 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
19518 {
19519 line_ptr += bytes_read;
19520 lh->add_include_dir (cur_dir);
19521 }
19522 line_ptr += bytes_read;
19523
19524 /* Read file name table. */
19525 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
19526 {
19527 unsigned int mod_time, length;
19528 dir_index d_index;
19529
19530 line_ptr += bytes_read;
19531 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19532 line_ptr += bytes_read;
19533 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19534 line_ptr += bytes_read;
19535 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19536 line_ptr += bytes_read;
19537
19538 lh->add_file_name (cur_file, d_index, mod_time, length);
19539 }
19540 line_ptr += bytes_read;
19541 }
19542
19543 if (line_ptr > (section->buffer + section->size))
19544 complaint (_("line number info header doesn't "
19545 "fit in `.debug_line' section"));
19546
19547 return lh;
19548 }
19549
19550 /* Subroutine of dwarf_decode_lines to simplify it.
19551 Return the file name of the psymtab for the given file_entry.
19552 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19553 If space for the result is malloc'd, *NAME_HOLDER will be set.
19554 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
19555
19556 static const char *
19557 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
19558 const dwarf2_psymtab *pst,
19559 const char *comp_dir,
19560 gdb::unique_xmalloc_ptr<char> *name_holder)
19561 {
19562 const char *include_name = fe.name;
19563 const char *include_name_to_compare = include_name;
19564 const char *pst_filename;
19565 int file_is_pst;
19566
19567 const char *dir_name = fe.include_dir (lh);
19568
19569 gdb::unique_xmalloc_ptr<char> hold_compare;
19570 if (!IS_ABSOLUTE_PATH (include_name)
19571 && (dir_name != NULL || comp_dir != NULL))
19572 {
19573 /* Avoid creating a duplicate psymtab for PST.
19574 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19575 Before we do the comparison, however, we need to account
19576 for DIR_NAME and COMP_DIR.
19577 First prepend dir_name (if non-NULL). If we still don't
19578 have an absolute path prepend comp_dir (if non-NULL).
19579 However, the directory we record in the include-file's
19580 psymtab does not contain COMP_DIR (to match the
19581 corresponding symtab(s)).
19582
19583 Example:
19584
19585 bash$ cd /tmp
19586 bash$ gcc -g ./hello.c
19587 include_name = "hello.c"
19588 dir_name = "."
19589 DW_AT_comp_dir = comp_dir = "/tmp"
19590 DW_AT_name = "./hello.c"
19591
19592 */
19593
19594 if (dir_name != NULL)
19595 {
19596 name_holder->reset (concat (dir_name, SLASH_STRING,
19597 include_name, (char *) NULL));
19598 include_name = name_holder->get ();
19599 include_name_to_compare = include_name;
19600 }
19601 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19602 {
19603 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19604 include_name, (char *) NULL));
19605 include_name_to_compare = hold_compare.get ();
19606 }
19607 }
19608
19609 pst_filename = pst->filename;
19610 gdb::unique_xmalloc_ptr<char> copied_name;
19611 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19612 {
19613 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19614 pst_filename, (char *) NULL));
19615 pst_filename = copied_name.get ();
19616 }
19617
19618 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
19619
19620 if (file_is_pst)
19621 return NULL;
19622 return include_name;
19623 }
19624
19625 /* State machine to track the state of the line number program. */
19626
19627 class lnp_state_machine
19628 {
19629 public:
19630 /* Initialize a machine state for the start of a line number
19631 program. */
19632 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19633 bool record_lines_p);
19634
19635 file_entry *current_file ()
19636 {
19637 /* lh->file_names is 0-based, but the file name numbers in the
19638 statement program are 1-based. */
19639 return m_line_header->file_name_at (m_file);
19640 }
19641
19642 /* Record the line in the state machine. END_SEQUENCE is true if
19643 we're processing the end of a sequence. */
19644 void record_line (bool end_sequence);
19645
19646 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19647 nop-out rest of the lines in this sequence. */
19648 void check_line_address (struct dwarf2_cu *cu,
19649 const gdb_byte *line_ptr,
19650 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19651
19652 void handle_set_discriminator (unsigned int discriminator)
19653 {
19654 m_discriminator = discriminator;
19655 m_line_has_non_zero_discriminator |= discriminator != 0;
19656 }
19657
19658 /* Handle DW_LNE_set_address. */
19659 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19660 {
19661 m_op_index = 0;
19662 address += baseaddr;
19663 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19664 }
19665
19666 /* Handle DW_LNS_advance_pc. */
19667 void handle_advance_pc (CORE_ADDR adjust);
19668
19669 /* Handle a special opcode. */
19670 void handle_special_opcode (unsigned char op_code);
19671
19672 /* Handle DW_LNS_advance_line. */
19673 void handle_advance_line (int line_delta)
19674 {
19675 advance_line (line_delta);
19676 }
19677
19678 /* Handle DW_LNS_set_file. */
19679 void handle_set_file (file_name_index file);
19680
19681 /* Handle DW_LNS_negate_stmt. */
19682 void handle_negate_stmt ()
19683 {
19684 m_is_stmt = !m_is_stmt;
19685 }
19686
19687 /* Handle DW_LNS_const_add_pc. */
19688 void handle_const_add_pc ();
19689
19690 /* Handle DW_LNS_fixed_advance_pc. */
19691 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19692 {
19693 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19694 m_op_index = 0;
19695 }
19696
19697 /* Handle DW_LNS_copy. */
19698 void handle_copy ()
19699 {
19700 record_line (false);
19701 m_discriminator = 0;
19702 }
19703
19704 /* Handle DW_LNE_end_sequence. */
19705 void handle_end_sequence ()
19706 {
19707 m_currently_recording_lines = true;
19708 }
19709
19710 private:
19711 /* Advance the line by LINE_DELTA. */
19712 void advance_line (int line_delta)
19713 {
19714 m_line += line_delta;
19715
19716 if (line_delta != 0)
19717 m_line_has_non_zero_discriminator = m_discriminator != 0;
19718 }
19719
19720 struct dwarf2_cu *m_cu;
19721
19722 gdbarch *m_gdbarch;
19723
19724 /* True if we're recording lines.
19725 Otherwise we're building partial symtabs and are just interested in
19726 finding include files mentioned by the line number program. */
19727 bool m_record_lines_p;
19728
19729 /* The line number header. */
19730 line_header *m_line_header;
19731
19732 /* These are part of the standard DWARF line number state machine,
19733 and initialized according to the DWARF spec. */
19734
19735 unsigned char m_op_index = 0;
19736 /* The line table index of the current file. */
19737 file_name_index m_file = 1;
19738 unsigned int m_line = 1;
19739
19740 /* These are initialized in the constructor. */
19741
19742 CORE_ADDR m_address;
19743 bool m_is_stmt;
19744 unsigned int m_discriminator;
19745
19746 /* Additional bits of state we need to track. */
19747
19748 /* The last file that we called dwarf2_start_subfile for.
19749 This is only used for TLLs. */
19750 unsigned int m_last_file = 0;
19751 /* The last file a line number was recorded for. */
19752 struct subfile *m_last_subfile = NULL;
19753
19754 /* When true, record the lines we decode. */
19755 bool m_currently_recording_lines = false;
19756
19757 /* The last line number that was recorded, used to coalesce
19758 consecutive entries for the same line. This can happen, for
19759 example, when discriminators are present. PR 17276. */
19760 unsigned int m_last_line = 0;
19761 bool m_line_has_non_zero_discriminator = false;
19762 };
19763
19764 void
19765 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19766 {
19767 CORE_ADDR addr_adj = (((m_op_index + adjust)
19768 / m_line_header->maximum_ops_per_instruction)
19769 * m_line_header->minimum_instruction_length);
19770 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19771 m_op_index = ((m_op_index + adjust)
19772 % m_line_header->maximum_ops_per_instruction);
19773 }
19774
19775 void
19776 lnp_state_machine::handle_special_opcode (unsigned char op_code)
19777 {
19778 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
19779 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
19780 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
19781 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
19782 / m_line_header->maximum_ops_per_instruction)
19783 * m_line_header->minimum_instruction_length);
19784 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19785 m_op_index = ((m_op_index + adj_opcode_d)
19786 % m_line_header->maximum_ops_per_instruction);
19787
19788 int line_delta = m_line_header->line_base + adj_opcode_r;
19789 advance_line (line_delta);
19790 record_line (false);
19791 m_discriminator = 0;
19792 }
19793
19794 void
19795 lnp_state_machine::handle_set_file (file_name_index file)
19796 {
19797 m_file = file;
19798
19799 const file_entry *fe = current_file ();
19800 if (fe == NULL)
19801 dwarf2_debug_line_missing_file_complaint ();
19802 else if (m_record_lines_p)
19803 {
19804 const char *dir = fe->include_dir (m_line_header);
19805
19806 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
19807 m_line_has_non_zero_discriminator = m_discriminator != 0;
19808 dwarf2_start_subfile (m_cu, fe->name, dir);
19809 }
19810 }
19811
19812 void
19813 lnp_state_machine::handle_const_add_pc ()
19814 {
19815 CORE_ADDR adjust
19816 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
19817
19818 CORE_ADDR addr_adj
19819 = (((m_op_index + adjust)
19820 / m_line_header->maximum_ops_per_instruction)
19821 * m_line_header->minimum_instruction_length);
19822
19823 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19824 m_op_index = ((m_op_index + adjust)
19825 % m_line_header->maximum_ops_per_instruction);
19826 }
19827
19828 /* Return non-zero if we should add LINE to the line number table.
19829 LINE is the line to add, LAST_LINE is the last line that was added,
19830 LAST_SUBFILE is the subfile for LAST_LINE.
19831 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19832 had a non-zero discriminator.
19833
19834 We have to be careful in the presence of discriminators.
19835 E.g., for this line:
19836
19837 for (i = 0; i < 100000; i++);
19838
19839 clang can emit four line number entries for that one line,
19840 each with a different discriminator.
19841 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19842
19843 However, we want gdb to coalesce all four entries into one.
19844 Otherwise the user could stepi into the middle of the line and
19845 gdb would get confused about whether the pc really was in the
19846 middle of the line.
19847
19848 Things are further complicated by the fact that two consecutive
19849 line number entries for the same line is a heuristic used by gcc
19850 to denote the end of the prologue. So we can't just discard duplicate
19851 entries, we have to be selective about it. The heuristic we use is
19852 that we only collapse consecutive entries for the same line if at least
19853 one of those entries has a non-zero discriminator. PR 17276.
19854
19855 Note: Addresses in the line number state machine can never go backwards
19856 within one sequence, thus this coalescing is ok. */
19857
19858 static int
19859 dwarf_record_line_p (struct dwarf2_cu *cu,
19860 unsigned int line, unsigned int last_line,
19861 int line_has_non_zero_discriminator,
19862 struct subfile *last_subfile)
19863 {
19864 if (cu->get_builder ()->get_current_subfile () != last_subfile)
19865 return 1;
19866 if (line != last_line)
19867 return 1;
19868 /* Same line for the same file that we've seen already.
19869 As a last check, for pr 17276, only record the line if the line
19870 has never had a non-zero discriminator. */
19871 if (!line_has_non_zero_discriminator)
19872 return 1;
19873 return 0;
19874 }
19875
19876 /* Use the CU's builder to record line number LINE beginning at
19877 address ADDRESS in the line table of subfile SUBFILE. */
19878
19879 static void
19880 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
19881 unsigned int line, CORE_ADDR address, bool is_stmt,
19882 struct dwarf2_cu *cu)
19883 {
19884 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
19885
19886 if (dwarf_line_debug)
19887 {
19888 fprintf_unfiltered (gdb_stdlog,
19889 "Recording line %u, file %s, address %s\n",
19890 line, lbasename (subfile->name),
19891 paddress (gdbarch, address));
19892 }
19893
19894 if (cu != nullptr)
19895 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
19896 }
19897
19898 /* Subroutine of dwarf_decode_lines_1 to simplify it.
19899 Mark the end of a set of line number records.
19900 The arguments are the same as for dwarf_record_line_1.
19901 If SUBFILE is NULL the request is ignored. */
19902
19903 static void
19904 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
19905 CORE_ADDR address, struct dwarf2_cu *cu)
19906 {
19907 if (subfile == NULL)
19908 return;
19909
19910 if (dwarf_line_debug)
19911 {
19912 fprintf_unfiltered (gdb_stdlog,
19913 "Finishing current line, file %s, address %s\n",
19914 lbasename (subfile->name),
19915 paddress (gdbarch, address));
19916 }
19917
19918 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
19919 }
19920
19921 void
19922 lnp_state_machine::record_line (bool end_sequence)
19923 {
19924 if (dwarf_line_debug)
19925 {
19926 fprintf_unfiltered (gdb_stdlog,
19927 "Processing actual line %u: file %u,"
19928 " address %s, is_stmt %u, discrim %u%s\n",
19929 m_line, m_file,
19930 paddress (m_gdbarch, m_address),
19931 m_is_stmt, m_discriminator,
19932 (end_sequence ? "\t(end sequence)" : ""));
19933 }
19934
19935 file_entry *fe = current_file ();
19936
19937 if (fe == NULL)
19938 dwarf2_debug_line_missing_file_complaint ();
19939 /* For now we ignore lines not starting on an instruction boundary.
19940 But not when processing end_sequence for compatibility with the
19941 previous version of the code. */
19942 else if (m_op_index == 0 || end_sequence)
19943 {
19944 fe->included_p = 1;
19945 if (m_record_lines_p)
19946 {
19947 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
19948 || end_sequence)
19949 {
19950 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
19951 m_currently_recording_lines ? m_cu : nullptr);
19952 }
19953
19954 if (!end_sequence)
19955 {
19956 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
19957
19958 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
19959 m_line_has_non_zero_discriminator,
19960 m_last_subfile))
19961 {
19962 buildsym_compunit *builder = m_cu->get_builder ();
19963 dwarf_record_line_1 (m_gdbarch,
19964 builder->get_current_subfile (),
19965 m_line, m_address, is_stmt,
19966 m_currently_recording_lines ? m_cu : nullptr);
19967 }
19968 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
19969 m_last_line = m_line;
19970 }
19971 }
19972 }
19973 }
19974
19975 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
19976 line_header *lh, bool record_lines_p)
19977 {
19978 m_cu = cu;
19979 m_gdbarch = arch;
19980 m_record_lines_p = record_lines_p;
19981 m_line_header = lh;
19982
19983 m_currently_recording_lines = true;
19984
19985 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
19986 was a line entry for it so that the backend has a chance to adjust it
19987 and also record it in case it needs it. This is currently used by MIPS
19988 code, cf. `mips_adjust_dwarf2_line'. */
19989 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
19990 m_is_stmt = lh->default_is_stmt;
19991 m_discriminator = 0;
19992 }
19993
19994 void
19995 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
19996 const gdb_byte *line_ptr,
19997 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
19998 {
19999 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20000 the pc range of the CU. However, we restrict the test to only ADDRESS
20001 values of zero to preserve GDB's previous behaviour which is to handle
20002 the specific case of a function being GC'd by the linker. */
20003
20004 if (address == 0 && address < unrelocated_lowpc)
20005 {
20006 /* This line table is for a function which has been
20007 GCd by the linker. Ignore it. PR gdb/12528 */
20008
20009 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20010 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20011
20012 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20013 line_offset, objfile_name (objfile));
20014 m_currently_recording_lines = false;
20015 /* Note: m_currently_recording_lines is left as false until we see
20016 DW_LNE_end_sequence. */
20017 }
20018 }
20019
20020 /* Subroutine of dwarf_decode_lines to simplify it.
20021 Process the line number information in LH.
20022 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20023 program in order to set included_p for every referenced header. */
20024
20025 static void
20026 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20027 const int decode_for_pst_p, CORE_ADDR lowpc)
20028 {
20029 const gdb_byte *line_ptr, *extended_end;
20030 const gdb_byte *line_end;
20031 unsigned int bytes_read, extended_len;
20032 unsigned char op_code, extended_op;
20033 CORE_ADDR baseaddr;
20034 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20035 bfd *abfd = objfile->obfd;
20036 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20037 /* True if we're recording line info (as opposed to building partial
20038 symtabs and just interested in finding include files mentioned by
20039 the line number program). */
20040 bool record_lines_p = !decode_for_pst_p;
20041
20042 baseaddr = objfile->text_section_offset ();
20043
20044 line_ptr = lh->statement_program_start;
20045 line_end = lh->statement_program_end;
20046
20047 /* Read the statement sequences until there's nothing left. */
20048 while (line_ptr < line_end)
20049 {
20050 /* The DWARF line number program state machine. Reset the state
20051 machine at the start of each sequence. */
20052 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
20053 bool end_sequence = false;
20054
20055 if (record_lines_p)
20056 {
20057 /* Start a subfile for the current file of the state
20058 machine. */
20059 const file_entry *fe = state_machine.current_file ();
20060
20061 if (fe != NULL)
20062 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
20063 }
20064
20065 /* Decode the table. */
20066 while (line_ptr < line_end && !end_sequence)
20067 {
20068 op_code = read_1_byte (abfd, line_ptr);
20069 line_ptr += 1;
20070
20071 if (op_code >= lh->opcode_base)
20072 {
20073 /* Special opcode. */
20074 state_machine.handle_special_opcode (op_code);
20075 }
20076 else switch (op_code)
20077 {
20078 case DW_LNS_extended_op:
20079 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20080 &bytes_read);
20081 line_ptr += bytes_read;
20082 extended_end = line_ptr + extended_len;
20083 extended_op = read_1_byte (abfd, line_ptr);
20084 line_ptr += 1;
20085 switch (extended_op)
20086 {
20087 case DW_LNE_end_sequence:
20088 state_machine.handle_end_sequence ();
20089 end_sequence = true;
20090 break;
20091 case DW_LNE_set_address:
20092 {
20093 CORE_ADDR address
20094 = cu->header.read_address (abfd, line_ptr, &bytes_read);
20095 line_ptr += bytes_read;
20096
20097 state_machine.check_line_address (cu, line_ptr,
20098 lowpc - baseaddr, address);
20099 state_machine.handle_set_address (baseaddr, address);
20100 }
20101 break;
20102 case DW_LNE_define_file:
20103 {
20104 const char *cur_file;
20105 unsigned int mod_time, length;
20106 dir_index dindex;
20107
20108 cur_file = read_direct_string (abfd, line_ptr,
20109 &bytes_read);
20110 line_ptr += bytes_read;
20111 dindex = (dir_index)
20112 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20113 line_ptr += bytes_read;
20114 mod_time =
20115 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20116 line_ptr += bytes_read;
20117 length =
20118 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20119 line_ptr += bytes_read;
20120 lh->add_file_name (cur_file, dindex, mod_time, length);
20121 }
20122 break;
20123 case DW_LNE_set_discriminator:
20124 {
20125 /* The discriminator is not interesting to the
20126 debugger; just ignore it. We still need to
20127 check its value though:
20128 if there are consecutive entries for the same
20129 (non-prologue) line we want to coalesce them.
20130 PR 17276. */
20131 unsigned int discr
20132 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20133 line_ptr += bytes_read;
20134
20135 state_machine.handle_set_discriminator (discr);
20136 }
20137 break;
20138 default:
20139 complaint (_("mangled .debug_line section"));
20140 return;
20141 }
20142 /* Make sure that we parsed the extended op correctly. If e.g.
20143 we expected a different address size than the producer used,
20144 we may have read the wrong number of bytes. */
20145 if (line_ptr != extended_end)
20146 {
20147 complaint (_("mangled .debug_line section"));
20148 return;
20149 }
20150 break;
20151 case DW_LNS_copy:
20152 state_machine.handle_copy ();
20153 break;
20154 case DW_LNS_advance_pc:
20155 {
20156 CORE_ADDR adjust
20157 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20158 line_ptr += bytes_read;
20159
20160 state_machine.handle_advance_pc (adjust);
20161 }
20162 break;
20163 case DW_LNS_advance_line:
20164 {
20165 int line_delta
20166 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20167 line_ptr += bytes_read;
20168
20169 state_machine.handle_advance_line (line_delta);
20170 }
20171 break;
20172 case DW_LNS_set_file:
20173 {
20174 file_name_index file
20175 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20176 &bytes_read);
20177 line_ptr += bytes_read;
20178
20179 state_machine.handle_set_file (file);
20180 }
20181 break;
20182 case DW_LNS_set_column:
20183 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20184 line_ptr += bytes_read;
20185 break;
20186 case DW_LNS_negate_stmt:
20187 state_machine.handle_negate_stmt ();
20188 break;
20189 case DW_LNS_set_basic_block:
20190 break;
20191 /* Add to the address register of the state machine the
20192 address increment value corresponding to special opcode
20193 255. I.e., this value is scaled by the minimum
20194 instruction length since special opcode 255 would have
20195 scaled the increment. */
20196 case DW_LNS_const_add_pc:
20197 state_machine.handle_const_add_pc ();
20198 break;
20199 case DW_LNS_fixed_advance_pc:
20200 {
20201 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20202 line_ptr += 2;
20203
20204 state_machine.handle_fixed_advance_pc (addr_adj);
20205 }
20206 break;
20207 default:
20208 {
20209 /* Unknown standard opcode, ignore it. */
20210 int i;
20211
20212 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20213 {
20214 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20215 line_ptr += bytes_read;
20216 }
20217 }
20218 }
20219 }
20220
20221 if (!end_sequence)
20222 dwarf2_debug_line_missing_end_sequence_complaint ();
20223
20224 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20225 in which case we still finish recording the last line). */
20226 state_machine.record_line (true);
20227 }
20228 }
20229
20230 /* Decode the Line Number Program (LNP) for the given line_header
20231 structure and CU. The actual information extracted and the type
20232 of structures created from the LNP depends on the value of PST.
20233
20234 1. If PST is NULL, then this procedure uses the data from the program
20235 to create all necessary symbol tables, and their linetables.
20236
20237 2. If PST is not NULL, this procedure reads the program to determine
20238 the list of files included by the unit represented by PST, and
20239 builds all the associated partial symbol tables.
20240
20241 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20242 It is used for relative paths in the line table.
20243 NOTE: When processing partial symtabs (pst != NULL),
20244 comp_dir == pst->dirname.
20245
20246 NOTE: It is important that psymtabs have the same file name (via strcmp)
20247 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20248 symtab we don't use it in the name of the psymtabs we create.
20249 E.g. expand_line_sal requires this when finding psymtabs to expand.
20250 A good testcase for this is mb-inline.exp.
20251
20252 LOWPC is the lowest address in CU (or 0 if not known).
20253
20254 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20255 for its PC<->lines mapping information. Otherwise only the filename
20256 table is read in. */
20257
20258 static void
20259 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
20260 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
20261 CORE_ADDR lowpc, int decode_mapping)
20262 {
20263 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20264 const int decode_for_pst_p = (pst != NULL);
20265
20266 if (decode_mapping)
20267 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
20268
20269 if (decode_for_pst_p)
20270 {
20271 /* Now that we're done scanning the Line Header Program, we can
20272 create the psymtab of each included file. */
20273 for (auto &file_entry : lh->file_names ())
20274 if (file_entry.included_p == 1)
20275 {
20276 gdb::unique_xmalloc_ptr<char> name_holder;
20277 const char *include_name =
20278 psymtab_include_file_name (lh, file_entry, pst,
20279 comp_dir, &name_holder);
20280 if (include_name != NULL)
20281 dwarf2_create_include_psymtab (include_name, pst, objfile);
20282 }
20283 }
20284 else
20285 {
20286 /* Make sure a symtab is created for every file, even files
20287 which contain only variables (i.e. no code with associated
20288 line numbers). */
20289 buildsym_compunit *builder = cu->get_builder ();
20290 struct compunit_symtab *cust = builder->get_compunit_symtab ();
20291
20292 for (auto &fe : lh->file_names ())
20293 {
20294 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
20295 if (builder->get_current_subfile ()->symtab == NULL)
20296 {
20297 builder->get_current_subfile ()->symtab
20298 = allocate_symtab (cust,
20299 builder->get_current_subfile ()->name);
20300 }
20301 fe.symtab = builder->get_current_subfile ()->symtab;
20302 }
20303 }
20304 }
20305
20306 /* Start a subfile for DWARF. FILENAME is the name of the file and
20307 DIRNAME the name of the source directory which contains FILENAME
20308 or NULL if not known.
20309 This routine tries to keep line numbers from identical absolute and
20310 relative file names in a common subfile.
20311
20312 Using the `list' example from the GDB testsuite, which resides in
20313 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20314 of /srcdir/list0.c yields the following debugging information for list0.c:
20315
20316 DW_AT_name: /srcdir/list0.c
20317 DW_AT_comp_dir: /compdir
20318 files.files[0].name: list0.h
20319 files.files[0].dir: /srcdir
20320 files.files[1].name: list0.c
20321 files.files[1].dir: /srcdir
20322
20323 The line number information for list0.c has to end up in a single
20324 subfile, so that `break /srcdir/list0.c:1' works as expected.
20325 start_subfile will ensure that this happens provided that we pass the
20326 concatenation of files.files[1].dir and files.files[1].name as the
20327 subfile's name. */
20328
20329 static void
20330 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20331 const char *dirname)
20332 {
20333 gdb::unique_xmalloc_ptr<char> copy;
20334
20335 /* In order not to lose the line information directory,
20336 we concatenate it to the filename when it makes sense.
20337 Note that the Dwarf3 standard says (speaking of filenames in line
20338 information): ``The directory index is ignored for file names
20339 that represent full path names''. Thus ignoring dirname in the
20340 `else' branch below isn't an issue. */
20341
20342 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
20343 {
20344 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20345 filename = copy.get ();
20346 }
20347
20348 cu->get_builder ()->start_subfile (filename);
20349 }
20350
20351 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20352 buildsym_compunit constructor. */
20353
20354 struct compunit_symtab *
20355 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20356 CORE_ADDR low_pc)
20357 {
20358 gdb_assert (m_builder == nullptr);
20359
20360 m_builder.reset (new struct buildsym_compunit
20361 (per_cu->dwarf2_per_objfile->objfile,
20362 name, comp_dir, language, low_pc));
20363
20364 list_in_scope = get_builder ()->get_file_symbols ();
20365
20366 get_builder ()->record_debugformat ("DWARF 2");
20367 get_builder ()->record_producer (producer);
20368
20369 processing_has_namespace_info = false;
20370
20371 return get_builder ()->get_compunit_symtab ();
20372 }
20373
20374 static void
20375 var_decode_location (struct attribute *attr, struct symbol *sym,
20376 struct dwarf2_cu *cu)
20377 {
20378 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20379 struct comp_unit_head *cu_header = &cu->header;
20380
20381 /* NOTE drow/2003-01-30: There used to be a comment and some special
20382 code here to turn a symbol with DW_AT_external and a
20383 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20384 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20385 with some versions of binutils) where shared libraries could have
20386 relocations against symbols in their debug information - the
20387 minimal symbol would have the right address, but the debug info
20388 would not. It's no longer necessary, because we will explicitly
20389 apply relocations when we read in the debug information now. */
20390
20391 /* A DW_AT_location attribute with no contents indicates that a
20392 variable has been optimized away. */
20393 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
20394 {
20395 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20396 return;
20397 }
20398
20399 /* Handle one degenerate form of location expression specially, to
20400 preserve GDB's previous behavior when section offsets are
20401 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20402 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20403
20404 if (attr->form_is_block ()
20405 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20406 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
20407 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20408 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
20409 && (DW_BLOCK (attr)->size
20410 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
20411 {
20412 unsigned int dummy;
20413
20414 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
20415 SET_SYMBOL_VALUE_ADDRESS
20416 (sym, cu->header.read_address (objfile->obfd,
20417 DW_BLOCK (attr)->data + 1,
20418 &dummy));
20419 else
20420 SET_SYMBOL_VALUE_ADDRESS
20421 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20422 &dummy));
20423 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
20424 fixup_symbol_section (sym, objfile);
20425 SET_SYMBOL_VALUE_ADDRESS
20426 (sym,
20427 SYMBOL_VALUE_ADDRESS (sym)
20428 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
20429 return;
20430 }
20431
20432 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20433 expression evaluator, and use LOC_COMPUTED only when necessary
20434 (i.e. when the value of a register or memory location is
20435 referenced, or a thread-local block, etc.). Then again, it might
20436 not be worthwhile. I'm assuming that it isn't unless performance
20437 or memory numbers show me otherwise. */
20438
20439 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
20440
20441 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
20442 cu->has_loclist = true;
20443 }
20444
20445 /* Given a pointer to a DWARF information entry, figure out if we need
20446 to make a symbol table entry for it, and if so, create a new entry
20447 and return a pointer to it.
20448 If TYPE is NULL, determine symbol type from the die, otherwise
20449 used the passed type.
20450 If SPACE is not NULL, use it to hold the new symbol. If it is
20451 NULL, allocate a new symbol on the objfile's obstack. */
20452
20453 static struct symbol *
20454 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20455 struct symbol *space)
20456 {
20457 struct dwarf2_per_objfile *dwarf2_per_objfile
20458 = cu->per_cu->dwarf2_per_objfile;
20459 struct objfile *objfile = dwarf2_per_objfile->objfile;
20460 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20461 struct symbol *sym = NULL;
20462 const char *name;
20463 struct attribute *attr = NULL;
20464 struct attribute *attr2 = NULL;
20465 CORE_ADDR baseaddr;
20466 struct pending **list_to_add = NULL;
20467
20468 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20469
20470 baseaddr = objfile->text_section_offset ();
20471
20472 name = dwarf2_name (die, cu);
20473 if (name)
20474 {
20475 const char *linkagename;
20476 int suppress_add = 0;
20477
20478 if (space)
20479 sym = space;
20480 else
20481 sym = allocate_symbol (objfile);
20482 OBJSTAT (objfile, n_syms++);
20483
20484 /* Cache this symbol's name and the name's demangled form (if any). */
20485 sym->set_language (cu->language, &objfile->objfile_obstack);
20486 linkagename = dwarf2_physname (name, die, cu);
20487 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
20488
20489 /* Fortran does not have mangling standard and the mangling does differ
20490 between gfortran, iFort etc. */
20491 if (cu->language == language_fortran
20492 && symbol_get_demangled_name (sym) == NULL)
20493 symbol_set_demangled_name (sym,
20494 dwarf2_full_name (name, die, cu),
20495 NULL);
20496
20497 /* Default assumptions.
20498 Use the passed type or decode it from the die. */
20499 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20500 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20501 if (type != NULL)
20502 SYMBOL_TYPE (sym) = type;
20503 else
20504 SYMBOL_TYPE (sym) = die_type (die, cu);
20505 attr = dwarf2_attr (die,
20506 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20507 cu);
20508 if (attr != nullptr)
20509 {
20510 SYMBOL_LINE (sym) = DW_UNSND (attr);
20511 }
20512
20513 attr = dwarf2_attr (die,
20514 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20515 cu);
20516 if (attr != nullptr)
20517 {
20518 file_name_index file_index = (file_name_index) DW_UNSND (attr);
20519 struct file_entry *fe;
20520
20521 if (cu->line_header != NULL)
20522 fe = cu->line_header->file_name_at (file_index);
20523 else
20524 fe = NULL;
20525
20526 if (fe == NULL)
20527 complaint (_("file index out of range"));
20528 else
20529 symbol_set_symtab (sym, fe->symtab);
20530 }
20531
20532 switch (die->tag)
20533 {
20534 case DW_TAG_label:
20535 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20536 if (attr != nullptr)
20537 {
20538 CORE_ADDR addr;
20539
20540 addr = attr->value_as_address ();
20541 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20542 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
20543 }
20544 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20545 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
20546 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
20547 add_symbol_to_list (sym, cu->list_in_scope);
20548 break;
20549 case DW_TAG_subprogram:
20550 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20551 finish_block. */
20552 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20553 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20554 if ((attr2 && (DW_UNSND (attr2) != 0))
20555 || cu->language == language_ada
20556 || cu->language == language_fortran)
20557 {
20558 /* Subprograms marked external are stored as a global symbol.
20559 Ada and Fortran subprograms, whether marked external or
20560 not, are always stored as a global symbol, because we want
20561 to be able to access them globally. For instance, we want
20562 to be able to break on a nested subprogram without having
20563 to specify the context. */
20564 list_to_add = cu->get_builder ()->get_global_symbols ();
20565 }
20566 else
20567 {
20568 list_to_add = cu->list_in_scope;
20569 }
20570 break;
20571 case DW_TAG_inlined_subroutine:
20572 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20573 finish_block. */
20574 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20575 SYMBOL_INLINED (sym) = 1;
20576 list_to_add = cu->list_in_scope;
20577 break;
20578 case DW_TAG_template_value_param:
20579 suppress_add = 1;
20580 /* Fall through. */
20581 case DW_TAG_constant:
20582 case DW_TAG_variable:
20583 case DW_TAG_member:
20584 /* Compilation with minimal debug info may result in
20585 variables with missing type entries. Change the
20586 misleading `void' type to something sensible. */
20587 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
20588 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
20589
20590 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20591 /* In the case of DW_TAG_member, we should only be called for
20592 static const members. */
20593 if (die->tag == DW_TAG_member)
20594 {
20595 /* dwarf2_add_field uses die_is_declaration,
20596 so we do the same. */
20597 gdb_assert (die_is_declaration (die, cu));
20598 gdb_assert (attr);
20599 }
20600 if (attr != nullptr)
20601 {
20602 dwarf2_const_value (attr, sym, cu);
20603 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20604 if (!suppress_add)
20605 {
20606 if (attr2 && (DW_UNSND (attr2) != 0))
20607 list_to_add = cu->get_builder ()->get_global_symbols ();
20608 else
20609 list_to_add = cu->list_in_scope;
20610 }
20611 break;
20612 }
20613 attr = dwarf2_attr (die, DW_AT_location, cu);
20614 if (attr != nullptr)
20615 {
20616 var_decode_location (attr, sym, cu);
20617 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20618
20619 /* Fortran explicitly imports any global symbols to the local
20620 scope by DW_TAG_common_block. */
20621 if (cu->language == language_fortran && die->parent
20622 && die->parent->tag == DW_TAG_common_block)
20623 attr2 = NULL;
20624
20625 if (SYMBOL_CLASS (sym) == LOC_STATIC
20626 && SYMBOL_VALUE_ADDRESS (sym) == 0
20627 && !dwarf2_per_objfile->has_section_at_zero)
20628 {
20629 /* When a static variable is eliminated by the linker,
20630 the corresponding debug information is not stripped
20631 out, but the variable address is set to null;
20632 do not add such variables into symbol table. */
20633 }
20634 else if (attr2 && (DW_UNSND (attr2) != 0))
20635 {
20636 if (SYMBOL_CLASS (sym) == LOC_STATIC
20637 && (objfile->flags & OBJF_MAINLINE) == 0
20638 && dwarf2_per_objfile->can_copy)
20639 {
20640 /* A global static variable might be subject to
20641 copy relocation. We first check for a local
20642 minsym, though, because maybe the symbol was
20643 marked hidden, in which case this would not
20644 apply. */
20645 bound_minimal_symbol found
20646 = (lookup_minimal_symbol_linkage
20647 (sym->linkage_name (), objfile));
20648 if (found.minsym != nullptr)
20649 sym->maybe_copied = 1;
20650 }
20651
20652 /* A variable with DW_AT_external is never static,
20653 but it may be block-scoped. */
20654 list_to_add
20655 = ((cu->list_in_scope
20656 == cu->get_builder ()->get_file_symbols ())
20657 ? cu->get_builder ()->get_global_symbols ()
20658 : cu->list_in_scope);
20659 }
20660 else
20661 list_to_add = cu->list_in_scope;
20662 }
20663 else
20664 {
20665 /* We do not know the address of this symbol.
20666 If it is an external symbol and we have type information
20667 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20668 The address of the variable will then be determined from
20669 the minimal symbol table whenever the variable is
20670 referenced. */
20671 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20672
20673 /* Fortran explicitly imports any global symbols to the local
20674 scope by DW_TAG_common_block. */
20675 if (cu->language == language_fortran && die->parent
20676 && die->parent->tag == DW_TAG_common_block)
20677 {
20678 /* SYMBOL_CLASS doesn't matter here because
20679 read_common_block is going to reset it. */
20680 if (!suppress_add)
20681 list_to_add = cu->list_in_scope;
20682 }
20683 else if (attr2 && (DW_UNSND (attr2) != 0)
20684 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
20685 {
20686 /* A variable with DW_AT_external is never static, but it
20687 may be block-scoped. */
20688 list_to_add
20689 = ((cu->list_in_scope
20690 == cu->get_builder ()->get_file_symbols ())
20691 ? cu->get_builder ()->get_global_symbols ()
20692 : cu->list_in_scope);
20693
20694 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
20695 }
20696 else if (!die_is_declaration (die, cu))
20697 {
20698 /* Use the default LOC_OPTIMIZED_OUT class. */
20699 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
20700 if (!suppress_add)
20701 list_to_add = cu->list_in_scope;
20702 }
20703 }
20704 break;
20705 case DW_TAG_formal_parameter:
20706 {
20707 /* If we are inside a function, mark this as an argument. If
20708 not, we might be looking at an argument to an inlined function
20709 when we do not have enough information to show inlined frames;
20710 pretend it's a local variable in that case so that the user can
20711 still see it. */
20712 struct context_stack *curr
20713 = cu->get_builder ()->get_current_context_stack ();
20714 if (curr != nullptr && curr->name != nullptr)
20715 SYMBOL_IS_ARGUMENT (sym) = 1;
20716 attr = dwarf2_attr (die, DW_AT_location, cu);
20717 if (attr != nullptr)
20718 {
20719 var_decode_location (attr, sym, cu);
20720 }
20721 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20722 if (attr != nullptr)
20723 {
20724 dwarf2_const_value (attr, sym, cu);
20725 }
20726
20727 list_to_add = cu->list_in_scope;
20728 }
20729 break;
20730 case DW_TAG_unspecified_parameters:
20731 /* From varargs functions; gdb doesn't seem to have any
20732 interest in this information, so just ignore it for now.
20733 (FIXME?) */
20734 break;
20735 case DW_TAG_template_type_param:
20736 suppress_add = 1;
20737 /* Fall through. */
20738 case DW_TAG_class_type:
20739 case DW_TAG_interface_type:
20740 case DW_TAG_structure_type:
20741 case DW_TAG_union_type:
20742 case DW_TAG_set_type:
20743 case DW_TAG_enumeration_type:
20744 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20745 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
20746
20747 {
20748 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
20749 really ever be static objects: otherwise, if you try
20750 to, say, break of a class's method and you're in a file
20751 which doesn't mention that class, it won't work unless
20752 the check for all static symbols in lookup_symbol_aux
20753 saves you. See the OtherFileClass tests in
20754 gdb.c++/namespace.exp. */
20755
20756 if (!suppress_add)
20757 {
20758 buildsym_compunit *builder = cu->get_builder ();
20759 list_to_add
20760 = (cu->list_in_scope == builder->get_file_symbols ()
20761 && cu->language == language_cplus
20762 ? builder->get_global_symbols ()
20763 : cu->list_in_scope);
20764
20765 /* The semantics of C++ state that "struct foo {
20766 ... }" also defines a typedef for "foo". */
20767 if (cu->language == language_cplus
20768 || cu->language == language_ada
20769 || cu->language == language_d
20770 || cu->language == language_rust)
20771 {
20772 /* The symbol's name is already allocated along
20773 with this objfile, so we don't need to
20774 duplicate it for the type. */
20775 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
20776 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
20777 }
20778 }
20779 }
20780 break;
20781 case DW_TAG_typedef:
20782 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20783 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20784 list_to_add = cu->list_in_scope;
20785 break;
20786 case DW_TAG_base_type:
20787 case DW_TAG_subrange_type:
20788 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20789 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20790 list_to_add = cu->list_in_scope;
20791 break;
20792 case DW_TAG_enumerator:
20793 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20794 if (attr != nullptr)
20795 {
20796 dwarf2_const_value (attr, sym, cu);
20797 }
20798 {
20799 /* NOTE: carlton/2003-11-10: See comment above in the
20800 DW_TAG_class_type, etc. block. */
20801
20802 list_to_add
20803 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
20804 && cu->language == language_cplus
20805 ? cu->get_builder ()->get_global_symbols ()
20806 : cu->list_in_scope);
20807 }
20808 break;
20809 case DW_TAG_imported_declaration:
20810 case DW_TAG_namespace:
20811 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20812 list_to_add = cu->get_builder ()->get_global_symbols ();
20813 break;
20814 case DW_TAG_module:
20815 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20816 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
20817 list_to_add = cu->get_builder ()->get_global_symbols ();
20818 break;
20819 case DW_TAG_common_block:
20820 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
20821 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
20822 add_symbol_to_list (sym, cu->list_in_scope);
20823 break;
20824 default:
20825 /* Not a tag we recognize. Hopefully we aren't processing
20826 trash data, but since we must specifically ignore things
20827 we don't recognize, there is nothing else we should do at
20828 this point. */
20829 complaint (_("unsupported tag: '%s'"),
20830 dwarf_tag_name (die->tag));
20831 break;
20832 }
20833
20834 if (suppress_add)
20835 {
20836 sym->hash_next = objfile->template_symbols;
20837 objfile->template_symbols = sym;
20838 list_to_add = NULL;
20839 }
20840
20841 if (list_to_add != NULL)
20842 add_symbol_to_list (sym, list_to_add);
20843
20844 /* For the benefit of old versions of GCC, check for anonymous
20845 namespaces based on the demangled name. */
20846 if (!cu->processing_has_namespace_info
20847 && cu->language == language_cplus)
20848 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
20849 }
20850 return (sym);
20851 }
20852
20853 /* Given an attr with a DW_FORM_dataN value in host byte order,
20854 zero-extend it as appropriate for the symbol's type. The DWARF
20855 standard (v4) is not entirely clear about the meaning of using
20856 DW_FORM_dataN for a constant with a signed type, where the type is
20857 wider than the data. The conclusion of a discussion on the DWARF
20858 list was that this is unspecified. We choose to always zero-extend
20859 because that is the interpretation long in use by GCC. */
20860
20861 static gdb_byte *
20862 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
20863 struct dwarf2_cu *cu, LONGEST *value, int bits)
20864 {
20865 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20866 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
20867 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
20868 LONGEST l = DW_UNSND (attr);
20869
20870 if (bits < sizeof (*value) * 8)
20871 {
20872 l &= ((LONGEST) 1 << bits) - 1;
20873 *value = l;
20874 }
20875 else if (bits == sizeof (*value) * 8)
20876 *value = l;
20877 else
20878 {
20879 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
20880 store_unsigned_integer (bytes, bits / 8, byte_order, l);
20881 return bytes;
20882 }
20883
20884 return NULL;
20885 }
20886
20887 /* Read a constant value from an attribute. Either set *VALUE, or if
20888 the value does not fit in *VALUE, set *BYTES - either already
20889 allocated on the objfile obstack, or newly allocated on OBSTACK,
20890 or, set *BATON, if we translated the constant to a location
20891 expression. */
20892
20893 static void
20894 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
20895 const char *name, struct obstack *obstack,
20896 struct dwarf2_cu *cu,
20897 LONGEST *value, const gdb_byte **bytes,
20898 struct dwarf2_locexpr_baton **baton)
20899 {
20900 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20901 struct comp_unit_head *cu_header = &cu->header;
20902 struct dwarf_block *blk;
20903 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
20904 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20905
20906 *value = 0;
20907 *bytes = NULL;
20908 *baton = NULL;
20909
20910 switch (attr->form)
20911 {
20912 case DW_FORM_addr:
20913 case DW_FORM_addrx:
20914 case DW_FORM_GNU_addr_index:
20915 {
20916 gdb_byte *data;
20917
20918 if (TYPE_LENGTH (type) != cu_header->addr_size)
20919 dwarf2_const_value_length_mismatch_complaint (name,
20920 cu_header->addr_size,
20921 TYPE_LENGTH (type));
20922 /* Symbols of this form are reasonably rare, so we just
20923 piggyback on the existing location code rather than writing
20924 a new implementation of symbol_computed_ops. */
20925 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
20926 (*baton)->per_cu = cu->per_cu;
20927 gdb_assert ((*baton)->per_cu);
20928
20929 (*baton)->size = 2 + cu_header->addr_size;
20930 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
20931 (*baton)->data = data;
20932
20933 data[0] = DW_OP_addr;
20934 store_unsigned_integer (&data[1], cu_header->addr_size,
20935 byte_order, DW_ADDR (attr));
20936 data[cu_header->addr_size + 1] = DW_OP_stack_value;
20937 }
20938 break;
20939 case DW_FORM_string:
20940 case DW_FORM_strp:
20941 case DW_FORM_strx:
20942 case DW_FORM_GNU_str_index:
20943 case DW_FORM_GNU_strp_alt:
20944 /* DW_STRING is already allocated on the objfile obstack, point
20945 directly to it. */
20946 *bytes = (const gdb_byte *) DW_STRING (attr);
20947 break;
20948 case DW_FORM_block1:
20949 case DW_FORM_block2:
20950 case DW_FORM_block4:
20951 case DW_FORM_block:
20952 case DW_FORM_exprloc:
20953 case DW_FORM_data16:
20954 blk = DW_BLOCK (attr);
20955 if (TYPE_LENGTH (type) != blk->size)
20956 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
20957 TYPE_LENGTH (type));
20958 *bytes = blk->data;
20959 break;
20960
20961 /* The DW_AT_const_value attributes are supposed to carry the
20962 symbol's value "represented as it would be on the target
20963 architecture." By the time we get here, it's already been
20964 converted to host endianness, so we just need to sign- or
20965 zero-extend it as appropriate. */
20966 case DW_FORM_data1:
20967 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
20968 break;
20969 case DW_FORM_data2:
20970 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
20971 break;
20972 case DW_FORM_data4:
20973 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
20974 break;
20975 case DW_FORM_data8:
20976 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
20977 break;
20978
20979 case DW_FORM_sdata:
20980 case DW_FORM_implicit_const:
20981 *value = DW_SND (attr);
20982 break;
20983
20984 case DW_FORM_udata:
20985 *value = DW_UNSND (attr);
20986 break;
20987
20988 default:
20989 complaint (_("unsupported const value attribute form: '%s'"),
20990 dwarf_form_name (attr->form));
20991 *value = 0;
20992 break;
20993 }
20994 }
20995
20996
20997 /* Copy constant value from an attribute to a symbol. */
20998
20999 static void
21000 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21001 struct dwarf2_cu *cu)
21002 {
21003 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21004 LONGEST value;
21005 const gdb_byte *bytes;
21006 struct dwarf2_locexpr_baton *baton;
21007
21008 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21009 sym->print_name (),
21010 &objfile->objfile_obstack, cu,
21011 &value, &bytes, &baton);
21012
21013 if (baton != NULL)
21014 {
21015 SYMBOL_LOCATION_BATON (sym) = baton;
21016 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21017 }
21018 else if (bytes != NULL)
21019 {
21020 SYMBOL_VALUE_BYTES (sym) = bytes;
21021 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21022 }
21023 else
21024 {
21025 SYMBOL_VALUE (sym) = value;
21026 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21027 }
21028 }
21029
21030 /* Return the type of the die in question using its DW_AT_type attribute. */
21031
21032 static struct type *
21033 die_type (struct die_info *die, struct dwarf2_cu *cu)
21034 {
21035 struct attribute *type_attr;
21036
21037 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21038 if (!type_attr)
21039 {
21040 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21041 /* A missing DW_AT_type represents a void type. */
21042 return objfile_type (objfile)->builtin_void;
21043 }
21044
21045 return lookup_die_type (die, type_attr, cu);
21046 }
21047
21048 /* True iff CU's producer generates GNAT Ada auxiliary information
21049 that allows to find parallel types through that information instead
21050 of having to do expensive parallel lookups by type name. */
21051
21052 static int
21053 need_gnat_info (struct dwarf2_cu *cu)
21054 {
21055 /* Assume that the Ada compiler was GNAT, which always produces
21056 the auxiliary information. */
21057 return (cu->language == language_ada);
21058 }
21059
21060 /* Return the auxiliary type of the die in question using its
21061 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21062 attribute is not present. */
21063
21064 static struct type *
21065 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21066 {
21067 struct attribute *type_attr;
21068
21069 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21070 if (!type_attr)
21071 return NULL;
21072
21073 return lookup_die_type (die, type_attr, cu);
21074 }
21075
21076 /* If DIE has a descriptive_type attribute, then set the TYPE's
21077 descriptive type accordingly. */
21078
21079 static void
21080 set_descriptive_type (struct type *type, struct die_info *die,
21081 struct dwarf2_cu *cu)
21082 {
21083 struct type *descriptive_type = die_descriptive_type (die, cu);
21084
21085 if (descriptive_type)
21086 {
21087 ALLOCATE_GNAT_AUX_TYPE (type);
21088 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21089 }
21090 }
21091
21092 /* Return the containing type of the die in question using its
21093 DW_AT_containing_type attribute. */
21094
21095 static struct type *
21096 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21097 {
21098 struct attribute *type_attr;
21099 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21100
21101 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21102 if (!type_attr)
21103 error (_("Dwarf Error: Problem turning containing type into gdb type "
21104 "[in module %s]"), objfile_name (objfile));
21105
21106 return lookup_die_type (die, type_attr, cu);
21107 }
21108
21109 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21110
21111 static struct type *
21112 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21113 {
21114 struct dwarf2_per_objfile *dwarf2_per_objfile
21115 = cu->per_cu->dwarf2_per_objfile;
21116 struct objfile *objfile = dwarf2_per_objfile->objfile;
21117 char *saved;
21118
21119 std::string message
21120 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21121 objfile_name (objfile),
21122 sect_offset_str (cu->header.sect_off),
21123 sect_offset_str (die->sect_off));
21124 saved = obstack_strdup (&objfile->objfile_obstack, message);
21125
21126 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21127 }
21128
21129 /* Look up the type of DIE in CU using its type attribute ATTR.
21130 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21131 DW_AT_containing_type.
21132 If there is no type substitute an error marker. */
21133
21134 static struct type *
21135 lookup_die_type (struct die_info *die, const struct attribute *attr,
21136 struct dwarf2_cu *cu)
21137 {
21138 struct dwarf2_per_objfile *dwarf2_per_objfile
21139 = cu->per_cu->dwarf2_per_objfile;
21140 struct objfile *objfile = dwarf2_per_objfile->objfile;
21141 struct type *this_type;
21142
21143 gdb_assert (attr->name == DW_AT_type
21144 || attr->name == DW_AT_GNAT_descriptive_type
21145 || attr->name == DW_AT_containing_type);
21146
21147 /* First see if we have it cached. */
21148
21149 if (attr->form == DW_FORM_GNU_ref_alt)
21150 {
21151 struct dwarf2_per_cu_data *per_cu;
21152 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21153
21154 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21155 dwarf2_per_objfile);
21156 this_type = get_die_type_at_offset (sect_off, per_cu);
21157 }
21158 else if (attr->form_is_ref ())
21159 {
21160 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21161
21162 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
21163 }
21164 else if (attr->form == DW_FORM_ref_sig8)
21165 {
21166 ULONGEST signature = DW_SIGNATURE (attr);
21167
21168 return get_signatured_type (die, signature, cu);
21169 }
21170 else
21171 {
21172 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21173 " at %s [in module %s]"),
21174 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21175 objfile_name (objfile));
21176 return build_error_marker_type (cu, die);
21177 }
21178
21179 /* If not cached we need to read it in. */
21180
21181 if (this_type == NULL)
21182 {
21183 struct die_info *type_die = NULL;
21184 struct dwarf2_cu *type_cu = cu;
21185
21186 if (attr->form_is_ref ())
21187 type_die = follow_die_ref (die, attr, &type_cu);
21188 if (type_die == NULL)
21189 return build_error_marker_type (cu, die);
21190 /* If we find the type now, it's probably because the type came
21191 from an inter-CU reference and the type's CU got expanded before
21192 ours. */
21193 this_type = read_type_die (type_die, type_cu);
21194 }
21195
21196 /* If we still don't have a type use an error marker. */
21197
21198 if (this_type == NULL)
21199 return build_error_marker_type (cu, die);
21200
21201 return this_type;
21202 }
21203
21204 /* Return the type in DIE, CU.
21205 Returns NULL for invalid types.
21206
21207 This first does a lookup in die_type_hash,
21208 and only reads the die in if necessary.
21209
21210 NOTE: This can be called when reading in partial or full symbols. */
21211
21212 static struct type *
21213 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21214 {
21215 struct type *this_type;
21216
21217 this_type = get_die_type (die, cu);
21218 if (this_type)
21219 return this_type;
21220
21221 return read_type_die_1 (die, cu);
21222 }
21223
21224 /* Read the type in DIE, CU.
21225 Returns NULL for invalid types. */
21226
21227 static struct type *
21228 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21229 {
21230 struct type *this_type = NULL;
21231
21232 switch (die->tag)
21233 {
21234 case DW_TAG_class_type:
21235 case DW_TAG_interface_type:
21236 case DW_TAG_structure_type:
21237 case DW_TAG_union_type:
21238 this_type = read_structure_type (die, cu);
21239 break;
21240 case DW_TAG_enumeration_type:
21241 this_type = read_enumeration_type (die, cu);
21242 break;
21243 case DW_TAG_subprogram:
21244 case DW_TAG_subroutine_type:
21245 case DW_TAG_inlined_subroutine:
21246 this_type = read_subroutine_type (die, cu);
21247 break;
21248 case DW_TAG_array_type:
21249 this_type = read_array_type (die, cu);
21250 break;
21251 case DW_TAG_set_type:
21252 this_type = read_set_type (die, cu);
21253 break;
21254 case DW_TAG_pointer_type:
21255 this_type = read_tag_pointer_type (die, cu);
21256 break;
21257 case DW_TAG_ptr_to_member_type:
21258 this_type = read_tag_ptr_to_member_type (die, cu);
21259 break;
21260 case DW_TAG_reference_type:
21261 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21262 break;
21263 case DW_TAG_rvalue_reference_type:
21264 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21265 break;
21266 case DW_TAG_const_type:
21267 this_type = read_tag_const_type (die, cu);
21268 break;
21269 case DW_TAG_volatile_type:
21270 this_type = read_tag_volatile_type (die, cu);
21271 break;
21272 case DW_TAG_restrict_type:
21273 this_type = read_tag_restrict_type (die, cu);
21274 break;
21275 case DW_TAG_string_type:
21276 this_type = read_tag_string_type (die, cu);
21277 break;
21278 case DW_TAG_typedef:
21279 this_type = read_typedef (die, cu);
21280 break;
21281 case DW_TAG_subrange_type:
21282 this_type = read_subrange_type (die, cu);
21283 break;
21284 case DW_TAG_base_type:
21285 this_type = read_base_type (die, cu);
21286 break;
21287 case DW_TAG_unspecified_type:
21288 this_type = read_unspecified_type (die, cu);
21289 break;
21290 case DW_TAG_namespace:
21291 this_type = read_namespace_type (die, cu);
21292 break;
21293 case DW_TAG_module:
21294 this_type = read_module_type (die, cu);
21295 break;
21296 case DW_TAG_atomic_type:
21297 this_type = read_tag_atomic_type (die, cu);
21298 break;
21299 default:
21300 complaint (_("unexpected tag in read_type_die: '%s'"),
21301 dwarf_tag_name (die->tag));
21302 break;
21303 }
21304
21305 return this_type;
21306 }
21307
21308 /* See if we can figure out if the class lives in a namespace. We do
21309 this by looking for a member function; its demangled name will
21310 contain namespace info, if there is any.
21311 Return the computed name or NULL.
21312 Space for the result is allocated on the objfile's obstack.
21313 This is the full-die version of guess_partial_die_structure_name.
21314 In this case we know DIE has no useful parent. */
21315
21316 static const char *
21317 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21318 {
21319 struct die_info *spec_die;
21320 struct dwarf2_cu *spec_cu;
21321 struct die_info *child;
21322 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21323
21324 spec_cu = cu;
21325 spec_die = die_specification (die, &spec_cu);
21326 if (spec_die != NULL)
21327 {
21328 die = spec_die;
21329 cu = spec_cu;
21330 }
21331
21332 for (child = die->child;
21333 child != NULL;
21334 child = child->sibling)
21335 {
21336 if (child->tag == DW_TAG_subprogram)
21337 {
21338 const char *linkage_name = dw2_linkage_name (child, cu);
21339
21340 if (linkage_name != NULL)
21341 {
21342 gdb::unique_xmalloc_ptr<char> actual_name
21343 (language_class_name_from_physname (cu->language_defn,
21344 linkage_name));
21345 const char *name = NULL;
21346
21347 if (actual_name != NULL)
21348 {
21349 const char *die_name = dwarf2_name (die, cu);
21350
21351 if (die_name != NULL
21352 && strcmp (die_name, actual_name.get ()) != 0)
21353 {
21354 /* Strip off the class name from the full name.
21355 We want the prefix. */
21356 int die_name_len = strlen (die_name);
21357 int actual_name_len = strlen (actual_name.get ());
21358 const char *ptr = actual_name.get ();
21359
21360 /* Test for '::' as a sanity check. */
21361 if (actual_name_len > die_name_len + 2
21362 && ptr[actual_name_len - die_name_len - 1] == ':')
21363 name = obstack_strndup (
21364 &objfile->per_bfd->storage_obstack,
21365 ptr, actual_name_len - die_name_len - 2);
21366 }
21367 }
21368 return name;
21369 }
21370 }
21371 }
21372
21373 return NULL;
21374 }
21375
21376 /* GCC might emit a nameless typedef that has a linkage name. Determine the
21377 prefix part in such case. See
21378 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21379
21380 static const char *
21381 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21382 {
21383 struct attribute *attr;
21384 const char *base;
21385
21386 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21387 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21388 return NULL;
21389
21390 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
21391 return NULL;
21392
21393 attr = dw2_linkage_name_attr (die, cu);
21394 if (attr == NULL || DW_STRING (attr) == NULL)
21395 return NULL;
21396
21397 /* dwarf2_name had to be already called. */
21398 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21399
21400 /* Strip the base name, keep any leading namespaces/classes. */
21401 base = strrchr (DW_STRING (attr), ':');
21402 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21403 return "";
21404
21405 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21406 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21407 DW_STRING (attr),
21408 &base[-1] - DW_STRING (attr));
21409 }
21410
21411 /* Return the name of the namespace/class that DIE is defined within,
21412 or "" if we can't tell. The caller should not xfree the result.
21413
21414 For example, if we're within the method foo() in the following
21415 code:
21416
21417 namespace N {
21418 class C {
21419 void foo () {
21420 }
21421 };
21422 }
21423
21424 then determine_prefix on foo's die will return "N::C". */
21425
21426 static const char *
21427 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
21428 {
21429 struct dwarf2_per_objfile *dwarf2_per_objfile
21430 = cu->per_cu->dwarf2_per_objfile;
21431 struct die_info *parent, *spec_die;
21432 struct dwarf2_cu *spec_cu;
21433 struct type *parent_type;
21434 const char *retval;
21435
21436 if (cu->language != language_cplus
21437 && cu->language != language_fortran && cu->language != language_d
21438 && cu->language != language_rust)
21439 return "";
21440
21441 retval = anonymous_struct_prefix (die, cu);
21442 if (retval)
21443 return retval;
21444
21445 /* We have to be careful in the presence of DW_AT_specification.
21446 For example, with GCC 3.4, given the code
21447
21448 namespace N {
21449 void foo() {
21450 // Definition of N::foo.
21451 }
21452 }
21453
21454 then we'll have a tree of DIEs like this:
21455
21456 1: DW_TAG_compile_unit
21457 2: DW_TAG_namespace // N
21458 3: DW_TAG_subprogram // declaration of N::foo
21459 4: DW_TAG_subprogram // definition of N::foo
21460 DW_AT_specification // refers to die #3
21461
21462 Thus, when processing die #4, we have to pretend that we're in
21463 the context of its DW_AT_specification, namely the contex of die
21464 #3. */
21465 spec_cu = cu;
21466 spec_die = die_specification (die, &spec_cu);
21467 if (spec_die == NULL)
21468 parent = die->parent;
21469 else
21470 {
21471 parent = spec_die->parent;
21472 cu = spec_cu;
21473 }
21474
21475 if (parent == NULL)
21476 return "";
21477 else if (parent->building_fullname)
21478 {
21479 const char *name;
21480 const char *parent_name;
21481
21482 /* It has been seen on RealView 2.2 built binaries,
21483 DW_TAG_template_type_param types actually _defined_ as
21484 children of the parent class:
21485
21486 enum E {};
21487 template class <class Enum> Class{};
21488 Class<enum E> class_e;
21489
21490 1: DW_TAG_class_type (Class)
21491 2: DW_TAG_enumeration_type (E)
21492 3: DW_TAG_enumerator (enum1:0)
21493 3: DW_TAG_enumerator (enum2:1)
21494 ...
21495 2: DW_TAG_template_type_param
21496 DW_AT_type DW_FORM_ref_udata (E)
21497
21498 Besides being broken debug info, it can put GDB into an
21499 infinite loop. Consider:
21500
21501 When we're building the full name for Class<E>, we'll start
21502 at Class, and go look over its template type parameters,
21503 finding E. We'll then try to build the full name of E, and
21504 reach here. We're now trying to build the full name of E,
21505 and look over the parent DIE for containing scope. In the
21506 broken case, if we followed the parent DIE of E, we'd again
21507 find Class, and once again go look at its template type
21508 arguments, etc., etc. Simply don't consider such parent die
21509 as source-level parent of this die (it can't be, the language
21510 doesn't allow it), and break the loop here. */
21511 name = dwarf2_name (die, cu);
21512 parent_name = dwarf2_name (parent, cu);
21513 complaint (_("template param type '%s' defined within parent '%s'"),
21514 name ? name : "<unknown>",
21515 parent_name ? parent_name : "<unknown>");
21516 return "";
21517 }
21518 else
21519 switch (parent->tag)
21520 {
21521 case DW_TAG_namespace:
21522 parent_type = read_type_die (parent, cu);
21523 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21524 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21525 Work around this problem here. */
21526 if (cu->language == language_cplus
21527 && strcmp (TYPE_NAME (parent_type), "::") == 0)
21528 return "";
21529 /* We give a name to even anonymous namespaces. */
21530 return TYPE_NAME (parent_type);
21531 case DW_TAG_class_type:
21532 case DW_TAG_interface_type:
21533 case DW_TAG_structure_type:
21534 case DW_TAG_union_type:
21535 case DW_TAG_module:
21536 parent_type = read_type_die (parent, cu);
21537 if (TYPE_NAME (parent_type) != NULL)
21538 return TYPE_NAME (parent_type);
21539 else
21540 /* An anonymous structure is only allowed non-static data
21541 members; no typedefs, no member functions, et cetera.
21542 So it does not need a prefix. */
21543 return "";
21544 case DW_TAG_compile_unit:
21545 case DW_TAG_partial_unit:
21546 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21547 if (cu->language == language_cplus
21548 && !dwarf2_per_objfile->types.empty ()
21549 && die->child != NULL
21550 && (die->tag == DW_TAG_class_type
21551 || die->tag == DW_TAG_structure_type
21552 || die->tag == DW_TAG_union_type))
21553 {
21554 const char *name = guess_full_die_structure_name (die, cu);
21555 if (name != NULL)
21556 return name;
21557 }
21558 return "";
21559 case DW_TAG_subprogram:
21560 /* Nested subroutines in Fortran get a prefix with the name
21561 of the parent's subroutine. */
21562 if (cu->language == language_fortran)
21563 {
21564 if ((die->tag == DW_TAG_subprogram)
21565 && (dwarf2_name (parent, cu) != NULL))
21566 return dwarf2_name (parent, cu);
21567 }
21568 return determine_prefix (parent, cu);
21569 case DW_TAG_enumeration_type:
21570 parent_type = read_type_die (parent, cu);
21571 if (TYPE_DECLARED_CLASS (parent_type))
21572 {
21573 if (TYPE_NAME (parent_type) != NULL)
21574 return TYPE_NAME (parent_type);
21575 return "";
21576 }
21577 /* Fall through. */
21578 default:
21579 return determine_prefix (parent, cu);
21580 }
21581 }
21582
21583 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21584 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21585 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21586 an obconcat, otherwise allocate storage for the result. The CU argument is
21587 used to determine the language and hence, the appropriate separator. */
21588
21589 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21590
21591 static char *
21592 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21593 int physname, struct dwarf2_cu *cu)
21594 {
21595 const char *lead = "";
21596 const char *sep;
21597
21598 if (suffix == NULL || suffix[0] == '\0'
21599 || prefix == NULL || prefix[0] == '\0')
21600 sep = "";
21601 else if (cu->language == language_d)
21602 {
21603 /* For D, the 'main' function could be defined in any module, but it
21604 should never be prefixed. */
21605 if (strcmp (suffix, "D main") == 0)
21606 {
21607 prefix = "";
21608 sep = "";
21609 }
21610 else
21611 sep = ".";
21612 }
21613 else if (cu->language == language_fortran && physname)
21614 {
21615 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21616 DW_AT_MIPS_linkage_name is preferred and used instead. */
21617
21618 lead = "__";
21619 sep = "_MOD_";
21620 }
21621 else
21622 sep = "::";
21623
21624 if (prefix == NULL)
21625 prefix = "";
21626 if (suffix == NULL)
21627 suffix = "";
21628
21629 if (obs == NULL)
21630 {
21631 char *retval
21632 = ((char *)
21633 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21634
21635 strcpy (retval, lead);
21636 strcat (retval, prefix);
21637 strcat (retval, sep);
21638 strcat (retval, suffix);
21639 return retval;
21640 }
21641 else
21642 {
21643 /* We have an obstack. */
21644 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21645 }
21646 }
21647
21648 /* Return sibling of die, NULL if no sibling. */
21649
21650 static struct die_info *
21651 sibling_die (struct die_info *die)
21652 {
21653 return die->sibling;
21654 }
21655
21656 /* Get name of a die, return NULL if not found. */
21657
21658 static const char *
21659 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
21660 struct objfile *objfile)
21661 {
21662 if (name && cu->language == language_cplus)
21663 {
21664 std::string canon_name = cp_canonicalize_string (name);
21665
21666 if (!canon_name.empty ())
21667 {
21668 if (canon_name != name)
21669 name = objfile->intern (canon_name);
21670 }
21671 }
21672
21673 return name;
21674 }
21675
21676 /* Get name of a die, return NULL if not found.
21677 Anonymous namespaces are converted to their magic string. */
21678
21679 static const char *
21680 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
21681 {
21682 struct attribute *attr;
21683 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21684
21685 attr = dwarf2_attr (die, DW_AT_name, cu);
21686 if ((!attr || !DW_STRING (attr))
21687 && die->tag != DW_TAG_namespace
21688 && die->tag != DW_TAG_class_type
21689 && die->tag != DW_TAG_interface_type
21690 && die->tag != DW_TAG_structure_type
21691 && die->tag != DW_TAG_union_type)
21692 return NULL;
21693
21694 switch (die->tag)
21695 {
21696 case DW_TAG_compile_unit:
21697 case DW_TAG_partial_unit:
21698 /* Compilation units have a DW_AT_name that is a filename, not
21699 a source language identifier. */
21700 case DW_TAG_enumeration_type:
21701 case DW_TAG_enumerator:
21702 /* These tags always have simple identifiers already; no need
21703 to canonicalize them. */
21704 return DW_STRING (attr);
21705
21706 case DW_TAG_namespace:
21707 if (attr != NULL && DW_STRING (attr) != NULL)
21708 return DW_STRING (attr);
21709 return CP_ANONYMOUS_NAMESPACE_STR;
21710
21711 case DW_TAG_class_type:
21712 case DW_TAG_interface_type:
21713 case DW_TAG_structure_type:
21714 case DW_TAG_union_type:
21715 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21716 structures or unions. These were of the form "._%d" in GCC 4.1,
21717 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21718 and GCC 4.4. We work around this problem by ignoring these. */
21719 if (attr && DW_STRING (attr)
21720 && (startswith (DW_STRING (attr), "._")
21721 || startswith (DW_STRING (attr), "<anonymous")))
21722 return NULL;
21723
21724 /* GCC might emit a nameless typedef that has a linkage name. See
21725 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21726 if (!attr || DW_STRING (attr) == NULL)
21727 {
21728 attr = dw2_linkage_name_attr (die, cu);
21729 if (attr == NULL || DW_STRING (attr) == NULL)
21730 return NULL;
21731
21732 /* Avoid demangling DW_STRING (attr) the second time on a second
21733 call for the same DIE. */
21734 if (!DW_STRING_IS_CANONICAL (attr))
21735 {
21736 gdb::unique_xmalloc_ptr<char> demangled
21737 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
21738 if (demangled == nullptr)
21739 return nullptr;
21740
21741 DW_STRING (attr) = objfile->intern (demangled.get ());
21742 DW_STRING_IS_CANONICAL (attr) = 1;
21743 }
21744
21745 /* Strip any leading namespaces/classes, keep only the base name.
21746 DW_AT_name for named DIEs does not contain the prefixes. */
21747 const char *base = strrchr (DW_STRING (attr), ':');
21748 if (base && base > DW_STRING (attr) && base[-1] == ':')
21749 return &base[1];
21750 else
21751 return DW_STRING (attr);
21752 }
21753 break;
21754
21755 default:
21756 break;
21757 }
21758
21759 if (!DW_STRING_IS_CANONICAL (attr))
21760 {
21761 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21762 objfile);
21763 DW_STRING_IS_CANONICAL (attr) = 1;
21764 }
21765 return DW_STRING (attr);
21766 }
21767
21768 /* Return the die that this die in an extension of, or NULL if there
21769 is none. *EXT_CU is the CU containing DIE on input, and the CU
21770 containing the return value on output. */
21771
21772 static struct die_info *
21773 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
21774 {
21775 struct attribute *attr;
21776
21777 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
21778 if (attr == NULL)
21779 return NULL;
21780
21781 return follow_die_ref (die, attr, ext_cu);
21782 }
21783
21784 /* A convenience function that returns an "unknown" DWARF name,
21785 including the value of V. STR is the name of the entity being
21786 printed, e.g., "TAG". */
21787
21788 static const char *
21789 dwarf_unknown (const char *str, unsigned v)
21790 {
21791 char *cell = get_print_cell ();
21792 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
21793 return cell;
21794 }
21795
21796 /* Convert a DIE tag into its string name. */
21797
21798 static const char *
21799 dwarf_tag_name (unsigned tag)
21800 {
21801 const char *name = get_DW_TAG_name (tag);
21802
21803 if (name == NULL)
21804 return dwarf_unknown ("TAG", tag);
21805
21806 return name;
21807 }
21808
21809 /* Convert a DWARF attribute code into its string name. */
21810
21811 static const char *
21812 dwarf_attr_name (unsigned attr)
21813 {
21814 const char *name;
21815
21816 #ifdef MIPS /* collides with DW_AT_HP_block_index */
21817 if (attr == DW_AT_MIPS_fde)
21818 return "DW_AT_MIPS_fde";
21819 #else
21820 if (attr == DW_AT_HP_block_index)
21821 return "DW_AT_HP_block_index";
21822 #endif
21823
21824 name = get_DW_AT_name (attr);
21825
21826 if (name == NULL)
21827 return dwarf_unknown ("AT", attr);
21828
21829 return name;
21830 }
21831
21832 /* Convert a DWARF value form code into its string name. */
21833
21834 static const char *
21835 dwarf_form_name (unsigned form)
21836 {
21837 const char *name = get_DW_FORM_name (form);
21838
21839 if (name == NULL)
21840 return dwarf_unknown ("FORM", form);
21841
21842 return name;
21843 }
21844
21845 static const char *
21846 dwarf_bool_name (unsigned mybool)
21847 {
21848 if (mybool)
21849 return "TRUE";
21850 else
21851 return "FALSE";
21852 }
21853
21854 /* Convert a DWARF type code into its string name. */
21855
21856 static const char *
21857 dwarf_type_encoding_name (unsigned enc)
21858 {
21859 const char *name = get_DW_ATE_name (enc);
21860
21861 if (name == NULL)
21862 return dwarf_unknown ("ATE", enc);
21863
21864 return name;
21865 }
21866
21867 static void
21868 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
21869 {
21870 unsigned int i;
21871
21872 print_spaces (indent, f);
21873 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
21874 dwarf_tag_name (die->tag), die->abbrev,
21875 sect_offset_str (die->sect_off));
21876
21877 if (die->parent != NULL)
21878 {
21879 print_spaces (indent, f);
21880 fprintf_unfiltered (f, " parent at offset: %s\n",
21881 sect_offset_str (die->parent->sect_off));
21882 }
21883
21884 print_spaces (indent, f);
21885 fprintf_unfiltered (f, " has children: %s\n",
21886 dwarf_bool_name (die->child != NULL));
21887
21888 print_spaces (indent, f);
21889 fprintf_unfiltered (f, " attributes:\n");
21890
21891 for (i = 0; i < die->num_attrs; ++i)
21892 {
21893 print_spaces (indent, f);
21894 fprintf_unfiltered (f, " %s (%s) ",
21895 dwarf_attr_name (die->attrs[i].name),
21896 dwarf_form_name (die->attrs[i].form));
21897
21898 switch (die->attrs[i].form)
21899 {
21900 case DW_FORM_addr:
21901 case DW_FORM_addrx:
21902 case DW_FORM_GNU_addr_index:
21903 fprintf_unfiltered (f, "address: ");
21904 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
21905 break;
21906 case DW_FORM_block2:
21907 case DW_FORM_block4:
21908 case DW_FORM_block:
21909 case DW_FORM_block1:
21910 fprintf_unfiltered (f, "block: size %s",
21911 pulongest (DW_BLOCK (&die->attrs[i])->size));
21912 break;
21913 case DW_FORM_exprloc:
21914 fprintf_unfiltered (f, "expression: size %s",
21915 pulongest (DW_BLOCK (&die->attrs[i])->size));
21916 break;
21917 case DW_FORM_data16:
21918 fprintf_unfiltered (f, "constant of 16 bytes");
21919 break;
21920 case DW_FORM_ref_addr:
21921 fprintf_unfiltered (f, "ref address: ");
21922 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21923 break;
21924 case DW_FORM_GNU_ref_alt:
21925 fprintf_unfiltered (f, "alt ref address: ");
21926 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21927 break;
21928 case DW_FORM_ref1:
21929 case DW_FORM_ref2:
21930 case DW_FORM_ref4:
21931 case DW_FORM_ref8:
21932 case DW_FORM_ref_udata:
21933 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
21934 (long) (DW_UNSND (&die->attrs[i])));
21935 break;
21936 case DW_FORM_data1:
21937 case DW_FORM_data2:
21938 case DW_FORM_data4:
21939 case DW_FORM_data8:
21940 case DW_FORM_udata:
21941 case DW_FORM_sdata:
21942 fprintf_unfiltered (f, "constant: %s",
21943 pulongest (DW_UNSND (&die->attrs[i])));
21944 break;
21945 case DW_FORM_sec_offset:
21946 fprintf_unfiltered (f, "section offset: %s",
21947 pulongest (DW_UNSND (&die->attrs[i])));
21948 break;
21949 case DW_FORM_ref_sig8:
21950 fprintf_unfiltered (f, "signature: %s",
21951 hex_string (DW_SIGNATURE (&die->attrs[i])));
21952 break;
21953 case DW_FORM_string:
21954 case DW_FORM_strp:
21955 case DW_FORM_line_strp:
21956 case DW_FORM_strx:
21957 case DW_FORM_GNU_str_index:
21958 case DW_FORM_GNU_strp_alt:
21959 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
21960 DW_STRING (&die->attrs[i])
21961 ? DW_STRING (&die->attrs[i]) : "",
21962 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
21963 break;
21964 case DW_FORM_flag:
21965 if (DW_UNSND (&die->attrs[i]))
21966 fprintf_unfiltered (f, "flag: TRUE");
21967 else
21968 fprintf_unfiltered (f, "flag: FALSE");
21969 break;
21970 case DW_FORM_flag_present:
21971 fprintf_unfiltered (f, "flag: TRUE");
21972 break;
21973 case DW_FORM_indirect:
21974 /* The reader will have reduced the indirect form to
21975 the "base form" so this form should not occur. */
21976 fprintf_unfiltered (f,
21977 "unexpected attribute form: DW_FORM_indirect");
21978 break;
21979 case DW_FORM_implicit_const:
21980 fprintf_unfiltered (f, "constant: %s",
21981 plongest (DW_SND (&die->attrs[i])));
21982 break;
21983 default:
21984 fprintf_unfiltered (f, "unsupported attribute form: %d.",
21985 die->attrs[i].form);
21986 break;
21987 }
21988 fprintf_unfiltered (f, "\n");
21989 }
21990 }
21991
21992 static void
21993 dump_die_for_error (struct die_info *die)
21994 {
21995 dump_die_shallow (gdb_stderr, 0, die);
21996 }
21997
21998 static void
21999 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22000 {
22001 int indent = level * 4;
22002
22003 gdb_assert (die != NULL);
22004
22005 if (level >= max_level)
22006 return;
22007
22008 dump_die_shallow (f, indent, die);
22009
22010 if (die->child != NULL)
22011 {
22012 print_spaces (indent, f);
22013 fprintf_unfiltered (f, " Children:");
22014 if (level + 1 < max_level)
22015 {
22016 fprintf_unfiltered (f, "\n");
22017 dump_die_1 (f, level + 1, max_level, die->child);
22018 }
22019 else
22020 {
22021 fprintf_unfiltered (f,
22022 " [not printed, max nesting level reached]\n");
22023 }
22024 }
22025
22026 if (die->sibling != NULL && level > 0)
22027 {
22028 dump_die_1 (f, level, max_level, die->sibling);
22029 }
22030 }
22031
22032 /* This is called from the pdie macro in gdbinit.in.
22033 It's not static so gcc will keep a copy callable from gdb. */
22034
22035 void
22036 dump_die (struct die_info *die, int max_level)
22037 {
22038 dump_die_1 (gdb_stdlog, 0, max_level, die);
22039 }
22040
22041 static void
22042 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22043 {
22044 void **slot;
22045
22046 slot = htab_find_slot_with_hash (cu->die_hash, die,
22047 to_underlying (die->sect_off),
22048 INSERT);
22049
22050 *slot = die;
22051 }
22052
22053 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22054 required kind. */
22055
22056 static sect_offset
22057 dwarf2_get_ref_die_offset (const struct attribute *attr)
22058 {
22059 if (attr->form_is_ref ())
22060 return (sect_offset) DW_UNSND (attr);
22061
22062 complaint (_("unsupported die ref attribute form: '%s'"),
22063 dwarf_form_name (attr->form));
22064 return {};
22065 }
22066
22067 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22068 * the value held by the attribute is not constant. */
22069
22070 static LONGEST
22071 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
22072 {
22073 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
22074 return DW_SND (attr);
22075 else if (attr->form == DW_FORM_udata
22076 || attr->form == DW_FORM_data1
22077 || attr->form == DW_FORM_data2
22078 || attr->form == DW_FORM_data4
22079 || attr->form == DW_FORM_data8)
22080 return DW_UNSND (attr);
22081 else
22082 {
22083 /* For DW_FORM_data16 see attribute::form_is_constant. */
22084 complaint (_("Attribute value is not a constant (%s)"),
22085 dwarf_form_name (attr->form));
22086 return default_value;
22087 }
22088 }
22089
22090 /* Follow reference or signature attribute ATTR of SRC_DIE.
22091 On entry *REF_CU is the CU of SRC_DIE.
22092 On exit *REF_CU is the CU of the result. */
22093
22094 static struct die_info *
22095 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22096 struct dwarf2_cu **ref_cu)
22097 {
22098 struct die_info *die;
22099
22100 if (attr->form_is_ref ())
22101 die = follow_die_ref (src_die, attr, ref_cu);
22102 else if (attr->form == DW_FORM_ref_sig8)
22103 die = follow_die_sig (src_die, attr, ref_cu);
22104 else
22105 {
22106 dump_die_for_error (src_die);
22107 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22108 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22109 }
22110
22111 return die;
22112 }
22113
22114 /* Follow reference OFFSET.
22115 On entry *REF_CU is the CU of the source die referencing OFFSET.
22116 On exit *REF_CU is the CU of the result.
22117 Returns NULL if OFFSET is invalid. */
22118
22119 static struct die_info *
22120 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22121 struct dwarf2_cu **ref_cu)
22122 {
22123 struct die_info temp_die;
22124 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22125 struct dwarf2_per_objfile *dwarf2_per_objfile
22126 = cu->per_cu->dwarf2_per_objfile;
22127
22128 gdb_assert (cu->per_cu != NULL);
22129
22130 target_cu = cu;
22131
22132 if (cu->per_cu->is_debug_types)
22133 {
22134 /* .debug_types CUs cannot reference anything outside their CU.
22135 If they need to, they have to reference a signatured type via
22136 DW_FORM_ref_sig8. */
22137 if (!cu->header.offset_in_cu_p (sect_off))
22138 return NULL;
22139 }
22140 else if (offset_in_dwz != cu->per_cu->is_dwz
22141 || !cu->header.offset_in_cu_p (sect_off))
22142 {
22143 struct dwarf2_per_cu_data *per_cu;
22144
22145 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22146 dwarf2_per_objfile);
22147
22148 /* If necessary, add it to the queue and load its DIEs. */
22149 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
22150 load_full_comp_unit (per_cu, false, cu->language);
22151
22152 target_cu = per_cu->cu;
22153 }
22154 else if (cu->dies == NULL)
22155 {
22156 /* We're loading full DIEs during partial symbol reading. */
22157 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
22158 load_full_comp_unit (cu->per_cu, false, language_minimal);
22159 }
22160
22161 *ref_cu = target_cu;
22162 temp_die.sect_off = sect_off;
22163
22164 if (target_cu != cu)
22165 target_cu->ancestor = cu;
22166
22167 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22168 &temp_die,
22169 to_underlying (sect_off));
22170 }
22171
22172 /* Follow reference attribute ATTR of SRC_DIE.
22173 On entry *REF_CU is the CU of SRC_DIE.
22174 On exit *REF_CU is the CU of the result. */
22175
22176 static struct die_info *
22177 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22178 struct dwarf2_cu **ref_cu)
22179 {
22180 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22181 struct dwarf2_cu *cu = *ref_cu;
22182 struct die_info *die;
22183
22184 die = follow_die_offset (sect_off,
22185 (attr->form == DW_FORM_GNU_ref_alt
22186 || cu->per_cu->is_dwz),
22187 ref_cu);
22188 if (!die)
22189 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22190 "at %s [in module %s]"),
22191 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22192 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
22193
22194 return die;
22195 }
22196
22197 /* See read.h. */
22198
22199 struct dwarf2_locexpr_baton
22200 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22201 dwarf2_per_cu_data *per_cu,
22202 CORE_ADDR (*get_frame_pc) (void *baton),
22203 void *baton, bool resolve_abstract_p)
22204 {
22205 struct dwarf2_cu *cu;
22206 struct die_info *die;
22207 struct attribute *attr;
22208 struct dwarf2_locexpr_baton retval;
22209 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22210 struct objfile *objfile = dwarf2_per_objfile->objfile;
22211
22212 if (per_cu->cu == NULL)
22213 load_cu (per_cu, false);
22214 cu = per_cu->cu;
22215 if (cu == NULL)
22216 {
22217 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22218 Instead just throw an error, not much else we can do. */
22219 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22220 sect_offset_str (sect_off), objfile_name (objfile));
22221 }
22222
22223 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22224 if (!die)
22225 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22226 sect_offset_str (sect_off), objfile_name (objfile));
22227
22228 attr = dwarf2_attr (die, DW_AT_location, cu);
22229 if (!attr && resolve_abstract_p
22230 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
22231 != dwarf2_per_objfile->abstract_to_concrete.end ()))
22232 {
22233 CORE_ADDR pc = (*get_frame_pc) (baton);
22234 CORE_ADDR baseaddr = objfile->text_section_offset ();
22235 struct gdbarch *gdbarch = get_objfile_arch (objfile);
22236
22237 for (const auto &cand_off
22238 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
22239 {
22240 struct dwarf2_cu *cand_cu = cu;
22241 struct die_info *cand
22242 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22243 if (!cand
22244 || !cand->parent
22245 || cand->parent->tag != DW_TAG_subprogram)
22246 continue;
22247
22248 CORE_ADDR pc_low, pc_high;
22249 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
22250 if (pc_low == ((CORE_ADDR) -1))
22251 continue;
22252 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22253 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22254 if (!(pc_low <= pc && pc < pc_high))
22255 continue;
22256
22257 die = cand;
22258 attr = dwarf2_attr (die, DW_AT_location, cu);
22259 break;
22260 }
22261 }
22262
22263 if (!attr)
22264 {
22265 /* DWARF: "If there is no such attribute, then there is no effect.".
22266 DATA is ignored if SIZE is 0. */
22267
22268 retval.data = NULL;
22269 retval.size = 0;
22270 }
22271 else if (attr->form_is_section_offset ())
22272 {
22273 struct dwarf2_loclist_baton loclist_baton;
22274 CORE_ADDR pc = (*get_frame_pc) (baton);
22275 size_t size;
22276
22277 fill_in_loclist_baton (cu, &loclist_baton, attr);
22278
22279 retval.data = dwarf2_find_location_expression (&loclist_baton,
22280 &size, pc);
22281 retval.size = size;
22282 }
22283 else
22284 {
22285 if (!attr->form_is_block ())
22286 error (_("Dwarf Error: DIE at %s referenced in module %s "
22287 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22288 sect_offset_str (sect_off), objfile_name (objfile));
22289
22290 retval.data = DW_BLOCK (attr)->data;
22291 retval.size = DW_BLOCK (attr)->size;
22292 }
22293 retval.per_cu = cu->per_cu;
22294
22295 age_cached_comp_units (dwarf2_per_objfile);
22296
22297 return retval;
22298 }
22299
22300 /* See read.h. */
22301
22302 struct dwarf2_locexpr_baton
22303 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22304 dwarf2_per_cu_data *per_cu,
22305 CORE_ADDR (*get_frame_pc) (void *baton),
22306 void *baton)
22307 {
22308 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
22309
22310 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
22311 }
22312
22313 /* Write a constant of a given type as target-ordered bytes into
22314 OBSTACK. */
22315
22316 static const gdb_byte *
22317 write_constant_as_bytes (struct obstack *obstack,
22318 enum bfd_endian byte_order,
22319 struct type *type,
22320 ULONGEST value,
22321 LONGEST *len)
22322 {
22323 gdb_byte *result;
22324
22325 *len = TYPE_LENGTH (type);
22326 result = (gdb_byte *) obstack_alloc (obstack, *len);
22327 store_unsigned_integer (result, *len, byte_order, value);
22328
22329 return result;
22330 }
22331
22332 /* See read.h. */
22333
22334 const gdb_byte *
22335 dwarf2_fetch_constant_bytes (sect_offset sect_off,
22336 dwarf2_per_cu_data *per_cu,
22337 obstack *obstack,
22338 LONGEST *len)
22339 {
22340 struct dwarf2_cu *cu;
22341 struct die_info *die;
22342 struct attribute *attr;
22343 const gdb_byte *result = NULL;
22344 struct type *type;
22345 LONGEST value;
22346 enum bfd_endian byte_order;
22347 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
22348
22349 if (per_cu->cu == NULL)
22350 load_cu (per_cu, false);
22351 cu = per_cu->cu;
22352 if (cu == NULL)
22353 {
22354 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22355 Instead just throw an error, not much else we can do. */
22356 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22357 sect_offset_str (sect_off), objfile_name (objfile));
22358 }
22359
22360 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22361 if (!die)
22362 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22363 sect_offset_str (sect_off), objfile_name (objfile));
22364
22365 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22366 if (attr == NULL)
22367 return NULL;
22368
22369 byte_order = (bfd_big_endian (objfile->obfd)
22370 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22371
22372 switch (attr->form)
22373 {
22374 case DW_FORM_addr:
22375 case DW_FORM_addrx:
22376 case DW_FORM_GNU_addr_index:
22377 {
22378 gdb_byte *tem;
22379
22380 *len = cu->header.addr_size;
22381 tem = (gdb_byte *) obstack_alloc (obstack, *len);
22382 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22383 result = tem;
22384 }
22385 break;
22386 case DW_FORM_string:
22387 case DW_FORM_strp:
22388 case DW_FORM_strx:
22389 case DW_FORM_GNU_str_index:
22390 case DW_FORM_GNU_strp_alt:
22391 /* DW_STRING is already allocated on the objfile obstack, point
22392 directly to it. */
22393 result = (const gdb_byte *) DW_STRING (attr);
22394 *len = strlen (DW_STRING (attr));
22395 break;
22396 case DW_FORM_block1:
22397 case DW_FORM_block2:
22398 case DW_FORM_block4:
22399 case DW_FORM_block:
22400 case DW_FORM_exprloc:
22401 case DW_FORM_data16:
22402 result = DW_BLOCK (attr)->data;
22403 *len = DW_BLOCK (attr)->size;
22404 break;
22405
22406 /* The DW_AT_const_value attributes are supposed to carry the
22407 symbol's value "represented as it would be on the target
22408 architecture." By the time we get here, it's already been
22409 converted to host endianness, so we just need to sign- or
22410 zero-extend it as appropriate. */
22411 case DW_FORM_data1:
22412 type = die_type (die, cu);
22413 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22414 if (result == NULL)
22415 result = write_constant_as_bytes (obstack, byte_order,
22416 type, value, len);
22417 break;
22418 case DW_FORM_data2:
22419 type = die_type (die, cu);
22420 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22421 if (result == NULL)
22422 result = write_constant_as_bytes (obstack, byte_order,
22423 type, value, len);
22424 break;
22425 case DW_FORM_data4:
22426 type = die_type (die, cu);
22427 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22428 if (result == NULL)
22429 result = write_constant_as_bytes (obstack, byte_order,
22430 type, value, len);
22431 break;
22432 case DW_FORM_data8:
22433 type = die_type (die, cu);
22434 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22435 if (result == NULL)
22436 result = write_constant_as_bytes (obstack, byte_order,
22437 type, value, len);
22438 break;
22439
22440 case DW_FORM_sdata:
22441 case DW_FORM_implicit_const:
22442 type = die_type (die, cu);
22443 result = write_constant_as_bytes (obstack, byte_order,
22444 type, DW_SND (attr), len);
22445 break;
22446
22447 case DW_FORM_udata:
22448 type = die_type (die, cu);
22449 result = write_constant_as_bytes (obstack, byte_order,
22450 type, DW_UNSND (attr), len);
22451 break;
22452
22453 default:
22454 complaint (_("unsupported const value attribute form: '%s'"),
22455 dwarf_form_name (attr->form));
22456 break;
22457 }
22458
22459 return result;
22460 }
22461
22462 /* See read.h. */
22463
22464 struct type *
22465 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
22466 dwarf2_per_cu_data *per_cu)
22467 {
22468 struct dwarf2_cu *cu;
22469 struct die_info *die;
22470
22471 if (per_cu->cu == NULL)
22472 load_cu (per_cu, false);
22473 cu = per_cu->cu;
22474 if (!cu)
22475 return NULL;
22476
22477 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22478 if (!die)
22479 return NULL;
22480
22481 return die_type (die, cu);
22482 }
22483
22484 /* See read.h. */
22485
22486 struct type *
22487 dwarf2_get_die_type (cu_offset die_offset,
22488 struct dwarf2_per_cu_data *per_cu)
22489 {
22490 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
22491 return get_die_type_at_offset (die_offset_sect, per_cu);
22492 }
22493
22494 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
22495 On entry *REF_CU is the CU of SRC_DIE.
22496 On exit *REF_CU is the CU of the result.
22497 Returns NULL if the referenced DIE isn't found. */
22498
22499 static struct die_info *
22500 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22501 struct dwarf2_cu **ref_cu)
22502 {
22503 struct die_info temp_die;
22504 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
22505 struct die_info *die;
22506
22507 /* While it might be nice to assert sig_type->type == NULL here,
22508 we can get here for DW_AT_imported_declaration where we need
22509 the DIE not the type. */
22510
22511 /* If necessary, add it to the queue and load its DIEs. */
22512
22513 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
22514 read_signatured_type (sig_type);
22515
22516 sig_cu = sig_type->per_cu.cu;
22517 gdb_assert (sig_cu != NULL);
22518 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22519 temp_die.sect_off = sig_type->type_offset_in_section;
22520 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
22521 to_underlying (temp_die.sect_off));
22522 if (die)
22523 {
22524 struct dwarf2_per_objfile *dwarf2_per_objfile
22525 = (*ref_cu)->per_cu->dwarf2_per_objfile;
22526
22527 /* For .gdb_index version 7 keep track of included TUs.
22528 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22529 if (dwarf2_per_objfile->index_table != NULL
22530 && dwarf2_per_objfile->index_table->version <= 7)
22531 {
22532 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
22533 }
22534
22535 *ref_cu = sig_cu;
22536 if (sig_cu != cu)
22537 sig_cu->ancestor = cu;
22538
22539 return die;
22540 }
22541
22542 return NULL;
22543 }
22544
22545 /* Follow signatured type referenced by ATTR in SRC_DIE.
22546 On entry *REF_CU is the CU of SRC_DIE.
22547 On exit *REF_CU is the CU of the result.
22548 The result is the DIE of the type.
22549 If the referenced type cannot be found an error is thrown. */
22550
22551 static struct die_info *
22552 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22553 struct dwarf2_cu **ref_cu)
22554 {
22555 ULONGEST signature = DW_SIGNATURE (attr);
22556 struct signatured_type *sig_type;
22557 struct die_info *die;
22558
22559 gdb_assert (attr->form == DW_FORM_ref_sig8);
22560
22561 sig_type = lookup_signatured_type (*ref_cu, signature);
22562 /* sig_type will be NULL if the signatured type is missing from
22563 the debug info. */
22564 if (sig_type == NULL)
22565 {
22566 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22567 " from DIE at %s [in module %s]"),
22568 hex_string (signature), sect_offset_str (src_die->sect_off),
22569 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22570 }
22571
22572 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22573 if (die == NULL)
22574 {
22575 dump_die_for_error (src_die);
22576 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22577 " from DIE at %s [in module %s]"),
22578 hex_string (signature), sect_offset_str (src_die->sect_off),
22579 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22580 }
22581
22582 return die;
22583 }
22584
22585 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22586 reading in and processing the type unit if necessary. */
22587
22588 static struct type *
22589 get_signatured_type (struct die_info *die, ULONGEST signature,
22590 struct dwarf2_cu *cu)
22591 {
22592 struct dwarf2_per_objfile *dwarf2_per_objfile
22593 = cu->per_cu->dwarf2_per_objfile;
22594 struct signatured_type *sig_type;
22595 struct dwarf2_cu *type_cu;
22596 struct die_info *type_die;
22597 struct type *type;
22598
22599 sig_type = lookup_signatured_type (cu, signature);
22600 /* sig_type will be NULL if the signatured type is missing from
22601 the debug info. */
22602 if (sig_type == NULL)
22603 {
22604 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22605 " from DIE at %s [in module %s]"),
22606 hex_string (signature), sect_offset_str (die->sect_off),
22607 objfile_name (dwarf2_per_objfile->objfile));
22608 return build_error_marker_type (cu, die);
22609 }
22610
22611 /* If we already know the type we're done. */
22612 if (sig_type->type != NULL)
22613 return sig_type->type;
22614
22615 type_cu = cu;
22616 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22617 if (type_die != NULL)
22618 {
22619 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22620 is created. This is important, for example, because for c++ classes
22621 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22622 type = read_type_die (type_die, type_cu);
22623 if (type == NULL)
22624 {
22625 complaint (_("Dwarf Error: Cannot build signatured type %s"
22626 " referenced from DIE at %s [in module %s]"),
22627 hex_string (signature), sect_offset_str (die->sect_off),
22628 objfile_name (dwarf2_per_objfile->objfile));
22629 type = build_error_marker_type (cu, die);
22630 }
22631 }
22632 else
22633 {
22634 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22635 " from DIE at %s [in module %s]"),
22636 hex_string (signature), sect_offset_str (die->sect_off),
22637 objfile_name (dwarf2_per_objfile->objfile));
22638 type = build_error_marker_type (cu, die);
22639 }
22640 sig_type->type = type;
22641
22642 return type;
22643 }
22644
22645 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22646 reading in and processing the type unit if necessary. */
22647
22648 static struct type *
22649 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22650 struct dwarf2_cu *cu) /* ARI: editCase function */
22651 {
22652 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22653 if (attr->form_is_ref ())
22654 {
22655 struct dwarf2_cu *type_cu = cu;
22656 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22657
22658 return read_type_die (type_die, type_cu);
22659 }
22660 else if (attr->form == DW_FORM_ref_sig8)
22661 {
22662 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22663 }
22664 else
22665 {
22666 struct dwarf2_per_objfile *dwarf2_per_objfile
22667 = cu->per_cu->dwarf2_per_objfile;
22668
22669 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22670 " at %s [in module %s]"),
22671 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22672 objfile_name (dwarf2_per_objfile->objfile));
22673 return build_error_marker_type (cu, die);
22674 }
22675 }
22676
22677 /* Load the DIEs associated with type unit PER_CU into memory. */
22678
22679 static void
22680 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
22681 {
22682 struct signatured_type *sig_type;
22683
22684 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22685 gdb_assert (! per_cu->type_unit_group_p ());
22686
22687 /* We have the per_cu, but we need the signatured_type.
22688 Fortunately this is an easy translation. */
22689 gdb_assert (per_cu->is_debug_types);
22690 sig_type = (struct signatured_type *) per_cu;
22691
22692 gdb_assert (per_cu->cu == NULL);
22693
22694 read_signatured_type (sig_type);
22695
22696 gdb_assert (per_cu->cu != NULL);
22697 }
22698
22699 /* Read in a signatured type and build its CU and DIEs.
22700 If the type is a stub for the real type in a DWO file,
22701 read in the real type from the DWO file as well. */
22702
22703 static void
22704 read_signatured_type (struct signatured_type *sig_type)
22705 {
22706 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
22707
22708 gdb_assert (per_cu->is_debug_types);
22709 gdb_assert (per_cu->cu == NULL);
22710
22711 cutu_reader reader (per_cu, NULL, 0, false);
22712
22713 if (!reader.dummy_p)
22714 {
22715 struct dwarf2_cu *cu = reader.cu;
22716 const gdb_byte *info_ptr = reader.info_ptr;
22717
22718 gdb_assert (cu->die_hash == NULL);
22719 cu->die_hash =
22720 htab_create_alloc_ex (cu->header.length / 12,
22721 die_hash,
22722 die_eq,
22723 NULL,
22724 &cu->comp_unit_obstack,
22725 hashtab_obstack_allocate,
22726 dummy_obstack_deallocate);
22727
22728 if (reader.comp_unit_die->has_children)
22729 reader.comp_unit_die->child
22730 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22731 reader.comp_unit_die);
22732 cu->dies = reader.comp_unit_die;
22733 /* comp_unit_die is not stored in die_hash, no need. */
22734
22735 /* We try not to read any attributes in this function, because
22736 not all CUs needed for references have been loaded yet, and
22737 symbol table processing isn't initialized. But we have to
22738 set the CU language, or we won't be able to build types
22739 correctly. Similarly, if we do not read the producer, we can
22740 not apply producer-specific interpretation. */
22741 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22742
22743 reader.keep ();
22744 }
22745
22746 sig_type->per_cu.tu_read = 1;
22747 }
22748
22749 /* Decode simple location descriptions.
22750 Given a pointer to a dwarf block that defines a location, compute
22751 the location and return the value.
22752
22753 NOTE drow/2003-11-18: This function is called in two situations
22754 now: for the address of static or global variables (partial symbols
22755 only) and for offsets into structures which are expected to be
22756 (more or less) constant. The partial symbol case should go away,
22757 and only the constant case should remain. That will let this
22758 function complain more accurately. A few special modes are allowed
22759 without complaint for global variables (for instance, global
22760 register values and thread-local values).
22761
22762 A location description containing no operations indicates that the
22763 object is optimized out. The return value is 0 for that case.
22764 FIXME drow/2003-11-16: No callers check for this case any more; soon all
22765 callers will only want a very basic result and this can become a
22766 complaint.
22767
22768 Note that stack[0] is unused except as a default error return. */
22769
22770 static CORE_ADDR
22771 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
22772 {
22773 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22774 size_t i;
22775 size_t size = blk->size;
22776 const gdb_byte *data = blk->data;
22777 CORE_ADDR stack[64];
22778 int stacki;
22779 unsigned int bytes_read, unsnd;
22780 gdb_byte op;
22781
22782 i = 0;
22783 stacki = 0;
22784 stack[stacki] = 0;
22785 stack[++stacki] = 0;
22786
22787 while (i < size)
22788 {
22789 op = data[i++];
22790 switch (op)
22791 {
22792 case DW_OP_lit0:
22793 case DW_OP_lit1:
22794 case DW_OP_lit2:
22795 case DW_OP_lit3:
22796 case DW_OP_lit4:
22797 case DW_OP_lit5:
22798 case DW_OP_lit6:
22799 case DW_OP_lit7:
22800 case DW_OP_lit8:
22801 case DW_OP_lit9:
22802 case DW_OP_lit10:
22803 case DW_OP_lit11:
22804 case DW_OP_lit12:
22805 case DW_OP_lit13:
22806 case DW_OP_lit14:
22807 case DW_OP_lit15:
22808 case DW_OP_lit16:
22809 case DW_OP_lit17:
22810 case DW_OP_lit18:
22811 case DW_OP_lit19:
22812 case DW_OP_lit20:
22813 case DW_OP_lit21:
22814 case DW_OP_lit22:
22815 case DW_OP_lit23:
22816 case DW_OP_lit24:
22817 case DW_OP_lit25:
22818 case DW_OP_lit26:
22819 case DW_OP_lit27:
22820 case DW_OP_lit28:
22821 case DW_OP_lit29:
22822 case DW_OP_lit30:
22823 case DW_OP_lit31:
22824 stack[++stacki] = op - DW_OP_lit0;
22825 break;
22826
22827 case DW_OP_reg0:
22828 case DW_OP_reg1:
22829 case DW_OP_reg2:
22830 case DW_OP_reg3:
22831 case DW_OP_reg4:
22832 case DW_OP_reg5:
22833 case DW_OP_reg6:
22834 case DW_OP_reg7:
22835 case DW_OP_reg8:
22836 case DW_OP_reg9:
22837 case DW_OP_reg10:
22838 case DW_OP_reg11:
22839 case DW_OP_reg12:
22840 case DW_OP_reg13:
22841 case DW_OP_reg14:
22842 case DW_OP_reg15:
22843 case DW_OP_reg16:
22844 case DW_OP_reg17:
22845 case DW_OP_reg18:
22846 case DW_OP_reg19:
22847 case DW_OP_reg20:
22848 case DW_OP_reg21:
22849 case DW_OP_reg22:
22850 case DW_OP_reg23:
22851 case DW_OP_reg24:
22852 case DW_OP_reg25:
22853 case DW_OP_reg26:
22854 case DW_OP_reg27:
22855 case DW_OP_reg28:
22856 case DW_OP_reg29:
22857 case DW_OP_reg30:
22858 case DW_OP_reg31:
22859 stack[++stacki] = op - DW_OP_reg0;
22860 if (i < size)
22861 dwarf2_complex_location_expr_complaint ();
22862 break;
22863
22864 case DW_OP_regx:
22865 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22866 i += bytes_read;
22867 stack[++stacki] = unsnd;
22868 if (i < size)
22869 dwarf2_complex_location_expr_complaint ();
22870 break;
22871
22872 case DW_OP_addr:
22873 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22874 &bytes_read);
22875 i += bytes_read;
22876 break;
22877
22878 case DW_OP_const1u:
22879 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22880 i += 1;
22881 break;
22882
22883 case DW_OP_const1s:
22884 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22885 i += 1;
22886 break;
22887
22888 case DW_OP_const2u:
22889 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22890 i += 2;
22891 break;
22892
22893 case DW_OP_const2s:
22894 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22895 i += 2;
22896 break;
22897
22898 case DW_OP_const4u:
22899 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22900 i += 4;
22901 break;
22902
22903 case DW_OP_const4s:
22904 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22905 i += 4;
22906 break;
22907
22908 case DW_OP_const8u:
22909 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22910 i += 8;
22911 break;
22912
22913 case DW_OP_constu:
22914 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22915 &bytes_read);
22916 i += bytes_read;
22917 break;
22918
22919 case DW_OP_consts:
22920 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22921 i += bytes_read;
22922 break;
22923
22924 case DW_OP_dup:
22925 stack[stacki + 1] = stack[stacki];
22926 stacki++;
22927 break;
22928
22929 case DW_OP_plus:
22930 stack[stacki - 1] += stack[stacki];
22931 stacki--;
22932 break;
22933
22934 case DW_OP_plus_uconst:
22935 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22936 &bytes_read);
22937 i += bytes_read;
22938 break;
22939
22940 case DW_OP_minus:
22941 stack[stacki - 1] -= stack[stacki];
22942 stacki--;
22943 break;
22944
22945 case DW_OP_deref:
22946 /* If we're not the last op, then we definitely can't encode
22947 this using GDB's address_class enum. This is valid for partial
22948 global symbols, although the variable's address will be bogus
22949 in the psymtab. */
22950 if (i < size)
22951 dwarf2_complex_location_expr_complaint ();
22952 break;
22953
22954 case DW_OP_GNU_push_tls_address:
22955 case DW_OP_form_tls_address:
22956 /* The top of the stack has the offset from the beginning
22957 of the thread control block at which the variable is located. */
22958 /* Nothing should follow this operator, so the top of stack would
22959 be returned. */
22960 /* This is valid for partial global symbols, but the variable's
22961 address will be bogus in the psymtab. Make it always at least
22962 non-zero to not look as a variable garbage collected by linker
22963 which have DW_OP_addr 0. */
22964 if (i < size)
22965 dwarf2_complex_location_expr_complaint ();
22966 stack[stacki]++;
22967 break;
22968
22969 case DW_OP_GNU_uninit:
22970 break;
22971
22972 case DW_OP_addrx:
22973 case DW_OP_GNU_addr_index:
22974 case DW_OP_GNU_const_index:
22975 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
22976 &bytes_read);
22977 i += bytes_read;
22978 break;
22979
22980 default:
22981 {
22982 const char *name = get_DW_OP_name (op);
22983
22984 if (name)
22985 complaint (_("unsupported stack op: '%s'"),
22986 name);
22987 else
22988 complaint (_("unsupported stack op: '%02x'"),
22989 op);
22990 }
22991
22992 return (stack[stacki]);
22993 }
22994
22995 /* Enforce maximum stack depth of SIZE-1 to avoid writing
22996 outside of the allocated space. Also enforce minimum>0. */
22997 if (stacki >= ARRAY_SIZE (stack) - 1)
22998 {
22999 complaint (_("location description stack overflow"));
23000 return 0;
23001 }
23002
23003 if (stacki <= 0)
23004 {
23005 complaint (_("location description stack underflow"));
23006 return 0;
23007 }
23008 }
23009 return (stack[stacki]);
23010 }
23011
23012 /* memory allocation interface */
23013
23014 static struct dwarf_block *
23015 dwarf_alloc_block (struct dwarf2_cu *cu)
23016 {
23017 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23018 }
23019
23020 static struct die_info *
23021 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23022 {
23023 struct die_info *die;
23024 size_t size = sizeof (struct die_info);
23025
23026 if (num_attrs > 1)
23027 size += (num_attrs - 1) * sizeof (struct attribute);
23028
23029 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23030 memset (die, 0, sizeof (struct die_info));
23031 return (die);
23032 }
23033
23034 \f
23035
23036 /* Macro support. */
23037
23038 /* An overload of dwarf_decode_macros that finds the correct section
23039 and ensures it is read in before calling the other overload. */
23040
23041 static void
23042 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23043 int section_is_gnu)
23044 {
23045 struct dwarf2_per_objfile *dwarf2_per_objfile
23046 = cu->per_cu->dwarf2_per_objfile;
23047 struct objfile *objfile = dwarf2_per_objfile->objfile;
23048 const struct line_header *lh = cu->line_header;
23049 unsigned int offset_size = cu->header.offset_size;
23050 struct dwarf2_section_info *section;
23051 const char *section_name;
23052
23053 if (cu->dwo_unit != nullptr)
23054 {
23055 if (section_is_gnu)
23056 {
23057 section = &cu->dwo_unit->dwo_file->sections.macro;
23058 section_name = ".debug_macro.dwo";
23059 }
23060 else
23061 {
23062 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23063 section_name = ".debug_macinfo.dwo";
23064 }
23065 }
23066 else
23067 {
23068 if (section_is_gnu)
23069 {
23070 section = &dwarf2_per_objfile->macro;
23071 section_name = ".debug_macro";
23072 }
23073 else
23074 {
23075 section = &dwarf2_per_objfile->macinfo;
23076 section_name = ".debug_macinfo";
23077 }
23078 }
23079
23080 section->read (objfile);
23081 if (section->buffer == nullptr)
23082 {
23083 complaint (_("missing %s section"), section_name);
23084 return;
23085 }
23086
23087 buildsym_compunit *builder = cu->get_builder ();
23088
23089 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
23090 offset_size, offset, section_is_gnu);
23091 }
23092
23093 /* Return the .debug_loc section to use for CU.
23094 For DWO files use .debug_loc.dwo. */
23095
23096 static struct dwarf2_section_info *
23097 cu_debug_loc_section (struct dwarf2_cu *cu)
23098 {
23099 struct dwarf2_per_objfile *dwarf2_per_objfile
23100 = cu->per_cu->dwarf2_per_objfile;
23101
23102 if (cu->dwo_unit)
23103 {
23104 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23105
23106 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23107 }
23108 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
23109 : &dwarf2_per_objfile->loc);
23110 }
23111
23112 /* A helper function that fills in a dwarf2_loclist_baton. */
23113
23114 static void
23115 fill_in_loclist_baton (struct dwarf2_cu *cu,
23116 struct dwarf2_loclist_baton *baton,
23117 const struct attribute *attr)
23118 {
23119 struct dwarf2_per_objfile *dwarf2_per_objfile
23120 = cu->per_cu->dwarf2_per_objfile;
23121 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23122
23123 section->read (dwarf2_per_objfile->objfile);
23124
23125 baton->per_cu = cu->per_cu;
23126 gdb_assert (baton->per_cu);
23127 /* We don't know how long the location list is, but make sure we
23128 don't run off the edge of the section. */
23129 baton->size = section->size - DW_UNSND (attr);
23130 baton->data = section->buffer + DW_UNSND (attr);
23131 baton->base_address = cu->base_address;
23132 baton->from_dwo = cu->dwo_unit != NULL;
23133 }
23134
23135 static void
23136 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
23137 struct dwarf2_cu *cu, int is_block)
23138 {
23139 struct dwarf2_per_objfile *dwarf2_per_objfile
23140 = cu->per_cu->dwarf2_per_objfile;
23141 struct objfile *objfile = dwarf2_per_objfile->objfile;
23142 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23143
23144 if (attr->form_is_section_offset ()
23145 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
23146 the section. If so, fall through to the complaint in the
23147 other branch. */
23148 && DW_UNSND (attr) < section->get_size (objfile))
23149 {
23150 struct dwarf2_loclist_baton *baton;
23151
23152 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
23153
23154 fill_in_loclist_baton (cu, baton, attr);
23155
23156 if (cu->base_known == 0)
23157 complaint (_("Location list used without "
23158 "specifying the CU base address."));
23159
23160 SYMBOL_ACLASS_INDEX (sym) = (is_block
23161 ? dwarf2_loclist_block_index
23162 : dwarf2_loclist_index);
23163 SYMBOL_LOCATION_BATON (sym) = baton;
23164 }
23165 else
23166 {
23167 struct dwarf2_locexpr_baton *baton;
23168
23169 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
23170 baton->per_cu = cu->per_cu;
23171 gdb_assert (baton->per_cu);
23172
23173 if (attr->form_is_block ())
23174 {
23175 /* Note that we're just copying the block's data pointer
23176 here, not the actual data. We're still pointing into the
23177 info_buffer for SYM's objfile; right now we never release
23178 that buffer, but when we do clean up properly this may
23179 need to change. */
23180 baton->size = DW_BLOCK (attr)->size;
23181 baton->data = DW_BLOCK (attr)->data;
23182 }
23183 else
23184 {
23185 dwarf2_invalid_attrib_class_complaint ("location description",
23186 sym->natural_name ());
23187 baton->size = 0;
23188 }
23189
23190 SYMBOL_ACLASS_INDEX (sym) = (is_block
23191 ? dwarf2_locexpr_block_index
23192 : dwarf2_locexpr_index);
23193 SYMBOL_LOCATION_BATON (sym) = baton;
23194 }
23195 }
23196
23197 /* See read.h. */
23198
23199 struct objfile *
23200 dwarf2_per_cu_data::objfile () const
23201 {
23202 struct objfile *objfile = dwarf2_per_objfile->objfile;
23203
23204 /* Return the master objfile, so that we can report and look up the
23205 correct file containing this variable. */
23206 if (objfile->separate_debug_objfile_backlink)
23207 objfile = objfile->separate_debug_objfile_backlink;
23208
23209 return objfile;
23210 }
23211
23212 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
23213 (CU_HEADERP is unused in such case) or prepare a temporary copy at
23214 CU_HEADERP first. */
23215
23216 static const struct comp_unit_head *
23217 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
23218 const struct dwarf2_per_cu_data *per_cu)
23219 {
23220 const gdb_byte *info_ptr;
23221
23222 if (per_cu->cu)
23223 return &per_cu->cu->header;
23224
23225 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
23226
23227 memset (cu_headerp, 0, sizeof (*cu_headerp));
23228 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
23229 rcuh_kind::COMPILE);
23230
23231 return cu_headerp;
23232 }
23233
23234 /* See read.h. */
23235
23236 int
23237 dwarf2_per_cu_data::addr_size () const
23238 {
23239 struct comp_unit_head cu_header_local;
23240 const struct comp_unit_head *cu_headerp;
23241
23242 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
23243
23244 return cu_headerp->addr_size;
23245 }
23246
23247 /* See read.h. */
23248
23249 int
23250 dwarf2_per_cu_data::offset_size () const
23251 {
23252 struct comp_unit_head cu_header_local;
23253 const struct comp_unit_head *cu_headerp;
23254
23255 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
23256
23257 return cu_headerp->offset_size;
23258 }
23259
23260 /* See read.h. */
23261
23262 int
23263 dwarf2_per_cu_data::ref_addr_size () const
23264 {
23265 struct comp_unit_head cu_header_local;
23266 const struct comp_unit_head *cu_headerp;
23267
23268 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
23269
23270 if (cu_headerp->version == 2)
23271 return cu_headerp->addr_size;
23272 else
23273 return cu_headerp->offset_size;
23274 }
23275
23276 /* See read.h. */
23277
23278 CORE_ADDR
23279 dwarf2_per_cu_data::text_offset () const
23280 {
23281 struct objfile *objfile = dwarf2_per_objfile->objfile;
23282
23283 return objfile->text_section_offset ();
23284 }
23285
23286 /* See read.h. */
23287
23288 struct type *
23289 dwarf2_per_cu_data::addr_type () const
23290 {
23291 struct objfile *objfile = dwarf2_per_objfile->objfile;
23292 struct type *void_type = objfile_type (objfile)->builtin_void;
23293 struct type *addr_type = lookup_pointer_type (void_type);
23294 int addr_size = this->addr_size ();
23295
23296 if (TYPE_LENGTH (addr_type) == addr_size)
23297 return addr_type;
23298
23299 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
23300 return addr_type;
23301 }
23302
23303 /* A helper function for dwarf2_find_containing_comp_unit that returns
23304 the index of the result, and that searches a vector. It will
23305 return a result even if the offset in question does not actually
23306 occur in any CU. This is separate so that it can be unit
23307 tested. */
23308
23309 static int
23310 dwarf2_find_containing_comp_unit
23311 (sect_offset sect_off,
23312 unsigned int offset_in_dwz,
23313 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
23314 {
23315 int low, high;
23316
23317 low = 0;
23318 high = all_comp_units.size () - 1;
23319 while (high > low)
23320 {
23321 struct dwarf2_per_cu_data *mid_cu;
23322 int mid = low + (high - low) / 2;
23323
23324 mid_cu = all_comp_units[mid];
23325 if (mid_cu->is_dwz > offset_in_dwz
23326 || (mid_cu->is_dwz == offset_in_dwz
23327 && mid_cu->sect_off + mid_cu->length > sect_off))
23328 high = mid;
23329 else
23330 low = mid + 1;
23331 }
23332 gdb_assert (low == high);
23333 return low;
23334 }
23335
23336 /* Locate the .debug_info compilation unit from CU's objfile which contains
23337 the DIE at OFFSET. Raises an error on failure. */
23338
23339 static struct dwarf2_per_cu_data *
23340 dwarf2_find_containing_comp_unit (sect_offset sect_off,
23341 unsigned int offset_in_dwz,
23342 struct dwarf2_per_objfile *dwarf2_per_objfile)
23343 {
23344 int low
23345 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23346 dwarf2_per_objfile->all_comp_units);
23347 struct dwarf2_per_cu_data *this_cu
23348 = dwarf2_per_objfile->all_comp_units[low];
23349
23350 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
23351 {
23352 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
23353 error (_("Dwarf Error: could not find partial DIE containing "
23354 "offset %s [in module %s]"),
23355 sect_offset_str (sect_off),
23356 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
23357
23358 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
23359 <= sect_off);
23360 return dwarf2_per_objfile->all_comp_units[low-1];
23361 }
23362 else
23363 {
23364 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
23365 && sect_off >= this_cu->sect_off + this_cu->length)
23366 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
23367 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
23368 return this_cu;
23369 }
23370 }
23371
23372 #if GDB_SELF_TEST
23373
23374 namespace selftests {
23375 namespace find_containing_comp_unit {
23376
23377 static void
23378 run_test ()
23379 {
23380 struct dwarf2_per_cu_data one {};
23381 struct dwarf2_per_cu_data two {};
23382 struct dwarf2_per_cu_data three {};
23383 struct dwarf2_per_cu_data four {};
23384
23385 one.length = 5;
23386 two.sect_off = sect_offset (one.length);
23387 two.length = 7;
23388
23389 three.length = 5;
23390 three.is_dwz = 1;
23391 four.sect_off = sect_offset (three.length);
23392 four.length = 7;
23393 four.is_dwz = 1;
23394
23395 std::vector<dwarf2_per_cu_data *> units;
23396 units.push_back (&one);
23397 units.push_back (&two);
23398 units.push_back (&three);
23399 units.push_back (&four);
23400
23401 int result;
23402
23403 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23404 SELF_CHECK (units[result] == &one);
23405 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23406 SELF_CHECK (units[result] == &one);
23407 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23408 SELF_CHECK (units[result] == &two);
23409
23410 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23411 SELF_CHECK (units[result] == &three);
23412 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23413 SELF_CHECK (units[result] == &three);
23414 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23415 SELF_CHECK (units[result] == &four);
23416 }
23417
23418 }
23419 }
23420
23421 #endif /* GDB_SELF_TEST */
23422
23423 /* Initialize dwarf2_cu CU, owned by PER_CU. */
23424
23425 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
23426 : per_cu (per_cu_),
23427 mark (false),
23428 has_loclist (false),
23429 checked_producer (false),
23430 producer_is_gxx_lt_4_6 (false),
23431 producer_is_gcc_lt_4_3 (false),
23432 producer_is_icc (false),
23433 producer_is_icc_lt_14 (false),
23434 producer_is_codewarrior (false),
23435 processing_has_namespace_info (false)
23436 {
23437 per_cu->cu = this;
23438 }
23439
23440 /* Destroy a dwarf2_cu. */
23441
23442 dwarf2_cu::~dwarf2_cu ()
23443 {
23444 per_cu->cu = NULL;
23445 }
23446
23447 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23448
23449 static void
23450 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23451 enum language pretend_language)
23452 {
23453 struct attribute *attr;
23454
23455 /* Set the language we're debugging. */
23456 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
23457 if (attr != nullptr)
23458 set_cu_language (DW_UNSND (attr), cu);
23459 else
23460 {
23461 cu->language = pretend_language;
23462 cu->language_defn = language_def (cu->language);
23463 }
23464
23465 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
23466 }
23467
23468 /* Increase the age counter on each cached compilation unit, and free
23469 any that are too old. */
23470
23471 static void
23472 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
23473 {
23474 struct dwarf2_per_cu_data *per_cu, **last_chain;
23475
23476 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
23477 per_cu = dwarf2_per_objfile->read_in_chain;
23478 while (per_cu != NULL)
23479 {
23480 per_cu->cu->last_used ++;
23481 if (per_cu->cu->last_used <= dwarf_max_cache_age)
23482 dwarf2_mark (per_cu->cu);
23483 per_cu = per_cu->cu->read_in_chain;
23484 }
23485
23486 per_cu = dwarf2_per_objfile->read_in_chain;
23487 last_chain = &dwarf2_per_objfile->read_in_chain;
23488 while (per_cu != NULL)
23489 {
23490 struct dwarf2_per_cu_data *next_cu;
23491
23492 next_cu = per_cu->cu->read_in_chain;
23493
23494 if (!per_cu->cu->mark)
23495 {
23496 delete per_cu->cu;
23497 *last_chain = next_cu;
23498 }
23499 else
23500 last_chain = &per_cu->cu->read_in_chain;
23501
23502 per_cu = next_cu;
23503 }
23504 }
23505
23506 /* Remove a single compilation unit from the cache. */
23507
23508 static void
23509 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
23510 {
23511 struct dwarf2_per_cu_data *per_cu, **last_chain;
23512 struct dwarf2_per_objfile *dwarf2_per_objfile
23513 = target_per_cu->dwarf2_per_objfile;
23514
23515 per_cu = dwarf2_per_objfile->read_in_chain;
23516 last_chain = &dwarf2_per_objfile->read_in_chain;
23517 while (per_cu != NULL)
23518 {
23519 struct dwarf2_per_cu_data *next_cu;
23520
23521 next_cu = per_cu->cu->read_in_chain;
23522
23523 if (per_cu == target_per_cu)
23524 {
23525 delete per_cu->cu;
23526 per_cu->cu = NULL;
23527 *last_chain = next_cu;
23528 break;
23529 }
23530 else
23531 last_chain = &per_cu->cu->read_in_chain;
23532
23533 per_cu = next_cu;
23534 }
23535 }
23536
23537 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23538 We store these in a hash table separate from the DIEs, and preserve them
23539 when the DIEs are flushed out of cache.
23540
23541 The CU "per_cu" pointer is needed because offset alone is not enough to
23542 uniquely identify the type. A file may have multiple .debug_types sections,
23543 or the type may come from a DWO file. Furthermore, while it's more logical
23544 to use per_cu->section+offset, with Fission the section with the data is in
23545 the DWO file but we don't know that section at the point we need it.
23546 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23547 because we can enter the lookup routine, get_die_type_at_offset, from
23548 outside this file, and thus won't necessarily have PER_CU->cu.
23549 Fortunately, PER_CU is stable for the life of the objfile. */
23550
23551 struct dwarf2_per_cu_offset_and_type
23552 {
23553 const struct dwarf2_per_cu_data *per_cu;
23554 sect_offset sect_off;
23555 struct type *type;
23556 };
23557
23558 /* Hash function for a dwarf2_per_cu_offset_and_type. */
23559
23560 static hashval_t
23561 per_cu_offset_and_type_hash (const void *item)
23562 {
23563 const struct dwarf2_per_cu_offset_and_type *ofs
23564 = (const struct dwarf2_per_cu_offset_and_type *) item;
23565
23566 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
23567 }
23568
23569 /* Equality function for a dwarf2_per_cu_offset_and_type. */
23570
23571 static int
23572 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
23573 {
23574 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23575 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23576 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23577 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
23578
23579 return (ofs_lhs->per_cu == ofs_rhs->per_cu
23580 && ofs_lhs->sect_off == ofs_rhs->sect_off);
23581 }
23582
23583 /* Set the type associated with DIE to TYPE. Save it in CU's hash
23584 table if necessary. For convenience, return TYPE.
23585
23586 The DIEs reading must have careful ordering to:
23587 * Not cause infinite loops trying to read in DIEs as a prerequisite for
23588 reading current DIE.
23589 * Not trying to dereference contents of still incompletely read in types
23590 while reading in other DIEs.
23591 * Enable referencing still incompletely read in types just by a pointer to
23592 the type without accessing its fields.
23593
23594 Therefore caller should follow these rules:
23595 * Try to fetch any prerequisite types we may need to build this DIE type
23596 before building the type and calling set_die_type.
23597 * After building type call set_die_type for current DIE as soon as
23598 possible before fetching more types to complete the current type.
23599 * Make the type as complete as possible before fetching more types. */
23600
23601 static struct type *
23602 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23603 {
23604 struct dwarf2_per_objfile *dwarf2_per_objfile
23605 = cu->per_cu->dwarf2_per_objfile;
23606 struct dwarf2_per_cu_offset_and_type **slot, ofs;
23607 struct objfile *objfile = dwarf2_per_objfile->objfile;
23608 struct attribute *attr;
23609 struct dynamic_prop prop;
23610
23611 /* For Ada types, make sure that the gnat-specific data is always
23612 initialized (if not already set). There are a few types where
23613 we should not be doing so, because the type-specific area is
23614 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23615 where the type-specific area is used to store the floatformat).
23616 But this is not a problem, because the gnat-specific information
23617 is actually not needed for these types. */
23618 if (need_gnat_info (cu)
23619 && TYPE_CODE (type) != TYPE_CODE_FUNC
23620 && TYPE_CODE (type) != TYPE_CODE_FLT
23621 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
23622 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
23623 && TYPE_CODE (type) != TYPE_CODE_METHOD
23624 && !HAVE_GNAT_AUX_INFO (type))
23625 INIT_GNAT_SPECIFIC (type);
23626
23627 /* Read DW_AT_allocated and set in type. */
23628 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23629 if (attr != NULL && attr->form_is_block ())
23630 {
23631 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
23632 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23633 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
23634 }
23635 else if (attr != NULL)
23636 {
23637 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
23638 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23639 sect_offset_str (die->sect_off));
23640 }
23641
23642 /* Read DW_AT_associated and set in type. */
23643 attr = dwarf2_attr (die, DW_AT_associated, cu);
23644 if (attr != NULL && attr->form_is_block ())
23645 {
23646 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
23647 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23648 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
23649 }
23650 else if (attr != NULL)
23651 {
23652 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
23653 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23654 sect_offset_str (die->sect_off));
23655 }
23656
23657 /* Read DW_AT_data_location and set in type. */
23658 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23659 if (attr_to_dynamic_prop (attr, die, cu, &prop,
23660 cu->per_cu->addr_type ()))
23661 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
23662
23663 if (dwarf2_per_objfile->die_type_hash == NULL)
23664 dwarf2_per_objfile->die_type_hash
23665 = htab_up (htab_create_alloc (127,
23666 per_cu_offset_and_type_hash,
23667 per_cu_offset_and_type_eq,
23668 NULL, xcalloc, xfree));
23669
23670 ofs.per_cu = cu->per_cu;
23671 ofs.sect_off = die->sect_off;
23672 ofs.type = type;
23673 slot = (struct dwarf2_per_cu_offset_and_type **)
23674 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
23675 if (*slot)
23676 complaint (_("A problem internal to GDB: DIE %s has type already set"),
23677 sect_offset_str (die->sect_off));
23678 *slot = XOBNEW (&objfile->objfile_obstack,
23679 struct dwarf2_per_cu_offset_and_type);
23680 **slot = ofs;
23681 return type;
23682 }
23683
23684 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23685 or return NULL if the die does not have a saved type. */
23686
23687 static struct type *
23688 get_die_type_at_offset (sect_offset sect_off,
23689 struct dwarf2_per_cu_data *per_cu)
23690 {
23691 struct dwarf2_per_cu_offset_and_type *slot, ofs;
23692 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23693
23694 if (dwarf2_per_objfile->die_type_hash == NULL)
23695 return NULL;
23696
23697 ofs.per_cu = per_cu;
23698 ofs.sect_off = sect_off;
23699 slot = ((struct dwarf2_per_cu_offset_and_type *)
23700 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
23701 if (slot)
23702 return slot->type;
23703 else
23704 return NULL;
23705 }
23706
23707 /* Look up the type for DIE in CU in die_type_hash,
23708 or return NULL if DIE does not have a saved type. */
23709
23710 static struct type *
23711 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23712 {
23713 return get_die_type_at_offset (die->sect_off, cu->per_cu);
23714 }
23715
23716 /* Add a dependence relationship from CU to REF_PER_CU. */
23717
23718 static void
23719 dwarf2_add_dependence (struct dwarf2_cu *cu,
23720 struct dwarf2_per_cu_data *ref_per_cu)
23721 {
23722 void **slot;
23723
23724 if (cu->dependencies == NULL)
23725 cu->dependencies
23726 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23727 NULL, &cu->comp_unit_obstack,
23728 hashtab_obstack_allocate,
23729 dummy_obstack_deallocate);
23730
23731 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23732 if (*slot == NULL)
23733 *slot = ref_per_cu;
23734 }
23735
23736 /* Subroutine of dwarf2_mark to pass to htab_traverse.
23737 Set the mark field in every compilation unit in the
23738 cache that we must keep because we are keeping CU. */
23739
23740 static int
23741 dwarf2_mark_helper (void **slot, void *data)
23742 {
23743 struct dwarf2_per_cu_data *per_cu;
23744
23745 per_cu = (struct dwarf2_per_cu_data *) *slot;
23746
23747 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23748 reading of the chain. As such dependencies remain valid it is not much
23749 useful to track and undo them during QUIT cleanups. */
23750 if (per_cu->cu == NULL)
23751 return 1;
23752
23753 if (per_cu->cu->mark)
23754 return 1;
23755 per_cu->cu->mark = true;
23756
23757 if (per_cu->cu->dependencies != NULL)
23758 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23759
23760 return 1;
23761 }
23762
23763 /* Set the mark field in CU and in every other compilation unit in the
23764 cache that we must keep because we are keeping CU. */
23765
23766 static void
23767 dwarf2_mark (struct dwarf2_cu *cu)
23768 {
23769 if (cu->mark)
23770 return;
23771 cu->mark = true;
23772 if (cu->dependencies != NULL)
23773 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
23774 }
23775
23776 static void
23777 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23778 {
23779 while (per_cu)
23780 {
23781 per_cu->cu->mark = false;
23782 per_cu = per_cu->cu->read_in_chain;
23783 }
23784 }
23785
23786 /* Trivial hash function for partial_die_info: the hash value of a DIE
23787 is its offset in .debug_info for this objfile. */
23788
23789 static hashval_t
23790 partial_die_hash (const void *item)
23791 {
23792 const struct partial_die_info *part_die
23793 = (const struct partial_die_info *) item;
23794
23795 return to_underlying (part_die->sect_off);
23796 }
23797
23798 /* Trivial comparison function for partial_die_info structures: two DIEs
23799 are equal if they have the same offset. */
23800
23801 static int
23802 partial_die_eq (const void *item_lhs, const void *item_rhs)
23803 {
23804 const struct partial_die_info *part_die_lhs
23805 = (const struct partial_die_info *) item_lhs;
23806 const struct partial_die_info *part_die_rhs
23807 = (const struct partial_die_info *) item_rhs;
23808
23809 return part_die_lhs->sect_off == part_die_rhs->sect_off;
23810 }
23811
23812 struct cmd_list_element *set_dwarf_cmdlist;
23813 struct cmd_list_element *show_dwarf_cmdlist;
23814
23815 static void
23816 set_dwarf_cmd (const char *args, int from_tty)
23817 {
23818 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
23819 gdb_stdout);
23820 }
23821
23822 static void
23823 show_dwarf_cmd (const char *args, int from_tty)
23824 {
23825 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
23826 }
23827
23828 static void
23829 show_check_physname (struct ui_file *file, int from_tty,
23830 struct cmd_list_element *c, const char *value)
23831 {
23832 fprintf_filtered (file,
23833 _("Whether to check \"physname\" is %s.\n"),
23834 value);
23835 }
23836
23837 void _initialize_dwarf2_read ();
23838 void
23839 _initialize_dwarf2_read ()
23840 {
23841 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
23842 Set DWARF specific variables.\n\
23843 Configure DWARF variables such as the cache size."),
23844 &set_dwarf_cmdlist, "maintenance set dwarf ",
23845 0/*allow-unknown*/, &maintenance_set_cmdlist);
23846
23847 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
23848 Show DWARF specific variables.\n\
23849 Show DWARF variables such as the cache size."),
23850 &show_dwarf_cmdlist, "maintenance show dwarf ",
23851 0/*allow-unknown*/, &maintenance_show_cmdlist);
23852
23853 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23854 &dwarf_max_cache_age, _("\
23855 Set the upper bound on the age of cached DWARF compilation units."), _("\
23856 Show the upper bound on the age of cached DWARF compilation units."), _("\
23857 A higher limit means that cached compilation units will be stored\n\
23858 in memory longer, and more total memory will be used. Zero disables\n\
23859 caching, which can slow down startup."),
23860 NULL,
23861 show_dwarf_max_cache_age,
23862 &set_dwarf_cmdlist,
23863 &show_dwarf_cmdlist);
23864
23865 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23866 Set debugging of the DWARF reader."), _("\
23867 Show debugging of the DWARF reader."), _("\
23868 When enabled (non-zero), debugging messages are printed during DWARF\n\
23869 reading and symtab expansion. A value of 1 (one) provides basic\n\
23870 information. A value greater than 1 provides more verbose information."),
23871 NULL,
23872 NULL,
23873 &setdebuglist, &showdebuglist);
23874
23875 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23876 Set debugging of the DWARF DIE reader."), _("\
23877 Show debugging of the DWARF DIE reader."), _("\
23878 When enabled (non-zero), DIEs are dumped after they are read in.\n\
23879 The value is the maximum depth to print."),
23880 NULL,
23881 NULL,
23882 &setdebuglist, &showdebuglist);
23883
23884 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23885 Set debugging of the dwarf line reader."), _("\
23886 Show debugging of the dwarf line reader."), _("\
23887 When enabled (non-zero), line number entries are dumped as they are read in.\n\
23888 A value of 1 (one) provides basic information.\n\
23889 A value greater than 1 provides more verbose information."),
23890 NULL,
23891 NULL,
23892 &setdebuglist, &showdebuglist);
23893
23894 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23895 Set cross-checking of \"physname\" code against demangler."), _("\
23896 Show cross-checking of \"physname\" code against demangler."), _("\
23897 When enabled, GDB's internal \"physname\" code is checked against\n\
23898 the demangler."),
23899 NULL, show_check_physname,
23900 &setdebuglist, &showdebuglist);
23901
23902 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23903 no_class, &use_deprecated_index_sections, _("\
23904 Set whether to use deprecated gdb_index sections."), _("\
23905 Show whether to use deprecated gdb_index sections."), _("\
23906 When enabled, deprecated .gdb_index sections are used anyway.\n\
23907 Normally they are ignored either because of a missing feature or\n\
23908 performance issue.\n\
23909 Warning: This option must be enabled before gdb reads the file."),
23910 NULL,
23911 NULL,
23912 &setlist, &showlist);
23913
23914 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
23915 &dwarf2_locexpr_funcs);
23916 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
23917 &dwarf2_loclist_funcs);
23918
23919 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
23920 &dwarf2_block_frame_base_locexpr_funcs);
23921 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
23922 &dwarf2_block_frame_base_loclist_funcs);
23923
23924 #if GDB_SELF_TEST
23925 selftests::register_test ("dw2_expand_symtabs_matching",
23926 selftests::dw2_expand_symtabs_matching::run_test);
23927 selftests::register_test ("dwarf2_find_containing_comp_unit",
23928 selftests::find_containing_comp_unit::run_test);
23929 #endif
23930 }
This page took 0.491601 seconds and 5 git commands to generate.